📄 esdi_wini.c
字号:
/* device dependent part of a hard disk driver for ibm ps/2 esdi adapter
*
* written by doug burks, based on other minix wini drivers.
* some additions by art roberts
*
* references:
* ibm personal system/2 hardware technical reference (1988)
* ibm 60/120mb fixed disk drive technical reference (1988)
*
* caveats:
* * this driver has been reported to work on ibm ps/2 models 50 and
* 70 with ibm's 60/120mb hard disks.
* * for a true esdi adapter, changes will have to be made, but this
* certainly serves as a good start.
* * no timeouts are implemented, so this driver could hang under
* adverse conditions.
* * the error processing has not been tested. my disk works too well.
*
* The file contains one entry point:
*
* esdi_winchester_task: main entry when system is brought up
*
*
* Changes:
* 3 May 1992 by Kees J. Bot: device dependent/independent split.
*/
#include "kernel.h"
#include "driver.h"
#include "drvlib.h"
#if ENABLE_ESDI_WINI
/* If the DMA buffer is large enough then use it always. */
#define USE_BUF (DMA_BUF_SIZE > BLOCK_SIZE)
/***** esdi i/o adapter ports */
#define CMD_REG 0x3510 /* command interface register */
#define STAT_REG 0x3510 /* status interface register */
#define BCTL_REG 0x3512 /* basic control register */
#define BST_REG 0x3512 /* basic status register */
#define ATT_REG 0x3513 /* attention register */
#define INT_REG 0x3513 /* interrupt status register */
/***** basic status register bits */
#define DMA_ENA 0x80 /* DMA enabled? */
#define INT_PND 0x40 /* interrupt pending? */
#define CMD_PRG 0x20 /* command in progress? */
#define BUSY 0x10 /* is adapter busy? */
#define STR_FUL 0x08 /* status interface register set? */
#define CMD_FUL 0x04 /* command interface register full? */
#define XFR_REQ 0x02 /* data transfer operation ready? */
#define INT_SET 0x01 /* adapter sending interrupt? */
/***** attention register commands */
#define ATT_CMD 0x01 /* command request */
#define ATT_EOI 0x02 /* end of interrupt processing */
#define ATT_ABT 0x03 /* abort the current command */
#define ATT_RST 0xE4 /* reset the esdi adapter */
/***** dma register addresses */
#define DMA_EXTCMD 0x18 /* extended function register */
#define DMA_EXEC 0x1A /* extended function execute */
/***** miscellaneous */
#define ERR (-1) /* general error code */
#define ERR_BAD_SECTOR (-2) /* block marked bad detected */
#define MAX_ERRORS 4 /* maximum number of read/write retries */
#define MAX_DRIVES 2 /* maximum number of physical drives */
#define NR_DEVICES (MAX_DRIVES*DEV_PER_DRIVE)
/* Maximum number of logical devices */
#define SUB_PER_DRIVE (NR_PARTITIONS * NR_PARTITIONS)
#define NR_SUBDEVS (MAX_DRIVES * SUB_PER_DRIVE)
#define SYS_PORTA 0x92 /* system control port a */
#define LIGHT_ON 0xC0 /* fixed-disk activity light reg. mask */
/***** variables */
PRIVATE struct wini { /* disk/partition information */
unsigned open_ct; /* in-use count */
struct device part[DEV_PER_DRIVE]; /* primary partitions: hd[0-4] */
struct device subpart[SUB_PER_DRIVE]; /* subpartitions: hd[1-4][a-d] */
} wini[MAX_DRIVES], *w_wn;
PRIVATE struct trans {
struct iorequest_s *iop; /* belongs to this I/O request */
unsigned long block; /* first sector to transfer */
unsigned count; /* byte count */
phys_bytes phys; /* user physical address */
phys_bytes dma; /* DMA physical address */
} wtrans[NR_IOREQS];
PRIVATE int nr_drives; /* actual number of physical disk drive */
PRIVATE int command[4]; /* controller command buffer */
PRIVATE unsigned int status_block[9]; /* status block output from a command */
PRIVATE int dma_channel; /* fixed disk dma channel number */
PRIVATE struct trans *w_tp; /* to add transfer requests */
PRIVATE unsigned w_count; /* number of bytes to transfer */
PRIVATE unsigned long w_nextblock; /* next block on disk to transfer */
PRIVATE int w_opcode; /* DEV_READ or DEV_WRITE */
PRIVATE int w_drive; /* selected drive */
PRIVATE int w_istat; /* interrupt status of last command */
PRIVATE struct device *w_dv; /* device's base and size */
/***** functions */
FORWARD _PROTOTYPE( struct device *w_prepare, (int device) );
FORWARD _PROTOTYPE( char *w_name, (void) );
FORWARD _PROTOTYPE( int w_do_open, (struct driver *dp, message *m_ptr) );
FORWARD _PROTOTYPE( int w_do_close, (struct driver *dp, message *m_ptr) );
FORWARD _PROTOTYPE( void w_init, (void) );
FORWARD _PROTOTYPE( int w_command, (int device, int cmd, int num_words) );
FORWARD _PROTOTYPE( int w_schedule, (int proc_nr, struct iorequest_s *iop) );
FORWARD _PROTOTYPE( int w_finish, (void) );
FORWARD _PROTOTYPE( int w_transfer, (struct trans *tp, unsigned count) );
FORWARD _PROTOTYPE( int w_att_write, (int value) );
FORWARD _PROTOTYPE( void w_interrupt, (int dma) );
FORWARD _PROTOTYPE( int w_handler, (int irq) );
FORWARD _PROTOTYPE( void w_dma_setup, (struct trans *tp, unsigned count) );
FORWARD _PROTOTYPE( void w_geometry, (struct partition *entry));
/* Entry points to this driver. */
PRIVATE struct driver w_dtab = {
w_name, /* current device's name */
w_do_open, /* open or mount request, initialize device */
w_do_close, /* release device */
do_diocntl, /* get or set a partition's geometry */
w_prepare, /* prepare for I/O on a given minor device */
w_schedule, /* precompute cylinder, head, sector, etc. */
w_finish, /* do the I/O */
nop_cleanup, /* no cleanup needed */
w_geometry /* tell the geometry of the disk */
};
/*===========================================================================*
* esdi_winchester_task *
*===========================================================================*/
PUBLIC void esdi_winchester_task()
{
driver_task(&w_dtab);
}
/*===========================================================================*
* w_prepare *
*===========================================================================*/
PRIVATE struct device *w_prepare(device)
int device;
{
/* Prepare for I/O on a device. */
/* Nothing to transfer as yet. */
w_count = 0;
if (device < NR_DEVICES) { /* hd0, hd1, ... */
w_drive = device / DEV_PER_DRIVE; /* save drive number */
w_wn = &wini[w_drive];
w_dv = &w_wn->part[device % DEV_PER_DRIVE];
} else
if ((unsigned) (device -= MINOR_hd1a) < NR_SUBDEVS) { /* hd1a, hd1b, ... */
w_drive = device / SUB_PER_DRIVE;
w_wn = &wini[w_drive];
w_dv = &w_wn->subpart[device % SUB_PER_DRIVE];
} else {
return(NIL_DEV);
}
return(w_drive < nr_drives ? w_dv : NIL_DEV);
}
/*===========================================================================*
* w_name *
*===========================================================================*/
PRIVATE char *w_name()
{
/* Return a name for the current device. */
static char name[] = "esdi-hd5";
name[7] = '0' + w_drive * DEV_PER_DRIVE;
return name;
}
/*============================================================================*
* w_do_open *
*============================================================================*/
PRIVATE int w_do_open(dp, m_ptr)
struct driver *dp;
message *m_ptr;
{
/* Device open: Initialize the controller and read the partition table. */
static int init_done = FALSE;
if (!init_done) { w_init(); init_done = TRUE; }
if (w_prepare(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
if (w_wn->open_ct++ == 0) {
/* partition the disk */
partition(&w_dtab, w_drive * DEV_PER_DRIVE, P_PRIMARY);
}
return(OK);
}
/*============================================================================*
* w_do_close *
*============================================================================*/
PRIVATE int w_do_close(dp, m_ptr)
struct driver *dp;
message *m_ptr;
{
/* Device close: Release a device. */
if (w_prepare(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
w_wn->open_ct--;
return(OK);
}
/*============================================================================*
* w_init *
*============================================================================*/
PRIVATE void w_init()
{
/* initializes everything needed to run the hard disk
*
* the following items are initialized:
* -- hard disk attributes stored in bios
* -- dma transfer channel, read from system register
* -- dma transfer and interrupts [disabled]
*
* the hard disk adapter is initialized when the ibm ps/2 is turned on,
* using the programmable option select registers. thus the only
* additional initialization is making sure the dma transfer and interrupts
* are disabled. other initialization problems could be checked for, such
* as an operation underway. the paranoid could add a check for adapter
* activity and abort the operations. the truly paranoid can reset the
* adapter. until such worries are proven, why bother?
*/
unsigned int drive; /* hard disk drive number */
unsigned long size; /* hard disk size */
/* get the number of drives from the bios */
phys_copy(0x475L, tmp_phys, 1L);
nr_drives = tmp_buf[0];
if (nr_drives > MAX_DRIVES) nr_drives = MAX_DRIVES;
put_irq_handler(AT_WINI_IRQ, w_handler);
enable_irq(AT_WINI_IRQ); /* ready for winchester interrupts */
for (drive = 0; drive < nr_drives; ++drive) {
(void) w_prepare(drive * DEV_PER_DRIVE);
if (w_command(drive, 0x0609, 6) != OK) {
printf("%s: unable to get parameters\n", w_name());
nr_drives = drive;
break;
}
/* size of the drive */
size = ((unsigned long) status_block[2] << 0) |
((unsigned long) status_block[3] << 16);
if (drive == 0) {
if (w_command(7, 0x060A, 5) != OK) {
printf("%s: unable to get dma channel\n", w_name());
nr_drives = 0;
return;
}
dma_channel = (status_block[2] & 0x3C00) >> 10;
}
printf("%s: %lu sectors\n", w_name(), size);
w_wn->part[0].dv_size = size << SECTOR_SHIFT;
}
}
/*===========================================================================*
* w_command *
*===========================================================================*/
PRIVATE int w_command(device, cmd, num_words)
int device; /* i device to operate on */
/* 1-2 physical disk drive number */
/* 7 hard disk controller */
int cmd; /* i command to execute */
int num_words; /* i expected size of status block */
{
/* executes a command for a particular device
*
* the operation is conducted as follows:
* -- create the command block
* -- initialize for command reading by the controller
* -- write the command block to the controller, making sure the
* controller has digested the previous command word, before shoving
* the next down its throat
* -- wait for an interrupt
* -- read expected number of words of command status information
* -- return the command status block
*
* reading and writing registers is accompanied by enabling and disabling
* interrupts to ensure that the status register contents still apply when
* the desired register is read/written.
*/
register int ki; /* -- scratch -- */
int status; /* disk adapter status register value */
device <<= 5; /* adjust device for our use */
command[0] = cmd | device; /* build command block */
command[1] = 0;
w_att_write(device | ATT_CMD);
for (ki = 0; ki < 2; ++ki) {
out_word(CMD_REG, command[ki]);
unlock();
while (TRUE) {
lock();
status = in_byte(BST_REG);
if (!(status & CMD_FUL)) break;
unlock();
}
}
unlock();
w_interrupt(0);
if (w_istat != (device | 0x01)) {
w_att_write(device | ATT_ABT);
w_interrupt(0);
return(ERR);
}
for (ki = 0; ki < num_words; ++ki) {
while (TRUE) {
lock();
status = in_byte(BST_REG);
if (status & STR_FUL) break;
unlock();
}
status_block[ki] = in_word(STAT_REG);
unlock();
}
w_att_write(device | ATT_EOI);
return(OK);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -