blob: a83033774382990b8a2948f1ce78e0e87055c769 [file] [log] [blame]
John Linn61ec9012011-04-30 00:07:43 -04001/*
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07002 * Cadence UART driver (found in Xilinx Zynq)
John Linn61ec9012011-04-30 00:07:43 -04003 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07004 * 2011 - 2014 (C) Xilinx Inc.
John Linn61ec9012011-04-30 00:07:43 -04005 *
6 * This program is free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation;
9 * either version 2 of the License, or (at your option) any
10 * later version.
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070011 *
12 * This driver has originally been pushed by Xilinx using a Zynq-branding. This
13 * still shows in the naming of this file, the kconfig symbols and some symbols
14 * in the code.
John Linn61ec9012011-04-30 00:07:43 -040015 */
16
Vlad Lungu0c0c47b2013-10-17 14:08:06 -070017#if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18#define SUPPORT_SYSRQ
19#endif
20
John Linn61ec9012011-04-30 00:07:43 -040021#include <linux/platform_device.h>
John Linn61ec9012011-04-30 00:07:43 -040022#include <linux/serial.h>
Vlad Lungu0c0c47b2013-10-17 14:08:06 -070023#include <linux/console.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020024#include <linux/serial_core.h>
Soren Brinkmann30e1e282013-05-13 10:46:38 -070025#include <linux/slab.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020026#include <linux/tty.h>
27#include <linux/tty_flip.h>
Josh Cartwright2326669c2013-01-21 19:57:41 +010028#include <linux/clk.h>
John Linn61ec9012011-04-30 00:07:43 -040029#include <linux/irq.h>
30#include <linux/io.h>
31#include <linux/of.h>
Paul Gortmaker578b9ce2011-05-27 16:14:23 -040032#include <linux/module.h>
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +053033#include <linux/pm_runtime.h>
John Linn61ec9012011-04-30 00:07:43 -040034
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070035#define CDNS_UART_TTY_NAME "ttyPS"
36#define CDNS_UART_NAME "xuartps"
37#define CDNS_UART_MAJOR 0 /* use dynamic node allocation */
38#define CDNS_UART_MINOR 0 /* works best with devtmpfs */
39#define CDNS_UART_NR_PORTS 2
40#define CDNS_UART_FIFO_SIZE 64 /* FIFO size */
Thomas Betker9646e4f2015-03-11 22:39:25 +010041#define CDNS_UART_REGISTER_SPACE 0x1000
John Linn61ec9012011-04-30 00:07:43 -040042
Suneel85baf542013-10-17 14:08:08 -070043/* Rx Trigger level */
44static int rx_trigger_level = 56;
45module_param(rx_trigger_level, uint, S_IRUGO);
46MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
47
48/* Rx Timeout */
49static int rx_timeout = 10;
50module_param(rx_timeout, uint, S_IRUGO);
51MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
52
Soren Brinkmanne555a212014-04-04 17:23:39 -070053/* Register offsets for the UART. */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -080054#define CDNS_UART_CR 0x00 /* Control Register */
55#define CDNS_UART_MR 0x04 /* Mode Register */
56#define CDNS_UART_IER 0x08 /* Interrupt Enable */
57#define CDNS_UART_IDR 0x0C /* Interrupt Disable */
58#define CDNS_UART_IMR 0x10 /* Interrupt Mask */
59#define CDNS_UART_ISR 0x14 /* Interrupt Status */
60#define CDNS_UART_BAUDGEN 0x18 /* Baud Rate Generator */
Nava kishore Manne3816b2f82016-09-15 14:45:29 +053061#define CDNS_UART_RXTOUT 0x1C /* RX Timeout */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -080062#define CDNS_UART_RXWM 0x20 /* RX FIFO Trigger Level */
63#define CDNS_UART_MODEMCR 0x24 /* Modem Control */
64#define CDNS_UART_MODEMSR 0x28 /* Modem Status */
65#define CDNS_UART_SR 0x2C /* Channel Status */
66#define CDNS_UART_FIFO 0x30 /* FIFO */
67#define CDNS_UART_BAUDDIV 0x34 /* Baud Rate Divider */
68#define CDNS_UART_FLOWDEL 0x38 /* Flow Delay */
69#define CDNS_UART_IRRX_PWIDTH 0x3C /* IR Min Received Pulse Width */
70#define CDNS_UART_IRTX_PWIDTH 0x40 /* IR Transmitted pulse Width */
71#define CDNS_UART_TXWM 0x44 /* TX FIFO Trigger Level */
Nava kishore Manne3816b2f82016-09-15 14:45:29 +053072#define CDNS_UART_RXBS 0x48 /* RX FIFO byte status register */
John Linn61ec9012011-04-30 00:07:43 -040073
Soren Brinkmanne555a212014-04-04 17:23:39 -070074/* Control Register Bit Definitions */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070075#define CDNS_UART_CR_STOPBRK 0x00000100 /* Stop TX break */
76#define CDNS_UART_CR_STARTBRK 0x00000080 /* Set TX break */
77#define CDNS_UART_CR_TX_DIS 0x00000020 /* TX disabled. */
78#define CDNS_UART_CR_TX_EN 0x00000010 /* TX enabled */
79#define CDNS_UART_CR_RX_DIS 0x00000008 /* RX disabled. */
80#define CDNS_UART_CR_RX_EN 0x00000004 /* RX enabled */
81#define CDNS_UART_CR_TXRST 0x00000002 /* TX logic reset */
82#define CDNS_UART_CR_RXRST 0x00000001 /* RX logic reset */
83#define CDNS_UART_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
Nava kishore Manne3816b2f82016-09-15 14:45:29 +053084#define CDNS_UART_RXBS_PARITY 0x00000001 /* Parity error status */
85#define CDNS_UART_RXBS_FRAMING 0x00000002 /* Framing error status */
86#define CDNS_UART_RXBS_BRK 0x00000004 /* Overrun error status */
John Linn61ec9012011-04-30 00:07:43 -040087
Soren Brinkmanne555a212014-04-04 17:23:39 -070088/*
89 * Mode Register:
John Linn61ec9012011-04-30 00:07:43 -040090 * The mode register (MR) defines the mode of transfer as well as the data
91 * format. If this register is modified during transmission or reception,
92 * data validity cannot be guaranteed.
John Linn61ec9012011-04-30 00:07:43 -040093 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070094#define CDNS_UART_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
95#define CDNS_UART_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
96#define CDNS_UART_MR_CHMODE_NORM 0x00000000 /* Normal mode */
Yasir-Khan5935a2b2017-01-23 13:26:08 +010097#define CDNS_UART_MR_CHMODE_MASK 0x00000300 /* Mask for mode bits */
John Linn61ec9012011-04-30 00:07:43 -040098
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070099#define CDNS_UART_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
100#define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
John Linn61ec9012011-04-30 00:07:43 -0400101
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700102#define CDNS_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
103#define CDNS_UART_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
104#define CDNS_UART_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
105#define CDNS_UART_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
106#define CDNS_UART_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
John Linn61ec9012011-04-30 00:07:43 -0400107
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700108#define CDNS_UART_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
109#define CDNS_UART_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
110#define CDNS_UART_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
John Linn61ec9012011-04-30 00:07:43 -0400111
Soren Brinkmanne555a212014-04-04 17:23:39 -0700112/*
113 * Interrupt Registers:
John Linn61ec9012011-04-30 00:07:43 -0400114 * Interrupt control logic uses the interrupt enable register (IER) and the
115 * interrupt disable register (IDR) to set the value of the bits in the
116 * interrupt mask register (IMR). The IMR determines whether to pass an
117 * interrupt to the interrupt status register (ISR).
118 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
119 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
120 * Reading either IER or IDR returns 0x00.
John Linn61ec9012011-04-30 00:07:43 -0400121 * All four registers have the same bit definitions.
122 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700123#define CDNS_UART_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
124#define CDNS_UART_IXR_PARITY 0x00000080 /* Parity error interrupt */
125#define CDNS_UART_IXR_FRAMING 0x00000040 /* Framing error interrupt */
126#define CDNS_UART_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
127#define CDNS_UART_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
128#define CDNS_UART_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
129#define CDNS_UART_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
130#define CDNS_UART_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
131#define CDNS_UART_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
132#define CDNS_UART_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
133#define CDNS_UART_IXR_MASK 0x00001FFF /* Valid bit mask */
John Linn61ec9012011-04-30 00:07:43 -0400134
Anirudha Sarangia3081892016-09-21 15:01:18 +0100135 /*
136 * Do not enable parity error interrupt for the following
137 * reason: When parity error interrupt is enabled, each Rx
138 * parity error always results in 2 events. The first one
139 * being parity error interrupt and the second one with a
140 * proper Rx interrupt with the incoming data. Disabling
141 * parity error interrupt ensures better handling of parity
142 * error events. With this change, for a parity error case, we
143 * get a Rx interrupt with parity error set in ISR register
144 * and we still handle parity errors in the desired way.
145 */
146
147#define CDNS_UART_RX_IRQS (CDNS_UART_IXR_FRAMING | \
148 CDNS_UART_IXR_OVERRUN | \
149 CDNS_UART_IXR_RXTRIG | \
Soren Brinkmann373e8822016-01-11 17:41:37 -0800150 CDNS_UART_IXR_TOUT)
151
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700152/* Goes in read_status_mask for break detection as the HW doesn't do it*/
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530153#define CDNS_UART_IXR_BRK 0x00002000
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700154
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530155#define CDNS_UART_RXBS_SUPPORT BIT(1)
Soren Brinkmanne555a212014-04-04 17:23:39 -0700156/*
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +0100157 * Modem Control register:
158 * The read/write Modem Control register controls the interface with the modem
159 * or data set, or a peripheral device emulating a modem.
160 */
161#define CDNS_UART_MODEMCR_FCM 0x00000020 /* Automatic flow control mode */
162#define CDNS_UART_MODEMCR_RTS 0x00000002 /* Request to send output control */
163#define CDNS_UART_MODEMCR_DTR 0x00000001 /* Data Terminal Ready */
164
165/*
Soren Brinkmanne555a212014-04-04 17:23:39 -0700166 * Channel Status Register:
John Linn61ec9012011-04-30 00:07:43 -0400167 * The channel status register (CSR) is provided to enable the control logic
168 * to monitor the status of bits in the channel interrupt status register,
169 * even if these are masked out by the interrupt mask register.
170 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700171#define CDNS_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
172#define CDNS_UART_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
173#define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
174#define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
John Linn61ec9012011-04-30 00:07:43 -0400175
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700176/* baud dividers min/max values */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700177#define CDNS_UART_BDIV_MIN 4
178#define CDNS_UART_BDIV_MAX 255
179#define CDNS_UART_CD_MAX 65535
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +0530180#define UART_AUTOSUSPEND_TIMEOUT 3000
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700181
John Linn61ec9012011-04-30 00:07:43 -0400182/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700183 * struct cdns_uart - device data
Michal Simek489810a12014-04-04 17:23:37 -0700184 * @port: Pointer to the UART port
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700185 * @uartclk: Reference clock
186 * @pclk: APB clock
Michal Simek489810a12014-04-04 17:23:37 -0700187 * @baud: Current baud rate
188 * @clk_rate_change_nb: Notifier block for clock changes
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700189 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700190struct cdns_uart {
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700191 struct uart_port *port;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700192 struct clk *uartclk;
193 struct clk *pclk;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700194 unsigned int baud;
195 struct notifier_block clk_rate_change_nb;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530196 u32 quirks;
197};
198struct cdns_platform_data {
199 u32 quirks;
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700200};
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700201#define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
202 clk_rate_change_nb);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700203
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100204/**
205 * cdns_uart_handle_rx - Handle the received bytes along with Rx errors.
206 * @dev_id: Id of the UART port
207 * @isrstatus: The interrupt status register value as read
208 * Return: None
209 */
210static void cdns_uart_handle_rx(void *dev_id, unsigned int isrstatus)
John Linn61ec9012011-04-30 00:07:43 -0400211{
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100212 struct uart_port *port = (struct uart_port *)dev_id;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530213 struct cdns_uart *cdns_uart = port->private_data;
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100214 unsigned int data;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530215 unsigned int rxbs_status = 0;
216 unsigned int status_mask;
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100217 unsigned int framerrprocessed = 0;
218 char status = TTY_NORMAL;
219 bool is_rxbs_support;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530220
221 is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
222
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100223 while ((readl(port->membase + CDNS_UART_SR) &
224 CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530225 if (is_rxbs_support)
226 rxbs_status = readl(port->membase + CDNS_UART_RXBS);
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800227 data = readl(port->membase + CDNS_UART_FIFO);
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100228 port->icount.rx++;
229 /*
230 * There is no hardware break detection in Zynq, so we interpret
231 * framing error with all-zeros data as a break sequence.
232 * Most of the time, there's another non-zero byte at the
233 * end of the sequence.
234 */
235 if (!is_rxbs_support && (isrstatus & CDNS_UART_IXR_FRAMING)) {
236 if (!data) {
237 port->read_status_mask |= CDNS_UART_IXR_BRK;
238 framerrprocessed = 1;
Soren Brinkmann354fb1a2016-01-11 17:41:38 -0800239 continue;
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100240 }
Soren Brinkmann354fb1a2016-01-11 17:41:38 -0800241 }
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530242 if (is_rxbs_support && (rxbs_status & CDNS_UART_RXBS_BRK)) {
243 port->icount.brk++;
244 status = TTY_BREAK;
245 if (uart_handle_break(port))
246 continue;
247 }
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700248
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100249 isrstatus &= port->read_status_mask;
250 isrstatus &= ~port->ignore_status_mask;
251 status_mask = port->read_status_mask;
252 status_mask &= ~port->ignore_status_mask;
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700253
Nava kishore Manne212d2492016-09-22 16:58:15 +0100254 if (data &&
255 (port->read_status_mask & CDNS_UART_IXR_BRK)) {
256 port->read_status_mask &= ~CDNS_UART_IXR_BRK;
257 port->icount.brk++;
258 if (uart_handle_break(port))
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100259 continue;
John Linn61ec9012011-04-30 00:07:43 -0400260 }
Nava kishore Manne212d2492016-09-22 16:58:15 +0100261
262 if (uart_handle_sysrq_char(port, data))
263 continue;
264
265 if (is_rxbs_support) {
266 if ((rxbs_status & CDNS_UART_RXBS_PARITY)
267 && (status_mask & CDNS_UART_IXR_PARITY)) {
268 port->icount.parity++;
269 status = TTY_PARITY;
270 }
271 if ((rxbs_status & CDNS_UART_RXBS_FRAMING)
272 && (status_mask & CDNS_UART_IXR_PARITY)) {
273 port->icount.frame++;
274 status = TTY_FRAME;
275 }
276 } else {
277 if (isrstatus & CDNS_UART_IXR_PARITY) {
278 port->icount.parity++;
279 status = TTY_PARITY;
280 }
281 if ((isrstatus & CDNS_UART_IXR_FRAMING) &&
282 !framerrprocessed) {
283 port->icount.frame++;
284 status = TTY_FRAME;
285 }
286 }
287 if (isrstatus & CDNS_UART_IXR_OVERRUN) {
288 port->icount.overrun++;
289 tty_insert_flip_char(&port->state->port, 0,
290 TTY_OVERRUN);
291 }
292 tty_insert_flip_char(&port->state->port, data, status);
293 isrstatus = 0;
John Linn61ec9012011-04-30 00:07:43 -0400294 }
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100295 spin_unlock(&port->lock);
Soren Brinkmann354fb1a2016-01-11 17:41:38 -0800296 tty_flip_buffer_push(&port->state->port);
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100297 spin_lock(&port->lock);
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800298}
299
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100300/**
301 * cdns_uart_handle_tx - Handle the bytes to be Txed.
302 * @dev_id: Id of the UART port
303 * Return: None
304 */
305static void cdns_uart_handle_tx(void *dev_id)
Soren Brinkmann079865802016-01-11 17:41:41 -0800306{
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100307 struct uart_port *port = (struct uart_port *)dev_id;
Soren Brinkmann079865802016-01-11 17:41:41 -0800308 unsigned int numbytes;
309
310 if (uart_circ_empty(&port->state->xmit)) {
311 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IDR);
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100312 } else {
313 numbytes = port->fifosize;
314 while (numbytes && !uart_circ_empty(&port->state->xmit) &&
315 !(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)) {
316 /*
317 * Get the data from the UART circular buffer
318 * and write it to the cdns_uart's TX_FIFO
319 * register.
320 */
321 writel(
322 port->state->xmit.buf[port->state->xmit.
323 tail], port->membase + CDNS_UART_FIFO);
324
325 port->icount.tx++;
326
327 /*
328 * Adjust the tail of the UART buffer and wrap
329 * the buffer if it reaches limit.
330 */
331 port->state->xmit.tail =
332 (port->state->xmit.tail + 1) &
333 (UART_XMIT_SIZE - 1);
334
335 numbytes--;
336 }
337
338 if (uart_circ_chars_pending(
339 &port->state->xmit) < WAKEUP_CHARS)
340 uart_write_wakeup(port);
Soren Brinkmann079865802016-01-11 17:41:41 -0800341 }
Soren Brinkmann079865802016-01-11 17:41:41 -0800342}
343
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800344/**
345 * cdns_uart_isr - Interrupt handler
346 * @irq: Irq number
347 * @dev_id: Id of the port
348 *
349 * Return: IRQHANDLED
350 */
351static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
352{
353 struct uart_port *port = (struct uart_port *)dev_id;
Soren Brinkmann079865802016-01-11 17:41:41 -0800354 unsigned int isrstatus;
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800355
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100356 spin_lock(&port->lock);
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800357
358 /* Read the interrupt status register to determine which
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100359 * interrupt(s) is/are active and clear them.
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800360 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800361 isrstatus = readl(port->membase + CDNS_UART_ISR);
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800362 writel(isrstatus, port->membase + CDNS_UART_ISR);
John Linn61ec9012011-04-30 00:07:43 -0400363
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100364 if (isrstatus & CDNS_UART_IXR_TXEMPTY) {
365 cdns_uart_handle_tx(dev_id);
366 isrstatus &= ~CDNS_UART_IXR_TXEMPTY;
367 }
368 if (isrstatus & CDNS_UART_IXR_MASK)
369 cdns_uart_handle_rx(dev_id, isrstatus);
John Linn61ec9012011-04-30 00:07:43 -0400370
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100371 spin_unlock(&port->lock);
John Linn61ec9012011-04-30 00:07:43 -0400372 return IRQ_HANDLED;
373}
374
375/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700376 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700377 * @clk: UART module input clock
378 * @baud: Desired baud rate
379 * @rbdiv: BDIV value (return value)
380 * @rcd: CD value (return value)
381 * @div8: Value for clk_sel bit in mod (return value)
Michal Simek489810a12014-04-04 17:23:37 -0700382 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700383 * was too much error, zero if no valid divisors are found.
384 *
385 * Formula to obtain baud rate is
386 * baud_tx/rx rate = clk/CD * (BDIV + 1)
387 * input_clk = (Uart User Defined Clock or Apb Clock)
388 * depends on UCLKEN in MR Reg
389 * clk = input_clk or input_clk/8;
390 * depends on CLKS in MR reg
391 * CD and BDIV depends on values in
392 * baud rate generate register
393 * baud rate clock divisor register
394 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700395static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
396 unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
John Linn61ec9012011-04-30 00:07:43 -0400397{
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700398 u32 cd, bdiv;
399 unsigned int calc_baud;
400 unsigned int bestbaud = 0;
John Linn61ec9012011-04-30 00:07:43 -0400401 unsigned int bauderror;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700402 unsigned int besterror = ~0;
John Linn61ec9012011-04-30 00:07:43 -0400403
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700404 if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700405 *div8 = 1;
406 clk /= 8;
407 } else {
408 *div8 = 0;
409 }
John Linn61ec9012011-04-30 00:07:43 -0400410
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700411 for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700412 cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700413 if (cd < 1 || cd > CDNS_UART_CD_MAX)
John Linn61ec9012011-04-30 00:07:43 -0400414 continue;
415
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700416 calc_baud = clk / (cd * (bdiv + 1));
John Linn61ec9012011-04-30 00:07:43 -0400417
418 if (baud > calc_baud)
419 bauderror = baud - calc_baud;
420 else
421 bauderror = calc_baud - baud;
422
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700423 if (besterror > bauderror) {
424 *rbdiv = bdiv;
425 *rcd = cd;
426 bestbaud = calc_baud;
427 besterror = bauderror;
John Linn61ec9012011-04-30 00:07:43 -0400428 }
429 }
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700430 /* use the values when percent error is acceptable */
431 if (((besterror * 100) / baud) < 3)
432 bestbaud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400433
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700434 return bestbaud;
435}
436
437/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700438 * cdns_uart_set_baud_rate - Calculate and set the baud rate
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700439 * @port: Handle to the uart port structure
440 * @baud: Baud rate to set
Michal Simek489810a12014-04-04 17:23:37 -0700441 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700442 * was too much error, zero if no valid divisors are found.
443 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700444static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700445 unsigned int baud)
446{
447 unsigned int calc_baud;
Soren Brinkmannd54b1812013-10-21 16:40:59 -0700448 u32 cd = 0, bdiv = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700449 u32 mreg;
450 int div8;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700451 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700452
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700453 calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700454 &div8);
455
456 /* Write new divisors to hardware */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800457 mreg = readl(port->membase + CDNS_UART_MR);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700458 if (div8)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700459 mreg |= CDNS_UART_MR_CLKSEL;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700460 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700461 mreg &= ~CDNS_UART_MR_CLKSEL;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800462 writel(mreg, port->membase + CDNS_UART_MR);
463 writel(cd, port->membase + CDNS_UART_BAUDGEN);
464 writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700465 cdns_uart->baud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400466
467 return calc_baud;
468}
469
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700470#ifdef CONFIG_COMMON_CLK
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700471/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700472 * cdns_uart_clk_notitifer_cb - Clock notifier callback
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700473 * @nb: Notifier block
474 * @event: Notify event
475 * @data: Notifier data
Soren Brinkmanne555a212014-04-04 17:23:39 -0700476 * Return: NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700477 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700478static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700479 unsigned long event, void *data)
480{
481 u32 ctrl_reg;
482 struct uart_port *port;
483 int locked = 0;
484 struct clk_notifier_data *ndata = data;
485 unsigned long flags = 0;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700486 struct cdns_uart *cdns_uart = to_cdns_uart(nb);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700487
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700488 port = cdns_uart->port;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700489 if (port->suspended)
490 return NOTIFY_OK;
491
492 switch (event) {
493 case PRE_RATE_CHANGE:
494 {
Soren Brinkmanne555a212014-04-04 17:23:39 -0700495 u32 bdiv, cd;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700496 int div8;
497
498 /*
499 * Find out if current baud-rate can be achieved with new clock
500 * frequency.
501 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700502 if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700503 &bdiv, &cd, &div8)) {
504 dev_warn(port->dev, "clock rate change rejected\n");
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700505 return NOTIFY_BAD;
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700506 }
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700507
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700508 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700509
510 /* Disable the TX and RX to set baud rate */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800511 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700512 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800513 writel(ctrl_reg, port->membase + CDNS_UART_CR);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700514
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700515 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700516
517 return NOTIFY_OK;
518 }
519 case POST_RATE_CHANGE:
520 /*
521 * Set clk dividers to generate correct baud with new clock
522 * frequency.
523 */
524
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700525 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700526
527 locked = 1;
528 port->uartclk = ndata->new_rate;
529
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700530 cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
531 cdns_uart->baud);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700532 /* fall through */
533 case ABORT_RATE_CHANGE:
534 if (!locked)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700535 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700536
537 /* Set TX/RX Reset */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800538 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700539 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800540 writel(ctrl_reg, port->membase + CDNS_UART_CR);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700541
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800542 while (readl(port->membase + CDNS_UART_CR) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700543 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700544 cpu_relax();
545
546 /*
547 * Clear the RX disable and TX disable bits and then set the TX
548 * enable bit and RX enable bit to enable the transmitter and
549 * receiver.
550 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800551 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
552 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700553 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
554 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800555 writel(ctrl_reg, port->membase + CDNS_UART_CR);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700556
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700557 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700558
559 return NOTIFY_OK;
560 default:
561 return NOTIFY_DONE;
562 }
563}
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700564#endif
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700565
John Linn61ec9012011-04-30 00:07:43 -0400566/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700567 * cdns_uart_start_tx - Start transmitting bytes
John Linn61ec9012011-04-30 00:07:43 -0400568 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700569 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700570static void cdns_uart_start_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400571{
Soren Brinkmann079865802016-01-11 17:41:41 -0800572 unsigned int status;
John Linn61ec9012011-04-30 00:07:43 -0400573
Soren Brinkmannea8dd8e2015-12-26 02:43:51 -0800574 if (uart_tx_stopped(port))
John Linn61ec9012011-04-30 00:07:43 -0400575 return;
576
Soren Brinkmanne3538c32015-12-26 02:43:49 -0800577 /*
578 * Set the TX enable bit and clear the TX disable bit to enable the
John Linn61ec9012011-04-30 00:07:43 -0400579 * transmitter.
580 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800581 status = readl(port->membase + CDNS_UART_CR);
Soren Brinkmanne3538c32015-12-26 02:43:49 -0800582 status &= ~CDNS_UART_CR_TX_DIS;
583 status |= CDNS_UART_CR_TX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800584 writel(status, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400585
Soren Brinkmannea8dd8e2015-12-26 02:43:51 -0800586 if (uart_circ_empty(&port->state->xmit))
587 return;
588
Soren Brinkmann079865802016-01-11 17:41:41 -0800589 cdns_uart_handle_tx(port);
John Linn61ec9012011-04-30 00:07:43 -0400590
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800591 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR);
John Linn61ec9012011-04-30 00:07:43 -0400592 /* Enable the TX Empty interrupt */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800593 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IER);
John Linn61ec9012011-04-30 00:07:43 -0400594}
595
596/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700597 * cdns_uart_stop_tx - Stop TX
John Linn61ec9012011-04-30 00:07:43 -0400598 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700599 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700600static void cdns_uart_stop_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400601{
602 unsigned int regval;
603
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800604 regval = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700605 regval |= CDNS_UART_CR_TX_DIS;
John Linn61ec9012011-04-30 00:07:43 -0400606 /* Disable the transmitter */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800607 writel(regval, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400608}
609
610/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700611 * cdns_uart_stop_rx - Stop RX
John Linn61ec9012011-04-30 00:07:43 -0400612 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700613 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700614static void cdns_uart_stop_rx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400615{
616 unsigned int regval;
617
Soren Brinkmann373e8822016-01-11 17:41:37 -0800618 /* Disable RX IRQs */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800619 writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR);
Soren Brinkmann373e8822016-01-11 17:41:37 -0800620
621 /* Disable the receiver */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800622 regval = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700623 regval |= CDNS_UART_CR_RX_DIS;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800624 writel(regval, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400625}
626
627/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700628 * cdns_uart_tx_empty - Check whether TX is empty
John Linn61ec9012011-04-30 00:07:43 -0400629 * @port: Handle to the uart port structure
630 *
Michal Simek489810a12014-04-04 17:23:37 -0700631 * Return: TIOCSER_TEMT on success, 0 otherwise
632 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700633static unsigned int cdns_uart_tx_empty(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400634{
635 unsigned int status;
636
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800637 status = readl(port->membase + CDNS_UART_SR) &
Thomas Betker19f22ef2015-03-12 22:11:59 +0100638 CDNS_UART_SR_TXEMPTY;
John Linn61ec9012011-04-30 00:07:43 -0400639 return status ? TIOCSER_TEMT : 0;
640}
641
642/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700643 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
John Linn61ec9012011-04-30 00:07:43 -0400644 * transmitting char breaks
645 * @port: Handle to the uart port structure
646 * @ctl: Value based on which start or stop decision is taken
Michal Simek489810a12014-04-04 17:23:37 -0700647 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700648static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
John Linn61ec9012011-04-30 00:07:43 -0400649{
650 unsigned int status;
651 unsigned long flags;
652
653 spin_lock_irqsave(&port->lock, flags);
654
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800655 status = readl(port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400656
657 if (ctl == -1)
Thomas Betker19f22ef2015-03-12 22:11:59 +0100658 writel(CDNS_UART_CR_STARTBRK | status,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800659 port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400660 else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700661 if ((status & CDNS_UART_CR_STOPBRK) == 0)
Thomas Betker19f22ef2015-03-12 22:11:59 +0100662 writel(CDNS_UART_CR_STOPBRK | status,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800663 port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400664 }
665 spin_unlock_irqrestore(&port->lock, flags);
666}
667
668/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700669 * cdns_uart_set_termios - termios operations, handling data length, parity,
John Linn61ec9012011-04-30 00:07:43 -0400670 * stop bits, flow control, baud rate
671 * @port: Handle to the uart port structure
672 * @termios: Handle to the input termios structure
673 * @old: Values of the previously saved termios structure
Michal Simek489810a12014-04-04 17:23:37 -0700674 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700675static void cdns_uart_set_termios(struct uart_port *port,
John Linn61ec9012011-04-30 00:07:43 -0400676 struct ktermios *termios, struct ktermios *old)
677{
678 unsigned int cval = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700679 unsigned int baud, minbaud, maxbaud;
John Linn61ec9012011-04-30 00:07:43 -0400680 unsigned long flags;
681 unsigned int ctrl_reg, mode_reg;
682
683 spin_lock_irqsave(&port->lock, flags);
684
Nathan Rossi6ecde472015-01-16 13:49:25 +0100685 /* Wait for the transmit FIFO to empty before making changes */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800686 if (!(readl(port->membase + CDNS_UART_CR) &
Thomas Betker19f22ef2015-03-12 22:11:59 +0100687 CDNS_UART_CR_TX_DIS)) {
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800688 while (!(readl(port->membase + CDNS_UART_SR) &
Nathan Rossi6ecde472015-01-16 13:49:25 +0100689 CDNS_UART_SR_TXEMPTY)) {
690 cpu_relax();
691 }
John Linn61ec9012011-04-30 00:07:43 -0400692 }
693
694 /* Disable the TX and RX to set baud rate */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800695 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700696 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800697 writel(ctrl_reg, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400698
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700699 /*
700 * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
701 * min and max baud should be calculated here based on port->uartclk.
702 * this way we get a valid baud and can safely call set_baud()
703 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700704 minbaud = port->uartclk /
705 ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
706 maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700707 baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700708 baud = cdns_uart_set_baud_rate(port, baud);
John Linn61ec9012011-04-30 00:07:43 -0400709 if (tty_termios_baud_rate(termios))
710 tty_termios_encode_baud_rate(termios, baud, baud);
711
Soren Brinkmanne555a212014-04-04 17:23:39 -0700712 /* Update the per-port timeout. */
John Linn61ec9012011-04-30 00:07:43 -0400713 uart_update_timeout(port, termios->c_cflag, baud);
714
715 /* Set TX/RX Reset */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800716 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700717 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800718 writel(ctrl_reg, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400719
Nava kishore Manne27b17ae2016-09-15 14:45:31 +0530720 while (readl(port->membase + CDNS_UART_CR) &
721 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
722 cpu_relax();
723
Soren Brinkmanne555a212014-04-04 17:23:39 -0700724 /*
725 * Clear the RX disable and TX disable bits and then set the TX enable
John Linn61ec9012011-04-30 00:07:43 -0400726 * bit and RX enable bit to enable the transmitter and receiver.
727 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800728 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700729 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
730 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800731 writel(ctrl_reg, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400732
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800733 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
John Linn61ec9012011-04-30 00:07:43 -0400734
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700735 port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
736 CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
John Linn61ec9012011-04-30 00:07:43 -0400737 port->ignore_status_mask = 0;
738
739 if (termios->c_iflag & INPCK)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700740 port->read_status_mask |= CDNS_UART_IXR_PARITY |
741 CDNS_UART_IXR_FRAMING;
John Linn61ec9012011-04-30 00:07:43 -0400742
743 if (termios->c_iflag & IGNPAR)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700744 port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
745 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400746
747 /* ignore all characters if CREAD is not set */
748 if ((termios->c_cflag & CREAD) == 0)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700749 port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
750 CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
751 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400752
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800753 mode_reg = readl(port->membase + CDNS_UART_MR);
John Linn61ec9012011-04-30 00:07:43 -0400754
755 /* Handling Data Size */
756 switch (termios->c_cflag & CSIZE) {
757 case CS6:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700758 cval |= CDNS_UART_MR_CHARLEN_6_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400759 break;
760 case CS7:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700761 cval |= CDNS_UART_MR_CHARLEN_7_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400762 break;
763 default:
764 case CS8:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700765 cval |= CDNS_UART_MR_CHARLEN_8_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400766 termios->c_cflag &= ~CSIZE;
767 termios->c_cflag |= CS8;
768 break;
769 }
770
771 /* Handling Parity and Stop Bits length */
772 if (termios->c_cflag & CSTOPB)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700773 cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
John Linn61ec9012011-04-30 00:07:43 -0400774 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700775 cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
John Linn61ec9012011-04-30 00:07:43 -0400776
777 if (termios->c_cflag & PARENB) {
778 /* Mark or Space parity */
779 if (termios->c_cflag & CMSPAR) {
780 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700781 cval |= CDNS_UART_MR_PARITY_MARK;
John Linn61ec9012011-04-30 00:07:43 -0400782 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700783 cval |= CDNS_UART_MR_PARITY_SPACE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700784 } else {
785 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700786 cval |= CDNS_UART_MR_PARITY_ODD;
John Linn61ec9012011-04-30 00:07:43 -0400787 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700788 cval |= CDNS_UART_MR_PARITY_EVEN;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700789 }
790 } else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700791 cval |= CDNS_UART_MR_PARITY_NONE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700792 }
793 cval |= mode_reg & 1;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800794 writel(cval, port->membase + CDNS_UART_MR);
John Linn61ec9012011-04-30 00:07:43 -0400795
796 spin_unlock_irqrestore(&port->lock, flags);
797}
798
799/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700800 * cdns_uart_startup - Called when an application opens a cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400801 * @port: Handle to the uart port structure
802 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700803 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -0700804 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700805static int cdns_uart_startup(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400806{
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530807 struct cdns_uart *cdns_uart = port->private_data;
808 bool is_brk_support;
Soren Brinkmann55861d12016-01-11 17:41:36 -0800809 int ret;
Soren Brinkmann6e14f7c2015-12-26 02:43:53 -0800810 unsigned long flags;
Soren Brinkmann55861d12016-01-11 17:41:36 -0800811 unsigned int status = 0;
John Linn61ec9012011-04-30 00:07:43 -0400812
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530813 is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
814
Soren Brinkmann6e14f7c2015-12-26 02:43:53 -0800815 spin_lock_irqsave(&port->lock, flags);
816
John Linn61ec9012011-04-30 00:07:43 -0400817 /* Disable the TX and RX */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100818 writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800819 port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400820
821 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
822 * no break chars.
823 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100824 writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800825 port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400826
Nava kishore Manne27b17ae2016-09-15 14:45:31 +0530827 while (readl(port->membase + CDNS_UART_CR) &
828 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
829 cpu_relax();
830
Soren Brinkmann6e14f7c2015-12-26 02:43:53 -0800831 /*
832 * Clear the RX disable bit and then set the RX enable bit to enable
833 * the receiver.
John Linn61ec9012011-04-30 00:07:43 -0400834 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800835 status = readl(port->membase + CDNS_UART_CR);
Soren Brinkmann6e14f7c2015-12-26 02:43:53 -0800836 status &= CDNS_UART_CR_RX_DIS;
837 status |= CDNS_UART_CR_RX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800838 writel(status, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400839
840 /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
841 * no parity.
842 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100843 writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700844 | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800845 port->membase + CDNS_UART_MR);
John Linn61ec9012011-04-30 00:07:43 -0400846
Suneel85baf542013-10-17 14:08:08 -0700847 /*
848 * Set the RX FIFO Trigger level to use most of the FIFO, but it
849 * can be tuned with a module parameter
850 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800851 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
John Linn61ec9012011-04-30 00:07:43 -0400852
Suneel85baf542013-10-17 14:08:08 -0700853 /*
854 * Receive Timeout register is enabled but it
855 * can be tuned with a module parameter
856 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800857 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
John Linn61ec9012011-04-30 00:07:43 -0400858
John Linn855f6fd2013-03-22 18:49:27 +0100859 /* Clear out any pending interrupts before enabling them */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800860 writel(readl(port->membase + CDNS_UART_ISR),
861 port->membase + CDNS_UART_ISR);
John Linn61ec9012011-04-30 00:07:43 -0400862
Soren Brinkmann55861d12016-01-11 17:41:36 -0800863 spin_unlock_irqrestore(&port->lock, flags);
864
865 ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port);
866 if (ret) {
867 dev_err(port->dev, "request_irq '%d' failed with %d\n",
868 port->irq, ret);
869 return ret;
870 }
871
John Linn61ec9012011-04-30 00:07:43 -0400872 /* Set the Interrupt Registers with desired interrupts */
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530873 if (is_brk_support)
874 writel(CDNS_UART_RX_IRQS | CDNS_UART_IXR_BRK,
875 port->membase + CDNS_UART_IER);
876 else
877 writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER);
John Linn61ec9012011-04-30 00:07:43 -0400878
Soren Brinkmann55861d12016-01-11 17:41:36 -0800879 return 0;
John Linn61ec9012011-04-30 00:07:43 -0400880}
881
882/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700883 * cdns_uart_shutdown - Called when an application closes a cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400884 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700885 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700886static void cdns_uart_shutdown(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400887{
888 int status;
Soren Brinkmanna19eda02015-12-26 02:43:55 -0800889 unsigned long flags;
890
891 spin_lock_irqsave(&port->lock, flags);
John Linn61ec9012011-04-30 00:07:43 -0400892
893 /* Disable interrupts */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800894 status = readl(port->membase + CDNS_UART_IMR);
895 writel(status, port->membase + CDNS_UART_IDR);
896 writel(0xffffffff, port->membase + CDNS_UART_ISR);
John Linn61ec9012011-04-30 00:07:43 -0400897
898 /* Disable the TX and RX */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100899 writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800900 port->membase + CDNS_UART_CR);
Soren Brinkmanna19eda02015-12-26 02:43:55 -0800901
902 spin_unlock_irqrestore(&port->lock, flags);
903
John Linn61ec9012011-04-30 00:07:43 -0400904 free_irq(port->irq, port);
905}
906
907/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700908 * cdns_uart_type - Set UART type to cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400909 * @port: Handle to the uart port structure
910 *
Michal Simek489810a12014-04-04 17:23:37 -0700911 * Return: string on success, NULL otherwise
912 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700913static const char *cdns_uart_type(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400914{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700915 return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
John Linn61ec9012011-04-30 00:07:43 -0400916}
917
918/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700919 * cdns_uart_verify_port - Verify the port params
John Linn61ec9012011-04-30 00:07:43 -0400920 * @port: Handle to the uart port structure
921 * @ser: Handle to the structure whose members are compared
922 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700923 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -0700924 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700925static int cdns_uart_verify_port(struct uart_port *port,
John Linn61ec9012011-04-30 00:07:43 -0400926 struct serial_struct *ser)
927{
928 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
929 return -EINVAL;
930 if (port->irq != ser->irq)
931 return -EINVAL;
932 if (ser->io_type != UPIO_MEM)
933 return -EINVAL;
934 if (port->iobase != ser->port)
935 return -EINVAL;
936 if (ser->hub6 != 0)
937 return -EINVAL;
938 return 0;
939}
940
941/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700942 * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
943 * called when the driver adds a cdns_uart port via
John Linn61ec9012011-04-30 00:07:43 -0400944 * uart_add_one_port()
945 * @port: Handle to the uart port structure
946 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700947 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -0700948 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700949static int cdns_uart_request_port(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400950{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700951 if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
952 CDNS_UART_NAME)) {
John Linn61ec9012011-04-30 00:07:43 -0400953 return -ENOMEM;
954 }
955
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700956 port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400957 if (!port->membase) {
958 dev_err(port->dev, "Unable to map registers\n");
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700959 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400960 return -ENOMEM;
961 }
962 return 0;
963}
964
965/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700966 * cdns_uart_release_port - Release UART port
John Linn61ec9012011-04-30 00:07:43 -0400967 * @port: Handle to the uart port structure
Soren Brinkmanne555a212014-04-04 17:23:39 -0700968 *
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700969 * Release the memory region attached to a cdns_uart port. Called when the
970 * driver removes a cdns_uart port via uart_remove_one_port().
Michal Simek489810a12014-04-04 17:23:37 -0700971 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700972static void cdns_uart_release_port(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400973{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700974 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400975 iounmap(port->membase);
976 port->membase = NULL;
977}
978
979/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700980 * cdns_uart_config_port - Configure UART port
John Linn61ec9012011-04-30 00:07:43 -0400981 * @port: Handle to the uart port structure
982 * @flags: If any
Michal Simek489810a12014-04-04 17:23:37 -0700983 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700984static void cdns_uart_config_port(struct uart_port *port, int flags)
John Linn61ec9012011-04-30 00:07:43 -0400985{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700986 if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
John Linn61ec9012011-04-30 00:07:43 -0400987 port->type = PORT_XUARTPS;
988}
989
990/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700991 * cdns_uart_get_mctrl - Get the modem control state
John Linn61ec9012011-04-30 00:07:43 -0400992 * @port: Handle to the uart port structure
993 *
Michal Simek489810a12014-04-04 17:23:37 -0700994 * Return: the modem control state
995 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700996static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400997{
998 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
999}
1000
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001001static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
John Linn61ec9012011-04-30 00:07:43 -04001002{
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001003 u32 val;
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001004 u32 mode_reg;
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001005
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001006 val = readl(port->membase + CDNS_UART_MODEMCR);
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001007 mode_reg = readl(port->membase + CDNS_UART_MR);
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001008
1009 val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001010 mode_reg &= ~CDNS_UART_MR_CHMODE_MASK;
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001011
1012 if (mctrl & TIOCM_RTS)
1013 val |= CDNS_UART_MODEMCR_RTS;
1014 if (mctrl & TIOCM_DTR)
1015 val |= CDNS_UART_MODEMCR_DTR;
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001016 if (mctrl & TIOCM_LOOP)
1017 mode_reg |= CDNS_UART_MR_CHMODE_L_LOOP;
1018 else
1019 mode_reg |= CDNS_UART_MR_CHMODE_NORM;
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001020
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001021 writel(val, port->membase + CDNS_UART_MODEMCR);
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001022 writel(mode_reg, port->membase + CDNS_UART_MR);
John Linn61ec9012011-04-30 00:07:43 -04001023}
1024
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001025#ifdef CONFIG_CONSOLE_POLL
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001026static int cdns_uart_poll_get_char(struct uart_port *port)
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001027{
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001028 int c;
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001029 unsigned long flags;
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001030
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001031 spin_lock_irqsave(&port->lock, flags);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001032
1033 /* Check if FIFO is empty */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001034 if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001035 c = NO_POLL_CHAR;
1036 else /* Read a character */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001037 c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001038
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001039 spin_unlock_irqrestore(&port->lock, flags);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001040
1041 return c;
1042}
1043
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001044static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001045{
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001046 unsigned long flags;
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001047
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001048 spin_lock_irqsave(&port->lock, flags);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001049
1050 /* Wait until FIFO is empty */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001051 while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001052 cpu_relax();
1053
1054 /* Write a character */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001055 writel(c, port->membase + CDNS_UART_FIFO);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001056
1057 /* Wait until FIFO is empty */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001058 while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001059 cpu_relax();
1060
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001061 spin_unlock_irqrestore(&port->lock, flags);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001062
1063 return;
1064}
1065#endif
1066
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301067static void cdns_uart_pm(struct uart_port *port, unsigned int state,
1068 unsigned int oldstate)
1069{
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301070 switch (state) {
1071 case UART_PM_STATE_OFF:
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301072 pm_runtime_mark_last_busy(port->dev);
1073 pm_runtime_put_autosuspend(port->dev);
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301074 break;
1075 default:
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301076 pm_runtime_get_sync(port->dev);
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301077 break;
1078 }
1079}
1080
Julia Lawallf098a0a2016-09-01 19:51:36 +02001081static const struct uart_ops cdns_uart_ops = {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001082 .set_mctrl = cdns_uart_set_mctrl,
1083 .get_mctrl = cdns_uart_get_mctrl,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001084 .start_tx = cdns_uart_start_tx,
1085 .stop_tx = cdns_uart_stop_tx,
1086 .stop_rx = cdns_uart_stop_rx,
1087 .tx_empty = cdns_uart_tx_empty,
1088 .break_ctl = cdns_uart_break_ctl,
1089 .set_termios = cdns_uart_set_termios,
1090 .startup = cdns_uart_startup,
1091 .shutdown = cdns_uart_shutdown,
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301092 .pm = cdns_uart_pm,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001093 .type = cdns_uart_type,
1094 .verify_port = cdns_uart_verify_port,
1095 .request_port = cdns_uart_request_port,
1096 .release_port = cdns_uart_release_port,
1097 .config_port = cdns_uart_config_port,
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001098#ifdef CONFIG_CONSOLE_POLL
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001099 .poll_get_char = cdns_uart_poll_get_char,
1100 .poll_put_char = cdns_uart_poll_put_char,
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001101#endif
John Linn61ec9012011-04-30 00:07:43 -04001102};
1103
Thomas Betker6db6df02015-03-11 22:39:26 +01001104static struct uart_port cdns_uart_port[CDNS_UART_NR_PORTS];
John Linn61ec9012011-04-30 00:07:43 -04001105
1106/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001107 * cdns_uart_get_port - Configure the port from platform device resource info
Michal Simek928e9262014-04-04 17:23:38 -07001108 * @id: Port id
1109 *
Michal Simek489810a12014-04-04 17:23:37 -07001110 * Return: a pointer to a uart_port or NULL for failure
1111 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001112static struct uart_port *cdns_uart_get_port(int id)
John Linn61ec9012011-04-30 00:07:43 -04001113{
1114 struct uart_port *port;
John Linn61ec9012011-04-30 00:07:43 -04001115
Michal Simek928e9262014-04-04 17:23:38 -07001116 /* Try the given port id if failed use default method */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001117 if (cdns_uart_port[id].mapbase != 0) {
Michal Simek928e9262014-04-04 17:23:38 -07001118 /* Find the next unused port */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001119 for (id = 0; id < CDNS_UART_NR_PORTS; id++)
1120 if (cdns_uart_port[id].mapbase == 0)
Michal Simek928e9262014-04-04 17:23:38 -07001121 break;
1122 }
John Linn61ec9012011-04-30 00:07:43 -04001123
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001124 if (id >= CDNS_UART_NR_PORTS)
John Linn61ec9012011-04-30 00:07:43 -04001125 return NULL;
1126
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001127 port = &cdns_uart_port[id];
John Linn61ec9012011-04-30 00:07:43 -04001128
1129 /* At this point, we've got an empty uart_port struct, initialize it */
1130 spin_lock_init(&port->lock);
1131 port->membase = NULL;
John Linn61ec9012011-04-30 00:07:43 -04001132 port->irq = 0;
1133 port->type = PORT_UNKNOWN;
1134 port->iotype = UPIO_MEM32;
1135 port->flags = UPF_BOOT_AUTOCONF;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001136 port->ops = &cdns_uart_ops;
1137 port->fifosize = CDNS_UART_FIFO_SIZE;
John Linn61ec9012011-04-30 00:07:43 -04001138 port->line = id;
1139 port->dev = NULL;
1140 return port;
1141}
1142
John Linn61ec9012011-04-30 00:07:43 -04001143#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1144/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001145 * cdns_uart_console_wait_tx - Wait for the TX to be full
John Linn61ec9012011-04-30 00:07:43 -04001146 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -07001147 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001148static void cdns_uart_console_wait_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -04001149{
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001150 while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
John Linn61ec9012011-04-30 00:07:43 -04001151 barrier();
1152}
1153
1154/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001155 * cdns_uart_console_putchar - write the character to the FIFO buffer
John Linn61ec9012011-04-30 00:07:43 -04001156 * @port: Handle to the uart port structure
1157 * @ch: Character to be written
Michal Simek489810a12014-04-04 17:23:37 -07001158 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001159static void cdns_uart_console_putchar(struct uart_port *port, int ch)
John Linn61ec9012011-04-30 00:07:43 -04001160{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001161 cdns_uart_console_wait_tx(port);
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001162 writel(ch, port->membase + CDNS_UART_FIFO);
John Linn61ec9012011-04-30 00:07:43 -04001163}
1164
Masahiro Yamada54585ba2015-05-07 18:55:40 +09001165static void __init cdns_early_write(struct console *con, const char *s,
1166 unsigned n)
Michal Simek6fa62fc2014-09-10 12:43:02 +02001167{
1168 struct earlycon_device *dev = con->data;
1169
1170 uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
1171}
1172
1173static int __init cdns_early_console_setup(struct earlycon_device *device,
1174 const char *opt)
1175{
Scott Telfordc41251b2016-09-22 16:58:16 +01001176 struct uart_port *port = &device->port;
1177
1178 if (!port->membase)
Michal Simek6fa62fc2014-09-10 12:43:02 +02001179 return -ENODEV;
1180
Scott Telfordc41251b2016-09-22 16:58:16 +01001181 /* initialise control register */
1182 writel(CDNS_UART_CR_TX_EN|CDNS_UART_CR_TXRST|CDNS_UART_CR_RXRST,
1183 port->membase + CDNS_UART_CR);
1184
1185 /* only set baud if specified on command line - otherwise
1186 * assume it has been initialized by a boot loader.
1187 */
1188 if (device->baud) {
1189 u32 cd = 0, bdiv = 0;
1190 u32 mr;
1191 int div8;
1192
1193 cdns_uart_calc_baud_divs(port->uartclk, device->baud,
1194 &bdiv, &cd, &div8);
1195 mr = CDNS_UART_MR_PARITY_NONE;
1196 if (div8)
1197 mr |= CDNS_UART_MR_CLKSEL;
1198
1199 writel(mr, port->membase + CDNS_UART_MR);
1200 writel(cd, port->membase + CDNS_UART_BAUDGEN);
1201 writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
1202 }
1203
Michal Simek6fa62fc2014-09-10 12:43:02 +02001204 device->con->write = cdns_early_write;
1205
1206 return 0;
1207}
Michal Simek93d7bba2016-02-18 08:57:20 +01001208OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
1209OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
1210OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
Nava kishore Manne0267a4f2016-10-12 13:17:27 +05301211OF_EARLYCON_DECLARE(cdns, "xlnx,zynqmp-uart", cdns_early_console_setup);
Michal Simek6fa62fc2014-09-10 12:43:02 +02001212
John Linn61ec9012011-04-30 00:07:43 -04001213/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001214 * cdns_uart_console_write - perform write operation
Michal Simek489810a12014-04-04 17:23:37 -07001215 * @co: Console handle
John Linn61ec9012011-04-30 00:07:43 -04001216 * @s: Pointer to character array
1217 * @count: No of characters
Michal Simek489810a12014-04-04 17:23:37 -07001218 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001219static void cdns_uart_console_write(struct console *co, const char *s,
John Linn61ec9012011-04-30 00:07:43 -04001220 unsigned int count)
1221{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001222 struct uart_port *port = &cdns_uart_port[co->index];
John Linn61ec9012011-04-30 00:07:43 -04001223 unsigned long flags;
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001224 unsigned int imr, ctrl;
John Linn61ec9012011-04-30 00:07:43 -04001225 int locked = 1;
1226
Soren Brinkmann74ea66d42016-01-11 17:41:39 -08001227 if (port->sysrq)
1228 locked = 0;
1229 else if (oops_in_progress)
John Linn61ec9012011-04-30 00:07:43 -04001230 locked = spin_trylock_irqsave(&port->lock, flags);
1231 else
1232 spin_lock_irqsave(&port->lock, flags);
1233
1234 /* save and disable interrupt */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001235 imr = readl(port->membase + CDNS_UART_IMR);
1236 writel(imr, port->membase + CDNS_UART_IDR);
John Linn61ec9012011-04-30 00:07:43 -04001237
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001238 /*
1239 * Make sure that the tx part is enabled. Set the TX enable bit and
1240 * clear the TX disable bit to enable the transmitter.
1241 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001242 ctrl = readl(port->membase + CDNS_UART_CR);
Soren Brinkmanne3538c32015-12-26 02:43:49 -08001243 ctrl &= ~CDNS_UART_CR_TX_DIS;
1244 ctrl |= CDNS_UART_CR_TX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001245 writel(ctrl, port->membase + CDNS_UART_CR);
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001246
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001247 uart_console_write(port, s, count, cdns_uart_console_putchar);
1248 cdns_uart_console_wait_tx(port);
John Linn61ec9012011-04-30 00:07:43 -04001249
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001250 writel(ctrl, port->membase + CDNS_UART_CR);
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001251
Soren Brinkmannb494a5f2014-04-04 17:23:42 -07001252 /* restore interrupt state */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001253 writel(imr, port->membase + CDNS_UART_IER);
John Linn61ec9012011-04-30 00:07:43 -04001254
1255 if (locked)
1256 spin_unlock_irqrestore(&port->lock, flags);
1257}
1258
1259/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001260 * cdns_uart_console_setup - Initialize the uart to default config
John Linn61ec9012011-04-30 00:07:43 -04001261 * @co: Console handle
1262 * @options: Initial settings of uart
1263 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001264 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -07001265 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001266static int __init cdns_uart_console_setup(struct console *co, char *options)
John Linn61ec9012011-04-30 00:07:43 -04001267{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001268 struct uart_port *port = &cdns_uart_port[co->index];
John Linn61ec9012011-04-30 00:07:43 -04001269 int baud = 9600;
1270 int bits = 8;
1271 int parity = 'n';
1272 int flow = 'n';
1273
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001274 if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
John Linn61ec9012011-04-30 00:07:43 -04001275 return -EINVAL;
1276
Thomas Betker136debf2015-03-11 22:39:28 +01001277 if (!port->membase) {
Peter Crosthwaitef6415492015-02-24 15:13:57 -08001278 pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
1279 co->index);
John Linn61ec9012011-04-30 00:07:43 -04001280 return -ENODEV;
1281 }
1282
1283 if (options)
1284 uart_parse_options(options, &baud, &parity, &bits, &flow);
1285
1286 return uart_set_options(port, co, baud, parity, bits, flow);
1287}
1288
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001289static struct uart_driver cdns_uart_uart_driver;
John Linn61ec9012011-04-30 00:07:43 -04001290
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001291static struct console cdns_uart_console = {
1292 .name = CDNS_UART_TTY_NAME,
1293 .write = cdns_uart_console_write,
John Linn61ec9012011-04-30 00:07:43 -04001294 .device = uart_console_device,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001295 .setup = cdns_uart_console_setup,
John Linn61ec9012011-04-30 00:07:43 -04001296 .flags = CON_PRINTBUFFER,
1297 .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001298 .data = &cdns_uart_uart_driver,
John Linn61ec9012011-04-30 00:07:43 -04001299};
1300
1301/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001302 * cdns_uart_console_init - Initialization call
John Linn61ec9012011-04-30 00:07:43 -04001303 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001304 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001305 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001306static int __init cdns_uart_console_init(void)
John Linn61ec9012011-04-30 00:07:43 -04001307{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001308 register_console(&cdns_uart_console);
John Linn61ec9012011-04-30 00:07:43 -04001309 return 0;
1310}
1311
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001312console_initcall(cdns_uart_console_init);
John Linn61ec9012011-04-30 00:07:43 -04001313
1314#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
1315
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001316static struct uart_driver cdns_uart_uart_driver = {
Soren Brinkmanne555a212014-04-04 17:23:39 -07001317 .owner = THIS_MODULE,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001318 .driver_name = CDNS_UART_NAME,
1319 .dev_name = CDNS_UART_TTY_NAME,
1320 .major = CDNS_UART_MAJOR,
1321 .minor = CDNS_UART_MINOR,
1322 .nr = CDNS_UART_NR_PORTS,
Soren Brinkmannd3641f62013-10-21 16:41:00 -07001323#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001324 .cons = &cdns_uart_console,
Soren Brinkmannd3641f62013-10-21 16:41:00 -07001325#endif
1326};
1327
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001328#ifdef CONFIG_PM_SLEEP
1329/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001330 * cdns_uart_suspend - suspend event
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001331 * @device: Pointer to the device structure
1332 *
Michal Simek489810a12014-04-04 17:23:37 -07001333 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001334 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001335static int cdns_uart_suspend(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001336{
1337 struct uart_port *port = dev_get_drvdata(device);
1338 struct tty_struct *tty;
1339 struct device *tty_dev;
1340 int may_wake = 0;
1341
1342 /* Get the tty which could be NULL so don't assume it's valid */
1343 tty = tty_port_tty_get(&port->state->port);
1344 if (tty) {
1345 tty_dev = tty->dev;
1346 may_wake = device_may_wakeup(tty_dev);
1347 tty_kref_put(tty);
1348 }
1349
1350 /*
1351 * Call the API provided in serial_core.c file which handles
1352 * the suspend.
1353 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001354 uart_suspend_port(&cdns_uart_uart_driver, port);
Shubhrajyoti Datta81e33b52017-03-15 08:38:56 -07001355 if (!(console_suspend_enabled && !may_wake)) {
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001356 unsigned long flags = 0;
1357
1358 spin_lock_irqsave(&port->lock, flags);
1359 /* Empty the receive FIFO 1st before making changes */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001360 while (!(readl(port->membase + CDNS_UART_SR) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001361 CDNS_UART_SR_RXEMPTY))
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001362 readl(port->membase + CDNS_UART_FIFO);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001363 /* set RX trigger level to 1 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001364 writel(1, port->membase + CDNS_UART_RXWM);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001365 /* disable RX timeout interrups */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001366 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001367 spin_unlock_irqrestore(&port->lock, flags);
1368 }
1369
1370 return 0;
1371}
1372
1373/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001374 * cdns_uart_resume - Resume after a previous suspend
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001375 * @device: Pointer to the device structure
1376 *
Michal Simek489810a12014-04-04 17:23:37 -07001377 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001378 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001379static int cdns_uart_resume(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001380{
1381 struct uart_port *port = dev_get_drvdata(device);
1382 unsigned long flags = 0;
1383 u32 ctrl_reg;
1384 struct tty_struct *tty;
1385 struct device *tty_dev;
1386 int may_wake = 0;
1387
1388 /* Get the tty which could be NULL so don't assume it's valid */
1389 tty = tty_port_tty_get(&port->state->port);
1390 if (tty) {
1391 tty_dev = tty->dev;
1392 may_wake = device_may_wakeup(tty_dev);
1393 tty_kref_put(tty);
1394 }
1395
1396 if (console_suspend_enabled && !may_wake) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001397 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001398
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001399 clk_enable(cdns_uart->pclk);
1400 clk_enable(cdns_uart->uartclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001401
1402 spin_lock_irqsave(&port->lock, flags);
1403
1404 /* Set TX/RX Reset */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001405 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001406 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001407 writel(ctrl_reg, port->membase + CDNS_UART_CR);
1408 while (readl(port->membase + CDNS_UART_CR) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001409 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001410 cpu_relax();
1411
1412 /* restore rx timeout value */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001413 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001414 /* Enable Tx/Rx */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001415 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001416 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1417 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001418 writel(ctrl_reg, port->membase + CDNS_UART_CR);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001419
Shubhrajyoti Datta81e33b52017-03-15 08:38:56 -07001420 clk_disable(cdns_uart->uartclk);
1421 clk_disable(cdns_uart->pclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001422 spin_unlock_irqrestore(&port->lock, flags);
1423 } else {
1424 spin_lock_irqsave(&port->lock, flags);
1425 /* restore original rx trigger level */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001426 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001427 /* enable RX timeout interrupt */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001428 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001429 spin_unlock_irqrestore(&port->lock, flags);
1430 }
1431
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001432 return uart_resume_port(&cdns_uart_uart_driver, port);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001433}
1434#endif /* ! CONFIG_PM_SLEEP */
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301435static int __maybe_unused cdns_runtime_suspend(struct device *dev)
1436{
1437 struct platform_device *pdev = to_platform_device(dev);
1438 struct uart_port *port = platform_get_drvdata(pdev);
1439 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001440
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301441 clk_disable(cdns_uart->uartclk);
1442 clk_disable(cdns_uart->pclk);
1443 return 0;
1444};
1445
1446static int __maybe_unused cdns_runtime_resume(struct device *dev)
1447{
1448 struct platform_device *pdev = to_platform_device(dev);
1449 struct uart_port *port = platform_get_drvdata(pdev);
1450 struct cdns_uart *cdns_uart = port->private_data;
1451
1452 clk_enable(cdns_uart->pclk);
1453 clk_enable(cdns_uart->uartclk);
1454 return 0;
1455};
1456
1457static const struct dev_pm_ops cdns_uart_dev_pm_ops = {
1458 SET_SYSTEM_SLEEP_PM_OPS(cdns_uart_suspend, cdns_uart_resume)
1459 SET_RUNTIME_PM_OPS(cdns_runtime_suspend,
1460 cdns_runtime_resume, NULL)
1461};
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001462
Nava kishore Manne3816b2f82016-09-15 14:45:29 +05301463static const struct cdns_platform_data zynqmp_uart_def = {
1464 .quirks = CDNS_UART_RXBS_SUPPORT, };
1465
1466/* Match table for of_platform binding */
1467static const struct of_device_id cdns_uart_of_match[] = {
1468 { .compatible = "xlnx,xuartps", },
1469 { .compatible = "cdns,uart-r1p8", },
1470 { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
Nava kishore Manne0267a4f2016-10-12 13:17:27 +05301471 { .compatible = "xlnx,zynqmp-uart", .data = &zynqmp_uart_def },
Nava kishore Manne3816b2f82016-09-15 14:45:29 +05301472 {}
1473};
1474MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
1475
John Linn61ec9012011-04-30 00:07:43 -04001476/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001477 * cdns_uart_probe - Platform driver probe
John Linn61ec9012011-04-30 00:07:43 -04001478 * @pdev: Pointer to the platform device structure
1479 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001480 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001481 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001482static int cdns_uart_probe(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -04001483{
Michal Simek5c90c072015-04-13 16:34:21 +02001484 int rc, id, irq;
John Linn61ec9012011-04-30 00:07:43 -04001485 struct uart_port *port;
Michal Simek5c90c072015-04-13 16:34:21 +02001486 struct resource *res;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001487 struct cdns_uart *cdns_uart_data;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +05301488 const struct of_device_id *match;
John Linn61ec9012011-04-30 00:07:43 -04001489
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001490 cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
Soren Brinkmannc03cae12013-10-17 14:08:05 -07001491 GFP_KERNEL);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001492 if (!cdns_uart_data)
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001493 return -ENOMEM;
1494
Nava kishore Manne3816b2f82016-09-15 14:45:29 +05301495 match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
1496 if (match && match->data) {
1497 const struct cdns_platform_data *data = match->data;
1498
1499 cdns_uart_data->quirks = data->quirks;
1500 }
1501
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001502 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
1503 if (IS_ERR(cdns_uart_data->pclk)) {
1504 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1505 if (!IS_ERR(cdns_uart_data->pclk))
1506 dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001507 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001508 if (IS_ERR(cdns_uart_data->pclk)) {
1509 dev_err(&pdev->dev, "pclk clock not found.\n");
1510 return PTR_ERR(cdns_uart_data->pclk);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001511 }
1512
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001513 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1514 if (IS_ERR(cdns_uart_data->uartclk)) {
1515 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1516 if (!IS_ERR(cdns_uart_data->uartclk))
1517 dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1518 }
1519 if (IS_ERR(cdns_uart_data->uartclk)) {
1520 dev_err(&pdev->dev, "uart_clk clock not found.\n");
1521 return PTR_ERR(cdns_uart_data->uartclk);
1522 }
1523
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301524 rc = clk_prepare(cdns_uart_data->pclk);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001525 if (rc) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001526 dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
Soren Brinkmannc03cae12013-10-17 14:08:05 -07001527 return rc;
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001528 }
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301529 rc = clk_prepare(cdns_uart_data->uartclk);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001530 if (rc) {
1531 dev_err(&pdev->dev, "Unable to enable device clock.\n");
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001532 goto err_out_clk_dis_pclk;
John Linn61ec9012011-04-30 00:07:43 -04001533 }
1534
1535 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001536 if (!res) {
1537 rc = -ENODEV;
1538 goto err_out_clk_disable;
1539 }
John Linn61ec9012011-04-30 00:07:43 -04001540
Michal Simek5c90c072015-04-13 16:34:21 +02001541 irq = platform_get_irq(pdev, 0);
1542 if (irq <= 0) {
1543 rc = -ENXIO;
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001544 goto err_out_clk_disable;
1545 }
John Linn61ec9012011-04-30 00:07:43 -04001546
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001547#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001548 cdns_uart_data->clk_rate_change_nb.notifier_call =
1549 cdns_uart_clk_notifier_cb;
1550 if (clk_notifier_register(cdns_uart_data->uartclk,
1551 &cdns_uart_data->clk_rate_change_nb))
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001552 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001553#endif
Michal Simek928e9262014-04-04 17:23:38 -07001554 /* Look for a serialN alias */
1555 id = of_alias_get_id(pdev->dev.of_node, "serial");
1556 if (id < 0)
1557 id = 0;
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001558
John Linn61ec9012011-04-30 00:07:43 -04001559 /* Initialize the port structure */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001560 port = cdns_uart_get_port(id);
John Linn61ec9012011-04-30 00:07:43 -04001561
1562 if (!port) {
1563 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001564 rc = -ENODEV;
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001565 goto err_out_notif_unreg;
John Linn61ec9012011-04-30 00:07:43 -04001566 }
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001567
Soren Brinkmann354fb1a2016-01-11 17:41:38 -08001568 /*
1569 * Register the port.
1570 * This function also registers this device with the tty layer
1571 * and triggers invocation of the config_port() entry point.
1572 */
1573 port->mapbase = res->start;
1574 port->irq = irq;
1575 port->dev = &pdev->dev;
1576 port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1577 port->private_data = cdns_uart_data;
1578 cdns_uart_data->port = port;
1579 platform_set_drvdata(pdev, port);
1580
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301581 pm_runtime_use_autosuspend(&pdev->dev);
1582 pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
1583 pm_runtime_enable(&pdev->dev);
1584 pm_runtime_set_active(&pdev->dev);
1585
Soren Brinkmann354fb1a2016-01-11 17:41:38 -08001586 rc = uart_add_one_port(&cdns_uart_uart_driver, port);
1587 if (rc) {
1588 dev_err(&pdev->dev,
1589 "uart_add_one_port() failed; err=%i\n", rc);
1590 goto err_out_notif_unreg;
1591 }
1592
1593 return 0;
1594
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001595err_out_notif_unreg:
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001596#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001597 clk_notifier_unregister(cdns_uart_data->uartclk,
1598 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001599#endif
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001600err_out_clk_disable:
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301601 pm_runtime_disable(&pdev->dev);
1602 pm_runtime_set_suspended(&pdev->dev);
1603 pm_runtime_dont_use_autosuspend(&pdev->dev);
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301604 clk_unprepare(cdns_uart_data->uartclk);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001605err_out_clk_dis_pclk:
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301606 clk_unprepare(cdns_uart_data->pclk);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001607
1608 return rc;
John Linn61ec9012011-04-30 00:07:43 -04001609}
1610
1611/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001612 * cdns_uart_remove - called when the platform driver is unregistered
John Linn61ec9012011-04-30 00:07:43 -04001613 * @pdev: Pointer to the platform device structure
1614 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001615 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001616 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001617static int cdns_uart_remove(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -04001618{
Jingoo Han696faed2013-05-23 19:39:36 +09001619 struct uart_port *port = platform_get_drvdata(pdev);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001620 struct cdns_uart *cdns_uart_data = port->private_data;
Josh Cartwright2326669c2013-01-21 19:57:41 +01001621 int rc;
John Linn61ec9012011-04-30 00:07:43 -04001622
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001623 /* Remove the cdns_uart port from the serial core */
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001624#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001625 clk_notifier_unregister(cdns_uart_data->uartclk,
1626 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001627#endif
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001628 rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001629 port->mapbase = 0;
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301630 clk_unprepare(cdns_uart_data->uartclk);
1631 clk_unprepare(cdns_uart_data->pclk);
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301632 pm_runtime_disable(&pdev->dev);
1633 pm_runtime_set_suspended(&pdev->dev);
1634 pm_runtime_dont_use_autosuspend(&pdev->dev);
John Linn61ec9012011-04-30 00:07:43 -04001635 return rc;
1636}
1637
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001638static struct platform_driver cdns_uart_platform_driver = {
1639 .probe = cdns_uart_probe,
1640 .remove = cdns_uart_remove,
John Linn61ec9012011-04-30 00:07:43 -04001641 .driver = {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001642 .name = CDNS_UART_NAME,
1643 .of_match_table = cdns_uart_of_match,
1644 .pm = &cdns_uart_dev_pm_ops,
John Linn61ec9012011-04-30 00:07:43 -04001645 },
1646};
1647
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001648static int __init cdns_uart_init(void)
John Linn61ec9012011-04-30 00:07:43 -04001649{
1650 int retval = 0;
1651
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001652 /* Register the cdns_uart driver with the serial core */
1653 retval = uart_register_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001654 if (retval)
1655 return retval;
1656
1657 /* Register the platform driver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001658 retval = platform_driver_register(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001659 if (retval)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001660 uart_unregister_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001661
1662 return retval;
1663}
1664
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001665static void __exit cdns_uart_exit(void)
John Linn61ec9012011-04-30 00:07:43 -04001666{
John Linn61ec9012011-04-30 00:07:43 -04001667 /* Unregister the platform driver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001668 platform_driver_unregister(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001669
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001670 /* Unregister the cdns_uart driver */
1671 uart_unregister_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001672}
1673
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001674module_init(cdns_uart_init);
1675module_exit(cdns_uart_exit);
John Linn61ec9012011-04-30 00:07:43 -04001676
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001677MODULE_DESCRIPTION("Driver for Cadence UART");
John Linn61ec9012011-04-30 00:07:43 -04001678MODULE_AUTHOR("Xilinx Inc.");
1679MODULE_LICENSE("GPL");