| 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 |
51 |
dgisselq |
// Copyright (C) 2015-2017, 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 |
51 |
dgisselq |
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
| 29 |
14 |
dgisselq |
// 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 |
51 |
dgisselq |
#include <stdint.h>
|
| 42 |
4 |
dgisselq |
#include <unistd.h>
|
| 43 |
|
|
#include <strings.h>
|
| 44 |
|
|
#include <ctype.h>
|
| 45 |
|
|
#include <string.h>
|
| 46 |
|
|
#include <signal.h>
|
| 47 |
|
|
#include <assert.h>
|
| 48 |
|
|
|
| 49 |
|
|
#include "port.h"
|
| 50 |
|
|
#include "regdefs.h"
|
| 51 |
|
|
#include "flashdrvr.h"
|
| 52 |
51 |
dgisselq |
#include "byteswap.h"
|
| 53 |
4 |
dgisselq |
|
| 54 |
|
|
const bool HIGH_SPEED = false;
|
| 55 |
|
|
|
| 56 |
51 |
dgisselq |
#define SETSCOPE
|
| 57 |
|
|
// #define SETSCOPE m_fpga->writeio(R_QSCOPE, 8180)
|
| 58 |
14 |
dgisselq |
|
| 59 |
|
|
|
| 60 |
4 |
dgisselq |
void FLASHDRVR::flwait(void) {
|
| 61 |
|
|
DEVBUS::BUSW v;
|
| 62 |
|
|
|
| 63 |
|
|
v = m_fpga->readio(R_QSPI_EREG);
|
| 64 |
|
|
if ((v&ERASEFLAG)==0)
|
| 65 |
|
|
return;
|
| 66 |
|
|
m_fpga->writeio(R_ICONTROL, ISPIF_DIS);
|
| 67 |
|
|
m_fpga->clear();
|
| 68 |
|
|
m_fpga->writeio(R_ICONTROL, ISPIF_EN);
|
| 69 |
|
|
|
| 70 |
|
|
do {
|
| 71 |
|
|
// Start by checking that we are still erasing. The interrupt
|
| 72 |
|
|
// may have been generated while we were setting things up and
|
| 73 |
|
|
// disabling things, so this just double checks for us. If
|
| 74 |
|
|
// the interrupt was tripped, we're done. If not, we can now
|
| 75 |
|
|
// wait for an interrupt.
|
| 76 |
|
|
v = m_fpga->readio(R_QSPI_EREG);
|
| 77 |
|
|
if (v&ERASEFLAG) {
|
| 78 |
|
|
m_fpga->usleep(400);
|
| 79 |
|
|
if (m_fpga->poll()) {
|
| 80 |
|
|
m_fpga->clear();
|
| 81 |
|
|
m_fpga->writeio(R_ICONTROL, ISPIF_EN);
|
| 82 |
|
|
}
|
| 83 |
|
|
}
|
| 84 |
|
|
} while(v & ERASEFLAG);
|
| 85 |
|
|
}
|
| 86 |
|
|
|
| 87 |
|
|
bool FLASHDRVR::erase_sector(const unsigned sector, const bool verify_erase) {
|
| 88 |
14 |
dgisselq |
DEVBUS::BUSW page[SZPAGEW];
|
| 89 |
4 |
dgisselq |
|
| 90 |
51 |
dgisselq |
if (m_debug) printf("EREG before : %08x\n", m_fpga->readio(R_QSPI_EREG));
|
| 91 |
|
|
if (m_debug) printf("Erasing sector: %08x\n", sector);
|
| 92 |
4 |
dgisselq |
m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
|
| 93 |
51 |
dgisselq |
if (m_debug) printf("EREG with WEL : %08x\n", m_fpga->readio(R_QSPI_EREG));
|
| 94 |
14 |
dgisselq |
SETSCOPE;
|
| 95 |
51 |
dgisselq |
m_fpga->writeio(R_QSPI_EREG, ERASEFLAG + (sector>>2));
|
| 96 |
|
|
if (m_debug) printf("EREG after : %08x\n", m_fpga->readio(R_QSPI_EREG));
|
| 97 |
4 |
dgisselq |
|
| 98 |
|
|
// If we're in high speed mode and we want to verify the erase, then
|
| 99 |
|
|
// we can skip waiting for the erase to complete by issueing a read
|
| 100 |
|
|
// command immediately. As soon as the erase completes the read will
|
| 101 |
|
|
// begin sending commands back. This allows us to recover the lost
|
| 102 |
|
|
// time between the interrupt and the next command being received.
|
| 103 |
|
|
if ((!HIGH_SPEED)||(!verify_erase)) {
|
| 104 |
|
|
flwait();
|
| 105 |
|
|
|
| 106 |
51 |
dgisselq |
if (m_debug) {
|
| 107 |
|
|
printf("@%08x -> %08x\n", R_QSPI_EREG,
|
| 108 |
4 |
dgisselq |
m_fpga->readio(R_QSPI_EREG));
|
| 109 |
51 |
dgisselq |
printf("@%08x -> %08x\n", R_QSPI_STAT,
|
| 110 |
14 |
dgisselq |
m_fpga->readio(R_QSPI_STAT));
|
| 111 |
51 |
dgisselq |
printf("@%08x -> %08x\n", sector,
|
| 112 |
4 |
dgisselq |
m_fpga->readio(sector));
|
| 113 |
51 |
dgisselq |
}
|
| 114 |
4 |
dgisselq |
}
|
| 115 |
|
|
|
| 116 |
|
|
// Now, let's verify that we erased the sector properly
|
| 117 |
|
|
if (verify_erase) {
|
| 118 |
|
|
for(int i=0; i<NPAGES; i++) {
|
| 119 |
14 |
dgisselq |
m_fpga->readi(sector+i*SZPAGEW, SZPAGEW, page);
|
| 120 |
|
|
for(int i=0; i<SZPAGEW; i++)
|
| 121 |
4 |
dgisselq |
if (page[i] != 0xffffffff)
|
| 122 |
|
|
return false;
|
| 123 |
|
|
}
|
| 124 |
|
|
}
|
| 125 |
|
|
|
| 126 |
|
|
return true;
|
| 127 |
|
|
}
|
| 128 |
|
|
|
| 129 |
51 |
dgisselq |
bool FLASHDRVR::page_program(const unsigned addr, const unsigned len,
|
| 130 |
|
|
const char *data, const bool verify_write) {
|
| 131 |
|
|
DEVBUS::BUSW buf[SZPAGEW], bswapd[SZPAGEW];
|
| 132 |
4 |
dgisselq |
|
| 133 |
|
|
assert(len > 0);
|
| 134 |
51 |
dgisselq |
assert(len <= PGLENB);
|
| 135 |
4 |
dgisselq |
assert(PAGEOF(addr)==PAGEOF(addr+len-1));
|
| 136 |
|
|
|
| 137 |
|
|
if (len <= 0)
|
| 138 |
|
|
return true;
|
| 139 |
|
|
|
| 140 |
51 |
dgisselq |
bool empty_page = true;
|
| 141 |
|
|
for(unsigned i=0; i<len; i+=4) {
|
| 142 |
|
|
DEVBUS::BUSW v;
|
| 143 |
|
|
v = buildword((const unsigned char *)&data[i]);
|
| 144 |
|
|
bswapd[(i>>2)] = v;
|
| 145 |
|
|
if (v != 0xffffffff)
|
| 146 |
|
|
empty_page = false;
|
| 147 |
|
|
}
|
| 148 |
4 |
dgisselq |
|
| 149 |
51 |
dgisselq |
if (!empty_page) {
|
| 150 |
|
|
// Write the page
|
| 151 |
|
|
m_fpga->writeio(R_ICONTROL, ISPIF_DIS);
|
| 152 |
|
|
m_fpga->clear();
|
| 153 |
|
|
m_fpga->writeio(R_ICONTROL, ISPIF_EN);
|
| 154 |
|
|
printf("Writing page: 0x%08x - 0x%08x\r", addr, addr+len-1);
|
| 155 |
|
|
m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
|
| 156 |
|
|
SETSCOPE;
|
| 157 |
|
|
m_fpga->writei(addr, (len>>2), bswapd);
|
| 158 |
|
|
fflush(stdout);
|
| 159 |
|
|
|
| 160 |
|
|
// If we're in high speed mode and we want to verify the write,
|
| 161 |
|
|
// then we can skip waiting for the write to complete by
|
| 162 |
|
|
// issueing a read command immediately. As soon as the write
|
| 163 |
|
|
// completes the read will begin sending commands back. This
|
| 164 |
|
|
// allows us to recover the lost time between the interrupt and
|
| 165 |
|
|
// the next command being received.
|
| 166 |
|
|
flwait();
|
| 167 |
|
|
}
|
| 168 |
4 |
dgisselq |
// if ((!HIGH_SPEED)||(!verify_write)) { }
|
| 169 |
|
|
if (verify_write) {
|
| 170 |
|
|
// printf("Attempting to verify page\n");
|
| 171 |
|
|
// NOW VERIFY THE PAGE
|
| 172 |
51 |
dgisselq |
m_fpga->readi(addr, len>>2, buf);
|
| 173 |
|
|
for(unsigned i=0; i<(len>>2); i++) {
|
| 174 |
|
|
if (buf[i] != bswapd[i]) {
|
| 175 |
|
|
printf("\nVERIFY FAILS[%d]: %08x\n", i, (i<<2)+addr);
|
| 176 |
4 |
dgisselq |
printf("\t(Flash[%d]) %08x != %08x (Goal[%08x])\n",
|
| 177 |
51 |
dgisselq |
(i<<2), buf[i], bswapd[i], (i<<2)+addr);
|
| 178 |
4 |
dgisselq |
return false;
|
| 179 |
|
|
}
|
| 180 |
|
|
} // printf("\nVerify success\n");
|
| 181 |
|
|
} return true;
|
| 182 |
|
|
}
|
| 183 |
|
|
|
| 184 |
18 |
dgisselq |
#define VCONF_VALUE 0x8b
|
| 185 |
49 |
dgisselq |
#define VCONF_VALUE_ALT 0x83
|
| 186 |
18 |
dgisselq |
|
| 187 |
|
|
bool FLASHDRVR::verify_config(void) {
|
| 188 |
|
|
unsigned cfg = m_fpga->readio(R_QSPI_VCONF);
|
| 189 |
49 |
dgisselq |
if (cfg != VCONF_VALUE)
|
| 190 |
|
|
printf("Unexpected volatile configuration = %02x\n", cfg);
|
| 191 |
|
|
return ((cfg == VCONF_VALUE)||(cfg == VCONF_VALUE_ALT));
|
| 192 |
18 |
dgisselq |
}
|
| 193 |
|
|
|
| 194 |
|
|
void FLASHDRVR::set_config(void) {
|
| 195 |
|
|
// There is some delay associated with these commands, but it should
|
| 196 |
|
|
// be dwarfed by the communication delay. If you wish to do this on the
|
| 197 |
|
|
// device itself, you may need to use some timers.
|
| 198 |
|
|
//
|
| 199 |
|
|
// Set the write-enable latch
|
| 200 |
|
|
m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
|
| 201 |
|
|
// Set the volatile configuration register
|
| 202 |
|
|
m_fpga->writeio(R_QSPI_VCONF, VCONF_VALUE);
|
| 203 |
|
|
// Clear the write-enable latch, since it didn't clear automatically
|
| 204 |
|
|
printf("EREG = %08x\n", m_fpga->readio(R_QSPI_EREG));
|
| 205 |
|
|
m_fpga->writeio(R_QSPI_EREG, ENABLEWP);
|
| 206 |
|
|
}
|
| 207 |
|
|
|
| 208 |
4 |
dgisselq |
bool FLASHDRVR::write(const unsigned addr, const unsigned len,
|
| 209 |
51 |
dgisselq |
const char *data, const bool verify) {
|
| 210 |
18 |
dgisselq |
|
| 211 |
51 |
dgisselq |
assert(addr >= EQSPIFLASH);
|
| 212 |
|
|
assert(addr+len <= EQSPIFLASH + FLASHLEN);
|
| 213 |
|
|
|
| 214 |
18 |
dgisselq |
if (!verify_config()) {
|
| 215 |
|
|
set_config();
|
| 216 |
30 |
dgisselq |
if (!verify_config()) {
|
| 217 |
|
|
printf("Invalid configuration, cannot program flash\n");
|
| 218 |
18 |
dgisselq |
return false;
|
| 219 |
30 |
dgisselq |
}
|
| 220 |
18 |
dgisselq |
}
|
| 221 |
|
|
|
| 222 |
4 |
dgisselq |
// Work through this one sector at a time.
|
| 223 |
|
|
// If this buffer is equal to the sector value(s), go on
|
| 224 |
|
|
// If not, erase the sector
|
| 225 |
|
|
|
| 226 |
51 |
dgisselq |
for(unsigned s=SECTOROF(addr); s<SECTOROF(addr+len+SECTORSZB-1);
|
| 227 |
|
|
s+=SECTORSZB) {
|
| 228 |
4 |
dgisselq |
// Do we need to erase?
|
| 229 |
51 |
dgisselq |
bool need_erase = false, need_program = false;
|
| 230 |
4 |
dgisselq |
unsigned newv = 0; // (s<addr)?addr:s;
|
| 231 |
|
|
{
|
| 232 |
51 |
dgisselq |
char *sbuf = new char[SECTORSZB];
|
| 233 |
|
|
const char *dp; // pointer to our "desired" buffer
|
| 234 |
4 |
dgisselq |
unsigned base,ln;
|
| 235 |
51 |
dgisselq |
|
| 236 |
4 |
dgisselq |
base = (addr>s)?addr:s;
|
| 237 |
51 |
dgisselq |
ln=((addr+len>s+SECTORSZB)?(s+SECTORSZB):(addr+len))-base;
|
| 238 |
|
|
m_fpga->readi(base, ln>>2, (uint32_t *)sbuf);
|
| 239 |
|
|
byteswapbuf(ln>>2, (uint32_t *)sbuf);
|
| 240 |
4 |
dgisselq |
|
| 241 |
|
|
dp = &data[base-addr];
|
| 242 |
30 |
dgisselq |
SETSCOPE;
|
| 243 |
4 |
dgisselq |
for(unsigned i=0; i<ln; i++) {
|
| 244 |
|
|
if ((sbuf[i]&dp[i]) != dp[i]) {
|
| 245 |
51 |
dgisselq |
if (m_debug) {
|
| 246 |
|
|
printf("\nNEED-ERASE @0x%08x ... %08x != %08x (Goal)\n",
|
| 247 |
|
|
i+base-addr, sbuf[i], dp[i]);
|
| 248 |
|
|
}
|
| 249 |
4 |
dgisselq |
need_erase = true;
|
| 250 |
51 |
dgisselq |
newv = (i&-4)+base;
|
| 251 |
4 |
dgisselq |
break;
|
| 252 |
51 |
dgisselq |
} else if ((sbuf[i] != dp[i])&&(newv == 0))
|
| 253 |
|
|
newv = (i&-4)+base;
|
| 254 |
4 |
dgisselq |
}
|
| 255 |
|
|
}
|
| 256 |
|
|
|
| 257 |
|
|
if (newv == 0)
|
| 258 |
|
|
continue; // This sector already matches
|
| 259 |
|
|
|
| 260 |
51 |
dgisselq |
// Erase the sector if necessary
|
| 261 |
|
|
if (!need_erase) {
|
| 262 |
|
|
if (m_debug) printf("NO ERASE NEEDED\n");
|
| 263 |
|
|
} else {
|
| 264 |
4 |
dgisselq |
printf("ERASING SECTOR: %08x\n", s);
|
| 265 |
|
|
if (!erase_sector(s, verify)) {
|
| 266 |
|
|
printf("SECTOR ERASE FAILED!\n");
|
| 267 |
|
|
return false;
|
| 268 |
|
|
} newv = (s<addr) ? addr : s;
|
| 269 |
|
|
}
|
| 270 |
51 |
dgisselq |
|
| 271 |
|
|
// Now walk through all of our pages in this sector and write
|
| 272 |
|
|
// to them.
|
| 273 |
|
|
for(unsigned p=newv; (p<s+SECTORSZB)&&(p<addr+len); p=PAGEOF(p+PGLENB)) {
|
| 274 |
4 |
dgisselq |
unsigned start = p, len = addr+len-start;
|
| 275 |
|
|
|
| 276 |
|
|
// BUT! if we cross page boundaries, we need to clip
|
| 277 |
|
|
// our results to the page boundary
|
| 278 |
|
|
if (PAGEOF(start+len-1)!=PAGEOF(start))
|
| 279 |
51 |
dgisselq |
len = PAGEOF(start+PGLENB)-start;
|
| 280 |
|
|
if (!page_program(start, len, &data[p-addr], verify)) {
|
| 281 |
4 |
dgisselq |
printf("WRITE-PAGE FAILED!\n");
|
| 282 |
|
|
return false;
|
| 283 |
|
|
}
|
| 284 |
51 |
dgisselq |
} if ((need_erase)||(need_program))
|
| 285 |
|
|
printf("Sector 0x%08x: DONE%15s\n", s, "");
|
| 286 |
4 |
dgisselq |
}
|
| 287 |
|
|
|
| 288 |
14 |
dgisselq |
m_fpga->writeio(R_QSPI_EREG, ENABLEWP); // Re-enable write protection
|
| 289 |
4 |
dgisselq |
|
| 290 |
|
|
return true;
|
| 291 |
|
|
}
|
| 292 |
|
|
|