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, 2006 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>
37 #include <asm/pgtable.h>
38 #include <asm/system.h>
40 #include <asm/dec/interrupts.h>
41 #include <asm/dec/ioasic.h>
42 #include <asm/dec/ioasic_addrs.h>
43 #include <asm/dec/ioasic_ints.h>
44 #include <asm/dec/machtype.h>
45 #include <asm/dec/system.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 *);
98 static irqreturn_t scsi_dma_err_int(int, void *);
99 static irqreturn_t scsi_dma_int(int, void *);
101 static struct scsi_host_template dec_esp_template = {
102 .module = THIS_MODULE,
105 .queuecommand = esp_queue,
106 .eh_abort_handler = esp_abort,
107 .eh_bus_reset_handler = esp_reset,
108 .slave_alloc = esp_slave_alloc,
109 .slave_destroy = esp_slave_destroy,
110 .proc_info = esp_proc_info,
111 .proc_name = "dec_esp",
113 .sg_tablesize = SG_ALL,
115 .use_clustering = DISABLE_CLUSTERING,
118 static struct NCR_ESP *dec_esp_platform;
120 /***************************************************************** Detection */
121 static int dec_esp_platform_probe(void)
127 esp = esp_allocate(&dec_esp_template, NULL, 1);
129 /* Do command transfer with programmed I/O */
130 esp->do_pio_cmds = 1;
132 /* Required functions */
133 esp->dma_bytes_sent = &dma_bytes_sent;
134 esp->dma_can_transfer = &dma_can_transfer;
135 esp->dma_dump_state = &dma_dump_state;
136 esp->dma_init_read = &dma_init_read;
137 esp->dma_init_write = &dma_init_write;
138 esp->dma_ints_off = &dma_ints_off;
139 esp->dma_ints_on = &dma_ints_on;
140 esp->dma_irq_p = &dma_irq_p;
141 esp->dma_ports_p = &dma_ports_p;
142 esp->dma_setup = &dma_setup;
144 /* Optional functions */
145 esp->dma_barrier = 0;
146 esp->dma_drain = &dma_drain;
147 esp->dma_invalidate = 0;
148 esp->dma_irq_entry = 0;
149 esp->dma_irq_exit = 0;
152 esp->dma_led_off = 0;
155 /* virtual DMA functions */
156 esp->dma_mmu_get_scsi_one = &dma_mmu_get_scsi_one;
157 esp->dma_mmu_get_scsi_sgl = &dma_mmu_get_scsi_sgl;
158 esp->dma_mmu_release_scsi_one = 0;
159 esp->dma_mmu_release_scsi_sgl = 0;
160 esp->dma_advance_sg = &dma_advance_sg;
163 /* SCSI chip speed */
164 esp->cfreq = 25000000;
168 /* ESP register base */
169 esp->eregs = (void *)CKSEG1ADDR(dec_kn_slot_base +
172 /* Set the command buffer */
173 esp->esp_command = (volatile unsigned char *) cmd_buffer;
175 /* get virtual dma address for command buffer */
176 esp->esp_command_dvma = virt_to_phys(cmd_buffer);
178 esp->irq = dec_interrupt[DEC_IRQ_ASC];
182 /* Check for differential SCSI-bus */
185 err = request_irq(esp->irq, esp_intr, IRQF_DISABLED,
186 "ncr53c94", esp->ehost);
189 err = request_irq(dec_interrupt[DEC_IRQ_ASC_MERR],
190 scsi_dma_merr_int, IRQF_DISABLED,
191 "ncr53c94 error", esp->ehost);
194 err = request_irq(dec_interrupt[DEC_IRQ_ASC_ERR],
195 scsi_dma_err_int, IRQF_DISABLED,
196 "ncr53c94 overrun", esp->ehost);
199 err = request_irq(dec_interrupt[DEC_IRQ_ASC_DMA], scsi_dma_int,
200 IRQF_DISABLED, "ncr53c94 dma", esp->ehost);
206 err = scsi_add_host(esp->ehost, NULL);
208 printk(KERN_ERR "ESP: Unable to register adapter\n");
212 scsi_scan_host(esp->ehost);
214 dec_esp_platform = esp;
220 free_irq(dec_interrupt[DEC_IRQ_ASC_DMA], esp->ehost);
222 free_irq(dec_interrupt[DEC_IRQ_ASC_ERR], esp->ehost);
224 free_irq(dec_interrupt[DEC_IRQ_ASC_MERR], esp->ehost);
226 free_irq(esp->irq, esp->ehost);
229 scsi_host_put(esp->ehost);
233 static int __init dec_esp_probe(struct device *dev)
236 resource_size_t start, len;
239 esp = esp_allocate(&dec_esp_template, NULL, 1);
241 dev_set_drvdata(dev, esp);
243 start = to_tc_dev(dev)->resource.start;
244 len = to_tc_dev(dev)->resource.end - start + 1;
246 if (!request_mem_region(start, len, dev->bus_id)) {
247 printk(KERN_ERR "%s: Unable to reserve MMIO resource\n",
253 /* Store base addr into esp struct. */
257 esp->eregs = (void *)CKSEG1ADDR(start + DEC_SCSI_SREG);
258 esp->do_pio_cmds = 1;
260 /* Set the command buffer. */
261 esp->esp_command = (volatile unsigned char *)pmaz_cmd_buffer;
263 /* Get virtual dma address for command buffer. */
264 esp->esp_command_dvma = virt_to_phys(pmaz_cmd_buffer);
266 esp->cfreq = tc_get_speed(to_tc_dev(dev)->bus);
268 esp->irq = to_tc_dev(dev)->interrupt;
270 /* Required functions. */
271 esp->dma_bytes_sent = &dma_bytes_sent;
272 esp->dma_can_transfer = &dma_can_transfer;
273 esp->dma_dump_state = &dma_dump_state;
274 esp->dma_init_read = &pmaz_dma_init_read;
275 esp->dma_init_write = &pmaz_dma_init_write;
276 esp->dma_ints_off = &pmaz_dma_ints_off;
277 esp->dma_ints_on = &pmaz_dma_ints_on;
278 esp->dma_irq_p = &dma_irq_p;
279 esp->dma_ports_p = &dma_ports_p;
280 esp->dma_setup = &pmaz_dma_setup;
282 /* Optional functions. */
283 esp->dma_barrier = 0;
284 esp->dma_drain = &pmaz_dma_drain;
285 esp->dma_invalidate = 0;
286 esp->dma_irq_entry = 0;
287 esp->dma_irq_exit = 0;
290 esp->dma_led_off = 0;
293 esp->dma_mmu_get_scsi_one = pmaz_dma_mmu_get_scsi_one;
294 esp->dma_mmu_get_scsi_sgl = 0;
295 esp->dma_mmu_release_scsi_one = 0;
296 esp->dma_mmu_release_scsi_sgl = 0;
297 esp->dma_advance_sg = 0;
299 err = request_irq(esp->irq, esp_intr, IRQF_DISABLED, "PMAZ_AA",
302 printk(KERN_ERR "%s: Unable to get IRQ %d\n",
303 dev->bus_id, esp->irq);
311 err = scsi_add_host(esp->ehost, dev);
313 printk(KERN_ERR "%s: Unable to register adapter\n",
318 scsi_scan_host(esp->ehost);
323 free_irq(esp->irq, esp->ehost);
326 release_mem_region(start, len);
330 scsi_host_put(esp->ehost);
334 static void __exit dec_esp_platform_remove(void)
336 struct NCR_ESP *esp = dec_esp_platform;
338 free_irq(esp->irq, esp->ehost);
340 scsi_host_put(esp->ehost);
341 dec_esp_platform = NULL;
344 static void __exit dec_esp_remove(struct device *dev)
346 struct NCR_ESP *esp = dev_get_drvdata(dev);
348 free_irq(esp->irq, esp->ehost);
350 scsi_host_put(esp->ehost);
354 /************************************************************* DMA Functions */
355 static irqreturn_t scsi_dma_merr_int(int irq, void *dev_id)
357 printk("Got unexpected SCSI DMA Interrupt! < ");
358 printk("SCSI_DMA_MEMRDERR ");
364 static irqreturn_t scsi_dma_err_int(int irq, void *dev_id)
371 static irqreturn_t scsi_dma_int(int irq, void *dev_id)
375 scsi_next_ptr = ioasic_read(IO_REG_SCSI_DMA_P);
378 scsi_next_ptr = (((scsi_next_ptr >> 3) + PAGE_SIZE) & PAGE_MASK) << 3;
379 ioasic_write(IO_REG_SCSI_DMA_BP, scsi_next_ptr);
385 static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
390 static void dma_drain(struct NCR_ESP *esp)
392 u32 nw, data0, data1, scsi_data_ptr;
395 nw = ioasic_read(IO_REG_SCSI_SCR);
398 * Is there something in the dma buffers left?
401 scsi_data_ptr = ioasic_read(IO_REG_SCSI_DMA_P) >> 3;
402 p = phys_to_virt(scsi_data_ptr);
405 data0 = ioasic_read(IO_REG_SCSI_SDR0);
406 p[0] = data0 & 0xffff;
409 data0 = ioasic_read(IO_REG_SCSI_SDR0);
410 p[0] = data0 & 0xffff;
411 p[1] = (data0 >> 16) & 0xffff;
414 data0 = ioasic_read(IO_REG_SCSI_SDR0);
415 data1 = ioasic_read(IO_REG_SCSI_SDR1);
416 p[0] = data0 & 0xffff;
417 p[1] = (data0 >> 16) & 0xffff;
418 p[2] = data1 & 0xffff;
421 printk("Strange: %d words in dma buffer left\n", nw);
427 static int dma_can_transfer(struct NCR_ESP *esp, struct scsi_cmnd * sp)
429 return sp->SCp.this_residual;
432 static void dma_dump_state(struct NCR_ESP *esp)
436 static void dma_init_read(struct NCR_ESP *esp, u32 vaddress, int length)
438 u32 scsi_next_ptr, ioasic_ssr;
442 panic("dec_esp.c: unable to handle partial word transfers, yet...");
444 dma_cache_wback_inv((unsigned long) phys_to_virt(vaddress), length);
446 spin_lock_irqsave(&ioasic_ssr_lock, flags);
449 ioasic_ssr = ioasic_read(IO_REG_SSR);
451 ioasic_ssr &= ~IO_SSR_SCSI_DMA_EN;
452 ioasic_write(IO_REG_SSR, ioasic_ssr);
455 ioasic_write(IO_REG_SCSI_SCR, 0);
456 ioasic_write(IO_REG_SCSI_DMA_P, vaddress << 3);
458 /* prepare for next page */
459 scsi_next_ptr = ((vaddress + PAGE_SIZE) & PAGE_MASK) << 3;
460 ioasic_write(IO_REG_SCSI_DMA_BP, scsi_next_ptr);
462 ioasic_ssr |= (IO_SSR_SCSI_DMA_DIR | IO_SSR_SCSI_DMA_EN);
464 ioasic_write(IO_REG_SSR, ioasic_ssr);
467 spin_unlock_irqrestore(&ioasic_ssr_lock, flags);
470 static void dma_init_write(struct NCR_ESP *esp, u32 vaddress, int length)
472 u32 scsi_next_ptr, ioasic_ssr;
476 panic("dec_esp.c: unable to handle partial word transfers, yet...");
478 dma_cache_wback_inv((unsigned long) phys_to_virt(vaddress), length);
480 spin_lock_irqsave(&ioasic_ssr_lock, flags);
483 ioasic_ssr = ioasic_read(IO_REG_SSR);
485 ioasic_ssr &= ~(IO_SSR_SCSI_DMA_DIR | IO_SSR_SCSI_DMA_EN);
486 ioasic_write(IO_REG_SSR, ioasic_ssr);
489 ioasic_write(IO_REG_SCSI_SCR, 0);
490 ioasic_write(IO_REG_SCSI_DMA_P, vaddress << 3);
492 /* prepare for next page */
493 scsi_next_ptr = ((vaddress + PAGE_SIZE) & PAGE_MASK) << 3;
494 ioasic_write(IO_REG_SCSI_DMA_BP, scsi_next_ptr);
496 ioasic_ssr |= IO_SSR_SCSI_DMA_EN;
498 ioasic_write(IO_REG_SSR, ioasic_ssr);
501 spin_unlock_irqrestore(&ioasic_ssr_lock, flags);
504 static void dma_ints_off(struct NCR_ESP *esp)
506 disable_irq(dec_interrupt[DEC_IRQ_ASC_DMA]);
509 static void dma_ints_on(struct NCR_ESP *esp)
511 enable_irq(dec_interrupt[DEC_IRQ_ASC_DMA]);
514 static int dma_irq_p(struct NCR_ESP *esp)
516 return (esp->eregs->esp_status & ESP_STAT_INTR);
519 static int dma_ports_p(struct NCR_ESP *esp)
522 * FIXME: what's this good for?
527 static void dma_setup(struct NCR_ESP *esp, u32 addr, int count, int write)
530 * DMA_ST_WRITE means "move data from device to memory"
531 * so when (write) is true, it actually means READ!
534 dma_init_read(esp, addr, count);
536 dma_init_write(esp, addr, count);
539 static void dma_mmu_get_scsi_one(struct NCR_ESP *esp, struct scsi_cmnd * sp)
541 sp->SCp.ptr = (char *)virt_to_phys(sp->request_buffer);
544 static void dma_mmu_get_scsi_sgl(struct NCR_ESP *esp, struct scsi_cmnd * sp)
546 int sz = sp->SCp.buffers_residual;
547 struct scatterlist *sg = sp->SCp.buffer;
550 sg[sz].dma_address = page_to_phys(sg[sz].page) + sg[sz].offset;
553 sp->SCp.ptr = (char *)(sp->SCp.buffer->dma_address);
556 static void dma_advance_sg(struct scsi_cmnd * sp)
558 sp->SCp.ptr = (char *)(sp->SCp.buffer->dma_address);
561 static void pmaz_dma_drain(struct NCR_ESP *esp)
563 memcpy(phys_to_virt(esp_virt_buffer),
564 (void *)CKSEG1ADDR(esp->slot + DEC_SCSI_SRAM +
566 scsi_current_length);
569 static void pmaz_dma_init_read(struct NCR_ESP *esp, u32 vaddress, int length)
571 volatile u32 *dmareg =
572 (volatile u32 *)CKSEG1ADDR(esp->slot + DEC_SCSI_DMAREG);
574 if (length > ESP_TGT_DMA_SIZE)
575 length = ESP_TGT_DMA_SIZE;
577 *dmareg = TC_ESP_DMA_ADDR(ESP_TGT_DMA_SIZE);
581 esp_virt_buffer = vaddress;
582 scsi_current_length = length;
585 static void pmaz_dma_init_write(struct NCR_ESP *esp, u32 vaddress, int length)
587 volatile u32 *dmareg =
588 (volatile u32 *)CKSEG1ADDR(esp->slot + DEC_SCSI_DMAREG);
590 memcpy((void *)CKSEG1ADDR(esp->slot + DEC_SCSI_SRAM +
592 phys_to_virt(vaddress), length);
595 *dmareg = TC_ESP_DMAR_WRITE | TC_ESP_DMA_ADDR(ESP_TGT_DMA_SIZE);
600 static void pmaz_dma_ints_off(struct NCR_ESP *esp)
604 static void pmaz_dma_ints_on(struct NCR_ESP *esp)
608 static void pmaz_dma_setup(struct NCR_ESP *esp, u32 addr, int count, int write)
611 * DMA_ST_WRITE means "move data from device to memory"
612 * so when (write) is true, it actually means READ!
615 pmaz_dma_init_read(esp, addr, count);
617 pmaz_dma_init_write(esp, addr, count);
620 static void pmaz_dma_mmu_get_scsi_one(struct NCR_ESP *esp, struct scsi_cmnd * sp)
622 sp->SCp.ptr = (char *)virt_to_phys(sp->request_buffer);
627 static int __init dec_esp_tc_probe(struct device *dev);
628 static int __exit dec_esp_tc_remove(struct device *dev);
630 static const struct tc_device_id dec_esp_tc_table[] = {
631 { "DEC ", "PMAZ-AA " },
634 MODULE_DEVICE_TABLE(tc, dec_esp_tc_table);
636 static struct tc_driver dec_esp_tc_driver = {
637 .id_table = dec_esp_tc_table,
641 .probe = dec_esp_tc_probe,
642 .remove = __exit_p(dec_esp_tc_remove),
646 static int __init dec_esp_tc_probe(struct device *dev)
648 int status = dec_esp_probe(dev);
654 static int __exit dec_esp_tc_remove(struct device *dev)
662 static int __init dec_esp_init(void)
666 status = tc_register_driver(&dec_esp_tc_driver);
668 dec_esp_platform_probe();
671 pr_info("ESP: Total of %d ESP hosts found, "
672 "%d actually in use.\n", nesps, esps_in_use);
673 esps_running = esps_in_use;
679 static void __exit dec_esp_exit(void)
681 dec_esp_platform_remove();
682 tc_unregister_driver(&dec_esp_tc_driver);
686 module_init(dec_esp_init);
687 module_exit(dec_esp_exit);