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 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 58 | struct clps711x_port { |
| 59 | struct uart_driver uart; |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 60 | struct clk *uart_clk; |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 61 | struct uart_port port[UART_CLPS711X_NR]; |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 62 | int tx_enabled[UART_CLPS711X_NR]; |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 63 | #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE |
| 64 | struct console console; |
| 65 | #endif |
| 66 | }; |
| 67 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 68 | static void clps711xuart_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 70 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
| 71 | |
| 72 | if (s->tx_enabled[port->line]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | disable_irq(TX_IRQ(port)); |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 74 | s->tx_enabled[port->line] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 78 | static void clps711xuart_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 80 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
| 81 | |
| 82 | if (!s->tx_enabled[port->line]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | enable_irq(TX_IRQ(port)); |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 84 | s->tx_enabled[port->line] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | static void clps711xuart_stop_rx(struct uart_port *port) |
| 89 | { |
| 90 | disable_irq(RX_IRQ(port)); |
| 91 | } |
| 92 | |
| 93 | static void clps711xuart_enable_ms(struct uart_port *port) |
| 94 | { |
| 95 | } |
| 96 | |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 97 | static irqreturn_t uart_clps711x_int_rx(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
| 99 | struct uart_port *port = dev_id; |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 100 | struct tty_struct *tty = tty_port_tty_get(&port->state->port); |
Russell King | f993724 | 2005-09-24 10:12:47 +0100 | [diff] [blame] | 101 | unsigned int status, ch, flg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 103 | if (!tty) |
| 104 | return IRQ_HANDLED; |
| 105 | |
| 106 | for (;;) { |
| 107 | status = clps_readl(SYSFLG(port)); |
| 108 | if (status & SYSFLG_URXFE) |
| 109 | break; |
| 110 | |
| 111 | ch = clps_readw(UARTDR(port)); |
| 112 | status = ch & (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR); |
| 113 | ch &= 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | port->icount.rx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | flg = TTY_NORMAL; |
| 117 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 118 | if (unlikely(status)) { |
| 119 | if (status & UARTDR_PARERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 120 | port->icount.parity++; |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 121 | else if (status & UARTDR_FRMERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 122 | port->icount.frame++; |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 123 | else if (status & UARTDR_OVERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 124 | port->icount.overrun++; |
| 125 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 126 | status &= port->read_status_mask; |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 127 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 128 | if (status & UARTDR_PARERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 129 | flg = TTY_PARITY; |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 130 | else if (status & UARTDR_FRMERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 131 | flg = TTY_FRAME; |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 132 | else if (status & UARTDR_OVERR) |
| 133 | flg = TTY_OVERRUN; |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 136 | if (uart_handle_sysrq_char(port, ch)) |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 137 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 139 | if (status & port->ignore_status_mask) |
| 140 | continue; |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 141 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 142 | uart_insert_char(port, status, UARTDR_OVERR, ch, flg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 144 | |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 145 | tty_flip_buffer_push(tty); |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 146 | |
| 147 | tty_kref_put(tty); |
| 148 | |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 149 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 152 | static irqreturn_t uart_clps711x_int_tx(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | struct uart_port *port = dev_id; |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 155 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 156 | struct circ_buf *xmit = &port->state->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | if (port->x_char) { |
| 159 | clps_writel(port->x_char, UARTDR(port)); |
| 160 | port->icount.tx++; |
| 161 | port->x_char = 0; |
| 162 | return IRQ_HANDLED; |
| 163 | } |
Alexander Shiyan | 7a6fbc9 | 2012-03-27 12:22:49 +0400 | [diff] [blame] | 164 | |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 165 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
| 166 | disable_irq_nosync(TX_IRQ(port)); |
| 167 | s->tx_enabled[port->line] = 0; |
| 168 | return IRQ_HANDLED; |
| 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 171 | while (!uart_circ_empty(xmit)) { |
| 172 | clps_writew(xmit->buf[xmit->tail], UARTDR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 174 | port->icount.tx++; |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 175 | if (clps_readl(SYSFLG(port) & SYSFLG_UTXFF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | break; |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 180 | uart_write_wakeup(port); |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return IRQ_HANDLED; |
| 183 | } |
| 184 | |
| 185 | static unsigned int clps711xuart_tx_empty(struct uart_port *port) |
| 186 | { |
| 187 | unsigned int status = clps_readl(SYSFLG(port)); |
| 188 | return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT; |
| 189 | } |
| 190 | |
| 191 | static unsigned int clps711xuart_get_mctrl(struct uart_port *port) |
| 192 | { |
Alexander Shiyan | 1593daf | 2012-10-14 11:05:28 +0400 | [diff] [blame] | 193 | unsigned int status, result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Alexander Shiyan | 1593daf | 2012-10-14 11:05:28 +0400 | [diff] [blame] | 195 | if (port->line == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | status = clps_readl(SYSFLG1); |
| 197 | if (status & SYSFLG1_DCD) |
| 198 | result |= TIOCM_CAR; |
| 199 | if (status & SYSFLG1_DSR) |
| 200 | result |= TIOCM_DSR; |
| 201 | if (status & SYSFLG1_CTS) |
| 202 | result |= TIOCM_CTS; |
Alexander Shiyan | 1593daf | 2012-10-14 11:05:28 +0400 | [diff] [blame] | 203 | } else |
| 204 | result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | return result; |
| 207 | } |
| 208 | |
| 209 | static void |
| 210 | clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl) |
| 211 | { |
| 212 | } |
| 213 | |
| 214 | static void clps711xuart_break_ctl(struct uart_port *port, int break_state) |
| 215 | { |
| 216 | unsigned long flags; |
| 217 | unsigned int ubrlcr; |
| 218 | |
| 219 | spin_lock_irqsave(&port->lock, flags); |
Alexander Shiyan | ec33552 | 2012-10-14 11:05:29 +0400 | [diff] [blame] | 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | ubrlcr = clps_readl(UBRLCR(port)); |
Alexander Shiyan | ec33552 | 2012-10-14 11:05:29 +0400 | [diff] [blame] | 222 | if (break_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | ubrlcr |= UBRLCR_BREAK; |
| 224 | else |
| 225 | ubrlcr &= ~UBRLCR_BREAK; |
| 226 | clps_writel(ubrlcr, UBRLCR(port)); |
Alexander Shiyan | ec33552 | 2012-10-14 11:05:29 +0400 | [diff] [blame] | 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | spin_unlock_irqrestore(&port->lock, flags); |
| 229 | } |
| 230 | |
| 231 | static int clps711xuart_startup(struct uart_port *port) |
| 232 | { |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 233 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 234 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 236 | s->tx_enabled[port->line] = 1; |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 237 | /* Allocate the IRQs */ |
| 238 | ret = devm_request_irq(port->dev, TX_IRQ(port), uart_clps711x_int_tx, |
| 239 | 0, UART_CLPS711X_NAME " TX", port); |
| 240 | if (ret) |
| 241 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 243 | ret = devm_request_irq(port->dev, RX_IRQ(port), uart_clps711x_int_rx, |
| 244 | 0, UART_CLPS711X_NAME " RX", port); |
| 245 | if (ret) { |
| 246 | devm_free_irq(port->dev, TX_IRQ(port), port); |
| 247 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Alexander Shiyan | f52ede2 | 2012-10-14 11:05:32 +0400 | [diff] [blame] | 250 | /* Disable break */ |
| 251 | clps_writel(clps_readl(UBRLCR(port)) & ~UBRLCR_BREAK, UBRLCR(port)); |
| 252 | |
| 253 | /* Enable the port */ |
| 254 | clps_writel(clps_readl(SYSCON(port)) | SYSCON_UARTEN, SYSCON(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static void clps711xuart_shutdown(struct uart_port *port) |
| 260 | { |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 261 | /* Free the interrupts */ |
| 262 | devm_free_irq(port->dev, TX_IRQ(port), port); |
| 263 | devm_free_irq(port->dev, RX_IRQ(port), port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Alexander Shiyan | f52ede2 | 2012-10-14 11:05:32 +0400 | [diff] [blame] | 265 | /* Disable the port */ |
| 266 | clps_writel(clps_readl(SYSCON(port)) & ~SYSCON_UARTEN, SYSCON(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 270 | clps711xuart_set_termios(struct uart_port *port, struct ktermios *termios, |
| 271 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
| 273 | unsigned int ubrlcr, baud, quot; |
| 274 | unsigned long flags; |
| 275 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame^] | 276 | /* Mask termios capabilities we don't support */ |
| 277 | termios->c_cflag &= ~CMSPAR; |
| 278 | termios->c_iflag &= ~(BRKINT | IGNBRK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 280 | /* Ask the core to calculate the divisor for us */ |
| 281 | baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096, |
| 282 | port->uartclk / 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | quot = uart_get_divisor(port, baud); |
| 284 | |
| 285 | switch (termios->c_cflag & CSIZE) { |
| 286 | case CS5: |
| 287 | ubrlcr = UBRLCR_WRDLEN5; |
| 288 | break; |
| 289 | case CS6: |
| 290 | ubrlcr = UBRLCR_WRDLEN6; |
| 291 | break; |
| 292 | case CS7: |
| 293 | ubrlcr = UBRLCR_WRDLEN7; |
| 294 | break; |
| 295 | default: // CS8 |
| 296 | ubrlcr = UBRLCR_WRDLEN8; |
| 297 | break; |
| 298 | } |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame^] | 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (termios->c_cflag & CSTOPB) |
| 301 | ubrlcr |= UBRLCR_XSTOP; |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame^] | 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | if (termios->c_cflag & PARENB) { |
| 304 | ubrlcr |= UBRLCR_PRTEN; |
| 305 | if (!(termios->c_cflag & PARODD)) |
| 306 | ubrlcr |= UBRLCR_EVENPRT; |
| 307 | } |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 308 | |
| 309 | /* Enable FIFO */ |
| 310 | ubrlcr |= UBRLCR_FIFOEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | spin_lock_irqsave(&port->lock, flags); |
| 313 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame^] | 314 | /* Set read status mask */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | port->read_status_mask = UARTDR_OVERR; |
| 316 | if (termios->c_iflag & INPCK) |
| 317 | port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR; |
| 318 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame^] | 319 | /* Set status ignore mask */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | port->ignore_status_mask = 0; |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame^] | 321 | if (!(termios->c_cflag & CREAD)) |
| 322 | port->ignore_status_mask |= UARTDR_OVERR | UARTDR_PARERR | |
| 323 | UARTDR_FRMERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame^] | 325 | uart_update_timeout(port, termios->c_cflag, baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame^] | 327 | clps_writel(ubrlcr | (quot - 1), UBRLCR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | spin_unlock_irqrestore(&port->lock, flags); |
| 330 | } |
| 331 | |
| 332 | static const char *clps711xuart_type(struct uart_port *port) |
| 333 | { |
| 334 | return port->type == PORT_CLPS711X ? "CLPS711x" : NULL; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Configure/autoconfigure the port. |
| 339 | */ |
| 340 | static void clps711xuart_config_port(struct uart_port *port, int flags) |
| 341 | { |
| 342 | if (flags & UART_CONFIG_TYPE) |
| 343 | port->type = PORT_CLPS711X; |
| 344 | } |
| 345 | |
| 346 | static void clps711xuart_release_port(struct uart_port *port) |
| 347 | { |
| 348 | } |
| 349 | |
| 350 | static int clps711xuart_request_port(struct uart_port *port) |
| 351 | { |
| 352 | return 0; |
| 353 | } |
| 354 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 355 | static struct uart_ops uart_clps711x_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | .tx_empty = clps711xuart_tx_empty, |
| 357 | .set_mctrl = clps711xuart_set_mctrl_null, |
| 358 | .get_mctrl = clps711xuart_get_mctrl, |
| 359 | .stop_tx = clps711xuart_stop_tx, |
| 360 | .start_tx = clps711xuart_start_tx, |
| 361 | .stop_rx = clps711xuart_stop_rx, |
| 362 | .enable_ms = clps711xuart_enable_ms, |
| 363 | .break_ctl = clps711xuart_break_ctl, |
| 364 | .startup = clps711xuart_startup, |
| 365 | .shutdown = clps711xuart_shutdown, |
| 366 | .set_termios = clps711xuart_set_termios, |
| 367 | .type = clps711xuart_type, |
| 368 | .config_port = clps711xuart_config_port, |
| 369 | .release_port = clps711xuart_release_port, |
| 370 | .request_port = clps711xuart_request_port, |
| 371 | }; |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 374 | static void uart_clps711x_console_putchar(struct uart_port *port, int ch) |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 375 | { |
| 376 | while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF) |
| 377 | barrier(); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 378 | |
| 379 | clps_writew(ch, UARTDR(port)); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 382 | static void uart_clps711x_console_write(struct console *co, const char *c, |
| 383 | unsigned n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 385 | struct clps711x_port *s = (struct clps711x_port *)co->data; |
| 386 | struct uart_port *port = &s->port[co->index]; |
| 387 | u32 syscon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 389 | /* Ensure that the port is enabled */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | syscon = clps_readl(SYSCON(port)); |
| 391 | clps_writel(syscon | SYSCON_UARTEN, SYSCON(port)); |
| 392 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 393 | uart_console_write(port, c, n, uart_clps711x_console_putchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 395 | /* Wait for transmitter to become empty */ |
| 396 | while (clps_readl(SYSFLG(port)) & SYSFLG_UBUSY) |
| 397 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 399 | /* Restore the uart state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | clps_writel(syscon, SYSCON(port)); |
| 401 | } |
| 402 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 403 | static void uart_clps711x_console_get_options(struct uart_port *port, |
| 404 | int *baud, int *parity, |
| 405 | int *bits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) { |
| 408 | unsigned int ubrlcr, quot; |
| 409 | |
| 410 | ubrlcr = clps_readl(UBRLCR(port)); |
| 411 | |
| 412 | *parity = 'n'; |
| 413 | if (ubrlcr & UBRLCR_PRTEN) { |
| 414 | if (ubrlcr & UBRLCR_EVENPRT) |
| 415 | *parity = 'e'; |
| 416 | else |
| 417 | *parity = 'o'; |
| 418 | } |
| 419 | |
| 420 | if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7) |
| 421 | *bits = 7; |
| 422 | else |
| 423 | *bits = 8; |
| 424 | |
| 425 | quot = ubrlcr & UBRLCR_BAUD_MASK; |
| 426 | *baud = port->uartclk / (16 * (quot + 1)); |
| 427 | } |
| 428 | } |
| 429 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 430 | static int uart_clps711x_console_setup(struct console *co, char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 432 | int baud = 38400, bits = 8, parity = 'n', flow = 'n'; |
| 433 | struct clps711x_port *s = (struct clps711x_port *)co->data; |
| 434 | struct uart_port *port = &s->port[(co->index > 0) ? co->index : 0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | if (options) |
| 437 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 438 | else |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 439 | uart_clps711x_console_get_options(port, &baud, &parity, &bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
| 441 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | #endif |
| 444 | |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 445 | static int __devinit uart_clps711x_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 447 | struct clps711x_port *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | int ret, i; |
| 449 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 450 | s = devm_kzalloc(&pdev->dev, sizeof(struct clps711x_port), GFP_KERNEL); |
| 451 | if (!s) { |
| 452 | dev_err(&pdev->dev, "Error allocating port structure\n"); |
| 453 | return -ENOMEM; |
| 454 | } |
| 455 | platform_set_drvdata(pdev, s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 457 | s->uart_clk = devm_clk_get(&pdev->dev, "uart"); |
| 458 | if (IS_ERR(s->uart_clk)) { |
| 459 | dev_err(&pdev->dev, "Can't get UART clocks\n"); |
| 460 | ret = PTR_ERR(s->uart_clk); |
| 461 | goto err_out; |
| 462 | } |
| 463 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 464 | s->uart.owner = THIS_MODULE; |
| 465 | s->uart.dev_name = "ttyCL"; |
| 466 | s->uart.major = UART_CLPS711X_MAJOR; |
| 467 | s->uart.minor = UART_CLPS711X_MINOR; |
| 468 | s->uart.nr = UART_CLPS711X_NR; |
| 469 | #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE |
| 470 | s->uart.cons = &s->console; |
| 471 | s->uart.cons->device = uart_console_device; |
| 472 | s->uart.cons->write = uart_clps711x_console_write; |
| 473 | s->uart.cons->setup = uart_clps711x_console_setup; |
| 474 | s->uart.cons->flags = CON_PRINTBUFFER; |
| 475 | s->uart.cons->index = -1; |
| 476 | s->uart.cons->data = s; |
| 477 | strcpy(s->uart.cons->name, "ttyCL"); |
| 478 | #endif |
| 479 | ret = uart_register_driver(&s->uart); |
| 480 | if (ret) { |
| 481 | dev_err(&pdev->dev, "Registering UART driver failed\n"); |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 482 | devm_clk_put(&pdev->dev, s->uart_clk); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 483 | goto err_out; |
| 484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 486 | for (i = 0; i < UART_CLPS711X_NR; i++) { |
| 487 | s->port[i].line = i; |
| 488 | s->port[i].dev = &pdev->dev; |
| 489 | s->port[i].irq = TX_IRQ(&s->port[i]); |
| 490 | s->port[i].iobase = SYSCON(&s->port[i]); |
| 491 | s->port[i].type = PORT_CLPS711X; |
| 492 | s->port[i].fifosize = 16; |
| 493 | s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE; |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 494 | s->port[i].uartclk = clk_get_rate(s->uart_clk); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 495 | s->port[i].ops = &uart_clps711x_ops; |
| 496 | WARN_ON(uart_add_one_port(&s->uart, &s->port[i])); |
| 497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
| 499 | return 0; |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 500 | |
| 501 | err_out: |
| 502 | platform_set_drvdata(pdev, NULL); |
| 503 | |
| 504 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 507 | static int __devexit uart_clps711x_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 509 | struct clps711x_port *s = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | int i; |
| 511 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 512 | for (i = 0; i < UART_CLPS711X_NR; i++) |
| 513 | uart_remove_one_port(&s->uart, &s->port[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 515 | devm_clk_put(&pdev->dev, s->uart_clk); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 516 | uart_unregister_driver(&s->uart); |
| 517 | platform_set_drvdata(pdev, NULL); |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 518 | |
| 519 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 522 | static struct platform_driver clps711x_uart_driver = { |
| 523 | .driver = { |
| 524 | .name = UART_CLPS711X_NAME, |
| 525 | .owner = THIS_MODULE, |
| 526 | }, |
| 527 | .probe = uart_clps711x_probe, |
| 528 | .remove = __devexit_p(uart_clps711x_remove), |
| 529 | }; |
| 530 | module_platform_driver(clps711x_uart_driver); |
| 531 | |
| 532 | static struct platform_device clps711x_uart_device = { |
| 533 | .name = UART_CLPS711X_NAME, |
| 534 | }; |
| 535 | |
| 536 | static int __init uart_clps711x_init(void) |
| 537 | { |
| 538 | return platform_device_register(&clps711x_uart_device); |
| 539 | } |
| 540 | module_init(uart_clps711x_init); |
| 541 | |
| 542 | static void __exit uart_clps711x_exit(void) |
| 543 | { |
| 544 | platform_device_unregister(&clps711x_uart_device); |
| 545 | } |
| 546 | module_exit(uart_clps711x_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | MODULE_AUTHOR("Deep Blue Solutions Ltd"); |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 549 | MODULE_DESCRIPTION("CLPS711X serial driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | MODULE_LICENSE("GPL"); |