zynqmp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx Zynq MPSoC Firmware layer
  4. *
  5. * Copyright (C) 2014-2018 Xilinx, Inc.
  6. *
  7. * Michal Simek <michal.simek@xilinx.com>
  8. * Davorin Mista <davorin.mista@aggios.com>
  9. * Jolly Shah <jollys@xilinx.com>
  10. * Rajan Vaja <rajanv@xilinx.com>
  11. */
  12. #include <linux/arm-smccc.h>
  13. #include <linux/compiler.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/slab.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/firmware/xlnx-zynqmp.h>
  22. #include "zynqmp-debug.h"
  23. /**
  24. * zynqmp_pm_ret_code() - Convert PMU-FW error codes to Linux error codes
  25. * @ret_status: PMUFW return code
  26. *
  27. * Return: corresponding Linux error code
  28. */
  29. static int zynqmp_pm_ret_code(u32 ret_status)
  30. {
  31. switch (ret_status) {
  32. case XST_PM_SUCCESS:
  33. case XST_PM_DOUBLE_REQ:
  34. return 0;
  35. case XST_PM_NO_ACCESS:
  36. return -EACCES;
  37. case XST_PM_ABORT_SUSPEND:
  38. return -ECANCELED;
  39. case XST_PM_INTERNAL:
  40. case XST_PM_CONFLICT:
  41. case XST_PM_INVALID_NODE:
  42. default:
  43. return -EINVAL;
  44. }
  45. }
  46. static noinline int do_fw_call_fail(u64 arg0, u64 arg1, u64 arg2,
  47. u32 *ret_payload)
  48. {
  49. return -ENODEV;
  50. }
  51. /*
  52. * PM function call wrapper
  53. * Invoke do_fw_call_smc or do_fw_call_hvc, depending on the configuration
  54. */
  55. static int (*do_fw_call)(u64, u64, u64, u32 *ret_payload) = do_fw_call_fail;
  56. /**
  57. * do_fw_call_smc() - Call system-level platform management layer (SMC)
  58. * @arg0: Argument 0 to SMC call
  59. * @arg1: Argument 1 to SMC call
  60. * @arg2: Argument 2 to SMC call
  61. * @ret_payload: Returned value array
  62. *
  63. * Invoke platform management function via SMC call (no hypervisor present).
  64. *
  65. * Return: Returns status, either success or error+reason
  66. */
  67. static noinline int do_fw_call_smc(u64 arg0, u64 arg1, u64 arg2,
  68. u32 *ret_payload)
  69. {
  70. struct arm_smccc_res res;
  71. arm_smccc_smc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
  72. if (ret_payload) {
  73. ret_payload[0] = lower_32_bits(res.a0);
  74. ret_payload[1] = upper_32_bits(res.a0);
  75. ret_payload[2] = lower_32_bits(res.a1);
  76. ret_payload[3] = upper_32_bits(res.a1);
  77. }
  78. return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
  79. }
  80. /**
  81. * do_fw_call_hvc() - Call system-level platform management layer (HVC)
  82. * @arg0: Argument 0 to HVC call
  83. * @arg1: Argument 1 to HVC call
  84. * @arg2: Argument 2 to HVC call
  85. * @ret_payload: Returned value array
  86. *
  87. * Invoke platform management function via HVC
  88. * HVC-based for communication through hypervisor
  89. * (no direct communication with ATF).
  90. *
  91. * Return: Returns status, either success or error+reason
  92. */
  93. static noinline int do_fw_call_hvc(u64 arg0, u64 arg1, u64 arg2,
  94. u32 *ret_payload)
  95. {
  96. struct arm_smccc_res res;
  97. arm_smccc_hvc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
  98. if (ret_payload) {
  99. ret_payload[0] = lower_32_bits(res.a0);
  100. ret_payload[1] = upper_32_bits(res.a0);
  101. ret_payload[2] = lower_32_bits(res.a1);
  102. ret_payload[3] = upper_32_bits(res.a1);
  103. }
  104. return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
  105. }
  106. /**
  107. * zynqmp_pm_invoke_fn() - Invoke the system-level platform management layer
  108. * caller function depending on the configuration
  109. * @pm_api_id: Requested PM-API call
  110. * @arg0: Argument 0 to requested PM-API call
  111. * @arg1: Argument 1 to requested PM-API call
  112. * @arg2: Argument 2 to requested PM-API call
  113. * @arg3: Argument 3 to requested PM-API call
  114. * @ret_payload: Returned value array
  115. *
  116. * Invoke platform management function for SMC or HVC call, depending on
  117. * configuration.
  118. * Following SMC Calling Convention (SMCCC) for SMC64:
  119. * Pm Function Identifier,
  120. * PM_SIP_SVC + PM_API_ID =
  121. * ((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)
  122. * ((SMC_64) << FUNCID_CC_SHIFT)
  123. * ((SIP_START) << FUNCID_OEN_SHIFT)
  124. * ((PM_API_ID) & FUNCID_NUM_MASK))
  125. *
  126. * PM_SIP_SVC - Registered ZynqMP SIP Service Call.
  127. * PM_API_ID - Platform Management API ID.
  128. *
  129. * Return: Returns status, either success or error+reason
  130. */
  131. int zynqmp_pm_invoke_fn(u32 pm_api_id, u32 arg0, u32 arg1,
  132. u32 arg2, u32 arg3, u32 *ret_payload)
  133. {
  134. /*
  135. * Added SIP service call Function Identifier
  136. * Make sure to stay in x0 register
  137. */
  138. u64 smc_arg[4];
  139. smc_arg[0] = PM_SIP_SVC | pm_api_id;
  140. smc_arg[1] = ((u64)arg1 << 32) | arg0;
  141. smc_arg[2] = ((u64)arg3 << 32) | arg2;
  142. return do_fw_call(smc_arg[0], smc_arg[1], smc_arg[2], ret_payload);
  143. }
  144. static u32 pm_api_version;
  145. static u32 pm_tz_version;
  146. /**
  147. * zynqmp_pm_get_api_version() - Get version number of PMU PM firmware
  148. * @version: Returned version value
  149. *
  150. * Return: Returns status, either success or error+reason
  151. */
  152. static int zynqmp_pm_get_api_version(u32 *version)
  153. {
  154. u32 ret_payload[PAYLOAD_ARG_CNT];
  155. int ret;
  156. if (!version)
  157. return -EINVAL;
  158. /* Check is PM API version already verified */
  159. if (pm_api_version > 0) {
  160. *version = pm_api_version;
  161. return 0;
  162. }
  163. ret = zynqmp_pm_invoke_fn(PM_GET_API_VERSION, 0, 0, 0, 0, ret_payload);
  164. *version = ret_payload[1];
  165. return ret;
  166. }
  167. /**
  168. * zynqmp_pm_get_trustzone_version() - Get secure trustzone firmware version
  169. * @version: Returned version value
  170. *
  171. * Return: Returns status, either success or error+reason
  172. */
  173. static int zynqmp_pm_get_trustzone_version(u32 *version)
  174. {
  175. u32 ret_payload[PAYLOAD_ARG_CNT];
  176. int ret;
  177. if (!version)
  178. return -EINVAL;
  179. /* Check is PM trustzone version already verified */
  180. if (pm_tz_version > 0) {
  181. *version = pm_tz_version;
  182. return 0;
  183. }
  184. ret = zynqmp_pm_invoke_fn(PM_GET_TRUSTZONE_VERSION, 0, 0,
  185. 0, 0, ret_payload);
  186. *version = ret_payload[1];
  187. return ret;
  188. }
  189. /**
  190. * get_set_conduit_method() - Choose SMC or HVC based communication
  191. * @np: Pointer to the device_node structure
  192. *
  193. * Use SMC or HVC-based functions to communicate with EL2/EL3.
  194. *
  195. * Return: Returns 0 on success or error code
  196. */
  197. static int get_set_conduit_method(struct device_node *np)
  198. {
  199. const char *method;
  200. if (of_property_read_string(np, "method", &method)) {
  201. pr_warn("%s missing \"method\" property\n", __func__);
  202. return -ENXIO;
  203. }
  204. if (!strcmp("hvc", method)) {
  205. do_fw_call = do_fw_call_hvc;
  206. } else if (!strcmp("smc", method)) {
  207. do_fw_call = do_fw_call_smc;
  208. } else {
  209. pr_warn("%s Invalid \"method\" property: %s\n",
  210. __func__, method);
  211. return -EINVAL;
  212. }
  213. return 0;
  214. }
  215. /**
  216. * zynqmp_pm_query_data() - Get query data from firmware
  217. * @qdata: Variable to the zynqmp_pm_query_data structure
  218. * @out: Returned output value
  219. *
  220. * Return: Returns status, either success or error+reason
  221. */
  222. static int zynqmp_pm_query_data(struct zynqmp_pm_query_data qdata, u32 *out)
  223. {
  224. int ret;
  225. ret = zynqmp_pm_invoke_fn(PM_QUERY_DATA, qdata.qid, qdata.arg1,
  226. qdata.arg2, qdata.arg3, out);
  227. /*
  228. * For clock name query, all bytes in SMC response are clock name
  229. * characters and return code is always success. For invalid clocks,
  230. * clock name bytes would be zeros.
  231. */
  232. return qdata.qid == PM_QID_CLOCK_GET_NAME ? 0 : ret;
  233. }
  234. /**
  235. * zynqmp_pm_clock_enable() - Enable the clock for given id
  236. * @clock_id: ID of the clock to be enabled
  237. *
  238. * This function is used by master to enable the clock
  239. * including peripherals and PLL clocks.
  240. *
  241. * Return: Returns status, either success or error+reason
  242. */
  243. static int zynqmp_pm_clock_enable(u32 clock_id)
  244. {
  245. return zynqmp_pm_invoke_fn(PM_CLOCK_ENABLE, clock_id, 0, 0, 0, NULL);
  246. }
  247. /**
  248. * zynqmp_pm_clock_disable() - Disable the clock for given id
  249. * @clock_id: ID of the clock to be disable
  250. *
  251. * This function is used by master to disable the clock
  252. * including peripherals and PLL clocks.
  253. *
  254. * Return: Returns status, either success or error+reason
  255. */
  256. static int zynqmp_pm_clock_disable(u32 clock_id)
  257. {
  258. return zynqmp_pm_invoke_fn(PM_CLOCK_DISABLE, clock_id, 0, 0, 0, NULL);
  259. }
  260. /**
  261. * zynqmp_pm_clock_getstate() - Get the clock state for given id
  262. * @clock_id: ID of the clock to be queried
  263. * @state: 1/0 (Enabled/Disabled)
  264. *
  265. * This function is used by master to get the state of clock
  266. * including peripherals and PLL clocks.
  267. *
  268. * Return: Returns status, either success or error+reason
  269. */
  270. static int zynqmp_pm_clock_getstate(u32 clock_id, u32 *state)
  271. {
  272. u32 ret_payload[PAYLOAD_ARG_CNT];
  273. int ret;
  274. ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETSTATE, clock_id, 0,
  275. 0, 0, ret_payload);
  276. *state = ret_payload[1];
  277. return ret;
  278. }
  279. /**
  280. * zynqmp_pm_clock_setdivider() - Set the clock divider for given id
  281. * @clock_id: ID of the clock
  282. * @divider: divider value
  283. *
  284. * This function is used by master to set divider for any clock
  285. * to achieve desired rate.
  286. *
  287. * Return: Returns status, either success or error+reason
  288. */
  289. static int zynqmp_pm_clock_setdivider(u32 clock_id, u32 divider)
  290. {
  291. return zynqmp_pm_invoke_fn(PM_CLOCK_SETDIVIDER, clock_id, divider,
  292. 0, 0, NULL);
  293. }
  294. /**
  295. * zynqmp_pm_clock_getdivider() - Get the clock divider for given id
  296. * @clock_id: ID of the clock
  297. * @divider: divider value
  298. *
  299. * This function is used by master to get divider values
  300. * for any clock.
  301. *
  302. * Return: Returns status, either success or error+reason
  303. */
  304. static int zynqmp_pm_clock_getdivider(u32 clock_id, u32 *divider)
  305. {
  306. u32 ret_payload[PAYLOAD_ARG_CNT];
  307. int ret;
  308. ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETDIVIDER, clock_id, 0,
  309. 0, 0, ret_payload);
  310. *divider = ret_payload[1];
  311. return ret;
  312. }
  313. /**
  314. * zynqmp_pm_clock_setrate() - Set the clock rate for given id
  315. * @clock_id: ID of the clock
  316. * @rate: rate value in hz
  317. *
  318. * This function is used by master to set rate for any clock.
  319. *
  320. * Return: Returns status, either success or error+reason
  321. */
  322. static int zynqmp_pm_clock_setrate(u32 clock_id, u64 rate)
  323. {
  324. return zynqmp_pm_invoke_fn(PM_CLOCK_SETRATE, clock_id,
  325. lower_32_bits(rate),
  326. upper_32_bits(rate),
  327. 0, NULL);
  328. }
  329. /**
  330. * zynqmp_pm_clock_getrate() - Get the clock rate for given id
  331. * @clock_id: ID of the clock
  332. * @rate: rate value in hz
  333. *
  334. * This function is used by master to get rate
  335. * for any clock.
  336. *
  337. * Return: Returns status, either success or error+reason
  338. */
  339. static int zynqmp_pm_clock_getrate(u32 clock_id, u64 *rate)
  340. {
  341. u32 ret_payload[PAYLOAD_ARG_CNT];
  342. int ret;
  343. ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETRATE, clock_id, 0,
  344. 0, 0, ret_payload);
  345. *rate = ((u64)ret_payload[2] << 32) | ret_payload[1];
  346. return ret;
  347. }
  348. /**
  349. * zynqmp_pm_clock_setparent() - Set the clock parent for given id
  350. * @clock_id: ID of the clock
  351. * @parent_id: parent id
  352. *
  353. * This function is used by master to set parent for any clock.
  354. *
  355. * Return: Returns status, either success or error+reason
  356. */
  357. static int zynqmp_pm_clock_setparent(u32 clock_id, u32 parent_id)
  358. {
  359. return zynqmp_pm_invoke_fn(PM_CLOCK_SETPARENT, clock_id,
  360. parent_id, 0, 0, NULL);
  361. }
  362. /**
  363. * zynqmp_pm_clock_getparent() - Get the clock parent for given id
  364. * @clock_id: ID of the clock
  365. * @parent_id: parent id
  366. *
  367. * This function is used by master to get parent index
  368. * for any clock.
  369. *
  370. * Return: Returns status, either success or error+reason
  371. */
  372. static int zynqmp_pm_clock_getparent(u32 clock_id, u32 *parent_id)
  373. {
  374. u32 ret_payload[PAYLOAD_ARG_CNT];
  375. int ret;
  376. ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETPARENT, clock_id, 0,
  377. 0, 0, ret_payload);
  378. *parent_id = ret_payload[1];
  379. return ret;
  380. }
  381. /**
  382. * zynqmp_is_valid_ioctl() - Check whether IOCTL ID is valid or not
  383. * @ioctl_id: IOCTL ID
  384. *
  385. * Return: 1 if IOCTL is valid else 0
  386. */
  387. static inline int zynqmp_is_valid_ioctl(u32 ioctl_id)
  388. {
  389. switch (ioctl_id) {
  390. case IOCTL_SET_PLL_FRAC_MODE:
  391. case IOCTL_GET_PLL_FRAC_MODE:
  392. case IOCTL_SET_PLL_FRAC_DATA:
  393. case IOCTL_GET_PLL_FRAC_DATA:
  394. return 1;
  395. default:
  396. return 0;
  397. }
  398. }
  399. /**
  400. * zynqmp_pm_ioctl() - PM IOCTL API for device control and configs
  401. * @node_id: Node ID of the device
  402. * @ioctl_id: ID of the requested IOCTL
  403. * @arg1: Argument 1 to requested IOCTL call
  404. * @arg2: Argument 2 to requested IOCTL call
  405. * @out: Returned output value
  406. *
  407. * This function calls IOCTL to firmware for device control and configuration.
  408. *
  409. * Return: Returns status, either success or error+reason
  410. */
  411. static int zynqmp_pm_ioctl(u32 node_id, u32 ioctl_id, u32 arg1, u32 arg2,
  412. u32 *out)
  413. {
  414. if (!zynqmp_is_valid_ioctl(ioctl_id))
  415. return -EINVAL;
  416. return zynqmp_pm_invoke_fn(PM_IOCTL, node_id, ioctl_id,
  417. arg1, arg2, out);
  418. }
  419. static const struct zynqmp_eemi_ops eemi_ops = {
  420. .get_api_version = zynqmp_pm_get_api_version,
  421. .query_data = zynqmp_pm_query_data,
  422. .clock_enable = zynqmp_pm_clock_enable,
  423. .clock_disable = zynqmp_pm_clock_disable,
  424. .clock_getstate = zynqmp_pm_clock_getstate,
  425. .clock_setdivider = zynqmp_pm_clock_setdivider,
  426. .clock_getdivider = zynqmp_pm_clock_getdivider,
  427. .clock_setrate = zynqmp_pm_clock_setrate,
  428. .clock_getrate = zynqmp_pm_clock_getrate,
  429. .clock_setparent = zynqmp_pm_clock_setparent,
  430. .clock_getparent = zynqmp_pm_clock_getparent,
  431. .ioctl = zynqmp_pm_ioctl,
  432. };
  433. /**
  434. * zynqmp_pm_get_eemi_ops - Get eemi ops functions
  435. *
  436. * Return: Pointer of eemi_ops structure
  437. */
  438. const struct zynqmp_eemi_ops *zynqmp_pm_get_eemi_ops(void)
  439. {
  440. return &eemi_ops;
  441. }
  442. EXPORT_SYMBOL_GPL(zynqmp_pm_get_eemi_ops);
  443. static int zynqmp_firmware_probe(struct platform_device *pdev)
  444. {
  445. struct device *dev = &pdev->dev;
  446. struct device_node *np;
  447. int ret;
  448. np = of_find_compatible_node(NULL, NULL, "xlnx,zynqmp");
  449. if (!np)
  450. return 0;
  451. of_node_put(np);
  452. ret = get_set_conduit_method(dev->of_node);
  453. if (ret)
  454. return ret;
  455. /* Check PM API version number */
  456. zynqmp_pm_get_api_version(&pm_api_version);
  457. if (pm_api_version < ZYNQMP_PM_VERSION) {
  458. panic("%s Platform Management API version error. Expected: v%d.%d - Found: v%d.%d\n",
  459. __func__,
  460. ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR,
  461. pm_api_version >> 16, pm_api_version & 0xFFFF);
  462. }
  463. pr_info("%s Platform Management API v%d.%d\n", __func__,
  464. pm_api_version >> 16, pm_api_version & 0xFFFF);
  465. /* Check trustzone version number */
  466. ret = zynqmp_pm_get_trustzone_version(&pm_tz_version);
  467. if (ret)
  468. panic("Legacy trustzone found without version support\n");
  469. if (pm_tz_version < ZYNQMP_TZ_VERSION)
  470. panic("%s Trustzone version error. Expected: v%d.%d - Found: v%d.%d\n",
  471. __func__,
  472. ZYNQMP_TZ_VERSION_MAJOR, ZYNQMP_TZ_VERSION_MINOR,
  473. pm_tz_version >> 16, pm_tz_version & 0xFFFF);
  474. pr_info("%s Trustzone version v%d.%d\n", __func__,
  475. pm_tz_version >> 16, pm_tz_version & 0xFFFF);
  476. zynqmp_pm_api_debugfs_init();
  477. return of_platform_populate(dev->of_node, NULL, NULL, dev);
  478. }
  479. static int zynqmp_firmware_remove(struct platform_device *pdev)
  480. {
  481. zynqmp_pm_api_debugfs_exit();
  482. return 0;
  483. }
  484. static const struct of_device_id zynqmp_firmware_of_match[] = {
  485. {.compatible = "xlnx,zynqmp-firmware"},
  486. {},
  487. };
  488. MODULE_DEVICE_TABLE(of, zynqmp_firmware_of_match);
  489. static struct platform_driver zynqmp_firmware_driver = {
  490. .driver = {
  491. .name = "zynqmp_firmware",
  492. .of_match_table = zynqmp_firmware_of_match,
  493. },
  494. .probe = zynqmp_firmware_probe,
  495. .remove = zynqmp_firmware_remove,
  496. };
  497. module_platform_driver(zynqmp_firmware_driver);