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