]>
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'), | |
35 | 0x0b: ('FAST_READ', 'Fast read data'), | |
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'), | |
44 | # 0xab: ('RDP', 'Release from deep powerdown'), | |
45 | # 0xab: ('RES', 'Read electronic ID'), | |
46 | 0xab: ('RDP_RES', 'Release from deep powerdown / Read electronic ID'), | |
47 | 0x90: ('REMS', 'Read electronic manufacturer & device ID'), | |
48 | 0xef: ('REMS2', 'Read ID for 2x I/O mode'), | |
49 | 0xb1: ('ENSO', 'Enter secured OTP'), | |
50 | 0xc1: ('EXSO', 'Exit secured OTP'), | |
51 | 0x2b: ('RDSCUR', 'Read security register'), | |
52 | 0x2f: ('WRSCUR', 'Write security register'), | |
53 | 0x70: ('ESRY', 'Enable SO to output RY/BY#'), | |
54 | 0x80: ('DSRY', 'Disable SO to output RY/BY#'), | |
1b1c914f UH |
55 | } |
56 | ||
57 | device_name = { | |
58 | 0x14: 'MX25L1605D', | |
59 | 0x15: 'MX25L3205D', | |
60 | 0x16: 'MX25L6405D', | |
61 | } | |
62 | ||
7cfbf663 UH |
63 | def decode_status_reg(data): |
64 | # TODO: Additional per-bit(s) self.put() calls with correct start/end. | |
65 | ||
66 | # Bits[0:0]: WIP (write in progress) | |
67 | s = 'W' if (data & (1 << 0)) else 'No w' | |
68 | ret = '%srite operation in progress.\n' % s | |
69 | ||
70 | # Bits[1:1]: WEL (write enable latch) | |
71 | s = '' if (data & (1 << 1)) else 'not ' | |
72 | ret += 'Internal write enable latch is %sset.\n' % s | |
73 | ||
74 | # Bits[5:2]: Block protect bits | |
75 | # TODO: More detailed decoding (chip-dependent). | |
76 | ret += 'Block protection bits (BP3-BP0): 0x%x.\n' % ((data & 0x3c) >> 2) | |
77 | ||
78 | # Bits[6:6]: Continuously program mode (CP mode) | |
79 | s = '' if (data & (1 << 6)) else 'not ' | |
80 | ret += 'Device is %sin continuously program mode (CP mode).\n' % s | |
81 | ||
82 | # Bits[7:7]: SRWD (status register write disable) | |
cd287c56 | 83 | s = 'not ' if (data & (1 << 7)) else '' |
7cfbf663 UH |
84 | ret += 'Status register writes are %sallowed.\n' % s |
85 | ||
86 | return ret | |
87 | ||
677d597b | 88 | class Decoder(srd.Decoder): |
a2c2afd9 | 89 | api_version = 1 |
1b1c914f | 90 | id = 'mx25lxx05d' |
9a12a6e7 | 91 | name = 'MX25Lxx05D' |
3d3da57d | 92 | longname = 'Macronix MX25Lxx05D' |
a465436e | 93 | desc = 'SPI (NOR) flash chip protocol.' |
1b1c914f | 94 | license = 'gplv2+' |
385508e9 | 95 | inputs = ['spi', 'logic'] |
1b1c914f | 96 | outputs = ['mx25lxx05d'] |
385508e9 | 97 | probes = [] |
b77614bc | 98 | optional_probes = [ |
385508e9 UH |
99 | {'id': 'hold', 'name': 'HOLD#', 'desc': 'TODO.'}, |
100 | {'id': 'wp_acc', 'name': 'WP#/ACC', 'desc': 'TODO.'}, | |
101 | ] | |
1b1c914f | 102 | options = {} # TODO |
9b4d8a57 | 103 | annotations = [ |
ee3e279c | 104 | ['Text', 'Human-readable text'], |
9b4d8a57 | 105 | ] |
1b1c914f UH |
106 | |
107 | def __init__(self, **kwargs): | |
4772a846 | 108 | self.state = None |
1b1c914f | 109 | self.cmdstate = 1 # TODO |
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): | |
4772a846 UH |
125 | self.putx([0, ['Command: %s' % cmds[self.cmd][1]]]) |
126 | self.state = None | |
1b1c914f UH |
127 | |
128 | # TODO: Check/display device ID / name | |
9b4d8a57 | 129 | def handle_rdid(self, mosi, miso): |
1b1c914f UH |
130 | if self.cmdstate == 1: |
131 | # Byte 1: Master sends command ID. | |
9b4d8a57 | 132 | self.start_sample = self.ss |
4772a846 | 133 | self.putx([0, ['Command: %s' % cmds[self.cmd][1]]]) |
1b1c914f UH |
134 | elif self.cmdstate == 2: |
135 | # Byte 2: Slave sends the JEDEC manufacturer ID. | |
385508e9 | 136 | self.putx([0, ['Manufacturer ID: 0x%02x' % miso]]) |
1b1c914f UH |
137 | elif self.cmdstate == 3: |
138 | # Byte 3: Slave sends the memory type (0x20 for this chip). | |
385508e9 | 139 | self.putx([0, ['Memory type: 0x%02x' % miso]]) |
1b1c914f UH |
140 | elif self.cmdstate == 4: |
141 | # Byte 4: Slave sends the device ID. | |
9b4d8a57 | 142 | self.device_id = miso |
385508e9 | 143 | self.putx([0, ['Device ID: 0x%02x' % miso]]) |
1b1c914f UH |
144 | |
145 | if self.cmdstate == 4: | |
146 | # TODO: Check self.device_id is valid & exists in device_names. | |
147 | # TODO: Same device ID? Check! | |
9b4d8a57 UH |
148 | d = 'Device: Macronix %s' % device_name[self.device_id] |
149 | self.put(self.start_sample, self.es, self.out_ann, [0, [d]]) | |
4772a846 | 150 | self.state = None |
1b1c914f UH |
151 | else: |
152 | self.cmdstate += 1 | |
153 | ||
1b1c914f UH |
154 | # TODO: Warn/abort if we don't see the necessary amount of bytes. |
155 | # TODO: Warn if WREN was not seen before. | |
9b4d8a57 | 156 | def handle_se(self, mosi, miso): |
1b1c914f UH |
157 | if self.cmdstate == 1: |
158 | # Byte 1: Master sends command ID. | |
159 | self.addr = 0 | |
9b4d8a57 | 160 | self.start_sample = self.ss |
4772a846 | 161 | self.putx([0, ['Command: %s' % cmds[self.cmd][1]]]) |
1b1c914f | 162 | elif self.cmdstate in (2, 3, 4): |
87e574b7 UH |
163 | # Bytes 2/3/4: Master sends sectror address (24bits, MSB-first). |
164 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) | |
165 | # self.putx([0, ['Sector address, byte %d: 0x%02x' % \ | |
166 | # (4 - self.cmdstate, mosi)]]) | |
1b1c914f UH |
167 | |
168 | if self.cmdstate == 4: | |
87e574b7 | 169 | d = 'Erase sector %d (0x%06x)' % (self.addr, self.addr) |
9b4d8a57 | 170 | self.put(self.start_sample, self.es, self.out_ann, [0, [d]]) |
1b1c914f UH |
171 | # TODO: Max. size depends on chip, check that too if possible. |
172 | if self.addr % 4096 != 0: | |
173 | # Sector addresses must be 4K-aligned (same for all 3 chips). | |
9b4d8a57 UH |
174 | d = 'Warning: Invalid sector address!' # TODO: type == WARN? |
175 | self.put(self.start_sample, self.es, self.out_ann, [0, [d]]) | |
4772a846 | 176 | self.state = None |
1b1c914f UH |
177 | else: |
178 | self.cmdstate += 1 | |
179 | ||
9b4d8a57 | 180 | def handle_rems(self, mosi, miso): |
1b1c914f UH |
181 | if self.cmdstate == 1: |
182 | # Byte 1: Master sends command ID. | |
9b4d8a57 | 183 | self.start_sample = self.ss |
4772a846 | 184 | self.putx([0, ['Command: %s' % cmds[self.cmd][1]]]) |
1b1c914f UH |
185 | elif self.cmdstate in (2, 3): |
186 | # Bytes 2/3: Master sends two dummy bytes. | |
187 | # TODO: Check dummy bytes? Check reply from device? | |
385508e9 | 188 | self.putx([0, ['Dummy byte: %s' % mosi]]) |
1b1c914f UH |
189 | elif self.cmdstate == 4: |
190 | # Byte 4: Master sends 0x00 or 0x01. | |
191 | # 0x00: Master wants manufacturer ID as first reply byte. | |
192 | # 0x01: Master wants device ID as first reply byte. | |
9b4d8a57 UH |
193 | self.manufacturer_id_first = True if (mosi == 0x00) else False |
194 | d = 'manufacturer' if (mosi == 0x00) else 'device' | |
385508e9 | 195 | self.putx([0, ['Master wants %s ID first' % d]]) |
1b1c914f UH |
196 | elif self.cmdstate == 5: |
197 | # Byte 5: Slave sends manufacturer ID (or device ID). | |
9b4d8a57 UH |
198 | self.ids = [miso] |
199 | d = 'Manufacturer' if self.manufacturer_id_first else 'Device' | |
385508e9 | 200 | self.putx([0, ['%s ID' % d]]) |
9b4d8a57 | 201 | elif self.cmdstate == 6: |
1b1c914f | 202 | # Byte 6: Slave sends device ID (or manufacturer ID). |
7f7ea759 | 203 | self.ids.append(miso) |
9b4d8a57 | 204 | d = 'Manufacturer' if self.manufacturer_id_first else 'Device' |
385508e9 | 205 | self.putx([0, ['%s ID' % d]]) |
1b1c914f UH |
206 | else: |
207 | # TODO: Error? | |
208 | pass | |
209 | ||
210 | if self.cmdstate == 6: | |
9b4d8a57 | 211 | self.end_sample = self.es |
1b1c914f | 212 | id = self.ids[1] if self.manufacturer_id_first else self.ids[0] |
385508e9 | 213 | self.putx([0, ['Device: Macronix %s' % device_name[id]]]) |
4772a846 | 214 | self.state = None |
1b1c914f UH |
215 | else: |
216 | self.cmdstate += 1 | |
217 | ||
9b4d8a57 | 218 | def handle_rdsr(self, mosi, miso): |
e4022299 UH |
219 | # Read status register: Master asserts CS#, sends RDSR command, |
220 | # reads status register byte. If CS# is kept asserted, the status | |
221 | # register can be read continuously / multiple times in a row. | |
222 | # When done, the master de-asserts CS# again. | |
223 | if self.cmdstate == 1: | |
224 | # Byte 1: Master sends command ID. | |
4772a846 | 225 | self.putx([0, ['Command: %s' % cmds[self.cmd][1]]]) |
e4022299 UH |
226 | elif self.cmdstate >= 2: |
227 | # Bytes 2-x: Slave sends status register as long as master clocks. | |
228 | if self.cmdstate <= 3: # TODO: While CS# asserted. | |
229 | self.putx([0, ['Status register: 0x%02x' % miso]]) | |
7cfbf663 | 230 | self.putx([0, [decode_status_reg(miso)]]) |
e4022299 UH |
231 | |
232 | if self.cmdstate == 3: # TODO: If CS# got de-asserted. | |
4772a846 | 233 | self.state = None |
e4022299 UH |
234 | return |
235 | ||
236 | self.cmdstate += 1 | |
237 | ||
238 | def handle_pp(self, mosi, miso): | |
239 | # Page program: Master asserts CS#, sends PP command, sends 3-byte | |
240 | # page address, sends >= 1 data bytes, de-asserts CS#. | |
241 | if self.cmdstate == 1: | |
242 | # Byte 1: Master sends command ID. | |
4772a846 | 243 | self.putx([0, ['Command: %s' % cmds[self.cmd][1]]]) |
e4022299 UH |
244 | elif self.cmdstate in (2, 3, 4): |
245 | # Bytes 2/3/4: Master sends page address (24bits, MSB-first). | |
246 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) | |
247 | # self.putx([0, ['Page address, byte %d: 0x%02x' % \ | |
248 | # (4 - self.cmdstate, mosi)]]) | |
249 | if self.cmdstate == 4: | |
250 | self.putx([0, ['Page address: 0x%06x' % self.addr]]) | |
251 | self.addr = 0 | |
252 | elif self.cmdstate >= 5: | |
253 | # Bytes 5-x: Master sends data bytes (until CS# de-asserted). | |
254 | # TODO: For now we hardcode 256 bytes per page / PP command. | |
255 | if self.cmdstate <= 256 + 4: # TODO: While CS# asserted. | |
256 | self.data.append(mosi) | |
257 | # self.putx([0, ['New data byte: 0x%02x' % mosi]]) | |
258 | ||
259 | if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted. | |
260 | # s = ', '.join(map(hex, self.data)) | |
261 | s = ''.join(map(chr, self.data)) | |
262 | self.putx([0, ['Page data: %s' % s]]) | |
263 | self.data = [] | |
4772a846 | 264 | self.state = None |
e4022299 UH |
265 | return |
266 | ||
267 | self.cmdstate += 1 | |
1b1c914f | 268 | |
5ebb76fe UH |
269 | def handle_read(self, mosi, miso): |
270 | # Read data bytes: Master asserts CS#, sends READ command, sends | |
271 | # 3-byte address, reads >= 1 data bytes, de-asserts CS#. | |
272 | if self.cmdstate == 1: | |
273 | # Byte 1: Master sends command ID. | |
4772a846 | 274 | self.putx([0, ['Command: %s' % cmds[self.cmd][1]]]) |
5ebb76fe UH |
275 | elif self.cmdstate in (2, 3, 4): |
276 | # Bytes 2/3/4: Master sends read address (24bits, MSB-first). | |
277 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) | |
278 | # self.putx([0, ['Read address, byte %d: 0x%02x' % \ | |
279 | # (4 - self.cmdstate, mosi)]]) | |
280 | if self.cmdstate == 4: | |
281 | self.putx([0, ['Read address: 0x%06x' % self.addr]]) | |
282 | self.addr = 0 | |
283 | elif self.cmdstate >= 5: | |
284 | # Bytes 5-x: Master reads data bytes (until CS# de-asserted). | |
285 | # TODO: For now we hardcode 256 bytes per READ command. | |
286 | if self.cmdstate <= 256 + 4: # TODO: While CS# asserted. | |
287 | self.data.append(miso) | |
288 | # self.putx([0, ['New read byte: 0x%02x' % miso]]) | |
289 | ||
290 | if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted. | |
291 | # s = ', '.join(map(hex, self.data)) | |
292 | s = ''.join(map(chr, self.data)) | |
293 | self.putx([0, ['Read data: %s' % s]]) | |
294 | self.data = [] | |
4772a846 | 295 | self.state = None |
5ebb76fe UH |
296 | return |
297 | ||
298 | self.cmdstate += 1 | |
299 | ||
2b9837d9 | 300 | def decode(self, ss, es, data): |
1b1c914f | 301 | |
9b4d8a57 | 302 | ptype, mosi, miso = data |
1b1c914f | 303 | |
e4022299 UH |
304 | # if ptype == 'DATA': |
305 | # s = 'MOSI: 0x%02x, MISO: 0x%02x' % (mosi, miso) | |
306 | # self.put(0, 0, self.out_ann, [0, [s]]) | |
307 | # pass | |
308 | ||
309 | # if ptype == 'CS-CHANGE': | |
310 | # if mosi == 1 and miso == 0: | |
311 | # self.put(0, 0, self.out_ann, [0, ['Asserting CS#']]) | |
312 | # elif mosi == 0 and miso == 1: | |
313 | # self.put(0, 0, self.out_ann, [0, ['De-asserting CS#']]) | |
314 | # return | |
315 | ||
3e3c0330 | 316 | if ptype != 'DATA': |
9b4d8a57 | 317 | return |
1b1c914f | 318 | |
9b4d8a57 | 319 | cmd = mosi |
e4022299 | 320 | self.ss, self.es = ss, es |
1b1c914f | 321 | |
9b4d8a57 | 322 | # If we encountered a known chip command, enter the resp. state. |
4772a846 | 323 | if self.state == None: |
9b4d8a57 UH |
324 | if cmd in cmds: |
325 | self.state = cmd | |
326 | self.cmd = cmd # TODO: Eliminate? | |
327 | self.cmdstate = 1 | |
1b1c914f | 328 | else: |
9b4d8a57 | 329 | pass # TODO |
1b1c914f | 330 | |
9b4d8a57 | 331 | # Handle commands. |
4772a846 UH |
332 | if self.state in cmds.keys(): |
333 | handle_reg = getattr(self, 'handle_%s' % cmds[self.cmd][0].lower()) | |
334 | handle_reg(mosi, miso) | |
9b4d8a57 UH |
335 | else: |
336 | self.put(0, 0, self.out_ann, [0, ['Unknown command: 0x%02x' % cmd]]) | |
4772a846 | 337 | self.state = None |
1b1c914f | 338 |