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)
167 static void sun_fd_set_dma_mode(int mode
)
177 printk("Unknown dma mode %d\n", mode
);
178 panic("floppy: Giving up...");
182 static void sun_fd_set_dma_addr(char *buffer
)
187 static void sun_fd_set_dma_count(int length
)
192 static void sun_fd_enable_dma(void)
194 pdma_base
= pdma_vaddr
;
195 pdma_areasize
= pdma_size
;
198 irqreturn_t
sparc_floppy_irq(int irq
, void *dev_cookie
)
200 if (likely(doing_pdma
)) {
201 void __iomem
*stat
= (void __iomem
*) fdc_status
;
202 unsigned char *vaddr
= pdma_vaddr
;
203 unsigned long size
= pdma_size
;
208 if (unlikely(!(val
& 0x80))) {
213 if (unlikely(!(val
& 0x20))) {
221 *vaddr
++ = readb(stat
+ 1);
223 unsigned char data
= *vaddr
++;
226 writeb(data
, stat
+ 1);
234 /* Send Terminal Count pulse to floppy controller. */
235 val
= readb(auxio_register
);
236 val
|= AUXIO_AUX1_FTCNT
;
237 writeb(val
, auxio_register
);
238 val
&= ~AUXIO_AUX1_FTCNT
;
239 writeb(val
, auxio_register
);
245 return floppy_interrupt(irq
, dev_cookie
);
248 static int sun_fd_request_irq(void)
256 error
= request_irq(FLOPPY_IRQ
, sparc_floppy_irq
,
259 return ((error
== 0) ? 0 : -1);
264 static void sun_fd_free_irq(void)
268 static unsigned int sun_get_dma_residue(void)
270 /* XXX This isn't really correct. XXX */
274 static int sun_fd_eject(int drive
)
276 set_dor(0x00, 0xff, 0x90);
278 set_dor(0x00, 0x6f, 0x00);
283 #include <asm/ebus_dma.h>
284 #include <asm/ns87303.h>
286 static struct ebus_dma_info sun_pci_fd_ebus_dma
;
287 static struct device
*sun_floppy_dev
;
288 static int sun_pci_broken_drive
= -1;
290 struct sun_pci_dma_op
{
296 static struct sun_pci_dma_op sun_pci_dma_current
= { -1U, 0, 0, NULL
};
297 static struct sun_pci_dma_op sun_pci_dma_pending
= { -1U, 0, 0, NULL
};
299 irqreturn_t
floppy_interrupt(int irq
, void *dev_id
);
301 static unsigned char sun_pci_fd_inb(unsigned long port
)
307 static void sun_pci_fd_outb(unsigned char val
, unsigned long port
)
313 static void sun_pci_fd_broken_outb(unsigned char val
, unsigned long port
)
317 * XXX: Due to SUN's broken floppy connector on AX and AXi
318 * we need to turn on MOTOR_0 also, if the floppy is
319 * jumpered to DS1 (like most PC floppies are). I hope
320 * this does not hurt correct hardware like the AXmp.
321 * (Eddie, Sep 12 1998).
323 if (port
== ((unsigned long)sun_fdc
) + 2) {
324 if (((val
& 0x03) == sun_pci_broken_drive
) && (val
& 0x20)) {
331 #ifdef PCI_FDC_SWAP_DRIVES
332 static void sun_pci_fd_lde_broken_outb(unsigned char val
, unsigned long port
)
336 * XXX: Due to SUN's broken floppy connector on AX and AXi
337 * we need to turn on MOTOR_0 also, if the floppy is
338 * jumpered to DS1 (like most PC floppies are). I hope
339 * this does not hurt correct hardware like the AXmp.
340 * (Eddie, Sep 12 1998).
342 if (port
== ((unsigned long)sun_fdc
) + 2) {
343 if (((val
& 0x03) == sun_pci_broken_drive
) && (val
& 0x10)) {
350 #endif /* PCI_FDC_SWAP_DRIVES */
352 static void sun_pci_fd_enable_dma(void)
354 BUG_ON((NULL
== sun_pci_dma_pending
.buf
) ||
355 (0 == sun_pci_dma_pending
.len
) ||
356 (0 == sun_pci_dma_pending
.direction
));
358 sun_pci_dma_current
.buf
= sun_pci_dma_pending
.buf
;
359 sun_pci_dma_current
.len
= sun_pci_dma_pending
.len
;
360 sun_pci_dma_current
.direction
= sun_pci_dma_pending
.direction
;
362 sun_pci_dma_pending
.buf
= NULL
;
363 sun_pci_dma_pending
.len
= 0;
364 sun_pci_dma_pending
.direction
= 0;
365 sun_pci_dma_pending
.addr
= -1U;
367 sun_pci_dma_current
.addr
=
368 dma_map_single(sun_floppy_dev
,
369 sun_pci_dma_current
.buf
,
370 sun_pci_dma_current
.len
,
371 sun_pci_dma_current
.direction
);
373 ebus_dma_enable(&sun_pci_fd_ebus_dma
, 1);
375 if (ebus_dma_request(&sun_pci_fd_ebus_dma
,
376 sun_pci_dma_current
.addr
,
377 sun_pci_dma_current
.len
))
381 static void sun_pci_fd_disable_dma(void)
383 ebus_dma_enable(&sun_pci_fd_ebus_dma
, 0);
384 if (sun_pci_dma_current
.addr
!= -1U)
385 dma_unmap_single(sun_floppy_dev
,
386 sun_pci_dma_current
.addr
,
387 sun_pci_dma_current
.len
,
388 sun_pci_dma_current
.direction
);
389 sun_pci_dma_current
.addr
= -1U;
392 static void sun_pci_fd_set_dma_mode(int mode
)
394 if (mode
== DMA_MODE_WRITE
)
395 sun_pci_dma_pending
.direction
= DMA_TO_DEVICE
;
397 sun_pci_dma_pending
.direction
= DMA_FROM_DEVICE
;
399 ebus_dma_prepare(&sun_pci_fd_ebus_dma
, mode
!= DMA_MODE_WRITE
);
402 static void sun_pci_fd_set_dma_count(int length
)
404 sun_pci_dma_pending
.len
= length
;
407 static void sun_pci_fd_set_dma_addr(char *buffer
)
409 sun_pci_dma_pending
.buf
= buffer
;
412 static unsigned int sun_pci_get_dma_residue(void)
414 return ebus_dma_residue(&sun_pci_fd_ebus_dma
);
417 static int sun_pci_fd_request_irq(void)
419 return ebus_dma_irq_enable(&sun_pci_fd_ebus_dma
, 1);
422 static void sun_pci_fd_free_irq(void)
424 ebus_dma_irq_enable(&sun_pci_fd_ebus_dma
, 0);
427 static int sun_pci_fd_eject(int drive
)
432 void sun_pci_fd_dma_callback(struct ebus_dma_info
*p
, int event
, void *cookie
)
434 floppy_interrupt(0, NULL
);
438 * Floppy probing, we'd like to use /dev/fd0 for a single Floppy on PCI,
439 * even if this is configured using DS1, thus looks like /dev/fd1 with
440 * the cabling used in Ultras.
442 #define DOR (port + 2)
443 #define MSR (port + 4)
444 #define FIFO (port + 5)
446 static void sun_pci_fd_out_byte(unsigned long port
, unsigned char val
,
449 unsigned char status
;
452 while (!((status
= inb(MSR
)) & 0x80) && --timeout
)
457 static unsigned char sun_pci_fd_sensei(unsigned long port
)
459 unsigned char result
[2] = { 0x70, 0x00 };
460 unsigned char status
;
463 sun_pci_fd_out_byte(port
, 0x08, FIFO
);
467 while (!((status
= inb(MSR
)) & 0x80) && --timeout
)
473 if ((status
& 0xf0) == 0xd0)
474 result
[i
++] = inb(FIFO
);
482 static void sun_pci_fd_reset(unsigned long port
)
484 unsigned char mask
= 0x00;
485 unsigned char status
;
490 status
= sun_pci_fd_sensei(port
);
491 if ((status
& 0xc0) == 0xc0)
492 mask
|= 1 << (status
& 0x03);
495 } while ((mask
!= 0x0f) && --timeout
);
498 static int sun_pci_fd_test_drive(unsigned long port
, int drive
)
500 unsigned char status
, data
;
504 sun_pci_fd_reset(port
);
506 data
= (0x10 << drive
) | 0x0c | drive
;
507 sun_pci_fd_out_byte(port
, data
, DOR
);
509 sun_pci_fd_out_byte(port
, 0x07, FIFO
);
510 sun_pci_fd_out_byte(port
, drive
& 0x03, FIFO
);
514 status
= sun_pci_fd_sensei(port
);
515 } while (((status
& 0xc0) == 0x80) && --timeout
);
520 ready
= (status
& 0x10) ? 0 : 1;
522 sun_pci_fd_reset(port
);
529 static int __init
ebus_fdthree_p(struct device_node
*dp
)
531 if (!strcmp(dp
->name
, "fdthree"))
533 if (!strcmp(dp
->name
, "floppy")) {
536 compat
= of_get_property(dp
, "compatible", NULL
);
537 if (compat
&& !strcmp(compat
, "fdthree"))
543 static unsigned long __init
sun_floppy_init(void)
545 static int initialized
= 0;
546 struct device_node
*dp
;
547 struct platform_device
*op
;
552 return sun_floppy_types
[0];
557 for_each_node_by_name(dp
, "SUNW,fdtwo") {
558 if (strcmp(dp
->parent
->name
, "sbus"))
560 op
= of_find_device_by_node(dp
);
566 FLOPPY_IRQ
= op
->archdata
.irqs
[0];
568 struct device_node
*ebus_dp
;
569 void __iomem
*auxio_reg
;
570 const char *state_prop
;
571 unsigned long config
;
574 for_each_node_by_name(ebus_dp
, "ebus") {
575 for (dp
= ebus_dp
->child
; dp
; dp
= dp
->sibling
) {
576 if (ebus_fdthree_p(dp
))
584 op
= of_find_device_by_node(dp
);
588 state_prop
= of_get_property(op
->dev
.of_node
, "status", NULL
);
589 if (state_prop
&& !strncmp(state_prop
, "disabled", 8))
592 FLOPPY_IRQ
= op
->archdata
.irqs
[0];
594 /* Make sure the high density bit is set, some systems
595 * (most notably Ultra5/Ultra10) come up with it clear.
597 auxio_reg
= (void __iomem
*) op
->resource
[2].start
;
598 writel(readl(auxio_reg
)|0x2, auxio_reg
);
600 sun_floppy_dev
= &op
->dev
;
602 spin_lock_init(&sun_pci_fd_ebus_dma
.lock
);
605 sun_pci_fd_ebus_dma
.regs
= (void __iomem
*)
606 op
->resource
[1].start
;
607 if (!sun_pci_fd_ebus_dma
.regs
)
610 sun_pci_fd_ebus_dma
.flags
= (EBUS_DMA_FLAG_USE_EBDMA_HANDLER
|
611 EBUS_DMA_FLAG_TCI_DISABLE
);
612 sun_pci_fd_ebus_dma
.callback
= sun_pci_fd_dma_callback
;
613 sun_pci_fd_ebus_dma
.client_cookie
= NULL
;
614 sun_pci_fd_ebus_dma
.irq
= FLOPPY_IRQ
;
615 strcpy(sun_pci_fd_ebus_dma
.name
, "floppy");
616 if (ebus_dma_register(&sun_pci_fd_ebus_dma
))
620 sun_fdc
= (struct sun_flpy_controller
*) op
->resource
[0].start
;
622 sun_fdops
.fd_inb
= sun_pci_fd_inb
;
623 sun_fdops
.fd_outb
= sun_pci_fd_outb
;
625 can_use_virtual_dma
= use_virtual_dma
= 0;
626 sun_fdops
.fd_enable_dma
= sun_pci_fd_enable_dma
;
627 sun_fdops
.fd_disable_dma
= sun_pci_fd_disable_dma
;
628 sun_fdops
.fd_set_dma_mode
= sun_pci_fd_set_dma_mode
;
629 sun_fdops
.fd_set_dma_addr
= sun_pci_fd_set_dma_addr
;
630 sun_fdops
.fd_set_dma_count
= sun_pci_fd_set_dma_count
;
631 sun_fdops
.get_dma_residue
= sun_pci_get_dma_residue
;
633 sun_fdops
.fd_request_irq
= sun_pci_fd_request_irq
;
634 sun_fdops
.fd_free_irq
= sun_pci_fd_free_irq
;
636 sun_fdops
.fd_eject
= sun_pci_fd_eject
;
638 fdc_status
= (unsigned long) &sun_fdc
->status_82077
;
641 * XXX: Find out on which machines this is really needed.
644 sun_pci_broken_drive
= 1;
645 sun_fdops
.fd_outb
= sun_pci_fd_broken_outb
;
648 allowed_drive_mask
= 0;
649 if (sun_pci_fd_test_drive((unsigned long)sun_fdc
, 0))
650 sun_floppy_types
[0] = 4;
651 if (sun_pci_fd_test_drive((unsigned long)sun_fdc
, 1))
652 sun_floppy_types
[1] = 4;
655 * Find NS87303 SuperIO config registers (through ecpp).
658 for (dp
= ebus_dp
->child
; dp
; dp
= dp
->sibling
) {
659 if (!strcmp(dp
->name
, "ecpp")) {
660 struct platform_device
*ecpp_op
;
662 ecpp_op
= of_find_device_by_node(dp
);
664 config
= ecpp_op
->resource
[1].start
;
671 * Sanity check, is this really the NS87303?
673 switch (config
& 0x3ff) {
684 return sun_floppy_types
[0];
686 /* Enable PC-AT mode. */
687 ns87303_modify(config
, ASC
, 0, 0xc0);
689 #ifdef PCI_FDC_SWAP_DRIVES
691 * If only Floppy 1 is present, swap drives.
693 if (!sun_floppy_types
[0] && sun_floppy_types
[1]) {
695 * Set the drive exchange bit in FCR on NS87303,
696 * make sure other bits are sane before doing so.
698 ns87303_modify(config
, FER
, FER_EDM
, 0);
699 ns87303_modify(config
, ASC
, ASC_DRV2_SEL
, 0);
700 ns87303_modify(config
, FCR
, 0, FCR_LDE
);
702 config
= sun_floppy_types
[0];
703 sun_floppy_types
[0] = sun_floppy_types
[1];
704 sun_floppy_types
[1] = config
;
706 if (sun_pci_broken_drive
!= -1) {
707 sun_pci_broken_drive
= 1 - sun_pci_broken_drive
;
708 sun_fdops
.fd_outb
= sun_pci_fd_lde_broken_outb
;
711 #endif /* PCI_FDC_SWAP_DRIVES */
713 return sun_floppy_types
[0];
715 prop
= of_get_property(op
->dev
.of_node
, "status", NULL
);
716 if (prop
&& !strncmp(state
, "disabled", 8))
720 * We cannot do of_ioremap here: it does request_region,
721 * which the generic floppy driver tries to do once again.
722 * But we must use the sdev resource values as they have
723 * had parent ranges applied.
725 sun_fdc
= (struct sun_flpy_controller
*)
726 (op
->resource
[0].start
+
727 ((op
->resource
[0].flags
& 0x1ffUL
) << 32UL));
729 /* Last minute sanity check... */
730 if (sbus_readb(&sun_fdc
->status1_82077
) == 0xff) {
731 sun_fdc
= (struct sun_flpy_controller
*)-1;
735 sun_fdops
.fd_inb
= sun_82077_fd_inb
;
736 sun_fdops
.fd_outb
= sun_82077_fd_outb
;
738 can_use_virtual_dma
= use_virtual_dma
= 1;
739 sun_fdops
.fd_enable_dma
= sun_fd_enable_dma
;
740 sun_fdops
.fd_disable_dma
= sun_fd_disable_dma
;
741 sun_fdops
.fd_set_dma_mode
= sun_fd_set_dma_mode
;
742 sun_fdops
.fd_set_dma_addr
= sun_fd_set_dma_addr
;
743 sun_fdops
.fd_set_dma_count
= sun_fd_set_dma_count
;
744 sun_fdops
.get_dma_residue
= sun_get_dma_residue
;
746 sun_fdops
.fd_request_irq
= sun_fd_request_irq
;
747 sun_fdops
.fd_free_irq
= sun_fd_free_irq
;
749 sun_fdops
.fd_eject
= sun_fd_eject
;
751 fdc_status
= (unsigned long) &sun_fdc
->status_82077
;
754 allowed_drive_mask
= 0x01;
755 sun_floppy_types
[0] = 4;
756 sun_floppy_types
[1] = 0;
758 return sun_floppy_types
[0];
761 #define EXTRA_FLOPPY_PARAMS
763 static DEFINE_SPINLOCK(dma_spin_lock
);
765 #define claim_dma_lock() \
766 ({ unsigned long flags; \
767 spin_lock_irqsave(&dma_spin_lock, flags); \
771 #define release_dma_lock(__flags) \
772 spin_unlock_irqrestore(&dma_spin_lock, __flags);
774 #endif /* !(__ASM_SPARC64_FLOPPY_H) */