imx-scu-soc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. static const char *imx_scu_soc_name(u32 id)
  67. {
  68. switch (id) {
  69. case 0x1:
  70. return "i.MX8QM";
  71. case 0x2:
  72. return "i.MX8QXP";
  73. case 0xe:
  74. return "i.MX8DXL";
  75. default:
  76. break;
  77. }
  78. return "NULL";
  79. }
  80. int imx_scu_soc_init(struct device *dev)
  81. {
  82. struct soc_device_attribute *soc_dev_attr;
  83. struct soc_device *soc_dev;
  84. int id, ret;
  85. u64 uid = 0;
  86. u32 val;
  87. ret = imx_scu_get_handle(&imx_sc_soc_ipc_handle);
  88. if (ret)
  89. return ret;
  90. soc_dev_attr = devm_kzalloc(dev, sizeof(*soc_dev_attr),
  91. GFP_KERNEL);
  92. if (!soc_dev_attr)
  93. return -ENOMEM;
  94. soc_dev_attr->family = "Freescale i.MX";
  95. ret = of_property_read_string(of_root,
  96. "model",
  97. &soc_dev_attr->machine);
  98. if (ret)
  99. return ret;
  100. id = imx_scu_soc_id();
  101. if (id < 0)
  102. return -EINVAL;
  103. ret = imx_scu_soc_uid(&uid);
  104. if (ret < 0)
  105. return -EINVAL;
  106. /* format soc_id value passed from SCU firmware */
  107. val = id & 0x1f;
  108. soc_dev_attr->soc_id = imx_scu_soc_name(val);
  109. /* format revision value passed from SCU firmware */
  110. val = (id >> 5) & 0xf;
  111. val = (((val >> 2) + 1) << 4) | (val & 0x3);
  112. soc_dev_attr->revision = devm_kasprintf(dev, GFP_KERNEL, "%d.%d",
  113. (val >> 4) & 0xf, val & 0xf);
  114. if (!soc_dev_attr->revision)
  115. return -ENOMEM;
  116. soc_dev_attr->serial_number = devm_kasprintf(dev, GFP_KERNEL,
  117. "%016llX", uid);
  118. if (!soc_dev_attr->serial_number)
  119. return -ENOMEM;
  120. soc_dev = soc_device_register(soc_dev_attr);
  121. if (IS_ERR(soc_dev))
  122. return PTR_ERR(soc_dev);
  123. return 0;
  124. }