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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [host/] [flashdrvr.cpp] - Diff between revs 4 and 14

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 4 Rev 14
Line 1... Line 1...
//
////////////////////////////////////////////////////////////////////////////////
//
//
// Filename:    flashdrvr.cpp
// Filename:    flashdrvr.cpp
//
//
// Project:     FPGA library development (Basys3 development board)
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
 
//
 
// Purpose:     Flash driver.  Encapsulates the erasing and programming (i.e.
 
//              writing) necessary to set the values in a flash device.
 
//
 
// Creator:     Dan Gisselquist, Ph.D.
 
//              Gisselquist Technology, LLC
 
//
 
////////////////////////////////////////////////////////////////////////////////
 
//
 
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
//
//
// Purpose:     Flash driver.  Encapsulate writing to the flash device.
// 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
 
// by the Free Software Foundation, either version 3 of the License, or (at
 
// your option) any later version.
//
//
// Creator:     Dan Gisselquist
// This program is distributed in the hope that it will be useful, but WITHOUT
//              Gisselquist Tecnology, LLC
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
 
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
// for more details.
//
//
// Copyright:   2015
// 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
 
// target there if the PDF file isn't present.)  If not, see
 
// <http://www.gnu.org/licenses/> for a copy.
 
//
 
// License:     GPL, v3, as defined and found on www.gnu.org,
 
//              http://www.gnu.org/licenses/gpl.html
 
//
 
//
 
////////////////////////////////////////////////////////////////////////////////
//
//
//
//
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
Line 25... Line 49...
#include "regdefs.h"
#include "regdefs.h"
#include "flashdrvr.h"
#include "flashdrvr.h"
 
 
const   bool    HIGH_SPEED = false;
const   bool    HIGH_SPEED = false;
 
 
 
#define SETSCOPE m_fpga->writeio(R_QSCOPE, 8180)
 
 
 
 
void    FLASHDRVR::flwait(void) {
void    FLASHDRVR::flwait(void) {
        DEVBUS::BUSW    v;
        DEVBUS::BUSW    v;
 
 
        v = m_fpga->readio(R_QSPI_EREG);
        v = m_fpga->readio(R_QSPI_EREG);
        if ((v&ERASEFLAG)==0)
        if ((v&ERASEFLAG)==0)
Line 53... Line 80...
                }
                }
        } while(v & ERASEFLAG);
        } while(v & ERASEFLAG);
}
}
 
 
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[SZPAGE];
        DEVBUS::BUSW    page[SZPAGEW];
 
 
        printf("Erasing sector: %08x\n", sector);
        printf("Erasing sector: %08x\n", sector);
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
 
        SETSCOPE;
        m_fpga->writeio(R_QSPI_EREG, ERASEFLAG + sector);
        m_fpga->writeio(R_QSPI_EREG, ERASEFLAG + sector);
 
 
        // 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
Line 69... Line 97...
        if  ((!HIGH_SPEED)||(!verify_erase)) {
        if  ((!HIGH_SPEED)||(!verify_erase)) {
                flwait();
                flwait();
 
 
                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_SREG,
                printf("@%08x -> %08x\n", R_QSPI_STAT,
                                m_fpga->readio(R_QSPI_SREG));
                                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*SZPAGE, SZPAGE, page);
                        m_fpga->readi(sector+i*SZPAGEW, SZPAGEW, page);
                        for(int i=0; i<SZPAGE; i++)
                        for(int i=0; i<SZPAGEW; i++)
                                if (page[i] != 0xffffffff)
                                if (page[i] != 0xffffffff)
                                        return false;
                                        return false;
                }
                }
        }
        }
 
 
        return true;
        return true;
}
}
 
 
bool    FLASHDRVR::write_page(const unsigned addr, const unsigned len,
bool    FLASHDRVR::write_page(const unsigned addr, const unsigned len,
                const unsigned *data, const bool verify_write) {
                const unsigned *data, const bool verify_write) {
        DEVBUS::BUSW    buf[SZPAGE];
        DEVBUS::BUSW    buf[SZPAGEW];
 
 
        assert(len > 0);
        assert(len > 0);
        assert(len <= PGLEN);
        assert(len <= PGLENW);
        assert(PAGEOF(addr)==PAGEOF(addr+len-1));
        assert(PAGEOF(addr)==PAGEOF(addr+len-1));
 
 
        if (len <= 0)
        if (len <= 0)
                return true;
                return true;
 
 
Line 105... Line 133...
        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\n", addr, addr+len-1);
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
        m_fpga->writeio(R_QSPI_EREG, DISABLEWP);
 
        SETSCOPE;
        m_fpga->writei(addr, len, data);
        m_fpga->writei(addr, len, data);
 
 
        // 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, then
        // we can skip waiting for the write to complete by issueing a read
        // we can skip waiting for the write to complete by issueing a read
        // command immediately.  As soon as the write completes the read will
        // command immediately.  As soon as the write completes the read will
Line 150... Line 179...
        // Because the status register may invoke protections here, we
        // Because the status register may invoke protections here, we
        // void them.
        // void them.
        // m_fpga->writeio(R_QSPI_SREG, 0);
        // m_fpga->writeio(R_QSPI_SREG, 0);
        // m_fpga->readio(R_VERSION);   // Read something innocuous
        // m_fpga->readio(R_VERSION);   // Read something innocuous
 
 
        for(unsigned s=SECTOROF(addr); s<SECTOROF(addr+len+SECTORSZ-1); s+=SECTORSZ) {
        for(unsigned s=SECTOROF(addr); s<SECTOROF(addr+len+SECTORSZW-1); s+=SECTORSZW) {
                // printf("IN LOOP, s=%08x\n", s);
                // 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;
                unsigned newv = 0; // (s<addr)?addr:s;
                unsigned newv = 0; // (s<addr)?addr:s;
                {
                {
                        DEVBUS::BUSW    *sbuf = new DEVBUS::BUSW[SECTORSZ];
                        DEVBUS::BUSW    *sbuf = new DEVBUS::BUSW[SECTORSZW];
                        const DEVBUS::BUSW *dp;
                        const DEVBUS::BUSW *dp;
                        unsigned        base,ln;
                        unsigned        base,ln;
                        base = (addr>s)?addr:s;
                        base = (addr>s)?addr:s;
                        ln=((addr+len>s+SECTORSZ)?(s+SECTORSZ):(addr+len))-base;
                        ln=((addr+len>s+SECTORSZW)?(s+SECTORSZW):(addr+len))-base;
                        m_fpga->readi(base, ln, sbuf);
                        m_fpga->readi(base, ln, sbuf);
 
 
                        dp = &data[base-addr];
                        dp = &data[base-addr];
                        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]) {
Line 193... Line 222...
                        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+SECTORSZ)&&(p<addr+len); p=PAGEOF(p+PGLEN)) {
                for(unsigned p=newv; (p<s+SECTORSZW)&&(p<addr+len); p=PAGEOF(p+PGLENW)) {
                        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+PGLEN)-start;
                                len = PAGEOF(start+PGLENW)-start;
                        if (!write_page(start, len, &data[p-addr], verify)) {
                        if (!write_page(start, len, &data[p-addr], verify)) {
                                printf("WRITE-PAGE FAILED!\n");
                                printf("WRITE-PAGE FAILED!\n");
                                return false;
                                return false;
                        }
                        }
                }
                }
        }
        }
 
 
        m_fpga->writeio(R_QSPI_EREG, 0); // Re-enable write protection
        m_fpga->writeio(R_QSPI_EREG, ENABLEWP); // Re-enable write protection
 
 
        return true;
        return true;
}
}
 
 
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

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