1 /* floppy.h: Sparc specific parts of the Floppy driver.
3 * Copyright (C) 1996, 2007 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>
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 /* Super paranoid... */
88 #undef HAVE_DISABLE_HLT
90 static int sun_floppy_types
[2] = { 0, 0 };
92 /* Here is where we catch the floppy driver trying to initialize,
93 * therefore this is where we call the PROM device tree probing
94 * routine etc. on the Sparc.
96 #define FLOPPY0_TYPE sun_floppy_init()
97 #define FLOPPY1_TYPE sun_floppy_types[1]
99 #define FDC1 ((unsigned long)sun_fdc)
104 /* No 64k boundary crossing problems on the Sparc. */
105 #define CROSS_64KB(a,s) (0)
107 static unsigned char sun_82077_fd_inb(unsigned long port
)
112 printk("floppy: Asked to read unknown port %lx\n", port
);
113 panic("floppy: Port bolixed.");
114 case 4: /* FD_STATUS */
115 return sbus_readb(&sun_fdc
->status_82077
) & ~STATUS_DMA
;
116 case 5: /* FD_DATA */
117 return sbus_readb(&sun_fdc
->data_82077
);
119 /* XXX: Is DCL on 0x80 in sun4m? */
120 return sbus_readb(&sun_fdc
->dir_82077
);
122 panic("sun_82072_fd_inb: How did I get here?");
125 static void sun_82077_fd_outb(unsigned char value
, unsigned long port
)
130 printk("floppy: Asked to write to unknown port %lx\n", port
);
131 panic("floppy: Port bolixed.");
133 /* Happily, the 82077 has a real DOR register. */
134 sbus_writeb(value
, &sun_fdc
->dor_82077
);
136 case 5: /* FD_DATA */
137 sbus_writeb(value
, &sun_fdc
->data_82077
);
140 sbus_writeb(value
, &sun_fdc
->dcr_82077
);
142 case 4: /* FD_STATUS */
143 sbus_writeb(value
, &sun_fdc
->status_82077
);
149 /* For pseudo-dma (Sun floppy drives have no real DMA available to
150 * them so we must eat the data fifo bytes directly ourselves) we have
151 * three state variables. doing_pdma tells our inline low-level
152 * assembly floppy interrupt entry point whether it should sit and eat
153 * bytes from the fifo or just transfer control up to the higher level
154 * floppy interrupt c-code. I tried very hard but I could not get the
155 * pseudo-dma to work in c-code without getting many overruns and
156 * underruns. If non-zero, doing_pdma encodes the direction of
157 * the transfer for debugging. 1=read 2=write
159 unsigned char *pdma_vaddr
;
160 unsigned long pdma_size
;
161 volatile int doing_pdma
= 0;
163 /* This is software state */
164 char *pdma_base
= NULL
;
165 unsigned long pdma_areasize
;
167 /* Common routines to all controller types on the Sparc. */
168 static void sun_fd_disable_dma(void)
172 mmu_unlockarea(pdma_base
, pdma_areasize
);
177 static void sun_fd_set_dma_mode(int mode
)
187 printk("Unknown dma mode %d\n", mode
);
188 panic("floppy: Giving up...");
192 static void sun_fd_set_dma_addr(char *buffer
)
197 static void sun_fd_set_dma_count(int length
)
202 static void sun_fd_enable_dma(void)
204 pdma_vaddr
= mmu_lockarea(pdma_vaddr
, pdma_size
);
205 pdma_base
= pdma_vaddr
;
206 pdma_areasize
= pdma_size
;
209 irqreturn_t
sparc_floppy_irq(int irq
, void *dev_cookie
)
211 if (likely(doing_pdma
)) {
212 void __iomem
*stat
= (void __iomem
*) fdc_status
;
213 unsigned char *vaddr
= pdma_vaddr
;
214 unsigned long size
= pdma_size
;
219 if (unlikely(!(val
& 0x80))) {
224 if (unlikely(!(val
& 0x20))) {
232 *vaddr
++ = readb(stat
+ 1);
234 unsigned char data
= *vaddr
++;
237 writeb(data
, stat
+ 1);
245 /* Send Terminal Count pulse to floppy controller. */
246 val
= readb(auxio_register
);
247 val
|= AUXIO_AUX1_FTCNT
;
248 writeb(val
, auxio_register
);
249 val
&= ~AUXIO_AUX1_FTCNT
;
250 writeb(val
, auxio_register
);
256 return floppy_interrupt(irq
, dev_cookie
);
259 static int sun_fd_request_irq(void)
267 error
= request_irq(FLOPPY_IRQ
, sparc_floppy_irq
,
268 IRQF_DISABLED
, "floppy", NULL
);
270 return ((error
== 0) ? 0 : -1);
275 static void sun_fd_free_irq(void)
279 static unsigned int sun_get_dma_residue(void)
281 /* XXX This isn't really correct. XXX */
285 static int sun_fd_eject(int drive
)
287 set_dor(0x00, 0xff, 0x90);
289 set_dor(0x00, 0x6f, 0x00);
295 #include <asm/ebus.h>
296 #include <asm/ns87303.h>
298 static struct ebus_dma_info sun_pci_fd_ebus_dma
;
299 static struct pci_dev
*sun_pci_ebus_dev
;
300 static int sun_pci_broken_drive
= -1;
302 struct sun_pci_dma_op
{
308 static struct sun_pci_dma_op sun_pci_dma_current
= { -1U, 0, 0, NULL
};
309 static struct sun_pci_dma_op sun_pci_dma_pending
= { -1U, 0, 0, NULL
};
311 extern irqreturn_t
floppy_interrupt(int irq
, void *dev_id
);
313 static unsigned char sun_pci_fd_inb(unsigned long port
)
319 static void sun_pci_fd_outb(unsigned char val
, unsigned long port
)
325 static void sun_pci_fd_broken_outb(unsigned char val
, unsigned long port
)
329 * XXX: Due to SUN's broken floppy connector on AX and AXi
330 * we need to turn on MOTOR_0 also, if the floppy is
331 * jumpered to DS1 (like most PC floppies are). I hope
332 * this does not hurt correct hardware like the AXmp.
333 * (Eddie, Sep 12 1998).
335 if (port
== ((unsigned long)sun_fdc
) + 2) {
336 if (((val
& 0x03) == sun_pci_broken_drive
) && (val
& 0x20)) {
343 #ifdef PCI_FDC_SWAP_DRIVES
344 static void sun_pci_fd_lde_broken_outb(unsigned char val
, unsigned long port
)
348 * XXX: Due to SUN's broken floppy connector on AX and AXi
349 * we need to turn on MOTOR_0 also, if the floppy is
350 * jumpered to DS1 (like most PC floppies are). I hope
351 * this does not hurt correct hardware like the AXmp.
352 * (Eddie, Sep 12 1998).
354 if (port
== ((unsigned long)sun_fdc
) + 2) {
355 if (((val
& 0x03) == sun_pci_broken_drive
) && (val
& 0x10)) {
362 #endif /* PCI_FDC_SWAP_DRIVES */
364 static void sun_pci_fd_enable_dma(void)
366 BUG_ON((NULL
== sun_pci_dma_pending
.buf
) ||
367 (0 == sun_pci_dma_pending
.len
) ||
368 (0 == sun_pci_dma_pending
.direction
));
370 sun_pci_dma_current
.buf
= sun_pci_dma_pending
.buf
;
371 sun_pci_dma_current
.len
= sun_pci_dma_pending
.len
;
372 sun_pci_dma_current
.direction
= sun_pci_dma_pending
.direction
;
374 sun_pci_dma_pending
.buf
= NULL
;
375 sun_pci_dma_pending
.len
= 0;
376 sun_pci_dma_pending
.direction
= 0;
377 sun_pci_dma_pending
.addr
= -1U;
379 sun_pci_dma_current
.addr
=
380 pci_map_single(sun_pci_ebus_dev
,
381 sun_pci_dma_current
.buf
,
382 sun_pci_dma_current
.len
,
383 sun_pci_dma_current
.direction
);
385 ebus_dma_enable(&sun_pci_fd_ebus_dma
, 1);
387 if (ebus_dma_request(&sun_pci_fd_ebus_dma
,
388 sun_pci_dma_current
.addr
,
389 sun_pci_dma_current
.len
))
393 static void sun_pci_fd_disable_dma(void)
395 ebus_dma_enable(&sun_pci_fd_ebus_dma
, 0);
396 if (sun_pci_dma_current
.addr
!= -1U)
397 pci_unmap_single(sun_pci_ebus_dev
,
398 sun_pci_dma_current
.addr
,
399 sun_pci_dma_current
.len
,
400 sun_pci_dma_current
.direction
);
401 sun_pci_dma_current
.addr
= -1U;
404 static void sun_pci_fd_set_dma_mode(int mode
)
406 if (mode
== DMA_MODE_WRITE
)
407 sun_pci_dma_pending
.direction
= PCI_DMA_TODEVICE
;
409 sun_pci_dma_pending
.direction
= PCI_DMA_FROMDEVICE
;
411 ebus_dma_prepare(&sun_pci_fd_ebus_dma
, mode
!= DMA_MODE_WRITE
);
414 static void sun_pci_fd_set_dma_count(int length
)
416 sun_pci_dma_pending
.len
= length
;
419 static void sun_pci_fd_set_dma_addr(char *buffer
)
421 sun_pci_dma_pending
.buf
= buffer
;
424 static unsigned int sun_pci_get_dma_residue(void)
426 return ebus_dma_residue(&sun_pci_fd_ebus_dma
);
429 static int sun_pci_fd_request_irq(void)
431 return ebus_dma_irq_enable(&sun_pci_fd_ebus_dma
, 1);
434 static void sun_pci_fd_free_irq(void)
436 ebus_dma_irq_enable(&sun_pci_fd_ebus_dma
, 0);
439 static int sun_pci_fd_eject(int drive
)
444 void sun_pci_fd_dma_callback(struct ebus_dma_info
*p
, int event
, void *cookie
)
446 floppy_interrupt(0, NULL
);
450 * Floppy probing, we'd like to use /dev/fd0 for a single Floppy on PCI,
451 * even if this is configured using DS1, thus looks like /dev/fd1 with
452 * the cabling used in Ultras.
454 #define DOR (port + 2)
455 #define MSR (port + 4)
456 #define FIFO (port + 5)
458 static void sun_pci_fd_out_byte(unsigned long port
, unsigned char val
,
461 unsigned char status
;
464 while (!((status
= inb(MSR
)) & 0x80) && --timeout
)
469 static unsigned char sun_pci_fd_sensei(unsigned long port
)
471 unsigned char result
[2] = { 0x70, 0x00 };
472 unsigned char status
;
475 sun_pci_fd_out_byte(port
, 0x08, FIFO
);
479 while (!((status
= inb(MSR
)) & 0x80) && --timeout
)
485 if ((status
& 0xf0) == 0xd0)
486 result
[i
++] = inb(FIFO
);
494 static void sun_pci_fd_reset(unsigned long port
)
496 unsigned char mask
= 0x00;
497 unsigned char status
;
502 status
= sun_pci_fd_sensei(port
);
503 if ((status
& 0xc0) == 0xc0)
504 mask
|= 1 << (status
& 0x03);
507 } while ((mask
!= 0x0f) && --timeout
);
510 static int sun_pci_fd_test_drive(unsigned long port
, int drive
)
512 unsigned char status
, data
;
516 sun_pci_fd_reset(port
);
518 data
= (0x10 << drive
) | 0x0c | drive
;
519 sun_pci_fd_out_byte(port
, data
, DOR
);
521 sun_pci_fd_out_byte(port
, 0x07, FIFO
);
522 sun_pci_fd_out_byte(port
, drive
& 0x03, FIFO
);
526 status
= sun_pci_fd_sensei(port
);
527 } while (((status
& 0xc0) == 0x80) && --timeout
);
532 ready
= (status
& 0x10) ? 0 : 1;
534 sun_pci_fd_reset(port
);
541 #endif /* CONFIG_PCI */
544 static int __init
ebus_fdthree_p(struct linux_ebus_device
*edev
)
546 if (!strcmp(edev
->prom_node
->name
, "fdthree"))
548 if (!strcmp(edev
->prom_node
->name
, "floppy")) {
551 compat
= of_get_property(edev
->prom_node
,
553 if (compat
&& !strcmp(compat
, "fdthree"))
560 static unsigned long __init
sun_floppy_init(void)
563 struct sbus_bus
*bus
;
564 struct sbus_dev
*sdev
= NULL
;
565 static int initialized
= 0;
568 return sun_floppy_types
[0];
571 for_all_sbusdev (sdev
, bus
) {
572 if (!strcmp(sdev
->prom_name
, "SUNW,fdtwo"))
577 FLOPPY_IRQ
= sdev
->irqs
[0];
580 struct linux_ebus
*ebus
;
581 struct linux_ebus_device
*edev
= NULL
;
582 unsigned long config
= 0;
583 void __iomem
*auxio_reg
;
584 const char *state_prop
;
586 for_each_ebus(ebus
) {
587 for_each_ebusdev(edev
, ebus
) {
588 if (ebus_fdthree_p(edev
))
596 state_prop
= of_get_property(edev
->prom_node
, "status", NULL
);
597 if (state_prop
&& !strncmp(state_prop
, "disabled", 8))
600 FLOPPY_IRQ
= edev
->irqs
[0];
602 /* Make sure the high density bit is set, some systems
603 * (most notably Ultra5/Ultra10) come up with it clear.
605 auxio_reg
= (void __iomem
*) edev
->resource
[2].start
;
606 writel(readl(auxio_reg
)|0x2, auxio_reg
);
608 sun_pci_ebus_dev
= ebus
->self
;
610 spin_lock_init(&sun_pci_fd_ebus_dma
.lock
);
613 sun_pci_fd_ebus_dma
.regs
= (void __iomem
*)
614 edev
->resource
[1].start
;
615 if (!sun_pci_fd_ebus_dma
.regs
)
618 sun_pci_fd_ebus_dma
.flags
= (EBUS_DMA_FLAG_USE_EBDMA_HANDLER
|
619 EBUS_DMA_FLAG_TCI_DISABLE
);
620 sun_pci_fd_ebus_dma
.callback
= sun_pci_fd_dma_callback
;
621 sun_pci_fd_ebus_dma
.client_cookie
= NULL
;
622 sun_pci_fd_ebus_dma
.irq
= FLOPPY_IRQ
;
623 strcpy(sun_pci_fd_ebus_dma
.name
, "floppy");
624 if (ebus_dma_register(&sun_pci_fd_ebus_dma
))
628 sun_fdc
= (struct sun_flpy_controller
*)edev
->resource
[0].start
;
630 sun_fdops
.fd_inb
= sun_pci_fd_inb
;
631 sun_fdops
.fd_outb
= sun_pci_fd_outb
;
633 can_use_virtual_dma
= use_virtual_dma
= 0;
634 sun_fdops
.fd_enable_dma
= sun_pci_fd_enable_dma
;
635 sun_fdops
.fd_disable_dma
= sun_pci_fd_disable_dma
;
636 sun_fdops
.fd_set_dma_mode
= sun_pci_fd_set_dma_mode
;
637 sun_fdops
.fd_set_dma_addr
= sun_pci_fd_set_dma_addr
;
638 sun_fdops
.fd_set_dma_count
= sun_pci_fd_set_dma_count
;
639 sun_fdops
.get_dma_residue
= sun_pci_get_dma_residue
;
641 sun_fdops
.fd_request_irq
= sun_pci_fd_request_irq
;
642 sun_fdops
.fd_free_irq
= sun_pci_fd_free_irq
;
644 sun_fdops
.fd_eject
= sun_pci_fd_eject
;
646 fdc_status
= (unsigned long) &sun_fdc
->status_82077
;
649 * XXX: Find out on which machines this is really needed.
652 sun_pci_broken_drive
= 1;
653 sun_fdops
.fd_outb
= sun_pci_fd_broken_outb
;
656 allowed_drive_mask
= 0;
657 if (sun_pci_fd_test_drive((unsigned long)sun_fdc
, 0))
658 sun_floppy_types
[0] = 4;
659 if (sun_pci_fd_test_drive((unsigned long)sun_fdc
, 1))
660 sun_floppy_types
[1] = 4;
663 * Find NS87303 SuperIO config registers (through ecpp).
665 for_each_ebus(ebus
) {
666 for_each_ebusdev(edev
, ebus
) {
667 if (!strcmp(edev
->prom_node
->name
, "ecpp")) {
668 config
= edev
->resource
[1].start
;
676 * Sanity check, is this really the NS87303?
678 switch (config
& 0x3ff) {
689 return sun_floppy_types
[0];
691 /* Enable PC-AT mode. */
692 ns87303_modify(config
, ASC
, 0, 0xc0);
694 #ifdef PCI_FDC_SWAP_DRIVES
696 * If only Floppy 1 is present, swap drives.
698 if (!sun_floppy_types
[0] && sun_floppy_types
[1]) {
700 * Set the drive exchange bit in FCR on NS87303,
701 * make sure other bits are sane before doing so.
703 ns87303_modify(config
, FER
, FER_EDM
, 0);
704 ns87303_modify(config
, ASC
, ASC_DRV2_SEL
, 0);
705 ns87303_modify(config
, FCR
, 0, FCR_LDE
);
707 config
= sun_floppy_types
[0];
708 sun_floppy_types
[0] = sun_floppy_types
[1];
709 sun_floppy_types
[1] = config
;
711 if (sun_pci_broken_drive
!= -1) {
712 sun_pci_broken_drive
= 1 - sun_pci_broken_drive
;
713 sun_fdops
.fd_outb
= sun_pci_fd_lde_broken_outb
;
716 #endif /* PCI_FDC_SWAP_DRIVES */
718 return sun_floppy_types
[0];
723 prom_getproperty(sdev
->prom_node
, "status", state
, sizeof(state
));
724 if(!strncmp(state
, "disabled", 8))
728 * We cannot do sbus_ioremap here: it does request_region,
729 * which the generic floppy driver tries to do once again.
730 * But we must use the sdev resource values as they have
731 * had parent ranges applied.
733 sun_fdc
= (struct sun_flpy_controller
*)
734 (sdev
->resource
[0].start
+
735 ((sdev
->resource
[0].flags
& 0x1ffUL
) << 32UL));
737 /* Last minute sanity check... */
738 if(sbus_readb(&sun_fdc
->status1_82077
) == 0xff) {
739 sun_fdc
= (struct sun_flpy_controller
*)-1;
743 sun_fdops
.fd_inb
= sun_82077_fd_inb
;
744 sun_fdops
.fd_outb
= sun_82077_fd_outb
;
746 can_use_virtual_dma
= use_virtual_dma
= 1;
747 sun_fdops
.fd_enable_dma
= sun_fd_enable_dma
;
748 sun_fdops
.fd_disable_dma
= sun_fd_disable_dma
;
749 sun_fdops
.fd_set_dma_mode
= sun_fd_set_dma_mode
;
750 sun_fdops
.fd_set_dma_addr
= sun_fd_set_dma_addr
;
751 sun_fdops
.fd_set_dma_count
= sun_fd_set_dma_count
;
752 sun_fdops
.get_dma_residue
= sun_get_dma_residue
;
754 sun_fdops
.fd_request_irq
= sun_fd_request_irq
;
755 sun_fdops
.fd_free_irq
= sun_fd_free_irq
;
757 sun_fdops
.fd_eject
= sun_fd_eject
;
759 fdc_status
= (unsigned long) &sun_fdc
->status_82077
;
762 allowed_drive_mask
= 0x01;
763 sun_floppy_types
[0] = 4;
764 sun_floppy_types
[1] = 0;
766 return sun_floppy_types
[0];
769 #define EXTRA_FLOPPY_PARAMS
771 static DEFINE_SPINLOCK(dma_spin_lock
);
773 #define claim_dma_lock() \
774 ({ unsigned long flags; \
775 spin_lock_irqsave(&dma_spin_lock, flags); \
779 #define release_dma_lock(__flags) \
780 spin_unlock_irqrestore(&dma_spin_lock, __flags);
782 #endif /* !(__ASM_SPARC64_FLOPPY_H) */