OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [devs/] [flash/] [intel/] [strata/] [current/] [src/] [flash_program_buf.c] - Blame information for rev 868

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      flash_program_buf.c
4
//
5
//      Flash programming
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    gthomas, hmt
43
// Contributors: gthomas
44
// Date:         2001-02-14
45
// Purpose:      
46
// Description:  
47
//              
48
//####DESCRIPTIONEND####
49
//
50
//==========================================================================
51
 
52
#include "strata.h"
53
 
54
#include <pkgconf/hal.h>
55
#include <cyg/hal/hal_arch.h>
56
 
57
// Platforms may define this for special handling when accessing the write buffer.
58
#ifndef CYGHWR_FLASH_WRITE_BUF
59
#define CYGHWR_FLASH_WRITE_BUF(a,b) (*(a) = *(b))
60
#endif
61
 
62
int
63
flash_program_buf(volatile flash_t *addr, flash_t *data, int len,
64
                  unsigned long block_mask, int buffer_size)
65
 __attribute__ ((section (".2ram.flash_program_buf")));
66
int
67
flash_program_buf(volatile flash_t *addr, flash_t *data, int len,
68
                  unsigned long block_mask, int buffer_size)
69
{
70
    volatile flash_t *ROM;
71
    volatile flash_t *BA;
72
    flash_t stat = 0;
73
    int timeout = 50000;
74
#ifdef FLASH_Write_Buffer
75
    int i, wc;
76
#endif
77
 
78
    // Get base address and map addresses to virtual addresses
79
    ROM = FLASH_P2V( CYGNUM_FLASH_BASE_MASK & (unsigned int)addr );
80
    BA = addr = FLASH_P2V(addr);
81
 
82
    // Clear any error conditions
83
    ROM[0] = FLASH_Clear_Status;
84
 
85
#ifdef FLASH_Write_Buffer
86
    // Write any big chunks first
87
    while (len >= buffer_size) {
88
        wc = buffer_size;
89
        if (wc > len) wc = len;
90
        len -= wc;
91
        // convert 'wc' in bytes to 'wc' in 'flash_t' 
92
        wc = wc / sizeof(flash_t);  // Word count
93
        *BA = FLASH_Write_Buffer;
94
        timeout = 5000000;
95
        while(((stat = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
96
            if (--timeout == 0) {
97
                goto bad;
98
            }
99
            *BA = FLASH_Write_Buffer;
100
        }
101
        *BA = FLASHWORD(wc-1);  // Count is 0..N-1
102
        for (i = 0;  i < wc;  i++) {
103
#ifdef CYGHWR_FLASH_WRITE_ELEM
104
            CYGHWR_FLASH_WRITE_ELEM(addr+i, data+i);
105
#else
106
            CYGHWR_FLASH_WRITE_BUF(addr+i, data+i);
107
#endif
108
        }
109
        *BA = FLASH_Confirm;
110
 
111
        ROM[0] = FLASH_Read_Status;
112
        timeout = 5000000;
113
        while(((stat = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
114
            if (--timeout == 0) {
115
                goto bad;
116
            }
117
        }
118
        // Jump out if there was an error
119
        if (stat & FLASH_ErrorMask) {
120
            goto bad;
121
        }
122
        // And verify the data - also increments the pointers.
123
        *BA = FLASH_Reset;
124
        for (i = 0;  i < wc;  i++) {
125
            if ( *addr++ != *data++ ) {
126
                stat = FLASH_ErrorNotVerified;
127
                goto bad;
128
            }
129
        }
130
    }
131
#endif
132
 
133
    while (len > 0) {
134
        BA[0] = FLASH_Program;
135
#ifdef CYGHWR_FLASH_WRITE_ELEM
136
        CYGHWR_FLASH_WRITE_ELEM(addr, data);
137
#else
138
        *addr = *data;
139
#endif
140
        timeout = 5000000;
141
        while(((stat = BA[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
142
            if (--timeout == 0) {
143
                goto bad;
144
            }
145
        }
146
        if (stat & FLASH_ErrorMask) {
147
            break;
148
        }
149
        BA[0] = FLASH_Reset;
150
        if (*addr++ != *data++) {
151
            stat = FLASH_ErrorNotVerified;
152
            break;
153
        }
154
        len -= sizeof( flash_t );
155
    }
156
 
157
    // Restore ROM to "normal" mode
158
 bad:
159
    BA[0] = FLASH_Reset;
160
 
161
    return stat;
162
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.