xref: /openbmc/linux/drivers/block/swim3.c (revision 56a1c5ee54f69dd767fb61d301883dc919ddc259)
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>
23174cd4b1SIngo 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
151*56a1c5eeSFinn Thain #define ONEMEG_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
159*56a1c5eeSFinn Thain #define GCR_MODE	13
1601da177e4SLinus Torvalds #define SEEK_COMPLETE	14
161*56a1c5eeSFinn Thain #define TWOMEG_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);
242b5775a6bSKees Cook static void scan_timeout(struct timer_list *t);
243b5775a6bSKees Cook static void seek_timeout(struct timer_list *t);
244b5775a6bSKees Cook static void settle_timeout(struct timer_list *t);
245b5775a6bSKees Cook static void xfer_timeout(struct timer_list *t);
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 
2602a842acaSChristoph Hellwig static bool swim3_end_request(struct floppy_state *fs, blk_status_t 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");
3372a842acaSChristoph Hellwig 			swim3_end_request(fs, BLK_STS_IOERR, 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));
346c877f424SChristoph Hellwig 		swim3_dbg("           current_nr_sectors=%u\n",
347c877f424SChristoph Hellwig 			  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);
3532a842acaSChristoph Hellwig 			swim3_end_request(fs, BLK_STS_IOERR, 0);
3541da177e4SLinus Torvalds 			continue;
3551da177e4SLinus Torvalds 		}
3561da177e4SLinus Torvalds 		if (fs->ejected) {
357b3025457SBenjamin Herrenschmidt 			swim3_dbg("%s", "  disk ejected\n");
3582a842acaSChristoph Hellwig 			swim3_end_request(fs, BLK_STS_IOERR, 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");
3672a842acaSChristoph Hellwig 				swim3_end_request(fs, BLK_STS_IOERR, 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,
395b5775a6bSKees Cook 			void (*proc)(struct timer_list *t))
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	if (fs->timeout_pending)
3981da177e4SLinus Torvalds 		del_timer(&fs->timeout);
3991da177e4SLinus Torvalds 	fs->timeout.expires = jiffies + nticks;
400841b86f3SKees Cook 	fs->timeout.function = proc;
4011da177e4SLinus Torvalds 	add_timer(&fs->timeout);
4021da177e4SLinus Torvalds 	fs->timeout_pending = 1;
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds static inline void scan_track(struct floppy_state *fs)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	swim3_select(fs, READ_DATA_0);
4101da177e4SLinus Torvalds 	in_8(&sw->intr);		/* clear SEEN_SECTOR bit */
4111da177e4SLinus Torvalds 	in_8(&sw->error);
4121da177e4SLinus Torvalds 	out_8(&sw->intr_enable, SEEN_SECTOR);
4131da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_ACTION);
4141da177e4SLinus Torvalds 	/* enable intr when track found */
4151da177e4SLinus Torvalds 	set_timeout(fs, HZ, scan_timeout);	/* enable timeout */
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds static inline void seek_track(struct floppy_state *fs, int n)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	if (n >= 0) {
4231da177e4SLinus Torvalds 		swim3_action(fs, SEEK_POSITIVE);
4241da177e4SLinus Torvalds 		sw->nseek = n;
4251da177e4SLinus Torvalds 	} else {
4261da177e4SLinus Torvalds 		swim3_action(fs, SEEK_NEGATIVE);
4271da177e4SLinus Torvalds 		sw->nseek = -n;
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 	fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1;
4301da177e4SLinus Torvalds 	swim3_select(fs, STEP);
4311da177e4SLinus Torvalds 	in_8(&sw->error);
4321da177e4SLinus Torvalds 	/* enable intr when seek finished */
4331da177e4SLinus Torvalds 	out_8(&sw->intr_enable, SEEK_DONE);
4341da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_SEEK);
4351da177e4SLinus Torvalds 	set_timeout(fs, 3*HZ, seek_timeout);	/* enable timeout */
4361da177e4SLinus Torvalds 	fs->settle_time = 0;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds static inline void init_dma(struct dbdma_cmd *cp, int cmd,
4401da177e4SLinus Torvalds 			    void *buf, int count)
4411da177e4SLinus Torvalds {
442f5718726SDavid Gibson 	cp->req_count = cpu_to_le16(count);
443f5718726SDavid Gibson 	cp->command = cpu_to_le16(cmd);
444f5718726SDavid Gibson 	cp->phy_addr = cpu_to_le32(virt_to_bus(buf));
4451da177e4SLinus Torvalds 	cp->xfer_status = 0;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds static inline void setup_transfer(struct floppy_state *fs)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	int n;
4511da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
4521da177e4SLinus Torvalds 	struct dbdma_cmd *cp = fs->dma_cmd;
4531da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr = fs->dma;
454b3025457SBenjamin Herrenschmidt 	struct request *req = fs->cur_req;
4551da177e4SLinus Torvalds 
456b3025457SBenjamin Herrenschmidt 	if (blk_rq_cur_sectors(req) <= 0) {
457b3025457SBenjamin Herrenschmidt 		swim3_warn("%s", "Transfer 0 sectors ?\n");
4581da177e4SLinus Torvalds 		return;
4591da177e4SLinus Torvalds 	}
460b3025457SBenjamin Herrenschmidt 	if (rq_data_dir(req) == WRITE)
4611da177e4SLinus Torvalds 		n = 1;
4621da177e4SLinus Torvalds 	else {
4631da177e4SLinus Torvalds 		n = fs->secpertrack - fs->req_sector + 1;
464b3025457SBenjamin Herrenschmidt 		if (n > blk_rq_cur_sectors(req))
465b3025457SBenjamin Herrenschmidt 			n = blk_rq_cur_sectors(req);
4661da177e4SLinus Torvalds 	}
467b3025457SBenjamin Herrenschmidt 
468b3025457SBenjamin Herrenschmidt 	swim3_dbg("  setup xfer at sect %d (of %d) head %d for %d\n",
469b3025457SBenjamin Herrenschmidt 		  fs->req_sector, fs->secpertrack, fs->head, n);
470b3025457SBenjamin Herrenschmidt 
4711da177e4SLinus Torvalds 	fs->scount = n;
4721da177e4SLinus Torvalds 	swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
4731da177e4SLinus Torvalds 	out_8(&sw->sector, fs->req_sector);
4741da177e4SLinus Torvalds 	out_8(&sw->nsect, n);
4751da177e4SLinus Torvalds 	out_8(&sw->gap3, 0);
4761da177e4SLinus Torvalds 	out_le32(&dr->cmdptr, virt_to_bus(cp));
477b3025457SBenjamin Herrenschmidt 	if (rq_data_dir(req) == WRITE) {
4781da177e4SLinus Torvalds 		/* Set up 3 dma commands: write preamble, data, postamble */
4791da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
4801da177e4SLinus Torvalds 		++cp;
481b4f42e28SJens Axboe 		init_dma(cp, OUTPUT_MORE, bio_data(req->bio), 512);
4821da177e4SLinus Torvalds 		++cp;
4831da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
4841da177e4SLinus Torvalds 	} else {
485b4f42e28SJens Axboe 		init_dma(cp, INPUT_LAST, bio_data(req->bio), n * 512);
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 	++cp;
4881da177e4SLinus Torvalds 	out_le16(&cp->command, DBDMA_STOP);
4891da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
4901da177e4SLinus Torvalds 	in_8(&sw->error);
4911da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
492b3025457SBenjamin Herrenschmidt 	if (rq_data_dir(req) == WRITE)
4931da177e4SLinus Torvalds 		out_8(&sw->control_bis, WRITE_SECTORS);
4941da177e4SLinus Torvalds 	in_8(&sw->intr);
4951da177e4SLinus Torvalds 	out_le32(&dr->control, (RUN << 16) | RUN);
4961da177e4SLinus Torvalds 	/* enable intr when transfer complete */
4971da177e4SLinus Torvalds 	out_8(&sw->intr_enable, TRANSFER_DONE);
4981da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_ACTION);
4991da177e4SLinus Torvalds 	set_timeout(fs, 2*HZ, xfer_timeout);	/* enable timeout */
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static void act(struct floppy_state *fs)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	for (;;) {
505b3025457SBenjamin Herrenschmidt 		swim3_dbg("  act loop, state=%d, req_cyl=%d, cur_cyl=%d\n",
506b3025457SBenjamin Herrenschmidt 			  fs->state, fs->req_cyl, fs->cur_cyl);
507b3025457SBenjamin Herrenschmidt 
5081da177e4SLinus Torvalds 		switch (fs->state) {
5091da177e4SLinus Torvalds 		case idle:
5101da177e4SLinus Torvalds 			return;		/* XXX shouldn't get here */
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 		case locating:
5131da177e4SLinus Torvalds 			if (swim3_readbit(fs, TRACK_ZERO)) {
514b3025457SBenjamin Herrenschmidt 				swim3_dbg("%s", "    locate track 0\n");
5151da177e4SLinus Torvalds 				fs->cur_cyl = 0;
5161da177e4SLinus Torvalds 				if (fs->req_cyl == 0)
5171da177e4SLinus Torvalds 					fs->state = do_transfer;
5181da177e4SLinus Torvalds 				else
5191da177e4SLinus Torvalds 					fs->state = seeking;
5201da177e4SLinus Torvalds 				break;
5211da177e4SLinus Torvalds 			}
5221da177e4SLinus Torvalds 			scan_track(fs);
5231da177e4SLinus Torvalds 			return;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 		case seeking:
5261da177e4SLinus Torvalds 			if (fs->cur_cyl < 0) {
5271da177e4SLinus Torvalds 				fs->expect_cyl = -1;
5281da177e4SLinus Torvalds 				fs->state = locating;
5291da177e4SLinus Torvalds 				break;
5301da177e4SLinus Torvalds 			}
5311da177e4SLinus Torvalds 			if (fs->req_cyl == fs->cur_cyl) {
532b3025457SBenjamin Herrenschmidt 				swim3_warn("%s", "Whoops, seeking 0\n");
5331da177e4SLinus Torvalds 				fs->state = do_transfer;
5341da177e4SLinus Torvalds 				break;
5351da177e4SLinus Torvalds 			}
5361da177e4SLinus Torvalds 			seek_track(fs, fs->req_cyl - fs->cur_cyl);
5371da177e4SLinus Torvalds 			return;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 		case settling:
5401da177e4SLinus Torvalds 			/* check for SEEK_COMPLETE after 30ms */
5411da177e4SLinus Torvalds 			fs->settle_time = (HZ + 32) / 33;
5421da177e4SLinus Torvalds 			set_timeout(fs, fs->settle_time, settle_timeout);
5431da177e4SLinus Torvalds 			return;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 		case do_transfer:
5461da177e4SLinus Torvalds 			if (fs->cur_cyl != fs->req_cyl) {
5471da177e4SLinus Torvalds 				if (fs->retries > 5) {
548b3025457SBenjamin Herrenschmidt 					swim3_err("Wrong cylinder in transfer, want: %d got %d\n",
549b3025457SBenjamin Herrenschmidt 						  fs->req_cyl, fs->cur_cyl);
5502a842acaSChristoph Hellwig 					swim3_end_request(fs, BLK_STS_IOERR, 0);
5511da177e4SLinus Torvalds 					fs->state = idle;
5521da177e4SLinus Torvalds 					return;
5531da177e4SLinus Torvalds 				}
5541da177e4SLinus Torvalds 				fs->state = seeking;
5551da177e4SLinus Torvalds 				break;
5561da177e4SLinus Torvalds 			}
5571da177e4SLinus Torvalds 			setup_transfer(fs);
5581da177e4SLinus Torvalds 			return;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 		case jogging:
5611da177e4SLinus Torvalds 			seek_track(fs, -5);
5621da177e4SLinus Torvalds 			return;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 		default:
565b3025457SBenjamin Herrenschmidt 			swim3_err("Unknown state %d\n", fs->state);
5661da177e4SLinus Torvalds 			return;
5671da177e4SLinus Torvalds 		}
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
571b5775a6bSKees Cook static void scan_timeout(struct timer_list *t)
5721da177e4SLinus Torvalds {
573b5775a6bSKees Cook 	struct floppy_state *fs = from_timer(fs, t, timeout);
5741da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
575b3025457SBenjamin Herrenschmidt 	unsigned long flags;
5761da177e4SLinus Torvalds 
577b3025457SBenjamin Herrenschmidt 	swim3_dbg("* scan timeout, state=%d\n", fs->state);
578b3025457SBenjamin Herrenschmidt 
579b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
5801da177e4SLinus Torvalds 	fs->timeout_pending = 0;
5811da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
5821da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
5831da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
5841da177e4SLinus Torvalds 	fs->cur_cyl = -1;
5851da177e4SLinus Torvalds 	if (fs->retries > 5) {
5862a842acaSChristoph Hellwig 		swim3_end_request(fs, BLK_STS_IOERR, 0);
5871da177e4SLinus Torvalds 		fs->state = idle;
5881da177e4SLinus Torvalds 		start_request(fs);
5891da177e4SLinus Torvalds 	} else {
5901da177e4SLinus Torvalds 		fs->state = jogging;
5911da177e4SLinus Torvalds 		act(fs);
5921da177e4SLinus Torvalds 	}
593b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
596b5775a6bSKees Cook static void seek_timeout(struct timer_list *t)
5971da177e4SLinus Torvalds {
598b5775a6bSKees Cook 	struct floppy_state *fs = from_timer(fs, t, timeout);
5991da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
600b3025457SBenjamin Herrenschmidt 	unsigned long flags;
6011da177e4SLinus Torvalds 
602b3025457SBenjamin Herrenschmidt 	swim3_dbg("* seek timeout, state=%d\n", fs->state);
603b3025457SBenjamin Herrenschmidt 
604b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
6051da177e4SLinus Torvalds 	fs->timeout_pending = 0;
6061da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_SEEK);
6071da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
6081da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
609b3025457SBenjamin Herrenschmidt 	swim3_err("%s", "Seek timeout\n");
6102a842acaSChristoph Hellwig 	swim3_end_request(fs, BLK_STS_IOERR, 0);
6111da177e4SLinus Torvalds 	fs->state = idle;
6121da177e4SLinus Torvalds 	start_request(fs);
613b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
616b5775a6bSKees Cook static void settle_timeout(struct timer_list *t)
6171da177e4SLinus Torvalds {
618b5775a6bSKees Cook 	struct floppy_state *fs = from_timer(fs, t, timeout);
6191da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
620b3025457SBenjamin Herrenschmidt 	unsigned long flags;
6211da177e4SLinus Torvalds 
622b3025457SBenjamin Herrenschmidt 	swim3_dbg("* settle timeout, state=%d\n", fs->state);
623b3025457SBenjamin Herrenschmidt 
624b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
6251da177e4SLinus Torvalds 	fs->timeout_pending = 0;
6261da177e4SLinus Torvalds 	if (swim3_readbit(fs, SEEK_COMPLETE)) {
6271da177e4SLinus Torvalds 		out_8(&sw->select, RELAX);
6281da177e4SLinus Torvalds 		fs->state = locating;
6291da177e4SLinus Torvalds 		act(fs);
630b3025457SBenjamin Herrenschmidt 		goto unlock;
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
6331da177e4SLinus Torvalds 	if (fs->settle_time < 2*HZ) {
6341da177e4SLinus Torvalds 		++fs->settle_time;
6351da177e4SLinus Torvalds 		set_timeout(fs, 1, settle_timeout);
636b3025457SBenjamin Herrenschmidt 		goto unlock;
6371da177e4SLinus Torvalds 	}
638b3025457SBenjamin Herrenschmidt 	swim3_err("%s", "Seek settle timeout\n");
6392a842acaSChristoph Hellwig 	swim3_end_request(fs, BLK_STS_IOERR, 0);
6401da177e4SLinus Torvalds 	fs->state = idle;
6411da177e4SLinus Torvalds 	start_request(fs);
642b3025457SBenjamin Herrenschmidt  unlock:
643b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
646b5775a6bSKees Cook static void xfer_timeout(struct timer_list *t)
6471da177e4SLinus Torvalds {
648b5775a6bSKees Cook 	struct floppy_state *fs = from_timer(fs, t, timeout);
6491da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
6501da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr = fs->dma;
651b3025457SBenjamin Herrenschmidt 	unsigned long flags;
6521da177e4SLinus Torvalds 	int n;
6531da177e4SLinus Torvalds 
654b3025457SBenjamin Herrenschmidt 	swim3_dbg("* xfer timeout, state=%d\n", fs->state);
655b3025457SBenjamin Herrenschmidt 
656b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
6571da177e4SLinus Torvalds 	fs->timeout_pending = 0;
6581da177e4SLinus Torvalds 	out_le32(&dr->control, RUN << 16);
6591da177e4SLinus Torvalds 	/* We must wait a bit for dbdma to stop */
6601da177e4SLinus Torvalds 	for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
6611da177e4SLinus Torvalds 		udelay(1);
6621da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
6631da177e4SLinus Torvalds 	out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
6641da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
665b3025457SBenjamin Herrenschmidt 	swim3_err("Timeout %sing sector %ld\n",
666b3025457SBenjamin Herrenschmidt 	       (rq_data_dir(fs->cur_req)==WRITE? "writ": "read"),
667b3025457SBenjamin Herrenschmidt 	       (long)blk_rq_pos(fs->cur_req));
6682a842acaSChristoph Hellwig 	swim3_end_request(fs, BLK_STS_IOERR, 0);
6691da177e4SLinus Torvalds 	fs->state = idle;
6701da177e4SLinus Torvalds 	start_request(fs);
671b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6747d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) dev_id;
6771da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
6781da177e4SLinus Torvalds 	int intr, err, n;
6791da177e4SLinus Torvalds 	int stat, resid;
6801da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr;
6811da177e4SLinus Torvalds 	struct dbdma_cmd *cp;
682b3025457SBenjamin Herrenschmidt 	unsigned long flags;
683b3025457SBenjamin Herrenschmidt 	struct request *req = fs->cur_req;
6841da177e4SLinus Torvalds 
685b3025457SBenjamin Herrenschmidt 	swim3_dbg("* interrupt, state=%d\n", fs->state);
686b3025457SBenjamin Herrenschmidt 
687b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
6881da177e4SLinus Torvalds 	intr = in_8(&sw->intr);
6891da177e4SLinus Torvalds 	err = (intr & ERROR_INTR)? in_8(&sw->error): 0;
6901da177e4SLinus Torvalds 	if ((intr & ERROR_INTR) && fs->state != do_transfer)
691b3025457SBenjamin Herrenschmidt 		swim3_err("Non-transfer error interrupt: state=%d, dir=%x, intr=%x, err=%x\n",
692b3025457SBenjamin Herrenschmidt 			  fs->state, rq_data_dir(req), intr, err);
6931da177e4SLinus Torvalds 	switch (fs->state) {
6941da177e4SLinus Torvalds 	case locating:
6951da177e4SLinus Torvalds 		if (intr & SEEN_SECTOR) {
6961da177e4SLinus Torvalds 			out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
6971da177e4SLinus Torvalds 			out_8(&sw->select, RELAX);
6981da177e4SLinus Torvalds 			out_8(&sw->intr_enable, 0);
6991da177e4SLinus Torvalds 			del_timer(&fs->timeout);
7001da177e4SLinus Torvalds 			fs->timeout_pending = 0;
7011da177e4SLinus Torvalds 			if (sw->ctrack == 0xff) {
702b3025457SBenjamin Herrenschmidt 				swim3_err("%s", "Seen sector but cyl=ff?\n");
7031da177e4SLinus Torvalds 				fs->cur_cyl = -1;
7041da177e4SLinus Torvalds 				if (fs->retries > 5) {
7052a842acaSChristoph Hellwig 					swim3_end_request(fs, BLK_STS_IOERR, 0);
7061da177e4SLinus Torvalds 					fs->state = idle;
7071da177e4SLinus Torvalds 					start_request(fs);
7081da177e4SLinus Torvalds 				} else {
7091da177e4SLinus Torvalds 					fs->state = jogging;
7101da177e4SLinus Torvalds 					act(fs);
7111da177e4SLinus Torvalds 				}
7121da177e4SLinus Torvalds 				break;
7131da177e4SLinus Torvalds 			}
7141da177e4SLinus Torvalds 			fs->cur_cyl = sw->ctrack;
7151da177e4SLinus Torvalds 			fs->cur_sector = sw->csect;
7161da177e4SLinus Torvalds 			if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
717b3025457SBenjamin Herrenschmidt 				swim3_err("Expected cyl %d, got %d\n",
7181da177e4SLinus Torvalds 					  fs->expect_cyl, fs->cur_cyl);
7191da177e4SLinus Torvalds 			fs->state = do_transfer;
7201da177e4SLinus Torvalds 			act(fs);
7211da177e4SLinus Torvalds 		}
7221da177e4SLinus Torvalds 		break;
7231da177e4SLinus Torvalds 	case seeking:
7241da177e4SLinus Torvalds 	case jogging:
7251da177e4SLinus Torvalds 		if (sw->nseek == 0) {
7261da177e4SLinus Torvalds 			out_8(&sw->control_bic, DO_SEEK);
7271da177e4SLinus Torvalds 			out_8(&sw->select, RELAX);
7281da177e4SLinus Torvalds 			out_8(&sw->intr_enable, 0);
7291da177e4SLinus Torvalds 			del_timer(&fs->timeout);
7301da177e4SLinus Torvalds 			fs->timeout_pending = 0;
7311da177e4SLinus Torvalds 			if (fs->state == seeking)
7321da177e4SLinus Torvalds 				++fs->retries;
7331da177e4SLinus Torvalds 			fs->state = settling;
7341da177e4SLinus Torvalds 			act(fs);
7351da177e4SLinus Torvalds 		}
7361da177e4SLinus Torvalds 		break;
7371da177e4SLinus Torvalds 	case settling:
7381da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
7391da177e4SLinus Torvalds 		del_timer(&fs->timeout);
7401da177e4SLinus Torvalds 		fs->timeout_pending = 0;
7411da177e4SLinus Torvalds 		act(fs);
7421da177e4SLinus Torvalds 		break;
7431da177e4SLinus Torvalds 	case do_transfer:
7441da177e4SLinus Torvalds 		if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
7451da177e4SLinus Torvalds 			break;
7461da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
7471da177e4SLinus Torvalds 		out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
7481da177e4SLinus Torvalds 		out_8(&sw->select, RELAX);
7491da177e4SLinus Torvalds 		del_timer(&fs->timeout);
7501da177e4SLinus Torvalds 		fs->timeout_pending = 0;
7511da177e4SLinus Torvalds 		dr = fs->dma;
7521da177e4SLinus Torvalds 		cp = fs->dma_cmd;
753b3025457SBenjamin Herrenschmidt 		if (rq_data_dir(req) == WRITE)
7541da177e4SLinus Torvalds 			++cp;
7551da177e4SLinus Torvalds 		/*
7561da177e4SLinus Torvalds 		 * Check that the main data transfer has finished.
7571da177e4SLinus Torvalds 		 * On writing, the swim3 sometimes doesn't use
7581da177e4SLinus Torvalds 		 * up all the bytes of the postamble, so we can still
7591da177e4SLinus Torvalds 		 * see DMA active here.  That doesn't matter as long
7601da177e4SLinus Torvalds 		 * as all the sector data has been transferred.
7611da177e4SLinus Torvalds 		 */
7621da177e4SLinus Torvalds 		if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) {
7631da177e4SLinus Torvalds 			/* wait a little while for DMA to complete */
7641da177e4SLinus Torvalds 			for (n = 0; n < 100; ++n) {
7651da177e4SLinus Torvalds 				if (cp->xfer_status != 0)
7661da177e4SLinus Torvalds 					break;
7671da177e4SLinus Torvalds 				udelay(1);
7681da177e4SLinus Torvalds 				barrier();
7691da177e4SLinus Torvalds 			}
7701da177e4SLinus Torvalds 		}
7711da177e4SLinus Torvalds 		/* turn off DMA */
7721da177e4SLinus Torvalds 		out_le32(&dr->control, (RUN | PAUSE) << 16);
773f5718726SDavid Gibson 		stat = le16_to_cpu(cp->xfer_status);
774f5718726SDavid Gibson 		resid = le16_to_cpu(cp->res_count);
7751da177e4SLinus Torvalds 		if (intr & ERROR_INTR) {
7761da177e4SLinus Torvalds 			n = fs->scount - 1 - resid / 512;
7771da177e4SLinus Torvalds 			if (n > 0) {
778b3025457SBenjamin Herrenschmidt 				blk_update_request(req, 0, n << 9);
7791da177e4SLinus Torvalds 				fs->req_sector += n;
7801da177e4SLinus Torvalds 			}
7811da177e4SLinus Torvalds 			if (fs->retries < 5) {
7821da177e4SLinus Torvalds 				++fs->retries;
7831da177e4SLinus Torvalds 				act(fs);
7841da177e4SLinus Torvalds 			} else {
785b3025457SBenjamin Herrenschmidt 				swim3_err("Error %sing block %ld (err=%x)\n",
786b3025457SBenjamin Herrenschmidt 				       rq_data_dir(req) == WRITE? "writ": "read",
787b3025457SBenjamin Herrenschmidt 				       (long)blk_rq_pos(req), err);
7882a842acaSChristoph Hellwig 				swim3_end_request(fs, BLK_STS_IOERR, 0);
7891da177e4SLinus Torvalds 				fs->state = idle;
7901da177e4SLinus Torvalds 			}
7911da177e4SLinus Torvalds 		} else {
7921da177e4SLinus Torvalds 			if ((stat & ACTIVE) == 0 || resid != 0) {
7931da177e4SLinus Torvalds 				/* musta been an error */
794b3025457SBenjamin Herrenschmidt 				swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid);
795b3025457SBenjamin Herrenschmidt 				swim3_err("  state=%d, dir=%x, intr=%x, err=%x\n",
796b3025457SBenjamin Herrenschmidt 					  fs->state, rq_data_dir(req), intr, err);
7972a842acaSChristoph Hellwig 				swim3_end_request(fs, BLK_STS_IOERR, 0);
7981da177e4SLinus Torvalds 				fs->state = idle;
7991da177e4SLinus Torvalds 				start_request(fs);
8001da177e4SLinus Torvalds 				break;
8011da177e4SLinus Torvalds 			}
802b3025457SBenjamin Herrenschmidt 			fs->retries = 0;
803b3025457SBenjamin Herrenschmidt 			if (swim3_end_request(fs, 0, fs->scount << 9)) {
8041da177e4SLinus Torvalds 				fs->req_sector += fs->scount;
8051da177e4SLinus Torvalds 				if (fs->req_sector > fs->secpertrack) {
8061da177e4SLinus Torvalds 					fs->req_sector -= fs->secpertrack;
8071da177e4SLinus Torvalds 					if (++fs->head > 1) {
8081da177e4SLinus Torvalds 						fs->head = 0;
8091da177e4SLinus Torvalds 						++fs->req_cyl;
8101da177e4SLinus Torvalds 					}
8111da177e4SLinus Torvalds 				}
8121da177e4SLinus Torvalds 				act(fs);
813467ca759STejun Heo 			} else
814467ca759STejun Heo 				fs->state = idle;
8151da177e4SLinus Torvalds 		}
8161da177e4SLinus Torvalds 		if (fs->state == idle)
8171da177e4SLinus Torvalds 			start_request(fs);
8181da177e4SLinus Torvalds 		break;
8191da177e4SLinus Torvalds 	default:
820b3025457SBenjamin Herrenschmidt 		swim3_err("Don't know what to do in state %d\n", fs->state);
8211da177e4SLinus Torvalds 	}
822b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
8231da177e4SLinus Torvalds 	return IRQ_HANDLED;
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds /*
8277d12e780SDavid Howells static void fd_dma_interrupt(int irq, void *dev_id)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds */
8311da177e4SLinus Torvalds 
832b3025457SBenjamin Herrenschmidt /* Called under the mutex to grab exclusive access to a drive */
8331da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state,
8341da177e4SLinus Torvalds 		      int interruptible)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	unsigned long flags;
8371da177e4SLinus Torvalds 
838b3025457SBenjamin Herrenschmidt 	swim3_dbg("%s", "-> grab drive\n");
839b3025457SBenjamin Herrenschmidt 
840b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
841b3025457SBenjamin Herrenschmidt 	if (fs->state != idle && fs->state != available) {
8421da177e4SLinus Torvalds 		++fs->wanted;
843106fd892SArnd Bergmann 		/* this will enable irqs in order to sleep */
844106fd892SArnd Bergmann 		if (!interruptible)
845106fd892SArnd Bergmann 			wait_event_lock_irq(fs->wait,
846106fd892SArnd Bergmann                                         fs->state == available,
847106fd892SArnd Bergmann                                         swim3_lock);
848106fd892SArnd Bergmann 		else if (wait_event_interruptible_lock_irq(fs->wait,
849106fd892SArnd Bergmann 					fs->state == available,
850106fd892SArnd Bergmann 					swim3_lock)) {
8511da177e4SLinus Torvalds 			--fs->wanted;
852106fd892SArnd Bergmann 			spin_unlock_irqrestore(&swim3_lock, flags);
8531da177e4SLinus Torvalds 			return -EINTR;
8541da177e4SLinus Torvalds 		}
8551da177e4SLinus Torvalds 		--fs->wanted;
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds 	fs->state = state;
858b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
859b3025457SBenjamin Herrenschmidt 
8601da177e4SLinus Torvalds 	return 0;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs)
8641da177e4SLinus Torvalds {
8651da177e4SLinus Torvalds 	unsigned long flags;
8661da177e4SLinus Torvalds 
867b3025457SBenjamin Herrenschmidt 	swim3_dbg("%s", "-> release drive\n");
868b3025457SBenjamin Herrenschmidt 
869b3025457SBenjamin Herrenschmidt 	spin_lock_irqsave(&swim3_lock, flags);
8701da177e4SLinus Torvalds 	fs->state = idle;
8711da177e4SLinus Torvalds 	start_request(fs);
872b3025457SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&swim3_lock, flags);
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs)
8761da177e4SLinus Torvalds {
8771da177e4SLinus Torvalds 	int err, n;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	err = grab_drive(fs, ejecting, 1);
8801da177e4SLinus Torvalds 	if (err)
8811da177e4SLinus Torvalds 		return err;
8821da177e4SLinus Torvalds 	swim3_action(fs, EJECT);
8831da177e4SLinus Torvalds 	for (n = 20; n > 0; --n) {
8841da177e4SLinus Torvalds 		if (signal_pending(current)) {
8851da177e4SLinus Torvalds 			err = -EINTR;
8861da177e4SLinus Torvalds 			break;
8871da177e4SLinus Torvalds 		}
8881da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
88986e84862SNishanth Aravamudan 		schedule_timeout_interruptible(1);
8901da177e4SLinus Torvalds 		if (swim3_readbit(fs, DISK_IN) == 0)
8911da177e4SLinus Torvalds 			break;
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 	swim3_select(fs, RELAX);
8941da177e4SLinus Torvalds 	udelay(150);
8951da177e4SLinus Torvalds 	fs->ejected = 1;
8961da177e4SLinus Torvalds 	release_drive(fs);
8971da177e4SLinus Torvalds 	return err;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds static struct floppy_struct floppy_type =
9011da177e4SLinus Torvalds 	{ 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL };	/*  7 1.44MB 3.5"   */
9021da177e4SLinus Torvalds 
9038a6cfeb6SArnd Bergmann static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode,
9041da177e4SLinus Torvalds 			unsigned int cmd, unsigned long param)
9051da177e4SLinus Torvalds {
906b4d9a442SAl Viro 	struct floppy_state *fs = bdev->bd_disk->private_data;
9071da177e4SLinus Torvalds 	int err;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
9101da177e4SLinus Torvalds 		return -EPERM;
9111da177e4SLinus Torvalds 
912d58b0c39SBenjamin Herrenschmidt 	if (fs->mdev->media_bay &&
913d58b0c39SBenjamin Herrenschmidt 	    check_media_bay(fs->mdev->media_bay) != MB_FD)
9141da177e4SLinus Torvalds 		return -ENXIO;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	switch (cmd) {
9171da177e4SLinus Torvalds 	case FDEJECT:
9181da177e4SLinus Torvalds 		if (fs->ref_count != 1)
9191da177e4SLinus Torvalds 			return -EBUSY;
9201da177e4SLinus Torvalds 		err = fd_eject(fs);
9211da177e4SLinus Torvalds 		return err;
9221da177e4SLinus Torvalds 	case FDGETPRM:
9231da177e4SLinus Torvalds 	        if (copy_to_user((void __user *) param, &floppy_type,
9241da177e4SLinus Torvalds 				 sizeof(struct floppy_struct)))
9251da177e4SLinus Torvalds 			return -EFAULT;
9261da177e4SLinus Torvalds 		return 0;
9271da177e4SLinus Torvalds 	}
9281da177e4SLinus Torvalds 	return -ENOTTY;
9291da177e4SLinus Torvalds }
9301da177e4SLinus Torvalds 
9318a6cfeb6SArnd Bergmann static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
9328a6cfeb6SArnd Bergmann 				 unsigned int cmd, unsigned long param)
9338a6cfeb6SArnd Bergmann {
9348a6cfeb6SArnd Bergmann 	int ret;
9358a6cfeb6SArnd Bergmann 
9362a48fc0aSArnd Bergmann 	mutex_lock(&swim3_mutex);
9378a6cfeb6SArnd Bergmann 	ret = floppy_locked_ioctl(bdev, mode, cmd, param);
9382a48fc0aSArnd Bergmann 	mutex_unlock(&swim3_mutex);
9398a6cfeb6SArnd Bergmann 
9408a6cfeb6SArnd Bergmann 	return ret;
9418a6cfeb6SArnd Bergmann }
9428a6cfeb6SArnd Bergmann 
943b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode)
9441da177e4SLinus Torvalds {
945b4d9a442SAl Viro 	struct floppy_state *fs = bdev->bd_disk->private_data;
9461da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
9471da177e4SLinus Torvalds 	int n, err = 0;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	if (fs->ref_count == 0) {
950d58b0c39SBenjamin Herrenschmidt 		if (fs->mdev->media_bay &&
951d58b0c39SBenjamin Herrenschmidt 		    check_media_bay(fs->mdev->media_bay) != MB_FD)
9521da177e4SLinus Torvalds 			return -ENXIO;
9531da177e4SLinus Torvalds 		out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
9541da177e4SLinus Torvalds 		out_8(&sw->control_bic, 0xff);
9551da177e4SLinus Torvalds 		out_8(&sw->mode, 0x95);
9561da177e4SLinus Torvalds 		udelay(10);
9571da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
9581da177e4SLinus Torvalds 		out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
9591da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_ON);
9601da177e4SLinus Torvalds 		fs->write_prot = -1;
9611da177e4SLinus Torvalds 		fs->cur_cyl = -1;
9621da177e4SLinus Torvalds 		for (n = 0; n < 2 * HZ; ++n) {
9631da177e4SLinus Torvalds 			if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE))
9641da177e4SLinus Torvalds 				break;
9651da177e4SLinus Torvalds 			if (signal_pending(current)) {
9661da177e4SLinus Torvalds 				err = -EINTR;
9671da177e4SLinus Torvalds 				break;
9681da177e4SLinus Torvalds 			}
9691da177e4SLinus Torvalds 			swim3_select(fs, RELAX);
97086e84862SNishanth Aravamudan 			schedule_timeout_interruptible(1);
9711da177e4SLinus Torvalds 		}
9721da177e4SLinus Torvalds 		if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
9731da177e4SLinus Torvalds 				 || swim3_readbit(fs, DISK_IN) == 0))
9741da177e4SLinus Torvalds 			err = -ENXIO;
9751da177e4SLinus Torvalds 		swim3_action(fs, SETMFM);
9761da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
9771da177e4SLinus Torvalds 
978b4d9a442SAl Viro 	} else if (fs->ref_count == -1 || mode & FMODE_EXCL)
9791da177e4SLinus Torvalds 		return -EBUSY;
9801da177e4SLinus Torvalds 
981b4d9a442SAl Viro 	if (err == 0 && (mode & FMODE_NDELAY) == 0
982b4d9a442SAl Viro 	    && (mode & (FMODE_READ|FMODE_WRITE))) {
983b4d9a442SAl Viro 		check_disk_change(bdev);
9841da177e4SLinus Torvalds 		if (fs->ejected)
9851da177e4SLinus Torvalds 			err = -ENXIO;
9861da177e4SLinus Torvalds 	}
9871da177e4SLinus Torvalds 
988b4d9a442SAl Viro 	if (err == 0 && (mode & FMODE_WRITE)) {
9891da177e4SLinus Torvalds 		if (fs->write_prot < 0)
9901da177e4SLinus Torvalds 			fs->write_prot = swim3_readbit(fs, WRITE_PROT);
9911da177e4SLinus Torvalds 		if (fs->write_prot)
9921da177e4SLinus Torvalds 			err = -EROFS;
9931da177e4SLinus Torvalds 	}
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	if (err) {
9961da177e4SLinus Torvalds 		if (fs->ref_count == 0) {
9971da177e4SLinus Torvalds 			swim3_action(fs, MOTOR_OFF);
9981da177e4SLinus Torvalds 			out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
9991da177e4SLinus Torvalds 			swim3_select(fs, RELAX);
10001da177e4SLinus Torvalds 		}
10011da177e4SLinus Torvalds 		return err;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
1004b4d9a442SAl Viro 	if (mode & FMODE_EXCL)
10051da177e4SLinus Torvalds 		fs->ref_count = -1;
10061da177e4SLinus Torvalds 	else
10071da177e4SLinus Torvalds 		++fs->ref_count;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	return 0;
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
10126e9624b8SArnd Bergmann static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
10136e9624b8SArnd Bergmann {
10146e9624b8SArnd Bergmann 	int ret;
10156e9624b8SArnd Bergmann 
10162a48fc0aSArnd Bergmann 	mutex_lock(&swim3_mutex);
10176e9624b8SArnd Bergmann 	ret = floppy_open(bdev, mode);
10182a48fc0aSArnd Bergmann 	mutex_unlock(&swim3_mutex);
10196e9624b8SArnd Bergmann 
10206e9624b8SArnd Bergmann 	return ret;
10216e9624b8SArnd Bergmann }
10226e9624b8SArnd Bergmann 
1023db2a144bSAl Viro static void floppy_release(struct gendisk *disk, fmode_t mode)
10241da177e4SLinus Torvalds {
1025b4d9a442SAl Viro 	struct floppy_state *fs = disk->private_data;
10261da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
1027b3025457SBenjamin Herrenschmidt 
10282a48fc0aSArnd Bergmann 	mutex_lock(&swim3_mutex);
10291da177e4SLinus Torvalds 	if (fs->ref_count > 0 && --fs->ref_count == 0) {
10301da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_OFF);
10311da177e4SLinus Torvalds 		out_8(&sw->control_bic, 0xff);
10321da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
10331da177e4SLinus Torvalds 	}
10342a48fc0aSArnd Bergmann 	mutex_unlock(&swim3_mutex);
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
10374bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk,
10384bbde777STejun Heo 					unsigned int clearing)
10391da177e4SLinus Torvalds {
10401da177e4SLinus Torvalds 	struct floppy_state *fs = disk->private_data;
10414bbde777STejun Heo 	return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	struct floppy_state *fs = disk->private_data;
10471da177e4SLinus Torvalds 	struct swim3 __iomem *sw;
10481da177e4SLinus Torvalds 	int ret, n;
10491da177e4SLinus Torvalds 
1050d58b0c39SBenjamin Herrenschmidt 	if (fs->mdev->media_bay &&
1051d58b0c39SBenjamin Herrenschmidt 	    check_media_bay(fs->mdev->media_bay) != MB_FD)
10521da177e4SLinus Torvalds 		return -ENXIO;
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	sw = fs->swim3;
10551da177e4SLinus Torvalds 	grab_drive(fs, revalidating, 0);
10561da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
10571da177e4SLinus Torvalds 	out_8(&sw->control_bis, DRIVE_ENABLE);
10581da177e4SLinus Torvalds 	swim3_action(fs, MOTOR_ON);	/* necessary? */
10591da177e4SLinus Torvalds 	fs->write_prot = -1;
10601da177e4SLinus Torvalds 	fs->cur_cyl = -1;
10611da177e4SLinus Torvalds 	mdelay(1);
10621da177e4SLinus Torvalds 	for (n = HZ; n > 0; --n) {
10631da177e4SLinus Torvalds 		if (swim3_readbit(fs, SEEK_COMPLETE))
10641da177e4SLinus Torvalds 			break;
10651da177e4SLinus Torvalds 		if (signal_pending(current))
10661da177e4SLinus Torvalds 			break;
10671da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
106886e84862SNishanth Aravamudan 		schedule_timeout_interruptible(1);
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 	ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
10711da177e4SLinus Torvalds 		|| swim3_readbit(fs, DISK_IN) == 0;
10721da177e4SLinus Torvalds 	if (ret)
10731da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_OFF);
10741da177e4SLinus Torvalds 	else {
10751da177e4SLinus Torvalds 		fs->ejected = 0;
10761da177e4SLinus Torvalds 		swim3_action(fs, SETMFM);
10771da177e4SLinus Torvalds 	}
10781da177e4SLinus Torvalds 	swim3_select(fs, RELAX);
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	release_drive(fs);
10811da177e4SLinus Torvalds 	return ret;
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
108483d5cde4SAlexey Dobriyan static const struct block_device_operations floppy_fops = {
10856e9624b8SArnd Bergmann 	.open		= floppy_unlocked_open,
1086b4d9a442SAl Viro 	.release	= floppy_release,
10878a6cfeb6SArnd Bergmann 	.ioctl		= floppy_ioctl,
10884bbde777STejun Heo 	.check_events	= floppy_check_events,
10891da177e4SLinus Torvalds 	.revalidate_disk= floppy_revalidate,
10901da177e4SLinus Torvalds };
10911da177e4SLinus Torvalds 
1092b3025457SBenjamin Herrenschmidt static void swim3_mb_event(struct macio_dev* mdev, int mb_state)
1093b3025457SBenjamin Herrenschmidt {
1094b3025457SBenjamin Herrenschmidt 	struct floppy_state *fs = macio_get_drvdata(mdev);
10957414d4f6SCong Ding 	struct swim3 __iomem *sw;
1096b3025457SBenjamin Herrenschmidt 
1097b3025457SBenjamin Herrenschmidt 	if (!fs)
1098b3025457SBenjamin Herrenschmidt 		return;
10997414d4f6SCong Ding 
11007414d4f6SCong Ding 	sw = fs->swim3;
11017414d4f6SCong Ding 
1102b3025457SBenjamin Herrenschmidt 	if (mb_state != MB_FD)
1103b3025457SBenjamin Herrenschmidt 		return;
1104b3025457SBenjamin Herrenschmidt 
1105b3025457SBenjamin Herrenschmidt 	/* Clear state */
1106b3025457SBenjamin Herrenschmidt 	out_8(&sw->intr_enable, 0);
1107b3025457SBenjamin Herrenschmidt 	in_8(&sw->intr);
1108b3025457SBenjamin Herrenschmidt 	in_8(&sw->error);
1109b3025457SBenjamin Herrenschmidt }
1110b3025457SBenjamin Herrenschmidt 
11113e9a6927SBenjamin Herrenschmidt static int swim3_add_device(struct macio_dev *mdev, int index)
11121da177e4SLinus Torvalds {
111361c7a080SGrant Likely 	struct device_node *swim = mdev->ofdev.dev.of_node;
11143e9a6927SBenjamin Herrenschmidt 	struct floppy_state *fs = &floppy_states[index];
11153e9a6927SBenjamin Herrenschmidt 	int rc = -EBUSY;
11161da177e4SLinus Torvalds 
1117b3025457SBenjamin Herrenschmidt 	/* Do this first for message macros */
1118b3025457SBenjamin Herrenschmidt 	memset(fs, 0, sizeof(*fs));
1119b3025457SBenjamin Herrenschmidt 	fs->mdev = mdev;
1120b3025457SBenjamin Herrenschmidt 	fs->index = index;
1121b3025457SBenjamin Herrenschmidt 
11223e9a6927SBenjamin Herrenschmidt 	/* Check & Request resources */
11233e9a6927SBenjamin Herrenschmidt 	if (macio_resource_count(mdev) < 2) {
1124b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "No address in device-tree\n");
11253e9a6927SBenjamin Herrenschmidt 		return -ENXIO;
1126cc5d0189SBenjamin Herrenschmidt 	}
1127b3025457SBenjamin Herrenschmidt 	if (macio_irq_count(mdev) < 1) {
1128b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "No interrupt in device-tree\n");
1129b3025457SBenjamin Herrenschmidt 		return -ENXIO;
1130cc5d0189SBenjamin Herrenschmidt 	}
11313e9a6927SBenjamin Herrenschmidt 	if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
1132b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Can't request mmio resource\n");
11333e9a6927SBenjamin Herrenschmidt 		return -EBUSY;
11341da177e4SLinus Torvalds 	}
11353e9a6927SBenjamin Herrenschmidt 	if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
1136b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Can't request dma resource\n");
11373e9a6927SBenjamin Herrenschmidt 		macio_release_resource(mdev, 0);
11383e9a6927SBenjamin Herrenschmidt 		return -EBUSY;
11393e9a6927SBenjamin Herrenschmidt 	}
11403e9a6927SBenjamin Herrenschmidt 	dev_set_drvdata(&mdev->ofdev.dev, fs);
11411da177e4SLinus Torvalds 
1142d58b0c39SBenjamin Herrenschmidt 	if (mdev->media_bay == NULL)
11431da177e4SLinus Torvalds 		pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	fs->state = idle;
11463e9a6927SBenjamin Herrenschmidt 	fs->swim3 = (struct swim3 __iomem *)
11473e9a6927SBenjamin Herrenschmidt 		ioremap(macio_resource_start(mdev, 0), 0x200);
11483e9a6927SBenjamin Herrenschmidt 	if (fs->swim3 == NULL) {
1149b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Couldn't map mmio registers\n");
11503e9a6927SBenjamin Herrenschmidt 		rc = -ENOMEM;
11513e9a6927SBenjamin Herrenschmidt 		goto out_release;
11523e9a6927SBenjamin Herrenschmidt 	}
11533e9a6927SBenjamin Herrenschmidt 	fs->dma = (struct dbdma_regs __iomem *)
11543e9a6927SBenjamin Herrenschmidt 		ioremap(macio_resource_start(mdev, 1), 0x200);
11553e9a6927SBenjamin Herrenschmidt 	if (fs->dma == NULL) {
1156b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Couldn't map dma registers\n");
11573e9a6927SBenjamin Herrenschmidt 		iounmap(fs->swim3);
11583e9a6927SBenjamin Herrenschmidt 		rc = -ENOMEM;
11593e9a6927SBenjamin Herrenschmidt 		goto out_release;
11603e9a6927SBenjamin Herrenschmidt 	}
11613e9a6927SBenjamin Herrenschmidt 	fs->swim3_intr = macio_irq(mdev, 0);
1162a419aef8SJoe Perches 	fs->dma_intr = macio_irq(mdev, 1);
11631da177e4SLinus Torvalds 	fs->cur_cyl = -1;
11641da177e4SLinus Torvalds 	fs->cur_sector = -1;
11651da177e4SLinus Torvalds 	fs->secpercyl = 36;
11661da177e4SLinus Torvalds 	fs->secpertrack = 18;
11671da177e4SLinus Torvalds 	fs->total_secs = 2880;
11681da177e4SLinus Torvalds 	init_waitqueue_head(&fs->wait);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
11711da177e4SLinus Torvalds 	memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
1172f5718726SDavid Gibson 	fs->dma_cmd[1].command = cpu_to_le16(DBDMA_STOP);
11731da177e4SLinus Torvalds 
1174b3025457SBenjamin Herrenschmidt 	if (mdev->media_bay == NULL || check_media_bay(mdev->media_bay) == MB_FD)
1175b3025457SBenjamin Herrenschmidt 		swim3_mb_event(mdev, MB_FD);
1176b3025457SBenjamin Herrenschmidt 
11771da177e4SLinus Torvalds 	if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
1178b3025457SBenjamin Herrenschmidt 		swim3_err("%s", "Couldn't request interrupt\n");
11791da177e4SLinus Torvalds 		pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
11803e9a6927SBenjamin Herrenschmidt 		goto out_unmap;
11811da177e4SLinus Torvalds 		return -EBUSY;
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 
1184b5775a6bSKees Cook 	timer_setup(&fs->timeout, NULL, 0);
11851da177e4SLinus Torvalds 
1186b3025457SBenjamin Herrenschmidt 	swim3_info("SWIM3 floppy controller %s\n",
1187d58b0c39SBenjamin Herrenschmidt 		mdev->media_bay ? "in media bay" : "");
11881da177e4SLinus Torvalds 
11893e9a6927SBenjamin Herrenschmidt 	return 0;
11901da177e4SLinus Torvalds 
11913e9a6927SBenjamin Herrenschmidt  out_unmap:
11923e9a6927SBenjamin Herrenschmidt 	iounmap(fs->dma);
11933e9a6927SBenjamin Herrenschmidt 	iounmap(fs->swim3);
11943e9a6927SBenjamin Herrenschmidt 
11953e9a6927SBenjamin Herrenschmidt  out_release:
11963e9a6927SBenjamin Herrenschmidt 	macio_release_resource(mdev, 0);
11973e9a6927SBenjamin Herrenschmidt 	macio_release_resource(mdev, 1);
11983e9a6927SBenjamin Herrenschmidt 
11993e9a6927SBenjamin Herrenschmidt 	return rc;
12003e9a6927SBenjamin Herrenschmidt }
12013e9a6927SBenjamin Herrenschmidt 
12028d85fce7SGreg Kroah-Hartman static int swim3_attach(struct macio_dev *mdev,
12038d85fce7SGreg Kroah-Hartman 			const struct of_device_id *match)
12043e9a6927SBenjamin Herrenschmidt {
12053e9a6927SBenjamin Herrenschmidt 	struct gendisk *disk;
1206b3025457SBenjamin Herrenschmidt 	int index, rc;
1207b3025457SBenjamin Herrenschmidt 
1208b3025457SBenjamin Herrenschmidt 	index = floppy_count++;
1209b3025457SBenjamin Herrenschmidt 	if (index >= MAX_FLOPPIES)
1210b3025457SBenjamin Herrenschmidt 		return -ENXIO;
12113e9a6927SBenjamin Herrenschmidt 
12123e9a6927SBenjamin Herrenschmidt 	/* Add the drive */
1213b3025457SBenjamin Herrenschmidt 	rc = swim3_add_device(mdev, index);
12143e9a6927SBenjamin Herrenschmidt 	if (rc)
12153e9a6927SBenjamin Herrenschmidt 		return rc;
1216b3025457SBenjamin Herrenschmidt 	/* Now register that disk. Same comment about failure handling */
1217b3025457SBenjamin Herrenschmidt 	disk = disks[index] = alloc_disk(1);
1218b3025457SBenjamin Herrenschmidt 	if (disk == NULL)
1219b3025457SBenjamin Herrenschmidt 		return -ENOMEM;
1220b3025457SBenjamin Herrenschmidt 	disk->queue = blk_init_queue(do_fd_request, &swim3_lock);
1221b3025457SBenjamin Herrenschmidt 	if (disk->queue == NULL) {
1222b3025457SBenjamin Herrenschmidt 		put_disk(disk);
1223b3025457SBenjamin Herrenschmidt 		return -ENOMEM;
1224b3025457SBenjamin Herrenschmidt 	}
12258fc45044SChristoph Hellwig 	blk_queue_bounce_limit(disk->queue, BLK_BOUNCE_HIGH);
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 
1248cc3f2e9fSArvind Yadav static const 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