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