diskio.c 4.4 KB

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