mock_sdio.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <assert.h>
  2. #include "src/cyw43.h"
  3. #include "src/cyw43_internal.h"
  4. #define BACKPLANE_BASE_ADDRESS (0x00000000)
  5. #define BACKPLANE_RAM_SIZE (512 * 1024)
  6. #define CHIPCOMMON_BASE_ADDRESS (0x18000000)
  7. // BUS_FUNCTION addresses.
  8. #define SDIOD_CCCR_IOEN (0x02)
  9. #define SDIOD_CCCR_IORDY (0x03)
  10. #define SDIOD_CCCR_BLKSIZE_0 (0x10)
  11. #define SDIOD_CCCR_SPEED_CONTROL (0x13)
  12. // BACKPLANE_FUNCTION addresses.
  13. #define SDIO_BACKPLANE_ADDRESS_LOW (0x1000a)
  14. #define SDIO_BACKPLANE_ADDRESS_MID (0x1000b)
  15. #define SDIO_BACKPLANE_ADDRESS_HIGH (0x1000c)
  16. #define SDIO_CHIP_CLOCK_CSR (0x1000e)
  17. #define SDIO_FUNC_ENABLE_1 (0x02)
  18. #define SDIO_FUNC_READY_1 (0x02)
  19. #define SBSDIO_ALP_AVAIL (0x40)
  20. #define SBSDIO_HT_AVAIL (0x80)
  21. static uint8_t sdiod_cccr_blksize_0;
  22. static uint32_t sdio_backplane_cur_addr = 0;
  23. static uint8_t backplane_ram[BACKPLANE_RAM_SIZE];
  24. void cyw43_sdio_init(void) {
  25. printf("cyw43_sdio_init()\n");
  26. }
  27. void cyw43_sdio_reinit(void) {
  28. printf("cyw43_sdio_reinit()\n");
  29. }
  30. void cyw43_sdio_deinit(void) {
  31. printf("cyw43_sdio_deinit()\n");
  32. }
  33. void cyw43_sdio_set_irq(bool enable) {
  34. printf("cyw43_sdio_set_irq(%u)\n", enable);
  35. }
  36. void cyw43_sdio_enable_high_speed_4bit(void) {
  37. printf("cyw43_sdio_enable_high_speed_4bit()\n");
  38. }
  39. int cyw43_sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp) {
  40. printf("cyw43_sdio_transfer(cmd=%u, arg=%08x)\n", cmd, arg);
  41. if (resp != NULL) {
  42. *resp = 0;
  43. }
  44. if (cmd == 52) {
  45. uint32_t write = (arg >> 31) & 1;
  46. uint32_t fn = (arg >> 28) & 0x7;
  47. uint32_t addr = (arg >> 9) & 0x1ffff;
  48. uint32_t val = arg & 0xff;
  49. if (fn == BUS_FUNCTION) {
  50. if (addr == SDIOD_CCCR_IOEN) {
  51. if (write == 0) {
  52. *resp = SDIO_FUNC_ENABLE_1;
  53. }
  54. } else if (addr == SDIOD_CCCR_IORDY) {
  55. if (write == 0) {
  56. *resp = SDIO_FUNC_READY_1;
  57. }
  58. } else if (addr == SDIOD_CCCR_BLKSIZE_0) {
  59. if (write == 0) {
  60. *resp = sdiod_cccr_blksize_0;
  61. } else {
  62. sdiod_cccr_blksize_0 = val;
  63. }
  64. } else if (addr == SDIOD_CCCR_SPEED_CONTROL) {
  65. if (write == 0) {
  66. *resp = 1; // bus supports high-speed mode
  67. }
  68. }
  69. } else if (fn == BACKPLANE_FUNCTION) {
  70. if (addr == SDIO_BACKPLANE_ADDRESS_LOW) {
  71. sdio_backplane_cur_addr = (sdio_backplane_cur_addr & 0xffff00ff) | (val << 8);
  72. } else if (addr == SDIO_BACKPLANE_ADDRESS_MID) {
  73. sdio_backplane_cur_addr = (sdio_backplane_cur_addr & 0xff00ffff) | (val << 16);
  74. } else if (addr == SDIO_BACKPLANE_ADDRESS_HIGH) {
  75. sdio_backplane_cur_addr = (sdio_backplane_cur_addr & 0x00ffffff) | (val << 24);
  76. } else if (addr == SDIO_CHIP_CLOCK_CSR) {
  77. if (write == 0) {
  78. *resp = SBSDIO_ALP_AVAIL | SBSDIO_HT_AVAIL;
  79. }
  80. }
  81. }
  82. }
  83. return 0;
  84. }
  85. int cyw43_sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t len, uint8_t *buf) {
  86. printf("cyw43_sdio_transfer_cmd53(write=%d, block_size=%u, arg=%08x, len=%u)\n", write, block_size, arg, len);
  87. // Decode parameters.
  88. uint32_t write_decoded = (arg >> 31) & 1;
  89. uint32_t fn = (arg >> 28) & 0x7;
  90. uint32_t block_mode = (arg >> 27) & 1;
  91. uint32_t addr = (arg >> 9) & 0x1ffff;
  92. uint32_t sz = arg & 0x1ff;
  93. // Validate parameters.
  94. assert(write == write_decoded);
  95. if (block_mode == 0) {
  96. assert(block_size == sz);
  97. assert(sz == len);
  98. } else {
  99. assert(block_size == 64);
  100. assert(sz * 64 == len);
  101. }
  102. // Ignore SBSDIO_SB_ACCESS_2_4B_FLAG.
  103. addr &= 0x7fff;
  104. if (fn == BACKPLANE_FUNCTION) {
  105. addr += sdio_backplane_cur_addr;
  106. if (addr < CHIPCOMMON_BASE_ADDRESS) {
  107. // Read/write backplane memory.
  108. assert(addr >= BACKPLANE_BASE_ADDRESS);
  109. assert(addr + len <= BACKPLANE_BASE_ADDRESS + BACKPLANE_RAM_SIZE);
  110. if (write) {
  111. memcpy(&backplane_ram[addr], buf, len);
  112. } else {
  113. memcpy(buf, &backplane_ram[addr], len);
  114. }
  115. }
  116. }
  117. return 0;
  118. }