| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 |  * linux/drivers/scsi/arm/arxescsi.c | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 |  * | 
 | 4 |  * Copyright (C) 1997-2000 Russell King, Stefan Hanske | 
 | 5 |  * | 
 | 6 |  * This driver is based on experimentation.  Hence, it may have made | 
 | 7 |  * assumptions about the particular card that I have available, and | 
 | 8 |  * may not be reliable! | 
 | 9 |  * | 
 | 10 |  * Changelog: | 
 | 11 |  *  30-08-1997	RMK	0.0.0	Created, READONLY version as cumana_2.c | 
 | 12 |  *  22-01-1998	RMK	0.0.1	Updated to 2.1.80 | 
 | 13 |  *  15-04-1998	RMK	0.0.1	Only do PIO if FAS216 will allow it. | 
 | 14 |  *  11-06-1998 	SH	0.0.2   Changed to support ARXE 16-bit SCSI card | 
 | 15 |  *				enabled writing | 
 | 16 |  *  01-01-2000	SH	0.1.0   Added *real* pseudo dma writing | 
 | 17 |  *				(arxescsi_pseudo_dma_write) | 
 | 18 |  *  02-04-2000	RMK	0.1.1	Updated for new error handling code. | 
 | 19 |  *  22-10-2000  SH		Updated for new registering scheme. | 
 | 20 |  */ | 
 | 21 | #include <linux/module.h> | 
 | 22 | #include <linux/blkdev.h> | 
 | 23 | #include <linux/kernel.h> | 
 | 24 | #include <linux/string.h> | 
 | 25 | #include <linux/ioport.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/proc_fs.h> | 
 | 27 | #include <linux/unistd.h> | 
 | 28 | #include <linux/stat.h> | 
 | 29 | #include <linux/delay.h> | 
 | 30 | #include <linux/init.h> | 
 | 31 | #include <linux/interrupt.h> | 
 | 32 |  | 
 | 33 | #include <asm/dma.h> | 
 | 34 | #include <asm/io.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/ecard.h> | 
 | 36 |  | 
 | 37 | #include "../scsi.h" | 
 | 38 | #include <scsi/scsi_host.h> | 
 | 39 | #include "fas216.h" | 
 | 40 |  | 
 | 41 | struct arxescsi_info { | 
 | 42 | 	FAS216_Info		info; | 
 | 43 | 	struct expansion_card	*ec; | 
 | 44 | 	void __iomem		*base; | 
 | 45 | }; | 
 | 46 |  | 
 | 47 | #define DMADATA_OFFSET	(0x200) | 
 | 48 |  | 
 | 49 | #define DMASTAT_OFFSET	(0x600) | 
 | 50 | #define DMASTAT_DRQ	(1 << 0) | 
 | 51 |  | 
 | 52 | #define CSTATUS_IRQ	(1 << 0) | 
 | 53 |  | 
 | 54 | #define VERSION "1.10 (23/01/2003 2.5.57)" | 
 | 55 |  | 
 | 56 | /* | 
 | 57 |  * Function: int arxescsi_dma_setup(host, SCpnt, direction, min_type) | 
 | 58 |  * Purpose : initialises DMA/PIO | 
 | 59 |  * Params  : host      - host | 
 | 60 |  *	     SCpnt     - command | 
 | 61 |  *	     direction - DMA on to/off of card | 
 | 62 |  *	     min_type  - minimum DMA support that we must have for this transfer | 
 | 63 |  * Returns : 0 if we should not set CMD_WITHDMA for transfer info command | 
 | 64 |  */ | 
 | 65 | static fasdmatype_t | 
| Christoph Hellwig | 0a04137 | 2005-10-31 18:31:56 +0100 | [diff] [blame] | 66 | arxescsi_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | 		       fasdmadir_t direction, fasdmatype_t min_type) | 
 | 68 | { | 
 | 69 | 	/* | 
 | 70 | 	 * We don't do real DMA | 
 | 71 | 	 */ | 
 | 72 | 	return fasdma_pseudo; | 
 | 73 | } | 
 | 74 |  | 
 | 75 | static void arxescsi_pseudo_dma_write(unsigned char *addr, void __iomem *base) | 
 | 76 | { | 
 | 77 |        __asm__ __volatile__( | 
 | 78 |        "               stmdb   sp!, {r0-r12}\n" | 
 | 79 |        "               mov     r3, %0\n" | 
 | 80 |        "               mov     r1, %1\n" | 
 | 81 |        "               add     r2, r1, #512\n" | 
 | 82 |        "               mov     r4, #256\n" | 
 | 83 |        ".loop_1:       ldmia   r3!, {r6, r8, r10, r12}\n" | 
 | 84 |        "               mov     r5, r6, lsl #16\n" | 
 | 85 |        "               mov     r7, r8, lsl #16\n" | 
 | 86 |        ".loop_2:       ldrb    r0, [r1, #1536]\n" | 
 | 87 |        "               tst     r0, #1\n" | 
 | 88 |        "               beq     .loop_2\n" | 
 | 89 |        "               stmia   r2, {r5-r8}\n\t" | 
 | 90 |        "               mov     r9, r10, lsl #16\n" | 
 | 91 |        "               mov     r11, r12, lsl #16\n" | 
 | 92 |        ".loop_3:       ldrb    r0, [r1, #1536]\n" | 
 | 93 |        "               tst     r0, #1\n" | 
 | 94 |        "               beq     .loop_3\n" | 
 | 95 |        "               stmia   r2, {r9-r12}\n" | 
 | 96 |        "               subs    r4, r4, #16\n" | 
 | 97 |        "               bne     .loop_1\n" | 
 | 98 |        "               ldmia   sp!, {r0-r12}\n" | 
 | 99 |        : | 
 | 100 |        : "r" (addr), "r" (base)); | 
 | 101 | } | 
 | 102 |  | 
 | 103 | /* | 
 | 104 |  * Function: int arxescsi_dma_pseudo(host, SCpnt, direction, transfer) | 
 | 105 |  * Purpose : handles pseudo DMA | 
 | 106 |  * Params  : host      - host | 
 | 107 |  *	     SCpnt     - command | 
 | 108 |  *	     direction - DMA on to/off of card | 
 | 109 |  *	     transfer  - minimum number of bytes we expect to transfer | 
 | 110 |  */ | 
 | 111 | static void | 
| Christoph Hellwig | 0a04137 | 2005-10-31 18:31:56 +0100 | [diff] [blame] | 112 | arxescsi_dma_pseudo(struct Scsi_Host *host, struct scsi_pointer *SCp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | 		    fasdmadir_t direction, int transfer) | 
 | 114 | { | 
 | 115 | 	struct arxescsi_info *info = (struct arxescsi_info *)host->hostdata; | 
 | 116 | 	unsigned int length, error = 0; | 
 | 117 | 	void __iomem *base = info->info.scsi.io_base; | 
 | 118 | 	unsigned char *addr; | 
 | 119 |  | 
 | 120 | 	length = SCp->this_residual; | 
 | 121 | 	addr = SCp->ptr; | 
 | 122 |  | 
 | 123 | 	if (direction == DMA_OUT) { | 
 | 124 | 		unsigned int word; | 
 | 125 | 		while (length > 256) { | 
 | 126 | 			if (readb(base + 0x80) & STAT_INT) { | 
 | 127 | 				error = 1; | 
 | 128 | 				break; | 
 | 129 | 			} | 
 | 130 | 			arxescsi_pseudo_dma_write(addr, base); | 
 | 131 | 			addr += 256; | 
 | 132 | 			length -= 256; | 
 | 133 | 		} | 
 | 134 |  | 
 | 135 | 		if (!error) | 
 | 136 | 			while (length > 0) { | 
 | 137 | 				if (readb(base + 0x80) & STAT_INT) | 
 | 138 | 					break; | 
 | 139 | 	  | 
 | 140 | 				if (!(readb(base + DMASTAT_OFFSET) & DMASTAT_DRQ)) | 
 | 141 | 					continue; | 
 | 142 |  | 
 | 143 | 				word = *addr | *(addr + 1) << 8; | 
 | 144 |  | 
 | 145 | 				writew(word, base + DMADATA_OFFSET); | 
 | 146 | 				if (length > 1) { | 
 | 147 | 					addr += 2; | 
 | 148 | 					length -= 2; | 
 | 149 | 				} else { | 
 | 150 | 					addr += 1; | 
 | 151 | 					length -= 1; | 
 | 152 | 				} | 
 | 153 | 			} | 
 | 154 | 	} | 
 | 155 | 	else { | 
 | 156 | 		if (transfer && (transfer & 255)) { | 
 | 157 | 			while (length >= 256) { | 
 | 158 | 				if (readb(base + 0x80) & STAT_INT) { | 
 | 159 | 					error = 1; | 
 | 160 | 					break; | 
 | 161 | 				} | 
 | 162 | 	     | 
 | 163 | 				if (!(readb(base + DMASTAT_OFFSET) & DMASTAT_DRQ)) | 
 | 164 | 					continue; | 
 | 165 |  | 
 | 166 | 				readsw(base + DMADATA_OFFSET, addr, 256 >> 1); | 
 | 167 | 				addr += 256; | 
 | 168 | 				length -= 256; | 
 | 169 | 			} | 
 | 170 | 		} | 
 | 171 |  | 
 | 172 | 		if (!(error)) | 
 | 173 | 			while (length > 0) { | 
 | 174 | 				unsigned long word; | 
 | 175 |  | 
 | 176 | 				if (readb(base + 0x80) & STAT_INT) | 
 | 177 | 					break; | 
 | 178 |  | 
 | 179 | 				if (!(readb(base + DMASTAT_OFFSET) & DMASTAT_DRQ)) | 
 | 180 | 					continue; | 
 | 181 |  | 
 | 182 | 				word = readw(base + DMADATA_OFFSET); | 
 | 183 | 				*addr++ = word; | 
 | 184 | 				if (--length > 0) { | 
 | 185 | 					*addr++ = word >> 8; | 
 | 186 | 					length --; | 
 | 187 | 				} | 
 | 188 | 			} | 
 | 189 | 	} | 
 | 190 | } | 
 | 191 |  | 
 | 192 | /* | 
 | 193 |  * Function: int arxescsi_dma_stop(host, SCpnt) | 
 | 194 |  * Purpose : stops DMA/PIO | 
 | 195 |  * Params  : host  - host | 
 | 196 |  *	     SCpnt - command | 
 | 197 |  */ | 
| Christoph Hellwig | 0a04137 | 2005-10-31 18:31:56 +0100 | [diff] [blame] | 198 | static void arxescsi_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { | 
 | 200 | 	/* | 
 | 201 | 	 * no DMA to stop | 
 | 202 | 	 */ | 
 | 203 | } | 
 | 204 |  | 
 | 205 | /* | 
 | 206 |  * Function: const char *arxescsi_info(struct Scsi_Host * host) | 
 | 207 |  * Purpose : returns a descriptive string about this interface, | 
 | 208 |  * Params  : host - driver host structure to return info for. | 
 | 209 |  * Returns : pointer to a static buffer containing null terminated string. | 
 | 210 |  */ | 
 | 211 | static const char *arxescsi_info(struct Scsi_Host *host) | 
 | 212 | { | 
 | 213 | 	struct arxescsi_info *info = (struct arxescsi_info *)host->hostdata; | 
 | 214 | 	static char string[150]; | 
 | 215 |  | 
 | 216 | 	sprintf(string, "%s (%s) in slot %d v%s", | 
 | 217 | 		host->hostt->name, info->info.scsi.type, info->ec->slot_no, | 
 | 218 | 		VERSION); | 
 | 219 |  | 
 | 220 | 	return string; | 
 | 221 | } | 
 | 222 |  | 
 | 223 | /* | 
 | 224 |  * Function: int arxescsi_proc_info(char *buffer, char **start, off_t offset, | 
 | 225 |  *					 int length, int host_no, int inout) | 
 | 226 |  * Purpose : Return information about the driver to a user process accessing | 
 | 227 |  *	     the /proc filesystem. | 
 | 228 |  * Params  : buffer - a buffer to write information to | 
 | 229 |  *	     start  - a pointer into this buffer set by this routine to the start | 
 | 230 |  *		      of the required information. | 
 | 231 |  *	     offset - offset into information that we have read upto. | 
 | 232 |  *	     length - length of buffer | 
 | 233 |  *	     host_no - host number to return information for | 
 | 234 |  *	     inout  - 0 for reading, 1 for writing. | 
 | 235 |  * Returns : length of data written to buffer. | 
 | 236 |  */ | 
 | 237 | static int | 
 | 238 | arxescsi_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length, | 
 | 239 | 		   int inout) | 
 | 240 | { | 
 | 241 | 	struct arxescsi_info *info; | 
 | 242 | 	char *p = buffer; | 
 | 243 | 	int pos; | 
 | 244 |  | 
 | 245 | 	info = (struct arxescsi_info *)host->hostdata; | 
 | 246 | 	if (inout == 1) | 
 | 247 | 		return -EINVAL; | 
 | 248 |  | 
 | 249 | 	p += sprintf(p, "ARXE 16-bit SCSI driver v%s\n", VERSION); | 
 | 250 | 	p += fas216_print_host(&info->info, p); | 
 | 251 | 	p += fas216_print_stats(&info->info, p); | 
 | 252 | 	p += fas216_print_devices(&info->info, p); | 
 | 253 |  | 
 | 254 | 	*start = buffer + offset; | 
 | 255 | 	pos = p - buffer - offset; | 
 | 256 | 	if (pos > length) | 
 | 257 | 		pos = length; | 
 | 258 |  | 
 | 259 | 	return pos; | 
 | 260 | } | 
 | 261 |  | 
| Christoph Hellwig | d0be4a7d | 2005-10-31 18:31:40 +0100 | [diff] [blame] | 262 | static struct scsi_host_template arxescsi_template = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | 	.proc_info			= arxescsi_proc_info, | 
 | 264 | 	.name				= "ARXE SCSI card", | 
 | 265 | 	.info				= arxescsi_info, | 
 | 266 | 	.queuecommand			= fas216_noqueue_command, | 
 | 267 | 	.eh_host_reset_handler		= fas216_eh_host_reset, | 
 | 268 | 	.eh_bus_reset_handler		= fas216_eh_bus_reset, | 
 | 269 | 	.eh_device_reset_handler	= fas216_eh_device_reset, | 
 | 270 | 	.eh_abort_handler		= fas216_eh_abort, | 
 | 271 | 	.can_queue			= 0, | 
 | 272 | 	.this_id			= 7, | 
 | 273 | 	.sg_tablesize			= SG_ALL, | 
 | 274 | 	.cmd_per_lun			= 1, | 
 | 275 | 	.use_clustering			= DISABLE_CLUSTERING, | 
 | 276 | 	.proc_name			= "arxescsi", | 
 | 277 | }; | 
 | 278 |  | 
 | 279 | static int __devinit | 
 | 280 | arxescsi_probe(struct expansion_card *ec, const struct ecard_id *id) | 
 | 281 | { | 
 | 282 | 	struct Scsi_Host *host; | 
 | 283 | 	struct arxescsi_info *info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | 	void __iomem *base; | 
 | 285 | 	int ret; | 
 | 286 |  | 
 | 287 | 	ret = ecard_request_resources(ec); | 
 | 288 | 	if (ret) | 
 | 289 | 		goto out; | 
 | 290 |  | 
| Russell King | 10bdaaa | 2007-05-10 18:40:51 +0100 | [diff] [blame] | 291 | 	base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | 	if (!base) { | 
 | 293 | 		ret = -ENOMEM; | 
 | 294 | 		goto out_region; | 
 | 295 | 	} | 
 | 296 |  | 
 | 297 | 	host = scsi_host_alloc(&arxescsi_template, sizeof(struct arxescsi_info)); | 
 | 298 | 	if (!host) { | 
 | 299 | 		ret = -ENOMEM; | 
| Russell King | 10bdaaa | 2007-05-10 18:40:51 +0100 | [diff] [blame] | 300 | 		goto out_region; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | 	} | 
 | 302 |  | 
 | 303 | 	info = (struct arxescsi_info *)host->hostdata; | 
 | 304 | 	info->ec = ec; | 
 | 305 | 	info->base = base; | 
 | 306 |  | 
 | 307 | 	info->info.scsi.io_base		= base + 0x2000; | 
 | 308 | 	info->info.scsi.irq		= NO_IRQ; | 
 | 309 | 	info->info.scsi.dma		= NO_DMA; | 
 | 310 | 	info->info.scsi.io_shift	= 5; | 
 | 311 | 	info->info.ifcfg.clockrate	= 24; /* MHz */ | 
 | 312 | 	info->info.ifcfg.select_timeout = 255; | 
 | 313 | 	info->info.ifcfg.asyncperiod	= 200; /* ns */ | 
 | 314 | 	info->info.ifcfg.sync_max_depth	= 0; | 
 | 315 | 	info->info.ifcfg.cntl3		= CNTL3_FASTSCSI | CNTL3_FASTCLK; | 
 | 316 | 	info->info.ifcfg.disconnect_ok	= 0; | 
 | 317 | 	info->info.ifcfg.wide_max_size	= 0; | 
 | 318 | 	info->info.ifcfg.capabilities	= FASCAP_PSEUDODMA; | 
 | 319 | 	info->info.dma.setup		= arxescsi_dma_setup; | 
 | 320 | 	info->info.dma.pseudo		= arxescsi_dma_pseudo; | 
 | 321 | 	info->info.dma.stop		= arxescsi_dma_stop; | 
 | 322 | 		 | 
 | 323 | 	ec->irqaddr = base; | 
 | 324 | 	ec->irqmask = CSTATUS_IRQ; | 
 | 325 |  | 
 | 326 | 	ret = fas216_init(host); | 
 | 327 | 	if (ret) | 
 | 328 | 		goto out_unregister; | 
 | 329 |  | 
 | 330 | 	ret = fas216_add(host, &ec->dev); | 
 | 331 | 	if (ret == 0) | 
 | 332 | 		goto out; | 
 | 333 |  | 
 | 334 | 	fas216_release(host); | 
 | 335 |  out_unregister: | 
 | 336 | 	scsi_host_put(host); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 |  out_region: | 
 | 338 | 	ecard_release_resources(ec); | 
 | 339 |  out: | 
 | 340 | 	return ret; | 
 | 341 | } | 
 | 342 |  | 
 | 343 | static void __devexit arxescsi_remove(struct expansion_card *ec) | 
 | 344 | { | 
 | 345 | 	struct Scsi_Host *host = ecard_get_drvdata(ec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 |  | 
 | 347 | 	ecard_set_drvdata(ec, NULL); | 
 | 348 | 	fas216_remove(host); | 
 | 349 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | 	fas216_release(host); | 
 | 351 | 	scsi_host_put(host); | 
 | 352 | 	ecard_release_resources(ec); | 
 | 353 | } | 
 | 354 |  | 
 | 355 | static const struct ecard_id arxescsi_cids[] = { | 
 | 356 | 	{ MANU_ARXE, PROD_ARXE_SCSI }, | 
 | 357 | 	{ 0xffff, 0xffff }, | 
 | 358 | }; | 
 | 359 |  | 
 | 360 | static struct ecard_driver arxescsi_driver = { | 
 | 361 | 	.probe		= arxescsi_probe, | 
 | 362 | 	.remove		= __devexit_p(arxescsi_remove), | 
 | 363 | 	.id_table	= arxescsi_cids, | 
 | 364 | 	.drv = { | 
 | 365 | 		.name		= "arxescsi", | 
 | 366 | 	}, | 
 | 367 | }; | 
 | 368 |  | 
 | 369 | static int __init init_arxe_scsi_driver(void) | 
 | 370 | { | 
 | 371 | 	return ecard_register_driver(&arxescsi_driver); | 
 | 372 | } | 
 | 373 |  | 
 | 374 | static void __exit exit_arxe_scsi_driver(void) | 
 | 375 | { | 
 | 376 | 	ecard_remove_driver(&arxescsi_driver); | 
 | 377 | } | 
 | 378 |  | 
 | 379 | module_init(init_arxe_scsi_driver); | 
 | 380 | module_exit(exit_arxe_scsi_driver); | 
 | 381 |  | 
 | 382 | MODULE_AUTHOR("Stefan Hanske"); | 
 | 383 | MODULE_DESCRIPTION("ARXESCSI driver for Acorn machines"); | 
 | 384 | MODULE_LICENSE("GPL"); | 
 | 385 |  |