blob: 61aadc7acb4998a58f27cb1872d883d7e771378d [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>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -04008#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/page.h>
11#include <asm/pgtable.h>
12#include <asm/amigaints.h>
13#include <asm/amigahw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include "scsi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "wd33c93.h"
17#include "a2091.h"
18
Adrian Bunk5880f482009-03-04 12:06:07 -080019
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020020struct a2091_hostdata {
21 struct WD33C93_hostdata wh;
22 struct a2091_scsiregs *regs;
23};
24
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020025static irqreturn_t a2091_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020027 struct Scsi_Host *instance = data;
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020028 struct a2091_hostdata *hdata = shost_priv(instance);
29 unsigned int status = hdata->regs->ISTR;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020030 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020032 if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
33 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020035 spin_lock_irqsave(instance->host_lock, flags);
36 wd33c93_intr(instance);
37 spin_unlock_irqrestore(instance->host_lock, flags);
38 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Henrik Kretzschmar65396412006-09-12 23:49:33 +020041static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020043 struct Scsi_Host *instance = cmd->device->host;
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020044 struct a2091_hostdata *hdata = shost_priv(instance);
45 struct WD33C93_hostdata *wh = &hdata->wh;
46 struct a2091_scsiregs *regs = hdata->regs;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020047 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
48 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020050 /* don't allow DMA if the physical address is bad */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (addr & A2091_XFER_MASK) {
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020052 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
53 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
54 GFP_KERNEL);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020055
56 /* can't allocate memory; use PIO */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020057 if (!wh->dma_bounce_buffer) {
58 wh->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020059 return 1;
60 }
61
62 /* get the physical address of the bounce buffer */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020063 addr = virt_to_bus(wh->dma_bounce_buffer);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020064
65 /* the bounce buffer may not be in the first 16M of physmem */
66 if (addr & A2091_XFER_MASK) {
67 /* we could use chipmem... maybe later */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020068 kfree(wh->dma_bounce_buffer);
69 wh->dma_bounce_buffer = NULL;
70 wh->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020071 return 1;
72 }
73
74 if (!dir_in) {
75 /* copy to bounce buffer for a write */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020076 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020077 cmd->SCp.this_residual);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
80
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020081 /* setup dma direction */
82 if (!dir_in)
83 cntr |= CNTR_DDIR;
84
85 /* remember direction */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020086 wh->dma_dir = dir_in;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020087
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020088 regs->CNTR = cntr;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020089
90 /* setup DMA *physical* address */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020091 regs->ACR = addr;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020092
93 if (dir_in) {
94 /* invalidate any cache */
95 cache_clear(addr, cmd->SCp.this_residual);
96 } else {
97 /* push any dirty cache */
98 cache_push(addr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200100 /* start DMA */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200101 regs->ST_DMA = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200103 /* return success */
104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200107static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200108 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200110 struct a2091_hostdata *hdata = shost_priv(instance);
111 struct WD33C93_hostdata *wh = &hdata->wh;
112 struct a2091_scsiregs *regs = hdata->regs;
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 Uytterhoeven65c27842010-04-12 21:55:03 +0200117 if (!wh->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 Uytterhoeven65c27842010-04-12 21:55:03 +0200124 if (wh->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 Uytterhoeven65c27842010-04-12 21:55:03 +0200140 if (status && wh->dma_bounce_buffer) {
141 if (wh->dma_dir)
142 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200143 SCpnt->SCp.this_residual);
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200144 kfree(wh->dma_bounce_buffer);
145 wh->dma_bounce_buffer = NULL;
146 wh->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200150static struct scsi_host_template a2091_scsi_template = {
151 .module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .name = "Commodore A2091/A590 SCSI",
Al Viro408bb252013-03-31 00:30:35 -0400153 .show_info = wd33c93_show_info,
154 .write_info = wd33c93_write_info,
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200155 .proc_name = "A2901",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .queuecommand = wd33c93_queuecommand,
157 .eh_abort_handler = wd33c93_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .eh_host_reset_handler = wd33c93_host_reset,
159 .can_queue = CAN_QUEUE,
160 .this_id = 7,
161 .sg_tablesize = SG_ALL,
162 .cmd_per_lun = CMD_PER_LUN,
163 .use_clustering = DISABLE_CLUSTERING
164};
165
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800166static int a2091_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200168 struct Scsi_Host *instance;
169 int error;
170 struct a2091_scsiregs *regs;
171 wd33c93_regs wdregs;
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200172 struct a2091_hostdata *hdata;
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200173
174 if (!request_mem_region(z->resource.start, 256, "wd33c93"))
175 return -EBUSY;
176
177 instance = scsi_host_alloc(&a2091_scsi_template,
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200178 sizeof(struct a2091_hostdata));
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200179 if (!instance) {
180 error = -ENOMEM;
181 goto fail_alloc;
182 }
183
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200184 instance->irq = IRQ_AMIGA_PORTS;
185 instance->unique_id = z->slotaddr;
186
Geert Uytterhoeven6112ea02011-01-09 11:03:43 +0100187 regs = ZTWO_VADDR(z->resource.start);
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200188 regs->DAWR = DAWR_A2091;
189
190 wdregs.SASR = &regs->SASR;
191 wdregs.SCMD = &regs->SCMD;
192
193 hdata = shost_priv(instance);
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200194 hdata->wh.no_sync = 0xff;
195 hdata->wh.fast = 0;
196 hdata->wh.dma_mode = CTRL_DMA;
197 hdata->regs = regs;
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200198
199 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10);
200 error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
201 "A2091 SCSI", instance);
202 if (error)
203 goto fail_irq;
204
205 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
206
207 error = scsi_add_host(instance, NULL);
208 if (error)
209 goto fail_host;
210
211 zorro_set_drvdata(z, instance);
212
213 scsi_scan_host(instance);
214 return 0;
215
216fail_host:
217 free_irq(IRQ_AMIGA_PORTS, instance);
218fail_irq:
219 scsi_host_put(instance);
220fail_alloc:
221 release_mem_region(z->resource.start, 256);
222 return error;
223}
224
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800225static void a2091_remove(struct zorro_dev *z)
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200226{
227 struct Scsi_Host *instance = zorro_get_drvdata(z);
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200228 struct a2091_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200229
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200230 hdata->regs->CNTR = 0;
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200231 scsi_remove_host(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 free_irq(IRQ_AMIGA_PORTS, instance);
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200233 scsi_host_put(instance);
234 release_mem_region(z->resource.start, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800237static struct zorro_device_id a2091_zorro_tbl[] = {
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200238 { ZORRO_PROD_CBM_A590_A2091_1 },
239 { ZORRO_PROD_CBM_A590_A2091_2 },
240 { 0 }
241};
242MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl);
243
244static struct zorro_driver a2091_driver = {
245 .name = "a2091",
246 .id_table = a2091_zorro_tbl,
247 .probe = a2091_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800248 .remove = a2091_remove,
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200249};
250
251static int __init a2091_init(void)
252{
253 return zorro_register_driver(&a2091_driver);
254}
255module_init(a2091_init);
256
257static void __exit a2091_exit(void)
258{
259 zorro_unregister_driver(&a2091_driver);
260}
261module_exit(a2091_exit);
262
263MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264MODULE_LICENSE("GPL");