io.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef IO_H
  2. #define IO_H
  3. #include <stdint.h>
  4. #include <stdarg.h>
  5. #include <stdbool.h>
  6. #include "ioregs.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. SYS_LED = leds;
  14. }
  15. static inline __attribute__((noreturn)) void reset(void)
  16. {
  17. while (1)
  18. SYS_RESET = 1;
  19. }
  20. extern const uint32_t time_zero;
  21. static inline uint32_t rdtime(void)
  22. {
  23. uint32_t t;
  24. asm volatile("rdtime %0" : "=r" (t));
  25. return t;
  26. }
  27. static inline uint64_t rdtimeq(void)
  28. {
  29. uint32_t l, h1, h0;
  30. asm volatile("rdtimeh %0; rdtime %1; %rdtimeh %2"
  31. : "=r" (h1), "=r" (l), "=r" (h0));
  32. return ((uint64_t)(((int32_t)l < 0) ? h1 : h0) << 32) + l;
  33. }
  34. static inline void udelay(uint32_t us)
  35. {
  36. uint32_t cycles = us * (CPU_HZ / 1000000);
  37. uint32_t start = rdtime();
  38. while (rdtime() - start < cycles)
  39. pause();
  40. }
  41. static inline void sd_set_mode(uint8_t divisor, bool cs)
  42. {
  43. SDCARD_CTL_SPEED = (divisor - 1) | (cs << 7);
  44. }
  45. /* Read/write SD card and start transaction */
  46. enum sd_data_flags {
  47. SD_B0 = 0x00, /* Byte offset 0 */
  48. SD_B1 = 0x01, /* Byte offset 1 */
  49. SD_B2 = 0x02, /* Byte offset 2 */
  50. SD_B3 = 0x03, /* Byte offset 3 */
  51. SD_GO8 = 0x04, /* Start 8-bit transaction */
  52. SD_GO16 = 0x08, /* Start 16-bit transaction */
  53. SD_GO32 = 0x0c, /* Start 32-bit transaction */
  54. SD_BE = 0x10, /* Bigendian data */
  55. SD_DATA = 0x20, /* Data register access (assumed) */
  56. SD_CLEARCRC = 0x40 /* Clear CRC registers */
  57. };
  58. static inline void sd_writeb(uint8_t d, enum sd_data_flags flags)
  59. {
  60. flags ^= (flags & SD_BE) ? 3 : 0;
  61. *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  62. }
  63. static inline void sd_writeh(uint16_t d, enum sd_data_flags flags)
  64. {
  65. flags ^= (flags & SD_BE) ? 2 : 0;
  66. *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  67. }
  68. static inline void sd_writel(uint32_t d, enum sd_data_flags flags)
  69. {
  70. *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  71. }
  72. static inline uint8_t sd_readb(enum sd_data_flags flags)
  73. {
  74. flags ^= (flags & SD_BE) ? 0 : 3;
  75. return *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  76. }
  77. static inline uint16_t sd_readh(enum sd_data_flags flags)
  78. {
  79. flags ^= (flags & SD_BE) ? 0 : 2;
  80. return *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  81. }
  82. static inline uint32_t sd_readl(enum sd_data_flags flags)
  83. {
  84. return *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  85. }
  86. static inline uint8_t sd_crc7_rd(void)
  87. {
  88. return SDCARD_CRC7_RD;
  89. }
  90. static inline uint8_t sd_crc7_wr(void)
  91. {
  92. return SDCARD_CRC7_WR;
  93. }
  94. static inline uint16_t sd_crc16_rd(void)
  95. {
  96. return SDCARD_CRC16_RD;
  97. }
  98. static inline uint8_t sd_crc16_wr(void)
  99. {
  100. return SDCARD_CRC16_WR;
  101. }
  102. static inline void i2c_set_speed(unsigned int khz)
  103. {
  104. I2C_DIVISOR = ((CPU_HZ/4000)-1)/khz;
  105. }
  106. #endif /* IO_H */