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 | * |
| 25 | * $Id: amba.c,v 1.41 2002/07/28 10:03:27 rmk Exp $ |
| 26 | * |
| 27 | * This is a generic driver for ARM AMBA-type serial ports. They |
| 28 | * have a lot of 16550-like features, but are not register compatible. |
| 29 | * Note that although they do have CTS, DCD and DSR inputs, they do |
| 30 | * not have an RI input, nor do they have DTR or RTS outputs. If |
| 31 | * required, these have to be supplied via some other means (eg, GPIO) |
| 32 | * and hooked into this driver. |
| 33 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #if defined(CONFIG_SERIAL_AMBA_PL010_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 36 | #define SUPPORT_SYSRQ |
| 37 | #endif |
| 38 | |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/ioport.h> |
| 41 | #include <linux/init.h> |
| 42 | #include <linux/console.h> |
| 43 | #include <linux/sysrq.h> |
| 44 | #include <linux/device.h> |
| 45 | #include <linux/tty.h> |
| 46 | #include <linux/tty_flip.h> |
| 47 | #include <linux/serial_core.h> |
| 48 | #include <linux/serial.h> |
Russell King | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 49 | #include <linux/amba/bus.h> |
| 50 | #include <linux/amba/serial.h> |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 51 | #include <linux/clk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Lennert Buytenhek | 4faf4e0 | 2006-06-20 19:24:07 +0100 | [diff] [blame] | 55 | #define UART_NR 8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #define SERIAL_AMBA_MAJOR 204 |
| 58 | #define SERIAL_AMBA_MINOR 16 |
| 59 | #define SERIAL_AMBA_NR UART_NR |
| 60 | |
| 61 | #define AMBA_ISR_PASS_LIMIT 256 |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0) |
| 64 | #define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 66 | #define UART_DUMMY_RSR_RX 256 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #define UART_PORT_SIZE 64 |
| 68 | |
| 69 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * We wrap our port structure around the generic uart_port. |
| 71 | */ |
| 72 | struct uart_amba_port { |
| 73 | struct uart_port port; |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 74 | struct clk *clk; |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 75 | struct amba_device *dev; |
| 76 | struct amba_pl010_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | unsigned int old_status; |
| 78 | }; |
| 79 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 80 | static void pl010_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 82 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | unsigned int cr; |
| 84 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 85 | cr = readb(uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | cr &= ~UART010_CR_TIE; |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 87 | writel(cr, uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 90 | static void pl010_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 92 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | unsigned int cr; |
| 94 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 95 | cr = readb(uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | cr |= UART010_CR_TIE; |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 97 | writel(cr, uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void pl010_stop_rx(struct uart_port *port) |
| 101 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 102 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | unsigned int cr; |
| 104 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 105 | cr = readb(uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | cr &= ~(UART010_CR_RIE | UART010_CR_RTIE); |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 107 | writel(cr, uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void pl010_enable_ms(struct uart_port *port) |
| 111 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 112 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | unsigned int cr; |
| 114 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 115 | cr = readb(uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | cr |= UART010_CR_MSIE; |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 117 | writel(cr, uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 120 | static void pl010_rx_chars(struct uart_amba_port *uap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 122 | struct tty_struct *tty = uap->port.info->tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | unsigned int status, ch, flag, rsr, max_count = 256; |
| 124 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 125 | status = readb(uap->port.membase + UART01x_FR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | while (UART_RX_DATA(status) && max_count--) { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 127 | ch = readb(uap->port.membase + UART01x_DR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | flag = TTY_NORMAL; |
| 129 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 130 | uap->port.icount.rx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Note that the error handling code is |
| 134 | * out of the main execution path |
| 135 | */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 136 | rsr = readb(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX; |
Russell King | 4584928 | 2005-04-26 15:29:44 +0100 | [diff] [blame] | 137 | if (unlikely(rsr & UART01x_RSR_ANY)) { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 138 | writel(0, uap->port.membase + UART01x_ECR); |
Lennert Buytenhek | a4ed06a | 2006-12-06 20:39:57 -0800 | [diff] [blame] | 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (rsr & UART01x_RSR_BE) { |
| 141 | rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE); |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 142 | uap->port.icount.brk++; |
| 143 | if (uart_handle_break(&uap->port)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | goto ignore_char; |
| 145 | } else if (rsr & UART01x_RSR_PE) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 146 | uap->port.icount.parity++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | else if (rsr & UART01x_RSR_FE) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 148 | uap->port.icount.frame++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (rsr & UART01x_RSR_OE) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 150 | uap->port.icount.overrun++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 152 | rsr &= uap->port.read_status_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | if (rsr & UART01x_RSR_BE) |
| 155 | flag = TTY_BREAK; |
| 156 | else if (rsr & UART01x_RSR_PE) |
| 157 | flag = TTY_PARITY; |
| 158 | else if (rsr & UART01x_RSR_FE) |
| 159 | flag = TTY_FRAME; |
| 160 | } |
| 161 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 162 | if (uart_handle_sysrq_char(&uap->port, ch)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | goto ignore_char; |
| 164 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 165 | uart_insert_char(&uap->port, rsr, UART01x_RSR_OE, ch, flag); |
Russell King | 05ab301 | 2005-05-09 23:21:59 +0100 | [diff] [blame] | 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | ignore_char: |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 168 | status = readb(uap->port.membase + UART01x_FR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
Russell King | db002b8 | 2007-06-05 19:39:49 +0100 | [diff] [blame] | 170 | spin_unlock(&uap->port.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | tty_flip_buffer_push(tty); |
Russell King | db002b8 | 2007-06-05 19:39:49 +0100 | [diff] [blame] | 172 | spin_lock(&uap->port.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 175 | static void pl010_tx_chars(struct uart_amba_port *uap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 177 | struct circ_buf *xmit = &uap->port.info->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | int count; |
| 179 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 180 | if (uap->port.x_char) { |
| 181 | writel(uap->port.x_char, uap->port.membase + UART01x_DR); |
| 182 | uap->port.icount.tx++; |
| 183 | uap->port.x_char = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | return; |
| 185 | } |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 186 | if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) { |
| 187 | pl010_stop_tx(&uap->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 191 | count = uap->port.fifosize >> 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | do { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 193 | writel(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 195 | uap->port.icount.tx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (uart_circ_empty(xmit)) |
| 197 | break; |
| 198 | } while (--count > 0); |
| 199 | |
| 200 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 201 | uart_write_wakeup(&uap->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | if (uart_circ_empty(xmit)) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 204 | pl010_stop_tx(&uap->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 207 | static void pl010_modem_status(struct uart_amba_port *uap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | unsigned int status, delta; |
| 210 | |
Russell King | 98639a6 | 2006-03-25 21:30:11 +0000 | [diff] [blame] | 211 | writel(0, uap->port.membase + UART010_ICR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Russell King | 98639a6 | 2006-03-25 21:30:11 +0000 | [diff] [blame] | 213 | status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | delta = status ^ uap->old_status; |
| 216 | uap->old_status = status; |
| 217 | |
| 218 | if (!delta) |
| 219 | return; |
| 220 | |
| 221 | if (delta & UART01x_FR_DCD) |
| 222 | uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD); |
| 223 | |
| 224 | if (delta & UART01x_FR_DSR) |
| 225 | uap->port.icount.dsr++; |
| 226 | |
| 227 | if (delta & UART01x_FR_CTS) |
| 228 | uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS); |
| 229 | |
| 230 | wake_up_interruptible(&uap->port.info->delta_msr_wait); |
| 231 | } |
| 232 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 233 | static irqreturn_t pl010_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 235 | struct uart_amba_port *uap = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT; |
| 237 | int handled = 0; |
| 238 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 239 | spin_lock(&uap->port.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 241 | status = readb(uap->port.membase + UART010_IIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (status) { |
| 243 | do { |
| 244 | if (status & (UART010_IIR_RTIS | UART010_IIR_RIS)) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 245 | pl010_rx_chars(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (status & UART010_IIR_MIS) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 247 | pl010_modem_status(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | if (status & UART010_IIR_TIS) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 249 | pl010_tx_chars(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | if (pass_counter-- == 0) |
| 252 | break; |
| 253 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 254 | status = readb(uap->port.membase + UART010_IIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS | |
| 256 | UART010_IIR_TIS)); |
| 257 | handled = 1; |
| 258 | } |
| 259 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 260 | spin_unlock(&uap->port.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | return IRQ_RETVAL(handled); |
| 263 | } |
| 264 | |
| 265 | static unsigned int pl010_tx_empty(struct uart_port *port) |
| 266 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 267 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 268 | unsigned int status = readb(uap->port.membase + UART01x_FR); |
| 269 | return status & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static unsigned int pl010_get_mctrl(struct uart_port *port) |
| 273 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 274 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | unsigned int result = 0; |
| 276 | unsigned int status; |
| 277 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 278 | status = readb(uap->port.membase + UART01x_FR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | if (status & UART01x_FR_DCD) |
| 280 | result |= TIOCM_CAR; |
| 281 | if (status & UART01x_FR_DSR) |
| 282 | result |= TIOCM_DSR; |
| 283 | if (status & UART01x_FR_CTS) |
| 284 | result |= TIOCM_CTS; |
| 285 | |
| 286 | return result; |
| 287 | } |
| 288 | |
| 289 | static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 290 | { |
| 291 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 293 | if (uap->data) |
| 294 | uap->data->set_mctrl(uap->dev, uap->port.membase, mctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void pl010_break_ctl(struct uart_port *port, int break_state) |
| 298 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 299 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | unsigned long flags; |
| 301 | unsigned int lcr_h; |
| 302 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 303 | spin_lock_irqsave(&uap->port.lock, flags); |
| 304 | lcr_h = readb(uap->port.membase + UART010_LCRH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | if (break_state == -1) |
| 306 | lcr_h |= UART01x_LCRH_BRK; |
| 307 | else |
| 308 | lcr_h &= ~UART01x_LCRH_BRK; |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 309 | writel(lcr_h, uap->port.membase + UART010_LCRH); |
| 310 | spin_unlock_irqrestore(&uap->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static int pl010_startup(struct uart_port *port) |
| 314 | { |
| 315 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 316 | int retval; |
| 317 | |
| 318 | /* |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 319 | * Try to enable the clock producer. |
| 320 | */ |
| 321 | retval = clk_enable(uap->clk); |
| 322 | if (retval) |
| 323 | goto out; |
| 324 | |
| 325 | uap->port.uartclk = clk_get_rate(uap->clk); |
| 326 | |
| 327 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | * Allocate the IRQ |
| 329 | */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 330 | retval = request_irq(uap->port.irq, pl010_int, 0, "uart-pl010", uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if (retval) |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 332 | goto clk_dis; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * initialise the old status of the modem signals |
| 336 | */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 337 | uap->old_status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Finally, enable interrupts |
| 341 | */ |
Russell King | 98639a6 | 2006-03-25 21:30:11 +0000 | [diff] [blame] | 342 | writel(UART01x_CR_UARTEN | UART010_CR_RIE | UART010_CR_RTIE, |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 343 | uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | return 0; |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 346 | |
| 347 | clk_dis: |
| 348 | clk_disable(uap->clk); |
| 349 | out: |
| 350 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void pl010_shutdown(struct uart_port *port) |
| 354 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 355 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
| 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | /* |
| 358 | * Free the interrupt |
| 359 | */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 360 | free_irq(uap->port.irq, uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * disable all interrupts, disable the port |
| 364 | */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 365 | writel(0, uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
| 367 | /* disable break condition and fifos */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 368 | writel(readb(uap->port.membase + UART010_LCRH) & |
Russell King | 98639a6 | 2006-03-25 21:30:11 +0000 | [diff] [blame] | 369 | ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN), |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 370 | uap->port.membase + UART010_LCRH); |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * Shut down the clock producer |
| 374 | */ |
| 375 | clk_disable(uap->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 379 | pl010_set_termios(struct uart_port *port, struct ktermios *termios, |
| 380 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 382 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | unsigned int lcr_h, old_cr; |
| 384 | unsigned long flags; |
| 385 | unsigned int baud, quot; |
| 386 | |
| 387 | /* |
| 388 | * Ask the core to calculate the divisor for us. |
| 389 | */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 390 | baud = uart_get_baud_rate(port, termios, old, 0, uap->port.uartclk/16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | quot = uart_get_divisor(port, baud); |
| 392 | |
| 393 | switch (termios->c_cflag & CSIZE) { |
| 394 | case CS5: |
| 395 | lcr_h = UART01x_LCRH_WLEN_5; |
| 396 | break; |
| 397 | case CS6: |
| 398 | lcr_h = UART01x_LCRH_WLEN_6; |
| 399 | break; |
| 400 | case CS7: |
| 401 | lcr_h = UART01x_LCRH_WLEN_7; |
| 402 | break; |
| 403 | default: // CS8 |
| 404 | lcr_h = UART01x_LCRH_WLEN_8; |
| 405 | break; |
| 406 | } |
| 407 | if (termios->c_cflag & CSTOPB) |
| 408 | lcr_h |= UART01x_LCRH_STP2; |
| 409 | if (termios->c_cflag & PARENB) { |
| 410 | lcr_h |= UART01x_LCRH_PEN; |
| 411 | if (!(termios->c_cflag & PARODD)) |
| 412 | lcr_h |= UART01x_LCRH_EPS; |
| 413 | } |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 414 | if (uap->port.fifosize > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | lcr_h |= UART01x_LCRH_FEN; |
| 416 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 417 | spin_lock_irqsave(&uap->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | /* |
| 420 | * Update the per-port timeout. |
| 421 | */ |
| 422 | uart_update_timeout(port, termios->c_cflag, baud); |
| 423 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 424 | uap->port.read_status_mask = UART01x_RSR_OE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | if (termios->c_iflag & INPCK) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 426 | uap->port.read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (termios->c_iflag & (BRKINT | PARMRK)) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 428 | uap->port.read_status_mask |= UART01x_RSR_BE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * Characters to ignore |
| 432 | */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 433 | uap->port.ignore_status_mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | if (termios->c_iflag & IGNPAR) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 435 | uap->port.ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | if (termios->c_iflag & IGNBRK) { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 437 | uap->port.ignore_status_mask |= UART01x_RSR_BE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | /* |
| 439 | * If we're ignoring parity and break indicators, |
| 440 | * ignore overruns too (for real raw support). |
| 441 | */ |
| 442 | if (termios->c_iflag & IGNPAR) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 443 | uap->port.ignore_status_mask |= UART01x_RSR_OE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Ignore all characters if CREAD is not set. |
| 448 | */ |
| 449 | if ((termios->c_cflag & CREAD) == 0) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 450 | uap->port.ignore_status_mask |= UART_DUMMY_RSR_RX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | /* first, disable everything */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 453 | old_cr = readb(uap->port.membase + UART010_CR) & ~UART010_CR_MSIE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | if (UART_ENABLE_MS(port, termios->c_cflag)) |
| 456 | old_cr |= UART010_CR_MSIE; |
| 457 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 458 | writel(0, uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | /* Set baud rate */ |
| 461 | quot -= 1; |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 462 | writel((quot & 0xf00) >> 8, uap->port.membase + UART010_LCRM); |
| 463 | writel(quot & 0xff, uap->port.membase + UART010_LCRL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | * ----------v----------v----------v----------v----- |
| 467 | * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L |
| 468 | * ----------^----------^----------^----------^----- |
| 469 | */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 470 | writel(lcr_h, uap->port.membase + UART010_LCRH); |
| 471 | writel(old_cr, uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 473 | spin_unlock_irqrestore(&uap->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static const char *pl010_type(struct uart_port *port) |
| 477 | { |
| 478 | return port->type == PORT_AMBA ? "AMBA" : NULL; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Release the memory region(s) being used by 'port' |
| 483 | */ |
| 484 | static void pl010_release_port(struct uart_port *port) |
| 485 | { |
| 486 | release_mem_region(port->mapbase, UART_PORT_SIZE); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Request the memory region(s) being used by 'port' |
| 491 | */ |
| 492 | static int pl010_request_port(struct uart_port *port) |
| 493 | { |
| 494 | return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010") |
| 495 | != NULL ? 0 : -EBUSY; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Configure/autoconfigure the port. |
| 500 | */ |
| 501 | static void pl010_config_port(struct uart_port *port, int flags) |
| 502 | { |
| 503 | if (flags & UART_CONFIG_TYPE) { |
| 504 | port->type = PORT_AMBA; |
| 505 | pl010_request_port(port); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * verify the new serial_struct (for TIOCSSERIAL). |
| 511 | */ |
| 512 | static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 513 | { |
| 514 | int ret = 0; |
| 515 | if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA) |
| 516 | ret = -EINVAL; |
| 517 | if (ser->irq < 0 || ser->irq >= NR_IRQS) |
| 518 | ret = -EINVAL; |
| 519 | if (ser->baud_base < 9600) |
| 520 | ret = -EINVAL; |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | static struct uart_ops amba_pl010_pops = { |
| 525 | .tx_empty = pl010_tx_empty, |
| 526 | .set_mctrl = pl010_set_mctrl, |
| 527 | .get_mctrl = pl010_get_mctrl, |
| 528 | .stop_tx = pl010_stop_tx, |
| 529 | .start_tx = pl010_start_tx, |
| 530 | .stop_rx = pl010_stop_rx, |
| 531 | .enable_ms = pl010_enable_ms, |
| 532 | .break_ctl = pl010_break_ctl, |
| 533 | .startup = pl010_startup, |
| 534 | .shutdown = pl010_shutdown, |
| 535 | .set_termios = pl010_set_termios, |
| 536 | .type = pl010_type, |
| 537 | .release_port = pl010_release_port, |
| 538 | .request_port = pl010_request_port, |
| 539 | .config_port = pl010_config_port, |
| 540 | .verify_port = pl010_verify_port, |
| 541 | }; |
| 542 | |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 543 | static struct uart_amba_port *amba_ports[UART_NR]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
| 545 | #ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE |
| 546 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 547 | static void pl010_console_putchar(struct uart_port *port, int ch) |
| 548 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 549 | struct uart_amba_port *uap = (struct uart_amba_port *)port; |
Russell King | 98639a6 | 2006-03-25 21:30:11 +0000 | [diff] [blame] | 550 | unsigned int status; |
| 551 | |
| 552 | do { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 553 | status = readb(uap->port.membase + UART01x_FR); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 554 | barrier(); |
Russell King | 98639a6 | 2006-03-25 21:30:11 +0000 | [diff] [blame] | 555 | } while (!UART_TX_READY(status)); |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 556 | writel(ch, uap->port.membase + UART01x_DR); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | static void |
| 560 | pl010_console_write(struct console *co, const char *s, unsigned int count) |
| 561 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 562 | struct uart_amba_port *uap = amba_ports[co->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | unsigned int status, old_cr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 565 | clk_enable(uap->clk); |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | /* |
| 568 | * First save the CR then disable the interrupts |
| 569 | */ |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 570 | old_cr = readb(uap->port.membase + UART010_CR); |
| 571 | writel(UART01x_CR_UARTEN, uap->port.membase + UART010_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 573 | uart_console_write(&uap->port, s, count, pl010_console_putchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
| 575 | /* |
| 576 | * Finally, wait for transmitter to become empty |
| 577 | * and restore the TCR |
| 578 | */ |
| 579 | do { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 580 | status = readb(uap->port.membase + UART01x_FR); |
Russell King | 98639a6 | 2006-03-25 21:30:11 +0000 | [diff] [blame] | 581 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } while (status & UART01x_FR_BUSY); |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 583 | writel(old_cr, uap->port.membase + UART010_CR); |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 584 | |
| 585 | clk_disable(uap->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static void __init |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 589 | pl010_console_get_options(struct uart_amba_port *uap, int *baud, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | int *parity, int *bits) |
| 591 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 592 | if (readb(uap->port.membase + UART010_CR) & UART01x_CR_UARTEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | unsigned int lcr_h, quot; |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 594 | lcr_h = readb(uap->port.membase + UART010_LCRH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | *parity = 'n'; |
| 597 | if (lcr_h & UART01x_LCRH_PEN) { |
| 598 | if (lcr_h & UART01x_LCRH_EPS) |
| 599 | *parity = 'e'; |
| 600 | else |
| 601 | *parity = 'o'; |
| 602 | } |
| 603 | |
| 604 | if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7) |
| 605 | *bits = 7; |
| 606 | else |
| 607 | *bits = 8; |
| 608 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 609 | quot = readb(uap->port.membase + UART010_LCRL) | |
| 610 | readb(uap->port.membase + UART010_LCRM) << 8; |
| 611 | *baud = uap->port.uartclk / (16 * (quot + 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | static int __init pl010_console_setup(struct console *co, char *options) |
| 616 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 617 | struct uart_amba_port *uap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | int baud = 38400; |
| 619 | int bits = 8; |
| 620 | int parity = 'n'; |
| 621 | int flow = 'n'; |
| 622 | |
| 623 | /* |
| 624 | * Check whether an invalid uart number has been specified, and |
| 625 | * if so, search for the first available port that does have |
| 626 | * console support. |
| 627 | */ |
| 628 | if (co->index >= UART_NR) |
| 629 | co->index = 0; |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 630 | uap = amba_ports[co->index]; |
| 631 | if (!uap) |
Russell King | d28122a | 2007-01-22 18:59:42 +0000 | [diff] [blame] | 632 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 634 | uap->port.uartclk = clk_get_rate(uap->clk); |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | if (options) |
| 637 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 638 | else |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 639 | pl010_console_get_options(uap, &baud, &parity, &bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 641 | return uart_set_options(&uap->port, co, baud, parity, bits, flow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Vincent Sanders | 2d93486 | 2005-09-14 22:36:03 +0100 | [diff] [blame] | 644 | static struct uart_driver amba_reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | static struct console amba_console = { |
| 646 | .name = "ttyAM", |
| 647 | .write = pl010_console_write, |
| 648 | .device = uart_console_device, |
| 649 | .setup = pl010_console_setup, |
| 650 | .flags = CON_PRINTBUFFER, |
| 651 | .index = -1, |
| 652 | .data = &amba_reg, |
| 653 | }; |
| 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | #define AMBA_CONSOLE &amba_console |
| 656 | #else |
| 657 | #define AMBA_CONSOLE NULL |
| 658 | #endif |
| 659 | |
| 660 | static struct uart_driver amba_reg = { |
| 661 | .owner = THIS_MODULE, |
| 662 | .driver_name = "ttyAM", |
| 663 | .dev_name = "ttyAM", |
| 664 | .major = SERIAL_AMBA_MAJOR, |
| 665 | .minor = SERIAL_AMBA_MINOR, |
| 666 | .nr = UART_NR, |
| 667 | .cons = AMBA_CONSOLE, |
| 668 | }; |
| 669 | |
| 670 | static int pl010_probe(struct amba_device *dev, void *id) |
| 671 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 672 | struct uart_amba_port *uap; |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 673 | void __iomem *base; |
| 674 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 676 | for (i = 0; i < ARRAY_SIZE(amba_ports); i++) |
| 677 | if (amba_ports[i] == NULL) |
| 678 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 680 | if (i == ARRAY_SIZE(amba_ports)) { |
| 681 | ret = -EBUSY; |
| 682 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 685 | uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL); |
| 686 | if (!uap) { |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 687 | ret = -ENOMEM; |
| 688 | goto out; |
| 689 | } |
| 690 | |
| 691 | base = ioremap(dev->res.start, PAGE_SIZE); |
| 692 | if (!base) { |
| 693 | ret = -ENOMEM; |
| 694 | goto free; |
| 695 | } |
| 696 | |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 697 | uap->clk = clk_get(&dev->dev, "UARTCLK"); |
| 698 | if (IS_ERR(uap->clk)) { |
| 699 | ret = PTR_ERR(uap->clk); |
| 700 | goto unmap; |
| 701 | } |
| 702 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 703 | uap->port.dev = &dev->dev; |
| 704 | uap->port.mapbase = dev->res.start; |
| 705 | uap->port.membase = base; |
| 706 | uap->port.iotype = UPIO_MEM; |
| 707 | uap->port.irq = dev->irq[0]; |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 708 | uap->port.fifosize = 16; |
| 709 | uap->port.ops = &amba_pl010_pops; |
| 710 | uap->port.flags = UPF_BOOT_AUTOCONF; |
| 711 | uap->port.line = i; |
| 712 | uap->dev = dev; |
| 713 | uap->data = dev->dev.platform_data; |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 714 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 715 | amba_ports[i] = uap; |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 716 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 717 | amba_set_drvdata(dev, uap); |
| 718 | ret = uart_add_one_port(&amba_reg, &uap->port); |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 719 | if (ret) { |
| 720 | amba_set_drvdata(dev, NULL); |
| 721 | amba_ports[i] = NULL; |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 722 | clk_put(uap->clk); |
| 723 | unmap: |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 724 | iounmap(base); |
| 725 | free: |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 726 | kfree(uap); |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 727 | } |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 728 | out: |
| 729 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static int pl010_remove(struct amba_device *dev) |
| 733 | { |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 734 | struct uart_amba_port *uap = amba_get_drvdata(dev); |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 735 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
| 737 | amba_set_drvdata(dev, NULL); |
| 738 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 739 | uart_remove_one_port(&amba_reg, &uap->port); |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 740 | |
| 741 | for (i = 0; i < ARRAY_SIZE(amba_ports); i++) |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 742 | if (amba_ports[i] == uap) |
Russell King | fbb18a2 | 2006-03-26 23:13:39 +0100 | [diff] [blame] | 743 | amba_ports[i] = NULL; |
| 744 | |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 745 | iounmap(uap->port.membase); |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 746 | clk_put(uap->clk); |
Russell King | 1b0646a | 2007-04-22 11:55:59 +0100 | [diff] [blame] | 747 | kfree(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
Pavel Machek | 0370aff | 2005-04-16 15:25:35 -0700 | [diff] [blame] | 751 | static int pl010_suspend(struct amba_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
| 753 | struct uart_amba_port *uap = amba_get_drvdata(dev); |
| 754 | |
| 755 | if (uap) |
| 756 | uart_suspend_port(&amba_reg, &uap->port); |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | static int pl010_resume(struct amba_device *dev) |
| 762 | { |
| 763 | struct uart_amba_port *uap = amba_get_drvdata(dev); |
| 764 | |
| 765 | if (uap) |
| 766 | uart_resume_port(&amba_reg, &uap->port); |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static struct amba_id pl010_ids[] __initdata = { |
| 772 | { |
| 773 | .id = 0x00041010, |
| 774 | .mask = 0x000fffff, |
| 775 | }, |
| 776 | { 0, 0 }, |
| 777 | }; |
| 778 | |
| 779 | static struct amba_driver pl010_driver = { |
| 780 | .drv = { |
| 781 | .name = "uart-pl010", |
| 782 | }, |
| 783 | .id_table = pl010_ids, |
| 784 | .probe = pl010_probe, |
| 785 | .remove = pl010_remove, |
| 786 | .suspend = pl010_suspend, |
| 787 | .resume = pl010_resume, |
| 788 | }; |
| 789 | |
| 790 | static int __init pl010_init(void) |
| 791 | { |
| 792 | int ret; |
| 793 | |
| 794 | printk(KERN_INFO "Serial: AMBA driver $Revision: 1.41 $\n"); |
| 795 | |
| 796 | ret = uart_register_driver(&amba_reg); |
| 797 | if (ret == 0) { |
| 798 | ret = amba_driver_register(&pl010_driver); |
| 799 | if (ret) |
| 800 | uart_unregister_driver(&amba_reg); |
| 801 | } |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | static void __exit pl010_exit(void) |
| 806 | { |
| 807 | amba_driver_unregister(&pl010_driver); |
| 808 | uart_unregister_driver(&amba_reg); |
| 809 | } |
| 810 | |
| 811 | module_init(pl010_init); |
| 812 | module_exit(pl010_exit); |
| 813 | |
| 814 | MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd"); |
| 815 | MODULE_DESCRIPTION("ARM AMBA serial port driver $Revision: 1.41 $"); |
| 816 | MODULE_LICENSE("GPL"); |