2 * Actions Semi Owl family serial console
4 * Copyright 2013 Actions Semi Inc.
5 * Author: Actions Semi, Inc.
7 * Copyright (c) 2016-2017 Andreas Färber
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/console.h>
24 #include <linux/delay.h>
26 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/serial.h>
30 #include <linux/serial_core.h>
32 #define OWL_UART_CTL 0x000
33 #define OWL_UART_TXDAT 0x008
34 #define OWL_UART_STAT 0x00c
36 #define OWL_UART_CTL_TRFS_TX BIT(14)
37 #define OWL_UART_CTL_EN BIT(15)
38 #define OWL_UART_CTL_RXIE BIT(18)
39 #define OWL_UART_CTL_TXIE BIT(19)
41 #define OWL_UART_STAT_RIP BIT(0)
42 #define OWL_UART_STAT_TIP BIT(1)
43 #define OWL_UART_STAT_TFFU BIT(6)
44 #define OWL_UART_STAT_TRFL_MASK (0x1f << 11)
45 #define OWL_UART_STAT_UTBB BIT(17)
47 static inline void owl_uart_write(struct uart_port *port, u32 val, unsigned int off)
49 writel(val, port->membase + off);
52 static inline u32 owl_uart_read(struct uart_port *port, unsigned int off)
54 return readl(port->membase + off);
57 #ifdef CONFIG_SERIAL_OWL_CONSOLE
59 static void owl_console_putchar(struct uart_port *port, int ch)
64 while (owl_uart_read(port, OWL_UART_STAT) & OWL_UART_STAT_TFFU)
67 owl_uart_write(port, ch, OWL_UART_TXDAT);
70 static void owl_uart_port_write(struct uart_port *port, const char *s,
77 local_irq_save(flags);
81 else if (oops_in_progress)
82 locked = spin_trylock(&port->lock);
84 spin_lock(&port->lock);
88 old_ctl = owl_uart_read(port, OWL_UART_CTL);
89 val = old_ctl | OWL_UART_CTL_TRFS_TX;
91 val &= ~(OWL_UART_CTL_RXIE | OWL_UART_CTL_TXIE);
92 owl_uart_write(port, val, OWL_UART_CTL);
94 uart_console_write(port, s, count, owl_console_putchar);
96 /* wait until all contents have been sent out */
97 while (owl_uart_read(port, OWL_UART_STAT) & OWL_UART_STAT_TRFL_MASK)
100 /* clear IRQ pending */
101 val = owl_uart_read(port, OWL_UART_STAT);
102 val |= OWL_UART_STAT_TIP | OWL_UART_STAT_RIP;
103 owl_uart_write(port, val, OWL_UART_STAT);
105 owl_uart_write(port, old_ctl, OWL_UART_CTL);
108 spin_unlock(&port->lock);
110 local_irq_restore(flags);
113 static void owl_uart_early_console_write(struct console *co,
117 struct earlycon_device *dev = co->data;
119 owl_uart_port_write(&dev->port, s, count);
123 owl_uart_early_console_setup(struct earlycon_device *device, const char *opt)
125 if (!device->port.membase)
128 device->con->write = owl_uart_early_console_write;
132 OF_EARLYCON_DECLARE(owl, "actions,owl-uart",
133 owl_uart_early_console_setup);
135 #endif /* CONFIG_SERIAL_OWL_CONSOLE */