xref: /linux/arch/sparc/include/asm/floppy_64.h (revision ead751507de86d90fa250431e9990a8b881f713c)
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_device.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);
51 	void		(*fd_outb) (unsigned char value, unsigned long port);
52 	void		(*fd_enable_dma) (void);
53 	void		(*fd_disable_dma) (void);
54 	void		(*fd_set_dma_mode) (int);
55 	void		(*fd_set_dma_addr) (char *);
56 	void		(*fd_set_dma_count) (int);
57 	unsigned int	(*get_dma_residue) (void);
58 	int		(*fd_request_irq) (void);
59 	void		(*fd_free_irq) (void);
60 	int		(*fd_eject) (int);
61 };
62 
63 static struct sun_floppy_ops sun_fdops;
64 
65 #define fd_inb(port)              sun_fdops.fd_inb(port)
66 #define fd_outb(value,port)       sun_fdops.fd_outb(value,port)
67 #define fd_enable_dma()           sun_fdops.fd_enable_dma()
68 #define fd_disable_dma()          sun_fdops.fd_disable_dma()
69 #define fd_request_dma()          (0) /* nothing... */
70 #define fd_free_dma()             /* nothing... */
71 #define fd_clear_dma_ff()         /* nothing... */
72 #define fd_set_dma_mode(mode)     sun_fdops.fd_set_dma_mode(mode)
73 #define fd_set_dma_addr(addr)     sun_fdops.fd_set_dma_addr(addr)
74 #define fd_set_dma_count(count)   sun_fdops.fd_set_dma_count(count)
75 #define get_dma_residue(x)        sun_fdops.get_dma_residue()
76 #define fd_cacheflush(addr, size) /* nothing... */
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 /* No 64k boundary crossing problems on the Sparc. */
99 #define CROSS_64KB(a,s) (0)
100 
101 static unsigned char sun_82077_fd_inb(unsigned long port)
102 {
103 	udelay(5);
104 	switch(port & 7) {
105 	default:
106 		printk("floppy: Asked to read unknown port %lx\n", port);
107 		panic("floppy: Port bolixed.");
108 	case 4: /* FD_STATUS */
109 		return sbus_readb(&sun_fdc->status_82077) & ~STATUS_DMA;
110 	case 5: /* FD_DATA */
111 		return sbus_readb(&sun_fdc->data_82077);
112 	case 7: /* FD_DIR */
113 		/* XXX: Is DCL on 0x80 in sun4m? */
114 		return sbus_readb(&sun_fdc->dir_82077);
115 	}
116 	panic("sun_82072_fd_inb: How did I get here?");
117 }
118 
119 static void sun_82077_fd_outb(unsigned char value, unsigned long port)
120 {
121 	udelay(5);
122 	switch(port & 7) {
123 	default:
124 		printk("floppy: Asked to write to unknown port %lx\n", port);
125 		panic("floppy: Port bolixed.");
126 	case 2: /* FD_DOR */
127 		/* Happily, the 82077 has a real DOR register. */
128 		sbus_writeb(value, &sun_fdc->dor_82077);
129 		break;
130 	case 5: /* FD_DATA */
131 		sbus_writeb(value, &sun_fdc->data_82077);
132 		break;
133 	case 7: /* FD_DCR */
134 		sbus_writeb(value, &sun_fdc->dcr_82077);
135 		break;
136 	case 4: /* FD_STATUS */
137 		sbus_writeb(value, &sun_fdc->status_82077);
138 		break;
139 	}
140 	return;
141 }
142 
143 /* For pseudo-dma (Sun floppy drives have no real DMA available to
144  * them so we must eat the data fifo bytes directly ourselves) we have
145  * three state variables.  doing_pdma tells our inline low-level
146  * assembly floppy interrupt entry point whether it should sit and eat
147  * bytes from the fifo or just transfer control up to the higher level
148  * floppy interrupt c-code.  I tried very hard but I could not get the
149  * pseudo-dma to work in c-code without getting many overruns and
150  * underruns.  If non-zero, doing_pdma encodes the direction of
151  * the transfer for debugging.  1=read 2=write
152  */
153 unsigned char *pdma_vaddr;
154 unsigned long pdma_size;
155 volatile int doing_pdma = 0;
156 
157 /* This is software state */
158 char *pdma_base = NULL;
159 unsigned long pdma_areasize;
160 
161 /* Common routines to all controller types on the Sparc. */
162 static void sun_fd_disable_dma(void)
163 {
164 	doing_pdma = 0;
165 	pdma_base = NULL;
166 }
167 
168 static void sun_fd_set_dma_mode(int mode)
169 {
170 	switch(mode) {
171 	case DMA_MODE_READ:
172 		doing_pdma = 1;
173 		break;
174 	case DMA_MODE_WRITE:
175 		doing_pdma = 2;
176 		break;
177 	default:
178 		printk("Unknown dma mode %d\n", mode);
179 		panic("floppy: Giving up...");
180 	}
181 }
182 
183 static void sun_fd_set_dma_addr(char *buffer)
184 {
185 	pdma_vaddr = buffer;
186 }
187 
188 static void sun_fd_set_dma_count(int length)
189 {
190 	pdma_size = length;
191 }
192 
193 static void sun_fd_enable_dma(void)
194 {
195 	pdma_base = pdma_vaddr;
196 	pdma_areasize = pdma_size;
197 }
198 
199 irqreturn_t sparc_floppy_irq(int irq, void *dev_cookie)
200 {
201 	if (likely(doing_pdma)) {
202 		void __iomem *stat = (void __iomem *) fdc_status;
203 		unsigned char *vaddr = pdma_vaddr;
204 		unsigned long size = pdma_size;
205 		u8 val;
206 
207 		while (size) {
208 			val = readb(stat);
209 			if (unlikely(!(val & 0x80))) {
210 				pdma_vaddr = vaddr;
211 				pdma_size = size;
212 				return IRQ_HANDLED;
213 			}
214 			if (unlikely(!(val & 0x20))) {
215 				pdma_vaddr = vaddr;
216 				pdma_size = size;
217 				doing_pdma = 0;
218 				goto main_interrupt;
219 			}
220 			if (val & 0x40) {
221 				/* read */
222 				*vaddr++ = readb(stat + 1);
223 			} else {
224 				unsigned char data = *vaddr++;
225 
226 				/* write */
227 				writeb(data, stat + 1);
228 			}
229 			size--;
230 		}
231 
232 		pdma_vaddr = vaddr;
233 		pdma_size = size;
234 
235 		/* Send Terminal Count pulse to floppy controller. */
236 		val = readb(auxio_register);
237 		val |= AUXIO_AUX1_FTCNT;
238 		writeb(val, auxio_register);
239 		val &= ~AUXIO_AUX1_FTCNT;
240 		writeb(val, auxio_register);
241 
242 		doing_pdma = 0;
243 	}
244 
245 main_interrupt:
246 	return floppy_interrupt(irq, dev_cookie);
247 }
248 
249 static int sun_fd_request_irq(void)
250 {
251 	static int once = 0;
252 	int error;
253 
254 	if(!once) {
255 		once = 1;
256 
257 		error = request_irq(FLOPPY_IRQ, sparc_floppy_irq,
258 				    0, "floppy", NULL);
259 
260 		return ((error == 0) ? 0 : -1);
261 	}
262 	return 0;
263 }
264 
265 static void sun_fd_free_irq(void)
266 {
267 }
268 
269 static unsigned int sun_get_dma_residue(void)
270 {
271 	/* XXX This isn't really correct. XXX */
272 	return 0;
273 }
274 
275 static int sun_fd_eject(int drive)
276 {
277 	set_dor(0x00, 0xff, 0x90);
278 	udelay(500);
279 	set_dor(0x00, 0x6f, 0x00);
280 	udelay(500);
281 	return 0;
282 }
283 
284 #include <asm/ebus_dma.h>
285 #include <asm/ns87303.h>
286 
287 static struct ebus_dma_info sun_pci_fd_ebus_dma;
288 static struct device *sun_floppy_dev;
289 static int sun_pci_broken_drive = -1;
290 
291 struct sun_pci_dma_op {
292 	unsigned int 	addr;
293 	int		len;
294 	int		direction;
295 	char		*buf;
296 };
297 static struct sun_pci_dma_op sun_pci_dma_current = { -1U, 0, 0, NULL};
298 static struct sun_pci_dma_op sun_pci_dma_pending = { -1U, 0, 0, NULL};
299 
300 irqreturn_t floppy_interrupt(int irq, void *dev_id);
301 
302 static unsigned char sun_pci_fd_inb(unsigned long port)
303 {
304 	udelay(5);
305 	return inb(port);
306 }
307 
308 static void sun_pci_fd_outb(unsigned char val, unsigned long port)
309 {
310 	udelay(5);
311 	outb(val, port);
312 }
313 
314 static void sun_pci_fd_broken_outb(unsigned char val, unsigned long port)
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 (port == ((unsigned long)sun_fdc) + 2) {
325 		if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x20)) {
326 			val |= 0x10;
327 		}
328 	}
329 	outb(val, port);
330 }
331 
332 #ifdef PCI_FDC_SWAP_DRIVES
333 static void sun_pci_fd_lde_broken_outb(unsigned char val, unsigned long port)
334 {
335 	udelay(5);
336 	/*
337 	 * XXX: Due to SUN's broken floppy connector on AX and AXi
338 	 *      we need to turn on MOTOR_0 also, if the floppy is
339 	 *      jumpered to DS1 (like most PC floppies are). I hope
340 	 *      this does not hurt correct hardware like the AXmp.
341 	 *      (Eddie, Sep 12 1998).
342 	 */
343 	if (port == ((unsigned long)sun_fdc) + 2) {
344 		if (((val & 0x03) == sun_pci_broken_drive) && (val & 0x10)) {
345 			val &= ~(0x03);
346 			val |= 0x21;
347 		}
348 	}
349 	outb(val, port);
350 }
351 #endif /* PCI_FDC_SWAP_DRIVES */
352 
353 static void sun_pci_fd_enable_dma(void)
354 {
355 	BUG_ON((NULL == sun_pci_dma_pending.buf) 	||
356 	    (0	  == sun_pci_dma_pending.len) 	||
357 	    (0	  == sun_pci_dma_pending.direction));
358 
359 	sun_pci_dma_current.buf = sun_pci_dma_pending.buf;
360 	sun_pci_dma_current.len = sun_pci_dma_pending.len;
361 	sun_pci_dma_current.direction = sun_pci_dma_pending.direction;
362 
363 	sun_pci_dma_pending.buf  = NULL;
364 	sun_pci_dma_pending.len  = 0;
365 	sun_pci_dma_pending.direction = 0;
366 	sun_pci_dma_pending.addr = -1U;
367 
368 	sun_pci_dma_current.addr =
369 		dma_map_single(sun_floppy_dev,
370 			       sun_pci_dma_current.buf,
371 			       sun_pci_dma_current.len,
372 			       sun_pci_dma_current.direction);
373 
374 	ebus_dma_enable(&sun_pci_fd_ebus_dma, 1);
375 
376 	if (ebus_dma_request(&sun_pci_fd_ebus_dma,
377 			     sun_pci_dma_current.addr,
378 			     sun_pci_dma_current.len))
379 		BUG();
380 }
381 
382 static void sun_pci_fd_disable_dma(void)
383 {
384 	ebus_dma_enable(&sun_pci_fd_ebus_dma, 0);
385 	if (sun_pci_dma_current.addr != -1U)
386 		dma_unmap_single(sun_floppy_dev,
387 				 sun_pci_dma_current.addr,
388 				 sun_pci_dma_current.len,
389 				 sun_pci_dma_current.direction);
390 	sun_pci_dma_current.addr = -1U;
391 }
392 
393 static void sun_pci_fd_set_dma_mode(int mode)
394 {
395 	if (mode == DMA_MODE_WRITE)
396 		sun_pci_dma_pending.direction = DMA_TO_DEVICE;
397 	else
398 		sun_pci_dma_pending.direction = DMA_FROM_DEVICE;
399 
400 	ebus_dma_prepare(&sun_pci_fd_ebus_dma, mode != DMA_MODE_WRITE);
401 }
402 
403 static void sun_pci_fd_set_dma_count(int length)
404 {
405 	sun_pci_dma_pending.len = length;
406 }
407 
408 static void sun_pci_fd_set_dma_addr(char *buffer)
409 {
410 	sun_pci_dma_pending.buf = buffer;
411 }
412 
413 static unsigned int sun_pci_get_dma_residue(void)
414 {
415 	return ebus_dma_residue(&sun_pci_fd_ebus_dma);
416 }
417 
418 static int sun_pci_fd_request_irq(void)
419 {
420 	return ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 1);
421 }
422 
423 static void sun_pci_fd_free_irq(void)
424 {
425 	ebus_dma_irq_enable(&sun_pci_fd_ebus_dma, 0);
426 }
427 
428 static int sun_pci_fd_eject(int drive)
429 {
430 	return -EINVAL;
431 }
432 
433 void sun_pci_fd_dma_callback(struct ebus_dma_info *p, int event, void *cookie)
434 {
435 	floppy_interrupt(0, NULL);
436 }
437 
438 /*
439  * Floppy probing, we'd like to use /dev/fd0 for a single Floppy on PCI,
440  * even if this is configured using DS1, thus looks like /dev/fd1 with
441  * the cabling used in Ultras.
442  */
443 #define DOR	(port + 2)
444 #define MSR	(port + 4)
445 #define FIFO	(port + 5)
446 
447 static void sun_pci_fd_out_byte(unsigned long port, unsigned char val,
448 			        unsigned long reg)
449 {
450 	unsigned char status;
451 	int timeout = 1000;
452 
453 	while (!((status = inb(MSR)) & 0x80) && --timeout)
454 		udelay(100);
455 	outb(val, reg);
456 }
457 
458 static unsigned char sun_pci_fd_sensei(unsigned long port)
459 {
460 	unsigned char result[2] = { 0x70, 0x00 };
461 	unsigned char status;
462 	int i = 0;
463 
464 	sun_pci_fd_out_byte(port, 0x08, FIFO);
465 	do {
466 		int timeout = 1000;
467 
468 		while (!((status = inb(MSR)) & 0x80) && --timeout)
469 			udelay(100);
470 
471 		if (!timeout)
472 			break;
473 
474 		if ((status & 0xf0) == 0xd0)
475 			result[i++] = inb(FIFO);
476 		else
477 			break;
478 	} while (i < 2);
479 
480 	return result[0];
481 }
482 
483 static void sun_pci_fd_reset(unsigned long port)
484 {
485 	unsigned char mask = 0x00;
486 	unsigned char status;
487 	int timeout = 10000;
488 
489 	outb(0x80, MSR);
490 	do {
491 		status = sun_pci_fd_sensei(port);
492 		if ((status & 0xc0) == 0xc0)
493 			mask |= 1 << (status & 0x03);
494 		else
495 			udelay(100);
496 	} while ((mask != 0x0f) && --timeout);
497 }
498 
499 static int sun_pci_fd_test_drive(unsigned long port, int drive)
500 {
501 	unsigned char status, data;
502 	int timeout = 1000;
503 	int ready;
504 
505 	sun_pci_fd_reset(port);
506 
507 	data = (0x10 << drive) | 0x0c | drive;
508 	sun_pci_fd_out_byte(port, data, DOR);
509 
510 	sun_pci_fd_out_byte(port, 0x07, FIFO);
511 	sun_pci_fd_out_byte(port, drive & 0x03, FIFO);
512 
513 	do {
514 		udelay(100);
515 		status = sun_pci_fd_sensei(port);
516 	} while (((status & 0xc0) == 0x80) && --timeout);
517 
518 	if (!timeout)
519 		ready = 0;
520 	else
521 		ready = (status & 0x10) ? 0 : 1;
522 
523 	sun_pci_fd_reset(port);
524 	return ready;
525 }
526 #undef FIFO
527 #undef MSR
528 #undef DOR
529 
530 static int __init ebus_fdthree_p(struct device_node *dp)
531 {
532 	if (!strcmp(dp->name, "fdthree"))
533 		return 1;
534 	if (!strcmp(dp->name, "floppy")) {
535 		const char *compat;
536 
537 		compat = of_get_property(dp, "compatible", NULL);
538 		if (compat && !strcmp(compat, "fdthree"))
539 			return 1;
540 	}
541 	return 0;
542 }
543 
544 static unsigned long __init sun_floppy_init(void)
545 {
546 	static int initialized = 0;
547 	struct device_node *dp;
548 	struct platform_device *op;
549 	const char *prop;
550 	char state[128];
551 
552 	if (initialized)
553 		return sun_floppy_types[0];
554 	initialized = 1;
555 
556 	op = NULL;
557 
558 	for_each_node_by_name(dp, "SUNW,fdtwo") {
559 		if (strcmp(dp->parent->name, "sbus"))
560 			continue;
561 		op = of_find_device_by_node(dp);
562 		if (op)
563 			break;
564 	}
565 	if (op) {
566 		floppy_op = op;
567 		FLOPPY_IRQ = op->archdata.irqs[0];
568 	} else {
569 		struct device_node *ebus_dp;
570 		void __iomem *auxio_reg;
571 		const char *state_prop;
572 		unsigned long config;
573 
574 		dp = NULL;
575 		for_each_node_by_name(ebus_dp, "ebus") {
576 			for (dp = ebus_dp->child; dp; dp = dp->sibling) {
577 				if (ebus_fdthree_p(dp))
578 					goto found_fdthree;
579 			}
580 		}
581 	found_fdthree:
582 		if (!dp)
583 			return 0;
584 
585 		op = of_find_device_by_node(dp);
586 		if (!op)
587 			return 0;
588 
589 		state_prop = of_get_property(op->dev.of_node, "status", NULL);
590 		if (state_prop && !strncmp(state_prop, "disabled", 8))
591 			return 0;
592 
593 		FLOPPY_IRQ = op->archdata.irqs[0];
594 
595 		/* Make sure the high density bit is set, some systems
596 		 * (most notably Ultra5/Ultra10) come up with it clear.
597 		 */
598 		auxio_reg = (void __iomem *) op->resource[2].start;
599 		writel(readl(auxio_reg)|0x2, auxio_reg);
600 
601 		sun_floppy_dev = &op->dev;
602 
603 		spin_lock_init(&sun_pci_fd_ebus_dma.lock);
604 
605 		/* XXX ioremap */
606 		sun_pci_fd_ebus_dma.regs = (void __iomem *)
607 			op->resource[1].start;
608 		if (!sun_pci_fd_ebus_dma.regs)
609 			return 0;
610 
611 		sun_pci_fd_ebus_dma.flags = (EBUS_DMA_FLAG_USE_EBDMA_HANDLER |
612 					     EBUS_DMA_FLAG_TCI_DISABLE);
613 		sun_pci_fd_ebus_dma.callback = sun_pci_fd_dma_callback;
614 		sun_pci_fd_ebus_dma.client_cookie = NULL;
615 		sun_pci_fd_ebus_dma.irq = FLOPPY_IRQ;
616 		strcpy(sun_pci_fd_ebus_dma.name, "floppy");
617 		if (ebus_dma_register(&sun_pci_fd_ebus_dma))
618 			return 0;
619 
620 		/* XXX ioremap */
621 		sun_fdc = (struct sun_flpy_controller *) op->resource[0].start;
622 
623 		sun_fdops.fd_inb = sun_pci_fd_inb;
624 		sun_fdops.fd_outb = sun_pci_fd_outb;
625 
626 		can_use_virtual_dma = use_virtual_dma = 0;
627 		sun_fdops.fd_enable_dma = sun_pci_fd_enable_dma;
628 		sun_fdops.fd_disable_dma = sun_pci_fd_disable_dma;
629 		sun_fdops.fd_set_dma_mode = sun_pci_fd_set_dma_mode;
630 		sun_fdops.fd_set_dma_addr = sun_pci_fd_set_dma_addr;
631 		sun_fdops.fd_set_dma_count = sun_pci_fd_set_dma_count;
632 		sun_fdops.get_dma_residue = sun_pci_get_dma_residue;
633 
634 		sun_fdops.fd_request_irq = sun_pci_fd_request_irq;
635 		sun_fdops.fd_free_irq = sun_pci_fd_free_irq;
636 
637 		sun_fdops.fd_eject = sun_pci_fd_eject;
638 
639 		fdc_status = (unsigned long) &sun_fdc->status_82077;
640 
641 		/*
642 		 * XXX: Find out on which machines this is really needed.
643 		 */
644 		if (1) {
645 			sun_pci_broken_drive = 1;
646 			sun_fdops.fd_outb = sun_pci_fd_broken_outb;
647 		}
648 
649 		allowed_drive_mask = 0;
650 		if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 0))
651 			sun_floppy_types[0] = 4;
652 		if (sun_pci_fd_test_drive((unsigned long)sun_fdc, 1))
653 			sun_floppy_types[1] = 4;
654 
655 		/*
656 		 * Find NS87303 SuperIO config registers (through ecpp).
657 		 */
658 		config = 0;
659 		for (dp = ebus_dp->child; dp; dp = dp->sibling) {
660 			if (!strcmp(dp->name, "ecpp")) {
661 				struct platform_device *ecpp_op;
662 
663 				ecpp_op = of_find_device_by_node(dp);
664 				if (ecpp_op)
665 					config = ecpp_op->resource[1].start;
666 				goto config_done;
667 			}
668 		}
669 	config_done:
670 
671 		/*
672 		 * Sanity check, is this really the NS87303?
673 		 */
674 		switch (config & 0x3ff) {
675 		case 0x02e:
676 		case 0x15c:
677 		case 0x26e:
678 		case 0x398:
679 			break;
680 		default:
681 			config = 0;
682 		}
683 
684 		if (!config)
685 			return sun_floppy_types[0];
686 
687 		/* Enable PC-AT mode. */
688 		ns87303_modify(config, ASC, 0, 0xc0);
689 
690 #ifdef PCI_FDC_SWAP_DRIVES
691 		/*
692 		 * If only Floppy 1 is present, swap drives.
693 		 */
694 		if (!sun_floppy_types[0] && sun_floppy_types[1]) {
695 			/*
696 			 * Set the drive exchange bit in FCR on NS87303,
697 			 * make sure other bits are sane before doing so.
698 			 */
699 			ns87303_modify(config, FER, FER_EDM, 0);
700 			ns87303_modify(config, ASC, ASC_DRV2_SEL, 0);
701 			ns87303_modify(config, FCR, 0, FCR_LDE);
702 
703 			config = sun_floppy_types[0];
704 			sun_floppy_types[0] = sun_floppy_types[1];
705 			sun_floppy_types[1] = config;
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