uart.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * Marvell NFC-over-UART driver
  3. *
  4. * Copyright (C) 2015, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available on the worldwide web at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  12. *
  13. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  14. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  15. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  16. * this warranty disclaimer.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/of_gpio.h>
  21. #include <net/nfc/nci.h>
  22. #include <net/nfc/nci_core.h>
  23. #include "nfcmrvl.h"
  24. static unsigned int hci_muxed;
  25. static unsigned int flow_control;
  26. static unsigned int break_control;
  27. static unsigned int reset_n_io;
  28. /*
  29. ** NFCMRVL NCI OPS
  30. */
  31. static int nfcmrvl_uart_nci_open(struct nfcmrvl_private *priv)
  32. {
  33. return 0;
  34. }
  35. static int nfcmrvl_uart_nci_close(struct nfcmrvl_private *priv)
  36. {
  37. return 0;
  38. }
  39. static int nfcmrvl_uart_nci_send(struct nfcmrvl_private *priv,
  40. struct sk_buff *skb)
  41. {
  42. struct nci_uart *nu = priv->drv_data;
  43. return nu->ops.send(nu, skb);
  44. }
  45. static void nfcmrvl_uart_nci_update_config(struct nfcmrvl_private *priv,
  46. const void *param)
  47. {
  48. struct nci_uart *nu = priv->drv_data;
  49. const struct nfcmrvl_fw_uart_config *config = param;
  50. nci_uart_set_config(nu, le32_to_cpu(config->baudrate),
  51. config->flow_control);
  52. }
  53. static struct nfcmrvl_if_ops uart_ops = {
  54. .nci_open = nfcmrvl_uart_nci_open,
  55. .nci_close = nfcmrvl_uart_nci_close,
  56. .nci_send = nfcmrvl_uart_nci_send,
  57. .nci_update_config = nfcmrvl_uart_nci_update_config
  58. };
  59. static int nfcmrvl_uart_parse_dt(struct device_node *node,
  60. struct nfcmrvl_platform_data *pdata)
  61. {
  62. struct device_node *matched_node;
  63. int ret;
  64. matched_node = of_find_compatible_node(node, NULL, "marvell,nfc-uart");
  65. if (!matched_node) {
  66. matched_node = of_find_compatible_node(node, NULL,
  67. "mrvl,nfc-uart");
  68. if (!matched_node)
  69. return -ENODEV;
  70. }
  71. ret = nfcmrvl_parse_dt(matched_node, pdata);
  72. if (ret < 0) {
  73. pr_err("Failed to get generic entries\n");
  74. return ret;
  75. }
  76. if (of_find_property(matched_node, "flow-control", NULL))
  77. pdata->flow_control = 1;
  78. else
  79. pdata->flow_control = 0;
  80. if (of_find_property(matched_node, "break-control", NULL))
  81. pdata->break_control = 1;
  82. else
  83. pdata->break_control = 0;
  84. return 0;
  85. }
  86. /*
  87. ** NCI UART OPS
  88. */
  89. static int nfcmrvl_nci_uart_open(struct nci_uart *nu)
  90. {
  91. struct nfcmrvl_private *priv;
  92. struct nfcmrvl_platform_data *pdata = NULL;
  93. struct nfcmrvl_platform_data config;
  94. struct device *dev = nu->tty->dev;
  95. /*
  96. * Platform data cannot be used here since usually it is already used
  97. * by low level serial driver. We can try to retrieve serial device
  98. * and check if DT entries were added.
  99. */
  100. if (dev && dev->parent && dev->parent->of_node)
  101. if (nfcmrvl_uart_parse_dt(dev->parent->of_node, &config) == 0)
  102. pdata = &config;
  103. if (!pdata) {
  104. pr_info("No platform data / DT -> fallback to module params\n");
  105. config.hci_muxed = hci_muxed;
  106. config.reset_n_io = reset_n_io;
  107. config.flow_control = flow_control;
  108. config.break_control = break_control;
  109. pdata = &config;
  110. }
  111. priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_UART, nu, &uart_ops,
  112. dev, pdata);
  113. if (IS_ERR(priv))
  114. return PTR_ERR(priv);
  115. priv->support_fw_dnld = true;
  116. nu->drv_data = priv;
  117. nu->ndev = priv->ndev;
  118. return 0;
  119. }
  120. static void nfcmrvl_nci_uart_close(struct nci_uart *nu)
  121. {
  122. nfcmrvl_nci_unregister_dev((struct nfcmrvl_private *)nu->drv_data);
  123. }
  124. static int nfcmrvl_nci_uart_recv(struct nci_uart *nu, struct sk_buff *skb)
  125. {
  126. return nfcmrvl_nci_recv_frame((struct nfcmrvl_private *)nu->drv_data,
  127. skb);
  128. }
  129. static void nfcmrvl_nci_uart_tx_start(struct nci_uart *nu)
  130. {
  131. struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
  132. if (priv->ndev->nfc_dev->fw_download_in_progress)
  133. return;
  134. /* Remove BREAK to wake up the NFCC */
  135. if (priv->config.break_control && nu->tty->ops->break_ctl) {
  136. nu->tty->ops->break_ctl(nu->tty, 0);
  137. usleep_range(3000, 5000);
  138. }
  139. }
  140. static void nfcmrvl_nci_uart_tx_done(struct nci_uart *nu)
  141. {
  142. struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
  143. if (priv->ndev->nfc_dev->fw_download_in_progress)
  144. return;
  145. /*
  146. ** To ensure that if the NFCC goes in DEEP SLEEP sate we can wake him
  147. ** up. we set BREAK. Once we will be ready to send again we will remove
  148. ** it.
  149. */
  150. if (priv->config.break_control && nu->tty->ops->break_ctl) {
  151. nu->tty->ops->break_ctl(nu->tty, -1);
  152. usleep_range(1000, 3000);
  153. }
  154. }
  155. static struct nci_uart nfcmrvl_nci_uart = {
  156. .owner = THIS_MODULE,
  157. .name = "nfcmrvl_uart",
  158. .driver = NCI_UART_DRIVER_MARVELL,
  159. .ops = {
  160. .open = nfcmrvl_nci_uart_open,
  161. .close = nfcmrvl_nci_uart_close,
  162. .recv = nfcmrvl_nci_uart_recv,
  163. .tx_start = nfcmrvl_nci_uart_tx_start,
  164. .tx_done = nfcmrvl_nci_uart_tx_done,
  165. }
  166. };
  167. /*
  168. ** Module init
  169. */
  170. static int nfcmrvl_uart_init_module(void)
  171. {
  172. return nci_uart_register(&nfcmrvl_nci_uart);
  173. }
  174. static void nfcmrvl_uart_exit_module(void)
  175. {
  176. nci_uart_unregister(&nfcmrvl_nci_uart);
  177. }
  178. module_init(nfcmrvl_uart_init_module);
  179. module_exit(nfcmrvl_uart_exit_module);
  180. MODULE_AUTHOR("Marvell International Ltd.");
  181. MODULE_DESCRIPTION("Marvell NFC-over-UART");
  182. MODULE_LICENSE("GPL v2");
  183. module_param(flow_control, uint, 0);
  184. MODULE_PARM_DESC(flow_control, "Tell if UART needs flow control at init.");
  185. module_param(break_control, uint, 0);
  186. MODULE_PARM_DESC(break_control, "Tell if UART driver must drive break signal.");
  187. module_param(hci_muxed, uint, 0);
  188. MODULE_PARM_DESC(hci_muxed, "Tell if transport is muxed in HCI one.");
  189. module_param(reset_n_io, uint, 0);
  190. MODULE_PARM_DESC(reset_n_io, "GPIO that is wired to RESET_N signal.");