| Line 10... | Line 10... | 
      
        | // Creator:     Dan Gisselquist, Ph.D.
 | // Creator:     Dan Gisselquist, Ph.D.
 | 
      
        | //              Gisselquist Technology, LLC
 | //              Gisselquist Technology, LLC
 | 
      
        | //
 | //
 | 
      
        | ////////////////////////////////////////////////////////////////////////////////
 | ////////////////////////////////////////////////////////////////////////////////
 | 
      
        | //
 | //
 | 
      
        | // Copyright (C) 2015-2016, Gisselquist Technology, LLC
 | // Copyright (C) 2015-2017, Gisselquist Technology, LLC
 | 
      
        | //
 | //
 | 
      
        | // This program is free software (firmware): you can redistribute it and/or
 | // This program is free software (firmware): you can redistribute it and/or
 | 
      
        | // modify it under the terms of  the GNU General Public License as published
 | // modify it under the terms of  the GNU General Public License as published
 | 
      
        | // by the Free Software Foundation, either version 3 of the License, or (at
 | // by the Free Software Foundation, either version 3 of the License, or (at
 | 
      
        | // your option) any later version.
 | // your option) any later version.
 | 
      
        | Line 23... | Line 23... | 
      
        | // ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
 | // ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
 | 
      
        | // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 | // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 | 
      
        | // for more details.
 | // for more details.
 | 
      
        | //
 | //
 | 
      
        | // You should have received a copy of the GNU General Public License along
 | // You should have received a copy of the GNU General Public License along
 | 
      
        | // with this program.  (It's in the $(ROOT)/doc directory, run make with no
 | // with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
 | 
      
        | // target there if the PDF file isn't present.)  If not, see
 | // target there if the PDF file isn't present.)  If not, see
 | 
      
        | // <http://www.gnu.org/licenses/> for a copy.
 | // <http://www.gnu.org/licenses/> for a copy.
 | 
      
        | //
 | //
 | 
      
        | // License:     GPL, v3, as defined and found on www.gnu.org,
 | // License:     GPL, v3, as defined and found on www.gnu.org,
 | 
      
        | //              http://www.gnu.org/licenses/gpl.html
 | //              http://www.gnu.org/licenses/gpl.html
 | 
      
        | Line 36... | Line 36... | 
      
        | ////////////////////////////////////////////////////////////////////////////////
 | ////////////////////////////////////////////////////////////////////////////////
 | 
      
        | //
 | //
 | 
      
        | //
 | //
 | 
      
        | #include <stdio.h>
 | #include <stdio.h>
 | 
      
        | #include <stdlib.h>
 | #include <stdlib.h>
 | 
      
        |   | #include <stdint.h>
 | 
      
        | #include <unistd.h>
 | #include <unistd.h>
 | 
      
        | #include <strings.h>
 | #include <strings.h>
 | 
      
        | #include <ctype.h>
 | #include <ctype.h>
 | 
      
        | #include <string.h>
 | #include <string.h>
 | 
      
        | #include <signal.h>
 | #include <signal.h>
 | 
      
        | #include <assert.h>
 | #include <assert.h>
 | 
      
        |  
 |  
 | 
      
        | #include "port.h"
 | #include "port.h"
 | 
      
        | #include "regdefs.h"
 | #include "regdefs.h"
 | 
      
        | #include "flashdrvr.h"
 | #include "flashdrvr.h"
 | 
      
        |   | #include "byteswap.h"
 | 
      
        |  
 |  
 | 
      
        | const   bool    HIGH_SPEED = false;
 | const   bool    HIGH_SPEED = false;
 | 
      
        |  
 |  
 | 
      
        | #define SETSCOPE m_fpga->writeio(R_QSCOPE, 8180)
 | #define SETSCOPE
 | 
      
        |   | // #define SETSCOPE m_fpga->writeio(R_QSCOPE, 8180)
 | 
      
        |  
 |  
 | 
      
        |  
 |  
 | 
      
        | void    FLASHDRVR::flwait(void) {
 | void    FLASHDRVR::flwait(void) {
 | 
      
        |         DEVBUS::BUSW    v;
 |         DEVBUS::BUSW    v;
 | 
      
        |  
 |  
 | 
      
        | Line 82... | Line 85... | 
      
        | }
 | }
 | 
      
        |  
 |  
 | 
      
        | bool    FLASHDRVR::erase_sector(const unsigned sector, const bool verify_erase) {
 | bool    FLASHDRVR::erase_sector(const unsigned sector, const bool verify_erase) {
 | 
      
        |         DEVBUS::BUSW    page[SZPAGEW];
 |         DEVBUS::BUSW    page[SZPAGEW];
 | 
      
        |  
 |  
 | 
      
        |         printf("EREG before   : %08x\n", m_fpga->readio(R_QSPI_EREG));
 |         if (m_debug) printf("EREG before   : %08x\n", m_fpga->readio(R_QSPI_EREG));
 | 
      
        |         printf("Erasing sector: %08x\n", sector);
 |         if (m_debug) printf("Erasing sector: %08x\n", sector);
 | 
      
        |         m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
 |         m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
 | 
      
        |         printf("EREG with WEL : %08x\n", m_fpga->readio(R_QSPI_EREG));
 |         if (m_debug) printf("EREG with WEL : %08x\n", m_fpga->readio(R_QSPI_EREG));
 | 
      
        |         SETSCOPE;
 |         SETSCOPE;
 | 
      
        |         m_fpga->writeio(R_QSPI_EREG, ERASEFLAG + sector);
 |         m_fpga->writeio(R_QSPI_EREG, ERASEFLAG + (sector>>2));
 | 
      
        |         printf("EREG after    : %08x\n", m_fpga->readio(R_QSPI_EREG));
 |         if (m_debug) printf("EREG after    : %08x\n", m_fpga->readio(R_QSPI_EREG));
 | 
      
        |  
 |  
 | 
      
        |         // If we're in high speed mode and we want to verify the erase, then
 |         // If we're in high speed mode and we want to verify the erase, then
 | 
      
        |         // we can skip waiting for the erase to complete by issueing a read
 |         // we can skip waiting for the erase to complete by issueing a read
 | 
      
        |         // command immediately.  As soon as the erase completes the read will
 |         // command immediately.  As soon as the erase completes the read will
 | 
      
        |         // begin sending commands back.  This allows us to recover the lost 
 |         // begin sending commands back.  This allows us to recover the lost 
 | 
      
        |         // time between the interrupt and the next command being received.
 |         // time between the interrupt and the next command being received.
 | 
      
        |         if  ((!HIGH_SPEED)||(!verify_erase)) {
 |         if  ((!HIGH_SPEED)||(!verify_erase)) {
 | 
      
        |                 flwait();
 |                 flwait();
 | 
      
        |  
 |  
 | 
      
        |   |                 if (m_debug) {
 | 
      
        |                 printf("@%08x -> %08x\n", R_QSPI_EREG,
 |                 printf("@%08x -> %08x\n", R_QSPI_EREG,
 | 
      
        |                                 m_fpga->readio(R_QSPI_EREG));
 |                                 m_fpga->readio(R_QSPI_EREG));
 | 
      
        |                 printf("@%08x -> %08x\n", R_QSPI_STAT,
 |                 printf("@%08x -> %08x\n", R_QSPI_STAT,
 | 
      
        |                                 m_fpga->readio(R_QSPI_STAT));
 |                                 m_fpga->readio(R_QSPI_STAT));
 | 
      
        |                 printf("@%08x -> %08x\n", sector,
 |                 printf("@%08x -> %08x\n", sector,
 | 
      
        |                                 m_fpga->readio(sector));
 |                                 m_fpga->readio(sector));
 | 
      
        |         }
 |         }
 | 
      
        |   |         }
 | 
      
        |  
 |  
 | 
      
        |         // Now, let's verify that we erased the sector properly
 |         // Now, let's verify that we erased the sector properly
 | 
      
        |         if (verify_erase) {
 |         if (verify_erase) {
 | 
      
        |                 for(int i=0; i<NPAGES; i++) {
 |                 for(int i=0; i<NPAGES; i++) {
 | 
      
        |                         m_fpga->readi(sector+i*SZPAGEW, SZPAGEW, page);
 |                         m_fpga->readi(sector+i*SZPAGEW, SZPAGEW, page);
 | 
      
        | Line 119... | Line 124... | 
      
        |         }
 |         }
 | 
      
        |  
 |  
 | 
      
        |         return true;
 |         return true;
 | 
      
        | }
 | }
 | 
      
        |  
 |  
 | 
      
        | bool    FLASHDRVR::write_page(const unsigned addr, const unsigned len,
 | bool    FLASHDRVR::page_program(const unsigned addr, const unsigned len,
 | 
      
        |                 const unsigned *data, const bool verify_write) {
 |                 const char *data, const bool verify_write) {
 | 
      
        |         DEVBUS::BUSW    buf[SZPAGEW];
 |         DEVBUS::BUSW    buf[SZPAGEW], bswapd[SZPAGEW];
 | 
      
        |  
 |  
 | 
      
        |         assert(len > 0);
 |         assert(len > 0);
 | 
      
        |         assert(len <= PGLENW);
 |         assert(len <= PGLENB);
 | 
      
        |         assert(PAGEOF(addr)==PAGEOF(addr+len-1));
 |         assert(PAGEOF(addr)==PAGEOF(addr+len-1));
 | 
      
        |  
 |  
 | 
      
        |         if (len <= 0)
 |         if (len <= 0)
 | 
      
        |                 return true;
 |                 return true;
 | 
      
        |  
 |  
 | 
      
        |   |         bool    empty_page = true;
 | 
      
        |   |         for(unsigned i=0; i<len; i+=4) {
 | 
      
        |   |                 DEVBUS::BUSW v;
 | 
      
        |   |                 v = buildword((const unsigned char *)&data[i]);
 | 
      
        |   |                 bswapd[(i>>2)] = v;
 | 
      
        |   |                 if (v != 0xffffffff)
 | 
      
        |   |                         empty_page = false;
 | 
      
        |   |         }
 | 
      
        |   |  
 | 
      
        |   |         if (!empty_page) {
 | 
      
        |         // Write the page
 |         // Write the page
 | 
      
        |         m_fpga->writeio(R_ICONTROL, ISPIF_DIS);
 |         m_fpga->writeio(R_ICONTROL, ISPIF_DIS);
 | 
      
        |         m_fpga->clear();
 |         m_fpga->clear();
 | 
      
        |         m_fpga->writeio(R_ICONTROL, ISPIF_EN);
 |         m_fpga->writeio(R_ICONTROL, ISPIF_EN);
 | 
      
        |         printf("Writing page: 0x%08x - 0x%08x\n", addr, addr+len-1);
 |                 printf("Writing page: 0x%08x - 0x%08x\r", addr, addr+len-1);
 | 
      
        |         m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
 |         m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
 | 
      
        |         SETSCOPE;
 |         SETSCOPE;
 | 
      
        |         m_fpga->writei(addr, len, data);
 |                 m_fpga->writei(addr, (len>>2), bswapd);
 | 
      
        |   |                 fflush(stdout);
 | 
      
        |  
 |  
 | 
      
        |         // If we're in high speed mode and we want to verify the write, then
 |                 // If we're in high speed mode and we want to verify the write,
 | 
      
        |         // we can skip waiting for the write to complete by issueing a read
 |                 // then we can skip waiting for the write to complete by
 | 
      
        |         // command immediately.  As soon as the write completes the read will
 |                 // issueing a read command immediately.  As soon as the write
 | 
      
        |         // begin sending commands back.  This allows us to recover the lost 
 |                 // completes the read will begin sending commands back.  This
 | 
      
        |         // time between the interrupt and the next command being received.
 |                 // allows us to recover the lost time between the interrupt and
 | 
      
        |   |                 // the next command being received.
 | 
      
        |         flwait();
 |         flwait();
 | 
      
        |   |         }
 | 
      
        |         // if ((!HIGH_SPEED)||(!verify_write)) { }
 |         // if ((!HIGH_SPEED)||(!verify_write)) { }
 | 
      
        |         if (verify_write) {
 |         if (verify_write) {
 | 
      
        |                 // printf("Attempting to verify page\n");
 |                 // printf("Attempting to verify page\n");
 | 
      
        |                 // NOW VERIFY THE PAGE
 |                 // NOW VERIFY THE PAGE
 | 
      
        |                 m_fpga->readi(addr, len, buf);
 |                 m_fpga->readi(addr, len>>2, buf);
 | 
      
        |                 for(int i=0; i<len; i++) {
 |                 for(unsigned i=0; i<(len>>2); i++) {
 | 
      
        |                         if (buf[i] != data[i]) {
 |                         if (buf[i] != bswapd[i]) {
 | 
      
        |                                 printf("\nVERIFY FAILS[%d]: %08x\n", i, i+addr);
 |                                 printf("\nVERIFY FAILS[%d]: %08x\n", i, (i<<2)+addr);
 | 
      
        |                                 printf("\t(Flash[%d]) %08x != %08x (Goal[%08x])\n",
 |                                 printf("\t(Flash[%d]) %08x != %08x (Goal[%08x])\n",
 | 
      
        |                                         i, buf[i], data[i], i+addr);
 |                                         (i<<2), buf[i], bswapd[i], (i<<2)+addr);
 | 
      
        |                                 return false;
 |                                 return false;
 | 
      
        |                         }
 |                         }
 | 
      
        |                 } // printf("\nVerify success\n");
 |                 } // printf("\nVerify success\n");
 | 
      
        |         } return true;
 |         } return true;
 | 
      
        | }
 | }
 | 
      
        | Line 186... | Line 204... | 
      
        |         printf("EREG = %08x\n", m_fpga->readio(R_QSPI_EREG));
 |         printf("EREG = %08x\n", m_fpga->readio(R_QSPI_EREG));
 | 
      
        |         m_fpga->writeio(R_QSPI_EREG, ENABLEWP);
 |         m_fpga->writeio(R_QSPI_EREG, ENABLEWP);
 | 
      
        | }
 | }
 | 
      
        |  
 |  
 | 
      
        | bool    FLASHDRVR::write(const unsigned addr, const unsigned len,
 | bool    FLASHDRVR::write(const unsigned addr, const unsigned len,
 | 
      
        |                 const unsigned *data, const bool verify) {
 |                 const char *data, const bool verify) {
 | 
      
        |   |  
 | 
      
        |   |         assert(addr >= EQSPIFLASH);
 | 
      
        |   |         assert(addr+len <= EQSPIFLASH + FLASHLEN);
 | 
      
        |  
 |  
 | 
      
        |         if (!verify_config()) {
 |         if (!verify_config()) {
 | 
      
        |                 set_config();
 |                 set_config();
 | 
      
        |                 if (!verify_config()) {
 |                 if (!verify_config()) {
 | 
      
        |                         printf("Invalid configuration, cannot program flash\n");
 |                         printf("Invalid configuration, cannot program flash\n");
 | 
      
        | Line 200... | Line 221... | 
      
        |  
 |  
 | 
      
        |         // Work through this one sector at a time.
 |         // Work through this one sector at a time.
 | 
      
        |         // If this buffer is equal to the sector value(s), go on
 |         // If this buffer is equal to the sector value(s), go on
 | 
      
        |         // If not, erase the sector
 |         // If not, erase the sector
 | 
      
        |  
 |  
 | 
      
        |         // m_fpga->writeio(R_QSPI_CREG, 2);
 |         for(unsigned s=SECTOROF(addr); s<SECTOROF(addr+len+SECTORSZB-1);
 | 
      
        |         // m_fpga->readio(R_VERSION);   // Read something innocuous
 |                         s+=SECTORSZB) {
 | 
      
        |  
 |   | 
      
        |         // Just to make sure the driver knows that these values are ...
 |   | 
      
        |         // m_fpga->readio(R_QSPI_CREG);
 |   | 
      
        |         // m_fpga->readio(R_QSPI_SREG);
 |   | 
      
        |         // Because the status register may invoke protections here, we
 |   | 
      
        |         // void them.
 |   | 
      
        |         // m_fpga->writeio(R_QSPI_SREG, 0);
 |   | 
      
        |         // m_fpga->readio(R_VERSION);   // Read something innocuous
 |   | 
      
        |  
 |   | 
      
        |         for(unsigned s=SECTOROF(addr); s<SECTOROF(addr+len+SECTORSZW-1); s+=SECTORSZW) {
 |   | 
      
        |                 // printf("IN LOOP, s=%08x\n", s);
 |   | 
      
        |                 // Do we need to erase?
 |                 // Do we need to erase?
 | 
      
        |                 bool    need_erase = false;
 |                 bool    need_erase = false, need_program = false;
 | 
      
        |                 unsigned newv = 0; // (s<addr)?addr:s;
 |                 unsigned newv = 0; // (s<addr)?addr:s;
 | 
      
        |                 {
 |                 {
 | 
      
        |                         DEVBUS::BUSW    *sbuf = new DEVBUS::BUSW[SECTORSZW];
 |                         char *sbuf = new char[SECTORSZB];
 | 
      
        |                         const DEVBUS::BUSW *dp;
 |                         const char *dp; // pointer to our "desired" buffer
 | 
      
        |                         unsigned        base,ln;
 |                         unsigned        base,ln;
 | 
      
        |   |  
 | 
      
        |                         base = (addr>s)?addr:s;
 |                         base = (addr>s)?addr:s;
 | 
      
        |                         ln=((addr+len>s+SECTORSZW)?(s+SECTORSZW):(addr+len))-base;
 |                         ln=((addr+len>s+SECTORSZB)?(s+SECTORSZB):(addr+len))-base;
 | 
      
        |                         m_fpga->readi(base, ln, sbuf);
 |                         m_fpga->readi(base, ln>>2, (uint32_t *)sbuf);
 | 
      
        |   |                         byteswapbuf(ln>>2, (uint32_t *)sbuf);
 | 
      
        |  
 |  
 | 
      
        |                         dp = &data[base-addr];
 |                         dp = &data[base-addr];
 | 
      
        |                         SETSCOPE;
 |                         SETSCOPE;
 | 
      
        |                         for(unsigned i=0; i<ln; i++) {
 |                         for(unsigned i=0; i<ln; i++) {
 | 
      
        |                                 if ((sbuf[i]&dp[i]) != dp[i]) {
 |                                 if ((sbuf[i]&dp[i]) != dp[i]) {
 | 
      
        |   |                                         if (m_debug) {
 | 
      
        |                                         printf("\nNEED-ERASE @0x%08x ... %08x != %08x (Goal)\n",
 |                                         printf("\nNEED-ERASE @0x%08x ... %08x != %08x (Goal)\n",
 | 
      
        |                                                 i+base-addr, sbuf[i], dp[i]);
 |                                                 i+base-addr, sbuf[i], dp[i]);
 | 
      
        |   |                                         }
 | 
      
        |                                         need_erase = true;
 |                                         need_erase = true;
 | 
      
        |                                         newv = i+base;
 |                                         newv = (i&-4)+base;
 | 
      
        |                                         break;
 |                                         break;
 | 
      
        |                                 } else if ((sbuf[i] != dp[i])&&(newv == 0)) {
 |                                 } else if ((sbuf[i] != dp[i])&&(newv == 0))
 | 
      
        |                                         // if (newv == 0)
 |                                         newv = (i&-4)+base;
 | 
      
        |                                                 // printf("MEM[%08x] = %08x (!= %08x (Goal))\n",
 |   | 
      
        |                                                         // i+base, sbuf[i], dp[i]);
 |   | 
      
        |                                         newv = i+base;
 |   | 
      
        |                                 }
 |   | 
      
        |                         }
 |                         }
 | 
      
        |                 }
 |                 }
 | 
      
        |  
 |  
 | 
      
        |                 if (newv == 0)
 |                 if (newv == 0)
 | 
      
        |                         continue; // This sector already matches
 |                         continue; // This sector already matches
 | 
      
        |  
 |  
 | 
      
        |                 // Just erase anyway
 |                 // Erase the sector if necessary
 | 
      
        |                 if (!need_erase)
 |                 if (!need_erase) {
 | 
      
        |                         printf("NO ERASE NEEDED\n");
 |                         if (m_debug) printf("NO ERASE NEEDED\n");
 | 
      
        |                 else {
 |                 } else {
 | 
      
        |                         printf("ERASING SECTOR: %08x\n", s);
 |                         printf("ERASING SECTOR: %08x\n", s);
 | 
      
        |                         if (!erase_sector(s, verify)) {
 |                         if (!erase_sector(s, verify)) {
 | 
      
        |                                 printf("SECTOR ERASE FAILED!\n");
 |                                 printf("SECTOR ERASE FAILED!\n");
 | 
      
        |                                 return false;
 |                                 return false;
 | 
      
        |                         } newv = (s<addr) ? addr : s;
 |                         } newv = (s<addr) ? addr : s;
 | 
      
        |                 }
 |                 }
 | 
      
        |                 for(unsigned p=newv; (p<s+SECTORSZW)&&(p<addr+len); p=PAGEOF(p+PGLENW)) {
 |  
 | 
      
        |   |                 // Now walk through all of our pages in this sector and write
 | 
      
        |   |                 // to them.
 | 
      
        |   |                 for(unsigned p=newv; (p<s+SECTORSZB)&&(p<addr+len); p=PAGEOF(p+PGLENB)) {
 | 
      
        |                         unsigned start = p, len = addr+len-start;
 |                         unsigned start = p, len = addr+len-start;
 | 
      
        |  
 |  
 | 
      
        |                         // BUT! if we cross page boundaries, we need to clip
 |                         // BUT! if we cross page boundaries, we need to clip
 | 
      
        |                         // our results to the page boundary
 |                         // our results to the page boundary
 | 
      
        |                         if (PAGEOF(start+len-1)!=PAGEOF(start))
 |                         if (PAGEOF(start+len-1)!=PAGEOF(start))
 | 
      
        |                                 len = PAGEOF(start+PGLENW)-start;
 |                                 len = PAGEOF(start+PGLENB)-start;
 | 
      
        |                         if (!write_page(start, len, &data[p-addr], verify)) {
 |                         if (!page_program(start, len, &data[p-addr], verify)) {
 | 
      
        |                                 printf("WRITE-PAGE FAILED!\n");
 |                                 printf("WRITE-PAGE FAILED!\n");
 | 
      
        |                                 return false;
 |                                 return false;
 | 
      
        |                         }
 |                         }
 | 
      
        |                 }
 |                 } if ((need_erase)||(need_program))
 | 
      
        |   |                         printf("Sector 0x%08x: DONE%15s\n", s, "");
 | 
      
        |         }
 |         }
 | 
      
        |  
 |  
 | 
      
        |         m_fpga->writeio(R_QSPI_EREG, ENABLEWP); // Re-enable write protection
 |         m_fpga->writeio(R_QSPI_EREG, ENABLEWP); // Re-enable write protection
 | 
      
        |  
 |  
 | 
      
        |         return true;
 |         return true;
 |