[SCSI] m68k: mac_esp asm fix
[linux-2.6/mini2440.git] / drivers / scsi / mac_esp.c
bloba99f9ce6be55e5256343925995d837039699d875
1 /* mac_esp.c: ESP front-end for Macintosh Quadra systems.
3 * Adapted from jazz_esp.c and the old mac_esp.c.
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.
8 * Copyright (C) 2007-2008 Finn Thain
9 */
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>
23 #include <asm/irq.h>
24 #include <asm/dma.h>
26 #include <asm/macints.h>
27 #include <asm/macintosh.h>
29 #include <scsi/scsi_host.h>
31 #include "esp_scsi.h"
33 #define DRV_MODULE_NAME "mac_esp"
34 #define PFX DRV_MODULE_NAME ": "
35 #define DRV_VERSION "1.000"
36 #define DRV_MODULE_RELDATE "Sept 15, 2007"
38 #define MAC_ESP_IO_BASE 0x50F00000
39 #define MAC_ESP_REGS_QUADRA (MAC_ESP_IO_BASE + 0x10000)
40 #define MAC_ESP_REGS_QUADRA2 (MAC_ESP_IO_BASE + 0xF000)
41 #define MAC_ESP_REGS_QUADRA3 (MAC_ESP_IO_BASE + 0x18000)
42 #define MAC_ESP_REGS_SPACING 0x402
43 #define MAC_ESP_PDMA_REG 0xF9800024
44 #define MAC_ESP_PDMA_REG_SPACING 0x4
45 #define MAC_ESP_PDMA_IO_OFFSET 0x100
47 #define esp_read8(REG) mac_esp_read8(esp, REG)
48 #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG)
50 struct mac_esp_priv {
51 struct esp *esp;
52 void __iomem *pdma_regs;
53 void __iomem *pdma_io;
54 int error;
56 static struct platform_device *internal_esp, *external_esp;
58 #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
59 platform_get_drvdata((struct platform_device *) \
60 (esp->dev)))
62 static inline void mac_esp_write8(struct esp *esp, u8 val, unsigned long reg)
64 nubus_writeb(val, esp->regs + reg * 16);
67 static inline u8 mac_esp_read8(struct esp *esp, unsigned long reg)
69 return nubus_readb(esp->regs + reg * 16);
72 /* For pseudo DMA and PIO we need the virtual address
73 * so this address mapping is the identity mapping.
76 static dma_addr_t mac_esp_map_single(struct esp *esp, void *buf,
77 size_t sz, int dir)
79 return (dma_addr_t)buf;
82 static int mac_esp_map_sg(struct esp *esp, struct scatterlist *sg,
83 int num_sg, int dir)
85 int i;
87 for (i = 0; i < num_sg; i++)
88 sg[i].dma_address = (u32)sg_virt(&sg[i]);
89 return num_sg;
92 static void mac_esp_unmap_single(struct esp *esp, dma_addr_t addr,
93 size_t sz, int dir)
95 /* Nothing to do. */
98 static void mac_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
99 int num_sg, int dir)
101 /* Nothing to do. */
104 static void mac_esp_reset_dma(struct esp *esp)
106 /* Nothing to do. */
109 static void mac_esp_dma_drain(struct esp *esp)
111 /* Nothing to do. */
114 static void mac_esp_dma_invalidate(struct esp *esp)
116 /* Nothing to do. */
119 static int mac_esp_dma_error(struct esp *esp)
121 return MAC_ESP_GET_PRIV(esp)->error;
124 static inline int mac_esp_wait_for_empty_fifo(struct esp *esp)
126 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
127 int i = 500000;
129 do {
130 if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES))
131 return 0;
133 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
134 return 1;
136 udelay(2);
137 } while (--i);
139 printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n",
140 esp_read8(ESP_STATUS));
141 mep->error = 1;
142 return 1;
145 static inline int mac_esp_wait_for_dreq(struct esp *esp)
147 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
148 int i = 500000;
150 do {
151 if (mep->pdma_regs == NULL) {
152 if (mac_irq_pending(IRQ_MAC_SCSIDRQ))
153 return 0;
154 } else {
155 if (nubus_readl(mep->pdma_regs) & 0x200)
156 return 0;
159 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
160 return 1;
162 udelay(2);
163 } while (--i);
165 printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n",
166 esp_read8(ESP_STATUS));
167 mep->error = 1;
168 return 1;
171 #define MAC_ESP_PDMA_LOOP(operands) \
172 asm volatile ( \
173 " tstw %1 \n" \
174 " jbeq 20f \n" \
175 "1: movew " operands " \n" \
176 "2: movew " operands " \n" \
177 "3: movew " operands " \n" \
178 "4: movew " operands " \n" \
179 "5: movew " operands " \n" \
180 "6: movew " operands " \n" \
181 "7: movew " operands " \n" \
182 "8: movew " operands " \n" \
183 "9: movew " operands " \n" \
184 "10: movew " operands " \n" \
185 "11: movew " operands " \n" \
186 "12: movew " operands " \n" \
187 "13: movew " operands " \n" \
188 "14: movew " operands " \n" \
189 "15: movew " operands " \n" \
190 "16: movew " operands " \n" \
191 " subqw #1,%1 \n" \
192 " jbne 1b \n" \
193 "20: tstw %2 \n" \
194 " jbeq 30f \n" \
195 "21: movew " operands " \n" \
196 " subqw #1,%2 \n" \
197 " jbne 21b \n" \
198 "30: tstw %3 \n" \
199 " jbeq 40f \n" \
200 "31: moveb " operands " \n" \
201 "32: nop \n" \
202 "40: \n" \
203 " \n" \
204 " .section __ex_table,\"a\" \n" \
205 " .align 4 \n" \
206 " .long 1b,40b \n" \
207 " .long 2b,40b \n" \
208 " .long 3b,40b \n" \
209 " .long 4b,40b \n" \
210 " .long 5b,40b \n" \
211 " .long 6b,40b \n" \
212 " .long 7b,40b \n" \
213 " .long 8b,40b \n" \
214 " .long 9b,40b \n" \
215 " .long 10b,40b \n" \
216 " .long 11b,40b \n" \
217 " .long 12b,40b \n" \
218 " .long 13b,40b \n" \
219 " .long 14b,40b \n" \
220 " .long 15b,40b \n" \
221 " .long 16b,40b \n" \
222 " .long 21b,40b \n" \
223 " .long 31b,40b \n" \
224 " .long 32b,40b \n" \
225 " .previous \n" \
226 : "+a" (addr), "+r" (count32), "+r" (count2) \
227 : "g" (count1), "a" (mep->pdma_io))
229 static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count,
230 u32 dma_count, int write, u8 cmd)
232 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
233 unsigned long flags;
235 local_irq_save(flags);
237 mep->error = 0;
239 if (!write)
240 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
242 esp_write8((esp_count >> 0) & 0xFF, ESP_TCLOW);
243 esp_write8((esp_count >> 8) & 0xFF, ESP_TCMED);
245 scsi_esp_cmd(esp, cmd);
247 do {
248 unsigned int count32 = esp_count >> 5;
249 unsigned int count2 = (esp_count & 0x1F) >> 1;
250 unsigned int count1 = esp_count & 1;
251 unsigned int start_addr = addr;
253 if (mac_esp_wait_for_dreq(esp))
254 break;
256 if (write) {
257 MAC_ESP_PDMA_LOOP("%4@,%0@+");
259 esp_count -= addr - start_addr;
260 } else {
261 unsigned int n;
263 MAC_ESP_PDMA_LOOP("%0@+,%4@");
265 if (mac_esp_wait_for_empty_fifo(esp))
266 break;
268 n = (esp_read8(ESP_TCMED) << 8) + esp_read8(ESP_TCLOW);
269 addr = start_addr + esp_count - n;
270 esp_count = n;
272 } while (esp_count);
274 local_irq_restore(flags);
278 * Programmed IO routines follow.
281 static inline int mac_esp_wait_for_fifo(struct esp *esp)
283 int i = 500000;
285 do {
286 if (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES)
287 return 0;
289 udelay(2);
290 } while (--i);
292 printk(KERN_ERR PFX "FIFO is empty (sreg %02x)\n",
293 esp_read8(ESP_STATUS));
294 return 1;
297 static inline int mac_esp_wait_for_intr(struct esp *esp)
299 int i = 500000;
301 do {
302 esp->sreg = esp_read8(ESP_STATUS);
303 if (esp->sreg & ESP_STAT_INTR)
304 return 0;
306 udelay(2);
307 } while (--i);
309 printk(KERN_ERR PFX "IRQ timeout (sreg %02x)\n", esp->sreg);
310 return 1;
313 #define MAC_ESP_PIO_LOOP(operands, reg1) \
314 asm volatile ( \
315 "1: moveb " operands " \n" \
316 " subqw #1,%1 \n" \
317 " jbne 1b \n" \
318 : "+a" (addr), "+r" (reg1) \
319 : "a" (fifo))
321 #define MAC_ESP_PIO_FILL(operands, reg1) \
322 asm volatile ( \
323 " moveb " operands " \n" \
324 " moveb " operands " \n" \
325 " moveb " operands " \n" \
326 " moveb " operands " \n" \
327 " moveb " operands " \n" \
328 " moveb " operands " \n" \
329 " moveb " operands " \n" \
330 " moveb " operands " \n" \
331 " moveb " operands " \n" \
332 " moveb " operands " \n" \
333 " moveb " operands " \n" \
334 " moveb " operands " \n" \
335 " moveb " operands " \n" \
336 " moveb " operands " \n" \
337 " moveb " operands " \n" \
338 " moveb " operands " \n" \
339 " subqw #8,%1 \n" \
340 " subqw #8,%1 \n" \
341 : "+a" (addr), "+r" (reg1) \
342 : "a" (fifo))
344 #define MAC_ESP_FIFO_SIZE 16
346 static void mac_esp_send_pio_cmd(struct esp *esp, u32 addr, u32 esp_count,
347 u32 dma_count, int write, u8 cmd)
349 unsigned long flags;
350 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
351 u8 *fifo = esp->regs + ESP_FDATA * 16;
353 local_irq_save(flags);
355 cmd &= ~ESP_CMD_DMA;
356 mep->error = 0;
358 if (write) {
359 scsi_esp_cmd(esp, cmd);
361 if (!mac_esp_wait_for_intr(esp)) {
362 if (mac_esp_wait_for_fifo(esp))
363 esp_count = 0;
364 } else {
365 esp_count = 0;
367 } else {
368 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
370 if (esp_count >= MAC_ESP_FIFO_SIZE)
371 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
372 else
373 MAC_ESP_PIO_LOOP("%0@+,%2@", esp_count);
375 scsi_esp_cmd(esp, cmd);
378 while (esp_count) {
379 unsigned int n;
381 if (mac_esp_wait_for_intr(esp)) {
382 mep->error = 1;
383 break;
386 if (esp->sreg & ESP_STAT_SPAM) {
387 printk(KERN_ERR PFX "gross error\n");
388 mep->error = 1;
389 break;
392 n = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
394 if (write) {
395 if (n > esp_count)
396 n = esp_count;
397 esp_count -= n;
399 MAC_ESP_PIO_LOOP("%2@,%0@+", n);
401 if ((esp->sreg & ESP_STAT_PMASK) == ESP_STATP)
402 break;
404 if (esp_count) {
405 esp->ireg = esp_read8(ESP_INTRPT);
406 if (esp->ireg & ESP_INTR_DC)
407 break;
409 scsi_esp_cmd(esp, ESP_CMD_TI);
411 } else {
412 esp->ireg = esp_read8(ESP_INTRPT);
413 if (esp->ireg & ESP_INTR_DC)
414 break;
416 n = MAC_ESP_FIFO_SIZE - n;
417 if (n > esp_count)
418 n = esp_count;
420 if (n == MAC_ESP_FIFO_SIZE) {
421 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
422 } else {
423 esp_count -= n;
424 MAC_ESP_PIO_LOOP("%0@+,%2@", n);
427 scsi_esp_cmd(esp, ESP_CMD_TI);
431 local_irq_restore(flags);
434 static int mac_esp_irq_pending(struct esp *esp)
436 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
437 return 1;
438 return 0;
441 static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
443 return dma_len > 0xFFFF ? 0xFFFF : dma_len;
446 static struct esp_driver_ops mac_esp_ops = {
447 .esp_write8 = mac_esp_write8,
448 .esp_read8 = mac_esp_read8,
449 .map_single = mac_esp_map_single,
450 .map_sg = mac_esp_map_sg,
451 .unmap_single = mac_esp_unmap_single,
452 .unmap_sg = mac_esp_unmap_sg,
453 .irq_pending = mac_esp_irq_pending,
454 .dma_length_limit = mac_esp_dma_length_limit,
455 .reset_dma = mac_esp_reset_dma,
456 .dma_drain = mac_esp_dma_drain,
457 .dma_invalidate = mac_esp_dma_invalidate,
458 .send_dma_cmd = mac_esp_send_pdma_cmd,
459 .dma_error = mac_esp_dma_error,
462 static int __devinit esp_mac_probe(struct platform_device *dev)
464 struct scsi_host_template *tpnt = &scsi_esp_template;
465 struct Scsi_Host *host;
466 struct esp *esp;
467 int err;
468 int chips_present;
469 struct mac_esp_priv *mep;
471 if (!MACH_IS_MAC)
472 return -ENODEV;
474 switch (macintosh_config->scsi_type) {
475 case MAC_SCSI_QUADRA:
476 case MAC_SCSI_QUADRA3:
477 chips_present = 1;
478 break;
479 case MAC_SCSI_QUADRA2:
480 if ((macintosh_config->ident == MAC_MODEL_Q900) ||
481 (macintosh_config->ident == MAC_MODEL_Q950))
482 chips_present = 2;
483 else
484 chips_present = 1;
485 break;
486 default:
487 chips_present = 0;
490 if (dev->id + 1 > chips_present)
491 return -ENODEV;
493 host = scsi_host_alloc(tpnt, sizeof(struct esp));
495 err = -ENOMEM;
496 if (!host)
497 goto fail;
499 host->max_id = 8;
500 host->use_clustering = DISABLE_CLUSTERING;
501 esp = shost_priv(host);
503 esp->host = host;
504 esp->dev = dev;
506 esp->command_block = kzalloc(16, GFP_KERNEL);
507 if (!esp->command_block)
508 goto fail_unlink;
509 esp->command_block_dma = (dma_addr_t)esp->command_block;
511 esp->scsi_id = 7;
512 host->this_id = esp->scsi_id;
513 esp->scsi_id_mask = 1 << esp->scsi_id;
515 mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL);
516 if (!mep)
517 goto fail_free_command_block;
518 mep->esp = esp;
519 platform_set_drvdata(dev, mep);
521 switch (macintosh_config->scsi_type) {
522 case MAC_SCSI_QUADRA:
523 esp->cfreq = 16500000;
524 esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA;
525 mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
526 mep->pdma_regs = NULL;
527 break;
528 case MAC_SCSI_QUADRA2:
529 esp->cfreq = 25000000;
530 esp->regs = (void __iomem *)(MAC_ESP_REGS_QUADRA2 +
531 dev->id * MAC_ESP_REGS_SPACING);
532 mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
533 mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG +
534 dev->id * MAC_ESP_PDMA_REG_SPACING);
535 nubus_writel(0x1d1, mep->pdma_regs);
536 break;
537 case MAC_SCSI_QUADRA3:
538 /* These quadras have a real DMA controller (the PSC) but we
539 * don't know how to drive it so we must use PIO instead.
541 esp->cfreq = 25000000;
542 esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA3;
543 mep->pdma_io = NULL;
544 mep->pdma_regs = NULL;
545 break;
548 esp->ops = &mac_esp_ops;
549 if (mep->pdma_io == NULL) {
550 printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id);
551 esp_write8(0, ESP_TCLOW);
552 esp_write8(0, ESP_TCMED);
553 esp->flags = ESP_FLAG_DISABLE_SYNC;
554 mac_esp_ops.send_dma_cmd = mac_esp_send_pio_cmd;
555 } else {
556 printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id);
559 host->irq = IRQ_MAC_SCSI;
560 err = request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "Mac ESP",
561 esp);
562 if (err < 0)
563 goto fail_free_priv;
565 err = scsi_esp_register(esp, &dev->dev);
566 if (err)
567 goto fail_free_irq;
569 return 0;
571 fail_free_irq:
572 free_irq(host->irq, esp);
573 fail_free_priv:
574 kfree(mep);
575 fail_free_command_block:
576 kfree(esp->command_block);
577 fail_unlink:
578 scsi_host_put(host);
579 fail:
580 return err;
583 static int __devexit esp_mac_remove(struct platform_device *dev)
585 struct mac_esp_priv *mep = platform_get_drvdata(dev);
586 struct esp *esp = mep->esp;
587 unsigned int irq = esp->host->irq;
589 scsi_esp_unregister(esp);
591 free_irq(irq, esp);
593 kfree(mep);
595 kfree(esp->command_block);
597 scsi_host_put(esp->host);
599 return 0;
602 static struct platform_driver esp_mac_driver = {
603 .probe = esp_mac_probe,
604 .remove = __devexit_p(esp_mac_remove),
605 .driver = {
606 .name = DRV_MODULE_NAME,
610 static int __init mac_esp_init(void)
612 int err;
614 err = platform_driver_register(&esp_mac_driver);
615 if (err)
616 return err;
618 internal_esp = platform_device_alloc(DRV_MODULE_NAME, 0);
619 if (internal_esp && platform_device_add(internal_esp)) {
620 platform_device_put(internal_esp);
621 internal_esp = NULL;
624 external_esp = platform_device_alloc(DRV_MODULE_NAME, 1);
625 if (external_esp && platform_device_add(external_esp)) {
626 platform_device_put(external_esp);
627 external_esp = NULL;
630 if (internal_esp || external_esp) {
631 return 0;
632 } else {
633 platform_driver_unregister(&esp_mac_driver);
634 return -ENOMEM;
638 static void __exit mac_esp_exit(void)
640 platform_driver_unregister(&esp_mac_driver);
642 if (internal_esp) {
643 platform_device_unregister(internal_esp);
644 internal_esp = NULL;
646 if (external_esp) {
647 platform_device_unregister(external_esp);
648 external_esp = NULL;
652 MODULE_DESCRIPTION("Mac ESP SCSI driver");
653 MODULE_AUTHOR("Finn Thain <fthain@telegraphics.com.au>");
654 MODULE_LICENSE("GPL v2");
655 MODULE_VERSION(DRV_VERSION);
657 module_init(mac_esp_init);
658 module_exit(mac_esp_exit);