Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

Need help to get security updates for your Linux BSP?
Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Generic Macintosh NCR5380 driver
  4 *
  5 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
  6 *
  7 * Copyright 2019 Finn Thain
  8 *
  9 * derived in part from:
 10 */
 11/*
 12 * Generic Generic NCR5380 driver
 13 *
 14 * Copyright 1995, Russell King
 15 */
 16
 17#include <linux/delay.h>
 18#include <linux/types.h>
 19#include <linux/module.h>
 20#include <linux/ioport.h>
 21#include <linux/init.h>
 22#include <linux/blkdev.h>
 23#include <linux/interrupt.h>
 24#include <linux/platform_device.h>
 25
 26#include <asm/hwtest.h>
 27#include <asm/io.h>
 28#include <asm/macintosh.h>
 29#include <asm/macints.h>
 30#include <asm/setup.h>
 31
 32#include <scsi/scsi_host.h>
 33
 34/* Definitions for the core NCR5380 driver. */
 35
 36#define NCR5380_implementation_fields   int pdma_residual
 
 
 37
 38#define NCR5380_read(reg)           in_8(hostdata->io + ((reg) << 4))
 39#define NCR5380_write(reg, value)   out_8(hostdata->io + ((reg) << 4), value)
 40
 41#define NCR5380_dma_xfer_len            macscsi_dma_xfer_len
 42#define NCR5380_dma_recv_setup          macscsi_pread
 43#define NCR5380_dma_send_setup          macscsi_pwrite
 44#define NCR5380_dma_residual            macscsi_dma_residual
 45
 46#define NCR5380_intr                    macscsi_intr
 47#define NCR5380_queue_command           macscsi_queue_command
 48#define NCR5380_abort                   macscsi_abort
 49#define NCR5380_host_reset              macscsi_host_reset
 50#define NCR5380_info                    macscsi_info
 
 
 51
 52#include "NCR5380.h"
 53
 54static int setup_can_queue = -1;
 55module_param(setup_can_queue, int, 0);
 56static int setup_cmd_per_lun = -1;
 57module_param(setup_cmd_per_lun, int, 0);
 58static int setup_sg_tablesize = -1;
 59module_param(setup_sg_tablesize, int, 0);
 60static int setup_use_pdma = 512;
 61module_param(setup_use_pdma, int, 0);
 
 
 62static int setup_hostid = -1;
 63module_param(setup_hostid, int, 0);
 64static int setup_toshiba_delay = -1;
 65module_param(setup_toshiba_delay, int, 0);
 66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 67#ifndef MODULE
 68static int __init mac_scsi_setup(char *str)
 69{
 70	int ints[8];
 71
 72	(void)get_options(str, ARRAY_SIZE(ints), ints);
 73
 74	if (ints[0] < 1) {
 75		pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
 76		return 0;
 77	}
 78	if (ints[0] >= 1)
 79		setup_can_queue = ints[1];
 80	if (ints[0] >= 2)
 81		setup_cmd_per_lun = ints[2];
 82	if (ints[0] >= 3)
 83		setup_sg_tablesize = ints[3];
 84	if (ints[0] >= 4)
 85		setup_hostid = ints[4];
 86	/* ints[5] (use_tagged_queuing) is ignored */
 
 87	if (ints[0] >= 6)
 88		setup_use_pdma = ints[6];
 89	if (ints[0] >= 7)
 90		setup_toshiba_delay = ints[7];
 91	return 1;
 92}
 93
 94__setup("mac5380=", mac_scsi_setup);
 95#endif /* !MODULE */
 96
 97/*
 98 * According to "Inside Macintosh: Devices", Mac OS requires disk drivers to
 99 * specify the number of bytes between the delays expected from a SCSI target.
100 * This allows the operating system to "prevent bus errors when a target fails
101 * to deliver the next byte within the processor bus error timeout period."
102 * Linux SCSI drivers lack knowledge of the timing behaviour of SCSI targets
103 * so bus errors are unavoidable.
104 *
105 * If a MOVE.B instruction faults, we assume that zero bytes were transferred
106 * and simply retry. That assumption probably depends on target behaviour but
107 * seems to hold up okay. The NOP provides synchronization: without it the
108 * fault can sometimes occur after the program counter has moved past the
109 * offending instruction. Post-increment addressing can't be used.
110 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
112#define MOVE_BYTE(operands) \
113	asm volatile ( \
114		"1:     moveb " operands "     \n" \
115		"11:    nop                    \n" \
116		"       addq #1,%0             \n" \
117		"       subq #1,%1             \n" \
118		"40:                           \n" \
119		"                              \n" \
120		".section .fixup,\"ax\"        \n" \
121		".even                         \n" \
122		"90:    movel #1, %2           \n" \
123		"       jra 40b                \n" \
124		".previous                     \n" \
125		"                              \n" \
126		".section __ex_table,\"a\"     \n" \
127		".align  4                     \n" \
128		".long   1b,90b                \n" \
129		".long  11b,90b                \n" \
130		".previous                     \n" \
131		: "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
132
133/*
134 * If a MOVE.W (or MOVE.L) instruction faults, it cannot be retried because
135 * the residual byte count would be uncertain. In that situation the MOVE_WORD
136 * macro clears n in the fixup section to abort the transfer.
137 */
138
139#define MOVE_WORD(operands) \
140	asm volatile ( \
141		"1:     movew " operands "     \n" \
142		"11:    nop                    \n" \
143		"       subq #2,%1             \n" \
144		"40:                           \n" \
145		"                              \n" \
146		".section .fixup,\"ax\"        \n" \
147		".even                         \n" \
148		"90:    movel #0, %1           \n" \
149		"       movel #2, %2           \n" \
150		"       jra 40b                \n" \
151		".previous                     \n" \
152		"                              \n" \
153		".section __ex_table,\"a\"     \n" \
154		".align  4                     \n" \
155		".long   1b,90b                \n" \
156		".long  11b,90b                \n" \
157		".previous                     \n" \
158		: "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
159
160#define MOVE_16_WORDS(operands) \
161	asm volatile ( \
162		"1:     movew " operands "     \n" \
163		"2:     movew " operands "     \n" \
164		"3:     movew " operands "     \n" \
165		"4:     movew " operands "     \n" \
166		"5:     movew " operands "     \n" \
167		"6:     movew " operands "     \n" \
168		"7:     movew " operands "     \n" \
169		"8:     movew " operands "     \n" \
170		"9:     movew " operands "     \n" \
171		"10:    movew " operands "     \n" \
172		"11:    movew " operands "     \n" \
173		"12:    movew " operands "     \n" \
174		"13:    movew " operands "     \n" \
175		"14:    movew " operands "     \n" \
176		"15:    movew " operands "     \n" \
177		"16:    movew " operands "     \n" \
178		"17:    nop                    \n" \
179		"       subl  #32,%1           \n" \
180		"40:                           \n" \
181		"                              \n" \
182		".section .fixup,\"ax\"        \n" \
183		".even                         \n" \
184		"90:    movel #0, %1           \n" \
185		"       movel #2, %2           \n" \
186		"       jra 40b                \n" \
187		".previous                     \n" \
188		"                              \n" \
189		".section __ex_table,\"a\"     \n" \
190		".align  4                     \n" \
191		".long   1b,90b                \n" \
192		".long   2b,90b                \n" \
193		".long   3b,90b                \n" \
194		".long   4b,90b                \n" \
195		".long   5b,90b                \n" \
196		".long   6b,90b                \n" \
197		".long   7b,90b                \n" \
198		".long   8b,90b                \n" \
199		".long   9b,90b                \n" \
200		".long  10b,90b                \n" \
201		".long  11b,90b                \n" \
202		".long  12b,90b                \n" \
203		".long  13b,90b                \n" \
204		".long  14b,90b                \n" \
205		".long  15b,90b                \n" \
206		".long  16b,90b                \n" \
207		".long  17b,90b                \n" \
208		".previous                     \n" \
209		: "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
210
211#define MAC_PDMA_DELAY		32
212
213static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n)
214{
215	unsigned char *addr = start;
216	int result = 0;
217
218	if (n >= 1) {
219		MOVE_BYTE("%3@,%0@");
220		if (result)
221			goto out;
222	}
223	if (n >= 1 && ((unsigned long)addr & 1)) {
224		MOVE_BYTE("%3@,%0@");
225		if (result)
226			goto out;
 
 
 
 
 
227	}
228	while (n >= 32)
229		MOVE_16_WORDS("%3@,%0@+");
230	while (n >= 2)
231		MOVE_WORD("%3@,%0@+");
232	if (result)
233		return start - addr; /* Negated to indicate uncertain length */
234	if (n == 1)
235		MOVE_BYTE("%3@,%0@");
236out:
237	return addr - start;
238}
239
240static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n)
241{
242	unsigned char *addr = start;
243	int result = 0;
244
245	if (n >= 1) {
246		MOVE_BYTE("%0@,%3@");
247		if (result)
248			goto out;
249	}
250	if (n >= 1 && ((unsigned long)addr & 1)) {
251		MOVE_BYTE("%0@,%3@");
252		if (result)
253			goto out;
254	}
255	while (n >= 32)
256		MOVE_16_WORDS("%0@+,%3@");
257	while (n >= 2)
258		MOVE_WORD("%0@+,%3@");
259	if (result)
260		return start - addr; /* Negated to indicate uncertain length */
261	if (n == 1)
262		MOVE_BYTE("%0@,%3@");
263out:
264	return addr - start;
265}
266
267/* The "SCSI DMA" chip on the IIfx implements this register. */
268#define CTRL_REG                0x8
269#define CTRL_INTERRUPTS_ENABLE  BIT(1)
270#define CTRL_HANDSHAKE_MODE     BIT(3)
271
272static inline void write_ctrl_reg(struct NCR5380_hostdata *hostdata, u32 value)
273{
274	out_be32(hostdata->io + (CTRL_REG << 4), value);
275}
276
277static inline int macscsi_pread(struct NCR5380_hostdata *hostdata,
278                                unsigned char *dst, int len)
279{
280	u8 __iomem *s = hostdata->pdma_io + (INPUT_DATA_REG << 4);
281	unsigned char *d = dst;
282	int result = 0;
283
284	hostdata->pdma_residual = len;
285
286	while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
287	                              BASR_DRQ | BASR_PHASE_MATCH,
288	                              BASR_DRQ | BASR_PHASE_MATCH, 0)) {
289		int bytes;
290
291		if (macintosh_config->ident == MAC_MODEL_IIFX)
292			write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
293			                         CTRL_INTERRUPTS_ENABLE);
294
295		bytes = mac_pdma_recv(s, d, min(hostdata->pdma_residual, 512));
296
297		if (bytes > 0) {
298			d += bytes;
299			hostdata->pdma_residual -= bytes;
300		}
301
302		if (hostdata->pdma_residual == 0)
303			goto out;
304
305		if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
306		                           BUS_AND_STATUS_REG, BASR_ACK,
307		                           BASR_ACK, 0) < 0)
308			scmd_printk(KERN_DEBUG, hostdata->connected,
309			            "%s: !REQ and !ACK\n", __func__);
310		if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
311			goto out;
312
313		if (bytes == 0)
314			udelay(MAC_PDMA_DELAY);
315
316		if (bytes >= 0)
317			continue;
318
319		dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
320		         "%s: bus error (%d/%d)\n", __func__, d - dst, len);
321		NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
322		result = -1;
323		goto out;
324	}
325
326	scmd_printk(KERN_ERR, hostdata->connected,
327	            "%s: phase mismatch or !DRQ\n", __func__);
328	NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
329	result = -1;
330out:
331	if (macintosh_config->ident == MAC_MODEL_IIFX)
332		write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
333	return result;
334}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
336static inline int macscsi_pwrite(struct NCR5380_hostdata *hostdata,
337                                 unsigned char *src, int len)
338{
339	unsigned char *s = src;
340	u8 __iomem *d = hostdata->pdma_io + (OUTPUT_DATA_REG << 4);
341	int result = 0;
342
343	hostdata->pdma_residual = len;
344
345	while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
346	                              BASR_DRQ | BASR_PHASE_MATCH,
347	                              BASR_DRQ | BASR_PHASE_MATCH, 0)) {
348		int bytes;
349
350		if (macintosh_config->ident == MAC_MODEL_IIFX)
351			write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
352			                         CTRL_INTERRUPTS_ENABLE);
353
354		bytes = mac_pdma_send(s, d, min(hostdata->pdma_residual, 512));
355
356		if (bytes > 0) {
357			s += bytes;
358			hostdata->pdma_residual -= bytes;
359		}
360
361		if (hostdata->pdma_residual == 0) {
362			if (NCR5380_poll_politely(hostdata, TARGET_COMMAND_REG,
363			                          TCR_LAST_BYTE_SENT,
364			                          TCR_LAST_BYTE_SENT,
365			                          0) < 0) {
366				scmd_printk(KERN_ERR, hostdata->connected,
367				            "%s: Last Byte Sent timeout\n", __func__);
368				result = -1;
369			}
370			goto out;
371		}
372
373		if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
374		                           BUS_AND_STATUS_REG, BASR_ACK,
375		                           BASR_ACK, 0) < 0)
376			scmd_printk(KERN_DEBUG, hostdata->connected,
377			            "%s: !REQ and !ACK\n", __func__);
378		if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
379			goto out;
380
381		if (bytes == 0)
382			udelay(MAC_PDMA_DELAY);
383
384		if (bytes >= 0)
385			continue;
386
387		dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
388		         "%s: bus error (%d/%d)\n", __func__, s - src, len);
389		NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
390		result = -1;
391		goto out;
392	}
393
394	scmd_printk(KERN_ERR, hostdata->connected,
395	            "%s: phase mismatch or !DRQ\n", __func__);
396	NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
397	result = -1;
398out:
399	if (macintosh_config->ident == MAC_MODEL_IIFX)
400		write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
401	return result;
402}
403
404static int macscsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
405                                struct scsi_cmnd *cmd)
406{
407	int resid = NCR5380_to_ncmd(cmd)->this_residual;
408
409	if (hostdata->flags & FLAG_NO_PSEUDO_DMA || resid < setup_use_pdma)
410		return 0;
411
412	return resid;
413}
414
415static int macscsi_dma_residual(struct NCR5380_hostdata *hostdata)
416{
417	return hostdata->pdma_residual;
418}
 
419
420#include "NCR5380.c"
421
422#define DRV_MODULE_NAME         "mac_scsi"
423#define PFX                     DRV_MODULE_NAME ": "
424
425static struct scsi_host_template mac_scsi_template = {
426	.module			= THIS_MODULE,
427	.proc_name		= DRV_MODULE_NAME,
 
 
428	.name			= "Macintosh NCR5380 SCSI",
429	.info			= macscsi_info,
430	.queuecommand		= macscsi_queue_command,
431	.eh_abort_handler	= macscsi_abort,
432	.eh_host_reset_handler	= macscsi_host_reset,
433	.can_queue		= 16,
434	.this_id		= 7,
435	.sg_tablesize		= 1,
436	.cmd_per_lun		= 2,
437	.dma_boundary		= PAGE_SIZE - 1,
438	.cmd_size		= sizeof(struct NCR5380_cmd),
439	.max_sectors		= 128,
440};
441
442static int __init mac_scsi_probe(struct platform_device *pdev)
443{
444	struct Scsi_Host *instance;
445	struct NCR5380_hostdata *hostdata;
446	int error;
447	int host_flags = 0;
448	struct resource *irq, *pio_mem, *pdma_mem = NULL;
449
450	pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
451	if (!pio_mem)
452		return -ENODEV;
453
 
454	pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 
455
456	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
457
458	if (!hwreg_present((unsigned char *)pio_mem->start +
459	                   (STATUS_REG << 4))) {
460		pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
461		return -ENODEV;
462	}
463
464	if (setup_can_queue > 0)
465		mac_scsi_template.can_queue = setup_can_queue;
466	if (setup_cmd_per_lun > 0)
467		mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
468	if (setup_sg_tablesize > 0)
469		mac_scsi_template.sg_tablesize = setup_sg_tablesize;
470	if (setup_hostid >= 0)
471		mac_scsi_template.this_id = setup_hostid & 7;
 
 
472
473	instance = scsi_host_alloc(&mac_scsi_template,
474	                           sizeof(struct NCR5380_hostdata));
475	if (!instance)
476		return -ENOMEM;
477
 
478	if (irq)
479		instance->irq = irq->start;
480	else
481		instance->irq = NO_IRQ;
482
483	hostdata = shost_priv(instance);
484	hostdata->base = pio_mem->start;
485	hostdata->io = (u8 __iomem *)pio_mem->start;
486
487	if (pdma_mem && setup_use_pdma)
488		hostdata->pdma_io = (u8 __iomem *)pdma_mem->start;
489	else
490		host_flags |= FLAG_NO_PSEUDO_DMA;
491
 
 
 
492	host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
493
494	error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
495	if (error)
496		goto fail_init;
497
498	if (instance->irq != NO_IRQ) {
499		error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
500		                    "NCR5380", instance);
501		if (error)
502			goto fail_irq;
503	}
504
505	NCR5380_maybe_reset_bus(instance);
506
507	error = scsi_add_host(instance, NULL);
508	if (error)
509		goto fail_host;
510
511	platform_set_drvdata(pdev, instance);
512
513	scsi_scan_host(instance);
514	return 0;
515
516fail_host:
517	if (instance->irq != NO_IRQ)
518		free_irq(instance->irq, instance);
519fail_irq:
520	NCR5380_exit(instance);
521fail_init:
522	scsi_host_put(instance);
523	return error;
524}
525
526static void __exit mac_scsi_remove(struct platform_device *pdev)
527{
528	struct Scsi_Host *instance = platform_get_drvdata(pdev);
529
530	scsi_remove_host(instance);
531	if (instance->irq != NO_IRQ)
532		free_irq(instance->irq, instance);
533	NCR5380_exit(instance);
534	scsi_host_put(instance);
 
535}
536
537static struct platform_driver mac_scsi_driver = {
538	.remove_new = __exit_p(mac_scsi_remove),
539	.driver = {
540		.name	= DRV_MODULE_NAME,
541	},
542};
543
544module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
545
546MODULE_ALIAS("platform:" DRV_MODULE_NAME);
547MODULE_LICENSE("GPL");
v4.6
 
  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
 12 */
 13
 
 14#include <linux/types.h>
 15#include <linux/module.h>
 16#include <linux/ioport.h>
 17#include <linux/init.h>
 18#include <linux/blkdev.h>
 19#include <linux/interrupt.h>
 20#include <linux/platform_device.h>
 21
 22#include <asm/hwtest.h>
 23#include <asm/io.h>
 
 24#include <asm/macints.h>
 25#include <asm/setup.h>
 26
 27#include <scsi/scsi_host.h>
 28
 29/* Definitions for the core NCR5380 driver. */
 30
 31#define PSEUDO_DMA
 32
 33#define NCR5380_implementation_fields   unsigned char *pdma_base
 34
 35#define NCR5380_read(reg)               macscsi_read(instance, reg)
 36#define NCR5380_write(reg, value)       macscsi_write(instance, reg, value)
 37
 38#define NCR5380_pread                   macscsi_pread
 39#define NCR5380_pwrite                  macscsi_pwrite
 40#define NCR5380_dma_xfer_len(instance, cmd, phase)	(cmd->transfersize)
 
 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
 50#include "NCR5380.h"
 51
 52static int setup_can_queue = -1;
 53module_param(setup_can_queue, int, 0);
 54static int setup_cmd_per_lun = -1;
 55module_param(setup_cmd_per_lun, int, 0);
 56static int setup_sg_tablesize = -1;
 57module_param(setup_sg_tablesize, int, 0);
 58static int setup_use_pdma = -1;
 59module_param(setup_use_pdma, int, 0);
 60static int setup_use_tagged_queuing = -1;
 61module_param(setup_use_tagged_queuing, int, 0);
 62static int setup_hostid = -1;
 63module_param(setup_hostid, int, 0);
 64static int setup_toshiba_delay = -1;
 65module_param(setup_toshiba_delay, int, 0);
 66
 67/*
 68 * NCR 5380 register access functions
 69 */
 70
 71static inline char macscsi_read(struct Scsi_Host *instance, int reg)
 72{
 73	return in_8(instance->base + (reg << 4));
 74}
 75
 76static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value)
 77{
 78	out_8(instance->base + (reg << 4), value);
 79}
 80
 81#ifndef MODULE
 82static int __init mac_scsi_setup(char *str)
 83{
 84	int ints[8];
 85
 86	(void)get_options(str, ARRAY_SIZE(ints), ints);
 87
 88	if (ints[0] < 1) {
 89		pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
 90		return 0;
 91	}
 92	if (ints[0] >= 1)
 93		setup_can_queue = ints[1];
 94	if (ints[0] >= 2)
 95		setup_cmd_per_lun = ints[2];
 96	if (ints[0] >= 3)
 97		setup_sg_tablesize = ints[3];
 98	if (ints[0] >= 4)
 99		setup_hostid = ints[4];
100	if (ints[0] >= 5)
101		setup_use_tagged_queuing = ints[5];
102	if (ints[0] >= 6)
103		setup_use_pdma = ints[6];
104	if (ints[0] >= 7)
105		setup_toshiba_delay = ints[7];
106	return 1;
107}
108
109__setup("mac5380=", mac_scsi_setup);
110#endif /* !MODULE */
111
112#ifdef PSEUDO_DMA
113/* 
114   Pseudo-DMA: (Ove Edlund)
115   The code attempts to catch bus errors that occur if one for example
116   "trips over the cable".
117   XXX: Since bus errors in the PDMA routines never happen on my 
118   computer, the bus error code is untested. 
119   If the code works as intended, a bus error results in Pseudo-DMA 
120   being disabled, meaning that the driver switches to slow handshake.
121   If bus errors are NOT extremely rare, this has to be changed. 
122*/
123
124#define CP_IO_TO_MEM(s,d,len)				\
125__asm__ __volatile__					\
126    ("    cmp.w  #4,%2\n"				\
127     "    bls    8f\n"					\
128     "    move.w %1,%%d0\n"				\
129     "    neg.b  %%d0\n"				\
130     "    and.w  #3,%%d0\n"				\
131     "    sub.w  %%d0,%2\n"				\
132     "    bra    2f\n"					\
133     " 1: move.b (%0),(%1)+\n"				\
134     " 2: dbf    %%d0,1b\n"				\
135     "    move.w %2,%%d0\n"				\
136     "    lsr.w  #5,%%d0\n"				\
137     "    bra    4f\n"					\
138     " 3: move.l (%0),(%1)+\n"				\
139     "31: move.l (%0),(%1)+\n"				\
140     "32: move.l (%0),(%1)+\n"				\
141     "33: move.l (%0),(%1)+\n"				\
142     "34: move.l (%0),(%1)+\n"				\
143     "35: move.l (%0),(%1)+\n"				\
144     "36: move.l (%0),(%1)+\n"				\
145     "37: move.l (%0),(%1)+\n"				\
146     " 4: dbf    %%d0,3b\n"				\
147     "    move.w %2,%%d0\n"				\
148     "    lsr.w  #2,%%d0\n"				\
149     "    and.w  #7,%%d0\n"				\
150     "    bra    6f\n"					\
151     " 5: move.l (%0),(%1)+\n"				\
152     " 6: dbf    %%d0,5b\n"				\
153     "    and.w  #3,%2\n"				\
154     "    bra    8f\n"					\
155     " 7: move.b (%0),(%1)+\n"				\
156     " 8: dbf    %2,7b\n"				\
157     "    moveq.l #0, %2\n"				\
158     " 9: \n"						\
159     ".section .fixup,\"ax\"\n"				\
160     "    .even\n"					\
161     "90: moveq.l #1, %2\n"				\
162     "    jra 9b\n"					\
163     ".previous\n"					\
164     ".section __ex_table,\"a\"\n"			\
165     "   .align 4\n"					\
166     "   .long  1b,90b\n"				\
167     "   .long  3b,90b\n"				\
168     "   .long 31b,90b\n"				\
169     "   .long 32b,90b\n"				\
170     "   .long 33b,90b\n"				\
171     "   .long 34b,90b\n"				\
172     "   .long 35b,90b\n"				\
173     "   .long 36b,90b\n"				\
174     "   .long 37b,90b\n"				\
175     "   .long  5b,90b\n"				\
176     "   .long  7b,90b\n"				\
177     ".previous"					\
178     : "=a"(s), "=a"(d), "=d"(len)			\
179     : "0"(s), "1"(d), "2"(len)				\
180     : "d0")
181
182static int macscsi_pread(struct Scsi_Host *instance,
183                         unsigned char *dst, int len)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184{
185	struct NCR5380_hostdata *hostdata = shost_priv(instance);
186	unsigned char *d;
187	unsigned char *s;
188
189	s = hostdata->pdma_base + (INPUT_DATA_REG << 4);
190	d = dst;
191
192	/* These conditions are derived from MacOS */
193
194	while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
195	       !(NCR5380_read(STATUS_REG) & SR_REQ))
196		;
197
198	if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
199	    (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
200		pr_err("Error in macscsi_pread\n");
201		return -1;
202	}
 
 
 
 
 
 
 
 
 
 
 
203
204	CP_IO_TO_MEM(s, d, len);
 
 
 
205
206	if (len != 0) {
207		pr_notice("Bus error in macscsi_pread\n");
208		return -1;
 
 
 
 
 
 
209	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
211	return 0;
 
 
212}
213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
215#define CP_MEM_TO_IO(s,d,len)				\
216__asm__ __volatile__					\
217    ("    cmp.w  #4,%2\n"				\
218     "    bls    8f\n"					\
219     "    move.w %0,%%d0\n"				\
220     "    neg.b  %%d0\n"				\
221     "    and.w  #3,%%d0\n"				\
222     "    sub.w  %%d0,%2\n"				\
223     "    bra    2f\n"					\
224     " 1: move.b (%0)+,(%1)\n"				\
225     " 2: dbf    %%d0,1b\n"				\
226     "    move.w %2,%%d0\n"				\
227     "    lsr.w  #5,%%d0\n"				\
228     "    bra    4f\n"					\
229     " 3: move.l (%0)+,(%1)\n"				\
230     "31: move.l (%0)+,(%1)\n"				\
231     "32: move.l (%0)+,(%1)\n"				\
232     "33: move.l (%0)+,(%1)\n"				\
233     "34: move.l (%0)+,(%1)\n"				\
234     "35: move.l (%0)+,(%1)\n"				\
235     "36: move.l (%0)+,(%1)\n"				\
236     "37: move.l (%0)+,(%1)\n"				\
237     " 4: dbf    %%d0,3b\n"				\
238     "    move.w %2,%%d0\n"				\
239     "    lsr.w  #2,%%d0\n"				\
240     "    and.w  #7,%%d0\n"				\
241     "    bra    6f\n"					\
242     " 5: move.l (%0)+,(%1)\n"				\
243     " 6: dbf    %%d0,5b\n"				\
244     "    and.w  #3,%2\n"				\
245     "    bra    8f\n"					\
246     " 7: move.b (%0)+,(%1)\n"				\
247     " 8: dbf    %2,7b\n"				\
248     "    moveq.l #0, %2\n"				\
249     " 9: \n"						\
250     ".section .fixup,\"ax\"\n"				\
251     "    .even\n"					\
252     "90: moveq.l #1, %2\n"				\
253     "    jra 9b\n"					\
254     ".previous\n"					\
255     ".section __ex_table,\"a\"\n"			\
256     "   .align 4\n"					\
257     "   .long  1b,90b\n"				\
258     "   .long  3b,90b\n"				\
259     "   .long 31b,90b\n"				\
260     "   .long 32b,90b\n"				\
261     "   .long 33b,90b\n"				\
262     "   .long 34b,90b\n"				\
263     "   .long 35b,90b\n"				\
264     "   .long 36b,90b\n"				\
265     "   .long 37b,90b\n"				\
266     "   .long  5b,90b\n"				\
267     "   .long  7b,90b\n"				\
268     ".previous"					\
269     : "=a"(s), "=a"(d), "=d"(len)			\
270     : "0"(s), "1"(d), "2"(len)				\
271     : "d0")
272
273static int macscsi_pwrite(struct Scsi_Host *instance,
274                          unsigned char *src, int len)
275{
276	struct NCR5380_hostdata *hostdata = shost_priv(instance);
277	unsigned char *s;
278	unsigned char *d;
279
280	s = src;
281	d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4);
282
283	/* These conditions are derived from MacOS */
284
285	while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
286	       (!(NCR5380_read(STATUS_REG) & SR_REQ) ||
287	        (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)))
288		;
289
290	if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
291		pr_err("Error in macscsi_pwrite\n");
292		return -1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293	}
294
295	CP_MEM_TO_IO(s, d, len);
 
 
 
 
 
 
 
 
296
297	if (len != 0) {
298		pr_notice("Bus error in macscsi_pwrite\n");
299		return -1;
300	}
 
 
 
 
 
 
301
302	return 0;
 
 
303}
304#endif
305
306#include "NCR5380.c"
307
308#define DRV_MODULE_NAME         "mac_scsi"
309#define PFX                     DRV_MODULE_NAME ": "
310
311static struct scsi_host_template mac_scsi_template = {
312	.module			= THIS_MODULE,
313	.proc_name		= DRV_MODULE_NAME,
314	.show_info		= macscsi_show_info,
315	.write_info		= macscsi_write_info,
316	.name			= "Macintosh NCR5380 SCSI",
317	.info			= macscsi_info,
318	.queuecommand		= macscsi_queue_command,
319	.eh_abort_handler	= macscsi_abort,
320	.eh_bus_reset_handler	= macscsi_bus_reset,
321	.can_queue		= 16,
322	.this_id		= 7,
323	.sg_tablesize		= SG_ALL,
324	.cmd_per_lun		= 2,
325	.use_clustering		= DISABLE_CLUSTERING,
326	.cmd_size		= NCR5380_CMD_SIZE,
327	.max_sectors		= 128,
328};
329
330static int __init mac_scsi_probe(struct platform_device *pdev)
331{
332	struct Scsi_Host *instance;
 
333	int error;
334	int host_flags = 0;
335	struct resource *irq, *pio_mem, *pdma_mem = NULL;
336
337	pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
338	if (!pio_mem)
339		return -ENODEV;
340
341#ifdef PSEUDO_DMA
342	pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
343#endif
344
345	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
346
347	if (!hwreg_present((unsigned char *)pio_mem->start +
348	                   (STATUS_REG << 4))) {
349		pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
350		return -ENODEV;
351	}
352
353	if (setup_can_queue > 0)
354		mac_scsi_template.can_queue = setup_can_queue;
355	if (setup_cmd_per_lun > 0)
356		mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
357	if (setup_sg_tablesize >= 0)
358		mac_scsi_template.sg_tablesize = setup_sg_tablesize;
359	if (setup_hostid >= 0)
360		mac_scsi_template.this_id = setup_hostid & 7;
361	if (setup_use_pdma < 0)
362		setup_use_pdma = 0;
363
364	instance = scsi_host_alloc(&mac_scsi_template,
365	                           sizeof(struct NCR5380_hostdata));
366	if (!instance)
367		return -ENOMEM;
368
369	instance->base = pio_mem->start;
370	if (irq)
371		instance->irq = irq->start;
372	else
373		instance->irq = NO_IRQ;
374
375	if (pdma_mem && setup_use_pdma) {
376		struct NCR5380_hostdata *hostdata = shost_priv(instance);
 
377
378		hostdata->pdma_base = (unsigned char *)pdma_mem->start;
379	} else
 
380		host_flags |= FLAG_NO_PSEUDO_DMA;
381
382#ifdef SUPPORT_TAGS
383	host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
384#endif
385	host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
386
387	error = NCR5380_init(instance, host_flags);
388	if (error)
389		goto fail_init;
390
391	if (instance->irq != NO_IRQ) {
392		error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
393		                    "NCR5380", instance);
394		if (error)
395			goto fail_irq;
396	}
397
398	NCR5380_maybe_reset_bus(instance);
399
400	error = scsi_add_host(instance, NULL);
401	if (error)
402		goto fail_host;
403
404	platform_set_drvdata(pdev, instance);
405
406	scsi_scan_host(instance);
407	return 0;
408
409fail_host:
410	if (instance->irq != NO_IRQ)
411		free_irq(instance->irq, instance);
412fail_irq:
413	NCR5380_exit(instance);
414fail_init:
415	scsi_host_put(instance);
416	return error;
417}
418
419static int __exit mac_scsi_remove(struct platform_device *pdev)
420{
421	struct Scsi_Host *instance = platform_get_drvdata(pdev);
422
423	scsi_remove_host(instance);
424	if (instance->irq != NO_IRQ)
425		free_irq(instance->irq, instance);
426	NCR5380_exit(instance);
427	scsi_host_put(instance);
428	return 0;
429}
430
431static struct platform_driver mac_scsi_driver = {
432	.remove = __exit_p(mac_scsi_remove),
433	.driver = {
434		.name	= DRV_MODULE_NAME,
435	},
436};
437
438module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
439
440MODULE_ALIAS("platform:" DRV_MODULE_NAME);
441MODULE_LICENSE("GPL");