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