blob: 194108b0de84249490390f955692c7e05b81842f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for AMBA 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
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This is a generic driver for ARM AMBA-type serial ports. They
24 * have a lot of 16550-like features, but are not register compatible.
25 * Note that although they do have CTS, DCD and DSR inputs, they do
26 * not have an RI input, nor do they have DTR or RTS outputs. If
27 * required, these have to be supplied via some other means (eg, GPIO)
28 * and hooked into this driver.
29 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#if defined(CONFIG_SERIAL_AMBA_PL010_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
32#define SUPPORT_SYSRQ
33#endif
34
35#include <linux/module.h>
36#include <linux/ioport.h>
37#include <linux/init.h>
38#include <linux/console.h>
39#include <linux/sysrq.h>
40#include <linux/device.h>
41#include <linux/tty.h>
42#include <linux/tty_flip.h>
43#include <linux/serial_core.h>
44#include <linux/serial.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000045#include <linux/amba/bus.h>
46#include <linux/amba/serial.h>
Russell Kinged519de2007-04-22 12:30:41 +010047#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Tushar Behera44acd262014-06-26 15:35:36 +053049#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Lennert Buytenhek4faf4e02006-06-20 19:24:07 +010051#define UART_NR 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define SERIAL_AMBA_MAJOR 204
54#define SERIAL_AMBA_MINOR 16
55#define SERIAL_AMBA_NR UART_NR
56
57#define AMBA_ISR_PASS_LIMIT 256
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0)
60#define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Russell Kingfbb18a22006-03-26 23:13:39 +010062#define UART_DUMMY_RSR_RX 256
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define UART_PORT_SIZE 64
64
65/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * We wrap our port structure around the generic uart_port.
67 */
68struct uart_amba_port {
69 struct uart_port port;
Russell Kinged519de2007-04-22 12:30:41 +010070 struct clk *clk;
Russell Kingfbb18a22006-03-26 23:13:39 +010071 struct amba_device *dev;
72 struct amba_pl010_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unsigned int old_status;
74};
75
Russell Kingb129a8c2005-08-31 10:12:14 +010076static void pl010_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Fabian Frederickb70e5e92014-10-05 19:19:46 +020078 struct uart_amba_port *uap =
79 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unsigned int cr;
81
Russell King1b0646a2007-04-22 11:55:59 +010082 cr = readb(uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 cr &= ~UART010_CR_TIE;
Russell King1b0646a2007-04-22 11:55:59 +010084 writel(cr, uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Russell Kingb129a8c2005-08-31 10:12:14 +010087static void pl010_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Fabian Frederickb70e5e92014-10-05 19:19:46 +020089 struct uart_amba_port *uap =
90 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 unsigned int cr;
92
Russell King1b0646a2007-04-22 11:55:59 +010093 cr = readb(uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 cr |= UART010_CR_TIE;
Russell King1b0646a2007-04-22 11:55:59 +010095 writel(cr, uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98static void pl010_stop_rx(struct uart_port *port)
99{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200100 struct uart_amba_port *uap =
101 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 unsigned int cr;
103
Russell King1b0646a2007-04-22 11:55:59 +0100104 cr = readb(uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 cr &= ~(UART010_CR_RIE | UART010_CR_RTIE);
Russell King1b0646a2007-04-22 11:55:59 +0100106 writel(cr, uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109static void pl010_enable_ms(struct uart_port *port)
110{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200111 struct uart_amba_port *uap =
112 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int cr;
114
Russell King1b0646a2007-04-22 11:55:59 +0100115 cr = readb(uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 cr |= UART010_CR_MSIE;
Russell King1b0646a2007-04-22 11:55:59 +0100117 writel(cr, uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Russell King1b0646a2007-04-22 11:55:59 +0100120static void pl010_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned int status, ch, flag, rsr, max_count = 256;
123
Russell King1b0646a2007-04-22 11:55:59 +0100124 status = readb(uap->port.membase + UART01x_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 while (UART_RX_DATA(status) && max_count--) {
Russell King1b0646a2007-04-22 11:55:59 +0100126 ch = readb(uap->port.membase + UART01x_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 flag = TTY_NORMAL;
128
Russell King1b0646a2007-04-22 11:55:59 +0100129 uap->port.icount.rx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /*
132 * Note that the error handling code is
133 * out of the main execution path
134 */
Russell King1b0646a2007-04-22 11:55:59 +0100135 rsr = readb(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX;
Russell King45849282005-04-26 15:29:44 +0100136 if (unlikely(rsr & UART01x_RSR_ANY)) {
Russell King1b0646a2007-04-22 11:55:59 +0100137 writel(0, uap->port.membase + UART01x_ECR);
Lennert Buytenheka4ed06a2006-12-06 20:39:57 -0800138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (rsr & UART01x_RSR_BE) {
140 rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
Russell King1b0646a2007-04-22 11:55:59 +0100141 uap->port.icount.brk++;
142 if (uart_handle_break(&uap->port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto ignore_char;
144 } else if (rsr & UART01x_RSR_PE)
Russell King1b0646a2007-04-22 11:55:59 +0100145 uap->port.icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 else if (rsr & UART01x_RSR_FE)
Russell King1b0646a2007-04-22 11:55:59 +0100147 uap->port.icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (rsr & UART01x_RSR_OE)
Russell King1b0646a2007-04-22 11:55:59 +0100149 uap->port.icount.overrun++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Russell King1b0646a2007-04-22 11:55:59 +0100151 rsr &= uap->port.read_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (rsr & UART01x_RSR_BE)
154 flag = TTY_BREAK;
155 else if (rsr & UART01x_RSR_PE)
156 flag = TTY_PARITY;
157 else if (rsr & UART01x_RSR_FE)
158 flag = TTY_FRAME;
159 }
160
Russell King1b0646a2007-04-22 11:55:59 +0100161 if (uart_handle_sysrq_char(&uap->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 goto ignore_char;
163
Russell King1b0646a2007-04-22 11:55:59 +0100164 uart_insert_char(&uap->port, rsr, UART01x_RSR_OE, ch, flag);
Russell King05ab3012005-05-09 23:21:59 +0100165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 ignore_char:
Russell King1b0646a2007-04-22 11:55:59 +0100167 status = readb(uap->port.membase + UART01x_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Russell Kingdb002b82007-06-05 19:39:49 +0100169 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100170 tty_flip_buffer_push(&uap->port.state->port);
Russell Kingdb002b82007-06-05 19:39:49 +0100171 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Russell King1b0646a2007-04-22 11:55:59 +0100174static void pl010_tx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700176 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int count;
178
Russell King1b0646a2007-04-22 11:55:59 +0100179 if (uap->port.x_char) {
180 writel(uap->port.x_char, uap->port.membase + UART01x_DR);
181 uap->port.icount.tx++;
182 uap->port.x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return;
184 }
Russell King1b0646a2007-04-22 11:55:59 +0100185 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
186 pl010_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return;
188 }
189
Russell King1b0646a2007-04-22 11:55:59 +0100190 count = uap->port.fifosize >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 do {
Russell King1b0646a2007-04-22 11:55:59 +0100192 writel(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Russell King1b0646a2007-04-22 11:55:59 +0100194 uap->port.icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (uart_circ_empty(xmit))
196 break;
197 } while (--count > 0);
198
199 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
Russell King1b0646a2007-04-22 11:55:59 +0100200 uart_write_wakeup(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (uart_circ_empty(xmit))
Russell King1b0646a2007-04-22 11:55:59 +0100203 pl010_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Russell King1b0646a2007-04-22 11:55:59 +0100206static void pl010_modem_status(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int status, delta;
209
Russell King98639a62006-03-25 21:30:11 +0000210 writel(0, uap->port.membase + UART010_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Russell King98639a62006-03-25 21:30:11 +0000212 status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 delta = status ^ uap->old_status;
215 uap->old_status = status;
216
217 if (!delta)
218 return;
219
220 if (delta & UART01x_FR_DCD)
221 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
222
223 if (delta & UART01x_FR_DSR)
224 uap->port.icount.dsr++;
225
226 if (delta & UART01x_FR_CTS)
227 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
228
Alan Coxbdc04e32009-09-19 13:13:31 -0700229 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
David Howells7d12e782006-10-05 14:55:46 +0100232static irqreturn_t pl010_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Russell King1b0646a2007-04-22 11:55:59 +0100234 struct uart_amba_port *uap = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
236 int handled = 0;
237
Russell King1b0646a2007-04-22 11:55:59 +0100238 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Russell King1b0646a2007-04-22 11:55:59 +0100240 status = readb(uap->port.membase + UART010_IIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (status) {
242 do {
243 if (status & (UART010_IIR_RTIS | UART010_IIR_RIS))
Russell King1b0646a2007-04-22 11:55:59 +0100244 pl010_rx_chars(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (status & UART010_IIR_MIS)
Russell King1b0646a2007-04-22 11:55:59 +0100246 pl010_modem_status(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (status & UART010_IIR_TIS)
Russell King1b0646a2007-04-22 11:55:59 +0100248 pl010_tx_chars(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (pass_counter-- == 0)
251 break;
252
Russell King1b0646a2007-04-22 11:55:59 +0100253 status = readb(uap->port.membase + UART010_IIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS |
255 UART010_IIR_TIS));
256 handled = 1;
257 }
258
Russell King1b0646a2007-04-22 11:55:59 +0100259 spin_unlock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return IRQ_RETVAL(handled);
262}
263
264static unsigned int pl010_tx_empty(struct uart_port *port)
265{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200266 struct uart_amba_port *uap =
267 container_of(port, struct uart_amba_port, port);
Russell King1b0646a2007-04-22 11:55:59 +0100268 unsigned int status = readb(uap->port.membase + UART01x_FR);
269 return status & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272static unsigned int pl010_get_mctrl(struct uart_port *port)
273{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200274 struct uart_amba_port *uap =
275 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 unsigned int result = 0;
277 unsigned int status;
278
Russell King1b0646a2007-04-22 11:55:59 +0100279 status = readb(uap->port.membase + UART01x_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (status & UART01x_FR_DCD)
281 result |= TIOCM_CAR;
282 if (status & UART01x_FR_DSR)
283 result |= TIOCM_DSR;
284 if (status & UART01x_FR_CTS)
285 result |= TIOCM_CTS;
286
287 return result;
288}
289
290static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl)
291{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200292 struct uart_amba_port *uap =
293 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Russell Kingfbb18a22006-03-26 23:13:39 +0100295 if (uap->data)
296 uap->data->set_mctrl(uap->dev, uap->port.membase, mctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static void pl010_break_ctl(struct uart_port *port, int break_state)
300{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200301 struct uart_amba_port *uap =
302 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 unsigned long flags;
304 unsigned int lcr_h;
305
Russell King1b0646a2007-04-22 11:55:59 +0100306 spin_lock_irqsave(&uap->port.lock, flags);
307 lcr_h = readb(uap->port.membase + UART010_LCRH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (break_state == -1)
309 lcr_h |= UART01x_LCRH_BRK;
310 else
311 lcr_h &= ~UART01x_LCRH_BRK;
Russell King1b0646a2007-04-22 11:55:59 +0100312 writel(lcr_h, uap->port.membase + UART010_LCRH);
313 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316static int pl010_startup(struct uart_port *port)
317{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200318 struct uart_amba_port *uap =
319 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int retval;
321
322 /*
Russell Kinged519de2007-04-22 12:30:41 +0100323 * Try to enable the clock producer.
324 */
Julia Lawall1c4c4392012-08-26 18:01:01 +0200325 retval = clk_prepare_enable(uap->clk);
Russell Kinged519de2007-04-22 12:30:41 +0100326 if (retval)
Julia Lawall1c4c4392012-08-26 18:01:01 +0200327 goto out;
Russell Kinged519de2007-04-22 12:30:41 +0100328
329 uap->port.uartclk = clk_get_rate(uap->clk);
330
331 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * Allocate the IRQ
333 */
Russell King1b0646a2007-04-22 11:55:59 +0100334 retval = request_irq(uap->port.irq, pl010_int, 0, "uart-pl010", uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (retval)
Russell Kinged519de2007-04-22 12:30:41 +0100336 goto clk_dis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /*
339 * initialise the old status of the modem signals
340 */
Russell King1b0646a2007-04-22 11:55:59 +0100341 uap->old_status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /*
344 * Finally, enable interrupts
345 */
Russell King98639a62006-03-25 21:30:11 +0000346 writel(UART01x_CR_UARTEN | UART010_CR_RIE | UART010_CR_RTIE,
Russell King1b0646a2007-04-22 11:55:59 +0100347 uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 return 0;
Russell Kinged519de2007-04-22 12:30:41 +0100350
351 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +0200352 clk_disable_unprepare(uap->clk);
Russell Kinged519de2007-04-22 12:30:41 +0100353 out:
354 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static void pl010_shutdown(struct uart_port *port)
358{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200359 struct uart_amba_port *uap =
360 container_of(port, struct uart_amba_port, port);
Russell King1b0646a2007-04-22 11:55:59 +0100361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /*
363 * Free the interrupt
364 */
Russell King1b0646a2007-04-22 11:55:59 +0100365 free_irq(uap->port.irq, uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /*
368 * disable all interrupts, disable the port
369 */
Russell King1b0646a2007-04-22 11:55:59 +0100370 writel(0, uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* disable break condition and fifos */
Russell King1b0646a2007-04-22 11:55:59 +0100373 writel(readb(uap->port.membase + UART010_LCRH) &
Russell King98639a62006-03-25 21:30:11 +0000374 ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN),
Russell King1b0646a2007-04-22 11:55:59 +0100375 uap->port.membase + UART010_LCRH);
Russell Kinged519de2007-04-22 12:30:41 +0100376
377 /*
378 * Shut down the clock producer
379 */
Julia Lawall1c4c4392012-08-26 18:01:01 +0200380 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383static void
Alan Cox606d0992006-12-08 02:38:45 -0800384pl010_set_termios(struct uart_port *port, struct ktermios *termios,
385 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200387 struct uart_amba_port *uap =
388 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned int lcr_h, old_cr;
390 unsigned long flags;
391 unsigned int baud, quot;
392
393 /*
394 * Ask the core to calculate the divisor for us.
395 */
Russell King1b0646a2007-04-22 11:55:59 +0100396 baud = uart_get_baud_rate(port, termios, old, 0, uap->port.uartclk/16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 quot = uart_get_divisor(port, baud);
398
399 switch (termios->c_cflag & CSIZE) {
400 case CS5:
401 lcr_h = UART01x_LCRH_WLEN_5;
402 break;
403 case CS6:
404 lcr_h = UART01x_LCRH_WLEN_6;
405 break;
406 case CS7:
407 lcr_h = UART01x_LCRH_WLEN_7;
408 break;
409 default: // CS8
410 lcr_h = UART01x_LCRH_WLEN_8;
411 break;
412 }
413 if (termios->c_cflag & CSTOPB)
414 lcr_h |= UART01x_LCRH_STP2;
415 if (termios->c_cflag & PARENB) {
416 lcr_h |= UART01x_LCRH_PEN;
417 if (!(termios->c_cflag & PARODD))
418 lcr_h |= UART01x_LCRH_EPS;
419 }
Russell King1b0646a2007-04-22 11:55:59 +0100420 if (uap->port.fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 lcr_h |= UART01x_LCRH_FEN;
422
Russell King1b0646a2007-04-22 11:55:59 +0100423 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /*
426 * Update the per-port timeout.
427 */
428 uart_update_timeout(port, termios->c_cflag, baud);
429
Russell King1b0646a2007-04-22 11:55:59 +0100430 uap->port.read_status_mask = UART01x_RSR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (termios->c_iflag & INPCK)
Russell King1b0646a2007-04-22 11:55:59 +0100432 uap->port.read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400433 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Russell King1b0646a2007-04-22 11:55:59 +0100434 uap->port.read_status_mask |= UART01x_RSR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * Characters to ignore
438 */
Russell King1b0646a2007-04-22 11:55:59 +0100439 uap->port.ignore_status_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (termios->c_iflag & IGNPAR)
Russell King1b0646a2007-04-22 11:55:59 +0100441 uap->port.ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (termios->c_iflag & IGNBRK) {
Russell King1b0646a2007-04-22 11:55:59 +0100443 uap->port.ignore_status_mask |= UART01x_RSR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /*
445 * If we're ignoring parity and break indicators,
446 * ignore overruns too (for real raw support).
447 */
448 if (termios->c_iflag & IGNPAR)
Russell King1b0646a2007-04-22 11:55:59 +0100449 uap->port.ignore_status_mask |= UART01x_RSR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 /*
453 * Ignore all characters if CREAD is not set.
454 */
455 if ((termios->c_cflag & CREAD) == 0)
Russell King1b0646a2007-04-22 11:55:59 +0100456 uap->port.ignore_status_mask |= UART_DUMMY_RSR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* first, disable everything */
Russell King1b0646a2007-04-22 11:55:59 +0100459 old_cr = readb(uap->port.membase + UART010_CR) & ~UART010_CR_MSIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (UART_ENABLE_MS(port, termios->c_cflag))
462 old_cr |= UART010_CR_MSIE;
463
Russell King1b0646a2007-04-22 11:55:59 +0100464 writel(0, uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* Set baud rate */
467 quot -= 1;
Russell King1b0646a2007-04-22 11:55:59 +0100468 writel((quot & 0xf00) >> 8, uap->port.membase + UART010_LCRM);
469 writel(quot & 0xff, uap->port.membase + UART010_LCRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /*
472 * ----------v----------v----------v----------v-----
473 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
474 * ----------^----------^----------^----------^-----
475 */
Russell King1b0646a2007-04-22 11:55:59 +0100476 writel(lcr_h, uap->port.membase + UART010_LCRH);
477 writel(old_cr, uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Russell King1b0646a2007-04-22 11:55:59 +0100479 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Peter Hurley732a84a2014-11-05 13:11:43 -0500482static void pl010_set_ldisc(struct uart_port *port, struct ktermios *termios)
Rodolfo Giometti7ed63d52010-03-10 15:23:48 -0800483{
Peter Hurley732a84a2014-11-05 13:11:43 -0500484 if (termios->c_line == N_PPS) {
Rodolfo Giometti7ed63d52010-03-10 15:23:48 -0800485 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -0500486 spin_lock_irq(&port->lock);
Rodolfo Giometti7ed63d52010-03-10 15:23:48 -0800487 pl010_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -0500488 spin_unlock_irq(&port->lock);
Rodolfo Giometti7ed63d52010-03-10 15:23:48 -0800489 } else
490 port->flags &= ~UPF_HARDPPS_CD;
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static const char *pl010_type(struct uart_port *port)
494{
495 return port->type == PORT_AMBA ? "AMBA" : NULL;
496}
497
498/*
499 * Release the memory region(s) being used by 'port'
500 */
501static void pl010_release_port(struct uart_port *port)
502{
503 release_mem_region(port->mapbase, UART_PORT_SIZE);
504}
505
506/*
507 * Request the memory region(s) being used by 'port'
508 */
509static int pl010_request_port(struct uart_port *port)
510{
511 return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
512 != NULL ? 0 : -EBUSY;
513}
514
515/*
516 * Configure/autoconfigure the port.
517 */
518static void pl010_config_port(struct uart_port *port, int flags)
519{
520 if (flags & UART_CONFIG_TYPE) {
521 port->type = PORT_AMBA;
522 pl010_request_port(port);
523 }
524}
525
526/*
527 * verify the new serial_struct (for TIOCSSERIAL).
528 */
529static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
530{
531 int ret = 0;
532 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
533 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -0700534 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ret = -EINVAL;
536 if (ser->baud_base < 9600)
537 ret = -EINVAL;
538 return ret;
539}
540
541static struct uart_ops amba_pl010_pops = {
542 .tx_empty = pl010_tx_empty,
543 .set_mctrl = pl010_set_mctrl,
544 .get_mctrl = pl010_get_mctrl,
545 .stop_tx = pl010_stop_tx,
546 .start_tx = pl010_start_tx,
547 .stop_rx = pl010_stop_rx,
548 .enable_ms = pl010_enable_ms,
549 .break_ctl = pl010_break_ctl,
550 .startup = pl010_startup,
551 .shutdown = pl010_shutdown,
552 .set_termios = pl010_set_termios,
Rodolfo Giometti7ed63d52010-03-10 15:23:48 -0800553 .set_ldisc = pl010_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 .type = pl010_type,
555 .release_port = pl010_release_port,
556 .request_port = pl010_request_port,
557 .config_port = pl010_config_port,
558 .verify_port = pl010_verify_port,
559};
560
Russell Kingfbb18a22006-03-26 23:13:39 +0100561static struct uart_amba_port *amba_ports[UART_NR];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563#ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
564
Russell Kingd3587882006-03-20 20:00:09 +0000565static void pl010_console_putchar(struct uart_port *port, int ch)
566{
Fabian Frederickb70e5e92014-10-05 19:19:46 +0200567 struct uart_amba_port *uap =
568 container_of(port, struct uart_amba_port, port);
Russell King98639a62006-03-25 21:30:11 +0000569 unsigned int status;
570
571 do {
Russell King1b0646a2007-04-22 11:55:59 +0100572 status = readb(uap->port.membase + UART01x_FR);
Russell Kingd3587882006-03-20 20:00:09 +0000573 barrier();
Russell King98639a62006-03-25 21:30:11 +0000574 } while (!UART_TX_READY(status));
Russell King1b0646a2007-04-22 11:55:59 +0100575 writel(ch, uap->port.membase + UART01x_DR);
Russell Kingd3587882006-03-20 20:00:09 +0000576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578static void
579pl010_console_write(struct console *co, const char *s, unsigned int count)
580{
Russell King1b0646a2007-04-22 11:55:59 +0100581 struct uart_amba_port *uap = amba_ports[co->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 unsigned int status, old_cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Russell Kinged519de2007-04-22 12:30:41 +0100584 clk_enable(uap->clk);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /*
587 * First save the CR then disable the interrupts
588 */
Russell King1b0646a2007-04-22 11:55:59 +0100589 old_cr = readb(uap->port.membase + UART010_CR);
590 writel(UART01x_CR_UARTEN, uap->port.membase + UART010_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Russell King1b0646a2007-04-22 11:55:59 +0100592 uart_console_write(&uap->port, s, count, pl010_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /*
595 * Finally, wait for transmitter to become empty
596 * and restore the TCR
597 */
598 do {
Russell King1b0646a2007-04-22 11:55:59 +0100599 status = readb(uap->port.membase + UART01x_FR);
Russell King98639a62006-03-25 21:30:11 +0000600 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 } while (status & UART01x_FR_BUSY);
Russell King1b0646a2007-04-22 11:55:59 +0100602 writel(old_cr, uap->port.membase + UART010_CR);
Russell Kinged519de2007-04-22 12:30:41 +0100603
604 clk_disable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static void __init
Russell King1b0646a2007-04-22 11:55:59 +0100608pl010_console_get_options(struct uart_amba_port *uap, int *baud,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int *parity, int *bits)
610{
Russell King1b0646a2007-04-22 11:55:59 +0100611 if (readb(uap->port.membase + UART010_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 unsigned int lcr_h, quot;
Russell King1b0646a2007-04-22 11:55:59 +0100613 lcr_h = readb(uap->port.membase + UART010_LCRH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 *parity = 'n';
616 if (lcr_h & UART01x_LCRH_PEN) {
617 if (lcr_h & UART01x_LCRH_EPS)
618 *parity = 'e';
619 else
620 *parity = 'o';
621 }
622
623 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
624 *bits = 7;
625 else
626 *bits = 8;
627
Russell King1b0646a2007-04-22 11:55:59 +0100628 quot = readb(uap->port.membase + UART010_LCRL) |
629 readb(uap->port.membase + UART010_LCRM) << 8;
630 *baud = uap->port.uartclk / (16 * (quot + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632}
633
634static int __init pl010_console_setup(struct console *co, char *options)
635{
Russell King1b0646a2007-04-22 11:55:59 +0100636 struct uart_amba_port *uap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 int baud = 38400;
638 int bits = 8;
639 int parity = 'n';
640 int flow = 'n';
Russell King36b8f1e2011-09-22 11:35:09 +0100641 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /*
644 * Check whether an invalid uart number has been specified, and
645 * if so, search for the first available port that does have
646 * console support.
647 */
648 if (co->index >= UART_NR)
649 co->index = 0;
Russell King1b0646a2007-04-22 11:55:59 +0100650 uap = amba_ports[co->index];
651 if (!uap)
Russell Kingd28122a2007-01-22 18:59:42 +0000652 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Russell King36b8f1e2011-09-22 11:35:09 +0100654 ret = clk_prepare(uap->clk);
655 if (ret)
656 return ret;
657
Russell Kinged519de2007-04-22 12:30:41 +0100658 uap->port.uartclk = clk_get_rate(uap->clk);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (options)
661 uart_parse_options(options, &baud, &parity, &bits, &flow);
662 else
Russell King1b0646a2007-04-22 11:55:59 +0100663 pl010_console_get_options(uap, &baud, &parity, &bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Russell King1b0646a2007-04-22 11:55:59 +0100665 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Vincent Sanders2d934862005-09-14 22:36:03 +0100668static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static struct console amba_console = {
670 .name = "ttyAM",
671 .write = pl010_console_write,
672 .device = uart_console_device,
673 .setup = pl010_console_setup,
674 .flags = CON_PRINTBUFFER,
675 .index = -1,
676 .data = &amba_reg,
677};
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679#define AMBA_CONSOLE &amba_console
680#else
681#define AMBA_CONSOLE NULL
682#endif
683
684static struct uart_driver amba_reg = {
685 .owner = THIS_MODULE,
686 .driver_name = "ttyAM",
687 .dev_name = "ttyAM",
688 .major = SERIAL_AMBA_MAJOR,
689 .minor = SERIAL_AMBA_MINOR,
690 .nr = UART_NR,
691 .cons = AMBA_CONSOLE,
692};
693
Russell Kingaa25afa2011-02-19 15:55:00 +0000694static int pl010_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Russell King1b0646a2007-04-22 11:55:59 +0100696 struct uart_amba_port *uap;
Russell Kingfbb18a22006-03-26 23:13:39 +0100697 void __iomem *base;
698 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Russell Kingfbb18a22006-03-26 23:13:39 +0100700 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
701 if (amba_ports[i] == NULL)
702 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Tushar Behera44acd262014-06-26 15:35:36 +0530704 if (i == ARRAY_SIZE(amba_ports))
705 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Tushar Behera44acd262014-06-26 15:35:36 +0530707 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
708 GFP_KERNEL);
709 if (!uap)
710 return -ENOMEM;
Russell Kingfbb18a22006-03-26 23:13:39 +0100711
Tushar Behera44acd262014-06-26 15:35:36 +0530712 base = devm_ioremap(&dev->dev, dev->res.start,
713 resource_size(&dev->res));
714 if (!base)
715 return -ENOMEM;
Russell Kingfbb18a22006-03-26 23:13:39 +0100716
Tushar Behera44acd262014-06-26 15:35:36 +0530717 uap->clk = devm_clk_get(&dev->dev, NULL);
718 if (IS_ERR(uap->clk))
719 return PTR_ERR(uap->clk);
Russell Kinged519de2007-04-22 12:30:41 +0100720
Russell King1b0646a2007-04-22 11:55:59 +0100721 uap->port.dev = &dev->dev;
722 uap->port.mapbase = dev->res.start;
723 uap->port.membase = base;
724 uap->port.iotype = UPIO_MEM;
725 uap->port.irq = dev->irq[0];
Russell King1b0646a2007-04-22 11:55:59 +0100726 uap->port.fifosize = 16;
727 uap->port.ops = &amba_pl010_pops;
728 uap->port.flags = UPF_BOOT_AUTOCONF;
729 uap->port.line = i;
730 uap->dev = dev;
Jingoo Han574de552013-07-30 17:06:57 +0900731 uap->data = dev_get_platdata(&dev->dev);
Russell Kingfbb18a22006-03-26 23:13:39 +0100732
Russell King1b0646a2007-04-22 11:55:59 +0100733 amba_ports[i] = uap;
Russell Kingfbb18a22006-03-26 23:13:39 +0100734
Russell King1b0646a2007-04-22 11:55:59 +0100735 amba_set_drvdata(dev, uap);
736 ret = uart_add_one_port(&amba_reg, &uap->port);
Tushar Behera44acd262014-06-26 15:35:36 +0530737 if (ret)
Russell Kingfbb18a22006-03-26 23:13:39 +0100738 amba_ports[i] = NULL;
Tushar Behera44acd262014-06-26 15:35:36 +0530739
Russell Kingfbb18a22006-03-26 23:13:39 +0100740 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743static int pl010_remove(struct amba_device *dev)
744{
Russell King1b0646a2007-04-22 11:55:59 +0100745 struct uart_amba_port *uap = amba_get_drvdata(dev);
Russell Kingfbb18a22006-03-26 23:13:39 +0100746 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Russell King1b0646a2007-04-22 11:55:59 +0100748 uart_remove_one_port(&amba_reg, &uap->port);
Russell Kingfbb18a22006-03-26 23:13:39 +0100749
750 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
Russell King1b0646a2007-04-22 11:55:59 +0100751 if (amba_ports[i] == uap)
Russell Kingfbb18a22006-03-26 23:13:39 +0100752 amba_ports[i] = NULL;
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755}
756
Ulf Hansson95468242013-12-03 11:04:27 +0100757#ifdef CONFIG_PM_SLEEP
758static int pl010_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Ulf Hansson95468242013-12-03 11:04:27 +0100760 struct uart_amba_port *uap = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (uap)
763 uart_suspend_port(&amba_reg, &uap->port);
764
765 return 0;
766}
767
Ulf Hansson95468242013-12-03 11:04:27 +0100768static int pl010_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Ulf Hansson95468242013-12-03 11:04:27 +0100770 struct uart_amba_port *uap = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (uap)
773 uart_resume_port(&amba_reg, &uap->port);
774
775 return 0;
776}
Ulf Hansson95468242013-12-03 11:04:27 +0100777#endif
778
779static SIMPLE_DEV_PM_OPS(pl010_dev_pm_ops, pl010_suspend, pl010_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Russell King2c39c9e2010-07-27 08:50:16 +0100781static struct amba_id pl010_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 {
783 .id = 0x00041010,
784 .mask = 0x000fffff,
785 },
786 { 0, 0 },
787};
788
Dave Martina664a112011-10-05 15:15:22 +0100789MODULE_DEVICE_TABLE(amba, pl010_ids);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791static struct amba_driver pl010_driver = {
792 .drv = {
793 .name = "uart-pl010",
Ulf Hansson95468242013-12-03 11:04:27 +0100794 .pm = &pl010_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 },
796 .id_table = pl010_ids,
797 .probe = pl010_probe,
798 .remove = pl010_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799};
800
801static int __init pl010_init(void)
802{
803 int ret;
804
Adrian Bunkd87a6d92008-07-16 21:53:31 +0100805 printk(KERN_INFO "Serial: AMBA driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 ret = uart_register_driver(&amba_reg);
808 if (ret == 0) {
809 ret = amba_driver_register(&pl010_driver);
810 if (ret)
811 uart_unregister_driver(&amba_reg);
812 }
813 return ret;
814}
815
816static void __exit pl010_exit(void)
817{
818 amba_driver_unregister(&pl010_driver);
819 uart_unregister_driver(&amba_reg);
820}
821
822module_init(pl010_init);
823module_exit(pl010_exit);
824
825MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
Adrian Bunkd87a6d92008-07-16 21:53:31 +0100826MODULE_DESCRIPTION("ARM AMBA serial port driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827MODULE_LICENSE("GPL");