Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-block.git] / drivers / scsi / mac_scsi.c
CommitLineData
1da177e4
LT
1/*
2 * Generic Macintosh NCR5380 driver
3 *
4 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
5 *
6 * derived in part from:
7 */
8/*
9 * Generic Generic NCR5380 driver
10 *
11 * Copyright 1995, Russell King
1da177e4
LT
12 */
13
1da177e4 14#include <linux/types.h>
1da177e4 15#include <linux/module.h>
1da177e4
LT
16#include <linux/ioport.h>
17#include <linux/init.h>
18#include <linux/blkdev.h>
19#include <linux/interrupt.h>
cbad48de 20#include <linux/platform_device.h>
1da177e4 21
cbad48de 22#include <asm/hwtest.h>
1da177e4 23#include <asm/io.h>
1da177e4 24#include <asm/macints.h>
cbad48de 25#include <asm/setup.h>
1da177e4 26
1da177e4 27#include <scsi/scsi_host.h>
92de3831
FT
28
29/* Definitions for the core NCR5380 driver. */
e744fdea 30
3a0f64bf
FT
31#define NCR5380_implementation_fields unsigned char *pdma_base; \
32 int pdma_residual
92de3831 33
54d8fe44
FT
34#define NCR5380_read(reg) macscsi_read(instance, reg)
35#define NCR5380_write(reg, value) macscsi_write(instance, reg, value)
92de3831 36
7e9ec8d9
FT
37#define NCR5380_dma_xfer_len(instance, cmd, phase) \
38 macscsi_dma_xfer_len(instance, cmd)
6c4b88ca
FT
39#define NCR5380_dma_recv_setup macscsi_pread
40#define NCR5380_dma_send_setup macscsi_pwrite
3a0f64bf 41#define NCR5380_dma_residual(instance) (hostdata->pdma_residual)
92de3831
FT
42
43#define NCR5380_intr macscsi_intr
44#define NCR5380_queue_command macscsi_queue_command
45#define NCR5380_abort macscsi_abort
46#define NCR5380_bus_reset macscsi_bus_reset
47#define NCR5380_info macscsi_info
92de3831 48
1da177e4
LT
49#include "NCR5380.h"
50
1da177e4 51static int setup_can_queue = -1;
6e9ae6d5 52module_param(setup_can_queue, int, 0);
1da177e4 53static int setup_cmd_per_lun = -1;
6e9ae6d5 54module_param(setup_cmd_per_lun, int, 0);
1da177e4 55static int setup_sg_tablesize = -1;
6e9ae6d5 56module_param(setup_sg_tablesize, int, 0);
1da177e4 57static int setup_use_pdma = -1;
6e9ae6d5 58module_param(setup_use_pdma, int, 0);
1da177e4 59static int setup_hostid = -1;
6e9ae6d5 60module_param(setup_hostid, int, 0);
9c3f0e2b
FT
61static int setup_toshiba_delay = -1;
62module_param(setup_toshiba_delay, int, 0);
1da177e4 63
1da177e4
LT
64/*
65 * NCR 5380 register access functions
66 */
67
cbad48de 68static inline char macscsi_read(struct Scsi_Host *instance, int reg)
1da177e4 69{
cbad48de 70 return in_8(instance->base + (reg << 4));
1da177e4
LT
71}
72
cbad48de 73static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value)
1da177e4 74{
cbad48de 75 out_8(instance->base + (reg << 4), value);
1da177e4 76}
1da177e4 77
6e9ae6d5
FT
78#ifndef MODULE
79static int __init mac_scsi_setup(char *str)
80{
9c3f0e2b 81 int ints[8];
6e9ae6d5
FT
82
83 (void)get_options(str, ARRAY_SIZE(ints), ints);
84
9c3f0e2b
FT
85 if (ints[0] < 1) {
86 pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
6e9ae6d5 87 return 0;
1da177e4 88 }
6e9ae6d5
FT
89 if (ints[0] >= 1)
90 setup_can_queue = ints[1];
91 if (ints[0] >= 2)
92 setup_cmd_per_lun = ints[2];
93 if (ints[0] >= 3)
94 setup_sg_tablesize = ints[3];
95 if (ints[0] >= 4)
96 setup_hostid = ints[4];
c4ec6f92 97 /* ints[5] (use_tagged_queuing) is ignored */
6e9ae6d5 98 if (ints[0] >= 6)
1da177e4 99 setup_use_pdma = ints[6];
9c3f0e2b
FT
100 if (ints[0] >= 7)
101 setup_toshiba_delay = ints[7];
1da177e4
LT
102 return 1;
103}
104
105__setup("mac5380=", mac_scsi_setup);
6e9ae6d5 106#endif /* !MODULE */
1da177e4 107
3a0f64bf
FT
108/* Pseudo DMA asm originally by Ove Edlund */
109
110#define CP_IO_TO_MEM(s,d,n) \
1da177e4
LT
111__asm__ __volatile__ \
112 (" cmp.w #4,%2\n" \
113 " bls 8f\n" \
114 " move.w %1,%%d0\n" \
115 " neg.b %%d0\n" \
116 " and.w #3,%%d0\n" \
117 " sub.w %%d0,%2\n" \
118 " bra 2f\n" \
119 " 1: move.b (%0),(%1)+\n" \
120 " 2: dbf %%d0,1b\n" \
121 " move.w %2,%%d0\n" \
122 " lsr.w #5,%%d0\n" \
123 " bra 4f\n" \
124 " 3: move.l (%0),(%1)+\n" \
125 "31: move.l (%0),(%1)+\n" \
126 "32: move.l (%0),(%1)+\n" \
127 "33: move.l (%0),(%1)+\n" \
128 "34: move.l (%0),(%1)+\n" \
129 "35: move.l (%0),(%1)+\n" \
130 "36: move.l (%0),(%1)+\n" \
131 "37: move.l (%0),(%1)+\n" \
132 " 4: dbf %%d0,3b\n" \
133 " move.w %2,%%d0\n" \
134 " lsr.w #2,%%d0\n" \
135 " and.w #7,%%d0\n" \
136 " bra 6f\n" \
137 " 5: move.l (%0),(%1)+\n" \
138 " 6: dbf %%d0,5b\n" \
139 " and.w #3,%2\n" \
140 " bra 8f\n" \
141 " 7: move.b (%0),(%1)+\n" \
142 " 8: dbf %2,7b\n" \
143 " moveq.l #0, %2\n" \
144 " 9: \n" \
145 ".section .fixup,\"ax\"\n" \
146 " .even\n" \
3a0f64bf
FT
147 "91: moveq.l #1, %2\n" \
148 " jra 9b\n" \
149 "94: moveq.l #4, %2\n" \
1da177e4
LT
150 " jra 9b\n" \
151 ".previous\n" \
152 ".section __ex_table,\"a\"\n" \
153 " .align 4\n" \
3a0f64bf
FT
154 " .long 1b,91b\n" \
155 " .long 3b,94b\n" \
156 " .long 31b,94b\n" \
157 " .long 32b,94b\n" \
158 " .long 33b,94b\n" \
159 " .long 34b,94b\n" \
160 " .long 35b,94b\n" \
161 " .long 36b,94b\n" \
162 " .long 37b,94b\n" \
163 " .long 5b,94b\n" \
164 " .long 7b,91b\n" \
1da177e4 165 ".previous" \
3a0f64bf
FT
166 : "=a"(s), "=a"(d), "=d"(n) \
167 : "0"(s), "1"(d), "2"(n) \
1da177e4
LT
168 : "d0")
169
ffdede67
FT
170static int macscsi_pread(struct Scsi_Host *instance,
171 unsigned char *dst, int len)
1da177e4 172{
cbad48de 173 struct NCR5380_hostdata *hostdata = shost_priv(instance);
3a0f64bf
FT
174 unsigned char *s = hostdata->pdma_base + (INPUT_DATA_REG << 4);
175 unsigned char *d = dst;
176 int n = len;
177 int transferred;
178
179 while (!NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
180 BASR_DRQ | BASR_PHASE_MATCH,
181 BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
182 CP_IO_TO_MEM(s, d, n);
183
184 transferred = d - dst - n;
185 hostdata->pdma_residual = len - transferred;
186
187 /* No bus error. */
188 if (n == 0)
189 return 0;
190
191 /* Target changed phase early? */
192 if (NCR5380_poll_politely2(instance, STATUS_REG, SR_REQ, SR_REQ,
193 BUS_AND_STATUS_REG, BASR_ACK, BASR_ACK, HZ / 64) < 0)
194 scmd_printk(KERN_ERR, hostdata->connected,
195 "%s: !REQ and !ACK\n", __func__);
196 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
197 return 0;
198
199 dsprintk(NDEBUG_PSEUDO_DMA, instance,
200 "%s: bus error (%d/%d)\n", __func__, transferred, len);
201 NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance);
202 d = dst + transferred;
203 n = len - transferred;
ffdede67
FT
204 }
205
3a0f64bf
FT
206 scmd_printk(KERN_ERR, hostdata->connected,
207 "%s: phase mismatch or !DRQ\n", __func__);
208 NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance);
209 return -1;
1da177e4
LT
210}
211
212
3a0f64bf 213#define CP_MEM_TO_IO(s,d,n) \
1da177e4
LT
214__asm__ __volatile__ \
215 (" cmp.w #4,%2\n" \
216 " bls 8f\n" \
217 " move.w %0,%%d0\n" \
218 " neg.b %%d0\n" \
219 " and.w #3,%%d0\n" \
220 " sub.w %%d0,%2\n" \
221 " bra 2f\n" \
222 " 1: move.b (%0)+,(%1)\n" \
223 " 2: dbf %%d0,1b\n" \
224 " move.w %2,%%d0\n" \
225 " lsr.w #5,%%d0\n" \
226 " bra 4f\n" \
227 " 3: move.l (%0)+,(%1)\n" \
228 "31: move.l (%0)+,(%1)\n" \
229 "32: move.l (%0)+,(%1)\n" \
230 "33: move.l (%0)+,(%1)\n" \
231 "34: move.l (%0)+,(%1)\n" \
232 "35: move.l (%0)+,(%1)\n" \
233 "36: move.l (%0)+,(%1)\n" \
234 "37: move.l (%0)+,(%1)\n" \
235 " 4: dbf %%d0,3b\n" \
236 " move.w %2,%%d0\n" \
237 " lsr.w #2,%%d0\n" \
238 " and.w #7,%%d0\n" \
239 " bra 6f\n" \
240 " 5: move.l (%0)+,(%1)\n" \
241 " 6: dbf %%d0,5b\n" \
242 " and.w #3,%2\n" \
243 " bra 8f\n" \
244 " 7: move.b (%0)+,(%1)\n" \
245 " 8: dbf %2,7b\n" \
246 " moveq.l #0, %2\n" \
247 " 9: \n" \
248 ".section .fixup,\"ax\"\n" \
249 " .even\n" \
3a0f64bf
FT
250 "91: moveq.l #1, %2\n" \
251 " jra 9b\n" \
252 "94: moveq.l #4, %2\n" \
1da177e4
LT
253 " jra 9b\n" \
254 ".previous\n" \
255 ".section __ex_table,\"a\"\n" \
256 " .align 4\n" \
3a0f64bf
FT
257 " .long 1b,91b\n" \
258 " .long 3b,94b\n" \
259 " .long 31b,94b\n" \
260 " .long 32b,94b\n" \
261 " .long 33b,94b\n" \
262 " .long 34b,94b\n" \
263 " .long 35b,94b\n" \
264 " .long 36b,94b\n" \
265 " .long 37b,94b\n" \
266 " .long 5b,94b\n" \
267 " .long 7b,91b\n" \
1da177e4 268 ".previous" \
3a0f64bf
FT
269 : "=a"(s), "=a"(d), "=d"(n) \
270 : "0"(s), "1"(d), "2"(n) \
1da177e4
LT
271 : "d0")
272
ffdede67
FT
273static int macscsi_pwrite(struct Scsi_Host *instance,
274 unsigned char *src, int len)
1da177e4 275{
cbad48de 276 struct NCR5380_hostdata *hostdata = shost_priv(instance);
3a0f64bf
FT
277 unsigned char *s = src;
278 unsigned char *d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4);
279 int n = len;
280 int transferred;
281
282 while (!NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
283 BASR_DRQ | BASR_PHASE_MATCH,
284 BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
285 CP_MEM_TO_IO(s, d, n);
286
287 transferred = s - src - n;
288 hostdata->pdma_residual = len - transferred;
289
290 /* Target changed phase early? */
291 if (NCR5380_poll_politely2(instance, STATUS_REG, SR_REQ, SR_REQ,
292 BUS_AND_STATUS_REG, BASR_ACK, BASR_ACK, HZ / 64) < 0)
293 scmd_printk(KERN_ERR, hostdata->connected,
294 "%s: !REQ and !ACK\n", __func__);
295 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
296 return 0;
297
298 /* No bus error. */
299 if (n == 0) {
300 if (NCR5380_poll_politely(instance, TARGET_COMMAND_REG,
301 TCR_LAST_BYTE_SENT,
302 TCR_LAST_BYTE_SENT, HZ / 64) < 0)
303 scmd_printk(KERN_ERR, hostdata->connected,
304 "%s: Last Byte Sent timeout\n", __func__);
305 return 0;
306 }
307
308 dsprintk(NDEBUG_PSEUDO_DMA, instance,
309 "%s: bus error (%d/%d)\n", __func__, transferred, len);
310 NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance);
311 s = src + transferred;
312 n = len - transferred;
ffdede67
FT
313 }
314
3a0f64bf
FT
315 scmd_printk(KERN_ERR, hostdata->connected,
316 "%s: phase mismatch or !DRQ\n", __func__);
317 NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance);
ffdede67 318
3a0f64bf 319 return -1;
ffdede67 320}
1da177e4 321
7e9ec8d9
FT
322static int macscsi_dma_xfer_len(struct Scsi_Host *instance,
323 struct scsi_cmnd *cmd)
324{
325 struct NCR5380_hostdata *hostdata = shost_priv(instance);
326
3a0f64bf
FT
327 if (hostdata->flags & FLAG_NO_PSEUDO_DMA ||
328 cmd->SCp.this_residual < 16)
7e9ec8d9
FT
329 return 0;
330
3a0f64bf 331 return cmd->SCp.this_residual;
7e9ec8d9
FT
332}
333
1da177e4
LT
334#include "NCR5380.c"
335
cbad48de
FT
336#define DRV_MODULE_NAME "mac_scsi"
337#define PFX DRV_MODULE_NAME ": "
338
339static struct scsi_host_template mac_scsi_template = {
aa2e2cb1
FT
340 .module = THIS_MODULE,
341 .proc_name = DRV_MODULE_NAME,
aa2e2cb1
FT
342 .name = "Macintosh NCR5380 SCSI",
343 .info = macscsi_info,
344 .queuecommand = macscsi_queue_command,
345 .eh_abort_handler = macscsi_abort,
346 .eh_bus_reset_handler = macscsi_bus_reset,
347 .can_queue = 16,
348 .this_id = 7,
3a0f64bf 349 .sg_tablesize = 1,
aa2e2cb1
FT
350 .cmd_per_lun = 2,
351 .use_clustering = DISABLE_CLUSTERING,
32b26a10 352 .cmd_size = NCR5380_CMD_SIZE,
0a4e3612 353 .max_sectors = 128,
1da177e4
LT
354};
355
cbad48de
FT
356static int __init mac_scsi_probe(struct platform_device *pdev)
357{
358 struct Scsi_Host *instance;
359 int error;
360 int host_flags = 0;
361 struct resource *irq, *pio_mem, *pdma_mem = NULL;
362
363 pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
364 if (!pio_mem)
365 return -ENODEV;
366
cbad48de 367 pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
cbad48de
FT
368
369 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
370
371 if (!hwreg_present((unsigned char *)pio_mem->start +
372 (STATUS_REG << 4))) {
373 pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
374 return -ENODEV;
375 }
376
377 if (setup_can_queue > 0)
378 mac_scsi_template.can_queue = setup_can_queue;
379 if (setup_cmd_per_lun > 0)
380 mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
381 if (setup_sg_tablesize >= 0)
382 mac_scsi_template.sg_tablesize = setup_sg_tablesize;
383 if (setup_hostid >= 0)
384 mac_scsi_template.this_id = setup_hostid & 7;
cbad48de
FT
385
386 instance = scsi_host_alloc(&mac_scsi_template,
387 sizeof(struct NCR5380_hostdata));
388 if (!instance)
389 return -ENOMEM;
390
391 instance->base = pio_mem->start;
392 if (irq)
393 instance->irq = irq->start;
394 else
395 instance->irq = NO_IRQ;
396
397 if (pdma_mem && setup_use_pdma) {
398 struct NCR5380_hostdata *hostdata = shost_priv(instance);
399
400 hostdata->pdma_base = (unsigned char *)pdma_mem->start;
401 } else
402 host_flags |= FLAG_NO_PSEUDO_DMA;
403
9c3f0e2b 404 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
ca513fc9 405
8053b0ee 406 error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
0ad0eff9
FT
407 if (error)
408 goto fail_init;
cbad48de
FT
409
410 if (instance->irq != NO_IRQ) {
411 error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
412 "NCR5380", instance);
413 if (error)
414 goto fail_irq;
415 }
416
9c3f0e2b
FT
417 NCR5380_maybe_reset_bus(instance);
418
cbad48de
FT
419 error = scsi_add_host(instance, NULL);
420 if (error)
421 goto fail_host;
422
423 platform_set_drvdata(pdev, instance);
424
425 scsi_scan_host(instance);
426 return 0;
427
428fail_host:
429 if (instance->irq != NO_IRQ)
430 free_irq(instance->irq, instance);
431fail_irq:
432 NCR5380_exit(instance);
0ad0eff9 433fail_init:
cbad48de
FT
434 scsi_host_put(instance);
435 return error;
436}
437
438static int __exit mac_scsi_remove(struct platform_device *pdev)
439{
440 struct Scsi_Host *instance = platform_get_drvdata(pdev);
441
442 scsi_remove_host(instance);
443 if (instance->irq != NO_IRQ)
444 free_irq(instance->irq, instance);
445 NCR5380_exit(instance);
446 scsi_host_put(instance);
447 return 0;
448}
449
450static struct platform_driver mac_scsi_driver = {
451 .remove = __exit_p(mac_scsi_remove),
452 .driver = {
453 .name = DRV_MODULE_NAME,
cbad48de
FT
454 },
455};
1da177e4 456
cbad48de 457module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
6e9ae6d5 458
cbad48de 459MODULE_ALIAS("platform:" DRV_MODULE_NAME);
6e9ae6d5 460MODULE_LICENSE("GPL");