liteuart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * LiteUART serial controller (LiteX) Driver
  4. *
  5. * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/console.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/litex.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/serial.h>
  15. #include <linux/serial_core.h>
  16. #include <linux/slab.h>
  17. #include <linux/timer.h>
  18. #include <linux/tty_flip.h>
  19. #include <linux/xarray.h>
  20. /*
  21. * CSRs definitions (base address offsets + width)
  22. *
  23. * The definitions below are true for LiteX SoC configured for 8-bit CSR Bus,
  24. * 32-bit aligned.
  25. *
  26. * Supporting other configurations might require new definitions or a more
  27. * generic way of indexing the LiteX CSRs.
  28. *
  29. * For more details on how CSRs are defined and handled in LiteX, see comments
  30. * in the LiteX SoC Driver: drivers/soc/litex/litex_soc_ctrl.c
  31. */
  32. #define OFF_RXTX 0x00
  33. #define OFF_TXFULL 0x04
  34. #define OFF_RXEMPTY 0x08
  35. #define OFF_EV_STATUS 0x0c
  36. #define OFF_EV_PENDING 0x10
  37. #define OFF_EV_ENABLE 0x14
  38. /* events */
  39. #define EV_TX BIT(0)
  40. #define EV_RX BIT(1)
  41. struct liteuart_port {
  42. struct uart_port port;
  43. struct timer_list timer;
  44. u8 irq_reg;
  45. };
  46. #define to_liteuart_port(port) container_of(port, struct liteuart_port, port)
  47. static DEFINE_XARRAY_FLAGS(liteuart_array, XA_FLAGS_ALLOC);
  48. #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
  49. static struct console liteuart_console;
  50. #endif
  51. static struct uart_driver liteuart_driver = {
  52. .owner = THIS_MODULE,
  53. .driver_name = KBUILD_MODNAME,
  54. .dev_name = "ttyLXU",
  55. .major = 0,
  56. .minor = 0,
  57. .nr = CONFIG_SERIAL_LITEUART_MAX_PORTS,
  58. #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
  59. .cons = &liteuart_console,
  60. #endif
  61. };
  62. static void liteuart_update_irq_reg(struct uart_port *port, bool set, u8 mask)
  63. {
  64. struct liteuart_port *uart = to_liteuart_port(port);
  65. if (set)
  66. uart->irq_reg |= mask;
  67. else
  68. uart->irq_reg &= ~mask;
  69. if (port->irq)
  70. litex_write8(port->membase + OFF_EV_ENABLE, uart->irq_reg);
  71. }
  72. static void liteuart_stop_tx(struct uart_port *port)
  73. {
  74. liteuart_update_irq_reg(port, false, EV_TX);
  75. }
  76. static void liteuart_start_tx(struct uart_port *port)
  77. {
  78. liteuart_update_irq_reg(port, true, EV_TX);
  79. }
  80. static void liteuart_stop_rx(struct uart_port *port)
  81. {
  82. struct liteuart_port *uart = to_liteuart_port(port);
  83. /* just delete timer */
  84. del_timer(&uart->timer);
  85. }
  86. static void liteuart_rx_chars(struct uart_port *port)
  87. {
  88. unsigned char __iomem *membase = port->membase;
  89. u8 ch;
  90. while (!litex_read8(membase + OFF_RXEMPTY)) {
  91. ch = litex_read8(membase + OFF_RXTX);
  92. port->icount.rx++;
  93. /* necessary for RXEMPTY to refresh its value */
  94. litex_write8(membase + OFF_EV_PENDING, EV_RX);
  95. /* no overflow bits in status */
  96. if (!(uart_handle_sysrq_char(port, ch)))
  97. uart_insert_char(port, 1, 0, ch, TTY_NORMAL);
  98. }
  99. tty_flip_buffer_push(&port->state->port);
  100. }
  101. static void liteuart_tx_chars(struct uart_port *port)
  102. {
  103. u8 ch;
  104. uart_port_tx(port, ch,
  105. !litex_read8(port->membase + OFF_TXFULL),
  106. litex_write8(port->membase + OFF_RXTX, ch));
  107. }
  108. static irqreturn_t liteuart_interrupt(int irq, void *data)
  109. {
  110. struct liteuart_port *uart = data;
  111. struct uart_port *port = &uart->port;
  112. unsigned long flags;
  113. u8 isr;
  114. /*
  115. * if polling, the context would be "in_serving_softirq", so use
  116. * irq[save|restore] spin_lock variants to cover all possibilities
  117. */
  118. uart_port_lock_irqsave(port, &flags);
  119. isr = litex_read8(port->membase + OFF_EV_PENDING) & uart->irq_reg;
  120. if (isr & EV_RX)
  121. liteuart_rx_chars(port);
  122. if (isr & EV_TX)
  123. liteuart_tx_chars(port);
  124. uart_port_unlock_irqrestore(port, flags);
  125. return IRQ_RETVAL(isr);
  126. }
  127. static void liteuart_timer(struct timer_list *t)
  128. {
  129. struct liteuart_port *uart = from_timer(uart, t, timer);
  130. struct uart_port *port = &uart->port;
  131. liteuart_interrupt(0, port);
  132. mod_timer(&uart->timer, jiffies + uart_poll_timeout(port));
  133. }
  134. static unsigned int liteuart_tx_empty(struct uart_port *port)
  135. {
  136. /* not really tx empty, just checking if tx is not full */
  137. if (!litex_read8(port->membase + OFF_TXFULL))
  138. return TIOCSER_TEMT;
  139. return 0;
  140. }
  141. static void liteuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  142. {
  143. /* modem control register is not present in LiteUART */
  144. }
  145. static unsigned int liteuart_get_mctrl(struct uart_port *port)
  146. {
  147. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  148. }
  149. static int liteuart_startup(struct uart_port *port)
  150. {
  151. struct liteuart_port *uart = to_liteuart_port(port);
  152. unsigned long flags;
  153. int ret;
  154. if (port->irq) {
  155. ret = request_irq(port->irq, liteuart_interrupt, 0,
  156. KBUILD_MODNAME, uart);
  157. if (ret) {
  158. dev_warn(port->dev,
  159. "line %d irq %d failed: switch to polling\n",
  160. port->line, port->irq);
  161. port->irq = 0;
  162. }
  163. }
  164. uart_port_lock_irqsave(port, &flags);
  165. /* only enabling rx irqs during startup */
  166. liteuart_update_irq_reg(port, true, EV_RX);
  167. uart_port_unlock_irqrestore(port, flags);
  168. if (!port->irq) {
  169. timer_setup(&uart->timer, liteuart_timer, 0);
  170. mod_timer(&uart->timer, jiffies + uart_poll_timeout(port));
  171. }
  172. return 0;
  173. }
  174. static void liteuart_shutdown(struct uart_port *port)
  175. {
  176. struct liteuart_port *uart = to_liteuart_port(port);
  177. unsigned long flags;
  178. uart_port_lock_irqsave(port, &flags);
  179. liteuart_update_irq_reg(port, false, EV_RX | EV_TX);
  180. uart_port_unlock_irqrestore(port, flags);
  181. if (port->irq)
  182. free_irq(port->irq, port);
  183. else
  184. del_timer_sync(&uart->timer);
  185. }
  186. static void liteuart_set_termios(struct uart_port *port, struct ktermios *new,
  187. const struct ktermios *old)
  188. {
  189. unsigned int baud;
  190. unsigned long flags;
  191. uart_port_lock_irqsave(port, &flags);
  192. /* update baudrate */
  193. baud = uart_get_baud_rate(port, new, old, 0, 460800);
  194. uart_update_timeout(port, new->c_cflag, baud);
  195. uart_port_unlock_irqrestore(port, flags);
  196. }
  197. static const char *liteuart_type(struct uart_port *port)
  198. {
  199. return "liteuart";
  200. }
  201. static void liteuart_config_port(struct uart_port *port, int flags)
  202. {
  203. /*
  204. * Driver core for serial ports forces a non-zero value for port type.
  205. * Write an arbitrary value here to accommodate the serial core driver,
  206. * as ID part of UAPI is redundant.
  207. */
  208. port->type = 1;
  209. }
  210. static int liteuart_verify_port(struct uart_port *port,
  211. struct serial_struct *ser)
  212. {
  213. if (port->type != PORT_UNKNOWN && ser->type != 1)
  214. return -EINVAL;
  215. return 0;
  216. }
  217. static const struct uart_ops liteuart_ops = {
  218. .tx_empty = liteuart_tx_empty,
  219. .set_mctrl = liteuart_set_mctrl,
  220. .get_mctrl = liteuart_get_mctrl,
  221. .stop_tx = liteuart_stop_tx,
  222. .start_tx = liteuart_start_tx,
  223. .stop_rx = liteuart_stop_rx,
  224. .startup = liteuart_startup,
  225. .shutdown = liteuart_shutdown,
  226. .set_termios = liteuart_set_termios,
  227. .type = liteuart_type,
  228. .config_port = liteuart_config_port,
  229. .verify_port = liteuart_verify_port,
  230. };
  231. static int liteuart_probe(struct platform_device *pdev)
  232. {
  233. struct liteuart_port *uart;
  234. struct uart_port *port;
  235. struct xa_limit limit;
  236. int dev_id, ret;
  237. uart = devm_kzalloc(&pdev->dev, sizeof(struct liteuart_port), GFP_KERNEL);
  238. if (!uart)
  239. return -ENOMEM;
  240. port = &uart->port;
  241. /* get membase */
  242. port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  243. if (IS_ERR(port->membase))
  244. return PTR_ERR(port->membase);
  245. ret = platform_get_irq_optional(pdev, 0);
  246. if (ret < 0 && ret != -ENXIO)
  247. return ret;
  248. if (ret > 0)
  249. port->irq = ret;
  250. /* look for aliases; auto-enumerate for free index if not found */
  251. dev_id = of_alias_get_id(pdev->dev.of_node, "serial");
  252. if (dev_id < 0)
  253. limit = XA_LIMIT(0, CONFIG_SERIAL_LITEUART_MAX_PORTS);
  254. else
  255. limit = XA_LIMIT(dev_id, dev_id);
  256. ret = xa_alloc(&liteuart_array, &dev_id, uart, limit, GFP_KERNEL);
  257. if (ret)
  258. return ret;
  259. /* values not from device tree */
  260. port->dev = &pdev->dev;
  261. port->iotype = UPIO_MEM;
  262. port->flags = UPF_BOOT_AUTOCONF;
  263. port->ops = &liteuart_ops;
  264. port->fifosize = 16;
  265. port->type = PORT_UNKNOWN;
  266. port->line = dev_id;
  267. spin_lock_init(&port->lock);
  268. platform_set_drvdata(pdev, port);
  269. ret = uart_add_one_port(&liteuart_driver, &uart->port);
  270. if (ret)
  271. goto err_erase_id;
  272. return 0;
  273. err_erase_id:
  274. xa_erase(&liteuart_array, dev_id);
  275. return ret;
  276. }
  277. static void liteuart_remove(struct platform_device *pdev)
  278. {
  279. struct uart_port *port = platform_get_drvdata(pdev);
  280. unsigned int line = port->line;
  281. uart_remove_one_port(&liteuart_driver, port);
  282. xa_erase(&liteuart_array, line);
  283. }
  284. static const struct of_device_id liteuart_of_match[] = {
  285. { .compatible = "litex,liteuart" },
  286. {}
  287. };
  288. MODULE_DEVICE_TABLE(of, liteuart_of_match);
  289. static struct platform_driver liteuart_platform_driver = {
  290. .probe = liteuart_probe,
  291. .remove_new = liteuart_remove,
  292. .driver = {
  293. .name = KBUILD_MODNAME,
  294. .of_match_table = liteuart_of_match,
  295. },
  296. };
  297. #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
  298. static void liteuart_putchar(struct uart_port *port, unsigned char ch)
  299. {
  300. while (litex_read8(port->membase + OFF_TXFULL))
  301. cpu_relax();
  302. litex_write8(port->membase + OFF_RXTX, ch);
  303. }
  304. static void liteuart_console_write(struct console *co, const char *s,
  305. unsigned int count)
  306. {
  307. struct liteuart_port *uart;
  308. struct uart_port *port;
  309. unsigned long flags;
  310. uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
  311. port = &uart->port;
  312. uart_port_lock_irqsave(port, &flags);
  313. uart_console_write(port, s, count, liteuart_putchar);
  314. uart_port_unlock_irqrestore(port, flags);
  315. }
  316. static int liteuart_console_setup(struct console *co, char *options)
  317. {
  318. struct liteuart_port *uart;
  319. struct uart_port *port;
  320. int baud = 115200;
  321. int bits = 8;
  322. int parity = 'n';
  323. int flow = 'n';
  324. uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
  325. if (!uart)
  326. return -ENODEV;
  327. port = &uart->port;
  328. if (!port->membase)
  329. return -ENODEV;
  330. if (options)
  331. uart_parse_options(options, &baud, &parity, &bits, &flow);
  332. return uart_set_options(port, co, baud, parity, bits, flow);
  333. }
  334. static struct console liteuart_console = {
  335. .name = KBUILD_MODNAME,
  336. .write = liteuart_console_write,
  337. .device = uart_console_device,
  338. .setup = liteuart_console_setup,
  339. .flags = CON_PRINTBUFFER,
  340. .index = -1,
  341. .data = &liteuart_driver,
  342. };
  343. static int __init liteuart_console_init(void)
  344. {
  345. register_console(&liteuart_console);
  346. return 0;
  347. }
  348. console_initcall(liteuart_console_init);
  349. static void early_liteuart_write(struct console *console, const char *s,
  350. unsigned int count)
  351. {
  352. struct earlycon_device *device = console->data;
  353. struct uart_port *port = &device->port;
  354. uart_console_write(port, s, count, liteuart_putchar);
  355. }
  356. static int __init early_liteuart_setup(struct earlycon_device *device,
  357. const char *options)
  358. {
  359. if (!device->port.membase)
  360. return -ENODEV;
  361. device->con->write = early_liteuart_write;
  362. return 0;
  363. }
  364. OF_EARLYCON_DECLARE(liteuart, "litex,liteuart", early_liteuart_setup);
  365. #endif /* CONFIG_SERIAL_LITEUART_CONSOLE */
  366. static int __init liteuart_init(void)
  367. {
  368. int res;
  369. res = uart_register_driver(&liteuart_driver);
  370. if (res)
  371. return res;
  372. res = platform_driver_register(&liteuart_platform_driver);
  373. if (res)
  374. uart_unregister_driver(&liteuart_driver);
  375. return res;
  376. }
  377. static void __exit liteuart_exit(void)
  378. {
  379. platform_driver_unregister(&liteuart_platform_driver);
  380. uart_unregister_driver(&liteuart_driver);
  381. }
  382. module_init(liteuart_init);
  383. module_exit(liteuart_exit);
  384. MODULE_AUTHOR("Antmicro <www.antmicro.com>");
  385. MODULE_DESCRIPTION("LiteUART serial driver");
  386. MODULE_LICENSE("GPL v2");
  387. MODULE_ALIAS("platform:liteuart");