| 1 |
2 |
alfik |
/*
|
| 2 |
7 |
alfik |
* This file is subject to the terms and conditions of the BSD License. See
|
| 3 |
|
|
* the file "LICENSE" in the main directory of this archive for more details.
|
| 4 |
|
|
*
|
| 5 |
|
|
* Copyright (C) 2014 Aleksander Osman
|
| 6 |
2 |
alfik |
*/
|
| 7 |
|
|
|
| 8 |
|
|
module driver_sd(
|
| 9 |
7 |
alfik |
input clk,
|
| 10 |
|
|
input rst_n,
|
| 11 |
2 |
alfik |
|
| 12 |
7 |
alfik |
//
|
| 13 |
2 |
alfik |
input [1:0] avs_address,
|
| 14 |
|
|
input avs_read,
|
| 15 |
|
|
output [31:0] avs_readdata,
|
| 16 |
|
|
input avs_write,
|
| 17 |
|
|
input [31:0] avs_writedata,
|
| 18 |
|
|
|
| 19 |
7 |
alfik |
//
|
| 20 |
|
|
output [31:0] avm_address,
|
| 21 |
2 |
alfik |
input avm_waitrequest,
|
| 22 |
7 |
alfik |
output avm_read,
|
| 23 |
2 |
alfik |
input [31:0] avm_readdata,
|
| 24 |
|
|
input avm_readdatavalid,
|
| 25 |
7 |
alfik |
output avm_write,
|
| 26 |
|
|
output [31:0] avm_writedata,
|
| 27 |
2 |
alfik |
|
| 28 |
7 |
alfik |
//
|
| 29 |
|
|
output reg sd_clk,
|
| 30 |
|
|
inout sd_cmd,
|
| 31 |
|
|
inout [3:0] sd_dat
|
| 32 |
2 |
alfik |
);
|
| 33 |
|
|
|
| 34 |
|
|
//------------------------------------------------------------------------------
|
| 35 |
|
|
|
| 36 |
|
|
always @(posedge clk or negedge rst_n) begin
|
| 37 |
7 |
alfik |
if(rst_n == 1'b0) sd_clk <= 1'b0;
|
| 38 |
|
|
else if(write_stop_sd_clk || read_stop_sd_clk) sd_clk <= 1'b0;
|
| 39 |
|
|
else sd_clk <= ~(sd_clk);
|
| 40 |
2 |
alfik |
end
|
| 41 |
|
|
|
| 42 |
7 |
alfik |
//------------------------------------------------------------------------------
|
| 43 |
2 |
alfik |
|
| 44 |
7 |
alfik |
wire current_dat0;
|
| 45 |
2 |
alfik |
|
| 46 |
7 |
alfik |
wire wr_data_done;
|
| 47 |
|
|
wire wr_data_last_in_sector;
|
| 48 |
|
|
wire wr_error;
|
| 49 |
|
|
wire wr_finished_sector;
|
| 50 |
|
|
|
| 51 |
|
|
wire rd_data_done;
|
| 52 |
|
|
wire rd_data_last_in_sector;
|
| 53 |
|
|
wire [31:0] rd_data;
|
| 54 |
|
|
wire rd_error;
|
| 55 |
|
|
|
| 56 |
|
|
dat dat_inst(
|
| 57 |
|
|
.clk (clk),
|
| 58 |
|
|
.rst_n (rst_n),
|
| 59 |
2 |
alfik |
|
| 60 |
7 |
alfik |
//
|
| 61 |
|
|
.sd_clk_is_one (sd_clk), //input
|
| 62 |
|
|
|
| 63 |
|
|
//
|
| 64 |
|
|
.wr_async_data_ready (wr_async_data_ready), //input
|
| 65 |
|
|
.wr_async_data (wr_async_data), //input [31:0]
|
| 66 |
|
|
.wr_data_done (wr_data_done), //output
|
| 67 |
|
|
.wr_data_last_in_sector (wr_data_last_in_sector), //output
|
| 68 |
|
|
.wr_error (wr_error), //output
|
| 69 |
|
|
.wr_finished_sector (wr_finished_sector), //output
|
| 70 |
|
|
|
| 71 |
|
|
//
|
| 72 |
|
|
.rd_async_start (rd_async_start), //input
|
| 73 |
|
|
.rd_async_abort (rd_async_abort), //input
|
| 74 |
|
|
.rd_data_done (rd_data_done), //output
|
| 75 |
|
|
.rd_data_last_in_sector (rd_data_last_in_sector), //output
|
| 76 |
|
|
.rd_data (rd_data), //output [31:0]
|
| 77 |
|
|
.rd_error (rd_error), //output
|
| 78 |
|
|
|
| 79 |
|
|
//
|
| 80 |
|
|
.current_dat0 (current_dat0), //output
|
| 81 |
|
|
|
| 82 |
|
|
//
|
| 83 |
|
|
.sd_dat (sd_dat) //inout [3:0]
|
| 84 |
|
|
);
|
| 85 |
2 |
alfik |
|
| 86 |
7 |
alfik |
//------------------------------------------------------------------------------
|
| 87 |
2 |
alfik |
|
| 88 |
7 |
alfik |
wire reply_ready;
|
| 89 |
|
|
wire [135:0] reply_contents;
|
| 90 |
|
|
wire reply_error;
|
| 91 |
2 |
alfik |
|
| 92 |
7 |
alfik |
wire cmd_ready = (operation_write)? write_cmd_ready : (operation_read)? read_cmd_ready : (operation_init)? init_cmd_ready : 1'b0;
|
| 93 |
|
|
wire [5:0] cmd_index = (operation_write)? write_cmd_index : (operation_read)? read_cmd_index : (operation_init)? init_cmd_index : 6'b0;
|
| 94 |
|
|
wire [31:0] cmd_arg = (operation_write)? write_cmd_arg : (operation_read)? read_cmd_arg : (operation_init)? init_cmd_arg : 32'b0;
|
| 95 |
|
|
wire [7:0] cmd_resp_length = (operation_write)? write_cmd_resp_length : (operation_read)? read_cmd_resp_length : (operation_init)? init_cmd_resp_length : 8'b0;
|
| 96 |
|
|
wire cmd_resp_has_crc7 = (operation_write)? write_cmd_resp_has_crc7 : (operation_read)? read_cmd_resp_has_crc7 : (operation_init)? init_cmd_resp_has_crc7 : 1'b0;
|
| 97 |
2 |
alfik |
|
| 98 |
7 |
alfik |
cmd cmd_inst(
|
| 99 |
|
|
.clk (clk),
|
| 100 |
|
|
.rst_n (rst_n),
|
| 101 |
|
|
|
| 102 |
|
|
//
|
| 103 |
|
|
.sd_clk_is_one (sd_clk), //input
|
| 104 |
|
|
|
| 105 |
|
|
//
|
| 106 |
|
|
.cmd_ready (cmd_ready), //input
|
| 107 |
|
|
.cmd_index (cmd_index), //input [5:0]
|
| 108 |
|
|
.cmd_arg (cmd_arg), //input [31:0]
|
| 109 |
|
|
.cmd_resp_length (cmd_resp_length), //input [7:0]
|
| 110 |
|
|
.cmd_resp_has_crc7 (cmd_resp_has_crc7), //input
|
| 111 |
|
|
|
| 112 |
|
|
//
|
| 113 |
|
|
.reply_ready (reply_ready), //output
|
| 114 |
|
|
.reply_contents (reply_contents), //output [135:0]
|
| 115 |
|
|
.reply_error (reply_error), //output
|
| 116 |
|
|
|
| 117 |
|
|
//
|
| 118 |
|
|
.sd_cmd (sd_cmd) //inout
|
| 119 |
|
|
);
|
| 120 |
2 |
alfik |
|
| 121 |
7 |
alfik |
//------------------------------------------------------------------------------
|
| 122 |
2 |
alfik |
|
| 123 |
7 |
alfik |
wire [31:0] read_data;
|
| 124 |
|
|
wire read_done;
|
| 125 |
2 |
alfik |
|
| 126 |
7 |
alfik |
wire write_done;
|
| 127 |
2 |
alfik |
|
| 128 |
7 |
alfik |
avalon_master avalon_master_inst(
|
| 129 |
|
|
.clk (clk),
|
| 130 |
|
|
.rst_n (rst_n),
|
| 131 |
|
|
|
| 132 |
|
|
//
|
| 133 |
|
|
.avm_address (avm_address), //output [31:0]
|
| 134 |
|
|
.avm_waitrequest (avm_waitrequest), //input
|
| 135 |
|
|
.avm_read (avm_read), //output
|
| 136 |
|
|
.avm_readdata (avm_readdata), //input [31:0]
|
| 137 |
|
|
.avm_readdatavalid (avm_readdatavalid), //input
|
| 138 |
|
|
.avm_write (avm_write), //output
|
| 139 |
|
|
.avm_writedata (avm_writedata), //output [31:0]
|
| 140 |
|
|
|
| 141 |
|
|
//
|
| 142 |
|
|
.avalon_address_base (avalon_address_base), //input [31:0]
|
| 143 |
|
|
|
| 144 |
|
|
//
|
| 145 |
|
|
.read_start (read_start), //input
|
| 146 |
|
|
.read_next (read_next), //input
|
| 147 |
|
|
.read_data (read_data), //output [31:0]
|
| 148 |
|
|
.read_done (read_done), //output
|
| 149 |
|
|
|
| 150 |
|
|
//
|
| 151 |
|
|
.write_start (write_start), //input
|
| 152 |
|
|
.write_next (write_next), //input
|
| 153 |
|
|
.write_data (write_data), //input [31:0]
|
| 154 |
|
|
.write_done (write_done) //output
|
| 155 |
|
|
);
|
| 156 |
2 |
alfik |
|
| 157 |
7 |
alfik |
//------------------------------------------------------------------------------
|
| 158 |
2 |
alfik |
|
| 159 |
7 |
alfik |
wire operation_write;
|
| 160 |
|
|
wire operation_read;
|
| 161 |
|
|
wire operation_init;
|
| 162 |
2 |
alfik |
|
| 163 |
7 |
alfik |
wire operation_sector_last;
|
| 164 |
2 |
alfik |
|
| 165 |
7 |
alfik |
wire [31:0] sd_address;
|
| 166 |
|
|
wire [31:0] avalon_address_base;
|
| 167 |
2 |
alfik |
|
| 168 |
7 |
alfik |
wire operation_sector_update = (operation_write && write_operation_sector_update) || (operation_read && read_operation_sector_update);
|
| 169 |
|
|
wire operation_finished_ok = (operation_write && write_operation_finished_ok) || (operation_read && read_operation_finished_ok) || (operation_init && init_operation_finished_ok);
|
| 170 |
|
|
wire operation_finished_with_error = (operation_write && write_operation_finished_with_error) || (operation_read && read_operation_finished_with_error) || (operation_init && init_operation_finished_with_error);
|
| 171 |
2 |
alfik |
|
| 172 |
7 |
alfik |
avalon_slave avalon_slave_inst(
|
| 173 |
|
|
.clk (clk),
|
| 174 |
|
|
.rst_n (rst_n),
|
| 175 |
|
|
|
| 176 |
|
|
//
|
| 177 |
|
|
.avs_address (avs_address), //input [1:0]
|
| 178 |
|
|
.avs_read (avs_read), //input
|
| 179 |
|
|
.avs_readdata (avs_readdata), //output [31:0]
|
| 180 |
|
|
.avs_write (avs_write), //input
|
| 181 |
|
|
.avs_writedata (avs_writedata), //input [31:0]
|
| 182 |
|
|
|
| 183 |
|
|
//
|
| 184 |
|
|
.operation_init (operation_init), //output
|
| 185 |
|
|
.operation_read (operation_read), //output
|
| 186 |
|
|
.operation_write (operation_write), //output
|
| 187 |
|
|
|
| 188 |
|
|
.operation_sector_update (operation_sector_update), //input
|
| 189 |
|
|
.operation_sector_last (operation_sector_last), //output
|
| 190 |
2 |
alfik |
|
| 191 |
7 |
alfik |
.operation_finished_ok (operation_finished_ok), //input
|
| 192 |
|
|
.operation_finished_with_error (operation_finished_with_error), //input
|
| 193 |
|
|
|
| 194 |
|
|
//
|
| 195 |
|
|
.sd_address (sd_address), //output [31:0]
|
| 196 |
|
|
.avalon_address_base (avalon_address_base) //output [31:0]
|
| 197 |
|
|
);
|
| 198 |
2 |
alfik |
|
| 199 |
7 |
alfik |
//------------------------------------------------------------------------------
|
| 200 |
2 |
alfik |
|
| 201 |
7 |
alfik |
wire init_operation_finished_ok;
|
| 202 |
|
|
wire init_operation_finished_with_error;
|
| 203 |
2 |
alfik |
|
| 204 |
7 |
alfik |
wire init_cmd_ready;
|
| 205 |
|
|
wire [5:0] init_cmd_index;
|
| 206 |
|
|
wire [31:0] init_cmd_arg;
|
| 207 |
|
|
wire [7:0] init_cmd_resp_length;
|
| 208 |
|
|
wire init_cmd_resp_has_crc7;
|
| 209 |
2 |
alfik |
|
| 210 |
7 |
alfik |
card_init card_init_inst(
|
| 211 |
|
|
.clk (clk),
|
| 212 |
|
|
.rst_n (rst_n),
|
| 213 |
|
|
|
| 214 |
|
|
//
|
| 215 |
|
|
.operation_init (operation_init), //input
|
| 216 |
|
|
.operation_finished_ok (init_operation_finished_ok), //output
|
| 217 |
|
|
.operation_finished_with_error (init_operation_finished_with_error), //output
|
| 218 |
|
|
|
| 219 |
|
|
//
|
| 220 |
|
|
.cmd_ready (init_cmd_ready), //output
|
| 221 |
|
|
.cmd_index (init_cmd_index), //output [5:0]
|
| 222 |
|
|
.cmd_arg (init_cmd_arg), //output [31:0]
|
| 223 |
|
|
.cmd_resp_length (init_cmd_resp_length), //output [7:0]
|
| 224 |
|
|
.cmd_resp_has_crc7 (init_cmd_resp_has_crc7), //output
|
| 225 |
2 |
alfik |
|
| 226 |
7 |
alfik |
.reply_ready (reply_ready), //input
|
| 227 |
|
|
.reply_contents (reply_contents), //input [135:0]
|
| 228 |
|
|
.reply_error (reply_error), //input
|
| 229 |
|
|
|
| 230 |
|
|
//
|
| 231 |
|
|
.current_dat0 (current_dat0) //input
|
| 232 |
|
|
);
|
| 233 |
2 |
alfik |
|
| 234 |
7 |
alfik |
//------------------------------------------------------------------------------
|
| 235 |
2 |
alfik |
|
| 236 |
7 |
alfik |
wire read_operation_sector_update;
|
| 237 |
|
|
wire read_operation_finished_ok;
|
| 238 |
|
|
wire read_operation_finished_with_error;
|
| 239 |
2 |
alfik |
|
| 240 |
7 |
alfik |
wire rd_async_start;
|
| 241 |
|
|
wire rd_async_abort;
|
| 242 |
2 |
alfik |
|
| 243 |
7 |
alfik |
wire read_stop_sd_clk;
|
| 244 |
2 |
alfik |
|
| 245 |
7 |
alfik |
wire read_cmd_ready;
|
| 246 |
|
|
wire [5:0] read_cmd_index;
|
| 247 |
|
|
wire [31:0] read_cmd_arg;
|
| 248 |
|
|
wire [7:0] read_cmd_resp_length;
|
| 249 |
|
|
wire read_cmd_resp_has_crc7;
|
| 250 |
2 |
alfik |
|
| 251 |
7 |
alfik |
wire write_start;
|
| 252 |
|
|
wire write_next;
|
| 253 |
|
|
wire [31:0] write_data;
|
| 254 |
2 |
alfik |
|
| 255 |
7 |
alfik |
card_read card_read_inst(
|
| 256 |
|
|
.clk (clk),
|
| 257 |
|
|
.rst_n (rst_n),
|
| 258 |
|
|
|
| 259 |
|
|
//
|
| 260 |
|
|
.operation_read (operation_read), //input
|
| 261 |
|
|
|
| 262 |
|
|
.operation_sector_last (operation_sector_last), //input
|
| 263 |
|
|
.operation_sector_update (read_operation_sector_update), //output
|
| 264 |
|
|
|
| 265 |
|
|
.operation_finished_ok (read_operation_finished_ok), //output
|
| 266 |
|
|
.operation_finished_with_error (read_operation_finished_with_error), //output
|
| 267 |
|
|
|
| 268 |
|
|
//
|
| 269 |
|
|
.cmd_ready (read_cmd_ready), //output
|
| 270 |
|
|
.cmd_index (read_cmd_index), //output [5:0]
|
| 271 |
|
|
.cmd_arg (read_cmd_arg), //output [31:0]
|
| 272 |
|
|
.cmd_resp_length (read_cmd_resp_length), //output [7:0]
|
| 273 |
|
|
.cmd_resp_has_crc7 (read_cmd_resp_has_crc7), //output
|
| 274 |
|
|
|
| 275 |
|
|
.reply_ready (reply_ready), //input
|
| 276 |
|
|
.reply_contents (reply_contents), //input [135:0]
|
| 277 |
|
|
.reply_error (reply_error), //input
|
| 278 |
|
|
|
| 279 |
|
|
//
|
| 280 |
|
|
.write_start (write_start), //output
|
| 281 |
|
|
.write_next (write_next), //output
|
| 282 |
|
|
.write_data (write_data), //output [31:0]
|
| 283 |
|
|
.write_done (write_done), //input
|
| 284 |
|
|
|
| 285 |
|
|
//
|
| 286 |
|
|
.rd_async_start (rd_async_start), //output
|
| 287 |
|
|
.rd_async_abort (rd_async_abort), //output
|
| 288 |
|
|
.rd_data_done (rd_data_done), //input
|
| 289 |
|
|
.rd_data_last_in_sector (rd_data_last_in_sector), //input
|
| 290 |
|
|
.rd_data (rd_data), //input [31:0]
|
| 291 |
|
|
.rd_error (rd_error), //input
|
| 292 |
|
|
|
| 293 |
|
|
//
|
| 294 |
|
|
.sd_address (sd_address), //input [31:0]
|
| 295 |
|
|
|
| 296 |
|
|
//
|
| 297 |
|
|
.current_dat0 (current_dat0), //input
|
| 298 |
|
|
|
| 299 |
|
|
//
|
| 300 |
|
|
.stop_sd_clk (read_stop_sd_clk) //output
|
| 301 |
|
|
);
|
| 302 |
2 |
alfik |
|
| 303 |
7 |
alfik |
//------------------------------------------------------------------------------
|
| 304 |
2 |
alfik |
|
| 305 |
7 |
alfik |
wire write_operation_sector_update;
|
| 306 |
|
|
wire write_operation_finished_ok;
|
| 307 |
|
|
wire write_operation_finished_with_error;
|
| 308 |
2 |
alfik |
|
| 309 |
7 |
alfik |
wire read_start;
|
| 310 |
|
|
wire read_next;
|
| 311 |
|
|
|
| 312 |
|
|
wire write_stop_sd_clk;
|
| 313 |
|
|
|
| 314 |
|
|
wire write_cmd_ready;
|
| 315 |
|
|
wire [5:0] write_cmd_index;
|
| 316 |
|
|
wire [31:0] write_cmd_arg;
|
| 317 |
|
|
wire [7:0] write_cmd_resp_length;
|
| 318 |
|
|
wire write_cmd_resp_has_crc7;
|
| 319 |
|
|
|
| 320 |
|
|
wire wr_async_data_ready;
|
| 321 |
|
|
wire [31:0] wr_async_data;
|
| 322 |
|
|
|
| 323 |
|
|
card_write card_write_inst(
|
| 324 |
|
|
.clk (clk),
|
| 325 |
|
|
.rst_n (rst_n),
|
| 326 |
|
|
|
| 327 |
|
|
//
|
| 328 |
|
|
.operation_write (operation_write), //input
|
| 329 |
|
|
|
| 330 |
|
|
.operation_sector_last (operation_sector_last), //input
|
| 331 |
|
|
.operation_sector_update (write_operation_sector_update), //output
|
| 332 |
|
|
|
| 333 |
|
|
.operation_finished_ok (write_operation_finished_ok), //output
|
| 334 |
|
|
.operation_finished_with_error (write_operation_finished_with_error), //output
|
| 335 |
|
|
|
| 336 |
|
|
//
|
| 337 |
|
|
.cmd_ready (write_cmd_ready), //output
|
| 338 |
|
|
.cmd_index (write_cmd_index), //output [5:0]
|
| 339 |
|
|
.cmd_arg (write_cmd_arg), //output [31:0]
|
| 340 |
|
|
.cmd_resp_length (write_cmd_resp_length), //output [7:0]
|
| 341 |
|
|
.cmd_resp_has_crc7 (write_cmd_resp_has_crc7), //output
|
| 342 |
|
|
|
| 343 |
|
|
.reply_ready (reply_ready), //input
|
| 344 |
|
|
.reply_contents (reply_contents), //input [135:0]
|
| 345 |
|
|
.reply_error (reply_error), //input
|
| 346 |
|
|
|
| 347 |
|
|
//
|
| 348 |
|
|
.read_start (read_start), //output
|
| 349 |
|
|
.read_next (read_next), //output
|
| 350 |
|
|
.read_data (read_data), //input [31:0]
|
| 351 |
|
|
.read_done (read_done), //input
|
| 352 |
|
|
|
| 353 |
|
|
//
|
| 354 |
|
|
.wr_async_data_ready (wr_async_data_ready), //output
|
| 355 |
|
|
.wr_async_data (wr_async_data), //output [31:0]
|
| 356 |
|
|
.wr_data_done (wr_data_done), //input
|
| 357 |
|
|
.wr_data_last_in_sector (wr_data_last_in_sector), //input
|
| 358 |
|
|
.wr_error (wr_error), //input
|
| 359 |
|
|
.wr_finished_sector (wr_finished_sector), //input
|
| 360 |
|
|
|
| 361 |
|
|
//
|
| 362 |
|
|
.sd_address (sd_address), //input [31:0]
|
| 363 |
|
|
|
| 364 |
|
|
//
|
| 365 |
|
|
.current_dat0 (current_dat0), //input
|
| 366 |
|
|
|
| 367 |
|
|
//
|
| 368 |
|
|
.stop_sd_clk (write_stop_sd_clk) //output
|
| 369 |
|
|
);
|
| 370 |
|
|
|
| 371 |
|
|
//------------------------------------------------------------------------------
|
| 372 |
|
|
|
| 373 |
2 |
alfik |
endmodule
|