1 /* SPI Flash PROM JTAG programming algorithms
2 
3 Copyright (C) 2009 Uwe Bonnes
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19 Code based on http://code.google.com/p/xilprg-hunz/
20 
21 */
22 
23 #ifndef  PROGALGSPIFLASH_H
24 #define  PROGALGSPIFLASH_H
25 
26 #include <stdio.h>
27 #include <string>
28 #include <stdint.h>
29 
30 #include "bitfile.h"
31 #include "jtag.h"
32 
33 typedef unsigned char byte;
34 
35 class ProgAlgSPIFlash
36 {
37  private:
38   static const byte USER1;
39   static const byte USER2;
40   static const byte CFG_IN;
41   static const byte JSHUTDOWN;
42   static const byte JSTART;
43   static const byte CONFIG;
44   static const byte BYPASS;
45 
46   Jtag *jtag;
47   FILE *fp_dbg;
48   unsigned int pgsize;
49   unsigned int pages;
50   unsigned int pages_per_sector;
51   unsigned int pages_per_block;
52   int sector_size;
53   int sector_erase_cmd;
54   int manf_id;
55   int prod_id;
56   byte *miso_buf;
57   byte *mosi_buf;
58   byte *buf;
59 
60   int xc_user(byte *in, byte *out, int len);
61   int spi_xfer_user1(uint8_t *last_miso, int miso_len, int miso_skip,
62 		     const uint8_t *mosi, int mosi_len, int preamble);
63   int spi_flashinfo_s33 (unsigned char * fbuf);
64   int spi_flashinfo_amic (unsigned char * fbuf);
65   int spi_flashinfo_amic_quad (unsigned char * fbuf);
66   int spi_flashinfo_w25 (unsigned char * fbuf);
67   int spi_flashinfo_at45(unsigned char * fbuf);
68   int spi_flashinfo_m25p(unsigned char * fbuf);
69   int spi_flashinfo_mx25l(unsigned char * fbuf);
70   int spi_flashinfo_sst(unsigned char * fbuf);
71   int wait(byte command, int report, int limit, double *delta);
72   int wait(byte command, byte mask, byte value, int report, int limit, double *delta);
73   int program_at45(BitFile &file);
74   int program_sst(BitFile &pfile);
75   void sst_disable_write_protect();
76   int sectorerase_and_program(BitFile &file);
77   int erase_at45();
78   int erase_bulk();
79   int erase_sst();
80   int sst_has_completed();
81  public:
82   ProgAlgSPIFlash(Jtag &j);
83   ~ProgAlgSPIFlash(void);
84   int spi_flashinfo(void);
85   int erase(void);
86   int program(BitFile &file);
87   int verify(BitFile &file);
88   int read(BitFile &file);
disable()89   void disable(){};
90   void test(int test_count);
91 };
92 #endif /*PROGALGSPIFLASH_H */
93