1#include <linux/types.h> 2#include <linux/init.h> 3#include <linux/interrupt.h> 4#include <linux/mm.h> 5#include <linux/slab.h> 6#include <linux/spinlock.h> 7#include <linux/zorro.h> 8 9#include <asm/page.h> 10#include <asm/pgtable.h> 11#include <asm/amigaints.h> 12#include <asm/amigahw.h> 13 14#include "scsi.h" 15#include "wd33c93.h" 16#include "a2091.h" 17 18 19struct a2091_hostdata { 20 struct WD33C93_hostdata wh; 21 struct a2091_scsiregs *regs; 22}; 23 24static irqreturn_t a2091_intr(int irq, void *data) 25{ 26 struct Scsi_Host *instance = data; 27 struct a2091_hostdata *hdata = shost_priv(instance); 28 unsigned int status = hdata->regs->ISTR; 29 unsigned long flags; 30 31 if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS)) 32 return IRQ_NONE; 33 34 spin_lock_irqsave(instance->host_lock, flags); 35 wd33c93_intr(instance); 36 spin_unlock_irqrestore(instance->host_lock, flags); 37 return IRQ_HANDLED; 38} 39 40static int dma_setup(struct scsi_cmnd *cmd, int dir_in) 41{ 42 struct Scsi_Host *instance = cmd->device->host; 43 struct a2091_hostdata *hdata = shost_priv(instance); 44 struct WD33C93_hostdata *wh = &hdata->wh; 45 struct a2091_scsiregs *regs = hdata->regs; 46 unsigned short cntr = CNTR_PDMD | CNTR_INTEN; 47 unsigned long addr = virt_to_bus(cmd->SCp.ptr); 48 49 /* don't allow DMA if the physical address is bad */ 50 if (addr & A2091_XFER_MASK) { 51 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff; 52 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len, 53 GFP_KERNEL); 54 55 /* can't allocate memory; use PIO */ 56 if (!wh->dma_bounce_buffer) { 57 wh->dma_bounce_len = 0; 58 return 1; 59 } 60 61 /* get the physical address of the bounce buffer */ 62 addr = virt_to_bus(wh->dma_bounce_buffer); 63 64 /* the bounce buffer may not be in the first 16M of physmem */ 65 if (addr & A2091_XFER_MASK) { 66 /* we could use chipmem... maybe later */ 67 kfree(wh->dma_bounce_buffer); 68 wh->dma_bounce_buffer = NULL; 69 wh->dma_bounce_len = 0; 70 return 1; 71 } 72 73 if (!dir_in) { 74 /* copy to bounce buffer for a write */ 75 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr, 76 cmd->SCp.this_residual); 77 } 78 } 79 80 /* setup dma direction */ 81 if (!dir_in) 82 cntr |= CNTR_DDIR; 83 84 /* remember direction */ 85 wh->dma_dir = dir_in; 86 87 regs->CNTR = cntr; 88 89 /* setup DMA *physical* address */ 90 regs->ACR = addr; 91 92 if (dir_in) { 93 /* invalidate any cache */ 94 cache_clear(addr, cmd->SCp.this_residual); 95 } else { 96 /* push any dirty cache */ 97 cache_push(addr, cmd->SCp.this_residual); 98 } 99 /* start DMA */ 100 regs->ST_DMA = 1; 101 102 /* return success */ 103 return 0; 104} 105 106static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt, 107 int status) 108{ 109 struct a2091_hostdata *hdata = shost_priv(instance); 110 struct WD33C93_hostdata *wh = &hdata->wh; 111 struct a2091_scsiregs *regs = hdata->regs; 112 113 /* disable SCSI interrupts */ 114 unsigned short cntr = CNTR_PDMD; 115 116 if (!wh->dma_dir) 117 cntr |= CNTR_DDIR; 118 119 /* disable SCSI interrupts */ 120 regs->CNTR = cntr; 121 122 /* flush if we were reading */ 123 if (wh->dma_dir) { 124 regs->FLUSH = 1; 125 while (!(regs->ISTR & ISTR_FE_FLG)) 126 ; 127 } 128 129 /* clear a possible interrupt */ 130 regs->CINT = 1; 131 132 /* stop DMA */ 133 regs->SP_DMA = 1; 134 135 /* restore the CONTROL bits (minus the direction flag) */ 136 regs->CNTR = CNTR_PDMD | CNTR_INTEN; 137 138 /* copy from a bounce buffer, if necessary */ 139 if (status && wh->dma_bounce_buffer) { 140 if (wh->dma_dir) 141 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer, 142 SCpnt->SCp.this_residual); 143 kfree(wh->dma_bounce_buffer); 144 wh->dma_bounce_buffer = NULL; 145 wh->dma_bounce_len = 0; 146 } 147} 148 149static int a2091_bus_reset(struct scsi_cmnd *cmd) 150{ 151 struct Scsi_Host *instance = cmd->device->host; 152 153 154 155 spin_lock_irq(instance->host_lock); 156 wd33c93_host_reset(cmd); 157 spin_unlock_irq(instance->host_lock); 158 159 return SUCCESS; 160} 161 162static struct scsi_host_template a2091_scsi_template = { 163 .module = THIS_MODULE, 164 .name = "Commodore A2091/A590 SCSI", 165 .proc_info = wd33c93_proc_info, 166 .proc_name = "A2901", 167 .queuecommand = wd33c93_queuecommand, 168 .eh_abort_handler = wd33c93_abort, 169 .eh_bus_reset_handler = a2091_bus_reset, 170 .eh_host_reset_handler = wd33c93_host_reset, 171 .can_queue = CAN_QUEUE, 172 .this_id = 7, 173 .sg_tablesize = SG_ALL, 174 .cmd_per_lun = CMD_PER_LUN, 175 .use_clustering = DISABLE_CLUSTERING 176}; 177 178static int __devinit a2091_probe(struct zorro_dev *z, 179 const struct zorro_device_id *ent) 180{ 181 struct Scsi_Host *instance; 182 int error; 183 struct a2091_scsiregs *regs; 184 wd33c93_regs wdregs; 185 struct a2091_hostdata *hdata; 186 187 if (!request_mem_region(z->resource.start, 256, "wd33c93")) 188 return -EBUSY; 189 190 instance = scsi_host_alloc(&a2091_scsi_template, 191 sizeof(struct a2091_hostdata)); 192 if (!instance) { 193 error = -ENOMEM; 194 goto fail_alloc; 195 } 196 197 instance->irq = IRQ_AMIGA_PORTS; 198 instance->unique_id = z->slotaddr; 199 200 regs = (struct a2091_scsiregs *)ZTWO_VADDR(z->resource.start); 201 regs->DAWR = DAWR_A2091; 202 203 wdregs.SASR = ®s->SASR; 204 wdregs.SCMD = ®s->SCMD; 205 206 hdata = shost_priv(instance); 207 hdata->wh.no_sync = 0xff; 208 hdata->wh.fast = 0; 209 hdata->wh.dma_mode = CTRL_DMA; 210 hdata->regs = regs; 211 212 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10); 213 error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED, 214 "A2091 SCSI", instance); 215 if (error) 216 goto fail_irq; 217 218 regs->CNTR = CNTR_PDMD | CNTR_INTEN; 219 220 error = scsi_add_host(instance, NULL); 221 if (error) 222 goto fail_host; 223 224 zorro_set_drvdata(z, instance); 225 226 scsi_scan_host(instance); 227 return 0; 228 229fail_host: 230 free_irq(IRQ_AMIGA_PORTS, instance); 231fail_irq: 232 scsi_host_put(instance); 233fail_alloc: 234 release_mem_region(z->resource.start, 256); 235 return error; 236} 237 238static void __devexit a2091_remove(struct zorro_dev *z) 239{ 240 struct Scsi_Host *instance = zorro_get_drvdata(z); 241 struct a2091_hostdata *hdata = shost_priv(instance); 242 243 hdata->regs->CNTR = 0; 244 scsi_remove_host(instance); 245 free_irq(IRQ_AMIGA_PORTS, instance); 246 scsi_host_put(instance); 247 release_mem_region(z->resource.start, 256); 248} 249 250static struct zorro_device_id a2091_zorro_tbl[] __devinitdata = { 251 { ZORRO_PROD_CBM_A590_A2091_1 }, 252 { ZORRO_PROD_CBM_A590_A2091_2 }, 253 { 0 } 254}; 255MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl); 256 257static struct zorro_driver a2091_driver = { 258 .name = "a2091", 259 .id_table = a2091_zorro_tbl, 260 .probe = a2091_probe, 261 .remove = __devexit_p(a2091_remove), 262}; 263 264static int __init a2091_init(void) 265{ 266 return zorro_register_driver(&a2091_driver); 267} 268module_init(a2091_init); 269 270static void __exit a2091_exit(void) 271{ 272 zorro_unregister_driver(&a2091_driver); 273} 274module_exit(a2091_exit); 275 276MODULE_DESCRIPTION("Commodore A2091/A590 SCSI"); 277MODULE_LICENSE("GPL"); 278