]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/scsi/a3000.c
m68k: amiga - A3000 SCSI platform device conversion
[net-next-2.6.git] / drivers / scsi / a3000.c
1 #include <linux/types.h>
2 #include <linux/mm.h>
3 #include <linux/ioport.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
6 #include <linux/spinlock.h>
7 #include <linux/interrupt.h>
8 #include <linux/platform_device.h>
9
10 #include <asm/page.h>
11 #include <asm/pgtable.h>
12 #include <asm/amigaints.h>
13 #include <asm/amigahw.h>
14
15 #include "scsi.h"
16 #include "wd33c93.h"
17 #include "a3000.h"
18
19
20 static irqreturn_t a3000_intr(int irq, void *data)
21 {
22         struct Scsi_Host *instance = data;
23         struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
24         unsigned int status = regs->ISTR;
25         unsigned long flags;
26
27         if (!(status & ISTR_INT_P))
28                 return IRQ_NONE;
29         if (status & ISTR_INTS) {
30                 spin_lock_irqsave(instance->host_lock, flags);
31                 wd33c93_intr(instance);
32                 spin_unlock_irqrestore(instance->host_lock, flags);
33                 return IRQ_HANDLED;
34         }
35         pr_warning("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
36         return IRQ_NONE;
37 }
38
39 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
40 {
41         struct Scsi_Host *instance = cmd->device->host;
42         struct WD33C93_hostdata *hdata = shost_priv(instance);
43         struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
44         unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
45         unsigned long addr = virt_to_bus(cmd->SCp.ptr);
46
47         /*
48          * if the physical address has the wrong alignment, or if
49          * physical address is bad, or if it is a write and at the
50          * end of a physical memory chunk, then allocate a bounce
51          * buffer
52          */
53         if (addr & A3000_XFER_MASK) {
54                 hdata->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
55                 hdata->dma_bounce_buffer = kmalloc(hdata->dma_bounce_len,
56                                                    GFP_KERNEL);
57
58                 /* can't allocate memory; use PIO */
59                 if (!hdata->dma_bounce_buffer) {
60                         hdata->dma_bounce_len = 0;
61                         return 1;
62                 }
63
64                 if (!dir_in) {
65                         /* copy to bounce buffer for a write */
66                         memcpy(hdata->dma_bounce_buffer, cmd->SCp.ptr,
67                                cmd->SCp.this_residual);
68                 }
69
70                 addr = virt_to_bus(hdata->dma_bounce_buffer);
71         }
72
73         /* setup dma direction */
74         if (!dir_in)
75                 cntr |= CNTR_DDIR;
76
77         /* remember direction */
78         hdata->dma_dir = dir_in;
79
80         regs->CNTR = cntr;
81
82         /* setup DMA *physical* address */
83         regs->ACR = addr;
84
85         if (dir_in) {
86                 /* invalidate any cache */
87                 cache_clear(addr, cmd->SCp.this_residual);
88         } else {
89                 /* push any dirty cache */
90                 cache_push(addr, cmd->SCp.this_residual);
91         }
92
93         /* start DMA */
94         mb();                   /* make sure setup is completed */
95         regs->ST_DMA = 1;
96         mb();                   /* make sure DMA has started before next IO */
97
98         /* return success */
99         return 0;
100 }
101
102 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
103                      int status)
104 {
105         struct WD33C93_hostdata *hdata = shost_priv(instance);
106         struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
107
108         /* disable SCSI interrupts */
109         unsigned short cntr = CNTR_PDMD;
110
111         if (!hdata->dma_dir)
112                 cntr |= CNTR_DDIR;
113
114         regs->CNTR = cntr;
115         mb();                   /* make sure CNTR is updated before next IO */
116
117         /* flush if we were reading */
118         if (hdata->dma_dir) {
119                 regs->FLUSH = 1;
120                 mb();           /* don't allow prefetch */
121                 while (!(regs->ISTR & ISTR_FE_FLG))
122                         barrier();
123                 mb();           /* no IO until FLUSH is done */
124         }
125
126         /* clear a possible interrupt */
127         /* I think that this CINT is only necessary if you are
128          * using the terminal count features.   HM 7 Mar 1994
129          */
130         regs->CINT = 1;
131
132         /* stop DMA */
133         regs->SP_DMA = 1;
134         mb();                   /* make sure DMA is stopped before next IO */
135
136         /* restore the CONTROL bits (minus the direction flag) */
137         regs->CNTR = CNTR_PDMD | CNTR_INTEN;
138         mb();                   /* make sure CNTR is updated before next IO */
139
140         /* copy from a bounce buffer, if necessary */
141         if (status && hdata->dma_bounce_buffer) {
142                 if (SCpnt) {
143                         if (hdata->dma_dir && SCpnt)
144                                 memcpy(SCpnt->SCp.ptr,
145                                        hdata->dma_bounce_buffer,
146                                        SCpnt->SCp.this_residual);
147                         kfree(hdata->dma_bounce_buffer);
148                         hdata->dma_bounce_buffer = NULL;
149                         hdata->dma_bounce_len = 0;
150                 } else {
151                         kfree(hdata->dma_bounce_buffer);
152                         hdata->dma_bounce_buffer = NULL;
153                         hdata->dma_bounce_len = 0;
154                 }
155         }
156 }
157
158 static int a3000_bus_reset(struct scsi_cmnd *cmd)
159 {
160         struct Scsi_Host *instance = cmd->device->host;
161
162         /* FIXME perform bus-specific reset */
163
164         /* FIXME 2: kill this entire function, which should
165            cause mid-layer to call wd33c93_host_reset anyway? */
166
167         spin_lock_irq(instance->host_lock);
168         wd33c93_host_reset(cmd);
169         spin_unlock_irq(instance->host_lock);
170
171         return SUCCESS;
172 }
173
174 static struct scsi_host_template amiga_a3000_scsi_template = {
175         .module                 = THIS_MODULE,
176         .name                   = "Amiga 3000 built-in SCSI",
177         .proc_info              = wd33c93_proc_info,
178         .proc_name              = "A3000",
179         .queuecommand           = wd33c93_queuecommand,
180         .eh_abort_handler       = wd33c93_abort,
181         .eh_bus_reset_handler   = a3000_bus_reset,
182         .eh_host_reset_handler  = wd33c93_host_reset,
183         .can_queue              = CAN_QUEUE,
184         .this_id                = 7,
185         .sg_tablesize           = SG_ALL,
186         .cmd_per_lun            = CMD_PER_LUN,
187         .use_clustering         = ENABLE_CLUSTERING
188 };
189
190 static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
191 {
192         struct resource *res;
193         struct Scsi_Host *instance;
194         int error;
195         struct a3000_scsiregs *regs;
196         wd33c93_regs wdregs;
197         struct WD33C93_hostdata *hdata;
198
199         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200         if (!res)
201                 return -ENODEV;
202
203         if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
204                 return -EBUSY;
205
206         instance = scsi_host_alloc(&amiga_a3000_scsi_template,
207                                    sizeof(struct WD33C93_hostdata));
208         if (!instance) {
209                 error = -ENOMEM;
210                 goto fail_alloc;
211         }
212
213         instance->base = ZTWO_VADDR(res->start);
214         instance->irq = IRQ_AMIGA_PORTS;
215
216         regs = (struct a3000_scsiregs *)(instance->base);
217         regs->DAWR = DAWR_A3000;
218
219         wdregs.SASR = &regs->SASR;
220         wdregs.SCMD = &regs->SCMD;
221
222         hdata = shost_priv(instance);
223         hdata->no_sync = 0xff;
224         hdata->fast = 0;
225         hdata->dma_mode = CTRL_DMA;
226
227         wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
228         error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
229                             "A3000 SCSI", instance);
230         if (error)
231                 goto fail_irq;
232
233         regs->CNTR = CNTR_PDMD | CNTR_INTEN;
234
235         error = scsi_add_host(instance, NULL);
236         if (error)
237                 goto fail_host;
238
239         platform_set_drvdata(pdev, instance);
240
241         scsi_scan_host(instance);
242         return 0;
243
244 fail_host:
245         free_irq(IRQ_AMIGA_PORTS, instance);
246 fail_irq:
247         scsi_host_put(instance);
248 fail_alloc:
249         release_mem_region(res->start, resource_size(res));
250         return error;
251 }
252
253 static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
254 {
255         struct Scsi_Host *instance = platform_get_drvdata(pdev);
256         struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
257         struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258
259         regs->CNTR = 0;
260         scsi_remove_host(instance);
261         free_irq(IRQ_AMIGA_PORTS, instance);
262         scsi_host_put(instance);
263         release_mem_region(res->start, resource_size(res));
264         return 0;
265 }
266
267 static struct platform_driver amiga_a3000_scsi_driver = {
268         .remove = __exit_p(amiga_a3000_scsi_remove),
269         .driver   = {
270                 .name   = "amiga-a3000-scsi",
271                 .owner  = THIS_MODULE,
272         },
273 };
274
275 static int __init amiga_a3000_scsi_init(void)
276 {
277         return platform_driver_probe(&amiga_a3000_scsi_driver,
278                                      amiga_a3000_scsi_probe);
279 }
280 module_init(amiga_a3000_scsi_init);
281
282 static void __exit amiga_a3000_scsi_exit(void)
283 {
284         platform_driver_unregister(&amiga_a3000_scsi_driver);
285 }
286 module_exit(amiga_a3000_scsi_exit);
287
288 MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
289 MODULE_LICENSE("GPL");
290 MODULE_ALIAS("platform:amiga-a3000-scsi");