URL
https://opencores.org/ocsvn/or1k/or1k/trunk
Subversion Repositories or1k
[/] [or1k/] [tags/] [nog_patch_33/] [or1ksim/] [peripheral/] [atadevice.c] - Rev 997
Go to most recent revision | Compare with Previous | Blame | View Log
/* atadevice.c -- ATA Device simulation Simulates a harddisk, using a local streams. Copyright (C) 2002 Richard Herveille, rherveille@opencores.org This file is part of OpenRISC 1000 Architectural Simulator 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 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "abstract.h" #include "sched.h" #include "messages.h" #include "atadevice.h" #include "atacmd.h" #include "sim-config.h" #include "atadevice_cmdi.h" #include <string.h> #include <stdarg.h> /* mandatory commands: - execute device diagnostics (done) - flush cache - identify device (done) - initialize device parameters (done) - read dma - read multiple - read sector(s) (done) - read verify sector(s) - seek - set features - set multiple mode - write dma - write multiple - write sector(s) optional commands: - download microcode - nop - read buffer - write buffer prohibited commands: - device reset */ /* D E V I C E S _ I N I T */ void ata_devices_init(ata_devices *devices, const char *filename0, const char *filename1) { ata_device_init(&devices->device0, 0, filename0); if (devices->device0.type) ata_device_init(&devices->device1, ATA_DHR_DEV, filename1); else ata_device_init(&devices->device1, 0, filename1); } void ata_device_init(ata_device *device, int dev, const char *filename) { /* set DeviceID */ device->settings.dev = dev; /* generate stream for hd_simulation */ switch(device->type) { case TYPE_NO_CONNECT: MSG_NOTE("ata_device, using type NO_CONNECT."); device->stream = NULL; break; case TYPE_FILE: MSG_NOTE("ata_device, using device type FILE."); device->stream = open_file(&device->size, filename); break; case TYPE_LOCAL: MSG_NOTE("ata_device, using device type LOCAL."); device->stream = open_local(); break; default: MSG_FATAL("ata_device, illegal device-type."); MSG_EMPTY("defaulting to type NO_CONNECT."); device->stream = NULL; break; } } /* Use a file to simulate a hard-disk */ FILE *open_file(unsigned long *size, const char *filename) { FILE *fp; unsigned long n; // TODO: /* check if a file with name 'filename' already exists */ if ( !(fp = fopen(filename, "rb+")) ) if ( !(fp = fopen(filename, "wb+")) ) { MSG_FATAL( strcat("ata_open_file, cannot open hd-file ", filename) ); return NULL; } else { /* TODO create a file 'size' large */ /* create a file 'size' large */ MSG_NOTE("ata_device; generating a new hard disk file."); MSG_EMPTY("This may take a while, depending on the requested size."); for (n=0; n < (*size << 20); n++) fputc(0, fp); } else /* file already exist */ fprintf(stderr, "file %s already exists. Using existing file.\n", filename); ata_device_debug(1, "requested filesize was: %d (MBytes).\n", *size); /* get the size of the file. This is also the size of the harddisk*/ fseek(fp, 0, SEEK_END); *size = ftell(fp); ata_device_debug(1, "actual filesize is: %d (MBytes).\n", *size >> 20); return fp; } /* Use a the local filesystem as a hard-disk */ FILE *open_local(void) { // TODO: MSG_WARNING("ata_device; device type LOCAL is not supported yet."); MSG_EMPTY("defaulting to device type NO_CONNECT."); return NULL; } /* D E V I C E S _ H W _ R E S E T */ /* power-on and hardware reset */ void ata_devices_hw_reset(ata_devices *devices, int reset_signal) { /* display debug information */ ata_device_debug(2, "ata_devices_hw_reset.\n"); /* find device 0 */ if ( (devices->device0.stream) && (devices->device1.stream) ) { /* this one is simple, device0 is device0 */ /* 1) handle device1 first */ ata_device_hw_reset(&devices->device1, reset_signal, \ 1, \ /* assert dasp, this is device1 */ 0, \ /* negate pdiag input, no more devices */ 0); /* negate dasp input, no more devices */ /* 2) Then handle device0 */ ata_device_hw_reset(&devices->device0, reset_signal, \ 0, \ devices->device1.sigs.pdiago, \ devices->device1.sigs.daspo); } else if (devices->device0.stream) { /* device0 is device0, there's no device1 */ ata_device_hw_reset(&devices->device0, reset_signal, \ 0, \ /* negate dasp, this is device0 */ 0, \ /* negate pdiag input, there's no device1*/ 0); /* negate dasp input, there's no device1 */ } else if (devices->device1.stream) { /* device1 is (logical) device0, there's no (physical) device0 */ ata_device_hw_reset(&devices->device1, reset_signal, \ 0, \ /* negate dasp, this is device0 */ 0, \ /* negate pdiag input, there's no device1*/ 0); /* negate dasp input, there's no device1 */ } else { /* no devices connected */ ata_device_debug(1, "ata_device_hw_reset, no devices connected.\n"); } } void ata_device_hw_reset(ata_device *device, int reset_signal, \ int daspo, int pdiagi, int daspi) { /* check ata-device state */ if (device->settings.state == ATA_STATE_HW_RST) { if (!reset_signal) { /* hardware reset finished */ /* set sectors_per_track & heads_per_cylinders */ device->settings.sectors_per_track = SECTORS; device->settings.heads_per_cylinder = HEADS; /* set device1 input signals */ device->sigs.pdiagi = pdiagi; device->sigs.daspi = daspi; ata_execute_device_diagnostics_cmd(device); /* clear busy bit */ device->regs.status &= ~ATA_SR_BSY; /* set DRDY bit, when not a PACKET device */ if (!device->packet) device->regs.status |= ATA_SR_DRDY; /* set new state */ device->settings.state = ATA_STATE_IDLE; } } else if (reset_signal) { /* hardware reset signal asserted, stop what we are doing */ /* negate bus signals */ device->sigs.iordy = 0; device->sigs.intrq = 0; device->sigs.dmarq = 0; device->sigs.pdiago = 0; device->sigs.daspo = daspo; /* set busy bit */ device->regs.status |= ATA_SR_BSY; PRINTF("setting status register BSY 0x%2X\n", device->regs.status); /* set new state */ device->settings.state = ATA_STATE_HW_RST; } } /* D E V I C E S _ D O _ C O N T R O L _ R E G I S T E R Handles - software reset - Interrupt enable bit */ void ata_device_do_control_register(ata_device *device) { /* TODO respond to new values of nIEN */ /* if device not selected, respond to new values of nIEN & SRST */ /* check if SRST bit is set */ if (device->regs.device_control & ATA_DCR_RST) { if (device->settings.state == ATA_STATE_IDLE) { /* start software reset */ /* negate bus signals */ device->sigs.pdiago = 0; device->sigs.intrq = 0; device->sigs.iordy = 0; device->sigs.dmarq = 0; /* set busy bit */ device->regs.status |= ATA_SR_BSY; /* set new state */ device->settings.state = ATA_STATE_SW_RST; /* display debug information */ ata_device_debug(2, "ata_device_sw_reset initiated.\n"); } } else if (device->settings.state == ATA_STATE_SW_RST) { /* are we doing a software reset ?? */ /* SRST bit cleared, end of software reset */ /*execute device diagnostics */ ata_execute_device_diagnostics_cmd(device); /* clear busy bit */ device->regs.status &= ~ATA_SR_BSY; /* set DRDY bit, when not a PACKET device */ if (!device->packet) device->regs.status |= ATA_SR_DRDY; /* set new state */ device->settings.state = ATA_STATE_IDLE; /* display debug information */ ata_device_debug(2, "ata_device_sw_reset done.\n"); } /* <else> We are doing a hardware reset (or similar) ignore command */ } /* D E V I C E S _ D O _ C O M M A N D _ R E G I S T E R */ void ata_device_do_command_register(ata_device *device) { /* check BSY & DRQ */ if ( (device->regs.status & ATA_SR_BSY) || (device->regs.status & ATA_SR_DRQ) ) if (device->regs.command != DEVICE_RESET) MSG_WARNING("ata_device_write, writing a command while BSY or DRQ asserted."); /* check if device selected */ if ( (device->regs.device_head & ATA_DHR_DEV) == device->settings.dev ) ata_device_execute_cmd(device); else { /* if not selected, only respond to EXECUTE DEVICE DIAGNOSTICS */ if (device->regs.command == EXECUTE_DEVICE_DIAGNOSTICS) ata_device_execute_cmd(device); } } /* D E V I C E S _ R E A D */ /* Read from devices */ short ata_devices_read(ata_devices *devices, char adr) { ata_device *device; /* check for no connected devices */ if ( (!devices->device0.stream) && (!devices->device1.stream) ) MSG_ERROR("ata_devices_read, no ata devices connected."); else { /* check if both device0 and device1 are connected */ if ( (devices->device0.stream) && (devices->device1.stream) ) { /* get the current active device */ if (devices->device1.regs.device_head & ATA_DHR_DEV) device = &devices->device1; else device = &devices->device0; } else { /* only one device connected */ if (devices->device1.stream) device = &devices->device1; else device = &devices->device0; } /* return data provided by selected device */ switch (adr) { case ATA_ASR : ata_device_debug(4, "alternate_status register read\n"); if ( (device->regs.device_head & ATA_DHR_DEV) == device->settings.dev ) return device -> regs.status; else { ata_device_debug(2, "device0 responds for device1, asr = 0x00\n"); return 0; // return 0 when device0 responds for device1 } case ATA_CHR : ata_device_debug(4, "cylinder_high register read, value = 0x%02X\n", \ device->regs.cylinder_high); return device -> regs.cylinder_high; case ATA_CLR : ata_device_debug(4, "cylinder_low register read, value = 0x%02X\n", \ device->regs.cylinder_low); return device -> regs.cylinder_low; case ATA_DR : ata_device_debug(4, "data register read, value = 0x%04X, cnt = %3d\n", \ device->settings.dbuf[device->settings.dbuf_cnt], device->settings.dbuf_cnt); return device -> settings.dbuf[device->settings.dbuf_cnt++]; case ATA_DHR : ata_device_debug(4, "device_head register read, value = 0x%02X\n", \ device->regs.device_head); return device -> regs.device_head; case ATA_ERR : ata_device_debug(4, "error register read, value = 0x%02X\n", \ device->regs.error); return device -> regs.error; case ATA_SCR : ata_device_debug(4, "sectorcount register read, value = 0x%02X\n", \ device->regs.sector_count); return device -> regs.sector_count; case ATA_SNR : ata_device_debug(4, "sectornumber register read, value = 0x%02X\n", \ device->regs.sector_number); return device -> regs.sector_number; case ATA_SR : ata_device_debug(4, "status register read\n"); if ( (device->regs.device_head & ATA_DHR_DEV) == device->settings.dev) return device -> regs.status; else { ata_device_debug(2, "device0 responds for device1, sr = 0x00\n"); return 0; // return 0 when device0 responds for device1 } // case ATA_DA : // return device -> regs.status; } } } /* D E V I C E S _ W R I T E */ /* Write to devices */ void ata_devices_write(ata_devices *devices, char adr, short value) { ata_device *device; /* check for no connected devices */ if (!devices->device0.stream && !devices->device1.stream) MSG_ERROR("ata_devices_write, no ata devices connected."); else { /* first device */ if (devices->device0.stream) ata_device_write(&devices->device0, adr, value); /* second device */ if (devices->device1.stream) ata_device_write(&devices->device1, adr, value); } } /* write to a single device */ void ata_device_write(ata_device *device, char adr, short value) { switch (adr) { case ATA_CR : /*display debug information */ ata_device_debug(4, "command register written, value = 0x%02X\n", value); device->regs.command = value; /* check command register settings and execute command */ ata_device_do_command_register(device); break; case ATA_CHR : /*display debug information */ ata_device_debug(4, "cylinder high register written, value = 0x%02X\n", value); device->regs.cylinder_high = value; break; case ATA_CLR : /*display debug information */ ata_device_debug(4, "cylinder low register written, value = 0x%02X\n", value); device->regs.cylinder_low = value; break; case ATA_DR : /*display debug information */ ata_device_debug(4, "data register written, value = 0x%04X\n", value); device->regs.dataport_i = value; break; case ATA_DCR : /*display debug information */ ata_device_debug(4, "device control register written, value = 0x%02X\n", value); device->regs.device_control = value; ata_device_do_control_register(device); break; case ATA_DHR : /*display debug information */ ata_device_debug(4, "device head register written, value = 0x%02X\n", value); device->regs.device_head = value; break; case ATA_FR : /*display debug information */ ata_device_debug(4, "features register written, value = 0x%02X\n", value); device->regs.features = value; break; case ATA_SCR : /*display debug information */ ata_device_debug(4, "sectorcount register written, value = 0x%02X\n", value); device->regs.sector_count = value; break; case ATA_SNR : /*display debug information */ ata_device_debug(4, "sectornumber register written, value = 0x%02X\n", value); device->regs.sector_number = value; break; } //endcase } /* -------------------------- */ /* -- print debug messages -- */ /* -------------------------- */ int ata_device_debug(int lvl, char *format, ...) { va_list ap; va_start(ap, format); if (ATA_DEVICE_DEBUG_LVL >= lvl) { fprintf(stderr, "ata_device_debug: "); vfprintf(stderr, format, ap); } va_end(ap); return 0; }
Go to most recent revision | Compare with Previous | Blame | View Log