dtc.c 12.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

#define PSEUDO_DMA
#define DONT_USE_INTR
#define UNSAFE			/* Leave interrupts enabled during pseudo-dma I/O */
#define DMA_WORKS_RIGHT


/*
 * DTC 3180/3280 driver, by
 *	Ray Van Tassle	rayvt@comm.mot.com
 *
 *	taken from ...
 *	Trantor T128/T128F/T228 driver by...
 *
 * 	Drew Eckhardt
 *	Visionary Computing
 *	(Unix and Linux consulting and custom programming)
 *	drew@colorado.edu
 *      +1 (303) 440-4894
F
Finn Thain 已提交
20
 */
L
Linus Torvalds 已提交
21 22 23 24 25 26 27 28 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

/*
 * The card is detected and initialized in one of several ways : 
 * 1.  Autoprobe (default) - since the board is memory mapped, 
 *     a BIOS signature is scanned for to locate the registers.
 *     An interrupt is triggered to autoprobe for the interrupt
 *     line.
 *
 * 2.  With command line overrides - dtc=address,irq may be 
 *     used on the LILO command line to override the defaults.
 * 
*/

/*----------------------------------------------------------------*/
/* the following will set the monitor border color (useful to find
 where something crashed or gets stuck at */
/* 1 = blue
 2 = green
 3 = cyan
 4 = red
 5 = magenta
 6 = yellow
 7 = white
*/
#if 0
#define rtrc(i) {inb(0x3da); outb(0x31, 0x3c0); outb((i), 0x3c0);}
#else
#define rtrc(i) {}
#endif


#include <linux/module.h>
#include <linux/signal.h>
#include <linux/blkdev.h>
#include <linux/delay.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/interrupt.h>
60
#include <linux/io.h>
L
Linus Torvalds 已提交
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
#include <scsi/scsi_host.h>
#include "dtc.h"
#define AUTOPROBE_IRQ
#include "NCR5380.h"

/*
 * The DTC3180 & 3280 boards are memory mapped.
 * 
 */

/*
 */
/* Offset from DTC_5380_OFFSET */
#define DTC_CONTROL_REG		0x100	/* rw */
#define D_CR_ACCESS		0x80	/* ro set=can access 3280 registers */
#define CSR_DIR_READ		0x40	/* rw direction, 1 = read 0 = write */

#define CSR_RESET              0x80	/* wo  Resets 53c400 */
#define CSR_5380_REG           0x80	/* ro  5380 registers can be accessed */
#define CSR_TRANS_DIR          0x40	/* rw  Data transfer direction */
#define CSR_SCSI_BUFF_INTR     0x20	/* rw  Enable int on transfer ready */
#define CSR_5380_INTR          0x10	/* rw  Enable 5380 interrupts */
#define CSR_SHARED_INTR        0x08	/* rw  Interrupt sharing */
#define CSR_HOST_BUF_NOT_RDY   0x04	/* ro  Host buffer not ready */
#define CSR_SCSI_BUF_RDY       0x02	/* ro  SCSI buffer ready */
#define CSR_GATED_5380_IRQ     0x01	/* ro  Last block xferred */
#define CSR_INT_BASE (CSR_SCSI_BUFF_INTR | CSR_5380_INTR)


#define DTC_BLK_CNT		0x101	/* rw 
					 * # of 128-byte blocks to transfer */


#define D_CR_ACCESS             0x80	/* ro set=can access 3280 registers */

#define DTC_SWITCH_REG		0x3982	/* ro - DIP switches */
#define DTC_RESUME_XFER		0x3982	/* wo - resume data xfer 
					 * after disconnect/reconnect*/

#define DTC_5380_OFFSET		0x3880	/* 8 registers here, see NCR5380.h */

/*!!!! for dtc, it's a 128 byte buffer at 3900 !!! */
#define DTC_DATA_BUF		0x3900	/* rw 128 bytes long */

static struct override {
	unsigned int address;
	int irq;
} overrides
#ifdef OVERRIDE
[] __initdata = OVERRIDE;
#else
112 113 114
[4] __initdata = {
	{ 0, IRQ_AUTO }, { 0, IRQ_AUTO }, { 0, IRQ_AUTO }, { 0, IRQ_AUTO }
};
L
Linus Torvalds 已提交
115 116
#endif

117
#define NO_OVERRIDES ARRAY_SIZE(overrides)
L
Linus Torvalds 已提交
118 119 120 121

static struct base {
	unsigned long address;
	int noauto;
122 123 124 125
} bases[] __initdata = {
	{ 0xcc000, 0 },
	{ 0xc8000, 0 },
	{ 0xdc000, 0 },
L
Linus Torvalds 已提交
126 127 128
	{ 0xd8000, 0 }
};

129
#define NO_BASES ARRAY_SIZE(bases)
L
Linus Torvalds 已提交
130 131 132 133

static const struct signature {
	const char *string;
	int offset;
134
} signatures[] = {
L
Linus Torvalds 已提交
135 136 137
	{"DATA TECHNOLOGY CORPORATION BIOS", 0x25},
};

138
#define NO_SIGNATURES ARRAY_SIZE(signatures)
L
Linus Torvalds 已提交
139 140 141 142 143 144

#ifndef MODULE
/*
 * Function : dtc_setup(char *str, int *ints)
 *
 * Purpose : LILO command line initialization of the overrides array,
145
 *
L
Linus Torvalds 已提交
146 147 148
 * Inputs : str - unused, ints - array of integer parameters with ints[0]
 *	equal to the number of ints.
 *
149
 */
L
Linus Torvalds 已提交
150

151
static int __init dtc_setup(char *str)
L
Linus Torvalds 已提交
152
{
153
	static int commandline_current;
L
Linus Torvalds 已提交
154
	int i;
155 156 157
	int ints[10];

	get_options(str, ARRAY_SIZE(ints), ints);
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167 168 169
	if (ints[0] != 2)
		printk("dtc_setup: usage dtc=address,irq\n");
	else if (commandline_current < NO_OVERRIDES) {
		overrides[commandline_current].address = ints[1];
		overrides[commandline_current].irq = ints[2];
		for (i = 0; i < NO_BASES; ++i)
			if (bases[i].address == ints[1]) {
				bases[i].noauto = 1;
				break;
			}
		++commandline_current;
	}
170
	return 1;
L
Linus Torvalds 已提交
171
}
172 173

__setup("dtc=", dtc_setup);
L
Linus Torvalds 已提交
174 175 176
#endif

/* 
177
 * Function : int dtc_detect(struct scsi_host_template * tpnt)
L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186 187 188
 *
 * Purpose : detects and initializes DTC 3180/3280 controllers
 *	that were autoprobed, overridden on the LILO command line, 
 *	or specified at compile time.
 *
 * Inputs : tpnt - template for this SCSI adapter.
 * 
 * Returns : 1 if a host adapter was found, 0 if not.
 *
*/

189
static int __init dtc_detect(struct scsi_host_template * tpnt)
L
Linus Torvalds 已提交
190
{
191
	static int current_override, current_base;
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	struct Scsi_Host *instance;
	unsigned int addr;
	void __iomem *base;
	int sig, count;

	for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
		addr = 0;
		base = NULL;

		if (overrides[current_override].address) {
			addr = overrides[current_override].address;
			base = ioremap(addr, 0x2000);
			if (!base)
				addr = 0;
		} else
			for (; !addr && (current_base < NO_BASES); ++current_base) {
208 209
				dprintk(NDEBUG_INIT, "dtc: probing address 0x%08x\n",
				        (unsigned int)bases[current_base].address);
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217
				if (bases[current_base].noauto)
					continue;
				base = ioremap(bases[current_base].address, 0x2000);
				if (!base)
					continue;
				for (sig = 0; sig < NO_SIGNATURES; ++sig) {
					if (check_signature(base + signatures[sig].offset, signatures[sig].string, strlen(signatures[sig].string))) {
						addr = bases[current_base].address;
218
						dprintk(NDEBUG_INIT, "dtc: detected board\n");
L
Linus Torvalds 已提交
219 220 221 222 223 224
						goto found;
					}
				}
				iounmap(base);
			}

225
		dprintk(NDEBUG_INIT, "dtc: addr = 0x%08x\n", addr);
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239

		if (!addr)
			break;

found:
		instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
		if (instance == NULL)
			break;

		instance->base = addr;
		((struct NCR5380_hostdata *)(instance)->hostdata)->base = base;

		NCR5380_init(instance, 0);

240 241
		NCR5380_maybe_reset_bus(instance);

L
Linus Torvalds 已提交
242 243 244 245 246 247
		NCR5380_write(DTC_CONTROL_REG, CSR_5380_INTR);	/* Enable int's */
		if (overrides[current_override].irq != IRQ_AUTO)
			instance->irq = overrides[current_override].irq;
		else
			instance->irq = NCR5380_probe_irq(instance, DTC_IRQS);

F
Finn Thain 已提交
248 249 250 251
		/* Compatibility with documented NCR5380 kernel parameters */
		if (instance->irq == 255)
			instance->irq = NO_IRQ;

L
Linus Torvalds 已提交
252 253 254
#ifndef DONT_USE_INTR
		/* With interrupts enabled, it will sometimes hang when doing heavy
		 * reads. So better not enable them until I finger it out. */
F
Finn Thain 已提交
255
		if (instance->irq != NO_IRQ)
256
			if (request_irq(instance->irq, dtc_intr, 0,
257
					"dtc", instance)) {
L
Linus Torvalds 已提交
258
				printk(KERN_ERR "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
F
Finn Thain 已提交
259
				instance->irq = NO_IRQ;
L
Linus Torvalds 已提交
260 261
			}

F
Finn Thain 已提交
262
		if (instance->irq == NO_IRQ) {
L
Linus Torvalds 已提交
263 264 265 266
			printk(KERN_WARNING "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
			printk(KERN_WARNING "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
		}
#else
F
Finn Thain 已提交
267
		if (instance->irq != NO_IRQ)
L
Linus Torvalds 已提交
268
			printk(KERN_WARNING "scsi%d : interrupts not used. Might as well not jumper it.\n", instance->host_no);
F
Finn Thain 已提交
269
		instance->irq = NO_IRQ;
L
Linus Torvalds 已提交
270
#endif
271 272
		dprintk(NDEBUG_INIT, "scsi%d : irq = %d\n",
		        instance->host_no, instance->irq);
L
Linus Torvalds 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

		++current_override;
		++count;
	}
	return count;
}

/*
 * Function : int dtc_biosparam(Disk * disk, struct block_device *dev, int *ip)
 *
 * Purpose : Generates a BIOS / DOS compatible H-C-S mapping for 
 *	the specified device / size.
 * 
 * Inputs : size = size of device in sectors (512 bytes), dev = block device
 *	major / minor, ip[] = {heads, sectors, cylinders}  
 *
 * Returns : always 0 (success), initializes ip
 *	
*/

/* 
 * XXX Most SCSI boards use this mapping, I could be incorrect.  Some one
 * using hard disks on a trantor should verify that this mapping corresponds
 * to that used by the BIOS / ASPI driver by running the linux fdisk program
 * and matching the H_C_S coordinates to what DOS uses.
*/

static int dtc_biosparam(struct scsi_device *sdev, struct block_device *dev,
			 sector_t capacity, int *ip)
{
	int size = capacity;

	ip[0] = 64;
	ip[1] = 32;
	ip[2] = size >> 11;
	return 0;
}


/****************************************************************
 * Function : int NCR5380_pread (struct Scsi_Host *instance, 
 *	unsigned char *dst, int len)
 *
 * Purpose : Fast 5380 pseudo-dma read function, reads len bytes to 
 *	dst
 * 
 * Inputs : dst = destination, len = length in bytes
 *
 * Returns : 0 on success, non zero on a failure such as a watchdog 
 * 	timeout.
*/

static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
{
	unsigned char *d = dst;
	int i;			/* For counting time spent in the poll-loop */
329
	struct NCR5380_hostdata *hostdata = shost_priv(instance);
L
Linus Torvalds 已提交
330 331 332 333

	i = 0;
	NCR5380_read(RESET_PARITY_INTERRUPT_REG);
	NCR5380_write(MODE_REG, MR_ENABLE_EOP_INTR | MR_DMA_MODE);
F
Finn Thain 已提交
334
	if (instance->irq == NO_IRQ)
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342 343 344
		NCR5380_write(DTC_CONTROL_REG, CSR_DIR_READ);
	else
		NCR5380_write(DTC_CONTROL_REG, CSR_DIR_READ | CSR_INT_BASE);
	NCR5380_write(DTC_BLK_CNT, len >> 7);	/* Block count */
	rtrc(1);
	while (len > 0) {
		rtrc(2);
		while (NCR5380_read(DTC_CONTROL_REG) & CSR_HOST_BUF_NOT_RDY)
			++i;
		rtrc(3);
345
		memcpy_fromio(d, hostdata->base + DTC_DATA_BUF, 128);
L
Linus Torvalds 已提交
346 347 348 349 350 351 352 353 354 355 356 357
		d += 128;
		len -= 128;
		rtrc(7);
		/*** with int's on, it sometimes hangs after here.
		 * Looks like something makes HBNR go away. */
	}
	rtrc(4);
	while (!(NCR5380_read(DTC_CONTROL_REG) & D_CR_ACCESS))
		++i;
	NCR5380_write(MODE_REG, 0);	/* Clear the operating mode */
	rtrc(0);
	NCR5380_read(RESET_PARITY_INTERRUPT_REG);
358 359
	if (i > hostdata->spin_max_r)
		hostdata->spin_max_r = i;
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	return (0);
}

/****************************************************************
 * Function : int NCR5380_pwrite (struct Scsi_Host *instance, 
 *	unsigned char *src, int len)
 *
 * Purpose : Fast 5380 pseudo-dma write function, transfers len bytes from
 *	src
 * 
 * Inputs : src = source, len = length in bytes
 *
 * Returns : 0 on success, non zero on a failure such as a watchdog 
 * 	timeout.
*/

static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
{
	int i;
379
	struct NCR5380_hostdata *hostdata = shost_priv(instance);
L
Linus Torvalds 已提交
380 381 382 383

	NCR5380_read(RESET_PARITY_INTERRUPT_REG);
	NCR5380_write(MODE_REG, MR_ENABLE_EOP_INTR | MR_DMA_MODE);
	/* set direction (write) */
F
Finn Thain 已提交
384
	if (instance->irq == NO_IRQ)
L
Linus Torvalds 已提交
385 386 387 388 389 390 391 392 393 394
		NCR5380_write(DTC_CONTROL_REG, 0);
	else
		NCR5380_write(DTC_CONTROL_REG, CSR_5380_INTR);
	NCR5380_write(DTC_BLK_CNT, len >> 7);	/* Block count */
	for (i = 0; len > 0; ++i) {
		rtrc(5);
		/* Poll until the host buffer can accept data. */
		while (NCR5380_read(DTC_CONTROL_REG) & CSR_HOST_BUF_NOT_RDY)
			++i;
		rtrc(3);
395
		memcpy_toio(hostdata->base + DTC_DATA_BUF, src, 128);
L
Linus Torvalds 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409
		src += 128;
		len -= 128;
	}
	rtrc(4);
	while (!(NCR5380_read(DTC_CONTROL_REG) & D_CR_ACCESS))
		++i;
	rtrc(6);
	/* Wait until the last byte has been sent to the disk */
	while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
		++i;
	rtrc(7);
	/* Check for parity error here. fixme. */
	NCR5380_write(MODE_REG, 0);	/* Clear the operating mode */
	rtrc(0);
410 411
	if (i > hostdata->spin_max_w)
		hostdata->spin_max_w = i;
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420
	return (0);
}

MODULE_LICENSE("GPL");

#include "NCR5380.c"

static int dtc_release(struct Scsi_Host *shost)
{
421 422
	struct NCR5380_hostdata *hostdata = shost_priv(shost);

F
Finn Thain 已提交
423
	if (shost->irq != NO_IRQ)
424
		free_irq(shost->irq, shost);
L
Linus Torvalds 已提交
425 426 427 428
	NCR5380_exit(shost);
	if (shost->io_port && shost->n_io_port)
		release_region(shost->io_port, shost->n_io_port);
	scsi_unregister(shost);
429
	iounmap(hostdata->base);
L
Linus Torvalds 已提交
430 431 432
	return 0;
}

433
static struct scsi_host_template driver_template = {
L
Linus Torvalds 已提交
434 435 436
	.name				= "DTC 3180/3280 ",
	.detect				= dtc_detect,
	.release			= dtc_release,
437 438 439
	.proc_name			= "dtc3x80",
	.show_info			= dtc_show_info,
	.write_info			= dtc_write_info,
F
Finn Thain 已提交
440
	.info				= dtc_info,
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448 449 450 451
	.queuecommand			= dtc_queue_command,
	.eh_abort_handler		= dtc_abort,
	.eh_bus_reset_handler		= dtc_bus_reset,
	.bios_param     		= dtc_biosparam,
	.can_queue      		= CAN_QUEUE,
	.this_id        		= 7,
	.sg_tablesize   		= SG_ALL,
	.cmd_per_lun    		= CMD_PER_LUN,
	.use_clustering 		= DISABLE_CLUSTERING,
};
#include "scsi_module.c"