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