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