blob: a515d0c1d2cb8e8e60fccf8efbc72449b5cc6648 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Driver for the SWIM3 (Super Woz Integrated Machine 3)
4 * floppy controller found on Power Macintoshes.
5 *
6 * Copyright (C) 1996 Paul Mackerras.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
10 * TODO:
11 * handle 2 drives
12 * handle GCR disks
13 */
14
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +010015#undef DEBUG
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/stddef.h>
18#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010019#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/timer.h>
21#include <linux/delay.h>
22#include <linux/fd.h>
23#include <linux/ioctl.h>
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -060024#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/interrupt.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Paul Mackerras515729e2005-11-08 12:15:36 +110028#include <linux/spinlock.h>
Arnd Bergmann106fd892014-02-26 12:01:44 +010029#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
31#include <asm/dbdma.h>
32#include <asm/prom.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/mediabay.h>
35#include <asm/machdep.h>
36#include <asm/pmac_feature.h>
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define MAX_FLOPPIES 2
39
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +010040static DEFINE_MUTEX(swim3_mutex);
41static struct gendisk *disks[MAX_FLOPPIES];
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043enum swim_state {
44 idle,
45 locating,
46 seeking,
47 settling,
48 do_transfer,
49 jogging,
50 available,
51 revalidating,
52 ejecting
53};
54
55#define REG(x) unsigned char x; char x ## _pad[15];
56
57/*
58 * The names for these registers mostly represent speculation on my part.
59 * It will be interesting to see how close they are to the names Apple uses.
60 */
61struct swim3 {
62 REG(data);
63 REG(timer); /* counts down at 1MHz */
64 REG(error);
65 REG(mode);
66 REG(select); /* controls CA0, CA1, CA2 and LSTRB signals */
67 REG(setup);
68 REG(control); /* writing bits clears them */
69 REG(status); /* writing bits sets them in control */
70 REG(intr);
71 REG(nseek); /* # tracks to seek */
72 REG(ctrack); /* current track number */
73 REG(csect); /* current sector number */
74 REG(gap3); /* size of gap 3 in track format */
75 REG(sector); /* sector # to read or write */
76 REG(nsect); /* # sectors to read or write */
77 REG(intr_enable);
78};
79
80#define control_bic control
81#define control_bis status
82
83/* Bits in select register */
84#define CA_MASK 7
85#define LSTRB 8
86
87/* Bits in control register */
88#define DO_SEEK 0x80
89#define FORMAT 0x40
90#define SELECT 0x20
91#define WRITE_SECTORS 0x10
92#define DO_ACTION 0x08
93#define DRIVE2_ENABLE 0x04
94#define DRIVE_ENABLE 0x02
95#define INTR_ENABLE 0x01
96
97/* Bits in status register */
98#define FIFO_1BYTE 0x80
99#define FIFO_2BYTE 0x40
100#define ERROR 0x20
101#define DATA 0x08
102#define RDDATA 0x04
103#define INTR_PENDING 0x02
104#define MARK_BYTE 0x01
105
106/* Bits in intr and intr_enable registers */
107#define ERROR_INTR 0x20
108#define DATA_CHANGED 0x10
109#define TRANSFER_DONE 0x08
110#define SEEN_SECTOR 0x04
111#define SEEK_DONE 0x02
112#define TIMER_DONE 0x01
113
114/* Bits in error register */
115#define ERR_DATA_CRC 0x80
116#define ERR_ADDR_CRC 0x40
117#define ERR_OVERRUN 0x04
118#define ERR_UNDERRUN 0x01
119
120/* Bits in setup register */
121#define S_SW_RESET 0x80
122#define S_GCR_WRITE 0x40
123#define S_IBM_DRIVE 0x20
124#define S_TEST_MODE 0x10
125#define S_FCLK_DIV2 0x08
126#define S_GCR 0x04
127#define S_COPY_PROT 0x02
128#define S_INV_WDATA 0x01
129
130/* Select values for swim3_action */
131#define SEEK_POSITIVE 0
132#define SEEK_NEGATIVE 4
133#define STEP 1
134#define MOTOR_ON 2
135#define MOTOR_OFF 6
136#define INDEX 3
137#define EJECT 7
138#define SETMFM 9
139#define SETGCR 13
140
141/* Select values for swim3_select and swim3_readbit */
142#define STEP_DIR 0
143#define STEPPING 1
144#define MOTOR_ON 2
145#define RELAX 3 /* also eject in progress */
146#define READ_DATA_0 4
Finn Thain56a1c5e2018-04-11 20:50:14 -0400147#define ONEMEG_DRIVE 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#define SINGLE_SIDED 6 /* drive or diskette is 4MB type? */
149#define DRIVE_PRESENT 7
150#define DISK_IN 8
151#define WRITE_PROT 9
152#define TRACK_ZERO 10
153#define TACHO 11
154#define READ_DATA_1 12
Finn Thain56a1c5e2018-04-11 20:50:14 -0400155#define GCR_MODE 13
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define SEEK_COMPLETE 14
Finn Thain56a1c5e2018-04-11 20:50:14 -0400157#define TWOMEG_MEDIA 15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/* Definitions of values used in writing and formatting */
160#define DATA_ESCAPE 0x99
161#define GCR_SYNC_EXC 0x3f
162#define GCR_SYNC_CONV 0x80
163#define GCR_FIRST_MARK 0xd5
164#define GCR_SECOND_MARK 0xaa
165#define GCR_ADDR_MARK "\xd5\xaa\x00"
166#define GCR_DATA_MARK "\xd5\xaa\x0b"
167#define GCR_SLIP_BYTE "\x27\xaa"
168#define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f"
169
170#define DATA_99 "\x99\x99"
171#define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
172#define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
173#define MFM_GAP_LEN 12
174
175struct floppy_state {
176 enum swim_state state;
177 struct swim3 __iomem *swim3; /* hardware registers */
178 struct dbdma_regs __iomem *dma; /* DMA controller registers */
179 int swim3_intr; /* interrupt number for SWIM3 */
180 int dma_intr; /* interrupt number for DMA channel */
181 int cur_cyl; /* cylinder head is on, or -1 */
182 int cur_sector; /* last sector we saw go past */
183 int req_cyl; /* the cylinder for the current r/w request */
184 int head; /* head number ditto */
185 int req_sector; /* sector number ditto */
186 int scount; /* # sectors we're transferring at present */
187 int retries;
188 int settle_time;
189 int secpercyl; /* disk geometry information */
190 int secpertrack;
191 int total_secs;
192 int write_prot; /* 1 if write-protected, 0 if not, -1 dunno */
193 struct dbdma_cmd *dma_cmd;
194 int ref_count;
195 int expect_cyl;
196 struct timer_list timeout;
197 int timeout_pending;
198 int ejected;
199 wait_queue_head_t wait;
200 int wanted;
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +0000201 struct macio_dev *mdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100203 int index;
204 struct request *cur_req;
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600205 struct blk_mq_tag_set tag_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100208#define swim3_err(fmt, arg...) dev_err(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
209#define swim3_warn(fmt, arg...) dev_warn(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
210#define swim3_info(fmt, arg...) dev_info(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
211
212#ifdef DEBUG
213#define swim3_dbg(fmt, arg...) dev_dbg(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
214#else
215#define swim3_dbg(fmt, arg...) do { } while(0)
216#endif
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static struct floppy_state floppy_states[MAX_FLOPPIES];
219static int floppy_count = 0;
220static DEFINE_SPINLOCK(swim3_lock);
221
222static unsigned short write_preamble[] = {
223 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */
224 0, 0, 0, 0, 0, 0, /* sync field */
225 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */
226 0x990f /* no escape for 512 bytes */
227};
228
229static unsigned short write_postamble[] = {
230 0x9904, /* insert CRC */
231 0x4e4e, 0x4e4e,
232 0x9908, /* stop writing */
233 0, 0, 0, 0, 0, 0
234};
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static void seek_track(struct floppy_state *fs, int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static void act(struct floppy_state *fs);
Kees Cookb5775a62017-10-17 15:32:27 -0700238static void scan_timeout(struct timer_list *t);
239static void seek_timeout(struct timer_list *t);
240static void settle_timeout(struct timer_list *t);
241static void xfer_timeout(struct timer_list *t);
David Howells7d12e782006-10-05 14:55:46 +0100242static irqreturn_t swim3_interrupt(int irq, void *dev_id);
243/*static void fd_dma_interrupt(int irq, void *dev_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int grab_drive(struct floppy_state *fs, enum swim_state state,
245 int interruptible);
246static void release_drive(struct floppy_state *fs);
247static int fd_eject(struct floppy_state *fs);
Al Virob4d9a442008-03-02 10:20:44 -0500248static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned int cmd, unsigned long param);
Al Virob4d9a442008-03-02 10:20:44 -0500250static int floppy_open(struct block_device *bdev, fmode_t mode);
Al Virodb2a1442013-05-05 21:52:57 -0400251static void floppy_release(struct gendisk *disk, fmode_t mode);
Tejun Heo4bbde772011-03-09 19:54:28 +0100252static unsigned int floppy_check_events(struct gendisk *disk,
253 unsigned int clearing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static int floppy_revalidate(struct gendisk *disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200256static bool swim3_end_request(struct floppy_state *fs, blk_status_t err, unsigned int nr_bytes)
Tejun Heof4bd4b92009-05-08 11:54:04 +0900257{
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100258 struct request *req = fs->cur_req;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100259
260 swim3_dbg(" end request, err=%d nr_bytes=%d, cur_req=%p\n",
261 err, nr_bytes, req);
262
263 if (err)
264 nr_bytes = blk_rq_cur_bytes(req);
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600265 if (blk_update_request(req, err, nr_bytes))
Tejun Heof4bd4b92009-05-08 11:54:04 +0900266 return true;
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600267 __blk_mq_end_request(req, err);
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100268 fs->cur_req = NULL;
Tejun Heof4bd4b92009-05-08 11:54:04 +0900269 return false;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static void swim3_select(struct floppy_state *fs, int sel)
273{
274 struct swim3 __iomem *sw = fs->swim3;
275
276 out_8(&sw->select, RELAX);
277 if (sel & 8)
278 out_8(&sw->control_bis, SELECT);
279 else
280 out_8(&sw->control_bic, SELECT);
281 out_8(&sw->select, sel & CA_MASK);
282}
283
284static void swim3_action(struct floppy_state *fs, int action)
285{
286 struct swim3 __iomem *sw = fs->swim3;
287
288 swim3_select(fs, action);
289 udelay(1);
290 out_8(&sw->select, sw->select | LSTRB);
291 udelay(2);
292 out_8(&sw->select, sw->select & ~LSTRB);
293 udelay(1);
294}
295
296static int swim3_readbit(struct floppy_state *fs, int bit)
297{
298 struct swim3 __iomem *sw = fs->swim3;
299 int stat;
300
301 swim3_select(fs, bit);
302 udelay(1);
303 stat = in_8(&sw->status);
304 return (stat & DATA) == 0;
305}
306
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600307static blk_status_t swim3_queue_rq(struct blk_mq_hw_ctx *hctx,
308 const struct blk_mq_queue_data *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600310 struct floppy_state *fs = hctx->queue->queuedata;
311 struct request *req = bd->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned long x;
313
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600314 spin_lock_irq(&swim3_lock);
315 if (fs->cur_req || fs->state != idle) {
316 spin_unlock_irq(&swim3_lock);
317 return BLK_STS_DEV_RESOURCE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600319 blk_mq_start_request(req);
320 fs->cur_req = req;
321 if (fs->mdev->media_bay &&
322 check_media_bay(fs->mdev->media_bay) != MB_FD) {
323 swim3_dbg("%s", " media bay absent, dropping req\n");
324 swim3_end_request(fs, BLK_STS_IOERR, 0);
325 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600327 if (fs->ejected) {
328 swim3_dbg("%s", " disk ejected\n");
329 swim3_end_request(fs, BLK_STS_IOERR, 0);
330 goto out;
331 }
332 if (rq_data_dir(req) == WRITE) {
333 if (fs->write_prot < 0)
334 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
335 if (fs->write_prot) {
336 swim3_dbg("%s", " try to write, disk write protected\n");
337 swim3_end_request(fs, BLK_STS_IOERR, 0);
338 goto out;
339 }
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600342 /*
343 * Do not remove the cast. blk_rq_pos(req) is now a sector_t and can be
344 * 64 bits, but it will never go past 32 bits for this driver anyway, so
345 * we can safely cast it down and not have to do a 64/32 division
346 */
347 fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl;
348 x = ((long)blk_rq_pos(req)) % fs->secpercyl;
349 fs->head = x / fs->secpertrack;
350 fs->req_sector = x % fs->secpertrack + 1;
351 fs->state = do_transfer;
352 fs->retries = 0;
353
354 act(fs);
355
356out:
357 spin_unlock_irq(&swim3_lock);
358 return BLK_STS_OK;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static void set_timeout(struct floppy_state *fs, int nticks,
Kees Cookb5775a62017-10-17 15:32:27 -0700362 void (*proc)(struct timer_list *t))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (fs->timeout_pending)
365 del_timer(&fs->timeout);
366 fs->timeout.expires = jiffies + nticks;
Kees Cook841b86f2017-10-23 09:40:42 +0200367 fs->timeout.function = proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 add_timer(&fs->timeout);
369 fs->timeout_pending = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static inline void scan_track(struct floppy_state *fs)
373{
374 struct swim3 __iomem *sw = fs->swim3;
375
376 swim3_select(fs, READ_DATA_0);
377 in_8(&sw->intr); /* clear SEEN_SECTOR bit */
378 in_8(&sw->error);
379 out_8(&sw->intr_enable, SEEN_SECTOR);
380 out_8(&sw->control_bis, DO_ACTION);
381 /* enable intr when track found */
382 set_timeout(fs, HZ, scan_timeout); /* enable timeout */
383}
384
385static inline void seek_track(struct floppy_state *fs, int n)
386{
387 struct swim3 __iomem *sw = fs->swim3;
388
389 if (n >= 0) {
390 swim3_action(fs, SEEK_POSITIVE);
391 sw->nseek = n;
392 } else {
393 swim3_action(fs, SEEK_NEGATIVE);
394 sw->nseek = -n;
395 }
396 fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1;
397 swim3_select(fs, STEP);
398 in_8(&sw->error);
399 /* enable intr when seek finished */
400 out_8(&sw->intr_enable, SEEK_DONE);
401 out_8(&sw->control_bis, DO_SEEK);
402 set_timeout(fs, 3*HZ, seek_timeout); /* enable timeout */
403 fs->settle_time = 0;
404}
405
Christoph Hellwigb60b2702021-04-06 08:18:39 +0200406/*
407 * XXX: this is a horrible hack, but at least allows ppc32 to get
408 * out of defining virt_to_bus, and this driver out of using the
409 * deprecated block layer bounce buffering for highmem addresses
410 * for no good reason.
411 */
412static unsigned long swim3_phys_to_bus(phys_addr_t paddr)
413{
414 return paddr + PCI_DRAM_OFFSET;
415}
416
417static phys_addr_t swim3_bio_phys(struct bio *bio)
418{
419 return page_to_phys(bio_page(bio)) + bio_offset(bio);
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static inline void init_dma(struct dbdma_cmd *cp, int cmd,
Christoph Hellwigb60b2702021-04-06 08:18:39 +0200423 phys_addr_t paddr, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
David Gibsonf5718722015-02-03 16:36:21 +1100425 cp->req_count = cpu_to_le16(count);
426 cp->command = cpu_to_le16(cmd);
Christoph Hellwigb60b2702021-04-06 08:18:39 +0200427 cp->phy_addr = cpu_to_le32(swim3_phys_to_bus(paddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 cp->xfer_status = 0;
429}
430
431static inline void setup_transfer(struct floppy_state *fs)
432{
433 int n;
434 struct swim3 __iomem *sw = fs->swim3;
435 struct dbdma_cmd *cp = fs->dma_cmd;
436 struct dbdma_regs __iomem *dr = fs->dma;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100437 struct request *req = fs->cur_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100439 if (blk_rq_cur_sectors(req) <= 0) {
440 swim3_warn("%s", "Transfer 0 sectors ?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return;
442 }
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100443 if (rq_data_dir(req) == WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 n = 1;
445 else {
446 n = fs->secpertrack - fs->req_sector + 1;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100447 if (n > blk_rq_cur_sectors(req))
448 n = blk_rq_cur_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100450
451 swim3_dbg(" setup xfer at sect %d (of %d) head %d for %d\n",
452 fs->req_sector, fs->secpertrack, fs->head, n);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 fs->scount = n;
455 swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
456 out_8(&sw->sector, fs->req_sector);
457 out_8(&sw->nsect, n);
458 out_8(&sw->gap3, 0);
Christoph Hellwigb60b2702021-04-06 08:18:39 +0200459 out_le32(&dr->cmdptr, swim3_phys_to_bus(virt_to_phys(cp)));
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100460 if (rq_data_dir(req) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Set up 3 dma commands: write preamble, data, postamble */
Christoph Hellwigb60b2702021-04-06 08:18:39 +0200462 init_dma(cp, OUTPUT_MORE, virt_to_phys(write_preamble),
463 sizeof(write_preamble));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ++cp;
Christoph Hellwigb60b2702021-04-06 08:18:39 +0200465 init_dma(cp, OUTPUT_MORE, swim3_bio_phys(req->bio), 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ++cp;
Christoph Hellwigb60b2702021-04-06 08:18:39 +0200467 init_dma(cp, OUTPUT_LAST, virt_to_phys(write_postamble),
468 sizeof(write_postamble));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 } else {
Christoph Hellwigb60b2702021-04-06 08:18:39 +0200470 init_dma(cp, INPUT_LAST, swim3_bio_phys(req->bio), n * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 ++cp;
473 out_le16(&cp->command, DBDMA_STOP);
474 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
475 in_8(&sw->error);
476 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100477 if (rq_data_dir(req) == WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 out_8(&sw->control_bis, WRITE_SECTORS);
479 in_8(&sw->intr);
480 out_le32(&dr->control, (RUN << 16) | RUN);
481 /* enable intr when transfer complete */
482 out_8(&sw->intr_enable, TRANSFER_DONE);
483 out_8(&sw->control_bis, DO_ACTION);
484 set_timeout(fs, 2*HZ, xfer_timeout); /* enable timeout */
485}
486
487static void act(struct floppy_state *fs)
488{
489 for (;;) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100490 swim3_dbg(" act loop, state=%d, req_cyl=%d, cur_cyl=%d\n",
491 fs->state, fs->req_cyl, fs->cur_cyl);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 switch (fs->state) {
494 case idle:
495 return; /* XXX shouldn't get here */
496
497 case locating:
498 if (swim3_readbit(fs, TRACK_ZERO)) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100499 swim3_dbg("%s", " locate track 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 fs->cur_cyl = 0;
501 if (fs->req_cyl == 0)
502 fs->state = do_transfer;
503 else
504 fs->state = seeking;
505 break;
506 }
507 scan_track(fs);
508 return;
509
510 case seeking:
511 if (fs->cur_cyl < 0) {
512 fs->expect_cyl = -1;
513 fs->state = locating;
514 break;
515 }
516 if (fs->req_cyl == fs->cur_cyl) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100517 swim3_warn("%s", "Whoops, seeking 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 fs->state = do_transfer;
519 break;
520 }
521 seek_track(fs, fs->req_cyl - fs->cur_cyl);
522 return;
523
524 case settling:
525 /* check for SEEK_COMPLETE after 30ms */
526 fs->settle_time = (HZ + 32) / 33;
527 set_timeout(fs, fs->settle_time, settle_timeout);
528 return;
529
530 case do_transfer:
531 if (fs->cur_cyl != fs->req_cyl) {
532 if (fs->retries > 5) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100533 swim3_err("Wrong cylinder in transfer, want: %d got %d\n",
534 fs->req_cyl, fs->cur_cyl);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200535 swim3_end_request(fs, BLK_STS_IOERR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 fs->state = idle;
537 return;
538 }
539 fs->state = seeking;
540 break;
541 }
542 setup_transfer(fs);
543 return;
544
545 case jogging:
546 seek_track(fs, -5);
547 return;
548
549 default:
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100550 swim3_err("Unknown state %d\n", fs->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return;
552 }
553 }
554}
555
Kees Cookb5775a62017-10-17 15:32:27 -0700556static void scan_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Kees Cookb5775a62017-10-17 15:32:27 -0700558 struct floppy_state *fs = from_timer(fs, t, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct swim3 __iomem *sw = fs->swim3;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100560 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100562 swim3_dbg("* scan timeout, state=%d\n", fs->state);
563
564 spin_lock_irqsave(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 fs->timeout_pending = 0;
566 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
567 out_8(&sw->select, RELAX);
568 out_8(&sw->intr_enable, 0);
569 fs->cur_cyl = -1;
570 if (fs->retries > 5) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200571 swim3_end_request(fs, BLK_STS_IOERR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 fs->state = idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 } else {
574 fs->state = jogging;
575 act(fs);
576 }
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100577 spin_unlock_irqrestore(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Kees Cookb5775a62017-10-17 15:32:27 -0700580static void seek_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Kees Cookb5775a62017-10-17 15:32:27 -0700582 struct floppy_state *fs = from_timer(fs, t, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct swim3 __iomem *sw = fs->swim3;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100584 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100586 swim3_dbg("* seek timeout, state=%d\n", fs->state);
587
588 spin_lock_irqsave(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 fs->timeout_pending = 0;
590 out_8(&sw->control_bic, DO_SEEK);
591 out_8(&sw->select, RELAX);
592 out_8(&sw->intr_enable, 0);
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100593 swim3_err("%s", "Seek timeout\n");
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200594 swim3_end_request(fs, BLK_STS_IOERR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 fs->state = idle;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100596 spin_unlock_irqrestore(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Kees Cookb5775a62017-10-17 15:32:27 -0700599static void settle_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Kees Cookb5775a62017-10-17 15:32:27 -0700601 struct floppy_state *fs = from_timer(fs, t, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct swim3 __iomem *sw = fs->swim3;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100603 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100605 swim3_dbg("* settle timeout, state=%d\n", fs->state);
606
607 spin_lock_irqsave(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 fs->timeout_pending = 0;
609 if (swim3_readbit(fs, SEEK_COMPLETE)) {
610 out_8(&sw->select, RELAX);
611 fs->state = locating;
612 act(fs);
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100613 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 out_8(&sw->select, RELAX);
616 if (fs->settle_time < 2*HZ) {
617 ++fs->settle_time;
618 set_timeout(fs, 1, settle_timeout);
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100619 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100621 swim3_err("%s", "Seek settle timeout\n");
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200622 swim3_end_request(fs, BLK_STS_IOERR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 fs->state = idle;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100624 unlock:
625 spin_unlock_irqrestore(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Kees Cookb5775a62017-10-17 15:32:27 -0700628static void xfer_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Kees Cookb5775a62017-10-17 15:32:27 -0700630 struct floppy_state *fs = from_timer(fs, t, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct swim3 __iomem *sw = fs->swim3;
632 struct dbdma_regs __iomem *dr = fs->dma;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100633 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int n;
635
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100636 swim3_dbg("* xfer timeout, state=%d\n", fs->state);
637
638 spin_lock_irqsave(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 fs->timeout_pending = 0;
640 out_le32(&dr->control, RUN << 16);
641 /* We must wait a bit for dbdma to stop */
642 for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
643 udelay(1);
644 out_8(&sw->intr_enable, 0);
645 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
646 out_8(&sw->select, RELAX);
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100647 swim3_err("Timeout %sing sector %ld\n",
648 (rq_data_dir(fs->cur_req)==WRITE? "writ": "read"),
649 (long)blk_rq_pos(fs->cur_req));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200650 swim3_end_request(fs, BLK_STS_IOERR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 fs->state = idle;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100652 spin_unlock_irqrestore(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
David Howells7d12e782006-10-05 14:55:46 +0100655static irqreturn_t swim3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct floppy_state *fs = (struct floppy_state *) dev_id;
658 struct swim3 __iomem *sw = fs->swim3;
659 int intr, err, n;
660 int stat, resid;
661 struct dbdma_regs __iomem *dr;
662 struct dbdma_cmd *cp;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100663 unsigned long flags;
664 struct request *req = fs->cur_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100666 swim3_dbg("* interrupt, state=%d\n", fs->state);
667
668 spin_lock_irqsave(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 intr = in_8(&sw->intr);
670 err = (intr & ERROR_INTR)? in_8(&sw->error): 0;
671 if ((intr & ERROR_INTR) && fs->state != do_transfer)
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100672 swim3_err("Non-transfer error interrupt: state=%d, dir=%x, intr=%x, err=%x\n",
673 fs->state, rq_data_dir(req), intr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 switch (fs->state) {
675 case locating:
676 if (intr & SEEN_SECTOR) {
677 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
678 out_8(&sw->select, RELAX);
679 out_8(&sw->intr_enable, 0);
680 del_timer(&fs->timeout);
681 fs->timeout_pending = 0;
682 if (sw->ctrack == 0xff) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100683 swim3_err("%s", "Seen sector but cyl=ff?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 fs->cur_cyl = -1;
685 if (fs->retries > 5) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200686 swim3_end_request(fs, BLK_STS_IOERR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 fs->state = idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 } else {
689 fs->state = jogging;
690 act(fs);
691 }
692 break;
693 }
694 fs->cur_cyl = sw->ctrack;
695 fs->cur_sector = sw->csect;
696 if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100697 swim3_err("Expected cyl %d, got %d\n",
698 fs->expect_cyl, fs->cur_cyl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 fs->state = do_transfer;
700 act(fs);
701 }
702 break;
703 case seeking:
704 case jogging:
705 if (sw->nseek == 0) {
706 out_8(&sw->control_bic, DO_SEEK);
707 out_8(&sw->select, RELAX);
708 out_8(&sw->intr_enable, 0);
709 del_timer(&fs->timeout);
710 fs->timeout_pending = 0;
711 if (fs->state == seeking)
712 ++fs->retries;
713 fs->state = settling;
714 act(fs);
715 }
716 break;
717 case settling:
718 out_8(&sw->intr_enable, 0);
719 del_timer(&fs->timeout);
720 fs->timeout_pending = 0;
721 act(fs);
722 break;
723 case do_transfer:
724 if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
725 break;
726 out_8(&sw->intr_enable, 0);
727 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
728 out_8(&sw->select, RELAX);
729 del_timer(&fs->timeout);
730 fs->timeout_pending = 0;
731 dr = fs->dma;
732 cp = fs->dma_cmd;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100733 if (rq_data_dir(req) == WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ++cp;
735 /*
736 * Check that the main data transfer has finished.
737 * On writing, the swim3 sometimes doesn't use
738 * up all the bytes of the postamble, so we can still
739 * see DMA active here. That doesn't matter as long
740 * as all the sector data has been transferred.
741 */
742 if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) {
743 /* wait a little while for DMA to complete */
744 for (n = 0; n < 100; ++n) {
745 if (cp->xfer_status != 0)
746 break;
747 udelay(1);
748 barrier();
749 }
750 }
751 /* turn off DMA */
752 out_le32(&dr->control, (RUN | PAUSE) << 16);
David Gibsonf5718722015-02-03 16:36:21 +1100753 stat = le16_to_cpu(cp->xfer_status);
754 resid = le16_to_cpu(cp->res_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (intr & ERROR_INTR) {
756 n = fs->scount - 1 - resid / 512;
757 if (n > 0) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100758 blk_update_request(req, 0, n << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 fs->req_sector += n;
760 }
761 if (fs->retries < 5) {
762 ++fs->retries;
763 act(fs);
764 } else {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100765 swim3_err("Error %sing block %ld (err=%x)\n",
766 rq_data_dir(req) == WRITE? "writ": "read",
767 (long)blk_rq_pos(req), err);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200768 swim3_end_request(fs, BLK_STS_IOERR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 fs->state = idle;
770 }
771 } else {
772 if ((stat & ACTIVE) == 0 || resid != 0) {
773 /* musta been an error */
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100774 swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid);
775 swim3_err(" state=%d, dir=%x, intr=%x, err=%x\n",
776 fs->state, rq_data_dir(req), intr, err);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200777 swim3_end_request(fs, BLK_STS_IOERR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 fs->state = idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
780 }
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100781 fs->retries = 0;
782 if (swim3_end_request(fs, 0, fs->scount << 9)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 fs->req_sector += fs->scount;
784 if (fs->req_sector > fs->secpertrack) {
785 fs->req_sector -= fs->secpertrack;
786 if (++fs->head > 1) {
787 fs->head = 0;
788 ++fs->req_cyl;
789 }
790 }
791 act(fs);
Tejun Heo467ca752009-04-28 13:06:12 +0900792 } else
793 fs->state = idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 break;
796 default:
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100797 swim3_err("Don't know what to do in state %d\n", fs->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100799 spin_unlock_irqrestore(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return IRQ_HANDLED;
801}
802
803/*
David Howells7d12e782006-10-05 14:55:46 +0100804static void fd_dma_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806}
807*/
808
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100809/* Called under the mutex to grab exclusive access to a drive */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static int grab_drive(struct floppy_state *fs, enum swim_state state,
811 int interruptible)
812{
813 unsigned long flags;
814
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100815 swim3_dbg("%s", "-> grab drive\n");
816
817 spin_lock_irqsave(&swim3_lock, flags);
818 if (fs->state != idle && fs->state != available) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 ++fs->wanted;
Arnd Bergmann106fd892014-02-26 12:01:44 +0100820 /* this will enable irqs in order to sleep */
821 if (!interruptible)
822 wait_event_lock_irq(fs->wait,
823 fs->state == available,
824 swim3_lock);
825 else if (wait_event_interruptible_lock_irq(fs->wait,
826 fs->state == available,
827 swim3_lock)) {
828 --fs->wanted;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100829 spin_unlock_irqrestore(&swim3_lock, flags);
Arnd Bergmann106fd892014-02-26 12:01:44 +0100830 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 --fs->wanted;
833 }
834 fs->state = state;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100835 spin_unlock_irqrestore(&swim3_lock, flags);
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 0;
838}
839
840static void release_drive(struct floppy_state *fs)
841{
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600842 struct request_queue *q = disks[fs->index]->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 unsigned long flags;
844
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100845 swim3_dbg("%s", "-> release drive\n");
846
847 spin_lock_irqsave(&swim3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 fs->state = idle;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +0100849 spin_unlock_irqrestore(&swim3_lock, flags);
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -0600850
851 blk_mq_freeze_queue(q);
852 blk_mq_quiesce_queue(q);
853 blk_mq_unquiesce_queue(q);
854 blk_mq_unfreeze_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857static int fd_eject(struct floppy_state *fs)
858{
859 int err, n;
860
861 err = grab_drive(fs, ejecting, 1);
862 if (err)
863 return err;
864 swim3_action(fs, EJECT);
865 for (n = 20; n > 0; --n) {
866 if (signal_pending(current)) {
867 err = -EINTR;
868 break;
869 }
870 swim3_select(fs, RELAX);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -0700871 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (swim3_readbit(fs, DISK_IN) == 0)
873 break;
874 }
875 swim3_select(fs, RELAX);
876 udelay(150);
877 fs->ejected = 1;
878 release_drive(fs);
879 return err;
880}
881
882static struct floppy_struct floppy_type =
883 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */
884
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200885static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 unsigned int cmd, unsigned long param)
887{
Al Virob4d9a442008-03-02 10:20:44 -0500888 struct floppy_state *fs = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int err;
890
891 if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
892 return -EPERM;
893
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +0000894 if (fs->mdev->media_bay &&
895 check_media_bay(fs->mdev->media_bay) != MB_FD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return -ENXIO;
897
898 switch (cmd) {
899 case FDEJECT:
900 if (fs->ref_count != 1)
901 return -EBUSY;
902 err = fd_eject(fs);
903 return err;
904 case FDGETPRM:
905 if (copy_to_user((void __user *) param, &floppy_type,
906 sizeof(struct floppy_struct)))
907 return -EFAULT;
908 return 0;
909 }
910 return -ENOTTY;
911}
912
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200913static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
914 unsigned int cmd, unsigned long param)
915{
916 int ret;
917
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200918 mutex_lock(&swim3_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200919 ret = floppy_locked_ioctl(bdev, mode, cmd, param);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200920 mutex_unlock(&swim3_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200921
922 return ret;
923}
924
Al Virob4d9a442008-03-02 10:20:44 -0500925static int floppy_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Al Virob4d9a442008-03-02 10:20:44 -0500927 struct floppy_state *fs = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct swim3 __iomem *sw = fs->swim3;
929 int n, err = 0;
930
931 if (fs->ref_count == 0) {
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +0000932 if (fs->mdev->media_bay &&
933 check_media_bay(fs->mdev->media_bay) != MB_FD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return -ENXIO;
935 out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
936 out_8(&sw->control_bic, 0xff);
937 out_8(&sw->mode, 0x95);
938 udelay(10);
939 out_8(&sw->intr_enable, 0);
940 out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
941 swim3_action(fs, MOTOR_ON);
942 fs->write_prot = -1;
943 fs->cur_cyl = -1;
944 for (n = 0; n < 2 * HZ; ++n) {
945 if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE))
946 break;
947 if (signal_pending(current)) {
948 err = -EINTR;
949 break;
950 }
951 swim3_select(fs, RELAX);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -0700952 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
955 || swim3_readbit(fs, DISK_IN) == 0))
956 err = -ENXIO;
957 swim3_action(fs, SETMFM);
958 swim3_select(fs, RELAX);
959
Al Virob4d9a442008-03-02 10:20:44 -0500960 } else if (fs->ref_count == -1 || mode & FMODE_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -EBUSY;
962
Al Virob4d9a442008-03-02 10:20:44 -0500963 if (err == 0 && (mode & FMODE_NDELAY) == 0
964 && (mode & (FMODE_READ|FMODE_WRITE))) {
Christoph Hellwig3dcbad22020-09-08 16:53:35 +0200965 if (bdev_check_media_change(bdev))
966 floppy_revalidate(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (fs->ejected)
968 err = -ENXIO;
969 }
970
Al Virob4d9a442008-03-02 10:20:44 -0500971 if (err == 0 && (mode & FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (fs->write_prot < 0)
973 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
974 if (fs->write_prot)
975 err = -EROFS;
976 }
977
978 if (err) {
979 if (fs->ref_count == 0) {
980 swim3_action(fs, MOTOR_OFF);
981 out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
982 swim3_select(fs, RELAX);
983 }
984 return err;
985 }
986
Al Virob4d9a442008-03-02 10:20:44 -0500987 if (mode & FMODE_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 fs->ref_count = -1;
989 else
990 ++fs->ref_count;
991
992 return 0;
993}
994
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200995static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
996{
997 int ret;
998
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200999 mutex_lock(&swim3_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001000 ret = floppy_open(bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001001 mutex_unlock(&swim3_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001002
1003 return ret;
1004}
1005
Al Virodb2a1442013-05-05 21:52:57 -04001006static void floppy_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Al Virob4d9a442008-03-02 10:20:44 -05001008 struct floppy_state *fs = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct swim3 __iomem *sw = fs->swim3;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001010
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001011 mutex_lock(&swim3_mutex);
Finn Thain296dcc42018-12-31 16:44:09 +11001012 if (fs->ref_count > 0)
1013 --fs->ref_count;
1014 else if (fs->ref_count == -1)
1015 fs->ref_count = 0;
1016 if (fs->ref_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 swim3_action(fs, MOTOR_OFF);
1018 out_8(&sw->control_bic, 0xff);
1019 swim3_select(fs, RELAX);
1020 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001021 mutex_unlock(&swim3_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Tejun Heo4bbde772011-03-09 19:54:28 +01001024static unsigned int floppy_check_events(struct gendisk *disk,
1025 unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct floppy_state *fs = disk->private_data;
Tejun Heo4bbde772011-03-09 19:54:28 +01001028 return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031static int floppy_revalidate(struct gendisk *disk)
1032{
1033 struct floppy_state *fs = disk->private_data;
1034 struct swim3 __iomem *sw;
1035 int ret, n;
1036
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +00001037 if (fs->mdev->media_bay &&
1038 check_media_bay(fs->mdev->media_bay) != MB_FD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 sw = fs->swim3;
1042 grab_drive(fs, revalidating, 0);
1043 out_8(&sw->intr_enable, 0);
1044 out_8(&sw->control_bis, DRIVE_ENABLE);
1045 swim3_action(fs, MOTOR_ON); /* necessary? */
1046 fs->write_prot = -1;
1047 fs->cur_cyl = -1;
1048 mdelay(1);
1049 for (n = HZ; n > 0; --n) {
1050 if (swim3_readbit(fs, SEEK_COMPLETE))
1051 break;
1052 if (signal_pending(current))
1053 break;
1054 swim3_select(fs, RELAX);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -07001055 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057 ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
1058 || swim3_readbit(fs, DISK_IN) == 0;
1059 if (ret)
1060 swim3_action(fs, MOTOR_OFF);
1061 else {
1062 fs->ejected = 0;
1063 swim3_action(fs, SETMFM);
1064 }
1065 swim3_select(fs, RELAX);
1066
1067 release_drive(fs);
1068 return ret;
1069}
1070
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001071static const struct block_device_operations floppy_fops = {
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001072 .open = floppy_unlocked_open,
Al Virob4d9a442008-03-02 10:20:44 -05001073 .release = floppy_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001074 .ioctl = floppy_ioctl,
Tejun Heo4bbde772011-03-09 19:54:28 +01001075 .check_events = floppy_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076};
1077
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -06001078static const struct blk_mq_ops swim3_mq_ops = {
1079 .queue_rq = swim3_queue_rq,
1080};
1081
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001082static void swim3_mb_event(struct macio_dev* mdev, int mb_state)
1083{
1084 struct floppy_state *fs = macio_get_drvdata(mdev);
Cong Ding7414d4f2013-02-21 15:16:40 -08001085 struct swim3 __iomem *sw;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001086
1087 if (!fs)
1088 return;
Cong Ding7414d4f2013-02-21 15:16:40 -08001089
1090 sw = fs->swim3;
1091
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001092 if (mb_state != MB_FD)
1093 return;
1094
1095 /* Clear state */
1096 out_8(&sw->intr_enable, 0);
1097 in_8(&sw->intr);
1098 in_8(&sw->error);
1099}
1100
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001101static int swim3_add_device(struct macio_dev *mdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Grant Likely61c7a082010-04-13 16:12:29 -07001103 struct device_node *swim = mdev->ofdev.dev.of_node;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001104 struct floppy_state *fs = &floppy_states[index];
1105 int rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001107 fs->mdev = mdev;
1108 fs->index = index;
1109
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001110 /* Check & Request resources */
1111 if (macio_resource_count(mdev) < 2) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001112 swim3_err("%s", "No address in device-tree\n");
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001113 return -ENXIO;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001114 }
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001115 if (macio_irq_count(mdev) < 1) {
1116 swim3_err("%s", "No interrupt in device-tree\n");
1117 return -ENXIO;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001118 }
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001119 if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001120 swim3_err("%s", "Can't request mmio resource\n");
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001121 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001123 if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001124 swim3_err("%s", "Can't request dma resource\n");
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001125 macio_release_resource(mdev, 0);
1126 return -EBUSY;
1127 }
1128 dev_set_drvdata(&mdev->ofdev.dev, fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +00001130 if (mdev->media_bay == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 fs->state = idle;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001134 fs->swim3 = (struct swim3 __iomem *)
1135 ioremap(macio_resource_start(mdev, 0), 0x200);
1136 if (fs->swim3 == NULL) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001137 swim3_err("%s", "Couldn't map mmio registers\n");
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001138 rc = -ENOMEM;
1139 goto out_release;
1140 }
1141 fs->dma = (struct dbdma_regs __iomem *)
1142 ioremap(macio_resource_start(mdev, 1), 0x200);
1143 if (fs->dma == NULL) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001144 swim3_err("%s", "Couldn't map dma registers\n");
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001145 iounmap(fs->swim3);
1146 rc = -ENOMEM;
1147 goto out_release;
1148 }
1149 fs->swim3_intr = macio_irq(mdev, 0);
Joe Perchesa419aef2009-08-18 11:18:35 -07001150 fs->dma_intr = macio_irq(mdev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 fs->cur_cyl = -1;
1152 fs->cur_sector = -1;
1153 fs->secpercyl = 36;
1154 fs->secpertrack = 18;
1155 fs->total_secs = 2880;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 init_waitqueue_head(&fs->wait);
1157
1158 fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
1159 memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
David Gibsonf5718722015-02-03 16:36:21 +11001160 fs->dma_cmd[1].command = cpu_to_le16(DBDMA_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001162 if (mdev->media_bay == NULL || check_media_bay(mdev->media_bay) == MB_FD)
1163 swim3_mb_event(mdev, MB_FD);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001166 swim3_err("%s", "Couldn't request interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001168 goto out_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Kees Cookb5775a62017-10-17 15:32:27 -07001171 timer_setup(&fs->timeout, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001173 swim3_info("SWIM3 floppy controller %s\n",
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +00001174 mdev->media_bay ? "in media bay" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001176 return 0;
1177
1178 out_unmap:
1179 iounmap(fs->dma);
1180 iounmap(fs->swim3);
1181
1182 out_release:
1183 macio_release_resource(mdev, 0);
1184 macio_release_resource(mdev, 1);
1185
1186 return rc;
1187}
1188
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001189static int swim3_attach(struct macio_dev *mdev,
1190 const struct of_device_id *match)
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001191{
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001192 struct floppy_state *fs;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001193 struct gendisk *disk;
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001194 int rc;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001195
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001196 if (floppy_count >= MAX_FLOPPIES)
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001197 return -ENXIO;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001198
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001199 if (floppy_count == 0) {
1200 rc = register_blkdev(FLOPPY_MAJOR, "fd");
1201 if (rc)
1202 return rc;
1203 }
1204
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001205 disk = alloc_disk(1);
1206 if (disk == NULL) {
1207 rc = -ENOMEM;
1208 goto out_unregister;
1209 }
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -06001210
Finn Thain427c5ce2018-12-31 16:44:09 +11001211 fs = &floppy_states[floppy_count];
1212 memset(fs, 0, sizeof(*fs));
1213
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -06001214 disk->queue = blk_mq_init_sq_queue(&fs->tag_set, &swim3_mq_ops, 2,
1215 BLK_MQ_F_SHOULD_MERGE);
1216 if (IS_ERR(disk->queue)) {
1217 rc = PTR_ERR(disk->queue);
1218 disk->queue = NULL;
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001219 goto out_put_disk;
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001220 }
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001221 disk->queue->queuedata = fs;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001222
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001223 rc = swim3_add_device(mdev, floppy_count);
1224 if (rc)
1225 goto out_cleanup_queue;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001226
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001227 disk->major = FLOPPY_MAJOR;
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001228 disk->first_minor = floppy_count;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001229 disk->fops = &floppy_fops;
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001230 disk->private_data = fs;
Martin Wilck773008f2019-03-27 14:51:04 +01001231 disk->events = DISK_EVENT_MEDIA_CHANGE;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001232 disk->flags |= GENHD_FL_REMOVABLE;
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001233 sprintf(disk->disk_name, "fd%d", floppy_count);
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001234 set_capacity(disk, 2880);
1235 add_disk(disk);
1236
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001237 disks[floppy_count++] = disk;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001238 return 0;
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001239
1240out_cleanup_queue:
1241 blk_cleanup_queue(disk->queue);
1242 disk->queue = NULL;
Omar Sandoval8ccb8cb2018-10-15 09:14:46 -06001243 blk_mq_free_tag_set(&fs->tag_set);
Omar Sandovaldbaa54b2018-10-11 12:20:43 -07001244out_put_disk:
1245 put_disk(disk);
1246out_unregister:
1247 if (floppy_count == 0)
1248 unregister_blkdev(FLOPPY_MAJOR, "fd");
1249 return rc;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001250}
1251
Arvind Yadavcc3f2e92017-06-16 15:24:39 +05301252static const struct of_device_id swim3_match[] =
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001253{
1254 {
1255 .name = "swim3",
1256 },
1257 {
1258 .compatible = "ohare-swim3"
1259 },
1260 {
1261 .compatible = "swim3"
1262 },
Axel Linf41c53a2011-08-03 15:02:55 +02001263 { /* end of list */ }
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001264};
1265
1266static struct macio_driver swim3_driver =
1267{
Benjamin Herrenschmidtc2cdf6a2010-06-02 17:09:18 +10001268 .driver = {
1269 .name = "swim3",
1270 .of_match_table = swim3_match,
1271 },
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001272 .probe = swim3_attach,
Benjamin Herrenschmidtb3025452011-12-12 12:42:12 +01001273#ifdef CONFIG_PMAC_MEDIABAY
1274 .mediabay_event = swim3_mb_event,
1275#endif
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001276#if 0
1277 .suspend = swim3_suspend,
1278 .resume = swim3_resume,
1279#endif
1280};
1281
1282
1283int swim3_init(void)
1284{
1285 macio_register_driver(&swim3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287}
1288
1289module_init(swim3_init)
1290
1291MODULE_LICENSE("GPL");
1292MODULE_AUTHOR("Paul Mackerras");
1293MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);