xref: /openbmc/linux/drivers/block/swim3.c (revision f41c53a569c4cf0556893ec9cfcf697d069799e1)
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 
191da177e4SLinus Torvalds #include <linux/stddef.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/sched.h>
221da177e4SLinus Torvalds #include <linux/timer.h>
231da177e4SLinus Torvalds #include <linux/delay.h>
241da177e4SLinus Torvalds #include <linux/fd.h>
251da177e4SLinus Torvalds #include <linux/ioctl.h>
261da177e4SLinus Torvalds #include <linux/blkdev.h>
271da177e4SLinus Torvalds #include <linux/interrupt.h>
282a48fc0aSArnd Bergmann #include <linux/mutex.h>
291da177e4SLinus Torvalds #include <linux/module.h>
30515729ecSPaul Mackerras #include <linux/spinlock.h>
311da177e4SLinus Torvalds #include <asm/io.h>
321da177e4SLinus Torvalds #include <asm/dbdma.h>
331da177e4SLinus Torvalds #include <asm/prom.h>
341da177e4SLinus Torvalds #include <asm/uaccess.h>
351da177e4SLinus Torvalds #include <asm/mediabay.h>
361da177e4SLinus Torvalds #include <asm/machdep.h>
371da177e4SLinus Torvalds #include <asm/pmac_feature.h>
381da177e4SLinus Torvalds 
392a48fc0aSArnd Bergmann static DEFINE_MUTEX(swim3_mutex);
401da177e4SLinus Torvalds static struct request_queue *swim3_queue;
411da177e4SLinus Torvalds static struct gendisk *disks[2];
421da177e4SLinus Torvalds static struct request *fd_req;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #define MAX_FLOPPIES	2
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds enum swim_state {
471da177e4SLinus Torvalds 	idle,
481da177e4SLinus Torvalds 	locating,
491da177e4SLinus Torvalds 	seeking,
501da177e4SLinus Torvalds 	settling,
511da177e4SLinus Torvalds 	do_transfer,
521da177e4SLinus Torvalds 	jogging,
531da177e4SLinus Torvalds 	available,
541da177e4SLinus Torvalds 	revalidating,
551da177e4SLinus Torvalds 	ejecting
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #define REG(x)	unsigned char x; char x ## _pad[15];
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /*
611da177e4SLinus Torvalds  * The names for these registers mostly represent speculation on my part.
621da177e4SLinus Torvalds  * It will be interesting to see how close they are to the names Apple uses.
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds struct swim3 {
651da177e4SLinus Torvalds 	REG(data);
661da177e4SLinus Torvalds 	REG(timer);		/* counts down at 1MHz */
671da177e4SLinus Torvalds 	REG(error);
681da177e4SLinus Torvalds 	REG(mode);
691da177e4SLinus Torvalds 	REG(select);		/* controls CA0, CA1, CA2 and LSTRB signals */
701da177e4SLinus Torvalds 	REG(setup);
711da177e4SLinus Torvalds 	REG(control);		/* writing bits clears them */
721da177e4SLinus Torvalds 	REG(status);		/* writing bits sets them in control */
731da177e4SLinus Torvalds 	REG(intr);
741da177e4SLinus Torvalds 	REG(nseek);		/* # tracks to seek */
751da177e4SLinus Torvalds 	REG(ctrack);		/* current track number */
761da177e4SLinus Torvalds 	REG(csect);		/* current sector number */
771da177e4SLinus Torvalds 	REG(gap3);		/* size of gap 3 in track format */
781da177e4SLinus Torvalds 	REG(sector);		/* sector # to read or write */
791da177e4SLinus Torvalds 	REG(nsect);		/* # sectors to read or write */
801da177e4SLinus Torvalds 	REG(intr_enable);
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #define control_bic	control
841da177e4SLinus Torvalds #define control_bis	status
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /* Bits in select register */
871da177e4SLinus Torvalds #define CA_MASK		7
881da177e4SLinus Torvalds #define LSTRB		8
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* Bits in control register */
911da177e4SLinus Torvalds #define DO_SEEK		0x80
921da177e4SLinus Torvalds #define FORMAT		0x40
931da177e4SLinus Torvalds #define SELECT		0x20
941da177e4SLinus Torvalds #define WRITE_SECTORS	0x10
951da177e4SLinus Torvalds #define DO_ACTION	0x08
961da177e4SLinus Torvalds #define DRIVE2_ENABLE	0x04
971da177e4SLinus Torvalds #define DRIVE_ENABLE	0x02
981da177e4SLinus Torvalds #define INTR_ENABLE	0x01
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /* Bits in status register */
1011da177e4SLinus Torvalds #define FIFO_1BYTE	0x80
1021da177e4SLinus Torvalds #define FIFO_2BYTE	0x40
1031da177e4SLinus Torvalds #define ERROR		0x20
1041da177e4SLinus Torvalds #define DATA		0x08
1051da177e4SLinus Torvalds #define RDDATA		0x04
1061da177e4SLinus Torvalds #define INTR_PENDING	0x02
1071da177e4SLinus Torvalds #define MARK_BYTE	0x01
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /* Bits in intr and intr_enable registers */
1101da177e4SLinus Torvalds #define ERROR_INTR	0x20
1111da177e4SLinus Torvalds #define DATA_CHANGED	0x10
1121da177e4SLinus Torvalds #define TRANSFER_DONE	0x08
1131da177e4SLinus Torvalds #define SEEN_SECTOR	0x04
1141da177e4SLinus Torvalds #define SEEK_DONE	0x02
1151da177e4SLinus Torvalds #define TIMER_DONE	0x01
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* Bits in error register */
1181da177e4SLinus Torvalds #define ERR_DATA_CRC	0x80
1191da177e4SLinus Torvalds #define ERR_ADDR_CRC	0x40
1201da177e4SLinus Torvalds #define ERR_OVERRUN	0x04
1211da177e4SLinus Torvalds #define ERR_UNDERRUN	0x01
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /* Bits in setup register */
1241da177e4SLinus Torvalds #define S_SW_RESET	0x80
1251da177e4SLinus Torvalds #define S_GCR_WRITE	0x40
1261da177e4SLinus Torvalds #define S_IBM_DRIVE	0x20
1271da177e4SLinus Torvalds #define S_TEST_MODE	0x10
1281da177e4SLinus Torvalds #define S_FCLK_DIV2	0x08
1291da177e4SLinus Torvalds #define S_GCR		0x04
1301da177e4SLinus Torvalds #define S_COPY_PROT	0x02
1311da177e4SLinus Torvalds #define S_INV_WDATA	0x01
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /* Select values for swim3_action */
1341da177e4SLinus Torvalds #define SEEK_POSITIVE	0
1351da177e4SLinus Torvalds #define SEEK_NEGATIVE	4
1361da177e4SLinus Torvalds #define STEP		1
1371da177e4SLinus Torvalds #define MOTOR_ON	2
1381da177e4SLinus Torvalds #define MOTOR_OFF	6
1391da177e4SLinus Torvalds #define INDEX		3
1401da177e4SLinus Torvalds #define EJECT		7
1411da177e4SLinus Torvalds #define SETMFM		9
1421da177e4SLinus Torvalds #define SETGCR		13
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /* Select values for swim3_select and swim3_readbit */
1451da177e4SLinus Torvalds #define STEP_DIR	0
1461da177e4SLinus Torvalds #define STEPPING	1
1471da177e4SLinus Torvalds #define MOTOR_ON	2
1481da177e4SLinus Torvalds #define RELAX		3	/* also eject in progress */
1491da177e4SLinus Torvalds #define READ_DATA_0	4
1501da177e4SLinus Torvalds #define TWOMEG_DRIVE	5
1511da177e4SLinus Torvalds #define SINGLE_SIDED	6	/* drive or diskette is 4MB type? */
1521da177e4SLinus Torvalds #define DRIVE_PRESENT	7
1531da177e4SLinus Torvalds #define DISK_IN		8
1541da177e4SLinus Torvalds #define WRITE_PROT	9
1551da177e4SLinus Torvalds #define TRACK_ZERO	10
1561da177e4SLinus Torvalds #define TACHO		11
1571da177e4SLinus Torvalds #define READ_DATA_1	12
1581da177e4SLinus Torvalds #define MFM_MODE	13
1591da177e4SLinus Torvalds #define SEEK_COMPLETE	14
1601da177e4SLinus Torvalds #define ONEMEG_MEDIA	15
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /* Definitions of values used in writing and formatting */
1631da177e4SLinus Torvalds #define DATA_ESCAPE	0x99
1641da177e4SLinus Torvalds #define GCR_SYNC_EXC	0x3f
1651da177e4SLinus Torvalds #define GCR_SYNC_CONV	0x80
1661da177e4SLinus Torvalds #define GCR_FIRST_MARK	0xd5
1671da177e4SLinus Torvalds #define GCR_SECOND_MARK	0xaa
1681da177e4SLinus Torvalds #define GCR_ADDR_MARK	"\xd5\xaa\x00"
1691da177e4SLinus Torvalds #define GCR_DATA_MARK	"\xd5\xaa\x0b"
1701da177e4SLinus Torvalds #define GCR_SLIP_BYTE	"\x27\xaa"
1711da177e4SLinus Torvalds #define GCR_SELF_SYNC	"\x3f\xbf\x1e\x34\x3c\x3f"
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds #define DATA_99		"\x99\x99"
1741da177e4SLinus Torvalds #define MFM_ADDR_MARK	"\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
1751da177e4SLinus Torvalds #define MFM_INDEX_MARK	"\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
1761da177e4SLinus Torvalds #define MFM_GAP_LEN	12
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds struct floppy_state {
1791da177e4SLinus Torvalds 	enum swim_state	state;
180515729ecSPaul Mackerras 	spinlock_t lock;
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)];
2071da177e4SLinus Torvalds };
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds static struct floppy_state floppy_states[MAX_FLOPPIES];
2101da177e4SLinus Torvalds static int floppy_count = 0;
2111da177e4SLinus Torvalds static DEFINE_SPINLOCK(swim3_lock);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static unsigned short write_preamble[] = {
2141da177e4SLinus Torvalds 	0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e,	/* gap field */
2151da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0,			/* sync field */
2161da177e4SLinus Torvalds 	0x99a1, 0x99a1, 0x99a1, 0x99fb,		/* data address mark */
2171da177e4SLinus Torvalds 	0x990f					/* no escape for 512 bytes */
2181da177e4SLinus Torvalds };
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static unsigned short write_postamble[] = {
2211da177e4SLinus Torvalds 	0x9904,					/* insert CRC */
2221da177e4SLinus Torvalds 	0x4e4e, 0x4e4e,
2231da177e4SLinus Torvalds 	0x9908,					/* stop writing */
2241da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0
2251da177e4SLinus Torvalds };
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds static void swim3_select(struct floppy_state *fs, int sel);
2281da177e4SLinus Torvalds static void swim3_action(struct floppy_state *fs, int action);
2291da177e4SLinus Torvalds static int swim3_readbit(struct floppy_state *fs, int bit);
230165125e1SJens Axboe static void do_fd_request(struct request_queue * q);
2311da177e4SLinus Torvalds static void start_request(struct floppy_state *fs);
2321da177e4SLinus Torvalds static void set_timeout(struct floppy_state *fs, int nticks,
2331da177e4SLinus Torvalds 			void (*proc)(unsigned long));
2341da177e4SLinus Torvalds static void scan_track(struct floppy_state *fs);
2351da177e4SLinus Torvalds static void seek_track(struct floppy_state *fs, int n);
2361da177e4SLinus Torvalds static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
2371da177e4SLinus Torvalds static void setup_transfer(struct floppy_state *fs);
2381da177e4SLinus Torvalds static void act(struct floppy_state *fs);
2391da177e4SLinus Torvalds static void scan_timeout(unsigned long data);
2401da177e4SLinus Torvalds static void seek_timeout(unsigned long data);
2411da177e4SLinus Torvalds static void settle_timeout(unsigned long data);
2421da177e4SLinus Torvalds static void xfer_timeout(unsigned long data);
2437d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id);
2447d12e780SDavid Howells /*static void fd_dma_interrupt(int irq, void *dev_id);*/
2451da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state,
2461da177e4SLinus Torvalds 		      int interruptible);
2471da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs);
2481da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs);
249b4d9a442SAl Viro static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
2501da177e4SLinus Torvalds 			unsigned int cmd, unsigned long param);
251b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode);
252b4d9a442SAl Viro static int floppy_release(struct gendisk *disk, fmode_t mode);
2534bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk,
2544bbde777STejun Heo 					unsigned int clearing);
2551da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk);
2561da177e4SLinus Torvalds 
257f4bd4b90STejun Heo static bool swim3_end_request(int err, unsigned int nr_bytes)
258f4bd4b90STejun Heo {
259f4bd4b90STejun Heo 	if (__blk_end_request(fd_req, err, nr_bytes))
260f4bd4b90STejun Heo 		return true;
261f4bd4b90STejun Heo 
262f4bd4b90STejun Heo 	fd_req = NULL;
263f4bd4b90STejun Heo 	return false;
264f4bd4b90STejun Heo }
265f4bd4b90STejun Heo 
266f4bd4b90STejun Heo static bool swim3_end_request_cur(int err)
267f4bd4b90STejun Heo {
268f4bd4b90STejun Heo 	return swim3_end_request(err, blk_rq_cur_bytes(fd_req));
269f4bd4b90STejun Heo }
270f4bd4b90STejun Heo 
2711da177e4SLinus Torvalds static void swim3_select(struct floppy_state *fs, int sel)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
2761da177e4SLinus Torvalds 	if (sel & 8)
2771da177e4SLinus Torvalds 		out_8(&sw->control_bis, SELECT);
2781da177e4SLinus Torvalds 	else
2791da177e4SLinus Torvalds 		out_8(&sw->control_bic, SELECT);
2801da177e4SLinus Torvalds 	out_8(&sw->select, sel & CA_MASK);
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static void swim3_action(struct floppy_state *fs, int action)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	swim3_select(fs, action);
2881da177e4SLinus Torvalds 	udelay(1);
2891da177e4SLinus Torvalds 	out_8(&sw->select, sw->select | LSTRB);
2901da177e4SLinus Torvalds 	udelay(2);
2911da177e4SLinus Torvalds 	out_8(&sw->select, sw->select & ~LSTRB);
2921da177e4SLinus Torvalds 	udelay(1);
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static int swim3_readbit(struct floppy_state *fs, int bit)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
2981da177e4SLinus Torvalds 	int stat;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	swim3_select(fs, bit);
3011da177e4SLinus Torvalds 	udelay(1);
3021da177e4SLinus Torvalds 	stat = in_8(&sw->status);
3031da177e4SLinus Torvalds 	return (stat & DATA) == 0;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
306165125e1SJens Axboe static void do_fd_request(struct request_queue * q)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	int i;
309d58b0c39SBenjamin Herrenschmidt 
310d58b0c39SBenjamin Herrenschmidt 	for(i=0; i<floppy_count; i++) {
311d58b0c39SBenjamin Herrenschmidt 		struct floppy_state *fs = &floppy_states[i];
312d58b0c39SBenjamin Herrenschmidt 		if (fs->mdev->media_bay &&
313d58b0c39SBenjamin Herrenschmidt 		    check_media_bay(fs->mdev->media_bay) != MB_FD)
3141da177e4SLinus Torvalds 			continue;
315d58b0c39SBenjamin Herrenschmidt 		start_request(fs);
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds static void start_request(struct floppy_state *fs)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct request *req;
3221da177e4SLinus Torvalds 	unsigned long x;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	if (fs->state == idle && fs->wanted) {
3251da177e4SLinus Torvalds 		fs->state = available;
3261da177e4SLinus Torvalds 		wake_up(&fs->wait);
3271da177e4SLinus Torvalds 		return;
3281da177e4SLinus Torvalds 	}
329f4bd4b90STejun Heo 	while (fs->state == idle) {
330f4bd4b90STejun Heo 		if (!fd_req) {
3319934c8c0STejun Heo 			fd_req = blk_fetch_request(swim3_queue);
332f4bd4b90STejun Heo 			if (!fd_req)
333f4bd4b90STejun Heo 				break;
334f4bd4b90STejun Heo 		}
335f4bd4b90STejun Heo 		req = fd_req;
3361da177e4SLinus Torvalds #if 0
33783096ebfSTejun Heo 		printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%u buf=%p\n",
3381da177e4SLinus Torvalds 		       req->rq_disk->disk_name, req->cmd,
33983096ebfSTejun Heo 		       (long)blk_rq_pos(req), blk_rq_sectors(req), req->buffer);
34083096ebfSTejun Heo 		printk("           errors=%d current_nr_sectors=%u\n",
34183096ebfSTejun Heo 		       req->errors, blk_rq_cur_sectors(req));
3421da177e4SLinus Torvalds #endif
3431da177e4SLinus Torvalds 
34483096ebfSTejun Heo 		if (blk_rq_pos(req) >= fs->total_secs) {
345f4bd4b90STejun Heo 			swim3_end_request_cur(-EIO);
3461da177e4SLinus Torvalds 			continue;
3471da177e4SLinus Torvalds 		}
3481da177e4SLinus Torvalds 		if (fs->ejected) {
349f4bd4b90STejun Heo 			swim3_end_request_cur(-EIO);
3501da177e4SLinus Torvalds 			continue;
3511da177e4SLinus Torvalds 		}
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 		if (rq_data_dir(req) == WRITE) {
3541da177e4SLinus Torvalds 			if (fs->write_prot < 0)
3551da177e4SLinus Torvalds 				fs->write_prot = swim3_readbit(fs, WRITE_PROT);
3561da177e4SLinus Torvalds 			if (fs->write_prot) {
357f4bd4b90STejun Heo 				swim3_end_request_cur(-EIO);
3581da177e4SLinus Torvalds 				continue;
3591da177e4SLinus Torvalds 			}
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 
36283096ebfSTejun Heo 		/* Do not remove the cast. blk_rq_pos(req) is now a
36383096ebfSTejun Heo 		 * sector_t and can be 64 bits, but it will never go
36483096ebfSTejun Heo 		 * past 32 bits for this driver anyway, so we can
36583096ebfSTejun Heo 		 * safely cast it down and not have to do a 64/32
36683096ebfSTejun Heo 		 * division
3671da177e4SLinus Torvalds 		 */
36883096ebfSTejun Heo 		fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl;
36983096ebfSTejun Heo 		x = ((long)blk_rq_pos(req)) % fs->secpercyl;
3701da177e4SLinus Torvalds 		fs->head = x / fs->secpertrack;
3711da177e4SLinus Torvalds 		fs->req_sector = x % fs->secpertrack + 1;
3721da177e4SLinus Torvalds 		fd_req = req;
3731da177e4SLinus Torvalds 		fs->state = do_transfer;
3741da177e4SLinus Torvalds 		fs->retries = 0;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 		act(fs);
3771da177e4SLinus Torvalds 	}
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds static void set_timeout(struct floppy_state *fs, int nticks,
3811da177e4SLinus Torvalds 			void (*proc)(unsigned long))
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	unsigned long flags;
3841da177e4SLinus Torvalds 
385515729ecSPaul Mackerras 	spin_lock_irqsave(&fs->lock, flags);
3861da177e4SLinus Torvalds 	if (fs->timeout_pending)
3871da177e4SLinus Torvalds 		del_timer(&fs->timeout);
3881da177e4SLinus Torvalds 	fs->timeout.expires = jiffies + nticks;
3891da177e4SLinus Torvalds 	fs->timeout.function = proc;
3901da177e4SLinus Torvalds 	fs->timeout.data = (unsigned long) fs;
3911da177e4SLinus Torvalds 	add_timer(&fs->timeout);
3921da177e4SLinus Torvalds 	fs->timeout_pending = 1;
393515729ecSPaul Mackerras 	spin_unlock_irqrestore(&fs->lock, flags);
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds static inline void scan_track(struct floppy_state *fs)
3971da177e4SLinus Torvalds {
3981da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	swim3_select(fs, READ_DATA_0);
4011da177e4SLinus Torvalds 	in_8(&sw->intr);		/* clear SEEN_SECTOR bit */
4021da177e4SLinus Torvalds 	in_8(&sw->error);
4031da177e4SLinus Torvalds 	out_8(&sw->intr_enable, SEEN_SECTOR);
4041da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_ACTION);
4051da177e4SLinus Torvalds 	/* enable intr when track found */
4061da177e4SLinus Torvalds 	set_timeout(fs, HZ, scan_timeout);	/* enable timeout */
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds static inline void seek_track(struct floppy_state *fs, int n)
4101da177e4SLinus Torvalds {
4111da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	if (n >= 0) {
4141da177e4SLinus Torvalds 		swim3_action(fs, SEEK_POSITIVE);
4151da177e4SLinus Torvalds 		sw->nseek = n;
4161da177e4SLinus Torvalds 	} else {
4171da177e4SLinus Torvalds 		swim3_action(fs, SEEK_NEGATIVE);
4181da177e4SLinus Torvalds 		sw->nseek = -n;
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 	fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1;
4211da177e4SLinus Torvalds 	swim3_select(fs, STEP);
4221da177e4SLinus Torvalds 	in_8(&sw->error);
4231da177e4SLinus Torvalds 	/* enable intr when seek finished */
4241da177e4SLinus Torvalds 	out_8(&sw->intr_enable, SEEK_DONE);
4251da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_SEEK);
4261da177e4SLinus Torvalds 	set_timeout(fs, 3*HZ, seek_timeout);	/* enable timeout */
4271da177e4SLinus Torvalds 	fs->settle_time = 0;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds static inline void init_dma(struct dbdma_cmd *cp, int cmd,
4311da177e4SLinus Torvalds 			    void *buf, int count)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	st_le16(&cp->req_count, count);
4341da177e4SLinus Torvalds 	st_le16(&cp->command, cmd);
4351da177e4SLinus Torvalds 	st_le32(&cp->phy_addr, virt_to_bus(buf));
4361da177e4SLinus Torvalds 	cp->xfer_status = 0;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds static inline void setup_transfer(struct floppy_state *fs)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	int n;
4421da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
4431da177e4SLinus Torvalds 	struct dbdma_cmd *cp = fs->dma_cmd;
4441da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr = fs->dma;
4451da177e4SLinus Torvalds 
44683096ebfSTejun Heo 	if (blk_rq_cur_sectors(fd_req) <= 0) {
4471da177e4SLinus Torvalds 		printk(KERN_ERR "swim3: transfer 0 sectors?\n");
4481da177e4SLinus Torvalds 		return;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 	if (rq_data_dir(fd_req) == WRITE)
4511da177e4SLinus Torvalds 		n = 1;
4521da177e4SLinus Torvalds 	else {
4531da177e4SLinus Torvalds 		n = fs->secpertrack - fs->req_sector + 1;
45483096ebfSTejun Heo 		if (n > blk_rq_cur_sectors(fd_req))
45583096ebfSTejun Heo 			n = blk_rq_cur_sectors(fd_req);
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds 	fs->scount = n;
4581da177e4SLinus Torvalds 	swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
4591da177e4SLinus Torvalds 	out_8(&sw->sector, fs->req_sector);
4601da177e4SLinus Torvalds 	out_8(&sw->nsect, n);
4611da177e4SLinus Torvalds 	out_8(&sw->gap3, 0);
4621da177e4SLinus Torvalds 	out_le32(&dr->cmdptr, virt_to_bus(cp));
4631da177e4SLinus Torvalds 	if (rq_data_dir(fd_req) == WRITE) {
4641da177e4SLinus Torvalds 		/* Set up 3 dma commands: write preamble, data, postamble */
4651da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
4661da177e4SLinus Torvalds 		++cp;
4671da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_MORE, fd_req->buffer, 512);
4681da177e4SLinus Torvalds 		++cp;
4691da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
4701da177e4SLinus Torvalds 	} else {
4711da177e4SLinus Torvalds 		init_dma(cp, INPUT_LAST, fd_req->buffer, n * 512);
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 	++cp;
4741da177e4SLinus Torvalds 	out_le16(&cp->command, DBDMA_STOP);
4751da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
4761da177e4SLinus Torvalds 	in_8(&sw->error);
4771da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
4781da177e4SLinus Torvalds 	if (rq_data_dir(fd_req) == WRITE)
4791da177e4SLinus Torvalds 		out_8(&sw->control_bis, WRITE_SECTORS);
4801da177e4SLinus Torvalds 	in_8(&sw->intr);
4811da177e4SLinus Torvalds 	out_le32(&dr->control, (RUN << 16) | RUN);
4821da177e4SLinus Torvalds 	/* enable intr when transfer complete */
4831da177e4SLinus Torvalds 	out_8(&sw->intr_enable, TRANSFER_DONE);
4841da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_ACTION);
4851da177e4SLinus Torvalds 	set_timeout(fs, 2*HZ, xfer_timeout);	/* enable timeout */
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds static void act(struct floppy_state *fs)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	for (;;) {
4911da177e4SLinus Torvalds 		switch (fs->state) {
4921da177e4SLinus Torvalds 		case idle:
4931da177e4SLinus Torvalds 			return;		/* XXX shouldn't get here */
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 		case locating:
4961da177e4SLinus Torvalds 			if (swim3_readbit(fs, TRACK_ZERO)) {
4971da177e4SLinus Torvalds 				fs->cur_cyl = 0;
4981da177e4SLinus Torvalds 				if (fs->req_cyl == 0)
4991da177e4SLinus Torvalds 					fs->state = do_transfer;
5001da177e4SLinus Torvalds 				else
5011da177e4SLinus Torvalds 					fs->state = seeking;
5021da177e4SLinus Torvalds 				break;
5031da177e4SLinus Torvalds 			}
5041da177e4SLinus Torvalds 			scan_track(fs);
5051da177e4SLinus Torvalds 			return;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 		case seeking:
5081da177e4SLinus Torvalds 			if (fs->cur_cyl < 0) {
5091da177e4SLinus Torvalds 				fs->expect_cyl = -1;
5101da177e4SLinus Torvalds 				fs->state = locating;
5111da177e4SLinus Torvalds 				break;
5121da177e4SLinus Torvalds 			}
5131da177e4SLinus Torvalds 			if (fs->req_cyl == fs->cur_cyl) {
5141da177e4SLinus Torvalds 				printk("whoops, seeking 0\n");
5151da177e4SLinus Torvalds 				fs->state = do_transfer;
5161da177e4SLinus Torvalds 				break;
5171da177e4SLinus Torvalds 			}
5181da177e4SLinus Torvalds 			seek_track(fs, fs->req_cyl - fs->cur_cyl);
5191da177e4SLinus Torvalds 			return;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 		case settling:
5221da177e4SLinus Torvalds 			/* check for SEEK_COMPLETE after 30ms */
5231da177e4SLinus Torvalds 			fs->settle_time = (HZ + 32) / 33;
5241da177e4SLinus Torvalds 			set_timeout(fs, fs->settle_time, settle_timeout);
5251da177e4SLinus Torvalds 			return;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		case do_transfer:
5281da177e4SLinus Torvalds 			if (fs->cur_cyl != fs->req_cyl) {
5291da177e4SLinus Torvalds 				if (fs->retries > 5) {
530f4bd4b90STejun Heo 					swim3_end_request_cur(-EIO);
5311da177e4SLinus Torvalds 					fs->state = idle;
5321da177e4SLinus Torvalds 					return;
5331da177e4SLinus Torvalds 				}
5341da177e4SLinus Torvalds 				fs->state = seeking;
5351da177e4SLinus Torvalds 				break;
5361da177e4SLinus Torvalds 			}
5371da177e4SLinus Torvalds 			setup_transfer(fs);
5381da177e4SLinus Torvalds 			return;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 		case jogging:
5411da177e4SLinus Torvalds 			seek_track(fs, -5);
5421da177e4SLinus Torvalds 			return;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 		default:
5451da177e4SLinus Torvalds 			printk(KERN_ERR"swim3: unknown state %d\n", fs->state);
5461da177e4SLinus Torvalds 			return;
5471da177e4SLinus Torvalds 		}
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds static void scan_timeout(unsigned long data)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
5541da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	fs->timeout_pending = 0;
5571da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
5581da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
5591da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
5601da177e4SLinus Torvalds 	fs->cur_cyl = -1;
5611da177e4SLinus Torvalds 	if (fs->retries > 5) {
562f4bd4b90STejun Heo 		swim3_end_request_cur(-EIO);
5631da177e4SLinus Torvalds 		fs->state = idle;
5641da177e4SLinus Torvalds 		start_request(fs);
5651da177e4SLinus Torvalds 	} else {
5661da177e4SLinus Torvalds 		fs->state = jogging;
5671da177e4SLinus Torvalds 		act(fs);
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds static void seek_timeout(unsigned long data)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
5741da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	fs->timeout_pending = 0;
5771da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_SEEK);
5781da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
5791da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
5801da177e4SLinus Torvalds 	printk(KERN_ERR "swim3: seek timeout\n");
581f4bd4b90STejun Heo 	swim3_end_request_cur(-EIO);
5821da177e4SLinus Torvalds 	fs->state = idle;
5831da177e4SLinus Torvalds 	start_request(fs);
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds static void settle_timeout(unsigned long data)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
5891da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	fs->timeout_pending = 0;
5921da177e4SLinus Torvalds 	if (swim3_readbit(fs, SEEK_COMPLETE)) {
5931da177e4SLinus Torvalds 		out_8(&sw->select, RELAX);
5941da177e4SLinus Torvalds 		fs->state = locating;
5951da177e4SLinus Torvalds 		act(fs);
5961da177e4SLinus Torvalds 		return;
5971da177e4SLinus Torvalds 	}
5981da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
5991da177e4SLinus Torvalds 	if (fs->settle_time < 2*HZ) {
6001da177e4SLinus Torvalds 		++fs->settle_time;
6011da177e4SLinus Torvalds 		set_timeout(fs, 1, settle_timeout);
6021da177e4SLinus Torvalds 		return;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 	printk(KERN_ERR "swim3: seek settle timeout\n");
605f4bd4b90STejun Heo 	swim3_end_request_cur(-EIO);
6061da177e4SLinus Torvalds 	fs->state = idle;
6071da177e4SLinus Torvalds 	start_request(fs);
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds static void xfer_timeout(unsigned long data)
6111da177e4SLinus Torvalds {
6121da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
6131da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
6141da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr = fs->dma;
6151da177e4SLinus Torvalds 	int n;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	fs->timeout_pending = 0;
6181da177e4SLinus Torvalds 	out_le32(&dr->control, RUN << 16);
6191da177e4SLinus Torvalds 	/* We must wait a bit for dbdma to stop */
6201da177e4SLinus Torvalds 	for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
6211da177e4SLinus Torvalds 		udelay(1);
6221da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
6231da177e4SLinus Torvalds 	out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
6241da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
6251da177e4SLinus Torvalds 	printk(KERN_ERR "swim3: timeout %sing sector %ld\n",
62683096ebfSTejun Heo 	       (rq_data_dir(fd_req)==WRITE? "writ": "read"),
62783096ebfSTejun Heo 	       (long)blk_rq_pos(fd_req));
628f4bd4b90STejun Heo 	swim3_end_request_cur(-EIO);
6291da177e4SLinus Torvalds 	fs->state = idle;
6301da177e4SLinus Torvalds 	start_request(fs);
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6337d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) dev_id;
6361da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
6371da177e4SLinus Torvalds 	int intr, err, n;
6381da177e4SLinus Torvalds 	int stat, resid;
6391da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr;
6401da177e4SLinus Torvalds 	struct dbdma_cmd *cp;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	intr = in_8(&sw->intr);
6431da177e4SLinus Torvalds 	err = (intr & ERROR_INTR)? in_8(&sw->error): 0;
6441da177e4SLinus Torvalds 	if ((intr & ERROR_INTR) && fs->state != do_transfer)
64514b1ffb5SOlaf Hering 		printk(KERN_ERR "swim3_interrupt, state=%d, dir=%x, intr=%x, err=%x\n",
6461da177e4SLinus Torvalds 		       fs->state, rq_data_dir(fd_req), intr, err);
6471da177e4SLinus Torvalds 	switch (fs->state) {
6481da177e4SLinus Torvalds 	case locating:
6491da177e4SLinus Torvalds 		if (intr & SEEN_SECTOR) {
6501da177e4SLinus Torvalds 			out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
6511da177e4SLinus Torvalds 			out_8(&sw->select, RELAX);
6521da177e4SLinus Torvalds 			out_8(&sw->intr_enable, 0);
6531da177e4SLinus Torvalds 			del_timer(&fs->timeout);
6541da177e4SLinus Torvalds 			fs->timeout_pending = 0;
6551da177e4SLinus Torvalds 			if (sw->ctrack == 0xff) {
6561da177e4SLinus Torvalds 				printk(KERN_ERR "swim3: seen sector but cyl=ff?\n");
6571da177e4SLinus Torvalds 				fs->cur_cyl = -1;
6581da177e4SLinus Torvalds 				if (fs->retries > 5) {
659f4bd4b90STejun Heo 					swim3_end_request_cur(-EIO);
6601da177e4SLinus Torvalds 					fs->state = idle;
6611da177e4SLinus Torvalds 					start_request(fs);
6621da177e4SLinus Torvalds 				} else {
6631da177e4SLinus Torvalds 					fs->state = jogging;
6641da177e4SLinus Torvalds 					act(fs);
6651da177e4SLinus Torvalds 				}
6661da177e4SLinus Torvalds 				break;
6671da177e4SLinus Torvalds 			}
6681da177e4SLinus Torvalds 			fs->cur_cyl = sw->ctrack;
6691da177e4SLinus Torvalds 			fs->cur_sector = sw->csect;
6701da177e4SLinus Torvalds 			if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
6711da177e4SLinus Torvalds 				printk(KERN_ERR "swim3: expected cyl %d, got %d\n",
6721da177e4SLinus Torvalds 				       fs->expect_cyl, fs->cur_cyl);
6731da177e4SLinus Torvalds 			fs->state = do_transfer;
6741da177e4SLinus Torvalds 			act(fs);
6751da177e4SLinus Torvalds 		}
6761da177e4SLinus Torvalds 		break;
6771da177e4SLinus Torvalds 	case seeking:
6781da177e4SLinus Torvalds 	case jogging:
6791da177e4SLinus Torvalds 		if (sw->nseek == 0) {
6801da177e4SLinus Torvalds 			out_8(&sw->control_bic, DO_SEEK);
6811da177e4SLinus Torvalds 			out_8(&sw->select, RELAX);
6821da177e4SLinus Torvalds 			out_8(&sw->intr_enable, 0);
6831da177e4SLinus Torvalds 			del_timer(&fs->timeout);
6841da177e4SLinus Torvalds 			fs->timeout_pending = 0;
6851da177e4SLinus Torvalds 			if (fs->state == seeking)
6861da177e4SLinus Torvalds 				++fs->retries;
6871da177e4SLinus Torvalds 			fs->state = settling;
6881da177e4SLinus Torvalds 			act(fs);
6891da177e4SLinus Torvalds 		}
6901da177e4SLinus Torvalds 		break;
6911da177e4SLinus Torvalds 	case settling:
6921da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
6931da177e4SLinus Torvalds 		del_timer(&fs->timeout);
6941da177e4SLinus Torvalds 		fs->timeout_pending = 0;
6951da177e4SLinus Torvalds 		act(fs);
6961da177e4SLinus Torvalds 		break;
6971da177e4SLinus Torvalds 	case do_transfer:
6981da177e4SLinus Torvalds 		if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
6991da177e4SLinus Torvalds 			break;
7001da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
7011da177e4SLinus Torvalds 		out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
7021da177e4SLinus Torvalds 		out_8(&sw->select, RELAX);
7031da177e4SLinus Torvalds 		del_timer(&fs->timeout);
7041da177e4SLinus Torvalds 		fs->timeout_pending = 0;
7051da177e4SLinus Torvalds 		dr = fs->dma;
7061da177e4SLinus Torvalds 		cp = fs->dma_cmd;
7071da177e4SLinus Torvalds 		if (rq_data_dir(fd_req) == WRITE)
7081da177e4SLinus Torvalds 			++cp;
7091da177e4SLinus Torvalds 		/*
7101da177e4SLinus Torvalds 		 * Check that the main data transfer has finished.
7111da177e4SLinus Torvalds 		 * On writing, the swim3 sometimes doesn't use
7121da177e4SLinus Torvalds 		 * up all the bytes of the postamble, so we can still
7131da177e4SLinus Torvalds 		 * see DMA active here.  That doesn't matter as long
7141da177e4SLinus Torvalds 		 * as all the sector data has been transferred.
7151da177e4SLinus Torvalds 		 */
7161da177e4SLinus Torvalds 		if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) {
7171da177e4SLinus Torvalds 			/* wait a little while for DMA to complete */
7181da177e4SLinus Torvalds 			for (n = 0; n < 100; ++n) {
7191da177e4SLinus Torvalds 				if (cp->xfer_status != 0)
7201da177e4SLinus Torvalds 					break;
7211da177e4SLinus Torvalds 				udelay(1);
7221da177e4SLinus Torvalds 				barrier();
7231da177e4SLinus Torvalds 			}
7241da177e4SLinus Torvalds 		}
7251da177e4SLinus Torvalds 		/* turn off DMA */
7261da177e4SLinus Torvalds 		out_le32(&dr->control, (RUN | PAUSE) << 16);
7271da177e4SLinus Torvalds 		stat = ld_le16(&cp->xfer_status);
7281da177e4SLinus Torvalds 		resid = ld_le16(&cp->res_count);
7291da177e4SLinus Torvalds 		if (intr & ERROR_INTR) {
7301da177e4SLinus Torvalds 			n = fs->scount - 1 - resid / 512;
7311da177e4SLinus Torvalds 			if (n > 0) {
732467ca759STejun Heo 				blk_update_request(fd_req, 0, n << 9);
7331da177e4SLinus Torvalds 				fs->req_sector += n;
7341da177e4SLinus Torvalds 			}
7351da177e4SLinus Torvalds 			if (fs->retries < 5) {
7361da177e4SLinus Torvalds 				++fs->retries;
7371da177e4SLinus Torvalds 				act(fs);
7381da177e4SLinus Torvalds 			} else {
7391da177e4SLinus Torvalds 				printk("swim3: error %sing block %ld (err=%x)\n",
7401da177e4SLinus Torvalds 				       rq_data_dir(fd_req) == WRITE? "writ": "read",
74183096ebfSTejun Heo 				       (long)blk_rq_pos(fd_req), err);
742f4bd4b90STejun Heo 				swim3_end_request_cur(-EIO);
7431da177e4SLinus Torvalds 				fs->state = idle;
7441da177e4SLinus Torvalds 			}
7451da177e4SLinus Torvalds 		} else {
7461da177e4SLinus Torvalds 			if ((stat & ACTIVE) == 0 || resid != 0) {
7471da177e4SLinus Torvalds 				/* musta been an error */
7481da177e4SLinus Torvalds 				printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid);
74914b1ffb5SOlaf Hering 				printk(KERN_ERR "  state=%d, dir=%x, intr=%x, err=%x\n",
7501da177e4SLinus Torvalds 				       fs->state, rq_data_dir(fd_req), intr, err);
751f4bd4b90STejun Heo 				swim3_end_request_cur(-EIO);
7521da177e4SLinus Torvalds 				fs->state = idle;
7531da177e4SLinus Torvalds 				start_request(fs);
7541da177e4SLinus Torvalds 				break;
7551da177e4SLinus Torvalds 			}
756f4bd4b90STejun Heo 			if (swim3_end_request(0, fs->scount << 9)) {
7571da177e4SLinus Torvalds 				fs->req_sector += fs->scount;
7581da177e4SLinus Torvalds 				if (fs->req_sector > fs->secpertrack) {
7591da177e4SLinus Torvalds 					fs->req_sector -= fs->secpertrack;
7601da177e4SLinus Torvalds 					if (++fs->head > 1) {
7611da177e4SLinus Torvalds 						fs->head = 0;
7621da177e4SLinus Torvalds 						++fs->req_cyl;
7631da177e4SLinus Torvalds 					}
7641da177e4SLinus Torvalds 				}
7651da177e4SLinus Torvalds 				act(fs);
766467ca759STejun Heo 			} else
767467ca759STejun Heo 				fs->state = idle;
7681da177e4SLinus Torvalds 		}
7691da177e4SLinus Torvalds 		if (fs->state == idle)
7701da177e4SLinus Torvalds 			start_request(fs);
7711da177e4SLinus Torvalds 		break;
7721da177e4SLinus Torvalds 	default:
7731da177e4SLinus Torvalds 		printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state);
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 	return IRQ_HANDLED;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds /*
7797d12e780SDavid Howells static void fd_dma_interrupt(int irq, void *dev_id)
7801da177e4SLinus Torvalds {
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds */
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state,
7851da177e4SLinus Torvalds 		      int interruptible)
7861da177e4SLinus Torvalds {
7871da177e4SLinus Torvalds 	unsigned long flags;
7881da177e4SLinus Torvalds 
789515729ecSPaul Mackerras 	spin_lock_irqsave(&fs->lock, flags);
7901da177e4SLinus Torvalds 	if (fs->state != idle) {
7911da177e4SLinus Torvalds 		++fs->wanted;
7921da177e4SLinus Torvalds 		while (fs->state != available) {
7931da177e4SLinus Torvalds 			if (interruptible && signal_pending(current)) {
7941da177e4SLinus Torvalds 				--fs->wanted;
795515729ecSPaul Mackerras 				spin_unlock_irqrestore(&fs->lock, flags);
7961da177e4SLinus Torvalds 				return -EINTR;
7971da177e4SLinus Torvalds 			}
7981da177e4SLinus Torvalds 			interruptible_sleep_on(&fs->wait);
7991da177e4SLinus Torvalds 		}
8001da177e4SLinus Torvalds 		--fs->wanted;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 	fs->state = state;
803515729ecSPaul Mackerras 	spin_unlock_irqrestore(&fs->lock, flags);
8041da177e4SLinus Torvalds 	return 0;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs)
8081da177e4SLinus Torvalds {
8091da177e4SLinus Torvalds 	unsigned long flags;
8101da177e4SLinus Torvalds 
811515729ecSPaul Mackerras 	spin_lock_irqsave(&fs->lock, flags);
8121da177e4SLinus Torvalds 	fs->state = idle;
8131da177e4SLinus Torvalds 	start_request(fs);
814515729ecSPaul Mackerras 	spin_unlock_irqrestore(&fs->lock, flags);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	int err, n;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	err = grab_drive(fs, ejecting, 1);
8221da177e4SLinus Torvalds 	if (err)
8231da177e4SLinus Torvalds 		return err;
8241da177e4SLinus Torvalds 	swim3_action(fs, EJECT);
8251da177e4SLinus Torvalds 	for (n = 20; n > 0; --n) {
8261da177e4SLinus Torvalds 		if (signal_pending(current)) {
8271da177e4SLinus Torvalds 			err = -EINTR;
8281da177e4SLinus Torvalds 			break;
8291da177e4SLinus Torvalds 		}
8301da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
83186e84862SNishanth Aravamudan 		schedule_timeout_interruptible(1);
8321da177e4SLinus Torvalds 		if (swim3_readbit(fs, DISK_IN) == 0)
8331da177e4SLinus Torvalds 			break;
8341da177e4SLinus Torvalds 	}
8351da177e4SLinus Torvalds 	swim3_select(fs, RELAX);
8361da177e4SLinus Torvalds 	udelay(150);
8371da177e4SLinus Torvalds 	fs->ejected = 1;
8381da177e4SLinus Torvalds 	release_drive(fs);
8391da177e4SLinus Torvalds 	return err;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds static struct floppy_struct floppy_type =
8431da177e4SLinus Torvalds 	{ 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL };	/*  7 1.44MB 3.5"   */
8441da177e4SLinus Torvalds 
8458a6cfeb6SArnd Bergmann static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode,
8461da177e4SLinus Torvalds 			unsigned int cmd, unsigned long param)
8471da177e4SLinus Torvalds {
848b4d9a442SAl Viro 	struct floppy_state *fs = bdev->bd_disk->private_data;
8491da177e4SLinus Torvalds 	int err;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
8521da177e4SLinus Torvalds 		return -EPERM;
8531da177e4SLinus Torvalds 
854d58b0c39SBenjamin Herrenschmidt 	if (fs->mdev->media_bay &&
855d58b0c39SBenjamin Herrenschmidt 	    check_media_bay(fs->mdev->media_bay) != MB_FD)
8561da177e4SLinus Torvalds 		return -ENXIO;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	switch (cmd) {
8591da177e4SLinus Torvalds 	case FDEJECT:
8601da177e4SLinus Torvalds 		if (fs->ref_count != 1)
8611da177e4SLinus Torvalds 			return -EBUSY;
8621da177e4SLinus Torvalds 		err = fd_eject(fs);
8631da177e4SLinus Torvalds 		return err;
8641da177e4SLinus Torvalds 	case FDGETPRM:
8651da177e4SLinus Torvalds 	        if (copy_to_user((void __user *) param, &floppy_type,
8661da177e4SLinus Torvalds 				 sizeof(struct floppy_struct)))
8671da177e4SLinus Torvalds 			return -EFAULT;
8681da177e4SLinus Torvalds 		return 0;
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 	return -ENOTTY;
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
8738a6cfeb6SArnd Bergmann static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
8748a6cfeb6SArnd Bergmann 				 unsigned int cmd, unsigned long param)
8758a6cfeb6SArnd Bergmann {
8768a6cfeb6SArnd Bergmann 	int ret;
8778a6cfeb6SArnd Bergmann 
8782a48fc0aSArnd Bergmann 	mutex_lock(&swim3_mutex);
8798a6cfeb6SArnd Bergmann 	ret = floppy_locked_ioctl(bdev, mode, cmd, param);
8802a48fc0aSArnd Bergmann 	mutex_unlock(&swim3_mutex);
8818a6cfeb6SArnd Bergmann 
8828a6cfeb6SArnd Bergmann 	return ret;
8838a6cfeb6SArnd Bergmann }
8848a6cfeb6SArnd Bergmann 
885b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode)
8861da177e4SLinus Torvalds {
887b4d9a442SAl Viro 	struct floppy_state *fs = bdev->bd_disk->private_data;
8881da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
8891da177e4SLinus Torvalds 	int n, err = 0;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	if (fs->ref_count == 0) {
892d58b0c39SBenjamin Herrenschmidt 		if (fs->mdev->media_bay &&
893d58b0c39SBenjamin Herrenschmidt 		    check_media_bay(fs->mdev->media_bay) != MB_FD)
8941da177e4SLinus Torvalds 			return -ENXIO;
8951da177e4SLinus Torvalds 		out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
8961da177e4SLinus Torvalds 		out_8(&sw->control_bic, 0xff);
8971da177e4SLinus Torvalds 		out_8(&sw->mode, 0x95);
8981da177e4SLinus Torvalds 		udelay(10);
8991da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
9001da177e4SLinus Torvalds 		out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
9011da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_ON);
9021da177e4SLinus Torvalds 		fs->write_prot = -1;
9031da177e4SLinus Torvalds 		fs->cur_cyl = -1;
9041da177e4SLinus Torvalds 		for (n = 0; n < 2 * HZ; ++n) {
9051da177e4SLinus Torvalds 			if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE))
9061da177e4SLinus Torvalds 				break;
9071da177e4SLinus Torvalds 			if (signal_pending(current)) {
9081da177e4SLinus Torvalds 				err = -EINTR;
9091da177e4SLinus Torvalds 				break;
9101da177e4SLinus Torvalds 			}
9111da177e4SLinus Torvalds 			swim3_select(fs, RELAX);
91286e84862SNishanth Aravamudan 			schedule_timeout_interruptible(1);
9131da177e4SLinus Torvalds 		}
9141da177e4SLinus Torvalds 		if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
9151da177e4SLinus Torvalds 				 || swim3_readbit(fs, DISK_IN) == 0))
9161da177e4SLinus Torvalds 			err = -ENXIO;
9171da177e4SLinus Torvalds 		swim3_action(fs, SETMFM);
9181da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
9191da177e4SLinus Torvalds 
920b4d9a442SAl Viro 	} else if (fs->ref_count == -1 || mode & FMODE_EXCL)
9211da177e4SLinus Torvalds 		return -EBUSY;
9221da177e4SLinus Torvalds 
923b4d9a442SAl Viro 	if (err == 0 && (mode & FMODE_NDELAY) == 0
924b4d9a442SAl Viro 	    && (mode & (FMODE_READ|FMODE_WRITE))) {
925b4d9a442SAl Viro 		check_disk_change(bdev);
9261da177e4SLinus Torvalds 		if (fs->ejected)
9271da177e4SLinus Torvalds 			err = -ENXIO;
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 
930b4d9a442SAl Viro 	if (err == 0 && (mode & FMODE_WRITE)) {
9311da177e4SLinus Torvalds 		if (fs->write_prot < 0)
9321da177e4SLinus Torvalds 			fs->write_prot = swim3_readbit(fs, WRITE_PROT);
9331da177e4SLinus Torvalds 		if (fs->write_prot)
9341da177e4SLinus Torvalds 			err = -EROFS;
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	if (err) {
9381da177e4SLinus Torvalds 		if (fs->ref_count == 0) {
9391da177e4SLinus Torvalds 			swim3_action(fs, MOTOR_OFF);
9401da177e4SLinus Torvalds 			out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
9411da177e4SLinus Torvalds 			swim3_select(fs, RELAX);
9421da177e4SLinus Torvalds 		}
9431da177e4SLinus Torvalds 		return err;
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 
946b4d9a442SAl Viro 	if (mode & FMODE_EXCL)
9471da177e4SLinus Torvalds 		fs->ref_count = -1;
9481da177e4SLinus Torvalds 	else
9491da177e4SLinus Torvalds 		++fs->ref_count;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	return 0;
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds 
9546e9624b8SArnd Bergmann static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
9556e9624b8SArnd Bergmann {
9566e9624b8SArnd Bergmann 	int ret;
9576e9624b8SArnd Bergmann 
9582a48fc0aSArnd Bergmann 	mutex_lock(&swim3_mutex);
9596e9624b8SArnd Bergmann 	ret = floppy_open(bdev, mode);
9602a48fc0aSArnd Bergmann 	mutex_unlock(&swim3_mutex);
9616e9624b8SArnd Bergmann 
9626e9624b8SArnd Bergmann 	return ret;
9636e9624b8SArnd Bergmann }
9646e9624b8SArnd Bergmann 
965b4d9a442SAl Viro static int floppy_release(struct gendisk *disk, fmode_t mode)
9661da177e4SLinus Torvalds {
967b4d9a442SAl Viro 	struct floppy_state *fs = disk->private_data;
9681da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
9692a48fc0aSArnd Bergmann 	mutex_lock(&swim3_mutex);
9701da177e4SLinus Torvalds 	if (fs->ref_count > 0 && --fs->ref_count == 0) {
9711da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_OFF);
9721da177e4SLinus Torvalds 		out_8(&sw->control_bic, 0xff);
9731da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
9741da177e4SLinus Torvalds 	}
9752a48fc0aSArnd Bergmann 	mutex_unlock(&swim3_mutex);
9761da177e4SLinus Torvalds 	return 0;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9794bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk,
9804bbde777STejun Heo 					unsigned int clearing)
9811da177e4SLinus Torvalds {
9821da177e4SLinus Torvalds 	struct floppy_state *fs = disk->private_data;
9834bbde777STejun Heo 	return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	struct floppy_state *fs = disk->private_data;
9891da177e4SLinus Torvalds 	struct swim3 __iomem *sw;
9901da177e4SLinus Torvalds 	int ret, n;
9911da177e4SLinus Torvalds 
992d58b0c39SBenjamin Herrenschmidt 	if (fs->mdev->media_bay &&
993d58b0c39SBenjamin Herrenschmidt 	    check_media_bay(fs->mdev->media_bay) != MB_FD)
9941da177e4SLinus Torvalds 		return -ENXIO;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	sw = fs->swim3;
9971da177e4SLinus Torvalds 	grab_drive(fs, revalidating, 0);
9981da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
9991da177e4SLinus Torvalds 	out_8(&sw->control_bis, DRIVE_ENABLE);
10001da177e4SLinus Torvalds 	swim3_action(fs, MOTOR_ON);	/* necessary? */
10011da177e4SLinus Torvalds 	fs->write_prot = -1;
10021da177e4SLinus Torvalds 	fs->cur_cyl = -1;
10031da177e4SLinus Torvalds 	mdelay(1);
10041da177e4SLinus Torvalds 	for (n = HZ; n > 0; --n) {
10051da177e4SLinus Torvalds 		if (swim3_readbit(fs, SEEK_COMPLETE))
10061da177e4SLinus Torvalds 			break;
10071da177e4SLinus Torvalds 		if (signal_pending(current))
10081da177e4SLinus Torvalds 			break;
10091da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
101086e84862SNishanth Aravamudan 		schedule_timeout_interruptible(1);
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds 	ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
10131da177e4SLinus Torvalds 		|| swim3_readbit(fs, DISK_IN) == 0;
10141da177e4SLinus Torvalds 	if (ret)
10151da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_OFF);
10161da177e4SLinus Torvalds 	else {
10171da177e4SLinus Torvalds 		fs->ejected = 0;
10181da177e4SLinus Torvalds 		swim3_action(fs, SETMFM);
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds 	swim3_select(fs, RELAX);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	release_drive(fs);
10231da177e4SLinus Torvalds 	return ret;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
102683d5cde4SAlexey Dobriyan static const struct block_device_operations floppy_fops = {
10276e9624b8SArnd Bergmann 	.open		= floppy_unlocked_open,
1028b4d9a442SAl Viro 	.release	= floppy_release,
10298a6cfeb6SArnd Bergmann 	.ioctl		= floppy_ioctl,
10304bbde777STejun Heo 	.check_events	= floppy_check_events,
10311da177e4SLinus Torvalds 	.revalidate_disk= floppy_revalidate,
10321da177e4SLinus Torvalds };
10331da177e4SLinus Torvalds 
10343e9a6927SBenjamin Herrenschmidt static int swim3_add_device(struct macio_dev *mdev, int index)
10351da177e4SLinus Torvalds {
103661c7a080SGrant Likely 	struct device_node *swim = mdev->ofdev.dev.of_node;
10373e9a6927SBenjamin Herrenschmidt 	struct floppy_state *fs = &floppy_states[index];
10383e9a6927SBenjamin Herrenschmidt 	int rc = -EBUSY;
10391da177e4SLinus Torvalds 
10403e9a6927SBenjamin Herrenschmidt 	/* Check & Request resources */
10413e9a6927SBenjamin Herrenschmidt 	if (macio_resource_count(mdev) < 2) {
10423e9a6927SBenjamin Herrenschmidt 		printk(KERN_WARNING "ifd%d: no address for %s\n",
10433e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
10443e9a6927SBenjamin Herrenschmidt 		return -ENXIO;
1045cc5d0189SBenjamin Herrenschmidt 	}
10463e9a6927SBenjamin Herrenschmidt 	if (macio_irq_count(mdev) < 2) {
10473e9a6927SBenjamin Herrenschmidt 		printk(KERN_WARNING "fd%d: no intrs for device %s\n",
10483e9a6927SBenjamin Herrenschmidt 			index, swim->full_name);
1049cc5d0189SBenjamin Herrenschmidt 	}
10503e9a6927SBenjamin Herrenschmidt 	if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
10513e9a6927SBenjamin Herrenschmidt 		printk(KERN_ERR "fd%d: can't request mmio resource for %s\n",
10523e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
10533e9a6927SBenjamin Herrenschmidt 		return -EBUSY;
10541da177e4SLinus Torvalds 	}
10553e9a6927SBenjamin Herrenschmidt 	if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
10563e9a6927SBenjamin Herrenschmidt 		printk(KERN_ERR "fd%d: can't request dma resource for %s\n",
10573e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
10583e9a6927SBenjamin Herrenschmidt 		macio_release_resource(mdev, 0);
10593e9a6927SBenjamin Herrenschmidt 		return -EBUSY;
10603e9a6927SBenjamin Herrenschmidt 	}
10613e9a6927SBenjamin Herrenschmidt 	dev_set_drvdata(&mdev->ofdev.dev, fs);
10621da177e4SLinus Torvalds 
1063d58b0c39SBenjamin Herrenschmidt 	if (mdev->media_bay == NULL)
10641da177e4SLinus Torvalds 		pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	memset(fs, 0, sizeof(*fs));
1067515729ecSPaul Mackerras 	spin_lock_init(&fs->lock);
10681da177e4SLinus Torvalds 	fs->state = idle;
10693e9a6927SBenjamin Herrenschmidt 	fs->swim3 = (struct swim3 __iomem *)
10703e9a6927SBenjamin Herrenschmidt 		ioremap(macio_resource_start(mdev, 0), 0x200);
10713e9a6927SBenjamin Herrenschmidt 	if (fs->swim3 == NULL) {
10723e9a6927SBenjamin Herrenschmidt 		printk("fd%d: couldn't map registers for %s\n",
10733e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
10743e9a6927SBenjamin Herrenschmidt 		rc = -ENOMEM;
10753e9a6927SBenjamin Herrenschmidt 		goto out_release;
10763e9a6927SBenjamin Herrenschmidt 	}
10773e9a6927SBenjamin Herrenschmidt 	fs->dma = (struct dbdma_regs __iomem *)
10783e9a6927SBenjamin Herrenschmidt 		ioremap(macio_resource_start(mdev, 1), 0x200);
10793e9a6927SBenjamin Herrenschmidt 	if (fs->dma == NULL) {
10803e9a6927SBenjamin Herrenschmidt 		printk("fd%d: couldn't map DMA for %s\n",
10813e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
10823e9a6927SBenjamin Herrenschmidt 		iounmap(fs->swim3);
10833e9a6927SBenjamin Herrenschmidt 		rc = -ENOMEM;
10843e9a6927SBenjamin Herrenschmidt 		goto out_release;
10853e9a6927SBenjamin Herrenschmidt 	}
10863e9a6927SBenjamin Herrenschmidt 	fs->swim3_intr = macio_irq(mdev, 0);
1087a419aef8SJoe Perches 	fs->dma_intr = macio_irq(mdev, 1);
10881da177e4SLinus Torvalds 	fs->cur_cyl = -1;
10891da177e4SLinus Torvalds 	fs->cur_sector = -1;
10901da177e4SLinus Torvalds 	fs->secpercyl = 36;
10911da177e4SLinus Torvalds 	fs->secpertrack = 18;
10921da177e4SLinus Torvalds 	fs->total_secs = 2880;
1093d58b0c39SBenjamin Herrenschmidt 	fs->mdev = mdev;
10941da177e4SLinus Torvalds 	init_waitqueue_head(&fs->wait);
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
10971da177e4SLinus Torvalds 	memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
10981da177e4SLinus Torvalds 	st_le16(&fs->dma_cmd[1].command, DBDMA_STOP);
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
11013e9a6927SBenjamin Herrenschmidt 		printk(KERN_ERR "fd%d: couldn't request irq %d for %s\n",
11023e9a6927SBenjamin Herrenschmidt 		       index, fs->swim3_intr, swim->full_name);
11031da177e4SLinus Torvalds 		pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
11043e9a6927SBenjamin Herrenschmidt 		goto out_unmap;
11051da177e4SLinus Torvalds 		return -EBUSY;
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds /*
11081da177e4SLinus Torvalds 	if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
11091da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
11101da177e4SLinus Torvalds 		       fs->dma_intr);
11111da177e4SLinus Torvalds 		return -EBUSY;
11121da177e4SLinus Torvalds 	}
11131da177e4SLinus Torvalds */
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	init_timer(&fs->timeout);
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	printk(KERN_INFO "fd%d: SWIM3 floppy controller %s\n", floppy_count,
1118d58b0c39SBenjamin Herrenschmidt 		mdev->media_bay ? "in media bay" : "");
11191da177e4SLinus Torvalds 
11203e9a6927SBenjamin Herrenschmidt 	return 0;
11211da177e4SLinus Torvalds 
11223e9a6927SBenjamin Herrenschmidt  out_unmap:
11233e9a6927SBenjamin Herrenschmidt 	iounmap(fs->dma);
11243e9a6927SBenjamin Herrenschmidt 	iounmap(fs->swim3);
11253e9a6927SBenjamin Herrenschmidt 
11263e9a6927SBenjamin Herrenschmidt  out_release:
11273e9a6927SBenjamin Herrenschmidt 	macio_release_resource(mdev, 0);
11283e9a6927SBenjamin Herrenschmidt 	macio_release_resource(mdev, 1);
11293e9a6927SBenjamin Herrenschmidt 
11303e9a6927SBenjamin Herrenschmidt 	return rc;
11313e9a6927SBenjamin Herrenschmidt }
11323e9a6927SBenjamin Herrenschmidt 
11333e9a6927SBenjamin Herrenschmidt static int __devinit swim3_attach(struct macio_dev *mdev, const struct of_device_id *match)
11343e9a6927SBenjamin Herrenschmidt {
11353e9a6927SBenjamin Herrenschmidt 	int i, rc;
11363e9a6927SBenjamin Herrenschmidt 	struct gendisk *disk;
11373e9a6927SBenjamin Herrenschmidt 
11383e9a6927SBenjamin Herrenschmidt 	/* Add the drive */
11393e9a6927SBenjamin Herrenschmidt 	rc = swim3_add_device(mdev, floppy_count);
11403e9a6927SBenjamin Herrenschmidt 	if (rc)
11413e9a6927SBenjamin Herrenschmidt 		return rc;
11423e9a6927SBenjamin Herrenschmidt 
11433e9a6927SBenjamin Herrenschmidt 	/* Now create the queue if not there yet */
11443e9a6927SBenjamin Herrenschmidt 	if (swim3_queue == NULL) {
11453e9a6927SBenjamin Herrenschmidt 		/* If we failed, there isn't much we can do as the driver is still
11463e9a6927SBenjamin Herrenschmidt 		 * too dumb to remove the device, just bail out
11473e9a6927SBenjamin Herrenschmidt 		 */
11483e9a6927SBenjamin Herrenschmidt 		if (register_blkdev(FLOPPY_MAJOR, "fd"))
11493e9a6927SBenjamin Herrenschmidt 			return 0;
11503e9a6927SBenjamin Herrenschmidt 		swim3_queue = blk_init_queue(do_fd_request, &swim3_lock);
11513e9a6927SBenjamin Herrenschmidt 		if (swim3_queue == NULL) {
11523e9a6927SBenjamin Herrenschmidt 			unregister_blkdev(FLOPPY_MAJOR, "fd");
11533e9a6927SBenjamin Herrenschmidt 			return 0;
11543e9a6927SBenjamin Herrenschmidt 		}
11553e9a6927SBenjamin Herrenschmidt 	}
11563e9a6927SBenjamin Herrenschmidt 
11573e9a6927SBenjamin Herrenschmidt 	/* Now register that disk. Same comment about failure handling */
11583e9a6927SBenjamin Herrenschmidt 	i = floppy_count++;
11593e9a6927SBenjamin Herrenschmidt 	disk = disks[i] = alloc_disk(1);
11603e9a6927SBenjamin Herrenschmidt 	if (disk == NULL)
11613e9a6927SBenjamin Herrenschmidt 		return 0;
11623e9a6927SBenjamin Herrenschmidt 
11633e9a6927SBenjamin Herrenschmidt 	disk->major = FLOPPY_MAJOR;
11643e9a6927SBenjamin Herrenschmidt 	disk->first_minor = i;
11653e9a6927SBenjamin Herrenschmidt 	disk->fops = &floppy_fops;
11663e9a6927SBenjamin Herrenschmidt 	disk->private_data = &floppy_states[i];
11673e9a6927SBenjamin Herrenschmidt 	disk->queue = swim3_queue;
11683e9a6927SBenjamin Herrenschmidt 	disk->flags |= GENHD_FL_REMOVABLE;
11693e9a6927SBenjamin Herrenschmidt 	sprintf(disk->disk_name, "fd%d", i);
11703e9a6927SBenjamin Herrenschmidt 	set_capacity(disk, 2880);
11713e9a6927SBenjamin Herrenschmidt 	add_disk(disk);
11723e9a6927SBenjamin Herrenschmidt 
11733e9a6927SBenjamin Herrenschmidt 	return 0;
11743e9a6927SBenjamin Herrenschmidt }
11753e9a6927SBenjamin Herrenschmidt 
11763e9a6927SBenjamin Herrenschmidt static struct of_device_id swim3_match[] =
11773e9a6927SBenjamin Herrenschmidt {
11783e9a6927SBenjamin Herrenschmidt 	{
11793e9a6927SBenjamin Herrenschmidt 	.name		= "swim3",
11803e9a6927SBenjamin Herrenschmidt 	},
11813e9a6927SBenjamin Herrenschmidt 	{
11823e9a6927SBenjamin Herrenschmidt 	.compatible	= "ohare-swim3"
11833e9a6927SBenjamin Herrenschmidt 	},
11843e9a6927SBenjamin Herrenschmidt 	{
11853e9a6927SBenjamin Herrenschmidt 	.compatible	= "swim3"
11863e9a6927SBenjamin Herrenschmidt 	},
1187*f41c53a5SAxel Lin 	{ /* end of list */ }
11883e9a6927SBenjamin Herrenschmidt };
11893e9a6927SBenjamin Herrenschmidt 
11903e9a6927SBenjamin Herrenschmidt static struct macio_driver swim3_driver =
11913e9a6927SBenjamin Herrenschmidt {
1192c2cdf6abSBenjamin Herrenschmidt 	.driver = {
11933e9a6927SBenjamin Herrenschmidt 		.name 		= "swim3",
1194c2cdf6abSBenjamin Herrenschmidt 		.of_match_table	= swim3_match,
1195c2cdf6abSBenjamin Herrenschmidt 	},
11963e9a6927SBenjamin Herrenschmidt 	.probe		= swim3_attach,
11973e9a6927SBenjamin Herrenschmidt #if 0
11983e9a6927SBenjamin Herrenschmidt 	.suspend	= swim3_suspend,
11993e9a6927SBenjamin Herrenschmidt 	.resume		= swim3_resume,
12003e9a6927SBenjamin Herrenschmidt #endif
12013e9a6927SBenjamin Herrenschmidt };
12023e9a6927SBenjamin Herrenschmidt 
12033e9a6927SBenjamin Herrenschmidt 
12043e9a6927SBenjamin Herrenschmidt int swim3_init(void)
12053e9a6927SBenjamin Herrenschmidt {
12063e9a6927SBenjamin Herrenschmidt 	macio_register_driver(&swim3_driver);
12071da177e4SLinus Torvalds 	return 0;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds module_init(swim3_init)
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds MODULE_LICENSE("GPL");
12131da177e4SLinus Torvalds MODULE_AUTHOR("Paul Mackerras");
12141da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);
1215