esp32_acm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/bitfield.h>
  3. #include <linux/bits.h>
  4. #include <linux/console.h>
  5. #include <linux/delay.h>
  6. #include <linux/io.h>
  7. #include <linux/irq.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/serial_core.h>
  12. #include <linux/slab.h>
  13. #include <linux/tty_flip.h>
  14. #include <asm/serial.h>
  15. #define DRIVER_NAME "esp32s3-acm"
  16. #define DEV_NAME "ttyGS"
  17. #define UART_NR 4
  18. #define ESP32S3_ACM_TX_FIFO_SIZE 64
  19. #define USB_SERIAL_JTAG_EP1_REG 0x00
  20. #define USB_SERIAL_JTAG_EP1_CONF_REG 0x04
  21. #define USB_SERIAL_JTAG_WR_DONE BIT(0)
  22. #define USB_SERIAL_JTAG_SERIAL_IN_EP_DATA_FREE BIT(1)
  23. #define USB_SERIAL_JTAG_INT_ST_REG 0x0c
  24. #define USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ST BIT(2)
  25. #define USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ST BIT(3)
  26. #define USB_SERIAL_JTAG_INT_ENA_REG 0x10
  27. #define USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ENA BIT(2)
  28. #define USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA BIT(3)
  29. #define USB_SERIAL_JTAG_INT_CLR_REG 0x14
  30. #define USB_SERIAL_JTAG_IN_EP1_ST_REG 0x2c
  31. #define USB_SERIAL_JTAG_IN_EP1_WR_ADDR GENMASK(8, 2)
  32. #define USB_SERIAL_JTAG_OUT_EP1_ST_REG 0x3c
  33. #define USB_SERIAL_JTAG_OUT_EP1_REC_DATA_CNT GENMASK(22, 16)
  34. static const struct of_device_id esp32s3_acm_dt_ids[] = {
  35. {
  36. .compatible = "esp,esp32s3-acm",
  37. }, { /* sentinel */ }
  38. };
  39. MODULE_DEVICE_TABLE(of, esp32s3_acm_dt_ids);
  40. static struct uart_port *esp32s3_acm_ports[UART_NR];
  41. static void esp32s3_acm_write(struct uart_port *port, unsigned long reg, u32 v)
  42. {
  43. writel(v, port->membase + reg);
  44. }
  45. static u32 esp32s3_acm_read(struct uart_port *port, unsigned long reg)
  46. {
  47. return readl(port->membase + reg);
  48. }
  49. static u32 esp32s3_acm_tx_fifo_free(struct uart_port *port)
  50. {
  51. u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_CONF_REG);
  52. return status & USB_SERIAL_JTAG_SERIAL_IN_EP_DATA_FREE;
  53. }
  54. static u32 esp32s3_acm_tx_fifo_cnt(struct uart_port *port)
  55. {
  56. u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_IN_EP1_ST_REG);
  57. return FIELD_GET(USB_SERIAL_JTAG_IN_EP1_WR_ADDR, status);
  58. }
  59. static u32 esp32s3_acm_rx_fifo_cnt(struct uart_port *port)
  60. {
  61. u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_OUT_EP1_ST_REG);
  62. return FIELD_GET(USB_SERIAL_JTAG_OUT_EP1_REC_DATA_CNT, status);
  63. }
  64. /* return TIOCSER_TEMT when transmitter is not busy */
  65. static unsigned int esp32s3_acm_tx_empty(struct uart_port *port)
  66. {
  67. return esp32s3_acm_tx_fifo_cnt(port) == 0 ? TIOCSER_TEMT : 0;
  68. }
  69. static void esp32s3_acm_set_mctrl(struct uart_port *port, unsigned int mctrl)
  70. {
  71. }
  72. static unsigned int esp32s3_acm_get_mctrl(struct uart_port *port)
  73. {
  74. return TIOCM_CAR;
  75. }
  76. static void esp32s3_acm_stop_tx(struct uart_port *port)
  77. {
  78. u32 int_ena;
  79. int_ena = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ENA_REG);
  80. int_ena &= ~USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA;
  81. esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, int_ena);
  82. }
  83. static void esp32s3_acm_rxint(struct uart_port *port)
  84. {
  85. struct tty_port *tty_port = &port->state->port;
  86. u32 rx_fifo_cnt = esp32s3_acm_rx_fifo_cnt(port);
  87. unsigned long flags;
  88. u32 i;
  89. if (!rx_fifo_cnt)
  90. return;
  91. spin_lock_irqsave(&port->lock, flags);
  92. for (i = 0; i < rx_fifo_cnt; ++i) {
  93. u32 rx = esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_REG);
  94. ++port->icount.rx;
  95. tty_insert_flip_char(tty_port, rx, TTY_NORMAL);
  96. }
  97. spin_unlock_irqrestore(&port->lock, flags);
  98. tty_flip_buffer_push(tty_port);
  99. }
  100. static void esp32s3_acm_push(struct uart_port *port)
  101. {
  102. if (esp32s3_acm_tx_fifo_free(port))
  103. esp32s3_acm_write(port, USB_SERIAL_JTAG_EP1_CONF_REG,
  104. USB_SERIAL_JTAG_WR_DONE);
  105. }
  106. static void esp32s3_acm_put_char(struct uart_port *port, u8 c)
  107. {
  108. esp32s3_acm_write(port, USB_SERIAL_JTAG_EP1_REG, c);
  109. }
  110. static void esp32s3_acm_put_char_sync(struct uart_port *port, u8 c)
  111. {
  112. unsigned long timeout = jiffies + HZ;
  113. while (!esp32s3_acm_tx_fifo_free(port)) {
  114. if (time_after(jiffies, timeout)) {
  115. dev_warn(port->dev, "timeout waiting for TX FIFO\n");
  116. return;
  117. }
  118. cpu_relax();
  119. }
  120. esp32s3_acm_put_char(port, c);
  121. esp32s3_acm_push(port);
  122. }
  123. static void esp32s3_acm_transmit_buffer(struct uart_port *port)
  124. {
  125. u32 tx_fifo_used;
  126. unsigned int pending;
  127. u8 ch;
  128. if (!esp32s3_acm_tx_fifo_free(port))
  129. return;
  130. tx_fifo_used = esp32s3_acm_tx_fifo_cnt(port);
  131. pending = uart_port_tx_limited(port, ch,
  132. ESP32S3_ACM_TX_FIFO_SIZE - tx_fifo_used,
  133. true, esp32s3_acm_put_char(port, ch),
  134. ({}));
  135. if (pending) {
  136. u32 int_ena;
  137. int_ena = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ENA_REG);
  138. int_ena |= USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA;
  139. esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, int_ena);
  140. }
  141. esp32s3_acm_push(port);
  142. }
  143. static void esp32s3_acm_txint(struct uart_port *port)
  144. {
  145. esp32s3_acm_transmit_buffer(port);
  146. }
  147. static irqreturn_t esp32s3_acm_int(int irq, void *dev_id)
  148. {
  149. struct uart_port *port = dev_id;
  150. u32 status;
  151. status = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ST_REG);
  152. esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_CLR_REG, status);
  153. if (status & USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ST)
  154. esp32s3_acm_rxint(port);
  155. if (status & USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ST)
  156. esp32s3_acm_txint(port);
  157. return IRQ_RETVAL(status);
  158. }
  159. static void esp32s3_acm_start_tx(struct uart_port *port)
  160. {
  161. esp32s3_acm_transmit_buffer(port);
  162. }
  163. static void esp32s3_acm_stop_rx(struct uart_port *port)
  164. {
  165. u32 int_ena;
  166. int_ena = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ENA_REG);
  167. int_ena &= ~USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ENA;
  168. esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, int_ena);
  169. }
  170. static int esp32s3_acm_startup(struct uart_port *port)
  171. {
  172. int ret;
  173. ret = request_irq(port->irq, esp32s3_acm_int, 0, DRIVER_NAME, port);
  174. if (ret)
  175. return ret;
  176. esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG,
  177. USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ENA);
  178. return 0;
  179. }
  180. static void esp32s3_acm_shutdown(struct uart_port *port)
  181. {
  182. esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, 0);
  183. free_irq(port->irq, port);
  184. }
  185. static void esp32s3_acm_set_termios(struct uart_port *port,
  186. struct ktermios *termios,
  187. const struct ktermios *old)
  188. {
  189. }
  190. static const char *esp32s3_acm_type(struct uart_port *port)
  191. {
  192. return "ESP32S3 ACM";
  193. }
  194. /* configure/auto-configure the port */
  195. static void esp32s3_acm_config_port(struct uart_port *port, int flags)
  196. {
  197. if (flags & UART_CONFIG_TYPE)
  198. port->type = PORT_GENERIC;
  199. }
  200. #ifdef CONFIG_CONSOLE_POLL
  201. static void esp32s3_acm_poll_put_char(struct uart_port *port, unsigned char c)
  202. {
  203. esp32s3_acm_put_char_sync(port, c);
  204. }
  205. static int esp32s3_acm_poll_get_char(struct uart_port *port)
  206. {
  207. if (esp32s3_acm_rx_fifo_cnt(port))
  208. return esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_REG);
  209. else
  210. return NO_POLL_CHAR;
  211. }
  212. #endif
  213. static const struct uart_ops esp32s3_acm_pops = {
  214. .tx_empty = esp32s3_acm_tx_empty,
  215. .set_mctrl = esp32s3_acm_set_mctrl,
  216. .get_mctrl = esp32s3_acm_get_mctrl,
  217. .stop_tx = esp32s3_acm_stop_tx,
  218. .start_tx = esp32s3_acm_start_tx,
  219. .stop_rx = esp32s3_acm_stop_rx,
  220. .startup = esp32s3_acm_startup,
  221. .shutdown = esp32s3_acm_shutdown,
  222. .set_termios = esp32s3_acm_set_termios,
  223. .type = esp32s3_acm_type,
  224. .config_port = esp32s3_acm_config_port,
  225. #ifdef CONFIG_CONSOLE_POLL
  226. .poll_put_char = esp32s3_acm_poll_put_char,
  227. .poll_get_char = esp32s3_acm_poll_get_char,
  228. #endif
  229. };
  230. static void esp32s3_acm_string_write(struct uart_port *port, const char *s,
  231. unsigned int count)
  232. {
  233. uart_console_write(port, s, count, esp32s3_acm_put_char_sync);
  234. }
  235. static void
  236. esp32s3_acm_console_write(struct console *co, const char *s, unsigned int count)
  237. {
  238. struct uart_port *port = esp32s3_acm_ports[co->index];
  239. unsigned long flags;
  240. bool locked = true;
  241. if (port->sysrq)
  242. locked = false;
  243. else if (oops_in_progress)
  244. locked = spin_trylock_irqsave(&port->lock, flags);
  245. else
  246. spin_lock_irqsave(&port->lock, flags);
  247. esp32s3_acm_string_write(port, s, count);
  248. if (locked)
  249. spin_unlock_irqrestore(&port->lock, flags);
  250. }
  251. static struct uart_driver esp32s3_acm_reg;
  252. static struct console esp32s3_acm_console = {
  253. .name = DEV_NAME,
  254. .write = esp32s3_acm_console_write,
  255. .device = uart_console_device,
  256. .flags = CON_PRINTBUFFER,
  257. .index = -1,
  258. .data = &esp32s3_acm_reg,
  259. };
  260. static void esp32s3_acm_earlycon_write(struct console *con, const char *s,
  261. unsigned int n)
  262. {
  263. struct earlycon_device *dev = con->data;
  264. uart_console_write(&dev->port, s, n, esp32s3_acm_put_char_sync);
  265. }
  266. #ifdef CONFIG_CONSOLE_POLL
  267. static int esp32s3_acm_earlycon_read(struct console *con, char *s, unsigned int n)
  268. {
  269. struct earlycon_device *dev = con->data;
  270. unsigned int num_read = 0;
  271. while (num_read < n) {
  272. int c = esp32s3_acm_poll_get_char(&dev->port);
  273. if (c == NO_POLL_CHAR)
  274. break;
  275. s[num_read++] = c;
  276. }
  277. return num_read;
  278. }
  279. #endif
  280. static int __init esp32s3_acm_early_console_setup(struct earlycon_device *device,
  281. const char *options)
  282. {
  283. if (!device->port.membase)
  284. return -ENODEV;
  285. device->con->write = esp32s3_acm_earlycon_write;
  286. #ifdef CONFIG_CONSOLE_POLL
  287. device->con->read = esp32s3_acm_earlycon_read;
  288. #endif
  289. return 0;
  290. }
  291. OF_EARLYCON_DECLARE(esp32s3acm, "esp,esp32s3-acm",
  292. esp32s3_acm_early_console_setup);
  293. static struct uart_driver esp32s3_acm_reg = {
  294. .owner = THIS_MODULE,
  295. .driver_name = DRIVER_NAME,
  296. .dev_name = DEV_NAME,
  297. .nr = ARRAY_SIZE(esp32s3_acm_ports),
  298. .cons = &esp32s3_acm_console,
  299. };
  300. static int esp32s3_acm_probe(struct platform_device *pdev)
  301. {
  302. struct device_node *np = pdev->dev.of_node;
  303. struct uart_port *port;
  304. struct resource *res;
  305. int ret;
  306. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  307. if (!port)
  308. return -ENOMEM;
  309. ret = of_alias_get_id(np, "serial");
  310. if (ret < 0) {
  311. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  312. return ret;
  313. }
  314. if (ret >= UART_NR) {
  315. dev_err(&pdev->dev, "driver limited to %d serial ports\n",
  316. UART_NR);
  317. return -ENOMEM;
  318. }
  319. port->line = ret;
  320. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  321. if (!res)
  322. return -ENODEV;
  323. port->mapbase = res->start;
  324. port->membase = devm_ioremap_resource(&pdev->dev, res);
  325. if (IS_ERR(port->membase))
  326. return PTR_ERR(port->membase);
  327. port->dev = &pdev->dev;
  328. port->type = PORT_GENERIC;
  329. port->iotype = UPIO_MEM;
  330. port->irq = platform_get_irq(pdev, 0);
  331. port->ops = &esp32s3_acm_pops;
  332. port->flags = UPF_BOOT_AUTOCONF;
  333. port->has_sysrq = 1;
  334. port->fifosize = ESP32S3_ACM_TX_FIFO_SIZE;
  335. esp32s3_acm_ports[port->line] = port;
  336. platform_set_drvdata(pdev, port);
  337. return uart_add_one_port(&esp32s3_acm_reg, port);
  338. }
  339. static void esp32s3_acm_remove(struct platform_device *pdev)
  340. {
  341. struct uart_port *port = platform_get_drvdata(pdev);
  342. uart_remove_one_port(&esp32s3_acm_reg, port);
  343. }
  344. static struct platform_driver esp32s3_acm_driver = {
  345. .probe = esp32s3_acm_probe,
  346. .remove = esp32s3_acm_remove,
  347. .driver = {
  348. .name = DRIVER_NAME,
  349. .of_match_table = esp32s3_acm_dt_ids,
  350. },
  351. };
  352. static int __init esp32s3_acm_init(void)
  353. {
  354. int ret;
  355. ret = uart_register_driver(&esp32s3_acm_reg);
  356. if (ret)
  357. return ret;
  358. ret = platform_driver_register(&esp32s3_acm_driver);
  359. if (ret)
  360. uart_unregister_driver(&esp32s3_acm_reg);
  361. return ret;
  362. }
  363. static void __exit esp32s3_acm_exit(void)
  364. {
  365. platform_driver_unregister(&esp32s3_acm_driver);
  366. uart_unregister_driver(&esp32s3_acm_reg);
  367. }
  368. module_init(esp32s3_acm_init);
  369. module_exit(esp32s3_acm_exit);
  370. MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
  371. MODULE_DESCRIPTION("Espressif ESP32 USB ACM gadget support");
  372. MODULE_LICENSE("GPL");