xref: /openbmc/linux/drivers/tty/serial/men_z135_uart.c (revision 01ba8d6af4b76aee99a8ae3a76748c8c36db49f3)
1e264ebf4SJohannes Thumshirn /*
2e264ebf4SJohannes Thumshirn  * MEN 16z135 High Speed UART
3e264ebf4SJohannes Thumshirn  *
4e264ebf4SJohannes Thumshirn  * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
5e264ebf4SJohannes Thumshirn  * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
6e264ebf4SJohannes Thumshirn  *
7e264ebf4SJohannes Thumshirn  * This program is free software; you can redistribute it and/or modify it
8e264ebf4SJohannes Thumshirn  * under the terms of the GNU General Public License as published by the Free
9e264ebf4SJohannes Thumshirn  * Software Foundation; version 2 of the License.
10e264ebf4SJohannes Thumshirn  */
11e264ebf4SJohannes Thumshirn #define pr_fmt(fmt) KBUILD_MODNAME ":" fmt
12e264ebf4SJohannes Thumshirn 
13e264ebf4SJohannes Thumshirn #include <linux/kernel.h>
14e264ebf4SJohannes Thumshirn #include <linux/module.h>
15e264ebf4SJohannes Thumshirn #include <linux/interrupt.h>
16e264ebf4SJohannes Thumshirn #include <linux/serial_core.h>
17e264ebf4SJohannes Thumshirn #include <linux/ioport.h>
18e264ebf4SJohannes Thumshirn #include <linux/io.h>
19e264ebf4SJohannes Thumshirn #include <linux/tty_flip.h>
20e264ebf4SJohannes Thumshirn #include <linux/bitops.h>
21e264ebf4SJohannes Thumshirn #include <linux/mcb.h>
22e264ebf4SJohannes Thumshirn 
23e264ebf4SJohannes Thumshirn #define MEN_Z135_MAX_PORTS		12
24e264ebf4SJohannes Thumshirn #define MEN_Z135_BASECLK		29491200
25e264ebf4SJohannes Thumshirn #define MEN_Z135_FIFO_SIZE		1024
26e264ebf4SJohannes Thumshirn #define MEN_Z135_FIFO_WATERMARK		1020
27e264ebf4SJohannes Thumshirn 
28e264ebf4SJohannes Thumshirn #define MEN_Z135_STAT_REG		0x0
29e264ebf4SJohannes Thumshirn #define MEN_Z135_RX_RAM			0x4
30e264ebf4SJohannes Thumshirn #define MEN_Z135_TX_RAM			0x400
31e264ebf4SJohannes Thumshirn #define MEN_Z135_RX_CTRL		0x800
32e264ebf4SJohannes Thumshirn #define MEN_Z135_TX_CTRL		0x804
33e264ebf4SJohannes Thumshirn #define MEN_Z135_CONF_REG		0x808
34e264ebf4SJohannes Thumshirn #define MEN_Z135_UART_FREQ		0x80c
35e264ebf4SJohannes Thumshirn #define MEN_Z135_BAUD_REG		0x810
36*01ba8d6aSJohannes Thumshirn #define MEN_Z135_TIMEOUT		0x814
37e264ebf4SJohannes Thumshirn 
38e264ebf4SJohannes Thumshirn #define MEN_Z135_MEM_SIZE		0x818
39e264ebf4SJohannes Thumshirn 
40*01ba8d6aSJohannes Thumshirn #define IRQ_ID(x) ((x) & 0x1f)
41e264ebf4SJohannes Thumshirn 
4210389e66SJohannes Thumshirn #define MEN_Z135_IER_RXCIEN BIT(0)		/* RX Space IRQ */
4310389e66SJohannes Thumshirn #define MEN_Z135_IER_TXCIEN BIT(1)		/* TX Space IRQ */
44e264ebf4SJohannes Thumshirn #define MEN_Z135_IER_RLSIEN BIT(2)		/* Receiver Line Status IRQ */
45e264ebf4SJohannes Thumshirn #define MEN_Z135_IER_MSIEN  BIT(3)		/* Modem Status IRQ */
46e264ebf4SJohannes Thumshirn #define MEN_Z135_ALL_IRQS (MEN_Z135_IER_RXCIEN		\
47e264ebf4SJohannes Thumshirn 				| MEN_Z135_IER_RLSIEN	\
48e264ebf4SJohannes Thumshirn 				| MEN_Z135_IER_MSIEN	\
49e264ebf4SJohannes Thumshirn 				| MEN_Z135_IER_TXCIEN)
50e264ebf4SJohannes Thumshirn 
51e264ebf4SJohannes Thumshirn #define MEN_Z135_MCR_DTR	BIT(24)
52e264ebf4SJohannes Thumshirn #define MEN_Z135_MCR_RTS	BIT(25)
53e264ebf4SJohannes Thumshirn #define MEN_Z135_MCR_OUT1	BIT(26)
54e264ebf4SJohannes Thumshirn #define MEN_Z135_MCR_OUT2	BIT(27)
55e264ebf4SJohannes Thumshirn #define MEN_Z135_MCR_LOOP	BIT(28)
56e264ebf4SJohannes Thumshirn #define MEN_Z135_MCR_RCFC	BIT(29)
57e264ebf4SJohannes Thumshirn 
58e264ebf4SJohannes Thumshirn #define MEN_Z135_MSR_DCTS	BIT(0)
59e264ebf4SJohannes Thumshirn #define MEN_Z135_MSR_DDSR	BIT(1)
60e264ebf4SJohannes Thumshirn #define MEN_Z135_MSR_DRI	BIT(2)
61e264ebf4SJohannes Thumshirn #define MEN_Z135_MSR_DDCD	BIT(3)
62e264ebf4SJohannes Thumshirn #define MEN_Z135_MSR_CTS	BIT(4)
63e264ebf4SJohannes Thumshirn #define MEN_Z135_MSR_DSR	BIT(5)
64e264ebf4SJohannes Thumshirn #define MEN_Z135_MSR_RI		BIT(6)
65e264ebf4SJohannes Thumshirn #define MEN_Z135_MSR_DCD	BIT(7)
66e264ebf4SJohannes Thumshirn 
67e264ebf4SJohannes Thumshirn #define MEN_Z135_LCR_SHIFT 8	/* LCR shift mask */
68e264ebf4SJohannes Thumshirn 
69e264ebf4SJohannes Thumshirn #define MEN_Z135_WL5 0		/* CS5 */
70e264ebf4SJohannes Thumshirn #define MEN_Z135_WL6 1		/* CS6 */
71e264ebf4SJohannes Thumshirn #define MEN_Z135_WL7 2		/* CS7 */
72e264ebf4SJohannes Thumshirn #define MEN_Z135_WL8 3		/* CS8 */
73e264ebf4SJohannes Thumshirn 
74e264ebf4SJohannes Thumshirn #define MEN_Z135_STB_SHIFT 2	/* Stopbits */
75e264ebf4SJohannes Thumshirn #define MEN_Z135_NSTB1 0
76e264ebf4SJohannes Thumshirn #define MEN_Z135_NSTB2 1
77e264ebf4SJohannes Thumshirn 
78e264ebf4SJohannes Thumshirn #define MEN_Z135_PEN_SHIFT 3	/* Parity enable */
79e264ebf4SJohannes Thumshirn #define MEN_Z135_PAR_DIS 0
80e264ebf4SJohannes Thumshirn #define MEN_Z135_PAR_ENA 1
81e264ebf4SJohannes Thumshirn 
82e264ebf4SJohannes Thumshirn #define MEN_Z135_PTY_SHIFT 4	/* Parity type */
83e264ebf4SJohannes Thumshirn #define MEN_Z135_PTY_ODD 0
84e264ebf4SJohannes Thumshirn #define MEN_Z135_PTY_EVN 1
85e264ebf4SJohannes Thumshirn 
86e264ebf4SJohannes Thumshirn #define MEN_Z135_LSR_DR BIT(0)
87e264ebf4SJohannes Thumshirn #define MEN_Z135_LSR_OE BIT(1)
88e264ebf4SJohannes Thumshirn #define MEN_Z135_LSR_PE BIT(2)
89e264ebf4SJohannes Thumshirn #define MEN_Z135_LSR_FE BIT(3)
90e264ebf4SJohannes Thumshirn #define MEN_Z135_LSR_BI BIT(4)
91e264ebf4SJohannes Thumshirn #define MEN_Z135_LSR_THEP BIT(5)
92e264ebf4SJohannes Thumshirn #define MEN_Z135_LSR_TEXP BIT(6)
93e264ebf4SJohannes Thumshirn #define MEN_Z135_LSR_RXFIFOERR BIT(7)
94e264ebf4SJohannes Thumshirn 
95*01ba8d6aSJohannes Thumshirn #define MEN_Z135_IRQ_ID_RLS BIT(0)
96*01ba8d6aSJohannes Thumshirn #define MEN_Z135_IRQ_ID_RDA BIT(1)
97*01ba8d6aSJohannes Thumshirn #define MEN_Z135_IRQ_ID_CTI BIT(2)
98*01ba8d6aSJohannes Thumshirn #define MEN_Z135_IRQ_ID_TSA BIT(3)
99*01ba8d6aSJohannes Thumshirn #define MEN_Z135_IRQ_ID_MST BIT(4)
100e264ebf4SJohannes Thumshirn 
101e264ebf4SJohannes Thumshirn #define LCR(x) (((x) >> MEN_Z135_LCR_SHIFT) & 0xff)
102e264ebf4SJohannes Thumshirn 
103e264ebf4SJohannes Thumshirn #define BYTES_TO_ALIGN(x) ((x) & 0x3)
104e264ebf4SJohannes Thumshirn 
105e264ebf4SJohannes Thumshirn static int line;
106e264ebf4SJohannes Thumshirn 
107e264ebf4SJohannes Thumshirn static int txlvl = 5;
108e264ebf4SJohannes Thumshirn module_param(txlvl, int, S_IRUGO);
109e264ebf4SJohannes Thumshirn MODULE_PARM_DESC(txlvl, "TX IRQ trigger level 0-7, default 5 (128 byte)");
110e264ebf4SJohannes Thumshirn 
111e264ebf4SJohannes Thumshirn static int rxlvl = 6;
112e264ebf4SJohannes Thumshirn module_param(rxlvl, int, S_IRUGO);
113e264ebf4SJohannes Thumshirn MODULE_PARM_DESC(rxlvl, "RX IRQ trigger level 0-7, default 6 (256 byte)");
114e264ebf4SJohannes Thumshirn 
115e264ebf4SJohannes Thumshirn static int align;
116e264ebf4SJohannes Thumshirn module_param(align, int, S_IRUGO);
117e264ebf4SJohannes Thumshirn MODULE_PARM_DESC(align, "Keep hardware FIFO write pointer aligned, default 0");
118e264ebf4SJohannes Thumshirn 
119*01ba8d6aSJohannes Thumshirn static uint rx_timeout;
120*01ba8d6aSJohannes Thumshirn module_param(rx_timeout, uint, S_IRUGO);
121*01ba8d6aSJohannes Thumshirn MODULE_PARM_DESC(rx_timeout, "RX timeout. "
122*01ba8d6aSJohannes Thumshirn 		"Timeout in seconds = (timeout_reg * baud_reg * 4) / freq_reg");
123*01ba8d6aSJohannes Thumshirn 
124e264ebf4SJohannes Thumshirn struct men_z135_port {
125e264ebf4SJohannes Thumshirn 	struct uart_port port;
126e264ebf4SJohannes Thumshirn 	struct mcb_device *mdev;
127e264ebf4SJohannes Thumshirn 	unsigned char *rxbuf;
128e264ebf4SJohannes Thumshirn 	u32 stat_reg;
129e264ebf4SJohannes Thumshirn 	spinlock_t lock;
130*01ba8d6aSJohannes Thumshirn 	bool automode;
131e264ebf4SJohannes Thumshirn };
132e264ebf4SJohannes Thumshirn #define to_men_z135(port) container_of((port), struct men_z135_port, port)
133e264ebf4SJohannes Thumshirn 
134e264ebf4SJohannes Thumshirn /**
135e264ebf4SJohannes Thumshirn  * men_z135_reg_set() - Set value in register
136e264ebf4SJohannes Thumshirn  * @uart: The UART port
137e264ebf4SJohannes Thumshirn  * @addr: Register address
138e264ebf4SJohannes Thumshirn  * @val: value to set
139e264ebf4SJohannes Thumshirn  */
140e264ebf4SJohannes Thumshirn static inline void men_z135_reg_set(struct men_z135_port *uart,
141e264ebf4SJohannes Thumshirn 				u32 addr, u32 val)
142e264ebf4SJohannes Thumshirn {
143e264ebf4SJohannes Thumshirn 	struct uart_port *port = &uart->port;
144e264ebf4SJohannes Thumshirn 	unsigned long flags;
145e264ebf4SJohannes Thumshirn 	u32 reg;
146e264ebf4SJohannes Thumshirn 
147e264ebf4SJohannes Thumshirn 	spin_lock_irqsave(&uart->lock, flags);
148e264ebf4SJohannes Thumshirn 
149e264ebf4SJohannes Thumshirn 	reg = ioread32(port->membase + addr);
150e264ebf4SJohannes Thumshirn 	reg |= val;
151e264ebf4SJohannes Thumshirn 	iowrite32(reg, port->membase + addr);
152e264ebf4SJohannes Thumshirn 
153e264ebf4SJohannes Thumshirn 	spin_unlock_irqrestore(&uart->lock, flags);
154e264ebf4SJohannes Thumshirn }
155e264ebf4SJohannes Thumshirn 
156e264ebf4SJohannes Thumshirn /**
157e264ebf4SJohannes Thumshirn  * men_z135_reg_clr() - Unset value in register
158e264ebf4SJohannes Thumshirn  * @uart: The UART port
159e264ebf4SJohannes Thumshirn  * @addr: Register address
160e264ebf4SJohannes Thumshirn  * @val: value to clear
161e264ebf4SJohannes Thumshirn  */
162e264ebf4SJohannes Thumshirn static inline void men_z135_reg_clr(struct men_z135_port *uart,
163e264ebf4SJohannes Thumshirn 				u32 addr, u32 val)
164e264ebf4SJohannes Thumshirn {
165e264ebf4SJohannes Thumshirn 	struct uart_port *port = &uart->port;
166e264ebf4SJohannes Thumshirn 	unsigned long flags;
167e264ebf4SJohannes Thumshirn 	u32 reg;
168e264ebf4SJohannes Thumshirn 
169e264ebf4SJohannes Thumshirn 	spin_lock_irqsave(&uart->lock, flags);
170e264ebf4SJohannes Thumshirn 
171e264ebf4SJohannes Thumshirn 	reg = ioread32(port->membase + addr);
172e264ebf4SJohannes Thumshirn 	reg &= ~val;
173e264ebf4SJohannes Thumshirn 	iowrite32(reg, port->membase + addr);
174e264ebf4SJohannes Thumshirn 
175e264ebf4SJohannes Thumshirn 	spin_unlock_irqrestore(&uart->lock, flags);
176e264ebf4SJohannes Thumshirn }
177e264ebf4SJohannes Thumshirn 
178e264ebf4SJohannes Thumshirn /**
179e264ebf4SJohannes Thumshirn  * men_z135_handle_modem_status() - Handle change of modem status
180e264ebf4SJohannes Thumshirn  * @port: The UART port
181e264ebf4SJohannes Thumshirn  *
182e264ebf4SJohannes Thumshirn  * Handle change of modem status register. This is done by reading the "delta"
183e264ebf4SJohannes Thumshirn  * versions of DCD (Data Carrier Detect) and CTS (Clear To Send).
184e264ebf4SJohannes Thumshirn  */
185e264ebf4SJohannes Thumshirn static void men_z135_handle_modem_status(struct men_z135_port *uart)
186e264ebf4SJohannes Thumshirn {
187*01ba8d6aSJohannes Thumshirn 	u8 msr;
188*01ba8d6aSJohannes Thumshirn 
189*01ba8d6aSJohannes Thumshirn 	msr = (uart->stat_reg >> 8) & 0xff;
190*01ba8d6aSJohannes Thumshirn 
191*01ba8d6aSJohannes Thumshirn 	if (msr & MEN_Z135_MSR_DDCD)
192e264ebf4SJohannes Thumshirn 		uart_handle_dcd_change(&uart->port,
193*01ba8d6aSJohannes Thumshirn 				msr & MEN_Z135_MSR_DCD);
194*01ba8d6aSJohannes Thumshirn 	if (msr & MEN_Z135_MSR_DCTS)
195e264ebf4SJohannes Thumshirn 		uart_handle_cts_change(&uart->port,
196*01ba8d6aSJohannes Thumshirn 				msr & MEN_Z135_MSR_CTS);
197e264ebf4SJohannes Thumshirn }
198e264ebf4SJohannes Thumshirn 
199e264ebf4SJohannes Thumshirn static void men_z135_handle_lsr(struct men_z135_port *uart)
200e264ebf4SJohannes Thumshirn {
201e264ebf4SJohannes Thumshirn 	struct uart_port *port = &uart->port;
202e264ebf4SJohannes Thumshirn 	u8 lsr;
203e264ebf4SJohannes Thumshirn 
204e264ebf4SJohannes Thumshirn 	lsr = (uart->stat_reg >> 16) & 0xff;
205e264ebf4SJohannes Thumshirn 
206e264ebf4SJohannes Thumshirn 	if (lsr & MEN_Z135_LSR_OE)
207e264ebf4SJohannes Thumshirn 		port->icount.overrun++;
208e264ebf4SJohannes Thumshirn 	if (lsr & MEN_Z135_LSR_PE)
209e264ebf4SJohannes Thumshirn 		port->icount.parity++;
210e264ebf4SJohannes Thumshirn 	if (lsr & MEN_Z135_LSR_FE)
211e264ebf4SJohannes Thumshirn 		port->icount.frame++;
212e264ebf4SJohannes Thumshirn 	if (lsr & MEN_Z135_LSR_BI) {
213e264ebf4SJohannes Thumshirn 		port->icount.brk++;
214e264ebf4SJohannes Thumshirn 		uart_handle_break(port);
215e264ebf4SJohannes Thumshirn 	}
216e264ebf4SJohannes Thumshirn }
217e264ebf4SJohannes Thumshirn 
218e264ebf4SJohannes Thumshirn /**
219e264ebf4SJohannes Thumshirn  * get_rx_fifo_content() - Get the number of bytes in RX FIFO
220e264ebf4SJohannes Thumshirn  * @uart: The UART port
221e264ebf4SJohannes Thumshirn  *
222e264ebf4SJohannes Thumshirn  * Read RXC register from hardware and return current FIFO fill size.
223e264ebf4SJohannes Thumshirn  */
224e264ebf4SJohannes Thumshirn static u16 get_rx_fifo_content(struct men_z135_port *uart)
225e264ebf4SJohannes Thumshirn {
226e264ebf4SJohannes Thumshirn 	struct uart_port *port = &uart->port;
227e264ebf4SJohannes Thumshirn 	u32 stat_reg;
228e264ebf4SJohannes Thumshirn 	u16 rxc;
229e264ebf4SJohannes Thumshirn 	u8 rxc_lo;
230e264ebf4SJohannes Thumshirn 	u8 rxc_hi;
231e264ebf4SJohannes Thumshirn 
232e264ebf4SJohannes Thumshirn 	stat_reg = ioread32(port->membase + MEN_Z135_STAT_REG);
233e264ebf4SJohannes Thumshirn 	rxc_lo = stat_reg >> 24;
234e264ebf4SJohannes Thumshirn 	rxc_hi = (stat_reg & 0xC0) >> 6;
235e264ebf4SJohannes Thumshirn 
236e264ebf4SJohannes Thumshirn 	rxc = rxc_lo | (rxc_hi << 8);
237e264ebf4SJohannes Thumshirn 
238e264ebf4SJohannes Thumshirn 	return rxc;
239e264ebf4SJohannes Thumshirn }
240e264ebf4SJohannes Thumshirn 
241e264ebf4SJohannes Thumshirn /**
242e264ebf4SJohannes Thumshirn  * men_z135_handle_rx() - RX tasklet routine
243e264ebf4SJohannes Thumshirn  * @arg: Pointer to struct men_z135_port
244e264ebf4SJohannes Thumshirn  *
245e264ebf4SJohannes Thumshirn  * Copy from RX FIFO and acknowledge number of bytes copied.
246e264ebf4SJohannes Thumshirn  */
247e264ebf4SJohannes Thumshirn static void men_z135_handle_rx(struct men_z135_port *uart)
248e264ebf4SJohannes Thumshirn {
249e264ebf4SJohannes Thumshirn 	struct uart_port *port = &uart->port;
250e264ebf4SJohannes Thumshirn 	struct tty_port *tport = &port->state->port;
251e264ebf4SJohannes Thumshirn 	int copied;
252e264ebf4SJohannes Thumshirn 	u16 size;
253e264ebf4SJohannes Thumshirn 	int room;
254e264ebf4SJohannes Thumshirn 
255e264ebf4SJohannes Thumshirn 	size = get_rx_fifo_content(uart);
256e264ebf4SJohannes Thumshirn 
257e264ebf4SJohannes Thumshirn 	if (size == 0)
258e264ebf4SJohannes Thumshirn 		return;
259e264ebf4SJohannes Thumshirn 
260e264ebf4SJohannes Thumshirn 	/* Avoid accidently accessing TX FIFO instead of RX FIFO. Last
261e264ebf4SJohannes Thumshirn 	 * longword in RX FIFO cannot be read.(0x004-0x3FF)
262e264ebf4SJohannes Thumshirn 	 */
263e264ebf4SJohannes Thumshirn 	if (size > MEN_Z135_FIFO_WATERMARK)
264e264ebf4SJohannes Thumshirn 		size = MEN_Z135_FIFO_WATERMARK;
265e264ebf4SJohannes Thumshirn 
266e264ebf4SJohannes Thumshirn 	room = tty_buffer_request_room(tport, size);
267e264ebf4SJohannes Thumshirn 	if (room != size)
268e264ebf4SJohannes Thumshirn 		dev_warn(&uart->mdev->dev,
269e264ebf4SJohannes Thumshirn 			"Not enough room in flip buffer, truncating to %d\n",
270e264ebf4SJohannes Thumshirn 			room);
271e264ebf4SJohannes Thumshirn 
272e264ebf4SJohannes Thumshirn 	if (room == 0)
273e264ebf4SJohannes Thumshirn 		return;
274e264ebf4SJohannes Thumshirn 
275e264ebf4SJohannes Thumshirn 	memcpy_fromio(uart->rxbuf, port->membase + MEN_Z135_RX_RAM, room);
276e264ebf4SJohannes Thumshirn 	/* Be sure to first copy all data and then acknowledge it */
277e264ebf4SJohannes Thumshirn 	mb();
278e264ebf4SJohannes Thumshirn 	iowrite32(room, port->membase +  MEN_Z135_RX_CTRL);
279e264ebf4SJohannes Thumshirn 
280e264ebf4SJohannes Thumshirn 	copied = tty_insert_flip_string(tport, uart->rxbuf, room);
281e264ebf4SJohannes Thumshirn 	if (copied != room)
282e264ebf4SJohannes Thumshirn 		dev_warn(&uart->mdev->dev,
283e264ebf4SJohannes Thumshirn 			"Only copied %d instead of %d bytes\n",
284e264ebf4SJohannes Thumshirn 			copied, room);
285e264ebf4SJohannes Thumshirn 
286e264ebf4SJohannes Thumshirn 	port->icount.rx += copied;
287e264ebf4SJohannes Thumshirn 
288e264ebf4SJohannes Thumshirn 	tty_flip_buffer_push(tport);
289e264ebf4SJohannes Thumshirn 
290e264ebf4SJohannes Thumshirn }
291e264ebf4SJohannes Thumshirn 
292e264ebf4SJohannes Thumshirn /**
293e264ebf4SJohannes Thumshirn  * men_z135_handle_tx() - TX tasklet routine
294e264ebf4SJohannes Thumshirn  * @arg: Pointer to struct men_z135_port
295e264ebf4SJohannes Thumshirn  *
296e264ebf4SJohannes Thumshirn  */
297e264ebf4SJohannes Thumshirn static void men_z135_handle_tx(struct men_z135_port *uart)
298e264ebf4SJohannes Thumshirn {
299e264ebf4SJohannes Thumshirn 	struct uart_port *port = &uart->port;
300e264ebf4SJohannes Thumshirn 	struct circ_buf *xmit = &port->state->xmit;
301e264ebf4SJohannes Thumshirn 	u32 txc;
302e264ebf4SJohannes Thumshirn 	u32 wptr;
303e264ebf4SJohannes Thumshirn 	int qlen;
304e264ebf4SJohannes Thumshirn 	int n;
305e264ebf4SJohannes Thumshirn 	int txfree;
306e264ebf4SJohannes Thumshirn 	int head;
307e264ebf4SJohannes Thumshirn 	int tail;
308e264ebf4SJohannes Thumshirn 	int s;
309e264ebf4SJohannes Thumshirn 
310e264ebf4SJohannes Thumshirn 	if (uart_circ_empty(xmit))
311e264ebf4SJohannes Thumshirn 		goto out;
312e264ebf4SJohannes Thumshirn 
313e264ebf4SJohannes Thumshirn 	if (uart_tx_stopped(port))
314e264ebf4SJohannes Thumshirn 		goto out;
315e264ebf4SJohannes Thumshirn 
316e264ebf4SJohannes Thumshirn 	if (port->x_char)
317e264ebf4SJohannes Thumshirn 		goto out;
318e264ebf4SJohannes Thumshirn 
319e264ebf4SJohannes Thumshirn 	/* calculate bytes to copy */
320e264ebf4SJohannes Thumshirn 	qlen = uart_circ_chars_pending(xmit);
321e264ebf4SJohannes Thumshirn 	if (qlen <= 0)
322e264ebf4SJohannes Thumshirn 		goto out;
323e264ebf4SJohannes Thumshirn 
324e264ebf4SJohannes Thumshirn 	wptr = ioread32(port->membase + MEN_Z135_TX_CTRL);
325e264ebf4SJohannes Thumshirn 	txc = (wptr >> 16) & 0x3ff;
326e264ebf4SJohannes Thumshirn 	wptr &= 0x3ff;
327e264ebf4SJohannes Thumshirn 
328e264ebf4SJohannes Thumshirn 	if (txc > MEN_Z135_FIFO_WATERMARK)
329e264ebf4SJohannes Thumshirn 		txc = MEN_Z135_FIFO_WATERMARK;
330e264ebf4SJohannes Thumshirn 
331e264ebf4SJohannes Thumshirn 	txfree = MEN_Z135_FIFO_WATERMARK - txc;
332e264ebf4SJohannes Thumshirn 	if (txfree <= 0) {
333*01ba8d6aSJohannes Thumshirn 		dev_err(&uart->mdev->dev,
334*01ba8d6aSJohannes Thumshirn 			"Not enough room in TX FIFO have %d, need %d\n",
335e264ebf4SJohannes Thumshirn 			txfree, qlen);
336e264ebf4SJohannes Thumshirn 		goto irq_en;
337e264ebf4SJohannes Thumshirn 	}
338e264ebf4SJohannes Thumshirn 
339e264ebf4SJohannes Thumshirn 	/* if we're not aligned, it's better to copy only 1 or 2 bytes and
340e264ebf4SJohannes Thumshirn 	 * then the rest.
341e264ebf4SJohannes Thumshirn 	 */
342e264ebf4SJohannes Thumshirn 	if (align && qlen >= 3 && BYTES_TO_ALIGN(wptr))
343e264ebf4SJohannes Thumshirn 		n = 4 - BYTES_TO_ALIGN(wptr);
344e264ebf4SJohannes Thumshirn 	else if (qlen > txfree)
345e264ebf4SJohannes Thumshirn 		n = txfree;
346e264ebf4SJohannes Thumshirn 	else
347e264ebf4SJohannes Thumshirn 		n = qlen;
348e264ebf4SJohannes Thumshirn 
349e264ebf4SJohannes Thumshirn 	if (n <= 0)
350e264ebf4SJohannes Thumshirn 		goto irq_en;
351e264ebf4SJohannes Thumshirn 
352e264ebf4SJohannes Thumshirn 	head = xmit->head & (UART_XMIT_SIZE - 1);
353e264ebf4SJohannes Thumshirn 	tail = xmit->tail & (UART_XMIT_SIZE - 1);
354e264ebf4SJohannes Thumshirn 
355e264ebf4SJohannes Thumshirn 	s = ((head >= tail) ? head : UART_XMIT_SIZE) - tail;
356e264ebf4SJohannes Thumshirn 	n = min(n, s);
357e264ebf4SJohannes Thumshirn 
358e264ebf4SJohannes Thumshirn 	memcpy_toio(port->membase + MEN_Z135_TX_RAM, &xmit->buf[xmit->tail], n);
359e264ebf4SJohannes Thumshirn 	xmit->tail = (xmit->tail + n) & (UART_XMIT_SIZE - 1);
360e264ebf4SJohannes Thumshirn 	mmiowb();
361e264ebf4SJohannes Thumshirn 
362e264ebf4SJohannes Thumshirn 	iowrite32(n & 0x3ff, port->membase + MEN_Z135_TX_CTRL);
363e264ebf4SJohannes Thumshirn 
364e264ebf4SJohannes Thumshirn 	port->icount.tx += n;
365e264ebf4SJohannes Thumshirn 
366a9977620SJohannes Thumshirn 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
367a9977620SJohannes Thumshirn 		uart_write_wakeup(port);
368a9977620SJohannes Thumshirn 
369e264ebf4SJohannes Thumshirn irq_en:
370e264ebf4SJohannes Thumshirn 	if (!uart_circ_empty(xmit))
371e264ebf4SJohannes Thumshirn 		men_z135_reg_set(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
372e264ebf4SJohannes Thumshirn 	else
373e264ebf4SJohannes Thumshirn 		men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
374e264ebf4SJohannes Thumshirn 
375e264ebf4SJohannes Thumshirn out:
376e264ebf4SJohannes Thumshirn 	return;
377e264ebf4SJohannes Thumshirn 
378e264ebf4SJohannes Thumshirn }
379e264ebf4SJohannes Thumshirn 
380e264ebf4SJohannes Thumshirn /**
381e264ebf4SJohannes Thumshirn  * men_z135_intr() - Handle legacy IRQs
382e264ebf4SJohannes Thumshirn  * @irq: The IRQ number
383e264ebf4SJohannes Thumshirn  * @data: Pointer to UART port
384e264ebf4SJohannes Thumshirn  *
385*01ba8d6aSJohannes Thumshirn  * Check IIR register to find the cause of the interrupt and handle it.
386*01ba8d6aSJohannes Thumshirn  * It is possible that multiple interrupts reason bits are set and reading
387*01ba8d6aSJohannes Thumshirn  * the IIR is a destructive read, so we always need to check for all possible
388*01ba8d6aSJohannes Thumshirn  * interrupts and handle them.
389e264ebf4SJohannes Thumshirn  */
390e264ebf4SJohannes Thumshirn static irqreturn_t men_z135_intr(int irq, void *data)
391e264ebf4SJohannes Thumshirn {
392e264ebf4SJohannes Thumshirn 	struct men_z135_port *uart = (struct men_z135_port *)data;
393e264ebf4SJohannes Thumshirn 	struct uart_port *port = &uart->port;
394*01ba8d6aSJohannes Thumshirn 	bool handled = false;
395*01ba8d6aSJohannes Thumshirn 	unsigned long flags;
396e264ebf4SJohannes Thumshirn 	int irq_id;
397e264ebf4SJohannes Thumshirn 
398e264ebf4SJohannes Thumshirn 	uart->stat_reg = ioread32(port->membase + MEN_Z135_STAT_REG);
399e264ebf4SJohannes Thumshirn 	irq_id = IRQ_ID(uart->stat_reg);
400*01ba8d6aSJohannes Thumshirn 
401*01ba8d6aSJohannes Thumshirn 	if (!irq_id)
402*01ba8d6aSJohannes Thumshirn 		goto out;
403*01ba8d6aSJohannes Thumshirn 
404*01ba8d6aSJohannes Thumshirn 	spin_lock_irqsave(&port->lock, flags);
405*01ba8d6aSJohannes Thumshirn 	/* It's save to write to IIR[7:6] RXC[9:8] */
406*01ba8d6aSJohannes Thumshirn 	iowrite8(irq_id, port->membase + MEN_Z135_STAT_REG);
407*01ba8d6aSJohannes Thumshirn 
408*01ba8d6aSJohannes Thumshirn 	if (irq_id & MEN_Z135_IRQ_ID_RLS) {
409e264ebf4SJohannes Thumshirn 		men_z135_handle_lsr(uart);
410*01ba8d6aSJohannes Thumshirn 		handled = true;
411e264ebf4SJohannes Thumshirn 	}
412e264ebf4SJohannes Thumshirn 
413*01ba8d6aSJohannes Thumshirn 	if (irq_id & (MEN_Z135_IRQ_ID_RDA | MEN_Z135_IRQ_ID_CTI)) {
414*01ba8d6aSJohannes Thumshirn 		if (irq_id & MEN_Z135_IRQ_ID_CTI)
415*01ba8d6aSJohannes Thumshirn 			dev_dbg(&uart->mdev->dev, "Character Timeout Indication\n");
416*01ba8d6aSJohannes Thumshirn 		men_z135_handle_rx(uart);
417*01ba8d6aSJohannes Thumshirn 		handled = true;
418*01ba8d6aSJohannes Thumshirn 	}
419*01ba8d6aSJohannes Thumshirn 
420*01ba8d6aSJohannes Thumshirn 	if (irq_id & MEN_Z135_IRQ_ID_TSA) {
421*01ba8d6aSJohannes Thumshirn 		men_z135_handle_tx(uart);
422*01ba8d6aSJohannes Thumshirn 		handled = true;
423*01ba8d6aSJohannes Thumshirn 	}
424*01ba8d6aSJohannes Thumshirn 
425*01ba8d6aSJohannes Thumshirn 	if (irq_id & MEN_Z135_IRQ_ID_MST) {
426*01ba8d6aSJohannes Thumshirn 		men_z135_handle_modem_status(uart);
427*01ba8d6aSJohannes Thumshirn 		handled = true;
428*01ba8d6aSJohannes Thumshirn 	}
429*01ba8d6aSJohannes Thumshirn 
430*01ba8d6aSJohannes Thumshirn 	spin_unlock_irqrestore(&port->lock, flags);
431*01ba8d6aSJohannes Thumshirn out:
432*01ba8d6aSJohannes Thumshirn 	return IRQ_RETVAL(handled);
433e264ebf4SJohannes Thumshirn }
434e264ebf4SJohannes Thumshirn 
435e264ebf4SJohannes Thumshirn /**
436e264ebf4SJohannes Thumshirn  * men_z135_request_irq() - Request IRQ for 16z135 core
437e264ebf4SJohannes Thumshirn  * @uart: z135 private uart port structure
438e264ebf4SJohannes Thumshirn  *
439e264ebf4SJohannes Thumshirn  * Request an IRQ for 16z135 to use. First try using MSI, if it fails
440e264ebf4SJohannes Thumshirn  * fall back to using legacy interrupts.
441e264ebf4SJohannes Thumshirn  */
442e264ebf4SJohannes Thumshirn static int men_z135_request_irq(struct men_z135_port *uart)
443e264ebf4SJohannes Thumshirn {
444e264ebf4SJohannes Thumshirn 	struct device *dev = &uart->mdev->dev;
445e264ebf4SJohannes Thumshirn 	struct uart_port *port = &uart->port;
446e264ebf4SJohannes Thumshirn 	int err = 0;
447e264ebf4SJohannes Thumshirn 
448e264ebf4SJohannes Thumshirn 	err = request_irq(port->irq, men_z135_intr, IRQF_SHARED,
449e264ebf4SJohannes Thumshirn 			"men_z135_intr", uart);
450e264ebf4SJohannes Thumshirn 	if (err)
451e264ebf4SJohannes Thumshirn 		dev_err(dev, "Error %d getting interrupt\n", err);
452e264ebf4SJohannes Thumshirn 
453e264ebf4SJohannes Thumshirn 	return err;
454e264ebf4SJohannes Thumshirn }
455e264ebf4SJohannes Thumshirn 
456e264ebf4SJohannes Thumshirn /**
457e264ebf4SJohannes Thumshirn  * men_z135_tx_empty() - Handle tx_empty call
458e264ebf4SJohannes Thumshirn  * @port: The UART port
459e264ebf4SJohannes Thumshirn  *
460e264ebf4SJohannes Thumshirn  * This function tests whether the TX FIFO and shifter for the port
461e264ebf4SJohannes Thumshirn  * described by @port is empty.
462e264ebf4SJohannes Thumshirn  */
463e264ebf4SJohannes Thumshirn static unsigned int men_z135_tx_empty(struct uart_port *port)
464e264ebf4SJohannes Thumshirn {
465e264ebf4SJohannes Thumshirn 	u32 wptr;
466e264ebf4SJohannes Thumshirn 	u16 txc;
467e264ebf4SJohannes Thumshirn 
468e264ebf4SJohannes Thumshirn 	wptr = ioread32(port->membase + MEN_Z135_TX_CTRL);
469e264ebf4SJohannes Thumshirn 	txc = (wptr >> 16) & 0x3ff;
470e264ebf4SJohannes Thumshirn 
471e264ebf4SJohannes Thumshirn 	if (txc == 0)
472e264ebf4SJohannes Thumshirn 		return TIOCSER_TEMT;
473e264ebf4SJohannes Thumshirn 	else
474e264ebf4SJohannes Thumshirn 		return 0;
475e264ebf4SJohannes Thumshirn }
476e264ebf4SJohannes Thumshirn 
477e264ebf4SJohannes Thumshirn /**
478e264ebf4SJohannes Thumshirn  * men_z135_set_mctrl() - Set modem control lines
479e264ebf4SJohannes Thumshirn  * @port: The UART port
480e264ebf4SJohannes Thumshirn  * @mctrl: The modem control lines
481e264ebf4SJohannes Thumshirn  *
482e264ebf4SJohannes Thumshirn  * This function sets the modem control lines for a port described by @port
483e264ebf4SJohannes Thumshirn  * to the state described by @mctrl
484e264ebf4SJohannes Thumshirn  */
485e264ebf4SJohannes Thumshirn static void men_z135_set_mctrl(struct uart_port *port, unsigned int mctrl)
486e264ebf4SJohannes Thumshirn {
487*01ba8d6aSJohannes Thumshirn 	u32 old;
488*01ba8d6aSJohannes Thumshirn 	u32 conf_reg;
489e264ebf4SJohannes Thumshirn 
490*01ba8d6aSJohannes Thumshirn 	conf_reg = old = ioread32(port->membase + MEN_Z135_CONF_REG);
491e264ebf4SJohannes Thumshirn 	if (mctrl & TIOCM_RTS)
492e264ebf4SJohannes Thumshirn 		conf_reg |= MEN_Z135_MCR_RTS;
493*01ba8d6aSJohannes Thumshirn 	else
494*01ba8d6aSJohannes Thumshirn 		conf_reg &= ~MEN_Z135_MCR_RTS;
495*01ba8d6aSJohannes Thumshirn 
496e264ebf4SJohannes Thumshirn 	if (mctrl & TIOCM_DTR)
497e264ebf4SJohannes Thumshirn 		conf_reg |= MEN_Z135_MCR_DTR;
498*01ba8d6aSJohannes Thumshirn 	else
499*01ba8d6aSJohannes Thumshirn 		conf_reg &= ~MEN_Z135_MCR_DTR;
500*01ba8d6aSJohannes Thumshirn 
501e264ebf4SJohannes Thumshirn 	if (mctrl & TIOCM_OUT1)
502e264ebf4SJohannes Thumshirn 		conf_reg |= MEN_Z135_MCR_OUT1;
503*01ba8d6aSJohannes Thumshirn 	else
504*01ba8d6aSJohannes Thumshirn 		conf_reg &= ~MEN_Z135_MCR_OUT1;
505*01ba8d6aSJohannes Thumshirn 
506e264ebf4SJohannes Thumshirn 	if (mctrl & TIOCM_OUT2)
507e264ebf4SJohannes Thumshirn 		conf_reg |= MEN_Z135_MCR_OUT2;
508*01ba8d6aSJohannes Thumshirn 	else
509*01ba8d6aSJohannes Thumshirn 		conf_reg &= ~MEN_Z135_MCR_OUT2;
510*01ba8d6aSJohannes Thumshirn 
511e264ebf4SJohannes Thumshirn 	if (mctrl & TIOCM_LOOP)
512e264ebf4SJohannes Thumshirn 		conf_reg |= MEN_Z135_MCR_LOOP;
513*01ba8d6aSJohannes Thumshirn 	else
514*01ba8d6aSJohannes Thumshirn 		conf_reg &= ~MEN_Z135_MCR_LOOP;
515e264ebf4SJohannes Thumshirn 
516*01ba8d6aSJohannes Thumshirn 	if (conf_reg != old)
517*01ba8d6aSJohannes Thumshirn 		iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
518e264ebf4SJohannes Thumshirn }
519e264ebf4SJohannes Thumshirn 
520e264ebf4SJohannes Thumshirn /**
521e264ebf4SJohannes Thumshirn  * men_z135_get_mctrl() - Get modem control lines
522e264ebf4SJohannes Thumshirn  * @port: The UART port
523e264ebf4SJohannes Thumshirn  *
524e264ebf4SJohannes Thumshirn  * Retruns the current state of modem control inputs.
525e264ebf4SJohannes Thumshirn  */
526e264ebf4SJohannes Thumshirn static unsigned int men_z135_get_mctrl(struct uart_port *port)
527e264ebf4SJohannes Thumshirn {
528e264ebf4SJohannes Thumshirn 	unsigned int mctrl = 0;
529e264ebf4SJohannes Thumshirn 	u8 msr;
530e264ebf4SJohannes Thumshirn 
531*01ba8d6aSJohannes Thumshirn 	msr = ioread8(port->membase + MEN_Z135_STAT_REG + 1);
532e264ebf4SJohannes Thumshirn 
533e264ebf4SJohannes Thumshirn 	if (msr & MEN_Z135_MSR_CTS)
534e264ebf4SJohannes Thumshirn 		mctrl |= TIOCM_CTS;
535e264ebf4SJohannes Thumshirn 	if (msr & MEN_Z135_MSR_DSR)
536e264ebf4SJohannes Thumshirn 		mctrl |= TIOCM_DSR;
537e264ebf4SJohannes Thumshirn 	if (msr & MEN_Z135_MSR_RI)
538e264ebf4SJohannes Thumshirn 		mctrl |= TIOCM_RI;
539e264ebf4SJohannes Thumshirn 	if (msr & MEN_Z135_MSR_DCD)
540e264ebf4SJohannes Thumshirn 		mctrl |= TIOCM_CAR;
541e264ebf4SJohannes Thumshirn 
542e264ebf4SJohannes Thumshirn 	return mctrl;
543e264ebf4SJohannes Thumshirn }
544e264ebf4SJohannes Thumshirn 
545e264ebf4SJohannes Thumshirn /**
546e264ebf4SJohannes Thumshirn  * men_z135_stop_tx() - Stop transmitting characters
547e264ebf4SJohannes Thumshirn  * @port: The UART port
548e264ebf4SJohannes Thumshirn  *
549e264ebf4SJohannes Thumshirn  * Stop transmitting characters. This might be due to CTS line becomming
550e264ebf4SJohannes Thumshirn  * inactive or the tty layer indicating we want to stop transmission due to
551e264ebf4SJohannes Thumshirn  * an XOFF character.
552e264ebf4SJohannes Thumshirn  */
553e264ebf4SJohannes Thumshirn static void men_z135_stop_tx(struct uart_port *port)
554e264ebf4SJohannes Thumshirn {
555e264ebf4SJohannes Thumshirn 	struct men_z135_port *uart = to_men_z135(port);
556e264ebf4SJohannes Thumshirn 
557e264ebf4SJohannes Thumshirn 	men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
558e264ebf4SJohannes Thumshirn }
559e264ebf4SJohannes Thumshirn 
560*01ba8d6aSJohannes Thumshirn /*
561*01ba8d6aSJohannes Thumshirn  * men_z135_disable_ms() - Disable Modem Status
562*01ba8d6aSJohannes Thumshirn  * port: The UART port
563*01ba8d6aSJohannes Thumshirn  *
564*01ba8d6aSJohannes Thumshirn  * Enable Modem Status IRQ.
565*01ba8d6aSJohannes Thumshirn  */
566*01ba8d6aSJohannes Thumshirn static void men_z135_disable_ms(struct uart_port *port)
567*01ba8d6aSJohannes Thumshirn {
568*01ba8d6aSJohannes Thumshirn 	struct men_z135_port *uart = to_men_z135(port);
569*01ba8d6aSJohannes Thumshirn 
570*01ba8d6aSJohannes Thumshirn 	men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_MSIEN);
571*01ba8d6aSJohannes Thumshirn }
572*01ba8d6aSJohannes Thumshirn 
573e264ebf4SJohannes Thumshirn /**
574e264ebf4SJohannes Thumshirn  * men_z135_start_tx() - Start transmitting characters
575e264ebf4SJohannes Thumshirn  * @port: The UART port
576e264ebf4SJohannes Thumshirn  *
577e264ebf4SJohannes Thumshirn  * Start transmitting character. This actually doesn't transmit anything, but
578e264ebf4SJohannes Thumshirn  * fires off the TX tasklet.
579e264ebf4SJohannes Thumshirn  */
580e264ebf4SJohannes Thumshirn static void men_z135_start_tx(struct uart_port *port)
581e264ebf4SJohannes Thumshirn {
582e264ebf4SJohannes Thumshirn 	struct men_z135_port *uart = to_men_z135(port);
583e264ebf4SJohannes Thumshirn 
584*01ba8d6aSJohannes Thumshirn 	if (uart->automode)
585*01ba8d6aSJohannes Thumshirn 		men_z135_disable_ms(port);
586*01ba8d6aSJohannes Thumshirn 
587e264ebf4SJohannes Thumshirn 	men_z135_handle_tx(uart);
588e264ebf4SJohannes Thumshirn }
589e264ebf4SJohannes Thumshirn 
590e264ebf4SJohannes Thumshirn /**
591e264ebf4SJohannes Thumshirn  * men_z135_stop_rx() - Stop receiving characters
592e264ebf4SJohannes Thumshirn  * @port: The UART port
593e264ebf4SJohannes Thumshirn  *
594e264ebf4SJohannes Thumshirn  * Stop receiving characters; the port is in the process of being closed.
595e264ebf4SJohannes Thumshirn  */
596e264ebf4SJohannes Thumshirn static void men_z135_stop_rx(struct uart_port *port)
597e264ebf4SJohannes Thumshirn {
598e264ebf4SJohannes Thumshirn 	struct men_z135_port *uart = to_men_z135(port);
599e264ebf4SJohannes Thumshirn 
600e264ebf4SJohannes Thumshirn 	men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_RXCIEN);
601e264ebf4SJohannes Thumshirn }
602e264ebf4SJohannes Thumshirn 
603e264ebf4SJohannes Thumshirn /**
604e264ebf4SJohannes Thumshirn  * men_z135_enable_ms() - Enable Modem Status
605e264ebf4SJohannes Thumshirn  * port:
606e264ebf4SJohannes Thumshirn  *
607e264ebf4SJohannes Thumshirn  * Enable Modem Status IRQ.
608e264ebf4SJohannes Thumshirn  */
609e264ebf4SJohannes Thumshirn static void men_z135_enable_ms(struct uart_port *port)
610e264ebf4SJohannes Thumshirn {
611e264ebf4SJohannes Thumshirn 	struct men_z135_port *uart = to_men_z135(port);
612e264ebf4SJohannes Thumshirn 
613e264ebf4SJohannes Thumshirn 	men_z135_reg_set(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_MSIEN);
614e264ebf4SJohannes Thumshirn }
615e264ebf4SJohannes Thumshirn 
616e264ebf4SJohannes Thumshirn static int men_z135_startup(struct uart_port *port)
617e264ebf4SJohannes Thumshirn {
618e264ebf4SJohannes Thumshirn 	struct men_z135_port *uart = to_men_z135(port);
619e264ebf4SJohannes Thumshirn 	int err;
620e264ebf4SJohannes Thumshirn 	u32 conf_reg = 0;
621e264ebf4SJohannes Thumshirn 
622e264ebf4SJohannes Thumshirn 	err = men_z135_request_irq(uart);
623e264ebf4SJohannes Thumshirn 	if (err)
624e264ebf4SJohannes Thumshirn 		return -ENODEV;
625e264ebf4SJohannes Thumshirn 
626e264ebf4SJohannes Thumshirn 	conf_reg = ioread32(port->membase + MEN_Z135_CONF_REG);
627e264ebf4SJohannes Thumshirn 
62810389e66SJohannes Thumshirn 	/* Activate all but TX space available IRQ */
62910389e66SJohannes Thumshirn 	conf_reg |= MEN_Z135_ALL_IRQS & ~MEN_Z135_IER_TXCIEN;
630e264ebf4SJohannes Thumshirn 	conf_reg &= ~(0xff << 16);
631e264ebf4SJohannes Thumshirn 	conf_reg |= (txlvl << 16);
632e264ebf4SJohannes Thumshirn 	conf_reg |= (rxlvl << 20);
633e264ebf4SJohannes Thumshirn 
634e264ebf4SJohannes Thumshirn 	iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
635e264ebf4SJohannes Thumshirn 
636*01ba8d6aSJohannes Thumshirn 	if (rx_timeout)
637*01ba8d6aSJohannes Thumshirn 		iowrite32(rx_timeout, port->membase + MEN_Z135_TIMEOUT);
638*01ba8d6aSJohannes Thumshirn 
639e264ebf4SJohannes Thumshirn 	return 0;
640e264ebf4SJohannes Thumshirn }
641e264ebf4SJohannes Thumshirn 
642e264ebf4SJohannes Thumshirn static void men_z135_shutdown(struct uart_port *port)
643e264ebf4SJohannes Thumshirn {
644e264ebf4SJohannes Thumshirn 	struct men_z135_port *uart = to_men_z135(port);
645e264ebf4SJohannes Thumshirn 	u32 conf_reg = 0;
646e264ebf4SJohannes Thumshirn 
647e264ebf4SJohannes Thumshirn 	conf_reg |= MEN_Z135_ALL_IRQS;
648e264ebf4SJohannes Thumshirn 
649e264ebf4SJohannes Thumshirn 	men_z135_reg_clr(uart, MEN_Z135_CONF_REG, conf_reg);
650e264ebf4SJohannes Thumshirn 
651e264ebf4SJohannes Thumshirn 	free_irq(uart->port.irq, uart);
652e264ebf4SJohannes Thumshirn }
653e264ebf4SJohannes Thumshirn 
654e264ebf4SJohannes Thumshirn static void men_z135_set_termios(struct uart_port *port,
655e264ebf4SJohannes Thumshirn 				struct ktermios *termios,
656e264ebf4SJohannes Thumshirn 				struct ktermios *old)
657e264ebf4SJohannes Thumshirn {
658*01ba8d6aSJohannes Thumshirn 	struct men_z135_port *uart = to_men_z135(port);
659e264ebf4SJohannes Thumshirn 	unsigned int baud;
660e264ebf4SJohannes Thumshirn 	u32 conf_reg;
661e264ebf4SJohannes Thumshirn 	u32 bd_reg;
662e264ebf4SJohannes Thumshirn 	u32 uart_freq;
663e264ebf4SJohannes Thumshirn 	u8 lcr;
664e264ebf4SJohannes Thumshirn 
665e264ebf4SJohannes Thumshirn 	conf_reg = ioread32(port->membase + MEN_Z135_CONF_REG);
666e264ebf4SJohannes Thumshirn 	lcr = LCR(conf_reg);
667e264ebf4SJohannes Thumshirn 
668e264ebf4SJohannes Thumshirn 	/* byte size */
669e264ebf4SJohannes Thumshirn 	switch (termios->c_cflag & CSIZE) {
670e264ebf4SJohannes Thumshirn 	case CS5:
671e264ebf4SJohannes Thumshirn 		lcr |= MEN_Z135_WL5;
672e264ebf4SJohannes Thumshirn 		break;
673e264ebf4SJohannes Thumshirn 	case CS6:
674e264ebf4SJohannes Thumshirn 		lcr |= MEN_Z135_WL6;
675e264ebf4SJohannes Thumshirn 		break;
676e264ebf4SJohannes Thumshirn 	case CS7:
677e264ebf4SJohannes Thumshirn 		lcr |= MEN_Z135_WL7;
678e264ebf4SJohannes Thumshirn 		break;
679e264ebf4SJohannes Thumshirn 	case CS8:
680e264ebf4SJohannes Thumshirn 		lcr |= MEN_Z135_WL8;
681e264ebf4SJohannes Thumshirn 		break;
682e264ebf4SJohannes Thumshirn 	}
683e264ebf4SJohannes Thumshirn 
684e264ebf4SJohannes Thumshirn 	/* stop bits */
685e264ebf4SJohannes Thumshirn 	if (termios->c_cflag & CSTOPB)
686e264ebf4SJohannes Thumshirn 		lcr |= MEN_Z135_NSTB2 << MEN_Z135_STB_SHIFT;
687e264ebf4SJohannes Thumshirn 
688e264ebf4SJohannes Thumshirn 	/* parity */
689e264ebf4SJohannes Thumshirn 	if (termios->c_cflag & PARENB) {
690e264ebf4SJohannes Thumshirn 		lcr |= MEN_Z135_PAR_ENA << MEN_Z135_PEN_SHIFT;
691e264ebf4SJohannes Thumshirn 
692e264ebf4SJohannes Thumshirn 		if (termios->c_cflag & PARODD)
693e264ebf4SJohannes Thumshirn 			lcr |= MEN_Z135_PTY_ODD << MEN_Z135_PTY_SHIFT;
694e264ebf4SJohannes Thumshirn 		else
695e264ebf4SJohannes Thumshirn 			lcr |= MEN_Z135_PTY_EVN << MEN_Z135_PTY_SHIFT;
696e264ebf4SJohannes Thumshirn 	} else
697e264ebf4SJohannes Thumshirn 		lcr |= MEN_Z135_PAR_DIS << MEN_Z135_PEN_SHIFT;
698e264ebf4SJohannes Thumshirn 
699*01ba8d6aSJohannes Thumshirn 	conf_reg |= MEN_Z135_IER_MSIEN;
700*01ba8d6aSJohannes Thumshirn 	if (termios->c_cflag & CRTSCTS) {
701*01ba8d6aSJohannes Thumshirn 		conf_reg |= MEN_Z135_MCR_RCFC;
702*01ba8d6aSJohannes Thumshirn 		uart->automode = true;
703*01ba8d6aSJohannes Thumshirn 		termios->c_cflag &= ~CLOCAL;
704*01ba8d6aSJohannes Thumshirn 	} else {
705*01ba8d6aSJohannes Thumshirn 		conf_reg &= ~MEN_Z135_MCR_RCFC;
706*01ba8d6aSJohannes Thumshirn 		uart->automode = false;
707*01ba8d6aSJohannes Thumshirn 	}
708*01ba8d6aSJohannes Thumshirn 
709e264ebf4SJohannes Thumshirn 	termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
710e264ebf4SJohannes Thumshirn 
711e264ebf4SJohannes Thumshirn 	conf_reg |= lcr << MEN_Z135_LCR_SHIFT;
712e264ebf4SJohannes Thumshirn 	iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
713e264ebf4SJohannes Thumshirn 
714e264ebf4SJohannes Thumshirn 	uart_freq = ioread32(port->membase + MEN_Z135_UART_FREQ);
715e264ebf4SJohannes Thumshirn 	if (uart_freq == 0)
716e264ebf4SJohannes Thumshirn 		uart_freq = MEN_Z135_BASECLK;
717e264ebf4SJohannes Thumshirn 
718e264ebf4SJohannes Thumshirn 	baud = uart_get_baud_rate(port, termios, old, 0, uart_freq / 16);
719e264ebf4SJohannes Thumshirn 
720e264ebf4SJohannes Thumshirn 	spin_lock(&port->lock);
721e264ebf4SJohannes Thumshirn 	if (tty_termios_baud_rate(termios))
722e264ebf4SJohannes Thumshirn 		tty_termios_encode_baud_rate(termios, baud, baud);
723e264ebf4SJohannes Thumshirn 
724e264ebf4SJohannes Thumshirn 	bd_reg = uart_freq / (4 * baud);
725e264ebf4SJohannes Thumshirn 	iowrite32(bd_reg, port->membase + MEN_Z135_BAUD_REG);
726e264ebf4SJohannes Thumshirn 
727e264ebf4SJohannes Thumshirn 	uart_update_timeout(port, termios->c_cflag, baud);
728e264ebf4SJohannes Thumshirn 	spin_unlock(&port->lock);
729e264ebf4SJohannes Thumshirn }
730e264ebf4SJohannes Thumshirn 
731e264ebf4SJohannes Thumshirn static const char *men_z135_type(struct uart_port *port)
732e264ebf4SJohannes Thumshirn {
733e264ebf4SJohannes Thumshirn 	return KBUILD_MODNAME;
734e264ebf4SJohannes Thumshirn }
735e264ebf4SJohannes Thumshirn 
736e264ebf4SJohannes Thumshirn static void men_z135_release_port(struct uart_port *port)
737e264ebf4SJohannes Thumshirn {
738e264ebf4SJohannes Thumshirn 	iounmap(port->membase);
739e264ebf4SJohannes Thumshirn 	port->membase = NULL;
740e264ebf4SJohannes Thumshirn 
741e264ebf4SJohannes Thumshirn 	release_mem_region(port->mapbase, MEN_Z135_MEM_SIZE);
742e264ebf4SJohannes Thumshirn }
743e264ebf4SJohannes Thumshirn 
744e264ebf4SJohannes Thumshirn static int men_z135_request_port(struct uart_port *port)
745e264ebf4SJohannes Thumshirn {
746e264ebf4SJohannes Thumshirn 	int size = MEN_Z135_MEM_SIZE;
747e264ebf4SJohannes Thumshirn 
748e264ebf4SJohannes Thumshirn 	if (!request_mem_region(port->mapbase, size, "men_z135_port"))
749e264ebf4SJohannes Thumshirn 		return -EBUSY;
750e264ebf4SJohannes Thumshirn 
751e264ebf4SJohannes Thumshirn 	port->membase = ioremap(port->mapbase, MEN_Z135_MEM_SIZE);
752e264ebf4SJohannes Thumshirn 	if (port->membase == NULL) {
753e264ebf4SJohannes Thumshirn 		release_mem_region(port->mapbase, MEN_Z135_MEM_SIZE);
754e264ebf4SJohannes Thumshirn 		return -ENOMEM;
755e264ebf4SJohannes Thumshirn 	}
756e264ebf4SJohannes Thumshirn 
757e264ebf4SJohannes Thumshirn 	return 0;
758e264ebf4SJohannes Thumshirn }
759e264ebf4SJohannes Thumshirn 
760e264ebf4SJohannes Thumshirn static void men_z135_config_port(struct uart_port *port, int type)
761e264ebf4SJohannes Thumshirn {
762e264ebf4SJohannes Thumshirn 	port->type = PORT_MEN_Z135;
763e264ebf4SJohannes Thumshirn 	men_z135_request_port(port);
764e264ebf4SJohannes Thumshirn }
765e264ebf4SJohannes Thumshirn 
766e264ebf4SJohannes Thumshirn static int men_z135_verify_port(struct uart_port *port,
767e264ebf4SJohannes Thumshirn 				struct serial_struct *serinfo)
768e264ebf4SJohannes Thumshirn {
769e264ebf4SJohannes Thumshirn 	return -EINVAL;
770e264ebf4SJohannes Thumshirn }
771e264ebf4SJohannes Thumshirn 
772e264ebf4SJohannes Thumshirn static struct uart_ops men_z135_ops = {
773e264ebf4SJohannes Thumshirn 	.tx_empty = men_z135_tx_empty,
774e264ebf4SJohannes Thumshirn 	.set_mctrl = men_z135_set_mctrl,
775e264ebf4SJohannes Thumshirn 	.get_mctrl = men_z135_get_mctrl,
776e264ebf4SJohannes Thumshirn 	.stop_tx = men_z135_stop_tx,
777e264ebf4SJohannes Thumshirn 	.start_tx = men_z135_start_tx,
778e264ebf4SJohannes Thumshirn 	.stop_rx = men_z135_stop_rx,
779e264ebf4SJohannes Thumshirn 	.enable_ms = men_z135_enable_ms,
780e264ebf4SJohannes Thumshirn 	.startup = men_z135_startup,
781e264ebf4SJohannes Thumshirn 	.shutdown = men_z135_shutdown,
782e264ebf4SJohannes Thumshirn 	.set_termios = men_z135_set_termios,
783e264ebf4SJohannes Thumshirn 	.type = men_z135_type,
784e264ebf4SJohannes Thumshirn 	.release_port = men_z135_release_port,
785e264ebf4SJohannes Thumshirn 	.request_port = men_z135_request_port,
786e264ebf4SJohannes Thumshirn 	.config_port = men_z135_config_port,
787e264ebf4SJohannes Thumshirn 	.verify_port = men_z135_verify_port,
788e264ebf4SJohannes Thumshirn };
789e264ebf4SJohannes Thumshirn 
790e264ebf4SJohannes Thumshirn static struct uart_driver men_z135_driver = {
791e264ebf4SJohannes Thumshirn 	.owner = THIS_MODULE,
792e264ebf4SJohannes Thumshirn 	.driver_name = KBUILD_MODNAME,
793e264ebf4SJohannes Thumshirn 	.dev_name = "ttyHSU",
794e264ebf4SJohannes Thumshirn 	.major = 0,
795e264ebf4SJohannes Thumshirn 	.minor = 0,
796e264ebf4SJohannes Thumshirn 	.nr = MEN_Z135_MAX_PORTS,
797e264ebf4SJohannes Thumshirn };
798e264ebf4SJohannes Thumshirn 
799e264ebf4SJohannes Thumshirn /**
800e264ebf4SJohannes Thumshirn  * men_z135_probe() - Probe a z135 instance
801e264ebf4SJohannes Thumshirn  * @mdev: The MCB device
802e264ebf4SJohannes Thumshirn  * @id: The MCB device ID
803e264ebf4SJohannes Thumshirn  *
804e264ebf4SJohannes Thumshirn  * men_z135_probe does the basic setup of hardware resources and registers the
805e264ebf4SJohannes Thumshirn  * new uart port to the tty layer.
806e264ebf4SJohannes Thumshirn  */
807e264ebf4SJohannes Thumshirn static int men_z135_probe(struct mcb_device *mdev,
808e264ebf4SJohannes Thumshirn 			const struct mcb_device_id *id)
809e264ebf4SJohannes Thumshirn {
810e264ebf4SJohannes Thumshirn 	struct men_z135_port *uart;
811e264ebf4SJohannes Thumshirn 	struct resource *mem;
812e264ebf4SJohannes Thumshirn 	struct device *dev;
813e264ebf4SJohannes Thumshirn 	int err;
814e264ebf4SJohannes Thumshirn 
815e264ebf4SJohannes Thumshirn 	dev = &mdev->dev;
816e264ebf4SJohannes Thumshirn 
817e264ebf4SJohannes Thumshirn 	uart = devm_kzalloc(dev, sizeof(struct men_z135_port), GFP_KERNEL);
818e264ebf4SJohannes Thumshirn 	if (!uart)
819e264ebf4SJohannes Thumshirn 		return -ENOMEM;
820e264ebf4SJohannes Thumshirn 
821e264ebf4SJohannes Thumshirn 	uart->rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
822e264ebf4SJohannes Thumshirn 	if (!uart->rxbuf)
823e264ebf4SJohannes Thumshirn 		return -ENOMEM;
824e264ebf4SJohannes Thumshirn 
825e264ebf4SJohannes Thumshirn 	mem = &mdev->mem;
826e264ebf4SJohannes Thumshirn 
827e264ebf4SJohannes Thumshirn 	mcb_set_drvdata(mdev, uart);
828e264ebf4SJohannes Thumshirn 
829e264ebf4SJohannes Thumshirn 	uart->port.uartclk = MEN_Z135_BASECLK * 16;
830e264ebf4SJohannes Thumshirn 	uart->port.fifosize = MEN_Z135_FIFO_SIZE;
831e264ebf4SJohannes Thumshirn 	uart->port.iotype = UPIO_MEM;
832e264ebf4SJohannes Thumshirn 	uart->port.ops = &men_z135_ops;
833e264ebf4SJohannes Thumshirn 	uart->port.irq = mcb_get_irq(mdev);
834e264ebf4SJohannes Thumshirn 	uart->port.iotype = UPIO_MEM;
835e264ebf4SJohannes Thumshirn 	uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
836e264ebf4SJohannes Thumshirn 	uart->port.line = line++;
837e264ebf4SJohannes Thumshirn 	uart->port.dev = dev;
838e264ebf4SJohannes Thumshirn 	uart->port.type = PORT_MEN_Z135;
839e264ebf4SJohannes Thumshirn 	uart->port.mapbase = mem->start;
840e264ebf4SJohannes Thumshirn 	uart->port.membase = NULL;
841e264ebf4SJohannes Thumshirn 	uart->mdev = mdev;
842e264ebf4SJohannes Thumshirn 
843e264ebf4SJohannes Thumshirn 	spin_lock_init(&uart->port.lock);
844e264ebf4SJohannes Thumshirn 	spin_lock_init(&uart->lock);
845e264ebf4SJohannes Thumshirn 
846e264ebf4SJohannes Thumshirn 	err = uart_add_one_port(&men_z135_driver, &uart->port);
847e264ebf4SJohannes Thumshirn 	if (err)
848e264ebf4SJohannes Thumshirn 		goto err;
849e264ebf4SJohannes Thumshirn 
850e264ebf4SJohannes Thumshirn 	return 0;
851e264ebf4SJohannes Thumshirn 
852e264ebf4SJohannes Thumshirn err:
853e264ebf4SJohannes Thumshirn 	free_page((unsigned long) uart->rxbuf);
854e264ebf4SJohannes Thumshirn 	dev_err(dev, "Failed to add UART: %d\n", err);
855e264ebf4SJohannes Thumshirn 
856e264ebf4SJohannes Thumshirn 	return err;
857e264ebf4SJohannes Thumshirn }
858e264ebf4SJohannes Thumshirn 
859e264ebf4SJohannes Thumshirn /**
860e264ebf4SJohannes Thumshirn  * men_z135_remove() - Remove a z135 instance from the system
861e264ebf4SJohannes Thumshirn  *
862e264ebf4SJohannes Thumshirn  * @mdev: The MCB device
863e264ebf4SJohannes Thumshirn  */
864e264ebf4SJohannes Thumshirn static void men_z135_remove(struct mcb_device *mdev)
865e264ebf4SJohannes Thumshirn {
866e264ebf4SJohannes Thumshirn 	struct men_z135_port *uart = mcb_get_drvdata(mdev);
867e264ebf4SJohannes Thumshirn 
868e264ebf4SJohannes Thumshirn 	line--;
869e264ebf4SJohannes Thumshirn 	uart_remove_one_port(&men_z135_driver, &uart->port);
870e264ebf4SJohannes Thumshirn 	free_page((unsigned long) uart->rxbuf);
871e264ebf4SJohannes Thumshirn }
872e264ebf4SJohannes Thumshirn 
873e264ebf4SJohannes Thumshirn static const struct mcb_device_id men_z135_ids[] = {
874e264ebf4SJohannes Thumshirn 	{ .device = 0x87 },
8756b1f40cfSAxel Lin 	{ }
876e264ebf4SJohannes Thumshirn };
877e264ebf4SJohannes Thumshirn MODULE_DEVICE_TABLE(mcb, men_z135_ids);
878e264ebf4SJohannes Thumshirn 
879e264ebf4SJohannes Thumshirn static struct mcb_driver mcb_driver = {
880e264ebf4SJohannes Thumshirn 	.driver = {
881e264ebf4SJohannes Thumshirn 		.name = "z135-uart",
882e264ebf4SJohannes Thumshirn 		.owner = THIS_MODULE,
883e264ebf4SJohannes Thumshirn 	},
884e264ebf4SJohannes Thumshirn 	.probe = men_z135_probe,
885e264ebf4SJohannes Thumshirn 	.remove = men_z135_remove,
886e264ebf4SJohannes Thumshirn 	.id_table = men_z135_ids,
887e264ebf4SJohannes Thumshirn };
888e264ebf4SJohannes Thumshirn 
889e264ebf4SJohannes Thumshirn /**
890e264ebf4SJohannes Thumshirn  * men_z135_init() - Driver Registration Routine
891e264ebf4SJohannes Thumshirn  *
892e264ebf4SJohannes Thumshirn  * men_z135_init is the first routine called when the driver is loaded. All it
893e264ebf4SJohannes Thumshirn  * does is register with the legacy MEN Chameleon subsystem.
894e264ebf4SJohannes Thumshirn  */
895e264ebf4SJohannes Thumshirn static int __init men_z135_init(void)
896e264ebf4SJohannes Thumshirn {
897e264ebf4SJohannes Thumshirn 	int err;
898e264ebf4SJohannes Thumshirn 
899e264ebf4SJohannes Thumshirn 	err = uart_register_driver(&men_z135_driver);
900e264ebf4SJohannes Thumshirn 	if (err) {
901e264ebf4SJohannes Thumshirn 		pr_err("Failed to register UART: %d\n", err);
902e264ebf4SJohannes Thumshirn 		return err;
903e264ebf4SJohannes Thumshirn 	}
904e264ebf4SJohannes Thumshirn 
905e264ebf4SJohannes Thumshirn 	err = mcb_register_driver(&mcb_driver);
906e264ebf4SJohannes Thumshirn 	if  (err) {
907e264ebf4SJohannes Thumshirn 		pr_err("Failed to register MCB driver: %d\n", err);
908e264ebf4SJohannes Thumshirn 		uart_unregister_driver(&men_z135_driver);
909e264ebf4SJohannes Thumshirn 		return err;
910e264ebf4SJohannes Thumshirn 	}
911e264ebf4SJohannes Thumshirn 
912e264ebf4SJohannes Thumshirn 	return 0;
913e264ebf4SJohannes Thumshirn }
914e264ebf4SJohannes Thumshirn module_init(men_z135_init);
915e264ebf4SJohannes Thumshirn 
916e264ebf4SJohannes Thumshirn /**
917e264ebf4SJohannes Thumshirn  * men_z135_exit() - Driver Exit Routine
918e264ebf4SJohannes Thumshirn  *
919e264ebf4SJohannes Thumshirn  * men_z135_exit is called just before the driver is removed from memory.
920e264ebf4SJohannes Thumshirn  */
921e264ebf4SJohannes Thumshirn static void __exit men_z135_exit(void)
922e264ebf4SJohannes Thumshirn {
923e264ebf4SJohannes Thumshirn 	mcb_unregister_driver(&mcb_driver);
924e264ebf4SJohannes Thumshirn 	uart_unregister_driver(&men_z135_driver);
925e264ebf4SJohannes Thumshirn }
926e264ebf4SJohannes Thumshirn module_exit(men_z135_exit);
927e264ebf4SJohannes Thumshirn 
928e264ebf4SJohannes Thumshirn MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
929e264ebf4SJohannes Thumshirn MODULE_LICENSE("GPL v2");
930e264ebf4SJohannes Thumshirn MODULE_DESCRIPTION("MEN 16z135 High Speed UART");
931e264ebf4SJohannes Thumshirn MODULE_ALIAS("mcb:16z135");
932