sgiioc4.c 18.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright (c) 2003-2006 Silicon Graphics, Inc.  All Rights Reserved.
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 *
 * For further information regarding this notice, see:
 *
 * http://oss.sgi.com/projects/GenInfo/NoticeExplan
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/hdreg.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/blkdev.h>
J
Jens Axboe 已提交
32
#include <linux/scatterlist.h>
33
#include <linux/ioc4.h>
L
Linus Torvalds 已提交
34 35 36 37
#include <asm/io.h>

#include <linux/ide.h>

38 39
#define DRV_NAME "SGIIOC4"

L
Linus Torvalds 已提交
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 77 78 79 80 81 82 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
/* IOC4 Specific Definitions */
#define IOC4_CMD_OFFSET		0x100
#define IOC4_CTRL_OFFSET	0x120
#define IOC4_DMA_OFFSET		0x140
#define IOC4_INTR_OFFSET	0x0

#define IOC4_TIMING		0x00
#define IOC4_DMA_PTR_L		0x01
#define IOC4_DMA_PTR_H		0x02
#define IOC4_DMA_ADDR_L		0x03
#define IOC4_DMA_ADDR_H		0x04
#define IOC4_BC_DEV		0x05
#define IOC4_BC_MEM		0x06
#define	IOC4_DMA_CTRL		0x07
#define	IOC4_DMA_END_ADDR	0x08

/* Bits in the IOC4 Control/Status Register */
#define	IOC4_S_DMA_START	0x01
#define	IOC4_S_DMA_STOP		0x02
#define	IOC4_S_DMA_DIR		0x04
#define	IOC4_S_DMA_ACTIVE	0x08
#define	IOC4_S_DMA_ERROR	0x10
#define	IOC4_ATA_MEMERR		0x02

/* Read/Write Directions */
#define	IOC4_DMA_WRITE		0x04
#define	IOC4_DMA_READ		0x00

/* Interrupt Register Offsets */
#define IOC4_INTR_REG		0x03
#define	IOC4_INTR_SET		0x05
#define	IOC4_INTR_CLEAR		0x07

#define IOC4_IDE_CACHELINE_SIZE	128
#define IOC4_CMD_CTL_BLK_SIZE	0x20
#define IOC4_SUPPORTED_FIRMWARE_REV 46

typedef struct {
	u32 timing_reg0;
	u32 timing_reg1;
	u32 low_mem_ptr;
	u32 high_mem_ptr;
	u32 low_mem_addr;
	u32 high_mem_addr;
	u32 dev_byte_count;
	u32 mem_byte_count;
	u32 status;
} ioc4_dma_regs_t;

/* Each Physical Region Descriptor Entry size is 16 bytes (2 * 64 bits) */
/* IOC4 has only 1 IDE channel */
#define IOC4_PRD_BYTES       16
#define IOC4_PRD_ENTRIES     (PAGE_SIZE /(4*IOC4_PRD_BYTES))


static void
sgiioc4_init_hwif_ports(hw_regs_t * hw, unsigned long data_port,
			unsigned long ctrl_port, unsigned long irq_port)
{
	unsigned long reg = data_port;
	int i;

	/* Registers are word (32 bit) aligned */
	for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
		hw->io_ports[i] = reg + i * 4;

	if (ctrl_port)
		hw->io_ports[IDE_CONTROL_OFFSET] = ctrl_port;

	if (irq_port)
		hw->io_ports[IDE_IRQ_OFFSET] = irq_port;
}

static void
sgiioc4_maskproc(ide_drive_t * drive, int mask)
{
116 117
	writeb(mask ? (drive->ctl | 2) : (drive->ctl & ~2),
	       (void __iomem *)IDE_CONTROL_REG);
L
Linus Torvalds 已提交
118 119 120 121 122 123
}


static int
sgiioc4_checkirq(ide_hwif_t * hwif)
{
124 125
	unsigned long intr_addr =
		hwif->io_ports[IDE_IRQ_OFFSET] + IOC4_INTR_REG * 4;
L
Linus Torvalds 已提交
126

127
	if ((u8)readl((void __iomem *)intr_addr) & 0x03)
L
Linus Torvalds 已提交
128 129 130 131 132
		return 1;

	return 0;
}

133
static u8 sgiioc4_INB(unsigned long);
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142 143

static int
sgiioc4_clearirq(ide_drive_t * drive)
{
	u32 intr_reg;
	ide_hwif_t *hwif = HWIF(drive);
	unsigned long other_ir =
	    hwif->io_ports[IDE_IRQ_OFFSET] + (IOC4_INTR_REG << 2);

	/* Code to check for PCI error conditions */
144
	intr_reg = readl((void __iomem *)other_ir);
L
Linus Torvalds 已提交
145 146
	if (intr_reg & 0x03) { /* Valid IOC4-IDE interrupt */
		/*
147
		 * Using sgiioc4_INB to read the IDE_STATUS_REG has a side effect
L
Linus Torvalds 已提交
148 149 150 151 152
		 * of clearing the interrupt.  The first read should clear it
		 * if it is set.  The second read should return a "clear" status
		 * if it got cleared.  If not, then spin for a bit trying to
		 * clear it.
		 */
153
		u8 stat = sgiioc4_INB(IDE_STATUS_REG);
L
Linus Torvalds 已提交
154
		int count = 0;
155
		stat = sgiioc4_INB(IDE_STATUS_REG);
L
Linus Torvalds 已提交
156 157
		while ((stat & 0x80) && (count++ < 100)) {
			udelay(1);
158
			stat = sgiioc4_INB(IDE_STATUS_REG);
L
Linus Torvalds 已提交
159 160 161
		}

		if (intr_reg & 0x02) {
162
			struct pci_dev *dev = to_pci_dev(hwif->dev);
L
Linus Torvalds 已提交
163 164 165 166 167
			/* Error when transferring DMA data on PCI bus */
			u32 pci_err_addr_low, pci_err_addr_high,
			    pci_stat_cmd_reg;

			pci_err_addr_low =
168
				readl((void __iomem *)hwif->io_ports[IDE_IRQ_OFFSET]);
L
Linus Torvalds 已提交
169
			pci_err_addr_high =
170
				readl((void __iomem *)(hwif->io_ports[IDE_IRQ_OFFSET] + 4));
171
			pci_read_config_dword(dev, PCI_COMMAND,
L
Linus Torvalds 已提交
172 173 174 175 176 177 178 179 180 181
					      &pci_stat_cmd_reg);
			printk(KERN_ERR
			       "%s(%s) : PCI Bus Error when doing DMA:"
				   " status-cmd reg is 0x%x\n",
			       __FUNCTION__, drive->name, pci_stat_cmd_reg);
			printk(KERN_ERR
			       "%s(%s) : PCI Error Address is 0x%x%x\n",
			       __FUNCTION__, drive->name,
			       pci_err_addr_high, pci_err_addr_low);
			/* Clear the PCI Error indicator */
182
			pci_write_config_dword(dev, PCI_COMMAND, 0x00000146);
L
Linus Torvalds 已提交
183 184 185
		}

		/* Clear the Interrupt, Error bits on the IOC4 */
186
		writel(0x03, (void __iomem *)other_ir);
L
Linus Torvalds 已提交
187

188
		intr_reg = readl((void __iomem *)other_ir);
L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196
	}

	return intr_reg & 3;
}

static void sgiioc4_ide_dma_start(ide_drive_t * drive)
{
	ide_hwif_t *hwif = HWIF(drive);
197 198
	unsigned long ioc4_dma_addr = hwif->dma_base + IOC4_DMA_CTRL * 4;
	unsigned int reg = readl((void __iomem *)ioc4_dma_addr);
L
Linus Torvalds 已提交
199 200
	unsigned int temp_reg = reg | IOC4_S_DMA_START;

201
	writel(temp_reg, (void __iomem *)ioc4_dma_addr);
L
Linus Torvalds 已提交
202 203 204 205 206
}

static u32
sgiioc4_ide_dma_stop(ide_hwif_t *hwif, u64 dma_base)
{
207
	unsigned long ioc4_dma_addr = dma_base + IOC4_DMA_CTRL * 4;
L
Linus Torvalds 已提交
208 209 210 211
	u32	ioc4_dma;
	int	count;

	count = 0;
212
	ioc4_dma = readl((void __iomem *)ioc4_dma_addr);
L
Linus Torvalds 已提交
213 214
	while ((ioc4_dma & IOC4_S_DMA_STOP) && (count++ < 200)) {
		udelay(1);
215
		ioc4_dma = readl((void __iomem *)ioc4_dma_addr);
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225
	}
	return ioc4_dma;
}

/* Stops the IOC4 DMA Engine */
static int
sgiioc4_ide_dma_end(ide_drive_t * drive)
{
	u32 ioc4_dma, bc_dev, bc_mem, num, valid = 0, cnt = 0;
	ide_hwif_t *hwif = HWIF(drive);
226
	unsigned long dma_base = hwif->dma_base;
L
Linus Torvalds 已提交
227
	int dma_stat = 0;
228
	unsigned long *ending_dma = ide_get_hwifdata(hwif);
L
Linus Torvalds 已提交
229

230
	writel(IOC4_S_DMA_STOP, (void __iomem *)(dma_base + IOC4_DMA_CTRL * 4));
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

	ioc4_dma = sgiioc4_ide_dma_stop(hwif, dma_base);

	if (ioc4_dma & IOC4_S_DMA_STOP) {
		printk(KERN_ERR
		       "%s(%s): IOC4 DMA STOP bit is still 1 :"
		       "ioc4_dma_reg 0x%x\n",
		       __FUNCTION__, drive->name, ioc4_dma);
		dma_stat = 1;
	}

	/*
	 * The IOC4 will DMA 1's to the ending dma area to indicate that
	 * previous data DMA is complete.  This is necessary because of relaxed
	 * ordering between register reads and DMA writes on the Altix.
	 */
	while ((cnt++ < 200) && (!valid)) {
		for (num = 0; num < 16; num++) {
			if (ending_dma[num]) {
				valid = 1;
				break;
			}
		}
		udelay(1);
	}
	if (!valid) {
		printk(KERN_ERR "%s(%s) : DMA incomplete\n", __FUNCTION__,
		       drive->name);
		dma_stat = 1;
	}

262 263
	bc_dev = readl((void __iomem *)(dma_base + IOC4_BC_DEV * 4));
	bc_mem = readl((void __iomem *)(dma_base + IOC4_BC_MEM * 4));
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

	if ((bc_dev & 0x01FF) || (bc_mem & 0x1FF)) {
		if (bc_dev > bc_mem + 8) {
			printk(KERN_ERR
			       "%s(%s): WARNING!! byte_count_dev %d "
			       "!= byte_count_mem %d\n",
			       __FUNCTION__, drive->name, bc_dev, bc_mem);
		}
	}

	drive->waiting_for_dma = 0;
	ide_destroy_dmatable(drive);

	return dma_stat;
}

280
static void sgiioc4_set_dma_mode(ide_drive_t *drive, const u8 speed)
281 282 283
{
}

L
Linus Torvalds 已提交
284 285 286 287 288 289 290
/* returns 1 if dma irq issued, 0 otherwise */
static int
sgiioc4_ide_dma_test_irq(ide_drive_t * drive)
{
	return sgiioc4_checkirq(HWIF(drive));
}

291
static void sgiioc4_dma_host_set(ide_drive_t *drive, int on)
L
Linus Torvalds 已提交
292
{
293 294
	if (!on)
		sgiioc4_clearirq(drive);
L
Linus Torvalds 已提交
295 296 297 298 299 300 301 302 303
}

static void
sgiioc4_resetproc(ide_drive_t * drive)
{
	sgiioc4_ide_dma_end(drive);
	sgiioc4_clearirq(drive);
}

304 305 306 307 308 309 310 311
static void
sgiioc4_dma_lost_irq(ide_drive_t * drive)
{
	sgiioc4_resetproc(drive);

	ide_dma_lost_irq(drive);
}

L
Linus Torvalds 已提交
312 313 314
static u8
sgiioc4_INB(unsigned long port)
{
315
	u8 reg = (u8) readb((void __iomem *) port);
L
Linus Torvalds 已提交
316 317 318 319

	if ((port & 0xFFF) == 0x11C) {	/* Status register of IOC4 */
		if (reg & 0x51) {	/* Not busy...check for interrupt */
			unsigned long other_ir = port - 0x110;
320
			unsigned int intr_reg = (u32) readl((void __iomem *) other_ir);
L
Linus Torvalds 已提交
321 322 323

			/* Clear the Interrupt, Error bits on the IOC4 */
			if (intr_reg & 0x03) {
324 325
				writel(0x03, (void __iomem *) other_ir);
				intr_reg = (u32) readl((void __iomem *) other_ir);
L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333
			}
		}
	}

	return reg;
}

/* Creates a dma map for the scatter-gather list entries */
334
static int __devinit
L
Linus Torvalds 已提交
335 336
ide_dma_sgiioc4(ide_hwif_t * hwif, unsigned long dma_base)
{
337
	struct pci_dev *dev = to_pci_dev(hwif->dev);
338
	void __iomem *virt_dma_base;
L
Linus Torvalds 已提交
339
	int num_ports = sizeof (ioc4_dma_regs_t);
340
	void *pad;
L
Linus Torvalds 已提交
341 342 343 344

	printk(KERN_INFO "%s: BM-DMA at 0x%04lx-0x%04lx\n", hwif->name,
	       dma_base, dma_base + num_ports - 1);

345
	if (!request_mem_region(dma_base, num_ports, hwif->name)) {
L
Linus Torvalds 已提交
346 347 348 349 350
		printk(KERN_ERR
		       "%s(%s) -- ERROR, Addresses 0x%p to 0x%p "
		       "ALREADY in use\n",
		       __FUNCTION__, hwif->name, (void *) dma_base,
		       (void *) dma_base + num_ports - 1);
351
		return -1;
L
Linus Torvalds 已提交
352 353
	}

354 355 356 357 358 359 360 361 362
	virt_dma_base = ioremap(dma_base, num_ports);
	if (virt_dma_base == NULL) {
		printk(KERN_ERR
		       "%s(%s) -- ERROR, Unable to map addresses 0x%lx to 0x%lx\n",
		       __FUNCTION__, hwif->name, dma_base, dma_base + num_ports - 1);
		goto dma_remap_failure;
	}
	hwif->dma_base = (unsigned long) virt_dma_base;

363
	hwif->dmatable_cpu = pci_alloc_consistent(dev,
L
Linus Torvalds 已提交
364 365 366 367
					  IOC4_PRD_ENTRIES * IOC4_PRD_BYTES,
					  &hwif->dmatable_dma);

	if (!hwif->dmatable_cpu)
368
		goto dma_pci_alloc_failure;
L
Linus Torvalds 已提交
369 370 371

	hwif->sg_max_nents = IOC4_PRD_ENTRIES;

372
	pad = pci_alloc_consistent(dev, IOC4_IDE_CACHELINE_SIZE,
373
				   (dma_addr_t *) &(hwif->dma_status));
L
Linus Torvalds 已提交
374

375 376
	if (pad) {
		ide_set_hwifdata(hwif, pad);
377
		return 0;
378
	}
L
Linus Torvalds 已提交
379

380
	pci_free_consistent(dev, IOC4_PRD_ENTRIES * IOC4_PRD_BYTES,
L
Linus Torvalds 已提交
381 382 383 384 385 386 387
			    hwif->dmatable_cpu, hwif->dmatable_dma);
	printk(KERN_INFO
	       "%s() -- Error! Unable to allocate DMA Maps for drive %s\n",
	       __FUNCTION__, hwif->name);
	printk(KERN_INFO
	       "Changing from DMA to PIO mode for Drive %s\n", hwif->name);

388 389 390 391 392 393
dma_pci_alloc_failure:
	iounmap(virt_dma_base);

dma_remap_failure:
	release_mem_region(dma_base, num_ports);

394
	return -1;
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402
}

/* Initializes the IOC4 DMA Engine */
static void
sgiioc4_configure_for_dma(int dma_direction, ide_drive_t * drive)
{
	u32 ioc4_dma;
	ide_hwif_t *hwif = HWIF(drive);
403 404
	unsigned long dma_base = hwif->dma_base;
	unsigned long ioc4_dma_addr = dma_base + IOC4_DMA_CTRL * 4;
L
Linus Torvalds 已提交
405 406
	u32 dma_addr, ending_dma_addr;

407
	ioc4_dma = readl((void __iomem *)ioc4_dma_addr);
L
Linus Torvalds 已提交
408 409 410 411 412

	if (ioc4_dma & IOC4_S_DMA_ACTIVE) {
		printk(KERN_WARNING
			"%s(%s):Warning!! DMA from previous transfer was still active\n",
		       __FUNCTION__, drive->name);
413
		writel(IOC4_S_DMA_STOP, (void __iomem *)ioc4_dma_addr);
L
Linus Torvalds 已提交
414 415 416 417 418 419 420 421
		ioc4_dma = sgiioc4_ide_dma_stop(hwif, dma_base);

		if (ioc4_dma & IOC4_S_DMA_STOP)
			printk(KERN_ERR
			       "%s(%s) : IOC4 Dma STOP bit is still 1\n",
			       __FUNCTION__, drive->name);
	}

422
	ioc4_dma = readl((void __iomem *)ioc4_dma_addr);
L
Linus Torvalds 已提交
423 424 425 426 427
	if (ioc4_dma & IOC4_S_DMA_ERROR) {
		printk(KERN_WARNING
		       "%s(%s) : Warning!! - DMA Error during Previous"
		       " transfer | status 0x%x\n",
		       __FUNCTION__, drive->name, ioc4_dma);
428
		writel(IOC4_S_DMA_STOP, (void __iomem *)ioc4_dma_addr);
L
Linus Torvalds 已提交
429 430 431 432 433 434 435 436 437 438
		ioc4_dma = sgiioc4_ide_dma_stop(hwif, dma_base);

		if (ioc4_dma & IOC4_S_DMA_STOP)
			printk(KERN_ERR
			       "%s(%s) : IOC4 DMA STOP bit is still 1\n",
			       __FUNCTION__, drive->name);
	}

	/* Address of the Scatter Gather List */
	dma_addr = cpu_to_le32(hwif->dmatable_dma);
439
	writel(dma_addr, (void __iomem *)(dma_base + IOC4_DMA_PTR_L * 4));
L
Linus Torvalds 已提交
440 441

	/* Address of the Ending DMA */
442
	memset(ide_get_hwifdata(hwif), 0, IOC4_IDE_CACHELINE_SIZE);
L
Linus Torvalds 已提交
443
	ending_dma_addr = cpu_to_le32(hwif->dma_status);
444
	writel(ending_dma_addr, (void __iomem *)(dma_base + IOC4_DMA_END_ADDR * 4));
L
Linus Torvalds 已提交
445

446
	writel(dma_direction, (void __iomem *)ioc4_dma_addr);
L
Linus Torvalds 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	drive->waiting_for_dma = 1;
}

/* IOC4 Scatter Gather list Format 					 */
/* 128 Bit entries to support 64 bit addresses in the future		 */
/* The Scatter Gather list Entry should be in the BIG-ENDIAN Format	 */
/* --------------------------------------------------------------------- */
/* | Upper 32 bits - Zero           |	 	Lower 32 bits- address | */
/* --------------------------------------------------------------------- */
/* | Upper 32 bits - Zero	    |EOL| 15 unused     | 16 Bit Length| */
/* --------------------------------------------------------------------- */
/* Creates the scatter gather list, DMA Table */
static unsigned int
sgiioc4_build_dma_table(ide_drive_t * drive, struct request *rq, int ddir)
{
	ide_hwif_t *hwif = HWIF(drive);
	unsigned int *table = hwif->dmatable_cpu;
	unsigned int count = 0, i = 1;
	struct scatterlist *sg;

	hwif->sg_nents = i = ide_build_sglist(drive, rq);

	if (!i)
		return 0;	/* sglist of length Zero */

	sg = hwif->sg_table;
	while (i && sg_dma_len(sg)) {
		dma_addr_t cur_addr;
		int cur_len;
		cur_addr = sg_dma_address(sg);
		cur_len = sg_dma_len(sg);

		while (cur_len) {
			if (count++ >= IOC4_PRD_ENTRIES) {
				printk(KERN_WARNING
				       "%s: DMA table too small\n",
				       drive->name);
				goto use_pio_instead;
			} else {
486
				u32 bcount =
L
Linus Torvalds 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500
				    0x10000 - (cur_addr & 0xffff);

				if (bcount > cur_len)
					bcount = cur_len;

				/* put the addr, length in
				 * the IOC4 dma-table format */
				*table = 0x0;
				table++;
				*table = cpu_to_be32(cur_addr);
				table++;
				*table = 0x0;
				table++;

501
				*table = cpu_to_be32(bcount);
L
Linus Torvalds 已提交
502 503 504 505 506 507 508
				table++;

				cur_addr += bcount;
				cur_len -= bcount;
			}
		}

J
Jens Axboe 已提交
509
		sg = sg_next(sg);
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518 519
		i--;
	}

	if (count) {
		table--;
		*table |= cpu_to_be32(0x80000000);
		return count;
	}

use_pio_instead:
520
	ide_destroy_dmatable(drive);
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

	return 0;		/* revert to PIO for this request */
}

static int sgiioc4_ide_dma_setup(ide_drive_t *drive)
{
	struct request *rq = HWGROUP(drive)->rq;
	unsigned int count = 0;
	int ddir;

	if (rq_data_dir(rq))
		ddir = PCI_DMA_TODEVICE;
	else
		ddir = PCI_DMA_FROMDEVICE;

	if (!(count = sgiioc4_build_dma_table(drive, rq, ddir))) {
		/* try PIO instead of DMA */
		ide_map_sg(drive, rq);
		return 1;
	}

	if (rq_data_dir(rq))
		/* Writes TO the IOC4 FROM Main Memory */
		ddir = IOC4_DMA_READ;
	else
		/* Writes FROM the IOC4 TO Main Memory */
		ddir = IOC4_DMA_WRITE;

	sgiioc4_configure_for_dma(ddir, drive);

	return 0;
}

static void __devinit
ide_init_sgiioc4(ide_hwif_t * hwif)
{
557
	hwif->mmio = 1;
558
	hwif->set_pio_mode = NULL; /* Sets timing for PIO mode */
559
	hwif->set_dma_mode = &sgiioc4_set_dma_mode;
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568
	hwif->selectproc = NULL;/* Use the default routine to select drive */
	hwif->reset_poll = NULL;/* No HBA specific reset_poll needed */
	hwif->pre_reset = NULL;	/* No HBA specific pre_set needed */
	hwif->resetproc = &sgiioc4_resetproc;/* Reset DMA engine,
						clear interrupts */
	hwif->maskproc = &sgiioc4_maskproc;	/* Mask on/off NIEN register */
	hwif->quirkproc = NULL;
	hwif->busproc = NULL;

569 570 571 572 573
	hwif->INB = &sgiioc4_INB;

	if (hwif->dma_base == 0)
		return;

574
	hwif->dma_host_set = &sgiioc4_dma_host_set;
L
Linus Torvalds 已提交
575 576 577 578
	hwif->dma_setup = &sgiioc4_ide_dma_setup;
	hwif->dma_start = &sgiioc4_ide_dma_start;
	hwif->ide_dma_end = &sgiioc4_ide_dma_end;
	hwif->ide_dma_test_irq = &sgiioc4_ide_dma_test_irq;
579
	hwif->dma_lost_irq = &sgiioc4_dma_lost_irq;
580
	hwif->dma_timeout = &ide_dma_timeout;
L
Linus Torvalds 已提交
581 582
}

583 584 585 586 587 588 589
static const struct ide_port_info sgiioc4_port_info __devinitdata = {
	.chipset		= ide_pci,
	.host_flags		= IDE_HFLAG_NO_DMA | /* no SFF-style DMA */
				  IDE_HFLAG_NO_AUTOTUNE,
	.mwdma_mask		= ATA_MWDMA2_ONLY,
};

L
Linus Torvalds 已提交
590
static int __devinit
591
sgiioc4_ide_setup_pci_device(struct pci_dev *dev)
L
Linus Torvalds 已提交
592
{
593 594 595
	unsigned long cmd_base, dma_base, irqport;
	unsigned long bar0, cmd_phys_base, ctl;
	void __iomem *virt_base;
L
Linus Torvalds 已提交
596 597
	ide_hwif_t *hwif;
	int h;
598
	u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
599
	hw_regs_t hw;
600
	struct ide_port_info d = sgiioc4_port_info;
L
Linus Torvalds 已提交
601

602 603 604
	/*
	 * Find an empty HWIF; if none available, return -ENOMEM.
	 */
L
Linus Torvalds 已提交
605 606 607 608 609
	for (h = 0; h < MAX_HWIFS; ++h) {
		hwif = &ide_hwifs[h];
		if (hwif->chipset == ide_unknown)
			break;
	}
610
	if (h == MAX_HWIFS) {
611 612
		printk(KERN_ERR "%s: too many IDE interfaces, no room in table\n",
				DRV_NAME);
613 614
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
615 616

	/*  Get the CmdBlk and CtrlBlk Base Registers */
617 618 619 620
	bar0 = pci_resource_start(dev, 0);
	virt_base = ioremap(bar0, pci_resource_len(dev, 0));
	if (virt_base == NULL) {
		printk(KERN_ERR "%s: Unable to remap BAR 0 address: 0x%lx\n",
621
				DRV_NAME, bar0);
622 623 624 625 626
		return -ENOMEM;
	}
	cmd_base = (unsigned long) virt_base + IOC4_CMD_OFFSET;
	ctl = (unsigned long) virt_base + IOC4_CTRL_OFFSET;
	irqport = (unsigned long) virt_base + IOC4_INTR_OFFSET;
L
Linus Torvalds 已提交
627 628
	dma_base = pci_resource_start(dev, 0) + IOC4_DMA_OFFSET;

629 630 631
	cmd_phys_base = bar0 + IOC4_CMD_OFFSET;
	if (!request_mem_region(cmd_phys_base, IOC4_CMD_CTL_BLK_SIZE,
	    hwif->name)) {
L
Linus Torvalds 已提交
632
		printk(KERN_ERR
633
			"%s : %s -- ERROR, Addresses "
L
Linus Torvalds 已提交
634
			"0x%p to 0x%p ALREADY in use\n",
635 636
		       __FUNCTION__, hwif->name, (void *) cmd_phys_base,
		       (void *) cmd_phys_base + IOC4_CMD_CTL_BLK_SIZE);
L
Linus Torvalds 已提交
637 638 639
		return -ENOMEM;
	}

640 641 642
	/* Initialize the IO registers */
	memset(&hw, 0, sizeof(hw));
	sgiioc4_init_hwif_ports(&hw, cmd_base, ctl, irqport);
643 644 645 646
	hw.irq = dev->irq;
	hw.chipset = ide_pci;
	hw.dev = &dev->dev;
	ide_init_port_hw(hwif, &hw);
L
Linus Torvalds 已提交
647

648
	hwif->dev = &dev->dev;
L
Linus Torvalds 已提交
649

650 651 652
	/* The IOC4 uses MMIO rather than Port IO. */
	default_hwif_mmiops(hwif);

L
Linus Torvalds 已提交
653
	/* Initializing chipset IRQ Registers */
654
	writel(0x03, (void __iomem *)(irqport + IOC4_INTR_SET * 4));
L
Linus Torvalds 已提交
655

656
	if (dma_base == 0 || ide_dma_sgiioc4(hwif, dma_base)) {
L
Linus Torvalds 已提交
657
		printk(KERN_INFO "%s: %s Bus-Master DMA disabled\n",
658
				 hwif->name, DRV_NAME);
659 660
		d.mwdma_mask = 0;
	}
L
Linus Torvalds 已提交
661

662 663
	ide_init_sgiioc4(hwif);

664
	idx[0] = hwif->index;
L
Linus Torvalds 已提交
665

666
	if (ide_device_add(idx, &d))
667
		return -EIO;
L
Linus Torvalds 已提交
668 669 670 671 672

	return 0;
}

static unsigned int __devinit
673
pci_init_sgiioc4(struct pci_dev *dev)
L
Linus Torvalds 已提交
674 675 676 677
{
	int ret;

	printk(KERN_INFO "%s: IDE controller at PCI slot %s, revision %d\n",
678 679 680
			 DRV_NAME, pci_name(dev), dev->revision);

	if (dev->revision < IOC4_SUPPORTED_FIRMWARE_REV) {
L
Linus Torvalds 已提交
681
		printk(KERN_ERR "Skipping %s IDE controller in slot %s: "
682 683 684
				"firmware is obsolete - please upgrade to "
				"revision46 or higher\n",
				DRV_NAME, pci_name(dev));
L
Linus Torvalds 已提交
685 686 687
		ret = -EAGAIN;
		goto out;
	}
688
	ret = sgiioc4_ide_setup_pci_device(dev);
L
Linus Torvalds 已提交
689 690 691 692 693
out:
	return ret;
}

int
694
ioc4_ide_attach_one(struct ioc4_driver_data *idd)
L
Linus Torvalds 已提交
695
{
696 697 698 699 700 701
	/* PCI-RT does not bring out IDE connection.
	 * Do not attach to this particular IOC4.
	 */
	if (idd->idd_variant == IOC4_VARIANT_PCI_RT)
		return 0;

702
	return pci_init_sgiioc4(idd->idd_pdev);
L
Linus Torvalds 已提交
703 704
}

705 706 707 708 709 710 711
static struct ioc4_submodule ioc4_ide_submodule = {
	.is_name = "IOC4_ide",
	.is_owner = THIS_MODULE,
	.is_probe = ioc4_ide_attach_one,
/*	.is_remove = ioc4_ide_remove_one,	*/
};

712
static int __init ioc4_ide_init(void)
713 714 715 716
{
	return ioc4_register_submodule(&ioc4_ide_submodule);
}

717
late_initcall(ioc4_ide_init); /* Call only after IDE init is done */
L
Linus Torvalds 已提交
718

719
MODULE_AUTHOR("Aniket Malatpure/Jeremy Higdon");
L
Linus Torvalds 已提交
720 721
MODULE_DESCRIPTION("IDE PCI driver module for SGI IOC4 Base-IO Card");
MODULE_LICENSE("GPL");