Merge git://git.infradead.org/~dedekind/ubi-2.6
[linux-2.6] / drivers / scsi / a2091.c
1 #include <linux/types.h>
2 #include <linux/mm.h>
3 #include <linux/blkdev.h>
4 #include <linux/init.h>
5 #include <linux/interrupt.h>
6
7 #include <asm/setup.h>
8 #include <asm/page.h>
9 #include <asm/pgtable.h>
10 #include <asm/amigaints.h>
11 #include <asm/amigahw.h>
12 #include <linux/zorro.h>
13 #include <asm/irq.h>
14 #include <linux/spinlock.h>
15
16 #include "scsi.h"
17 #include <scsi/scsi_host.h>
18 #include "wd33c93.h"
19 #include "a2091.h"
20
21 #include<linux/stat.h>
22
23 #define DMA(ptr) ((a2091_scsiregs *)((ptr)->base))
24 #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
25
26 static irqreturn_t a2091_intr (int irq, void *_instance)
27 {
28     unsigned long flags;
29     unsigned int status;
30     struct Scsi_Host *instance = (struct Scsi_Host *)_instance;
31
32     status = DMA(instance)->ISTR;
33     if (!(status & (ISTR_INT_F|ISTR_INT_P)) || !(status & ISTR_INTS))
34         return IRQ_NONE;
35
36     spin_lock_irqsave(instance->host_lock, flags);
37     wd33c93_intr(instance);
38     spin_unlock_irqrestore(instance->host_lock, flags);
39     return IRQ_HANDLED;
40 }
41
42 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
43 {
44     unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
45     unsigned long addr = virt_to_bus(cmd->SCp.ptr);
46     struct Scsi_Host *instance = cmd->device->host;
47
48     /* don't allow DMA if the physical address is bad */
49     if (addr & A2091_XFER_MASK ||
50         (!dir_in && mm_end_of_chunk (addr, cmd->SCp.this_residual)))
51     {
52         HDATA(instance)->dma_bounce_len = (cmd->SCp.this_residual + 511)
53             & ~0x1ff;
54         HDATA(instance)->dma_bounce_buffer =
55             kmalloc (HDATA(instance)->dma_bounce_len, GFP_KERNEL);
56         
57         /* can't allocate memory; use PIO */
58         if (!HDATA(instance)->dma_bounce_buffer) {
59             HDATA(instance)->dma_bounce_len = 0;
60             return 1;
61         }
62
63         /* get the physical address of the bounce buffer */
64         addr = virt_to_bus(HDATA(instance)->dma_bounce_buffer);
65
66         /* the bounce buffer may not be in the first 16M of physmem */
67         if (addr & A2091_XFER_MASK) {
68             /* we could use chipmem... maybe later */
69             kfree (HDATA(instance)->dma_bounce_buffer);
70             HDATA(instance)->dma_bounce_buffer = NULL;
71             HDATA(instance)->dma_bounce_len = 0;
72             return 1;
73         }
74
75         if (!dir_in) {
76                 /* copy to bounce buffer for a write */
77                 memcpy (HDATA(instance)->dma_bounce_buffer,
78                         cmd->SCp.ptr, cmd->SCp.this_residual);
79         }
80     }
81
82     /* setup dma direction */
83     if (!dir_in)
84         cntr |= CNTR_DDIR;
85
86     /* remember direction */
87     HDATA(cmd->device->host)->dma_dir = dir_in;
88
89     DMA(cmd->device->host)->CNTR = cntr;
90
91     /* setup DMA *physical* address */
92     DMA(cmd->device->host)->ACR = addr;
93
94     if (dir_in){
95         /* invalidate any cache */
96         cache_clear (addr, cmd->SCp.this_residual);
97     }else{
98         /* push any dirty cache */
99         cache_push (addr, cmd->SCp.this_residual);
100       }
101     /* start DMA */
102     DMA(cmd->device->host)->ST_DMA = 1;
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     /* disable SCSI interrupts */
118     DMA(instance)->CNTR = cntr;
119
120     /* flush if we were reading */
121     if (HDATA(instance)->dma_dir) {
122         DMA(instance)->FLUSH = 1;
123         while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
124             ;
125     }
126
127     /* clear a possible interrupt */
128     DMA(instance)->CINT = 1;
129
130     /* stop DMA */
131     DMA(instance)->SP_DMA = 1;
132
133     /* restore the CONTROL bits (minus the direction flag) */
134     DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
135
136     /* copy from a bounce buffer, if necessary */
137     if (status && HDATA(instance)->dma_bounce_buffer) {
138         if( HDATA(instance)->dma_dir )
139                 memcpy (SCpnt->SCp.ptr, 
140                         HDATA(instance)->dma_bounce_buffer,
141                         SCpnt->SCp.this_residual);
142         kfree (HDATA(instance)->dma_bounce_buffer);
143         HDATA(instance)->dma_bounce_buffer = NULL;
144         HDATA(instance)->dma_bounce_len = 0;
145     }
146 }
147
148 int __init a2091_detect(struct scsi_host_template *tpnt)
149 {
150     static unsigned char called = 0;
151     struct Scsi_Host *instance;
152     unsigned long address;
153     struct zorro_dev *z = NULL;
154     wd33c93_regs regs;
155     int num_a2091 = 0;
156
157     if (!MACH_IS_AMIGA || called)
158         return 0;
159     called = 1;
160
161     tpnt->proc_name = "A2091";
162     tpnt->proc_info = &wd33c93_proc_info;
163
164     while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
165         if (z->id != ZORRO_PROD_CBM_A590_A2091_1 &&
166             z->id != ZORRO_PROD_CBM_A590_A2091_2)
167             continue;
168         address = z->resource.start;
169         if (!request_mem_region(address, 256, "wd33c93"))
170             continue;
171
172         instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
173         if (instance == NULL) {
174             release_mem_region(address, 256);
175             continue;
176         }
177         instance->base = ZTWO_VADDR(address);
178         instance->irq = IRQ_AMIGA_PORTS;
179         instance->unique_id = z->slotaddr;
180         DMA(instance)->DAWR = DAWR_A2091;
181         regs.SASR = &(DMA(instance)->SASR);
182         regs.SCMD = &(DMA(instance)->SCMD);
183         wd33c93_init(instance, regs, dma_setup, dma_stop, WD33C93_FS_8_10);
184         request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED, "A2091 SCSI",
185                     instance);
186         DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
187         num_a2091++;
188     }
189
190     return num_a2091;
191 }
192
193 static int a2091_bus_reset(struct scsi_cmnd *cmd)
194 {
195         /* FIXME perform bus-specific reset */
196
197         /* FIXME 2: kill this function, and let midlayer fall back
198            to the same action, calling wd33c93_host_reset() */
199
200         spin_lock_irq(cmd->device->host->host_lock);
201         wd33c93_host_reset(cmd);
202         spin_unlock_irq(cmd->device->host->host_lock);
203
204         return SUCCESS;
205 }
206
207 #define HOSTS_C
208
209 static struct scsi_host_template driver_template = {
210         .proc_name              = "A2901",
211         .name                   = "Commodore A2091/A590 SCSI",
212         .detect                 = a2091_detect,
213         .release                = a2091_release,
214         .queuecommand           = wd33c93_queuecommand,
215         .eh_abort_handler       = wd33c93_abort,
216         .eh_bus_reset_handler   = a2091_bus_reset,
217         .eh_host_reset_handler  = wd33c93_host_reset,
218         .can_queue              = CAN_QUEUE,
219         .this_id                = 7,
220         .sg_tablesize           = SG_ALL,
221         .cmd_per_lun            = CMD_PER_LUN,
222         .use_clustering         = DISABLE_CLUSTERING
223 };
224
225
226 #include "scsi_module.c"
227
228 int a2091_release(struct Scsi_Host *instance)
229 {
230 #ifdef MODULE
231         DMA(instance)->CNTR = 0;
232         release_mem_region(ZTWO_PADDR(instance->base), 256);
233         free_irq(IRQ_AMIGA_PORTS, instance);
234         wd33c93_release();
235 #endif
236         return 1;
237 }
238
239 MODULE_LICENSE("GPL");