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/mutex.h>
20 #include <linux/hdreg.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
25 #include <asm/mac_via.h>
27 #define CARDNAME "swim"
29 struct sector_header
{
36 } __attribute__((packed
));
38 #define DRIVER_VERSION "Version 0.2 (2008-10-30)"
40 #define REG(x) unsigned char x, x ## _pad[0x200 - 1];
60 } __attribute__((packed
));
62 #define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
63 #define swim_read(base, reg) in_8(&(base)->read_##reg)
84 } __attribute__((packed
));
86 #define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
87 #define iwm_read(base, reg) in_8(&(base)->reg)
89 /* bits in phase register */
91 #define SEEK_POSITIVE 0x070
92 #define SEEK_NEGATIVE 0x074
94 #define MOTOR_ON 0x072
95 #define MOTOR_OFF 0x076
104 #define CA_MASK 0x077
106 /* Select values for swim_select and swim_readbit */
108 #define READ_DATA_0 0x074
109 #define ONEMEG_DRIVE 0x075
110 #define SINGLE_SIDED 0x076
111 #define DRIVE_PRESENT 0x077
112 #define DISK_IN 0x170
113 #define WRITE_PROT 0x171
114 #define TRACK_ZERO 0x172
116 #define READ_DATA_1 0x174
117 #define GCR_MODE 0x175
118 #define SEEK_COMPLETE 0x176
119 #define TWOMEG_MEDIA 0x177
121 /* Bits in handshake register */
123 #define MARK_BYTE 0x01
124 #define CRC_ZERO 0x02
129 #define DAT2BYTE 0x40
130 #define DAT1BYTE 0x80
132 /* bits in setup register */
134 #define S_INV_WDATA 0x01
135 #define S_3_5_SELECT 0x02
137 #define S_FCLK_DIV2 0x08
138 #define S_ERROR_CORR 0x10
139 #define S_IBM_DRIVE 0x20
140 #define S_GCR_WRITE 0x40
141 #define S_TIMEOUT 0x80
143 /* bits in mode register */
149 #define WRITE_MODE 0x10
153 /*----------------------------------------------------------------------------*/
155 enum drive_location
{
156 INTERNAL_DRIVE
= 0x02,
157 EXTERNAL_DRIVE
= 0x04,
165 struct floppy_state
{
167 /* physical properties */
169 enum drive_location location
; /* internal or external drive */
170 int head_number
; /* single- or double-sided drive */
176 enum media_type type
;
183 /* in-use information */
188 struct gendisk
*disk
;
189 struct blk_mq_tag_set tag_set
;
191 /* parent controller */
193 struct swim_priv
*swd
;
206 #define FD_MAX_UNIT 2
209 struct swim __iomem
*base
;
212 struct floppy_state unit
[FD_MAX_UNIT
];
215 extern int swim_read_sector_header(struct swim __iomem
*base
,
216 struct sector_header
*header
);
217 extern int swim_read_sector_data(struct swim __iomem
*base
,
218 unsigned char *data
);
220 static DEFINE_MUTEX(swim_mutex
);
221 static inline void set_swim_mode(struct swim __iomem
*base
, int enable
)
223 struct iwm __iomem
*iwm_base
;
227 swim_write(base
, mode0
, 0xf8);
231 iwm_base
= (struct iwm __iomem
*)base
;
232 local_irq_save(flags
);
234 iwm_read(iwm_base
, q7L
);
235 iwm_read(iwm_base
, mtrOff
);
236 iwm_read(iwm_base
, q6H
);
238 iwm_write(iwm_base
, q7H
, 0x57);
239 iwm_write(iwm_base
, q7H
, 0x17);
240 iwm_write(iwm_base
, q7H
, 0x57);
241 iwm_write(iwm_base
, q7H
, 0x57);
243 local_irq_restore(flags
);
246 static inline int get_swim_mode(struct swim __iomem
*base
)
250 local_irq_save(flags
);
252 swim_write(base
, phase
, 0xf5);
253 if (swim_read(base
, phase
) != 0xf5)
255 swim_write(base
, phase
, 0xf6);
256 if (swim_read(base
, phase
) != 0xf6)
258 swim_write(base
, phase
, 0xf7);
259 if (swim_read(base
, phase
) != 0xf7)
261 local_irq_restore(flags
);
264 local_irq_restore(flags
);
268 static inline void swim_select(struct swim __iomem
*base
, int sel
)
270 swim_write(base
, phase
, RELAX
);
272 via1_set_head(sel
& 0x100);
274 swim_write(base
, phase
, sel
& CA_MASK
);
277 static inline void swim_action(struct swim __iomem
*base
, int action
)
281 local_irq_save(flags
);
283 swim_select(base
, action
);
285 swim_write(base
, phase
, (LSTRB
<<4) | LSTRB
);
287 swim_write(base
, phase
, (LSTRB
<<4) | ((~LSTRB
) & 0x0F));
290 local_irq_restore(flags
);
293 static inline int swim_readbit(struct swim __iomem
*base
, int bit
)
297 swim_select(base
, bit
);
301 stat
= swim_read(base
, handshake
);
303 return (stat
& SENSE
) == 0;
306 static inline void swim_drive(struct swim __iomem
*base
,
307 enum drive_location location
)
309 if (location
== INTERNAL_DRIVE
) {
310 swim_write(base
, mode0
, EXTERNAL_DRIVE
); /* clear drive 1 bit */
311 swim_write(base
, mode1
, INTERNAL_DRIVE
); /* set drive 0 bit */
312 } else if (location
== EXTERNAL_DRIVE
) {
313 swim_write(base
, mode0
, INTERNAL_DRIVE
); /* clear drive 0 bit */
314 swim_write(base
, mode1
, EXTERNAL_DRIVE
); /* set drive 1 bit */
318 static inline void swim_motor(struct swim __iomem
*base
,
319 enum motor_action action
)
324 swim_action(base
, MOTOR_ON
);
326 for (i
= 0; i
< 2*HZ
; i
++) {
327 swim_select(base
, RELAX
);
328 if (swim_readbit(base
, MOTOR_ON
))
330 current
->state
= TASK_INTERRUPTIBLE
;
333 } else if (action
== OFF
) {
334 swim_action(base
, MOTOR_OFF
);
335 swim_select(base
, RELAX
);
339 static inline void swim_eject(struct swim __iomem
*base
)
343 swim_action(base
, EJECT
);
345 for (i
= 0; i
< 2*HZ
; i
++) {
346 swim_select(base
, RELAX
);
347 if (!swim_readbit(base
, DISK_IN
))
349 current
->state
= TASK_INTERRUPTIBLE
;
352 swim_select(base
, RELAX
);
355 static inline void swim_head(struct swim __iomem
*base
, enum head head
)
357 /* wait drive is ready */
359 if (head
== UPPER_HEAD
)
360 swim_select(base
, READ_DATA_1
);
361 else if (head
== LOWER_HEAD
)
362 swim_select(base
, READ_DATA_0
);
365 static inline int swim_step(struct swim __iomem
*base
)
369 swim_action(base
, STEP
);
371 for (wait
= 0; wait
< HZ
; wait
++) {
373 current
->state
= TASK_INTERRUPTIBLE
;
376 swim_select(base
, RELAX
);
377 if (!swim_readbit(base
, STEP
))
383 static inline int swim_track00(struct swim __iomem
*base
)
387 swim_action(base
, SEEK_NEGATIVE
);
389 for (try = 0; try < 100; try++) {
391 swim_select(base
, RELAX
);
392 if (swim_readbit(base
, TRACK_ZERO
))
399 if (swim_readbit(base
, TRACK_ZERO
))
405 static inline int swim_seek(struct swim __iomem
*base
, int step
)
411 swim_action(base
, SEEK_NEGATIVE
);
414 swim_action(base
, SEEK_POSITIVE
);
416 for ( ; step
> 0; step
--) {
424 static inline int swim_track(struct floppy_state
*fs
, int track
)
426 struct swim __iomem
*base
= fs
->swd
->base
;
429 ret
= swim_seek(base
, track
- fs
->track
);
441 static int floppy_eject(struct floppy_state
*fs
)
443 struct swim __iomem
*base
= fs
->swd
->base
;
445 swim_drive(base
, fs
->location
);
446 swim_motor(base
, OFF
);
455 static inline int swim_read_sector(struct floppy_state
*fs
,
457 int sector
, unsigned char *buffer
)
459 struct swim __iomem
*base
= fs
->swd
->base
;
461 struct sector_header header
;
465 swim_track(fs
, track
);
467 swim_write(base
, mode1
, MOTON
);
468 swim_head(base
, side
);
469 swim_write(base
, mode0
, side
);
471 local_irq_save(flags
);
472 for (i
= 0; i
< 36; i
++) {
473 ret
= swim_read_sector_header(base
, &header
);
474 if (!ret
&& (header
.sector
== sector
)) {
477 ret
= swim_read_sector_data(base
, buffer
);
481 local_irq_restore(flags
);
483 swim_write(base
, mode0
, MOTON
);
485 if ((header
.side
!= side
) || (header
.track
!= track
) ||
486 (header
.sector
!= sector
))
492 static blk_status_t
floppy_read_sectors(struct floppy_state
*fs
,
493 int req_sector
, int sectors_nb
,
494 unsigned char *buffer
)
496 struct swim __iomem
*base
= fs
->swd
->base
;
498 int side
, track
, sector
;
502 swim_drive(base
, fs
->location
);
503 for (i
= req_sector
; i
< req_sector
+ sectors_nb
; i
++) {
505 track
= i
/ fs
->secpercyl
;
506 x
= i
% fs
->secpercyl
;
507 side
= x
/ fs
->secpertrack
;
508 sector
= x
% fs
->secpertrack
+ 1;
512 ret
= swim_read_sector(fs
, side
, track
, sector
,
515 return BLK_STS_IOERR
;
516 } while (ret
!= 512);
524 static blk_status_t
swim_queue_rq(struct blk_mq_hw_ctx
*hctx
,
525 const struct blk_mq_queue_data
*bd
)
527 struct floppy_state
*fs
= hctx
->queue
->queuedata
;
528 struct swim_priv
*swd
= fs
->swd
;
529 struct request
*req
= bd
->rq
;
532 if (!spin_trylock_irq(&swd
->lock
))
533 return BLK_STS_DEV_RESOURCE
;
535 blk_mq_start_request(req
);
537 if (!fs
->disk_in
|| rq_data_dir(req
) == WRITE
) {
543 err
= floppy_read_sectors(fs
, blk_rq_pos(req
),
544 blk_rq_cur_sectors(req
),
546 } while (blk_update_request(req
, err
, blk_rq_cur_bytes(req
)));
547 __blk_mq_end_request(req
, err
);
551 spin_unlock_irq(&swd
->lock
);
556 static struct floppy_struct floppy_type
[4] = {
557 { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL
}, /* no testing */
558 { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL
}, /* 360KB SS 3.5"*/
559 { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL
}, /* 720KB 3.5" */
560 { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL
}, /* 1.44MB 3.5" */
563 static int get_floppy_geometry(struct floppy_state
*fs
, int type
,
564 struct floppy_struct
**g
)
566 if (type
>= ARRAY_SIZE(floppy_type
))
570 *g
= &floppy_type
[type
];
571 else if (fs
->type
== HD_MEDIA
) /* High-Density media */
572 *g
= &floppy_type
[3];
573 else if (fs
->head_number
== 2) /* double-sided */
574 *g
= &floppy_type
[2];
576 *g
= &floppy_type
[1];
581 static void setup_medium(struct floppy_state
*fs
)
583 struct swim __iomem
*base
= fs
->swd
->base
;
585 if (swim_readbit(base
, DISK_IN
)) {
586 struct floppy_struct
*g
;
588 fs
->write_protected
= swim_readbit(base
, WRITE_PROT
);
590 if (swim_track00(base
))
592 "SWIM: cannot move floppy head to track 0\n");
596 fs
->type
= swim_readbit(base
, TWOMEG_MEDIA
) ?
598 fs
->head_number
= swim_readbit(base
, SINGLE_SIDED
) ? 1 : 2;
599 get_floppy_geometry(fs
, 0, &g
);
600 fs
->total_secs
= g
->size
;
601 fs
->secpercyl
= g
->head
* g
->sect
;
602 fs
->secpertrack
= g
->sect
;
609 static int floppy_open(struct block_device
*bdev
, fmode_t mode
)
611 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
612 struct swim __iomem
*base
= fs
->swd
->base
;
615 if (fs
->ref_count
== -1 || (fs
->ref_count
&& mode
& FMODE_EXCL
))
618 if (mode
& FMODE_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
& FMODE_NDELAY
)
640 if (mode
& (FMODE_READ
|FMODE_WRITE
)) {
641 check_disk_change(bdev
);
642 if ((mode
& FMODE_WRITE
) && fs
->write_protected
) {
649 if (fs
->ref_count
< 0)
651 else if (fs
->ref_count
> 0)
654 if (fs
->ref_count
== 0)
655 swim_motor(base
, OFF
);
659 static int floppy_unlocked_open(struct block_device
*bdev
, fmode_t mode
)
663 mutex_lock(&swim_mutex
);
664 ret
= floppy_open(bdev
, mode
);
665 mutex_unlock(&swim_mutex
);
670 static void floppy_release(struct gendisk
*disk
, fmode_t mode
)
672 struct floppy_state
*fs
= disk
->private_data
;
673 struct swim __iomem
*base
= fs
->swd
->base
;
675 mutex_lock(&swim_mutex
);
676 if (fs
->ref_count
< 0)
678 else if (fs
->ref_count
> 0)
681 if (fs
->ref_count
== 0)
682 swim_motor(base
, OFF
);
683 mutex_unlock(&swim_mutex
);
686 static int floppy_ioctl(struct block_device
*bdev
, fmode_t mode
,
687 unsigned int cmd
, unsigned long param
)
689 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
692 if ((cmd
& 0x80) && !capable(CAP_SYS_ADMIN
))
697 if (fs
->ref_count
!= 1)
699 mutex_lock(&swim_mutex
);
700 err
= floppy_eject(fs
);
701 mutex_unlock(&swim_mutex
);
705 if (copy_to_user((void __user
*) param
, (void *) &floppy_type
,
706 sizeof(struct floppy_struct
)))
713 static int floppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
715 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
716 struct floppy_struct
*g
;
719 ret
= get_floppy_geometry(fs
, 0, &g
);
723 geo
->heads
= g
->head
;
724 geo
->sectors
= g
->sect
;
725 geo
->cylinders
= g
->track
;
730 static unsigned int floppy_check_events(struct gendisk
*disk
,
731 unsigned int clearing
)
733 struct floppy_state
*fs
= disk
->private_data
;
735 return fs
->ejected
? DISK_EVENT_MEDIA_CHANGE
: 0;
738 static int floppy_revalidate(struct gendisk
*disk
)
740 struct floppy_state
*fs
= disk
->private_data
;
741 struct swim __iomem
*base
= fs
->swd
->base
;
743 swim_drive(base
, fs
->location
);
749 swim_motor(base
, OFF
);
756 static const struct block_device_operations floppy_fops
= {
757 .owner
= THIS_MODULE
,
758 .open
= floppy_unlocked_open
,
759 .release
= floppy_release
,
760 .ioctl
= floppy_ioctl
,
761 .getgeo
= floppy_getgeo
,
762 .check_events
= floppy_check_events
,
763 .revalidate_disk
= floppy_revalidate
,
766 static struct kobject
*floppy_find(dev_t dev
, int *part
, void *data
)
768 struct swim_priv
*swd
= data
;
769 int drive
= (*part
& 3);
771 if (drive
>= swd
->floppy_count
)
775 return get_disk_and_module(swd
->unit
[drive
].disk
);
778 static int swim_add_floppy(struct swim_priv
*swd
, enum drive_location location
)
780 struct floppy_state
*fs
= &swd
->unit
[swd
->floppy_count
];
781 struct swim __iomem
*base
= swd
->base
;
783 fs
->location
= location
;
785 swim_drive(base
, location
);
787 swim_motor(base
, OFF
);
800 static const struct blk_mq_ops swim_mq_ops
= {
801 .queue_rq
= swim_queue_rq
,
804 static int swim_floppy_init(struct swim_priv
*swd
)
808 struct swim __iomem
*base
= swd
->base
;
810 /* scan floppy drives */
812 swim_drive(base
, INTERNAL_DRIVE
);
813 if (swim_readbit(base
, DRIVE_PRESENT
) &&
814 !swim_readbit(base
, ONEMEG_DRIVE
))
815 swim_add_floppy(swd
, INTERNAL_DRIVE
);
816 swim_drive(base
, EXTERNAL_DRIVE
);
817 if (swim_readbit(base
, DRIVE_PRESENT
) &&
818 !swim_readbit(base
, ONEMEG_DRIVE
))
819 swim_add_floppy(swd
, EXTERNAL_DRIVE
);
821 /* register floppy drives */
823 err
= register_blkdev(FLOPPY_MAJOR
, "fd");
825 printk(KERN_ERR
"Unable to get major %d for SWIM floppy\n",
830 spin_lock_init(&swd
->lock
);
832 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
833 struct request_queue
*q
;
835 swd
->unit
[drive
].disk
= alloc_disk(1);
836 if (swd
->unit
[drive
].disk
== NULL
) {
841 q
= blk_mq_init_sq_queue(&swd
->unit
[drive
].tag_set
, &swim_mq_ops
,
842 2, BLK_MQ_F_SHOULD_MERGE
);
848 swd
->unit
[drive
].disk
->queue
= q
;
849 blk_queue_bounce_limit(swd
->unit
[drive
].disk
->queue
,
851 swd
->unit
[drive
].disk
->queue
->queuedata
= &swd
->unit
[drive
];
852 swd
->unit
[drive
].swd
= swd
;
855 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
856 swd
->unit
[drive
].disk
->flags
= GENHD_FL_REMOVABLE
;
857 swd
->unit
[drive
].disk
->major
= FLOPPY_MAJOR
;
858 swd
->unit
[drive
].disk
->first_minor
= drive
;
859 sprintf(swd
->unit
[drive
].disk
->disk_name
, "fd%d", drive
);
860 swd
->unit
[drive
].disk
->fops
= &floppy_fops
;
861 swd
->unit
[drive
].disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
862 swd
->unit
[drive
].disk
->private_data
= &swd
->unit
[drive
];
863 set_capacity(swd
->unit
[drive
].disk
, 2880);
864 add_disk(swd
->unit
[drive
].disk
);
867 blk_register_region(MKDEV(FLOPPY_MAJOR
, 0), 256, THIS_MODULE
,
868 floppy_find
, NULL
, swd
);
873 unregister_blkdev(FLOPPY_MAJOR
, "fd");
875 struct gendisk
*disk
= swd
->unit
[drive
].disk
;
879 blk_cleanup_queue(disk
->queue
);
882 blk_mq_free_tag_set(&swd
->unit
[drive
].tag_set
);
889 static int swim_probe(struct platform_device
*dev
)
891 struct resource
*res
;
892 struct swim __iomem
*swim_base
;
893 struct swim_priv
*swd
;
896 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
902 if (!request_mem_region(res
->start
, resource_size(res
), CARDNAME
)) {
907 swim_base
= (struct swim __iomem
*)res
->start
;
915 set_swim_mode(swim_base
, 1);
916 if (!get_swim_mode(swim_base
)) {
917 printk(KERN_INFO
"SWIM device not found !\n");
922 /* set platform driver data */
924 swd
= kzalloc(sizeof(struct swim_priv
), GFP_KERNEL
);
929 platform_set_drvdata(dev
, swd
);
931 swd
->base
= swim_base
;
933 ret
= swim_floppy_init(swd
);
942 release_mem_region(res
->start
, resource_size(res
));
947 static int swim_remove(struct platform_device
*dev
)
949 struct swim_priv
*swd
= platform_get_drvdata(dev
);
951 struct resource
*res
;
953 blk_unregister_region(MKDEV(FLOPPY_MAJOR
, 0), 256);
955 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
956 del_gendisk(swd
->unit
[drive
].disk
);
957 blk_cleanup_queue(swd
->unit
[drive
].disk
->queue
);
958 blk_mq_free_tag_set(&swd
->unit
[drive
].tag_set
);
959 put_disk(swd
->unit
[drive
].disk
);
962 unregister_blkdev(FLOPPY_MAJOR
, "fd");
966 for (drive
= 0; drive
< swd
->floppy_count
; drive
++)
967 floppy_eject(&swd
->unit
[drive
]);
969 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
971 release_mem_region(res
->start
, resource_size(res
));
978 static struct platform_driver swim_driver
= {
980 .remove
= swim_remove
,
986 static int __init
swim_init(void)
988 printk(KERN_INFO
"SWIM floppy driver %s\n", DRIVER_VERSION
);
990 return platform_driver_register(&swim_driver
);
992 module_init(swim_init
);
994 static void __exit
swim_exit(void)
996 platform_driver_unregister(&swim_driver
);
998 module_exit(swim_exit
);
1000 MODULE_DESCRIPTION("Driver for SWIM floppy controller");
1001 MODULE_LICENSE("GPL");
1002 MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
1003 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR
);