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