sgiioc4.c 16.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 * Copyright (c) 2003-2006 Silicon Graphics, Inc.  All Rights Reserved.
3
 * Copyright (C) 2008 MontaVista Software, Inc.
Linus Torvalds's avatar
Linus Torvalds committed
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
 *
 * 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/init.h>
#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/blkdev.h>
Jens Axboe's avatar
Jens Axboe committed
30
#include <linux/scatterlist.h>
31
#include <linux/ioc4.h>
Linus Torvalds's avatar
Linus Torvalds committed
32 33 34 35
#include <asm/io.h>

#include <linux/ide.h>

36 37
#define DRV_NAME "SGIIOC4"

Linus Torvalds's avatar
Linus Torvalds committed
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/* 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 */
101 102
	for (i = 0; i <= 7; i++)
		hw->io_ports_array[i] = reg + i * 4;
Linus Torvalds's avatar
Linus Torvalds committed
103

104 105
	hw->io_ports.ctl_addr = ctrl_port;
	hw->io_ports.irq_addr = irq_port;
Linus Torvalds's avatar
Linus Torvalds committed
106 107 108 109 110
}

static int
sgiioc4_checkirq(ide_hwif_t * hwif)
{
111
	unsigned long intr_addr =
112
		hwif->io_ports.irq_addr + IOC4_INTR_REG * 4;
Linus Torvalds's avatar
Linus Torvalds committed
113

114
	if ((u8)readl((void __iomem *)intr_addr) & 0x03)
Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118 119
		return 1;

	return 0;
}

120
static u8 sgiioc4_read_status(ide_hwif_t *);
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125

static int
sgiioc4_clearirq(ide_drive_t * drive)
{
	u32 intr_reg;
126
	ide_hwif_t *hwif = drive->hwif;
127 128
	struct ide_io_ports *io_ports = &hwif->io_ports;
	unsigned long other_ir = io_ports->irq_addr + (IOC4_INTR_REG << 2);
Linus Torvalds's avatar
Linus Torvalds committed
129 130

	/* Code to check for PCI error conditions */
131
	intr_reg = readl((void __iomem *)other_ir);
Linus Torvalds's avatar
Linus Torvalds committed
132 133
	if (intr_reg & 0x03) { /* Valid IOC4-IDE interrupt */
		/*
134 135
		 * Using sgiioc4_read_status to read the Status register has a
		 * side effect of clearing the interrupt.  The first read should
136 137 138
		 * 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.
Linus Torvalds's avatar
Linus Torvalds committed
139
		 */
140
		u8 stat = sgiioc4_read_status(hwif);
Linus Torvalds's avatar
Linus Torvalds committed
141
		int count = 0;
142 143

		stat = sgiioc4_read_status(hwif);
144
		while ((stat & ATA_BUSY) && (count++ < 100)) {
Linus Torvalds's avatar
Linus Torvalds committed
145
			udelay(1);
146
			stat = sgiioc4_read_status(hwif);
Linus Torvalds's avatar
Linus Torvalds committed
147 148 149
		}

		if (intr_reg & 0x02) {
150
			struct pci_dev *dev = to_pci_dev(hwif->dev);
Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155
			/* 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 =
156
				readl((void __iomem *)io_ports->irq_addr);
Linus Torvalds's avatar
Linus Torvalds committed
157
			pci_err_addr_high =
158
				readl((void __iomem *)(io_ports->irq_addr + 4));
159
			pci_read_config_dword(dev, PCI_COMMAND,
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163
					      &pci_stat_cmd_reg);
			printk(KERN_ERR
			       "%s(%s) : PCI Bus Error when doing DMA:"
				   " status-cmd reg is 0x%x\n",
164
			       __func__, drive->name, pci_stat_cmd_reg);
Linus Torvalds's avatar
Linus Torvalds committed
165 166
			printk(KERN_ERR
			       "%s(%s) : PCI Error Address is 0x%x%x\n",
167
			       __func__, drive->name,
Linus Torvalds's avatar
Linus Torvalds committed
168 169
			       pci_err_addr_high, pci_err_addr_low);
			/* Clear the PCI Error indicator */
170
			pci_write_config_dword(dev, PCI_COMMAND, 0x00000146);
Linus Torvalds's avatar
Linus Torvalds committed
171 172 173
		}

		/* Clear the Interrupt, Error bits on the IOC4 */
174
		writel(0x03, (void __iomem *)other_ir);
Linus Torvalds's avatar
Linus Torvalds committed
175

176
		intr_reg = readl((void __iomem *)other_ir);
Linus Torvalds's avatar
Linus Torvalds committed
177 178 179 180 181
	}

	return intr_reg & 3;
}

182
static void sgiioc4_dma_start(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
183
{
184
	ide_hwif_t *hwif = drive->hwif;
185 186
	unsigned long ioc4_dma_addr = hwif->dma_base + IOC4_DMA_CTRL * 4;
	unsigned int reg = readl((void __iomem *)ioc4_dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
187 188
	unsigned int temp_reg = reg | IOC4_S_DMA_START;

189
	writel(temp_reg, (void __iomem *)ioc4_dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
190 191 192 193 194
}

static u32
sgiioc4_ide_dma_stop(ide_hwif_t *hwif, u64 dma_base)
{
195
	unsigned long ioc4_dma_addr = dma_base + IOC4_DMA_CTRL * 4;
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199
	u32	ioc4_dma;
	int	count;

	count = 0;
200
	ioc4_dma = readl((void __iomem *)ioc4_dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
201 202
	while ((ioc4_dma & IOC4_S_DMA_STOP) && (count++ < 200)) {
		udelay(1);
203
		ioc4_dma = readl((void __iomem *)ioc4_dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
204 205 206 207 208
	}
	return ioc4_dma;
}

/* Stops the IOC4 DMA Engine */
209
static int sgiioc4_dma_end(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
210 211
{
	u32 ioc4_dma, bc_dev, bc_mem, num, valid = 0, cnt = 0;
212
	ide_hwif_t *hwif = drive->hwif;
213
	unsigned long dma_base = hwif->dma_base;
Linus Torvalds's avatar
Linus Torvalds committed
214
	int dma_stat = 0;
215
	unsigned long *ending_dma = ide_get_hwifdata(hwif);
Linus Torvalds's avatar
Linus Torvalds committed
216

217
	writel(IOC4_S_DMA_STOP, (void __iomem *)(dma_base + IOC4_DMA_CTRL * 4));
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220 221 222 223 224

	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",
225
		       __func__, drive->name, ioc4_dma);
Linus Torvalds's avatar
Linus Torvalds committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		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) {
244
		printk(KERN_ERR "%s(%s) : DMA incomplete\n", __func__,
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248
		       drive->name);
		dma_stat = 1;
	}

249 250
	bc_dev = readl((void __iomem *)(dma_base + IOC4_BC_DEV * 4));
	bc_mem = readl((void __iomem *)(dma_base + IOC4_BC_MEM * 4));
Linus Torvalds's avatar
Linus Torvalds committed
251 252 253 254 255 256

	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",
257
			       __func__, drive->name, bc_dev, bc_mem);
Linus Torvalds's avatar
Linus Torvalds committed
258 259 260 261 262 263
		}
	}

	return dma_stat;
}

264
static void sgiioc4_set_dma_mode(ide_drive_t *drive, const u8 speed)
265 266 267
{
}

Linus Torvalds's avatar
Linus Torvalds committed
268
/* returns 1 if dma irq issued, 0 otherwise */
269
static int sgiioc4_dma_test_irq(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
270
{
271
	return sgiioc4_checkirq(drive->hwif);
Linus Torvalds's avatar
Linus Torvalds committed
272 273
}

274
static void sgiioc4_dma_host_set(ide_drive_t *drive, int on)
Linus Torvalds's avatar
Linus Torvalds committed
275
{
276 277
	if (!on)
		sgiioc4_clearirq(drive);
Linus Torvalds's avatar
Linus Torvalds committed
278 279
}

280
static void sgiioc4_resetproc(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
281
{
282 283
	struct ide_cmd *cmd = &drive->hwif->cmd;

284
	sgiioc4_dma_end(drive);
285
	ide_dma_unmap_sg(drive, cmd);
Linus Torvalds's avatar
Linus Torvalds committed
286 287 288
	sgiioc4_clearirq(drive);
}

289 290 291 292 293 294 295 296
static void
sgiioc4_dma_lost_irq(ide_drive_t * drive)
{
	sgiioc4_resetproc(drive);

	ide_dma_lost_irq(drive);
}

297
static u8 sgiioc4_read_status(ide_hwif_t *hwif)
Linus Torvalds's avatar
Linus Torvalds committed
298
{
299
	unsigned long port = hwif->io_ports.status_addr;
300
	u8 reg = (u8) readb((void __iomem *) port);
Linus Torvalds's avatar
Linus Torvalds committed
301

302 303 304
	if (!(reg & ATA_BUSY)) {	/* Not busy... check for interrupt */
		unsigned long other_ir = port - 0x110;
		unsigned int intr_reg = (u32) readl((void __iomem *) other_ir);
Linus Torvalds's avatar
Linus Torvalds committed
305

306 307 308 309
		/* Clear the Interrupt, Error bits on the IOC4 */
		if (intr_reg & 0x03) {
			writel(0x03, (void __iomem *) other_ir);
			intr_reg = (u32) readl((void __iomem *) other_ir);
Linus Torvalds's avatar
Linus Torvalds committed
310 311 312 313 314 315 316
		}
	}

	return reg;
}

/* Creates a dma map for the scatter-gather list entries */
317
static int __devinit
318
ide_dma_sgiioc4(ide_hwif_t *hwif, const struct ide_port_info *d)
Linus Torvalds's avatar
Linus Torvalds committed
319
{
320
	struct pci_dev *dev = to_pci_dev(hwif->dev);
321
	unsigned long dma_base = pci_resource_start(dev, 0) + IOC4_DMA_OFFSET;
Linus Torvalds's avatar
Linus Torvalds committed
322
	int num_ports = sizeof (ioc4_dma_regs_t);
323
	void *pad;
Linus Torvalds's avatar
Linus Torvalds committed
324

Sergei Shtylylov's avatar
Sergei Shtylylov committed
325
	printk(KERN_INFO "    %s: MMIO-DMA\n", hwif->name);
Linus Torvalds's avatar
Linus Torvalds committed
326

Sergei Shtylylov's avatar
Sergei Shtylylov committed
327 328 329 330
	if (request_mem_region(dma_base, num_ports, hwif->name) == NULL) {
		printk(KERN_ERR "%s(%s) -- ERROR: addresses 0x%08lx to 0x%08lx "
		       "already in use\n", __func__, hwif->name,
		       dma_base, dma_base + num_ports - 1);
331
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
332 333
	}

334 335
	hwif->dma_base = (unsigned long)hwif->io_ports.irq_addr +
			 IOC4_DMA_OFFSET;
336

337
	hwif->sg_max_nents = IOC4_PRD_ENTRIES;
Linus Torvalds's avatar
Linus Torvalds committed
338

339 340
	hwif->prd_max_nents = IOC4_PRD_ENTRIES;
	hwif->prd_ent_size = IOC4_PRD_BYTES;
Linus Torvalds's avatar
Linus Torvalds committed
341

342 343
	if (ide_allocate_dma_engine(hwif))
		goto dma_pci_alloc_failure;
Linus Torvalds's avatar
Linus Torvalds committed
344

345
	pad = pci_alloc_consistent(dev, IOC4_IDE_CACHELINE_SIZE,
346
				   (dma_addr_t *)&hwif->extra_base);
347 348
	if (pad) {
		ide_set_hwifdata(hwif, pad);
349
		return 0;
350
	}
Linus Torvalds's avatar
Linus Torvalds committed
351

352 353
	ide_release_dma_engine(hwif);

Sergei Shtylylov's avatar
Sergei Shtylylov committed
354
	printk(KERN_ERR "%s(%s) -- ERROR: Unable to allocate DMA maps\n",
355
	       __func__, hwif->name);
Sergei Shtylylov's avatar
Sergei Shtylylov committed
356
	printk(KERN_INFO "%s: changing from DMA to PIO mode", hwif->name);
Linus Torvalds's avatar
Linus Torvalds committed
357

358 359 360
dma_pci_alloc_failure:
	release_mem_region(dma_base, num_ports);

361
	return -1;
Linus Torvalds's avatar
Linus Torvalds committed
362 363 364 365 366 367 368
}

/* Initializes the IOC4 DMA Engine */
static void
sgiioc4_configure_for_dma(int dma_direction, ide_drive_t * drive)
{
	u32 ioc4_dma;
369
	ide_hwif_t *hwif = drive->hwif;
370 371
	unsigned long dma_base = hwif->dma_base;
	unsigned long ioc4_dma_addr = dma_base + IOC4_DMA_CTRL * 4;
Linus Torvalds's avatar
Linus Torvalds committed
372 373
	u32 dma_addr, ending_dma_addr;

374
	ioc4_dma = readl((void __iomem *)ioc4_dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
375 376 377 378

	if (ioc4_dma & IOC4_S_DMA_ACTIVE) {
		printk(KERN_WARNING
			"%s(%s):Warning!! DMA from previous transfer was still active\n",
379
		       __func__, drive->name);
380
		writel(IOC4_S_DMA_STOP, (void __iomem *)ioc4_dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
381 382 383 384 385
		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",
386
			       __func__, drive->name);
Linus Torvalds's avatar
Linus Torvalds committed
387 388
	}

389
	ioc4_dma = readl((void __iomem *)ioc4_dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
390 391 392 393
	if (ioc4_dma & IOC4_S_DMA_ERROR) {
		printk(KERN_WARNING
		       "%s(%s) : Warning!! - DMA Error during Previous"
		       " transfer | status 0x%x\n",
394
		       __func__, drive->name, ioc4_dma);
395
		writel(IOC4_S_DMA_STOP, (void __iomem *)ioc4_dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
396 397 398 399 400
		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",
401
			       __func__, drive->name);
Linus Torvalds's avatar
Linus Torvalds committed
402 403 404 405
	}

	/* Address of the Scatter Gather List */
	dma_addr = cpu_to_le32(hwif->dmatable_dma);
406
	writel(dma_addr, (void __iomem *)(dma_base + IOC4_DMA_PTR_L * 4));
Linus Torvalds's avatar
Linus Torvalds committed
407 408

	/* Address of the Ending DMA */
409
	memset(ide_get_hwifdata(hwif), 0, IOC4_IDE_CACHELINE_SIZE);
410
	ending_dma_addr = cpu_to_le32(hwif->extra_base);
411
	writel(ending_dma_addr, (void __iomem *)(dma_base + IOC4_DMA_END_ADDR * 4));
Linus Torvalds's avatar
Linus Torvalds committed
412

413
	writel(dma_direction, (void __iomem *)ioc4_dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
414 415 416 417 418 419 420 421 422 423 424
}

/* 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 */
425
static int sgiioc4_build_dmatable(ide_drive_t *drive, struct ide_cmd *cmd)
Linus Torvalds's avatar
Linus Torvalds committed
426
{
427
	ide_hwif_t *hwif = drive->hwif;
Linus Torvalds's avatar
Linus Torvalds committed
428
	unsigned int *table = hwif->dmatable_cpu;
429
	unsigned int count = 0, i = cmd->sg_nents;
430
	struct scatterlist *sg = hwif->sg_table;
Linus Torvalds's avatar
Linus Torvalds committed
431 432 433 434 435 436 437 438 439 440 441 442

	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);
443
				return 0;
Linus Torvalds's avatar
Linus Torvalds committed
444
			} else {
445
				u32 bcount =
Linus Torvalds's avatar
Linus Torvalds committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459
				    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++;

460
				*table = cpu_to_be32(bcount);
Linus Torvalds's avatar
Linus Torvalds committed
461 462 463 464 465 466 467
				table++;

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

Jens Axboe's avatar
Jens Axboe committed
468
		sg = sg_next(sg);
Linus Torvalds's avatar
Linus Torvalds committed
469 470 471 472 473 474 475 476 477 478 479 480
		i--;
	}

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

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

481
static int sgiioc4_dma_setup(ide_drive_t *drive, struct ide_cmd *cmd)
Linus Torvalds's avatar
Linus Torvalds committed
482 483
{
	int ddir;
484
	u8 write = !!(cmd->tf_flags & IDE_TFLAG_WRITE);
Linus Torvalds's avatar
Linus Torvalds committed
485

486
	if (sgiioc4_build_dmatable(drive, cmd) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
487 488 489
		/* try PIO instead of DMA */
		return 1;

490
	if (write)
Linus Torvalds's avatar
Linus Torvalds committed
491 492 493 494 495 496 497 498 499 500 501
		/* 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;
}

502 503 504 505
static const struct ide_tp_ops sgiioc4_tp_ops = {
	.exec_command		= ide_exec_command,
	.read_status		= sgiioc4_read_status,
	.read_altstatus		= ide_read_altstatus,
506
	.write_devctl		= ide_write_devctl,
507

508
	.dev_select		= ide_dev_select,
509 510 511 512 513 514 515
	.tf_load		= ide_tf_load,
	.tf_read		= ide_tf_read,

	.input_data		= ide_input_data,
	.output_data		= ide_output_data,
};

516 517 518 519 520 521
static const struct ide_port_ops sgiioc4_port_ops = {
	.set_dma_mode		= sgiioc4_set_dma_mode,
	/* reset DMA engine, clear IRQs */
	.resetproc		= sgiioc4_resetproc,
};

522
static const struct ide_dma_ops sgiioc4_dma_ops = {
523 524 525 526 527 528 529 530
	.dma_host_set		= sgiioc4_dma_host_set,
	.dma_setup		= sgiioc4_dma_setup,
	.dma_start		= sgiioc4_dma_start,
	.dma_end		= sgiioc4_dma_end,
	.dma_test_irq		= sgiioc4_dma_test_irq,
	.dma_lost_irq		= sgiioc4_dma_lost_irq,
};

531
static const struct ide_port_info sgiioc4_port_info __devinitconst = {
532
	.name			= DRV_NAME,
533
	.chipset		= ide_pci,
534
	.init_dma		= ide_dma_sgiioc4,
535
	.tp_ops			= &sgiioc4_tp_ops,
536
	.port_ops		= &sgiioc4_port_ops,
537
	.dma_ops		= &sgiioc4_dma_ops,
538
	.host_flags		= IDE_HFLAG_MMIO,
539
	.irq_flags		= IRQF_SHARED,
540 541 542
	.mwdma_mask		= ATA_MWDMA2_ONLY,
};

Linus Torvalds's avatar
Linus Torvalds committed
543
static int __devinit
544
sgiioc4_ide_setup_pci_device(struct pci_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
545
{
546
	unsigned long cmd_base, irqport;
547 548
	unsigned long bar0, cmd_phys_base, ctl;
	void __iomem *virt_base;
549
	hw_regs_t hw, *hws[] = { &hw, NULL, NULL, NULL };
550
	int rc;
Linus Torvalds's avatar
Linus Torvalds committed
551 552

	/*  Get the CmdBlk and CtrlBlk Base Registers */
553
	bar0 = pci_resource_start(dev, 0);
554
	virt_base = pci_ioremap_bar(dev, 0);
555 556
	if (virt_base == NULL) {
		printk(KERN_ERR "%s: Unable to remap BAR 0 address: 0x%lx\n",
557
				DRV_NAME, bar0);
558 559 560 561 562
		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;
Linus Torvalds's avatar
Linus Torvalds committed
563

564
	cmd_phys_base = bar0 + IOC4_CMD_OFFSET;
Sergei Shtylylov's avatar
Sergei Shtylylov committed
565 566 567 568 569
	if (request_mem_region(cmd_phys_base, IOC4_CMD_CTL_BLK_SIZE,
			       DRV_NAME) == NULL) {
		printk(KERN_ERR "%s %s -- ERROR: addresses 0x%08lx to 0x%08lx "
		       "already in use\n", DRV_NAME, pci_name(dev),
		       cmd_phys_base, cmd_phys_base + IOC4_CMD_CTL_BLK_SIZE);
570 571
		rc = -EBUSY;
		goto req_mem_rgn_err;
Linus Torvalds's avatar
Linus Torvalds committed
572 573
	}

574 575 576
	/* Initialize the IO registers */
	memset(&hw, 0, sizeof(hw));
	sgiioc4_init_hwif_ports(&hw, cmd_base, ctl, irqport);
577 578 579
	hw.irq = dev->irq;
	hw.chipset = ide_pci;
	hw.dev = &dev->dev;
580

Linus Torvalds's avatar
Linus Torvalds committed
581
	/* Initializing chipset IRQ Registers */
582
	writel(0x03, (void __iomem *)(irqport + IOC4_INTR_SET * 4));
Linus Torvalds's avatar
Linus Torvalds committed
583

584 585 586
	rc = ide_host_add(&sgiioc4_port_info, hws, NULL);
	if (!rc)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
587

588
	release_mem_region(cmd_phys_base, IOC4_CMD_CTL_BLK_SIZE);
589
req_mem_rgn_err:
590
	iounmap(virt_base);
591
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
592 593 594
}

static unsigned int __devinit
595
pci_init_sgiioc4(struct pci_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
596 597 598 599
{
	int ret;

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

	if (dev->revision < IOC4_SUPPORTED_FIRMWARE_REV) {
Linus Torvalds's avatar
Linus Torvalds committed
603
		printk(KERN_ERR "Skipping %s IDE controller in slot %s: "
604 605 606
				"firmware is obsolete - please upgrade to "
				"revision46 or higher\n",
				DRV_NAME, pci_name(dev));
Linus Torvalds's avatar
Linus Torvalds committed
607 608 609
		ret = -EAGAIN;
		goto out;
	}
610
	ret = sgiioc4_ide_setup_pci_device(dev);
Linus Torvalds's avatar
Linus Torvalds committed
611 612 613 614
out:
	return ret;
}

615
int __devinit
616
ioc4_ide_attach_one(struct ioc4_driver_data *idd)
Linus Torvalds's avatar
Linus Torvalds committed
617
{
618 619 620 621 622 623
	/* 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;

624
	return pci_init_sgiioc4(idd->idd_pdev);
Linus Torvalds's avatar
Linus Torvalds committed
625 626
}

627
static struct ioc4_submodule __devinitdata ioc4_ide_submodule = {
628 629 630 631 632 633
	.is_name = "IOC4_ide",
	.is_owner = THIS_MODULE,
	.is_probe = ioc4_ide_attach_one,
/*	.is_remove = ioc4_ide_remove_one,	*/
};

634
static int __init ioc4_ide_init(void)
635 636 637 638
{
	return ioc4_register_submodule(&ioc4_ide_submodule);
}

639
late_initcall(ioc4_ide_init); /* Call only after IDE init is done */
Linus Torvalds's avatar
Linus Torvalds committed
640

641
MODULE_AUTHOR("Aniket Malatpure/Jeremy Higdon");
Linus Torvalds's avatar
Linus Torvalds committed
642 643
MODULE_DESCRIPTION("IDE PCI driver module for SGI IOC4 Base-IO Card");
MODULE_LICENSE("GPL");