RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / a3000.c
blob77c4513f64fb09be312d385c36d08b61e4b6f56f
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>
10 #include <asm/page.h>
11 #include <asm/pgtable.h>
12 #include <asm/amigaints.h>
13 #include <asm/amigahw.h>
15 #include "scsi.h"
16 #include "wd33c93.h"
17 #include "a3000.h"
20 struct a3000_hostdata {
21 struct WD33C93_hostdata wh;
22 struct a3000_scsiregs *regs;
25 static irqreturn_t a3000_intr(int irq, void *data)
27 struct Scsi_Host *instance = data;
28 struct a3000_hostdata *hdata = shost_priv(instance);
29 unsigned int status = hdata->regs->ISTR;
30 unsigned long flags;
32 if (!(status & ISTR_INT_P))
33 return IRQ_NONE;
34 if (status & ISTR_INTS) {
35 spin_lock_irqsave(instance->host_lock, flags);
36 wd33c93_intr(instance);
37 spin_unlock_irqrestore(instance->host_lock, flags);
38 return IRQ_HANDLED;
40 pr_warning("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
41 return IRQ_NONE;
44 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
46 struct Scsi_Host *instance = cmd->device->host;
47 struct a3000_hostdata *hdata = shost_priv(instance);
48 struct WD33C93_hostdata *wh = &hdata->wh;
49 struct a3000_scsiregs *regs = hdata->regs;
50 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
51 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
54 * if the physical address has the wrong alignment, or if
55 * physical address is bad, or if it is a write and at the
56 * end of a physical memory chunk, then allocate a bounce
57 * buffer
59 if (addr & A3000_XFER_MASK) {
60 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
61 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
62 GFP_KERNEL);
64 /* can't allocate memory; use PIO */
65 if (!wh->dma_bounce_buffer) {
66 wh->dma_bounce_len = 0;
67 return 1;
70 if (!dir_in) {
71 /* copy to bounce buffer for a write */
72 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
73 cmd->SCp.this_residual);
76 addr = virt_to_bus(wh->dma_bounce_buffer);
79 /* setup dma direction */
80 if (!dir_in)
81 cntr |= CNTR_DDIR;
83 /* remember direction */
84 wh->dma_dir = dir_in;
86 regs->CNTR = cntr;
88 /* setup DMA *physical* address */
89 regs->ACR = addr;
91 if (dir_in) {
92 /* invalidate any cache */
93 cache_clear(addr, cmd->SCp.this_residual);
94 } else {
95 /* push any dirty cache */
96 cache_push(addr, cmd->SCp.this_residual);
99 /* start DMA */
100 mb(); /* make sure setup is completed */
101 regs->ST_DMA = 1;
102 mb(); /* make sure DMA has started before next IO */
104 /* return success */
105 return 0;
108 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
109 int status)
111 struct a3000_hostdata *hdata = shost_priv(instance);
112 struct WD33C93_hostdata *wh = &hdata->wh;
113 struct a3000_scsiregs *regs = hdata->regs;
115 /* disable SCSI interrupts */
116 unsigned short cntr = CNTR_PDMD;
118 if (!wh->dma_dir)
119 cntr |= CNTR_DDIR;
121 regs->CNTR = cntr;
122 mb(); /* make sure CNTR is updated before next IO */
124 /* flush if we were reading */
125 if (wh->dma_dir) {
126 regs->FLUSH = 1;
127 mb(); /* don't allow prefetch */
128 while (!(regs->ISTR & ISTR_FE_FLG))
129 barrier();
130 mb(); /* no IO until FLUSH is done */
133 /* clear a possible interrupt */
134 /* I think that this CINT is only necessary if you are
135 * using the terminal count features. HM 7 Mar 1994
137 regs->CINT = 1;
139 /* stop DMA */
140 regs->SP_DMA = 1;
141 mb(); /* make sure DMA is stopped before next IO */
143 /* restore the CONTROL bits (minus the direction flag) */
144 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
145 mb(); /* make sure CNTR is updated before next IO */
147 /* copy from a bounce buffer, if necessary */
148 if (status && wh->dma_bounce_buffer) {
149 if (SCpnt) {
150 if (wh->dma_dir && SCpnt)
151 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
152 SCpnt->SCp.this_residual);
153 kfree(wh->dma_bounce_buffer);
154 wh->dma_bounce_buffer = NULL;
155 wh->dma_bounce_len = 0;
156 } else {
157 kfree(wh->dma_bounce_buffer);
158 wh->dma_bounce_buffer = NULL;
159 wh->dma_bounce_len = 0;
164 static int a3000_bus_reset(struct scsi_cmnd *cmd)
166 struct Scsi_Host *instance = cmd->device->host;
170 spin_lock_irq(instance->host_lock);
171 wd33c93_host_reset(cmd);
172 spin_unlock_irq(instance->host_lock);
174 return SUCCESS;
177 static struct scsi_host_template amiga_a3000_scsi_template = {
178 .module = THIS_MODULE,
179 .name = "Amiga 3000 built-in SCSI",
180 .proc_info = wd33c93_proc_info,
181 .proc_name = "A3000",
182 .queuecommand = wd33c93_queuecommand,
183 .eh_abort_handler = wd33c93_abort,
184 .eh_bus_reset_handler = a3000_bus_reset,
185 .eh_host_reset_handler = wd33c93_host_reset,
186 .can_queue = CAN_QUEUE,
187 .this_id = 7,
188 .sg_tablesize = SG_ALL,
189 .cmd_per_lun = CMD_PER_LUN,
190 .use_clustering = ENABLE_CLUSTERING
193 static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
195 struct resource *res;
196 struct Scsi_Host *instance;
197 int error;
198 struct a3000_scsiregs *regs;
199 wd33c93_regs wdregs;
200 struct a3000_hostdata *hdata;
202 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203 if (!res)
204 return -ENODEV;
206 if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
207 return -EBUSY;
209 instance = scsi_host_alloc(&amiga_a3000_scsi_template,
210 sizeof(struct a3000_hostdata));
211 if (!instance) {
212 error = -ENOMEM;
213 goto fail_alloc;
216 instance->irq = IRQ_AMIGA_PORTS;
218 regs = (struct a3000_scsiregs *)ZTWO_VADDR(res->start);
219 regs->DAWR = DAWR_A3000;
221 wdregs.SASR = &regs->SASR;
222 wdregs.SCMD = &regs->SCMD;
224 hdata = shost_priv(instance);
225 hdata->wh.no_sync = 0xff;
226 hdata->wh.fast = 0;
227 hdata->wh.dma_mode = CTRL_DMA;
228 hdata->regs = regs;
230 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
231 error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
232 "A3000 SCSI", instance);
233 if (error)
234 goto fail_irq;
236 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
238 error = scsi_add_host(instance, NULL);
239 if (error)
240 goto fail_host;
242 platform_set_drvdata(pdev, instance);
244 scsi_scan_host(instance);
245 return 0;
247 fail_host:
248 free_irq(IRQ_AMIGA_PORTS, instance);
249 fail_irq:
250 scsi_host_put(instance);
251 fail_alloc:
252 release_mem_region(res->start, resource_size(res));
253 return error;
256 static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
258 struct Scsi_Host *instance = platform_get_drvdata(pdev);
259 struct a3000_hostdata *hdata = shost_priv(instance);
260 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
262 hdata->regs->CNTR = 0;
263 scsi_remove_host(instance);
264 free_irq(IRQ_AMIGA_PORTS, instance);
265 scsi_host_put(instance);
266 release_mem_region(res->start, resource_size(res));
267 return 0;
270 static struct platform_driver amiga_a3000_scsi_driver = {
271 .remove = __exit_p(amiga_a3000_scsi_remove),
272 .driver = {
273 .name = "amiga-a3000-scsi",
274 .owner = THIS_MODULE,
278 static int __init amiga_a3000_scsi_init(void)
280 return platform_driver_probe(&amiga_a3000_scsi_driver,
281 amiga_a3000_scsi_probe);
283 module_init(amiga_a3000_scsi_init);
285 static void __exit amiga_a3000_scsi_exit(void)
287 platform_driver_unregister(&amiga_a3000_scsi_driver);
289 module_exit(amiga_a3000_scsi_exit);
291 MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
292 MODULE_LICENSE("GPL");
293 MODULE_ALIAS("platform:amiga-a3000-scsi");