Import 2.3.9pre5
[davej-history.git] / drivers / block / swim3.c
blobcee5493da0ec4359c23b539e360ed4c841d29d6a
1 /*
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.
14 * TODO:
15 * handle 2 drives
16 * handle GCR disks
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 <asm/io.h>
27 #include <asm/dbdma.h>
28 #include <asm/prom.h>
29 #include <asm/uaccess.h>
30 #include <asm/mediabay.h>
31 #include <asm/feature.h>
33 #define MAJOR_NR FLOPPY_MAJOR
34 #include <linux/blk.h>
36 static int floppy_blocksizes[2] = {512,512};
37 static int floppy_sizes[2] = {2880,2880};
39 #define MAX_FLOPPIES 2
41 enum swim_state {
42 idle,
43 locating,
44 seeking,
45 settling,
46 do_transfer,
47 jogging,
48 available,
49 revalidating,
50 ejecting
53 #define REG(x) unsigned char x; char x ## _pad[15];
56 * The names for these registers mostly represent speculation on my part.
57 * It will be interesting to see how close they are to the names Apple uses.
59 struct swim3 {
60 REG(data);
61 REG(timer); /* counts down at 1MHz */
62 REG(error);
63 REG(mode);
64 REG(select); /* controls CA0, CA1, CA2 and LSTRB signals */
65 REG(setup);
66 REG(control); /* writing bits clears them */
67 REG(status); /* writing bits sets them in control */
68 REG(intr);
69 REG(nseek); /* # tracks to seek */
70 REG(ctrack); /* current track number */
71 REG(csect); /* current sector number */
72 REG(gap3); /* size of gap 3 in track format */
73 REG(sector); /* sector # to read or write */
74 REG(nsect); /* # sectors to read or write */
75 REG(intr_enable);
78 #define control_bic control
79 #define control_bis status
81 /* Bits in select register */
82 #define CA_MASK 7
83 #define LSTRB 8
85 /* Bits in control register */
86 #define DO_SEEK 0x80
87 #define FORMAT 0x40
88 #define SELECT 0x20
89 #define WRITE_SECTORS 0x10
90 #define DO_ACTION 0x08
91 #define DRIVE2_ENABLE 0x04
92 #define DRIVE_ENABLE 0x02
93 #define INTR_ENABLE 0x01
95 /* Bits in status register */
96 #define FIFO_1BYTE 0x80
97 #define FIFO_2BYTE 0x40
98 #define ERROR 0x20
99 #define DATA 0x08
100 #define RDDATA 0x04
101 #define INTR_PENDING 0x02
102 #define MARK_BYTE 0x01
104 /* Bits in intr and intr_enable registers */
105 #define ERROR_INTR 0x20
106 #define DATA_CHANGED 0x10
107 #define TRANSFER_DONE 0x08
108 #define SEEN_SECTOR 0x04
109 #define SEEK_DONE 0x02
110 #define TIMER_DONE 0x01
112 /* Bits in error register */
113 #define ERR_DATA_CRC 0x80
114 #define ERR_ADDR_CRC 0x40
115 #define ERR_OVERRUN 0x04
116 #define ERR_UNDERRUN 0x01
118 /* Bits in setup register */
119 #define S_SW_RESET 0x80
120 #define S_GCR_WRITE 0x40
121 #define S_IBM_DRIVE 0x20
122 #define S_TEST_MODE 0x10
123 #define S_FCLK_DIV2 0x08
124 #define S_GCR 0x04
125 #define S_COPY_PROT 0x02
126 #define S_INV_WDATA 0x01
128 /* Select values for swim3_action */
129 #define SEEK_POSITIVE 0
130 #define SEEK_NEGATIVE 4
131 #define STEP 1
132 #define MOTOR_ON 2
133 #define MOTOR_OFF 6
134 #define INDEX 3
135 #define EJECT 7
136 #define SETMFM 9
137 #define SETGCR 13
139 /* Select values for swim3_select and swim3_readbit */
140 #define STEP_DIR 0
141 #define STEPPING 1
142 #define MOTOR_ON 2
143 #define RELAX 3 /* also eject in progress */
144 #define READ_DATA_0 4
145 #define TWOMEG_DRIVE 5
146 #define SINGLE_SIDED 6
147 #define DRIVE_PRESENT 7
148 #define DISK_IN 8
149 #define WRITE_PROT 9
150 #define TRACK_ZERO 10
151 #define TACHO 11
152 #define READ_DATA_1 12
153 #define MFM_MODE 13
154 #define SEEK_COMPLETE 14
155 #define ONEMEG_MEDIA 15
157 /* Definitions of values used in writing and formatting */
158 #define DATA_ESCAPE 0x99
159 #define GCR_SYNC_EXC 0x3f
160 #define GCR_SYNC_CONV 0x80
161 #define GCR_FIRST_MARK 0xd5
162 #define GCR_SECOND_MARK 0xaa
163 #define GCR_ADDR_MARK "\xd5\xaa\x00"
164 #define GCR_DATA_MARK "\xd5\xaa\x0b"
165 #define GCR_SLIP_BYTE "\x27\xaa"
166 #define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f"
168 #define DATA_99 "\x99\x99"
169 #define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
170 #define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
171 #define MFM_GAP_LEN 12
173 struct floppy_state {
174 enum swim_state state;
175 volatile struct swim3 *swim3; /* hardware registers */
176 struct dbdma_regs *dma; /* DMA controller registers */
177 int swim3_intr; /* interrupt number for SWIM3 */
178 int dma_intr; /* interrupt number for DMA channel */
179 int cur_cyl; /* cylinder head is on, or -1 */
180 int cur_sector; /* last sector we saw go past */
181 int req_cyl; /* the cylinder for the current r/w request */
182 int head; /* head number ditto */
183 int req_sector; /* sector number ditto */
184 int scount; /* # sectors we're transferring at present */
185 int retries;
186 int secpercyl; /* disk geometry information */
187 int secpertrack;
188 int total_secs;
189 int write_prot; /* 1 if write-protected, 0 if not, -1 dunno */
190 struct dbdma_cmd *dma_cmd;
191 int ref_count;
192 int expect_cyl;
193 struct timer_list timeout;
194 int timeout_pending;
195 int ejected;
196 wait_queue_head_t wait;
197 int wanted;
198 struct device_node* media_bay; /* NULL when not in bay */
199 char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
202 static struct floppy_state floppy_states[MAX_FLOPPIES];
203 static int floppy_count = 0;
205 static unsigned short write_preamble[] = {
206 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */
207 0, 0, 0, 0, 0, 0, /* sync field */
208 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */
209 0x990f /* no escape for 512 bytes */
212 static unsigned short write_postamble[] = {
213 0x9904, /* insert CRC */
214 0x4e4e, 0x4e4e,
215 0x9908, /* stop writing */
216 0, 0, 0, 0, 0, 0
219 static void swim3_select(struct floppy_state *fs, int sel);
220 static void swim3_action(struct floppy_state *fs, int action);
221 static int swim3_readbit(struct floppy_state *fs, int bit);
222 static void do_fd_request(void);
223 static void start_request(struct floppy_state *fs);
224 static void set_timeout(struct floppy_state *fs, int nticks,
225 void (*proc)(unsigned long));
226 static void scan_track(struct floppy_state *fs);
227 static void seek_track(struct floppy_state *fs, int n);
228 static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
229 static void setup_transfer(struct floppy_state *fs);
230 static void act(struct floppy_state *fs);
231 static void scan_timeout(unsigned long data);
232 static void seek_timeout(unsigned long data);
233 static void xfer_timeout(unsigned long data);
234 static void swim3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
235 /*static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs);*/
236 static int grab_drive(struct floppy_state *fs, enum swim_state state,
237 int interruptible);
238 static void release_drive(struct floppy_state *fs);
239 static int fd_eject(struct floppy_state *fs);
240 static int floppy_ioctl(struct inode *inode, struct file *filp,
241 unsigned int cmd, unsigned long param);
242 static int floppy_open(struct inode *inode, struct file *filp);
243 static int floppy_release(struct inode *inode, struct file *filp);
244 static ssize_t floppy_read(struct file *filp, char *buf,
245 size_t count, loff_t *ppos);
246 static ssize_t floppy_write(struct file *filp, const char *buf,
247 size_t count, loff_t *ppos);
248 static int floppy_check_change(kdev_t dev);
249 static int floppy_revalidate(kdev_t dev);
250 static int swim3_add_device(struct device_node *swims);
251 int swim3_init(void);
253 #define IOCTL_MODE_BIT 8
254 #define OPEN_WRITE_BIT 16
256 static void swim3_select(struct floppy_state *fs, int sel)
258 volatile struct swim3 *sw = fs->swim3;
260 out_8(&sw->select, RELAX);
261 if (sel & 8)
262 out_8(&sw->control_bis, SELECT);
263 else
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);
273 udelay(1);
274 out_8(&sw->select, sw->select | LSTRB);
275 udelay(2);
276 out_8(&sw->select, sw->select & ~LSTRB);
277 udelay(1);
278 out_8(&sw->select, RELAX);
281 static int swim3_readbit(struct floppy_state *fs, int bit)
283 volatile struct swim3 *sw = fs->swim3;
284 int stat;
286 swim3_select(fs, bit);
287 udelay(10);
288 stat = in_8(&sw->status);
289 out_8(&sw->select, RELAX);
290 return (stat & DATA) == 0;
293 static void do_fd_request(void)
295 int i;
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))
300 continue;
301 start_request(&floppy_states[i]);
303 sti();
306 static void start_request(struct floppy_state *fs)
308 unsigned long x;
310 if (fs->state == idle && fs->wanted) {
311 fs->state = available;
312 wake_up(&fs->wait);
313 return;
315 while (CURRENT && 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");
320 #if 0
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);
326 #endif
328 if (CURRENT->sector < 0 || CURRENT->sector >= fs->total_secs) {
329 end_request(0);
330 continue;
332 if (CURRENT->current_nr_sectors == 0) {
333 end_request(1);
334 continue;
336 if (fs->ejected) {
337 end_request(0);
338 continue;
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) {
345 end_request(0);
346 continue;
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;
355 fs->retries = 0;
357 act(fs);
361 static void set_timeout(struct floppy_state *fs, int nticks,
362 void (*proc)(unsigned long))
364 unsigned long flags;
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;
380 int xx;
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;
394 if (n >= 0) {
395 swim3_action(fs, SEEK_POSITIVE);
396 sw->nseek = n;
397 } else {
398 swim3_action(fs, SEEK_NEGATIVE);
399 sw->nseek = -n;
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));
415 cp->xfer_status = 0;
418 static inline void setup_transfer(struct floppy_state *fs)
420 int n;
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");
427 return;
429 if (CURRENT->cmd == WRITE)
430 n = 1;
431 else {
432 n = fs->secpertrack - fs->req_sector + 1;
433 if (n > CURRENT->current_nr_sectors)
434 n = CURRENT->current_nr_sectors;
436 fs->scount = n;
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);
440 out_8(&sw->gap3, 0);
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));
445 ++cp;
446 init_dma(cp, OUTPUT_MORE, CURRENT->buffer, 512);
447 ++cp;
448 init_dma(cp, OUTPUT_MORE, write_postamble, sizeof(write_postamble));
449 } else {
450 init_dma(cp, INPUT_MORE, CURRENT->buffer, n * 512);
452 ++cp;
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;
466 for (;;) {
467 switch (fs->state) {
468 case idle:
469 return; /* XXX shouldn't get here */
471 case locating:
472 if (swim3_readbit(fs, TRACK_ZERO)) {
473 fs->cur_cyl = 0;
474 if (fs->req_cyl == 0)
475 fs->state = do_transfer;
476 else
477 fs->state = seeking;
478 break;
480 scan_track(fs);
481 return;
483 case seeking:
484 if (fs->cur_cyl < 0) {
485 fs->expect_cyl = -1;
486 fs->state = locating;
487 break;
489 if (fs->req_cyl == fs->cur_cyl) {
490 printk("whoops, seeking 0\n");
491 fs->state = do_transfer;
492 break;
494 seek_track(fs, fs->req_cyl - fs->cur_cyl);
495 return;
497 case settling:
498 /* wait for SEEK_COMPLETE to become true */
499 swim3_select(fs, SEEK_COMPLETE);
500 udelay(10);
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);
506 return;
508 out_8(&sw->intr_enable, 0);
509 in_8(&sw->intr);
510 fs->state = locating;
511 break;
513 case do_transfer:
514 if (fs->cur_cyl != fs->req_cyl) {
515 if (fs->retries > 5) {
516 end_request(0);
517 fs->state = idle;
518 return;
520 fs->state = seeking;
521 break;
523 setup_transfer(fs);
524 return;
526 case jogging:
527 seek_track(fs, -5);
528 return;
530 default:
531 printk(KERN_ERR"swim3: unknown state %d\n", fs->state);
532 return;
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);
546 fs->cur_cyl = -1;
547 if (fs->retries > 5) {
548 end_request(0);
549 fs->state = idle;
550 start_request(fs);
551 } else {
552 fs->state = jogging;
553 act(fs);
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;
573 act(fs);
574 return;
576 printk(KERN_ERR "swim3: seek timeout\n");
577 end_request(0);
578 fs->state = idle;
579 start_request(fs);
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;
588 unsigned long s;
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)
596 ++cp;
597 if (ld_le16(&cp->xfer_status) != 0)
598 s = fs->scount - ((ld_le16(&cp->res_count) + 511) >> 9);
599 else
600 s = 0;
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);
605 end_request(0);
606 fs->state = idle;
607 start_request(fs);
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;
614 int intr, err, n;
615 int stat, resid;
616 struct dbdma_regs *dr;
617 struct dbdma_cmd *cp;
619 err = in_8(&sw->error);
620 intr = in_8(&sw->intr);
621 #if 0
622 printk("swim3 intr state=%d intr=%x err=%x\n", fs->state, intr, err);
623 #endif
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);
627 switch (fs->state) {
628 case locating:
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");
637 fs->cur_cyl = -1;
638 if (fs->retries > 5) {
639 end_request(0);
640 fs->state = idle;
641 start_request(fs);
642 } else {
643 fs->state = jogging;
644 act(fs);
646 break;
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;
654 act(fs);
656 break;
657 case seeking:
658 case jogging:
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)
666 ++fs->retries;
667 fs->state = settling;
668 act(fs);
670 break;
671 case settling:
672 out_8(&sw->intr_enable, 0);
673 del_timer(&fs->timeout);
674 fs->timeout_pending = 0;
675 act(fs);
676 break;
677 case do_transfer:
678 if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
679 break;
680 dr = fs->dma;
681 cp = fs->dma_cmd;
682 st_le32(&dr->control, RUN << 16);
683 out_8(&sw->intr_enable, 0);
684 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
685 out_8(&sw->select, RELAX);
686 del_timer(&fs->timeout);
687 fs->timeout_pending = 0;
688 if (CURRENT->cmd == WRITE)
689 ++cp;
690 stat = ld_le16(&cp->xfer_status);
691 resid = ld_le16(&cp->res_count);
692 if (intr & ERROR_INTR) {
693 n = fs->scount - 1 - resid / 512;
694 if (n > 0) {
695 CURRENT->sector += n;
696 CURRENT->current_nr_sectors -= n;
697 CURRENT->buffer += n * 512;
698 fs->req_sector += n;
700 if (fs->retries < 5) {
701 ++fs->retries;
702 act(fs);
703 } else {
704 printk("swim3: error %sing block %ld (err=%x)\n",
705 CURRENT->cmd == WRITE? "writ": "read",
706 CURRENT->sector, err);
707 end_request(0);
708 fs->state = idle;
710 } else {
711 if ((stat & ACTIVE) == 0 || resid != 0) {
712 /* musta been an error */
713 printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid);
714 printk(KERN_ERR " state=%d, cmd=%x, intr=%x, err=%x\n",
715 fs->state, CURRENT->cmd, intr, err);
716 end_request(0);
717 fs->state = idle;
718 start_request(fs);
719 break;
721 CURRENT->sector += fs->scount;
722 CURRENT->current_nr_sectors -= fs->scount;
723 CURRENT->buffer += fs->scount * 512;
724 if (CURRENT->current_nr_sectors <= 0) {
725 end_request(1);
726 fs->state = idle;
727 } else {
728 fs->req_sector += fs->scount;
729 if (fs->req_sector > fs->secpertrack) {
730 fs->req_sector -= fs->secpertrack;
731 if (++fs->head > 1) {
732 fs->head = 0;
733 ++fs->req_cyl;
736 act(fs);
739 if (fs->state == idle)
740 start_request(fs);
741 break;
742 default:
743 printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state);
748 static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
753 static int grab_drive(struct floppy_state *fs, enum swim_state state,
754 int interruptible)
756 unsigned long flags;
758 save_flags(flags);
759 cli();
760 if (fs->state != idle) {
761 ++fs->wanted;
762 while (fs->state != available) {
763 if (interruptible && signal_pending(current)) {
764 --fs->wanted;
765 restore_flags(flags);
766 return -EINTR;
768 interruptible_sleep_on(&fs->wait);
770 --fs->wanted;
772 fs->state = state;
773 restore_flags(flags);
774 return 0;
777 static void release_drive(struct floppy_state *fs)
779 unsigned long flags;
781 save_flags(flags);
782 cli();
783 fs->state = idle;
784 start_request(fs);
785 restore_flags(flags);
788 static int fd_eject(struct floppy_state *fs)
790 int err, n;
792 err = grab_drive(fs, ejecting, 1);
793 if (err)
794 return err;
795 swim3_action(fs, EJECT);
796 for (n = 2*HZ; n > 0; --n) {
797 if (swim3_readbit(fs, RELAX))
798 break;
799 if (signal_pending(current)) {
800 err = -EINTR;
801 break;
803 current->state = TASK_INTERRUPTIBLE;
804 schedule_timeout(1);
806 fs->ejected = 1;
807 release_drive(fs);
808 return err;
811 static struct floppy_struct floppy_type =
812 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */
814 static int floppy_ioctl(struct inode *inode, struct file *filp,
815 unsigned int cmd, unsigned long param)
817 struct floppy_state *fs;
818 int err;
819 int devnum = MINOR(inode->i_rdev);
821 if (devnum >= floppy_count)
822 return -ENODEV;
824 if (((cmd & 0x40) && !(filp && (filp->f_mode & IOCTL_MODE_BIT))) ||
825 ((cmd & 0x80) && !suser()))
826 return -EPERM;
828 fs = &floppy_states[devnum];
830 if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
831 return -ENXIO;
833 switch (cmd) {
834 case FDEJECT:
835 if (fs->ref_count != 1)
836 return -EBUSY;
837 err = fd_eject(fs);
838 return err;
839 case FDGETPRM:
840 err = copy_to_user((void *) param, (void *) &floppy_type,
841 sizeof(struct floppy_struct));
842 return err;
844 return -ENOIOCTLCMD;
847 static int floppy_open(struct inode *inode, struct file *filp)
849 struct floppy_state *fs;
850 volatile struct swim3 *sw;
851 int n, err;
852 int devnum = MINOR(inode->i_rdev);
854 if (devnum >= floppy_count)
855 return -ENODEV;
856 if (filp == 0)
857 return -EIO;
859 fs = &floppy_states[devnum];
860 sw = fs->swim3;
861 err = 0;
862 if (fs->ref_count == 0) {
863 if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
864 return -ENXIO;
865 out_8(&sw->mode, 0x95);
866 out_8(&sw->control_bic, 0xff);
867 out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
868 udelay(10);
869 out_8(&sw->intr_enable, 0);
870 out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
871 swim3_action(fs, MOTOR_ON);
872 fs->write_prot = -1;
873 fs->cur_cyl = -1;
874 for (n = HZ; n > 0; --n) {
875 if (swim3_readbit(fs, SEEK_COMPLETE))
876 break;
877 if (signal_pending(current)) {
878 err = -EINTR;
879 break;
881 current->state = TASK_INTERRUPTIBLE;
882 schedule_timeout(1);
884 if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
885 || swim3_readbit(fs, DISK_IN) == 0))
886 err = -ENXIO;
887 swim3_action(fs, 9);
889 } else if (fs->ref_count == -1 || filp->f_flags & O_EXCL)
890 return -EBUSY;
892 if (err == 0 && (filp->f_flags & O_NDELAY) == 0
893 && (filp->f_mode & 3)) {
894 check_disk_change(inode->i_rdev);
895 if (fs->ejected)
896 err = -ENXIO;
899 if (err == 0 && (filp->f_mode & 2)) {
900 if (fs->write_prot < 0)
901 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
902 if (fs->write_prot)
903 err = -EROFS;
906 if (err) {
907 if (fs->ref_count == 0) {
908 swim3_action(fs, MOTOR_OFF);
909 out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
911 return err;
914 if (filp->f_flags & O_EXCL)
915 fs->ref_count = -1;
916 else
917 ++fs->ref_count;
919 /* Allow ioctls if we have write-permissions even if read-only open */
920 if ((filp->f_mode & 2) || (permission(inode, 2) == 0))
921 filp->f_mode |= IOCTL_MODE_BIT;
922 if (filp->f_mode & 2)
923 filp->f_mode |= OPEN_WRITE_BIT;
925 return 0;
928 static int floppy_release(struct inode *inode, struct file *filp)
930 struct floppy_state *fs;
931 volatile struct swim3 *sw;
932 int devnum = MINOR(inode->i_rdev);
934 if (devnum >= floppy_count)
935 return -ENODEV;
938 * If filp is NULL, we're being called from blkdev_release
939 * or after a failed mount attempt. In the former case the
940 * device has already been sync'ed, and in the latter no
941 * sync is required. Otherwise, sync if filp is writable.
943 if (filp && (filp->f_mode & (2 | OPEN_WRITE_BIT)))
944 block_fsync (filp, filp->f_dentry);
946 fs = &floppy_states[devnum];
947 sw = fs->swim3;
948 if (fs->ref_count > 0 && --fs->ref_count == 0) {
949 swim3_action(fs, MOTOR_OFF);
950 out_8(&sw->control_bic, 0xff);
952 return 0;
955 static int floppy_check_change(kdev_t dev)
957 struct floppy_state *fs;
958 int devnum = MINOR(dev);
960 if (MAJOR(dev) != MAJOR_NR || (devnum >= floppy_count))
961 return 0;
963 fs = &floppy_states[devnum];
964 return fs->ejected;
967 static int floppy_revalidate(kdev_t dev)
969 struct floppy_state *fs;
970 volatile struct swim3 *sw;
971 int ret, n;
972 int devnum = MINOR(dev);
974 if (MAJOR(dev) != MAJOR_NR || (devnum >= floppy_count))
975 return 0;
977 fs = &floppy_states[devnum];
979 if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
980 return -ENXIO;
982 sw = fs->swim3;
983 grab_drive(fs, revalidating, 0);
984 out_8(&sw->intr_enable, 0);
985 out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
986 swim3_action(fs, MOTOR_ON);
987 fs->write_prot = -1;
988 fs->cur_cyl = -1;
989 for (n = HZ; n > 0; --n) {
990 if (swim3_readbit(fs, SEEK_COMPLETE))
991 break;
992 if (signal_pending(current))
993 break;
994 current->state = TASK_INTERRUPTIBLE;
995 schedule_timeout(1);
997 ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
998 || swim3_readbit(fs, DISK_IN) == 0;
999 if (ret)
1000 swim3_action(fs, MOTOR_OFF);
1001 else {
1002 fs->ejected = 0;
1003 swim3_action(fs, 9);
1006 release_drive(fs);
1007 return ret;
1010 static ssize_t floppy_read(struct file *filp, char *buf,
1011 size_t count, loff_t *ppos)
1013 struct inode *inode = filp->f_dentry->d_inode;
1014 struct floppy_state *fs;
1015 int devnum = MINOR(inode->i_rdev);
1017 if (devnum >= floppy_count)
1018 return -ENODEV;
1020 fs = &floppy_states[devnum];
1021 if (fs->ejected)
1022 return -ENXIO;
1023 return block_read(filp, buf, count, ppos);
1026 static ssize_t floppy_write(struct file * filp, const char * buf,
1027 size_t count, loff_t *ppos)
1029 struct inode * inode = filp->f_dentry->d_inode;
1030 struct floppy_state *fs;
1031 int devnum = MINOR(inode->i_rdev);
1033 if (devnum >= floppy_count)
1034 return -ENODEV;
1035 check_disk_change(inode->i_rdev);
1036 fs = &floppy_states[devnum];
1037 if (fs->ejected)
1038 return -ENXIO;
1039 if (fs->write_prot < 0)
1040 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
1041 if (fs->write_prot)
1042 return -EROFS;
1043 return block_write(filp, buf, count, ppos);
1046 static void floppy_off(unsigned int nr)
1050 static struct file_operations floppy_fops = {
1051 NULL, /* lseek */
1052 floppy_read, /* read */
1053 floppy_write, /* write */
1054 NULL, /* readdir */
1055 NULL, /* poll */
1056 floppy_ioctl, /* ioctl */
1057 NULL, /* mmap */
1058 floppy_open, /* open */
1059 NULL, /* flush */
1060 floppy_release, /* release */
1061 block_fsync, /* fsync */
1062 NULL, /* fasync */
1063 floppy_check_change, /* check_media_change */
1064 floppy_revalidate, /* revalidate */
1067 int swim3_init(void)
1069 struct device_node *swim;
1071 swim = find_devices("floppy");
1072 while (swim && (floppy_count < MAX_FLOPPIES))
1074 swim3_add_device(swim);
1075 swim = swim->next;
1078 swim = find_devices("swim3");
1079 while (swim && (floppy_count < MAX_FLOPPIES))
1081 swim3_add_device(swim);
1082 swim = swim->next;
1085 if (floppy_count > 0)
1087 if (register_blkdev(MAJOR_NR, "fd", &floppy_fops)) {
1088 printk(KERN_ERR "Unable to get major %d for floppy\n",
1089 MAJOR_NR);
1090 return -EBUSY;
1092 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1093 blksize_size[MAJOR_NR] = floppy_blocksizes;
1094 blk_size[MAJOR_NR] = floppy_sizes;
1097 return 0;
1100 static int swim3_add_device(struct device_node *swim)
1102 struct device_node *mediabay;
1103 struct floppy_state *fs = &floppy_states[floppy_count];
1105 if (swim->n_addrs < 2)
1107 printk(KERN_INFO "swim3: expecting 2 addrs (n_addrs:%d, n_intrs:%d)\n",
1108 swim->n_addrs, swim->n_intrs);
1109 return -EINVAL;
1112 if (swim->n_intrs < 2)
1114 printk(KERN_INFO "swim3: expecting 2 intrs (n_addrs:%d, n_intrs:%d)\n",
1115 swim->n_addrs, swim->n_intrs);
1116 return -EINVAL;
1119 mediabay = (strcasecmp(swim->parent->type, "media-bay") == 0) ? swim->parent : NULL;
1120 if (mediabay == NULL)
1121 feature_set(swim, FEATURE_SWIM3_enable);
1123 memset(fs, 0, sizeof(*fs));
1124 fs->state = idle;
1125 fs->swim3 = (volatile struct swim3 *) ioremap(swim->addrs[0].address, 0x200);
1126 fs->dma = (struct dbdma_regs *) ioremap(swim->addrs[1].address, 0x200);
1127 fs->swim3_intr = swim->intrs[0].line;
1128 fs->dma_intr = swim->intrs[1].line;
1129 fs->cur_cyl = -1;
1130 fs->cur_sector = -1;
1131 fs->secpercyl = 36;
1132 fs->secpertrack = 18;
1133 fs->total_secs = 2880;
1134 fs->media_bay = mediabay;
1135 init_waitqueue_head(&fs->wait);
1137 fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
1138 memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
1139 st_le16(&fs->dma_cmd[1].command, DBDMA_STOP);
1141 if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
1142 printk(KERN_ERR "Couldn't get irq %d for SWIM3\n", fs->swim3_intr);
1143 feature_clear(swim, FEATURE_SWIM3_enable);
1144 return -EBUSY;
1147 if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
1148 printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
1149 fs->dma_intr);
1150 feature_clear(swim, FEATURE_SWIM3_enable);
1151 return -EBUSY;
1155 init_timer(&fs->timeout);
1157 do_floppy = NULL;
1159 printk(KERN_INFO "fd%d: SWIM3 floppy controller %s\n", floppy_count,
1160 mediabay ? "in media bay" : "");
1162 floppy_count++;
1164 return 0;