]>
Commit | Line | Data |
---|---|---|
1b1c914f UH |
1 | ## |
2 | ## This file is part of the sigrok project. | |
3 | ## | |
9b4d8a57 | 4 | ## Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de> |
1b1c914f UH |
5 | ## |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program; if not, write to the Free Software | |
18 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | ## | |
20 | ||
21 | # | |
22 | # Macronix MX25Lxx05D SPI (NOR) flash chip decoder. | |
23 | # Works for MX25L1605D/MX25L3205D/MX25L6405D. | |
24 | # | |
25 | ||
26 | # | |
27 | # TODO: Description | |
28 | # | |
29 | # Details: | |
30 | # http://www.macronix.com/QuickPlace/hq/PageLibrary4825740B00298A3B.nsf/h_Index/3F21BAC2E121E17848257639003A3146/$File/MX25L1605D-3205D-6405D-1.5.pdf | |
31 | # | |
32 | ||
677d597b | 33 | import sigrokdecode as srd |
1b1c914f UH |
34 | |
35 | # States | |
36 | IDLE = -1 | |
37 | ||
38 | # Chip commands (also used as additional decoder states). | |
39 | CMD_WREN = 0x06 | |
40 | CMD_WRDI = 0x04 | |
41 | CMD_RDID = 0x9f | |
42 | CMD_RDSR = 0x05 | |
43 | CMD_WRSR = 0x01 | |
44 | CMD_READ = 0x03 | |
45 | CMD_FAST_READ = 0x0b | |
46 | CMD_2READ = 0xbb | |
47 | CMD_SE = 0x20 | |
48 | CMD_BE = 0xd8 | |
49 | CMD_CE = 0x60 | |
50 | CMD_CE2 = 0xc7 | |
51 | CMD_PP = 0x02 | |
52 | CMD_CP = 0xad | |
53 | CMD_DP = 0xb9 | |
54 | # CMD_RDP = 0xab | |
55 | # CMD_RES = 0xab | |
56 | CMD_RDP_RES = 0xab # Note: RDP/RES have the same ID. | |
57 | CMD_REMS = 0x90 | |
58 | CMD_REMS2 = 0xef | |
59 | CMD_ENSO = 0xb1 | |
60 | CMD_EXSO = 0xc1 | |
61 | CMD_RDSCUR = 0x2b | |
62 | CMD_WRSCUR = 0x2f | |
63 | CMD_ESRY = 0x70 | |
64 | CMD_DSRY = 0x80 | |
65 | ||
66 | # TODO: (Short) command names as strings in a dict, too? | |
67 | ||
68 | # Dict which maps command IDs to their description. | |
69 | cmds = { | |
70 | CMD_WREN: 'Write enable', | |
71 | CMD_WRDI: 'Write disable', | |
72 | CMD_RDID: 'Read identification', | |
73 | CMD_RDSR: 'Read status register', | |
74 | CMD_WRSR: 'Write status register', | |
75 | CMD_READ: 'Read data', | |
76 | CMD_FAST_READ: 'Fast read data', | |
77 | CMD_2READ: '2x I/O read', | |
78 | CMD_SE: 'Sector erase', | |
79 | CMD_BE: 'Block erase', | |
80 | CMD_CE: 'Chip erase', | |
81 | CMD_CE2: 'Chip erase', # Alternative command ID | |
82 | CMD_PP: 'Page program', | |
83 | CMD_CP: 'Continuously program mode', | |
84 | CMD_DP: 'Deep power down', | |
85 | # CMD_RDP: 'Release from deep powerdown', | |
86 | # CMD_RES: 'Read electronic ID', | |
87 | CMD_RDP_RES: 'Release from deep powerdown / Read electronic ID', | |
88 | CMD_REMS: 'Read electronic manufacturer & device ID', | |
89 | CMD_REMS2: 'Read ID for 2x I/O mode', | |
90 | CMD_ENSO: 'Enter secured OTP', | |
91 | CMD_EXSO: 'Exit secured OTP', | |
92 | CMD_RDSCUR: 'Read security register', | |
93 | CMD_WRSCUR: 'Write security register', | |
94 | CMD_ESRY: 'Enable SO to output RY/BY#', | |
95 | CMD_DSRY: 'Disable SO to output RY/BY#', | |
96 | } | |
97 | ||
98 | device_name = { | |
99 | 0x14: 'MX25L1605D', | |
100 | 0x15: 'MX25L3205D', | |
101 | 0x16: 'MX25L6405D', | |
102 | } | |
103 | ||
677d597b | 104 | class Decoder(srd.Decoder): |
a2c2afd9 | 105 | api_version = 1 |
1b1c914f | 106 | id = 'mx25lxx05d' |
9a12a6e7 | 107 | name = 'MX25Lxx05D' |
3d3da57d | 108 | longname = 'Macronix MX25Lxx05D' |
1b1c914f UH |
109 | desc = 'Macronix MX25Lxx05D SPI flash chip decoder' |
110 | longdesc = 'TODO' | |
1b1c914f | 111 | license = 'gplv2+' |
385508e9 | 112 | inputs = ['spi', 'logic'] |
1b1c914f | 113 | outputs = ['mx25lxx05d'] |
385508e9 | 114 | probes = [] |
b77614bc | 115 | optional_probes = [ |
385508e9 UH |
116 | {'id': 'hold', 'name': 'HOLD#', 'desc': 'TODO.'}, |
117 | {'id': 'wp_acc', 'name': 'WP#/ACC', 'desc': 'TODO.'}, | |
118 | ] | |
1b1c914f | 119 | options = {} # TODO |
9b4d8a57 UH |
120 | annotations = [ |
121 | ['TODO', 'TODO'], | |
122 | ] | |
1b1c914f UH |
123 | |
124 | def __init__(self, **kwargs): | |
1b1c914f UH |
125 | self.state = IDLE |
126 | self.cmdstate = 1 # TODO | |
1b1c914f UH |
127 | |
128 | def start(self, metadata): | |
56202222 UH |
129 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mx25lxx05d') |
130 | self.out_ann = self.add(srd.OUTPUT_ANN, 'mx25lxx05d') | |
1b1c914f UH |
131 | |
132 | def report(self): | |
133 | pass | |
134 | ||
385508e9 | 135 | def putx(self, data): |
9b4d8a57 UH |
136 | # Simplification, most annotations span extactly one SPI byte/packet. |
137 | self.put(self.ss, self.es, self.out_ann, data) | |
138 | ||
139 | def handle_wren(self, mosi, miso): | |
385508e9 | 140 | self.putx([0, ['Command: %s' % cmds[self.cmd]]]) |
1b1c914f UH |
141 | self.state = IDLE |
142 | ||
143 | # TODO: Check/display device ID / name | |
9b4d8a57 | 144 | def handle_rdid(self, mosi, miso): |
1b1c914f UH |
145 | if self.cmdstate == 1: |
146 | # Byte 1: Master sends command ID. | |
9b4d8a57 | 147 | self.start_sample = self.ss |
385508e9 | 148 | self.putx([0, ['Command: %s' % cmds[self.cmd]]]) |
1b1c914f UH |
149 | elif self.cmdstate == 2: |
150 | # Byte 2: Slave sends the JEDEC manufacturer ID. | |
385508e9 | 151 | self.putx([0, ['Manufacturer ID: 0x%02x' % miso]]) |
1b1c914f UH |
152 | elif self.cmdstate == 3: |
153 | # Byte 3: Slave sends the memory type (0x20 for this chip). | |
385508e9 | 154 | self.putx([0, ['Memory type: 0x%02x' % miso]]) |
1b1c914f UH |
155 | elif self.cmdstate == 4: |
156 | # Byte 4: Slave sends the device ID. | |
9b4d8a57 | 157 | self.device_id = miso |
385508e9 | 158 | self.putx([0, ['Device ID: 0x%02x' % miso]]) |
1b1c914f UH |
159 | |
160 | if self.cmdstate == 4: | |
161 | # TODO: Check self.device_id is valid & exists in device_names. | |
162 | # TODO: Same device ID? Check! | |
9b4d8a57 UH |
163 | d = 'Device: Macronix %s' % device_name[self.device_id] |
164 | self.put(self.start_sample, self.es, self.out_ann, [0, [d]]) | |
1b1c914f UH |
165 | self.state = IDLE |
166 | else: | |
167 | self.cmdstate += 1 | |
168 | ||
1b1c914f UH |
169 | # TODO: Warn/abort if we don't see the necessary amount of bytes. |
170 | # TODO: Warn if WREN was not seen before. | |
9b4d8a57 | 171 | def handle_se(self, mosi, miso): |
1b1c914f UH |
172 | if self.cmdstate == 1: |
173 | # Byte 1: Master sends command ID. | |
174 | self.addr = 0 | |
9b4d8a57 | 175 | self.start_sample = self.ss |
385508e9 | 176 | self.putx([0, ['Command: %s' % cmds[self.cmd]]]) |
1b1c914f UH |
177 | elif self.cmdstate in (2, 3, 4): |
178 | # Bytes 2/3/4: Master sends address of the sector to erase. | |
179 | # Note: Assumes SPI data is 8 bits wide (it is for MX25Lxx05D). | |
180 | # TODO: LSB-first of MSB-first? | |
181 | self.addr <<= 8 | |
9b4d8a57 | 182 | self.addr |= mosi |
385508e9 | 183 | self.putx([0, ['Address byte %d: 0x%02x' % (self.cmdstate - 1, |
9b4d8a57 | 184 | miso)]]) # TODO: Count from 0 or 1? |
1b1c914f UH |
185 | |
186 | if self.cmdstate == 4: | |
9b4d8a57 UH |
187 | d = 'Erase sector %d' % self.addr |
188 | self.put(self.start_sample, self.es, self.out_ann, [0, [d]]) | |
1b1c914f UH |
189 | # TODO: Max. size depends on chip, check that too if possible. |
190 | if self.addr % 4096 != 0: | |
191 | # Sector addresses must be 4K-aligned (same for all 3 chips). | |
9b4d8a57 UH |
192 | d = 'Warning: Invalid sector address!' # TODO: type == WARN? |
193 | self.put(self.start_sample, self.es, self.out_ann, [0, [d]]) | |
1b1c914f UH |
194 | self.state = IDLE |
195 | else: | |
196 | self.cmdstate += 1 | |
197 | ||
9b4d8a57 | 198 | def handle_rems(self, mosi, miso): |
1b1c914f UH |
199 | if self.cmdstate == 1: |
200 | # Byte 1: Master sends command ID. | |
9b4d8a57 | 201 | self.start_sample = self.ss |
385508e9 | 202 | self.putx([0, ['Command: %s' % cmds[self.cmd]]]) |
1b1c914f UH |
203 | elif self.cmdstate in (2, 3): |
204 | # Bytes 2/3: Master sends two dummy bytes. | |
205 | # TODO: Check dummy bytes? Check reply from device? | |
385508e9 | 206 | self.putx([0, ['Dummy byte: %s' % mosi]]) |
1b1c914f UH |
207 | elif self.cmdstate == 4: |
208 | # Byte 4: Master sends 0x00 or 0x01. | |
209 | # 0x00: Master wants manufacturer ID as first reply byte. | |
210 | # 0x01: Master wants device ID as first reply byte. | |
9b4d8a57 UH |
211 | self.manufacturer_id_first = True if (mosi == 0x00) else False |
212 | d = 'manufacturer' if (mosi == 0x00) else 'device' | |
385508e9 | 213 | self.putx([0, ['Master wants %s ID first' % d]]) |
1b1c914f UH |
214 | elif self.cmdstate == 5: |
215 | # Byte 5: Slave sends manufacturer ID (or device ID). | |
9b4d8a57 UH |
216 | self.ids = [miso] |
217 | d = 'Manufacturer' if self.manufacturer_id_first else 'Device' | |
385508e9 | 218 | self.putx([0, ['%s ID' % d]]) |
9b4d8a57 | 219 | elif self.cmdstate == 6: |
1b1c914f | 220 | # Byte 6: Slave sends device ID (or manufacturer ID). |
9b4d8a57 UH |
221 | self.ids += [miso] |
222 | d = 'Manufacturer' if self.manufacturer_id_first else 'Device' | |
385508e9 | 223 | self.putx([0, ['%s ID' % d]]) |
1b1c914f UH |
224 | else: |
225 | # TODO: Error? | |
226 | pass | |
227 | ||
228 | if self.cmdstate == 6: | |
9b4d8a57 | 229 | self.end_sample = self.es |
1b1c914f | 230 | id = self.ids[1] if self.manufacturer_id_first else self.ids[0] |
385508e9 | 231 | self.putx([0, ['Device: Macronix %s' % device_name[id]]]) |
1b1c914f UH |
232 | self.state = IDLE |
233 | else: | |
234 | self.cmdstate += 1 | |
235 | ||
9b4d8a57 | 236 | def handle_rdsr(self, mosi, miso): |
385508e9 | 237 | self.putx([0, ['Command: %s (0x%02x)' % (cmds[self.cmd], miso)]]) |
9b4d8a57 | 238 | self.state = IDLE |
1b1c914f | 239 | |
2b9837d9 | 240 | def decode(self, ss, es, data): |
1b1c914f | 241 | |
9b4d8a57 | 242 | ptype, mosi, miso = data |
1b1c914f | 243 | |
9b4d8a57 UH |
244 | if ptype != 'data': |
245 | return | |
1b1c914f | 246 | |
9b4d8a57 UH |
247 | cmd = mosi |
248 | self.ss = ss | |
249 | self.es = es | |
1b1c914f | 250 | |
9b4d8a57 UH |
251 | # If we encountered a known chip command, enter the resp. state. |
252 | if self.state == IDLE: | |
253 | if cmd in cmds: | |
254 | self.state = cmd | |
255 | self.cmd = cmd # TODO: Eliminate? | |
256 | self.cmdstate = 1 | |
1b1c914f | 257 | else: |
9b4d8a57 | 258 | pass # TODO |
1b1c914f | 259 | |
9b4d8a57 UH |
260 | # Handle commands. |
261 | # TODO: Use some generic way to invoke the resp. method. | |
262 | if self.state == CMD_WREN: | |
263 | self.handle_wren(mosi, miso) | |
264 | elif self.state == CMD_SE: | |
265 | self.handle_se(mosi, miso) | |
266 | elif self.state == CMD_RDID: | |
267 | self.handle_rdid(mosi, miso) | |
268 | if self.state == CMD_REMS: | |
269 | self.handle_rems(mosi, miso) | |
270 | if self.state == CMD_RDSR: | |
271 | self.handle_rdsr(mosi, miso) | |
272 | else: | |
273 | self.put(0, 0, self.out_ann, [0, ['Unknown command: 0x%02x' % cmd]]) | |
274 | self.state = IDLE | |
1b1c914f | 275 |