1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for SWIM (Sander Woz Integrated Machine) floppy controller
5 * Copyright (C) 2004,2008 Laurent Vivier <Laurent@lvivier.info>
7 * based on Alastair Bridgewater SWIM analysis, 2001
8 * based on SWIM3 driver (c) Paul Mackerras, 1996
9 * based on netBSD IWM driver (c) 1997, 1998 Hauke Fath.
11 * 2004-08-21 (lv) - Initial implementation
12 * 2008-10-30 (lv) - Port to 2.6
15 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/blk-mq.h>
19 #include <linux/major.h>
20 #include <linux/mutex.h>
21 #include <linux/hdreg.h>
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
26 #include <asm/mac_via.h>
28 #define CARDNAME "swim"
30 struct sector_header
{
37 } __attribute__((packed
));
39 #define DRIVER_VERSION "Version 0.2 (2008-10-30)"
41 #define REG(x) unsigned char x, x ## _pad[0x200 - 1];
61 } __attribute__((packed
));
63 #define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
64 #define swim_read(base, reg) in_8(&(base)->read_##reg)
85 } __attribute__((packed
));
87 #define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
88 #define iwm_read(base, reg) in_8(&(base)->reg)
90 /* bits in phase register */
92 #define SEEK_POSITIVE 0x070
93 #define SEEK_NEGATIVE 0x074
95 #define MOTOR_ON 0x072
96 #define MOTOR_OFF 0x076
105 #define CA_MASK 0x077
107 /* Select values for swim_select and swim_readbit */
109 #define READ_DATA_0 0x074
110 #define ONEMEG_DRIVE 0x075
111 #define SINGLE_SIDED 0x076
112 #define DRIVE_PRESENT 0x077
113 #define DISK_IN 0x170
114 #define WRITE_PROT 0x171
115 #define TRACK_ZERO 0x172
117 #define READ_DATA_1 0x174
118 #define GCR_MODE 0x175
119 #define SEEK_COMPLETE 0x176
120 #define TWOMEG_MEDIA 0x177
122 /* Bits in handshake register */
124 #define MARK_BYTE 0x01
125 #define CRC_ZERO 0x02
130 #define DAT2BYTE 0x40
131 #define DAT1BYTE 0x80
133 /* bits in setup register */
135 #define S_INV_WDATA 0x01
136 #define S_3_5_SELECT 0x02
138 #define S_FCLK_DIV2 0x08
139 #define S_ERROR_CORR 0x10
140 #define S_IBM_DRIVE 0x20
141 #define S_GCR_WRITE 0x40
142 #define S_TIMEOUT 0x80
144 /* bits in mode register */
150 #define WRITE_MODE 0x10
154 /*----------------------------------------------------------------------------*/
156 enum drive_location
{
157 INTERNAL_DRIVE
= 0x02,
158 EXTERNAL_DRIVE
= 0x04,
166 struct floppy_state
{
168 /* physical properties */
170 enum drive_location location
; /* internal or external drive */
171 int head_number
; /* single- or double-sided drive */
177 enum media_type type
;
184 /* in-use information */
190 struct gendisk
*disk
;
191 struct blk_mq_tag_set tag_set
;
193 /* parent controller */
195 struct swim_priv
*swd
;
208 #define FD_MAX_UNIT 2
211 struct swim __iomem
*base
;
214 struct floppy_state unit
[FD_MAX_UNIT
];
217 extern int swim_read_sector_header(struct swim __iomem
*base
,
218 struct sector_header
*header
);
219 extern int swim_read_sector_data(struct swim __iomem
*base
,
220 unsigned char *data
);
222 static DEFINE_MUTEX(swim_mutex
);
223 static inline void set_swim_mode(struct swim __iomem
*base
, int enable
)
225 struct iwm __iomem
*iwm_base
;
229 swim_write(base
, mode0
, 0xf8);
233 iwm_base
= (struct iwm __iomem
*)base
;
234 local_irq_save(flags
);
236 iwm_read(iwm_base
, q7L
);
237 iwm_read(iwm_base
, mtrOff
);
238 iwm_read(iwm_base
, q6H
);
240 iwm_write(iwm_base
, q7H
, 0x57);
241 iwm_write(iwm_base
, q7H
, 0x17);
242 iwm_write(iwm_base
, q7H
, 0x57);
243 iwm_write(iwm_base
, q7H
, 0x57);
245 local_irq_restore(flags
);
248 static inline int get_swim_mode(struct swim __iomem
*base
)
252 local_irq_save(flags
);
254 swim_write(base
, phase
, 0xf5);
255 if (swim_read(base
, phase
) != 0xf5)
257 swim_write(base
, phase
, 0xf6);
258 if (swim_read(base
, phase
) != 0xf6)
260 swim_write(base
, phase
, 0xf7);
261 if (swim_read(base
, phase
) != 0xf7)
263 local_irq_restore(flags
);
266 local_irq_restore(flags
);
270 static inline void swim_select(struct swim __iomem
*base
, int sel
)
272 swim_write(base
, phase
, RELAX
);
274 via1_set_head(sel
& 0x100);
276 swim_write(base
, phase
, sel
& CA_MASK
);
279 static inline void swim_action(struct swim __iomem
*base
, int action
)
283 local_irq_save(flags
);
285 swim_select(base
, action
);
287 swim_write(base
, phase
, (LSTRB
<<4) | LSTRB
);
289 swim_write(base
, phase
, (LSTRB
<<4) | ((~LSTRB
) & 0x0F));
292 local_irq_restore(flags
);
295 static inline int swim_readbit(struct swim __iomem
*base
, int bit
)
299 swim_select(base
, bit
);
303 stat
= swim_read(base
, handshake
);
305 return (stat
& SENSE
) == 0;
308 static inline void swim_drive(struct swim __iomem
*base
,
309 enum drive_location location
)
311 if (location
== INTERNAL_DRIVE
) {
312 swim_write(base
, mode0
, EXTERNAL_DRIVE
); /* clear drive 1 bit */
313 swim_write(base
, mode1
, INTERNAL_DRIVE
); /* set drive 0 bit */
314 } else if (location
== EXTERNAL_DRIVE
) {
315 swim_write(base
, mode0
, INTERNAL_DRIVE
); /* clear drive 0 bit */
316 swim_write(base
, mode1
, EXTERNAL_DRIVE
); /* set drive 1 bit */
320 static inline void swim_motor(struct swim __iomem
*base
,
321 enum motor_action action
)
326 swim_action(base
, MOTOR_ON
);
328 for (i
= 0; i
< 2*HZ
; i
++) {
329 swim_select(base
, RELAX
);
330 if (swim_readbit(base
, MOTOR_ON
))
332 set_current_state(TASK_INTERRUPTIBLE
);
335 } else if (action
== OFF
) {
336 swim_action(base
, MOTOR_OFF
);
337 swim_select(base
, RELAX
);
341 static inline void swim_eject(struct swim __iomem
*base
)
345 swim_action(base
, EJECT
);
347 for (i
= 0; i
< 2*HZ
; i
++) {
348 swim_select(base
, RELAX
);
349 if (!swim_readbit(base
, DISK_IN
))
351 set_current_state(TASK_INTERRUPTIBLE
);
354 swim_select(base
, RELAX
);
357 static inline void swim_head(struct swim __iomem
*base
, enum head head
)
359 /* wait drive is ready */
361 if (head
== UPPER_HEAD
)
362 swim_select(base
, READ_DATA_1
);
363 else if (head
== LOWER_HEAD
)
364 swim_select(base
, READ_DATA_0
);
367 static inline int swim_step(struct swim __iomem
*base
)
371 swim_action(base
, STEP
);
373 for (wait
= 0; wait
< HZ
; wait
++) {
375 set_current_state(TASK_INTERRUPTIBLE
);
378 swim_select(base
, RELAX
);
379 if (!swim_readbit(base
, STEP
))
385 static inline int swim_track00(struct swim __iomem
*base
)
389 swim_action(base
, SEEK_NEGATIVE
);
391 for (try = 0; try < 100; try++) {
393 swim_select(base
, RELAX
);
394 if (swim_readbit(base
, TRACK_ZERO
))
401 if (swim_readbit(base
, TRACK_ZERO
))
407 static inline int swim_seek(struct swim __iomem
*base
, int step
)
413 swim_action(base
, SEEK_NEGATIVE
);
416 swim_action(base
, SEEK_POSITIVE
);
418 for ( ; step
> 0; step
--) {
426 static inline int swim_track(struct floppy_state
*fs
, int track
)
428 struct swim __iomem
*base
= fs
->swd
->base
;
431 ret
= swim_seek(base
, track
- fs
->track
);
443 static int floppy_eject(struct floppy_state
*fs
)
445 struct swim __iomem
*base
= fs
->swd
->base
;
447 swim_drive(base
, fs
->location
);
448 swim_motor(base
, OFF
);
457 static inline int swim_read_sector(struct floppy_state
*fs
,
459 int sector
, unsigned char *buffer
)
461 struct swim __iomem
*base
= fs
->swd
->base
;
463 struct sector_header header
;
467 swim_track(fs
, track
);
469 swim_write(base
, mode1
, MOTON
);
470 swim_head(base
, side
);
471 swim_write(base
, mode0
, side
);
473 local_irq_save(flags
);
474 for (i
= 0; i
< 36; i
++) {
475 ret
= swim_read_sector_header(base
, &header
);
476 if (!ret
&& (header
.sector
== sector
)) {
479 ret
= swim_read_sector_data(base
, buffer
);
483 local_irq_restore(flags
);
485 swim_write(base
, mode0
, MOTON
);
487 if ((header
.side
!= side
) || (header
.track
!= track
) ||
488 (header
.sector
!= sector
))
494 static blk_status_t
floppy_read_sectors(struct floppy_state
*fs
,
495 int req_sector
, int sectors_nb
,
496 unsigned char *buffer
)
498 struct swim __iomem
*base
= fs
->swd
->base
;
500 int side
, track
, sector
;
504 swim_drive(base
, fs
->location
);
505 for (i
= req_sector
; i
< req_sector
+ sectors_nb
; i
++) {
507 track
= i
/ fs
->secpercyl
;
508 x
= i
% fs
->secpercyl
;
509 side
= x
/ fs
->secpertrack
;
510 sector
= x
% fs
->secpertrack
+ 1;
514 ret
= swim_read_sector(fs
, side
, track
, sector
,
517 return BLK_STS_IOERR
;
518 } while (ret
!= 512);
526 static blk_status_t
swim_queue_rq(struct blk_mq_hw_ctx
*hctx
,
527 const struct blk_mq_queue_data
*bd
)
529 struct floppy_state
*fs
= hctx
->queue
->queuedata
;
530 struct swim_priv
*swd
= fs
->swd
;
531 struct request
*req
= bd
->rq
;
534 if (!spin_trylock_irq(&swd
->lock
))
535 return BLK_STS_DEV_RESOURCE
;
537 blk_mq_start_request(req
);
539 if (!fs
->disk_in
|| rq_data_dir(req
) == WRITE
) {
545 err
= floppy_read_sectors(fs
, blk_rq_pos(req
),
546 blk_rq_cur_sectors(req
),
548 } while (blk_update_request(req
, err
, blk_rq_cur_bytes(req
)));
549 __blk_mq_end_request(req
, err
);
553 spin_unlock_irq(&swd
->lock
);
558 static struct floppy_struct floppy_type
[4] = {
559 { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL
}, /* no testing */
560 { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL
}, /* 360KB SS 3.5"*/
561 { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL
}, /* 720KB 3.5" */
562 { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL
}, /* 1.44MB 3.5" */
565 static int get_floppy_geometry(struct floppy_state
*fs
, int type
,
566 struct floppy_struct
**g
)
568 if (type
>= ARRAY_SIZE(floppy_type
))
572 *g
= &floppy_type
[type
];
573 else if (fs
->type
== HD_MEDIA
) /* High-Density media */
574 *g
= &floppy_type
[3];
575 else if (fs
->head_number
== 2) /* double-sided */
576 *g
= &floppy_type
[2];
578 *g
= &floppy_type
[1];
583 static void setup_medium(struct floppy_state
*fs
)
585 struct swim __iomem
*base
= fs
->swd
->base
;
587 if (swim_readbit(base
, DISK_IN
)) {
588 struct floppy_struct
*g
;
590 fs
->write_protected
= swim_readbit(base
, WRITE_PROT
);
592 if (swim_track00(base
))
594 "SWIM: cannot move floppy head to track 0\n");
598 fs
->type
= swim_readbit(base
, TWOMEG_MEDIA
) ?
600 fs
->head_number
= swim_readbit(base
, SINGLE_SIDED
) ? 1 : 2;
601 get_floppy_geometry(fs
, 0, &g
);
602 fs
->total_secs
= g
->size
;
603 fs
->secpercyl
= g
->head
* g
->sect
;
604 fs
->secpertrack
= g
->sect
;
611 static int floppy_open(struct gendisk
*disk
, blk_mode_t mode
)
613 struct floppy_state
*fs
= disk
->private_data
;
614 struct swim __iomem
*base
= fs
->swd
->base
;
617 if (fs
->ref_count
== -1 || (fs
->ref_count
&& mode
& BLK_OPEN_EXCL
))
619 if (mode
& BLK_OPEN_EXCL
)
623 swim_write(base
, setup
, S_IBM_DRIVE
| S_FCLK_DIV2
);
625 swim_drive(base
, fs
->location
);
626 swim_motor(base
, ON
);
627 swim_action(base
, SETMFM
);
635 set_capacity(fs
->disk
, fs
->total_secs
);
637 if (mode
& BLK_OPEN_NDELAY
)
640 if (mode
& (BLK_OPEN_READ
| BLK_OPEN_WRITE
)) {
641 if (disk_check_media_change(disk
) && fs
->disk_in
)
643 if ((mode
& BLK_OPEN_WRITE
) && fs
->write_protected
) {
650 if (fs
->ref_count
< 0)
652 else if (fs
->ref_count
> 0)
655 if (fs
->ref_count
== 0)
656 swim_motor(base
, OFF
);
660 static int floppy_unlocked_open(struct gendisk
*disk
, blk_mode_t mode
)
664 mutex_lock(&swim_mutex
);
665 ret
= floppy_open(disk
, mode
);
666 mutex_unlock(&swim_mutex
);
671 static void floppy_release(struct gendisk
*disk
)
673 struct floppy_state
*fs
= disk
->private_data
;
674 struct swim __iomem
*base
= fs
->swd
->base
;
676 mutex_lock(&swim_mutex
);
677 if (fs
->ref_count
< 0)
679 else if (fs
->ref_count
> 0)
682 if (fs
->ref_count
== 0)
683 swim_motor(base
, OFF
);
684 mutex_unlock(&swim_mutex
);
687 static int floppy_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
688 unsigned int cmd
, unsigned long param
)
690 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
693 if ((cmd
& 0x80) && !capable(CAP_SYS_ADMIN
))
698 if (fs
->ref_count
!= 1)
700 mutex_lock(&swim_mutex
);
701 err
= floppy_eject(fs
);
702 mutex_unlock(&swim_mutex
);
706 if (copy_to_user((void __user
*) param
, (void *) &floppy_type
,
707 sizeof(struct floppy_struct
)))
714 static int floppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
716 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
717 struct floppy_struct
*g
;
720 ret
= get_floppy_geometry(fs
, 0, &g
);
724 geo
->heads
= g
->head
;
725 geo
->sectors
= g
->sect
;
726 geo
->cylinders
= g
->track
;
731 static unsigned int floppy_check_events(struct gendisk
*disk
,
732 unsigned int clearing
)
734 struct floppy_state
*fs
= disk
->private_data
;
736 return fs
->ejected
? DISK_EVENT_MEDIA_CHANGE
: 0;
739 static const struct block_device_operations floppy_fops
= {
740 .owner
= THIS_MODULE
,
741 .open
= floppy_unlocked_open
,
742 .release
= floppy_release
,
743 .ioctl
= floppy_ioctl
,
744 .getgeo
= floppy_getgeo
,
745 .check_events
= floppy_check_events
,
748 static int swim_add_floppy(struct swim_priv
*swd
, enum drive_location location
)
750 struct floppy_state
*fs
= &swd
->unit
[swd
->floppy_count
];
751 struct swim __iomem
*base
= swd
->base
;
753 fs
->location
= location
;
755 swim_drive(base
, location
);
757 swim_motor(base
, OFF
);
770 static const struct blk_mq_ops swim_mq_ops
= {
771 .queue_rq
= swim_queue_rq
,
774 static void swim_cleanup_floppy_disk(struct floppy_state
*fs
)
776 struct gendisk
*disk
= fs
->disk
;
782 del_gendisk(fs
->disk
);
785 blk_mq_free_tag_set(&fs
->tag_set
);
788 static int swim_floppy_init(struct swim_priv
*swd
)
790 struct queue_limits lim
= {
791 .features
= BLK_FEAT_ROTATIONAL
,
795 struct swim __iomem
*base
= swd
->base
;
797 /* scan floppy drives */
799 swim_drive(base
, INTERNAL_DRIVE
);
800 if (swim_readbit(base
, DRIVE_PRESENT
) &&
801 !swim_readbit(base
, ONEMEG_DRIVE
))
802 swim_add_floppy(swd
, INTERNAL_DRIVE
);
803 swim_drive(base
, EXTERNAL_DRIVE
);
804 if (swim_readbit(base
, DRIVE_PRESENT
) &&
805 !swim_readbit(base
, ONEMEG_DRIVE
))
806 swim_add_floppy(swd
, EXTERNAL_DRIVE
);
808 /* register floppy drives */
810 err
= register_blkdev(FLOPPY_MAJOR
, "fd");
812 printk(KERN_ERR
"Unable to get major %d for SWIM floppy\n",
817 spin_lock_init(&swd
->lock
);
819 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
820 err
= blk_mq_alloc_sq_tag_set(&swd
->unit
[drive
].tag_set
,
821 &swim_mq_ops
, 2, BLK_MQ_F_SHOULD_MERGE
);
825 swd
->unit
[drive
].disk
=
826 blk_mq_alloc_disk(&swd
->unit
[drive
].tag_set
, &lim
,
828 if (IS_ERR(swd
->unit
[drive
].disk
)) {
829 blk_mq_free_tag_set(&swd
->unit
[drive
].tag_set
);
830 err
= PTR_ERR(swd
->unit
[drive
].disk
);
834 swd
->unit
[drive
].swd
= swd
;
837 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
838 swd
->unit
[drive
].disk
->flags
= GENHD_FL_REMOVABLE
;
839 swd
->unit
[drive
].disk
->major
= FLOPPY_MAJOR
;
840 swd
->unit
[drive
].disk
->first_minor
= drive
;
841 swd
->unit
[drive
].disk
->minors
= 1;
842 sprintf(swd
->unit
[drive
].disk
->disk_name
, "fd%d", drive
);
843 swd
->unit
[drive
].disk
->fops
= &floppy_fops
;
844 swd
->unit
[drive
].disk
->flags
|= GENHD_FL_NO_PART
;
845 swd
->unit
[drive
].disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
846 swd
->unit
[drive
].disk
->private_data
= &swd
->unit
[drive
];
847 set_capacity(swd
->unit
[drive
].disk
, 2880);
848 err
= add_disk(swd
->unit
[drive
].disk
);
851 swd
->unit
[drive
].registered
= true;
857 unregister_blkdev(FLOPPY_MAJOR
, "fd");
859 swim_cleanup_floppy_disk(&swd
->unit
[drive
]);
864 static int swim_probe(struct platform_device
*dev
)
866 struct resource
*res
;
867 struct swim __iomem
*swim_base
;
868 struct swim_priv
*swd
;
871 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
877 if (!request_mem_region(res
->start
, resource_size(res
), CARDNAME
)) {
882 swim_base
= (struct swim __iomem
*)res
->start
;
890 set_swim_mode(swim_base
, 1);
891 if (!get_swim_mode(swim_base
)) {
892 printk(KERN_INFO
"SWIM device not found !\n");
897 /* set platform driver data */
899 swd
= kzalloc(sizeof(struct swim_priv
), GFP_KERNEL
);
904 platform_set_drvdata(dev
, swd
);
906 swd
->base
= swim_base
;
908 ret
= swim_floppy_init(swd
);
917 release_mem_region(res
->start
, resource_size(res
));
922 static void swim_remove(struct platform_device
*dev
)
924 struct swim_priv
*swd
= platform_get_drvdata(dev
);
926 struct resource
*res
;
928 for (drive
= 0; drive
< swd
->floppy_count
; drive
++)
929 swim_cleanup_floppy_disk(&swd
->unit
[drive
]);
931 unregister_blkdev(FLOPPY_MAJOR
, "fd");
935 for (drive
= 0; drive
< swd
->floppy_count
; drive
++)
936 floppy_eject(&swd
->unit
[drive
]);
938 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
940 release_mem_region(res
->start
, resource_size(res
));
945 static struct platform_driver swim_driver
= {
947 .remove
= swim_remove
,
953 static int __init
swim_init(void)
955 printk(KERN_INFO
"SWIM floppy driver %s\n", DRIVER_VERSION
);
957 return platform_driver_register(&swim_driver
);
959 module_init(swim_init
);
961 static void __exit
swim_exit(void)
963 platform_driver_unregister(&swim_driver
);
965 module_exit(swim_exit
);
967 MODULE_DESCRIPTION("Driver for SWIM floppy controller");
968 MODULE_LICENSE("GPL");
969 MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
970 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR
);