Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/types.h> |
| 2 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 3 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/ioport.h> |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/spinlock.h> |
| 8 | #include <linux/interrupt.h> |
| 9 | |
| 10 | #include <asm/setup.h> |
| 11 | #include <asm/page.h> |
| 12 | #include <asm/pgtable.h> |
| 13 | #include <asm/amigaints.h> |
| 14 | #include <asm/amigahw.h> |
| 15 | #include <asm/irq.h> |
| 16 | |
| 17 | #include "scsi.h" |
| 18 | #include <scsi/scsi_host.h> |
| 19 | #include "wd33c93.h" |
| 20 | #include "a3000.h" |
| 21 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 22 | #include <linux/stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 24 | |
| 25 | #define DMA(ptr) ((a3000_scsiregs *)((ptr)->base)) |
| 26 | #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | static struct Scsi_Host *a3000_host = NULL; |
| 29 | |
Adrian Bunk | 9387edb | 2009-03-04 12:06:08 -0800 | [diff] [blame] | 30 | static int a3000_release(struct Scsi_Host *instance); |
| 31 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 32 | static irqreturn_t a3000_intr(int irq, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | unsigned long flags; |
| 35 | unsigned int status = DMA(a3000_host)->ISTR; |
| 36 | |
| 37 | if (!(status & ISTR_INT_P)) |
| 38 | return IRQ_NONE; |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 39 | if (status & ISTR_INTS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | spin_lock_irqsave(a3000_host->host_lock, flags); |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 41 | wd33c93_intr(a3000_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | spin_unlock_irqrestore(a3000_host->host_lock, flags); |
| 43 | return IRQ_HANDLED; |
| 44 | } |
| 45 | printk("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status); |
| 46 | return IRQ_NONE; |
| 47 | } |
| 48 | |
Henrik Kretzschmar | 6539641 | 2006-09-12 23:49:33 +0200 | [diff] [blame] | 49 | static int dma_setup(struct scsi_cmnd *cmd, int dir_in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 51 | unsigned short cntr = CNTR_PDMD | CNTR_INTEN; |
| 52 | unsigned long addr = virt_to_bus(cmd->SCp.ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 54 | /* |
| 55 | * if the physical address has the wrong alignment, or if |
| 56 | * physical address is bad, or if it is a write and at the |
| 57 | * end of a physical memory chunk, then allocate a bounce |
| 58 | * buffer |
| 59 | */ |
| 60 | if (addr & A3000_XFER_MASK) { |
| 61 | HDATA(a3000_host)->dma_bounce_len = |
| 62 | (cmd->SCp.this_residual + 511) & ~0x1ff; |
| 63 | HDATA(a3000_host)->dma_bounce_buffer = |
| 64 | kmalloc(HDATA(a3000_host)->dma_bounce_len, GFP_KERNEL); |
| 65 | |
| 66 | /* can't allocate memory; use PIO */ |
| 67 | if (!HDATA(a3000_host)->dma_bounce_buffer) { |
| 68 | HDATA(a3000_host)->dma_bounce_len = 0; |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | if (!dir_in) { |
| 73 | /* copy to bounce buffer for a write */ |
| 74 | memcpy(HDATA(a3000_host)->dma_bounce_buffer, |
| 75 | cmd->SCp.ptr, cmd->SCp.this_residual); |
| 76 | } |
| 77 | |
| 78 | addr = virt_to_bus(HDATA(a3000_host)->dma_bounce_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 81 | /* setup dma direction */ |
| 82 | if (!dir_in) |
| 83 | cntr |= CNTR_DDIR; |
| 84 | |
| 85 | /* remember direction */ |
| 86 | HDATA(a3000_host)->dma_dir = dir_in; |
| 87 | |
| 88 | DMA(a3000_host)->CNTR = cntr; |
| 89 | |
| 90 | /* setup DMA *physical* address */ |
| 91 | DMA(a3000_host)->ACR = addr; |
| 92 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 101 | /* start DMA */ |
| 102 | mb(); /* make sure setup is completed */ |
| 103 | DMA(a3000_host)->ST_DMA = 1; |
| 104 | mb(); /* make sure DMA has started before next IO */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 106 | /* return success */ |
| 107 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Henrik Kretzschmar | 6539641 | 2006-09-12 23:49:33 +0200 | [diff] [blame] | 110 | static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt, |
| 111 | int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 113 | /* disable SCSI interrupts */ |
| 114 | unsigned short cntr = CNTR_PDMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 116 | if (!HDATA(instance)->dma_dir) |
| 117 | cntr |= CNTR_DDIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 119 | DMA(instance)->CNTR = cntr; |
| 120 | mb(); /* make sure CNTR is updated before next IO */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 122 | /* flush if we were reading */ |
| 123 | if (HDATA(instance)->dma_dir) { |
| 124 | DMA(instance)->FLUSH = 1; |
| 125 | mb(); /* don't allow prefetch */ |
| 126 | while (!(DMA(instance)->ISTR & ISTR_FE_FLG)) |
| 127 | barrier(); |
| 128 | mb(); /* no IO until FLUSH is done */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 130 | |
| 131 | /* clear a possible interrupt */ |
| 132 | /* I think that this CINT is only necessary if you are |
| 133 | * using the terminal count features. HM 7 Mar 1994 |
| 134 | */ |
| 135 | DMA(instance)->CINT = 1; |
| 136 | |
| 137 | /* stop DMA */ |
| 138 | DMA(instance)->SP_DMA = 1; |
| 139 | mb(); /* make sure DMA is stopped before next IO */ |
| 140 | |
| 141 | /* restore the CONTROL bits (minus the direction flag) */ |
| 142 | DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN; |
| 143 | mb(); /* make sure CNTR is updated before next IO */ |
| 144 | |
| 145 | /* copy from a bounce buffer, if necessary */ |
| 146 | if (status && HDATA(instance)->dma_bounce_buffer) { |
| 147 | if (SCpnt) { |
| 148 | if (HDATA(instance)->dma_dir && SCpnt) |
| 149 | memcpy(SCpnt->SCp.ptr, |
| 150 | HDATA(instance)->dma_bounce_buffer, |
| 151 | SCpnt->SCp.this_residual); |
| 152 | kfree(HDATA(instance)->dma_bounce_buffer); |
| 153 | HDATA(instance)->dma_bounce_buffer = NULL; |
| 154 | HDATA(instance)->dma_bounce_len = 0; |
| 155 | } else { |
| 156 | kfree(HDATA(instance)->dma_bounce_buffer); |
| 157 | HDATA(instance)->dma_bounce_buffer = NULL; |
| 158 | HDATA(instance)->dma_bounce_len = 0; |
| 159 | } |
| 160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Adrian Bunk | 9387edb | 2009-03-04 12:06:08 -0800 | [diff] [blame] | 163 | static int __init a3000_detect(struct scsi_host_template *tpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 165 | wd33c93_regs regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 167 | if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(A3000_SCSI)) |
| 168 | return 0; |
| 169 | if (!request_mem_region(0xDD0000, 256, "wd33c93")) |
| 170 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 172 | tpnt->proc_name = "A3000"; |
| 173 | tpnt->proc_info = &wd33c93_proc_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 175 | a3000_host = scsi_register(tpnt, sizeof(struct WD33C93_hostdata)); |
| 176 | if (a3000_host == NULL) |
| 177 | goto fail_register; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 179 | a3000_host->base = ZTWO_VADDR(0xDD0000); |
| 180 | a3000_host->irq = IRQ_AMIGA_PORTS; |
| 181 | DMA(a3000_host)->DAWR = DAWR_A3000; |
| 182 | regs.SASR = &(DMA(a3000_host)->SASR); |
| 183 | regs.SCMD = &(DMA(a3000_host)->SCMD); |
| 184 | HDATA(a3000_host)->no_sync = 0xff; |
| 185 | HDATA(a3000_host)->fast = 0; |
| 186 | HDATA(a3000_host)->dma_mode = CTRL_DMA; |
| 187 | wd33c93_init(a3000_host, regs, dma_setup, dma_stop, WD33C93_FS_12_15); |
| 188 | if (request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, "A3000 SCSI", |
| 189 | a3000_intr)) |
| 190 | goto fail_irq; |
| 191 | DMA(a3000_host)->CNTR = CNTR_PDMD | CNTR_INTEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 193 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | fail_irq: |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 196 | scsi_unregister(a3000_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | fail_register: |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 198 | release_mem_region(0xDD0000, 256); |
| 199 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Henrik Kretzschmar | 6539641 | 2006-09-12 23:49:33 +0200 | [diff] [blame] | 202 | static int a3000_bus_reset(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | /* FIXME perform bus-specific reset */ |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 205 | |
Jeff Garzik | df0ae24 | 2005-05-28 07:57:14 -0400 | [diff] [blame] | 206 | /* FIXME 2: kill this entire function, which should |
| 207 | cause mid-layer to call wd33c93_host_reset anyway? */ |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 208 | |
| 209 | spin_lock_irq(cmd->device->host->host_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | wd33c93_host_reset(cmd); |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 211 | spin_unlock_irq(cmd->device->host->host_lock); |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return SUCCESS; |
| 214 | } |
| 215 | |
| 216 | #define HOSTS_C |
| 217 | |
Christoph Hellwig | d0be4a7d | 2005-10-31 18:31:40 +0100 | [diff] [blame] | 218 | static struct scsi_host_template driver_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | .proc_name = "A3000", |
| 220 | .name = "Amiga 3000 built-in SCSI", |
| 221 | .detect = a3000_detect, |
| 222 | .release = a3000_release, |
| 223 | .queuecommand = wd33c93_queuecommand, |
| 224 | .eh_abort_handler = wd33c93_abort, |
| 225 | .eh_bus_reset_handler = a3000_bus_reset, |
| 226 | .eh_host_reset_handler = wd33c93_host_reset, |
| 227 | .can_queue = CAN_QUEUE, |
| 228 | .this_id = 7, |
| 229 | .sg_tablesize = SG_ALL, |
| 230 | .cmd_per_lun = CMD_PER_LUN, |
| 231 | .use_clustering = ENABLE_CLUSTERING |
| 232 | }; |
| 233 | |
| 234 | |
| 235 | #include "scsi_module.c" |
| 236 | |
Adrian Bunk | 9387edb | 2009-03-04 12:06:08 -0800 | [diff] [blame] | 237 | static int a3000_release(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Geert Uytterhoeven | 2135101 | 2010-04-04 11:00:35 +0200 | [diff] [blame^] | 239 | DMA(instance)->CNTR = 0; |
| 240 | release_mem_region(0xDD0000, 256); |
| 241 | free_irq(IRQ_AMIGA_PORTS, a3000_intr); |
| 242 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | MODULE_LICENSE("GPL"); |