timings.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #ifdef ZULUSCSI_MCU_RP23XX
  25. zuluscsi_timings_t g_zuluscsi_timings =
  26. {
  27. .clk_hz = 150000000,
  28. .scsi =
  29. {
  30. .delay0 = 0,
  31. .delay1 = 0,
  32. .req_delay = 0,
  33. .gpio_ack = 0,
  34. .gpio_req = 0,
  35. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE
  36. },
  37. .sdio =
  38. {
  39. .clk_div_1mhz = 0,
  40. .clk_div_pio = 0,
  41. .delay0 = 0,
  42. .delay1 = 0
  43. }
  44. };
  45. #else
  46. zuluscsi_timings_t g_zuluscsi_timings =
  47. {
  48. .clk_hz = 125000000,
  49. .pll =
  50. {
  51. .refdiv = 1,
  52. .vco_freq = 1500000000,
  53. .post_div1 = 6,
  54. .post_div2 = 2
  55. },
  56. .scsi =
  57. {
  58. .req_delay = 7,
  59. .clk_period_ps = 5000
  60. },
  61. .scsi_20 =
  62. {
  63. .delay0 = 4,
  64. .delay1 = 6,
  65. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  66. .total_delay_adjust = -1,
  67. .max_sync = 25,
  68. },
  69. .scsi_10 =
  70. {
  71. .delay0 = 4,
  72. .delay1 = 6,
  73. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  74. .total_delay_adjust = -1,
  75. .max_sync = 25,
  76. },
  77. .scsi_5 =
  78. {
  79. .delay0 = 7,
  80. .delay1 = 14,
  81. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  82. .total_delay_adjust = -1,
  83. .max_sync = 50,
  84. },
  85. .sdio =
  86. {
  87. .clk_div_1mhz = 25, // = 125MHz clk / clk_div_pio
  88. .clk_div_pio = 5,
  89. .delay0 = 3 - 1, // subtract one for the instruction delay
  90. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  91. }
  92. };
  93. #endif
  94. static zuluscsi_timings_t predefined_timings[] = {
  95. {
  96. .clk_hz = 125000000,
  97. .pll =
  98. {
  99. .refdiv = 1,
  100. .vco_freq = 1500000000,
  101. .post_div1 = 6,
  102. .post_div2 = 2
  103. },
  104. .scsi =
  105. {
  106. .req_delay = 7,
  107. .clk_period_ps = 5000
  108. },
  109. .scsi_20 =
  110. {
  111. .delay0 = 4,
  112. .delay1 = 6,
  113. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  114. .total_delay_adjust = -1,
  115. .max_sync = 25,
  116. },
  117. .scsi_10 =
  118. {
  119. .delay0 = 4,
  120. .delay1 = 6,
  121. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  122. .total_delay_adjust = -1,
  123. .max_sync = 25,
  124. },
  125. .scsi_5 =
  126. {
  127. .delay0 = 7,
  128. .delay1 = 14,
  129. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  130. .total_delay_adjust = -1,
  131. .max_sync = 50,
  132. },
  133. .sdio =
  134. {
  135. .clk_div_1mhz = 25, // = 125MHz clk / clk_div_pio
  136. .clk_div_pio = 5,
  137. .delay0 = 3 - 1, // subtract one for the instruction delay
  138. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  139. }
  140. },
  141. {
  142. .clk_hz = 150000000,
  143. .pll =
  144. {
  145. .refdiv = 1,
  146. .vco_freq = 1500000000,
  147. .post_div1 = 5,
  148. .post_div2 = 2
  149. },
  150. .scsi =
  151. {
  152. .req_delay = 9,
  153. .clk_period_ps = 6667
  154. },
  155. .scsi_20 =
  156. {
  157. .delay0 = 3,
  158. .delay1 = 4,
  159. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  160. .total_delay_adjust = 0,
  161. .max_sync = 18,
  162. },
  163. .scsi_10 =
  164. {
  165. .delay0 = 4,
  166. .delay1 = 6,
  167. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  168. .total_delay_adjust = 0,
  169. .max_sync = 25,
  170. },
  171. .scsi_5 =
  172. {
  173. .delay0 = 7,
  174. .delay1 = 14,
  175. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  176. .total_delay_adjust = 0,
  177. .max_sync = 50,
  178. },
  179. .sdio =
  180. {
  181. .clk_div_1mhz = 25, // = 125MHz clk / clk_div_pio
  182. .clk_div_pio = 5,
  183. .delay0 = 3 - 1, // subtract one for the instruction delay
  184. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  185. }
  186. },
  187. {
  188. .clk_hz = 250000000,
  189. .pll =
  190. {
  191. .refdiv = 1,
  192. .vco_freq = 1500000000,
  193. .post_div1 = 6,
  194. .post_div2 = 1
  195. },
  196. .scsi =
  197. {
  198. .req_delay = 14,
  199. .clk_period_ps = 4000,
  200. },
  201. .scsi_20 =
  202. {
  203. .delay0 = 2,
  204. .delay1 = 4,
  205. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  206. .total_delay_adjust = 1,
  207. .max_sync = 12,
  208. },
  209. .scsi_10 =
  210. {
  211. .delay0 = 8,
  212. .delay1 = 10,
  213. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  214. .total_delay_adjust = 1,
  215. .max_sync = 25,
  216. },
  217. .scsi_5 =
  218. {
  219. .delay0 = 14,
  220. .delay1 = 15,
  221. .mode = ZULUSCSI_PIO_TARGET_MODE_SIMPLE,
  222. .total_delay_adjust = 1,
  223. .max_sync = 50,
  224. },
  225. .sdio =
  226. {
  227. .clk_div_1mhz = 50, // = 250MHz clk / clk_div_pio
  228. .clk_div_pio = 5, // SDIO at 50MHz
  229. .delay0 = 4 - 1, // subtract one for the instruction delay
  230. .delay1 = 1 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  231. }
  232. }
  233. };
  234. bool set_timings(uint32_t target_clk_in_khz)
  235. {
  236. uint32_t number_of_timings = sizeof(predefined_timings)/sizeof( predefined_timings[0]);
  237. for (uint8_t i = 0; i < number_of_timings; i++)
  238. {
  239. if (target_clk_in_khz == predefined_timings[i].clk_hz / 1000)
  240. {
  241. memcpy(&g_zuluscsi_timings, &predefined_timings[i], sizeof(g_zuluscsi_timings));
  242. g_max_sync_10_period = g_zuluscsi_timings.scsi_10.max_sync;
  243. g_max_sync_20_period = g_zuluscsi_timings.scsi_20.max_sync;
  244. g_max_sync_5_period = g_zuluscsi_timings.scsi_5.max_sync;
  245. return true;
  246. }
  247. }
  248. return false;
  249. }