2
0

sdcard.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef SDCARD_H
  2. #define SDCARD_H
  3. #include "io.h"
  4. #include "irq.h"
  5. static inline void sd_set_mode(uint8_t divisor, bool cs)
  6. {
  7. waitfor(SDCARD_IRQ);
  8. SDCARD_CTL_SPEED = (divisor - 1) | (cs << 7);
  9. }
  10. /* Read/write SD card and start transaction */
  11. enum sd_data_flags {
  12. SD_B0 = 0x00, /* Byte offset 0 */
  13. SD_B1 = 0x01, /* Byte offset 1 */
  14. SD_B2 = 0x02, /* Byte offset 2 */
  15. SD_B3 = 0x03, /* Byte offset 3 */
  16. SD_GO8 = 0x04, /* Start 8-bit transaction */
  17. SD_GO16 = 0x08, /* Start 16-bit transaction */
  18. SD_GO32 = 0x0c, /* Start 32-bit transaction */
  19. SD_BE = 0x10, /* Bigendian data */
  20. SD_DATA = 0x20, /* Data register access (assumed) */
  21. SD_CLEARCRC = 0x40 /* Clear CRC registers */
  22. };
  23. static inline void sd_writeb(uint8_t d, enum sd_data_flags flags)
  24. {
  25. waitfor(SDCARD_IRQ);
  26. flags ^= (flags & SD_BE) ? 3 : 0;
  27. *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  28. }
  29. static inline void sd_writeh(uint16_t d, enum sd_data_flags flags)
  30. {
  31. waitfor(SDCARD_IRQ);
  32. flags ^= (flags & SD_BE) ? 2 : 0;
  33. *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  34. }
  35. static inline void sd_writel(uint32_t d, enum sd_data_flags flags)
  36. {
  37. waitfor(SDCARD_IRQ);
  38. *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  39. }
  40. static inline uint8_t sd_readb(enum sd_data_flags flags)
  41. {
  42. waitfor(SDCARD_IRQ);
  43. flags ^= (flags & SD_BE) ? 0 : 3;
  44. return *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  45. }
  46. static inline uint16_t sd_readh(enum sd_data_flags flags)
  47. {
  48. waitfor(SDCARD_IRQ);
  49. flags ^= (flags & SD_BE) ? 0 : 2;
  50. return *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  51. }
  52. static inline uint32_t sd_readl(enum sd_data_flags flags)
  53. {
  54. waitfor(SDCARD_IRQ);
  55. return *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  56. }
  57. static inline uint8_t sd_crc7_rd(void)
  58. {
  59. waitfor(SDCARD_IRQ);
  60. return SDCARD_CRC7_RD;
  61. }
  62. static inline uint8_t sd_crc7_wr(void)
  63. {
  64. waitfor(SDCARD_IRQ);
  65. return SDCARD_CRC7_WR;
  66. }
  67. static inline uint16_t sd_crc16_rd(void)
  68. {
  69. waitfor(SDCARD_IRQ);
  70. return SDCARD_CRC16_RD;
  71. }
  72. static inline uint8_t sd_crc16_wr(void)
  73. {
  74. waitfor(SDCARD_IRQ);
  75. return SDCARD_CRC16_WR;
  76. }
  77. #endif /* SDCARD_H */