/* * JR-IDE Project * - (c) 2017 Alan Hightower * * This program is free software: 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "netpi.h" int verbose = 1; #include #include int flash_wake (void) { uint8_t cmd[5]; memset (cmd, 0, sizeof (cmd)); cmd[0] = 0xab; if (spi_xfer (cmd, sizeof (cmd)) != sizeof (cmd)) return -1; return cmd[4]; } int flash_write_enable (void) { uint8_t cmd = 0x06; return (spi_xfer (&cmd, 1) == 1) ? 0 : 1; } int flash_busy (void) { uint8_t cmd[2] = { 0x05, 0x00 }; return (spi_xfer (cmd, 2) == 2) ? (cmd[1] & 0x01) : -1; } int flash_status (void) { uint8_t cmd[4] = { 0x05, 0x00, 0x35, 0x00 }; if (spi_xfer (&cmd[0], 2) != 2) return -1; if (spi_xfer (&cmd[2], 2) != 2) return -1; return (((int) cmd[3]) << 8) | cmd[1]; } int flash_erase (void) { int rc; if ((rc = flash_write_enable ()) < 0) return rc; uint8_t cmd = 0xc7; if (spi_xfer (&cmd, 1) != 1) return -1; do { rc = flash_busy(); } while (rc > 0); return rc; } int flash_program (uint32_t address, uint8_t *page) { int rc; if ((rc = flash_write_enable ()) < 0) return rc; uint8_t cmd[260]; cmd[0] = 0x02; cmd[1] = (address >> 16) & 0xff; cmd[2] = (address >> 8) & 0xff; cmd[3] = (address) & 0xff; memcpy (&cmd[4], page, 256); if (spi_xfer (&cmd, 260) != 260) return -1; do { rc = flash_busy(); } while (rc > 0); return rc; } int cpld_reset (int value) { int fd = open ("/sys/class/gpio/gpio25/value", O_WRONLY); if (fd < 0) return fd; char buffer = value ? '0' : '1'; write (fd, &buffer, 1); close (fd); return 0; } int main (int argc, char *argv[]) { if (spi_init (0, 8000000)) return 0; cpld_reset (1); flash_wake (); printf ("flash status %04x\n", flash_status()); flash_erase(); // 01 write status regiser // 03 read data // 02 page program // 52 block erase 32 // d8 block erate 64 uint8_t data[128 * 1024]; memset (data, 0, sizeof (data)); int fd = open ("rtl/icestorm/bridge.bin", O_RDONLY); read (fd, data, sizeof (data)); close (fd); int i, address, pages = sizeof (data) / 256; for (i = 0, address = 0; i < pages; i++, address += 256) { printf ("page %d\n", i); flash_program (address, &data[address]); } spi_term (); cpld_reset (0); return 0; }