serial: liteuart: Don't mix devm_*() with non-devm_*() calls
[platform/kernel/linux-rpi.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         uart = devm_kzalloc(&pdev->dev, sizeof(struct liteuart_port), GFP_KERNEL);
290         if (!uart)
291                 return -ENOMEM;
292
293         port = &uart->port;
294
295         /* get membase */
296         port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
297         if (IS_ERR(port->membase))
298                 return PTR_ERR(port->membase);
299
300         ret = platform_get_irq_optional(pdev, 0);
301         if (ret < 0 && ret != -ENXIO)
302                 return ret;
303         if (ret > 0)
304                 port->irq = ret;
305
306         /* look for aliases; auto-enumerate for free index if not found */
307         dev_id = of_alias_get_id(pdev->dev.of_node, "serial");
308         if (dev_id < 0)
309                 limit = XA_LIMIT(0, CONFIG_SERIAL_LITEUART_MAX_PORTS);
310         else
311                 limit = XA_LIMIT(dev_id, dev_id);
312
313         ret = xa_alloc(&liteuart_array, &dev_id, uart, limit, GFP_KERNEL);
314         if (ret)
315                 return ret;
316
317         uart->id = dev_id;
318         /* values not from device tree */
319         port->dev = &pdev->dev;
320         port->iotype = UPIO_MEM;
321         port->flags = UPF_BOOT_AUTOCONF;
322         port->ops = &liteuart_ops;
323         port->fifosize = 16;
324         port->type = PORT_UNKNOWN;
325         port->line = dev_id;
326         spin_lock_init(&port->lock);
327
328         platform_set_drvdata(pdev, port);
329
330         ret = uart_add_one_port(&liteuart_driver, &uart->port);
331         if (ret)
332                 goto err_erase_id;
333
334         return 0;
335
336 err_erase_id:
337         xa_erase(&liteuart_array, uart->id);
338
339         return ret;
340 }
341
342 static int liteuart_remove(struct platform_device *pdev)
343 {
344         struct uart_port *port = platform_get_drvdata(pdev);
345         struct liteuart_port *uart = to_liteuart_port(port);
346
347         uart_remove_one_port(&liteuart_driver, port);
348         xa_erase(&liteuart_array, uart->id);
349
350         return 0;
351 }
352
353 static const struct of_device_id liteuart_of_match[] = {
354         { .compatible = "litex,liteuart" },
355         {}
356 };
357 MODULE_DEVICE_TABLE(of, liteuart_of_match);
358
359 static struct platform_driver liteuart_platform_driver = {
360         .probe = liteuart_probe,
361         .remove = liteuart_remove,
362         .driver = {
363                 .name = KBUILD_MODNAME,
364                 .of_match_table = liteuart_of_match,
365         },
366 };
367
368 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
369
370 static void liteuart_putchar(struct uart_port *port, unsigned char ch)
371 {
372         while (litex_read8(port->membase + OFF_TXFULL))
373                 cpu_relax();
374
375         litex_write8(port->membase + OFF_RXTX, ch);
376 }
377
378 static void liteuart_console_write(struct console *co, const char *s,
379         unsigned int count)
380 {
381         struct liteuart_port *uart;
382         struct uart_port *port;
383         unsigned long flags;
384
385         uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
386         port = &uart->port;
387
388         spin_lock_irqsave(&port->lock, flags);
389         uart_console_write(port, s, count, liteuart_putchar);
390         spin_unlock_irqrestore(&port->lock, flags);
391 }
392
393 static int liteuart_console_setup(struct console *co, char *options)
394 {
395         struct liteuart_port *uart;
396         struct uart_port *port;
397         int baud = 115200;
398         int bits = 8;
399         int parity = 'n';
400         int flow = 'n';
401
402         uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
403         if (!uart)
404                 return -ENODEV;
405
406         port = &uart->port;
407         if (!port->membase)
408                 return -ENODEV;
409
410         if (options)
411                 uart_parse_options(options, &baud, &parity, &bits, &flow);
412
413         return uart_set_options(port, co, baud, parity, bits, flow);
414 }
415
416 static struct console liteuart_console = {
417         .name = KBUILD_MODNAME,
418         .write = liteuart_console_write,
419         .device = uart_console_device,
420         .setup = liteuart_console_setup,
421         .flags = CON_PRINTBUFFER,
422         .index = -1,
423         .data = &liteuart_driver,
424 };
425
426 static int __init liteuart_console_init(void)
427 {
428         register_console(&liteuart_console);
429
430         return 0;
431 }
432 console_initcall(liteuart_console_init);
433
434 static void early_liteuart_write(struct console *console, const char *s,
435                                     unsigned int count)
436 {
437         struct earlycon_device *device = console->data;
438         struct uart_port *port = &device->port;
439
440         uart_console_write(port, s, count, liteuart_putchar);
441 }
442
443 static int __init early_liteuart_setup(struct earlycon_device *device,
444                                        const char *options)
445 {
446         if (!device->port.membase)
447                 return -ENODEV;
448
449         device->con->write = early_liteuart_write;
450         return 0;
451 }
452
453 OF_EARLYCON_DECLARE(liteuart, "litex,liteuart", early_liteuart_setup);
454 #endif /* CONFIG_SERIAL_LITEUART_CONSOLE */
455
456 static int __init liteuart_init(void)
457 {
458         int res;
459
460         res = uart_register_driver(&liteuart_driver);
461         if (res)
462                 return res;
463
464         res = platform_driver_register(&liteuart_platform_driver);
465         if (res)
466                 uart_unregister_driver(&liteuart_driver);
467
468         return res;
469 }
470
471 static void __exit liteuart_exit(void)
472 {
473         platform_driver_unregister(&liteuart_platform_driver);
474         uart_unregister_driver(&liteuart_driver);
475 }
476
477 module_init(liteuart_init);
478 module_exit(liteuart_exit);
479
480 MODULE_AUTHOR("Antmicro <www.antmicro.com>");
481 MODULE_DESCRIPTION("LiteUART serial driver");
482 MODULE_LICENSE("GPL v2");
483 MODULE_ALIAS("platform:liteuart");