]> sigrok.org Git - libsigrokdecode.git/blame - decoders/lpc/pd.py
The start() method no longer takes a metadata parameter
[libsigrokdecode.git] / decoders / lpc / pd.py
CommitLineData
271acd3b 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
271acd3b 3##
edc6c8fd 4## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
271acd3b
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# LPC protocol decoder
22
23import sigrokdecode as srd
24
271acd3b
UH
25# ...
26fields = {
2002229d
UH
27 # START field (indicates start or stop of a transaction)
28 'START': {
271acd3b
UH
29 0b0000: 'Start of cycle for a target',
30 0b0001: 'Reserved',
31 0b0010: 'Grant for bus master 0',
32 0b0011: 'Grant for bus master 1',
33 0b0100: 'Reserved',
34 0b0101: 'Reserved',
35 0b0110: 'Reserved',
36 0b0111: 'Reserved',
37 0b1000: 'Reserved',
38 0b1001: 'Reserved',
39 0b1010: 'Reserved',
40 0b1011: 'Reserved',
41 0b1100: 'Reserved',
42 0b1101: 'Start of cycle for a Firmware Memory Read cycle',
43 0b1110: 'Start of cycle for a Firmware Memory Write cycle',
2002229d 44 0b1111: 'Stop/abort (end of a cycle for a target)',
271acd3b 45 },
2002229d
UH
46 # Cycle type / direction field
47 # Bit 0 (LAD[0]) is unused, should always be 0.
48 # Neither host nor peripheral are allowed to drive 0b11x0.
49 'CT_DR': {
271acd3b
UH
50 0b0000: 'I/O read',
51 0b0010: 'I/O write',
52 0b0100: 'Memory read',
53 0b0110: 'Memory write',
54 0b1000: 'DMA read',
55 0b1010: 'DMA write',
2002229d
UH
56 0b1100: 'Reserved / not allowed',
57 0b1110: 'Reserved / not allowed',
58 },
59 # SIZE field (determines how many bytes are to be transferred)
60 # Bits[3:2] are reserved, must be driven to 0b00.
61 # Neither host nor peripheral are allowed to drive 0b0010.
62 'SIZE': {
63 0b0000: '8 bits (1 byte)',
64 0b0001: '16 bits (2 bytes)',
65 0b0010: 'Reserved / not allowed',
66 0b0011: '32 bits (4 bytes)',
67 },
68 # CHANNEL field (bits[2:0] contain the DMA channel number)
69 'CHANNEL': {
70 0b0000: '0',
71 0b0001: '1',
72 0b0010: '2',
73 0b0011: '3',
74 0b0100: '4',
75 0b0101: '5',
76 0b0110: '6',
77 0b0111: '7',
271acd3b 78 },
2002229d 79 # SYNC field (used to add wait states)
271acd3b
UH
80 'SYNC': {
81 0b0000: 'Ready',
82 0b0001: 'Reserved',
83 0b0010: 'Reserved',
84 0b0011: 'Reserved',
85 0b0100: 'Reserved',
86 0b0101: 'Short wait',
87 0b0110: 'Long wait',
88 0b0111: 'Reserved',
89 0b1000: 'Reserved',
90 0b1001: 'Ready more (DMA only)',
91 0b1010: 'Error',
92 0b1011: 'Reserved',
93 0b1100: 'Reserved',
94 0b1101: 'Reserved',
95 0b1110: 'Reserved',
96 0b1111: 'Reserved',
97 },
98}
99
100class Decoder(srd.Decoder):
101 api_version = 1
102 id = 'lpc'
103 name = 'LPC'
104 longname = 'Low-Pin-Count'
a465436e 105 desc = 'Protocol for low-bandwidth devices on PC mainboards.'
271acd3b
UH
106 license = 'gplv2+'
107 inputs = ['logic']
108 outputs = ['lpc']
109 probes = [
110 {'id': 'lframe', 'name': 'LFRAME#', 'desc': 'TODO'},
271acd3b
UH
111 {'id': 'lclk', 'name': 'LCLK', 'desc': 'TODO'},
112 {'id': 'lad0', 'name': 'LAD[0]', 'desc': 'TODO'},
113 {'id': 'lad1', 'name': 'LAD[1]', 'desc': 'TODO'},
114 {'id': 'lad2', 'name': 'LAD[2]', 'desc': 'TODO'},
115 {'id': 'lad3', 'name': 'LAD[3]', 'desc': 'TODO'},
116 ]
117 optional_probes = [
6554fbc9 118 {'id': 'lreset', 'name': 'LRESET#', 'desc': 'TODO'},
271acd3b
UH
119 {'id': 'ldrq', 'name': 'LDRQ#', 'desc': 'TODO'},
120 {'id': 'serirq', 'name': 'SERIRQ', 'desc': 'TODO'},
121 {'id': 'clkrun', 'name': 'CLKRUN#', 'desc': 'TODO'},
122 {'id': 'lpme', 'name': 'LPME#', 'desc': 'TODO'},
123 {'id': 'lpcpd', 'name': 'LPCPD#', 'desc': 'TODO'},
124 {'id': 'lsmi', 'name': 'LSMI#', 'desc': 'TODO'},
125 ]
126 options = {}
127 annotations = [
f7aa0719
UH
128 ['warnings', 'Warnings'],
129 ['start', 'Start'],
130 ['cycle_type', 'Cycle-type/direction'],
131 ['addr', 'Address'],
132 ['tar1', 'Turn-around cycle 1'],
133 ['sync', 'Sync'],
134 ['data', 'Data'],
135 ['tar2', 'Turn-around cycle 2'],
271acd3b
UH
136 ]
137
138 def __init__(self, **kwargs):
139 self.state = 'IDLE'
140 self.oldlclk = -1
141 self.samplenum = 0
142 self.clocknum = 0
143 self.lad = -1
144 self.addr = 0
145 self.cur_nibble = 0
146 self.cycle_type = -1
cded73ba
UH
147 self.databyte = 0
148 self.tarcount = 0
149 self.synccount = 0
59d3200c 150 self.oldpins = None
63374ad8 151 self.ss_block = self.es_block = None
271acd3b 152
8915b346 153 def start(self):
cded73ba 154 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'lpc')
271acd3b
UH
155 self.out_ann = self.add(srd.OUTPUT_ANN, 'lpc')
156
157 def report(self):
158 pass
159
edc6c8fd 160 def putb(self, data):
63374ad8 161 self.put(self.ss_block, self.es_block, self.out_ann, data)
edc6c8fd 162
cded73ba 163 def handle_get_start(self, lad, lad_bits, lframe):
271acd3b
UH
164 # LAD[3:0]: START field (1 clock cycle).
165
166 # The last value of LAD[3:0] before LFRAME# gets de-asserted is what
167 # the peripherals must use. However, the host can keep LFRAME# asserted
168 # multiple clocks, and we output all START fields that occur, even
169 # though the peripherals are supposed to ignore all but the last one.
63374ad8
UH
170 self.es_block = self.samplenum
171 self.putb([1, [fields['START'][lad], 'START', 'St', 'S']])
172 self.ss_block = self.samplenum
271acd3b
UH
173
174 # Output a warning if LAD[3:0] changes while LFRAME# is low.
175 # TODO
176 if (self.lad != -1 and self.lad != lad):
f7aa0719 177 self.putb([0, ['LAD[3:0] changed while LFRAME# was asserted']])
271acd3b
UH
178
179 # LFRAME# is asserted (low). Wait until it gets de-asserted again
180 # (the host is allowed to keep it asserted multiple clocks).
181 if lframe != 1:
182 return
183
184 self.start_field = self.lad
185 self.state = 'GET CT/DR'
186
187 def handle_get_ct_dr(self, lad, lad_bits):
188 # LAD[3:0]: Cycle type / direction field (1 clock cycle).
189
190 self.cycle_type = fields['CT_DR'][lad]
191
192 # TODO: Warning/error on invalid cycle types.
193 if self.cycle_type == 'Reserved':
f7aa0719 194 self.putb([0, ['Invalid cycle type (%s)' % lad_bits]])
271acd3b 195
63374ad8 196 self.es_block = self.samplenum
f7aa0719 197 self.putb([2, ['Cycle type: %s' % self.cycle_type]])
63374ad8 198 self.ss_block = self.samplenum
271acd3b
UH
199
200 self.state = 'GET ADDR'
201 self.addr = 0
202 self.cur_nibble = 0
203
204 def handle_get_addr(self, lad, lad_bits):
205 # LAD[3:0]: ADDR field (4/8/0 clock cycles).
206
207 # I/O cycles: 4 ADDR clocks. Memory cycles: 8 ADDR clocks.
208 # DMA cycles: no ADDR clocks at all.
209 if self.cycle_type in ('I/O read', 'I/O write'):
210 addr_nibbles = 4 # Address is 16bits.
211 elif self.cycle_type in ('Memory read', 'Memory write'):
212 addr_nibbles = 8 # Address is 32bits.
213 else:
214 addr_nibbles = 0 # TODO: How to handle later on?
215
cded73ba 216 # Addresses are driven MSN-first.
271acd3b
UH
217 offset = ((addr_nibbles - 1) - self.cur_nibble) * 4
218 self.addr |= (lad << offset)
219
220 # Continue if we haven't seen all ADDR cycles, yet.
271acd3b
UH
221 if (self.cur_nibble < addr_nibbles - 1):
222 self.cur_nibble += 1
223 return
224
63374ad8 225 self.es_block = self.samplenum
cded73ba 226 s = 'Address: 0x%%0%dx' % addr_nibbles
f7aa0719 227 self.putb([3, [s % self.addr]])
63374ad8 228 self.ss_block = self.samplenum
271acd3b
UH
229
230 self.state = 'GET TAR'
231 self.tar_count = 0
232
233 def handle_get_tar(self, lad, lad_bits):
234 # LAD[3:0]: First TAR (turn-around) field (2 clock cycles).
235
63374ad8 236 self.es_block = self.samplenum
f7aa0719 237 self.putb([4, ['TAR, cycle %d: %s' % (self.tarcount, lad_bits)]])
63374ad8 238 self.ss_block = self.samplenum
271acd3b
UH
239
240 # On the first TAR clock cycle LAD[3:0] is driven to 1111 by
241 # either the host or peripheral. On the second clock cycle,
242 # the host or peripheral tri-states LAD[3:0], but its value
243 # should still be 1111, due to pull-ups on the LAD lines.
244 if lad_bits != '1111':
f7aa0719 245 self.putb([0, ['TAR, cycle %d: %s (expected 1111)' % \
edc6c8fd 246 (self.tarcount, lad_bits)]])
271acd3b 247
cded73ba 248 if (self.tarcount != 1):
271acd3b
UH
249 self.tarcount += 1
250 return
251
cded73ba 252 self.tarcount = 0
271acd3b
UH
253 self.state = 'GET SYNC'
254
255 def handle_get_sync(self, lad, lad_bits):
256 # LAD[3:0]: SYNC field (1-n clock cycles).
257
258 self.sync_val = lad_bits
259 self.cycle_type = fields['SYNC'][lad]
260
261 # TODO: Warnings if reserved value are seen?
262 if self.cycle_type == 'Reserved':
f7aa0719 263 self.putb([0, ['SYNC, cycle %d: %s (reserved value)' % \
edc6c8fd 264 (self.synccount, self.sync_val)]])
271acd3b 265
63374ad8 266 self.es_block = self.samplenum
f7aa0719 267 self.putb([5, ['SYNC, cycle %d: %s' % (self.synccount, self.sync_val)]])
63374ad8 268 self.ss_block = self.samplenum
271acd3b
UH
269
270 # TODO
271
271acd3b 272 self.cycle_count = 0
cded73ba 273 self.state = 'GET DATA'
271acd3b
UH
274
275 def handle_get_data(self, lad, lad_bits):
276 # LAD[3:0]: DATA field (2 clock cycles).
277
cded73ba 278 # Data is driven LSN-first.
271acd3b
UH
279 if (self.cycle_count == 0):
280 self.databyte = lad
281 elif (self.cycle_count == 1):
282 self.databyte |= (lad << 4)
283 else:
cded73ba 284 raise Exception('Invalid cycle_count: %d' % self.cycle_count)
271acd3b 285
cded73ba 286 if (self.cycle_count != 1):
271acd3b
UH
287 self.cycle_count += 1
288 return
289
63374ad8 290 self.es_block = self.samplenum
f7aa0719 291 self.putb([6, ['DATA: 0x%02x' % self.databyte]])
63374ad8 292 self.ss_block = self.samplenum
cded73ba
UH
293
294 self.cycle_count = 0
271acd3b
UH
295 self.state = 'GET TAR2'
296
297 def handle_get_tar2(self, lad, lad_bits):
298 # LAD[3:0]: Second TAR field (2 clock cycles).
299
63374ad8 300 self.es_block = self.samplenum
f7aa0719 301 self.putb([7, ['TAR, cycle %d: %s' % (self.tarcount, lad_bits)]])
63374ad8 302 self.ss_block = self.samplenum
271acd3b
UH
303
304 # On the first TAR clock cycle LAD[3:0] is driven to 1111 by
305 # either the host or peripheral. On the second clock cycle,
306 # the host or peripheral tri-states LAD[3:0], but its value
307 # should still be 1111, due to pull-ups on the LAD lines.
308 if lad_bits != '1111':
edc6c8fd
UH
309 self.putb([0, ['Warning: TAR, cycle %d: %s (expected 1111)'
310 % (self.tarcount, lad_bits)]])
271acd3b 311
cded73ba 312 if (self.tarcount != 1):
271acd3b
UH
313 self.tarcount += 1
314 return
315
cded73ba
UH
316 self.tarcount = 0
317 self.state = 'IDLE'
271acd3b 318
271acd3b 319 def decode(self, ss, es, data):
63374ad8 320 for (self.samplenum, pins) in data:
271acd3b
UH
321
322 # If none of the pins changed, there's nothing to do.
323 if self.oldpins == pins:
324 continue
325
326 # Store current pin values for the next round.
327 self.oldpins = pins
328
329 # Get individual pin values into local variables.
6554fbc9
UH
330 (lframe, lclk, lad0, lad1, lad2, lad3) = pins[:6]
331 (lreset, ldrq, serirq, clkrun, lpme, lpcpd, lsmi) = pins[6:]
271acd3b 332
1cb84473
UH
333 # Only look at the signals upon rising LCLK edges. The LPC clock
334 # is the same as the PCI clock (which is sampled at rising edges).
335 if not (self.oldlclk == 0 and lclk == 1):
336 self.oldlclk = lclk
337 continue
271acd3b
UH
338
339 # Store LAD[3:0] bit values (one nibble) in local variables.
2002229d 340 # Most (but not all) states need this.
271acd3b
UH
341 if self.state != 'IDLE':
342 lad = (lad3 << 3) | (lad2 << 2) | (lad1 << 1) | lad0
cded73ba 343 lad_bits = bin(lad)[2:].zfill(4)
edc6c8fd 344 # self.putb([0, ['LAD: %s' % lad_bits]])
cded73ba
UH
345
346 # TODO: Only memory read/write is currently supported/tested.
271acd3b
UH
347
348 # State machine
349 if self.state == 'IDLE':
350 # A valid LPC cycle starts with LFRAME# being asserted (low).
271acd3b
UH
351 if lframe != 0:
352 continue
63374ad8 353 self.ss_block = self.samplenum
271acd3b
UH
354 self.state = 'GET START'
355 self.lad = -1
356 # self.clocknum = 0
357 elif self.state == 'GET START':
cded73ba 358 self.handle_get_start(lad, lad_bits, lframe)
271acd3b 359 elif self.state == 'GET CT/DR':
cded73ba 360 self.handle_get_ct_dr(lad, lad_bits)
271acd3b 361 elif self.state == 'GET ADDR':
cded73ba 362 self.handle_get_addr(lad, lad_bits)
271acd3b 363 elif self.state == 'GET TAR':
cded73ba 364 self.handle_get_tar(lad, lad_bits)
271acd3b 365 elif self.state == 'GET SYNC':
cded73ba 366 self.handle_get_sync(lad, lad_bits)
271acd3b 367 elif self.state == 'GET DATA':
cded73ba 368 self.handle_get_data(lad, lad_bits)
271acd3b 369 elif self.state == 'GET TAR2':
cded73ba 370 self.handle_get_tar2(lad, lad_bits)
271acd3b
UH
371 else:
372 raise Exception('Invalid state: %s' % self.state)
373