f8a89ec25042f9acba0e94d840a3f933e148f6ef
[sfrench/cifs-2.6.git] / drivers / scsi / a3000.c
1 #include <linux/types.h>
2 #include <linux/mm.h>
3 #include <linux/blkdev.h>
4 #include <linux/sched.h>
5 #include <linux/ioport.h>
6 #include <linux/init.h>
7 #include <linux/spinlock.h>
8 #include <linux/interrupt.h>
9
10 #include <asm/setup.h>
11 #include <asm/page.h>
12 #include <asm/pgtable.h>
13 #include <asm/amigaints.h>
14 #include <asm/amigahw.h>
15 #include <asm/irq.h>
16
17 #include "scsi.h"
18 #include <scsi/scsi_host.h>
19 #include "wd33c93.h"
20 #include "a3000.h"
21
22 #include<linux/stat.h>
23
24 #define DMA(ptr) ((a3000_scsiregs *)((ptr)->base))
25 #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
26
27 static struct Scsi_Host *a3000_host = NULL;
28
29 static irqreturn_t a3000_intr (int irq, void *dummy, struct pt_regs *fp)
30 {
31         unsigned long flags;
32         unsigned int status = DMA(a3000_host)->ISTR;
33
34         if (!(status & ISTR_INT_P))
35                 return IRQ_NONE;
36         if (status & ISTR_INTS)
37         {
38                 spin_lock_irqsave(a3000_host->host_lock, flags);
39                 wd33c93_intr (a3000_host);
40                 spin_unlock_irqrestore(a3000_host->host_lock, flags);
41                 return IRQ_HANDLED;
42         }
43         printk("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
44         return IRQ_NONE;
45 }
46
47 static int dma_setup (Scsi_Cmnd *cmd, int dir_in)
48 {
49     unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
50     unsigned long addr = virt_to_bus(cmd->SCp.ptr);
51
52     /*
53      * if the physical address has the wrong alignment, or if
54      * physical address is bad, or if it is a write and at the
55      * end of a physical memory chunk, then allocate a bounce
56      * buffer
57      */
58     if (addr & A3000_XFER_MASK ||
59         (!dir_in && mm_end_of_chunk (addr, cmd->SCp.this_residual)))
60     {
61         HDATA(a3000_host)->dma_bounce_len = (cmd->SCp.this_residual + 511)
62             & ~0x1ff;
63         HDATA(a3000_host)->dma_bounce_buffer =
64             kmalloc (HDATA(a3000_host)->dma_bounce_len, GFP_KERNEL);
65         
66         /* can't allocate memory; use PIO */
67         if (!HDATA(a3000_host)->dma_bounce_buffer) {
68             HDATA(a3000_host)->dma_bounce_len = 0;
69             return 1;
70         }
71
72         if (!dir_in) {
73             /* copy to bounce buffer for a write */
74             if (cmd->use_sg) {
75                 memcpy (HDATA(a3000_host)->dma_bounce_buffer,
76                         cmd->SCp.ptr, cmd->SCp.this_residual);
77             } else
78                 memcpy (HDATA(a3000_host)->dma_bounce_buffer,
79                         cmd->request_buffer, cmd->request_bufflen);
80         }
81
82         addr = virt_to_bus(HDATA(a3000_host)->dma_bounce_buffer);
83     }
84
85     /* setup dma direction */
86     if (!dir_in)
87         cntr |= CNTR_DDIR;
88
89     /* remember direction */
90     HDATA(a3000_host)->dma_dir = dir_in;
91
92     DMA(a3000_host)->CNTR = cntr;
93
94     /* setup DMA *physical* address */
95     DMA(a3000_host)->ACR = addr;
96
97     if (dir_in)
98         /* invalidate any cache */
99         cache_clear (addr, cmd->SCp.this_residual);
100     else
101         /* push any dirty cache */
102         cache_push (addr, cmd->SCp.this_residual);
103
104     /* start DMA */
105     mb();                       /* make sure setup is completed */
106     DMA(a3000_host)->ST_DMA = 1;
107     mb();                       /* make sure DMA has started before next IO */
108
109     /* return success */
110     return 0;
111 }
112
113 static void dma_stop (struct Scsi_Host *instance, Scsi_Cmnd *SCpnt,
114                       int status)
115 {
116     /* disable SCSI interrupts */
117     unsigned short cntr = CNTR_PDMD;
118
119     if (!HDATA(instance)->dma_dir)
120         cntr |= CNTR_DDIR;
121
122     DMA(instance)->CNTR = cntr;
123     mb();                       /* make sure CNTR is updated before next IO */
124
125     /* flush if we were reading */
126     if (HDATA(instance)->dma_dir) {
127         DMA(instance)->FLUSH = 1;
128         mb();                   /* don't allow prefetch */
129         while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
130             barrier();
131         mb();                   /* no IO until FLUSH is done */
132     }
133
134     /* clear a possible interrupt */
135     /* I think that this CINT is only necessary if you are
136      * using the terminal count features.   HM 7 Mar 1994
137      */
138     DMA(instance)->CINT = 1;
139
140     /* stop DMA */
141     DMA(instance)->SP_DMA = 1;
142     mb();                       /* make sure DMA is stopped before next IO */
143
144     /* restore the CONTROL bits (minus the direction flag) */
145     DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
146     mb();                       /* make sure CNTR is updated before next IO */
147
148     /* copy from a bounce buffer, if necessary */
149     if (status && HDATA(instance)->dma_bounce_buffer) {
150         if (SCpnt && SCpnt->use_sg) {
151             if (HDATA(instance)->dma_dir && SCpnt)
152                 memcpy (SCpnt->SCp.ptr,
153                         HDATA(instance)->dma_bounce_buffer,
154                         SCpnt->SCp.this_residual);
155             kfree (HDATA(instance)->dma_bounce_buffer);
156             HDATA(instance)->dma_bounce_buffer = NULL;
157             HDATA(instance)->dma_bounce_len = 0;
158         } else {
159             if (HDATA(instance)->dma_dir && SCpnt)
160                 memcpy (SCpnt->request_buffer,
161                         HDATA(instance)->dma_bounce_buffer,
162                         SCpnt->request_bufflen);
163
164             kfree (HDATA(instance)->dma_bounce_buffer);
165             HDATA(instance)->dma_bounce_buffer = NULL;
166             HDATA(instance)->dma_bounce_len = 0;
167         }
168     }
169 }
170
171 int __init a3000_detect(Scsi_Host_Template *tpnt)
172 {
173     wd33c93_regs regs;
174
175     if  (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(A3000_SCSI))
176         return 0;
177     if (!request_mem_region(0xDD0000, 256, "wd33c93"))
178         return 0;
179
180     tpnt->proc_name = "A3000";
181     tpnt->proc_info = &wd33c93_proc_info;
182
183     a3000_host = scsi_register (tpnt, sizeof(struct WD33C93_hostdata));
184     if (a3000_host == NULL)
185         goto fail_register;
186
187     a3000_host->base = ZTWO_VADDR(0xDD0000);
188     a3000_host->irq = IRQ_AMIGA_PORTS;
189     DMA(a3000_host)->DAWR = DAWR_A3000;
190     regs.SASR = &(DMA(a3000_host)->SASR);
191     regs.SCMD = &(DMA(a3000_host)->SCMD);
192     wd33c93_init(a3000_host, regs, dma_setup, dma_stop, WD33C93_FS_12_15);
193     if (request_irq(IRQ_AMIGA_PORTS, a3000_intr, SA_SHIRQ, "A3000 SCSI",
194                     a3000_intr))
195         goto fail_irq;
196     DMA(a3000_host)->CNTR = CNTR_PDMD | CNTR_INTEN;
197
198     return 1;
199
200 fail_irq:
201     wd33c93_release();
202     scsi_unregister(a3000_host);
203 fail_register:
204     release_mem_region(0xDD0000, 256);
205     return 0;
206 }
207
208 static int a3000_bus_reset(Scsi_Cmnd *cmd)
209 {
210         /* FIXME perform bus-specific reset */
211         wd33c93_host_reset(cmd);
212         return SUCCESS;
213 }
214
215 #define HOSTS_C
216
217 static Scsi_Host_Template driver_template = {
218         .proc_name              = "A3000",
219         .name                   = "Amiga 3000 built-in SCSI",
220         .detect                 = a3000_detect,
221         .release                = a3000_release,
222         .queuecommand           = wd33c93_queuecommand,
223         .eh_abort_handler       = wd33c93_abort,
224         .eh_bus_reset_handler   = a3000_bus_reset,
225         .eh_host_reset_handler  = wd33c93_host_reset,
226         .can_queue              = CAN_QUEUE,
227         .this_id                = 7,
228         .sg_tablesize           = SG_ALL,
229         .cmd_per_lun            = CMD_PER_LUN,
230         .use_clustering         = ENABLE_CLUSTERING
231 };
232
233
234 #include "scsi_module.c"
235
236 int a3000_release(struct Scsi_Host *instance)
237 {
238     wd33c93_release();
239     DMA(instance)->CNTR = 0;
240     release_mem_region(0xDD0000, 256);
241     free_irq(IRQ_AMIGA_PORTS, a3000_intr);
242     return 1;
243 }
244
245 MODULE_LICENSE("GPL");