11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Driver for the SWIM3 (Super Woz Integrated Machine 3) 31da177e4SLinus Torvalds * floppy controller found on Power Macintoshes. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1996 Paul Mackerras. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 81da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 91da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 101da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds /* 141da177e4SLinus Torvalds * TODO: 151da177e4SLinus Torvalds * handle 2 drives 161da177e4SLinus Torvalds * handle GCR disks 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 19b3025457SBenjamin Herrenschmidt #undef DEBUG 20b3025457SBenjamin Herrenschmidt 211da177e4SLinus Torvalds #include <linux/stddef.h> 221da177e4SLinus Torvalds #include <linux/kernel.h> 23174cd4b1SIngo Molnar #include <linux/sched/signal.h> 241da177e4SLinus Torvalds #include <linux/timer.h> 251da177e4SLinus Torvalds #include <linux/delay.h> 261da177e4SLinus Torvalds #include <linux/fd.h> 271da177e4SLinus Torvalds #include <linux/ioctl.h> 288ccb8cb1SOmar Sandoval #include <linux/blk-mq.h> 291da177e4SLinus Torvalds #include <linux/interrupt.h> 302a48fc0aSArnd Bergmann #include <linux/mutex.h> 311da177e4SLinus Torvalds #include <linux/module.h> 32515729ecSPaul Mackerras #include <linux/spinlock.h> 33106fd892SArnd Bergmann #include <linux/wait.h> 341da177e4SLinus Torvalds #include <asm/io.h> 351da177e4SLinus Torvalds #include <asm/dbdma.h> 361da177e4SLinus Torvalds #include <asm/prom.h> 377c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 381da177e4SLinus Torvalds #include <asm/mediabay.h> 391da177e4SLinus Torvalds #include <asm/machdep.h> 401da177e4SLinus Torvalds #include <asm/pmac_feature.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define MAX_FLOPPIES 2 431da177e4SLinus Torvalds 44b3025457SBenjamin Herrenschmidt static DEFINE_MUTEX(swim3_mutex); 45b3025457SBenjamin Herrenschmidt static struct gendisk *disks[MAX_FLOPPIES]; 46b3025457SBenjamin Herrenschmidt 471da177e4SLinus Torvalds enum swim_state { 481da177e4SLinus Torvalds idle, 491da177e4SLinus Torvalds locating, 501da177e4SLinus Torvalds seeking, 511da177e4SLinus Torvalds settling, 521da177e4SLinus Torvalds do_transfer, 531da177e4SLinus Torvalds jogging, 541da177e4SLinus Torvalds available, 551da177e4SLinus Torvalds revalidating, 561da177e4SLinus Torvalds ejecting 571da177e4SLinus Torvalds }; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds #define REG(x) unsigned char x; char x ## _pad[15]; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds /* 621da177e4SLinus Torvalds * The names for these registers mostly represent speculation on my part. 631da177e4SLinus Torvalds * It will be interesting to see how close they are to the names Apple uses. 641da177e4SLinus Torvalds */ 651da177e4SLinus Torvalds struct swim3 { 661da177e4SLinus Torvalds REG(data); 671da177e4SLinus Torvalds REG(timer); /* counts down at 1MHz */ 681da177e4SLinus Torvalds REG(error); 691da177e4SLinus Torvalds REG(mode); 701da177e4SLinus Torvalds REG(select); /* controls CA0, CA1, CA2 and LSTRB signals */ 711da177e4SLinus Torvalds REG(setup); 721da177e4SLinus Torvalds REG(control); /* writing bits clears them */ 731da177e4SLinus Torvalds REG(status); /* writing bits sets them in control */ 741da177e4SLinus Torvalds REG(intr); 751da177e4SLinus Torvalds REG(nseek); /* # tracks to seek */ 761da177e4SLinus Torvalds REG(ctrack); /* current track number */ 771da177e4SLinus Torvalds REG(csect); /* current sector number */ 781da177e4SLinus Torvalds REG(gap3); /* size of gap 3 in track format */ 791da177e4SLinus Torvalds REG(sector); /* sector # to read or write */ 801da177e4SLinus Torvalds REG(nsect); /* # sectors to read or write */ 811da177e4SLinus Torvalds REG(intr_enable); 821da177e4SLinus Torvalds }; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds #define control_bic control 851da177e4SLinus Torvalds #define control_bis status 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* Bits in select register */ 881da177e4SLinus Torvalds #define CA_MASK 7 891da177e4SLinus Torvalds #define LSTRB 8 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* Bits in control register */ 921da177e4SLinus Torvalds #define DO_SEEK 0x80 931da177e4SLinus Torvalds #define FORMAT 0x40 941da177e4SLinus Torvalds #define SELECT 0x20 951da177e4SLinus Torvalds #define WRITE_SECTORS 0x10 961da177e4SLinus Torvalds #define DO_ACTION 0x08 971da177e4SLinus Torvalds #define DRIVE2_ENABLE 0x04 981da177e4SLinus Torvalds #define DRIVE_ENABLE 0x02 991da177e4SLinus Torvalds #define INTR_ENABLE 0x01 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /* Bits in status register */ 1021da177e4SLinus Torvalds #define FIFO_1BYTE 0x80 1031da177e4SLinus Torvalds #define FIFO_2BYTE 0x40 1041da177e4SLinus Torvalds #define ERROR 0x20 1051da177e4SLinus Torvalds #define DATA 0x08 1061da177e4SLinus Torvalds #define RDDATA 0x04 1071da177e4SLinus Torvalds #define INTR_PENDING 0x02 1081da177e4SLinus Torvalds #define MARK_BYTE 0x01 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* Bits in intr and intr_enable registers */ 1111da177e4SLinus Torvalds #define ERROR_INTR 0x20 1121da177e4SLinus Torvalds #define DATA_CHANGED 0x10 1131da177e4SLinus Torvalds #define TRANSFER_DONE 0x08 1141da177e4SLinus Torvalds #define SEEN_SECTOR 0x04 1151da177e4SLinus Torvalds #define SEEK_DONE 0x02 1161da177e4SLinus Torvalds #define TIMER_DONE 0x01 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* Bits in error register */ 1191da177e4SLinus Torvalds #define ERR_DATA_CRC 0x80 1201da177e4SLinus Torvalds #define ERR_ADDR_CRC 0x40 1211da177e4SLinus Torvalds #define ERR_OVERRUN 0x04 1221da177e4SLinus Torvalds #define ERR_UNDERRUN 0x01 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /* Bits in setup register */ 1251da177e4SLinus Torvalds #define S_SW_RESET 0x80 1261da177e4SLinus Torvalds #define S_GCR_WRITE 0x40 1271da177e4SLinus Torvalds #define S_IBM_DRIVE 0x20 1281da177e4SLinus Torvalds #define S_TEST_MODE 0x10 1291da177e4SLinus Torvalds #define S_FCLK_DIV2 0x08 1301da177e4SLinus Torvalds #define S_GCR 0x04 1311da177e4SLinus Torvalds #define S_COPY_PROT 0x02 1321da177e4SLinus Torvalds #define S_INV_WDATA 0x01 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* Select values for swim3_action */ 1351da177e4SLinus Torvalds #define SEEK_POSITIVE 0 1361da177e4SLinus Torvalds #define SEEK_NEGATIVE 4 1371da177e4SLinus Torvalds #define STEP 1 1381da177e4SLinus Torvalds #define MOTOR_ON 2 1391da177e4SLinus Torvalds #define MOTOR_OFF 6 1401da177e4SLinus Torvalds #define INDEX 3 1411da177e4SLinus Torvalds #define EJECT 7 1421da177e4SLinus Torvalds #define SETMFM 9 1431da177e4SLinus Torvalds #define SETGCR 13 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds /* Select values for swim3_select and swim3_readbit */ 1461da177e4SLinus Torvalds #define STEP_DIR 0 1471da177e4SLinus Torvalds #define STEPPING 1 1481da177e4SLinus Torvalds #define MOTOR_ON 2 1491da177e4SLinus Torvalds #define RELAX 3 /* also eject in progress */ 1501da177e4SLinus Torvalds #define READ_DATA_0 4 15156a1c5eeSFinn Thain #define ONEMEG_DRIVE 5 1521da177e4SLinus Torvalds #define SINGLE_SIDED 6 /* drive or diskette is 4MB type? */ 1531da177e4SLinus Torvalds #define DRIVE_PRESENT 7 1541da177e4SLinus Torvalds #define DISK_IN 8 1551da177e4SLinus Torvalds #define WRITE_PROT 9 1561da177e4SLinus Torvalds #define TRACK_ZERO 10 1571da177e4SLinus Torvalds #define TACHO 11 1581da177e4SLinus Torvalds #define READ_DATA_1 12 15956a1c5eeSFinn Thain #define GCR_MODE 13 1601da177e4SLinus Torvalds #define SEEK_COMPLETE 14 16156a1c5eeSFinn Thain #define TWOMEG_MEDIA 15 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* Definitions of values used in writing and formatting */ 1641da177e4SLinus Torvalds #define DATA_ESCAPE 0x99 1651da177e4SLinus Torvalds #define GCR_SYNC_EXC 0x3f 1661da177e4SLinus Torvalds #define GCR_SYNC_CONV 0x80 1671da177e4SLinus Torvalds #define GCR_FIRST_MARK 0xd5 1681da177e4SLinus Torvalds #define GCR_SECOND_MARK 0xaa 1691da177e4SLinus Torvalds #define GCR_ADDR_MARK "\xd5\xaa\x00" 1701da177e4SLinus Torvalds #define GCR_DATA_MARK "\xd5\xaa\x0b" 1711da177e4SLinus Torvalds #define GCR_SLIP_BYTE "\x27\xaa" 1721da177e4SLinus Torvalds #define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f" 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds #define DATA_99 "\x99\x99" 1751da177e4SLinus Torvalds #define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe" 1761da177e4SLinus Torvalds #define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc" 1771da177e4SLinus Torvalds #define MFM_GAP_LEN 12 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds struct floppy_state { 1801da177e4SLinus Torvalds enum swim_state state; 1811da177e4SLinus Torvalds struct swim3 __iomem *swim3; /* hardware registers */ 1821da177e4SLinus Torvalds struct dbdma_regs __iomem *dma; /* DMA controller registers */ 1831da177e4SLinus Torvalds int swim3_intr; /* interrupt number for SWIM3 */ 1841da177e4SLinus Torvalds int dma_intr; /* interrupt number for DMA channel */ 1851da177e4SLinus Torvalds int cur_cyl; /* cylinder head is on, or -1 */ 1861da177e4SLinus Torvalds int cur_sector; /* last sector we saw go past */ 1871da177e4SLinus Torvalds int req_cyl; /* the cylinder for the current r/w request */ 1881da177e4SLinus Torvalds int head; /* head number ditto */ 1891da177e4SLinus Torvalds int req_sector; /* sector number ditto */ 1901da177e4SLinus Torvalds int scount; /* # sectors we're transferring at present */ 1911da177e4SLinus Torvalds int retries; 1921da177e4SLinus Torvalds int settle_time; 1931da177e4SLinus Torvalds int secpercyl; /* disk geometry information */ 1941da177e4SLinus Torvalds int secpertrack; 1951da177e4SLinus Torvalds int total_secs; 1961da177e4SLinus Torvalds int write_prot; /* 1 if write-protected, 0 if not, -1 dunno */ 1971da177e4SLinus Torvalds struct dbdma_cmd *dma_cmd; 1981da177e4SLinus Torvalds int ref_count; 1991da177e4SLinus Torvalds int expect_cyl; 2001da177e4SLinus Torvalds struct timer_list timeout; 2011da177e4SLinus Torvalds int timeout_pending; 2021da177e4SLinus Torvalds int ejected; 2031da177e4SLinus Torvalds wait_queue_head_t wait; 2041da177e4SLinus Torvalds int wanted; 205d58b0c39SBenjamin Herrenschmidt struct macio_dev *mdev; 2061da177e4SLinus Torvalds char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)]; 207b3025457SBenjamin Herrenschmidt int index; 208b3025457SBenjamin Herrenschmidt struct request *cur_req; 2098ccb8cb1SOmar Sandoval struct blk_mq_tag_set tag_set; 2101da177e4SLinus Torvalds }; 2111da177e4SLinus Torvalds 212b3025457SBenjamin Herrenschmidt #define swim3_err(fmt, arg...) dev_err(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) 213b3025457SBenjamin Herrenschmidt #define swim3_warn(fmt, arg...) dev_warn(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) 214b3025457SBenjamin Herrenschmidt #define swim3_info(fmt, arg...) dev_info(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) 215b3025457SBenjamin Herrenschmidt 216b3025457SBenjamin Herrenschmidt #ifdef DEBUG 217b3025457SBenjamin Herrenschmidt #define swim3_dbg(fmt, arg...) dev_dbg(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) 218b3025457SBenjamin Herrenschmidt #else 219b3025457SBenjamin Herrenschmidt #define swim3_dbg(fmt, arg...) do { } while(0) 220b3025457SBenjamin Herrenschmidt #endif 221b3025457SBenjamin Herrenschmidt 2221da177e4SLinus Torvalds static struct floppy_state floppy_states[MAX_FLOPPIES]; 2231da177e4SLinus Torvalds static int floppy_count = 0; 2241da177e4SLinus Torvalds static DEFINE_SPINLOCK(swim3_lock); 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds static unsigned short write_preamble[] = { 2271da177e4SLinus Torvalds 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */ 2281da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, /* sync field */ 2291da177e4SLinus Torvalds 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */ 2301da177e4SLinus Torvalds 0x990f /* no escape for 512 bytes */ 2311da177e4SLinus Torvalds }; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds static unsigned short write_postamble[] = { 2341da177e4SLinus Torvalds 0x9904, /* insert CRC */ 2351da177e4SLinus Torvalds 0x4e4e, 0x4e4e, 2361da177e4SLinus Torvalds 0x9908, /* stop writing */ 2371da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0 2381da177e4SLinus Torvalds }; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static void seek_track(struct floppy_state *fs, int n); 2411da177e4SLinus Torvalds static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count); 2421da177e4SLinus Torvalds static void act(struct floppy_state *fs); 243b5775a6bSKees Cook static void scan_timeout(struct timer_list *t); 244b5775a6bSKees Cook static void seek_timeout(struct timer_list *t); 245b5775a6bSKees Cook static void settle_timeout(struct timer_list *t); 246b5775a6bSKees Cook static void xfer_timeout(struct timer_list *t); 2477d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id); 2487d12e780SDavid Howells /*static void fd_dma_interrupt(int irq, void *dev_id);*/ 2491da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state, 2501da177e4SLinus Torvalds int interruptible); 2511da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs); 2521da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs); 253b4d9a442SAl Viro static int floppy_ioctl(struct block_device *bdev, fmode_t mode, 2541da177e4SLinus Torvalds unsigned int cmd, unsigned long param); 255b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode); 256db2a144bSAl Viro static void floppy_release(struct gendisk *disk, fmode_t mode); 2574bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk, 2584bbde777STejun Heo unsigned int clearing); 2591da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk); 2601da177e4SLinus Torvalds 2612a842acaSChristoph Hellwig static bool swim3_end_request(struct floppy_state *fs, blk_status_t err, unsigned int nr_bytes) 262f4bd4b90STejun Heo { 263b3025457SBenjamin Herrenschmidt struct request *req = fs->cur_req; 264b3025457SBenjamin Herrenschmidt 265b3025457SBenjamin Herrenschmidt swim3_dbg(" end request, err=%d nr_bytes=%d, cur_req=%p\n", 266b3025457SBenjamin Herrenschmidt err, nr_bytes, req); 267b3025457SBenjamin Herrenschmidt 268b3025457SBenjamin Herrenschmidt if (err) 269b3025457SBenjamin Herrenschmidt nr_bytes = blk_rq_cur_bytes(req); 2708ccb8cb1SOmar Sandoval if (blk_update_request(req, err, nr_bytes)) 271f4bd4b90STejun Heo return true; 2728ccb8cb1SOmar Sandoval __blk_mq_end_request(req, err); 273b3025457SBenjamin Herrenschmidt fs->cur_req = NULL; 274f4bd4b90STejun Heo return false; 275f4bd4b90STejun Heo } 276f4bd4b90STejun Heo 2771da177e4SLinus Torvalds static void swim3_select(struct floppy_state *fs, int sel) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds out_8(&sw->select, RELAX); 2821da177e4SLinus Torvalds if (sel & 8) 2831da177e4SLinus Torvalds out_8(&sw->control_bis, SELECT); 2841da177e4SLinus Torvalds else 2851da177e4SLinus Torvalds out_8(&sw->control_bic, SELECT); 2861da177e4SLinus Torvalds out_8(&sw->select, sel & CA_MASK); 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static void swim3_action(struct floppy_state *fs, int action) 2901da177e4SLinus Torvalds { 2911da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds swim3_select(fs, action); 2941da177e4SLinus Torvalds udelay(1); 2951da177e4SLinus Torvalds out_8(&sw->select, sw->select | LSTRB); 2961da177e4SLinus Torvalds udelay(2); 2971da177e4SLinus Torvalds out_8(&sw->select, sw->select & ~LSTRB); 2981da177e4SLinus Torvalds udelay(1); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds static int swim3_readbit(struct floppy_state *fs, int bit) 3021da177e4SLinus Torvalds { 3031da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 3041da177e4SLinus Torvalds int stat; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds swim3_select(fs, bit); 3071da177e4SLinus Torvalds udelay(1); 3081da177e4SLinus Torvalds stat = in_8(&sw->status); 3091da177e4SLinus Torvalds return (stat & DATA) == 0; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3128ccb8cb1SOmar Sandoval static blk_status_t swim3_queue_rq(struct blk_mq_hw_ctx *hctx, 3138ccb8cb1SOmar Sandoval const struct blk_mq_queue_data *bd) 3141da177e4SLinus Torvalds { 3158ccb8cb1SOmar Sandoval struct floppy_state *fs = hctx->queue->queuedata; 3168ccb8cb1SOmar Sandoval struct request *req = bd->rq; 3171da177e4SLinus Torvalds unsigned long x; 3181da177e4SLinus Torvalds 3198ccb8cb1SOmar Sandoval spin_lock_irq(&swim3_lock); 3208ccb8cb1SOmar Sandoval if (fs->cur_req || fs->state != idle) { 3218ccb8cb1SOmar Sandoval spin_unlock_irq(&swim3_lock); 3228ccb8cb1SOmar Sandoval return BLK_STS_DEV_RESOURCE; 3231da177e4SLinus Torvalds } 3248ccb8cb1SOmar Sandoval blk_mq_start_request(req); 3258ccb8cb1SOmar Sandoval fs->cur_req = req; 326b3025457SBenjamin Herrenschmidt if (fs->mdev->media_bay && 327b3025457SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) { 328b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " media bay absent, dropping req\n"); 3292a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 3308ccb8cb1SOmar Sandoval goto out; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds if (fs->ejected) { 333b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " disk ejected\n"); 3342a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 3358ccb8cb1SOmar Sandoval goto out; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds if (rq_data_dir(req) == WRITE) { 3381da177e4SLinus Torvalds if (fs->write_prot < 0) 3391da177e4SLinus Torvalds fs->write_prot = swim3_readbit(fs, WRITE_PROT); 3401da177e4SLinus Torvalds if (fs->write_prot) { 341b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " try to write, disk write protected\n"); 3422a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 3438ccb8cb1SOmar Sandoval goto out; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3478ccb8cb1SOmar Sandoval /* 3488ccb8cb1SOmar Sandoval * Do not remove the cast. blk_rq_pos(req) is now a sector_t and can be 3498ccb8cb1SOmar Sandoval * 64 bits, but it will never go past 32 bits for this driver anyway, so 3508ccb8cb1SOmar Sandoval * we can safely cast it down and not have to do a 64/32 division 3511da177e4SLinus Torvalds */ 35283096ebfSTejun Heo fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl; 35383096ebfSTejun Heo x = ((long)blk_rq_pos(req)) % fs->secpercyl; 3541da177e4SLinus Torvalds fs->head = x / fs->secpertrack; 3551da177e4SLinus Torvalds fs->req_sector = x % fs->secpertrack + 1; 3561da177e4SLinus Torvalds fs->state = do_transfer; 3571da177e4SLinus Torvalds fs->retries = 0; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds act(fs); 3601da177e4SLinus Torvalds 3618ccb8cb1SOmar Sandoval out: 3628ccb8cb1SOmar Sandoval spin_unlock_irq(&swim3_lock); 3638ccb8cb1SOmar Sandoval return BLK_STS_OK; 364b3025457SBenjamin Herrenschmidt } 365b3025457SBenjamin Herrenschmidt 3661da177e4SLinus Torvalds static void set_timeout(struct floppy_state *fs, int nticks, 367b5775a6bSKees Cook void (*proc)(struct timer_list *t)) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds if (fs->timeout_pending) 3701da177e4SLinus Torvalds del_timer(&fs->timeout); 3711da177e4SLinus Torvalds fs->timeout.expires = jiffies + nticks; 372841b86f3SKees Cook fs->timeout.function = proc; 3731da177e4SLinus Torvalds add_timer(&fs->timeout); 3741da177e4SLinus Torvalds fs->timeout_pending = 1; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds static inline void scan_track(struct floppy_state *fs) 3781da177e4SLinus Torvalds { 3791da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds swim3_select(fs, READ_DATA_0); 3821da177e4SLinus Torvalds in_8(&sw->intr); /* clear SEEN_SECTOR bit */ 3831da177e4SLinus Torvalds in_8(&sw->error); 3841da177e4SLinus Torvalds out_8(&sw->intr_enable, SEEN_SECTOR); 3851da177e4SLinus Torvalds out_8(&sw->control_bis, DO_ACTION); 3861da177e4SLinus Torvalds /* enable intr when track found */ 3871da177e4SLinus Torvalds set_timeout(fs, HZ, scan_timeout); /* enable timeout */ 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds static inline void seek_track(struct floppy_state *fs, int n) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds if (n >= 0) { 3951da177e4SLinus Torvalds swim3_action(fs, SEEK_POSITIVE); 3961da177e4SLinus Torvalds sw->nseek = n; 3971da177e4SLinus Torvalds } else { 3981da177e4SLinus Torvalds swim3_action(fs, SEEK_NEGATIVE); 3991da177e4SLinus Torvalds sw->nseek = -n; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1; 4021da177e4SLinus Torvalds swim3_select(fs, STEP); 4031da177e4SLinus Torvalds in_8(&sw->error); 4041da177e4SLinus Torvalds /* enable intr when seek finished */ 4051da177e4SLinus Torvalds out_8(&sw->intr_enable, SEEK_DONE); 4061da177e4SLinus Torvalds out_8(&sw->control_bis, DO_SEEK); 4071da177e4SLinus Torvalds set_timeout(fs, 3*HZ, seek_timeout); /* enable timeout */ 4081da177e4SLinus Torvalds fs->settle_time = 0; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds static inline void init_dma(struct dbdma_cmd *cp, int cmd, 4121da177e4SLinus Torvalds void *buf, int count) 4131da177e4SLinus Torvalds { 414f5718726SDavid Gibson cp->req_count = cpu_to_le16(count); 415f5718726SDavid Gibson cp->command = cpu_to_le16(cmd); 416f5718726SDavid Gibson cp->phy_addr = cpu_to_le32(virt_to_bus(buf)); 4171da177e4SLinus Torvalds cp->xfer_status = 0; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds static inline void setup_transfer(struct floppy_state *fs) 4211da177e4SLinus Torvalds { 4221da177e4SLinus Torvalds int n; 4231da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 4241da177e4SLinus Torvalds struct dbdma_cmd *cp = fs->dma_cmd; 4251da177e4SLinus Torvalds struct dbdma_regs __iomem *dr = fs->dma; 426b3025457SBenjamin Herrenschmidt struct request *req = fs->cur_req; 4271da177e4SLinus Torvalds 428b3025457SBenjamin Herrenschmidt if (blk_rq_cur_sectors(req) <= 0) { 429b3025457SBenjamin Herrenschmidt swim3_warn("%s", "Transfer 0 sectors ?\n"); 4301da177e4SLinus Torvalds return; 4311da177e4SLinus Torvalds } 432b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) 4331da177e4SLinus Torvalds n = 1; 4341da177e4SLinus Torvalds else { 4351da177e4SLinus Torvalds n = fs->secpertrack - fs->req_sector + 1; 436b3025457SBenjamin Herrenschmidt if (n > blk_rq_cur_sectors(req)) 437b3025457SBenjamin Herrenschmidt n = blk_rq_cur_sectors(req); 4381da177e4SLinus Torvalds } 439b3025457SBenjamin Herrenschmidt 440b3025457SBenjamin Herrenschmidt swim3_dbg(" setup xfer at sect %d (of %d) head %d for %d\n", 441b3025457SBenjamin Herrenschmidt fs->req_sector, fs->secpertrack, fs->head, n); 442b3025457SBenjamin Herrenschmidt 4431da177e4SLinus Torvalds fs->scount = n; 4441da177e4SLinus Torvalds swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0); 4451da177e4SLinus Torvalds out_8(&sw->sector, fs->req_sector); 4461da177e4SLinus Torvalds out_8(&sw->nsect, n); 4471da177e4SLinus Torvalds out_8(&sw->gap3, 0); 4481da177e4SLinus Torvalds out_le32(&dr->cmdptr, virt_to_bus(cp)); 449b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) { 4501da177e4SLinus Torvalds /* Set up 3 dma commands: write preamble, data, postamble */ 4511da177e4SLinus Torvalds init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble)); 4521da177e4SLinus Torvalds ++cp; 453b4f42e28SJens Axboe init_dma(cp, OUTPUT_MORE, bio_data(req->bio), 512); 4541da177e4SLinus Torvalds ++cp; 4551da177e4SLinus Torvalds init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble)); 4561da177e4SLinus Torvalds } else { 457b4f42e28SJens Axboe init_dma(cp, INPUT_LAST, bio_data(req->bio), n * 512); 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds ++cp; 4601da177e4SLinus Torvalds out_le16(&cp->command, DBDMA_STOP); 4611da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 4621da177e4SLinus Torvalds in_8(&sw->error); 4631da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 464b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) 4651da177e4SLinus Torvalds out_8(&sw->control_bis, WRITE_SECTORS); 4661da177e4SLinus Torvalds in_8(&sw->intr); 4671da177e4SLinus Torvalds out_le32(&dr->control, (RUN << 16) | RUN); 4681da177e4SLinus Torvalds /* enable intr when transfer complete */ 4691da177e4SLinus Torvalds out_8(&sw->intr_enable, TRANSFER_DONE); 4701da177e4SLinus Torvalds out_8(&sw->control_bis, DO_ACTION); 4711da177e4SLinus Torvalds set_timeout(fs, 2*HZ, xfer_timeout); /* enable timeout */ 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds static void act(struct floppy_state *fs) 4751da177e4SLinus Torvalds { 4761da177e4SLinus Torvalds for (;;) { 477b3025457SBenjamin Herrenschmidt swim3_dbg(" act loop, state=%d, req_cyl=%d, cur_cyl=%d\n", 478b3025457SBenjamin Herrenschmidt fs->state, fs->req_cyl, fs->cur_cyl); 479b3025457SBenjamin Herrenschmidt 4801da177e4SLinus Torvalds switch (fs->state) { 4811da177e4SLinus Torvalds case idle: 4821da177e4SLinus Torvalds return; /* XXX shouldn't get here */ 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds case locating: 4851da177e4SLinus Torvalds if (swim3_readbit(fs, TRACK_ZERO)) { 486b3025457SBenjamin Herrenschmidt swim3_dbg("%s", " locate track 0\n"); 4871da177e4SLinus Torvalds fs->cur_cyl = 0; 4881da177e4SLinus Torvalds if (fs->req_cyl == 0) 4891da177e4SLinus Torvalds fs->state = do_transfer; 4901da177e4SLinus Torvalds else 4911da177e4SLinus Torvalds fs->state = seeking; 4921da177e4SLinus Torvalds break; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds scan_track(fs); 4951da177e4SLinus Torvalds return; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds case seeking: 4981da177e4SLinus Torvalds if (fs->cur_cyl < 0) { 4991da177e4SLinus Torvalds fs->expect_cyl = -1; 5001da177e4SLinus Torvalds fs->state = locating; 5011da177e4SLinus Torvalds break; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds if (fs->req_cyl == fs->cur_cyl) { 504b3025457SBenjamin Herrenschmidt swim3_warn("%s", "Whoops, seeking 0\n"); 5051da177e4SLinus Torvalds fs->state = do_transfer; 5061da177e4SLinus Torvalds break; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds seek_track(fs, fs->req_cyl - fs->cur_cyl); 5091da177e4SLinus Torvalds return; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds case settling: 5121da177e4SLinus Torvalds /* check for SEEK_COMPLETE after 30ms */ 5131da177e4SLinus Torvalds fs->settle_time = (HZ + 32) / 33; 5141da177e4SLinus Torvalds set_timeout(fs, fs->settle_time, settle_timeout); 5151da177e4SLinus Torvalds return; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds case do_transfer: 5181da177e4SLinus Torvalds if (fs->cur_cyl != fs->req_cyl) { 5191da177e4SLinus Torvalds if (fs->retries > 5) { 520b3025457SBenjamin Herrenschmidt swim3_err("Wrong cylinder in transfer, want: %d got %d\n", 521b3025457SBenjamin Herrenschmidt fs->req_cyl, fs->cur_cyl); 5222a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 5231da177e4SLinus Torvalds fs->state = idle; 5241da177e4SLinus Torvalds return; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds fs->state = seeking; 5271da177e4SLinus Torvalds break; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds setup_transfer(fs); 5301da177e4SLinus Torvalds return; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds case jogging: 5331da177e4SLinus Torvalds seek_track(fs, -5); 5341da177e4SLinus Torvalds return; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds default: 537b3025457SBenjamin Herrenschmidt swim3_err("Unknown state %d\n", fs->state); 5381da177e4SLinus Torvalds return; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 543b5775a6bSKees Cook static void scan_timeout(struct timer_list *t) 5441da177e4SLinus Torvalds { 545b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 5461da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 547b3025457SBenjamin Herrenschmidt unsigned long flags; 5481da177e4SLinus Torvalds 549b3025457SBenjamin Herrenschmidt swim3_dbg("* scan timeout, state=%d\n", fs->state); 550b3025457SBenjamin Herrenschmidt 551b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 5521da177e4SLinus Torvalds fs->timeout_pending = 0; 5531da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 5541da177e4SLinus Torvalds out_8(&sw->select, RELAX); 5551da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 5561da177e4SLinus Torvalds fs->cur_cyl = -1; 5571da177e4SLinus Torvalds if (fs->retries > 5) { 5582a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 5591da177e4SLinus Torvalds fs->state = idle; 5601da177e4SLinus Torvalds } else { 5611da177e4SLinus Torvalds fs->state = jogging; 5621da177e4SLinus Torvalds act(fs); 5631da177e4SLinus Torvalds } 564b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 567b5775a6bSKees Cook static void seek_timeout(struct timer_list *t) 5681da177e4SLinus Torvalds { 569b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 5701da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 571b3025457SBenjamin Herrenschmidt unsigned long flags; 5721da177e4SLinus Torvalds 573b3025457SBenjamin Herrenschmidt swim3_dbg("* seek timeout, state=%d\n", fs->state); 574b3025457SBenjamin Herrenschmidt 575b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 5761da177e4SLinus Torvalds fs->timeout_pending = 0; 5771da177e4SLinus Torvalds out_8(&sw->control_bic, DO_SEEK); 5781da177e4SLinus Torvalds out_8(&sw->select, RELAX); 5791da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 580b3025457SBenjamin Herrenschmidt swim3_err("%s", "Seek timeout\n"); 5812a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 5821da177e4SLinus Torvalds fs->state = idle; 583b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 586b5775a6bSKees Cook static void settle_timeout(struct timer_list *t) 5871da177e4SLinus Torvalds { 588b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 5891da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 590b3025457SBenjamin Herrenschmidt unsigned long flags; 5911da177e4SLinus Torvalds 592b3025457SBenjamin Herrenschmidt swim3_dbg("* settle timeout, state=%d\n", fs->state); 593b3025457SBenjamin Herrenschmidt 594b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 5951da177e4SLinus Torvalds fs->timeout_pending = 0; 5961da177e4SLinus Torvalds if (swim3_readbit(fs, SEEK_COMPLETE)) { 5971da177e4SLinus Torvalds out_8(&sw->select, RELAX); 5981da177e4SLinus Torvalds fs->state = locating; 5991da177e4SLinus Torvalds act(fs); 600b3025457SBenjamin Herrenschmidt goto unlock; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds out_8(&sw->select, RELAX); 6031da177e4SLinus Torvalds if (fs->settle_time < 2*HZ) { 6041da177e4SLinus Torvalds ++fs->settle_time; 6051da177e4SLinus Torvalds set_timeout(fs, 1, settle_timeout); 606b3025457SBenjamin Herrenschmidt goto unlock; 6071da177e4SLinus Torvalds } 608b3025457SBenjamin Herrenschmidt swim3_err("%s", "Seek settle timeout\n"); 6092a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 6101da177e4SLinus Torvalds fs->state = idle; 611b3025457SBenjamin Herrenschmidt unlock: 612b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 615b5775a6bSKees Cook static void xfer_timeout(struct timer_list *t) 6161da177e4SLinus Torvalds { 617b5775a6bSKees Cook struct floppy_state *fs = from_timer(fs, t, timeout); 6181da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 6191da177e4SLinus Torvalds struct dbdma_regs __iomem *dr = fs->dma; 620b3025457SBenjamin Herrenschmidt unsigned long flags; 6211da177e4SLinus Torvalds int n; 6221da177e4SLinus Torvalds 623b3025457SBenjamin Herrenschmidt swim3_dbg("* xfer timeout, state=%d\n", fs->state); 624b3025457SBenjamin Herrenschmidt 625b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 6261da177e4SLinus Torvalds fs->timeout_pending = 0; 6271da177e4SLinus Torvalds out_le32(&dr->control, RUN << 16); 6281da177e4SLinus Torvalds /* We must wait a bit for dbdma to stop */ 6291da177e4SLinus Torvalds for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++) 6301da177e4SLinus Torvalds udelay(1); 6311da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 6321da177e4SLinus Torvalds out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); 6331da177e4SLinus Torvalds out_8(&sw->select, RELAX); 634b3025457SBenjamin Herrenschmidt swim3_err("Timeout %sing sector %ld\n", 635b3025457SBenjamin Herrenschmidt (rq_data_dir(fs->cur_req)==WRITE? "writ": "read"), 636b3025457SBenjamin Herrenschmidt (long)blk_rq_pos(fs->cur_req)); 6372a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 6381da177e4SLinus Torvalds fs->state = idle; 639b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6427d12e780SDavid Howells static irqreturn_t swim3_interrupt(int irq, void *dev_id) 6431da177e4SLinus Torvalds { 6441da177e4SLinus Torvalds struct floppy_state *fs = (struct floppy_state *) dev_id; 6451da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 6461da177e4SLinus Torvalds int intr, err, n; 6471da177e4SLinus Torvalds int stat, resid; 6481da177e4SLinus Torvalds struct dbdma_regs __iomem *dr; 6491da177e4SLinus Torvalds struct dbdma_cmd *cp; 650b3025457SBenjamin Herrenschmidt unsigned long flags; 651b3025457SBenjamin Herrenschmidt struct request *req = fs->cur_req; 6521da177e4SLinus Torvalds 653b3025457SBenjamin Herrenschmidt swim3_dbg("* interrupt, state=%d\n", fs->state); 654b3025457SBenjamin Herrenschmidt 655b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 6561da177e4SLinus Torvalds intr = in_8(&sw->intr); 6571da177e4SLinus Torvalds err = (intr & ERROR_INTR)? in_8(&sw->error): 0; 6581da177e4SLinus Torvalds if ((intr & ERROR_INTR) && fs->state != do_transfer) 659b3025457SBenjamin Herrenschmidt swim3_err("Non-transfer error interrupt: state=%d, dir=%x, intr=%x, err=%x\n", 660b3025457SBenjamin Herrenschmidt fs->state, rq_data_dir(req), intr, err); 6611da177e4SLinus Torvalds switch (fs->state) { 6621da177e4SLinus Torvalds case locating: 6631da177e4SLinus Torvalds if (intr & SEEN_SECTOR) { 6641da177e4SLinus Torvalds out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 6651da177e4SLinus Torvalds out_8(&sw->select, RELAX); 6661da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 6671da177e4SLinus Torvalds del_timer(&fs->timeout); 6681da177e4SLinus Torvalds fs->timeout_pending = 0; 6691da177e4SLinus Torvalds if (sw->ctrack == 0xff) { 670b3025457SBenjamin Herrenschmidt swim3_err("%s", "Seen sector but cyl=ff?\n"); 6711da177e4SLinus Torvalds fs->cur_cyl = -1; 6721da177e4SLinus Torvalds if (fs->retries > 5) { 6732a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 6741da177e4SLinus Torvalds fs->state = idle; 6751da177e4SLinus Torvalds } else { 6761da177e4SLinus Torvalds fs->state = jogging; 6771da177e4SLinus Torvalds act(fs); 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds break; 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds fs->cur_cyl = sw->ctrack; 6821da177e4SLinus Torvalds fs->cur_sector = sw->csect; 6831da177e4SLinus Torvalds if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl) 684b3025457SBenjamin Herrenschmidt swim3_err("Expected cyl %d, got %d\n", 6851da177e4SLinus Torvalds fs->expect_cyl, fs->cur_cyl); 6861da177e4SLinus Torvalds fs->state = do_transfer; 6871da177e4SLinus Torvalds act(fs); 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds break; 6901da177e4SLinus Torvalds case seeking: 6911da177e4SLinus Torvalds case jogging: 6921da177e4SLinus Torvalds if (sw->nseek == 0) { 6931da177e4SLinus Torvalds out_8(&sw->control_bic, DO_SEEK); 6941da177e4SLinus Torvalds out_8(&sw->select, RELAX); 6951da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 6961da177e4SLinus Torvalds del_timer(&fs->timeout); 6971da177e4SLinus Torvalds fs->timeout_pending = 0; 6981da177e4SLinus Torvalds if (fs->state == seeking) 6991da177e4SLinus Torvalds ++fs->retries; 7001da177e4SLinus Torvalds fs->state = settling; 7011da177e4SLinus Torvalds act(fs); 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds break; 7041da177e4SLinus Torvalds case settling: 7051da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 7061da177e4SLinus Torvalds del_timer(&fs->timeout); 7071da177e4SLinus Torvalds fs->timeout_pending = 0; 7081da177e4SLinus Torvalds act(fs); 7091da177e4SLinus Torvalds break; 7101da177e4SLinus Torvalds case do_transfer: 7111da177e4SLinus Torvalds if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0) 7121da177e4SLinus Torvalds break; 7131da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 7141da177e4SLinus Torvalds out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); 7151da177e4SLinus Torvalds out_8(&sw->select, RELAX); 7161da177e4SLinus Torvalds del_timer(&fs->timeout); 7171da177e4SLinus Torvalds fs->timeout_pending = 0; 7181da177e4SLinus Torvalds dr = fs->dma; 7191da177e4SLinus Torvalds cp = fs->dma_cmd; 720b3025457SBenjamin Herrenschmidt if (rq_data_dir(req) == WRITE) 7211da177e4SLinus Torvalds ++cp; 7221da177e4SLinus Torvalds /* 7231da177e4SLinus Torvalds * Check that the main data transfer has finished. 7241da177e4SLinus Torvalds * On writing, the swim3 sometimes doesn't use 7251da177e4SLinus Torvalds * up all the bytes of the postamble, so we can still 7261da177e4SLinus Torvalds * see DMA active here. That doesn't matter as long 7271da177e4SLinus Torvalds * as all the sector data has been transferred. 7281da177e4SLinus Torvalds */ 7291da177e4SLinus Torvalds if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) { 7301da177e4SLinus Torvalds /* wait a little while for DMA to complete */ 7311da177e4SLinus Torvalds for (n = 0; n < 100; ++n) { 7321da177e4SLinus Torvalds if (cp->xfer_status != 0) 7331da177e4SLinus Torvalds break; 7341da177e4SLinus Torvalds udelay(1); 7351da177e4SLinus Torvalds barrier(); 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds /* turn off DMA */ 7391da177e4SLinus Torvalds out_le32(&dr->control, (RUN | PAUSE) << 16); 740f5718726SDavid Gibson stat = le16_to_cpu(cp->xfer_status); 741f5718726SDavid Gibson resid = le16_to_cpu(cp->res_count); 7421da177e4SLinus Torvalds if (intr & ERROR_INTR) { 7431da177e4SLinus Torvalds n = fs->scount - 1 - resid / 512; 7441da177e4SLinus Torvalds if (n > 0) { 745b3025457SBenjamin Herrenschmidt blk_update_request(req, 0, n << 9); 7461da177e4SLinus Torvalds fs->req_sector += n; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds if (fs->retries < 5) { 7491da177e4SLinus Torvalds ++fs->retries; 7501da177e4SLinus Torvalds act(fs); 7511da177e4SLinus Torvalds } else { 752b3025457SBenjamin Herrenschmidt swim3_err("Error %sing block %ld (err=%x)\n", 753b3025457SBenjamin Herrenschmidt rq_data_dir(req) == WRITE? "writ": "read", 754b3025457SBenjamin Herrenschmidt (long)blk_rq_pos(req), err); 7552a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 7561da177e4SLinus Torvalds fs->state = idle; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds } else { 7591da177e4SLinus Torvalds if ((stat & ACTIVE) == 0 || resid != 0) { 7601da177e4SLinus Torvalds /* musta been an error */ 761b3025457SBenjamin Herrenschmidt swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid); 762b3025457SBenjamin Herrenschmidt swim3_err(" state=%d, dir=%x, intr=%x, err=%x\n", 763b3025457SBenjamin Herrenschmidt fs->state, rq_data_dir(req), intr, err); 7642a842acaSChristoph Hellwig swim3_end_request(fs, BLK_STS_IOERR, 0); 7651da177e4SLinus Torvalds fs->state = idle; 7661da177e4SLinus Torvalds break; 7671da177e4SLinus Torvalds } 768b3025457SBenjamin Herrenschmidt fs->retries = 0; 769b3025457SBenjamin Herrenschmidt if (swim3_end_request(fs, 0, fs->scount << 9)) { 7701da177e4SLinus Torvalds fs->req_sector += fs->scount; 7711da177e4SLinus Torvalds if (fs->req_sector > fs->secpertrack) { 7721da177e4SLinus Torvalds fs->req_sector -= fs->secpertrack; 7731da177e4SLinus Torvalds if (++fs->head > 1) { 7741da177e4SLinus Torvalds fs->head = 0; 7751da177e4SLinus Torvalds ++fs->req_cyl; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds act(fs); 779467ca759STejun Heo } else 780467ca759STejun Heo fs->state = idle; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds break; 7831da177e4SLinus Torvalds default: 784b3025457SBenjamin Herrenschmidt swim3_err("Don't know what to do in state %d\n", fs->state); 7851da177e4SLinus Torvalds } 786b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 7871da177e4SLinus Torvalds return IRQ_HANDLED; 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds /* 7917d12e780SDavid Howells static void fd_dma_interrupt(int irq, void *dev_id) 7921da177e4SLinus Torvalds { 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds */ 7951da177e4SLinus Torvalds 796b3025457SBenjamin Herrenschmidt /* Called under the mutex to grab exclusive access to a drive */ 7971da177e4SLinus Torvalds static int grab_drive(struct floppy_state *fs, enum swim_state state, 7981da177e4SLinus Torvalds int interruptible) 7991da177e4SLinus Torvalds { 8001da177e4SLinus Torvalds unsigned long flags; 8011da177e4SLinus Torvalds 802b3025457SBenjamin Herrenschmidt swim3_dbg("%s", "-> grab drive\n"); 803b3025457SBenjamin Herrenschmidt 804b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 805b3025457SBenjamin Herrenschmidt if (fs->state != idle && fs->state != available) { 8061da177e4SLinus Torvalds ++fs->wanted; 807106fd892SArnd Bergmann /* this will enable irqs in order to sleep */ 808106fd892SArnd Bergmann if (!interruptible) 809106fd892SArnd Bergmann wait_event_lock_irq(fs->wait, 810106fd892SArnd Bergmann fs->state == available, 811106fd892SArnd Bergmann swim3_lock); 812106fd892SArnd Bergmann else if (wait_event_interruptible_lock_irq(fs->wait, 813106fd892SArnd Bergmann fs->state == available, 814106fd892SArnd Bergmann swim3_lock)) { 8151da177e4SLinus Torvalds --fs->wanted; 816106fd892SArnd Bergmann spin_unlock_irqrestore(&swim3_lock, flags); 8171da177e4SLinus Torvalds return -EINTR; 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds --fs->wanted; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds fs->state = state; 822b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 823b3025457SBenjamin Herrenschmidt 8241da177e4SLinus Torvalds return 0; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds static void release_drive(struct floppy_state *fs) 8281da177e4SLinus Torvalds { 8298ccb8cb1SOmar Sandoval struct request_queue *q = disks[fs->index]->queue; 8301da177e4SLinus Torvalds unsigned long flags; 8311da177e4SLinus Torvalds 832b3025457SBenjamin Herrenschmidt swim3_dbg("%s", "-> release drive\n"); 833b3025457SBenjamin Herrenschmidt 834b3025457SBenjamin Herrenschmidt spin_lock_irqsave(&swim3_lock, flags); 8351da177e4SLinus Torvalds fs->state = idle; 836b3025457SBenjamin Herrenschmidt spin_unlock_irqrestore(&swim3_lock, flags); 8378ccb8cb1SOmar Sandoval 8388ccb8cb1SOmar Sandoval blk_mq_freeze_queue(q); 8398ccb8cb1SOmar Sandoval blk_mq_quiesce_queue(q); 8408ccb8cb1SOmar Sandoval blk_mq_unquiesce_queue(q); 8418ccb8cb1SOmar Sandoval blk_mq_unfreeze_queue(q); 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds static int fd_eject(struct floppy_state *fs) 8451da177e4SLinus Torvalds { 8461da177e4SLinus Torvalds int err, n; 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds err = grab_drive(fs, ejecting, 1); 8491da177e4SLinus Torvalds if (err) 8501da177e4SLinus Torvalds return err; 8511da177e4SLinus Torvalds swim3_action(fs, EJECT); 8521da177e4SLinus Torvalds for (n = 20; n > 0; --n) { 8531da177e4SLinus Torvalds if (signal_pending(current)) { 8541da177e4SLinus Torvalds err = -EINTR; 8551da177e4SLinus Torvalds break; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds swim3_select(fs, RELAX); 85886e84862SNishanth Aravamudan schedule_timeout_interruptible(1); 8591da177e4SLinus Torvalds if (swim3_readbit(fs, DISK_IN) == 0) 8601da177e4SLinus Torvalds break; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds swim3_select(fs, RELAX); 8631da177e4SLinus Torvalds udelay(150); 8641da177e4SLinus Torvalds fs->ejected = 1; 8651da177e4SLinus Torvalds release_drive(fs); 8661da177e4SLinus Torvalds return err; 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds static struct floppy_struct floppy_type = 8701da177e4SLinus Torvalds { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */ 8711da177e4SLinus Torvalds 8728a6cfeb6SArnd Bergmann static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode, 8731da177e4SLinus Torvalds unsigned int cmd, unsigned long param) 8741da177e4SLinus Torvalds { 875b4d9a442SAl Viro struct floppy_state *fs = bdev->bd_disk->private_data; 8761da177e4SLinus Torvalds int err; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)) 8791da177e4SLinus Torvalds return -EPERM; 8801da177e4SLinus Torvalds 881d58b0c39SBenjamin Herrenschmidt if (fs->mdev->media_bay && 882d58b0c39SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) 8831da177e4SLinus Torvalds return -ENXIO; 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds switch (cmd) { 8861da177e4SLinus Torvalds case FDEJECT: 8871da177e4SLinus Torvalds if (fs->ref_count != 1) 8881da177e4SLinus Torvalds return -EBUSY; 8891da177e4SLinus Torvalds err = fd_eject(fs); 8901da177e4SLinus Torvalds return err; 8911da177e4SLinus Torvalds case FDGETPRM: 8921da177e4SLinus Torvalds if (copy_to_user((void __user *) param, &floppy_type, 8931da177e4SLinus Torvalds sizeof(struct floppy_struct))) 8941da177e4SLinus Torvalds return -EFAULT; 8951da177e4SLinus Torvalds return 0; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds return -ENOTTY; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 9008a6cfeb6SArnd Bergmann static int floppy_ioctl(struct block_device *bdev, fmode_t mode, 9018a6cfeb6SArnd Bergmann unsigned int cmd, unsigned long param) 9028a6cfeb6SArnd Bergmann { 9038a6cfeb6SArnd Bergmann int ret; 9048a6cfeb6SArnd Bergmann 9052a48fc0aSArnd Bergmann mutex_lock(&swim3_mutex); 9068a6cfeb6SArnd Bergmann ret = floppy_locked_ioctl(bdev, mode, cmd, param); 9072a48fc0aSArnd Bergmann mutex_unlock(&swim3_mutex); 9088a6cfeb6SArnd Bergmann 9098a6cfeb6SArnd Bergmann return ret; 9108a6cfeb6SArnd Bergmann } 9118a6cfeb6SArnd Bergmann 912b4d9a442SAl Viro static int floppy_open(struct block_device *bdev, fmode_t mode) 9131da177e4SLinus Torvalds { 914b4d9a442SAl Viro struct floppy_state *fs = bdev->bd_disk->private_data; 9151da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 9161da177e4SLinus Torvalds int n, err = 0; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds if (fs->ref_count == 0) { 919d58b0c39SBenjamin Herrenschmidt if (fs->mdev->media_bay && 920d58b0c39SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) 9211da177e4SLinus Torvalds return -ENXIO; 9221da177e4SLinus Torvalds out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2); 9231da177e4SLinus Torvalds out_8(&sw->control_bic, 0xff); 9241da177e4SLinus Torvalds out_8(&sw->mode, 0x95); 9251da177e4SLinus Torvalds udelay(10); 9261da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 9271da177e4SLinus Torvalds out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE); 9281da177e4SLinus Torvalds swim3_action(fs, MOTOR_ON); 9291da177e4SLinus Torvalds fs->write_prot = -1; 9301da177e4SLinus Torvalds fs->cur_cyl = -1; 9311da177e4SLinus Torvalds for (n = 0; n < 2 * HZ; ++n) { 9321da177e4SLinus Torvalds if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE)) 9331da177e4SLinus Torvalds break; 9341da177e4SLinus Torvalds if (signal_pending(current)) { 9351da177e4SLinus Torvalds err = -EINTR; 9361da177e4SLinus Torvalds break; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds swim3_select(fs, RELAX); 93986e84862SNishanth Aravamudan schedule_timeout_interruptible(1); 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0 9421da177e4SLinus Torvalds || swim3_readbit(fs, DISK_IN) == 0)) 9431da177e4SLinus Torvalds err = -ENXIO; 9441da177e4SLinus Torvalds swim3_action(fs, SETMFM); 9451da177e4SLinus Torvalds swim3_select(fs, RELAX); 9461da177e4SLinus Torvalds 947b4d9a442SAl Viro } else if (fs->ref_count == -1 || mode & FMODE_EXCL) 9481da177e4SLinus Torvalds return -EBUSY; 9491da177e4SLinus Torvalds 950b4d9a442SAl Viro if (err == 0 && (mode & FMODE_NDELAY) == 0 951b4d9a442SAl Viro && (mode & (FMODE_READ|FMODE_WRITE))) { 952b4d9a442SAl Viro check_disk_change(bdev); 9531da177e4SLinus Torvalds if (fs->ejected) 9541da177e4SLinus Torvalds err = -ENXIO; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 957b4d9a442SAl Viro if (err == 0 && (mode & FMODE_WRITE)) { 9581da177e4SLinus Torvalds if (fs->write_prot < 0) 9591da177e4SLinus Torvalds fs->write_prot = swim3_readbit(fs, WRITE_PROT); 9601da177e4SLinus Torvalds if (fs->write_prot) 9611da177e4SLinus Torvalds err = -EROFS; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds if (err) { 9651da177e4SLinus Torvalds if (fs->ref_count == 0) { 9661da177e4SLinus Torvalds swim3_action(fs, MOTOR_OFF); 9671da177e4SLinus Torvalds out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE); 9681da177e4SLinus Torvalds swim3_select(fs, RELAX); 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds return err; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 973b4d9a442SAl Viro if (mode & FMODE_EXCL) 9741da177e4SLinus Torvalds fs->ref_count = -1; 9751da177e4SLinus Torvalds else 9761da177e4SLinus Torvalds ++fs->ref_count; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds return 0; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9816e9624b8SArnd Bergmann static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode) 9826e9624b8SArnd Bergmann { 9836e9624b8SArnd Bergmann int ret; 9846e9624b8SArnd Bergmann 9852a48fc0aSArnd Bergmann mutex_lock(&swim3_mutex); 9866e9624b8SArnd Bergmann ret = floppy_open(bdev, mode); 9872a48fc0aSArnd Bergmann mutex_unlock(&swim3_mutex); 9886e9624b8SArnd Bergmann 9896e9624b8SArnd Bergmann return ret; 9906e9624b8SArnd Bergmann } 9916e9624b8SArnd Bergmann 992db2a144bSAl Viro static void floppy_release(struct gendisk *disk, fmode_t mode) 9931da177e4SLinus Torvalds { 994b4d9a442SAl Viro struct floppy_state *fs = disk->private_data; 9951da177e4SLinus Torvalds struct swim3 __iomem *sw = fs->swim3; 996b3025457SBenjamin Herrenschmidt 9972a48fc0aSArnd Bergmann mutex_lock(&swim3_mutex); 998296dcc40SFinn Thain if (fs->ref_count > 0) 999296dcc40SFinn Thain --fs->ref_count; 1000296dcc40SFinn Thain else if (fs->ref_count == -1) 1001296dcc40SFinn Thain fs->ref_count = 0; 1002296dcc40SFinn Thain if (fs->ref_count == 0) { 10031da177e4SLinus Torvalds swim3_action(fs, MOTOR_OFF); 10041da177e4SLinus Torvalds out_8(&sw->control_bic, 0xff); 10051da177e4SLinus Torvalds swim3_select(fs, RELAX); 10061da177e4SLinus Torvalds } 10072a48fc0aSArnd Bergmann mutex_unlock(&swim3_mutex); 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 10104bbde777STejun Heo static unsigned int floppy_check_events(struct gendisk *disk, 10114bbde777STejun Heo unsigned int clearing) 10121da177e4SLinus Torvalds { 10131da177e4SLinus Torvalds struct floppy_state *fs = disk->private_data; 10144bbde777STejun Heo return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0; 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds static int floppy_revalidate(struct gendisk *disk) 10181da177e4SLinus Torvalds { 10191da177e4SLinus Torvalds struct floppy_state *fs = disk->private_data; 10201da177e4SLinus Torvalds struct swim3 __iomem *sw; 10211da177e4SLinus Torvalds int ret, n; 10221da177e4SLinus Torvalds 1023d58b0c39SBenjamin Herrenschmidt if (fs->mdev->media_bay && 1024d58b0c39SBenjamin Herrenschmidt check_media_bay(fs->mdev->media_bay) != MB_FD) 10251da177e4SLinus Torvalds return -ENXIO; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds sw = fs->swim3; 10281da177e4SLinus Torvalds grab_drive(fs, revalidating, 0); 10291da177e4SLinus Torvalds out_8(&sw->intr_enable, 0); 10301da177e4SLinus Torvalds out_8(&sw->control_bis, DRIVE_ENABLE); 10311da177e4SLinus Torvalds swim3_action(fs, MOTOR_ON); /* necessary? */ 10321da177e4SLinus Torvalds fs->write_prot = -1; 10331da177e4SLinus Torvalds fs->cur_cyl = -1; 10341da177e4SLinus Torvalds mdelay(1); 10351da177e4SLinus Torvalds for (n = HZ; n > 0; --n) { 10361da177e4SLinus Torvalds if (swim3_readbit(fs, SEEK_COMPLETE)) 10371da177e4SLinus Torvalds break; 10381da177e4SLinus Torvalds if (signal_pending(current)) 10391da177e4SLinus Torvalds break; 10401da177e4SLinus Torvalds swim3_select(fs, RELAX); 104186e84862SNishanth Aravamudan schedule_timeout_interruptible(1); 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds ret = swim3_readbit(fs, SEEK_COMPLETE) == 0 10441da177e4SLinus Torvalds || swim3_readbit(fs, DISK_IN) == 0; 10451da177e4SLinus Torvalds if (ret) 10461da177e4SLinus Torvalds swim3_action(fs, MOTOR_OFF); 10471da177e4SLinus Torvalds else { 10481da177e4SLinus Torvalds fs->ejected = 0; 10491da177e4SLinus Torvalds swim3_action(fs, SETMFM); 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds swim3_select(fs, RELAX); 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds release_drive(fs); 10541da177e4SLinus Torvalds return ret; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 105783d5cde4SAlexey Dobriyan static const struct block_device_operations floppy_fops = { 10586e9624b8SArnd Bergmann .open = floppy_unlocked_open, 1059b4d9a442SAl Viro .release = floppy_release, 10608a6cfeb6SArnd Bergmann .ioctl = floppy_ioctl, 10614bbde777STejun Heo .check_events = floppy_check_events, 10621da177e4SLinus Torvalds .revalidate_disk= floppy_revalidate, 10631da177e4SLinus Torvalds }; 10641da177e4SLinus Torvalds 10658ccb8cb1SOmar Sandoval static const struct blk_mq_ops swim3_mq_ops = { 10668ccb8cb1SOmar Sandoval .queue_rq = swim3_queue_rq, 10678ccb8cb1SOmar Sandoval }; 10688ccb8cb1SOmar Sandoval 1069b3025457SBenjamin Herrenschmidt static void swim3_mb_event(struct macio_dev* mdev, int mb_state) 1070b3025457SBenjamin Herrenschmidt { 1071b3025457SBenjamin Herrenschmidt struct floppy_state *fs = macio_get_drvdata(mdev); 10727414d4f6SCong Ding struct swim3 __iomem *sw; 1073b3025457SBenjamin Herrenschmidt 1074b3025457SBenjamin Herrenschmidt if (!fs) 1075b3025457SBenjamin Herrenschmidt return; 10767414d4f6SCong Ding 10777414d4f6SCong Ding sw = fs->swim3; 10787414d4f6SCong Ding 1079b3025457SBenjamin Herrenschmidt if (mb_state != MB_FD) 1080b3025457SBenjamin Herrenschmidt return; 1081b3025457SBenjamin Herrenschmidt 1082b3025457SBenjamin Herrenschmidt /* Clear state */ 1083b3025457SBenjamin Herrenschmidt out_8(&sw->intr_enable, 0); 1084b3025457SBenjamin Herrenschmidt in_8(&sw->intr); 1085b3025457SBenjamin Herrenschmidt in_8(&sw->error); 1086b3025457SBenjamin Herrenschmidt } 1087b3025457SBenjamin Herrenschmidt 10883e9a6927SBenjamin Herrenschmidt static int swim3_add_device(struct macio_dev *mdev, int index) 10891da177e4SLinus Torvalds { 109061c7a080SGrant Likely struct device_node *swim = mdev->ofdev.dev.of_node; 10913e9a6927SBenjamin Herrenschmidt struct floppy_state *fs = &floppy_states[index]; 10923e9a6927SBenjamin Herrenschmidt int rc = -EBUSY; 10931da177e4SLinus Torvalds 1094b3025457SBenjamin Herrenschmidt fs->mdev = mdev; 1095b3025457SBenjamin Herrenschmidt fs->index = index; 1096b3025457SBenjamin Herrenschmidt 10973e9a6927SBenjamin Herrenschmidt /* Check & Request resources */ 10983e9a6927SBenjamin Herrenschmidt if (macio_resource_count(mdev) < 2) { 1099b3025457SBenjamin Herrenschmidt swim3_err("%s", "No address in device-tree\n"); 11003e9a6927SBenjamin Herrenschmidt return -ENXIO; 1101cc5d0189SBenjamin Herrenschmidt } 1102b3025457SBenjamin Herrenschmidt if (macio_irq_count(mdev) < 1) { 1103b3025457SBenjamin Herrenschmidt swim3_err("%s", "No interrupt in device-tree\n"); 1104b3025457SBenjamin Herrenschmidt return -ENXIO; 1105cc5d0189SBenjamin Herrenschmidt } 11063e9a6927SBenjamin Herrenschmidt if (macio_request_resource(mdev, 0, "swim3 (mmio)")) { 1107b3025457SBenjamin Herrenschmidt swim3_err("%s", "Can't request mmio resource\n"); 11083e9a6927SBenjamin Herrenschmidt return -EBUSY; 11091da177e4SLinus Torvalds } 11103e9a6927SBenjamin Herrenschmidt if (macio_request_resource(mdev, 1, "swim3 (dma)")) { 1111b3025457SBenjamin Herrenschmidt swim3_err("%s", "Can't request dma resource\n"); 11123e9a6927SBenjamin Herrenschmidt macio_release_resource(mdev, 0); 11133e9a6927SBenjamin Herrenschmidt return -EBUSY; 11143e9a6927SBenjamin Herrenschmidt } 11153e9a6927SBenjamin Herrenschmidt dev_set_drvdata(&mdev->ofdev.dev, fs); 11161da177e4SLinus Torvalds 1117d58b0c39SBenjamin Herrenschmidt if (mdev->media_bay == NULL) 11181da177e4SLinus Torvalds pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1); 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds fs->state = idle; 11213e9a6927SBenjamin Herrenschmidt fs->swim3 = (struct swim3 __iomem *) 11223e9a6927SBenjamin Herrenschmidt ioremap(macio_resource_start(mdev, 0), 0x200); 11233e9a6927SBenjamin Herrenschmidt if (fs->swim3 == NULL) { 1124b3025457SBenjamin Herrenschmidt swim3_err("%s", "Couldn't map mmio registers\n"); 11253e9a6927SBenjamin Herrenschmidt rc = -ENOMEM; 11263e9a6927SBenjamin Herrenschmidt goto out_release; 11273e9a6927SBenjamin Herrenschmidt } 11283e9a6927SBenjamin Herrenschmidt fs->dma = (struct dbdma_regs __iomem *) 11293e9a6927SBenjamin Herrenschmidt ioremap(macio_resource_start(mdev, 1), 0x200); 11303e9a6927SBenjamin Herrenschmidt if (fs->dma == NULL) { 1131b3025457SBenjamin Herrenschmidt swim3_err("%s", "Couldn't map dma registers\n"); 11323e9a6927SBenjamin Herrenschmidt iounmap(fs->swim3); 11333e9a6927SBenjamin Herrenschmidt rc = -ENOMEM; 11343e9a6927SBenjamin Herrenschmidt goto out_release; 11353e9a6927SBenjamin Herrenschmidt } 11363e9a6927SBenjamin Herrenschmidt fs->swim3_intr = macio_irq(mdev, 0); 1137a419aef8SJoe Perches fs->dma_intr = macio_irq(mdev, 1); 11381da177e4SLinus Torvalds fs->cur_cyl = -1; 11391da177e4SLinus Torvalds fs->cur_sector = -1; 11401da177e4SLinus Torvalds fs->secpercyl = 36; 11411da177e4SLinus Torvalds fs->secpertrack = 18; 11421da177e4SLinus Torvalds fs->total_secs = 2880; 11431da177e4SLinus Torvalds init_waitqueue_head(&fs->wait); 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space); 11461da177e4SLinus Torvalds memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd)); 1147f5718726SDavid Gibson fs->dma_cmd[1].command = cpu_to_le16(DBDMA_STOP); 11481da177e4SLinus Torvalds 1149b3025457SBenjamin Herrenschmidt if (mdev->media_bay == NULL || check_media_bay(mdev->media_bay) == MB_FD) 1150b3025457SBenjamin Herrenschmidt swim3_mb_event(mdev, MB_FD); 1151b3025457SBenjamin Herrenschmidt 11521da177e4SLinus Torvalds if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) { 1153b3025457SBenjamin Herrenschmidt swim3_err("%s", "Couldn't request interrupt\n"); 11541da177e4SLinus Torvalds pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0); 11553e9a6927SBenjamin Herrenschmidt goto out_unmap; 11561da177e4SLinus Torvalds } 11571da177e4SLinus Torvalds 1158b5775a6bSKees Cook timer_setup(&fs->timeout, NULL, 0); 11591da177e4SLinus Torvalds 1160b3025457SBenjamin Herrenschmidt swim3_info("SWIM3 floppy controller %s\n", 1161d58b0c39SBenjamin Herrenschmidt mdev->media_bay ? "in media bay" : ""); 11621da177e4SLinus Torvalds 11633e9a6927SBenjamin Herrenschmidt return 0; 11641da177e4SLinus Torvalds 11653e9a6927SBenjamin Herrenschmidt out_unmap: 11663e9a6927SBenjamin Herrenschmidt iounmap(fs->dma); 11673e9a6927SBenjamin Herrenschmidt iounmap(fs->swim3); 11683e9a6927SBenjamin Herrenschmidt 11693e9a6927SBenjamin Herrenschmidt out_release: 11703e9a6927SBenjamin Herrenschmidt macio_release_resource(mdev, 0); 11713e9a6927SBenjamin Herrenschmidt macio_release_resource(mdev, 1); 11723e9a6927SBenjamin Herrenschmidt 11733e9a6927SBenjamin Herrenschmidt return rc; 11743e9a6927SBenjamin Herrenschmidt } 11753e9a6927SBenjamin Herrenschmidt 11768d85fce7SGreg Kroah-Hartman static int swim3_attach(struct macio_dev *mdev, 11778d85fce7SGreg Kroah-Hartman const struct of_device_id *match) 11783e9a6927SBenjamin Herrenschmidt { 1179dbaa54b6SOmar Sandoval struct floppy_state *fs; 11803e9a6927SBenjamin Herrenschmidt struct gendisk *disk; 1181dbaa54b6SOmar Sandoval int rc; 1182b3025457SBenjamin Herrenschmidt 1183dbaa54b6SOmar Sandoval if (floppy_count >= MAX_FLOPPIES) 1184b3025457SBenjamin Herrenschmidt return -ENXIO; 11853e9a6927SBenjamin Herrenschmidt 1186dbaa54b6SOmar Sandoval if (floppy_count == 0) { 1187dbaa54b6SOmar Sandoval rc = register_blkdev(FLOPPY_MAJOR, "fd"); 11883e9a6927SBenjamin Herrenschmidt if (rc) 11893e9a6927SBenjamin Herrenschmidt return rc; 1190dbaa54b6SOmar Sandoval } 1191dbaa54b6SOmar Sandoval 1192dbaa54b6SOmar Sandoval disk = alloc_disk(1); 1193dbaa54b6SOmar Sandoval if (disk == NULL) { 1194dbaa54b6SOmar Sandoval rc = -ENOMEM; 1195dbaa54b6SOmar Sandoval goto out_unregister; 1196dbaa54b6SOmar Sandoval } 11978ccb8cb1SOmar Sandoval 1198427c5ce4SFinn Thain fs = &floppy_states[floppy_count]; 1199427c5ce4SFinn Thain memset(fs, 0, sizeof(*fs)); 1200427c5ce4SFinn Thain 12018ccb8cb1SOmar Sandoval disk->queue = blk_mq_init_sq_queue(&fs->tag_set, &swim3_mq_ops, 2, 12028ccb8cb1SOmar Sandoval BLK_MQ_F_SHOULD_MERGE); 12038ccb8cb1SOmar Sandoval if (IS_ERR(disk->queue)) { 12048ccb8cb1SOmar Sandoval rc = PTR_ERR(disk->queue); 12058ccb8cb1SOmar Sandoval disk->queue = NULL; 1206dbaa54b6SOmar Sandoval goto out_put_disk; 1207b3025457SBenjamin Herrenschmidt } 12088fc45044SChristoph Hellwig blk_queue_bounce_limit(disk->queue, BLK_BOUNCE_HIGH); 1209dbaa54b6SOmar Sandoval disk->queue->queuedata = fs; 12103e9a6927SBenjamin Herrenschmidt 1211dbaa54b6SOmar Sandoval rc = swim3_add_device(mdev, floppy_count); 1212dbaa54b6SOmar Sandoval if (rc) 1213dbaa54b6SOmar Sandoval goto out_cleanup_queue; 12143e9a6927SBenjamin Herrenschmidt 12153e9a6927SBenjamin Herrenschmidt disk->major = FLOPPY_MAJOR; 1216dbaa54b6SOmar Sandoval disk->first_minor = floppy_count; 12173e9a6927SBenjamin Herrenschmidt disk->fops = &floppy_fops; 1218dbaa54b6SOmar Sandoval disk->private_data = fs; 1219*773008f6SMartin Wilck disk->events = DISK_EVENT_MEDIA_CHANGE; 12203e9a6927SBenjamin Herrenschmidt disk->flags |= GENHD_FL_REMOVABLE; 1221dbaa54b6SOmar Sandoval sprintf(disk->disk_name, "fd%d", floppy_count); 12223e9a6927SBenjamin Herrenschmidt set_capacity(disk, 2880); 12233e9a6927SBenjamin Herrenschmidt add_disk(disk); 12243e9a6927SBenjamin Herrenschmidt 1225dbaa54b6SOmar Sandoval disks[floppy_count++] = disk; 12263e9a6927SBenjamin Herrenschmidt return 0; 1227dbaa54b6SOmar Sandoval 1228dbaa54b6SOmar Sandoval out_cleanup_queue: 1229dbaa54b6SOmar Sandoval blk_cleanup_queue(disk->queue); 1230dbaa54b6SOmar Sandoval disk->queue = NULL; 12318ccb8cb1SOmar Sandoval blk_mq_free_tag_set(&fs->tag_set); 1232dbaa54b6SOmar Sandoval out_put_disk: 1233dbaa54b6SOmar Sandoval put_disk(disk); 1234dbaa54b6SOmar Sandoval out_unregister: 1235dbaa54b6SOmar Sandoval if (floppy_count == 0) 1236dbaa54b6SOmar Sandoval unregister_blkdev(FLOPPY_MAJOR, "fd"); 1237dbaa54b6SOmar Sandoval return rc; 12383e9a6927SBenjamin Herrenschmidt } 12393e9a6927SBenjamin Herrenschmidt 1240cc3f2e9fSArvind Yadav static const struct of_device_id swim3_match[] = 12413e9a6927SBenjamin Herrenschmidt { 12423e9a6927SBenjamin Herrenschmidt { 12433e9a6927SBenjamin Herrenschmidt .name = "swim3", 12443e9a6927SBenjamin Herrenschmidt }, 12453e9a6927SBenjamin Herrenschmidt { 12463e9a6927SBenjamin Herrenschmidt .compatible = "ohare-swim3" 12473e9a6927SBenjamin Herrenschmidt }, 12483e9a6927SBenjamin Herrenschmidt { 12493e9a6927SBenjamin Herrenschmidt .compatible = "swim3" 12503e9a6927SBenjamin Herrenschmidt }, 1251f41c53a5SAxel Lin { /* end of list */ } 12523e9a6927SBenjamin Herrenschmidt }; 12533e9a6927SBenjamin Herrenschmidt 12543e9a6927SBenjamin Herrenschmidt static struct macio_driver swim3_driver = 12553e9a6927SBenjamin Herrenschmidt { 1256c2cdf6abSBenjamin Herrenschmidt .driver = { 12573e9a6927SBenjamin Herrenschmidt .name = "swim3", 1258c2cdf6abSBenjamin Herrenschmidt .of_match_table = swim3_match, 1259c2cdf6abSBenjamin Herrenschmidt }, 12603e9a6927SBenjamin Herrenschmidt .probe = swim3_attach, 1261b3025457SBenjamin Herrenschmidt #ifdef CONFIG_PMAC_MEDIABAY 1262b3025457SBenjamin Herrenschmidt .mediabay_event = swim3_mb_event, 1263b3025457SBenjamin Herrenschmidt #endif 12643e9a6927SBenjamin Herrenschmidt #if 0 12653e9a6927SBenjamin Herrenschmidt .suspend = swim3_suspend, 12663e9a6927SBenjamin Herrenschmidt .resume = swim3_resume, 12673e9a6927SBenjamin Herrenschmidt #endif 12683e9a6927SBenjamin Herrenschmidt }; 12693e9a6927SBenjamin Herrenschmidt 12703e9a6927SBenjamin Herrenschmidt 12713e9a6927SBenjamin Herrenschmidt int swim3_init(void) 12723e9a6927SBenjamin Herrenschmidt { 12733e9a6927SBenjamin Herrenschmidt macio_register_driver(&swim3_driver); 12741da177e4SLinus Torvalds return 0; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds module_init(swim3_init) 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 12801da177e4SLinus Torvalds MODULE_AUTHOR("Paul Mackerras"); 12811da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR); 1282