0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/console.h>
0009 #include <linux/litex.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/of_address.h>
0013 #include <linux/of_platform.h>
0014 #include <linux/serial.h>
0015 #include <linux/serial_core.h>
0016 #include <linux/slab.h>
0017 #include <linux/timer.h>
0018 #include <linux/tty_flip.h>
0019 #include <linux/xarray.h>
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 #define OFF_RXTX 0x00
0034 #define OFF_TXFULL 0x04
0035 #define OFF_RXEMPTY 0x08
0036 #define OFF_EV_STATUS 0x0c
0037 #define OFF_EV_PENDING 0x10
0038 #define OFF_EV_ENABLE 0x14
0039
0040
0041 #define EV_TX 0x1
0042 #define EV_RX 0x2
0043
0044 struct liteuart_port {
0045 struct uart_port port;
0046 struct timer_list timer;
0047 u32 id;
0048 };
0049
0050 #define to_liteuart_port(port) container_of(port, struct liteuart_port, port)
0051
0052 static DEFINE_XARRAY_FLAGS(liteuart_array, XA_FLAGS_ALLOC);
0053
0054 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
0055 static struct console liteuart_console;
0056 #endif
0057
0058 static struct uart_driver liteuart_driver = {
0059 .owner = THIS_MODULE,
0060 .driver_name = "liteuart",
0061 .dev_name = "ttyLXU",
0062 .major = 0,
0063 .minor = 0,
0064 .nr = CONFIG_SERIAL_LITEUART_MAX_PORTS,
0065 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
0066 .cons = &liteuart_console,
0067 #endif
0068 };
0069
0070 static void liteuart_timer(struct timer_list *t)
0071 {
0072 struct liteuart_port *uart = from_timer(uart, t, timer);
0073 struct uart_port *port = &uart->port;
0074 unsigned char __iomem *membase = port->membase;
0075 unsigned int flg = TTY_NORMAL;
0076 int ch;
0077 unsigned long status;
0078
0079 while ((status = !litex_read8(membase + OFF_RXEMPTY)) == 1) {
0080 ch = litex_read8(membase + OFF_RXTX);
0081 port->icount.rx++;
0082
0083
0084 litex_write8(membase + OFF_EV_PENDING, EV_TX | EV_RX);
0085
0086
0087 if (!(uart_handle_sysrq_char(port, ch)))
0088 uart_insert_char(port, status, 0, ch, flg);
0089
0090 tty_flip_buffer_push(&port->state->port);
0091 }
0092
0093 mod_timer(&uart->timer, jiffies + uart_poll_timeout(port));
0094 }
0095
0096 static void liteuart_putchar(struct uart_port *port, unsigned char ch)
0097 {
0098 while (litex_read8(port->membase + OFF_TXFULL))
0099 cpu_relax();
0100
0101 litex_write8(port->membase + OFF_RXTX, ch);
0102 }
0103
0104 static unsigned int liteuart_tx_empty(struct uart_port *port)
0105 {
0106
0107 if (!litex_read8(port->membase + OFF_TXFULL))
0108 return TIOCSER_TEMT;
0109
0110 return 0;
0111 }
0112
0113 static void liteuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
0114 {
0115
0116 }
0117
0118 static unsigned int liteuart_get_mctrl(struct uart_port *port)
0119 {
0120 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
0121 }
0122
0123 static void liteuart_stop_tx(struct uart_port *port)
0124 {
0125 }
0126
0127 static void liteuart_start_tx(struct uart_port *port)
0128 {
0129 struct circ_buf *xmit = &port->state->xmit;
0130 unsigned char ch;
0131
0132 if (unlikely(port->x_char)) {
0133 litex_write8(port->membase + OFF_RXTX, port->x_char);
0134 port->icount.tx++;
0135 port->x_char = 0;
0136 } else if (!uart_circ_empty(xmit)) {
0137 while (xmit->head != xmit->tail) {
0138 ch = xmit->buf[xmit->tail];
0139 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
0140 port->icount.tx++;
0141 liteuart_putchar(port, ch);
0142 }
0143 }
0144
0145 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
0146 uart_write_wakeup(port);
0147 }
0148
0149 static void liteuart_stop_rx(struct uart_port *port)
0150 {
0151 struct liteuart_port *uart = to_liteuart_port(port);
0152
0153
0154 del_timer(&uart->timer);
0155 }
0156
0157 static void liteuart_break_ctl(struct uart_port *port, int break_state)
0158 {
0159
0160 }
0161
0162 static int liteuart_startup(struct uart_port *port)
0163 {
0164 struct liteuart_port *uart = to_liteuart_port(port);
0165
0166
0167 litex_write8(port->membase + OFF_EV_ENABLE, 0);
0168
0169
0170 timer_setup(&uart->timer, liteuart_timer, 0);
0171 mod_timer(&uart->timer, jiffies + uart_poll_timeout(port));
0172
0173 return 0;
0174 }
0175
0176 static void liteuart_shutdown(struct uart_port *port)
0177 {
0178 }
0179
0180 static void liteuart_set_termios(struct uart_port *port, struct ktermios *new,
0181 struct ktermios *old)
0182 {
0183 unsigned int baud;
0184 unsigned long flags;
0185
0186 spin_lock_irqsave(&port->lock, flags);
0187
0188
0189 baud = uart_get_baud_rate(port, new, old, 0, 460800);
0190 uart_update_timeout(port, new->c_cflag, baud);
0191
0192 spin_unlock_irqrestore(&port->lock, flags);
0193 }
0194
0195 static const char *liteuart_type(struct uart_port *port)
0196 {
0197 return "liteuart";
0198 }
0199
0200 static void liteuart_release_port(struct uart_port *port)
0201 {
0202 }
0203
0204 static int liteuart_request_port(struct uart_port *port)
0205 {
0206 return 0;
0207 }
0208
0209 static void liteuart_config_port(struct uart_port *port, int flags)
0210 {
0211
0212
0213
0214
0215
0216 port->type = 1;
0217 }
0218
0219 static int liteuart_verify_port(struct uart_port *port,
0220 struct serial_struct *ser)
0221 {
0222 if (port->type != PORT_UNKNOWN && ser->type != 1)
0223 return -EINVAL;
0224
0225 return 0;
0226 }
0227
0228 static const struct uart_ops liteuart_ops = {
0229 .tx_empty = liteuart_tx_empty,
0230 .set_mctrl = liteuart_set_mctrl,
0231 .get_mctrl = liteuart_get_mctrl,
0232 .stop_tx = liteuart_stop_tx,
0233 .start_tx = liteuart_start_tx,
0234 .stop_rx = liteuart_stop_rx,
0235 .break_ctl = liteuart_break_ctl,
0236 .startup = liteuart_startup,
0237 .shutdown = liteuart_shutdown,
0238 .set_termios = liteuart_set_termios,
0239 .type = liteuart_type,
0240 .release_port = liteuart_release_port,
0241 .request_port = liteuart_request_port,
0242 .config_port = liteuart_config_port,
0243 .verify_port = liteuart_verify_port,
0244 };
0245
0246 static int liteuart_probe(struct platform_device *pdev)
0247 {
0248 struct liteuart_port *uart;
0249 struct uart_port *port;
0250 struct xa_limit limit;
0251 int dev_id, ret;
0252
0253
0254 dev_id = of_alias_get_id(pdev->dev.of_node, "serial");
0255 if (dev_id < 0)
0256 limit = XA_LIMIT(0, CONFIG_SERIAL_LITEUART_MAX_PORTS);
0257 else
0258 limit = XA_LIMIT(dev_id, dev_id);
0259
0260 uart = devm_kzalloc(&pdev->dev, sizeof(struct liteuart_port), GFP_KERNEL);
0261 if (!uart)
0262 return -ENOMEM;
0263
0264 ret = xa_alloc(&liteuart_array, &dev_id, uart, limit, GFP_KERNEL);
0265 if (ret)
0266 return ret;
0267
0268 uart->id = dev_id;
0269 port = &uart->port;
0270
0271
0272 port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
0273 if (IS_ERR(port->membase)) {
0274 ret = PTR_ERR(port->membase);
0275 goto err_erase_id;
0276 }
0277
0278
0279 port->dev = &pdev->dev;
0280 port->iotype = UPIO_MEM;
0281 port->flags = UPF_BOOT_AUTOCONF;
0282 port->ops = &liteuart_ops;
0283 port->regshift = 2;
0284 port->fifosize = 16;
0285 port->iobase = 1;
0286 port->type = PORT_UNKNOWN;
0287 port->line = dev_id;
0288 spin_lock_init(&port->lock);
0289
0290 platform_set_drvdata(pdev, port);
0291
0292 ret = uart_add_one_port(&liteuart_driver, &uart->port);
0293 if (ret)
0294 goto err_erase_id;
0295
0296 return 0;
0297
0298 err_erase_id:
0299 xa_erase(&liteuart_array, uart->id);
0300
0301 return ret;
0302 }
0303
0304 static int liteuart_remove(struct platform_device *pdev)
0305 {
0306 struct uart_port *port = platform_get_drvdata(pdev);
0307 struct liteuart_port *uart = to_liteuart_port(port);
0308
0309 uart_remove_one_port(&liteuart_driver, port);
0310 xa_erase(&liteuart_array, uart->id);
0311
0312 return 0;
0313 }
0314
0315 static const struct of_device_id liteuart_of_match[] = {
0316 { .compatible = "litex,liteuart" },
0317 {}
0318 };
0319 MODULE_DEVICE_TABLE(of, liteuart_of_match);
0320
0321 static struct platform_driver liteuart_platform_driver = {
0322 .probe = liteuart_probe,
0323 .remove = liteuart_remove,
0324 .driver = {
0325 .name = "liteuart",
0326 .of_match_table = liteuart_of_match,
0327 },
0328 };
0329
0330 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
0331
0332 static void liteuart_console_write(struct console *co, const char *s,
0333 unsigned int count)
0334 {
0335 struct liteuart_port *uart;
0336 struct uart_port *port;
0337 unsigned long flags;
0338
0339 uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
0340 port = &uart->port;
0341
0342 spin_lock_irqsave(&port->lock, flags);
0343 uart_console_write(port, s, count, liteuart_putchar);
0344 spin_unlock_irqrestore(&port->lock, flags);
0345 }
0346
0347 static int liteuart_console_setup(struct console *co, char *options)
0348 {
0349 struct liteuart_port *uart;
0350 struct uart_port *port;
0351 int baud = 115200;
0352 int bits = 8;
0353 int parity = 'n';
0354 int flow = 'n';
0355
0356 uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
0357 if (!uart)
0358 return -ENODEV;
0359
0360 port = &uart->port;
0361 if (!port->membase)
0362 return -ENODEV;
0363
0364 if (options)
0365 uart_parse_options(options, &baud, &parity, &bits, &flow);
0366
0367 return uart_set_options(port, co, baud, parity, bits, flow);
0368 }
0369
0370 static struct console liteuart_console = {
0371 .name = "liteuart",
0372 .write = liteuart_console_write,
0373 .device = uart_console_device,
0374 .setup = liteuart_console_setup,
0375 .flags = CON_PRINTBUFFER,
0376 .index = -1,
0377 .data = &liteuart_driver,
0378 };
0379
0380 static int __init liteuart_console_init(void)
0381 {
0382 register_console(&liteuart_console);
0383
0384 return 0;
0385 }
0386 console_initcall(liteuart_console_init);
0387
0388 static void early_liteuart_write(struct console *console, const char *s,
0389 unsigned int count)
0390 {
0391 struct earlycon_device *device = console->data;
0392 struct uart_port *port = &device->port;
0393
0394 uart_console_write(port, s, count, liteuart_putchar);
0395 }
0396
0397 static int __init early_liteuart_setup(struct earlycon_device *device,
0398 const char *options)
0399 {
0400 if (!device->port.membase)
0401 return -ENODEV;
0402
0403 device->con->write = early_liteuart_write;
0404 return 0;
0405 }
0406
0407 OF_EARLYCON_DECLARE(liteuart, "litex,liteuart", early_liteuart_setup);
0408 #endif
0409
0410 static int __init liteuart_init(void)
0411 {
0412 int res;
0413
0414 res = uart_register_driver(&liteuart_driver);
0415 if (res)
0416 return res;
0417
0418 res = platform_driver_register(&liteuart_platform_driver);
0419 if (res) {
0420 uart_unregister_driver(&liteuart_driver);
0421 return res;
0422 }
0423
0424 return 0;
0425 }
0426
0427 static void __exit liteuart_exit(void)
0428 {
0429 platform_driver_unregister(&liteuart_platform_driver);
0430 uart_unregister_driver(&liteuart_driver);
0431 }
0432
0433 module_init(liteuart_init);
0434 module_exit(liteuart_exit);
0435
0436 MODULE_AUTHOR("Antmicro <www.antmicro.com>");
0437 MODULE_DESCRIPTION("LiteUART serial driver");
0438 MODULE_LICENSE("GPL v2");
0439 MODULE_ALIAS("platform:liteuart");