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