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