aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/scsi/Kconfig10
-rw-r--r--drivers/scsi/Makefile1
-rw-r--r--drivers/scsi/esp_scsi.c35
-rw-r--r--drivers/scsi/esp_scsi.h9
-rw-r--r--drivers/scsi/mac_esp.c657
5 files changed, 700 insertions, 12 deletions
diff --git a/drivers/scsi/Kconfig b/drivers/scsi/Kconfig
index 7f78e3ea517d..99c57b0c1d54 100644
--- a/drivers/scsi/Kconfig
+++ b/drivers/scsi/Kconfig
@@ -1677,6 +1677,16 @@ config MAC_SCSI
1677 SCSI-HOWTO, available from 1677 SCSI-HOWTO, available from
1678 <http://www.tldp.org/docs.html#howto>. 1678 <http://www.tldp.org/docs.html#howto>.
1679 1679
1680config SCSI_MAC_ESP
1681 tristate "Macintosh NCR53c9[46] SCSI"
1682 depends on MAC && SCSI
1683 help
1684 This is the NCR 53c9x SCSI controller found on most of the 68040
1685 based Macintoshes.
1686
1687 To compile this driver as a module, choose M here: the module
1688 will be called mac_esp.
1689
1680config MVME147_SCSI 1690config MVME147_SCSI
1681 bool "WD33C93 SCSI driver for MVME147" 1691 bool "WD33C93 SCSI driver for MVME147"
1682 depends on MVME147 && SCSI=y 1692 depends on MVME147 && SCSI=y
diff --git a/drivers/scsi/Makefile b/drivers/scsi/Makefile
index 23e6ecbd4778..6c775e350c98 100644
--- a/drivers/scsi/Makefile
+++ b/drivers/scsi/Makefile
@@ -46,6 +46,7 @@ obj-$(CONFIG_MVME147_SCSI) += mvme147.o wd33c93.o
46obj-$(CONFIG_SGIWD93_SCSI) += sgiwd93.o wd33c93.o 46obj-$(CONFIG_SGIWD93_SCSI) += sgiwd93.o wd33c93.o
47obj-$(CONFIG_ATARI_SCSI) += atari_scsi.o 47obj-$(CONFIG_ATARI_SCSI) += atari_scsi.o
48obj-$(CONFIG_MAC_SCSI) += mac_scsi.o 48obj-$(CONFIG_MAC_SCSI) += mac_scsi.o
49obj-$(CONFIG_SCSI_MAC_ESP) += esp_scsi.o mac_esp.o
49obj-$(CONFIG_SUN3_SCSI) += sun3_scsi.o sun3_scsi_vme.o 50obj-$(CONFIG_SUN3_SCSI) += sun3_scsi.o sun3_scsi_vme.o
50obj-$(CONFIG_MVME16x_SCSI) += 53c700.o mvme16x_scsi.o 51obj-$(CONFIG_MVME16x_SCSI) += 53c700.o mvme16x_scsi.o
51obj-$(CONFIG_BVME6000_SCSI) += 53c700.o bvme6000_scsi.o 52obj-$(CONFIG_BVME6000_SCSI) += 53c700.o bvme6000_scsi.o
diff --git a/drivers/scsi/esp_scsi.c b/drivers/scsi/esp_scsi.c
index bfdee5968892..a0b6d414953d 100644
--- a/drivers/scsi/esp_scsi.c
+++ b/drivers/scsi/esp_scsi.c
@@ -978,7 +978,7 @@ static int esp_check_spur_intr(struct esp *esp)
978 */ 978 */
979 if (!esp->ops->dma_error(esp)) { 979 if (!esp->ops->dma_error(esp)) {
980 printk(KERN_ERR PFX "esp%d: Spurious irq, " 980 printk(KERN_ERR PFX "esp%d: Spurious irq, "
981 "sreg=%x.\n", 981 "sreg=%02x.\n",
982 esp->host->unique_id, esp->sreg); 982 esp->host->unique_id, esp->sreg);
983 return -1; 983 return -1;
984 } 984 }
@@ -1447,6 +1447,9 @@ static void esp_msgin_sdtr(struct esp *esp, struct esp_target_data *tp)
1447 if (offset > 15) 1447 if (offset > 15)
1448 goto do_reject; 1448 goto do_reject;
1449 1449
1450 if (esp->flags & ESP_FLAG_DISABLE_SYNC)
1451 offset = 0;
1452
1450 if (offset) { 1453 if (offset) {
1451 int rounded_up, one_clock; 1454 int rounded_up, one_clock;
1452 1455
@@ -1697,7 +1700,12 @@ again:
1697 else 1700 else
1698 ent->flags &= ~ESP_CMD_FLAG_WRITE; 1701 ent->flags &= ~ESP_CMD_FLAG_WRITE;
1699 1702
1700 dma_len = esp_dma_length_limit(esp, dma_addr, dma_len); 1703 if (esp->ops->dma_length_limit)
1704 dma_len = esp->ops->dma_length_limit(esp, dma_addr,
1705 dma_len);
1706 else
1707 dma_len = esp_dma_length_limit(esp, dma_addr, dma_len);
1708
1701 esp->data_dma_len = dma_len; 1709 esp->data_dma_len = dma_len;
1702 1710
1703 if (!dma_len) { 1711 if (!dma_len) {
@@ -1761,7 +1769,6 @@ again:
1761 esp_advance_dma(esp, ent, cmd, bytes_sent); 1769 esp_advance_dma(esp, ent, cmd, bytes_sent);
1762 esp_event(esp, ESP_EVENT_CHECK_PHASE); 1770 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1763 goto again; 1771 goto again;
1764 break;
1765 } 1772 }
1766 1773
1767 case ESP_EVENT_STATUS: { 1774 case ESP_EVENT_STATUS: {
@@ -2235,7 +2242,7 @@ static void esp_bootup_reset(struct esp *esp)
2235 2242
2236static void esp_set_clock_params(struct esp *esp) 2243static void esp_set_clock_params(struct esp *esp)
2237{ 2244{
2238 int fmhz; 2245 int fhz;
2239 u8 ccf; 2246 u8 ccf;
2240 2247
2241 /* This is getting messy but it has to be done correctly or else 2248 /* This is getting messy but it has to be done correctly or else
@@ -2270,9 +2277,9 @@ static void esp_set_clock_params(struct esp *esp)
2270 * This entails the smallest and largest sync period we could ever 2277 * This entails the smallest and largest sync period we could ever
2271 * handle on this ESP. 2278 * handle on this ESP.
2272 */ 2279 */
2273 fmhz = esp->cfreq; 2280 fhz = esp->cfreq;
2274 2281
2275 ccf = ((fmhz / 1000000) + 4) / 5; 2282 ccf = ((fhz / 1000000) + 4) / 5;
2276 if (ccf == 1) 2283 if (ccf == 1)
2277 ccf = 2; 2284 ccf = 2;
2278 2285
@@ -2281,16 +2288,16 @@ static void esp_set_clock_params(struct esp *esp)
2281 * been unable to find the clock-frequency PROM property. All 2288 * been unable to find the clock-frequency PROM property. All
2282 * other machines provide useful values it seems. 2289 * other machines provide useful values it seems.
2283 */ 2290 */
2284 if (fmhz <= 5000000 || ccf < 1 || ccf > 8) { 2291 if (fhz <= 5000000 || ccf < 1 || ccf > 8) {
2285 fmhz = 20000000; 2292 fhz = 20000000;
2286 ccf = 4; 2293 ccf = 4;
2287 } 2294 }
2288 2295
2289 esp->cfact = (ccf == 8 ? 0 : ccf); 2296 esp->cfact = (ccf == 8 ? 0 : ccf);
2290 esp->cfreq = fmhz; 2297 esp->cfreq = fhz;
2291 esp->ccycle = ESP_MHZ_TO_CYCLE(fmhz); 2298 esp->ccycle = ESP_HZ_TO_CYCLE(fhz);
2292 esp->ctick = ESP_TICK(ccf, esp->ccycle); 2299 esp->ctick = ESP_TICK(ccf, esp->ccycle);
2293 esp->neg_defp = ESP_NEG_DEFP(fmhz, ccf); 2300 esp->neg_defp = ESP_NEG_DEFP(fhz, ccf);
2294 esp->sync_defp = SYNC_DEFP_SLOW; 2301 esp->sync_defp = SYNC_DEFP_SLOW;
2295} 2302}
2296 2303
@@ -2382,6 +2389,12 @@ static int esp_slave_configure(struct scsi_device *dev)
2382 struct esp_target_data *tp = &esp->target[dev->id]; 2389 struct esp_target_data *tp = &esp->target[dev->id];
2383 int goal_tags, queue_depth; 2390 int goal_tags, queue_depth;
2384 2391
2392 if (esp->flags & ESP_FLAG_DISABLE_SYNC) {
2393 /* Bypass async domain validation */
2394 dev->ppr = 0;
2395 dev->sdtr = 0;
2396 }
2397
2385 goal_tags = 0; 2398 goal_tags = 0;
2386 2399
2387 if (dev->tagged_supported) { 2400 if (dev->tagged_supported) {
diff --git a/drivers/scsi/esp_scsi.h b/drivers/scsi/esp_scsi.h
index 9367a88d3974..bb43a1388188 100644
--- a/drivers/scsi/esp_scsi.h
+++ b/drivers/scsi/esp_scsi.h
@@ -224,7 +224,7 @@
224#define ESP_TIMEO_CONST 8192 224#define ESP_TIMEO_CONST 8192
225#define ESP_NEG_DEFP(mhz, cfact) \ 225#define ESP_NEG_DEFP(mhz, cfact) \
226 ((ESP_BUS_TIMEOUT * ((mhz) / 1000)) / (8192 * (cfact))) 226 ((ESP_BUS_TIMEOUT * ((mhz) / 1000)) / (8192 * (cfact)))
227#define ESP_MHZ_TO_CYCLE(mhertz) ((1000000000) / ((mhertz) / 1000)) 227#define ESP_HZ_TO_CYCLE(hertz) ((1000000000) / ((hertz) / 1000))
228#define ESP_TICK(ccf, cycle) ((7682 * (ccf) * (cycle) / 1000)) 228#define ESP_TICK(ccf, cycle) ((7682 * (ccf) * (cycle) / 1000))
229 229
230/* For slow to medium speed input clock rates we shoot for 5mb/s, but for high 230/* For slow to medium speed input clock rates we shoot for 5mb/s, but for high
@@ -368,6 +368,12 @@ struct esp_driver_ops {
368 */ 368 */
369 int (*irq_pending)(struct esp *esp); 369 int (*irq_pending)(struct esp *esp);
370 370
371 /* Return the maximum allowable size of a DMA transfer for a
372 * given buffer.
373 */
374 u32 (*dma_length_limit)(struct esp *esp, u32 dma_addr,
375 u32 dma_len);
376
371 /* Reset the DMA engine entirely. On return, ESP interrupts 377 /* Reset the DMA engine entirely. On return, ESP interrupts
372 * should be enabled. Often the interrupt enabling is 378 * should be enabled. Often the interrupt enabling is
373 * controlled in the DMA engine. 379 * controlled in the DMA engine.
@@ -471,6 +477,7 @@ struct esp {
471#define ESP_FLAG_DOING_SLOWCMD 0x00000004 477#define ESP_FLAG_DOING_SLOWCMD 0x00000004
472#define ESP_FLAG_WIDE_CAPABLE 0x00000008 478#define ESP_FLAG_WIDE_CAPABLE 0x00000008
473#define ESP_FLAG_QUICKIRQ_CHECK 0x00000010 479#define ESP_FLAG_QUICKIRQ_CHECK 0x00000010
480#define ESP_FLAG_DISABLE_SYNC 0x00000020
474 481
475 u8 select_state; 482 u8 select_state;
476#define ESP_SELECT_NONE 0x00 /* Not selecting */ 483#define ESP_SELECT_NONE 0x00 /* Not selecting */
diff --git a/drivers/scsi/mac_esp.c b/drivers/scsi/mac_esp.c
new file mode 100644
index 000000000000..cd37bd69a115
--- /dev/null
+++ b/drivers/scsi/mac_esp.c
@@ -0,0 +1,657 @@
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
23#include <asm/irq.h>
24#include <asm/dma.h>
25
26#include <asm/macints.h>
27#include <asm/macintosh.h>
28
29#include <scsi/scsi_host.h>
30
31#include "esp_scsi.h"
32
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"
37
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
46
47#define esp_read8(REG) mac_esp_read8(esp, REG)
48#define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG)
49
50struct mac_esp_priv {
51 struct esp *esp;
52 void __iomem *pdma_regs;
53 void __iomem *pdma_io;
54 int error;
55};
56static struct platform_device *internal_esp, *external_esp;
57
58#define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
59 platform_get_drvdata((struct platform_device *) \
60 (esp->dev)))
61
62static inline void mac_esp_write8(struct esp *esp, u8 val, unsigned long reg)
63{
64 nubus_writeb(val, esp->regs + reg * 16);
65}
66
67static inline u8 mac_esp_read8(struct esp *esp, unsigned long reg)
68{
69 return nubus_readb(esp->regs + reg * 16);
70}
71
72/* For pseudo DMA and PIO we need the virtual address
73 * so this address mapping is the identity mapping.
74 */
75
76static dma_addr_t mac_esp_map_single(struct esp *esp, void *buf,
77 size_t sz, int dir)
78{
79 return (dma_addr_t)buf;
80}
81
82static int mac_esp_map_sg(struct esp *esp, struct scatterlist *sg,
83 int num_sg, int dir)
84{
85 int i;
86
87 for (i = 0; i < num_sg; i++)
88 sg[i].dma_address = (u32)sg_virt(&sg[i]);
89 return num_sg;
90}
91
92static void mac_esp_unmap_single(struct esp *esp, dma_addr_t addr,
93 size_t sz, int dir)
94{
95 /* Nothing to do. */
96}
97
98static void mac_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
99 int num_sg, int dir)
100{
101 /* Nothing to do. */
102}
103
104static void mac_esp_reset_dma(struct esp *esp)
105{
106 /* Nothing to do. */
107}
108
109static void mac_esp_dma_drain(struct esp *esp)
110{
111 /* Nothing to do. */
112}
113
114static void mac_esp_dma_invalidate(struct esp *esp)
115{
116 /* Nothing to do. */
117}
118
119static int mac_esp_dma_error(struct esp *esp)
120{
121 return MAC_ESP_GET_PRIV(esp)->error;
122}
123
124static inline int mac_esp_wait_for_empty_fifo(struct esp *esp)
125{
126 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
127 int i = 500000;
128
129 do {
130 if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES))
131 return 0;
132
133 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
134 return 1;
135
136 udelay(2);
137 } while (--i);
138
139 printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n",
140 esp_read8(ESP_STATUS));
141 mep->error = 1;
142 return 1;
143}
144
145static inline int mac_esp_wait_for_dreq(struct esp *esp)
146{
147 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
148 int i = 500000;
149
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;
157 }
158
159 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
160 return 1;
161
162 udelay(2);
163 } while (--i);
164
165 printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n",
166 esp_read8(ESP_STATUS));
167 mep->error = 1;
168 return 1;
169}
170
171#define MAC_ESP_PDMA_LOOP(operands) \
172 asm volatile ( \
173 " tstw %2 \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,%2 \n" \
192 " jbne 1b \n" \
193 "20: tstw %3 \n" \
194 " jbeq 30f \n" \
195 "21: movew " operands " \n" \
196 " subqw #1,%3 \n" \
197 " jbne 21b \n" \
198 "30: tstw %4 \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) \
227 : "a" (mep->pdma_io), "r" (count32), "r" (count2), "g" (esp_count))
228
229static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count,
230 u32 dma_count, int write, u8 cmd)
231{
232 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
233 unsigned long flags;
234
235 local_irq_save(flags);
236
237 mep->error = 0;
238
239 if (!write)
240 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
241
242 esp_write8((esp_count >> 0) & 0xFF, ESP_TCLOW);
243 esp_write8((esp_count >> 8) & 0xFF, ESP_TCMED);
244
245 scsi_esp_cmd(esp, cmd);
246
247 do {
248 unsigned int count32 = esp_count >> 5;
249 unsigned int count2 = (esp_count & 0x1F) >> 1;
250 unsigned int start_addr = addr;
251
252 if (mac_esp_wait_for_dreq(esp))
253 break;
254
255 if (write) {
256 MAC_ESP_PDMA_LOOP("%1@,%0@+");
257
258 esp_count -= addr - start_addr;
259 } else {
260 unsigned int n;
261
262 MAC_ESP_PDMA_LOOP("%0@+,%1@");
263
264 if (mac_esp_wait_for_empty_fifo(esp))
265 break;
266
267 n = (esp_read8(ESP_TCMED) << 8) + esp_read8(ESP_TCLOW);
268 addr = start_addr + esp_count - n;
269 esp_count = n;
270 }
271 } while (esp_count);
272
273 local_irq_restore(flags);
274}
275
276/*
277 * Programmed IO routines follow.
278 */
279
280static inline int mac_esp_wait_for_fifo(struct esp *esp)
281{
282 int i = 500000;
283
284 do {
285 if (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES)
286 return 0;
287
288 udelay(2);
289 } while (--i);
290
291 printk(KERN_ERR PFX "FIFO is empty (sreg %02x)\n",
292 esp_read8(ESP_STATUS));
293 return 1;
294}
295
296static inline int mac_esp_wait_for_intr(struct esp *esp)
297{
298 int i = 500000;
299
300 do {
301 esp->sreg = esp_read8(ESP_STATUS);
302 if (esp->sreg & ESP_STAT_INTR)
303 return 0;
304
305 udelay(2);
306 } while (--i);
307
308 printk(KERN_ERR PFX "IRQ timeout (sreg %02x)\n", esp->sreg);
309 return 1;
310}
311
312#define MAC_ESP_PIO_LOOP(operands, reg1) \
313 asm volatile ( \
314 "1: moveb " operands " \n" \
315 " subqw #1,%1 \n" \
316 " jbne 1b \n" \
317 : "+a" (addr), "+r" (reg1) \
318 : "a" (fifo))
319
320#define MAC_ESP_PIO_FILL(operands, reg1) \
321 asm volatile ( \
322 " moveb " operands " \n" \
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 " subqw #8,%1 \n" \
339 " subqw #8,%1 \n" \
340 : "+a" (addr), "+r" (reg1) \
341 : "a" (fifo))
342
343#define MAC_ESP_FIFO_SIZE 16
344
345static void mac_esp_send_pio_cmd(struct esp *esp, u32 addr, u32 esp_count,
346 u32 dma_count, int write, u8 cmd)
347{
348 unsigned long flags;
349 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
350 u8 *fifo = esp->regs + ESP_FDATA * 16;
351
352 local_irq_save(flags);
353
354 cmd &= ~ESP_CMD_DMA;
355 mep->error = 0;
356
357 if (write) {
358 scsi_esp_cmd(esp, cmd);
359
360 if (!mac_esp_wait_for_intr(esp)) {
361 if (mac_esp_wait_for_fifo(esp))
362 esp_count = 0;
363 } else {
364 esp_count = 0;
365 }
366 } else {
367 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
368
369 if (esp_count >= MAC_ESP_FIFO_SIZE)
370 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
371 else
372 MAC_ESP_PIO_LOOP("%0@+,%2@", esp_count);
373
374 scsi_esp_cmd(esp, cmd);
375 }
376
377 while (esp_count) {
378 unsigned int n;
379
380 if (mac_esp_wait_for_intr(esp)) {
381 mep->error = 1;
382 break;
383 }
384
385 if (esp->sreg & ESP_STAT_SPAM) {
386 printk(KERN_ERR PFX "gross error\n");
387 mep->error = 1;
388 break;
389 }
390
391 n = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
392
393 if (write) {
394 if (n > esp_count)
395 n = esp_count;
396 esp_count -= n;
397
398 MAC_ESP_PIO_LOOP("%2@,%0@+", n);
399
400 if ((esp->sreg & ESP_STAT_PMASK) == ESP_STATP)
401 break;
402
403 if (esp_count) {
404 esp->ireg = esp_read8(ESP_INTRPT);
405 if (esp->ireg & ESP_INTR_DC)
406 break;
407
408 scsi_esp_cmd(esp, ESP_CMD_TI);
409 }
410 } else {
411 esp->ireg = esp_read8(ESP_INTRPT);
412 if (esp->ireg & ESP_INTR_DC)
413 break;
414
415 n = MAC_ESP_FIFO_SIZE - n;
416 if (n > esp_count)
417 n = esp_count;
418
419 if (n == MAC_ESP_FIFO_SIZE) {
420 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
421 } else {
422 esp_count -= n;
423 MAC_ESP_PIO_LOOP("%0@+,%2@", n);
424 }
425
426 scsi_esp_cmd(esp, ESP_CMD_TI);
427 }
428 }
429
430 local_irq_restore(flags);
431}
432
433static int mac_esp_irq_pending(struct esp *esp)
434{
435 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
436 return 1;
437 return 0;
438}
439
440static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
441{
442 return dma_len > 0xFFFF ? 0xFFFF : dma_len;
443}
444
445static struct esp_driver_ops mac_esp_ops = {
446 .esp_write8 = mac_esp_write8,
447 .esp_read8 = mac_esp_read8,
448 .map_single = mac_esp_map_single,
449 .map_sg = mac_esp_map_sg,
450 .unmap_single = mac_esp_unmap_single,
451 .unmap_sg = mac_esp_unmap_sg,
452 .irq_pending = mac_esp_irq_pending,
453 .dma_length_limit = mac_esp_dma_length_limit,
454 .reset_dma = mac_esp_reset_dma,
455 .dma_drain = mac_esp_dma_drain,
456 .dma_invalidate = mac_esp_dma_invalidate,
457 .send_dma_cmd = mac_esp_send_pdma_cmd,
458 .dma_error = mac_esp_dma_error,
459};
460
461static int __devinit esp_mac_probe(struct platform_device *dev)
462{
463 struct scsi_host_template *tpnt = &scsi_esp_template;
464 struct Scsi_Host *host;
465 struct esp *esp;
466 int err;
467 int chips_present;
468 struct mac_esp_priv *mep;
469
470 if (!MACH_IS_MAC)
471 return -ENODEV;
472
473 switch (macintosh_config->scsi_type) {
474 case MAC_SCSI_QUADRA:
475 case MAC_SCSI_QUADRA3:
476 chips_present = 1;
477 break;
478 case MAC_SCSI_QUADRA2:
479 if ((macintosh_config->ident == MAC_MODEL_Q900) ||
480 (macintosh_config->ident == MAC_MODEL_Q950))
481 chips_present = 2;
482 else
483 chips_present = 1;
484 break;
485 default:
486 chips_present = 0;
487 }
488
489 if (dev->id + 1 > chips_present)
490 return -ENODEV;
491
492 host = scsi_host_alloc(tpnt, sizeof(struct esp));
493
494 err = -ENOMEM;
495 if (!host)
496 goto fail;
497
498 host->max_id = 8;
499 host->use_clustering = DISABLE_CLUSTERING;
500 esp = shost_priv(host);
501
502 esp->host = host;
503 esp->dev = dev;
504
505 esp->command_block = kzalloc(16, GFP_KERNEL);
506 if (!esp->command_block)
507 goto fail_unlink;
508 esp->command_block_dma = (dma_addr_t)esp->command_block;
509
510 esp->scsi_id = 7;
511 host->this_id = esp->scsi_id;
512 esp->scsi_id_mask = 1 << esp->scsi_id;
513
514 mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL);
515 if (!mep)
516 goto fail_free_command_block;
517 mep->esp = esp;
518 platform_set_drvdata(dev, mep);
519
520 switch (macintosh_config->scsi_type) {
521 case MAC_SCSI_QUADRA:
522 esp->cfreq = 16500000;
523 esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA;
524 mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
525 mep->pdma_regs = NULL;
526 break;
527 case MAC_SCSI_QUADRA2:
528 esp->cfreq = 25000000;
529 esp->regs = (void __iomem *)(MAC_ESP_REGS_QUADRA2 +
530 dev->id * MAC_ESP_REGS_SPACING);
531 mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
532 mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG +
533 dev->id * MAC_ESP_PDMA_REG_SPACING);
534 nubus_writel(0x1d1, mep->pdma_regs);
535 break;
536 case MAC_SCSI_QUADRA3:
537 /* These quadras have a real DMA controller (the PSC) but we
538 * don't know how to drive it so we must use PIO instead.
539 */
540 esp->cfreq = 25000000;
541 esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA3;
542 mep->pdma_io = NULL;
543 mep->pdma_regs = NULL;
544 break;
545 }
546
547 esp->ops = &mac_esp_ops;
548 if (mep->pdma_io == NULL) {
549 printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id);
550 esp_write8(0, ESP_TCLOW);
551 esp_write8(0, ESP_TCMED);
552 esp->flags = ESP_FLAG_DISABLE_SYNC;
553 mac_esp_ops.send_dma_cmd = mac_esp_send_pio_cmd;
554 } else {
555 printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id);
556 }
557
558 host->irq = IRQ_MAC_SCSI;
559 err = request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "Mac ESP",
560 esp);
561 if (err < 0)
562 goto fail_free_priv;
563
564 err = scsi_esp_register(esp, &dev->dev);
565 if (err)
566 goto fail_free_irq;
567
568 return 0;
569
570fail_free_irq:
571 free_irq(host->irq, esp);
572fail_free_priv:
573 kfree(mep);
574fail_free_command_block:
575 kfree(esp->command_block);
576fail_unlink:
577 scsi_host_put(host);
578fail:
579 return err;
580}
581
582static int __devexit esp_mac_remove(struct platform_device *dev)
583{
584 struct mac_esp_priv *mep = platform_get_drvdata(dev);
585 struct esp *esp = mep->esp;
586 unsigned int irq = esp->host->irq;
587
588 scsi_esp_unregister(esp);
589
590 free_irq(irq, esp);
591
592 kfree(mep);
593
594 kfree(esp->command_block);
595
596 scsi_host_put(esp->host);
597
598 return 0;
599}
600
601static struct platform_driver esp_mac_driver = {
602 .probe = esp_mac_probe,
603 .remove = __devexit_p(esp_mac_remove),
604 .driver = {
605 .name = DRV_MODULE_NAME,
606 },
607};
608
609static int __init mac_esp_init(void)
610{
611 int err;
612
613 err = platform_driver_register(&esp_mac_driver);
614 if (err)
615 return err;
616
617 internal_esp = platform_device_alloc(DRV_MODULE_NAME, 0);
618 if (internal_esp && platform_device_add(internal_esp)) {
619 platform_device_put(internal_esp);
620 internal_esp = NULL;
621 }
622
623 external_esp = platform_device_alloc(DRV_MODULE_NAME, 1);
624 if (external_esp && platform_device_add(external_esp)) {
625 platform_device_put(external_esp);
626 external_esp = NULL;
627 }
628
629 if (internal_esp || external_esp) {
630 return 0;
631 } else {
632 platform_driver_unregister(&esp_mac_driver);
633 return -ENOMEM;
634 }
635}
636
637static void __exit mac_esp_exit(void)
638{
639 platform_driver_unregister(&esp_mac_driver);
640
641 if (internal_esp) {
642 platform_device_unregister(internal_esp);
643 internal_esp = NULL;
644 }
645 if (external_esp) {
646 platform_device_unregister(external_esp);
647 external_esp = NULL;
648 }
649}
650
651MODULE_DESCRIPTION("Mac ESP SCSI driver");
652MODULE_AUTHOR("Finn Thain <fthain@telegraphics.com.au>");
653MODULE_LICENSE("GPLv2");
654MODULE_VERSION(DRV_VERSION);
655
656module_init(mac_esp_init);
657module_exit(mac_esp_exit);