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/init.h>
16 #include <asm/pgtable.h>
17 #include <asm/system.h>
18 #include <asm/idprom.h>
19 #include <asm/oplib.h>
20 #include <asm/auxio.h>
26 * Define this to enable exchanging drive 0 and 1 if only drive 1 is
27 * probed on PCI machines.
29 #undef PCI_FDC_SWAP_DRIVES
33 * 1) Netbsd Sun floppy driver.
34 * 2) NCR 82077 controller manual
35 * 3) Intel 82077 controller manual
37 struct sun_flpy_controller {
38 volatile unsigned char status1_82077; /* Auxiliary Status reg. 1 */
39 volatile unsigned char status2_82077; /* Auxiliary Status reg. 2 */
40 volatile unsigned char dor_82077; /* Digital Output reg. */
41 volatile unsigned char tapectl_82077; /* Tape Control reg */
42 volatile unsigned char status_82077; /* Main Status Register. */
43 #define drs_82077 status_82077 /* Digital Rate Select reg. */
44 volatile unsigned char data_82077; /* Data fifo. */
45 volatile unsigned char ___unused;
46 volatile unsigned char dir_82077; /* Digital Input reg. */
47 #define dcr_82077 dir_82077 /* Config Control reg. */
50 /* You'll only ever find one controller on an Ultra anyways. */
51 static struct sun_flpy_controller *sun_fdc = (struct sun_flpy_controller *)-1;
52 unsigned long fdc_status;
53 static struct sbus_dev *floppy_sdev = NULL;
55 struct sun_floppy_ops {
56 unsigned char (*fd_inb) (unsigned long port);
57 void (*fd_outb) (unsigned char value, unsigned long port);
58 void (*fd_enable_dma) (void);
59 void (*fd_disable_dma) (void);
60 void (*fd_set_dma_mode) (int);
61 void (*fd_set_dma_addr) (char *);
62 void (*fd_set_dma_count) (int);
63 unsigned int (*get_dma_residue) (void);
64 int (*fd_request_irq) (void);
65 void (*fd_free_irq) (void);
66 int (*fd_eject) (int);
69 static struct sun_floppy_ops sun_fdops;
71 #define fd_inb(port) sun_fdops.fd_inb(port)
72 #define fd_outb(value,port) sun_fdops.fd_outb(value,port)
73 #define fd_enable_dma() sun_fdops.fd_enable_dma()
74 #define fd_disable_dma() sun_fdops.fd_disable_dma()
75 #define fd_request_dma() (0) /* nothing... */
76 #define fd_free_dma() /* nothing... */
77 #define fd_clear_dma_ff() /* nothing... */
78 #define fd_set_dma_mode(mode) sun_fdops.fd_set_dma_mode(mode)
79 #define fd_set_dma_addr(addr) sun_fdops.fd_set_dma_addr(addr)
80 #define fd_set_dma_count(count) sun_fdops.fd_set_dma_count(count)
81 #define get_dma_residue(x) sun_fdops.get_dma_residue()
82 #define fd_cacheflush(addr, size) /* nothing... */
83 #define fd_request_irq() sun_fdops.fd_request_irq()
84 #define fd_free_irq() sun_fdops.fd_free_irq()
85 #define fd_eject(drive) sun_fdops.fd_eject(drive)
87 static int FLOPPY_MOTOR_MASK = 0x10;
89 /* Super paranoid... */
90 #undef HAVE_DISABLE_HLT
92 static int sun_floppy_types[2] = { 0, 0 };
94 /* Here is where we catch the floppy driver trying to initialize,
95 * therefore this is where we call the PROM device tree probing
96 * routine etc. on the Sparc.
98 #define FLOPPY0_TYPE sun_floppy_init()
99 #define FLOPPY1_TYPE sun_floppy_types[1]
101 #define FDC1 ((unsigned long)sun_fdc)
106 /* No 64k boundary crossing problems on the Sparc. */
107 #define CROSS_64KB(a,s) (0)
109 static unsigned char sun_82077_fd_inb(unsigned long port)
114 printk("floppy: Asked to read unknown port %lx\n", port);
115 panic("floppy: Port bolixed.");
116 case 4: /* FD_STATUS */
117 return sbus_readb(&sun_fdc->status_82077) & ~STATUS_DMA;
118 case 5: /* FD_DATA */
119 return sbus_readb(&sun_fdc->data_82077);
121 /* XXX: Is DCL on 0x80 in sun4m? */
122 return sbus_readb(&sun_fdc->dir_82077);
124 panic("sun_82072_fd_inb: How did I get here?");
127 static void sun_82077_fd_outb(unsigned char value, unsigned long port)
132 printk("floppy: Asked to write to unknown port %lx\n", port);
133 panic("floppy: Port bolixed.");
135 /* Happily, the 82077 has a real DOR register. */
136 sbus_writeb(value, &sun_fdc->dor_82077);
138 case 5: /* FD_DATA */
139 sbus_writeb(value, &sun_fdc->data_82077);
142 sbus_writeb(value, &sun_fdc->dcr_82077);
144 case 4: /* FD_STATUS */
145 sbus_writeb(value, &sun_fdc->status_82077);
151 /* For pseudo-dma (Sun floppy drives have no real DMA available to
152 * them so we must eat the data fifo bytes directly ourselves) we have
153 * three state variables. doing_pdma tells our inline low-level
154 * assembly floppy interrupt entry point whether it should sit and eat
155 * bytes from the fifo or just transfer control up to the higher level
156 * floppy interrupt c-code. I tried very hard but I could not get the
157 * pseudo-dma to work in c-code without getting many overruns and
158 * underruns. If non-zero, doing_pdma encodes the direction of
159 * the transfer for debugging. 1=read 2=write
161 unsigned char *pdma_vaddr;
162 unsigned long pdma_size;
163 volatile int doing_pdma = 0;
165 /* This is software state */
166 char *pdma_base = NULL;
167 unsigned long pdma_areasize;
169 /* Common routines to all controller types on the Sparc. */
170 static void sun_fd_disable_dma(void)
174 mmu_unlockarea(pdma_base, pdma_areasize);
179 static void sun_fd_set_dma_mode(int mode)
189 printk("Unknown dma mode %d\n", mode);
190 panic("floppy: Giving up...");
194 static void sun_fd_set_dma_addr(char *buffer)
199 static void sun_fd_set_dma_count(int length)
204 static void sun_fd_enable_dma(void)
206 pdma_vaddr = mmu_lockarea(pdma_vaddr, pdma_size);
207 pdma_base = pdma_vaddr;
208 pdma_areasize = pdma_size;
211 extern irqreturn_t sparc_floppy_irq(int, void *, struct pt_regs *);
213 static int sun_fd_request_irq(void)
221 error = request_irq(FLOPPY_IRQ, sparc_floppy_irq,
222 SA_INTERRUPT, "floppy", NULL);
224 return ((error == 0) ? 0 : -1);
229 static void sun_fd_free_irq(void)
233 static unsigned int sun_get_dma_residue(void)
235 /* XXX This isn't really correct. XXX */
239 static int sun_fd_eject(int drive)
241 set_dor(0x00, 0xff, 0x90);
243 set_dor(0x00, 0x6f, 0x00);
249 #include <asm/ebus.h>
251 #include <asm/ns87303.h>
253 static struct ebus_dma_info sun_pci_fd_ebus_dma;
254 static struct pci_dev *sun_pci_ebus_dev;
255 static int sun_pci_broken_drive = -1;
257 struct sun_pci_dma_op {
263 static struct sun_pci_dma_op sun_pci_dma_current = { -1U, 0, 0, NULL};
264 static struct sun_pci_dma_op sun_pci_dma_pending = { -1U, 0, 0, NULL};
266 extern irqreturn_t floppy_interrupt(int irq, void *dev_id, struct pt_regs *regs);
268 static unsigned char sun_pci_fd_inb(unsigned long port)
274 static void sun_pci_fd_outb(unsigned char val, unsigned long port)
280 static void sun_pci_fd_broken_outb(unsigned char val, unsigned long port)
284 * XXX: Due to SUN's broken floppy connector on AX and AXi
285 * we need to turn on MOTOR_0 also, if the floppy is
286 * jumpered to DS1 (like most PC floppies are). I hope
287 * this does not hurt correct hardware like the AXmp.
288 * (Eddie, Sep 12 1998).
290 if (port == ((unsigned long)sun_fdc) + 2) {
291 if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x20)) {
298 #ifdef PCI_FDC_SWAP_DRIVES
299 static void sun_pci_fd_lde_broken_outb(unsigned char val, unsigned long port)
303 * XXX: Due to SUN's broken floppy connector on AX and AXi
304 * we need to turn on MOTOR_0 also, if the floppy is
305 * jumpered to DS1 (like most PC floppies are). I hope
306 * this does not hurt correct hardware like the AXmp.
307 * (Eddie, Sep 12 1998).
309 if (port == ((unsigned long)sun_fdc) + 2) {
310 if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x10)) {
317 #endif /* PCI_FDC_SWAP_DRIVES */
319 static void sun_pci_fd_enable_dma(void)
321 BUG_ON((NULL == sun_pci_dma_pending.buf) ||
322 (0 == sun_pci_dma_pending.len) ||
323 (0 == sun_pci_dma_pending.direction));
325 sun_pci_dma_current.buf = sun_pci_dma_pending.buf;
326 sun_pci_dma_current.len = sun_pci_dma_pending.len;
327 sun_pci_dma_current.direction = sun_pci_dma_pending.direction;
329 sun_pci_dma_pending.buf = NULL;
330 sun_pci_dma_pending.len = 0;
331 sun_pci_dma_pending.direction = 0;
332 sun_pci_dma_pending.addr = -1U;
334 sun_pci_dma_current.addr =
335 pci_map_single(sun_pci_ebus_dev,
336 sun_pci_dma_current.buf,
337 sun_pci_dma_current.len,
338 sun_pci_dma_current.direction);
340 ebus_dma_enable(&sun_pci_fd_ebus_dma, 1);
342 if (ebus_dma_request(&sun_pci_fd_ebus_dma,
343 sun_pci_dma_current.addr,
344 sun_pci_dma_current.len))
348 static void sun_pci_fd_disable_dma(void)
350 ebus_dma_enable(&sun_pci_fd_ebus_dma, 0);
351 if (sun_pci_dma_current.addr != -1U)
352 pci_unmap_single(sun_pci_ebus_dev,
353 sun_pci_dma_current.addr,
354 sun_pci_dma_current.len,
355 sun_pci_dma_current.direction);
356 sun_pci_dma_current.addr = -1U;
359 static void sun_pci_fd_set_dma_mode(int mode)
361 if (mode == DMA_MODE_WRITE)
362 sun_pci_dma_pending.direction = PCI_DMA_TODEVICE;
364 sun_pci_dma_pending.direction = PCI_DMA_FROMDEVICE;
366 ebus_dma_prepare(&sun_pci_fd_ebus_dma, mode != DMA_MODE_WRITE);
369 static void sun_pci_fd_set_dma_count(int length)
371 sun_pci_dma_pending.len = length;
374 static void sun_pci_fd_set_dma_addr(char *buffer)
376 sun_pci_dma_pending.buf = buffer;
379 static unsigned int sun_pci_get_dma_residue(void)
381 return ebus_dma_residue(&sun_pci_fd_ebus_dma);
384 static int sun_pci_fd_request_irq(void)
386 return ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 1);
389 static void sun_pci_fd_free_irq(void)
391 ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 0);
394 static int sun_pci_fd_eject(int drive)
399 void sun_pci_fd_dma_callback(struct ebus_dma_info *p, int event, void *cookie)
401 floppy_interrupt(0, NULL, NULL);
405 * Floppy probing, we'd like to use /dev/fd0 for a single Floppy on PCI,
406 * even if this is configured using DS1, thus looks like /dev/fd1 with
407 * the cabling used in Ultras.
409 #define DOR (port + 2)
410 #define MSR (port + 4)
411 #define FIFO (port + 5)
413 static void sun_pci_fd_out_byte(unsigned long port, unsigned char val,
416 unsigned char status;
419 while (!((status = inb(MSR)) & 0x80) && --timeout)
424 static unsigned char sun_pci_fd_sensei(unsigned long port)
426 unsigned char result[2] = { 0x70, 0x00 };
427 unsigned char status;
430 sun_pci_fd_out_byte(port, 0x08, FIFO);
434 while (!((status = inb(MSR)) & 0x80) && --timeout)
440 if ((status & 0xf0) == 0xd0)
441 result[i++] = inb(FIFO);
449 static void sun_pci_fd_reset(unsigned long port)
451 unsigned char mask = 0x00;
452 unsigned char status;
457 status = sun_pci_fd_sensei(port);
458 if ((status & 0xc0) == 0xc0)
459 mask |= 1 << (status & 0x03);
462 } while ((mask != 0x0f) && --timeout);
465 static int sun_pci_fd_test_drive(unsigned long port, int drive)
467 unsigned char status, data;
471 sun_pci_fd_reset(port);
473 data = (0x10 << drive) | 0x0c | drive;
474 sun_pci_fd_out_byte(port, data, DOR);
476 sun_pci_fd_out_byte(port, 0x07, FIFO);
477 sun_pci_fd_out_byte(port, drive & 0x03, FIFO);
481 status = sun_pci_fd_sensei(port);
482 } while (((status & 0xc0) == 0x80) && --timeout);
487 ready = (status & 0x10) ? 0 : 1;
489 sun_pci_fd_reset(port);
496 #endif /* CONFIG_PCI */
499 static int __init ebus_fdthree_p(struct linux_ebus_device *edev)
501 if (!strcmp(edev->prom_node->name, "fdthree"))
503 if (!strcmp(edev->prom_node->name, "floppy")) {
506 compat = of_get_property(edev->prom_node,
508 if (compat && !strcmp(compat, "fdthree"))
516 #undef ISA_FLOPPY_WORKS
518 #ifdef ISA_FLOPPY_WORKS
519 static unsigned long __init isa_floppy_init(void)
521 struct sparc_isa_bridge *isa_br;
522 struct sparc_isa_device *isa_dev = NULL;
524 for_each_isa(isa_br) {
525 for_each_isadev(isa_dev, isa_br) {
526 if (!strcmp(isa_dev->prom_node->name, "dma")) {
527 struct sparc_isa_device *child =
531 if (!strcmp(child->prom_node->name,
545 /* We could use DMA on devices behind the ISA bridge, but...
547 * There is a slight problem. Normally on x86 kit the x86 processor
548 * delays I/O port instructions when the ISA bus "dma in progress"
549 * signal is active. Well, sparc64 systems do not monitor this
550 * signal thus we would need to block all I/O port accesses in software
551 * when a dma transfer is active for some device.
554 sun_fdc = (struct sun_flpy_controller *)isa_dev->resource.start;
555 FLOPPY_IRQ = isa_dev->irq;
557 sun_fdops.fd_inb = sun_pci_fd_inb;
558 sun_fdops.fd_outb = sun_pci_fd_outb;
560 can_use_virtual_dma = use_virtual_dma = 1;
561 sun_fdops.fd_enable_dma = sun_fd_enable_dma;
562 sun_fdops.fd_disable_dma = sun_fd_disable_dma;
563 sun_fdops.fd_set_dma_mode = sun_fd_set_dma_mode;
564 sun_fdops.fd_set_dma_addr = sun_fd_set_dma_addr;
565 sun_fdops.fd_set_dma_count = sun_fd_set_dma_count;
566 sun_fdops.get_dma_residue = sun_get_dma_residue;
568 sun_fdops.fd_request_irq = sun_fd_request_irq;
569 sun_fdops.fd_free_irq = sun_fd_free_irq;
571 /* Floppy eject is manual. Actually, could determine this
572 * via presence of 'manual' property in OBP node.
574 sun_fdops.fd_eject = sun_pci_fd_eject;
576 fdc_status = (unsigned long) &sun_fdc->status_82077;
577 FLOPPY_MOTOR_MASK = 0xf0;
579 allowed_drive_mask = 0;
580 sun_floppy_types[0] = 0;
581 sun_floppy_types[1] = 4;
583 sun_pci_broken_drive = 1;
584 sun_fdops.fd_outb = sun_pci_fd_broken_outb;
586 return sun_floppy_types[0];
588 #endif /* ISA_FLOPPY_WORKS */
592 static unsigned long __init sun_floppy_init(void)
595 struct sbus_bus *bus;
596 struct sbus_dev *sdev = NULL;
597 static int initialized = 0;
600 return sun_floppy_types[0];
603 for_all_sbusdev (sdev, bus) {
604 if (!strcmp(sdev->prom_name, "SUNW,fdtwo"))
609 FLOPPY_IRQ = sdev->irqs[0];
612 struct linux_ebus *ebus;
613 struct linux_ebus_device *edev = NULL;
614 unsigned long config = 0;
615 void __iomem *auxio_reg;
618 for_each_ebus(ebus) {
619 for_each_ebusdev(edev, ebus) {
620 if (ebus_fdthree_p(edev))
626 #ifdef ISA_FLOPPY_WORKS
627 return isa_floppy_init();
633 state_prop = of_get_property(edev->prom_node, "status", NULL);
634 if (state_prop && !strncmp(state_prop, "disabled", 8))
637 FLOPPY_IRQ = edev->irqs[0];
639 /* Make sure the high density bit is set, some systems
640 * (most notably Ultra5/Ultra10) come up with it clear.
642 auxio_reg = (void __iomem *) edev->resource[2].start;
643 writel(readl(auxio_reg)|0x2, auxio_reg);
645 sun_pci_ebus_dev = ebus->self;
647 spin_lock_init(&sun_pci_fd_ebus_dma.lock);
650 sun_pci_fd_ebus_dma.regs = (void __iomem *)
651 edev->resource[1].start;
652 if (!sun_pci_fd_ebus_dma.regs)
655 sun_pci_fd_ebus_dma.flags = (EBUS_DMA_FLAG_USE_EBDMA_HANDLER |
656 EBUS_DMA_FLAG_TCI_DISABLE);
657 sun_pci_fd_ebus_dma.callback = sun_pci_fd_dma_callback;
658 sun_pci_fd_ebus_dma.client_cookie = NULL;
659 sun_pci_fd_ebus_dma.irq = FLOPPY_IRQ;
660 strcpy(sun_pci_fd_ebus_dma.name, "floppy");
661 if (ebus_dma_register(&sun_pci_fd_ebus_dma))
665 sun_fdc = (struct sun_flpy_controller *)edev->resource[0].start;
667 sun_fdops.fd_inb = sun_pci_fd_inb;
668 sun_fdops.fd_outb = sun_pci_fd_outb;
670 can_use_virtual_dma = use_virtual_dma = 0;
671 sun_fdops.fd_enable_dma = sun_pci_fd_enable_dma;
672 sun_fdops.fd_disable_dma = sun_pci_fd_disable_dma;
673 sun_fdops.fd_set_dma_mode = sun_pci_fd_set_dma_mode;
674 sun_fdops.fd_set_dma_addr = sun_pci_fd_set_dma_addr;
675 sun_fdops.fd_set_dma_count = sun_pci_fd_set_dma_count;
676 sun_fdops.get_dma_residue = sun_pci_get_dma_residue;
678 sun_fdops.fd_request_irq = sun_pci_fd_request_irq;
679 sun_fdops.fd_free_irq = sun_pci_fd_free_irq;
681 sun_fdops.fd_eject = sun_pci_fd_eject;
683 fdc_status = (unsigned long) &sun_fdc->status_82077;
684 FLOPPY_MOTOR_MASK = 0xf0;
687 * XXX: Find out on which machines this is really needed.
690 sun_pci_broken_drive = 1;
691 sun_fdops.fd_outb = sun_pci_fd_broken_outb;
694 allowed_drive_mask = 0;
695 if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 0))
696 sun_floppy_types[0] = 4;
697 if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 1))
698 sun_floppy_types[1] = 4;
701 * Find NS87303 SuperIO config registers (through ecpp).
703 for_each_ebus(ebus) {
704 for_each_ebusdev(edev, ebus) {
705 if (!strcmp(edev->prom_node->name, "ecpp")) {
706 config = edev->resource[1].start;
714 * Sanity check, is this really the NS87303?
716 switch (config & 0x3ff) {
727 return sun_floppy_types[0];
729 /* Enable PC-AT mode. */
730 ns87303_modify(config, ASC, 0, 0xc0);
732 #ifdef PCI_FDC_SWAP_DRIVES
734 * If only Floppy 1 is present, swap drives.
736 if (!sun_floppy_types[0] && sun_floppy_types[1]) {
738 * Set the drive exchange bit in FCR on NS87303,
739 * make sure other bits are sane before doing so.
741 ns87303_modify(config, FER, FER_EDM, 0);
742 ns87303_modify(config, ASC, ASC_DRV2_SEL, 0);
743 ns87303_modify(config, FCR, 0, FCR_LDE);
745 config = sun_floppy_types[0];
746 sun_floppy_types[0] = sun_floppy_types[1];
747 sun_floppy_types[1] = config;
749 if (sun_pci_broken_drive != -1) {
750 sun_pci_broken_drive = 1 - sun_pci_broken_drive;
751 sun_fdops.fd_outb = sun_pci_fd_lde_broken_outb;
754 #endif /* PCI_FDC_SWAP_DRIVES */
756 return sun_floppy_types[0];
761 prom_getproperty(sdev->prom_node, "status", state, sizeof(state));
762 if(!strncmp(state, "disabled", 8))
766 * We cannot do sbus_ioremap here: it does request_region,
767 * which the generic floppy driver tries to do once again.
768 * But we must use the sdev resource values as they have
769 * had parent ranges applied.
771 sun_fdc = (struct sun_flpy_controller *)
772 (sdev->resource[0].start +
773 ((sdev->resource[0].flags & 0x1ffUL) << 32UL));
775 /* Last minute sanity check... */
776 if(sbus_readb(&sun_fdc->status1_82077) == 0xff) {
777 sun_fdc = (struct sun_flpy_controller *)-1;
781 sun_fdops.fd_inb = sun_82077_fd_inb;
782 sun_fdops.fd_outb = sun_82077_fd_outb;
784 can_use_virtual_dma = use_virtual_dma = 1;
785 sun_fdops.fd_enable_dma = sun_fd_enable_dma;
786 sun_fdops.fd_disable_dma = sun_fd_disable_dma;
787 sun_fdops.fd_set_dma_mode = sun_fd_set_dma_mode;
788 sun_fdops.fd_set_dma_addr = sun_fd_set_dma_addr;
789 sun_fdops.fd_set_dma_count = sun_fd_set_dma_count;
790 sun_fdops.get_dma_residue = sun_get_dma_residue;
792 sun_fdops.fd_request_irq = sun_fd_request_irq;
793 sun_fdops.fd_free_irq = sun_fd_free_irq;
795 sun_fdops.fd_eject = sun_fd_eject;
797 fdc_status = (unsigned long) &sun_fdc->status_82077;
800 allowed_drive_mask = 0x01;
801 sun_floppy_types[0] = 4;
802 sun_floppy_types[1] = 0;
804 return sun_floppy_types[0];
807 #define EXTRA_FLOPPY_PARAMS
809 #endif /* !(__ASM_SPARC64_FLOPPY_H) */