2 * Driver for SWIM (Sander Woz Integrated Machine) floppy controller
4 * Copyright (C) 2004,2008 Laurent Vivier <Laurent@lvivier.info>
6 * based on Alastair Bridgewater SWIM analysis, 2001
7 * based on SWIM3 driver (c) Paul Mackerras, 1996
8 * based on netBSD IWM driver (c) 1997, 1998 Hauke Fath.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * 2004-08-21 (lv) - Initial implementation
16 * 2008-10-30 (lv) - Port to 2.6
19 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/blk-mq.h>
23 #include <linux/mutex.h>
24 #include <linux/hdreg.h>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/platform_device.h>
29 #include <asm/mac_via.h>
31 #define CARDNAME "swim"
33 struct sector_header
{
40 } __attribute__((packed
));
42 #define DRIVER_VERSION "Version 0.2 (2008-10-30)"
44 #define REG(x) unsigned char x, x ## _pad[0x200 - 1];
64 } __attribute__((packed
));
66 #define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
67 #define swim_read(base, reg) in_8(&(base)->read_##reg)
88 } __attribute__((packed
));
90 #define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
91 #define iwm_read(base, reg) in_8(&(base)->reg)
93 /* bits in phase register */
95 #define SEEK_POSITIVE 0x070
96 #define SEEK_NEGATIVE 0x074
98 #define MOTOR_ON 0x072
99 #define MOTOR_OFF 0x076
108 #define CA_MASK 0x077
110 /* Select values for swim_select and swim_readbit */
112 #define READ_DATA_0 0x074
113 #define ONEMEG_DRIVE 0x075
114 #define SINGLE_SIDED 0x076
115 #define DRIVE_PRESENT 0x077
116 #define DISK_IN 0x170
117 #define WRITE_PROT 0x171
118 #define TRACK_ZERO 0x172
120 #define READ_DATA_1 0x174
121 #define GCR_MODE 0x175
122 #define SEEK_COMPLETE 0x176
123 #define TWOMEG_MEDIA 0x177
125 /* Bits in handshake register */
127 #define MARK_BYTE 0x01
128 #define CRC_ZERO 0x02
133 #define DAT2BYTE 0x40
134 #define DAT1BYTE 0x80
136 /* bits in setup register */
138 #define S_INV_WDATA 0x01
139 #define S_3_5_SELECT 0x02
141 #define S_FCLK_DIV2 0x08
142 #define S_ERROR_CORR 0x10
143 #define S_IBM_DRIVE 0x20
144 #define S_GCR_WRITE 0x40
145 #define S_TIMEOUT 0x80
147 /* bits in mode register */
153 #define WRITE_MODE 0x10
157 /*----------------------------------------------------------------------------*/
159 enum drive_location
{
160 INTERNAL_DRIVE
= 0x02,
161 EXTERNAL_DRIVE
= 0x04,
169 struct floppy_state
{
171 /* physical properties */
173 enum drive_location location
; /* internal or external drive */
174 int head_number
; /* single- or double-sided drive */
180 enum media_type type
;
187 /* in-use information */
192 struct gendisk
*disk
;
193 struct blk_mq_tag_set tag_set
;
195 /* parent controller */
197 struct swim_priv
*swd
;
210 #define FD_MAX_UNIT 2
213 struct swim __iomem
*base
;
216 struct floppy_state unit
[FD_MAX_UNIT
];
219 extern int swim_read_sector_header(struct swim __iomem
*base
,
220 struct sector_header
*header
);
221 extern int swim_read_sector_data(struct swim __iomem
*base
,
222 unsigned char *data
);
224 static DEFINE_MUTEX(swim_mutex
);
225 static inline void set_swim_mode(struct swim __iomem
*base
, int enable
)
227 struct iwm __iomem
*iwm_base
;
231 swim_write(base
, mode0
, 0xf8);
235 iwm_base
= (struct iwm __iomem
*)base
;
236 local_irq_save(flags
);
238 iwm_read(iwm_base
, q7L
);
239 iwm_read(iwm_base
, mtrOff
);
240 iwm_read(iwm_base
, q6H
);
242 iwm_write(iwm_base
, q7H
, 0x57);
243 iwm_write(iwm_base
, q7H
, 0x17);
244 iwm_write(iwm_base
, q7H
, 0x57);
245 iwm_write(iwm_base
, q7H
, 0x57);
247 local_irq_restore(flags
);
250 static inline int get_swim_mode(struct swim __iomem
*base
)
254 local_irq_save(flags
);
256 swim_write(base
, phase
, 0xf5);
257 if (swim_read(base
, phase
) != 0xf5)
259 swim_write(base
, phase
, 0xf6);
260 if (swim_read(base
, phase
) != 0xf6)
262 swim_write(base
, phase
, 0xf7);
263 if (swim_read(base
, phase
) != 0xf7)
265 local_irq_restore(flags
);
268 local_irq_restore(flags
);
272 static inline void swim_select(struct swim __iomem
*base
, int sel
)
274 swim_write(base
, phase
, RELAX
);
276 via1_set_head(sel
& 0x100);
278 swim_write(base
, phase
, sel
& CA_MASK
);
281 static inline void swim_action(struct swim __iomem
*base
, int action
)
285 local_irq_save(flags
);
287 swim_select(base
, action
);
289 swim_write(base
, phase
, (LSTRB
<<4) | LSTRB
);
291 swim_write(base
, phase
, (LSTRB
<<4) | ((~LSTRB
) & 0x0F));
294 local_irq_restore(flags
);
297 static inline int swim_readbit(struct swim __iomem
*base
, int bit
)
301 swim_select(base
, bit
);
305 stat
= swim_read(base
, handshake
);
307 return (stat
& SENSE
) == 0;
310 static inline void swim_drive(struct swim __iomem
*base
,
311 enum drive_location location
)
313 if (location
== INTERNAL_DRIVE
) {
314 swim_write(base
, mode0
, EXTERNAL_DRIVE
); /* clear drive 1 bit */
315 swim_write(base
, mode1
, INTERNAL_DRIVE
); /* set drive 0 bit */
316 } else if (location
== EXTERNAL_DRIVE
) {
317 swim_write(base
, mode0
, INTERNAL_DRIVE
); /* clear drive 0 bit */
318 swim_write(base
, mode1
, EXTERNAL_DRIVE
); /* set drive 1 bit */
322 static inline void swim_motor(struct swim __iomem
*base
,
323 enum motor_action action
)
328 swim_action(base
, MOTOR_ON
);
330 for (i
= 0; i
< 2*HZ
; i
++) {
331 swim_select(base
, RELAX
);
332 if (swim_readbit(base
, MOTOR_ON
))
334 current
->state
= TASK_INTERRUPTIBLE
;
337 } else if (action
== OFF
) {
338 swim_action(base
, MOTOR_OFF
);
339 swim_select(base
, RELAX
);
343 static inline void swim_eject(struct swim __iomem
*base
)
347 swim_action(base
, EJECT
);
349 for (i
= 0; i
< 2*HZ
; i
++) {
350 swim_select(base
, RELAX
);
351 if (!swim_readbit(base
, DISK_IN
))
353 current
->state
= TASK_INTERRUPTIBLE
;
356 swim_select(base
, RELAX
);
359 static inline void swim_head(struct swim __iomem
*base
, enum head head
)
361 /* wait drive is ready */
363 if (head
== UPPER_HEAD
)
364 swim_select(base
, READ_DATA_1
);
365 else if (head
== LOWER_HEAD
)
366 swim_select(base
, READ_DATA_0
);
369 static inline int swim_step(struct swim __iomem
*base
)
373 swim_action(base
, STEP
);
375 for (wait
= 0; wait
< HZ
; wait
++) {
377 current
->state
= TASK_INTERRUPTIBLE
;
380 swim_select(base
, RELAX
);
381 if (!swim_readbit(base
, STEP
))
387 static inline int swim_track00(struct swim __iomem
*base
)
391 swim_action(base
, SEEK_NEGATIVE
);
393 for (try = 0; try < 100; try++) {
395 swim_select(base
, RELAX
);
396 if (swim_readbit(base
, TRACK_ZERO
))
403 if (swim_readbit(base
, TRACK_ZERO
))
409 static inline int swim_seek(struct swim __iomem
*base
, int step
)
415 swim_action(base
, SEEK_NEGATIVE
);
418 swim_action(base
, SEEK_POSITIVE
);
420 for ( ; step
> 0; step
--) {
428 static inline int swim_track(struct floppy_state
*fs
, int track
)
430 struct swim __iomem
*base
= fs
->swd
->base
;
433 ret
= swim_seek(base
, track
- fs
->track
);
445 static int floppy_eject(struct floppy_state
*fs
)
447 struct swim __iomem
*base
= fs
->swd
->base
;
449 swim_drive(base
, fs
->location
);
450 swim_motor(base
, OFF
);
459 static inline int swim_read_sector(struct floppy_state
*fs
,
461 int sector
, unsigned char *buffer
)
463 struct swim __iomem
*base
= fs
->swd
->base
;
465 struct sector_header header
;
469 swim_track(fs
, track
);
471 swim_write(base
, mode1
, MOTON
);
472 swim_head(base
, side
);
473 swim_write(base
, mode0
, side
);
475 local_irq_save(flags
);
476 for (i
= 0; i
< 36; i
++) {
477 ret
= swim_read_sector_header(base
, &header
);
478 if (!ret
&& (header
.sector
== sector
)) {
481 ret
= swim_read_sector_data(base
, buffer
);
485 local_irq_restore(flags
);
487 swim_write(base
, mode0
, MOTON
);
489 if ((header
.side
!= side
) || (header
.track
!= track
) ||
490 (header
.sector
!= sector
))
496 static blk_status_t
floppy_read_sectors(struct floppy_state
*fs
,
497 int req_sector
, int sectors_nb
,
498 unsigned char *buffer
)
500 struct swim __iomem
*base
= fs
->swd
->base
;
502 int side
, track
, sector
;
506 swim_drive(base
, fs
->location
);
507 for (i
= req_sector
; i
< req_sector
+ sectors_nb
; i
++) {
509 track
= i
/ fs
->secpercyl
;
510 x
= i
% fs
->secpercyl
;
511 side
= x
/ fs
->secpertrack
;
512 sector
= x
% fs
->secpertrack
+ 1;
516 ret
= swim_read_sector(fs
, side
, track
, sector
,
519 return BLK_STS_IOERR
;
520 } while (ret
!= 512);
528 static blk_status_t
swim_queue_rq(struct blk_mq_hw_ctx
*hctx
,
529 const struct blk_mq_queue_data
*bd
)
531 struct floppy_state
*fs
= hctx
->queue
->queuedata
;
532 struct swim_priv
*swd
= fs
->swd
;
533 struct request
*req
= bd
->rq
;
536 if (!spin_trylock_irq(&swd
->lock
))
537 return BLK_STS_DEV_RESOURCE
;
539 blk_mq_start_request(req
);
541 if (!fs
->disk_in
|| rq_data_dir(req
) == WRITE
) {
547 err
= floppy_read_sectors(fs
, blk_rq_pos(req
),
548 blk_rq_cur_sectors(req
),
550 } while (blk_update_request(req
, err
, blk_rq_cur_bytes(req
)));
551 __blk_mq_end_request(req
, err
);
555 spin_unlock_irq(&swd
->lock
);
560 static struct floppy_struct floppy_type
[4] = {
561 { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL
}, /* no testing */
562 { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL
}, /* 360KB SS 3.5"*/
563 { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL
}, /* 720KB 3.5" */
564 { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL
}, /* 1.44MB 3.5" */
567 static int get_floppy_geometry(struct floppy_state
*fs
, int type
,
568 struct floppy_struct
**g
)
570 if (type
>= ARRAY_SIZE(floppy_type
))
574 *g
= &floppy_type
[type
];
575 else if (fs
->type
== HD_MEDIA
) /* High-Density media */
576 *g
= &floppy_type
[3];
577 else if (fs
->head_number
== 2) /* double-sided */
578 *g
= &floppy_type
[2];
580 *g
= &floppy_type
[1];
585 static void setup_medium(struct floppy_state
*fs
)
587 struct swim __iomem
*base
= fs
->swd
->base
;
589 if (swim_readbit(base
, DISK_IN
)) {
590 struct floppy_struct
*g
;
592 fs
->write_protected
= swim_readbit(base
, WRITE_PROT
);
594 if (swim_track00(base
))
596 "SWIM: cannot move floppy head to track 0\n");
600 fs
->type
= swim_readbit(base
, TWOMEG_MEDIA
) ?
602 fs
->head_number
= swim_readbit(base
, SINGLE_SIDED
) ? 1 : 2;
603 get_floppy_geometry(fs
, 0, &g
);
604 fs
->total_secs
= g
->size
;
605 fs
->secpercyl
= g
->head
* g
->sect
;
606 fs
->secpertrack
= g
->sect
;
613 static int floppy_open(struct block_device
*bdev
, fmode_t mode
)
615 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
616 struct swim __iomem
*base
= fs
->swd
->base
;
619 if (fs
->ref_count
== -1 || (fs
->ref_count
&& mode
& FMODE_EXCL
))
622 if (mode
& FMODE_EXCL
)
627 swim_write(base
, setup
, S_IBM_DRIVE
| S_FCLK_DIV2
);
629 swim_drive(base
, fs
->location
);
630 swim_motor(base
, ON
);
631 swim_action(base
, SETMFM
);
639 set_capacity(fs
->disk
, fs
->total_secs
);
641 if (mode
& FMODE_NDELAY
)
644 if (mode
& (FMODE_READ
|FMODE_WRITE
)) {
645 check_disk_change(bdev
);
646 if ((mode
& FMODE_WRITE
) && fs
->write_protected
) {
653 if (fs
->ref_count
< 0)
655 else if (fs
->ref_count
> 0)
658 if (fs
->ref_count
== 0)
659 swim_motor(base
, OFF
);
663 static int floppy_unlocked_open(struct block_device
*bdev
, fmode_t mode
)
667 mutex_lock(&swim_mutex
);
668 ret
= floppy_open(bdev
, mode
);
669 mutex_unlock(&swim_mutex
);
674 static void floppy_release(struct gendisk
*disk
, fmode_t mode
)
676 struct floppy_state
*fs
= disk
->private_data
;
677 struct swim __iomem
*base
= fs
->swd
->base
;
679 mutex_lock(&swim_mutex
);
680 if (fs
->ref_count
< 0)
682 else if (fs
->ref_count
> 0)
685 if (fs
->ref_count
== 0)
686 swim_motor(base
, OFF
);
687 mutex_unlock(&swim_mutex
);
690 static int floppy_ioctl(struct block_device
*bdev
, fmode_t mode
,
691 unsigned int cmd
, unsigned long param
)
693 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
696 if ((cmd
& 0x80) && !capable(CAP_SYS_ADMIN
))
701 if (fs
->ref_count
!= 1)
703 mutex_lock(&swim_mutex
);
704 err
= floppy_eject(fs
);
705 mutex_unlock(&swim_mutex
);
709 if (copy_to_user((void __user
*) param
, (void *) &floppy_type
,
710 sizeof(struct floppy_struct
)))
717 static int floppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
719 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
720 struct floppy_struct
*g
;
723 ret
= get_floppy_geometry(fs
, 0, &g
);
727 geo
->heads
= g
->head
;
728 geo
->sectors
= g
->sect
;
729 geo
->cylinders
= g
->track
;
734 static unsigned int floppy_check_events(struct gendisk
*disk
,
735 unsigned int clearing
)
737 struct floppy_state
*fs
= disk
->private_data
;
739 return fs
->ejected
? DISK_EVENT_MEDIA_CHANGE
: 0;
742 static int floppy_revalidate(struct gendisk
*disk
)
744 struct floppy_state
*fs
= disk
->private_data
;
745 struct swim __iomem
*base
= fs
->swd
->base
;
747 swim_drive(base
, fs
->location
);
753 swim_motor(base
, OFF
);
760 static const struct block_device_operations floppy_fops
= {
761 .owner
= THIS_MODULE
,
762 .open
= floppy_unlocked_open
,
763 .release
= floppy_release
,
764 .ioctl
= floppy_ioctl
,
765 .getgeo
= floppy_getgeo
,
766 .check_events
= floppy_check_events
,
767 .revalidate_disk
= floppy_revalidate
,
770 static struct kobject
*floppy_find(dev_t dev
, int *part
, void *data
)
772 struct swim_priv
*swd
= data
;
773 int drive
= (*part
& 3);
775 if (drive
>= swd
->floppy_count
)
779 return get_disk_and_module(swd
->unit
[drive
].disk
);
782 static int swim_add_floppy(struct swim_priv
*swd
, enum drive_location location
)
784 struct floppy_state
*fs
= &swd
->unit
[swd
->floppy_count
];
785 struct swim __iomem
*base
= swd
->base
;
787 fs
->location
= location
;
789 swim_drive(base
, location
);
791 swim_motor(base
, OFF
);
804 static const struct blk_mq_ops swim_mq_ops
= {
805 .queue_rq
= swim_queue_rq
,
808 static int swim_floppy_init(struct swim_priv
*swd
)
812 struct swim __iomem
*base
= swd
->base
;
814 /* scan floppy drives */
816 swim_drive(base
, INTERNAL_DRIVE
);
817 if (swim_readbit(base
, DRIVE_PRESENT
) &&
818 !swim_readbit(base
, ONEMEG_DRIVE
))
819 swim_add_floppy(swd
, INTERNAL_DRIVE
);
820 swim_drive(base
, EXTERNAL_DRIVE
);
821 if (swim_readbit(base
, DRIVE_PRESENT
) &&
822 !swim_readbit(base
, ONEMEG_DRIVE
))
823 swim_add_floppy(swd
, EXTERNAL_DRIVE
);
825 /* register floppy drives */
827 err
= register_blkdev(FLOPPY_MAJOR
, "fd");
829 printk(KERN_ERR
"Unable to get major %d for SWIM floppy\n",
834 spin_lock_init(&swd
->lock
);
836 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
837 struct request_queue
*q
;
839 swd
->unit
[drive
].disk
= alloc_disk(1);
840 if (swd
->unit
[drive
].disk
== NULL
) {
845 q
= blk_mq_init_sq_queue(&swd
->unit
[drive
].tag_set
, &swim_mq_ops
,
846 2, BLK_MQ_F_SHOULD_MERGE
);
852 swd
->unit
[drive
].disk
->queue
= q
;
853 blk_queue_bounce_limit(swd
->unit
[drive
].disk
->queue
,
855 swd
->unit
[drive
].disk
->queue
->queuedata
= &swd
->unit
[drive
];
856 swd
->unit
[drive
].swd
= swd
;
859 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
860 swd
->unit
[drive
].disk
->flags
= GENHD_FL_REMOVABLE
;
861 swd
->unit
[drive
].disk
->major
= FLOPPY_MAJOR
;
862 swd
->unit
[drive
].disk
->first_minor
= drive
;
863 sprintf(swd
->unit
[drive
].disk
->disk_name
, "fd%d", drive
);
864 swd
->unit
[drive
].disk
->fops
= &floppy_fops
;
865 swd
->unit
[drive
].disk
->private_data
= &swd
->unit
[drive
];
866 set_capacity(swd
->unit
[drive
].disk
, 2880);
867 add_disk(swd
->unit
[drive
].disk
);
870 blk_register_region(MKDEV(FLOPPY_MAJOR
, 0), 256, THIS_MODULE
,
871 floppy_find
, NULL
, swd
);
876 unregister_blkdev(FLOPPY_MAJOR
, "fd");
878 struct gendisk
*disk
= swd
->unit
[drive
].disk
;
882 blk_cleanup_queue(disk
->queue
);
885 blk_mq_free_tag_set(&swd
->unit
[drive
].tag_set
);
892 static int swim_probe(struct platform_device
*dev
)
894 struct resource
*res
;
895 struct swim __iomem
*swim_base
;
896 struct swim_priv
*swd
;
899 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
905 if (!request_mem_region(res
->start
, resource_size(res
), CARDNAME
)) {
910 swim_base
= (struct swim __iomem
*)res
->start
;
918 set_swim_mode(swim_base
, 1);
919 if (!get_swim_mode(swim_base
)) {
920 printk(KERN_INFO
"SWIM device not found !\n");
925 /* set platform driver data */
927 swd
= kzalloc(sizeof(struct swim_priv
), GFP_KERNEL
);
932 platform_set_drvdata(dev
, swd
);
934 swd
->base
= swim_base
;
936 ret
= swim_floppy_init(swd
);
945 release_mem_region(res
->start
, resource_size(res
));
950 static int swim_remove(struct platform_device
*dev
)
952 struct swim_priv
*swd
= platform_get_drvdata(dev
);
954 struct resource
*res
;
956 blk_unregister_region(MKDEV(FLOPPY_MAJOR
, 0), 256);
958 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
959 del_gendisk(swd
->unit
[drive
].disk
);
960 blk_cleanup_queue(swd
->unit
[drive
].disk
->queue
);
961 blk_mq_free_tag_set(&swd
->unit
[drive
].tag_set
);
962 put_disk(swd
->unit
[drive
].disk
);
965 unregister_blkdev(FLOPPY_MAJOR
, "fd");
969 for (drive
= 0; drive
< swd
->floppy_count
; drive
++)
970 floppy_eject(&swd
->unit
[drive
]);
972 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
974 release_mem_region(res
->start
, resource_size(res
));
981 static struct platform_driver swim_driver
= {
983 .remove
= swim_remove
,
989 static int __init
swim_init(void)
991 printk(KERN_INFO
"SWIM floppy driver %s\n", DRIVER_VERSION
);
993 return platform_driver_register(&swim_driver
);
995 module_init(swim_init
);
997 static void __exit
swim_exit(void)
999 platform_driver_unregister(&swim_driver
);
1001 module_exit(swim_exit
);
1003 MODULE_DESCRIPTION("Driver for SWIM floppy controller");
1004 MODULE_LICENSE("GPL");
1005 MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
1006 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR
);