⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swim3.c

📁 Linux块设备驱动源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Driver for the SWIM3 (Super Woz Integrated Machine 3) * floppy controller found on Power Macintoshes. * * Copyright (C) 1996 Paul Mackerras. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. *//* * TODO: * handle 2 drives * handle GCR disks */#include <linux/config.h>#include <linux/stddef.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/timer.h>#include <linux/delay.h>#include <linux/fd.h>#include <linux/ioctl.h>#include <linux/blkdev.h>#include <linux/devfs_fs_kernel.h>#include <linux/interrupt.h>#include <linux/module.h>#include <asm/io.h>#include <asm/dbdma.h>#include <asm/prom.h>#include <asm/uaccess.h>#include <asm/mediabay.h>#include <asm/machdep.h>#include <asm/pmac_feature.h>static struct request_queue *swim3_queue;static struct gendisk *disks[2];static struct request *fd_req;#define MAX_FLOPPIES	2enum swim_state {	idle,	locating,	seeking,	settling,	do_transfer,	jogging,	available,	revalidating,	ejecting};#define REG(x)	unsigned char x; char x ## _pad[15];/* * The names for these registers mostly represent speculation on my part. * It will be interesting to see how close they are to the names Apple uses. */struct swim3 {	REG(data);	REG(timer);		/* counts down at 1MHz */	REG(error);	REG(mode);	REG(select);		/* controls CA0, CA1, CA2 and LSTRB signals */	REG(setup);	REG(control);		/* writing bits clears them */	REG(status);		/* writing bits sets them in control */	REG(intr);	REG(nseek);		/* # tracks to seek */	REG(ctrack);		/* current track number */	REG(csect);		/* current sector number */	REG(gap3);		/* size of gap 3 in track format */	REG(sector);		/* sector # to read or write */	REG(nsect);		/* # sectors to read or write */	REG(intr_enable);};#define control_bic	control#define control_bis	status/* Bits in select register */#define CA_MASK		7#define LSTRB		8/* Bits in control register */#define DO_SEEK		0x80#define FORMAT		0x40#define SELECT		0x20#define WRITE_SECTORS	0x10#define DO_ACTION	0x08#define DRIVE2_ENABLE	0x04#define DRIVE_ENABLE	0x02#define INTR_ENABLE	0x01/* Bits in status register */#define FIFO_1BYTE	0x80#define FIFO_2BYTE	0x40#define ERROR		0x20#define DATA		0x08#define RDDATA		0x04#define INTR_PENDING	0x02#define MARK_BYTE	0x01/* Bits in intr and intr_enable registers */#define ERROR_INTR	0x20#define DATA_CHANGED	0x10#define TRANSFER_DONE	0x08#define SEEN_SECTOR	0x04#define SEEK_DONE	0x02#define TIMER_DONE	0x01/* Bits in error register */#define ERR_DATA_CRC	0x80#define ERR_ADDR_CRC	0x40#define ERR_OVERRUN	0x04#define ERR_UNDERRUN	0x01/* Bits in setup register */#define S_SW_RESET	0x80#define S_GCR_WRITE	0x40#define S_IBM_DRIVE	0x20#define S_TEST_MODE	0x10#define S_FCLK_DIV2	0x08#define S_GCR		0x04#define S_COPY_PROT	0x02#define S_INV_WDATA	0x01/* Select values for swim3_action */#define SEEK_POSITIVE	0#define SEEK_NEGATIVE	4#define STEP		1#define MOTOR_ON	2#define MOTOR_OFF	6#define INDEX		3#define EJECT		7#define SETMFM		9#define SETGCR		13/* Select values for swim3_select and swim3_readbit */#define STEP_DIR	0#define STEPPING	1#define MOTOR_ON	2#define RELAX		3	/* also eject in progress */#define READ_DATA_0	4#define TWOMEG_DRIVE	5#define SINGLE_SIDED	6	/* drive or diskette is 4MB type? */#define DRIVE_PRESENT	7#define DISK_IN		8#define WRITE_PROT	9#define TRACK_ZERO	10#define TACHO		11#define READ_DATA_1	12#define MFM_MODE	13#define SEEK_COMPLETE	14#define ONEMEG_MEDIA	15/* Definitions of values used in writing and formatting */#define DATA_ESCAPE	0x99#define GCR_SYNC_EXC	0x3f#define GCR_SYNC_CONV	0x80#define GCR_FIRST_MARK	0xd5#define GCR_SECOND_MARK	0xaa#define GCR_ADDR_MARK	"\xd5\xaa\x00"#define GCR_DATA_MARK	"\xd5\xaa\x0b"#define GCR_SLIP_BYTE	"\x27\xaa"#define GCR_SELF_SYNC	"\x3f\xbf\x1e\x34\x3c\x3f"#define DATA_99		"\x99\x99"#define MFM_ADDR_MARK	"\x99\xa1\x99\xa1\x99\xa1\x99\xfe"#define MFM_INDEX_MARK	"\x99\xc2\x99\xc2\x99\xc2\x99\xfc"#define MFM_GAP_LEN	12struct floppy_state {	enum swim_state	state;	struct swim3 __iomem *swim3;	/* hardware registers */	struct dbdma_regs __iomem *dma;	/* DMA controller registers */	int	swim3_intr;	/* interrupt number for SWIM3 */	int	dma_intr;	/* interrupt number for DMA channel */	int	cur_cyl;	/* cylinder head is on, or -1 */	int	cur_sector;	/* last sector we saw go past */	int	req_cyl;	/* the cylinder for the current r/w request */	int	head;		/* head number ditto */	int	req_sector;	/* sector number ditto */	int	scount;		/* # sectors we're transferring at present */	int	retries;	int	settle_time;	int	secpercyl;	/* disk geometry information */	int	secpertrack;	int	total_secs;	int	write_prot;	/* 1 if write-protected, 0 if not, -1 dunno */	struct dbdma_cmd *dma_cmd;	int	ref_count;	int	expect_cyl;	struct timer_list timeout;	int	timeout_pending;	int	ejected;	wait_queue_head_t wait;	int	wanted;	struct device_node*	media_bay; /* NULL when not in bay */	char	dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];};static struct floppy_state floppy_states[MAX_FLOPPIES];static int floppy_count = 0;static DEFINE_SPINLOCK(swim3_lock);static unsigned short write_preamble[] = {	0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e,	/* gap field */	0, 0, 0, 0, 0, 0,			/* sync field */	0x99a1, 0x99a1, 0x99a1, 0x99fb,		/* data address mark */	0x990f					/* no escape for 512 bytes */};static unsigned short write_postamble[] = {	0x9904,					/* insert CRC */	0x4e4e, 0x4e4e,	0x9908,					/* stop writing */	0, 0, 0, 0, 0, 0};static void swim3_select(struct floppy_state *fs, int sel);static void swim3_action(struct floppy_state *fs, int action);static int swim3_readbit(struct floppy_state *fs, int bit);static void do_fd_request(request_queue_t * q);static void start_request(struct floppy_state *fs);static void set_timeout(struct floppy_state *fs, int nticks,			void (*proc)(unsigned long));static void scan_track(struct floppy_state *fs);static void seek_track(struct floppy_state *fs, int n);static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);static void setup_transfer(struct floppy_state *fs);static void act(struct floppy_state *fs);static void scan_timeout(unsigned long data);static void seek_timeout(unsigned long data);static void settle_timeout(unsigned long data);static void xfer_timeout(unsigned long data);static irqreturn_t swim3_interrupt(int irq, void *dev_id, struct pt_regs *regs);/*static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs);*/static int grab_drive(struct floppy_state *fs, enum swim_state state,		      int interruptible);static void release_drive(struct floppy_state *fs);static int fd_eject(struct floppy_state *fs);static int floppy_ioctl(struct inode *inode, struct file *filp,			unsigned int cmd, unsigned long param);static int floppy_open(struct inode *inode, struct file *filp);static int floppy_release(struct inode *inode, struct file *filp);static int floppy_check_change(struct gendisk *disk);static int floppy_revalidate(struct gendisk *disk);static int swim3_add_device(struct device_node *swims);int swim3_init(void);#ifndef CONFIG_PMAC_MEDIABAY#define check_media_bay(which, what)	1#endifstatic void swim3_select(struct floppy_state *fs, int sel){	struct swim3 __iomem *sw = fs->swim3;	out_8(&sw->select, RELAX);	if (sel & 8)		out_8(&sw->control_bis, SELECT);	else		out_8(&sw->control_bic, SELECT);	out_8(&sw->select, sel & CA_MASK);}static void swim3_action(struct floppy_state *fs, int action){	struct swim3 __iomem *sw = fs->swim3;	swim3_select(fs, action);	udelay(1);	out_8(&sw->select, sw->select | LSTRB);	udelay(2);	out_8(&sw->select, sw->select & ~LSTRB);	udelay(1);}static int swim3_readbit(struct floppy_state *fs, int bit){	struct swim3 __iomem *sw = fs->swim3;	int stat;	swim3_select(fs, bit);	udelay(1);	stat = in_8(&sw->status);	return (stat & DATA) == 0;}static void do_fd_request(request_queue_t * q){	int i;	for(i=0;i<floppy_count;i++)	{#ifdef CONFIG_PMAC_MEDIABAY		if (floppy_states[i].media_bay &&			check_media_bay(floppy_states[i].media_bay, MB_FD))			continue;#endif /* CONFIG_PMAC_MEDIABAY */		start_request(&floppy_states[i]);	}	sti();}static void start_request(struct floppy_state *fs){	struct request *req;	unsigned long x;	if (fs->state == idle && fs->wanted) {		fs->state = available;		wake_up(&fs->wait);		return;	}	while (fs->state == idle && (req = elv_next_request(swim3_queue))) {#if 0		printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%ld buf=%p\n",		       req->rq_disk->disk_name, req->cmd,		       (long)req->sector, req->nr_sectors, req->buffer);		printk("           rq_status=%d errors=%d current_nr_sectors=%ld\n",		       req->rq_status, req->errors, req->current_nr_sectors);#endif		if (req->sector < 0 || req->sector >= fs->total_secs) {			end_request(req, 0);			continue;		}		if (req->current_nr_sectors == 0) {			end_request(req, 1);			continue;		}		if (fs->ejected) {			end_request(req, 0);			continue;		}		if (rq_data_dir(req) == WRITE) {			if (fs->write_prot < 0)				fs->write_prot = swim3_readbit(fs, WRITE_PROT);			if (fs->write_prot) {				end_request(req, 0);				continue;			}		}		/* Do not remove the cast. req->sector is now a sector_t and		 * can be 64 bits, but it will never go past 32 bits for this		 * driver anyway, so we can safely cast it down and not have		 * to do a 64/32 division		 */		fs->req_cyl = ((long)req->sector) / fs->secpercyl;		x = ((long)req->sector) % fs->secpercyl;		fs->head = x / fs->secpertrack;		fs->req_sector = x % fs->secpertrack + 1;		fd_req = req;		fs->state = do_transfer;		fs->retries = 0;		act(fs);	}}static void set_timeout(struct floppy_state *fs, int nticks,			void (*proc)(unsigned long)){	unsigned long flags;	save_flags(flags); cli();	if (fs->timeout_pending)		del_timer(&fs->timeout);	fs->timeout.expires = jiffies + nticks;	fs->timeout.function = proc;	fs->timeout.data = (unsigned long) fs;	add_timer(&fs->timeout);	fs->timeout_pending = 1;	restore_flags(flags);}static inline void scan_track(struct floppy_state *fs){	struct swim3 __iomem *sw = fs->swim3;	swim3_select(fs, READ_DATA_0);	in_8(&sw->intr);		/* clear SEEN_SECTOR bit */	in_8(&sw->error);	out_8(&sw->intr_enable, SEEN_SECTOR);	out_8(&sw->control_bis, DO_ACTION);	/* enable intr when track found */	set_timeout(fs, HZ, scan_timeout);	/* enable timeout */}static inline void seek_track(struct floppy_state *fs, int n){	struct swim3 __iomem *sw = fs->swim3;	if (n >= 0) {		swim3_action(fs, SEEK_POSITIVE);		sw->nseek = n;	} else {		swim3_action(fs, SEEK_NEGATIVE);		sw->nseek = -n;	}	fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1;	swim3_select(fs, STEP);	in_8(&sw->error);	/* enable intr when seek finished */	out_8(&sw->intr_enable, SEEK_DONE);	out_8(&sw->control_bis, DO_SEEK);	set_timeout(fs, 3*HZ, seek_timeout);	/* enable timeout */	fs->settle_time = 0;}static inline void init_dma(struct dbdma_cmd *cp, int cmd,			    void *buf, int count){	st_le16(&cp->req_count, count);	st_le16(&cp->command, cmd);	st_le32(&cp->phy_addr, virt_to_bus(buf));	cp->xfer_status = 0;}static inline void setup_transfer(struct floppy_state *fs){	int n;	struct swim3 __iomem *sw = fs->swim3;	struct dbdma_cmd *cp = fs->dma_cmd;	struct dbdma_regs __iomem *dr = fs->dma;	if (fd_req->current_nr_sectors <= 0) {		printk(KERN_ERR "swim3: transfer 0 sectors?\n");		return;	}	if (rq_data_dir(fd_req) == WRITE)		n = 1;	else {		n = fs->secpertrack - fs->req_sector + 1;		if (n > fd_req->current_nr_sectors)			n = fd_req->current_nr_sectors;	}	fs->scount = n;	swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);	out_8(&sw->sector, fs->req_sector);	out_8(&sw->nsect, n);	out_8(&sw->gap3, 0);	out_le32(&dr->cmdptr, virt_to_bus(cp));	if (rq_data_dir(fd_req) == WRITE) {		/* Set up 3 dma commands: write preamble, data, postamble */		init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));		++cp;		init_dma(cp, OUTPUT_MORE, fd_req->buffer, 512);		++cp;		init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));	} else {		init_dma(cp, INPUT_LAST, fd_req->buffer, n * 512);	}	++cp;	out_le16(&cp->command, DBDMA_STOP);	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);	in_8(&sw->error);	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);	if (rq_data_dir(fd_req) == WRITE)		out_8(&sw->control_bis, WRITE_SECTORS);	in_8(&sw->intr);	out_le32(&dr->control, (RUN << 16) | RUN);	/* enable intr when transfer complete */	out_8(&sw->intr_enable, TRANSFER_DONE);	out_8(&sw->control_bis, DO_ACTION);	set_timeout(fs, 2*HZ, xfer_timeout);	/* enable timeout */}static void act(struct floppy_state *fs){	for (;;) {		switch (fs->state) {		case idle:			return;		/* XXX shouldn't get here */		case locating:			if (swim3_readbit(fs, TRACK_ZERO)) {				fs->cur_cyl = 0;				if (fs->req_cyl == 0)					fs->state = do_transfer;				else					fs->state = seeking;				break;			}			scan_track(fs);			return;		case seeking:			if (fs->cur_cyl < 0) {				fs->expect_cyl = -1;				fs->state = locating;				break;			}			if (fs->req_cyl == fs->cur_cyl) {				printk("whoops, seeking 0\n");				fs->state = do_transfer;				break;			}			seek_track(fs, fs->req_cyl - fs->cur_cyl);			return;		case settling:			/* check for SEEK_COMPLETE after 30ms */			fs->settle_time = (HZ + 32) / 33;			set_timeout(fs, fs->settle_time, settle_timeout);			return;		case do_transfer:			if (fs->cur_cyl != fs->req_cyl) {				if (fs->retries > 5) {					end_request(fd_req, 0);					fs->state = idle;					return;				}				fs->state = seeking;				break;			}			setup_transfer(fs);			return;		case jogging:			seek_track(fs, -5);			return;		default:			printk(KERN_ERR"swim3: unknown state %d\n", fs->state);			return;		}	}}static void scan_timeout(unsigned long data){	struct floppy_state *fs = (struct floppy_state *) data;	struct swim3 __iomem *sw = fs->swim3;	fs->timeout_pending = 0;	out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);	out_8(&sw->select, RELAX);	out_8(&sw->intr_enable, 0);	fs->cur_cyl = -1;	if (fs->retries > 5) {		end_request(fd_req, 0);		fs->state = idle;		start_request(fs);	} else {		fs->state = jogging;		act(fs);	}}static void seek_timeout(unsigned long data){	struct floppy_state *fs = (struct floppy_state *) data;	struct swim3 __iomem *sw = fs->swim3;	fs->timeout_pending = 0;	out_8(&sw->control_bic, DO_SEEK);	out_8(&sw->select, RELAX);	out_8(&sw->intr_enable, 0);	printk(KERN_ERR "swim3: seek timeout\n");	end_request(fd_req, 0);	fs->state = idle;	start_request(fs);}static void settle_timeout(unsigned long data){	struct floppy_state *fs = (struct floppy_state *) data;	struct swim3 __iomem *sw = fs->swim3;	fs->timeout_pending = 0;	if (swim3_readbit(fs, SEEK_COMPLETE)) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -