Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6 into for-linus
[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     {
51         HDATA(instance)->dma_bounce_len = (cmd->SCp.this_residual + 511)
52             & ~0x1ff;
53         HDATA(instance)->dma_bounce_buffer =
54             kmalloc (HDATA(instance)->dma_bounce_len, GFP_KERNEL);
55         
56         /* can't allocate memory; use PIO */
57         if (!HDATA(instance)->dma_bounce_buffer) {
58             HDATA(instance)->dma_bounce_len = 0;
59             return 1;
60         }
61
62         /* get the physical address of the bounce buffer */
63         addr = virt_to_bus(HDATA(instance)->dma_bounce_buffer);
64
65         /* the bounce buffer may not be in the first 16M of physmem */
66         if (addr & A2091_XFER_MASK) {
67             /* we could use chipmem... maybe later */
68             kfree (HDATA(instance)->dma_bounce_buffer);
69             HDATA(instance)->dma_bounce_buffer = NULL;
70             HDATA(instance)->dma_bounce_len = 0;
71             return 1;
72         }
73
74         if (!dir_in) {
75                 /* copy to bounce buffer for a write */
76                 memcpy (HDATA(instance)->dma_bounce_buffer,
77                         cmd->SCp.ptr, cmd->SCp.this_residual);
78         }
79     }
80
81     /* setup dma direction */
82     if (!dir_in)
83         cntr |= CNTR_DDIR;
84
85     /* remember direction */
86     HDATA(cmd->device->host)->dma_dir = dir_in;
87
88     DMA(cmd->device->host)->CNTR = cntr;
89
90     /* setup DMA *physical* address */
91     DMA(cmd->device->host)->ACR = addr;
92
93     if (dir_in){
94         /* invalidate any cache */
95         cache_clear (addr, cmd->SCp.this_residual);
96     }else{
97         /* push any dirty cache */
98         cache_push (addr, cmd->SCp.this_residual);
99       }
100     /* start DMA */
101     DMA(cmd->device->host)->ST_DMA = 1;
102
103     /* return success */
104     return 0;
105 }
106
107 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
108                       int status)
109 {
110     /* disable SCSI interrupts */
111     unsigned short cntr = CNTR_PDMD;
112
113     if (!HDATA(instance)->dma_dir)
114             cntr |= CNTR_DDIR;
115
116     /* disable SCSI interrupts */
117     DMA(instance)->CNTR = cntr;
118
119     /* flush if we were reading */
120     if (HDATA(instance)->dma_dir) {
121         DMA(instance)->FLUSH = 1;
122         while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
123             ;
124     }
125
126     /* clear a possible interrupt */
127     DMA(instance)->CINT = 1;
128
129     /* stop DMA */
130     DMA(instance)->SP_DMA = 1;
131
132     /* restore the CONTROL bits (minus the direction flag) */
133     DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
134
135     /* copy from a bounce buffer, if necessary */
136     if (status && HDATA(instance)->dma_bounce_buffer) {
137         if( HDATA(instance)->dma_dir )
138                 memcpy (SCpnt->SCp.ptr, 
139                         HDATA(instance)->dma_bounce_buffer,
140                         SCpnt->SCp.this_residual);
141         kfree (HDATA(instance)->dma_bounce_buffer);
142         HDATA(instance)->dma_bounce_buffer = NULL;
143         HDATA(instance)->dma_bounce_len = 0;
144     }
145 }
146
147 int __init a2091_detect(struct scsi_host_template *tpnt)
148 {
149     static unsigned char called = 0;
150     struct Scsi_Host *instance;
151     unsigned long address;
152     struct zorro_dev *z = NULL;
153     wd33c93_regs regs;
154     int num_a2091 = 0;
155
156     if (!MACH_IS_AMIGA || called)
157         return 0;
158     called = 1;
159
160     tpnt->proc_name = "A2091";
161     tpnt->proc_info = &wd33c93_proc_info;
162
163     while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
164         if (z->id != ZORRO_PROD_CBM_A590_A2091_1 &&
165             z->id != ZORRO_PROD_CBM_A590_A2091_2)
166             continue;
167         address = z->resource.start;
168         if (!request_mem_region(address, 256, "wd33c93"))
169             continue;
170
171         instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
172         if (instance == NULL) {
173             release_mem_region(address, 256);
174             continue;
175         }
176         instance->base = ZTWO_VADDR(address);
177         instance->irq = IRQ_AMIGA_PORTS;
178         instance->unique_id = z->slotaddr;
179         DMA(instance)->DAWR = DAWR_A2091;
180         regs.SASR = &(DMA(instance)->SASR);
181         regs.SCMD = &(DMA(instance)->SCMD);
182         wd33c93_init(instance, regs, dma_setup, dma_stop, WD33C93_FS_8_10);
183         request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED, "A2091 SCSI",
184                     instance);
185         DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
186         num_a2091++;
187     }
188
189     return num_a2091;
190 }
191
192 static int a2091_bus_reset(struct scsi_cmnd *cmd)
193 {
194         /* FIXME perform bus-specific reset */
195
196         /* FIXME 2: kill this function, and let midlayer fall back
197            to the same action, calling wd33c93_host_reset() */
198
199         spin_lock_irq(cmd->device->host->host_lock);
200         wd33c93_host_reset(cmd);
201         spin_unlock_irq(cmd->device->host->host_lock);
202
203         return SUCCESS;
204 }
205
206 #define HOSTS_C
207
208 static struct scsi_host_template driver_template = {
209         .proc_name              = "A2901",
210         .name                   = "Commodore A2091/A590 SCSI",
211         .detect                 = a2091_detect,
212         .release                = a2091_release,
213         .queuecommand           = wd33c93_queuecommand,
214         .eh_abort_handler       = wd33c93_abort,
215         .eh_bus_reset_handler   = a2091_bus_reset,
216         .eh_host_reset_handler  = wd33c93_host_reset,
217         .can_queue              = CAN_QUEUE,
218         .this_id                = 7,
219         .sg_tablesize           = SG_ALL,
220         .cmd_per_lun            = CMD_PER_LUN,
221         .use_clustering         = DISABLE_CLUSTERING
222 };
223
224
225 #include "scsi_module.c"
226
227 int a2091_release(struct Scsi_Host *instance)
228 {
229 #ifdef MODULE
230         DMA(instance)->CNTR = 0;
231         release_mem_region(ZTWO_PADDR(instance->base), 256);
232         free_irq(IRQ_AMIGA_PORTS, instance);
233         wd33c93_release();
234 #endif
235         return 1;
236 }
237
238 MODULE_LICENSE("GPL");