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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [host/] [flashdrvr.cpp] - Blame information for rev 48

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

Line No. Rev Author Line
1 8 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    flashdrvr.cpp
4
//
5
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6
//
7
// Purpose:     Flash driver.  Encapsulate writing to the flash device.
8
//
9
// Creator:     Dan Gisselquist
10
//              Gisselquist Tecnology, LLC
11
//
12
////////////////////////////////////////////////////////////////////////////////
13
//
14 45 dgisselq
// Copyright (C) 2016-2017, Gisselquist Technology, LLC
15 8 dgisselq
//
16
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20
//
21
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26 45 dgisselq
// You should have received a copy of the GNU General Public License along
27
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
28
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31 8 dgisselq
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
#include <stdio.h>
39
#include <stdlib.h>
40 45 dgisselq
#include <stdint.h>
41 8 dgisselq
#include <unistd.h>
42
#include <strings.h>
43
#include <ctype.h>
44
#include <string.h>
45
#include <signal.h>
46
#include <assert.h>
47
 
48 11 dgisselq
#include "devbus.h"
49 8 dgisselq
#include "regdefs.h"
50
#include "flashdrvr.h"
51 45 dgisselq
#include "byteswap.h"
52 8 dgisselq
 
53
const   bool    HIGH_SPEED = false;
54
 
55
void    FLASHDRVR::flwait(void) {
56
        DEVBUS::BUSW    v;
57
 
58
        v = m_fpga->readio(R_QSPI_EREG);
59
        if ((v&ERASEFLAG)==0)
60
                return;
61
        m_fpga->writeio(R_ICONTROL, ISPIF_DIS);
62
        m_fpga->clear();
63
        m_fpga->writeio(R_ICONTROL, ISPIF_EN);
64
 
65
        do {
66
                // Start by checking that we are still erasing.  The interrupt
67
                // may have been generated while we were setting things up and
68
                // disabling things, so this just double checks for us.  If
69
                // the interrupt was tripped, we're done.  If not, we can now
70
                // wait for an interrupt.
71
                v = m_fpga->readio(R_QSPI_EREG);
72
                if (v&ERASEFLAG) {
73
                        m_fpga->usleep(400);
74
                        if (m_fpga->poll()) {
75
                                m_fpga->clear();
76
                                m_fpga->writeio(R_ICONTROL, ISPIF_EN);
77
                        }
78
                }
79
        } while(v & ERASEFLAG);
80
}
81
 
82
bool    FLASHDRVR::erase_sector(const unsigned sector, const bool verify_erase) {
83 45 dgisselq
        DEVBUS::BUSW    page[SZPAGEW];
84 8 dgisselq
 
85 45 dgisselq
        if (m_debug) printf("Erasing sector: %08x\n", sector);
86 8 dgisselq
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
87 45 dgisselq
        m_fpga->writeio(R_QSPI_EREG, ERASEFLAG + (sector>>2));
88 8 dgisselq
 
89
        // If we're in high speed mode and we want to verify the erase, then
90
        // we can skip waiting for the erase to complete by issueing a read
91
        // command immediately.  As soon as the erase completes the read will
92
        // begin sending commands back.  This allows us to recover the lost 
93
        // time between the interrupt and the next command being received.
94
        if  ((!HIGH_SPEED)||(!verify_erase)) {
95
                flwait();
96
 
97 45 dgisselq
                if (m_debug) {
98
                        printf("@%08x -> %08x\n", R_QSPI_EREG,
99 8 dgisselq
                                m_fpga->readio(R_QSPI_EREG));
100 45 dgisselq
                        printf("@%08x -> %08x\n", R_QSPI_SREG,
101
                                        m_fpga->readio(R_QSPI_SREG));
102
                        printf("@%08x -> %08x\n", sector,
103 8 dgisselq
                                m_fpga->readio(sector));
104 45 dgisselq
                }
105 8 dgisselq
        }
106
 
107
        // Now, let's verify that we erased the sector properly
108
        if (verify_erase) {
109
                for(int i=0; i<NPAGES; i++) {
110 45 dgisselq
                        m_fpga->readi(sector+i*SZPAGEW, SZPAGEW, page);
111
                        for(int i=0; i<SZPAGEW; i++)
112 8 dgisselq
                                if (page[i] != 0xffffffff)
113
                                        return false;
114
                }
115
        }
116
 
117
        return true;
118
}
119
 
120 45 dgisselq
bool    FLASHDRVR::page_program(const unsigned addr, const unsigned len,
121
                const char *data, const bool verify_write) {
122
        DEVBUS::BUSW    buf[SZPAGEW], bswapd[SZPAGEW];
123 8 dgisselq
 
124
        assert(len > 0);
125 45 dgisselq
        assert(len <= PGLENB);
126 8 dgisselq
        assert(PAGEOF(addr)==PAGEOF(addr+len-1));
127
 
128
        if (len <= 0)
129
                return true;
130
 
131 45 dgisselq
        bool    empty_page = true;
132
        for(unsigned i=0; i<len; i+=4) {
133
                DEVBUS::BUSW v;
134
                v = buildword((const unsigned char *)&data[i]);
135
                bswapd[(i>>2)] = v;
136
                if (v != 0xffffffff)
137
                        empty_page = false;
138
        }
139 8 dgisselq
 
140 45 dgisselq
        if (!empty_page) {
141
                // Write the page
142
                m_fpga->writeio(R_ICONTROL, ISPIF_DIS);
143
                m_fpga->clear();
144
                m_fpga->writeio(R_ICONTROL, ISPIF_EN);
145
                printf("Writing page: 0x%08x - 0x%08x\r", addr, addr+len-1);
146
                m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
147
                m_fpga->writei(addr, (len>>2), bswapd);
148
                fflush(stdout);
149
 
150
                // If we're in high speed mode and we want to verify the write,
151
                // then we can skip waiting for the write to complete by
152
                // issueing a read command immediately.  As soon as the write
153
                // completes the read will begin sending commands back.  This
154
                // allows us to recover the lost time between the interrupt and
155
                // the next command being received.
156
                flwait();
157
        }
158 11 dgisselq
        // if ((!HIGH_SPEED)||(!verify_write)) { }
159
        if (verify_write) {
160
                // printf("Attempting to verify page\n");
161 8 dgisselq
                // NOW VERIFY THE PAGE
162 45 dgisselq
                m_fpga->readi(addr, len>>2, buf);
163
                for(unsigned i=0; i<(len>>2); i++) {
164
                        if (buf[i] != bswapd[i]) {
165
                                printf("\nVERIFY FAILS[%d]: %08x\n", i, (i<<2)+addr);
166 8 dgisselq
                                printf("\t(Flash[%d]) %08x != %08x (Goal[%08x])\n",
167 45 dgisselq
                                        (i<<2), buf[i], bswapd[i], (i<<2)+addr);
168 8 dgisselq
                                return false;
169
                        }
170 11 dgisselq
                } // printf("\nVerify success\n");
171 8 dgisselq
        } return true;
172
}
173
 
174
bool    FLASHDRVR::write(const unsigned addr, const unsigned len,
175 45 dgisselq
                const char *data, const bool verify) {
176 8 dgisselq
        // Work through this one sector at a time.
177
        // If this buffer is equal to the sector value(s), go on
178
        // If not, erase the sector
179
 
180 45 dgisselq
        for(unsigned s=SECTOROF(addr); s<SECTOROF(addr+len+SECTORSZB-1);
181
                        s+=SECTORSZB) {
182 8 dgisselq
                // Do we need to erase?
183 45 dgisselq
                bool    need_erase = false, need_program = false;
184 8 dgisselq
                unsigned newv = 0; // (s<addr)?addr:s;
185
                {
186 45 dgisselq
                        char *sbuf = new char[SECTORSZB];
187
                        const char *dp; // pointer to our "desired" buffer
188 8 dgisselq
                        unsigned        base,ln;
189 45 dgisselq
 
190 8 dgisselq
                        base = (addr>s)?addr:s;
191 45 dgisselq
                        ln=((addr+len>s+SECTORSZB)?(s+SECTORSZB):(addr+len))-base;
192
                        m_fpga->readi(base, ln>>2, (uint32_t *)sbuf);
193
                        byteswapbuf(ln>>2, (uint32_t *)sbuf);
194 8 dgisselq
 
195
                        dp = &data[base-addr];
196
                        for(unsigned i=0; i<ln; i++) {
197
                                if ((sbuf[i]&dp[i]) != dp[i]) {
198 45 dgisselq
                                        if (m_debug) {
199
                                                printf("\nNEED-ERASE @0x%08x ... %08x != %08x (Goal)\n",
200
                                                        i+base-addr, sbuf[i], dp[i]);
201
                                        }
202 8 dgisselq
                                        need_erase = true;
203 45 dgisselq
                                        newv = (i&-4)+base;
204 8 dgisselq
                                        break;
205 45 dgisselq
                                } else if ((sbuf[i] != dp[i])&&(newv == 0))
206
                                        newv = (i&-4)+base;
207 8 dgisselq
                        }
208
                }
209
 
210
                if (newv == 0)
211
                        continue; // This sector already matches
212
 
213 45 dgisselq
                // Erase the sector if necessary
214
                if (!need_erase) {
215
                        if (m_debug) printf("NO ERASE NEEDED\n");
216
                } else {
217 14 dgisselq
                        printf("ERASING SECTOR: %08x\n", s);
218 11 dgisselq
                        if (!erase_sector(s, verify)) {
219
                                printf("SECTOR ERASE FAILED!\n");
220
                                return false;
221
                        } newv = (s<addr) ? addr : s;
222 8 dgisselq
                }
223 45 dgisselq
 
224
                // Now walk through all of our pages in this sector and write
225
                // to them.
226
                for(unsigned p=newv; (p<s+SECTORSZB)&&(p<addr+len); p=PAGEOF(p+PGLENB)) {
227 11 dgisselq
                        unsigned start = p, len = addr+len-start;
228
 
229
                        // BUT! if we cross page boundaries, we need to clip
230
                        // our results to the page boundary
231
                        if (PAGEOF(start+len-1)!=PAGEOF(start))
232 45 dgisselq
                                len = PAGEOF(start+PGLENB)-start;
233
                        if (!page_program(start, len, &data[p-addr], verify)) {
234 8 dgisselq
                                printf("WRITE-PAGE FAILED!\n");
235
                                return false;
236 11 dgisselq
                        }
237 45 dgisselq
                } if ((need_erase)||(need_program))
238
                        printf("Sector 0x%08x: DONE%15s\n", s, "");
239 8 dgisselq
        }
240
 
241 45 dgisselq
        m_fpga->writeio(R_QSPI_EREG, ENABLEWP); // Re-enable write protection
242 8 dgisselq
 
243
        return true;
244
}
245
 

powered by: WebSVN 2.1.0

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