2 * altera_uart.c -- Altera 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/timer.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #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>
29 #include <linux/altera_uart.h>
31 #define DRV_NAME "altera_uart"
32 #define SERIAL_ALTERA_MAJOR 204
33 #define SERIAL_ALTERA_MINOR 213
36 * Altera UART register definitions according to the Nios UART datasheet:
37 * http://www.altera.com/literature/ds/ds_nios_uart.pdf
40 #define ALTERA_UART_SIZE 32
42 #define ALTERA_UART_RXDATA_REG 0
43 #define ALTERA_UART_TXDATA_REG 4
44 #define ALTERA_UART_STATUS_REG 8
45 #define ALTERA_UART_CONTROL_REG 12
46 #define ALTERA_UART_DIVISOR_REG 16
47 #define ALTERA_UART_EOP_REG 20
49 #define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
50 #define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
51 #define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
52 #define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
53 #define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
54 #define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
55 #define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
56 #define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
57 #define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
58 #define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
59 #define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
60 #define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
62 /* Enable interrupt on... */
63 #define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
64 #define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
65 #define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
66 #define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
67 #define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
68 #define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
69 #define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
70 #define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
71 #define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
73 #define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
74 #define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
75 #define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
76 #define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
79 * Local per-uart structure.
82 struct uart_port port
;
83 struct timer_list tmr
;
84 unsigned int sigs
; /* Local copy of line sigs */
85 unsigned short imr
; /* Local IMR mirror */
88 static u32
altera_uart_readl(struct uart_port
*port
, int reg
)
90 return readl(port
->membase
+ (reg
<< port
->regshift
));
93 static void altera_uart_writel(struct uart_port
*port
, u32 dat
, int reg
)
95 writel(dat
, port
->membase
+ (reg
<< port
->regshift
));
98 static unsigned int altera_uart_tx_empty(struct uart_port
*port
)
100 return (altera_uart_readl(port
, ALTERA_UART_STATUS_REG
) &
101 ALTERA_UART_STATUS_TMT_MSK
) ? TIOCSER_TEMT
: 0;
104 static unsigned int altera_uart_get_mctrl(struct uart_port
*port
)
106 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
109 sigs
= (altera_uart_readl(port
, ALTERA_UART_STATUS_REG
) &
110 ALTERA_UART_STATUS_CTS_MSK
) ? TIOCM_CTS
: 0;
111 sigs
|= (pp
->sigs
& TIOCM_RTS
);
116 static void altera_uart_set_mctrl(struct uart_port
*port
, unsigned int sigs
)
118 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
121 if (sigs
& TIOCM_RTS
)
122 pp
->imr
|= ALTERA_UART_CONTROL_RTS_MSK
;
124 pp
->imr
&= ~ALTERA_UART_CONTROL_RTS_MSK
;
125 altera_uart_writel(port
, pp
->imr
, ALTERA_UART_CONTROL_REG
);
128 static void altera_uart_start_tx(struct uart_port
*port
)
130 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
132 pp
->imr
|= ALTERA_UART_CONTROL_TRDY_MSK
;
133 altera_uart_writel(port
, pp
->imr
, ALTERA_UART_CONTROL_REG
);
136 static void altera_uart_stop_tx(struct uart_port
*port
)
138 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
140 pp
->imr
&= ~ALTERA_UART_CONTROL_TRDY_MSK
;
141 altera_uart_writel(port
, pp
->imr
, ALTERA_UART_CONTROL_REG
);
144 static void altera_uart_stop_rx(struct uart_port
*port
)
146 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
148 pp
->imr
&= ~ALTERA_UART_CONTROL_RRDY_MSK
;
149 altera_uart_writel(port
, pp
->imr
, ALTERA_UART_CONTROL_REG
);
152 static void altera_uart_break_ctl(struct uart_port
*port
, int break_state
)
154 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
157 spin_lock_irqsave(&port
->lock
, flags
);
158 if (break_state
== -1)
159 pp
->imr
|= ALTERA_UART_CONTROL_TRBK_MSK
;
161 pp
->imr
&= ~ALTERA_UART_CONTROL_TRBK_MSK
;
162 altera_uart_writel(port
, pp
->imr
, ALTERA_UART_CONTROL_REG
);
163 spin_unlock_irqrestore(&port
->lock
, flags
);
166 static void altera_uart_set_termios(struct uart_port
*port
,
167 struct ktermios
*termios
,
168 struct ktermios
*old
)
171 unsigned int baud
, baudclk
;
173 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 4000000);
174 baudclk
= port
->uartclk
/ baud
;
177 tty_termios_copy_hw(termios
, old
);
178 tty_termios_encode_baud_rate(termios
, baud
, baud
);
180 spin_lock_irqsave(&port
->lock
, flags
);
181 uart_update_timeout(port
, termios
->c_cflag
, baud
);
182 altera_uart_writel(port
, baudclk
, ALTERA_UART_DIVISOR_REG
);
183 spin_unlock_irqrestore(&port
->lock
, flags
);
186 * FIXME: port->read_status_mask and port->ignore_status_mask
187 * need to be initialized based on termios settings for
188 * INPCK, IGNBRK, IGNPAR, PARMRK, BRKINT
192 static void altera_uart_rx_chars(struct altera_uart
*pp
)
194 struct uart_port
*port
= &pp
->port
;
195 unsigned char ch
, flag
;
196 unsigned short status
;
198 while ((status
= altera_uart_readl(port
, ALTERA_UART_STATUS_REG
)) &
199 ALTERA_UART_STATUS_RRDY_MSK
) {
200 ch
= altera_uart_readl(port
, ALTERA_UART_RXDATA_REG
);
204 if (status
& ALTERA_UART_STATUS_E_MSK
) {
205 altera_uart_writel(port
, status
,
206 ALTERA_UART_STATUS_REG
);
208 if (status
& ALTERA_UART_STATUS_BRK_MSK
) {
210 if (uart_handle_break(port
))
212 } else if (status
& ALTERA_UART_STATUS_PE_MSK
) {
213 port
->icount
.parity
++;
214 } else if (status
& ALTERA_UART_STATUS_ROE_MSK
) {
215 port
->icount
.overrun
++;
216 } else if (status
& ALTERA_UART_STATUS_FE_MSK
) {
217 port
->icount
.frame
++;
220 status
&= port
->read_status_mask
;
222 if (status
& ALTERA_UART_STATUS_BRK_MSK
)
224 else if (status
& ALTERA_UART_STATUS_PE_MSK
)
226 else if (status
& ALTERA_UART_STATUS_FE_MSK
)
230 if (uart_handle_sysrq_char(port
, ch
))
232 uart_insert_char(port
, status
, ALTERA_UART_STATUS_ROE_MSK
, ch
,
236 spin_unlock(&port
->lock
);
237 tty_flip_buffer_push(&port
->state
->port
);
238 spin_lock(&port
->lock
);
241 static void altera_uart_tx_chars(struct altera_uart
*pp
)
243 struct uart_port
*port
= &pp
->port
;
244 struct circ_buf
*xmit
= &port
->state
->xmit
;
247 /* Send special char - probably flow control */
248 altera_uart_writel(port
, port
->x_char
, ALTERA_UART_TXDATA_REG
);
254 while (altera_uart_readl(port
, ALTERA_UART_STATUS_REG
) &
255 ALTERA_UART_STATUS_TRDY_MSK
) {
256 if (xmit
->head
== xmit
->tail
)
258 altera_uart_writel(port
, xmit
->buf
[xmit
->tail
],
259 ALTERA_UART_TXDATA_REG
);
260 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
264 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
265 uart_write_wakeup(port
);
267 if (xmit
->head
== xmit
->tail
) {
268 pp
->imr
&= ~ALTERA_UART_CONTROL_TRDY_MSK
;
269 altera_uart_writel(port
, pp
->imr
, ALTERA_UART_CONTROL_REG
);
273 static irqreturn_t
altera_uart_interrupt(int irq
, void *data
)
275 struct uart_port
*port
= data
;
276 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
279 isr
= altera_uart_readl(port
, ALTERA_UART_STATUS_REG
) & pp
->imr
;
281 spin_lock(&port
->lock
);
282 if (isr
& ALTERA_UART_STATUS_RRDY_MSK
)
283 altera_uart_rx_chars(pp
);
284 if (isr
& ALTERA_UART_STATUS_TRDY_MSK
)
285 altera_uart_tx_chars(pp
);
286 spin_unlock(&port
->lock
);
288 return IRQ_RETVAL(isr
);
291 static void altera_uart_timer(unsigned long data
)
293 struct uart_port
*port
= (void *)data
;
294 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
296 altera_uart_interrupt(0, port
);
297 mod_timer(&pp
->tmr
, jiffies
+ uart_poll_timeout(port
));
300 static void altera_uart_config_port(struct uart_port
*port
, int flags
)
302 port
->type
= PORT_ALTERA_UART
;
304 /* Clear mask, so no surprise interrupts. */
305 altera_uart_writel(port
, 0, ALTERA_UART_CONTROL_REG
);
306 /* Clear status register */
307 altera_uart_writel(port
, 0, ALTERA_UART_STATUS_REG
);
310 static int altera_uart_startup(struct uart_port
*port
)
312 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
317 setup_timer(&pp
->tmr
, altera_uart_timer
, (unsigned long)port
);
318 mod_timer(&pp
->tmr
, jiffies
+ uart_poll_timeout(port
));
322 ret
= request_irq(port
->irq
, altera_uart_interrupt
, 0,
325 pr_err(DRV_NAME
": unable to attach Altera UART %d "
326 "interrupt vector=%d\n", port
->line
, port
->irq
);
330 spin_lock_irqsave(&port
->lock
, flags
);
332 /* Enable RX interrupts now */
333 pp
->imr
= ALTERA_UART_CONTROL_RRDY_MSK
;
334 writel(pp
->imr
, port
->membase
+ ALTERA_UART_CONTROL_REG
);
336 spin_unlock_irqrestore(&port
->lock
, flags
);
341 static void altera_uart_shutdown(struct uart_port
*port
)
343 struct altera_uart
*pp
= container_of(port
, struct altera_uart
, port
);
346 spin_lock_irqsave(&port
->lock
, flags
);
348 /* Disable all interrupts now */
350 writel(pp
->imr
, port
->membase
+ ALTERA_UART_CONTROL_REG
);
352 spin_unlock_irqrestore(&port
->lock
, flags
);
355 free_irq(port
->irq
, port
);
357 del_timer_sync(&pp
->tmr
);
360 static const char *altera_uart_type(struct uart_port
*port
)
362 return (port
->type
== PORT_ALTERA_UART
) ? "Altera UART" : NULL
;
365 static int altera_uart_request_port(struct uart_port
*port
)
367 /* UARTs always present */
371 static void altera_uart_release_port(struct uart_port
*port
)
373 /* Nothing to release... */
376 static int altera_uart_verify_port(struct uart_port
*port
,
377 struct serial_struct
*ser
)
379 if ((ser
->type
!= PORT_UNKNOWN
) && (ser
->type
!= PORT_ALTERA_UART
))
384 #ifdef CONFIG_CONSOLE_POLL
385 static int altera_uart_poll_get_char(struct uart_port
*port
)
387 while (!(altera_uart_readl(port
, ALTERA_UART_STATUS_REG
) &
388 ALTERA_UART_STATUS_RRDY_MSK
))
391 return altera_uart_readl(port
, ALTERA_UART_RXDATA_REG
);
394 static void altera_uart_poll_put_char(struct uart_port
*port
, unsigned char c
)
396 while (!(altera_uart_readl(port
, ALTERA_UART_STATUS_REG
) &
397 ALTERA_UART_STATUS_TRDY_MSK
))
400 altera_uart_writel(port
, c
, ALTERA_UART_TXDATA_REG
);
405 * Define the basic serial functions we support.
407 static const struct uart_ops altera_uart_ops
= {
408 .tx_empty
= altera_uart_tx_empty
,
409 .get_mctrl
= altera_uart_get_mctrl
,
410 .set_mctrl
= altera_uart_set_mctrl
,
411 .start_tx
= altera_uart_start_tx
,
412 .stop_tx
= altera_uart_stop_tx
,
413 .stop_rx
= altera_uart_stop_rx
,
414 .break_ctl
= altera_uart_break_ctl
,
415 .startup
= altera_uart_startup
,
416 .shutdown
= altera_uart_shutdown
,
417 .set_termios
= altera_uart_set_termios
,
418 .type
= altera_uart_type
,
419 .request_port
= altera_uart_request_port
,
420 .release_port
= altera_uart_release_port
,
421 .config_port
= altera_uart_config_port
,
422 .verify_port
= altera_uart_verify_port
,
423 #ifdef CONFIG_CONSOLE_POLL
424 .poll_get_char
= altera_uart_poll_get_char
,
425 .poll_put_char
= altera_uart_poll_put_char
,
429 static struct altera_uart altera_uart_ports
[CONFIG_SERIAL_ALTERA_UART_MAXPORTS
];
431 #if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
433 static void altera_uart_console_putc(struct uart_port
*port
, int c
)
435 while (!(altera_uart_readl(port
, ALTERA_UART_STATUS_REG
) &
436 ALTERA_UART_STATUS_TRDY_MSK
))
439 writel(c
, port
->membase
+ ALTERA_UART_TXDATA_REG
);
442 static void altera_uart_console_write(struct console
*co
, const char *s
,
445 struct uart_port
*port
= &(altera_uart_ports
+ co
->index
)->port
;
447 uart_console_write(port
, s
, count
, altera_uart_console_putc
);
450 static int __init
altera_uart_console_setup(struct console
*co
, char *options
)
452 struct uart_port
*port
;
453 int baud
= CONFIG_SERIAL_ALTERA_UART_BAUDRATE
;
458 if (co
->index
< 0 || co
->index
>= CONFIG_SERIAL_ALTERA_UART_MAXPORTS
)
460 port
= &altera_uart_ports
[co
->index
].port
;
465 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
467 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
470 static struct uart_driver altera_uart_driver
;
472 static struct console altera_uart_console
= {
474 .write
= altera_uart_console_write
,
475 .device
= uart_console_device
,
476 .setup
= altera_uart_console_setup
,
477 .flags
= CON_PRINTBUFFER
,
479 .data
= &altera_uart_driver
,
482 static int __init
altera_uart_console_init(void)
484 register_console(&altera_uart_console
);
488 console_initcall(altera_uart_console_init
);
490 #define ALTERA_UART_CONSOLE (&altera_uart_console)
494 #define ALTERA_UART_CONSOLE NULL
496 #endif /* CONFIG_SERIAL_ALTERA_UART_CONSOLE */
499 * Define the altera_uart UART driver structure.
501 static struct uart_driver altera_uart_driver
= {
502 .owner
= THIS_MODULE
,
503 .driver_name
= DRV_NAME
,
505 .major
= SERIAL_ALTERA_MAJOR
,
506 .minor
= SERIAL_ALTERA_MINOR
,
507 .nr
= CONFIG_SERIAL_ALTERA_UART_MAXPORTS
,
508 .cons
= ALTERA_UART_CONSOLE
,
511 static int altera_uart_probe(struct platform_device
*pdev
)
513 struct altera_uart_platform_uart
*platp
= dev_get_platdata(&pdev
->dev
);
514 struct uart_port
*port
;
515 struct resource
*res_mem
;
516 struct resource
*res_irq
;
520 /* if id is -1 scan for a free id and use that one */
522 for (i
= 0; i
< CONFIG_SERIAL_ALTERA_UART_MAXPORTS
; i
++)
523 if (altera_uart_ports
[i
].port
.mapbase
== 0)
527 if (i
< 0 || i
>= CONFIG_SERIAL_ALTERA_UART_MAXPORTS
)
530 port
= &altera_uart_ports
[i
].port
;
532 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
534 port
->mapbase
= res_mem
->start
;
536 port
->mapbase
= platp
->mapbase
;
540 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
542 port
->irq
= res_irq
->start
;
544 port
->irq
= platp
->irq
;
546 /* Check platform data first so we can override device node data */
548 port
->uartclk
= platp
->uartclk
;
550 ret
= of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
556 port
->membase
= ioremap(port
->mapbase
, ALTERA_UART_SIZE
);
561 port
->regshift
= platp
->bus_shift
;
566 port
->type
= PORT_ALTERA_UART
;
567 port
->iotype
= SERIAL_IO_MEM
;
568 port
->ops
= &altera_uart_ops
;
569 port
->flags
= UPF_BOOT_AUTOCONF
;
570 port
->dev
= &pdev
->dev
;
572 platform_set_drvdata(pdev
, port
);
574 uart_add_one_port(&altera_uart_driver
, port
);
579 static int altera_uart_remove(struct platform_device
*pdev
)
581 struct uart_port
*port
= platform_get_drvdata(pdev
);
584 uart_remove_one_port(&altera_uart_driver
, port
);
592 static const struct of_device_id altera_uart_match
[] = {
593 { .compatible
= "ALTR,uart-1.0", },
594 { .compatible
= "altr,uart-1.0", },
597 MODULE_DEVICE_TABLE(of
, altera_uart_match
);
598 #endif /* CONFIG_OF */
600 static struct platform_driver altera_uart_platform_driver
= {
601 .probe
= altera_uart_probe
,
602 .remove
= altera_uart_remove
,
605 .of_match_table
= of_match_ptr(altera_uart_match
),
609 static int __init
altera_uart_init(void)
613 rc
= uart_register_driver(&altera_uart_driver
);
616 rc
= platform_driver_register(&altera_uart_platform_driver
);
618 uart_unregister_driver(&altera_uart_driver
);
622 static void __exit
altera_uart_exit(void)
624 platform_driver_unregister(&altera_uart_platform_driver
);
625 uart_unregister_driver(&altera_uart_driver
);
628 module_init(altera_uart_init
);
629 module_exit(altera_uart_exit
);
631 MODULE_DESCRIPTION("Altera UART driver");
632 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
633 MODULE_LICENSE("GPL");
634 MODULE_ALIAS("platform:" DRV_NAME
);
635 MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_ALTERA_MAJOR
);