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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [host/] [flashdrvr.cpp] - Blame information for rev 30

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

Line No. Rev Author Line
1 14 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 4 dgisselq
//
3
// Filename:    flashdrvr.cpp
4
//
5 14 dgisselq
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6 4 dgisselq
//
7 14 dgisselq
// Purpose:     Flash driver.  Encapsulates the erasing and programming (i.e.
8
//              writing) necessary to set the values in a flash device.
9 4 dgisselq
//
10 14 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
11
//              Gisselquist Technology, LLC
12 4 dgisselq
//
13 14 dgisselq
////////////////////////////////////////////////////////////////////////////////
14 4 dgisselq
//
15 14 dgisselq
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
16 4 dgisselq
//
17 14 dgisselq
// This program is free software (firmware): you can redistribute it and/or
18
// modify it under the terms of  the GNU General Public License as published
19
// by the Free Software Foundation, either version 3 of the License, or (at
20
// your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful, but WITHOUT
23
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
24
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
// for more details.
26
//
27
// You should have received a copy of the GNU General Public License along
28
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
29
// target there if the PDF file isn't present.)  If not, see
30
// <http://www.gnu.org/licenses/> for a copy.
31
//
32
// License:     GPL, v3, as defined and found on www.gnu.org,
33
//              http://www.gnu.org/licenses/gpl.html
34
//
35
//
36
////////////////////////////////////////////////////////////////////////////////
37
//
38
//
39 4 dgisselq
#include <stdio.h>
40
#include <stdlib.h>
41
#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
#include "port.h"
49
#include "regdefs.h"
50
#include "flashdrvr.h"
51
 
52
const   bool    HIGH_SPEED = false;
53
 
54 14 dgisselq
#define SETSCOPE m_fpga->writeio(R_QSCOPE, 8180)
55
 
56
 
57 4 dgisselq
void    FLASHDRVR::flwait(void) {
58
        DEVBUS::BUSW    v;
59
 
60
        v = m_fpga->readio(R_QSPI_EREG);
61
        if ((v&ERASEFLAG)==0)
62
                return;
63
        m_fpga->writeio(R_ICONTROL, ISPIF_DIS);
64
        m_fpga->clear();
65
        m_fpga->writeio(R_ICONTROL, ISPIF_EN);
66
 
67
        do {
68
                // Start by checking that we are still erasing.  The interrupt
69
                // may have been generated while we were setting things up and
70
                // disabling things, so this just double checks for us.  If
71
                // the interrupt was tripped, we're done.  If not, we can now
72
                // wait for an interrupt.
73
                v = m_fpga->readio(R_QSPI_EREG);
74
                if (v&ERASEFLAG) {
75
                        m_fpga->usleep(400);
76
                        if (m_fpga->poll()) {
77
                                m_fpga->clear();
78
                                m_fpga->writeio(R_ICONTROL, ISPIF_EN);
79
                        }
80
                }
81
        } while(v & ERASEFLAG);
82
}
83
 
84
bool    FLASHDRVR::erase_sector(const unsigned sector, const bool verify_erase) {
85 14 dgisselq
        DEVBUS::BUSW    page[SZPAGEW];
86 4 dgisselq
 
87 18 dgisselq
        printf("EREG before   : %08x\n", m_fpga->readio(R_QSPI_EREG));
88 4 dgisselq
        printf("Erasing sector: %08x\n", sector);
89
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
90 18 dgisselq
        printf("EREG with WEL : %08x\n", m_fpga->readio(R_QSPI_EREG));
91 14 dgisselq
        SETSCOPE;
92 4 dgisselq
        m_fpga->writeio(R_QSPI_EREG, ERASEFLAG + sector);
93 18 dgisselq
        printf("EREG after    : %08x\n", m_fpga->readio(R_QSPI_EREG));
94 4 dgisselq
 
95
        // If we're in high speed mode and we want to verify the erase, then
96
        // we can skip waiting for the erase to complete by issueing a read
97
        // command immediately.  As soon as the erase completes the read will
98
        // begin sending commands back.  This allows us to recover the lost 
99
        // time between the interrupt and the next command being received.
100
        if  ((!HIGH_SPEED)||(!verify_erase)) {
101
                flwait();
102
 
103
                printf("@%08x -> %08x\n", R_QSPI_EREG,
104
                                m_fpga->readio(R_QSPI_EREG));
105 14 dgisselq
                printf("@%08x -> %08x\n", R_QSPI_STAT,
106
                                m_fpga->readio(R_QSPI_STAT));
107 4 dgisselq
                printf("@%08x -> %08x\n", sector,
108
                                m_fpga->readio(sector));
109
        }
110
 
111
        // Now, let's verify that we erased the sector properly
112
        if (verify_erase) {
113
                for(int i=0; i<NPAGES; i++) {
114 14 dgisselq
                        m_fpga->readi(sector+i*SZPAGEW, SZPAGEW, page);
115
                        for(int i=0; i<SZPAGEW; i++)
116 4 dgisselq
                                if (page[i] != 0xffffffff)
117
                                        return false;
118
                }
119
        }
120
 
121
        return true;
122
}
123
 
124
bool    FLASHDRVR::write_page(const unsigned addr, const unsigned len,
125
                const unsigned *data, const bool verify_write) {
126 14 dgisselq
        DEVBUS::BUSW    buf[SZPAGEW];
127 4 dgisselq
 
128
        assert(len > 0);
129 14 dgisselq
        assert(len <= PGLENW);
130 4 dgisselq
        assert(PAGEOF(addr)==PAGEOF(addr+len-1));
131
 
132
        if (len <= 0)
133
                return true;
134
 
135
        // Write the page
136
        m_fpga->writeio(R_ICONTROL, ISPIF_DIS);
137
        m_fpga->clear();
138
        m_fpga->writeio(R_ICONTROL, ISPIF_EN);
139
        printf("Writing page: 0x%08x - 0x%08x\n", addr, addr+len-1);
140
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
141 14 dgisselq
        SETSCOPE;
142 4 dgisselq
        m_fpga->writei(addr, len, data);
143
 
144
        // If we're in high speed mode and we want to verify the write, then
145
        // we can skip waiting for the write to complete by issueing a read
146
        // command immediately.  As soon as the write completes the read will
147
        // begin sending commands back.  This allows us to recover the lost 
148
        // time between the interrupt and the next command being received.
149
        flwait();
150
        // if ((!HIGH_SPEED)||(!verify_write)) { }
151
        if (verify_write) {
152
                // printf("Attempting to verify page\n");
153
                // NOW VERIFY THE PAGE
154
                m_fpga->readi(addr, len, buf);
155
                for(int i=0; i<len; i++) {
156
                        if (buf[i] != data[i]) {
157
                                printf("\nVERIFY FAILS[%d]: %08x\n", i, i+addr);
158
                                printf("\t(Flash[%d]) %08x != %08x (Goal[%08x])\n",
159
                                        i, buf[i], data[i], i+addr);
160
                                return false;
161
                        }
162
                } // printf("\nVerify success\n");
163
        } return true;
164
}
165
 
166 18 dgisselq
#define VCONF_VALUE     0x8b
167
 
168
bool    FLASHDRVR::verify_config(void) {
169
        unsigned cfg = m_fpga->readio(R_QSPI_VCONF);
170
        printf("CFG = %02x\n", cfg);
171
        return (cfg == VCONF_VALUE);
172
}
173
 
174
void    FLASHDRVR::set_config(void) {
175
        // There is some delay associated with these commands, but it should
176
        // be dwarfed by the communication delay.  If you wish to do this on the
177
        // device itself, you may need to use some timers.
178
        //
179
        // Set the write-enable latch
180
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
181
        // Set the volatile configuration register
182
        m_fpga->writeio(R_QSPI_VCONF, VCONF_VALUE);
183
        // Clear the write-enable latch, since it didn't clear automatically
184
        printf("EREG = %08x\n", m_fpga->readio(R_QSPI_EREG));
185
        m_fpga->writeio(R_QSPI_EREG, ENABLEWP);
186
}
187
 
188 4 dgisselq
bool    FLASHDRVR::write(const unsigned addr, const unsigned len,
189
                const unsigned *data, const bool verify) {
190 18 dgisselq
 
191
        if (!verify_config()) {
192
                set_config();
193 30 dgisselq
                if (!verify_config()) {
194
                        printf("Invalid configuration, cannot program flash\n");
195 18 dgisselq
                        return false;
196 30 dgisselq
                }
197 18 dgisselq
        }
198
 
199 4 dgisselq
        // Work through this one sector at a time.
200
        // If this buffer is equal to the sector value(s), go on
201
        // If not, erase the sector
202
 
203
        // m_fpga->writeio(R_QSPI_CREG, 2);
204
        // m_fpga->readio(R_VERSION);   // Read something innocuous
205
 
206
        // Just to make sure the driver knows that these values are ...
207
        // m_fpga->readio(R_QSPI_CREG);
208
        // m_fpga->readio(R_QSPI_SREG);
209
        // Because the status register may invoke protections here, we
210
        // void them.
211
        // m_fpga->writeio(R_QSPI_SREG, 0);
212
        // m_fpga->readio(R_VERSION);   // Read something innocuous
213
 
214 14 dgisselq
        for(unsigned s=SECTOROF(addr); s<SECTOROF(addr+len+SECTORSZW-1); s+=SECTORSZW) {
215 4 dgisselq
                // printf("IN LOOP, s=%08x\n", s);
216
                // Do we need to erase?
217
                bool    need_erase = false;
218
                unsigned newv = 0; // (s<addr)?addr:s;
219
                {
220 14 dgisselq
                        DEVBUS::BUSW    *sbuf = new DEVBUS::BUSW[SECTORSZW];
221 4 dgisselq
                        const DEVBUS::BUSW *dp;
222
                        unsigned        base,ln;
223
                        base = (addr>s)?addr:s;
224 14 dgisselq
                        ln=((addr+len>s+SECTORSZW)?(s+SECTORSZW):(addr+len))-base;
225 4 dgisselq
                        m_fpga->readi(base, ln, sbuf);
226
 
227
                        dp = &data[base-addr];
228 30 dgisselq
                        SETSCOPE;
229 4 dgisselq
                        for(unsigned i=0; i<ln; i++) {
230
                                if ((sbuf[i]&dp[i]) != dp[i]) {
231
                                        printf("\nNEED-ERASE @0x%08x ... %08x != %08x (Goal)\n",
232
                                                i+base-addr, sbuf[i], dp[i]);
233
                                        need_erase = true;
234
                                        newv = i+base;
235
                                        break;
236
                                } else if ((sbuf[i] != dp[i])&&(newv == 0)) {
237
                                        // if (newv == 0)
238
                                                // printf("MEM[%08x] = %08x (!= %08x (Goal))\n",
239
                                                        // i+base, sbuf[i], dp[i]);
240
                                        newv = i+base;
241
                                }
242
                        }
243
                }
244
 
245
                if (newv == 0)
246
                        continue; // This sector already matches
247
 
248
                // Just erase anyway
249
                if (!need_erase)
250
                        printf("NO ERASE NEEDED\n");
251
                else {
252
                        printf("ERASING SECTOR: %08x\n", s);
253
                        if (!erase_sector(s, verify)) {
254
                                printf("SECTOR ERASE FAILED!\n");
255
                                return false;
256
                        } newv = (s<addr) ? addr : s;
257
                }
258 14 dgisselq
                for(unsigned p=newv; (p<s+SECTORSZW)&&(p<addr+len); p=PAGEOF(p+PGLENW)) {
259 4 dgisselq
                        unsigned start = p, len = addr+len-start;
260
 
261
                        // BUT! if we cross page boundaries, we need to clip
262
                        // our results to the page boundary
263
                        if (PAGEOF(start+len-1)!=PAGEOF(start))
264 14 dgisselq
                                len = PAGEOF(start+PGLENW)-start;
265 4 dgisselq
                        if (!write_page(start, len, &data[p-addr], verify)) {
266
                                printf("WRITE-PAGE FAILED!\n");
267
                                return false;
268
                        }
269
                }
270
        }
271
 
272 14 dgisselq
        m_fpga->writeio(R_QSPI_EREG, ENABLEWP); // Re-enable write protection
273 4 dgisselq
 
274
        return true;
275
}
276
 

powered by: WebSVN 2.1.0

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