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/stddef.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/timer.h>
23 #include <linux/delay.h>
25 #include <linux/ioctl.h>
27 #include <asm/dbdma.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
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.
61 REG(timer
); /* counts down at 1MHz */
64 REG(select
); /* controls CA0, CA1, CA2 and LSTRB signals */
66 REG(control
); /* writing bits clears them */
67 REG(status
); /* writing bits sets them in control */
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 */
78 #define control_bic control
79 #define control_bis status
81 /* Bits in select register */
85 /* Bits in control register */
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
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
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
139 /* Select values for swim3_select and swim3_readbit */
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
150 #define TRACK_ZERO 10
152 #define READ_DATA_1 12
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 */
186 int secpercyl
; /* disk geometry information */
189 int write_prot
; /* 1 if write-protected, 0 if not, -1 dunno */
190 struct dbdma_cmd
*dma_cmd
;
193 struct timer_list timeout
;
196 wait_queue_head_t wait
;
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 */
215 0x9908, /* stop writing */
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
,
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
);
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(void)
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 (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");
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_MORE
, write_postamble
, sizeof(write_postamble
));
450 init_dma(cp
, INPUT_MORE
, 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 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
)
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;
695 CURRENT
->sector
+= n
;
696 CURRENT
->current_nr_sectors
-= n
;
697 CURRENT
->buffer
+= n
* 512;
700 if (fs
->retries
< 5) {
704 printk("swim3: error %sing block %ld (err=%x)\n",
705 CURRENT
->cmd
== WRITE
? "writ": "read",
706 CURRENT
->sector
, err
);
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
);
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) {
728 fs
->req_sector
+= fs
->scount
;
729 if (fs
->req_sector
> fs
->secpertrack
) {
730 fs
->req_sector
-= fs
->secpertrack
;
731 if (++fs
->head
> 1) {
739 if (fs
->state
== idle
)
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
,
760 if (fs
->state
!= idle
) {
762 while (fs
->state
!= available
) {
763 if (interruptible
&& signal_pending(current
)) {
765 restore_flags(flags
);
768 interruptible_sleep_on(&fs
->wait
);
773 restore_flags(flags
);
777 static void release_drive(struct floppy_state
*fs
)
785 restore_flags(flags
);
788 static int fd_eject(struct floppy_state
*fs
)
792 err
= grab_drive(fs
, ejecting
, 1);
795 swim3_action(fs
, EJECT
);
796 for (n
= 2*HZ
; n
> 0; --n
) {
797 if (swim3_readbit(fs
, RELAX
))
799 if (signal_pending(current
)) {
803 current
->state
= TASK_INTERRUPTIBLE
;
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
;
819 int devnum
= MINOR(inode
->i_rdev
);
821 if (devnum
>= floppy_count
)
824 if (((cmd
& 0x40) && !(filp
&& (filp
->f_mode
& IOCTL_MODE_BIT
))) ||
825 ((cmd
& 0x80) && !suser()))
828 fs
= &floppy_states
[devnum
];
830 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
835 if (fs
->ref_count
!= 1)
840 err
= copy_to_user((void *) param
, (void *) &floppy_type
,
841 sizeof(struct floppy_struct
));
847 static int floppy_open(struct inode
*inode
, struct file
*filp
)
849 struct floppy_state
*fs
;
850 volatile struct swim3
*sw
;
852 int devnum
= MINOR(inode
->i_rdev
);
854 if (devnum
>= floppy_count
)
859 fs
= &floppy_states
[devnum
];
862 if (fs
->ref_count
== 0) {
863 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
865 out_8(&sw
->mode
, 0x95);
866 out_8(&sw
->control_bic
, 0xff);
867 out_8(&sw
->setup
, S_IBM_DRIVE
| S_FCLK_DIV2
);
869 out_8(&sw
->intr_enable
, 0);
870 out_8(&sw
->control_bis
, DRIVE_ENABLE
| INTR_ENABLE
);
871 swim3_action(fs
, MOTOR_ON
);
874 for (n
= HZ
; n
> 0; --n
) {
875 if (swim3_readbit(fs
, SEEK_COMPLETE
))
877 if (signal_pending(current
)) {
881 current
->state
= TASK_INTERRUPTIBLE
;
884 if (err
== 0 && (swim3_readbit(fs
, SEEK_COMPLETE
) == 0
885 || swim3_readbit(fs
, DISK_IN
) == 0))
889 } else if (fs
->ref_count
== -1 || filp
->f_flags
& O_EXCL
)
892 if (err
== 0 && (filp
->f_flags
& O_NDELAY
) == 0
893 && (filp
->f_mode
& 3)) {
894 check_disk_change(inode
->i_rdev
);
899 if (err
== 0 && (filp
->f_mode
& 2)) {
900 if (fs
->write_prot
< 0)
901 fs
->write_prot
= swim3_readbit(fs
, WRITE_PROT
);
907 if (fs
->ref_count
== 0) {
908 swim3_action(fs
, MOTOR_OFF
);
909 out_8(&sw
->control_bic
, DRIVE_ENABLE
| INTR_ENABLE
);
914 if (filp
->f_flags
& O_EXCL
)
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
;
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
)
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
];
948 if (fs
->ref_count
> 0 && --fs
->ref_count
== 0) {
949 swim3_action(fs
, MOTOR_OFF
);
950 out_8(&sw
->control_bic
, 0xff);
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
))
963 fs
= &floppy_states
[devnum
];
967 static int floppy_revalidate(kdev_t dev
)
969 struct floppy_state
*fs
;
970 volatile struct swim3
*sw
;
972 int devnum
= MINOR(dev
);
974 if (MAJOR(dev
) != MAJOR_NR
|| (devnum
>= floppy_count
))
977 fs
= &floppy_states
[devnum
];
979 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
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
);
989 for (n
= HZ
; n
> 0; --n
) {
990 if (swim3_readbit(fs
, SEEK_COMPLETE
))
992 if (signal_pending(current
))
994 current
->state
= TASK_INTERRUPTIBLE
;
997 ret
= swim3_readbit(fs
, SEEK_COMPLETE
) == 0
998 || swim3_readbit(fs
, DISK_IN
) == 0;
1000 swim3_action(fs
, MOTOR_OFF
);
1003 swim3_action(fs
, 9);
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
)
1020 fs
= &floppy_states
[devnum
];
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
)
1035 check_disk_change(inode
->i_rdev
);
1036 fs
= &floppy_states
[devnum
];
1039 if (fs
->write_prot
< 0)
1040 fs
->write_prot
= swim3_readbit(fs
, WRITE_PROT
);
1043 return block_write(filp
, buf
, count
, ppos
);
1046 static void floppy_off(unsigned int nr
)
1050 static struct file_operations floppy_fops
= {
1052 floppy_read
, /* read */
1053 floppy_write
, /* write */
1056 floppy_ioctl
, /* ioctl */
1058 floppy_open
, /* open */
1060 floppy_release
, /* release */
1061 block_fsync
, /* fsync */
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
);
1078 swim
= find_devices("swim3");
1079 while (swim
&& (floppy_count
< MAX_FLOPPIES
))
1081 swim3_add_device(swim
);
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",
1092 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
1093 blksize_size
[MAJOR_NR
] = floppy_blocksizes
;
1094 blk_size
[MAJOR_NR
] = floppy_sizes
;
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
);
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
);
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
));
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
;
1130 fs
->cur_sector
= -1;
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
);
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",
1150 feature_clear(swim, FEATURE_SWIM3_enable);
1155 init_timer(&fs
->timeout
);
1159 printk(KERN_INFO
"fd%d: SWIM3 floppy controller %s\n", floppy_count
,
1160 mediabay
? "in media bay" : "");