tegra-tcu.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/console.h>
  6. #include <linux/mailbox_client.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/serial.h>
  12. #include <linux/serial_core.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #define TCU_MBOX_BYTE(i, x) ((x) << (i * 8))
  17. #define TCU_MBOX_BYTE_V(x, i) (((x) >> (i * 8)) & 0xff)
  18. #define TCU_MBOX_NUM_BYTES(x) ((x) << 24)
  19. #define TCU_MBOX_NUM_BYTES_V(x) (((x) >> 24) & 0x3)
  20. struct tegra_tcu {
  21. struct uart_driver driver;
  22. #if IS_ENABLED(CONFIG_SERIAL_TEGRA_TCU_CONSOLE)
  23. struct console console;
  24. #endif
  25. struct uart_port port;
  26. struct mbox_client tx_client, rx_client;
  27. struct mbox_chan *tx, *rx;
  28. };
  29. static unsigned int tegra_tcu_uart_tx_empty(struct uart_port *port)
  30. {
  31. return TIOCSER_TEMT;
  32. }
  33. static void tegra_tcu_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  34. {
  35. }
  36. static unsigned int tegra_tcu_uart_get_mctrl(struct uart_port *port)
  37. {
  38. return 0;
  39. }
  40. static void tegra_tcu_uart_stop_tx(struct uart_port *port)
  41. {
  42. }
  43. static void tegra_tcu_write_one(struct tegra_tcu *tcu, u32 value,
  44. unsigned int count)
  45. {
  46. void *msg;
  47. value |= TCU_MBOX_NUM_BYTES(count);
  48. msg = (void *)(unsigned long)value;
  49. mbox_send_message(tcu->tx, msg);
  50. mbox_flush(tcu->tx, 1000);
  51. }
  52. static void tegra_tcu_write(struct tegra_tcu *tcu, const char *s,
  53. unsigned int count)
  54. {
  55. unsigned int written = 0, i = 0;
  56. bool insert_nl = false;
  57. u32 value = 0;
  58. while (i < count) {
  59. if (insert_nl) {
  60. value |= TCU_MBOX_BYTE(written++, '\n');
  61. insert_nl = false;
  62. i++;
  63. } else if (s[i] == '\n') {
  64. value |= TCU_MBOX_BYTE(written++, '\r');
  65. insert_nl = true;
  66. } else {
  67. value |= TCU_MBOX_BYTE(written++, s[i++]);
  68. }
  69. if (written == 3) {
  70. tegra_tcu_write_one(tcu, value, 3);
  71. value = written = 0;
  72. }
  73. }
  74. if (written)
  75. tegra_tcu_write_one(tcu, value, written);
  76. }
  77. static void tegra_tcu_uart_start_tx(struct uart_port *port)
  78. {
  79. struct tegra_tcu *tcu = port->private_data;
  80. struct circ_buf *xmit = &port->state->xmit;
  81. unsigned long count;
  82. for (;;) {
  83. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  84. if (!count)
  85. break;
  86. tegra_tcu_write(tcu, &xmit->buf[xmit->tail], count);
  87. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  88. }
  89. uart_write_wakeup(port);
  90. }
  91. static void tegra_tcu_uart_stop_rx(struct uart_port *port)
  92. {
  93. }
  94. static void tegra_tcu_uart_break_ctl(struct uart_port *port, int ctl)
  95. {
  96. }
  97. static int tegra_tcu_uart_startup(struct uart_port *port)
  98. {
  99. return 0;
  100. }
  101. static void tegra_tcu_uart_shutdown(struct uart_port *port)
  102. {
  103. }
  104. static void tegra_tcu_uart_set_termios(struct uart_port *port,
  105. struct ktermios *new,
  106. struct ktermios *old)
  107. {
  108. }
  109. static const struct uart_ops tegra_tcu_uart_ops = {
  110. .tx_empty = tegra_tcu_uart_tx_empty,
  111. .set_mctrl = tegra_tcu_uart_set_mctrl,
  112. .get_mctrl = tegra_tcu_uart_get_mctrl,
  113. .stop_tx = tegra_tcu_uart_stop_tx,
  114. .start_tx = tegra_tcu_uart_start_tx,
  115. .stop_rx = tegra_tcu_uart_stop_rx,
  116. .break_ctl = tegra_tcu_uart_break_ctl,
  117. .startup = tegra_tcu_uart_startup,
  118. .shutdown = tegra_tcu_uart_shutdown,
  119. .set_termios = tegra_tcu_uart_set_termios,
  120. };
  121. #if IS_ENABLED(CONFIG_SERIAL_TEGRA_TCU_CONSOLE)
  122. static void tegra_tcu_console_write(struct console *cons, const char *s,
  123. unsigned int count)
  124. {
  125. struct tegra_tcu *tcu = container_of(cons, struct tegra_tcu, console);
  126. tegra_tcu_write(tcu, s, count);
  127. }
  128. static int tegra_tcu_console_setup(struct console *cons, char *options)
  129. {
  130. return 0;
  131. }
  132. #endif
  133. static void tegra_tcu_receive(struct mbox_client *cl, void *msg)
  134. {
  135. struct tegra_tcu *tcu = container_of(cl, struct tegra_tcu, rx_client);
  136. struct tty_port *port = &tcu->port.state->port;
  137. u32 value = (u32)(unsigned long)msg;
  138. unsigned int num_bytes, i;
  139. num_bytes = TCU_MBOX_NUM_BYTES_V(value);
  140. for (i = 0; i < num_bytes; i++)
  141. tty_insert_flip_char(port, TCU_MBOX_BYTE_V(value, i),
  142. TTY_NORMAL);
  143. tty_flip_buffer_push(port);
  144. }
  145. static int tegra_tcu_probe(struct platform_device *pdev)
  146. {
  147. struct uart_port *port;
  148. struct tegra_tcu *tcu;
  149. int err;
  150. tcu = devm_kzalloc(&pdev->dev, sizeof(*tcu), GFP_KERNEL);
  151. if (!tcu)
  152. return -ENOMEM;
  153. tcu->tx_client.dev = &pdev->dev;
  154. tcu->rx_client.dev = &pdev->dev;
  155. tcu->rx_client.rx_callback = tegra_tcu_receive;
  156. tcu->tx = mbox_request_channel_byname(&tcu->tx_client, "tx");
  157. if (IS_ERR(tcu->tx)) {
  158. err = PTR_ERR(tcu->tx);
  159. dev_err(&pdev->dev, "failed to get tx mailbox: %d\n", err);
  160. return err;
  161. }
  162. tcu->rx = mbox_request_channel_byname(&tcu->rx_client, "rx");
  163. if (IS_ERR(tcu->rx)) {
  164. err = PTR_ERR(tcu->rx);
  165. dev_err(&pdev->dev, "failed to get rx mailbox: %d\n", err);
  166. goto free_tx;
  167. }
  168. #if IS_ENABLED(CONFIG_SERIAL_TEGRA_TCU_CONSOLE)
  169. /* setup the console */
  170. strcpy(tcu->console.name, "ttyTCU");
  171. tcu->console.device = uart_console_device;
  172. tcu->console.flags = CON_PRINTBUFFER | CON_ANYTIME;
  173. tcu->console.index = -1;
  174. tcu->console.write = tegra_tcu_console_write;
  175. tcu->console.setup = tegra_tcu_console_setup;
  176. tcu->console.data = &tcu->driver;
  177. #endif
  178. /* setup the driver */
  179. tcu->driver.owner = THIS_MODULE;
  180. tcu->driver.driver_name = "tegra-tcu";
  181. tcu->driver.dev_name = "ttyTCU";
  182. #if IS_ENABLED(CONFIG_SERIAL_TEGRA_TCU_CONSOLE)
  183. tcu->driver.cons = &tcu->console;
  184. #endif
  185. tcu->driver.nr = 1;
  186. err = uart_register_driver(&tcu->driver);
  187. if (err) {
  188. dev_err(&pdev->dev, "failed to register UART driver: %d\n",
  189. err);
  190. goto free_rx;
  191. }
  192. /* setup the port */
  193. port = &tcu->port;
  194. spin_lock_init(&port->lock);
  195. port->dev = &pdev->dev;
  196. port->type = PORT_TEGRA_TCU;
  197. port->ops = &tegra_tcu_uart_ops;
  198. port->fifosize = 1;
  199. port->iotype = UPIO_MEM;
  200. port->flags = UPF_BOOT_AUTOCONF;
  201. port->private_data = tcu;
  202. err = uart_add_one_port(&tcu->driver, port);
  203. if (err) {
  204. dev_err(&pdev->dev, "failed to add UART port: %d\n", err);
  205. goto unregister_uart;
  206. }
  207. platform_set_drvdata(pdev, tcu);
  208. #if IS_ENABLED(CONFIG_SERIAL_TEGRA_TCU_CONSOLE)
  209. register_console(&tcu->console);
  210. #endif
  211. return 0;
  212. unregister_uart:
  213. uart_unregister_driver(&tcu->driver);
  214. free_rx:
  215. mbox_free_channel(tcu->rx);
  216. free_tx:
  217. mbox_free_channel(tcu->tx);
  218. return err;
  219. }
  220. static int tegra_tcu_remove(struct platform_device *pdev)
  221. {
  222. struct tegra_tcu *tcu = platform_get_drvdata(pdev);
  223. #if IS_ENABLED(CONFIG_SERIAL_TEGRA_TCU_CONSOLE)
  224. unregister_console(&tcu->console);
  225. #endif
  226. uart_remove_one_port(&tcu->driver, &tcu->port);
  227. uart_unregister_driver(&tcu->driver);
  228. mbox_free_channel(tcu->rx);
  229. mbox_free_channel(tcu->tx);
  230. return 0;
  231. }
  232. static const struct of_device_id tegra_tcu_match[] = {
  233. { .compatible = "nvidia,tegra194-tcu" },
  234. { }
  235. };
  236. static struct platform_driver tegra_tcu_driver = {
  237. .driver = {
  238. .name = "tegra-tcu",
  239. .of_match_table = tegra_tcu_match,
  240. },
  241. .probe = tegra_tcu_probe,
  242. .remove = tegra_tcu_remove,
  243. };
  244. module_platform_driver(tegra_tcu_driver);
  245. MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
  246. MODULE_LICENSE("GPL v2");
  247. MODULE_DESCRIPTION("NVIDIA Tegra Combined UART driver");