blob: 4e55bc327a54758e8f9252e5a8ef188274ae05af [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
John Linn61ec9012011-04-30 00:07:43 -04002/*
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07003 * Cadence UART driver (found in Xilinx Zynq)
John Linn61ec9012011-04-30 00:07:43 -04004 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07005 * 2011 - 2014 (C) Xilinx Inc.
John Linn61ec9012011-04-30 00:07:43 -04006 *
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07007 * This driver has originally been pushed by Xilinx using a Zynq-branding. This
8 * still shows in the naming of this file, the kconfig symbols and some symbols
9 * in the code.
John Linn61ec9012011-04-30 00:07:43 -040010 */
11
Vlad Lungu0c0c47b2013-10-17 14:08:06 -070012#if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
13#define SUPPORT_SYSRQ
14#endif
15
John Linn61ec9012011-04-30 00:07:43 -040016#include <linux/platform_device.h>
John Linn61ec9012011-04-30 00:07:43 -040017#include <linux/serial.h>
Vlad Lungu0c0c47b2013-10-17 14:08:06 -070018#include <linux/console.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020019#include <linux/serial_core.h>
Soren Brinkmann30e1e282013-05-13 10:46:38 -070020#include <linux/slab.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020021#include <linux/tty.h>
22#include <linux/tty_flip.h>
Josh Cartwright2326669c2013-01-21 19:57:41 +010023#include <linux/clk.h>
John Linn61ec9012011-04-30 00:07:43 -040024#include <linux/irq.h>
25#include <linux/io.h>
26#include <linux/of.h>
Paul Gortmaker578b9ce2011-05-27 16:14:23 -040027#include <linux/module.h>
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +053028#include <linux/pm_runtime.h>
Shubhrajyoti Datta277375b2019-05-24 14:41:29 +053029#include <linux/iopoll.h>
John Linn61ec9012011-04-30 00:07:43 -040030
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070031#define CDNS_UART_TTY_NAME "ttyPS"
32#define CDNS_UART_NAME "xuartps"
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070033#define CDNS_UART_FIFO_SIZE 64 /* FIFO size */
Thomas Betker9646e4f2015-03-11 22:39:25 +010034#define CDNS_UART_REGISTER_SPACE 0x1000
Shubhrajyoti Datta277375b2019-05-24 14:41:29 +053035#define TX_TIMEOUT 500000
John Linn61ec9012011-04-30 00:07:43 -040036
Suneel85baf542013-10-17 14:08:08 -070037/* Rx Trigger level */
38static int rx_trigger_level = 56;
Shubhrajyoti Dattaab262662019-06-12 13:14:38 +020039static int uartps_major;
Nava kishore Manne26d5a322019-06-12 13:14:39 +020040module_param(rx_trigger_level, uint, 0444);
Suneel85baf542013-10-17 14:08:08 -070041MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
42
43/* Rx Timeout */
44static int rx_timeout = 10;
Nava kishore Manne26d5a322019-06-12 13:14:39 +020045module_param(rx_timeout, uint, 0444);
Suneel85baf542013-10-17 14:08:08 -070046MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
47
Soren Brinkmanne555a212014-04-04 17:23:39 -070048/* Register offsets for the UART. */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -080049#define CDNS_UART_CR 0x00 /* Control Register */
50#define CDNS_UART_MR 0x04 /* Mode Register */
51#define CDNS_UART_IER 0x08 /* Interrupt Enable */
52#define CDNS_UART_IDR 0x0C /* Interrupt Disable */
53#define CDNS_UART_IMR 0x10 /* Interrupt Mask */
54#define CDNS_UART_ISR 0x14 /* Interrupt Status */
55#define CDNS_UART_BAUDGEN 0x18 /* Baud Rate Generator */
Nava kishore Manne3816b2f82016-09-15 14:45:29 +053056#define CDNS_UART_RXTOUT 0x1C /* RX Timeout */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -080057#define CDNS_UART_RXWM 0x20 /* RX FIFO Trigger Level */
58#define CDNS_UART_MODEMCR 0x24 /* Modem Control */
59#define CDNS_UART_MODEMSR 0x28 /* Modem Status */
60#define CDNS_UART_SR 0x2C /* Channel Status */
61#define CDNS_UART_FIFO 0x30 /* FIFO */
62#define CDNS_UART_BAUDDIV 0x34 /* Baud Rate Divider */
63#define CDNS_UART_FLOWDEL 0x38 /* Flow Delay */
64#define CDNS_UART_IRRX_PWIDTH 0x3C /* IR Min Received Pulse Width */
65#define CDNS_UART_IRTX_PWIDTH 0x40 /* IR Transmitted pulse Width */
66#define CDNS_UART_TXWM 0x44 /* TX FIFO Trigger Level */
Nava kishore Manne3816b2f82016-09-15 14:45:29 +053067#define CDNS_UART_RXBS 0x48 /* RX FIFO byte status register */
John Linn61ec9012011-04-30 00:07:43 -040068
Soren Brinkmanne555a212014-04-04 17:23:39 -070069/* Control Register Bit Definitions */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070070#define CDNS_UART_CR_STOPBRK 0x00000100 /* Stop TX break */
71#define CDNS_UART_CR_STARTBRK 0x00000080 /* Set TX break */
72#define CDNS_UART_CR_TX_DIS 0x00000020 /* TX disabled. */
73#define CDNS_UART_CR_TX_EN 0x00000010 /* TX enabled */
74#define CDNS_UART_CR_RX_DIS 0x00000008 /* RX disabled. */
75#define CDNS_UART_CR_RX_EN 0x00000004 /* RX enabled */
76#define CDNS_UART_CR_TXRST 0x00000002 /* TX logic reset */
77#define CDNS_UART_CR_RXRST 0x00000001 /* RX logic reset */
78#define CDNS_UART_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
Nava kishore Manne3816b2f82016-09-15 14:45:29 +053079#define CDNS_UART_RXBS_PARITY 0x00000001 /* Parity error status */
80#define CDNS_UART_RXBS_FRAMING 0x00000002 /* Framing error status */
81#define CDNS_UART_RXBS_BRK 0x00000004 /* Overrun error status */
John Linn61ec9012011-04-30 00:07:43 -040082
Soren Brinkmanne555a212014-04-04 17:23:39 -070083/*
84 * Mode Register:
John Linn61ec9012011-04-30 00:07:43 -040085 * The mode register (MR) defines the mode of transfer as well as the data
86 * format. If this register is modified during transmission or reception,
87 * data validity cannot be guaranteed.
John Linn61ec9012011-04-30 00:07:43 -040088 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070089#define CDNS_UART_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
90#define CDNS_UART_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
91#define CDNS_UART_MR_CHMODE_NORM 0x00000000 /* Normal mode */
Yasir-Khan5935a2b2017-01-23 13:26:08 +010092#define CDNS_UART_MR_CHMODE_MASK 0x00000300 /* Mask for mode bits */
John Linn61ec9012011-04-30 00:07:43 -040093
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070094#define CDNS_UART_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
95#define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
John Linn61ec9012011-04-30 00:07:43 -040096
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070097#define CDNS_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
98#define CDNS_UART_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
99#define CDNS_UART_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
100#define CDNS_UART_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
101#define CDNS_UART_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
John Linn61ec9012011-04-30 00:07:43 -0400102
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700103#define CDNS_UART_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
104#define CDNS_UART_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
105#define CDNS_UART_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
John Linn61ec9012011-04-30 00:07:43 -0400106
Soren Brinkmanne555a212014-04-04 17:23:39 -0700107/*
108 * Interrupt Registers:
John Linn61ec9012011-04-30 00:07:43 -0400109 * Interrupt control logic uses the interrupt enable register (IER) and the
110 * interrupt disable register (IDR) to set the value of the bits in the
111 * interrupt mask register (IMR). The IMR determines whether to pass an
112 * interrupt to the interrupt status register (ISR).
113 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
114 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
115 * Reading either IER or IDR returns 0x00.
John Linn61ec9012011-04-30 00:07:43 -0400116 * All four registers have the same bit definitions.
117 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700118#define CDNS_UART_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
119#define CDNS_UART_IXR_PARITY 0x00000080 /* Parity error interrupt */
120#define CDNS_UART_IXR_FRAMING 0x00000040 /* Framing error interrupt */
121#define CDNS_UART_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
122#define CDNS_UART_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
123#define CDNS_UART_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
124#define CDNS_UART_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
125#define CDNS_UART_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
126#define CDNS_UART_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
127#define CDNS_UART_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
Nava kishore Manne26068312018-12-18 13:18:42 +0100128#define CDNS_UART_IXR_RXMASK 0x000021e7 /* Valid RX bit mask */
John Linn61ec9012011-04-30 00:07:43 -0400129
Anirudha Sarangia3081892016-09-21 15:01:18 +0100130 /*
131 * Do not enable parity error interrupt for the following
132 * reason: When parity error interrupt is enabled, each Rx
133 * parity error always results in 2 events. The first one
134 * being parity error interrupt and the second one with a
135 * proper Rx interrupt with the incoming data. Disabling
136 * parity error interrupt ensures better handling of parity
137 * error events. With this change, for a parity error case, we
138 * get a Rx interrupt with parity error set in ISR register
139 * and we still handle parity errors in the desired way.
140 */
141
142#define CDNS_UART_RX_IRQS (CDNS_UART_IXR_FRAMING | \
143 CDNS_UART_IXR_OVERRUN | \
144 CDNS_UART_IXR_RXTRIG | \
Soren Brinkmann373e8822016-01-11 17:41:37 -0800145 CDNS_UART_IXR_TOUT)
146
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700147/* Goes in read_status_mask for break detection as the HW doesn't do it*/
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530148#define CDNS_UART_IXR_BRK 0x00002000
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700149
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530150#define CDNS_UART_RXBS_SUPPORT BIT(1)
Soren Brinkmanne555a212014-04-04 17:23:39 -0700151/*
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +0100152 * Modem Control register:
153 * The read/write Modem Control register controls the interface with the modem
154 * or data set, or a peripheral device emulating a modem.
155 */
156#define CDNS_UART_MODEMCR_FCM 0x00000020 /* Automatic flow control mode */
157#define CDNS_UART_MODEMCR_RTS 0x00000002 /* Request to send output control */
158#define CDNS_UART_MODEMCR_DTR 0x00000001 /* Data Terminal Ready */
159
160/*
Soren Brinkmanne555a212014-04-04 17:23:39 -0700161 * Channel Status Register:
John Linn61ec9012011-04-30 00:07:43 -0400162 * The channel status register (CSR) is provided to enable the control logic
163 * to monitor the status of bits in the channel interrupt status register,
164 * even if these are masked out by the interrupt mask register.
165 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700166#define CDNS_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
167#define CDNS_UART_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
168#define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
169#define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
Helmut Grohnede4ed392018-06-04 12:22:11 +0200170#define CDNS_UART_SR_TACTIVE 0x00000800 /* TX state machine active */
John Linn61ec9012011-04-30 00:07:43 -0400171
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700172/* baud dividers min/max values */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700173#define CDNS_UART_BDIV_MIN 4
174#define CDNS_UART_BDIV_MAX 255
175#define CDNS_UART_CD_MAX 65535
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +0530176#define UART_AUTOSUSPEND_TIMEOUT 3000
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700177
John Linn61ec9012011-04-30 00:07:43 -0400178/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700179 * struct cdns_uart - device data
Michal Simek489810a12014-04-04 17:23:37 -0700180 * @port: Pointer to the UART port
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700181 * @uartclk: Reference clock
182 * @pclk: APB clock
Michal Simek46a460f2018-09-03 15:10:52 +0200183 * @cdns_uart_driver: Pointer to UART driver
Michal Simek489810a12014-04-04 17:23:37 -0700184 * @baud: Current baud rate
Michal Simekbed25ac2018-09-03 15:10:58 +0200185 * @id: Port ID
Michal Simek489810a12014-04-04 17:23:37 -0700186 * @clk_rate_change_nb: Notifier block for clock changes
Nava kishore Manne094094a2017-05-26 13:13:18 +0200187 * @quirks: Flags for RXBS support.
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700188 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700189struct cdns_uart {
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700190 struct uart_port *port;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700191 struct clk *uartclk;
192 struct clk *pclk;
Michal Simek46a460f2018-09-03 15:10:52 +0200193 struct uart_driver *cdns_uart_driver;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700194 unsigned int baud;
Michal Simekbed25ac2018-09-03 15:10:58 +0200195 int id;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700196 struct notifier_block clk_rate_change_nb;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530197 u32 quirks;
Shubhrajyoti Datta18631782019-04-09 13:15:24 +0530198 bool cts_override;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530199};
200struct cdns_platform_data {
201 u32 quirks;
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700202};
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700203#define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
Nava kishore Manne7be7a012019-06-12 13:14:42 +0200204 clk_rate_change_nb)
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700205
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100206/**
207 * cdns_uart_handle_rx - Handle the received bytes along with Rx errors.
208 * @dev_id: Id of the UART port
209 * @isrstatus: The interrupt status register value as read
210 * Return: None
211 */
212static void cdns_uart_handle_rx(void *dev_id, unsigned int isrstatus)
John Linn61ec9012011-04-30 00:07:43 -0400213{
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100214 struct uart_port *port = (struct uart_port *)dev_id;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530215 struct cdns_uart *cdns_uart = port->private_data;
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100216 unsigned int data;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530217 unsigned int rxbs_status = 0;
218 unsigned int status_mask;
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100219 unsigned int framerrprocessed = 0;
220 char status = TTY_NORMAL;
221 bool is_rxbs_support;
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530222
223 is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
224
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100225 while ((readl(port->membase + CDNS_UART_SR) &
226 CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530227 if (is_rxbs_support)
228 rxbs_status = readl(port->membase + CDNS_UART_RXBS);
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800229 data = readl(port->membase + CDNS_UART_FIFO);
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100230 port->icount.rx++;
231 /*
232 * There is no hardware break detection in Zynq, so we interpret
233 * framing error with all-zeros data as a break sequence.
234 * Most of the time, there's another non-zero byte at the
235 * end of the sequence.
236 */
237 if (!is_rxbs_support && (isrstatus & CDNS_UART_IXR_FRAMING)) {
238 if (!data) {
239 port->read_status_mask |= CDNS_UART_IXR_BRK;
240 framerrprocessed = 1;
Soren Brinkmann354fb1a2016-01-11 17:41:38 -0800241 continue;
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100242 }
Soren Brinkmann354fb1a2016-01-11 17:41:38 -0800243 }
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530244 if (is_rxbs_support && (rxbs_status & CDNS_UART_RXBS_BRK)) {
245 port->icount.brk++;
246 status = TTY_BREAK;
247 if (uart_handle_break(port))
248 continue;
249 }
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700250
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100251 isrstatus &= port->read_status_mask;
252 isrstatus &= ~port->ignore_status_mask;
253 status_mask = port->read_status_mask;
254 status_mask &= ~port->ignore_status_mask;
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700255
Nava kishore Manne212d2492016-09-22 16:58:15 +0100256 if (data &&
257 (port->read_status_mask & CDNS_UART_IXR_BRK)) {
258 port->read_status_mask &= ~CDNS_UART_IXR_BRK;
259 port->icount.brk++;
260 if (uart_handle_break(port))
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100261 continue;
John Linn61ec9012011-04-30 00:07:43 -0400262 }
Nava kishore Manne212d2492016-09-22 16:58:15 +0100263
264 if (uart_handle_sysrq_char(port, data))
265 continue;
266
267 if (is_rxbs_support) {
268 if ((rxbs_status & CDNS_UART_RXBS_PARITY)
269 && (status_mask & CDNS_UART_IXR_PARITY)) {
270 port->icount.parity++;
271 status = TTY_PARITY;
272 }
273 if ((rxbs_status & CDNS_UART_RXBS_FRAMING)
274 && (status_mask & CDNS_UART_IXR_PARITY)) {
275 port->icount.frame++;
276 status = TTY_FRAME;
277 }
278 } else {
279 if (isrstatus & CDNS_UART_IXR_PARITY) {
280 port->icount.parity++;
281 status = TTY_PARITY;
282 }
283 if ((isrstatus & CDNS_UART_IXR_FRAMING) &&
284 !framerrprocessed) {
285 port->icount.frame++;
286 status = TTY_FRAME;
287 }
288 }
289 if (isrstatus & CDNS_UART_IXR_OVERRUN) {
290 port->icount.overrun++;
291 tty_insert_flip_char(&port->state->port, 0,
292 TTY_OVERRUN);
293 }
294 tty_insert_flip_char(&port->state->port, data, status);
295 isrstatus = 0;
John Linn61ec9012011-04-30 00:07:43 -0400296 }
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100297 spin_unlock(&port->lock);
Soren Brinkmann354fb1a2016-01-11 17:41:38 -0800298 tty_flip_buffer_push(&port->state->port);
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100299 spin_lock(&port->lock);
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800300}
301
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100302/**
303 * cdns_uart_handle_tx - Handle the bytes to be Txed.
304 * @dev_id: Id of the UART port
305 * Return: None
306 */
307static void cdns_uart_handle_tx(void *dev_id)
Soren Brinkmann079865802016-01-11 17:41:41 -0800308{
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100309 struct uart_port *port = (struct uart_port *)dev_id;
Soren Brinkmann079865802016-01-11 17:41:41 -0800310 unsigned int numbytes;
311
312 if (uart_circ_empty(&port->state->xmit)) {
313 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IDR);
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100314 } else {
315 numbytes = port->fifosize;
316 while (numbytes && !uart_circ_empty(&port->state->xmit) &&
Nava kishore Mannea53f82d2019-06-12 13:14:41 +0200317 !(readl(port->membase + CDNS_UART_SR) &
318 CDNS_UART_SR_TXFULL)) {
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100319 /*
320 * Get the data from the UART circular buffer
321 * and write it to the cdns_uart's TX_FIFO
322 * register.
323 */
324 writel(
Nava kishore Manneb6415f22019-06-12 13:14:40 +0200325 port->state->xmit.buf[port->state->xmit.tail],
326 port->membase + CDNS_UART_FIFO);
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100327
328 port->icount.tx++;
329
330 /*
331 * Adjust the tail of the UART buffer and wrap
332 * the buffer if it reaches limit.
333 */
334 port->state->xmit.tail =
335 (port->state->xmit.tail + 1) &
336 (UART_XMIT_SIZE - 1);
337
338 numbytes--;
339 }
340
341 if (uart_circ_chars_pending(
342 &port->state->xmit) < WAKEUP_CHARS)
343 uart_write_wakeup(port);
Soren Brinkmann079865802016-01-11 17:41:41 -0800344 }
Soren Brinkmann079865802016-01-11 17:41:41 -0800345}
346
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800347/**
348 * cdns_uart_isr - Interrupt handler
349 * @irq: Irq number
350 * @dev_id: Id of the port
351 *
352 * Return: IRQHANDLED
353 */
354static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
355{
356 struct uart_port *port = (struct uart_port *)dev_id;
Soren Brinkmann079865802016-01-11 17:41:41 -0800357 unsigned int isrstatus;
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800358
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100359 spin_lock(&port->lock);
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800360
361 /* Read the interrupt status register to determine which
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100362 * interrupt(s) is/are active and clear them.
Soren Brinkmann5ede4a52015-12-26 02:43:56 -0800363 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800364 isrstatus = readl(port->membase + CDNS_UART_ISR);
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800365 writel(isrstatus, port->membase + CDNS_UART_ISR);
John Linn61ec9012011-04-30 00:07:43 -0400366
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100367 if (isrstatus & CDNS_UART_IXR_TXEMPTY) {
368 cdns_uart_handle_tx(dev_id);
369 isrstatus &= ~CDNS_UART_IXR_TXEMPTY;
370 }
Anssi Hannula7abab162019-02-15 18:45:08 +0200371
372 /*
373 * Skip RX processing if RX is disabled as RXEMPTY will never be set
374 * as read bytes will not be removed from the FIFO.
375 */
376 if (isrstatus & CDNS_UART_IXR_RXMASK &&
377 !(readl(port->membase + CDNS_UART_CR) & CDNS_UART_CR_RX_DIS))
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100378 cdns_uart_handle_rx(dev_id, isrstatus);
John Linn61ec9012011-04-30 00:07:43 -0400379
Anirudha Sarangic8dbdc82016-09-22 16:58:14 +0100380 spin_unlock(&port->lock);
John Linn61ec9012011-04-30 00:07:43 -0400381 return IRQ_HANDLED;
382}
383
384/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700385 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700386 * @clk: UART module input clock
387 * @baud: Desired baud rate
388 * @rbdiv: BDIV value (return value)
389 * @rcd: CD value (return value)
390 * @div8: Value for clk_sel bit in mod (return value)
Michal Simek489810a12014-04-04 17:23:37 -0700391 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700392 * was too much error, zero if no valid divisors are found.
393 *
394 * Formula to obtain baud rate is
395 * baud_tx/rx rate = clk/CD * (BDIV + 1)
396 * input_clk = (Uart User Defined Clock or Apb Clock)
397 * depends on UCLKEN in MR Reg
398 * clk = input_clk or input_clk/8;
399 * depends on CLKS in MR reg
400 * CD and BDIV depends on values in
401 * baud rate generate register
402 * baud rate clock divisor register
403 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700404static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
405 unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
John Linn61ec9012011-04-30 00:07:43 -0400406{
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700407 u32 cd, bdiv;
408 unsigned int calc_baud;
409 unsigned int bestbaud = 0;
John Linn61ec9012011-04-30 00:07:43 -0400410 unsigned int bauderror;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700411 unsigned int besterror = ~0;
John Linn61ec9012011-04-30 00:07:43 -0400412
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700413 if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700414 *div8 = 1;
415 clk /= 8;
416 } else {
417 *div8 = 0;
418 }
John Linn61ec9012011-04-30 00:07:43 -0400419
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700420 for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700421 cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700422 if (cd < 1 || cd > CDNS_UART_CD_MAX)
John Linn61ec9012011-04-30 00:07:43 -0400423 continue;
424
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700425 calc_baud = clk / (cd * (bdiv + 1));
John Linn61ec9012011-04-30 00:07:43 -0400426
427 if (baud > calc_baud)
428 bauderror = baud - calc_baud;
429 else
430 bauderror = calc_baud - baud;
431
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700432 if (besterror > bauderror) {
433 *rbdiv = bdiv;
434 *rcd = cd;
435 bestbaud = calc_baud;
436 besterror = bauderror;
John Linn61ec9012011-04-30 00:07:43 -0400437 }
438 }
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700439 /* use the values when percent error is acceptable */
440 if (((besterror * 100) / baud) < 3)
441 bestbaud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400442
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700443 return bestbaud;
444}
445
446/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700447 * cdns_uart_set_baud_rate - Calculate and set the baud rate
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700448 * @port: Handle to the uart port structure
449 * @baud: Baud rate to set
Michal Simek489810a12014-04-04 17:23:37 -0700450 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700451 * was too much error, zero if no valid divisors are found.
452 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700453static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700454 unsigned int baud)
455{
456 unsigned int calc_baud;
Soren Brinkmannd54b1812013-10-21 16:40:59 -0700457 u32 cd = 0, bdiv = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700458 u32 mreg;
459 int div8;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700460 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700461
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700462 calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700463 &div8);
464
465 /* Write new divisors to hardware */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800466 mreg = readl(port->membase + CDNS_UART_MR);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700467 if (div8)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700468 mreg |= CDNS_UART_MR_CLKSEL;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700469 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700470 mreg &= ~CDNS_UART_MR_CLKSEL;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800471 writel(mreg, port->membase + CDNS_UART_MR);
472 writel(cd, port->membase + CDNS_UART_BAUDGEN);
473 writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700474 cdns_uart->baud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400475
476 return calc_baud;
477}
478
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700479#ifdef CONFIG_COMMON_CLK
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700480/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700481 * cdns_uart_clk_notitifer_cb - Clock notifier callback
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700482 * @nb: Notifier block
483 * @event: Notify event
484 * @data: Notifier data
Soren Brinkmanne555a212014-04-04 17:23:39 -0700485 * Return: NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700486 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700487static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700488 unsigned long event, void *data)
489{
490 u32 ctrl_reg;
491 struct uart_port *port;
492 int locked = 0;
493 struct clk_notifier_data *ndata = data;
494 unsigned long flags = 0;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700495 struct cdns_uart *cdns_uart = to_cdns_uart(nb);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700496
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700497 port = cdns_uart->port;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700498 if (port->suspended)
499 return NOTIFY_OK;
500
501 switch (event) {
502 case PRE_RATE_CHANGE:
503 {
Soren Brinkmanne555a212014-04-04 17:23:39 -0700504 u32 bdiv, cd;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700505 int div8;
506
507 /*
508 * Find out if current baud-rate can be achieved with new clock
509 * frequency.
510 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700511 if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700512 &bdiv, &cd, &div8)) {
513 dev_warn(port->dev, "clock rate change rejected\n");
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700514 return NOTIFY_BAD;
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700515 }
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700516
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700517 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700518
519 /* Disable the TX and RX to set baud rate */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800520 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700521 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800522 writel(ctrl_reg, port->membase + CDNS_UART_CR);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700523
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700524 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700525
526 return NOTIFY_OK;
527 }
528 case POST_RATE_CHANGE:
529 /*
530 * Set clk dividers to generate correct baud with new clock
531 * frequency.
532 */
533
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700534 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700535
536 locked = 1;
537 port->uartclk = ndata->new_rate;
538
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700539 cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
540 cdns_uart->baud);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700541 /* fall through */
542 case ABORT_RATE_CHANGE:
543 if (!locked)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700544 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700545
546 /* Set TX/RX Reset */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800547 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700548 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800549 writel(ctrl_reg, port->membase + CDNS_UART_CR);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700550
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800551 while (readl(port->membase + CDNS_UART_CR) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700552 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700553 cpu_relax();
554
555 /*
556 * Clear the RX disable and TX disable bits and then set the TX
557 * enable bit and RX enable bit to enable the transmitter and
558 * receiver.
559 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800560 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
561 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700562 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
563 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800564 writel(ctrl_reg, port->membase + CDNS_UART_CR);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700565
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700566 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700567
568 return NOTIFY_OK;
569 default:
570 return NOTIFY_DONE;
571 }
572}
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700573#endif
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700574
John Linn61ec9012011-04-30 00:07:43 -0400575/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700576 * cdns_uart_start_tx - Start transmitting bytes
John Linn61ec9012011-04-30 00:07:43 -0400577 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700578 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700579static void cdns_uart_start_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400580{
Soren Brinkmann079865802016-01-11 17:41:41 -0800581 unsigned int status;
John Linn61ec9012011-04-30 00:07:43 -0400582
Soren Brinkmannea8dd8e2015-12-26 02:43:51 -0800583 if (uart_tx_stopped(port))
John Linn61ec9012011-04-30 00:07:43 -0400584 return;
585
Soren Brinkmanne3538c32015-12-26 02:43:49 -0800586 /*
587 * Set the TX enable bit and clear the TX disable bit to enable the
John Linn61ec9012011-04-30 00:07:43 -0400588 * transmitter.
589 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800590 status = readl(port->membase + CDNS_UART_CR);
Soren Brinkmanne3538c32015-12-26 02:43:49 -0800591 status &= ~CDNS_UART_CR_TX_DIS;
592 status |= CDNS_UART_CR_TX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800593 writel(status, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400594
Soren Brinkmannea8dd8e2015-12-26 02:43:51 -0800595 if (uart_circ_empty(&port->state->xmit))
596 return;
597
Soren Brinkmann079865802016-01-11 17:41:41 -0800598 cdns_uart_handle_tx(port);
John Linn61ec9012011-04-30 00:07:43 -0400599
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800600 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR);
John Linn61ec9012011-04-30 00:07:43 -0400601 /* Enable the TX Empty interrupt */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800602 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IER);
John Linn61ec9012011-04-30 00:07:43 -0400603}
604
605/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700606 * cdns_uart_stop_tx - Stop TX
John Linn61ec9012011-04-30 00:07:43 -0400607 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700608 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700609static void cdns_uart_stop_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400610{
611 unsigned int regval;
612
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800613 regval = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700614 regval |= CDNS_UART_CR_TX_DIS;
John Linn61ec9012011-04-30 00:07:43 -0400615 /* Disable the transmitter */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800616 writel(regval, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400617}
618
619/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700620 * cdns_uart_stop_rx - Stop RX
John Linn61ec9012011-04-30 00:07:43 -0400621 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700622 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700623static void cdns_uart_stop_rx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400624{
625 unsigned int regval;
626
Soren Brinkmann373e8822016-01-11 17:41:37 -0800627 /* Disable RX IRQs */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800628 writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR);
Soren Brinkmann373e8822016-01-11 17:41:37 -0800629
630 /* Disable the receiver */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800631 regval = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700632 regval |= CDNS_UART_CR_RX_DIS;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800633 writel(regval, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400634}
635
636/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700637 * cdns_uart_tx_empty - Check whether TX is empty
John Linn61ec9012011-04-30 00:07:43 -0400638 * @port: Handle to the uart port structure
639 *
Michal Simek489810a12014-04-04 17:23:37 -0700640 * Return: TIOCSER_TEMT on success, 0 otherwise
641 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700642static unsigned int cdns_uart_tx_empty(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400643{
644 unsigned int status;
645
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800646 status = readl(port->membase + CDNS_UART_SR) &
Thomas Betker19f22ef2015-03-12 22:11:59 +0100647 CDNS_UART_SR_TXEMPTY;
John Linn61ec9012011-04-30 00:07:43 -0400648 return status ? TIOCSER_TEMT : 0;
649}
650
651/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700652 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
John Linn61ec9012011-04-30 00:07:43 -0400653 * transmitting char breaks
654 * @port: Handle to the uart port structure
655 * @ctl: Value based on which start or stop decision is taken
Michal Simek489810a12014-04-04 17:23:37 -0700656 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700657static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
John Linn61ec9012011-04-30 00:07:43 -0400658{
659 unsigned int status;
660 unsigned long flags;
661
662 spin_lock_irqsave(&port->lock, flags);
663
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800664 status = readl(port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400665
666 if (ctl == -1)
Thomas Betker19f22ef2015-03-12 22:11:59 +0100667 writel(CDNS_UART_CR_STARTBRK | status,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800668 port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400669 else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700670 if ((status & CDNS_UART_CR_STOPBRK) == 0)
Thomas Betker19f22ef2015-03-12 22:11:59 +0100671 writel(CDNS_UART_CR_STOPBRK | status,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800672 port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400673 }
674 spin_unlock_irqrestore(&port->lock, flags);
675}
676
677/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700678 * cdns_uart_set_termios - termios operations, handling data length, parity,
John Linn61ec9012011-04-30 00:07:43 -0400679 * stop bits, flow control, baud rate
680 * @port: Handle to the uart port structure
681 * @termios: Handle to the input termios structure
682 * @old: Values of the previously saved termios structure
Michal Simek489810a12014-04-04 17:23:37 -0700683 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700684static void cdns_uart_set_termios(struct uart_port *port,
John Linn61ec9012011-04-30 00:07:43 -0400685 struct ktermios *termios, struct ktermios *old)
686{
687 unsigned int cval = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700688 unsigned int baud, minbaud, maxbaud;
John Linn61ec9012011-04-30 00:07:43 -0400689 unsigned long flags;
Shubhrajyoti Datta277375b2019-05-24 14:41:29 +0530690 unsigned int ctrl_reg, mode_reg, val;
691 int err;
John Linn61ec9012011-04-30 00:07:43 -0400692
Nathan Rossi6ecde472015-01-16 13:49:25 +0100693 /* Wait for the transmit FIFO to empty before making changes */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800694 if (!(readl(port->membase + CDNS_UART_CR) &
Thomas Betker19f22ef2015-03-12 22:11:59 +0100695 CDNS_UART_CR_TX_DIS)) {
Shubhrajyoti Datta277375b2019-05-24 14:41:29 +0530696 err = readl_poll_timeout(port->membase + CDNS_UART_SR,
697 val, (val & CDNS_UART_SR_TXEMPTY),
698 1000, TX_TIMEOUT);
699 if (err) {
700 dev_err(port->dev, "timed out waiting for tx empty");
701 return;
Nathan Rossi6ecde472015-01-16 13:49:25 +0100702 }
John Linn61ec9012011-04-30 00:07:43 -0400703 }
Shubhrajyoti Datta10747562019-05-24 14:41:28 +0530704 spin_lock_irqsave(&port->lock, flags);
John Linn61ec9012011-04-30 00:07:43 -0400705
706 /* Disable the TX and RX to set baud rate */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800707 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700708 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800709 writel(ctrl_reg, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400710
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700711 /*
712 * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
713 * min and max baud should be calculated here based on port->uartclk.
714 * this way we get a valid baud and can safely call set_baud()
715 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700716 minbaud = port->uartclk /
717 ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
718 maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700719 baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700720 baud = cdns_uart_set_baud_rate(port, baud);
John Linn61ec9012011-04-30 00:07:43 -0400721 if (tty_termios_baud_rate(termios))
722 tty_termios_encode_baud_rate(termios, baud, baud);
723
Soren Brinkmanne555a212014-04-04 17:23:39 -0700724 /* Update the per-port timeout. */
John Linn61ec9012011-04-30 00:07:43 -0400725 uart_update_timeout(port, termios->c_cflag, baud);
726
727 /* Set TX/RX Reset */
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_TXRST | CDNS_UART_CR_RXRST;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800730 writel(ctrl_reg, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400731
Nava kishore Manne27b17ae2016-09-15 14:45:31 +0530732 while (readl(port->membase + CDNS_UART_CR) &
733 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
734 cpu_relax();
735
Soren Brinkmanne555a212014-04-04 17:23:39 -0700736 /*
737 * Clear the RX disable and TX disable bits and then set the TX enable
John Linn61ec9012011-04-30 00:07:43 -0400738 * bit and RX enable bit to enable the transmitter and receiver.
739 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800740 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700741 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
742 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800743 writel(ctrl_reg, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400744
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800745 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
John Linn61ec9012011-04-30 00:07:43 -0400746
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700747 port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
748 CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
John Linn61ec9012011-04-30 00:07:43 -0400749 port->ignore_status_mask = 0;
750
751 if (termios->c_iflag & INPCK)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700752 port->read_status_mask |= CDNS_UART_IXR_PARITY |
753 CDNS_UART_IXR_FRAMING;
John Linn61ec9012011-04-30 00:07:43 -0400754
755 if (termios->c_iflag & IGNPAR)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700756 port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
757 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400758
759 /* ignore all characters if CREAD is not set */
760 if ((termios->c_cflag & CREAD) == 0)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700761 port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
762 CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
763 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400764
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800765 mode_reg = readl(port->membase + CDNS_UART_MR);
John Linn61ec9012011-04-30 00:07:43 -0400766
767 /* Handling Data Size */
768 switch (termios->c_cflag & CSIZE) {
769 case CS6:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700770 cval |= CDNS_UART_MR_CHARLEN_6_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400771 break;
772 case CS7:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700773 cval |= CDNS_UART_MR_CHARLEN_7_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400774 break;
775 default:
776 case CS8:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700777 cval |= CDNS_UART_MR_CHARLEN_8_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400778 termios->c_cflag &= ~CSIZE;
779 termios->c_cflag |= CS8;
780 break;
781 }
782
783 /* Handling Parity and Stop Bits length */
784 if (termios->c_cflag & CSTOPB)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700785 cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
John Linn61ec9012011-04-30 00:07:43 -0400786 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700787 cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
John Linn61ec9012011-04-30 00:07:43 -0400788
789 if (termios->c_cflag & PARENB) {
790 /* Mark or Space parity */
791 if (termios->c_cflag & CMSPAR) {
792 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700793 cval |= CDNS_UART_MR_PARITY_MARK;
John Linn61ec9012011-04-30 00:07:43 -0400794 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700795 cval |= CDNS_UART_MR_PARITY_SPACE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700796 } else {
797 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700798 cval |= CDNS_UART_MR_PARITY_ODD;
John Linn61ec9012011-04-30 00:07:43 -0400799 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700800 cval |= CDNS_UART_MR_PARITY_EVEN;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700801 }
802 } else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700803 cval |= CDNS_UART_MR_PARITY_NONE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700804 }
805 cval |= mode_reg & 1;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800806 writel(cval, port->membase + CDNS_UART_MR);
John Linn61ec9012011-04-30 00:07:43 -0400807
808 spin_unlock_irqrestore(&port->lock, flags);
809}
810
811/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700812 * cdns_uart_startup - Called when an application opens a cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400813 * @port: Handle to the uart port structure
814 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700815 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -0700816 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700817static int cdns_uart_startup(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400818{
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530819 struct cdns_uart *cdns_uart = port->private_data;
820 bool is_brk_support;
Soren Brinkmann55861d12016-01-11 17:41:36 -0800821 int ret;
Soren Brinkmann6e14f7c2015-12-26 02:43:53 -0800822 unsigned long flags;
Soren Brinkmann55861d12016-01-11 17:41:36 -0800823 unsigned int status = 0;
John Linn61ec9012011-04-30 00:07:43 -0400824
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530825 is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
826
Soren Brinkmann6e14f7c2015-12-26 02:43:53 -0800827 spin_lock_irqsave(&port->lock, flags);
828
John Linn61ec9012011-04-30 00:07:43 -0400829 /* Disable the TX and RX */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100830 writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800831 port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400832
833 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
834 * no break chars.
835 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100836 writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800837 port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400838
Nava kishore Manne27b17ae2016-09-15 14:45:31 +0530839 while (readl(port->membase + CDNS_UART_CR) &
840 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
841 cpu_relax();
842
Soren Brinkmann6e14f7c2015-12-26 02:43:53 -0800843 /*
844 * Clear the RX disable bit and then set the RX enable bit to enable
845 * the receiver.
John Linn61ec9012011-04-30 00:07:43 -0400846 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800847 status = readl(port->membase + CDNS_UART_CR);
Helmut Grohne68d12bb2018-06-04 12:22:05 +0200848 status &= ~CDNS_UART_CR_RX_DIS;
Soren Brinkmann6e14f7c2015-12-26 02:43:53 -0800849 status |= CDNS_UART_CR_RX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800850 writel(status, port->membase + CDNS_UART_CR);
John Linn61ec9012011-04-30 00:07:43 -0400851
852 /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
853 * no parity.
854 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100855 writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700856 | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800857 port->membase + CDNS_UART_MR);
John Linn61ec9012011-04-30 00:07:43 -0400858
Suneel85baf542013-10-17 14:08:08 -0700859 /*
860 * Set the RX FIFO Trigger level to use most of the FIFO, but it
861 * can be tuned with a module parameter
862 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800863 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
John Linn61ec9012011-04-30 00:07:43 -0400864
Suneel85baf542013-10-17 14:08:08 -0700865 /*
866 * Receive Timeout register is enabled but it
867 * can be tuned with a module parameter
868 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800869 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
John Linn61ec9012011-04-30 00:07:43 -0400870
John Linn855f6fd2013-03-22 18:49:27 +0100871 /* Clear out any pending interrupts before enabling them */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800872 writel(readl(port->membase + CDNS_UART_ISR),
873 port->membase + CDNS_UART_ISR);
John Linn61ec9012011-04-30 00:07:43 -0400874
Soren Brinkmann55861d12016-01-11 17:41:36 -0800875 spin_unlock_irqrestore(&port->lock, flags);
876
877 ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port);
878 if (ret) {
879 dev_err(port->dev, "request_irq '%d' failed with %d\n",
880 port->irq, ret);
881 return ret;
882 }
883
John Linn61ec9012011-04-30 00:07:43 -0400884 /* Set the Interrupt Registers with desired interrupts */
Nava kishore Manne3816b2f82016-09-15 14:45:29 +0530885 if (is_brk_support)
886 writel(CDNS_UART_RX_IRQS | CDNS_UART_IXR_BRK,
887 port->membase + CDNS_UART_IER);
888 else
889 writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER);
John Linn61ec9012011-04-30 00:07:43 -0400890
Soren Brinkmann55861d12016-01-11 17:41:36 -0800891 return 0;
John Linn61ec9012011-04-30 00:07:43 -0400892}
893
894/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700895 * cdns_uart_shutdown - Called when an application closes a cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400896 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700897 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700898static void cdns_uart_shutdown(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400899{
900 int status;
Soren Brinkmanna19eda02015-12-26 02:43:55 -0800901 unsigned long flags;
902
903 spin_lock_irqsave(&port->lock, flags);
John Linn61ec9012011-04-30 00:07:43 -0400904
905 /* Disable interrupts */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800906 status = readl(port->membase + CDNS_UART_IMR);
907 writel(status, port->membase + CDNS_UART_IDR);
908 writel(0xffffffff, port->membase + CDNS_UART_ISR);
John Linn61ec9012011-04-30 00:07:43 -0400909
910 /* Disable the TX and RX */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100911 writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
Soren Brinkmanna8df6a52016-01-11 17:41:40 -0800912 port->membase + CDNS_UART_CR);
Soren Brinkmanna19eda02015-12-26 02:43:55 -0800913
914 spin_unlock_irqrestore(&port->lock, flags);
915
John Linn61ec9012011-04-30 00:07:43 -0400916 free_irq(port->irq, port);
917}
918
919/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700920 * cdns_uart_type - Set UART type to cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400921 * @port: Handle to the uart port structure
922 *
Michal Simek489810a12014-04-04 17:23:37 -0700923 * Return: string on success, NULL otherwise
924 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700925static const char *cdns_uart_type(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400926{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700927 return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
John Linn61ec9012011-04-30 00:07:43 -0400928}
929
930/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700931 * cdns_uart_verify_port - Verify the port params
John Linn61ec9012011-04-30 00:07:43 -0400932 * @port: Handle to the uart port structure
933 * @ser: Handle to the structure whose members are compared
934 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700935 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -0700936 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700937static int cdns_uart_verify_port(struct uart_port *port,
John Linn61ec9012011-04-30 00:07:43 -0400938 struct serial_struct *ser)
939{
940 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
941 return -EINVAL;
942 if (port->irq != ser->irq)
943 return -EINVAL;
944 if (ser->io_type != UPIO_MEM)
945 return -EINVAL;
946 if (port->iobase != ser->port)
947 return -EINVAL;
948 if (ser->hub6 != 0)
949 return -EINVAL;
950 return 0;
951}
952
953/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700954 * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
955 * called when the driver adds a cdns_uart port via
John Linn61ec9012011-04-30 00:07:43 -0400956 * uart_add_one_port()
957 * @port: Handle to the uart port structure
958 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700959 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -0700960 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700961static int cdns_uart_request_port(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400962{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700963 if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
964 CDNS_UART_NAME)) {
John Linn61ec9012011-04-30 00:07:43 -0400965 return -ENOMEM;
966 }
967
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700968 port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400969 if (!port->membase) {
970 dev_err(port->dev, "Unable to map registers\n");
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700971 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400972 return -ENOMEM;
973 }
974 return 0;
975}
976
977/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700978 * cdns_uart_release_port - Release UART port
John Linn61ec9012011-04-30 00:07:43 -0400979 * @port: Handle to the uart port structure
Soren Brinkmanne555a212014-04-04 17:23:39 -0700980 *
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700981 * Release the memory region attached to a cdns_uart port. Called when the
982 * driver removes a cdns_uart port via uart_remove_one_port().
Michal Simek489810a12014-04-04 17:23:37 -0700983 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700984static void cdns_uart_release_port(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400985{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700986 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400987 iounmap(port->membase);
988 port->membase = NULL;
989}
990
991/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700992 * cdns_uart_config_port - Configure UART port
John Linn61ec9012011-04-30 00:07:43 -0400993 * @port: Handle to the uart port structure
994 * @flags: If any
Michal Simek489810a12014-04-04 17:23:37 -0700995 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700996static void cdns_uart_config_port(struct uart_port *port, int flags)
John Linn61ec9012011-04-30 00:07:43 -0400997{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700998 if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
John Linn61ec9012011-04-30 00:07:43 -0400999 port->type = PORT_XUARTPS;
1000}
1001
1002/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001003 * cdns_uart_get_mctrl - Get the modem control state
John Linn61ec9012011-04-30 00:07:43 -04001004 * @port: Handle to the uart port structure
1005 *
Michal Simek489810a12014-04-04 17:23:37 -07001006 * Return: the modem control state
1007 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001008static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -04001009{
Shubhrajyoti Datta18631782019-04-09 13:15:24 +05301010 struct cdns_uart *cdns_uart_data = port->private_data;
1011
1012 if (cdns_uart_data->cts_override)
1013 return 0;
1014
John Linn61ec9012011-04-30 00:07:43 -04001015 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
1016}
1017
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001018static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
John Linn61ec9012011-04-30 00:07:43 -04001019{
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001020 u32 val;
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001021 u32 mode_reg;
Shubhrajyoti Datta18631782019-04-09 13:15:24 +05301022 struct cdns_uart *cdns_uart_data = port->private_data;
1023
1024 if (cdns_uart_data->cts_override)
1025 return;
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001026
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001027 val = readl(port->membase + CDNS_UART_MODEMCR);
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001028 mode_reg = readl(port->membase + CDNS_UART_MR);
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001029
Michal Simek6ac1b912018-09-03 15:05:37 +02001030 val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR |
1031 CDNS_UART_MODEMCR_FCM);
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001032 mode_reg &= ~CDNS_UART_MR_CHMODE_MASK;
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001033
Michal Simek6ac1b912018-09-03 15:05:37 +02001034 if (mctrl & TIOCM_RTS || mctrl & TIOCM_DTR)
1035 val |= CDNS_UART_MODEMCR_FCM;
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001036 if (mctrl & TIOCM_LOOP)
1037 mode_reg |= CDNS_UART_MR_CHMODE_L_LOOP;
1038 else
1039 mode_reg |= CDNS_UART_MR_CHMODE_NORM;
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +01001040
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001041 writel(val, port->membase + CDNS_UART_MODEMCR);
Yasir-Khan5935a2b2017-01-23 13:26:08 +01001042 writel(mode_reg, port->membase + CDNS_UART_MR);
John Linn61ec9012011-04-30 00:07:43 -04001043}
1044
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001045#ifdef CONFIG_CONSOLE_POLL
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001046static int cdns_uart_poll_get_char(struct uart_port *port)
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001047{
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001048 int c;
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001049 unsigned long flags;
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001050
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001051 spin_lock_irqsave(&port->lock, flags);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001052
1053 /* Check if FIFO is empty */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001054 if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001055 c = NO_POLL_CHAR;
1056 else /* Read a character */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001057 c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001058
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001059 spin_unlock_irqrestore(&port->lock, flags);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001060
1061 return c;
1062}
1063
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001064static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001065{
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001066 unsigned long flags;
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001067
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001068 spin_lock_irqsave(&port->lock, flags);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001069
1070 /* Wait until FIFO is empty */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001071 while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001072 cpu_relax();
1073
1074 /* Write a character */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001075 writel(c, port->membase + CDNS_UART_FIFO);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001076
1077 /* Wait until FIFO is empty */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001078 while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001079 cpu_relax();
1080
Soren Brinkmannf0f54a82015-12-26 02:43:50 -08001081 spin_unlock_irqrestore(&port->lock, flags);
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001082}
1083#endif
1084
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301085static void cdns_uart_pm(struct uart_port *port, unsigned int state,
1086 unsigned int oldstate)
1087{
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301088 switch (state) {
1089 case UART_PM_STATE_OFF:
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301090 pm_runtime_mark_last_busy(port->dev);
1091 pm_runtime_put_autosuspend(port->dev);
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301092 break;
1093 default:
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301094 pm_runtime_get_sync(port->dev);
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301095 break;
1096 }
1097}
1098
Julia Lawallf098a0a2016-09-01 19:51:36 +02001099static const struct uart_ops cdns_uart_ops = {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001100 .set_mctrl = cdns_uart_set_mctrl,
1101 .get_mctrl = cdns_uart_get_mctrl,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001102 .start_tx = cdns_uart_start_tx,
1103 .stop_tx = cdns_uart_stop_tx,
1104 .stop_rx = cdns_uart_stop_rx,
1105 .tx_empty = cdns_uart_tx_empty,
1106 .break_ctl = cdns_uart_break_ctl,
1107 .set_termios = cdns_uart_set_termios,
1108 .startup = cdns_uart_startup,
1109 .shutdown = cdns_uart_shutdown,
Shubhrajyoti Datta210417c2016-05-27 15:05:19 +05301110 .pm = cdns_uart_pm,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001111 .type = cdns_uart_type,
1112 .verify_port = cdns_uart_verify_port,
1113 .request_port = cdns_uart_request_port,
1114 .release_port = cdns_uart_release_port,
1115 .config_port = cdns_uart_config_port,
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001116#ifdef CONFIG_CONSOLE_POLL
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001117 .poll_get_char = cdns_uart_poll_get_char,
1118 .poll_put_char = cdns_uart_poll_put_char,
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001119#endif
John Linn61ec9012011-04-30 00:07:43 -04001120};
1121
John Linn61ec9012011-04-30 00:07:43 -04001122#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1123/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001124 * cdns_uart_console_putchar - write the character to the FIFO buffer
John Linn61ec9012011-04-30 00:07:43 -04001125 * @port: Handle to the uart port structure
1126 * @ch: Character to be written
Michal Simek489810a12014-04-04 17:23:37 -07001127 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001128static void cdns_uart_console_putchar(struct uart_port *port, int ch)
John Linn61ec9012011-04-30 00:07:43 -04001129{
Helmut Grohnede4ed392018-06-04 12:22:11 +02001130 while (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)
1131 cpu_relax();
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001132 writel(ch, port->membase + CDNS_UART_FIFO);
John Linn61ec9012011-04-30 00:07:43 -04001133}
1134
Jeffy Chen99d273162017-07-18 14:02:56 +08001135static void cdns_early_write(struct console *con, const char *s,
Masahiro Yamada54585ba2015-05-07 18:55:40 +09001136 unsigned n)
Michal Simek6fa62fc2014-09-10 12:43:02 +02001137{
1138 struct earlycon_device *dev = con->data;
1139
1140 uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
1141}
1142
1143static int __init cdns_early_console_setup(struct earlycon_device *device,
1144 const char *opt)
1145{
Scott Telfordc41251b2016-09-22 16:58:16 +01001146 struct uart_port *port = &device->port;
1147
1148 if (!port->membase)
Michal Simek6fa62fc2014-09-10 12:43:02 +02001149 return -ENODEV;
1150
Scott Telfordc41251b2016-09-22 16:58:16 +01001151 /* initialise control register */
1152 writel(CDNS_UART_CR_TX_EN|CDNS_UART_CR_TXRST|CDNS_UART_CR_RXRST,
1153 port->membase + CDNS_UART_CR);
1154
1155 /* only set baud if specified on command line - otherwise
1156 * assume it has been initialized by a boot loader.
1157 */
Michal Simek66dd99c2018-04-23 10:55:21 +02001158 if (port->uartclk && device->baud) {
Scott Telfordc41251b2016-09-22 16:58:16 +01001159 u32 cd = 0, bdiv = 0;
1160 u32 mr;
1161 int div8;
1162
1163 cdns_uart_calc_baud_divs(port->uartclk, device->baud,
1164 &bdiv, &cd, &div8);
1165 mr = CDNS_UART_MR_PARITY_NONE;
1166 if (div8)
1167 mr |= CDNS_UART_MR_CLKSEL;
1168
1169 writel(mr, port->membase + CDNS_UART_MR);
1170 writel(cd, port->membase + CDNS_UART_BAUDGEN);
1171 writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
1172 }
1173
Michal Simek6fa62fc2014-09-10 12:43:02 +02001174 device->con->write = cdns_early_write;
1175
1176 return 0;
1177}
Michal Simek93d7bba2016-02-18 08:57:20 +01001178OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
1179OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
1180OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
Nava kishore Manne0267a4f2016-10-12 13:17:27 +05301181OF_EARLYCON_DECLARE(cdns, "xlnx,zynqmp-uart", cdns_early_console_setup);
Michal Simek6fa62fc2014-09-10 12:43:02 +02001182
Michal Simek0413fe02018-04-23 11:51:01 +02001183
1184/* Static pointer to console port */
1185static struct uart_port *console_port;
1186
John Linn61ec9012011-04-30 00:07:43 -04001187/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001188 * cdns_uart_console_write - perform write operation
Michal Simek489810a12014-04-04 17:23:37 -07001189 * @co: Console handle
John Linn61ec9012011-04-30 00:07:43 -04001190 * @s: Pointer to character array
1191 * @count: No of characters
Michal Simek489810a12014-04-04 17:23:37 -07001192 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001193static void cdns_uart_console_write(struct console *co, const char *s,
John Linn61ec9012011-04-30 00:07:43 -04001194 unsigned int count)
1195{
Michal Simek0413fe02018-04-23 11:51:01 +02001196 struct uart_port *port = console_port;
Stephen Rothwell2bf593f2019-07-31 16:05:57 +10001197 unsigned long flags = 0;
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001198 unsigned int imr, ctrl;
John Linn61ec9012011-04-30 00:07:43 -04001199 int locked = 1;
1200
Soren Brinkmann74ea66d42016-01-11 17:41:39 -08001201 if (port->sysrq)
1202 locked = 0;
1203 else if (oops_in_progress)
John Linn61ec9012011-04-30 00:07:43 -04001204 locked = spin_trylock_irqsave(&port->lock, flags);
1205 else
1206 spin_lock_irqsave(&port->lock, flags);
1207
1208 /* save and disable interrupt */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001209 imr = readl(port->membase + CDNS_UART_IMR);
1210 writel(imr, port->membase + CDNS_UART_IDR);
John Linn61ec9012011-04-30 00:07:43 -04001211
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001212 /*
1213 * Make sure that the tx part is enabled. Set the TX enable bit and
1214 * clear the TX disable bit to enable the transmitter.
1215 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001216 ctrl = readl(port->membase + CDNS_UART_CR);
Soren Brinkmanne3538c32015-12-26 02:43:49 -08001217 ctrl &= ~CDNS_UART_CR_TX_DIS;
1218 ctrl |= CDNS_UART_CR_TX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001219 writel(ctrl, port->membase + CDNS_UART_CR);
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001220
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001221 uart_console_write(port, s, count, cdns_uart_console_putchar);
Helmut Grohnede4ed392018-06-04 12:22:11 +02001222 while ((readl(port->membase + CDNS_UART_SR) &
1223 (CDNS_UART_SR_TXEMPTY | CDNS_UART_SR_TACTIVE)) !=
1224 CDNS_UART_SR_TXEMPTY)
1225 cpu_relax();
John Linn61ec9012011-04-30 00:07:43 -04001226
Soren Brinkmannb494a5f2014-04-04 17:23:42 -07001227 /* restore interrupt state */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001228 writel(imr, port->membase + CDNS_UART_IER);
John Linn61ec9012011-04-30 00:07:43 -04001229
1230 if (locked)
1231 spin_unlock_irqrestore(&port->lock, flags);
1232}
1233
1234/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001235 * cdns_uart_console_setup - Initialize the uart to default config
John Linn61ec9012011-04-30 00:07:43 -04001236 * @co: Console handle
1237 * @options: Initial settings of uart
1238 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001239 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -07001240 */
Michal Simek4bb1ce22018-09-03 15:10:49 +02001241static int cdns_uart_console_setup(struct console *co, char *options)
John Linn61ec9012011-04-30 00:07:43 -04001242{
Michal Simek0413fe02018-04-23 11:51:01 +02001243 struct uart_port *port = console_port;
1244
John Linn61ec9012011-04-30 00:07:43 -04001245 int baud = 9600;
1246 int bits = 8;
1247 int parity = 'n';
1248 int flow = 'n';
1249
Thomas Betker136debf2015-03-11 22:39:28 +01001250 if (!port->membase) {
Peter Crosthwaitef6415492015-02-24 15:13:57 -08001251 pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
1252 co->index);
John Linn61ec9012011-04-30 00:07:43 -04001253 return -ENODEV;
1254 }
1255
1256 if (options)
1257 uart_parse_options(options, &baud, &parity, &bits, &flow);
1258
1259 return uart_set_options(port, co, baud, parity, bits, flow);
1260}
John Linn61ec9012011-04-30 00:07:43 -04001261#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
1262
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001263#ifdef CONFIG_PM_SLEEP
1264/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001265 * cdns_uart_suspend - suspend event
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001266 * @device: Pointer to the device structure
1267 *
Michal Simek489810a12014-04-04 17:23:37 -07001268 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001269 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001270static int cdns_uart_suspend(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001271{
1272 struct uart_port *port = dev_get_drvdata(device);
Michal Simek46a460f2018-09-03 15:10:52 +02001273 struct cdns_uart *cdns_uart = port->private_data;
Nava kishore Manne4b9d33c2018-09-03 15:10:51 +02001274 int may_wake;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001275
Nava kishore Manne4b9d33c2018-09-03 15:10:51 +02001276 may_wake = device_may_wakeup(device);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001277
Shubhrajyoti Datta82b1b2e2018-12-18 13:18:40 +01001278 if (console_suspend_enabled && uart_console(port) && may_wake) {
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001279 unsigned long flags = 0;
1280
1281 spin_lock_irqsave(&port->lock, flags);
1282 /* Empty the receive FIFO 1st before making changes */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001283 while (!(readl(port->membase + CDNS_UART_SR) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001284 CDNS_UART_SR_RXEMPTY))
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001285 readl(port->membase + CDNS_UART_FIFO);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001286 /* set RX trigger level to 1 */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001287 writel(1, port->membase + CDNS_UART_RXWM);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001288 /* disable RX timeout interrups */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001289 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001290 spin_unlock_irqrestore(&port->lock, flags);
1291 }
1292
Nava kishore Manne4b9d33c2018-09-03 15:10:51 +02001293 /*
1294 * Call the API provided in serial_core.c file which handles
1295 * the suspend.
1296 */
Michal Simek46a460f2018-09-03 15:10:52 +02001297 return uart_suspend_port(cdns_uart->cdns_uart_driver, port);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001298}
1299
1300/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001301 * cdns_uart_resume - Resume after a previous suspend
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001302 * @device: Pointer to the device structure
1303 *
Michal Simek489810a12014-04-04 17:23:37 -07001304 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001305 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001306static int cdns_uart_resume(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001307{
1308 struct uart_port *port = dev_get_drvdata(device);
Michal Simek46a460f2018-09-03 15:10:52 +02001309 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001310 unsigned long flags = 0;
1311 u32 ctrl_reg;
Nava kishore Manne4b9d33c2018-09-03 15:10:51 +02001312 int may_wake;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001313
Nava kishore Manne4b9d33c2018-09-03 15:10:51 +02001314 may_wake = device_may_wakeup(device);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001315
Shubhrajyoti Datta82b1b2e2018-12-18 13:18:40 +01001316 if (console_suspend_enabled && uart_console(port) && !may_wake) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001317 clk_enable(cdns_uart->pclk);
1318 clk_enable(cdns_uart->uartclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001319
1320 spin_lock_irqsave(&port->lock, flags);
1321
1322 /* Set TX/RX Reset */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001323 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001324 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001325 writel(ctrl_reg, port->membase + CDNS_UART_CR);
1326 while (readl(port->membase + CDNS_UART_CR) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001327 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001328 cpu_relax();
1329
1330 /* restore rx timeout value */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001331 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001332 /* Enable Tx/Rx */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001333 ctrl_reg = readl(port->membase + CDNS_UART_CR);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001334 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1335 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001336 writel(ctrl_reg, port->membase + CDNS_UART_CR);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001337
Shubhrajyoti Datta81e33b52017-03-15 08:38:56 -07001338 clk_disable(cdns_uart->uartclk);
1339 clk_disable(cdns_uart->pclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001340 spin_unlock_irqrestore(&port->lock, flags);
1341 } else {
1342 spin_lock_irqsave(&port->lock, flags);
1343 /* restore original rx trigger level */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001344 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001345 /* enable RX timeout interrupt */
Soren Brinkmanna8df6a52016-01-11 17:41:40 -08001346 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001347 spin_unlock_irqrestore(&port->lock, flags);
1348 }
1349
Michal Simek46a460f2018-09-03 15:10:52 +02001350 return uart_resume_port(cdns_uart->cdns_uart_driver, port);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001351}
1352#endif /* ! CONFIG_PM_SLEEP */
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301353static int __maybe_unused cdns_runtime_suspend(struct device *dev)
1354{
Wolfram Sanga406c4b2018-04-19 16:06:23 +02001355 struct uart_port *port = dev_get_drvdata(dev);
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301356 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001357
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301358 clk_disable(cdns_uart->uartclk);
1359 clk_disable(cdns_uart->pclk);
1360 return 0;
1361};
1362
1363static int __maybe_unused cdns_runtime_resume(struct device *dev)
1364{
Wolfram Sanga406c4b2018-04-19 16:06:23 +02001365 struct uart_port *port = dev_get_drvdata(dev);
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301366 struct cdns_uart *cdns_uart = port->private_data;
1367
1368 clk_enable(cdns_uart->pclk);
1369 clk_enable(cdns_uart->uartclk);
1370 return 0;
1371};
1372
1373static const struct dev_pm_ops cdns_uart_dev_pm_ops = {
1374 SET_SYSTEM_SLEEP_PM_OPS(cdns_uart_suspend, cdns_uart_resume)
1375 SET_RUNTIME_PM_OPS(cdns_runtime_suspend,
1376 cdns_runtime_resume, NULL)
1377};
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001378
Nava kishore Manne3816b2f82016-09-15 14:45:29 +05301379static const struct cdns_platform_data zynqmp_uart_def = {
1380 .quirks = CDNS_UART_RXBS_SUPPORT, };
1381
1382/* Match table for of_platform binding */
1383static const struct of_device_id cdns_uart_of_match[] = {
1384 { .compatible = "xlnx,xuartps", },
1385 { .compatible = "cdns,uart-r1p8", },
1386 { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
Nava kishore Manne0267a4f2016-10-12 13:17:27 +05301387 { .compatible = "xlnx,zynqmp-uart", .data = &zynqmp_uart_def },
Nava kishore Manne3816b2f82016-09-15 14:45:29 +05301388 {}
1389};
1390MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
1391
Michal Simekae1cca32018-09-20 13:41:53 +02001392/*
1393 * Maximum number of instances without alias IDs but if there is alias
1394 * which target "< MAX_UART_INSTANCES" range this ID can't be used.
1395 */
1396#define MAX_UART_INSTANCES 32
1397
1398/* Stores static aliases list */
1399static DECLARE_BITMAP(alias_bitmap, MAX_UART_INSTANCES);
1400static int alias_bitmap_initialized;
1401
1402/* Stores actual bitmap of allocated IDs with alias IDs together */
1403static DECLARE_BITMAP(bitmap, MAX_UART_INSTANCES);
1404/* Protect bitmap operations to have unique IDs */
1405static DEFINE_MUTEX(bitmap_lock);
1406
1407static int cdns_get_id(struct platform_device *pdev)
1408{
1409 int id, ret;
1410
1411 mutex_lock(&bitmap_lock);
1412
1413 /* Alias list is stable that's why get alias bitmap only once */
1414 if (!alias_bitmap_initialized) {
1415 ret = of_alias_get_alias_list(cdns_uart_of_match, "serial",
1416 alias_bitmap, MAX_UART_INSTANCES);
Michal Simek59eaeba2018-10-12 07:43:11 +02001417 if (ret && ret != -EOVERFLOW) {
Wei Yongjun823f4e52018-09-25 14:23:21 +00001418 mutex_unlock(&bitmap_lock);
Michal Simekae1cca32018-09-20 13:41:53 +02001419 return ret;
Wei Yongjun823f4e52018-09-25 14:23:21 +00001420 }
Michal Simekae1cca32018-09-20 13:41:53 +02001421
1422 alias_bitmap_initialized++;
1423 }
1424
1425 /* Make sure that alias ID is not taken by instance without alias */
1426 bitmap_or(bitmap, bitmap, alias_bitmap, MAX_UART_INSTANCES);
1427
1428 dev_dbg(&pdev->dev, "Alias bitmap: %*pb\n",
1429 MAX_UART_INSTANCES, bitmap);
1430
1431 /* Look for a serialN alias */
1432 id = of_alias_get_id(pdev->dev.of_node, "serial");
1433 if (id < 0) {
1434 dev_warn(&pdev->dev,
1435 "No serial alias passed. Using the first free id\n");
1436
1437 /*
1438 * Start with id 0 and check if there is no serial0 alias
1439 * which points to device which is compatible with this driver.
1440 * If alias exists then try next free position.
1441 */
1442 id = 0;
1443
1444 for (;;) {
1445 dev_info(&pdev->dev, "Checking id %d\n", id);
1446 id = find_next_zero_bit(bitmap, MAX_UART_INSTANCES, id);
1447
1448 /* No free empty instance */
1449 if (id == MAX_UART_INSTANCES) {
1450 dev_err(&pdev->dev, "No free ID\n");
1451 mutex_unlock(&bitmap_lock);
1452 return -EINVAL;
1453 }
1454
1455 dev_dbg(&pdev->dev, "The empty id is %d\n", id);
1456 /* Check if ID is empty */
1457 if (!test_and_set_bit(id, bitmap)) {
1458 /* Break the loop if bit is taken */
1459 dev_dbg(&pdev->dev,
1460 "Selected ID %d allocation passed\n",
1461 id);
1462 break;
1463 }
1464 dev_dbg(&pdev->dev,
1465 "Selected ID %d allocation failed\n", id);
1466 /* if taking bit fails then try next one */
1467 id++;
1468 }
1469 }
1470
1471 mutex_unlock(&bitmap_lock);
1472
1473 return id;
1474}
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 Simekbed25ac2018-09-03 15:10:58 +02001484 int rc, 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;
Michal Simek024ca322018-09-03 15:10:57 +02001489 struct uart_driver *cdns_uart_uart_driver;
1490 char *driver_name;
1491#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1492 struct console *cdns_uart_console;
1493#endif
John Linn61ec9012011-04-30 00:07:43 -04001494
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001495 cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
Soren Brinkmannc03cae12013-10-17 14:08:05 -07001496 GFP_KERNEL);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001497 if (!cdns_uart_data)
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001498 return -ENOMEM;
Michal Simek0a84bae2018-04-23 11:51:03 +02001499 port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
1500 if (!port)
1501 return -ENOMEM;
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001502
Michal Simek024ca322018-09-03 15:10:57 +02001503 cdns_uart_uart_driver = devm_kzalloc(&pdev->dev,
1504 sizeof(*cdns_uart_uart_driver),
1505 GFP_KERNEL);
1506 if (!cdns_uart_uart_driver)
1507 return -ENOMEM;
1508
Michal Simekae1cca32018-09-20 13:41:53 +02001509 cdns_uart_data->id = cdns_get_id(pdev);
Michal Simekbed25ac2018-09-03 15:10:58 +02001510 if (cdns_uart_data->id < 0)
Michal Simekae1cca32018-09-20 13:41:53 +02001511 return cdns_uart_data->id;
Michal Simek14090ad2018-09-03 15:10:53 +02001512
Michal Simek024ca322018-09-03 15:10:57 +02001513 /* There is a need to use unique driver name */
1514 driver_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%d",
Michal Simekbed25ac2018-09-03 15:10:58 +02001515 CDNS_UART_NAME, cdns_uart_data->id);
Michal Simekae1cca32018-09-20 13:41:53 +02001516 if (!driver_name) {
1517 rc = -ENOMEM;
1518 goto err_out_id;
1519 }
Michal Simek024ca322018-09-03 15:10:57 +02001520
1521 cdns_uart_uart_driver->owner = THIS_MODULE;
1522 cdns_uart_uart_driver->driver_name = driver_name;
1523 cdns_uart_uart_driver->dev_name = CDNS_UART_TTY_NAME;
Shubhrajyoti Dattaab262662019-06-12 13:14:38 +02001524 cdns_uart_uart_driver->major = uartps_major;
Michal Simekbed25ac2018-09-03 15:10:58 +02001525 cdns_uart_uart_driver->minor = cdns_uart_data->id;
Michal Simek024ca322018-09-03 15:10:57 +02001526 cdns_uart_uart_driver->nr = 1;
1527
Michal Simek10a53152018-09-03 15:10:55 +02001528#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
Michal Simek024ca322018-09-03 15:10:57 +02001529 cdns_uart_console = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_console),
1530 GFP_KERNEL);
Michal Simek32cf21a2018-12-18 13:18:41 +01001531 if (!cdns_uart_console) {
1532 rc = -ENOMEM;
1533 goto err_out_id;
1534 }
Michal Simek024ca322018-09-03 15:10:57 +02001535
1536 strncpy(cdns_uart_console->name, CDNS_UART_TTY_NAME,
1537 sizeof(cdns_uart_console->name));
Michal Simekbed25ac2018-09-03 15:10:58 +02001538 cdns_uart_console->index = cdns_uart_data->id;
Michal Simek024ca322018-09-03 15:10:57 +02001539 cdns_uart_console->write = cdns_uart_console_write;
1540 cdns_uart_console->device = uart_console_device;
1541 cdns_uart_console->setup = cdns_uart_console_setup;
1542 cdns_uart_console->flags = CON_PRINTBUFFER;
1543 cdns_uart_console->data = cdns_uart_uart_driver;
1544 cdns_uart_uart_driver->cons = cdns_uart_console;
Michal Simek10a53152018-09-03 15:10:55 +02001545#endif
1546
Michal Simek024ca322018-09-03 15:10:57 +02001547 rc = uart_register_driver(cdns_uart_uart_driver);
1548 if (rc < 0) {
1549 dev_err(&pdev->dev, "Failed to register driver\n");
Michal Simekae1cca32018-09-20 13:41:53 +02001550 goto err_out_id;
Michal Simeke4bbb512018-09-03 15:10:54 +02001551 }
1552
Michal Simek024ca322018-09-03 15:10:57 +02001553 cdns_uart_data->cdns_uart_driver = cdns_uart_uart_driver;
1554
1555 /*
1556 * Setting up proper name_base needs to be done after uart
1557 * registration because tty_driver structure is not filled.
1558 * name_base is 0 by default.
1559 */
Michal Simekbed25ac2018-09-03 15:10:58 +02001560 cdns_uart_uart_driver->tty_driver->name_base = cdns_uart_data->id;
Michal Simek46a460f2018-09-03 15:10:52 +02001561
Nava kishore Manne3816b2f82016-09-15 14:45:29 +05301562 match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
1563 if (match && match->data) {
1564 const struct cdns_platform_data *data = match->data;
1565
1566 cdns_uart_data->quirks = data->quirks;
1567 }
1568
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001569 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
Rajan Vajaf6949362019-03-01 01:37:08 -08001570 if (PTR_ERR(cdns_uart_data->pclk) == -EPROBE_DEFER) {
Greg Kroah-Hartman82ca0d542018-12-06 12:47:22 +01001571 rc = PTR_ERR(cdns_uart_data->pclk);
1572 goto err_out_unregister_driver;
Josh Cartwright2326669c2013-01-21 19:57:41 +01001573 }
1574
Rajan Vajaf6949362019-03-01 01:37:08 -08001575 if (IS_ERR(cdns_uart_data->pclk)) {
1576 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1577 if (IS_ERR(cdns_uart_data->pclk)) {
1578 rc = PTR_ERR(cdns_uart_data->pclk);
1579 goto err_out_unregister_driver;
1580 }
1581 dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
Greg Kroah-Hartman82ca0d542018-12-06 12:47:22 +01001582 }
Rajan Vajaf6949362019-03-01 01:37:08 -08001583
1584 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1585 if (PTR_ERR(cdns_uart_data->uartclk) == -EPROBE_DEFER) {
Greg Kroah-Hartman82ca0d542018-12-06 12:47:22 +01001586 rc = PTR_ERR(cdns_uart_data->uartclk);
1587 goto err_out_unregister_driver;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001588 }
1589
Rajan Vajaf6949362019-03-01 01:37:08 -08001590 if (IS_ERR(cdns_uart_data->uartclk)) {
1591 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1592 if (IS_ERR(cdns_uart_data->uartclk)) {
1593 rc = PTR_ERR(cdns_uart_data->uartclk);
1594 goto err_out_unregister_driver;
1595 }
1596 dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1597 }
1598
Shubhrajyoti Dattaecfc5772017-04-06 12:27:41 +05301599 rc = clk_prepare_enable(cdns_uart_data->pclk);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001600 if (rc) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001601 dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
Michal Simeke4bbb512018-09-03 15:10:54 +02001602 goto err_out_unregister_driver;
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001603 }
Shubhrajyoti Dattaecfc5772017-04-06 12:27:41 +05301604 rc = clk_prepare_enable(cdns_uart_data->uartclk);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001605 if (rc) {
1606 dev_err(&pdev->dev, "Unable to enable device clock.\n");
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001607 goto err_out_clk_dis_pclk;
John Linn61ec9012011-04-30 00:07:43 -04001608 }
1609
1610 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001611 if (!res) {
1612 rc = -ENODEV;
1613 goto err_out_clk_disable;
1614 }
John Linn61ec9012011-04-30 00:07:43 -04001615
Michal Simek5c90c072015-04-13 16:34:21 +02001616 irq = platform_get_irq(pdev, 0);
1617 if (irq <= 0) {
1618 rc = -ENXIO;
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001619 goto err_out_clk_disable;
1620 }
John Linn61ec9012011-04-30 00:07:43 -04001621
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001622#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001623 cdns_uart_data->clk_rate_change_nb.notifier_call =
1624 cdns_uart_clk_notifier_cb;
1625 if (clk_notifier_register(cdns_uart_data->uartclk,
1626 &cdns_uart_data->clk_rate_change_nb))
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001627 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001628#endif
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001629
Michal Simek1f210722018-04-23 11:51:02 +02001630 /* At this point, we've got an empty uart_port struct, initialize it */
1631 spin_lock_init(&port->lock);
Michal Simek1f210722018-04-23 11:51:02 +02001632 port->type = PORT_UNKNOWN;
1633 port->iotype = UPIO_MEM32;
1634 port->flags = UPF_BOOT_AUTOCONF;
1635 port->ops = &cdns_uart_ops;
1636 port->fifosize = CDNS_UART_FIFO_SIZE;
Michal Simek1f210722018-04-23 11:51:02 +02001637
Soren Brinkmann354fb1a2016-01-11 17:41:38 -08001638 /*
1639 * Register the port.
1640 * This function also registers this device with the tty layer
1641 * and triggers invocation of the config_port() entry point.
1642 */
1643 port->mapbase = res->start;
1644 port->irq = irq;
1645 port->dev = &pdev->dev;
1646 port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1647 port->private_data = cdns_uart_data;
1648 cdns_uart_data->port = port;
1649 platform_set_drvdata(pdev, port);
1650
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301651 pm_runtime_use_autosuspend(&pdev->dev);
1652 pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301653 pm_runtime_set_active(&pdev->dev);
Shubhrajyoti Dattaecfc5772017-04-06 12:27:41 +05301654 pm_runtime_enable(&pdev->dev);
Shubhrajyoti Datta86df8dd2018-12-18 13:18:39 +01001655 device_init_wakeup(port->dev, true);
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301656
Michal Simek0413fe02018-04-23 11:51:01 +02001657#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1658 /*
1659 * If console hasn't been found yet try to assign this port
1660 * because it is required to be assigned for console setup function.
1661 * If register_console() don't assign value, then console_port pointer
1662 * is cleanup.
1663 */
Michal Simek427c8ae2018-09-03 15:10:56 +02001664 if (!console_port)
Michal Simek0413fe02018-04-23 11:51:01 +02001665 console_port = port;
1666#endif
1667
Michal Simek024ca322018-09-03 15:10:57 +02001668 rc = uart_add_one_port(cdns_uart_uart_driver, port);
Soren Brinkmann354fb1a2016-01-11 17:41:38 -08001669 if (rc) {
1670 dev_err(&pdev->dev,
1671 "uart_add_one_port() failed; err=%i\n", rc);
Shubhrajyoti Dattad653c432017-05-09 11:50:32 +05301672 goto err_out_pm_disable;
Soren Brinkmann354fb1a2016-01-11 17:41:38 -08001673 }
1674
Michal Simek0413fe02018-04-23 11:51:01 +02001675#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1676 /* This is not port which is used for console that's why clean it up */
Michal Simek427c8ae2018-09-03 15:10:56 +02001677 if (console_port == port &&
Michal Simek024ca322018-09-03 15:10:57 +02001678 !(cdns_uart_uart_driver->cons->flags & CON_ENABLED))
Michal Simek0413fe02018-04-23 11:51:01 +02001679 console_port = NULL;
1680#endif
1681
Michal Simek5e9bd2d2019-10-04 15:04:11 +02001682 uartps_major = cdns_uart_uart_driver->tty_driver->major;
Shubhrajyoti Datta18631782019-04-09 13:15:24 +05301683 cdns_uart_data->cts_override = of_property_read_bool(pdev->dev.of_node,
1684 "cts-override");
Soren Brinkmann354fb1a2016-01-11 17:41:38 -08001685 return 0;
1686
Shubhrajyoti Dattad653c432017-05-09 11:50:32 +05301687err_out_pm_disable:
1688 pm_runtime_disable(&pdev->dev);
1689 pm_runtime_set_suspended(&pdev->dev);
1690 pm_runtime_dont_use_autosuspend(&pdev->dev);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001691#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001692 clk_notifier_unregister(cdns_uart_data->uartclk,
1693 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001694#endif
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001695err_out_clk_disable:
Shubhrajyoti Dattaecfc5772017-04-06 12:27:41 +05301696 clk_disable_unprepare(cdns_uart_data->uartclk);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001697err_out_clk_dis_pclk:
Shubhrajyoti Dattaecfc5772017-04-06 12:27:41 +05301698 clk_disable_unprepare(cdns_uart_data->pclk);
Michal Simeke4bbb512018-09-03 15:10:54 +02001699err_out_unregister_driver:
Michal Simek024ca322018-09-03 15:10:57 +02001700 uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
Michal Simekae1cca32018-09-20 13:41:53 +02001701err_out_id:
1702 mutex_lock(&bitmap_lock);
Michal Simek2088cfd2018-10-08 14:17:19 +02001703 if (cdns_uart_data->id < MAX_UART_INSTANCES)
1704 clear_bit(cdns_uart_data->id, bitmap);
Michal Simekae1cca32018-09-20 13:41:53 +02001705 mutex_unlock(&bitmap_lock);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001706 return rc;
John Linn61ec9012011-04-30 00:07:43 -04001707}
1708
1709/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001710 * cdns_uart_remove - called when the platform driver is unregistered
John Linn61ec9012011-04-30 00:07:43 -04001711 * @pdev: Pointer to the platform device structure
1712 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001713 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001714 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001715static int cdns_uart_remove(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -04001716{
Jingoo Han696faed2013-05-23 19:39:36 +09001717 struct uart_port *port = platform_get_drvdata(pdev);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001718 struct cdns_uart *cdns_uart_data = port->private_data;
Josh Cartwright2326669c2013-01-21 19:57:41 +01001719 int rc;
John Linn61ec9012011-04-30 00:07:43 -04001720
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001721 /* Remove the cdns_uart port from the serial core */
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001722#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001723 clk_notifier_unregister(cdns_uart_data->uartclk,
1724 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001725#endif
Michal Simek46a460f2018-09-03 15:10:52 +02001726 rc = uart_remove_one_port(cdns_uart_data->cdns_uart_driver, port);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001727 port->mapbase = 0;
Michal Simekae1cca32018-09-20 13:41:53 +02001728 mutex_lock(&bitmap_lock);
Michal Simek2088cfd2018-10-08 14:17:19 +02001729 if (cdns_uart_data->id < MAX_UART_INSTANCES)
1730 clear_bit(cdns_uart_data->id, bitmap);
Michal Simekae1cca32018-09-20 13:41:53 +02001731 mutex_unlock(&bitmap_lock);
Shubhrajyoti Dattaecfc5772017-04-06 12:27:41 +05301732 clk_disable_unprepare(cdns_uart_data->uartclk);
1733 clk_disable_unprepare(cdns_uart_data->pclk);
Shubhrajyoti Dattad62100f2017-03-09 11:19:07 +05301734 pm_runtime_disable(&pdev->dev);
1735 pm_runtime_set_suspended(&pdev->dev);
1736 pm_runtime_dont_use_autosuspend(&pdev->dev);
Shubhrajyoti Datta86df8dd2018-12-18 13:18:39 +01001737 device_init_wakeup(&pdev->dev, false);
Michal Simek427c8ae2018-09-03 15:10:56 +02001738
1739#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1740 if (console_port == port)
1741 console_port = NULL;
1742#endif
1743
Michal Simek5e9bd2d2019-10-04 15:04:11 +02001744 /* If this is last instance major number should be initialized */
1745 mutex_lock(&bitmap_lock);
1746 if (bitmap_empty(bitmap, MAX_UART_INSTANCES))
1747 uartps_major = 0;
1748 mutex_unlock(&bitmap_lock);
1749
Michal Simek024ca322018-09-03 15:10:57 +02001750 uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001751 return rc;
1752}
1753
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001754static struct platform_driver cdns_uart_platform_driver = {
1755 .probe = cdns_uart_probe,
1756 .remove = cdns_uart_remove,
John Linn61ec9012011-04-30 00:07:43 -04001757 .driver = {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001758 .name = CDNS_UART_NAME,
1759 .of_match_table = cdns_uart_of_match,
1760 .pm = &cdns_uart_dev_pm_ops,
Anders Roxell64609792018-10-30 12:35:44 +01001761 .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_XILINX_PS_UART),
John Linn61ec9012011-04-30 00:07:43 -04001762 },
1763};
1764
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001765static int __init cdns_uart_init(void)
John Linn61ec9012011-04-30 00:07:43 -04001766{
John Linn61ec9012011-04-30 00:07:43 -04001767 /* Register the platform driver */
Michal Simeke4bbb512018-09-03 15:10:54 +02001768 return platform_driver_register(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001769}
1770
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001771static void __exit cdns_uart_exit(void)
John Linn61ec9012011-04-30 00:07:43 -04001772{
John Linn61ec9012011-04-30 00:07:43 -04001773 /* Unregister the platform driver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001774 platform_driver_unregister(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001775}
1776
Shubhrajyoti Datta1d672432017-09-20 12:20:11 +05301777arch_initcall(cdns_uart_init);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001778module_exit(cdns_uart_exit);
John Linn61ec9012011-04-30 00:07:43 -04001779
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001780MODULE_DESCRIPTION("Driver for Cadence UART");
John Linn61ec9012011-04-30 00:07:43 -04001781MODULE_AUTHOR("Xilinx Inc.");
1782MODULE_LICENSE("GPL");