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