stm32mp1_scmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright (c) 2019-2021, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <platform_def.h>
  9. #include <drivers/clk.h>
  10. #include <drivers/scmi-msg.h>
  11. #include <drivers/scmi.h>
  12. #include <drivers/st/stm32mp1_clk.h>
  13. #include <drivers/st/stm32mp_reset.h>
  14. #include <dt-bindings/clock/stm32mp1-clks.h>
  15. #include <dt-bindings/reset/stm32mp1-resets.h>
  16. #define TIMEOUT_US_1MS 1000U
  17. #define SCMI_CLOCK_NAME_SIZE 16U
  18. #define SCMI_RSTD_NAME_SIZE 16U
  19. /*
  20. * struct stm32_scmi_clk - Data for the exposed clock
  21. * @clock_id: Clock identifier in RCC clock driver
  22. * @name: Clock string ID exposed to agent
  23. * @enabled: State of the SCMI clock
  24. */
  25. struct stm32_scmi_clk {
  26. unsigned long clock_id;
  27. const char *name;
  28. bool enabled;
  29. };
  30. /*
  31. * struct stm32_scmi_rstd - Data for the exposed reset controller
  32. * @reset_id: Reset identifier in RCC reset driver
  33. * @name: Reset string ID exposed to agent
  34. */
  35. struct stm32_scmi_rstd {
  36. unsigned long reset_id;
  37. const char *name;
  38. };
  39. /* Locate all non-secure SMT message buffers in last page of SYSRAM */
  40. #define SMT_BUFFER_BASE STM32MP_SCMI_NS_SHM_BASE
  41. #define SMT_BUFFER0_BASE SMT_BUFFER_BASE
  42. #define SMT_BUFFER1_BASE (SMT_BUFFER_BASE + 0x200)
  43. CASSERT((STM32MP_SCMI_NS_SHM_BASE + STM32MP_SCMI_NS_SHM_SIZE) >=
  44. (SMT_BUFFER1_BASE + SMT_BUF_SLOT_SIZE),
  45. assert_scmi_non_secure_shm_fits_scmi_overall_buffer_size);
  46. static struct scmi_msg_channel scmi_channel[] = {
  47. [0] = {
  48. .shm_addr = SMT_BUFFER0_BASE,
  49. .shm_size = SMT_BUF_SLOT_SIZE,
  50. },
  51. [1] = {
  52. .shm_addr = SMT_BUFFER1_BASE,
  53. .shm_size = SMT_BUF_SLOT_SIZE,
  54. },
  55. };
  56. struct scmi_msg_channel *plat_scmi_get_channel(unsigned int agent_id)
  57. {
  58. assert(agent_id < ARRAY_SIZE(scmi_channel));
  59. return &scmi_channel[agent_id];
  60. }
  61. #define CLOCK_CELL(_scmi_id, _id, _name, _init_enabled) \
  62. [_scmi_id] = { \
  63. .clock_id = _id, \
  64. .name = _name, \
  65. .enabled = _init_enabled, \
  66. }
  67. static struct stm32_scmi_clk stm32_scmi0_clock[] = {
  68. CLOCK_CELL(CK_SCMI0_HSE, CK_HSE, "ck_hse", true),
  69. CLOCK_CELL(CK_SCMI0_HSI, CK_HSI, "ck_hsi", true),
  70. CLOCK_CELL(CK_SCMI0_CSI, CK_CSI, "ck_csi", true),
  71. CLOCK_CELL(CK_SCMI0_LSE, CK_LSE, "ck_lse", true),
  72. CLOCK_CELL(CK_SCMI0_LSI, CK_LSI, "ck_lsi", true),
  73. CLOCK_CELL(CK_SCMI0_PLL2_Q, PLL2_Q, "pll2_q", true),
  74. CLOCK_CELL(CK_SCMI0_PLL2_R, PLL2_R, "pll2_r", true),
  75. CLOCK_CELL(CK_SCMI0_MPU, CK_MPU, "ck_mpu", true),
  76. CLOCK_CELL(CK_SCMI0_AXI, CK_AXI, "ck_axi", true),
  77. CLOCK_CELL(CK_SCMI0_BSEC, BSEC, "bsec", true),
  78. CLOCK_CELL(CK_SCMI0_CRYP1, CRYP1, "cryp1", false),
  79. CLOCK_CELL(CK_SCMI0_GPIOZ, GPIOZ, "gpioz", false),
  80. CLOCK_CELL(CK_SCMI0_HASH1, HASH1, "hash1", false),
  81. CLOCK_CELL(CK_SCMI0_I2C4, I2C4_K, "i2c4_k", false),
  82. CLOCK_CELL(CK_SCMI0_I2C6, I2C6_K, "i2c6_k", false),
  83. CLOCK_CELL(CK_SCMI0_IWDG1, IWDG1, "iwdg1", false),
  84. CLOCK_CELL(CK_SCMI0_RNG1, RNG1_K, "rng1_k", true),
  85. CLOCK_CELL(CK_SCMI0_RTC, RTC, "ck_rtc", true),
  86. CLOCK_CELL(CK_SCMI0_RTCAPB, RTCAPB, "rtcapb", true),
  87. CLOCK_CELL(CK_SCMI0_SPI6, SPI6_K, "spi6_k", false),
  88. CLOCK_CELL(CK_SCMI0_USART1, USART1_K, "usart1_k", false),
  89. };
  90. static struct stm32_scmi_clk stm32_scmi1_clock[] = {
  91. CLOCK_CELL(CK_SCMI1_PLL3_Q, PLL3_Q, "pll3_q", true),
  92. CLOCK_CELL(CK_SCMI1_PLL3_R, PLL3_R, "pll3_r", true),
  93. CLOCK_CELL(CK_SCMI1_MCU, CK_MCU, "ck_mcu", false),
  94. };
  95. #define RESET_CELL(_scmi_id, _id, _name) \
  96. [_scmi_id] = { \
  97. .reset_id = _id, \
  98. .name = _name, \
  99. }
  100. static struct stm32_scmi_rstd stm32_scmi0_reset_domain[] = {
  101. RESET_CELL(RST_SCMI0_SPI6, SPI6_R, "spi6"),
  102. RESET_CELL(RST_SCMI0_I2C4, I2C4_R, "i2c4"),
  103. RESET_CELL(RST_SCMI0_I2C6, I2C6_R, "i2c6"),
  104. RESET_CELL(RST_SCMI0_USART1, USART1_R, "usart1"),
  105. RESET_CELL(RST_SCMI0_STGEN, STGEN_R, "stgen"),
  106. RESET_CELL(RST_SCMI0_GPIOZ, GPIOZ_R, "gpioz"),
  107. RESET_CELL(RST_SCMI0_CRYP1, CRYP1_R, "cryp1"),
  108. RESET_CELL(RST_SCMI0_HASH1, HASH1_R, "hash1"),
  109. RESET_CELL(RST_SCMI0_RNG1, RNG1_R, "rng1"),
  110. RESET_CELL(RST_SCMI0_MDMA, MDMA_R, "mdma"),
  111. RESET_CELL(RST_SCMI0_MCU, MCU_R, "mcu"),
  112. };
  113. struct scmi_agent_resources {
  114. struct stm32_scmi_clk *clock;
  115. size_t clock_count;
  116. struct stm32_scmi_rstd *rstd;
  117. size_t rstd_count;
  118. };
  119. static const struct scmi_agent_resources agent_resources[] = {
  120. [0] = {
  121. .clock = stm32_scmi0_clock,
  122. .clock_count = ARRAY_SIZE(stm32_scmi0_clock),
  123. .rstd = stm32_scmi0_reset_domain,
  124. .rstd_count = ARRAY_SIZE(stm32_scmi0_reset_domain),
  125. },
  126. [1] = {
  127. .clock = stm32_scmi1_clock,
  128. .clock_count = ARRAY_SIZE(stm32_scmi1_clock),
  129. },
  130. };
  131. static const struct scmi_agent_resources *find_resource(unsigned int agent_id)
  132. {
  133. assert(agent_id < ARRAY_SIZE(agent_resources));
  134. return &agent_resources[agent_id];
  135. }
  136. #if ENABLE_ASSERTIONS
  137. static size_t plat_scmi_protocol_count_paranoid(void)
  138. {
  139. unsigned int n = 0U;
  140. unsigned int count = 0U;
  141. for (n = 0U; n < ARRAY_SIZE(agent_resources); n++) {
  142. if (agent_resources[n].clock_count) {
  143. count++;
  144. break;
  145. }
  146. }
  147. for (n = 0U; n < ARRAY_SIZE(agent_resources); n++) {
  148. if (agent_resources[n].rstd_count) {
  149. count++;
  150. break;
  151. }
  152. }
  153. return count;
  154. }
  155. #endif
  156. static const char vendor[] = "ST";
  157. static const char sub_vendor[] = "";
  158. const char *plat_scmi_vendor_name(void)
  159. {
  160. return vendor;
  161. }
  162. const char *plat_scmi_sub_vendor_name(void)
  163. {
  164. return sub_vendor;
  165. }
  166. /* Currently supporting Clocks and Reset Domains */
  167. static const uint8_t plat_protocol_list[] = {
  168. SCMI_PROTOCOL_ID_CLOCK,
  169. SCMI_PROTOCOL_ID_RESET_DOMAIN,
  170. 0U /* Null termination */
  171. };
  172. size_t plat_scmi_protocol_count(void)
  173. {
  174. const size_t count = ARRAY_SIZE(plat_protocol_list) - 1U;
  175. assert(count == plat_scmi_protocol_count_paranoid());
  176. return count;
  177. }
  178. const uint8_t *plat_scmi_protocol_list(unsigned int agent_id __unused)
  179. {
  180. assert(plat_scmi_protocol_count_paranoid() ==
  181. (ARRAY_SIZE(plat_protocol_list) - 1U));
  182. return plat_protocol_list;
  183. }
  184. /*
  185. * Platform SCMI clocks
  186. */
  187. static struct stm32_scmi_clk *find_clock(unsigned int agent_id,
  188. unsigned int scmi_id)
  189. {
  190. const struct scmi_agent_resources *resource = find_resource(agent_id);
  191. size_t n = 0U;
  192. if (resource != NULL) {
  193. for (n = 0U; n < resource->clock_count; n++) {
  194. if (n == scmi_id) {
  195. return &resource->clock[n];
  196. }
  197. }
  198. }
  199. return NULL;
  200. }
  201. size_t plat_scmi_clock_count(unsigned int agent_id)
  202. {
  203. const struct scmi_agent_resources *resource = find_resource(agent_id);
  204. if (resource == NULL) {
  205. return 0U;
  206. }
  207. return resource->clock_count;
  208. }
  209. const char *plat_scmi_clock_get_name(unsigned int agent_id,
  210. unsigned int scmi_id)
  211. {
  212. struct stm32_scmi_clk *clock = find_clock(agent_id, scmi_id);
  213. if ((clock == NULL) ||
  214. !stm32mp_nsec_can_access_clock(clock->clock_id)) {
  215. return NULL;
  216. }
  217. return clock->name;
  218. }
  219. int32_t plat_scmi_clock_rates_array(unsigned int agent_id, unsigned int scmi_id,
  220. unsigned long *array, size_t *nb_elts,
  221. uint32_t start_idx)
  222. {
  223. struct stm32_scmi_clk *clock = find_clock(agent_id, scmi_id);
  224. if (clock == NULL) {
  225. return SCMI_NOT_FOUND;
  226. }
  227. if (!stm32mp_nsec_can_access_clock(clock->clock_id)) {
  228. return SCMI_DENIED;
  229. }
  230. if (start_idx > 0) {
  231. return SCMI_OUT_OF_RANGE;
  232. }
  233. if (array == NULL) {
  234. *nb_elts = 1U;
  235. } else if (*nb_elts == 1U) {
  236. *array = clk_get_rate(clock->clock_id);
  237. } else {
  238. return SCMI_GENERIC_ERROR;
  239. }
  240. return SCMI_SUCCESS;
  241. }
  242. unsigned long plat_scmi_clock_get_rate(unsigned int agent_id,
  243. unsigned int scmi_id)
  244. {
  245. struct stm32_scmi_clk *clock = find_clock(agent_id, scmi_id);
  246. if ((clock == NULL) ||
  247. !stm32mp_nsec_can_access_clock(clock->clock_id)) {
  248. return 0U;
  249. }
  250. return clk_get_rate(clock->clock_id);
  251. }
  252. int32_t plat_scmi_clock_get_state(unsigned int agent_id, unsigned int scmi_id)
  253. {
  254. struct stm32_scmi_clk *clock = find_clock(agent_id, scmi_id);
  255. if ((clock == NULL) ||
  256. !stm32mp_nsec_can_access_clock(clock->clock_id)) {
  257. return 0U;
  258. }
  259. return (int32_t)clock->enabled;
  260. }
  261. int32_t plat_scmi_clock_set_state(unsigned int agent_id, unsigned int scmi_id,
  262. bool enable_not_disable)
  263. {
  264. struct stm32_scmi_clk *clock = find_clock(agent_id, scmi_id);
  265. if (clock == NULL) {
  266. return SCMI_NOT_FOUND;
  267. }
  268. if (!stm32mp_nsec_can_access_clock(clock->clock_id)) {
  269. return SCMI_DENIED;
  270. }
  271. if (enable_not_disable) {
  272. if (!clock->enabled) {
  273. VERBOSE("SCMI clock %u enable\n", scmi_id);
  274. clk_enable(clock->clock_id);
  275. clock->enabled = true;
  276. }
  277. } else {
  278. if (clock->enabled) {
  279. VERBOSE("SCMI clock %u disable\n", scmi_id);
  280. clk_disable(clock->clock_id);
  281. clock->enabled = false;
  282. }
  283. }
  284. return SCMI_SUCCESS;
  285. }
  286. /*
  287. * Platform SCMI reset domains
  288. */
  289. static struct stm32_scmi_rstd *find_rstd(unsigned int agent_id,
  290. unsigned int scmi_id)
  291. {
  292. const struct scmi_agent_resources *resource = find_resource(agent_id);
  293. size_t n;
  294. if (resource != NULL) {
  295. for (n = 0U; n < resource->rstd_count; n++) {
  296. if (n == scmi_id) {
  297. return &resource->rstd[n];
  298. }
  299. }
  300. }
  301. return NULL;
  302. }
  303. const char *plat_scmi_rstd_get_name(unsigned int agent_id, unsigned int scmi_id)
  304. {
  305. const struct stm32_scmi_rstd *rstd = find_rstd(agent_id, scmi_id);
  306. if (rstd == NULL) {
  307. return NULL;
  308. }
  309. return rstd->name;
  310. }
  311. size_t plat_scmi_rstd_count(unsigned int agent_id)
  312. {
  313. const struct scmi_agent_resources *resource = find_resource(agent_id);
  314. if (resource == NULL) {
  315. return 0U;
  316. }
  317. return resource->rstd_count;
  318. }
  319. int32_t plat_scmi_rstd_autonomous(unsigned int agent_id, unsigned int scmi_id,
  320. uint32_t state)
  321. {
  322. const struct stm32_scmi_rstd *rstd = find_rstd(agent_id, scmi_id);
  323. if (rstd == NULL) {
  324. return SCMI_NOT_FOUND;
  325. }
  326. if (!stm32mp_nsec_can_access_reset(rstd->reset_id)) {
  327. return SCMI_DENIED;
  328. }
  329. /* Supports only reset with context loss */
  330. if (state != 0U) {
  331. return SCMI_NOT_SUPPORTED;
  332. }
  333. VERBOSE("SCMI reset %lu cycle\n", rstd->reset_id);
  334. if (stm32mp_reset_assert(rstd->reset_id, TIMEOUT_US_1MS)) {
  335. return SCMI_HARDWARE_ERROR;
  336. }
  337. if (stm32mp_reset_deassert(rstd->reset_id, TIMEOUT_US_1MS)) {
  338. return SCMI_HARDWARE_ERROR;
  339. }
  340. return SCMI_SUCCESS;
  341. }
  342. int32_t plat_scmi_rstd_set_state(unsigned int agent_id, unsigned int scmi_id,
  343. bool assert_not_deassert)
  344. {
  345. const struct stm32_scmi_rstd *rstd = find_rstd(agent_id, scmi_id);
  346. if (rstd == NULL) {
  347. return SCMI_NOT_FOUND;
  348. }
  349. if (!stm32mp_nsec_can_access_reset(rstd->reset_id)) {
  350. return SCMI_DENIED;
  351. }
  352. if (assert_not_deassert) {
  353. VERBOSE("SCMI reset %lu set\n", rstd->reset_id);
  354. stm32mp_reset_set(rstd->reset_id);
  355. } else {
  356. VERBOSE("SCMI reset %lu release\n", rstd->reset_id);
  357. stm32mp_reset_release(rstd->reset_id);
  358. }
  359. return SCMI_SUCCESS;
  360. }
  361. /*
  362. * Initialize platform SCMI resources
  363. */
  364. void stm32mp1_init_scmi_server(void)
  365. {
  366. size_t i;
  367. for (i = 0U; i < ARRAY_SIZE(scmi_channel); i++) {
  368. scmi_smt_init_agent_channel(&scmi_channel[i]);
  369. }
  370. for (i = 0U; i < ARRAY_SIZE(agent_resources); i++) {
  371. const struct scmi_agent_resources *res = &agent_resources[i];
  372. size_t j;
  373. for (j = 0U; j < res->clock_count; j++) {
  374. struct stm32_scmi_clk *clk = &res->clock[j];
  375. if ((clk->name == NULL) ||
  376. (strlen(clk->name) >= SCMI_CLOCK_NAME_SIZE)) {
  377. ERROR("Invalid SCMI clock name\n");
  378. panic();
  379. }
  380. /* Sync SCMI clocks with their targeted initial state */
  381. if (clk->enabled &&
  382. stm32mp_nsec_can_access_clock(clk->clock_id)) {
  383. clk_enable(clk->clock_id);
  384. }
  385. }
  386. for (j = 0U; j < res->rstd_count; j++) {
  387. struct stm32_scmi_rstd *rstd = &res->rstd[j];
  388. if ((rstd->name == NULL) ||
  389. (strlen(rstd->name) >= SCMI_RSTD_NAME_SIZE)) {
  390. ERROR("Invalid SCMI reset domain name\n");
  391. panic();
  392. }
  393. }
  394. }
  395. }