xref: /openbmc/linux/drivers/tty/serial/bcm63xx_uart.c (revision 2d47b7160243b1422006b91debf438484a4fde58)
1ab4382d2SGreg Kroah-Hartman /*
2ab4382d2SGreg Kroah-Hartman  * This file is subject to the terms and conditions of the GNU General Public
3ab4382d2SGreg Kroah-Hartman  * License.  See the file "COPYING" in the main directory of this archive
4ab4382d2SGreg Kroah-Hartman  * for more details.
5ab4382d2SGreg Kroah-Hartman  *
6ab4382d2SGreg Kroah-Hartman  * Derived from many drivers using generic_serial interface.
7ab4382d2SGreg Kroah-Hartman  *
8ab4382d2SGreg Kroah-Hartman  * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
9ab4382d2SGreg Kroah-Hartman  *
10ab4382d2SGreg Kroah-Hartman  *  Serial driver for BCM63xx integrated UART.
11ab4382d2SGreg Kroah-Hartman  *
12ab4382d2SGreg Kroah-Hartman  * Hardware flow control was _not_ tested since I only have RX/TX on
13ab4382d2SGreg Kroah-Hartman  * my board.
14ab4382d2SGreg Kroah-Hartman  */
15ab4382d2SGreg Kroah-Hartman 
16ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_BCM63XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
17ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
18ab4382d2SGreg Kroah-Hartman #endif
19ab4382d2SGreg Kroah-Hartman 
20ab4382d2SGreg Kroah-Hartman #include <linux/kernel.h>
21ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
22ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
23ab4382d2SGreg Kroah-Hartman #include <linux/delay.h>
24ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
25ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
29ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
32ab4382d2SGreg Kroah-Hartman 
33ab4382d2SGreg Kroah-Hartman #include <bcm63xx_clk.h>
34ab4382d2SGreg Kroah-Hartman #include <bcm63xx_irq.h>
35ab4382d2SGreg Kroah-Hartman #include <bcm63xx_regs.h>
36ab4382d2SGreg Kroah-Hartman #include <bcm63xx_io.h>
37ab4382d2SGreg Kroah-Hartman 
38ab4382d2SGreg Kroah-Hartman #define BCM63XX_NR_UARTS	2
39ab4382d2SGreg Kroah-Hartman 
40ab4382d2SGreg Kroah-Hartman static struct uart_port ports[BCM63XX_NR_UARTS];
41ab4382d2SGreg Kroah-Hartman 
42ab4382d2SGreg Kroah-Hartman /*
43ab4382d2SGreg Kroah-Hartman  * rx interrupt mask / stat
44ab4382d2SGreg Kroah-Hartman  *
45ab4382d2SGreg Kroah-Hartman  * mask:
46ab4382d2SGreg Kroah-Hartman  *  - rx fifo full
47ab4382d2SGreg Kroah-Hartman  *  - rx fifo above threshold
48ab4382d2SGreg Kroah-Hartman  *  - rx fifo not empty for too long
49ab4382d2SGreg Kroah-Hartman  */
50ab4382d2SGreg Kroah-Hartman #define UART_RX_INT_MASK	(UART_IR_MASK(UART_IR_RXOVER) |		\
51ab4382d2SGreg Kroah-Hartman 				UART_IR_MASK(UART_IR_RXTHRESH) |	\
52ab4382d2SGreg Kroah-Hartman 				UART_IR_MASK(UART_IR_RXTIMEOUT))
53ab4382d2SGreg Kroah-Hartman 
54ab4382d2SGreg Kroah-Hartman #define UART_RX_INT_STAT	(UART_IR_STAT(UART_IR_RXOVER) |		\
55ab4382d2SGreg Kroah-Hartman 				UART_IR_STAT(UART_IR_RXTHRESH) |	\
56ab4382d2SGreg Kroah-Hartman 				UART_IR_STAT(UART_IR_RXTIMEOUT))
57ab4382d2SGreg Kroah-Hartman 
58ab4382d2SGreg Kroah-Hartman /*
59ab4382d2SGreg Kroah-Hartman  * tx interrupt mask / stat
60ab4382d2SGreg Kroah-Hartman  *
61ab4382d2SGreg Kroah-Hartman  * mask:
62ab4382d2SGreg Kroah-Hartman  * - tx fifo empty
63ab4382d2SGreg Kroah-Hartman  * - tx fifo below threshold
64ab4382d2SGreg Kroah-Hartman  */
65ab4382d2SGreg Kroah-Hartman #define UART_TX_INT_MASK	(UART_IR_MASK(UART_IR_TXEMPTY) |	\
66ab4382d2SGreg Kroah-Hartman 				UART_IR_MASK(UART_IR_TXTRESH))
67ab4382d2SGreg Kroah-Hartman 
68ab4382d2SGreg Kroah-Hartman #define UART_TX_INT_STAT	(UART_IR_STAT(UART_IR_TXEMPTY) |	\
69ab4382d2SGreg Kroah-Hartman 				UART_IR_STAT(UART_IR_TXTRESH))
70ab4382d2SGreg Kroah-Hartman 
71ab4382d2SGreg Kroah-Hartman /*
72ab4382d2SGreg Kroah-Hartman  * external input interrupt
73ab4382d2SGreg Kroah-Hartman  *
74ab4382d2SGreg Kroah-Hartman  * mask: any edge on CTS, DCD
75ab4382d2SGreg Kroah-Hartman  */
76ab4382d2SGreg Kroah-Hartman #define UART_EXTINP_INT_MASK	(UART_EXTINP_IRMASK(UART_EXTINP_IR_CTS) | \
77ab4382d2SGreg Kroah-Hartman 				 UART_EXTINP_IRMASK(UART_EXTINP_IR_DCD))
78ab4382d2SGreg Kroah-Hartman 
79ab4382d2SGreg Kroah-Hartman /*
80ab4382d2SGreg Kroah-Hartman  * handy uart register accessor
81ab4382d2SGreg Kroah-Hartman  */
82ab4382d2SGreg Kroah-Hartman static inline unsigned int bcm_uart_readl(struct uart_port *port,
83ab4382d2SGreg Kroah-Hartman 					 unsigned int offset)
84ab4382d2SGreg Kroah-Hartman {
85ab4382d2SGreg Kroah-Hartman 	return bcm_readl(port->membase + offset);
86ab4382d2SGreg Kroah-Hartman }
87ab4382d2SGreg Kroah-Hartman 
88ab4382d2SGreg Kroah-Hartman static inline void bcm_uart_writel(struct uart_port *port,
89ab4382d2SGreg Kroah-Hartman 				  unsigned int value, unsigned int offset)
90ab4382d2SGreg Kroah-Hartman {
91ab4382d2SGreg Kroah-Hartman 	bcm_writel(value, port->membase + offset);
92ab4382d2SGreg Kroah-Hartman }
93ab4382d2SGreg Kroah-Hartman 
94ab4382d2SGreg Kroah-Hartman /*
95ab4382d2SGreg Kroah-Hartman  * serial core request to check if uart tx fifo is empty
96ab4382d2SGreg Kroah-Hartman  */
97ab4382d2SGreg Kroah-Hartman static unsigned int bcm_uart_tx_empty(struct uart_port *port)
98ab4382d2SGreg Kroah-Hartman {
99ab4382d2SGreg Kroah-Hartman 	unsigned int val;
100ab4382d2SGreg Kroah-Hartman 
101ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_IR_REG);
102ab4382d2SGreg Kroah-Hartman 	return (val & UART_IR_STAT(UART_IR_TXEMPTY)) ? 1 : 0;
103ab4382d2SGreg Kroah-Hartman }
104ab4382d2SGreg Kroah-Hartman 
105ab4382d2SGreg Kroah-Hartman /*
106ab4382d2SGreg Kroah-Hartman  * serial core request to set RTS and DTR pin state and loopback mode
107ab4382d2SGreg Kroah-Hartman  */
108ab4382d2SGreg Kroah-Hartman static void bcm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
109ab4382d2SGreg Kroah-Hartman {
110ab4382d2SGreg Kroah-Hartman 	unsigned int val;
111ab4382d2SGreg Kroah-Hartman 
112ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_MCTL_REG);
113ab4382d2SGreg Kroah-Hartman 	val &= ~(UART_MCTL_DTR_MASK | UART_MCTL_RTS_MASK);
114ab4382d2SGreg Kroah-Hartman 	/* invert of written value is reflected on the pin */
115ab4382d2SGreg Kroah-Hartman 	if (!(mctrl & TIOCM_DTR))
116ab4382d2SGreg Kroah-Hartman 		val |= UART_MCTL_DTR_MASK;
117ab4382d2SGreg Kroah-Hartman 	if (!(mctrl & TIOCM_RTS))
118ab4382d2SGreg Kroah-Hartman 		val |= UART_MCTL_RTS_MASK;
119ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_MCTL_REG);
120ab4382d2SGreg Kroah-Hartman 
121ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_CTL_REG);
122ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
123ab4382d2SGreg Kroah-Hartman 		val |= UART_CTL_LOOPBACK_MASK;
124ab4382d2SGreg Kroah-Hartman 	else
125ab4382d2SGreg Kroah-Hartman 		val &= ~UART_CTL_LOOPBACK_MASK;
126ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_CTL_REG);
127ab4382d2SGreg Kroah-Hartman }
128ab4382d2SGreg Kroah-Hartman 
129ab4382d2SGreg Kroah-Hartman /*
130ab4382d2SGreg Kroah-Hartman  * serial core request to return RI, CTS, DCD and DSR pin state
131ab4382d2SGreg Kroah-Hartman  */
132ab4382d2SGreg Kroah-Hartman static unsigned int bcm_uart_get_mctrl(struct uart_port *port)
133ab4382d2SGreg Kroah-Hartman {
134ab4382d2SGreg Kroah-Hartman 	unsigned int val, mctrl;
135ab4382d2SGreg Kroah-Hartman 
136ab4382d2SGreg Kroah-Hartman 	mctrl = 0;
137ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_EXTINP_REG);
138ab4382d2SGreg Kroah-Hartman 	if (val & UART_EXTINP_RI_MASK)
139ab4382d2SGreg Kroah-Hartman 		mctrl |= TIOCM_RI;
140ab4382d2SGreg Kroah-Hartman 	if (val & UART_EXTINP_CTS_MASK)
141ab4382d2SGreg Kroah-Hartman 		mctrl |= TIOCM_CTS;
142ab4382d2SGreg Kroah-Hartman 	if (val & UART_EXTINP_DCD_MASK)
143ab4382d2SGreg Kroah-Hartman 		mctrl |= TIOCM_CD;
144ab4382d2SGreg Kroah-Hartman 	if (val & UART_EXTINP_DSR_MASK)
145ab4382d2SGreg Kroah-Hartman 		mctrl |= TIOCM_DSR;
146ab4382d2SGreg Kroah-Hartman 	return mctrl;
147ab4382d2SGreg Kroah-Hartman }
148ab4382d2SGreg Kroah-Hartman 
149ab4382d2SGreg Kroah-Hartman /*
150ab4382d2SGreg Kroah-Hartman  * serial core request to disable tx ASAP (used for flow control)
151ab4382d2SGreg Kroah-Hartman  */
152ab4382d2SGreg Kroah-Hartman static void bcm_uart_stop_tx(struct uart_port *port)
153ab4382d2SGreg Kroah-Hartman {
154ab4382d2SGreg Kroah-Hartman 	unsigned int val;
155ab4382d2SGreg Kroah-Hartman 
156ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_CTL_REG);
157ab4382d2SGreg Kroah-Hartman 	val &= ~(UART_CTL_TXEN_MASK);
158ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_CTL_REG);
159ab4382d2SGreg Kroah-Hartman 
160ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_IR_REG);
161ab4382d2SGreg Kroah-Hartman 	val &= ~UART_TX_INT_MASK;
162ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_IR_REG);
163ab4382d2SGreg Kroah-Hartman }
164ab4382d2SGreg Kroah-Hartman 
165ab4382d2SGreg Kroah-Hartman /*
166ab4382d2SGreg Kroah-Hartman  * serial core request to (re)enable tx
167ab4382d2SGreg Kroah-Hartman  */
168ab4382d2SGreg Kroah-Hartman static void bcm_uart_start_tx(struct uart_port *port)
169ab4382d2SGreg Kroah-Hartman {
170ab4382d2SGreg Kroah-Hartman 	unsigned int val;
171ab4382d2SGreg Kroah-Hartman 
172ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_IR_REG);
173ab4382d2SGreg Kroah-Hartman 	val |= UART_TX_INT_MASK;
174ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_IR_REG);
175ab4382d2SGreg Kroah-Hartman 
176ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_CTL_REG);
177ab4382d2SGreg Kroah-Hartman 	val |= UART_CTL_TXEN_MASK;
178ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_CTL_REG);
179ab4382d2SGreg Kroah-Hartman }
180ab4382d2SGreg Kroah-Hartman 
181ab4382d2SGreg Kroah-Hartman /*
182ab4382d2SGreg Kroah-Hartman  * serial core request to stop rx, called before port shutdown
183ab4382d2SGreg Kroah-Hartman  */
184ab4382d2SGreg Kroah-Hartman static void bcm_uart_stop_rx(struct uart_port *port)
185ab4382d2SGreg Kroah-Hartman {
186ab4382d2SGreg Kroah-Hartman 	unsigned int val;
187ab4382d2SGreg Kroah-Hartman 
188ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_IR_REG);
189ab4382d2SGreg Kroah-Hartman 	val &= ~UART_RX_INT_MASK;
190ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_IR_REG);
191ab4382d2SGreg Kroah-Hartman }
192ab4382d2SGreg Kroah-Hartman 
193ab4382d2SGreg Kroah-Hartman /*
194ab4382d2SGreg Kroah-Hartman  * serial core request to enable modem status interrupt reporting
195ab4382d2SGreg Kroah-Hartman  */
196ab4382d2SGreg Kroah-Hartman static void bcm_uart_enable_ms(struct uart_port *port)
197ab4382d2SGreg Kroah-Hartman {
198ab4382d2SGreg Kroah-Hartman 	unsigned int val;
199ab4382d2SGreg Kroah-Hartman 
200ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_IR_REG);
201ab4382d2SGreg Kroah-Hartman 	val |= UART_IR_MASK(UART_IR_EXTIP);
202ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_IR_REG);
203ab4382d2SGreg Kroah-Hartman }
204ab4382d2SGreg Kroah-Hartman 
205ab4382d2SGreg Kroah-Hartman /*
206ab4382d2SGreg Kroah-Hartman  * serial core request to start/stop emitting break char
207ab4382d2SGreg Kroah-Hartman  */
208ab4382d2SGreg Kroah-Hartman static void bcm_uart_break_ctl(struct uart_port *port, int ctl)
209ab4382d2SGreg Kroah-Hartman {
210ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
211ab4382d2SGreg Kroah-Hartman 	unsigned int val;
212ab4382d2SGreg Kroah-Hartman 
213ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
214ab4382d2SGreg Kroah-Hartman 
215ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_CTL_REG);
216ab4382d2SGreg Kroah-Hartman 	if (ctl)
217ab4382d2SGreg Kroah-Hartman 		val |= UART_CTL_XMITBRK_MASK;
218ab4382d2SGreg Kroah-Hartman 	else
219ab4382d2SGreg Kroah-Hartman 		val &= ~UART_CTL_XMITBRK_MASK;
220ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_CTL_REG);
221ab4382d2SGreg Kroah-Hartman 
222ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
223ab4382d2SGreg Kroah-Hartman }
224ab4382d2SGreg Kroah-Hartman 
225ab4382d2SGreg Kroah-Hartman /*
226ab4382d2SGreg Kroah-Hartman  * return port type in string format
227ab4382d2SGreg Kroah-Hartman  */
228ab4382d2SGreg Kroah-Hartman static const char *bcm_uart_type(struct uart_port *port)
229ab4382d2SGreg Kroah-Hartman {
230ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_BCM63XX) ? "bcm63xx_uart" : NULL;
231ab4382d2SGreg Kroah-Hartman }
232ab4382d2SGreg Kroah-Hartman 
233ab4382d2SGreg Kroah-Hartman /*
234ab4382d2SGreg Kroah-Hartman  * read all chars in rx fifo and send them to core
235ab4382d2SGreg Kroah-Hartman  */
236ab4382d2SGreg Kroah-Hartman static void bcm_uart_do_rx(struct uart_port *port)
237ab4382d2SGreg Kroah-Hartman {
238ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty;
239ab4382d2SGreg Kroah-Hartman 	unsigned int max_count;
240ab4382d2SGreg Kroah-Hartman 
241ab4382d2SGreg Kroah-Hartman 	/* limit number of char read in interrupt, should not be
242ab4382d2SGreg Kroah-Hartman 	 * higher than fifo size anyway since we're much faster than
243ab4382d2SGreg Kroah-Hartman 	 * serial port */
244ab4382d2SGreg Kroah-Hartman 	max_count = 32;
245ab4382d2SGreg Kroah-Hartman 	tty = port->state->port.tty;
246ab4382d2SGreg Kroah-Hartman 	do {
247ab4382d2SGreg Kroah-Hartman 		unsigned int iestat, c, cstat;
248ab4382d2SGreg Kroah-Hartman 		char flag;
249ab4382d2SGreg Kroah-Hartman 
250ab4382d2SGreg Kroah-Hartman 		/* get overrun/fifo empty information from ier
251ab4382d2SGreg Kroah-Hartman 		 * register */
252ab4382d2SGreg Kroah-Hartman 		iestat = bcm_uart_readl(port, UART_IR_REG);
2533bc46b31SMaxime Bizon 
2543bc46b31SMaxime Bizon 		if (unlikely(iestat & UART_IR_STAT(UART_IR_RXOVER))) {
2553bc46b31SMaxime Bizon 			unsigned int val;
2563bc46b31SMaxime Bizon 
2573bc46b31SMaxime Bizon 			/* fifo reset is required to clear
2583bc46b31SMaxime Bizon 			 * interrupt */
2593bc46b31SMaxime Bizon 			val = bcm_uart_readl(port, UART_CTL_REG);
2603bc46b31SMaxime Bizon 			val |= UART_CTL_RSTRXFIFO_MASK;
2613bc46b31SMaxime Bizon 			bcm_uart_writel(port, val, UART_CTL_REG);
2623bc46b31SMaxime Bizon 
2633bc46b31SMaxime Bizon 			port->icount.overrun++;
2643bc46b31SMaxime Bizon 			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
2653bc46b31SMaxime Bizon 		}
2663bc46b31SMaxime Bizon 
267ab4382d2SGreg Kroah-Hartman 		if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
268ab4382d2SGreg Kroah-Hartman 			break;
269ab4382d2SGreg Kroah-Hartman 
270ab4382d2SGreg Kroah-Hartman 		cstat = c = bcm_uart_readl(port, UART_FIFO_REG);
271ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
272ab4382d2SGreg Kroah-Hartman 		flag = TTY_NORMAL;
273ab4382d2SGreg Kroah-Hartman 		c &= 0xff;
274ab4382d2SGreg Kroah-Hartman 
275ab4382d2SGreg Kroah-Hartman 		if (unlikely((cstat & UART_FIFO_ANYERR_MASK))) {
276ab4382d2SGreg Kroah-Hartman 			/* do stats first */
277ab4382d2SGreg Kroah-Hartman 			if (cstat & UART_FIFO_BRKDET_MASK) {
278ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
279ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
280ab4382d2SGreg Kroah-Hartman 					continue;
281ab4382d2SGreg Kroah-Hartman 			}
282ab4382d2SGreg Kroah-Hartman 
283ab4382d2SGreg Kroah-Hartman 			if (cstat & UART_FIFO_PARERR_MASK)
284ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
285ab4382d2SGreg Kroah-Hartman 			if (cstat & UART_FIFO_FRAMEERR_MASK)
286ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
287ab4382d2SGreg Kroah-Hartman 
288ab4382d2SGreg Kroah-Hartman 			/* update flag wrt read_status_mask */
289ab4382d2SGreg Kroah-Hartman 			cstat &= port->read_status_mask;
290ab4382d2SGreg Kroah-Hartman 			if (cstat & UART_FIFO_BRKDET_MASK)
291ab4382d2SGreg Kroah-Hartman 				flag = TTY_BREAK;
292ab4382d2SGreg Kroah-Hartman 			if (cstat & UART_FIFO_FRAMEERR_MASK)
293ab4382d2SGreg Kroah-Hartman 				flag = TTY_FRAME;
294ab4382d2SGreg Kroah-Hartman 			if (cstat & UART_FIFO_PARERR_MASK)
295ab4382d2SGreg Kroah-Hartman 				flag = TTY_PARITY;
296ab4382d2SGreg Kroah-Hartman 		}
297ab4382d2SGreg Kroah-Hartman 
298ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c))
299ab4382d2SGreg Kroah-Hartman 			continue;
300ab4382d2SGreg Kroah-Hartman 
301ab4382d2SGreg Kroah-Hartman 
302ab4382d2SGreg Kroah-Hartman 		if ((cstat & port->ignore_status_mask) == 0)
303ab4382d2SGreg Kroah-Hartman 			tty_insert_flip_char(tty, c, flag);
304ab4382d2SGreg Kroah-Hartman 
305ab4382d2SGreg Kroah-Hartman 	} while (--max_count);
306ab4382d2SGreg Kroah-Hartman 
307ab4382d2SGreg Kroah-Hartman 	tty_flip_buffer_push(tty);
308ab4382d2SGreg Kroah-Hartman }
309ab4382d2SGreg Kroah-Hartman 
310ab4382d2SGreg Kroah-Hartman /*
311ab4382d2SGreg Kroah-Hartman  * fill tx fifo with chars to send, stop when fifo is about to be full
312ab4382d2SGreg Kroah-Hartman  * or when all chars have been sent.
313ab4382d2SGreg Kroah-Hartman  */
314ab4382d2SGreg Kroah-Hartman static void bcm_uart_do_tx(struct uart_port *port)
315ab4382d2SGreg Kroah-Hartman {
316ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit;
317ab4382d2SGreg Kroah-Hartman 	unsigned int val, max_count;
318ab4382d2SGreg Kroah-Hartman 
319ab4382d2SGreg Kroah-Hartman 	if (port->x_char) {
320ab4382d2SGreg Kroah-Hartman 		bcm_uart_writel(port, port->x_char, UART_FIFO_REG);
321ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
322ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
323ab4382d2SGreg Kroah-Hartman 		return;
324ab4382d2SGreg Kroah-Hartman 	}
325ab4382d2SGreg Kroah-Hartman 
326ab4382d2SGreg Kroah-Hartman 	if (uart_tx_stopped(port)) {
327ab4382d2SGreg Kroah-Hartman 		bcm_uart_stop_tx(port);
328ab4382d2SGreg Kroah-Hartman 		return;
329ab4382d2SGreg Kroah-Hartman 	}
330ab4382d2SGreg Kroah-Hartman 
331ab4382d2SGreg Kroah-Hartman 	xmit = &port->state->xmit;
332ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit))
333ab4382d2SGreg Kroah-Hartman 		goto txq_empty;
334ab4382d2SGreg Kroah-Hartman 
335ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_MCTL_REG);
336ab4382d2SGreg Kroah-Hartman 	val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT;
337ab4382d2SGreg Kroah-Hartman 	max_count = port->fifosize - val;
338ab4382d2SGreg Kroah-Hartman 
339ab4382d2SGreg Kroah-Hartman 	while (max_count--) {
340ab4382d2SGreg Kroah-Hartman 		unsigned int c;
341ab4382d2SGreg Kroah-Hartman 
342ab4382d2SGreg Kroah-Hartman 		c = xmit->buf[xmit->tail];
343ab4382d2SGreg Kroah-Hartman 		bcm_uart_writel(port, c, UART_FIFO_REG);
344ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
345ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
346ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
347ab4382d2SGreg Kroah-Hartman 			break;
348ab4382d2SGreg Kroah-Hartman 	}
349ab4382d2SGreg Kroah-Hartman 
350ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
351ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
352ab4382d2SGreg Kroah-Hartman 
353ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit))
354ab4382d2SGreg Kroah-Hartman 		goto txq_empty;
355ab4382d2SGreg Kroah-Hartman 	return;
356ab4382d2SGreg Kroah-Hartman 
357ab4382d2SGreg Kroah-Hartman txq_empty:
358ab4382d2SGreg Kroah-Hartman 	/* nothing to send, disable transmit interrupt */
359ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_IR_REG);
360ab4382d2SGreg Kroah-Hartman 	val &= ~UART_TX_INT_MASK;
361ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_IR_REG);
362ab4382d2SGreg Kroah-Hartman 	return;
363ab4382d2SGreg Kroah-Hartman }
364ab4382d2SGreg Kroah-Hartman 
365ab4382d2SGreg Kroah-Hartman /*
366ab4382d2SGreg Kroah-Hartman  * process uart interrupt
367ab4382d2SGreg Kroah-Hartman  */
368ab4382d2SGreg Kroah-Hartman static irqreturn_t bcm_uart_interrupt(int irq, void *dev_id)
369ab4382d2SGreg Kroah-Hartman {
370ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
371ab4382d2SGreg Kroah-Hartman 	unsigned int irqstat;
372ab4382d2SGreg Kroah-Hartman 
373ab4382d2SGreg Kroah-Hartman 	port = dev_id;
374ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
375ab4382d2SGreg Kroah-Hartman 
376ab4382d2SGreg Kroah-Hartman 	irqstat = bcm_uart_readl(port, UART_IR_REG);
377ab4382d2SGreg Kroah-Hartman 	if (irqstat & UART_RX_INT_STAT)
378ab4382d2SGreg Kroah-Hartman 		bcm_uart_do_rx(port);
379ab4382d2SGreg Kroah-Hartman 
380ab4382d2SGreg Kroah-Hartman 	if (irqstat & UART_TX_INT_STAT)
381ab4382d2SGreg Kroah-Hartman 		bcm_uart_do_tx(port);
382ab4382d2SGreg Kroah-Hartman 
383ab4382d2SGreg Kroah-Hartman 	if (irqstat & UART_IR_MASK(UART_IR_EXTIP)) {
384ab4382d2SGreg Kroah-Hartman 		unsigned int estat;
385ab4382d2SGreg Kroah-Hartman 
386ab4382d2SGreg Kroah-Hartman 		estat = bcm_uart_readl(port, UART_EXTINP_REG);
387ab4382d2SGreg Kroah-Hartman 		if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_CTS))
388ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port,
389ab4382d2SGreg Kroah-Hartman 					       estat & UART_EXTINP_CTS_MASK);
390ab4382d2SGreg Kroah-Hartman 		if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_DCD))
391ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port,
392ab4382d2SGreg Kroah-Hartman 					       estat & UART_EXTINP_DCD_MASK);
393ab4382d2SGreg Kroah-Hartman 	}
394ab4382d2SGreg Kroah-Hartman 
395ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
396ab4382d2SGreg Kroah-Hartman 	return IRQ_HANDLED;
397ab4382d2SGreg Kroah-Hartman }
398ab4382d2SGreg Kroah-Hartman 
399ab4382d2SGreg Kroah-Hartman /*
400ab4382d2SGreg Kroah-Hartman  * enable rx & tx operation on uart
401ab4382d2SGreg Kroah-Hartman  */
402ab4382d2SGreg Kroah-Hartman static void bcm_uart_enable(struct uart_port *port)
403ab4382d2SGreg Kroah-Hartman {
404ab4382d2SGreg Kroah-Hartman 	unsigned int val;
405ab4382d2SGreg Kroah-Hartman 
406ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_CTL_REG);
407ab4382d2SGreg Kroah-Hartman 	val |= (UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK);
408ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_CTL_REG);
409ab4382d2SGreg Kroah-Hartman }
410ab4382d2SGreg Kroah-Hartman 
411ab4382d2SGreg Kroah-Hartman /*
412ab4382d2SGreg Kroah-Hartman  * disable rx & tx operation on uart
413ab4382d2SGreg Kroah-Hartman  */
414ab4382d2SGreg Kroah-Hartman static void bcm_uart_disable(struct uart_port *port)
415ab4382d2SGreg Kroah-Hartman {
416ab4382d2SGreg Kroah-Hartman 	unsigned int val;
417ab4382d2SGreg Kroah-Hartman 
418ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_CTL_REG);
419ab4382d2SGreg Kroah-Hartman 	val &= ~(UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK |
420ab4382d2SGreg Kroah-Hartman 		 UART_CTL_RXEN_MASK);
421ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_CTL_REG);
422ab4382d2SGreg Kroah-Hartman }
423ab4382d2SGreg Kroah-Hartman 
424ab4382d2SGreg Kroah-Hartman /*
425ab4382d2SGreg Kroah-Hartman  * clear all unread data in rx fifo and unsent data in tx fifo
426ab4382d2SGreg Kroah-Hartman  */
427ab4382d2SGreg Kroah-Hartman static void bcm_uart_flush(struct uart_port *port)
428ab4382d2SGreg Kroah-Hartman {
429ab4382d2SGreg Kroah-Hartman 	unsigned int val;
430ab4382d2SGreg Kroah-Hartman 
431ab4382d2SGreg Kroah-Hartman 	/* empty rx and tx fifo */
432ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_CTL_REG);
433ab4382d2SGreg Kroah-Hartman 	val |= UART_CTL_RSTRXFIFO_MASK | UART_CTL_RSTTXFIFO_MASK;
434ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_CTL_REG);
435ab4382d2SGreg Kroah-Hartman 
436ab4382d2SGreg Kroah-Hartman 	/* read any pending char to make sure all irq status are
437ab4382d2SGreg Kroah-Hartman 	 * cleared */
438ab4382d2SGreg Kroah-Hartman 	(void)bcm_uart_readl(port, UART_FIFO_REG);
439ab4382d2SGreg Kroah-Hartman }
440ab4382d2SGreg Kroah-Hartman 
441ab4382d2SGreg Kroah-Hartman /*
442ab4382d2SGreg Kroah-Hartman  * serial core request to initialize uart and start rx operation
443ab4382d2SGreg Kroah-Hartman  */
444ab4382d2SGreg Kroah-Hartman static int bcm_uart_startup(struct uart_port *port)
445ab4382d2SGreg Kroah-Hartman {
446ab4382d2SGreg Kroah-Hartman 	unsigned int val;
447ab4382d2SGreg Kroah-Hartman 	int ret;
448ab4382d2SGreg Kroah-Hartman 
449ab4382d2SGreg Kroah-Hartman 	/* mask all irq and flush port */
450ab4382d2SGreg Kroah-Hartman 	bcm_uart_disable(port);
451ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, 0, UART_IR_REG);
452ab4382d2SGreg Kroah-Hartman 	bcm_uart_flush(port);
453ab4382d2SGreg Kroah-Hartman 
454ab4382d2SGreg Kroah-Hartman 	/* clear any pending external input interrupt */
455ab4382d2SGreg Kroah-Hartman 	(void)bcm_uart_readl(port, UART_EXTINP_REG);
456ab4382d2SGreg Kroah-Hartman 
457ab4382d2SGreg Kroah-Hartman 	/* set rx/tx fifo thresh to fifo half size */
458ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_MCTL_REG);
459ab4382d2SGreg Kroah-Hartman 	val &= ~(UART_MCTL_RXFIFOTHRESH_MASK | UART_MCTL_TXFIFOTHRESH_MASK);
460ab4382d2SGreg Kroah-Hartman 	val |= (port->fifosize / 2) << UART_MCTL_RXFIFOTHRESH_SHIFT;
461ab4382d2SGreg Kroah-Hartman 	val |= (port->fifosize / 2) << UART_MCTL_TXFIFOTHRESH_SHIFT;
462ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_MCTL_REG);
463ab4382d2SGreg Kroah-Hartman 
464ab4382d2SGreg Kroah-Hartman 	/* set rx fifo timeout to 1 char time */
465ab4382d2SGreg Kroah-Hartman 	val = bcm_uart_readl(port, UART_CTL_REG);
466ab4382d2SGreg Kroah-Hartman 	val &= ~UART_CTL_RXTMOUTCNT_MASK;
467ab4382d2SGreg Kroah-Hartman 	val |= 1 << UART_CTL_RXTMOUTCNT_SHIFT;
468ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_CTL_REG);
469ab4382d2SGreg Kroah-Hartman 
470ab4382d2SGreg Kroah-Hartman 	/* report any edge on dcd and cts */
471ab4382d2SGreg Kroah-Hartman 	val = UART_EXTINP_INT_MASK;
472ab4382d2SGreg Kroah-Hartman 	val |= UART_EXTINP_DCD_NOSENSE_MASK;
473ab4382d2SGreg Kroah-Hartman 	val |= UART_EXTINP_CTS_NOSENSE_MASK;
474ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, val, UART_EXTINP_REG);
475ab4382d2SGreg Kroah-Hartman 
476ab4382d2SGreg Kroah-Hartman 	/* register irq and enable rx interrupts */
477ab4382d2SGreg Kroah-Hartman 	ret = request_irq(port->irq, bcm_uart_interrupt, 0,
478ab4382d2SGreg Kroah-Hartman 			  bcm_uart_type(port), port);
479ab4382d2SGreg Kroah-Hartman 	if (ret)
480ab4382d2SGreg Kroah-Hartman 		return ret;
481ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, UART_RX_INT_MASK, UART_IR_REG);
482ab4382d2SGreg Kroah-Hartman 	bcm_uart_enable(port);
483ab4382d2SGreg Kroah-Hartman 	return 0;
484ab4382d2SGreg Kroah-Hartman }
485ab4382d2SGreg Kroah-Hartman 
486ab4382d2SGreg Kroah-Hartman /*
487ab4382d2SGreg Kroah-Hartman  * serial core request to flush & disable uart
488ab4382d2SGreg Kroah-Hartman  */
489ab4382d2SGreg Kroah-Hartman static void bcm_uart_shutdown(struct uart_port *port)
490ab4382d2SGreg Kroah-Hartman {
491ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
492ab4382d2SGreg Kroah-Hartman 
493ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
494ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, 0, UART_IR_REG);
495ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
496ab4382d2SGreg Kroah-Hartman 
497ab4382d2SGreg Kroah-Hartman 	bcm_uart_disable(port);
498ab4382d2SGreg Kroah-Hartman 	bcm_uart_flush(port);
499ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
500ab4382d2SGreg Kroah-Hartman }
501ab4382d2SGreg Kroah-Hartman 
502ab4382d2SGreg Kroah-Hartman /*
503ab4382d2SGreg Kroah-Hartman  * serial core request to change current uart setting
504ab4382d2SGreg Kroah-Hartman  */
505ab4382d2SGreg Kroah-Hartman static void bcm_uart_set_termios(struct uart_port *port,
506ab4382d2SGreg Kroah-Hartman 				 struct ktermios *new,
507ab4382d2SGreg Kroah-Hartman 				 struct ktermios *old)
508ab4382d2SGreg Kroah-Hartman {
509ab4382d2SGreg Kroah-Hartman 	unsigned int ctl, baud, quot, ier;
510ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
511ab4382d2SGreg Kroah-Hartman 
512ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
513ab4382d2SGreg Kroah-Hartman 
514ab4382d2SGreg Kroah-Hartman 	/* disable uart while changing speed */
515ab4382d2SGreg Kroah-Hartman 	bcm_uart_disable(port);
516ab4382d2SGreg Kroah-Hartman 	bcm_uart_flush(port);
517ab4382d2SGreg Kroah-Hartman 
518ab4382d2SGreg Kroah-Hartman 	/* update Control register */
519ab4382d2SGreg Kroah-Hartman 	ctl = bcm_uart_readl(port, UART_CTL_REG);
520ab4382d2SGreg Kroah-Hartman 	ctl &= ~UART_CTL_BITSPERSYM_MASK;
521ab4382d2SGreg Kroah-Hartman 
522ab4382d2SGreg Kroah-Hartman 	switch (new->c_cflag & CSIZE) {
523ab4382d2SGreg Kroah-Hartman 	case CS5:
524ab4382d2SGreg Kroah-Hartman 		ctl |= (0 << UART_CTL_BITSPERSYM_SHIFT);
525ab4382d2SGreg Kroah-Hartman 		break;
526ab4382d2SGreg Kroah-Hartman 	case CS6:
527ab4382d2SGreg Kroah-Hartman 		ctl |= (1 << UART_CTL_BITSPERSYM_SHIFT);
528ab4382d2SGreg Kroah-Hartman 		break;
529ab4382d2SGreg Kroah-Hartman 	case CS7:
530ab4382d2SGreg Kroah-Hartman 		ctl |= (2 << UART_CTL_BITSPERSYM_SHIFT);
531ab4382d2SGreg Kroah-Hartman 		break;
532ab4382d2SGreg Kroah-Hartman 	default:
533ab4382d2SGreg Kroah-Hartman 		ctl |= (3 << UART_CTL_BITSPERSYM_SHIFT);
534ab4382d2SGreg Kroah-Hartman 		break;
535ab4382d2SGreg Kroah-Hartman 	}
536ab4382d2SGreg Kroah-Hartman 
537ab4382d2SGreg Kroah-Hartman 	ctl &= ~UART_CTL_STOPBITS_MASK;
538ab4382d2SGreg Kroah-Hartman 	if (new->c_cflag & CSTOPB)
539ab4382d2SGreg Kroah-Hartman 		ctl |= UART_CTL_STOPBITS_2;
540ab4382d2SGreg Kroah-Hartman 	else
541ab4382d2SGreg Kroah-Hartman 		ctl |= UART_CTL_STOPBITS_1;
542ab4382d2SGreg Kroah-Hartman 
543ab4382d2SGreg Kroah-Hartman 	ctl &= ~(UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
544ab4382d2SGreg Kroah-Hartman 	if (new->c_cflag & PARENB)
545ab4382d2SGreg Kroah-Hartman 		ctl |= (UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
546ab4382d2SGreg Kroah-Hartman 	ctl &= ~(UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
547ab4382d2SGreg Kroah-Hartman 	if (new->c_cflag & PARODD)
548ab4382d2SGreg Kroah-Hartman 		ctl |= (UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
549ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, ctl, UART_CTL_REG);
550ab4382d2SGreg Kroah-Hartman 
551ab4382d2SGreg Kroah-Hartman 	/* update Baudword register */
552ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
553ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud) - 1;
554ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, quot, UART_BAUD_REG);
555ab4382d2SGreg Kroah-Hartman 
556ab4382d2SGreg Kroah-Hartman 	/* update Interrupt register */
557ab4382d2SGreg Kroah-Hartman 	ier = bcm_uart_readl(port, UART_IR_REG);
558ab4382d2SGreg Kroah-Hartman 
559ab4382d2SGreg Kroah-Hartman 	ier &= ~UART_IR_MASK(UART_IR_EXTIP);
560ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, new->c_cflag))
561ab4382d2SGreg Kroah-Hartman 		ier |= UART_IR_MASK(UART_IR_EXTIP);
562ab4382d2SGreg Kroah-Hartman 
563ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, ier, UART_IR_REG);
564ab4382d2SGreg Kroah-Hartman 
565ab4382d2SGreg Kroah-Hartman 	/* update read/ignore mask */
566ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = UART_FIFO_VALID_MASK;
567ab4382d2SGreg Kroah-Hartman 	if (new->c_iflag & INPCK) {
568ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= UART_FIFO_FRAMEERR_MASK;
569ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= UART_FIFO_PARERR_MASK;
570ab4382d2SGreg Kroah-Hartman 	}
571ab4382d2SGreg Kroah-Hartman 	if (new->c_iflag & (BRKINT))
572ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= UART_FIFO_BRKDET_MASK;
573ab4382d2SGreg Kroah-Hartman 
574ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
575ab4382d2SGreg Kroah-Hartman 	if (new->c_iflag & IGNPAR)
576ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= UART_FIFO_PARERR_MASK;
577ab4382d2SGreg Kroah-Hartman 	if (new->c_iflag & IGNBRK)
578ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= UART_FIFO_BRKDET_MASK;
579ab4382d2SGreg Kroah-Hartman 	if (!(new->c_cflag & CREAD))
580ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= UART_FIFO_VALID_MASK;
581ab4382d2SGreg Kroah-Hartman 
582ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, new->c_cflag, baud);
583ab4382d2SGreg Kroah-Hartman 	bcm_uart_enable(port);
584ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
585ab4382d2SGreg Kroah-Hartman }
586ab4382d2SGreg Kroah-Hartman 
587ab4382d2SGreg Kroah-Hartman /*
588ab4382d2SGreg Kroah-Hartman  * serial core request to claim uart iomem
589ab4382d2SGreg Kroah-Hartman  */
590ab4382d2SGreg Kroah-Hartman static int bcm_uart_request_port(struct uart_port *port)
591ab4382d2SGreg Kroah-Hartman {
592ab4382d2SGreg Kroah-Hartman 	unsigned int size;
593ab4382d2SGreg Kroah-Hartman 
594ab4382d2SGreg Kroah-Hartman 	size = RSET_UART_SIZE;
595ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "bcm63xx")) {
596ab4382d2SGreg Kroah-Hartman 		dev_err(port->dev, "Memory region busy\n");
597ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
598ab4382d2SGreg Kroah-Hartman 	}
599ab4382d2SGreg Kroah-Hartman 
600ab4382d2SGreg Kroah-Hartman 	port->membase = ioremap(port->mapbase, size);
601ab4382d2SGreg Kroah-Hartman 	if (!port->membase) {
602ab4382d2SGreg Kroah-Hartman 		dev_err(port->dev, "Unable to map registers\n");
603ab4382d2SGreg Kroah-Hartman 		release_mem_region(port->mapbase, size);
604ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
605ab4382d2SGreg Kroah-Hartman 	}
606ab4382d2SGreg Kroah-Hartman 	return 0;
607ab4382d2SGreg Kroah-Hartman }
608ab4382d2SGreg Kroah-Hartman 
609ab4382d2SGreg Kroah-Hartman /*
610ab4382d2SGreg Kroah-Hartman  * serial core request to release uart iomem
611ab4382d2SGreg Kroah-Hartman  */
612ab4382d2SGreg Kroah-Hartman static void bcm_uart_release_port(struct uart_port *port)
613ab4382d2SGreg Kroah-Hartman {
614ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, RSET_UART_SIZE);
615ab4382d2SGreg Kroah-Hartman 	iounmap(port->membase);
616ab4382d2SGreg Kroah-Hartman }
617ab4382d2SGreg Kroah-Hartman 
618ab4382d2SGreg Kroah-Hartman /*
619ab4382d2SGreg Kroah-Hartman  * serial core request to do any port required autoconfiguration
620ab4382d2SGreg Kroah-Hartman  */
621ab4382d2SGreg Kroah-Hartman static void bcm_uart_config_port(struct uart_port *port, int flags)
622ab4382d2SGreg Kroah-Hartman {
623ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
624ab4382d2SGreg Kroah-Hartman 		if (bcm_uart_request_port(port))
625ab4382d2SGreg Kroah-Hartman 			return;
626ab4382d2SGreg Kroah-Hartman 		port->type = PORT_BCM63XX;
627ab4382d2SGreg Kroah-Hartman 	}
628ab4382d2SGreg Kroah-Hartman }
629ab4382d2SGreg Kroah-Hartman 
630ab4382d2SGreg Kroah-Hartman /*
631ab4382d2SGreg Kroah-Hartman  * serial core request to check that port information in serinfo are
632ab4382d2SGreg Kroah-Hartman  * suitable
633ab4382d2SGreg Kroah-Hartman  */
634ab4382d2SGreg Kroah-Hartman static int bcm_uart_verify_port(struct uart_port *port,
635ab4382d2SGreg Kroah-Hartman 				struct serial_struct *serinfo)
636ab4382d2SGreg Kroah-Hartman {
637ab4382d2SGreg Kroah-Hartman 	if (port->type != PORT_BCM63XX)
638ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
639ab4382d2SGreg Kroah-Hartman 	if (port->irq != serinfo->irq)
640ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
641ab4382d2SGreg Kroah-Hartman 	if (port->iotype != serinfo->io_type)
642ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
643ab4382d2SGreg Kroah-Hartman 	if (port->mapbase != (unsigned long)serinfo->iomem_base)
644ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
645ab4382d2SGreg Kroah-Hartman 	return 0;
646ab4382d2SGreg Kroah-Hartman }
647ab4382d2SGreg Kroah-Hartman 
648ab4382d2SGreg Kroah-Hartman /* serial core callbacks */
649ab4382d2SGreg Kroah-Hartman static struct uart_ops bcm_uart_ops = {
650ab4382d2SGreg Kroah-Hartman 	.tx_empty	= bcm_uart_tx_empty,
651ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= bcm_uart_get_mctrl,
652ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= bcm_uart_set_mctrl,
653ab4382d2SGreg Kroah-Hartman 	.start_tx	= bcm_uart_start_tx,
654ab4382d2SGreg Kroah-Hartman 	.stop_tx	= bcm_uart_stop_tx,
655ab4382d2SGreg Kroah-Hartman 	.stop_rx	= bcm_uart_stop_rx,
656ab4382d2SGreg Kroah-Hartman 	.enable_ms	= bcm_uart_enable_ms,
657ab4382d2SGreg Kroah-Hartman 	.break_ctl	= bcm_uart_break_ctl,
658ab4382d2SGreg Kroah-Hartman 	.startup	= bcm_uart_startup,
659ab4382d2SGreg Kroah-Hartman 	.shutdown	= bcm_uart_shutdown,
660ab4382d2SGreg Kroah-Hartman 	.set_termios	= bcm_uart_set_termios,
661ab4382d2SGreg Kroah-Hartman 	.type		= bcm_uart_type,
662ab4382d2SGreg Kroah-Hartman 	.release_port	= bcm_uart_release_port,
663ab4382d2SGreg Kroah-Hartman 	.request_port	= bcm_uart_request_port,
664ab4382d2SGreg Kroah-Hartman 	.config_port	= bcm_uart_config_port,
665ab4382d2SGreg Kroah-Hartman 	.verify_port	= bcm_uart_verify_port,
666ab4382d2SGreg Kroah-Hartman };
667ab4382d2SGreg Kroah-Hartman 
668ab4382d2SGreg Kroah-Hartman 
669ab4382d2SGreg Kroah-Hartman 
670ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_BCM63XX_CONSOLE
671ab4382d2SGreg Kroah-Hartman static inline void wait_for_xmitr(struct uart_port *port)
672ab4382d2SGreg Kroah-Hartman {
673ab4382d2SGreg Kroah-Hartman 	unsigned int tmout;
674ab4382d2SGreg Kroah-Hartman 
675ab4382d2SGreg Kroah-Hartman 	/* Wait up to 10ms for the character(s) to be sent. */
676ab4382d2SGreg Kroah-Hartman 	tmout = 10000;
677ab4382d2SGreg Kroah-Hartman 	while (--tmout) {
678ab4382d2SGreg Kroah-Hartman 		unsigned int val;
679ab4382d2SGreg Kroah-Hartman 
680ab4382d2SGreg Kroah-Hartman 		val = bcm_uart_readl(port, UART_IR_REG);
681ab4382d2SGreg Kroah-Hartman 		if (val & UART_IR_STAT(UART_IR_TXEMPTY))
682ab4382d2SGreg Kroah-Hartman 			break;
683ab4382d2SGreg Kroah-Hartman 		udelay(1);
684ab4382d2SGreg Kroah-Hartman 	}
685ab4382d2SGreg Kroah-Hartman 
686ab4382d2SGreg Kroah-Hartman 	/* Wait up to 1s for flow control if necessary */
687ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_CONS_FLOW) {
688ab4382d2SGreg Kroah-Hartman 		tmout = 1000000;
689ab4382d2SGreg Kroah-Hartman 		while (--tmout) {
690ab4382d2SGreg Kroah-Hartman 			unsigned int val;
691ab4382d2SGreg Kroah-Hartman 
692ab4382d2SGreg Kroah-Hartman 			val = bcm_uart_readl(port, UART_EXTINP_REG);
693ab4382d2SGreg Kroah-Hartman 			if (val & UART_EXTINP_CTS_MASK)
694ab4382d2SGreg Kroah-Hartman 				break;
695ab4382d2SGreg Kroah-Hartman 			udelay(1);
696ab4382d2SGreg Kroah-Hartman 		}
697ab4382d2SGreg Kroah-Hartman 	}
698ab4382d2SGreg Kroah-Hartman }
699ab4382d2SGreg Kroah-Hartman 
700ab4382d2SGreg Kroah-Hartman /*
701ab4382d2SGreg Kroah-Hartman  * output given char
702ab4382d2SGreg Kroah-Hartman  */
703ab4382d2SGreg Kroah-Hartman static void bcm_console_putchar(struct uart_port *port, int ch)
704ab4382d2SGreg Kroah-Hartman {
705ab4382d2SGreg Kroah-Hartman 	wait_for_xmitr(port);
706ab4382d2SGreg Kroah-Hartman 	bcm_uart_writel(port, ch, UART_FIFO_REG);
707ab4382d2SGreg Kroah-Hartman }
708ab4382d2SGreg Kroah-Hartman 
709ab4382d2SGreg Kroah-Hartman /*
710ab4382d2SGreg Kroah-Hartman  * console core request to output given string
711ab4382d2SGreg Kroah-Hartman  */
712ab4382d2SGreg Kroah-Hartman static void bcm_console_write(struct console *co, const char *s,
713ab4382d2SGreg Kroah-Hartman 			      unsigned int count)
714ab4382d2SGreg Kroah-Hartman {
715ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
716ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
717ab4382d2SGreg Kroah-Hartman 	int locked;
718ab4382d2SGreg Kroah-Hartman 
719ab4382d2SGreg Kroah-Hartman 	port = &ports[co->index];
720ab4382d2SGreg Kroah-Hartman 
721ab4382d2SGreg Kroah-Hartman 	local_irq_save(flags);
722ab4382d2SGreg Kroah-Hartman 	if (port->sysrq) {
723ab4382d2SGreg Kroah-Hartman 		/* bcm_uart_interrupt() already took the lock */
724ab4382d2SGreg Kroah-Hartman 		locked = 0;
725ab4382d2SGreg Kroah-Hartman 	} else if (oops_in_progress) {
726ab4382d2SGreg Kroah-Hartman 		locked = spin_trylock(&port->lock);
727ab4382d2SGreg Kroah-Hartman 	} else {
728ab4382d2SGreg Kroah-Hartman 		spin_lock(&port->lock);
729ab4382d2SGreg Kroah-Hartman 		locked = 1;
730ab4382d2SGreg Kroah-Hartman 	}
731ab4382d2SGreg Kroah-Hartman 
732ab4382d2SGreg Kroah-Hartman 	/* call helper to deal with \r\n */
733ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, bcm_console_putchar);
734ab4382d2SGreg Kroah-Hartman 
735ab4382d2SGreg Kroah-Hartman 	/* and wait for char to be transmitted */
736ab4382d2SGreg Kroah-Hartman 	wait_for_xmitr(port);
737ab4382d2SGreg Kroah-Hartman 
738ab4382d2SGreg Kroah-Hartman 	if (locked)
739ab4382d2SGreg Kroah-Hartman 		spin_unlock(&port->lock);
740ab4382d2SGreg Kroah-Hartman 	local_irq_restore(flags);
741ab4382d2SGreg Kroah-Hartman }
742ab4382d2SGreg Kroah-Hartman 
743ab4382d2SGreg Kroah-Hartman /*
744ab4382d2SGreg Kroah-Hartman  * console core request to setup given console, find matching uart
745ab4382d2SGreg Kroah-Hartman  * port and setup it.
746ab4382d2SGreg Kroah-Hartman  */
747ab4382d2SGreg Kroah-Hartman static int bcm_console_setup(struct console *co, char *options)
748ab4382d2SGreg Kroah-Hartman {
749ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
750ab4382d2SGreg Kroah-Hartman 	int baud = 9600;
751ab4382d2SGreg Kroah-Hartman 	int bits = 8;
752ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
753ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
754ab4382d2SGreg Kroah-Hartman 
755ab4382d2SGreg Kroah-Hartman 	if (co->index < 0 || co->index >= BCM63XX_NR_UARTS)
756ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
757ab4382d2SGreg Kroah-Hartman 	port = &ports[co->index];
758ab4382d2SGreg Kroah-Hartman 	if (!port->membase)
759ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
760ab4382d2SGreg Kroah-Hartman 	if (options)
761ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
762ab4382d2SGreg Kroah-Hartman 
763ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
764ab4382d2SGreg Kroah-Hartman }
765ab4382d2SGreg Kroah-Hartman 
766ab4382d2SGreg Kroah-Hartman static struct uart_driver bcm_uart_driver;
767ab4382d2SGreg Kroah-Hartman 
768ab4382d2SGreg Kroah-Hartman static struct console bcm63xx_console = {
769ab4382d2SGreg Kroah-Hartman 	.name		= "ttyS",
770ab4382d2SGreg Kroah-Hartman 	.write		= bcm_console_write,
771ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
772ab4382d2SGreg Kroah-Hartman 	.setup		= bcm_console_setup,
773ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
774ab4382d2SGreg Kroah-Hartman 	.index		= -1,
775ab4382d2SGreg Kroah-Hartman 	.data		= &bcm_uart_driver,
776ab4382d2SGreg Kroah-Hartman };
777ab4382d2SGreg Kroah-Hartman 
778ab4382d2SGreg Kroah-Hartman static int __init bcm63xx_console_init(void)
779ab4382d2SGreg Kroah-Hartman {
780ab4382d2SGreg Kroah-Hartman 	register_console(&bcm63xx_console);
781ab4382d2SGreg Kroah-Hartman 	return 0;
782ab4382d2SGreg Kroah-Hartman }
783ab4382d2SGreg Kroah-Hartman 
784ab4382d2SGreg Kroah-Hartman console_initcall(bcm63xx_console_init);
785ab4382d2SGreg Kroah-Hartman 
786ab4382d2SGreg Kroah-Hartman #define BCM63XX_CONSOLE	(&bcm63xx_console)
787ab4382d2SGreg Kroah-Hartman #else
788ab4382d2SGreg Kroah-Hartman #define BCM63XX_CONSOLE	NULL
789ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_BCM63XX_CONSOLE */
790ab4382d2SGreg Kroah-Hartman 
791ab4382d2SGreg Kroah-Hartman static struct uart_driver bcm_uart_driver = {
792ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
793ab4382d2SGreg Kroah-Hartman 	.driver_name	= "bcm63xx_uart",
794ab4382d2SGreg Kroah-Hartman 	.dev_name	= "ttyS",
795ab4382d2SGreg Kroah-Hartman 	.major		= TTY_MAJOR,
796ab4382d2SGreg Kroah-Hartman 	.minor		= 64,
797ab4382d2SGreg Kroah-Hartman 	.nr		= BCM63XX_NR_UARTS,
798ab4382d2SGreg Kroah-Hartman 	.cons		= BCM63XX_CONSOLE,
799ab4382d2SGreg Kroah-Hartman };
800ab4382d2SGreg Kroah-Hartman 
801ab4382d2SGreg Kroah-Hartman /*
802ab4382d2SGreg Kroah-Hartman  * platform driver probe/remove callback
803ab4382d2SGreg Kroah-Hartman  */
804ab4382d2SGreg Kroah-Hartman static int __devinit bcm_uart_probe(struct platform_device *pdev)
805ab4382d2SGreg Kroah-Hartman {
806ab4382d2SGreg Kroah-Hartman 	struct resource *res_mem, *res_irq;
807ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
808ab4382d2SGreg Kroah-Hartman 	struct clk *clk;
809ab4382d2SGreg Kroah-Hartman 	int ret;
810ab4382d2SGreg Kroah-Hartman 
811ab4382d2SGreg Kroah-Hartman 	if (pdev->id < 0 || pdev->id >= BCM63XX_NR_UARTS)
812ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
813ab4382d2SGreg Kroah-Hartman 
814ab4382d2SGreg Kroah-Hartman 	if (ports[pdev->id].membase)
815ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
816ab4382d2SGreg Kroah-Hartman 
817ab4382d2SGreg Kroah-Hartman 	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
818ab4382d2SGreg Kroah-Hartman 	if (!res_mem)
819ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
820ab4382d2SGreg Kroah-Hartman 
821ab4382d2SGreg Kroah-Hartman 	res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
822ab4382d2SGreg Kroah-Hartman 	if (!res_irq)
823ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
824ab4382d2SGreg Kroah-Hartman 
825ab4382d2SGreg Kroah-Hartman 	clk = clk_get(&pdev->dev, "periph");
826ab4382d2SGreg Kroah-Hartman 	if (IS_ERR(clk))
827ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
828ab4382d2SGreg Kroah-Hartman 
829ab4382d2SGreg Kroah-Hartman 	port = &ports[pdev->id];
830ab4382d2SGreg Kroah-Hartman 	memset(port, 0, sizeof(*port));
831ab4382d2SGreg Kroah-Hartman 	port->iotype = UPIO_MEM;
832ab4382d2SGreg Kroah-Hartman 	port->mapbase = res_mem->start;
833ab4382d2SGreg Kroah-Hartman 	port->irq = res_irq->start;
834ab4382d2SGreg Kroah-Hartman 	port->ops = &bcm_uart_ops;
835ab4382d2SGreg Kroah-Hartman 	port->flags = UPF_BOOT_AUTOCONF;
836ab4382d2SGreg Kroah-Hartman 	port->dev = &pdev->dev;
837ab4382d2SGreg Kroah-Hartman 	port->fifosize = 16;
838ab4382d2SGreg Kroah-Hartman 	port->uartclk = clk_get_rate(clk) / 2;
839ab4382d2SGreg Kroah-Hartman 	port->line = pdev->id;
840ab4382d2SGreg Kroah-Hartman 	clk_put(clk);
841ab4382d2SGreg Kroah-Hartman 
842ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&bcm_uart_driver, port);
843ab4382d2SGreg Kroah-Hartman 	if (ret) {
844ab4382d2SGreg Kroah-Hartman 		ports[pdev->id].membase = 0;
845ab4382d2SGreg Kroah-Hartman 		return ret;
846ab4382d2SGreg Kroah-Hartman 	}
847ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
848ab4382d2SGreg Kroah-Hartman 	return 0;
849ab4382d2SGreg Kroah-Hartman }
850ab4382d2SGreg Kroah-Hartman 
851ab4382d2SGreg Kroah-Hartman static int __devexit bcm_uart_remove(struct platform_device *pdev)
852ab4382d2SGreg Kroah-Hartman {
853ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
854ab4382d2SGreg Kroah-Hartman 
855ab4382d2SGreg Kroah-Hartman 	port = platform_get_drvdata(pdev);
856ab4382d2SGreg Kroah-Hartman 	uart_remove_one_port(&bcm_uart_driver, port);
857ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, NULL);
858ab4382d2SGreg Kroah-Hartman 	/* mark port as free */
859ab4382d2SGreg Kroah-Hartman 	ports[pdev->id].membase = 0;
860ab4382d2SGreg Kroah-Hartman 	return 0;
861ab4382d2SGreg Kroah-Hartman }
862ab4382d2SGreg Kroah-Hartman 
863ab4382d2SGreg Kroah-Hartman /*
864ab4382d2SGreg Kroah-Hartman  * platform driver stuff
865ab4382d2SGreg Kroah-Hartman  */
866ab4382d2SGreg Kroah-Hartman static struct platform_driver bcm_uart_platform_driver = {
867ab4382d2SGreg Kroah-Hartman 	.probe	= bcm_uart_probe,
868*2d47b716SBill Pemberton 	.remove	= bcm_uart_remove,
869ab4382d2SGreg Kroah-Hartman 	.driver	= {
870ab4382d2SGreg Kroah-Hartman 		.owner = THIS_MODULE,
871ab4382d2SGreg Kroah-Hartman 		.name  = "bcm63xx_uart",
872ab4382d2SGreg Kroah-Hartman 	},
873ab4382d2SGreg Kroah-Hartman };
874ab4382d2SGreg Kroah-Hartman 
875ab4382d2SGreg Kroah-Hartman static int __init bcm_uart_init(void)
876ab4382d2SGreg Kroah-Hartman {
877ab4382d2SGreg Kroah-Hartman 	int ret;
878ab4382d2SGreg Kroah-Hartman 
879ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&bcm_uart_driver);
880ab4382d2SGreg Kroah-Hartman 	if (ret)
881ab4382d2SGreg Kroah-Hartman 		return ret;
882ab4382d2SGreg Kroah-Hartman 
883ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&bcm_uart_platform_driver);
884ab4382d2SGreg Kroah-Hartman 	if (ret)
885ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&bcm_uart_driver);
886ab4382d2SGreg Kroah-Hartman 
887ab4382d2SGreg Kroah-Hartman 	return ret;
888ab4382d2SGreg Kroah-Hartman }
889ab4382d2SGreg Kroah-Hartman 
890ab4382d2SGreg Kroah-Hartman static void __exit bcm_uart_exit(void)
891ab4382d2SGreg Kroah-Hartman {
892ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&bcm_uart_platform_driver);
893ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&bcm_uart_driver);
894ab4382d2SGreg Kroah-Hartman }
895ab4382d2SGreg Kroah-Hartman 
896ab4382d2SGreg Kroah-Hartman module_init(bcm_uart_init);
897ab4382d2SGreg Kroah-Hartman module_exit(bcm_uart_exit);
898ab4382d2SGreg Kroah-Hartman 
899ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
900ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Broadcom 63<xx integrated uart driver");
901ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
902