1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Driver for the SWIM3 (Super Woz Integrated Machine 3) 41da177e4SLinus Torvalds * floppy controller found on Power Macintoshes. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996 Paul Mackerras. 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds /* 101da177e4SLinus Torvalds * TODO: 111da177e4SLinus Torvalds * handle 2 drives 121da177e4SLinus Torvalds * handle GCR disks 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 15b3025457SBenjamin Herrenschmidt #undef DEBUG 16b3025457SBenjamin Herrenschmidt 171da177e4SLinus Torvalds #include <linux/stddef.h> 181da177e4SLinus Torvalds #include <linux/kernel.h> 19174cd4b1SIngo Molnar #include <linux/sched/signal.h> 201da177e4SLinus Torvalds #include <linux/timer.h> 211da177e4SLinus Torvalds #include <linux/delay.h> 221da177e4SLinus Torvalds #include <linux/fd.h> 231da177e4SLinus Torvalds #include <linux/ioctl.h> 248ccb8cb1SOmar Sandoval #include <linux/blk-mq.h> 251da177e4SLinus Torvalds #include <linux/interrupt.h> 262a48fc0aSArnd Bergmann #include <linux/mutex.h> 271da177e4SLinus Torvalds #include <linux/module.h> 28515729ecSPaul Mackerras #include <linux/spinlock.h> 29106fd892SArnd Bergmann #include <linux/wait.h> 301da177e4SLinus Torvalds #include <asm/io.h> 311da177e4SLinus Torvalds #include <asm/dbdma.h> 321da177e4SLinus Torvalds #include <asm/prom.h> 337c0f6ba6SLinus Torvalds #include <linux/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 #define MAX_FLOPPIES 2 391da177e4SLinus Torvalds 40b3025457SBenjamin Herrenschmidt static DEFINE_MUTEX(swim3_mutex); 41b3025457SBenjamin Herrenschmidt static struct gendisk *disks[MAX_FLOPPIES]; 42b3025457SBenjamin Herrenschmidt 431da177e4SLinus Torvalds enum swim_state { 441da177e4SLinus Torvalds idle, 451da177e4SLinus Torvalds locating, 461da177e4SLinus Torvalds seeking, 471da177e4SLinus Torvalds settling, 481da177e4SLinus Torvalds do_transfer, 491da177e4SLinus Torvalds jogging, 501da177e4SLinus Torvalds available, 511da177e4SLinus Torvalds revalidating, 521da177e4SLinus Torvalds ejecting 531da177e4SLinus Torvalds }; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #define REG(x) unsigned char x; char x ## _pad[15]; 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /* 581da177e4SLinus Torvalds * The names for these registers mostly represent speculation on my part. 591da177e4SLinus Torvalds * It will be interesting to see how close they are to the names Apple uses. 601da177e4SLinus Torvalds */ 611da177e4SLinus Torvalds struct swim3 { 621da177e4SLinus Torvalds REG(data); 631da177e4SLinus Torvalds REG(timer); /* counts down at 1MHz */ 641da177e4SLinus Torvalds REG(error); 651da177e4SLinus Torvalds REG(mode); 661da177e4SLinus Torvalds REG(select); /* controls CA0, CA1, CA2 and LSTRB signals */ 671da177e4SLinus Torvalds REG(setup); 681da177e4SLinus Torvalds REG(control); /* writing bits clears them */ 691da177e4SLinus Torvalds REG(status); /* writing bits sets them in control */ 701da177e4SLinus Torvalds REG(intr); 711da177e4SLinus Torvalds REG(nseek); /* # tracks to seek */ 721da177e4SLinus Torvalds REG(ctrack); /* current track number */ 731da177e4SLinus Torvalds REG(csect); /* current sector number */ 741da177e4SLinus Torvalds REG(gap3); /* size of gap 3 in track format */ 751da177e4SLinus Torvalds REG(sector); /* sector # to read or write */ 761da177e4SLinus Torvalds REG(nsect); /* # sectors to read or write */ 771da177e4SLinus Torvalds REG(intr_enable); 781da177e4SLinus Torvalds }; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #define control_bic control 811da177e4SLinus Torvalds #define control_bis status 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds /* Bits in select register */ 841da177e4SLinus Torvalds #define CA_MASK 7 851da177e4SLinus Torvalds #define LSTRB 8 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* Bits in control register */ 881da177e4SLinus Torvalds #define DO_SEEK 0x80 891da177e4SLinus Torvalds #define FORMAT 0x40 901da177e4SLinus Torvalds #define SELECT 0x20 911da177e4SLinus Torvalds #define WRITE_SECTORS 0x10 921da177e4SLinus Torvalds #define DO_ACTION 0x08 931da177e4SLinus Torvalds #define DRIVE2_ENABLE 0x04 941da177e4SLinus Torvalds #define DRIVE_ENABLE 0x02 951da177e4SLinus Torvalds #define INTR_ENABLE 0x01 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* Bits in status register */ 981da177e4SLinus Torvalds #define FIFO_1BYTE 0x80 991da177e4SLinus Torvalds #define FIFO_2BYTE 0x40 1001da177e4SLinus Torvalds #define ERROR 0x20 1011da177e4SLinus Torvalds #define DATA 0x08 1021da177e4SLinus Torvalds #define RDDATA 0x04 1031da177e4SLinus Torvalds #define INTR_PENDING 0x02 1041da177e4SLinus Torvalds #define MARK_BYTE 0x01 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* Bits in intr and intr_enable registers */ 1071da177e4SLinus Torvalds #define ERROR_INTR 0x20 1081da177e4SLinus Torvalds #define DATA_CHANGED 0x10 1091da177e4SLinus Torvalds #define TRANSFER_DONE 0x08 1101da177e4SLinus Torvalds #define SEEN_SECTOR 0x04 1111da177e4SLinus Torvalds #define SEEK_DONE 0x02 1121da177e4SLinus Torvalds #define TIMER_DONE 0x01 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* Bits in error register */ 1151da177e4SLinus Torvalds #define ERR_DATA_CRC 0x80 1161da177e4SLinus Torvalds #define ERR_ADDR_CRC 0x40 1171da177e4SLinus Torvalds #define ERR_OVERRUN 0x04 1181da177e4SLinus Torvalds #define ERR_UNDERRUN 0x01 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* Bits in setup register */ 1211da177e4SLinus Torvalds #define S_SW_RESET 0x80 1221da177e4SLinus Torvalds #define S_GCR_WRITE 0x40 1231da177e4SLinus Torvalds #define S_IBM_DRIVE 0x20 1241da177e4SLinus Torvalds #define S_TEST_MODE 0x10 1251da177e4SLinus Torvalds #define S_FCLK_DIV2 0x08 1261da177e4SLinus Torvalds #define S_GCR 0x04 1271da177e4SLinus Torvalds #define S_COPY_PROT 0x02 1281da177e4SLinus Torvalds #define S_INV_WDATA 0x01 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /* Select values for swim3_action */ 1311da177e4SLinus Torvalds #define SEEK_POSITIVE 0 1321da177e4SLinus Torvalds #define SEEK_NEGATIVE 4 1331da177e4SLinus Torvalds #define STEP 1 1341da177e4SLinus Torvalds #define MOTOR_ON 2 1351da177e4SLinus Torvalds #define MOTOR_OFF 6 1361da177e4SLinus Torvalds #define INDEX 3 1371da177e4SLinus Torvalds #define EJECT 7 1381da177e4SLinus Torvalds #define SETMFM 9 1391da177e4SLinus Torvalds #define SETGCR 13 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* Select values for swim3_select and swim3_readbit */ 1421da177e4SLinus Torvalds #define STEP_DIR 0 1431da177e4SLinus Torvalds #define STEPPING 1 1441da177e4SLinus Torvalds #define MOTOR_ON 2 1451da177e4SLinus Torvalds #define RELAX 3 /* also eject in progress */ 1461da177e4SLinus Torvalds #define READ_DATA_0 4 14756a1c5eeSFinn Thain #define ONEMEG_DRIVE 5 1481da177e4SLinus Torvalds #define SINGLE_SIDED 6 /* drive or diskette is 4MB type? */ 1491da177e4SLinus Torvalds #define DRIVE_PRESENT 7 1501da177e4SLinus Torvalds #define DISK_IN 8 1511da177e4SLinus Torvalds #define WRITE_PROT 9 1521da177e4SLinus Torvalds #define TRACK_ZERO 10 1531da177e4SLinus Torvalds #define TACHO 11 1541da177e4SLinus Torvalds #define READ_DATA_1 12 15556a1c5eeSFinn Thain #define GCR_MODE 13 1561da177e4SLinus Torvalds #define SEEK_COMPLETE 14 15756a1c5eeSFinn Thain #define TWOMEG_MEDIA 15 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds /* Definitions of values used in writing and formatting */ 1601da177e4SLinus Torvalds #define DATA_ESCAPE 0x99 1611da177e4SLinus Torvalds #define GCR_SYNC_EXC 0x3f 1621da177e4SLinus Torvalds #define GCR_SYNC_CONV 0x80 1631da177e4SLinus Torvalds #define GCR_FIRST_MARK 0xd5 1641da177e4SLinus Torvalds #define GCR_SECOND_MARK 0xaa 1651da177e4SLinus Torvalds #define GCR_ADDR_MARK "\xd5\xaa\x00" 1661da177e4SLinus Torvalds #define GCR_DATA_MARK "\xd5\xaa\x0b" 1671da177e4SLinus Torvalds #define GCR_SLIP_BYTE "\x27\xaa" 1681da177e4SLinus Torvalds #define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f" 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds #define DATA_99 "\x99\x99" 1711da177e4SLinus Torvalds #define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe" 1721da177e4SLinus Torvalds #define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc" 1731da177e4SLinus Torvalds #define MFM_GAP_LEN 12 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds struct floppy_state { 1761da177e4SLinus Torvalds enum swim_state state; 1771da177e4SLinus Torvalds struct swim3 __iomem *swim3; /* hardware registers */ 1781da177e4SLinus Torvalds struct dbdma_regs __iomem *dma; /* DMA controller registers */ 1791da177e4SLinus Torvalds int swim3_intr; /* interrupt number for SWIM3 */ 1801da177e4SLinus Torvalds int dma_intr; /* interrupt number for DMA channel */ 1811da177e4SLinus Torvalds int cur_cyl; /* cylinder head is on, or -1 */ 1821da177e4SLinus Torvalds int cur_sector; /* last sector we saw go past */ 1831da177e4SLinus Torvalds int req_cyl; /* the cylinder for the current r/w request */ 1841da177e4SLinus Torvalds int head; /* head number ditto */ 1851da177e4SLinus Torvalds int req_sector; /* sector number ditto */ 1861da177e4SLinus Torvalds int scount; /* # sectors we're transferring at present */ 1871da177e4SLinus Torvalds int retries; 1881da177e4SLinus Torvalds int settle_time; 1891da177e4SLinus Torvalds int secpercyl; /* disk geometry information */ 1901da177e4SLinus Torvalds int secpertrack; 1911da177e4SLinus Torvalds int total_secs; 1921da177e4SLinus Torvalds int write_prot; /* 1 if write-protected, 0 if not, -1 dunno */ 1931da177e4SLinus Torvalds struct dbdma_cmd *dma_cmd; 1941da177e4SLinus Torvalds int ref_count; 1951da177e4SLinus Torvalds int expect_cyl; 1961da177e4SLinus Torvalds struct timer_list timeout; 1971da177e4SLinus Torvalds int timeout_pending; 1981da177e4SLinus Torvalds int ejected; 1991da177e4SLinus Torvalds wait_queue_head_t wait; 2001da177e4SLinus Torvalds int wanted; 201d58b0c39SBenjamin Herrenschmidt struct macio_dev *mdev; 2021da177e4SLinus Torvalds char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)]; 203b3025457SBenjamin Herrenschmidt int index; 204b3025457SBenjamin Herrenschmidt struct request *cur_req; 2058ccb8cb1SOmar Sandoval struct blk_mq_tag_set tag_set; 2061da177e4SLinus Torvalds }; 2071da177e4SLinus Torvalds 208b3025457SBenjamin Herrenschmidt #define swim3_err(fmt, arg...) dev_err(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) 209b3025457SBenjamin Herrenschmidt #define swim3_warn(fmt, arg...) dev_warn(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) 210b3025457SBenjamin Herrenschmidt #define swim3_info(fmt, arg...) dev_info(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) 211b3025457SBenjamin Herrenschmidt 212b3025457SBenjamin Herrenschmidt #ifdef DEBUG 213b3025457SBenjamin Herrenschmidt #define swim3_dbg(fmt, arg...) dev_dbg(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) 214b3025457SBenjamin Herrenschmidt #else 215b3025457SBenjamin Herrenschmidt #define swim3_dbg(fmt, arg...) do { } while(0) 216b3025457SBenjamin Herrenschmidt #endif 217b3025457SBenjamin Herrenschmidt 2181da177e4SLinus Torvalds static struct floppy_state floppy_states[MAX_FLOPPIES]; 2191da177e4SLinus Torvalds static int floppy_count = 0; 2201da177e4SLinus Torvalds static DEFINE_SPINLOCK(swim3_lock); 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds static unsigned short write_preamble[] = { 2231da177e4SLinus Torvalds 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */ 2241da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, /* sync field */ 2251da177e4SLinus Torvalds 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */ 2261da177e4SLinus Torvalds 0x990f /* no escape for 512 bytes */ 2271da177e4SLinus Torvalds }; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static unsigned short write_postamble[] = { 2301da177e4SLinus Torvalds 0x9904, /* insert CRC */ 2311da177e4SLinus Torvalds 0x4e4e, 0x4e4e, 2321da177e4SLinus Torvalds 0x9908, /* stop writing */ 2331da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0 2341da177e4SLinus Torvalds }; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static void seek_track(struct floppy_state *fs, int n); 2371da177e4SLinus Torvalds static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count); 2381da177e4SLinus Torvalds static void act(struct floppy_state *fs); 239b5775a6bSKees Cook static void scan_timeout(struct timer_list *t); 240b5775a6bSKees Cook static void seek_timeout(struct timer_list *t); 241b5775a6bSKees Cook static void settle_timeout(struct timer_list *t); 242b5775a6bSKees Cook static void xfer_timeout(struct timer_list *t); 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); 252db2a144bSAl Viro static void 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 2572a842acaSChristoph Hellwig static bool swim3_end_request(struct floppy_state *fs, blk_status_t err, unsigned int nr_bytes) 258f4bd4b90STejun Heo { 259b3025457SBenjamin Herrenschmidt struct request *req = fs->cur_req; 260b3025457SBenjamin Herrenschmidt 261b3025457SBenjamin Herrenschmidt swim3_dbg(" end request, err=%d nr_bytes=%d, cur_req=%p\n", 262b3025457SBenjamin Herrenschmidt err, nr_bytes, req); 263b3025457SBenjamin Herrenschmidt 264b3025457SBenjamin Herrenschmidt if (err) 265b3025457SBenjamin Herrenschmidt nr_bytes = blk_rq_cur_bytes(req); 2668ccb8cb1SOmar Sandoval if (blk_update_request(req, err, nr_bytes)) 267f4bd4b90STejun Heo return true; 2688ccb8cb1SOmar Sandoval __blk_mq_end_request(req, err); 269b3025457SBenjamin Herrenschmidt fs->cur_req = NULL; 270f4bd4b90STejun Heo return false; 271f4bd4b90STejun Heo } 272f4bd4b90STejun Heo 2731da177e4SLinus Torvalds static void swim3_select(struct floppy_state *fs, int sel) 2741da177e4SLinus Torvalds { 2751da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds out_8(&sw->select, RELAX); 2781da177e4SLinus Torvalds if (sel & 8) 2791da177e4SLinus Torvalds out_8(&sw->control_bis, SELECT); 2801da177e4SLinus Torvalds else 2811da177e4SLinus Torvalds out_8(&sw->control_bic, SELECT); 2821da177e4SLinus Torvalds out_8(&sw->select, sel & CA_MASK); 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds static void swim3_action(struct floppy_state *fs, int action) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds swim3_select(fs, action); 2901da177e4SLinus Torvalds udelay(1); 2911da177e4SLinus Torvalds out_8(&sw->select, sw->select | LSTRB); 2921da177e4SLinus Torvalds udelay(2); 2931da177e4SLinus Torvalds out_8(&sw->select, sw->select & ~LSTRB); 2941da177e4SLinus Torvalds udelay(1); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds static int swim3_readbit(struct floppy_state *fs, int bit) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 3001da177e4SLinus Torvalds int stat; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds swim3_select(fs, bit); 3031da177e4SLinus Torvalds udelay(1); 3041da177e4SLinus Torvalds stat = in_8(&sw->status); 3051da177e4SLinus Torvalds return (stat & DATA) == 0; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3088ccb8cb1SOmar Sandoval static blk_status_t swim3_queue_rq(struct blk_mq_hw_ctx *hctx, 3098ccb8cb1SOmar Sandoval const struct blk_mq_queue_data *bd) 3101da177e4SLinus Torvalds { 3118ccb8cb1SOmar Sandoval struct floppy_state *fs = hctx->queue->queuedata; 3128ccb8cb1SOmar Sandoval struct request *req = bd->rq; 3131da177e4SLinus Torvalds unsigned long x; 3141da177e4SLinus Torvalds 3158ccb8cb1SOmar Sandoval spin_lock_irq(&swim3_lock); 3168ccb8cb1SOmar Sandoval if (fs->cur_req || fs->state != idle) { 3178ccb8cb1SOmar Sandoval spin_unlock_irq(&swim3_lock); 3188ccb8cb1SOmar Sandoval return BLK_STS_DEV_RESOURCE; 3191da177e4SLinus Torvalds } 3208ccb8cb1SOmar Sandoval blk_mq_start_request(req); 3218ccb8cb1SOmar Sandoval fs->cur_req = req; 322b3025457SBenjamin Herrenschmidt if (fs->mdev->media_bay && 323b3025457SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) { 324b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " media bay absent, dropping req\n"); 3252a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 3268ccb8cb1SOmar Sandoval goto out; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds if (fs->ejected) { 329b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " disk ejected\n"); 3302a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 3318ccb8cb1SOmar Sandoval goto out; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds if (rq_data_dir(req) == WRITE) { 3341da177e4SLinus Torvalds if (fs->write_prot < 0) 3351da177e4SLinus Torvalds fs->write_prot = swim3_readbit(fs, WRITE_PROT); 3361da177e4SLinus Torvalds if (fs->write_prot) { 337b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " try to write, disk write protected\n"); 3382a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 3398ccb8cb1SOmar Sandoval goto out; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3438ccb8cb1SOmar Sandoval /* 3448ccb8cb1SOmar Sandoval * Do not remove the cast. blk_rq_pos(req) is now a sector_t and can be 3458ccb8cb1SOmar Sandoval * 64 bits, but it will never go past 32 bits for this driver anyway, so 3468ccb8cb1SOmar Sandoval * we can safely cast it down and not have to do a 64/32 division 3471da177e4SLinus Torvalds */ 34883096ebfSTejun Heo fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl; 34983096ebfSTejun Heo x = ((long)blk_rq_pos(req)) % fs->secpercyl; 3501da177e4SLinus Torvalds fs->head = x / fs->secpertrack; 3511da177e4SLinus Torvalds fs->req_sector = x % fs->secpertrack + 1; 3521da177e4SLinus Torvalds fs->state = do_transfer; 3531da177e4SLinus Torvalds fs->retries = 0; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds act(fs); 3561da177e4SLinus Torvalds 3578ccb8cb1SOmar Sandoval out: 3588ccb8cb1SOmar Sandoval spin_unlock_irq(&swim3_lock); 3598ccb8cb1SOmar Sandoval return BLK_STS_OK; 360b3025457SBenjamin Herrenschmidt } 361b3025457SBenjamin Herrenschmidt 3621da177e4SLinus Torvalds static void set_timeout(struct floppy_state *fs, int nticks, 363b5775a6bSKees Cook void (*proc)(struct timer_list *t)) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds if (fs->timeout_pending) 3661da177e4SLinus Torvalds del_timer(&fs->timeout); 3671da177e4SLinus Torvalds fs->timeout.expires = jiffies + nticks; 368841b86f3SKees Cook fs->timeout.function = proc; 3691da177e4SLinus Torvalds add_timer(&fs->timeout); 3701da177e4SLinus Torvalds fs->timeout_pending = 1; 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 { 410f5718726SDavid Gibson cp->req_count = cpu_to_le16(count); 411f5718726SDavid Gibson cp->command = cpu_to_le16(cmd); 412f5718726SDavid Gibson cp->phy_addr = cpu_to_le32(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; 422b3025457SBenjamin Herrenschmidt struct request *req = fs->cur_req; 4231da177e4SLinus Torvalds 424b3025457SBenjamin Herrenschmidt if (blk_rq_cur_sectors(req) <= 0) { 425b3025457SBenjamin Herrenschmidt swim3_warn("%s", "Transfer 0 sectors ?\n"); 4261da177e4SLinus Torvalds return; 4271da177e4SLinus Torvalds } 428b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) 4291da177e4SLinus Torvalds n = 1; 4301da177e4SLinus Torvalds else { 4311da177e4SLinus Torvalds n = fs->secpertrack - fs->req_sector + 1; 432b3025457SBenjamin Herrenschmidt if (n > blk_rq_cur_sectors(req)) 433b3025457SBenjamin Herrenschmidt n = blk_rq_cur_sectors(req); 4341da177e4SLinus Torvalds } 435b3025457SBenjamin Herrenschmidt 436b3025457SBenjamin Herrenschmidt swim3_dbg(" setup xfer at sect %d (of %d) head %d for %d\n", 437b3025457SBenjamin Herrenschmidt fs->req_sector, fs->secpertrack, fs->head, n); 438b3025457SBenjamin Herrenschmidt 4391da177e4SLinus Torvalds fs->scount = n; 4401da177e4SLinus Torvalds swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0); 4411da177e4SLinus Torvalds out_8(&sw->sector, fs->req_sector); 4421da177e4SLinus Torvalds out_8(&sw->nsect, n); 4431da177e4SLinus Torvalds out_8(&sw->gap3, 0); 4441da177e4SLinus Torvalds out_le32(&dr->cmdptr, virt_to_bus(cp)); 445b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) { 4461da177e4SLinus Torvalds /* Set up 3 dma commands: write preamble, data, postamble */ 4471da177e4SLinus Torvalds init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble)); 4481da177e4SLinus Torvalds ++cp; 449b4f42e28SJens Axboe init_dma(cp, OUTPUT_MORE, bio_data(req->bio), 512); 4501da177e4SLinus Torvalds ++cp; 4511da177e4SLinus Torvalds init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble)); 4521da177e4SLinus Torvalds } else { 453b4f42e28SJens Axboe init_dma(cp, INPUT_LAST, bio_data(req->bio), n * 512); 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds ++cp; 4561da177e4SLinus Torvalds out_le16(&cp->command, DBDMA_STOP); 4571da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 4581da177e4SLinus Torvalds in_8(&sw->error); 4591da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 460b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) 4611da177e4SLinus Torvalds out_8(&sw->control_bis, WRITE_SECTORS); 4621da177e4SLinus Torvalds in_8(&sw->intr); 4631da177e4SLinus Torvalds out_le32(&dr->control, (RUN << 16) | RUN); 4641da177e4SLinus Torvalds /* enable intr when transfer complete */ 4651da177e4SLinus Torvalds out_8(&sw->intr_enable, TRANSFER_DONE); 4661da177e4SLinus Torvalds out_8(&sw->control_bis, DO_ACTION); 4671da177e4SLinus Torvalds set_timeout(fs, 2*HZ, xfer_timeout); /* enable timeout */ 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds static void act(struct floppy_state *fs) 4711da177e4SLinus Torvalds { 4721da177e4SLinus Torvalds for (;;) { 473b3025457SBenjamin Herrenschmidt swim3_dbg(" act loop, state=%d, req_cyl=%d, cur_cyl=%d\n", 474b3025457SBenjamin Herrenschmidt fs->state, fs->req_cyl, fs->cur_cyl); 475b3025457SBenjamin Herrenschmidt 4761da177e4SLinus Torvalds switch (fs->state) { 4771da177e4SLinus Torvalds case idle: 4781da177e4SLinus Torvalds return; /* XXX shouldn't get here */ 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds case locating: 4811da177e4SLinus Torvalds if (swim3_readbit(fs, TRACK_ZERO)) { 482b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " locate track 0\n"); 4831da177e4SLinus Torvalds fs->cur_cyl = 0; 4841da177e4SLinus Torvalds if (fs->req_cyl == 0) 4851da177e4SLinus Torvalds fs->state = do_transfer; 4861da177e4SLinus Torvalds else 4871da177e4SLinus Torvalds fs->state = seeking; 4881da177e4SLinus Torvalds break; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds scan_track(fs); 4911da177e4SLinus Torvalds return; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds case seeking: 4941da177e4SLinus Torvalds if (fs->cur_cyl < 0) { 4951da177e4SLinus Torvalds fs->expect_cyl = -1; 4961da177e4SLinus Torvalds fs->state = locating; 4971da177e4SLinus Torvalds break; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds if (fs->req_cyl == fs->cur_cyl) { 500b3025457SBenjamin Herrenschmidt swim3_warn("%s", "Whoops, seeking 0\n"); 5011da177e4SLinus Torvalds fs->state = do_transfer; 5021da177e4SLinus Torvalds break; 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds seek_track(fs, fs->req_cyl - fs->cur_cyl); 5051da177e4SLinus Torvalds return; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds case settling: 5081da177e4SLinus Torvalds /* check for SEEK_COMPLETE after 30ms */ 5091da177e4SLinus Torvalds fs->settle_time = (HZ + 32) / 33; 5101da177e4SLinus Torvalds set_timeout(fs, fs->settle_time, settle_timeout); 5111da177e4SLinus Torvalds return; 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds case do_transfer: 5141da177e4SLinus Torvalds if (fs->cur_cyl != fs->req_cyl) { 5151da177e4SLinus Torvalds if (fs->retries > 5) { 516b3025457SBenjamin Herrenschmidt swim3_err("Wrong cylinder in transfer, want: %d got %d\n", 517b3025457SBenjamin Herrenschmidt fs->req_cyl, fs->cur_cyl); 5182a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 5191da177e4SLinus Torvalds fs->state = idle; 5201da177e4SLinus Torvalds return; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds fs->state = seeking; 5231da177e4SLinus Torvalds break; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds setup_transfer(fs); 5261da177e4SLinus Torvalds return; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds case jogging: 5291da177e4SLinus Torvalds seek_track(fs, -5); 5301da177e4SLinus Torvalds return; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds default: 533b3025457SBenjamin Herrenschmidt swim3_err("Unknown state %d\n", fs->state); 5341da177e4SLinus Torvalds return; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 539b5775a6bSKees Cook static void scan_timeout(struct timer_list *t) 5401da177e4SLinus Torvalds { 541b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 5421da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 543b3025457SBenjamin Herrenschmidt unsigned long flags; 5441da177e4SLinus Torvalds 545b3025457SBenjamin Herrenschmidt swim3_dbg("* scan timeout, state=%d\n", fs->state); 546b3025457SBenjamin Herrenschmidt 547b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 5481da177e4SLinus Torvalds fs->timeout_pending = 0; 5491da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 5501da177e4SLinus Torvalds out_8(&sw->select, RELAX); 5511da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 5521da177e4SLinus Torvalds fs->cur_cyl = -1; 5531da177e4SLinus Torvalds if (fs->retries > 5) { 5542a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 5551da177e4SLinus Torvalds fs->state = idle; 5561da177e4SLinus Torvalds } else { 5571da177e4SLinus Torvalds fs->state = jogging; 5581da177e4SLinus Torvalds act(fs); 5591da177e4SLinus Torvalds } 560b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 563b5775a6bSKees Cook static void seek_timeout(struct timer_list *t) 5641da177e4SLinus Torvalds { 565b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 5661da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 567b3025457SBenjamin Herrenschmidt unsigned long flags; 5681da177e4SLinus Torvalds 569b3025457SBenjamin Herrenschmidt swim3_dbg("* seek timeout, state=%d\n", fs->state); 570b3025457SBenjamin Herrenschmidt 571b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 5721da177e4SLinus Torvalds fs->timeout_pending = 0; 5731da177e4SLinus Torvalds out_8(&sw->control_bic, DO_SEEK); 5741da177e4SLinus Torvalds out_8(&sw->select, RELAX); 5751da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 576b3025457SBenjamin Herrenschmidt swim3_err("%s", "Seek timeout\n"); 5772a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 5781da177e4SLinus Torvalds fs->state = idle; 579b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 582b5775a6bSKees Cook static void settle_timeout(struct timer_list *t) 5831da177e4SLinus Torvalds { 584b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 5851da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 586b3025457SBenjamin Herrenschmidt unsigned long flags; 5871da177e4SLinus Torvalds 588b3025457SBenjamin Herrenschmidt swim3_dbg("* settle timeout, state=%d\n", fs->state); 589b3025457SBenjamin Herrenschmidt 590b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 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); 596b3025457SBenjamin Herrenschmidt goto unlock; 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); 602b3025457SBenjamin Herrenschmidt goto unlock; 6031da177e4SLinus Torvalds } 604b3025457SBenjamin Herrenschmidt swim3_err("%s", "Seek settle timeout\n"); 6052a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 6061da177e4SLinus Torvalds fs->state = idle; 607b3025457SBenjamin Herrenschmidt unlock: 608b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 611b5775a6bSKees Cook static void xfer_timeout(struct timer_list *t) 6121da177e4SLinus Torvalds { 613b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 6141da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 6151da177e4SLinus Torvalds struct dbdma_regs __iomem *dr = fs->dma; 616b3025457SBenjamin Herrenschmidt unsigned long flags; 6171da177e4SLinus Torvalds int n; 6181da177e4SLinus Torvalds 619b3025457SBenjamin Herrenschmidt swim3_dbg("* xfer timeout, state=%d\n", fs->state); 620b3025457SBenjamin Herrenschmidt 621b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 6221da177e4SLinus Torvalds fs->timeout_pending = 0; 6231da177e4SLinus Torvalds out_le32(&dr->control, RUN << 16); 6241da177e4SLinus Torvalds /* We must wait a bit for dbdma to stop */ 6251da177e4SLinus Torvalds for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++) 6261da177e4SLinus Torvalds udelay(1); 6271da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 6281da177e4SLinus Torvalds out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); 6291da177e4SLinus Torvalds out_8(&sw->select, RELAX); 630b3025457SBenjamin Herrenschmidt swim3_err("Timeout %sing sector %ld\n", 631b3025457SBenjamin Herrenschmidt (rq_data_dir(fs->cur_req)==WRITE? "writ": "read"), 632b3025457SBenjamin Herrenschmidt (long)blk_rq_pos(fs->cur_req)); 6332a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 6341da177e4SLinus Torvalds fs->state = idle; 635b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6387d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id) 6391da177e4SLinus Torvalds { 6401da177e4SLinus Torvalds struct floppy_state *fs = (struct floppy_state *) dev_id; 6411da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 6421da177e4SLinus Torvalds int intr, err, n; 6431da177e4SLinus Torvalds int stat, resid; 6441da177e4SLinus Torvalds struct dbdma_regs __iomem *dr; 6451da177e4SLinus Torvalds struct dbdma_cmd *cp; 646b3025457SBenjamin Herrenschmidt unsigned long flags; 647b3025457SBenjamin Herrenschmidt struct request *req = fs->cur_req; 6481da177e4SLinus Torvalds 649b3025457SBenjamin Herrenschmidt swim3_dbg("* interrupt, state=%d\n", fs->state); 650b3025457SBenjamin Herrenschmidt 651b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 6521da177e4SLinus Torvalds intr = in_8(&sw->intr); 6531da177e4SLinus Torvalds err = (intr & ERROR_INTR)? in_8(&sw->error): 0; 6541da177e4SLinus Torvalds if ((intr & ERROR_INTR) && fs->state != do_transfer) 655b3025457SBenjamin Herrenschmidt swim3_err("Non-transfer error interrupt: state=%d, dir=%x, intr=%x, err=%x\n", 656b3025457SBenjamin Herrenschmidt fs->state, rq_data_dir(req), intr, err); 6571da177e4SLinus Torvalds switch (fs->state) { 6581da177e4SLinus Torvalds case locating: 6591da177e4SLinus Torvalds if (intr & SEEN_SECTOR) { 6601da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 6611da177e4SLinus Torvalds out_8(&sw->select, RELAX); 6621da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 6631da177e4SLinus Torvalds del_timer(&fs->timeout); 6641da177e4SLinus Torvalds fs->timeout_pending = 0; 6651da177e4SLinus Torvalds if (sw->ctrack == 0xff) { 666b3025457SBenjamin Herrenschmidt swim3_err("%s", "Seen sector but cyl=ff?\n"); 6671da177e4SLinus Torvalds fs->cur_cyl = -1; 6681da177e4SLinus Torvalds if (fs->retries > 5) { 6692a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 6701da177e4SLinus Torvalds fs->state = idle; 6711da177e4SLinus Torvalds } else { 6721da177e4SLinus Torvalds fs->state = jogging; 6731da177e4SLinus Torvalds act(fs); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds break; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds fs->cur_cyl = sw->ctrack; 6781da177e4SLinus Torvalds fs->cur_sector = sw->csect; 6791da177e4SLinus Torvalds if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl) 680b3025457SBenjamin Herrenschmidt swim3_err("Expected cyl %d, got %d\n", 6811da177e4SLinus Torvalds fs->expect_cyl, fs->cur_cyl); 6821da177e4SLinus Torvalds fs->state = do_transfer; 6831da177e4SLinus Torvalds act(fs); 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds break; 6861da177e4SLinus Torvalds case seeking: 6871da177e4SLinus Torvalds case jogging: 6881da177e4SLinus Torvalds if (sw->nseek == 0) { 6891da177e4SLinus Torvalds out_8(&sw->control_bic, DO_SEEK); 6901da177e4SLinus Torvalds out_8(&sw->select, RELAX); 6911da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 6921da177e4SLinus Torvalds del_timer(&fs->timeout); 6931da177e4SLinus Torvalds fs->timeout_pending = 0; 6941da177e4SLinus Torvalds if (fs->state == seeking) 6951da177e4SLinus Torvalds ++fs->retries; 6961da177e4SLinus Torvalds fs->state = settling; 6971da177e4SLinus Torvalds act(fs); 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds break; 7001da177e4SLinus Torvalds case settling: 7011da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 7021da177e4SLinus Torvalds del_timer(&fs->timeout); 7031da177e4SLinus Torvalds fs->timeout_pending = 0; 7041da177e4SLinus Torvalds act(fs); 7051da177e4SLinus Torvalds break; 7061da177e4SLinus Torvalds case do_transfer: 7071da177e4SLinus Torvalds if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0) 7081da177e4SLinus Torvalds break; 7091da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 7101da177e4SLinus Torvalds out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); 7111da177e4SLinus Torvalds out_8(&sw->select, RELAX); 7121da177e4SLinus Torvalds del_timer(&fs->timeout); 7131da177e4SLinus Torvalds fs->timeout_pending = 0; 7141da177e4SLinus Torvalds dr = fs->dma; 7151da177e4SLinus Torvalds cp = fs->dma_cmd; 716b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) 7171da177e4SLinus Torvalds ++cp; 7181da177e4SLinus Torvalds /* 7191da177e4SLinus Torvalds * Check that the main data transfer has finished. 7201da177e4SLinus Torvalds * On writing, the swim3 sometimes doesn't use 7211da177e4SLinus Torvalds * up all the bytes of the postamble, so we can still 7221da177e4SLinus Torvalds * see DMA active here. That doesn't matter as long 7231da177e4SLinus Torvalds * as all the sector data has been transferred. 7241da177e4SLinus Torvalds */ 7251da177e4SLinus Torvalds if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) { 7261da177e4SLinus Torvalds /* wait a little while for DMA to complete */ 7271da177e4SLinus Torvalds for (n = 0; n < 100; ++n) { 7281da177e4SLinus Torvalds if (cp->xfer_status != 0) 7291da177e4SLinus Torvalds break; 7301da177e4SLinus Torvalds udelay(1); 7311da177e4SLinus Torvalds barrier(); 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds /* turn off DMA */ 7351da177e4SLinus Torvalds out_le32(&dr->control, (RUN | PAUSE) << 16); 736f5718726SDavid Gibson stat = le16_to_cpu(cp->xfer_status); 737f5718726SDavid Gibson resid = le16_to_cpu(cp->res_count); 7381da177e4SLinus Torvalds if (intr & ERROR_INTR) { 7391da177e4SLinus Torvalds n = fs->scount - 1 - resid / 512; 7401da177e4SLinus Torvalds if (n > 0) { 741b3025457SBenjamin Herrenschmidt blk_update_request(req, 0, n << 9); 7421da177e4SLinus Torvalds fs->req_sector += n; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds if (fs->retries < 5) { 7451da177e4SLinus Torvalds ++fs->retries; 7461da177e4SLinus Torvalds act(fs); 7471da177e4SLinus Torvalds } else { 748b3025457SBenjamin Herrenschmidt swim3_err("Error %sing block %ld (err=%x)\n", 749b3025457SBenjamin Herrenschmidt rq_data_dir(req) == WRITE? "writ": "read", 750b3025457SBenjamin Herrenschmidt (long)blk_rq_pos(req), err); 7512a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 7521da177e4SLinus Torvalds fs->state = idle; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds } else { 7551da177e4SLinus Torvalds if ((stat & ACTIVE) == 0 || resid != 0) { 7561da177e4SLinus Torvalds /* musta been an error */ 757b3025457SBenjamin Herrenschmidt swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid); 758b3025457SBenjamin Herrenschmidt swim3_err(" state=%d, dir=%x, intr=%x, err=%x\n", 759b3025457SBenjamin Herrenschmidt fs->state, rq_data_dir(req), intr, err); 7602a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 7611da177e4SLinus Torvalds fs->state = idle; 7621da177e4SLinus Torvalds break; 7631da177e4SLinus Torvalds } 764b3025457SBenjamin Herrenschmidt fs->retries = 0; 765b3025457SBenjamin Herrenschmidt if (swim3_end_request(fs, 0, fs->scount << 9)) { 7661da177e4SLinus Torvalds fs->req_sector += fs->scount; 7671da177e4SLinus Torvalds if (fs->req_sector > fs->secpertrack) { 7681da177e4SLinus Torvalds fs->req_sector -= fs->secpertrack; 7691da177e4SLinus Torvalds if (++fs->head > 1) { 7701da177e4SLinus Torvalds fs->head = 0; 7711da177e4SLinus Torvalds ++fs->req_cyl; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds act(fs); 775467ca759STejun Heo } else 776467ca759STejun Heo fs->state = idle; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds break; 7791da177e4SLinus Torvalds default: 780b3025457SBenjamin Herrenschmidt swim3_err("Don't know what to do in state %d\n", fs->state); 7811da177e4SLinus Torvalds } 782b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 7831da177e4SLinus Torvalds return IRQ_HANDLED; 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds /* 7877d12e780SDavid Howells static void fd_dma_interrupt(int irq, void *dev_id) 7881da177e4SLinus Torvalds { 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds */ 7911da177e4SLinus Torvalds 792b3025457SBenjamin Herrenschmidt /* Called under the mutex to grab exclusive access to a drive */ 7931da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state, 7941da177e4SLinus Torvalds int interruptible) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds unsigned long flags; 7971da177e4SLinus Torvalds 798b3025457SBenjamin Herrenschmidt swim3_dbg("%s", "-> grab drive\n"); 799b3025457SBenjamin Herrenschmidt 800b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 801b3025457SBenjamin Herrenschmidt if (fs->state != idle && fs->state != available) { 8021da177e4SLinus Torvalds ++fs->wanted; 803106fd892SArnd Bergmann /* this will enable irqs in order to sleep */ 804106fd892SArnd Bergmann if (!interruptible) 805106fd892SArnd Bergmann wait_event_lock_irq(fs->wait, 806106fd892SArnd Bergmann fs->state == available, 807106fd892SArnd Bergmann swim3_lock); 808106fd892SArnd Bergmann else if (wait_event_interruptible_lock_irq(fs->wait, 809106fd892SArnd Bergmann fs->state == available, 810106fd892SArnd Bergmann swim3_lock)) { 8111da177e4SLinus Torvalds --fs->wanted; 812106fd892SArnd Bergmann spin_unlock_irqrestore(&swim3_lock, flags); 8131da177e4SLinus Torvalds return -EINTR; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds --fs->wanted; 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds fs->state = state; 818b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 819b3025457SBenjamin Herrenschmidt 8201da177e4SLinus Torvalds return 0; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs) 8241da177e4SLinus Torvalds { 8258ccb8cb1SOmar Sandoval struct request_queue *q = disks[fs->index]->queue; 8261da177e4SLinus Torvalds unsigned long flags; 8271da177e4SLinus Torvalds 828b3025457SBenjamin Herrenschmidt swim3_dbg("%s", "-> release drive\n"); 829b3025457SBenjamin Herrenschmidt 830b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 8311da177e4SLinus Torvalds fs->state = idle; 832b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 8338ccb8cb1SOmar Sandoval 8348ccb8cb1SOmar Sandoval blk_mq_freeze_queue(q); 8358ccb8cb1SOmar Sandoval blk_mq_quiesce_queue(q); 8368ccb8cb1SOmar Sandoval blk_mq_unquiesce_queue(q); 8378ccb8cb1SOmar Sandoval blk_mq_unfreeze_queue(q); 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs) 8411da177e4SLinus Torvalds { 8421da177e4SLinus Torvalds int err, n; 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds err = grab_drive(fs, ejecting, 1); 8451da177e4SLinus Torvalds if (err) 8461da177e4SLinus Torvalds return err; 8471da177e4SLinus Torvalds swim3_action(fs, EJECT); 8481da177e4SLinus Torvalds for (n = 20; n > 0; --n) { 8491da177e4SLinus Torvalds if (signal_pending(current)) { 8501da177e4SLinus Torvalds err = -EINTR; 8511da177e4SLinus Torvalds break; 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds swim3_select(fs, RELAX); 85486e84862SNishanth Aravamudan schedule_timeout_interruptible(1); 8551da177e4SLinus Torvalds if (swim3_readbit(fs, DISK_IN) == 0) 8561da177e4SLinus Torvalds break; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds swim3_select(fs, RELAX); 8591da177e4SLinus Torvalds udelay(150); 8601da177e4SLinus Torvalds fs->ejected = 1; 8611da177e4SLinus Torvalds release_drive(fs); 8621da177e4SLinus Torvalds return err; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds static struct floppy_struct floppy_type = 8661da177e4SLinus Torvalds { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */ 8671da177e4SLinus Torvalds 8688a6cfeb6SArnd Bergmann static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode, 8691da177e4SLinus Torvalds unsigned int cmd, unsigned long param) 8701da177e4SLinus Torvalds { 871b4d9a442SAl Viro struct floppy_state *fs = bdev->bd_disk->private_data; 8721da177e4SLinus Torvalds int err; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)) 8751da177e4SLinus Torvalds return -EPERM; 8761da177e4SLinus Torvalds 877d58b0c39SBenjamin Herrenschmidt if (fs->mdev->media_bay && 878d58b0c39SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) 8791da177e4SLinus Torvalds return -ENXIO; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds switch (cmd) { 8821da177e4SLinus Torvalds case FDEJECT: 8831da177e4SLinus Torvalds if (fs->ref_count != 1) 8841da177e4SLinus Torvalds return -EBUSY; 8851da177e4SLinus Torvalds err = fd_eject(fs); 8861da177e4SLinus Torvalds return err; 8871da177e4SLinus Torvalds case FDGETPRM: 8881da177e4SLinus Torvalds if (copy_to_user((void __user *) param, &floppy_type, 8891da177e4SLinus Torvalds sizeof(struct floppy_struct))) 8901da177e4SLinus Torvalds return -EFAULT; 8911da177e4SLinus Torvalds return 0; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds return -ENOTTY; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 8968a6cfeb6SArnd Bergmann static int floppy_ioctl(struct block_device *bdev, fmode_t mode, 8978a6cfeb6SArnd Bergmann unsigned int cmd, unsigned long param) 8988a6cfeb6SArnd Bergmann { 8998a6cfeb6SArnd Bergmann int ret; 9008a6cfeb6SArnd Bergmann 9012a48fc0aSArnd Bergmann mutex_lock(&swim3_mutex); 9028a6cfeb6SArnd Bergmann ret = floppy_locked_ioctl(bdev, mode, cmd, param); 9032a48fc0aSArnd Bergmann mutex_unlock(&swim3_mutex); 9048a6cfeb6SArnd Bergmann 9058a6cfeb6SArnd Bergmann return ret; 9068a6cfeb6SArnd Bergmann } 9078a6cfeb6SArnd Bergmann 908b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode) 9091da177e4SLinus Torvalds { 910b4d9a442SAl Viro struct floppy_state *fs = bdev->bd_disk->private_data; 9111da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 9121da177e4SLinus Torvalds int n, err = 0; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds if (fs->ref_count == 0) { 915d58b0c39SBenjamin Herrenschmidt if (fs->mdev->media_bay && 916d58b0c39SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) 9171da177e4SLinus Torvalds return -ENXIO; 9181da177e4SLinus Torvalds out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2); 9191da177e4SLinus Torvalds out_8(&sw->control_bic, 0xff); 9201da177e4SLinus Torvalds out_8(&sw->mode, 0x95); 9211da177e4SLinus Torvalds udelay(10); 9221da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 9231da177e4SLinus Torvalds out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE); 9241da177e4SLinus Torvalds swim3_action(fs, MOTOR_ON); 9251da177e4SLinus Torvalds fs->write_prot = -1; 9261da177e4SLinus Torvalds fs->cur_cyl = -1; 9271da177e4SLinus Torvalds for (n = 0; n < 2 * HZ; ++n) { 9281da177e4SLinus Torvalds if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE)) 9291da177e4SLinus Torvalds break; 9301da177e4SLinus Torvalds if (signal_pending(current)) { 9311da177e4SLinus Torvalds err = -EINTR; 9321da177e4SLinus Torvalds break; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds swim3_select(fs, RELAX); 93586e84862SNishanth Aravamudan schedule_timeout_interruptible(1); 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0 9381da177e4SLinus Torvalds || swim3_readbit(fs, DISK_IN) == 0)) 9391da177e4SLinus Torvalds err = -ENXIO; 9401da177e4SLinus Torvalds swim3_action(fs, SETMFM); 9411da177e4SLinus Torvalds swim3_select(fs, RELAX); 9421da177e4SLinus Torvalds 943b4d9a442SAl Viro } else if (fs->ref_count == -1 || mode & FMODE_EXCL) 9441da177e4SLinus Torvalds return -EBUSY; 9451da177e4SLinus Torvalds 946b4d9a442SAl Viro if (err == 0 && (mode & FMODE_NDELAY) == 0 947b4d9a442SAl Viro && (mode & (FMODE_READ|FMODE_WRITE))) { 948b4d9a442SAl Viro check_disk_change(bdev); 9491da177e4SLinus Torvalds if (fs->ejected) 9501da177e4SLinus Torvalds err = -ENXIO; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 953b4d9a442SAl Viro if (err == 0 && (mode & FMODE_WRITE)) { 9541da177e4SLinus Torvalds if (fs->write_prot < 0) 9551da177e4SLinus Torvalds fs->write_prot = swim3_readbit(fs, WRITE_PROT); 9561da177e4SLinus Torvalds if (fs->write_prot) 9571da177e4SLinus Torvalds err = -EROFS; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds if (err) { 9611da177e4SLinus Torvalds if (fs->ref_count == 0) { 9621da177e4SLinus Torvalds swim3_action(fs, MOTOR_OFF); 9631da177e4SLinus Torvalds out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE); 9641da177e4SLinus Torvalds swim3_select(fs, RELAX); 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds return err; 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 969b4d9a442SAl Viro if (mode & FMODE_EXCL) 9701da177e4SLinus Torvalds fs->ref_count = -1; 9711da177e4SLinus Torvalds else 9721da177e4SLinus Torvalds ++fs->ref_count; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds return 0; 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds 9776e9624b8SArnd Bergmann static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode) 9786e9624b8SArnd Bergmann { 9796e9624b8SArnd Bergmann int ret; 9806e9624b8SArnd Bergmann 9812a48fc0aSArnd Bergmann mutex_lock(&swim3_mutex); 9826e9624b8SArnd Bergmann ret = floppy_open(bdev, mode); 9832a48fc0aSArnd Bergmann mutex_unlock(&swim3_mutex); 9846e9624b8SArnd Bergmann 9856e9624b8SArnd Bergmann return ret; 9866e9624b8SArnd Bergmann } 9876e9624b8SArnd Bergmann 988db2a144bSAl Viro static void floppy_release(struct gendisk *disk, fmode_t mode) 9891da177e4SLinus Torvalds { 990b4d9a442SAl Viro struct floppy_state *fs = disk->private_data; 9911da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 992b3025457SBenjamin Herrenschmidt 9932a48fc0aSArnd Bergmann mutex_lock(&swim3_mutex); 994296dcc40SFinn Thain if (fs->ref_count > 0) 995296dcc40SFinn Thain --fs->ref_count; 996296dcc40SFinn Thain else if (fs->ref_count == -1) 997296dcc40SFinn Thain fs->ref_count = 0; 998296dcc40SFinn Thain if (fs->ref_count == 0) { 9991da177e4SLinus Torvalds swim3_action(fs, MOTOR_OFF); 10001da177e4SLinus Torvalds out_8(&sw->control_bic, 0xff); 10011da177e4SLinus Torvalds swim3_select(fs, RELAX); 10021da177e4SLinus Torvalds } 10032a48fc0aSArnd Bergmann mutex_unlock(&swim3_mutex); 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10064bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk, 10074bbde777STejun Heo unsigned int clearing) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds struct floppy_state *fs = disk->private_data; 10104bbde777STejun Heo return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk) 10141da177e4SLinus Torvalds { 10151da177e4SLinus Torvalds struct floppy_state *fs = disk->private_data; 10161da177e4SLinus Torvalds struct swim3 __iomem *sw; 10171da177e4SLinus Torvalds int ret, n; 10181da177e4SLinus Torvalds 1019d58b0c39SBenjamin Herrenschmidt if (fs->mdev->media_bay && 1020d58b0c39SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) 10211da177e4SLinus Torvalds return -ENXIO; 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds sw = fs->swim3; 10241da177e4SLinus Torvalds grab_drive(fs, revalidating, 0); 10251da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 10261da177e4SLinus Torvalds out_8(&sw->control_bis, DRIVE_ENABLE); 10271da177e4SLinus Torvalds swim3_action(fs, MOTOR_ON); /* necessary? */ 10281da177e4SLinus Torvalds fs->write_prot = -1; 10291da177e4SLinus Torvalds fs->cur_cyl = -1; 10301da177e4SLinus Torvalds mdelay(1); 10311da177e4SLinus Torvalds for (n = HZ; n > 0; --n) { 10321da177e4SLinus Torvalds if (swim3_readbit(fs, SEEK_COMPLETE)) 10331da177e4SLinus Torvalds break; 10341da177e4SLinus Torvalds if (signal_pending(current)) 10351da177e4SLinus Torvalds break; 10361da177e4SLinus Torvalds swim3_select(fs, RELAX); 103786e84862SNishanth Aravamudan schedule_timeout_interruptible(1); 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds ret = swim3_readbit(fs, SEEK_COMPLETE) == 0 10401da177e4SLinus Torvalds || swim3_readbit(fs, DISK_IN) == 0; 10411da177e4SLinus Torvalds if (ret) 10421da177e4SLinus Torvalds swim3_action(fs, MOTOR_OFF); 10431da177e4SLinus Torvalds else { 10441da177e4SLinus Torvalds fs->ejected = 0; 10451da177e4SLinus Torvalds swim3_action(fs, SETMFM); 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds swim3_select(fs, RELAX); 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds release_drive(fs); 10501da177e4SLinus Torvalds return ret; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 105383d5cde4SAlexey Dobriyan static const struct block_device_operations floppy_fops = { 10546e9624b8SArnd Bergmann .open = floppy_unlocked_open, 1055b4d9a442SAl Viro .release = floppy_release, 10568a6cfeb6SArnd Bergmann .ioctl = floppy_ioctl, 10574bbde777STejun Heo .check_events = floppy_check_events, 10581da177e4SLinus Torvalds .revalidate_disk= floppy_revalidate, 10591da177e4SLinus Torvalds }; 10601da177e4SLinus Torvalds 10618ccb8cb1SOmar Sandoval static const struct blk_mq_ops swim3_mq_ops = { 10628ccb8cb1SOmar Sandoval .queue_rq = swim3_queue_rq, 10638ccb8cb1SOmar Sandoval }; 10648ccb8cb1SOmar Sandoval 1065b3025457SBenjamin Herrenschmidt static void swim3_mb_event(struct macio_dev* mdev, int mb_state) 1066b3025457SBenjamin Herrenschmidt { 1067b3025457SBenjamin Herrenschmidt struct floppy_state *fs = macio_get_drvdata(mdev); 10687414d4f6SCong Ding struct swim3 __iomem *sw; 1069b3025457SBenjamin Herrenschmidt 1070b3025457SBenjamin Herrenschmidt if (!fs) 1071b3025457SBenjamin Herrenschmidt return; 10727414d4f6SCong Ding 10737414d4f6SCong Ding sw = fs->swim3; 10747414d4f6SCong Ding 1075b3025457SBenjamin Herrenschmidt if (mb_state != MB_FD) 1076b3025457SBenjamin Herrenschmidt return; 1077b3025457SBenjamin Herrenschmidt 1078b3025457SBenjamin Herrenschmidt /* Clear state */ 1079b3025457SBenjamin Herrenschmidt out_8(&sw->intr_enable, 0); 1080b3025457SBenjamin Herrenschmidt in_8(&sw->intr); 1081b3025457SBenjamin Herrenschmidt in_8(&sw->error); 1082b3025457SBenjamin Herrenschmidt } 1083b3025457SBenjamin Herrenschmidt 10843e9a6927SBenjamin Herrenschmidt static int swim3_add_device(struct macio_dev *mdev, int index) 10851da177e4SLinus Torvalds { 108661c7a080SGrant Likely struct device_node *swim = mdev->ofdev.dev.of_node; 10873e9a6927SBenjamin Herrenschmidt struct floppy_state *fs = &floppy_states[index]; 10883e9a6927SBenjamin Herrenschmidt int rc = -EBUSY; 10891da177e4SLinus Torvalds 1090b3025457SBenjamin Herrenschmidt fs->mdev = mdev; 1091b3025457SBenjamin Herrenschmidt fs->index = index; 1092b3025457SBenjamin Herrenschmidt 10933e9a6927SBenjamin Herrenschmidt /* Check & Request resources */ 10943e9a6927SBenjamin Herrenschmidt if (macio_resource_count(mdev) < 2) { 1095b3025457SBenjamin Herrenschmidt swim3_err("%s", "No address in device-tree\n"); 10963e9a6927SBenjamin Herrenschmidt return -ENXIO; 1097cc5d0189SBenjamin Herrenschmidt } 1098b3025457SBenjamin Herrenschmidt if (macio_irq_count(mdev) < 1) { 1099b3025457SBenjamin Herrenschmidt swim3_err("%s", "No interrupt in device-tree\n"); 1100b3025457SBenjamin Herrenschmidt return -ENXIO; 1101cc5d0189SBenjamin Herrenschmidt } 11023e9a6927SBenjamin Herrenschmidt if (macio_request_resource(mdev, 0, "swim3 (mmio)")) { 1103b3025457SBenjamin Herrenschmidt swim3_err("%s", "Can't request mmio resource\n"); 11043e9a6927SBenjamin Herrenschmidt return -EBUSY; 11051da177e4SLinus Torvalds } 11063e9a6927SBenjamin Herrenschmidt if (macio_request_resource(mdev, 1, "swim3 (dma)")) { 1107b3025457SBenjamin Herrenschmidt swim3_err("%s", "Can't request dma resource\n"); 11083e9a6927SBenjamin Herrenschmidt macio_release_resource(mdev, 0); 11093e9a6927SBenjamin Herrenschmidt return -EBUSY; 11103e9a6927SBenjamin Herrenschmidt } 11113e9a6927SBenjamin Herrenschmidt dev_set_drvdata(&mdev->ofdev.dev, fs); 11121da177e4SLinus Torvalds 1113d58b0c39SBenjamin Herrenschmidt if (mdev->media_bay == NULL) 11141da177e4SLinus Torvalds pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1); 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds fs->state = idle; 11173e9a6927SBenjamin Herrenschmidt fs->swim3 = (struct swim3 __iomem *) 11183e9a6927SBenjamin Herrenschmidt ioremap(macio_resource_start(mdev, 0), 0x200); 11193e9a6927SBenjamin Herrenschmidt if (fs->swim3 == NULL) { 1120b3025457SBenjamin Herrenschmidt swim3_err("%s", "Couldn't map mmio registers\n"); 11213e9a6927SBenjamin Herrenschmidt rc = -ENOMEM; 11223e9a6927SBenjamin Herrenschmidt goto out_release; 11233e9a6927SBenjamin Herrenschmidt } 11243e9a6927SBenjamin Herrenschmidt fs->dma = (struct dbdma_regs __iomem *) 11253e9a6927SBenjamin Herrenschmidt ioremap(macio_resource_start(mdev, 1), 0x200); 11263e9a6927SBenjamin Herrenschmidt if (fs->dma == NULL) { 1127b3025457SBenjamin Herrenschmidt swim3_err("%s", "Couldn't map dma registers\n"); 11283e9a6927SBenjamin Herrenschmidt iounmap(fs->swim3); 11293e9a6927SBenjamin Herrenschmidt rc = -ENOMEM; 11303e9a6927SBenjamin Herrenschmidt goto out_release; 11313e9a6927SBenjamin Herrenschmidt } 11323e9a6927SBenjamin Herrenschmidt fs->swim3_intr = macio_irq(mdev, 0); 1133a419aef8SJoe Perches fs->dma_intr = macio_irq(mdev, 1); 11341da177e4SLinus Torvalds fs->cur_cyl = -1; 11351da177e4SLinus Torvalds fs->cur_sector = -1; 11361da177e4SLinus Torvalds fs->secpercyl = 36; 11371da177e4SLinus Torvalds fs->secpertrack = 18; 11381da177e4SLinus Torvalds fs->total_secs = 2880; 11391da177e4SLinus Torvalds init_waitqueue_head(&fs->wait); 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space); 11421da177e4SLinus Torvalds memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd)); 1143f5718726SDavid Gibson fs->dma_cmd[1].command = cpu_to_le16(DBDMA_STOP); 11441da177e4SLinus Torvalds 1145b3025457SBenjamin Herrenschmidt if (mdev->media_bay == NULL || check_media_bay(mdev->media_bay) == MB_FD) 1146b3025457SBenjamin Herrenschmidt swim3_mb_event(mdev, MB_FD); 1147b3025457SBenjamin Herrenschmidt 11481da177e4SLinus Torvalds if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) { 1149b3025457SBenjamin Herrenschmidt swim3_err("%s", "Couldn't request interrupt\n"); 11501da177e4SLinus Torvalds pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0); 11513e9a6927SBenjamin Herrenschmidt goto out_unmap; 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds 1154b5775a6bSKees Cook timer_setup(&fs->timeout, NULL, 0); 11551da177e4SLinus Torvalds 1156b3025457SBenjamin Herrenschmidt swim3_info("SWIM3 floppy controller %s\n", 1157d58b0c39SBenjamin Herrenschmidt mdev->media_bay ? "in media bay" : ""); 11581da177e4SLinus Torvalds 11593e9a6927SBenjamin Herrenschmidt return 0; 11601da177e4SLinus Torvalds 11613e9a6927SBenjamin Herrenschmidt out_unmap: 11623e9a6927SBenjamin Herrenschmidt iounmap(fs->dma); 11633e9a6927SBenjamin Herrenschmidt iounmap(fs->swim3); 11643e9a6927SBenjamin Herrenschmidt 11653e9a6927SBenjamin Herrenschmidt out_release: 11663e9a6927SBenjamin Herrenschmidt macio_release_resource(mdev, 0); 11673e9a6927SBenjamin Herrenschmidt macio_release_resource(mdev, 1); 11683e9a6927SBenjamin Herrenschmidt 11693e9a6927SBenjamin Herrenschmidt return rc; 11703e9a6927SBenjamin Herrenschmidt } 11713e9a6927SBenjamin Herrenschmidt 11728d85fce7SGreg Kroah-Hartman static int swim3_attach(struct macio_dev *mdev, 11738d85fce7SGreg Kroah-Hartman const struct of_device_id *match) 11743e9a6927SBenjamin Herrenschmidt { 1175dbaa54b6SOmar Sandoval struct floppy_state *fs; 11763e9a6927SBenjamin Herrenschmidt struct gendisk *disk; 1177dbaa54b6SOmar Sandoval int rc; 1178b3025457SBenjamin Herrenschmidt 1179dbaa54b6SOmar Sandoval if (floppy_count >= MAX_FLOPPIES) 1180b3025457SBenjamin Herrenschmidt return -ENXIO; 11813e9a6927SBenjamin Herrenschmidt 1182dbaa54b6SOmar Sandoval if (floppy_count == 0) { 1183dbaa54b6SOmar Sandoval rc = register_blkdev(FLOPPY_MAJOR, "fd"); 11843e9a6927SBenjamin Herrenschmidt if (rc) 11853e9a6927SBenjamin Herrenschmidt return rc; 1186dbaa54b6SOmar Sandoval } 1187dbaa54b6SOmar Sandoval 1188dbaa54b6SOmar Sandoval disk = alloc_disk(1); 1189dbaa54b6SOmar Sandoval if (disk == NULL) { 1190dbaa54b6SOmar Sandoval rc = -ENOMEM; 1191dbaa54b6SOmar Sandoval goto out_unregister; 1192dbaa54b6SOmar Sandoval } 11938ccb8cb1SOmar Sandoval 1194427c5ce4SFinn Thain fs = &floppy_states[floppy_count]; 1195427c5ce4SFinn Thain memset(fs, 0, sizeof(*fs)); 1196427c5ce4SFinn Thain 11978ccb8cb1SOmar Sandoval disk->queue = blk_mq_init_sq_queue(&fs->tag_set, &swim3_mq_ops, 2, 11988ccb8cb1SOmar Sandoval BLK_MQ_F_SHOULD_MERGE); 11998ccb8cb1SOmar Sandoval if (IS_ERR(disk->queue)) { 12008ccb8cb1SOmar Sandoval rc = PTR_ERR(disk->queue); 12018ccb8cb1SOmar Sandoval disk->queue = NULL; 1202dbaa54b6SOmar Sandoval goto out_put_disk; 1203b3025457SBenjamin Herrenschmidt } 12048fc45044SChristoph Hellwig blk_queue_bounce_limit(disk->queue, BLK_BOUNCE_HIGH); 1205dbaa54b6SOmar Sandoval disk->queue->queuedata = fs; 12063e9a6927SBenjamin Herrenschmidt 1207dbaa54b6SOmar Sandoval rc = swim3_add_device(mdev, floppy_count); 1208dbaa54b6SOmar Sandoval if (rc) 1209dbaa54b6SOmar Sandoval goto out_cleanup_queue; 12103e9a6927SBenjamin Herrenschmidt 12113e9a6927SBenjamin Herrenschmidt disk->major = FLOPPY_MAJOR; 1212dbaa54b6SOmar Sandoval disk->first_minor = floppy_count; 12133e9a6927SBenjamin Herrenschmidt disk->fops = &floppy_fops; 1214dbaa54b6SOmar Sandoval disk->private_data = fs; 1215773008f6SMartin Wilck disk->events = DISK_EVENT_MEDIA_CHANGE; 12163e9a6927SBenjamin Herrenschmidt disk->flags |= GENHD_FL_REMOVABLE; 1217dbaa54b6SOmar Sandoval sprintf(disk->disk_name, "fd%d", floppy_count); 12183e9a6927SBenjamin Herrenschmidt set_capacity(disk, 2880); 12193e9a6927SBenjamin Herrenschmidt add_disk(disk); 12203e9a6927SBenjamin Herrenschmidt 1221dbaa54b6SOmar Sandoval disks[floppy_count++] = disk; 12223e9a6927SBenjamin Herrenschmidt return 0; 1223dbaa54b6SOmar Sandoval 1224dbaa54b6SOmar Sandoval out_cleanup_queue: 1225dbaa54b6SOmar Sandoval blk_cleanup_queue(disk->queue); 1226dbaa54b6SOmar Sandoval disk->queue = NULL; 12278ccb8cb1SOmar Sandoval blk_mq_free_tag_set(&fs->tag_set); 1228dbaa54b6SOmar Sandoval out_put_disk: 1229dbaa54b6SOmar Sandoval put_disk(disk); 1230dbaa54b6SOmar Sandoval out_unregister: 1231dbaa54b6SOmar Sandoval if (floppy_count == 0) 1232dbaa54b6SOmar Sandoval unregister_blkdev(FLOPPY_MAJOR, "fd"); 1233dbaa54b6SOmar Sandoval return rc; 12343e9a6927SBenjamin Herrenschmidt } 12353e9a6927SBenjamin Herrenschmidt 1236cc3f2e9fSArvind Yadav static const struct of_device_id swim3_match[] = 12373e9a6927SBenjamin Herrenschmidt { 12383e9a6927SBenjamin Herrenschmidt { 12393e9a6927SBenjamin Herrenschmidt .name = "swim3", 12403e9a6927SBenjamin Herrenschmidt }, 12413e9a6927SBenjamin Herrenschmidt { 12423e9a6927SBenjamin Herrenschmidt .compatible = "ohare-swim3" 12433e9a6927SBenjamin Herrenschmidt }, 12443e9a6927SBenjamin Herrenschmidt { 12453e9a6927SBenjamin Herrenschmidt .compatible = "swim3" 12463e9a6927SBenjamin Herrenschmidt }, 1247f41c53a5SAxel Lin { /* end of list */ } 12483e9a6927SBenjamin Herrenschmidt }; 12493e9a6927SBenjamin Herrenschmidt 12503e9a6927SBenjamin Herrenschmidt static struct macio_driver swim3_driver = 12513e9a6927SBenjamin Herrenschmidt { 1252c2cdf6abSBenjamin Herrenschmidt .driver = { 12533e9a6927SBenjamin Herrenschmidt .name = "swim3", 1254c2cdf6abSBenjamin Herrenschmidt .of_match_table = swim3_match, 1255c2cdf6abSBenjamin Herrenschmidt }, 12563e9a6927SBenjamin Herrenschmidt .probe = swim3_attach, 1257b3025457SBenjamin Herrenschmidt #ifdef CONFIG_PMAC_MEDIABAY 1258b3025457SBenjamin Herrenschmidt .mediabay_event = swim3_mb_event, 1259b3025457SBenjamin Herrenschmidt #endif 12603e9a6927SBenjamin Herrenschmidt #if 0 12613e9a6927SBenjamin Herrenschmidt .suspend = swim3_suspend, 12623e9a6927SBenjamin Herrenschmidt .resume = swim3_resume, 12633e9a6927SBenjamin Herrenschmidt #endif 12643e9a6927SBenjamin Herrenschmidt }; 12653e9a6927SBenjamin Herrenschmidt 12663e9a6927SBenjamin Herrenschmidt 12673e9a6927SBenjamin Herrenschmidt int swim3_init(void) 12683e9a6927SBenjamin Herrenschmidt { 12693e9a6927SBenjamin Herrenschmidt macio_register_driver(&swim3_driver); 12701da177e4SLinus Torvalds return 0; 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds module_init(swim3_init) 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 12761da177e4SLinus Torvalds MODULE_AUTHOR("Paul Mackerras"); 12771da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR); 1278