summaryrefslogtreecommitdiffstats
path: root/fpga/hw-v2-mci/src/sdio/sdio_top.vhd
blob: 6997f8f7541fe1816adc5eeea9cf3a1e27cac7a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
-----------------------------------------------------------------------------------
-- Copyright (C) 2013 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany --
-- written by Matthias Kleffel                                                   --
--                                                                               --
-- This program is free software; you can redistribute it and/or modify          --
-- it under the terms of the GNU General Public License as published by          --
-- the Free Software Foundation as version 3 of the License, or                  --
--                                                                               --
-- This program is distributed in the hope that it will be useful,               --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of                --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  --
-- GNU General Public License V3 for more details.                               --
--                                                                               --
-- You should have received a copy of the GNU General Public License             --
-- along with this program. If not, see <http://www.gnu.org/licenses/>.          --
-----------------------------------------------------------------------------------

library ieee;
	use ieee.std_logic_1164.all;
	use ieee.numeric_std.all;
library work;
	use work.all;
	use work.mt_toolbox.all;

entity sdio_top is
	port (
		-- common
		clk      : in  std_logic;
		reset    : in  std_logic;
		
		-- input
		in_clk   : in  std_logic;
		in_data  : in  std_logic_vector(31 downto 0);
		
		-- SDIO interface
		sd_clk   : in    std_logic;
		sd_cmd   : inout std_logic;
		sd_dat   : inout std_logic_vector(3 downto 0)
	);
end sdio_top;

architecture rtl of sdio_top is
	
	-- spitted tristate pads
	signal sd_cmd_i  : std_logic;
	signal sd_cmd_o  : std_logic;
	signal sd_cmd_oe : std_logic;
	signal sd_dat_i  : std_logic_vector(3 downto 0);
	signal sd_dat_o  : std_logic_vector(3 downto 0);
	signal sd_dat_oe : std_logic;
	
	-- status/control
	signal ctx_active : std_logic;
	signal dtx_active : std_logic;
	
	-- command receiver
	signal cmd_ok  : std_logic;
	signal cmd_err : std_logic;
	signal cmd_ind : unsigned(5 downto 0);
	signal cmd_arg : std_logic_vector(31 downto 0);
		
	-- response transmitter
	signal res_start : std_logic;
	signal res_done  : std_logic;
	signal res_ind   : unsigned(5 downto 0);
	signal res_arg   : std_logic_vector(31 downto 0);
	
	-- bus-interface
	signal bus_rena  : std_logic;
	signal bus_wena  : std_logic;
	signal bus_addr  : unsigned(16 downto 0);
	signal bus_oor   : std_logic;
	signal bus_rdat  : std_logic_vector(7 downto 0);
	signal bus_wdat  : std_logic_vector(7 downto 0);
	
	-- registers
	signal fn1_start : std_logic;
	signal fn1_abort : std_logic;
	signal fn1_count : unsigned(8 downto 0);
	signal fn1_bsize : unsigned(15 downto 0);
	
	-- TX-control <-> TX-core
	signal tx_ready : std_logic;
	signal tx_sync  : std_logic;
	signal tx_clk   : std_logic;
	signal tx_dat   : std_logic_vector(7 downto 0);
	
begin
	
	-- create tristate pads
	sd_cmd_i <= to_X01(sd_cmd);
	sd_dat_i <= to_X01(sd_dat);
	sd_cmd <= sd_cmd_o when sd_cmd_oe='1' else 'Z';
	sd_dat <= sd_dat_o when sd_dat_oe='1' else "ZZZZ";

	
	--
	-- control logic
	--
	
	process(sd_clk,reset)
	begin
		if reset='1' then
			bus_oor   <= '0';
			bus_rdat  <= x"00";
			fn1_bsize <= x"0200";
			fn1_abort <= '0';
		elsif rising_edge(sd_clk) then
			-- set default values
			bus_oor   <= '0';
			bus_rdat  <= x"00";
			fn1_abort <= '0';
			
			-- handle request
			case to_integer(bus_addr) is
				-- some random static registers
				when 0 =>
					bus_rdat <= x"43";
				when 1 =>
					bus_rdat <= x"03";
				
				-- IO Abort
				when 6 =>
					if bus_wena='1' and bus_wdat(2 downto 0)="001" then
						fn1_abort <= '1';
					end if;
				
				-- FN1 blocksize
				when 16#110# =>
					bus_rdat <= to_slv8(fn1_bsize(7 downto 0));
					if bus_wena='1' then
						fn1_bsize(7 downto 0) <= unsigned(bus_wdat);
					end if;
				when 16#111# =>
					bus_rdat <= to_slv8(fn1_bsize(15 downto 8));
					if bus_wena='1' then
						fn1_bsize(15 downto 8) <= unsigned(bus_wdat);
					end if;
					
				when others =>
					-- unknown register
					bus_oor <= '1';
			end case;
		end if;
	end process;
	
	
	--
	-- components
	--
	
	-- command receiver
	crx: entity sdio_cmd_rx
		port map (
			-- common
			clk      => clk,
			reset    => reset,
			
			-- SDIO interface
			sd_clk   => sd_clk,
			sd_cmd_i => sd_cmd_i,
			
			-- status/control
			tx_active => ctx_active,
	
			-- 'event' output
			cmd_ok   => cmd_ok,
			cmd_err  => cmd_err,
			
			-- command data
			cmd_ind  => cmd_ind,
			cmd_arg  => cmd_arg
		);
	
	-- response transmitter
	ctx: entity sdio_cmd_tx
		port map (
			-- common
			clk       => clk,
			reset     => reset,
			
			-- SDIO interface
			sd_clk    => sd_clk,
			sd_cmd_oe => sd_cmd_oe,
			sd_cmd_o  => sd_cmd_o,
			
			-- status/control
			tx_active => ctx_active,
			
			-- response control
			res_start => res_start,
			res_done  => res_done,
			
			-- response data
			res_ind   => res_ind,
			res_arg   => res_arg
		);
	
	-- command handler
	cmd: entity sdio_cmd_handler
		port map (
			-- common
			clk        => clk,
			reset      => reset,
			sd_clk     => sd_clk,
			
			-- status
			dtx_active => dtx_active,
		
			-- command receiver
			cmd_ok     => cmd_ok,
			cmd_err    => cmd_err,
			cmd_ind    => cmd_ind,
			cmd_arg    => cmd_arg,
				
			-- response transmitter
			res_start  => res_start,
			res_done   => res_done,
			res_ind    => res_ind,
			res_arg    => res_arg,
			
			-- function0 - register-interface
			fn0_rena   => bus_rena,
			fn0_wena   => bus_wena,
			fn0_addr   => bus_addr,
			fn0_oor    => bus_oor,
			fn0_rdat   => bus_rdat,
			fn0_wdat   => bus_wdat,
			
			-- function1 - control
			fn1_start  => fn1_start,
			fn1_count  => fn1_count
		);

	-- data transmitter
	dtx: entity sdio_dat_tx
		port map (
			-- common
			clk       => clk,
			reset     => reset,
			
			-- control
			start     => fn1_start,
			abort     => fn1_abort,
			bcount    => fn1_count,
			bsize     => fn1_bsize,
			
			-- status
			active    => dtx_active,
		
			-- input data
			in_ready  => tx_ready,
			in_sync   => tx_sync,
			in_clk    => tx_clk,
			in_dat    => tx_dat,
			
			-- SDIO interface
			sd_clk    => sd_clk,
			sd_dat_oe => sd_dat_oe,
			sd_dat_o  => sd_dat_o
		);

	-- TX data control/fifo
	txctl: entity sdio_txctrl
		port map (
			-- clocks
			clk_sys   => clk,
			rst_sys   => reset,
			sd_clk    => sd_clk,
			
			-- config
			cfg_bsize => fn1_bsize,
			
			-- input
			in_clk    => in_clk,
			in_data   => in_data,
			
			-- output
			out_ready => tx_ready,
			out_sync  => tx_sync,
			out_clk   => tx_clk,
			out_dat   => tx_dat
		);

end rtl;