scsi_accel_greenpak.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. #include "scsi_accel_greenpak.h"
  22. #include "ZuluSCSI_platform.h"
  23. #include <ZuluSCSI_log.h>
  24. #include <assert.h>
  25. #ifndef GREENPAK_PLD_IO1
  26. void scsi_accel_greenpak_send(const uint32_t *buf, uint32_t num_words, volatile int *resetFlag)
  27. {
  28. assert(false);
  29. }
  30. void scsi_accel_greenpak_recv(uint32_t *buf, uint32_t num_words, volatile int *resetFlag)
  31. {
  32. assert(false);
  33. }
  34. #else
  35. /*********************************************************/
  36. /* Optimized writes to SCSI bus in GREENPAK_PIO mode */
  37. /*********************************************************/
  38. extern const uint32_t g_scsi_out_byte_to_bop_pld1hi[256];
  39. extern const uint32_t g_scsi_out_byte_to_bop_pld1lo[256];
  40. // Optimized ASM blocks for the SCSI communication subroutine
  41. // Take 8 bits from d and format them for writing
  42. // d is name of data operand, b is bit offset, x is unique label
  43. #define ASM_LOAD_DATA_PLD1HI(d, b, x) \
  44. " load_data1_" x "_%=: \n" \
  45. " ubfx %[tmp1], %[" d "], #" b ", #8 \n" \
  46. " ldr %[tmp1], [%[byte_lookup_pld1hi], %[tmp1], lsl #2] \n"
  47. #define ASM_LOAD_DATA_PLD1LO(d, b, x) \
  48. " load_data1_" x "_%=: \n" \
  49. " ubfx %[tmp1], %[" d "], #" b ", #8 \n" \
  50. " ldr %[tmp1], [%[byte_lookup_pld1lo], %[tmp1], lsl #2] \n"
  51. // Write data to SCSI port
  52. #define ASM_SEND_DATA(x) \
  53. " send_data" x "_%=: \n" \
  54. " str %[tmp1], [%[out_port_bop]] \n"
  55. // Wait for ACK to be low or REQ to be high.
  56. // The external logic will set REQ low when PLD_IO1 is toggled and
  57. // back high as soon as ACK goes low. New data can be written to
  58. // GPIO as soon as ACK goes low. If an interrupt happens in the
  59. // middle, we may miss the ACK pulse and should check REQ.
  60. #define ASM_WAIT_DONE(x) \
  61. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  62. " cbz %[tmp2], wait_done_" x "_%= \n" \
  63. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  64. " cbz %[tmp2], wait_done_" x "_%= \n" \
  65. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  66. " cbz %[tmp2], wait_done_" x "_%= \n" \
  67. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  68. " cbz %[tmp2], wait_done_" x "_%= \n" \
  69. " wait_req_inactive_" x "_%=: \n" \
  70. " ldr %[tmp2], [%[req_pin_bb]] \n" \
  71. " cbnz %[tmp2], wait_done_" x "_%= \n" \
  72. " ldr %[tmp2], [%[reset_flag]] \n" \
  73. " cbnz %[tmp2], wait_done_" x "_%= \n" \
  74. " b.n wait_req_inactive_" x "_%= \n" \
  75. " wait_done_" x "_%=: \n"
  76. // Send bytes to SCSI bus using the asynchronous handshake mechanism
  77. // Takes 4 bytes at a time for sending from buf.
  78. void scsi_accel_greenpak_send(const uint32_t *buf, uint32_t num_words, volatile int *resetFlag)
  79. {
  80. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  81. uint32_t ack_pin_bb = PERIPH_BB_BASE + (((uint32_t)&GPIO_ISTAT(SCSI_ACK_PORT)) - APB1_BUS_BASE) * 32 + SCSI_IN_ACK_IDX * 4;
  82. uint32_t req_pin_bb = PERIPH_BB_BASE + (((uint32_t)&GPIO_ISTAT(SCSI_OUT_PORT)) - APB1_BUS_BASE) * 32 + SCSI_OUT_REQ_IDX * 4;
  83. register uint32_t tmp1 = 0;
  84. register uint32_t tmp2 = 0;
  85. register uint32_t data = 0;
  86. // Set REQ pin as input and PLD_IO2 high to enable logic
  87. GPIO_BC(SCSI_OUT_PORT) = GREENPAK_PLD_IO1;
  88. gpio_init(SCSI_OUT_PORT, GPIO_MODE_IPU, 0, SCSI_OUT_REQ);
  89. GPIO_BOP(SCSI_OUT_PORT) = GREENPAK_PLD_IO2;
  90. asm volatile (
  91. " ldr %[data], [%[buf]], #4 \n" \
  92. ASM_LOAD_DATA_PLD1HI("data", "0", "first")
  93. "inner_loop_%=: \n" \
  94. ASM_SEND_DATA("0")
  95. ASM_LOAD_DATA_PLD1LO("data", "8", "8")
  96. ASM_WAIT_DONE("0")
  97. ASM_SEND_DATA("8")
  98. ASM_LOAD_DATA_PLD1HI("data", "16", "16")
  99. ASM_WAIT_DONE("8")
  100. ASM_SEND_DATA("16")
  101. ASM_LOAD_DATA_PLD1LO("data", "24", "24")
  102. ASM_WAIT_DONE("16")
  103. ASM_SEND_DATA("24")
  104. " ldr %[data], [%[buf]], #4 \n" \
  105. ASM_LOAD_DATA_PLD1HI("data", "0", "0")
  106. ASM_WAIT_DONE("24")
  107. " subs %[num_words], %[num_words], #1 \n" \
  108. " bne inner_loop_%= \n"
  109. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  110. [buf] "+r" (buf), [num_words] "+r" (num_words)
  111. : /* Input */ [ack_pin_bb] "r" (ack_pin_bb),
  112. [req_pin_bb] "r" (req_pin_bb),
  113. [out_port_bop] "r"(out_port_bop),
  114. [byte_lookup_pld1hi] "r" (g_scsi_out_byte_to_bop_pld1hi),
  115. [byte_lookup_pld1lo] "r" (g_scsi_out_byte_to_bop_pld1lo),
  116. [reset_flag] "r" (resetFlag)
  117. : /* Clobber */ );
  118. SCSI_RELEASE_DATA_REQ();
  119. // Disable external logic and set REQ pin as output
  120. GPIO_BC(SCSI_OUT_PORT) = GREENPAK_PLD_IO2;
  121. gpio_init(SCSI_OUT_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_REQ);
  122. }
  123. /**********************************************/
  124. /* Mapping from data bytes to GPIO BOP values */
  125. /**********************************************/
  126. #define PARITY(n) ((1 ^ (n) ^ ((n)>>1) ^ ((n)>>2) ^ ((n)>>3) ^ ((n)>>4) ^ ((n)>>5) ^ ((n)>>6) ^ ((n)>>7)) & 1)
  127. #define X(n) (\
  128. ((n & 0x01) ? (SCSI_OUT_DB0 << 16) : SCSI_OUT_DB0) | \
  129. ((n & 0x02) ? (SCSI_OUT_DB1 << 16) : SCSI_OUT_DB1) | \
  130. ((n & 0x04) ? (SCSI_OUT_DB2 << 16) : SCSI_OUT_DB2) | \
  131. ((n & 0x08) ? (SCSI_OUT_DB3 << 16) : SCSI_OUT_DB3) | \
  132. ((n & 0x10) ? (SCSI_OUT_DB4 << 16) : SCSI_OUT_DB4) | \
  133. ((n & 0x20) ? (SCSI_OUT_DB5 << 16) : SCSI_OUT_DB5) | \
  134. ((n & 0x40) ? (SCSI_OUT_DB6 << 16) : SCSI_OUT_DB6) | \
  135. ((n & 0x80) ? (SCSI_OUT_DB7 << 16) : SCSI_OUT_DB7) | \
  136. (PARITY(n) ? (SCSI_OUT_DBP << 16) : SCSI_OUT_DBP) | \
  137. (GREENPAK_PLD_IO1) \
  138. )
  139. const uint32_t g_scsi_out_byte_to_bop_pld1hi[256] =
  140. {
  141. X(0x00), X(0x01), X(0x02), X(0x03), X(0x04), X(0x05), X(0x06), X(0x07), X(0x08), X(0x09), X(0x0a), X(0x0b), X(0x0c), X(0x0d), X(0x0e), X(0x0f),
  142. X(0x10), X(0x11), X(0x12), X(0x13), X(0x14), X(0x15), X(0x16), X(0x17), X(0x18), X(0x19), X(0x1a), X(0x1b), X(0x1c), X(0x1d), X(0x1e), X(0x1f),
  143. X(0x20), X(0x21), X(0x22), X(0x23), X(0x24), X(0x25), X(0x26), X(0x27), X(0x28), X(0x29), X(0x2a), X(0x2b), X(0x2c), X(0x2d), X(0x2e), X(0x2f),
  144. X(0x30), X(0x31), X(0x32), X(0x33), X(0x34), X(0x35), X(0x36), X(0x37), X(0x38), X(0x39), X(0x3a), X(0x3b), X(0x3c), X(0x3d), X(0x3e), X(0x3f),
  145. X(0x40), X(0x41), X(0x42), X(0x43), X(0x44), X(0x45), X(0x46), X(0x47), X(0x48), X(0x49), X(0x4a), X(0x4b), X(0x4c), X(0x4d), X(0x4e), X(0x4f),
  146. X(0x50), X(0x51), X(0x52), X(0x53), X(0x54), X(0x55), X(0x56), X(0x57), X(0x58), X(0x59), X(0x5a), X(0x5b), X(0x5c), X(0x5d), X(0x5e), X(0x5f),
  147. X(0x60), X(0x61), X(0x62), X(0x63), X(0x64), X(0x65), X(0x66), X(0x67), X(0x68), X(0x69), X(0x6a), X(0x6b), X(0x6c), X(0x6d), X(0x6e), X(0x6f),
  148. X(0x70), X(0x71), X(0x72), X(0x73), X(0x74), X(0x75), X(0x76), X(0x77), X(0x78), X(0x79), X(0x7a), X(0x7b), X(0x7c), X(0x7d), X(0x7e), X(0x7f),
  149. X(0x80), X(0x81), X(0x82), X(0x83), X(0x84), X(0x85), X(0x86), X(0x87), X(0x88), X(0x89), X(0x8a), X(0x8b), X(0x8c), X(0x8d), X(0x8e), X(0x8f),
  150. X(0x90), X(0x91), X(0x92), X(0x93), X(0x94), X(0x95), X(0x96), X(0x97), X(0x98), X(0x99), X(0x9a), X(0x9b), X(0x9c), X(0x9d), X(0x9e), X(0x9f),
  151. X(0xa0), X(0xa1), X(0xa2), X(0xa3), X(0xa4), X(0xa5), X(0xa6), X(0xa7), X(0xa8), X(0xa9), X(0xaa), X(0xab), X(0xac), X(0xad), X(0xae), X(0xaf),
  152. X(0xb0), X(0xb1), X(0xb2), X(0xb3), X(0xb4), X(0xb5), X(0xb6), X(0xb7), X(0xb8), X(0xb9), X(0xba), X(0xbb), X(0xbc), X(0xbd), X(0xbe), X(0xbf),
  153. X(0xc0), X(0xc1), X(0xc2), X(0xc3), X(0xc4), X(0xc5), X(0xc6), X(0xc7), X(0xc8), X(0xc9), X(0xca), X(0xcb), X(0xcc), X(0xcd), X(0xce), X(0xcf),
  154. X(0xd0), X(0xd1), X(0xd2), X(0xd3), X(0xd4), X(0xd5), X(0xd6), X(0xd7), X(0xd8), X(0xd9), X(0xda), X(0xdb), X(0xdc), X(0xdd), X(0xde), X(0xdf),
  155. X(0xe0), X(0xe1), X(0xe2), X(0xe3), X(0xe4), X(0xe5), X(0xe6), X(0xe7), X(0xe8), X(0xe9), X(0xea), X(0xeb), X(0xec), X(0xed), X(0xee), X(0xef),
  156. X(0xf0), X(0xf1), X(0xf2), X(0xf3), X(0xf4), X(0xf5), X(0xf6), X(0xf7), X(0xf8), X(0xf9), X(0xfa), X(0xfb), X(0xfc), X(0xfd), X(0xfe), X(0xff)
  157. };
  158. #undef X
  159. #define X(n) (\
  160. ((n & 0x01) ? (SCSI_OUT_DB0 << 16) : SCSI_OUT_DB0) | \
  161. ((n & 0x02) ? (SCSI_OUT_DB1 << 16) : SCSI_OUT_DB1) | \
  162. ((n & 0x04) ? (SCSI_OUT_DB2 << 16) : SCSI_OUT_DB2) | \
  163. ((n & 0x08) ? (SCSI_OUT_DB3 << 16) : SCSI_OUT_DB3) | \
  164. ((n & 0x10) ? (SCSI_OUT_DB4 << 16) : SCSI_OUT_DB4) | \
  165. ((n & 0x20) ? (SCSI_OUT_DB5 << 16) : SCSI_OUT_DB5) | \
  166. ((n & 0x40) ? (SCSI_OUT_DB6 << 16) : SCSI_OUT_DB6) | \
  167. ((n & 0x80) ? (SCSI_OUT_DB7 << 16) : SCSI_OUT_DB7) | \
  168. (PARITY(n) ? (SCSI_OUT_DBP << 16) : SCSI_OUT_DBP) | \
  169. (GREENPAK_PLD_IO1 << 16) \
  170. )
  171. const uint32_t g_scsi_out_byte_to_bop_pld1lo[256] =
  172. {
  173. X(0x00), X(0x01), X(0x02), X(0x03), X(0x04), X(0x05), X(0x06), X(0x07), X(0x08), X(0x09), X(0x0a), X(0x0b), X(0x0c), X(0x0d), X(0x0e), X(0x0f),
  174. X(0x10), X(0x11), X(0x12), X(0x13), X(0x14), X(0x15), X(0x16), X(0x17), X(0x18), X(0x19), X(0x1a), X(0x1b), X(0x1c), X(0x1d), X(0x1e), X(0x1f),
  175. X(0x20), X(0x21), X(0x22), X(0x23), X(0x24), X(0x25), X(0x26), X(0x27), X(0x28), X(0x29), X(0x2a), X(0x2b), X(0x2c), X(0x2d), X(0x2e), X(0x2f),
  176. X(0x30), X(0x31), X(0x32), X(0x33), X(0x34), X(0x35), X(0x36), X(0x37), X(0x38), X(0x39), X(0x3a), X(0x3b), X(0x3c), X(0x3d), X(0x3e), X(0x3f),
  177. X(0x40), X(0x41), X(0x42), X(0x43), X(0x44), X(0x45), X(0x46), X(0x47), X(0x48), X(0x49), X(0x4a), X(0x4b), X(0x4c), X(0x4d), X(0x4e), X(0x4f),
  178. X(0x50), X(0x51), X(0x52), X(0x53), X(0x54), X(0x55), X(0x56), X(0x57), X(0x58), X(0x59), X(0x5a), X(0x5b), X(0x5c), X(0x5d), X(0x5e), X(0x5f),
  179. X(0x60), X(0x61), X(0x62), X(0x63), X(0x64), X(0x65), X(0x66), X(0x67), X(0x68), X(0x69), X(0x6a), X(0x6b), X(0x6c), X(0x6d), X(0x6e), X(0x6f),
  180. X(0x70), X(0x71), X(0x72), X(0x73), X(0x74), X(0x75), X(0x76), X(0x77), X(0x78), X(0x79), X(0x7a), X(0x7b), X(0x7c), X(0x7d), X(0x7e), X(0x7f),
  181. X(0x80), X(0x81), X(0x82), X(0x83), X(0x84), X(0x85), X(0x86), X(0x87), X(0x88), X(0x89), X(0x8a), X(0x8b), X(0x8c), X(0x8d), X(0x8e), X(0x8f),
  182. X(0x90), X(0x91), X(0x92), X(0x93), X(0x94), X(0x95), X(0x96), X(0x97), X(0x98), X(0x99), X(0x9a), X(0x9b), X(0x9c), X(0x9d), X(0x9e), X(0x9f),
  183. X(0xa0), X(0xa1), X(0xa2), X(0xa3), X(0xa4), X(0xa5), X(0xa6), X(0xa7), X(0xa8), X(0xa9), X(0xaa), X(0xab), X(0xac), X(0xad), X(0xae), X(0xaf),
  184. X(0xb0), X(0xb1), X(0xb2), X(0xb3), X(0xb4), X(0xb5), X(0xb6), X(0xb7), X(0xb8), X(0xb9), X(0xba), X(0xbb), X(0xbc), X(0xbd), X(0xbe), X(0xbf),
  185. X(0xc0), X(0xc1), X(0xc2), X(0xc3), X(0xc4), X(0xc5), X(0xc6), X(0xc7), X(0xc8), X(0xc9), X(0xca), X(0xcb), X(0xcc), X(0xcd), X(0xce), X(0xcf),
  186. X(0xd0), X(0xd1), X(0xd2), X(0xd3), X(0xd4), X(0xd5), X(0xd6), X(0xd7), X(0xd8), X(0xd9), X(0xda), X(0xdb), X(0xdc), X(0xdd), X(0xde), X(0xdf),
  187. X(0xe0), X(0xe1), X(0xe2), X(0xe3), X(0xe4), X(0xe5), X(0xe6), X(0xe7), X(0xe8), X(0xe9), X(0xea), X(0xeb), X(0xec), X(0xed), X(0xee), X(0xef),
  188. X(0xf0), X(0xf1), X(0xf2), X(0xf3), X(0xf4), X(0xf5), X(0xf6), X(0xf7), X(0xf8), X(0xf9), X(0xfa), X(0xfb), X(0xfc), X(0xfd), X(0xfe), X(0xff)
  189. };
  190. #undef X
  191. /*********************************************************/
  192. /* Optimized reads from SCSI bus in GREENPAK_PIO mode */
  193. /*********************************************************/
  194. // Wait for ACK to go high and back low.
  195. // This indicates that there is a byte ready to be read
  196. // If interrupt occurs in middle, we may miss ACK going high.
  197. // In that case, verify that REQ is already low.
  198. #define ASM_WAIT_ACK(x) \
  199. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  200. " cbnz %[tmp2], ack_is_high_" x "_%= \n" \
  201. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  202. " cbnz %[tmp2], ack_is_high_" x "_%= \n" \
  203. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  204. " cbnz %[tmp2], ack_is_high_" x "_%= \n" \
  205. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  206. " cbnz %[tmp2], ack_is_high_" x "_%= \n" \
  207. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  208. " cbnz %[tmp2], ack_is_high_" x "_%= \n" \
  209. " wait_req_low_" x "_%=: \n" \
  210. " cpsid i \n" \
  211. " ldr %[tmp2], [%[req_pin_bb]] \n" \
  212. " cbz %[tmp2], ack_is_high_" x "_%= \n" \
  213. " ldr %[tmp2], [%[reset_flag]] \n" \
  214. " cbnz %[tmp2], ack_is_high_" x "_%= \n" \
  215. " cpsie i \n" \
  216. " b.n wait_req_low_" x "_%= \n" \
  217. " ack_is_high_" x "_%=: \n" \
  218. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  219. " cbz %[tmp2], ack_is_low_" x "_%= \n" \
  220. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  221. " cbz %[tmp2], ack_is_low_" x "_%= \n" \
  222. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  223. " cbz %[tmp2], ack_is_low_" x "_%= \n" \
  224. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  225. " cbz %[tmp2], ack_is_low_" x "_%= \n" \
  226. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  227. " cbz %[tmp2], ack_is_low_" x "_%= \n" \
  228. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  229. " cbz %[tmp2], ack_is_low_" x "_%= \n" \
  230. " wait_ack_low_" x "_%=: \n" \
  231. " cpsid i \n" \
  232. " ldr %[tmp2], [%[ack_pin_bb]] \n" \
  233. " cbz %[tmp2], ack_is_low_" x "_%= \n" \
  234. " ldr %[tmp2], [%[reset_flag]] \n" \
  235. " cbnz %[tmp2], ack_is_low_" x "_%= \n" \
  236. " cpsie i \n" \
  237. " b.n wait_ack_low_" x "_%= \n" \
  238. " ack_is_low_" x "_%=: \n"
  239. // Prepare for reception of data by loading the next PLD_IO1 value
  240. // and disabling interrupts.
  241. #define ASM_PREP_RECV(l) \
  242. " mov %[tmp1], %[" l "] \n" \
  243. " cpsid i \n"
  244. // Read GPIO bus, take the data byte and toggle PLD_IO1.
  245. // Note that the PLD_IO1 write is done first to reduce latency, but
  246. // the istat value that is read by next instruction is still the old
  247. // one due to IO port delays. Interrupts must be disabled for this
  248. // sequence to work correctly.
  249. //
  250. // d is the name of register where data is to be stored
  251. // b is the bit offset to store the byte at
  252. // x is unique label
  253. #define ASM_RECV_DATA(d, b, x) \
  254. " read_data_" x "_%=: \n" \
  255. " str %[tmp1], [%[out_port_bop]] \n" \
  256. " ldr %[tmp1], [%[in_port_istat]] \n" \
  257. " ubfx %[tmp1], %[tmp1], %[data_in_shift], #8 \n" \
  258. " bfi %[" d "], %[tmp1], #" b ", #8 \n" \
  259. " cpsie i \n"
  260. // Read bytes from SCSI bus using asynchronous handshake mechanism
  261. // Takes 4 bytes at a time.
  262. void scsi_accel_greenpak_recv(uint32_t *buf, uint32_t num_words, volatile int *resetFlag)
  263. {
  264. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  265. volatile uint32_t *in_port_istat = (volatile uint32_t*)&GPIO_ISTAT(SCSI_IN_PORT);
  266. uint32_t ack_pin_bb = PERIPH_BB_BASE + (((uint32_t)&GPIO_ISTAT(SCSI_ACK_PORT)) - APB1_BUS_BASE) * 32 + SCSI_IN_ACK_IDX * 4;
  267. uint32_t req_pin_bb = PERIPH_BB_BASE + (((uint32_t)&GPIO_ISTAT(SCSI_OUT_PORT)) - APB1_BUS_BASE) * 32 + SCSI_OUT_REQ_IDX * 4;
  268. register uint32_t tmp1 = 0;
  269. register uint32_t tmp2 = 0;
  270. register uint32_t data = 0;
  271. // Last word requires special handling so that hardware doesn't issue new REQ pulse.
  272. assert(num_words >= 2);
  273. num_words -= 1;
  274. // Set PLD_IO3 high to enable read from SCSI bus
  275. GPIO_BOP(SCSI_OUT_PORT) = GREENPAK_PLD_IO3;
  276. // Make sure that the previous access has fully completed.
  277. // E.g. Macintosh can hold ACK low for long time after last byte of block.
  278. while (SCSI_IN(ACK) && !*resetFlag);
  279. // Set REQ pin as input and PLD_IO2 high to enable logic
  280. GPIO_BC(SCSI_OUT_PORT) = GREENPAK_PLD_IO1;
  281. gpio_init(SCSI_OUT_PORT, GPIO_MODE_IPU, 0, SCSI_OUT_REQ);
  282. GPIO_BOP(SCSI_OUT_PORT) = GREENPAK_PLD_IO2;
  283. asm volatile (
  284. "inner_loop_%=: \n"
  285. ASM_PREP_RECV("pld1_hi")
  286. ASM_WAIT_ACK("0")
  287. ASM_RECV_DATA("data", "0", "0")
  288. ASM_PREP_RECV("pld1_lo")
  289. ASM_WAIT_ACK("8")
  290. ASM_RECV_DATA("data", "8", "8")
  291. ASM_PREP_RECV("pld1_hi")
  292. ASM_WAIT_ACK("16")
  293. ASM_RECV_DATA("data", "16", "16")
  294. ASM_PREP_RECV("pld1_lo")
  295. ASM_WAIT_ACK("24")
  296. ASM_RECV_DATA("data", "24", "24")
  297. " mvn %[data], %[data] \n"
  298. " str %[data], [%[buf]], #4 \n"
  299. " subs %[num_words], %[num_words], #1 \n"
  300. " bne inner_loop_%= \n"
  301. // Process last word separately to avoid issuing extra REQ pulse at end.
  302. "recv_last_word_%=: \n"
  303. ASM_PREP_RECV("pld1_hi")
  304. ASM_WAIT_ACK("0b")
  305. ASM_RECV_DATA("data", "0", "0b")
  306. ASM_PREP_RECV("pld1_lo")
  307. ASM_WAIT_ACK("8b")
  308. ASM_RECV_DATA("data", "8", "8b")
  309. ASM_PREP_RECV("pld1_hi")
  310. ASM_WAIT_ACK("16b")
  311. ASM_RECV_DATA("data", "16", "16b")
  312. ASM_PREP_RECV("pld1_hi")
  313. ASM_WAIT_ACK("24b")
  314. ASM_RECV_DATA("data", "24", "24b")
  315. " mvn %[data], %[data] \n"
  316. " str %[data], [%[buf]], #4 \n"
  317. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  318. [buf] "+r" (buf), [num_words] "+r" (num_words)
  319. : /* Input */ [ack_pin_bb] "r" (ack_pin_bb),
  320. [req_pin_bb] "r" (req_pin_bb),
  321. [out_port_bop] "r"(out_port_bop),
  322. [in_port_istat] "r" (in_port_istat),
  323. [reset_flag] "r" (resetFlag),
  324. [data_in_shift] "I" (SCSI_IN_SHIFT),
  325. [pld1_lo] "I" (SCSI_OUT_PLD1 << 16),
  326. [pld1_hi] "I" (SCSI_OUT_PLD1)
  327. : /* Clobber */ );
  328. SCSI_RELEASE_DATA_REQ();
  329. // Disable external logic and set REQ pin as output
  330. GPIO_BC(SCSI_OUT_PORT) = GREENPAK_PLD_IO2;
  331. gpio_init(SCSI_OUT_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_REQ);
  332. GPIO_BC(SCSI_OUT_PORT) = GREENPAK_PLD_IO3;
  333. }
  334. #endif