uart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Marvell NFC-over-UART driver
  4. *
  5. * Copyright (C) 2015, Marvell International Ltd.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/delay.h>
  9. #include <linux/of_gpio.h>
  10. #include <net/nfc/nci.h>
  11. #include <net/nfc/nci_core.h>
  12. #include "nfcmrvl.h"
  13. static unsigned int hci_muxed;
  14. static unsigned int flow_control;
  15. static unsigned int break_control;
  16. static int reset_n_io = -EINVAL;
  17. /*
  18. * NFCMRVL NCI OPS
  19. */
  20. static int nfcmrvl_uart_nci_open(struct nfcmrvl_private *priv)
  21. {
  22. return 0;
  23. }
  24. static int nfcmrvl_uart_nci_close(struct nfcmrvl_private *priv)
  25. {
  26. return 0;
  27. }
  28. static int nfcmrvl_uart_nci_send(struct nfcmrvl_private *priv,
  29. struct sk_buff *skb)
  30. {
  31. struct nci_uart *nu = priv->drv_data;
  32. return nu->ops.send(nu, skb);
  33. }
  34. static void nfcmrvl_uart_nci_update_config(struct nfcmrvl_private *priv,
  35. const void *param)
  36. {
  37. struct nci_uart *nu = priv->drv_data;
  38. const struct nfcmrvl_fw_uart_config *config = param;
  39. nci_uart_set_config(nu, le32_to_cpu(config->baudrate),
  40. config->flow_control);
  41. }
  42. static const struct nfcmrvl_if_ops uart_ops = {
  43. .nci_open = nfcmrvl_uart_nci_open,
  44. .nci_close = nfcmrvl_uart_nci_close,
  45. .nci_send = nfcmrvl_uart_nci_send,
  46. .nci_update_config = nfcmrvl_uart_nci_update_config
  47. };
  48. static int nfcmrvl_uart_parse_dt(struct device_node *node,
  49. struct nfcmrvl_platform_data *pdata)
  50. {
  51. struct device_node *matched_node;
  52. int ret;
  53. matched_node = of_get_compatible_child(node, "marvell,nfc-uart");
  54. if (!matched_node) {
  55. matched_node = of_get_compatible_child(node, "mrvl,nfc-uart");
  56. if (!matched_node)
  57. return -ENODEV;
  58. }
  59. ret = nfcmrvl_parse_dt(matched_node, pdata);
  60. if (ret < 0) {
  61. pr_err("Failed to get generic entries\n");
  62. of_node_put(matched_node);
  63. return ret;
  64. }
  65. if (of_find_property(matched_node, "flow-control", NULL))
  66. pdata->flow_control = 1;
  67. else
  68. pdata->flow_control = 0;
  69. if (of_find_property(matched_node, "break-control", NULL))
  70. pdata->break_control = 1;
  71. else
  72. pdata->break_control = 0;
  73. of_node_put(matched_node);
  74. return 0;
  75. }
  76. /*
  77. * NCI UART OPS
  78. */
  79. static int nfcmrvl_nci_uart_open(struct nci_uart *nu)
  80. {
  81. struct nfcmrvl_private *priv;
  82. struct nfcmrvl_platform_data config;
  83. const struct nfcmrvl_platform_data *pdata = NULL;
  84. struct device *dev = nu->tty->dev;
  85. /*
  86. * Platform data cannot be used here since usually it is already used
  87. * by low level serial driver. We can try to retrieve serial device
  88. * and check if DT entries were added.
  89. */
  90. if (dev && dev->parent && dev->parent->of_node)
  91. if (nfcmrvl_uart_parse_dt(dev->parent->of_node, &config) == 0)
  92. pdata = &config;
  93. if (!pdata) {
  94. pr_info("No platform data / DT -> fallback to module params\n");
  95. config.hci_muxed = hci_muxed;
  96. config.reset_n_io = reset_n_io;
  97. config.flow_control = flow_control;
  98. config.break_control = break_control;
  99. pdata = &config;
  100. }
  101. priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_UART, nu, &uart_ops,
  102. dev, pdata);
  103. if (IS_ERR(priv))
  104. return PTR_ERR(priv);
  105. priv->support_fw_dnld = true;
  106. nu->drv_data = priv;
  107. nu->ndev = priv->ndev;
  108. return 0;
  109. }
  110. static void nfcmrvl_nci_uart_close(struct nci_uart *nu)
  111. {
  112. nfcmrvl_nci_unregister_dev((struct nfcmrvl_private *)nu->drv_data);
  113. }
  114. static int nfcmrvl_nci_uart_recv(struct nci_uart *nu, struct sk_buff *skb)
  115. {
  116. return nfcmrvl_nci_recv_frame((struct nfcmrvl_private *)nu->drv_data,
  117. skb);
  118. }
  119. static void nfcmrvl_nci_uart_tx_start(struct nci_uart *nu)
  120. {
  121. struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
  122. if (priv->ndev->nfc_dev->fw_download_in_progress)
  123. return;
  124. /* Remove BREAK to wake up the NFCC */
  125. if (priv->config.break_control && nu->tty->ops->break_ctl) {
  126. nu->tty->ops->break_ctl(nu->tty, 0);
  127. usleep_range(3000, 5000);
  128. }
  129. }
  130. static void nfcmrvl_nci_uart_tx_done(struct nci_uart *nu)
  131. {
  132. struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
  133. if (priv->ndev->nfc_dev->fw_download_in_progress)
  134. return;
  135. /*
  136. * To ensure that if the NFCC goes in DEEP SLEEP sate we can wake him
  137. * up. we set BREAK. Once we will be ready to send again we will remove
  138. * it.
  139. */
  140. if (priv->config.break_control && nu->tty->ops->break_ctl) {
  141. nu->tty->ops->break_ctl(nu->tty, -1);
  142. usleep_range(1000, 3000);
  143. }
  144. }
  145. static struct nci_uart nfcmrvl_nci_uart = {
  146. .owner = THIS_MODULE,
  147. .name = "nfcmrvl_uart",
  148. .driver = NCI_UART_DRIVER_MARVELL,
  149. .ops = {
  150. .open = nfcmrvl_nci_uart_open,
  151. .close = nfcmrvl_nci_uart_close,
  152. .recv = nfcmrvl_nci_uart_recv,
  153. .tx_start = nfcmrvl_nci_uart_tx_start,
  154. .tx_done = nfcmrvl_nci_uart_tx_done,
  155. }
  156. };
  157. module_driver(nfcmrvl_nci_uart, nci_uart_register, nci_uart_unregister);
  158. MODULE_AUTHOR("Marvell International Ltd.");
  159. MODULE_DESCRIPTION("Marvell NFC-over-UART");
  160. MODULE_LICENSE("GPL v2");
  161. module_param(flow_control, uint, 0);
  162. MODULE_PARM_DESC(flow_control, "Tell if UART needs flow control at init.");
  163. module_param(break_control, uint, 0);
  164. MODULE_PARM_DESC(break_control, "Tell if UART driver must drive break signal.");
  165. module_param(hci_muxed, uint, 0);
  166. MODULE_PARM_DESC(hci_muxed, "Tell if transport is muxed in HCI one.");
  167. module_param(reset_n_io, int, 0);
  168. MODULE_PARM_DESC(reset_n_io, "GPIO that is wired to RESET_N signal.");