📄 ps2esdi.c
字号:
/* ps2esdi driver based on assembler code by Arindam Banerji, written by Peter De Schrijver *//* Reassuring note to IBM : This driver was NOT developed by vice-versa engineering the PS/2's BIOS *//* Dedicated to Wannes, Tofke, Ykke, Godot, Killroy and all those other lovely fish out there... *//* This code was written during the long and boring WINA elections 1994 *//* Thanks to Arindam Banerij for giving me the source of his driver *//* This code may be freely distributed and modified in any way, as long as these notes remain intact *//* Revised: 05/07/94 by Arindam Banerji (axb@cse.nd.edu) *//* Revised: 09/08/94 by Peter De Schrijver (stud11@cc4.kuleuven.ac.be) Thanks to Arindam Banerij for sending me the docs of the adapter *//* BA Modified for ThinkPad 720 by Boris Ashkinazi *//* (bash@vnet.ibm.com) 08/08/95 *//* Modified further for ThinkPad-720C by Uri Blumenthal *//* (uri@watson.ibm.com) Sep 11, 1995 *//* TODO : + Timeouts + Get disk parameters + DMA above 16MB + reset after read/write error */#include <linux/config.h>#include <linux/major.h>#ifdef CONFIG_BLK_DEV_PS2#define MAJOR_NR PS2ESDI_MAJOR#include <linux/errno.h>#include <linux/sched.h>#include <linux/mm.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/genhd.h>#include <linux/ps2esdi.h>#include <linux/devfs_fs_kernel.h>#include <linux/blk.h>#include <linux/blkpg.h>#include <linux/mca.h>#include <linux/init.h>#include <linux/ioport.h>#include <asm/system.h>#include <asm/io.h>#include <asm/segment.h>#include <asm/dma.h>#include <asm/uaccess.h>#define PS2ESDI_IRQ 14#define MAX_HD 1#define MAX_RETRIES 5#define MAX_16BIT 65536#define ESDI_TIMEOUT 0xf000#define ESDI_STAT_TIMEOUT 4#define TYPE_0_CMD_BLK_LENGTH 2#define TYPE_1_CMD_BLK_LENGTH 4static void reset_ctrl(void);int ps2esdi_init(void);static void ps2esdi_geninit(void);static void do_ps2esdi_request(request_queue_t * q);static void ps2esdi_readwrite(int cmd, u_char drive, u_int block, u_int count);static void ps2esdi_fill_cmd_block(u_short * cmd_blk, u_short cmd,u_short cyl, u_short head, u_short sector, u_short length, u_char drive);static int ps2esdi_out_cmd_blk(u_short * cmd_blk);static void ps2esdi_prep_dma(char *buffer, u_short length, u_char dma_xmode);static void ps2esdi_interrupt_handler(int irq, void *dev_id, struct pt_regs *regs);static void (*current_int_handler) (u_int) = NULL;static void ps2esdi_normal_interrupt_handler(u_int);static void ps2esdi_initial_reset_int_handler(u_int);static void ps2esdi_geometry_int_handler(u_int);static void ps2esdi_continue_request(void);static int ps2esdi_open(struct inode *inode, struct file *file);static int ps2esdi_release(struct inode *inode, struct file *file);static int ps2esdi_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long arg);static int ps2esdi_reread_partitions(kdev_t dev);static int ps2esdi_read_status_words(int num_words, int max_words, u_short * buffer);static void dump_cmd_complete_status(u_int int_ret_code);static void ps2esdi_get_device_cfg(void);void ps2esdi_reset_timer(unsigned long unused);u_int dma_arb_level; /* DMA arbitration level */static DECLARE_WAIT_QUEUE_HEAD(ps2esdi_int);static DECLARE_WAIT_QUEUE_HEAD(ps2esdi_wait_open);int no_int_yet;static int access_count[MAX_HD];static char ps2esdi_valid[MAX_HD];static int ps2esdi_sizes[MAX_HD << 6];static int ps2esdi_blocksizes[MAX_HD << 6];static int ps2esdi_drives;static struct hd_struct ps2esdi[MAX_HD << 6];static u_short io_base;static struct timer_list esdi_timer = { function: ps2esdi_reset_timer };static int reset_status;static int ps2esdi_slot = -1;int tp720esdi = 0; /* Is it Integrated ESDI of ThinkPad-720? */int intg_esdi = 0; /* If integrated adapter */struct ps2esdi_i_struct { unsigned int head, sect, cyl, wpcom, lzone, ctl;};#if 0#if 0 /* try both - I don't know which one is better... UB */struct ps2esdi_i_struct ps2esdi_info[] ={ {4, 48, 1553, 0, 0, 0}, {0, 0, 0, 0, 0, 0}};#elsestruct ps2esdi_i_struct ps2esdi_info[] ={ {64, 32, 161, 0, 0, 0}, {0, 0, 0, 0, 0, 0}};#endif#endifstruct ps2esdi_i_struct ps2esdi_info[] ={ {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}};static struct block_device_operations ps2esdi_fops ={ open: ps2esdi_open, release: ps2esdi_release, ioctl: ps2esdi_ioctl,};static struct gendisk ps2esdi_gendisk ={ MAJOR_NR, /* Major number */ "ed", /* Major name */ 6, /* Bits to shift to get real from partition */ 1 << 6, /* Number of partitions per real disk */ ps2esdi, /* hd struct */ ps2esdi_sizes, /* block sizes */ 0, /* number */ (void *) ps2esdi_info, /* internal */ NULL, /* next */ &ps2esdi_fops, /* file operations */};/* initialization routine called by ll_rw_blk.c */int __init ps2esdi_init(void){ /* register the device - pass the name, major number and operations vector . */ if (devfs_register_blkdev(MAJOR_NR, "ed", &ps2esdi_fops)) { printk("%s: Unable to get major number %d\n", DEVICE_NAME, MAJOR_NR); return -1; } /* set up some global information - indicating device specific info */ blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST); read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read ahead */ /* some minor housekeeping - setup the global gendisk structure */ ps2esdi_gendisk.next = gendisk_head; gendisk_head = &ps2esdi_gendisk; ps2esdi_geninit(); return 0;} /* ps2esdi_init */#ifdef MODULEint cyl[2] = {-1,-1};int head[2] = {-1, -1};int sect[2] = {-1, -1};MODULE_PARM(tp720esdi, "i");MODULE_PARM(cyl, "i");MODULE_PARM(head, "i");MODULE_PARM(track, "i");int init_module(void) { int drive; for(drive = 0; drive <= 1; drive++) { struct ps2_esdi_i_struct *info = &ps2esdi_info[drive]; if (cyl[drive] != -1) { info->cyl = info->lzone = cyl[drive]; info->wpcom = 0; } if (head[drive] != -1) { info->head = head[drive]; info->ctl = (head[drive] > 8 ? 8 : 0); } if (sect[drive] != -1) info->sect = sect[drive]; } return ps2esdi_init();}voidcleanup_module(void){ if(ps2esdi_slot) { mca_mark_as_unused(ps2esdi_slot); mca_set_adapter_procfn(ps2esdi_slot, NULL, NULL); } release_region(io_base, 4); free_dma(dma_arb_level); free_irq(PS2ESDI_IRQ, NULL) devfs_unregister_blkdev(MAJOR_NR, "ed"); blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));}#endif /* MODULE *//* handles boot time command line parameters */void __init tp720_setup(char *str, int *ints){ /* no params, just sets the tp720esdi flag if it exists */ printk("%s: TP 720 ESDI flag set\n", DEVICE_NAME); tp720esdi = 1;}void __init ed_setup(char *str, int *ints){ int hdind = 0; /* handles 3 parameters only - corresponding to 1. Number of cylinders 2. Number of heads 3. Sectors/track */ if (ints[0] != 3) return; /* print out the information - seen at boot time */ printk("%s: ints[0]=%d ints[1]=%d ints[2]=%d ints[3]=%d\n", DEVICE_NAME, ints[0], ints[1], ints[2], ints[3]); /* set the index into device specific information table */ if (ps2esdi_info[0].head != 0) hdind = 1; /* set up all the device information */ ps2esdi_info[hdind].head = ints[2]; ps2esdi_info[hdind].sect = ints[3]; ps2esdi_info[hdind].cyl = ints[1]; ps2esdi_info[hdind].wpcom = 0; ps2esdi_info[hdind].lzone = ints[1]; ps2esdi_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);#if 0 /* this may be needed for PS2/Mod.80, but it hurts ThinkPad! */ ps2esdi_drives = hdind + 1; /* increment index for the next time */#endif} /* ed_setup */static int ps2esdi_getinfo(char *buf, int slot, void *d){ int len = 0; len += sprintf(buf + len, "DMA Arbitration Level: %d\n", dma_arb_level); len += sprintf(buf + len, "IO Port: %x\n", io_base); len += sprintf(buf + len, "IRQ: 14\n"); len += sprintf(buf + len, "Drives: %d\n", ps2esdi_drives); return len;}/* ps2 esdi specific initialization - called thru the gendisk chain */static void __init ps2esdi_geninit(void){ /* The first part contains the initialization code for the ESDI disk subsystem. All we really do is search for the POS registers of the controller to do some simple setup operations. First, we must ensure that the controller is installed, enabled, and configured as PRIMARY. Then we must determine the DMA arbitration level being used by the controller so we can handle data transfer operations properly. If all of this works, then we will set the INIT_FLAG to a non-zero value. */ int slot = 0, i, reset_start, reset_end; u_char status; unsigned short adapterID; if ((slot = mca_find_adapter(INTG_ESDI_ID, 0)) != MCA_NOTFOUND) { adapterID = INTG_ESDI_ID; printk("%s: integrated ESDI adapter found in slot %d\n", DEVICE_NAME, slot+1);#ifndef MODULE mca_set_adapter_name(slot, "PS/2 Integrated ESDI");#endif } else if ((slot = mca_find_adapter(NRML_ESDI_ID, 0)) != -1) { adapterID = NRML_ESDI_ID; printk("%s: normal ESDI adapter found in slot %d\n", DEVICE_NAME, slot+1); mca_set_adapter_name(slot, "PS/2 ESDI"); } else { return; } ps2esdi_slot = slot; mca_mark_as_used(slot); mca_set_adapter_procfn(slot, (MCA_ProcFn) ps2esdi_getinfo, NULL); /* Found the slot - read the POS register 2 to get the necessary configuration and status information. POS register 2 has the following information : Bit Function 7 reserved = 0 6 arbitration method 0 - fairness enabled 1 - fairness disabled, linear priority assignment 5-2 arbitration level 1 alternate address 1 alternate address 0 - use addresses 0x3510 - 0x3517 0 adapter enable */ status = mca_read_stored_pos(slot, 2); /* is it enabled ? */ if (!(status & STATUS_ENABLED)) { printk("%s: ESDI adapter disabled\n", DEVICE_NAME); return; } /* try to grab IRQ, and try to grab a slow IRQ if it fails, so we can share with the SCSI driver */ if (request_irq(PS2ESDI_IRQ, ps2esdi_interrupt_handler, SA_INTERRUPT | SA_SHIRQ, "PS/2 ESDI", &ps2esdi_gendisk) && request_irq(PS2ESDI_IRQ, ps2esdi_interrupt_handler, SA_SHIRQ, "PS/2 ESDI", &ps2esdi_gendisk) ) { printk("%s: Unable to get IRQ %d\n", DEVICE_NAME, PS2ESDI_IRQ); return; } if (status & STATUS_ALTERNATE) io_base = ALT_IO_BASE; else io_base = PRIMARY_IO_BASE; /* get the dma arbitration level */ dma_arb_level = (status >> 2) & 0xf; /* BA */ printk("%s: DMA arbitration level : %d\n", DEVICE_NAME, dma_arb_level); LITE_ON; current_int_handler = ps2esdi_initial_reset_int_handler; reset_ctrl(); reset_status = 0; reset_start = jiffies; while (!reset_status) { init_timer(&esdi_timer); esdi_timer.expires = jiffies + HZ; esdi_timer.data = 0; add_timer(&esdi_timer); sleep_on(&ps2esdi_int); } reset_end = jiffies; LITE_OFF; printk("%s: reset interrupt after %d jiffies, %u.%02u secs\n", DEVICE_NAME, reset_end - reset_start, (reset_end - reset_start) / HZ, (reset_end - reset_start) % HZ); /* Integrated ESDI Disk and Controller has only one drive! */ if (adapterID == INTG_ESDI_ID) {/* if not "normal" PS2 ESDI adapter */ ps2esdi_drives = 1; /* then we have only one physical disk! */ intg_esdi = 1; } /* finally this part sets up some global data structures etc. */ ps2esdi_get_device_cfg();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -