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 
12 #include <linux/bitfield.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/console.h>
18 #include <linux/of.h>
19 #include <linux/tty.h>
20 #include <linux/tty_flip.h>
21 #include <linux/serial.h>
22 #include <linux/serial_core.h>
23 #include <linux/platform_device.h>
24 #include <linux/io.h>
25 #include <linux/altera_jtaguart.h>
26 
27 #define DRV_NAME "altera_jtaguart"
28 
29 /*
30  * Altera JTAG UART register definitions according to the Altera JTAG UART
31  * datasheet: https://www.altera.com/literature/hb/nios2/n2cpu_nii51009.pdf
32  */
33 
34 #define ALTERA_JTAGUART_SIZE			8
35 
36 #define ALTERA_JTAGUART_DATA_REG		0
37 
38 #define ALTERA_JTAGUART_DATA_DATA_MSK		0x000000FF
39 #define ALTERA_JTAGUART_DATA_RVALID_MSK		0x00008000
40 #define ALTERA_JTAGUART_DATA_RAVAIL_MSK		0xFFFF0000
41 #define ALTERA_JTAGUART_DATA_RAVAIL_OFF		16
42 
43 #define ALTERA_JTAGUART_CONTROL_REG		4
44 
45 #define ALTERA_JTAGUART_CONTROL_RE_MSK		0x00000001
46 #define ALTERA_JTAGUART_CONTROL_WE_MSK		0x00000002
47 #define ALTERA_JTAGUART_CONTROL_RI_MSK		0x00000100
48 #define ALTERA_JTAGUART_CONTROL_RI_OFF		8
49 #define ALTERA_JTAGUART_CONTROL_WI_MSK		0x00000200
50 #define ALTERA_JTAGUART_CONTROL_AC_MSK		0x00000400
51 #define ALTERA_JTAGUART_CONTROL_WSPACE_MSK	0xFFFF0000
52 
53 /*
54  * Local per-uart structure.
55  */
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 */
60 };
61 
62 static unsigned int altera_jtaguart_tx_space(struct uart_port *port, u32 *ctlp)
63 {
64 	u32 ctl = readl(port->membase + ALTERA_JTAGUART_CONTROL_REG);
65 
66 	if (ctlp)
67 		*ctlp = ctl;
68 
69 	return FIELD_GET(ALTERA_JTAGUART_CONTROL_WSPACE_MSK, ctl);
70 }
71 
72 static unsigned int altera_jtaguart_tx_empty(struct uart_port *port)
73 {
74 	return altera_jtaguart_tx_space(port, NULL) ? TIOCSER_TEMT : 0;
75 }
76 
77 static unsigned int altera_jtaguart_get_mctrl(struct uart_port *port)
78 {
79 	return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
80 }
81 
82 static void altera_jtaguart_set_mctrl(struct uart_port *port, unsigned int sigs)
83 {
84 }
85 
86 static void altera_jtaguart_start_tx(struct uart_port *port)
87 {
88 	struct altera_jtaguart *pp =
89 	    container_of(port, struct altera_jtaguart, port);
90 
91 	pp->imr |= ALTERA_JTAGUART_CONTROL_WE_MSK;
92 	writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
93 }
94 
95 static void altera_jtaguart_stop_tx(struct uart_port *port)
96 {
97 	struct altera_jtaguart *pp =
98 	    container_of(port, struct altera_jtaguart, port);
99 
100 	pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
101 	writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
102 }
103 
104 static void altera_jtaguart_stop_rx(struct uart_port *port)
105 {
106 	struct altera_jtaguart *pp =
107 	    container_of(port, struct altera_jtaguart, port);
108 
109 	pp->imr &= ~ALTERA_JTAGUART_CONTROL_RE_MSK;
110 	writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
111 }
112 
113 static void altera_jtaguart_break_ctl(struct uart_port *port, int break_state)
114 {
115 }
116 
117 static void altera_jtaguart_set_termios(struct uart_port *port,
118 				        struct ktermios *termios,
119 				        const struct ktermios *old)
120 {
121 	/* Just copy the old termios settings back */
122 	if (old)
123 		tty_termios_copy_hw(termios, old);
124 }
125 
126 static void altera_jtaguart_rx_chars(struct altera_jtaguart *pp)
127 {
128 	struct uart_port *port = &pp->port;
129 	unsigned char ch, flag;
130 	unsigned long status;
131 
132 	while ((status = readl(port->membase + ALTERA_JTAGUART_DATA_REG)) &
133 	       ALTERA_JTAGUART_DATA_RVALID_MSK) {
134 		ch = status & ALTERA_JTAGUART_DATA_DATA_MSK;
135 		flag = TTY_NORMAL;
136 		port->icount.rx++;
137 
138 		if (uart_handle_sysrq_char(port, ch))
139 			continue;
140 		uart_insert_char(port, 0, 0, ch, flag);
141 	}
142 
143 	tty_flip_buffer_push(&port->state->port);
144 }
145 
146 static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp)
147 {
148 	struct uart_port *port = &pp->port;
149 	struct circ_buf *xmit = &port->state->xmit;
150 	unsigned int pending, count;
151 
152 	if (port->x_char) {
153 		/* Send special char - probably flow control */
154 		writel(port->x_char, port->membase + ALTERA_JTAGUART_DATA_REG);
155 		port->x_char = 0;
156 		port->icount.tx++;
157 		return;
158 	}
159 
160 	pending = uart_circ_chars_pending(xmit);
161 	if (pending > 0) {
162 		count = altera_jtaguart_tx_space(port, NULL);
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 		altera_jtaguart_stop_tx(port);
180 }
181 
182 static irqreturn_t altera_jtaguart_interrupt(int irq, void *data)
183 {
184 	struct uart_port *port = data;
185 	struct altera_jtaguart *pp =
186 	    container_of(port, struct altera_jtaguart, port);
187 	unsigned int isr;
188 
189 	isr = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) >>
190 	       ALTERA_JTAGUART_CONTROL_RI_OFF) & pp->imr;
191 
192 	spin_lock(&port->lock);
193 
194 	if (isr & ALTERA_JTAGUART_CONTROL_RE_MSK)
195 		altera_jtaguart_rx_chars(pp);
196 	if (isr & ALTERA_JTAGUART_CONTROL_WE_MSK)
197 		altera_jtaguart_tx_chars(pp);
198 
199 	spin_unlock(&port->lock);
200 
201 	return IRQ_RETVAL(isr);
202 }
203 
204 static void altera_jtaguart_config_port(struct uart_port *port, int flags)
205 {
206 	port->type = PORT_ALTERA_JTAGUART;
207 
208 	/* Clear mask, so no surprise interrupts. */
209 	writel(0, port->membase + ALTERA_JTAGUART_CONTROL_REG);
210 }
211 
212 static int altera_jtaguart_startup(struct uart_port *port)
213 {
214 	struct altera_jtaguart *pp =
215 	    container_of(port, struct altera_jtaguart, port);
216 	unsigned long flags;
217 	int ret;
218 
219 	ret = request_irq(port->irq, altera_jtaguart_interrupt, 0,
220 			DRV_NAME, port);
221 	if (ret) {
222 		pr_err(DRV_NAME ": unable to attach Altera JTAG UART %d "
223 		       "interrupt vector=%d\n", port->line, port->irq);
224 		return ret;
225 	}
226 
227 	spin_lock_irqsave(&port->lock, flags);
228 
229 	/* Enable RX interrupts now */
230 	pp->imr = ALTERA_JTAGUART_CONTROL_RE_MSK;
231 	writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
232 
233 	spin_unlock_irqrestore(&port->lock, flags);
234 
235 	return 0;
236 }
237 
238 static void altera_jtaguart_shutdown(struct uart_port *port)
239 {
240 	struct altera_jtaguart *pp =
241 	    container_of(port, struct altera_jtaguart, port);
242 	unsigned long flags;
243 
244 	spin_lock_irqsave(&port->lock, flags);
245 
246 	/* Disable all interrupts now */
247 	pp->imr = 0;
248 	writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
249 
250 	spin_unlock_irqrestore(&port->lock, flags);
251 
252 	free_irq(port->irq, port);
253 }
254 
255 static const char *altera_jtaguart_type(struct uart_port *port)
256 {
257 	return (port->type == PORT_ALTERA_JTAGUART) ? "Altera JTAG UART" : NULL;
258 }
259 
260 static int altera_jtaguart_request_port(struct uart_port *port)
261 {
262 	/* UARTs always present */
263 	return 0;
264 }
265 
266 static void altera_jtaguart_release_port(struct uart_port *port)
267 {
268 	/* Nothing to release... */
269 }
270 
271 static int altera_jtaguart_verify_port(struct uart_port *port,
272 				       struct serial_struct *ser)
273 {
274 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ALTERA_JTAGUART)
275 		return -EINVAL;
276 	return 0;
277 }
278 
279 /*
280  *	Define the basic serial functions we support.
281  */
282 static const struct uart_ops altera_jtaguart_ops = {
283 	.tx_empty	= altera_jtaguart_tx_empty,
284 	.get_mctrl	= altera_jtaguart_get_mctrl,
285 	.set_mctrl	= altera_jtaguart_set_mctrl,
286 	.start_tx	= altera_jtaguart_start_tx,
287 	.stop_tx	= altera_jtaguart_stop_tx,
288 	.stop_rx	= altera_jtaguart_stop_rx,
289 	.break_ctl	= altera_jtaguart_break_ctl,
290 	.startup	= altera_jtaguart_startup,
291 	.shutdown	= altera_jtaguart_shutdown,
292 	.set_termios	= altera_jtaguart_set_termios,
293 	.type		= altera_jtaguart_type,
294 	.request_port	= altera_jtaguart_request_port,
295 	.release_port	= altera_jtaguart_release_port,
296 	.config_port	= altera_jtaguart_config_port,
297 	.verify_port	= altera_jtaguart_verify_port,
298 };
299 
300 #define ALTERA_JTAGUART_MAXPORTS 1
301 static struct altera_jtaguart altera_jtaguart_ports[ALTERA_JTAGUART_MAXPORTS];
302 
303 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
304 
305 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
306 static void altera_jtaguart_console_putc(struct uart_port *port, unsigned char c)
307 {
308 	unsigned long flags;
309 	u32 status;
310 
311 	spin_lock_irqsave(&port->lock, flags);
312 	while (!altera_jtaguart_tx_space(port, &status)) {
313 		spin_unlock_irqrestore(&port->lock, flags);
314 
315 		if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0) {
316 			return;	/* no connection activity */
317 		}
318 
319 		cpu_relax();
320 		spin_lock_irqsave(&port->lock, flags);
321 	}
322 	writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
323 	spin_unlock_irqrestore(&port->lock, flags);
324 }
325 #else
326 static void altera_jtaguart_console_putc(struct uart_port *port, unsigned char c)
327 {
328 	unsigned long flags;
329 
330 	spin_lock_irqsave(&port->lock, flags);
331 	while (!altera_jtaguart_tx_space(port, NULL)) {
332 		spin_unlock_irqrestore(&port->lock, flags);
333 		cpu_relax();
334 		spin_lock_irqsave(&port->lock, flags);
335 	}
336 	writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
337 	spin_unlock_irqrestore(&port->lock, flags);
338 }
339 #endif
340 
341 static void altera_jtaguart_console_write(struct console *co, const char *s,
342 					  unsigned int count)
343 {
344 	struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
345 
346 	uart_console_write(port, s, count, altera_jtaguart_console_putc);
347 }
348 
349 static int __init altera_jtaguart_console_setup(struct console *co,
350 						char *options)
351 {
352 	struct uart_port *port;
353 
354 	if (co->index < 0 || co->index >= ALTERA_JTAGUART_MAXPORTS)
355 		return -EINVAL;
356 	port = &altera_jtaguart_ports[co->index].port;
357 	if (port->membase == NULL)
358 		return -ENODEV;
359 	return 0;
360 }
361 
362 static struct uart_driver altera_jtaguart_driver;
363 
364 static struct console altera_jtaguart_console = {
365 	.name	= "ttyJ",
366 	.write	= altera_jtaguart_console_write,
367 	.device	= uart_console_device,
368 	.setup	= altera_jtaguart_console_setup,
369 	.flags	= CON_PRINTBUFFER,
370 	.index	= -1,
371 	.data	= &altera_jtaguart_driver,
372 };
373 
374 static int __init altera_jtaguart_console_init(void)
375 {
376 	register_console(&altera_jtaguart_console);
377 	return 0;
378 }
379 
380 console_initcall(altera_jtaguart_console_init);
381 
382 #define	ALTERA_JTAGUART_CONSOLE	(&altera_jtaguart_console)
383 
384 static void altera_jtaguart_earlycon_write(struct console *co, const char *s,
385 					   unsigned int count)
386 {
387 	struct earlycon_device *dev = co->data;
388 
389 	uart_console_write(&dev->port, s, count, altera_jtaguart_console_putc);
390 }
391 
392 static int __init altera_jtaguart_earlycon_setup(struct earlycon_device *dev,
393 						 const char *options)
394 {
395 	if (!dev->port.membase)
396 		return -ENODEV;
397 
398 	dev->con->write = altera_jtaguart_earlycon_write;
399 	return 0;
400 }
401 
402 OF_EARLYCON_DECLARE(juart, "altr,juart-1.0", altera_jtaguart_earlycon_setup);
403 
404 #else
405 
406 #define	ALTERA_JTAGUART_CONSOLE	NULL
407 
408 #endif /* CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE */
409 
410 static struct uart_driver altera_jtaguart_driver = {
411 	.owner		= THIS_MODULE,
412 	.driver_name	= "altera_jtaguart",
413 	.dev_name	= "ttyJ",
414 	.major		= ALTERA_JTAGUART_MAJOR,
415 	.minor		= ALTERA_JTAGUART_MINOR,
416 	.nr		= ALTERA_JTAGUART_MAXPORTS,
417 	.cons		= ALTERA_JTAGUART_CONSOLE,
418 };
419 
420 static int altera_jtaguart_probe(struct platform_device *pdev)
421 {
422 	struct altera_jtaguart_platform_uart *platp =
423 			dev_get_platdata(&pdev->dev);
424 	struct uart_port *port;
425 	struct resource *res_mem;
426 	int i = pdev->id;
427 	int irq;
428 
429 	/* -1 emphasizes that the platform must have one port, no .N suffix */
430 	if (i == -1)
431 		i = 0;
432 
433 	if (i >= ALTERA_JTAGUART_MAXPORTS)
434 		return -EINVAL;
435 
436 	port = &altera_jtaguart_ports[i].port;
437 
438 	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
439 	if (res_mem)
440 		port->mapbase = res_mem->start;
441 	else if (platp)
442 		port->mapbase = platp->mapbase;
443 	else
444 		return -ENODEV;
445 
446 	irq = platform_get_irq_optional(pdev, 0);
447 	if (irq < 0 && irq != -ENXIO)
448 		return irq;
449 	if (irq > 0)
450 		port->irq = irq;
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);
531