1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ab4382d2SGreg Kroah-Hartman /*
3ab4382d2SGreg Kroah-Hartman * Derived from many drivers using generic_serial interface.
4ab4382d2SGreg Kroah-Hartman *
5ab4382d2SGreg Kroah-Hartman * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
6ab4382d2SGreg Kroah-Hartman *
7ab4382d2SGreg Kroah-Hartman * Serial driver for BCM63xx integrated UART.
8ab4382d2SGreg Kroah-Hartman *
9ab4382d2SGreg Kroah-Hartman * Hardware flow control was _not_ tested since I only have RX/TX on
10ab4382d2SGreg Kroah-Hartman * my board.
11ab4382d2SGreg Kroah-Hartman */
12ab4382d2SGreg Kroah-Hartman
13ab4382d2SGreg Kroah-Hartman #include <linux/kernel.h>
14ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
15ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
16ab4382d2SGreg Kroah-Hartman #include <linux/delay.h>
17ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
18ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
19ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
20ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
21ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
22ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
23ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
24ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
25d29e0d04SFlorian Fainelli #include <linux/serial_bcm63xx.h>
26f04430ceSFlorian Fainelli #include <linux/io.h>
279277285fSFlorian Fainelli #include <linux/of.h>
28ab4382d2SGreg Kroah-Hartman
29ab4382d2SGreg Kroah-Hartman #define BCM63XX_NR_UARTS 2
30ab4382d2SGreg Kroah-Hartman
31ab4382d2SGreg Kroah-Hartman static struct uart_port ports[BCM63XX_NR_UARTS];
32ab4382d2SGreg Kroah-Hartman
33ab4382d2SGreg Kroah-Hartman /*
34ab4382d2SGreg Kroah-Hartman * rx interrupt mask / stat
35ab4382d2SGreg Kroah-Hartman *
36ab4382d2SGreg Kroah-Hartman * mask:
37ab4382d2SGreg Kroah-Hartman * - rx fifo full
38ab4382d2SGreg Kroah-Hartman * - rx fifo above threshold
39ab4382d2SGreg Kroah-Hartman * - rx fifo not empty for too long
40ab4382d2SGreg Kroah-Hartman */
41ab4382d2SGreg Kroah-Hartman #define UART_RX_INT_MASK (UART_IR_MASK(UART_IR_RXOVER) | \
42ab4382d2SGreg Kroah-Hartman UART_IR_MASK(UART_IR_RXTHRESH) | \
43ab4382d2SGreg Kroah-Hartman UART_IR_MASK(UART_IR_RXTIMEOUT))
44ab4382d2SGreg Kroah-Hartman
45ab4382d2SGreg Kroah-Hartman #define UART_RX_INT_STAT (UART_IR_STAT(UART_IR_RXOVER) | \
46ab4382d2SGreg Kroah-Hartman UART_IR_STAT(UART_IR_RXTHRESH) | \
47ab4382d2SGreg Kroah-Hartman UART_IR_STAT(UART_IR_RXTIMEOUT))
48ab4382d2SGreg Kroah-Hartman
49ab4382d2SGreg Kroah-Hartman /*
50ab4382d2SGreg Kroah-Hartman * tx interrupt mask / stat
51ab4382d2SGreg Kroah-Hartman *
52ab4382d2SGreg Kroah-Hartman * mask:
53ab4382d2SGreg Kroah-Hartman * - tx fifo empty
54ab4382d2SGreg Kroah-Hartman * - tx fifo below threshold
55ab4382d2SGreg Kroah-Hartman */
56ab4382d2SGreg Kroah-Hartman #define UART_TX_INT_MASK (UART_IR_MASK(UART_IR_TXEMPTY) | \
57ab4382d2SGreg Kroah-Hartman UART_IR_MASK(UART_IR_TXTRESH))
58ab4382d2SGreg Kroah-Hartman
59ab4382d2SGreg Kroah-Hartman #define UART_TX_INT_STAT (UART_IR_STAT(UART_IR_TXEMPTY) | \
60ab4382d2SGreg Kroah-Hartman UART_IR_STAT(UART_IR_TXTRESH))
61ab4382d2SGreg Kroah-Hartman
62ab4382d2SGreg Kroah-Hartman /*
63ab4382d2SGreg Kroah-Hartman * external input interrupt
64ab4382d2SGreg Kroah-Hartman *
65ab4382d2SGreg Kroah-Hartman * mask: any edge on CTS, DCD
66ab4382d2SGreg Kroah-Hartman */
67ab4382d2SGreg Kroah-Hartman #define UART_EXTINP_INT_MASK (UART_EXTINP_IRMASK(UART_EXTINP_IR_CTS) | \
68ab4382d2SGreg Kroah-Hartman UART_EXTINP_IRMASK(UART_EXTINP_IR_DCD))
69ab4382d2SGreg Kroah-Hartman
70ab4382d2SGreg Kroah-Hartman /*
71ab4382d2SGreg Kroah-Hartman * handy uart register accessor
72ab4382d2SGreg Kroah-Hartman */
bcm_uart_readl(struct uart_port * port,unsigned int offset)73ab4382d2SGreg Kroah-Hartman static inline unsigned int bcm_uart_readl(struct uart_port *port,
74ab4382d2SGreg Kroah-Hartman unsigned int offset)
75ab4382d2SGreg Kroah-Hartman {
7699cf9790SFlorian Fainelli return __raw_readl(port->membase + offset);
77ab4382d2SGreg Kroah-Hartman }
78ab4382d2SGreg Kroah-Hartman
bcm_uart_writel(struct uart_port * port,unsigned int value,unsigned int offset)79ab4382d2SGreg Kroah-Hartman static inline void bcm_uart_writel(struct uart_port *port,
80ab4382d2SGreg Kroah-Hartman unsigned int value, unsigned int offset)
81ab4382d2SGreg Kroah-Hartman {
8299cf9790SFlorian Fainelli __raw_writel(value, port->membase + offset);
83ab4382d2SGreg Kroah-Hartman }
84ab4382d2SGreg Kroah-Hartman
85ab4382d2SGreg Kroah-Hartman /*
86ab4382d2SGreg Kroah-Hartman * serial core request to check if uart tx fifo is empty
87ab4382d2SGreg Kroah-Hartman */
bcm_uart_tx_empty(struct uart_port * port)88ab4382d2SGreg Kroah-Hartman static unsigned int bcm_uart_tx_empty(struct uart_port *port)
89ab4382d2SGreg Kroah-Hartman {
90ab4382d2SGreg Kroah-Hartman unsigned int val;
91ab4382d2SGreg Kroah-Hartman
92ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG);
93ab4382d2SGreg Kroah-Hartman return (val & UART_IR_STAT(UART_IR_TXEMPTY)) ? 1 : 0;
94ab4382d2SGreg Kroah-Hartman }
95ab4382d2SGreg Kroah-Hartman
96ab4382d2SGreg Kroah-Hartman /*
97ab4382d2SGreg Kroah-Hartman * serial core request to set RTS and DTR pin state and loopback mode
98ab4382d2SGreg Kroah-Hartman */
bcm_uart_set_mctrl(struct uart_port * port,unsigned int mctrl)99ab4382d2SGreg Kroah-Hartman static void bcm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
100ab4382d2SGreg Kroah-Hartman {
101ab4382d2SGreg Kroah-Hartman unsigned int val;
102ab4382d2SGreg Kroah-Hartman
103ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_MCTL_REG);
104ab4382d2SGreg Kroah-Hartman val &= ~(UART_MCTL_DTR_MASK | UART_MCTL_RTS_MASK);
105ab4382d2SGreg Kroah-Hartman /* invert of written value is reflected on the pin */
106ab4382d2SGreg Kroah-Hartman if (!(mctrl & TIOCM_DTR))
107ab4382d2SGreg Kroah-Hartman val |= UART_MCTL_DTR_MASK;
108ab4382d2SGreg Kroah-Hartman if (!(mctrl & TIOCM_RTS))
109ab4382d2SGreg Kroah-Hartman val |= UART_MCTL_RTS_MASK;
110ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_MCTL_REG);
111ab4382d2SGreg Kroah-Hartman
112ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG);
113ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_LOOP)
114ab4382d2SGreg Kroah-Hartman val |= UART_CTL_LOOPBACK_MASK;
115ab4382d2SGreg Kroah-Hartman else
116ab4382d2SGreg Kroah-Hartman val &= ~UART_CTL_LOOPBACK_MASK;
117ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG);
118ab4382d2SGreg Kroah-Hartman }
119ab4382d2SGreg Kroah-Hartman
120ab4382d2SGreg Kroah-Hartman /*
121ab4382d2SGreg Kroah-Hartman * serial core request to return RI, CTS, DCD and DSR pin state
122ab4382d2SGreg Kroah-Hartman */
bcm_uart_get_mctrl(struct uart_port * port)123ab4382d2SGreg Kroah-Hartman static unsigned int bcm_uart_get_mctrl(struct uart_port *port)
124ab4382d2SGreg Kroah-Hartman {
125ab4382d2SGreg Kroah-Hartman unsigned int val, mctrl;
126ab4382d2SGreg Kroah-Hartman
127ab4382d2SGreg Kroah-Hartman mctrl = 0;
128ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_EXTINP_REG);
129ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_RI_MASK)
130ab4382d2SGreg Kroah-Hartman mctrl |= TIOCM_RI;
131ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_CTS_MASK)
132ab4382d2SGreg Kroah-Hartman mctrl |= TIOCM_CTS;
133ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_DCD_MASK)
134ab4382d2SGreg Kroah-Hartman mctrl |= TIOCM_CD;
135ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_DSR_MASK)
136ab4382d2SGreg Kroah-Hartman mctrl |= TIOCM_DSR;
137ab4382d2SGreg Kroah-Hartman return mctrl;
138ab4382d2SGreg Kroah-Hartman }
139ab4382d2SGreg Kroah-Hartman
140ab4382d2SGreg Kroah-Hartman /*
141ab4382d2SGreg Kroah-Hartman * serial core request to disable tx ASAP (used for flow control)
142ab4382d2SGreg Kroah-Hartman */
bcm_uart_stop_tx(struct uart_port * port)143ab4382d2SGreg Kroah-Hartman static void bcm_uart_stop_tx(struct uart_port *port)
144ab4382d2SGreg Kroah-Hartman {
145ab4382d2SGreg Kroah-Hartman unsigned int val;
146ab4382d2SGreg Kroah-Hartman
147ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG);
148ab4382d2SGreg Kroah-Hartman val &= ~(UART_CTL_TXEN_MASK);
149ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG);
150ab4382d2SGreg Kroah-Hartman
151ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG);
152ab4382d2SGreg Kroah-Hartman val &= ~UART_TX_INT_MASK;
153ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG);
154ab4382d2SGreg Kroah-Hartman }
155ab4382d2SGreg Kroah-Hartman
156ab4382d2SGreg Kroah-Hartman /*
157ab4382d2SGreg Kroah-Hartman * serial core request to (re)enable tx
158ab4382d2SGreg Kroah-Hartman */
bcm_uart_start_tx(struct uart_port * port)159ab4382d2SGreg Kroah-Hartman static void bcm_uart_start_tx(struct uart_port *port)
160ab4382d2SGreg Kroah-Hartman {
161ab4382d2SGreg Kroah-Hartman unsigned int val;
162ab4382d2SGreg Kroah-Hartman
163ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG);
164ab4382d2SGreg Kroah-Hartman val |= UART_TX_INT_MASK;
165ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG);
166ab4382d2SGreg Kroah-Hartman
167ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG);
168ab4382d2SGreg Kroah-Hartman val |= UART_CTL_TXEN_MASK;
169ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG);
170ab4382d2SGreg Kroah-Hartman }
171ab4382d2SGreg Kroah-Hartman
172ab4382d2SGreg Kroah-Hartman /*
173ab4382d2SGreg Kroah-Hartman * serial core request to stop rx, called before port shutdown
174ab4382d2SGreg Kroah-Hartman */
bcm_uart_stop_rx(struct uart_port * port)175ab4382d2SGreg Kroah-Hartman static void bcm_uart_stop_rx(struct uart_port *port)
176ab4382d2SGreg Kroah-Hartman {
177ab4382d2SGreg Kroah-Hartman unsigned int val;
178ab4382d2SGreg Kroah-Hartman
179ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG);
180ab4382d2SGreg Kroah-Hartman val &= ~UART_RX_INT_MASK;
181ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG);
182ab4382d2SGreg Kroah-Hartman }
183ab4382d2SGreg Kroah-Hartman
184ab4382d2SGreg Kroah-Hartman /*
185ab4382d2SGreg Kroah-Hartman * serial core request to enable modem status interrupt reporting
186ab4382d2SGreg Kroah-Hartman */
bcm_uart_enable_ms(struct uart_port * port)187ab4382d2SGreg Kroah-Hartman static void bcm_uart_enable_ms(struct uart_port *port)
188ab4382d2SGreg Kroah-Hartman {
189ab4382d2SGreg Kroah-Hartman unsigned int val;
190ab4382d2SGreg Kroah-Hartman
191ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG);
192ab4382d2SGreg Kroah-Hartman val |= UART_IR_MASK(UART_IR_EXTIP);
193ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG);
194ab4382d2SGreg Kroah-Hartman }
195ab4382d2SGreg Kroah-Hartman
196ab4382d2SGreg Kroah-Hartman /*
197ab4382d2SGreg Kroah-Hartman * serial core request to start/stop emitting break char
198ab4382d2SGreg Kroah-Hartman */
bcm_uart_break_ctl(struct uart_port * port,int ctl)199ab4382d2SGreg Kroah-Hartman static void bcm_uart_break_ctl(struct uart_port *port, int ctl)
200ab4382d2SGreg Kroah-Hartman {
201ab4382d2SGreg Kroah-Hartman unsigned long flags;
202ab4382d2SGreg Kroah-Hartman unsigned int val;
203ab4382d2SGreg Kroah-Hartman
204ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags);
205ab4382d2SGreg Kroah-Hartman
206ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG);
207ab4382d2SGreg Kroah-Hartman if (ctl)
208ab4382d2SGreg Kroah-Hartman val |= UART_CTL_XMITBRK_MASK;
209ab4382d2SGreg Kroah-Hartman else
210ab4382d2SGreg Kroah-Hartman val &= ~UART_CTL_XMITBRK_MASK;
211ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG);
212ab4382d2SGreg Kroah-Hartman
213ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags);
214ab4382d2SGreg Kroah-Hartman }
215ab4382d2SGreg Kroah-Hartman
216ab4382d2SGreg Kroah-Hartman /*
217ab4382d2SGreg Kroah-Hartman * return port type in string format
218ab4382d2SGreg Kroah-Hartman */
bcm_uart_type(struct uart_port * port)219ab4382d2SGreg Kroah-Hartman static const char *bcm_uart_type(struct uart_port *port)
220ab4382d2SGreg Kroah-Hartman {
221ab4382d2SGreg Kroah-Hartman return (port->type == PORT_BCM63XX) ? "bcm63xx_uart" : NULL;
222ab4382d2SGreg Kroah-Hartman }
223ab4382d2SGreg Kroah-Hartman
224ab4382d2SGreg Kroah-Hartman /*
225ab4382d2SGreg Kroah-Hartman * read all chars in rx fifo and send them to core
226ab4382d2SGreg Kroah-Hartman */
bcm_uart_do_rx(struct uart_port * port)227ab4382d2SGreg Kroah-Hartman static void bcm_uart_do_rx(struct uart_port *port)
228ab4382d2SGreg Kroah-Hartman {
229064256feSJonas Gorski struct tty_port *tty_port = &port->state->port;
230ab4382d2SGreg Kroah-Hartman unsigned int max_count;
231ab4382d2SGreg Kroah-Hartman
232ab4382d2SGreg Kroah-Hartman /* limit number of char read in interrupt, should not be
233ab4382d2SGreg Kroah-Hartman * higher than fifo size anyway since we're much faster than
234ab4382d2SGreg Kroah-Hartman * serial port */
235ab4382d2SGreg Kroah-Hartman max_count = 32;
236ab4382d2SGreg Kroah-Hartman do {
237ab4382d2SGreg Kroah-Hartman unsigned int iestat, c, cstat;
238ab4382d2SGreg Kroah-Hartman char flag;
239ab4382d2SGreg Kroah-Hartman
240ab4382d2SGreg Kroah-Hartman /* get overrun/fifo empty information from ier
241ab4382d2SGreg Kroah-Hartman * register */
242ab4382d2SGreg Kroah-Hartman iestat = bcm_uart_readl(port, UART_IR_REG);
2433bc46b31SMaxime Bizon
2443bc46b31SMaxime Bizon if (unlikely(iestat & UART_IR_STAT(UART_IR_RXOVER))) {
2453bc46b31SMaxime Bizon unsigned int val;
2463bc46b31SMaxime Bizon
2473bc46b31SMaxime Bizon /* fifo reset is required to clear
2483bc46b31SMaxime Bizon * interrupt */
2493bc46b31SMaxime Bizon val = bcm_uart_readl(port, UART_CTL_REG);
2503bc46b31SMaxime Bizon val |= UART_CTL_RSTRXFIFO_MASK;
2513bc46b31SMaxime Bizon bcm_uart_writel(port, val, UART_CTL_REG);
2523bc46b31SMaxime Bizon
2533bc46b31SMaxime Bizon port->icount.overrun++;
254064256feSJonas Gorski tty_insert_flip_char(tty_port, 0, TTY_OVERRUN);
2553bc46b31SMaxime Bizon }
2563bc46b31SMaxime Bizon
257ab4382d2SGreg Kroah-Hartman if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
258ab4382d2SGreg Kroah-Hartman break;
259ab4382d2SGreg Kroah-Hartman
260ab4382d2SGreg Kroah-Hartman cstat = c = bcm_uart_readl(port, UART_FIFO_REG);
261ab4382d2SGreg Kroah-Hartman port->icount.rx++;
262ab4382d2SGreg Kroah-Hartman flag = TTY_NORMAL;
263ab4382d2SGreg Kroah-Hartman c &= 0xff;
264ab4382d2SGreg Kroah-Hartman
265ab4382d2SGreg Kroah-Hartman if (unlikely((cstat & UART_FIFO_ANYERR_MASK))) {
266ab4382d2SGreg Kroah-Hartman /* do stats first */
267ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_BRKDET_MASK) {
268ab4382d2SGreg Kroah-Hartman port->icount.brk++;
269ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port))
270ab4382d2SGreg Kroah-Hartman continue;
271ab4382d2SGreg Kroah-Hartman }
272ab4382d2SGreg Kroah-Hartman
273ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_PARERR_MASK)
274ab4382d2SGreg Kroah-Hartman port->icount.parity++;
275ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_FRAMEERR_MASK)
276ab4382d2SGreg Kroah-Hartman port->icount.frame++;
277ab4382d2SGreg Kroah-Hartman
278ab4382d2SGreg Kroah-Hartman /* update flag wrt read_status_mask */
279ab4382d2SGreg Kroah-Hartman cstat &= port->read_status_mask;
280ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_BRKDET_MASK)
281ab4382d2SGreg Kroah-Hartman flag = TTY_BREAK;
282ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_FRAMEERR_MASK)
283ab4382d2SGreg Kroah-Hartman flag = TTY_FRAME;
284ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_PARERR_MASK)
285ab4382d2SGreg Kroah-Hartman flag = TTY_PARITY;
286ab4382d2SGreg Kroah-Hartman }
287ab4382d2SGreg Kroah-Hartman
288ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c))
289ab4382d2SGreg Kroah-Hartman continue;
290ab4382d2SGreg Kroah-Hartman
291ab4382d2SGreg Kroah-Hartman
292ab4382d2SGreg Kroah-Hartman if ((cstat & port->ignore_status_mask) == 0)
293064256feSJonas Gorski tty_insert_flip_char(tty_port, c, flag);
294ab4382d2SGreg Kroah-Hartman
295ab4382d2SGreg Kroah-Hartman } while (--max_count);
296ab4382d2SGreg Kroah-Hartman
297064256feSJonas Gorski tty_flip_buffer_push(tty_port);
298ab4382d2SGreg Kroah-Hartman }
299ab4382d2SGreg Kroah-Hartman
300ab4382d2SGreg Kroah-Hartman /*
301ab4382d2SGreg Kroah-Hartman * fill tx fifo with chars to send, stop when fifo is about to be full
302ab4382d2SGreg Kroah-Hartman * or when all chars have been sent.
303ab4382d2SGreg Kroah-Hartman */
bcm_uart_do_tx(struct uart_port * port)304ab4382d2SGreg Kroah-Hartman static void bcm_uart_do_tx(struct uart_port *port)
305ab4382d2SGreg Kroah-Hartman {
306d11cc8c3SJiri Slaby (SUSE) unsigned int val;
307d11cc8c3SJiri Slaby (SUSE) bool pending;
308d11cc8c3SJiri Slaby (SUSE) u8 ch;
309ab4382d2SGreg Kroah-Hartman
310ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_MCTL_REG);
311ab4382d2SGreg Kroah-Hartman val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT;
312*d3ebc559SJonas Gorski pending = uart_port_tx_limited_flags(port, ch, UART_TX_NOSTOP,
313*d3ebc559SJonas Gorski port->fifosize - val,
314d11cc8c3SJiri Slaby (SUSE) true,
315d11cc8c3SJiri Slaby (SUSE) bcm_uart_writel(port, ch, UART_FIFO_REG),
316d11cc8c3SJiri Slaby (SUSE) ({}));
317d11cc8c3SJiri Slaby (SUSE) if (pending)
318ab4382d2SGreg Kroah-Hartman return;
319ab4382d2SGreg Kroah-Hartman
320ab4382d2SGreg Kroah-Hartman /* nothing to send, disable transmit interrupt */
321ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG);
322ab4382d2SGreg Kroah-Hartman val &= ~UART_TX_INT_MASK;
323ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG);
324*d3ebc559SJonas Gorski
325*d3ebc559SJonas Gorski if (uart_tx_stopped(port))
326*d3ebc559SJonas Gorski bcm_uart_stop_tx(port);
327ab4382d2SGreg Kroah-Hartman }
328ab4382d2SGreg Kroah-Hartman
329ab4382d2SGreg Kroah-Hartman /*
330ab4382d2SGreg Kroah-Hartman * process uart interrupt
331ab4382d2SGreg Kroah-Hartman */
bcm_uart_interrupt(int irq,void * dev_id)332ab4382d2SGreg Kroah-Hartman static irqreturn_t bcm_uart_interrupt(int irq, void *dev_id)
333ab4382d2SGreg Kroah-Hartman {
334ab4382d2SGreg Kroah-Hartman struct uart_port *port;
335ab4382d2SGreg Kroah-Hartman unsigned int irqstat;
336ab4382d2SGreg Kroah-Hartman
337ab4382d2SGreg Kroah-Hartman port = dev_id;
338ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock);
339ab4382d2SGreg Kroah-Hartman
340ab4382d2SGreg Kroah-Hartman irqstat = bcm_uart_readl(port, UART_IR_REG);
341ab4382d2SGreg Kroah-Hartman if (irqstat & UART_RX_INT_STAT)
342ab4382d2SGreg Kroah-Hartman bcm_uart_do_rx(port);
343ab4382d2SGreg Kroah-Hartman
344ab4382d2SGreg Kroah-Hartman if (irqstat & UART_TX_INT_STAT)
345ab4382d2SGreg Kroah-Hartman bcm_uart_do_tx(port);
346ab4382d2SGreg Kroah-Hartman
347ab4382d2SGreg Kroah-Hartman if (irqstat & UART_IR_MASK(UART_IR_EXTIP)) {
348ab4382d2SGreg Kroah-Hartman unsigned int estat;
349ab4382d2SGreg Kroah-Hartman
350ab4382d2SGreg Kroah-Hartman estat = bcm_uart_readl(port, UART_EXTINP_REG);
351ab4382d2SGreg Kroah-Hartman if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_CTS))
352ab4382d2SGreg Kroah-Hartman uart_handle_cts_change(port,
353ab4382d2SGreg Kroah-Hartman estat & UART_EXTINP_CTS_MASK);
354ab4382d2SGreg Kroah-Hartman if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_DCD))
355ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port,
356ab4382d2SGreg Kroah-Hartman estat & UART_EXTINP_DCD_MASK);
357ab4382d2SGreg Kroah-Hartman }
358ab4382d2SGreg Kroah-Hartman
359ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock);
360ab4382d2SGreg Kroah-Hartman return IRQ_HANDLED;
361ab4382d2SGreg Kroah-Hartman }
362ab4382d2SGreg Kroah-Hartman
363ab4382d2SGreg Kroah-Hartman /*
364ab4382d2SGreg Kroah-Hartman * enable rx & tx operation on uart
365ab4382d2SGreg Kroah-Hartman */
bcm_uart_enable(struct uart_port * port)366ab4382d2SGreg Kroah-Hartman static void bcm_uart_enable(struct uart_port *port)
367ab4382d2SGreg Kroah-Hartman {
368ab4382d2SGreg Kroah-Hartman unsigned int val;
369ab4382d2SGreg Kroah-Hartman
370ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG);
371ab4382d2SGreg Kroah-Hartman val |= (UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK);
372ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG);
373ab4382d2SGreg Kroah-Hartman }
374ab4382d2SGreg Kroah-Hartman
375ab4382d2SGreg Kroah-Hartman /*
376ab4382d2SGreg Kroah-Hartman * disable rx & tx operation on uart
377ab4382d2SGreg Kroah-Hartman */
bcm_uart_disable(struct uart_port * port)378ab4382d2SGreg Kroah-Hartman static void bcm_uart_disable(struct uart_port *port)
379ab4382d2SGreg Kroah-Hartman {
380ab4382d2SGreg Kroah-Hartman unsigned int val;
381ab4382d2SGreg Kroah-Hartman
382ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG);
383ab4382d2SGreg Kroah-Hartman val &= ~(UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK |
384ab4382d2SGreg Kroah-Hartman UART_CTL_RXEN_MASK);
385ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG);
386ab4382d2SGreg Kroah-Hartman }
387ab4382d2SGreg Kroah-Hartman
388ab4382d2SGreg Kroah-Hartman /*
389ab4382d2SGreg Kroah-Hartman * clear all unread data in rx fifo and unsent data in tx fifo
390ab4382d2SGreg Kroah-Hartman */
bcm_uart_flush(struct uart_port * port)391ab4382d2SGreg Kroah-Hartman static void bcm_uart_flush(struct uart_port *port)
392ab4382d2SGreg Kroah-Hartman {
393ab4382d2SGreg Kroah-Hartman unsigned int val;
394ab4382d2SGreg Kroah-Hartman
395ab4382d2SGreg Kroah-Hartman /* empty rx and tx fifo */
396ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG);
397ab4382d2SGreg Kroah-Hartman val |= UART_CTL_RSTRXFIFO_MASK | UART_CTL_RSTTXFIFO_MASK;
398ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG);
399ab4382d2SGreg Kroah-Hartman
400ab4382d2SGreg Kroah-Hartman /* read any pending char to make sure all irq status are
401ab4382d2SGreg Kroah-Hartman * cleared */
402ab4382d2SGreg Kroah-Hartman (void)bcm_uart_readl(port, UART_FIFO_REG);
403ab4382d2SGreg Kroah-Hartman }
404ab4382d2SGreg Kroah-Hartman
405ab4382d2SGreg Kroah-Hartman /*
406ab4382d2SGreg Kroah-Hartman * serial core request to initialize uart and start rx operation
407ab4382d2SGreg Kroah-Hartman */
bcm_uart_startup(struct uart_port * port)408ab4382d2SGreg Kroah-Hartman static int bcm_uart_startup(struct uart_port *port)
409ab4382d2SGreg Kroah-Hartman {
410ab4382d2SGreg Kroah-Hartman unsigned int val;
411ab4382d2SGreg Kroah-Hartman int ret;
412ab4382d2SGreg Kroah-Hartman
413ab4382d2SGreg Kroah-Hartman /* mask all irq and flush port */
414ab4382d2SGreg Kroah-Hartman bcm_uart_disable(port);
415ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, 0, UART_IR_REG);
416ab4382d2SGreg Kroah-Hartman bcm_uart_flush(port);
417ab4382d2SGreg Kroah-Hartman
418ab4382d2SGreg Kroah-Hartman /* clear any pending external input interrupt */
419ab4382d2SGreg Kroah-Hartman (void)bcm_uart_readl(port, UART_EXTINP_REG);
420ab4382d2SGreg Kroah-Hartman
421ab4382d2SGreg Kroah-Hartman /* set rx/tx fifo thresh to fifo half size */
422ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_MCTL_REG);
423ab4382d2SGreg Kroah-Hartman val &= ~(UART_MCTL_RXFIFOTHRESH_MASK | UART_MCTL_TXFIFOTHRESH_MASK);
424ab4382d2SGreg Kroah-Hartman val |= (port->fifosize / 2) << UART_MCTL_RXFIFOTHRESH_SHIFT;
425ab4382d2SGreg Kroah-Hartman val |= (port->fifosize / 2) << UART_MCTL_TXFIFOTHRESH_SHIFT;
426ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_MCTL_REG);
427ab4382d2SGreg Kroah-Hartman
428ab4382d2SGreg Kroah-Hartman /* set rx fifo timeout to 1 char time */
429ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG);
430ab4382d2SGreg Kroah-Hartman val &= ~UART_CTL_RXTMOUTCNT_MASK;
431ab4382d2SGreg Kroah-Hartman val |= 1 << UART_CTL_RXTMOUTCNT_SHIFT;
432ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG);
433ab4382d2SGreg Kroah-Hartman
434ab4382d2SGreg Kroah-Hartman /* report any edge on dcd and cts */
435ab4382d2SGreg Kroah-Hartman val = UART_EXTINP_INT_MASK;
436ab4382d2SGreg Kroah-Hartman val |= UART_EXTINP_DCD_NOSENSE_MASK;
437ab4382d2SGreg Kroah-Hartman val |= UART_EXTINP_CTS_NOSENSE_MASK;
438ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_EXTINP_REG);
439ab4382d2SGreg Kroah-Hartman
440ab4382d2SGreg Kroah-Hartman /* register irq and enable rx interrupts */
441ab4382d2SGreg Kroah-Hartman ret = request_irq(port->irq, bcm_uart_interrupt, 0,
44283f7fa6cSSimon Arlott dev_name(port->dev), port);
443ab4382d2SGreg Kroah-Hartman if (ret)
444ab4382d2SGreg Kroah-Hartman return ret;
445ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, UART_RX_INT_MASK, UART_IR_REG);
446ab4382d2SGreg Kroah-Hartman bcm_uart_enable(port);
447ab4382d2SGreg Kroah-Hartman return 0;
448ab4382d2SGreg Kroah-Hartman }
449ab4382d2SGreg Kroah-Hartman
450ab4382d2SGreg Kroah-Hartman /*
451ab4382d2SGreg Kroah-Hartman * serial core request to flush & disable uart
452ab4382d2SGreg Kroah-Hartman */
bcm_uart_shutdown(struct uart_port * port)453ab4382d2SGreg Kroah-Hartman static void bcm_uart_shutdown(struct uart_port *port)
454ab4382d2SGreg Kroah-Hartman {
455ab4382d2SGreg Kroah-Hartman unsigned long flags;
456ab4382d2SGreg Kroah-Hartman
457ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags);
458ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, 0, UART_IR_REG);
459ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags);
460ab4382d2SGreg Kroah-Hartman
461ab4382d2SGreg Kroah-Hartman bcm_uart_disable(port);
462ab4382d2SGreg Kroah-Hartman bcm_uart_flush(port);
463ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port);
464ab4382d2SGreg Kroah-Hartman }
465ab4382d2SGreg Kroah-Hartman
466ab4382d2SGreg Kroah-Hartman /*
467ab4382d2SGreg Kroah-Hartman * serial core request to change current uart setting
468ab4382d2SGreg Kroah-Hartman */
bcm_uart_set_termios(struct uart_port * port,struct ktermios * new,const struct ktermios * old)469bec5b814SIlpo Järvinen static void bcm_uart_set_termios(struct uart_port *port, struct ktermios *new,
470bec5b814SIlpo Järvinen const struct ktermios *old)
471ab4382d2SGreg Kroah-Hartman {
472ab4382d2SGreg Kroah-Hartman unsigned int ctl, baud, quot, ier;
473ab4382d2SGreg Kroah-Hartman unsigned long flags;
4740e5ec414SRussell Enderby int tries;
475ab4382d2SGreg Kroah-Hartman
476ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags);
477ab4382d2SGreg Kroah-Hartman
4780e5ec414SRussell Enderby /* Drain the hot tub fully before we power it off for the winter. */
4790e5ec414SRussell Enderby for (tries = 3; !bcm_uart_tx_empty(port) && tries; tries--)
4800e5ec414SRussell Enderby mdelay(10);
4810e5ec414SRussell Enderby
482ab4382d2SGreg Kroah-Hartman /* disable uart while changing speed */
483ab4382d2SGreg Kroah-Hartman bcm_uart_disable(port);
484ab4382d2SGreg Kroah-Hartman bcm_uart_flush(port);
485ab4382d2SGreg Kroah-Hartman
486ab4382d2SGreg Kroah-Hartman /* update Control register */
487ab4382d2SGreg Kroah-Hartman ctl = bcm_uart_readl(port, UART_CTL_REG);
488ab4382d2SGreg Kroah-Hartman ctl &= ~UART_CTL_BITSPERSYM_MASK;
489ab4382d2SGreg Kroah-Hartman
490ab4382d2SGreg Kroah-Hartman switch (new->c_cflag & CSIZE) {
491ab4382d2SGreg Kroah-Hartman case CS5:
492ab4382d2SGreg Kroah-Hartman ctl |= (0 << UART_CTL_BITSPERSYM_SHIFT);
493ab4382d2SGreg Kroah-Hartman break;
494ab4382d2SGreg Kroah-Hartman case CS6:
495ab4382d2SGreg Kroah-Hartman ctl |= (1 << UART_CTL_BITSPERSYM_SHIFT);
496ab4382d2SGreg Kroah-Hartman break;
497ab4382d2SGreg Kroah-Hartman case CS7:
498ab4382d2SGreg Kroah-Hartman ctl |= (2 << UART_CTL_BITSPERSYM_SHIFT);
499ab4382d2SGreg Kroah-Hartman break;
500ab4382d2SGreg Kroah-Hartman default:
501ab4382d2SGreg Kroah-Hartman ctl |= (3 << UART_CTL_BITSPERSYM_SHIFT);
502ab4382d2SGreg Kroah-Hartman break;
503ab4382d2SGreg Kroah-Hartman }
504ab4382d2SGreg Kroah-Hartman
505ab4382d2SGreg Kroah-Hartman ctl &= ~UART_CTL_STOPBITS_MASK;
506ab4382d2SGreg Kroah-Hartman if (new->c_cflag & CSTOPB)
507ab4382d2SGreg Kroah-Hartman ctl |= UART_CTL_STOPBITS_2;
508ab4382d2SGreg Kroah-Hartman else
509ab4382d2SGreg Kroah-Hartman ctl |= UART_CTL_STOPBITS_1;
510ab4382d2SGreg Kroah-Hartman
511ab4382d2SGreg Kroah-Hartman ctl &= ~(UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
512ab4382d2SGreg Kroah-Hartman if (new->c_cflag & PARENB)
513ab4382d2SGreg Kroah-Hartman ctl |= (UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
514ab4382d2SGreg Kroah-Hartman ctl &= ~(UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
515ab4382d2SGreg Kroah-Hartman if (new->c_cflag & PARODD)
516ab4382d2SGreg Kroah-Hartman ctl |= (UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
517ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, ctl, UART_CTL_REG);
518ab4382d2SGreg Kroah-Hartman
519ab4382d2SGreg Kroah-Hartman /* update Baudword register */
520ab4382d2SGreg Kroah-Hartman baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
521ab4382d2SGreg Kroah-Hartman quot = uart_get_divisor(port, baud) - 1;
522ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, quot, UART_BAUD_REG);
523ab4382d2SGreg Kroah-Hartman
524ab4382d2SGreg Kroah-Hartman /* update Interrupt register */
525ab4382d2SGreg Kroah-Hartman ier = bcm_uart_readl(port, UART_IR_REG);
526ab4382d2SGreg Kroah-Hartman
527ab4382d2SGreg Kroah-Hartman ier &= ~UART_IR_MASK(UART_IR_EXTIP);
528ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(port, new->c_cflag))
529ab4382d2SGreg Kroah-Hartman ier |= UART_IR_MASK(UART_IR_EXTIP);
530ab4382d2SGreg Kroah-Hartman
531ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, ier, UART_IR_REG);
532ab4382d2SGreg Kroah-Hartman
533ab4382d2SGreg Kroah-Hartman /* update read/ignore mask */
534ab4382d2SGreg Kroah-Hartman port->read_status_mask = UART_FIFO_VALID_MASK;
535ab4382d2SGreg Kroah-Hartman if (new->c_iflag & INPCK) {
536ab4382d2SGreg Kroah-Hartman port->read_status_mask |= UART_FIFO_FRAMEERR_MASK;
537ab4382d2SGreg Kroah-Hartman port->read_status_mask |= UART_FIFO_PARERR_MASK;
538ab4382d2SGreg Kroah-Hartman }
539ef8b9ddcSPeter Hurley if (new->c_iflag & (IGNBRK | BRKINT))
540ab4382d2SGreg Kroah-Hartman port->read_status_mask |= UART_FIFO_BRKDET_MASK;
541ab4382d2SGreg Kroah-Hartman
542ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0;
543ab4382d2SGreg Kroah-Hartman if (new->c_iflag & IGNPAR)
544ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= UART_FIFO_PARERR_MASK;
545ab4382d2SGreg Kroah-Hartman if (new->c_iflag & IGNBRK)
546ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= UART_FIFO_BRKDET_MASK;
547ab4382d2SGreg Kroah-Hartman if (!(new->c_cflag & CREAD))
548ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= UART_FIFO_VALID_MASK;
549ab4382d2SGreg Kroah-Hartman
550ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, new->c_cflag, baud);
551ab4382d2SGreg Kroah-Hartman bcm_uart_enable(port);
552ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags);
553ab4382d2SGreg Kroah-Hartman }
554ab4382d2SGreg Kroah-Hartman
555ab4382d2SGreg Kroah-Hartman /*
556ab4382d2SGreg Kroah-Hartman * serial core request to claim uart iomem
557ab4382d2SGreg Kroah-Hartman */
bcm_uart_request_port(struct uart_port * port)558ab4382d2SGreg Kroah-Hartman static int bcm_uart_request_port(struct uart_port *port)
559ab4382d2SGreg Kroah-Hartman {
560e979f3b7SKevin Cernekee /* UARTs always present */
561ab4382d2SGreg Kroah-Hartman return 0;
562ab4382d2SGreg Kroah-Hartman }
563ab4382d2SGreg Kroah-Hartman
564ab4382d2SGreg Kroah-Hartman /*
565ab4382d2SGreg Kroah-Hartman * serial core request to release uart iomem
566ab4382d2SGreg Kroah-Hartman */
bcm_uart_release_port(struct uart_port * port)567ab4382d2SGreg Kroah-Hartman static void bcm_uart_release_port(struct uart_port *port)
568ab4382d2SGreg Kroah-Hartman {
569e979f3b7SKevin Cernekee /* Nothing to release ... */
570ab4382d2SGreg Kroah-Hartman }
571ab4382d2SGreg Kroah-Hartman
572ab4382d2SGreg Kroah-Hartman /*
573ab4382d2SGreg Kroah-Hartman * serial core request to do any port required autoconfiguration
574ab4382d2SGreg Kroah-Hartman */
bcm_uart_config_port(struct uart_port * port,int flags)575ab4382d2SGreg Kroah-Hartman static void bcm_uart_config_port(struct uart_port *port, int flags)
576ab4382d2SGreg Kroah-Hartman {
577ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE) {
578ab4382d2SGreg Kroah-Hartman if (bcm_uart_request_port(port))
579ab4382d2SGreg Kroah-Hartman return;
580ab4382d2SGreg Kroah-Hartman port->type = PORT_BCM63XX;
581ab4382d2SGreg Kroah-Hartman }
582ab4382d2SGreg Kroah-Hartman }
583ab4382d2SGreg Kroah-Hartman
584ab4382d2SGreg Kroah-Hartman /*
585ab4382d2SGreg Kroah-Hartman * serial core request to check that port information in serinfo are
586ab4382d2SGreg Kroah-Hartman * suitable
587ab4382d2SGreg Kroah-Hartman */
bcm_uart_verify_port(struct uart_port * port,struct serial_struct * serinfo)588ab4382d2SGreg Kroah-Hartman static int bcm_uart_verify_port(struct uart_port *port,
589ab4382d2SGreg Kroah-Hartman struct serial_struct *serinfo)
590ab4382d2SGreg Kroah-Hartman {
591ab4382d2SGreg Kroah-Hartman if (port->type != PORT_BCM63XX)
592ab4382d2SGreg Kroah-Hartman return -EINVAL;
593ab4382d2SGreg Kroah-Hartman if (port->irq != serinfo->irq)
594ab4382d2SGreg Kroah-Hartman return -EINVAL;
595ab4382d2SGreg Kroah-Hartman if (port->iotype != serinfo->io_type)
596ab4382d2SGreg Kroah-Hartman return -EINVAL;
597ab4382d2SGreg Kroah-Hartman if (port->mapbase != (unsigned long)serinfo->iomem_base)
598ab4382d2SGreg Kroah-Hartman return -EINVAL;
599ab4382d2SGreg Kroah-Hartman return 0;
600ab4382d2SGreg Kroah-Hartman }
601ab4382d2SGreg Kroah-Hartman
602945de8beSArend van Spriel #ifdef CONFIG_CONSOLE_POLL
603945de8beSArend van Spriel /*
604945de8beSArend van Spriel * return true when outstanding tx equals fifo size
605945de8beSArend van Spriel */
bcm_uart_tx_full(struct uart_port * port)606945de8beSArend van Spriel static bool bcm_uart_tx_full(struct uart_port *port)
607945de8beSArend van Spriel {
608945de8beSArend van Spriel unsigned int val;
609945de8beSArend van Spriel
610945de8beSArend van Spriel val = bcm_uart_readl(port, UART_MCTL_REG);
611945de8beSArend van Spriel val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT;
612945de8beSArend van Spriel return !(port->fifosize - val);
613945de8beSArend van Spriel }
614945de8beSArend van Spriel
bcm_uart_poll_get_char(struct uart_port * port)615945de8beSArend van Spriel static int bcm_uart_poll_get_char(struct uart_port *port)
616945de8beSArend van Spriel {
617945de8beSArend van Spriel unsigned int iestat;
618945de8beSArend van Spriel
619945de8beSArend van Spriel iestat = bcm_uart_readl(port, UART_IR_REG);
620945de8beSArend van Spriel if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
621945de8beSArend van Spriel return NO_POLL_CHAR;
622945de8beSArend van Spriel
623945de8beSArend van Spriel return bcm_uart_readl(port, UART_FIFO_REG);
624945de8beSArend van Spriel }
625945de8beSArend van Spriel
bcm_uart_poll_put_char(struct uart_port * port,unsigned char c)626945de8beSArend van Spriel static void bcm_uart_poll_put_char(struct uart_port *port, unsigned char c)
627945de8beSArend van Spriel {
628945de8beSArend van Spriel while (bcm_uart_tx_full(port)) {
629945de8beSArend van Spriel cpu_relax();
630945de8beSArend van Spriel }
631945de8beSArend van Spriel
632945de8beSArend van Spriel bcm_uart_writel(port, c, UART_FIFO_REG);
633945de8beSArend van Spriel }
634945de8beSArend van Spriel #endif
635945de8beSArend van Spriel
636ab4382d2SGreg Kroah-Hartman /* serial core callbacks */
6371f1d8703SJulia Lawall static const struct uart_ops bcm_uart_ops = {
638ab4382d2SGreg Kroah-Hartman .tx_empty = bcm_uart_tx_empty,
639ab4382d2SGreg Kroah-Hartman .get_mctrl = bcm_uart_get_mctrl,
640ab4382d2SGreg Kroah-Hartman .set_mctrl = bcm_uart_set_mctrl,
641ab4382d2SGreg Kroah-Hartman .start_tx = bcm_uart_start_tx,
642ab4382d2SGreg Kroah-Hartman .stop_tx = bcm_uart_stop_tx,
643ab4382d2SGreg Kroah-Hartman .stop_rx = bcm_uart_stop_rx,
644ab4382d2SGreg Kroah-Hartman .enable_ms = bcm_uart_enable_ms,
645ab4382d2SGreg Kroah-Hartman .break_ctl = bcm_uart_break_ctl,
646ab4382d2SGreg Kroah-Hartman .startup = bcm_uart_startup,
647ab4382d2SGreg Kroah-Hartman .shutdown = bcm_uart_shutdown,
648ab4382d2SGreg Kroah-Hartman .set_termios = bcm_uart_set_termios,
649ab4382d2SGreg Kroah-Hartman .type = bcm_uart_type,
650ab4382d2SGreg Kroah-Hartman .release_port = bcm_uart_release_port,
651ab4382d2SGreg Kroah-Hartman .request_port = bcm_uart_request_port,
652ab4382d2SGreg Kroah-Hartman .config_port = bcm_uart_config_port,
653ab4382d2SGreg Kroah-Hartman .verify_port = bcm_uart_verify_port,
654945de8beSArend van Spriel #ifdef CONFIG_CONSOLE_POLL
655945de8beSArend van Spriel .poll_get_char = bcm_uart_poll_get_char,
656945de8beSArend van Spriel .poll_put_char = bcm_uart_poll_put_char,
657945de8beSArend van Spriel #endif
658ab4382d2SGreg Kroah-Hartman };
659ab4382d2SGreg Kroah-Hartman
660ab4382d2SGreg Kroah-Hartman
661ab4382d2SGreg Kroah-Hartman
662ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_BCM63XX_CONSOLE
wait_for_xmitr(struct uart_port * port)6631a5b34ebSDenys Vlasenko static void wait_for_xmitr(struct uart_port *port)
664ab4382d2SGreg Kroah-Hartman {
665ab4382d2SGreg Kroah-Hartman unsigned int tmout;
666ab4382d2SGreg Kroah-Hartman
667ab4382d2SGreg Kroah-Hartman /* Wait up to 10ms for the character(s) to be sent. */
668ab4382d2SGreg Kroah-Hartman tmout = 10000;
669ab4382d2SGreg Kroah-Hartman while (--tmout) {
670ab4382d2SGreg Kroah-Hartman unsigned int val;
671ab4382d2SGreg Kroah-Hartman
672ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG);
673ab4382d2SGreg Kroah-Hartman if (val & UART_IR_STAT(UART_IR_TXEMPTY))
674ab4382d2SGreg Kroah-Hartman break;
675ab4382d2SGreg Kroah-Hartman udelay(1);
676ab4382d2SGreg Kroah-Hartman }
677ab4382d2SGreg Kroah-Hartman
678ab4382d2SGreg Kroah-Hartman /* Wait up to 1s for flow control if necessary */
679ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_CONS_FLOW) {
680ab4382d2SGreg Kroah-Hartman tmout = 1000000;
681ab4382d2SGreg Kroah-Hartman while (--tmout) {
682ab4382d2SGreg Kroah-Hartman unsigned int val;
683ab4382d2SGreg Kroah-Hartman
684ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_EXTINP_REG);
685ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_CTS_MASK)
686ab4382d2SGreg Kroah-Hartman break;
687ab4382d2SGreg Kroah-Hartman udelay(1);
688ab4382d2SGreg Kroah-Hartman }
689ab4382d2SGreg Kroah-Hartman }
690ab4382d2SGreg Kroah-Hartman }
691ab4382d2SGreg Kroah-Hartman
692ab4382d2SGreg Kroah-Hartman /*
693ab4382d2SGreg Kroah-Hartman * output given char
694ab4382d2SGreg Kroah-Hartman */
bcm_console_putchar(struct uart_port * port,unsigned char ch)6953f8bab17SJiri Slaby static void bcm_console_putchar(struct uart_port *port, unsigned char ch)
696ab4382d2SGreg Kroah-Hartman {
697ab4382d2SGreg Kroah-Hartman wait_for_xmitr(port);
698ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, ch, UART_FIFO_REG);
699ab4382d2SGreg Kroah-Hartman }
700ab4382d2SGreg Kroah-Hartman
701ab4382d2SGreg Kroah-Hartman /*
702ab4382d2SGreg Kroah-Hartman * console core request to output given string
703ab4382d2SGreg Kroah-Hartman */
bcm_console_write(struct console * co,const char * s,unsigned int count)704ab4382d2SGreg Kroah-Hartman static void bcm_console_write(struct console *co, const char *s,
705ab4382d2SGreg Kroah-Hartman unsigned int count)
706ab4382d2SGreg Kroah-Hartman {
707ab4382d2SGreg Kroah-Hartman struct uart_port *port;
708ab4382d2SGreg Kroah-Hartman unsigned long flags;
709ab4382d2SGreg Kroah-Hartman int locked;
710ab4382d2SGreg Kroah-Hartman
711ab4382d2SGreg Kroah-Hartman port = &ports[co->index];
712ab4382d2SGreg Kroah-Hartman
713ab4382d2SGreg Kroah-Hartman local_irq_save(flags);
714ab4382d2SGreg Kroah-Hartman if (port->sysrq) {
715ab4382d2SGreg Kroah-Hartman /* bcm_uart_interrupt() already took the lock */
716ab4382d2SGreg Kroah-Hartman locked = 0;
717ab4382d2SGreg Kroah-Hartman } else if (oops_in_progress) {
718ab4382d2SGreg Kroah-Hartman locked = spin_trylock(&port->lock);
719ab4382d2SGreg Kroah-Hartman } else {
720ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock);
721ab4382d2SGreg Kroah-Hartman locked = 1;
722ab4382d2SGreg Kroah-Hartman }
723ab4382d2SGreg Kroah-Hartman
724ab4382d2SGreg Kroah-Hartman /* call helper to deal with \r\n */
725ab4382d2SGreg Kroah-Hartman uart_console_write(port, s, count, bcm_console_putchar);
726ab4382d2SGreg Kroah-Hartman
727ab4382d2SGreg Kroah-Hartman /* and wait for char to be transmitted */
728ab4382d2SGreg Kroah-Hartman wait_for_xmitr(port);
729ab4382d2SGreg Kroah-Hartman
730ab4382d2SGreg Kroah-Hartman if (locked)
731ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock);
732ab4382d2SGreg Kroah-Hartman local_irq_restore(flags);
733ab4382d2SGreg Kroah-Hartman }
734ab4382d2SGreg Kroah-Hartman
735ab4382d2SGreg Kroah-Hartman /*
736ab4382d2SGreg Kroah-Hartman * console core request to setup given console, find matching uart
737ab4382d2SGreg Kroah-Hartman * port and setup it.
738ab4382d2SGreg Kroah-Hartman */
bcm_console_setup(struct console * co,char * options)739ab4382d2SGreg Kroah-Hartman static int bcm_console_setup(struct console *co, char *options)
740ab4382d2SGreg Kroah-Hartman {
741ab4382d2SGreg Kroah-Hartman struct uart_port *port;
742ab4382d2SGreg Kroah-Hartman int baud = 9600;
743ab4382d2SGreg Kroah-Hartman int bits = 8;
744ab4382d2SGreg Kroah-Hartman int parity = 'n';
745ab4382d2SGreg Kroah-Hartman int flow = 'n';
746ab4382d2SGreg Kroah-Hartman
747ab4382d2SGreg Kroah-Hartman if (co->index < 0 || co->index >= BCM63XX_NR_UARTS)
748ab4382d2SGreg Kroah-Hartman return -EINVAL;
749ab4382d2SGreg Kroah-Hartman port = &ports[co->index];
750ab4382d2SGreg Kroah-Hartman if (!port->membase)
751ab4382d2SGreg Kroah-Hartman return -ENODEV;
752ab4382d2SGreg Kroah-Hartman if (options)
753ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow);
754ab4382d2SGreg Kroah-Hartman
755ab4382d2SGreg Kroah-Hartman return uart_set_options(port, co, baud, parity, bits, flow);
756ab4382d2SGreg Kroah-Hartman }
757ab4382d2SGreg Kroah-Hartman
758ab4382d2SGreg Kroah-Hartman static struct uart_driver bcm_uart_driver;
759ab4382d2SGreg Kroah-Hartman
760ab4382d2SGreg Kroah-Hartman static struct console bcm63xx_console = {
761ab4382d2SGreg Kroah-Hartman .name = "ttyS",
762ab4382d2SGreg Kroah-Hartman .write = bcm_console_write,
763ab4382d2SGreg Kroah-Hartman .device = uart_console_device,
764ab4382d2SGreg Kroah-Hartman .setup = bcm_console_setup,
765ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER,
766ab4382d2SGreg Kroah-Hartman .index = -1,
767ab4382d2SGreg Kroah-Hartman .data = &bcm_uart_driver,
768ab4382d2SGreg Kroah-Hartman };
769ab4382d2SGreg Kroah-Hartman
bcm63xx_console_init(void)770ab4382d2SGreg Kroah-Hartman static int __init bcm63xx_console_init(void)
771ab4382d2SGreg Kroah-Hartman {
772ab4382d2SGreg Kroah-Hartman register_console(&bcm63xx_console);
773ab4382d2SGreg Kroah-Hartman return 0;
774ab4382d2SGreg Kroah-Hartman }
775ab4382d2SGreg Kroah-Hartman
776ab4382d2SGreg Kroah-Hartman console_initcall(bcm63xx_console_init);
777ab4382d2SGreg Kroah-Hartman
bcm_early_write(struct console * con,const char * s,unsigned n)7781ab8e4b1SKevin Cernekee static void bcm_early_write(struct console *con, const char *s, unsigned n)
7791ab8e4b1SKevin Cernekee {
7801ab8e4b1SKevin Cernekee struct earlycon_device *dev = con->data;
7811ab8e4b1SKevin Cernekee
7821ab8e4b1SKevin Cernekee uart_console_write(&dev->port, s, n, bcm_console_putchar);
7831ab8e4b1SKevin Cernekee wait_for_xmitr(&dev->port);
7841ab8e4b1SKevin Cernekee }
7851ab8e4b1SKevin Cernekee
bcm_early_console_setup(struct earlycon_device * device,const char * opt)7861ab8e4b1SKevin Cernekee static int __init bcm_early_console_setup(struct earlycon_device *device,
7871ab8e4b1SKevin Cernekee const char *opt)
7881ab8e4b1SKevin Cernekee {
7891ab8e4b1SKevin Cernekee if (!device->port.membase)
7901ab8e4b1SKevin Cernekee return -ENODEV;
7911ab8e4b1SKevin Cernekee
7921ab8e4b1SKevin Cernekee device->con->write = bcm_early_write;
7931ab8e4b1SKevin Cernekee return 0;
7941ab8e4b1SKevin Cernekee }
7951ab8e4b1SKevin Cernekee
7961ab8e4b1SKevin Cernekee OF_EARLYCON_DECLARE(bcm63xx_uart, "brcm,bcm6345-uart", bcm_early_console_setup);
7971ab8e4b1SKevin Cernekee
798ab4382d2SGreg Kroah-Hartman #define BCM63XX_CONSOLE (&bcm63xx_console)
799ab4382d2SGreg Kroah-Hartman #else
800ab4382d2SGreg Kroah-Hartman #define BCM63XX_CONSOLE NULL
801ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_BCM63XX_CONSOLE */
802ab4382d2SGreg Kroah-Hartman
803ab4382d2SGreg Kroah-Hartman static struct uart_driver bcm_uart_driver = {
804ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE,
805ab4382d2SGreg Kroah-Hartman .driver_name = "bcm63xx_uart",
806ab4382d2SGreg Kroah-Hartman .dev_name = "ttyS",
807ab4382d2SGreg Kroah-Hartman .major = TTY_MAJOR,
808ab4382d2SGreg Kroah-Hartman .minor = 64,
809ab4382d2SGreg Kroah-Hartman .nr = BCM63XX_NR_UARTS,
810ab4382d2SGreg Kroah-Hartman .cons = BCM63XX_CONSOLE,
811ab4382d2SGreg Kroah-Hartman };
812ab4382d2SGreg Kroah-Hartman
813ab4382d2SGreg Kroah-Hartman /*
814ab4382d2SGreg Kroah-Hartman * platform driver probe/remove callback
815ab4382d2SGreg Kroah-Hartman */
bcm_uart_probe(struct platform_device * pdev)8169671f099SBill Pemberton static int bcm_uart_probe(struct platform_device *pdev)
817ab4382d2SGreg Kroah-Hartman {
818fc67c913SLad Prabhakar struct resource *res_mem;
819ab4382d2SGreg Kroah-Hartman struct uart_port *port;
820ab4382d2SGreg Kroah-Hartman struct clk *clk;
821ab4382d2SGreg Kroah-Hartman int ret;
822ab4382d2SGreg Kroah-Hartman
823a5938866SJonas Gorski if (pdev->dev.of_node) {
824a5938866SJonas Gorski pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
825a5938866SJonas Gorski
826a5938866SJonas Gorski if (pdev->id < 0)
8279277285fSFlorian Fainelli pdev->id = of_alias_get_id(pdev->dev.of_node, "uart");
828a5938866SJonas Gorski }
8299277285fSFlorian Fainelli
830ab4382d2SGreg Kroah-Hartman if (pdev->id < 0 || pdev->id >= BCM63XX_NR_UARTS)
831ab4382d2SGreg Kroah-Hartman return -EINVAL;
832ab4382d2SGreg Kroah-Hartman
833e979f3b7SKevin Cernekee port = &ports[pdev->id];
834e979f3b7SKevin Cernekee if (port->membase)
835ab4382d2SGreg Kroah-Hartman return -EBUSY;
836e979f3b7SKevin Cernekee memset(port, 0, sizeof(*port));
837ab4382d2SGreg Kroah-Hartman
838b03a4ecbSYangtao Li port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res_mem);
839e979f3b7SKevin Cernekee if (IS_ERR(port->membase))
840e979f3b7SKevin Cernekee return PTR_ERR(port->membase);
841b03a4ecbSYangtao Li port->mapbase = res_mem->start;
842e979f3b7SKevin Cernekee
843fc67c913SLad Prabhakar ret = platform_get_irq(pdev, 0);
844fc67c913SLad Prabhakar if (ret < 0)
845fc67c913SLad Prabhakar return ret;
846fc67c913SLad Prabhakar port->irq = ret;
847ab4382d2SGreg Kroah-Hartman
848fa1e6a8aSJonas Gorski clk = clk_get(&pdev->dev, "refclk");
849fa1e6a8aSJonas Gorski if (IS_ERR(clk) && pdev->dev.of_node)
850fa1e6a8aSJonas Gorski clk = of_clk_get(pdev->dev.of_node, 0);
851fa1e6a8aSJonas Gorski
852092a9f59SFlorian Fainelli if (IS_ERR(clk))
853ab4382d2SGreg Kroah-Hartman return -ENODEV;
854ab4382d2SGreg Kroah-Hartman
855ab4382d2SGreg Kroah-Hartman port->iotype = UPIO_MEM;
856ab4382d2SGreg Kroah-Hartman port->ops = &bcm_uart_ops;
857ab4382d2SGreg Kroah-Hartman port->flags = UPF_BOOT_AUTOCONF;
858ab4382d2SGreg Kroah-Hartman port->dev = &pdev->dev;
859ab4382d2SGreg Kroah-Hartman port->fifosize = 16;
860ab4382d2SGreg Kroah-Hartman port->uartclk = clk_get_rate(clk) / 2;
861ab4382d2SGreg Kroah-Hartman port->line = pdev->id;
86224036fb7SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_BCM63XX_CONSOLE);
863ab4382d2SGreg Kroah-Hartman clk_put(clk);
864ab4382d2SGreg Kroah-Hartman
865ab4382d2SGreg Kroah-Hartman ret = uart_add_one_port(&bcm_uart_driver, port);
866ab4382d2SGreg Kroah-Hartman if (ret) {
86716420ad0SLad, Prabhakar ports[pdev->id].membase = NULL;
868ab4382d2SGreg Kroah-Hartman return ret;
869ab4382d2SGreg Kroah-Hartman }
870ab4382d2SGreg Kroah-Hartman platform_set_drvdata(pdev, port);
871ab4382d2SGreg Kroah-Hartman return 0;
872ab4382d2SGreg Kroah-Hartman }
873ab4382d2SGreg Kroah-Hartman
bcm_uart_remove(struct platform_device * pdev)874ae8d8a14SBill Pemberton static int bcm_uart_remove(struct platform_device *pdev)
875ab4382d2SGreg Kroah-Hartman {
876ab4382d2SGreg Kroah-Hartman struct uart_port *port;
877ab4382d2SGreg Kroah-Hartman
878ab4382d2SGreg Kroah-Hartman port = platform_get_drvdata(pdev);
879ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&bcm_uart_driver, port);
880ab4382d2SGreg Kroah-Hartman /* mark port as free */
88116420ad0SLad, Prabhakar ports[pdev->id].membase = NULL;
882ab4382d2SGreg Kroah-Hartman return 0;
883ab4382d2SGreg Kroah-Hartman }
884ab4382d2SGreg Kroah-Hartman
8859277285fSFlorian Fainelli static const struct of_device_id bcm63xx_of_match[] = {
8869277285fSFlorian Fainelli { .compatible = "brcm,bcm6345-uart" },
8879277285fSFlorian Fainelli { /* sentinel */ }
8889277285fSFlorian Fainelli };
8899277285fSFlorian Fainelli MODULE_DEVICE_TABLE(of, bcm63xx_of_match);
8909277285fSFlorian Fainelli
891ab4382d2SGreg Kroah-Hartman /*
892ab4382d2SGreg Kroah-Hartman * platform driver stuff
893ab4382d2SGreg Kroah-Hartman */
894ab4382d2SGreg Kroah-Hartman static struct platform_driver bcm_uart_platform_driver = {
895ab4382d2SGreg Kroah-Hartman .probe = bcm_uart_probe,
8962d47b716SBill Pemberton .remove = bcm_uart_remove,
897ab4382d2SGreg Kroah-Hartman .driver = {
898ab4382d2SGreg Kroah-Hartman .name = "bcm63xx_uart",
8999277285fSFlorian Fainelli .of_match_table = bcm63xx_of_match,
900ab4382d2SGreg Kroah-Hartman },
901ab4382d2SGreg Kroah-Hartman };
902ab4382d2SGreg Kroah-Hartman
bcm_uart_init(void)903ab4382d2SGreg Kroah-Hartman static int __init bcm_uart_init(void)
904ab4382d2SGreg Kroah-Hartman {
905ab4382d2SGreg Kroah-Hartman int ret;
906ab4382d2SGreg Kroah-Hartman
907ab4382d2SGreg Kroah-Hartman ret = uart_register_driver(&bcm_uart_driver);
908ab4382d2SGreg Kroah-Hartman if (ret)
909ab4382d2SGreg Kroah-Hartman return ret;
910ab4382d2SGreg Kroah-Hartman
911ab4382d2SGreg Kroah-Hartman ret = platform_driver_register(&bcm_uart_platform_driver);
912ab4382d2SGreg Kroah-Hartman if (ret)
913ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&bcm_uart_driver);
914ab4382d2SGreg Kroah-Hartman
915ab4382d2SGreg Kroah-Hartman return ret;
916ab4382d2SGreg Kroah-Hartman }
917ab4382d2SGreg Kroah-Hartman
bcm_uart_exit(void)918ab4382d2SGreg Kroah-Hartman static void __exit bcm_uart_exit(void)
919ab4382d2SGreg Kroah-Hartman {
920ab4382d2SGreg Kroah-Hartman platform_driver_unregister(&bcm_uart_platform_driver);
921ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&bcm_uart_driver);
922ab4382d2SGreg Kroah-Hartman }
923ab4382d2SGreg Kroah-Hartman
924ab4382d2SGreg Kroah-Hartman module_init(bcm_uart_init);
925ab4382d2SGreg Kroah-Hartman module_exit(bcm_uart_exit);
926ab4382d2SGreg Kroah-Hartman
927ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
9284137cd9bSKevin Cernekee MODULE_DESCRIPTION("Broadcom 63xx integrated uart driver");
929ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
930