1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 #if defined(CONFIG_SERIAL_NETX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/ioport.h>
13 #include <linux/init.h>
14 #include <linux/console.h>
15 #include <linux/sysrq.h>
16 #include <linux/platform_device.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/serial_core.h>
20 #include <linux/serial.h>
24 #include <mach/hardware.h>
25 #include <mach/netx-regs.h>
27 /* We've been assigned a range on the "Low-density serial ports" major */
28 #define SERIAL_NX_MAJOR 204
29 #define MINOR_START 170
35 UART_BAUDDIV_MSB
= 0x0c,
36 UART_BAUDDIV_LSB
= 0x10,
43 UART_RTS_TRAIL
= 0x2c,
44 UART_DRV_ENABLE
= 0x30,
46 UART_RXFIFO_IRQLEVEL
= 0x38,
47 UART_TXFIFO_IRQLEVEL
= 0x3c,
55 #define LINE_CR_BRK (1<<0)
56 #define LINE_CR_PEN (1<<1)
57 #define LINE_CR_EPS (1<<2)
58 #define LINE_CR_STP2 (1<<3)
59 #define LINE_CR_FEN (1<<4)
60 #define LINE_CR_5BIT (0<<5)
61 #define LINE_CR_6BIT (1<<5)
62 #define LINE_CR_7BIT (2<<5)
63 #define LINE_CR_8BIT (3<<5)
64 #define LINE_CR_BITS_MASK (3<<5)
66 #define CR_UART_EN (1<<0)
67 #define CR_SIREN (1<<1)
68 #define CR_SIRLP (1<<2)
69 #define CR_MSIE (1<<3)
72 #define CR_RTIE (1<<6)
78 #define FR_BUSY (1<<3)
79 #define FR_RXFE (1<<4)
80 #define FR_TXFF (1<<5)
81 #define FR_RXFF (1<<6)
82 #define FR_TXFE (1<<7)
84 #define IIR_MIS (1<<0)
85 #define IIR_RIS (1<<1)
86 #define IIR_TIS (1<<2)
87 #define IIR_RTIS (1<<3)
90 #define RTS_CR_AUTO (1<<0)
91 #define RTS_CR_RTS (1<<1)
92 #define RTS_CR_COUNT (1<<2)
93 #define RTS_CR_MOD2 (1<<3)
94 #define RTS_CR_RTS_POL (1<<4)
95 #define RTS_CR_CTS_CTR (1<<5)
96 #define RTS_CR_CTS_POL (1<<6)
97 #define RTS_CR_STICK (1<<7)
99 #define UART_PORT_SIZE 0x40
100 #define DRIVER_NAME "netx-uart"
103 struct uart_port port
;
106 static void netx_stop_tx(struct uart_port
*port
)
109 val
= readl(port
->membase
+ UART_CR
);
110 writel(val
& ~CR_TIE
, port
->membase
+ UART_CR
);
113 static void netx_stop_rx(struct uart_port
*port
)
116 val
= readl(port
->membase
+ UART_CR
);
117 writel(val
& ~CR_RIE
, port
->membase
+ UART_CR
);
120 static void netx_enable_ms(struct uart_port
*port
)
123 val
= readl(port
->membase
+ UART_CR
);
124 writel(val
| CR_MSIE
, port
->membase
+ UART_CR
);
127 static inline void netx_transmit_buffer(struct uart_port
*port
)
129 struct circ_buf
*xmit
= &port
->state
->xmit
;
132 writel(port
->x_char
, port
->membase
+ UART_DR
);
138 if (uart_tx_stopped(port
) || uart_circ_empty(xmit
)) {
144 /* send xmit->buf[xmit->tail]
145 * out the port here */
146 writel(xmit
->buf
[xmit
->tail
], port
->membase
+ UART_DR
);
147 xmit
->tail
= (xmit
->tail
+ 1) &
148 (UART_XMIT_SIZE
- 1);
150 if (uart_circ_empty(xmit
))
152 } while (!(readl(port
->membase
+ UART_FR
) & FR_TXFF
));
154 if (uart_circ_empty(xmit
))
158 static void netx_start_tx(struct uart_port
*port
)
161 readl(port
->membase
+ UART_CR
) | CR_TIE
, port
->membase
+ UART_CR
);
163 if (!(readl(port
->membase
+ UART_FR
) & FR_TXFF
))
164 netx_transmit_buffer(port
);
167 static unsigned int netx_tx_empty(struct uart_port
*port
)
169 return readl(port
->membase
+ UART_FR
) & FR_BUSY
? 0 : TIOCSER_TEMT
;
172 static void netx_txint(struct uart_port
*port
)
174 struct circ_buf
*xmit
= &port
->state
->xmit
;
176 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
181 netx_transmit_buffer(port
);
183 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
184 uart_write_wakeup(port
);
187 static void netx_rxint(struct uart_port
*port
, unsigned long *flags
)
189 unsigned char rx
, flg
, status
;
191 while (!(readl(port
->membase
+ UART_FR
) & FR_RXFE
)) {
192 rx
= readl(port
->membase
+ UART_DR
);
195 status
= readl(port
->membase
+ UART_SR
);
196 if (status
& SR_BE
) {
197 writel(0, port
->membase
+ UART_SR
);
198 if (uart_handle_break(port
))
202 if (unlikely(status
& (SR_FE
| SR_PE
| SR_OE
))) {
205 port
->icount
.parity
++;
206 else if (status
& SR_FE
)
207 port
->icount
.frame
++;
209 port
->icount
.overrun
++;
211 status
&= port
->read_status_mask
;
215 else if (status
& SR_PE
)
217 else if (status
& SR_FE
)
221 if (uart_handle_sysrq_char(port
, rx
))
224 uart_insert_char(port
, status
, SR_OE
, rx
, flg
);
227 spin_unlock_irqrestore(&port
->lock
, *flags
);
228 tty_flip_buffer_push(&port
->state
->port
);
229 spin_lock_irqsave(&port
->lock
, *flags
);
232 static irqreturn_t
netx_int(int irq
, void *dev_id
)
234 struct uart_port
*port
= dev_id
;
236 unsigned char status
;
238 spin_lock_irqsave(&port
->lock
,flags
);
240 status
= readl(port
->membase
+ UART_IIR
) & IIR_MASK
;
242 if (status
& IIR_RIS
)
243 netx_rxint(port
, &flags
);
244 if (status
& IIR_TIS
)
246 if (status
& IIR_MIS
) {
247 if (readl(port
->membase
+ UART_FR
) & FR_CTS
)
248 uart_handle_cts_change(port
, 1);
250 uart_handle_cts_change(port
, 0);
252 writel(0, port
->membase
+ UART_IIR
);
253 status
= readl(port
->membase
+ UART_IIR
) & IIR_MASK
;
256 spin_unlock_irqrestore(&port
->lock
,flags
);
260 static unsigned int netx_get_mctrl(struct uart_port
*port
)
262 unsigned int ret
= TIOCM_DSR
| TIOCM_CAR
;
264 if (readl(port
->membase
+ UART_FR
) & FR_CTS
)
270 static void netx_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
274 /* FIXME: Locking needed ? */
275 if (mctrl
& TIOCM_RTS
) {
276 val
= readl(port
->membase
+ UART_RTS_CR
);
277 writel(val
| RTS_CR_RTS
, port
->membase
+ UART_RTS_CR
);
281 static void netx_break_ctl(struct uart_port
*port
, int break_state
)
283 unsigned int line_cr
;
284 spin_lock_irq(&port
->lock
);
286 line_cr
= readl(port
->membase
+ UART_LINE_CR
);
287 if (break_state
!= 0)
288 line_cr
|= LINE_CR_BRK
;
290 line_cr
&= ~LINE_CR_BRK
;
291 writel(line_cr
, port
->membase
+ UART_LINE_CR
);
293 spin_unlock_irq(&port
->lock
);
296 static int netx_startup(struct uart_port
*port
)
300 ret
= request_irq(port
->irq
, netx_int
, 0,
303 dev_err(port
->dev
, "unable to grab irq%d\n",port
->irq
);
307 writel(readl(port
->membase
+ UART_LINE_CR
) | LINE_CR_FEN
,
308 port
->membase
+ UART_LINE_CR
);
310 writel(CR_MSIE
| CR_RIE
| CR_TIE
| CR_RTIE
| CR_UART_EN
,
311 port
->membase
+ UART_CR
);
317 static void netx_shutdown(struct uart_port
*port
)
319 writel(0, port
->membase
+ UART_CR
) ;
321 free_irq(port
->irq
, port
);
325 netx_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
326 struct ktermios
*old
)
328 unsigned int baud
, quot
;
329 unsigned char old_cr
;
330 unsigned char line_cr
= LINE_CR_FEN
;
331 unsigned char rts_cr
= 0;
333 switch (termios
->c_cflag
& CSIZE
) {
335 line_cr
|= LINE_CR_5BIT
;
338 line_cr
|= LINE_CR_6BIT
;
341 line_cr
|= LINE_CR_7BIT
;
344 line_cr
|= LINE_CR_8BIT
;
348 if (termios
->c_cflag
& CSTOPB
)
349 line_cr
|= LINE_CR_STP2
;
351 if (termios
->c_cflag
& PARENB
) {
352 line_cr
|= LINE_CR_PEN
;
353 if (!(termios
->c_cflag
& PARODD
))
354 line_cr
|= LINE_CR_EPS
;
357 if (termios
->c_cflag
& CRTSCTS
)
358 rts_cr
= RTS_CR_AUTO
| RTS_CR_CTS_CTR
| RTS_CR_RTS_POL
;
360 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
366 spin_lock_irq(&port
->lock
);
368 uart_update_timeout(port
, termios
->c_cflag
, baud
);
370 old_cr
= readl(port
->membase
+ UART_CR
);
372 /* disable interrupts */
373 writel(old_cr
& ~(CR_MSIE
| CR_RIE
| CR_TIE
| CR_RTIE
),
374 port
->membase
+ UART_CR
);
376 /* drain transmitter */
377 while (readl(port
->membase
+ UART_FR
) & FR_BUSY
);
380 writel(old_cr
& ~CR_UART_EN
, port
->membase
+ UART_CR
);
382 /* modem status interrupts */
384 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
387 writel((quot
>>8) & 0xff, port
->membase
+ UART_BAUDDIV_MSB
);
388 writel(quot
& 0xff, port
->membase
+ UART_BAUDDIV_LSB
);
389 writel(line_cr
, port
->membase
+ UART_LINE_CR
);
391 writel(rts_cr
, port
->membase
+ UART_RTS_CR
);
394 * Characters to ignore
396 port
->ignore_status_mask
= 0;
397 if (termios
->c_iflag
& IGNPAR
)
398 port
->ignore_status_mask
|= SR_PE
;
399 if (termios
->c_iflag
& IGNBRK
) {
400 port
->ignore_status_mask
|= SR_BE
;
402 * If we're ignoring parity and break indicators,
403 * ignore overruns too (for real raw support).
405 if (termios
->c_iflag
& IGNPAR
)
406 port
->ignore_status_mask
|= SR_PE
;
409 port
->read_status_mask
= 0;
410 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
411 port
->read_status_mask
|= SR_BE
;
412 if (termios
->c_iflag
& INPCK
)
413 port
->read_status_mask
|= SR_PE
| SR_FE
;
415 writel(old_cr
, port
->membase
+ UART_CR
);
417 spin_unlock_irq(&port
->lock
);
420 static const char *netx_type(struct uart_port
*port
)
422 return port
->type
== PORT_NETX
? "NETX" : NULL
;
425 static void netx_release_port(struct uart_port
*port
)
427 release_mem_region(port
->mapbase
, UART_PORT_SIZE
);
430 static int netx_request_port(struct uart_port
*port
)
432 return request_mem_region(port
->mapbase
, UART_PORT_SIZE
,
433 DRIVER_NAME
) != NULL
? 0 : -EBUSY
;
436 static void netx_config_port(struct uart_port
*port
, int flags
)
438 if (flags
& UART_CONFIG_TYPE
&& netx_request_port(port
) == 0)
439 port
->type
= PORT_NETX
;
443 netx_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
447 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_NETX
)
453 static struct uart_ops netx_pops
= {
454 .tx_empty
= netx_tx_empty
,
455 .set_mctrl
= netx_set_mctrl
,
456 .get_mctrl
= netx_get_mctrl
,
457 .stop_tx
= netx_stop_tx
,
458 .start_tx
= netx_start_tx
,
459 .stop_rx
= netx_stop_rx
,
460 .enable_ms
= netx_enable_ms
,
461 .break_ctl
= netx_break_ctl
,
462 .startup
= netx_startup
,
463 .shutdown
= netx_shutdown
,
464 .set_termios
= netx_set_termios
,
466 .release_port
= netx_release_port
,
467 .request_port
= netx_request_port
,
468 .config_port
= netx_config_port
,
469 .verify_port
= netx_verify_port
,
472 static struct netx_port netx_ports
[] = {
477 .membase
= (char __iomem
*)io_p2v(NETX_PA_UART0
),
478 .mapbase
= NETX_PA_UART0
,
479 .irq
= NETX_IRQ_UART0
,
480 .uartclk
= 100000000,
482 .flags
= UPF_BOOT_AUTOCONF
,
490 .membase
= (char __iomem
*)io_p2v(NETX_PA_UART1
),
491 .mapbase
= NETX_PA_UART1
,
492 .irq
= NETX_IRQ_UART1
,
493 .uartclk
= 100000000,
495 .flags
= UPF_BOOT_AUTOCONF
,
503 .membase
= (char __iomem
*)io_p2v(NETX_PA_UART2
),
504 .mapbase
= NETX_PA_UART2
,
505 .irq
= NETX_IRQ_UART2
,
506 .uartclk
= 100000000,
508 .flags
= UPF_BOOT_AUTOCONF
,
515 #ifdef CONFIG_SERIAL_NETX_CONSOLE
517 static void netx_console_putchar(struct uart_port
*port
, int ch
)
519 while (readl(port
->membase
+ UART_FR
) & FR_BUSY
);
520 writel(ch
, port
->membase
+ UART_DR
);
524 netx_console_write(struct console
*co
, const char *s
, unsigned int count
)
526 struct uart_port
*port
= &netx_ports
[co
->index
].port
;
527 unsigned char cr_save
;
529 cr_save
= readl(port
->membase
+ UART_CR
);
530 writel(cr_save
| CR_UART_EN
, port
->membase
+ UART_CR
);
532 uart_console_write(port
, s
, count
, netx_console_putchar
);
534 while (readl(port
->membase
+ UART_FR
) & FR_BUSY
);
535 writel(cr_save
, port
->membase
+ UART_CR
);
539 netx_console_get_options(struct uart_port
*port
, int *baud
,
540 int *parity
, int *bits
, int *flow
)
542 unsigned char line_cr
;
544 *baud
= (readl(port
->membase
+ UART_BAUDDIV_MSB
) << 8) |
545 readl(port
->membase
+ UART_BAUDDIV_LSB
);
552 line_cr
= readl(port
->membase
+ UART_LINE_CR
);
554 if (line_cr
& LINE_CR_PEN
) {
555 if (line_cr
& LINE_CR_EPS
)
561 switch (line_cr
& LINE_CR_BITS_MASK
) {
576 if (readl(port
->membase
+ UART_RTS_CR
) & RTS_CR_AUTO
)
581 netx_console_setup(struct console
*co
, char *options
)
583 struct netx_port
*sport
;
590 * Check whether an invalid uart number has been specified, and
591 * if so, search for the first available port that does have
594 if (co
->index
== -1 || co
->index
>= ARRAY_SIZE(netx_ports
))
596 sport
= &netx_ports
[co
->index
];
599 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
601 /* if the UART is enabled, assume it has been correctly setup
602 * by the bootloader and get the options
604 if (readl(sport
->port
.membase
+ UART_CR
) & CR_UART_EN
) {
605 netx_console_get_options(&sport
->port
, &baud
,
606 &parity
, &bits
, &flow
);
611 return uart_set_options(&sport
->port
, co
, baud
, parity
, bits
, flow
);
614 static struct uart_driver netx_reg
;
615 static struct console netx_console
= {
617 .write
= netx_console_write
,
618 .device
= uart_console_device
,
619 .setup
= netx_console_setup
,
620 .flags
= CON_PRINTBUFFER
,
625 static int __init
netx_console_init(void)
627 register_console(&netx_console
);
630 console_initcall(netx_console_init
);
632 #define NETX_CONSOLE &netx_console
634 #define NETX_CONSOLE NULL
637 static struct uart_driver netx_reg
= {
638 .owner
= THIS_MODULE
,
639 .driver_name
= DRIVER_NAME
,
641 .major
= SERIAL_NX_MAJOR
,
642 .minor
= MINOR_START
,
643 .nr
= ARRAY_SIZE(netx_ports
),
644 .cons
= NETX_CONSOLE
,
647 static int serial_netx_suspend(struct platform_device
*pdev
, pm_message_t state
)
649 struct netx_port
*sport
= platform_get_drvdata(pdev
);
652 uart_suspend_port(&netx_reg
, &sport
->port
);
657 static int serial_netx_resume(struct platform_device
*pdev
)
659 struct netx_port
*sport
= platform_get_drvdata(pdev
);
662 uart_resume_port(&netx_reg
, &sport
->port
);
667 static int serial_netx_probe(struct platform_device
*pdev
)
669 struct uart_port
*port
= &netx_ports
[pdev
->id
].port
;
671 dev_info(&pdev
->dev
, "initialising\n");
673 port
->dev
= &pdev
->dev
;
675 writel(1, port
->membase
+ UART_RXFIFO_IRQLEVEL
);
676 uart_add_one_port(&netx_reg
, &netx_ports
[pdev
->id
].port
);
677 platform_set_drvdata(pdev
, &netx_ports
[pdev
->id
]);
682 static int serial_netx_remove(struct platform_device
*pdev
)
684 struct netx_port
*sport
= platform_get_drvdata(pdev
);
687 uart_remove_one_port(&netx_reg
, &sport
->port
);
692 static struct platform_driver serial_netx_driver
= {
693 .probe
= serial_netx_probe
,
694 .remove
= serial_netx_remove
,
696 .suspend
= serial_netx_suspend
,
697 .resume
= serial_netx_resume
,
704 static int __init
netx_serial_init(void)
708 printk(KERN_INFO
"Serial: NetX driver\n");
710 ret
= uart_register_driver(&netx_reg
);
714 ret
= platform_driver_register(&serial_netx_driver
);
716 uart_unregister_driver(&netx_reg
);
721 static void __exit
netx_serial_exit(void)
723 platform_driver_unregister(&serial_netx_driver
);
724 uart_unregister_driver(&netx_reg
);
727 module_init(netx_serial_init
);
728 module_exit(netx_serial_exit
);
730 MODULE_AUTHOR("Sascha Hauer");
731 MODULE_DESCRIPTION("NetX serial port driver");
732 MODULE_LICENSE("GPL");
733 MODULE_ALIAS("platform:" DRIVER_NAME
);