tty: add SPDX identifiers to all remaining files in drivers/tty/
[linux-2.6-block.git] / drivers / tty / serial / altera_jtaguart.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * altera_jtaguart.c -- Altera JTAG UART driver
4  *
5  * Based on mcf.c -- Freescale ColdFire UART driver
6  *
7  * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
8  * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
9  * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/console.h>
22 #include <linux/of.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial.h>
26 #include <linux/serial_core.h>
27 #include <linux/platform_device.h>
28 #include <linux/io.h>
29 #include <linux/altera_jtaguart.h>
30
31 #define DRV_NAME "altera_jtaguart"
32
33 /*
34  * Altera JTAG UART register definitions according to the Altera JTAG UART
35  * datasheet: http://www.altera.com/literature/hb/nios2/n2cpu_nii51009.pdf
36  */
37
38 #define ALTERA_JTAGUART_SIZE                    8
39
40 #define ALTERA_JTAGUART_DATA_REG                0
41
42 #define ALTERA_JTAGUART_DATA_DATA_MSK           0x000000FF
43 #define ALTERA_JTAGUART_DATA_RVALID_MSK         0x00008000
44 #define ALTERA_JTAGUART_DATA_RAVAIL_MSK         0xFFFF0000
45 #define ALTERA_JTAGUART_DATA_RAVAIL_OFF         16
46
47 #define ALTERA_JTAGUART_CONTROL_REG             4
48
49 #define ALTERA_JTAGUART_CONTROL_RE_MSK          0x00000001
50 #define ALTERA_JTAGUART_CONTROL_WE_MSK          0x00000002
51 #define ALTERA_JTAGUART_CONTROL_RI_MSK          0x00000100
52 #define ALTERA_JTAGUART_CONTROL_RI_OFF          8
53 #define ALTERA_JTAGUART_CONTROL_WI_MSK          0x00000200
54 #define ALTERA_JTAGUART_CONTROL_AC_MSK          0x00000400
55 #define ALTERA_JTAGUART_CONTROL_WSPACE_MSK      0xFFFF0000
56 #define ALTERA_JTAGUART_CONTROL_WSPACE_OFF      16
57
58 /*
59  * Local per-uart structure.
60  */
61 struct altera_jtaguart {
62         struct uart_port port;
63         unsigned int sigs;      /* Local copy of line sigs */
64         unsigned long imr;      /* Local IMR mirror */
65 };
66
67 static unsigned int altera_jtaguart_tx_empty(struct uart_port *port)
68 {
69         return (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
70                 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) ? TIOCSER_TEMT : 0;
71 }
72
73 static unsigned int altera_jtaguart_get_mctrl(struct uart_port *port)
74 {
75         return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
76 }
77
78 static void altera_jtaguart_set_mctrl(struct uart_port *port, unsigned int sigs)
79 {
80 }
81
82 static void altera_jtaguart_start_tx(struct uart_port *port)
83 {
84         struct altera_jtaguart *pp =
85             container_of(port, struct altera_jtaguart, port);
86
87         pp->imr |= ALTERA_JTAGUART_CONTROL_WE_MSK;
88         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
89 }
90
91 static void altera_jtaguart_stop_tx(struct uart_port *port)
92 {
93         struct altera_jtaguart *pp =
94             container_of(port, struct altera_jtaguart, port);
95
96         pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
97         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
98 }
99
100 static void altera_jtaguart_stop_rx(struct uart_port *port)
101 {
102         struct altera_jtaguart *pp =
103             container_of(port, struct altera_jtaguart, port);
104
105         pp->imr &= ~ALTERA_JTAGUART_CONTROL_RE_MSK;
106         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
107 }
108
109 static void altera_jtaguart_break_ctl(struct uart_port *port, int break_state)
110 {
111 }
112
113 static void altera_jtaguart_set_termios(struct uart_port *port,
114                                         struct ktermios *termios,
115                                         struct ktermios *old)
116 {
117         /* Just copy the old termios settings back */
118         if (old)
119                 tty_termios_copy_hw(termios, old);
120 }
121
122 static void altera_jtaguart_rx_chars(struct altera_jtaguart *pp)
123 {
124         struct uart_port *port = &pp->port;
125         unsigned char ch, flag;
126         unsigned long status;
127
128         while ((status = readl(port->membase + ALTERA_JTAGUART_DATA_REG)) &
129                ALTERA_JTAGUART_DATA_RVALID_MSK) {
130                 ch = status & ALTERA_JTAGUART_DATA_DATA_MSK;
131                 flag = TTY_NORMAL;
132                 port->icount.rx++;
133
134                 if (uart_handle_sysrq_char(port, ch))
135                         continue;
136                 uart_insert_char(port, 0, 0, ch, flag);
137         }
138
139         spin_unlock(&port->lock);
140         tty_flip_buffer_push(&port->state->port);
141         spin_lock(&port->lock);
142 }
143
144 static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp)
145 {
146         struct uart_port *port = &pp->port;
147         struct circ_buf *xmit = &port->state->xmit;
148         unsigned int pending, count;
149
150         if (port->x_char) {
151                 /* Send special char - probably flow control */
152                 writel(port->x_char, port->membase + ALTERA_JTAGUART_DATA_REG);
153                 port->x_char = 0;
154                 port->icount.tx++;
155                 return;
156         }
157
158         pending = uart_circ_chars_pending(xmit);
159         if (pending > 0) {
160                 count = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
161                                 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) >>
162                         ALTERA_JTAGUART_CONTROL_WSPACE_OFF;
163                 if (count > pending)
164                         count = pending;
165                 if (count > 0) {
166                         pending -= count;
167                         while (count--) {
168                                 writel(xmit->buf[xmit->tail],
169                                        port->membase + ALTERA_JTAGUART_DATA_REG);
170                                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
171                                 port->icount.tx++;
172                         }
173                         if (pending < WAKEUP_CHARS)
174                                 uart_write_wakeup(port);
175                 }
176         }
177
178         if (pending == 0) {
179                 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
180                 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
181         }
182 }
183
184 static irqreturn_t altera_jtaguart_interrupt(int irq, void *data)
185 {
186         struct uart_port *port = data;
187         struct altera_jtaguart *pp =
188             container_of(port, struct altera_jtaguart, port);
189         unsigned int isr;
190
191         isr = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) >>
192                ALTERA_JTAGUART_CONTROL_RI_OFF) & pp->imr;
193
194         spin_lock(&port->lock);
195
196         if (isr & ALTERA_JTAGUART_CONTROL_RE_MSK)
197                 altera_jtaguart_rx_chars(pp);
198         if (isr & ALTERA_JTAGUART_CONTROL_WE_MSK)
199                 altera_jtaguart_tx_chars(pp);
200
201         spin_unlock(&port->lock);
202
203         return IRQ_RETVAL(isr);
204 }
205
206 static void altera_jtaguart_config_port(struct uart_port *port, int flags)
207 {
208         port->type = PORT_ALTERA_JTAGUART;
209
210         /* Clear mask, so no surprise interrupts. */
211         writel(0, port->membase + ALTERA_JTAGUART_CONTROL_REG);
212 }
213
214 static int altera_jtaguart_startup(struct uart_port *port)
215 {
216         struct altera_jtaguart *pp =
217             container_of(port, struct altera_jtaguart, port);
218         unsigned long flags;
219         int ret;
220
221         ret = request_irq(port->irq, altera_jtaguart_interrupt, 0,
222                         DRV_NAME, port);
223         if (ret) {
224                 pr_err(DRV_NAME ": unable to attach Altera JTAG UART %d "
225                        "interrupt vector=%d\n", port->line, port->irq);
226                 return ret;
227         }
228
229         spin_lock_irqsave(&port->lock, flags);
230
231         /* Enable RX interrupts now */
232         pp->imr = ALTERA_JTAGUART_CONTROL_RE_MSK;
233         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
234
235         spin_unlock_irqrestore(&port->lock, flags);
236
237         return 0;
238 }
239
240 static void altera_jtaguart_shutdown(struct uart_port *port)
241 {
242         struct altera_jtaguart *pp =
243             container_of(port, struct altera_jtaguart, port);
244         unsigned long flags;
245
246         spin_lock_irqsave(&port->lock, flags);
247
248         /* Disable all interrupts now */
249         pp->imr = 0;
250         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
251
252         spin_unlock_irqrestore(&port->lock, flags);
253
254         free_irq(port->irq, port);
255 }
256
257 static const char *altera_jtaguart_type(struct uart_port *port)
258 {
259         return (port->type == PORT_ALTERA_JTAGUART) ? "Altera JTAG UART" : NULL;
260 }
261
262 static int altera_jtaguart_request_port(struct uart_port *port)
263 {
264         /* UARTs always present */
265         return 0;
266 }
267
268 static void altera_jtaguart_release_port(struct uart_port *port)
269 {
270         /* Nothing to release... */
271 }
272
273 static int altera_jtaguart_verify_port(struct uart_port *port,
274                                        struct serial_struct *ser)
275 {
276         if (ser->type != PORT_UNKNOWN && ser->type != PORT_ALTERA_JTAGUART)
277                 return -EINVAL;
278         return 0;
279 }
280
281 /*
282  *      Define the basic serial functions we support.
283  */
284 static const struct uart_ops altera_jtaguart_ops = {
285         .tx_empty       = altera_jtaguart_tx_empty,
286         .get_mctrl      = altera_jtaguart_get_mctrl,
287         .set_mctrl      = altera_jtaguart_set_mctrl,
288         .start_tx       = altera_jtaguart_start_tx,
289         .stop_tx        = altera_jtaguart_stop_tx,
290         .stop_rx        = altera_jtaguart_stop_rx,
291         .break_ctl      = altera_jtaguart_break_ctl,
292         .startup        = altera_jtaguart_startup,
293         .shutdown       = altera_jtaguart_shutdown,
294         .set_termios    = altera_jtaguart_set_termios,
295         .type           = altera_jtaguart_type,
296         .request_port   = altera_jtaguart_request_port,
297         .release_port   = altera_jtaguart_release_port,
298         .config_port    = altera_jtaguart_config_port,
299         .verify_port    = altera_jtaguart_verify_port,
300 };
301
302 #define ALTERA_JTAGUART_MAXPORTS 1
303 static struct altera_jtaguart altera_jtaguart_ports[ALTERA_JTAGUART_MAXPORTS];
304
305 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
306
307 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
308 static void altera_jtaguart_console_putc(struct uart_port *port, int c)
309 {
310         unsigned long status;
311         unsigned long flags;
312
313         spin_lock_irqsave(&port->lock, flags);
314         while (((status = readl(port->membase + ALTERA_JTAGUART_CONTROL_REG)) &
315                 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
316                 if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0) {
317                         spin_unlock_irqrestore(&port->lock, flags);
318                         return; /* no connection activity */
319                 }
320                 spin_unlock_irqrestore(&port->lock, flags);
321                 cpu_relax();
322                 spin_lock_irqsave(&port->lock, flags);
323         }
324         writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
325         spin_unlock_irqrestore(&port->lock, flags);
326 }
327 #else
328 static void altera_jtaguart_console_putc(struct uart_port *port, int c)
329 {
330         unsigned long flags;
331
332         spin_lock_irqsave(&port->lock, flags);
333         while ((readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
334                 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
335                 spin_unlock_irqrestore(&port->lock, flags);
336                 cpu_relax();
337                 spin_lock_irqsave(&port->lock, flags);
338         }
339         writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
340         spin_unlock_irqrestore(&port->lock, flags);
341 }
342 #endif
343
344 static void altera_jtaguart_console_write(struct console *co, const char *s,
345                                           unsigned int count)
346 {
347         struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
348
349         uart_console_write(port, s, count, altera_jtaguart_console_putc);
350 }
351
352 static int __init altera_jtaguart_console_setup(struct console *co,
353                                                 char *options)
354 {
355         struct uart_port *port;
356
357         if (co->index < 0 || co->index >= ALTERA_JTAGUART_MAXPORTS)
358                 return -EINVAL;
359         port = &altera_jtaguart_ports[co->index].port;
360         if (port->membase == NULL)
361                 return -ENODEV;
362         return 0;
363 }
364
365 static struct uart_driver altera_jtaguart_driver;
366
367 static struct console altera_jtaguart_console = {
368         .name   = "ttyJ",
369         .write  = altera_jtaguart_console_write,
370         .device = uart_console_device,
371         .setup  = altera_jtaguart_console_setup,
372         .flags  = CON_PRINTBUFFER,
373         .index  = -1,
374         .data   = &altera_jtaguart_driver,
375 };
376
377 static int __init altera_jtaguart_console_init(void)
378 {
379         register_console(&altera_jtaguart_console);
380         return 0;
381 }
382
383 console_initcall(altera_jtaguart_console_init);
384
385 #define ALTERA_JTAGUART_CONSOLE (&altera_jtaguart_console)
386
387 static void altera_jtaguart_earlycon_write(struct console *co, const char *s,
388                                            unsigned int count)
389 {
390         struct earlycon_device *dev = co->data;
391
392         uart_console_write(&dev->port, s, count, altera_jtaguart_console_putc);
393 }
394
395 static int __init altera_jtaguart_earlycon_setup(struct earlycon_device *dev,
396                                                  const char *options)
397 {
398         if (!dev->port.membase)
399                 return -ENODEV;
400
401         dev->con->write = altera_jtaguart_earlycon_write;
402         return 0;
403 }
404
405 OF_EARLYCON_DECLARE(juart, "altr,juart-1.0", altera_jtaguart_earlycon_setup);
406
407 #else
408
409 #define ALTERA_JTAGUART_CONSOLE NULL
410
411 #endif /* CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE */
412
413 static struct uart_driver altera_jtaguart_driver = {
414         .owner          = THIS_MODULE,
415         .driver_name    = "altera_jtaguart",
416         .dev_name       = "ttyJ",
417         .major          = ALTERA_JTAGUART_MAJOR,
418         .minor          = ALTERA_JTAGUART_MINOR,
419         .nr             = ALTERA_JTAGUART_MAXPORTS,
420         .cons           = ALTERA_JTAGUART_CONSOLE,
421 };
422
423 static int altera_jtaguart_probe(struct platform_device *pdev)
424 {
425         struct altera_jtaguart_platform_uart *platp =
426                         dev_get_platdata(&pdev->dev);
427         struct uart_port *port;
428         struct resource *res_irq, *res_mem;
429         int i = pdev->id;
430
431         /* -1 emphasizes that the platform must have one port, no .N suffix */
432         if (i == -1)
433                 i = 0;
434
435         if (i >= ALTERA_JTAGUART_MAXPORTS)
436                 return -EINVAL;
437
438         port = &altera_jtaguart_ports[i].port;
439
440         res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
441         if (res_mem)
442                 port->mapbase = res_mem->start;
443         else if (platp)
444                 port->mapbase = platp->mapbase;
445         else
446                 return -ENODEV;
447
448         res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
449         if (res_irq)
450                 port->irq = res_irq->start;
451         else if (platp)
452                 port->irq = platp->irq;
453         else
454                 return -ENODEV;
455
456         port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE);
457         if (!port->membase)
458                 return -ENOMEM;
459
460         port->line = i;
461         port->type = PORT_ALTERA_JTAGUART;
462         port->iotype = SERIAL_IO_MEM;
463         port->ops = &altera_jtaguart_ops;
464         port->flags = UPF_BOOT_AUTOCONF;
465         port->dev = &pdev->dev;
466
467         uart_add_one_port(&altera_jtaguart_driver, port);
468
469         return 0;
470 }
471
472 static int altera_jtaguart_remove(struct platform_device *pdev)
473 {
474         struct uart_port *port;
475         int i = pdev->id;
476
477         if (i == -1)
478                 i = 0;
479
480         port = &altera_jtaguart_ports[i].port;
481         uart_remove_one_port(&altera_jtaguart_driver, port);
482         iounmap(port->membase);
483
484         return 0;
485 }
486
487 #ifdef CONFIG_OF
488 static const struct of_device_id altera_jtaguart_match[] = {
489         { .compatible = "ALTR,juart-1.0", },
490         { .compatible = "altr,juart-1.0", },
491         {},
492 };
493 MODULE_DEVICE_TABLE(of, altera_jtaguart_match);
494 #endif /* CONFIG_OF */
495
496 static struct platform_driver altera_jtaguart_platform_driver = {
497         .probe  = altera_jtaguart_probe,
498         .remove = altera_jtaguart_remove,
499         .driver = {
500                 .name           = DRV_NAME,
501                 .of_match_table = of_match_ptr(altera_jtaguart_match),
502         },
503 };
504
505 static int __init altera_jtaguart_init(void)
506 {
507         int rc;
508
509         rc = uart_register_driver(&altera_jtaguart_driver);
510         if (rc)
511                 return rc;
512         rc = platform_driver_register(&altera_jtaguart_platform_driver);
513         if (rc)
514                 uart_unregister_driver(&altera_jtaguart_driver);
515         return rc;
516 }
517
518 static void __exit altera_jtaguart_exit(void)
519 {
520         platform_driver_unregister(&altera_jtaguart_platform_driver);
521         uart_unregister_driver(&altera_jtaguart_driver);
522 }
523
524 module_init(altera_jtaguart_init);
525 module_exit(altera_jtaguart_exit);
526
527 MODULE_DESCRIPTION("Altera JTAG UART driver");
528 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
529 MODULE_LICENSE("GPL");
530 MODULE_ALIAS("platform:" DRV_NAME);