12874c5fdSThomas 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 act(struct floppy_state *fs); 238b5775a6bSKees Cook static void scan_timeout(struct timer_list *t); 239b5775a6bSKees Cook static void seek_timeout(struct timer_list *t); 240b5775a6bSKees Cook static void settle_timeout(struct timer_list *t); 241b5775a6bSKees Cook static void xfer_timeout(struct timer_list *t); 2427d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id); 2437d12e780SDavid Howells /*static void fd_dma_interrupt(int irq, void *dev_id);*/ 2441da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state, 2451da177e4SLinus Torvalds int interruptible); 2461da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs); 2471da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs); 248b4d9a442SAl Viro static int floppy_ioctl(struct block_device *bdev, fmode_t mode, 2491da177e4SLinus Torvalds unsigned int cmd, unsigned long param); 250b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode); 251db2a144bSAl Viro static void floppy_release(struct gendisk *disk, fmode_t mode); 2524bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk, 2534bbde777STejun Heo unsigned int clearing); 2541da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk); 2551da177e4SLinus Torvalds 2562a842acaSChristoph Hellwig static bool swim3_end_request(struct floppy_state *fs, blk_status_t err, unsigned int nr_bytes) 257f4bd4b90STejun Heo { 258b3025457SBenjamin Herrenschmidt struct request *req = fs->cur_req; 259b3025457SBenjamin Herrenschmidt 260b3025457SBenjamin Herrenschmidt swim3_dbg(" end request, err=%d nr_bytes=%d, cur_req=%p\n", 261b3025457SBenjamin Herrenschmidt err, nr_bytes, req); 262b3025457SBenjamin Herrenschmidt 263b3025457SBenjamin Herrenschmidt if (err) 264b3025457SBenjamin Herrenschmidt nr_bytes = blk_rq_cur_bytes(req); 2658ccb8cb1SOmar Sandoval if (blk_update_request(req, err, nr_bytes)) 266f4bd4b90STejun Heo return true; 2678ccb8cb1SOmar Sandoval __blk_mq_end_request(req, err); 268b3025457SBenjamin Herrenschmidt fs->cur_req = NULL; 269f4bd4b90STejun Heo return false; 270f4bd4b90STejun Heo } 271f4bd4b90STejun Heo 2721da177e4SLinus Torvalds static void swim3_select(struct floppy_state *fs, int sel) 2731da177e4SLinus Torvalds { 2741da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds out_8(&sw->select, RELAX); 2771da177e4SLinus Torvalds if (sel & 8) 2781da177e4SLinus Torvalds out_8(&sw->control_bis, SELECT); 2791da177e4SLinus Torvalds else 2801da177e4SLinus Torvalds out_8(&sw->control_bic, SELECT); 2811da177e4SLinus Torvalds out_8(&sw->select, sel & CA_MASK); 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds static void swim3_action(struct floppy_state *fs, int action) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds swim3_select(fs, action); 2891da177e4SLinus Torvalds udelay(1); 2901da177e4SLinus Torvalds out_8(&sw->select, sw->select | LSTRB); 2911da177e4SLinus Torvalds udelay(2); 2921da177e4SLinus Torvalds out_8(&sw->select, sw->select & ~LSTRB); 2931da177e4SLinus Torvalds udelay(1); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds static int swim3_readbit(struct floppy_state *fs, int bit) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 2991da177e4SLinus Torvalds int stat; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds swim3_select(fs, bit); 3021da177e4SLinus Torvalds udelay(1); 3031da177e4SLinus Torvalds stat = in_8(&sw->status); 3041da177e4SLinus Torvalds return (stat & DATA) == 0; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3078ccb8cb1SOmar Sandoval static blk_status_t swim3_queue_rq(struct blk_mq_hw_ctx *hctx, 3088ccb8cb1SOmar Sandoval const struct blk_mq_queue_data *bd) 3091da177e4SLinus Torvalds { 3108ccb8cb1SOmar Sandoval struct floppy_state *fs = hctx->queue->queuedata; 3118ccb8cb1SOmar Sandoval struct request *req = bd->rq; 3121da177e4SLinus Torvalds unsigned long x; 3131da177e4SLinus Torvalds 3148ccb8cb1SOmar Sandoval spin_lock_irq(&swim3_lock); 3158ccb8cb1SOmar Sandoval if (fs->cur_req || fs->state != idle) { 3168ccb8cb1SOmar Sandoval spin_unlock_irq(&swim3_lock); 3178ccb8cb1SOmar Sandoval return BLK_STS_DEV_RESOURCE; 3181da177e4SLinus Torvalds } 3198ccb8cb1SOmar Sandoval blk_mq_start_request(req); 3208ccb8cb1SOmar Sandoval fs->cur_req = req; 321b3025457SBenjamin Herrenschmidt if (fs->mdev->media_bay && 322b3025457SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) { 323b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " media bay absent, dropping req\n"); 3242a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 3258ccb8cb1SOmar Sandoval goto out; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds if (fs->ejected) { 328b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " disk ejected\n"); 3292a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 3308ccb8cb1SOmar Sandoval goto out; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds if (rq_data_dir(req) == WRITE) { 3331da177e4SLinus Torvalds if (fs->write_prot < 0) 3341da177e4SLinus Torvalds fs->write_prot = swim3_readbit(fs, WRITE_PROT); 3351da177e4SLinus Torvalds if (fs->write_prot) { 336b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " try to write, disk write protected\n"); 3372a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 3388ccb8cb1SOmar Sandoval goto out; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3428ccb8cb1SOmar Sandoval /* 3438ccb8cb1SOmar Sandoval * Do not remove the cast. blk_rq_pos(req) is now a sector_t and can be 3448ccb8cb1SOmar Sandoval * 64 bits, but it will never go past 32 bits for this driver anyway, so 3458ccb8cb1SOmar Sandoval * we can safely cast it down and not have to do a 64/32 division 3461da177e4SLinus Torvalds */ 34783096ebfSTejun Heo fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl; 34883096ebfSTejun Heo x = ((long)blk_rq_pos(req)) % fs->secpercyl; 3491da177e4SLinus Torvalds fs->head = x / fs->secpertrack; 3501da177e4SLinus Torvalds fs->req_sector = x % fs->secpertrack + 1; 3511da177e4SLinus Torvalds fs->state = do_transfer; 3521da177e4SLinus Torvalds fs->retries = 0; 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds act(fs); 3551da177e4SLinus Torvalds 3568ccb8cb1SOmar Sandoval out: 3578ccb8cb1SOmar Sandoval spin_unlock_irq(&swim3_lock); 3588ccb8cb1SOmar Sandoval return BLK_STS_OK; 359b3025457SBenjamin Herrenschmidt } 360b3025457SBenjamin Herrenschmidt 3611da177e4SLinus Torvalds static void set_timeout(struct floppy_state *fs, int nticks, 362b5775a6bSKees Cook void (*proc)(struct timer_list *t)) 3631da177e4SLinus Torvalds { 3641da177e4SLinus Torvalds if (fs->timeout_pending) 3651da177e4SLinus Torvalds del_timer(&fs->timeout); 3661da177e4SLinus Torvalds fs->timeout.expires = jiffies + nticks; 367841b86f3SKees Cook fs->timeout.function = proc; 3681da177e4SLinus Torvalds add_timer(&fs->timeout); 3691da177e4SLinus Torvalds fs->timeout_pending = 1; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds static inline void scan_track(struct floppy_state *fs) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds swim3_select(fs, READ_DATA_0); 3771da177e4SLinus Torvalds in_8(&sw->intr); /* clear SEEN_SECTOR bit */ 3781da177e4SLinus Torvalds in_8(&sw->error); 3791da177e4SLinus Torvalds out_8(&sw->intr_enable, SEEN_SECTOR); 3801da177e4SLinus Torvalds out_8(&sw->control_bis, DO_ACTION); 3811da177e4SLinus Torvalds /* enable intr when track found */ 3821da177e4SLinus Torvalds set_timeout(fs, HZ, scan_timeout); /* enable timeout */ 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds static inline void seek_track(struct floppy_state *fs, int n) 3861da177e4SLinus Torvalds { 3871da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds if (n >= 0) { 3901da177e4SLinus Torvalds swim3_action(fs, SEEK_POSITIVE); 3911da177e4SLinus Torvalds sw->nseek = n; 3921da177e4SLinus Torvalds } else { 3931da177e4SLinus Torvalds swim3_action(fs, SEEK_NEGATIVE); 3941da177e4SLinus Torvalds sw->nseek = -n; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1; 3971da177e4SLinus Torvalds swim3_select(fs, STEP); 3981da177e4SLinus Torvalds in_8(&sw->error); 3991da177e4SLinus Torvalds /* enable intr when seek finished */ 4001da177e4SLinus Torvalds out_8(&sw->intr_enable, SEEK_DONE); 4011da177e4SLinus Torvalds out_8(&sw->control_bis, DO_SEEK); 4021da177e4SLinus Torvalds set_timeout(fs, 3*HZ, seek_timeout); /* enable timeout */ 4031da177e4SLinus Torvalds fs->settle_time = 0; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 406*b60b270bSChristoph Hellwig /* 407*b60b270bSChristoph Hellwig * XXX: this is a horrible hack, but at least allows ppc32 to get 408*b60b270bSChristoph Hellwig * out of defining virt_to_bus, and this driver out of using the 409*b60b270bSChristoph Hellwig * deprecated block layer bounce buffering for highmem addresses 410*b60b270bSChristoph Hellwig * for no good reason. 411*b60b270bSChristoph Hellwig */ 412*b60b270bSChristoph Hellwig static unsigned long swim3_phys_to_bus(phys_addr_t paddr) 413*b60b270bSChristoph Hellwig { 414*b60b270bSChristoph Hellwig return paddr + PCI_DRAM_OFFSET; 415*b60b270bSChristoph Hellwig } 416*b60b270bSChristoph Hellwig 417*b60b270bSChristoph Hellwig static phys_addr_t swim3_bio_phys(struct bio *bio) 418*b60b270bSChristoph Hellwig { 419*b60b270bSChristoph Hellwig return page_to_phys(bio_page(bio)) + bio_offset(bio); 420*b60b270bSChristoph Hellwig } 421*b60b270bSChristoph Hellwig 4221da177e4SLinus Torvalds static inline void init_dma(struct dbdma_cmd *cp, int cmd, 423*b60b270bSChristoph Hellwig phys_addr_t paddr, int count) 4241da177e4SLinus Torvalds { 425f5718726SDavid Gibson cp->req_count = cpu_to_le16(count); 426f5718726SDavid Gibson cp->command = cpu_to_le16(cmd); 427*b60b270bSChristoph Hellwig cp->phy_addr = cpu_to_le32(swim3_phys_to_bus(paddr)); 4281da177e4SLinus Torvalds cp->xfer_status = 0; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds static inline void setup_transfer(struct floppy_state *fs) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds int n; 4341da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 4351da177e4SLinus Torvalds struct dbdma_cmd *cp = fs->dma_cmd; 4361da177e4SLinus Torvalds struct dbdma_regs __iomem *dr = fs->dma; 437b3025457SBenjamin Herrenschmidt struct request *req = fs->cur_req; 4381da177e4SLinus Torvalds 439b3025457SBenjamin Herrenschmidt if (blk_rq_cur_sectors(req) <= 0) { 440b3025457SBenjamin Herrenschmidt swim3_warn("%s", "Transfer 0 sectors ?\n"); 4411da177e4SLinus Torvalds return; 4421da177e4SLinus Torvalds } 443b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) 4441da177e4SLinus Torvalds n = 1; 4451da177e4SLinus Torvalds else { 4461da177e4SLinus Torvalds n = fs->secpertrack - fs->req_sector + 1; 447b3025457SBenjamin Herrenschmidt if (n > blk_rq_cur_sectors(req)) 448b3025457SBenjamin Herrenschmidt n = blk_rq_cur_sectors(req); 4491da177e4SLinus Torvalds } 450b3025457SBenjamin Herrenschmidt 451b3025457SBenjamin Herrenschmidt swim3_dbg(" setup xfer at sect %d (of %d) head %d for %d\n", 452b3025457SBenjamin Herrenschmidt fs->req_sector, fs->secpertrack, fs->head, n); 453b3025457SBenjamin Herrenschmidt 4541da177e4SLinus Torvalds fs->scount = n; 4551da177e4SLinus Torvalds swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0); 4561da177e4SLinus Torvalds out_8(&sw->sector, fs->req_sector); 4571da177e4SLinus Torvalds out_8(&sw->nsect, n); 4581da177e4SLinus Torvalds out_8(&sw->gap3, 0); 459*b60b270bSChristoph Hellwig out_le32(&dr->cmdptr, swim3_phys_to_bus(virt_to_phys(cp))); 460b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) { 4611da177e4SLinus Torvalds /* Set up 3 dma commands: write preamble, data, postamble */ 462*b60b270bSChristoph Hellwig init_dma(cp, OUTPUT_MORE, virt_to_phys(write_preamble), 463*b60b270bSChristoph Hellwig sizeof(write_preamble)); 4641da177e4SLinus Torvalds ++cp; 465*b60b270bSChristoph Hellwig init_dma(cp, OUTPUT_MORE, swim3_bio_phys(req->bio), 512); 4661da177e4SLinus Torvalds ++cp; 467*b60b270bSChristoph Hellwig init_dma(cp, OUTPUT_LAST, virt_to_phys(write_postamble), 468*b60b270bSChristoph Hellwig sizeof(write_postamble)); 4691da177e4SLinus Torvalds } else { 470*b60b270bSChristoph Hellwig init_dma(cp, INPUT_LAST, swim3_bio_phys(req->bio), n * 512); 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds ++cp; 4731da177e4SLinus Torvalds out_le16(&cp->command, DBDMA_STOP); 4741da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 4751da177e4SLinus Torvalds in_8(&sw->error); 4761da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 477b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) 4781da177e4SLinus Torvalds out_8(&sw->control_bis, WRITE_SECTORS); 4791da177e4SLinus Torvalds in_8(&sw->intr); 4801da177e4SLinus Torvalds out_le32(&dr->control, (RUN << 16) | RUN); 4811da177e4SLinus Torvalds /* enable intr when transfer complete */ 4821da177e4SLinus Torvalds out_8(&sw->intr_enable, TRANSFER_DONE); 4831da177e4SLinus Torvalds out_8(&sw->control_bis, DO_ACTION); 4841da177e4SLinus Torvalds set_timeout(fs, 2*HZ, xfer_timeout); /* enable timeout */ 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds static void act(struct floppy_state *fs) 4881da177e4SLinus Torvalds { 4891da177e4SLinus Torvalds for (;;) { 490b3025457SBenjamin Herrenschmidt swim3_dbg(" act loop, state=%d, req_cyl=%d, cur_cyl=%d\n", 491b3025457SBenjamin Herrenschmidt fs->state, fs->req_cyl, fs->cur_cyl); 492b3025457SBenjamin Herrenschmidt 4931da177e4SLinus Torvalds switch (fs->state) { 4941da177e4SLinus Torvalds case idle: 4951da177e4SLinus Torvalds return; /* XXX shouldn't get here */ 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds case locating: 4981da177e4SLinus Torvalds if (swim3_readbit(fs, TRACK_ZERO)) { 499b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " locate track 0\n"); 5001da177e4SLinus Torvalds fs->cur_cyl = 0; 5011da177e4SLinus Torvalds if (fs->req_cyl == 0) 5021da177e4SLinus Torvalds fs->state = do_transfer; 5031da177e4SLinus Torvalds else 5041da177e4SLinus Torvalds fs->state = seeking; 5051da177e4SLinus Torvalds break; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds scan_track(fs); 5081da177e4SLinus Torvalds return; 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds case seeking: 5111da177e4SLinus Torvalds if (fs->cur_cyl < 0) { 5121da177e4SLinus Torvalds fs->expect_cyl = -1; 5131da177e4SLinus Torvalds fs->state = locating; 5141da177e4SLinus Torvalds break; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds if (fs->req_cyl == fs->cur_cyl) { 517b3025457SBenjamin Herrenschmidt swim3_warn("%s", "Whoops, seeking 0\n"); 5181da177e4SLinus Torvalds fs->state = do_transfer; 5191da177e4SLinus Torvalds break; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds seek_track(fs, fs->req_cyl - fs->cur_cyl); 5221da177e4SLinus Torvalds return; 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds case settling: 5251da177e4SLinus Torvalds /* check for SEEK_COMPLETE after 30ms */ 5261da177e4SLinus Torvalds fs->settle_time = (HZ + 32) / 33; 5271da177e4SLinus Torvalds set_timeout(fs, fs->settle_time, settle_timeout); 5281da177e4SLinus Torvalds return; 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds case do_transfer: 5311da177e4SLinus Torvalds if (fs->cur_cyl != fs->req_cyl) { 5321da177e4SLinus Torvalds if (fs->retries > 5) { 533b3025457SBenjamin Herrenschmidt swim3_err("Wrong cylinder in transfer, want: %d got %d\n", 534b3025457SBenjamin Herrenschmidt fs->req_cyl, fs->cur_cyl); 5352a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 5361da177e4SLinus Torvalds fs->state = idle; 5371da177e4SLinus Torvalds return; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds fs->state = seeking; 5401da177e4SLinus Torvalds break; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds setup_transfer(fs); 5431da177e4SLinus Torvalds return; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds case jogging: 5461da177e4SLinus Torvalds seek_track(fs, -5); 5471da177e4SLinus Torvalds return; 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds default: 550b3025457SBenjamin Herrenschmidt swim3_err("Unknown state %d\n", fs->state); 5511da177e4SLinus Torvalds return; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 556b5775a6bSKees Cook static void scan_timeout(struct timer_list *t) 5571da177e4SLinus Torvalds { 558b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 5591da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 560b3025457SBenjamin Herrenschmidt unsigned long flags; 5611da177e4SLinus Torvalds 562b3025457SBenjamin Herrenschmidt swim3_dbg("* scan timeout, state=%d\n", fs->state); 563b3025457SBenjamin Herrenschmidt 564b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 5651da177e4SLinus Torvalds fs->timeout_pending = 0; 5661da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 5671da177e4SLinus Torvalds out_8(&sw->select, RELAX); 5681da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 5691da177e4SLinus Torvalds fs->cur_cyl = -1; 5701da177e4SLinus Torvalds if (fs->retries > 5) { 5712a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 5721da177e4SLinus Torvalds fs->state = idle; 5731da177e4SLinus Torvalds } else { 5741da177e4SLinus Torvalds fs->state = jogging; 5751da177e4SLinus Torvalds act(fs); 5761da177e4SLinus Torvalds } 577b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 580b5775a6bSKees Cook static void seek_timeout(struct timer_list *t) 5811da177e4SLinus Torvalds { 582b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 5831da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 584b3025457SBenjamin Herrenschmidt unsigned long flags; 5851da177e4SLinus Torvalds 586b3025457SBenjamin Herrenschmidt swim3_dbg("* seek timeout, state=%d\n", fs->state); 587b3025457SBenjamin Herrenschmidt 588b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 5891da177e4SLinus Torvalds fs->timeout_pending = 0; 5901da177e4SLinus Torvalds out_8(&sw->control_bic, DO_SEEK); 5911da177e4SLinus Torvalds out_8(&sw->select, RELAX); 5921da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 593b3025457SBenjamin Herrenschmidt swim3_err("%s", "Seek timeout\n"); 5942a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 5951da177e4SLinus Torvalds fs->state = idle; 596b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 599b5775a6bSKees Cook static void settle_timeout(struct timer_list *t) 6001da177e4SLinus Torvalds { 601b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 6021da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 603b3025457SBenjamin Herrenschmidt unsigned long flags; 6041da177e4SLinus Torvalds 605b3025457SBenjamin Herrenschmidt swim3_dbg("* settle timeout, state=%d\n", fs->state); 606b3025457SBenjamin Herrenschmidt 607b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 6081da177e4SLinus Torvalds fs->timeout_pending = 0; 6091da177e4SLinus Torvalds if (swim3_readbit(fs, SEEK_COMPLETE)) { 6101da177e4SLinus Torvalds out_8(&sw->select, RELAX); 6111da177e4SLinus Torvalds fs->state = locating; 6121da177e4SLinus Torvalds act(fs); 613b3025457SBenjamin Herrenschmidt goto unlock; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds out_8(&sw->select, RELAX); 6161da177e4SLinus Torvalds if (fs->settle_time < 2*HZ) { 6171da177e4SLinus Torvalds ++fs->settle_time; 6181da177e4SLinus Torvalds set_timeout(fs, 1, settle_timeout); 619b3025457SBenjamin Herrenschmidt goto unlock; 6201da177e4SLinus Torvalds } 621b3025457SBenjamin Herrenschmidt swim3_err("%s", "Seek settle timeout\n"); 6222a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 6231da177e4SLinus Torvalds fs->state = idle; 624b3025457SBenjamin Herrenschmidt unlock: 625b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 628b5775a6bSKees Cook static void xfer_timeout(struct timer_list *t) 6291da177e4SLinus Torvalds { 630b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 6311da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 6321da177e4SLinus Torvalds struct dbdma_regs __iomem *dr = fs->dma; 633b3025457SBenjamin Herrenschmidt unsigned long flags; 6341da177e4SLinus Torvalds int n; 6351da177e4SLinus Torvalds 636b3025457SBenjamin Herrenschmidt swim3_dbg("* xfer timeout, state=%d\n", fs->state); 637b3025457SBenjamin Herrenschmidt 638b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 6391da177e4SLinus Torvalds fs->timeout_pending = 0; 6401da177e4SLinus Torvalds out_le32(&dr->control, RUN << 16); 6411da177e4SLinus Torvalds /* We must wait a bit for dbdma to stop */ 6421da177e4SLinus Torvalds for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++) 6431da177e4SLinus Torvalds udelay(1); 6441da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 6451da177e4SLinus Torvalds out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); 6461da177e4SLinus Torvalds out_8(&sw->select, RELAX); 647b3025457SBenjamin Herrenschmidt swim3_err("Timeout %sing sector %ld\n", 648b3025457SBenjamin Herrenschmidt (rq_data_dir(fs->cur_req)==WRITE? "writ": "read"), 649b3025457SBenjamin Herrenschmidt (long)blk_rq_pos(fs->cur_req)); 6502a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 6511da177e4SLinus Torvalds fs->state = idle; 652b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 6557d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id) 6561da177e4SLinus Torvalds { 6571da177e4SLinus Torvalds struct floppy_state *fs = (struct floppy_state *) dev_id; 6581da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 6591da177e4SLinus Torvalds int intr, err, n; 6601da177e4SLinus Torvalds int stat, resid; 6611da177e4SLinus Torvalds struct dbdma_regs __iomem *dr; 6621da177e4SLinus Torvalds struct dbdma_cmd *cp; 663b3025457SBenjamin Herrenschmidt unsigned long flags; 664b3025457SBenjamin Herrenschmidt struct request *req = fs->cur_req; 6651da177e4SLinus Torvalds 666b3025457SBenjamin Herrenschmidt swim3_dbg("* interrupt, state=%d\n", fs->state); 667b3025457SBenjamin Herrenschmidt 668b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 6691da177e4SLinus Torvalds intr = in_8(&sw->intr); 6701da177e4SLinus Torvalds err = (intr & ERROR_INTR)? in_8(&sw->error): 0; 6711da177e4SLinus Torvalds if ((intr & ERROR_INTR) && fs->state != do_transfer) 672b3025457SBenjamin Herrenschmidt swim3_err("Non-transfer error interrupt: state=%d, dir=%x, intr=%x, err=%x\n", 673b3025457SBenjamin Herrenschmidt fs->state, rq_data_dir(req), intr, err); 6741da177e4SLinus Torvalds switch (fs->state) { 6751da177e4SLinus Torvalds case locating: 6761da177e4SLinus Torvalds if (intr & SEEN_SECTOR) { 6771da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 6781da177e4SLinus Torvalds out_8(&sw->select, RELAX); 6791da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 6801da177e4SLinus Torvalds del_timer(&fs->timeout); 6811da177e4SLinus Torvalds fs->timeout_pending = 0; 6821da177e4SLinus Torvalds if (sw->ctrack == 0xff) { 683b3025457SBenjamin Herrenschmidt swim3_err("%s", "Seen sector but cyl=ff?\n"); 6841da177e4SLinus Torvalds fs->cur_cyl = -1; 6851da177e4SLinus Torvalds if (fs->retries > 5) { 6862a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 6871da177e4SLinus Torvalds fs->state = idle; 6881da177e4SLinus Torvalds } else { 6891da177e4SLinus Torvalds fs->state = jogging; 6901da177e4SLinus Torvalds act(fs); 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds break; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds fs->cur_cyl = sw->ctrack; 6951da177e4SLinus Torvalds fs->cur_sector = sw->csect; 6961da177e4SLinus Torvalds if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl) 697b3025457SBenjamin Herrenschmidt swim3_err("Expected cyl %d, got %d\n", 6981da177e4SLinus Torvalds fs->expect_cyl, fs->cur_cyl); 6991da177e4SLinus Torvalds fs->state = do_transfer; 7001da177e4SLinus Torvalds act(fs); 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds break; 7031da177e4SLinus Torvalds case seeking: 7041da177e4SLinus Torvalds case jogging: 7051da177e4SLinus Torvalds if (sw->nseek == 0) { 7061da177e4SLinus Torvalds out_8(&sw->control_bic, DO_SEEK); 7071da177e4SLinus Torvalds out_8(&sw->select, RELAX); 7081da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 7091da177e4SLinus Torvalds del_timer(&fs->timeout); 7101da177e4SLinus Torvalds fs->timeout_pending = 0; 7111da177e4SLinus Torvalds if (fs->state == seeking) 7121da177e4SLinus Torvalds ++fs->retries; 7131da177e4SLinus Torvalds fs->state = settling; 7141da177e4SLinus Torvalds act(fs); 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds break; 7171da177e4SLinus Torvalds case settling: 7181da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 7191da177e4SLinus Torvalds del_timer(&fs->timeout); 7201da177e4SLinus Torvalds fs->timeout_pending = 0; 7211da177e4SLinus Torvalds act(fs); 7221da177e4SLinus Torvalds break; 7231da177e4SLinus Torvalds case do_transfer: 7241da177e4SLinus Torvalds if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0) 7251da177e4SLinus Torvalds break; 7261da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 7271da177e4SLinus Torvalds out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); 7281da177e4SLinus Torvalds out_8(&sw->select, RELAX); 7291da177e4SLinus Torvalds del_timer(&fs->timeout); 7301da177e4SLinus Torvalds fs->timeout_pending = 0; 7311da177e4SLinus Torvalds dr = fs->dma; 7321da177e4SLinus Torvalds cp = fs->dma_cmd; 733b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) 7341da177e4SLinus Torvalds ++cp; 7351da177e4SLinus Torvalds /* 7361da177e4SLinus Torvalds * Check that the main data transfer has finished. 7371da177e4SLinus Torvalds * On writing, the swim3 sometimes doesn't use 7381da177e4SLinus Torvalds * up all the bytes of the postamble, so we can still 7391da177e4SLinus Torvalds * see DMA active here. That doesn't matter as long 7401da177e4SLinus Torvalds * as all the sector data has been transferred. 7411da177e4SLinus Torvalds */ 7421da177e4SLinus Torvalds if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) { 7431da177e4SLinus Torvalds /* wait a little while for DMA to complete */ 7441da177e4SLinus Torvalds for (n = 0; n < 100; ++n) { 7451da177e4SLinus Torvalds if (cp->xfer_status != 0) 7461da177e4SLinus Torvalds break; 7471da177e4SLinus Torvalds udelay(1); 7481da177e4SLinus Torvalds barrier(); 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds /* turn off DMA */ 7521da177e4SLinus Torvalds out_le32(&dr->control, (RUN | PAUSE) << 16); 753f5718726SDavid Gibson stat = le16_to_cpu(cp->xfer_status); 754f5718726SDavid Gibson resid = le16_to_cpu(cp->res_count); 7551da177e4SLinus Torvalds if (intr & ERROR_INTR) { 7561da177e4SLinus Torvalds n = fs->scount - 1 - resid / 512; 7571da177e4SLinus Torvalds if (n > 0) { 758b3025457SBenjamin Herrenschmidt blk_update_request(req, 0, n << 9); 7591da177e4SLinus Torvalds fs->req_sector += n; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds if (fs->retries < 5) { 7621da177e4SLinus Torvalds ++fs->retries; 7631da177e4SLinus Torvalds act(fs); 7641da177e4SLinus Torvalds } else { 765b3025457SBenjamin Herrenschmidt swim3_err("Error %sing block %ld (err=%x)\n", 766b3025457SBenjamin Herrenschmidt rq_data_dir(req) == WRITE? "writ": "read", 767b3025457SBenjamin Herrenschmidt (long)blk_rq_pos(req), err); 7682a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 7691da177e4SLinus Torvalds fs->state = idle; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds } else { 7721da177e4SLinus Torvalds if ((stat & ACTIVE) == 0 || resid != 0) { 7731da177e4SLinus Torvalds /* musta been an error */ 774b3025457SBenjamin Herrenschmidt swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid); 775b3025457SBenjamin Herrenschmidt swim3_err(" state=%d, dir=%x, intr=%x, err=%x\n", 776b3025457SBenjamin Herrenschmidt fs->state, rq_data_dir(req), intr, err); 7772a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 7781da177e4SLinus Torvalds fs->state = idle; 7791da177e4SLinus Torvalds break; 7801da177e4SLinus Torvalds } 781b3025457SBenjamin Herrenschmidt fs->retries = 0; 782b3025457SBenjamin Herrenschmidt if (swim3_end_request(fs, 0, fs->scount << 9)) { 7831da177e4SLinus Torvalds fs->req_sector += fs->scount; 7841da177e4SLinus Torvalds if (fs->req_sector > fs->secpertrack) { 7851da177e4SLinus Torvalds fs->req_sector -= fs->secpertrack; 7861da177e4SLinus Torvalds if (++fs->head > 1) { 7871da177e4SLinus Torvalds fs->head = 0; 7881da177e4SLinus Torvalds ++fs->req_cyl; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds act(fs); 792467ca759STejun Heo } else 793467ca759STejun Heo fs->state = idle; 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds break; 7961da177e4SLinus Torvalds default: 797b3025457SBenjamin Herrenschmidt swim3_err("Don't know what to do in state %d\n", fs->state); 7981da177e4SLinus Torvalds } 799b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 8001da177e4SLinus Torvalds return IRQ_HANDLED; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* 8047d12e780SDavid Howells static void fd_dma_interrupt(int irq, void *dev_id) 8051da177e4SLinus Torvalds { 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds */ 8081da177e4SLinus Torvalds 809b3025457SBenjamin Herrenschmidt /* Called under the mutex to grab exclusive access to a drive */ 8101da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state, 8111da177e4SLinus Torvalds int interruptible) 8121da177e4SLinus Torvalds { 8131da177e4SLinus Torvalds unsigned long flags; 8141da177e4SLinus Torvalds 815b3025457SBenjamin Herrenschmidt swim3_dbg("%s", "-> grab drive\n"); 816b3025457SBenjamin Herrenschmidt 817b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 818b3025457SBenjamin Herrenschmidt if (fs->state != idle && fs->state != available) { 8191da177e4SLinus Torvalds ++fs->wanted; 820106fd892SArnd Bergmann /* this will enable irqs in order to sleep */ 821106fd892SArnd Bergmann if (!interruptible) 822106fd892SArnd Bergmann wait_event_lock_irq(fs->wait, 823106fd892SArnd Bergmann fs->state == available, 824106fd892SArnd Bergmann swim3_lock); 825106fd892SArnd Bergmann else if (wait_event_interruptible_lock_irq(fs->wait, 826106fd892SArnd Bergmann fs->state == available, 827106fd892SArnd Bergmann swim3_lock)) { 8281da177e4SLinus Torvalds --fs->wanted; 829106fd892SArnd Bergmann spin_unlock_irqrestore(&swim3_lock, flags); 8301da177e4SLinus Torvalds return -EINTR; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds --fs->wanted; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds fs->state = state; 835b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 836b3025457SBenjamin Herrenschmidt 8371da177e4SLinus Torvalds return 0; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs) 8411da177e4SLinus Torvalds { 8428ccb8cb1SOmar Sandoval struct request_queue *q = disks[fs->index]->queue; 8431da177e4SLinus Torvalds unsigned long flags; 8441da177e4SLinus Torvalds 845b3025457SBenjamin Herrenschmidt swim3_dbg("%s", "-> release drive\n"); 846b3025457SBenjamin Herrenschmidt 847b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 8481da177e4SLinus Torvalds fs->state = idle; 849b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 8508ccb8cb1SOmar Sandoval 8518ccb8cb1SOmar Sandoval blk_mq_freeze_queue(q); 8528ccb8cb1SOmar Sandoval blk_mq_quiesce_queue(q); 8538ccb8cb1SOmar Sandoval blk_mq_unquiesce_queue(q); 8548ccb8cb1SOmar Sandoval blk_mq_unfreeze_queue(q); 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs) 8581da177e4SLinus Torvalds { 8591da177e4SLinus Torvalds int err, n; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds err = grab_drive(fs, ejecting, 1); 8621da177e4SLinus Torvalds if (err) 8631da177e4SLinus Torvalds return err; 8641da177e4SLinus Torvalds swim3_action(fs, EJECT); 8651da177e4SLinus Torvalds for (n = 20; n > 0; --n) { 8661da177e4SLinus Torvalds if (signal_pending(current)) { 8671da177e4SLinus Torvalds err = -EINTR; 8681da177e4SLinus Torvalds break; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds swim3_select(fs, RELAX); 87186e84862SNishanth Aravamudan schedule_timeout_interruptible(1); 8721da177e4SLinus Torvalds if (swim3_readbit(fs, DISK_IN) == 0) 8731da177e4SLinus Torvalds break; 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds swim3_select(fs, RELAX); 8761da177e4SLinus Torvalds udelay(150); 8771da177e4SLinus Torvalds fs->ejected = 1; 8781da177e4SLinus Torvalds release_drive(fs); 8791da177e4SLinus Torvalds return err; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds static struct floppy_struct floppy_type = 8831da177e4SLinus Torvalds { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */ 8841da177e4SLinus Torvalds 8858a6cfeb6SArnd Bergmann static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode, 8861da177e4SLinus Torvalds unsigned int cmd, unsigned long param) 8871da177e4SLinus Torvalds { 888b4d9a442SAl Viro struct floppy_state *fs = bdev->bd_disk->private_data; 8891da177e4SLinus Torvalds int err; 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)) 8921da177e4SLinus Torvalds return -EPERM; 8931da177e4SLinus Torvalds 894d58b0c39SBenjamin Herrenschmidt if (fs->mdev->media_bay && 895d58b0c39SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) 8961da177e4SLinus Torvalds return -ENXIO; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds switch (cmd) { 8991da177e4SLinus Torvalds case FDEJECT: 9001da177e4SLinus Torvalds if (fs->ref_count != 1) 9011da177e4SLinus Torvalds return -EBUSY; 9021da177e4SLinus Torvalds err = fd_eject(fs); 9031da177e4SLinus Torvalds return err; 9041da177e4SLinus Torvalds case FDGETPRM: 9051da177e4SLinus Torvalds if (copy_to_user((void __user *) param, &floppy_type, 9061da177e4SLinus Torvalds sizeof(struct floppy_struct))) 9071da177e4SLinus Torvalds return -EFAULT; 9081da177e4SLinus Torvalds return 0; 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds return -ENOTTY; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 9138a6cfeb6SArnd Bergmann static int floppy_ioctl(struct block_device *bdev, fmode_t mode, 9148a6cfeb6SArnd Bergmann unsigned int cmd, unsigned long param) 9158a6cfeb6SArnd Bergmann { 9168a6cfeb6SArnd Bergmann int ret; 9178a6cfeb6SArnd Bergmann 9182a48fc0aSArnd Bergmann mutex_lock(&swim3_mutex); 9198a6cfeb6SArnd Bergmann ret = floppy_locked_ioctl(bdev, mode, cmd, param); 9202a48fc0aSArnd Bergmann mutex_unlock(&swim3_mutex); 9218a6cfeb6SArnd Bergmann 9228a6cfeb6SArnd Bergmann return ret; 9238a6cfeb6SArnd Bergmann } 9248a6cfeb6SArnd Bergmann 925b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode) 9261da177e4SLinus Torvalds { 927b4d9a442SAl Viro struct floppy_state *fs = bdev->bd_disk->private_data; 9281da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 9291da177e4SLinus Torvalds int n, err = 0; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds if (fs->ref_count == 0) { 932d58b0c39SBenjamin Herrenschmidt if (fs->mdev->media_bay && 933d58b0c39SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) 9341da177e4SLinus Torvalds return -ENXIO; 9351da177e4SLinus Torvalds out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2); 9361da177e4SLinus Torvalds out_8(&sw->control_bic, 0xff); 9371da177e4SLinus Torvalds out_8(&sw->mode, 0x95); 9381da177e4SLinus Torvalds udelay(10); 9391da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 9401da177e4SLinus Torvalds out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE); 9411da177e4SLinus Torvalds swim3_action(fs, MOTOR_ON); 9421da177e4SLinus Torvalds fs->write_prot = -1; 9431da177e4SLinus Torvalds fs->cur_cyl = -1; 9441da177e4SLinus Torvalds for (n = 0; n < 2 * HZ; ++n) { 9451da177e4SLinus Torvalds if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE)) 9461da177e4SLinus Torvalds break; 9471da177e4SLinus Torvalds if (signal_pending(current)) { 9481da177e4SLinus Torvalds err = -EINTR; 9491da177e4SLinus Torvalds break; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds swim3_select(fs, RELAX); 95286e84862SNishanth Aravamudan schedule_timeout_interruptible(1); 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0 9551da177e4SLinus Torvalds || swim3_readbit(fs, DISK_IN) == 0)) 9561da177e4SLinus Torvalds err = -ENXIO; 9571da177e4SLinus Torvalds swim3_action(fs, SETMFM); 9581da177e4SLinus Torvalds swim3_select(fs, RELAX); 9591da177e4SLinus Torvalds 960b4d9a442SAl Viro } else if (fs->ref_count == -1 || mode & FMODE_EXCL) 9611da177e4SLinus Torvalds return -EBUSY; 9621da177e4SLinus Torvalds 963b4d9a442SAl Viro if (err == 0 && (mode & FMODE_NDELAY) == 0 964b4d9a442SAl Viro && (mode & (FMODE_READ|FMODE_WRITE))) { 9653dcbad29SChristoph Hellwig if (bdev_check_media_change(bdev)) 9663dcbad29SChristoph Hellwig floppy_revalidate(bdev->bd_disk); 9671da177e4SLinus Torvalds if (fs->ejected) 9681da177e4SLinus Torvalds err = -ENXIO; 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds 971b4d9a442SAl Viro if (err == 0 && (mode & FMODE_WRITE)) { 9721da177e4SLinus Torvalds if (fs->write_prot < 0) 9731da177e4SLinus Torvalds fs->write_prot = swim3_readbit(fs, WRITE_PROT); 9741da177e4SLinus Torvalds if (fs->write_prot) 9751da177e4SLinus Torvalds err = -EROFS; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds if (err) { 9791da177e4SLinus Torvalds if (fs->ref_count == 0) { 9801da177e4SLinus Torvalds swim3_action(fs, MOTOR_OFF); 9811da177e4SLinus Torvalds out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE); 9821da177e4SLinus Torvalds swim3_select(fs, RELAX); 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds return err; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 987b4d9a442SAl Viro if (mode & FMODE_EXCL) 9881da177e4SLinus Torvalds fs->ref_count = -1; 9891da177e4SLinus Torvalds else 9901da177e4SLinus Torvalds ++fs->ref_count; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds return 0; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9956e9624b8SArnd Bergmann static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode) 9966e9624b8SArnd Bergmann { 9976e9624b8SArnd Bergmann int ret; 9986e9624b8SArnd Bergmann 9992a48fc0aSArnd Bergmann mutex_lock(&swim3_mutex); 10006e9624b8SArnd Bergmann ret = floppy_open(bdev, mode); 10012a48fc0aSArnd Bergmann mutex_unlock(&swim3_mutex); 10026e9624b8SArnd Bergmann 10036e9624b8SArnd Bergmann return ret; 10046e9624b8SArnd Bergmann } 10056e9624b8SArnd Bergmann 1006db2a144bSAl Viro static void floppy_release(struct gendisk *disk, fmode_t mode) 10071da177e4SLinus Torvalds { 1008b4d9a442SAl Viro struct floppy_state *fs = disk->private_data; 10091da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 1010b3025457SBenjamin Herrenschmidt 10112a48fc0aSArnd Bergmann mutex_lock(&swim3_mutex); 1012296dcc40SFinn Thain if (fs->ref_count > 0) 1013296dcc40SFinn Thain --fs->ref_count; 1014296dcc40SFinn Thain else if (fs->ref_count == -1) 1015296dcc40SFinn Thain fs->ref_count = 0; 1016296dcc40SFinn Thain if (fs->ref_count == 0) { 10171da177e4SLinus Torvalds swim3_action(fs, MOTOR_OFF); 10181da177e4SLinus Torvalds out_8(&sw->control_bic, 0xff); 10191da177e4SLinus Torvalds swim3_select(fs, RELAX); 10201da177e4SLinus Torvalds } 10212a48fc0aSArnd Bergmann mutex_unlock(&swim3_mutex); 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 10244bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk, 10254bbde777STejun Heo unsigned int clearing) 10261da177e4SLinus Torvalds { 10271da177e4SLinus Torvalds struct floppy_state *fs = disk->private_data; 10284bbde777STejun Heo return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk) 10321da177e4SLinus Torvalds { 10331da177e4SLinus Torvalds struct floppy_state *fs = disk->private_data; 10341da177e4SLinus Torvalds struct swim3 __iomem *sw; 10351da177e4SLinus Torvalds int ret, n; 10361da177e4SLinus Torvalds 1037d58b0c39SBenjamin Herrenschmidt if (fs->mdev->media_bay && 1038d58b0c39SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) 10391da177e4SLinus Torvalds return -ENXIO; 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds sw = fs->swim3; 10421da177e4SLinus Torvalds grab_drive(fs, revalidating, 0); 10431da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 10441da177e4SLinus Torvalds out_8(&sw->control_bis, DRIVE_ENABLE); 10451da177e4SLinus Torvalds swim3_action(fs, MOTOR_ON); /* necessary? */ 10461da177e4SLinus Torvalds fs->write_prot = -1; 10471da177e4SLinus Torvalds fs->cur_cyl = -1; 10481da177e4SLinus Torvalds mdelay(1); 10491da177e4SLinus Torvalds for (n = HZ; n > 0; --n) { 10501da177e4SLinus Torvalds if (swim3_readbit(fs, SEEK_COMPLETE)) 10511da177e4SLinus Torvalds break; 10521da177e4SLinus Torvalds if (signal_pending(current)) 10531da177e4SLinus Torvalds break; 10541da177e4SLinus Torvalds swim3_select(fs, RELAX); 105586e84862SNishanth Aravamudan schedule_timeout_interruptible(1); 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds ret = swim3_readbit(fs, SEEK_COMPLETE) == 0 10581da177e4SLinus Torvalds || swim3_readbit(fs, DISK_IN) == 0; 10591da177e4SLinus Torvalds if (ret) 10601da177e4SLinus Torvalds swim3_action(fs, MOTOR_OFF); 10611da177e4SLinus Torvalds else { 10621da177e4SLinus Torvalds fs->ejected = 0; 10631da177e4SLinus Torvalds swim3_action(fs, SETMFM); 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds swim3_select(fs, RELAX); 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds release_drive(fs); 10681da177e4SLinus Torvalds return ret; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 107183d5cde4SAlexey Dobriyan static const struct block_device_operations floppy_fops = { 10726e9624b8SArnd Bergmann .open = floppy_unlocked_open, 1073b4d9a442SAl Viro .release = floppy_release, 10748a6cfeb6SArnd Bergmann .ioctl = floppy_ioctl, 10754bbde777STejun Heo .check_events = floppy_check_events, 10761da177e4SLinus Torvalds }; 10771da177e4SLinus Torvalds 10788ccb8cb1SOmar Sandoval static const struct blk_mq_ops swim3_mq_ops = { 10798ccb8cb1SOmar Sandoval .queue_rq = swim3_queue_rq, 10808ccb8cb1SOmar Sandoval }; 10818ccb8cb1SOmar Sandoval 1082b3025457SBenjamin Herrenschmidt static void swim3_mb_event(struct macio_dev* mdev, int mb_state) 1083b3025457SBenjamin Herrenschmidt { 1084b3025457SBenjamin Herrenschmidt struct floppy_state *fs = macio_get_drvdata(mdev); 10857414d4f6SCong Ding struct swim3 __iomem *sw; 1086b3025457SBenjamin Herrenschmidt 1087b3025457SBenjamin Herrenschmidt if (!fs) 1088b3025457SBenjamin Herrenschmidt return; 10897414d4f6SCong Ding 10907414d4f6SCong Ding sw = fs->swim3; 10917414d4f6SCong Ding 1092b3025457SBenjamin Herrenschmidt if (mb_state != MB_FD) 1093b3025457SBenjamin Herrenschmidt return; 1094b3025457SBenjamin Herrenschmidt 1095b3025457SBenjamin Herrenschmidt /* Clear state */ 1096b3025457SBenjamin Herrenschmidt out_8(&sw->intr_enable, 0); 1097b3025457SBenjamin Herrenschmidt in_8(&sw->intr); 1098b3025457SBenjamin Herrenschmidt in_8(&sw->error); 1099b3025457SBenjamin Herrenschmidt } 1100b3025457SBenjamin Herrenschmidt 11013e9a6927SBenjamin Herrenschmidt static int swim3_add_device(struct macio_dev *mdev, int index) 11021da177e4SLinus Torvalds { 110361c7a080SGrant Likely struct device_node *swim = mdev->ofdev.dev.of_node; 11043e9a6927SBenjamin Herrenschmidt struct floppy_state *fs = &floppy_states[index]; 11053e9a6927SBenjamin Herrenschmidt int rc = -EBUSY; 11061da177e4SLinus Torvalds 1107b3025457SBenjamin Herrenschmidt fs->mdev = mdev; 1108b3025457SBenjamin Herrenschmidt fs->index = index; 1109b3025457SBenjamin Herrenschmidt 11103e9a6927SBenjamin Herrenschmidt /* Check & Request resources */ 11113e9a6927SBenjamin Herrenschmidt if (macio_resource_count(mdev) < 2) { 1112b3025457SBenjamin Herrenschmidt swim3_err("%s", "No address in device-tree\n"); 11133e9a6927SBenjamin Herrenschmidt return -ENXIO; 1114cc5d0189SBenjamin Herrenschmidt } 1115b3025457SBenjamin Herrenschmidt if (macio_irq_count(mdev) < 1) { 1116b3025457SBenjamin Herrenschmidt swim3_err("%s", "No interrupt in device-tree\n"); 1117b3025457SBenjamin Herrenschmidt return -ENXIO; 1118cc5d0189SBenjamin Herrenschmidt } 11193e9a6927SBenjamin Herrenschmidt if (macio_request_resource(mdev, 0, "swim3 (mmio)")) { 1120b3025457SBenjamin Herrenschmidt swim3_err("%s", "Can't request mmio resource\n"); 11213e9a6927SBenjamin Herrenschmidt return -EBUSY; 11221da177e4SLinus Torvalds } 11233e9a6927SBenjamin Herrenschmidt if (macio_request_resource(mdev, 1, "swim3 (dma)")) { 1124b3025457SBenjamin Herrenschmidt swim3_err("%s", "Can't request dma resource\n"); 11253e9a6927SBenjamin Herrenschmidt macio_release_resource(mdev, 0); 11263e9a6927SBenjamin Herrenschmidt return -EBUSY; 11273e9a6927SBenjamin Herrenschmidt } 11283e9a6927SBenjamin Herrenschmidt dev_set_drvdata(&mdev->ofdev.dev, fs); 11291da177e4SLinus Torvalds 1130d58b0c39SBenjamin Herrenschmidt if (mdev->media_bay == NULL) 11311da177e4SLinus Torvalds pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1); 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds fs->state = idle; 11343e9a6927SBenjamin Herrenschmidt fs->swim3 = (struct swim3 __iomem *) 11353e9a6927SBenjamin Herrenschmidt ioremap(macio_resource_start(mdev, 0), 0x200); 11363e9a6927SBenjamin Herrenschmidt if (fs->swim3 == NULL) { 1137b3025457SBenjamin Herrenschmidt swim3_err("%s", "Couldn't map mmio registers\n"); 11383e9a6927SBenjamin Herrenschmidt rc = -ENOMEM; 11393e9a6927SBenjamin Herrenschmidt goto out_release; 11403e9a6927SBenjamin Herrenschmidt } 11413e9a6927SBenjamin Herrenschmidt fs->dma = (struct dbdma_regs __iomem *) 11423e9a6927SBenjamin Herrenschmidt ioremap(macio_resource_start(mdev, 1), 0x200); 11433e9a6927SBenjamin Herrenschmidt if (fs->dma == NULL) { 1144b3025457SBenjamin Herrenschmidt swim3_err("%s", "Couldn't map dma registers\n"); 11453e9a6927SBenjamin Herrenschmidt iounmap(fs->swim3); 11463e9a6927SBenjamin Herrenschmidt rc = -ENOMEM; 11473e9a6927SBenjamin Herrenschmidt goto out_release; 11483e9a6927SBenjamin Herrenschmidt } 11493e9a6927SBenjamin Herrenschmidt fs->swim3_intr = macio_irq(mdev, 0); 1150a419aef8SJoe Perches fs->dma_intr = macio_irq(mdev, 1); 11511da177e4SLinus Torvalds fs->cur_cyl = -1; 11521da177e4SLinus Torvalds fs->cur_sector = -1; 11531da177e4SLinus Torvalds fs->secpercyl = 36; 11541da177e4SLinus Torvalds fs->secpertrack = 18; 11551da177e4SLinus Torvalds fs->total_secs = 2880; 11561da177e4SLinus Torvalds init_waitqueue_head(&fs->wait); 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space); 11591da177e4SLinus Torvalds memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd)); 1160f5718726SDavid Gibson fs->dma_cmd[1].command = cpu_to_le16(DBDMA_STOP); 11611da177e4SLinus Torvalds 1162b3025457SBenjamin Herrenschmidt if (mdev->media_bay == NULL || check_media_bay(mdev->media_bay) == MB_FD) 1163b3025457SBenjamin Herrenschmidt swim3_mb_event(mdev, MB_FD); 1164b3025457SBenjamin Herrenschmidt 11651da177e4SLinus Torvalds if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) { 1166b3025457SBenjamin Herrenschmidt swim3_err("%s", "Couldn't request interrupt\n"); 11671da177e4SLinus Torvalds pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0); 11683e9a6927SBenjamin Herrenschmidt goto out_unmap; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 1171b5775a6bSKees Cook timer_setup(&fs->timeout, NULL, 0); 11721da177e4SLinus Torvalds 1173b3025457SBenjamin Herrenschmidt swim3_info("SWIM3 floppy controller %s\n", 1174d58b0c39SBenjamin Herrenschmidt mdev->media_bay ? "in media bay" : ""); 11751da177e4SLinus Torvalds 11763e9a6927SBenjamin Herrenschmidt return 0; 11771da177e4SLinus Torvalds 11783e9a6927SBenjamin Herrenschmidt out_unmap: 11793e9a6927SBenjamin Herrenschmidt iounmap(fs->dma); 11803e9a6927SBenjamin Herrenschmidt iounmap(fs->swim3); 11813e9a6927SBenjamin Herrenschmidt 11823e9a6927SBenjamin Herrenschmidt out_release: 11833e9a6927SBenjamin Herrenschmidt macio_release_resource(mdev, 0); 11843e9a6927SBenjamin Herrenschmidt macio_release_resource(mdev, 1); 11853e9a6927SBenjamin Herrenschmidt 11863e9a6927SBenjamin Herrenschmidt return rc; 11873e9a6927SBenjamin Herrenschmidt } 11883e9a6927SBenjamin Herrenschmidt 11898d85fce7SGreg Kroah-Hartman static int swim3_attach(struct macio_dev *mdev, 11908d85fce7SGreg Kroah-Hartman const struct of_device_id *match) 11913e9a6927SBenjamin Herrenschmidt { 1192dbaa54b6SOmar Sandoval struct floppy_state *fs; 11933e9a6927SBenjamin Herrenschmidt struct gendisk *disk; 1194dbaa54b6SOmar Sandoval int rc; 1195b3025457SBenjamin Herrenschmidt 1196dbaa54b6SOmar Sandoval if (floppy_count >= MAX_FLOPPIES) 1197b3025457SBenjamin Herrenschmidt return -ENXIO; 11983e9a6927SBenjamin Herrenschmidt 1199dbaa54b6SOmar Sandoval if (floppy_count == 0) { 1200dbaa54b6SOmar Sandoval rc = register_blkdev(FLOPPY_MAJOR, "fd"); 12013e9a6927SBenjamin Herrenschmidt if (rc) 12023e9a6927SBenjamin Herrenschmidt return rc; 1203dbaa54b6SOmar Sandoval } 1204dbaa54b6SOmar Sandoval 1205dbaa54b6SOmar Sandoval disk = alloc_disk(1); 1206dbaa54b6SOmar Sandoval if (disk == NULL) { 1207dbaa54b6SOmar Sandoval rc = -ENOMEM; 1208dbaa54b6SOmar Sandoval goto out_unregister; 1209dbaa54b6SOmar Sandoval } 12108ccb8cb1SOmar Sandoval 1211427c5ce4SFinn Thain fs = &floppy_states[floppy_count]; 1212427c5ce4SFinn Thain memset(fs, 0, sizeof(*fs)); 1213427c5ce4SFinn Thain 12148ccb8cb1SOmar Sandoval disk->queue = blk_mq_init_sq_queue(&fs->tag_set, &swim3_mq_ops, 2, 12158ccb8cb1SOmar Sandoval BLK_MQ_F_SHOULD_MERGE); 12168ccb8cb1SOmar Sandoval if (IS_ERR(disk->queue)) { 12178ccb8cb1SOmar Sandoval rc = PTR_ERR(disk->queue); 12188ccb8cb1SOmar Sandoval disk->queue = NULL; 1219dbaa54b6SOmar Sandoval goto out_put_disk; 1220b3025457SBenjamin Herrenschmidt } 1221dbaa54b6SOmar Sandoval disk->queue->queuedata = fs; 12223e9a6927SBenjamin Herrenschmidt 1223dbaa54b6SOmar Sandoval rc = swim3_add_device(mdev, floppy_count); 1224dbaa54b6SOmar Sandoval if (rc) 1225dbaa54b6SOmar Sandoval goto out_cleanup_queue; 12263e9a6927SBenjamin Herrenschmidt 12273e9a6927SBenjamin Herrenschmidt disk->major = FLOPPY_MAJOR; 1228dbaa54b6SOmar Sandoval disk->first_minor = floppy_count; 12293e9a6927SBenjamin Herrenschmidt disk->fops = &floppy_fops; 1230dbaa54b6SOmar Sandoval disk->private_data = fs; 1231773008f6SMartin Wilck disk->events = DISK_EVENT_MEDIA_CHANGE; 12323e9a6927SBenjamin Herrenschmidt disk->flags |= GENHD_FL_REMOVABLE; 1233dbaa54b6SOmar Sandoval sprintf(disk->disk_name, "fd%d", floppy_count); 12343e9a6927SBenjamin Herrenschmidt set_capacity(disk, 2880); 12353e9a6927SBenjamin Herrenschmidt add_disk(disk); 12363e9a6927SBenjamin Herrenschmidt 1237dbaa54b6SOmar Sandoval disks[floppy_count++] = disk; 12383e9a6927SBenjamin Herrenschmidt return 0; 1239dbaa54b6SOmar Sandoval 1240dbaa54b6SOmar Sandoval out_cleanup_queue: 1241dbaa54b6SOmar Sandoval blk_cleanup_queue(disk->queue); 1242dbaa54b6SOmar Sandoval disk->queue = NULL; 12438ccb8cb1SOmar Sandoval blk_mq_free_tag_set(&fs->tag_set); 1244dbaa54b6SOmar Sandoval out_put_disk: 1245dbaa54b6SOmar Sandoval put_disk(disk); 1246dbaa54b6SOmar Sandoval out_unregister: 1247dbaa54b6SOmar Sandoval if (floppy_count == 0) 1248dbaa54b6SOmar Sandoval unregister_blkdev(FLOPPY_MAJOR, "fd"); 1249dbaa54b6SOmar Sandoval return rc; 12503e9a6927SBenjamin Herrenschmidt } 12513e9a6927SBenjamin Herrenschmidt 1252cc3f2e9fSArvind Yadav static const struct of_device_id swim3_match[] = 12533e9a6927SBenjamin Herrenschmidt { 12543e9a6927SBenjamin Herrenschmidt { 12553e9a6927SBenjamin Herrenschmidt .name = "swim3", 12563e9a6927SBenjamin Herrenschmidt }, 12573e9a6927SBenjamin Herrenschmidt { 12583e9a6927SBenjamin Herrenschmidt .compatible = "ohare-swim3" 12593e9a6927SBenjamin Herrenschmidt }, 12603e9a6927SBenjamin Herrenschmidt { 12613e9a6927SBenjamin Herrenschmidt .compatible = "swim3" 12623e9a6927SBenjamin Herrenschmidt }, 1263f41c53a5SAxel Lin { /* end of list */ } 12643e9a6927SBenjamin Herrenschmidt }; 12653e9a6927SBenjamin Herrenschmidt 12663e9a6927SBenjamin Herrenschmidt static struct macio_driver swim3_driver = 12673e9a6927SBenjamin Herrenschmidt { 1268c2cdf6abSBenjamin Herrenschmidt .driver = { 12693e9a6927SBenjamin Herrenschmidt .name = "swim3", 1270c2cdf6abSBenjamin Herrenschmidt .of_match_table = swim3_match, 1271c2cdf6abSBenjamin Herrenschmidt }, 12723e9a6927SBenjamin Herrenschmidt .probe = swim3_attach, 1273b3025457SBenjamin Herrenschmidt #ifdef CONFIG_PMAC_MEDIABAY 1274b3025457SBenjamin Herrenschmidt .mediabay_event = swim3_mb_event, 1275b3025457SBenjamin Herrenschmidt #endif 12763e9a6927SBenjamin Herrenschmidt #if 0 12773e9a6927SBenjamin Herrenschmidt .suspend = swim3_suspend, 12783e9a6927SBenjamin Herrenschmidt .resume = swim3_resume, 12793e9a6927SBenjamin Herrenschmidt #endif 12803e9a6927SBenjamin Herrenschmidt }; 12813e9a6927SBenjamin Herrenschmidt 12823e9a6927SBenjamin Herrenschmidt 12833e9a6927SBenjamin Herrenschmidt int swim3_init(void) 12843e9a6927SBenjamin Herrenschmidt { 12853e9a6927SBenjamin Herrenschmidt macio_register_driver(&swim3_driver); 12861da177e4SLinus Torvalds return 0; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds module_init(swim3_init) 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 12921da177e4SLinus Torvalds MODULE_AUTHOR("Paul Mackerras"); 12931da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR); 1294