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