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

Subversion Repositories openarty

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

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
        printf("Erasing sector: %08x\n", sector);
88
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
89 14 dgisselq
        SETSCOPE;
90 4 dgisselq
        m_fpga->writeio(R_QSPI_EREG, ERASEFLAG + sector);
91
 
92
        // If we're in high speed mode and we want to verify the erase, then
93
        // we can skip waiting for the erase to complete by issueing a read
94
        // command immediately.  As soon as the erase completes the read will
95
        // begin sending commands back.  This allows us to recover the lost 
96
        // time between the interrupt and the next command being received.
97
        if  ((!HIGH_SPEED)||(!verify_erase)) {
98
                flwait();
99
 
100
                printf("@%08x -> %08x\n", R_QSPI_EREG,
101
                                m_fpga->readio(R_QSPI_EREG));
102 14 dgisselq
                printf("@%08x -> %08x\n", R_QSPI_STAT,
103
                                m_fpga->readio(R_QSPI_STAT));
104 4 dgisselq
                printf("@%08x -> %08x\n", sector,
105
                                m_fpga->readio(sector));
106
        }
107
 
108
        // Now, let's verify that we erased the sector properly
109
        if (verify_erase) {
110
                for(int i=0; i<NPAGES; i++) {
111 14 dgisselq
                        m_fpga->readi(sector+i*SZPAGEW, SZPAGEW, page);
112
                        for(int i=0; i<SZPAGEW; i++)
113 4 dgisselq
                                if (page[i] != 0xffffffff)
114
                                        return false;
115
                }
116
        }
117
 
118
        return true;
119
}
120
 
121
bool    FLASHDRVR::write_page(const unsigned addr, const unsigned len,
122
                const unsigned *data, const bool verify_write) {
123 14 dgisselq
        DEVBUS::BUSW    buf[SZPAGEW];
124 4 dgisselq
 
125
        assert(len > 0);
126 14 dgisselq
        assert(len <= PGLENW);
127 4 dgisselq
        assert(PAGEOF(addr)==PAGEOF(addr+len-1));
128
 
129
        if (len <= 0)
130
                return true;
131
 
132
        // Write the page
133
        m_fpga->writeio(R_ICONTROL, ISPIF_DIS);
134
        m_fpga->clear();
135
        m_fpga->writeio(R_ICONTROL, ISPIF_EN);
136
        printf("Writing page: 0x%08x - 0x%08x\n", addr, addr+len-1);
137
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
138 14 dgisselq
        SETSCOPE;
139 4 dgisselq
        m_fpga->writei(addr, len, data);
140
 
141
        // If we're in high speed mode and we want to verify the write, then
142
        // we can skip waiting for the write to complete by issueing a read
143
        // command immediately.  As soon as the write completes the read will
144
        // begin sending commands back.  This allows us to recover the lost 
145
        // time between the interrupt and the next command being received.
146
        flwait();
147
        // if ((!HIGH_SPEED)||(!verify_write)) { }
148
        if (verify_write) {
149
                // printf("Attempting to verify page\n");
150
                // NOW VERIFY THE PAGE
151
                m_fpga->readi(addr, len, buf);
152
                for(int i=0; i<len; i++) {
153
                        if (buf[i] != data[i]) {
154
                                printf("\nVERIFY FAILS[%d]: %08x\n", i, i+addr);
155
                                printf("\t(Flash[%d]) %08x != %08x (Goal[%08x])\n",
156
                                        i, buf[i], data[i], i+addr);
157
                                return false;
158
                        }
159
                } // printf("\nVerify success\n");
160
        } return true;
161
}
162
 
163
bool    FLASHDRVR::write(const unsigned addr, const unsigned len,
164
                const unsigned *data, const bool verify) {
165
        // Work through this one sector at a time.
166
        // If this buffer is equal to the sector value(s), go on
167
        // If not, erase the sector
168
 
169
        /*
170
        fprintf(stderr, "FLASH->write(%08x, %d, ..., %s)\n", addr, len,
171
                        (verify)?"Verify":"");
172
        */
173
        // m_fpga->writeio(R_QSPI_CREG, 2);
174
        // m_fpga->readio(R_VERSION);   // Read something innocuous
175
 
176
        // Just to make sure the driver knows that these values are ...
177
        // m_fpga->readio(R_QSPI_CREG);
178
        // m_fpga->readio(R_QSPI_SREG);
179
        // Because the status register may invoke protections here, we
180
        // void them.
181
        // m_fpga->writeio(R_QSPI_SREG, 0);
182
        // m_fpga->readio(R_VERSION);   // Read something innocuous
183
 
184 14 dgisselq
        for(unsigned s=SECTOROF(addr); s<SECTOROF(addr+len+SECTORSZW-1); s+=SECTORSZW) {
185 4 dgisselq
                // printf("IN LOOP, s=%08x\n", s);
186
                // Do we need to erase?
187
                bool    need_erase = false;
188
                unsigned newv = 0; // (s<addr)?addr:s;
189
                {
190 14 dgisselq
                        DEVBUS::BUSW    *sbuf = new DEVBUS::BUSW[SECTORSZW];
191 4 dgisselq
                        const DEVBUS::BUSW *dp;
192
                        unsigned        base,ln;
193
                        base = (addr>s)?addr:s;
194 14 dgisselq
                        ln=((addr+len>s+SECTORSZW)?(s+SECTORSZW):(addr+len))-base;
195 4 dgisselq
                        m_fpga->readi(base, ln, sbuf);
196
 
197
                        dp = &data[base-addr];
198
                        for(unsigned i=0; i<ln; i++) {
199
                                if ((sbuf[i]&dp[i]) != dp[i]) {
200
                                        printf("\nNEED-ERASE @0x%08x ... %08x != %08x (Goal)\n",
201
                                                i+base-addr, sbuf[i], dp[i]);
202
                                        need_erase = true;
203
                                        newv = i+base;
204
                                        break;
205
                                } else if ((sbuf[i] != dp[i])&&(newv == 0)) {
206
                                        // if (newv == 0)
207
                                                // printf("MEM[%08x] = %08x (!= %08x (Goal))\n",
208
                                                        // i+base, sbuf[i], dp[i]);
209
                                        newv = i+base;
210
                                }
211
                        }
212
                }
213
 
214
                if (newv == 0)
215
                        continue; // This sector already matches
216
 
217
                // Just erase anyway
218
                if (!need_erase)
219
                        printf("NO ERASE NEEDED\n");
220
                else {
221
                        printf("ERASING SECTOR: %08x\n", s);
222
                        if (!erase_sector(s, verify)) {
223
                                printf("SECTOR ERASE FAILED!\n");
224
                                return false;
225
                        } newv = (s<addr) ? addr : s;
226
                }
227 14 dgisselq
                for(unsigned p=newv; (p<s+SECTORSZW)&&(p<addr+len); p=PAGEOF(p+PGLENW)) {
228 4 dgisselq
                        unsigned start = p, len = addr+len-start;
229
 
230
                        // BUT! if we cross page boundaries, we need to clip
231
                        // our results to the page boundary
232
                        if (PAGEOF(start+len-1)!=PAGEOF(start))
233 14 dgisselq
                                len = PAGEOF(start+PGLENW)-start;
234 4 dgisselq
                        if (!write_page(start, len, &data[p-addr], verify)) {
235
                                printf("WRITE-PAGE FAILED!\n");
236
                                return false;
237
                        }
238
                }
239
        }
240
 
241 14 dgisselq
        m_fpga->writeio(R_QSPI_EREG, ENABLEWP); // Re-enable write protection
242 4 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.