GNU Linux-libre 4.9.292-gnu1
[releases.git] / drivers / tty / serial / xilinx_uartps.c
1 /*
2  * Cadence UART driver (found in Xilinx Zynq)
3  *
4  * 2011 - 2014 (C) Xilinx Inc.
5  *
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.
11  *
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.
15  */
16
17 #if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18 #define SUPPORT_SYSRQ
19 #endif
20
21 #include <linux/platform_device.h>
22 #include <linux/serial.h>
23 #include <linux/console.h>
24 #include <linux/serial_core.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/clk.h>
29 #include <linux/irq.h>
30 #include <linux/io.h>
31 #include <linux/of.h>
32 #include <linux/module.h>
33
34 #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        0x1000
41
42 /* Rx Trigger level */
43 static int rx_trigger_level = 56;
44 module_param(rx_trigger_level, uint, S_IRUGO);
45 MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
46
47 /* Rx Timeout */
48 static int rx_timeout = 10;
49 module_param(rx_timeout, uint, S_IRUGO);
50 MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
51
52 /* Register offsets for the UART. */
53 #define CDNS_UART_CR            0x00  /* Control Register */
54 #define CDNS_UART_MR            0x04  /* Mode Register */
55 #define CDNS_UART_IER           0x08  /* Interrupt Enable */
56 #define CDNS_UART_IDR           0x0C  /* Interrupt Disable */
57 #define CDNS_UART_IMR           0x10  /* Interrupt Mask */
58 #define CDNS_UART_ISR           0x14  /* Interrupt Status */
59 #define CDNS_UART_BAUDGEN       0x18  /* Baud Rate Generator */
60 #define CDNS_UART_RXTOUT        0x1C  /* RX Timeout */
61 #define CDNS_UART_RXWM          0x20  /* RX FIFO Trigger Level */
62 #define CDNS_UART_MODEMCR       0x24  /* Modem Control */
63 #define CDNS_UART_MODEMSR       0x28  /* Modem Status */
64 #define CDNS_UART_SR            0x2C  /* Channel Status */
65 #define CDNS_UART_FIFO          0x30  /* FIFO */
66 #define CDNS_UART_BAUDDIV       0x34  /* Baud Rate Divider */
67 #define CDNS_UART_FLOWDEL       0x38  /* Flow Delay */
68 #define CDNS_UART_IRRX_PWIDTH   0x3C  /* IR Min Received Pulse Width */
69 #define CDNS_UART_IRTX_PWIDTH   0x40  /* IR Transmitted pulse Width */
70 #define CDNS_UART_TXWM          0x44  /* TX FIFO Trigger Level */
71 #define CDNS_UART_RXBS          0x48  /* RX FIFO byte status register */
72
73 /* Control Register Bit Definitions */
74 #define CDNS_UART_CR_STOPBRK    0x00000100  /* Stop TX break */
75 #define CDNS_UART_CR_STARTBRK   0x00000080  /* Set TX break */
76 #define CDNS_UART_CR_TX_DIS     0x00000020  /* TX disabled. */
77 #define CDNS_UART_CR_TX_EN      0x00000010  /* TX enabled */
78 #define CDNS_UART_CR_RX_DIS     0x00000008  /* RX disabled. */
79 #define CDNS_UART_CR_RX_EN      0x00000004  /* RX enabled */
80 #define CDNS_UART_CR_TXRST      0x00000002  /* TX logic reset */
81 #define CDNS_UART_CR_RXRST      0x00000001  /* RX logic reset */
82 #define CDNS_UART_CR_RST_TO     0x00000040  /* Restart Timeout Counter */
83 #define CDNS_UART_RXBS_PARITY    0x00000001 /* Parity error status */
84 #define CDNS_UART_RXBS_FRAMING   0x00000002 /* Framing error status */
85 #define CDNS_UART_RXBS_BRK       0x00000004 /* Overrun error status */
86
87 /*
88  * Mode Register:
89  * The mode register (MR) defines the mode of transfer as well as the data
90  * format. If this register is modified during transmission or reception,
91  * data validity cannot be guaranteed.
92  */
93 #define CDNS_UART_MR_CLKSEL             0x00000001  /* Pre-scalar selection */
94 #define CDNS_UART_MR_CHMODE_L_LOOP      0x00000200  /* Local loop back mode */
95 #define CDNS_UART_MR_CHMODE_NORM        0x00000000  /* Normal mode */
96
97 #define CDNS_UART_MR_STOPMODE_2_BIT     0x00000080  /* 2 stop bits */
98 #define CDNS_UART_MR_STOPMODE_1_BIT     0x00000000  /* 1 stop bit */
99
100 #define CDNS_UART_MR_PARITY_NONE        0x00000020  /* No parity mode */
101 #define CDNS_UART_MR_PARITY_MARK        0x00000018  /* Mark parity mode */
102 #define CDNS_UART_MR_PARITY_SPACE       0x00000010  /* Space parity mode */
103 #define CDNS_UART_MR_PARITY_ODD         0x00000008  /* Odd parity mode */
104 #define CDNS_UART_MR_PARITY_EVEN        0x00000000  /* Even parity mode */
105
106 #define CDNS_UART_MR_CHARLEN_6_BIT      0x00000006  /* 6 bits data */
107 #define CDNS_UART_MR_CHARLEN_7_BIT      0x00000004  /* 7 bits data */
108 #define CDNS_UART_MR_CHARLEN_8_BIT      0x00000000  /* 8 bits data */
109
110 /*
111  * Interrupt Registers:
112  * Interrupt control logic uses the interrupt enable register (IER) and the
113  * interrupt disable register (IDR) to set the value of the bits in the
114  * interrupt mask register (IMR). The IMR determines whether to pass an
115  * interrupt to the interrupt status register (ISR).
116  * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
117  * interrupt. IMR and ISR are read only, and IER and IDR are write only.
118  * Reading either IER or IDR returns 0x00.
119  * All four registers have the same bit definitions.
120  */
121 #define CDNS_UART_IXR_TOUT      0x00000100 /* RX Timeout error interrupt */
122 #define CDNS_UART_IXR_PARITY    0x00000080 /* Parity error interrupt */
123 #define CDNS_UART_IXR_FRAMING   0x00000040 /* Framing error interrupt */
124 #define CDNS_UART_IXR_OVERRUN   0x00000020 /* Overrun error interrupt */
125 #define CDNS_UART_IXR_TXFULL    0x00000010 /* TX FIFO Full interrupt */
126 #define CDNS_UART_IXR_TXEMPTY   0x00000008 /* TX FIFO empty interrupt */
127 #define CDNS_UART_ISR_RXEMPTY   0x00000002 /* RX FIFO empty interrupt */
128 #define CDNS_UART_IXR_RXTRIG    0x00000001 /* RX FIFO trigger interrupt */
129 #define CDNS_UART_IXR_RXFULL    0x00000004 /* RX FIFO full interrupt. */
130 #define CDNS_UART_IXR_RXEMPTY   0x00000002 /* RX FIFO empty interrupt. */
131 #define CDNS_UART_IXR_RXMASK    0x000021e7 /* Valid RX bit mask */
132
133         /*
134          * Do not enable parity error interrupt for the following
135          * reason: When parity error interrupt is enabled, each Rx
136          * parity error always results in 2 events. The first one
137          * being parity error interrupt and the second one with a
138          * proper Rx interrupt with the incoming data.  Disabling
139          * parity error interrupt ensures better handling of parity
140          * error events. With this change, for a parity error case, we
141          * get a Rx interrupt with parity error set in ISR register
142          * and we still handle parity errors in the desired way.
143          */
144
145 #define CDNS_UART_RX_IRQS       (CDNS_UART_IXR_FRAMING | \
146                                  CDNS_UART_IXR_OVERRUN | \
147                                  CDNS_UART_IXR_RXTRIG |  \
148                                  CDNS_UART_IXR_TOUT)
149
150 /* Goes in read_status_mask for break detection as the HW doesn't do it*/
151 #define CDNS_UART_IXR_BRK       0x00002000
152
153 #define CDNS_UART_RXBS_SUPPORT BIT(1)
154 /*
155  * Modem Control register:
156  * The read/write Modem Control register controls the interface with the modem
157  * or data set, or a peripheral device emulating a modem.
158  */
159 #define CDNS_UART_MODEMCR_FCM   0x00000020 /* Automatic flow control mode */
160 #define CDNS_UART_MODEMCR_RTS   0x00000002 /* Request to send output control */
161 #define CDNS_UART_MODEMCR_DTR   0x00000001 /* Data Terminal Ready */
162
163 /*
164  * Channel Status Register:
165  * The channel status register (CSR) is provided to enable the control logic
166  * to monitor the status of bits in the channel interrupt status register,
167  * even if these are masked out by the interrupt mask register.
168  */
169 #define CDNS_UART_SR_RXEMPTY    0x00000002 /* RX FIFO empty */
170 #define CDNS_UART_SR_TXEMPTY    0x00000008 /* TX FIFO empty */
171 #define CDNS_UART_SR_TXFULL     0x00000010 /* TX FIFO full */
172 #define CDNS_UART_SR_RXTRIG     0x00000001 /* Rx Trigger */
173
174 /* baud dividers min/max values */
175 #define CDNS_UART_BDIV_MIN      4
176 #define CDNS_UART_BDIV_MAX      255
177 #define CDNS_UART_CD_MAX        65535
178
179 /**
180  * struct cdns_uart - device data
181  * @port:               Pointer to the UART port
182  * @uartclk:            Reference clock
183  * @pclk:               APB clock
184  * @baud:               Current baud rate
185  * @clk_rate_change_nb: Notifier block for clock changes
186  */
187 struct cdns_uart {
188         struct uart_port        *port;
189         struct clk              *uartclk;
190         struct clk              *pclk;
191         unsigned int            baud;
192         struct notifier_block   clk_rate_change_nb;
193         u32                     quirks;
194 };
195 struct cdns_platform_data {
196         u32 quirks;
197 };
198 #define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
199                 clk_rate_change_nb);
200
201 /**
202  * cdns_uart_handle_rx - Handle the received bytes along with Rx errors.
203  * @dev_id: Id of the UART port
204  * @isrstatus: The interrupt status register value as read
205  * Return: None
206  */
207 static void cdns_uart_handle_rx(void *dev_id, unsigned int isrstatus)
208 {
209         struct uart_port *port = (struct uart_port *)dev_id;
210         struct cdns_uart *cdns_uart = port->private_data;
211         unsigned int data;
212         unsigned int rxbs_status = 0;
213         unsigned int status_mask;
214         unsigned int framerrprocessed = 0;
215         char status = TTY_NORMAL;
216         bool is_rxbs_support;
217
218         is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
219
220         while ((readl(port->membase + CDNS_UART_SR) &
221                 CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
222                 if (is_rxbs_support)
223                         rxbs_status = readl(port->membase + CDNS_UART_RXBS);
224                 data = readl(port->membase + CDNS_UART_FIFO);
225                 port->icount.rx++;
226                 /*
227                  * There is no hardware break detection in Zynq, so we interpret
228                  * framing error with all-zeros data as a break sequence.
229                  * Most of the time, there's another non-zero byte at the
230                  * end of the sequence.
231                  */
232                 if (!is_rxbs_support && (isrstatus & CDNS_UART_IXR_FRAMING)) {
233                         if (!data) {
234                                 port->read_status_mask |= CDNS_UART_IXR_BRK;
235                                 framerrprocessed = 1;
236                                 continue;
237                         }
238                 }
239                 if (is_rxbs_support && (rxbs_status & CDNS_UART_RXBS_BRK)) {
240                         port->icount.brk++;
241                         status = TTY_BREAK;
242                         if (uart_handle_break(port))
243                                 continue;
244                 }
245
246                 isrstatus &= port->read_status_mask;
247                 isrstatus &= ~port->ignore_status_mask;
248                 status_mask = port->read_status_mask;
249                 status_mask &= ~port->ignore_status_mask;
250
251                 if (data &&
252                     (port->read_status_mask & CDNS_UART_IXR_BRK)) {
253                         port->read_status_mask &= ~CDNS_UART_IXR_BRK;
254                         port->icount.brk++;
255                         if (uart_handle_break(port))
256                                 continue;
257                 }
258
259                 if (uart_handle_sysrq_char(port, data))
260                         continue;
261
262                 if (is_rxbs_support) {
263                         if ((rxbs_status & CDNS_UART_RXBS_PARITY)
264                             && (status_mask & CDNS_UART_IXR_PARITY)) {
265                                 port->icount.parity++;
266                                 status = TTY_PARITY;
267                         }
268                         if ((rxbs_status & CDNS_UART_RXBS_FRAMING)
269                             && (status_mask & CDNS_UART_IXR_PARITY)) {
270                                 port->icount.frame++;
271                                 status = TTY_FRAME;
272                         }
273                 } else {
274                         if (isrstatus & CDNS_UART_IXR_PARITY) {
275                                 port->icount.parity++;
276                                 status = TTY_PARITY;
277                         }
278                         if ((isrstatus & CDNS_UART_IXR_FRAMING) &&
279                             !framerrprocessed) {
280                                 port->icount.frame++;
281                                 status = TTY_FRAME;
282                         }
283                 }
284                 if (isrstatus & CDNS_UART_IXR_OVERRUN) {
285                         port->icount.overrun++;
286                         tty_insert_flip_char(&port->state->port, 0,
287                                              TTY_OVERRUN);
288                 }
289                 tty_insert_flip_char(&port->state->port, data, status);
290                 isrstatus = 0;
291         }
292         spin_unlock(&port->lock);
293         tty_flip_buffer_push(&port->state->port);
294         spin_lock(&port->lock);
295 }
296
297 /**
298  * cdns_uart_handle_tx - Handle the bytes to be Txed.
299  * @dev_id: Id of the UART port
300  * Return: None
301  */
302 static void cdns_uart_handle_tx(void *dev_id)
303 {
304         struct uart_port *port = (struct uart_port *)dev_id;
305         unsigned int numbytes;
306
307         if (uart_circ_empty(&port->state->xmit)) {
308                 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IDR);
309         } else {
310                 numbytes = port->fifosize;
311                 while (numbytes && !uart_circ_empty(&port->state->xmit) &&
312                        !(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)) {
313                         /*
314                          * Get the data from the UART circular buffer
315                          * and write it to the cdns_uart's TX_FIFO
316                          * register.
317                          */
318                         writel(
319                                 port->state->xmit.buf[port->state->xmit.
320                                 tail], port->membase + CDNS_UART_FIFO);
321
322                         port->icount.tx++;
323
324                         /*
325                          * Adjust the tail of the UART buffer and wrap
326                          * the buffer if it reaches limit.
327                          */
328                         port->state->xmit.tail =
329                                 (port->state->xmit.tail + 1) &
330                                         (UART_XMIT_SIZE - 1);
331
332                         numbytes--;
333                 }
334
335                 if (uart_circ_chars_pending(
336                                 &port->state->xmit) < WAKEUP_CHARS)
337                         uart_write_wakeup(port);
338         }
339 }
340
341 /**
342  * cdns_uart_isr - Interrupt handler
343  * @irq: Irq number
344  * @dev_id: Id of the port
345  *
346  * Return: IRQHANDLED
347  */
348 static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
349 {
350         struct uart_port *port = (struct uart_port *)dev_id;
351         unsigned int isrstatus;
352
353         spin_lock(&port->lock);
354
355         /* Read the interrupt status register to determine which
356          * interrupt(s) is/are active and clear them.
357          */
358         isrstatus = readl(port->membase + CDNS_UART_ISR);
359         writel(isrstatus, port->membase + CDNS_UART_ISR);
360
361         if (isrstatus & CDNS_UART_IXR_TXEMPTY) {
362                 cdns_uart_handle_tx(dev_id);
363                 isrstatus &= ~CDNS_UART_IXR_TXEMPTY;
364         }
365
366         /*
367          * Skip RX processing if RX is disabled as RXEMPTY will never be set
368          * as read bytes will not be removed from the FIFO.
369          */
370         if (isrstatus & CDNS_UART_IXR_RXMASK &&
371             !(readl(port->membase + CDNS_UART_CR) & CDNS_UART_CR_RX_DIS))
372                 cdns_uart_handle_rx(dev_id, isrstatus);
373
374         spin_unlock(&port->lock);
375         return IRQ_HANDLED;
376 }
377
378 /**
379  * cdns_uart_calc_baud_divs - Calculate baud rate divisors
380  * @clk: UART module input clock
381  * @baud: Desired baud rate
382  * @rbdiv: BDIV value (return value)
383  * @rcd: CD value (return value)
384  * @div8: Value for clk_sel bit in mod (return value)
385  * Return: baud rate, requested baud when possible, or actual baud when there
386  *      was too much error, zero if no valid divisors are found.
387  *
388  * Formula to obtain baud rate is
389  *      baud_tx/rx rate = clk/CD * (BDIV + 1)
390  *      input_clk = (Uart User Defined Clock or Apb Clock)
391  *              depends on UCLKEN in MR Reg
392  *      clk = input_clk or input_clk/8;
393  *              depends on CLKS in MR reg
394  *      CD and BDIV depends on values in
395  *                      baud rate generate register
396  *                      baud rate clock divisor register
397  */
398 static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
399                 unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
400 {
401         u32 cd, bdiv;
402         unsigned int calc_baud;
403         unsigned int bestbaud = 0;
404         unsigned int bauderror;
405         unsigned int besterror = ~0;
406
407         if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
408                 *div8 = 1;
409                 clk /= 8;
410         } else {
411                 *div8 = 0;
412         }
413
414         for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
415                 cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
416                 if (cd < 1 || cd > CDNS_UART_CD_MAX)
417                         continue;
418
419                 calc_baud = clk / (cd * (bdiv + 1));
420
421                 if (baud > calc_baud)
422                         bauderror = baud - calc_baud;
423                 else
424                         bauderror = calc_baud - baud;
425
426                 if (besterror > bauderror) {
427                         *rbdiv = bdiv;
428                         *rcd = cd;
429                         bestbaud = calc_baud;
430                         besterror = bauderror;
431                 }
432         }
433         /* use the values when percent error is acceptable */
434         if (((besterror * 100) / baud) < 3)
435                 bestbaud = baud;
436
437         return bestbaud;
438 }
439
440 /**
441  * cdns_uart_set_baud_rate - Calculate and set the baud rate
442  * @port: Handle to the uart port structure
443  * @baud: Baud rate to set
444  * Return: baud rate, requested baud when possible, or actual baud when there
445  *         was too much error, zero if no valid divisors are found.
446  */
447 static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
448                 unsigned int baud)
449 {
450         unsigned int calc_baud;
451         u32 cd = 0, bdiv = 0;
452         u32 mreg;
453         int div8;
454         struct cdns_uart *cdns_uart = port->private_data;
455
456         calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
457                         &div8);
458
459         /* Write new divisors to hardware */
460         mreg = readl(port->membase + CDNS_UART_MR);
461         if (div8)
462                 mreg |= CDNS_UART_MR_CLKSEL;
463         else
464                 mreg &= ~CDNS_UART_MR_CLKSEL;
465         writel(mreg, port->membase + CDNS_UART_MR);
466         writel(cd, port->membase + CDNS_UART_BAUDGEN);
467         writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
468         cdns_uart->baud = baud;
469
470         return calc_baud;
471 }
472
473 #ifdef CONFIG_COMMON_CLK
474 /**
475  * cdns_uart_clk_notitifer_cb - Clock notifier callback
476  * @nb:         Notifier block
477  * @event:      Notify event
478  * @data:       Notifier data
479  * Return:      NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
480  */
481 static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
482                 unsigned long event, void *data)
483 {
484         u32 ctrl_reg;
485         struct uart_port *port;
486         int locked = 0;
487         struct clk_notifier_data *ndata = data;
488         unsigned long flags = 0;
489         struct cdns_uart *cdns_uart = to_cdns_uart(nb);
490
491         port = cdns_uart->port;
492         if (port->suspended)
493                 return NOTIFY_OK;
494
495         switch (event) {
496         case PRE_RATE_CHANGE:
497         {
498                 u32 bdiv, cd;
499                 int div8;
500
501                 /*
502                  * Find out if current baud-rate can be achieved with new clock
503                  * frequency.
504                  */
505                 if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
506                                         &bdiv, &cd, &div8)) {
507                         dev_warn(port->dev, "clock rate change rejected\n");
508                         return NOTIFY_BAD;
509                 }
510
511                 spin_lock_irqsave(&cdns_uart->port->lock, flags);
512
513                 /* Disable the TX and RX to set baud rate */
514                 ctrl_reg = readl(port->membase + CDNS_UART_CR);
515                 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
516                 writel(ctrl_reg, port->membase + CDNS_UART_CR);
517
518                 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
519
520                 return NOTIFY_OK;
521         }
522         case POST_RATE_CHANGE:
523                 /*
524                  * Set clk dividers to generate correct baud with new clock
525                  * frequency.
526                  */
527
528                 spin_lock_irqsave(&cdns_uart->port->lock, flags);
529
530                 locked = 1;
531                 port->uartclk = ndata->new_rate;
532
533                 cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
534                                 cdns_uart->baud);
535                 /* fall through */
536         case ABORT_RATE_CHANGE:
537                 if (!locked)
538                         spin_lock_irqsave(&cdns_uart->port->lock, flags);
539
540                 /* Set TX/RX Reset */
541                 ctrl_reg = readl(port->membase + CDNS_UART_CR);
542                 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
543                 writel(ctrl_reg, port->membase + CDNS_UART_CR);
544
545                 while (readl(port->membase + CDNS_UART_CR) &
546                                 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
547                         cpu_relax();
548
549                 /*
550                  * Clear the RX disable and TX disable bits and then set the TX
551                  * enable bit and RX enable bit to enable the transmitter and
552                  * receiver.
553                  */
554                 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
555                 ctrl_reg = readl(port->membase + CDNS_UART_CR);
556                 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
557                 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
558                 writel(ctrl_reg, port->membase + CDNS_UART_CR);
559
560                 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
561
562                 return NOTIFY_OK;
563         default:
564                 return NOTIFY_DONE;
565         }
566 }
567 #endif
568
569 /**
570  * cdns_uart_start_tx -  Start transmitting bytes
571  * @port: Handle to the uart port structure
572  */
573 static void cdns_uart_start_tx(struct uart_port *port)
574 {
575         unsigned int status;
576
577         if (uart_tx_stopped(port))
578                 return;
579
580         /*
581          * Set the TX enable bit and clear the TX disable bit to enable the
582          * transmitter.
583          */
584         status = readl(port->membase + CDNS_UART_CR);
585         status &= ~CDNS_UART_CR_TX_DIS;
586         status |= CDNS_UART_CR_TX_EN;
587         writel(status, port->membase + CDNS_UART_CR);
588
589         if (uart_circ_empty(&port->state->xmit))
590                 return;
591
592         writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR);
593
594         cdns_uart_handle_tx(port);
595
596         /* Enable the TX Empty interrupt */
597         writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IER);
598 }
599
600 /**
601  * cdns_uart_stop_tx - Stop TX
602  * @port: Handle to the uart port structure
603  */
604 static void cdns_uart_stop_tx(struct uart_port *port)
605 {
606         unsigned int regval;
607
608         regval = readl(port->membase + CDNS_UART_CR);
609         regval |= CDNS_UART_CR_TX_DIS;
610         /* Disable the transmitter */
611         writel(regval, port->membase + CDNS_UART_CR);
612 }
613
614 /**
615  * cdns_uart_stop_rx - Stop RX
616  * @port: Handle to the uart port structure
617  */
618 static void cdns_uart_stop_rx(struct uart_port *port)
619 {
620         unsigned int regval;
621
622         /* Disable RX IRQs */
623         writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR);
624
625         /* Disable the receiver */
626         regval = readl(port->membase + CDNS_UART_CR);
627         regval |= CDNS_UART_CR_RX_DIS;
628         writel(regval, port->membase + CDNS_UART_CR);
629 }
630
631 /**
632  * cdns_uart_tx_empty -  Check whether TX is empty
633  * @port: Handle to the uart port structure
634  *
635  * Return: TIOCSER_TEMT on success, 0 otherwise
636  */
637 static unsigned int cdns_uart_tx_empty(struct uart_port *port)
638 {
639         unsigned int status;
640
641         status = readl(port->membase + CDNS_UART_SR) &
642                                 CDNS_UART_SR_TXEMPTY;
643         return status ? TIOCSER_TEMT : 0;
644 }
645
646 /**
647  * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
648  *                      transmitting char breaks
649  * @port: Handle to the uart port structure
650  * @ctl: Value based on which start or stop decision is taken
651  */
652 static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
653 {
654         unsigned int status;
655         unsigned long flags;
656
657         spin_lock_irqsave(&port->lock, flags);
658
659         status = readl(port->membase + CDNS_UART_CR);
660
661         if (ctl == -1)
662                 writel(CDNS_UART_CR_STARTBRK | status,
663                                 port->membase + CDNS_UART_CR);
664         else {
665                 if ((status & CDNS_UART_CR_STOPBRK) == 0)
666                         writel(CDNS_UART_CR_STOPBRK | status,
667                                         port->membase + CDNS_UART_CR);
668         }
669         spin_unlock_irqrestore(&port->lock, flags);
670 }
671
672 /**
673  * cdns_uart_set_termios - termios operations, handling data length, parity,
674  *                              stop bits, flow control, baud rate
675  * @port: Handle to the uart port structure
676  * @termios: Handle to the input termios structure
677  * @old: Values of the previously saved termios structure
678  */
679 static void cdns_uart_set_termios(struct uart_port *port,
680                                 struct ktermios *termios, struct ktermios *old)
681 {
682         unsigned int cval = 0;
683         unsigned int baud, minbaud, maxbaud;
684         unsigned long flags;
685         unsigned int ctrl_reg, mode_reg;
686
687         spin_lock_irqsave(&port->lock, flags);
688
689         /* Wait for the transmit FIFO to empty before making changes */
690         if (!(readl(port->membase + CDNS_UART_CR) &
691                                 CDNS_UART_CR_TX_DIS)) {
692                 while (!(readl(port->membase + CDNS_UART_SR) &
693                                 CDNS_UART_SR_TXEMPTY)) {
694                         cpu_relax();
695                 }
696         }
697
698         /* Disable the TX and RX to set baud rate */
699         ctrl_reg = readl(port->membase + CDNS_UART_CR);
700         ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
701         writel(ctrl_reg, port->membase + CDNS_UART_CR);
702
703         /*
704          * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
705          * min and max baud should be calculated here based on port->uartclk.
706          * this way we get a valid baud and can safely call set_baud()
707          */
708         minbaud = port->uartclk /
709                         ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
710         maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
711         baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
712         baud = cdns_uart_set_baud_rate(port, baud);
713         if (tty_termios_baud_rate(termios))
714                 tty_termios_encode_baud_rate(termios, baud, baud);
715
716         /* Update the per-port timeout. */
717         uart_update_timeout(port, termios->c_cflag, baud);
718
719         /* Set TX/RX Reset */
720         ctrl_reg = readl(port->membase + CDNS_UART_CR);
721         ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
722         writel(ctrl_reg, port->membase + CDNS_UART_CR);
723
724         while (readl(port->membase + CDNS_UART_CR) &
725                 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
726                 cpu_relax();
727
728         /*
729          * Clear the RX disable and TX disable bits and then set the TX enable
730          * bit and RX enable bit to enable the transmitter and receiver.
731          */
732         ctrl_reg = readl(port->membase + CDNS_UART_CR);
733         ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
734         ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
735         writel(ctrl_reg, port->membase + CDNS_UART_CR);
736
737         writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
738
739         port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
740                         CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
741         port->ignore_status_mask = 0;
742
743         if (termios->c_iflag & INPCK)
744                 port->read_status_mask |= CDNS_UART_IXR_PARITY |
745                 CDNS_UART_IXR_FRAMING;
746
747         if (termios->c_iflag & IGNPAR)
748                 port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
749                         CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
750
751         /* ignore all characters if CREAD is not set */
752         if ((termios->c_cflag & CREAD) == 0)
753                 port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
754                         CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
755                         CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
756
757         mode_reg = readl(port->membase + CDNS_UART_MR);
758
759         /* Handling Data Size */
760         switch (termios->c_cflag & CSIZE) {
761         case CS6:
762                 cval |= CDNS_UART_MR_CHARLEN_6_BIT;
763                 break;
764         case CS7:
765                 cval |= CDNS_UART_MR_CHARLEN_7_BIT;
766                 break;
767         default:
768         case CS8:
769                 cval |= CDNS_UART_MR_CHARLEN_8_BIT;
770                 termios->c_cflag &= ~CSIZE;
771                 termios->c_cflag |= CS8;
772                 break;
773         }
774
775         /* Handling Parity and Stop Bits length */
776         if (termios->c_cflag & CSTOPB)
777                 cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
778         else
779                 cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
780
781         if (termios->c_cflag & PARENB) {
782                 /* Mark or Space parity */
783                 if (termios->c_cflag & CMSPAR) {
784                         if (termios->c_cflag & PARODD)
785                                 cval |= CDNS_UART_MR_PARITY_MARK;
786                         else
787                                 cval |= CDNS_UART_MR_PARITY_SPACE;
788                 } else {
789                         if (termios->c_cflag & PARODD)
790                                 cval |= CDNS_UART_MR_PARITY_ODD;
791                         else
792                                 cval |= CDNS_UART_MR_PARITY_EVEN;
793                 }
794         } else {
795                 cval |= CDNS_UART_MR_PARITY_NONE;
796         }
797         cval |= mode_reg & 1;
798         writel(cval, port->membase + CDNS_UART_MR);
799
800         spin_unlock_irqrestore(&port->lock, flags);
801 }
802
803 /**
804  * cdns_uart_startup - Called when an application opens a cdns_uart port
805  * @port: Handle to the uart port structure
806  *
807  * Return: 0 on success, negative errno otherwise
808  */
809 static int cdns_uart_startup(struct uart_port *port)
810 {
811         struct cdns_uart *cdns_uart = port->private_data;
812         bool is_brk_support;
813         int ret;
814         unsigned long flags;
815         unsigned int status = 0;
816
817         is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
818
819         spin_lock_irqsave(&port->lock, flags);
820
821         /* Disable the TX and RX */
822         writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
823                         port->membase + CDNS_UART_CR);
824
825         /* Set the Control Register with TX/RX Enable, TX/RX Reset,
826          * no break chars.
827          */
828         writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
829                         port->membase + CDNS_UART_CR);
830
831         while (readl(port->membase + CDNS_UART_CR) &
832                 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
833                 cpu_relax();
834
835         /*
836          * Clear the RX disable bit and then set the RX enable bit to enable
837          * the receiver.
838          */
839         status = readl(port->membase + CDNS_UART_CR);
840         status &= CDNS_UART_CR_RX_DIS;
841         status |= CDNS_UART_CR_RX_EN;
842         writel(status, port->membase + CDNS_UART_CR);
843
844         /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
845          * no parity.
846          */
847         writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
848                 | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
849                 port->membase + CDNS_UART_MR);
850
851         /*
852          * Set the RX FIFO Trigger level to use most of the FIFO, but it
853          * can be tuned with a module parameter
854          */
855         writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
856
857         /*
858          * Receive Timeout register is enabled but it
859          * can be tuned with a module parameter
860          */
861         writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
862
863         /* Clear out any pending interrupts before enabling them */
864         writel(readl(port->membase + CDNS_UART_ISR),
865                         port->membase + CDNS_UART_ISR);
866
867         spin_unlock_irqrestore(&port->lock, flags);
868
869         ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port);
870         if (ret) {
871                 dev_err(port->dev, "request_irq '%d' failed with %d\n",
872                         port->irq, ret);
873                 return ret;
874         }
875
876         /* Set the Interrupt Registers with desired interrupts */
877         if (is_brk_support)
878                 writel(CDNS_UART_RX_IRQS | CDNS_UART_IXR_BRK,
879                                         port->membase + CDNS_UART_IER);
880         else
881                 writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER);
882
883         return 0;
884 }
885
886 /**
887  * cdns_uart_shutdown - Called when an application closes a cdns_uart port
888  * @port: Handle to the uart port structure
889  */
890 static void cdns_uart_shutdown(struct uart_port *port)
891 {
892         int status;
893         unsigned long flags;
894
895         spin_lock_irqsave(&port->lock, flags);
896
897         /* Disable interrupts */
898         status = readl(port->membase + CDNS_UART_IMR);
899         writel(status, port->membase + CDNS_UART_IDR);
900         writel(0xffffffff, port->membase + CDNS_UART_ISR);
901
902         /* Disable the TX and RX */
903         writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
904                         port->membase + CDNS_UART_CR);
905
906         spin_unlock_irqrestore(&port->lock, flags);
907
908         free_irq(port->irq, port);
909 }
910
911 /**
912  * cdns_uart_type - Set UART type to cdns_uart port
913  * @port: Handle to the uart port structure
914  *
915  * Return: string on success, NULL otherwise
916  */
917 static const char *cdns_uart_type(struct uart_port *port)
918 {
919         return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
920 }
921
922 /**
923  * cdns_uart_verify_port - Verify the port params
924  * @port: Handle to the uart port structure
925  * @ser: Handle to the structure whose members are compared
926  *
927  * Return: 0 on success, negative errno otherwise.
928  */
929 static int cdns_uart_verify_port(struct uart_port *port,
930                                         struct serial_struct *ser)
931 {
932         if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
933                 return -EINVAL;
934         if (port->irq != ser->irq)
935                 return -EINVAL;
936         if (ser->io_type != UPIO_MEM)
937                 return -EINVAL;
938         if (port->iobase != ser->port)
939                 return -EINVAL;
940         if (ser->hub6 != 0)
941                 return -EINVAL;
942         return 0;
943 }
944
945 /**
946  * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
947  *                              called when the driver adds a cdns_uart port via
948  *                              uart_add_one_port()
949  * @port: Handle to the uart port structure
950  *
951  * Return: 0 on success, negative errno otherwise.
952  */
953 static int cdns_uart_request_port(struct uart_port *port)
954 {
955         if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
956                                          CDNS_UART_NAME)) {
957                 return -ENOMEM;
958         }
959
960         port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
961         if (!port->membase) {
962                 dev_err(port->dev, "Unable to map registers\n");
963                 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
964                 return -ENOMEM;
965         }
966         return 0;
967 }
968
969 /**
970  * cdns_uart_release_port - Release UART port
971  * @port: Handle to the uart port structure
972  *
973  * Release the memory region attached to a cdns_uart port. Called when the
974  * driver removes a cdns_uart port via uart_remove_one_port().
975  */
976 static void cdns_uart_release_port(struct uart_port *port)
977 {
978         release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
979         iounmap(port->membase);
980         port->membase = NULL;
981 }
982
983 /**
984  * cdns_uart_config_port - Configure UART port
985  * @port: Handle to the uart port structure
986  * @flags: If any
987  */
988 static void cdns_uart_config_port(struct uart_port *port, int flags)
989 {
990         if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
991                 port->type = PORT_XUARTPS;
992 }
993
994 /**
995  * cdns_uart_get_mctrl - Get the modem control state
996  * @port: Handle to the uart port structure
997  *
998  * Return: the modem control state
999  */
1000 static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
1001 {
1002         return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
1003 }
1004
1005 static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1006 {
1007         u32 val;
1008
1009         val = readl(port->membase + CDNS_UART_MODEMCR);
1010
1011         val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
1012
1013         if (mctrl & TIOCM_RTS)
1014                 val |= CDNS_UART_MODEMCR_RTS;
1015         if (mctrl & TIOCM_DTR)
1016                 val |= CDNS_UART_MODEMCR_DTR;
1017
1018         writel(val, port->membase + CDNS_UART_MODEMCR);
1019 }
1020
1021 #ifdef CONFIG_CONSOLE_POLL
1022 static int cdns_uart_poll_get_char(struct uart_port *port)
1023 {
1024         int c;
1025         unsigned long flags;
1026
1027         spin_lock_irqsave(&port->lock, flags);
1028
1029         /* Check if FIFO is empty */
1030         if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
1031                 c = NO_POLL_CHAR;
1032         else /* Read a character */
1033                 c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
1034
1035         spin_unlock_irqrestore(&port->lock, flags);
1036
1037         return c;
1038 }
1039
1040 static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
1041 {
1042         unsigned long flags;
1043
1044         spin_lock_irqsave(&port->lock, flags);
1045
1046         /* Wait until FIFO is empty */
1047         while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1048                 cpu_relax();
1049
1050         /* Write a character */
1051         writel(c, port->membase + CDNS_UART_FIFO);
1052
1053         /* Wait until FIFO is empty */
1054         while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1055                 cpu_relax();
1056
1057         spin_unlock_irqrestore(&port->lock, flags);
1058
1059         return;
1060 }
1061 #endif
1062
1063 static void cdns_uart_pm(struct uart_port *port, unsigned int state,
1064                    unsigned int oldstate)
1065 {
1066         struct cdns_uart *cdns_uart = port->private_data;
1067
1068         switch (state) {
1069         case UART_PM_STATE_OFF:
1070                 clk_disable(cdns_uart->uartclk);
1071                 clk_disable(cdns_uart->pclk);
1072                 break;
1073         default:
1074                 clk_enable(cdns_uart->pclk);
1075                 clk_enable(cdns_uart->uartclk);
1076                 break;
1077         }
1078 }
1079
1080 static const struct uart_ops cdns_uart_ops = {
1081         .set_mctrl      = cdns_uart_set_mctrl,
1082         .get_mctrl      = cdns_uart_get_mctrl,
1083         .start_tx       = cdns_uart_start_tx,
1084         .stop_tx        = cdns_uart_stop_tx,
1085         .stop_rx        = cdns_uart_stop_rx,
1086         .tx_empty       = cdns_uart_tx_empty,
1087         .break_ctl      = cdns_uart_break_ctl,
1088         .set_termios    = cdns_uart_set_termios,
1089         .startup        = cdns_uart_startup,
1090         .shutdown       = cdns_uart_shutdown,
1091         .pm             = cdns_uart_pm,
1092         .type           = cdns_uart_type,
1093         .verify_port    = cdns_uart_verify_port,
1094         .request_port   = cdns_uart_request_port,
1095         .release_port   = cdns_uart_release_port,
1096         .config_port    = cdns_uart_config_port,
1097 #ifdef CONFIG_CONSOLE_POLL
1098         .poll_get_char  = cdns_uart_poll_get_char,
1099         .poll_put_char  = cdns_uart_poll_put_char,
1100 #endif
1101 };
1102
1103 static struct uart_port cdns_uart_port[CDNS_UART_NR_PORTS];
1104
1105 /**
1106  * cdns_uart_get_port - Configure the port from platform device resource info
1107  * @id: Port id
1108  *
1109  * Return: a pointer to a uart_port or NULL for failure
1110  */
1111 static struct uart_port *cdns_uart_get_port(int id)
1112 {
1113         struct uart_port *port;
1114
1115         /* Try the given port id if failed use default method */
1116         if (id < CDNS_UART_NR_PORTS && cdns_uart_port[id].mapbase != 0) {
1117                 /* Find the next unused port */
1118                 for (id = 0; id < CDNS_UART_NR_PORTS; id++)
1119                         if (cdns_uart_port[id].mapbase == 0)
1120                                 break;
1121         }
1122
1123         if (id >= CDNS_UART_NR_PORTS)
1124                 return NULL;
1125
1126         port = &cdns_uart_port[id];
1127
1128         /* At this point, we've got an empty uart_port struct, initialize it */
1129         spin_lock_init(&port->lock);
1130         port->membase   = NULL;
1131         port->irq       = 0;
1132         port->type      = PORT_UNKNOWN;
1133         port->iotype    = UPIO_MEM32;
1134         port->flags     = UPF_BOOT_AUTOCONF;
1135         port->ops       = &cdns_uart_ops;
1136         port->fifosize  = CDNS_UART_FIFO_SIZE;
1137         port->line      = id;
1138         port->dev       = NULL;
1139         return port;
1140 }
1141
1142 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1143 /**
1144  * cdns_uart_console_wait_tx - Wait for the TX to be full
1145  * @port: Handle to the uart port structure
1146  */
1147 static void cdns_uart_console_wait_tx(struct uart_port *port)
1148 {
1149         while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1150                 barrier();
1151 }
1152
1153 /**
1154  * cdns_uart_console_putchar - write the character to the FIFO buffer
1155  * @port: Handle to the uart port structure
1156  * @ch: Character to be written
1157  */
1158 static void cdns_uart_console_putchar(struct uart_port *port, int ch)
1159 {
1160         cdns_uart_console_wait_tx(port);
1161         writel(ch, port->membase + CDNS_UART_FIFO);
1162 }
1163
1164 static void __init cdns_early_write(struct console *con, const char *s,
1165                                     unsigned n)
1166 {
1167         struct earlycon_device *dev = con->data;
1168
1169         uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
1170 }
1171
1172 static int __init cdns_early_console_setup(struct earlycon_device *device,
1173                                            const char *opt)
1174 {
1175         struct uart_port *port = &device->port;
1176
1177         if (!port->membase)
1178                 return -ENODEV;
1179
1180         /* initialise control register */
1181         writel(CDNS_UART_CR_TX_EN|CDNS_UART_CR_TXRST|CDNS_UART_CR_RXRST,
1182                port->membase + CDNS_UART_CR);
1183
1184         /* only set baud if specified on command line - otherwise
1185          * assume it has been initialized by a boot loader.
1186          */
1187         if (device->baud) {
1188                 u32 cd = 0, bdiv = 0;
1189                 u32 mr;
1190                 int div8;
1191
1192                 cdns_uart_calc_baud_divs(port->uartclk, device->baud,
1193                                          &bdiv, &cd, &div8);
1194                 mr = CDNS_UART_MR_PARITY_NONE;
1195                 if (div8)
1196                         mr |= CDNS_UART_MR_CLKSEL;
1197
1198                 writel(mr,   port->membase + CDNS_UART_MR);
1199                 writel(cd,   port->membase + CDNS_UART_BAUDGEN);
1200                 writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
1201         }
1202
1203         device->con->write = cdns_early_write;
1204
1205         return 0;
1206 }
1207 OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
1208 OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
1209 OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
1210 OF_EARLYCON_DECLARE(cdns, "xlnx,zynqmp-uart", cdns_early_console_setup);
1211
1212 /**
1213  * cdns_uart_console_write - perform write operation
1214  * @co: Console handle
1215  * @s: Pointer to character array
1216  * @count: No of characters
1217  */
1218 static void cdns_uart_console_write(struct console *co, const char *s,
1219                                 unsigned int count)
1220 {
1221         struct uart_port *port = &cdns_uart_port[co->index];
1222         unsigned long flags;
1223         unsigned int imr, ctrl;
1224         int locked = 1;
1225
1226         if (port->sysrq)
1227                 locked = 0;
1228         else if (oops_in_progress)
1229                 locked = spin_trylock_irqsave(&port->lock, flags);
1230         else
1231                 spin_lock_irqsave(&port->lock, flags);
1232
1233         /* save and disable interrupt */
1234         imr = readl(port->membase + CDNS_UART_IMR);
1235         writel(imr, port->membase + CDNS_UART_IDR);
1236
1237         /*
1238          * Make sure that the tx part is enabled. Set the TX enable bit and
1239          * clear the TX disable bit to enable the transmitter.
1240          */
1241         ctrl = readl(port->membase + CDNS_UART_CR);
1242         ctrl &= ~CDNS_UART_CR_TX_DIS;
1243         ctrl |= CDNS_UART_CR_TX_EN;
1244         writel(ctrl, port->membase + CDNS_UART_CR);
1245
1246         uart_console_write(port, s, count, cdns_uart_console_putchar);
1247         cdns_uart_console_wait_tx(port);
1248
1249         writel(ctrl, port->membase + CDNS_UART_CR);
1250
1251         /* restore interrupt state */
1252         writel(imr, port->membase + CDNS_UART_IER);
1253
1254         if (locked)
1255                 spin_unlock_irqrestore(&port->lock, flags);
1256 }
1257
1258 /**
1259  * cdns_uart_console_setup - Initialize the uart to default config
1260  * @co: Console handle
1261  * @options: Initial settings of uart
1262  *
1263  * Return: 0 on success, negative errno otherwise.
1264  */
1265 static int cdns_uart_console_setup(struct console *co, char *options)
1266 {
1267         struct uart_port *port = &cdns_uart_port[co->index];
1268         int baud = 9600;
1269         int bits = 8;
1270         int parity = 'n';
1271         int flow = 'n';
1272
1273         if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
1274                 return -EINVAL;
1275
1276         if (!port->membase) {
1277                 pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
1278                          co->index);
1279                 return -ENODEV;
1280         }
1281
1282         if (options)
1283                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1284
1285         return uart_set_options(port, co, baud, parity, bits, flow);
1286 }
1287
1288 static struct uart_driver cdns_uart_uart_driver;
1289
1290 static struct console cdns_uart_console = {
1291         .name   = CDNS_UART_TTY_NAME,
1292         .write  = cdns_uart_console_write,
1293         .device = uart_console_device,
1294         .setup  = cdns_uart_console_setup,
1295         .flags  = CON_PRINTBUFFER,
1296         .index  = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
1297         .data   = &cdns_uart_uart_driver,
1298 };
1299
1300 /**
1301  * cdns_uart_console_init - Initialization call
1302  *
1303  * Return: 0 on success, negative errno otherwise
1304  */
1305 static int __init cdns_uart_console_init(void)
1306 {
1307         register_console(&cdns_uart_console);
1308         return 0;
1309 }
1310
1311 console_initcall(cdns_uart_console_init);
1312
1313 #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
1314
1315 static struct uart_driver cdns_uart_uart_driver = {
1316         .owner          = THIS_MODULE,
1317         .driver_name    = CDNS_UART_NAME,
1318         .dev_name       = CDNS_UART_TTY_NAME,
1319         .major          = CDNS_UART_MAJOR,
1320         .minor          = CDNS_UART_MINOR,
1321         .nr             = CDNS_UART_NR_PORTS,
1322 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1323         .cons           = &cdns_uart_console,
1324 #endif
1325 };
1326
1327 #ifdef CONFIG_PM_SLEEP
1328 /**
1329  * cdns_uart_suspend - suspend event
1330  * @device: Pointer to the device structure
1331  *
1332  * Return: 0
1333  */
1334 static int cdns_uart_suspend(struct device *device)
1335 {
1336         struct uart_port *port = dev_get_drvdata(device);
1337         struct tty_struct *tty;
1338         struct device *tty_dev;
1339         int may_wake = 0;
1340
1341         /* Get the tty which could be NULL so don't assume it's valid */
1342         tty = tty_port_tty_get(&port->state->port);
1343         if (tty) {
1344                 tty_dev = tty->dev;
1345                 may_wake = device_may_wakeup(tty_dev);
1346                 tty_kref_put(tty);
1347         }
1348
1349         /*
1350          * Call the API provided in serial_core.c file which handles
1351          * the suspend.
1352          */
1353         uart_suspend_port(&cdns_uart_uart_driver, port);
1354         if (console_suspend_enabled && !may_wake) {
1355                 struct cdns_uart *cdns_uart = port->private_data;
1356
1357                 clk_disable(cdns_uart->uartclk);
1358                 clk_disable(cdns_uart->pclk);
1359         } else {
1360                 unsigned long flags = 0;
1361
1362                 spin_lock_irqsave(&port->lock, flags);
1363                 /* Empty the receive FIFO 1st before making changes */
1364                 while (!(readl(port->membase + CDNS_UART_SR) &
1365                                         CDNS_UART_SR_RXEMPTY))
1366                         readl(port->membase + CDNS_UART_FIFO);
1367                 /* set RX trigger level to 1 */
1368                 writel(1, port->membase + CDNS_UART_RXWM);
1369                 /* disable RX timeout interrups */
1370                 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
1371                 spin_unlock_irqrestore(&port->lock, flags);
1372         }
1373
1374         return 0;
1375 }
1376
1377 /**
1378  * cdns_uart_resume - Resume after a previous suspend
1379  * @device: Pointer to the device structure
1380  *
1381  * Return: 0
1382  */
1383 static int cdns_uart_resume(struct device *device)
1384 {
1385         struct uart_port *port = dev_get_drvdata(device);
1386         unsigned long flags = 0;
1387         u32 ctrl_reg;
1388         struct tty_struct *tty;
1389         struct device *tty_dev;
1390         int may_wake = 0;
1391
1392         /* Get the tty which could be NULL so don't assume it's valid */
1393         tty = tty_port_tty_get(&port->state->port);
1394         if (tty) {
1395                 tty_dev = tty->dev;
1396                 may_wake = device_may_wakeup(tty_dev);
1397                 tty_kref_put(tty);
1398         }
1399
1400         if (console_suspend_enabled && !may_wake) {
1401                 struct cdns_uart *cdns_uart = port->private_data;
1402
1403                 clk_enable(cdns_uart->pclk);
1404                 clk_enable(cdns_uart->uartclk);
1405
1406                 spin_lock_irqsave(&port->lock, flags);
1407
1408                 /* Set TX/RX Reset */
1409                 ctrl_reg = readl(port->membase + CDNS_UART_CR);
1410                 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
1411                 writel(ctrl_reg, port->membase + CDNS_UART_CR);
1412                 while (readl(port->membase + CDNS_UART_CR) &
1413                                 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
1414                         cpu_relax();
1415
1416                 /* restore rx timeout value */
1417                 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
1418                 /* Enable Tx/Rx */
1419                 ctrl_reg = readl(port->membase + CDNS_UART_CR);
1420                 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1421                 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
1422                 writel(ctrl_reg, port->membase + CDNS_UART_CR);
1423
1424                 spin_unlock_irqrestore(&port->lock, flags);
1425         } else {
1426                 spin_lock_irqsave(&port->lock, flags);
1427                 /* restore original rx trigger level */
1428                 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
1429                 /* enable RX timeout interrupt */
1430                 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
1431                 spin_unlock_irqrestore(&port->lock, flags);
1432         }
1433
1434         return uart_resume_port(&cdns_uart_uart_driver, port);
1435 }
1436 #endif /* ! CONFIG_PM_SLEEP */
1437
1438 static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
1439                 cdns_uart_resume);
1440
1441 static const struct cdns_platform_data zynqmp_uart_def = {
1442                                 .quirks = CDNS_UART_RXBS_SUPPORT, };
1443
1444 /* Match table for of_platform binding */
1445 static const struct of_device_id cdns_uart_of_match[] = {
1446         { .compatible = "xlnx,xuartps", },
1447         { .compatible = "cdns,uart-r1p8", },
1448         { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
1449         { .compatible = "xlnx,zynqmp-uart", .data = &zynqmp_uart_def },
1450         {}
1451 };
1452 MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
1453
1454 /**
1455  * cdns_uart_probe - Platform driver probe
1456  * @pdev: Pointer to the platform device structure
1457  *
1458  * Return: 0 on success, negative errno otherwise
1459  */
1460 static int cdns_uart_probe(struct platform_device *pdev)
1461 {
1462         int rc, id, irq;
1463         struct uart_port *port;
1464         struct resource *res;
1465         struct cdns_uart *cdns_uart_data;
1466         const struct of_device_id *match;
1467
1468         cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
1469                         GFP_KERNEL);
1470         if (!cdns_uart_data)
1471                 return -ENOMEM;
1472
1473         match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
1474         if (match && match->data) {
1475                 const struct cdns_platform_data *data = match->data;
1476
1477                 cdns_uart_data->quirks = data->quirks;
1478         }
1479
1480         cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
1481         if (IS_ERR(cdns_uart_data->pclk)) {
1482                 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1483                 if (!IS_ERR(cdns_uart_data->pclk))
1484                         dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
1485         }
1486         if (IS_ERR(cdns_uart_data->pclk)) {
1487                 dev_err(&pdev->dev, "pclk clock not found.\n");
1488                 return PTR_ERR(cdns_uart_data->pclk);
1489         }
1490
1491         cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1492         if (IS_ERR(cdns_uart_data->uartclk)) {
1493                 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1494                 if (!IS_ERR(cdns_uart_data->uartclk))
1495                         dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1496         }
1497         if (IS_ERR(cdns_uart_data->uartclk)) {
1498                 dev_err(&pdev->dev, "uart_clk clock not found.\n");
1499                 return PTR_ERR(cdns_uart_data->uartclk);
1500         }
1501
1502         rc = clk_prepare(cdns_uart_data->pclk);
1503         if (rc) {
1504                 dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
1505                 return rc;
1506         }
1507         rc = clk_prepare(cdns_uart_data->uartclk);
1508         if (rc) {
1509                 dev_err(&pdev->dev, "Unable to enable device clock.\n");
1510                 goto err_out_clk_dis_pclk;
1511         }
1512
1513         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1514         if (!res) {
1515                 rc = -ENODEV;
1516                 goto err_out_clk_disable;
1517         }
1518
1519         irq = platform_get_irq(pdev, 0);
1520         if (irq <= 0) {
1521                 rc = -ENXIO;
1522                 goto err_out_clk_disable;
1523         }
1524
1525 #ifdef CONFIG_COMMON_CLK
1526         cdns_uart_data->clk_rate_change_nb.notifier_call =
1527                         cdns_uart_clk_notifier_cb;
1528         if (clk_notifier_register(cdns_uart_data->uartclk,
1529                                 &cdns_uart_data->clk_rate_change_nb))
1530                 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1531 #endif
1532         /* Look for a serialN alias */
1533         id = of_alias_get_id(pdev->dev.of_node, "serial");
1534         if (id < 0)
1535                 id = 0;
1536
1537         /* Initialize the port structure */
1538         port = cdns_uart_get_port(id);
1539
1540         if (!port) {
1541                 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
1542                 rc = -ENODEV;
1543                 goto err_out_notif_unreg;
1544         }
1545
1546         /*
1547          * Register the port.
1548          * This function also registers this device with the tty layer
1549          * and triggers invocation of the config_port() entry point.
1550          */
1551         port->mapbase = res->start;
1552         port->irq = irq;
1553         port->dev = &pdev->dev;
1554         port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1555         port->private_data = cdns_uart_data;
1556         cdns_uart_data->port = port;
1557         platform_set_drvdata(pdev, port);
1558
1559         rc = uart_add_one_port(&cdns_uart_uart_driver, port);
1560         if (rc) {
1561                 dev_err(&pdev->dev,
1562                         "uart_add_one_port() failed; err=%i\n", rc);
1563                 goto err_out_notif_unreg;
1564         }
1565
1566         return 0;
1567
1568 err_out_notif_unreg:
1569 #ifdef CONFIG_COMMON_CLK
1570         clk_notifier_unregister(cdns_uart_data->uartclk,
1571                         &cdns_uart_data->clk_rate_change_nb);
1572 #endif
1573 err_out_clk_disable:
1574         clk_unprepare(cdns_uart_data->uartclk);
1575 err_out_clk_dis_pclk:
1576         clk_unprepare(cdns_uart_data->pclk);
1577
1578         return rc;
1579 }
1580
1581 /**
1582  * cdns_uart_remove - called when the platform driver is unregistered
1583  * @pdev: Pointer to the platform device structure
1584  *
1585  * Return: 0 on success, negative errno otherwise
1586  */
1587 static int cdns_uart_remove(struct platform_device *pdev)
1588 {
1589         struct uart_port *port = platform_get_drvdata(pdev);
1590         struct cdns_uart *cdns_uart_data = port->private_data;
1591         int rc;
1592
1593         /* Remove the cdns_uart port from the serial core */
1594 #ifdef CONFIG_COMMON_CLK
1595         clk_notifier_unregister(cdns_uart_data->uartclk,
1596                         &cdns_uart_data->clk_rate_change_nb);
1597 #endif
1598         rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
1599         port->mapbase = 0;
1600         clk_unprepare(cdns_uart_data->uartclk);
1601         clk_unprepare(cdns_uart_data->pclk);
1602         return rc;
1603 }
1604
1605 static struct platform_driver cdns_uart_platform_driver = {
1606         .probe   = cdns_uart_probe,
1607         .remove  = cdns_uart_remove,
1608         .driver  = {
1609                 .name = CDNS_UART_NAME,
1610                 .of_match_table = cdns_uart_of_match,
1611                 .pm = &cdns_uart_dev_pm_ops,
1612                 .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_XILINX_PS_UART),
1613                 },
1614 };
1615
1616 static int __init cdns_uart_init(void)
1617 {
1618         int retval = 0;
1619
1620         /* Register the cdns_uart driver with the serial core */
1621         retval = uart_register_driver(&cdns_uart_uart_driver);
1622         if (retval)
1623                 return retval;
1624
1625         /* Register the platform driver */
1626         retval = platform_driver_register(&cdns_uart_platform_driver);
1627         if (retval)
1628                 uart_unregister_driver(&cdns_uart_uart_driver);
1629
1630         return retval;
1631 }
1632
1633 static void __exit cdns_uart_exit(void)
1634 {
1635         /* Unregister the platform driver */
1636         platform_driver_unregister(&cdns_uart_platform_driver);
1637
1638         /* Unregister the cdns_uart driver */
1639         uart_unregister_driver(&cdns_uart_uart_driver);
1640 }
1641
1642 module_init(cdns_uart_init);
1643 module_exit(cdns_uart_exit);
1644
1645 MODULE_DESCRIPTION("Driver for Cadence UART");
1646 MODULE_AUTHOR("Xilinx Inc.");
1647 MODULE_LICENSE("GPL");