mox_sp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Marek Behun <marek.behun@nic.cz>
  4. */
  5. #include <common.h>
  6. #include <asm/arch/soc.h>
  7. #include <asm/io.h>
  8. #include <linux/bitops.h>
  9. #include <linux/delay.h>
  10. #define RWTM_BASE (MVEBU_REGISTER(0xb0000))
  11. #define RWTM_CMD_PARAM(i) (size_t)(RWTM_BASE + (i) * 4)
  12. #define RWTM_CMD (RWTM_BASE + 0x40)
  13. #define RWTM_CMD_RETSTATUS (RWTM_BASE + 0x80)
  14. #define RWTM_CMD_STATUS(i) (size_t)(RWTM_BASE + 0x84 + (i) * 4)
  15. #define RWTM_HOST_INT_RESET (RWTM_BASE + 0xc8)
  16. #define RWTM_HOST_INT_MASK (RWTM_BASE + 0xcc)
  17. #define SP_CMD_COMPLETE BIT(0)
  18. #define MBOX_STS_SUCCESS (0x0 << 30)
  19. #define MBOX_STS_FAIL (0x1 << 30)
  20. #define MBOX_STS_BADCMD (0x2 << 30)
  21. #define MBOX_STS_LATER (0x3 << 30)
  22. #define MBOX_STS_ERROR(s) ((s) & (3 << 30))
  23. #define MBOX_STS_VALUE(s) (((s) >> 10) & 0xfffff)
  24. #define MBOX_STS_CMD(s) ((s) & 0x3ff)
  25. enum mbox_cmd {
  26. MBOX_CMD_GET_RANDOM = 1,
  27. MBOX_CMD_BOARD_INFO,
  28. MBOX_CMD_ECDSA_PUB_KEY,
  29. MBOX_CMD_HASH,
  30. MBOX_CMD_SIGN,
  31. MBOX_CMD_VERIFY,
  32. MBOX_CMD_OTP_READ,
  33. MBOX_CMD_OTP_WRITE
  34. };
  35. static int mbox_do_cmd(enum mbox_cmd cmd, u32 *out, int nout)
  36. {
  37. const int tries = 50;
  38. int i;
  39. u32 status;
  40. clrbits_le32(RWTM_HOST_INT_MASK, SP_CMD_COMPLETE);
  41. writel(cmd, RWTM_CMD);
  42. for (i = 0; i < tries; ++i) {
  43. mdelay(10);
  44. if (readl(RWTM_HOST_INT_RESET) & SP_CMD_COMPLETE)
  45. break;
  46. }
  47. if (i == tries) {
  48. /* if timed out, don't read status */
  49. setbits_le32(RWTM_HOST_INT_RESET, SP_CMD_COMPLETE);
  50. return -ETIMEDOUT;
  51. }
  52. for (i = 0; i < nout; ++i)
  53. out[i] = readl(RWTM_CMD_STATUS(i));
  54. status = readl(RWTM_CMD_RETSTATUS);
  55. setbits_le32(RWTM_HOST_INT_RESET, SP_CMD_COMPLETE);
  56. if (MBOX_STS_CMD(status) != cmd)
  57. return -EIO;
  58. else if (MBOX_STS_ERROR(status) == MBOX_STS_FAIL)
  59. return -(int)MBOX_STS_VALUE(status);
  60. else if (MBOX_STS_ERROR(status) != MBOX_STS_SUCCESS)
  61. return -EIO;
  62. else
  63. return MBOX_STS_VALUE(status);
  64. }
  65. const char *mox_sp_get_ecdsa_public_key(void)
  66. {
  67. static char public_key[135];
  68. u32 out[16];
  69. int res;
  70. if (public_key[0])
  71. return public_key;
  72. res = mbox_do_cmd(MBOX_CMD_ECDSA_PUB_KEY, out, 16);
  73. if (res < 0)
  74. return NULL;
  75. sprintf(public_key,
  76. "%06x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
  77. (u32)res, out[0], out[1], out[2], out[3], out[4], out[5],
  78. out[6], out[7], out[8], out[9], out[10], out[11], out[12],
  79. out[13], out[14], out[15]);
  80. return public_key;
  81. }
  82. static inline void res_to_mac(u8 *mac, u32 t1, u32 t2)
  83. {
  84. mac[0] = t1 >> 8;
  85. mac[1] = t1;
  86. mac[2] = t2 >> 24;
  87. mac[3] = t2 >> 16;
  88. mac[4] = t2 >> 8;
  89. mac[5] = t2;
  90. }
  91. int mbox_sp_get_board_info(u64 *sn, u8 *mac1, u8 *mac2, int *bv, int *ram)
  92. {
  93. u32 out[8];
  94. int res;
  95. res = mbox_do_cmd(MBOX_CMD_BOARD_INFO, out, 8);
  96. if (res < 0)
  97. return res;
  98. if (sn) {
  99. *sn = out[1];
  100. *sn <<= 32;
  101. *sn |= out[0];
  102. }
  103. if (bv)
  104. *bv = out[2];
  105. if (ram)
  106. *ram = out[3];
  107. if (mac1)
  108. res_to_mac(mac1, out[4], out[5]);
  109. if (mac2)
  110. res_to_mac(mac2, out[6], out[7]);
  111. return 0;
  112. }