2 * Driver for the SWIM3 (Super Woz Integrated Machine 3)
3 * floppy controller found on Power Macintoshes.
5 * Copyright (C) 1996 Paul Mackerras.
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.
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>
26 #include <linux/ioctl.h>
28 #include <asm/dbdma.h>
30 #include <asm/uaccess.h>
31 #include <asm/mediabay.h>
32 #include <asm/machdep.h>
33 #include <asm/pmac_feature.h>
35 #define MAJOR_NR FLOPPY_MAJOR
36 #include <linux/blk.h>
37 #include <linux/devfs_fs_kernel.h>
39 static int floppy_blocksizes[2] = {512,512};
40 static int floppy_sizes[2] = {1440,1440};
42 #define MAX_FLOPPIES 2
56 #define REG(x) unsigned char x; char x ## _pad[15];
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.
64 REG(timer); /* counts down at 1MHz */
67 REG(select); /* controls CA0, CA1, CA2 and LSTRB signals */
69 REG(control); /* writing bits clears them */
70 REG(status); /* writing bits sets them in control */
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 */
81 #define control_bic control
82 #define control_bis status
84 /* Bits in select register */
88 /* Bits in control register */
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
98 /* Bits in status register */
99 #define FIFO_1BYTE 0x80
100 #define FIFO_2BYTE 0x40
104 #define INTR_PENDING 0x02
105 #define MARK_BYTE 0x01
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
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
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
128 #define S_COPY_PROT 0x02
129 #define S_INV_WDATA 0x01
131 /* Select values for swim3_action */
132 #define SEEK_POSITIVE 0
133 #define SEEK_NEGATIVE 4
142 /* Select values for swim3_select and swim3_readbit */
146 #define RELAX 3 /* also eject in progress */
147 #define READ_DATA_0 4
148 #define TWOMEG_DRIVE 5
149 #define SINGLE_SIDED 6
150 #define DRIVE_PRESENT 7
153 #define TRACK_ZERO 10
155 #define READ_DATA_1 12
157 #define SEEK_COMPLETE 14
158 #define ONEMEG_MEDIA 15
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"
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
176 struct floppy_state {
177 enum swim_state state;
178 volatile struct swim3 *swim3; /* hardware registers */
179 struct dbdma_regs *dma; /* DMA controller registers */
180 int swim3_intr; /* interrupt number for SWIM3 */
181 int dma_intr; /* interrupt number for DMA channel */
182 int cur_cyl; /* cylinder head is on, or -1 */
183 int cur_sector; /* last sector we saw go past */
184 int req_cyl; /* the cylinder for the current r/w request */
185 int head; /* head number ditto */
186 int req_sector; /* sector number ditto */
187 int scount; /* # sectors we're transferring at present */
189 int secpercyl; /* disk geometry information */
192 int write_prot; /* 1 if write-protected, 0 if not, -1 dunno */
193 struct dbdma_cmd *dma_cmd;
196 struct timer_list timeout;
199 wait_queue_head_t wait;
201 struct device_node* media_bay; /* NULL when not in bay */
202 char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
205 static struct floppy_state floppy_states[MAX_FLOPPIES];
206 static int floppy_count = 0;
208 static unsigned short write_preamble[] = {
209 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */
210 0, 0, 0, 0, 0, 0, /* sync field */
211 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */
212 0x990f /* no escape for 512 bytes */
215 static unsigned short write_postamble[] = {
216 0x9904, /* insert CRC */
218 0x9908, /* stop writing */
222 static void swim3_select(struct floppy_state *fs, int sel);
223 static void swim3_action(struct floppy_state *fs, int action);
224 static int swim3_readbit(struct floppy_state *fs, int bit);
225 static void do_fd_request(request_queue_t * q);
226 static void start_request(struct floppy_state *fs);
227 static void set_timeout(struct floppy_state *fs, int nticks,
228 void (*proc)(unsigned long));
229 static void scan_track(struct floppy_state *fs);
230 static void seek_track(struct floppy_state *fs, int n);
231 static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
232 static void setup_transfer(struct floppy_state *fs);
233 static void act(struct floppy_state *fs);
234 static void scan_timeout(unsigned long data);
235 static void seek_timeout(unsigned long data);
236 static void xfer_timeout(unsigned long data);
237 static void swim3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
238 /*static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs);*/
239 static int grab_drive(struct floppy_state *fs, enum swim_state state,
241 static void release_drive(struct floppy_state *fs);
242 static int fd_eject(struct floppy_state *fs);
243 static int floppy_ioctl(struct inode *inode, struct file *filp,
244 unsigned int cmd, unsigned long param);
245 static int floppy_open(struct inode *inode, struct file *filp);
246 static int floppy_release(struct inode *inode, struct file *filp);
247 static int floppy_check_change(kdev_t dev);
248 static int floppy_revalidate(kdev_t dev);
249 static int swim3_add_device(struct device_node *swims);
250 int swim3_init(void);
252 #ifndef CONFIG_PMAC_PBOOK
253 #define check_media_bay(which, what) 1
256 static void swim3_select(struct floppy_state *fs, int sel)
258 volatile struct swim3 *sw = fs->swim3;
260 out_8(&sw->select, RELAX);
262 out_8(&sw->control_bis, SELECT);
264 out_8(&sw->control_bic, SELECT);
265 out_8(&sw->select, sel & CA_MASK);
268 static void swim3_action(struct floppy_state *fs, int action)
270 volatile struct swim3 *sw = fs->swim3;
272 swim3_select(fs, action);
274 out_8(&sw->select, sw->select | LSTRB);
276 out_8(&sw->select, sw->select & ~LSTRB);
278 out_8(&sw->select, RELAX);
281 static int swim3_readbit(struct floppy_state *fs, int bit)
283 volatile struct swim3 *sw = fs->swim3;
286 swim3_select(fs, bit);
288 stat = in_8(&sw->status);
289 out_8(&sw->select, RELAX);
290 return (stat & DATA) == 0;
293 static void do_fd_request(request_queue_t * q)
296 for(i=0;i<floppy_count;i++)
298 if (floppy_states[i].media_bay &&
299 check_media_bay(floppy_states[i].media_bay, MB_FD))
301 start_request(&floppy_states[i]);
306 static void start_request(struct floppy_state *fs)
310 if (fs->state == idle && fs->wanted) {
311 fs->state = available;
315 while (!QUEUE_EMPTY && fs->state == idle) {
316 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
317 panic(DEVICE_NAME ": request list destroyed");
318 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
319 panic(DEVICE_NAME ": block not locked");
321 printk("do_fd_req: dev=%x cmd=%d sec=%ld nr_sec=%ld buf=%p\n",
322 kdev_t_to_nr(CURRENT->rq_dev), CURRENT->cmd,
323 CURRENT->sector, CURRENT->nr_sectors, CURRENT->buffer);
324 printk(" rq_status=%d errors=%d current_nr_sectors=%ld\n",
325 CURRENT->rq_status, CURRENT->errors, CURRENT->current_nr_sectors);
328 if (CURRENT->sector < 0 || CURRENT->sector >= fs->total_secs) {
332 if (CURRENT->current_nr_sectors == 0) {
341 if (CURRENT->cmd == WRITE) {
342 if (fs->write_prot < 0)
343 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
344 if (fs->write_prot) {
350 fs->req_cyl = CURRENT->sector / fs->secpercyl;
351 x = CURRENT->sector % fs->secpercyl;
352 fs->head = x / fs->secpertrack;
353 fs->req_sector = x % fs->secpertrack + 1;
354 fs->state = do_transfer;
361 static void set_timeout(struct floppy_state *fs, int nticks,
362 void (*proc)(unsigned long))
366 save_flags(flags); cli();
367 if (fs->timeout_pending)
368 del_timer(&fs->timeout);
369 fs->timeout.expires = jiffies + nticks;
370 fs->timeout.function = proc;
371 fs->timeout.data = (unsigned long) fs;
372 add_timer(&fs->timeout);
373 fs->timeout_pending = 1;
374 restore_flags(flags);
377 static inline void scan_track(struct floppy_state *fs)
379 volatile struct swim3 *sw = fs->swim3;
382 swim3_select(fs, READ_DATA_0);
383 xx = sw->intr; /* clear SEEN_SECTOR bit */
384 out_8(&sw->control_bis, DO_ACTION);
385 /* enable intr when track found */
386 out_8(&sw->intr_enable, ERROR_INTR | SEEN_SECTOR);
387 set_timeout(fs, HZ, scan_timeout); /* enable timeout */
390 static inline void seek_track(struct floppy_state *fs, int n)
392 volatile struct swim3 *sw = fs->swim3;
395 swim3_action(fs, SEEK_POSITIVE);
398 swim3_action(fs, SEEK_NEGATIVE);
401 fs->expect_cyl = (fs->cur_cyl > 0)? fs->cur_cyl + n: -1;
402 swim3_select(fs, STEP);
403 out_8(&sw->control_bis, DO_SEEK);
404 /* enable intr when seek finished */
405 out_8(&sw->intr_enable, ERROR_INTR | SEEK_DONE);
406 set_timeout(fs, HZ/2, seek_timeout); /* enable timeout */
409 static inline void init_dma(struct dbdma_cmd *cp, int cmd,
410 void *buf, int count)
412 st_le16(&cp->req_count, count);
413 st_le16(&cp->command, cmd);
414 st_le32(&cp->phy_addr, virt_to_bus(buf));
418 static inline void setup_transfer(struct floppy_state *fs)
421 volatile struct swim3 *sw = fs->swim3;
422 struct dbdma_cmd *cp = fs->dma_cmd;
423 struct dbdma_regs *dr = fs->dma;
425 if (CURRENT->current_nr_sectors <= 0) {
426 printk(KERN_ERR "swim3: transfer 0 sectors?\n");
429 if (CURRENT->cmd == WRITE)
432 n = fs->secpertrack - fs->req_sector + 1;
433 if (n > CURRENT->current_nr_sectors)
434 n = CURRENT->current_nr_sectors;
437 swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
438 out_8(&sw->sector, fs->req_sector);
439 out_8(&sw->nsect, n);
441 st_le32(&dr->cmdptr, virt_to_bus(cp));
442 if (CURRENT->cmd == WRITE) {
443 /* Set up 3 dma commands: write preamble, data, postamble */
444 init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
446 init_dma(cp, OUTPUT_MORE, CURRENT->buffer, 512);
448 init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
450 init_dma(cp, INPUT_LAST, CURRENT->buffer, n * 512);
453 out_le16(&cp->command, DBDMA_STOP);
454 out_le32(&dr->control, (RUN << 16) | RUN);
455 out_8(&sw->control_bis,
456 (CURRENT->cmd == WRITE? WRITE_SECTORS: 0) | DO_ACTION);
457 /* enable intr when transfer complete */
458 out_8(&sw->intr_enable, ERROR_INTR | TRANSFER_DONE);
459 set_timeout(fs, 2*HZ, xfer_timeout); /* enable timeout */
462 static void act(struct floppy_state *fs)
464 volatile struct swim3 *sw = fs->swim3;
469 return; /* XXX shouldn't get here */
472 if (swim3_readbit(fs, TRACK_ZERO)) {
474 if (fs->req_cyl == 0)
475 fs->state = do_transfer;
484 if (fs->cur_cyl < 0) {
486 fs->state = locating;
489 if (fs->req_cyl == fs->cur_cyl) {
490 printk("whoops, seeking 0\n");
491 fs->state = do_transfer;
494 seek_track(fs, fs->req_cyl - fs->cur_cyl);
498 /* wait for SEEK_COMPLETE to become true */
499 swim3_select(fs, SEEK_COMPLETE);
501 out_8(&sw->intr_enable, ERROR_INTR | DATA_CHANGED);
502 in_8(&sw->intr); /* clear DATA_CHANGED */
503 if (in_8(&sw->status) & DATA) {
504 /* seek_complete is not yet true */
505 set_timeout(fs, HZ/2, seek_timeout);
508 out_8(&sw->intr_enable, 0);
510 fs->state = locating;
514 if (fs->cur_cyl != fs->req_cyl) {
515 if (fs->retries > 5) {
531 printk(KERN_ERR"swim3: unknown state %d\n", fs->state);
537 static void scan_timeout(unsigned long data)
539 struct floppy_state *fs = (struct floppy_state *) data;
540 volatile struct swim3 *sw = fs->swim3;
542 fs->timeout_pending = 0;
543 out_8(&sw->control_bic, DO_ACTION);
544 out_8(&sw->select, RELAX);
545 out_8(&sw->intr_enable, 0);
547 if (fs->retries > 5) {
557 static void seek_timeout(unsigned long data)
559 struct floppy_state *fs = (struct floppy_state *) data;
560 volatile struct swim3 *sw = fs->swim3;
562 fs->timeout_pending = 0;
563 if (fs->state == settling) {
564 printk(KERN_ERR "swim3: MSI sel=%x ctrl=%x stat=%x intr=%x ie=%x\n",
565 sw->select, sw->control, sw->status, sw->intr, sw->intr_enable);
567 out_8(&sw->control_bic, DO_SEEK);
568 out_8(&sw->select, RELAX);
569 out_8(&sw->intr_enable, 0);
570 if (fs->state == settling && swim3_readbit(fs, SEEK_COMPLETE)) {
571 /* printk(KERN_DEBUG "swim3: missed settling interrupt\n"); */
572 fs->state = locating;
576 printk(KERN_ERR "swim3: seek timeout\n");
582 static void xfer_timeout(unsigned long data)
584 struct floppy_state *fs = (struct floppy_state *) data;
585 volatile struct swim3 *sw = fs->swim3;
586 struct dbdma_regs *dr = fs->dma;
587 struct dbdma_cmd *cp = fs->dma_cmd;
590 fs->timeout_pending = 0;
591 st_le32(&dr->control, RUN << 16);
592 out_8(&sw->intr_enable, 0);
593 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
594 out_8(&sw->select, RELAX);
595 if (CURRENT->cmd == WRITE)
597 if (ld_le16(&cp->xfer_status) != 0)
598 s = fs->scount - ((ld_le16(&cp->res_count) + 511) >> 9);
601 CURRENT->sector += s;
602 CURRENT->current_nr_sectors -= s;
603 printk(KERN_ERR "swim3: timeout %sing sector %ld\n",
604 (CURRENT->cmd==WRITE? "writ": "read"), CURRENT->sector);
610 static void swim3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
612 struct floppy_state *fs = (struct floppy_state *) dev_id;
613 volatile struct swim3 *sw = fs->swim3;
616 struct dbdma_regs *dr;
617 struct dbdma_cmd *cp;
619 err = in_8(&sw->error);
620 intr = in_8(&sw->intr);
622 printk("swim3 intr state=%d intr=%x err=%x\n", fs->state, intr, err);
624 if ((intr & ERROR_INTR) && fs->state != do_transfer)
625 printk(KERN_ERR "swim3_interrupt, state=%d, cmd=%x, intr=%x, err=%x\n",
626 fs->state, CURRENT->cmd, intr, err);
629 if (intr & SEEN_SECTOR) {
630 out_8(&sw->control_bic, DO_ACTION);
631 out_8(&sw->select, RELAX);
632 out_8(&sw->intr_enable, 0);
633 del_timer(&fs->timeout);
634 fs->timeout_pending = 0;
635 if (sw->ctrack == 0xff) {
636 printk(KERN_ERR "swim3: seen sector but cyl=ff?\n");
638 if (fs->retries > 5) {
648 fs->cur_cyl = sw->ctrack;
649 fs->cur_sector = sw->csect;
650 if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
651 printk(KERN_ERR "swim3: expected cyl %d, got %d\n",
652 fs->expect_cyl, fs->cur_cyl);
653 fs->state = do_transfer;
659 if (sw->nseek == 0) {
660 out_8(&sw->control_bic, DO_SEEK);
661 out_8(&sw->select, RELAX);
662 out_8(&sw->intr_enable, 0);
663 del_timer(&fs->timeout);
664 fs->timeout_pending = 0;
665 if (fs->state == seeking)
667 fs->state = settling;
672 out_8(&sw->intr_enable, 0);
673 del_timer(&fs->timeout);
674 fs->timeout_pending = 0;
678 if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
682 /* We must wait a bit for dbdma to complete */
683 for (n=0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
686 out_8(&sw->intr_enable, 0);
687 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
688 out_8(&sw->select, RELAX);
689 del_timer(&fs->timeout);
690 fs->timeout_pending = 0;
691 if (CURRENT->cmd == WRITE)
693 stat = ld_le16(&cp->xfer_status);
694 resid = ld_le16(&cp->res_count);
695 if (intr & ERROR_INTR) {
696 n = fs->scount - 1 - resid / 512;
698 CURRENT->sector += n;
699 CURRENT->current_nr_sectors -= n;
700 CURRENT->buffer += n * 512;
703 if (fs->retries < 5) {
707 printk("swim3: error %sing block %ld (err=%x)\n",
708 CURRENT->cmd == WRITE? "writ": "read",
709 CURRENT->sector, err);
714 if ((stat & ACTIVE) == 0 || resid != 0) {
715 /* musta been an error */
716 printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid);
717 printk(KERN_ERR " state=%d, cmd=%x, intr=%x, err=%x\n",
718 fs->state, CURRENT->cmd, intr, err);
724 CURRENT->sector += fs->scount;
725 CURRENT->current_nr_sectors -= fs->scount;
726 CURRENT->buffer += fs->scount * 512;
727 if (CURRENT->current_nr_sectors <= 0) {
731 fs->req_sector += fs->scount;
732 if (fs->req_sector > fs->secpertrack) {
733 fs->req_sector -= fs->secpertrack;
734 if (++fs->head > 1) {
742 if (fs->state == idle)
746 printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state);
751 static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
756 static int grab_drive(struct floppy_state *fs, enum swim_state state,
763 if (fs->state != idle) {
765 while (fs->state != available) {
766 if (interruptible && signal_pending(current)) {
768 restore_flags(flags);
771 interruptible_sleep_on(&fs->wait);
776 restore_flags(flags);
780 static void release_drive(struct floppy_state *fs)
788 restore_flags(flags);
791 static int fd_eject(struct floppy_state *fs)
795 err = grab_drive(fs, ejecting, 1);
798 swim3_action(fs, EJECT);
799 for (n = 2*HZ; n > 0; --n) {
800 if (swim3_readbit(fs, RELAX))
802 if (signal_pending(current)) {
806 current->state = TASK_INTERRUPTIBLE;
814 static struct floppy_struct floppy_type =
815 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */
817 static int floppy_ioctl(struct inode *inode, struct file *filp,
818 unsigned int cmd, unsigned long param)
820 struct floppy_state *fs;
822 int devnum = MINOR(inode->i_rdev);
824 if (devnum >= floppy_count)
827 if ((cmd & 0x80) && !suser())
830 fs = &floppy_states[devnum];
832 if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
837 if (fs->ref_count != 1)
842 if (copy_to_user((void *) param, (void *)&floppy_type,
843 sizeof(struct floppy_struct)))
850 static int floppy_open(struct inode *inode, struct file *filp)
852 struct floppy_state *fs;
853 volatile struct swim3 *sw;
855 int devnum = MINOR(inode->i_rdev);
857 if (devnum >= floppy_count)
862 fs = &floppy_states[devnum];
865 if (fs->ref_count == 0) {
866 if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
868 out_8(&sw->mode, 0x95);
869 out_8(&sw->control_bic, 0xff);
870 out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
872 out_8(&sw->intr_enable, 0);
873 out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
874 swim3_action(fs, MOTOR_ON);
877 for (n = HZ; n > 0; --n) {
878 if (swim3_readbit(fs, SEEK_COMPLETE))
880 if (signal_pending(current)) {
884 current->state = TASK_INTERRUPTIBLE;
887 if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
888 || swim3_readbit(fs, DISK_IN) == 0))
892 } else if (fs->ref_count == -1 || filp->f_flags & O_EXCL)
895 if (err == 0 && (filp->f_flags & O_NDELAY) == 0
896 && (filp->f_mode & 3)) {
897 check_disk_change(inode->i_rdev);
902 if (err == 0 && (filp->f_mode & 2)) {
903 if (fs->write_prot < 0)
904 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
910 if (fs->ref_count == 0) {
911 swim3_action(fs, MOTOR_OFF);
912 out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
917 if (filp->f_flags & O_EXCL)
925 static int floppy_release(struct inode *inode, struct file *filp)
927 struct floppy_state *fs;
928 volatile struct swim3 *sw;
929 int devnum = MINOR(inode->i_rdev);
931 if (devnum >= floppy_count)
934 fs = &floppy_states[devnum];
936 if (fs->ref_count > 0 && --fs->ref_count == 0) {
937 swim3_action(fs, MOTOR_OFF);
938 out_8(&sw->control_bic, 0xff);
943 static int floppy_check_change(kdev_t dev)
945 struct floppy_state *fs;
946 int devnum = MINOR(dev);
948 if (MAJOR(dev) != MAJOR_NR || (devnum >= floppy_count))
951 fs = &floppy_states[devnum];
955 static int floppy_revalidate(kdev_t dev)
957 struct floppy_state *fs;
958 volatile struct swim3 *sw;
960 int devnum = MINOR(dev);
962 if (MAJOR(dev) != MAJOR_NR || (devnum >= floppy_count))
965 fs = &floppy_states[devnum];
967 if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
971 grab_drive(fs, revalidating, 0);
972 out_8(&sw->intr_enable, 0);
973 out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
974 swim3_action(fs, MOTOR_ON);
977 for (n = HZ; n > 0; --n) {
978 if (swim3_readbit(fs, SEEK_COMPLETE))
980 if (signal_pending(current))
982 current->state = TASK_INTERRUPTIBLE;
985 ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
986 || swim3_readbit(fs, DISK_IN) == 0;
988 swim3_action(fs, MOTOR_OFF);
998 static void floppy_off(unsigned int nr)
1002 static struct block_device_operations floppy_fops = {
1004 release: floppy_release,
1005 ioctl: floppy_ioctl,
1006 check_media_change: floppy_check_change,
1007 revalidate: floppy_revalidate,
1010 static devfs_handle_t floppy_devfs_handle;
1012 int swim3_init(void)
1014 struct device_node *swim;
1016 floppy_devfs_handle = devfs_mk_dir(NULL, "floppy", NULL);
1018 swim = find_devices("floppy");
1019 while (swim && (floppy_count < MAX_FLOPPIES))
1021 swim3_add_device(swim);
1025 swim = find_devices("swim3");
1026 while (swim && (floppy_count < MAX_FLOPPIES))
1028 swim3_add_device(swim);
1032 if (floppy_count > 0)
1034 if (devfs_register_blkdev(MAJOR_NR, "fd", &floppy_fops)) {
1035 printk(KERN_ERR "Unable to get major %d for floppy\n",
1039 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1040 blksize_size[MAJOR_NR] = floppy_blocksizes;
1041 blk_size[MAJOR_NR] = floppy_sizes;
1047 static int swim3_add_device(struct device_node *swim)
1049 struct device_node *mediabay;
1050 struct floppy_state *fs = &floppy_states[floppy_count];
1051 char floppy_name[16];
1052 devfs_handle_t floppy_handle;
1054 if (swim->n_addrs < 2)
1056 printk(KERN_INFO "swim3: expecting 2 addrs (n_addrs:%d, n_intrs:%d)\n",
1057 swim->n_addrs, swim->n_intrs);
1061 if (swim->n_intrs < 2)
1063 printk(KERN_INFO "swim3: expecting 2 intrs (n_addrs:%d, n_intrs:%d)\n",
1064 swim->n_addrs, swim->n_intrs);
1068 if (!request_OF_resource(swim, 0, NULL)) {
1069 printk(KERN_INFO "swim3: can't request IO resource !\n");
1073 mediabay = (strcasecmp(swim->parent->type, "media-bay") == 0) ? swim->parent : NULL;
1074 if (mediabay == NULL)
1075 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
1077 memset(fs, 0, sizeof(*fs));
1079 fs->swim3 = (volatile struct swim3 *) ioremap(swim->addrs[0].address, 0x200);
1080 fs->dma = (struct dbdma_regs *) ioremap(swim->addrs[1].address, 0x200);
1081 fs->swim3_intr = swim->intrs[0].line;
1082 fs->dma_intr = swim->intrs[1].line;
1084 fs->cur_sector = -1;
1086 fs->secpertrack = 18;
1087 fs->total_secs = 2880;
1088 fs->media_bay = mediabay;
1089 init_waitqueue_head(&fs->wait);
1091 fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
1092 memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
1093 st_le16(&fs->dma_cmd[1].command, DBDMA_STOP);
1095 if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
1096 printk(KERN_ERR "Couldn't get irq %d for SWIM3\n", fs->swim3_intr);
1097 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
1101 if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
1102 printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
1104 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
1109 init_timer(&fs->timeout);
1113 printk(KERN_INFO "fd%d: SWIM3 floppy controller %s\n", floppy_count,
1114 mediabay ? "in media bay" : "");
1115 sprintf(floppy_name, "%s%d", floppy_devfs_handle ? "" : "floppy",
1117 floppy_handle = devfs_register(floppy_devfs_handle, floppy_name,
1118 DEVFS_FL_DEFAULT, MAJOR_NR, floppy_count,
1119 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP |S_IWGRP,
1120 &floppy_fops, NULL);