[555] | 1 | /* |
---|
| 2 | -*- c++ -*- |
---|
[552] | 3 | * |
---|
| 4 | * SOCLIB_LGPL_HEADER_BEGIN |
---|
| 5 | * |
---|
| 6 | * This file is part of SoCLib, GNU LGPLv2.1. |
---|
| 7 | * |
---|
| 8 | * SoCLib is free software; you can redistribute it and/or modify it |
---|
| 9 | * under the terms of the GNU Lesser General Public License as published |
---|
| 10 | * by the Free Software Foundation; version 2.1 of the License. |
---|
| 11 | * |
---|
| 12 | * SoCLib is distributed in the hope that it will be useful, but |
---|
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 15 | * Lesser General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU Lesser General Public |
---|
| 18 | * License along with SoCLib; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
| 20 | * 02110-1301 USA |
---|
| 21 | * |
---|
| 22 | * SOCLIB_LGPL_HEADER_END |
---|
| 23 | * |
---|
| 24 | * Copyright (c) UPMC, Lip6, SoC |
---|
| 25 | * manuel.bouyer@lip6.fr october 2013 |
---|
| 26 | * |
---|
| 27 | * Maintainers: bouyer |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include <stdint.h> |
---|
| 31 | #include <errno.h> |
---|
| 32 | #include <iostream> |
---|
| 33 | #include <fcntl.h> |
---|
| 34 | #include <cassert> |
---|
| 35 | #include "sdmmc.h" |
---|
| 36 | |
---|
| 37 | namespace soclib { namespace caba { |
---|
| 38 | |
---|
| 39 | using namespace soclib::caba; |
---|
| 40 | |
---|
| 41 | //////////////////////// |
---|
[557] | 42 | void SdMMC::genMealy() |
---|
[552] | 43 | { |
---|
| 44 | if(p_resetn.read() == false) |
---|
| 45 | { |
---|
[556] | 46 | spi_fsm = S_IDLE; |
---|
[552] | 47 | m_acmd = false; |
---|
| 48 | m_sdstate = SD_IDLE; |
---|
| 49 | return; |
---|
| 50 | } |
---|
| 51 | if (p_spi_ss.read()) { |
---|
[556] | 52 | if (spi_fsm != S_IDLE) { |
---|
[555] | 53 | std::cerr << name() << " deselect but not idle, state " |
---|
[556] | 54 | << std::dec << spi_fsm << " last cmd " << (int)command |
---|
| 55 | << " args " << std::hex << args << std::dec |
---|
| 56 | << " bitcount " << (int)spi_bitcount |
---|
[555] | 57 | << " idx " << m_data_idx << " len_snd " << m_datalen_snd |
---|
| 58 | << " len_rcv " << m_datalen_rcv << std::endl; |
---|
| 59 | } |
---|
[556] | 60 | spi_fsm = S_IDLE; |
---|
| 61 | spi_clk = p_spi_clk; |
---|
[552] | 62 | return; |
---|
| 63 | } |
---|
| 64 | |
---|
[556] | 65 | switch(spi_fsm) { |
---|
[552] | 66 | case S_IDLE: |
---|
[556] | 67 | if (p_spi_clk.read() == 1 && spi_clk == 0) { |
---|
[552] | 68 | // rising edge |
---|
[556] | 69 | command = (command << 1) | p_spi_mosi; |
---|
| 70 | spi_bitcount = 6; |
---|
| 71 | spi_fsm = S_RECEIVE_CMD; |
---|
[552] | 72 | } |
---|
| 73 | break; |
---|
| 74 | case S_RECEIVE_CMD: |
---|
[556] | 75 | if (p_spi_clk.read() == 1 && spi_clk == 0) { |
---|
[552] | 76 | // rising edge |
---|
[556] | 77 | command = (command << 1) | p_spi_mosi; |
---|
| 78 | if (spi_bitcount == 0) { |
---|
| 79 | if ((command & 0x80) == 0) { |
---|
| 80 | spi_fsm = S_RECEIVE_ARGS_START; |
---|
[552] | 81 | } else { |
---|
[555] | 82 | #ifdef SOCLIB_MODULE_DEBUG0 |
---|
[556] | 83 | std::cout << name() << " S_RECEIVE_CMD " << std::hex << (int)command << std::endl; |
---|
[552] | 84 | #endif |
---|
[556] | 85 | spi_fsm = S_IDLE; |
---|
[552] | 86 | } |
---|
[556] | 87 | } else { |
---|
| 88 | spi_bitcount = spi_bitcount - 1; |
---|
[552] | 89 | } |
---|
| 90 | } |
---|
| 91 | break; |
---|
| 92 | case S_RECEIVE_ARGS_START: |
---|
[556] | 93 | if (p_spi_clk.read() == 1 && spi_clk == 0) { |
---|
[552] | 94 | // rising edge |
---|
[556] | 95 | args = (args << 1) | p_spi_mosi; |
---|
| 96 | spi_bitcount = 30; |
---|
| 97 | spi_fsm = S_RECEIVE_ARGS; |
---|
[552] | 98 | } |
---|
| 99 | break; |
---|
| 100 | case S_RECEIVE_ARGS: |
---|
[556] | 101 | if (p_spi_clk.read() == 1 && spi_clk == 0) { |
---|
[552] | 102 | // rising edge |
---|
[556] | 103 | args = (args << 1) | p_spi_mosi; |
---|
| 104 | if (spi_bitcount == 0) { |
---|
| 105 | spi_bitcount = 7; |
---|
| 106 | spi_fsm = S_RECEIVE_CRC; |
---|
| 107 | } else { |
---|
| 108 | spi_bitcount = spi_bitcount - 1; |
---|
[552] | 109 | } |
---|
| 110 | } |
---|
| 111 | break; |
---|
| 112 | case S_RECEIVE_CRC: |
---|
[556] | 113 | if (p_spi_clk.read() == 1 && spi_clk == 0) { |
---|
[552] | 114 | // rising edge |
---|
[556] | 115 | cmdcrc = (cmdcrc << 1) | p_spi_mosi; |
---|
| 116 | if (spi_bitcount == 0) { |
---|
| 117 | handle_sdmmc_cmd(command, args); |
---|
| 118 | spi_bitcount = 0; // SEND_DATA will reset it |
---|
| 119 | spi_fsm = S_SEND_DATA; |
---|
[552] | 120 | m_data_idx = 0; |
---|
| 121 | } else { |
---|
[556] | 122 | spi_bitcount = spi_bitcount - 1; |
---|
[552] | 123 | } |
---|
| 124 | } |
---|
| 125 | break; |
---|
| 126 | |
---|
| 127 | case S_SEND_DATA: |
---|
[556] | 128 | if (p_spi_clk.read() == 0 && spi_clk == 1) { |
---|
[552] | 129 | // falling edge |
---|
[556] | 130 | if (spi_bitcount == 0) { |
---|
[552] | 131 | if (m_data_idx != m_datalen_snd) { |
---|
[556] | 132 | spi_shiftreg = m_databuf[m_data_idx]; |
---|
| 133 | spi_bitcount = 7; |
---|
| 134 | spi_fsm = S_SEND_DATA; |
---|
[552] | 135 | m_data_idx++; |
---|
[555] | 136 | #ifdef SOCLIB_MODULE_DEBUG0 |
---|
[552] | 137 | std::cout << name() << " S_SEND_DATA " << std::dec << m_datalen_snd << " idx " << m_data_idx << " " << std::hex << (uint32_t)m_databuf[m_data_idx] << std::endl; |
---|
| 138 | #endif |
---|
| 139 | } else if (m_datalen_rcv != 0) { |
---|
[556] | 140 | spi_fsm = S_RECEIVE_DATA_WAIT; |
---|
| 141 | spi_bitcount = 7; |
---|
[552] | 142 | m_data_idx = 0; |
---|
| 143 | } else { |
---|
[556] | 144 | spi_fsm = S_IDLE; |
---|
[552] | 145 | } |
---|
| 146 | } else { |
---|
[556] | 147 | spi_bitcount = spi_bitcount - 1; |
---|
| 148 | spi_shiftreg = spi_shiftreg << 1; |
---|
[552] | 149 | } |
---|
| 150 | } |
---|
| 151 | break; |
---|
[555] | 152 | case S_RECEIVE_DATA_WAIT: |
---|
[556] | 153 | if (p_spi_clk.read() == 1 && spi_clk == 0) { |
---|
| 154 | // rising edge |
---|
[555] | 155 | uint8_t s_data; |
---|
| 156 | s_data = (m_databuf[0] << 1) | p_spi_mosi; |
---|
| 157 | m_databuf[0] = s_data; |
---|
[556] | 158 | if (spi_bitcount == 0) { |
---|
[555] | 159 | #ifdef SOCLIB_MODULE_DEBUG |
---|
| 160 | std::cout << name() << " S_RECEIVE_DATA_WAIT " << std::dec << (int)s_data << std::endl; |
---|
| 161 | #endif |
---|
[556] | 162 | spi_bitcount = 7; |
---|
[555] | 163 | if (s_data == 0xfe) { // data start token |
---|
[556] | 164 | spi_fsm = S_RECEIVE_DATA; |
---|
[555] | 165 | m_data_idx = 1; |
---|
| 166 | } else { |
---|
| 167 | #ifdef SOCLIB_MODULE_DEBUG |
---|
| 168 | std::cout << name() << " S_RECEIVE_DATA_WAIT " << std::hex << (int)s_data << std::endl; |
---|
| 169 | #endif |
---|
[556] | 170 | spi_fsm = S_RECEIVE_DATA_WAIT; |
---|
[555] | 171 | } |
---|
[556] | 172 | } else { |
---|
| 173 | spi_bitcount = spi_bitcount - 1; |
---|
[555] | 174 | } |
---|
| 175 | } |
---|
| 176 | break; |
---|
| 177 | case S_RECEIVE_DATA: |
---|
[556] | 178 | if (p_spi_clk.read() == 1 && spi_clk == 0) { |
---|
[555] | 179 | // rising edge |
---|
| 180 | m_databuf[m_data_idx] = (m_databuf[m_data_idx] << 1) | p_spi_mosi; |
---|
[556] | 181 | if (spi_bitcount == 0) { |
---|
[555] | 182 | m_data_idx++; |
---|
| 183 | if (m_data_idx != m_datalen_rcv) { |
---|
[556] | 184 | spi_fsm = S_RECEIVE_DATA; |
---|
| 185 | spi_bitcount = 7; |
---|
[555] | 186 | } else { |
---|
[556] | 187 | handle_sdmmc_write(command, args); |
---|
[555] | 188 | if (m_datalen_snd > 0) { |
---|
[556] | 189 | spi_bitcount = 0; // SEND_DATA will reset it |
---|
| 190 | spi_fsm = S_SEND_DATA; |
---|
[555] | 191 | m_data_idx = 0; |
---|
| 192 | } else { |
---|
[556] | 193 | spi_fsm = S_IDLE; |
---|
[555] | 194 | } |
---|
| 195 | } |
---|
| 196 | } else { |
---|
[556] | 197 | spi_bitcount = spi_bitcount - 1; |
---|
[555] | 198 | } |
---|
| 199 | } |
---|
| 200 | break; |
---|
[552] | 201 | } |
---|
| 202 | |
---|
[557] | 203 | //// now genrate output signal |
---|
| 204 | |
---|
[556] | 205 | switch(spi_fsm) { |
---|
[552] | 206 | case S_IDLE: |
---|
| 207 | p_spi_miso = !p_spi_ss.read(); |
---|
| 208 | break; |
---|
| 209 | case S_SEND_DATA: |
---|
[556] | 210 | p_spi_miso = (spi_shiftreg & 0x80) != 0; |
---|
[552] | 211 | break; |
---|
| 212 | default: |
---|
| 213 | p_spi_miso = !p_spi_ss.read(); |
---|
| 214 | break; |
---|
| 215 | } |
---|
[557] | 216 | spi_clk = p_spi_clk.read(); |
---|
| 217 | } // end GenMealy() |
---|
[552] | 218 | |
---|
| 219 | |
---|
| 220 | ////////////////////// |
---|
| 221 | void SdMMC::handle_sdmmc_cmd(uint8_t cmd, uint32_t data) |
---|
| 222 | { |
---|
| 223 | m_datalen_rcv = 0; |
---|
| 224 | m_databuf[0] = 0x04; // illegal command |
---|
| 225 | m_datalen_snd = 1; |
---|
| 226 | |
---|
| 227 | if (m_sdstate == SD_IDLE) |
---|
| 228 | m_databuf[0] |= 0x01; // idle |
---|
| 229 | |
---|
| 230 | if ((cmd & 0x40) == 0) { |
---|
| 231 | //illegal command |
---|
| 232 | return; |
---|
| 233 | } |
---|
| 234 | cmd &= 0x3f; |
---|
| 235 | if (m_acmd) { |
---|
[555] | 236 | #ifdef SOCLIB_MODULE_DEBUG0 |
---|
[556] | 237 | std::cout << name() << " new acmd " << std::dec << (int)cmd << " args " << std::hex << data << " crc " << (int)cmdcrc << std::endl; |
---|
[552] | 238 | #endif |
---|
| 239 | m_acmd = false; |
---|
| 240 | switch (cmd) { |
---|
| 241 | case 41: |
---|
| 242 | m_databuf[0] = 0x0; // card ready |
---|
| 243 | m_datalen_snd = 1; |
---|
| 244 | m_sdstate = SD_READY; |
---|
| 245 | break; |
---|
| 246 | case 51: |
---|
| 247 | // send SCR |
---|
| 248 | m_databuf[ 0] = (m_sdstate == SD_IDLE) ? 0x1 : 0x0; // R1 |
---|
| 249 | m_databuf[ 1] = 0xfe; // data token |
---|
| 250 | m_databuf[ 2] = 0x00; // SCR_STRUCTURE / SD_SPEC |
---|
| 251 | m_databuf[ 3] = 0x05; // DATA_STAT_AFTER_ERASE, SD_SECURITY, SD_BUS_WIDTHS |
---|
| 252 | m_databuf[ 4] = 0; // SD_SPEC3, EX_SECURITY, SD_SPEC4 |
---|
| 253 | m_databuf[ 5] = 0; // CMD_SUPPORT |
---|
| 254 | m_databuf[ 6] = 0; // vendor specific |
---|
| 255 | m_databuf[ 7] = 0; // vendor specific |
---|
| 256 | m_databuf[ 8] = 0; // vendor specific |
---|
| 257 | m_databuf[ 9] = 0; // vendor specific |
---|
| 258 | m_databuf[10] = 0x0; // CRC16 |
---|
| 259 | m_databuf[11] = 0x0; // CRC16 |
---|
| 260 | m_datalen_snd = 12; |
---|
| 261 | break; |
---|
| 262 | default: |
---|
| 263 | std::cout << name() << " unknown acmd " << std::dec |
---|
| 264 | << (int)cmd << std::endl; |
---|
| 265 | break; // return illegal command |
---|
| 266 | } |
---|
| 267 | } else { |
---|
[555] | 268 | #ifdef SOCLIB_MODULE_DEBUG0 |
---|
[556] | 269 | std::cout << name() << " new cmd " << std::dec << (int)cmd << " args " << std::hex << data << " crc " << (int)cmdcrc << std::endl; |
---|
[552] | 270 | #endif |
---|
| 271 | switch (cmd) { |
---|
| 272 | case 0: |
---|
| 273 | m_databuf[0] = 0x1; |
---|
| 274 | m_datalen_snd = 1; |
---|
| 275 | m_sdstate = SD_IDLE; |
---|
| 276 | break; |
---|
| 277 | case 8: |
---|
| 278 | // reply with illegal command for now |
---|
| 279 | break; |
---|
| 280 | case 9: |
---|
| 281 | { |
---|
| 282 | // send CSD |
---|
| 283 | // we use a block len of 1024 |
---|
| 284 | uint32_t csize = ((m_device_size + (512 * 1024) - 1) / (512 * 1024)) - 1; |
---|
| 285 | m_databuf[ 0] = (m_sdstate == SD_IDLE) ? 0x1 : 0x0; // R1 |
---|
| 286 | m_databuf[ 1] = 0xfe; // data token |
---|
| 287 | m_databuf[ 2] = 0x00; // CSD_STRUCTURE |
---|
| 288 | m_databuf[ 3] = 0xe; // TAAC |
---|
| 289 | m_databuf[ 4] = 0; // NSAC |
---|
| 290 | m_databuf[ 5] = 0x32; // TRAN_SPEED |
---|
| 291 | m_databuf[ 6] = 0x5b; // CCC_H |
---|
| 292 | m_databuf[ 7] = 0x5a; // CCC_L + READ_BL_LEN |
---|
| 293 | m_databuf[ 8] = 0x80; // READ_BL_PARTIAL, R/W_BLK_MISALIGN, DSR_IMP |
---|
| 294 | m_databuf[ 8] |= (csize >> 10) & 0x03; // CSIZE[12-11] |
---|
| 295 | m_databuf[ 9] = (csize >> 2) & 0xff; // CSIZE[10-2] |
---|
| 296 | m_databuf[10] = (csize << 6) & 0xc0; // CSIZE[1-0] |
---|
| 297 | m_databuf[10] |= 0; // R_CURR_MIN, R_CURR_MAX |
---|
| 298 | m_databuf[11] = 0x3; // W_CURR_MIN, W_CURR_MAX, CSIZE_MULT[2-1]; |
---|
| 299 | m_databuf[12] = 0xff; // CSIZE_MULT[1], ERASE_BLK_EN, ERASE_SECTOR_SIZE[6-1] |
---|
| 300 | m_databuf[13] = 0x80; // ERASE_SECTOR_SIZE[0]. WP_GRP_SIZE |
---|
| 301 | m_databuf[14] = 0x0a; // WP_GRP_ENABLE, R2W_FACTOR, WRITE_BL_LEN[2-3] |
---|
| 302 | m_databuf[15] = 0x40; // WRITE_BL_LEN[0-1], WR_BL_PARTIAL |
---|
| 303 | m_databuf[16] = 0; // FILE_FORMAT |
---|
| 304 | m_databuf[17] = 0x1; // CRC7 |
---|
| 305 | m_databuf[18] = 0x0; // CRC16 |
---|
| 306 | m_databuf[19] = 0x0; // CRC16 |
---|
| 307 | m_datalen_snd = 20; |
---|
| 308 | break; |
---|
| 309 | } |
---|
| 310 | case 10: |
---|
| 311 | // send CID |
---|
| 312 | m_databuf[ 0] = (m_sdstate == SD_IDLE) ? 0x1 : 0x0; // R1 |
---|
| 313 | m_databuf[ 1] = 0xfe; // data token |
---|
| 314 | m_databuf[ 2] = 0xda; // MID |
---|
| 315 | m_databuf[ 3] = 'P'; // OID |
---|
| 316 | m_databuf[ 4] = '6'; // OID |
---|
| 317 | m_databuf[ 5] = 's'; // PNM |
---|
| 318 | m_databuf[ 6] = 'o'; // PNM |
---|
| 319 | m_databuf[ 7] = 'c'; // PNM |
---|
| 320 | m_databuf[ 8] = 's'; // PNM |
---|
| 321 | m_databuf[ 9] = 'd'; // PNM |
---|
| 322 | m_databuf[10] = 0x01; // PRV |
---|
| 323 | m_databuf[11] = 0xde; // PSN |
---|
| 324 | m_databuf[12] = 0xad; // PSN |
---|
| 325 | m_databuf[13] = 0xbe; // PSN |
---|
| 326 | m_databuf[14] = 0xef; // PSN |
---|
| 327 | m_databuf[15] = 10; // MDT |
---|
| 328 | m_databuf[16] = 13; // MDT |
---|
| 329 | m_databuf[17] = 0x1; // CRC7 |
---|
| 330 | m_databuf[18] = 0x0; // CRC16 |
---|
| 331 | m_databuf[19] = 0x0; // CRC16 |
---|
| 332 | m_datalen_snd = 20; |
---|
| 333 | break; |
---|
| 334 | case 16: |
---|
| 335 | // set block size |
---|
| 336 | if (m_sdstate != SD_IDLE && data == 512) { |
---|
| 337 | m_databuf[0] = 0x00; |
---|
| 338 | m_datalen_snd = 1; |
---|
| 339 | } // else illegal command |
---|
| 340 | break; |
---|
| 341 | case 17: |
---|
| 342 | { |
---|
| 343 | int ret; |
---|
| 344 | // read data block |
---|
| 345 | if (m_sdstate == SD_IDLE) { |
---|
| 346 | // return illegal command |
---|
| 347 | return; |
---|
| 348 | } |
---|
| 349 | if (data >= m_device_size) { |
---|
| 350 | std::cerr << name() << " read: request " << data |
---|
| 351 | << " past end of file " << m_device_size << std::endl; |
---|
| 352 | m_databuf[0] = 0x00; // R1 OK |
---|
| 353 | m_databuf[1] = 0x08; // error tocken "out of range" |
---|
| 354 | m_datalen_snd = 2; |
---|
| 355 | return; |
---|
| 356 | } |
---|
| 357 | do { |
---|
| 358 | if (lseek(m_fd, data, SEEK_SET) < 0) { |
---|
| 359 | std::cerr << name() << " lseek: " << |
---|
| 360 | strerror(errno) << std::endl; |
---|
| 361 | m_databuf[0] = 0x00; // R1 OK |
---|
| 362 | m_databuf[1] = 0x02; // error tocken "CC err" |
---|
| 363 | m_datalen_snd = 2; |
---|
| 364 | return; |
---|
| 365 | } |
---|
| 366 | ret = read(m_fd, &m_databuf[2], 512); |
---|
| 367 | } while (ret < 0 && errno == EINTR); |
---|
| 368 | if (ret < 0) { |
---|
| 369 | std::cerr << name() << " read: " << |
---|
| 370 | strerror(errno) << std::endl; |
---|
| 371 | m_databuf[0] = 0x00; // R1 OK |
---|
| 372 | m_databuf[1] = 0x04; // error tocken "card ECC failed" |
---|
| 373 | m_datalen_snd = 2; |
---|
| 374 | return; |
---|
| 375 | } |
---|
| 376 | m_databuf[514] = m_databuf[515] = 0; // XXX CRC |
---|
| 377 | m_databuf[0] = 0x0; // R1 |
---|
| 378 | m_databuf[1] = 0xfe; // start block tocken |
---|
| 379 | m_datalen_snd = 516; |
---|
| 380 | break; |
---|
| 381 | } |
---|
[555] | 382 | case 24: |
---|
| 383 | { |
---|
| 384 | // write data block |
---|
| 385 | if (m_sdstate == SD_IDLE) { |
---|
| 386 | // return illegal command |
---|
| 387 | return; |
---|
| 388 | } |
---|
| 389 | #ifdef SOCLIB_MODULE_DEBUG |
---|
| 390 | std::cout << name() << " new cmd write " << std::dec << (int)cmd << " args " << std::hex << data << std::endl; |
---|
| 391 | #endif |
---|
| 392 | m_databuf[0] = 0x0; // R1 |
---|
| 393 | m_datalen_snd = 1; |
---|
| 394 | m_datalen_rcv = 512 + 2 + 1; // data + tocken + CRC |
---|
| 395 | break; |
---|
| 396 | } |
---|
[552] | 397 | case 55: |
---|
| 398 | // app-specific command follow |
---|
| 399 | m_acmd = true; |
---|
| 400 | m_databuf[0] = (m_sdstate == SD_IDLE) ? 0x1 : 0x0; |
---|
| 401 | m_datalen_snd = 1; |
---|
| 402 | break; |
---|
| 403 | case 58: |
---|
| 404 | // send OCR |
---|
| 405 | m_databuf[4] = (m_sdstate == SD_IDLE) ? 0x1 : 0x0; // R1 |
---|
| 406 | m_databuf[3] = 0x80; // power up complete, SDSC |
---|
| 407 | m_databuf[2] = 0xff; // all voltages supported |
---|
| 408 | m_databuf[1] = 0x00; |
---|
| 409 | m_databuf[0] = 0x00; |
---|
| 410 | m_datalen_snd = 5; |
---|
| 411 | break; |
---|
| 412 | default: |
---|
| 413 | std::cout << name() << " unknown cmd " << std::dec |
---|
| 414 | << (int)cmd << std::endl; |
---|
| 415 | break; // return illegal command |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | } |
---|
| 419 | |
---|
[555] | 420 | void SdMMC::handle_sdmmc_write(uint8_t cmd, uint32_t data) |
---|
[552] | 421 | { |
---|
[555] | 422 | m_datalen_rcv = 0; |
---|
| 423 | cmd &= 0x3f; |
---|
| 424 | #ifdef SOCLIB_MODULE_DEBUG |
---|
| 425 | std::cout << name() << " cmd write " << std::dec << (int)cmd << " args " << std::hex << data << std::endl; |
---|
| 426 | #endif |
---|
| 427 | switch(cmd) { |
---|
| 428 | case 24: |
---|
| 429 | { |
---|
| 430 | int ret; |
---|
| 431 | // write data block |
---|
| 432 | assert(m_sdstate != SD_IDLE && "can't write in idle state"); |
---|
| 433 | if (data >= m_device_size) { |
---|
| 434 | std::cerr << name() << " write: request " << data |
---|
| 435 | << " past end of file " << m_device_size << std::endl; |
---|
| 436 | m_databuf[0] = 0xd; // write error |
---|
| 437 | m_datalen_snd = 1; |
---|
| 438 | return; |
---|
| 439 | } |
---|
| 440 | do { |
---|
| 441 | if (lseek(m_fd, data, SEEK_SET) < 0) { |
---|
| 442 | std::cerr << name() << " lseek: " << |
---|
| 443 | strerror(errno) << std::endl; |
---|
| 444 | m_databuf[0] = 0xd; // write error |
---|
| 445 | m_datalen_snd = 1; |
---|
| 446 | return; |
---|
| 447 | } |
---|
| 448 | ret = write(m_fd, &m_databuf[1], 512); |
---|
| 449 | } while (ret < 0 && errno == EINTR); |
---|
| 450 | if (ret < 0) { |
---|
| 451 | std::cerr << name() << " write: " << |
---|
| 452 | strerror(errno) << std::endl; |
---|
| 453 | m_databuf[0] = 0xd; // write error |
---|
| 454 | m_datalen_snd = 1; |
---|
| 455 | return; |
---|
| 456 | } |
---|
| 457 | m_databuf[0] = 0x5; // write complete |
---|
| 458 | m_databuf[1] = 0x0; // busy |
---|
| 459 | m_datalen_snd = 2; |
---|
| 460 | break; |
---|
| 461 | } |
---|
| 462 | default: |
---|
| 463 | std::cerr << name() << " unkown write cmd " << std::dec << |
---|
| 464 | (int)cmd << std::endl; |
---|
| 465 | m_databuf[0] = 0xd; // write error; |
---|
| 466 | m_datalen_snd = 1; |
---|
| 467 | } |
---|
[552] | 468 | return; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 472 | SdMMC::SdMMC( sc_core::sc_module_name name, |
---|
| 473 | const std::string &filename, |
---|
| 474 | const uint32_t latency) |
---|
| 475 | |
---|
| 476 | : caba::BaseModule(name), |
---|
| 477 | m_latency(latency), |
---|
| 478 | p_clk("p_clk"), |
---|
| 479 | p_resetn("p_resetn"), |
---|
| 480 | p_spi_ss("p_spi_ss"), |
---|
| 481 | p_spi_clk("p_spi_clk"), |
---|
| 482 | p_spi_mosi("p_spi_mosi"), |
---|
| 483 | p_spi_miso("p_spi_miso") |
---|
| 484 | { |
---|
| 485 | std::cout << " - Building SdMMC " << name << std::endl; |
---|
| 486 | |
---|
[557] | 487 | SC_METHOD(genMealy); |
---|
[552] | 488 | dont_initialize(); |
---|
| 489 | sensitive << p_clk.neg(); |
---|
[557] | 490 | sensitive << p_resetn; |
---|
| 491 | sensitive << p_spi_ss; |
---|
| 492 | sensitive << p_spi_clk; |
---|
[552] | 493 | |
---|
| 494 | m_fd = ::open(filename.c_str(), O_RDWR); |
---|
| 495 | if ( m_fd < 0 ) |
---|
| 496 | { |
---|
| 497 | std::cout << "Error in component SdMMC : " << name |
---|
| 498 | << " Unable to open file " << filename << std::endl; |
---|
| 499 | exit(1); |
---|
| 500 | } |
---|
| 501 | m_device_size = lseek(m_fd, 0, SEEK_END); |
---|
| 502 | |
---|
| 503 | } // end constructor |
---|
| 504 | |
---|
| 505 | SdMMC::~SdMMC() |
---|
| 506 | { |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | |
---|
| 510 | ////////////////////////// |
---|
| 511 | void SdMMC::print_trace() |
---|
| 512 | { |
---|
| 513 | const char* spi_str[] = |
---|
| 514 | { |
---|
| 515 | "S_IDLE", |
---|
| 516 | "S_RECEIVE_CMD", |
---|
| 517 | "S_RECEIVE_ARGS_START", |
---|
| 518 | "S_RECEIVE_ARGS", |
---|
| 519 | "S_RECEIVE_CRC", |
---|
[555] | 520 | "S_RECEIVE_DATA_START", |
---|
[552] | 521 | "S_RECEIVE_DATA", |
---|
| 522 | "S_SEND_DATA", |
---|
| 523 | "S_NOP", |
---|
| 524 | }; |
---|
[556] | 525 | if (spi_clk != p_spi_clk.read()) { |
---|
| 526 | std::cout << name() << " SPI_FSM : " << spi_str[spi_fsm] |
---|
[552] | 527 | << std::dec |
---|
[556] | 528 | << " clk " << spi_clk << "->" << p_spi_clk << " ss " << p_spi_ss |
---|
[552] | 529 | << " mosi " << p_spi_mosi << " miso " << p_spi_miso |
---|
| 530 | << std::endl; |
---|
[556] | 531 | std::cout << " spi_shiftreg: " << std::hex << (int)spi_shiftreg |
---|
| 532 | << " spi_bitcount: " << (int)spi_bitcount |
---|
[552] | 533 | << std::endl; |
---|
| 534 | } |
---|
| 535 | } |
---|
| 536 | }} // end namespace |
---|
| 537 | |
---|
| 538 | // Local Variables: |
---|
| 539 | // tab-width: 4 |
---|
| 540 | // c-basic-offset: 4 |
---|
| 541 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 542 | // indent-tabs-mode: nil |
---|
| 543 | // End: |
---|
| 544 | |
---|
| 545 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 546 | |
---|