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/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 | |
| 22 | #include<linux/stat.h> |
| 23 | |
| 24 | #define DMA(ptr) ((a2091_scsiregs *)((ptr)->base)) |
| 25 | #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata)) |
| 26 | |
Adrian Bunk | 5880f48 | 2009-03-04 12:06:07 -0800 | [diff] [blame] | 27 | static int a2091_release(struct Scsi_Host *instance); |
| 28 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 29 | static irqreturn_t a2091_intr (int irq, void *_instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | unsigned long flags; |
| 32 | unsigned int status; |
| 33 | struct Scsi_Host *instance = (struct Scsi_Host *)_instance; |
| 34 | |
| 35 | status = DMA(instance)->ISTR; |
| 36 | if (!(status & (ISTR_INT_F|ISTR_INT_P)) || !(status & ISTR_INTS)) |
| 37 | return IRQ_NONE; |
| 38 | |
| 39 | spin_lock_irqsave(instance->host_lock, flags); |
| 40 | wd33c93_intr(instance); |
| 41 | spin_unlock_irqrestore(instance->host_lock, flags); |
| 42 | return IRQ_HANDLED; |
| 43 | } |
| 44 | |
Henrik Kretzschmar | 6539641 | 2006-09-12 23:49:33 +0200 | [diff] [blame] | 45 | static int dma_setup(struct scsi_cmnd *cmd, int dir_in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
| 47 | unsigned short cntr = CNTR_PDMD | CNTR_INTEN; |
| 48 | unsigned long addr = virt_to_bus(cmd->SCp.ptr); |
| 49 | struct Scsi_Host *instance = cmd->device->host; |
| 50 | |
| 51 | /* don't allow DMA if the physical address is bad */ |
Adrian Bunk | 7b89280 | 2008-02-06 01:36:29 -0800 | [diff] [blame] | 52 | if (addr & A2091_XFER_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
| 54 | HDATA(instance)->dma_bounce_len = (cmd->SCp.this_residual + 511) |
| 55 | & ~0x1ff; |
| 56 | HDATA(instance)->dma_bounce_buffer = |
| 57 | kmalloc (HDATA(instance)->dma_bounce_len, GFP_KERNEL); |
| 58 | |
| 59 | /* can't allocate memory; use PIO */ |
| 60 | if (!HDATA(instance)->dma_bounce_buffer) { |
| 61 | HDATA(instance)->dma_bounce_len = 0; |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | /* get the physical address of the bounce buffer */ |
| 66 | addr = virt_to_bus(HDATA(instance)->dma_bounce_buffer); |
| 67 | |
| 68 | /* the bounce buffer may not be in the first 16M of physmem */ |
| 69 | if (addr & A2091_XFER_MASK) { |
| 70 | /* we could use chipmem... maybe later */ |
| 71 | kfree (HDATA(instance)->dma_bounce_buffer); |
| 72 | HDATA(instance)->dma_bounce_buffer = NULL; |
| 73 | HDATA(instance)->dma_bounce_len = 0; |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | if (!dir_in) { |
Boaz Harrosh | f2c1afa | 2007-09-09 20:57:05 +0300 | [diff] [blame] | 78 | /* copy to bounce buffer for a write */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | memcpy (HDATA(instance)->dma_bounce_buffer, |
| 80 | cmd->SCp.ptr, cmd->SCp.this_residual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | /* setup dma direction */ |
| 85 | if (!dir_in) |
| 86 | cntr |= CNTR_DDIR; |
| 87 | |
| 88 | /* remember direction */ |
| 89 | HDATA(cmd->device->host)->dma_dir = dir_in; |
| 90 | |
| 91 | DMA(cmd->device->host)->CNTR = cntr; |
| 92 | |
| 93 | /* setup DMA *physical* address */ |
| 94 | DMA(cmd->device->host)->ACR = addr; |
| 95 | |
| 96 | if (dir_in){ |
| 97 | /* invalidate any cache */ |
| 98 | cache_clear (addr, cmd->SCp.this_residual); |
| 99 | }else{ |
| 100 | /* push any dirty cache */ |
| 101 | cache_push (addr, cmd->SCp.this_residual); |
| 102 | } |
| 103 | /* start DMA */ |
| 104 | DMA(cmd->device->host)->ST_DMA = 1; |
| 105 | |
| 106 | /* return success */ |
| 107 | return 0; |
| 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | int status) |
| 112 | { |
| 113 | /* disable SCSI interrupts */ |
| 114 | unsigned short cntr = CNTR_PDMD; |
| 115 | |
| 116 | if (!HDATA(instance)->dma_dir) |
| 117 | cntr |= CNTR_DDIR; |
| 118 | |
| 119 | /* disable SCSI interrupts */ |
| 120 | DMA(instance)->CNTR = cntr; |
| 121 | |
| 122 | /* flush if we were reading */ |
| 123 | if (HDATA(instance)->dma_dir) { |
| 124 | DMA(instance)->FLUSH = 1; |
| 125 | while (!(DMA(instance)->ISTR & ISTR_FE_FLG)) |
| 126 | ; |
| 127 | } |
| 128 | |
| 129 | /* clear a possible interrupt */ |
| 130 | DMA(instance)->CINT = 1; |
| 131 | |
| 132 | /* stop DMA */ |
| 133 | DMA(instance)->SP_DMA = 1; |
| 134 | |
| 135 | /* restore the CONTROL bits (minus the direction flag) */ |
| 136 | DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN; |
| 137 | |
| 138 | /* copy from a bounce buffer, if necessary */ |
| 139 | if (status && HDATA(instance)->dma_bounce_buffer) { |
Boaz Harrosh | f2c1afa | 2007-09-09 20:57:05 +0300 | [diff] [blame] | 140 | if( HDATA(instance)->dma_dir ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | memcpy (SCpnt->SCp.ptr, |
| 142 | HDATA(instance)->dma_bounce_buffer, |
| 143 | SCpnt->SCp.this_residual); |
Boaz Harrosh | f2c1afa | 2007-09-09 20:57:05 +0300 | [diff] [blame] | 144 | kfree (HDATA(instance)->dma_bounce_buffer); |
| 145 | HDATA(instance)->dma_bounce_buffer = NULL; |
| 146 | HDATA(instance)->dma_bounce_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Adrian Bunk | 5880f48 | 2009-03-04 12:06:07 -0800 | [diff] [blame] | 150 | static int __init a2091_detect(struct scsi_host_template *tpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | static unsigned char called = 0; |
| 153 | struct Scsi_Host *instance; |
| 154 | unsigned long address; |
| 155 | struct zorro_dev *z = NULL; |
| 156 | wd33c93_regs regs; |
| 157 | int num_a2091 = 0; |
| 158 | |
| 159 | if (!MACH_IS_AMIGA || called) |
| 160 | return 0; |
| 161 | called = 1; |
| 162 | |
| 163 | tpnt->proc_name = "A2091"; |
| 164 | tpnt->proc_info = &wd33c93_proc_info; |
| 165 | |
| 166 | while ((z = zorro_find_device(ZORRO_WILDCARD, z))) { |
| 167 | if (z->id != ZORRO_PROD_CBM_A590_A2091_1 && |
| 168 | z->id != ZORRO_PROD_CBM_A590_A2091_2) |
| 169 | continue; |
| 170 | address = z->resource.start; |
| 171 | if (!request_mem_region(address, 256, "wd33c93")) |
| 172 | continue; |
| 173 | |
| 174 | instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata)); |
Geert Uytterhoeven | d38f47a | 2009-01-02 11:41:24 +0100 | [diff] [blame] | 175 | if (instance == NULL) |
| 176 | goto release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | instance->base = ZTWO_VADDR(address); |
| 178 | instance->irq = IRQ_AMIGA_PORTS; |
| 179 | instance->unique_id = z->slotaddr; |
| 180 | DMA(instance)->DAWR = DAWR_A2091; |
| 181 | regs.SASR = &(DMA(instance)->SASR); |
| 182 | regs.SCMD = &(DMA(instance)->SCMD); |
James Bottomley | a579dab | 2008-03-31 22:06:50 -0500 | [diff] [blame] | 183 | HDATA(instance)->no_sync = 0xff; |
| 184 | HDATA(instance)->fast = 0; |
| 185 | HDATA(instance)->dma_mode = CTRL_DMA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | wd33c93_init(instance, regs, dma_setup, dma_stop, WD33C93_FS_8_10); |
Geert Uytterhoeven | d38f47a | 2009-01-02 11:41:24 +0100 | [diff] [blame] | 187 | if (request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED, "A2091 SCSI", |
| 188 | instance)) |
| 189 | goto unregister; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN; |
| 191 | num_a2091++; |
Geert Uytterhoeven | d38f47a | 2009-01-02 11:41:24 +0100 | [diff] [blame] | 192 | continue; |
| 193 | |
| 194 | unregister: |
| 195 | scsi_unregister(instance); |
| 196 | wd33c93_release(); |
| 197 | release: |
| 198 | release_mem_region(address, 256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return num_a2091; |
| 202 | } |
| 203 | |
Henrik Kretzschmar | 6539641 | 2006-09-12 23:49:33 +0200 | [diff] [blame] | 204 | static int a2091_bus_reset(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | /* FIXME perform bus-specific reset */ |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 207 | |
Jeff Garzik | df0ae24 | 2005-05-28 07:57:14 -0400 | [diff] [blame] | 208 | /* FIXME 2: kill this function, and let midlayer fall back |
| 209 | to the same action, calling wd33c93_host_reset() */ |
| 210 | |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 211 | spin_lock_irq(cmd->device->host->host_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | wd33c93_host_reset(cmd); |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 213 | spin_unlock_irq(cmd->device->host->host_lock); |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return SUCCESS; |
| 216 | } |
| 217 | |
| 218 | #define HOSTS_C |
| 219 | |
Christoph Hellwig | d0be4a7d | 2005-10-31 18:31:40 +0100 | [diff] [blame] | 220 | static struct scsi_host_template driver_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | .proc_name = "A2901", |
| 222 | .name = "Commodore A2091/A590 SCSI", |
| 223 | .detect = a2091_detect, |
| 224 | .release = a2091_release, |
| 225 | .queuecommand = wd33c93_queuecommand, |
| 226 | .eh_abort_handler = wd33c93_abort, |
| 227 | .eh_bus_reset_handler = a2091_bus_reset, |
| 228 | .eh_host_reset_handler = wd33c93_host_reset, |
| 229 | .can_queue = CAN_QUEUE, |
| 230 | .this_id = 7, |
| 231 | .sg_tablesize = SG_ALL, |
| 232 | .cmd_per_lun = CMD_PER_LUN, |
| 233 | .use_clustering = DISABLE_CLUSTERING |
| 234 | }; |
| 235 | |
| 236 | |
| 237 | #include "scsi_module.c" |
| 238 | |
Adrian Bunk | 5880f48 | 2009-03-04 12:06:07 -0800 | [diff] [blame] | 239 | static int a2091_release(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
| 241 | #ifdef MODULE |
| 242 | DMA(instance)->CNTR = 0; |
| 243 | release_mem_region(ZTWO_PADDR(instance->base), 256); |
| 244 | free_irq(IRQ_AMIGA_PORTS, instance); |
| 245 | wd33c93_release(); |
| 246 | #endif |
| 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | MODULE_LICENSE("GPL"); |