Added newly supported boards:
[goodfet] / client / GoodFETSPI.py
1 #!/usr/bin/env python
2 # GoodFET SPI and SPIFlash Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys, time, string, cStringIO, struct, glob, os;
9
10 from GoodFET import GoodFET;
11
12 class GoodFETSPI(GoodFET):
13     APP=0x01;
14     def SPIsetup(self):
15         """Move the FET into the SPI application."""
16         self.writecmd(0x01,0x10,0,self.data); #SPI/SETUP
17         
18     def SPItrans8(self,byte):
19         """Read and write 8 bits by SPI."""
20         data=self.SPItrans([byte]);
21         return ord(data[0]);
22     
23     def SPItrans(self,data):
24         """Exchange data by SPI."""
25         self.data=data;
26         self.writecmd(0x01,0x00,len(data),data);
27         return self.data;
28
29 class GoodFETSPI25C(GoodFETSPI):
30     #opcodes
31     WREN=0x06;
32     WRDI=0x04;
33     RDSR=0x05;
34     WRSR=0x01;
35     READ=0x03;
36     WRITE=0x02;
37     
38     def peek8(self,adr,memory="vn"):
39         """Read a byte from the given address."""
40         data=self.SPItrans([self.READ,(adr>>8)&0xFF,adr&0xFF,0x00]);
41         return ord(data[3]);
42
43
44 class GoodFETSPIFlash(GoodFETSPI):
45     JEDECmanufacturers={0xFF: "MISSING",
46                         0xEF: "Winbond",
47                         0xC2: "MXIC",
48                         0x20: "Numonyx/ST",
49                         0x1F: "Atmel",
50                         0x01: "AMD/Spansion"
51                         };
52
53     JEDECdevices={0xFFFFFF: "MISSING",
54                   0xEF3015: "W25X16L",
55                   0xEF3014: "W25X80L",
56                   0xEF3013: "W25X40L",
57                   0xEF3012: "W25X20L",
58                   0xEF3011: "W25X10L",
59                   0xC22017: "MX25L6405D",
60                   0xC22016: "MX25L3205D",
61                   0xC22015: "MX25L1605D",
62                   0xC22014: "MX25L8005",
63                   0xC22013: "MX25L4005",
64                   0x204011: "M45PE10",
65                   0x202014: "M25P80",
66                   0x1f4501: "AT24DF081",
67                   };
68     
69     JEDECsizes={0x17: 0x800000,
70                 0x16: 0x400000,
71                 0x15: 0x200000,
72                 0x14: 0x100000,
73                 0x13: 0x080000,
74                 0x12: 0x040000,
75                 0x11: 0x020000
76                 };
77     
78     JEDECsize=0;
79
80     def SPIjedec(self):
81         """Grab an SPI Flash ROM's JEDEC bytes.  Some chips don't implement
82         this, such as Numonyx M25P* except in the 110nm processed."""
83         data=[0x9f, 0, 0, 0];
84         data=self.SPItrans(data);
85         jedec=0;
86         self.JEDECmanufacturer=ord(data[1]);
87         if self.JEDECmanufacturer==0xFF:
88             self.JEDECtype=0x20;
89             self.JEDECcapacity=0x14;
90             jedec=0x202014;
91         else:
92             self.JEDECtype=ord(data[2]);
93             self.JEDECcapacity=ord(data[3]);
94             jedec=(ord(data[1])<<16)+(ord(data[2])<<8)+ord(data[3]);
95         self.JEDECsize=self.JEDECsizes.get(self.JEDECcapacity);
96         if self.JEDECsize==None:
97             self.JEDECsize=0;
98         
99         if jedec==0x1F4501:
100             self.JEDECsize=1024**2;
101         self.JEDECdevice=jedec;
102         return data;
103     def SPIpeek(self,adr):
104         """Grab a byte from an SPI Flash ROM."""
105         data=[0x03,
106               (adr&0xFF0000)>>16,
107               (adr&0xFF00)>>8,
108               adr&0xFF,
109               0];
110         self.SPItrans(data);
111         return ord(self.data[4]);
112     def SPIpeekblock(self,adr):
113         """Grab a few block from an SPI Flash ROM.  Block size is unknown"""
114         data=[(adr&0xFF0000)>>16,
115               (adr&0xFF00)>>8,
116               adr&0xFF];
117         
118         self.writecmd(0x01,0x02,3,data);
119         return self.data;
120     
121     def SPIpokebyte(self,adr,val):
122         self.SPIpokebytes(adr,[val]);
123     def SPIpokebytes(self,adr,data):
124         #Used to be 24 bits, BE, not 32 bits, LE.
125         adranddata=[adr&0xFF,
126                     (adr&0xFF00)>>8,
127                     (adr&0xFF0000)>>16,
128                     0, #MSB
129                     ]+data;
130         #print "%06x: poking %i bytes" % (adr,len(data));
131         self.writecmd(0x01,0x03,
132                       len(adranddata),adranddata);
133         
134     def SPIchiperase(self):
135         """Mass erase an SPI Flash ROM."""
136         self.writecmd(0x01,0x81);
137     def SPIwriteenable(self):
138         """SPI Flash Write Enable"""
139         data=[0x06];
140         self.SPItrans(data);
141         
142     def SPIjedecmanstr(self):
143         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
144         man=self.JEDECmanufacturers.get(self.JEDECmanufacturer)
145         if man==0:
146             man="UNKNOWN";
147         return man;
148     
149     def SPIjedecstr(self):
150         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
151         man=self.JEDECmanufacturers.get(self.JEDECmanufacturer);
152         if man==0:
153             man="UNKNOWN";
154         device=self.JEDECdevices.get(self.JEDECdevice);
155         if device==0:
156             device="???"
157         return "%s %s" % (man,device);
158