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