2 * altera_jtaguart.c -- Altera JTAG UART driver
4 * Based on mcf.c -- Freescale ColdFire UART driver
6 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
7 * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
8 * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/console.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
24 #include <linux/serial.h>
25 #include <linux/serial_core.h>
26 #include <linux/platform_device.h>
28 #include <linux/altera_jtaguart.h>
30 #define DRV_NAME "altera_jtaguart"
33 * Altera JTAG UART register definitions according to the Altera JTAG UART
34 * datasheet: http://www.altera.com/literature/hb/nios2/n2cpu_nii51009.pdf
37 #define ALTERA_JTAGUART_SIZE 8
39 #define ALTERA_JTAGUART_DATA_REG 0
41 #define ALTERA_JTAGUART_DATA_DATA_MSK 0x000000FF
42 #define ALTERA_JTAGUART_DATA_RVALID_MSK 0x00008000
43 #define ALTERA_JTAGUART_DATA_RAVAIL_MSK 0xFFFF0000
44 #define ALTERA_JTAGUART_DATA_RAVAIL_OFF 16
46 #define ALTERA_JTAGUART_CONTROL_REG 4
48 #define ALTERA_JTAGUART_CONTROL_RE_MSK 0x00000001
49 #define ALTERA_JTAGUART_CONTROL_WE_MSK 0x00000002
50 #define ALTERA_JTAGUART_CONTROL_RI_MSK 0x00000100
51 #define ALTERA_JTAGUART_CONTROL_RI_OFF 8
52 #define ALTERA_JTAGUART_CONTROL_WI_MSK 0x00000200
53 #define ALTERA_JTAGUART_CONTROL_AC_MSK 0x00000400
54 #define ALTERA_JTAGUART_CONTROL_WSPACE_MSK 0xFFFF0000
55 #define ALTERA_JTAGUART_CONTROL_WSPACE_OFF 16
58 * Local per-uart structure.
60 struct altera_jtaguart
{
61 struct uart_port port
;
62 unsigned int sigs
; /* Local copy of line sigs */
63 unsigned long imr
; /* Local IMR mirror */
66 static unsigned int altera_jtaguart_tx_empty(struct uart_port
*port
)
68 return (readl(port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
) &
69 ALTERA_JTAGUART_CONTROL_WSPACE_MSK
) ? TIOCSER_TEMT
: 0;
72 static unsigned int altera_jtaguart_get_mctrl(struct uart_port
*port
)
74 return TIOCM_CAR
| TIOCM_DSR
| TIOCM_CTS
;
77 static void altera_jtaguart_set_mctrl(struct uart_port
*port
, unsigned int sigs
)
81 static void altera_jtaguart_start_tx(struct uart_port
*port
)
83 struct altera_jtaguart
*pp
=
84 container_of(port
, struct altera_jtaguart
, port
);
86 pp
->imr
|= ALTERA_JTAGUART_CONTROL_WE_MSK
;
87 writel(pp
->imr
, port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
);
90 static void altera_jtaguart_stop_tx(struct uart_port
*port
)
92 struct altera_jtaguart
*pp
=
93 container_of(port
, struct altera_jtaguart
, port
);
95 pp
->imr
&= ~ALTERA_JTAGUART_CONTROL_WE_MSK
;
96 writel(pp
->imr
, port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
);
99 static void altera_jtaguart_stop_rx(struct uart_port
*port
)
101 struct altera_jtaguart
*pp
=
102 container_of(port
, struct altera_jtaguart
, port
);
104 pp
->imr
&= ~ALTERA_JTAGUART_CONTROL_RE_MSK
;
105 writel(pp
->imr
, port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
);
108 static void altera_jtaguart_break_ctl(struct uart_port
*port
, int break_state
)
112 static void altera_jtaguart_set_termios(struct uart_port
*port
,
113 struct ktermios
*termios
,
114 struct ktermios
*old
)
116 /* Just copy the old termios settings back */
118 tty_termios_copy_hw(termios
, old
);
121 static void altera_jtaguart_rx_chars(struct altera_jtaguart
*pp
)
123 struct uart_port
*port
= &pp
->port
;
124 unsigned char ch
, flag
;
125 unsigned long status
;
127 while ((status
= readl(port
->membase
+ ALTERA_JTAGUART_DATA_REG
)) &
128 ALTERA_JTAGUART_DATA_RVALID_MSK
) {
129 ch
= status
& ALTERA_JTAGUART_DATA_DATA_MSK
;
133 if (uart_handle_sysrq_char(port
, ch
))
135 uart_insert_char(port
, 0, 0, ch
, flag
);
138 spin_unlock(&port
->lock
);
139 tty_flip_buffer_push(&port
->state
->port
);
140 spin_lock(&port
->lock
);
143 static void altera_jtaguart_tx_chars(struct altera_jtaguart
*pp
)
145 struct uart_port
*port
= &pp
->port
;
146 struct circ_buf
*xmit
= &port
->state
->xmit
;
147 unsigned int pending
, count
;
150 /* Send special char - probably flow control */
151 writel(port
->x_char
, port
->membase
+ ALTERA_JTAGUART_DATA_REG
);
157 pending
= uart_circ_chars_pending(xmit
);
159 count
= (readl(port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
) &
160 ALTERA_JTAGUART_CONTROL_WSPACE_MSK
) >>
161 ALTERA_JTAGUART_CONTROL_WSPACE_OFF
;
167 writel(xmit
->buf
[xmit
->tail
],
168 port
->membase
+ ALTERA_JTAGUART_DATA_REG
);
169 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
172 if (pending
< WAKEUP_CHARS
)
173 uart_write_wakeup(port
);
178 pp
->imr
&= ~ALTERA_JTAGUART_CONTROL_WE_MSK
;
179 writel(pp
->imr
, port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
);
183 static irqreturn_t
altera_jtaguart_interrupt(int irq
, void *data
)
185 struct uart_port
*port
= data
;
186 struct altera_jtaguart
*pp
=
187 container_of(port
, struct altera_jtaguart
, port
);
190 isr
= (readl(port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
) >>
191 ALTERA_JTAGUART_CONTROL_RI_OFF
) & pp
->imr
;
193 spin_lock(&port
->lock
);
195 if (isr
& ALTERA_JTAGUART_CONTROL_RE_MSK
)
196 altera_jtaguart_rx_chars(pp
);
197 if (isr
& ALTERA_JTAGUART_CONTROL_WE_MSK
)
198 altera_jtaguart_tx_chars(pp
);
200 spin_unlock(&port
->lock
);
202 return IRQ_RETVAL(isr
);
205 static void altera_jtaguart_config_port(struct uart_port
*port
, int flags
)
207 port
->type
= PORT_ALTERA_JTAGUART
;
209 /* Clear mask, so no surprise interrupts. */
210 writel(0, port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
);
213 static int altera_jtaguart_startup(struct uart_port
*port
)
215 struct altera_jtaguart
*pp
=
216 container_of(port
, struct altera_jtaguart
, port
);
220 ret
= request_irq(port
->irq
, altera_jtaguart_interrupt
, 0,
223 pr_err(DRV_NAME
": unable to attach Altera JTAG UART %d "
224 "interrupt vector=%d\n", port
->line
, port
->irq
);
228 spin_lock_irqsave(&port
->lock
, flags
);
230 /* Enable RX interrupts now */
231 pp
->imr
= ALTERA_JTAGUART_CONTROL_RE_MSK
;
232 writel(pp
->imr
, port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
);
234 spin_unlock_irqrestore(&port
->lock
, flags
);
239 static void altera_jtaguart_shutdown(struct uart_port
*port
)
241 struct altera_jtaguart
*pp
=
242 container_of(port
, struct altera_jtaguart
, port
);
245 spin_lock_irqsave(&port
->lock
, flags
);
247 /* Disable all interrupts now */
249 writel(pp
->imr
, port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
);
251 spin_unlock_irqrestore(&port
->lock
, flags
);
253 free_irq(port
->irq
, port
);
256 static const char *altera_jtaguart_type(struct uart_port
*port
)
258 return (port
->type
== PORT_ALTERA_JTAGUART
) ? "Altera JTAG UART" : NULL
;
261 static int altera_jtaguart_request_port(struct uart_port
*port
)
263 /* UARTs always present */
267 static void altera_jtaguart_release_port(struct uart_port
*port
)
269 /* Nothing to release... */
272 static int altera_jtaguart_verify_port(struct uart_port
*port
,
273 struct serial_struct
*ser
)
275 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_ALTERA_JTAGUART
)
281 * Define the basic serial functions we support.
283 static const struct uart_ops altera_jtaguart_ops
= {
284 .tx_empty
= altera_jtaguart_tx_empty
,
285 .get_mctrl
= altera_jtaguart_get_mctrl
,
286 .set_mctrl
= altera_jtaguart_set_mctrl
,
287 .start_tx
= altera_jtaguart_start_tx
,
288 .stop_tx
= altera_jtaguart_stop_tx
,
289 .stop_rx
= altera_jtaguart_stop_rx
,
290 .break_ctl
= altera_jtaguart_break_ctl
,
291 .startup
= altera_jtaguart_startup
,
292 .shutdown
= altera_jtaguart_shutdown
,
293 .set_termios
= altera_jtaguart_set_termios
,
294 .type
= altera_jtaguart_type
,
295 .request_port
= altera_jtaguart_request_port
,
296 .release_port
= altera_jtaguart_release_port
,
297 .config_port
= altera_jtaguart_config_port
,
298 .verify_port
= altera_jtaguart_verify_port
,
301 #define ALTERA_JTAGUART_MAXPORTS 1
302 static struct altera_jtaguart altera_jtaguart_ports
[ALTERA_JTAGUART_MAXPORTS
];
304 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
306 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
307 static void altera_jtaguart_console_putc(struct uart_port
*port
, int c
)
309 unsigned long status
;
312 spin_lock_irqsave(&port
->lock
, flags
);
313 while (((status
= readl(port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
)) &
314 ALTERA_JTAGUART_CONTROL_WSPACE_MSK
) == 0) {
315 if ((status
& ALTERA_JTAGUART_CONTROL_AC_MSK
) == 0) {
316 spin_unlock_irqrestore(&port
->lock
, flags
);
317 return; /* no connection activity */
319 spin_unlock_irqrestore(&port
->lock
, flags
);
321 spin_lock_irqsave(&port
->lock
, flags
);
323 writel(c
, port
->membase
+ ALTERA_JTAGUART_DATA_REG
);
324 spin_unlock_irqrestore(&port
->lock
, flags
);
327 static void altera_jtaguart_console_putc(struct uart_port
*port
, int c
)
331 spin_lock_irqsave(&port
->lock
, flags
);
332 while ((readl(port
->membase
+ ALTERA_JTAGUART_CONTROL_REG
) &
333 ALTERA_JTAGUART_CONTROL_WSPACE_MSK
) == 0) {
334 spin_unlock_irqrestore(&port
->lock
, flags
);
336 spin_lock_irqsave(&port
->lock
, flags
);
338 writel(c
, port
->membase
+ ALTERA_JTAGUART_DATA_REG
);
339 spin_unlock_irqrestore(&port
->lock
, flags
);
343 static void altera_jtaguart_console_write(struct console
*co
, const char *s
,
346 struct uart_port
*port
= &(altera_jtaguart_ports
+ co
->index
)->port
;
348 uart_console_write(port
, s
, count
, altera_jtaguart_console_putc
);
351 static int __init
altera_jtaguart_console_setup(struct console
*co
,
354 struct uart_port
*port
;
356 if (co
->index
< 0 || co
->index
>= ALTERA_JTAGUART_MAXPORTS
)
358 port
= &altera_jtaguart_ports
[co
->index
].port
;
359 if (port
->membase
== NULL
)
364 static struct uart_driver altera_jtaguart_driver
;
366 static struct console altera_jtaguart_console
= {
368 .write
= altera_jtaguart_console_write
,
369 .device
= uart_console_device
,
370 .setup
= altera_jtaguart_console_setup
,
371 .flags
= CON_PRINTBUFFER
,
373 .data
= &altera_jtaguart_driver
,
376 static int __init
altera_jtaguart_console_init(void)
378 register_console(&altera_jtaguart_console
);
382 console_initcall(altera_jtaguart_console_init
);
384 #define ALTERA_JTAGUART_CONSOLE (&altera_jtaguart_console)
386 static void altera_jtaguart_earlycon_write(struct console
*co
, const char *s
,
389 struct earlycon_device
*dev
= co
->data
;
391 uart_console_write(&dev
->port
, s
, count
, altera_jtaguart_console_putc
);
394 static int __init
altera_jtaguart_earlycon_setup(struct earlycon_device
*dev
,
397 if (!dev
->port
.membase
)
400 dev
->con
->write
= altera_jtaguart_earlycon_write
;
404 OF_EARLYCON_DECLARE(juart
, "altr,juart-1.0", altera_jtaguart_earlycon_setup
);
408 #define ALTERA_JTAGUART_CONSOLE NULL
410 #endif /* CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE */
412 static struct uart_driver altera_jtaguart_driver
= {
413 .owner
= THIS_MODULE
,
414 .driver_name
= "altera_jtaguart",
416 .major
= ALTERA_JTAGUART_MAJOR
,
417 .minor
= ALTERA_JTAGUART_MINOR
,
418 .nr
= ALTERA_JTAGUART_MAXPORTS
,
419 .cons
= ALTERA_JTAGUART_CONSOLE
,
422 static int altera_jtaguart_probe(struct platform_device
*pdev
)
424 struct altera_jtaguart_platform_uart
*platp
=
425 dev_get_platdata(&pdev
->dev
);
426 struct uart_port
*port
;
427 struct resource
*res_irq
, *res_mem
;
430 /* -1 emphasizes that the platform must have one port, no .N suffix */
434 if (i
>= ALTERA_JTAGUART_MAXPORTS
)
437 port
= &altera_jtaguart_ports
[i
].port
;
439 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
441 port
->mapbase
= res_mem
->start
;
443 port
->mapbase
= platp
->mapbase
;
447 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
449 port
->irq
= res_irq
->start
;
451 port
->irq
= platp
->irq
;
455 port
->membase
= ioremap(port
->mapbase
, ALTERA_JTAGUART_SIZE
);
460 port
->type
= PORT_ALTERA_JTAGUART
;
461 port
->iotype
= SERIAL_IO_MEM
;
462 port
->ops
= &altera_jtaguart_ops
;
463 port
->flags
= UPF_BOOT_AUTOCONF
;
464 port
->dev
= &pdev
->dev
;
466 uart_add_one_port(&altera_jtaguart_driver
, port
);
471 static int altera_jtaguart_remove(struct platform_device
*pdev
)
473 struct uart_port
*port
;
479 port
= &altera_jtaguart_ports
[i
].port
;
480 uart_remove_one_port(&altera_jtaguart_driver
, port
);
486 static const struct of_device_id altera_jtaguart_match
[] = {
487 { .compatible
= "ALTR,juart-1.0", },
488 { .compatible
= "altr,juart-1.0", },
491 MODULE_DEVICE_TABLE(of
, altera_jtaguart_match
);
492 #endif /* CONFIG_OF */
494 static struct platform_driver altera_jtaguart_platform_driver
= {
495 .probe
= altera_jtaguart_probe
,
496 .remove
= altera_jtaguart_remove
,
499 .of_match_table
= of_match_ptr(altera_jtaguart_match
),
503 static int __init
altera_jtaguart_init(void)
507 rc
= uart_register_driver(&altera_jtaguart_driver
);
510 rc
= platform_driver_register(&altera_jtaguart_platform_driver
);
512 uart_unregister_driver(&altera_jtaguart_driver
);
516 static void __exit
altera_jtaguart_exit(void)
518 platform_driver_unregister(&altera_jtaguart_platform_driver
);
519 uart_unregister_driver(&altera_jtaguart_driver
);
522 module_init(altera_jtaguart_init
);
523 module_exit(altera_jtaguart_exit
);
525 MODULE_DESCRIPTION("Altera JTAG UART driver");
526 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
527 MODULE_LICENSE("GPL");
528 MODULE_ALIAS("platform:" DRV_NAME
);