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