timings.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2024 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 "timings.h"
  22. #include <string.h>
  23. #include "scsi2sd_timings.h"
  24. static zuluscsi_timings_t predefined_timings[] = {
  25. {
  26. .clk_hz = 125000000,
  27. .pll =
  28. {
  29. .refdiv = 1,
  30. .vco_freq = 1500000000,
  31. .post_div1 = 6,
  32. .post_div2 = 2
  33. },
  34. .scsi =
  35. {
  36. .req_delay = 7,
  37. .clk_period_ps = 5000
  38. },
  39. .scsi_20 =
  40. {
  41. .delay0 = 4,
  42. .delay1 = 6,
  43. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  44. .total_delay_adjust = -1,
  45. .max_sync = 25,
  46. },
  47. .scsi_10 =
  48. {
  49. .delay0 = 4,
  50. .delay1 = 6,
  51. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  52. .total_delay_adjust = -1,
  53. .max_sync = 25,
  54. },
  55. .scsi_5 =
  56. {
  57. .delay0 = 10 - 1,
  58. .delay1 = 15 - 1,
  59. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  60. .total_delay_adjust = -1,
  61. .max_sync = 50,
  62. },
  63. .sdio =
  64. {
  65. .clk_div_1mhz = 25, // = 125MHz clk / clk_div_pio
  66. .clk_div_pio = 5,
  67. .delay0 = 3 - 1, // subtract one for the instruction delay
  68. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  69. }
  70. },
  71. {
  72. .clk_hz = 150000000,
  73. .pll =
  74. {
  75. .refdiv = 1,
  76. .vco_freq = 1500000000,
  77. .post_div1 = 5,
  78. .post_div2 = 2
  79. },
  80. .scsi =
  81. {
  82. .req_delay = 9,
  83. .clk_period_ps = 6667
  84. },
  85. .scsi_20 =
  86. {
  87. .delay0 = 3 - 1,
  88. .delay1 = 4 - 1,
  89. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  90. .total_delay_adjust = 0,
  91. .max_sync = 18,
  92. },
  93. .scsi_10 =
  94. {
  95. .delay0 = 4 - 1,
  96. .delay1 = 5 - 1,
  97. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  98. .total_delay_adjust = 0,
  99. .max_sync = 25,
  100. },
  101. .scsi_5 =
  102. {
  103. .delay0 = 10 - 1,
  104. .delay1 = 15, // should be 18 - 1 but max currently is 15
  105. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  106. .total_delay_adjust = 0,
  107. .max_sync = 50,
  108. },
  109. .sdio =
  110. {
  111. .clk_div_1mhz = 30, // = 150MHz clk / clk_div_pio
  112. .clk_div_pio = 5,
  113. .delay0 = 3 - 1, // subtract one for the instruction delay
  114. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  115. }
  116. },
  117. {
  118. .clk_hz = 250000000,
  119. .pll =
  120. {
  121. .refdiv = 1,
  122. .vco_freq = 1500000000,
  123. .post_div1 = 6,
  124. .post_div2 = 1
  125. },
  126. .scsi =
  127. {
  128. .req_delay = 14,
  129. .clk_period_ps = 4000,
  130. },
  131. .scsi_20 =
  132. {
  133. .delay0 = 3 - 1,
  134. .delay1 = 5 - 1,
  135. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  136. .total_delay_adjust = 1,
  137. .max_sync = 12,
  138. },
  139. .scsi_10 =
  140. {
  141. .delay0 = 6 - 1,
  142. .delay1 = 9 - 1,
  143. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  144. .total_delay_adjust = 1,
  145. .max_sync = 25,
  146. },
  147. .scsi_5 =
  148. {
  149. .delay0 = 15, // maxed out should be 16
  150. .delay1 = 15, // maxed out should be 30
  151. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  152. .total_delay_adjust = 1,
  153. .max_sync = 50,
  154. },
  155. #ifdef ZULUSCSI_PICO_2
  156. .sdio =
  157. {
  158. .clk_div_1mhz = 30, // set by trail and error
  159. .clk_div_pio = 6, // SDIO at 41.7MHz
  160. .delay0 = 4 - 1, // subtract one for the instruction delay
  161. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  162. }
  163. #else
  164. .sdio =
  165. {
  166. .clk_div_1mhz = 50, // = 250MHz clk / clk_div_pio
  167. .clk_div_pio = 5, // SDIO at 50MHz
  168. .delay0 = 4 - 1, // subtract one for the instruction delay
  169. .delay1 = 1 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  170. }
  171. #endif
  172. }
  173. };
  174. zuluscsi_timings_t current_timings;
  175. #ifdef ZULUSCSI_MCU_RP23XX
  176. zuluscsi_timings_t *g_zuluscsi_timings = &predefined_timings[1];
  177. #else
  178. zuluscsi_timings_t *g_zuluscsi_timings = &predefined_timings[0];
  179. #endif
  180. bool set_timings(uint32_t target_clk_in_khz)
  181. {
  182. uint32_t number_of_timings = sizeof(predefined_timings)/sizeof( predefined_timings[0]);
  183. for (uint8_t i = 0; i < number_of_timings; i++)
  184. {
  185. if (target_clk_in_khz == predefined_timings[i].clk_hz / 1000)
  186. {
  187. g_zuluscsi_timings = &current_timings;
  188. memcpy(g_zuluscsi_timings, &predefined_timings[i], sizeof(current_timings));
  189. g_max_sync_10_period = g_zuluscsi_timings->scsi_10.max_sync;
  190. g_max_sync_20_period = g_zuluscsi_timings->scsi_20.max_sync;
  191. g_max_sync_5_period = g_zuluscsi_timings->scsi_5.max_sync;
  192. return true;
  193. }
  194. }
  195. return false;
  196. }