1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2c4b05856SSrinivas Kandagatla /*
3c4b05856SSrinivas Kandagatla * st-asc.c: ST Asynchronous serial controller (ASC) driver
4c4b05856SSrinivas Kandagatla *
5c4b05856SSrinivas Kandagatla * Copyright (C) 2003-2013 STMicroelectronics (R&D) Limited
6c4b05856SSrinivas Kandagatla */
7c4b05856SSrinivas Kandagatla
8c4b05856SSrinivas Kandagatla #include <linux/module.h>
9c4b05856SSrinivas Kandagatla #include <linux/serial.h>
10c4b05856SSrinivas Kandagatla #include <linux/console.h>
11c4b05856SSrinivas Kandagatla #include <linux/sysrq.h>
126929cb00SLee Jones #include <linux/pinctrl/consumer.h>
13c4b05856SSrinivas Kandagatla #include <linux/platform_device.h>
14c4b05856SSrinivas Kandagatla #include <linux/io.h>
15c4b05856SSrinivas Kandagatla #include <linux/irq.h>
16c4b05856SSrinivas Kandagatla #include <linux/tty.h>
17c4b05856SSrinivas Kandagatla #include <linux/tty_flip.h>
18c4b05856SSrinivas Kandagatla #include <linux/delay.h>
19c4b05856SSrinivas Kandagatla #include <linux/spinlock.h>
20c4b05856SSrinivas Kandagatla #include <linux/of.h>
21c4b05856SSrinivas Kandagatla #include <linux/of_platform.h>
22c4b05856SSrinivas Kandagatla #include <linux/serial_core.h>
23c4b05856SSrinivas Kandagatla #include <linux/clk.h>
24a4f6072fSLee Jones #include <linux/gpio/consumer.h>
25c4b05856SSrinivas Kandagatla
26c4b05856SSrinivas Kandagatla #define DRIVER_NAME "st-asc"
27c4b05856SSrinivas Kandagatla #define ASC_SERIAL_NAME "ttyAS"
28c4b05856SSrinivas Kandagatla #define ASC_FIFO_SIZE 16
29c4b05856SSrinivas Kandagatla #define ASC_MAX_PORTS 8
30c4b05856SSrinivas Kandagatla
316929cb00SLee Jones /* Pinctrl states */
326929cb00SLee Jones #define DEFAULT 0
336929cb00SLee Jones #define NO_HW_FLOWCTRL 1
346929cb00SLee Jones
35c4b05856SSrinivas Kandagatla struct asc_port {
36c4b05856SSrinivas Kandagatla struct uart_port port;
37a4f6072fSLee Jones struct gpio_desc *rts;
38c4b05856SSrinivas Kandagatla struct clk *clk;
396929cb00SLee Jones struct pinctrl *pinctrl;
406929cb00SLee Jones struct pinctrl_state *states[2];
41c4b05856SSrinivas Kandagatla unsigned int hw_flow_control:1;
42c4b05856SSrinivas Kandagatla unsigned int force_m1:1;
43c4b05856SSrinivas Kandagatla };
44c4b05856SSrinivas Kandagatla
45c4b05856SSrinivas Kandagatla static struct asc_port asc_ports[ASC_MAX_PORTS];
46c4b05856SSrinivas Kandagatla static struct uart_driver asc_uart_driver;
47c4b05856SSrinivas Kandagatla
48c4b05856SSrinivas Kandagatla /*---- UART Register definitions ------------------------------*/
49c4b05856SSrinivas Kandagatla
50c4b05856SSrinivas Kandagatla /* Register offsets */
51c4b05856SSrinivas Kandagatla
52c4b05856SSrinivas Kandagatla #define ASC_BAUDRATE 0x00
53c4b05856SSrinivas Kandagatla #define ASC_TXBUF 0x04
54c4b05856SSrinivas Kandagatla #define ASC_RXBUF 0x08
55c4b05856SSrinivas Kandagatla #define ASC_CTL 0x0C
56c4b05856SSrinivas Kandagatla #define ASC_INTEN 0x10
57c4b05856SSrinivas Kandagatla #define ASC_STA 0x14
58c4b05856SSrinivas Kandagatla #define ASC_GUARDTIME 0x18
59c4b05856SSrinivas Kandagatla #define ASC_TIMEOUT 0x1C
60c4b05856SSrinivas Kandagatla #define ASC_TXRESET 0x20
61c4b05856SSrinivas Kandagatla #define ASC_RXRESET 0x24
62c4b05856SSrinivas Kandagatla #define ASC_RETRIES 0x28
63c4b05856SSrinivas Kandagatla
64c4b05856SSrinivas Kandagatla /* ASC_RXBUF */
65c4b05856SSrinivas Kandagatla #define ASC_RXBUF_PE 0x100
66c4b05856SSrinivas Kandagatla #define ASC_RXBUF_FE 0x200
670e4b5597SLee Jones /*
68c4b05856SSrinivas Kandagatla * Some of status comes from higher bits of the character and some come from
69c4b05856SSrinivas Kandagatla * the status register. Combining both of them in to single status using dummy
70c4b05856SSrinivas Kandagatla * bits.
71c4b05856SSrinivas Kandagatla */
72c4b05856SSrinivas Kandagatla #define ASC_RXBUF_DUMMY_RX 0x10000
73c4b05856SSrinivas Kandagatla #define ASC_RXBUF_DUMMY_BE 0x20000
74c4b05856SSrinivas Kandagatla #define ASC_RXBUF_DUMMY_OE 0x40000
75c4b05856SSrinivas Kandagatla
76c4b05856SSrinivas Kandagatla /* ASC_CTL */
77c4b05856SSrinivas Kandagatla
78c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_MSK 0x0007
79c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_8BIT 0x0001
80c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_7BIT_PAR 0x0003
81c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_9BIT 0x0004
82c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_8BIT_WKUP 0x0005
83c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_8BIT_PAR 0x0007
84c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_MSK 0x0018
85c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_HALFBIT 0x0000
86c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_1BIT 0x0008
87c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_1_HALFBIT 0x0010
88c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_2BIT 0x0018
89c4b05856SSrinivas Kandagatla #define ASC_CTL_PARITYODD 0x0020
90c4b05856SSrinivas Kandagatla #define ASC_CTL_LOOPBACK 0x0040
91c4b05856SSrinivas Kandagatla #define ASC_CTL_RUN 0x0080
92c4b05856SSrinivas Kandagatla #define ASC_CTL_RXENABLE 0x0100
93c4b05856SSrinivas Kandagatla #define ASC_CTL_SCENABLE 0x0200
94c4b05856SSrinivas Kandagatla #define ASC_CTL_FIFOENABLE 0x0400
95c4b05856SSrinivas Kandagatla #define ASC_CTL_CTSENABLE 0x0800
96c4b05856SSrinivas Kandagatla #define ASC_CTL_BAUDMODE 0x1000
97c4b05856SSrinivas Kandagatla
98c4b05856SSrinivas Kandagatla /* ASC_GUARDTIME */
99c4b05856SSrinivas Kandagatla
100c4b05856SSrinivas Kandagatla #define ASC_GUARDTIME_MSK 0x00FF
101c4b05856SSrinivas Kandagatla
102c4b05856SSrinivas Kandagatla /* ASC_INTEN */
103c4b05856SSrinivas Kandagatla
104c4b05856SSrinivas Kandagatla #define ASC_INTEN_RBE 0x0001
105c4b05856SSrinivas Kandagatla #define ASC_INTEN_TE 0x0002
106c4b05856SSrinivas Kandagatla #define ASC_INTEN_THE 0x0004
107c4b05856SSrinivas Kandagatla #define ASC_INTEN_PE 0x0008
108c4b05856SSrinivas Kandagatla #define ASC_INTEN_FE 0x0010
109c4b05856SSrinivas Kandagatla #define ASC_INTEN_OE 0x0020
110c4b05856SSrinivas Kandagatla #define ASC_INTEN_TNE 0x0040
111c4b05856SSrinivas Kandagatla #define ASC_INTEN_TOI 0x0080
112c4b05856SSrinivas Kandagatla #define ASC_INTEN_RHF 0x0100
113c4b05856SSrinivas Kandagatla
114c4b05856SSrinivas Kandagatla /* ASC_RETRIES */
115c4b05856SSrinivas Kandagatla
116c4b05856SSrinivas Kandagatla #define ASC_RETRIES_MSK 0x00FF
117c4b05856SSrinivas Kandagatla
118c4b05856SSrinivas Kandagatla /* ASC_RXBUF */
119c4b05856SSrinivas Kandagatla
120c4b05856SSrinivas Kandagatla #define ASC_RXBUF_MSK 0x03FF
121c4b05856SSrinivas Kandagatla
122c4b05856SSrinivas Kandagatla /* ASC_STA */
123c4b05856SSrinivas Kandagatla
124c4b05856SSrinivas Kandagatla #define ASC_STA_RBF 0x0001
125c4b05856SSrinivas Kandagatla #define ASC_STA_TE 0x0002
126c4b05856SSrinivas Kandagatla #define ASC_STA_THE 0x0004
127c4b05856SSrinivas Kandagatla #define ASC_STA_PE 0x0008
128c4b05856SSrinivas Kandagatla #define ASC_STA_FE 0x0010
129c4b05856SSrinivas Kandagatla #define ASC_STA_OE 0x0020
130c4b05856SSrinivas Kandagatla #define ASC_STA_TNE 0x0040
131c4b05856SSrinivas Kandagatla #define ASC_STA_TOI 0x0080
132c4b05856SSrinivas Kandagatla #define ASC_STA_RHF 0x0100
133c4b05856SSrinivas Kandagatla #define ASC_STA_TF 0x0200
134c4b05856SSrinivas Kandagatla #define ASC_STA_NKD 0x0400
135c4b05856SSrinivas Kandagatla
136c4b05856SSrinivas Kandagatla /* ASC_TIMEOUT */
137c4b05856SSrinivas Kandagatla
138c4b05856SSrinivas Kandagatla #define ASC_TIMEOUT_MSK 0x00FF
139c4b05856SSrinivas Kandagatla
140c4b05856SSrinivas Kandagatla /* ASC_TXBUF */
141c4b05856SSrinivas Kandagatla
142c4b05856SSrinivas Kandagatla #define ASC_TXBUF_MSK 0x01FF
143c4b05856SSrinivas Kandagatla
144c4b05856SSrinivas Kandagatla /*---- Inline function definitions ---------------------------*/
145c4b05856SSrinivas Kandagatla
to_asc_port(struct uart_port * port)146c4b05856SSrinivas Kandagatla static inline struct asc_port *to_asc_port(struct uart_port *port)
147c4b05856SSrinivas Kandagatla {
148c4b05856SSrinivas Kandagatla return container_of(port, struct asc_port, port);
149c4b05856SSrinivas Kandagatla }
150c4b05856SSrinivas Kandagatla
asc_in(struct uart_port * port,u32 offset)151c4b05856SSrinivas Kandagatla static inline u32 asc_in(struct uart_port *port, u32 offset)
152c4b05856SSrinivas Kandagatla {
153daea65a7SDaniel Thompson #ifdef readl_relaxed
15408177eceSDaniel Thompson return readl_relaxed(port->membase + offset);
155daea65a7SDaniel Thompson #else
156daea65a7SDaniel Thompson return readl(port->membase + offset);
157daea65a7SDaniel Thompson #endif
158c4b05856SSrinivas Kandagatla }
159c4b05856SSrinivas Kandagatla
asc_out(struct uart_port * port,u32 offset,u32 value)160c4b05856SSrinivas Kandagatla static inline void asc_out(struct uart_port *port, u32 offset, u32 value)
161c4b05856SSrinivas Kandagatla {
16208177eceSDaniel Thompson #ifdef writel_relaxed
16308177eceSDaniel Thompson writel_relaxed(value, port->membase + offset);
16408177eceSDaniel Thompson #else
165c4b05856SSrinivas Kandagatla writel(value, port->membase + offset);
16608177eceSDaniel Thompson #endif
167c4b05856SSrinivas Kandagatla }
168c4b05856SSrinivas Kandagatla
169c4b05856SSrinivas Kandagatla /*
170c4b05856SSrinivas Kandagatla * Some simple utility functions to enable and disable interrupts.
171c4b05856SSrinivas Kandagatla * Note that these need to be called with interrupts disabled.
172c4b05856SSrinivas Kandagatla */
asc_disable_tx_interrupts(struct uart_port * port)173c4b05856SSrinivas Kandagatla static inline void asc_disable_tx_interrupts(struct uart_port *port)
174c4b05856SSrinivas Kandagatla {
175c4b05856SSrinivas Kandagatla u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_THE;
176c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable);
177c4b05856SSrinivas Kandagatla (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
178c4b05856SSrinivas Kandagatla }
179c4b05856SSrinivas Kandagatla
asc_enable_tx_interrupts(struct uart_port * port)180c4b05856SSrinivas Kandagatla static inline void asc_enable_tx_interrupts(struct uart_port *port)
181c4b05856SSrinivas Kandagatla {
182c4b05856SSrinivas Kandagatla u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_THE;
183c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable);
184c4b05856SSrinivas Kandagatla }
185c4b05856SSrinivas Kandagatla
asc_disable_rx_interrupts(struct uart_port * port)186c4b05856SSrinivas Kandagatla static inline void asc_disable_rx_interrupts(struct uart_port *port)
187c4b05856SSrinivas Kandagatla {
188c4b05856SSrinivas Kandagatla u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_RBE;
189c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable);
190c4b05856SSrinivas Kandagatla (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
191c4b05856SSrinivas Kandagatla }
192c4b05856SSrinivas Kandagatla
asc_enable_rx_interrupts(struct uart_port * port)193c4b05856SSrinivas Kandagatla static inline void asc_enable_rx_interrupts(struct uart_port *port)
194c4b05856SSrinivas Kandagatla {
195c4b05856SSrinivas Kandagatla u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_RBE;
196c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable);
197c4b05856SSrinivas Kandagatla }
198c4b05856SSrinivas Kandagatla
asc_txfifo_is_empty(struct uart_port * port)199c4b05856SSrinivas Kandagatla static inline u32 asc_txfifo_is_empty(struct uart_port *port)
200c4b05856SSrinivas Kandagatla {
201c4b05856SSrinivas Kandagatla return asc_in(port, ASC_STA) & ASC_STA_TE;
202c4b05856SSrinivas Kandagatla }
203c4b05856SSrinivas Kandagatla
asc_txfifo_is_half_empty(struct uart_port * port)2041ffcd67dSDaniel Thompson static inline u32 asc_txfifo_is_half_empty(struct uart_port *port)
205c4b05856SSrinivas Kandagatla {
2061ffcd67dSDaniel Thompson return asc_in(port, ASC_STA) & ASC_STA_THE;
207c4b05856SSrinivas Kandagatla }
208c4b05856SSrinivas Kandagatla
asc_port_name(struct uart_port * port)209c4b05856SSrinivas Kandagatla static inline const char *asc_port_name(struct uart_port *port)
210c4b05856SSrinivas Kandagatla {
211c4b05856SSrinivas Kandagatla return to_platform_device(port->dev)->name;
212c4b05856SSrinivas Kandagatla }
213c4b05856SSrinivas Kandagatla
214c4b05856SSrinivas Kandagatla /*----------------------------------------------------------------------*/
215c4b05856SSrinivas Kandagatla
216c4b05856SSrinivas Kandagatla /*
217c4b05856SSrinivas Kandagatla * This section contains code to support the use of the ASC as a
218c4b05856SSrinivas Kandagatla * generic serial port.
219c4b05856SSrinivas Kandagatla */
220c4b05856SSrinivas Kandagatla
asc_hw_txroom(struct uart_port * port)221c4b05856SSrinivas Kandagatla static inline unsigned asc_hw_txroom(struct uart_port *port)
222c4b05856SSrinivas Kandagatla {
223c4b05856SSrinivas Kandagatla u32 status = asc_in(port, ASC_STA);
224c4b05856SSrinivas Kandagatla
225c4b05856SSrinivas Kandagatla if (status & ASC_STA_THE)
226c4b05856SSrinivas Kandagatla return port->fifosize / 2;
227c4b05856SSrinivas Kandagatla else if (!(status & ASC_STA_TF))
228c4b05856SSrinivas Kandagatla return 1;
229c4b05856SSrinivas Kandagatla
230c4b05856SSrinivas Kandagatla return 0;
231c4b05856SSrinivas Kandagatla }
232c4b05856SSrinivas Kandagatla
233c4b05856SSrinivas Kandagatla /*
234c4b05856SSrinivas Kandagatla * Start transmitting chars.
235c4b05856SSrinivas Kandagatla * This is called from both interrupt and task level.
236c4b05856SSrinivas Kandagatla * Either way interrupts are disabled.
237c4b05856SSrinivas Kandagatla */
asc_transmit_chars(struct uart_port * port)238c4b05856SSrinivas Kandagatla static void asc_transmit_chars(struct uart_port *port)
239c4b05856SSrinivas Kandagatla {
240d11cc8c3SJiri Slaby (SUSE) u8 ch;
241c4b05856SSrinivas Kandagatla
242d11cc8c3SJiri Slaby (SUSE) uart_port_tx_limited(port, ch, asc_hw_txroom(port),
243d11cc8c3SJiri Slaby (SUSE) true,
244d11cc8c3SJiri Slaby (SUSE) asc_out(port, ASC_TXBUF, ch),
245d11cc8c3SJiri Slaby (SUSE) ({}));
246c4b05856SSrinivas Kandagatla }
247c4b05856SSrinivas Kandagatla
asc_receive_chars(struct uart_port * port)248c4b05856SSrinivas Kandagatla static void asc_receive_chars(struct uart_port *port)
249c4b05856SSrinivas Kandagatla {
250c4b05856SSrinivas Kandagatla struct tty_port *tport = &port->state->port;
2515ab066e5SLee Jones unsigned long status, mode;
252c4b05856SSrinivas Kandagatla unsigned long c = 0;
253fd2b55f8SJiri Slaby u8 flag;
2545ab066e5SLee Jones bool ignore_pe = false;
2555ab066e5SLee Jones
2565ab066e5SLee Jones /*
2575ab066e5SLee Jones * Datasheet states: If the MODE field selects an 8-bit frame then
2585ab066e5SLee Jones * this [parity error] bit is undefined. Software should ignore this
2595ab066e5SLee Jones * bit when reading 8-bit frames.
2605ab066e5SLee Jones */
2615ab066e5SLee Jones mode = asc_in(port, ASC_CTL) & ASC_CTL_MODE_MSK;
2625ab066e5SLee Jones if (mode == ASC_CTL_MODE_8BIT || mode == ASC_CTL_MODE_8BIT_PAR)
2635ab066e5SLee Jones ignore_pe = true;
264c4b05856SSrinivas Kandagatla
2657b8a0353SAndy Shevchenko if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
266c4b05856SSrinivas Kandagatla pm_wakeup_event(tport->tty->dev, 0);
267c4b05856SSrinivas Kandagatla
268c4b05856SSrinivas Kandagatla while ((status = asc_in(port, ASC_STA)) & ASC_STA_RBF) {
269c4b05856SSrinivas Kandagatla c = asc_in(port, ASC_RXBUF) | ASC_RXBUF_DUMMY_RX;
270c4b05856SSrinivas Kandagatla flag = TTY_NORMAL;
271c4b05856SSrinivas Kandagatla port->icount.rx++;
272c4b05856SSrinivas Kandagatla
2735ab066e5SLee Jones if (status & ASC_STA_OE || c & ASC_RXBUF_FE ||
2745ab066e5SLee Jones (c & ASC_RXBUF_PE && !ignore_pe)) {
275c4b05856SSrinivas Kandagatla
276c4b05856SSrinivas Kandagatla if (c & ASC_RXBUF_FE) {
277c3c00b6fSDaniel Thompson if (c == (ASC_RXBUF_FE | ASC_RXBUF_DUMMY_RX)) {
278c4b05856SSrinivas Kandagatla port->icount.brk++;
279c4b05856SSrinivas Kandagatla if (uart_handle_break(port))
280c4b05856SSrinivas Kandagatla continue;
281c4b05856SSrinivas Kandagatla c |= ASC_RXBUF_DUMMY_BE;
282c4b05856SSrinivas Kandagatla } else {
283c4b05856SSrinivas Kandagatla port->icount.frame++;
284c4b05856SSrinivas Kandagatla }
285c4b05856SSrinivas Kandagatla } else if (c & ASC_RXBUF_PE) {
286c4b05856SSrinivas Kandagatla port->icount.parity++;
287c4b05856SSrinivas Kandagatla }
288c4b05856SSrinivas Kandagatla /*
289c4b05856SSrinivas Kandagatla * Reading any data from the RX FIFO clears the
290c4b05856SSrinivas Kandagatla * overflow error condition.
291c4b05856SSrinivas Kandagatla */
292c4b05856SSrinivas Kandagatla if (status & ASC_STA_OE) {
293c4b05856SSrinivas Kandagatla port->icount.overrun++;
294c4b05856SSrinivas Kandagatla c |= ASC_RXBUF_DUMMY_OE;
295c4b05856SSrinivas Kandagatla }
296c4b05856SSrinivas Kandagatla
297c4b05856SSrinivas Kandagatla c &= port->read_status_mask;
298c4b05856SSrinivas Kandagatla
299c4b05856SSrinivas Kandagatla if (c & ASC_RXBUF_DUMMY_BE)
300c4b05856SSrinivas Kandagatla flag = TTY_BREAK;
301c4b05856SSrinivas Kandagatla else if (c & ASC_RXBUF_PE)
302c4b05856SSrinivas Kandagatla flag = TTY_PARITY;
303c4b05856SSrinivas Kandagatla else if (c & ASC_RXBUF_FE)
304c4b05856SSrinivas Kandagatla flag = TTY_FRAME;
305c4b05856SSrinivas Kandagatla }
306c4b05856SSrinivas Kandagatla
307c3c00b6fSDaniel Thompson if (uart_handle_sysrq_char(port, c & 0xff))
308c4b05856SSrinivas Kandagatla continue;
309c4b05856SSrinivas Kandagatla
310c4b05856SSrinivas Kandagatla uart_insert_char(port, c, ASC_RXBUF_DUMMY_OE, c & 0xff, flag);
311c4b05856SSrinivas Kandagatla }
312c4b05856SSrinivas Kandagatla
313c4b05856SSrinivas Kandagatla /* Tell the rest of the system the news. New characters! */
314c4b05856SSrinivas Kandagatla tty_flip_buffer_push(tport);
315c4b05856SSrinivas Kandagatla }
316c4b05856SSrinivas Kandagatla
asc_interrupt(int irq,void * ptr)317c4b05856SSrinivas Kandagatla static irqreturn_t asc_interrupt(int irq, void *ptr)
318c4b05856SSrinivas Kandagatla {
319c4b05856SSrinivas Kandagatla struct uart_port *port = ptr;
320c4b05856SSrinivas Kandagatla u32 status;
321c4b05856SSrinivas Kandagatla
32247d4dfd9SThomas Gleixner uart_port_lock(port);
323c4b05856SSrinivas Kandagatla
324c4b05856SSrinivas Kandagatla status = asc_in(port, ASC_STA);
325c4b05856SSrinivas Kandagatla
326c4b05856SSrinivas Kandagatla if (status & ASC_STA_RBF) {
327c4b05856SSrinivas Kandagatla /* Receive FIFO not empty */
328c4b05856SSrinivas Kandagatla asc_receive_chars(port);
329c4b05856SSrinivas Kandagatla }
330c4b05856SSrinivas Kandagatla
331c4b05856SSrinivas Kandagatla if ((status & ASC_STA_THE) &&
332c4b05856SSrinivas Kandagatla (asc_in(port, ASC_INTEN) & ASC_INTEN_THE)) {
333c4b05856SSrinivas Kandagatla /* Transmitter FIFO at least half empty */
334c4b05856SSrinivas Kandagatla asc_transmit_chars(port);
335c4b05856SSrinivas Kandagatla }
336c4b05856SSrinivas Kandagatla
33747d4dfd9SThomas Gleixner uart_port_unlock(port);
338c4b05856SSrinivas Kandagatla
339c4b05856SSrinivas Kandagatla return IRQ_HANDLED;
340c4b05856SSrinivas Kandagatla }
341c4b05856SSrinivas Kandagatla
342c4b05856SSrinivas Kandagatla /*----------------------------------------------------------------------*/
343c4b05856SSrinivas Kandagatla
344c4b05856SSrinivas Kandagatla /*
345c4b05856SSrinivas Kandagatla * UART Functions
346c4b05856SSrinivas Kandagatla */
347c4b05856SSrinivas Kandagatla
asc_tx_empty(struct uart_port * port)348c4b05856SSrinivas Kandagatla static unsigned int asc_tx_empty(struct uart_port *port)
349c4b05856SSrinivas Kandagatla {
350c4b05856SSrinivas Kandagatla return asc_txfifo_is_empty(port) ? TIOCSER_TEMT : 0;
351c4b05856SSrinivas Kandagatla }
352c4b05856SSrinivas Kandagatla
asc_set_mctrl(struct uart_port * port,unsigned int mctrl)353c4b05856SSrinivas Kandagatla static void asc_set_mctrl(struct uart_port *port, unsigned int mctrl)
354c4b05856SSrinivas Kandagatla {
355a4f6072fSLee Jones struct asc_port *ascport = to_asc_port(port);
356a4f6072fSLee Jones
357c4b05856SSrinivas Kandagatla /*
358a4f6072fSLee Jones * This routine is used for seting signals of: DTR, DCD, CTS and RTS.
359a4f6072fSLee Jones * We use ASC's hardware for CTS/RTS when hardware flow-control is
360a4f6072fSLee Jones * enabled, however if the RTS line is required for another purpose,
361a4f6072fSLee Jones * commonly controlled using HUP from userspace, then we need to toggle
362a4f6072fSLee Jones * it manually, using GPIO.
363a4f6072fSLee Jones *
364a4f6072fSLee Jones * Some boards also have DTR and DCD implemented using PIO pins, code to
365a4f6072fSLee Jones * do this should be hooked in here.
366c4b05856SSrinivas Kandagatla */
367a4f6072fSLee Jones
368a4f6072fSLee Jones if (!ascport->rts)
369a4f6072fSLee Jones return;
370a4f6072fSLee Jones
371a4f6072fSLee Jones /* If HW flow-control is enabled, we can't fiddle with the RTS line */
372a4f6072fSLee Jones if (asc_in(port, ASC_CTL) & ASC_CTL_CTSENABLE)
373a4f6072fSLee Jones return;
374a4f6072fSLee Jones
375a4f6072fSLee Jones gpiod_set_value(ascport->rts, mctrl & TIOCM_RTS);
376c4b05856SSrinivas Kandagatla }
377c4b05856SSrinivas Kandagatla
asc_get_mctrl(struct uart_port * port)378c4b05856SSrinivas Kandagatla static unsigned int asc_get_mctrl(struct uart_port *port)
379c4b05856SSrinivas Kandagatla {
380c4b05856SSrinivas Kandagatla /*
381c4b05856SSrinivas Kandagatla * This routine is used for geting signals of: DTR, DCD, DSR, RI,
382c4b05856SSrinivas Kandagatla * and CTS/RTS
383c4b05856SSrinivas Kandagatla */
384c4b05856SSrinivas Kandagatla return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
385c4b05856SSrinivas Kandagatla }
386c4b05856SSrinivas Kandagatla
387c4b05856SSrinivas Kandagatla /* There are probably characters waiting to be transmitted. */
asc_start_tx(struct uart_port * port)388c4b05856SSrinivas Kandagatla static void asc_start_tx(struct uart_port *port)
389c4b05856SSrinivas Kandagatla {
3901788cf6aSJiri Slaby (SUSE) struct tty_port *tport = &port->state->port;
391c4b05856SSrinivas Kandagatla
3921788cf6aSJiri Slaby (SUSE) if (!kfifo_is_empty(&tport->xmit_fifo))
393c4b05856SSrinivas Kandagatla asc_enable_tx_interrupts(port);
394c4b05856SSrinivas Kandagatla }
395c4b05856SSrinivas Kandagatla
396c4b05856SSrinivas Kandagatla /* Transmit stop */
asc_stop_tx(struct uart_port * port)397c4b05856SSrinivas Kandagatla static void asc_stop_tx(struct uart_port *port)
398c4b05856SSrinivas Kandagatla {
399c4b05856SSrinivas Kandagatla asc_disable_tx_interrupts(port);
400c4b05856SSrinivas Kandagatla }
401c4b05856SSrinivas Kandagatla
402c4b05856SSrinivas Kandagatla /* Receive stop */
asc_stop_rx(struct uart_port * port)403c4b05856SSrinivas Kandagatla static void asc_stop_rx(struct uart_port *port)
404c4b05856SSrinivas Kandagatla {
405c4b05856SSrinivas Kandagatla asc_disable_rx_interrupts(port);
406c4b05856SSrinivas Kandagatla }
407c4b05856SSrinivas Kandagatla
408c4b05856SSrinivas Kandagatla /* Handle breaks - ignored by us */
asc_break_ctl(struct uart_port * port,int break_state)409c4b05856SSrinivas Kandagatla static void asc_break_ctl(struct uart_port *port, int break_state)
410c4b05856SSrinivas Kandagatla {
411c4b05856SSrinivas Kandagatla /* Nothing here yet .. */
412c4b05856SSrinivas Kandagatla }
413c4b05856SSrinivas Kandagatla
414c4b05856SSrinivas Kandagatla /*
415c4b05856SSrinivas Kandagatla * Enable port for reception.
416c4b05856SSrinivas Kandagatla */
asc_startup(struct uart_port * port)417c4b05856SSrinivas Kandagatla static int asc_startup(struct uart_port *port)
418c4b05856SSrinivas Kandagatla {
419616ea8d2SSudeep Holla if (request_irq(port->irq, asc_interrupt, 0,
420c4b05856SSrinivas Kandagatla asc_port_name(port), port)) {
421c4b05856SSrinivas Kandagatla dev_err(port->dev, "cannot allocate irq.\n");
422c4b05856SSrinivas Kandagatla return -ENODEV;
423c4b05856SSrinivas Kandagatla }
424c4b05856SSrinivas Kandagatla
425c4b05856SSrinivas Kandagatla asc_transmit_chars(port);
426c4b05856SSrinivas Kandagatla asc_enable_rx_interrupts(port);
427c4b05856SSrinivas Kandagatla
428c4b05856SSrinivas Kandagatla return 0;
429c4b05856SSrinivas Kandagatla }
430c4b05856SSrinivas Kandagatla
asc_shutdown(struct uart_port * port)431c4b05856SSrinivas Kandagatla static void asc_shutdown(struct uart_port *port)
432c4b05856SSrinivas Kandagatla {
433c4b05856SSrinivas Kandagatla asc_disable_tx_interrupts(port);
434c4b05856SSrinivas Kandagatla asc_disable_rx_interrupts(port);
435c4b05856SSrinivas Kandagatla free_irq(port->irq, port);
436c4b05856SSrinivas Kandagatla }
437c4b05856SSrinivas Kandagatla
asc_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)438c4b05856SSrinivas Kandagatla static void asc_pm(struct uart_port *port, unsigned int state,
439c4b05856SSrinivas Kandagatla unsigned int oldstate)
440c4b05856SSrinivas Kandagatla {
441c4b05856SSrinivas Kandagatla struct asc_port *ascport = to_asc_port(port);
44218ee37e1SJohan Hovold unsigned long flags;
443c4b05856SSrinivas Kandagatla u32 ctl;
444c4b05856SSrinivas Kandagatla
445c4b05856SSrinivas Kandagatla switch (state) {
446c4b05856SSrinivas Kandagatla case UART_PM_STATE_ON:
447c4b05856SSrinivas Kandagatla clk_prepare_enable(ascport->clk);
448c4b05856SSrinivas Kandagatla break;
449c4b05856SSrinivas Kandagatla case UART_PM_STATE_OFF:
450c4b05856SSrinivas Kandagatla /*
451c4b05856SSrinivas Kandagatla * Disable the ASC baud rate generator, which is as close as
452c4b05856SSrinivas Kandagatla * we can come to turning it off. Note this is not called with
453c4b05856SSrinivas Kandagatla * the port spinlock held.
454c4b05856SSrinivas Kandagatla */
45547d4dfd9SThomas Gleixner uart_port_lock_irqsave(port, &flags);
456c4b05856SSrinivas Kandagatla ctl = asc_in(port, ASC_CTL) & ~ASC_CTL_RUN;
457c4b05856SSrinivas Kandagatla asc_out(port, ASC_CTL, ctl);
45847d4dfd9SThomas Gleixner uart_port_unlock_irqrestore(port, flags);
459c4b05856SSrinivas Kandagatla clk_disable_unprepare(ascport->clk);
460c4b05856SSrinivas Kandagatla break;
461c4b05856SSrinivas Kandagatla }
462c4b05856SSrinivas Kandagatla }
463c4b05856SSrinivas Kandagatla
asc_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)464c4b05856SSrinivas Kandagatla static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
465bec5b814SIlpo Järvinen const struct ktermios *old)
466c4b05856SSrinivas Kandagatla {
467c4b05856SSrinivas Kandagatla struct asc_port *ascport = to_asc_port(port);
4684e489a6eSBartosz Golaszewski bool manual_rts, toggle_rts = false;
469d7356256SLee Jones struct gpio_desc *gpiod;
470c4b05856SSrinivas Kandagatla unsigned int baud;
471c4b05856SSrinivas Kandagatla u32 ctrl_val;
472c4b05856SSrinivas Kandagatla tcflag_t cflag;
473c4b05856SSrinivas Kandagatla unsigned long flags;
474c4b05856SSrinivas Kandagatla
475c4b05856SSrinivas Kandagatla /* Update termios to reflect hardware capabilities */
476c4b05856SSrinivas Kandagatla termios->c_cflag &= ~(CMSPAR |
477c4b05856SSrinivas Kandagatla (ascport->hw_flow_control ? 0 : CRTSCTS));
478c4b05856SSrinivas Kandagatla
479c4b05856SSrinivas Kandagatla port->uartclk = clk_get_rate(ascport->clk);
480c4b05856SSrinivas Kandagatla
481c4b05856SSrinivas Kandagatla baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
482c4b05856SSrinivas Kandagatla cflag = termios->c_cflag;
483c4b05856SSrinivas Kandagatla
48447d4dfd9SThomas Gleixner uart_port_lock_irqsave(port, &flags);
485c4b05856SSrinivas Kandagatla
486c4b05856SSrinivas Kandagatla /* read control register */
487c4b05856SSrinivas Kandagatla ctrl_val = asc_in(port, ASC_CTL);
488c4b05856SSrinivas Kandagatla
489c4b05856SSrinivas Kandagatla /* stop serial port and reset value */
490c4b05856SSrinivas Kandagatla asc_out(port, ASC_CTL, (ctrl_val & ~ASC_CTL_RUN));
491c4b05856SSrinivas Kandagatla ctrl_val = ASC_CTL_RXENABLE | ASC_CTL_FIFOENABLE;
492c4b05856SSrinivas Kandagatla
493c4b05856SSrinivas Kandagatla /* reset fifo rx & tx */
494c4b05856SSrinivas Kandagatla asc_out(port, ASC_TXRESET, 1);
495c4b05856SSrinivas Kandagatla asc_out(port, ASC_RXRESET, 1);
496c4b05856SSrinivas Kandagatla
497c4b05856SSrinivas Kandagatla /* set character length */
498c4b05856SSrinivas Kandagatla if ((cflag & CSIZE) == CS7) {
499c4b05856SSrinivas Kandagatla ctrl_val |= ASC_CTL_MODE_7BIT_PAR;
50052bb1cb7SIlpo Järvinen cflag |= PARENB;
501c4b05856SSrinivas Kandagatla } else {
502c4b05856SSrinivas Kandagatla ctrl_val |= (cflag & PARENB) ? ASC_CTL_MODE_8BIT_PAR :
503c4b05856SSrinivas Kandagatla ASC_CTL_MODE_8BIT;
50452bb1cb7SIlpo Järvinen cflag &= ~CSIZE;
50552bb1cb7SIlpo Järvinen cflag |= CS8;
506c4b05856SSrinivas Kandagatla }
50752bb1cb7SIlpo Järvinen termios->c_cflag = cflag;
508c4b05856SSrinivas Kandagatla
509c4b05856SSrinivas Kandagatla /* set stop bit */
510c4b05856SSrinivas Kandagatla ctrl_val |= (cflag & CSTOPB) ? ASC_CTL_STOP_2BIT : ASC_CTL_STOP_1BIT;
511c4b05856SSrinivas Kandagatla
512c4b05856SSrinivas Kandagatla /* odd parity */
513c4b05856SSrinivas Kandagatla if (cflag & PARODD)
514c4b05856SSrinivas Kandagatla ctrl_val |= ASC_CTL_PARITYODD;
515c4b05856SSrinivas Kandagatla
516c4b05856SSrinivas Kandagatla /* hardware flow control */
517d7356256SLee Jones if ((cflag & CRTSCTS)) {
518c4b05856SSrinivas Kandagatla ctrl_val |= ASC_CTL_CTSENABLE;
519c4b05856SSrinivas Kandagatla
520d7356256SLee Jones /* If flow-control selected, stop handling RTS manually */
521d7356256SLee Jones if (ascport->rts) {
5224e489a6eSBartosz Golaszewski toggle_rts = true;
5234e489a6eSBartosz Golaszewski manual_rts = false;
524d7356256SLee Jones }
525d7356256SLee Jones } else {
526d7356256SLee Jones /* If flow-control disabled, it's safe to handle RTS manually */
5274e489a6eSBartosz Golaszewski if (!ascport->rts && ascport->states[NO_HW_FLOWCTRL])
5284e489a6eSBartosz Golaszewski manual_rts = toggle_rts = true;
5298c44f9b5SDmitry Torokhov }
530d7356256SLee Jones
531c4b05856SSrinivas Kandagatla if ((baud < 19200) && !ascport->force_m1) {
532c4b05856SSrinivas Kandagatla asc_out(port, ASC_BAUDRATE, (port->uartclk / (16 * baud)));
533c4b05856SSrinivas Kandagatla } else {
534c4b05856SSrinivas Kandagatla /*
535c4b05856SSrinivas Kandagatla * MODE 1: recommended for high bit rates (above 19.2K)
536c4b05856SSrinivas Kandagatla *
537c4b05856SSrinivas Kandagatla * baudrate * 16 * 2^16
538c4b05856SSrinivas Kandagatla * ASCBaudRate = ------------------------
539c4b05856SSrinivas Kandagatla * inputclock
540c4b05856SSrinivas Kandagatla *
5411d6ba284SMaxime COQUELIN * To keep maths inside 64bits, we divide inputclock by 16.
542c4b05856SSrinivas Kandagatla */
5431d6ba284SMaxime COQUELIN u64 dividend = (u64)baud * (1 << 16);
5441d6ba284SMaxime COQUELIN
5451d6ba284SMaxime COQUELIN do_div(dividend, port->uartclk / 16);
5461d6ba284SMaxime COQUELIN asc_out(port, ASC_BAUDRATE, dividend);
547c4b05856SSrinivas Kandagatla ctrl_val |= ASC_CTL_BAUDMODE;
548c4b05856SSrinivas Kandagatla }
549c4b05856SSrinivas Kandagatla
550c4b05856SSrinivas Kandagatla uart_update_timeout(port, cflag, baud);
551c4b05856SSrinivas Kandagatla
552c4b05856SSrinivas Kandagatla ascport->port.read_status_mask = ASC_RXBUF_DUMMY_OE;
553c4b05856SSrinivas Kandagatla if (termios->c_iflag & INPCK)
554c4b05856SSrinivas Kandagatla ascport->port.read_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
555ef8b9ddcSPeter Hurley if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
556c4b05856SSrinivas Kandagatla ascport->port.read_status_mask |= ASC_RXBUF_DUMMY_BE;
557c4b05856SSrinivas Kandagatla
558c4b05856SSrinivas Kandagatla /*
559c4b05856SSrinivas Kandagatla * Characters to ignore
560c4b05856SSrinivas Kandagatla */
561c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask = 0;
562c4b05856SSrinivas Kandagatla if (termios->c_iflag & IGNPAR)
563c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
564c4b05856SSrinivas Kandagatla if (termios->c_iflag & IGNBRK) {
565c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_BE;
566c4b05856SSrinivas Kandagatla /*
567c4b05856SSrinivas Kandagatla * If we're ignoring parity and break indicators,
568c4b05856SSrinivas Kandagatla * ignore overruns too (for real raw support).
569c4b05856SSrinivas Kandagatla */
570c4b05856SSrinivas Kandagatla if (termios->c_iflag & IGNPAR)
571c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_OE;
572c4b05856SSrinivas Kandagatla }
573c4b05856SSrinivas Kandagatla
574c4b05856SSrinivas Kandagatla /*
575c4b05856SSrinivas Kandagatla * Ignore all characters if CREAD is not set.
576c4b05856SSrinivas Kandagatla */
577c4b05856SSrinivas Kandagatla if (!(termios->c_cflag & CREAD))
578c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_RX;
579c4b05856SSrinivas Kandagatla
580c4b05856SSrinivas Kandagatla /* Set the timeout */
581c4b05856SSrinivas Kandagatla asc_out(port, ASC_TIMEOUT, 20);
582c4b05856SSrinivas Kandagatla
583c4b05856SSrinivas Kandagatla /* write final value and enable port */
584c4b05856SSrinivas Kandagatla asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
585c4b05856SSrinivas Kandagatla
58647d4dfd9SThomas Gleixner uart_port_unlock_irqrestore(port, flags);
5874e489a6eSBartosz Golaszewski
5884e489a6eSBartosz Golaszewski if (toggle_rts) {
5894e489a6eSBartosz Golaszewski if (manual_rts) {
5904e489a6eSBartosz Golaszewski pinctrl_select_state(ascport->pinctrl,
5914e489a6eSBartosz Golaszewski ascport->states[NO_HW_FLOWCTRL]);
5924e489a6eSBartosz Golaszewski
5934e489a6eSBartosz Golaszewski gpiod = devm_gpiod_get(port->dev, "rts", GPIOD_OUT_LOW);
5944e489a6eSBartosz Golaszewski if (!IS_ERR(gpiod)) {
5954e489a6eSBartosz Golaszewski gpiod_set_consumer_name(gpiod,
5964e489a6eSBartosz Golaszewski port->dev->of_node->name);
5974e489a6eSBartosz Golaszewski ascport->rts = gpiod;
5984e489a6eSBartosz Golaszewski }
5994e489a6eSBartosz Golaszewski } else {
6004e489a6eSBartosz Golaszewski devm_gpiod_put(port->dev, ascport->rts);
6014e489a6eSBartosz Golaszewski ascport->rts = NULL;
6024e489a6eSBartosz Golaszewski pinctrl_select_state(ascport->pinctrl,
6034e489a6eSBartosz Golaszewski ascport->states[DEFAULT]);
6044e489a6eSBartosz Golaszewski }
6054e489a6eSBartosz Golaszewski }
606c4b05856SSrinivas Kandagatla }
607c4b05856SSrinivas Kandagatla
asc_type(struct uart_port * port)608c4b05856SSrinivas Kandagatla static const char *asc_type(struct uart_port *port)
609c4b05856SSrinivas Kandagatla {
610c4b05856SSrinivas Kandagatla return (port->type == PORT_ASC) ? DRIVER_NAME : NULL;
611c4b05856SSrinivas Kandagatla }
612c4b05856SSrinivas Kandagatla
asc_release_port(struct uart_port * port)613c4b05856SSrinivas Kandagatla static void asc_release_port(struct uart_port *port)
614c4b05856SSrinivas Kandagatla {
615c4b05856SSrinivas Kandagatla }
616c4b05856SSrinivas Kandagatla
asc_request_port(struct uart_port * port)617c4b05856SSrinivas Kandagatla static int asc_request_port(struct uart_port *port)
618c4b05856SSrinivas Kandagatla {
619c4b05856SSrinivas Kandagatla return 0;
620c4b05856SSrinivas Kandagatla }
621c4b05856SSrinivas Kandagatla
622c4b05856SSrinivas Kandagatla /*
623c4b05856SSrinivas Kandagatla * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set
624c4b05856SSrinivas Kandagatla * Set type field if successful
625c4b05856SSrinivas Kandagatla */
asc_config_port(struct uart_port * port,int flags)626c4b05856SSrinivas Kandagatla static void asc_config_port(struct uart_port *port, int flags)
627c4b05856SSrinivas Kandagatla {
628c4b05856SSrinivas Kandagatla if ((flags & UART_CONFIG_TYPE))
629c4b05856SSrinivas Kandagatla port->type = PORT_ASC;
630c4b05856SSrinivas Kandagatla }
631c4b05856SSrinivas Kandagatla
632c4b05856SSrinivas Kandagatla static int
asc_verify_port(struct uart_port * port,struct serial_struct * ser)633c4b05856SSrinivas Kandagatla asc_verify_port(struct uart_port *port, struct serial_struct *ser)
634c4b05856SSrinivas Kandagatla {
635c4b05856SSrinivas Kandagatla /* No user changeable parameters */
636c4b05856SSrinivas Kandagatla return -EINVAL;
637c4b05856SSrinivas Kandagatla }
638c4b05856SSrinivas Kandagatla
639c4b05856SSrinivas Kandagatla #ifdef CONFIG_CONSOLE_POLL
640c4b05856SSrinivas Kandagatla /*
641c4b05856SSrinivas Kandagatla * Console polling routines for writing and reading from the uart while
642c4b05856SSrinivas Kandagatla * in an interrupt or debug context (i.e. kgdb).
643c4b05856SSrinivas Kandagatla */
644c4b05856SSrinivas Kandagatla
asc_get_poll_char(struct uart_port * port)645c4b05856SSrinivas Kandagatla static int asc_get_poll_char(struct uart_port *port)
646c4b05856SSrinivas Kandagatla {
647c4b05856SSrinivas Kandagatla if (!(asc_in(port, ASC_STA) & ASC_STA_RBF))
648c4b05856SSrinivas Kandagatla return NO_POLL_CHAR;
649c4b05856SSrinivas Kandagatla
650c4b05856SSrinivas Kandagatla return asc_in(port, ASC_RXBUF);
651c4b05856SSrinivas Kandagatla }
652c4b05856SSrinivas Kandagatla
asc_put_poll_char(struct uart_port * port,unsigned char c)653c4b05856SSrinivas Kandagatla static void asc_put_poll_char(struct uart_port *port, unsigned char c)
654c4b05856SSrinivas Kandagatla {
6551ffcd67dSDaniel Thompson while (!asc_txfifo_is_half_empty(port))
656c4b05856SSrinivas Kandagatla cpu_relax();
657c4b05856SSrinivas Kandagatla asc_out(port, ASC_TXBUF, c);
658c4b05856SSrinivas Kandagatla }
659c4b05856SSrinivas Kandagatla
660c4b05856SSrinivas Kandagatla #endif /* CONFIG_CONSOLE_POLL */
661c4b05856SSrinivas Kandagatla
662c4b05856SSrinivas Kandagatla /*---------------------------------------------------------------------*/
663c4b05856SSrinivas Kandagatla
664eeb8bc1aSJulia Lawall static const struct uart_ops asc_uart_ops = {
665c4b05856SSrinivas Kandagatla .tx_empty = asc_tx_empty,
666c4b05856SSrinivas Kandagatla .set_mctrl = asc_set_mctrl,
667c4b05856SSrinivas Kandagatla .get_mctrl = asc_get_mctrl,
668c4b05856SSrinivas Kandagatla .start_tx = asc_start_tx,
669c4b05856SSrinivas Kandagatla .stop_tx = asc_stop_tx,
670c4b05856SSrinivas Kandagatla .stop_rx = asc_stop_rx,
671c4b05856SSrinivas Kandagatla .break_ctl = asc_break_ctl,
672c4b05856SSrinivas Kandagatla .startup = asc_startup,
673c4b05856SSrinivas Kandagatla .shutdown = asc_shutdown,
674c4b05856SSrinivas Kandagatla .set_termios = asc_set_termios,
675c4b05856SSrinivas Kandagatla .type = asc_type,
676c4b05856SSrinivas Kandagatla .release_port = asc_release_port,
677c4b05856SSrinivas Kandagatla .request_port = asc_request_port,
678c4b05856SSrinivas Kandagatla .config_port = asc_config_port,
679c4b05856SSrinivas Kandagatla .verify_port = asc_verify_port,
680c4b05856SSrinivas Kandagatla .pm = asc_pm,
681c4b05856SSrinivas Kandagatla #ifdef CONFIG_CONSOLE_POLL
682c4b05856SSrinivas Kandagatla .poll_get_char = asc_get_poll_char,
683c4b05856SSrinivas Kandagatla .poll_put_char = asc_put_poll_char,
684c4b05856SSrinivas Kandagatla #endif /* CONFIG_CONSOLE_POLL */
685c4b05856SSrinivas Kandagatla };
686c4b05856SSrinivas Kandagatla
asc_init_port(struct asc_port * ascport,struct platform_device * pdev)687c4b05856SSrinivas Kandagatla static int asc_init_port(struct asc_port *ascport,
688c4b05856SSrinivas Kandagatla struct platform_device *pdev)
689c4b05856SSrinivas Kandagatla {
690c4b05856SSrinivas Kandagatla struct uart_port *port = &ascport->port;
6913abeff77SJulia Lawall struct resource *res;
6926929cb00SLee Jones int ret;
693c4b05856SSrinivas Kandagatla
694c4b05856SSrinivas Kandagatla port->iotype = UPIO_MEM;
695c4b05856SSrinivas Kandagatla port->flags = UPF_BOOT_AUTOCONF;
696c4b05856SSrinivas Kandagatla port->ops = &asc_uart_ops;
697c4b05856SSrinivas Kandagatla port->fifosize = ASC_FIFO_SIZE;
698c4b05856SSrinivas Kandagatla port->dev = &pdev->dev;
699c4b05856SSrinivas Kandagatla port->irq = platform_get_irq(pdev, 0);
70039e17343SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_ST_ASC_CONSOLE);
701c4b05856SSrinivas Kandagatla
7029b4e18f0SYangtao Li port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
7033abeff77SJulia Lawall if (IS_ERR(port->membase))
7043abeff77SJulia Lawall return PTR_ERR(port->membase);
7053abeff77SJulia Lawall port->mapbase = res->start;
706c4b05856SSrinivas Kandagatla
707c4b05856SSrinivas Kandagatla spin_lock_init(&port->lock);
708c4b05856SSrinivas Kandagatla
709c4b05856SSrinivas Kandagatla ascport->clk = devm_clk_get(&pdev->dev, NULL);
710c4b05856SSrinivas Kandagatla
711c4b05856SSrinivas Kandagatla if (WARN_ON(IS_ERR(ascport->clk)))
712c4b05856SSrinivas Kandagatla return -EINVAL;
713c4b05856SSrinivas Kandagatla /* ensure that clk rate is correct by enabling the clk */
714130a9571SYuanjun Gong ret = clk_prepare_enable(ascport->clk);
715130a9571SYuanjun Gong if (ret)
716130a9571SYuanjun Gong return ret;
717c4b05856SSrinivas Kandagatla ascport->port.uartclk = clk_get_rate(ascport->clk);
718c4b05856SSrinivas Kandagatla WARN_ON(ascport->port.uartclk == 0);
719c4b05856SSrinivas Kandagatla clk_disable_unprepare(ascport->clk);
720c4b05856SSrinivas Kandagatla
7216929cb00SLee Jones ascport->pinctrl = devm_pinctrl_get(&pdev->dev);
7226929cb00SLee Jones if (IS_ERR(ascport->pinctrl)) {
7236929cb00SLee Jones ret = PTR_ERR(ascport->pinctrl);
7246929cb00SLee Jones dev_err(&pdev->dev, "Failed to get Pinctrl: %d\n", ret);
7252b01bfaeSDan Carpenter return ret;
7266929cb00SLee Jones }
7276929cb00SLee Jones
7286929cb00SLee Jones ascport->states[DEFAULT] =
7296929cb00SLee Jones pinctrl_lookup_state(ascport->pinctrl, "default");
7306929cb00SLee Jones if (IS_ERR(ascport->states[DEFAULT])) {
7316929cb00SLee Jones ret = PTR_ERR(ascport->states[DEFAULT]);
7326929cb00SLee Jones dev_err(&pdev->dev,
7336929cb00SLee Jones "Failed to look up Pinctrl state 'default': %d\n", ret);
7346929cb00SLee Jones return ret;
7356929cb00SLee Jones }
7366929cb00SLee Jones
7376929cb00SLee Jones /* "no-hw-flowctrl" state is optional */
7386929cb00SLee Jones ascport->states[NO_HW_FLOWCTRL] =
7396929cb00SLee Jones pinctrl_lookup_state(ascport->pinctrl, "no-hw-flowctrl");
7406929cb00SLee Jones if (IS_ERR(ascport->states[NO_HW_FLOWCTRL]))
7416929cb00SLee Jones ascport->states[NO_HW_FLOWCTRL] = NULL;
7426929cb00SLee Jones
743c4b05856SSrinivas Kandagatla return 0;
744c4b05856SSrinivas Kandagatla }
745c4b05856SSrinivas Kandagatla
asc_of_get_asc_port(struct platform_device * pdev)746c4b05856SSrinivas Kandagatla static struct asc_port *asc_of_get_asc_port(struct platform_device *pdev)
747c4b05856SSrinivas Kandagatla {
748c4b05856SSrinivas Kandagatla struct device_node *np = pdev->dev.of_node;
749c4b05856SSrinivas Kandagatla int id;
750c4b05856SSrinivas Kandagatla
751c4b05856SSrinivas Kandagatla if (!np)
752c4b05856SSrinivas Kandagatla return NULL;
753c4b05856SSrinivas Kandagatla
754dfd9190cSPatrice Chotard id = of_alias_get_id(np, "serial");
755dfd9190cSPatrice Chotard if (id < 0)
756c4b05856SSrinivas Kandagatla id = of_alias_get_id(np, ASC_SERIAL_NAME);
757c4b05856SSrinivas Kandagatla
758c4b05856SSrinivas Kandagatla if (id < 0)
759c4b05856SSrinivas Kandagatla id = 0;
760c4b05856SSrinivas Kandagatla
761c4b05856SSrinivas Kandagatla if (WARN_ON(id >= ASC_MAX_PORTS))
762c4b05856SSrinivas Kandagatla return NULL;
763c4b05856SSrinivas Kandagatla
764c4b05856SSrinivas Kandagatla asc_ports[id].hw_flow_control = of_property_read_bool(np,
765233680a2SLee Jones "uart-has-rtscts");
766643662d1SRaphael Gallais-Pou asc_ports[id].force_m1 = of_property_read_bool(np, "st,force-m1");
767c4b05856SSrinivas Kandagatla asc_ports[id].port.line = id;
768a4f6072fSLee Jones asc_ports[id].rts = NULL;
769a4f6072fSLee Jones
770c4b05856SSrinivas Kandagatla return &asc_ports[id];
771c4b05856SSrinivas Kandagatla }
772c4b05856SSrinivas Kandagatla
773a4f2dc9eSSrinivas Kandagatla #ifdef CONFIG_OF
774ed0bb232SFabian Frederick static const struct of_device_id asc_match[] = {
775c4b05856SSrinivas Kandagatla { .compatible = "st,asc", },
776c4b05856SSrinivas Kandagatla {},
777c4b05856SSrinivas Kandagatla };
778c4b05856SSrinivas Kandagatla
779c4b05856SSrinivas Kandagatla MODULE_DEVICE_TABLE(of, asc_match);
780a4f2dc9eSSrinivas Kandagatla #endif
781c4b05856SSrinivas Kandagatla
asc_serial_probe(struct platform_device * pdev)782c4b05856SSrinivas Kandagatla static int asc_serial_probe(struct platform_device *pdev)
783c4b05856SSrinivas Kandagatla {
784c4b05856SSrinivas Kandagatla int ret;
785c4b05856SSrinivas Kandagatla struct asc_port *ascport;
786c4b05856SSrinivas Kandagatla
787c4b05856SSrinivas Kandagatla ascport = asc_of_get_asc_port(pdev);
788c4b05856SSrinivas Kandagatla if (!ascport)
789c4b05856SSrinivas Kandagatla return -ENODEV;
790c4b05856SSrinivas Kandagatla
791c4b05856SSrinivas Kandagatla ret = asc_init_port(ascport, pdev);
792c4b05856SSrinivas Kandagatla if (ret)
793c4b05856SSrinivas Kandagatla return ret;
794c4b05856SSrinivas Kandagatla
795c4b05856SSrinivas Kandagatla ret = uart_add_one_port(&asc_uart_driver, &ascport->port);
796c4b05856SSrinivas Kandagatla if (ret)
797c4b05856SSrinivas Kandagatla return ret;
798c4b05856SSrinivas Kandagatla
799c4b05856SSrinivas Kandagatla platform_set_drvdata(pdev, &ascport->port);
800c4b05856SSrinivas Kandagatla
801c4b05856SSrinivas Kandagatla return 0;
802c4b05856SSrinivas Kandagatla }
803c4b05856SSrinivas Kandagatla
asc_serial_remove(struct platform_device * pdev)804338bc8f9SUwe Kleine-König static void asc_serial_remove(struct platform_device *pdev)
805c4b05856SSrinivas Kandagatla {
806c4b05856SSrinivas Kandagatla struct uart_port *port = platform_get_drvdata(pdev);
807c4b05856SSrinivas Kandagatla
808d5b3d02dSUwe Kleine-König uart_remove_one_port(&asc_uart_driver, port);
809c4b05856SSrinivas Kandagatla }
810c4b05856SSrinivas Kandagatla
asc_serial_suspend(struct device * dev)811c4b05856SSrinivas Kandagatla static int asc_serial_suspend(struct device *dev)
812c4b05856SSrinivas Kandagatla {
813a406c4b8SWolfram Sang struct uart_port *port = dev_get_drvdata(dev);
814c4b05856SSrinivas Kandagatla
815c4b05856SSrinivas Kandagatla return uart_suspend_port(&asc_uart_driver, port);
816c4b05856SSrinivas Kandagatla }
817c4b05856SSrinivas Kandagatla
asc_serial_resume(struct device * dev)818c4b05856SSrinivas Kandagatla static int asc_serial_resume(struct device *dev)
819c4b05856SSrinivas Kandagatla {
820a406c4b8SWolfram Sang struct uart_port *port = dev_get_drvdata(dev);
821c4b05856SSrinivas Kandagatla
822c4b05856SSrinivas Kandagatla return uart_resume_port(&asc_uart_driver, port);
823c4b05856SSrinivas Kandagatla }
824c4b05856SSrinivas Kandagatla
825c4b05856SSrinivas Kandagatla /*----------------------------------------------------------------------*/
826c4b05856SSrinivas Kandagatla
827c4b05856SSrinivas Kandagatla #ifdef CONFIG_SERIAL_ST_ASC_CONSOLE
asc_console_putchar(struct uart_port * port,unsigned char ch)8283f8bab17SJiri Slaby static void asc_console_putchar(struct uart_port *port, unsigned char ch)
829c4b05856SSrinivas Kandagatla {
830c4b05856SSrinivas Kandagatla unsigned int timeout = 1000000;
831c4b05856SSrinivas Kandagatla
832c4b05856SSrinivas Kandagatla /* Wait for upto 1 second in case flow control is stopping us. */
8331ffcd67dSDaniel Thompson while (--timeout && !asc_txfifo_is_half_empty(port))
834c4b05856SSrinivas Kandagatla udelay(1);
835c4b05856SSrinivas Kandagatla
836c4b05856SSrinivas Kandagatla asc_out(port, ASC_TXBUF, ch);
837c4b05856SSrinivas Kandagatla }
838c4b05856SSrinivas Kandagatla
839c4b05856SSrinivas Kandagatla /*
840c4b05856SSrinivas Kandagatla * Print a string to the serial port trying not to disturb
841c4b05856SSrinivas Kandagatla * any possible real use of the port...
842c4b05856SSrinivas Kandagatla */
843c4b05856SSrinivas Kandagatla
asc_console_write(struct console * co,const char * s,unsigned count)844c4b05856SSrinivas Kandagatla static void asc_console_write(struct console *co, const char *s, unsigned count)
845c4b05856SSrinivas Kandagatla {
846c4b05856SSrinivas Kandagatla struct uart_port *port = &asc_ports[co->index].port;
847c4b05856SSrinivas Kandagatla unsigned long flags;
848c4b05856SSrinivas Kandagatla unsigned long timeout = 1000000;
849c4b05856SSrinivas Kandagatla int locked = 1;
850c4b05856SSrinivas Kandagatla u32 intenable;
851c4b05856SSrinivas Kandagatla
852c4b05856SSrinivas Kandagatla if (port->sysrq)
853c4b05856SSrinivas Kandagatla locked = 0; /* asc_interrupt has already claimed the lock */
854c4b05856SSrinivas Kandagatla else if (oops_in_progress)
85547d4dfd9SThomas Gleixner locked = uart_port_trylock_irqsave(port, &flags);
856c4b05856SSrinivas Kandagatla else
85747d4dfd9SThomas Gleixner uart_port_lock_irqsave(port, &flags);
858c4b05856SSrinivas Kandagatla
859c4b05856SSrinivas Kandagatla /*
860c4b05856SSrinivas Kandagatla * Disable interrupts so we don't get the IRQ line bouncing
861c4b05856SSrinivas Kandagatla * up and down while interrupts are disabled.
862c4b05856SSrinivas Kandagatla */
863c4b05856SSrinivas Kandagatla intenable = asc_in(port, ASC_INTEN);
864c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, 0);
865c4b05856SSrinivas Kandagatla (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
866c4b05856SSrinivas Kandagatla
867c4b05856SSrinivas Kandagatla uart_console_write(port, s, count, asc_console_putchar);
868c4b05856SSrinivas Kandagatla
869c4b05856SSrinivas Kandagatla while (--timeout && !asc_txfifo_is_empty(port))
870c4b05856SSrinivas Kandagatla udelay(1);
871c4b05856SSrinivas Kandagatla
872c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable);
873c4b05856SSrinivas Kandagatla
874c4b05856SSrinivas Kandagatla if (locked)
87547d4dfd9SThomas Gleixner uart_port_unlock_irqrestore(port, flags);
876c4b05856SSrinivas Kandagatla }
877c4b05856SSrinivas Kandagatla
asc_console_setup(struct console * co,char * options)878c4b05856SSrinivas Kandagatla static int asc_console_setup(struct console *co, char *options)
879c4b05856SSrinivas Kandagatla {
880c4b05856SSrinivas Kandagatla struct asc_port *ascport;
881b5090cb4SLee Jones int baud = 115200;
882c4b05856SSrinivas Kandagatla int bits = 8;
883c4b05856SSrinivas Kandagatla int parity = 'n';
884c4b05856SSrinivas Kandagatla int flow = 'n';
885c4b05856SSrinivas Kandagatla
886c4b05856SSrinivas Kandagatla if (co->index >= ASC_MAX_PORTS)
887c4b05856SSrinivas Kandagatla return -ENODEV;
888c4b05856SSrinivas Kandagatla
889c4b05856SSrinivas Kandagatla ascport = &asc_ports[co->index];
890c4b05856SSrinivas Kandagatla
891c4b05856SSrinivas Kandagatla /*
892c4b05856SSrinivas Kandagatla * This driver does not support early console initialization
893c4b05856SSrinivas Kandagatla * (use ARM early printk support instead), so we only expect
894c4b05856SSrinivas Kandagatla * this to be called during the uart port registration when the
895c4b05856SSrinivas Kandagatla * driver gets probed and the port should be mapped at that point.
896c4b05856SSrinivas Kandagatla */
8975b4e79aeSMaxime COQUELIN if (ascport->port.mapbase == 0 || ascport->port.membase == NULL)
8985b4e79aeSMaxime COQUELIN return -ENXIO;
899c4b05856SSrinivas Kandagatla
900c4b05856SSrinivas Kandagatla if (options)
901c4b05856SSrinivas Kandagatla uart_parse_options(options, &baud, &parity, &bits, &flow);
902c4b05856SSrinivas Kandagatla
903c4b05856SSrinivas Kandagatla return uart_set_options(&ascport->port, co, baud, parity, bits, flow);
904c4b05856SSrinivas Kandagatla }
905c4b05856SSrinivas Kandagatla
906c4b05856SSrinivas Kandagatla static struct console asc_console = {
907c4b05856SSrinivas Kandagatla .name = ASC_SERIAL_NAME,
908c4b05856SSrinivas Kandagatla .device = uart_console_device,
909c4b05856SSrinivas Kandagatla .write = asc_console_write,
910c4b05856SSrinivas Kandagatla .setup = asc_console_setup,
911c4b05856SSrinivas Kandagatla .flags = CON_PRINTBUFFER,
912c4b05856SSrinivas Kandagatla .index = -1,
913c4b05856SSrinivas Kandagatla .data = &asc_uart_driver,
914c4b05856SSrinivas Kandagatla };
915c4b05856SSrinivas Kandagatla
916c4b05856SSrinivas Kandagatla #define ASC_SERIAL_CONSOLE (&asc_console)
917c4b05856SSrinivas Kandagatla
918c4b05856SSrinivas Kandagatla #else
919c4b05856SSrinivas Kandagatla #define ASC_SERIAL_CONSOLE NULL
920c4b05856SSrinivas Kandagatla #endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */
921c4b05856SSrinivas Kandagatla
922c4b05856SSrinivas Kandagatla static struct uart_driver asc_uart_driver = {
923c4b05856SSrinivas Kandagatla .owner = THIS_MODULE,
924c4b05856SSrinivas Kandagatla .driver_name = DRIVER_NAME,
925c4b05856SSrinivas Kandagatla .dev_name = ASC_SERIAL_NAME,
926c4b05856SSrinivas Kandagatla .major = 0,
927c4b05856SSrinivas Kandagatla .minor = 0,
928c4b05856SSrinivas Kandagatla .nr = ASC_MAX_PORTS,
929c4b05856SSrinivas Kandagatla .cons = ASC_SERIAL_CONSOLE,
930c4b05856SSrinivas Kandagatla };
931c4b05856SSrinivas Kandagatla
932*68c341c8SRaphael Gallais-Pou static DEFINE_SIMPLE_DEV_PM_OPS(asc_serial_pm_ops, asc_serial_suspend,
933*68c341c8SRaphael Gallais-Pou asc_serial_resume);
934c4b05856SSrinivas Kandagatla
935c4b05856SSrinivas Kandagatla static struct platform_driver asc_serial_driver = {
936c4b05856SSrinivas Kandagatla .probe = asc_serial_probe,
937338bc8f9SUwe Kleine-König .remove_new = asc_serial_remove,
938c4b05856SSrinivas Kandagatla .driver = {
939c4b05856SSrinivas Kandagatla .name = DRIVER_NAME,
940*68c341c8SRaphael Gallais-Pou .pm = pm_sleep_ptr(&asc_serial_pm_ops),
941c4b05856SSrinivas Kandagatla .of_match_table = of_match_ptr(asc_match),
942c4b05856SSrinivas Kandagatla },
943c4b05856SSrinivas Kandagatla };
944c4b05856SSrinivas Kandagatla
asc_init(void)945c4b05856SSrinivas Kandagatla static int __init asc_init(void)
946c4b05856SSrinivas Kandagatla {
947c4b05856SSrinivas Kandagatla int ret;
94806324664SKees Cook static const char banner[] __initconst =
949c4b05856SSrinivas Kandagatla KERN_INFO "STMicroelectronics ASC driver initialized\n";
950c4b05856SSrinivas Kandagatla
951c4b05856SSrinivas Kandagatla printk(banner);
952c4b05856SSrinivas Kandagatla
953c4b05856SSrinivas Kandagatla ret = uart_register_driver(&asc_uart_driver);
954c4b05856SSrinivas Kandagatla if (ret)
955c4b05856SSrinivas Kandagatla return ret;
956c4b05856SSrinivas Kandagatla
957c4b05856SSrinivas Kandagatla ret = platform_driver_register(&asc_serial_driver);
958c4b05856SSrinivas Kandagatla if (ret)
959c4b05856SSrinivas Kandagatla uart_unregister_driver(&asc_uart_driver);
960c4b05856SSrinivas Kandagatla
961c4b05856SSrinivas Kandagatla return ret;
962c4b05856SSrinivas Kandagatla }
963c4b05856SSrinivas Kandagatla
asc_exit(void)964c4b05856SSrinivas Kandagatla static void __exit asc_exit(void)
965c4b05856SSrinivas Kandagatla {
966c4b05856SSrinivas Kandagatla platform_driver_unregister(&asc_serial_driver);
967c4b05856SSrinivas Kandagatla uart_unregister_driver(&asc_uart_driver);
968c4b05856SSrinivas Kandagatla }
969c4b05856SSrinivas Kandagatla
970c4b05856SSrinivas Kandagatla module_init(asc_init);
971c4b05856SSrinivas Kandagatla module_exit(asc_exit);
972c4b05856SSrinivas Kandagatla
973c4b05856SSrinivas Kandagatla MODULE_ALIAS("platform:" DRIVER_NAME);
974c4b05856SSrinivas Kandagatla MODULE_AUTHOR("STMicroelectronics (R&D) Limited");
975c4b05856SSrinivas Kandagatla MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver");
976c4b05856SSrinivas Kandagatla MODULE_LICENSE("GPL");
977