imx-scu-soc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 NXP.
  4. */
  5. #include <dt-bindings/firmware/imx/rsrc.h>
  6. #include <linux/firmware/imx/sci.h>
  7. #include <linux/slab.h>
  8. #include <linux/sys_soc.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/of.h>
  11. static struct imx_sc_ipc *imx_sc_soc_ipc_handle;
  12. struct imx_sc_msg_misc_get_soc_id {
  13. struct imx_sc_rpc_msg hdr;
  14. union {
  15. struct {
  16. u32 control;
  17. u16 resource;
  18. } __packed req;
  19. struct {
  20. u32 id;
  21. } resp;
  22. } data;
  23. } __packed __aligned(4);
  24. struct imx_sc_msg_misc_get_soc_uid {
  25. struct imx_sc_rpc_msg hdr;
  26. u32 uid_low;
  27. u32 uid_high;
  28. } __packed;
  29. static int imx_scu_soc_uid(u64 *soc_uid)
  30. {
  31. struct imx_sc_msg_misc_get_soc_uid msg;
  32. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  33. int ret;
  34. hdr->ver = IMX_SC_RPC_VERSION;
  35. hdr->svc = IMX_SC_RPC_SVC_MISC;
  36. hdr->func = IMX_SC_MISC_FUNC_UNIQUE_ID;
  37. hdr->size = 1;
  38. ret = imx_scu_call_rpc(imx_sc_soc_ipc_handle, &msg, true);
  39. if (ret) {
  40. pr_err("%s: get soc uid failed, ret %d\n", __func__, ret);
  41. return ret;
  42. }
  43. *soc_uid = msg.uid_high;
  44. *soc_uid <<= 32;
  45. *soc_uid |= msg.uid_low;
  46. return 0;
  47. }
  48. static int imx_scu_soc_id(void)
  49. {
  50. struct imx_sc_msg_misc_get_soc_id msg;
  51. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  52. int ret;
  53. hdr->ver = IMX_SC_RPC_VERSION;
  54. hdr->svc = IMX_SC_RPC_SVC_MISC;
  55. hdr->func = IMX_SC_MISC_FUNC_GET_CONTROL;
  56. hdr->size = 3;
  57. msg.data.req.control = IMX_SC_C_ID;
  58. msg.data.req.resource = IMX_SC_R_SYSTEM;
  59. ret = imx_scu_call_rpc(imx_sc_soc_ipc_handle, &msg, true);
  60. if (ret) {
  61. pr_err("%s: get soc info failed, ret %d\n", __func__, ret);
  62. return ret;
  63. }
  64. return msg.data.resp.id;
  65. }
  66. int imx_scu_soc_init(struct device *dev)
  67. {
  68. struct soc_device_attribute *soc_dev_attr;
  69. struct soc_device *soc_dev;
  70. int id, ret;
  71. u64 uid = 0;
  72. u32 val;
  73. ret = imx_scu_get_handle(&imx_sc_soc_ipc_handle);
  74. if (ret)
  75. return ret;
  76. soc_dev_attr = devm_kzalloc(dev, sizeof(*soc_dev_attr),
  77. GFP_KERNEL);
  78. if (!soc_dev_attr)
  79. return -ENOMEM;
  80. soc_dev_attr->family = "Freescale i.MX";
  81. ret = of_property_read_string(of_root,
  82. "model",
  83. &soc_dev_attr->machine);
  84. if (ret)
  85. return ret;
  86. id = imx_scu_soc_id();
  87. if (id < 0)
  88. return -EINVAL;
  89. ret = imx_scu_soc_uid(&uid);
  90. if (ret < 0)
  91. return -EINVAL;
  92. /* format soc_id value passed from SCU firmware */
  93. val = id & 0x1f;
  94. soc_dev_attr->soc_id = devm_kasprintf(dev, GFP_KERNEL, "0x%x", val);
  95. if (!soc_dev_attr->soc_id)
  96. return -ENOMEM;
  97. /* format revision value passed from SCU firmware */
  98. val = (id >> 5) & 0xf;
  99. val = (((val >> 2) + 1) << 4) | (val & 0x3);
  100. soc_dev_attr->revision = devm_kasprintf(dev, GFP_KERNEL, "%d.%d",
  101. (val >> 4) & 0xf, val & 0xf);
  102. if (!soc_dev_attr->revision)
  103. return -ENOMEM;
  104. soc_dev_attr->serial_number = devm_kasprintf(dev, GFP_KERNEL,
  105. "%016llX", uid);
  106. if (!soc_dev_attr->serial_number)
  107. return -ENOMEM;
  108. soc_dev = soc_device_register(soc_dev_attr);
  109. if (IS_ERR(soc_dev))
  110. return PTR_ERR(soc_dev);
  111. return 0;
  112. }