blob: 8809775e2ba30c5d53aa5108a75f3f67aa8e0640 [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 */
40#define CDNS_UART_REGISTER_SPACE 0xFFF
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/*
136 * Channel Status Register:
John Linn61ec9012011-04-30 00:07:43 -0400137 * The channel status register (CSR) is provided to enable the control logic
138 * to monitor the status of bits in the channel interrupt status register,
139 * even if these are masked out by the interrupt mask register.
140 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700141#define CDNS_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
142#define CDNS_UART_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
143#define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
144#define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
John Linn61ec9012011-04-30 00:07:43 -0400145
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700146/* baud dividers min/max values */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700147#define CDNS_UART_BDIV_MIN 4
148#define CDNS_UART_BDIV_MAX 255
149#define CDNS_UART_CD_MAX 65535
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700150
John Linn61ec9012011-04-30 00:07:43 -0400151/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700152 * struct cdns_uart - device data
Michal Simek489810a12014-04-04 17:23:37 -0700153 * @port: Pointer to the UART port
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700154 * @uartclk: Reference clock
155 * @pclk: APB clock
Michal Simek489810a12014-04-04 17:23:37 -0700156 * @baud: Current baud rate
157 * @clk_rate_change_nb: Notifier block for clock changes
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700158 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700159struct cdns_uart {
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700160 struct uart_port *port;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700161 struct clk *uartclk;
162 struct clk *pclk;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700163 unsigned int baud;
164 struct notifier_block clk_rate_change_nb;
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700165};
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700166#define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
167 clk_rate_change_nb);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700168
169/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700170 * cdns_uart_isr - Interrupt handler
John Linn61ec9012011-04-30 00:07:43 -0400171 * @irq: Irq number
172 * @dev_id: Id of the port
173 *
Michal Simek489810a12014-04-04 17:23:37 -0700174 * Return: IRQHANDLED
175 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700176static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
John Linn61ec9012011-04-30 00:07:43 -0400177{
178 struct uart_port *port = (struct uart_port *)dev_id;
John Linn61ec9012011-04-30 00:07:43 -0400179 unsigned long flags;
180 unsigned int isrstatus, numbytes;
181 unsigned int data;
182 char status = TTY_NORMAL;
183
John Linn61ec9012011-04-30 00:07:43 -0400184 spin_lock_irqsave(&port->lock, flags);
185
186 /* Read the interrupt status register to determine which
187 * interrupt(s) is/are active.
188 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700189 isrstatus = cdns_uart_readl(CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400190
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700191 /*
192 * There is no hardware break detection, so we interpret framing
193 * error with all-zeros data as a break sequence. Most of the time,
194 * there's another non-zero byte at the end of the sequence.
195 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700196 if (isrstatus & CDNS_UART_IXR_FRAMING) {
197 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
198 CDNS_UART_SR_RXEMPTY)) {
199 if (!cdns_uart_readl(CDNS_UART_FIFO_OFFSET)) {
200 port->read_status_mask |= CDNS_UART_IXR_BRK;
201 isrstatus &= ~CDNS_UART_IXR_FRAMING;
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700202 }
203 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700204 cdns_uart_writel(CDNS_UART_IXR_FRAMING, CDNS_UART_ISR_OFFSET);
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700205 }
206
John Linn61ec9012011-04-30 00:07:43 -0400207 /* drop byte with parity error if IGNPAR specified */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700208 if (isrstatus & port->ignore_status_mask & CDNS_UART_IXR_PARITY)
209 isrstatus &= ~(CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT);
John Linn61ec9012011-04-30 00:07:43 -0400210
211 isrstatus &= port->read_status_mask;
212 isrstatus &= ~port->ignore_status_mask;
213
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700214 if ((isrstatus & CDNS_UART_IXR_TOUT) ||
215 (isrstatus & CDNS_UART_IXR_RXTRIG)) {
John Linn61ec9012011-04-30 00:07:43 -0400216 /* Receive Timeout Interrupt */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700217 while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
218 CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
219 data = cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700220
221 /* Non-NULL byte after BREAK is garbage (99%) */
222 if (data && (port->read_status_mask &
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700223 CDNS_UART_IXR_BRK)) {
224 port->read_status_mask &= ~CDNS_UART_IXR_BRK;
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700225 port->icount.brk++;
226 if (uart_handle_break(port))
227 continue;
228 }
229
Soren Brinkmannc2db11e2013-12-02 11:38:38 -0800230#ifdef SUPPORT_SYSRQ
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700231 /*
232 * uart_handle_sysrq_char() doesn't work if
233 * spinlocked, for some reason
234 */
235 if (port->sysrq) {
236 spin_unlock(&port->lock);
237 if (uart_handle_sysrq_char(port,
238 (unsigned char)data)) {
239 spin_lock(&port->lock);
240 continue;
241 }
242 spin_lock(&port->lock);
243 }
Soren Brinkmannc2db11e2013-12-02 11:38:38 -0800244#endif
Vlad Lungu0c0c47b2013-10-17 14:08:06 -0700245
John Linn61ec9012011-04-30 00:07:43 -0400246 port->icount.rx++;
247
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700248 if (isrstatus & CDNS_UART_IXR_PARITY) {
John Linn61ec9012011-04-30 00:07:43 -0400249 port->icount.parity++;
250 status = TTY_PARITY;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700251 } else if (isrstatus & CDNS_UART_IXR_FRAMING) {
John Linn61ec9012011-04-30 00:07:43 -0400252 port->icount.frame++;
253 status = TTY_FRAME;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700254 } else if (isrstatus & CDNS_UART_IXR_OVERRUN) {
John Linn61ec9012011-04-30 00:07:43 -0400255 port->icount.overrun++;
Soren Brinkmanne555a212014-04-04 17:23:39 -0700256 }
John Linn61ec9012011-04-30 00:07:43 -0400257
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700258 uart_insert_char(port, isrstatus, CDNS_UART_IXR_OVERRUN,
Jiri Slaby2e124b42013-01-03 15:53:06 +0100259 data, status);
John Linn61ec9012011-04-30 00:07:43 -0400260 }
261 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100262 tty_flip_buffer_push(&port->state->port);
John Linn61ec9012011-04-30 00:07:43 -0400263 spin_lock(&port->lock);
264 }
265
266 /* Dispatch an appropriate handler */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700267 if ((isrstatus & CDNS_UART_IXR_TXEMPTY) == CDNS_UART_IXR_TXEMPTY) {
John Linn61ec9012011-04-30 00:07:43 -0400268 if (uart_circ_empty(&port->state->xmit)) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700269 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY,
270 CDNS_UART_IDR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400271 } else {
272 numbytes = port->fifosize;
273 /* Break if no more data available in the UART buffer */
274 while (numbytes--) {
275 if (uart_circ_empty(&port->state->xmit))
276 break;
277 /* Get the data from the UART circular buffer
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700278 * and write it to the cdns_uart's TX_FIFO
John Linn61ec9012011-04-30 00:07:43 -0400279 * register.
280 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700281 cdns_uart_writel(
John Linn61ec9012011-04-30 00:07:43 -0400282 port->state->xmit.buf[port->state->xmit.
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700283 tail], CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400284
285 port->icount.tx++;
286
287 /* Adjust the tail of the UART buffer and wrap
288 * the buffer if it reaches limit.
289 */
290 port->state->xmit.tail =
Soren Brinkmanne555a212014-04-04 17:23:39 -0700291 (port->state->xmit.tail + 1) &
John Linn61ec9012011-04-30 00:07:43 -0400292 (UART_XMIT_SIZE - 1);
293 }
294
295 if (uart_circ_chars_pending(
296 &port->state->xmit) < WAKEUP_CHARS)
297 uart_write_wakeup(port);
298 }
299 }
300
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700301 cdns_uart_writel(isrstatus, CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400302
303 /* be sure to release the lock and tty before leaving */
304 spin_unlock_irqrestore(&port->lock, flags);
John Linn61ec9012011-04-30 00:07:43 -0400305
306 return IRQ_HANDLED;
307}
308
309/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700310 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700311 * @clk: UART module input clock
312 * @baud: Desired baud rate
313 * @rbdiv: BDIV value (return value)
314 * @rcd: CD value (return value)
315 * @div8: Value for clk_sel bit in mod (return value)
Michal Simek489810a12014-04-04 17:23:37 -0700316 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700317 * was too much error, zero if no valid divisors are found.
318 *
319 * Formula to obtain baud rate is
320 * baud_tx/rx rate = clk/CD * (BDIV + 1)
321 * input_clk = (Uart User Defined Clock or Apb Clock)
322 * depends on UCLKEN in MR Reg
323 * clk = input_clk or input_clk/8;
324 * depends on CLKS in MR reg
325 * CD and BDIV depends on values in
326 * baud rate generate register
327 * baud rate clock divisor register
328 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700329static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
330 unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
John Linn61ec9012011-04-30 00:07:43 -0400331{
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700332 u32 cd, bdiv;
333 unsigned int calc_baud;
334 unsigned int bestbaud = 0;
John Linn61ec9012011-04-30 00:07:43 -0400335 unsigned int bauderror;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700336 unsigned int besterror = ~0;
John Linn61ec9012011-04-30 00:07:43 -0400337
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700338 if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700339 *div8 = 1;
340 clk /= 8;
341 } else {
342 *div8 = 0;
343 }
John Linn61ec9012011-04-30 00:07:43 -0400344
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700345 for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700346 cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700347 if (cd < 1 || cd > CDNS_UART_CD_MAX)
John Linn61ec9012011-04-30 00:07:43 -0400348 continue;
349
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700350 calc_baud = clk / (cd * (bdiv + 1));
John Linn61ec9012011-04-30 00:07:43 -0400351
352 if (baud > calc_baud)
353 bauderror = baud - calc_baud;
354 else
355 bauderror = calc_baud - baud;
356
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700357 if (besterror > bauderror) {
358 *rbdiv = bdiv;
359 *rcd = cd;
360 bestbaud = calc_baud;
361 besterror = bauderror;
John Linn61ec9012011-04-30 00:07:43 -0400362 }
363 }
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700364 /* use the values when percent error is acceptable */
365 if (((besterror * 100) / baud) < 3)
366 bestbaud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400367
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700368 return bestbaud;
369}
370
371/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700372 * cdns_uart_set_baud_rate - Calculate and set the baud rate
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700373 * @port: Handle to the uart port structure
374 * @baud: Baud rate to set
Michal Simek489810a12014-04-04 17:23:37 -0700375 * Return: baud rate, requested baud when possible, or actual baud when there
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700376 * was too much error, zero if no valid divisors are found.
377 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700378static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700379 unsigned int baud)
380{
381 unsigned int calc_baud;
Soren Brinkmannd54b1812013-10-21 16:40:59 -0700382 u32 cd = 0, bdiv = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700383 u32 mreg;
384 int div8;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700385 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700386
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700387 calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700388 &div8);
389
390 /* Write new divisors to hardware */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700391 mreg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700392 if (div8)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700393 mreg |= CDNS_UART_MR_CLKSEL;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700394 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700395 mreg &= ~CDNS_UART_MR_CLKSEL;
396 cdns_uart_writel(mreg, CDNS_UART_MR_OFFSET);
397 cdns_uart_writel(cd, CDNS_UART_BAUDGEN_OFFSET);
398 cdns_uart_writel(bdiv, CDNS_UART_BAUDDIV_OFFSET);
399 cdns_uart->baud = baud;
John Linn61ec9012011-04-30 00:07:43 -0400400
401 return calc_baud;
402}
403
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700404#ifdef CONFIG_COMMON_CLK
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700405/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700406 * cdns_uart_clk_notitifer_cb - Clock notifier callback
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700407 * @nb: Notifier block
408 * @event: Notify event
409 * @data: Notifier data
Soren Brinkmanne555a212014-04-04 17:23:39 -0700410 * Return: NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700411 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700412static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700413 unsigned long event, void *data)
414{
415 u32 ctrl_reg;
416 struct uart_port *port;
417 int locked = 0;
418 struct clk_notifier_data *ndata = data;
419 unsigned long flags = 0;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700420 struct cdns_uart *cdns_uart = to_cdns_uart(nb);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700421
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700422 port = cdns_uart->port;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700423 if (port->suspended)
424 return NOTIFY_OK;
425
426 switch (event) {
427 case PRE_RATE_CHANGE:
428 {
Soren Brinkmanne555a212014-04-04 17:23:39 -0700429 u32 bdiv, cd;
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700430 int div8;
431
432 /*
433 * Find out if current baud-rate can be achieved with new clock
434 * frequency.
435 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700436 if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700437 &bdiv, &cd, &div8)) {
438 dev_warn(port->dev, "clock rate change rejected\n");
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700439 return NOTIFY_BAD;
Soren Brinkmann5ce15d22014-04-04 17:23:40 -0700440 }
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700441
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700442 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700443
444 /* Disable the TX and RX to set baud rate */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700445 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
446 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
447 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700448
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700449 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700450
451 return NOTIFY_OK;
452 }
453 case POST_RATE_CHANGE:
454 /*
455 * Set clk dividers to generate correct baud with new clock
456 * frequency.
457 */
458
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700459 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700460
461 locked = 1;
462 port->uartclk = ndata->new_rate;
463
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700464 cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
465 cdns_uart->baud);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700466 /* fall through */
467 case ABORT_RATE_CHANGE:
468 if (!locked)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700469 spin_lock_irqsave(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700470
471 /* Set TX/RX Reset */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700472 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
473 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
474 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700475
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700476 while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
477 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700478 cpu_relax();
479
480 /*
481 * Clear the RX disable and TX disable bits and then set the TX
482 * enable bit and RX enable bit to enable the transmitter and
483 * receiver.
484 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700485 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
486 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
487 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
488 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
489 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700490
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700491 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700492
493 return NOTIFY_OK;
494 default:
495 return NOTIFY_DONE;
496 }
497}
Soren Brinkmann7ac57342013-10-21 16:41:01 -0700498#endif
Soren Brinkmannc4b05102013-10-17 14:08:11 -0700499
John Linn61ec9012011-04-30 00:07:43 -0400500/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700501 * cdns_uart_start_tx - Start transmitting bytes
John Linn61ec9012011-04-30 00:07:43 -0400502 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700503 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700504static void cdns_uart_start_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400505{
506 unsigned int status, numbytes = port->fifosize;
507
508 if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
509 return;
510
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700511 status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400512 /* Set the TX enable bit and clear the TX disable bit to enable the
513 * transmitter.
514 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700515 cdns_uart_writel((status & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
516 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400517
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700518 while (numbytes-- && ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
519 CDNS_UART_SR_TXFULL)) != CDNS_UART_SR_TXFULL) {
John Linn61ec9012011-04-30 00:07:43 -0400520 /* Break if no more data available in the UART buffer */
521 if (uart_circ_empty(&port->state->xmit))
522 break;
523
524 /* Get the data from the UART circular buffer and
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700525 * write it to the cdns_uart's TX_FIFO register.
John Linn61ec9012011-04-30 00:07:43 -0400526 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700527 cdns_uart_writel(
John Linn61ec9012011-04-30 00:07:43 -0400528 port->state->xmit.buf[port->state->xmit.tail],
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700529 CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400530 port->icount.tx++;
531
532 /* Adjust the tail of the UART buffer and wrap
533 * the buffer if it reaches limit.
534 */
535 port->state->xmit.tail = (port->state->xmit.tail + 1) &
536 (UART_XMIT_SIZE - 1);
537 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700538 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400539 /* Enable the TX Empty interrupt */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700540 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_IER_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400541
542 if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
543 uart_write_wakeup(port);
544}
545
546/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700547 * cdns_uart_stop_tx - Stop TX
John Linn61ec9012011-04-30 00:07:43 -0400548 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700549 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700550static void cdns_uart_stop_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400551{
552 unsigned int regval;
553
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700554 regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
555 regval |= CDNS_UART_CR_TX_DIS;
John Linn61ec9012011-04-30 00:07:43 -0400556 /* Disable the transmitter */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700557 cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400558}
559
560/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700561 * cdns_uart_stop_rx - Stop RX
John Linn61ec9012011-04-30 00:07:43 -0400562 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700563 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700564static void cdns_uart_stop_rx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400565{
566 unsigned int regval;
567
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700568 regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
569 regval |= CDNS_UART_CR_RX_DIS;
John Linn61ec9012011-04-30 00:07:43 -0400570 /* Disable the receiver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700571 cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400572}
573
574/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700575 * cdns_uart_tx_empty - Check whether TX is empty
John Linn61ec9012011-04-30 00:07:43 -0400576 * @port: Handle to the uart port structure
577 *
Michal Simek489810a12014-04-04 17:23:37 -0700578 * Return: TIOCSER_TEMT on success, 0 otherwise
579 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700580static unsigned int cdns_uart_tx_empty(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400581{
582 unsigned int status;
583
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700584 status = cdns_uart_readl(CDNS_UART_ISR_OFFSET) & CDNS_UART_IXR_TXEMPTY;
John Linn61ec9012011-04-30 00:07:43 -0400585 return status ? TIOCSER_TEMT : 0;
586}
587
588/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700589 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
John Linn61ec9012011-04-30 00:07:43 -0400590 * transmitting char breaks
591 * @port: Handle to the uart port structure
592 * @ctl: Value based on which start or stop decision is taken
Michal Simek489810a12014-04-04 17:23:37 -0700593 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700594static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
John Linn61ec9012011-04-30 00:07:43 -0400595{
596 unsigned int status;
597 unsigned long flags;
598
599 spin_lock_irqsave(&port->lock, flags);
600
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700601 status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400602
603 if (ctl == -1)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700604 cdns_uart_writel(CDNS_UART_CR_STARTBRK | status,
605 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400606 else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700607 if ((status & CDNS_UART_CR_STOPBRK) == 0)
608 cdns_uart_writel(CDNS_UART_CR_STOPBRK | status,
609 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400610 }
611 spin_unlock_irqrestore(&port->lock, flags);
612}
613
614/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700615 * cdns_uart_set_termios - termios operations, handling data length, parity,
John Linn61ec9012011-04-30 00:07:43 -0400616 * stop bits, flow control, baud rate
617 * @port: Handle to the uart port structure
618 * @termios: Handle to the input termios structure
619 * @old: Values of the previously saved termios structure
Michal Simek489810a12014-04-04 17:23:37 -0700620 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700621static void cdns_uart_set_termios(struct uart_port *port,
John Linn61ec9012011-04-30 00:07:43 -0400622 struct ktermios *termios, struct ktermios *old)
623{
624 unsigned int cval = 0;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700625 unsigned int baud, minbaud, maxbaud;
John Linn61ec9012011-04-30 00:07:43 -0400626 unsigned long flags;
627 unsigned int ctrl_reg, mode_reg;
628
629 spin_lock_irqsave(&port->lock, flags);
630
631 /* Empty the receive FIFO 1st before making changes */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700632 while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
633 CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
634 cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400635 }
636
637 /* Disable the TX and RX to set baud rate */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700638 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
639 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
640 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400641
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700642 /*
643 * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
644 * min and max baud should be calculated here based on port->uartclk.
645 * this way we get a valid baud and can safely call set_baud()
646 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700647 minbaud = port->uartclk /
648 ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
649 maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700650 baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700651 baud = cdns_uart_set_baud_rate(port, baud);
John Linn61ec9012011-04-30 00:07:43 -0400652 if (tty_termios_baud_rate(termios))
653 tty_termios_encode_baud_rate(termios, baud, baud);
654
Soren Brinkmanne555a212014-04-04 17:23:39 -0700655 /* Update the per-port timeout. */
John Linn61ec9012011-04-30 00:07:43 -0400656 uart_update_timeout(port, termios->c_cflag, baud);
657
658 /* Set TX/RX Reset */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700659 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
660 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
661 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400662
Soren Brinkmanne555a212014-04-04 17:23:39 -0700663 /*
664 * Clear the RX disable and TX disable bits and then set the TX enable
John Linn61ec9012011-04-30 00:07:43 -0400665 * bit and RX enable bit to enable the transmitter and receiver.
666 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700667 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
668 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
669 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
670 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400671
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700672 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400673
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700674 port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
675 CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
John Linn61ec9012011-04-30 00:07:43 -0400676 port->ignore_status_mask = 0;
677
678 if (termios->c_iflag & INPCK)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700679 port->read_status_mask |= CDNS_UART_IXR_PARITY |
680 CDNS_UART_IXR_FRAMING;
John Linn61ec9012011-04-30 00:07:43 -0400681
682 if (termios->c_iflag & IGNPAR)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700683 port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
684 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400685
686 /* ignore all characters if CREAD is not set */
687 if ((termios->c_cflag & CREAD) == 0)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700688 port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
689 CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
690 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
John Linn61ec9012011-04-30 00:07:43 -0400691
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700692 mode_reg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400693
694 /* Handling Data Size */
695 switch (termios->c_cflag & CSIZE) {
696 case CS6:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700697 cval |= CDNS_UART_MR_CHARLEN_6_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400698 break;
699 case CS7:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700700 cval |= CDNS_UART_MR_CHARLEN_7_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400701 break;
702 default:
703 case CS8:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700704 cval |= CDNS_UART_MR_CHARLEN_8_BIT;
John Linn61ec9012011-04-30 00:07:43 -0400705 termios->c_cflag &= ~CSIZE;
706 termios->c_cflag |= CS8;
707 break;
708 }
709
710 /* Handling Parity and Stop Bits length */
711 if (termios->c_cflag & CSTOPB)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700712 cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
John Linn61ec9012011-04-30 00:07:43 -0400713 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700714 cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
John Linn61ec9012011-04-30 00:07:43 -0400715
716 if (termios->c_cflag & PARENB) {
717 /* Mark or Space parity */
718 if (termios->c_cflag & CMSPAR) {
719 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700720 cval |= CDNS_UART_MR_PARITY_MARK;
John Linn61ec9012011-04-30 00:07:43 -0400721 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700722 cval |= CDNS_UART_MR_PARITY_SPACE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700723 } else {
724 if (termios->c_cflag & PARODD)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700725 cval |= CDNS_UART_MR_PARITY_ODD;
John Linn61ec9012011-04-30 00:07:43 -0400726 else
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700727 cval |= CDNS_UART_MR_PARITY_EVEN;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700728 }
729 } else {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700730 cval |= CDNS_UART_MR_PARITY_NONE;
Soren Brinkmanne6b39bf2013-10-17 14:08:10 -0700731 }
732 cval |= mode_reg & 1;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700733 cdns_uart_writel(cval, CDNS_UART_MR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400734
735 spin_unlock_irqrestore(&port->lock, flags);
736}
737
738/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700739 * cdns_uart_startup - Called when an application opens a cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400740 * @port: Handle to the uart port structure
741 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700742 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -0700743 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700744static int cdns_uart_startup(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400745{
746 unsigned int retval = 0, status = 0;
747
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700748 retval = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME,
John Linn61ec9012011-04-30 00:07:43 -0400749 (void *)port);
750 if (retval)
751 return retval;
752
753 /* Disable the TX and RX */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700754 cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
755 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400756
757 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
758 * no break chars.
759 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700760 cdns_uart_writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
761 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400762
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700763 status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400764
765 /* Clear the RX disable and TX disable bits and then set the TX enable
766 * bit and RX enable bit to enable the transmitter and receiver.
767 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700768 cdns_uart_writel((status & ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS))
769 | (CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN |
770 CDNS_UART_CR_STOPBRK), CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400771
772 /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
773 * no parity.
774 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700775 cdns_uart_writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
776 | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
777 CDNS_UART_MR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400778
Suneel85baf542013-10-17 14:08:08 -0700779 /*
780 * Set the RX FIFO Trigger level to use most of the FIFO, but it
781 * can be tuned with a module parameter
782 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700783 cdns_uart_writel(rx_trigger_level, CDNS_UART_RXWM_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400784
Suneel85baf542013-10-17 14:08:08 -0700785 /*
786 * Receive Timeout register is enabled but it
787 * can be tuned with a module parameter
788 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700789 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400790
John Linn855f6fd2013-03-22 18:49:27 +0100791 /* Clear out any pending interrupts before enabling them */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700792 cdns_uart_writel(cdns_uart_readl(CDNS_UART_ISR_OFFSET),
793 CDNS_UART_ISR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400794
795 /* Set the Interrupt Registers with desired interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700796 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_PARITY |
797 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN |
798 CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT,
799 CDNS_UART_IER_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400800
801 return retval;
802}
803
804/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700805 * cdns_uart_shutdown - Called when an application closes a cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400806 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -0700807 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700808static void cdns_uart_shutdown(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400809{
810 int status;
811
812 /* Disable interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700813 status = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
814 cdns_uart_writel(status, CDNS_UART_IDR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400815
816 /* Disable the TX and RX */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700817 cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
818 CDNS_UART_CR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -0400819 free_irq(port->irq, port);
820}
821
822/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700823 * cdns_uart_type - Set UART type to cdns_uart port
John Linn61ec9012011-04-30 00:07:43 -0400824 * @port: Handle to the uart port structure
825 *
Michal Simek489810a12014-04-04 17:23:37 -0700826 * Return: string on success, NULL otherwise
827 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700828static const char *cdns_uart_type(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400829{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700830 return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
John Linn61ec9012011-04-30 00:07:43 -0400831}
832
833/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700834 * cdns_uart_verify_port - Verify the port params
John Linn61ec9012011-04-30 00:07:43 -0400835 * @port: Handle to the uart port structure
836 * @ser: Handle to the structure whose members are compared
837 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700838 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -0700839 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700840static int cdns_uart_verify_port(struct uart_port *port,
John Linn61ec9012011-04-30 00:07:43 -0400841 struct serial_struct *ser)
842{
843 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
844 return -EINVAL;
845 if (port->irq != ser->irq)
846 return -EINVAL;
847 if (ser->io_type != UPIO_MEM)
848 return -EINVAL;
849 if (port->iobase != ser->port)
850 return -EINVAL;
851 if (ser->hub6 != 0)
852 return -EINVAL;
853 return 0;
854}
855
856/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700857 * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
858 * called when the driver adds a cdns_uart port via
John Linn61ec9012011-04-30 00:07:43 -0400859 * uart_add_one_port()
860 * @port: Handle to the uart port structure
861 *
Soren Brinkmanne555a212014-04-04 17:23:39 -0700862 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -0700863 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700864static int cdns_uart_request_port(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400865{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700866 if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
867 CDNS_UART_NAME)) {
John Linn61ec9012011-04-30 00:07:43 -0400868 return -ENOMEM;
869 }
870
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700871 port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400872 if (!port->membase) {
873 dev_err(port->dev, "Unable to map registers\n");
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700874 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400875 return -ENOMEM;
876 }
877 return 0;
878}
879
880/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700881 * cdns_uart_release_port - Release UART port
John Linn61ec9012011-04-30 00:07:43 -0400882 * @port: Handle to the uart port structure
Soren Brinkmanne555a212014-04-04 17:23:39 -0700883 *
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700884 * Release the memory region attached to a cdns_uart port. Called when the
885 * driver removes a cdns_uart port via uart_remove_one_port().
Michal Simek489810a12014-04-04 17:23:37 -0700886 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700887static void cdns_uart_release_port(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400888{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700889 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
John Linn61ec9012011-04-30 00:07:43 -0400890 iounmap(port->membase);
891 port->membase = NULL;
892}
893
894/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700895 * cdns_uart_config_port - Configure UART port
John Linn61ec9012011-04-30 00:07:43 -0400896 * @port: Handle to the uart port structure
897 * @flags: If any
Michal Simek489810a12014-04-04 17:23:37 -0700898 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700899static void cdns_uart_config_port(struct uart_port *port, int flags)
John Linn61ec9012011-04-30 00:07:43 -0400900{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700901 if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
John Linn61ec9012011-04-30 00:07:43 -0400902 port->type = PORT_XUARTPS;
903}
904
905/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700906 * cdns_uart_get_mctrl - Get the modem control state
John Linn61ec9012011-04-30 00:07:43 -0400907 * @port: Handle to the uart port structure
908 *
Michal Simek489810a12014-04-04 17:23:37 -0700909 * Return: the modem control state
910 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700911static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400912{
913 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
914}
915
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700916static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
John Linn61ec9012011-04-30 00:07:43 -0400917{
918 /* N/A */
919}
920
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700921static void cdns_uart_enable_ms(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -0400922{
923 /* N/A */
924}
925
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700926#ifdef CONFIG_CONSOLE_POLL
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700927static int cdns_uart_poll_get_char(struct uart_port *port)
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700928{
929 u32 imr;
930 int c;
931
932 /* Disable all interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700933 imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
934 cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700935
936 /* Check if FIFO is empty */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700937 if (cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_RXEMPTY)
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700938 c = NO_POLL_CHAR;
939 else /* Read a character */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700940 c = (unsigned char) cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700941
942 /* Enable interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700943 cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700944
945 return c;
946}
947
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700948static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700949{
950 u32 imr;
951
952 /* Disable all interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700953 imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
954 cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700955
956 /* Wait until FIFO is empty */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700957 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700958 cpu_relax();
959
960 /* Write a character */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700961 cdns_uart_writel(c, CDNS_UART_FIFO_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700962
963 /* Wait until FIFO is empty */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700964 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700965 cpu_relax();
966
967 /* Enable interrupts */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700968 cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700969
970 return;
971}
972#endif
973
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700974static struct uart_ops cdns_uart_ops = {
975 .set_mctrl = cdns_uart_set_mctrl,
976 .get_mctrl = cdns_uart_get_mctrl,
977 .enable_ms = cdns_uart_enable_ms,
978 .start_tx = cdns_uart_start_tx,
979 .stop_tx = cdns_uart_stop_tx,
980 .stop_rx = cdns_uart_stop_rx,
981 .tx_empty = cdns_uart_tx_empty,
982 .break_ctl = cdns_uart_break_ctl,
983 .set_termios = cdns_uart_set_termios,
984 .startup = cdns_uart_startup,
985 .shutdown = cdns_uart_shutdown,
986 .type = cdns_uart_type,
987 .verify_port = cdns_uart_verify_port,
988 .request_port = cdns_uart_request_port,
989 .release_port = cdns_uart_release_port,
990 .config_port = cdns_uart_config_port,
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700991#ifdef CONFIG_CONSOLE_POLL
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700992 .poll_get_char = cdns_uart_poll_get_char,
993 .poll_put_char = cdns_uart_poll_put_char,
Vlad Lungu6ee04c62013-10-17 14:08:07 -0700994#endif
John Linn61ec9012011-04-30 00:07:43 -0400995};
996
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -0700997static struct uart_port cdns_uart_port[2];
John Linn61ec9012011-04-30 00:07:43 -0400998
999/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001000 * cdns_uart_get_port - Configure the port from platform device resource info
Michal Simek928e9262014-04-04 17:23:38 -07001001 * @id: Port id
1002 *
Michal Simek489810a12014-04-04 17:23:37 -07001003 * Return: a pointer to a uart_port or NULL for failure
1004 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001005static struct uart_port *cdns_uart_get_port(int id)
John Linn61ec9012011-04-30 00:07:43 -04001006{
1007 struct uart_port *port;
John Linn61ec9012011-04-30 00:07:43 -04001008
Michal Simek928e9262014-04-04 17:23:38 -07001009 /* Try the given port id if failed use default method */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001010 if (cdns_uart_port[id].mapbase != 0) {
Michal Simek928e9262014-04-04 17:23:38 -07001011 /* Find the next unused port */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001012 for (id = 0; id < CDNS_UART_NR_PORTS; id++)
1013 if (cdns_uart_port[id].mapbase == 0)
Michal Simek928e9262014-04-04 17:23:38 -07001014 break;
1015 }
John Linn61ec9012011-04-30 00:07:43 -04001016
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001017 if (id >= CDNS_UART_NR_PORTS)
John Linn61ec9012011-04-30 00:07:43 -04001018 return NULL;
1019
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001020 port = &cdns_uart_port[id];
John Linn61ec9012011-04-30 00:07:43 -04001021
1022 /* At this point, we've got an empty uart_port struct, initialize it */
1023 spin_lock_init(&port->lock);
1024 port->membase = NULL;
1025 port->iobase = 1; /* mark port in use */
1026 port->irq = 0;
1027 port->type = PORT_UNKNOWN;
1028 port->iotype = UPIO_MEM32;
1029 port->flags = UPF_BOOT_AUTOCONF;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001030 port->ops = &cdns_uart_ops;
1031 port->fifosize = CDNS_UART_FIFO_SIZE;
John Linn61ec9012011-04-30 00:07:43 -04001032 port->line = id;
1033 port->dev = NULL;
1034 return port;
1035}
1036
John Linn61ec9012011-04-30 00:07:43 -04001037#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1038/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001039 * cdns_uart_console_wait_tx - Wait for the TX to be full
John Linn61ec9012011-04-30 00:07:43 -04001040 * @port: Handle to the uart port structure
Michal Simek489810a12014-04-04 17:23:37 -07001041 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001042static void cdns_uart_console_wait_tx(struct uart_port *port)
John Linn61ec9012011-04-30 00:07:43 -04001043{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001044 while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY)
1045 != CDNS_UART_SR_TXEMPTY)
John Linn61ec9012011-04-30 00:07:43 -04001046 barrier();
1047}
1048
1049/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001050 * cdns_uart_console_putchar - write the character to the FIFO buffer
John Linn61ec9012011-04-30 00:07:43 -04001051 * @port: Handle to the uart port structure
1052 * @ch: Character to be written
Michal Simek489810a12014-04-04 17:23:37 -07001053 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001054static void cdns_uart_console_putchar(struct uart_port *port, int ch)
John Linn61ec9012011-04-30 00:07:43 -04001055{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001056 cdns_uart_console_wait_tx(port);
1057 cdns_uart_writel(ch, CDNS_UART_FIFO_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -04001058}
1059
1060/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001061 * cdns_uart_console_write - perform write operation
Michal Simek489810a12014-04-04 17:23:37 -07001062 * @co: Console handle
John Linn61ec9012011-04-30 00:07:43 -04001063 * @s: Pointer to character array
1064 * @count: No of characters
Michal Simek489810a12014-04-04 17:23:37 -07001065 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001066static void cdns_uart_console_write(struct console *co, const char *s,
John Linn61ec9012011-04-30 00:07:43 -04001067 unsigned int count)
1068{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001069 struct uart_port *port = &cdns_uart_port[co->index];
John Linn61ec9012011-04-30 00:07:43 -04001070 unsigned long flags;
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001071 unsigned int imr, ctrl;
John Linn61ec9012011-04-30 00:07:43 -04001072 int locked = 1;
1073
1074 if (oops_in_progress)
1075 locked = spin_trylock_irqsave(&port->lock, flags);
1076 else
1077 spin_lock_irqsave(&port->lock, flags);
1078
1079 /* save and disable interrupt */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001080 imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
1081 cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -04001082
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001083 /*
1084 * Make sure that the tx part is enabled. Set the TX enable bit and
1085 * clear the TX disable bit to enable the transmitter.
1086 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001087 ctrl = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1088 cdns_uart_writel((ctrl & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
1089 CDNS_UART_CR_OFFSET);
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001090
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001091 uart_console_write(port, s, count, cdns_uart_console_putchar);
1092 cdns_uart_console_wait_tx(port);
John Linn61ec9012011-04-30 00:07:43 -04001093
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001094 cdns_uart_writel(ctrl, CDNS_UART_CR_OFFSET);
Lars-Peter Clausend3755f52013-10-17 14:08:09 -07001095
Soren Brinkmannb494a5f2014-04-04 17:23:42 -07001096 /* restore interrupt state */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001097 cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
John Linn61ec9012011-04-30 00:07:43 -04001098
1099 if (locked)
1100 spin_unlock_irqrestore(&port->lock, flags);
1101}
1102
1103/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001104 * cdns_uart_console_setup - Initialize the uart to default config
John Linn61ec9012011-04-30 00:07:43 -04001105 * @co: Console handle
1106 * @options: Initial settings of uart
1107 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001108 * Return: 0 on success, negative errno otherwise.
Michal Simek489810a12014-04-04 17:23:37 -07001109 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001110static int __init cdns_uart_console_setup(struct console *co, char *options)
John Linn61ec9012011-04-30 00:07:43 -04001111{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001112 struct uart_port *port = &cdns_uart_port[co->index];
John Linn61ec9012011-04-30 00:07:43 -04001113 int baud = 9600;
1114 int bits = 8;
1115 int parity = 'n';
1116 int flow = 'n';
1117
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001118 if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
John Linn61ec9012011-04-30 00:07:43 -04001119 return -EINVAL;
1120
1121 if (!port->mapbase) {
1122 pr_debug("console on ttyPS%i not present\n", co->index);
1123 return -ENODEV;
1124 }
1125
1126 if (options)
1127 uart_parse_options(options, &baud, &parity, &bits, &flow);
1128
1129 return uart_set_options(port, co, baud, parity, bits, flow);
1130}
1131
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001132static struct uart_driver cdns_uart_uart_driver;
John Linn61ec9012011-04-30 00:07:43 -04001133
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001134static struct console cdns_uart_console = {
1135 .name = CDNS_UART_TTY_NAME,
1136 .write = cdns_uart_console_write,
John Linn61ec9012011-04-30 00:07:43 -04001137 .device = uart_console_device,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001138 .setup = cdns_uart_console_setup,
John Linn61ec9012011-04-30 00:07:43 -04001139 .flags = CON_PRINTBUFFER,
1140 .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001141 .data = &cdns_uart_uart_driver,
John Linn61ec9012011-04-30 00:07:43 -04001142};
1143
1144/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001145 * cdns_uart_console_init - Initialization call
John Linn61ec9012011-04-30 00:07:43 -04001146 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001147 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001148 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001149static int __init cdns_uart_console_init(void)
John Linn61ec9012011-04-30 00:07:43 -04001150{
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001151 register_console(&cdns_uart_console);
John Linn61ec9012011-04-30 00:07:43 -04001152 return 0;
1153}
1154
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001155console_initcall(cdns_uart_console_init);
John Linn61ec9012011-04-30 00:07:43 -04001156
1157#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
1158
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001159static struct uart_driver cdns_uart_uart_driver = {
Soren Brinkmanne555a212014-04-04 17:23:39 -07001160 .owner = THIS_MODULE,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001161 .driver_name = CDNS_UART_NAME,
1162 .dev_name = CDNS_UART_TTY_NAME,
1163 .major = CDNS_UART_MAJOR,
1164 .minor = CDNS_UART_MINOR,
1165 .nr = CDNS_UART_NR_PORTS,
Soren Brinkmannd3641f62013-10-21 16:41:00 -07001166#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001167 .cons = &cdns_uart_console,
Soren Brinkmannd3641f62013-10-21 16:41:00 -07001168#endif
1169};
1170
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001171#ifdef CONFIG_PM_SLEEP
1172/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001173 * cdns_uart_suspend - suspend event
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001174 * @device: Pointer to the device structure
1175 *
Michal Simek489810a12014-04-04 17:23:37 -07001176 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001177 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001178static int cdns_uart_suspend(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001179{
1180 struct uart_port *port = dev_get_drvdata(device);
1181 struct tty_struct *tty;
1182 struct device *tty_dev;
1183 int may_wake = 0;
1184
1185 /* Get the tty which could be NULL so don't assume it's valid */
1186 tty = tty_port_tty_get(&port->state->port);
1187 if (tty) {
1188 tty_dev = tty->dev;
1189 may_wake = device_may_wakeup(tty_dev);
1190 tty_kref_put(tty);
1191 }
1192
1193 /*
1194 * Call the API provided in serial_core.c file which handles
1195 * the suspend.
1196 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001197 uart_suspend_port(&cdns_uart_uart_driver, port);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001198 if (console_suspend_enabled && !may_wake) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001199 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001200
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001201 clk_disable(cdns_uart->uartclk);
1202 clk_disable(cdns_uart->pclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001203 } else {
1204 unsigned long flags = 0;
1205
1206 spin_lock_irqsave(&port->lock, flags);
1207 /* Empty the receive FIFO 1st before making changes */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001208 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
1209 CDNS_UART_SR_RXEMPTY))
1210 cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001211 /* set RX trigger level to 1 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001212 cdns_uart_writel(1, CDNS_UART_RXWM_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001213 /* disable RX timeout interrups */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001214 cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IDR_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001215 spin_unlock_irqrestore(&port->lock, flags);
1216 }
1217
1218 return 0;
1219}
1220
1221/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001222 * cdns_uart_resume - Resume after a previous suspend
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001223 * @device: Pointer to the device structure
1224 *
Michal Simek489810a12014-04-04 17:23:37 -07001225 * Return: 0
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001226 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001227static int cdns_uart_resume(struct device *device)
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001228{
1229 struct uart_port *port = dev_get_drvdata(device);
1230 unsigned long flags = 0;
1231 u32 ctrl_reg;
1232 struct tty_struct *tty;
1233 struct device *tty_dev;
1234 int may_wake = 0;
1235
1236 /* Get the tty which could be NULL so don't assume it's valid */
1237 tty = tty_port_tty_get(&port->state->port);
1238 if (tty) {
1239 tty_dev = tty->dev;
1240 may_wake = device_may_wakeup(tty_dev);
1241 tty_kref_put(tty);
1242 }
1243
1244 if (console_suspend_enabled && !may_wake) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001245 struct cdns_uart *cdns_uart = port->private_data;
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001246
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001247 clk_enable(cdns_uart->pclk);
1248 clk_enable(cdns_uart->uartclk);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001249
1250 spin_lock_irqsave(&port->lock, flags);
1251
1252 /* Set TX/RX Reset */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001253 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1254 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
1255 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
1256 while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
1257 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001258 cpu_relax();
1259
1260 /* restore rx timeout value */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001261 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001262 /* Enable Tx/Rx */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001263 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1264 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1265 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
1266 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001267
1268 spin_unlock_irqrestore(&port->lock, flags);
1269 } else {
1270 spin_lock_irqsave(&port->lock, flags);
1271 /* restore original rx trigger level */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001272 cdns_uart_writel(rx_trigger_level, CDNS_UART_RXWM_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001273 /* enable RX timeout interrupt */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001274 cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IER_OFFSET);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001275 spin_unlock_irqrestore(&port->lock, flags);
1276 }
1277
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001278 return uart_resume_port(&cdns_uart_uart_driver, port);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001279}
1280#endif /* ! CONFIG_PM_SLEEP */
1281
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001282static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
1283 cdns_uart_resume);
Soren Brinkmann4b47d9a2013-10-17 14:08:12 -07001284
John Linn61ec9012011-04-30 00:07:43 -04001285/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001286 * cdns_uart_probe - Platform driver probe
John Linn61ec9012011-04-30 00:07:43 -04001287 * @pdev: Pointer to the platform device structure
1288 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001289 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001290 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001291static int cdns_uart_probe(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -04001292{
Michal Simek928e9262014-04-04 17:23:38 -07001293 int rc, id;
John Linn61ec9012011-04-30 00:07:43 -04001294 struct uart_port *port;
1295 struct resource *res, *res2;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001296 struct cdns_uart *cdns_uart_data;
John Linn61ec9012011-04-30 00:07:43 -04001297
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001298 cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
Soren Brinkmannc03cae12013-10-17 14:08:05 -07001299 GFP_KERNEL);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001300 if (!cdns_uart_data)
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001301 return -ENOMEM;
1302
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001303 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
1304 if (IS_ERR(cdns_uart_data->pclk)) {
1305 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1306 if (!IS_ERR(cdns_uart_data->pclk))
1307 dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001308 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001309 if (IS_ERR(cdns_uart_data->pclk)) {
1310 dev_err(&pdev->dev, "pclk clock not found.\n");
1311 return PTR_ERR(cdns_uart_data->pclk);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001312 }
1313
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001314 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1315 if (IS_ERR(cdns_uart_data->uartclk)) {
1316 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1317 if (!IS_ERR(cdns_uart_data->uartclk))
1318 dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1319 }
1320 if (IS_ERR(cdns_uart_data->uartclk)) {
1321 dev_err(&pdev->dev, "uart_clk clock not found.\n");
1322 return PTR_ERR(cdns_uart_data->uartclk);
1323 }
1324
1325 rc = clk_prepare_enable(cdns_uart_data->pclk);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001326 if (rc) {
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001327 dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
Soren Brinkmannc03cae12013-10-17 14:08:05 -07001328 return rc;
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001329 }
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001330 rc = clk_prepare_enable(cdns_uart_data->uartclk);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001331 if (rc) {
1332 dev_err(&pdev->dev, "Unable to enable device clock.\n");
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001333 goto err_out_clk_dis_pclk;
John Linn61ec9012011-04-30 00:07:43 -04001334 }
1335
1336 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001337 if (!res) {
1338 rc = -ENODEV;
1339 goto err_out_clk_disable;
1340 }
John Linn61ec9012011-04-30 00:07:43 -04001341
1342 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001343 if (!res2) {
1344 rc = -ENODEV;
1345 goto err_out_clk_disable;
1346 }
John Linn61ec9012011-04-30 00:07:43 -04001347
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001348#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001349 cdns_uart_data->clk_rate_change_nb.notifier_call =
1350 cdns_uart_clk_notifier_cb;
1351 if (clk_notifier_register(cdns_uart_data->uartclk,
1352 &cdns_uart_data->clk_rate_change_nb))
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001353 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001354#endif
Michal Simek928e9262014-04-04 17:23:38 -07001355 /* Look for a serialN alias */
1356 id = of_alias_get_id(pdev->dev.of_node, "serial");
1357 if (id < 0)
1358 id = 0;
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001359
John Linn61ec9012011-04-30 00:07:43 -04001360 /* Initialize the port structure */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001361 port = cdns_uart_get_port(id);
John Linn61ec9012011-04-30 00:07:43 -04001362
1363 if (!port) {
1364 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001365 rc = -ENODEV;
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001366 goto err_out_notif_unreg;
John Linn61ec9012011-04-30 00:07:43 -04001367 } else {
1368 /* Register the port.
1369 * This function also registers this device with the tty layer
1370 * and triggers invocation of the config_port() entry point.
1371 */
1372 port->mapbase = res->start;
1373 port->irq = res2->start;
1374 port->dev = &pdev->dev;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001375 port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1376 port->private_data = cdns_uart_data;
1377 cdns_uart_data->port = port;
Jingoo Han696faed2013-05-23 19:39:36 +09001378 platform_set_drvdata(pdev, port);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001379 rc = uart_add_one_port(&cdns_uart_uart_driver, port);
John Linn61ec9012011-04-30 00:07:43 -04001380 if (rc) {
1381 dev_err(&pdev->dev,
1382 "uart_add_one_port() failed; err=%i\n", rc);
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001383 goto err_out_notif_unreg;
John Linn61ec9012011-04-30 00:07:43 -04001384 }
1385 return 0;
1386 }
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001387
Soren Brinkmannc4b05102013-10-17 14:08:11 -07001388err_out_notif_unreg:
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001389#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001390 clk_notifier_unregister(cdns_uart_data->uartclk,
1391 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001392#endif
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001393err_out_clk_disable:
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001394 clk_disable_unprepare(cdns_uart_data->uartclk);
1395err_out_clk_dis_pclk:
1396 clk_disable_unprepare(cdns_uart_data->pclk);
Soren Brinkmann30e1e282013-05-13 10:46:38 -07001397
1398 return rc;
John Linn61ec9012011-04-30 00:07:43 -04001399}
1400
1401/**
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001402 * cdns_uart_remove - called when the platform driver is unregistered
John Linn61ec9012011-04-30 00:07:43 -04001403 * @pdev: Pointer to the platform device structure
1404 *
Soren Brinkmanne555a212014-04-04 17:23:39 -07001405 * Return: 0 on success, negative errno otherwise
Michal Simek489810a12014-04-04 17:23:37 -07001406 */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001407static int cdns_uart_remove(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -04001408{
Jingoo Han696faed2013-05-23 19:39:36 +09001409 struct uart_port *port = platform_get_drvdata(pdev);
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001410 struct cdns_uart *cdns_uart_data = port->private_data;
Josh Cartwright2326669c2013-01-21 19:57:41 +01001411 int rc;
John Linn61ec9012011-04-30 00:07:43 -04001412
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001413 /* Remove the cdns_uart port from the serial core */
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001414#ifdef CONFIG_COMMON_CLK
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001415 clk_notifier_unregister(cdns_uart_data->uartclk,
1416 &cdns_uart_data->clk_rate_change_nb);
Soren Brinkmann7ac57342013-10-21 16:41:01 -07001417#endif
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001418 rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
Josh Cartwright2326669c2013-01-21 19:57:41 +01001419 port->mapbase = 0;
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001420 clk_disable_unprepare(cdns_uart_data->uartclk);
1421 clk_disable_unprepare(cdns_uart_data->pclk);
John Linn61ec9012011-04-30 00:07:43 -04001422 return rc;
1423}
1424
John Linn61ec9012011-04-30 00:07:43 -04001425/* Match table for of_platform binding */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001426static struct of_device_id cdns_uart_of_match[] = {
John Linn61ec9012011-04-30 00:07:43 -04001427 { .compatible = "xlnx,xuartps", },
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001428 { .compatible = "cdns,uart-r1p8", },
John Linn61ec9012011-04-30 00:07:43 -04001429 {}
1430};
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001431MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
John Linn61ec9012011-04-30 00:07:43 -04001432
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001433static struct platform_driver cdns_uart_platform_driver = {
1434 .probe = cdns_uart_probe,
1435 .remove = cdns_uart_remove,
John Linn61ec9012011-04-30 00:07:43 -04001436 .driver = {
1437 .owner = THIS_MODULE,
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001438 .name = CDNS_UART_NAME,
1439 .of_match_table = cdns_uart_of_match,
1440 .pm = &cdns_uart_dev_pm_ops,
John Linn61ec9012011-04-30 00:07:43 -04001441 },
1442};
1443
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001444static int __init cdns_uart_init(void)
John Linn61ec9012011-04-30 00:07:43 -04001445{
1446 int retval = 0;
1447
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001448 /* Register the cdns_uart driver with the serial core */
1449 retval = uart_register_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001450 if (retval)
1451 return retval;
1452
1453 /* Register the platform driver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001454 retval = platform_driver_register(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001455 if (retval)
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001456 uart_unregister_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001457
1458 return retval;
1459}
1460
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001461static void __exit cdns_uart_exit(void)
John Linn61ec9012011-04-30 00:07:43 -04001462{
John Linn61ec9012011-04-30 00:07:43 -04001463 /* Unregister the platform driver */
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001464 platform_driver_unregister(&cdns_uart_platform_driver);
John Linn61ec9012011-04-30 00:07:43 -04001465
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001466 /* Unregister the cdns_uart driver */
1467 uart_unregister_driver(&cdns_uart_uart_driver);
John Linn61ec9012011-04-30 00:07:43 -04001468}
1469
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001470module_init(cdns_uart_init);
1471module_exit(cdns_uart_exit);
John Linn61ec9012011-04-30 00:07:43 -04001472
Soren Brinkmannd9bb3fb2014-04-04 17:23:43 -07001473MODULE_DESCRIPTION("Driver for Cadence UART");
John Linn61ec9012011-04-30 00:07:43 -04001474MODULE_AUTHOR("Xilinx Inc.");
1475MODULE_LICENSE("GPL");