tkernel_2/monitor/driver/flash/src/main.c | bare source | permlink (0.01 seconds) |
1: /* 2: *---------------------------------------------------------------------- 3: * T-Kernel 2.0 Software Package 4: * 5: * Copyright 2011 by Ken Sakamura. 6: * This software is distributed under the latest version of T-License 2.x. 7: *---------------------------------------------------------------------- 8: * 9: * Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17. 10: * Modified by TRON Forum(http://www.tron.org/) at 2015/06/01. 11: * 12: *---------------------------------------------------------------------- 13: */ 14: 15: /* 16: * main.c 17: * 18: * Flash ROM write processing 19: */ 20: 21: #include "flash.h" 22: 23: IMPORT B __flashwr_org[], __flashwr_start[], __flashwr_end[]; 24: 25: /* 26: * Flash ROM sector erase / write 27: * addr Flash ROM write start address (must be on a sector boundary) 28: * data write data start address (RAM) 29: * nsec number of sectors to write 30: * msg 0 : no message display no verify write 31: * 1 : message display with verify write 32: * -1 : message display no verify write 33: * return value error code 34: */ 35: EXPORT ER writeFrom( UW addr, UW data, W nsec, W msg ) 36: { 37: ER (* volatile flashwr_p)( UW addr, void *data, W nsec, BOOL reset ); 38: BOOL reset; 39: W sz; 40: UB c; 41: ER er; 42: 43: /* parameter check */ 44: if ( nsec <= 0 ) return E_PAR; 45: if ( (addr & (FROM_SECSZ-1)) != 0 ) return E_PAR; 46: sz = FROM_SECSZ * nsec; 47: if ( !inMemArea(addr, addr + sz, MSA_FROM) ) return E_PAR; 48: if ( !inMemArea(data, data + sz, MSA_OS|MSA_ERAM|MSA_WRK) ) 49: return E_PAR; 50: 51: /* check the write-protect status */ 52: er = flashwr_protect(addr, nsec); 53: if ( er < E_OK ) return er; 54: 55: /* Confirm if monitor itself is to be written */ 56: reset = isOverlapMemArea(addr, addr + sz, MSA_MON); 57: if ( reset && msg != 0 ) { 58: DSP_S("Update Monitor Area, "); 59: if (msg > 0) { 60: DSP_S("OK (y/n)? "); 61: c = (UB)getChar(1); 62: putChar(c); 63: putChar('\n'); 64: if (c != 'Y' && c != 'y') return E_OK; 65: } else if (msg < 0) { 66: DSP_S("Restart System after Writing."); 67: } 68: } 69: 70: /* initial set-up before Flash ROM write */ 71: flashwr_setup(reset); 72: 73: if ( __flashwr_start != __flashwr_org ) { 74: /* transfer flashwr() to RAM area */ 75: memcpy(__flashwr_start, __flashwr_org, 76: __flashwr_end - __flashwr_start); 77: } 78: 79: /* Flash ROM sector erase / write 80: * flashwr() is executed in RAM area. 81: * if reset = TRUE, doesn't return. 82: * The offset is too large, and so we use indirect address. 83: */ 84: flashwr_p = &flashwr; 85: er = (*flashwr_p)(addr, (UH*)data, nsec, reset); 86: 87: /* restore after Flash ROM write */ 88: flashwr_done(); 89: 90: return er; 91: } 92: 93: /* 94: * set up Flash ROM loading processing 95: * mode 0 : set up for loading write data 96: * -1 : set up for writing already loaded data 97: * 98: * in the case of setting up loading (mode= 0) 99: * addr returns the following value. 100: * addr[0] the start address in RAM area for loading data 101: * addr[1] the end address in RAM area for loading data 102: * addr[1] - addr[0] + 1 = load area size 103: * in principle, load area size matches the size of FLASH ROM. 104: * But if RAM is small, there may be cases 105: * in which load area size is smaller than that of Flash ROM size. 106: * addr[2] the distance between the data load RAM area and Flash ROM area 107: * adjustment is made so that the addr[0] position is written to the beginning of Flash ROM. 108: * addr[2] = addr[0] - Flash ROM start address 109: * 110: * in the case of setting up for writing (mode = -1), 111: * we set the writing area based on the addr value when we called this function using mode = 0. 112: * addr[0] starting address of loaded data in RAM area (to be written) 113: * addr[1] ending address of loaded data in RAM area (to be written) 114: * addr[1] - addr[0] + 1 = size of written data 115: * addr[2] the value remains the same after it was set by mode = 0 (ignored) 116: * the modified values are returned in addr. 117: * addr[0] Flash ROM write start address 118: * addr[1] start address of write data in RAM 119: * address will be adjusted to the sector boundary of Flash ROM. 120: * addr[2] number of sectors to write 121: * Since writing is done in the unit of sectors, the writing will be done from the sector boundary, 122: * areas immediately before and after the designated area may be part of the write operation. 123: */ 124: EXPORT void setupFlashLoad( W mode, UW addr[3] ) 125: { 126: UW SECMSK = FROM_SECSZ - 1; 127: UW ofs, sa, romsize, ramtop, ramend; 128: const MEMSEG *rom, *ram; 129: 130: /* Flash ROM capacity */ 131: rom = MemArea(MSA_FROM, 1); 132: romsize = rom->end - rom->top; 133: 134: /* RAM area for writing */ 135: ram = MemArea(MSA_OS|MSA_WRK, 1); 136: ramtop = (ram->top + SECMSK) & ~SECMSK; 137: ramend = ram->end & ~SECMSK; 138: 139: /* Use the end of RAM area for working area 140: if we have enough RAM, we set aside the area as large as the last sector */ 141: sa = (ramend - FROM_SECSZ) - romsize; 142: if ( sa < ramtop ) sa = ramtop; 143: ofs = sa - (UW)rom->top; /* the distance between the ROM area and RAM work area */ 144: 145: if ( mode >= 0 ) { 146: /* set up loading */ 147: addr[0] = rom->top + ofs; /* RAM address lower limit */ 148: addr[1] = rom->end + ofs - 1; /* RAM address upper limit */ 149: addr[2] = ofs; /* offset */ 150: if ( addr[1] >= ramend ) addr[1] = ramend - 1; 151: } else { 152: /* set up writing */ 153: sa = addr[0] & ~SECMSK; /* RAM start address */ 154: addr[2] = ((addr[1] & ~SECMSK) - sa) / FROM_SECSZ + 1; 155: /* number of sectors */ 156: addr[1] = sa; /* RAM start address */ 157: addr[0] = sa - ofs; /* ROM start address */ 158: } 159: }