1 /* $Id: floppy.h,v 1.32 2001/10/26 17:59:36 davem Exp $
2 * asm-sparc64/floppy.h: Sparc specific parts of the Floppy driver.
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 * Ultra/PCI support added: Sep 1997 Eddie C. Dost (ecd@skynet.be)
10 #ifndef __ASM_SPARC64_FLOPPY_H
11 #define __ASM_SPARC64_FLOPPY_H
13 #include <linux/config.h>
14 #include <linux/init.h>
17 #include <asm/pgtable.h>
18 #include <asm/system.h>
19 #include <asm/idprom.h>
20 #include <asm/oplib.h>
21 #include <asm/auxio.h>
27 * Define this to enable exchanging drive 0 and 1 if only drive 1 is
28 * probed on PCI machines.
30 #undef PCI_FDC_SWAP_DRIVES
34 * 1) Netbsd Sun floppy driver.
35 * 2) NCR 82077 controller manual
36 * 3) Intel 82077 controller manual
38 struct sun_flpy_controller {
39 volatile unsigned char status1_82077; /* Auxiliary Status reg. 1 */
40 volatile unsigned char status2_82077; /* Auxiliary Status reg. 2 */
41 volatile unsigned char dor_82077; /* Digital Output reg. */
42 volatile unsigned char tapectl_82077; /* Tape Control reg */
43 volatile unsigned char status_82077; /* Main Status Register. */
44 #define drs_82077 status_82077 /* Digital Rate Select reg. */
45 volatile unsigned char data_82077; /* Data fifo. */
46 volatile unsigned char ___unused;
47 volatile unsigned char dir_82077; /* Digital Input reg. */
48 #define dcr_82077 dir_82077 /* Config Control reg. */
51 /* You'll only ever find one controller on an Ultra anyways. */
52 static struct sun_flpy_controller *sun_fdc = (struct sun_flpy_controller *)-1;
53 unsigned long fdc_status;
54 static struct sbus_dev *floppy_sdev = NULL;
56 struct sun_floppy_ops {
57 unsigned char (*fd_inb) (unsigned long port);
58 void (*fd_outb) (unsigned char value, unsigned long port);
59 void (*fd_enable_dma) (void);
60 void (*fd_disable_dma) (void);
61 void (*fd_set_dma_mode) (int);
62 void (*fd_set_dma_addr) (char *);
63 void (*fd_set_dma_count) (int);
64 unsigned int (*get_dma_residue) (void);
65 int (*fd_request_irq) (void);
66 void (*fd_free_irq) (void);
67 int (*fd_eject) (int);
70 static struct sun_floppy_ops sun_fdops;
72 #define fd_inb(port) sun_fdops.fd_inb(port)
73 #define fd_outb(value,port) sun_fdops.fd_outb(value,port)
74 #define fd_enable_dma() sun_fdops.fd_enable_dma()
75 #define fd_disable_dma() sun_fdops.fd_disable_dma()
76 #define fd_request_dma() (0) /* nothing... */
77 #define fd_free_dma() /* nothing... */
78 #define fd_clear_dma_ff() /* nothing... */
79 #define fd_set_dma_mode(mode) sun_fdops.fd_set_dma_mode(mode)
80 #define fd_set_dma_addr(addr) sun_fdops.fd_set_dma_addr(addr)
81 #define fd_set_dma_count(count) sun_fdops.fd_set_dma_count(count)
82 #define get_dma_residue(x) sun_fdops.get_dma_residue()
83 #define fd_cacheflush(addr, size) /* nothing... */
84 #define fd_request_irq() sun_fdops.fd_request_irq()
85 #define fd_free_irq() sun_fdops.fd_free_irq()
86 #define fd_eject(drive) sun_fdops.fd_eject(drive)
88 static int FLOPPY_MOTOR_MASK = 0x10;
90 /* Super paranoid... */
91 #undef HAVE_DISABLE_HLT
93 static int sun_floppy_types[2] = { 0, 0 };
95 /* Here is where we catch the floppy driver trying to initialize,
96 * therefore this is where we call the PROM device tree probing
97 * routine etc. on the Sparc.
99 #define FLOPPY0_TYPE sun_floppy_init()
100 #define FLOPPY1_TYPE sun_floppy_types[1]
102 #define FDC1 ((unsigned long)sun_fdc)
107 /* No 64k boundary crossing problems on the Sparc. */
108 #define CROSS_64KB(a,s) (0)
110 static unsigned char sun_82077_fd_inb(unsigned long port)
115 printk("floppy: Asked to read unknown port %lx\n", port);
116 panic("floppy: Port bolixed.");
117 case 4: /* FD_STATUS */
118 return sbus_readb(&sun_fdc->status_82077) & ~STATUS_DMA;
119 case 5: /* FD_DATA */
120 return sbus_readb(&sun_fdc->data_82077);
122 /* XXX: Is DCL on 0x80 in sun4m? */
123 return sbus_readb(&sun_fdc->dir_82077);
125 panic("sun_82072_fd_inb: How did I get here?");
128 static void sun_82077_fd_outb(unsigned char value, unsigned long port)
133 printk("floppy: Asked to write to unknown port %lx\n", port);
134 panic("floppy: Port bolixed.");
136 /* Happily, the 82077 has a real DOR register. */
137 sbus_writeb(value, &sun_fdc->dor_82077);
139 case 5: /* FD_DATA */
140 sbus_writeb(value, &sun_fdc->data_82077);
143 sbus_writeb(value, &sun_fdc->dcr_82077);
145 case 4: /* FD_STATUS */
146 sbus_writeb(value, &sun_fdc->status_82077);
152 /* For pseudo-dma (Sun floppy drives have no real DMA available to
153 * them so we must eat the data fifo bytes directly ourselves) we have
154 * three state variables. doing_pdma tells our inline low-level
155 * assembly floppy interrupt entry point whether it should sit and eat
156 * bytes from the fifo or just transfer control up to the higher level
157 * floppy interrupt c-code. I tried very hard but I could not get the
158 * pseudo-dma to work in c-code without getting many overruns and
159 * underruns. If non-zero, doing_pdma encodes the direction of
160 * the transfer for debugging. 1=read 2=write
162 unsigned char *pdma_vaddr;
163 unsigned long pdma_size;
164 volatile int doing_pdma = 0;
166 /* This is software state */
167 char *pdma_base = NULL;
168 unsigned long pdma_areasize;
170 /* Common routines to all controller types on the Sparc. */
171 static void sun_fd_disable_dma(void)
175 mmu_unlockarea(pdma_base, pdma_areasize);
180 static void sun_fd_set_dma_mode(int mode)
190 printk("Unknown dma mode %d\n", mode);
191 panic("floppy: Giving up...");
195 static void sun_fd_set_dma_addr(char *buffer)
200 static void sun_fd_set_dma_count(int length)
205 static void sun_fd_enable_dma(void)
207 pdma_vaddr = mmu_lockarea(pdma_vaddr, pdma_size);
208 pdma_base = pdma_vaddr;
209 pdma_areasize = pdma_size;
212 extern irqreturn_t sparc_floppy_irq(int, void *, struct pt_regs *);
214 static int sun_fd_request_irq(void)
222 error = request_irq(FLOPPY_IRQ, sparc_floppy_irq,
223 SA_INTERRUPT, "floppy", NULL);
225 return ((error == 0) ? 0 : -1);
230 static void sun_fd_free_irq(void)
234 static unsigned int sun_get_dma_residue(void)
236 /* XXX This isn't really correct. XXX */
240 static int sun_fd_eject(int drive)
242 set_dor(0x00, 0xff, 0x90);
244 set_dor(0x00, 0x6f, 0x00);
250 #include <asm/ebus.h>
252 #include <asm/ns87303.h>
254 static struct ebus_dma_info sun_pci_fd_ebus_dma;
255 static struct pci_dev *sun_pci_ebus_dev;
256 static int sun_pci_broken_drive = -1;
258 struct sun_pci_dma_op {
264 static struct sun_pci_dma_op sun_pci_dma_current = { -1U, 0, 0, NULL};
265 static struct sun_pci_dma_op sun_pci_dma_pending = { -1U, 0, 0, NULL};
267 extern irqreturn_t floppy_interrupt(int irq, void *dev_id, struct pt_regs *regs);
269 static unsigned char sun_pci_fd_inb(unsigned long port)
275 static void sun_pci_fd_outb(unsigned char val, unsigned long port)
281 static void sun_pci_fd_broken_outb(unsigned char val, unsigned long port)
285 * XXX: Due to SUN's broken floppy connector on AX and AXi
286 * we need to turn on MOTOR_0 also, if the floppy is
287 * jumpered to DS1 (like most PC floppies are). I hope
288 * this does not hurt correct hardware like the AXmp.
289 * (Eddie, Sep 12 1998).
291 if (port == ((unsigned long)sun_fdc) + 2) {
292 if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x20)) {
299 #ifdef PCI_FDC_SWAP_DRIVES
300 static void sun_pci_fd_lde_broken_outb(unsigned char val, unsigned long port)
304 * XXX: Due to SUN's broken floppy connector on AX and AXi
305 * we need to turn on MOTOR_0 also, if the floppy is
306 * jumpered to DS1 (like most PC floppies are). I hope
307 * this does not hurt correct hardware like the AXmp.
308 * (Eddie, Sep 12 1998).
310 if (port == ((unsigned long)sun_fdc) + 2) {
311 if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x10)) {
318 #endif /* PCI_FDC_SWAP_DRIVES */
320 static void sun_pci_fd_enable_dma(void)
322 BUG_ON((NULL == sun_pci_dma_pending.buf) ||
323 (0 == sun_pci_dma_pending.len) ||
324 (0 == sun_pci_dma_pending.direction));
326 sun_pci_dma_current.buf = sun_pci_dma_pending.buf;
327 sun_pci_dma_current.len = sun_pci_dma_pending.len;
328 sun_pci_dma_current.direction = sun_pci_dma_pending.direction;
330 sun_pci_dma_pending.buf = NULL;
331 sun_pci_dma_pending.len = 0;
332 sun_pci_dma_pending.direction = 0;
333 sun_pci_dma_pending.addr = -1U;
335 sun_pci_dma_current.addr =
336 pci_map_single(sun_pci_ebus_dev,
337 sun_pci_dma_current.buf,
338 sun_pci_dma_current.len,
339 sun_pci_dma_current.direction);
341 ebus_dma_enable(&sun_pci_fd_ebus_dma, 1);
343 if (ebus_dma_request(&sun_pci_fd_ebus_dma,
344 sun_pci_dma_current.addr,
345 sun_pci_dma_current.len))
349 static void sun_pci_fd_disable_dma(void)
351 ebus_dma_enable(&sun_pci_fd_ebus_dma, 0);
352 if (sun_pci_dma_current.addr != -1U)
353 pci_unmap_single(sun_pci_ebus_dev,
354 sun_pci_dma_current.addr,
355 sun_pci_dma_current.len,
356 sun_pci_dma_current.direction);
357 sun_pci_dma_current.addr = -1U;
360 static void sun_pci_fd_set_dma_mode(int mode)
362 if (mode == DMA_MODE_WRITE)
363 sun_pci_dma_pending.direction = PCI_DMA_TODEVICE;
365 sun_pci_dma_pending.direction = PCI_DMA_FROMDEVICE;
367 ebus_dma_prepare(&sun_pci_fd_ebus_dma, mode != DMA_MODE_WRITE);
370 static void sun_pci_fd_set_dma_count(int length)
372 sun_pci_dma_pending.len = length;
375 static void sun_pci_fd_set_dma_addr(char *buffer)
377 sun_pci_dma_pending.buf = buffer;
380 static unsigned int sun_pci_get_dma_residue(void)
382 return ebus_dma_residue(&sun_pci_fd_ebus_dma);
385 static int sun_pci_fd_request_irq(void)
387 return ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 1);
390 static void sun_pci_fd_free_irq(void)
392 ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 0);
395 static int sun_pci_fd_eject(int drive)
400 void sun_pci_fd_dma_callback(struct ebus_dma_info *p, int event, void *cookie)
402 floppy_interrupt(0, NULL, NULL);
406 * Floppy probing, we'd like to use /dev/fd0 for a single Floppy on PCI,
407 * even if this is configured using DS1, thus looks like /dev/fd1 with
408 * the cabling used in Ultras.
410 #define DOR (port + 2)
411 #define MSR (port + 4)
412 #define FIFO (port + 5)
414 static void sun_pci_fd_out_byte(unsigned long port, unsigned char val,
417 unsigned char status;
420 while (!((status = inb(MSR)) & 0x80) && --timeout)
425 static unsigned char sun_pci_fd_sensei(unsigned long port)
427 unsigned char result[2] = { 0x70, 0x00 };
428 unsigned char status;
431 sun_pci_fd_out_byte(port, 0x08, FIFO);
435 while (!((status = inb(MSR)) & 0x80) && --timeout)
441 if ((status & 0xf0) == 0xd0)
442 result[i++] = inb(FIFO);
450 static void sun_pci_fd_reset(unsigned long port)
452 unsigned char mask = 0x00;
453 unsigned char status;
458 status = sun_pci_fd_sensei(port);
459 if ((status & 0xc0) == 0xc0)
460 mask |= 1 << (status & 0x03);
463 } while ((mask != 0x0f) && --timeout);
466 static int sun_pci_fd_test_drive(unsigned long port, int drive)
468 unsigned char status, data;
472 sun_pci_fd_reset(port);
474 data = (0x10 << drive) | 0x0c | drive;
475 sun_pci_fd_out_byte(port, data, DOR);
477 sun_pci_fd_out_byte(port, 0x07, FIFO);
478 sun_pci_fd_out_byte(port, drive & 0x03, FIFO);
482 status = sun_pci_fd_sensei(port);
483 } while (((status & 0xc0) == 0x80) && --timeout);
488 ready = (status & 0x10) ? 0 : 1;
490 sun_pci_fd_reset(port);
497 #endif /* CONFIG_PCI */
500 static int __init ebus_fdthree_p(struct linux_ebus_device *edev)
502 if (!strcmp(edev->prom_name, "fdthree"))
504 if (!strcmp(edev->prom_name, "floppy")) {
506 prom_getstring(edev->prom_node,
508 compat, sizeof(compat));
510 if (!strcmp(compat, "fdthree"))
518 #undef ISA_FLOPPY_WORKS
520 #ifdef ISA_FLOPPY_WORKS
521 static unsigned long __init isa_floppy_init(void)
523 struct sparc_isa_bridge *isa_br;
524 struct sparc_isa_device *isa_dev = NULL;
526 for_each_isa(isa_br) {
527 for_each_isadev(isa_dev, isa_br) {
528 if (!strcmp(isa_dev->prom_name, "dma")) {
529 struct sparc_isa_device *child =
533 if (!strcmp(child->prom_name,
547 /* We could use DMA on devices behind the ISA bridge, but...
549 * There is a slight problem. Normally on x86 kit the x86 processor
550 * delays I/O port instructions when the ISA bus "dma in progress"
551 * signal is active. Well, sparc64 systems do not monitor this
552 * signal thus we would need to block all I/O port accesses in software
553 * when a dma transfer is active for some device.
556 sun_fdc = (struct sun_flpy_controller *)isa_dev->resource.start;
557 FLOPPY_IRQ = isa_dev->irq;
559 sun_fdops.fd_inb = sun_pci_fd_inb;
560 sun_fdops.fd_outb = sun_pci_fd_outb;
562 can_use_virtual_dma = use_virtual_dma = 1;
563 sun_fdops.fd_enable_dma = sun_fd_enable_dma;
564 sun_fdops.fd_disable_dma = sun_fd_disable_dma;
565 sun_fdops.fd_set_dma_mode = sun_fd_set_dma_mode;
566 sun_fdops.fd_set_dma_addr = sun_fd_set_dma_addr;
567 sun_fdops.fd_set_dma_count = sun_fd_set_dma_count;
568 sun_fdops.get_dma_residue = sun_get_dma_residue;
570 sun_fdops.fd_request_irq = sun_fd_request_irq;
571 sun_fdops.fd_free_irq = sun_fd_free_irq;
573 /* Floppy eject is manual. Actually, could determine this
574 * via presence of 'manual' property in OBP node.
576 sun_fdops.fd_eject = sun_pci_fd_eject;
578 fdc_status = (unsigned long) &sun_fdc->status_82077;
579 FLOPPY_MOTOR_MASK = 0xf0;
581 allowed_drive_mask = 0;
582 sun_floppy_types[0] = 0;
583 sun_floppy_types[1] = 4;
585 sun_pci_broken_drive = 1;
586 sun_fdops.fd_outb = sun_pci_fd_broken_outb;
588 return sun_floppy_types[0];
590 #endif /* ISA_FLOPPY_WORKS */
594 static unsigned long __init sun_floppy_init(void)
597 struct sbus_bus *bus;
598 struct sbus_dev *sdev = NULL;
599 static int initialized = 0;
602 return sun_floppy_types[0];
605 for_all_sbusdev (sdev, bus) {
606 if (!strcmp(sdev->prom_name, "SUNW,fdtwo"))
611 FLOPPY_IRQ = sdev->irqs[0];
614 struct linux_ebus *ebus;
615 struct linux_ebus_device *edev = NULL;
616 unsigned long config = 0;
617 void __iomem *auxio_reg;
619 for_each_ebus(ebus) {
620 for_each_ebusdev(edev, ebus) {
621 if (ebus_fdthree_p(edev))
627 #ifdef ISA_FLOPPY_WORKS
628 return isa_floppy_init();
634 prom_getproperty(edev->prom_node, "status",
635 state, sizeof(state));
636 if (!strncmp(state, "disabled", 8))
639 FLOPPY_IRQ = edev->irqs[0];
641 /* Make sure the high density bit is set, some systems
642 * (most notably Ultra5/Ultra10) come up with it clear.
644 auxio_reg = (void __iomem *) edev->resource[2].start;
645 writel(readl(auxio_reg)|0x2, auxio_reg);
647 sun_pci_ebus_dev = ebus->self;
649 spin_lock_init(&sun_pci_fd_ebus_dma.lock);
652 sun_pci_fd_ebus_dma.regs = (void __iomem *)
653 edev->resource[1].start;
654 if (!sun_pci_fd_ebus_dma.regs)
657 sun_pci_fd_ebus_dma.flags = (EBUS_DMA_FLAG_USE_EBDMA_HANDLER |
658 EBUS_DMA_FLAG_TCI_DISABLE);
659 sun_pci_fd_ebus_dma.callback = sun_pci_fd_dma_callback;
660 sun_pci_fd_ebus_dma.client_cookie = NULL;
661 sun_pci_fd_ebus_dma.irq = FLOPPY_IRQ;
662 strcpy(sun_pci_fd_ebus_dma.name, "floppy");
663 if (ebus_dma_register(&sun_pci_fd_ebus_dma))
667 sun_fdc = (struct sun_flpy_controller *)edev->resource[0].start;
669 sun_fdops.fd_inb = sun_pci_fd_inb;
670 sun_fdops.fd_outb = sun_pci_fd_outb;
672 can_use_virtual_dma = use_virtual_dma = 0;
673 sun_fdops.fd_enable_dma = sun_pci_fd_enable_dma;
674 sun_fdops.fd_disable_dma = sun_pci_fd_disable_dma;
675 sun_fdops.fd_set_dma_mode = sun_pci_fd_set_dma_mode;
676 sun_fdops.fd_set_dma_addr = sun_pci_fd_set_dma_addr;
677 sun_fdops.fd_set_dma_count = sun_pci_fd_set_dma_count;
678 sun_fdops.get_dma_residue = sun_pci_get_dma_residue;
680 sun_fdops.fd_request_irq = sun_pci_fd_request_irq;
681 sun_fdops.fd_free_irq = sun_pci_fd_free_irq;
683 sun_fdops.fd_eject = sun_pci_fd_eject;
685 fdc_status = (unsigned long) &sun_fdc->status_82077;
686 FLOPPY_MOTOR_MASK = 0xf0;
689 * XXX: Find out on which machines this is really needed.
692 sun_pci_broken_drive = 1;
693 sun_fdops.fd_outb = sun_pci_fd_broken_outb;
696 allowed_drive_mask = 0;
697 if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 0))
698 sun_floppy_types[0] = 4;
699 if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 1))
700 sun_floppy_types[1] = 4;
703 * Find NS87303 SuperIO config registers (through ecpp).
705 for_each_ebus(ebus) {
706 for_each_ebusdev(edev, ebus) {
707 if (!strcmp(edev->prom_name, "ecpp")) {
708 config = edev->resource[1].start;
716 * Sanity check, is this really the NS87303?
718 switch (config & 0x3ff) {
729 return sun_floppy_types[0];
731 /* Enable PC-AT mode. */
732 ns87303_modify(config, ASC, 0, 0xc0);
734 #ifdef PCI_FDC_SWAP_DRIVES
736 * If only Floppy 1 is present, swap drives.
738 if (!sun_floppy_types[0] && sun_floppy_types[1]) {
740 * Set the drive exchange bit in FCR on NS87303,
741 * make sure other bits are sane before doing so.
743 ns87303_modify(config, FER, FER_EDM, 0);
744 ns87303_modify(config, ASC, ASC_DRV2_SEL, 0);
745 ns87303_modify(config, FCR, 0, FCR_LDE);
747 config = sun_floppy_types[0];
748 sun_floppy_types[0] = sun_floppy_types[1];
749 sun_floppy_types[1] = config;
751 if (sun_pci_broken_drive != -1) {
752 sun_pci_broken_drive = 1 - sun_pci_broken_drive;
753 sun_fdops.fd_outb = sun_pci_fd_lde_broken_outb;
756 #endif /* PCI_FDC_SWAP_DRIVES */
758 return sun_floppy_types[0];
763 prom_getproperty(sdev->prom_node, "status", state, sizeof(state));
764 if(!strncmp(state, "disabled", 8))
768 * We cannot do sbus_ioremap here: it does request_region,
769 * which the generic floppy driver tries to do once again.
770 * But we must use the sdev resource values as they have
771 * had parent ranges applied.
773 sun_fdc = (struct sun_flpy_controller *)
774 (sdev->resource[0].start +
775 ((sdev->resource[0].flags & 0x1ffUL) << 32UL));
777 /* Last minute sanity check... */
778 if(sbus_readb(&sun_fdc->status1_82077) == 0xff) {
779 sun_fdc = (struct sun_flpy_controller *)-1;
783 sun_fdops.fd_inb = sun_82077_fd_inb;
784 sun_fdops.fd_outb = sun_82077_fd_outb;
786 can_use_virtual_dma = use_virtual_dma = 1;
787 sun_fdops.fd_enable_dma = sun_fd_enable_dma;
788 sun_fdops.fd_disable_dma = sun_fd_disable_dma;
789 sun_fdops.fd_set_dma_mode = sun_fd_set_dma_mode;
790 sun_fdops.fd_set_dma_addr = sun_fd_set_dma_addr;
791 sun_fdops.fd_set_dma_count = sun_fd_set_dma_count;
792 sun_fdops.get_dma_residue = sun_get_dma_residue;
794 sun_fdops.fd_request_irq = sun_fd_request_irq;
795 sun_fdops.fd_free_irq = sun_fd_free_irq;
797 sun_fdops.fd_eject = sun_fd_eject;
799 fdc_status = (unsigned long) &sun_fdc->status_82077;
802 allowed_drive_mask = 0x01;
803 sun_floppy_types[0] = 4;
804 sun_floppy_types[1] = 0;
806 return sun_floppy_types[0];
809 #define EXTRA_FLOPPY_PARAMS
811 #endif /* !(__ASM_SPARC64_FLOPPY_H) */