1 // SPDX-License-Identifier: GPL-2.0+
3 * altera_jtaguart.c -- Altera JTAG UART driver
5 * Based on mcf.c -- Freescale ColdFire UART driver
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>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/console.h>
18 #include <linux/tty.h>
19 #include <linux/tty_flip.h>
20 #include <linux/serial.h>
21 #include <linux/serial_core.h>
22 #include <linux/platform_device.h>
24 #include <linux/altera_jtaguart.h>
26 #define DRV_NAME "altera_jtaguart"
29 * Altera JTAG UART register definitions according to the Altera JTAG UART
30 * datasheet: https://www.altera.com/literature/hb/nios2/n2cpu_nii51009.pdf
33 #define ALTERA_JTAGUART_SIZE 8
35 #define ALTERA_JTAGUART_DATA_REG 0
37 #define ALTERA_JTAGUART_DATA_DATA_MSK 0x000000FF
38 #define ALTERA_JTAGUART_DATA_RVALID_MSK 0x00008000
39 #define ALTERA_JTAGUART_DATA_RAVAIL_MSK 0xFFFF0000
40 #define ALTERA_JTAGUART_DATA_RAVAIL_OFF 16
42 #define ALTERA_JTAGUART_CONTROL_REG 4
44 #define ALTERA_JTAGUART_CONTROL_RE_MSK 0x00000001
45 #define ALTERA_JTAGUART_CONTROL_WE_MSK 0x00000002
46 #define ALTERA_JTAGUART_CONTROL_RI_MSK 0x00000100
47 #define ALTERA_JTAGUART_CONTROL_RI_OFF 8
48 #define ALTERA_JTAGUART_CONTROL_WI_MSK 0x00000200
49 #define ALTERA_JTAGUART_CONTROL_AC_MSK 0x00000400
50 #define ALTERA_JTAGUART_CONTROL_WSPACE_MSK 0xFFFF0000
51 #define ALTERA_JTAGUART_CONTROL_WSPACE_OFF 16
54 * Local per-uart structure.
56 struct altera_jtaguart {
57 struct uart_port port;
58 unsigned int sigs; /* Local copy of line sigs */
59 unsigned long imr; /* Local IMR mirror */
62 static unsigned int altera_jtaguart_tx_empty(struct uart_port *port)
64 return (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
65 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) ? TIOCSER_TEMT : 0;
68 static unsigned int altera_jtaguart_get_mctrl(struct uart_port *port)
70 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
73 static void altera_jtaguart_set_mctrl(struct uart_port *port, unsigned int sigs)
77 static void altera_jtaguart_start_tx(struct uart_port *port)
79 struct altera_jtaguart *pp =
80 container_of(port, struct altera_jtaguart, port);
82 pp->imr |= ALTERA_JTAGUART_CONTROL_WE_MSK;
83 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
86 static void altera_jtaguart_stop_tx(struct uart_port *port)
88 struct altera_jtaguart *pp =
89 container_of(port, struct altera_jtaguart, port);
91 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
92 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
95 static void altera_jtaguart_stop_rx(struct uart_port *port)
97 struct altera_jtaguart *pp =
98 container_of(port, struct altera_jtaguart, port);
100 pp->imr &= ~ALTERA_JTAGUART_CONTROL_RE_MSK;
101 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
104 static void altera_jtaguart_break_ctl(struct uart_port *port, int break_state)
108 static void altera_jtaguart_set_termios(struct uart_port *port,
109 struct ktermios *termios,
110 struct ktermios *old)
112 /* Just copy the old termios settings back */
114 tty_termios_copy_hw(termios, old);
117 static void altera_jtaguart_rx_chars(struct altera_jtaguart *pp)
119 struct uart_port *port = &pp->port;
120 unsigned char ch, flag;
121 unsigned long status;
123 while ((status = readl(port->membase + ALTERA_JTAGUART_DATA_REG)) &
124 ALTERA_JTAGUART_DATA_RVALID_MSK) {
125 ch = status & ALTERA_JTAGUART_DATA_DATA_MSK;
129 if (uart_handle_sysrq_char(port, ch))
131 uart_insert_char(port, 0, 0, ch, flag);
134 tty_flip_buffer_push(&port->state->port);
137 static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp)
139 struct uart_port *port = &pp->port;
140 struct circ_buf *xmit = &port->state->xmit;
141 unsigned int pending, count;
144 /* Send special char - probably flow control */
145 writel(port->x_char, port->membase + ALTERA_JTAGUART_DATA_REG);
151 pending = uart_circ_chars_pending(xmit);
153 count = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
154 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) >>
155 ALTERA_JTAGUART_CONTROL_WSPACE_OFF;
161 writel(xmit->buf[xmit->tail],
162 port->membase + ALTERA_JTAGUART_DATA_REG);
163 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
166 if (pending < WAKEUP_CHARS)
167 uart_write_wakeup(port);
172 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
173 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
177 static irqreturn_t altera_jtaguart_interrupt(int irq, void *data)
179 struct uart_port *port = data;
180 struct altera_jtaguart *pp =
181 container_of(port, struct altera_jtaguart, port);
184 isr = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) >>
185 ALTERA_JTAGUART_CONTROL_RI_OFF) & pp->imr;
187 spin_lock(&port->lock);
189 if (isr & ALTERA_JTAGUART_CONTROL_RE_MSK)
190 altera_jtaguart_rx_chars(pp);
191 if (isr & ALTERA_JTAGUART_CONTROL_WE_MSK)
192 altera_jtaguart_tx_chars(pp);
194 spin_unlock(&port->lock);
196 return IRQ_RETVAL(isr);
199 static void altera_jtaguart_config_port(struct uart_port *port, int flags)
201 port->type = PORT_ALTERA_JTAGUART;
203 /* Clear mask, so no surprise interrupts. */
204 writel(0, port->membase + ALTERA_JTAGUART_CONTROL_REG);
207 static int altera_jtaguart_startup(struct uart_port *port)
209 struct altera_jtaguart *pp =
210 container_of(port, struct altera_jtaguart, port);
214 ret = request_irq(port->irq, altera_jtaguart_interrupt, 0,
217 pr_err(DRV_NAME ": unable to attach Altera JTAG UART %d "
218 "interrupt vector=%d\n", port->line, port->irq);
222 spin_lock_irqsave(&port->lock, flags);
224 /* Enable RX interrupts now */
225 pp->imr = ALTERA_JTAGUART_CONTROL_RE_MSK;
226 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
228 spin_unlock_irqrestore(&port->lock, flags);
233 static void altera_jtaguart_shutdown(struct uart_port *port)
235 struct altera_jtaguart *pp =
236 container_of(port, struct altera_jtaguart, port);
239 spin_lock_irqsave(&port->lock, flags);
241 /* Disable all interrupts now */
243 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
245 spin_unlock_irqrestore(&port->lock, flags);
247 free_irq(port->irq, port);
250 static const char *altera_jtaguart_type(struct uart_port *port)
252 return (port->type == PORT_ALTERA_JTAGUART) ? "Altera JTAG UART" : NULL;
255 static int altera_jtaguart_request_port(struct uart_port *port)
257 /* UARTs always present */
261 static void altera_jtaguart_release_port(struct uart_port *port)
263 /* Nothing to release... */
266 static int altera_jtaguart_verify_port(struct uart_port *port,
267 struct serial_struct *ser)
269 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ALTERA_JTAGUART)
275 * Define the basic serial functions we support.
277 static const struct uart_ops altera_jtaguart_ops = {
278 .tx_empty = altera_jtaguart_tx_empty,
279 .get_mctrl = altera_jtaguart_get_mctrl,
280 .set_mctrl = altera_jtaguart_set_mctrl,
281 .start_tx = altera_jtaguart_start_tx,
282 .stop_tx = altera_jtaguart_stop_tx,
283 .stop_rx = altera_jtaguart_stop_rx,
284 .break_ctl = altera_jtaguart_break_ctl,
285 .startup = altera_jtaguart_startup,
286 .shutdown = altera_jtaguart_shutdown,
287 .set_termios = altera_jtaguart_set_termios,
288 .type = altera_jtaguart_type,
289 .request_port = altera_jtaguart_request_port,
290 .release_port = altera_jtaguart_release_port,
291 .config_port = altera_jtaguart_config_port,
292 .verify_port = altera_jtaguart_verify_port,
295 #define ALTERA_JTAGUART_MAXPORTS 1
296 static struct altera_jtaguart altera_jtaguart_ports[ALTERA_JTAGUART_MAXPORTS];
298 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
300 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
301 static void altera_jtaguart_console_putc(struct uart_port *port, int c)
303 unsigned long status;
306 spin_lock_irqsave(&port->lock, flags);
307 while (((status = readl(port->membase + ALTERA_JTAGUART_CONTROL_REG)) &
308 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
309 if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0) {
310 spin_unlock_irqrestore(&port->lock, flags);
311 return; /* no connection activity */
313 spin_unlock_irqrestore(&port->lock, flags);
315 spin_lock_irqsave(&port->lock, flags);
317 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
318 spin_unlock_irqrestore(&port->lock, flags);
321 static void altera_jtaguart_console_putc(struct uart_port *port, int c)
325 spin_lock_irqsave(&port->lock, flags);
326 while ((readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
327 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
328 spin_unlock_irqrestore(&port->lock, flags);
330 spin_lock_irqsave(&port->lock, flags);
332 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
333 spin_unlock_irqrestore(&port->lock, flags);
337 static void altera_jtaguart_console_write(struct console *co, const char *s,
340 struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
342 uart_console_write(port, s, count, altera_jtaguart_console_putc);
345 static int __init altera_jtaguart_console_setup(struct console *co,
348 struct uart_port *port;
350 if (co->index < 0 || co->index >= ALTERA_JTAGUART_MAXPORTS)
352 port = &altera_jtaguart_ports[co->index].port;
353 if (port->membase == NULL)
358 static struct uart_driver altera_jtaguart_driver;
360 static struct console altera_jtaguart_console = {
362 .write = altera_jtaguart_console_write,
363 .device = uart_console_device,
364 .setup = altera_jtaguart_console_setup,
365 .flags = CON_PRINTBUFFER,
367 .data = &altera_jtaguart_driver,
370 static int __init altera_jtaguart_console_init(void)
372 register_console(&altera_jtaguart_console);
376 console_initcall(altera_jtaguart_console_init);
378 #define ALTERA_JTAGUART_CONSOLE (&altera_jtaguart_console)
380 static void altera_jtaguart_earlycon_write(struct console *co, const char *s,
383 struct earlycon_device *dev = co->data;
385 uart_console_write(&dev->port, s, count, altera_jtaguart_console_putc);
388 static int __init altera_jtaguart_earlycon_setup(struct earlycon_device *dev,
391 if (!dev->port.membase)
394 dev->con->write = altera_jtaguart_earlycon_write;
398 OF_EARLYCON_DECLARE(juart, "altr,juart-1.0", altera_jtaguart_earlycon_setup);
402 #define ALTERA_JTAGUART_CONSOLE NULL
404 #endif /* CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE */
406 static struct uart_driver altera_jtaguart_driver = {
407 .owner = THIS_MODULE,
408 .driver_name = "altera_jtaguart",
410 .major = ALTERA_JTAGUART_MAJOR,
411 .minor = ALTERA_JTAGUART_MINOR,
412 .nr = ALTERA_JTAGUART_MAXPORTS,
413 .cons = ALTERA_JTAGUART_CONSOLE,
416 static int altera_jtaguart_probe(struct platform_device *pdev)
418 struct altera_jtaguart_platform_uart *platp =
419 dev_get_platdata(&pdev->dev);
420 struct uart_port *port;
421 struct resource *res_irq, *res_mem;
424 /* -1 emphasizes that the platform must have one port, no .N suffix */
428 if (i >= ALTERA_JTAGUART_MAXPORTS)
431 port = &altera_jtaguart_ports[i].port;
433 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
435 port->mapbase = res_mem->start;
437 port->mapbase = platp->mapbase;
441 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
443 port->irq = res_irq->start;
445 port->irq = platp->irq;
449 port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE);
454 port->type = PORT_ALTERA_JTAGUART;
455 port->iotype = SERIAL_IO_MEM;
456 port->ops = &altera_jtaguart_ops;
457 port->flags = UPF_BOOT_AUTOCONF;
458 port->dev = &pdev->dev;
460 uart_add_one_port(&altera_jtaguart_driver, port);
465 static int altera_jtaguart_remove(struct platform_device *pdev)
467 struct uart_port *port;
473 port = &altera_jtaguart_ports[i].port;
474 uart_remove_one_port(&altera_jtaguart_driver, port);
475 iounmap(port->membase);
481 static const struct of_device_id altera_jtaguart_match[] = {
482 { .compatible = "ALTR,juart-1.0", },
483 { .compatible = "altr,juart-1.0", },
486 MODULE_DEVICE_TABLE(of, altera_jtaguart_match);
487 #endif /* CONFIG_OF */
489 static struct platform_driver altera_jtaguart_platform_driver = {
490 .probe = altera_jtaguart_probe,
491 .remove = altera_jtaguart_remove,
494 .of_match_table = of_match_ptr(altera_jtaguart_match),
498 static int __init altera_jtaguart_init(void)
502 rc = uart_register_driver(&altera_jtaguart_driver);
505 rc = platform_driver_register(&altera_jtaguart_platform_driver);
507 uart_unregister_driver(&altera_jtaguart_driver);
511 static void __exit altera_jtaguart_exit(void)
513 platform_driver_unregister(&altera_jtaguart_platform_driver);
514 uart_unregister_driver(&altera_jtaguart_driver);
517 module_init(altera_jtaguart_init);
518 module_exit(altera_jtaguart_exit);
520 MODULE_DESCRIPTION("Altera JTAG UART driver");
521 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
522 MODULE_LICENSE("GPL");
523 MODULE_ALIAS("platform:" DRV_NAME);