xref: /linux/arch/sparc/include/asm/floppy_64.h (revision 55a42f78ffd386e01a5404419f8c5ded7db70a21)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* floppy.h: Sparc specific parts of the Floppy driver.
3  *
4  * Copyright (C) 1996, 2007, 2008 David S. Miller (davem@davemloft.net)
5  * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6  *
7  * Ultra/PCI support added: Sep 1997  Eddie C. Dost  (ecd@skynet.be)
8  */
9 
10 #ifndef __ASM_SPARC64_FLOPPY_H
11 #define __ASM_SPARC64_FLOPPY_H
12 
13 #include <linux/of.h>
14 #include <linux/of_platform.h>
15 #include <linux/dma-mapping.h>
16 
17 #include <asm/auxio.h>
18 
19 /*
20  * Define this to enable exchanging drive 0 and 1 if only drive 1 is
21  * probed on PCI machines.
22  */
23 #undef PCI_FDC_SWAP_DRIVES
24 
25 
26 /* References:
27  * 1) Netbsd Sun floppy driver.
28  * 2) NCR 82077 controller manual
29  * 3) Intel 82077 controller manual
30  */
31 struct sun_flpy_controller {
32 	volatile unsigned char status1_82077; /* Auxiliary Status reg. 1 */
33 	volatile unsigned char status2_82077; /* Auxiliary Status reg. 2 */
34 	volatile unsigned char dor_82077;     /* Digital Output reg. */
35 	volatile unsigned char tapectl_82077; /* Tape Control reg */
36 	volatile unsigned char status_82077;  /* Main Status Register. */
37 #define drs_82077              status_82077   /* Digital Rate Select reg. */
38 	volatile unsigned char data_82077;    /* Data fifo. */
39 	volatile unsigned char ___unused;
40 	volatile unsigned char dir_82077;     /* Digital Input reg. */
41 #define dcr_82077              dir_82077      /* Config Control reg. */
42 };
43 
44 /* You'll only ever find one controller on an Ultra anyways. */
45 static struct sun_flpy_controller *sun_fdc = (struct sun_flpy_controller *)-1;
46 unsigned long fdc_status;
47 static struct platform_device *floppy_op = NULL;
48 
49 struct sun_floppy_ops {
50 	unsigned char	(*fd_inb) (unsigned long port, unsigned int reg);
51 	void		(*fd_outb) (unsigned char value, unsigned long base,
52 				    unsigned int reg);
53 	void		(*fd_enable_dma) (void);
54 	void		(*fd_disable_dma) (void);
55 	void		(*fd_set_dma_mode) (int);
56 	void		(*fd_set_dma_addr) (char *);
57 	void		(*fd_set_dma_count) (int);
58 	unsigned int	(*get_dma_residue) (void);
59 	int		(*fd_request_irq) (void);
60 	void		(*fd_free_irq) (void);
61 	int		(*fd_eject) (int);
62 };
63 
64 static struct sun_floppy_ops sun_fdops;
65 
66 #define fd_inb(base, reg)         sun_fdops.fd_inb(base, reg)
67 #define fd_outb(value, base, reg) sun_fdops.fd_outb(value, base, reg)
68 #define fd_enable_dma()           sun_fdops.fd_enable_dma()
69 #define fd_disable_dma()          sun_fdops.fd_disable_dma()
70 #define fd_request_dma()          (0) /* nothing... */
71 #define fd_free_dma()             /* nothing... */
72 #define fd_clear_dma_ff()         /* nothing... */
73 #define fd_set_dma_mode(mode)     sun_fdops.fd_set_dma_mode(mode)
74 #define fd_set_dma_addr(addr)     sun_fdops.fd_set_dma_addr(addr)
75 #define fd_set_dma_count(count)   sun_fdops.fd_set_dma_count(count)
76 #define get_dma_residue(x)        sun_fdops.get_dma_residue()
77 #define fd_request_irq()          sun_fdops.fd_request_irq()
78 #define fd_free_irq()             sun_fdops.fd_free_irq()
79 #define fd_eject(drive)           sun_fdops.fd_eject(drive)
80 
81 /* Super paranoid... */
82 #undef HAVE_DISABLE_HLT
83 
84 static int sun_floppy_types[2] = { 0, 0 };
85 
86 /* Here is where we catch the floppy driver trying to initialize,
87  * therefore this is where we call the PROM device tree probing
88  * routine etc. on the Sparc.
89  */
90 #define FLOPPY0_TYPE		sun_floppy_init()
91 #define FLOPPY1_TYPE		sun_floppy_types[1]
92 
93 #define FDC1			((unsigned long)sun_fdc)
94 
95 #define N_FDC    1
96 #define N_DRIVE  8
97 
98 static unsigned char sun_82077_fd_inb(unsigned long base, unsigned int reg)
99 {
100 	udelay(5);
101 	switch (reg) {
102 	default:
103 		printk("floppy: Asked to read unknown port %x\n", reg);
104 		panic("floppy: Port bolixed.");
105 	case FD_STATUS:
106 		return sbus_readb(&sun_fdc->status_82077) & ~STATUS_DMA;
107 	case FD_DATA:
108 		return sbus_readb(&sun_fdc->data_82077);
109 	case FD_DIR:
110 		/* XXX: Is DCL on 0x80 in sun4m? */
111 		return sbus_readb(&sun_fdc->dir_82077);
112 	}
113 	panic("sun_82072_fd_inb: How did I get here?");
114 }
115 
116 static void sun_82077_fd_outb(unsigned char value, unsigned long base,
117 			      unsigned int reg)
118 {
119 	udelay(5);
120 	switch (reg) {
121 	default:
122 		printk("floppy: Asked to write to unknown port %x\n", reg);
123 		panic("floppy: Port bolixed.");
124 	case FD_DOR:
125 		/* Happily, the 82077 has a real DOR register. */
126 		sbus_writeb(value, &sun_fdc->dor_82077);
127 		break;
128 	case FD_DATA:
129 		sbus_writeb(value, &sun_fdc->data_82077);
130 		break;
131 	case FD_DCR:
132 		sbus_writeb(value, &sun_fdc->dcr_82077);
133 		break;
134 	case FD_DSR:
135 		sbus_writeb(value, &sun_fdc->status_82077);
136 		break;
137 	}
138 	return;
139 }
140 
141 /* For pseudo-dma (Sun floppy drives have no real DMA available to
142  * them so we must eat the data fifo bytes directly ourselves) we have
143  * three state variables.  doing_pdma tells our inline low-level
144  * assembly floppy interrupt entry point whether it should sit and eat
145  * bytes from the fifo or just transfer control up to the higher level
146  * floppy interrupt c-code.  I tried very hard but I could not get the
147  * pseudo-dma to work in c-code without getting many overruns and
148  * underruns.  If non-zero, doing_pdma encodes the direction of
149  * the transfer for debugging.  1=read 2=write
150  */
151 unsigned char *pdma_vaddr;
152 unsigned long pdma_size;
153 volatile int doing_pdma = 0;
154 
155 /* This is software state */
156 char *pdma_base = NULL;
157 unsigned long pdma_areasize;
158 
159 /* Common routines to all controller types on the Sparc. */
160 static void sun_fd_disable_dma(void)
161 {
162 	doing_pdma = 0;
163 	pdma_base = NULL;
164 }
165 
166 static void sun_fd_set_dma_mode(int mode)
167 {
168 	switch(mode) {
169 	case DMA_MODE_READ:
170 		doing_pdma = 1;
171 		break;
172 	case DMA_MODE_WRITE:
173 		doing_pdma = 2;
174 		break;
175 	default:
176 		printk("Unknown dma mode %d\n", mode);
177 		panic("floppy: Giving up...");
178 	}
179 }
180 
181 static void sun_fd_set_dma_addr(char *buffer)
182 {
183 	pdma_vaddr = buffer;
184 }
185 
186 static void sun_fd_set_dma_count(int length)
187 {
188 	pdma_size = length;
189 }
190 
191 static void sun_fd_enable_dma(void)
192 {
193 	pdma_base = pdma_vaddr;
194 	pdma_areasize = pdma_size;
195 }
196 
197 static irqreturn_t sparc_floppy_irq(int irq, void *dev_cookie)
198 {
199 	if (likely(doing_pdma)) {
200 		void __iomem *stat = (void __iomem *) fdc_status;
201 		unsigned char *vaddr = pdma_vaddr;
202 		unsigned long size = pdma_size;
203 		u8 val;
204 
205 		while (size) {
206 			val = readb(stat);
207 			if (unlikely(!(val & 0x80))) {
208 				pdma_vaddr = vaddr;
209 				pdma_size = size;
210 				return IRQ_HANDLED;
211 			}
212 			if (unlikely(!(val & 0x20))) {
213 				pdma_vaddr = vaddr;
214 				pdma_size = size;
215 				doing_pdma = 0;
216 				goto main_interrupt;
217 			}
218 			if (val & 0x40) {
219 				/* read */
220 				*vaddr++ = readb(stat + 1);
221 			} else {
222 				unsigned char data = *vaddr++;
223 
224 				/* write */
225 				writeb(data, stat + 1);
226 			}
227 			size--;
228 		}
229 
230 		pdma_vaddr = vaddr;
231 		pdma_size = size;
232 
233 		/* Send Terminal Count pulse to floppy controller. */
234 		val = readb(auxio_register);
235 		val |= AUXIO_AUX1_FTCNT;
236 		writeb(val, auxio_register);
237 		val &= ~AUXIO_AUX1_FTCNT;
238 		writeb(val, auxio_register);
239 
240 		doing_pdma = 0;
241 	}
242 
243 main_interrupt:
244 	return floppy_interrupt(irq, dev_cookie);
245 }
246 
247 static int sun_fd_request_irq(void)
248 {
249 	static int once = 0;
250 	int error;
251 
252 	if(!once) {
253 		once = 1;
254 
255 		error = request_irq(FLOPPY_IRQ, sparc_floppy_irq,
256 				    0, "floppy", NULL);
257 
258 		return ((error == 0) ? 0 : -1);
259 	}
260 	return 0;
261 }
262 
263 static void sun_fd_free_irq(void)
264 {
265 }
266 
267 static unsigned int sun_get_dma_residue(void)
268 {
269 	/* XXX This isn't really correct. XXX */
270 	return 0;
271 }
272 
273 static int sun_fd_eject(int drive)
274 {
275 	set_dor(0x00, 0xff, 0x90);
276 	udelay(500);
277 	set_dor(0x00, 0x6f, 0x00);
278 	udelay(500);
279 	return 0;
280 }
281 
282 #include <asm/ebus_dma.h>
283 #include <asm/ns87303.h>
284 
285 static struct ebus_dma_info sun_pci_fd_ebus_dma;
286 static struct device *sun_floppy_dev;
287 static int sun_pci_broken_drive = -1;
288 
289 struct sun_pci_dma_op {
290 	unsigned int 	addr;
291 	int		len;
292 	int		direction;
293 	char		*buf;
294 };
295 static struct sun_pci_dma_op sun_pci_dma_current = { -1U, 0, 0, NULL};
296 static struct sun_pci_dma_op sun_pci_dma_pending = { -1U, 0, 0, NULL};
297 
298 irqreturn_t floppy_interrupt(int irq, void *dev_id);
299 
300 static unsigned char sun_pci_fd_inb(unsigned long base, unsigned int reg)
301 {
302 	udelay(5);
303 	return inb(base + reg);
304 }
305 
306 static void sun_pci_fd_outb(unsigned char val, unsigned long base,
307 			    unsigned int reg)
308 {
309 	udelay(5);
310 	outb(val, base + reg);
311 }
312 
313 static void sun_pci_fd_broken_outb(unsigned char val, unsigned long base,
314 				   unsigned int reg)
315 {
316 	udelay(5);
317 	/*
318 	 * XXX: Due to SUN's broken floppy connector on AX and AXi
319 	 *      we need to turn on MOTOR_0 also, if the floppy is
320 	 *      jumpered to DS1 (like most PC floppies are). I hope
321 	 *      this does not hurt correct hardware like the AXmp.
322 	 *      (Eddie, Sep 12 1998).
323 	 */
324 	if (reg == FD_DOR) {
325 		if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x20)) {
326 			val |= 0x10;
327 		}
328 	}
329 	outb(val, base + reg);
330 }
331 
332 #ifdef PCI_FDC_SWAP_DRIVES
333 static void sun_pci_fd_lde_broken_outb(unsigned char val, unsigned long base,
334 				       unsigned int reg)
335 {
336 	udelay(5);
337 	/*
338 	 * XXX: Due to SUN's broken floppy connector on AX and AXi
339 	 *      we need to turn on MOTOR_0 also, if the floppy is
340 	 *      jumpered to DS1 (like most PC floppies are). I hope
341 	 *      this does not hurt correct hardware like the AXmp.
342 	 *      (Eddie, Sep 12 1998).
343 	 */
344 	if (reg == FD_DOR) {
345 		if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x10)) {
346 			val &= ~(0x03);
347 			val |= 0x21;
348 		}
349 	}
350 	outb(val, base + reg);
351 }
352 #endif /* PCI_FDC_SWAP_DRIVES */
353 
354 static void sun_pci_fd_enable_dma(void)
355 {
356 	BUG_ON((NULL == sun_pci_dma_pending.buf) 	||
357 	    (0	  == sun_pci_dma_pending.len) 	||
358 	    (0	  == sun_pci_dma_pending.direction));
359 
360 	sun_pci_dma_current.buf = sun_pci_dma_pending.buf;
361 	sun_pci_dma_current.len = sun_pci_dma_pending.len;
362 	sun_pci_dma_current.direction = sun_pci_dma_pending.direction;
363 
364 	sun_pci_dma_pending.buf  = NULL;
365 	sun_pci_dma_pending.len  = 0;
366 	sun_pci_dma_pending.direction = 0;
367 	sun_pci_dma_pending.addr = -1U;
368 
369 	sun_pci_dma_current.addr =
370 		dma_map_single(sun_floppy_dev,
371 			       sun_pci_dma_current.buf,
372 			       sun_pci_dma_current.len,
373 			       sun_pci_dma_current.direction);
374 
375 	ebus_dma_enable(&sun_pci_fd_ebus_dma, 1);
376 
377 	if (ebus_dma_request(&sun_pci_fd_ebus_dma,
378 			     sun_pci_dma_current.addr,
379 			     sun_pci_dma_current.len))
380 		BUG();
381 }
382 
383 static void sun_pci_fd_disable_dma(void)
384 {
385 	ebus_dma_enable(&sun_pci_fd_ebus_dma, 0);
386 	if (sun_pci_dma_current.addr != -1U)
387 		dma_unmap_single(sun_floppy_dev,
388 				 sun_pci_dma_current.addr,
389 				 sun_pci_dma_current.len,
390 				 sun_pci_dma_current.direction);
391 	sun_pci_dma_current.addr = -1U;
392 }
393 
394 static void sun_pci_fd_set_dma_mode(int mode)
395 {
396 	if (mode == DMA_MODE_WRITE)
397 		sun_pci_dma_pending.direction = DMA_TO_DEVICE;
398 	else
399 		sun_pci_dma_pending.direction = DMA_FROM_DEVICE;
400 
401 	ebus_dma_prepare(&sun_pci_fd_ebus_dma, mode != DMA_MODE_WRITE);
402 }
403 
404 static void sun_pci_fd_set_dma_count(int length)
405 {
406 	sun_pci_dma_pending.len = length;
407 }
408 
409 static void sun_pci_fd_set_dma_addr(char *buffer)
410 {
411 	sun_pci_dma_pending.buf = buffer;
412 }
413 
414 static unsigned int sun_pci_get_dma_residue(void)
415 {
416 	return ebus_dma_residue(&sun_pci_fd_ebus_dma);
417 }
418 
419 static int sun_pci_fd_request_irq(void)
420 {
421 	return ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 1);
422 }
423 
424 static void sun_pci_fd_free_irq(void)
425 {
426 	ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 0);
427 }
428 
429 static int sun_pci_fd_eject(int drive)
430 {
431 	return -EINVAL;
432 }
433 
434 static void sun_pci_fd_dma_callback(struct ebus_dma_info *p, int event,
435 				    void *cookie)
436 {
437 	floppy_interrupt(0, NULL);
438 }
439 
440 /*
441  * Floppy probing, we'd like to use /dev/fd0 for a single Floppy on PCI,
442  * even if this is configured using DS1, thus looks like /dev/fd1 with
443  * the cabling used in Ultras.
444  */
445 #define DOR	(port + 2)
446 #define MSR	(port + 4)
447 #define FIFO	(port + 5)
448 
449 static void sun_pci_fd_out_byte(unsigned long port, unsigned char val,
450 			        unsigned long reg)
451 {
452 	unsigned char status;
453 	int timeout = 1000;
454 
455 	while (!((status = inb(MSR)) & 0x80) && --timeout)
456 		udelay(100);
457 	outb(val, reg);
458 }
459 
460 static unsigned char sun_pci_fd_sensei(unsigned long port)
461 {
462 	unsigned char result[2] = { 0x70, 0x00 };
463 	unsigned char status;
464 	int i = 0;
465 
466 	sun_pci_fd_out_byte(port, 0x08, FIFO);
467 	do {
468 		int timeout = 1000;
469 
470 		while (!((status = inb(MSR)) & 0x80) && --timeout)
471 			udelay(100);
472 
473 		if (!timeout)
474 			break;
475 
476 		if ((status & 0xf0) == 0xd0)
477 			result[i++] = inb(FIFO);
478 		else
479 			break;
480 	} while (i < 2);
481 
482 	return result[0];
483 }
484 
485 static void sun_pci_fd_reset(unsigned long port)
486 {
487 	unsigned char mask = 0x00;
488 	unsigned char status;
489 	int timeout = 10000;
490 
491 	outb(0x80, MSR);
492 	do {
493 		status = sun_pci_fd_sensei(port);
494 		if ((status & 0xc0) == 0xc0)
495 			mask |= 1 << (status & 0x03);
496 		else
497 			udelay(100);
498 	} while ((mask != 0x0f) && --timeout);
499 }
500 
501 static int sun_pci_fd_test_drive(unsigned long port, int drive)
502 {
503 	unsigned char status, data;
504 	int timeout = 1000;
505 	int ready;
506 
507 	sun_pci_fd_reset(port);
508 
509 	data = (0x10 << drive) | 0x0c | drive;
510 	sun_pci_fd_out_byte(port, data, DOR);
511 
512 	sun_pci_fd_out_byte(port, 0x07, FIFO);
513 	sun_pci_fd_out_byte(port, drive & 0x03, FIFO);
514 
515 	do {
516 		udelay(100);
517 		status = sun_pci_fd_sensei(port);
518 	} while (((status & 0xc0) == 0x80) && --timeout);
519 
520 	if (!timeout)
521 		ready = 0;
522 	else
523 		ready = (status & 0x10) ? 0 : 1;
524 
525 	sun_pci_fd_reset(port);
526 	return ready;
527 }
528 #undef FIFO
529 #undef MSR
530 #undef DOR
531 
532 static int __init ebus_fdthree_p(struct device_node *dp)
533 {
534 	if (of_node_name_eq(dp, "fdthree"))
535 		return 1;
536 	if (of_node_name_eq(dp, "floppy")) {
537 		const char *compat;
538 
539 		compat = of_get_property(dp, "compatible", NULL);
540 		if (compat && !strcmp(compat, "fdthree"))
541 			return 1;
542 	}
543 	return 0;
544 }
545 
546 static unsigned long __init sun_floppy_init(void)
547 {
548 	static int initialized = 0;
549 	struct device_node *dp;
550 	struct platform_device *op;
551 	const char *prop;
552 	char state[128];
553 
554 	if (initialized)
555 		return sun_floppy_types[0];
556 	initialized = 1;
557 
558 	op = NULL;
559 
560 	for_each_node_by_name(dp, "SUNW,fdtwo") {
561 		if (!of_node_name_eq(dp->parent, "sbus"))
562 			continue;
563 		op = of_find_device_by_node(dp);
564 		if (op)
565 			break;
566 	}
567 	if (op) {
568 		floppy_op = op;
569 		FLOPPY_IRQ = op->archdata.irqs[0];
570 	} else {
571 		struct device_node *ebus_dp;
572 		void __iomem *auxio_reg;
573 		const char *state_prop;
574 		unsigned long config;
575 
576 		dp = NULL;
577 		for_each_node_by_name(ebus_dp, "ebus") {
578 			for (dp = ebus_dp->child; dp; dp = dp->sibling) {
579 				if (ebus_fdthree_p(dp))
580 					goto found_fdthree;
581 			}
582 		}
583 	found_fdthree:
584 		if (!dp)
585 			return 0;
586 
587 		op = of_find_device_by_node(dp);
588 		if (!op)
589 			return 0;
590 
591 		state_prop = of_get_property(op->dev.of_node, "status", NULL);
592 		if (state_prop && !strncmp(state_prop, "disabled", 8))
593 			return 0;
594 
595 		FLOPPY_IRQ = op->archdata.irqs[0];
596 
597 		/* Make sure the high density bit is set, some systems
598 		 * (most notably Ultra5/Ultra10) come up with it clear.
599 		 */
600 		auxio_reg = (void __iomem *) op->resource[2].start;
601 		writel(readl(auxio_reg)|0x2, auxio_reg);
602 
603 		sun_floppy_dev = &op->dev;
604 
605 		spin_lock_init(&sun_pci_fd_ebus_dma.lock);
606 
607 		/* XXX ioremap */
608 		sun_pci_fd_ebus_dma.regs = (void __iomem *)
609 			op->resource[1].start;
610 		if (!sun_pci_fd_ebus_dma.regs)
611 			return 0;
612 
613 		sun_pci_fd_ebus_dma.flags = (EBUS_DMA_FLAG_USE_EBDMA_HANDLER |
614 					     EBUS_DMA_FLAG_TCI_DISABLE);
615 		sun_pci_fd_ebus_dma.callback = sun_pci_fd_dma_callback;
616 		sun_pci_fd_ebus_dma.client_cookie = NULL;
617 		sun_pci_fd_ebus_dma.irq = FLOPPY_IRQ;
618 		strcpy(sun_pci_fd_ebus_dma.name, "floppy");
619 		if (ebus_dma_register(&sun_pci_fd_ebus_dma))
620 			return 0;
621 
622 		/* XXX ioremap */
623 		sun_fdc = (struct sun_flpy_controller *) op->resource[0].start;
624 
625 		sun_fdops.fd_inb = sun_pci_fd_inb;
626 		sun_fdops.fd_outb = sun_pci_fd_outb;
627 
628 		can_use_virtual_dma = use_virtual_dma = 0;
629 		sun_fdops.fd_enable_dma = sun_pci_fd_enable_dma;
630 		sun_fdops.fd_disable_dma = sun_pci_fd_disable_dma;
631 		sun_fdops.fd_set_dma_mode = sun_pci_fd_set_dma_mode;
632 		sun_fdops.fd_set_dma_addr = sun_pci_fd_set_dma_addr;
633 		sun_fdops.fd_set_dma_count = sun_pci_fd_set_dma_count;
634 		sun_fdops.get_dma_residue = sun_pci_get_dma_residue;
635 
636 		sun_fdops.fd_request_irq = sun_pci_fd_request_irq;
637 		sun_fdops.fd_free_irq = sun_pci_fd_free_irq;
638 
639 		sun_fdops.fd_eject = sun_pci_fd_eject;
640 
641 		fdc_status = (unsigned long) &sun_fdc->status_82077;
642 
643 		/*
644 		 * XXX: Find out on which machines this is really needed.
645 		 */
646 		if (1) {
647 			sun_pci_broken_drive = 1;
648 			sun_fdops.fd_outb = sun_pci_fd_broken_outb;
649 		}
650 
651 		allowed_drive_mask = 0;
652 		if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 0))
653 			sun_floppy_types[0] = 4;
654 		if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 1))
655 			sun_floppy_types[1] = 4;
656 
657 		/*
658 		 * Find NS87303 SuperIO config registers (through ecpp).
659 		 */
660 		config = 0;
661 		for (dp = ebus_dp->child; dp; dp = dp->sibling) {
662 			if (of_node_name_eq(dp, "ecpp")) {
663 				struct platform_device *ecpp_op;
664 
665 				ecpp_op = of_find_device_by_node(dp);
666 				if (ecpp_op)
667 					config = ecpp_op->resource[1].start;
668 				goto config_done;
669 			}
670 		}
671 	config_done:
672 
673 		/*
674 		 * Sanity check, is this really the NS87303?
675 		 */
676 		switch (config & 0x3ff) {
677 		case 0x02e:
678 		case 0x15c:
679 		case 0x26e:
680 		case 0x398:
681 			break;
682 		default:
683 			config = 0;
684 		}
685 
686 		if (!config)
687 			return sun_floppy_types[0];
688 
689 		/* Enable PC-AT mode. */
690 		ns87303_modify(config, ASC, 0, 0xc0);
691 
692 #ifdef PCI_FDC_SWAP_DRIVES
693 		/*
694 		 * If only Floppy 1 is present, swap drives.
695 		 */
696 		if (!sun_floppy_types[0] && sun_floppy_types[1]) {
697 			/*
698 			 * Set the drive exchange bit in FCR on NS87303,
699 			 * make sure other bits are sane before doing so.
700 			 */
701 			ns87303_modify(config, FER, FER_EDM, 0);
702 			ns87303_modify(config, ASC, ASC_DRV2_SEL, 0);
703 			ns87303_modify(config, FCR, 0, FCR_LDE);
704 
705 			swap(sun_floppy_types[0], sun_floppy_types[1]);
706 
707 			if (sun_pci_broken_drive != -1) {
708 				sun_pci_broken_drive = 1 - sun_pci_broken_drive;
709 				sun_fdops.fd_outb = sun_pci_fd_lde_broken_outb;
710 			}
711 		}
712 #endif /* PCI_FDC_SWAP_DRIVES */
713 
714 		return sun_floppy_types[0];
715 	}
716 	prop = of_get_property(op->dev.of_node, "status", NULL);
717 	if (prop && !strncmp(state, "disabled", 8))
718 		return 0;
719 
720 	/*
721 	 * We cannot do of_ioremap here: it does request_region,
722 	 * which the generic floppy driver tries to do once again.
723 	 * But we must use the sdev resource values as they have
724 	 * had parent ranges applied.
725 	 */
726 	sun_fdc = (struct sun_flpy_controller *)
727 		(op->resource[0].start +
728 		 ((op->resource[0].flags & 0x1ffUL) << 32UL));
729 
730 	/* Last minute sanity check... */
731 	if (sbus_readb(&sun_fdc->status1_82077) == 0xff) {
732 		sun_fdc = (struct sun_flpy_controller *)-1;
733 		return 0;
734 	}
735 
736         sun_fdops.fd_inb = sun_82077_fd_inb;
737         sun_fdops.fd_outb = sun_82077_fd_outb;
738 
739 	can_use_virtual_dma = use_virtual_dma = 1;
740 	sun_fdops.fd_enable_dma = sun_fd_enable_dma;
741 	sun_fdops.fd_disable_dma = sun_fd_disable_dma;
742 	sun_fdops.fd_set_dma_mode = sun_fd_set_dma_mode;
743 	sun_fdops.fd_set_dma_addr = sun_fd_set_dma_addr;
744 	sun_fdops.fd_set_dma_count = sun_fd_set_dma_count;
745 	sun_fdops.get_dma_residue = sun_get_dma_residue;
746 
747 	sun_fdops.fd_request_irq = sun_fd_request_irq;
748 	sun_fdops.fd_free_irq = sun_fd_free_irq;
749 
750 	sun_fdops.fd_eject = sun_fd_eject;
751 
752         fdc_status = (unsigned long) &sun_fdc->status_82077;
753 
754 	/* Success... */
755 	allowed_drive_mask = 0x01;
756 	sun_floppy_types[0] = 4;
757 	sun_floppy_types[1] = 0;
758 
759 	return sun_floppy_types[0];
760 }
761 
762 #define EXTRA_FLOPPY_PARAMS
763 
764 static DEFINE_SPINLOCK(dma_spin_lock);
765 
766 #define claim_dma_lock() \
767 ({	unsigned long flags; \
768 	spin_lock_irqsave(&dma_spin_lock, flags); \
769 	flags; \
770 })
771 
772 #define release_dma_lock(__flags) \
773 	spin_unlock_irqrestore(&dma_spin_lock, __flags);
774 
775 #endif /* !(__ASM_SPARC64_FLOPPY_H) */
776