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 */
189 struct gendisk *disk;
190 struct blk_mq_tag_set tag_set;
192 /* parent controller */
194 struct swim_priv *swd;
207 #define FD_MAX_UNIT 2
210 struct swim __iomem *base;
213 struct floppy_state unit[FD_MAX_UNIT];
216 extern int swim_read_sector_header(struct swim __iomem *base,
217 struct sector_header *header);
218 extern int swim_read_sector_data(struct swim __iomem *base,
219 unsigned char *data);
221 static DEFINE_MUTEX(swim_mutex);
222 static inline void set_swim_mode(struct swim __iomem *base, int enable)
224 struct iwm __iomem *iwm_base;
228 swim_write(base, mode0, 0xf8);
232 iwm_base = (struct iwm __iomem *)base;
233 local_irq_save(flags);
235 iwm_read(iwm_base, q7L);
236 iwm_read(iwm_base, mtrOff);
237 iwm_read(iwm_base, q6H);
239 iwm_write(iwm_base, q7H, 0x57);
240 iwm_write(iwm_base, q7H, 0x17);
241 iwm_write(iwm_base, q7H, 0x57);
242 iwm_write(iwm_base, q7H, 0x57);
244 local_irq_restore(flags);
247 static inline int get_swim_mode(struct swim __iomem *base)
251 local_irq_save(flags);
253 swim_write(base, phase, 0xf5);
254 if (swim_read(base, phase) != 0xf5)
256 swim_write(base, phase, 0xf6);
257 if (swim_read(base, phase) != 0xf6)
259 swim_write(base, phase, 0xf7);
260 if (swim_read(base, phase) != 0xf7)
262 local_irq_restore(flags);
265 local_irq_restore(flags);
269 static inline void swim_select(struct swim __iomem *base, int sel)
271 swim_write(base, phase, RELAX);
273 via1_set_head(sel & 0x100);
275 swim_write(base, phase, sel & CA_MASK);
278 static inline void swim_action(struct swim __iomem *base, int action)
282 local_irq_save(flags);
284 swim_select(base, action);
286 swim_write(base, phase, (LSTRB<<4) | LSTRB);
288 swim_write(base, phase, (LSTRB<<4) | ((~LSTRB) & 0x0F));
291 local_irq_restore(flags);
294 static inline int swim_readbit(struct swim __iomem *base, int bit)
298 swim_select(base, bit);
302 stat = swim_read(base, handshake);
304 return (stat & SENSE) == 0;
307 static inline void swim_drive(struct swim __iomem *base,
308 enum drive_location location)
310 if (location == INTERNAL_DRIVE) {
311 swim_write(base, mode0, EXTERNAL_DRIVE); /* clear drive 1 bit */
312 swim_write(base, mode1, INTERNAL_DRIVE); /* set drive 0 bit */
313 } else if (location == EXTERNAL_DRIVE) {
314 swim_write(base, mode0, INTERNAL_DRIVE); /* clear drive 0 bit */
315 swim_write(base, mode1, EXTERNAL_DRIVE); /* set drive 1 bit */
319 static inline void swim_motor(struct swim __iomem *base,
320 enum motor_action action)
325 swim_action(base, MOTOR_ON);
327 for (i = 0; i < 2*HZ; i++) {
328 swim_select(base, RELAX);
329 if (swim_readbit(base, MOTOR_ON))
331 set_current_state(TASK_INTERRUPTIBLE);
334 } else if (action == OFF) {
335 swim_action(base, MOTOR_OFF);
336 swim_select(base, RELAX);
340 static inline void swim_eject(struct swim __iomem *base)
344 swim_action(base, EJECT);
346 for (i = 0; i < 2*HZ; i++) {
347 swim_select(base, RELAX);
348 if (!swim_readbit(base, DISK_IN))
350 set_current_state(TASK_INTERRUPTIBLE);
353 swim_select(base, RELAX);
356 static inline void swim_head(struct swim __iomem *base, enum head head)
358 /* wait drive is ready */
360 if (head == UPPER_HEAD)
361 swim_select(base, READ_DATA_1);
362 else if (head == LOWER_HEAD)
363 swim_select(base, READ_DATA_0);
366 static inline int swim_step(struct swim __iomem *base)
370 swim_action(base, STEP);
372 for (wait = 0; wait < HZ; wait++) {
374 set_current_state(TASK_INTERRUPTIBLE);
377 swim_select(base, RELAX);
378 if (!swim_readbit(base, STEP))
384 static inline int swim_track00(struct swim __iomem *base)
388 swim_action(base, SEEK_NEGATIVE);
390 for (try = 0; try < 100; try++) {
392 swim_select(base, RELAX);
393 if (swim_readbit(base, TRACK_ZERO))
400 if (swim_readbit(base, TRACK_ZERO))
406 static inline int swim_seek(struct swim __iomem *base, int step)
412 swim_action(base, SEEK_NEGATIVE);
415 swim_action(base, SEEK_POSITIVE);
417 for ( ; step > 0; step--) {
425 static inline int swim_track(struct floppy_state *fs, int track)
427 struct swim __iomem *base = fs->swd->base;
430 ret = swim_seek(base, track - fs->track);
442 static int floppy_eject(struct floppy_state *fs)
444 struct swim __iomem *base = fs->swd->base;
446 swim_drive(base, fs->location);
447 swim_motor(base, OFF);
456 static inline int swim_read_sector(struct floppy_state *fs,
458 int sector, unsigned char *buffer)
460 struct swim __iomem *base = fs->swd->base;
462 struct sector_header header;
466 swim_track(fs, track);
468 swim_write(base, mode1, MOTON);
469 swim_head(base, side);
470 swim_write(base, mode0, side);
472 local_irq_save(flags);
473 for (i = 0; i < 36; i++) {
474 ret = swim_read_sector_header(base, &header);
475 if (!ret && (header.sector == sector)) {
478 ret = swim_read_sector_data(base, buffer);
482 local_irq_restore(flags);
484 swim_write(base, mode0, MOTON);
486 if ((header.side != side) || (header.track != track) ||
487 (header.sector != sector))
493 static blk_status_t floppy_read_sectors(struct floppy_state *fs,
494 int req_sector, int sectors_nb,
495 unsigned char *buffer)
497 struct swim __iomem *base = fs->swd->base;
499 int side, track, sector;
503 swim_drive(base, fs->location);
504 for (i = req_sector; i < req_sector + sectors_nb; i++) {
506 track = i / fs->secpercyl;
507 x = i % fs->secpercyl;
508 side = x / fs->secpertrack;
509 sector = x % fs->secpertrack + 1;
513 ret = swim_read_sector(fs, side, track, sector,
516 return BLK_STS_IOERR;
517 } while (ret != 512);
525 static blk_status_t swim_queue_rq(struct blk_mq_hw_ctx *hctx,
526 const struct blk_mq_queue_data *bd)
528 struct floppy_state *fs = hctx->queue->queuedata;
529 struct swim_priv *swd = fs->swd;
530 struct request *req = bd->rq;
533 if (!spin_trylock_irq(&swd->lock))
534 return BLK_STS_DEV_RESOURCE;
536 blk_mq_start_request(req);
538 if (!fs->disk_in || rq_data_dir(req) == WRITE) {
544 err = floppy_read_sectors(fs, blk_rq_pos(req),
545 blk_rq_cur_sectors(req),
547 } while (blk_update_request(req, err, blk_rq_cur_bytes(req)));
548 __blk_mq_end_request(req, err);
552 spin_unlock_irq(&swd->lock);
557 static struct floppy_struct floppy_type[4] = {
558 { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL }, /* no testing */
559 { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 360KB SS 3.5"*/
560 { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 720KB 3.5" */
561 { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL }, /* 1.44MB 3.5" */
564 static int get_floppy_geometry(struct floppy_state *fs, int type,
565 struct floppy_struct **g)
567 if (type >= ARRAY_SIZE(floppy_type))
571 *g = &floppy_type[type];
572 else if (fs->type == HD_MEDIA) /* High-Density media */
573 *g = &floppy_type[3];
574 else if (fs->head_number == 2) /* double-sided */
575 *g = &floppy_type[2];
577 *g = &floppy_type[1];
582 static void setup_medium(struct floppy_state *fs)
584 struct swim __iomem *base = fs->swd->base;
586 if (swim_readbit(base, DISK_IN)) {
587 struct floppy_struct *g;
589 fs->write_protected = swim_readbit(base, WRITE_PROT);
591 if (swim_track00(base))
593 "SWIM: cannot move floppy head to track 0\n");
597 fs->type = swim_readbit(base, TWOMEG_MEDIA) ?
599 fs->head_number = swim_readbit(base, SINGLE_SIDED) ? 1 : 2;
600 get_floppy_geometry(fs, 0, &g);
601 fs->total_secs = g->size;
602 fs->secpercyl = g->head * g->sect;
603 fs->secpertrack = g->sect;
610 static int floppy_open(struct block_device *bdev, fmode_t mode)
612 struct floppy_state *fs = bdev->bd_disk->private_data;
613 struct swim __iomem *base = fs->swd->base;
616 if (fs->ref_count == -1 || (fs->ref_count && mode & FMODE_EXCL))
619 if (mode & FMODE_EXCL)
624 swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2);
626 swim_drive(base, fs->location);
627 swim_motor(base, ON);
628 swim_action(base, SETMFM);
636 set_capacity(fs->disk, fs->total_secs);
638 if (mode & FMODE_NDELAY)
641 if (mode & (FMODE_READ|FMODE_WRITE)) {
642 if (bdev_check_media_change(bdev) && fs->disk_in)
644 if ((mode & FMODE_WRITE) && fs->write_protected) {
651 if (fs->ref_count < 0)
653 else if (fs->ref_count > 0)
656 if (fs->ref_count == 0)
657 swim_motor(base, OFF);
661 static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
665 mutex_lock(&swim_mutex);
666 ret = floppy_open(bdev, mode);
667 mutex_unlock(&swim_mutex);
672 static void floppy_release(struct gendisk *disk, fmode_t mode)
674 struct floppy_state *fs = disk->private_data;
675 struct swim __iomem *base = fs->swd->base;
677 mutex_lock(&swim_mutex);
678 if (fs->ref_count < 0)
680 else if (fs->ref_count > 0)
683 if (fs->ref_count == 0)
684 swim_motor(base, OFF);
685 mutex_unlock(&swim_mutex);
688 static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
689 unsigned int cmd, unsigned long param)
691 struct floppy_state *fs = bdev->bd_disk->private_data;
694 if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
699 if (fs->ref_count != 1)
701 mutex_lock(&swim_mutex);
702 err = floppy_eject(fs);
703 mutex_unlock(&swim_mutex);
707 if (copy_to_user((void __user *) param, (void *) &floppy_type,
708 sizeof(struct floppy_struct)))
715 static int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
717 struct floppy_state *fs = bdev->bd_disk->private_data;
718 struct floppy_struct *g;
721 ret = get_floppy_geometry(fs, 0, &g);
725 geo->heads = g->head;
726 geo->sectors = g->sect;
727 geo->cylinders = g->track;
732 static unsigned int floppy_check_events(struct gendisk *disk,
733 unsigned int clearing)
735 struct floppy_state *fs = disk->private_data;
737 return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
740 static const struct block_device_operations floppy_fops = {
741 .owner = THIS_MODULE,
742 .open = floppy_unlocked_open,
743 .release = floppy_release,
744 .ioctl = floppy_ioctl,
745 .getgeo = floppy_getgeo,
746 .check_events = floppy_check_events,
749 static int swim_add_floppy(struct swim_priv *swd, enum drive_location location)
751 struct floppy_state *fs = &swd->unit[swd->floppy_count];
752 struct swim __iomem *base = swd->base;
754 fs->location = location;
756 swim_drive(base, location);
758 swim_motor(base, OFF);
771 static const struct blk_mq_ops swim_mq_ops = {
772 .queue_rq = swim_queue_rq,
775 static int swim_floppy_init(struct swim_priv *swd)
779 struct swim __iomem *base = swd->base;
781 /* scan floppy drives */
783 swim_drive(base, INTERNAL_DRIVE);
784 if (swim_readbit(base, DRIVE_PRESENT) &&
785 !swim_readbit(base, ONEMEG_DRIVE))
786 swim_add_floppy(swd, INTERNAL_DRIVE);
787 swim_drive(base, EXTERNAL_DRIVE);
788 if (swim_readbit(base, DRIVE_PRESENT) &&
789 !swim_readbit(base, ONEMEG_DRIVE))
790 swim_add_floppy(swd, EXTERNAL_DRIVE);
792 /* register floppy drives */
794 err = register_blkdev(FLOPPY_MAJOR, "fd");
796 printk(KERN_ERR "Unable to get major %d for SWIM floppy\n",
801 spin_lock_init(&swd->lock);
803 for (drive = 0; drive < swd->floppy_count; drive++) {
804 err = blk_mq_alloc_sq_tag_set(&swd->unit[drive].tag_set,
805 &swim_mq_ops, 2, BLK_MQ_F_SHOULD_MERGE);
809 swd->unit[drive].disk =
810 blk_mq_alloc_disk(&swd->unit[drive].tag_set,
812 if (IS_ERR(swd->unit[drive].disk)) {
813 blk_mq_free_tag_set(&swd->unit[drive].tag_set);
814 err = PTR_ERR(swd->unit[drive].disk);
818 swd->unit[drive].swd = swd;
821 for (drive = 0; drive < swd->floppy_count; drive++) {
822 swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE;
823 swd->unit[drive].disk->major = FLOPPY_MAJOR;
824 swd->unit[drive].disk->first_minor = drive;
825 swd->unit[drive].disk->minors = 1;
826 sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive);
827 swd->unit[drive].disk->fops = &floppy_fops;
828 swd->unit[drive].disk->events = DISK_EVENT_MEDIA_CHANGE;
829 swd->unit[drive].disk->private_data = &swd->unit[drive];
830 set_capacity(swd->unit[drive].disk, 2880);
831 add_disk(swd->unit[drive].disk);
837 unregister_blkdev(FLOPPY_MAJOR, "fd");
839 struct gendisk *disk = swd->unit[drive].disk;
843 blk_cleanup_disk(disk);
844 blk_mq_free_tag_set(&swd->unit[drive].tag_set);
849 static int swim_probe(struct platform_device *dev)
851 struct resource *res;
852 struct swim __iomem *swim_base;
853 struct swim_priv *swd;
856 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
862 if (!request_mem_region(res->start, resource_size(res), CARDNAME)) {
867 swim_base = (struct swim __iomem *)res->start;
875 set_swim_mode(swim_base, 1);
876 if (!get_swim_mode(swim_base)) {
877 printk(KERN_INFO "SWIM device not found !\n");
882 /* set platform driver data */
884 swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL);
889 platform_set_drvdata(dev, swd);
891 swd->base = swim_base;
893 ret = swim_floppy_init(swd);
902 release_mem_region(res->start, resource_size(res));
907 static int swim_remove(struct platform_device *dev)
909 struct swim_priv *swd = platform_get_drvdata(dev);
911 struct resource *res;
913 for (drive = 0; drive < swd->floppy_count; drive++) {
914 del_gendisk(swd->unit[drive].disk);
915 blk_cleanup_queue(swd->unit[drive].disk->queue);
916 blk_mq_free_tag_set(&swd->unit[drive].tag_set);
917 put_disk(swd->unit[drive].disk);
920 unregister_blkdev(FLOPPY_MAJOR, "fd");
924 for (drive = 0; drive < swd->floppy_count; drive++)
925 floppy_eject(&swd->unit[drive]);
927 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
929 release_mem_region(res->start, resource_size(res));
936 static struct platform_driver swim_driver = {
938 .remove = swim_remove,
944 static int __init swim_init(void)
946 printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION);
948 return platform_driver_register(&swim_driver);
950 module_init(swim_init);
952 static void __exit swim_exit(void)
954 platform_driver_unregister(&swim_driver);
956 module_exit(swim_exit);
958 MODULE_DESCRIPTION("Driver for SWIM floppy controller");
959 MODULE_LICENSE("GPL");
960 MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
961 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);