io.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef IO_H
  2. #define IO_H
  3. #include <stdint.h>
  4. #include <stdarg.h>
  5. #include <stdbool.h>
  6. #include "iodev.h"
  7. static inline void pause(void)
  8. {
  9. /* Placeholder for anything that might want to be done while waiting */
  10. }
  11. static inline void set_led(uint8_t leds)
  12. {
  13. LED = leds;
  14. }
  15. extern const uint32_t time_zero;
  16. static inline uint32_t rdtime(void)
  17. {
  18. uint32_t t;
  19. asm volatile("rdtime %0" : "=r" (t));
  20. return t;
  21. }
  22. static inline uint64_t rdtimeq(void)
  23. {
  24. uint32_t l, h1, h0;
  25. asm volatile("rdtimeh %0; rdtime %1; %rdtimeh %2"
  26. : "=r" (h1), "=r" (l), "=r" (h0));
  27. return ((int32_t)l < 0) ? h1 : h0;
  28. }
  29. static inline void udelay(uint32_t us)
  30. {
  31. uint32_t cycles = us * (CPU_HZ / 1000000);
  32. uint32_t start = rdtime();
  33. while (rdtime() - start < cycles)
  34. pause();
  35. }
  36. static inline void sd_set_mode(uint8_t divisor, bool cs)
  37. {
  38. SD_CTL_SPEED = (divisor - 1) | (cs << 7);
  39. }
  40. /* Read/write SD card and start transaction */
  41. enum sd_data_flags {
  42. SD_B0 = 0x00, /* Byte offset 0 */
  43. SD_B1 = 0x01, /* Byte offset 1 */
  44. SD_B2 = 0x02, /* Byte offset 2 */
  45. SD_B3 = 0x03, /* Byte offset 3 */
  46. SD_GO8 = 0x04, /* Start 8-bit transaction */
  47. SD_GO16 = 0x08, /* Start 16-bit transaction */
  48. SD_GO32 = 0x0c, /* Start 32-bit transaction */
  49. SD_BE = 0x10, /* Bigendian data */
  50. SD_DATA = 0x20, /* Data register access (assumed) */
  51. SD_CLEARCRC = 0x40 /* Clear CRC registers */
  52. };
  53. static inline void sd_writeb(uint8_t d, enum sd_data_flags flags)
  54. {
  55. flags ^= (flags & SD_BE) ? 3 : 0;
  56. *(volatile uint8_t *)IODEVA(SD_DEV,0,flags | SD_DATA) = d;
  57. }
  58. static inline void sd_writeh(uint16_t d, enum sd_data_flags flags)
  59. {
  60. flags ^= (flags & SD_BE) ? 2 : 0;
  61. *(volatile uint16_t *)IODEVA(SD_DEV,0,flags | SD_DATA) = d;
  62. }
  63. static inline void sd_writel(uint32_t d, enum sd_data_flags flags)
  64. {
  65. *(volatile uint32_t *)IODEVA(SD_DEV,0,flags | SD_DATA) = d;
  66. }
  67. static inline uint8_t sd_readb(enum sd_data_flags flags)
  68. {
  69. flags ^= (flags & SD_BE) ? 0 : 3;
  70. return *(volatile uint8_t *)IODEVA(SD_DEV,0,flags | SD_DATA);
  71. }
  72. static inline uint16_t sd_readh(enum sd_data_flags flags)
  73. {
  74. flags ^= (flags & SD_BE) ? 0 : 2;
  75. return *(volatile uint16_t *)IODEVA(SD_DEV,0,flags | SD_DATA);
  76. }
  77. static inline uint32_t sd_readl(enum sd_data_flags flags)
  78. {
  79. return *(volatile uint32_t *)IODEVA(SD_DEV,0,flags | SD_DATA);
  80. }
  81. static inline uint8_t sd_crc7_rd(void)
  82. {
  83. return SD_CRC7_RD;
  84. }
  85. static inline uint8_t sd_crc7_wr(void)
  86. {
  87. return SD_CRC7_WR;
  88. }
  89. static inline uint16_t sd_crc16_rd(void)
  90. {
  91. return SD_CRC16_RD;
  92. }
  93. static inline uint8_t sd_crc16_wr(void)
  94. {
  95. return SD_CRC16_WR;
  96. }
  97. #endif /* IO_H */