1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2013 Tilera Corporation. All Rights Reserved.
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/module.h>
14 #include <linux/serial_core.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
18 #include <gxio/common.h>
19 #include <gxio/iorpc_globals.h>
20 #include <gxio/iorpc_uart.h>
21 #include <gxio/kiorpc.h>
23 #include <hv/drv_uart_intf.h>
26 * Use device name ttyS, major 4, minor 64-65.
27 * This is the usual serial port name, 8250 conventional range.
29 #define TILEGX_UART_MAJOR TTY_MAJOR
30 #define TILEGX_UART_MINOR 64
31 #define TILEGX_UART_NAME "ttyS"
32 #define DRIVER_NAME_STRING "TILEGx_Serial"
33 #define TILEGX_UART_REF_CLK 125000000; /* REF_CLK is always 125 MHz. */
35 struct tile_uart_port
{
37 struct uart_port uart
;
39 /* GXIO device context. */
40 gxio_uart_context_t context
;
42 /* UART access mutex. */
45 /* CPU receiving interrupts. */
49 static struct tile_uart_port tile_uart_ports
[TILEGX_UART_NR
];
50 static struct uart_driver tilegx_uart_driver
;
54 * Read UART rx fifo, and insert the chars into tty buffer.
56 static void receive_chars(struct tile_uart_port
*tile_uart
,
57 struct tty_struct
*tty
)
61 UART_FIFO_COUNT_t count
;
62 gxio_uart_context_t
*context
= &tile_uart
->context
;
63 struct tty_port
*port
= tty
->port
;
65 count
.word
= gxio_uart_read(context
, UART_FIFO_COUNT
);
66 for (i
= 0; i
< count
.rfifo_count
; i
++) {
67 c
= (char)gxio_uart_read(context
, UART_RECEIVE_DATA
);
68 tty_insert_flip_char(port
, c
, TTY_NORMAL
);
74 * Drain the Rx FIFO, called by interrupt handler.
76 static void handle_receive(struct tile_uart_port
*tile_uart
)
78 struct tty_port
*port
= &tile_uart
->uart
.state
->port
;
79 struct tty_struct
*tty
= tty_port_tty_get(port
);
80 gxio_uart_context_t
*context
= &tile_uart
->context
;
85 /* First read UART rx fifo. */
86 receive_chars(tile_uart
, tty
);
88 /* Reset RFIFO_WE interrupt. */
89 gxio_uart_write(context
, UART_INTERRUPT_STATUS
,
90 UART_INTERRUPT_MASK__RFIFO_WE_MASK
);
92 /* Final read, if any chars comes between the first read and
93 * the interrupt reset.
95 receive_chars(tile_uart
, tty
);
97 spin_unlock(&tile_uart
->uart
.lock
);
98 tty_flip_buffer_push(port
);
99 spin_lock(&tile_uart
->uart
.lock
);
105 * Push one char to UART Write FIFO.
106 * Return 0 on success, -1 if write filo is full.
108 static int tilegx_putchar(gxio_uart_context_t
*context
, char c
)
111 flag
.word
= gxio_uart_read(context
, UART_FLAG
);
115 gxio_uart_write(context
, UART_TRANSMIT_DATA
, (unsigned long)c
);
121 * Send chars to UART Write FIFO; called by interrupt handler.
123 static void handle_transmit(struct tile_uart_port
*tile_uart
)
126 struct uart_port
*port
;
127 struct circ_buf
*xmit
;
128 gxio_uart_context_t
*context
= &tile_uart
->context
;
130 /* First reset WFIFO_RE interrupt. */
131 gxio_uart_write(context
, UART_INTERRUPT_STATUS
,
132 UART_INTERRUPT_MASK__WFIFO_RE_MASK
);
134 port
= &tile_uart
->uart
;
135 xmit
= &port
->state
->xmit
;
137 if (tilegx_putchar(context
, port
->x_char
))
143 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
146 while (!uart_circ_empty(xmit
)) {
147 ch
= xmit
->buf
[xmit
->tail
];
148 if (tilegx_putchar(context
, ch
))
150 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
154 /* Reset WFIFO_RE interrupt. */
155 gxio_uart_write(context
, UART_INTERRUPT_STATUS
,
156 UART_INTERRUPT_MASK__WFIFO_RE_MASK
);
158 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
159 uart_write_wakeup(port
);
164 * UART Interrupt handler.
166 static irqreturn_t
tilegx_interrupt(int irq
, void *dev_id
)
169 UART_INTERRUPT_STATUS_t intr_stat
;
170 struct tile_uart_port
*tile_uart
;
171 gxio_uart_context_t
*context
;
172 struct uart_port
*port
= dev_id
;
173 irqreturn_t ret
= IRQ_NONE
;
175 spin_lock_irqsave(&port
->lock
, flags
);
177 tile_uart
= container_of(port
, struct tile_uart_port
, uart
);
178 context
= &tile_uart
->context
;
179 intr_stat
.word
= gxio_uart_read(context
, UART_INTERRUPT_STATUS
);
181 if (intr_stat
.rfifo_we
) {
182 handle_receive(tile_uart
);
185 if (intr_stat
.wfifo_re
) {
186 handle_transmit(tile_uart
);
190 spin_unlock_irqrestore(&port
->lock
, flags
);
196 * Return TIOCSER_TEMT when transmitter FIFO is empty.
198 static u_int
tilegx_tx_empty(struct uart_port
*port
)
202 struct tile_uart_port
*tile_uart
;
203 gxio_uart_context_t
*context
;
205 tile_uart
= container_of(port
, struct tile_uart_port
, uart
);
206 if (!mutex_trylock(&tile_uart
->mutex
))
208 context
= &tile_uart
->context
;
210 flag
.word
= gxio_uart_read(context
, UART_FLAG
);
211 ret
= (flag
.wfifo_empty
) ? TIOCSER_TEMT
: 0;
212 mutex_unlock(&tile_uart
->mutex
);
219 * Set state of the modem control output lines.
221 static void tilegx_set_mctrl(struct uart_port
*port
, u_int mctrl
)
228 * Get state of the modem control input lines.
230 static u_int
tilegx_get_mctrl(struct uart_port
*port
)
232 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
239 static void tilegx_stop_tx(struct uart_port
*port
)
246 * Start transmitting.
248 static void tilegx_start_tx(struct uart_port
*port
)
251 struct circ_buf
*xmit
;
252 struct tile_uart_port
*tile_uart
;
253 gxio_uart_context_t
*context
;
255 tile_uart
= container_of(port
, struct tile_uart_port
, uart
);
256 if (!mutex_trylock(&tile_uart
->mutex
))
258 context
= &tile_uart
->context
;
259 xmit
= &port
->state
->xmit
;
261 if (tilegx_putchar(context
, port
->x_char
))
267 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
268 mutex_unlock(&tile_uart
->mutex
);
272 while (!uart_circ_empty(xmit
)) {
273 ch
= xmit
->buf
[xmit
->tail
];
274 if (tilegx_putchar(context
, ch
))
276 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
280 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
281 uart_write_wakeup(port
);
283 mutex_unlock(&tile_uart
->mutex
);
288 * Stop receiving - port is in process of being closed.
290 static void tilegx_stop_rx(struct uart_port
*port
)
293 struct tile_uart_port
*tile_uart
;
294 gxio_uart_context_t
*context
;
297 tile_uart
= container_of(port
, struct tile_uart_port
, uart
);
298 if (!mutex_trylock(&tile_uart
->mutex
))
301 context
= &tile_uart
->context
;
302 cpu
= tile_uart
->irq_cpu
;
303 err
= gxio_uart_cfg_interrupt(context
, cpu_x(cpu
), cpu_y(cpu
),
305 mutex_unlock(&tile_uart
->mutex
);
309 * Control the transmission of a break signal.
311 static void tilegx_break_ctl(struct uart_port
*port
, int break_state
)
318 * Perform initialization and enable port for reception.
320 static int tilegx_startup(struct uart_port
*port
)
322 struct tile_uart_port
*tile_uart
;
323 gxio_uart_context_t
*context
;
325 int cpu
= raw_smp_processor_id(); /* pick an arbitrary cpu */
327 tile_uart
= container_of(port
, struct tile_uart_port
, uart
);
328 if (mutex_lock_interruptible(&tile_uart
->mutex
))
330 context
= &tile_uart
->context
;
332 /* Now open the hypervisor device if we haven't already. */
333 if (context
->fd
< 0) {
334 UART_INTERRUPT_MASK_t intr_mask
;
336 /* Initialize UART device. */
337 ret
= gxio_uart_init(context
, port
->line
);
343 /* Create our IRQs. */
344 port
->irq
= irq_alloc_hwirq(-1);
347 tile_irq_activate(port
->irq
, TILE_IRQ_PERCPU
);
349 /* Register our IRQs. */
350 ret
= request_irq(port
->irq
, tilegx_interrupt
, 0,
351 tilegx_uart_driver
.driver_name
, port
);
355 /* Request that the hardware start sending us interrupts. */
356 tile_uart
->irq_cpu
= cpu
;
357 ret
= gxio_uart_cfg_interrupt(context
, cpu_x(cpu
), cpu_y(cpu
),
358 KERNEL_PL
, port
->irq
);
362 /* Enable UART Tx/Rx Interrupt. */
363 intr_mask
.word
= gxio_uart_read(context
, UART_INTERRUPT_MASK
);
364 intr_mask
.wfifo_re
= 0;
365 intr_mask
.rfifo_we
= 0;
366 gxio_uart_write(context
, UART_INTERRUPT_MASK
, intr_mask
.word
);
368 /* Reset the Tx/Rx interrupt in case it's set. */
369 gxio_uart_write(context
, UART_INTERRUPT_STATUS
,
370 UART_INTERRUPT_MASK__WFIFO_RE_MASK
|
371 UART_INTERRUPT_MASK__RFIFO_WE_MASK
);
374 mutex_unlock(&tile_uart
->mutex
);
378 free_irq(port
->irq
, port
);
380 irq_free_hwirq(port
->irq
);
382 gxio_uart_destroy(context
);
385 mutex_unlock(&tile_uart
->mutex
);
391 * Release kernel resources if it is the last close, disable the port,
392 * free IRQ and close the port.
394 static void tilegx_shutdown(struct uart_port
*port
)
397 UART_INTERRUPT_MASK_t intr_mask
;
398 struct tile_uart_port
*tile_uart
;
399 gxio_uart_context_t
*context
;
402 tile_uart
= container_of(port
, struct tile_uart_port
, uart
);
403 if (mutex_lock_interruptible(&tile_uart
->mutex
))
405 context
= &tile_uart
->context
;
407 /* Disable UART Tx/Rx Interrupt. */
408 intr_mask
.word
= gxio_uart_read(context
, UART_INTERRUPT_MASK
);
409 intr_mask
.wfifo_re
= 1;
410 intr_mask
.rfifo_we
= 1;
411 gxio_uart_write(context
, UART_INTERRUPT_MASK
, intr_mask
.word
);
413 /* Request that the hardware stop sending us interrupts. */
414 cpu
= tile_uart
->irq_cpu
;
415 err
= gxio_uart_cfg_interrupt(context
, cpu_x(cpu
), cpu_y(cpu
),
419 free_irq(port
->irq
, port
);
420 irq_free_hwirq(port
->irq
);
424 gxio_uart_destroy(context
);
426 mutex_unlock(&tile_uart
->mutex
);
433 static void tilegx_flush_buffer(struct uart_port
*port
)
440 * Change the port parameters.
442 static void tilegx_set_termios(struct uart_port
*port
,
443 struct ktermios
*termios
, struct ktermios
*old
)
446 UART_DIVISOR_t divisor
;
449 struct tile_uart_port
*tile_uart
;
450 gxio_uart_context_t
*context
;
452 tile_uart
= container_of(port
, struct tile_uart_port
, uart
);
453 if (!mutex_trylock(&tile_uart
->mutex
))
455 context
= &tile_uart
->context
;
457 /* Open the hypervisor device if we haven't already. */
458 if (context
->fd
< 0) {
459 err
= gxio_uart_init(context
, port
->line
);
461 mutex_unlock(&tile_uart
->mutex
);
466 divisor
.word
= gxio_uart_read(context
, UART_DIVISOR
);
467 type
.word
= gxio_uart_read(context
, UART_TYPE
);
470 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 16);
471 divisor
.divisor
= uart_get_divisor(port
, baud
);
474 if ((termios
->c_cflag
& CSIZE
) == CS7
)
475 type
.dbits
= UART_TYPE__DBITS_VAL_SEVEN_DBITS
;
477 type
.dbits
= UART_TYPE__DBITS_VAL_EIGHT_DBITS
;
480 if (termios
->c_cflag
& PARENB
) {
481 /* Mark or Space parity. */
482 if (termios
->c_cflag
& CMSPAR
)
483 if (termios
->c_cflag
& PARODD
)
484 type
.ptype
= UART_TYPE__PTYPE_VAL_MARK
;
486 type
.ptype
= UART_TYPE__PTYPE_VAL_SPACE
;
487 else if (termios
->c_cflag
& PARODD
)
488 type
.ptype
= UART_TYPE__PTYPE_VAL_ODD
;
490 type
.ptype
= UART_TYPE__PTYPE_VAL_EVEN
;
492 type
.ptype
= UART_TYPE__PTYPE_VAL_NONE
;
495 if (termios
->c_cflag
& CSTOPB
)
496 type
.sbits
= UART_TYPE__SBITS_VAL_TWO_SBITS
;
498 type
.sbits
= UART_TYPE__SBITS_VAL_ONE_SBITS
;
500 /* Set the uart paramters. */
501 gxio_uart_write(context
, UART_DIVISOR
, divisor
.word
);
502 gxio_uart_write(context
, UART_TYPE
, type
.word
);
504 mutex_unlock(&tile_uart
->mutex
);
509 * Return string describing the specified port.
511 static const char *tilegx_type(struct uart_port
*port
)
513 return port
->type
== PORT_TILEGX
? DRIVER_NAME_STRING
: NULL
;
518 * Release the resources being used by 'port'.
520 static void tilegx_release_port(struct uart_port
*port
)
522 /* Nothing to release. */
527 * Request the resources being used by 'port'.
529 static int tilegx_request_port(struct uart_port
*port
)
531 /* Always present. */
537 * Configure/autoconfigure the port.
539 static void tilegx_config_port(struct uart_port
*port
, int flags
)
541 if (flags
& UART_CONFIG_TYPE
)
542 port
->type
= PORT_TILEGX
;
547 * Verify the new serial_struct (for TIOCSSERIAL).
549 static int tilegx_verify_port(struct uart_port
*port
,
550 struct serial_struct
*ser
)
552 if ((ser
->type
!= PORT_UNKNOWN
) && (ser
->type
!= PORT_TILEGX
))
558 #ifdef CONFIG_CONSOLE_POLL
561 * Console polling routines for writing and reading from the uart while
562 * in an interrupt or debug context.
565 static int tilegx_poll_get_char(struct uart_port
*port
)
567 UART_FIFO_COUNT_t count
;
568 gxio_uart_context_t
*context
;
569 struct tile_uart_port
*tile_uart
;
571 tile_uart
= container_of(port
, struct tile_uart_port
, uart
);
572 context
= &tile_uart
->context
;
573 count
.word
= gxio_uart_read(context
, UART_FIFO_COUNT
);
574 if (count
.rfifo_count
== 0)
576 return (char)gxio_uart_read(context
, UART_RECEIVE_DATA
);
579 static void tilegx_poll_put_char(struct uart_port
*port
, unsigned char c
)
581 gxio_uart_context_t
*context
;
582 struct tile_uart_port
*tile_uart
;
584 tile_uart
= container_of(port
, struct tile_uart_port
, uart
);
585 context
= &tile_uart
->context
;
586 gxio_uart_write(context
, UART_TRANSMIT_DATA
, (unsigned long)c
);
589 #endif /* CONFIG_CONSOLE_POLL */
592 static const struct uart_ops tilegx_ops
= {
593 .tx_empty
= tilegx_tx_empty
,
594 .set_mctrl
= tilegx_set_mctrl
,
595 .get_mctrl
= tilegx_get_mctrl
,
596 .stop_tx
= tilegx_stop_tx
,
597 .start_tx
= tilegx_start_tx
,
598 .stop_rx
= tilegx_stop_rx
,
599 .break_ctl
= tilegx_break_ctl
,
600 .startup
= tilegx_startup
,
601 .shutdown
= tilegx_shutdown
,
602 .flush_buffer
= tilegx_flush_buffer
,
603 .set_termios
= tilegx_set_termios
,
605 .release_port
= tilegx_release_port
,
606 .request_port
= tilegx_request_port
,
607 .config_port
= tilegx_config_port
,
608 .verify_port
= tilegx_verify_port
,
609 #ifdef CONFIG_CONSOLE_POLL
610 .poll_get_char
= tilegx_poll_get_char
,
611 .poll_put_char
= tilegx_poll_put_char
,
616 static void tilegx_init_ports(void)
619 struct uart_port
*port
;
621 for (i
= 0; i
< TILEGX_UART_NR
; i
++) {
622 port
= &tile_uart_ports
[i
].uart
;
623 port
->ops
= &tilegx_ops
;
625 port
->type
= PORT_TILEGX
;
626 port
->uartclk
= TILEGX_UART_REF_CLK
;
627 port
->flags
= UPF_BOOT_AUTOCONF
;
629 tile_uart_ports
[i
].context
.fd
= -1;
630 mutex_init(&tile_uart_ports
[i
].mutex
);
635 static struct uart_driver tilegx_uart_driver
= {
636 .owner
= THIS_MODULE
,
637 .driver_name
= DRIVER_NAME_STRING
,
638 .dev_name
= TILEGX_UART_NAME
,
639 .major
= TILEGX_UART_MAJOR
,
640 .minor
= TILEGX_UART_MINOR
,
641 .nr
= TILEGX_UART_NR
,
645 static int __init
tilegx_init(void)
649 struct tty_driver
*tty_drv
;
651 ret
= uart_register_driver(&tilegx_uart_driver
);
654 tty_drv
= tilegx_uart_driver
.tty_driver
;
655 tty_drv
->init_termios
.c_cflag
= B115200
| CS8
| CREAD
| HUPCL
| CLOCAL
;
656 tty_drv
->init_termios
.c_ispeed
= 115200;
657 tty_drv
->init_termios
.c_ospeed
= 115200;
661 for (i
= 0; i
< TILEGX_UART_NR
; i
++) {
662 struct uart_port
*port
= &tile_uart_ports
[i
].uart
;
663 ret
= uart_add_one_port(&tilegx_uart_driver
, port
);
670 static void __exit
tilegx_exit(void)
673 struct uart_port
*port
;
675 for (i
= 0; i
< TILEGX_UART_NR
; i
++) {
676 port
= &tile_uart_ports
[i
].uart
;
677 uart_remove_one_port(&tilegx_uart_driver
, port
);
680 uart_unregister_driver(&tilegx_uart_driver
);
684 module_init(tilegx_init
);
685 module_exit(tilegx_exit
);
687 MODULE_AUTHOR("Tilera Corporation");
688 MODULE_DESCRIPTION("TILEGx serial port driver");
689 MODULE_LICENSE("GPL");