timings_RP2MCU.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2024-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 "timings_RP2MCU.h"
  22. #include <string.h>
  23. #include "timings.h"
  24. static zuluscsi_timings_t predefined_timings[] = {
  25. // predefined_timings[0] - 125000000
  26. {
  27. .clk_hz = 125000000,
  28. .pll =
  29. {
  30. .refdiv = 1,
  31. .vco_freq = 1500000000,
  32. .post_div1 = 6,
  33. .post_div2 = 2
  34. },
  35. .scsi =
  36. {
  37. .req_delay = 7,
  38. .clk_period_ps = 8000
  39. },
  40. .scsi_20 =
  41. {
  42. .delay0 = 3 - 1,
  43. .delay1 = 3 - 1,
  44. .total_period_adjust = -1,
  45. .rdelay1 = 3 - 1,
  46. .rtotal_period_adjust = -1,
  47. .max_sync = 12,
  48. },
  49. .scsi_10 =
  50. {
  51. .delay0 = 5 - 1,
  52. .delay1 = 7 - 1,
  53. .total_period_adjust = -1,
  54. .rdelay1 = 6,
  55. .rtotal_period_adjust = -1,
  56. .max_sync = 25,
  57. },
  58. .scsi_5 =
  59. {
  60. .delay0 = 10 - 1,
  61. .delay1 = 15 - 1,
  62. .total_period_adjust = -1,
  63. .rdelay1 = 15 - 1,
  64. .rtotal_period_adjust = -1,
  65. .max_sync = 50,
  66. },
  67. .sdio =
  68. {
  69. .clk_div_1mhz = 25, // = 125MHz clk / clk_div_pio
  70. .clk_div_pio = 4,
  71. .delay0 = 3 - 1, // subtract one for the instruction delay
  72. .delay1 = 1 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  73. },
  74. .audio =
  75. {
  76. .clk_div_pio = 48,
  77. .audio_clocked = false,
  78. }
  79. },
  80. // predefined_timings[1] - 133000000
  81. {
  82. .clk_hz = 133000000,
  83. .pll =
  84. {
  85. .refdiv = 1,
  86. .vco_freq = 1596000000,
  87. .post_div1 = 6,
  88. .post_div2 = 2
  89. },
  90. .scsi =
  91. {
  92. .req_delay = 7,
  93. .clk_period_ps = 7519
  94. },
  95. .scsi_20 =
  96. {
  97. .delay0 = 3 - 1,
  98. .delay1 = 3 - 1,
  99. .total_period_adjust = -1,
  100. .rdelay1 = 3 - 1,
  101. .rtotal_period_adjust = -1,
  102. .max_sync = 12,
  103. },
  104. .scsi_10 =
  105. {
  106. .delay0 = 5 - 1,
  107. .delay1 = 7 - 1,
  108. .total_period_adjust = -1,
  109. .rdelay1 = 7 - 1,
  110. .rtotal_period_adjust = 0,
  111. .max_sync = 25,
  112. },
  113. .scsi_5 =
  114. {
  115. .delay0 = 10 - 1,
  116. .delay1 = 15 - 1,
  117. .total_period_adjust = -1,
  118. .rdelay1 = 15 - 1,
  119. .rtotal_period_adjust = 0,
  120. .max_sync = 50,
  121. },
  122. .sdio =
  123. {
  124. .clk_div_1mhz = 25,
  125. .clk_div_pio = 5,
  126. .delay0 = 3 - 1, // subtract one for the instruction delay
  127. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  128. },
  129. .audio =
  130. {
  131. .clk_div_pio = 48,
  132. .audio_clocked = false,
  133. }
  134. },
  135. // predefined_timings[2] - 135428571 - RP2040 Audio DAC Attack S/PDIF clocks
  136. {
  137. .clk_hz = 135428571,
  138. .pll =
  139. {
  140. .refdiv = 1,
  141. .vco_freq = 948000000,
  142. .post_div1 = 7,
  143. .post_div2 = 1
  144. },
  145. .scsi =
  146. {
  147. .req_delay = 7,
  148. .clk_period_ps = 7384
  149. },
  150. .scsi_20 =
  151. {
  152. .delay0 = 3 - 1,
  153. .delay1 = 3 - 1,
  154. .total_period_adjust = -1,
  155. .rdelay1 = 3 - 1,
  156. .rtotal_period_adjust = -1,
  157. .max_sync = 12,
  158. },
  159. .scsi_10 =
  160. {
  161. .delay0 = 5 - 1,
  162. .delay1 = 7 - 1,
  163. .total_period_adjust = -1,
  164. .rdelay1 = 7 - 1,
  165. .rtotal_period_adjust = -1,
  166. .max_sync = 25,
  167. },
  168. .scsi_5 =
  169. {
  170. .delay0 = 10 - 1,
  171. .delay1 = 15 - 1,
  172. .total_period_adjust = -1,
  173. .rdelay1 = 15 - 1,
  174. .rtotal_period_adjust = -1,
  175. .max_sync = 50,
  176. },
  177. .sdio =
  178. {
  179. .clk_div_1mhz = 27 , // = 135MHz clk / clk_div_pio
  180. .clk_div_pio = 5,
  181. .delay0 = 3 - 1, // subtract one for the instruction delay
  182. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  183. },
  184. .audio =
  185. {
  186. // 44.1KHz to the nearest integer with a sys clk of 135.43MHz and 2 x 16-bit samples with the pio clock running 2x I2S clock
  187. // 135.43Mhz / 16 / 2 / 2 / 44.1KHz = 47.98 ~= 48
  188. .clk_div_pio = 48,
  189. .audio_clocked = true,
  190. }
  191. },
  192. // predefined_timings[3] - 150000000
  193. {
  194. .clk_hz = 150000000,
  195. .pll =
  196. {
  197. .refdiv = 1,
  198. .vco_freq = 1500000000,
  199. .post_div1 = 5,
  200. .post_div2 = 2
  201. },
  202. .scsi =
  203. {
  204. .req_delay = 9,
  205. .clk_period_ps = 6667
  206. },
  207. .scsi_20 =
  208. {
  209. .delay0 = 2 - 1,
  210. .delay1 = 3 - 1,
  211. .total_period_adjust = 0,
  212. .rdelay1 = 3 - 1,
  213. .rtotal_period_adjust = -1,
  214. .max_sync = 12,
  215. },
  216. .scsi_10 =
  217. {
  218. .delay0 = 4 - 1,
  219. .delay1 = 5 - 1,
  220. .total_period_adjust = 0,
  221. .rdelay1 = 5 - 1,
  222. .rtotal_period_adjust = 0,
  223. .max_sync = 25,
  224. },
  225. .scsi_5 =
  226. {
  227. .delay0 = 10 - 1,
  228. .delay1 = 15, // should be 18 - 1 but max currently is 15
  229. .total_period_adjust = 0,
  230. .rdelay1 = 15,
  231. .rtotal_period_adjust = 0,
  232. .max_sync = 50,
  233. },
  234. .sdio =
  235. {
  236. .clk_div_1mhz = 30,// = 150MHz clk / clk_div_pio
  237. .clk_div_pio = 5,
  238. .delay0 = 3 - 1, // subtract one for the instruction delay
  239. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  240. },
  241. .audio =
  242. {
  243. .clk_div_pio = 54,
  244. .audio_clocked = false,
  245. }
  246. },
  247. // predefined_timings[4] - 250000000
  248. {
  249. .clk_hz = 250000000,
  250. .pll =
  251. {
  252. .refdiv = 1,
  253. .vco_freq = 1500000000,
  254. .post_div1 = 6,
  255. .post_div2 = 1
  256. },
  257. .scsi =
  258. {
  259. .req_delay = 14,
  260. .clk_period_ps = 4000,
  261. },
  262. .scsi_20 =
  263. {
  264. .delay0 = 3 - 1,
  265. .delay1 = 5 - 1,
  266. .total_period_adjust = 1,
  267. .rdelay1 = 5 - 1,
  268. .rtotal_period_adjust = -1,
  269. .max_sync = 12,
  270. },
  271. .scsi_10 =
  272. {
  273. .delay0 = 6 - 1,
  274. .delay1 = 8 - 1,
  275. .total_period_adjust = 0,
  276. .rdelay1 = 8 - 1,
  277. .rtotal_period_adjust = 0,
  278. .max_sync = 25,
  279. },
  280. .scsi_5 =
  281. {
  282. .delay0 = 15, // maxed out should be 16
  283. .delay1 = 15, // maxed out should be 30
  284. .total_period_adjust = 1,
  285. .rdelay1 = 15,
  286. .rtotal_period_adjust = 1,
  287. .max_sync = 50,
  288. },
  289. .sdio =
  290. {
  291. .clk_div_1mhz = 30,// set by trail and error
  292. .clk_div_pio = 5, // SDIO at 50MHz
  293. .delay0 = 4 - 1, // subtract one for the instruction delay
  294. .delay1 = 1 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  295. }
  296. },
  297. // predefined_timings[5] - 155250000 - Default clocks for Blaster I2S Audio
  298. {
  299. .clk_hz = 155250000,
  300. .pll =
  301. {
  302. .refdiv = 3,
  303. .vco_freq = 1242000000,
  304. .post_div1 = 4,
  305. .post_div2 = 2,
  306. },
  307. .scsi =
  308. {
  309. .req_delay = 10,
  310. .clk_period_ps = 6441
  311. },
  312. .scsi_20 =
  313. {
  314. .delay0 = 2 - 1,
  315. .delay1 = 3 - 1,
  316. .total_period_adjust = 0,
  317. .rdelay1 = 3 - 1,
  318. .rtotal_period_adjust = -1,
  319. .max_sync = 12,
  320. },
  321. .scsi_10 =
  322. {
  323. .delay0 = 4 - 1,
  324. .delay1 = 5 - 1,
  325. .total_period_adjust = 0,
  326. .rdelay1 = 5 - 1,
  327. .rtotal_period_adjust = 0,
  328. .max_sync = 25,
  329. },
  330. .scsi_5 =
  331. {
  332. .delay0 = 10 - 1,
  333. .delay1 = 15, // should be 18 - 1 but max currently is 15
  334. .total_period_adjust = 0,
  335. .rdelay1 = 15,
  336. .rtotal_period_adjust = 0,
  337. .max_sync = 50,
  338. },
  339. .sdio =
  340. {
  341. .clk_div_1mhz = 26, // = 155.25MHz clk / clk_div_pio
  342. .clk_div_pio = 6,
  343. .delay0 = 4 - 1, // subtract one for the instruction delay
  344. .delay1 = 2 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  345. },
  346. .audio =
  347. {
  348. // 44.1KHz to the nearest integer with a sys clk of 155.25Mhz and 2 x 16-bit samples with the pio clock running 2x I2S clock
  349. // 155.25Mhz / 16 / 2 / 2 / 44.1KHz = 55.01 ~= 55
  350. .clk_div_pio = 55,
  351. .audio_clocked = true,
  352. }
  353. },
  354. // predefined_timings[6] - 175000000 - Alternate clocking for I2S Audio
  355. {
  356. // predefined_timings[6] - Clocking for I2S Audio at 175MHz system clock
  357. .clk_hz = 175000000,
  358. .pll =
  359. {
  360. .refdiv = 2,
  361. .vco_freq = 1050000000,
  362. .post_div1 = 6,
  363. .post_div2 = 1,
  364. },
  365. .scsi =
  366. {
  367. .req_delay = 10,
  368. .clk_period_ps = 5714
  369. },
  370. .scsi_20 =
  371. {
  372. .delay0 = 3 - 1,
  373. .delay1 = 4 - 1,
  374. .total_period_adjust = -1,
  375. .rdelay1 = 3 - 1,
  376. .rtotal_period_adjust = -1,
  377. .max_sync = 12,
  378. },
  379. .scsi_10 =
  380. {
  381. .delay0 = 4 - 1,
  382. .delay1 = 6 - 1,
  383. .total_period_adjust = 0,
  384. .rdelay1 = 6 - 1,
  385. .rtotal_period_adjust = 0,
  386. .max_sync = 25,
  387. },
  388. .scsi_5 =
  389. {
  390. .delay0 = 4 - 1,
  391. .delay1 = 10 - 1,
  392. .total_period_adjust = 0,
  393. .rdelay1 = 10 - 1,
  394. .rtotal_period_adjust = 0,
  395. .max_sync = 50,
  396. },
  397. .sdio =
  398. {
  399. .clk_div_1mhz = 30,
  400. .clk_div_pio = 5,
  401. .delay0 = 4 - 1, // subtract one for the instruction delay
  402. .delay1 = 1 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  403. },
  404. .audio =
  405. {
  406. // Divider for 44.1KHz to the nearest integer with a sys clk divided by 2 x 16-bit samples with the pio clock running 2x I2S clock
  407. // 175Mhz / 16 / 2 / 2 / 44.1KHz ~= 62
  408. .clk_div_pio = 62,
  409. .audio_clocked = true,
  410. }
  411. },
  412. // predefined_timings[7] - 200400000 - Alternate clocking for I2S Audio
  413. {
  414. .clk_hz = 200400000,
  415. .pll =
  416. {
  417. .refdiv = 2,
  418. .vco_freq = 1002000000,
  419. .post_div1 = 5,
  420. .post_div2 = 1,
  421. },
  422. .scsi =
  423. {
  424. .req_delay = 12,
  425. .clk_period_ps = 4990
  426. },
  427. .scsi_20 =
  428. {
  429. .delay0 = 3 - 1,
  430. .delay1 = 4 - 1,
  431. .total_period_adjust = 1,
  432. .rdelay1 = 5 - 1,
  433. .rtotal_period_adjust = -1,
  434. .max_sync = 12,
  435. },
  436. .scsi_10 =
  437. {
  438. .delay0 = 5 - 1,
  439. .delay1 = 7 - 1,
  440. .total_period_adjust = 0,
  441. .rdelay1 = 7- 1,
  442. .rtotal_period_adjust = 0,
  443. .max_sync = 25,
  444. },
  445. .scsi_5 =
  446. {
  447. .delay0 = 5 - 1,
  448. .delay1 = 11 - 1,
  449. .total_period_adjust = 0,
  450. .rdelay1 = 11 - 1,
  451. .rtotal_period_adjust = 0,
  452. .max_sync = 50,
  453. },
  454. .sdio =
  455. {
  456. .clk_div_1mhz = 30,
  457. .clk_div_pio = 5,
  458. .delay0 = 4 - 1, // subtract one for the instruction delay
  459. .delay1 = 1 - 1 // clk_div_pio - delay0 and subtract one for the instruction delay
  460. },
  461. .audio =
  462. {
  463. // Divider for 44.1KHz to the nearest integer with a sys clk divided by 2 x 16-bit samples with the pio clock running 2x I2S clock
  464. // 200.4Mhz / 16 / 2 / 2 / 44.1KHz = 71.003 ~= 71
  465. .clk_div_pio = 71,
  466. .audio_clocked = true,
  467. }
  468. },
  469. };
  470. static zuluscsi_timings_t current_timings;
  471. #ifdef ENABLE_AUDIO_OUTPUT_SPDIF
  472. zuluscsi_timings_t *g_zuluscsi_timings = &predefined_timings[2];
  473. #elif defined(ENABLE_AUDIO_OUTPUT_I2S)
  474. zuluscsi_timings_t *g_zuluscsi_timings = &predefined_timings[7];
  475. #elif defined(ZULUSCSI_MCU_RP23XX)
  476. zuluscsi_timings_t *g_zuluscsi_timings = &predefined_timings[3];
  477. #elif defined(ZULUSCSI_PICO)
  478. zuluscsi_timings_t *g_zuluscsi_timings = &predefined_timings[1];
  479. #else
  480. zuluscsi_timings_t *g_zuluscsi_timings = &predefined_timings[0];
  481. #endif
  482. bool set_timings(zuluscsi_speed_grade_t speed_grade)
  483. {
  484. uint8_t timings_index = 0;
  485. switch (speed_grade)
  486. {
  487. #ifdef ENABLE_AUDIO_OUTPUT_I2S
  488. case SPEED_GRADE_MAX:
  489. case SPEED_GRADE_A:
  490. timings_index = 4;
  491. break;
  492. case SPEED_GRADE_B:
  493. timings_index = 6;
  494. break;
  495. case SPEED_GRADE_C:
  496. timings_index = 5;
  497. break;
  498. case SPEED_GRADE_AUDIO_SPDIF:
  499. timings_index = 2;
  500. break;
  501. case SPEED_GRADE_AUDIO_I2S:
  502. timings_index = 7;
  503. break;
  504. #elif defined(ZULUSCSI_MCU_RP23XX)
  505. case SPEED_GRADE_MAX:
  506. case SPEED_GRADE_A:
  507. timings_index = 4;
  508. break;
  509. case SPEED_GRADE_B:
  510. timings_index = 7;
  511. break;
  512. case SPEED_GRADE_C:
  513. timings_index = 6;
  514. break;
  515. case SPEED_GRADE_AUDIO_SPDIF:
  516. timings_index = 2;
  517. break;
  518. case SPEED_GRADE_AUDIO_I2S:
  519. timings_index = 5;
  520. break;
  521. #else
  522. case SPEED_GRADE_MAX:
  523. case SPEED_GRADE_A:
  524. timings_index = 4;
  525. break;
  526. case SPEED_GRADE_B:
  527. timings_index = 3;
  528. break;
  529. case SPEED_GRADE_C:
  530. timings_index = 1;
  531. break;
  532. case SPEED_GRADE_AUDIO_SPDIF:
  533. timings_index = 2;
  534. break;
  535. case SPEED_GRADE_AUDIO_I2S:
  536. timings_index = 5;
  537. break;
  538. #endif
  539. default:
  540. break;
  541. }
  542. if (speed_grade != SPEED_GRADE_DEFAULT && speed_grade != SPEED_GRADE_CUSTOM)
  543. {
  544. g_zuluscsi_timings = &current_timings;
  545. memcpy(g_zuluscsi_timings, &predefined_timings[timings_index], sizeof(*g_zuluscsi_timings));
  546. g_max_sync_10_period = g_zuluscsi_timings->scsi_10.max_sync;
  547. g_max_sync_20_period = g_zuluscsi_timings->scsi_20.max_sync;
  548. g_max_sync_5_period = g_zuluscsi_timings->scsi_5.max_sync;
  549. return true;
  550. }
  551. return false;
  552. }