blob: 308541ff85cf9ff127a118696bc2b62d0d6d255e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/init.h>
6#include <linux/interrupt.h>
7
8#include <asm/setup.h>
9#include <asm/page.h>
10#include <asm/pgtable.h>
11#include <asm/amigaints.h>
12#include <asm/amigahw.h>
13#include <linux/zorro.h>
14#include <asm/irq.h>
15#include <linux/spinlock.h>
16
17#include "scsi.h"
18#include <scsi/scsi_host.h>
19#include "wd33c93.h"
20#include "a2091.h"
21
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020022#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020024
Adrian Bunk5880f482009-03-04 12:06:07 -080025static int a2091_release(struct Scsi_Host *instance);
26
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020027static irqreturn_t a2091_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020029 struct Scsi_Host *instance = data;
30 a2091_scsiregs *regs = (a2091_scsiregs *)(instance->base);
31 unsigned int status = regs->ISTR;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020032 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020034 if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
35 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020037 spin_lock_irqsave(instance->host_lock, flags);
38 wd33c93_intr(instance);
39 spin_unlock_irqrestore(instance->host_lock, flags);
40 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Henrik Kretzschmar65396412006-09-12 23:49:33 +020043static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020045 struct Scsi_Host *instance = cmd->device->host;
46 struct WD33C93_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020047 a2091_scsiregs *regs = (a2091_scsiregs *)(instance->base);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020048 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
49 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020051 /* don't allow DMA if the physical address is bad */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (addr & A2091_XFER_MASK) {
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020053 hdata->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
54 hdata->dma_bounce_buffer = kmalloc(hdata->dma_bounce_len,
55 GFP_KERNEL);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020056
57 /* can't allocate memory; use PIO */
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020058 if (!hdata->dma_bounce_buffer) {
59 hdata->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020060 return 1;
61 }
62
63 /* get the physical address of the bounce buffer */
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020064 addr = virt_to_bus(hdata->dma_bounce_buffer);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020065
66 /* the bounce buffer may not be in the first 16M of physmem */
67 if (addr & A2091_XFER_MASK) {
68 /* we could use chipmem... maybe later */
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020069 kfree(hdata->dma_bounce_buffer);
70 hdata->dma_bounce_buffer = NULL;
71 hdata->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020072 return 1;
73 }
74
75 if (!dir_in) {
76 /* copy to bounce buffer for a write */
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020077 memcpy(hdata->dma_bounce_buffer, cmd->SCp.ptr,
78 cmd->SCp.this_residual);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020082 /* setup dma direction */
83 if (!dir_in)
84 cntr |= CNTR_DDIR;
85
86 /* remember direction */
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020087 hdata->dma_dir = dir_in;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020088
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020089 regs->CNTR = cntr;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020090
91 /* setup DMA *physical* address */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020092 regs->ACR = addr;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020093
94 if (dir_in) {
95 /* invalidate any cache */
96 cache_clear(addr, cmd->SCp.this_residual);
97 } else {
98 /* push any dirty cache */
99 cache_push(addr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200101 /* start DMA */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200102 regs->ST_DMA = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200104 /* return success */
105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200108static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200109 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200111 struct WD33C93_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200112 a2091_scsiregs *regs = (a2091_scsiregs *)(instance->base);
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200113
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200114 /* disable SCSI interrupts */
115 unsigned short cntr = CNTR_PDMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200117 if (!hdata->dma_dir)
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200118 cntr |= CNTR_DDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200120 /* disable SCSI interrupts */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200121 regs->CNTR = cntr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200123 /* flush if we were reading */
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200124 if (hdata->dma_dir) {
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200125 regs->FLUSH = 1;
126 while (!(regs->ISTR & ISTR_FE_FLG))
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200127 ;
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200130 /* clear a possible interrupt */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200131 regs->CINT = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200133 /* stop DMA */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200134 regs->SP_DMA = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200136 /* restore the CONTROL bits (minus the direction flag) */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200137 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200139 /* copy from a bounce buffer, if necessary */
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200140 if (status && hdata->dma_bounce_buffer) {
141 if (hdata->dma_dir)
142 memcpy(SCpnt->SCp.ptr, hdata->dma_bounce_buffer,
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200143 SCpnt->SCp.this_residual);
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200144 kfree(hdata->dma_bounce_buffer);
145 hdata->dma_bounce_buffer = NULL;
146 hdata->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Adrian Bunk5880f482009-03-04 12:06:07 -0800150static int __init a2091_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200152 static unsigned char called = 0;
153 struct Scsi_Host *instance;
154 unsigned long address;
155 struct zorro_dev *z = NULL;
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200156 wd33c93_regs wdregs;
157 a2091_scsiregs *regs;
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200158 struct WD33C93_hostdata *hdata;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200159 int num_a2091 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200161 if (!MACH_IS_AMIGA || called)
162 return 0;
163 called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200165 tpnt->proc_name = "A2091";
166 tpnt->proc_info = &wd33c93_proc_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200168 while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
169 if (z->id != ZORRO_PROD_CBM_A590_A2091_1 &&
170 z->id != ZORRO_PROD_CBM_A590_A2091_2)
171 continue;
172 address = z->resource.start;
173 if (!request_mem_region(address, 256, "wd33c93"))
174 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200176 instance = scsi_register(tpnt, sizeof(struct WD33C93_hostdata));
177 if (instance == NULL)
178 goto release;
179 instance->base = ZTWO_VADDR(address);
180 instance->irq = IRQ_AMIGA_PORTS;
181 instance->unique_id = z->slotaddr;
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200182 regs = (a2091_scsiregs *)(instance->base);
183 regs->DAWR = DAWR_A2091;
184 wdregs.SASR = &regs->SASR;
185 wdregs.SCMD = &regs->SCMD;
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200186 hdata = shost_priv(instance);
187 hdata->no_sync = 0xff;
188 hdata->fast = 0;
189 hdata->dma_mode = CTRL_DMA;
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200190 wd33c93_init(instance, wdregs, dma_setup, dma_stop,
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200191 WD33C93_FS_8_10);
192 if (request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
193 "A2091 SCSI", instance))
194 goto unregister;
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200195 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200196 num_a2091++;
197 continue;
Geert Uytterhoevend38f47a2009-01-02 11:41:24 +0100198
199unregister:
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200200 scsi_unregister(instance);
Geert Uytterhoevend38f47a2009-01-02 11:41:24 +0100201release:
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200202 release_mem_region(address, 256);
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200205 return num_a2091;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200208static int a2091_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 /* FIXME perform bus-specific reset */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400211
Jeff Garzik df0ae242005-05-28 07:57:14 -0400212 /* FIXME 2: kill this function, and let midlayer fall back
213 to the same action, calling wd33c93_host_reset() */
214
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400215 spin_lock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 wd33c93_host_reset(cmd);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400217 spin_unlock_irq(cmd->device->host->host_lock);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return SUCCESS;
220}
221
222#define HOSTS_C
223
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100224static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .proc_name = "A2901",
226 .name = "Commodore A2091/A590 SCSI",
227 .detect = a2091_detect,
228 .release = a2091_release,
229 .queuecommand = wd33c93_queuecommand,
230 .eh_abort_handler = wd33c93_abort,
231 .eh_bus_reset_handler = a2091_bus_reset,
232 .eh_host_reset_handler = wd33c93_host_reset,
233 .can_queue = CAN_QUEUE,
234 .this_id = 7,
235 .sg_tablesize = SG_ALL,
236 .cmd_per_lun = CMD_PER_LUN,
237 .use_clustering = DISABLE_CLUSTERING
238};
239
240
241#include "scsi_module.c"
242
Adrian Bunk5880f482009-03-04 12:06:07 -0800243static int a2091_release(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245#ifdef MODULE
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200246 a2091_scsiregs *regs = (a2091_scsiregs *)(instance->base);
247
248 regs->CNTR = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 release_mem_region(ZTWO_PADDR(instance->base), 256);
250 free_irq(IRQ_AMIGA_PORTS, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#endif
252 return 1;
253}
254
255MODULE_LICENSE("GPL");