blob: 1bb5d3f0e260f1a94a74b15deb47301212784d19 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/init.h>
3#include <linux/interrupt.h>
Geert Uytterhoevenc737e222009-08-16 11:17:35 +02004#include <linux/mm.h>
5#include <linux/slab.h>
6#include <linux/spinlock.h>
7#include <linux/zorro.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/page.h>
10#include <asm/pgtable.h>
11#include <asm/amigaints.h>
12#include <asm/amigahw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include "scsi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "wd33c93.h"
16#include "a2091.h"
17
Adrian Bunk5880f482009-03-04 12:06:07 -080018
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020019struct a2091_hostdata {
20 struct WD33C93_hostdata wh;
21 struct a2091_scsiregs *regs;
22};
23
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020024static irqreturn_t a2091_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020026 struct Scsi_Host *instance = data;
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020027 struct a2091_hostdata *hdata = shost_priv(instance);
28 unsigned int status = hdata->regs->ISTR;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020029 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020031 if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
32 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020034 spin_lock_irqsave(instance->host_lock, flags);
35 wd33c93_intr(instance);
36 spin_unlock_irqrestore(instance->host_lock, flags);
37 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Henrik Kretzschmar65396412006-09-12 23:49:33 +020040static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020042 struct Scsi_Host *instance = cmd->device->host;
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020043 struct a2091_hostdata *hdata = shost_priv(instance);
44 struct WD33C93_hostdata *wh = &hdata->wh;
45 struct a2091_scsiregs *regs = hdata->regs;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020046 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
47 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020049 /* don't allow DMA if the physical address is bad */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (addr & A2091_XFER_MASK) {
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020051 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
52 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
53 GFP_KERNEL);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020054
55 /* can't allocate memory; use PIO */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020056 if (!wh->dma_bounce_buffer) {
57 wh->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020058 return 1;
59 }
60
61 /* get the physical address of the bounce buffer */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020062 addr = virt_to_bus(wh->dma_bounce_buffer);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020063
64 /* the bounce buffer may not be in the first 16M of physmem */
65 if (addr & A2091_XFER_MASK) {
66 /* we could use chipmem... maybe later */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020067 kfree(wh->dma_bounce_buffer);
68 wh->dma_bounce_buffer = NULL;
69 wh->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020070 return 1;
71 }
72
73 if (!dir_in) {
74 /* copy to bounce buffer for a write */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020075 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020076 cmd->SCp.this_residual);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020080 /* setup dma direction */
81 if (!dir_in)
82 cntr |= CNTR_DDIR;
83
84 /* remember direction */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020085 wh->dma_dir = dir_in;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020086
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020087 regs->CNTR = cntr;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020088
89 /* setup DMA *physical* address */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020090 regs->ACR = addr;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020091
92 if (dir_in) {
93 /* invalidate any cache */
94 cache_clear(addr, cmd->SCp.this_residual);
95 } else {
96 /* push any dirty cache */
97 cache_push(addr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020099 /* start DMA */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200100 regs->ST_DMA = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200102 /* return success */
103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200106static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200107 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200109 struct a2091_hostdata *hdata = shost_priv(instance);
110 struct WD33C93_hostdata *wh = &hdata->wh;
111 struct a2091_scsiregs *regs = hdata->regs;
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200112
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200113 /* disable SCSI interrupts */
114 unsigned short cntr = CNTR_PDMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200116 if (!wh->dma_dir)
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200117 cntr |= CNTR_DDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200119 /* disable SCSI interrupts */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200120 regs->CNTR = cntr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200122 /* flush if we were reading */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200123 if (wh->dma_dir) {
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200124 regs->FLUSH = 1;
125 while (!(regs->ISTR & ISTR_FE_FLG))
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200126 ;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200129 /* clear a possible interrupt */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200130 regs->CINT = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200132 /* stop DMA */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200133 regs->SP_DMA = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200135 /* restore the CONTROL bits (minus the direction flag) */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200136 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200138 /* copy from a bounce buffer, if necessary */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200139 if (status && wh->dma_bounce_buffer) {
140 if (wh->dma_dir)
141 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200142 SCpnt->SCp.this_residual);
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200143 kfree(wh->dma_bounce_buffer);
144 wh->dma_bounce_buffer = NULL;
145 wh->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200149static int a2091_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200151 struct Scsi_Host *instance = cmd->device->host;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* FIXME perform bus-specific reset */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400154
Jeff Garzik df0ae242005-05-28 07:57:14 -0400155 /* FIXME 2: kill this function, and let midlayer fall back
156 to the same action, calling wd33c93_host_reset() */
157
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200158 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 wd33c93_host_reset(cmd);
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200160 spin_unlock_irq(instance->host_lock);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return SUCCESS;
163}
164
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200165static struct scsi_host_template a2091_scsi_template = {
166 .module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .name = "Commodore A2091/A590 SCSI",
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200168 .proc_info = wd33c93_proc_info,
169 .proc_name = "A2901",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .queuecommand = wd33c93_queuecommand,
171 .eh_abort_handler = wd33c93_abort,
172 .eh_bus_reset_handler = a2091_bus_reset,
173 .eh_host_reset_handler = wd33c93_host_reset,
174 .can_queue = CAN_QUEUE,
175 .this_id = 7,
176 .sg_tablesize = SG_ALL,
177 .cmd_per_lun = CMD_PER_LUN,
178 .use_clustering = DISABLE_CLUSTERING
179};
180
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200181static int __devinit a2091_probe(struct zorro_dev *z,
182 const struct zorro_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200184 struct Scsi_Host *instance;
185 int error;
186 struct a2091_scsiregs *regs;
187 wd33c93_regs wdregs;
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200188 struct a2091_hostdata *hdata;
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200189
190 if (!request_mem_region(z->resource.start, 256, "wd33c93"))
191 return -EBUSY;
192
193 instance = scsi_host_alloc(&a2091_scsi_template,
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200194 sizeof(struct a2091_hostdata));
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200195 if (!instance) {
196 error = -ENOMEM;
197 goto fail_alloc;
198 }
199
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200200 instance->irq = IRQ_AMIGA_PORTS;
201 instance->unique_id = z->slotaddr;
202
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200203 regs = (struct a2091_scsiregs *)ZTWO_VADDR(z->resource.start);
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200204 regs->DAWR = DAWR_A2091;
205
206 wdregs.SASR = &regs->SASR;
207 wdregs.SCMD = &regs->SCMD;
208
209 hdata = shost_priv(instance);
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200210 hdata->wh.no_sync = 0xff;
211 hdata->wh.fast = 0;
212 hdata->wh.dma_mode = CTRL_DMA;
213 hdata->regs = regs;
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200214
215 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10);
216 error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
217 "A2091 SCSI", instance);
218 if (error)
219 goto fail_irq;
220
221 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
222
223 error = scsi_add_host(instance, NULL);
224 if (error)
225 goto fail_host;
226
227 zorro_set_drvdata(z, instance);
228
229 scsi_scan_host(instance);
230 return 0;
231
232fail_host:
233 free_irq(IRQ_AMIGA_PORTS, instance);
234fail_irq:
235 scsi_host_put(instance);
236fail_alloc:
237 release_mem_region(z->resource.start, 256);
238 return error;
239}
240
241static void __devexit a2091_remove(struct zorro_dev *z)
242{
243 struct Scsi_Host *instance = zorro_get_drvdata(z);
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200244 struct a2091_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200245
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200246 hdata->regs->CNTR = 0;
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200247 scsi_remove_host(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 free_irq(IRQ_AMIGA_PORTS, instance);
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200249 scsi_host_put(instance);
250 release_mem_region(z->resource.start, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200253static struct zorro_device_id a2091_zorro_tbl[] __devinitdata = {
254 { ZORRO_PROD_CBM_A590_A2091_1 },
255 { ZORRO_PROD_CBM_A590_A2091_2 },
256 { 0 }
257};
258MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl);
259
260static struct zorro_driver a2091_driver = {
261 .name = "a2091",
262 .id_table = a2091_zorro_tbl,
263 .probe = a2091_probe,
264 .remove = __devexit_p(a2091_remove),
265};
266
267static int __init a2091_init(void)
268{
269 return zorro_register_driver(&a2091_driver);
270}
271module_init(a2091_init);
272
273static void __exit a2091_exit(void)
274{
275 zorro_unregister_driver(&a2091_driver);
276}
277module_exit(a2091_exit);
278
279MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280MODULE_LICENSE("GPL");