uart.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_get_compatible_child(node, "marvell,nfc-uart");
  65. if (!matched_node) {
  66. matched_node = of_get_compatible_child(node, "mrvl,nfc-uart");
  67. if (!matched_node)
  68. return -ENODEV;
  69. }
  70. ret = nfcmrvl_parse_dt(matched_node, pdata);
  71. if (ret < 0) {
  72. pr_err("Failed to get generic entries\n");
  73. return ret;
  74. }
  75. if (of_find_property(matched_node, "flow-control", NULL))
  76. pdata->flow_control = 1;
  77. else
  78. pdata->flow_control = 0;
  79. if (of_find_property(matched_node, "break-control", NULL))
  80. pdata->break_control = 1;
  81. else
  82. pdata->break_control = 0;
  83. return 0;
  84. }
  85. /*
  86. ** NCI UART OPS
  87. */
  88. static int nfcmrvl_nci_uart_open(struct nci_uart *nu)
  89. {
  90. struct nfcmrvl_private *priv;
  91. struct nfcmrvl_platform_data *pdata = NULL;
  92. struct nfcmrvl_platform_data config;
  93. struct device *dev = nu->tty->dev;
  94. /*
  95. * Platform data cannot be used here since usually it is already used
  96. * by low level serial driver. We can try to retrieve serial device
  97. * and check if DT entries were added.
  98. */
  99. if (dev && dev->parent && dev->parent->of_node)
  100. if (nfcmrvl_uart_parse_dt(dev->parent->of_node, &config) == 0)
  101. pdata = &config;
  102. if (!pdata) {
  103. pr_info("No platform data / DT -> fallback to module params\n");
  104. config.hci_muxed = hci_muxed;
  105. config.reset_n_io = reset_n_io;
  106. config.flow_control = flow_control;
  107. config.break_control = break_control;
  108. pdata = &config;
  109. }
  110. priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_UART, nu, &uart_ops,
  111. dev, pdata);
  112. if (IS_ERR(priv))
  113. return PTR_ERR(priv);
  114. priv->support_fw_dnld = true;
  115. nu->drv_data = priv;
  116. nu->ndev = priv->ndev;
  117. return 0;
  118. }
  119. static void nfcmrvl_nci_uart_close(struct nci_uart *nu)
  120. {
  121. nfcmrvl_nci_unregister_dev((struct nfcmrvl_private *)nu->drv_data);
  122. }
  123. static int nfcmrvl_nci_uart_recv(struct nci_uart *nu, struct sk_buff *skb)
  124. {
  125. return nfcmrvl_nci_recv_frame((struct nfcmrvl_private *)nu->drv_data,
  126. skb);
  127. }
  128. static void nfcmrvl_nci_uart_tx_start(struct nci_uart *nu)
  129. {
  130. struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
  131. if (priv->ndev->nfc_dev->fw_download_in_progress)
  132. return;
  133. /* Remove BREAK to wake up the NFCC */
  134. if (priv->config.break_control && nu->tty->ops->break_ctl) {
  135. nu->tty->ops->break_ctl(nu->tty, 0);
  136. usleep_range(3000, 5000);
  137. }
  138. }
  139. static void nfcmrvl_nci_uart_tx_done(struct nci_uart *nu)
  140. {
  141. struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
  142. if (priv->ndev->nfc_dev->fw_download_in_progress)
  143. return;
  144. /*
  145. ** To ensure that if the NFCC goes in DEEP SLEEP sate we can wake him
  146. ** up. we set BREAK. Once we will be ready to send again we will remove
  147. ** it.
  148. */
  149. if (priv->config.break_control && nu->tty->ops->break_ctl) {
  150. nu->tty->ops->break_ctl(nu->tty, -1);
  151. usleep_range(1000, 3000);
  152. }
  153. }
  154. static struct nci_uart nfcmrvl_nci_uart = {
  155. .owner = THIS_MODULE,
  156. .name = "nfcmrvl_uart",
  157. .driver = NCI_UART_DRIVER_MARVELL,
  158. .ops = {
  159. .open = nfcmrvl_nci_uart_open,
  160. .close = nfcmrvl_nci_uart_close,
  161. .recv = nfcmrvl_nci_uart_recv,
  162. .tx_start = nfcmrvl_nci_uart_tx_start,
  163. .tx_done = nfcmrvl_nci_uart_tx_done,
  164. }
  165. };
  166. /*
  167. ** Module init
  168. */
  169. static int nfcmrvl_uart_init_module(void)
  170. {
  171. return nci_uart_register(&nfcmrvl_nci_uart);
  172. }
  173. static void nfcmrvl_uart_exit_module(void)
  174. {
  175. nci_uart_unregister(&nfcmrvl_nci_uart);
  176. }
  177. module_init(nfcmrvl_uart_init_module);
  178. module_exit(nfcmrvl_uart_exit_module);
  179. MODULE_AUTHOR("Marvell International Ltd.");
  180. MODULE_DESCRIPTION("Marvell NFC-over-UART");
  181. MODULE_LICENSE("GPL v2");
  182. module_param(flow_control, uint, 0);
  183. MODULE_PARM_DESC(flow_control, "Tell if UART needs flow control at init.");
  184. module_param(break_control, uint, 0);
  185. MODULE_PARM_DESC(break_control, "Tell if UART driver must drive break signal.");
  186. module_param(hci_muxed, uint, 0);
  187. MODULE_PARM_DESC(hci_muxed, "Tell if transport is muxed in HCI one.");
  188. module_param(reset_n_io, uint, 0);
  189. MODULE_PARM_DESC(reset_n_io, "GPIO that is wired to RESET_N signal.");