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
13 #include <linux/of_device.h>
14 #include <linux/dma-mapping.h>
16 #include <asm/auxio.h>
19 * Define this to enable exchanging drive 0 and 1 if only drive 1 is
20 * probed on PCI machines.
22 #undef PCI_FDC_SWAP_DRIVES
26 * 1) Netbsd Sun floppy driver.
27 * 2) NCR 82077 controller manual
28 * 3) Intel 82077 controller manual
30 struct sun_flpy_controller
{
31 volatile unsigned char status1_82077
; /* Auxiliary Status reg. 1 */
32 volatile unsigned char status2_82077
; /* Auxiliary Status reg. 2 */
33 volatile unsigned char dor_82077
; /* Digital Output reg. */
34 volatile unsigned char tapectl_82077
; /* Tape Control reg */
35 volatile unsigned char status_82077
; /* Main Status Register. */
36 #define drs_82077 status_82077 /* Digital Rate Select reg. */
37 volatile unsigned char data_82077
; /* Data fifo. */
38 volatile unsigned char ___unused
;
39 volatile unsigned char dir_82077
; /* Digital Input reg. */
40 #define dcr_82077 dir_82077 /* Config Control reg. */
43 /* You'll only ever find one controller on an Ultra anyways. */
44 static struct sun_flpy_controller
*sun_fdc
= (struct sun_flpy_controller
*)-1;
45 unsigned long fdc_status
;
46 static struct platform_device
*floppy_op
= NULL
;
48 struct sun_floppy_ops
{
49 unsigned char (*fd_inb
) (unsigned long port
);
50 void (*fd_outb
) (unsigned char value
, unsigned long port
);
51 void (*fd_enable_dma
) (void);
52 void (*fd_disable_dma
) (void);
53 void (*fd_set_dma_mode
) (int);
54 void (*fd_set_dma_addr
) (char *);
55 void (*fd_set_dma_count
) (int);
56 unsigned int (*get_dma_residue
) (void);
57 int (*fd_request_irq
) (void);
58 void (*fd_free_irq
) (void);
59 int (*fd_eject
) (int);
62 static struct sun_floppy_ops sun_fdops
;
64 #define fd_inb(port) sun_fdops.fd_inb(port)
65 #define fd_outb(value,port) sun_fdops.fd_outb(value,port)
66 #define fd_enable_dma() sun_fdops.fd_enable_dma()
67 #define fd_disable_dma() sun_fdops.fd_disable_dma()
68 #define fd_request_dma() (0) /* nothing... */
69 #define fd_free_dma() /* nothing... */
70 #define fd_clear_dma_ff() /* nothing... */
71 #define fd_set_dma_mode(mode) sun_fdops.fd_set_dma_mode(mode)
72 #define fd_set_dma_addr(addr) sun_fdops.fd_set_dma_addr(addr)
73 #define fd_set_dma_count(count) sun_fdops.fd_set_dma_count(count)
74 #define get_dma_residue(x) sun_fdops.get_dma_residue()
75 #define fd_cacheflush(addr, size) /* nothing... */
76 #define fd_request_irq() sun_fdops.fd_request_irq()
77 #define fd_free_irq() sun_fdops.fd_free_irq()
78 #define fd_eject(drive) sun_fdops.fd_eject(drive)
80 /* Super paranoid... */
81 #undef HAVE_DISABLE_HLT
83 static int sun_floppy_types
[2] = { 0, 0 };
85 /* Here is where we catch the floppy driver trying to initialize,
86 * therefore this is where we call the PROM device tree probing
87 * routine etc. on the Sparc.
89 #define FLOPPY0_TYPE sun_floppy_init()
90 #define FLOPPY1_TYPE sun_floppy_types[1]
92 #define FDC1 ((unsigned long)sun_fdc)
97 /* No 64k boundary crossing problems on the Sparc. */
98 #define CROSS_64KB(a,s) (0)
100 static unsigned char sun_82077_fd_inb(unsigned long port
)
105 printk("floppy: Asked to read unknown port %lx\n", port
);
106 panic("floppy: Port bolixed.");
107 case 4: /* FD_STATUS */
108 return sbus_readb(&sun_fdc
->status_82077
) & ~STATUS_DMA
;
109 case 5: /* FD_DATA */
110 return sbus_readb(&sun_fdc
->data_82077
);
112 /* XXX: Is DCL on 0x80 in sun4m? */
113 return sbus_readb(&sun_fdc
->dir_82077
);
115 panic("sun_82072_fd_inb: How did I get here?");
118 static void sun_82077_fd_outb(unsigned char value
, unsigned long port
)
123 printk("floppy: Asked to write to unknown port %lx\n", port
);
124 panic("floppy: Port bolixed.");
126 /* Happily, the 82077 has a real DOR register. */
127 sbus_writeb(value
, &sun_fdc
->dor_82077
);
129 case 5: /* FD_DATA */
130 sbus_writeb(value
, &sun_fdc
->data_82077
);
133 sbus_writeb(value
, &sun_fdc
->dcr_82077
);
135 case 4: /* FD_STATUS */
136 sbus_writeb(value
, &sun_fdc
->status_82077
);
142 /* For pseudo-dma (Sun floppy drives have no real DMA available to
143 * them so we must eat the data fifo bytes directly ourselves) we have
144 * three state variables. doing_pdma tells our inline low-level
145 * assembly floppy interrupt entry point whether it should sit and eat
146 * bytes from the fifo or just transfer control up to the higher level
147 * floppy interrupt c-code. I tried very hard but I could not get the
148 * pseudo-dma to work in c-code without getting many overruns and
149 * underruns. If non-zero, doing_pdma encodes the direction of
150 * the transfer for debugging. 1=read 2=write
152 unsigned char *pdma_vaddr
;
153 unsigned long pdma_size
;
154 volatile int doing_pdma
= 0;
156 /* This is software state */
157 char *pdma_base
= NULL
;
158 unsigned long pdma_areasize
;
160 /* Common routines to all controller types on the Sparc. */
161 static void sun_fd_disable_dma(void)
165 mmu_unlockarea(pdma_base
, pdma_areasize
);
170 static void sun_fd_set_dma_mode(int mode
)
180 printk("Unknown dma mode %d\n", mode
);
181 panic("floppy: Giving up...");
185 static void sun_fd_set_dma_addr(char *buffer
)
190 static void sun_fd_set_dma_count(int length
)
195 static void sun_fd_enable_dma(void)
197 pdma_vaddr
= mmu_lockarea(pdma_vaddr
, pdma_size
);
198 pdma_base
= pdma_vaddr
;
199 pdma_areasize
= pdma_size
;
202 irqreturn_t
sparc_floppy_irq(int irq
, void *dev_cookie
)
204 if (likely(doing_pdma
)) {
205 void __iomem
*stat
= (void __iomem
*) fdc_status
;
206 unsigned char *vaddr
= pdma_vaddr
;
207 unsigned long size
= pdma_size
;
212 if (unlikely(!(val
& 0x80))) {
217 if (unlikely(!(val
& 0x20))) {
225 *vaddr
++ = readb(stat
+ 1);
227 unsigned char data
= *vaddr
++;
230 writeb(data
, stat
+ 1);
238 /* Send Terminal Count pulse to floppy controller. */
239 val
= readb(auxio_register
);
240 val
|= AUXIO_AUX1_FTCNT
;
241 writeb(val
, auxio_register
);
242 val
&= ~AUXIO_AUX1_FTCNT
;
243 writeb(val
, auxio_register
);
249 return floppy_interrupt(irq
, dev_cookie
);
252 static int sun_fd_request_irq(void)
260 error
= request_irq(FLOPPY_IRQ
, sparc_floppy_irq
,
261 IRQF_DISABLED
, "floppy", NULL
);
263 return ((error
== 0) ? 0 : -1);
268 static void sun_fd_free_irq(void)
272 static unsigned int sun_get_dma_residue(void)
274 /* XXX This isn't really correct. XXX */
278 static int sun_fd_eject(int drive
)
280 set_dor(0x00, 0xff, 0x90);
282 set_dor(0x00, 0x6f, 0x00);
287 #include <asm/ebus_dma.h>
288 #include <asm/ns87303.h>
290 static struct ebus_dma_info sun_pci_fd_ebus_dma
;
291 static struct device
*sun_floppy_dev
;
292 static int sun_pci_broken_drive
= -1;
294 struct sun_pci_dma_op
{
300 static struct sun_pci_dma_op sun_pci_dma_current
= { -1U, 0, 0, NULL
};
301 static struct sun_pci_dma_op sun_pci_dma_pending
= { -1U, 0, 0, NULL
};
303 extern irqreturn_t
floppy_interrupt(int irq
, void *dev_id
);
305 static unsigned char sun_pci_fd_inb(unsigned long port
)
311 static void sun_pci_fd_outb(unsigned char val
, unsigned long port
)
317 static void sun_pci_fd_broken_outb(unsigned char val
, unsigned long port
)
321 * XXX: Due to SUN's broken floppy connector on AX and AXi
322 * we need to turn on MOTOR_0 also, if the floppy is
323 * jumpered to DS1 (like most PC floppies are). I hope
324 * this does not hurt correct hardware like the AXmp.
325 * (Eddie, Sep 12 1998).
327 if (port
== ((unsigned long)sun_fdc
) + 2) {
328 if (((val
& 0x03) == sun_pci_broken_drive
) && (val
& 0x20)) {
335 #ifdef PCI_FDC_SWAP_DRIVES
336 static void sun_pci_fd_lde_broken_outb(unsigned char val
, unsigned long port
)
340 * XXX: Due to SUN's broken floppy connector on AX and AXi
341 * we need to turn on MOTOR_0 also, if the floppy is
342 * jumpered to DS1 (like most PC floppies are). I hope
343 * this does not hurt correct hardware like the AXmp.
344 * (Eddie, Sep 12 1998).
346 if (port
== ((unsigned long)sun_fdc
) + 2) {
347 if (((val
& 0x03) == sun_pci_broken_drive
) && (val
& 0x10)) {
354 #endif /* PCI_FDC_SWAP_DRIVES */
356 static void sun_pci_fd_enable_dma(void)
358 BUG_ON((NULL
== sun_pci_dma_pending
.buf
) ||
359 (0 == sun_pci_dma_pending
.len
) ||
360 (0 == sun_pci_dma_pending
.direction
));
362 sun_pci_dma_current
.buf
= sun_pci_dma_pending
.buf
;
363 sun_pci_dma_current
.len
= sun_pci_dma_pending
.len
;
364 sun_pci_dma_current
.direction
= sun_pci_dma_pending
.direction
;
366 sun_pci_dma_pending
.buf
= NULL
;
367 sun_pci_dma_pending
.len
= 0;
368 sun_pci_dma_pending
.direction
= 0;
369 sun_pci_dma_pending
.addr
= -1U;
371 sun_pci_dma_current
.addr
=
372 dma_map_single(sun_floppy_dev
,
373 sun_pci_dma_current
.buf
,
374 sun_pci_dma_current
.len
,
375 sun_pci_dma_current
.direction
);
377 ebus_dma_enable(&sun_pci_fd_ebus_dma
, 1);
379 if (ebus_dma_request(&sun_pci_fd_ebus_dma
,
380 sun_pci_dma_current
.addr
,
381 sun_pci_dma_current
.len
))
385 static void sun_pci_fd_disable_dma(void)
387 ebus_dma_enable(&sun_pci_fd_ebus_dma
, 0);
388 if (sun_pci_dma_current
.addr
!= -1U)
389 dma_unmap_single(sun_floppy_dev
,
390 sun_pci_dma_current
.addr
,
391 sun_pci_dma_current
.len
,
392 sun_pci_dma_current
.direction
);
393 sun_pci_dma_current
.addr
= -1U;
396 static void sun_pci_fd_set_dma_mode(int mode
)
398 if (mode
== DMA_MODE_WRITE
)
399 sun_pci_dma_pending
.direction
= DMA_TO_DEVICE
;
401 sun_pci_dma_pending
.direction
= DMA_FROM_DEVICE
;
403 ebus_dma_prepare(&sun_pci_fd_ebus_dma
, mode
!= DMA_MODE_WRITE
);
406 static void sun_pci_fd_set_dma_count(int length
)
408 sun_pci_dma_pending
.len
= length
;
411 static void sun_pci_fd_set_dma_addr(char *buffer
)
413 sun_pci_dma_pending
.buf
= buffer
;
416 static unsigned int sun_pci_get_dma_residue(void)
418 return ebus_dma_residue(&sun_pci_fd_ebus_dma
);
421 static int sun_pci_fd_request_irq(void)
423 return ebus_dma_irq_enable(&sun_pci_fd_ebus_dma
, 1);
426 static void sun_pci_fd_free_irq(void)
428 ebus_dma_irq_enable(&sun_pci_fd_ebus_dma
, 0);
431 static int sun_pci_fd_eject(int drive
)
436 void sun_pci_fd_dma_callback(struct ebus_dma_info
*p
, int event
, void *cookie
)
438 floppy_interrupt(0, NULL
);
442 * Floppy probing, we'd like to use /dev/fd0 for a single Floppy on PCI,
443 * even if this is configured using DS1, thus looks like /dev/fd1 with
444 * the cabling used in Ultras.
446 #define DOR (port + 2)
447 #define MSR (port + 4)
448 #define FIFO (port + 5)
450 static void sun_pci_fd_out_byte(unsigned long port
, unsigned char val
,
453 unsigned char status
;
456 while (!((status
= inb(MSR
)) & 0x80) && --timeout
)
461 static unsigned char sun_pci_fd_sensei(unsigned long port
)
463 unsigned char result
[2] = { 0x70, 0x00 };
464 unsigned char status
;
467 sun_pci_fd_out_byte(port
, 0x08, FIFO
);
471 while (!((status
= inb(MSR
)) & 0x80) && --timeout
)
477 if ((status
& 0xf0) == 0xd0)
478 result
[i
++] = inb(FIFO
);
486 static void sun_pci_fd_reset(unsigned long port
)
488 unsigned char mask
= 0x00;
489 unsigned char status
;
494 status
= sun_pci_fd_sensei(port
);
495 if ((status
& 0xc0) == 0xc0)
496 mask
|= 1 << (status
& 0x03);
499 } while ((mask
!= 0x0f) && --timeout
);
502 static int sun_pci_fd_test_drive(unsigned long port
, int drive
)
504 unsigned char status
, data
;
508 sun_pci_fd_reset(port
);
510 data
= (0x10 << drive
) | 0x0c | drive
;
511 sun_pci_fd_out_byte(port
, data
, DOR
);
513 sun_pci_fd_out_byte(port
, 0x07, FIFO
);
514 sun_pci_fd_out_byte(port
, drive
& 0x03, FIFO
);
518 status
= sun_pci_fd_sensei(port
);
519 } while (((status
& 0xc0) == 0x80) && --timeout
);
524 ready
= (status
& 0x10) ? 0 : 1;
526 sun_pci_fd_reset(port
);
533 static int __init
ebus_fdthree_p(struct device_node
*dp
)
535 if (!strcmp(dp
->name
, "fdthree"))
537 if (!strcmp(dp
->name
, "floppy")) {
540 compat
= of_get_property(dp
, "compatible", NULL
);
541 if (compat
&& !strcmp(compat
, "fdthree"))
547 static unsigned long __init
sun_floppy_init(void)
549 static int initialized
= 0;
550 struct device_node
*dp
;
551 struct platform_device
*op
;
556 return sun_floppy_types
[0];
561 for_each_node_by_name(dp
, "SUNW,fdtwo") {
562 if (strcmp(dp
->parent
->name
, "sbus"))
564 op
= of_find_device_by_node(dp
);
570 FLOPPY_IRQ
= op
->archdata
.irqs
[0];
572 struct device_node
*ebus_dp
;
573 void __iomem
*auxio_reg
;
574 const char *state_prop
;
575 unsigned long config
;
578 for_each_node_by_name(ebus_dp
, "ebus") {
579 for (dp
= ebus_dp
->child
; dp
; dp
= dp
->sibling
) {
580 if (ebus_fdthree_p(dp
))
588 op
= of_find_device_by_node(dp
);
592 state_prop
= of_get_property(op
->dev
.of_node
, "status", NULL
);
593 if (state_prop
&& !strncmp(state_prop
, "disabled", 8))
596 FLOPPY_IRQ
= op
->archdata
.irqs
[0];
598 /* Make sure the high density bit is set, some systems
599 * (most notably Ultra5/Ultra10) come up with it clear.
601 auxio_reg
= (void __iomem
*) op
->resource
[2].start
;
602 writel(readl(auxio_reg
)|0x2, auxio_reg
);
604 sun_floppy_dev
= &op
->dev
;
606 spin_lock_init(&sun_pci_fd_ebus_dma
.lock
);
609 sun_pci_fd_ebus_dma
.regs
= (void __iomem
*)
610 op
->resource
[1].start
;
611 if (!sun_pci_fd_ebus_dma
.regs
)
614 sun_pci_fd_ebus_dma
.flags
= (EBUS_DMA_FLAG_USE_EBDMA_HANDLER
|
615 EBUS_DMA_FLAG_TCI_DISABLE
);
616 sun_pci_fd_ebus_dma
.callback
= sun_pci_fd_dma_callback
;
617 sun_pci_fd_ebus_dma
.client_cookie
= NULL
;
618 sun_pci_fd_ebus_dma
.irq
= FLOPPY_IRQ
;
619 strcpy(sun_pci_fd_ebus_dma
.name
, "floppy");
620 if (ebus_dma_register(&sun_pci_fd_ebus_dma
))
624 sun_fdc
= (struct sun_flpy_controller
*) op
->resource
[0].start
;
626 sun_fdops
.fd_inb
= sun_pci_fd_inb
;
627 sun_fdops
.fd_outb
= sun_pci_fd_outb
;
629 can_use_virtual_dma
= use_virtual_dma
= 0;
630 sun_fdops
.fd_enable_dma
= sun_pci_fd_enable_dma
;
631 sun_fdops
.fd_disable_dma
= sun_pci_fd_disable_dma
;
632 sun_fdops
.fd_set_dma_mode
= sun_pci_fd_set_dma_mode
;
633 sun_fdops
.fd_set_dma_addr
= sun_pci_fd_set_dma_addr
;
634 sun_fdops
.fd_set_dma_count
= sun_pci_fd_set_dma_count
;
635 sun_fdops
.get_dma_residue
= sun_pci_get_dma_residue
;
637 sun_fdops
.fd_request_irq
= sun_pci_fd_request_irq
;
638 sun_fdops
.fd_free_irq
= sun_pci_fd_free_irq
;
640 sun_fdops
.fd_eject
= sun_pci_fd_eject
;
642 fdc_status
= (unsigned long) &sun_fdc
->status_82077
;
645 * XXX: Find out on which machines this is really needed.
648 sun_pci_broken_drive
= 1;
649 sun_fdops
.fd_outb
= sun_pci_fd_broken_outb
;
652 allowed_drive_mask
= 0;
653 if (sun_pci_fd_test_drive((unsigned long)sun_fdc
, 0))
654 sun_floppy_types
[0] = 4;
655 if (sun_pci_fd_test_drive((unsigned long)sun_fdc
, 1))
656 sun_floppy_types
[1] = 4;
659 * Find NS87303 SuperIO config registers (through ecpp).
662 for (dp
= ebus_dp
->child
; dp
; dp
= dp
->sibling
) {
663 if (!strcmp(dp
->name
, "ecpp")) {
664 struct platform_device
*ecpp_op
;
666 ecpp_op
= of_find_device_by_node(dp
);
668 config
= ecpp_op
->resource
[1].start
;
675 * Sanity check, is this really the NS87303?
677 switch (config
& 0x3ff) {
688 return sun_floppy_types
[0];
690 /* Enable PC-AT mode. */
691 ns87303_modify(config
, ASC
, 0, 0xc0);
693 #ifdef PCI_FDC_SWAP_DRIVES
695 * If only Floppy 1 is present, swap drives.
697 if (!sun_floppy_types
[0] && sun_floppy_types
[1]) {
699 * Set the drive exchange bit in FCR on NS87303,
700 * make sure other bits are sane before doing so.
702 ns87303_modify(config
, FER
, FER_EDM
, 0);
703 ns87303_modify(config
, ASC
, ASC_DRV2_SEL
, 0);
704 ns87303_modify(config
, FCR
, 0, FCR_LDE
);
706 config
= sun_floppy_types
[0];
707 sun_floppy_types
[0] = sun_floppy_types
[1];
708 sun_floppy_types
[1] = config
;
710 if (sun_pci_broken_drive
!= -1) {
711 sun_pci_broken_drive
= 1 - sun_pci_broken_drive
;
712 sun_fdops
.fd_outb
= sun_pci_fd_lde_broken_outb
;
715 #endif /* PCI_FDC_SWAP_DRIVES */
717 return sun_floppy_types
[0];
719 prop
= of_get_property(op
->dev
.of_node
, "status", NULL
);
720 if (prop
&& !strncmp(state
, "disabled", 8))
724 * We cannot do of_ioremap here: it does request_region,
725 * which the generic floppy driver tries to do once again.
726 * But we must use the sdev resource values as they have
727 * had parent ranges applied.
729 sun_fdc
= (struct sun_flpy_controller
*)
730 (op
->resource
[0].start
+
731 ((op
->resource
[0].flags
& 0x1ffUL
) << 32UL));
733 /* Last minute sanity check... */
734 if (sbus_readb(&sun_fdc
->status1_82077
) == 0xff) {
735 sun_fdc
= (struct sun_flpy_controller
*)-1;
739 sun_fdops
.fd_inb
= sun_82077_fd_inb
;
740 sun_fdops
.fd_outb
= sun_82077_fd_outb
;
742 can_use_virtual_dma
= use_virtual_dma
= 1;
743 sun_fdops
.fd_enable_dma
= sun_fd_enable_dma
;
744 sun_fdops
.fd_disable_dma
= sun_fd_disable_dma
;
745 sun_fdops
.fd_set_dma_mode
= sun_fd_set_dma_mode
;
746 sun_fdops
.fd_set_dma_addr
= sun_fd_set_dma_addr
;
747 sun_fdops
.fd_set_dma_count
= sun_fd_set_dma_count
;
748 sun_fdops
.get_dma_residue
= sun_get_dma_residue
;
750 sun_fdops
.fd_request_irq
= sun_fd_request_irq
;
751 sun_fdops
.fd_free_irq
= sun_fd_free_irq
;
753 sun_fdops
.fd_eject
= sun_fd_eject
;
755 fdc_status
= (unsigned long) &sun_fdc
->status_82077
;
758 allowed_drive_mask
= 0x01;
759 sun_floppy_types
[0] = 4;
760 sun_floppy_types
[1] = 0;
762 return sun_floppy_types
[0];
765 #define EXTRA_FLOPPY_PARAMS
767 static DEFINE_SPINLOCK(dma_spin_lock
);
769 #define claim_dma_lock() \
770 ({ unsigned long flags; \
771 spin_lock_irqsave(&dma_spin_lock, flags); \
775 #define release_dma_lock(__flags) \
776 spin_unlock_irqrestore(&dma_spin_lock, __flags);
778 #endif /* !(__ASM_SPARC64_FLOPPY_H) */