1 /* floppy.h: Sparc specific parts of the Floppy driver.
3 * Copyright (C) 1996, 2007, 2008 David S. Miller (davem@davemloft.net)
4 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * Ultra/PCI support added: Sep 1997 Eddie C. Dost (ecd@skynet.be)
9 #ifndef __ASM_SPARC64_FLOPPY_H
10 #define __ASM_SPARC64_FLOPPY_H
12 #include <linux/init.h>
13 #include <linux/pci.h>
15 #include <linux/of_device.h>
18 #include <asm/pgtable.h>
19 #include <asm/system.h>
20 #include <asm/idprom.h>
21 #include <asm/oplib.h>
22 #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 of_device *floppy_op = 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 /* Super paranoid... */
89 #undef HAVE_DISABLE_HLT
91 static int sun_floppy_types[2] = { 0, 0 };
93 /* Here is where we catch the floppy driver trying to initialize,
94 * therefore this is where we call the PROM device tree probing
95 * routine etc. on the Sparc.
97 #define FLOPPY0_TYPE sun_floppy_init()
98 #define FLOPPY1_TYPE sun_floppy_types[1]
100 #define FDC1 ((unsigned long)sun_fdc)
105 /* No 64k boundary crossing problems on the Sparc. */
106 #define CROSS_64KB(a,s) (0)
108 static unsigned char sun_82077_fd_inb(unsigned long port)
113 printk("floppy: Asked to read unknown port %lx\n", port);
114 panic("floppy: Port bolixed.");
115 case 4: /* FD_STATUS */
116 return sbus_readb(&sun_fdc->status_82077) & ~STATUS_DMA;
117 case 5: /* FD_DATA */
118 return sbus_readb(&sun_fdc->data_82077);
120 /* XXX: Is DCL on 0x80 in sun4m? */
121 return sbus_readb(&sun_fdc->dir_82077);
123 panic("sun_82072_fd_inb: How did I get here?");
126 static void sun_82077_fd_outb(unsigned char value, unsigned long port)
131 printk("floppy: Asked to write to unknown port %lx\n", port);
132 panic("floppy: Port bolixed.");
134 /* Happily, the 82077 has a real DOR register. */
135 sbus_writeb(value, &sun_fdc->dor_82077);
137 case 5: /* FD_DATA */
138 sbus_writeb(value, &sun_fdc->data_82077);
141 sbus_writeb(value, &sun_fdc->dcr_82077);
143 case 4: /* FD_STATUS */
144 sbus_writeb(value, &sun_fdc->status_82077);
150 /* For pseudo-dma (Sun floppy drives have no real DMA available to
151 * them so we must eat the data fifo bytes directly ourselves) we have
152 * three state variables. doing_pdma tells our inline low-level
153 * assembly floppy interrupt entry point whether it should sit and eat
154 * bytes from the fifo or just transfer control up to the higher level
155 * floppy interrupt c-code. I tried very hard but I could not get the
156 * pseudo-dma to work in c-code without getting many overruns and
157 * underruns. If non-zero, doing_pdma encodes the direction of
158 * the transfer for debugging. 1=read 2=write
160 unsigned char *pdma_vaddr;
161 unsigned long pdma_size;
162 volatile int doing_pdma = 0;
164 /* This is software state */
165 char *pdma_base = NULL;
166 unsigned long pdma_areasize;
168 /* Common routines to all controller types on the Sparc. */
169 static void sun_fd_disable_dma(void)
173 mmu_unlockarea(pdma_base, pdma_areasize);
178 static void sun_fd_set_dma_mode(int mode)
188 printk("Unknown dma mode %d\n", mode);
189 panic("floppy: Giving up...");
193 static void sun_fd_set_dma_addr(char *buffer)
198 static void sun_fd_set_dma_count(int length)
203 static void sun_fd_enable_dma(void)
205 pdma_vaddr = mmu_lockarea(pdma_vaddr, pdma_size);
206 pdma_base = pdma_vaddr;
207 pdma_areasize = pdma_size;
210 irqreturn_t sparc_floppy_irq(int irq, void *dev_cookie)
212 if (likely(doing_pdma)) {
213 void __iomem *stat = (void __iomem *) fdc_status;
214 unsigned char *vaddr = pdma_vaddr;
215 unsigned long size = pdma_size;
220 if (unlikely(!(val & 0x80))) {
225 if (unlikely(!(val & 0x20))) {
233 *vaddr++ = readb(stat + 1);
235 unsigned char data = *vaddr++;
238 writeb(data, stat + 1);
246 /* Send Terminal Count pulse to floppy controller. */
247 val = readb(auxio_register);
248 val |= AUXIO_AUX1_FTCNT;
249 writeb(val, auxio_register);
250 val &= ~AUXIO_AUX1_FTCNT;
251 writeb(val, auxio_register);
257 return floppy_interrupt(irq, dev_cookie);
260 static int sun_fd_request_irq(void)
268 error = request_irq(FLOPPY_IRQ, sparc_floppy_irq,
269 IRQF_DISABLED, "floppy", NULL);
271 return ((error == 0) ? 0 : -1);
276 static void sun_fd_free_irq(void)
280 static unsigned int sun_get_dma_residue(void)
282 /* XXX This isn't really correct. XXX */
286 static int sun_fd_eject(int drive)
288 set_dor(0x00, 0xff, 0x90);
290 set_dor(0x00, 0x6f, 0x00);
296 #include <asm/ebus.h>
297 #include <asm/ns87303.h>
299 static struct ebus_dma_info sun_pci_fd_ebus_dma;
300 static struct pci_dev *sun_pci_ebus_dev;
301 static int sun_pci_broken_drive = -1;
303 struct sun_pci_dma_op {
309 static struct sun_pci_dma_op sun_pci_dma_current = { -1U, 0, 0, NULL};
310 static struct sun_pci_dma_op sun_pci_dma_pending = { -1U, 0, 0, NULL};
312 extern irqreturn_t floppy_interrupt(int irq, void *dev_id);
314 static unsigned char sun_pci_fd_inb(unsigned long port)
320 static void sun_pci_fd_outb(unsigned char val, unsigned long port)
326 static void sun_pci_fd_broken_outb(unsigned char val, unsigned long port)
330 * XXX: Due to SUN's broken floppy connector on AX and AXi
331 * we need to turn on MOTOR_0 also, if the floppy is
332 * jumpered to DS1 (like most PC floppies are). I hope
333 * this does not hurt correct hardware like the AXmp.
334 * (Eddie, Sep 12 1998).
336 if (port == ((unsigned long)sun_fdc) + 2) {
337 if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x20)) {
344 #ifdef PCI_FDC_SWAP_DRIVES
345 static void sun_pci_fd_lde_broken_outb(unsigned char val, unsigned long port)
349 * XXX: Due to SUN's broken floppy connector on AX and AXi
350 * we need to turn on MOTOR_0 also, if the floppy is
351 * jumpered to DS1 (like most PC floppies are). I hope
352 * this does not hurt correct hardware like the AXmp.
353 * (Eddie, Sep 12 1998).
355 if (port == ((unsigned long)sun_fdc) + 2) {
356 if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x10)) {
363 #endif /* PCI_FDC_SWAP_DRIVES */
365 static void sun_pci_fd_enable_dma(void)
367 BUG_ON((NULL == sun_pci_dma_pending.buf) ||
368 (0 == sun_pci_dma_pending.len) ||
369 (0 == sun_pci_dma_pending.direction));
371 sun_pci_dma_current.buf = sun_pci_dma_pending.buf;
372 sun_pci_dma_current.len = sun_pci_dma_pending.len;
373 sun_pci_dma_current.direction = sun_pci_dma_pending.direction;
375 sun_pci_dma_pending.buf = NULL;
376 sun_pci_dma_pending.len = 0;
377 sun_pci_dma_pending.direction = 0;
378 sun_pci_dma_pending.addr = -1U;
380 sun_pci_dma_current.addr =
381 pci_map_single(sun_pci_ebus_dev,
382 sun_pci_dma_current.buf,
383 sun_pci_dma_current.len,
384 sun_pci_dma_current.direction);
386 ebus_dma_enable(&sun_pci_fd_ebus_dma, 1);
388 if (ebus_dma_request(&sun_pci_fd_ebus_dma,
389 sun_pci_dma_current.addr,
390 sun_pci_dma_current.len))
394 static void sun_pci_fd_disable_dma(void)
396 ebus_dma_enable(&sun_pci_fd_ebus_dma, 0);
397 if (sun_pci_dma_current.addr != -1U)
398 pci_unmap_single(sun_pci_ebus_dev,
399 sun_pci_dma_current.addr,
400 sun_pci_dma_current.len,
401 sun_pci_dma_current.direction);
402 sun_pci_dma_current.addr = -1U;
405 static void sun_pci_fd_set_dma_mode(int mode)
407 if (mode == DMA_MODE_WRITE)
408 sun_pci_dma_pending.direction = PCI_DMA_TODEVICE;
410 sun_pci_dma_pending.direction = PCI_DMA_FROMDEVICE;
412 ebus_dma_prepare(&sun_pci_fd_ebus_dma, mode != DMA_MODE_WRITE);
415 static void sun_pci_fd_set_dma_count(int length)
417 sun_pci_dma_pending.len = length;
420 static void sun_pci_fd_set_dma_addr(char *buffer)
422 sun_pci_dma_pending.buf = buffer;
425 static unsigned int sun_pci_get_dma_residue(void)
427 return ebus_dma_residue(&sun_pci_fd_ebus_dma);
430 static int sun_pci_fd_request_irq(void)
432 return ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 1);
435 static void sun_pci_fd_free_irq(void)
437 ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 0);
440 static int sun_pci_fd_eject(int drive)
445 void sun_pci_fd_dma_callback(struct ebus_dma_info *p, int event, void *cookie)
447 floppy_interrupt(0, NULL);
451 * Floppy probing, we'd like to use /dev/fd0 for a single Floppy on PCI,
452 * even if this is configured using DS1, thus looks like /dev/fd1 with
453 * the cabling used in Ultras.
455 #define DOR (port + 2)
456 #define MSR (port + 4)
457 #define FIFO (port + 5)
459 static void sun_pci_fd_out_byte(unsigned long port, unsigned char val,
462 unsigned char status;
465 while (!((status = inb(MSR)) & 0x80) && --timeout)
470 static unsigned char sun_pci_fd_sensei(unsigned long port)
472 unsigned char result[2] = { 0x70, 0x00 };
473 unsigned char status;
476 sun_pci_fd_out_byte(port, 0x08, FIFO);
480 while (!((status = inb(MSR)) & 0x80) && --timeout)
486 if ((status & 0xf0) == 0xd0)
487 result[i++] = inb(FIFO);
495 static void sun_pci_fd_reset(unsigned long port)
497 unsigned char mask = 0x00;
498 unsigned char status;
503 status = sun_pci_fd_sensei(port);
504 if ((status & 0xc0) == 0xc0)
505 mask |= 1 << (status & 0x03);
508 } while ((mask != 0x0f) && --timeout);
511 static int sun_pci_fd_test_drive(unsigned long port, int drive)
513 unsigned char status, data;
517 sun_pci_fd_reset(port);
519 data = (0x10 << drive) | 0x0c | drive;
520 sun_pci_fd_out_byte(port, data, DOR);
522 sun_pci_fd_out_byte(port, 0x07, FIFO);
523 sun_pci_fd_out_byte(port, drive & 0x03, FIFO);
527 status = sun_pci_fd_sensei(port);
528 } while (((status & 0xc0) == 0x80) && --timeout);
533 ready = (status & 0x10) ? 0 : 1;
535 sun_pci_fd_reset(port);
542 #endif /* CONFIG_PCI */
545 static int __init ebus_fdthree_p(struct linux_ebus_device *edev)
547 if (!strcmp(edev->prom_node->name, "fdthree"))
549 if (!strcmp(edev->prom_node->name, "floppy")) {
552 compat = of_get_property(edev->prom_node,
554 if (compat && !strcmp(compat, "fdthree"))
561 static unsigned long __init sun_floppy_init(void)
563 static int initialized = 0;
564 struct device_node *dp;
565 struct of_device *op;
570 return sun_floppy_types[0];
575 for_each_node_by_name(dp, "SUNW,fdtwo") {
576 if (strcmp(dp->parent->name, "sbus"))
578 op = of_find_device_by_node(dp);
584 FLOPPY_IRQ = op->irqs[0];
587 struct linux_ebus *ebus;
588 struct linux_ebus_device *edev = NULL;
589 unsigned long config = 0;
590 void __iomem *auxio_reg;
591 const char *state_prop;
593 for_each_ebus(ebus) {
594 for_each_ebusdev(edev, ebus) {
595 if (ebus_fdthree_p(edev))
605 state_prop = of_get_property(op->node, "status", NULL);
606 if (state_prop && !strncmp(state_prop, "disabled", 8))
609 FLOPPY_IRQ = edev->irqs[0];
611 /* Make sure the high density bit is set, some systems
612 * (most notably Ultra5/Ultra10) come up with it clear.
614 auxio_reg = (void __iomem *) edev->resource[2].start;
615 writel(readl(auxio_reg)|0x2, auxio_reg);
617 sun_pci_ebus_dev = ebus->self;
619 spin_lock_init(&sun_pci_fd_ebus_dma.lock);
622 sun_pci_fd_ebus_dma.regs = (void __iomem *)
623 edev->resource[1].start;
624 if (!sun_pci_fd_ebus_dma.regs)
627 sun_pci_fd_ebus_dma.flags = (EBUS_DMA_FLAG_USE_EBDMA_HANDLER |
628 EBUS_DMA_FLAG_TCI_DISABLE);
629 sun_pci_fd_ebus_dma.callback = sun_pci_fd_dma_callback;
630 sun_pci_fd_ebus_dma.client_cookie = NULL;
631 sun_pci_fd_ebus_dma.irq = FLOPPY_IRQ;
632 strcpy(sun_pci_fd_ebus_dma.name, "floppy");
633 if (ebus_dma_register(&sun_pci_fd_ebus_dma))
637 sun_fdc = (struct sun_flpy_controller *)edev->resource[0].start;
639 sun_fdops.fd_inb = sun_pci_fd_inb;
640 sun_fdops.fd_outb = sun_pci_fd_outb;
642 can_use_virtual_dma = use_virtual_dma = 0;
643 sun_fdops.fd_enable_dma = sun_pci_fd_enable_dma;
644 sun_fdops.fd_disable_dma = sun_pci_fd_disable_dma;
645 sun_fdops.fd_set_dma_mode = sun_pci_fd_set_dma_mode;
646 sun_fdops.fd_set_dma_addr = sun_pci_fd_set_dma_addr;
647 sun_fdops.fd_set_dma_count = sun_pci_fd_set_dma_count;
648 sun_fdops.get_dma_residue = sun_pci_get_dma_residue;
650 sun_fdops.fd_request_irq = sun_pci_fd_request_irq;
651 sun_fdops.fd_free_irq = sun_pci_fd_free_irq;
653 sun_fdops.fd_eject = sun_pci_fd_eject;
655 fdc_status = (unsigned long) &sun_fdc->status_82077;
658 * XXX: Find out on which machines this is really needed.
661 sun_pci_broken_drive = 1;
662 sun_fdops.fd_outb = sun_pci_fd_broken_outb;
665 allowed_drive_mask = 0;
666 if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 0))
667 sun_floppy_types[0] = 4;
668 if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 1))
669 sun_floppy_types[1] = 4;
672 * Find NS87303 SuperIO config registers (through ecpp).
674 for_each_ebus(ebus) {
675 for_each_ebusdev(edev, ebus) {
676 if (!strcmp(edev->prom_node->name, "ecpp")) {
677 config = edev->resource[1].start;
685 * Sanity check, is this really the NS87303?
687 switch (config & 0x3ff) {
698 return sun_floppy_types[0];
700 /* Enable PC-AT mode. */
701 ns87303_modify(config, ASC, 0, 0xc0);
703 #ifdef PCI_FDC_SWAP_DRIVES
705 * If only Floppy 1 is present, swap drives.
707 if (!sun_floppy_types[0] && sun_floppy_types[1]) {
709 * Set the drive exchange bit in FCR on NS87303,
710 * make sure other bits are sane before doing so.
712 ns87303_modify(config, FER, FER_EDM, 0);
713 ns87303_modify(config, ASC, ASC_DRV2_SEL, 0);
714 ns87303_modify(config, FCR, 0, FCR_LDE);
716 config = sun_floppy_types[0];
717 sun_floppy_types[0] = sun_floppy_types[1];
718 sun_floppy_types[1] = config;
720 if (sun_pci_broken_drive != -1) {
721 sun_pci_broken_drive = 1 - sun_pci_broken_drive;
722 sun_fdops.fd_outb = sun_pci_fd_lde_broken_outb;
725 #endif /* PCI_FDC_SWAP_DRIVES */
727 return sun_floppy_types[0];
732 prop = of_get_property(op->node, "status", NULL);
733 if (prop && !strncmp(state, "disabled", 8))
737 * We cannot do of_ioremap here: it does request_region,
738 * which the generic floppy driver tries to do once again.
739 * But we must use the sdev resource values as they have
740 * had parent ranges applied.
742 sun_fdc = (struct sun_flpy_controller *)
743 (op->resource[0].start +
744 ((op->resource[0].flags & 0x1ffUL) << 32UL));
746 /* Last minute sanity check... */
747 if (sbus_readb(&sun_fdc->status1_82077) == 0xff) {
748 sun_fdc = (struct sun_flpy_controller *)-1;
752 sun_fdops.fd_inb = sun_82077_fd_inb;
753 sun_fdops.fd_outb = sun_82077_fd_outb;
755 can_use_virtual_dma = use_virtual_dma = 1;
756 sun_fdops.fd_enable_dma = sun_fd_enable_dma;
757 sun_fdops.fd_disable_dma = sun_fd_disable_dma;
758 sun_fdops.fd_set_dma_mode = sun_fd_set_dma_mode;
759 sun_fdops.fd_set_dma_addr = sun_fd_set_dma_addr;
760 sun_fdops.fd_set_dma_count = sun_fd_set_dma_count;
761 sun_fdops.get_dma_residue = sun_get_dma_residue;
763 sun_fdops.fd_request_irq = sun_fd_request_irq;
764 sun_fdops.fd_free_irq = sun_fd_free_irq;
766 sun_fdops.fd_eject = sun_fd_eject;
768 fdc_status = (unsigned long) &sun_fdc->status_82077;
771 allowed_drive_mask = 0x01;
772 sun_floppy_types[0] = 4;
773 sun_floppy_types[1] = 0;
775 return sun_floppy_types[0];
778 #define EXTRA_FLOPPY_PARAMS
780 static DEFINE_SPINLOCK(dma_spin_lock);
782 #define claim_dma_lock() \
783 ({ unsigned long flags; \
784 spin_lock_irqsave(&dma_spin_lock, flags); \
788 #define release_dma_lock(__flags) \
789 spin_unlock_irqrestore(&dma_spin_lock, __flags);
791 #endif /* !(__ASM_SPARC64_FLOPPY_H) */