xref: /openbmc/linux/drivers/block/swim3.c (revision 174cd4b1e5fbd0d74c68cf3a74f5bd4923485512)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Driver for the SWIM3 (Super Woz Integrated Machine 3)
31da177e4SLinus Torvalds  * floppy controller found on Power Macintoshes.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1996 Paul Mackerras.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
91da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
101da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds /*
141da177e4SLinus Torvalds  * TODO:
151da177e4SLinus Torvalds  * handle 2 drives
161da177e4SLinus Torvalds  * handle GCR disks
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
19b3025457SBenjamin Herrenschmidt #undef DEBUG
20b3025457SBenjamin Herrenschmidt 
211da177e4SLinus Torvalds #include <linux/stddef.h>
221da177e4SLinus Torvalds #include <linux/kernel.h>
23*174cd4b1SIngo Molnar #include <linux/sched/signal.h>
241da177e4SLinus Torvalds #include <linux/timer.h>
251da177e4SLinus Torvalds #include <linux/delay.h>
261da177e4SLinus Torvalds #include <linux/fd.h>
271da177e4SLinus Torvalds #include <linux/ioctl.h>
281da177e4SLinus Torvalds #include <linux/blkdev.h>
291da177e4SLinus Torvalds #include <linux/interrupt.h>
302a48fc0aSArnd Bergmann #include <linux/mutex.h>
311da177e4SLinus Torvalds #include <linux/module.h>
32515729ecSPaul Mackerras #include <linux/spinlock.h>
33106fd892SArnd Bergmann #include <linux/wait.h>
341da177e4SLinus Torvalds #include <asm/io.h>
351da177e4SLinus Torvalds #include <asm/dbdma.h>
361da177e4SLinus Torvalds #include <asm/prom.h>
377c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
381da177e4SLinus Torvalds #include <asm/mediabay.h>
391da177e4SLinus Torvalds #include <asm/machdep.h>
401da177e4SLinus Torvalds #include <asm/pmac_feature.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #define MAX_FLOPPIES	2
431da177e4SLinus Torvalds 
44b3025457SBenjamin Herrenschmidt static DEFINE_MUTEX(swim3_mutex);
45b3025457SBenjamin Herrenschmidt static struct gendisk *disks[MAX_FLOPPIES];
46b3025457SBenjamin Herrenschmidt 
471da177e4SLinus Torvalds enum swim_state {
481da177e4SLinus Torvalds 	idle,
491da177e4SLinus Torvalds 	locating,
501da177e4SLinus Torvalds 	seeking,
511da177e4SLinus Torvalds 	settling,
521da177e4SLinus Torvalds 	do_transfer,
531da177e4SLinus Torvalds 	jogging,
541da177e4SLinus Torvalds 	available,
551da177e4SLinus Torvalds 	revalidating,
561da177e4SLinus Torvalds 	ejecting
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #define REG(x)	unsigned char x; char x ## _pad[15];
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /*
621da177e4SLinus Torvalds  * The names for these registers mostly represent speculation on my part.
631da177e4SLinus Torvalds  * It will be interesting to see how close they are to the names Apple uses.
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds struct swim3 {
661da177e4SLinus Torvalds 	REG(data);
671da177e4SLinus Torvalds 	REG(timer);		/* counts down at 1MHz */
681da177e4SLinus Torvalds 	REG(error);
691da177e4SLinus Torvalds 	REG(mode);
701da177e4SLinus Torvalds 	REG(select);		/* controls CA0, CA1, CA2 and LSTRB signals */
711da177e4SLinus Torvalds 	REG(setup);
721da177e4SLinus Torvalds 	REG(control);		/* writing bits clears them */
731da177e4SLinus Torvalds 	REG(status);		/* writing bits sets them in control */
741da177e4SLinus Torvalds 	REG(intr);
751da177e4SLinus Torvalds 	REG(nseek);		/* # tracks to seek */
761da177e4SLinus Torvalds 	REG(ctrack);		/* current track number */
771da177e4SLinus Torvalds 	REG(csect);		/* current sector number */
781da177e4SLinus Torvalds 	REG(gap3);		/* size of gap 3 in track format */
791da177e4SLinus Torvalds 	REG(sector);		/* sector # to read or write */
801da177e4SLinus Torvalds 	REG(nsect);		/* # sectors to read or write */
811da177e4SLinus Torvalds 	REG(intr_enable);
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #define control_bic	control
851da177e4SLinus Torvalds #define control_bis	status
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* Bits in select register */
881da177e4SLinus Torvalds #define CA_MASK		7
891da177e4SLinus Torvalds #define LSTRB		8
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* Bits in control register */
921da177e4SLinus Torvalds #define DO_SEEK		0x80
931da177e4SLinus Torvalds #define FORMAT		0x40
941da177e4SLinus Torvalds #define SELECT		0x20
951da177e4SLinus Torvalds #define WRITE_SECTORS	0x10
961da177e4SLinus Torvalds #define DO_ACTION	0x08
971da177e4SLinus Torvalds #define DRIVE2_ENABLE	0x04
981da177e4SLinus Torvalds #define DRIVE_ENABLE	0x02
991da177e4SLinus Torvalds #define INTR_ENABLE	0x01
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /* Bits in status register */
1021da177e4SLinus Torvalds #define FIFO_1BYTE	0x80
1031da177e4SLinus Torvalds #define FIFO_2BYTE	0x40
1041da177e4SLinus Torvalds #define ERROR		0x20
1051da177e4SLinus Torvalds #define DATA		0x08
1061da177e4SLinus Torvalds #define RDDATA		0x04
1071da177e4SLinus Torvalds #define INTR_PENDING	0x02
1081da177e4SLinus Torvalds #define MARK_BYTE	0x01
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* Bits in intr and intr_enable registers */
1111da177e4SLinus Torvalds #define ERROR_INTR	0x20
1121da177e4SLinus Torvalds #define DATA_CHANGED	0x10
1131da177e4SLinus Torvalds #define TRANSFER_DONE	0x08
1141da177e4SLinus Torvalds #define SEEN_SECTOR	0x04
1151da177e4SLinus Torvalds #define SEEK_DONE	0x02
1161da177e4SLinus Torvalds #define TIMER_DONE	0x01
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds /* Bits in error register */
1191da177e4SLinus Torvalds #define ERR_DATA_CRC	0x80
1201da177e4SLinus Torvalds #define ERR_ADDR_CRC	0x40
1211da177e4SLinus Torvalds #define ERR_OVERRUN	0x04
1221da177e4SLinus Torvalds #define ERR_UNDERRUN	0x01
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /* Bits in setup register */
1251da177e4SLinus Torvalds #define S_SW_RESET	0x80
1261da177e4SLinus Torvalds #define S_GCR_WRITE	0x40
1271da177e4SLinus Torvalds #define S_IBM_DRIVE	0x20
1281da177e4SLinus Torvalds #define S_TEST_MODE	0x10
1291da177e4SLinus Torvalds #define S_FCLK_DIV2	0x08
1301da177e4SLinus Torvalds #define S_GCR		0x04
1311da177e4SLinus Torvalds #define S_COPY_PROT	0x02
1321da177e4SLinus Torvalds #define S_INV_WDATA	0x01
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /* Select values for swim3_action */
1351da177e4SLinus Torvalds #define SEEK_POSITIVE	0
1361da177e4SLinus Torvalds #define SEEK_NEGATIVE	4
1371da177e4SLinus Torvalds #define STEP		1
1381da177e4SLinus Torvalds #define MOTOR_ON	2
1391da177e4SLinus Torvalds #define MOTOR_OFF	6
1401da177e4SLinus Torvalds #define INDEX		3
1411da177e4SLinus Torvalds #define EJECT		7
1421da177e4SLinus Torvalds #define SETMFM		9
1431da177e4SLinus Torvalds #define SETGCR		13
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /* Select values for swim3_select and swim3_readbit */
1461da177e4SLinus Torvalds #define STEP_DIR	0
1471da177e4SLinus Torvalds #define STEPPING	1
1481da177e4SLinus Torvalds #define MOTOR_ON	2
1491da177e4SLinus Torvalds #define RELAX		3	/* also eject in progress */
1501da177e4SLinus Torvalds #define READ_DATA_0	4
1511da177e4SLinus Torvalds #define TWOMEG_DRIVE	5
1521da177e4SLinus Torvalds #define SINGLE_SIDED	6	/* drive or diskette is 4MB type? */
1531da177e4SLinus Torvalds #define DRIVE_PRESENT	7
1541da177e4SLinus Torvalds #define DISK_IN		8
1551da177e4SLinus Torvalds #define WRITE_PROT	9
1561da177e4SLinus Torvalds #define TRACK_ZERO	10
1571da177e4SLinus Torvalds #define TACHO		11
1581da177e4SLinus Torvalds #define READ_DATA_1	12
1591da177e4SLinus Torvalds #define MFM_MODE	13
1601da177e4SLinus Torvalds #define SEEK_COMPLETE	14
1611da177e4SLinus Torvalds #define ONEMEG_MEDIA	15
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds /* Definitions of values used in writing and formatting */
1641da177e4SLinus Torvalds #define DATA_ESCAPE	0x99
1651da177e4SLinus Torvalds #define GCR_SYNC_EXC	0x3f
1661da177e4SLinus Torvalds #define GCR_SYNC_CONV	0x80
1671da177e4SLinus Torvalds #define GCR_FIRST_MARK	0xd5
1681da177e4SLinus Torvalds #define GCR_SECOND_MARK	0xaa
1691da177e4SLinus Torvalds #define GCR_ADDR_MARK	"\xd5\xaa\x00"
1701da177e4SLinus Torvalds #define GCR_DATA_MARK	"\xd5\xaa\x0b"
1711da177e4SLinus Torvalds #define GCR_SLIP_BYTE	"\x27\xaa"
1721da177e4SLinus Torvalds #define GCR_SELF_SYNC	"\x3f\xbf\x1e\x34\x3c\x3f"
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds #define DATA_99		"\x99\x99"
1751da177e4SLinus Torvalds #define MFM_ADDR_MARK	"\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
1761da177e4SLinus Torvalds #define MFM_INDEX_MARK	"\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
1771da177e4SLinus Torvalds #define MFM_GAP_LEN	12
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds struct floppy_state {
1801da177e4SLinus Torvalds 	enum swim_state	state;
1811da177e4SLinus Torvalds 	struct swim3 __iomem *swim3;	/* hardware registers */
1821da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dma;	/* DMA controller registers */
1831da177e4SLinus Torvalds 	int	swim3_intr;	/* interrupt number for SWIM3 */
1841da177e4SLinus Torvalds 	int	dma_intr;	/* interrupt number for DMA channel */
1851da177e4SLinus Torvalds 	int	cur_cyl;	/* cylinder head is on, or -1 */
1861da177e4SLinus Torvalds 	int	cur_sector;	/* last sector we saw go past */
1871da177e4SLinus Torvalds 	int	req_cyl;	/* the cylinder for the current r/w request */
1881da177e4SLinus Torvalds 	int	head;		/* head number ditto */
1891da177e4SLinus Torvalds 	int	req_sector;	/* sector number ditto */
1901da177e4SLinus Torvalds 	int	scount;		/* # sectors we're transferring at present */
1911da177e4SLinus Torvalds 	int	retries;
1921da177e4SLinus Torvalds 	int	settle_time;
1931da177e4SLinus Torvalds 	int	secpercyl;	/* disk geometry information */
1941da177e4SLinus Torvalds 	int	secpertrack;
1951da177e4SLinus Torvalds 	int	total_secs;
1961da177e4SLinus Torvalds 	int	write_prot;	/* 1 if write-protected, 0 if not, -1 dunno */
1971da177e4SLinus Torvalds 	struct dbdma_cmd *dma_cmd;
1981da177e4SLinus Torvalds 	int	ref_count;
1991da177e4SLinus Torvalds 	int	expect_cyl;
2001da177e4SLinus Torvalds 	struct timer_list timeout;
2011da177e4SLinus Torvalds 	int	timeout_pending;
2021da177e4SLinus Torvalds 	int	ejected;
2031da177e4SLinus Torvalds 	wait_queue_head_t wait;
2041da177e4SLinus Torvalds 	int	wanted;
205d58b0c39SBenjamin Herrenschmidt 	struct macio_dev *mdev;
2061da177e4SLinus Torvalds 	char	dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
207b3025457SBenjamin Herrenschmidt 	int	index;
208b3025457SBenjamin Herrenschmidt 	struct request *cur_req;
2091da177e4SLinus Torvalds };
2101da177e4SLinus Torvalds 
211b3025457SBenjamin Herrenschmidt #define swim3_err(fmt, arg...)	dev_err(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
212b3025457SBenjamin Herrenschmidt #define swim3_warn(fmt, arg...)	dev_warn(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
213b3025457SBenjamin Herrenschmidt #define swim3_info(fmt, arg...)	dev_info(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
214b3025457SBenjamin Herrenschmidt 
215b3025457SBenjamin Herrenschmidt #ifdef DEBUG
216b3025457SBenjamin Herrenschmidt #define swim3_dbg(fmt, arg...)	dev_dbg(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
217b3025457SBenjamin Herrenschmidt #else
218b3025457SBenjamin Herrenschmidt #define swim3_dbg(fmt, arg...)	do { } while(0)
219b3025457SBenjamin Herrenschmidt #endif
220b3025457SBenjamin Herrenschmidt 
2211da177e4SLinus Torvalds static struct floppy_state floppy_states[MAX_FLOPPIES];
2221da177e4SLinus Torvalds static int floppy_count = 0;
2231da177e4SLinus Torvalds static DEFINE_SPINLOCK(swim3_lock);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static unsigned short write_preamble[] = {
2261da177e4SLinus Torvalds 	0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e,	/* gap field */
2271da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0,			/* sync field */
2281da177e4SLinus Torvalds 	0x99a1, 0x99a1, 0x99a1, 0x99fb,		/* data address mark */
2291da177e4SLinus Torvalds 	0x990f					/* no escape for 512 bytes */
2301da177e4SLinus Torvalds };
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static unsigned short write_postamble[] = {
2331da177e4SLinus Torvalds 	0x9904,					/* insert CRC */
2341da177e4SLinus Torvalds 	0x4e4e, 0x4e4e,
2351da177e4SLinus Torvalds 	0x9908,					/* stop writing */
2361da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0
2371da177e4SLinus Torvalds };
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static void seek_track(struct floppy_state *fs, int n);
2401da177e4SLinus Torvalds static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
2411da177e4SLinus Torvalds static void act(struct floppy_state *fs);
2421da177e4SLinus Torvalds static void scan_timeout(unsigned long data);
2431da177e4SLinus Torvalds static void seek_timeout(unsigned long data);
2441da177e4SLinus Torvalds static void settle_timeout(unsigned long data);
2451da177e4SLinus Torvalds static void xfer_timeout(unsigned long data);
2467d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id);
2477d12e780SDavid Howells /*static void fd_dma_interrupt(int irq, void *dev_id);*/
2481da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state,
2491da177e4SLinus Torvalds 		      int interruptible);
2501da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs);
2511da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs);
252b4d9a442SAl Viro static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
2531da177e4SLinus Torvalds 			unsigned int cmd, unsigned long param);
254b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode);
255db2a144bSAl Viro static void floppy_release(struct gendisk *disk, fmode_t mode);
2564bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk,
2574bbde777STejun Heo 					unsigned int clearing);
2581da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk);
2591da177e4SLinus Torvalds 
260b3025457SBenjamin Herrenschmidt static bool swim3_end_request(struct floppy_state *fs, int err, unsigned int nr_bytes)
261f4bd4b90STejun Heo {
262b3025457SBenjamin Herrenschmidt 	struct request *req = fs->cur_req;
263b3025457SBenjamin Herrenschmidt 	int rc;
264b3025457SBenjamin Herrenschmidt 
265b3025457SBenjamin Herrenschmidt 	swim3_dbg("  end request, err=%d nr_bytes=%d, cur_req=%p\n",
266b3025457SBenjamin Herrenschmidt 		  err, nr_bytes, req);
267b3025457SBenjamin Herrenschmidt 
268b3025457SBenjamin Herrenschmidt 	if (err)
269b3025457SBenjamin Herrenschmidt 		nr_bytes = blk_rq_cur_bytes(req);
270b3025457SBenjamin Herrenschmidt 	rc = __blk_end_request(req, err, nr_bytes);
271b3025457SBenjamin Herrenschmidt 	if (rc)
272f4bd4b90STejun Heo 		return true;
273b3025457SBenjamin Herrenschmidt 	fs->cur_req = NULL;
274f4bd4b90STejun Heo 	return false;
275f4bd4b90STejun Heo }
276f4bd4b90STejun Heo 
2771da177e4SLinus Torvalds static void swim3_select(struct floppy_state *fs, int sel)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
2821da177e4SLinus Torvalds 	if (sel & 8)
2831da177e4SLinus Torvalds 		out_8(&sw->control_bis, SELECT);
2841da177e4SLinus Torvalds 	else
2851da177e4SLinus Torvalds 		out_8(&sw->control_bic, SELECT);
2861da177e4SLinus Torvalds 	out_8(&sw->select, sel & CA_MASK);
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds static void swim3_action(struct floppy_state *fs, int action)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	swim3_select(fs, action);
2941da177e4SLinus Torvalds 	udelay(1);
2951da177e4SLinus Torvalds 	out_8(&sw->select, sw->select | LSTRB);
2961da177e4SLinus Torvalds 	udelay(2);
2971da177e4SLinus Torvalds 	out_8(&sw->select, sw->select & ~LSTRB);
2981da177e4SLinus Torvalds 	udelay(1);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds static int swim3_readbit(struct floppy_state *fs, int bit)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
3041da177e4SLinus Torvalds 	int stat;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	swim3_select(fs, bit);
3071da177e4SLinus Torvalds 	udelay(1);
3081da177e4SLinus Torvalds 	stat = in_8(&sw->status);
3091da177e4SLinus Torvalds 	return (stat & DATA) == 0;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static void start_request(struct floppy_state *fs)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	struct request *req;
3151da177e4SLinus Torvalds 	unsigned long x;
3161da177e4SLinus Torvalds 
317b3025457SBenjamin Herrenschmidt 	swim3_dbg("start request, initial state=%d\n", fs->state);
318b3025457SBenjamin Herrenschmidt 
3191da177e4SLinus Torvalds 	if (fs->state == idle && fs->wanted) {
3201da177e4SLinus Torvalds 		fs->state = available;
3211da177e4SLinus Torvalds 		wake_up(&fs->wait);
3221da177e4SLinus Torvalds 		return;
3231da177e4SLinus Torvalds 	}
324f4bd4b90STejun Heo 	while (fs->state == idle) {
325b3025457SBenjamin Herrenschmidt 		swim3_dbg("start request, idle loop, cur_req=%p\n", fs->cur_req);
326b3025457SBenjamin Herrenschmidt 		if (!fs->cur_req) {
327b3025457SBenjamin Herrenschmidt 			fs->cur_req = blk_fetch_request(disks[fs->index]->queue);
328b3025457SBenjamin Herrenschmidt 			swim3_dbg("  fetched request %p\n", fs->cur_req);
329b3025457SBenjamin Herrenschmidt 			if (!fs->cur_req)
330f4bd4b90STejun Heo 				break;
331f4bd4b90STejun Heo 		}
332b3025457SBenjamin Herrenschmidt 		req = fs->cur_req;
333b3025457SBenjamin Herrenschmidt 
334b3025457SBenjamin Herrenschmidt 		if (fs->mdev->media_bay &&
335b3025457SBenjamin Herrenschmidt 		    check_media_bay(fs->mdev->media_bay) != MB_FD) {
336b3025457SBenjamin Herrenschmidt 			swim3_dbg("%s", "  media bay absent, dropping req\n");
337b3025457SBenjamin Herrenschmidt 			swim3_end_request(fs, -ENODEV, 0);
338b3025457SBenjamin Herrenschmidt 			continue;
339b3025457SBenjamin Herrenschmidt 		}
340b3025457SBenjamin Herrenschmidt 
341b3025457SBenjamin Herrenschmidt #if 0 /* This is really too verbose */
342b3025457SBenjamin Herrenschmidt 		swim3_dbg("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%u buf=%p\n",
3431da177e4SLinus Torvalds 			  req->rq_disk->disk_name, req->cmd,
344b3025457SBenjamin Herrenschmidt 			  (long)blk_rq_pos(req), blk_rq_sectors(req),
345b4f42e28SJens Axboe 			  bio_data(req->bio));
346b3025457SBenjamin Herrenschmidt 		swim3_dbg("           errors=%d current_nr_sectors=%u\n",
34783096ebfSTejun Heo 			  req->errors, blk_rq_cur_sectors(req));
3481da177e4SLinus Torvalds #endif
3491da177e4SLinus Torvalds 
35083096ebfSTejun Heo 		if (blk_rq_pos(req) >= fs->total_secs) {
351b3025457SBenjamin Herrenschmidt 			swim3_dbg("  pos out of bounds (%ld, max is %ld)\n",
352b3025457SBenjamin Herrenschmidt 				  (long)blk_rq_pos(req), (long)fs->total_secs);
353b3025457SBenjamin Herrenschmidt 			swim3_end_request(fs, -EIO, 0);
3541da177e4SLinus Torvalds 			continue;
3551da177e4SLinus Torvalds 		}
3561da177e4SLinus Torvalds 		if (fs->ejected) {
357b3025457SBenjamin Herrenschmidt 			swim3_dbg("%s", "  disk ejected\n");
358b3025457SBenjamin Herrenschmidt 			swim3_end_request(fs, -EIO, 0);
3591da177e4SLinus Torvalds 			continue;
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		if (rq_data_dir(req) == WRITE) {
3631da177e4SLinus Torvalds 			if (fs->write_prot < 0)
3641da177e4SLinus Torvalds 				fs->write_prot = swim3_readbit(fs, WRITE_PROT);
3651da177e4SLinus Torvalds 			if (fs->write_prot) {
366b3025457SBenjamin Herrenschmidt 				swim3_dbg("%s", "  try to write, disk write protected\n");
367b3025457SBenjamin Herrenschmidt 				swim3_end_request(fs, -EIO, 0);
3681da177e4SLinus Torvalds 				continue;
3691da177e4SLinus Torvalds 			}
3701da177e4SLinus Torvalds 		}
3711da177e4SLinus Torvalds 
37283096ebfSTejun Heo 		/* Do not remove the cast. blk_rq_pos(req) is now a
37383096ebfSTejun Heo 		 * sector_t and can be 64 bits, but it will never go
37483096ebfSTejun Heo 		 * past 32 bits for this driver anyway, so we can
37583096ebfSTejun Heo 		 * safely cast it down and not have to do a 64/32
37683096ebfSTejun Heo 		 * division
3771da177e4SLinus Torvalds 		 */
37883096ebfSTejun Heo 		fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl;
37983096ebfSTejun Heo 		x = ((long)blk_rq_pos(req)) % fs->secpercyl;
3801da177e4SLinus Torvalds 		fs->head = x / fs->secpertrack;
3811da177e4SLinus Torvalds 		fs->req_sector = x % fs->secpertrack + 1;
3821da177e4SLinus Torvalds 		fs->state = do_transfer;
3831da177e4SLinus Torvalds 		fs->retries = 0;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 		act(fs);
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
389b3025457SBenjamin Herrenschmidt static void do_fd_request(struct request_queue * q)
390b3025457SBenjamin Herrenschmidt {
391b3025457SBenjamin Herrenschmidt 	start_request(q->queuedata);
392b3025457SBenjamin Herrenschmidt }
393b3025457SBenjamin Herrenschmidt 
3941da177e4SLinus Torvalds static void set_timeout(struct floppy_state *fs, int nticks,
3951da177e4SLinus Torvalds 			void (*proc)(unsigned long))
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	if (fs->timeout_pending)
3981da177e4SLinus Torvalds 		del_timer(&fs->timeout);
3991da177e4SLinus Torvalds 	fs->timeout.expires = jiffies + nticks;
4001da177e4SLinus Torvalds 	fs->timeout.function = proc;
4011da177e4SLinus Torvalds 	fs->timeout.data = (unsigned long) fs;
4021da177e4SLinus Torvalds 	add_timer(&fs->timeout);
4031da177e4SLinus Torvalds 	fs->timeout_pending = 1;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds static inline void scan_track(struct floppy_state *fs)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	swim3_select(fs, READ_DATA_0);
4111da177e4SLinus Torvalds 	in_8(&sw->intr);		/* clear SEEN_SECTOR bit */
4121da177e4SLinus Torvalds 	in_8(&sw->error);
4131da177e4SLinus Torvalds 	out_8(&sw->intr_enable, SEEN_SECTOR);
4141da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_ACTION);
4151da177e4SLinus Torvalds 	/* enable intr when track found */
4161da177e4SLinus Torvalds 	set_timeout(fs, HZ, scan_timeout);	/* enable timeout */
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds static inline void seek_track(struct floppy_state *fs, int n)
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	if (n >= 0) {
4241da177e4SLinus Torvalds 		swim3_action(fs, SEEK_POSITIVE);
4251da177e4SLinus Torvalds 		sw->nseek = n;
4261da177e4SLinus Torvalds 	} else {
4271da177e4SLinus Torvalds 		swim3_action(fs, SEEK_NEGATIVE);
4281da177e4SLinus Torvalds 		sw->nseek = -n;
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 	fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1;
4311da177e4SLinus Torvalds 	swim3_select(fs, STEP);
4321da177e4SLinus Torvalds 	in_8(&sw->error);
4331da177e4SLinus Torvalds 	/* enable intr when seek finished */
4341da177e4SLinus Torvalds 	out_8(&sw->intr_enable, SEEK_DONE);
4351da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_SEEK);
4361da177e4SLinus Torvalds 	set_timeout(fs, 3*HZ, seek_timeout);	/* enable timeout */
4371da177e4SLinus Torvalds 	fs->settle_time = 0;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds static inline void init_dma(struct dbdma_cmd *cp, int cmd,
4411da177e4SLinus Torvalds 			    void *buf, int count)
4421da177e4SLinus Torvalds {
443f5718726SDavid Gibson 	cp->req_count = cpu_to_le16(count);
444f5718726SDavid Gibson 	cp->command = cpu_to_le16(cmd);
445f5718726SDavid Gibson 	cp->phy_addr = cpu_to_le32(virt_to_bus(buf));
4461da177e4SLinus Torvalds 	cp->xfer_status = 0;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds static inline void setup_transfer(struct floppy_state *fs)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	int n;
4521da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
4531da177e4SLinus Torvalds 	struct dbdma_cmd *cp = fs->dma_cmd;
4541da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr = fs->dma;
455b3025457SBenjamin Herrenschmidt 	struct request *req = fs->cur_req;
4561da177e4SLinus Torvalds 
457b3025457SBenjamin Herrenschmidt 	if (blk_rq_cur_sectors(req) <= 0) {
458b3025457SBenjamin Herrenschmidt 		swim3_warn("%s", "Transfer 0 sectors ?\n");
4591da177e4SLinus Torvalds 		return;
4601da177e4SLinus Torvalds 	}
461b3025457SBenjamin Herrenschmidt 	if (rq_data_dir(req) == WRITE)
4621da177e4SLinus Torvalds 		n = 1;
4631da177e4SLinus Torvalds 	else {
4641da177e4SLinus Torvalds 		n = fs->secpertrack - fs->req_sector + 1;
465b3025457SBenjamin Herrenschmidt 		if (n > blk_rq_cur_sectors(req))
466b3025457SBenjamin Herrenschmidt 			n = blk_rq_cur_sectors(req);
4671da177e4SLinus Torvalds 	}
468b3025457SBenjamin Herrenschmidt 
469b3025457SBenjamin Herrenschmidt 	swim3_dbg("  setup xfer at sect %d (of %d) head %d for %d\n",
470b3025457SBenjamin Herrenschmidt 		  fs->req_sector, fs->secpertrack, fs->head, n);
471b3025457SBenjamin Herrenschmidt 
4721da177e4SLinus Torvalds 	fs->scount = n;
4731da177e4SLinus Torvalds 	swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
4741da177e4SLinus Torvalds 	out_8(&sw->sector, fs->req_sector);
4751da177e4SLinus Torvalds 	out_8(&sw->nsect, n);
4761da177e4SLinus Torvalds 	out_8(&sw->gap3, 0);
4771da177e4SLinus Torvalds 	out_le32(&dr->cmdptr, virt_to_bus(cp));
478b3025457SBenjamin Herrenschmidt 	if (rq_data_dir(req) == WRITE) {
4791da177e4SLinus Torvalds 		/* Set up 3 dma commands: write preamble, data, postamble */
4801da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
4811da177e4SLinus Torvalds 		++cp;
482b4f42e28SJens Axboe 		init_dma(cp, OUTPUT_MORE, bio_data(req->bio), 512);
4831da177e4SLinus Torvalds 		++cp;
4841da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
4851da177e4SLinus Torvalds 	} else {
486b4f42e28SJens Axboe 		init_dma(cp, INPUT_LAST, bio_data(req->bio), n * 512);
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 	++cp;
4891da177e4SLinus Torvalds 	out_le16(&cp->command, DBDMA_STOP);
4901da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
4911da177e4SLinus Torvalds 	in_8(&sw->error);
4921da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
493b3025457SBenjamin Herrenschmidt 	if (rq_data_dir(req) == WRITE)
4941da177e4SLinus Torvalds 		out_8(&sw->control_bis, WRITE_SECTORS);
4951da177e4SLinus Torvalds 	in_8(&sw->intr);
4961da177e4SLinus Torvalds 	out_le32(&dr->control, (RUN << 16) | RUN);
4971da177e4SLinus Torvalds 	/* enable intr when transfer complete */
4981da177e4SLinus Torvalds 	out_8(&sw->intr_enable, TRANSFER_DONE);
4991da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_ACTION);
5001da177e4SLinus Torvalds 	set_timeout(fs, 2*HZ, xfer_timeout);	/* enable timeout */
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds static void act(struct floppy_state *fs)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	for (;;) {
506b3025457SBenjamin Herrenschmidt 		swim3_dbg("  act loop, state=%d, req_cyl=%d, cur_cyl=%d\n",
507b3025457SBenjamin Herrenschmidt 			  fs->state, fs->req_cyl, fs->cur_cyl);
508b3025457SBenjamin Herrenschmidt 
5091da177e4SLinus Torvalds 		switch (fs->state) {
5101da177e4SLinus Torvalds 		case idle:
5111da177e4SLinus Torvalds 			return;		/* XXX shouldn't get here */
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 		case locating:
5141da177e4SLinus Torvalds 			if (swim3_readbit(fs, TRACK_ZERO)) {
515b3025457SBenjamin Herrenschmidt 				swim3_dbg("%s", "    locate track 0\n");
5161da177e4SLinus Torvalds 				fs->cur_cyl = 0;
5171da177e4SLinus Torvalds 				if (fs->req_cyl == 0)
5181da177e4SLinus Torvalds 					fs->state = do_transfer;
5191da177e4SLinus Torvalds 				else
5201da177e4SLinus Torvalds 					fs->state = seeking;
5211da177e4SLinus Torvalds 				break;
5221da177e4SLinus Torvalds 			}
5231da177e4SLinus Torvalds 			scan_track(fs);
5241da177e4SLinus Torvalds 			return;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 		case seeking:
5271da177e4SLinus Torvalds 			if (fs->cur_cyl < 0) {
5281da177e4SLinus Torvalds 				fs->expect_cyl = -1;
5291da177e4SLinus Torvalds 				fs->state = locating;
5301da177e4SLinus Torvalds 				break;
5311da177e4SLinus Torvalds 			}
5321da177e4SLinus Torvalds 			if (fs->req_cyl == fs->cur_cyl) {
533b3025457SBenjamin Herrenschmidt 				swim3_warn("%s", "Whoops, seeking 0\n");
5341da177e4SLinus Torvalds 				fs->state = do_transfer;
5351da177e4SLinus Torvalds 				break;
5361da177e4SLinus Torvalds 			}
5371da177e4SLinus Torvalds 			seek_track(fs, fs->req_cyl - fs->cur_cyl);
5381da177e4SLinus Torvalds 			return;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 		case settling:
5411da177e4SLinus Torvalds 			/* check for SEEK_COMPLETE after 30ms */
5421da177e4SLinus Torvalds 			fs->settle_time = (HZ + 32) / 33;
5431da177e4SLinus Torvalds 			set_timeout(fs, fs->settle_time, settle_timeout);
5441da177e4SLinus Torvalds 			return;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 		case do_transfer:
5471da177e4SLinus Torvalds 			if (fs->cur_cyl != fs->req_cyl) {
5481da177e4SLinus Torvalds 				if (fs->retries > 5) {
549b3025457SBenjamin Herrenschmidt 					swim3_err("Wrong cylinder in transfer, want: %d got %d\n",
550b3025457SBenjamin Herrenschmidt 						  fs->req_cyl, fs->cur_cyl);
551b3025457SBenjamin Herrenschmidt 					swim3_end_request(fs, -EIO, 0);
5521da177e4SLinus Torvalds 					fs->state = idle;
5531da177e4SLinus Torvalds 					return;
5541da177e4SLinus Torvalds 				}
5551da177e4SLinus Torvalds 				fs->state = seeking;
5561da177e4SLinus Torvalds 				break;
5571da177e4SLinus Torvalds 			}
5581da177e4SLinus Torvalds 			setup_transfer(fs);
5591da177e4SLinus Torvalds 			return;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 		case jogging:
5621da177e4SLinus Torvalds 			seek_track(fs, -5);
5631da177e4SLinus Torvalds 			return;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 		default:
566b3025457SBenjamin Herrenschmidt 			swim3_err("Unknown state %d\n", fs->state);
5671da177e4SLinus Torvalds 			return;
5681da177e4SLinus Torvalds 		}
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds static void scan_timeout(unsigned long data)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
5751da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
576b3025457SBenjamin Herrenschmidt 	unsigned long flags;
5771da177e4SLinus Torvalds 
578b3025457SBenjamin Herrenschmidt 	swim3_dbg("* scan timeout, state=%d\n", fs->state);
579b3025457SBenjamin Herrenschmidt 
580b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
5811da177e4SLinus Torvalds 	fs->timeout_pending = 0;
5821da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
5831da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
5841da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
5851da177e4SLinus Torvalds 	fs->cur_cyl = -1;
5861da177e4SLinus Torvalds 	if (fs->retries > 5) {
587b3025457SBenjamin Herrenschmidt 		swim3_end_request(fs, -EIO, 0);
5881da177e4SLinus Torvalds 		fs->state = idle;
5891da177e4SLinus Torvalds 		start_request(fs);
5901da177e4SLinus Torvalds 	} else {
5911da177e4SLinus Torvalds 		fs->state = jogging;
5921da177e4SLinus Torvalds 		act(fs);
5931da177e4SLinus Torvalds 	}
594b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds static void seek_timeout(unsigned long data)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
6001da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
601b3025457SBenjamin Herrenschmidt 	unsigned long flags;
6021da177e4SLinus Torvalds 
603b3025457SBenjamin Herrenschmidt 	swim3_dbg("* seek timeout, state=%d\n", fs->state);
604b3025457SBenjamin Herrenschmidt 
605b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
6061da177e4SLinus Torvalds 	fs->timeout_pending = 0;
6071da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_SEEK);
6081da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
6091da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
610b3025457SBenjamin Herrenschmidt 	swim3_err("%s", "Seek timeout\n");
611b3025457SBenjamin Herrenschmidt 	swim3_end_request(fs, -EIO, 0);
6121da177e4SLinus Torvalds 	fs->state = idle;
6131da177e4SLinus Torvalds 	start_request(fs);
614b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds static void settle_timeout(unsigned long data)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
6201da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
621b3025457SBenjamin Herrenschmidt 	unsigned long flags;
6221da177e4SLinus Torvalds 
623b3025457SBenjamin Herrenschmidt 	swim3_dbg("* settle timeout, state=%d\n", fs->state);
624b3025457SBenjamin Herrenschmidt 
625b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
6261da177e4SLinus Torvalds 	fs->timeout_pending = 0;
6271da177e4SLinus Torvalds 	if (swim3_readbit(fs, SEEK_COMPLETE)) {
6281da177e4SLinus Torvalds 		out_8(&sw->select, RELAX);
6291da177e4SLinus Torvalds 		fs->state = locating;
6301da177e4SLinus Torvalds 		act(fs);
631b3025457SBenjamin Herrenschmidt 		goto unlock;
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
6341da177e4SLinus Torvalds 	if (fs->settle_time < 2*HZ) {
6351da177e4SLinus Torvalds 		++fs->settle_time;
6361da177e4SLinus Torvalds 		set_timeout(fs, 1, settle_timeout);
637b3025457SBenjamin Herrenschmidt 		goto unlock;
6381da177e4SLinus Torvalds 	}
639b3025457SBenjamin Herrenschmidt 	swim3_err("%s", "Seek settle timeout\n");
640b3025457SBenjamin Herrenschmidt 	swim3_end_request(fs, -EIO, 0);
6411da177e4SLinus Torvalds 	fs->state = idle;
6421da177e4SLinus Torvalds 	start_request(fs);
643b3025457SBenjamin Herrenschmidt  unlock:
644b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds static void xfer_timeout(unsigned long data)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
6501da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
6511da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr = fs->dma;
652b3025457SBenjamin Herrenschmidt 	unsigned long flags;
6531da177e4SLinus Torvalds 	int n;
6541da177e4SLinus Torvalds 
655b3025457SBenjamin Herrenschmidt 	swim3_dbg("* xfer timeout, state=%d\n", fs->state);
656b3025457SBenjamin Herrenschmidt 
657b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
6581da177e4SLinus Torvalds 	fs->timeout_pending = 0;
6591da177e4SLinus Torvalds 	out_le32(&dr->control, RUN << 16);
6601da177e4SLinus Torvalds 	/* We must wait a bit for dbdma to stop */
6611da177e4SLinus Torvalds 	for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
6621da177e4SLinus Torvalds 		udelay(1);
6631da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
6641da177e4SLinus Torvalds 	out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
6651da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
666b3025457SBenjamin Herrenschmidt 	swim3_err("Timeout %sing sector %ld\n",
667b3025457SBenjamin Herrenschmidt 	       (rq_data_dir(fs->cur_req)==WRITE? "writ": "read"),
668b3025457SBenjamin Herrenschmidt 	       (long)blk_rq_pos(fs->cur_req));
669b3025457SBenjamin Herrenschmidt 	swim3_end_request(fs, -EIO, 0);
6701da177e4SLinus Torvalds 	fs->state = idle;
6711da177e4SLinus Torvalds 	start_request(fs);
672b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6757d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) dev_id;
6781da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
6791da177e4SLinus Torvalds 	int intr, err, n;
6801da177e4SLinus Torvalds 	int stat, resid;
6811da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr;
6821da177e4SLinus Torvalds 	struct dbdma_cmd *cp;
683b3025457SBenjamin Herrenschmidt 	unsigned long flags;
684b3025457SBenjamin Herrenschmidt 	struct request *req = fs->cur_req;
6851da177e4SLinus Torvalds 
686b3025457SBenjamin Herrenschmidt 	swim3_dbg("* interrupt, state=%d\n", fs->state);
687b3025457SBenjamin Herrenschmidt 
688b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
6891da177e4SLinus Torvalds 	intr = in_8(&sw->intr);
6901da177e4SLinus Torvalds 	err = (intr & ERROR_INTR)? in_8(&sw->error): 0;
6911da177e4SLinus Torvalds 	if ((intr & ERROR_INTR) && fs->state != do_transfer)
692b3025457SBenjamin Herrenschmidt 		swim3_err("Non-transfer error interrupt: state=%d, dir=%x, intr=%x, err=%x\n",
693b3025457SBenjamin Herrenschmidt 			  fs->state, rq_data_dir(req), intr, err);
6941da177e4SLinus Torvalds 	switch (fs->state) {
6951da177e4SLinus Torvalds 	case locating:
6961da177e4SLinus Torvalds 		if (intr & SEEN_SECTOR) {
6971da177e4SLinus Torvalds 			out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
6981da177e4SLinus Torvalds 			out_8(&sw->select, RELAX);
6991da177e4SLinus Torvalds 			out_8(&sw->intr_enable, 0);
7001da177e4SLinus Torvalds 			del_timer(&fs->timeout);
7011da177e4SLinus Torvalds 			fs->timeout_pending = 0;
7021da177e4SLinus Torvalds 			if (sw->ctrack == 0xff) {
703b3025457SBenjamin Herrenschmidt 				swim3_err("%s", "Seen sector but cyl=ff?\n");
7041da177e4SLinus Torvalds 				fs->cur_cyl = -1;
7051da177e4SLinus Torvalds 				if (fs->retries > 5) {
706b3025457SBenjamin Herrenschmidt 					swim3_end_request(fs, -EIO, 0);
7071da177e4SLinus Torvalds 					fs->state = idle;
7081da177e4SLinus Torvalds 					start_request(fs);
7091da177e4SLinus Torvalds 				} else {
7101da177e4SLinus Torvalds 					fs->state = jogging;
7111da177e4SLinus Torvalds 					act(fs);
7121da177e4SLinus Torvalds 				}
7131da177e4SLinus Torvalds 				break;
7141da177e4SLinus Torvalds 			}
7151da177e4SLinus Torvalds 			fs->cur_cyl = sw->ctrack;
7161da177e4SLinus Torvalds 			fs->cur_sector = sw->csect;
7171da177e4SLinus Torvalds 			if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
718b3025457SBenjamin Herrenschmidt 				swim3_err("Expected cyl %d, got %d\n",
7191da177e4SLinus Torvalds 					  fs->expect_cyl, fs->cur_cyl);
7201da177e4SLinus Torvalds 			fs->state = do_transfer;
7211da177e4SLinus Torvalds 			act(fs);
7221da177e4SLinus Torvalds 		}
7231da177e4SLinus Torvalds 		break;
7241da177e4SLinus Torvalds 	case seeking:
7251da177e4SLinus Torvalds 	case jogging:
7261da177e4SLinus Torvalds 		if (sw->nseek == 0) {
7271da177e4SLinus Torvalds 			out_8(&sw->control_bic, DO_SEEK);
7281da177e4SLinus Torvalds 			out_8(&sw->select, RELAX);
7291da177e4SLinus Torvalds 			out_8(&sw->intr_enable, 0);
7301da177e4SLinus Torvalds 			del_timer(&fs->timeout);
7311da177e4SLinus Torvalds 			fs->timeout_pending = 0;
7321da177e4SLinus Torvalds 			if (fs->state == seeking)
7331da177e4SLinus Torvalds 				++fs->retries;
7341da177e4SLinus Torvalds 			fs->state = settling;
7351da177e4SLinus Torvalds 			act(fs);
7361da177e4SLinus Torvalds 		}
7371da177e4SLinus Torvalds 		break;
7381da177e4SLinus Torvalds 	case settling:
7391da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
7401da177e4SLinus Torvalds 		del_timer(&fs->timeout);
7411da177e4SLinus Torvalds 		fs->timeout_pending = 0;
7421da177e4SLinus Torvalds 		act(fs);
7431da177e4SLinus Torvalds 		break;
7441da177e4SLinus Torvalds 	case do_transfer:
7451da177e4SLinus Torvalds 		if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
7461da177e4SLinus Torvalds 			break;
7471da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
7481da177e4SLinus Torvalds 		out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
7491da177e4SLinus Torvalds 		out_8(&sw->select, RELAX);
7501da177e4SLinus Torvalds 		del_timer(&fs->timeout);
7511da177e4SLinus Torvalds 		fs->timeout_pending = 0;
7521da177e4SLinus Torvalds 		dr = fs->dma;
7531da177e4SLinus Torvalds 		cp = fs->dma_cmd;
754b3025457SBenjamin Herrenschmidt 		if (rq_data_dir(req) == WRITE)
7551da177e4SLinus Torvalds 			++cp;
7561da177e4SLinus Torvalds 		/*
7571da177e4SLinus Torvalds 		 * Check that the main data transfer has finished.
7581da177e4SLinus Torvalds 		 * On writing, the swim3 sometimes doesn't use
7591da177e4SLinus Torvalds 		 * up all the bytes of the postamble, so we can still
7601da177e4SLinus Torvalds 		 * see DMA active here.  That doesn't matter as long
7611da177e4SLinus Torvalds 		 * as all the sector data has been transferred.
7621da177e4SLinus Torvalds 		 */
7631da177e4SLinus Torvalds 		if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) {
7641da177e4SLinus Torvalds 			/* wait a little while for DMA to complete */
7651da177e4SLinus Torvalds 			for (n = 0; n < 100; ++n) {
7661da177e4SLinus Torvalds 				if (cp->xfer_status != 0)
7671da177e4SLinus Torvalds 					break;
7681da177e4SLinus Torvalds 				udelay(1);
7691da177e4SLinus Torvalds 				barrier();
7701da177e4SLinus Torvalds 			}
7711da177e4SLinus Torvalds 		}
7721da177e4SLinus Torvalds 		/* turn off DMA */
7731da177e4SLinus Torvalds 		out_le32(&dr->control, (RUN | PAUSE) << 16);
774f5718726SDavid Gibson 		stat = le16_to_cpu(cp->xfer_status);
775f5718726SDavid Gibson 		resid = le16_to_cpu(cp->res_count);
7761da177e4SLinus Torvalds 		if (intr & ERROR_INTR) {
7771da177e4SLinus Torvalds 			n = fs->scount - 1 - resid / 512;
7781da177e4SLinus Torvalds 			if (n > 0) {
779b3025457SBenjamin Herrenschmidt 				blk_update_request(req, 0, n << 9);
7801da177e4SLinus Torvalds 				fs->req_sector += n;
7811da177e4SLinus Torvalds 			}
7821da177e4SLinus Torvalds 			if (fs->retries < 5) {
7831da177e4SLinus Torvalds 				++fs->retries;
7841da177e4SLinus Torvalds 				act(fs);
7851da177e4SLinus Torvalds 			} else {
786b3025457SBenjamin Herrenschmidt 				swim3_err("Error %sing block %ld (err=%x)\n",
787b3025457SBenjamin Herrenschmidt 				       rq_data_dir(req) == WRITE? "writ": "read",
788b3025457SBenjamin Herrenschmidt 				       (long)blk_rq_pos(req), err);
789b3025457SBenjamin Herrenschmidt 				swim3_end_request(fs, -EIO, 0);
7901da177e4SLinus Torvalds 				fs->state = idle;
7911da177e4SLinus Torvalds 			}
7921da177e4SLinus Torvalds 		} else {
7931da177e4SLinus Torvalds 			if ((stat & ACTIVE) == 0 || resid != 0) {
7941da177e4SLinus Torvalds 				/* musta been an error */
795b3025457SBenjamin Herrenschmidt 				swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid);
796b3025457SBenjamin Herrenschmidt 				swim3_err("  state=%d, dir=%x, intr=%x, err=%x\n",
797b3025457SBenjamin Herrenschmidt 					  fs->state, rq_data_dir(req), intr, err);
798b3025457SBenjamin Herrenschmidt 				swim3_end_request(fs, -EIO, 0);
7991da177e4SLinus Torvalds 				fs->state = idle;
8001da177e4SLinus Torvalds 				start_request(fs);
8011da177e4SLinus Torvalds 				break;
8021da177e4SLinus Torvalds 			}
803b3025457SBenjamin Herrenschmidt 			fs->retries = 0;
804b3025457SBenjamin Herrenschmidt 			if (swim3_end_request(fs, 0, fs->scount << 9)) {
8051da177e4SLinus Torvalds 				fs->req_sector += fs->scount;
8061da177e4SLinus Torvalds 				if (fs->req_sector > fs->secpertrack) {
8071da177e4SLinus Torvalds 					fs->req_sector -= fs->secpertrack;
8081da177e4SLinus Torvalds 					if (++fs->head > 1) {
8091da177e4SLinus Torvalds 						fs->head = 0;
8101da177e4SLinus Torvalds 						++fs->req_cyl;
8111da177e4SLinus Torvalds 					}
8121da177e4SLinus Torvalds 				}
8131da177e4SLinus Torvalds 				act(fs);
814467ca759STejun Heo 			} else
815467ca759STejun Heo 				fs->state = idle;
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 		if (fs->state == idle)
8181da177e4SLinus Torvalds 			start_request(fs);
8191da177e4SLinus Torvalds 		break;
8201da177e4SLinus Torvalds 	default:
821b3025457SBenjamin Herrenschmidt 		swim3_err("Don't know what to do in state %d\n", fs->state);
8221da177e4SLinus Torvalds 	}
823b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
8241da177e4SLinus Torvalds 	return IRQ_HANDLED;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds /*
8287d12e780SDavid Howells static void fd_dma_interrupt(int irq, void *dev_id)
8291da177e4SLinus Torvalds {
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds */
8321da177e4SLinus Torvalds 
833b3025457SBenjamin Herrenschmidt /* Called under the mutex to grab exclusive access to a drive */
8341da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state,
8351da177e4SLinus Torvalds 		      int interruptible)
8361da177e4SLinus Torvalds {
8371da177e4SLinus Torvalds 	unsigned long flags;
8381da177e4SLinus Torvalds 
839b3025457SBenjamin Herrenschmidt 	swim3_dbg("%s", "-> grab drive\n");
840b3025457SBenjamin Herrenschmidt 
841b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
842b3025457SBenjamin Herrenschmidt 	if (fs->state != idle && fs->state != available) {
8431da177e4SLinus Torvalds 		++fs->wanted;
844106fd892SArnd Bergmann 		/* this will enable irqs in order to sleep */
845106fd892SArnd Bergmann 		if (!interruptible)
846106fd892SArnd Bergmann 			wait_event_lock_irq(fs->wait,
847106fd892SArnd Bergmann                                         fs->state == available,
848106fd892SArnd Bergmann                                         swim3_lock);
849106fd892SArnd Bergmann 		else if (wait_event_interruptible_lock_irq(fs->wait,
850106fd892SArnd Bergmann 					fs->state == available,
851106fd892SArnd Bergmann 					swim3_lock)) {
8521da177e4SLinus Torvalds 			--fs->wanted;
853106fd892SArnd Bergmann 			spin_unlock_irqrestore(&swim3_lock, flags);
8541da177e4SLinus Torvalds 			return -EINTR;
8551da177e4SLinus Torvalds 		}
8561da177e4SLinus Torvalds 		--fs->wanted;
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 	fs->state = state;
859b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
860b3025457SBenjamin Herrenschmidt 
8611da177e4SLinus Torvalds 	return 0;
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs)
8651da177e4SLinus Torvalds {
8661da177e4SLinus Torvalds 	unsigned long flags;
8671da177e4SLinus Torvalds 
868b3025457SBenjamin Herrenschmidt 	swim3_dbg("%s", "-> release drive\n");
869b3025457SBenjamin Herrenschmidt 
870b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
8711da177e4SLinus Torvalds 	fs->state = idle;
8721da177e4SLinus Torvalds 	start_request(fs);
873b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs)
8771da177e4SLinus Torvalds {
8781da177e4SLinus Torvalds 	int err, n;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	err = grab_drive(fs, ejecting, 1);
8811da177e4SLinus Torvalds 	if (err)
8821da177e4SLinus Torvalds 		return err;
8831da177e4SLinus Torvalds 	swim3_action(fs, EJECT);
8841da177e4SLinus Torvalds 	for (n = 20; n > 0; --n) {
8851da177e4SLinus Torvalds 		if (signal_pending(current)) {
8861da177e4SLinus Torvalds 			err = -EINTR;
8871da177e4SLinus Torvalds 			break;
8881da177e4SLinus Torvalds 		}
8891da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
89086e84862SNishanth Aravamudan 		schedule_timeout_interruptible(1);
8911da177e4SLinus Torvalds 		if (swim3_readbit(fs, DISK_IN) == 0)
8921da177e4SLinus Torvalds 			break;
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 	swim3_select(fs, RELAX);
8951da177e4SLinus Torvalds 	udelay(150);
8961da177e4SLinus Torvalds 	fs->ejected = 1;
8971da177e4SLinus Torvalds 	release_drive(fs);
8981da177e4SLinus Torvalds 	return err;
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds static struct floppy_struct floppy_type =
9021da177e4SLinus Torvalds 	{ 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL };	/*  7 1.44MB 3.5"   */
9031da177e4SLinus Torvalds 
9048a6cfeb6SArnd Bergmann static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode,
9051da177e4SLinus Torvalds 			unsigned int cmd, unsigned long param)
9061da177e4SLinus Torvalds {
907b4d9a442SAl Viro 	struct floppy_state *fs = bdev->bd_disk->private_data;
9081da177e4SLinus Torvalds 	int err;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
9111da177e4SLinus Torvalds 		return -EPERM;
9121da177e4SLinus Torvalds 
913d58b0c39SBenjamin Herrenschmidt 	if (fs->mdev->media_bay &&
914d58b0c39SBenjamin Herrenschmidt 	    check_media_bay(fs->mdev->media_bay) != MB_FD)
9151da177e4SLinus Torvalds 		return -ENXIO;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	switch (cmd) {
9181da177e4SLinus Torvalds 	case FDEJECT:
9191da177e4SLinus Torvalds 		if (fs->ref_count != 1)
9201da177e4SLinus Torvalds 			return -EBUSY;
9211da177e4SLinus Torvalds 		err = fd_eject(fs);
9221da177e4SLinus Torvalds 		return err;
9231da177e4SLinus Torvalds 	case FDGETPRM:
9241da177e4SLinus Torvalds 	        if (copy_to_user((void __user *) param, &floppy_type,
9251da177e4SLinus Torvalds 				 sizeof(struct floppy_struct)))
9261da177e4SLinus Torvalds 			return -EFAULT;
9271da177e4SLinus Torvalds 		return 0;
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 	return -ENOTTY;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9328a6cfeb6SArnd Bergmann static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
9338a6cfeb6SArnd Bergmann 				 unsigned int cmd, unsigned long param)
9348a6cfeb6SArnd Bergmann {
9358a6cfeb6SArnd Bergmann 	int ret;
9368a6cfeb6SArnd Bergmann 
9372a48fc0aSArnd Bergmann 	mutex_lock(&swim3_mutex);
9388a6cfeb6SArnd Bergmann 	ret = floppy_locked_ioctl(bdev, mode, cmd, param);
9392a48fc0aSArnd Bergmann 	mutex_unlock(&swim3_mutex);
9408a6cfeb6SArnd Bergmann 
9418a6cfeb6SArnd Bergmann 	return ret;
9428a6cfeb6SArnd Bergmann }
9438a6cfeb6SArnd Bergmann 
944b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode)
9451da177e4SLinus Torvalds {
946b4d9a442SAl Viro 	struct floppy_state *fs = bdev->bd_disk->private_data;
9471da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
9481da177e4SLinus Torvalds 	int n, err = 0;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	if (fs->ref_count == 0) {
951d58b0c39SBenjamin Herrenschmidt 		if (fs->mdev->media_bay &&
952d58b0c39SBenjamin Herrenschmidt 		    check_media_bay(fs->mdev->media_bay) != MB_FD)
9531da177e4SLinus Torvalds 			return -ENXIO;
9541da177e4SLinus Torvalds 		out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
9551da177e4SLinus Torvalds 		out_8(&sw->control_bic, 0xff);
9561da177e4SLinus Torvalds 		out_8(&sw->mode, 0x95);
9571da177e4SLinus Torvalds 		udelay(10);
9581da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
9591da177e4SLinus Torvalds 		out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
9601da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_ON);
9611da177e4SLinus Torvalds 		fs->write_prot = -1;
9621da177e4SLinus Torvalds 		fs->cur_cyl = -1;
9631da177e4SLinus Torvalds 		for (n = 0; n < 2 * HZ; ++n) {
9641da177e4SLinus Torvalds 			if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE))
9651da177e4SLinus Torvalds 				break;
9661da177e4SLinus Torvalds 			if (signal_pending(current)) {
9671da177e4SLinus Torvalds 				err = -EINTR;
9681da177e4SLinus Torvalds 				break;
9691da177e4SLinus Torvalds 			}
9701da177e4SLinus Torvalds 			swim3_select(fs, RELAX);
97186e84862SNishanth Aravamudan 			schedule_timeout_interruptible(1);
9721da177e4SLinus Torvalds 		}
9731da177e4SLinus Torvalds 		if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
9741da177e4SLinus Torvalds 				 || swim3_readbit(fs, DISK_IN) == 0))
9751da177e4SLinus Torvalds 			err = -ENXIO;
9761da177e4SLinus Torvalds 		swim3_action(fs, SETMFM);
9771da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
9781da177e4SLinus Torvalds 
979b4d9a442SAl Viro 	} else if (fs->ref_count == -1 || mode & FMODE_EXCL)
9801da177e4SLinus Torvalds 		return -EBUSY;
9811da177e4SLinus Torvalds 
982b4d9a442SAl Viro 	if (err == 0 && (mode & FMODE_NDELAY) == 0
983b4d9a442SAl Viro 	    && (mode & (FMODE_READ|FMODE_WRITE))) {
984b4d9a442SAl Viro 		check_disk_change(bdev);
9851da177e4SLinus Torvalds 		if (fs->ejected)
9861da177e4SLinus Torvalds 			err = -ENXIO;
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 
989b4d9a442SAl Viro 	if (err == 0 && (mode & FMODE_WRITE)) {
9901da177e4SLinus Torvalds 		if (fs->write_prot < 0)
9911da177e4SLinus Torvalds 			fs->write_prot = swim3_readbit(fs, WRITE_PROT);
9921da177e4SLinus Torvalds 		if (fs->write_prot)
9931da177e4SLinus Torvalds 			err = -EROFS;
9941da177e4SLinus Torvalds 	}
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	if (err) {
9971da177e4SLinus Torvalds 		if (fs->ref_count == 0) {
9981da177e4SLinus Torvalds 			swim3_action(fs, MOTOR_OFF);
9991da177e4SLinus Torvalds 			out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
10001da177e4SLinus Torvalds 			swim3_select(fs, RELAX);
10011da177e4SLinus Torvalds 		}
10021da177e4SLinus Torvalds 		return err;
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 
1005b4d9a442SAl Viro 	if (mode & FMODE_EXCL)
10061da177e4SLinus Torvalds 		fs->ref_count = -1;
10071da177e4SLinus Torvalds 	else
10081da177e4SLinus Torvalds 		++fs->ref_count;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	return 0;
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
10136e9624b8SArnd Bergmann static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
10146e9624b8SArnd Bergmann {
10156e9624b8SArnd Bergmann 	int ret;
10166e9624b8SArnd Bergmann 
10172a48fc0aSArnd Bergmann 	mutex_lock(&swim3_mutex);
10186e9624b8SArnd Bergmann 	ret = floppy_open(bdev, mode);
10192a48fc0aSArnd Bergmann 	mutex_unlock(&swim3_mutex);
10206e9624b8SArnd Bergmann 
10216e9624b8SArnd Bergmann 	return ret;
10226e9624b8SArnd Bergmann }
10236e9624b8SArnd Bergmann 
1024db2a144bSAl Viro static void floppy_release(struct gendisk *disk, fmode_t mode)
10251da177e4SLinus Torvalds {
1026b4d9a442SAl Viro 	struct floppy_state *fs = disk->private_data;
10271da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
1028b3025457SBenjamin Herrenschmidt 
10292a48fc0aSArnd Bergmann 	mutex_lock(&swim3_mutex);
10301da177e4SLinus Torvalds 	if (fs->ref_count > 0 && --fs->ref_count == 0) {
10311da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_OFF);
10321da177e4SLinus Torvalds 		out_8(&sw->control_bic, 0xff);
10331da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
10341da177e4SLinus Torvalds 	}
10352a48fc0aSArnd Bergmann 	mutex_unlock(&swim3_mutex);
10361da177e4SLinus Torvalds }
10371da177e4SLinus Torvalds 
10384bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk,
10394bbde777STejun Heo 					unsigned int clearing)
10401da177e4SLinus Torvalds {
10411da177e4SLinus Torvalds 	struct floppy_state *fs = disk->private_data;
10424bbde777STejun Heo 	return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk)
10461da177e4SLinus Torvalds {
10471da177e4SLinus Torvalds 	struct floppy_state *fs = disk->private_data;
10481da177e4SLinus Torvalds 	struct swim3 __iomem *sw;
10491da177e4SLinus Torvalds 	int ret, n;
10501da177e4SLinus Torvalds 
1051d58b0c39SBenjamin Herrenschmidt 	if (fs->mdev->media_bay &&
1052d58b0c39SBenjamin Herrenschmidt 	    check_media_bay(fs->mdev->media_bay) != MB_FD)
10531da177e4SLinus Torvalds 		return -ENXIO;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	sw = fs->swim3;
10561da177e4SLinus Torvalds 	grab_drive(fs, revalidating, 0);
10571da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
10581da177e4SLinus Torvalds 	out_8(&sw->control_bis, DRIVE_ENABLE);
10591da177e4SLinus Torvalds 	swim3_action(fs, MOTOR_ON);	/* necessary? */
10601da177e4SLinus Torvalds 	fs->write_prot = -1;
10611da177e4SLinus Torvalds 	fs->cur_cyl = -1;
10621da177e4SLinus Torvalds 	mdelay(1);
10631da177e4SLinus Torvalds 	for (n = HZ; n > 0; --n) {
10641da177e4SLinus Torvalds 		if (swim3_readbit(fs, SEEK_COMPLETE))
10651da177e4SLinus Torvalds 			break;
10661da177e4SLinus Torvalds 		if (signal_pending(current))
10671da177e4SLinus Torvalds 			break;
10681da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
106986e84862SNishanth Aravamudan 		schedule_timeout_interruptible(1);
10701da177e4SLinus Torvalds 	}
10711da177e4SLinus Torvalds 	ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
10721da177e4SLinus Torvalds 		|| swim3_readbit(fs, DISK_IN) == 0;
10731da177e4SLinus Torvalds 	if (ret)
10741da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_OFF);
10751da177e4SLinus Torvalds 	else {
10761da177e4SLinus Torvalds 		fs->ejected = 0;
10771da177e4SLinus Torvalds 		swim3_action(fs, SETMFM);
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 	swim3_select(fs, RELAX);
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	release_drive(fs);
10821da177e4SLinus Torvalds 	return ret;
10831da177e4SLinus Torvalds }
10841da177e4SLinus Torvalds 
108583d5cde4SAlexey Dobriyan static const struct block_device_operations floppy_fops = {
10866e9624b8SArnd Bergmann 	.open		= floppy_unlocked_open,
1087b4d9a442SAl Viro 	.release	= floppy_release,
10888a6cfeb6SArnd Bergmann 	.ioctl		= floppy_ioctl,
10894bbde777STejun Heo 	.check_events	= floppy_check_events,
10901da177e4SLinus Torvalds 	.revalidate_disk= floppy_revalidate,
10911da177e4SLinus Torvalds };
10921da177e4SLinus Torvalds 
1093b3025457SBenjamin Herrenschmidt static void swim3_mb_event(struct macio_dev* mdev, int mb_state)
1094b3025457SBenjamin Herrenschmidt {
1095b3025457SBenjamin Herrenschmidt 	struct floppy_state *fs = macio_get_drvdata(mdev);
10967414d4f6SCong Ding 	struct swim3 __iomem *sw;
1097b3025457SBenjamin Herrenschmidt 
1098b3025457SBenjamin Herrenschmidt 	if (!fs)
1099b3025457SBenjamin Herrenschmidt 		return;
11007414d4f6SCong Ding 
11017414d4f6SCong Ding 	sw = fs->swim3;
11027414d4f6SCong Ding 
1103b3025457SBenjamin Herrenschmidt 	if (mb_state != MB_FD)
1104b3025457SBenjamin Herrenschmidt 		return;
1105b3025457SBenjamin Herrenschmidt 
1106b3025457SBenjamin Herrenschmidt 	/* Clear state */
1107b3025457SBenjamin Herrenschmidt 	out_8(&sw->intr_enable, 0);
1108b3025457SBenjamin Herrenschmidt 	in_8(&sw->intr);
1109b3025457SBenjamin Herrenschmidt 	in_8(&sw->error);
1110b3025457SBenjamin Herrenschmidt }
1111b3025457SBenjamin Herrenschmidt 
11123e9a6927SBenjamin Herrenschmidt static int swim3_add_device(struct macio_dev *mdev, int index)
11131da177e4SLinus Torvalds {
111461c7a080SGrant Likely 	struct device_node *swim = mdev->ofdev.dev.of_node;
11153e9a6927SBenjamin Herrenschmidt 	struct floppy_state *fs = &floppy_states[index];
11163e9a6927SBenjamin Herrenschmidt 	int rc = -EBUSY;
11171da177e4SLinus Torvalds 
1118b3025457SBenjamin Herrenschmidt 	/* Do this first for message macros */
1119b3025457SBenjamin Herrenschmidt 	memset(fs, 0, sizeof(*fs));
1120b3025457SBenjamin Herrenschmidt 	fs->mdev = mdev;
1121b3025457SBenjamin Herrenschmidt 	fs->index = index;
1122b3025457SBenjamin Herrenschmidt 
11233e9a6927SBenjamin Herrenschmidt 	/* Check & Request resources */
11243e9a6927SBenjamin Herrenschmidt 	if (macio_resource_count(mdev) < 2) {
1125b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "No address in device-tree\n");
11263e9a6927SBenjamin Herrenschmidt 		return -ENXIO;
1127cc5d0189SBenjamin Herrenschmidt 	}
1128b3025457SBenjamin Herrenschmidt 	if (macio_irq_count(mdev) < 1) {
1129b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "No interrupt in device-tree\n");
1130b3025457SBenjamin Herrenschmidt 		return -ENXIO;
1131cc5d0189SBenjamin Herrenschmidt 	}
11323e9a6927SBenjamin Herrenschmidt 	if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
1133b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Can't request mmio resource\n");
11343e9a6927SBenjamin Herrenschmidt 		return -EBUSY;
11351da177e4SLinus Torvalds 	}
11363e9a6927SBenjamin Herrenschmidt 	if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
1137b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Can't request dma resource\n");
11383e9a6927SBenjamin Herrenschmidt 		macio_release_resource(mdev, 0);
11393e9a6927SBenjamin Herrenschmidt 		return -EBUSY;
11403e9a6927SBenjamin Herrenschmidt 	}
11413e9a6927SBenjamin Herrenschmidt 	dev_set_drvdata(&mdev->ofdev.dev, fs);
11421da177e4SLinus Torvalds 
1143d58b0c39SBenjamin Herrenschmidt 	if (mdev->media_bay == NULL)
11441da177e4SLinus Torvalds 		pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	fs->state = idle;
11473e9a6927SBenjamin Herrenschmidt 	fs->swim3 = (struct swim3 __iomem *)
11483e9a6927SBenjamin Herrenschmidt 		ioremap(macio_resource_start(mdev, 0), 0x200);
11493e9a6927SBenjamin Herrenschmidt 	if (fs->swim3 == NULL) {
1150b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Couldn't map mmio registers\n");
11513e9a6927SBenjamin Herrenschmidt 		rc = -ENOMEM;
11523e9a6927SBenjamin Herrenschmidt 		goto out_release;
11533e9a6927SBenjamin Herrenschmidt 	}
11543e9a6927SBenjamin Herrenschmidt 	fs->dma = (struct dbdma_regs __iomem *)
11553e9a6927SBenjamin Herrenschmidt 		ioremap(macio_resource_start(mdev, 1), 0x200);
11563e9a6927SBenjamin Herrenschmidt 	if (fs->dma == NULL) {
1157b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Couldn't map dma registers\n");
11583e9a6927SBenjamin Herrenschmidt 		iounmap(fs->swim3);
11593e9a6927SBenjamin Herrenschmidt 		rc = -ENOMEM;
11603e9a6927SBenjamin Herrenschmidt 		goto out_release;
11613e9a6927SBenjamin Herrenschmidt 	}
11623e9a6927SBenjamin Herrenschmidt 	fs->swim3_intr = macio_irq(mdev, 0);
1163a419aef8SJoe Perches 	fs->dma_intr = macio_irq(mdev, 1);
11641da177e4SLinus Torvalds 	fs->cur_cyl = -1;
11651da177e4SLinus Torvalds 	fs->cur_sector = -1;
11661da177e4SLinus Torvalds 	fs->secpercyl = 36;
11671da177e4SLinus Torvalds 	fs->secpertrack = 18;
11681da177e4SLinus Torvalds 	fs->total_secs = 2880;
11691da177e4SLinus Torvalds 	init_waitqueue_head(&fs->wait);
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
11721da177e4SLinus Torvalds 	memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
1173f5718726SDavid Gibson 	fs->dma_cmd[1].command = cpu_to_le16(DBDMA_STOP);
11741da177e4SLinus Torvalds 
1175b3025457SBenjamin Herrenschmidt 	if (mdev->media_bay == NULL || check_media_bay(mdev->media_bay) == MB_FD)
1176b3025457SBenjamin Herrenschmidt 		swim3_mb_event(mdev, MB_FD);
1177b3025457SBenjamin Herrenschmidt 
11781da177e4SLinus Torvalds 	if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
1179b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Couldn't request interrupt\n");
11801da177e4SLinus Torvalds 		pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
11813e9a6927SBenjamin Herrenschmidt 		goto out_unmap;
11821da177e4SLinus Torvalds 		return -EBUSY;
11831da177e4SLinus Torvalds 	}
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	init_timer(&fs->timeout);
11861da177e4SLinus Torvalds 
1187b3025457SBenjamin Herrenschmidt 	swim3_info("SWIM3 floppy controller %s\n",
1188d58b0c39SBenjamin Herrenschmidt 		mdev->media_bay ? "in media bay" : "");
11891da177e4SLinus Torvalds 
11903e9a6927SBenjamin Herrenschmidt 	return 0;
11911da177e4SLinus Torvalds 
11923e9a6927SBenjamin Herrenschmidt  out_unmap:
11933e9a6927SBenjamin Herrenschmidt 	iounmap(fs->dma);
11943e9a6927SBenjamin Herrenschmidt 	iounmap(fs->swim3);
11953e9a6927SBenjamin Herrenschmidt 
11963e9a6927SBenjamin Herrenschmidt  out_release:
11973e9a6927SBenjamin Herrenschmidt 	macio_release_resource(mdev, 0);
11983e9a6927SBenjamin Herrenschmidt 	macio_release_resource(mdev, 1);
11993e9a6927SBenjamin Herrenschmidt 
12003e9a6927SBenjamin Herrenschmidt 	return rc;
12013e9a6927SBenjamin Herrenschmidt }
12023e9a6927SBenjamin Herrenschmidt 
12038d85fce7SGreg Kroah-Hartman static int swim3_attach(struct macio_dev *mdev,
12048d85fce7SGreg Kroah-Hartman 			const struct of_device_id *match)
12053e9a6927SBenjamin Herrenschmidt {
12063e9a6927SBenjamin Herrenschmidt 	struct gendisk *disk;
1207b3025457SBenjamin Herrenschmidt 	int index, rc;
1208b3025457SBenjamin Herrenschmidt 
1209b3025457SBenjamin Herrenschmidt 	index = floppy_count++;
1210b3025457SBenjamin Herrenschmidt 	if (index >= MAX_FLOPPIES)
1211b3025457SBenjamin Herrenschmidt 		return -ENXIO;
12123e9a6927SBenjamin Herrenschmidt 
12133e9a6927SBenjamin Herrenschmidt 	/* Add the drive */
1214b3025457SBenjamin Herrenschmidt 	rc = swim3_add_device(mdev, index);
12153e9a6927SBenjamin Herrenschmidt 	if (rc)
12163e9a6927SBenjamin Herrenschmidt 		return rc;
1217b3025457SBenjamin Herrenschmidt 	/* Now register that disk. Same comment about failure handling */
1218b3025457SBenjamin Herrenschmidt 	disk = disks[index] = alloc_disk(1);
1219b3025457SBenjamin Herrenschmidt 	if (disk == NULL)
1220b3025457SBenjamin Herrenschmidt 		return -ENOMEM;
1221b3025457SBenjamin Herrenschmidt 	disk->queue = blk_init_queue(do_fd_request, &swim3_lock);
1222b3025457SBenjamin Herrenschmidt 	if (disk->queue == NULL) {
1223b3025457SBenjamin Herrenschmidt 		put_disk(disk);
1224b3025457SBenjamin Herrenschmidt 		return -ENOMEM;
1225b3025457SBenjamin Herrenschmidt 	}
1226b3025457SBenjamin Herrenschmidt 	disk->queue->queuedata = &floppy_states[index];
12273e9a6927SBenjamin Herrenschmidt 
1228b3025457SBenjamin Herrenschmidt 	if (index == 0) {
12293e9a6927SBenjamin Herrenschmidt 		/* If we failed, there isn't much we can do as the driver is still
12303e9a6927SBenjamin Herrenschmidt 		 * too dumb to remove the device, just bail out
12313e9a6927SBenjamin Herrenschmidt 		 */
12323e9a6927SBenjamin Herrenschmidt 		if (register_blkdev(FLOPPY_MAJOR, "fd"))
12333e9a6927SBenjamin Herrenschmidt 			return 0;
12343e9a6927SBenjamin Herrenschmidt 	}
12353e9a6927SBenjamin Herrenschmidt 
12363e9a6927SBenjamin Herrenschmidt 	disk->major = FLOPPY_MAJOR;
1237b3025457SBenjamin Herrenschmidt 	disk->first_minor = index;
12383e9a6927SBenjamin Herrenschmidt 	disk->fops = &floppy_fops;
1239b3025457SBenjamin Herrenschmidt 	disk->private_data = &floppy_states[index];
12403e9a6927SBenjamin Herrenschmidt 	disk->flags |= GENHD_FL_REMOVABLE;
1241b3025457SBenjamin Herrenschmidt 	sprintf(disk->disk_name, "fd%d", index);
12423e9a6927SBenjamin Herrenschmidt 	set_capacity(disk, 2880);
12433e9a6927SBenjamin Herrenschmidt 	add_disk(disk);
12443e9a6927SBenjamin Herrenschmidt 
12453e9a6927SBenjamin Herrenschmidt 	return 0;
12463e9a6927SBenjamin Herrenschmidt }
12473e9a6927SBenjamin Herrenschmidt 
12483e9a6927SBenjamin Herrenschmidt static struct of_device_id swim3_match[] =
12493e9a6927SBenjamin Herrenschmidt {
12503e9a6927SBenjamin Herrenschmidt 	{
12513e9a6927SBenjamin Herrenschmidt 	.name		= "swim3",
12523e9a6927SBenjamin Herrenschmidt 	},
12533e9a6927SBenjamin Herrenschmidt 	{
12543e9a6927SBenjamin Herrenschmidt 	.compatible	= "ohare-swim3"
12553e9a6927SBenjamin Herrenschmidt 	},
12563e9a6927SBenjamin Herrenschmidt 	{
12573e9a6927SBenjamin Herrenschmidt 	.compatible	= "swim3"
12583e9a6927SBenjamin Herrenschmidt 	},
1259f41c53a5SAxel Lin 	{ /* end of list */ }
12603e9a6927SBenjamin Herrenschmidt };
12613e9a6927SBenjamin Herrenschmidt 
12623e9a6927SBenjamin Herrenschmidt static struct macio_driver swim3_driver =
12633e9a6927SBenjamin Herrenschmidt {
1264c2cdf6abSBenjamin Herrenschmidt 	.driver = {
12653e9a6927SBenjamin Herrenschmidt 		.name 		= "swim3",
1266c2cdf6abSBenjamin Herrenschmidt 		.of_match_table	= swim3_match,
1267c2cdf6abSBenjamin Herrenschmidt 	},
12683e9a6927SBenjamin Herrenschmidt 	.probe		= swim3_attach,
1269b3025457SBenjamin Herrenschmidt #ifdef CONFIG_PMAC_MEDIABAY
1270b3025457SBenjamin Herrenschmidt 	.mediabay_event	= swim3_mb_event,
1271b3025457SBenjamin Herrenschmidt #endif
12723e9a6927SBenjamin Herrenschmidt #if 0
12733e9a6927SBenjamin Herrenschmidt 	.suspend	= swim3_suspend,
12743e9a6927SBenjamin Herrenschmidt 	.resume		= swim3_resume,
12753e9a6927SBenjamin Herrenschmidt #endif
12763e9a6927SBenjamin Herrenschmidt };
12773e9a6927SBenjamin Herrenschmidt 
12783e9a6927SBenjamin Herrenschmidt 
12793e9a6927SBenjamin Herrenschmidt int swim3_init(void)
12803e9a6927SBenjamin Herrenschmidt {
12813e9a6927SBenjamin Herrenschmidt 	macio_register_driver(&swim3_driver);
12821da177e4SLinus Torvalds 	return 0;
12831da177e4SLinus Torvalds }
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds module_init(swim3_init)
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds MODULE_LICENSE("GPL");
12881da177e4SLinus Torvalds MODULE_AUTHOR("Paul Mackerras");
12891da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);
1290