]>
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 | ||
156509ca | 21 | # Macronix MX25Lxx05D SPI (NOR) flash chip protocol decoder |
1b1c914f | 22 | |
156509ca | 23 | # Note: Works for MX25L1605D/MX25L3205D/MX25L6405D. |
1b1c914f | 24 | |
677d597b | 25 | import sigrokdecode as srd |
1b1c914f | 26 | |
4772a846 | 27 | # Dict which maps command IDs to their names and descriptions. |
1b1c914f | 28 | cmds = { |
4772a846 UH |
29 | 0x06: ('WREN', 'Write enable'), |
30 | 0x04: ('WRDI', 'Write disable'), | |
31 | 0x9f: ('RDID', 'Read identification'), | |
32 | 0x05: ('RDSR', 'Read status register'), | |
33 | 0x01: ('WRSR', 'Write status register'), | |
34 | 0x03: ('READ', 'Read data'), | |
781ef945 | 35 | 0x0b: ('FAST/READ', 'Fast read data'), |
4772a846 UH |
36 | 0xbb: ('2READ', '2x I/O read'), |
37 | 0x20: ('SE', 'Sector erase'), | |
38 | 0xd8: ('BE', 'Block erase'), | |
39 | 0x60: ('CE', 'Chip erase'), | |
40 | 0xc7: ('CE2', 'Chip erase'), # Alternative command ID | |
41 | 0x02: ('PP', 'Page program'), | |
42 | 0xad: ('CP', 'Continuously program mode'), | |
43 | 0xb9: ('DP', 'Deep power down'), | |
781ef945 | 44 | 0xab: ('RDP/RES', 'Release from deep powerdown / Read electronic ID'), |
4772a846 UH |
45 | 0x90: ('REMS', 'Read electronic manufacturer & device ID'), |
46 | 0xef: ('REMS2', 'Read ID for 2x I/O mode'), | |
47 | 0xb1: ('ENSO', 'Enter secured OTP'), | |
48 | 0xc1: ('EXSO', 'Exit secured OTP'), | |
49 | 0x2b: ('RDSCUR', 'Read security register'), | |
50 | 0x2f: ('WRSCUR', 'Write security register'), | |
51 | 0x70: ('ESRY', 'Enable SO to output RY/BY#'), | |
52 | 0x80: ('DSRY', 'Disable SO to output RY/BY#'), | |
1b1c914f UH |
53 | } |
54 | ||
55 | device_name = { | |
56 | 0x14: 'MX25L1605D', | |
57 | 0x15: 'MX25L3205D', | |
58 | 0x16: 'MX25L6405D', | |
59 | } | |
60 | ||
7cfbf663 UH |
61 | def decode_status_reg(data): |
62 | # TODO: Additional per-bit(s) self.put() calls with correct start/end. | |
63 | ||
64 | # Bits[0:0]: WIP (write in progress) | |
65 | s = 'W' if (data & (1 << 0)) else 'No w' | |
66 | ret = '%srite operation in progress.\n' % s | |
67 | ||
68 | # Bits[1:1]: WEL (write enable latch) | |
69 | s = '' if (data & (1 << 1)) else 'not ' | |
70 | ret += 'Internal write enable latch is %sset.\n' % s | |
71 | ||
72 | # Bits[5:2]: Block protect bits | |
73 | # TODO: More detailed decoding (chip-dependent). | |
74 | ret += 'Block protection bits (BP3-BP0): 0x%x.\n' % ((data & 0x3c) >> 2) | |
75 | ||
76 | # Bits[6:6]: Continuously program mode (CP mode) | |
77 | s = '' if (data & (1 << 6)) else 'not ' | |
78 | ret += 'Device is %sin continuously program mode (CP mode).\n' % s | |
79 | ||
80 | # Bits[7:7]: SRWD (status register write disable) | |
cd287c56 | 81 | s = 'not ' if (data & (1 << 7)) else '' |
7cfbf663 UH |
82 | ret += 'Status register writes are %sallowed.\n' % s |
83 | ||
84 | return ret | |
85 | ||
677d597b | 86 | class Decoder(srd.Decoder): |
a2c2afd9 | 87 | api_version = 1 |
1b1c914f | 88 | id = 'mx25lxx05d' |
9a12a6e7 | 89 | name = 'MX25Lxx05D' |
3d3da57d | 90 | longname = 'Macronix MX25Lxx05D' |
a465436e | 91 | desc = 'SPI (NOR) flash chip protocol.' |
1b1c914f | 92 | license = 'gplv2+' |
385508e9 | 93 | inputs = ['spi', 'logic'] |
1b1c914f | 94 | outputs = ['mx25lxx05d'] |
385508e9 | 95 | probes = [] |
b77614bc | 96 | optional_probes = [ |
385508e9 UH |
97 | {'id': 'hold', 'name': 'HOLD#', 'desc': 'TODO.'}, |
98 | {'id': 'wp_acc', 'name': 'WP#/ACC', 'desc': 'TODO.'}, | |
99 | ] | |
781ef945 | 100 | options = {} |
9b4d8a57 | 101 | annotations = [ |
ee3e279c | 102 | ['Text', 'Human-readable text'], |
173c919c UH |
103 | ['Verbose decode', 'Decoded register bits, read/write data'], |
104 | ['Warnings', 'Human-readable warnings'], | |
9b4d8a57 | 105 | ] |
1b1c914f UH |
106 | |
107 | def __init__(self, **kwargs): | |
4772a846 | 108 | self.state = None |
781ef945 | 109 | self.cmdstate = 1 |
e4022299 UH |
110 | self.addr = 0 |
111 | self.data = [] | |
1b1c914f UH |
112 | |
113 | def start(self, metadata): | |
56202222 UH |
114 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mx25lxx05d') |
115 | self.out_ann = self.add(srd.OUTPUT_ANN, 'mx25lxx05d') | |
1b1c914f UH |
116 | |
117 | def report(self): | |
118 | pass | |
119 | ||
385508e9 | 120 | def putx(self, data): |
ee3e279c | 121 | # Simplification, most annotations span exactly one SPI byte/packet. |
9b4d8a57 UH |
122 | self.put(self.ss, self.es, self.out_ann, data) |
123 | ||
124 | def handle_wren(self, mosi, miso): | |
781ef945 | 125 | self.putx([0, ['Command: %s' % cmds[self.state][1]]]) |
4772a846 | 126 | self.state = None |
1b1c914f | 127 | |
b54936a9 UH |
128 | def handle_wrdi(self, mosi, miso): |
129 | pass # TODO | |
130 | ||
1b1c914f | 131 | # TODO: Check/display device ID / name |
9b4d8a57 | 132 | def handle_rdid(self, mosi, miso): |
1b1c914f UH |
133 | if self.cmdstate == 1: |
134 | # Byte 1: Master sends command ID. | |
9b4d8a57 | 135 | self.start_sample = self.ss |
781ef945 | 136 | self.putx([0, ['Command: %s' % cmds[self.state][1]]]) |
1b1c914f UH |
137 | elif self.cmdstate == 2: |
138 | # Byte 2: Slave sends the JEDEC manufacturer ID. | |
385508e9 | 139 | self.putx([0, ['Manufacturer ID: 0x%02x' % miso]]) |
1b1c914f UH |
140 | elif self.cmdstate == 3: |
141 | # Byte 3: Slave sends the memory type (0x20 for this chip). | |
385508e9 | 142 | self.putx([0, ['Memory type: 0x%02x' % miso]]) |
1b1c914f UH |
143 | elif self.cmdstate == 4: |
144 | # Byte 4: Slave sends the device ID. | |
9b4d8a57 | 145 | self.device_id = miso |
385508e9 | 146 | self.putx([0, ['Device ID: 0x%02x' % miso]]) |
1b1c914f UH |
147 | |
148 | if self.cmdstate == 4: | |
149 | # TODO: Check self.device_id is valid & exists in device_names. | |
150 | # TODO: Same device ID? Check! | |
9b4d8a57 UH |
151 | d = 'Device: Macronix %s' % device_name[self.device_id] |
152 | self.put(self.start_sample, self.es, self.out_ann, [0, [d]]) | |
4772a846 | 153 | self.state = None |
1b1c914f UH |
154 | else: |
155 | self.cmdstate += 1 | |
156 | ||
b54936a9 UH |
157 | def handle_rdsr(self, mosi, miso): |
158 | # Read status register: Master asserts CS#, sends RDSR command, | |
159 | # reads status register byte. If CS# is kept asserted, the status | |
160 | # register can be read continuously / multiple times in a row. | |
161 | # When done, the master de-asserts CS# again. | |
162 | if self.cmdstate == 1: | |
163 | # Byte 1: Master sends command ID. | |
164 | self.putx([0, ['Command: %s' % cmds[self.state][1]]]) | |
165 | elif self.cmdstate >= 2: | |
166 | # Bytes 2-x: Slave sends status register as long as master clocks. | |
167 | if self.cmdstate <= 3: # TODO: While CS# asserted. | |
168 | self.putx([0, ['Status register: 0x%02x' % miso]]) | |
173c919c | 169 | self.putx([1, [decode_status_reg(miso)]]) |
b54936a9 UH |
170 | |
171 | if self.cmdstate == 3: # TODO: If CS# got de-asserted. | |
172 | self.state = None | |
173 | return | |
174 | ||
175 | self.cmdstate += 1 | |
176 | ||
177 | def handle_wrsr(self, mosi, miso): | |
178 | pass # TODO | |
179 | ||
180 | def handle_read(self, mosi, miso): | |
181 | # Read data bytes: Master asserts CS#, sends READ command, sends | |
182 | # 3-byte address, reads >= 1 data bytes, de-asserts CS#. | |
183 | if self.cmdstate == 1: | |
184 | # Byte 1: Master sends command ID. | |
185 | self.putx([0, ['Command: %s' % cmds[self.state][1]]]) | |
186 | elif self.cmdstate in (2, 3, 4): | |
187 | # Bytes 2/3/4: Master sends read address (24bits, MSB-first). | |
188 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) | |
189 | # self.putx([0, ['Read address, byte %d: 0x%02x' % \ | |
190 | # (4 - self.cmdstate, mosi)]]) | |
191 | if self.cmdstate == 4: | |
192 | self.putx([0, ['Read address: 0x%06x' % self.addr]]) | |
193 | self.addr = 0 | |
194 | elif self.cmdstate >= 5: | |
195 | # Bytes 5-x: Master reads data bytes (until CS# de-asserted). | |
196 | # TODO: For now we hardcode 256 bytes per READ command. | |
197 | if self.cmdstate <= 256 + 4: # TODO: While CS# asserted. | |
198 | self.data.append(miso) | |
199 | # self.putx([0, ['New read byte: 0x%02x' % miso]]) | |
200 | ||
201 | if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted. | |
202 | # s = ', '.join(map(hex, self.data)) | |
203 | s = ''.join(map(chr, self.data)) | |
173c919c UH |
204 | self.putx([0, ['Read data']]) |
205 | self.putx([1, ['Read data: %s' % s]]) | |
b54936a9 UH |
206 | self.data = [] |
207 | self.state = None | |
208 | return | |
209 | ||
210 | self.cmdstate += 1 | |
211 | ||
212 | def handle_fast_read(self, mosi, miso): | |
213 | pass # TODO | |
214 | ||
215 | def handle_2read(self, mosi, miso): | |
216 | pass # TODO | |
217 | ||
1b1c914f UH |
218 | # TODO: Warn/abort if we don't see the necessary amount of bytes. |
219 | # TODO: Warn if WREN was not seen before. | |
9b4d8a57 | 220 | def handle_se(self, mosi, miso): |
1b1c914f UH |
221 | if self.cmdstate == 1: |
222 | # Byte 1: Master sends command ID. | |
223 | self.addr = 0 | |
9b4d8a57 | 224 | self.start_sample = self.ss |
781ef945 | 225 | self.putx([0, ['Command: %s' % cmds[self.state][1]]]) |
1b1c914f | 226 | elif self.cmdstate in (2, 3, 4): |
87e574b7 UH |
227 | # Bytes 2/3/4: Master sends sectror address (24bits, MSB-first). |
228 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) | |
229 | # self.putx([0, ['Sector address, byte %d: 0x%02x' % \ | |
230 | # (4 - self.cmdstate, mosi)]]) | |
1b1c914f UH |
231 | |
232 | if self.cmdstate == 4: | |
87e574b7 | 233 | d = 'Erase sector %d (0x%06x)' % (self.addr, self.addr) |
9b4d8a57 | 234 | self.put(self.start_sample, self.es, self.out_ann, [0, [d]]) |
1b1c914f UH |
235 | # TODO: Max. size depends on chip, check that too if possible. |
236 | if self.addr % 4096 != 0: | |
237 | # Sector addresses must be 4K-aligned (same for all 3 chips). | |
173c919c UH |
238 | d = 'Warning: Invalid sector address!' |
239 | self.put(self.start_sample, self.es, self.out_ann, [2, [d]]) | |
4772a846 | 240 | self.state = None |
1b1c914f UH |
241 | else: |
242 | self.cmdstate += 1 | |
243 | ||
b54936a9 UH |
244 | def handle_be(self, mosi, miso): |
245 | pass # TODO | |
246 | ||
247 | def handle_ce(self, mosi, miso): | |
248 | pass # TODO | |
249 | ||
250 | def handle_ce2(self, mosi, miso): | |
251 | pass # TODO | |
252 | ||
253 | def handle_pp(self, mosi, miso): | |
254 | # Page program: Master asserts CS#, sends PP command, sends 3-byte | |
255 | # page address, sends >= 1 data bytes, de-asserts CS#. | |
256 | if self.cmdstate == 1: | |
257 | # Byte 1: Master sends command ID. | |
258 | self.putx([0, ['Command: %s' % cmds[self.state][1]]]) | |
259 | elif self.cmdstate in (2, 3, 4): | |
260 | # Bytes 2/3/4: Master sends page address (24bits, MSB-first). | |
261 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) | |
262 | # self.putx([0, ['Page address, byte %d: 0x%02x' % \ | |
263 | # (4 - self.cmdstate, mosi)]]) | |
264 | if self.cmdstate == 4: | |
265 | self.putx([0, ['Page address: 0x%06x' % self.addr]]) | |
266 | self.addr = 0 | |
267 | elif self.cmdstate >= 5: | |
268 | # Bytes 5-x: Master sends data bytes (until CS# de-asserted). | |
269 | # TODO: For now we hardcode 256 bytes per page / PP command. | |
270 | if self.cmdstate <= 256 + 4: # TODO: While CS# asserted. | |
271 | self.data.append(mosi) | |
272 | # self.putx([0, ['New data byte: 0x%02x' % mosi]]) | |
273 | ||
274 | if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted. | |
275 | # s = ', '.join(map(hex, self.data)) | |
276 | s = ''.join(map(chr, self.data)) | |
173c919c UH |
277 | self.putx([0, ['Page data']]) |
278 | self.putx([1, ['Page data: %s' % s]]) | |
b54936a9 UH |
279 | self.data = [] |
280 | self.state = None | |
281 | return | |
282 | ||
283 | self.cmdstate += 1 | |
284 | ||
285 | def handle_cp(self, mosi, miso): | |
286 | pass # TODO | |
287 | ||
288 | def handle_dp(self, mosi, miso): | |
289 | pass # TODO | |
290 | ||
291 | def handle_rdp_res(self, mosi, miso): | |
292 | pass # TODO | |
293 | ||
9b4d8a57 | 294 | def handle_rems(self, mosi, miso): |
1b1c914f UH |
295 | if self.cmdstate == 1: |
296 | # Byte 1: Master sends command ID. | |
9b4d8a57 | 297 | self.start_sample = self.ss |
781ef945 | 298 | self.putx([0, ['Command: %s' % cmds[self.state][1]]]) |
1b1c914f UH |
299 | elif self.cmdstate in (2, 3): |
300 | # Bytes 2/3: Master sends two dummy bytes. | |
301 | # TODO: Check dummy bytes? Check reply from device? | |
385508e9 | 302 | self.putx([0, ['Dummy byte: %s' % mosi]]) |
1b1c914f UH |
303 | elif self.cmdstate == 4: |
304 | # Byte 4: Master sends 0x00 or 0x01. | |
305 | # 0x00: Master wants manufacturer ID as first reply byte. | |
306 | # 0x01: Master wants device ID as first reply byte. | |
9b4d8a57 UH |
307 | self.manufacturer_id_first = True if (mosi == 0x00) else False |
308 | d = 'manufacturer' if (mosi == 0x00) else 'device' | |
385508e9 | 309 | self.putx([0, ['Master wants %s ID first' % d]]) |
1b1c914f UH |
310 | elif self.cmdstate == 5: |
311 | # Byte 5: Slave sends manufacturer ID (or device ID). | |
9b4d8a57 UH |
312 | self.ids = [miso] |
313 | d = 'Manufacturer' if self.manufacturer_id_first else 'Device' | |
385508e9 | 314 | self.putx([0, ['%s ID' % d]]) |
9b4d8a57 | 315 | elif self.cmdstate == 6: |
1b1c914f | 316 | # Byte 6: Slave sends device ID (or manufacturer ID). |
7f7ea759 | 317 | self.ids.append(miso) |
9b4d8a57 | 318 | d = 'Manufacturer' if self.manufacturer_id_first else 'Device' |
385508e9 | 319 | self.putx([0, ['%s ID' % d]]) |
1b1c914f UH |
320 | |
321 | if self.cmdstate == 6: | |
9b4d8a57 | 322 | self.end_sample = self.es |
1b1c914f | 323 | id = self.ids[1] if self.manufacturer_id_first else self.ids[0] |
385508e9 | 324 | self.putx([0, ['Device: Macronix %s' % device_name[id]]]) |
4772a846 | 325 | self.state = None |
1b1c914f UH |
326 | else: |
327 | self.cmdstate += 1 | |
328 | ||
b54936a9 UH |
329 | def handle_rems2(self, mosi, miso): |
330 | pass # TODO | |
e4022299 | 331 | |
b54936a9 UH |
332 | def handle_enso(self, mosi, miso): |
333 | pass # TODO | |
e4022299 | 334 | |
b54936a9 UH |
335 | def handle_exso(self, mosi, miso): |
336 | pass # TODO | |
e4022299 | 337 | |
b54936a9 UH |
338 | def handle_rdscur(self, mosi, miso): |
339 | pass # TODO | |
e4022299 | 340 | |
b54936a9 UH |
341 | def handle_wrscur(self, mosi, miso): |
342 | pass # TODO | |
e4022299 | 343 | |
b54936a9 UH |
344 | def handle_esry(self, mosi, miso): |
345 | pass # TODO | |
1b1c914f | 346 | |
b54936a9 UH |
347 | def handle_dsry(self, mosi, miso): |
348 | pass # TODO | |
5ebb76fe | 349 | |
2b9837d9 | 350 | def decode(self, ss, es, data): |
1b1c914f | 351 | |
9b4d8a57 | 352 | ptype, mosi, miso = data |
1b1c914f | 353 | |
e4022299 | 354 | # if ptype == 'DATA': |
781ef945 | 355 | # self.putx([0, ['MOSI: 0x%02x, MISO: 0x%02x' % (mosi, miso)]]) |
e4022299 UH |
356 | |
357 | # if ptype == 'CS-CHANGE': | |
358 | # if mosi == 1 and miso == 0: | |
781ef945 | 359 | # self.putx([0, ['Asserting CS#']]) |
e4022299 | 360 | # elif mosi == 0 and miso == 1: |
781ef945 | 361 | # self.putx([0, ['De-asserting CS#']]) |
e4022299 | 362 | |
3e3c0330 | 363 | if ptype != 'DATA': |
9b4d8a57 | 364 | return |
1b1c914f | 365 | |
e4022299 | 366 | self.ss, self.es = ss, es |
1b1c914f | 367 | |
9b4d8a57 | 368 | # If we encountered a known chip command, enter the resp. state. |
4772a846 | 369 | if self.state == None: |
781ef945 UH |
370 | self.state = mosi |
371 | self.cmdstate = 1 | |
1b1c914f | 372 | |
9b4d8a57 | 373 | # Handle commands. |
781ef945 UH |
374 | if self.state in cmds: |
375 | s = 'handle_%s' % cmds[self.state][0].lower().replace('/', '_') | |
376 | handle_reg = getattr(self, s) | |
4772a846 | 377 | handle_reg(mosi, miso) |
9b4d8a57 | 378 | else: |
781ef945 | 379 | self.putx([0, ['Unknown command: 0x%02x' % mosi]]) |
4772a846 | 380 | self.state = None |
1b1c914f | 381 |