2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
22 # Macronix MX25Lxx05D SPI (NOR) flash chip decoder.
23 # Works for MX25L1605D/MX25L3205D/MX25L6405D.
30 # http://www.macronix.com/QuickPlace/hq/PageLibrary4825740B00298A3B.nsf/h_Index/3F21BAC2E121E17848257639003A3146/$File/MX25L1605D-3205D-6405D-1.5.pdf
33 import sigrokdecode as srd
38 # Chip commands (also used as additional decoder states).
56 CMD_RDP_RES = 0xab # Note: RDP/RES have the same ID.
66 # TODO: (Short) command names as strings in a dict, too?
68 # Dict which maps command IDs to their description.
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',
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#',
104 # FIXME: This is just some example input for testing purposes...
108 {'type': 'D', 'range': (100, 110), 'data': 0x90, 'ann': ''},
109 {'type': 'D', 'range': (120, 130), 'data': 0xff, 'ann': ''},
110 {'type': 'D', 'range': (170, 180), 'data': 0xff, 'ann': ''},
111 {'type': 'D', 'range': (190, 200), 'data': 0x00, 'ann': ''},
112 {'type': 'D', 'range': (400, 410), 'data': 0xff, 'ann': ''},
113 {'type': 'D', 'range': (411, 421), 'data': 0xff, 'ann': ''},
115 {'type': 'D', 'range': (10, 11), 'data': 0x9f, 'ann': ''},
116 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
117 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
118 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
120 {'type': 'D', 'range': (10, 11), 'data': 0x20, 'ann': ''},
121 {'type': 'D', 'range': (10, 11), 'data': 0x12, 'ann': ''},
122 {'type': 'D', 'range': (10, 11), 'data': 0x34, 'ann': ''},
123 {'type': 'D', 'range': (10, 11), 'data': 0x56, 'ann': ''},
125 {'type': 'D', 'range': (10, 11), 'data': 0x20, 'ann': ''},
126 {'type': 'D', 'range': (10, 11), 'data': 0x44, 'ann': ''},
127 {'type': 'D', 'range': (10, 11), 'data': 0x55, 'ann': ''},
128 {'type': 'D', 'range': (10, 11), 'data': 0x66, 'ann': ''},
130 {'type': 'D', 'range': (10, 11), 'data': 0x06, 'ann': ''},
135 {'type': 'D', 'range': (100, 110), 'data': 0xff, 'ann': ''},
136 {'type': 'D', 'range': (120, 130), 'data': 0xff, 'ann': ''},
137 {'type': 'D', 'range': (170, 180), 'data': 0xff, 'ann': ''},
138 {'type': 'D', 'range': (190, 200), 'data': 0xff, 'ann': ''},
139 {'type': 'D', 'range': (400, 410), 'data': 0xc2, 'ann': ''},
140 {'type': 'D', 'range': (411, 421), 'data': 0x14, 'ann': ''},
142 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
143 {'type': 'D', 'range': (10, 11), 'data': 0xc2, 'ann': ''},
144 {'type': 'D', 'range': (10, 11), 'data': 0x20, 'ann': ''},
145 {'type': 'D', 'range': (10, 11), 'data': 0x15, 'ann': ''},
147 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
148 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
149 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
150 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
152 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
153 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
154 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
155 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
157 {'type': 'D', 'range': (10, 11), 'data': 0xff, 'ann': ''},
160 class Decoder(srd.Decoder):
163 longname = 'Macronix MX25Lxx05D SPI flash chip decoder'
164 desc = 'Macronix MX25Lxx05D SPI flash chip decoder'
166 author = 'Uwe Hermann'
167 email = 'uwe@hermann-uwe.de'
169 inputs = ['spi', 'spi', 'logic']
170 outputs = ['mx25lxx05d']
171 probes = [] # TODO: HOLD#, WP#/ACC
175 def __init__(self, **kwargs):
177 self.cmdstate = 1 # TODO
180 def start(self, metadata):
181 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mx25lxx05d')
182 self.out_ann = self.add(srd.OUTPUT_ANN, 'mx25lxx05d')
187 def handle_wren(self, miso_packet, mosi_packet):
188 self.out += [{'type': self.cmd, 'range': mosi_packet['range'],
189 'data': None, 'ann': cmds[self.state]}]
192 # TODO: Check/display device ID / name
193 def handle_rdid(self, miso_packet, mosi_packet):
197 if self.cmdstate == 1:
198 # Byte 1: Master sends command ID.
199 self.start_sample = mosi_packet['range'][0]
201 elif self.cmdstate == 2:
202 # Byte 2: Slave sends the JEDEC manufacturer ID.
203 o = [{'type': self.cmd, 'range': miso_packet['range'],
204 'data': miso_packet['data'], 'ann': 'Manufacturer ID'}]
205 elif self.cmdstate == 3:
206 # Byte 3: Slave sends the memory type (0x20 for this chip).
207 o = [{'type': self.cmd, 'range': miso_packet['range'],
208 'data': miso_packet['data'], 'ann': 'Memory type'}]
209 elif self.cmdstate == 4:
210 # Byte 4: Slave sends the device ID.
211 self.device_id = miso_packet['data']
212 o = [{'type': self.cmd, 'range': miso_packet['range'],
213 'data': miso_packet['data'], 'ann': 'Device ID'}]
215 if self.cmdstate == 4:
216 # TODO: Check self.device_id is valid & exists in device_names.
217 # TODO: Same device ID? Check!
218 dev = 'Device: Macronix %s' % device_name[self.device_id]
219 o += [{'type': 'RDID', # TODO: self.cmd?
220 'range': (self.start_sample, miso_packet['range'][1]),
221 'data': None, # TODO?
229 # TODO: Warn/abort if we don't see the necessary amount of bytes.
230 # TODO: Warn if WREN was not seen before.
231 def handle_se(self, miso_packet, mosi_packet):
232 if self.cmdstate == 1:
233 # Byte 1: Master sends command ID.
235 self.start_sample = mosi_packet['range'][0]
236 o = [{'type': self.cmd, 'range': mosi_packet['range'],
237 'data': self.cmd, 'ann': 'Command ID'}]
238 elif self.cmdstate in (2, 3, 4):
239 # Bytes 2/3/4: Master sends address of the sector to erase.
240 # Note: Assumes SPI data is 8 bits wide (it is for MX25Lxx05D).
241 # TODO: LSB-first of MSB-first?
243 self.addr |= mosi_packet['data']
244 o = [] # TODO: Output 'Address byte 1' and such fields?
246 if self.cmdstate == 4:
247 o += [{'type': self.cmd,
248 'range': (self.start_sample, mosi_packet['range'][1]),
249 'data': '0x%x' % self.addr, 'ann': cmds[self.state]}]
250 # TODO: Max. size depends on chip, check that too if possible.
251 if self.addr % 4096 != 0:
252 # Sector addresses must be 4K-aligned (same for all 3 chips).
253 o += [{'type': self.cmd, # TODO: Type == 'Warning' or such?
254 'range': (self.start_sample, mosi_packet['range'][1]),
255 'data': None, 'ann': 'Warning: Invalid sector address!'}]
262 def handle_rems(self, miso_packet, mosi_packet):
263 if self.cmdstate == 1:
264 # Byte 1: Master sends command ID.
265 self.start_sample = mosi_packet['range'][0]
266 o = [{'type': self.cmd, 'range': mosi_packet['range'],
267 'data': self.cmd, 'ann': 'Command ID'}]
268 elif self.cmdstate in (2, 3):
269 # Bytes 2/3: Master sends two dummy bytes.
270 # TODO: Check dummy bytes? Check reply from device?
271 o = [{'type': self.cmd, 'range': mosi_packet['range'],
272 'data': mosi_packet['data'], 'ann': 'Dummy byte'}]
273 elif self.cmdstate == 4:
274 # Byte 4: Master sends 0x00 or 0x01.
275 # 0x00: Master wants manufacturer ID as first reply byte.
276 # 0x01: Master wants device ID as first reply byte.
277 b = mosi_packet['data']
278 self.manufacturer_id_first = True if (b == 0x00) else False
279 d = 'manufacturer' if (b == 0x00) else 'device'
280 o = [{'type': self.cmd, 'range': mosi_packet['range'],
281 'data': b, 'ann': '%s (%s ID first)' % (cmds[self.cmd], d)}]
282 elif self.cmdstate == 5:
283 # Byte 5: Slave sends manufacturer ID (or device ID).
284 self.ids = [miso_packet['data']]
286 elif self.cmdstate in (5, 6):
287 # Byte 6: Slave sends device ID (or manufacturer ID).
288 self.ids += [miso_packet['data']]
289 ann = 'Manufacturer' if self.manufacturer_id_first else 'Device'
290 o = [{'type': self.cmd, 'range': miso_packet['range'],
291 'data': '0x%02x' % self.ids[0], 'ann': ann}]
292 ann = 'Device' if self.manufacturer_id_first else 'Manufacturer'
293 o += [{'type': self.cmd, 'range': miso_packet['range'],
294 'data': '0x%02x' % self.ids[1], 'ann': '%s ID' % ann}]
299 if self.cmdstate == 6:
300 self.end_sample = miso_packet['range'][1]
301 id = self.ids[1] if self.manufacturer_id_first else self.ids[0]
302 dev = 'Device: Macronix %s' % device_name[id]
303 o += [{'type': self.cmd,
304 'range': (self.start_sample, self.end_sample),
305 'data': None, # TODO: Both IDs? Which format?
313 def decode(self, ss, es, data):
316 # Iterate over all SPI MISO/MOSI packets. TODO: HOLD#, WP#/ACC?
317 for i in range(len(miso_packets)):
319 p_miso = miso_packets[i]
320 p_mosi = mosi_packets[i]
322 # Assumption: Every p_miso has a p_mosi entry with same range.
324 # For now, skip non-data packets.
325 if p_mosi['type'] != 'D':
330 # If we encountered a known chip command, enter the resp. state.
331 if self.state == IDLE:
334 self.cmd = cmd # TODO: Eliminate?
340 # TODO: Use some generic way to invoke the resp. method.
341 if self.state == CMD_WREN:
342 self.handle_wren(p_miso, p_mosi)
343 elif self.state == CMD_SE:
344 self.handle_se(p_miso, p_mosi)
345 elif self.state == CMD_RDID:
346 self.handle_rdid(p_miso, p_mosi)
347 if self.state == CMD_REMS:
348 self.handle_rems(p_miso, p_mosi)
353 # self.put(0, 0, self.out_proto, out_proto)
354 self.put(0, 0, self.out_ann, self.out)