m68k: amiga - A2091/A590 SCSI zorro_driver conversion
[pandora-kernel.git] / drivers / scsi / a2091.c
1 #include <linux/types.h>
2 #include <linux/init.h>
3 #include <linux/interrupt.h>
4 #include <linux/mm.h>
5 #include <linux/slab.h>
6 #include <linux/spinlock.h>
7 #include <linux/zorro.h>
8
9 #include <asm/page.h>
10 #include <asm/pgtable.h>
11 #include <asm/amigaints.h>
12 #include <asm/amigahw.h>
13
14 #include "scsi.h"
15 #include "wd33c93.h"
16 #include "a2091.h"
17
18
19 static irqreturn_t a2091_intr(int irq, void *data)
20 {
21         struct Scsi_Host *instance = data;
22         struct a2091_scsiregs *regs = (struct a2091_scsiregs *)(instance->base);
23         unsigned int status = regs->ISTR;
24         unsigned long flags;
25
26         if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
27                 return IRQ_NONE;
28
29         spin_lock_irqsave(instance->host_lock, flags);
30         wd33c93_intr(instance);
31         spin_unlock_irqrestore(instance->host_lock, flags);
32         return IRQ_HANDLED;
33 }
34
35 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
36 {
37         struct Scsi_Host *instance = cmd->device->host;
38         struct WD33C93_hostdata *hdata = shost_priv(instance);
39         struct a2091_scsiregs *regs = (struct a2091_scsiregs *)(instance->base);
40         unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
41         unsigned long addr = virt_to_bus(cmd->SCp.ptr);
42
43         /* don't allow DMA if the physical address is bad */
44         if (addr & A2091_XFER_MASK) {
45                 hdata->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
46                 hdata->dma_bounce_buffer = kmalloc(hdata->dma_bounce_len,
47                                                    GFP_KERNEL);
48
49                 /* can't allocate memory; use PIO */
50                 if (!hdata->dma_bounce_buffer) {
51                         hdata->dma_bounce_len = 0;
52                         return 1;
53                 }
54
55                 /* get the physical address of the bounce buffer */
56                 addr = virt_to_bus(hdata->dma_bounce_buffer);
57
58                 /* the bounce buffer may not be in the first 16M of physmem */
59                 if (addr & A2091_XFER_MASK) {
60                         /* we could use chipmem... maybe later */
61                         kfree(hdata->dma_bounce_buffer);
62                         hdata->dma_bounce_buffer = NULL;
63                         hdata->dma_bounce_len = 0;
64                         return 1;
65                 }
66
67                 if (!dir_in) {
68                         /* copy to bounce buffer for a write */
69                         memcpy(hdata->dma_bounce_buffer, cmd->SCp.ptr,
70                                cmd->SCp.this_residual);
71                 }
72         }
73
74         /* setup dma direction */
75         if (!dir_in)
76                 cntr |= CNTR_DDIR;
77
78         /* remember direction */
79         hdata->dma_dir = dir_in;
80
81         regs->CNTR = cntr;
82
83         /* setup DMA *physical* address */
84         regs->ACR = addr;
85
86         if (dir_in) {
87                 /* invalidate any cache */
88                 cache_clear(addr, cmd->SCp.this_residual);
89         } else {
90                 /* push any dirty cache */
91                 cache_push(addr, cmd->SCp.this_residual);
92         }
93         /* start DMA */
94         regs->ST_DMA = 1;
95
96         /* return success */
97         return 0;
98 }
99
100 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
101                      int status)
102 {
103         struct WD33C93_hostdata *hdata = shost_priv(instance);
104         struct a2091_scsiregs *regs = (struct a2091_scsiregs *)(instance->base);
105
106         /* disable SCSI interrupts */
107         unsigned short cntr = CNTR_PDMD;
108
109         if (!hdata->dma_dir)
110                 cntr |= CNTR_DDIR;
111
112         /* disable SCSI interrupts */
113         regs->CNTR = cntr;
114
115         /* flush if we were reading */
116         if (hdata->dma_dir) {
117                 regs->FLUSH = 1;
118                 while (!(regs->ISTR & ISTR_FE_FLG))
119                         ;
120         }
121
122         /* clear a possible interrupt */
123         regs->CINT = 1;
124
125         /* stop DMA */
126         regs->SP_DMA = 1;
127
128         /* restore the CONTROL bits (minus the direction flag) */
129         regs->CNTR = CNTR_PDMD | CNTR_INTEN;
130
131         /* copy from a bounce buffer, if necessary */
132         if (status && hdata->dma_bounce_buffer) {
133                 if (hdata->dma_dir)
134                         memcpy(SCpnt->SCp.ptr, hdata->dma_bounce_buffer,
135                                SCpnt->SCp.this_residual);
136                 kfree(hdata->dma_bounce_buffer);
137                 hdata->dma_bounce_buffer = NULL;
138                 hdata->dma_bounce_len = 0;
139         }
140 }
141
142 static int a2091_bus_reset(struct scsi_cmnd *cmd)
143 {
144         struct Scsi_Host *instance = cmd->device->host;
145
146         /* FIXME perform bus-specific reset */
147
148         /* FIXME 2: kill this function, and let midlayer fall back
149            to the same action, calling wd33c93_host_reset() */
150
151         spin_lock_irq(instance->host_lock);
152         wd33c93_host_reset(cmd);
153         spin_unlock_irq(instance->host_lock);
154
155         return SUCCESS;
156 }
157
158 static struct scsi_host_template a2091_scsi_template = {
159         .module                 = THIS_MODULE,
160         .name                   = "Commodore A2091/A590 SCSI",
161         .proc_info              = wd33c93_proc_info,
162         .proc_name              = "A2901",
163         .queuecommand           = wd33c93_queuecommand,
164         .eh_abort_handler       = wd33c93_abort,
165         .eh_bus_reset_handler   = a2091_bus_reset,
166         .eh_host_reset_handler  = wd33c93_host_reset,
167         .can_queue              = CAN_QUEUE,
168         .this_id                = 7,
169         .sg_tablesize           = SG_ALL,
170         .cmd_per_lun            = CMD_PER_LUN,
171         .use_clustering         = DISABLE_CLUSTERING
172 };
173
174 static int __devinit a2091_probe(struct zorro_dev *z,
175                                  const struct zorro_device_id *ent)
176 {
177         struct Scsi_Host *instance;
178         int error;
179         struct a2091_scsiregs *regs;
180         wd33c93_regs wdregs;
181         struct WD33C93_hostdata *hdata;
182
183         if (!request_mem_region(z->resource.start, 256, "wd33c93"))
184                 return -EBUSY;
185
186         instance = scsi_host_alloc(&a2091_scsi_template,
187                                    sizeof(struct WD33C93_hostdata));
188         if (!instance) {
189                 error = -ENOMEM;
190                 goto fail_alloc;
191         }
192
193         instance->base = ZTWO_VADDR(z->resource.start);
194         instance->irq = IRQ_AMIGA_PORTS;
195         instance->unique_id = z->slotaddr;
196
197         regs = (struct a2091_scsiregs *)(instance->base);
198         regs->DAWR = DAWR_A2091;
199
200         wdregs.SASR = &regs->SASR;
201         wdregs.SCMD = &regs->SCMD;
202
203         hdata = shost_priv(instance);
204         hdata->no_sync = 0xff;
205         hdata->fast = 0;
206         hdata->dma_mode = CTRL_DMA;
207
208         wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10);
209         error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
210                             "A2091 SCSI", instance);
211         if (error)
212                 goto fail_irq;
213
214         regs->CNTR = CNTR_PDMD | CNTR_INTEN;
215
216         error = scsi_add_host(instance, NULL);
217         if (error)
218                 goto fail_host;
219
220         zorro_set_drvdata(z, instance);
221
222         scsi_scan_host(instance);
223         return 0;
224
225 fail_host:
226         free_irq(IRQ_AMIGA_PORTS, instance);
227 fail_irq:
228         scsi_host_put(instance);
229 fail_alloc:
230         release_mem_region(z->resource.start, 256);
231         return error;
232 }
233
234 static void __devexit a2091_remove(struct zorro_dev *z)
235 {
236         struct Scsi_Host *instance = zorro_get_drvdata(z);
237         struct a2091_scsiregs *regs = (struct a2091_scsiregs *)(instance->base);
238
239         regs->CNTR = 0;
240         scsi_remove_host(instance);
241         free_irq(IRQ_AMIGA_PORTS, instance);
242         scsi_host_put(instance);
243         release_mem_region(z->resource.start, 256);
244 }
245
246 static struct zorro_device_id a2091_zorro_tbl[] __devinitdata = {
247         { ZORRO_PROD_CBM_A590_A2091_1 },
248         { ZORRO_PROD_CBM_A590_A2091_2 },
249         { 0 }
250 };
251 MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl);
252
253 static struct zorro_driver a2091_driver = {
254         .name           = "a2091",
255         .id_table       = a2091_zorro_tbl,
256         .probe          = a2091_probe,
257         .remove         = __devexit_p(a2091_remove),
258 };
259
260 static int __init a2091_init(void)
261 {
262         return zorro_register_driver(&a2091_driver);
263 }
264 module_init(a2091_init);
265
266 static void __exit a2091_exit(void)
267 {
268         zorro_unregister_driver(&a2091_driver);
269 }
270 module_exit(a2091_exit);
271
272 MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
273 MODULE_LICENSE("GPL");