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
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>
21 #include <linux/nubus.h>
22 #include <linux/slab.h>
26 #include <asm/macints.h>
27 #include <asm/macintosh.h>
28 #include <asm/mac_via.h>
30 #include <scsi/scsi_host.h>
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"
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
48 #define esp_read8(REG) mac_esp_read8(esp, REG)
49 #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG)
53 void __iomem
*pdma_regs
;
54 void __iomem
*pdma_io
;
56 static struct esp
*esp_chips
[2];
57 static DEFINE_SPINLOCK(esp_chips_lock
);
59 #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
60 dev_get_drvdata((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 static void mac_esp_reset_dma(struct esp
*esp
)
77 static void mac_esp_dma_drain(struct esp
*esp
)
82 static void mac_esp_dma_invalidate(struct esp
*esp
)
87 static int mac_esp_dma_error(struct esp
*esp
)
89 return esp
->send_cmd_error
;
92 static inline int mac_esp_wait_for_empty_fifo(struct esp
*esp
)
97 if (!(esp_read8(ESP_FFLAGS
) & ESP_FF_FBYTES
))
100 if (esp_read8(ESP_STATUS
) & ESP_STAT_INTR
)
106 printk(KERN_ERR PFX
"FIFO is not empty (sreg %02x)\n",
107 esp_read8(ESP_STATUS
));
108 esp
->send_cmd_error
= 1;
112 static inline int mac_esp_wait_for_dreq(struct esp
*esp
)
114 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
118 if (mep
->pdma_regs
== NULL
) {
119 if (via2_scsi_drq_pending())
122 if (nubus_readl(mep
->pdma_regs
) & 0x200)
126 if (esp_read8(ESP_STATUS
) & ESP_STAT_INTR
)
132 printk(KERN_ERR PFX
"PDMA timeout (sreg %02x)\n",
133 esp_read8(ESP_STATUS
));
134 esp
->send_cmd_error
= 1;
138 #define MAC_ESP_PDMA_LOOP(operands) \
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" \
162 "21: movew " operands " \n" \
167 "31: moveb " operands " \n" \
171 " .section __ex_table,\"a\" \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" \
193 : "+a" (addr), "+r" (count32), "+r" (count2) \
194 : "g" (count1), "a" (mep->pdma_io))
196 static void mac_esp_send_pdma_cmd(struct esp
*esp
, u32 addr
, u32 esp_count
,
197 u32 dma_count
, int write
, u8 cmd
)
199 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
201 esp
->send_cmd_error
= 0;
204 scsi_esp_cmd(esp
, ESP_CMD_FLUSH
);
206 esp_write8((esp_count
>> 0) & 0xFF, ESP_TCLOW
);
207 esp_write8((esp_count
>> 8) & 0xFF, ESP_TCMED
);
209 scsi_esp_cmd(esp
, cmd
);
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
;
217 if (mac_esp_wait_for_dreq(esp
))
221 MAC_ESP_PDMA_LOOP("%4@,%0@+");
223 esp_count
-= addr
- start_addr
;
227 MAC_ESP_PDMA_LOOP("%0@+,%4@");
229 if (mac_esp_wait_for_empty_fifo(esp
))
232 n
= (esp_read8(ESP_TCMED
) << 8) + esp_read8(ESP_TCLOW
);
233 addr
= start_addr
+ esp_count
- n
;
239 static int mac_esp_irq_pending(struct esp
*esp
)
241 if (esp_read8(ESP_STATUS
) & ESP_STAT_INTR
)
246 static u32
mac_esp_dma_length_limit(struct esp
*esp
, u32 dma_addr
, u32 dma_len
)
248 return dma_len
> 0xFFFF ? 0xFFFF : dma_len
;
251 static irqreturn_t
mac_scsi_esp_intr(int irq
, void *dev_id
)
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.
263 (mac_esp_read8(esp_chips
[0], ESP_STATUS
) & ESP_STAT_INTR
)) {
264 (void)scsi_esp_intr(irq
, esp_chips
[0]);
268 (mac_esp_read8(esp_chips
[1], ESP_STATUS
) & ESP_STAT_INTR
)) {
269 (void)scsi_esp_intr(irq
, esp_chips
[1]);
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
,
289 static int esp_mac_probe(struct platform_device
*dev
)
291 struct scsi_host_template
*tpnt
= &scsi_esp_template
;
292 struct Scsi_Host
*host
;
295 struct mac_esp_priv
*mep
;
303 host
= scsi_host_alloc(tpnt
, sizeof(struct esp
));
310 host
->dma_boundary
= PAGE_SIZE
- 1;
311 esp
= shost_priv(host
);
314 esp
->dev
= &dev
->dev
;
316 esp
->command_block
= kzalloc(16, GFP_KERNEL
);
317 if (!esp
->command_block
)
319 esp
->command_block_dma
= (dma_addr_t
)esp
->command_block
;
322 host
->this_id
= esp
->scsi_id
;
323 esp
->scsi_id_mask
= 1 << esp
->scsi_id
;
325 mep
= kzalloc(sizeof(struct mac_esp_priv
), GFP_KERNEL
);
327 goto fail_free_command_block
;
329 platform_set_drvdata(dev
, mep
);
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
;
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
);
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.
351 esp
->cfreq
= 25000000;
352 esp
->regs
= (void __iomem
*)MAC_ESP_REGS_QUADRA3
;
354 mep
->pdma_regs
= NULL
;
357 esp
->fifo_reg
= esp
->regs
+ ESP_FDATA
* 16;
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
;
368 printk(KERN_INFO PFX
"using PDMA for controller %d\n", dev
->id
);
371 host
->irq
= IRQ_MAC_SCSI
;
373 /* The request_irq() call is intended to succeed for the first device
374 * and fail for the second device.
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
);
382 esp_chips
[dev
->id
] = esp
;
383 spin_unlock(&esp_chips_lock
);
385 err
= scsi_esp_register(esp
);
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
);
398 spin_unlock(&esp_chips_lock
);
401 fail_free_command_block
:
402 kfree(esp
->command_block
);
409 static int esp_mac_remove(struct platform_device
*dev
)
411 struct mac_esp_priv
*mep
= platform_get_drvdata(dev
);
412 struct esp
*esp
= mep
->esp
;
413 unsigned int irq
= esp
->host
->irq
;
415 scsi_esp_unregister(esp
);
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
);
423 spin_unlock(&esp_chips_lock
);
427 kfree(esp
->command_block
);
429 scsi_host_put(esp
->host
);
434 static struct platform_driver esp_mac_driver
= {
435 .probe
= esp_mac_probe
,
436 .remove
= esp_mac_remove
,
438 .name
= DRV_MODULE_NAME
,
442 static int __init
mac_esp_init(void)
444 return platform_driver_register(&esp_mac_driver
);
447 static void __exit
mac_esp_exit(void)
449 platform_driver_unregister(&esp_mac_driver
);
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
);
458 module_init(mac_esp_init
);
459 module_exit(mac_esp_exit
);