Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/amba.c |
| 3 | * |
| 4 | * Driver for AMBA serial ports |
| 5 | * |
| 6 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 7 | * |
| 8 | * Copyright 1999 ARM Limited |
| 9 | * Copyright (C) 2000 Deep Blue Solutions Ltd. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * This is a generic driver for ARM AMBA-type serial ports. They |
| 26 | * have a lot of 16550-like features, but are not register compatible. |
| 27 | * Note that although they do have CTS, DCD and DSR inputs, they do |
| 28 | * not have an RI input, nor do they have DTR or RTS outputs. If |
| 29 | * required, these have to be supplied via some other means (eg, GPIO) |
| 30 | * and hooked into this driver. |
| 31 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 34 | #define SUPPORT_SYSRQ |
| 35 | #endif |
| 36 | |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/ioport.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/console.h> |
| 41 | #include <linux/sysrq.h> |
| 42 | #include <linux/device.h> |
| 43 | #include <linux/tty.h> |
| 44 | #include <linux/tty_flip.h> |
| 45 | #include <linux/serial_core.h> |
| 46 | #include <linux/serial.h> |
Russell King | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 47 | #include <linux/amba/bus.h> |
| 48 | #include <linux/amba/serial.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 49 | #include <linux/clk.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 50 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | #include <asm/io.h> |
Russell King | c6b8fda | 2005-10-28 14:05:16 +0100 | [diff] [blame] | 53 | #include <asm/sizes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | #define UART_NR 14 |
| 56 | |
| 57 | #define SERIAL_AMBA_MAJOR 204 |
| 58 | #define SERIAL_AMBA_MINOR 64 |
| 59 | #define SERIAL_AMBA_NR UART_NR |
| 60 | |
| 61 | #define AMBA_ISR_PASS_LIMIT 256 |
| 62 | |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 63 | #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE) |
| 64 | #define UART_DUMMY_DR_RX (1 << 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 66 | /* There is by now at least one vendor with differing details, so handle it */ |
| 67 | struct vendor_data { |
| 68 | unsigned int ifls; |
| 69 | unsigned int fifosize; |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 70 | unsigned int lcrh_tx; |
| 71 | unsigned int lcrh_rx; |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 72 | bool oversampling; |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static struct vendor_data vendor_arm = { |
| 76 | .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8, |
| 77 | .fifosize = 16, |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 78 | .lcrh_tx = UART011_LCRH, |
| 79 | .lcrh_rx = UART011_LCRH, |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 80 | .oversampling = false, |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static struct vendor_data vendor_st = { |
| 84 | .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF, |
| 85 | .fifosize = 64, |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 86 | .lcrh_tx = ST_UART011_LCRH_TX, |
| 87 | .lcrh_rx = ST_UART011_LCRH_RX, |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 88 | .oversampling = true, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame^] | 91 | /* |
| 92 | * We wrap our port structure around the generic uart_port. |
| 93 | */ |
| 94 | struct uart_amba_port { |
| 95 | struct uart_port port; |
| 96 | struct clk *clk; |
| 97 | const struct vendor_data *vendor; |
| 98 | unsigned int im; /* interrupt mask */ |
| 99 | unsigned int old_status; |
| 100 | unsigned int lcrh_tx; /* vendor-specific */ |
| 101 | unsigned int lcrh_rx; /* vendor-specific */ |
| 102 | bool autorts; |
| 103 | char type[12]; |
| 104 | }; |
| 105 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 106 | static void pl011_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 109 | |
| 110 | uap->im &= ~UART011_TXIM; |
| 111 | writew(uap->im, uap->port.membase + UART011_IMSC); |
| 112 | } |
| 113 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 114 | static void pl011_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
| 116 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 117 | |
| 118 | uap->im |= UART011_TXIM; |
| 119 | writew(uap->im, uap->port.membase + UART011_IMSC); |
| 120 | } |
| 121 | |
| 122 | static void pl011_stop_rx(struct uart_port *port) |
| 123 | { |
| 124 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 125 | |
| 126 | uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM| |
| 127 | UART011_PEIM|UART011_BEIM|UART011_OEIM); |
| 128 | writew(uap->im, uap->port.membase + UART011_IMSC); |
| 129 | } |
| 130 | |
| 131 | static void pl011_enable_ms(struct uart_port *port) |
| 132 | { |
| 133 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 134 | |
| 135 | uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM; |
| 136 | writew(uap->im, uap->port.membase + UART011_IMSC); |
| 137 | } |
| 138 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 139 | static void pl011_rx_chars(struct uart_amba_port *uap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 141 | struct tty_struct *tty = uap->port.state->port.tty; |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 142 | unsigned int status, ch, flag, max_count = 256; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | status = readw(uap->port.membase + UART01x_FR); |
| 145 | while ((status & UART01x_FR_RXFE) == 0 && max_count--) { |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 146 | ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | flag = TTY_NORMAL; |
| 148 | uap->port.icount.rx++; |
| 149 | |
| 150 | /* |
| 151 | * Note that the error handling code is |
| 152 | * out of the main execution path |
| 153 | */ |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 154 | if (unlikely(ch & UART_DR_ERROR)) { |
| 155 | if (ch & UART011_DR_BE) { |
| 156 | ch &= ~(UART011_DR_FE | UART011_DR_PE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | uap->port.icount.brk++; |
| 158 | if (uart_handle_break(&uap->port)) |
| 159 | goto ignore_char; |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 160 | } else if (ch & UART011_DR_PE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | uap->port.icount.parity++; |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 162 | else if (ch & UART011_DR_FE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | uap->port.icount.frame++; |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 164 | if (ch & UART011_DR_OE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | uap->port.icount.overrun++; |
| 166 | |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 167 | ch &= uap->port.read_status_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 169 | if (ch & UART011_DR_BE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | flag = TTY_BREAK; |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 171 | else if (ch & UART011_DR_PE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | flag = TTY_PARITY; |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 173 | else if (ch & UART011_DR_FE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | flag = TTY_FRAME; |
| 175 | } |
| 176 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 177 | if (uart_handle_sysrq_char(&uap->port, ch & 255)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | goto ignore_char; |
| 179 | |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 180 | uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag); |
Russell King | 05ab301 | 2005-05-09 23:21:59 +0100 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | ignore_char: |
| 183 | status = readw(uap->port.membase + UART01x_FR); |
| 184 | } |
Thomas Gleixner | 2389b27 | 2007-05-29 21:53:50 +0100 | [diff] [blame] | 185 | spin_unlock(&uap->port.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | tty_flip_buffer_push(tty); |
Thomas Gleixner | 2389b27 | 2007-05-29 21:53:50 +0100 | [diff] [blame] | 187 | spin_lock(&uap->port.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void pl011_tx_chars(struct uart_amba_port *uap) |
| 191 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 192 | struct circ_buf *xmit = &uap->port.state->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | int count; |
| 194 | |
| 195 | if (uap->port.x_char) { |
| 196 | writew(uap->port.x_char, uap->port.membase + UART01x_DR); |
| 197 | uap->port.icount.tx++; |
| 198 | uap->port.x_char = 0; |
| 199 | return; |
| 200 | } |
| 201 | if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 202 | pl011_stop_tx(&uap->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | count = uap->port.fifosize >> 1; |
| 207 | do { |
| 208 | writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR); |
| 209 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 210 | uap->port.icount.tx++; |
| 211 | if (uart_circ_empty(xmit)) |
| 212 | break; |
| 213 | } while (--count > 0); |
| 214 | |
| 215 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 216 | uart_write_wakeup(&uap->port); |
| 217 | |
| 218 | if (uart_circ_empty(xmit)) |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 219 | pl011_stop_tx(&uap->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void pl011_modem_status(struct uart_amba_port *uap) |
| 223 | { |
| 224 | unsigned int status, delta; |
| 225 | |
| 226 | status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY; |
| 227 | |
| 228 | delta = status ^ uap->old_status; |
| 229 | uap->old_status = status; |
| 230 | |
| 231 | if (!delta) |
| 232 | return; |
| 233 | |
| 234 | if (delta & UART01x_FR_DCD) |
| 235 | uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD); |
| 236 | |
| 237 | if (delta & UART01x_FR_DSR) |
| 238 | uap->port.icount.dsr++; |
| 239 | |
| 240 | if (delta & UART01x_FR_CTS) |
| 241 | uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS); |
| 242 | |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 243 | wake_up_interruptible(&uap->port.state->port.delta_msr_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 246 | static irqreturn_t pl011_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | struct uart_amba_port *uap = dev_id; |
| 249 | unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT; |
| 250 | int handled = 0; |
| 251 | |
| 252 | spin_lock(&uap->port.lock); |
| 253 | |
| 254 | status = readw(uap->port.membase + UART011_MIS); |
| 255 | if (status) { |
| 256 | do { |
| 257 | writew(status & ~(UART011_TXIS|UART011_RTIS| |
| 258 | UART011_RXIS), |
| 259 | uap->port.membase + UART011_ICR); |
| 260 | |
| 261 | if (status & (UART011_RTIS|UART011_RXIS)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | pl011_rx_chars(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (status & (UART011_DSRMIS|UART011_DCDMIS| |
| 264 | UART011_CTSMIS|UART011_RIMIS)) |
| 265 | pl011_modem_status(uap); |
| 266 | if (status & UART011_TXIS) |
| 267 | pl011_tx_chars(uap); |
| 268 | |
| 269 | if (pass_counter-- == 0) |
| 270 | break; |
| 271 | |
| 272 | status = readw(uap->port.membase + UART011_MIS); |
| 273 | } while (status != 0); |
| 274 | handled = 1; |
| 275 | } |
| 276 | |
| 277 | spin_unlock(&uap->port.lock); |
| 278 | |
| 279 | return IRQ_RETVAL(handled); |
| 280 | } |
| 281 | |
| 282 | static unsigned int pl01x_tx_empty(struct uart_port *port) |
| 283 | { |
| 284 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 285 | unsigned int status = readw(uap->port.membase + UART01x_FR); |
| 286 | return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT; |
| 287 | } |
| 288 | |
| 289 | static unsigned int pl01x_get_mctrl(struct uart_port *port) |
| 290 | { |
| 291 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 292 | unsigned int result = 0; |
| 293 | unsigned int status = readw(uap->port.membase + UART01x_FR); |
| 294 | |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 295 | #define TIOCMBIT(uartbit, tiocmbit) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | if (status & uartbit) \ |
| 297 | result |= tiocmbit |
| 298 | |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 299 | TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR); |
| 300 | TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR); |
| 301 | TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS); |
| 302 | TIOCMBIT(UART011_FR_RI, TIOCM_RNG); |
| 303 | #undef TIOCMBIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | return result; |
| 305 | } |
| 306 | |
| 307 | static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 308 | { |
| 309 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 310 | unsigned int cr; |
| 311 | |
| 312 | cr = readw(uap->port.membase + UART011_CR); |
| 313 | |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 314 | #define TIOCMBIT(tiocmbit, uartbit) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | if (mctrl & tiocmbit) \ |
| 316 | cr |= uartbit; \ |
| 317 | else \ |
| 318 | cr &= ~uartbit |
| 319 | |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 320 | TIOCMBIT(TIOCM_RTS, UART011_CR_RTS); |
| 321 | TIOCMBIT(TIOCM_DTR, UART011_CR_DTR); |
| 322 | TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1); |
| 323 | TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2); |
| 324 | TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE); |
Rabin Vincent | 3b43816 | 2010-02-12 06:43:11 +0100 | [diff] [blame] | 325 | |
| 326 | if (uap->autorts) { |
| 327 | /* We need to disable auto-RTS if we want to turn RTS off */ |
| 328 | TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN); |
| 329 | } |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 330 | #undef TIOCMBIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
| 332 | writew(cr, uap->port.membase + UART011_CR); |
| 333 | } |
| 334 | |
| 335 | static void pl011_break_ctl(struct uart_port *port, int break_state) |
| 336 | { |
| 337 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 338 | unsigned long flags; |
| 339 | unsigned int lcr_h; |
| 340 | |
| 341 | spin_lock_irqsave(&uap->port.lock, flags); |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 342 | lcr_h = readw(uap->port.membase + uap->lcrh_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | if (break_state == -1) |
| 344 | lcr_h |= UART01x_LCRH_BRK; |
| 345 | else |
| 346 | lcr_h &= ~UART01x_LCRH_BRK; |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 347 | writew(lcr_h, uap->port.membase + uap->lcrh_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | spin_unlock_irqrestore(&uap->port.lock, flags); |
| 349 | } |
| 350 | |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 351 | #ifdef CONFIG_CONSOLE_POLL |
| 352 | static int pl010_get_poll_char(struct uart_port *port) |
| 353 | { |
| 354 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 355 | unsigned int status; |
| 356 | |
Jason Wessel | f5316b4 | 2010-05-20 21:04:22 -0500 | [diff] [blame] | 357 | status = readw(uap->port.membase + UART01x_FR); |
| 358 | if (status & UART01x_FR_RXFE) |
| 359 | return NO_POLL_CHAR; |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 360 | |
| 361 | return readw(uap->port.membase + UART01x_DR); |
| 362 | } |
| 363 | |
| 364 | static void pl010_put_poll_char(struct uart_port *port, |
| 365 | unsigned char ch) |
| 366 | { |
| 367 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 368 | |
| 369 | while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) |
| 370 | barrier(); |
| 371 | |
| 372 | writew(ch, uap->port.membase + UART01x_DR); |
| 373 | } |
| 374 | |
| 375 | #endif /* CONFIG_CONSOLE_POLL */ |
| 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | static int pl011_startup(struct uart_port *port) |
| 378 | { |
| 379 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 380 | unsigned int cr; |
| 381 | int retval; |
| 382 | |
| 383 | /* |
| 384 | * Try to enable the clock producer. |
| 385 | */ |
| 386 | retval = clk_enable(uap->clk); |
| 387 | if (retval) |
| 388 | goto out; |
| 389 | |
| 390 | uap->port.uartclk = clk_get_rate(uap->clk); |
| 391 | |
| 392 | /* |
| 393 | * Allocate the IRQ |
| 394 | */ |
| 395 | retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap); |
| 396 | if (retval) |
| 397 | goto clk_dis; |
| 398 | |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame^] | 399 | writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * Provoke TX FIFO interrupt into asserting. |
| 403 | */ |
| 404 | cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE; |
| 405 | writew(cr, uap->port.membase + UART011_CR); |
| 406 | writew(0, uap->port.membase + UART011_FBRD); |
| 407 | writew(1, uap->port.membase + UART011_IBRD); |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 408 | writew(0, uap->port.membase + uap->lcrh_rx); |
| 409 | if (uap->lcrh_tx != uap->lcrh_rx) { |
| 410 | int i; |
| 411 | /* |
| 412 | * Wait 10 PCLKs before writing LCRH_TX register, |
| 413 | * to get this delay write read only register 10 times |
| 414 | */ |
| 415 | for (i = 0; i < 10; ++i) |
| 416 | writew(0xff, uap->port.membase + UART011_MIS); |
| 417 | writew(0, uap->port.membase + uap->lcrh_tx); |
| 418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | writew(0, uap->port.membase + UART01x_DR); |
| 420 | while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY) |
| 421 | barrier(); |
| 422 | |
| 423 | cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE; |
| 424 | writew(cr, uap->port.membase + UART011_CR); |
| 425 | |
Russell King | 5063e2c | 2010-12-22 17:09:08 +0000 | [diff] [blame] | 426 | /* Clear pending error interrupts */ |
| 427 | writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS, |
| 428 | uap->port.membase + UART011_ICR); |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* |
| 431 | * initialise the old status of the modem signals |
| 432 | */ |
| 433 | uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY; |
| 434 | |
| 435 | /* |
| 436 | * Finally, enable interrupts |
| 437 | */ |
| 438 | spin_lock_irq(&uap->port.lock); |
| 439 | uap->im = UART011_RXIM | UART011_RTIM; |
| 440 | writew(uap->im, uap->port.membase + UART011_IMSC); |
| 441 | spin_unlock_irq(&uap->port.lock); |
| 442 | |
| 443 | return 0; |
| 444 | |
| 445 | clk_dis: |
| 446 | clk_disable(uap->clk); |
| 447 | out: |
| 448 | return retval; |
| 449 | } |
| 450 | |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 451 | static void pl011_shutdown_channel(struct uart_amba_port *uap, |
| 452 | unsigned int lcrh) |
| 453 | { |
| 454 | unsigned long val; |
| 455 | |
| 456 | val = readw(uap->port.membase + lcrh); |
| 457 | val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN); |
| 458 | writew(val, uap->port.membase + lcrh); |
| 459 | } |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | static void pl011_shutdown(struct uart_port *port) |
| 462 | { |
| 463 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | * disable all interrupts |
| 467 | */ |
| 468 | spin_lock_irq(&uap->port.lock); |
| 469 | uap->im = 0; |
| 470 | writew(uap->im, uap->port.membase + UART011_IMSC); |
| 471 | writew(0xffff, uap->port.membase + UART011_ICR); |
| 472 | spin_unlock_irq(&uap->port.lock); |
| 473 | |
| 474 | /* |
| 475 | * Free the interrupt |
| 476 | */ |
| 477 | free_irq(uap->port.irq, uap); |
| 478 | |
| 479 | /* |
| 480 | * disable the port |
| 481 | */ |
Rabin Vincent | 3b43816 | 2010-02-12 06:43:11 +0100 | [diff] [blame] | 482 | uap->autorts = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR); |
| 484 | |
| 485 | /* |
| 486 | * disable break condition and fifos |
| 487 | */ |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 488 | pl011_shutdown_channel(uap, uap->lcrh_rx); |
| 489 | if (uap->lcrh_rx != uap->lcrh_tx) |
| 490 | pl011_shutdown_channel(uap, uap->lcrh_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * Shut down the clock producer |
| 494 | */ |
| 495 | clk_disable(uap->clk); |
| 496 | } |
| 497 | |
| 498 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 499 | pl011_set_termios(struct uart_port *port, struct ktermios *termios, |
| 500 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Rabin Vincent | 3b43816 | 2010-02-12 06:43:11 +0100 | [diff] [blame] | 502 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | unsigned int lcr_h, old_cr; |
| 504 | unsigned long flags; |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame^] | 505 | unsigned int baud, quot, clkdiv; |
| 506 | |
| 507 | if (uap->vendor->oversampling) |
| 508 | clkdiv = 8; |
| 509 | else |
| 510 | clkdiv = 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | /* |
| 513 | * Ask the core to calculate the divisor for us. |
| 514 | */ |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 515 | baud = uart_get_baud_rate(port, termios, old, 0, |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame^] | 516 | port->uartclk / clkdiv); |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 517 | |
| 518 | if (baud > port->uartclk/16) |
| 519 | quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud); |
| 520 | else |
| 521 | quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | switch (termios->c_cflag & CSIZE) { |
| 524 | case CS5: |
| 525 | lcr_h = UART01x_LCRH_WLEN_5; |
| 526 | break; |
| 527 | case CS6: |
| 528 | lcr_h = UART01x_LCRH_WLEN_6; |
| 529 | break; |
| 530 | case CS7: |
| 531 | lcr_h = UART01x_LCRH_WLEN_7; |
| 532 | break; |
| 533 | default: // CS8 |
| 534 | lcr_h = UART01x_LCRH_WLEN_8; |
| 535 | break; |
| 536 | } |
| 537 | if (termios->c_cflag & CSTOPB) |
| 538 | lcr_h |= UART01x_LCRH_STP2; |
| 539 | if (termios->c_cflag & PARENB) { |
| 540 | lcr_h |= UART01x_LCRH_PEN; |
| 541 | if (!(termios->c_cflag & PARODD)) |
| 542 | lcr_h |= UART01x_LCRH_EPS; |
| 543 | } |
| 544 | if (port->fifosize > 1) |
| 545 | lcr_h |= UART01x_LCRH_FEN; |
| 546 | |
| 547 | spin_lock_irqsave(&port->lock, flags); |
| 548 | |
| 549 | /* |
| 550 | * Update the per-port timeout. |
| 551 | */ |
| 552 | uart_update_timeout(port, termios->c_cflag, baud); |
| 553 | |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 554 | port->read_status_mask = UART011_DR_OE | 255; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | if (termios->c_iflag & INPCK) |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 556 | port->read_status_mask |= UART011_DR_FE | UART011_DR_PE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (termios->c_iflag & (BRKINT | PARMRK)) |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 558 | port->read_status_mask |= UART011_DR_BE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * Characters to ignore |
| 562 | */ |
| 563 | port->ignore_status_mask = 0; |
| 564 | if (termios->c_iflag & IGNPAR) |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 565 | port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | if (termios->c_iflag & IGNBRK) { |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 567 | port->ignore_status_mask |= UART011_DR_BE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | /* |
| 569 | * If we're ignoring parity and break indicators, |
| 570 | * ignore overruns too (for real raw support). |
| 571 | */ |
| 572 | if (termios->c_iflag & IGNPAR) |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 573 | port->ignore_status_mask |= UART011_DR_OE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Ignore all characters if CREAD is not set. |
| 578 | */ |
| 579 | if ((termios->c_cflag & CREAD) == 0) |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 580 | port->ignore_status_mask |= UART_DUMMY_DR_RX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | if (UART_ENABLE_MS(port, termios->c_cflag)) |
| 583 | pl011_enable_ms(port); |
| 584 | |
| 585 | /* first, disable everything */ |
| 586 | old_cr = readw(port->membase + UART011_CR); |
| 587 | writew(0, port->membase + UART011_CR); |
| 588 | |
Rabin Vincent | 3b43816 | 2010-02-12 06:43:11 +0100 | [diff] [blame] | 589 | if (termios->c_cflag & CRTSCTS) { |
| 590 | if (old_cr & UART011_CR_RTS) |
| 591 | old_cr |= UART011_CR_RTSEN; |
| 592 | |
| 593 | old_cr |= UART011_CR_CTSEN; |
| 594 | uap->autorts = true; |
| 595 | } else { |
| 596 | old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN); |
| 597 | uap->autorts = false; |
| 598 | } |
| 599 | |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame^] | 600 | if (uap->vendor->oversampling) { |
| 601 | if (baud > port->uartclk / 16) |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 602 | old_cr |= ST_UART011_CR_OVSFACT; |
| 603 | else |
| 604 | old_cr &= ~ST_UART011_CR_OVSFACT; |
| 605 | } |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | /* Set baud rate */ |
| 608 | writew(quot & 0x3f, port->membase + UART011_FBRD); |
| 609 | writew(quot >> 6, port->membase + UART011_IBRD); |
| 610 | |
| 611 | /* |
| 612 | * ----------v----------v----------v----------v----- |
| 613 | * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L |
| 614 | * ----------^----------^----------^----------^----- |
| 615 | */ |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 616 | writew(lcr_h, port->membase + uap->lcrh_rx); |
| 617 | if (uap->lcrh_rx != uap->lcrh_tx) { |
| 618 | int i; |
| 619 | /* |
| 620 | * Wait 10 PCLKs before writing LCRH_TX register, |
| 621 | * to get this delay write read only register 10 times |
| 622 | */ |
| 623 | for (i = 0; i < 10; ++i) |
| 624 | writew(0xff, uap->port.membase + UART011_MIS); |
| 625 | writew(lcr_h, port->membase + uap->lcrh_tx); |
| 626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | writew(old_cr, port->membase + UART011_CR); |
| 628 | |
| 629 | spin_unlock_irqrestore(&port->lock, flags); |
| 630 | } |
| 631 | |
| 632 | static const char *pl011_type(struct uart_port *port) |
| 633 | { |
Russell King | e8a7ba8 | 2010-12-28 09:16:54 +0000 | [diff] [blame] | 634 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 635 | return uap->port.type == PORT_AMBA ? uap->type : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Release the memory region(s) being used by 'port' |
| 640 | */ |
| 641 | static void pl010_release_port(struct uart_port *port) |
| 642 | { |
| 643 | release_mem_region(port->mapbase, SZ_4K); |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * Request the memory region(s) being used by 'port' |
| 648 | */ |
| 649 | static int pl010_request_port(struct uart_port *port) |
| 650 | { |
| 651 | return request_mem_region(port->mapbase, SZ_4K, "uart-pl011") |
| 652 | != NULL ? 0 : -EBUSY; |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * Configure/autoconfigure the port. |
| 657 | */ |
| 658 | static void pl010_config_port(struct uart_port *port, int flags) |
| 659 | { |
| 660 | if (flags & UART_CONFIG_TYPE) { |
| 661 | port->type = PORT_AMBA; |
| 662 | pl010_request_port(port); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * verify the new serial_struct (for TIOCSSERIAL). |
| 668 | */ |
| 669 | static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 670 | { |
| 671 | int ret = 0; |
| 672 | if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA) |
| 673 | ret = -EINVAL; |
Yinghai Lu | a62c413 | 2008-08-19 20:49:55 -0700 | [diff] [blame] | 674 | if (ser->irq < 0 || ser->irq >= nr_irqs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | ret = -EINVAL; |
| 676 | if (ser->baud_base < 9600) |
| 677 | ret = -EINVAL; |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | static struct uart_ops amba_pl011_pops = { |
| 682 | .tx_empty = pl01x_tx_empty, |
| 683 | .set_mctrl = pl011_set_mctrl, |
| 684 | .get_mctrl = pl01x_get_mctrl, |
| 685 | .stop_tx = pl011_stop_tx, |
| 686 | .start_tx = pl011_start_tx, |
| 687 | .stop_rx = pl011_stop_rx, |
| 688 | .enable_ms = pl011_enable_ms, |
| 689 | .break_ctl = pl011_break_ctl, |
| 690 | .startup = pl011_startup, |
| 691 | .shutdown = pl011_shutdown, |
| 692 | .set_termios = pl011_set_termios, |
| 693 | .type = pl011_type, |
| 694 | .release_port = pl010_release_port, |
| 695 | .request_port = pl010_request_port, |
| 696 | .config_port = pl010_config_port, |
| 697 | .verify_port = pl010_verify_port, |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 698 | #ifdef CONFIG_CONSOLE_POLL |
| 699 | .poll_get_char = pl010_get_poll_char, |
| 700 | .poll_put_char = pl010_put_poll_char, |
| 701 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | }; |
| 703 | |
| 704 | static struct uart_amba_port *amba_ports[UART_NR]; |
| 705 | |
| 706 | #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE |
| 707 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 708 | static void pl011_console_putchar(struct uart_port *port, int ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 710 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 712 | while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) |
| 713 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | writew(ch, uap->port.membase + UART01x_DR); |
| 715 | } |
| 716 | |
| 717 | static void |
| 718 | pl011_console_write(struct console *co, const char *s, unsigned int count) |
| 719 | { |
| 720 | struct uart_amba_port *uap = amba_ports[co->index]; |
| 721 | unsigned int status, old_cr, new_cr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
| 723 | clk_enable(uap->clk); |
| 724 | |
| 725 | /* |
| 726 | * First save the CR then disable the interrupts |
| 727 | */ |
| 728 | old_cr = readw(uap->port.membase + UART011_CR); |
| 729 | new_cr = old_cr & ~UART011_CR_CTSEN; |
| 730 | new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE; |
| 731 | writew(new_cr, uap->port.membase + UART011_CR); |
| 732 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 733 | uart_console_write(&uap->port, s, count, pl011_console_putchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Finally, wait for transmitter to become empty |
| 737 | * and restore the TCR |
| 738 | */ |
| 739 | do { |
| 740 | status = readw(uap->port.membase + UART01x_FR); |
| 741 | } while (status & UART01x_FR_BUSY); |
| 742 | writew(old_cr, uap->port.membase + UART011_CR); |
| 743 | |
| 744 | clk_disable(uap->clk); |
| 745 | } |
| 746 | |
| 747 | static void __init |
| 748 | pl011_console_get_options(struct uart_amba_port *uap, int *baud, |
| 749 | int *parity, int *bits) |
| 750 | { |
| 751 | if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) { |
| 752 | unsigned int lcr_h, ibrd, fbrd; |
| 753 | |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 754 | lcr_h = readw(uap->port.membase + uap->lcrh_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
| 756 | *parity = 'n'; |
| 757 | if (lcr_h & UART01x_LCRH_PEN) { |
| 758 | if (lcr_h & UART01x_LCRH_EPS) |
| 759 | *parity = 'e'; |
| 760 | else |
| 761 | *parity = 'o'; |
| 762 | } |
| 763 | |
| 764 | if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7) |
| 765 | *bits = 7; |
| 766 | else |
| 767 | *bits = 8; |
| 768 | |
| 769 | ibrd = readw(uap->port.membase + UART011_IBRD); |
| 770 | fbrd = readw(uap->port.membase + UART011_FBRD); |
| 771 | |
| 772 | *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd); |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 773 | |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame^] | 774 | if (uap->vendor->oversampling) { |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 775 | if (readw(uap->port.membase + UART011_CR) |
| 776 | & ST_UART011_CR_OVSFACT) |
| 777 | *baud *= 2; |
| 778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
| 782 | static int __init pl011_console_setup(struct console *co, char *options) |
| 783 | { |
| 784 | struct uart_amba_port *uap; |
| 785 | int baud = 38400; |
| 786 | int bits = 8; |
| 787 | int parity = 'n'; |
| 788 | int flow = 'n'; |
| 789 | |
| 790 | /* |
| 791 | * Check whether an invalid uart number has been specified, and |
| 792 | * if so, search for the first available port that does have |
| 793 | * console support. |
| 794 | */ |
| 795 | if (co->index >= UART_NR) |
| 796 | co->index = 0; |
| 797 | uap = amba_ports[co->index]; |
Russell King | d28122a | 2007-01-22 18:59:42 +0000 | [diff] [blame] | 798 | if (!uap) |
| 799 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
| 801 | uap->port.uartclk = clk_get_rate(uap->clk); |
| 802 | |
| 803 | if (options) |
| 804 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 805 | else |
| 806 | pl011_console_get_options(uap, &baud, &parity, &bits); |
| 807 | |
| 808 | return uart_set_options(&uap->port, co, baud, parity, bits, flow); |
| 809 | } |
| 810 | |
Vincent Sanders | 2d93486 | 2005-09-14 22:36:03 +0100 | [diff] [blame] | 811 | static struct uart_driver amba_reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | static struct console amba_console = { |
| 813 | .name = "ttyAMA", |
| 814 | .write = pl011_console_write, |
| 815 | .device = uart_console_device, |
| 816 | .setup = pl011_console_setup, |
| 817 | .flags = CON_PRINTBUFFER, |
| 818 | .index = -1, |
| 819 | .data = &amba_reg, |
| 820 | }; |
| 821 | |
| 822 | #define AMBA_CONSOLE (&amba_console) |
| 823 | #else |
| 824 | #define AMBA_CONSOLE NULL |
| 825 | #endif |
| 826 | |
| 827 | static struct uart_driver amba_reg = { |
| 828 | .owner = THIS_MODULE, |
| 829 | .driver_name = "ttyAMA", |
| 830 | .dev_name = "ttyAMA", |
| 831 | .major = SERIAL_AMBA_MAJOR, |
| 832 | .minor = SERIAL_AMBA_MINOR, |
| 833 | .nr = UART_NR, |
| 834 | .cons = AMBA_CONSOLE, |
| 835 | }; |
| 836 | |
Alessandro Rubini | 03fbdb1 | 2009-05-20 22:39:08 +0100 | [diff] [blame] | 837 | static int pl011_probe(struct amba_device *dev, struct amba_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
| 839 | struct uart_amba_port *uap; |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 840 | struct vendor_data *vendor = id->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | void __iomem *base; |
| 842 | int i, ret; |
| 843 | |
| 844 | for (i = 0; i < ARRAY_SIZE(amba_ports); i++) |
| 845 | if (amba_ports[i] == NULL) |
| 846 | break; |
| 847 | |
| 848 | if (i == ARRAY_SIZE(amba_ports)) { |
| 849 | ret = -EBUSY; |
| 850 | goto out; |
| 851 | } |
| 852 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 853 | uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | if (uap == NULL) { |
| 855 | ret = -ENOMEM; |
| 856 | goto out; |
| 857 | } |
| 858 | |
Linus Walleij | dc890c2 | 2009-06-07 23:27:31 +0100 | [diff] [blame] | 859 | base = ioremap(dev->res.start, resource_size(&dev->res)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | if (!base) { |
| 861 | ret = -ENOMEM; |
| 862 | goto free; |
| 863 | } |
| 864 | |
Russell King | ee569c4 | 2008-11-30 17:38:14 +0000 | [diff] [blame] | 865 | uap->clk = clk_get(&dev->dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | if (IS_ERR(uap->clk)) { |
| 867 | ret = PTR_ERR(uap->clk); |
| 868 | goto unmap; |
| 869 | } |
| 870 | |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame^] | 871 | uap->vendor = vendor; |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 872 | uap->lcrh_rx = vendor->lcrh_rx; |
| 873 | uap->lcrh_tx = vendor->lcrh_tx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | uap->port.dev = &dev->dev; |
| 875 | uap->port.mapbase = dev->res.start; |
| 876 | uap->port.membase = base; |
| 877 | uap->port.iotype = UPIO_MEM; |
| 878 | uap->port.irq = dev->irq[0]; |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 879 | uap->port.fifosize = vendor->fifosize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | uap->port.ops = &amba_pl011_pops; |
| 881 | uap->port.flags = UPF_BOOT_AUTOCONF; |
| 882 | uap->port.line = i; |
| 883 | |
Russell King | e8a7ba8 | 2010-12-28 09:16:54 +0000 | [diff] [blame] | 884 | snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev)); |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | amba_ports[i] = uap; |
| 887 | |
| 888 | amba_set_drvdata(dev, uap); |
| 889 | ret = uart_add_one_port(&amba_reg, &uap->port); |
| 890 | if (ret) { |
| 891 | amba_set_drvdata(dev, NULL); |
| 892 | amba_ports[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | clk_put(uap->clk); |
| 894 | unmap: |
| 895 | iounmap(base); |
| 896 | free: |
| 897 | kfree(uap); |
| 898 | } |
| 899 | out: |
| 900 | return ret; |
| 901 | } |
| 902 | |
| 903 | static int pl011_remove(struct amba_device *dev) |
| 904 | { |
| 905 | struct uart_amba_port *uap = amba_get_drvdata(dev); |
| 906 | int i; |
| 907 | |
| 908 | amba_set_drvdata(dev, NULL); |
| 909 | |
| 910 | uart_remove_one_port(&amba_reg, &uap->port); |
| 911 | |
| 912 | for (i = 0; i < ARRAY_SIZE(amba_ports); i++) |
| 913 | if (amba_ports[i] == uap) |
| 914 | amba_ports[i] = NULL; |
| 915 | |
| 916 | iounmap(uap->port.membase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | clk_put(uap->clk); |
| 918 | kfree(uap); |
| 919 | return 0; |
| 920 | } |
| 921 | |
Leo Chen | b736b89 | 2009-07-28 23:43:33 +0100 | [diff] [blame] | 922 | #ifdef CONFIG_PM |
| 923 | static int pl011_suspend(struct amba_device *dev, pm_message_t state) |
| 924 | { |
| 925 | struct uart_amba_port *uap = amba_get_drvdata(dev); |
| 926 | |
| 927 | if (!uap) |
| 928 | return -EINVAL; |
| 929 | |
| 930 | return uart_suspend_port(&amba_reg, &uap->port); |
| 931 | } |
| 932 | |
| 933 | static int pl011_resume(struct amba_device *dev) |
| 934 | { |
| 935 | struct uart_amba_port *uap = amba_get_drvdata(dev); |
| 936 | |
| 937 | if (!uap) |
| 938 | return -EINVAL; |
| 939 | |
| 940 | return uart_resume_port(&amba_reg, &uap->port); |
| 941 | } |
| 942 | #endif |
| 943 | |
Russell King | 2c39c9e | 2010-07-27 08:50:16 +0100 | [diff] [blame] | 944 | static struct amba_id pl011_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | { |
| 946 | .id = 0x00041011, |
| 947 | .mask = 0x000fffff, |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 948 | .data = &vendor_arm, |
| 949 | }, |
| 950 | { |
| 951 | .id = 0x00380802, |
| 952 | .mask = 0x00ffffff, |
| 953 | .data = &vendor_st, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | }, |
| 955 | { 0, 0 }, |
| 956 | }; |
| 957 | |
| 958 | static struct amba_driver pl011_driver = { |
| 959 | .drv = { |
| 960 | .name = "uart-pl011", |
| 961 | }, |
| 962 | .id_table = pl011_ids, |
| 963 | .probe = pl011_probe, |
| 964 | .remove = pl011_remove, |
Leo Chen | b736b89 | 2009-07-28 23:43:33 +0100 | [diff] [blame] | 965 | #ifdef CONFIG_PM |
| 966 | .suspend = pl011_suspend, |
| 967 | .resume = pl011_resume, |
| 968 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | }; |
| 970 | |
| 971 | static int __init pl011_init(void) |
| 972 | { |
| 973 | int ret; |
| 974 | printk(KERN_INFO "Serial: AMBA PL011 UART driver\n"); |
| 975 | |
| 976 | ret = uart_register_driver(&amba_reg); |
| 977 | if (ret == 0) { |
| 978 | ret = amba_driver_register(&pl011_driver); |
| 979 | if (ret) |
| 980 | uart_unregister_driver(&amba_reg); |
| 981 | } |
| 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | static void __exit pl011_exit(void) |
| 986 | { |
| 987 | amba_driver_unregister(&pl011_driver); |
| 988 | uart_unregister_driver(&amba_reg); |
| 989 | } |
| 990 | |
Alessandro Rubini | 4dd9e74 | 2009-05-05 05:54:13 +0100 | [diff] [blame] | 991 | /* |
| 992 | * While this can be a module, if builtin it's most likely the console |
| 993 | * So let's leave module_exit but move module_init to an earlier place |
| 994 | */ |
| 995 | arch_initcall(pl011_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | module_exit(pl011_exit); |
| 997 | |
| 998 | MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd"); |
| 999 | MODULE_DESCRIPTION("ARM AMBA serial port driver"); |
| 1000 | MODULE_LICENSE("GPL"); |