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>
27 #include <linux/blkdev.h>
28 #include <linux/devfs_fs_kernel.h>
29 #include <linux/interrupt.h>
30 #include <linux/module.h>
31 #include <linux/spinlock.h>
33 #include <asm/dbdma.h>
35 #include <asm/uaccess.h>
36 #include <asm/mediabay.h>
37 #include <asm/machdep.h>
38 #include <asm/pmac_feature.h>
40 static struct request_queue
*swim3_queue
;
41 static struct gendisk
*disks
[2];
42 static struct request
*fd_req
;
44 #define MAX_FLOPPIES 2
58 #define REG(x) unsigned char x; char x ## _pad[15];
61 * The names for these registers mostly represent speculation on my part.
62 * It will be interesting to see how close they are to the names Apple uses.
66 REG(timer
); /* counts down at 1MHz */
69 REG(select
); /* controls CA0, CA1, CA2 and LSTRB signals */
71 REG(control
); /* writing bits clears them */
72 REG(status
); /* writing bits sets them in control */
74 REG(nseek
); /* # tracks to seek */
75 REG(ctrack
); /* current track number */
76 REG(csect
); /* current sector number */
77 REG(gap3
); /* size of gap 3 in track format */
78 REG(sector
); /* sector # to read or write */
79 REG(nsect
); /* # sectors to read or write */
83 #define control_bic control
84 #define control_bis status
86 /* Bits in select register */
90 /* Bits in control register */
94 #define WRITE_SECTORS 0x10
95 #define DO_ACTION 0x08
96 #define DRIVE2_ENABLE 0x04
97 #define DRIVE_ENABLE 0x02
98 #define INTR_ENABLE 0x01
100 /* Bits in status register */
101 #define FIFO_1BYTE 0x80
102 #define FIFO_2BYTE 0x40
106 #define INTR_PENDING 0x02
107 #define MARK_BYTE 0x01
109 /* Bits in intr and intr_enable registers */
110 #define ERROR_INTR 0x20
111 #define DATA_CHANGED 0x10
112 #define TRANSFER_DONE 0x08
113 #define SEEN_SECTOR 0x04
114 #define SEEK_DONE 0x02
115 #define TIMER_DONE 0x01
117 /* Bits in error register */
118 #define ERR_DATA_CRC 0x80
119 #define ERR_ADDR_CRC 0x40
120 #define ERR_OVERRUN 0x04
121 #define ERR_UNDERRUN 0x01
123 /* Bits in setup register */
124 #define S_SW_RESET 0x80
125 #define S_GCR_WRITE 0x40
126 #define S_IBM_DRIVE 0x20
127 #define S_TEST_MODE 0x10
128 #define S_FCLK_DIV2 0x08
130 #define S_COPY_PROT 0x02
131 #define S_INV_WDATA 0x01
133 /* Select values for swim3_action */
134 #define SEEK_POSITIVE 0
135 #define SEEK_NEGATIVE 4
144 /* Select values for swim3_select and swim3_readbit */
148 #define RELAX 3 /* also eject in progress */
149 #define READ_DATA_0 4
150 #define TWOMEG_DRIVE 5
151 #define SINGLE_SIDED 6 /* drive or diskette is 4MB type? */
152 #define DRIVE_PRESENT 7
155 #define TRACK_ZERO 10
157 #define READ_DATA_1 12
159 #define SEEK_COMPLETE 14
160 #define ONEMEG_MEDIA 15
162 /* Definitions of values used in writing and formatting */
163 #define DATA_ESCAPE 0x99
164 #define GCR_SYNC_EXC 0x3f
165 #define GCR_SYNC_CONV 0x80
166 #define GCR_FIRST_MARK 0xd5
167 #define GCR_SECOND_MARK 0xaa
168 #define GCR_ADDR_MARK "\xd5\xaa\x00"
169 #define GCR_DATA_MARK "\xd5\xaa\x0b"
170 #define GCR_SLIP_BYTE "\x27\xaa"
171 #define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f"
173 #define DATA_99 "\x99\x99"
174 #define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
175 #define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
176 #define MFM_GAP_LEN 12
178 struct floppy_state
{
179 enum swim_state state
;
181 struct swim3 __iomem
*swim3
; /* hardware registers */
182 struct dbdma_regs __iomem
*dma
; /* DMA controller registers */
183 int swim3_intr
; /* interrupt number for SWIM3 */
184 int dma_intr
; /* interrupt number for DMA channel */
185 int cur_cyl
; /* cylinder head is on, or -1 */
186 int cur_sector
; /* last sector we saw go past */
187 int req_cyl
; /* the cylinder for the current r/w request */
188 int head
; /* head number ditto */
189 int req_sector
; /* sector number ditto */
190 int scount
; /* # sectors we're transferring at present */
193 int secpercyl
; /* disk geometry information */
196 int write_prot
; /* 1 if write-protected, 0 if not, -1 dunno */
197 struct dbdma_cmd
*dma_cmd
;
200 struct timer_list timeout
;
203 wait_queue_head_t wait
;
205 struct device_node
* media_bay
; /* NULL when not in bay */
206 char dbdma_cmd_space
[5 * sizeof(struct dbdma_cmd
)];
209 static struct floppy_state floppy_states
[MAX_FLOPPIES
];
210 static int floppy_count
= 0;
211 static DEFINE_SPINLOCK(swim3_lock
);
213 static unsigned short write_preamble
[] = {
214 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */
215 0, 0, 0, 0, 0, 0, /* sync field */
216 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */
217 0x990f /* no escape for 512 bytes */
220 static unsigned short write_postamble
[] = {
221 0x9904, /* insert CRC */
223 0x9908, /* stop writing */
227 static void swim3_select(struct floppy_state
*fs
, int sel
);
228 static void swim3_action(struct floppy_state
*fs
, int action
);
229 static int swim3_readbit(struct floppy_state
*fs
, int bit
);
230 static void do_fd_request(request_queue_t
* q
);
231 static void start_request(struct floppy_state
*fs
);
232 static void set_timeout(struct floppy_state
*fs
, int nticks
,
233 void (*proc
)(unsigned long));
234 static void scan_track(struct floppy_state
*fs
);
235 static void seek_track(struct floppy_state
*fs
, int n
);
236 static void init_dma(struct dbdma_cmd
*cp
, int cmd
, void *buf
, int count
);
237 static void setup_transfer(struct floppy_state
*fs
);
238 static void act(struct floppy_state
*fs
);
239 static void scan_timeout(unsigned long data
);
240 static void seek_timeout(unsigned long data
);
241 static void settle_timeout(unsigned long data
);
242 static void xfer_timeout(unsigned long data
);
243 static irqreturn_t
swim3_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
);
244 /*static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs);*/
245 static int grab_drive(struct floppy_state
*fs
, enum swim_state state
,
247 static void release_drive(struct floppy_state
*fs
);
248 static int fd_eject(struct floppy_state
*fs
);
249 static int floppy_ioctl(struct inode
*inode
, struct file
*filp
,
250 unsigned int cmd
, unsigned long param
);
251 static int floppy_open(struct inode
*inode
, struct file
*filp
);
252 static int floppy_release(struct inode
*inode
, struct file
*filp
);
253 static int floppy_check_change(struct gendisk
*disk
);
254 static int floppy_revalidate(struct gendisk
*disk
);
255 static int swim3_add_device(struct device_node
*swims
);
256 int swim3_init(void);
258 #ifndef CONFIG_PMAC_MEDIABAY
259 #define check_media_bay(which, what) 1
262 static void swim3_select(struct floppy_state
*fs
, int sel
)
264 struct swim3 __iomem
*sw
= fs
->swim3
;
266 out_8(&sw
->select
, RELAX
);
268 out_8(&sw
->control_bis
, SELECT
);
270 out_8(&sw
->control_bic
, SELECT
);
271 out_8(&sw
->select
, sel
& CA_MASK
);
274 static void swim3_action(struct floppy_state
*fs
, int action
)
276 struct swim3 __iomem
*sw
= fs
->swim3
;
278 swim3_select(fs
, action
);
280 out_8(&sw
->select
, sw
->select
| LSTRB
);
282 out_8(&sw
->select
, sw
->select
& ~LSTRB
);
286 static int swim3_readbit(struct floppy_state
*fs
, int bit
)
288 struct swim3 __iomem
*sw
= fs
->swim3
;
291 swim3_select(fs
, bit
);
293 stat
= in_8(&sw
->status
);
294 return (stat
& DATA
) == 0;
297 static void do_fd_request(request_queue_t
* q
)
300 for(i
=0;i
<floppy_count
;i
++)
302 #ifdef CONFIG_PMAC_MEDIABAY
303 if (floppy_states
[i
].media_bay
&&
304 check_media_bay(floppy_states
[i
].media_bay
, MB_FD
))
306 #endif /* CONFIG_PMAC_MEDIABAY */
307 start_request(&floppy_states
[i
]);
311 static void start_request(struct floppy_state
*fs
)
316 if (fs
->state
== idle
&& fs
->wanted
) {
317 fs
->state
= available
;
321 while (fs
->state
== idle
&& (req
= elv_next_request(swim3_queue
))) {
323 printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%ld buf=%p\n",
324 req
->rq_disk
->disk_name
, req
->cmd
,
325 (long)req
->sector
, req
->nr_sectors
, req
->buffer
);
326 printk(" rq_status=%d errors=%d current_nr_sectors=%ld\n",
327 req
->rq_status
, req
->errors
, req
->current_nr_sectors
);
330 if (req
->sector
< 0 || req
->sector
>= fs
->total_secs
) {
334 if (req
->current_nr_sectors
== 0) {
343 if (rq_data_dir(req
) == WRITE
) {
344 if (fs
->write_prot
< 0)
345 fs
->write_prot
= swim3_readbit(fs
, WRITE_PROT
);
346 if (fs
->write_prot
) {
352 /* Do not remove the cast. req->sector is now a sector_t and
353 * can be 64 bits, but it will never go past 32 bits for this
354 * driver anyway, so we can safely cast it down and not have
355 * to do a 64/32 division
357 fs
->req_cyl
= ((long)req
->sector
) / fs
->secpercyl
;
358 x
= ((long)req
->sector
) % fs
->secpercyl
;
359 fs
->head
= x
/ fs
->secpertrack
;
360 fs
->req_sector
= x
% fs
->secpertrack
+ 1;
362 fs
->state
= do_transfer
;
369 static void set_timeout(struct floppy_state
*fs
, int nticks
,
370 void (*proc
)(unsigned long))
374 spin_lock_irqsave(&fs
->lock
, flags
);
375 if (fs
->timeout_pending
)
376 del_timer(&fs
->timeout
);
377 fs
->timeout
.expires
= jiffies
+ nticks
;
378 fs
->timeout
.function
= proc
;
379 fs
->timeout
.data
= (unsigned long) fs
;
380 add_timer(&fs
->timeout
);
381 fs
->timeout_pending
= 1;
382 spin_unlock_irqrestore(&fs
->lock
, flags
);
385 static inline void scan_track(struct floppy_state
*fs
)
387 struct swim3 __iomem
*sw
= fs
->swim3
;
389 swim3_select(fs
, READ_DATA_0
);
390 in_8(&sw
->intr
); /* clear SEEN_SECTOR bit */
392 out_8(&sw
->intr_enable
, SEEN_SECTOR
);
393 out_8(&sw
->control_bis
, DO_ACTION
);
394 /* enable intr when track found */
395 set_timeout(fs
, HZ
, scan_timeout
); /* enable timeout */
398 static inline void seek_track(struct floppy_state
*fs
, int n
)
400 struct swim3 __iomem
*sw
= fs
->swim3
;
403 swim3_action(fs
, SEEK_POSITIVE
);
406 swim3_action(fs
, SEEK_NEGATIVE
);
409 fs
->expect_cyl
= (fs
->cur_cyl
>= 0)? fs
->cur_cyl
+ n
: -1;
410 swim3_select(fs
, STEP
);
412 /* enable intr when seek finished */
413 out_8(&sw
->intr_enable
, SEEK_DONE
);
414 out_8(&sw
->control_bis
, DO_SEEK
);
415 set_timeout(fs
, 3*HZ
, seek_timeout
); /* enable timeout */
419 static inline void init_dma(struct dbdma_cmd
*cp
, int cmd
,
420 void *buf
, int count
)
422 st_le16(&cp
->req_count
, count
);
423 st_le16(&cp
->command
, cmd
);
424 st_le32(&cp
->phy_addr
, virt_to_bus(buf
));
428 static inline void setup_transfer(struct floppy_state
*fs
)
431 struct swim3 __iomem
*sw
= fs
->swim3
;
432 struct dbdma_cmd
*cp
= fs
->dma_cmd
;
433 struct dbdma_regs __iomem
*dr
= fs
->dma
;
435 if (fd_req
->current_nr_sectors
<= 0) {
436 printk(KERN_ERR
"swim3: transfer 0 sectors?\n");
439 if (rq_data_dir(fd_req
) == WRITE
)
442 n
= fs
->secpertrack
- fs
->req_sector
+ 1;
443 if (n
> fd_req
->current_nr_sectors
)
444 n
= fd_req
->current_nr_sectors
;
447 swim3_select(fs
, fs
->head
? READ_DATA_1
: READ_DATA_0
);
448 out_8(&sw
->sector
, fs
->req_sector
);
449 out_8(&sw
->nsect
, n
);
451 out_le32(&dr
->cmdptr
, virt_to_bus(cp
));
452 if (rq_data_dir(fd_req
) == WRITE
) {
453 /* Set up 3 dma commands: write preamble, data, postamble */
454 init_dma(cp
, OUTPUT_MORE
, write_preamble
, sizeof(write_preamble
));
456 init_dma(cp
, OUTPUT_MORE
, fd_req
->buffer
, 512);
458 init_dma(cp
, OUTPUT_LAST
, write_postamble
, sizeof(write_postamble
));
460 init_dma(cp
, INPUT_LAST
, fd_req
->buffer
, n
* 512);
463 out_le16(&cp
->command
, DBDMA_STOP
);
464 out_8(&sw
->control_bic
, DO_ACTION
| WRITE_SECTORS
);
466 out_8(&sw
->control_bic
, DO_ACTION
| WRITE_SECTORS
);
467 if (rq_data_dir(fd_req
) == WRITE
)
468 out_8(&sw
->control_bis
, WRITE_SECTORS
);
470 out_le32(&dr
->control
, (RUN
<< 16) | RUN
);
471 /* enable intr when transfer complete */
472 out_8(&sw
->intr_enable
, TRANSFER_DONE
);
473 out_8(&sw
->control_bis
, DO_ACTION
);
474 set_timeout(fs
, 2*HZ
, xfer_timeout
); /* enable timeout */
477 static void act(struct floppy_state
*fs
)
482 return; /* XXX shouldn't get here */
485 if (swim3_readbit(fs
, TRACK_ZERO
)) {
487 if (fs
->req_cyl
== 0)
488 fs
->state
= do_transfer
;
497 if (fs
->cur_cyl
< 0) {
499 fs
->state
= locating
;
502 if (fs
->req_cyl
== fs
->cur_cyl
) {
503 printk("whoops, seeking 0\n");
504 fs
->state
= do_transfer
;
507 seek_track(fs
, fs
->req_cyl
- fs
->cur_cyl
);
511 /* check for SEEK_COMPLETE after 30ms */
512 fs
->settle_time
= (HZ
+ 32) / 33;
513 set_timeout(fs
, fs
->settle_time
, settle_timeout
);
517 if (fs
->cur_cyl
!= fs
->req_cyl
) {
518 if (fs
->retries
> 5) {
519 end_request(fd_req
, 0);
534 printk(KERN_ERR
"swim3: unknown state %d\n", fs
->state
);
540 static void scan_timeout(unsigned long data
)
542 struct floppy_state
*fs
= (struct floppy_state
*) data
;
543 struct swim3 __iomem
*sw
= fs
->swim3
;
545 fs
->timeout_pending
= 0;
546 out_8(&sw
->control_bic
, DO_ACTION
| WRITE_SECTORS
);
547 out_8(&sw
->select
, RELAX
);
548 out_8(&sw
->intr_enable
, 0);
550 if (fs
->retries
> 5) {
551 end_request(fd_req
, 0);
560 static void seek_timeout(unsigned long data
)
562 struct floppy_state
*fs
= (struct floppy_state
*) data
;
563 struct swim3 __iomem
*sw
= fs
->swim3
;
565 fs
->timeout_pending
= 0;
566 out_8(&sw
->control_bic
, DO_SEEK
);
567 out_8(&sw
->select
, RELAX
);
568 out_8(&sw
->intr_enable
, 0);
569 printk(KERN_ERR
"swim3: seek timeout\n");
570 end_request(fd_req
, 0);
575 static void settle_timeout(unsigned long data
)
577 struct floppy_state
*fs
= (struct floppy_state
*) data
;
578 struct swim3 __iomem
*sw
= fs
->swim3
;
580 fs
->timeout_pending
= 0;
581 if (swim3_readbit(fs
, SEEK_COMPLETE
)) {
582 out_8(&sw
->select
, RELAX
);
583 fs
->state
= locating
;
587 out_8(&sw
->select
, RELAX
);
588 if (fs
->settle_time
< 2*HZ
) {
590 set_timeout(fs
, 1, settle_timeout
);
593 printk(KERN_ERR
"swim3: seek settle timeout\n");
594 end_request(fd_req
, 0);
599 static void xfer_timeout(unsigned long data
)
601 struct floppy_state
*fs
= (struct floppy_state
*) data
;
602 struct swim3 __iomem
*sw
= fs
->swim3
;
603 struct dbdma_regs __iomem
*dr
= fs
->dma
;
604 struct dbdma_cmd
*cp
= fs
->dma_cmd
;
608 fs
->timeout_pending
= 0;
609 out_le32(&dr
->control
, RUN
<< 16);
610 /* We must wait a bit for dbdma to stop */
611 for (n
= 0; (in_le32(&dr
->status
) & ACTIVE
) && n
< 1000; n
++)
613 out_8(&sw
->intr_enable
, 0);
614 out_8(&sw
->control_bic
, WRITE_SECTORS
| DO_ACTION
);
615 out_8(&sw
->select
, RELAX
);
616 if (rq_data_dir(fd_req
) == WRITE
)
618 if (ld_le16(&cp
->xfer_status
) != 0)
619 s
= fs
->scount
- ((ld_le16(&cp
->res_count
) + 511) >> 9);
623 fd_req
->current_nr_sectors
-= s
;
624 printk(KERN_ERR
"swim3: timeout %sing sector %ld\n",
625 (rq_data_dir(fd_req
)==WRITE
? "writ": "read"), (long)fd_req
->sector
);
626 end_request(fd_req
, 0);
631 static irqreturn_t
swim3_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
633 struct floppy_state
*fs
= (struct floppy_state
*) dev_id
;
634 struct swim3 __iomem
*sw
= fs
->swim3
;
637 struct dbdma_regs __iomem
*dr
;
638 struct dbdma_cmd
*cp
;
640 intr
= in_8(&sw
->intr
);
641 err
= (intr
& ERROR_INTR
)? in_8(&sw
->error
): 0;
642 if ((intr
& ERROR_INTR
) && fs
->state
!= do_transfer
)
643 printk(KERN_ERR
"swim3_interrupt, state=%d, dir=%lx, intr=%x, err=%x\n",
644 fs
->state
, rq_data_dir(fd_req
), intr
, err
);
647 if (intr
& SEEN_SECTOR
) {
648 out_8(&sw
->control_bic
, DO_ACTION
| WRITE_SECTORS
);
649 out_8(&sw
->select
, RELAX
);
650 out_8(&sw
->intr_enable
, 0);
651 del_timer(&fs
->timeout
);
652 fs
->timeout_pending
= 0;
653 if (sw
->ctrack
== 0xff) {
654 printk(KERN_ERR
"swim3: seen sector but cyl=ff?\n");
656 if (fs
->retries
> 5) {
657 end_request(fd_req
, 0);
666 fs
->cur_cyl
= sw
->ctrack
;
667 fs
->cur_sector
= sw
->csect
;
668 if (fs
->expect_cyl
!= -1 && fs
->expect_cyl
!= fs
->cur_cyl
)
669 printk(KERN_ERR
"swim3: expected cyl %d, got %d\n",
670 fs
->expect_cyl
, fs
->cur_cyl
);
671 fs
->state
= do_transfer
;
677 if (sw
->nseek
== 0) {
678 out_8(&sw
->control_bic
, DO_SEEK
);
679 out_8(&sw
->select
, RELAX
);
680 out_8(&sw
->intr_enable
, 0);
681 del_timer(&fs
->timeout
);
682 fs
->timeout_pending
= 0;
683 if (fs
->state
== seeking
)
685 fs
->state
= settling
;
690 out_8(&sw
->intr_enable
, 0);
691 del_timer(&fs
->timeout
);
692 fs
->timeout_pending
= 0;
696 if ((intr
& (ERROR_INTR
| TRANSFER_DONE
)) == 0)
698 out_8(&sw
->intr_enable
, 0);
699 out_8(&sw
->control_bic
, WRITE_SECTORS
| DO_ACTION
);
700 out_8(&sw
->select
, RELAX
);
701 del_timer(&fs
->timeout
);
702 fs
->timeout_pending
= 0;
705 if (rq_data_dir(fd_req
) == WRITE
)
708 * Check that the main data transfer has finished.
709 * On writing, the swim3 sometimes doesn't use
710 * up all the bytes of the postamble, so we can still
711 * see DMA active here. That doesn't matter as long
712 * as all the sector data has been transferred.
714 if ((intr
& ERROR_INTR
) == 0 && cp
->xfer_status
== 0) {
715 /* wait a little while for DMA to complete */
716 for (n
= 0; n
< 100; ++n
) {
717 if (cp
->xfer_status
!= 0)
724 out_le32(&dr
->control
, (RUN
| PAUSE
) << 16);
725 stat
= ld_le16(&cp
->xfer_status
);
726 resid
= ld_le16(&cp
->res_count
);
727 if (intr
& ERROR_INTR
) {
728 n
= fs
->scount
- 1 - resid
/ 512;
731 fd_req
->current_nr_sectors
-= n
;
732 fd_req
->buffer
+= n
* 512;
735 if (fs
->retries
< 5) {
739 printk("swim3: error %sing block %ld (err=%x)\n",
740 rq_data_dir(fd_req
) == WRITE
? "writ": "read",
741 (long)fd_req
->sector
, err
);
742 end_request(fd_req
, 0);
746 if ((stat
& ACTIVE
) == 0 || resid
!= 0) {
747 /* musta been an error */
748 printk(KERN_ERR
"swim3: fd dma: stat=%x resid=%d\n", stat
, resid
);
749 printk(KERN_ERR
" state=%d, dir=%lx, intr=%x, err=%x\n",
750 fs
->state
, rq_data_dir(fd_req
), intr
, err
);
751 end_request(fd_req
, 0);
756 fd_req
->sector
+= fs
->scount
;
757 fd_req
->current_nr_sectors
-= fs
->scount
;
758 fd_req
->buffer
+= fs
->scount
* 512;
759 if (fd_req
->current_nr_sectors
<= 0) {
760 end_request(fd_req
, 1);
763 fs
->req_sector
+= fs
->scount
;
764 if (fs
->req_sector
> fs
->secpertrack
) {
765 fs
->req_sector
-= fs
->secpertrack
;
766 if (++fs
->head
> 1) {
774 if (fs
->state
== idle
)
778 printk(KERN_ERR
"swim3: don't know what to do in state %d\n", fs
->state
);
784 static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
789 static int grab_drive(struct floppy_state
*fs
, enum swim_state state
,
794 spin_lock_irqsave(&fs
->lock
, flags
);
795 if (fs
->state
!= idle
) {
797 while (fs
->state
!= available
) {
798 if (interruptible
&& signal_pending(current
)) {
800 spin_unlock_irqrestore(&fs
->lock
, flags
);
803 interruptible_sleep_on(&fs
->wait
);
808 spin_unlock_irqrestore(&fs
->lock
, flags
);
812 static void release_drive(struct floppy_state
*fs
)
816 spin_lock_irqsave(&fs
->lock
, flags
);
819 spin_unlock_irqrestore(&fs
->lock
, flags
);
822 static int fd_eject(struct floppy_state
*fs
)
826 err
= grab_drive(fs
, ejecting
, 1);
829 swim3_action(fs
, EJECT
);
830 for (n
= 20; n
> 0; --n
) {
831 if (signal_pending(current
)) {
835 swim3_select(fs
, RELAX
);
836 schedule_timeout_interruptible(1);
837 if (swim3_readbit(fs
, DISK_IN
) == 0)
840 swim3_select(fs
, RELAX
);
847 static struct floppy_struct floppy_type
=
848 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL
}; /* 7 1.44MB 3.5" */
850 static int floppy_ioctl(struct inode
*inode
, struct file
*filp
,
851 unsigned int cmd
, unsigned long param
)
853 struct floppy_state
*fs
= inode
->i_bdev
->bd_disk
->private_data
;
856 if ((cmd
& 0x80) && !capable(CAP_SYS_ADMIN
))
859 #ifdef CONFIG_PMAC_MEDIABAY
860 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
866 if (fs
->ref_count
!= 1)
871 if (copy_to_user((void __user
*) param
, &floppy_type
,
872 sizeof(struct floppy_struct
)))
879 static int floppy_open(struct inode
*inode
, struct file
*filp
)
881 struct floppy_state
*fs
= inode
->i_bdev
->bd_disk
->private_data
;
882 struct swim3 __iomem
*sw
= fs
->swim3
;
885 if (fs
->ref_count
== 0) {
886 #ifdef CONFIG_PMAC_MEDIABAY
887 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
890 out_8(&sw
->setup
, S_IBM_DRIVE
| S_FCLK_DIV2
);
891 out_8(&sw
->control_bic
, 0xff);
892 out_8(&sw
->mode
, 0x95);
894 out_8(&sw
->intr_enable
, 0);
895 out_8(&sw
->control_bis
, DRIVE_ENABLE
| INTR_ENABLE
);
896 swim3_action(fs
, MOTOR_ON
);
899 for (n
= 0; n
< 2 * HZ
; ++n
) {
900 if (n
>= HZ
/30 && swim3_readbit(fs
, SEEK_COMPLETE
))
902 if (signal_pending(current
)) {
906 swim3_select(fs
, RELAX
);
907 schedule_timeout_interruptible(1);
909 if (err
== 0 && (swim3_readbit(fs
, SEEK_COMPLETE
) == 0
910 || swim3_readbit(fs
, DISK_IN
) == 0))
912 swim3_action(fs
, SETMFM
);
913 swim3_select(fs
, RELAX
);
915 } else if (fs
->ref_count
== -1 || filp
->f_flags
& O_EXCL
)
918 if (err
== 0 && (filp
->f_flags
& O_NDELAY
) == 0
919 && (filp
->f_mode
& 3)) {
920 check_disk_change(inode
->i_bdev
);
925 if (err
== 0 && (filp
->f_mode
& 2)) {
926 if (fs
->write_prot
< 0)
927 fs
->write_prot
= swim3_readbit(fs
, WRITE_PROT
);
933 if (fs
->ref_count
== 0) {
934 swim3_action(fs
, MOTOR_OFF
);
935 out_8(&sw
->control_bic
, DRIVE_ENABLE
| INTR_ENABLE
);
936 swim3_select(fs
, RELAX
);
941 if (filp
->f_flags
& O_EXCL
)
949 static int floppy_release(struct inode
*inode
, struct file
*filp
)
951 struct floppy_state
*fs
= inode
->i_bdev
->bd_disk
->private_data
;
952 struct swim3 __iomem
*sw
= fs
->swim3
;
953 if (fs
->ref_count
> 0 && --fs
->ref_count
== 0) {
954 swim3_action(fs
, MOTOR_OFF
);
955 out_8(&sw
->control_bic
, 0xff);
956 swim3_select(fs
, RELAX
);
961 static int floppy_check_change(struct gendisk
*disk
)
963 struct floppy_state
*fs
= disk
->private_data
;
967 static int floppy_revalidate(struct gendisk
*disk
)
969 struct floppy_state
*fs
= disk
->private_data
;
970 struct swim3 __iomem
*sw
;
973 #ifdef CONFIG_PMAC_MEDIABAY
974 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
979 grab_drive(fs
, revalidating
, 0);
980 out_8(&sw
->intr_enable
, 0);
981 out_8(&sw
->control_bis
, DRIVE_ENABLE
);
982 swim3_action(fs
, MOTOR_ON
); /* necessary? */
986 for (n
= HZ
; n
> 0; --n
) {
987 if (swim3_readbit(fs
, SEEK_COMPLETE
))
989 if (signal_pending(current
))
991 swim3_select(fs
, RELAX
);
992 schedule_timeout_interruptible(1);
994 ret
= swim3_readbit(fs
, SEEK_COMPLETE
) == 0
995 || swim3_readbit(fs
, DISK_IN
) == 0;
997 swim3_action(fs
, MOTOR_OFF
);
1000 swim3_action(fs
, SETMFM
);
1002 swim3_select(fs
, RELAX
);
1008 static struct block_device_operations floppy_fops
= {
1009 .open
= floppy_open
,
1010 .release
= floppy_release
,
1011 .ioctl
= floppy_ioctl
,
1012 .media_changed
= floppy_check_change
,
1013 .revalidate_disk
= floppy_revalidate
,
1016 int swim3_init(void)
1018 struct device_node
*swim
;
1022 devfs_mk_dir("floppy");
1024 swim
= find_devices("floppy");
1025 while (swim
&& (floppy_count
< MAX_FLOPPIES
))
1027 swim3_add_device(swim
);
1031 swim
= find_devices("swim3");
1032 while (swim
&& (floppy_count
< MAX_FLOPPIES
))
1034 swim3_add_device(swim
);
1041 for (i
= 0; i
< floppy_count
; i
++) {
1042 disks
[i
] = alloc_disk(1);
1047 if (register_blkdev(FLOPPY_MAJOR
, "fd")) {
1052 swim3_queue
= blk_init_queue(do_fd_request
, &swim3_lock
);
1058 for (i
= 0; i
< floppy_count
; i
++) {
1059 struct gendisk
*disk
= disks
[i
];
1060 disk
->major
= FLOPPY_MAJOR
;
1061 disk
->first_minor
= i
;
1062 disk
->fops
= &floppy_fops
;
1063 disk
->private_data
= &floppy_states
[i
];
1064 disk
->queue
= swim3_queue
;
1065 disk
->flags
|= GENHD_FL_REMOVABLE
;
1066 sprintf(disk
->disk_name
, "fd%d", i
);
1067 sprintf(disk
->devfs_name
, "floppy/%d", i
);
1068 set_capacity(disk
, 2880);
1074 unregister_blkdev(FLOPPY_MAJOR
, "fd");
1078 /* shouldn't we do something with results of swim_add_device()? */
1082 static int swim3_add_device(struct device_node
*swim
)
1084 struct device_node
*mediabay
;
1085 struct floppy_state
*fs
= &floppy_states
[floppy_count
];
1086 struct resource res_reg
, res_dma
;
1088 if (of_address_to_resource(swim
, 0, &res_reg
) ||
1089 of_address_to_resource(swim
, 1, &res_dma
)) {
1090 printk(KERN_ERR
"swim3: Can't get addresses\n");
1093 if (request_mem_region(res_reg
.start
, res_reg
.end
- res_reg
.start
+ 1,
1094 " (reg)") == NULL
) {
1095 printk(KERN_ERR
"swim3: Can't request register space\n");
1098 if (request_mem_region(res_dma
.start
, res_dma
.end
- res_dma
.start
+ 1,
1099 " (dma)") == NULL
) {
1100 release_mem_region(res_reg
.start
,
1101 res_reg
.end
- res_reg
.start
+ 1);
1102 printk(KERN_ERR
"swim3: Can't request DMA space\n");
1106 if (swim
->n_intrs
< 2) {
1107 printk(KERN_INFO
"swim3: expecting 2 intrs (n_intrs:%d)\n",
1109 release_mem_region(res_reg
.start
,
1110 res_reg
.end
- res_reg
.start
+ 1);
1111 release_mem_region(res_dma
.start
,
1112 res_dma
.end
- res_dma
.start
+ 1);
1116 mediabay
= (strcasecmp(swim
->parent
->type
, "media-bay") == 0) ? swim
->parent
: NULL
;
1117 if (mediabay
== NULL
)
1118 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE
, swim
, 0, 1);
1120 memset(fs
, 0, sizeof(*fs
));
1121 spin_lock_init(&fs
->lock
);
1123 fs
->swim3
= (struct swim3 __iomem
*)ioremap(res_reg
.start
, 0x200);
1124 fs
->dma
= (struct dbdma_regs __iomem
*)ioremap(res_dma
.start
, 0x200);
1125 fs
->swim3_intr
= swim
->intrs
[0].line
;
1126 fs
->dma_intr
= swim
->intrs
[1].line
;
1128 fs
->cur_sector
= -1;
1130 fs
->secpertrack
= 18;
1131 fs
->total_secs
= 2880;
1132 fs
->media_bay
= mediabay
;
1133 init_waitqueue_head(&fs
->wait
);
1135 fs
->dma_cmd
= (struct dbdma_cmd
*) DBDMA_ALIGN(fs
->dbdma_cmd_space
);
1136 memset(fs
->dma_cmd
, 0, 2 * sizeof(struct dbdma_cmd
));
1137 st_le16(&fs
->dma_cmd
[1].command
, DBDMA_STOP
);
1139 if (request_irq(fs
->swim3_intr
, swim3_interrupt
, 0, "SWIM3", fs
)) {
1140 printk(KERN_ERR
"Couldn't get irq %d for SWIM3\n", fs
->swim3_intr
);
1141 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE
, swim
, 0, 0);
1145 if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
1146 printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
1148 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
1153 init_timer(&fs
->timeout
);
1155 printk(KERN_INFO
"fd%d: SWIM3 floppy controller %s\n", floppy_count
,
1156 mediabay
? "in media bay" : "");
1163 module_init(swim3_init
)
1165 MODULE_LICENSE("GPL");
1166 MODULE_AUTHOR("Paul Mackerras");
1167 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR
);