Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Driver for CLPS711x serial ports |
| 3 | * |
| 4 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 5 | * |
| 6 | * Copyright 1999 ARM Limited |
| 7 | * Copyright (C) 2000 Deep Blue Solutions Ltd. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 25 | #define SUPPORT_SYSRQ |
| 26 | #endif |
| 27 | |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/ioport.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/console.h> |
| 32 | #include <linux/sysrq.h> |
| 33 | #include <linux/spinlock.h> |
| 34 | #include <linux/device.h> |
| 35 | #include <linux/tty.h> |
| 36 | #include <linux/tty_flip.h> |
| 37 | #include <linux/serial_core.h> |
| 38 | #include <linux/serial.h> |
Russell King | 9973022 | 2009-03-25 10:21:35 +0000 | [diff] [blame] | 39 | #include <linux/io.h> |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 40 | #include <linux/clk.h> |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 41 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 43 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 46 | #define UART_CLPS711X_NAME "uart-clps711x" |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 47 | #define UART_CLPS711X_NR 2 |
| 48 | #define UART_CLPS711X_MAJOR 204 |
| 49 | #define UART_CLPS711X_MINOR 40 |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 50 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 51 | #define UBRLCR(port) ((port)->line ? UBRLCR2 : UBRLCR1) |
| 52 | #define UARTDR(port) ((port)->line ? UARTDR2 : UARTDR1) |
| 53 | #define SYSFLG(port) ((port)->line ? SYSFLG2 : SYSFLG1) |
| 54 | #define SYSCON(port) ((port)->line ? SYSCON2 : SYSCON1) |
| 55 | #define TX_IRQ(port) ((port)->line ? IRQ_UTXINT2 : IRQ_UTXINT1) |
| 56 | #define RX_IRQ(port) ((port)->line ? IRQ_URXINT2 : IRQ_URXINT1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | #define UART_ANY_ERR (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR) |
| 59 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 60 | struct clps711x_port { |
| 61 | struct uart_driver uart; |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 62 | struct clk *uart_clk; |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 63 | struct uart_port port[UART_CLPS711X_NR]; |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 64 | int tx_enabled[UART_CLPS711X_NR]; |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 65 | #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE |
| 66 | struct console console; |
| 67 | #endif |
| 68 | }; |
| 69 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 70 | static void clps711xuart_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 72 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
| 73 | |
| 74 | if (s->tx_enabled[port->line]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | disable_irq(TX_IRQ(port)); |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 76 | s->tx_enabled[port->line] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 80 | static void clps711xuart_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 82 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
| 83 | |
| 84 | if (!s->tx_enabled[port->line]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | enable_irq(TX_IRQ(port)); |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 86 | s->tx_enabled[port->line] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | static void clps711xuart_stop_rx(struct uart_port *port) |
| 91 | { |
| 92 | disable_irq(RX_IRQ(port)); |
| 93 | } |
| 94 | |
| 95 | static void clps711xuart_enable_ms(struct uart_port *port) |
| 96 | { |
| 97 | } |
| 98 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 99 | static irqreturn_t clps711xuart_int_rx(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | struct uart_port *port = dev_id; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 102 | struct tty_struct *tty = port->state->port.tty; |
Russell King | f993724 | 2005-09-24 10:12:47 +0100 | [diff] [blame] | 103 | unsigned int status, ch, flg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | status = clps_readl(SYSFLG(port)); |
| 106 | while (!(status & SYSFLG_URXFE)) { |
| 107 | ch = clps_readl(UARTDR(port)); |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | port->icount.rx++; |
| 110 | |
| 111 | flg = TTY_NORMAL; |
| 112 | |
| 113 | /* |
| 114 | * Note that the error handling code is |
| 115 | * out of the main execution path |
| 116 | */ |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 117 | if (unlikely(ch & UART_ANY_ERR)) { |
| 118 | if (ch & UARTDR_PARERR) |
| 119 | port->icount.parity++; |
| 120 | else if (ch & UARTDR_FRMERR) |
| 121 | port->icount.frame++; |
| 122 | if (ch & UARTDR_OVERR) |
| 123 | port->icount.overrun++; |
| 124 | |
| 125 | ch &= port->read_status_mask; |
| 126 | |
| 127 | if (ch & UARTDR_PARERR) |
| 128 | flg = TTY_PARITY; |
| 129 | else if (ch & UARTDR_FRMERR) |
| 130 | flg = TTY_FRAME; |
| 131 | |
| 132 | #ifdef SUPPORT_SYSRQ |
| 133 | port->sysrq = 0; |
| 134 | #endif |
| 135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 137 | if (uart_handle_sysrq_char(port, ch)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | goto ignore_char; |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* |
| 141 | * CHECK: does overrun affect the current character? |
| 142 | * ASSUMPTION: it does not. |
| 143 | */ |
Russell King | 05ab301 | 2005-05-09 23:21:59 +0100 | [diff] [blame] | 144 | uart_insert_char(port, ch, UARTDR_OVERR, ch, flg); |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 145 | |
| 146 | ignore_char: |
| 147 | status = clps_readl(SYSFLG(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 149 | tty_flip_buffer_push(tty); |
| 150 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 153 | static irqreturn_t clps711xuart_int_tx(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | struct uart_port *port = dev_id; |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 156 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 157 | struct circ_buf *xmit = &port->state->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | if (port->x_char) { |
| 160 | clps_writel(port->x_char, UARTDR(port)); |
| 161 | port->icount.tx++; |
| 162 | port->x_char = 0; |
| 163 | return IRQ_HANDLED; |
| 164 | } |
Alexander Shiyan | 7a6fbc9 | 2012-03-27 12:22:49 +0400 | [diff] [blame] | 165 | |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 166 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
| 167 | disable_irq_nosync(TX_IRQ(port)); |
| 168 | s->tx_enabled[port->line] = 0; |
| 169 | return IRQ_HANDLED; |
| 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 172 | while (!uart_circ_empty(xmit)) { |
| 173 | clps_writew(xmit->buf[xmit->tail], UARTDR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 175 | port->icount.tx++; |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 176 | if (clps_readl(SYSFLG(port) & SYSFLG_UTXFF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | break; |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 181 | uart_write_wakeup(port); |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return IRQ_HANDLED; |
| 184 | } |
| 185 | |
| 186 | static unsigned int clps711xuart_tx_empty(struct uart_port *port) |
| 187 | { |
| 188 | unsigned int status = clps_readl(SYSFLG(port)); |
| 189 | return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT; |
| 190 | } |
| 191 | |
| 192 | static unsigned int clps711xuart_get_mctrl(struct uart_port *port) |
| 193 | { |
Alexander Shiyan | 1593daf | 2012-10-14 11:05:28 +0400 | [diff] [blame^] | 194 | unsigned int status, result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Alexander Shiyan | 1593daf | 2012-10-14 11:05:28 +0400 | [diff] [blame^] | 196 | if (port->line == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | status = clps_readl(SYSFLG1); |
| 198 | if (status & SYSFLG1_DCD) |
| 199 | result |= TIOCM_CAR; |
| 200 | if (status & SYSFLG1_DSR) |
| 201 | result |= TIOCM_DSR; |
| 202 | if (status & SYSFLG1_CTS) |
| 203 | result |= TIOCM_CTS; |
Alexander Shiyan | 1593daf | 2012-10-14 11:05:28 +0400 | [diff] [blame^] | 204 | } else |
| 205 | result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl) |
| 212 | { |
| 213 | } |
| 214 | |
| 215 | static void clps711xuart_break_ctl(struct uart_port *port, int break_state) |
| 216 | { |
| 217 | unsigned long flags; |
| 218 | unsigned int ubrlcr; |
| 219 | |
| 220 | spin_lock_irqsave(&port->lock, flags); |
| 221 | ubrlcr = clps_readl(UBRLCR(port)); |
| 222 | if (break_state == -1) |
| 223 | ubrlcr |= UBRLCR_BREAK; |
| 224 | else |
| 225 | ubrlcr &= ~UBRLCR_BREAK; |
| 226 | clps_writel(ubrlcr, UBRLCR(port)); |
| 227 | spin_unlock_irqrestore(&port->lock, flags); |
| 228 | } |
| 229 | |
| 230 | static int clps711xuart_startup(struct uart_port *port) |
| 231 | { |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 232 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | unsigned int syscon; |
| 234 | int retval; |
| 235 | |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 236 | s->tx_enabled[port->line] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * Allocate the IRQs |
| 240 | */ |
| 241 | retval = request_irq(TX_IRQ(port), clps711xuart_int_tx, 0, |
| 242 | "clps711xuart_tx", port); |
| 243 | if (retval) |
| 244 | return retval; |
| 245 | |
| 246 | retval = request_irq(RX_IRQ(port), clps711xuart_int_rx, 0, |
| 247 | "clps711xuart_rx", port); |
| 248 | if (retval) { |
| 249 | free_irq(TX_IRQ(port), port); |
| 250 | return retval; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * enable the port |
| 255 | */ |
| 256 | syscon = clps_readl(SYSCON(port)); |
| 257 | syscon |= SYSCON_UARTEN; |
| 258 | clps_writel(syscon, SYSCON(port)); |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static void clps711xuart_shutdown(struct uart_port *port) |
| 264 | { |
| 265 | unsigned int ubrlcr, syscon; |
| 266 | |
| 267 | /* |
| 268 | * Free the interrupt |
| 269 | */ |
| 270 | free_irq(TX_IRQ(port), port); /* TX interrupt */ |
| 271 | free_irq(RX_IRQ(port), port); /* RX interrupt */ |
| 272 | |
| 273 | /* |
| 274 | * disable the port |
| 275 | */ |
| 276 | syscon = clps_readl(SYSCON(port)); |
| 277 | syscon &= ~SYSCON_UARTEN; |
| 278 | clps_writel(syscon, SYSCON(port)); |
| 279 | |
| 280 | /* |
| 281 | * disable break condition and fifos |
| 282 | */ |
| 283 | ubrlcr = clps_readl(UBRLCR(port)); |
| 284 | ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK); |
| 285 | clps_writel(ubrlcr, UBRLCR(port)); |
| 286 | } |
| 287 | |
| 288 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 289 | clps711xuart_set_termios(struct uart_port *port, struct ktermios *termios, |
| 290 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | unsigned int ubrlcr, baud, quot; |
| 293 | unsigned long flags; |
| 294 | |
| 295 | /* |
| 296 | * We don't implement CREAD. |
| 297 | */ |
| 298 | termios->c_cflag |= CREAD; |
| 299 | |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 300 | /* Ask the core to calculate the divisor for us */ |
| 301 | baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096, |
| 302 | port->uartclk / 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | quot = uart_get_divisor(port, baud); |
| 304 | |
| 305 | switch (termios->c_cflag & CSIZE) { |
| 306 | case CS5: |
| 307 | ubrlcr = UBRLCR_WRDLEN5; |
| 308 | break; |
| 309 | case CS6: |
| 310 | ubrlcr = UBRLCR_WRDLEN6; |
| 311 | break; |
| 312 | case CS7: |
| 313 | ubrlcr = UBRLCR_WRDLEN7; |
| 314 | break; |
| 315 | default: // CS8 |
| 316 | ubrlcr = UBRLCR_WRDLEN8; |
| 317 | break; |
| 318 | } |
| 319 | if (termios->c_cflag & CSTOPB) |
| 320 | ubrlcr |= UBRLCR_XSTOP; |
| 321 | if (termios->c_cflag & PARENB) { |
| 322 | ubrlcr |= UBRLCR_PRTEN; |
| 323 | if (!(termios->c_cflag & PARODD)) |
| 324 | ubrlcr |= UBRLCR_EVENPRT; |
| 325 | } |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 326 | |
| 327 | /* Enable FIFO */ |
| 328 | ubrlcr |= UBRLCR_FIFOEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 330 | spin_lock_irqsave(&port->lock, flags); |
| 331 | |
| 332 | /* |
| 333 | * Update the per-port timeout. |
| 334 | */ |
| 335 | uart_update_timeout(port, termios->c_cflag, baud); |
| 336 | |
| 337 | port->read_status_mask = UARTDR_OVERR; |
| 338 | if (termios->c_iflag & INPCK) |
| 339 | port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR; |
| 340 | |
| 341 | /* |
| 342 | * Characters to ignore |
| 343 | */ |
| 344 | port->ignore_status_mask = 0; |
| 345 | if (termios->c_iflag & IGNPAR) |
| 346 | port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR; |
| 347 | if (termios->c_iflag & IGNBRK) { |
| 348 | /* |
| 349 | * If we're ignoring parity and break indicators, |
| 350 | * ignore overruns to (for real raw support). |
| 351 | */ |
| 352 | if (termios->c_iflag & IGNPAR) |
| 353 | port->ignore_status_mask |= UARTDR_OVERR; |
| 354 | } |
| 355 | |
| 356 | quot -= 1; |
| 357 | |
| 358 | clps_writel(ubrlcr | quot, UBRLCR(port)); |
| 359 | |
| 360 | spin_unlock_irqrestore(&port->lock, flags); |
| 361 | } |
| 362 | |
| 363 | static const char *clps711xuart_type(struct uart_port *port) |
| 364 | { |
| 365 | return port->type == PORT_CLPS711X ? "CLPS711x" : NULL; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Configure/autoconfigure the port. |
| 370 | */ |
| 371 | static void clps711xuart_config_port(struct uart_port *port, int flags) |
| 372 | { |
| 373 | if (flags & UART_CONFIG_TYPE) |
| 374 | port->type = PORT_CLPS711X; |
| 375 | } |
| 376 | |
| 377 | static void clps711xuart_release_port(struct uart_port *port) |
| 378 | { |
| 379 | } |
| 380 | |
| 381 | static int clps711xuart_request_port(struct uart_port *port) |
| 382 | { |
| 383 | return 0; |
| 384 | } |
| 385 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 386 | static struct uart_ops uart_clps711x_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | .tx_empty = clps711xuart_tx_empty, |
| 388 | .set_mctrl = clps711xuart_set_mctrl_null, |
| 389 | .get_mctrl = clps711xuart_get_mctrl, |
| 390 | .stop_tx = clps711xuart_stop_tx, |
| 391 | .start_tx = clps711xuart_start_tx, |
| 392 | .stop_rx = clps711xuart_stop_rx, |
| 393 | .enable_ms = clps711xuart_enable_ms, |
| 394 | .break_ctl = clps711xuart_break_ctl, |
| 395 | .startup = clps711xuart_startup, |
| 396 | .shutdown = clps711xuart_shutdown, |
| 397 | .set_termios = clps711xuart_set_termios, |
| 398 | .type = clps711xuart_type, |
| 399 | .config_port = clps711xuart_config_port, |
| 400 | .release_port = clps711xuart_release_port, |
| 401 | .request_port = clps711xuart_request_port, |
| 402 | }; |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 405 | static void uart_clps711x_console_putchar(struct uart_port *port, int ch) |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 406 | { |
| 407 | while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF) |
| 408 | barrier(); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 409 | |
| 410 | clps_writew(ch, UARTDR(port)); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 413 | static void uart_clps711x_console_write(struct console *co, const char *c, |
| 414 | unsigned n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 416 | struct clps711x_port *s = (struct clps711x_port *)co->data; |
| 417 | struct uart_port *port = &s->port[co->index]; |
| 418 | u32 syscon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 420 | /* Ensure that the port is enabled */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | syscon = clps_readl(SYSCON(port)); |
| 422 | clps_writel(syscon | SYSCON_UARTEN, SYSCON(port)); |
| 423 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 424 | uart_console_write(port, c, n, uart_clps711x_console_putchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 426 | /* Wait for transmitter to become empty */ |
| 427 | while (clps_readl(SYSFLG(port)) & SYSFLG_UBUSY) |
| 428 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 430 | /* Restore the uart state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | clps_writel(syscon, SYSCON(port)); |
| 432 | } |
| 433 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 434 | static void uart_clps711x_console_get_options(struct uart_port *port, |
| 435 | int *baud, int *parity, |
| 436 | int *bits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
| 438 | if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) { |
| 439 | unsigned int ubrlcr, quot; |
| 440 | |
| 441 | ubrlcr = clps_readl(UBRLCR(port)); |
| 442 | |
| 443 | *parity = 'n'; |
| 444 | if (ubrlcr & UBRLCR_PRTEN) { |
| 445 | if (ubrlcr & UBRLCR_EVENPRT) |
| 446 | *parity = 'e'; |
| 447 | else |
| 448 | *parity = 'o'; |
| 449 | } |
| 450 | |
| 451 | if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7) |
| 452 | *bits = 7; |
| 453 | else |
| 454 | *bits = 8; |
| 455 | |
| 456 | quot = ubrlcr & UBRLCR_BAUD_MASK; |
| 457 | *baud = port->uartclk / (16 * (quot + 1)); |
| 458 | } |
| 459 | } |
| 460 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 461 | static int uart_clps711x_console_setup(struct console *co, char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 463 | int baud = 38400, bits = 8, parity = 'n', flow = 'n'; |
| 464 | struct clps711x_port *s = (struct clps711x_port *)co->data; |
| 465 | struct uart_port *port = &s->port[(co->index > 0) ? co->index : 0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | if (options) |
| 468 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 469 | else |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 470 | uart_clps711x_console_get_options(port, &baud, &parity, &bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
| 472 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 473 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | #endif |
| 475 | |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 476 | static int __devinit uart_clps711x_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 478 | struct clps711x_port *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | int ret, i; |
| 480 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 481 | s = devm_kzalloc(&pdev->dev, sizeof(struct clps711x_port), GFP_KERNEL); |
| 482 | if (!s) { |
| 483 | dev_err(&pdev->dev, "Error allocating port structure\n"); |
| 484 | return -ENOMEM; |
| 485 | } |
| 486 | platform_set_drvdata(pdev, s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 488 | s->uart_clk = devm_clk_get(&pdev->dev, "uart"); |
| 489 | if (IS_ERR(s->uart_clk)) { |
| 490 | dev_err(&pdev->dev, "Can't get UART clocks\n"); |
| 491 | ret = PTR_ERR(s->uart_clk); |
| 492 | goto err_out; |
| 493 | } |
| 494 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 495 | s->uart.owner = THIS_MODULE; |
| 496 | s->uart.dev_name = "ttyCL"; |
| 497 | s->uart.major = UART_CLPS711X_MAJOR; |
| 498 | s->uart.minor = UART_CLPS711X_MINOR; |
| 499 | s->uart.nr = UART_CLPS711X_NR; |
| 500 | #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE |
| 501 | s->uart.cons = &s->console; |
| 502 | s->uart.cons->device = uart_console_device; |
| 503 | s->uart.cons->write = uart_clps711x_console_write; |
| 504 | s->uart.cons->setup = uart_clps711x_console_setup; |
| 505 | s->uart.cons->flags = CON_PRINTBUFFER; |
| 506 | s->uart.cons->index = -1; |
| 507 | s->uart.cons->data = s; |
| 508 | strcpy(s->uart.cons->name, "ttyCL"); |
| 509 | #endif |
| 510 | ret = uart_register_driver(&s->uart); |
| 511 | if (ret) { |
| 512 | dev_err(&pdev->dev, "Registering UART driver failed\n"); |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 513 | devm_clk_put(&pdev->dev, s->uart_clk); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 514 | goto err_out; |
| 515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 517 | for (i = 0; i < UART_CLPS711X_NR; i++) { |
| 518 | s->port[i].line = i; |
| 519 | s->port[i].dev = &pdev->dev; |
| 520 | s->port[i].irq = TX_IRQ(&s->port[i]); |
| 521 | s->port[i].iobase = SYSCON(&s->port[i]); |
| 522 | s->port[i].type = PORT_CLPS711X; |
| 523 | s->port[i].fifosize = 16; |
| 524 | s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE; |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 525 | s->port[i].uartclk = clk_get_rate(s->uart_clk); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 526 | s->port[i].ops = &uart_clps711x_ops; |
| 527 | WARN_ON(uart_add_one_port(&s->uart, &s->port[i])); |
| 528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 530 | return 0; |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 531 | |
| 532 | err_out: |
| 533 | platform_set_drvdata(pdev, NULL); |
| 534 | |
| 535 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 538 | static int __devexit uart_clps711x_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 540 | struct clps711x_port *s = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | int i; |
| 542 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 543 | for (i = 0; i < UART_CLPS711X_NR; i++) |
| 544 | uart_remove_one_port(&s->uart, &s->port[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 546 | devm_clk_put(&pdev->dev, s->uart_clk); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 547 | uart_unregister_driver(&s->uart); |
| 548 | platform_set_drvdata(pdev, NULL); |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 549 | |
| 550 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 553 | static struct platform_driver clps711x_uart_driver = { |
| 554 | .driver = { |
| 555 | .name = UART_CLPS711X_NAME, |
| 556 | .owner = THIS_MODULE, |
| 557 | }, |
| 558 | .probe = uart_clps711x_probe, |
| 559 | .remove = __devexit_p(uart_clps711x_remove), |
| 560 | }; |
| 561 | module_platform_driver(clps711x_uart_driver); |
| 562 | |
| 563 | static struct platform_device clps711x_uart_device = { |
| 564 | .name = UART_CLPS711X_NAME, |
| 565 | }; |
| 566 | |
| 567 | static int __init uart_clps711x_init(void) |
| 568 | { |
| 569 | return platform_device_register(&clps711x_uart_device); |
| 570 | } |
| 571 | module_init(uart_clps711x_init); |
| 572 | |
| 573 | static void __exit uart_clps711x_exit(void) |
| 574 | { |
| 575 | platform_device_unregister(&clps711x_uart_device); |
| 576 | } |
| 577 | module_exit(uart_clps711x_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | MODULE_AUTHOR("Deep Blue Solutions Ltd"); |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 580 | MODULE_DESCRIPTION("CLPS711X serial driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | MODULE_LICENSE("GPL"); |