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