blob: b8fbd4767c17fac51cd4c18fb341c24a0464fdc3 [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
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070042#define cdns_uart_readl(offset) ioread32(port->membase + offset)
43#define cdns_uart_writel(val, offset) iowrite32(val, port->membase + offset)
John Linn61ec9012011-04-30 00:07:43 -040044
Suneel85baf542013-10-17 14:08:08 -070045/* Rx Trigger level */
46static int rx_trigger_level = 56;
47module_param(rx_trigger_level, uint, S_IRUGO);
48MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
49
50/* Rx Timeout */
51static int rx_timeout = 10;
52module_param(rx_timeout, uint, S_IRUGO);
53MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
54
Soren Brinkmanne555a212014-04-04 17:23:39 -070055/* Register offsets for the UART. */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070056#define CDNS_UART_CR_OFFSET 0x00 /* Control Register */
57#define CDNS_UART_MR_OFFSET 0x04 /* Mode Register */
58#define CDNS_UART_IER_OFFSET 0x08 /* Interrupt Enable */
59#define CDNS_UART_IDR_OFFSET 0x0C /* Interrupt Disable */
60#define CDNS_UART_IMR_OFFSET 0x10 /* Interrupt Mask */
61#define CDNS_UART_ISR_OFFSET 0x14 /* Interrupt Status */
62#define CDNS_UART_BAUDGEN_OFFSET 0x18 /* Baud Rate Generator */
63#define CDNS_UART_RXTOUT_OFFSET 0x1C /* RX Timeout */
64#define CDNS_UART_RXWM_OFFSET 0x20 /* RX FIFO Trigger Level */
65#define CDNS_UART_MODEMCR_OFFSET 0x24 /* Modem Control */
66#define CDNS_UART_MODEMSR_OFFSET 0x28 /* Modem Status */
67#define CDNS_UART_SR_OFFSET 0x2C /* Channel Status */
68#define CDNS_UART_FIFO_OFFSET 0x30 /* FIFO */
69#define CDNS_UART_BAUDDIV_OFFSET 0x34 /* Baud Rate Divider */
70#define CDNS_UART_FLOWDEL_OFFSET 0x38 /* Flow Delay */
71#define CDNS_UART_IRRX_PWIDTH_OFFSET 0x3C /* IR Min Received Pulse Width */
72#define CDNS_UART_IRTX_PWIDTH_OFFSET 0x40 /* IR Transmitted pulse Width */
73#define CDNS_UART_TXWM_OFFSET 0x44 /* TX FIFO Trigger Level */
John Linn61ec9012011-04-30 00:07:43 -040074
Soren Brinkmanne555a212014-04-04 17:23:39 -070075/* Control Register Bit Definitions */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070076#define CDNS_UART_CR_STOPBRK 0x00000100 /* Stop TX break */
77#define CDNS_UART_CR_STARTBRK 0x00000080 /* Set TX break */
78#define CDNS_UART_CR_TX_DIS 0x00000020 /* TX disabled. */
79#define CDNS_UART_CR_TX_EN 0x00000010 /* TX enabled */
80#define CDNS_UART_CR_RX_DIS 0x00000008 /* RX disabled. */
81#define CDNS_UART_CR_RX_EN 0x00000004 /* RX enabled */
82#define CDNS_UART_CR_TXRST 0x00000002 /* TX logic reset */
83#define CDNS_UART_CR_RXRST 0x00000001 /* RX logic reset */
84#define CDNS_UART_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
John Linn61ec9012011-04-30 00:07:43 -040085
Soren Brinkmanne555a212014-04-04 17:23:39 -070086/*
87 * Mode Register:
John Linn61ec9012011-04-30 00:07:43 -040088 * The mode register (MR) defines the mode of transfer as well as the data
89 * format. If this register is modified during transmission or reception,
90 * data validity cannot be guaranteed.
John Linn61ec9012011-04-30 00:07:43 -040091 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070092#define CDNS_UART_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
93#define CDNS_UART_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
94#define CDNS_UART_MR_CHMODE_NORM 0x00000000 /* Normal mode */
John Linn61ec9012011-04-30 00:07:43 -040095
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070096#define CDNS_UART_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
97#define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
John Linn61ec9012011-04-30 00:07:43 -040098
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -070099#define CDNS_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
100#define CDNS_UART_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
101#define CDNS_UART_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
102#define CDNS_UART_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
103#define CDNS_UART_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
John Linn61ec9012011-04-30 00:07:43 -0400104
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700105#define CDNS_UART_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
106#define CDNS_UART_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
107#define CDNS_UART_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
John Linn61ec9012011-04-30 00:07:43 -0400108
Soren Brinkmanne555a212014-04-04 17:23:39 -0700109/*
110 * Interrupt Registers:
John Linn61ec9012011-04-30 00:07:43 -0400111 * Interrupt control logic uses the interrupt enable register (IER) and the
112 * interrupt disable register (IDR) to set the value of the bits in the
113 * interrupt mask register (IMR). The IMR determines whether to pass an
114 * interrupt to the interrupt status register (ISR).
115 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
116 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
117 * Reading either IER or IDR returns 0x00.
John Linn61ec9012011-04-30 00:07:43 -0400118 * All four registers have the same bit definitions.
119 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700120#define CDNS_UART_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
121#define CDNS_UART_IXR_PARITY 0x00000080 /* Parity error interrupt */
122#define CDNS_UART_IXR_FRAMING 0x00000040 /* Framing error interrupt */
123#define CDNS_UART_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
124#define CDNS_UART_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
125#define CDNS_UART_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
126#define CDNS_UART_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
127#define CDNS_UART_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
128#define CDNS_UART_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
129#define CDNS_UART_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
130#define CDNS_UART_IXR_MASK 0x00001FFF /* Valid bit mask */
John Linn61ec9012011-04-30 00:07:43 -0400131
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700132/* Goes in read_status_mask for break detection as the HW doesn't do it*/
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700133#define CDNS_UART_IXR_BRK 0x80000000
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700134
Soren Brinkmanne555a212014-04-04 17:23:39 -0700135/*
Lars-Peter Clausen19038ad2014-11-05 13:35:16 +0100136 * Modem Control register:
137 * The read/write Modem Control register controls the interface with the modem
138 * or data set, or a peripheral device emulating a modem.
139 */
140#define CDNS_UART_MODEMCR_FCM 0x00000020 /* Automatic flow control mode */
141#define CDNS_UART_MODEMCR_RTS 0x00000002 /* Request to send output control */
142#define CDNS_UART_MODEMCR_DTR 0x00000001 /* Data Terminal Ready */
143
144/*
Soren Brinkmanne555a212014-04-04 17:23:39 -0700145 * Channel Status Register:
John Linn61ec9012011-04-30 00:07:43 -0400146 * The channel status register (CSR) is provided to enable the control logic
147 * to monitor the status of bits in the channel interrupt status register,
148 * even if these are masked out by the interrupt mask register.
149 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700150#define CDNS_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
151#define CDNS_UART_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
152#define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
153#define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
John Linn61ec9012011-04-30 00:07:43 -0400154
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700155/* baud dividers min/max values */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700156#define CDNS_UART_BDIV_MIN 4
157#define CDNS_UART_BDIV_MAX 255
158#define CDNS_UART_CD_MAX 65535
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700159
John Linn61ec9012011-04-30 00:07:43 -0400160/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700161 * struct cdns_uart - device data
Michal Simek489810a12014-04-04 17:23:37 -0700162 * @port: Pointer to the UART port
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700163 * @uartclk: Reference clock
164 * @pclk: APB clock
Michal Simek489810a12014-04-04 17:23:37 -0700165 * @baud: Current baud rate
166 * @clk_rate_change_nb: Notifier block for clock changes
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700167 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700168struct cdns_uart {
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700169 struct uart_port *port;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700170 struct clk *uartclk;
171 struct clk *pclk;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700172 unsigned int baud;
173 struct notifier_block clk_rate_change_nb;
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700174};
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700175#define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
176 clk_rate_change_nb);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700177
178/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700179 * cdns_uart_isr - Interrupt handler
John Linn61ec9012011-04-30 00:07:43 -0400180 * @irq: Irq number
181 * @dev_id: Id of the port
182 *
Michal Simek489810a12014-04-04 17:23:37 -0700183 * Return: IRQHANDLED
184 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700185static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
John Linn61ec9012011-04-30 00:07:43 -0400186{
187 struct uart_port *port = (struct uart_port *)dev_id;
John Linn61ec9012011-04-30 00:07:43 -0400188 unsigned long flags;
189 unsigned int isrstatus, numbytes;
190 unsigned int data;
191 char status = TTY_NORMAL;
192
John Linn61ec9012011-04-30 00:07:43 -0400193 spin_lock_irqsave(&port->lock, flags);
194
195 /* Read the interrupt status register to determine which
196 * interrupt(s) is/are active.
197 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700198 isrstatus = cdns_uart_readl(CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400199
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700200 /*
201 * There is no hardware break detection, so we interpret framing
202 * error with all-zeros data as a break sequence. Most of the time,
203 * there's another non-zero byte at the end of the sequence.
204 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700205 if (isrstatus & CDNS_UART_IXR_FRAMING) {
206 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
207 CDNS_UART_SR_RXEMPTY)) {
208 if (!cdns_uart_readl(CDNS_UART_FIFO_OFFSET)) {
209 port->read_status_mask |= CDNS_UART_IXR_BRK;
210 isrstatus &= ~CDNS_UART_IXR_FRAMING;
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700211 }
212 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700213 cdns_uart_writel(CDNS_UART_IXR_FRAMING, CDNS_UART_ISR_OFFSET);
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700214 }
215
John Linn61ec9012011-04-30 00:07:43 -0400216 /* drop byte with parity error if IGNPAR specified */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700217 if (isrstatus & port->ignore_status_mask & CDNS_UART_IXR_PARITY)
218 isrstatus &= ~(CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT);
John Linn61ec9012011-04-30 00:07:43 -0400219
220 isrstatus &= port->read_status_mask;
221 isrstatus &= ~port->ignore_status_mask;
222
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700223 if ((isrstatus & CDNS_UART_IXR_TOUT) ||
224 (isrstatus & CDNS_UART_IXR_RXTRIG)) {
John Linn61ec9012011-04-30 00:07:43 -0400225 /* Receive Timeout Interrupt */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700226 while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
227 CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
228 data = cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700229
230 /* Non-NULL byte after BREAK is garbage (99%) */
231 if (data && (port->read_status_mask &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700232 CDNS_UART_IXR_BRK)) {
233 port->read_status_mask &= ~CDNS_UART_IXR_BRK;
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700234 port->icount.brk++;
235 if (uart_handle_break(port))
236 continue;
237 }
238
Soren Brinkmannc2db11e2013-12-02 11:38:38 -0800239#ifdef SUPPORT_SYSRQ
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700240 /*
241 * uart_handle_sysrq_char() doesn't work if
242 * spinlocked, for some reason
243 */
244 if (port->sysrq) {
245 spin_unlock(&port->lock);
246 if (uart_handle_sysrq_char(port,
247 (unsigned char)data)) {
248 spin_lock(&port->lock);
249 continue;
250 }
251 spin_lock(&port->lock);
252 }
Soren Brinkmannc2db11e2013-12-02 11:38:38 -0800253#endif
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700254
John Linn61ec9012011-04-30 00:07:43 -0400255 port->icount.rx++;
256
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700257 if (isrstatus & CDNS_UART_IXR_PARITY) {
John Linn61ec9012011-04-30 00:07:43 -0400258 port->icount.parity++;
259 status = TTY_PARITY;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700260 } else if (isrstatus & CDNS_UART_IXR_FRAMING) {
John Linn61ec9012011-04-30 00:07:43 -0400261 port->icount.frame++;
262 status = TTY_FRAME;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700263 } else if (isrstatus & CDNS_UART_IXR_OVERRUN) {
John Linn61ec9012011-04-30 00:07:43 -0400264 port->icount.overrun++;
Soren Brinkmanne555a212014-04-04 17:23:39 -0700265 }
John Linn61ec9012011-04-30 00:07:43 -0400266
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700267 uart_insert_char(port, isrstatus, CDNS_UART_IXR_OVERRUN,
Jiri Slaby2e124b42013-01-03 15:53:06 +0100268 data, status);
John Linn61ec9012011-04-30 00:07:43 -0400269 }
270 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100271 tty_flip_buffer_push(&port->state->port);
John Linn61ec9012011-04-30 00:07:43 -0400272 spin_lock(&port->lock);
273 }
274
275 /* Dispatch an appropriate handler */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700276 if ((isrstatus & CDNS_UART_IXR_TXEMPTY) == CDNS_UART_IXR_TXEMPTY) {
John Linn61ec9012011-04-30 00:07:43 -0400277 if (uart_circ_empty(&port->state->xmit)) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700278 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY,
279 CDNS_UART_IDR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400280 } else {
281 numbytes = port->fifosize;
282 /* Break if no more data available in the UART buffer */
283 while (numbytes--) {
284 if (uart_circ_empty(&port->state->xmit))
285 break;
286 /* Get the data from the UART circular buffer
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700287 * and write it to the cdns_uart's TX_FIFO
John Linn61ec9012011-04-30 00:07:43 -0400288 * register.
289 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700290 cdns_uart_writel(
John Linn61ec9012011-04-30 00:07:43 -0400291 port->state->xmit.buf[port->state->xmit.
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700292 tail], CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400293
294 port->icount.tx++;
295
296 /* Adjust the tail of the UART buffer and wrap
297 * the buffer if it reaches limit.
298 */
299 port->state->xmit.tail =
Soren Brinkmanne555a212014-04-04 17:23:39 -0700300 (port->state->xmit.tail + 1) &
John Linn61ec9012011-04-30 00:07:43 -0400301 (UART_XMIT_SIZE - 1);
302 }
303
304 if (uart_circ_chars_pending(
305 &port->state->xmit) < WAKEUP_CHARS)
306 uart_write_wakeup(port);
307 }
308 }
309
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700310 cdns_uart_writel(isrstatus, CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400311
312 /* be sure to release the lock and tty before leaving */
313 spin_unlock_irqrestore(&port->lock, flags);
John Linn61ec9012011-04-30 00:07:43 -0400314
315 return IRQ_HANDLED;
316}
317
318/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700319 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700320 * @clk: UART module input clock
321 * @baud: Desired baud rate
322 * @rbdiv: BDIV value (return value)
323 * @rcd: CD value (return value)
324 * @div8: Value for clk_sel bit in mod (return value)
Michal Simek489810a12014-04-04 17:23:37 -0700325 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700326 * was too much error, zero if no valid divisors are found.
327 *
328 * Formula to obtain baud rate is
329 * baud_tx/rx rate = clk/CD * (BDIV + 1)
330 * input_clk = (Uart User Defined Clock or Apb Clock)
331 * depends on UCLKEN in MR Reg
332 * clk = input_clk or input_clk/8;
333 * depends on CLKS in MR reg
334 * CD and BDIV depends on values in
335 * baud rate generate register
336 * baud rate clock divisor register
337 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700338static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
339 unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
John Linn61ec9012011-04-30 00:07:43 -0400340{
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700341 u32 cd, bdiv;
342 unsigned int calc_baud;
343 unsigned int bestbaud = 0;
John Linn61ec9012011-04-30 00:07:43 -0400344 unsigned int bauderror;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700345 unsigned int besterror = ~0;
John Linn61ec9012011-04-30 00:07:43 -0400346
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700347 if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700348 *div8 = 1;
349 clk /= 8;
350 } else {
351 *div8 = 0;
352 }
John Linn61ec9012011-04-30 00:07:43 -0400353
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700354 for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700355 cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700356 if (cd < 1 || cd > CDNS_UART_CD_MAX)
John Linn61ec9012011-04-30 00:07:43 -0400357 continue;
358
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700359 calc_baud = clk / (cd * (bdiv + 1));
John Linn61ec9012011-04-30 00:07:43 -0400360
361 if (baud > calc_baud)
362 bauderror = baud - calc_baud;
363 else
364 bauderror = calc_baud - baud;
365
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700366 if (besterror > bauderror) {
367 *rbdiv = bdiv;
368 *rcd = cd;
369 bestbaud = calc_baud;
370 besterror = bauderror;
John Linn61ec9012011-04-30 00:07:43 -0400371 }
372 }
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700373 /* use the values when percent error is acceptable */
374 if (((besterror * 100) / baud) < 3)
375 bestbaud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400376
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700377 return bestbaud;
378}
379
380/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700381 * cdns_uart_set_baud_rate - Calculate and set the baud rate
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700382 * @port: Handle to the uart port structure
383 * @baud: Baud rate to set
Michal Simek489810a12014-04-04 17:23:37 -0700384 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700385 * was too much error, zero if no valid divisors are found.
386 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700387static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700388 unsigned int baud)
389{
390 unsigned int calc_baud;
Soren Brinkmannd54b1812013-10-21 16:40:59 -0700391 u32 cd = 0, bdiv = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700392 u32 mreg;
393 int div8;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700394 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700395
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700396 calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700397 &div8);
398
399 /* Write new divisors to hardware */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700400 mreg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700401 if (div8)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700402 mreg |= CDNS_UART_MR_CLKSEL;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700403 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700404 mreg &= ~CDNS_UART_MR_CLKSEL;
405 cdns_uart_writel(mreg, CDNS_UART_MR_OFFSET);
406 cdns_uart_writel(cd, CDNS_UART_BAUDGEN_OFFSET);
407 cdns_uart_writel(bdiv, CDNS_UART_BAUDDIV_OFFSET);
408 cdns_uart->baud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400409
410 return calc_baud;
411}
412
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700413#ifdef CONFIG_COMMON_CLK
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700414/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700415 * cdns_uart_clk_notitifer_cb - Clock notifier callback
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700416 * @nb: Notifier block
417 * @event: Notify event
418 * @data: Notifier data
Soren Brinkmanne555a212014-04-04 17:23:39 -0700419 * Return: NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700420 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700421static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700422 unsigned long event, void *data)
423{
424 u32 ctrl_reg;
425 struct uart_port *port;
426 int locked = 0;
427 struct clk_notifier_data *ndata = data;
428 unsigned long flags = 0;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700429 struct cdns_uart *cdns_uart = to_cdns_uart(nb);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700430
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700431 port = cdns_uart->port;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700432 if (port->suspended)
433 return NOTIFY_OK;
434
435 switch (event) {
436 case PRE_RATE_CHANGE:
437 {
Soren Brinkmanne555a212014-04-04 17:23:39 -0700438 u32 bdiv, cd;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700439 int div8;
440
441 /*
442 * Find out if current baud-rate can be achieved with new clock
443 * frequency.
444 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700445 if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700446 &bdiv, &cd, &div8)) {
447 dev_warn(port->dev, "clock rate change rejected\n");
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700448 return NOTIFY_BAD;
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700449 }
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700450
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700451 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700452
453 /* Disable the TX and RX to set baud rate */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700454 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
455 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
456 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700457
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700458 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700459
460 return NOTIFY_OK;
461 }
462 case POST_RATE_CHANGE:
463 /*
464 * Set clk dividers to generate correct baud with new clock
465 * frequency.
466 */
467
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700468 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700469
470 locked = 1;
471 port->uartclk = ndata->new_rate;
472
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700473 cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
474 cdns_uart->baud);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700475 /* fall through */
476 case ABORT_RATE_CHANGE:
477 if (!locked)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700478 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700479
480 /* Set TX/RX Reset */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700481 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
482 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
483 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700484
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700485 while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
486 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700487 cpu_relax();
488
489 /*
490 * Clear the RX disable and TX disable bits and then set the TX
491 * enable bit and RX enable bit to enable the transmitter and
492 * receiver.
493 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700494 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
495 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
496 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
497 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
498 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700499
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700500 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700501
502 return NOTIFY_OK;
503 default:
504 return NOTIFY_DONE;
505 }
506}
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700507#endif
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700508
John Linn61ec9012011-04-30 00:07:43 -0400509/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700510 * cdns_uart_start_tx - Start transmitting bytes
John Linn61ec9012011-04-30 00:07:43 -0400511 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700512 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700513static void cdns_uart_start_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400514{
515 unsigned int status, numbytes = port->fifosize;
516
517 if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
518 return;
519
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700520 status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400521 /* Set the TX enable bit and clear the TX disable bit to enable the
522 * transmitter.
523 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700524 cdns_uart_writel((status & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
525 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400526
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700527 while (numbytes-- && ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
528 CDNS_UART_SR_TXFULL)) != CDNS_UART_SR_TXFULL) {
John Linn61ec9012011-04-30 00:07:43 -0400529 /* Break if no more data available in the UART buffer */
530 if (uart_circ_empty(&port->state->xmit))
531 break;
532
533 /* Get the data from the UART circular buffer and
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700534 * write it to the cdns_uart's TX_FIFO register.
John Linn61ec9012011-04-30 00:07:43 -0400535 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700536 cdns_uart_writel(
John Linn61ec9012011-04-30 00:07:43 -0400537 port->state->xmit.buf[port->state->xmit.tail],
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700538 CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400539 port->icount.tx++;
540
541 /* Adjust the tail of the UART buffer and wrap
542 * the buffer if it reaches limit.
543 */
544 port->state->xmit.tail = (port->state->xmit.tail + 1) &
545 (UART_XMIT_SIZE - 1);
546 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700547 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400548 /* Enable the TX Empty interrupt */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700549 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_IER_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400550
551 if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
552 uart_write_wakeup(port);
553}
554
555/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700556 * cdns_uart_stop_tx - Stop TX
John Linn61ec9012011-04-30 00:07:43 -0400557 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700558 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700559static void cdns_uart_stop_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400560{
561 unsigned int regval;
562
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700563 regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
564 regval |= CDNS_UART_CR_TX_DIS;
John Linn61ec9012011-04-30 00:07:43 -0400565 /* Disable the transmitter */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700566 cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400567}
568
569/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700570 * cdns_uart_stop_rx - Stop RX
John Linn61ec9012011-04-30 00:07:43 -0400571 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700572 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700573static void cdns_uart_stop_rx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400574{
575 unsigned int regval;
576
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700577 regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
578 regval |= CDNS_UART_CR_RX_DIS;
John Linn61ec9012011-04-30 00:07:43 -0400579 /* Disable the receiver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700580 cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400581}
582
583/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700584 * cdns_uart_tx_empty - Check whether TX is empty
John Linn61ec9012011-04-30 00:07:43 -0400585 * @port: Handle to the uart port structure
586 *
Michal Simek489810a12014-04-04 17:23:37 -0700587 * Return: TIOCSER_TEMT on success, 0 otherwise
588 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700589static unsigned int cdns_uart_tx_empty(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400590{
591 unsigned int status;
592
Lars-Peter Clausen6731af52014-09-05 09:45:17 +0200593 status = cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY;
John Linn61ec9012011-04-30 00:07:43 -0400594 return status ? TIOCSER_TEMT : 0;
595}
596
597/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700598 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
John Linn61ec9012011-04-30 00:07:43 -0400599 * transmitting char breaks
600 * @port: Handle to the uart port structure
601 * @ctl: Value based on which start or stop decision is taken
Michal Simek489810a12014-04-04 17:23:37 -0700602 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700603static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
John Linn61ec9012011-04-30 00:07:43 -0400604{
605 unsigned int status;
606 unsigned long flags;
607
608 spin_lock_irqsave(&port->lock, flags);
609
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700610 status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400611
612 if (ctl == -1)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700613 cdns_uart_writel(CDNS_UART_CR_STARTBRK | status,
614 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400615 else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700616 if ((status & CDNS_UART_CR_STOPBRK) == 0)
617 cdns_uart_writel(CDNS_UART_CR_STOPBRK | status,
618 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400619 }
620 spin_unlock_irqrestore(&port->lock, flags);
621}
622
623/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700624 * cdns_uart_set_termios - termios operations, handling data length, parity,
John Linn61ec9012011-04-30 00:07:43 -0400625 * stop bits, flow control, baud rate
626 * @port: Handle to the uart port structure
627 * @termios: Handle to the input termios structure
628 * @old: Values of the previously saved termios structure
Michal Simek489810a12014-04-04 17:23:37 -0700629 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700630static void cdns_uart_set_termios(struct uart_port *port,
John Linn61ec9012011-04-30 00:07:43 -0400631 struct ktermios *termios, struct ktermios *old)
632{
633 unsigned int cval = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700634 unsigned int baud, minbaud, maxbaud;
John Linn61ec9012011-04-30 00:07:43 -0400635 unsigned long flags;
636 unsigned int ctrl_reg, mode_reg;
637
638 spin_lock_irqsave(&port->lock, flags);
639
Nathan Rossi6ecde472015-01-16 13:49:25 +0100640 /* Wait for the transmit FIFO to empty before making changes */
641 if (!(cdns_uart_readl(CDNS_UART_CR_OFFSET) & CDNS_UART_CR_TX_DIS)) {
642 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
643 CDNS_UART_SR_TXEMPTY)) {
644 cpu_relax();
645 }
John Linn61ec9012011-04-30 00:07:43 -0400646 }
647
648 /* Disable the TX and RX to set baud rate */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700649 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
650 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
651 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400652
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700653 /*
654 * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
655 * min and max baud should be calculated here based on port->uartclk.
656 * this way we get a valid baud and can safely call set_baud()
657 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700658 minbaud = port->uartclk /
659 ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
660 maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700661 baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700662 baud = cdns_uart_set_baud_rate(port, baud);
John Linn61ec9012011-04-30 00:07:43 -0400663 if (tty_termios_baud_rate(termios))
664 tty_termios_encode_baud_rate(termios, baud, baud);
665
Soren Brinkmanne555a212014-04-04 17:23:39 -0700666 /* Update the per-port timeout. */
John Linn61ec9012011-04-30 00:07:43 -0400667 uart_update_timeout(port, termios->c_cflag, baud);
668
669 /* Set TX/RX Reset */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700670 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
671 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
672 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400673
Soren Brinkmanne555a212014-04-04 17:23:39 -0700674 /*
675 * Clear the RX disable and TX disable bits and then set the TX enable
John Linn61ec9012011-04-30 00:07:43 -0400676 * bit and RX enable bit to enable the transmitter and receiver.
677 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700678 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
679 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
680 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
681 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400682
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700683 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400684
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700685 port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
686 CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
John Linn61ec9012011-04-30 00:07:43 -0400687 port->ignore_status_mask = 0;
688
689 if (termios->c_iflag & INPCK)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700690 port->read_status_mask |= CDNS_UART_IXR_PARITY |
691 CDNS_UART_IXR_FRAMING;
John Linn61ec9012011-04-30 00:07:43 -0400692
693 if (termios->c_iflag & IGNPAR)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700694 port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
695 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400696
697 /* ignore all characters if CREAD is not set */
698 if ((termios->c_cflag & CREAD) == 0)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700699 port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
700 CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
701 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400702
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700703 mode_reg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400704
705 /* Handling Data Size */
706 switch (termios->c_cflag & CSIZE) {
707 case CS6:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700708 cval |= CDNS_UART_MR_CHARLEN_6_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400709 break;
710 case CS7:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700711 cval |= CDNS_UART_MR_CHARLEN_7_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400712 break;
713 default:
714 case CS8:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700715 cval |= CDNS_UART_MR_CHARLEN_8_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400716 termios->c_cflag &= ~CSIZE;
717 termios->c_cflag |= CS8;
718 break;
719 }
720
721 /* Handling Parity and Stop Bits length */
722 if (termios->c_cflag & CSTOPB)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700723 cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
John Linn61ec9012011-04-30 00:07:43 -0400724 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700725 cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
John Linn61ec9012011-04-30 00:07:43 -0400726
727 if (termios->c_cflag & PARENB) {
728 /* Mark or Space parity */
729 if (termios->c_cflag & CMSPAR) {
730 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700731 cval |= CDNS_UART_MR_PARITY_MARK;
John Linn61ec9012011-04-30 00:07:43 -0400732 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700733 cval |= CDNS_UART_MR_PARITY_SPACE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700734 } else {
735 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700736 cval |= CDNS_UART_MR_PARITY_ODD;
John Linn61ec9012011-04-30 00:07:43 -0400737 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700738 cval |= CDNS_UART_MR_PARITY_EVEN;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700739 }
740 } else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700741 cval |= CDNS_UART_MR_PARITY_NONE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700742 }
743 cval |= mode_reg & 1;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700744 cdns_uart_writel(cval, CDNS_UART_MR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400745
746 spin_unlock_irqrestore(&port->lock, flags);
747}
748
749/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700750 * cdns_uart_startup - Called when an application opens a cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400751 * @port: Handle to the uart port structure
752 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700753 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -0700754 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700755static int cdns_uart_startup(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400756{
757 unsigned int retval = 0, status = 0;
758
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700759 retval = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME,
John Linn61ec9012011-04-30 00:07:43 -0400760 (void *)port);
761 if (retval)
762 return retval;
763
764 /* Disable the TX and RX */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700765 cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
766 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400767
768 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
769 * no break chars.
770 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700771 cdns_uart_writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
772 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400773
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700774 status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400775
776 /* Clear the RX disable and TX disable bits and then set the TX enable
777 * bit and RX enable bit to enable the transmitter and receiver.
778 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700779 cdns_uart_writel((status & ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS))
780 | (CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN |
781 CDNS_UART_CR_STOPBRK), 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 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700786 cdns_uart_writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
787 | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
788 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 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700794 cdns_uart_writel(rx_trigger_level, 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 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700800 cdns_uart_writel(rx_timeout, 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 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700803 cdns_uart_writel(cdns_uart_readl(CDNS_UART_ISR_OFFSET),
804 CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400805
806 /* Set the Interrupt Registers with desired interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700807 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_PARITY |
808 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN |
809 CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT,
810 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 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700824 status = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
825 cdns_uart_writel(status, CDNS_UART_IDR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400826
827 /* Disable the TX and RX */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700828 cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
829 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
931 val = cdns_uart_readl(CDNS_UART_MODEMCR_OFFSET);
932
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
940 cdns_uart_writel(val, 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 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700950 imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
951 cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700952
953 /* Check if FIFO is empty */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700954 if (cdns_uart_readl(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 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700957 c = (unsigned char) cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700958
959 /* Enable interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700960 cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700961
962 return c;
963}
964
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700965static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700966{
967 u32 imr;
968
969 /* Disable all interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700970 imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
971 cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700972
973 /* Wait until FIFO is empty */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700974 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700975 cpu_relax();
976
977 /* Write a character */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700978 cdns_uart_writel(c, CDNS_UART_FIFO_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700979
980 /* Wait until FIFO is empty */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700981 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700982 cpu_relax();
983
984 /* Enable interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700985 cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700986
987 return;
988}
989#endif
990
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700991static struct uart_ops cdns_uart_ops = {
992 .set_mctrl = cdns_uart_set_mctrl,
993 .get_mctrl = cdns_uart_get_mctrl,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700994 .start_tx = cdns_uart_start_tx,
995 .stop_tx = cdns_uart_stop_tx,
996 .stop_rx = cdns_uart_stop_rx,
997 .tx_empty = cdns_uart_tx_empty,
998 .break_ctl = cdns_uart_break_ctl,
999 .set_termios = cdns_uart_set_termios,
1000 .startup = cdns_uart_startup,
1001 .shutdown = cdns_uart_shutdown,
1002 .type = cdns_uart_type,
1003 .verify_port = cdns_uart_verify_port,
1004 .request_port = cdns_uart_request_port,
1005 .release_port = cdns_uart_release_port,
1006 .config_port = cdns_uart_config_port,
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001007#ifdef CONFIG_CONSOLE_POLL
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001008 .poll_get_char = cdns_uart_poll_get_char,
1009 .poll_put_char = cdns_uart_poll_put_char,
Vlad Lungu6ee04c62013-10-17 14:08:07 -07001010#endif
John Linn61ec9012011-04-30 00:07:43 -04001011};
1012
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001013static struct uart_port cdns_uart_port[2];
John Linn61ec9012011-04-30 00:07:43 -04001014
1015/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001016 * cdns_uart_get_port - Configure the port from platform device resource info
Michal Simek928e9262014-04-04 17:23:38 -07001017 * @id: Port id
1018 *
Michal Simek489810a12014-04-04 17:23:37 -07001019 * Return: a pointer to a uart_port or NULL for failure
1020 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001021static struct uart_port *cdns_uart_get_port(int id)
John Linn61ec9012011-04-30 00:07:43 -04001022{
1023 struct uart_port *port;
John Linn61ec9012011-04-30 00:07:43 -04001024
Michal Simek928e9262014-04-04 17:23:38 -07001025 /* Try the given port id if failed use default method */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001026 if (cdns_uart_port[id].mapbase != 0) {
Michal Simek928e9262014-04-04 17:23:38 -07001027 /* Find the next unused port */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001028 for (id = 0; id < CDNS_UART_NR_PORTS; id++)
1029 if (cdns_uart_port[id].mapbase == 0)
Michal Simek928e9262014-04-04 17:23:38 -07001030 break;
1031 }
John Linn61ec9012011-04-30 00:07:43 -04001032
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001033 if (id >= CDNS_UART_NR_PORTS)
John Linn61ec9012011-04-30 00:07:43 -04001034 return NULL;
1035
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001036 port = &cdns_uart_port[id];
John Linn61ec9012011-04-30 00:07:43 -04001037
1038 /* At this point, we've got an empty uart_port struct, initialize it */
1039 spin_lock_init(&port->lock);
1040 port->membase = NULL;
1041 port->iobase = 1; /* mark port in use */
1042 port->irq = 0;
1043 port->type = PORT_UNKNOWN;
1044 port->iotype = UPIO_MEM32;
1045 port->flags = UPF_BOOT_AUTOCONF;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001046 port->ops = &cdns_uart_ops;
1047 port->fifosize = CDNS_UART_FIFO_SIZE;
John Linn61ec9012011-04-30 00:07:43 -04001048 port->line = id;
1049 port->dev = NULL;
1050 return port;
1051}
1052
John Linn61ec9012011-04-30 00:07:43 -04001053#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1054/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001055 * cdns_uart_console_wait_tx - Wait for the TX to be full
John Linn61ec9012011-04-30 00:07:43 -04001056 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -07001057 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001058static void cdns_uart_console_wait_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -04001059{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001060 while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY)
1061 != CDNS_UART_SR_TXEMPTY)
John Linn61ec9012011-04-30 00:07:43 -04001062 barrier();
1063}
1064
1065/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001066 * cdns_uart_console_putchar - write the character to the FIFO buffer
John Linn61ec9012011-04-30 00:07:43 -04001067 * @port: Handle to the uart port structure
1068 * @ch: Character to be written
Michal Simek489810a12014-04-04 17:23:37 -07001069 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001070static void cdns_uart_console_putchar(struct uart_port *port, int ch)
John Linn61ec9012011-04-30 00:07:43 -04001071{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001072 cdns_uart_console_wait_tx(port);
1073 cdns_uart_writel(ch, CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -04001074}
1075
Michal Simek6fa62fc2014-09-10 12:43:02 +02001076static void cdns_early_write(struct console *con, const char *s, unsigned n)
1077{
1078 struct earlycon_device *dev = con->data;
1079
1080 uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
1081}
1082
1083static int __init cdns_early_console_setup(struct earlycon_device *device,
1084 const char *opt)
1085{
1086 if (!device->port.membase)
1087 return -ENODEV;
1088
1089 device->con->write = cdns_early_write;
1090
1091 return 0;
1092}
1093EARLYCON_DECLARE(cdns, cdns_early_console_setup);
1094
John Linn61ec9012011-04-30 00:07:43 -04001095/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001096 * cdns_uart_console_write - perform write operation
Michal Simek489810a12014-04-04 17:23:37 -07001097 * @co: Console handle
John Linn61ec9012011-04-30 00:07:43 -04001098 * @s: Pointer to character array
1099 * @count: No of characters
Michal Simek489810a12014-04-04 17:23:37 -07001100 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001101static void cdns_uart_console_write(struct console *co, const char *s,
John Linn61ec9012011-04-30 00:07:43 -04001102 unsigned int count)
1103{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001104 struct uart_port *port = &cdns_uart_port[co->index];
John Linn61ec9012011-04-30 00:07:43 -04001105 unsigned long flags;
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001106 unsigned int imr, ctrl;
John Linn61ec9012011-04-30 00:07:43 -04001107 int locked = 1;
1108
1109 if (oops_in_progress)
1110 locked = spin_trylock_irqsave(&port->lock, flags);
1111 else
1112 spin_lock_irqsave(&port->lock, flags);
1113
1114 /* save and disable interrupt */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001115 imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
1116 cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -04001117
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001118 /*
1119 * Make sure that the tx part is enabled. Set the TX enable bit and
1120 * clear the TX disable bit to enable the transmitter.
1121 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001122 ctrl = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1123 cdns_uart_writel((ctrl & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
1124 CDNS_UART_CR_OFFSET);
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001125
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001126 uart_console_write(port, s, count, cdns_uart_console_putchar);
1127 cdns_uart_console_wait_tx(port);
John Linn61ec9012011-04-30 00:07:43 -04001128
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001129 cdns_uart_writel(ctrl, CDNS_UART_CR_OFFSET);
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001130
Soren Brinkmannb494a5f2014-04-04 17:23:42 -07001131 /* restore interrupt state */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001132 cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -04001133
1134 if (locked)
1135 spin_unlock_irqrestore(&port->lock, flags);
1136}
1137
1138/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001139 * cdns_uart_console_setup - Initialize the uart to default config
John Linn61ec9012011-04-30 00:07:43 -04001140 * @co: Console handle
1141 * @options: Initial settings of uart
1142 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001143 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -07001144 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001145static int __init cdns_uart_console_setup(struct console *co, char *options)
John Linn61ec9012011-04-30 00:07:43 -04001146{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001147 struct uart_port *port = &cdns_uart_port[co->index];
John Linn61ec9012011-04-30 00:07:43 -04001148 int baud = 9600;
1149 int bits = 8;
1150 int parity = 'n';
1151 int flow = 'n';
1152
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001153 if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
John Linn61ec9012011-04-30 00:07:43 -04001154 return -EINVAL;
1155
1156 if (!port->mapbase) {
Peter Crosthwaitef6415492015-02-24 15:13:57 -08001157 pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
1158 co->index);
John Linn61ec9012011-04-30 00:07:43 -04001159 return -ENODEV;
1160 }
1161
1162 if (options)
1163 uart_parse_options(options, &baud, &parity, &bits, &flow);
1164
1165 return uart_set_options(port, co, baud, parity, bits, flow);
1166}
1167
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001168static struct uart_driver cdns_uart_uart_driver;
John Linn61ec9012011-04-30 00:07:43 -04001169
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001170static struct console cdns_uart_console = {
1171 .name = CDNS_UART_TTY_NAME,
1172 .write = cdns_uart_console_write,
John Linn61ec9012011-04-30 00:07:43 -04001173 .device = uart_console_device,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001174 .setup = cdns_uart_console_setup,
John Linn61ec9012011-04-30 00:07:43 -04001175 .flags = CON_PRINTBUFFER,
1176 .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001177 .data = &cdns_uart_uart_driver,
John Linn61ec9012011-04-30 00:07:43 -04001178};
1179
1180/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001181 * cdns_uart_console_init - Initialization call
John Linn61ec9012011-04-30 00:07:43 -04001182 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001183 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001184 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001185static int __init cdns_uart_console_init(void)
John Linn61ec9012011-04-30 00:07:43 -04001186{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001187 register_console(&cdns_uart_console);
John Linn61ec9012011-04-30 00:07:43 -04001188 return 0;
1189}
1190
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001191console_initcall(cdns_uart_console_init);
John Linn61ec9012011-04-30 00:07:43 -04001192
1193#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
1194
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001195static struct uart_driver cdns_uart_uart_driver = {
Soren Brinkmanne555a212014-04-04 17:23:39 -07001196 .owner = THIS_MODULE,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001197 .driver_name = CDNS_UART_NAME,
1198 .dev_name = CDNS_UART_TTY_NAME,
1199 .major = CDNS_UART_MAJOR,
1200 .minor = CDNS_UART_MINOR,
1201 .nr = CDNS_UART_NR_PORTS,
Soren Brinkmannd3641f62013-10-21 16:41:00 -07001202#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001203 .cons = &cdns_uart_console,
Soren Brinkmannd3641f62013-10-21 16:41:00 -07001204#endif
1205};
1206
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001207#ifdef CONFIG_PM_SLEEP
1208/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001209 * cdns_uart_suspend - suspend event
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001210 * @device: Pointer to the device structure
1211 *
Michal Simek489810a12014-04-04 17:23:37 -07001212 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001213 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001214static int cdns_uart_suspend(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001215{
1216 struct uart_port *port = dev_get_drvdata(device);
1217 struct tty_struct *tty;
1218 struct device *tty_dev;
1219 int may_wake = 0;
1220
1221 /* Get the tty which could be NULL so don't assume it's valid */
1222 tty = tty_port_tty_get(&port->state->port);
1223 if (tty) {
1224 tty_dev = tty->dev;
1225 may_wake = device_may_wakeup(tty_dev);
1226 tty_kref_put(tty);
1227 }
1228
1229 /*
1230 * Call the API provided in serial_core.c file which handles
1231 * the suspend.
1232 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001233 uart_suspend_port(&cdns_uart_uart_driver, port);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001234 if (console_suspend_enabled && !may_wake) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001235 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001236
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001237 clk_disable(cdns_uart->uartclk);
1238 clk_disable(cdns_uart->pclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001239 } else {
1240 unsigned long flags = 0;
1241
1242 spin_lock_irqsave(&port->lock, flags);
1243 /* Empty the receive FIFO 1st before making changes */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001244 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
1245 CDNS_UART_SR_RXEMPTY))
1246 cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001247 /* set RX trigger level to 1 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001248 cdns_uart_writel(1, CDNS_UART_RXWM_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001249 /* disable RX timeout interrups */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001250 cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IDR_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001251 spin_unlock_irqrestore(&port->lock, flags);
1252 }
1253
1254 return 0;
1255}
1256
1257/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001258 * cdns_uart_resume - Resume after a previous suspend
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001259 * @device: Pointer to the device structure
1260 *
Michal Simek489810a12014-04-04 17:23:37 -07001261 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001262 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001263static int cdns_uart_resume(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001264{
1265 struct uart_port *port = dev_get_drvdata(device);
1266 unsigned long flags = 0;
1267 u32 ctrl_reg;
1268 struct tty_struct *tty;
1269 struct device *tty_dev;
1270 int may_wake = 0;
1271
1272 /* Get the tty which could be NULL so don't assume it's valid */
1273 tty = tty_port_tty_get(&port->state->port);
1274 if (tty) {
1275 tty_dev = tty->dev;
1276 may_wake = device_may_wakeup(tty_dev);
1277 tty_kref_put(tty);
1278 }
1279
1280 if (console_suspend_enabled && !may_wake) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001281 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001282
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001283 clk_enable(cdns_uart->pclk);
1284 clk_enable(cdns_uart->uartclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001285
1286 spin_lock_irqsave(&port->lock, flags);
1287
1288 /* Set TX/RX Reset */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001289 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1290 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
1291 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
1292 while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
1293 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001294 cpu_relax();
1295
1296 /* restore rx timeout value */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001297 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001298 /* Enable Tx/Rx */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001299 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1300 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1301 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
1302 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001303
1304 spin_unlock_irqrestore(&port->lock, flags);
1305 } else {
1306 spin_lock_irqsave(&port->lock, flags);
1307 /* restore original rx trigger level */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001308 cdns_uart_writel(rx_trigger_level, CDNS_UART_RXWM_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001309 /* enable RX timeout interrupt */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001310 cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IER_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001311 spin_unlock_irqrestore(&port->lock, flags);
1312 }
1313
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001314 return uart_resume_port(&cdns_uart_uart_driver, port);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001315}
1316#endif /* ! CONFIG_PM_SLEEP */
1317
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001318static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
1319 cdns_uart_resume);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001320
John Linn61ec9012011-04-30 00:07:43 -04001321/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001322 * cdns_uart_probe - Platform driver probe
John Linn61ec9012011-04-30 00:07:43 -04001323 * @pdev: Pointer to the platform device structure
1324 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001325 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001326 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001327static int cdns_uart_probe(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -04001328{
Michal Simek928e9262014-04-04 17:23:38 -07001329 int rc, id;
John Linn61ec9012011-04-30 00:07:43 -04001330 struct uart_port *port;
1331 struct resource *res, *res2;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001332 struct cdns_uart *cdns_uart_data;
John Linn61ec9012011-04-30 00:07:43 -04001333
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001334 cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
Soren Brinkmannc03cae12013-10-17 14:08:05 -07001335 GFP_KERNEL);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001336 if (!cdns_uart_data)
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001337 return -ENOMEM;
1338
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001339 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
1340 if (IS_ERR(cdns_uart_data->pclk)) {
1341 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1342 if (!IS_ERR(cdns_uart_data->pclk))
1343 dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001344 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001345 if (IS_ERR(cdns_uart_data->pclk)) {
1346 dev_err(&pdev->dev, "pclk clock not found.\n");
1347 return PTR_ERR(cdns_uart_data->pclk);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001348 }
1349
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001350 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1351 if (IS_ERR(cdns_uart_data->uartclk)) {
1352 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1353 if (!IS_ERR(cdns_uart_data->uartclk))
1354 dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1355 }
1356 if (IS_ERR(cdns_uart_data->uartclk)) {
1357 dev_err(&pdev->dev, "uart_clk clock not found.\n");
1358 return PTR_ERR(cdns_uart_data->uartclk);
1359 }
1360
1361 rc = clk_prepare_enable(cdns_uart_data->pclk);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001362 if (rc) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001363 dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
Soren Brinkmannc03cae12013-10-17 14:08:05 -07001364 return rc;
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001365 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001366 rc = clk_prepare_enable(cdns_uart_data->uartclk);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001367 if (rc) {
1368 dev_err(&pdev->dev, "Unable to enable device clock.\n");
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001369 goto err_out_clk_dis_pclk;
John Linn61ec9012011-04-30 00:07:43 -04001370 }
1371
1372 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001373 if (!res) {
1374 rc = -ENODEV;
1375 goto err_out_clk_disable;
1376 }
John Linn61ec9012011-04-30 00:07:43 -04001377
1378 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001379 if (!res2) {
1380 rc = -ENODEV;
1381 goto err_out_clk_disable;
1382 }
John Linn61ec9012011-04-30 00:07:43 -04001383
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001384#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001385 cdns_uart_data->clk_rate_change_nb.notifier_call =
1386 cdns_uart_clk_notifier_cb;
1387 if (clk_notifier_register(cdns_uart_data->uartclk,
1388 &cdns_uart_data->clk_rate_change_nb))
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001389 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001390#endif
Michal Simek928e9262014-04-04 17:23:38 -07001391 /* Look for a serialN alias */
1392 id = of_alias_get_id(pdev->dev.of_node, "serial");
1393 if (id < 0)
1394 id = 0;
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001395
John Linn61ec9012011-04-30 00:07:43 -04001396 /* Initialize the port structure */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001397 port = cdns_uart_get_port(id);
John Linn61ec9012011-04-30 00:07:43 -04001398
1399 if (!port) {
1400 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001401 rc = -ENODEV;
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001402 goto err_out_notif_unreg;
John Linn61ec9012011-04-30 00:07:43 -04001403 } else {
1404 /* Register the port.
1405 * This function also registers this device with the tty layer
1406 * and triggers invocation of the config_port() entry point.
1407 */
1408 port->mapbase = res->start;
1409 port->irq = res2->start;
1410 port->dev = &pdev->dev;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001411 port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1412 port->private_data = cdns_uart_data;
1413 cdns_uart_data->port = port;
Jingoo Han696faed2013-05-23 19:39:36 +09001414 platform_set_drvdata(pdev, port);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001415 rc = uart_add_one_port(&cdns_uart_uart_driver, port);
John Linn61ec9012011-04-30 00:07:43 -04001416 if (rc) {
1417 dev_err(&pdev->dev,
1418 "uart_add_one_port() failed; err=%i\n", rc);
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001419 goto err_out_notif_unreg;
John Linn61ec9012011-04-30 00:07:43 -04001420 }
1421 return 0;
1422 }
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001423
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001424err_out_notif_unreg:
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001425#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001426 clk_notifier_unregister(cdns_uart_data->uartclk,
1427 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001428#endif
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001429err_out_clk_disable:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001430 clk_disable_unprepare(cdns_uart_data->uartclk);
1431err_out_clk_dis_pclk:
1432 clk_disable_unprepare(cdns_uart_data->pclk);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001433
1434 return rc;
John Linn61ec9012011-04-30 00:07:43 -04001435}
1436
1437/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001438 * cdns_uart_remove - called when the platform driver is unregistered
John Linn61ec9012011-04-30 00:07:43 -04001439 * @pdev: Pointer to the platform device structure
1440 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001441 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001442 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001443static int cdns_uart_remove(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -04001444{
Jingoo Han696faed2013-05-23 19:39:36 +09001445 struct uart_port *port = platform_get_drvdata(pdev);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001446 struct cdns_uart *cdns_uart_data = port->private_data;
Josh Cartwright2326669c2013-01-21 19:57:41 +01001447 int rc;
John Linn61ec9012011-04-30 00:07:43 -04001448
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001449 /* Remove the cdns_uart port from the serial core */
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001450#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001451 clk_notifier_unregister(cdns_uart_data->uartclk,
1452 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001453#endif
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001454 rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001455 port->mapbase = 0;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001456 clk_disable_unprepare(cdns_uart_data->uartclk);
1457 clk_disable_unprepare(cdns_uart_data->pclk);
John Linn61ec9012011-04-30 00:07:43 -04001458 return rc;
1459}
1460
John Linn61ec9012011-04-30 00:07:43 -04001461/* Match table for of_platform binding */
Fabian Fredericked0bb232015-03-16 20:17:11 +01001462static const struct of_device_id cdns_uart_of_match[] = {
John Linn61ec9012011-04-30 00:07:43 -04001463 { .compatible = "xlnx,xuartps", },
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001464 { .compatible = "cdns,uart-r1p8", },
John Linn61ec9012011-04-30 00:07:43 -04001465 {}
1466};
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001467MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
John Linn61ec9012011-04-30 00:07:43 -04001468
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001469static struct platform_driver cdns_uart_platform_driver = {
1470 .probe = cdns_uart_probe,
1471 .remove = cdns_uart_remove,
John Linn61ec9012011-04-30 00:07:43 -04001472 .driver = {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001473 .name = CDNS_UART_NAME,
1474 .of_match_table = cdns_uart_of_match,
1475 .pm = &cdns_uart_dev_pm_ops,
John Linn61ec9012011-04-30 00:07:43 -04001476 },
1477};
1478
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001479static int __init cdns_uart_init(void)
John Linn61ec9012011-04-30 00:07:43 -04001480{
1481 int retval = 0;
1482
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001483 /* Register the cdns_uart driver with the serial core */
1484 retval = uart_register_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001485 if (retval)
1486 return retval;
1487
1488 /* Register the platform driver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001489 retval = platform_driver_register(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001490 if (retval)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001491 uart_unregister_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001492
1493 return retval;
1494}
1495
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001496static void __exit cdns_uart_exit(void)
John Linn61ec9012011-04-30 00:07:43 -04001497{
John Linn61ec9012011-04-30 00:07:43 -04001498 /* Unregister the platform driver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001499 platform_driver_unregister(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001500
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001501 /* Unregister the cdns_uart driver */
1502 uart_unregister_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001503}
1504
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001505module_init(cdns_uart_init);
1506module_exit(cdns_uart_exit);
John Linn61ec9012011-04-30 00:07:43 -04001507
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001508MODULE_DESCRIPTION("Driver for Cadence UART");
John Linn61ec9012011-04-30 00:07:43 -04001509MODULE_AUTHOR("Xilinx Inc.");
1510MODULE_LICENSE("GPL");