📄 hw_eeprom.c
字号:
/* This file is part of the program psim. Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au> 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#ifndef _HW_EEPROM_C_#define _HW_EEPROM_C_#include "device_table.h"#ifdef HAVE_STRING_H#include <string.h>#else#ifdef HAVE_STRINGS_H#include <strings.h>#endif#endif/* DEVICE eeprom - JEDEC? compatible electricaly erasable programable device DESCRIPTION This device implements a small byte addressable EEPROM. Programming is performed using the same write sequences as used by standard modern EEPROM components. Writes occure in real time, the device returning a progress value until the programing has been completed. It is based on the AMD 29F040 component. PROPERTIES reg = <address> <size> (required) Determine where the device lives in the parents address space. nr-sectors = <integer> (required) When erasing an entire sector is cleared at a time. This specifies the number of sectors in the EEPROM component. sector-size = <integer> (required) The number of bytes in a sector. When erasing, memory chunks of this size are cleared. NOTE: The product nr-sectors * sector-size does not need to map the size specified in the reg property. If the specified size is smaller part of the eeprom will not be accessible while if it is larger the addresses will wrap. byte-write-delay = <integer> (required) Number of clock ticks before the programming of a single byte completes. sector-start-delay = <integer> (required) When erasing sectors, the number of clock ticks after the sector has been specified that the actual erase process commences. erase-delay = <intger> (required) Number of clock ticks before an erase program completes manufacture-code = <integer> (required) The one byte value returned when the auto-select manufacturer code is read. device-code = <integer> (required) The one byte value returned when the auto-select device code is read. input-file = <file-name> (optional) Initialize the eeprom using the specified binary file. output-file = <file-name> (optional) When ever the eeprom is updated, save the modified image into the specified file. EXAMPLES Enable tracing of the eeprom: | bash$ psim -t eeprom-device \ Configure something very like the Amd Am29F040 - 512byte EEPROM (but a bit faster): | -o '/eeprom@0xfff00000/reg 0xfff00000 0x80000' \ | -o '/eeprom@0xfff00000/nr-sectors 8' \ | -o '/eeprom@0xfff00000/sector-size 0x10000' \ | -o '/eeprom@0xfff00000/byte-write-delay 1000' \ | -o '/eeprom@0xfff00000/sector-start-delay 100' \ | -o '/eeprom@0xfff00000/erase-delay 1000' \ | -o '/eeprom@0xfff00000/manufacture-code 0x01' \ | -o '/eeprom@0xfff00000/device-code 0xa4' \ Initialize the eeprom from the file <</dev/zero>>: | -o '/eeprom@0xfff00000/input-file /dev/zero' BUGS */typedef enum { read_reset, write_nr_2, write_nr_3, write_nr_4, write_nr_5, write_nr_6, byte_program, byte_programming, chip_erase, sector_erase, sector_erase_suspend, autoselect,} hw_eeprom_states;static const char *state2a(hw_eeprom_states state){ switch (state) { case read_reset: return "read_reset"; case write_nr_2: return "write_nr_2"; case write_nr_3: return "write_nr_3"; case write_nr_4: return "write_nr_4"; case write_nr_5: return "write_nr_5"; case write_nr_6: return "write_nr_6"; case byte_program: return "byte_program"; case byte_programming: return "byte_programming"; case chip_erase: return "chip_erase"; case sector_erase: return "sector_erase"; case sector_erase_suspend: return "sector_erase_suspend"; case autoselect: return "autoselect"; } return NULL;}typedef struct _hw_eeprom_device { /* general */ hw_eeprom_states state; unsigned8 *memory; unsigned sizeof_memory; unsigned erase_delay; signed64 program_start_time; signed64 program_finish_time; unsigned8 manufacture_code; unsigned8 device_code; unsigned8 toggle_bit; /* initialization */ const char *input_file_name; const char *output_file_name; /* for sector and sector programming */ hw_eeprom_states sector_state; unsigned8 *sectors; unsigned nr_sectors; unsigned sizeof_sector; unsigned sector_start_delay; unsigned sector_start_time; /* byte and byte programming */ unsigned byte_write_delay; unsigned_word byte_program_address; unsigned8 byte_program_byte;} hw_eeprom_device;typedef struct _hw_eeprom_reg_spec { unsigned32 base; unsigned32 size;} hw_eeprom_reg_spec;static voidhw_eeprom_init_data(device *me){ hw_eeprom_device *eeprom = (hw_eeprom_device*)device_data(me); /* have we any input or output files */ if (device_find_property(me, "input-file") != NULL) eeprom->input_file_name = device_find_string_property(me, "input-file"); if (device_find_property(me, "output-file") != NULL) eeprom->input_file_name = device_find_string_property(me, "output-file"); /* figure out the sectors in the eeprom */ if (eeprom->sectors == NULL) { eeprom->nr_sectors = device_find_integer_property(me, "nr-sectors"); eeprom->sizeof_sector = device_find_integer_property(me, "sector-size"); eeprom->sectors = zalloc(eeprom->nr_sectors); } else memset(eeprom->sectors, 0, eeprom->nr_sectors); /* initialize the eeprom */ if (eeprom->memory == NULL) { eeprom->sizeof_memory = eeprom->sizeof_sector * eeprom->nr_sectors; eeprom->memory = zalloc(eeprom->sizeof_memory); } else memset(eeprom->memory, 0, eeprom->sizeof_memory); if (eeprom->input_file_name != NULL) { int i; FILE *input_file = fopen(eeprom->input_file_name, "r"); if (input_file == NULL) { perror("eeprom"); device_error(me, "Failed to open input file %s\n", eeprom->input_file_name); } for (i = 0; i < eeprom->sizeof_memory; i++) { if (fread(&eeprom->memory[i], 1, 1, input_file) != 1) break; } fclose(input_file); } /* timing */ eeprom->byte_write_delay = device_find_integer_property(me, "byte-write-delay"); eeprom->sector_start_delay = device_find_integer_property(me, "sector-start-delay"); eeprom->erase_delay = device_find_integer_property(me, "erase-delay"); /* misc */ eeprom->manufacture_code = device_find_integer_property(me, "manufacture-code"); eeprom->device_code = device_find_integer_property(me, "device-code");}static voidinvalid_read(device *me, hw_eeprom_states state, unsigned_word address, const char *reason){ DTRACE(eeprom, ("Invalid read to 0x%lx while in state %s (%s)\n", (unsigned long)address, state2a(state), reason));}static voidinvalid_write(device *me, hw_eeprom_states state, unsigned_word address, unsigned8 data, const char *reason){ DTRACE(eeprom, ("Invalid write of 0x%lx to 0x%lx while in state %s (%s)\n", (unsigned long)data, (unsigned long)address, state2a(state), reason));}static voiddump_eeprom(device *me, hw_eeprom_device *eeprom){ if (eeprom->output_file_name != NULL) { int i; FILE *output_file = fopen(eeprom->output_file_name, "w"); if (output_file == NULL) { perror("eeprom"); device_error(me, "Failed to open output file %s\n", eeprom->output_file_name); } for (i = 0; i < eeprom->sizeof_memory; i++) { if (fwrite(&eeprom->memory[i], 1, 1, output_file) != 1) break; } fclose(output_file); }}/* program a single byte of eeprom */static voidstart_programming_byte(device *me, hw_eeprom_device *eeprom, unsigned_word address, unsigned8 new_byte){ unsigned8 old_byte = eeprom->memory[address]; DTRACE(eeprom, ("start-programing-byte - address 0x%lx, new 0x%lx, old 0x%lx\n", (unsigned long)address, (unsigned long)new_byte, (unsigned long)old_byte)); eeprom->byte_program_address = address; /* : old new : ~old : new&~old : 0 0 : 1 : 0 : 0 1 : 1 : 1 -- can not set a bit : 1 0 : 0 : 0 : 1 1 : 0 : 0 */ if (~old_byte & new_byte) invalid_write(me, eeprom->state, address, new_byte, "setting cleared bit"); /* : old new : old&new : 0 0 : 0 : 0 1 : 0 : 1 0 : 0 : 1 1 : 1 */ eeprom->byte_program_byte = new_byte & old_byte; eeprom->memory[address] = ~new_byte & ~0x24; /* LE-bits 5:3 zero */ eeprom->program_start_time = device_event_queue_time(me); eeprom->program_finish_time = (eeprom->program_start_time + eeprom->byte_write_delay);}static voidfinish_programming_byte(device *me, hw_eeprom_device *eeprom){ DTRACE(eeprom, ("finish-programming-byte - address 0x%lx, byte 0x%lx\n", (unsigned long)eeprom->byte_program_address, (unsigned long)eeprom->byte_program_byte)); eeprom->memory[eeprom->byte_program_address] = eeprom->byte_program_byte; dump_eeprom(me, eeprom);}/* erase the eeprom completly */static voidstart_erasing_chip(device *me, hw_eeprom_device *eeprom){ DTRACE(eeprom, ("start-erasing-chip\n")); memset(eeprom->memory, 0, eeprom->sizeof_memory); eeprom->program_start_time = device_event_queue_time(me); eeprom->program_finish_time = (eeprom->program_start_time + eeprom->erase_delay);}static voidfinish_erasing_chip(device *me, hw_eeprom_device *eeprom){ DTRACE(eeprom, ("finish-erasing-chip\n")); memset(eeprom->memory, 0xff, eeprom->sizeof_memory); dump_eeprom(me, eeprom);}/* erase a single sector of the eeprom */static voidstart_erasing_sector(device *me, hw_eeprom_device *eeprom, unsigned_word address){ int sector = address / eeprom->sizeof_sector; DTRACE(eeprom, ("start-erasing-sector - address 0x%lx, sector %d\n", (unsigned long)address, sector)); ASSERT(sector < eeprom->nr_sectors); eeprom->sectors[sector] = 1; memset(eeprom->memory + sector * eeprom->sizeof_sector, 0x4, eeprom->sizeof_sector); eeprom->program_start_time = device_event_queue_time(me); eeprom->sector_start_time = (eeprom->program_start_time + eeprom->sector_start_delay); eeprom->program_finish_time = (eeprom->sector_start_time + eeprom->erase_delay);}static voidfinish_erasing_sector(device *me, hw_eeprom_device *eeprom){ int sector; DTRACE(eeprom, ("finish-erasing-sector\n")); for (sector = 0; sector < eeprom->nr_sectors; sector++) { if (eeprom->sectors[sector]) { eeprom->sectors[sector] = 0; memset(eeprom->memory + sector * eeprom->sizeof_sector, 0xff, eeprom->sizeof_sector); } } dump_eeprom(me, eeprom);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -