nci.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * NCI based driver for Samsung S3FWRN5 NFC chip
  3. *
  4. * Copyright (C) 2015 Samsung Electrnoics
  5. * Robert Baldyga <r.baldyga@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2 or later, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/completion.h>
  20. #include <linux/firmware.h>
  21. #include "s3fwrn5.h"
  22. #include "nci.h"
  23. static int s3fwrn5_nci_prop_rsp(struct nci_dev *ndev, struct sk_buff *skb)
  24. {
  25. __u8 status = skb->data[0];
  26. nci_req_complete(ndev, status);
  27. return 0;
  28. }
  29. static struct nci_driver_ops s3fwrn5_nci_prop_ops[] = {
  30. {
  31. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  32. NCI_PROP_AGAIN),
  33. .rsp = s3fwrn5_nci_prop_rsp,
  34. },
  35. {
  36. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  37. NCI_PROP_GET_RFREG),
  38. .rsp = s3fwrn5_nci_prop_rsp,
  39. },
  40. {
  41. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  42. NCI_PROP_SET_RFREG),
  43. .rsp = s3fwrn5_nci_prop_rsp,
  44. },
  45. {
  46. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  47. NCI_PROP_GET_RFREG_VER),
  48. .rsp = s3fwrn5_nci_prop_rsp,
  49. },
  50. {
  51. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  52. NCI_PROP_SET_RFREG_VER),
  53. .rsp = s3fwrn5_nci_prop_rsp,
  54. },
  55. {
  56. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  57. NCI_PROP_START_RFREG),
  58. .rsp = s3fwrn5_nci_prop_rsp,
  59. },
  60. {
  61. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  62. NCI_PROP_STOP_RFREG),
  63. .rsp = s3fwrn5_nci_prop_rsp,
  64. },
  65. {
  66. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  67. NCI_PROP_FW_CFG),
  68. .rsp = s3fwrn5_nci_prop_rsp,
  69. },
  70. {
  71. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  72. NCI_PROP_WR_RESET),
  73. .rsp = s3fwrn5_nci_prop_rsp,
  74. },
  75. };
  76. void s3fwrn5_nci_get_prop_ops(struct nci_driver_ops **ops, size_t *n)
  77. {
  78. *ops = s3fwrn5_nci_prop_ops;
  79. *n = ARRAY_SIZE(s3fwrn5_nci_prop_ops);
  80. }
  81. #define S3FWRN5_RFREG_SECTION_SIZE 252
  82. int s3fwrn5_nci_rf_configure(struct s3fwrn5_info *info, const char *fw_name)
  83. {
  84. const struct firmware *fw;
  85. struct nci_prop_fw_cfg_cmd fw_cfg;
  86. struct nci_prop_set_rfreg_cmd set_rfreg;
  87. struct nci_prop_stop_rfreg_cmd stop_rfreg;
  88. u32 checksum;
  89. int i, len;
  90. int ret;
  91. ret = request_firmware(&fw, fw_name, &info->ndev->nfc_dev->dev);
  92. if (ret < 0)
  93. return ret;
  94. /* Compute rfreg checksum */
  95. checksum = 0;
  96. for (i = 0; i < fw->size; i += 4)
  97. checksum += *((u32 *)(fw->data+i));
  98. /* Set default clock configuration for external crystal */
  99. fw_cfg.clk_type = 0x01;
  100. fw_cfg.clk_speed = 0xff;
  101. fw_cfg.clk_req = 0xff;
  102. ret = nci_prop_cmd(info->ndev, NCI_PROP_FW_CFG,
  103. sizeof(fw_cfg), (__u8 *)&fw_cfg);
  104. if (ret < 0)
  105. goto out;
  106. /* Start rfreg configuration */
  107. dev_info(&info->ndev->nfc_dev->dev,
  108. "rfreg configuration update: %s\n", fw_name);
  109. ret = nci_prop_cmd(info->ndev, NCI_PROP_START_RFREG, 0, NULL);
  110. if (ret < 0) {
  111. dev_err(&info->ndev->nfc_dev->dev,
  112. "Unable to start rfreg update\n");
  113. goto out;
  114. }
  115. /* Update rfreg */
  116. set_rfreg.index = 0;
  117. for (i = 0; i < fw->size; i += S3FWRN5_RFREG_SECTION_SIZE) {
  118. len = (fw->size - i < S3FWRN5_RFREG_SECTION_SIZE) ?
  119. (fw->size - i) : S3FWRN5_RFREG_SECTION_SIZE;
  120. memcpy(set_rfreg.data, fw->data+i, len);
  121. ret = nci_prop_cmd(info->ndev, NCI_PROP_SET_RFREG,
  122. len+1, (__u8 *)&set_rfreg);
  123. if (ret < 0) {
  124. dev_err(&info->ndev->nfc_dev->dev,
  125. "rfreg update error (code=%d)\n", ret);
  126. goto out;
  127. }
  128. set_rfreg.index++;
  129. }
  130. /* Finish rfreg configuration */
  131. stop_rfreg.checksum = checksum & 0xffff;
  132. ret = nci_prop_cmd(info->ndev, NCI_PROP_STOP_RFREG,
  133. sizeof(stop_rfreg), (__u8 *)&stop_rfreg);
  134. if (ret < 0) {
  135. dev_err(&info->ndev->nfc_dev->dev,
  136. "Unable to stop rfreg update\n");
  137. goto out;
  138. }
  139. dev_info(&info->ndev->nfc_dev->dev,
  140. "rfreg configuration update: success\n");
  141. out:
  142. release_firmware(fw);
  143. return ret;
  144. }