]>
Commit | Line | Data |
---|---|---|
66a8517e UH |
1 | ## |
2 | ## This file is part of the sigrok project. | |
3 | ## | |
4 | ## Copyright (C) 2012 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 | # ST STM32 JTAG protocol decoder | |
22 | ||
23 | import sigrokdecode as srd | |
24 | ||
25 | # JTAG debug port data registers (in IR[3:0]) and their sizes (in bits) | |
457acc28 UH |
26 | # Note: The ARM DAP-DP is not IEEE 1149.1 (JTAG) compliant (as per ARM docs), |
27 | # as it does not implement the EXTEST, SAMPLE, and PRELOAD instructions. | |
28 | # Instead, BYPASS is decoded for any of these instructions. | |
66a8517e UH |
29 | ir = { |
30 | '1111': ['BYPASS', 1], # Bypass register | |
31 | '1110': ['IDCODE', 32], # ID code register | |
32 | '1010': ['DPACC', 35], # Debug port access register | |
33 | '1011': ['APACC', 35], # Access port access register | |
e9656a0c | 34 | '1000': ['ABORT', 35], # Abort register # TODO: 32 bits? Datasheet typo? |
66a8517e UH |
35 | } |
36 | ||
37 | # ARM Cortex-M3 r1p1-01rel0 ID code | |
38 | cm3_idcode = 0x3ba00477 | |
39 | ||
40 | # JTAG ID code in the STM32F10xxx BSC (boundary scan) TAP | |
41 | jtag_idcode = { | |
42 | 0x06412041: 'Low-density device, rev. A', | |
43 | 0x06410041: 'Medium-density device, rev. A', | |
44 | 0x16410041: 'Medium-density device, rev. B/Z/Y', | |
45 | 0x06414041: 'High-density device, rev. A/Z/Y', | |
46 | 0x06430041: 'XL-density device, rev. A', | |
47 | 0x06418041: 'Connectivity-line device, rev. A/Z', | |
48 | } | |
49 | ||
d274e1bf | 50 | # ACK[2:0] in the DPACC/APACC registers (unlisted values are reserved) |
66a8517e | 51 | ack_val = { |
66a8517e UH |
52 | '001': 'WAIT', |
53 | '010': 'OK/FAULT', | |
66a8517e UH |
54 | } |
55 | ||
56 | # 32bit debug port registers (addressed via A[3:2]) | |
d274e1bf | 57 | dp_reg = { |
66a8517e UH |
58 | '00': 'Reserved', # Must be kept at reset value |
59 | '01': 'DP CTRL/STAT', | |
60 | '10': 'DP SELECT', | |
61 | '11': 'DP RDBUFF', | |
62 | } | |
63 | ||
d274e1bf UH |
64 | # APB-AP registers (each of them 32 bits wide) |
65 | apb_ap_reg = { | |
66 | 0x00: ['CSW', 'Control/status word'], | |
67 | 0x04: ['TAR', 'Transfer address'], | |
68 | # 0x08: Reserved SBZ | |
69 | 0x0c: ['DRW', 'Data read/write'], | |
70 | 0x10: ['BD0', 'Banked data 0'], | |
71 | 0x14: ['BD1', 'Banked data 1'], | |
72 | 0x18: ['BD2', 'Banked data 2'], | |
73 | 0x1c: ['BD3', 'Banked data 3'], | |
74 | # 0x20-0xf4: Reserved SBZ | |
75 | 0x800000000: ['ROM', 'Debug ROM address'], | |
76 | 0xfc: ['IDR', 'Identification register'], | |
77 | } | |
78 | ||
e9656a0c | 79 | # TODO: All start/end sample values in self.put() calls are bogus. |
d274e1bf | 80 | # TODO: Split off generic ARM/Cortex-M3 parts into another protocol decoder? |
e9656a0c | 81 | |
457acc28 UH |
82 | # Bits[31:28]: Version (here: 0x3) |
83 | # JTAG-DP: 0x3, SW-DP: 0x2 | |
84 | # Bits[27:12]: Part number (here: 0xba00) | |
85 | # JTAG-DP: 0xba00, SW-DP: 0xba10 | |
86 | # Bits[11:1]: JEDEC (JEP-106) manufacturer ID (here: 0x23b) | |
87 | # Bits[11:8]: Continuation code ('ARM Limited': 0x04) | |
88 | # Bits[7:1]: Identity code ('ARM Limited': 0x3b) | |
89 | # Bits[0:0]: Reserved (here: 0x1) | |
90 | def decode_device_id_code(bits): | |
91 | id_hex = '0x%x' % int('0b' + bits, 2) | |
92 | ver = '0x%x' % int('0b' + bits[-32:-28], 2) | |
93 | part = '0x%x' % int('0b' + bits[-28:-12], 2) | |
94 | manuf = '0x%x' % int('0b' + bits[-12:-1], 2) | |
95 | res = '0x%x' % int('0b' + bits[-1], 2) | |
96 | return (id_hex, ver, part, manuf, res) | |
97 | ||
d274e1bf UH |
98 | # DPACC is used to access debug port registers (CTRL/STAT, SELECT, RDBUFF). |
99 | # APACC is used to access all Access Port (AHB-AP) registers. | |
100 | ||
101 | # APACC/DPACC, when transferring data IN: | |
102 | # Bits[34:3] = DATA[31:0]: 32bit data to transfer (write request) | |
103 | # Bits[2:1] = A[3:2]: 2-bit address (debug/access port register) | |
104 | # Bits[0:0] = RnW: Read request (1) or write request (0) | |
105 | def data_in(instruction, bits): | |
c840e704 UH |
106 | data, a, rnw = bits[:-3], bits[-3:-1], bits[-1] |
107 | data_hex = '0x%x' % int('0b' + data, 2) | |
108 | r = 'Read request' if (rnw == '1') else 'Write request' | |
d274e1bf UH |
109 | # reg = dp_reg[a] if (instruction == 'DPACC') else apb_ap_reg[a] |
110 | reg = dp_reg[a] if (instruction == 'DPACC') else a # TODO | |
111 | return 'New transaction: DATA: %s, A: %s, RnW: %s' % (data_hex, reg, r) | |
112 | ||
113 | # APACC/DPACC, when transferring data OUT: | |
114 | # Bits[34:3] = DATA[31:0]: 32bit data which is read (read request) | |
115 | # Bits[2:0] = ACK[2:0]: 3-bit acknowledge | |
116 | def data_out(bits): | |
c840e704 UH |
117 | data, ack = bits[:-3], bits[-3:] |
118 | data_hex = '0x%x' % int('0b' + data, 2) | |
d274e1bf UH |
119 | ack_meaning = ack_val.get(ack, 'Reserved') |
120 | return 'Previous transaction result: DATA: %s, ACK: %s' \ | |
121 | % (data_hex, ack_meaning) | |
c840e704 | 122 | |
66a8517e UH |
123 | class Decoder(srd.Decoder): |
124 | api_version = 1 | |
125 | id = 'jtag_stm32' | |
126 | name = 'JTAG / STM32' | |
127 | longname = 'Joint Test Action Group / ST STM32' | |
128 | desc = 'ST STM32-specific JTAG protocol.' | |
129 | license = 'gplv2+' | |
130 | inputs = ['jtag'] | |
131 | outputs = ['jtag_stm32'] | |
132 | probes = [] | |
133 | optional_probes = [] | |
134 | options = {} | |
135 | annotations = [ | |
e9656a0c | 136 | ['Text', 'Human-readable text'], |
66a8517e UH |
137 | ] |
138 | ||
139 | def __init__(self, **kwargs): | |
140 | self.state = 'IDLE' | |
e9656a0c | 141 | # self.state = 'BYPASS' |
66a8517e UH |
142 | |
143 | def start(self, metadata): | |
144 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'jtag_stm32') | |
145 | self.out_ann = self.add(srd.OUTPUT_ANN, 'jtag_stm32') | |
146 | ||
147 | def report(self): | |
148 | pass | |
149 | ||
d274e1bf | 150 | def handle_reg_bypass(self, cmd, bits): |
66a8517e | 151 | # TODO |
e9656a0c | 152 | self.put(self.ss, self.es, self.out_ann, [0, ['BYPASS: ' + bits]]) |
66a8517e | 153 | |
d274e1bf | 154 | def handle_reg_idcode(self, cmd, bits): |
66a8517e | 155 | # TODO |
c840e704 UH |
156 | # IDCODE is a read-only register which is always accessible. |
157 | # IR == IDCODE: The device ID code is shifted out via DR next. | |
e9656a0c | 158 | self.put(self.ss, self.es, self.out_ann, |
457acc28 UH |
159 | [0, ['IDCODE: %s (ver=%s, part=%s, manuf=%s, res=%s)' % \ |
160 | decode_device_id_code(bits)]]) | |
66a8517e | 161 | |
d274e1bf UH |
162 | def handle_reg_dpacc(self, cmd, bits): |
163 | # self.put(self.ss, self.es, self.out_ann, | |
164 | # [0, ['DPACC/%s: %s' % (cmd, bits)]]) | |
165 | s = data_in('DPACC', bits) if (cmd == 'DR TDI') else data_out(bits) | |
166 | self.put(self.ss, self.es, self.out_ann, [0, [s]]) | |
167 | ||
168 | def handle_reg_apacc(self, cmd, bits): | |
169 | # self.put(self.ss, self.es, self.out_ann, | |
170 | # [0, ['APACC/%s: %s' % (cmd, bits)]]) | |
171 | s = data_in('APACC', bits) if (cmd == 'DR TDI') else data_out(bits) | |
172 | self.put(self.ss, self.es, self.out_ann, [0, [s]]) | |
173 | ||
174 | def handle_reg_abort(self, cmd, bits): | |
66a8517e UH |
175 | # Bits[31:1]: reserved. Bit[0]: DAPABORT. |
176 | a = '' if (bits[0] == '1') else 'No ' | |
177 | s = 'DAPABORT = %s: %sDAP abort generated' % (bits[0], a) | |
178 | self.put(self.ss, self.es, self.out_ann, [0, [s]]) | |
179 | ||
e9656a0c | 180 | # Warn if DAPABORT[31:1] contains non-zero bits. |
66a8517e | 181 | if (bits[:-1] != ('0' * 31)): |
e9656a0c UH |
182 | self.put(self.ss, self.es, self.out_ann, |
183 | [0, ['WARNING: DAPABORT[31:1] reserved!']]) | |
184 | ||
d274e1bf | 185 | def handle_reg_unknown(self, cmd, bits): |
e9656a0c UH |
186 | self.put(self.ss, self.es, self.out_ann, |
187 | [0, ['Unknown instruction: ' % bits]]) # TODO | |
66a8517e UH |
188 | |
189 | def decode(self, ss, es, data): | |
190 | # Assumption: The right-most char in the 'val' bitstring is the LSB. | |
191 | cmd, val = data | |
192 | ||
193 | self.ss, self.es = ss, es | |
194 | ||
e9656a0c | 195 | # self.put(self.ss, self.es, self.out_ann, [0, [cmd + ' / ' + val]]) |
66a8517e UH |
196 | |
197 | # State machine | |
e9656a0c UH |
198 | if self.state == 'IDLE': |
199 | # Wait until a new instruction is shifted into the IR register. | |
200 | if cmd != 'IR TDI': | |
201 | return | |
202 | # Switch to the state named after the instruction, or 'UNKNOWN'. | |
d274e1bf UH |
203 | # Ignore bits other than IR[3:0]. While the IR register is only |
204 | # 4 bits in size, some programs (e.g. OpenOCD) might fill in a | |
205 | # few more (dummy) bits. OpenOCD makes IR at least 8 bits long. | |
e9656a0c UH |
206 | self.state = ir.get(val[-4:], ['UNKNOWN', 0])[0] |
207 | self.put(self.ss, self.es, self.out_ann, [0, ['IR: ' + self.state]]) | |
d274e1bf UH |
208 | elif self.state == 'BYPASS': |
209 | # Here we're interested in incoming bits (TDI). | |
e9656a0c UH |
210 | if cmd != 'DR TDI': |
211 | return | |
212 | handle_reg = getattr(self, 'handle_reg_%s' % self.state.lower()) | |
d274e1bf | 213 | handle_reg(cmd, val) |
e9656a0c | 214 | self.state = 'IDLE' |
d274e1bf UH |
215 | elif self.state in ('IDCODE', 'ABORT', 'UNKNOWN'): |
216 | # Here we're interested in outgoing bits (TDO). | |
457acc28 | 217 | if cmd != 'DR TDO': |
e9656a0c UH |
218 | return |
219 | handle_reg = getattr(self, 'handle_reg_%s' % self.state.lower()) | |
d274e1bf | 220 | handle_reg(cmd, val) |
e9656a0c | 221 | self.state = 'IDLE' |
d274e1bf UH |
222 | elif self.state in ('DPACC', 'APACC'): |
223 | # Here we're interested in incoming and outgoing bits (TDI/TDO). | |
224 | if cmd not in ('DR TDI', 'DR TDO'): | |
225 | return | |
226 | handle_reg = getattr(self, 'handle_reg_%s' % self.state.lower()) | |
227 | handle_reg(cmd, val) | |
228 | if cmd == 'DR TDO': # TODO: Assumes 'DR TDI' comes before 'DR TDO' | |
229 | self.state = 'IDLE' | |
e9656a0c UH |
230 | else: |
231 | raise Exception('Invalid state: %s' % self.state) | |
66a8517e | 232 |