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/feature.h>
34 #define MAJOR_NR FLOPPY_MAJOR
35 #include <linux/blk.h>
37 static int floppy_blocksizes
[2] = {512,512};
38 static int floppy_sizes
[2] = {2880,2880};
40 #define MAX_FLOPPIES 2
54 #define REG(x) unsigned char x; char x ## _pad[15];
57 * The names for these registers mostly represent speculation on my part.
58 * It will be interesting to see how close they are to the names Apple uses.
62 REG(timer
); /* counts down at 1MHz */
65 REG(select
); /* controls CA0, CA1, CA2 and LSTRB signals */
67 REG(control
); /* writing bits clears them */
68 REG(status
); /* writing bits sets them in control */
70 REG(nseek
); /* # tracks to seek */
71 REG(ctrack
); /* current track number */
72 REG(csect
); /* current sector number */
73 REG(gap3
); /* size of gap 3 in track format */
74 REG(sector
); /* sector # to read or write */
75 REG(nsect
); /* # sectors to read or write */
79 #define control_bic control
80 #define control_bis status
82 /* Bits in select register */
86 /* Bits in control register */
90 #define WRITE_SECTORS 0x10
91 #define DO_ACTION 0x08
92 #define DRIVE2_ENABLE 0x04
93 #define DRIVE_ENABLE 0x02
94 #define INTR_ENABLE 0x01
96 /* Bits in status register */
97 #define FIFO_1BYTE 0x80
98 #define FIFO_2BYTE 0x40
102 #define INTR_PENDING 0x02
103 #define MARK_BYTE 0x01
105 /* Bits in intr and intr_enable registers */
106 #define ERROR_INTR 0x20
107 #define DATA_CHANGED 0x10
108 #define TRANSFER_DONE 0x08
109 #define SEEN_SECTOR 0x04
110 #define SEEK_DONE 0x02
111 #define TIMER_DONE 0x01
113 /* Bits in error register */
114 #define ERR_DATA_CRC 0x80
115 #define ERR_ADDR_CRC 0x40
116 #define ERR_OVERRUN 0x04
117 #define ERR_UNDERRUN 0x01
119 /* Bits in setup register */
120 #define S_SW_RESET 0x80
121 #define S_GCR_WRITE 0x40
122 #define S_IBM_DRIVE 0x20
123 #define S_TEST_MODE 0x10
124 #define S_FCLK_DIV2 0x08
126 #define S_COPY_PROT 0x02
127 #define S_INV_WDATA 0x01
129 /* Select values for swim3_action */
130 #define SEEK_POSITIVE 0
131 #define SEEK_NEGATIVE 4
140 /* Select values for swim3_select and swim3_readbit */
144 #define RELAX 3 /* also eject in progress */
145 #define READ_DATA_0 4
146 #define TWOMEG_DRIVE 5
147 #define SINGLE_SIDED 6
148 #define DRIVE_PRESENT 7
151 #define TRACK_ZERO 10
153 #define READ_DATA_1 12
155 #define SEEK_COMPLETE 14
156 #define ONEMEG_MEDIA 15
158 /* Definitions of values used in writing and formatting */
159 #define DATA_ESCAPE 0x99
160 #define GCR_SYNC_EXC 0x3f
161 #define GCR_SYNC_CONV 0x80
162 #define GCR_FIRST_MARK 0xd5
163 #define GCR_SECOND_MARK 0xaa
164 #define GCR_ADDR_MARK "\xd5\xaa\x00"
165 #define GCR_DATA_MARK "\xd5\xaa\x0b"
166 #define GCR_SLIP_BYTE "\x27\xaa"
167 #define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f"
169 #define DATA_99 "\x99\x99"
170 #define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
171 #define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
172 #define MFM_GAP_LEN 12
174 struct floppy_state
{
175 enum swim_state state
;
176 volatile struct swim3
*swim3
; /* hardware registers */
177 struct dbdma_regs
*dma
; /* DMA controller registers */
178 int swim3_intr
; /* interrupt number for SWIM3 */
179 int dma_intr
; /* interrupt number for DMA channel */
180 int cur_cyl
; /* cylinder head is on, or -1 */
181 int cur_sector
; /* last sector we saw go past */
182 int req_cyl
; /* the cylinder for the current r/w request */
183 int head
; /* head number ditto */
184 int req_sector
; /* sector number ditto */
185 int scount
; /* # sectors we're transferring at present */
187 int secpercyl
; /* disk geometry information */
190 int write_prot
; /* 1 if write-protected, 0 if not, -1 dunno */
191 struct dbdma_cmd
*dma_cmd
;
194 struct timer_list timeout
;
197 wait_queue_head_t wait
;
199 struct device_node
* media_bay
; /* NULL when not in bay */
200 char dbdma_cmd_space
[5 * sizeof(struct dbdma_cmd
)];
203 static struct floppy_state floppy_states
[MAX_FLOPPIES
];
204 static int floppy_count
= 0;
206 static unsigned short write_preamble
[] = {
207 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */
208 0, 0, 0, 0, 0, 0, /* sync field */
209 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */
210 0x990f /* no escape for 512 bytes */
213 static unsigned short write_postamble
[] = {
214 0x9904, /* insert CRC */
216 0x9908, /* stop writing */
220 static void swim3_select(struct floppy_state
*fs
, int sel
);
221 static void swim3_action(struct floppy_state
*fs
, int action
);
222 static int swim3_readbit(struct floppy_state
*fs
, int bit
);
223 static void do_fd_request(request_queue_t
* q
);
224 static void start_request(struct floppy_state
*fs
);
225 static void set_timeout(struct floppy_state
*fs
, int nticks
,
226 void (*proc
)(unsigned long));
227 static void scan_track(struct floppy_state
*fs
);
228 static void seek_track(struct floppy_state
*fs
, int n
);
229 static void init_dma(struct dbdma_cmd
*cp
, int cmd
, void *buf
, int count
);
230 static void setup_transfer(struct floppy_state
*fs
);
231 static void act(struct floppy_state
*fs
);
232 static void scan_timeout(unsigned long data
);
233 static void seek_timeout(unsigned long data
);
234 static void xfer_timeout(unsigned long data
);
235 static void swim3_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
);
236 /*static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs);*/
237 static int grab_drive(struct floppy_state
*fs
, enum swim_state state
,
239 static void release_drive(struct floppy_state
*fs
);
240 static int fd_eject(struct floppy_state
*fs
);
241 static int floppy_ioctl(struct inode
*inode
, struct file
*filp
,
242 unsigned int cmd
, unsigned long param
);
243 static int floppy_open(struct inode
*inode
, struct file
*filp
);
244 static int floppy_release(struct inode
*inode
, struct file
*filp
);
245 static int floppy_check_change(kdev_t dev
);
246 static int floppy_revalidate(kdev_t dev
);
247 static int swim3_add_device(struct device_node
*swims
);
248 int swim3_init(void);
250 #ifndef CONFIG_PMAC_PBOOK
251 static inline int check_media_bay(struct device_node
*which_bay
, int what
)
257 static void swim3_select(struct floppy_state
*fs
, int sel
)
259 volatile struct swim3
*sw
= fs
->swim3
;
261 out_8(&sw
->select
, RELAX
);
263 out_8(&sw
->control_bis
, SELECT
);
265 out_8(&sw
->control_bic
, SELECT
);
266 out_8(&sw
->select
, sel
& CA_MASK
);
269 static void swim3_action(struct floppy_state
*fs
, int action
)
271 volatile struct swim3
*sw
= fs
->swim3
;
273 swim3_select(fs
, action
);
275 out_8(&sw
->select
, sw
->select
| LSTRB
);
277 out_8(&sw
->select
, sw
->select
& ~LSTRB
);
279 out_8(&sw
->select
, RELAX
);
282 static int swim3_readbit(struct floppy_state
*fs
, int bit
)
284 volatile struct swim3
*sw
= fs
->swim3
;
287 swim3_select(fs
, bit
);
289 stat
= in_8(&sw
->status
);
290 out_8(&sw
->select
, RELAX
);
291 return (stat
& DATA
) == 0;
294 static void do_fd_request(request_queue_t
* q
)
297 for(i
=0;i
<floppy_count
;i
++)
299 if (floppy_states
[i
].media_bay
&&
300 check_media_bay(floppy_states
[i
].media_bay
, MB_FD
))
302 start_request(&floppy_states
[i
]);
307 static void start_request(struct floppy_state
*fs
)
311 if (fs
->state
== idle
&& fs
->wanted
) {
312 fs
->state
= available
;
316 while (!QUEUE_EMPTY
&& fs
->state
== idle
) {
317 if (MAJOR(CURRENT
->rq_dev
) != MAJOR_NR
)
318 panic(DEVICE_NAME
": request list destroyed");
319 if (CURRENT
->bh
&& !buffer_locked(CURRENT
->bh
))
320 panic(DEVICE_NAME
": block not locked");
322 printk("do_fd_req: dev=%x cmd=%d sec=%ld nr_sec=%ld buf=%p\n",
323 kdev_t_to_nr(CURRENT
->rq_dev
), CURRENT
->cmd
,
324 CURRENT
->sector
, CURRENT
->nr_sectors
, CURRENT
->buffer
);
325 printk(" rq_status=%d errors=%d current_nr_sectors=%ld\n",
326 CURRENT
->rq_status
, CURRENT
->errors
, CURRENT
->current_nr_sectors
);
329 if (CURRENT
->sector
< 0 || CURRENT
->sector
>= fs
->total_secs
) {
333 if (CURRENT
->current_nr_sectors
== 0) {
342 if (CURRENT
->cmd
== WRITE
) {
343 if (fs
->write_prot
< 0)
344 fs
->write_prot
= swim3_readbit(fs
, WRITE_PROT
);
345 if (fs
->write_prot
) {
351 fs
->req_cyl
= CURRENT
->sector
/ fs
->secpercyl
;
352 x
= CURRENT
->sector
% fs
->secpercyl
;
353 fs
->head
= x
/ fs
->secpertrack
;
354 fs
->req_sector
= x
% fs
->secpertrack
+ 1;
355 fs
->state
= do_transfer
;
362 static void set_timeout(struct floppy_state
*fs
, int nticks
,
363 void (*proc
)(unsigned long))
367 save_flags(flags
); cli();
368 if (fs
->timeout_pending
)
369 del_timer(&fs
->timeout
);
370 fs
->timeout
.expires
= jiffies
+ nticks
;
371 fs
->timeout
.function
= proc
;
372 fs
->timeout
.data
= (unsigned long) fs
;
373 add_timer(&fs
->timeout
);
374 fs
->timeout_pending
= 1;
375 restore_flags(flags
);
378 static inline void scan_track(struct floppy_state
*fs
)
380 volatile struct swim3
*sw
= fs
->swim3
;
383 swim3_select(fs
, READ_DATA_0
);
384 xx
= sw
->intr
; /* clear SEEN_SECTOR bit */
385 out_8(&sw
->control_bis
, DO_ACTION
);
386 /* enable intr when track found */
387 out_8(&sw
->intr_enable
, ERROR_INTR
| SEEN_SECTOR
);
388 set_timeout(fs
, HZ
, scan_timeout
); /* enable timeout */
391 static inline void seek_track(struct floppy_state
*fs
, int n
)
393 volatile struct swim3
*sw
= fs
->swim3
;
396 swim3_action(fs
, SEEK_POSITIVE
);
399 swim3_action(fs
, SEEK_NEGATIVE
);
402 fs
->expect_cyl
= (fs
->cur_cyl
> 0)? fs
->cur_cyl
+ n
: -1;
403 swim3_select(fs
, STEP
);
404 out_8(&sw
->control_bis
, DO_SEEK
);
405 /* enable intr when seek finished */
406 out_8(&sw
->intr_enable
, ERROR_INTR
| SEEK_DONE
);
407 set_timeout(fs
, HZ
/2, seek_timeout
); /* enable timeout */
410 static inline void init_dma(struct dbdma_cmd
*cp
, int cmd
,
411 void *buf
, int count
)
413 st_le16(&cp
->req_count
, count
);
414 st_le16(&cp
->command
, cmd
);
415 st_le32(&cp
->phy_addr
, virt_to_bus(buf
));
419 static inline void setup_transfer(struct floppy_state
*fs
)
422 volatile struct swim3
*sw
= fs
->swim3
;
423 struct dbdma_cmd
*cp
= fs
->dma_cmd
;
424 struct dbdma_regs
*dr
= fs
->dma
;
426 if (CURRENT
->current_nr_sectors
<= 0) {
427 printk(KERN_ERR
"swim3: transfer 0 sectors?\n");
430 if (CURRENT
->cmd
== WRITE
)
433 n
= fs
->secpertrack
- fs
->req_sector
+ 1;
434 if (n
> CURRENT
->current_nr_sectors
)
435 n
= CURRENT
->current_nr_sectors
;
438 swim3_select(fs
, fs
->head
? READ_DATA_1
: READ_DATA_0
);
439 out_8(&sw
->sector
, fs
->req_sector
);
440 out_8(&sw
->nsect
, n
);
442 st_le32(&dr
->cmdptr
, virt_to_bus(cp
));
443 if (CURRENT
->cmd
== WRITE
) {
444 /* Set up 3 dma commands: write preamble, data, postamble */
445 init_dma(cp
, OUTPUT_MORE
, write_preamble
, sizeof(write_preamble
));
447 init_dma(cp
, OUTPUT_MORE
, CURRENT
->buffer
, 512);
449 init_dma(cp
, OUTPUT_MORE
, write_postamble
, sizeof(write_postamble
));
451 init_dma(cp
, INPUT_MORE
, CURRENT
->buffer
, n
* 512);
454 out_le16(&cp
->command
, DBDMA_STOP
);
455 out_le32(&dr
->control
, (RUN
<< 16) | RUN
);
456 out_8(&sw
->control_bis
,
457 (CURRENT
->cmd
== WRITE
? WRITE_SECTORS
: 0) | DO_ACTION
);
458 /* enable intr when transfer complete */
459 out_8(&sw
->intr_enable
, ERROR_INTR
| TRANSFER_DONE
);
460 set_timeout(fs
, 2*HZ
, xfer_timeout
); /* enable timeout */
463 static void act(struct floppy_state
*fs
)
465 volatile struct swim3
*sw
= fs
->swim3
;
470 return; /* XXX shouldn't get here */
473 if (swim3_readbit(fs
, TRACK_ZERO
)) {
475 if (fs
->req_cyl
== 0)
476 fs
->state
= do_transfer
;
485 if (fs
->cur_cyl
< 0) {
487 fs
->state
= locating
;
490 if (fs
->req_cyl
== fs
->cur_cyl
) {
491 printk("whoops, seeking 0\n");
492 fs
->state
= do_transfer
;
495 seek_track(fs
, fs
->req_cyl
- fs
->cur_cyl
);
499 /* wait for SEEK_COMPLETE to become true */
500 swim3_select(fs
, SEEK_COMPLETE
);
502 out_8(&sw
->intr_enable
, ERROR_INTR
| DATA_CHANGED
);
503 in_8(&sw
->intr
); /* clear DATA_CHANGED */
504 if (in_8(&sw
->status
) & DATA
) {
505 /* seek_complete is not yet true */
506 set_timeout(fs
, HZ
/2, seek_timeout
);
509 out_8(&sw
->intr_enable
, 0);
511 fs
->state
= locating
;
515 if (fs
->cur_cyl
!= fs
->req_cyl
) {
516 if (fs
->retries
> 5) {
532 printk(KERN_ERR
"swim3: unknown state %d\n", fs
->state
);
538 static void scan_timeout(unsigned long data
)
540 struct floppy_state
*fs
= (struct floppy_state
*) data
;
541 volatile struct swim3
*sw
= fs
->swim3
;
543 fs
->timeout_pending
= 0;
544 out_8(&sw
->control_bic
, DO_ACTION
);
545 out_8(&sw
->select
, RELAX
);
546 out_8(&sw
->intr_enable
, 0);
548 if (fs
->retries
> 5) {
558 static void seek_timeout(unsigned long data
)
560 struct floppy_state
*fs
= (struct floppy_state
*) data
;
561 volatile struct swim3
*sw
= fs
->swim3
;
563 fs
->timeout_pending
= 0;
564 if (fs
->state
== settling
) {
565 printk(KERN_ERR
"swim3: MSI sel=%x ctrl=%x stat=%x intr=%x ie=%x\n",
566 sw
->select
, sw
->control
, sw
->status
, sw
->intr
, sw
->intr_enable
);
568 out_8(&sw
->control_bic
, DO_SEEK
);
569 out_8(&sw
->select
, RELAX
);
570 out_8(&sw
->intr_enable
, 0);
571 if (fs
->state
== settling
&& swim3_readbit(fs
, SEEK_COMPLETE
)) {
572 /* printk(KERN_DEBUG "swim3: missed settling interrupt\n"); */
573 fs
->state
= locating
;
577 printk(KERN_ERR
"swim3: seek timeout\n");
583 static void xfer_timeout(unsigned long data
)
585 struct floppy_state
*fs
= (struct floppy_state
*) data
;
586 volatile struct swim3
*sw
= fs
->swim3
;
587 struct dbdma_regs
*dr
= fs
->dma
;
588 struct dbdma_cmd
*cp
= fs
->dma_cmd
;
591 fs
->timeout_pending
= 0;
592 st_le32(&dr
->control
, RUN
<< 16);
593 out_8(&sw
->intr_enable
, 0);
594 out_8(&sw
->control_bic
, WRITE_SECTORS
| DO_ACTION
);
595 out_8(&sw
->select
, RELAX
);
596 if (CURRENT
->cmd
== WRITE
)
598 if (ld_le16(&cp
->xfer_status
) != 0)
599 s
= fs
->scount
- ((ld_le16(&cp
->res_count
) + 511) >> 9);
602 CURRENT
->sector
+= s
;
603 CURRENT
->current_nr_sectors
-= s
;
604 printk(KERN_ERR
"swim3: timeout %sing sector %ld\n",
605 (CURRENT
->cmd
==WRITE
? "writ": "read"), CURRENT
->sector
);
611 static void swim3_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
613 struct floppy_state
*fs
= (struct floppy_state
*) dev_id
;
614 volatile struct swim3
*sw
= fs
->swim3
;
617 struct dbdma_regs
*dr
;
618 struct dbdma_cmd
*cp
;
620 err
= in_8(&sw
->error
);
621 intr
= in_8(&sw
->intr
);
623 printk("swim3 intr state=%d intr=%x err=%x\n", fs
->state
, intr
, err
);
625 if ((intr
& ERROR_INTR
) && fs
->state
!= do_transfer
)
626 printk(KERN_ERR
"swim3_interrupt, state=%d, cmd=%x, intr=%x, err=%x\n",
627 fs
->state
, CURRENT
->cmd
, intr
, err
);
630 if (intr
& SEEN_SECTOR
) {
631 out_8(&sw
->control_bic
, DO_ACTION
);
632 out_8(&sw
->select
, RELAX
);
633 out_8(&sw
->intr_enable
, 0);
634 del_timer(&fs
->timeout
);
635 fs
->timeout_pending
= 0;
636 if (sw
->ctrack
== 0xff) {
637 printk(KERN_ERR
"swim3: seen sector but cyl=ff?\n");
639 if (fs
->retries
> 5) {
649 fs
->cur_cyl
= sw
->ctrack
;
650 fs
->cur_sector
= sw
->csect
;
651 if (fs
->expect_cyl
!= -1 && fs
->expect_cyl
!= fs
->cur_cyl
)
652 printk(KERN_ERR
"swim3: expected cyl %d, got %d\n",
653 fs
->expect_cyl
, fs
->cur_cyl
);
654 fs
->state
= do_transfer
;
660 if (sw
->nseek
== 0) {
661 out_8(&sw
->control_bic
, DO_SEEK
);
662 out_8(&sw
->select
, RELAX
);
663 out_8(&sw
->intr_enable
, 0);
664 del_timer(&fs
->timeout
);
665 fs
->timeout_pending
= 0;
666 if (fs
->state
== seeking
)
668 fs
->state
= settling
;
673 out_8(&sw
->intr_enable
, 0);
674 del_timer(&fs
->timeout
);
675 fs
->timeout_pending
= 0;
679 if ((intr
& (ERROR_INTR
| TRANSFER_DONE
)) == 0)
683 st_le32(&dr
->control
, RUN
<< 16);
684 out_8(&sw
->intr_enable
, 0);
685 out_8(&sw
->control_bic
, WRITE_SECTORS
| DO_ACTION
);
686 out_8(&sw
->select
, RELAX
);
687 del_timer(&fs
->timeout
);
688 fs
->timeout_pending
= 0;
689 if (CURRENT
->cmd
== WRITE
)
691 stat
= ld_le16(&cp
->xfer_status
);
692 resid
= ld_le16(&cp
->res_count
);
693 if (intr
& ERROR_INTR
) {
694 n
= fs
->scount
- 1 - resid
/ 512;
696 CURRENT
->sector
+= n
;
697 CURRENT
->current_nr_sectors
-= n
;
698 CURRENT
->buffer
+= n
* 512;
701 if (fs
->retries
< 5) {
705 printk("swim3: error %sing block %ld (err=%x)\n",
706 CURRENT
->cmd
== WRITE
? "writ": "read",
707 CURRENT
->sector
, err
);
712 if ((stat
& ACTIVE
) == 0 || resid
!= 0) {
713 /* musta been an error */
714 printk(KERN_ERR
"swim3: fd dma: stat=%x resid=%d\n", stat
, resid
);
715 printk(KERN_ERR
" state=%d, cmd=%x, intr=%x, err=%x\n",
716 fs
->state
, CURRENT
->cmd
, intr
, err
);
722 CURRENT
->sector
+= fs
->scount
;
723 CURRENT
->current_nr_sectors
-= fs
->scount
;
724 CURRENT
->buffer
+= fs
->scount
* 512;
725 if (CURRENT
->current_nr_sectors
<= 0) {
729 fs
->req_sector
+= fs
->scount
;
730 if (fs
->req_sector
> fs
->secpertrack
) {
731 fs
->req_sector
-= fs
->secpertrack
;
732 if (++fs
->head
> 1) {
740 if (fs
->state
== idle
)
744 printk(KERN_ERR
"swim3: don't know what to do in state %d\n", fs
->state
);
749 static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
754 static int grab_drive(struct floppy_state
*fs
, enum swim_state state
,
761 if (fs
->state
!= idle
) {
763 while (fs
->state
!= available
) {
764 if (interruptible
&& signal_pending(current
)) {
766 restore_flags(flags
);
769 interruptible_sleep_on(&fs
->wait
);
774 restore_flags(flags
);
778 static void release_drive(struct floppy_state
*fs
)
786 restore_flags(flags
);
789 static int fd_eject(struct floppy_state
*fs
)
793 err
= grab_drive(fs
, ejecting
, 1);
796 swim3_action(fs
, EJECT
);
797 for (n
= 2*HZ
; n
> 0; --n
) {
798 if (swim3_readbit(fs
, RELAX
))
800 if (signal_pending(current
)) {
804 current
->state
= TASK_INTERRUPTIBLE
;
812 int swim3_fd_eject(int devnum
)
814 if (devnum
>= floppy_count
)
816 /* Do not check this - this function should ONLY be called early
817 * in the boot process! */
818 /* if (floppy_states[devnum].ref_count != 1) return -EBUSY; */
819 return fd_eject(&floppy_states
[devnum
]);
822 static struct floppy_struct floppy_type
=
823 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL
}; /* 7 1.44MB 3.5" */
825 static int floppy_ioctl(struct inode
*inode
, struct file
*filp
,
826 unsigned int cmd
, unsigned long param
)
828 struct floppy_state
*fs
;
830 int devnum
= MINOR(inode
->i_rdev
);
832 if (devnum
>= floppy_count
)
835 if ((cmd
& 0x80) && !suser())
838 fs
= &floppy_states
[devnum
];
840 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
845 if (fs
->ref_count
!= 1)
850 err
= copy_to_user((void *) param
, (void *) &floppy_type
,
851 sizeof(struct floppy_struct
));
857 static int floppy_open(struct inode
*inode
, struct file
*filp
)
859 struct floppy_state
*fs
;
860 volatile struct swim3
*sw
;
862 int devnum
= MINOR(inode
->i_rdev
);
864 if (devnum
>= floppy_count
)
869 fs
= &floppy_states
[devnum
];
872 if (fs
->ref_count
== 0) {
873 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
875 out_8(&sw
->mode
, 0x95);
876 out_8(&sw
->control_bic
, 0xff);
877 out_8(&sw
->setup
, S_IBM_DRIVE
| S_FCLK_DIV2
);
879 out_8(&sw
->intr_enable
, 0);
880 out_8(&sw
->control_bis
, DRIVE_ENABLE
| INTR_ENABLE
);
881 swim3_action(fs
, MOTOR_ON
);
884 for (n
= HZ
; n
> 0; --n
) {
885 if (swim3_readbit(fs
, SEEK_COMPLETE
))
887 if (signal_pending(current
)) {
891 current
->state
= TASK_INTERRUPTIBLE
;
894 if (err
== 0 && (swim3_readbit(fs
, SEEK_COMPLETE
) == 0
895 || swim3_readbit(fs
, DISK_IN
) == 0))
899 } else if (fs
->ref_count
== -1 || filp
->f_flags
& O_EXCL
)
902 if (err
== 0 && (filp
->f_flags
& O_NDELAY
) == 0
903 && (filp
->f_mode
& 3)) {
904 check_disk_change(inode
->i_rdev
);
909 if (err
== 0 && (filp
->f_mode
& 2)) {
910 if (fs
->write_prot
< 0)
911 fs
->write_prot
= swim3_readbit(fs
, WRITE_PROT
);
917 if (fs
->ref_count
== 0) {
918 swim3_action(fs
, MOTOR_OFF
);
919 out_8(&sw
->control_bic
, DRIVE_ENABLE
| INTR_ENABLE
);
924 if (filp
->f_flags
& O_EXCL
)
932 static int floppy_release(struct inode
*inode
, struct file
*filp
)
934 struct floppy_state
*fs
;
935 volatile struct swim3
*sw
;
936 int devnum
= MINOR(inode
->i_rdev
);
938 if (devnum
>= floppy_count
)
941 fs
= &floppy_states
[devnum
];
943 if (fs
->ref_count
> 0 && --fs
->ref_count
== 0) {
944 swim3_action(fs
, MOTOR_OFF
);
945 out_8(&sw
->control_bic
, 0xff);
950 static int floppy_check_change(kdev_t dev
)
952 struct floppy_state
*fs
;
953 int devnum
= MINOR(dev
);
955 if (MAJOR(dev
) != MAJOR_NR
|| (devnum
>= floppy_count
))
958 fs
= &floppy_states
[devnum
];
962 static int floppy_revalidate(kdev_t dev
)
964 struct floppy_state
*fs
;
965 volatile struct swim3
*sw
;
967 int devnum
= MINOR(dev
);
969 if (MAJOR(dev
) != MAJOR_NR
|| (devnum
>= floppy_count
))
972 fs
= &floppy_states
[devnum
];
974 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
978 grab_drive(fs
, revalidating
, 0);
979 out_8(&sw
->intr_enable
, 0);
980 out_8(&sw
->control_bis
, DRIVE_ENABLE
| INTR_ENABLE
);
981 swim3_action(fs
, MOTOR_ON
);
984 for (n
= HZ
; n
> 0; --n
) {
985 if (swim3_readbit(fs
, SEEK_COMPLETE
))
987 if (signal_pending(current
))
989 current
->state
= TASK_INTERRUPTIBLE
;
992 ret
= swim3_readbit(fs
, SEEK_COMPLETE
) == 0
993 || swim3_readbit(fs
, DISK_IN
) == 0;
995 swim3_action(fs
, MOTOR_OFF
);
1005 static void floppy_off(unsigned int nr
)
1009 static struct block_device_operations floppy_fops
= {
1011 release
: floppy_release
,
1012 ioctl
: floppy_ioctl
,
1013 check_media_change
: floppy_check_change
,
1014 revalidate
: floppy_revalidate
,
1017 int swim3_init(void)
1019 struct device_node
*swim
;
1021 swim
= find_devices("floppy");
1022 while (swim
&& (floppy_count
< MAX_FLOPPIES
))
1024 swim3_add_device(swim
);
1028 swim
= find_devices("swim3");
1029 while (swim
&& (floppy_count
< MAX_FLOPPIES
))
1031 swim3_add_device(swim
);
1035 if (floppy_count
> 0)
1037 if (register_blkdev(MAJOR_NR
, "fd", &floppy_fops
)) {
1038 printk(KERN_ERR
"Unable to get major %d for floppy\n",
1042 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
), DEVICE_REQUEST
);
1043 blksize_size
[MAJOR_NR
] = floppy_blocksizes
;
1044 blk_size
[MAJOR_NR
] = floppy_sizes
;
1050 static int swim3_add_device(struct device_node
*swim
)
1052 struct device_node
*mediabay
;
1053 struct floppy_state
*fs
= &floppy_states
[floppy_count
];
1055 if (swim
->n_addrs
< 2)
1057 printk(KERN_INFO
"swim3: expecting 2 addrs (n_addrs:%d, n_intrs:%d)\n",
1058 swim
->n_addrs
, swim
->n_intrs
);
1062 if (swim
->n_intrs
< 2)
1064 printk(KERN_INFO
"swim3: expecting 2 intrs (n_addrs:%d, n_intrs:%d)\n",
1065 swim
->n_addrs
, swim
->n_intrs
);
1069 mediabay
= (strcasecmp(swim
->parent
->type
, "media-bay") == 0) ? swim
->parent
: NULL
;
1070 if (mediabay
== NULL
)
1071 feature_set(swim
, FEATURE_SWIM3_enable
);
1073 memset(fs
, 0, sizeof(*fs
));
1075 fs
->swim3
= (volatile struct swim3
*) ioremap(swim
->addrs
[0].address
, 0x200);
1076 fs
->dma
= (struct dbdma_regs
*) ioremap(swim
->addrs
[1].address
, 0x200);
1077 fs
->swim3_intr
= swim
->intrs
[0].line
;
1078 fs
->dma_intr
= swim
->intrs
[1].line
;
1080 fs
->cur_sector
= -1;
1082 fs
->secpertrack
= 18;
1083 fs
->total_secs
= 2880;
1084 fs
->media_bay
= mediabay
;
1085 init_waitqueue_head(&fs
->wait
);
1087 fs
->dma_cmd
= (struct dbdma_cmd
*) DBDMA_ALIGN(fs
->dbdma_cmd_space
);
1088 memset(fs
->dma_cmd
, 0, 2 * sizeof(struct dbdma_cmd
));
1089 st_le16(&fs
->dma_cmd
[1].command
, DBDMA_STOP
);
1091 if (request_irq(fs
->swim3_intr
, swim3_interrupt
, 0, "SWIM3", fs
)) {
1092 printk(KERN_ERR
"Couldn't get irq %d for SWIM3\n", fs
->swim3_intr
);
1093 feature_clear(swim
, FEATURE_SWIM3_enable
);
1097 if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
1098 printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
1100 feature_clear(swim, FEATURE_SWIM3_enable);
1105 init_timer(&fs
->timeout
);
1109 printk(KERN_INFO
"fd%d: SWIM3 floppy controller %s\n", floppy_count
,
1110 mediabay
? "in media bay" : "");