xref: /openbmc/linux/drivers/block/swim3.c (revision 467ca759fc83fc35cb7d15aec0d74c62cffc4481)
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>
281da177e4SLinus Torvalds #include <linux/module.h>
29515729ecSPaul Mackerras #include <linux/spinlock.h>
301da177e4SLinus Torvalds #include <asm/io.h>
311da177e4SLinus Torvalds #include <asm/dbdma.h>
321da177e4SLinus Torvalds #include <asm/prom.h>
331da177e4SLinus Torvalds #include <asm/uaccess.h>
341da177e4SLinus Torvalds #include <asm/mediabay.h>
351da177e4SLinus Torvalds #include <asm/machdep.h>
361da177e4SLinus Torvalds #include <asm/pmac_feature.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds static struct request_queue *swim3_queue;
391da177e4SLinus Torvalds static struct gendisk *disks[2];
401da177e4SLinus Torvalds static struct request *fd_req;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #define MAX_FLOPPIES	2
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds enum swim_state {
451da177e4SLinus Torvalds 	idle,
461da177e4SLinus Torvalds 	locating,
471da177e4SLinus Torvalds 	seeking,
481da177e4SLinus Torvalds 	settling,
491da177e4SLinus Torvalds 	do_transfer,
501da177e4SLinus Torvalds 	jogging,
511da177e4SLinus Torvalds 	available,
521da177e4SLinus Torvalds 	revalidating,
531da177e4SLinus Torvalds 	ejecting
541da177e4SLinus Torvalds };
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #define REG(x)	unsigned char x; char x ## _pad[15];
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /*
591da177e4SLinus Torvalds  * The names for these registers mostly represent speculation on my part.
601da177e4SLinus Torvalds  * It will be interesting to see how close they are to the names Apple uses.
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds struct swim3 {
631da177e4SLinus Torvalds 	REG(data);
641da177e4SLinus Torvalds 	REG(timer);		/* counts down at 1MHz */
651da177e4SLinus Torvalds 	REG(error);
661da177e4SLinus Torvalds 	REG(mode);
671da177e4SLinus Torvalds 	REG(select);		/* controls CA0, CA1, CA2 and LSTRB signals */
681da177e4SLinus Torvalds 	REG(setup);
691da177e4SLinus Torvalds 	REG(control);		/* writing bits clears them */
701da177e4SLinus Torvalds 	REG(status);		/* writing bits sets them in control */
711da177e4SLinus Torvalds 	REG(intr);
721da177e4SLinus Torvalds 	REG(nseek);		/* # tracks to seek */
731da177e4SLinus Torvalds 	REG(ctrack);		/* current track number */
741da177e4SLinus Torvalds 	REG(csect);		/* current sector number */
751da177e4SLinus Torvalds 	REG(gap3);		/* size of gap 3 in track format */
761da177e4SLinus Torvalds 	REG(sector);		/* sector # to read or write */
771da177e4SLinus Torvalds 	REG(nsect);		/* # sectors to read or write */
781da177e4SLinus Torvalds 	REG(intr_enable);
791da177e4SLinus Torvalds };
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #define control_bic	control
821da177e4SLinus Torvalds #define control_bis	status
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* Bits in select register */
851da177e4SLinus Torvalds #define CA_MASK		7
861da177e4SLinus Torvalds #define LSTRB		8
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* Bits in control register */
891da177e4SLinus Torvalds #define DO_SEEK		0x80
901da177e4SLinus Torvalds #define FORMAT		0x40
911da177e4SLinus Torvalds #define SELECT		0x20
921da177e4SLinus Torvalds #define WRITE_SECTORS	0x10
931da177e4SLinus Torvalds #define DO_ACTION	0x08
941da177e4SLinus Torvalds #define DRIVE2_ENABLE	0x04
951da177e4SLinus Torvalds #define DRIVE_ENABLE	0x02
961da177e4SLinus Torvalds #define INTR_ENABLE	0x01
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /* Bits in status register */
991da177e4SLinus Torvalds #define FIFO_1BYTE	0x80
1001da177e4SLinus Torvalds #define FIFO_2BYTE	0x40
1011da177e4SLinus Torvalds #define ERROR		0x20
1021da177e4SLinus Torvalds #define DATA		0x08
1031da177e4SLinus Torvalds #define RDDATA		0x04
1041da177e4SLinus Torvalds #define INTR_PENDING	0x02
1051da177e4SLinus Torvalds #define MARK_BYTE	0x01
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /* Bits in intr and intr_enable registers */
1081da177e4SLinus Torvalds #define ERROR_INTR	0x20
1091da177e4SLinus Torvalds #define DATA_CHANGED	0x10
1101da177e4SLinus Torvalds #define TRANSFER_DONE	0x08
1111da177e4SLinus Torvalds #define SEEN_SECTOR	0x04
1121da177e4SLinus Torvalds #define SEEK_DONE	0x02
1131da177e4SLinus Torvalds #define TIMER_DONE	0x01
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* Bits in error register */
1161da177e4SLinus Torvalds #define ERR_DATA_CRC	0x80
1171da177e4SLinus Torvalds #define ERR_ADDR_CRC	0x40
1181da177e4SLinus Torvalds #define ERR_OVERRUN	0x04
1191da177e4SLinus Torvalds #define ERR_UNDERRUN	0x01
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /* Bits in setup register */
1221da177e4SLinus Torvalds #define S_SW_RESET	0x80
1231da177e4SLinus Torvalds #define S_GCR_WRITE	0x40
1241da177e4SLinus Torvalds #define S_IBM_DRIVE	0x20
1251da177e4SLinus Torvalds #define S_TEST_MODE	0x10
1261da177e4SLinus Torvalds #define S_FCLK_DIV2	0x08
1271da177e4SLinus Torvalds #define S_GCR		0x04
1281da177e4SLinus Torvalds #define S_COPY_PROT	0x02
1291da177e4SLinus Torvalds #define S_INV_WDATA	0x01
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /* Select values for swim3_action */
1321da177e4SLinus Torvalds #define SEEK_POSITIVE	0
1331da177e4SLinus Torvalds #define SEEK_NEGATIVE	4
1341da177e4SLinus Torvalds #define STEP		1
1351da177e4SLinus Torvalds #define MOTOR_ON	2
1361da177e4SLinus Torvalds #define MOTOR_OFF	6
1371da177e4SLinus Torvalds #define INDEX		3
1381da177e4SLinus Torvalds #define EJECT		7
1391da177e4SLinus Torvalds #define SETMFM		9
1401da177e4SLinus Torvalds #define SETGCR		13
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /* Select values for swim3_select and swim3_readbit */
1431da177e4SLinus Torvalds #define STEP_DIR	0
1441da177e4SLinus Torvalds #define STEPPING	1
1451da177e4SLinus Torvalds #define MOTOR_ON	2
1461da177e4SLinus Torvalds #define RELAX		3	/* also eject in progress */
1471da177e4SLinus Torvalds #define READ_DATA_0	4
1481da177e4SLinus Torvalds #define TWOMEG_DRIVE	5
1491da177e4SLinus Torvalds #define SINGLE_SIDED	6	/* drive or diskette is 4MB type? */
1501da177e4SLinus Torvalds #define DRIVE_PRESENT	7
1511da177e4SLinus Torvalds #define DISK_IN		8
1521da177e4SLinus Torvalds #define WRITE_PROT	9
1531da177e4SLinus Torvalds #define TRACK_ZERO	10
1541da177e4SLinus Torvalds #define TACHO		11
1551da177e4SLinus Torvalds #define READ_DATA_1	12
1561da177e4SLinus Torvalds #define MFM_MODE	13
1571da177e4SLinus Torvalds #define SEEK_COMPLETE	14
1581da177e4SLinus Torvalds #define ONEMEG_MEDIA	15
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /* Definitions of values used in writing and formatting */
1611da177e4SLinus Torvalds #define DATA_ESCAPE	0x99
1621da177e4SLinus Torvalds #define GCR_SYNC_EXC	0x3f
1631da177e4SLinus Torvalds #define GCR_SYNC_CONV	0x80
1641da177e4SLinus Torvalds #define GCR_FIRST_MARK	0xd5
1651da177e4SLinus Torvalds #define GCR_SECOND_MARK	0xaa
1661da177e4SLinus Torvalds #define GCR_ADDR_MARK	"\xd5\xaa\x00"
1671da177e4SLinus Torvalds #define GCR_DATA_MARK	"\xd5\xaa\x0b"
1681da177e4SLinus Torvalds #define GCR_SLIP_BYTE	"\x27\xaa"
1691da177e4SLinus Torvalds #define GCR_SELF_SYNC	"\x3f\xbf\x1e\x34\x3c\x3f"
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds #define DATA_99		"\x99\x99"
1721da177e4SLinus Torvalds #define MFM_ADDR_MARK	"\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
1731da177e4SLinus Torvalds #define MFM_INDEX_MARK	"\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
1741da177e4SLinus Torvalds #define MFM_GAP_LEN	12
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds struct floppy_state {
1771da177e4SLinus Torvalds 	enum swim_state	state;
178515729ecSPaul Mackerras 	spinlock_t lock;
1791da177e4SLinus Torvalds 	struct swim3 __iomem *swim3;	/* hardware registers */
1801da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dma;	/* DMA controller registers */
1811da177e4SLinus Torvalds 	int	swim3_intr;	/* interrupt number for SWIM3 */
1821da177e4SLinus Torvalds 	int	dma_intr;	/* interrupt number for DMA channel */
1831da177e4SLinus Torvalds 	int	cur_cyl;	/* cylinder head is on, or -1 */
1841da177e4SLinus Torvalds 	int	cur_sector;	/* last sector we saw go past */
1851da177e4SLinus Torvalds 	int	req_cyl;	/* the cylinder for the current r/w request */
1861da177e4SLinus Torvalds 	int	head;		/* head number ditto */
1871da177e4SLinus Torvalds 	int	req_sector;	/* sector number ditto */
1881da177e4SLinus Torvalds 	int	scount;		/* # sectors we're transferring at present */
1891da177e4SLinus Torvalds 	int	retries;
1901da177e4SLinus Torvalds 	int	settle_time;
1911da177e4SLinus Torvalds 	int	secpercyl;	/* disk geometry information */
1921da177e4SLinus Torvalds 	int	secpertrack;
1931da177e4SLinus Torvalds 	int	total_secs;
1941da177e4SLinus Torvalds 	int	write_prot;	/* 1 if write-protected, 0 if not, -1 dunno */
1951da177e4SLinus Torvalds 	struct dbdma_cmd *dma_cmd;
1961da177e4SLinus Torvalds 	int	ref_count;
1971da177e4SLinus Torvalds 	int	expect_cyl;
1981da177e4SLinus Torvalds 	struct timer_list timeout;
1991da177e4SLinus Torvalds 	int	timeout_pending;
2001da177e4SLinus Torvalds 	int	ejected;
2011da177e4SLinus Torvalds 	wait_queue_head_t wait;
2021da177e4SLinus Torvalds 	int	wanted;
2031da177e4SLinus Torvalds 	struct device_node*	media_bay; /* NULL when not in bay */
2041da177e4SLinus Torvalds 	char	dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
2051da177e4SLinus Torvalds };
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static struct floppy_state floppy_states[MAX_FLOPPIES];
2081da177e4SLinus Torvalds static int floppy_count = 0;
2091da177e4SLinus Torvalds static DEFINE_SPINLOCK(swim3_lock);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds static unsigned short write_preamble[] = {
2121da177e4SLinus Torvalds 	0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e,	/* gap field */
2131da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0,			/* sync field */
2141da177e4SLinus Torvalds 	0x99a1, 0x99a1, 0x99a1, 0x99fb,		/* data address mark */
2151da177e4SLinus Torvalds 	0x990f					/* no escape for 512 bytes */
2161da177e4SLinus Torvalds };
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds static unsigned short write_postamble[] = {
2191da177e4SLinus Torvalds 	0x9904,					/* insert CRC */
2201da177e4SLinus Torvalds 	0x4e4e, 0x4e4e,
2211da177e4SLinus Torvalds 	0x9908,					/* stop writing */
2221da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0
2231da177e4SLinus Torvalds };
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static void swim3_select(struct floppy_state *fs, int sel);
2261da177e4SLinus Torvalds static void swim3_action(struct floppy_state *fs, int action);
2271da177e4SLinus Torvalds static int swim3_readbit(struct floppy_state *fs, int bit);
228165125e1SJens Axboe static void do_fd_request(struct request_queue * q);
2291da177e4SLinus Torvalds static void start_request(struct floppy_state *fs);
2301da177e4SLinus Torvalds static void set_timeout(struct floppy_state *fs, int nticks,
2311da177e4SLinus Torvalds 			void (*proc)(unsigned long));
2321da177e4SLinus Torvalds static void scan_track(struct floppy_state *fs);
2331da177e4SLinus Torvalds static void seek_track(struct floppy_state *fs, int n);
2341da177e4SLinus Torvalds static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
2351da177e4SLinus Torvalds static void setup_transfer(struct floppy_state *fs);
2361da177e4SLinus Torvalds static void act(struct floppy_state *fs);
2371da177e4SLinus Torvalds static void scan_timeout(unsigned long data);
2381da177e4SLinus Torvalds static void seek_timeout(unsigned long data);
2391da177e4SLinus Torvalds static void settle_timeout(unsigned long data);
2401da177e4SLinus Torvalds static void xfer_timeout(unsigned long data);
2417d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id);
2427d12e780SDavid Howells /*static void fd_dma_interrupt(int irq, void *dev_id);*/
2431da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state,
2441da177e4SLinus Torvalds 		      int interruptible);
2451da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs);
2461da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs);
247b4d9a442SAl Viro static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
2481da177e4SLinus Torvalds 			unsigned int cmd, unsigned long param);
249b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode);
250b4d9a442SAl Viro static int floppy_release(struct gendisk *disk, fmode_t mode);
2511da177e4SLinus Torvalds static int floppy_check_change(struct gendisk *disk);
2521da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds static void swim3_select(struct floppy_state *fs, int sel)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
2591da177e4SLinus Torvalds 	if (sel & 8)
2601da177e4SLinus Torvalds 		out_8(&sw->control_bis, SELECT);
2611da177e4SLinus Torvalds 	else
2621da177e4SLinus Torvalds 		out_8(&sw->control_bic, SELECT);
2631da177e4SLinus Torvalds 	out_8(&sw->select, sel & CA_MASK);
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds static void swim3_action(struct floppy_state *fs, int action)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	swim3_select(fs, action);
2711da177e4SLinus Torvalds 	udelay(1);
2721da177e4SLinus Torvalds 	out_8(&sw->select, sw->select | LSTRB);
2731da177e4SLinus Torvalds 	udelay(2);
2741da177e4SLinus Torvalds 	out_8(&sw->select, sw->select & ~LSTRB);
2751da177e4SLinus Torvalds 	udelay(1);
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static int swim3_readbit(struct floppy_state *fs, int bit)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
2811da177e4SLinus Torvalds 	int stat;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	swim3_select(fs, bit);
2841da177e4SLinus Torvalds 	udelay(1);
2851da177e4SLinus Torvalds 	stat = in_8(&sw->status);
2861da177e4SLinus Torvalds 	return (stat & DATA) == 0;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
289165125e1SJens Axboe static void do_fd_request(struct request_queue * q)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	int i;
2921da177e4SLinus Torvalds 	for(i=0;i<floppy_count;i++)
2931da177e4SLinus Torvalds 	{
2948c870933SBenjamin Herrenschmidt #ifdef CONFIG_PMAC_MEDIABAY
2951da177e4SLinus Torvalds 		if (floppy_states[i].media_bay &&
2961da177e4SLinus Torvalds 			check_media_bay(floppy_states[i].media_bay, MB_FD))
2971da177e4SLinus Torvalds 			continue;
2988c870933SBenjamin Herrenschmidt #endif /* CONFIG_PMAC_MEDIABAY */
2991da177e4SLinus Torvalds 		start_request(&floppy_states[i]);
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds static void start_request(struct floppy_state *fs)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	struct request *req;
3061da177e4SLinus Torvalds 	unsigned long x;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	if (fs->state == idle && fs->wanted) {
3091da177e4SLinus Torvalds 		fs->state = available;
3101da177e4SLinus Torvalds 		wake_up(&fs->wait);
3111da177e4SLinus Torvalds 		return;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	while (fs->state == idle && (req = elv_next_request(swim3_queue))) {
3141da177e4SLinus Torvalds #if 0
3151da177e4SLinus Torvalds 		printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%ld buf=%p\n",
3161da177e4SLinus Torvalds 		       req->rq_disk->disk_name, req->cmd,
3171da177e4SLinus Torvalds 		       (long)req->sector, req->nr_sectors, req->buffer);
318cdd60262SJens Axboe 		printk("           errors=%d current_nr_sectors=%ld\n",
319cdd60262SJens Axboe 		       req->errors, req->current_nr_sectors);
3201da177e4SLinus Torvalds #endif
3211da177e4SLinus Torvalds 
322*467ca759STejun Heo 		if (req->sector >= fs->total_secs) {
323f06d9a2bSTejun Heo 			__blk_end_request_cur(req, -EIO);
3241da177e4SLinus Torvalds 			continue;
3251da177e4SLinus Torvalds 		}
3261da177e4SLinus Torvalds 		if (fs->ejected) {
327f06d9a2bSTejun Heo 			__blk_end_request_cur(req, -EIO);
3281da177e4SLinus Torvalds 			continue;
3291da177e4SLinus Torvalds 		}
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 		if (rq_data_dir(req) == WRITE) {
3321da177e4SLinus Torvalds 			if (fs->write_prot < 0)
3331da177e4SLinus Torvalds 				fs->write_prot = swim3_readbit(fs, WRITE_PROT);
3341da177e4SLinus Torvalds 			if (fs->write_prot) {
335f06d9a2bSTejun Heo 				__blk_end_request_cur(req, -EIO);
3361da177e4SLinus Torvalds 				continue;
3371da177e4SLinus Torvalds 			}
3381da177e4SLinus Torvalds 		}
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 		/* Do not remove the cast. req->sector is now a sector_t and
3411da177e4SLinus Torvalds 		 * can be 64 bits, but it will never go past 32 bits for this
3421da177e4SLinus Torvalds 		 * driver anyway, so we can safely cast it down and not have
3431da177e4SLinus Torvalds 		 * to do a 64/32 division
3441da177e4SLinus Torvalds 		 */
3451da177e4SLinus Torvalds 		fs->req_cyl = ((long)req->sector) / fs->secpercyl;
3461da177e4SLinus Torvalds 		x = ((long)req->sector) % fs->secpercyl;
3471da177e4SLinus Torvalds 		fs->head = x / fs->secpertrack;
3481da177e4SLinus Torvalds 		fs->req_sector = x % fs->secpertrack + 1;
3491da177e4SLinus Torvalds 		fd_req = req;
3501da177e4SLinus Torvalds 		fs->state = do_transfer;
3511da177e4SLinus Torvalds 		fs->retries = 0;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 		act(fs);
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds static void set_timeout(struct floppy_state *fs, int nticks,
3581da177e4SLinus Torvalds 			void (*proc)(unsigned long))
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	unsigned long flags;
3611da177e4SLinus Torvalds 
362515729ecSPaul Mackerras 	spin_lock_irqsave(&fs->lock, flags);
3631da177e4SLinus Torvalds 	if (fs->timeout_pending)
3641da177e4SLinus Torvalds 		del_timer(&fs->timeout);
3651da177e4SLinus Torvalds 	fs->timeout.expires = jiffies + nticks;
3661da177e4SLinus Torvalds 	fs->timeout.function = proc;
3671da177e4SLinus Torvalds 	fs->timeout.data = (unsigned long) fs;
3681da177e4SLinus Torvalds 	add_timer(&fs->timeout);
3691da177e4SLinus Torvalds 	fs->timeout_pending = 1;
370515729ecSPaul Mackerras 	spin_unlock_irqrestore(&fs->lock, flags);
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds static inline void scan_track(struct floppy_state *fs)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	swim3_select(fs, READ_DATA_0);
3781da177e4SLinus Torvalds 	in_8(&sw->intr);		/* clear SEEN_SECTOR bit */
3791da177e4SLinus Torvalds 	in_8(&sw->error);
3801da177e4SLinus Torvalds 	out_8(&sw->intr_enable, SEEN_SECTOR);
3811da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_ACTION);
3821da177e4SLinus Torvalds 	/* enable intr when track found */
3831da177e4SLinus Torvalds 	set_timeout(fs, HZ, scan_timeout);	/* enable timeout */
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds static inline void seek_track(struct floppy_state *fs, int n)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	if (n >= 0) {
3911da177e4SLinus Torvalds 		swim3_action(fs, SEEK_POSITIVE);
3921da177e4SLinus Torvalds 		sw->nseek = n;
3931da177e4SLinus Torvalds 	} else {
3941da177e4SLinus Torvalds 		swim3_action(fs, SEEK_NEGATIVE);
3951da177e4SLinus Torvalds 		sw->nseek = -n;
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 	fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1;
3981da177e4SLinus Torvalds 	swim3_select(fs, STEP);
3991da177e4SLinus Torvalds 	in_8(&sw->error);
4001da177e4SLinus Torvalds 	/* enable intr when seek finished */
4011da177e4SLinus Torvalds 	out_8(&sw->intr_enable, SEEK_DONE);
4021da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_SEEK);
4031da177e4SLinus Torvalds 	set_timeout(fs, 3*HZ, seek_timeout);	/* enable timeout */
4041da177e4SLinus Torvalds 	fs->settle_time = 0;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds static inline void init_dma(struct dbdma_cmd *cp, int cmd,
4081da177e4SLinus Torvalds 			    void *buf, int count)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	st_le16(&cp->req_count, count);
4111da177e4SLinus Torvalds 	st_le16(&cp->command, cmd);
4121da177e4SLinus Torvalds 	st_le32(&cp->phy_addr, virt_to_bus(buf));
4131da177e4SLinus Torvalds 	cp->xfer_status = 0;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds static inline void setup_transfer(struct floppy_state *fs)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	int n;
4191da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
4201da177e4SLinus Torvalds 	struct dbdma_cmd *cp = fs->dma_cmd;
4211da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr = fs->dma;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	if (fd_req->current_nr_sectors <= 0) {
4241da177e4SLinus Torvalds 		printk(KERN_ERR "swim3: transfer 0 sectors?\n");
4251da177e4SLinus Torvalds 		return;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 	if (rq_data_dir(fd_req) == WRITE)
4281da177e4SLinus Torvalds 		n = 1;
4291da177e4SLinus Torvalds 	else {
4301da177e4SLinus Torvalds 		n = fs->secpertrack - fs->req_sector + 1;
4311da177e4SLinus Torvalds 		if (n > fd_req->current_nr_sectors)
4321da177e4SLinus Torvalds 			n = fd_req->current_nr_sectors;
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 	fs->scount = n;
4351da177e4SLinus Torvalds 	swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
4361da177e4SLinus Torvalds 	out_8(&sw->sector, fs->req_sector);
4371da177e4SLinus Torvalds 	out_8(&sw->nsect, n);
4381da177e4SLinus Torvalds 	out_8(&sw->gap3, 0);
4391da177e4SLinus Torvalds 	out_le32(&dr->cmdptr, virt_to_bus(cp));
4401da177e4SLinus Torvalds 	if (rq_data_dir(fd_req) == WRITE) {
4411da177e4SLinus Torvalds 		/* Set up 3 dma commands: write preamble, data, postamble */
4421da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
4431da177e4SLinus Torvalds 		++cp;
4441da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_MORE, fd_req->buffer, 512);
4451da177e4SLinus Torvalds 		++cp;
4461da177e4SLinus Torvalds 		init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
4471da177e4SLinus Torvalds 	} else {
4481da177e4SLinus Torvalds 		init_dma(cp, INPUT_LAST, fd_req->buffer, n * 512);
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 	++cp;
4511da177e4SLinus Torvalds 	out_le16(&cp->command, DBDMA_STOP);
4521da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
4531da177e4SLinus Torvalds 	in_8(&sw->error);
4541da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
4551da177e4SLinus Torvalds 	if (rq_data_dir(fd_req) == WRITE)
4561da177e4SLinus Torvalds 		out_8(&sw->control_bis, WRITE_SECTORS);
4571da177e4SLinus Torvalds 	in_8(&sw->intr);
4581da177e4SLinus Torvalds 	out_le32(&dr->control, (RUN << 16) | RUN);
4591da177e4SLinus Torvalds 	/* enable intr when transfer complete */
4601da177e4SLinus Torvalds 	out_8(&sw->intr_enable, TRANSFER_DONE);
4611da177e4SLinus Torvalds 	out_8(&sw->control_bis, DO_ACTION);
4621da177e4SLinus Torvalds 	set_timeout(fs, 2*HZ, xfer_timeout);	/* enable timeout */
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds static void act(struct floppy_state *fs)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	for (;;) {
4681da177e4SLinus Torvalds 		switch (fs->state) {
4691da177e4SLinus Torvalds 		case idle:
4701da177e4SLinus Torvalds 			return;		/* XXX shouldn't get here */
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 		case locating:
4731da177e4SLinus Torvalds 			if (swim3_readbit(fs, TRACK_ZERO)) {
4741da177e4SLinus Torvalds 				fs->cur_cyl = 0;
4751da177e4SLinus Torvalds 				if (fs->req_cyl == 0)
4761da177e4SLinus Torvalds 					fs->state = do_transfer;
4771da177e4SLinus Torvalds 				else
4781da177e4SLinus Torvalds 					fs->state = seeking;
4791da177e4SLinus Torvalds 				break;
4801da177e4SLinus Torvalds 			}
4811da177e4SLinus Torvalds 			scan_track(fs);
4821da177e4SLinus Torvalds 			return;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 		case seeking:
4851da177e4SLinus Torvalds 			if (fs->cur_cyl < 0) {
4861da177e4SLinus Torvalds 				fs->expect_cyl = -1;
4871da177e4SLinus Torvalds 				fs->state = locating;
4881da177e4SLinus Torvalds 				break;
4891da177e4SLinus Torvalds 			}
4901da177e4SLinus Torvalds 			if (fs->req_cyl == fs->cur_cyl) {
4911da177e4SLinus Torvalds 				printk("whoops, seeking 0\n");
4921da177e4SLinus Torvalds 				fs->state = do_transfer;
4931da177e4SLinus Torvalds 				break;
4941da177e4SLinus Torvalds 			}
4951da177e4SLinus Torvalds 			seek_track(fs, fs->req_cyl - fs->cur_cyl);
4961da177e4SLinus Torvalds 			return;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 		case settling:
4991da177e4SLinus Torvalds 			/* check for SEEK_COMPLETE after 30ms */
5001da177e4SLinus Torvalds 			fs->settle_time = (HZ + 32) / 33;
5011da177e4SLinus Torvalds 			set_timeout(fs, fs->settle_time, settle_timeout);
5021da177e4SLinus Torvalds 			return;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 		case do_transfer:
5051da177e4SLinus Torvalds 			if (fs->cur_cyl != fs->req_cyl) {
5061da177e4SLinus Torvalds 				if (fs->retries > 5) {
507f06d9a2bSTejun Heo 					__blk_end_request_cur(fd_req, -EIO);
5081da177e4SLinus Torvalds 					fs->state = idle;
5091da177e4SLinus Torvalds 					return;
5101da177e4SLinus Torvalds 				}
5111da177e4SLinus Torvalds 				fs->state = seeking;
5121da177e4SLinus Torvalds 				break;
5131da177e4SLinus Torvalds 			}
5141da177e4SLinus Torvalds 			setup_transfer(fs);
5151da177e4SLinus Torvalds 			return;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 		case jogging:
5181da177e4SLinus Torvalds 			seek_track(fs, -5);
5191da177e4SLinus Torvalds 			return;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 		default:
5221da177e4SLinus Torvalds 			printk(KERN_ERR"swim3: unknown state %d\n", fs->state);
5231da177e4SLinus Torvalds 			return;
5241da177e4SLinus Torvalds 		}
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds static void scan_timeout(unsigned long data)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
5311da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	fs->timeout_pending = 0;
5341da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
5351da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
5361da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
5371da177e4SLinus Torvalds 	fs->cur_cyl = -1;
5381da177e4SLinus Torvalds 	if (fs->retries > 5) {
539f06d9a2bSTejun Heo 		__blk_end_request_cur(fd_req, -EIO);
5401da177e4SLinus Torvalds 		fs->state = idle;
5411da177e4SLinus Torvalds 		start_request(fs);
5421da177e4SLinus Torvalds 	} else {
5431da177e4SLinus Torvalds 		fs->state = jogging;
5441da177e4SLinus Torvalds 		act(fs);
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds static void seek_timeout(unsigned long data)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
5511da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	fs->timeout_pending = 0;
5541da177e4SLinus Torvalds 	out_8(&sw->control_bic, DO_SEEK);
5551da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
5561da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
5571da177e4SLinus Torvalds 	printk(KERN_ERR "swim3: seek timeout\n");
558f06d9a2bSTejun Heo 	__blk_end_request_cur(fd_req, -EIO);
5591da177e4SLinus Torvalds 	fs->state = idle;
5601da177e4SLinus Torvalds 	start_request(fs);
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds static void settle_timeout(unsigned long data)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
5661da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	fs->timeout_pending = 0;
5691da177e4SLinus Torvalds 	if (swim3_readbit(fs, SEEK_COMPLETE)) {
5701da177e4SLinus Torvalds 		out_8(&sw->select, RELAX);
5711da177e4SLinus Torvalds 		fs->state = locating;
5721da177e4SLinus Torvalds 		act(fs);
5731da177e4SLinus Torvalds 		return;
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
5761da177e4SLinus Torvalds 	if (fs->settle_time < 2*HZ) {
5771da177e4SLinus Torvalds 		++fs->settle_time;
5781da177e4SLinus Torvalds 		set_timeout(fs, 1, settle_timeout);
5791da177e4SLinus Torvalds 		return;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 	printk(KERN_ERR "swim3: seek settle timeout\n");
582f06d9a2bSTejun Heo 	__blk_end_request_cur(fd_req, -EIO);
5831da177e4SLinus Torvalds 	fs->state = idle;
5841da177e4SLinus Torvalds 	start_request(fs);
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds static void xfer_timeout(unsigned long data)
5881da177e4SLinus Torvalds {
5891da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) data;
5901da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
5911da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr = fs->dma;
5921da177e4SLinus Torvalds 	int n;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	fs->timeout_pending = 0;
5951da177e4SLinus Torvalds 	out_le32(&dr->control, RUN << 16);
5961da177e4SLinus Torvalds 	/* We must wait a bit for dbdma to stop */
5971da177e4SLinus Torvalds 	for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
5981da177e4SLinus Torvalds 		udelay(1);
5991da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
6001da177e4SLinus Torvalds 	out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
6011da177e4SLinus Torvalds 	out_8(&sw->select, RELAX);
6021da177e4SLinus Torvalds 	printk(KERN_ERR "swim3: timeout %sing sector %ld\n",
6031da177e4SLinus Torvalds 	       (rq_data_dir(fd_req)==WRITE? "writ": "read"), (long)fd_req->sector);
604f06d9a2bSTejun Heo 	__blk_end_request_cur(fd_req, -EIO);
6051da177e4SLinus Torvalds 	fs->state = idle;
6061da177e4SLinus Torvalds 	start_request(fs);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6097d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds 	struct floppy_state *fs = (struct floppy_state *) dev_id;
6121da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
6131da177e4SLinus Torvalds 	int intr, err, n;
6141da177e4SLinus Torvalds 	int stat, resid;
6151da177e4SLinus Torvalds 	struct dbdma_regs __iomem *dr;
6161da177e4SLinus Torvalds 	struct dbdma_cmd *cp;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	intr = in_8(&sw->intr);
6191da177e4SLinus Torvalds 	err = (intr & ERROR_INTR)? in_8(&sw->error): 0;
6201da177e4SLinus Torvalds 	if ((intr & ERROR_INTR) && fs->state != do_transfer)
62114b1ffb5SOlaf Hering 		printk(KERN_ERR "swim3_interrupt, state=%d, dir=%x, intr=%x, err=%x\n",
6221da177e4SLinus Torvalds 		       fs->state, rq_data_dir(fd_req), intr, err);
6231da177e4SLinus Torvalds 	switch (fs->state) {
6241da177e4SLinus Torvalds 	case locating:
6251da177e4SLinus Torvalds 		if (intr & SEEN_SECTOR) {
6261da177e4SLinus Torvalds 			out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
6271da177e4SLinus Torvalds 			out_8(&sw->select, RELAX);
6281da177e4SLinus Torvalds 			out_8(&sw->intr_enable, 0);
6291da177e4SLinus Torvalds 			del_timer(&fs->timeout);
6301da177e4SLinus Torvalds 			fs->timeout_pending = 0;
6311da177e4SLinus Torvalds 			if (sw->ctrack == 0xff) {
6321da177e4SLinus Torvalds 				printk(KERN_ERR "swim3: seen sector but cyl=ff?\n");
6331da177e4SLinus Torvalds 				fs->cur_cyl = -1;
6341da177e4SLinus Torvalds 				if (fs->retries > 5) {
635f06d9a2bSTejun Heo 					__blk_end_request_cur(fd_req, -EIO);
6361da177e4SLinus Torvalds 					fs->state = idle;
6371da177e4SLinus Torvalds 					start_request(fs);
6381da177e4SLinus Torvalds 				} else {
6391da177e4SLinus Torvalds 					fs->state = jogging;
6401da177e4SLinus Torvalds 					act(fs);
6411da177e4SLinus Torvalds 				}
6421da177e4SLinus Torvalds 				break;
6431da177e4SLinus Torvalds 			}
6441da177e4SLinus Torvalds 			fs->cur_cyl = sw->ctrack;
6451da177e4SLinus Torvalds 			fs->cur_sector = sw->csect;
6461da177e4SLinus Torvalds 			if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
6471da177e4SLinus Torvalds 				printk(KERN_ERR "swim3: expected cyl %d, got %d\n",
6481da177e4SLinus Torvalds 				       fs->expect_cyl, fs->cur_cyl);
6491da177e4SLinus Torvalds 			fs->state = do_transfer;
6501da177e4SLinus Torvalds 			act(fs);
6511da177e4SLinus Torvalds 		}
6521da177e4SLinus Torvalds 		break;
6531da177e4SLinus Torvalds 	case seeking:
6541da177e4SLinus Torvalds 	case jogging:
6551da177e4SLinus Torvalds 		if (sw->nseek == 0) {
6561da177e4SLinus Torvalds 			out_8(&sw->control_bic, DO_SEEK);
6571da177e4SLinus Torvalds 			out_8(&sw->select, RELAX);
6581da177e4SLinus Torvalds 			out_8(&sw->intr_enable, 0);
6591da177e4SLinus Torvalds 			del_timer(&fs->timeout);
6601da177e4SLinus Torvalds 			fs->timeout_pending = 0;
6611da177e4SLinus Torvalds 			if (fs->state == seeking)
6621da177e4SLinus Torvalds 				++fs->retries;
6631da177e4SLinus Torvalds 			fs->state = settling;
6641da177e4SLinus Torvalds 			act(fs);
6651da177e4SLinus Torvalds 		}
6661da177e4SLinus Torvalds 		break;
6671da177e4SLinus Torvalds 	case settling:
6681da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
6691da177e4SLinus Torvalds 		del_timer(&fs->timeout);
6701da177e4SLinus Torvalds 		fs->timeout_pending = 0;
6711da177e4SLinus Torvalds 		act(fs);
6721da177e4SLinus Torvalds 		break;
6731da177e4SLinus Torvalds 	case do_transfer:
6741da177e4SLinus Torvalds 		if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
6751da177e4SLinus Torvalds 			break;
6761da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
6771da177e4SLinus Torvalds 		out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
6781da177e4SLinus Torvalds 		out_8(&sw->select, RELAX);
6791da177e4SLinus Torvalds 		del_timer(&fs->timeout);
6801da177e4SLinus Torvalds 		fs->timeout_pending = 0;
6811da177e4SLinus Torvalds 		dr = fs->dma;
6821da177e4SLinus Torvalds 		cp = fs->dma_cmd;
6831da177e4SLinus Torvalds 		if (rq_data_dir(fd_req) == WRITE)
6841da177e4SLinus Torvalds 			++cp;
6851da177e4SLinus Torvalds 		/*
6861da177e4SLinus Torvalds 		 * Check that the main data transfer has finished.
6871da177e4SLinus Torvalds 		 * On writing, the swim3 sometimes doesn't use
6881da177e4SLinus Torvalds 		 * up all the bytes of the postamble, so we can still
6891da177e4SLinus Torvalds 		 * see DMA active here.  That doesn't matter as long
6901da177e4SLinus Torvalds 		 * as all the sector data has been transferred.
6911da177e4SLinus Torvalds 		 */
6921da177e4SLinus Torvalds 		if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) {
6931da177e4SLinus Torvalds 			/* wait a little while for DMA to complete */
6941da177e4SLinus Torvalds 			for (n = 0; n < 100; ++n) {
6951da177e4SLinus Torvalds 				if (cp->xfer_status != 0)
6961da177e4SLinus Torvalds 					break;
6971da177e4SLinus Torvalds 				udelay(1);
6981da177e4SLinus Torvalds 				barrier();
6991da177e4SLinus Torvalds 			}
7001da177e4SLinus Torvalds 		}
7011da177e4SLinus Torvalds 		/* turn off DMA */
7021da177e4SLinus Torvalds 		out_le32(&dr->control, (RUN | PAUSE) << 16);
7031da177e4SLinus Torvalds 		stat = ld_le16(&cp->xfer_status);
7041da177e4SLinus Torvalds 		resid = ld_le16(&cp->res_count);
7051da177e4SLinus Torvalds 		if (intr & ERROR_INTR) {
7061da177e4SLinus Torvalds 			n = fs->scount - 1 - resid / 512;
7071da177e4SLinus Torvalds 			if (n > 0) {
708*467ca759STejun Heo 				blk_update_request(fd_req, 0, n << 9);
7091da177e4SLinus Torvalds 				fs->req_sector += n;
7101da177e4SLinus Torvalds 			}
7111da177e4SLinus Torvalds 			if (fs->retries < 5) {
7121da177e4SLinus Torvalds 				++fs->retries;
7131da177e4SLinus Torvalds 				act(fs);
7141da177e4SLinus Torvalds 			} else {
7151da177e4SLinus Torvalds 				printk("swim3: error %sing block %ld (err=%x)\n",
7161da177e4SLinus Torvalds 				       rq_data_dir(fd_req) == WRITE? "writ": "read",
7171da177e4SLinus Torvalds 				       (long)fd_req->sector, err);
718f06d9a2bSTejun Heo 				__blk_end_request_cur(fd_req, -EIO);
7191da177e4SLinus Torvalds 				fs->state = idle;
7201da177e4SLinus Torvalds 			}
7211da177e4SLinus Torvalds 		} else {
7221da177e4SLinus Torvalds 			if ((stat & ACTIVE) == 0 || resid != 0) {
7231da177e4SLinus Torvalds 				/* musta been an error */
7241da177e4SLinus Torvalds 				printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid);
72514b1ffb5SOlaf Hering 				printk(KERN_ERR "  state=%d, dir=%x, intr=%x, err=%x\n",
7261da177e4SLinus Torvalds 				       fs->state, rq_data_dir(fd_req), intr, err);
727f06d9a2bSTejun Heo 				__blk_end_request_cur(fd_req, -EIO);
7281da177e4SLinus Torvalds 				fs->state = idle;
7291da177e4SLinus Torvalds 				start_request(fs);
7301da177e4SLinus Torvalds 				break;
7311da177e4SLinus Torvalds 			}
732*467ca759STejun Heo 			if (__blk_end_request(fd_req, 0, fs->scount << 9)) {
7331da177e4SLinus Torvalds 				fs->req_sector += fs->scount;
7341da177e4SLinus Torvalds 				if (fs->req_sector > fs->secpertrack) {
7351da177e4SLinus Torvalds 					fs->req_sector -= fs->secpertrack;
7361da177e4SLinus Torvalds 					if (++fs->head > 1) {
7371da177e4SLinus Torvalds 						fs->head = 0;
7381da177e4SLinus Torvalds 						++fs->req_cyl;
7391da177e4SLinus Torvalds 					}
7401da177e4SLinus Torvalds 				}
7411da177e4SLinus Torvalds 				act(fs);
742*467ca759STejun Heo 			} else
743*467ca759STejun Heo 				fs->state = idle;
7441da177e4SLinus Torvalds 		}
7451da177e4SLinus Torvalds 		if (fs->state == idle)
7461da177e4SLinus Torvalds 			start_request(fs);
7471da177e4SLinus Torvalds 		break;
7481da177e4SLinus Torvalds 	default:
7491da177e4SLinus Torvalds 		printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state);
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 	return IRQ_HANDLED;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /*
7557d12e780SDavid Howells static void fd_dma_interrupt(int irq, void *dev_id)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds */
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state,
7611da177e4SLinus Torvalds 		      int interruptible)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	unsigned long flags;
7641da177e4SLinus Torvalds 
765515729ecSPaul Mackerras 	spin_lock_irqsave(&fs->lock, flags);
7661da177e4SLinus Torvalds 	if (fs->state != idle) {
7671da177e4SLinus Torvalds 		++fs->wanted;
7681da177e4SLinus Torvalds 		while (fs->state != available) {
7691da177e4SLinus Torvalds 			if (interruptible && signal_pending(current)) {
7701da177e4SLinus Torvalds 				--fs->wanted;
771515729ecSPaul Mackerras 				spin_unlock_irqrestore(&fs->lock, flags);
7721da177e4SLinus Torvalds 				return -EINTR;
7731da177e4SLinus Torvalds 			}
7741da177e4SLinus Torvalds 			interruptible_sleep_on(&fs->wait);
7751da177e4SLinus Torvalds 		}
7761da177e4SLinus Torvalds 		--fs->wanted;
7771da177e4SLinus Torvalds 	}
7781da177e4SLinus Torvalds 	fs->state = state;
779515729ecSPaul Mackerras 	spin_unlock_irqrestore(&fs->lock, flags);
7801da177e4SLinus Torvalds 	return 0;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	unsigned long flags;
7861da177e4SLinus Torvalds 
787515729ecSPaul Mackerras 	spin_lock_irqsave(&fs->lock, flags);
7881da177e4SLinus Torvalds 	fs->state = idle;
7891da177e4SLinus Torvalds 	start_request(fs);
790515729ecSPaul Mackerras 	spin_unlock_irqrestore(&fs->lock, flags);
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs)
7941da177e4SLinus Torvalds {
7951da177e4SLinus Torvalds 	int err, n;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	err = grab_drive(fs, ejecting, 1);
7981da177e4SLinus Torvalds 	if (err)
7991da177e4SLinus Torvalds 		return err;
8001da177e4SLinus Torvalds 	swim3_action(fs, EJECT);
8011da177e4SLinus Torvalds 	for (n = 20; n > 0; --n) {
8021da177e4SLinus Torvalds 		if (signal_pending(current)) {
8031da177e4SLinus Torvalds 			err = -EINTR;
8041da177e4SLinus Torvalds 			break;
8051da177e4SLinus Torvalds 		}
8061da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
80786e84862SNishanth Aravamudan 		schedule_timeout_interruptible(1);
8081da177e4SLinus Torvalds 		if (swim3_readbit(fs, DISK_IN) == 0)
8091da177e4SLinus Torvalds 			break;
8101da177e4SLinus Torvalds 	}
8111da177e4SLinus Torvalds 	swim3_select(fs, RELAX);
8121da177e4SLinus Torvalds 	udelay(150);
8131da177e4SLinus Torvalds 	fs->ejected = 1;
8141da177e4SLinus Torvalds 	release_drive(fs);
8151da177e4SLinus Torvalds 	return err;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds static struct floppy_struct floppy_type =
8191da177e4SLinus Torvalds 	{ 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL };	/*  7 1.44MB 3.5"   */
8201da177e4SLinus Torvalds 
821b4d9a442SAl Viro static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
8221da177e4SLinus Torvalds 			unsigned int cmd, unsigned long param)
8231da177e4SLinus Torvalds {
824b4d9a442SAl Viro 	struct floppy_state *fs = bdev->bd_disk->private_data;
8251da177e4SLinus Torvalds 	int err;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
8281da177e4SLinus Torvalds 		return -EPERM;
8291da177e4SLinus Torvalds 
8308c870933SBenjamin Herrenschmidt #ifdef CONFIG_PMAC_MEDIABAY
8311da177e4SLinus Torvalds 	if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
8321da177e4SLinus Torvalds 		return -ENXIO;
8338c870933SBenjamin Herrenschmidt #endif
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	switch (cmd) {
8361da177e4SLinus Torvalds 	case FDEJECT:
8371da177e4SLinus Torvalds 		if (fs->ref_count != 1)
8381da177e4SLinus Torvalds 			return -EBUSY;
8391da177e4SLinus Torvalds 		err = fd_eject(fs);
8401da177e4SLinus Torvalds 		return err;
8411da177e4SLinus Torvalds 	case FDGETPRM:
8421da177e4SLinus Torvalds 	        if (copy_to_user((void __user *) param, &floppy_type,
8431da177e4SLinus Torvalds 				 sizeof(struct floppy_struct)))
8441da177e4SLinus Torvalds 			return -EFAULT;
8451da177e4SLinus Torvalds 		return 0;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 	return -ENOTTY;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
850b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode)
8511da177e4SLinus Torvalds {
852b4d9a442SAl Viro 	struct floppy_state *fs = bdev->bd_disk->private_data;
8531da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
8541da177e4SLinus Torvalds 	int n, err = 0;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	if (fs->ref_count == 0) {
8578c870933SBenjamin Herrenschmidt #ifdef CONFIG_PMAC_MEDIABAY
8581da177e4SLinus Torvalds 		if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
8591da177e4SLinus Torvalds 			return -ENXIO;
8608c870933SBenjamin Herrenschmidt #endif
8611da177e4SLinus Torvalds 		out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
8621da177e4SLinus Torvalds 		out_8(&sw->control_bic, 0xff);
8631da177e4SLinus Torvalds 		out_8(&sw->mode, 0x95);
8641da177e4SLinus Torvalds 		udelay(10);
8651da177e4SLinus Torvalds 		out_8(&sw->intr_enable, 0);
8661da177e4SLinus Torvalds 		out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
8671da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_ON);
8681da177e4SLinus Torvalds 		fs->write_prot = -1;
8691da177e4SLinus Torvalds 		fs->cur_cyl = -1;
8701da177e4SLinus Torvalds 		for (n = 0; n < 2 * HZ; ++n) {
8711da177e4SLinus Torvalds 			if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE))
8721da177e4SLinus Torvalds 				break;
8731da177e4SLinus Torvalds 			if (signal_pending(current)) {
8741da177e4SLinus Torvalds 				err = -EINTR;
8751da177e4SLinus Torvalds 				break;
8761da177e4SLinus Torvalds 			}
8771da177e4SLinus Torvalds 			swim3_select(fs, RELAX);
87886e84862SNishanth Aravamudan 			schedule_timeout_interruptible(1);
8791da177e4SLinus Torvalds 		}
8801da177e4SLinus Torvalds 		if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
8811da177e4SLinus Torvalds 				 || swim3_readbit(fs, DISK_IN) == 0))
8821da177e4SLinus Torvalds 			err = -ENXIO;
8831da177e4SLinus Torvalds 		swim3_action(fs, SETMFM);
8841da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
8851da177e4SLinus Torvalds 
886b4d9a442SAl Viro 	} else if (fs->ref_count == -1 || mode & FMODE_EXCL)
8871da177e4SLinus Torvalds 		return -EBUSY;
8881da177e4SLinus Torvalds 
889b4d9a442SAl Viro 	if (err == 0 && (mode & FMODE_NDELAY) == 0
890b4d9a442SAl Viro 	    && (mode & (FMODE_READ|FMODE_WRITE))) {
891b4d9a442SAl Viro 		check_disk_change(bdev);
8921da177e4SLinus Torvalds 		if (fs->ejected)
8931da177e4SLinus Torvalds 			err = -ENXIO;
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 
896b4d9a442SAl Viro 	if (err == 0 && (mode & FMODE_WRITE)) {
8971da177e4SLinus Torvalds 		if (fs->write_prot < 0)
8981da177e4SLinus Torvalds 			fs->write_prot = swim3_readbit(fs, WRITE_PROT);
8991da177e4SLinus Torvalds 		if (fs->write_prot)
9001da177e4SLinus Torvalds 			err = -EROFS;
9011da177e4SLinus Torvalds 	}
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	if (err) {
9041da177e4SLinus Torvalds 		if (fs->ref_count == 0) {
9051da177e4SLinus Torvalds 			swim3_action(fs, MOTOR_OFF);
9061da177e4SLinus Torvalds 			out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
9071da177e4SLinus Torvalds 			swim3_select(fs, RELAX);
9081da177e4SLinus Torvalds 		}
9091da177e4SLinus Torvalds 		return err;
9101da177e4SLinus Torvalds 	}
9111da177e4SLinus Torvalds 
912b4d9a442SAl Viro 	if (mode & FMODE_EXCL)
9131da177e4SLinus Torvalds 		fs->ref_count = -1;
9141da177e4SLinus Torvalds 	else
9151da177e4SLinus Torvalds 		++fs->ref_count;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	return 0;
9181da177e4SLinus Torvalds }
9191da177e4SLinus Torvalds 
920b4d9a442SAl Viro static int floppy_release(struct gendisk *disk, fmode_t mode)
9211da177e4SLinus Torvalds {
922b4d9a442SAl Viro 	struct floppy_state *fs = disk->private_data;
9231da177e4SLinus Torvalds 	struct swim3 __iomem *sw = fs->swim3;
9241da177e4SLinus Torvalds 	if (fs->ref_count > 0 && --fs->ref_count == 0) {
9251da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_OFF);
9261da177e4SLinus Torvalds 		out_8(&sw->control_bic, 0xff);
9271da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 	return 0;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds static int floppy_check_change(struct gendisk *disk)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	struct floppy_state *fs = disk->private_data;
9351da177e4SLinus Torvalds 	return fs->ejected;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk)
9391da177e4SLinus Torvalds {
9401da177e4SLinus Torvalds 	struct floppy_state *fs = disk->private_data;
9411da177e4SLinus Torvalds 	struct swim3 __iomem *sw;
9421da177e4SLinus Torvalds 	int ret, n;
9431da177e4SLinus Torvalds 
9448c870933SBenjamin Herrenschmidt #ifdef CONFIG_PMAC_MEDIABAY
9451da177e4SLinus Torvalds 	if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
9461da177e4SLinus Torvalds 		return -ENXIO;
9478c870933SBenjamin Herrenschmidt #endif
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	sw = fs->swim3;
9501da177e4SLinus Torvalds 	grab_drive(fs, revalidating, 0);
9511da177e4SLinus Torvalds 	out_8(&sw->intr_enable, 0);
9521da177e4SLinus Torvalds 	out_8(&sw->control_bis, DRIVE_ENABLE);
9531da177e4SLinus Torvalds 	swim3_action(fs, MOTOR_ON);	/* necessary? */
9541da177e4SLinus Torvalds 	fs->write_prot = -1;
9551da177e4SLinus Torvalds 	fs->cur_cyl = -1;
9561da177e4SLinus Torvalds 	mdelay(1);
9571da177e4SLinus Torvalds 	for (n = HZ; n > 0; --n) {
9581da177e4SLinus Torvalds 		if (swim3_readbit(fs, SEEK_COMPLETE))
9591da177e4SLinus Torvalds 			break;
9601da177e4SLinus Torvalds 		if (signal_pending(current))
9611da177e4SLinus Torvalds 			break;
9621da177e4SLinus Torvalds 		swim3_select(fs, RELAX);
96386e84862SNishanth Aravamudan 		schedule_timeout_interruptible(1);
9641da177e4SLinus Torvalds 	}
9651da177e4SLinus Torvalds 	ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
9661da177e4SLinus Torvalds 		|| swim3_readbit(fs, DISK_IN) == 0;
9671da177e4SLinus Torvalds 	if (ret)
9681da177e4SLinus Torvalds 		swim3_action(fs, MOTOR_OFF);
9691da177e4SLinus Torvalds 	else {
9701da177e4SLinus Torvalds 		fs->ejected = 0;
9711da177e4SLinus Torvalds 		swim3_action(fs, SETMFM);
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 	swim3_select(fs, RELAX);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	release_drive(fs);
9761da177e4SLinus Torvalds 	return ret;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds static struct block_device_operations floppy_fops = {
980b4d9a442SAl Viro 	.open		= floppy_open,
981b4d9a442SAl Viro 	.release	= floppy_release,
982b4d9a442SAl Viro 	.locked_ioctl	= floppy_ioctl,
9831da177e4SLinus Torvalds 	.media_changed	= floppy_check_change,
9841da177e4SLinus Torvalds 	.revalidate_disk= floppy_revalidate,
9851da177e4SLinus Torvalds };
9861da177e4SLinus Torvalds 
9873e9a6927SBenjamin Herrenschmidt static int swim3_add_device(struct macio_dev *mdev, int index)
9881da177e4SLinus Torvalds {
9893e9a6927SBenjamin Herrenschmidt 	struct device_node *swim = mdev->ofdev.node;
9901da177e4SLinus Torvalds 	struct device_node *mediabay;
9913e9a6927SBenjamin Herrenschmidt 	struct floppy_state *fs = &floppy_states[index];
9923e9a6927SBenjamin Herrenschmidt 	int rc = -EBUSY;
9931da177e4SLinus Torvalds 
9943e9a6927SBenjamin Herrenschmidt 	/* Check & Request resources */
9953e9a6927SBenjamin Herrenschmidt 	if (macio_resource_count(mdev) < 2) {
9963e9a6927SBenjamin Herrenschmidt 		printk(KERN_WARNING "ifd%d: no address for %s\n",
9973e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
9983e9a6927SBenjamin Herrenschmidt 		return -ENXIO;
999cc5d0189SBenjamin Herrenschmidt 	}
10003e9a6927SBenjamin Herrenschmidt 	if (macio_irq_count(mdev) < 2) {
10013e9a6927SBenjamin Herrenschmidt 		printk(KERN_WARNING "fd%d: no intrs for device %s\n",
10023e9a6927SBenjamin Herrenschmidt 			index, swim->full_name);
1003cc5d0189SBenjamin Herrenschmidt 	}
10043e9a6927SBenjamin Herrenschmidt 	if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
10053e9a6927SBenjamin Herrenschmidt 		printk(KERN_ERR "fd%d: can't request mmio resource for %s\n",
10063e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
10073e9a6927SBenjamin Herrenschmidt 		return -EBUSY;
10081da177e4SLinus Torvalds 	}
10093e9a6927SBenjamin Herrenschmidt 	if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
10103e9a6927SBenjamin Herrenschmidt 		printk(KERN_ERR "fd%d: can't request dma resource for %s\n",
10113e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
10123e9a6927SBenjamin Herrenschmidt 		macio_release_resource(mdev, 0);
10133e9a6927SBenjamin Herrenschmidt 		return -EBUSY;
10143e9a6927SBenjamin Herrenschmidt 	}
10153e9a6927SBenjamin Herrenschmidt 	dev_set_drvdata(&mdev->ofdev.dev, fs);
10161da177e4SLinus Torvalds 
10173e9a6927SBenjamin Herrenschmidt 	mediabay = (strcasecmp(swim->parent->type, "media-bay") == 0) ?
10183e9a6927SBenjamin Herrenschmidt 		swim->parent : NULL;
10191da177e4SLinus Torvalds 	if (mediabay == NULL)
10201da177e4SLinus Torvalds 		pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	memset(fs, 0, sizeof(*fs));
1023515729ecSPaul Mackerras 	spin_lock_init(&fs->lock);
10241da177e4SLinus Torvalds 	fs->state = idle;
10253e9a6927SBenjamin Herrenschmidt 	fs->swim3 = (struct swim3 __iomem *)
10263e9a6927SBenjamin Herrenschmidt 		ioremap(macio_resource_start(mdev, 0), 0x200);
10273e9a6927SBenjamin Herrenschmidt 	if (fs->swim3 == NULL) {
10283e9a6927SBenjamin Herrenschmidt 		printk("fd%d: couldn't map registers for %s\n",
10293e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
10303e9a6927SBenjamin Herrenschmidt 		rc = -ENOMEM;
10313e9a6927SBenjamin Herrenschmidt 		goto out_release;
10323e9a6927SBenjamin Herrenschmidt 	}
10333e9a6927SBenjamin Herrenschmidt 	fs->dma = (struct dbdma_regs __iomem *)
10343e9a6927SBenjamin Herrenschmidt 		ioremap(macio_resource_start(mdev, 1), 0x200);
10353e9a6927SBenjamin Herrenschmidt 	if (fs->dma == NULL) {
10363e9a6927SBenjamin Herrenschmidt 		printk("fd%d: couldn't map DMA for %s\n",
10373e9a6927SBenjamin Herrenschmidt 		       index, swim->full_name);
10383e9a6927SBenjamin Herrenschmidt 		iounmap(fs->swim3);
10393e9a6927SBenjamin Herrenschmidt 		rc = -ENOMEM;
10403e9a6927SBenjamin Herrenschmidt 		goto out_release;
10413e9a6927SBenjamin Herrenschmidt 	}
10423e9a6927SBenjamin Herrenschmidt 	fs->swim3_intr = macio_irq(mdev, 0);
10433e9a6927SBenjamin Herrenschmidt 	fs->dma_intr = macio_irq(mdev, 1);;
10441da177e4SLinus Torvalds 	fs->cur_cyl = -1;
10451da177e4SLinus Torvalds 	fs->cur_sector = -1;
10461da177e4SLinus Torvalds 	fs->secpercyl = 36;
10471da177e4SLinus Torvalds 	fs->secpertrack = 18;
10481da177e4SLinus Torvalds 	fs->total_secs = 2880;
10491da177e4SLinus Torvalds 	fs->media_bay = mediabay;
10501da177e4SLinus Torvalds 	init_waitqueue_head(&fs->wait);
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
10531da177e4SLinus Torvalds 	memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
10541da177e4SLinus Torvalds 	st_le16(&fs->dma_cmd[1].command, DBDMA_STOP);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
10573e9a6927SBenjamin Herrenschmidt 		printk(KERN_ERR "fd%d: couldn't request irq %d for %s\n",
10583e9a6927SBenjamin Herrenschmidt 		       index, fs->swim3_intr, swim->full_name);
10591da177e4SLinus Torvalds 		pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
10603e9a6927SBenjamin Herrenschmidt 		goto out_unmap;
10611da177e4SLinus Torvalds 		return -EBUSY;
10621da177e4SLinus Torvalds 	}
10631da177e4SLinus Torvalds /*
10641da177e4SLinus Torvalds 	if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
10651da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
10661da177e4SLinus Torvalds 		       fs->dma_intr);
10671da177e4SLinus Torvalds 		return -EBUSY;
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds */
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	init_timer(&fs->timeout);
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	printk(KERN_INFO "fd%d: SWIM3 floppy controller %s\n", floppy_count,
10741da177e4SLinus Torvalds 		mediabay ? "in media bay" : "");
10751da177e4SLinus Torvalds 
10763e9a6927SBenjamin Herrenschmidt 	return 0;
10771da177e4SLinus Torvalds 
10783e9a6927SBenjamin Herrenschmidt  out_unmap:
10793e9a6927SBenjamin Herrenschmidt 	iounmap(fs->dma);
10803e9a6927SBenjamin Herrenschmidt 	iounmap(fs->swim3);
10813e9a6927SBenjamin Herrenschmidt 
10823e9a6927SBenjamin Herrenschmidt  out_release:
10833e9a6927SBenjamin Herrenschmidt 	macio_release_resource(mdev, 0);
10843e9a6927SBenjamin Herrenschmidt 	macio_release_resource(mdev, 1);
10853e9a6927SBenjamin Herrenschmidt 
10863e9a6927SBenjamin Herrenschmidt 	return rc;
10873e9a6927SBenjamin Herrenschmidt }
10883e9a6927SBenjamin Herrenschmidt 
10893e9a6927SBenjamin Herrenschmidt static int __devinit swim3_attach(struct macio_dev *mdev, const struct of_device_id *match)
10903e9a6927SBenjamin Herrenschmidt {
10913e9a6927SBenjamin Herrenschmidt 	int i, rc;
10923e9a6927SBenjamin Herrenschmidt 	struct gendisk *disk;
10933e9a6927SBenjamin Herrenschmidt 
10943e9a6927SBenjamin Herrenschmidt 	/* Add the drive */
10953e9a6927SBenjamin Herrenschmidt 	rc = swim3_add_device(mdev, floppy_count);
10963e9a6927SBenjamin Herrenschmidt 	if (rc)
10973e9a6927SBenjamin Herrenschmidt 		return rc;
10983e9a6927SBenjamin Herrenschmidt 
10993e9a6927SBenjamin Herrenschmidt 	/* Now create the queue if not there yet */
11003e9a6927SBenjamin Herrenschmidt 	if (swim3_queue == NULL) {
11013e9a6927SBenjamin Herrenschmidt 		/* If we failed, there isn't much we can do as the driver is still
11023e9a6927SBenjamin Herrenschmidt 		 * too dumb to remove the device, just bail out
11033e9a6927SBenjamin Herrenschmidt 		 */
11043e9a6927SBenjamin Herrenschmidt 		if (register_blkdev(FLOPPY_MAJOR, "fd"))
11053e9a6927SBenjamin Herrenschmidt 			return 0;
11063e9a6927SBenjamin Herrenschmidt 		swim3_queue = blk_init_queue(do_fd_request, &swim3_lock);
11073e9a6927SBenjamin Herrenschmidt 		if (swim3_queue == NULL) {
11083e9a6927SBenjamin Herrenschmidt 			unregister_blkdev(FLOPPY_MAJOR, "fd");
11093e9a6927SBenjamin Herrenschmidt 			return 0;
11103e9a6927SBenjamin Herrenschmidt 		}
11113e9a6927SBenjamin Herrenschmidt 	}
11123e9a6927SBenjamin Herrenschmidt 
11133e9a6927SBenjamin Herrenschmidt 	/* Now register that disk. Same comment about failure handling */
11143e9a6927SBenjamin Herrenschmidt 	i = floppy_count++;
11153e9a6927SBenjamin Herrenschmidt 	disk = disks[i] = alloc_disk(1);
11163e9a6927SBenjamin Herrenschmidt 	if (disk == NULL)
11173e9a6927SBenjamin Herrenschmidt 		return 0;
11183e9a6927SBenjamin Herrenschmidt 
11193e9a6927SBenjamin Herrenschmidt 	disk->major = FLOPPY_MAJOR;
11203e9a6927SBenjamin Herrenschmidt 	disk->first_minor = i;
11213e9a6927SBenjamin Herrenschmidt 	disk->fops = &floppy_fops;
11223e9a6927SBenjamin Herrenschmidt 	disk->private_data = &floppy_states[i];
11233e9a6927SBenjamin Herrenschmidt 	disk->queue = swim3_queue;
11243e9a6927SBenjamin Herrenschmidt 	disk->flags |= GENHD_FL_REMOVABLE;
11253e9a6927SBenjamin Herrenschmidt 	sprintf(disk->disk_name, "fd%d", i);
11263e9a6927SBenjamin Herrenschmidt 	set_capacity(disk, 2880);
11273e9a6927SBenjamin Herrenschmidt 	add_disk(disk);
11283e9a6927SBenjamin Herrenschmidt 
11293e9a6927SBenjamin Herrenschmidt 	return 0;
11303e9a6927SBenjamin Herrenschmidt }
11313e9a6927SBenjamin Herrenschmidt 
11323e9a6927SBenjamin Herrenschmidt static struct of_device_id swim3_match[] =
11333e9a6927SBenjamin Herrenschmidt {
11343e9a6927SBenjamin Herrenschmidt 	{
11353e9a6927SBenjamin Herrenschmidt 	.name		= "swim3",
11363e9a6927SBenjamin Herrenschmidt 	},
11373e9a6927SBenjamin Herrenschmidt 	{
11383e9a6927SBenjamin Herrenschmidt 	.compatible	= "ohare-swim3"
11393e9a6927SBenjamin Herrenschmidt 	},
11403e9a6927SBenjamin Herrenschmidt 	{
11413e9a6927SBenjamin Herrenschmidt 	.compatible	= "swim3"
11423e9a6927SBenjamin Herrenschmidt 	},
11433e9a6927SBenjamin Herrenschmidt };
11443e9a6927SBenjamin Herrenschmidt 
11453e9a6927SBenjamin Herrenschmidt static struct macio_driver swim3_driver =
11463e9a6927SBenjamin Herrenschmidt {
11473e9a6927SBenjamin Herrenschmidt 	.name 		= "swim3",
11483e9a6927SBenjamin Herrenschmidt 	.match_table	= swim3_match,
11493e9a6927SBenjamin Herrenschmidt 	.probe		= swim3_attach,
11503e9a6927SBenjamin Herrenschmidt #if 0
11513e9a6927SBenjamin Herrenschmidt 	.suspend	= swim3_suspend,
11523e9a6927SBenjamin Herrenschmidt 	.resume		= swim3_resume,
11533e9a6927SBenjamin Herrenschmidt #endif
11543e9a6927SBenjamin Herrenschmidt };
11553e9a6927SBenjamin Herrenschmidt 
11563e9a6927SBenjamin Herrenschmidt 
11573e9a6927SBenjamin Herrenschmidt int swim3_init(void)
11583e9a6927SBenjamin Herrenschmidt {
11593e9a6927SBenjamin Herrenschmidt 	macio_register_driver(&swim3_driver);
11601da177e4SLinus Torvalds 	return 0;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds module_init(swim3_init)
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds MODULE_LICENSE("GPL");
11661da177e4SLinus Torvalds MODULE_AUTHOR("Paul Mackerras");
11671da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);
1168