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