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>
26 #include <linux/blkdev.h>
27 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/spinlock.h>
31 #include <asm/dbdma.h>
33 #include <asm/uaccess.h>
34 #include <asm/mediabay.h>
35 #include <asm/machdep.h>
36 #include <asm/pmac_feature.h>
38 static struct request_queue
*swim3_queue
;
39 static struct gendisk
*disks
[2];
40 static struct request
*fd_req
;
42 #define MAX_FLOPPIES 2
56 #define REG(x) unsigned char x; char x ## _pad[15];
59 * The names for these registers mostly represent speculation on my part.
60 * It will be interesting to see how close they are to the names Apple uses.
64 REG(timer
); /* counts down at 1MHz */
67 REG(select
); /* controls CA0, CA1, CA2 and LSTRB signals */
69 REG(control
); /* writing bits clears them */
70 REG(status
); /* writing bits sets them in control */
72 REG(nseek
); /* # tracks to seek */
73 REG(ctrack
); /* current track number */
74 REG(csect
); /* current sector number */
75 REG(gap3
); /* size of gap 3 in track format */
76 REG(sector
); /* sector # to read or write */
77 REG(nsect
); /* # sectors to read or write */
81 #define control_bic control
82 #define control_bis status
84 /* Bits in select register */
88 /* Bits in control register */
92 #define WRITE_SECTORS 0x10
93 #define DO_ACTION 0x08
94 #define DRIVE2_ENABLE 0x04
95 #define DRIVE_ENABLE 0x02
96 #define INTR_ENABLE 0x01
98 /* Bits in status register */
99 #define FIFO_1BYTE 0x80
100 #define FIFO_2BYTE 0x40
104 #define INTR_PENDING 0x02
105 #define MARK_BYTE 0x01
107 /* Bits in intr and intr_enable registers */
108 #define ERROR_INTR 0x20
109 #define DATA_CHANGED 0x10
110 #define TRANSFER_DONE 0x08
111 #define SEEN_SECTOR 0x04
112 #define SEEK_DONE 0x02
113 #define TIMER_DONE 0x01
115 /* Bits in error register */
116 #define ERR_DATA_CRC 0x80
117 #define ERR_ADDR_CRC 0x40
118 #define ERR_OVERRUN 0x04
119 #define ERR_UNDERRUN 0x01
121 /* Bits in setup register */
122 #define S_SW_RESET 0x80
123 #define S_GCR_WRITE 0x40
124 #define S_IBM_DRIVE 0x20
125 #define S_TEST_MODE 0x10
126 #define S_FCLK_DIV2 0x08
128 #define S_COPY_PROT 0x02
129 #define S_INV_WDATA 0x01
131 /* Select values for swim3_action */
132 #define SEEK_POSITIVE 0
133 #define SEEK_NEGATIVE 4
142 /* Select values for swim3_select and swim3_readbit */
146 #define RELAX 3 /* also eject in progress */
147 #define READ_DATA_0 4
148 #define TWOMEG_DRIVE 5
149 #define SINGLE_SIDED 6 /* drive or diskette is 4MB type? */
150 #define DRIVE_PRESENT 7
153 #define TRACK_ZERO 10
155 #define READ_DATA_1 12
157 #define SEEK_COMPLETE 14
158 #define ONEMEG_MEDIA 15
160 /* Definitions of values used in writing and formatting */
161 #define DATA_ESCAPE 0x99
162 #define GCR_SYNC_EXC 0x3f
163 #define GCR_SYNC_CONV 0x80
164 #define GCR_FIRST_MARK 0xd5
165 #define GCR_SECOND_MARK 0xaa
166 #define GCR_ADDR_MARK "\xd5\xaa\x00"
167 #define GCR_DATA_MARK "\xd5\xaa\x0b"
168 #define GCR_SLIP_BYTE "\x27\xaa"
169 #define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f"
171 #define DATA_99 "\x99\x99"
172 #define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
173 #define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
174 #define MFM_GAP_LEN 12
176 struct floppy_state
{
177 enum swim_state state
;
179 struct swim3 __iomem
*swim3
; /* hardware registers */
180 struct dbdma_regs __iomem
*dma
; /* DMA controller registers */
181 int swim3_intr
; /* interrupt number for SWIM3 */
182 int dma_intr
; /* interrupt number for DMA channel */
183 int cur_cyl
; /* cylinder head is on, or -1 */
184 int cur_sector
; /* last sector we saw go past */
185 int req_cyl
; /* the cylinder for the current r/w request */
186 int head
; /* head number ditto */
187 int req_sector
; /* sector number ditto */
188 int scount
; /* # sectors we're transferring at present */
191 int secpercyl
; /* disk geometry information */
194 int write_prot
; /* 1 if write-protected, 0 if not, -1 dunno */
195 struct dbdma_cmd
*dma_cmd
;
198 struct timer_list timeout
;
201 wait_queue_head_t wait
;
203 struct device_node
* media_bay
; /* NULL when not in bay */
204 char dbdma_cmd_space
[5 * sizeof(struct dbdma_cmd
)];
207 static struct floppy_state floppy_states
[MAX_FLOPPIES
];
208 static int floppy_count
= 0;
209 static DEFINE_SPINLOCK(swim3_lock
);
211 static unsigned short write_preamble
[] = {
212 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */
213 0, 0, 0, 0, 0, 0, /* sync field */
214 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */
215 0x990f /* no escape for 512 bytes */
218 static unsigned short write_postamble
[] = {
219 0x9904, /* insert CRC */
221 0x9908, /* stop writing */
225 static void swim3_select(struct floppy_state
*fs
, int sel
);
226 static void swim3_action(struct floppy_state
*fs
, int action
);
227 static int swim3_readbit(struct floppy_state
*fs
, int bit
);
228 static void do_fd_request(struct request_queue
* q
);
229 static void start_request(struct floppy_state
*fs
);
230 static void set_timeout(struct floppy_state
*fs
, int nticks
,
231 void (*proc
)(unsigned long));
232 static void scan_track(struct floppy_state
*fs
);
233 static void seek_track(struct floppy_state
*fs
, int n
);
234 static void init_dma(struct dbdma_cmd
*cp
, int cmd
, void *buf
, int count
);
235 static void setup_transfer(struct floppy_state
*fs
);
236 static void act(struct floppy_state
*fs
);
237 static void scan_timeout(unsigned long data
);
238 static void seek_timeout(unsigned long data
);
239 static void settle_timeout(unsigned long data
);
240 static void xfer_timeout(unsigned long data
);
241 static irqreturn_t
swim3_interrupt(int irq
, void *dev_id
);
242 /*static void fd_dma_interrupt(int irq, void *dev_id);*/
243 static int grab_drive(struct floppy_state
*fs
, enum swim_state state
,
245 static void release_drive(struct floppy_state
*fs
);
246 static int fd_eject(struct floppy_state
*fs
);
247 static int floppy_ioctl(struct inode
*inode
, struct file
*filp
,
248 unsigned int cmd
, unsigned long param
);
249 static int floppy_open(struct inode
*inode
, struct file
*filp
);
250 static int floppy_release(struct inode
*inode
, struct file
*filp
);
251 static int floppy_check_change(struct gendisk
*disk
);
252 static int floppy_revalidate(struct gendisk
*disk
);
254 static void swim3_select(struct floppy_state
*fs
, int sel
)
256 struct swim3 __iomem
*sw
= fs
->swim3
;
258 out_8(&sw
->select
, RELAX
);
260 out_8(&sw
->control_bis
, SELECT
);
262 out_8(&sw
->control_bic
, SELECT
);
263 out_8(&sw
->select
, sel
& CA_MASK
);
266 static void swim3_action(struct floppy_state
*fs
, int action
)
268 struct swim3 __iomem
*sw
= fs
->swim3
;
270 swim3_select(fs
, action
);
272 out_8(&sw
->select
, sw
->select
| LSTRB
);
274 out_8(&sw
->select
, sw
->select
& ~LSTRB
);
278 static int swim3_readbit(struct floppy_state
*fs
, int bit
)
280 struct swim3 __iomem
*sw
= fs
->swim3
;
283 swim3_select(fs
, bit
);
285 stat
= in_8(&sw
->status
);
286 return (stat
& DATA
) == 0;
289 static void do_fd_request(struct request_queue
* q
)
292 for(i
=0;i
<floppy_count
;i
++)
294 #ifdef CONFIG_PMAC_MEDIABAY
295 if (floppy_states
[i
].media_bay
&&
296 check_media_bay(floppy_states
[i
].media_bay
, MB_FD
))
298 #endif /* CONFIG_PMAC_MEDIABAY */
299 start_request(&floppy_states
[i
]);
303 static void start_request(struct floppy_state
*fs
)
308 if (fs
->state
== idle
&& fs
->wanted
) {
309 fs
->state
= available
;
313 while (fs
->state
== idle
&& (req
= elv_next_request(swim3_queue
))) {
315 printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%ld buf=%p\n",
316 req
->rq_disk
->disk_name
, req
->cmd
,
317 (long)req
->sector
, req
->nr_sectors
, req
->buffer
);
318 printk(" errors=%d current_nr_sectors=%ld\n",
319 req
->errors
, req
->current_nr_sectors
);
322 if (req
->sector
< 0 || req
->sector
>= fs
->total_secs
) {
326 if (req
->current_nr_sectors
== 0) {
335 if (rq_data_dir(req
) == WRITE
) {
336 if (fs
->write_prot
< 0)
337 fs
->write_prot
= swim3_readbit(fs
, WRITE_PROT
);
338 if (fs
->write_prot
) {
344 /* Do not remove the cast. req->sector is now a sector_t and
345 * can be 64 bits, but it will never go past 32 bits for this
346 * driver anyway, so we can safely cast it down and not have
347 * to do a 64/32 division
349 fs
->req_cyl
= ((long)req
->sector
) / fs
->secpercyl
;
350 x
= ((long)req
->sector
) % fs
->secpercyl
;
351 fs
->head
= x
/ fs
->secpertrack
;
352 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 spin_lock_irqsave(&fs
->lock
, flags
);
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 spin_unlock_irqrestore(&fs
->lock
, flags
);
377 static inline void scan_track(struct floppy_state
*fs
)
379 struct swim3 __iomem
*sw
= fs
->swim3
;
381 swim3_select(fs
, READ_DATA_0
);
382 in_8(&sw
->intr
); /* clear SEEN_SECTOR bit */
384 out_8(&sw
->intr_enable
, SEEN_SECTOR
);
385 out_8(&sw
->control_bis
, DO_ACTION
);
386 /* enable intr when track found */
387 set_timeout(fs
, HZ
, scan_timeout
); /* enable timeout */
390 static inline void seek_track(struct floppy_state
*fs
, int n
)
392 struct swim3 __iomem
*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
);
404 /* enable intr when seek finished */
405 out_8(&sw
->intr_enable
, SEEK_DONE
);
406 out_8(&sw
->control_bis
, DO_SEEK
);
407 set_timeout(fs
, 3*HZ
, seek_timeout
); /* enable timeout */
411 static inline void init_dma(struct dbdma_cmd
*cp
, int cmd
,
412 void *buf
, int count
)
414 st_le16(&cp
->req_count
, count
);
415 st_le16(&cp
->command
, cmd
);
416 st_le32(&cp
->phy_addr
, virt_to_bus(buf
));
420 static inline void setup_transfer(struct floppy_state
*fs
)
423 struct swim3 __iomem
*sw
= fs
->swim3
;
424 struct dbdma_cmd
*cp
= fs
->dma_cmd
;
425 struct dbdma_regs __iomem
*dr
= fs
->dma
;
427 if (fd_req
->current_nr_sectors
<= 0) {
428 printk(KERN_ERR
"swim3: transfer 0 sectors?\n");
431 if (rq_data_dir(fd_req
) == WRITE
)
434 n
= fs
->secpertrack
- fs
->req_sector
+ 1;
435 if (n
> fd_req
->current_nr_sectors
)
436 n
= fd_req
->current_nr_sectors
;
439 swim3_select(fs
, fs
->head
? READ_DATA_1
: READ_DATA_0
);
440 out_8(&sw
->sector
, fs
->req_sector
);
441 out_8(&sw
->nsect
, n
);
443 out_le32(&dr
->cmdptr
, virt_to_bus(cp
));
444 if (rq_data_dir(fd_req
) == WRITE
) {
445 /* Set up 3 dma commands: write preamble, data, postamble */
446 init_dma(cp
, OUTPUT_MORE
, write_preamble
, sizeof(write_preamble
));
448 init_dma(cp
, OUTPUT_MORE
, fd_req
->buffer
, 512);
450 init_dma(cp
, OUTPUT_LAST
, write_postamble
, sizeof(write_postamble
));
452 init_dma(cp
, INPUT_LAST
, fd_req
->buffer
, n
* 512);
455 out_le16(&cp
->command
, DBDMA_STOP
);
456 out_8(&sw
->control_bic
, DO_ACTION
| WRITE_SECTORS
);
458 out_8(&sw
->control_bic
, DO_ACTION
| WRITE_SECTORS
);
459 if (rq_data_dir(fd_req
) == WRITE
)
460 out_8(&sw
->control_bis
, WRITE_SECTORS
);
462 out_le32(&dr
->control
, (RUN
<< 16) | RUN
);
463 /* enable intr when transfer complete */
464 out_8(&sw
->intr_enable
, TRANSFER_DONE
);
465 out_8(&sw
->control_bis
, DO_ACTION
);
466 set_timeout(fs
, 2*HZ
, xfer_timeout
); /* enable timeout */
469 static void act(struct floppy_state
*fs
)
474 return; /* XXX shouldn't get here */
477 if (swim3_readbit(fs
, TRACK_ZERO
)) {
479 if (fs
->req_cyl
== 0)
480 fs
->state
= do_transfer
;
489 if (fs
->cur_cyl
< 0) {
491 fs
->state
= locating
;
494 if (fs
->req_cyl
== fs
->cur_cyl
) {
495 printk("whoops, seeking 0\n");
496 fs
->state
= do_transfer
;
499 seek_track(fs
, fs
->req_cyl
- fs
->cur_cyl
);
503 /* check for SEEK_COMPLETE after 30ms */
504 fs
->settle_time
= (HZ
+ 32) / 33;
505 set_timeout(fs
, fs
->settle_time
, settle_timeout
);
509 if (fs
->cur_cyl
!= fs
->req_cyl
) {
510 if (fs
->retries
> 5) {
511 end_request(fd_req
, 0);
526 printk(KERN_ERR
"swim3: unknown state %d\n", fs
->state
);
532 static void scan_timeout(unsigned long data
)
534 struct floppy_state
*fs
= (struct floppy_state
*) data
;
535 struct swim3 __iomem
*sw
= fs
->swim3
;
537 fs
->timeout_pending
= 0;
538 out_8(&sw
->control_bic
, DO_ACTION
| WRITE_SECTORS
);
539 out_8(&sw
->select
, RELAX
);
540 out_8(&sw
->intr_enable
, 0);
542 if (fs
->retries
> 5) {
543 end_request(fd_req
, 0);
552 static void seek_timeout(unsigned long data
)
554 struct floppy_state
*fs
= (struct floppy_state
*) data
;
555 struct swim3 __iomem
*sw
= fs
->swim3
;
557 fs
->timeout_pending
= 0;
558 out_8(&sw
->control_bic
, DO_SEEK
);
559 out_8(&sw
->select
, RELAX
);
560 out_8(&sw
->intr_enable
, 0);
561 printk(KERN_ERR
"swim3: seek timeout\n");
562 end_request(fd_req
, 0);
567 static void settle_timeout(unsigned long data
)
569 struct floppy_state
*fs
= (struct floppy_state
*) data
;
570 struct swim3 __iomem
*sw
= fs
->swim3
;
572 fs
->timeout_pending
= 0;
573 if (swim3_readbit(fs
, SEEK_COMPLETE
)) {
574 out_8(&sw
->select
, RELAX
);
575 fs
->state
= locating
;
579 out_8(&sw
->select
, RELAX
);
580 if (fs
->settle_time
< 2*HZ
) {
582 set_timeout(fs
, 1, settle_timeout
);
585 printk(KERN_ERR
"swim3: seek settle timeout\n");
586 end_request(fd_req
, 0);
591 static void xfer_timeout(unsigned long data
)
593 struct floppy_state
*fs
= (struct floppy_state
*) data
;
594 struct swim3 __iomem
*sw
= fs
->swim3
;
595 struct dbdma_regs __iomem
*dr
= fs
->dma
;
596 struct dbdma_cmd
*cp
= fs
->dma_cmd
;
600 fs
->timeout_pending
= 0;
601 out_le32(&dr
->control
, RUN
<< 16);
602 /* We must wait a bit for dbdma to stop */
603 for (n
= 0; (in_le32(&dr
->status
) & ACTIVE
) && n
< 1000; n
++)
605 out_8(&sw
->intr_enable
, 0);
606 out_8(&sw
->control_bic
, WRITE_SECTORS
| DO_ACTION
);
607 out_8(&sw
->select
, RELAX
);
608 if (rq_data_dir(fd_req
) == WRITE
)
610 if (ld_le16(&cp
->xfer_status
) != 0)
611 s
= fs
->scount
- ((ld_le16(&cp
->res_count
) + 511) >> 9);
615 fd_req
->current_nr_sectors
-= s
;
616 printk(KERN_ERR
"swim3: timeout %sing sector %ld\n",
617 (rq_data_dir(fd_req
)==WRITE
? "writ": "read"), (long)fd_req
->sector
);
618 end_request(fd_req
, 0);
623 static irqreturn_t
swim3_interrupt(int irq
, void *dev_id
)
625 struct floppy_state
*fs
= (struct floppy_state
*) dev_id
;
626 struct swim3 __iomem
*sw
= fs
->swim3
;
629 struct dbdma_regs __iomem
*dr
;
630 struct dbdma_cmd
*cp
;
632 intr
= in_8(&sw
->intr
);
633 err
= (intr
& ERROR_INTR
)? in_8(&sw
->error
): 0;
634 if ((intr
& ERROR_INTR
) && fs
->state
!= do_transfer
)
635 printk(KERN_ERR
"swim3_interrupt, state=%d, dir=%x, intr=%x, err=%x\n",
636 fs
->state
, rq_data_dir(fd_req
), intr
, err
);
639 if (intr
& SEEN_SECTOR
) {
640 out_8(&sw
->control_bic
, DO_ACTION
| WRITE_SECTORS
);
641 out_8(&sw
->select
, RELAX
);
642 out_8(&sw
->intr_enable
, 0);
643 del_timer(&fs
->timeout
);
644 fs
->timeout_pending
= 0;
645 if (sw
->ctrack
== 0xff) {
646 printk(KERN_ERR
"swim3: seen sector but cyl=ff?\n");
648 if (fs
->retries
> 5) {
649 end_request(fd_req
, 0);
658 fs
->cur_cyl
= sw
->ctrack
;
659 fs
->cur_sector
= sw
->csect
;
660 if (fs
->expect_cyl
!= -1 && fs
->expect_cyl
!= fs
->cur_cyl
)
661 printk(KERN_ERR
"swim3: expected cyl %d, got %d\n",
662 fs
->expect_cyl
, fs
->cur_cyl
);
663 fs
->state
= do_transfer
;
669 if (sw
->nseek
== 0) {
670 out_8(&sw
->control_bic
, DO_SEEK
);
671 out_8(&sw
->select
, RELAX
);
672 out_8(&sw
->intr_enable
, 0);
673 del_timer(&fs
->timeout
);
674 fs
->timeout_pending
= 0;
675 if (fs
->state
== seeking
)
677 fs
->state
= settling
;
682 out_8(&sw
->intr_enable
, 0);
683 del_timer(&fs
->timeout
);
684 fs
->timeout_pending
= 0;
688 if ((intr
& (ERROR_INTR
| TRANSFER_DONE
)) == 0)
690 out_8(&sw
->intr_enable
, 0);
691 out_8(&sw
->control_bic
, WRITE_SECTORS
| DO_ACTION
);
692 out_8(&sw
->select
, RELAX
);
693 del_timer(&fs
->timeout
);
694 fs
->timeout_pending
= 0;
697 if (rq_data_dir(fd_req
) == WRITE
)
700 * Check that the main data transfer has finished.
701 * On writing, the swim3 sometimes doesn't use
702 * up all the bytes of the postamble, so we can still
703 * see DMA active here. That doesn't matter as long
704 * as all the sector data has been transferred.
706 if ((intr
& ERROR_INTR
) == 0 && cp
->xfer_status
== 0) {
707 /* wait a little while for DMA to complete */
708 for (n
= 0; n
< 100; ++n
) {
709 if (cp
->xfer_status
!= 0)
716 out_le32(&dr
->control
, (RUN
| PAUSE
) << 16);
717 stat
= ld_le16(&cp
->xfer_status
);
718 resid
= ld_le16(&cp
->res_count
);
719 if (intr
& ERROR_INTR
) {
720 n
= fs
->scount
- 1 - resid
/ 512;
723 fd_req
->current_nr_sectors
-= n
;
724 fd_req
->buffer
+= n
* 512;
727 if (fs
->retries
< 5) {
731 printk("swim3: error %sing block %ld (err=%x)\n",
732 rq_data_dir(fd_req
) == WRITE
? "writ": "read",
733 (long)fd_req
->sector
, err
);
734 end_request(fd_req
, 0);
738 if ((stat
& ACTIVE
) == 0 || resid
!= 0) {
739 /* musta been an error */
740 printk(KERN_ERR
"swim3: fd dma: stat=%x resid=%d\n", stat
, resid
);
741 printk(KERN_ERR
" state=%d, dir=%x, intr=%x, err=%x\n",
742 fs
->state
, rq_data_dir(fd_req
), intr
, err
);
743 end_request(fd_req
, 0);
748 fd_req
->sector
+= fs
->scount
;
749 fd_req
->current_nr_sectors
-= fs
->scount
;
750 fd_req
->buffer
+= fs
->scount
* 512;
751 if (fd_req
->current_nr_sectors
<= 0) {
752 end_request(fd_req
, 1);
755 fs
->req_sector
+= fs
->scount
;
756 if (fs
->req_sector
> fs
->secpertrack
) {
757 fs
->req_sector
-= fs
->secpertrack
;
758 if (++fs
->head
> 1) {
766 if (fs
->state
== idle
)
770 printk(KERN_ERR
"swim3: don't know what to do in state %d\n", fs
->state
);
776 static void fd_dma_interrupt(int irq, void *dev_id)
781 static int grab_drive(struct floppy_state
*fs
, enum swim_state state
,
786 spin_lock_irqsave(&fs
->lock
, flags
);
787 if (fs
->state
!= idle
) {
789 while (fs
->state
!= available
) {
790 if (interruptible
&& signal_pending(current
)) {
792 spin_unlock_irqrestore(&fs
->lock
, flags
);
795 interruptible_sleep_on(&fs
->wait
);
800 spin_unlock_irqrestore(&fs
->lock
, flags
);
804 static void release_drive(struct floppy_state
*fs
)
808 spin_lock_irqsave(&fs
->lock
, flags
);
811 spin_unlock_irqrestore(&fs
->lock
, flags
);
814 static int fd_eject(struct floppy_state
*fs
)
818 err
= grab_drive(fs
, ejecting
, 1);
821 swim3_action(fs
, EJECT
);
822 for (n
= 20; n
> 0; --n
) {
823 if (signal_pending(current
)) {
827 swim3_select(fs
, RELAX
);
828 schedule_timeout_interruptible(1);
829 if (swim3_readbit(fs
, DISK_IN
) == 0)
832 swim3_select(fs
, RELAX
);
839 static struct floppy_struct floppy_type
=
840 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL
}; /* 7 1.44MB 3.5" */
842 static int floppy_ioctl(struct inode
*inode
, struct file
*filp
,
843 unsigned int cmd
, unsigned long param
)
845 struct floppy_state
*fs
= inode
->i_bdev
->bd_disk
->private_data
;
848 if ((cmd
& 0x80) && !capable(CAP_SYS_ADMIN
))
851 #ifdef CONFIG_PMAC_MEDIABAY
852 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
858 if (fs
->ref_count
!= 1)
863 if (copy_to_user((void __user
*) param
, &floppy_type
,
864 sizeof(struct floppy_struct
)))
871 static int floppy_open(struct inode
*inode
, struct file
*filp
)
873 struct floppy_state
*fs
= inode
->i_bdev
->bd_disk
->private_data
;
874 struct swim3 __iomem
*sw
= fs
->swim3
;
877 if (fs
->ref_count
== 0) {
878 #ifdef CONFIG_PMAC_MEDIABAY
879 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
882 out_8(&sw
->setup
, S_IBM_DRIVE
| S_FCLK_DIV2
);
883 out_8(&sw
->control_bic
, 0xff);
884 out_8(&sw
->mode
, 0x95);
886 out_8(&sw
->intr_enable
, 0);
887 out_8(&sw
->control_bis
, DRIVE_ENABLE
| INTR_ENABLE
);
888 swim3_action(fs
, MOTOR_ON
);
891 for (n
= 0; n
< 2 * HZ
; ++n
) {
892 if (n
>= HZ
/30 && swim3_readbit(fs
, SEEK_COMPLETE
))
894 if (signal_pending(current
)) {
898 swim3_select(fs
, RELAX
);
899 schedule_timeout_interruptible(1);
901 if (err
== 0 && (swim3_readbit(fs
, SEEK_COMPLETE
) == 0
902 || swim3_readbit(fs
, DISK_IN
) == 0))
904 swim3_action(fs
, SETMFM
);
905 swim3_select(fs
, RELAX
);
907 } else if (fs
->ref_count
== -1 || filp
->f_flags
& O_EXCL
)
910 if (err
== 0 && (filp
->f_flags
& O_NDELAY
) == 0
911 && (filp
->f_mode
& 3)) {
912 check_disk_change(inode
->i_bdev
);
917 if (err
== 0 && (filp
->f_mode
& 2)) {
918 if (fs
->write_prot
< 0)
919 fs
->write_prot
= swim3_readbit(fs
, WRITE_PROT
);
925 if (fs
->ref_count
== 0) {
926 swim3_action(fs
, MOTOR_OFF
);
927 out_8(&sw
->control_bic
, DRIVE_ENABLE
| INTR_ENABLE
);
928 swim3_select(fs
, RELAX
);
933 if (filp
->f_flags
& O_EXCL
)
941 static int floppy_release(struct inode
*inode
, struct file
*filp
)
943 struct floppy_state
*fs
= inode
->i_bdev
->bd_disk
->private_data
;
944 struct swim3 __iomem
*sw
= fs
->swim3
;
945 if (fs
->ref_count
> 0 && --fs
->ref_count
== 0) {
946 swim3_action(fs
, MOTOR_OFF
);
947 out_8(&sw
->control_bic
, 0xff);
948 swim3_select(fs
, RELAX
);
953 static int floppy_check_change(struct gendisk
*disk
)
955 struct floppy_state
*fs
= disk
->private_data
;
959 static int floppy_revalidate(struct gendisk
*disk
)
961 struct floppy_state
*fs
= disk
->private_data
;
962 struct swim3 __iomem
*sw
;
965 #ifdef CONFIG_PMAC_MEDIABAY
966 if (fs
->media_bay
&& check_media_bay(fs
->media_bay
, MB_FD
))
971 grab_drive(fs
, revalidating
, 0);
972 out_8(&sw
->intr_enable
, 0);
973 out_8(&sw
->control_bis
, DRIVE_ENABLE
);
974 swim3_action(fs
, MOTOR_ON
); /* necessary? */
978 for (n
= HZ
; n
> 0; --n
) {
979 if (swim3_readbit(fs
, SEEK_COMPLETE
))
981 if (signal_pending(current
))
983 swim3_select(fs
, RELAX
);
984 schedule_timeout_interruptible(1);
986 ret
= swim3_readbit(fs
, SEEK_COMPLETE
) == 0
987 || swim3_readbit(fs
, DISK_IN
) == 0;
989 swim3_action(fs
, MOTOR_OFF
);
992 swim3_action(fs
, SETMFM
);
994 swim3_select(fs
, RELAX
);
1000 static struct block_device_operations floppy_fops
= {
1001 .open
= floppy_open
,
1002 .release
= floppy_release
,
1003 .ioctl
= floppy_ioctl
,
1004 .media_changed
= floppy_check_change
,
1005 .revalidate_disk
= floppy_revalidate
,
1008 static int swim3_add_device(struct macio_dev
*mdev
, int index
)
1010 struct device_node
*swim
= mdev
->ofdev
.node
;
1011 struct device_node
*mediabay
;
1012 struct floppy_state
*fs
= &floppy_states
[index
];
1015 /* Check & Request resources */
1016 if (macio_resource_count(mdev
) < 2) {
1017 printk(KERN_WARNING
"ifd%d: no address for %s\n",
1018 index
, swim
->full_name
);
1021 if (macio_irq_count(mdev
) < 2) {
1022 printk(KERN_WARNING
"fd%d: no intrs for device %s\n",
1023 index
, swim
->full_name
);
1025 if (macio_request_resource(mdev
, 0, "swim3 (mmio)")) {
1026 printk(KERN_ERR
"fd%d: can't request mmio resource for %s\n",
1027 index
, swim
->full_name
);
1030 if (macio_request_resource(mdev
, 1, "swim3 (dma)")) {
1031 printk(KERN_ERR
"fd%d: can't request dma resource for %s\n",
1032 index
, swim
->full_name
);
1033 macio_release_resource(mdev
, 0);
1036 dev_set_drvdata(&mdev
->ofdev
.dev
, fs
);
1038 mediabay
= (strcasecmp(swim
->parent
->type
, "media-bay") == 0) ?
1039 swim
->parent
: NULL
;
1040 if (mediabay
== NULL
)
1041 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE
, swim
, 0, 1);
1043 memset(fs
, 0, sizeof(*fs
));
1044 spin_lock_init(&fs
->lock
);
1046 fs
->swim3
= (struct swim3 __iomem
*)
1047 ioremap(macio_resource_start(mdev
, 0), 0x200);
1048 if (fs
->swim3
== NULL
) {
1049 printk("fd%d: couldn't map registers for %s\n",
1050 index
, swim
->full_name
);
1054 fs
->dma
= (struct dbdma_regs __iomem
*)
1055 ioremap(macio_resource_start(mdev
, 1), 0x200);
1056 if (fs
->dma
== NULL
) {
1057 printk("fd%d: couldn't map DMA for %s\n",
1058 index
, swim
->full_name
);
1063 fs
->swim3_intr
= macio_irq(mdev
, 0);
1064 fs
->dma_intr
= macio_irq(mdev
, 1);;
1066 fs
->cur_sector
= -1;
1068 fs
->secpertrack
= 18;
1069 fs
->total_secs
= 2880;
1070 fs
->media_bay
= mediabay
;
1071 init_waitqueue_head(&fs
->wait
);
1073 fs
->dma_cmd
= (struct dbdma_cmd
*) DBDMA_ALIGN(fs
->dbdma_cmd_space
);
1074 memset(fs
->dma_cmd
, 0, 2 * sizeof(struct dbdma_cmd
));
1075 st_le16(&fs
->dma_cmd
[1].command
, DBDMA_STOP
);
1077 if (request_irq(fs
->swim3_intr
, swim3_interrupt
, 0, "SWIM3", fs
)) {
1078 printk(KERN_ERR
"fd%d: couldn't request irq %d for %s\n",
1079 index
, fs
->swim3_intr
, swim
->full_name
);
1080 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE
, swim
, 0, 0);
1085 if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
1086 printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
1092 init_timer(&fs
->timeout
);
1094 printk(KERN_INFO
"fd%d: SWIM3 floppy controller %s\n", floppy_count
,
1095 mediabay
? "in media bay" : "");
1104 macio_release_resource(mdev
, 0);
1105 macio_release_resource(mdev
, 1);
1110 static int __devinit
swim3_attach(struct macio_dev
*mdev
, const struct of_device_id
*match
)
1113 struct gendisk
*disk
;
1116 rc
= swim3_add_device(mdev
, floppy_count
);
1120 /* Now create the queue if not there yet */
1121 if (swim3_queue
== NULL
) {
1122 /* If we failed, there isn't much we can do as the driver is still
1123 * too dumb to remove the device, just bail out
1125 if (register_blkdev(FLOPPY_MAJOR
, "fd"))
1127 swim3_queue
= blk_init_queue(do_fd_request
, &swim3_lock
);
1128 if (swim3_queue
== NULL
) {
1129 unregister_blkdev(FLOPPY_MAJOR
, "fd");
1134 /* Now register that disk. Same comment about failure handling */
1136 disk
= disks
[i
] = alloc_disk(1);
1140 disk
->major
= FLOPPY_MAJOR
;
1141 disk
->first_minor
= i
;
1142 disk
->fops
= &floppy_fops
;
1143 disk
->private_data
= &floppy_states
[i
];
1144 disk
->queue
= swim3_queue
;
1145 disk
->flags
|= GENHD_FL_REMOVABLE
;
1146 sprintf(disk
->disk_name
, "fd%d", i
);
1147 set_capacity(disk
, 2880);
1153 static struct of_device_id swim3_match
[] =
1159 .compatible
= "ohare-swim3"
1162 .compatible
= "swim3"
1166 static struct macio_driver swim3_driver
=
1169 .match_table
= swim3_match
,
1170 .probe
= swim3_attach
,
1172 .suspend
= swim3_suspend
,
1173 .resume
= swim3_resume
,
1178 int swim3_init(void)
1180 macio_register_driver(&swim3_driver
);
1184 module_init(swim3_init
)
1186 MODULE_LICENSE("GPL");
1187 MODULE_AUTHOR("Paul Mackerras");
1188 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR
);