Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the SWIM3 (Super Woz Integrated Machine 3) |
| 3 | * floppy controller found on Power Macintoshes. |
| 4 | * |
| 5 | * Copyright (C) 1996 Paul Mackerras. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * TODO: |
| 15 | * handle 2 drives |
| 16 | * handle GCR disks |
| 17 | */ |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/stddef.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/timer.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/fd.h> |
| 25 | #include <linux/ioctl.h> |
| 26 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/module.h> |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 29 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/io.h> |
| 31 | #include <asm/dbdma.h> |
| 32 | #include <asm/prom.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <asm/mediabay.h> |
| 35 | #include <asm/machdep.h> |
| 36 | #include <asm/pmac_feature.h> |
| 37 | |
| 38 | static struct request_queue *swim3_queue; |
| 39 | static struct gendisk *disks[2]; |
| 40 | static struct request *fd_req; |
| 41 | |
| 42 | #define MAX_FLOPPIES 2 |
| 43 | |
| 44 | enum swim_state { |
| 45 | idle, |
| 46 | locating, |
| 47 | seeking, |
| 48 | settling, |
| 49 | do_transfer, |
| 50 | jogging, |
| 51 | available, |
| 52 | revalidating, |
| 53 | ejecting |
| 54 | }; |
| 55 | |
| 56 | #define REG(x) unsigned char x; char x ## _pad[15]; |
| 57 | |
| 58 | /* |
| 59 | * The names for these registers mostly represent speculation on my part. |
| 60 | * It will be interesting to see how close they are to the names Apple uses. |
| 61 | */ |
| 62 | struct swim3 { |
| 63 | REG(data); |
| 64 | REG(timer); /* counts down at 1MHz */ |
| 65 | REG(error); |
| 66 | REG(mode); |
| 67 | REG(select); /* controls CA0, CA1, CA2 and LSTRB signals */ |
| 68 | REG(setup); |
| 69 | REG(control); /* writing bits clears them */ |
| 70 | REG(status); /* writing bits sets them in control */ |
| 71 | REG(intr); |
| 72 | REG(nseek); /* # tracks to seek */ |
| 73 | REG(ctrack); /* current track number */ |
| 74 | REG(csect); /* current sector number */ |
| 75 | REG(gap3); /* size of gap 3 in track format */ |
| 76 | REG(sector); /* sector # to read or write */ |
| 77 | REG(nsect); /* # sectors to read or write */ |
| 78 | REG(intr_enable); |
| 79 | }; |
| 80 | |
| 81 | #define control_bic control |
| 82 | #define control_bis status |
| 83 | |
| 84 | /* Bits in select register */ |
| 85 | #define CA_MASK 7 |
| 86 | #define LSTRB 8 |
| 87 | |
| 88 | /* Bits in control register */ |
| 89 | #define DO_SEEK 0x80 |
| 90 | #define FORMAT 0x40 |
| 91 | #define SELECT 0x20 |
| 92 | #define WRITE_SECTORS 0x10 |
| 93 | #define DO_ACTION 0x08 |
| 94 | #define DRIVE2_ENABLE 0x04 |
| 95 | #define DRIVE_ENABLE 0x02 |
| 96 | #define INTR_ENABLE 0x01 |
| 97 | |
| 98 | /* Bits in status register */ |
| 99 | #define FIFO_1BYTE 0x80 |
| 100 | #define FIFO_2BYTE 0x40 |
| 101 | #define ERROR 0x20 |
| 102 | #define DATA 0x08 |
| 103 | #define RDDATA 0x04 |
| 104 | #define INTR_PENDING 0x02 |
| 105 | #define MARK_BYTE 0x01 |
| 106 | |
| 107 | /* Bits in intr and intr_enable registers */ |
| 108 | #define ERROR_INTR 0x20 |
| 109 | #define DATA_CHANGED 0x10 |
| 110 | #define TRANSFER_DONE 0x08 |
| 111 | #define SEEN_SECTOR 0x04 |
| 112 | #define SEEK_DONE 0x02 |
| 113 | #define TIMER_DONE 0x01 |
| 114 | |
| 115 | /* Bits in error register */ |
| 116 | #define ERR_DATA_CRC 0x80 |
| 117 | #define ERR_ADDR_CRC 0x40 |
| 118 | #define ERR_OVERRUN 0x04 |
| 119 | #define ERR_UNDERRUN 0x01 |
| 120 | |
| 121 | /* Bits in setup register */ |
| 122 | #define S_SW_RESET 0x80 |
| 123 | #define S_GCR_WRITE 0x40 |
| 124 | #define S_IBM_DRIVE 0x20 |
| 125 | #define S_TEST_MODE 0x10 |
| 126 | #define S_FCLK_DIV2 0x08 |
| 127 | #define S_GCR 0x04 |
| 128 | #define S_COPY_PROT 0x02 |
| 129 | #define S_INV_WDATA 0x01 |
| 130 | |
| 131 | /* Select values for swim3_action */ |
| 132 | #define SEEK_POSITIVE 0 |
| 133 | #define SEEK_NEGATIVE 4 |
| 134 | #define STEP 1 |
| 135 | #define MOTOR_ON 2 |
| 136 | #define MOTOR_OFF 6 |
| 137 | #define INDEX 3 |
| 138 | #define EJECT 7 |
| 139 | #define SETMFM 9 |
| 140 | #define SETGCR 13 |
| 141 | |
| 142 | /* Select values for swim3_select and swim3_readbit */ |
| 143 | #define STEP_DIR 0 |
| 144 | #define STEPPING 1 |
| 145 | #define MOTOR_ON 2 |
| 146 | #define RELAX 3 /* also eject in progress */ |
| 147 | #define READ_DATA_0 4 |
| 148 | #define TWOMEG_DRIVE 5 |
| 149 | #define SINGLE_SIDED 6 /* drive or diskette is 4MB type? */ |
| 150 | #define DRIVE_PRESENT 7 |
| 151 | #define DISK_IN 8 |
| 152 | #define WRITE_PROT 9 |
| 153 | #define TRACK_ZERO 10 |
| 154 | #define TACHO 11 |
| 155 | #define READ_DATA_1 12 |
| 156 | #define MFM_MODE 13 |
| 157 | #define SEEK_COMPLETE 14 |
| 158 | #define ONEMEG_MEDIA 15 |
| 159 | |
| 160 | /* Definitions of values used in writing and formatting */ |
| 161 | #define DATA_ESCAPE 0x99 |
| 162 | #define GCR_SYNC_EXC 0x3f |
| 163 | #define GCR_SYNC_CONV 0x80 |
| 164 | #define GCR_FIRST_MARK 0xd5 |
| 165 | #define GCR_SECOND_MARK 0xaa |
| 166 | #define GCR_ADDR_MARK "\xd5\xaa\x00" |
| 167 | #define GCR_DATA_MARK "\xd5\xaa\x0b" |
| 168 | #define GCR_SLIP_BYTE "\x27\xaa" |
| 169 | #define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f" |
| 170 | |
| 171 | #define DATA_99 "\x99\x99" |
| 172 | #define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe" |
| 173 | #define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc" |
| 174 | #define MFM_GAP_LEN 12 |
| 175 | |
| 176 | struct floppy_state { |
| 177 | enum swim_state state; |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 178 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | struct swim3 __iomem *swim3; /* hardware registers */ |
| 180 | struct dbdma_regs __iomem *dma; /* DMA controller registers */ |
| 181 | int swim3_intr; /* interrupt number for SWIM3 */ |
| 182 | int dma_intr; /* interrupt number for DMA channel */ |
| 183 | int cur_cyl; /* cylinder head is on, or -1 */ |
| 184 | int cur_sector; /* last sector we saw go past */ |
| 185 | int req_cyl; /* the cylinder for the current r/w request */ |
| 186 | int head; /* head number ditto */ |
| 187 | int req_sector; /* sector number ditto */ |
| 188 | int scount; /* # sectors we're transferring at present */ |
| 189 | int retries; |
| 190 | int settle_time; |
| 191 | int secpercyl; /* disk geometry information */ |
| 192 | int secpertrack; |
| 193 | int total_secs; |
| 194 | int write_prot; /* 1 if write-protected, 0 if not, -1 dunno */ |
| 195 | struct dbdma_cmd *dma_cmd; |
| 196 | int ref_count; |
| 197 | int expect_cyl; |
| 198 | struct timer_list timeout; |
| 199 | int timeout_pending; |
| 200 | int ejected; |
| 201 | wait_queue_head_t wait; |
| 202 | int wanted; |
| 203 | struct device_node* media_bay; /* NULL when not in bay */ |
| 204 | char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)]; |
| 205 | }; |
| 206 | |
| 207 | static struct floppy_state floppy_states[MAX_FLOPPIES]; |
| 208 | static int floppy_count = 0; |
| 209 | static DEFINE_SPINLOCK(swim3_lock); |
| 210 | |
| 211 | static unsigned short write_preamble[] = { |
| 212 | 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */ |
| 213 | 0, 0, 0, 0, 0, 0, /* sync field */ |
| 214 | 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */ |
| 215 | 0x990f /* no escape for 512 bytes */ |
| 216 | }; |
| 217 | |
| 218 | static unsigned short write_postamble[] = { |
| 219 | 0x9904, /* insert CRC */ |
| 220 | 0x4e4e, 0x4e4e, |
| 221 | 0x9908, /* stop writing */ |
| 222 | 0, 0, 0, 0, 0, 0 |
| 223 | }; |
| 224 | |
| 225 | static void swim3_select(struct floppy_state *fs, int sel); |
| 226 | static void swim3_action(struct floppy_state *fs, int action); |
| 227 | static int swim3_readbit(struct floppy_state *fs, int bit); |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 228 | static void do_fd_request(struct request_queue * q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | static void start_request(struct floppy_state *fs); |
| 230 | static void set_timeout(struct floppy_state *fs, int nticks, |
| 231 | void (*proc)(unsigned long)); |
| 232 | static void scan_track(struct floppy_state *fs); |
| 233 | static void seek_track(struct floppy_state *fs, int n); |
| 234 | static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count); |
| 235 | static void setup_transfer(struct floppy_state *fs); |
| 236 | static void act(struct floppy_state *fs); |
| 237 | static void scan_timeout(unsigned long data); |
| 238 | static void seek_timeout(unsigned long data); |
| 239 | static void settle_timeout(unsigned long data); |
| 240 | static void xfer_timeout(unsigned long data); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 241 | static irqreturn_t swim3_interrupt(int irq, void *dev_id); |
| 242 | /*static void fd_dma_interrupt(int irq, void *dev_id);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | static int grab_drive(struct floppy_state *fs, enum swim_state state, |
| 244 | int interruptible); |
| 245 | static void release_drive(struct floppy_state *fs); |
| 246 | static int fd_eject(struct floppy_state *fs); |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 247 | static int floppy_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | unsigned int cmd, unsigned long param); |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 249 | static int floppy_open(struct block_device *bdev, fmode_t mode); |
| 250 | static int floppy_release(struct gendisk *disk, fmode_t mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | static int floppy_check_change(struct gendisk *disk); |
| 252 | static int floppy_revalidate(struct gendisk *disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 254 | static bool swim3_end_request(int err, unsigned int nr_bytes) |
| 255 | { |
| 256 | if (__blk_end_request(fd_req, err, nr_bytes)) |
| 257 | return true; |
| 258 | |
| 259 | fd_req = NULL; |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | static bool swim3_end_request_cur(int err) |
| 264 | { |
| 265 | return swim3_end_request(err, blk_rq_cur_bytes(fd_req)); |
| 266 | } |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | static void swim3_select(struct floppy_state *fs, int sel) |
| 269 | { |
| 270 | struct swim3 __iomem *sw = fs->swim3; |
| 271 | |
| 272 | out_8(&sw->select, RELAX); |
| 273 | if (sel & 8) |
| 274 | out_8(&sw->control_bis, SELECT); |
| 275 | else |
| 276 | out_8(&sw->control_bic, SELECT); |
| 277 | out_8(&sw->select, sel & CA_MASK); |
| 278 | } |
| 279 | |
| 280 | static void swim3_action(struct floppy_state *fs, int action) |
| 281 | { |
| 282 | struct swim3 __iomem *sw = fs->swim3; |
| 283 | |
| 284 | swim3_select(fs, action); |
| 285 | udelay(1); |
| 286 | out_8(&sw->select, sw->select | LSTRB); |
| 287 | udelay(2); |
| 288 | out_8(&sw->select, sw->select & ~LSTRB); |
| 289 | udelay(1); |
| 290 | } |
| 291 | |
| 292 | static int swim3_readbit(struct floppy_state *fs, int bit) |
| 293 | { |
| 294 | struct swim3 __iomem *sw = fs->swim3; |
| 295 | int stat; |
| 296 | |
| 297 | swim3_select(fs, bit); |
| 298 | udelay(1); |
| 299 | stat = in_8(&sw->status); |
| 300 | return (stat & DATA) == 0; |
| 301 | } |
| 302 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 303 | static void do_fd_request(struct request_queue * q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
| 305 | int i; |
| 306 | for(i=0;i<floppy_count;i++) |
| 307 | { |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 308 | #ifdef CONFIG_PMAC_MEDIABAY |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (floppy_states[i].media_bay && |
| 310 | check_media_bay(floppy_states[i].media_bay, MB_FD)) |
| 311 | continue; |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 312 | #endif /* CONFIG_PMAC_MEDIABAY */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | start_request(&floppy_states[i]); |
| 314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void start_request(struct floppy_state *fs) |
| 318 | { |
| 319 | struct request *req; |
| 320 | unsigned long x; |
| 321 | |
| 322 | if (fs->state == idle && fs->wanted) { |
| 323 | fs->state = available; |
| 324 | wake_up(&fs->wait); |
| 325 | return; |
| 326 | } |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 327 | while (fs->state == idle) { |
| 328 | if (!fd_req) { |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame^] | 329 | fd_req = blk_fetch_request(swim3_queue); |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 330 | if (!fd_req) |
| 331 | break; |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 332 | } |
| 333 | req = fd_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | #if 0 |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 335 | printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%u buf=%p\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | req->rq_disk->disk_name, req->cmd, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 337 | (long)blk_rq_pos(req), blk_rq_sectors(req), req->buffer); |
| 338 | printk(" errors=%d current_nr_sectors=%u\n", |
| 339 | req->errors, blk_rq_cur_sectors(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | #endif |
| 341 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 342 | if (blk_rq_pos(req) >= fs->total_secs) { |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 343 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | continue; |
| 345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | if (fs->ejected) { |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 347 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | continue; |
| 349 | } |
| 350 | |
| 351 | if (rq_data_dir(req) == WRITE) { |
| 352 | if (fs->write_prot < 0) |
| 353 | fs->write_prot = swim3_readbit(fs, WRITE_PROT); |
| 354 | if (fs->write_prot) { |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 355 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | continue; |
| 357 | } |
| 358 | } |
| 359 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 360 | /* Do not remove the cast. blk_rq_pos(req) is now a |
| 361 | * sector_t and can be 64 bits, but it will never go |
| 362 | * past 32 bits for this driver anyway, so we can |
| 363 | * safely cast it down and not have to do a 64/32 |
| 364 | * division |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 366 | fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl; |
| 367 | x = ((long)blk_rq_pos(req)) % fs->secpercyl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | fs->head = x / fs->secpertrack; |
| 369 | fs->req_sector = x % fs->secpertrack + 1; |
| 370 | fd_req = req; |
| 371 | fs->state = do_transfer; |
| 372 | fs->retries = 0; |
| 373 | |
| 374 | act(fs); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | static void set_timeout(struct floppy_state *fs, int nticks, |
| 379 | void (*proc)(unsigned long)) |
| 380 | { |
| 381 | unsigned long flags; |
| 382 | |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 383 | spin_lock_irqsave(&fs->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (fs->timeout_pending) |
| 385 | del_timer(&fs->timeout); |
| 386 | fs->timeout.expires = jiffies + nticks; |
| 387 | fs->timeout.function = proc; |
| 388 | fs->timeout.data = (unsigned long) fs; |
| 389 | add_timer(&fs->timeout); |
| 390 | fs->timeout_pending = 1; |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 391 | spin_unlock_irqrestore(&fs->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static inline void scan_track(struct floppy_state *fs) |
| 395 | { |
| 396 | struct swim3 __iomem *sw = fs->swim3; |
| 397 | |
| 398 | swim3_select(fs, READ_DATA_0); |
| 399 | in_8(&sw->intr); /* clear SEEN_SECTOR bit */ |
| 400 | in_8(&sw->error); |
| 401 | out_8(&sw->intr_enable, SEEN_SECTOR); |
| 402 | out_8(&sw->control_bis, DO_ACTION); |
| 403 | /* enable intr when track found */ |
| 404 | set_timeout(fs, HZ, scan_timeout); /* enable timeout */ |
| 405 | } |
| 406 | |
| 407 | static inline void seek_track(struct floppy_state *fs, int n) |
| 408 | { |
| 409 | struct swim3 __iomem *sw = fs->swim3; |
| 410 | |
| 411 | if (n >= 0) { |
| 412 | swim3_action(fs, SEEK_POSITIVE); |
| 413 | sw->nseek = n; |
| 414 | } else { |
| 415 | swim3_action(fs, SEEK_NEGATIVE); |
| 416 | sw->nseek = -n; |
| 417 | } |
| 418 | fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1; |
| 419 | swim3_select(fs, STEP); |
| 420 | in_8(&sw->error); |
| 421 | /* enable intr when seek finished */ |
| 422 | out_8(&sw->intr_enable, SEEK_DONE); |
| 423 | out_8(&sw->control_bis, DO_SEEK); |
| 424 | set_timeout(fs, 3*HZ, seek_timeout); /* enable timeout */ |
| 425 | fs->settle_time = 0; |
| 426 | } |
| 427 | |
| 428 | static inline void init_dma(struct dbdma_cmd *cp, int cmd, |
| 429 | void *buf, int count) |
| 430 | { |
| 431 | st_le16(&cp->req_count, count); |
| 432 | st_le16(&cp->command, cmd); |
| 433 | st_le32(&cp->phy_addr, virt_to_bus(buf)); |
| 434 | cp->xfer_status = 0; |
| 435 | } |
| 436 | |
| 437 | static inline void setup_transfer(struct floppy_state *fs) |
| 438 | { |
| 439 | int n; |
| 440 | struct swim3 __iomem *sw = fs->swim3; |
| 441 | struct dbdma_cmd *cp = fs->dma_cmd; |
| 442 | struct dbdma_regs __iomem *dr = fs->dma; |
| 443 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 444 | if (blk_rq_cur_sectors(fd_req) <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | printk(KERN_ERR "swim3: transfer 0 sectors?\n"); |
| 446 | return; |
| 447 | } |
| 448 | if (rq_data_dir(fd_req) == WRITE) |
| 449 | n = 1; |
| 450 | else { |
| 451 | n = fs->secpertrack - fs->req_sector + 1; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 452 | if (n > blk_rq_cur_sectors(fd_req)) |
| 453 | n = blk_rq_cur_sectors(fd_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | fs->scount = n; |
| 456 | swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0); |
| 457 | out_8(&sw->sector, fs->req_sector); |
| 458 | out_8(&sw->nsect, n); |
| 459 | out_8(&sw->gap3, 0); |
| 460 | out_le32(&dr->cmdptr, virt_to_bus(cp)); |
| 461 | if (rq_data_dir(fd_req) == WRITE) { |
| 462 | /* Set up 3 dma commands: write preamble, data, postamble */ |
| 463 | init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble)); |
| 464 | ++cp; |
| 465 | init_dma(cp, OUTPUT_MORE, fd_req->buffer, 512); |
| 466 | ++cp; |
| 467 | init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble)); |
| 468 | } else { |
| 469 | init_dma(cp, INPUT_LAST, fd_req->buffer, n * 512); |
| 470 | } |
| 471 | ++cp; |
| 472 | out_le16(&cp->command, DBDMA_STOP); |
| 473 | out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); |
| 474 | in_8(&sw->error); |
| 475 | out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); |
| 476 | if (rq_data_dir(fd_req) == WRITE) |
| 477 | out_8(&sw->control_bis, WRITE_SECTORS); |
| 478 | in_8(&sw->intr); |
| 479 | out_le32(&dr->control, (RUN << 16) | RUN); |
| 480 | /* enable intr when transfer complete */ |
| 481 | out_8(&sw->intr_enable, TRANSFER_DONE); |
| 482 | out_8(&sw->control_bis, DO_ACTION); |
| 483 | set_timeout(fs, 2*HZ, xfer_timeout); /* enable timeout */ |
| 484 | } |
| 485 | |
| 486 | static void act(struct floppy_state *fs) |
| 487 | { |
| 488 | for (;;) { |
| 489 | switch (fs->state) { |
| 490 | case idle: |
| 491 | return; /* XXX shouldn't get here */ |
| 492 | |
| 493 | case locating: |
| 494 | if (swim3_readbit(fs, TRACK_ZERO)) { |
| 495 | fs->cur_cyl = 0; |
| 496 | if (fs->req_cyl == 0) |
| 497 | fs->state = do_transfer; |
| 498 | else |
| 499 | fs->state = seeking; |
| 500 | break; |
| 501 | } |
| 502 | scan_track(fs); |
| 503 | return; |
| 504 | |
| 505 | case seeking: |
| 506 | if (fs->cur_cyl < 0) { |
| 507 | fs->expect_cyl = -1; |
| 508 | fs->state = locating; |
| 509 | break; |
| 510 | } |
| 511 | if (fs->req_cyl == fs->cur_cyl) { |
| 512 | printk("whoops, seeking 0\n"); |
| 513 | fs->state = do_transfer; |
| 514 | break; |
| 515 | } |
| 516 | seek_track(fs, fs->req_cyl - fs->cur_cyl); |
| 517 | return; |
| 518 | |
| 519 | case settling: |
| 520 | /* check for SEEK_COMPLETE after 30ms */ |
| 521 | fs->settle_time = (HZ + 32) / 33; |
| 522 | set_timeout(fs, fs->settle_time, settle_timeout); |
| 523 | return; |
| 524 | |
| 525 | case do_transfer: |
| 526 | if (fs->cur_cyl != fs->req_cyl) { |
| 527 | if (fs->retries > 5) { |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 528 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | fs->state = idle; |
| 530 | return; |
| 531 | } |
| 532 | fs->state = seeking; |
| 533 | break; |
| 534 | } |
| 535 | setup_transfer(fs); |
| 536 | return; |
| 537 | |
| 538 | case jogging: |
| 539 | seek_track(fs, -5); |
| 540 | return; |
| 541 | |
| 542 | default: |
| 543 | printk(KERN_ERR"swim3: unknown state %d\n", fs->state); |
| 544 | return; |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static void scan_timeout(unsigned long data) |
| 550 | { |
| 551 | struct floppy_state *fs = (struct floppy_state *) data; |
| 552 | struct swim3 __iomem *sw = fs->swim3; |
| 553 | |
| 554 | fs->timeout_pending = 0; |
| 555 | out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); |
| 556 | out_8(&sw->select, RELAX); |
| 557 | out_8(&sw->intr_enable, 0); |
| 558 | fs->cur_cyl = -1; |
| 559 | if (fs->retries > 5) { |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 560 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | fs->state = idle; |
| 562 | start_request(fs); |
| 563 | } else { |
| 564 | fs->state = jogging; |
| 565 | act(fs); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | static void seek_timeout(unsigned long data) |
| 570 | { |
| 571 | struct floppy_state *fs = (struct floppy_state *) data; |
| 572 | struct swim3 __iomem *sw = fs->swim3; |
| 573 | |
| 574 | fs->timeout_pending = 0; |
| 575 | out_8(&sw->control_bic, DO_SEEK); |
| 576 | out_8(&sw->select, RELAX); |
| 577 | out_8(&sw->intr_enable, 0); |
| 578 | printk(KERN_ERR "swim3: seek timeout\n"); |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 579 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | fs->state = idle; |
| 581 | start_request(fs); |
| 582 | } |
| 583 | |
| 584 | static void settle_timeout(unsigned long data) |
| 585 | { |
| 586 | struct floppy_state *fs = (struct floppy_state *) data; |
| 587 | struct swim3 __iomem *sw = fs->swim3; |
| 588 | |
| 589 | fs->timeout_pending = 0; |
| 590 | if (swim3_readbit(fs, SEEK_COMPLETE)) { |
| 591 | out_8(&sw->select, RELAX); |
| 592 | fs->state = locating; |
| 593 | act(fs); |
| 594 | return; |
| 595 | } |
| 596 | out_8(&sw->select, RELAX); |
| 597 | if (fs->settle_time < 2*HZ) { |
| 598 | ++fs->settle_time; |
| 599 | set_timeout(fs, 1, settle_timeout); |
| 600 | return; |
| 601 | } |
| 602 | printk(KERN_ERR "swim3: seek settle timeout\n"); |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 603 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | fs->state = idle; |
| 605 | start_request(fs); |
| 606 | } |
| 607 | |
| 608 | static void xfer_timeout(unsigned long data) |
| 609 | { |
| 610 | struct floppy_state *fs = (struct floppy_state *) data; |
| 611 | struct swim3 __iomem *sw = fs->swim3; |
| 612 | struct dbdma_regs __iomem *dr = fs->dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | int n; |
| 614 | |
| 615 | fs->timeout_pending = 0; |
| 616 | out_le32(&dr->control, RUN << 16); |
| 617 | /* We must wait a bit for dbdma to stop */ |
| 618 | for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++) |
| 619 | udelay(1); |
| 620 | out_8(&sw->intr_enable, 0); |
| 621 | out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); |
| 622 | out_8(&sw->select, RELAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | printk(KERN_ERR "swim3: timeout %sing sector %ld\n", |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 624 | (rq_data_dir(fd_req)==WRITE? "writ": "read"), |
| 625 | (long)blk_rq_pos(fd_req)); |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 626 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | fs->state = idle; |
| 628 | start_request(fs); |
| 629 | } |
| 630 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 631 | static irqreturn_t swim3_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
| 633 | struct floppy_state *fs = (struct floppy_state *) dev_id; |
| 634 | struct swim3 __iomem *sw = fs->swim3; |
| 635 | int intr, err, n; |
| 636 | int stat, resid; |
| 637 | struct dbdma_regs __iomem *dr; |
| 638 | struct dbdma_cmd *cp; |
| 639 | |
| 640 | intr = in_8(&sw->intr); |
| 641 | err = (intr & ERROR_INTR)? in_8(&sw->error): 0; |
| 642 | if ((intr & ERROR_INTR) && fs->state != do_transfer) |
Olaf Hering | 14b1ffb | 2006-10-02 22:57:23 +0200 | [diff] [blame] | 643 | printk(KERN_ERR "swim3_interrupt, state=%d, dir=%x, intr=%x, err=%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | fs->state, rq_data_dir(fd_req), intr, err); |
| 645 | switch (fs->state) { |
| 646 | case locating: |
| 647 | if (intr & SEEN_SECTOR) { |
| 648 | out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); |
| 649 | out_8(&sw->select, RELAX); |
| 650 | out_8(&sw->intr_enable, 0); |
| 651 | del_timer(&fs->timeout); |
| 652 | fs->timeout_pending = 0; |
| 653 | if (sw->ctrack == 0xff) { |
| 654 | printk(KERN_ERR "swim3: seen sector but cyl=ff?\n"); |
| 655 | fs->cur_cyl = -1; |
| 656 | if (fs->retries > 5) { |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 657 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | fs->state = idle; |
| 659 | start_request(fs); |
| 660 | } else { |
| 661 | fs->state = jogging; |
| 662 | act(fs); |
| 663 | } |
| 664 | break; |
| 665 | } |
| 666 | fs->cur_cyl = sw->ctrack; |
| 667 | fs->cur_sector = sw->csect; |
| 668 | if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl) |
| 669 | printk(KERN_ERR "swim3: expected cyl %d, got %d\n", |
| 670 | fs->expect_cyl, fs->cur_cyl); |
| 671 | fs->state = do_transfer; |
| 672 | act(fs); |
| 673 | } |
| 674 | break; |
| 675 | case seeking: |
| 676 | case jogging: |
| 677 | if (sw->nseek == 0) { |
| 678 | out_8(&sw->control_bic, DO_SEEK); |
| 679 | out_8(&sw->select, RELAX); |
| 680 | out_8(&sw->intr_enable, 0); |
| 681 | del_timer(&fs->timeout); |
| 682 | fs->timeout_pending = 0; |
| 683 | if (fs->state == seeking) |
| 684 | ++fs->retries; |
| 685 | fs->state = settling; |
| 686 | act(fs); |
| 687 | } |
| 688 | break; |
| 689 | case settling: |
| 690 | out_8(&sw->intr_enable, 0); |
| 691 | del_timer(&fs->timeout); |
| 692 | fs->timeout_pending = 0; |
| 693 | act(fs); |
| 694 | break; |
| 695 | case do_transfer: |
| 696 | if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0) |
| 697 | break; |
| 698 | out_8(&sw->intr_enable, 0); |
| 699 | out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); |
| 700 | out_8(&sw->select, RELAX); |
| 701 | del_timer(&fs->timeout); |
| 702 | fs->timeout_pending = 0; |
| 703 | dr = fs->dma; |
| 704 | cp = fs->dma_cmd; |
| 705 | if (rq_data_dir(fd_req) == WRITE) |
| 706 | ++cp; |
| 707 | /* |
| 708 | * Check that the main data transfer has finished. |
| 709 | * On writing, the swim3 sometimes doesn't use |
| 710 | * up all the bytes of the postamble, so we can still |
| 711 | * see DMA active here. That doesn't matter as long |
| 712 | * as all the sector data has been transferred. |
| 713 | */ |
| 714 | if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) { |
| 715 | /* wait a little while for DMA to complete */ |
| 716 | for (n = 0; n < 100; ++n) { |
| 717 | if (cp->xfer_status != 0) |
| 718 | break; |
| 719 | udelay(1); |
| 720 | barrier(); |
| 721 | } |
| 722 | } |
| 723 | /* turn off DMA */ |
| 724 | out_le32(&dr->control, (RUN | PAUSE) << 16); |
| 725 | stat = ld_le16(&cp->xfer_status); |
| 726 | resid = ld_le16(&cp->res_count); |
| 727 | if (intr & ERROR_INTR) { |
| 728 | n = fs->scount - 1 - resid / 512; |
| 729 | if (n > 0) { |
Tejun Heo | 467ca75 | 2009-04-28 13:06:12 +0900 | [diff] [blame] | 730 | blk_update_request(fd_req, 0, n << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | fs->req_sector += n; |
| 732 | } |
| 733 | if (fs->retries < 5) { |
| 734 | ++fs->retries; |
| 735 | act(fs); |
| 736 | } else { |
| 737 | printk("swim3: error %sing block %ld (err=%x)\n", |
| 738 | rq_data_dir(fd_req) == WRITE? "writ": "read", |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 739 | (long)blk_rq_pos(fd_req), err); |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 740 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | fs->state = idle; |
| 742 | } |
| 743 | } else { |
| 744 | if ((stat & ACTIVE) == 0 || resid != 0) { |
| 745 | /* musta been an error */ |
| 746 | printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid); |
Olaf Hering | 14b1ffb | 2006-10-02 22:57:23 +0200 | [diff] [blame] | 747 | printk(KERN_ERR " state=%d, dir=%x, intr=%x, err=%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | fs->state, rq_data_dir(fd_req), intr, err); |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 749 | swim3_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | fs->state = idle; |
| 751 | start_request(fs); |
| 752 | break; |
| 753 | } |
Tejun Heo | f4bd4b9 | 2009-05-08 11:54:04 +0900 | [diff] [blame] | 754 | if (swim3_end_request(0, fs->scount << 9)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | fs->req_sector += fs->scount; |
| 756 | if (fs->req_sector > fs->secpertrack) { |
| 757 | fs->req_sector -= fs->secpertrack; |
| 758 | if (++fs->head > 1) { |
| 759 | fs->head = 0; |
| 760 | ++fs->req_cyl; |
| 761 | } |
| 762 | } |
| 763 | act(fs); |
Tejun Heo | 467ca75 | 2009-04-28 13:06:12 +0900 | [diff] [blame] | 764 | } else |
| 765 | fs->state = idle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | if (fs->state == idle) |
| 768 | start_request(fs); |
| 769 | break; |
| 770 | default: |
| 771 | printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state); |
| 772 | } |
| 773 | return IRQ_HANDLED; |
| 774 | } |
| 775 | |
| 776 | /* |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 777 | static void fd_dma_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
| 779 | } |
| 780 | */ |
| 781 | |
| 782 | static int grab_drive(struct floppy_state *fs, enum swim_state state, |
| 783 | int interruptible) |
| 784 | { |
| 785 | unsigned long flags; |
| 786 | |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 787 | spin_lock_irqsave(&fs->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if (fs->state != idle) { |
| 789 | ++fs->wanted; |
| 790 | while (fs->state != available) { |
| 791 | if (interruptible && signal_pending(current)) { |
| 792 | --fs->wanted; |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 793 | spin_unlock_irqrestore(&fs->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | return -EINTR; |
| 795 | } |
| 796 | interruptible_sleep_on(&fs->wait); |
| 797 | } |
| 798 | --fs->wanted; |
| 799 | } |
| 800 | fs->state = state; |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 801 | spin_unlock_irqrestore(&fs->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static void release_drive(struct floppy_state *fs) |
| 806 | { |
| 807 | unsigned long flags; |
| 808 | |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 809 | spin_lock_irqsave(&fs->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | fs->state = idle; |
| 811 | start_request(fs); |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 812 | spin_unlock_irqrestore(&fs->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | static int fd_eject(struct floppy_state *fs) |
| 816 | { |
| 817 | int err, n; |
| 818 | |
| 819 | err = grab_drive(fs, ejecting, 1); |
| 820 | if (err) |
| 821 | return err; |
| 822 | swim3_action(fs, EJECT); |
| 823 | for (n = 20; n > 0; --n) { |
| 824 | if (signal_pending(current)) { |
| 825 | err = -EINTR; |
| 826 | break; |
| 827 | } |
| 828 | swim3_select(fs, RELAX); |
Nishanth Aravamudan | 86e8486 | 2005-09-10 00:27:28 -0700 | [diff] [blame] | 829 | schedule_timeout_interruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | if (swim3_readbit(fs, DISK_IN) == 0) |
| 831 | break; |
| 832 | } |
| 833 | swim3_select(fs, RELAX); |
| 834 | udelay(150); |
| 835 | fs->ejected = 1; |
| 836 | release_drive(fs); |
| 837 | return err; |
| 838 | } |
| 839 | |
| 840 | static struct floppy_struct floppy_type = |
| 841 | { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */ |
| 842 | |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 843 | static int floppy_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | unsigned int cmd, unsigned long param) |
| 845 | { |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 846 | struct floppy_state *fs = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | int err; |
| 848 | |
| 849 | if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)) |
| 850 | return -EPERM; |
| 851 | |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 852 | #ifdef CONFIG_PMAC_MEDIABAY |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD)) |
| 854 | return -ENXIO; |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 855 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
| 857 | switch (cmd) { |
| 858 | case FDEJECT: |
| 859 | if (fs->ref_count != 1) |
| 860 | return -EBUSY; |
| 861 | err = fd_eject(fs); |
| 862 | return err; |
| 863 | case FDGETPRM: |
| 864 | if (copy_to_user((void __user *) param, &floppy_type, |
| 865 | sizeof(struct floppy_struct))) |
| 866 | return -EFAULT; |
| 867 | return 0; |
| 868 | } |
| 869 | return -ENOTTY; |
| 870 | } |
| 871 | |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 872 | static int floppy_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | { |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 874 | struct floppy_state *fs = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | struct swim3 __iomem *sw = fs->swim3; |
| 876 | int n, err = 0; |
| 877 | |
| 878 | if (fs->ref_count == 0) { |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 879 | #ifdef CONFIG_PMAC_MEDIABAY |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD)) |
| 881 | return -ENXIO; |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 882 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2); |
| 884 | out_8(&sw->control_bic, 0xff); |
| 885 | out_8(&sw->mode, 0x95); |
| 886 | udelay(10); |
| 887 | out_8(&sw->intr_enable, 0); |
| 888 | out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE); |
| 889 | swim3_action(fs, MOTOR_ON); |
| 890 | fs->write_prot = -1; |
| 891 | fs->cur_cyl = -1; |
| 892 | for (n = 0; n < 2 * HZ; ++n) { |
| 893 | if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE)) |
| 894 | break; |
| 895 | if (signal_pending(current)) { |
| 896 | err = -EINTR; |
| 897 | break; |
| 898 | } |
| 899 | swim3_select(fs, RELAX); |
Nishanth Aravamudan | 86e8486 | 2005-09-10 00:27:28 -0700 | [diff] [blame] | 900 | schedule_timeout_interruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0 |
| 903 | || swim3_readbit(fs, DISK_IN) == 0)) |
| 904 | err = -ENXIO; |
| 905 | swim3_action(fs, SETMFM); |
| 906 | swim3_select(fs, RELAX); |
| 907 | |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 908 | } else if (fs->ref_count == -1 || mode & FMODE_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | return -EBUSY; |
| 910 | |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 911 | if (err == 0 && (mode & FMODE_NDELAY) == 0 |
| 912 | && (mode & (FMODE_READ|FMODE_WRITE))) { |
| 913 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | if (fs->ejected) |
| 915 | err = -ENXIO; |
| 916 | } |
| 917 | |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 918 | if (err == 0 && (mode & FMODE_WRITE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (fs->write_prot < 0) |
| 920 | fs->write_prot = swim3_readbit(fs, WRITE_PROT); |
| 921 | if (fs->write_prot) |
| 922 | err = -EROFS; |
| 923 | } |
| 924 | |
| 925 | if (err) { |
| 926 | if (fs->ref_count == 0) { |
| 927 | swim3_action(fs, MOTOR_OFF); |
| 928 | out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE); |
| 929 | swim3_select(fs, RELAX); |
| 930 | } |
| 931 | return err; |
| 932 | } |
| 933 | |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 934 | if (mode & FMODE_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | fs->ref_count = -1; |
| 936 | else |
| 937 | ++fs->ref_count; |
| 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 942 | static int floppy_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 944 | struct floppy_state *fs = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | struct swim3 __iomem *sw = fs->swim3; |
| 946 | if (fs->ref_count > 0 && --fs->ref_count == 0) { |
| 947 | swim3_action(fs, MOTOR_OFF); |
| 948 | out_8(&sw->control_bic, 0xff); |
| 949 | swim3_select(fs, RELAX); |
| 950 | } |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | static int floppy_check_change(struct gendisk *disk) |
| 955 | { |
| 956 | struct floppy_state *fs = disk->private_data; |
| 957 | return fs->ejected; |
| 958 | } |
| 959 | |
| 960 | static int floppy_revalidate(struct gendisk *disk) |
| 961 | { |
| 962 | struct floppy_state *fs = disk->private_data; |
| 963 | struct swim3 __iomem *sw; |
| 964 | int ret, n; |
| 965 | |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 966 | #ifdef CONFIG_PMAC_MEDIABAY |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD)) |
| 968 | return -ENXIO; |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 969 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | sw = fs->swim3; |
| 972 | grab_drive(fs, revalidating, 0); |
| 973 | out_8(&sw->intr_enable, 0); |
| 974 | out_8(&sw->control_bis, DRIVE_ENABLE); |
| 975 | swim3_action(fs, MOTOR_ON); /* necessary? */ |
| 976 | fs->write_prot = -1; |
| 977 | fs->cur_cyl = -1; |
| 978 | mdelay(1); |
| 979 | for (n = HZ; n > 0; --n) { |
| 980 | if (swim3_readbit(fs, SEEK_COMPLETE)) |
| 981 | break; |
| 982 | if (signal_pending(current)) |
| 983 | break; |
| 984 | swim3_select(fs, RELAX); |
Nishanth Aravamudan | 86e8486 | 2005-09-10 00:27:28 -0700 | [diff] [blame] | 985 | schedule_timeout_interruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | } |
| 987 | ret = swim3_readbit(fs, SEEK_COMPLETE) == 0 |
| 988 | || swim3_readbit(fs, DISK_IN) == 0; |
| 989 | if (ret) |
| 990 | swim3_action(fs, MOTOR_OFF); |
| 991 | else { |
| 992 | fs->ejected = 0; |
| 993 | swim3_action(fs, SETMFM); |
| 994 | } |
| 995 | swim3_select(fs, RELAX); |
| 996 | |
| 997 | release_drive(fs); |
| 998 | return ret; |
| 999 | } |
| 1000 | |
| 1001 | static struct block_device_operations floppy_fops = { |
Al Viro | b4d9a44 | 2008-03-02 10:20:44 -0500 | [diff] [blame] | 1002 | .open = floppy_open, |
| 1003 | .release = floppy_release, |
| 1004 | .locked_ioctl = floppy_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | .media_changed = floppy_check_change, |
| 1006 | .revalidate_disk= floppy_revalidate, |
| 1007 | }; |
| 1008 | |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1009 | static int swim3_add_device(struct macio_dev *mdev, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | { |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1011 | struct device_node *swim = mdev->ofdev.node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | struct device_node *mediabay; |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1013 | struct floppy_state *fs = &floppy_states[index]; |
| 1014 | int rc = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1016 | /* Check & Request resources */ |
| 1017 | if (macio_resource_count(mdev) < 2) { |
| 1018 | printk(KERN_WARNING "ifd%d: no address for %s\n", |
| 1019 | index, swim->full_name); |
| 1020 | return -ENXIO; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1021 | } |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1022 | if (macio_irq_count(mdev) < 2) { |
| 1023 | printk(KERN_WARNING "fd%d: no intrs for device %s\n", |
| 1024 | index, swim->full_name); |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1025 | } |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1026 | if (macio_request_resource(mdev, 0, "swim3 (mmio)")) { |
| 1027 | printk(KERN_ERR "fd%d: can't request mmio resource for %s\n", |
| 1028 | index, swim->full_name); |
| 1029 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1031 | if (macio_request_resource(mdev, 1, "swim3 (dma)")) { |
| 1032 | printk(KERN_ERR "fd%d: can't request dma resource for %s\n", |
| 1033 | index, swim->full_name); |
| 1034 | macio_release_resource(mdev, 0); |
| 1035 | return -EBUSY; |
| 1036 | } |
| 1037 | dev_set_drvdata(&mdev->ofdev.dev, fs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1039 | mediabay = (strcasecmp(swim->parent->type, "media-bay") == 0) ? |
| 1040 | swim->parent : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | if (mediabay == NULL) |
| 1042 | pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1); |
| 1043 | |
| 1044 | memset(fs, 0, sizeof(*fs)); |
Paul Mackerras | 515729e | 2005-11-08 12:15:36 +1100 | [diff] [blame] | 1045 | spin_lock_init(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | fs->state = idle; |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1047 | fs->swim3 = (struct swim3 __iomem *) |
| 1048 | ioremap(macio_resource_start(mdev, 0), 0x200); |
| 1049 | if (fs->swim3 == NULL) { |
| 1050 | printk("fd%d: couldn't map registers for %s\n", |
| 1051 | index, swim->full_name); |
| 1052 | rc = -ENOMEM; |
| 1053 | goto out_release; |
| 1054 | } |
| 1055 | fs->dma = (struct dbdma_regs __iomem *) |
| 1056 | ioremap(macio_resource_start(mdev, 1), 0x200); |
| 1057 | if (fs->dma == NULL) { |
| 1058 | printk("fd%d: couldn't map DMA for %s\n", |
| 1059 | index, swim->full_name); |
| 1060 | iounmap(fs->swim3); |
| 1061 | rc = -ENOMEM; |
| 1062 | goto out_release; |
| 1063 | } |
| 1064 | fs->swim3_intr = macio_irq(mdev, 0); |
| 1065 | fs->dma_intr = macio_irq(mdev, 1);; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | fs->cur_cyl = -1; |
| 1067 | fs->cur_sector = -1; |
| 1068 | fs->secpercyl = 36; |
| 1069 | fs->secpertrack = 18; |
| 1070 | fs->total_secs = 2880; |
| 1071 | fs->media_bay = mediabay; |
| 1072 | init_waitqueue_head(&fs->wait); |
| 1073 | |
| 1074 | fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space); |
| 1075 | memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd)); |
| 1076 | st_le16(&fs->dma_cmd[1].command, DBDMA_STOP); |
| 1077 | |
| 1078 | if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) { |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1079 | printk(KERN_ERR "fd%d: couldn't request irq %d for %s\n", |
| 1080 | index, fs->swim3_intr, swim->full_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0); |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1082 | goto out_unmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | return -EBUSY; |
| 1084 | } |
| 1085 | /* |
| 1086 | if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) { |
| 1087 | printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA", |
| 1088 | fs->dma_intr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | return -EBUSY; |
| 1090 | } |
| 1091 | */ |
| 1092 | |
| 1093 | init_timer(&fs->timeout); |
| 1094 | |
| 1095 | printk(KERN_INFO "fd%d: SWIM3 floppy controller %s\n", floppy_count, |
| 1096 | mediabay ? "in media bay" : ""); |
| 1097 | |
Benjamin Herrenschmidt | 3e9a692 | 2006-07-03 17:25:26 +1000 | [diff] [blame] | 1098 | return 0; |
| 1099 | |
| 1100 | out_unmap: |
| 1101 | iounmap(fs->dma); |
| 1102 | iounmap(fs->swim3); |
| 1103 | |
| 1104 | out_release: |
| 1105 | macio_release_resource(mdev, 0); |
| 1106 | macio_release_resource(mdev, 1); |
| 1107 | |
| 1108 | return rc; |
| 1109 | } |
| 1110 | |
| 1111 | static int __devinit swim3_attach(struct macio_dev *mdev, const struct of_device_id *match) |
| 1112 | { |
| 1113 | int i, rc; |
| 1114 | struct gendisk *disk; |
| 1115 | |
| 1116 | /* Add the drive */ |
| 1117 | rc = swim3_add_device(mdev, floppy_count); |
| 1118 | if (rc) |
| 1119 | return rc; |
| 1120 | |
| 1121 | /* Now create the queue if not there yet */ |
| 1122 | if (swim3_queue == NULL) { |
| 1123 | /* If we failed, there isn't much we can do as the driver is still |
| 1124 | * too dumb to remove the device, just bail out |
| 1125 | */ |
| 1126 | if (register_blkdev(FLOPPY_MAJOR, "fd")) |
| 1127 | return 0; |
| 1128 | swim3_queue = blk_init_queue(do_fd_request, &swim3_lock); |
| 1129 | if (swim3_queue == NULL) { |
| 1130 | unregister_blkdev(FLOPPY_MAJOR, "fd"); |
| 1131 | return 0; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | /* Now register that disk. Same comment about failure handling */ |
| 1136 | i = floppy_count++; |
| 1137 | disk = disks[i] = alloc_disk(1); |
| 1138 | if (disk == NULL) |
| 1139 | return 0; |
| 1140 | |
| 1141 | disk->major = FLOPPY_MAJOR; |
| 1142 | disk->first_minor = i; |
| 1143 | disk->fops = &floppy_fops; |
| 1144 | disk->private_data = &floppy_states[i]; |
| 1145 | disk->queue = swim3_queue; |
| 1146 | disk->flags |= GENHD_FL_REMOVABLE; |
| 1147 | sprintf(disk->disk_name, "fd%d", i); |
| 1148 | set_capacity(disk, 2880); |
| 1149 | add_disk(disk); |
| 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | static struct of_device_id swim3_match[] = |
| 1155 | { |
| 1156 | { |
| 1157 | .name = "swim3", |
| 1158 | }, |
| 1159 | { |
| 1160 | .compatible = "ohare-swim3" |
| 1161 | }, |
| 1162 | { |
| 1163 | .compatible = "swim3" |
| 1164 | }, |
| 1165 | }; |
| 1166 | |
| 1167 | static struct macio_driver swim3_driver = |
| 1168 | { |
| 1169 | .name = "swim3", |
| 1170 | .match_table = swim3_match, |
| 1171 | .probe = swim3_attach, |
| 1172 | #if 0 |
| 1173 | .suspend = swim3_suspend, |
| 1174 | .resume = swim3_resume, |
| 1175 | #endif |
| 1176 | }; |
| 1177 | |
| 1178 | |
| 1179 | int swim3_init(void) |
| 1180 | { |
| 1181 | macio_register_driver(&swim3_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | module_init(swim3_init) |
| 1186 | |
| 1187 | MODULE_LICENSE("GPL"); |
| 1188 | MODULE_AUTHOR("Paul Mackerras"); |
| 1189 | MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR); |