2 * dec_esp.c: Driver for SCSI chips on IOASIC based TURBOchannel DECstations
3 * and TURBOchannel PMAZ-A cards
5 * TURBOchannel changes by Harald Koerfgen
6 * PMAZ-A support by David Airlie
9 * Copyright (C) 1997 Thomas Bogendoerfer (tsbogend@alpha.franken.de)
11 * jazz_esp is based on David S. Miller's ESP driver and cyber_esp
13 * 20000819 - Small PMAZ-AA fixes by Florian Lohoff <flo@rfc822.org>
14 * Be warned the PMAZ-AA works currently as a single card.
15 * Dont try to put multiple cards in one machine - They are
16 * both detected but it may crash under high load garbling your
18 * 20001005 - Initialization fixes for 2.4.0-test9
19 * Florian Lohoff <flo@rfc822.org>
21 * Copyright (C) 2002, 2003, 2005 Maciej W. Rozycki
24 #include <linux/kernel.h>
25 #include <linux/delay.h>
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
29 #include <linux/blkdev.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/stat.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
39 #include <asm/dec/interrupts.h>
40 #include <asm/dec/ioasic.h>
41 #include <asm/dec/ioasic_addrs.h>
42 #include <asm/dec/ioasic_ints.h>
43 #include <asm/dec/machtype.h>
44 #include <asm/dec/system.h>
45 #include <asm/dec/tc.h>
47 #define DEC_SCSI_SREG 0
48 #define DEC_SCSI_DMAREG 0x40000
49 #define DEC_SCSI_SRAM 0x80000
50 #define DEC_SCSI_DIAG 0xC0000
53 #include <scsi/scsi_host.h>
56 static int dma_bytes_sent(struct NCR_ESP
*esp
, int fifo_count
);
57 static void dma_drain(struct NCR_ESP
*esp
);
58 static int dma_can_transfer(struct NCR_ESP
*esp
, struct scsi_cmnd
*sp
);
59 static void dma_dump_state(struct NCR_ESP
*esp
);
60 static void dma_init_read(struct NCR_ESP
*esp
, u32 vaddress
, int length
);
61 static void dma_init_write(struct NCR_ESP
*esp
, u32 vaddress
, int length
);
62 static void dma_ints_off(struct NCR_ESP
*esp
);
63 static void dma_ints_on(struct NCR_ESP
*esp
);
64 static int dma_irq_p(struct NCR_ESP
*esp
);
65 static int dma_ports_p(struct NCR_ESP
*esp
);
66 static void dma_setup(struct NCR_ESP
*esp
, u32 addr
, int count
, int write
);
67 static void dma_mmu_get_scsi_one(struct NCR_ESP
*esp
, struct scsi_cmnd
* sp
);
68 static void dma_mmu_get_scsi_sgl(struct NCR_ESP
*esp
, struct scsi_cmnd
* sp
);
69 static void dma_advance_sg(struct scsi_cmnd
* sp
);
71 static void pmaz_dma_drain(struct NCR_ESP
*esp
);
72 static void pmaz_dma_init_read(struct NCR_ESP
*esp
, u32 vaddress
, int length
);
73 static void pmaz_dma_init_write(struct NCR_ESP
*esp
, u32 vaddress
, int length
);
74 static void pmaz_dma_ints_off(struct NCR_ESP
*esp
);
75 static void pmaz_dma_ints_on(struct NCR_ESP
*esp
);
76 static void pmaz_dma_setup(struct NCR_ESP
*esp
, u32 addr
, int count
, int write
);
77 static void pmaz_dma_mmu_get_scsi_one(struct NCR_ESP
*esp
, struct scsi_cmnd
* sp
);
79 #define TC_ESP_RAM_SIZE 0x20000
80 #define ESP_TGT_DMA_SIZE ((TC_ESP_RAM_SIZE/7) & ~(sizeof(int)-1))
83 #define TC_ESP_DMAR_MASK 0x1ffff
84 #define TC_ESP_DMAR_WRITE 0x80000000
85 #define TC_ESP_DMA_ADDR(x) ((unsigned)(x) & TC_ESP_DMAR_MASK)
88 int scsi_current_length
;
90 volatile unsigned char cmd_buffer
[16];
91 volatile unsigned char pmaz_cmd_buffer
[16];
92 /* This is where all commands are put
93 * before they are trasfered to the ESP chip
97 static irqreturn_t
scsi_dma_merr_int(int, void *, struct pt_regs
*);
98 static irqreturn_t
scsi_dma_err_int(int, void *, struct pt_regs
*);
99 static irqreturn_t
scsi_dma_int(int, void *, struct pt_regs
*);
101 static int dec_esp_detect(struct scsi_host_template
* tpnt
);
103 static int dec_esp_release(struct Scsi_Host
*shost
)
106 free_irq(shost
->irq
, NULL
);
107 if (shost
->io_port
&& shost
->n_io_port
)
108 release_region(shost
->io_port
, shost
->n_io_port
);
109 scsi_unregister(shost
);
113 static struct scsi_host_template driver_template
= {
114 .proc_name
= "dec_esp",
115 .proc_info
= esp_proc_info
,
117 .detect
= dec_esp_detect
,
118 .slave_alloc
= esp_slave_alloc
,
119 .slave_destroy
= esp_slave_destroy
,
120 .release
= dec_esp_release
,
122 .queuecommand
= esp_queue
,
123 .eh_abort_handler
= esp_abort
,
124 .eh_bus_reset_handler
= esp_reset
,
127 .sg_tablesize
= SG_ALL
,
129 .use_clustering
= DISABLE_CLUSTERING
,
133 #include "scsi_module.c"
135 /***************************************************************** Detection */
136 static int dec_esp_detect(struct scsi_host_template
* tpnt
)
139 struct ConfigDev
*esp_dev
;
141 unsigned long mem_start
;
145 esp
= esp_allocate(tpnt
, (void *) esp_dev
);
147 /* Do command transfer with programmed I/O */
148 esp
->do_pio_cmds
= 1;
150 /* Required functions */
151 esp
->dma_bytes_sent
= &dma_bytes_sent
;
152 esp
->dma_can_transfer
= &dma_can_transfer
;
153 esp
->dma_dump_state
= &dma_dump_state
;
154 esp
->dma_init_read
= &dma_init_read
;
155 esp
->dma_init_write
= &dma_init_write
;
156 esp
->dma_ints_off
= &dma_ints_off
;
157 esp
->dma_ints_on
= &dma_ints_on
;
158 esp
->dma_irq_p
= &dma_irq_p
;
159 esp
->dma_ports_p
= &dma_ports_p
;
160 esp
->dma_setup
= &dma_setup
;
162 /* Optional functions */
163 esp
->dma_barrier
= 0;
164 esp
->dma_drain
= &dma_drain
;
165 esp
->dma_invalidate
= 0;
166 esp
->dma_irq_entry
= 0;
167 esp
->dma_irq_exit
= 0;
170 esp
->dma_led_off
= 0;
173 /* virtual DMA functions */
174 esp
->dma_mmu_get_scsi_one
= &dma_mmu_get_scsi_one
;
175 esp
->dma_mmu_get_scsi_sgl
= &dma_mmu_get_scsi_sgl
;
176 esp
->dma_mmu_release_scsi_one
= 0;
177 esp
->dma_mmu_release_scsi_sgl
= 0;
178 esp
->dma_advance_sg
= &dma_advance_sg
;
181 /* SCSI chip speed */
182 esp
->cfreq
= 25000000;
186 /* ESP register base */
187 esp
->eregs
= (void *)CKSEG1ADDR(dec_kn_slot_base
+
190 /* Set the command buffer */
191 esp
->esp_command
= (volatile unsigned char *) cmd_buffer
;
193 /* get virtual dma address for command buffer */
194 esp
->esp_command_dvma
= virt_to_phys(cmd_buffer
);
196 esp
->irq
= dec_interrupt
[DEC_IRQ_ASC
];
200 /* Check for differential SCSI-bus */
205 if (request_irq(esp
->irq
, esp_intr
, SA_INTERRUPT
,
206 "ncr53c94", esp
->ehost
))
208 if (request_irq(dec_interrupt
[DEC_IRQ_ASC_MERR
],
209 scsi_dma_merr_int
, SA_INTERRUPT
,
210 "ncr53c94 error", esp
->ehost
))
212 if (request_irq(dec_interrupt
[DEC_IRQ_ASC_ERR
],
213 scsi_dma_err_int
, SA_INTERRUPT
,
214 "ncr53c94 overrun", esp
->ehost
))
215 goto err_free_irq_merr
;
216 if (request_irq(dec_interrupt
[DEC_IRQ_ASC_DMA
],
217 scsi_dma_int
, SA_INTERRUPT
,
218 "ncr53c94 dma", esp
->ehost
))
219 goto err_free_irq_err
;
224 while ((slot
= search_tc_card("PMAZ-AA")) >= 0) {
228 esp
= esp_allocate(tpnt
, (void *) esp_dev
);
230 mem_start
= get_tc_base_addr(slot
);
232 /* Store base addr into esp struct */
233 esp
->slot
= CPHYSADDR(mem_start
);
236 esp
->eregs
= (void *)CKSEG1ADDR(mem_start
+
238 esp
->do_pio_cmds
= 1;
240 /* Set the command buffer */
241 esp
->esp_command
= (volatile unsigned char *) pmaz_cmd_buffer
;
243 /* get virtual dma address for command buffer */
244 esp
->esp_command_dvma
= virt_to_phys(pmaz_cmd_buffer
);
246 esp
->cfreq
= get_tc_speed();
248 esp
->irq
= get_tc_irq_nr(slot
);
250 /* Required functions */
251 esp
->dma_bytes_sent
= &dma_bytes_sent
;
252 esp
->dma_can_transfer
= &dma_can_transfer
;
253 esp
->dma_dump_state
= &dma_dump_state
;
254 esp
->dma_init_read
= &pmaz_dma_init_read
;
255 esp
->dma_init_write
= &pmaz_dma_init_write
;
256 esp
->dma_ints_off
= &pmaz_dma_ints_off
;
257 esp
->dma_ints_on
= &pmaz_dma_ints_on
;
258 esp
->dma_irq_p
= &dma_irq_p
;
259 esp
->dma_ports_p
= &dma_ports_p
;
260 esp
->dma_setup
= &pmaz_dma_setup
;
262 /* Optional functions */
263 esp
->dma_barrier
= 0;
264 esp
->dma_drain
= &pmaz_dma_drain
;
265 esp
->dma_invalidate
= 0;
266 esp
->dma_irq_entry
= 0;
267 esp
->dma_irq_exit
= 0;
270 esp
->dma_led_off
= 0;
273 esp
->dma_mmu_get_scsi_one
= pmaz_dma_mmu_get_scsi_one
;
274 esp
->dma_mmu_get_scsi_sgl
= 0;
275 esp
->dma_mmu_release_scsi_one
= 0;
276 esp
->dma_mmu_release_scsi_sgl
= 0;
277 esp
->dma_advance_sg
= 0;
279 if (request_irq(esp
->irq
, esp_intr
, SA_INTERRUPT
,
280 "PMAZ_AA", esp
->ehost
)) {
282 release_tc_card(slot
);
292 printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps
, esps_in_use
);
293 esps_running
= esps_in_use
;
299 free_irq(dec_interrupt
[DEC_IRQ_ASC_ERR
], scsi_dma_err_int
);
301 free_irq(dec_interrupt
[DEC_IRQ_ASC_MERR
], scsi_dma_merr_int
);
303 free_irq(esp
->irq
, esp_intr
);
309 /************************************************************* DMA Functions */
310 static irqreturn_t
scsi_dma_merr_int(int irq
, void *dev_id
, struct pt_regs
*regs
)
312 printk("Got unexpected SCSI DMA Interrupt! < ");
313 printk("SCSI_DMA_MEMRDERR ");
319 static irqreturn_t
scsi_dma_err_int(int irq
, void *dev_id
, struct pt_regs
*regs
)
326 static irqreturn_t
scsi_dma_int(int irq
, void *dev_id
, struct pt_regs
*regs
)
330 scsi_next_ptr
= ioasic_read(IO_REG_SCSI_DMA_P
);
333 scsi_next_ptr
= (((scsi_next_ptr
>> 3) + PAGE_SIZE
) & PAGE_MASK
) << 3;
334 ioasic_write(IO_REG_SCSI_DMA_BP
, scsi_next_ptr
);
340 static int dma_bytes_sent(struct NCR_ESP
*esp
, int fifo_count
)
345 static void dma_drain(struct NCR_ESP
*esp
)
347 u32 nw
, data0
, data1
, scsi_data_ptr
;
350 nw
= ioasic_read(IO_REG_SCSI_SCR
);
353 * Is there something in the dma buffers left?
356 scsi_data_ptr
= ioasic_read(IO_REG_SCSI_DMA_P
) >> 3;
357 p
= phys_to_virt(scsi_data_ptr
);
360 data0
= ioasic_read(IO_REG_SCSI_SDR0
);
361 p
[0] = data0
& 0xffff;
364 data0
= ioasic_read(IO_REG_SCSI_SDR0
);
365 p
[0] = data0
& 0xffff;
366 p
[1] = (data0
>> 16) & 0xffff;
369 data0
= ioasic_read(IO_REG_SCSI_SDR0
);
370 data1
= ioasic_read(IO_REG_SCSI_SDR1
);
371 p
[0] = data0
& 0xffff;
372 p
[1] = (data0
>> 16) & 0xffff;
373 p
[2] = data1
& 0xffff;
376 printk("Strange: %d words in dma buffer left\n", nw
);
382 static int dma_can_transfer(struct NCR_ESP
*esp
, struct scsi_cmnd
* sp
)
384 return sp
->SCp
.this_residual
;
387 static void dma_dump_state(struct NCR_ESP
*esp
)
391 static void dma_init_read(struct NCR_ESP
*esp
, u32 vaddress
, int length
)
393 u32 scsi_next_ptr
, ioasic_ssr
;
397 panic("dec_esp.c: unable to handle partial word transfers, yet...");
399 dma_cache_wback_inv((unsigned long) phys_to_virt(vaddress
), length
);
401 spin_lock_irqsave(&ioasic_ssr_lock
, flags
);
404 ioasic_ssr
= ioasic_read(IO_REG_SSR
);
406 ioasic_ssr
&= ~IO_SSR_SCSI_DMA_EN
;
407 ioasic_write(IO_REG_SSR
, ioasic_ssr
);
410 ioasic_write(IO_REG_SCSI_SCR
, 0);
411 ioasic_write(IO_REG_SCSI_DMA_P
, vaddress
<< 3);
413 /* prepare for next page */
414 scsi_next_ptr
= ((vaddress
+ PAGE_SIZE
) & PAGE_MASK
) << 3;
415 ioasic_write(IO_REG_SCSI_DMA_BP
, scsi_next_ptr
);
417 ioasic_ssr
|= (IO_SSR_SCSI_DMA_DIR
| IO_SSR_SCSI_DMA_EN
);
419 ioasic_write(IO_REG_SSR
, ioasic_ssr
);
422 spin_unlock_irqrestore(&ioasic_ssr_lock
, flags
);
425 static void dma_init_write(struct NCR_ESP
*esp
, u32 vaddress
, int length
)
427 u32 scsi_next_ptr
, ioasic_ssr
;
431 panic("dec_esp.c: unable to handle partial word transfers, yet...");
433 dma_cache_wback_inv((unsigned long) phys_to_virt(vaddress
), length
);
435 spin_lock_irqsave(&ioasic_ssr_lock
, flags
);
438 ioasic_ssr
= ioasic_read(IO_REG_SSR
);
440 ioasic_ssr
&= ~(IO_SSR_SCSI_DMA_DIR
| IO_SSR_SCSI_DMA_EN
);
441 ioasic_write(IO_REG_SSR
, ioasic_ssr
);
444 ioasic_write(IO_REG_SCSI_SCR
, 0);
445 ioasic_write(IO_REG_SCSI_DMA_P
, vaddress
<< 3);
447 /* prepare for next page */
448 scsi_next_ptr
= ((vaddress
+ PAGE_SIZE
) & PAGE_MASK
) << 3;
449 ioasic_write(IO_REG_SCSI_DMA_BP
, scsi_next_ptr
);
451 ioasic_ssr
|= IO_SSR_SCSI_DMA_EN
;
453 ioasic_write(IO_REG_SSR
, ioasic_ssr
);
456 spin_unlock_irqrestore(&ioasic_ssr_lock
, flags
);
459 static void dma_ints_off(struct NCR_ESP
*esp
)
461 disable_irq(dec_interrupt
[DEC_IRQ_ASC_DMA
]);
464 static void dma_ints_on(struct NCR_ESP
*esp
)
466 enable_irq(dec_interrupt
[DEC_IRQ_ASC_DMA
]);
469 static int dma_irq_p(struct NCR_ESP
*esp
)
471 return (esp
->eregs
->esp_status
& ESP_STAT_INTR
);
474 static int dma_ports_p(struct NCR_ESP
*esp
)
477 * FIXME: what's this good for?
482 static void dma_setup(struct NCR_ESP
*esp
, u32 addr
, int count
, int write
)
485 * DMA_ST_WRITE means "move data from device to memory"
486 * so when (write) is true, it actually means READ!
489 dma_init_read(esp
, addr
, count
);
491 dma_init_write(esp
, addr
, count
);
494 static void dma_mmu_get_scsi_one(struct NCR_ESP
*esp
, struct scsi_cmnd
* sp
)
496 sp
->SCp
.ptr
= (char *)virt_to_phys(sp
->request_buffer
);
499 static void dma_mmu_get_scsi_sgl(struct NCR_ESP
*esp
, struct scsi_cmnd
* sp
)
501 int sz
= sp
->SCp
.buffers_residual
;
502 struct scatterlist
*sg
= sp
->SCp
.buffer
;
505 sg
[sz
].dma_address
= page_to_phys(sg
[sz
].page
) + sg
[sz
].offset
;
508 sp
->SCp
.ptr
= (char *)(sp
->SCp
.buffer
->dma_address
);
511 static void dma_advance_sg(struct scsi_cmnd
* sp
)
513 sp
->SCp
.ptr
= (char *)(sp
->SCp
.buffer
->dma_address
);
516 static void pmaz_dma_drain(struct NCR_ESP
*esp
)
518 memcpy(phys_to_virt(esp_virt_buffer
),
519 (void *)CKSEG1ADDR(esp
->slot
+ DEC_SCSI_SRAM
+
521 scsi_current_length
);
524 static void pmaz_dma_init_read(struct NCR_ESP
*esp
, u32 vaddress
, int length
)
526 volatile u32
*dmareg
=
527 (volatile u32
*)CKSEG1ADDR(esp
->slot
+ DEC_SCSI_DMAREG
);
529 if (length
> ESP_TGT_DMA_SIZE
)
530 length
= ESP_TGT_DMA_SIZE
;
532 *dmareg
= TC_ESP_DMA_ADDR(ESP_TGT_DMA_SIZE
);
536 esp_virt_buffer
= vaddress
;
537 scsi_current_length
= length
;
540 static void pmaz_dma_init_write(struct NCR_ESP
*esp
, u32 vaddress
, int length
)
542 volatile u32
*dmareg
=
543 (volatile u32
*)CKSEG1ADDR(esp
->slot
+ DEC_SCSI_DMAREG
);
545 memcpy((void *)CKSEG1ADDR(esp
->slot
+ DEC_SCSI_SRAM
+
547 phys_to_virt(vaddress
), length
);
550 *dmareg
= TC_ESP_DMAR_WRITE
| TC_ESP_DMA_ADDR(ESP_TGT_DMA_SIZE
);
555 static void pmaz_dma_ints_off(struct NCR_ESP
*esp
)
559 static void pmaz_dma_ints_on(struct NCR_ESP
*esp
)
563 static void pmaz_dma_setup(struct NCR_ESP
*esp
, u32 addr
, int count
, int write
)
566 * DMA_ST_WRITE means "move data from device to memory"
567 * so when (write) is true, it actually means READ!
570 pmaz_dma_init_read(esp
, addr
, count
);
572 pmaz_dma_init_write(esp
, addr
, count
);
575 static void pmaz_dma_mmu_get_scsi_one(struct NCR_ESP
*esp
, struct scsi_cmnd
* sp
)
577 sp
->SCp
.ptr
= (char *)virt_to_phys(sp
->request_buffer
);