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 block_device *bdev, fmode_t mode)
613 struct floppy_state *fs = bdev->bd_disk->private_data;
614 struct swim __iomem *base = fs->swd->base;
617 if (fs->ref_count == -1 || (fs->ref_count && mode & FMODE_EXCL))
620 if (mode & FMODE_EXCL)
625 swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2);
627 swim_drive(base, fs->location);
628 swim_motor(base, ON);
629 swim_action(base, SETMFM);
637 set_capacity(fs->disk, fs->total_secs);
639 if (mode & FMODE_NDELAY)
642 if (mode & (FMODE_READ|FMODE_WRITE)) {
643 if (bdev_check_media_change(bdev) && fs->disk_in)
645 if ((mode & FMODE_WRITE) && fs->write_protected) {
652 if (fs->ref_count < 0)
654 else if (fs->ref_count > 0)
657 if (fs->ref_count == 0)
658 swim_motor(base, OFF);
662 static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
666 mutex_lock(&swim_mutex);
667 ret = floppy_open(bdev, mode);
668 mutex_unlock(&swim_mutex);
673 static void floppy_release(struct gendisk *disk, fmode_t mode)
675 struct floppy_state *fs = disk->private_data;
676 struct swim __iomem *base = fs->swd->base;
678 mutex_lock(&swim_mutex);
679 if (fs->ref_count < 0)
681 else if (fs->ref_count > 0)
684 if (fs->ref_count == 0)
685 swim_motor(base, OFF);
686 mutex_unlock(&swim_mutex);
689 static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
690 unsigned int cmd, unsigned long param)
692 struct floppy_state *fs = bdev->bd_disk->private_data;
695 if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
700 if (fs->ref_count != 1)
702 mutex_lock(&swim_mutex);
703 err = floppy_eject(fs);
704 mutex_unlock(&swim_mutex);
708 if (copy_to_user((void __user *) param, (void *) &floppy_type,
709 sizeof(struct floppy_struct)))
716 static int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
718 struct floppy_state *fs = bdev->bd_disk->private_data;
719 struct floppy_struct *g;
722 ret = get_floppy_geometry(fs, 0, &g);
726 geo->heads = g->head;
727 geo->sectors = g->sect;
728 geo->cylinders = g->track;
733 static unsigned int floppy_check_events(struct gendisk *disk,
734 unsigned int clearing)
736 struct floppy_state *fs = disk->private_data;
738 return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
741 static const struct block_device_operations floppy_fops = {
742 .owner = THIS_MODULE,
743 .open = floppy_unlocked_open,
744 .release = floppy_release,
745 .ioctl = floppy_ioctl,
746 .getgeo = floppy_getgeo,
747 .check_events = floppy_check_events,
750 static int swim_add_floppy(struct swim_priv *swd, enum drive_location location)
752 struct floppy_state *fs = &swd->unit[swd->floppy_count];
753 struct swim __iomem *base = swd->base;
755 fs->location = location;
757 swim_drive(base, location);
759 swim_motor(base, OFF);
772 static const struct blk_mq_ops swim_mq_ops = {
773 .queue_rq = swim_queue_rq,
776 static void swim_cleanup_floppy_disk(struct floppy_state *fs)
778 struct gendisk *disk = fs->disk;
784 del_gendisk(fs->disk);
787 blk_mq_free_tag_set(&fs->tag_set);
790 static int swim_floppy_init(struct swim_priv *swd)
794 struct swim __iomem *base = swd->base;
796 /* scan floppy drives */
798 swim_drive(base, INTERNAL_DRIVE);
799 if (swim_readbit(base, DRIVE_PRESENT) &&
800 !swim_readbit(base, ONEMEG_DRIVE))
801 swim_add_floppy(swd, INTERNAL_DRIVE);
802 swim_drive(base, EXTERNAL_DRIVE);
803 if (swim_readbit(base, DRIVE_PRESENT) &&
804 !swim_readbit(base, ONEMEG_DRIVE))
805 swim_add_floppy(swd, EXTERNAL_DRIVE);
807 /* register floppy drives */
809 err = register_blkdev(FLOPPY_MAJOR, "fd");
811 printk(KERN_ERR "Unable to get major %d for SWIM floppy\n",
816 spin_lock_init(&swd->lock);
818 for (drive = 0; drive < swd->floppy_count; drive++) {
819 err = blk_mq_alloc_sq_tag_set(&swd->unit[drive].tag_set,
820 &swim_mq_ops, 2, BLK_MQ_F_SHOULD_MERGE);
824 swd->unit[drive].disk =
825 blk_mq_alloc_disk(&swd->unit[drive].tag_set,
827 if (IS_ERR(swd->unit[drive].disk)) {
828 blk_mq_free_tag_set(&swd->unit[drive].tag_set);
829 err = PTR_ERR(swd->unit[drive].disk);
833 swd->unit[drive].swd = swd;
836 for (drive = 0; drive < swd->floppy_count; drive++) {
837 swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE;
838 swd->unit[drive].disk->major = FLOPPY_MAJOR;
839 swd->unit[drive].disk->first_minor = drive;
840 swd->unit[drive].disk->minors = 1;
841 sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive);
842 swd->unit[drive].disk->fops = &floppy_fops;
843 swd->unit[drive].disk->flags |= GENHD_FL_NO_PART;
844 swd->unit[drive].disk->events = DISK_EVENT_MEDIA_CHANGE;
845 swd->unit[drive].disk->private_data = &swd->unit[drive];
846 set_capacity(swd->unit[drive].disk, 2880);
847 err = add_disk(swd->unit[drive].disk);
850 swd->unit[drive].registered = true;
856 unregister_blkdev(FLOPPY_MAJOR, "fd");
858 swim_cleanup_floppy_disk(&swd->unit[drive]);
863 static int swim_probe(struct platform_device *dev)
865 struct resource *res;
866 struct swim __iomem *swim_base;
867 struct swim_priv *swd;
870 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
876 if (!request_mem_region(res->start, resource_size(res), CARDNAME)) {
881 swim_base = (struct swim __iomem *)res->start;
889 set_swim_mode(swim_base, 1);
890 if (!get_swim_mode(swim_base)) {
891 printk(KERN_INFO "SWIM device not found !\n");
896 /* set platform driver data */
898 swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL);
903 platform_set_drvdata(dev, swd);
905 swd->base = swim_base;
907 ret = swim_floppy_init(swd);
916 release_mem_region(res->start, resource_size(res));
921 static int swim_remove(struct platform_device *dev)
923 struct swim_priv *swd = platform_get_drvdata(dev);
925 struct resource *res;
927 for (drive = 0; drive < swd->floppy_count; drive++)
928 swim_cleanup_floppy_disk(&swd->unit[drive]);
930 unregister_blkdev(FLOPPY_MAJOR, "fd");
934 for (drive = 0; drive < swd->floppy_count; drive++)
935 floppy_eject(&swd->unit[drive]);
937 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
939 release_mem_region(res->start, resource_size(res));
946 static struct platform_driver swim_driver = {
948 .remove = swim_remove,
954 static int __init swim_init(void)
956 printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION);
958 return platform_driver_register(&swim_driver);
960 module_init(swim_init);
962 static void __exit swim_exit(void)
964 platform_driver_unregister(&swim_driver);
966 module_exit(swim_exit);
968 MODULE_DESCRIPTION("Driver for SWIM floppy controller");
969 MODULE_LICENSE("GPL");
970 MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
971 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);