blob: f218ec658f5d200e415d6bdf7503f7bc83f7a825 [file] [log] [blame]
John Linn61ec9012011-04-30 00:07:43 -04001/*
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07002 * Cadence UART driver (found in Xilinx Zynq)
John Linn61ec9012011-04-30 00:07:43 -04003 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07004 * 2011 - 2014 (C) Xilinx Inc.
John Linn61ec9012011-04-30 00:07:43 -04005 *
6 * This program is free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation;
9 * either version 2 of the License, or (at your option) any
10 * later version.
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070011 *
12 * This driver has originally been pushed by Xilinx using a Zynq-branding. This
13 * still shows in the naming of this file, the kconfig symbols and some symbols
14 * in the code.
John Linn61ec9012011-04-30 00:07:43 -040015 */
16
Vlad Lungu0c0c47b2013-10-17 14:08:06 -070017#if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18#define SUPPORT_SYSRQ
19#endif
20
John Linn61ec9012011-04-30 00:07:43 -040021#include <linux/platform_device.h>
John Linn61ec9012011-04-30 00:07:43 -040022#include <linux/serial.h>
Vlad Lungu0c0c47b2013-10-17 14:08:06 -070023#include <linux/console.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020024#include <linux/serial_core.h>
Soren Brinkmann30e1e282013-05-13 10:46:38 -070025#include <linux/slab.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020026#include <linux/tty.h>
27#include <linux/tty_flip.h>
Josh Cartwright2326669c2013-01-21 19:57:41 +010028#include <linux/clk.h>
John Linn61ec9012011-04-30 00:07:43 -040029#include <linux/irq.h>
30#include <linux/io.h>
31#include <linux/of.h>
Paul Gortmaker578b9ce2011-05-27 16:14:23 -040032#include <linux/module.h>
John Linn61ec9012011-04-30 00:07:43 -040033
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070034#define CDNS_UART_TTY_NAME "ttyPS"
35#define CDNS_UART_NAME "xuartps"
36#define CDNS_UART_MAJOR 0 /* use dynamic node allocation */
37#define CDNS_UART_MINOR 0 /* works best with devtmpfs */
38#define CDNS_UART_NR_PORTS 2
39#define CDNS_UART_FIFO_SIZE 64 /* FIFO size */
Thomas Betker9646e4f2015-03-11 22:39:25 +010040#define CDNS_UART_REGISTER_SPACE 0x1000
John Linn61ec9012011-04-30 00:07:43 -040041
Suneel85baf542013-10-17 14:08:08 -070042/* Rx Trigger level */
43static int rx_trigger_level = 56;
44module_param(rx_trigger_level, uint, S_IRUGO);
45MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
46
47/* Rx Timeout */
48static int rx_timeout = 10;
49module_param(rx_timeout, uint, S_IRUGO);
50MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
51
Soren Brinkmanne555a212014-04-04 17:23:39 -070052/* Register offsets for the UART. */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070053#define CDNS_UART_CR_OFFSET 0x00 /* Control Register */
54#define CDNS_UART_MR_OFFSET 0x04 /* Mode Register */
55#define CDNS_UART_IER_OFFSET 0x08 /* Interrupt Enable */
56#define CDNS_UART_IDR_OFFSET 0x0C /* Interrupt Disable */
57#define CDNS_UART_IMR_OFFSET 0x10 /* Interrupt Mask */
58#define CDNS_UART_ISR_OFFSET 0x14 /* Interrupt Status */
59#define CDNS_UART_BAUDGEN_OFFSET 0x18 /* Baud Rate Generator */
60#define CDNS_UART_RXTOUT_OFFSET 0x1C /* RX Timeout */
61#define CDNS_UART_RXWM_OFFSET 0x20 /* RX FIFO Trigger Level */
62#define CDNS_UART_MODEMCR_OFFSET 0x24 /* Modem Control */
63#define CDNS_UART_MODEMSR_OFFSET 0x28 /* Modem Status */
64#define CDNS_UART_SR_OFFSET 0x2C /* Channel Status */
65#define CDNS_UART_FIFO_OFFSET 0x30 /* FIFO */
66#define CDNS_UART_BAUDDIV_OFFSET 0x34 /* Baud Rate Divider */
67#define CDNS_UART_FLOWDEL_OFFSET 0x38 /* Flow Delay */
68#define CDNS_UART_IRRX_PWIDTH_OFFSET 0x3C /* IR Min Received Pulse Width */
69#define CDNS_UART_IRTX_PWIDTH_OFFSET 0x40 /* IR Transmitted pulse Width */
70#define CDNS_UART_TXWM_OFFSET 0x44 /* TX FIFO Trigger Level */
John Linn61ec9012011-04-30 00:07:43 -040071
Soren Brinkmanne555a212014-04-04 17:23:39 -070072/* Control Register Bit Definitions */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070073#define CDNS_UART_CR_STOPBRK 0x00000100 /* Stop TX break */
74#define CDNS_UART_CR_STARTBRK 0x00000080 /* Set TX break */
75#define CDNS_UART_CR_TX_DIS 0x00000020 /* TX disabled. */
76#define CDNS_UART_CR_TX_EN 0x00000010 /* TX enabled */
77#define CDNS_UART_CR_RX_DIS 0x00000008 /* RX disabled. */
78#define CDNS_UART_CR_RX_EN 0x00000004 /* RX enabled */
79#define CDNS_UART_CR_TXRST 0x00000002 /* TX logic reset */
80#define CDNS_UART_CR_RXRST 0x00000001 /* RX logic reset */
81#define CDNS_UART_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
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 */
John Linn61ec9012011-04-30 00:07:43 -040092
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070093#define CDNS_UART_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
94#define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
John Linn61ec9012011-04-30 00:07:43 -040095
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070096#define CDNS_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
97#define CDNS_UART_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
98#define CDNS_UART_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
99#define CDNS_UART_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
100#define CDNS_UART_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
John Linn61ec9012011-04-30 00:07:43 -0400101
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700102#define CDNS_UART_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
103#define CDNS_UART_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
104#define CDNS_UART_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
John Linn61ec9012011-04-30 00:07:43 -0400105
Soren Brinkmanne555a212014-04-04 17:23:39 -0700106/*
107 * Interrupt Registers:
John Linn61ec9012011-04-30 00:07:43 -0400108 * Interrupt control logic uses the interrupt enable register (IER) and the
109 * interrupt disable register (IDR) to set the value of the bits in the
110 * interrupt mask register (IMR). The IMR determines whether to pass an
111 * interrupt to the interrupt status register (ISR).
112 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
113 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
114 * Reading either IER or IDR returns 0x00.
John Linn61ec9012011-04-30 00:07:43 -0400115 * All four registers have the same bit definitions.
116 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700117#define CDNS_UART_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
118#define CDNS_UART_IXR_PARITY 0x00000080 /* Parity error interrupt */
119#define CDNS_UART_IXR_FRAMING 0x00000040 /* Framing error interrupt */
120#define CDNS_UART_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
121#define CDNS_UART_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
122#define CDNS_UART_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
123#define CDNS_UART_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
124#define CDNS_UART_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
125#define CDNS_UART_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
126#define CDNS_UART_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
127#define CDNS_UART_IXR_MASK 0x00001FFF /* Valid bit mask */
John Linn61ec9012011-04-30 00:07:43 -0400128
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700129/* Goes in read_status_mask for break detection as the HW doesn't do it*/
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700130#define CDNS_UART_IXR_BRK 0x80000000
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700131
Soren Brinkmanne555a212014-04-04 17:23:39 -0700132/*
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +0100133 * Modem Control register:
134 * The read/write Modem Control register controls the interface with the modem
135 * or data set, or a peripheral device emulating a modem.
136 */
137#define CDNS_UART_MODEMCR_FCM 0x00000020 /* Automatic flow control mode */
138#define CDNS_UART_MODEMCR_RTS 0x00000002 /* Request to send output control */
139#define CDNS_UART_MODEMCR_DTR 0x00000001 /* Data Terminal Ready */
140
141/*
Soren Brinkmanne555a212014-04-04 17:23:39 -0700142 * Channel Status Register:
John Linn61ec9012011-04-30 00:07:43 -0400143 * The channel status register (CSR) is provided to enable the control logic
144 * to monitor the status of bits in the channel interrupt status register,
145 * even if these are masked out by the interrupt mask register.
146 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700147#define CDNS_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
148#define CDNS_UART_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
149#define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
150#define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
John Linn61ec9012011-04-30 00:07:43 -0400151
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700152/* baud dividers min/max values */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700153#define CDNS_UART_BDIV_MIN 4
154#define CDNS_UART_BDIV_MAX 255
155#define CDNS_UART_CD_MAX 65535
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700156
John Linn61ec9012011-04-30 00:07:43 -0400157/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700158 * struct cdns_uart - device data
Michal Simek489810a12014-04-04 17:23:37 -0700159 * @port: Pointer to the UART port
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700160 * @uartclk: Reference clock
161 * @pclk: APB clock
Michal Simek489810a12014-04-04 17:23:37 -0700162 * @baud: Current baud rate
163 * @clk_rate_change_nb: Notifier block for clock changes
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700164 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700165struct cdns_uart {
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700166 struct uart_port *port;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700167 struct clk *uartclk;
168 struct clk *pclk;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700169 unsigned int baud;
170 struct notifier_block clk_rate_change_nb;
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700171};
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700172#define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
173 clk_rate_change_nb);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700174
175/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700176 * cdns_uart_isr - Interrupt handler
John Linn61ec9012011-04-30 00:07:43 -0400177 * @irq: Irq number
178 * @dev_id: Id of the port
179 *
Michal Simek489810a12014-04-04 17:23:37 -0700180 * Return: IRQHANDLED
181 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700182static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
John Linn61ec9012011-04-30 00:07:43 -0400183{
184 struct uart_port *port = (struct uart_port *)dev_id;
John Linn61ec9012011-04-30 00:07:43 -0400185 unsigned long flags;
186 unsigned int isrstatus, numbytes;
187 unsigned int data;
188 char status = TTY_NORMAL;
189
John Linn61ec9012011-04-30 00:07:43 -0400190 spin_lock_irqsave(&port->lock, flags);
191
192 /* Read the interrupt status register to determine which
193 * interrupt(s) is/are active.
194 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100195 isrstatus = readl(port->membase + CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400196
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700197 /*
198 * There is no hardware break detection, so we interpret framing
199 * error with all-zeros data as a break sequence. Most of the time,
200 * there's another non-zero byte at the end of the sequence.
201 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700202 if (isrstatus & CDNS_UART_IXR_FRAMING) {
Thomas Betker19f22ef2015-03-12 22:11:59 +0100203 while (!(readl(port->membase + CDNS_UART_SR_OFFSET) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700204 CDNS_UART_SR_RXEMPTY)) {
Thomas Betker19f22ef2015-03-12 22:11:59 +0100205 if (!readl(port->membase + CDNS_UART_FIFO_OFFSET)) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700206 port->read_status_mask |= CDNS_UART_IXR_BRK;
207 isrstatus &= ~CDNS_UART_IXR_FRAMING;
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700208 }
209 }
Thomas Betker19f22ef2015-03-12 22:11:59 +0100210 writel(CDNS_UART_IXR_FRAMING,
211 port->membase + CDNS_UART_ISR_OFFSET);
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700212 }
213
John Linn61ec9012011-04-30 00:07:43 -0400214 /* drop byte with parity error if IGNPAR specified */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700215 if (isrstatus & port->ignore_status_mask & CDNS_UART_IXR_PARITY)
216 isrstatus &= ~(CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT);
John Linn61ec9012011-04-30 00:07:43 -0400217
218 isrstatus &= port->read_status_mask;
219 isrstatus &= ~port->ignore_status_mask;
220
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700221 if ((isrstatus & CDNS_UART_IXR_TOUT) ||
222 (isrstatus & CDNS_UART_IXR_RXTRIG)) {
John Linn61ec9012011-04-30 00:07:43 -0400223 /* Receive Timeout Interrupt */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100224 while (!(readl(port->membase + CDNS_UART_SR_OFFSET) &
225 CDNS_UART_SR_RXEMPTY)) {
226 data = readl(port->membase + CDNS_UART_FIFO_OFFSET);
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700227
228 /* Non-NULL byte after BREAK is garbage (99%) */
229 if (data && (port->read_status_mask &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700230 CDNS_UART_IXR_BRK)) {
231 port->read_status_mask &= ~CDNS_UART_IXR_BRK;
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700232 port->icount.brk++;
233 if (uart_handle_break(port))
234 continue;
235 }
236
Soren Brinkmannc2db11e2013-12-02 11:38:38 -0800237#ifdef SUPPORT_SYSRQ
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700238 /*
239 * uart_handle_sysrq_char() doesn't work if
240 * spinlocked, for some reason
241 */
242 if (port->sysrq) {
243 spin_unlock(&port->lock);
244 if (uart_handle_sysrq_char(port,
245 (unsigned char)data)) {
246 spin_lock(&port->lock);
247 continue;
248 }
249 spin_lock(&port->lock);
250 }
Soren Brinkmannc2db11e2013-12-02 11:38:38 -0800251#endif
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700252
John Linn61ec9012011-04-30 00:07:43 -0400253 port->icount.rx++;
254
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700255 if (isrstatus & CDNS_UART_IXR_PARITY) {
John Linn61ec9012011-04-30 00:07:43 -0400256 port->icount.parity++;
257 status = TTY_PARITY;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700258 } else if (isrstatus & CDNS_UART_IXR_FRAMING) {
John Linn61ec9012011-04-30 00:07:43 -0400259 port->icount.frame++;
260 status = TTY_FRAME;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700261 } else if (isrstatus & CDNS_UART_IXR_OVERRUN) {
John Linn61ec9012011-04-30 00:07:43 -0400262 port->icount.overrun++;
Soren Brinkmanne555a212014-04-04 17:23:39 -0700263 }
John Linn61ec9012011-04-30 00:07:43 -0400264
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700265 uart_insert_char(port, isrstatus, CDNS_UART_IXR_OVERRUN,
Jiri Slaby2e124b42013-01-03 15:53:06 +0100266 data, status);
John Linn61ec9012011-04-30 00:07:43 -0400267 }
268 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100269 tty_flip_buffer_push(&port->state->port);
John Linn61ec9012011-04-30 00:07:43 -0400270 spin_lock(&port->lock);
271 }
272
273 /* Dispatch an appropriate handler */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700274 if ((isrstatus & CDNS_UART_IXR_TXEMPTY) == CDNS_UART_IXR_TXEMPTY) {
John Linn61ec9012011-04-30 00:07:43 -0400275 if (uart_circ_empty(&port->state->xmit)) {
Thomas Betker19f22ef2015-03-12 22:11:59 +0100276 writel(CDNS_UART_IXR_TXEMPTY,
277 port->membase + CDNS_UART_IDR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400278 } else {
279 numbytes = port->fifosize;
280 /* Break if no more data available in the UART buffer */
281 while (numbytes--) {
282 if (uart_circ_empty(&port->state->xmit))
283 break;
284 /* Get the data from the UART circular buffer
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700285 * and write it to the cdns_uart's TX_FIFO
John Linn61ec9012011-04-30 00:07:43 -0400286 * register.
287 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100288 writel(port->state->xmit.buf[
289 port->state->xmit.tail],
290 port->membase + CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400291
292 port->icount.tx++;
293
294 /* Adjust the tail of the UART buffer and wrap
295 * the buffer if it reaches limit.
296 */
297 port->state->xmit.tail =
Soren Brinkmanne555a212014-04-04 17:23:39 -0700298 (port->state->xmit.tail + 1) &
John Linn61ec9012011-04-30 00:07:43 -0400299 (UART_XMIT_SIZE - 1);
300 }
301
302 if (uart_circ_chars_pending(
303 &port->state->xmit) < WAKEUP_CHARS)
304 uart_write_wakeup(port);
305 }
306 }
307
Thomas Betker19f22ef2015-03-12 22:11:59 +0100308 writel(isrstatus, port->membase + CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400309
310 /* be sure to release the lock and tty before leaving */
311 spin_unlock_irqrestore(&port->lock, flags);
John Linn61ec9012011-04-30 00:07:43 -0400312
313 return IRQ_HANDLED;
314}
315
316/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700317 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700318 * @clk: UART module input clock
319 * @baud: Desired baud rate
320 * @rbdiv: BDIV value (return value)
321 * @rcd: CD value (return value)
322 * @div8: Value for clk_sel bit in mod (return value)
Michal Simek489810a12014-04-04 17:23:37 -0700323 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700324 * was too much error, zero if no valid divisors are found.
325 *
326 * Formula to obtain baud rate is
327 * baud_tx/rx rate = clk/CD * (BDIV + 1)
328 * input_clk = (Uart User Defined Clock or Apb Clock)
329 * depends on UCLKEN in MR Reg
330 * clk = input_clk or input_clk/8;
331 * depends on CLKS in MR reg
332 * CD and BDIV depends on values in
333 * baud rate generate register
334 * baud rate clock divisor register
335 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700336static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
337 unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
John Linn61ec9012011-04-30 00:07:43 -0400338{
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700339 u32 cd, bdiv;
340 unsigned int calc_baud;
341 unsigned int bestbaud = 0;
John Linn61ec9012011-04-30 00:07:43 -0400342 unsigned int bauderror;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700343 unsigned int besterror = ~0;
John Linn61ec9012011-04-30 00:07:43 -0400344
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700345 if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700346 *div8 = 1;
347 clk /= 8;
348 } else {
349 *div8 = 0;
350 }
John Linn61ec9012011-04-30 00:07:43 -0400351
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700352 for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700353 cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700354 if (cd < 1 || cd > CDNS_UART_CD_MAX)
John Linn61ec9012011-04-30 00:07:43 -0400355 continue;
356
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700357 calc_baud = clk / (cd * (bdiv + 1));
John Linn61ec9012011-04-30 00:07:43 -0400358
359 if (baud > calc_baud)
360 bauderror = baud - calc_baud;
361 else
362 bauderror = calc_baud - baud;
363
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700364 if (besterror > bauderror) {
365 *rbdiv = bdiv;
366 *rcd = cd;
367 bestbaud = calc_baud;
368 besterror = bauderror;
John Linn61ec9012011-04-30 00:07:43 -0400369 }
370 }
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700371 /* use the values when percent error is acceptable */
372 if (((besterror * 100) / baud) < 3)
373 bestbaud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400374
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700375 return bestbaud;
376}
377
378/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700379 * cdns_uart_set_baud_rate - Calculate and set the baud rate
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700380 * @port: Handle to the uart port structure
381 * @baud: Baud rate to set
Michal Simek489810a12014-04-04 17:23:37 -0700382 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700383 * was too much error, zero if no valid divisors are found.
384 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700385static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700386 unsigned int baud)
387{
388 unsigned int calc_baud;
Soren Brinkmannd54b1812013-10-21 16:40:59 -0700389 u32 cd = 0, bdiv = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700390 u32 mreg;
391 int div8;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700392 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700393
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700394 calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700395 &div8);
396
397 /* Write new divisors to hardware */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100398 mreg = readl(port->membase + CDNS_UART_MR_OFFSET);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700399 if (div8)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700400 mreg |= CDNS_UART_MR_CLKSEL;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700401 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700402 mreg &= ~CDNS_UART_MR_CLKSEL;
Thomas Betker19f22ef2015-03-12 22:11:59 +0100403 writel(mreg, port->membase + CDNS_UART_MR_OFFSET);
404 writel(cd, port->membase + CDNS_UART_BAUDGEN_OFFSET);
405 writel(bdiv, port->membase + CDNS_UART_BAUDDIV_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700406 cdns_uart->baud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400407
408 return calc_baud;
409}
410
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700411#ifdef CONFIG_COMMON_CLK
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700412/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700413 * cdns_uart_clk_notitifer_cb - Clock notifier callback
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700414 * @nb: Notifier block
415 * @event: Notify event
416 * @data: Notifier data
Soren Brinkmanne555a212014-04-04 17:23:39 -0700417 * Return: NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700418 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700419static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700420 unsigned long event, void *data)
421{
422 u32 ctrl_reg;
423 struct uart_port *port;
424 int locked = 0;
425 struct clk_notifier_data *ndata = data;
426 unsigned long flags = 0;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700427 struct cdns_uart *cdns_uart = to_cdns_uart(nb);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700428
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700429 port = cdns_uart->port;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700430 if (port->suspended)
431 return NOTIFY_OK;
432
433 switch (event) {
434 case PRE_RATE_CHANGE:
435 {
Soren Brinkmanne555a212014-04-04 17:23:39 -0700436 u32 bdiv, cd;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700437 int div8;
438
439 /*
440 * Find out if current baud-rate can be achieved with new clock
441 * frequency.
442 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700443 if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700444 &bdiv, &cd, &div8)) {
445 dev_warn(port->dev, "clock rate change rejected\n");
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700446 return NOTIFY_BAD;
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700447 }
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700448
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700449 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700450
451 /* Disable the TX and RX to set baud rate */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100452 ctrl_reg = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700453 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
Thomas Betker19f22ef2015-03-12 22:11:59 +0100454 writel(ctrl_reg, port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700455
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700456 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700457
458 return NOTIFY_OK;
459 }
460 case POST_RATE_CHANGE:
461 /*
462 * Set clk dividers to generate correct baud with new clock
463 * frequency.
464 */
465
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700466 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700467
468 locked = 1;
469 port->uartclk = ndata->new_rate;
470
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700471 cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
472 cdns_uart->baud);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700473 /* fall through */
474 case ABORT_RATE_CHANGE:
475 if (!locked)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700476 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700477
478 /* Set TX/RX Reset */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100479 ctrl_reg = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700480 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
Thomas Betker19f22ef2015-03-12 22:11:59 +0100481 writel(ctrl_reg, port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700482
Thomas Betker19f22ef2015-03-12 22:11:59 +0100483 while (readl(port->membase + CDNS_UART_CR_OFFSET) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700484 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700485 cpu_relax();
486
487 /*
488 * Clear the RX disable and TX disable bits and then set the TX
489 * enable bit and RX enable bit to enable the transmitter and
490 * receiver.
491 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100492 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT_OFFSET);
493 ctrl_reg = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700494 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
495 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
Thomas Betker19f22ef2015-03-12 22:11:59 +0100496 writel(ctrl_reg, port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700497
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700498 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700499
500 return NOTIFY_OK;
501 default:
502 return NOTIFY_DONE;
503 }
504}
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700505#endif
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700506
John Linn61ec9012011-04-30 00:07:43 -0400507/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700508 * cdns_uart_start_tx - Start transmitting bytes
John Linn61ec9012011-04-30 00:07:43 -0400509 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700510 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700511static void cdns_uart_start_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400512{
513 unsigned int status, numbytes = port->fifosize;
514
515 if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
516 return;
517
Thomas Betker19f22ef2015-03-12 22:11:59 +0100518 status = readl(port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400519 /* Set the TX enable bit and clear the TX disable bit to enable the
520 * transmitter.
521 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100522 writel((status & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
523 port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400524
Thomas Betker19f22ef2015-03-12 22:11:59 +0100525 while (numbytes-- && ((readl(port->membase + CDNS_UART_SR_OFFSET) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700526 CDNS_UART_SR_TXFULL)) != CDNS_UART_SR_TXFULL) {
John Linn61ec9012011-04-30 00:07:43 -0400527 /* Break if no more data available in the UART buffer */
528 if (uart_circ_empty(&port->state->xmit))
529 break;
530
531 /* Get the data from the UART circular buffer and
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700532 * write it to the cdns_uart's TX_FIFO register.
John Linn61ec9012011-04-30 00:07:43 -0400533 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100534 writel(port->state->xmit.buf[port->state->xmit.tail],
535 port->membase + CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400536 port->icount.tx++;
537
538 /* Adjust the tail of the UART buffer and wrap
539 * the buffer if it reaches limit.
540 */
541 port->state->xmit.tail = (port->state->xmit.tail + 1) &
542 (UART_XMIT_SIZE - 1);
543 }
Thomas Betker19f22ef2015-03-12 22:11:59 +0100544 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400545 /* Enable the TX Empty interrupt */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100546 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IER_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400547
548 if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
549 uart_write_wakeup(port);
550}
551
552/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700553 * cdns_uart_stop_tx - Stop TX
John Linn61ec9012011-04-30 00:07:43 -0400554 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700555 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700556static void cdns_uart_stop_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400557{
558 unsigned int regval;
559
Thomas Betker19f22ef2015-03-12 22:11:59 +0100560 regval = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700561 regval |= CDNS_UART_CR_TX_DIS;
John Linn61ec9012011-04-30 00:07:43 -0400562 /* Disable the transmitter */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100563 writel(regval, port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400564}
565
566/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700567 * cdns_uart_stop_rx - Stop RX
John Linn61ec9012011-04-30 00:07:43 -0400568 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700569 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700570static void cdns_uart_stop_rx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400571{
572 unsigned int regval;
573
Thomas Betker19f22ef2015-03-12 22:11:59 +0100574 regval = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700575 regval |= CDNS_UART_CR_RX_DIS;
John Linn61ec9012011-04-30 00:07:43 -0400576 /* Disable the receiver */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100577 writel(regval, port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400578}
579
580/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700581 * cdns_uart_tx_empty - Check whether TX is empty
John Linn61ec9012011-04-30 00:07:43 -0400582 * @port: Handle to the uart port structure
583 *
Michal Simek489810a12014-04-04 17:23:37 -0700584 * Return: TIOCSER_TEMT on success, 0 otherwise
585 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700586static unsigned int cdns_uart_tx_empty(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400587{
588 unsigned int status;
589
Thomas Betker19f22ef2015-03-12 22:11:59 +0100590 status = readl(port->membase + CDNS_UART_SR_OFFSET) &
591 CDNS_UART_SR_TXEMPTY;
John Linn61ec9012011-04-30 00:07:43 -0400592 return status ? TIOCSER_TEMT : 0;
593}
594
595/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700596 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
John Linn61ec9012011-04-30 00:07:43 -0400597 * transmitting char breaks
598 * @port: Handle to the uart port structure
599 * @ctl: Value based on which start or stop decision is taken
Michal Simek489810a12014-04-04 17:23:37 -0700600 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700601static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
John Linn61ec9012011-04-30 00:07:43 -0400602{
603 unsigned int status;
604 unsigned long flags;
605
606 spin_lock_irqsave(&port->lock, flags);
607
Thomas Betker19f22ef2015-03-12 22:11:59 +0100608 status = readl(port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400609
610 if (ctl == -1)
Thomas Betker19f22ef2015-03-12 22:11:59 +0100611 writel(CDNS_UART_CR_STARTBRK | status,
612 port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400613 else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700614 if ((status & CDNS_UART_CR_STOPBRK) == 0)
Thomas Betker19f22ef2015-03-12 22:11:59 +0100615 writel(CDNS_UART_CR_STOPBRK | status,
616 port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400617 }
618 spin_unlock_irqrestore(&port->lock, flags);
619}
620
621/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700622 * cdns_uart_set_termios - termios operations, handling data length, parity,
John Linn61ec9012011-04-30 00:07:43 -0400623 * stop bits, flow control, baud rate
624 * @port: Handle to the uart port structure
625 * @termios: Handle to the input termios structure
626 * @old: Values of the previously saved termios structure
Michal Simek489810a12014-04-04 17:23:37 -0700627 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700628static void cdns_uart_set_termios(struct uart_port *port,
John Linn61ec9012011-04-30 00:07:43 -0400629 struct ktermios *termios, struct ktermios *old)
630{
631 unsigned int cval = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700632 unsigned int baud, minbaud, maxbaud;
John Linn61ec9012011-04-30 00:07:43 -0400633 unsigned long flags;
634 unsigned int ctrl_reg, mode_reg;
635
636 spin_lock_irqsave(&port->lock, flags);
637
Nathan Rossi6ecde472015-01-16 13:49:25 +0100638 /* Wait for the transmit FIFO to empty before making changes */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100639 if (!(readl(port->membase + CDNS_UART_CR_OFFSET) &
640 CDNS_UART_CR_TX_DIS)) {
641 while (!(readl(port->membase + CDNS_UART_SR_OFFSET) &
Nathan Rossi6ecde472015-01-16 13:49:25 +0100642 CDNS_UART_SR_TXEMPTY)) {
643 cpu_relax();
644 }
John Linn61ec9012011-04-30 00:07:43 -0400645 }
646
647 /* Disable the TX and RX to set baud rate */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100648 ctrl_reg = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700649 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
Thomas Betker19f22ef2015-03-12 22:11:59 +0100650 writel(ctrl_reg, port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400651
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700652 /*
653 * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
654 * min and max baud should be calculated here based on port->uartclk.
655 * this way we get a valid baud and can safely call set_baud()
656 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700657 minbaud = port->uartclk /
658 ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
659 maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700660 baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700661 baud = cdns_uart_set_baud_rate(port, baud);
John Linn61ec9012011-04-30 00:07:43 -0400662 if (tty_termios_baud_rate(termios))
663 tty_termios_encode_baud_rate(termios, baud, baud);
664
Soren Brinkmanne555a212014-04-04 17:23:39 -0700665 /* Update the per-port timeout. */
John Linn61ec9012011-04-30 00:07:43 -0400666 uart_update_timeout(port, termios->c_cflag, baud);
667
668 /* Set TX/RX Reset */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100669 ctrl_reg = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700670 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
Thomas Betker19f22ef2015-03-12 22:11:59 +0100671 writel(ctrl_reg, port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400672
Soren Brinkmanne555a212014-04-04 17:23:39 -0700673 /*
674 * Clear the RX disable and TX disable bits and then set the TX enable
John Linn61ec9012011-04-30 00:07:43 -0400675 * bit and RX enable bit to enable the transmitter and receiver.
676 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100677 ctrl_reg = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700678 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
679 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
Thomas Betker19f22ef2015-03-12 22:11:59 +0100680 writel(ctrl_reg, port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400681
Thomas Betker19f22ef2015-03-12 22:11:59 +0100682 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400683
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700684 port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
685 CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
John Linn61ec9012011-04-30 00:07:43 -0400686 port->ignore_status_mask = 0;
687
688 if (termios->c_iflag & INPCK)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700689 port->read_status_mask |= CDNS_UART_IXR_PARITY |
690 CDNS_UART_IXR_FRAMING;
John Linn61ec9012011-04-30 00:07:43 -0400691
692 if (termios->c_iflag & IGNPAR)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700693 port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
694 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400695
696 /* ignore all characters if CREAD is not set */
697 if ((termios->c_cflag & CREAD) == 0)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700698 port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
699 CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
700 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400701
Thomas Betker19f22ef2015-03-12 22:11:59 +0100702 mode_reg = readl(port->membase + CDNS_UART_MR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400703
704 /* Handling Data Size */
705 switch (termios->c_cflag & CSIZE) {
706 case CS6:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700707 cval |= CDNS_UART_MR_CHARLEN_6_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400708 break;
709 case CS7:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700710 cval |= CDNS_UART_MR_CHARLEN_7_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400711 break;
712 default:
713 case CS8:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700714 cval |= CDNS_UART_MR_CHARLEN_8_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400715 termios->c_cflag &= ~CSIZE;
716 termios->c_cflag |= CS8;
717 break;
718 }
719
720 /* Handling Parity and Stop Bits length */
721 if (termios->c_cflag & CSTOPB)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700722 cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
John Linn61ec9012011-04-30 00:07:43 -0400723 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700724 cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
John Linn61ec9012011-04-30 00:07:43 -0400725
726 if (termios->c_cflag & PARENB) {
727 /* Mark or Space parity */
728 if (termios->c_cflag & CMSPAR) {
729 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700730 cval |= CDNS_UART_MR_PARITY_MARK;
John Linn61ec9012011-04-30 00:07:43 -0400731 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700732 cval |= CDNS_UART_MR_PARITY_SPACE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700733 } else {
734 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700735 cval |= CDNS_UART_MR_PARITY_ODD;
John Linn61ec9012011-04-30 00:07:43 -0400736 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700737 cval |= CDNS_UART_MR_PARITY_EVEN;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700738 }
739 } else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700740 cval |= CDNS_UART_MR_PARITY_NONE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700741 }
742 cval |= mode_reg & 1;
Thomas Betker19f22ef2015-03-12 22:11:59 +0100743 writel(cval, port->membase + CDNS_UART_MR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400744
745 spin_unlock_irqrestore(&port->lock, flags);
746}
747
748/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700749 * cdns_uart_startup - Called when an application opens a cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400750 * @port: Handle to the uart port structure
751 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700752 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -0700753 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700754static int cdns_uart_startup(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400755{
756 unsigned int retval = 0, status = 0;
757
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700758 retval = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME,
John Linn61ec9012011-04-30 00:07:43 -0400759 (void *)port);
760 if (retval)
761 return retval;
762
763 /* Disable the TX and RX */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100764 writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
765 port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400766
767 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
768 * no break chars.
769 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100770 writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
771 port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400772
Thomas Betker19f22ef2015-03-12 22:11:59 +0100773 status = readl(port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400774
775 /* Clear the RX disable and TX disable bits and then set the TX enable
776 * bit and RX enable bit to enable the transmitter and receiver.
777 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100778 writel((status & ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS))
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700779 | (CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN |
Thomas Betker19f22ef2015-03-12 22:11:59 +0100780 CDNS_UART_CR_STOPBRK),
781 port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400782
783 /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
784 * no parity.
785 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100786 writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700787 | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
Thomas Betker19f22ef2015-03-12 22:11:59 +0100788 port->membase + CDNS_UART_MR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400789
Suneel85baf542013-10-17 14:08:08 -0700790 /*
791 * Set the RX FIFO Trigger level to use most of the FIFO, but it
792 * can be tuned with a module parameter
793 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100794 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400795
Suneel85baf542013-10-17 14:08:08 -0700796 /*
797 * Receive Timeout register is enabled but it
798 * can be tuned with a module parameter
799 */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100800 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400801
John Linn855f6fd2013-03-22 18:49:27 +0100802 /* Clear out any pending interrupts before enabling them */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100803 writel(readl(port->membase + CDNS_UART_ISR_OFFSET),
804 port->membase + CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400805
806 /* Set the Interrupt Registers with desired interrupts */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100807 writel(CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_PARITY |
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700808 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN |
809 CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT,
Thomas Betker19f22ef2015-03-12 22:11:59 +0100810 port->membase + CDNS_UART_IER_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400811
812 return retval;
813}
814
815/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700816 * cdns_uart_shutdown - Called when an application closes a cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400817 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700818 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700819static void cdns_uart_shutdown(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400820{
821 int status;
822
823 /* Disable interrupts */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100824 status = readl(port->membase + CDNS_UART_IMR_OFFSET);
825 writel(status, port->membase + CDNS_UART_IDR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400826
827 /* Disable the TX and RX */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100828 writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
829 port->membase + CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400830 free_irq(port->irq, port);
831}
832
833/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700834 * cdns_uart_type - Set UART type to cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400835 * @port: Handle to the uart port structure
836 *
Michal Simek489810a12014-04-04 17:23:37 -0700837 * Return: string on success, NULL otherwise
838 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700839static const char *cdns_uart_type(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400840{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700841 return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
John Linn61ec9012011-04-30 00:07:43 -0400842}
843
844/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700845 * cdns_uart_verify_port - Verify the port params
John Linn61ec9012011-04-30 00:07:43 -0400846 * @port: Handle to the uart port structure
847 * @ser: Handle to the structure whose members are compared
848 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700849 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -0700850 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700851static int cdns_uart_verify_port(struct uart_port *port,
John Linn61ec9012011-04-30 00:07:43 -0400852 struct serial_struct *ser)
853{
854 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
855 return -EINVAL;
856 if (port->irq != ser->irq)
857 return -EINVAL;
858 if (ser->io_type != UPIO_MEM)
859 return -EINVAL;
860 if (port->iobase != ser->port)
861 return -EINVAL;
862 if (ser->hub6 != 0)
863 return -EINVAL;
864 return 0;
865}
866
867/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700868 * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
869 * called when the driver adds a cdns_uart port via
John Linn61ec9012011-04-30 00:07:43 -0400870 * uart_add_one_port()
871 * @port: Handle to the uart port structure
872 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700873 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -0700874 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700875static int cdns_uart_request_port(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400876{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700877 if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
878 CDNS_UART_NAME)) {
John Linn61ec9012011-04-30 00:07:43 -0400879 return -ENOMEM;
880 }
881
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700882 port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400883 if (!port->membase) {
884 dev_err(port->dev, "Unable to map registers\n");
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700885 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400886 return -ENOMEM;
887 }
888 return 0;
889}
890
891/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700892 * cdns_uart_release_port - Release UART port
John Linn61ec9012011-04-30 00:07:43 -0400893 * @port: Handle to the uart port structure
Soren Brinkmanne555a212014-04-04 17:23:39 -0700894 *
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700895 * Release the memory region attached to a cdns_uart port. Called when the
896 * driver removes a cdns_uart port via uart_remove_one_port().
Michal Simek489810a12014-04-04 17:23:37 -0700897 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700898static void cdns_uart_release_port(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400899{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700900 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400901 iounmap(port->membase);
902 port->membase = NULL;
903}
904
905/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700906 * cdns_uart_config_port - Configure UART port
John Linn61ec9012011-04-30 00:07:43 -0400907 * @port: Handle to the uart port structure
908 * @flags: If any
Michal Simek489810a12014-04-04 17:23:37 -0700909 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700910static void cdns_uart_config_port(struct uart_port *port, int flags)
John Linn61ec9012011-04-30 00:07:43 -0400911{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700912 if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
John Linn61ec9012011-04-30 00:07:43 -0400913 port->type = PORT_XUARTPS;
914}
915
916/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700917 * cdns_uart_get_mctrl - Get the modem control state
John Linn61ec9012011-04-30 00:07:43 -0400918 * @port: Handle to the uart port structure
919 *
Michal Simek489810a12014-04-04 17:23:37 -0700920 * Return: the modem control state
921 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700922static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400923{
924 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
925}
926
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700927static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
John Linn61ec9012011-04-30 00:07:43 -0400928{
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +0100929 u32 val;
930
Thomas Betker19f22ef2015-03-12 22:11:59 +0100931 val = readl(port->membase + CDNS_UART_MODEMCR_OFFSET);
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +0100932
933 val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
934
935 if (mctrl & TIOCM_RTS)
936 val |= CDNS_UART_MODEMCR_RTS;
937 if (mctrl & TIOCM_DTR)
938 val |= CDNS_UART_MODEMCR_DTR;
939
Thomas Betker19f22ef2015-03-12 22:11:59 +0100940 writel(val, port->membase + CDNS_UART_MODEMCR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400941}
942
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700943#ifdef CONFIG_CONSOLE_POLL
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700944static int cdns_uart_poll_get_char(struct uart_port *port)
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700945{
946 u32 imr;
947 int c;
948
949 /* Disable all interrupts */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100950 imr = readl(port->membase + CDNS_UART_IMR_OFFSET);
951 writel(imr, port->membase + CDNS_UART_IDR_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700952
953 /* Check if FIFO is empty */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100954 if (readl(port->membase + CDNS_UART_SR_OFFSET) & CDNS_UART_SR_RXEMPTY)
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700955 c = NO_POLL_CHAR;
956 else /* Read a character */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100957 c = (unsigned char) readl(
958 port->membase + CDNS_UART_FIFO_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700959
960 /* Enable interrupts */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100961 writel(imr, port->membase + CDNS_UART_IER_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700962
963 return c;
964}
965
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700966static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700967{
968 u32 imr;
969
970 /* Disable all interrupts */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100971 imr = readl(port->membase + CDNS_UART_IMR_OFFSET);
972 writel(imr, port->membase + CDNS_UART_IDR_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700973
974 /* Wait until FIFO is empty */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100975 while (!(readl(port->membase + CDNS_UART_SR_OFFSET) &
976 CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700977 cpu_relax();
978
979 /* Write a character */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100980 writel(c, port->membase + CDNS_UART_FIFO_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700981
982 /* Wait until FIFO is empty */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100983 while (!(readl(port->membase + CDNS_UART_SR_OFFSET) &
984 CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700985 cpu_relax();
986
987 /* Enable interrupts */
Thomas Betker19f22ef2015-03-12 22:11:59 +0100988 writel(imr, port->membase + CDNS_UART_IER_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700989
990 return;
991}
992#endif
993
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700994static struct uart_ops cdns_uart_ops = {
995 .set_mctrl = cdns_uart_set_mctrl,
996 .get_mctrl = cdns_uart_get_mctrl,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700997 .start_tx = cdns_uart_start_tx,
998 .stop_tx = cdns_uart_stop_tx,
999 .stop_rx = cdns_uart_stop_rx,
1000 .tx_empty = cdns_uart_tx_empty,
1001 .break_ctl = cdns_uart_break_ctl,
1002 .set_termios = cdns_uart_set_termios,
1003 .startup = cdns_uart_startup,
1004 .shutdown = cdns_uart_shutdown,
1005 .type = cdns_uart_type,
1006 .verify_port = cdns_uart_verify_port,
1007 .request_port = cdns_uart_request_port,
1008 .release_port = cdns_uart_release_port,
1009 .config_port = cdns_uart_config_port,
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001010#ifdef CONFIG_CONSOLE_POLL
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001011 .poll_get_char = cdns_uart_poll_get_char,
1012 .poll_put_char = cdns_uart_poll_put_char,
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001013#endif
John Linn61ec9012011-04-30 00:07:43 -04001014};
1015
Thomas Betker6db6df02015-03-11 22:39:26 +01001016static struct uart_port cdns_uart_port[CDNS_UART_NR_PORTS];
John Linn61ec9012011-04-30 00:07:43 -04001017
1018/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001019 * cdns_uart_get_port - Configure the port from platform device resource info
Michal Simek928e9262014-04-04 17:23:38 -07001020 * @id: Port id
1021 *
Michal Simek489810a12014-04-04 17:23:37 -07001022 * Return: a pointer to a uart_port or NULL for failure
1023 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001024static struct uart_port *cdns_uart_get_port(int id)
John Linn61ec9012011-04-30 00:07:43 -04001025{
1026 struct uart_port *port;
John Linn61ec9012011-04-30 00:07:43 -04001027
Michal Simek928e9262014-04-04 17:23:38 -07001028 /* Try the given port id if failed use default method */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001029 if (cdns_uart_port[id].mapbase != 0) {
Michal Simek928e9262014-04-04 17:23:38 -07001030 /* Find the next unused port */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001031 for (id = 0; id < CDNS_UART_NR_PORTS; id++)
1032 if (cdns_uart_port[id].mapbase == 0)
Michal Simek928e9262014-04-04 17:23:38 -07001033 break;
1034 }
John Linn61ec9012011-04-30 00:07:43 -04001035
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001036 if (id >= CDNS_UART_NR_PORTS)
John Linn61ec9012011-04-30 00:07:43 -04001037 return NULL;
1038
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001039 port = &cdns_uart_port[id];
John Linn61ec9012011-04-30 00:07:43 -04001040
1041 /* At this point, we've got an empty uart_port struct, initialize it */
1042 spin_lock_init(&port->lock);
1043 port->membase = NULL;
John Linn61ec9012011-04-30 00:07:43 -04001044 port->irq = 0;
1045 port->type = PORT_UNKNOWN;
1046 port->iotype = UPIO_MEM32;
1047 port->flags = UPF_BOOT_AUTOCONF;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001048 port->ops = &cdns_uart_ops;
1049 port->fifosize = CDNS_UART_FIFO_SIZE;
John Linn61ec9012011-04-30 00:07:43 -04001050 port->line = id;
1051 port->dev = NULL;
1052 return port;
1053}
1054
John Linn61ec9012011-04-30 00:07:43 -04001055#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1056/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001057 * cdns_uart_console_wait_tx - Wait for the TX to be full
John Linn61ec9012011-04-30 00:07:43 -04001058 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -07001059 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001060static void cdns_uart_console_wait_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -04001061{
Thomas Betker19f22ef2015-03-12 22:11:59 +01001062 while (!(readl(port->membase + CDNS_UART_SR_OFFSET) &
1063 CDNS_UART_SR_TXEMPTY))
John Linn61ec9012011-04-30 00:07:43 -04001064 barrier();
1065}
1066
1067/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001068 * cdns_uart_console_putchar - write the character to the FIFO buffer
John Linn61ec9012011-04-30 00:07:43 -04001069 * @port: Handle to the uart port structure
1070 * @ch: Character to be written
Michal Simek489810a12014-04-04 17:23:37 -07001071 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001072static void cdns_uart_console_putchar(struct uart_port *port, int ch)
John Linn61ec9012011-04-30 00:07:43 -04001073{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001074 cdns_uart_console_wait_tx(port);
Thomas Betker19f22ef2015-03-12 22:11:59 +01001075 writel(ch, port->membase + CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -04001076}
1077
Michal Simek6fa62fc2014-09-10 12:43:02 +02001078static void cdns_early_write(struct console *con, const char *s, unsigned n)
1079{
1080 struct earlycon_device *dev = con->data;
1081
1082 uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
1083}
1084
1085static int __init cdns_early_console_setup(struct earlycon_device *device,
1086 const char *opt)
1087{
1088 if (!device->port.membase)
1089 return -ENODEV;
1090
1091 device->con->write = cdns_early_write;
1092
1093 return 0;
1094}
1095EARLYCON_DECLARE(cdns, cdns_early_console_setup);
1096
John Linn61ec9012011-04-30 00:07:43 -04001097/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001098 * cdns_uart_console_write - perform write operation
Michal Simek489810a12014-04-04 17:23:37 -07001099 * @co: Console handle
John Linn61ec9012011-04-30 00:07:43 -04001100 * @s: Pointer to character array
1101 * @count: No of characters
Michal Simek489810a12014-04-04 17:23:37 -07001102 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001103static void cdns_uart_console_write(struct console *co, const char *s,
John Linn61ec9012011-04-30 00:07:43 -04001104 unsigned int count)
1105{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001106 struct uart_port *port = &cdns_uart_port[co->index];
John Linn61ec9012011-04-30 00:07:43 -04001107 unsigned long flags;
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001108 unsigned int imr, ctrl;
John Linn61ec9012011-04-30 00:07:43 -04001109 int locked = 1;
1110
1111 if (oops_in_progress)
1112 locked = spin_trylock_irqsave(&port->lock, flags);
1113 else
1114 spin_lock_irqsave(&port->lock, flags);
1115
1116 /* save and disable interrupt */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001117 imr = readl(port->membase + CDNS_UART_IMR_OFFSET);
1118 writel(imr, port->membase + CDNS_UART_IDR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -04001119
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001120 /*
1121 * Make sure that the tx part is enabled. Set the TX enable bit and
1122 * clear the TX disable bit to enable the transmitter.
1123 */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001124 ctrl = readl(port->membase + CDNS_UART_CR_OFFSET);
1125 writel((ctrl & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
1126 port->membase + CDNS_UART_CR_OFFSET);
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001127
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001128 uart_console_write(port, s, count, cdns_uart_console_putchar);
1129 cdns_uart_console_wait_tx(port);
John Linn61ec9012011-04-30 00:07:43 -04001130
Thomas Betker19f22ef2015-03-12 22:11:59 +01001131 writel(ctrl, port->membase + CDNS_UART_CR_OFFSET);
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001132
Soren Brinkmannb494a5f2014-04-04 17:23:42 -07001133 /* restore interrupt state */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001134 writel(imr, port->membase + CDNS_UART_IER_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -04001135
1136 if (locked)
1137 spin_unlock_irqrestore(&port->lock, flags);
1138}
1139
1140/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001141 * cdns_uart_console_setup - Initialize the uart to default config
John Linn61ec9012011-04-30 00:07:43 -04001142 * @co: Console handle
1143 * @options: Initial settings of uart
1144 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001145 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -07001146 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001147static int __init cdns_uart_console_setup(struct console *co, char *options)
John Linn61ec9012011-04-30 00:07:43 -04001148{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001149 struct uart_port *port = &cdns_uart_port[co->index];
John Linn61ec9012011-04-30 00:07:43 -04001150 int baud = 9600;
1151 int bits = 8;
1152 int parity = 'n';
1153 int flow = 'n';
1154
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001155 if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
John Linn61ec9012011-04-30 00:07:43 -04001156 return -EINVAL;
1157
Thomas Betker136debf2015-03-11 22:39:28 +01001158 if (!port->membase) {
Peter Crosthwaitef6415492015-02-24 15:13:57 -08001159 pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
1160 co->index);
John Linn61ec9012011-04-30 00:07:43 -04001161 return -ENODEV;
1162 }
1163
1164 if (options)
1165 uart_parse_options(options, &baud, &parity, &bits, &flow);
1166
1167 return uart_set_options(port, co, baud, parity, bits, flow);
1168}
1169
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001170static struct uart_driver cdns_uart_uart_driver;
John Linn61ec9012011-04-30 00:07:43 -04001171
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001172static struct console cdns_uart_console = {
1173 .name = CDNS_UART_TTY_NAME,
1174 .write = cdns_uart_console_write,
John Linn61ec9012011-04-30 00:07:43 -04001175 .device = uart_console_device,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001176 .setup = cdns_uart_console_setup,
John Linn61ec9012011-04-30 00:07:43 -04001177 .flags = CON_PRINTBUFFER,
1178 .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001179 .data = &cdns_uart_uart_driver,
John Linn61ec9012011-04-30 00:07:43 -04001180};
1181
1182/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001183 * cdns_uart_console_init - Initialization call
John Linn61ec9012011-04-30 00:07:43 -04001184 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001185 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001186 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001187static int __init cdns_uart_console_init(void)
John Linn61ec9012011-04-30 00:07:43 -04001188{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001189 register_console(&cdns_uart_console);
John Linn61ec9012011-04-30 00:07:43 -04001190 return 0;
1191}
1192
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001193console_initcall(cdns_uart_console_init);
John Linn61ec9012011-04-30 00:07:43 -04001194
1195#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
1196
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001197static struct uart_driver cdns_uart_uart_driver = {
Soren Brinkmanne555a212014-04-04 17:23:39 -07001198 .owner = THIS_MODULE,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001199 .driver_name = CDNS_UART_NAME,
1200 .dev_name = CDNS_UART_TTY_NAME,
1201 .major = CDNS_UART_MAJOR,
1202 .minor = CDNS_UART_MINOR,
1203 .nr = CDNS_UART_NR_PORTS,
Soren Brinkmannd3641f62013-10-21 16:41:00 -07001204#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001205 .cons = &cdns_uart_console,
Soren Brinkmannd3641f62013-10-21 16:41:00 -07001206#endif
1207};
1208
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001209#ifdef CONFIG_PM_SLEEP
1210/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001211 * cdns_uart_suspend - suspend event
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001212 * @device: Pointer to the device structure
1213 *
Michal Simek489810a12014-04-04 17:23:37 -07001214 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001215 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001216static int cdns_uart_suspend(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001217{
1218 struct uart_port *port = dev_get_drvdata(device);
1219 struct tty_struct *tty;
1220 struct device *tty_dev;
1221 int may_wake = 0;
1222
1223 /* Get the tty which could be NULL so don't assume it's valid */
1224 tty = tty_port_tty_get(&port->state->port);
1225 if (tty) {
1226 tty_dev = tty->dev;
1227 may_wake = device_may_wakeup(tty_dev);
1228 tty_kref_put(tty);
1229 }
1230
1231 /*
1232 * Call the API provided in serial_core.c file which handles
1233 * the suspend.
1234 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001235 uart_suspend_port(&cdns_uart_uart_driver, port);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001236 if (console_suspend_enabled && !may_wake) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001237 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001238
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001239 clk_disable(cdns_uart->uartclk);
1240 clk_disable(cdns_uart->pclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001241 } else {
1242 unsigned long flags = 0;
1243
1244 spin_lock_irqsave(&port->lock, flags);
1245 /* Empty the receive FIFO 1st before making changes */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001246 while (!(readl(port->membase + CDNS_UART_SR_OFFSET) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001247 CDNS_UART_SR_RXEMPTY))
Thomas Betker19f22ef2015-03-12 22:11:59 +01001248 readl(port->membase + CDNS_UART_FIFO_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001249 /* set RX trigger level to 1 */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001250 writel(1, port->membase + CDNS_UART_RXWM_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001251 /* disable RX timeout interrups */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001252 writel(CDNS_UART_IXR_TOUT,
1253 port->membase + CDNS_UART_IDR_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001254 spin_unlock_irqrestore(&port->lock, flags);
1255 }
1256
1257 return 0;
1258}
1259
1260/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001261 * cdns_uart_resume - Resume after a previous suspend
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001262 * @device: Pointer to the device structure
1263 *
Michal Simek489810a12014-04-04 17:23:37 -07001264 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001265 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001266static int cdns_uart_resume(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001267{
1268 struct uart_port *port = dev_get_drvdata(device);
1269 unsigned long flags = 0;
1270 u32 ctrl_reg;
1271 struct tty_struct *tty;
1272 struct device *tty_dev;
1273 int may_wake = 0;
1274
1275 /* Get the tty which could be NULL so don't assume it's valid */
1276 tty = tty_port_tty_get(&port->state->port);
1277 if (tty) {
1278 tty_dev = tty->dev;
1279 may_wake = device_may_wakeup(tty_dev);
1280 tty_kref_put(tty);
1281 }
1282
1283 if (console_suspend_enabled && !may_wake) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001284 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001285
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001286 clk_enable(cdns_uart->pclk);
1287 clk_enable(cdns_uart->uartclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001288
1289 spin_lock_irqsave(&port->lock, flags);
1290
1291 /* Set TX/RX Reset */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001292 ctrl_reg = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001293 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
Thomas Betker19f22ef2015-03-12 22:11:59 +01001294 writel(ctrl_reg, port->membase + CDNS_UART_CR_OFFSET);
1295 while (readl(port->membase + CDNS_UART_CR_OFFSET) &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001296 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001297 cpu_relax();
1298
1299 /* restore rx timeout value */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001300 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001301 /* Enable Tx/Rx */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001302 ctrl_reg = readl(port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001303 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1304 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
Thomas Betker19f22ef2015-03-12 22:11:59 +01001305 writel(ctrl_reg, port->membase + CDNS_UART_CR_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001306
1307 spin_unlock_irqrestore(&port->lock, flags);
1308 } else {
1309 spin_lock_irqsave(&port->lock, flags);
1310 /* restore original rx trigger level */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001311 writel(rx_trigger_level,
1312 port->membase + CDNS_UART_RXWM_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001313 /* enable RX timeout interrupt */
Thomas Betker19f22ef2015-03-12 22:11:59 +01001314 writel(CDNS_UART_IXR_TOUT,
1315 port->membase + CDNS_UART_IER_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001316 spin_unlock_irqrestore(&port->lock, flags);
1317 }
1318
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001319 return uart_resume_port(&cdns_uart_uart_driver, port);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001320}
1321#endif /* ! CONFIG_PM_SLEEP */
1322
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001323static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
1324 cdns_uart_resume);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001325
John Linn61ec9012011-04-30 00:07:43 -04001326/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001327 * cdns_uart_probe - Platform driver probe
John Linn61ec9012011-04-30 00:07:43 -04001328 * @pdev: Pointer to the platform device structure
1329 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001330 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001331 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001332static int cdns_uart_probe(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -04001333{
Michal Simek928e9262014-04-04 17:23:38 -07001334 int rc, id;
John Linn61ec9012011-04-30 00:07:43 -04001335 struct uart_port *port;
1336 struct resource *res, *res2;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001337 struct cdns_uart *cdns_uart_data;
John Linn61ec9012011-04-30 00:07:43 -04001338
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001339 cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
Soren Brinkmannc03cae12013-10-17 14:08:05 -07001340 GFP_KERNEL);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001341 if (!cdns_uart_data)
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001342 return -ENOMEM;
1343
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001344 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
1345 if (IS_ERR(cdns_uart_data->pclk)) {
1346 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1347 if (!IS_ERR(cdns_uart_data->pclk))
1348 dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001349 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001350 if (IS_ERR(cdns_uart_data->pclk)) {
1351 dev_err(&pdev->dev, "pclk clock not found.\n");
1352 return PTR_ERR(cdns_uart_data->pclk);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001353 }
1354
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001355 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1356 if (IS_ERR(cdns_uart_data->uartclk)) {
1357 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1358 if (!IS_ERR(cdns_uart_data->uartclk))
1359 dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1360 }
1361 if (IS_ERR(cdns_uart_data->uartclk)) {
1362 dev_err(&pdev->dev, "uart_clk clock not found.\n");
1363 return PTR_ERR(cdns_uart_data->uartclk);
1364 }
1365
1366 rc = clk_prepare_enable(cdns_uart_data->pclk);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001367 if (rc) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001368 dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
Soren Brinkmannc03cae12013-10-17 14:08:05 -07001369 return rc;
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001370 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001371 rc = clk_prepare_enable(cdns_uart_data->uartclk);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001372 if (rc) {
1373 dev_err(&pdev->dev, "Unable to enable device clock.\n");
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001374 goto err_out_clk_dis_pclk;
John Linn61ec9012011-04-30 00:07:43 -04001375 }
1376
1377 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001378 if (!res) {
1379 rc = -ENODEV;
1380 goto err_out_clk_disable;
1381 }
John Linn61ec9012011-04-30 00:07:43 -04001382
1383 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001384 if (!res2) {
1385 rc = -ENODEV;
1386 goto err_out_clk_disable;
1387 }
John Linn61ec9012011-04-30 00:07:43 -04001388
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001389#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001390 cdns_uart_data->clk_rate_change_nb.notifier_call =
1391 cdns_uart_clk_notifier_cb;
1392 if (clk_notifier_register(cdns_uart_data->uartclk,
1393 &cdns_uart_data->clk_rate_change_nb))
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001394 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001395#endif
Michal Simek928e9262014-04-04 17:23:38 -07001396 /* Look for a serialN alias */
1397 id = of_alias_get_id(pdev->dev.of_node, "serial");
1398 if (id < 0)
1399 id = 0;
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001400
John Linn61ec9012011-04-30 00:07:43 -04001401 /* Initialize the port structure */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001402 port = cdns_uart_get_port(id);
John Linn61ec9012011-04-30 00:07:43 -04001403
1404 if (!port) {
1405 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001406 rc = -ENODEV;
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001407 goto err_out_notif_unreg;
John Linn61ec9012011-04-30 00:07:43 -04001408 } else {
1409 /* Register the port.
1410 * This function also registers this device with the tty layer
1411 * and triggers invocation of the config_port() entry point.
1412 */
1413 port->mapbase = res->start;
1414 port->irq = res2->start;
1415 port->dev = &pdev->dev;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001416 port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1417 port->private_data = cdns_uart_data;
1418 cdns_uart_data->port = port;
Jingoo Han696faed2013-05-23 19:39:36 +09001419 platform_set_drvdata(pdev, port);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001420 rc = uart_add_one_port(&cdns_uart_uart_driver, port);
John Linn61ec9012011-04-30 00:07:43 -04001421 if (rc) {
1422 dev_err(&pdev->dev,
1423 "uart_add_one_port() failed; err=%i\n", rc);
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001424 goto err_out_notif_unreg;
John Linn61ec9012011-04-30 00:07:43 -04001425 }
1426 return 0;
1427 }
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001428
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001429err_out_notif_unreg:
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001430#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001431 clk_notifier_unregister(cdns_uart_data->uartclk,
1432 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001433#endif
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001434err_out_clk_disable:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001435 clk_disable_unprepare(cdns_uart_data->uartclk);
1436err_out_clk_dis_pclk:
1437 clk_disable_unprepare(cdns_uart_data->pclk);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001438
1439 return rc;
John Linn61ec9012011-04-30 00:07:43 -04001440}
1441
1442/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001443 * cdns_uart_remove - called when the platform driver is unregistered
John Linn61ec9012011-04-30 00:07:43 -04001444 * @pdev: Pointer to the platform device structure
1445 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001446 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001447 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001448static int cdns_uart_remove(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -04001449{
Jingoo Han696faed2013-05-23 19:39:36 +09001450 struct uart_port *port = platform_get_drvdata(pdev);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001451 struct cdns_uart *cdns_uart_data = port->private_data;
Josh Cartwright2326669c2013-01-21 19:57:41 +01001452 int rc;
John Linn61ec9012011-04-30 00:07:43 -04001453
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001454 /* Remove the cdns_uart port from the serial core */
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001455#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001456 clk_notifier_unregister(cdns_uart_data->uartclk,
1457 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001458#endif
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001459 rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001460 port->mapbase = 0;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001461 clk_disable_unprepare(cdns_uart_data->uartclk);
1462 clk_disable_unprepare(cdns_uart_data->pclk);
John Linn61ec9012011-04-30 00:07:43 -04001463 return rc;
1464}
1465
John Linn61ec9012011-04-30 00:07:43 -04001466/* Match table for of_platform binding */
Fabian Fredericked0bb232015-03-16 20:17:11 +01001467static const struct of_device_id cdns_uart_of_match[] = {
John Linn61ec9012011-04-30 00:07:43 -04001468 { .compatible = "xlnx,xuartps", },
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001469 { .compatible = "cdns,uart-r1p8", },
John Linn61ec9012011-04-30 00:07:43 -04001470 {}
1471};
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001472MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
John Linn61ec9012011-04-30 00:07:43 -04001473
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001474static struct platform_driver cdns_uart_platform_driver = {
1475 .probe = cdns_uart_probe,
1476 .remove = cdns_uart_remove,
John Linn61ec9012011-04-30 00:07:43 -04001477 .driver = {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001478 .name = CDNS_UART_NAME,
1479 .of_match_table = cdns_uart_of_match,
1480 .pm = &cdns_uart_dev_pm_ops,
John Linn61ec9012011-04-30 00:07:43 -04001481 },
1482};
1483
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001484static int __init cdns_uart_init(void)
John Linn61ec9012011-04-30 00:07:43 -04001485{
1486 int retval = 0;
1487
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001488 /* Register the cdns_uart driver with the serial core */
1489 retval = uart_register_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001490 if (retval)
1491 return retval;
1492
1493 /* Register the platform driver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001494 retval = platform_driver_register(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001495 if (retval)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001496 uart_unregister_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001497
1498 return retval;
1499}
1500
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001501static void __exit cdns_uart_exit(void)
John Linn61ec9012011-04-30 00:07:43 -04001502{
John Linn61ec9012011-04-30 00:07:43 -04001503 /* Unregister the platform driver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001504 platform_driver_unregister(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001505
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001506 /* Unregister the cdns_uart driver */
1507 uart_unregister_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001508}
1509
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001510module_init(cdns_uart_init);
1511module_exit(cdns_uart_exit);
John Linn61ec9012011-04-30 00:07:43 -04001512
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001513MODULE_DESCRIPTION("Driver for Cadence UART");
John Linn61ec9012011-04-30 00:07:43 -04001514MODULE_AUTHOR("Xilinx Inc.");
1515MODULE_LICENSE("GPL");