diskio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* sd2snes - SD card based universal cartridge for the SNES
  2. Copyright (C) 2009-2010 Maximilian Rehkopf <otakon@gmx.net>
  3. This file was adapted from sd2iec, written by Ingo Korb.
  4. Original copyright header follows:
  5. */
  6. /* sd2iec - SD/MMC to Commodore serial bus interface/controller
  7. Copyright (C) 2007-2009 Ingo Korb <ingo@akana.de>
  8. Inspiration and low-level SD/MMC access based on code from MMC2IEC
  9. by Lars Pontoppidan et al., see sdcard.c|h and config.h.
  10. FAT filesystem access based on code from ChaN and Jim Brain, see ff.c|h.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; version 2 of the License only.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. diskio.c: Generic disk access routines and supporting stuff
  22. */
  23. #include "config.h"
  24. #include "diskio.h"
  25. #include "sdcard.h"
  26. volatile enum diskstates disk_state;
  27. #ifdef NEED_DISKMUX
  28. uint32_t drive_config;
  29. /* This function calculates the default drive configuration. */
  30. /* Its result is static after compilation, but doing this in */
  31. /* C in less messy than doing it with the preprocessor. */
  32. uint32_t get_default_driveconfig(void) {
  33. uint32_t result = 0xffffffffL;
  34. /* Order matters: Whatever is checked first will be last in the config */
  35. #ifdef HAVE_DF
  36. result = (result << 4) + (DISK_TYPE_DF << DRIVE_BITS) + 0;
  37. #endif
  38. #ifdef CONFIG_TWINSD
  39. result = (result << 4) + (DISK_TYPE_SD << DRIVE_BITS) + 1;
  40. #endif
  41. #ifdef HAVE_SD
  42. result = (result << 4) + (DISK_TYPE_SD << DRIVE_BITS) + 0;
  43. #endif
  44. #ifdef HAVE_ATA
  45. result = (result << 4) + (DISK_TYPE_ATA << DRIVE_BITS) + 0;
  46. #endif
  47. return result;
  48. }
  49. void disk_init(void) {
  50. #ifdef HAVE_SD
  51. sd_init();
  52. #endif
  53. #ifdef HAVE_ATA
  54. ata_init();
  55. #endif
  56. #ifdef HAVE_DF
  57. df_init();
  58. #endif
  59. }
  60. DSTATUS disk_status(BYTE drv) {
  61. switch(drv >> DRIVE_BITS) {
  62. #ifdef HAVE_DF
  63. case DISK_TYPE_DF:
  64. return df_status(drv & DRIVE_MASK);
  65. #endif
  66. #ifdef HAVE_ATA
  67. case DISK_TYPE_ATA:
  68. return ata_status(drv & DRIVE_MASK);
  69. case DISK_TYPE_ATA2:
  70. return ata_status((drv & DRIVE_MASK) + 2);
  71. #endif
  72. #ifdef HAVE_SD
  73. case DISK_TYPE_SD:
  74. return sd_status(drv & DRIVE_MASK);
  75. #endif
  76. default:
  77. return STA_NOINIT|STA_NODISK;
  78. }
  79. }
  80. DSTATUS disk_initialize(BYTE drv) {
  81. switch(drv >> DRIVE_BITS) {
  82. #ifdef HAVE_DF
  83. case DISK_TYPE_DF:
  84. return df_initialize(drv & DRIVE_MASK);
  85. #endif
  86. #ifdef HAVE_ATA
  87. case DISK_TYPE_ATA:
  88. return ata_initialize(drv & DRIVE_MASK);
  89. case DISK_TYPE_ATA2:
  90. return ata_initialize((drv & DRIVE_MASK) + 2);
  91. #endif
  92. #ifdef HAVE_SD
  93. case DISK_TYPE_SD:
  94. return sd_initialize(drv & DRIVE_MASK);
  95. #endif
  96. default:
  97. return STA_NOINIT|STA_NODISK;
  98. }
  99. }
  100. DRESULT disk_read(BYTE drv, BYTE *buffer, DWORD sector, BYTE count) {
  101. switch(drv >> DRIVE_BITS) {
  102. #ifdef HAVE_DF
  103. case DISK_TYPE_DF:
  104. return df_read(drv & DRIVE_MASK,buffer,sector,count);
  105. #endif
  106. #ifdef HAVE_ATA
  107. case DISK_TYPE_ATA:
  108. return ata_read(drv & DRIVE_MASK,buffer,sector,count);
  109. case DISK_TYPE_ATA2:
  110. return ata_read((drv & DRIVE_MASK) + 2,buffer,sector,count);
  111. #endif
  112. #ifdef HAVE_SD
  113. case DISK_TYPE_SD:
  114. return sd_read(drv & DRIVE_MASK,buffer,sector,count);
  115. #endif
  116. default:
  117. return RES_ERROR;
  118. }
  119. }
  120. DRESULT disk_write(BYTE drv, const BYTE *buffer, DWORD sector, BYTE count) {
  121. switch(drv >> DRIVE_BITS) {
  122. #ifdef HAVE_DF
  123. case DISK_TYPE_DF:
  124. return df_write(drv & DRIVE_MASK,buffer,sector,count);
  125. #endif
  126. #ifdef HAVE_ATA
  127. case DISK_TYPE_ATA:
  128. return ata_write(drv & DRIVE_MASK,buffer,sector,count);
  129. case DISK_TYPE_ATA2:
  130. return ata_write((drv & DRIVE_MASK) + 2,buffer,sector,count);
  131. #endif
  132. #ifdef HAVE_SD
  133. case DISK_TYPE_SD:
  134. return sd_write(drv & DRIVE_MASK,buffer,sector,count);
  135. #endif
  136. default:
  137. return RES_ERROR;
  138. }
  139. }
  140. DRESULT disk_getinfo(BYTE drv, BYTE page, void *buffer) {
  141. switch(drv >> DRIVE_BITS) {
  142. #ifdef HAVE_DF
  143. case DISK_TYPE_DF:
  144. return df_getinfo(drv & DRIVE_MASK,page,buffer);
  145. #endif
  146. #ifdef HAVE_ATA
  147. case DISK_TYPE_ATA:
  148. return ata_getinfo(drv & DRIVE_MASK,page,buffer);
  149. case DISK_TYPE_ATA2:
  150. return ata_getinfo((drv & DRIVE_MASK) + 2,page,buffer);
  151. #endif
  152. #ifdef HAVE_SD
  153. case DISK_TYPE_SD:
  154. return sd_getinfo(drv & DRIVE_MASK,page,buffer);
  155. #endif
  156. default:
  157. return RES_ERROR;
  158. }
  159. }
  160. #endif