Commit | Line | Data |
---|---|---|
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/delay.h> |
1da177e4 | 16 | #include <linux/module.h> |
1da177e4 LT |
17 | #include <linux/ioport.h> |
18 | #include <linux/init.h> | |
19 | #include <linux/blkdev.h> | |
20 | #include <linux/interrupt.h> | |
cbad48de | 21 | #include <linux/platform_device.h> |
1da177e4 | 22 | |
cbad48de | 23 | #include <asm/hwtest.h> |
1da177e4 | 24 | #include <asm/io.h> |
1da177e4 | 25 | #include <asm/macints.h> |
cbad48de | 26 | #include <asm/setup.h> |
1da177e4 | 27 | |
1da177e4 | 28 | #include <scsi/scsi_host.h> |
92de3831 FT |
29 | |
30 | /* Definitions for the core NCR5380 driver. */ | |
e744fdea | 31 | |
e744fdea BH |
32 | #define PSEUDO_DMA |
33 | ||
cbad48de | 34 | #define NCR5380_implementation_fields unsigned char *pdma_base |
92de3831 | 35 | |
54d8fe44 FT |
36 | #define NCR5380_read(reg) macscsi_read(instance, reg) |
37 | #define NCR5380_write(reg, value) macscsi_write(instance, reg, value) | |
92de3831 FT |
38 | |
39 | #define NCR5380_pread macscsi_pread | |
40 | #define NCR5380_pwrite macscsi_pwrite | |
41 | ||
42 | #define NCR5380_intr macscsi_intr | |
43 | #define NCR5380_queue_command macscsi_queue_command | |
44 | #define NCR5380_abort macscsi_abort | |
45 | #define NCR5380_bus_reset macscsi_bus_reset | |
46 | #define NCR5380_info macscsi_info | |
47 | #define NCR5380_show_info macscsi_show_info | |
48 | #define NCR5380_write_info macscsi_write_info | |
49 | ||
1da177e4 LT |
50 | #include "NCR5380.h" |
51 | ||
1da177e4 | 52 | #define RESET_BOOT |
1da177e4 | 53 | |
1da177e4 | 54 | static int setup_can_queue = -1; |
6e9ae6d5 | 55 | module_param(setup_can_queue, int, 0); |
1da177e4 | 56 | static int setup_cmd_per_lun = -1; |
6e9ae6d5 | 57 | module_param(setup_cmd_per_lun, int, 0); |
1da177e4 | 58 | static int setup_sg_tablesize = -1; |
6e9ae6d5 | 59 | module_param(setup_sg_tablesize, int, 0); |
1da177e4 | 60 | static int setup_use_pdma = -1; |
6e9ae6d5 | 61 | module_param(setup_use_pdma, int, 0); |
1da177e4 | 62 | static int setup_use_tagged_queuing = -1; |
6e9ae6d5 | 63 | module_param(setup_use_tagged_queuing, int, 0); |
1da177e4 | 64 | static int setup_hostid = -1; |
6e9ae6d5 | 65 | module_param(setup_hostid, int, 0); |
1da177e4 LT |
66 | |
67 | /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms, | |
68 | * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more | |
69 | * need ten times the standard value... */ | |
70 | #define TOSHIBA_DELAY | |
71 | ||
72 | #ifdef TOSHIBA_DELAY | |
73 | #define AFTER_RESET_DELAY (5*HZ/2) | |
74 | #else | |
75 | #define AFTER_RESET_DELAY (HZ/2) | |
76 | #endif | |
77 | ||
1da177e4 LT |
78 | /* |
79 | * NCR 5380 register access functions | |
80 | */ | |
81 | ||
cbad48de | 82 | static inline char macscsi_read(struct Scsi_Host *instance, int reg) |
1da177e4 | 83 | { |
cbad48de | 84 | return in_8(instance->base + (reg << 4)); |
1da177e4 LT |
85 | } |
86 | ||
cbad48de | 87 | static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value) |
1da177e4 | 88 | { |
cbad48de | 89 | out_8(instance->base + (reg << 4), value); |
1da177e4 | 90 | } |
1da177e4 | 91 | |
6e9ae6d5 FT |
92 | #ifndef MODULE |
93 | static int __init mac_scsi_setup(char *str) | |
94 | { | |
1da177e4 | 95 | int ints[7]; |
6e9ae6d5 FT |
96 | |
97 | (void)get_options(str, ARRAY_SIZE(ints), ints); | |
98 | ||
99 | if (ints[0] < 1 || ints[0] > 6) { | |
100 | pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>]]]]]\n"); | |
101 | return 0; | |
1da177e4 | 102 | } |
6e9ae6d5 FT |
103 | if (ints[0] >= 1) |
104 | setup_can_queue = ints[1]; | |
105 | if (ints[0] >= 2) | |
106 | setup_cmd_per_lun = ints[2]; | |
107 | if (ints[0] >= 3) | |
108 | setup_sg_tablesize = ints[3]; | |
109 | if (ints[0] >= 4) | |
110 | setup_hostid = ints[4]; | |
111 | if (ints[0] >= 5) | |
112 | setup_use_tagged_queuing = ints[5]; | |
113 | if (ints[0] >= 6) | |
1da177e4 | 114 | setup_use_pdma = ints[6]; |
1da177e4 LT |
115 | return 1; |
116 | } | |
117 | ||
118 | __setup("mac5380=", mac_scsi_setup); | |
6e9ae6d5 | 119 | #endif /* !MODULE */ |
1da177e4 | 120 | |
1da177e4 LT |
121 | #ifdef RESET_BOOT |
122 | /* | |
123 | * Our 'bus reset on boot' function | |
124 | */ | |
125 | ||
126 | static void mac_scsi_reset_boot(struct Scsi_Host *instance) | |
127 | { | |
128 | unsigned long end; | |
129 | ||
1da177e4 LT |
130 | /* |
131 | * Do a SCSI reset to clean up the bus during initialization. No messing | |
132 | * with the queues, interrupts, or locks necessary here. | |
133 | */ | |
134 | ||
135 | printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." ); | |
136 | ||
1da177e4 LT |
137 | /* get in phase */ |
138 | NCR5380_write( TARGET_COMMAND_REG, | |
139 | PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) )); | |
140 | ||
141 | /* assert RST */ | |
142 | NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST ); | |
143 | /* The min. reset hold time is 25us, so 40us should be enough */ | |
144 | udelay( 50 ); | |
145 | /* reset RST and interrupt */ | |
146 | NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE ); | |
147 | NCR5380_read( RESET_PARITY_INTERRUPT_REG ); | |
148 | ||
149 | for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); ) | |
150 | barrier(); | |
151 | ||
1da177e4 LT |
152 | printk(KERN_INFO " done\n" ); |
153 | } | |
154 | #endif | |
155 | ||
ffdede67 | 156 | #ifdef PSEUDO_DMA |
1da177e4 LT |
157 | /* |
158 | Pseudo-DMA: (Ove Edlund) | |
159 | The code attempts to catch bus errors that occur if one for example | |
160 | "trips over the cable". | |
161 | XXX: Since bus errors in the PDMA routines never happen on my | |
162 | computer, the bus error code is untested. | |
163 | If the code works as intended, a bus error results in Pseudo-DMA | |
542cb459 | 164 | being disabled, meaning that the driver switches to slow handshake. |
1da177e4 LT |
165 | If bus errors are NOT extremely rare, this has to be changed. |
166 | */ | |
167 | ||
168 | #define CP_IO_TO_MEM(s,d,len) \ | |
169 | __asm__ __volatile__ \ | |
170 | (" cmp.w #4,%2\n" \ | |
171 | " bls 8f\n" \ | |
172 | " move.w %1,%%d0\n" \ | |
173 | " neg.b %%d0\n" \ | |
174 | " and.w #3,%%d0\n" \ | |
175 | " sub.w %%d0,%2\n" \ | |
176 | " bra 2f\n" \ | |
177 | " 1: move.b (%0),(%1)+\n" \ | |
178 | " 2: dbf %%d0,1b\n" \ | |
179 | " move.w %2,%%d0\n" \ | |
180 | " lsr.w #5,%%d0\n" \ | |
181 | " bra 4f\n" \ | |
182 | " 3: move.l (%0),(%1)+\n" \ | |
183 | "31: move.l (%0),(%1)+\n" \ | |
184 | "32: move.l (%0),(%1)+\n" \ | |
185 | "33: move.l (%0),(%1)+\n" \ | |
186 | "34: move.l (%0),(%1)+\n" \ | |
187 | "35: move.l (%0),(%1)+\n" \ | |
188 | "36: move.l (%0),(%1)+\n" \ | |
189 | "37: move.l (%0),(%1)+\n" \ | |
190 | " 4: dbf %%d0,3b\n" \ | |
191 | " move.w %2,%%d0\n" \ | |
192 | " lsr.w #2,%%d0\n" \ | |
193 | " and.w #7,%%d0\n" \ | |
194 | " bra 6f\n" \ | |
195 | " 5: move.l (%0),(%1)+\n" \ | |
196 | " 6: dbf %%d0,5b\n" \ | |
197 | " and.w #3,%2\n" \ | |
198 | " bra 8f\n" \ | |
199 | " 7: move.b (%0),(%1)+\n" \ | |
200 | " 8: dbf %2,7b\n" \ | |
201 | " moveq.l #0, %2\n" \ | |
202 | " 9: \n" \ | |
203 | ".section .fixup,\"ax\"\n" \ | |
204 | " .even\n" \ | |
205 | "90: moveq.l #1, %2\n" \ | |
206 | " jra 9b\n" \ | |
207 | ".previous\n" \ | |
208 | ".section __ex_table,\"a\"\n" \ | |
209 | " .align 4\n" \ | |
210 | " .long 1b,90b\n" \ | |
211 | " .long 3b,90b\n" \ | |
212 | " .long 31b,90b\n" \ | |
213 | " .long 32b,90b\n" \ | |
214 | " .long 33b,90b\n" \ | |
215 | " .long 34b,90b\n" \ | |
216 | " .long 35b,90b\n" \ | |
217 | " .long 36b,90b\n" \ | |
218 | " .long 37b,90b\n" \ | |
219 | " .long 5b,90b\n" \ | |
220 | " .long 7b,90b\n" \ | |
221 | ".previous" \ | |
222 | : "=a"(s), "=a"(d), "=d"(len) \ | |
223 | : "0"(s), "1"(d), "2"(len) \ | |
224 | : "d0") | |
225 | ||
ffdede67 FT |
226 | static int macscsi_pread(struct Scsi_Host *instance, |
227 | unsigned char *dst, int len) | |
1da177e4 | 228 | { |
cbad48de | 229 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
ffdede67 FT |
230 | unsigned char *d; |
231 | unsigned char *s; | |
232 | ||
cbad48de | 233 | s = hostdata->pdma_base + (INPUT_DATA_REG << 4); |
ffdede67 FT |
234 | d = dst; |
235 | ||
236 | /* These conditions are derived from MacOS */ | |
237 | ||
238 | while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && | |
239 | !(NCR5380_read(STATUS_REG) & SR_REQ)) | |
240 | ; | |
241 | ||
242 | if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && | |
243 | (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) { | |
244 | pr_err("Error in macscsi_pread\n"); | |
245 | return -1; | |
246 | } | |
247 | ||
248 | CP_IO_TO_MEM(s, d, len); | |
249 | ||
250 | if (len != 0) { | |
251 | pr_notice("Bus error in macscsi_pread\n"); | |
252 | return -1; | |
253 | } | |
254 | ||
255 | return 0; | |
1da177e4 LT |
256 | } |
257 | ||
258 | ||
259 | #define CP_MEM_TO_IO(s,d,len) \ | |
260 | __asm__ __volatile__ \ | |
261 | (" cmp.w #4,%2\n" \ | |
262 | " bls 8f\n" \ | |
263 | " move.w %0,%%d0\n" \ | |
264 | " neg.b %%d0\n" \ | |
265 | " and.w #3,%%d0\n" \ | |
266 | " sub.w %%d0,%2\n" \ | |
267 | " bra 2f\n" \ | |
268 | " 1: move.b (%0)+,(%1)\n" \ | |
269 | " 2: dbf %%d0,1b\n" \ | |
270 | " move.w %2,%%d0\n" \ | |
271 | " lsr.w #5,%%d0\n" \ | |
272 | " bra 4f\n" \ | |
273 | " 3: move.l (%0)+,(%1)\n" \ | |
274 | "31: move.l (%0)+,(%1)\n" \ | |
275 | "32: move.l (%0)+,(%1)\n" \ | |
276 | "33: move.l (%0)+,(%1)\n" \ | |
277 | "34: move.l (%0)+,(%1)\n" \ | |
278 | "35: move.l (%0)+,(%1)\n" \ | |
279 | "36: move.l (%0)+,(%1)\n" \ | |
280 | "37: move.l (%0)+,(%1)\n" \ | |
281 | " 4: dbf %%d0,3b\n" \ | |
282 | " move.w %2,%%d0\n" \ | |
283 | " lsr.w #2,%%d0\n" \ | |
284 | " and.w #7,%%d0\n" \ | |
285 | " bra 6f\n" \ | |
286 | " 5: move.l (%0)+,(%1)\n" \ | |
287 | " 6: dbf %%d0,5b\n" \ | |
288 | " and.w #3,%2\n" \ | |
289 | " bra 8f\n" \ | |
290 | " 7: move.b (%0)+,(%1)\n" \ | |
291 | " 8: dbf %2,7b\n" \ | |
292 | " moveq.l #0, %2\n" \ | |
293 | " 9: \n" \ | |
294 | ".section .fixup,\"ax\"\n" \ | |
295 | " .even\n" \ | |
296 | "90: moveq.l #1, %2\n" \ | |
297 | " jra 9b\n" \ | |
298 | ".previous\n" \ | |
299 | ".section __ex_table,\"a\"\n" \ | |
300 | " .align 4\n" \ | |
301 | " .long 1b,90b\n" \ | |
302 | " .long 3b,90b\n" \ | |
303 | " .long 31b,90b\n" \ | |
304 | " .long 32b,90b\n" \ | |
305 | " .long 33b,90b\n" \ | |
306 | " .long 34b,90b\n" \ | |
307 | " .long 35b,90b\n" \ | |
308 | " .long 36b,90b\n" \ | |
309 | " .long 37b,90b\n" \ | |
310 | " .long 5b,90b\n" \ | |
311 | " .long 7b,90b\n" \ | |
312 | ".previous" \ | |
313 | : "=a"(s), "=a"(d), "=d"(len) \ | |
314 | : "0"(s), "1"(d), "2"(len) \ | |
315 | : "d0") | |
316 | ||
ffdede67 FT |
317 | static int macscsi_pwrite(struct Scsi_Host *instance, |
318 | unsigned char *src, int len) | |
1da177e4 | 319 | { |
cbad48de | 320 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
ffdede67 FT |
321 | unsigned char *s; |
322 | unsigned char *d; | |
1da177e4 | 323 | |
ffdede67 | 324 | s = src; |
cbad48de | 325 | d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4); |
ffdede67 FT |
326 | |
327 | /* These conditions are derived from MacOS */ | |
328 | ||
329 | while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && | |
330 | (!(NCR5380_read(STATUS_REG) & SR_REQ) || | |
331 | (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))) | |
332 | ; | |
333 | ||
334 | if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) { | |
335 | pr_err("Error in macscsi_pwrite\n"); | |
336 | return -1; | |
337 | } | |
338 | ||
339 | CP_MEM_TO_IO(s, d, len); | |
340 | ||
341 | if (len != 0) { | |
342 | pr_notice("Bus error in macscsi_pwrite\n"); | |
343 | return -1; | |
344 | } | |
345 | ||
346 | return 0; | |
347 | } | |
348 | #endif | |
1da177e4 | 349 | |
1da177e4 LT |
350 | #include "NCR5380.c" |
351 | ||
cbad48de FT |
352 | #define DRV_MODULE_NAME "mac_scsi" |
353 | #define PFX DRV_MODULE_NAME ": " | |
354 | ||
355 | static struct scsi_host_template mac_scsi_template = { | |
356 | .module = THIS_MODULE, | |
357 | .proc_name = DRV_MODULE_NAME, | |
dd7ab71b AV |
358 | .show_info = macscsi_show_info, |
359 | .write_info = macscsi_write_info, | |
1da177e4 | 360 | .name = "Macintosh NCR5380 SCSI", |
1da177e4 LT |
361 | .info = macscsi_info, |
362 | .queuecommand = macscsi_queue_command, | |
363 | .eh_abort_handler = macscsi_abort, | |
364 | .eh_bus_reset_handler = macscsi_bus_reset, | |
d572f65f | 365 | .can_queue = 16, |
1da177e4 LT |
366 | .this_id = 7, |
367 | .sg_tablesize = SG_ALL, | |
d572f65f | 368 | .cmd_per_lun = 2, |
1da177e4 LT |
369 | .use_clustering = DISABLE_CLUSTERING |
370 | }; | |
371 | ||
cbad48de FT |
372 | static int __init mac_scsi_probe(struct platform_device *pdev) |
373 | { | |
374 | struct Scsi_Host *instance; | |
375 | int error; | |
376 | int host_flags = 0; | |
377 | struct resource *irq, *pio_mem, *pdma_mem = NULL; | |
378 | ||
379 | pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
380 | if (!pio_mem) | |
381 | return -ENODEV; | |
382 | ||
383 | #ifdef PSEUDO_DMA | |
384 | pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); | |
385 | #endif | |
386 | ||
387 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
388 | ||
389 | if (!hwreg_present((unsigned char *)pio_mem->start + | |
390 | (STATUS_REG << 4))) { | |
391 | pr_info(PFX "no device detected at %pap\n", &pio_mem->start); | |
392 | return -ENODEV; | |
393 | } | |
394 | ||
395 | if (setup_can_queue > 0) | |
396 | mac_scsi_template.can_queue = setup_can_queue; | |
397 | if (setup_cmd_per_lun > 0) | |
398 | mac_scsi_template.cmd_per_lun = setup_cmd_per_lun; | |
399 | if (setup_sg_tablesize >= 0) | |
400 | mac_scsi_template.sg_tablesize = setup_sg_tablesize; | |
401 | if (setup_hostid >= 0) | |
402 | mac_scsi_template.this_id = setup_hostid & 7; | |
cbad48de FT |
403 | if (setup_use_pdma < 0) |
404 | setup_use_pdma = 0; | |
405 | ||
406 | instance = scsi_host_alloc(&mac_scsi_template, | |
407 | sizeof(struct NCR5380_hostdata)); | |
408 | if (!instance) | |
409 | return -ENOMEM; | |
410 | ||
411 | instance->base = pio_mem->start; | |
412 | if (irq) | |
413 | instance->irq = irq->start; | |
414 | else | |
415 | instance->irq = NO_IRQ; | |
416 | ||
417 | if (pdma_mem && setup_use_pdma) { | |
418 | struct NCR5380_hostdata *hostdata = shost_priv(instance); | |
419 | ||
420 | hostdata->pdma_base = (unsigned char *)pdma_mem->start; | |
421 | } else | |
422 | host_flags |= FLAG_NO_PSEUDO_DMA; | |
423 | ||
424 | #ifdef RESET_BOOT | |
425 | mac_scsi_reset_boot(instance); | |
426 | #endif | |
427 | ||
ca513fc9 FT |
428 | #ifdef SUPPORT_TAGS |
429 | host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0; | |
430 | #endif | |
431 | ||
cbad48de FT |
432 | NCR5380_init(instance, host_flags); |
433 | ||
434 | if (instance->irq != NO_IRQ) { | |
435 | error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED, | |
436 | "NCR5380", instance); | |
437 | if (error) | |
438 | goto fail_irq; | |
439 | } | |
440 | ||
441 | error = scsi_add_host(instance, NULL); | |
442 | if (error) | |
443 | goto fail_host; | |
444 | ||
445 | platform_set_drvdata(pdev, instance); | |
446 | ||
447 | scsi_scan_host(instance); | |
448 | return 0; | |
449 | ||
450 | fail_host: | |
451 | if (instance->irq != NO_IRQ) | |
452 | free_irq(instance->irq, instance); | |
453 | fail_irq: | |
454 | NCR5380_exit(instance); | |
455 | scsi_host_put(instance); | |
456 | return error; | |
457 | } | |
458 | ||
459 | static int __exit mac_scsi_remove(struct platform_device *pdev) | |
460 | { | |
461 | struct Scsi_Host *instance = platform_get_drvdata(pdev); | |
462 | ||
463 | scsi_remove_host(instance); | |
464 | if (instance->irq != NO_IRQ) | |
465 | free_irq(instance->irq, instance); | |
466 | NCR5380_exit(instance); | |
467 | scsi_host_put(instance); | |
468 | return 0; | |
469 | } | |
470 | ||
471 | static struct platform_driver mac_scsi_driver = { | |
472 | .remove = __exit_p(mac_scsi_remove), | |
473 | .driver = { | |
474 | .name = DRV_MODULE_NAME, | |
cbad48de FT |
475 | }, |
476 | }; | |
1da177e4 | 477 | |
cbad48de | 478 | module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe); |
6e9ae6d5 | 479 | |
cbad48de | 480 | MODULE_ALIAS("platform:" DRV_MODULE_NAME); |
6e9ae6d5 | 481 | MODULE_LICENSE("GPL"); |