1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3*  sym53c500_cs.c	Bob Tracy (rct@frus.com)
4*
5*  A rewrite of the pcmcia-cs add-on driver for newer (circa 1997)
6*  New Media Bus Toaster PCMCIA SCSI cards using the Symbios Logic
7*  53c500 controller: intended for use with 2.6 and later kernels.
8*  The pcmcia-cs add-on version of this driver is not supported
9*  beyond 2.4.  It consisted of three files with history/copyright
10*  information as follows:
11*
12*  SYM53C500.h
13*	Bob Tracy (rct@frus.com)
14*	Original by Tom Corner (tcorner@via.at).
15*	Adapted from NCR53c406a.h which is Copyrighted (C) 1994
16*	Normunds Saumanis (normunds@rx.tech.swh.lv)
17*
18*  SYM53C500.c
19*	Bob Tracy (rct@frus.com)
20*	Original driver by Tom Corner (tcorner@via.at) was adapted
21*	from NCR53c406a.c which is Copyrighted (C) 1994, 1995, 1996
22*	Normunds Saumanis (normunds@fi.ibm.com)
23*
24*  sym53c500.c
25*	Bob Tracy (rct@frus.com)
26*	Original by Tom Corner (tcorner@via.at) was adapted from a
27*	driver for the Qlogic SCSI card written by
28*	David Hinds (dhinds@allegro.stanford.edu).
29*/
30
31#define SYM53C500_DEBUG 0
32#define VERBOSE_SYM53C500_DEBUG 0
33
34/*
35*  Set this to 0 if you encounter kernel lockups while transferring
36*  data in PIO mode.  Note this can be changed via "sysfs".
37*/
38#define USE_FAST_PIO 1
39
40/* =============== End of user configurable parameters ============== */
41
42#include <linux/module.h>
43#include <linux/moduleparam.h>
44#include <linux/errno.h>
45#include <linux/init.h>
46#include <linux/interrupt.h>
47#include <linux/kernel.h>
48#include <linux/slab.h>
49#include <linux/string.h>
50#include <linux/ioport.h>
51#include <linux/blkdev.h>
52#include <linux/spinlock.h>
53#include <linux/bitops.h>
54
55#include <asm/io.h>
56#include <asm/dma.h>
57#include <asm/irq.h>
58
59#include <scsi/scsi_ioctl.h>
60#include <scsi/scsi_cmnd.h>
61#include <scsi/scsi_device.h>
62#include <scsi/scsi.h>
63#include <scsi/scsi_host.h>
64
65#include <pcmcia/cistpl.h>
66#include <pcmcia/ds.h>
67#include <pcmcia/ciscode.h>
68
69
70/* ================================================================== */
71
72#define SYNC_MODE 0 		/* Synchronous transfer mode */
73
74/* Default configuration */
75#define C1_IMG   0x07		/* ID=7 */
76#define C2_IMG   0x48		/* FE SCSI2 */
77#define C3_IMG   0x20		/* CDB */
78#define C4_IMG   0x04		/* ANE */
79#define C5_IMG   0xa4		/* ? changed from b6= AA PI SIE POL */
80#define C7_IMG   0x80		/* added for SYM53C500 t. corner */
81
82/* Hardware Registers: offsets from io_port (base) */
83
84/* Control Register Set 0 */
85#define TC_LSB		0x00		/* transfer counter lsb */
86#define TC_MSB		0x01		/* transfer counter msb */
87#define SCSI_FIFO	0x02		/* scsi fifo register */
88#define CMD_REG		0x03		/* command register */
89#define STAT_REG	0x04		/* status register */
90#define DEST_ID		0x04		/* selection/reselection bus id */
91#define INT_REG		0x05		/* interrupt status register */
92#define SRTIMOUT	0x05		/* select/reselect timeout reg */
93#define SEQ_REG		0x06		/* sequence step register */
94#define SYNCPRD		0x06		/* synchronous transfer period */
95#define FIFO_FLAGS	0x07		/* indicates # of bytes in fifo */
96#define SYNCOFF		0x07		/* synchronous offset register */
97#define CONFIG1		0x08		/* configuration register */
98#define CLKCONV		0x09		/* clock conversion register */
99/* #define TESTREG	0x0A */		/* test mode register */
100#define CONFIG2		0x0B		/* configuration 2 register */
101#define CONFIG3		0x0C		/* configuration 3 register */
102#define CONFIG4		0x0D		/* configuration 4 register */
103#define TC_HIGH		0x0E		/* transfer counter high */
104/* #define FIFO_BOTTOM	0x0F */		/* reserve FIFO byte register */
105
106/* Control Register Set 1 */
107/* #define JUMPER_SENSE	0x00 */		/* jumper sense port reg (r/w) */
108/* #define SRAM_PTR	0x01 */		/* SRAM address pointer reg (r/w) */
109/* #define SRAM_DATA	0x02 */		/* SRAM data register (r/w) */
110#define PIO_FIFO	0x04		/* PIO FIFO registers (r/w) */
111/* #define PIO_FIFO1	0x05 */		/*  */
112/* #define PIO_FIFO2	0x06 */		/*  */
113/* #define PIO_FIFO3	0x07 */		/*  */
114#define PIO_STATUS	0x08		/* PIO status (r/w) */
115/* #define ATA_CMD	0x09 */		/* ATA command/status reg (r/w) */
116/* #define ATA_ERR	0x0A */		/* ATA features/error reg (r/w) */
117#define PIO_FLAG	0x0B		/* PIO flag interrupt enable (r/w) */
118#define CONFIG5		0x09		/* configuration 5 register */
119/* #define SIGNATURE	0x0E */		/* signature register (r) */
120/* #define CONFIG6	0x0F */		/* configuration 6 register (r) */
121#define CONFIG7		0x0d
122
123/* select register set 0 */
124#define REG0(x)		(outb(C4_IMG, (x) + CONFIG4))
125/* select register set 1 */
126#define REG1(x)		outb(C7_IMG, (x) + CONFIG7); outb(C5_IMG, (x) + CONFIG5)
127
128#if SYM53C500_DEBUG
129#define DEB(x) x
130#else
131#define DEB(x)
132#endif
133
134#if VERBOSE_SYM53C500_DEBUG
135#define VDEB(x) x
136#else
137#define VDEB(x)
138#endif
139
140#define LOAD_DMA_COUNT(x, count) \
141  outb(count & 0xff, (x) + TC_LSB); \
142  outb((count >> 8) & 0xff, (x) + TC_MSB); \
143  outb((count >> 16) & 0xff, (x) + TC_HIGH);
144
145/* Chip commands */
146#define DMA_OP               0x80
147
148#define SCSI_NOP             0x00
149#define FLUSH_FIFO           0x01
150#define CHIP_RESET           0x02
151#define SCSI_RESET           0x03
152#define RESELECT             0x40
153#define SELECT_NO_ATN        0x41
154#define SELECT_ATN           0x42
155#define SELECT_ATN_STOP      0x43
156#define ENABLE_SEL           0x44
157#define DISABLE_SEL          0x45
158#define SELECT_ATN3          0x46
159#define RESELECT3            0x47
160#define TRANSFER_INFO        0x10
161#define INIT_CMD_COMPLETE    0x11
162#define MSG_ACCEPT           0x12
163#define TRANSFER_PAD         0x18
164#define SET_ATN              0x1a
165#define RESET_ATN            0x1b
166#define SEND_MSG             0x20
167#define SEND_STATUS          0x21
168#define SEND_DATA            0x22
169#define DISCONN_SEQ          0x23
170#define TERMINATE_SEQ        0x24
171#define TARG_CMD_COMPLETE    0x25
172#define DISCONN              0x27
173#define RECV_MSG             0x28
174#define RECV_CMD             0x29
175#define RECV_DATA            0x2a
176#define RECV_CMD_SEQ         0x2b
177#define TARGET_ABORT_DMA     0x04
178
179/* ================================================================== */
180
181struct scsi_info_t {
182	struct pcmcia_device	*p_dev;
183	struct Scsi_Host *host;
184	unsigned short manf_id;
185};
186
187/*
188*  Repository for per-instance host data.
189*/
190struct sym53c500_data {
191	struct scsi_cmnd *current_SC;
192	int fast_pio;
193};
194
195struct sym53c500_cmd_priv {
196	int status;
197	int message;
198	int phase;
199};
200
201enum Phase {
202    idle,
203    data_out,
204    data_in,
205    command_ph,
206    status_ph,
207    message_out,
208    message_in
209};
210
211/* ================================================================== */
212
213static void
214chip_init(int io_port)
215{
216	REG1(io_port);
217	outb(0x01, io_port + PIO_STATUS);
218	outb(0x00, io_port + PIO_FLAG);
219
220	outb(C4_IMG, io_port + CONFIG4);	/* REG0(io_port); */
221	outb(C3_IMG, io_port + CONFIG3);
222	outb(C2_IMG, io_port + CONFIG2);
223	outb(C1_IMG, io_port + CONFIG1);
224
225	outb(0x05, io_port + CLKCONV);	/* clock conversion factor */
226	outb(0x9C, io_port + SRTIMOUT);	/* Selection timeout */
227	outb(0x05, io_port + SYNCPRD);	/* Synchronous transfer period */
228	outb(SYNC_MODE, io_port + SYNCOFF);	/* synchronous mode */
229}
230
231static void
232SYM53C500_int_host_reset(int io_port)
233{
234	outb(C4_IMG, io_port + CONFIG4);	/* REG0(io_port); */
235	outb(CHIP_RESET, io_port + CMD_REG);
236	outb(SCSI_NOP, io_port + CMD_REG);	/* required after reset */
237	outb(SCSI_RESET, io_port + CMD_REG);
238	chip_init(io_port);
239}
240
241static __inline__ int
242SYM53C500_pio_read(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
243{
244	int i;
245	int len;	/* current scsi fifo size */
246
247	REG1(base);
248	while (reqlen) {
249		i = inb(base + PIO_STATUS);
250		/* VDEB(printk("pio_status=%x\n", i)); */
251		if (i & 0x80)
252			return 0;
253
254		switch (i & 0x1e) {
255		default:
256		case 0x10:	/* fifo empty */
257			len = 0;
258			break;
259		case 0x0:
260			len = 1;
261			break;
262		case 0x8:	/* fifo 1/3 full */
263			len = 42;
264			break;
265		case 0xc:	/* fifo 2/3 full */
266			len = 84;
267			break;
268		case 0xe:	/* fifo full */
269			len = 128;
270			break;
271		}
272
273		if ((i & 0x40) && len == 0) { /* fifo empty and interrupt occurred */
274			return 0;
275		}
276
277		if (len) {
278			if (len > reqlen)
279				len = reqlen;
280
281			if (fast_pio && len > 3) {
282				insl(base + PIO_FIFO, request, len >> 2);
283				request += len & 0xfc;
284				reqlen -= len & 0xfc;
285			} else {
286				while (len--) {
287					*request++ = inb(base + PIO_FIFO);
288					reqlen--;
289				}
290			}
291		}
292	}
293	return 0;
294}
295
296static __inline__ int
297SYM53C500_pio_write(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
298{
299	int i = 0;
300	int len;	/* current scsi fifo size */
301
302	REG1(base);
303	while (reqlen && !(i & 0x40)) {
304		i = inb(base + PIO_STATUS);
305		/* VDEB(printk("pio_status=%x\n", i)); */
306		if (i & 0x80)	/* error */
307			return 0;
308
309		switch (i & 0x1e) {
310		case 0x10:
311			len = 128;
312			break;
313		case 0x0:
314			len = 84;
315			break;
316		case 0x8:
317			len = 42;
318			break;
319		case 0xc:
320			len = 1;
321			break;
322		default:
323		case 0xe:
324			len = 0;
325			break;
326		}
327
328		if (len) {
329			if (len > reqlen)
330				len = reqlen;
331
332			if (fast_pio && len > 3) {
333				outsl(base + PIO_FIFO, request, len >> 2);
334				request += len & 0xfc;
335				reqlen -= len & 0xfc;
336			} else {
337				while (len--) {
338					outb(*request++, base + PIO_FIFO);
339					reqlen--;
340				}
341			}
342		}
343	}
344	return 0;
345}
346
347static irqreturn_t
348SYM53C500_intr(int irq, void *dev_id)
349{
350	unsigned long flags;
351	struct Scsi_Host *dev = dev_id;
352	DEB(unsigned char fifo_size;)
353	DEB(unsigned char seq_reg;)
354	unsigned char status, int_reg;
355	unsigned char pio_status;
356	int port_base = dev->io_port;
357	struct sym53c500_data *data =
358	    (struct sym53c500_data *)dev->hostdata;
359	struct scsi_cmnd *curSC = data->current_SC;
360	struct sym53c500_cmd_priv *scp = scsi_cmd_priv(curSC);
361	int fast_pio = data->fast_pio;
362
363	spin_lock_irqsave(dev->host_lock, flags);
364
365	VDEB(printk("SYM53C500_intr called\n"));
366
367	REG1(port_base);
368	pio_status = inb(port_base + PIO_STATUS);
369	REG0(port_base);
370	status = inb(port_base + STAT_REG);
371	DEB(seq_reg = inb(port_base + SEQ_REG));
372	int_reg = inb(port_base + INT_REG);
373	DEB(fifo_size = inb(port_base + FIFO_FLAGS) & 0x1f);
374
375#if SYM53C500_DEBUG
376	printk("status=%02x, seq_reg=%02x, int_reg=%02x, fifo_size=%02x",
377	    status, seq_reg, int_reg, fifo_size);
378	printk(", pio=%02x\n", pio_status);
379#endif /* SYM53C500_DEBUG */
380
381	if (int_reg & 0x80) {	/* SCSI reset intr */
382		DEB(printk("SYM53C500: reset intr received\n"));
383		curSC->result = DID_RESET << 16;
384		goto idle_out;
385	}
386
387	if (pio_status & 0x80) {
388		printk("SYM53C500: Warning: PIO error!\n");
389		curSC->result = DID_ERROR << 16;
390		goto idle_out;
391	}
392
393	if (status & 0x20) {		/* Parity error */
394		printk("SYM53C500: Warning: parity error!\n");
395		curSC->result = DID_PARITY << 16;
396		goto idle_out;
397	}
398
399	if (status & 0x40) {		/* Gross error */
400		printk("SYM53C500: Warning: gross error!\n");
401		curSC->result = DID_ERROR << 16;
402		goto idle_out;
403	}
404
405	if (int_reg & 0x20) {		/* Disconnect */
406		DEB(printk("SYM53C500: disconnect intr received\n"));
407		if (scp->phase != message_in) {	/* Unexpected disconnect */
408			curSC->result = DID_NO_CONNECT << 16;
409		} else {	/* Command complete, return status and message */
410			curSC->result = (scp->status & 0xff) |
411				((scp->message & 0xff) << 8) | (DID_OK << 16);
412		}
413		goto idle_out;
414	}
415
416	switch (status & 0x07) {	/* scsi phase */
417	case 0x00:			/* DATA-OUT */
418		if (int_reg & 0x10) {	/* Target requesting info transfer */
419			struct scatterlist *sg;
420			int i;
421
422			scp->phase = data_out;
423			VDEB(printk("SYM53C500: Data-Out phase\n"));
424			outb(FLUSH_FIFO, port_base + CMD_REG);
425			LOAD_DMA_COUNT(port_base, scsi_bufflen(curSC));	/* Max transfer size */
426			outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
427
428			scsi_for_each_sg(curSC, sg, scsi_sg_count(curSC), i) {
429				SYM53C500_pio_write(fast_pio, port_base,
430				    sg_virt(sg), sg->length);
431			}
432			REG0(port_base);
433		}
434		break;
435
436	case 0x01:		/* DATA-IN */
437		if (int_reg & 0x10) {	/* Target requesting info transfer */
438			struct scatterlist *sg;
439			int i;
440
441			scp->phase = data_in;
442			VDEB(printk("SYM53C500: Data-In phase\n"));
443			outb(FLUSH_FIFO, port_base + CMD_REG);
444			LOAD_DMA_COUNT(port_base, scsi_bufflen(curSC));	/* Max transfer size */
445			outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
446
447			scsi_for_each_sg(curSC, sg, scsi_sg_count(curSC), i) {
448				SYM53C500_pio_read(fast_pio, port_base,
449					sg_virt(sg), sg->length);
450			}
451			REG0(port_base);
452		}
453		break;
454
455	case 0x02:		/* COMMAND */
456		scp->phase = command_ph;
457		printk("SYM53C500: Warning: Unknown interrupt occurred in command phase!\n");
458		break;
459
460	case 0x03:		/* STATUS */
461		scp->phase = status_ph;
462		VDEB(printk("SYM53C500: Status phase\n"));
463		outb(FLUSH_FIFO, port_base + CMD_REG);
464		outb(INIT_CMD_COMPLETE, port_base + CMD_REG);
465		break;
466
467	case 0x04:		/* Reserved */
468	case 0x05:		/* Reserved */
469		printk("SYM53C500: WARNING: Reserved phase!!!\n");
470		break;
471
472	case 0x06:		/* MESSAGE-OUT */
473		DEB(printk("SYM53C500: Message-Out phase\n"));
474		scp->phase = message_out;
475		outb(SET_ATN, port_base + CMD_REG);	/* Reject the message */
476		outb(MSG_ACCEPT, port_base + CMD_REG);
477		break;
478
479	case 0x07:		/* MESSAGE-IN */
480		VDEB(printk("SYM53C500: Message-In phase\n"));
481		scp->phase = message_in;
482
483		scp->status = inb(port_base + SCSI_FIFO);
484		scp->message = inb(port_base + SCSI_FIFO);
485
486		VDEB(printk("SCSI FIFO size=%d\n", inb(port_base + FIFO_FLAGS) & 0x1f));
487		DEB(printk("Status = %02x  Message = %02x\n", scp->status, scp->message));
488
489		if (scp->message == SAVE_POINTERS || scp->message == DISCONNECT) {
490			outb(SET_ATN, port_base + CMD_REG);	/* Reject message */
491			DEB(printk("Discarding SAVE_POINTERS message\n"));
492		}
493		outb(MSG_ACCEPT, port_base + CMD_REG);
494		break;
495	}
496out:
497	spin_unlock_irqrestore(dev->host_lock, flags);
498	return IRQ_HANDLED;
499
500idle_out:
501	scp->phase = idle;
502	scsi_done(curSC);
503	goto out;
504}
505
506static void
507SYM53C500_release(struct pcmcia_device *link)
508{
509	struct scsi_info_t *info = link->priv;
510	struct Scsi_Host *shost = info->host;
511
512	dev_dbg(&link->dev, "SYM53C500_release\n");
513
514	/*
515	*  Do this before releasing/freeing resources.
516	*/
517	scsi_remove_host(shost);
518
519	/*
520	*  Interrupts getting hosed on card removal.  Try
521	*  the following code, mostly from qlogicfas.c.
522	*/
523	if (shost->irq)
524		free_irq(shost->irq, shost);
525	if (shost->io_port && shost->n_io_port)
526		release_region(shost->io_port, shost->n_io_port);
527
528	pcmcia_disable_device(link);
529
530	scsi_host_put(shost);
531} /* SYM53C500_release */
532
533static const char*
534SYM53C500_info(struct Scsi_Host *SChost)
535{
536	static char info_msg[256];
537	struct sym53c500_data *data =
538	    (struct sym53c500_data *)SChost->hostdata;
539
540	DEB(printk("SYM53C500_info called\n"));
541	(void)snprintf(info_msg, sizeof(info_msg),
542	    "SYM53C500 at 0x%lx, IRQ %d, %s PIO mode.",
543	    SChost->io_port, SChost->irq, data->fast_pio ? "fast" : "slow");
544	return (info_msg);
545}
546
547static int SYM53C500_queue_lck(struct scsi_cmnd *SCpnt)
548{
549	struct sym53c500_cmd_priv *scp = scsi_cmd_priv(SCpnt);
550	int i;
551	int port_base = SCpnt->device->host->io_port;
552	struct sym53c500_data *data =
553	    (struct sym53c500_data *)SCpnt->device->host->hostdata;
554
555	VDEB(printk("SYM53C500_queue called\n"));
556
557	DEB(printk("cmd=%02x, cmd_len=%02x, target=%02x, lun=%02x, bufflen=%d\n",
558	    SCpnt->cmnd[0], SCpnt->cmd_len, SCpnt->device->id,
559		   (u8)SCpnt->device->lun,  scsi_bufflen(SCpnt)));
560
561	VDEB(for (i = 0; i < SCpnt->cmd_len; i++)
562	    printk("cmd[%d]=%02x  ", i, SCpnt->cmnd[i]));
563	VDEB(printk("\n"));
564
565	data->current_SC = SCpnt;
566	scp->phase = command_ph;
567	scp->status = 0;
568	scp->message = 0;
569
570	/* We are locked here already by the mid layer */
571	REG0(port_base);
572	outb(scmd_id(SCpnt), port_base + DEST_ID);	/* set destination */
573	outb(FLUSH_FIFO, port_base + CMD_REG);	/* reset the fifos */
574
575	for (i = 0; i < SCpnt->cmd_len; i++) {
576		outb(SCpnt->cmnd[i], port_base + SCSI_FIFO);
577	}
578	outb(SELECT_NO_ATN, port_base + CMD_REG);
579
580	return 0;
581}
582
583static DEF_SCSI_QCMD(SYM53C500_queue)
584
585static int
586SYM53C500_host_reset(struct scsi_cmnd *SCpnt)
587{
588	int port_base = SCpnt->device->host->io_port;
589
590	DEB(printk("SYM53C500_host_reset called\n"));
591	spin_lock_irq(SCpnt->device->host->host_lock);
592	SYM53C500_int_host_reset(port_base);
593	spin_unlock_irq(SCpnt->device->host->host_lock);
594
595	return SUCCESS;
596}
597
598static int
599SYM53C500_biosparm(struct scsi_device *disk,
600    struct block_device *dev,
601    sector_t capacity, int *info_array)
602{
603	int size;
604
605	DEB(printk("SYM53C500_biosparm called\n"));
606
607	size = capacity;
608	info_array[0] = 64;		/* heads */
609	info_array[1] = 32;		/* sectors */
610	info_array[2] = size >> 11;	/* cylinders */
611	if (info_array[2] > 1024) {	/* big disk */
612		info_array[0] = 255;
613		info_array[1] = 63;
614		info_array[2] = size / (255 * 63);
615	}
616	return 0;
617}
618
619static ssize_t
620SYM53C500_show_pio(struct device *dev, struct device_attribute *attr,
621		   char *buf)
622{
623	struct Scsi_Host *SHp = class_to_shost(dev);
624	struct sym53c500_data *data =
625	    (struct sym53c500_data *)SHp->hostdata;
626
627	return snprintf(buf, 4, "%d\n", data->fast_pio);
628}
629
630static ssize_t
631SYM53C500_store_pio(struct device *dev, struct device_attribute *attr,
632		    const char *buf, size_t count)
633{
634	int pio;
635	struct Scsi_Host *SHp = class_to_shost(dev);
636	struct sym53c500_data *data =
637	    (struct sym53c500_data *)SHp->hostdata;
638
639	pio = simple_strtoul(buf, NULL, 0);
640	if (pio == 0 || pio == 1) {
641		data->fast_pio = pio;
642		return count;
643	}
644	else
645		return -EINVAL;
646}
647
648/*
649*  SCSI HBA device attributes we want to
650*  make available via sysfs.
651*/
652static struct device_attribute SYM53C500_pio_attr = {
653	.attr = {
654		.name = "fast_pio",
655		.mode = (S_IRUGO | S_IWUSR),
656	},
657	.show = SYM53C500_show_pio,
658	.store = SYM53C500_store_pio,
659};
660
661static struct attribute *SYM53C500_shost_attrs[] = {
662	&SYM53C500_pio_attr.attr,
663	NULL,
664};
665
666ATTRIBUTE_GROUPS(SYM53C500_shost);
667
668/*
669*  scsi_host_template initializer
670*/
671static const struct scsi_host_template sym53c500_driver_template = {
672     .module			= THIS_MODULE,
673     .name			= "SYM53C500",
674     .info			= SYM53C500_info,
675     .queuecommand		= SYM53C500_queue,
676     .eh_host_reset_handler	= SYM53C500_host_reset,
677     .bios_param		= SYM53C500_biosparm,
678     .proc_name			= "SYM53C500",
679     .can_queue			= 1,
680     .this_id			= 7,
681     .sg_tablesize		= 32,
682     .shost_groups		= SYM53C500_shost_groups,
683     .cmd_size			= sizeof(struct sym53c500_cmd_priv),
684};
685
686static int SYM53C500_config_check(struct pcmcia_device *p_dev, void *priv_data)
687{
688	p_dev->io_lines = 10;
689	p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
690	p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
691
692	if (p_dev->resource[0]->start == 0)
693		return -ENODEV;
694
695	return pcmcia_request_io(p_dev);
696}
697
698static int
699SYM53C500_config(struct pcmcia_device *link)
700{
701	struct scsi_info_t *info = link->priv;
702	int ret;
703	int irq_level, port_base;
704	struct Scsi_Host *host;
705	const struct scsi_host_template *tpnt = &sym53c500_driver_template;
706	struct sym53c500_data *data;
707
708	dev_dbg(&link->dev, "SYM53C500_config\n");
709
710	info->manf_id = link->manf_id;
711
712	ret = pcmcia_loop_config(link, SYM53C500_config_check, NULL);
713	if (ret)
714		goto failed;
715
716	if (!link->irq)
717		goto failed;
718
719	ret = pcmcia_enable_device(link);
720	if (ret)
721		goto failed;
722
723	/*
724	*  That's the trouble with copying liberally from another driver.
725	*  Some things probably aren't relevant, and I suspect this entire
726	*  section dealing with manufacturer IDs can be scrapped.	--rct
727	*/
728	if ((info->manf_id == MANFID_MACNICA) ||
729	    (info->manf_id == MANFID_PIONEER) ||
730	    (info->manf_id == 0x0098)) {
731		/* set ATAcmd */
732		outb(0xb4, link->resource[0]->start + 0xd);
733		outb(0x24, link->resource[0]->start + 0x9);
734		outb(0x04, link->resource[0]->start + 0xd);
735	}
736
737	/*
738	*  irq_level == 0 implies tpnt->can_queue == 0, which
739	*  is not supported in 2.6.  Thus, only irq_level > 0
740	*  will be allowed.
741	*
742	*  Possible port_base values are as follows:
743	*
744	*	0x130, 0x230, 0x280, 0x290,
745	*	0x320, 0x330, 0x340, 0x350
746	*/
747	port_base = link->resource[0]->start;
748	irq_level = link->irq;
749
750	DEB(printk("SYM53C500: port_base=0x%x, irq=%d, fast_pio=%d\n",
751	    port_base, irq_level, USE_FAST_PIO);)
752
753	chip_init(port_base);
754
755	host = scsi_host_alloc(tpnt, sizeof(struct sym53c500_data));
756	if (!host) {
757		printk("SYM53C500: Unable to register host, giving up.\n");
758		goto err_release;
759	}
760
761	data = (struct sym53c500_data *)host->hostdata;
762
763	if (irq_level > 0) {
764		if (request_irq(irq_level, SYM53C500_intr, IRQF_SHARED, "SYM53C500", host)) {
765			printk("SYM53C500: unable to allocate IRQ %d\n", irq_level);
766			goto err_free_scsi;
767		}
768		DEB(printk("SYM53C500: allocated IRQ %d\n", irq_level));
769	} else if (irq_level == 0) {
770		DEB(printk("SYM53C500: No interrupts detected\n"));
771		goto err_free_scsi;
772	} else {
773		DEB(printk("SYM53C500: Shouldn't get here!\n"));
774		goto err_free_scsi;
775	}
776
777	host->unique_id = port_base;
778	host->irq = irq_level;
779	host->io_port = port_base;
780	host->n_io_port = 0x10;
781	host->dma_channel = -1;
782
783	/*
784	*  Note fast_pio is set to USE_FAST_PIO by
785	*  default, but can be changed via "sysfs".
786	*/
787	data->fast_pio = USE_FAST_PIO;
788
789	info->host = host;
790
791	if (scsi_add_host(host, NULL))
792		goto err_free_irq;
793
794	scsi_scan_host(host);
795
796	return 0;
797
798err_free_irq:
799	free_irq(irq_level, host);
800err_free_scsi:
801	scsi_host_put(host);
802err_release:
803	release_region(port_base, 0x10);
804	printk(KERN_INFO "sym53c500_cs: no SCSI devices found\n");
805	return -ENODEV;
806
807failed:
808	SYM53C500_release(link);
809	return -ENODEV;
810} /* SYM53C500_config */
811
812static int sym53c500_resume(struct pcmcia_device *link)
813{
814	struct scsi_info_t *info = link->priv;
815
816	/* See earlier comment about manufacturer IDs. */
817	if ((info->manf_id == MANFID_MACNICA) ||
818	    (info->manf_id == MANFID_PIONEER) ||
819	    (info->manf_id == 0x0098)) {
820		outb(0x80, link->resource[0]->start + 0xd);
821		outb(0x24, link->resource[0]->start + 0x9);
822		outb(0x04, link->resource[0]->start + 0xd);
823	}
824	/*
825	 *  If things don't work after a "resume",
826	 *  this is a good place to start looking.
827	 */
828	SYM53C500_int_host_reset(link->resource[0]->start);
829
830	return 0;
831}
832
833static void
834SYM53C500_detach(struct pcmcia_device *link)
835{
836	dev_dbg(&link->dev, "SYM53C500_detach\n");
837
838	SYM53C500_release(link);
839
840	kfree(link->priv);
841	link->priv = NULL;
842} /* SYM53C500_detach */
843
844static int
845SYM53C500_probe(struct pcmcia_device *link)
846{
847	struct scsi_info_t *info;
848
849	dev_dbg(&link->dev, "SYM53C500_attach()\n");
850
851	/* Create new SCSI device */
852	info = kzalloc(sizeof(*info), GFP_KERNEL);
853	if (!info)
854		return -ENOMEM;
855	info->p_dev = link;
856	link->priv = info;
857	link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
858
859	return SYM53C500_config(link);
860} /* SYM53C500_attach */
861
862MODULE_AUTHOR("Bob Tracy <rct@frus.com>");
863MODULE_DESCRIPTION("SYM53C500 PCMCIA SCSI driver");
864MODULE_LICENSE("GPL");
865
866static const struct pcmcia_device_id sym53c500_ids[] = {
867	PCMCIA_DEVICE_PROD_ID12("BASICS by New Media Corporation", "SCSI Sym53C500", 0x23c78a9d, 0x0099e7f7),
868	PCMCIA_DEVICE_PROD_ID12("New Media Corporation", "SCSI Bus Toaster Sym53C500", 0x085a850b, 0x45432eb8),
869	PCMCIA_DEVICE_PROD_ID2("SCSI9000", 0x21648f44),
870	PCMCIA_DEVICE_NULL,
871};
872MODULE_DEVICE_TABLE(pcmcia, sym53c500_ids);
873
874static struct pcmcia_driver sym53c500_cs_driver = {
875	.owner		= THIS_MODULE,
876	.name		= "sym53c500_cs",
877	.probe		= SYM53C500_probe,
878	.remove		= SYM53C500_detach,
879	.id_table       = sym53c500_ids,
880	.resume		= sym53c500_resume,
881};
882module_pcmcia_driver(sym53c500_cs_driver);
883