]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de> | |
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 | import sigrokdecode as srd | |
22 | ||
23 | # ... | |
24 | fields = { | |
25 | # START field (indicates start or stop of a transaction) | |
26 | 'START': { | |
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', | |
42 | 0b1111: 'Stop/abort (end of a cycle for a target)', | |
43 | }, | |
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': { | |
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', | |
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', | |
76 | }, | |
77 | # SYNC field (used to add wait states) | |
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 | ||
98 | class Decoder(srd.Decoder): | |
99 | api_version = 2 | |
100 | id = 'lpc' | |
101 | name = 'LPC' | |
102 | longname = 'Low-Pin-Count' | |
103 | desc = 'Protocol for low-bandwidth devices on PC mainboards.' | |
104 | license = 'gplv2+' | |
105 | inputs = ['logic'] | |
106 | outputs = ['lpc'] | |
107 | channels = ( | |
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'}, | |
114 | ) | |
115 | optional_channels = ( | |
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'}, | |
123 | ) | |
124 | annotations = ( | |
125 | ('warnings', 'Warnings'), | |
126 | ('start', 'Start'), | |
127 | ('cycle-type', 'Cycle-type/direction'), | |
128 | ('addr', 'Address'), | |
129 | ('tar1', 'Turn-around cycle 1'), | |
130 | ('sync', 'Sync'), | |
131 | ('data', 'Data'), | |
132 | ('tar2', 'Turn-around cycle 2'), | |
133 | ) | |
134 | annotation_rows = ( | |
135 | ('data', 'Data', (1, 2, 3, 4, 5, 6, 7)), | |
136 | ('warnings', 'Warnings', (0,)), | |
137 | ) | |
138 | ||
139 | def __init__(self, **kwargs): | |
140 | self.state = 'IDLE' | |
141 | self.oldlclk = -1 | |
142 | self.samplenum = 0 | |
143 | self.clocknum = 0 | |
144 | self.lad = -1 | |
145 | self.addr = 0 | |
146 | self.cur_nibble = 0 | |
147 | self.cycle_type = -1 | |
148 | self.databyte = 0 | |
149 | self.tarcount = 0 | |
150 | self.synccount = 0 | |
151 | self.oldpins = None | |
152 | self.ss_block = self.es_block = None | |
153 | ||
154 | def start(self): | |
155 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
156 | ||
157 | def putb(self, data): | |
158 | self.put(self.ss_block, self.es_block, self.out_ann, data) | |
159 | ||
160 | def handle_get_start(self, lad, lad_bits, lframe): | |
161 | # LAD[3:0]: START field (1 clock cycle). | |
162 | ||
163 | # The last value of LAD[3:0] before LFRAME# gets de-asserted is what | |
164 | # the peripherals must use. However, the host can keep LFRAME# asserted | |
165 | # multiple clocks, and we output all START fields that occur, even | |
166 | # though the peripherals are supposed to ignore all but the last one. | |
167 | self.es_block = self.samplenum | |
168 | self.putb([1, [fields['START'][lad], 'START', 'St', 'S']]) | |
169 | self.ss_block = self.samplenum | |
170 | ||
171 | # Output a warning if LAD[3:0] changes while LFRAME# is low. | |
172 | # TODO | |
173 | if (self.lad != -1 and self.lad != lad): | |
174 | self.putb([0, ['LAD[3:0] changed while LFRAME# was asserted']]) | |
175 | ||
176 | # LFRAME# is asserted (low). Wait until it gets de-asserted again | |
177 | # (the host is allowed to keep it asserted multiple clocks). | |
178 | if lframe != 1: | |
179 | return | |
180 | ||
181 | self.start_field = self.lad | |
182 | self.state = 'GET CT/DR' | |
183 | ||
184 | def handle_get_ct_dr(self, lad, lad_bits): | |
185 | # LAD[3:0]: Cycle type / direction field (1 clock cycle). | |
186 | ||
187 | self.cycle_type = fields['CT_DR'][lad] | |
188 | ||
189 | # TODO: Warning/error on invalid cycle types. | |
190 | if self.cycle_type == 'Reserved': | |
191 | self.putb([0, ['Invalid cycle type (%s)' % lad_bits]]) | |
192 | ||
193 | self.es_block = self.samplenum | |
194 | self.putb([2, ['Cycle type: %s' % self.cycle_type]]) | |
195 | self.ss_block = self.samplenum | |
196 | ||
197 | self.state = 'GET ADDR' | |
198 | self.addr = 0 | |
199 | self.cur_nibble = 0 | |
200 | ||
201 | def handle_get_addr(self, lad, lad_bits): | |
202 | # LAD[3:0]: ADDR field (4/8/0 clock cycles). | |
203 | ||
204 | # I/O cycles: 4 ADDR clocks. Memory cycles: 8 ADDR clocks. | |
205 | # DMA cycles: no ADDR clocks at all. | |
206 | if self.cycle_type in ('I/O read', 'I/O write'): | |
207 | addr_nibbles = 4 # Address is 16bits. | |
208 | elif self.cycle_type in ('Memory read', 'Memory write'): | |
209 | addr_nibbles = 8 # Address is 32bits. | |
210 | else: | |
211 | addr_nibbles = 0 # TODO: How to handle later on? | |
212 | ||
213 | # Addresses are driven MSN-first. | |
214 | offset = ((addr_nibbles - 1) - self.cur_nibble) * 4 | |
215 | self.addr |= (lad << offset) | |
216 | ||
217 | # Continue if we haven't seen all ADDR cycles, yet. | |
218 | if (self.cur_nibble < addr_nibbles - 1): | |
219 | self.cur_nibble += 1 | |
220 | return | |
221 | ||
222 | self.es_block = self.samplenum | |
223 | s = 'Address: 0x%%0%dx' % addr_nibbles | |
224 | self.putb([3, [s % self.addr]]) | |
225 | self.ss_block = self.samplenum | |
226 | ||
227 | self.state = 'GET TAR' | |
228 | self.tar_count = 0 | |
229 | ||
230 | def handle_get_tar(self, lad, lad_bits): | |
231 | # LAD[3:0]: First TAR (turn-around) field (2 clock cycles). | |
232 | ||
233 | self.es_block = self.samplenum | |
234 | self.putb([4, ['TAR, cycle %d: %s' % (self.tarcount, lad_bits)]]) | |
235 | self.ss_block = self.samplenum | |
236 | ||
237 | # On the first TAR clock cycle LAD[3:0] is driven to 1111 by | |
238 | # either the host or peripheral. On the second clock cycle, | |
239 | # the host or peripheral tri-states LAD[3:0], but its value | |
240 | # should still be 1111, due to pull-ups on the LAD lines. | |
241 | if lad_bits != '1111': | |
242 | self.putb([0, ['TAR, cycle %d: %s (expected 1111)' % \ | |
243 | (self.tarcount, lad_bits)]]) | |
244 | ||
245 | if (self.tarcount != 1): | |
246 | self.tarcount += 1 | |
247 | return | |
248 | ||
249 | self.tarcount = 0 | |
250 | self.state = 'GET SYNC' | |
251 | ||
252 | def handle_get_sync(self, lad, lad_bits): | |
253 | # LAD[3:0]: SYNC field (1-n clock cycles). | |
254 | ||
255 | self.sync_val = lad_bits | |
256 | self.cycle_type = fields['SYNC'][lad] | |
257 | ||
258 | # TODO: Warnings if reserved value are seen? | |
259 | if self.cycle_type == 'Reserved': | |
260 | self.putb([0, ['SYNC, cycle %d: %s (reserved value)' % \ | |
261 | (self.synccount, self.sync_val)]]) | |
262 | ||
263 | self.es_block = self.samplenum | |
264 | self.putb([5, ['SYNC, cycle %d: %s' % (self.synccount, self.sync_val)]]) | |
265 | self.ss_block = self.samplenum | |
266 | ||
267 | # TODO | |
268 | ||
269 | self.cycle_count = 0 | |
270 | self.state = 'GET DATA' | |
271 | ||
272 | def handle_get_data(self, lad, lad_bits): | |
273 | # LAD[3:0]: DATA field (2 clock cycles). | |
274 | ||
275 | # Data is driven LSN-first. | |
276 | if (self.cycle_count == 0): | |
277 | self.databyte = lad | |
278 | elif (self.cycle_count == 1): | |
279 | self.databyte |= (lad << 4) | |
280 | else: | |
281 | raise Exception('Invalid cycle_count: %d' % self.cycle_count) | |
282 | ||
283 | if (self.cycle_count != 1): | |
284 | self.cycle_count += 1 | |
285 | return | |
286 | ||
287 | self.es_block = self.samplenum | |
288 | self.putb([6, ['DATA: 0x%02x' % self.databyte]]) | |
289 | self.ss_block = self.samplenum | |
290 | ||
291 | self.cycle_count = 0 | |
292 | self.state = 'GET TAR2' | |
293 | ||
294 | def handle_get_tar2(self, lad, lad_bits): | |
295 | # LAD[3:0]: Second TAR field (2 clock cycles). | |
296 | ||
297 | self.es_block = self.samplenum | |
298 | self.putb([7, ['TAR, cycle %d: %s' % (self.tarcount, lad_bits)]]) | |
299 | self.ss_block = self.samplenum | |
300 | ||
301 | # On the first TAR clock cycle LAD[3:0] is driven to 1111 by | |
302 | # either the host or peripheral. On the second clock cycle, | |
303 | # the host or peripheral tri-states LAD[3:0], but its value | |
304 | # should still be 1111, due to pull-ups on the LAD lines. | |
305 | if lad_bits != '1111': | |
306 | self.putb([0, ['Warning: TAR, cycle %d: %s (expected 1111)' | |
307 | % (self.tarcount, lad_bits)]]) | |
308 | ||
309 | if (self.tarcount != 1): | |
310 | self.tarcount += 1 | |
311 | return | |
312 | ||
313 | self.tarcount = 0 | |
314 | self.state = 'IDLE' | |
315 | ||
316 | def decode(self, ss, es, data): | |
317 | for (self.samplenum, pins) in data: | |
318 | ||
319 | # If none of the pins changed, there's nothing to do. | |
320 | if self.oldpins == pins: | |
321 | continue | |
322 | ||
323 | # Store current pin values for the next round. | |
324 | self.oldpins = pins | |
325 | ||
326 | # Get individual pin values into local variables. | |
327 | (lframe, lclk, lad0, lad1, lad2, lad3) = pins[:6] | |
328 | (lreset, ldrq, serirq, clkrun, lpme, lpcpd, lsmi) = pins[6:] | |
329 | ||
330 | # Only look at the signals upon rising LCLK edges. The LPC clock | |
331 | # is the same as the PCI clock (which is sampled at rising edges). | |
332 | if not (self.oldlclk == 0 and lclk == 1): | |
333 | self.oldlclk = lclk | |
334 | continue | |
335 | ||
336 | # Store LAD[3:0] bit values (one nibble) in local variables. | |
337 | # Most (but not all) states need this. | |
338 | if self.state != 'IDLE': | |
339 | lad = (lad3 << 3) | (lad2 << 2) | (lad1 << 1) | lad0 | |
340 | lad_bits = bin(lad)[2:].zfill(4) | |
341 | # self.putb([0, ['LAD: %s' % lad_bits]]) | |
342 | ||
343 | # TODO: Only memory read/write is currently supported/tested. | |
344 | ||
345 | # State machine | |
346 | if self.state == 'IDLE': | |
347 | # A valid LPC cycle starts with LFRAME# being asserted (low). | |
348 | if lframe != 0: | |
349 | continue | |
350 | self.ss_block = self.samplenum | |
351 | self.state = 'GET START' | |
352 | self.lad = -1 | |
353 | # self.clocknum = 0 | |
354 | elif self.state == 'GET START': | |
355 | self.handle_get_start(lad, lad_bits, lframe) | |
356 | elif self.state == 'GET CT/DR': | |
357 | self.handle_get_ct_dr(lad, lad_bits) | |
358 | elif self.state == 'GET ADDR': | |
359 | self.handle_get_addr(lad, lad_bits) | |
360 | elif self.state == 'GET TAR': | |
361 | self.handle_get_tar(lad, lad_bits) | |
362 | elif self.state == 'GET SYNC': | |
363 | self.handle_get_sync(lad, lad_bits) | |
364 | elif self.state == 'GET DATA': | |
365 | self.handle_get_data(lad, lad_bits) | |
366 | elif self.state == 'GET TAR2': | |
367 | self.handle_get_tar2(lad, lad_bits) |