2 use ieee.std_logic_1164.all;
3 use ieee.math_real.all;
6 use work.wishbone_types.all;
9 generic(ABITS : INTEGER:=8;
12 port(sys_clk : in std_ulogic;
13 sys_reset : in std_ulogic;
14 dmi_addr : out std_ulogic_vector(ABITS - 1 downto 0);
15 dmi_din : in std_ulogic_vector(DBITS - 1 downto 0);
16 dmi_dout : out std_ulogic_vector(DBITS - 1 downto 0);
17 dmi_req : out std_ulogic;
18 dmi_wr : out std_ulogic;
19 dmi_ack : in std_ulogic
20 -- dmi_err : in std_ulogic TODO: Add error response
24 architecture behaviour of dmi_dtm is
25 -- Signals coming out of the JTAGG block
26 signal jtag_reset_n : std_ulogic;
27 signal tdi : std_ulogic;
28 signal tdo : std_ulogic;
29 signal tck : std_ulogic;
30 signal jce1 : std_ulogic;
31 signal jshift : std_ulogic;
32 signal update : std_ulogic;
34 -- signals to match dmi_dtb_xilinx
35 signal jtag_reset : std_ulogic;
36 signal capture : std_ulogic;
37 signal jtag_clk : std_ulogic;
38 signal sel : std_ulogic;
39 signal shift : std_ulogic;
42 signal jce1_d : std_ulogic;
43 constant TCK_DELAY : INTEGER := 8;
44 signal tck_d : std_ulogic_vector(TCK_DELAY+1 downto 1);
46 -- ** JTAG clock domain **
49 signal shiftr : std_ulogic_vector(ABITS + DBITS + 1 downto 0);
52 signal request : std_ulogic_vector(ABITS + DBITS + 1 downto 0);
54 -- A request is present
55 signal jtag_req : std_ulogic;
57 -- Synchronizer for jtag_rsp (sys clk -> jtag_clk)
58 signal dmi_ack_0 : std_ulogic;
59 signal dmi_ack_1 : std_ulogic;
61 -- ** sys clock domain **
63 -- Synchronizer for jtag_req (jtag clk -> sys clk)
64 signal jtag_req_0 : std_ulogic;
65 signal jtag_req_1 : std_ulogic;
67 -- ** combination signals
68 signal jtag_bsy : std_ulogic;
69 signal op_valid : std_ulogic;
70 signal rsp_op : std_ulogic_vector(1 downto 0);
73 constant DMI_REQ_NOP : std_ulogic_vector(1 downto 0) := "00";
74 constant DMI_REQ_RD : std_ulogic_vector(1 downto 0) := "01";
75 constant DMI_REQ_WR : std_ulogic_vector(1 downto 0) := "10";
76 constant DMI_RSP_OK : std_ulogic_vector(1 downto 0) := "00";
77 constant DMI_RSP_BSY : std_ulogic_vector(1 downto 0) := "11";
79 attribute ASYNC_REG : string;
80 attribute ASYNC_REG of jtag_req_0: signal is "TRUE";
81 attribute ASYNC_REG of jtag_req_1: signal is "TRUE";
82 attribute ASYNC_REG of dmi_ack_0: signal is "TRUE";
83 attribute ASYNC_REG of dmi_ack_1: signal is "TRUE";
88 ER1 : string := "ENABLED";
89 ER2 : string := "ENABLED"
92 JTDO1 : in std_ulogic;
93 JTDO2 : in std_ulogic;
94 JTDI : out std_ulogic;
95 JTCK : out std_ulogic;
96 JRTI1 : out std_ulogic;
97 JRTI2 : out std_ulogic;
98 JSHIFT : out std_ulogic;
99 JUPDATE : out std_ulogic;
100 JRSTN : out std_ulogic;
101 JCE1 : out std_ulogic;
102 JCE2 : out std_ulogic
108 INIT : std_logic_vector
134 JRSTN => jtag_reset_n,
139 -- JRTI1 looks like it could be connected to SEL, but
140 -- in practise JRTI1 is only high briefly, not for the duration
141 -- of the transmission. possibly mw_debug could be modified.
142 -- The ecp5 is probably the only jtag device anyway.
145 -- TDI needs to align with TCK, we use LUT delays here.
146 -- From https://github.com/enjoy-digital/litex/pull/1087
148 del: for i in 1 to TCK_DELAY generate
149 attribute keep : boolean;
150 attribute keep of l: label is true;
154 INIT => b"0000_0000_0000_0010"
158 B => '0', C => '0', D => '0',
162 jtag_clk <= tck_d(TCK_DELAY+1);
165 jce1_sync : process(jtag_clk)
167 if rising_edge(jtag_clk) then
169 capture <= jce1 and not jce1_d;
173 -- latch the shift signal, otherwise
174 -- we miss the last shift in
175 -- (maybe because we are delaying tck?)
176 shift_sync : process(jtag_clk)
178 if (sys_reset = '1') then
180 elsif rising_edge(jtag_clk) then
185 jtag_reset <= not jtag_reset_n;
187 -- dmi_req synchronization
188 dmi_req_sync : process(sys_clk)
190 -- sys_reset is synchronous
191 if rising_edge(sys_clk) then
192 if (sys_reset = '1') then
196 jtag_req_0 <= jtag_req;
197 jtag_req_1 <= jtag_req_0;
201 dmi_req <= jtag_req_1;
203 -- dmi_ack synchronization
204 dmi_ack_sync: process(jtag_clk, jtag_reset)
206 -- jtag_reset is async (see comments)
207 if jtag_reset = '1' then
210 elsif rising_edge(jtag_clk) then
211 dmi_ack_0 <= dmi_ack;
212 dmi_ack_1 <= dmi_ack_0;
216 -- jtag_bsy indicates whether we can start a new request, we can when
217 -- we aren't already processing one (jtag_req) and the synchronized ack
218 -- of the previous one is 0.
220 jtag_bsy <= jtag_req or dmi_ack_1;
222 -- decode request type in shift register
223 with shiftr(1 downto 0) select op_valid <=
228 -- encode response op
229 rsp_op <= DMI_RSP_BSY when jtag_bsy = '1' else DMI_RSP_OK;
231 -- Some DMI out signals are directly driven from the request register
232 dmi_addr <= request(ABITS + DBITS + 1 downto DBITS + 2);
233 dmi_dout <= request(DBITS + 1 downto 2);
234 dmi_wr <= '1' when request(1 downto 0) = DMI_REQ_WR else '0';
236 -- TDO is wired to shift register bit 0
239 -- Main state machine. Handles shift registers, request latch and
240 -- jtag_req latch. Could be split into 3 processes but it's probably
243 shifter: process(jtag_clk, jtag_reset, sys_reset)
245 if jtag_reset = '1' or sys_reset = '1' then
246 shiftr <= (others => '0');
248 request <= (others => '0');
249 elsif rising_edge(jtag_clk) then
251 -- Handle jtag "commands" when sel is 1
253 -- Shift state, rotate the register
255 shiftr <= tdi & shiftr(ABITS + DBITS + 1 downto 1);
258 -- Update state (trigger)
260 -- Latch the request if we aren't already processing one and
261 -- it has a valid command opcode.
263 if update = '1' and op_valid = '1' then
264 if jtag_bsy = '0' then
268 -- Set the shift register "op" to "busy". This will prevent
269 -- us from re-starting the command on the next update if
270 -- the command completes before that.
271 shiftr(1 downto 0) <= DMI_RSP_BSY;
274 -- Request completion.
276 -- Capture the response data for reads and clear request flag.
278 -- Note: We clear req (and thus dmi_req) here which relies on tck
279 -- ticking and sel set. This means we are stuck with dmi_req up if
280 -- the jtag interface stops. Slaves must be resilient to this.
282 if jtag_req = '1' and dmi_ack_1 = '1' then
284 if request(1 downto 0) = DMI_REQ_RD then
285 request(DBITS + 1 downto 2) <= dmi_din;
289 -- Capture state, grab latch content with updated status
290 if capture = '1' then
291 shiftr <= request(ABITS + DBITS + 1 downto 2) & rsp_op;
297 end architecture behaviour;