xref: /linux/drivers/scsi/mac_esp.c (revision 5ff328836dfde0cef9f28c8b8791a90a36d7a183)
1 /* mac_esp.c: ESP front-end for Macintosh Quadra systems.
2  *
3  * Adapted from jazz_esp.c and the old mac_esp.c.
4  *
5  * The pseudo DMA algorithm is based on the one used in NetBSD.
6  * See sys/arch/mac68k/obio/esp.c for some background information.
7  *
8  * Copyright (C) 2007-2008 Finn Thain
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/scatterlist.h>
19 #include <linux/delay.h>
20 #include <linux/io.h>
21 #include <linux/nubus.h>
22 #include <linux/slab.h>
23 
24 #include <asm/irq.h>
25 #include <asm/dma.h>
26 #include <asm/macints.h>
27 #include <asm/macintosh.h>
28 #include <asm/mac_via.h>
29 
30 #include <scsi/scsi_host.h>
31 
32 #include "esp_scsi.h"
33 
34 #define DRV_MODULE_NAME     "mac_esp"
35 #define PFX                 DRV_MODULE_NAME ": "
36 #define DRV_VERSION         "1.000"
37 #define DRV_MODULE_RELDATE  "Sept 15, 2007"
38 
39 #define MAC_ESP_IO_BASE          0x50F00000
40 #define MAC_ESP_REGS_QUADRA      (MAC_ESP_IO_BASE + 0x10000)
41 #define MAC_ESP_REGS_QUADRA2     (MAC_ESP_IO_BASE + 0xF000)
42 #define MAC_ESP_REGS_QUADRA3     (MAC_ESP_IO_BASE + 0x18000)
43 #define MAC_ESP_REGS_SPACING     0x402
44 #define MAC_ESP_PDMA_REG         0xF9800024
45 #define MAC_ESP_PDMA_REG_SPACING 0x4
46 #define MAC_ESP_PDMA_IO_OFFSET   0x100
47 
48 #define esp_read8(REG)		mac_esp_read8(esp, REG)
49 #define esp_write8(VAL, REG)	mac_esp_write8(esp, VAL, REG)
50 
51 struct mac_esp_priv {
52 	struct esp *esp;
53 	void __iomem *pdma_regs;
54 	void __iomem *pdma_io;
55 };
56 static struct esp *esp_chips[2];
57 static DEFINE_SPINLOCK(esp_chips_lock);
58 
59 #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
60 			       dev_get_drvdata((esp)->dev))
61 
62 static inline void mac_esp_write8(struct esp *esp, u8 val, unsigned long reg)
63 {
64 	nubus_writeb(val, esp->regs + reg * 16);
65 }
66 
67 static inline u8 mac_esp_read8(struct esp *esp, unsigned long reg)
68 {
69 	return nubus_readb(esp->regs + reg * 16);
70 }
71 
72 static void mac_esp_reset_dma(struct esp *esp)
73 {
74 	/* Nothing to do. */
75 }
76 
77 static void mac_esp_dma_drain(struct esp *esp)
78 {
79 	/* Nothing to do. */
80 }
81 
82 static void mac_esp_dma_invalidate(struct esp *esp)
83 {
84 	/* Nothing to do. */
85 }
86 
87 static int mac_esp_dma_error(struct esp *esp)
88 {
89 	return esp->send_cmd_error;
90 }
91 
92 static inline int mac_esp_wait_for_empty_fifo(struct esp *esp)
93 {
94 	int i = 500000;
95 
96 	do {
97 		if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES))
98 			return 0;
99 
100 		if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
101 			return 1;
102 
103 		udelay(2);
104 	} while (--i);
105 
106 	printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n",
107 	       esp_read8(ESP_STATUS));
108 	esp->send_cmd_error = 1;
109 	return 1;
110 }
111 
112 static inline int mac_esp_wait_for_dreq(struct esp *esp)
113 {
114 	struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
115 	int i = 500000;
116 
117 	do {
118 		if (mep->pdma_regs == NULL) {
119 			if (via2_scsi_drq_pending())
120 				return 0;
121 		} else {
122 			if (nubus_readl(mep->pdma_regs) & 0x200)
123 				return 0;
124 		}
125 
126 		if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
127 			return 1;
128 
129 		udelay(2);
130 	} while (--i);
131 
132 	printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n",
133 	       esp_read8(ESP_STATUS));
134 	esp->send_cmd_error = 1;
135 	return 1;
136 }
137 
138 #define MAC_ESP_PDMA_LOOP(operands) \
139 	asm volatile ( \
140 	     "       tstw %1                   \n" \
141 	     "       jbeq 20f                  \n" \
142 	     "1:     movew " operands "        \n" \
143 	     "2:     movew " operands "        \n" \
144 	     "3:     movew " operands "        \n" \
145 	     "4:     movew " operands "        \n" \
146 	     "5:     movew " operands "        \n" \
147 	     "6:     movew " operands "        \n" \
148 	     "7:     movew " operands "        \n" \
149 	     "8:     movew " operands "        \n" \
150 	     "9:     movew " operands "        \n" \
151 	     "10:    movew " operands "        \n" \
152 	     "11:    movew " operands "        \n" \
153 	     "12:    movew " operands "        \n" \
154 	     "13:    movew " operands "        \n" \
155 	     "14:    movew " operands "        \n" \
156 	     "15:    movew " operands "        \n" \
157 	     "16:    movew " operands "        \n" \
158 	     "       subqw #1,%1               \n" \
159 	     "       jbne 1b                   \n" \
160 	     "20:    tstw %2                   \n" \
161 	     "       jbeq 30f                  \n" \
162 	     "21:    movew " operands "        \n" \
163 	     "       subqw #1,%2               \n" \
164 	     "       jbne 21b                  \n" \
165 	     "30:    tstw %3                   \n" \
166 	     "       jbeq 40f                  \n" \
167 	     "31:    moveb " operands "        \n" \
168 	     "32:    nop                       \n" \
169 	     "40:                              \n" \
170 	     "                                 \n" \
171 	     "       .section __ex_table,\"a\" \n" \
172 	     "       .align  4                 \n" \
173 	     "       .long   1b,40b            \n" \
174 	     "       .long   2b,40b            \n" \
175 	     "       .long   3b,40b            \n" \
176 	     "       .long   4b,40b            \n" \
177 	     "       .long   5b,40b            \n" \
178 	     "       .long   6b,40b            \n" \
179 	     "       .long   7b,40b            \n" \
180 	     "       .long   8b,40b            \n" \
181 	     "       .long   9b,40b            \n" \
182 	     "       .long  10b,40b            \n" \
183 	     "       .long  11b,40b            \n" \
184 	     "       .long  12b,40b            \n" \
185 	     "       .long  13b,40b            \n" \
186 	     "       .long  14b,40b            \n" \
187 	     "       .long  15b,40b            \n" \
188 	     "       .long  16b,40b            \n" \
189 	     "       .long  21b,40b            \n" \
190 	     "       .long  31b,40b            \n" \
191 	     "       .long  32b,40b            \n" \
192 	     "       .previous                 \n" \
193 	     : "+a" (addr), "+r" (count32), "+r" (count2) \
194 	     : "g" (count1), "a" (mep->pdma_io))
195 
196 static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count,
197 				  u32 dma_count, int write, u8 cmd)
198 {
199 	struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
200 
201 	esp->send_cmd_error = 0;
202 
203 	if (!write)
204 		scsi_esp_cmd(esp, ESP_CMD_FLUSH);
205 
206 	esp_write8((esp_count >> 0) & 0xFF, ESP_TCLOW);
207 	esp_write8((esp_count >> 8) & 0xFF, ESP_TCMED);
208 
209 	scsi_esp_cmd(esp, cmd);
210 
211 	do {
212 		unsigned int count32 = esp_count >> 5;
213 		unsigned int count2 = (esp_count & 0x1F) >> 1;
214 		unsigned int count1 = esp_count & 1;
215 		unsigned int start_addr = addr;
216 
217 		if (mac_esp_wait_for_dreq(esp))
218 			break;
219 
220 		if (write) {
221 			MAC_ESP_PDMA_LOOP("%4@,%0@+");
222 
223 			esp_count -= addr - start_addr;
224 		} else {
225 			unsigned int n;
226 
227 			MAC_ESP_PDMA_LOOP("%0@+,%4@");
228 
229 			if (mac_esp_wait_for_empty_fifo(esp))
230 				break;
231 
232 			n = (esp_read8(ESP_TCMED) << 8) + esp_read8(ESP_TCLOW);
233 			addr = start_addr + esp_count - n;
234 			esp_count = n;
235 		}
236 	} while (esp_count);
237 }
238 
239 static int mac_esp_irq_pending(struct esp *esp)
240 {
241 	if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
242 		return 1;
243 	return 0;
244 }
245 
246 static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
247 {
248 	return dma_len > 0xFFFF ? 0xFFFF : dma_len;
249 }
250 
251 static irqreturn_t mac_scsi_esp_intr(int irq, void *dev_id)
252 {
253 	int got_intr;
254 
255 	/*
256 	 * This is an edge triggered IRQ, so we have to be careful to
257 	 * avoid missing a transition when it is shared by two ESP devices.
258 	 */
259 
260 	do {
261 		got_intr = 0;
262 		if (esp_chips[0] &&
263 		    (mac_esp_read8(esp_chips[0], ESP_STATUS) & ESP_STAT_INTR)) {
264 			(void)scsi_esp_intr(irq, esp_chips[0]);
265 			got_intr = 1;
266 		}
267 		if (esp_chips[1] &&
268 		    (mac_esp_read8(esp_chips[1], ESP_STATUS) & ESP_STAT_INTR)) {
269 			(void)scsi_esp_intr(irq, esp_chips[1]);
270 			got_intr = 1;
271 		}
272 	} while (got_intr);
273 
274 	return IRQ_HANDLED;
275 }
276 
277 static struct esp_driver_ops mac_esp_ops = {
278 	.esp_write8       = mac_esp_write8,
279 	.esp_read8        = mac_esp_read8,
280 	.irq_pending      = mac_esp_irq_pending,
281 	.dma_length_limit = mac_esp_dma_length_limit,
282 	.reset_dma        = mac_esp_reset_dma,
283 	.dma_drain        = mac_esp_dma_drain,
284 	.dma_invalidate   = mac_esp_dma_invalidate,
285 	.send_dma_cmd     = mac_esp_send_pdma_cmd,
286 	.dma_error        = mac_esp_dma_error,
287 };
288 
289 static int esp_mac_probe(struct platform_device *dev)
290 {
291 	struct scsi_host_template *tpnt = &scsi_esp_template;
292 	struct Scsi_Host *host;
293 	struct esp *esp;
294 	int err;
295 	struct mac_esp_priv *mep;
296 
297 	if (!MACH_IS_MAC)
298 		return -ENODEV;
299 
300 	if (dev->id > 1)
301 		return -ENODEV;
302 
303 	host = scsi_host_alloc(tpnt, sizeof(struct esp));
304 
305 	err = -ENOMEM;
306 	if (!host)
307 		goto fail;
308 
309 	host->max_id = 8;
310 	host->dma_boundary = PAGE_SIZE - 1;
311 	esp = shost_priv(host);
312 
313 	esp->host = host;
314 	esp->dev = &dev->dev;
315 
316 	esp->command_block = kzalloc(16, GFP_KERNEL);
317 	if (!esp->command_block)
318 		goto fail_unlink;
319 	esp->command_block_dma = (dma_addr_t)esp->command_block;
320 
321 	esp->scsi_id = 7;
322 	host->this_id = esp->scsi_id;
323 	esp->scsi_id_mask = 1 << esp->scsi_id;
324 
325 	mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL);
326 	if (!mep)
327 		goto fail_free_command_block;
328 	mep->esp = esp;
329 	platform_set_drvdata(dev, mep);
330 
331 	switch (macintosh_config->scsi_type) {
332 	case MAC_SCSI_QUADRA:
333 		esp->cfreq     = 16500000;
334 		esp->regs      = (void __iomem *)MAC_ESP_REGS_QUADRA;
335 		mep->pdma_io   = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
336 		mep->pdma_regs = NULL;
337 		break;
338 	case MAC_SCSI_QUADRA2:
339 		esp->cfreq     = 25000000;
340 		esp->regs      = (void __iomem *)(MAC_ESP_REGS_QUADRA2 +
341 				 dev->id * MAC_ESP_REGS_SPACING);
342 		mep->pdma_io   = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
343 		mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG +
344 				 dev->id * MAC_ESP_PDMA_REG_SPACING);
345 		nubus_writel(0x1d1, mep->pdma_regs);
346 		break;
347 	case MAC_SCSI_QUADRA3:
348 		/* These quadras have a real DMA controller (the PSC) but we
349 		 * don't know how to drive it so we must use PIO instead.
350 		 */
351 		esp->cfreq     = 25000000;
352 		esp->regs      = (void __iomem *)MAC_ESP_REGS_QUADRA3;
353 		mep->pdma_io   = NULL;
354 		mep->pdma_regs = NULL;
355 		break;
356 	}
357 	esp->fifo_reg = esp->regs + ESP_FDATA * 16;
358 
359 	esp->ops = &mac_esp_ops;
360 	esp->flags = ESP_FLAG_NO_DMA_MAP;
361 	if (mep->pdma_io == NULL) {
362 		printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id);
363 		esp_write8(0, ESP_TCLOW);
364 		esp_write8(0, ESP_TCMED);
365 		esp->flags |= ESP_FLAG_DISABLE_SYNC;
366 		mac_esp_ops.send_dma_cmd = esp_send_pio_cmd;
367 	} else {
368 		printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id);
369 	}
370 
371 	host->irq = IRQ_MAC_SCSI;
372 
373 	/* The request_irq() call is intended to succeed for the first device
374 	 * and fail for the second device.
375 	 */
376 	err = request_irq(host->irq, mac_scsi_esp_intr, 0, "ESP", NULL);
377 	spin_lock(&esp_chips_lock);
378 	if (err < 0 && esp_chips[!dev->id] == NULL) {
379 		spin_unlock(&esp_chips_lock);
380 		goto fail_free_priv;
381 	}
382 	esp_chips[dev->id] = esp;
383 	spin_unlock(&esp_chips_lock);
384 
385 	err = scsi_esp_register(esp);
386 	if (err)
387 		goto fail_free_irq;
388 
389 	return 0;
390 
391 fail_free_irq:
392 	spin_lock(&esp_chips_lock);
393 	esp_chips[dev->id] = NULL;
394 	if (esp_chips[!dev->id] == NULL) {
395 		spin_unlock(&esp_chips_lock);
396 		free_irq(host->irq, NULL);
397 	} else
398 		spin_unlock(&esp_chips_lock);
399 fail_free_priv:
400 	kfree(mep);
401 fail_free_command_block:
402 	kfree(esp->command_block);
403 fail_unlink:
404 	scsi_host_put(host);
405 fail:
406 	return err;
407 }
408 
409 static int esp_mac_remove(struct platform_device *dev)
410 {
411 	struct mac_esp_priv *mep = platform_get_drvdata(dev);
412 	struct esp *esp = mep->esp;
413 	unsigned int irq = esp->host->irq;
414 
415 	scsi_esp_unregister(esp);
416 
417 	spin_lock(&esp_chips_lock);
418 	esp_chips[dev->id] = NULL;
419 	if (esp_chips[!dev->id] == NULL) {
420 		spin_unlock(&esp_chips_lock);
421 		free_irq(irq, NULL);
422 	} else
423 		spin_unlock(&esp_chips_lock);
424 
425 	kfree(mep);
426 
427 	kfree(esp->command_block);
428 
429 	scsi_host_put(esp->host);
430 
431 	return 0;
432 }
433 
434 static struct platform_driver esp_mac_driver = {
435 	.probe    = esp_mac_probe,
436 	.remove   = esp_mac_remove,
437 	.driver   = {
438 		.name	= DRV_MODULE_NAME,
439 	},
440 };
441 
442 static int __init mac_esp_init(void)
443 {
444 	return platform_driver_register(&esp_mac_driver);
445 }
446 
447 static void __exit mac_esp_exit(void)
448 {
449 	platform_driver_unregister(&esp_mac_driver);
450 }
451 
452 MODULE_DESCRIPTION("Mac ESP SCSI driver");
453 MODULE_AUTHOR("Finn Thain");
454 MODULE_LICENSE("GPL v2");
455 MODULE_VERSION(DRV_VERSION);
456 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
457 
458 module_init(mac_esp_init);
459 module_exit(mac_esp_exit);
460