scsi_accel_asm.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "scsi_accel_asm.h"
  2. #include "BlueSCSI_platform.h"
  3. // Optimized ASM blocks for the SCSI communication subroutine
  4. // Take 8 bits from d and format them for writing
  5. // d is name of data operand, b is bit offset, x is unique label
  6. #define ASM_LOAD_DATA(d, b, x) \
  7. " load_data1_" x "_%=: \n" \
  8. " ubfx %[tmp1], %[" d "], #" b ", #8 \n" \
  9. " ldr %[tmp1], [%[byte_lookup], %[tmp1], lsl #2] \n"
  10. // Write data to SCSI port and set REQ high
  11. #define ASM_SEND_DATA(x) \
  12. " send_data" x "_%=: \n" \
  13. " str %[tmp1], [%[out_port_bop]] \n"
  14. // Read data from SCSI port, set REQ high, store data to d at bit offset b
  15. #define ASM_RECV_DATA(d, b, x) \
  16. " recv_data" x "_%=: \n" \
  17. " ldr %[tmp1], [%[in_port_istat]] \n" \
  18. " mov %[tmp2], %[req_high_bop] \n" \
  19. " str %[tmp2], [%[out_port_bop]] \n" \
  20. " ubfx %[tmp1], %[tmp1], %[data_in_shift], #8 \n" \
  21. " bfi %[" d "], %[tmp1], #" b ", #8 \n"
  22. // Wait for ACK to be high, set REQ low, wait ACK low
  23. #define ASM_HANDSHAKE(x) \
  24. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  25. " str %[tmp2], [%[req_pin_bb]] \n" \
  26. " cbnz %[tmp2], req_is_low_now" x "_%= \n" \
  27. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  28. " str %[tmp2], [%[req_pin_bb]] \n" \
  29. " cbnz %[tmp2], req_is_low_now" x "_%= \n" \
  30. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  31. " str %[tmp2], [%[req_pin_bb]] \n" \
  32. " cbnz %[tmp2], req_is_low_now" x "_%= \n" \
  33. " wait_ack_inactive" x "_%=: \n" \
  34. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  35. " str %[tmp2], [%[req_pin_bb]] \n" \
  36. " cbnz %[tmp2], req_is_low_now" x "_%= \n" \
  37. " ldr %[tmp2], [%[reset_flag]] \n" \
  38. " cbnz %[tmp2], req_is_low_now" x "_%= \n" \
  39. " b.n wait_ack_inactive" x "_%= \n" \
  40. " req_is_low_now" x "_%=: \n" \
  41. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  42. " cbz %[tmp2], over_ack_active" x "_%= \n" \
  43. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  44. " cbz %[tmp2], over_ack_active" x "_%= \n" \
  45. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  46. " cbz %[tmp2], over_ack_active" x "_%= \n" \
  47. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  48. " cbz %[tmp2], over_ack_active" x "_%= \n" \
  49. " wait_ack_active" x "_%=: \n" \
  50. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  51. " cbz %[tmp2], over_ack_active" x "_%= \n" \
  52. " ldr %[tmp2], [%[reset_flag]] \n" \
  53. " cbnz %[tmp2], over_ack_active" x "_%= \n" \
  54. " b.n wait_ack_active" x "_%= \n" \
  55. " over_ack_active" x "_%=: \n" \
  56. // Send bytes to SCSI bus using the asynchronous handshake mechanism
  57. // Takes 4 bytes at a time for sending from buf.
  58. void scsi_accel_asm_send(const uint32_t *buf, uint32_t num_words, volatile int *resetFlag)
  59. {
  60. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  61. const uint32_t *byte_lookup = g_scsi_out_byte_to_bop;
  62. uint32_t ack_pin_bb = PERIPH_BB_BASE + (((uint32_t)&GPIO_ISTAT(SCSI_ACK_PORT)) - APB1_BUS_BASE) * 32 + SCSI_IN_ACK_IDX * 4;
  63. uint32_t req_pin_bb = PERIPH_BB_BASE + (((uint32_t)out_port_bop) - APB1_BUS_BASE) * 32 + (SCSI_OUT_REQ_IDX + 16) * 4;
  64. register uint32_t tmp1 = 0;
  65. register uint32_t tmp2 = 0;
  66. register uint32_t data = 0;
  67. asm volatile (
  68. " ldr %[data], [%[buf]], #4 \n" \
  69. ASM_LOAD_DATA("data", "0", "first")
  70. "inner_loop_%=: \n" \
  71. ASM_SEND_DATA("0")
  72. ASM_LOAD_DATA("data", "8", "8")
  73. ASM_HANDSHAKE("0")
  74. ASM_SEND_DATA("8")
  75. ASM_LOAD_DATA("data", "16", "16")
  76. ASM_HANDSHAKE("8")
  77. ASM_SEND_DATA("16")
  78. ASM_LOAD_DATA("data", "24", "24")
  79. ASM_HANDSHAKE("16")
  80. ASM_SEND_DATA("24")
  81. " ldr %[data], [%[buf]], #4 \n" \
  82. ASM_LOAD_DATA("data", "0", "0")
  83. ASM_HANDSHAKE("24")
  84. " subs %[num_words], %[num_words], #1 \n" \
  85. " bne inner_loop_%= \n"
  86. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  87. [buf] "+r" (buf), [num_words] "+r" (num_words)
  88. : /* Input */ [ack_pin_bb] "r" (ack_pin_bb),
  89. [req_pin_bb] "r" (req_pin_bb),
  90. [out_port_bop] "r"(out_port_bop),
  91. [byte_lookup] "r" (byte_lookup),
  92. [reset_flag] "r" (resetFlag)
  93. : /* Clobber */ );
  94. SCSI_RELEASE_DATA_REQ();
  95. }
  96. // Read bytes from SCSI bus using asynchronous handshake mechanism
  97. // Takes 4 bytes at a time.
  98. void scsi_accel_asm_recv(uint32_t *buf, uint32_t num_words, volatile int *resetFlag)
  99. {
  100. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  101. volatile uint32_t *in_port_istat = (volatile uint32_t*)&GPIO_ISTAT(SCSI_IN_PORT);
  102. uint32_t ack_pin_bb = PERIPH_BB_BASE + (((uint32_t)&GPIO_ISTAT(SCSI_ACK_PORT)) - APB1_BUS_BASE) * 32 + SCSI_IN_ACK_IDX * 4;
  103. uint32_t req_pin_bb = PERIPH_BB_BASE + (((uint32_t)out_port_bop) - APB1_BUS_BASE) * 32 + (SCSI_OUT_REQ_IDX + 16) * 4;
  104. register uint32_t tmp1 = 0;
  105. register uint32_t tmp2 = 0;
  106. register uint32_t data = 0;
  107. asm volatile (
  108. "inner_loop_%=: \n" \
  109. ASM_HANDSHAKE("0")
  110. ASM_RECV_DATA("data", "0", "0")
  111. ASM_HANDSHAKE("8")
  112. ASM_RECV_DATA("data", "8", "8")
  113. ASM_HANDSHAKE("16")
  114. ASM_RECV_DATA("data", "16", "16")
  115. ASM_HANDSHAKE("24")
  116. ASM_RECV_DATA("data", "24", "24")
  117. " mvn %[data], %[data] \n" \
  118. " str %[data], [%[buf]], #4 \n" \
  119. " subs %[num_words], %[num_words], #1 \n" \
  120. " bne inner_loop_%= \n"
  121. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  122. [buf] "+r" (buf), [num_words] "+r" (num_words)
  123. : /* Input */ [ack_pin_bb] "r" (ack_pin_bb),
  124. [req_pin_bb] "r" (req_pin_bb),
  125. [out_port_bop] "r"(out_port_bop),
  126. [in_port_istat] "r" (in_port_istat),
  127. [reset_flag] "r" (resetFlag),
  128. [data_in_shift] "I" (SCSI_IN_SHIFT),
  129. [req_high_bop] "I" (SCSI_OUT_REQ)
  130. : /* Clobber */ );
  131. SCSI_RELEASE_DATA_REQ();
  132. }