blob: 44b27ec3234190370af5a18a6472b63f6db3aff0 [file] [log] [blame]
Bryan Wu194de562007-05-06 14:50:30 -07001/*
Mike Frysinger1ba7a3e2008-01-11 15:56:26 +08002 * Blackfin On-Chip Serial Driver
Bryan Wu194de562007-05-06 14:50:30 -07003 *
Sonic Zhangb06d2f22012-05-16 14:22:23 +08004 * Copyright 2006-2011 Analog Devices Inc.
Bryan Wu194de562007-05-06 14:50:30 -07005 *
Mike Frysinger1ba7a3e2008-01-11 15:56:26 +08006 * Enter bugs at http://blackfin.uclinux.org/
Bryan Wu194de562007-05-06 14:50:30 -07007 *
Mike Frysinger1ba7a3e2008-01-11 15:56:26 +08008 * Licensed under the GPL-2 or later.
Bryan Wu194de562007-05-06 14:50:30 -07009 */
10
11#if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12#define SUPPORT_SYSRQ
13#endif
14
Sonic Zhang57afb392009-09-09 10:46:19 +000015#define DRIVER_NAME "bfin-uart"
16#define pr_fmt(fmt) DRIVER_NAME ": " fmt
17
Bryan Wu194de562007-05-06 14:50:30 -070018#include <linux/module.h>
19#include <linux/ioport.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Mike Frysinger599b7142010-01-19 06:13:13 -050021#include <linux/io.h>
Bryan Wu194de562007-05-06 14:50:30 -070022#include <linux/init.h>
23#include <linux/console.h>
24#include <linux/sysrq.h>
25#include <linux/platform_device.h>
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/serial_core.h>
Sonic Zhang57afb392009-09-09 10:46:19 +000029#include <linux/gpio.h>
30#include <linux/irq.h>
31#include <linux/kgdb.h>
32#include <linux/slab.h>
Sonic Zhangb6100992010-10-27 04:16:47 -040033#include <linux/dma-mapping.h>
Bryan Wu194de562007-05-06 14:50:30 -070034
Sonic Zhang57afb392009-09-09 10:46:19 +000035#include <asm/portmux.h>
Bryan Wu194de562007-05-06 14:50:30 -070036#include <asm/cacheflush.h>
Sonic Zhang57afb392009-09-09 10:46:19 +000037#include <asm/dma.h>
Sonic Zhang57afb392009-09-09 10:46:19 +000038#include <asm/bfin_serial.h>
Bryan Wu194de562007-05-06 14:50:30 -070039
Mike Frysinger607c2682009-06-22 18:41:47 +010040#ifdef CONFIG_SERIAL_BFIN_MODULE
41# undef CONFIG_EARLY_PRINTK
42#endif
43
Bryan Wu194de562007-05-06 14:50:30 -070044/* UART name and device definitions */
Sonic Zhang57afb392009-09-09 10:46:19 +000045#define BFIN_SERIAL_DEV_NAME "ttyBF"
Bryan Wu194de562007-05-06 14:50:30 -070046#define BFIN_SERIAL_MAJOR 204
47#define BFIN_SERIAL_MINOR 64
48
Sonic Zhang57afb392009-09-09 10:46:19 +000049static struct bfin_serial_port *bfin_serial_ports[BFIN_UART_NR_PORTS];
Mike Frysingerc9607ec2008-10-13 10:33:16 +010050
Sonic Zhang52e15f0e2009-01-02 13:40:14 +000051#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
52 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
53
54# ifndef CONFIG_SERIAL_BFIN_PIO
55# error KGDB only support UART in PIO mode.
56# endif
57
58static int kgdboc_port_line;
59static int kgdboc_break_enabled;
60#endif
Bryan Wu194de562007-05-06 14:50:30 -070061/*
62 * Setup for console. Argument comes from the menuconfig
63 */
64#define DMA_RX_XCOUNT 512
65#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
66
Sonic Zhang0aef4562008-02-29 12:08:42 +080067#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
Bryan Wu194de562007-05-06 14:50:30 -070068
69#ifdef CONFIG_SERIAL_BFIN_DMA
70static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
71#else
Bryan Wu194de562007-05-06 14:50:30 -070072static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070073#endif
74
Graf Yang80d5c472009-01-02 13:40:22 +000075static void bfin_serial_reset_irda(struct uart_port *port);
76
Sonic Zhangd307d362009-04-07 16:52:26 +010077#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
78 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
79static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
80{
81 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
82 if (uart->cts_pin < 0)
83 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
84
85 /* CTS PIN is negative assertive. */
86 if (UART_GET_CTS(uart))
87 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
88 else
89 return TIOCM_DSR | TIOCM_CAR;
90}
91
92static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
93{
94 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
95 if (uart->rts_pin < 0)
96 return;
97
98 /* RTS PIN is negative assertive. */
99 if (mctrl & TIOCM_RTS)
100 UART_ENABLE_RTS(uart);
101 else
102 UART_DISABLE_RTS(uart);
103}
104
105/*
106 * Handle any change of modem status signal.
107 */
108static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
109{
110 struct bfin_serial_port *uart = dev_id;
Peter Hurleyd01f4d12014-09-10 15:06:26 -0400111 struct uart_port *uport = &uart->port;
112 unsigned int status = bfin_serial_get_mctrl(uport);
Sonic Zhangd307d362009-04-07 16:52:26 +0100113#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang64851632011-12-13 12:22:02 +0800114
Sonic Zhangd307d362009-04-07 16:52:26 +0100115 UART_CLEAR_SCTS(uart);
Peter Hurleyd01f4d12014-09-10 15:06:26 -0400116 if (uport->hw_stopped) {
Sonic Zhang64851632011-12-13 12:22:02 +0800117 if (status) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -0400118 uport->hw_stopped = 0;
119 uart_write_wakeup(uport);
Sonic Zhang64851632011-12-13 12:22:02 +0800120 }
121 } else {
122 if (!status)
Peter Hurleyd01f4d12014-09-10 15:06:26 -0400123 uport->hw_stopped = 1;
Sonic Zhang64851632011-12-13 12:22:02 +0800124 }
Peter Hurley8620d3e2014-09-10 15:06:28 -0400125#else
Peter Hurleyd01f4d12014-09-10 15:06:26 -0400126 uart_handle_cts_change(uport, status & TIOCM_CTS);
Peter Hurley8620d3e2014-09-10 15:06:28 -0400127#endif
Sonic Zhangd307d362009-04-07 16:52:26 +0100128
129 return IRQ_HANDLED;
130}
131#else
132static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
133{
134 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
135}
136
137static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
138{
139}
140#endif
141
Bryan Wu194de562007-05-06 14:50:30 -0700142/*
143 * interrupts are disabled on entry
144 */
145static void bfin_serial_stop_tx(struct uart_port *port)
146{
147 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000148#ifdef CONFIG_SERIAL_BFIN_DMA
Alan Coxebd2c8f2009-09-19 13:13:28 -0700149 struct circ_buf *xmit = &uart->port.state->xmit;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000150#endif
Bryan Wu194de562007-05-06 14:50:30 -0700151
Roy Huangf4d640c92007-07-12 16:43:46 +0800152 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +0800153 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +0800154
Bryan Wu194de562007-05-06 14:50:30 -0700155#ifdef CONFIG_SERIAL_BFIN_DMA
156 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800157 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
158 uart->port.icount.tx += uart->tx_count;
159 uart->tx_count = 0;
160 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -0700161#else
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800162#if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
Roy Huangf4d640c92007-07-12 16:43:46 +0800163 /* Clear TFI bit */
164 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -0700165#endif
Mike Frysinger89bf6dc52008-05-07 11:41:26 +0800166 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c92007-07-12 16:43:46 +0800167#endif
Bryan Wu194de562007-05-06 14:50:30 -0700168}
169
170/*
171 * port is locked and interrupts are disabled
172 */
173static void bfin_serial_start_tx(struct uart_port *port)
174{
175 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700176 struct tty_struct *tty = uart->port.state->port.tty;
Graf Yang80d5c472009-01-02 13:40:22 +0000177
178 /*
179 * To avoid losting RX interrupt, we reset IR function
180 * before sending data.
181 */
Alan Coxadc8d742012-07-14 15:31:47 +0100182 if (tty->termios.c_line == N_IRDA)
Graf Yang80d5c472009-01-02 13:40:22 +0000183 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700184
185#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800186 if (uart->tx_done)
187 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700188#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800189 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800190 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800191#endif
Bryan Wu194de562007-05-06 14:50:30 -0700192}
193
194/*
195 * Interrupts are enabled
196 */
197static void bfin_serial_stop_rx(struct uart_port *port)
198{
199 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000200
Roy Huangf4d640c92007-07-12 16:43:46 +0800201 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700202}
203
Mike Frysinger50e2e152008-04-25 03:03:03 +0800204#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800205# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
206# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
207#else
208# define UART_GET_ANOMALY_THRESHOLD(uart) 0
209# define UART_SET_ANOMALY_THRESHOLD(uart, v)
210#endif
211
Bryan Wu194de562007-05-06 14:50:30 -0700212#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700213static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
214{
Bryan Wu194de562007-05-06 14:50:30 -0700215 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800216 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700217
Sonic Zhang759eb042007-11-21 17:00:32 +0800218 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800219 UART_CLEAR_LSR(uart);
220
Sonic Zhangbb7e58f2011-07-19 18:09:24 +0800221 ch = UART_GET_CHAR(uart);
222 uart->port.icount.rx++;
Bryan Wu194de562007-05-06 14:50:30 -0700223
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000224#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
225 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
Sonic Zhangcdc592d2010-01-19 06:13:12 -0500226 if (kgdb_connected && kgdboc_port_line == uart->port.line
227 && kgdboc_break_enabled)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000228 if (ch == 0x3) {/* Ctrl + C */
229 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800230 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800231 }
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000232
Jiri Slaby2e124b42013-01-03 15:53:06 +0100233 if (!uart->port.state)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000234 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800235#endif
Mike Frysinger50e2e152008-04-25 03:03:03 +0800236 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800237 /* The BF533 (and BF561) family of processors have a nice anomaly
238 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800239 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800240 * character comes across. Due to the nature of the flood, it is
241 * not possible to reliably catch bytes that are sent too quickly
242 * after this break. So application code talking to the Blackfin
243 * which sends a break signal must allow at least 1.5 character
244 * times after the end of the break for things to stabilize. This
245 * timeout was picked as it must absolutely be larger than 1
246 * character time +/- some percent. So 1.5 sounds good. All other
247 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800248 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800249 if (anomaly_start.tv_sec) {
250 struct timeval curr;
251 suseconds_t usecs;
252
253 if ((~ch & (~ch + 1)) & 0xff)
254 goto known_good_char;
255
256 do_gettimeofday(&curr);
257 if (curr.tv_sec - anomaly_start.tv_sec > 1)
258 goto known_good_char;
259
260 usecs = 0;
261 if (curr.tv_sec != anomaly_start.tv_sec)
262 usecs += USEC_PER_SEC;
263 usecs += curr.tv_usec - anomaly_start.tv_usec;
264
265 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
266 goto known_good_char;
267
268 if (ch)
269 anomaly_start.tv_sec = 0;
270 else
271 anomaly_start = curr;
272
273 return;
274
275 known_good_char:
Sonic Zhange482a232009-01-02 13:40:45 +0000276 status &= ~BI;
Mike Frysinger8851c712007-12-24 19:48:04 +0800277 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800278 }
Bryan Wu194de562007-05-06 14:50:30 -0700279 }
Bryan Wu194de562007-05-06 14:50:30 -0700280
281 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800282 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800283 if (bfin_revid() < 5)
284 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700285 uart->port.icount.brk++;
286 if (uart_handle_break(&uart->port))
287 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800288 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800289 }
290 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700291 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800292 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700293 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800294 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700295 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800296
297 status &= uart->port.read_status_mask;
298
299 if (status & BI)
300 flg = TTY_BREAK;
301 else if (status & PE)
302 flg = TTY_PARITY;
303 else if (status & FE)
304 flg = TTY_FRAME;
305 else
Bryan Wu194de562007-05-06 14:50:30 -0700306 flg = TTY_NORMAL;
307
308 if (uart_handle_sysrq_char(&uart->port, ch))
309 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700310
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800311 uart_insert_char(&uart->port, status, OE, ch, flg);
312
313 ignore_char:
Jiri Slaby2e124b42013-01-03 15:53:06 +0100314 tty_flip_buffer_push(&uart->port.state->port);
Bryan Wu194de562007-05-06 14:50:30 -0700315}
316
317static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
318{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700319 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700320
Bryan Wu194de562007-05-06 14:50:30 -0700321 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800322#if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100323 /* Clear TFI bit */
324 UART_PUT_LSR(uart, TFI);
325#endif
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100326 /* Anomaly notes:
327 * 05000215 - we always clear ETBEI within last UART TX
328 * interrupt to end a string. It is always set
329 * when start a new tx.
330 */
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100331 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700332 return;
333 }
334
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800335 if (uart->port.x_char) {
336 UART_PUT_CHAR(uart, uart->port.x_char);
337 uart->port.icount.tx++;
338 uart->port.x_char = 0;
339 }
340
Sonic Zhang759eb042007-11-21 17:00:32 +0800341 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
342 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
343 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
344 uart->port.icount.tx++;
Sonic Zhang759eb042007-11-21 17:00:32 +0800345 }
Bryan Wu194de562007-05-06 14:50:30 -0700346
347 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
348 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700349}
350
Aubrey Li5c4e4722007-05-21 18:09:38 +0800351static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
352{
353 struct bfin_serial_port *uart = dev_id;
354
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800355 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800356 bfin_serial_rx_chars(uart);
Sonic Zhang759eb042007-11-21 17:00:32 +0800357
Aubrey Li5c4e4722007-05-21 18:09:38 +0800358 return IRQ_HANDLED;
359}
360
361static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700362{
363 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700364
Roy Huangf4d640c92007-07-12 16:43:46 +0800365 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800366 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800367 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700368 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800369
Bryan Wu194de562007-05-06 14:50:30 -0700370 return IRQ_HANDLED;
371}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800372#endif
Bryan Wu194de562007-05-06 14:50:30 -0700373
Bryan Wu194de562007-05-06 14:50:30 -0700374#ifdef CONFIG_SERIAL_BFIN_DMA
375static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
376{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700377 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700378
Bryan Wu194de562007-05-06 14:50:30 -0700379 uart->tx_done = 0;
380
Bryan Wu194de562007-05-06 14:50:30 -0700381 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800382 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700383 uart->tx_done = 1;
384 return;
385 }
386
Sonic Zhang1b733512007-12-21 16:45:12 +0800387 if (uart->port.x_char) {
388 UART_PUT_CHAR(uart, uart->port.x_char);
389 uart->port.icount.tx++;
390 uart->port.x_char = 0;
391 }
392
Bryan Wu194de562007-05-06 14:50:30 -0700393 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
394 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
395 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
396 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
397 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
398 set_dma_config(uart->tx_dma_channel,
399 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
400 INTR_ON_BUF,
401 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800402 DATA_SIZE_8,
403 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700404 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
405 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
406 set_dma_x_modify(uart->tx_dma_channel, 1);
Graf Yangf9d36da2009-06-11 13:42:17 +0100407 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700408 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800409
Roy Huangf4d640c92007-07-12 16:43:46 +0800410 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700411}
412
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800413static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700414{
Bryan Wu194de562007-05-06 14:50:30 -0700415 int i, flg, status;
416
417 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800418 UART_CLEAR_LSR(uart);
419
Sonic Zhang56f5de82008-02-25 15:19:09 +0800420 uart->port.icount.rx +=
421 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
422 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700423
424 if (status & BI) {
425 uart->port.icount.brk++;
426 if (uart_handle_break(&uart->port))
427 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800428 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800429 }
430 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700431 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800432 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700433 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800434 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700435 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800436
437 status &= uart->port.read_status_mask;
438
439 if (status & BI)
440 flg = TTY_BREAK;
441 else if (status & PE)
442 flg = TTY_PARITY;
443 else if (status & FE)
444 flg = TTY_FRAME;
445 else
Bryan Wu194de562007-05-06 14:50:30 -0700446 flg = TTY_NORMAL;
447
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100448 for (i = uart->rx_dma_buf.tail; ; i++) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800449 if (i >= UART_XMIT_SIZE)
450 i = 0;
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100451 if (i == uart->rx_dma_buf.head)
452 break;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800453 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
454 uart_insert_char(&uart->port, status, OE,
455 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700456 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800457
458 dma_ignore_char:
Jiri Slaby2e124b42013-01-03 15:53:06 +0100459 tty_flip_buffer_push(&uart->port.state->port);
Bryan Wu194de562007-05-06 14:50:30 -0700460}
461
462void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
463{
Mike Frysinger59e4e3e2009-04-06 17:32:28 +0100464 int x_pos, pos;
Steven Miao9642dbe2012-11-07 13:27:56 +0800465 unsigned long flags;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000466
Steven Miao9642dbe2012-11-07 13:27:56 +0800467 spin_lock_irqsave(&uart->rx_lock, flags);
Bryan Wu194de562007-05-06 14:50:30 -0700468
Sonic Zhang8516c562009-06-11 13:38:16 +0100469 /* 2D DMA RX buffer ring is used. Because curr_y_count and
470 * curr_x_count can't be read as an atomic operation,
471 * curr_y_count should be read before curr_x_count. When
472 * curr_x_count is read, curr_y_count may already indicate
473 * next buffer line. But, the position calculated here is
474 * still indicate the old line. The wrong position data may
475 * be smaller than current buffer tail, which cause garbages
476 * are received if it is not prohibit.
477 */
Sonic Zhang56f5de82008-02-25 15:19:09 +0800478 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
479 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
480 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100481 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang56f5de82008-02-25 15:19:09 +0800482 uart->rx_dma_nrows = 0;
483 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700484 if (x_pos == DMA_RX_XCOUNT)
485 x_pos = 0;
486
487 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang8516c562009-06-11 13:38:16 +0100488 /* Ignore receiving data if new position is in the same line of
489 * current buffer tail and small.
490 */
491 if (pos > uart->rx_dma_buf.tail ||
492 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800493 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700494 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800495 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700496 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800497
Steven Miao9642dbe2012-11-07 13:27:56 +0800498 spin_unlock_irqrestore(&uart->rx_lock, flags);
Sonic Zhang68a784c2009-01-02 13:40:38 +0000499
Sonic Zhang0a278422008-04-25 04:36:47 +0800500 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700501}
502
503static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
504{
505 struct bfin_serial_port *uart = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700506 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700507
508 spin_lock(&uart->port.lock);
509 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700510 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800511 clear_dma_irqstat(uart->tx_dma_channel);
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100512 /* Anomaly notes:
513 * 05000215 - we always clear ETBEI within last UART TX
514 * interrupt to end a string. It is always set
515 * when start a new tx.
516 */
Roy Huangf4d640c92007-07-12 16:43:46 +0800517 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800518 uart->port.icount.tx += uart->tx_count;
Sonic Zhang239c25b2012-05-16 14:22:24 +0800519 if (!(xmit->tail == 0 && xmit->head == 0)) {
Sonic Zhang60f4b002012-03-13 15:51:55 +0800520 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
Sonic Zhang1b733512007-12-21 16:45:12 +0800521
Sonic Zhang60f4b002012-03-13 15:51:55 +0800522 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
523 uart_write_wakeup(&uart->port);
524 }
Sonic Zhang56f5de82008-02-25 15:19:09 +0800525
Sonic Zhang1b733512007-12-21 16:45:12 +0800526 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700527 }
528
529 spin_unlock(&uart->port.lock);
530 return IRQ_HANDLED;
531}
532
533static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
534{
535 struct bfin_serial_port *uart = dev_id;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800536 unsigned int irqstat;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100537 int x_pos, pos;
Sonic Zhang0711d852008-02-25 15:16:50 +0800538
Sonic Zhang0f66e502011-01-11 00:16:43 -0500539 spin_lock(&uart->rx_lock);
Bryan Wu194de562007-05-06 14:50:30 -0700540 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
541 clear_dma_irqstat(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100542
543 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
Sonic Zhang35ff6932009-06-11 13:42:57 +0100544 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100545 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100546 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang8516c562009-06-11 13:38:16 +0100547 uart->rx_dma_nrows = 0;
548
549 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
550 if (pos > uart->rx_dma_buf.tail ||
551 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
552 uart->rx_dma_buf.head = pos;
553 bfin_serial_dma_rx_chars(uart);
554 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
555 }
556
Sonic Zhang0f66e502011-01-11 00:16:43 -0500557 spin_unlock(&uart->rx_lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800558
Bryan Wu194de562007-05-06 14:50:30 -0700559 return IRQ_HANDLED;
560}
561#endif
562
563/*
564 * Return TIOCSER_TEMT when transmitter is not busy.
565 */
566static unsigned int bfin_serial_tx_empty(struct uart_port *port)
567{
568 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800569 unsigned int lsr;
Bryan Wu194de562007-05-06 14:50:30 -0700570
571 lsr = UART_GET_LSR(uart);
572 if (lsr & TEMT)
573 return TIOCSER_TEMT;
574 else
575 return 0;
576}
577
Bryan Wu194de562007-05-06 14:50:30 -0700578static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
579{
Mike Frysingercf686762007-06-11 16:12:49 +0800580 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang59bd2342012-05-16 14:22:26 +0800581 u32 lcr = UART_GET_LCR(uart);
Mike Frysingercf686762007-06-11 16:12:49 +0800582 if (break_state)
583 lcr |= SB;
584 else
585 lcr &= ~SB;
586 UART_PUT_LCR(uart, lcr);
587 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700588}
589
590static int bfin_serial_startup(struct uart_port *port)
591{
592 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
593
594#ifdef CONFIG_SERIAL_BFIN_DMA
595 dma_addr_t dma_handle;
596
597 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
598 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
599 return -EBUSY;
600 }
601
602 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
603 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
604 free_dma(uart->rx_dma_channel);
605 return -EBUSY;
606 }
607
608 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
609 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
610
611 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
612 uart->rx_dma_buf.head = 0;
613 uart->rx_dma_buf.tail = 0;
614 uart->rx_dma_nrows = 0;
615
616 set_dma_config(uart->rx_dma_channel,
617 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
618 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800619 DATA_SIZE_8,
620 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700621 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
622 set_dma_x_modify(uart->rx_dma_channel, 1);
623 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
624 set_dma_y_modify(uart->rx_dma_channel, 1);
625 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
626 enable_dma(uart->rx_dma_channel);
627
628 uart->rx_dma_timer.data = (unsigned long)(uart);
629 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
630 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
631 add_timer(&(uart->rx_dma_timer));
632#else
Sonic Zhang6f955702009-04-07 16:51:15 +0100633# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000634 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
635 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
636 kgdboc_break_enabled = 0;
637 else {
638# endif
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800639 if (request_irq(uart->rx_irq, bfin_serial_rx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700640 "BFIN_UART_RX", uart)) {
641 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
642 return -EBUSY;
643 }
644
645 if (request_irq
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800646 (uart->tx_irq, bfin_serial_tx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700647 "BFIN_UART_TX", uart)) {
648 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800649 free_irq(uart->rx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700650 return -EBUSY;
651 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100652
653# ifdef CONFIG_BF54x
654 {
Sonic Zhangb6100992010-10-27 04:16:47 -0400655 /*
656 * UART2 and UART3 on BF548 share interrupt PINs and DMA
657 * controllers with SPORT2 and SPORT3. UART rx and tx
658 * interrupts are generated in PIO mode only when configure
659 * their peripheral mapping registers properly, which means
660 * request corresponding DMA channels in PIO mode as well.
661 */
Sonic Zhangab2375f2008-10-13 10:33:51 +0100662 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
663
Sonic Zhang47918f02011-08-12 11:15:51 +0800664 switch (uart->rx_irq) {
Sonic Zhangab2375f2008-10-13 10:33:51 +0100665 case IRQ_UART3_RX:
666 uart_dma_ch_rx = CH_UART3_RX;
667 uart_dma_ch_tx = CH_UART3_TX;
668 break;
669 case IRQ_UART2_RX:
670 uart_dma_ch_rx = CH_UART2_RX;
671 uart_dma_ch_tx = CH_UART2_TX;
672 break;
673 default:
674 uart_dma_ch_rx = uart_dma_ch_tx = 0;
675 break;
Joe Perchesfc8114722013-10-08 16:14:21 -0700676 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100677
678 if (uart_dma_ch_rx &&
679 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
680 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800681 free_irq(uart->rx_irq, uart);
682 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100683 return -EBUSY;
684 }
685 if (uart_dma_ch_tx &&
686 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
687 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
688 free_dma(uart_dma_ch_rx);
Sonic Zhang47918f02011-08-12 11:15:51 +0800689 free_irq(uart->rx_irq, uart);
690 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100691 return -EBUSY;
692 }
693 }
694# endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100695# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000696 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
697 }
698# endif
Bryan Wu194de562007-05-06 14:50:30 -0700699#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100700
701#ifdef CONFIG_SERIAL_BFIN_CTSRTS
702 if (uart->cts_pin >= 0) {
703 if (request_irq(gpio_to_irq(uart->cts_pin),
704 bfin_serial_mctrl_cts_int,
705 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800706 0, "BFIN_UART_CTS", uart)) {
Sonic Zhang6f955702009-04-07 16:51:15 +0100707 uart->cts_pin = -1;
Joe Perchesa89f2462010-11-07 13:10:23 -0800708 pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
Sonic Zhang6f955702009-04-07 16:51:15 +0100709 }
710 }
Sonic Zhang32b44562011-12-05 15:12:50 +0800711 if (uart->rts_pin >= 0) {
712 if (gpio_request(uart->rts_pin, DRIVER_NAME)) {
713 pr_info("fail to request RTS PIN at GPIO_%d\n", uart->rts_pin);
714 uart->rts_pin = -1;
715 } else
716 gpio_direction_output(uart->rts_pin, 0);
717 }
Sonic Zhang6f955702009-04-07 16:51:15 +0100718#endif
Sonic Zhangd307d362009-04-07 16:52:26 +0100719#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhangb48dc712011-12-13 12:22:01 +0800720 if (uart->cts_pin >= 0) {
721 if (request_irq(uart->status_irq, bfin_serial_mctrl_cts_int,
Michael Opdenacker190c6cc2013-10-06 08:27:18 +0200722 0, "BFIN_UART_MODEM_STATUS", uart)) {
Sonic Zhangb48dc712011-12-13 12:22:01 +0800723 uart->cts_pin = -1;
724 dev_info(port->dev, "Unable to attach BlackFin UART Modem Status interrupt.\n");
725 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100726
Sonic Zhangb48dc712011-12-13 12:22:01 +0800727 /* CTS RTS PINs are negative assertive. */
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800728 UART_PUT_MCR(uart, UART_GET_MCR(uart) | ACTS);
Sonic Zhangb48dc712011-12-13 12:22:01 +0800729 UART_SET_IER(uart, EDSSI);
730 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100731#endif
732
Roy Huangf4d640c92007-07-12 16:43:46 +0800733 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700734 return 0;
735}
736
737static void bfin_serial_shutdown(struct uart_port *port)
738{
739 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
740
741#ifdef CONFIG_SERIAL_BFIN_DMA
742 disable_dma(uart->tx_dma_channel);
743 free_dma(uart->tx_dma_channel);
744 disable_dma(uart->rx_dma_channel);
745 free_dma(uart->rx_dma_channel);
746 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800747 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700748#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100749#ifdef CONFIG_BF54x
750 switch (uart->port.irq) {
751 case IRQ_UART3_RX:
752 free_dma(CH_UART3_RX);
753 free_dma(CH_UART3_TX);
754 break;
755 case IRQ_UART2_RX:
756 free_dma(CH_UART2_RX);
757 free_dma(CH_UART2_TX);
758 break;
759 default:
760 break;
Joe Perchesfc8114722013-10-08 16:14:21 -0700761 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100762#endif
Sonic Zhang47918f02011-08-12 11:15:51 +0800763 free_irq(uart->rx_irq, uart);
764 free_irq(uart->tx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700765#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100766
Sonic Zhangd307d362009-04-07 16:52:26 +0100767#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang6f955702009-04-07 16:51:15 +0100768 if (uart->cts_pin >= 0)
769 free_irq(gpio_to_irq(uart->cts_pin), uart);
Sonic Zhang32b44562011-12-05 15:12:50 +0800770 if (uart->rts_pin >= 0)
771 gpio_free(uart->rts_pin);
Sonic Zhangd307d362009-04-07 16:52:26 +0100772#endif
773#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang57afb392009-09-09 10:46:19 +0000774 if (uart->cts_pin >= 0)
Sonic Zhangd307d362009-04-07 16:52:26 +0100775 free_irq(uart->status_irq, uart);
776#endif
Bryan Wu194de562007-05-06 14:50:30 -0700777}
778
779static void
780bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
781 struct ktermios *old)
782{
783 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
784 unsigned long flags;
785 unsigned int baud, quot;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800786 unsigned int ier, lcr = 0;
Sonic Zhang1cd3f2d2012-11-19 14:40:56 +0800787 unsigned long timeout;
Bryan Wu194de562007-05-06 14:50:30 -0700788
Peter Hurley8bd67d7d22014-06-16 09:17:10 -0400789#ifdef CONFIG_SERIAL_BFIN_CTSRTS
790 if (old == NULL && uart->cts_pin != -1)
791 termios->c_cflag |= CRTSCTS;
792 else if (uart->cts_pin == -1)
793 termios->c_cflag &= ~CRTSCTS;
794#endif
795
Bryan Wu194de562007-05-06 14:50:30 -0700796 switch (termios->c_cflag & CSIZE) {
797 case CS8:
798 lcr = WLS(8);
799 break;
800 case CS7:
801 lcr = WLS(7);
802 break;
803 case CS6:
804 lcr = WLS(6);
805 break;
806 case CS5:
807 lcr = WLS(5);
808 break;
809 default:
Masanari Iida46e99c42012-10-24 23:29:41 +0900810 printk(KERN_ERR "%s: word length not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700811 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700812 }
813
Sonic Zhang84507792009-06-11 13:50:20 +0100814 /* Anomaly notes:
815 * 05000231 - STOP bit is always set to 1 whatever the user is set.
816 */
817 if (termios->c_cflag & CSTOPB) {
818 if (ANOMALY_05000231)
819 printk(KERN_WARNING "STOP bits other than 1 is not "
820 "supported in case of anomaly 05000231.\n");
821 else
822 lcr |= STB;
823 }
Mike Frysinger19aa6382007-06-11 16:16:45 +0800824 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700825 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800826 if (!(termios->c_cflag & PARODD))
827 lcr |= EPS;
828 if (termios->c_cflag & CMSPAR)
829 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700830
Sonic Zhang5bb06b62010-10-27 04:16:50 -0400831 spin_lock_irqsave(&uart->port.lock, flags);
832
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800833 port->read_status_mask = OE;
834 if (termios->c_iflag & INPCK)
835 port->read_status_mask |= (FE | PE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400836 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800837 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700838
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800839 /*
840 * Characters to ignore
841 */
842 port->ignore_status_mask = 0;
843 if (termios->c_iflag & IGNPAR)
844 port->ignore_status_mask |= FE | PE;
845 if (termios->c_iflag & IGNBRK) {
846 port->ignore_status_mask |= BI;
847 /*
848 * If we're ignoring parity and break indicators,
849 * ignore overruns too (for real raw support).
850 */
851 if (termios->c_iflag & IGNPAR)
852 port->ignore_status_mask |= OE;
853 }
Bryan Wu194de562007-05-06 14:50:30 -0700854
855 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Graf Yangca3e4422010-05-23 04:40:13 -0400856 quot = uart_get_divisor(port, baud);
857
858 /* If discipline is not IRDA, apply ANOMALY_05000230 */
859 if (termios->c_line != N_IRDA)
860 quot -= ANOMALY_05000230;
861
Mike Frysinger8851c712007-12-24 19:48:04 +0800862 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
863
Sonic Zhang1cd3f2d2012-11-19 14:40:56 +0800864 /* Wait till the transfer buffer is empty */
865 timeout = jiffies + msecs_to_jiffies(10);
866 while (UART_GET_GCTL(uart) & UCEN && !(UART_GET_LSR(uart) & TEMT))
867 if (time_after(jiffies, timeout)) {
868 dev_warn(port->dev, "timeout waiting for TX buffer empty\n");
869 break;
870 }
871
Bryan Wu194de562007-05-06 14:50:30 -0700872 /* Disable UART */
873 ier = UART_GET_IER(uart);
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800874 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) & ~UCEN);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800875 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700876
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800877 /* Set DLAB in LCR to Access CLK */
Mike Frysinger45828b82008-05-07 11:41:26 +0800878 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700879
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800880 UART_PUT_CLK(uart, quot);
Bryan Wu194de562007-05-06 14:50:30 -0700881 SSYNC();
882
883 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800884 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700885
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800886 UART_PUT_LCR(uart, (UART_GET_LCR(uart) & ~LCR_MASK) | lcr);
Bryan Wu194de562007-05-06 14:50:30 -0700887
888 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800889 UART_ENABLE_INTS(uart, ier);
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800890 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) | UCEN);
Bryan Wu194de562007-05-06 14:50:30 -0700891
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100892 /* Port speed changed, update the per-port timeout. */
893 uart_update_timeout(port, termios->c_cflag, baud);
894
Bryan Wu194de562007-05-06 14:50:30 -0700895 spin_unlock_irqrestore(&uart->port.lock, flags);
896}
897
898static const char *bfin_serial_type(struct uart_port *port)
899{
900 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
901
902 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
903}
904
905/*
906 * Release the memory region(s) being used by 'port'.
907 */
908static void bfin_serial_release_port(struct uart_port *port)
909{
910}
911
912/*
913 * Request the memory region(s) being used by 'port'.
914 */
915static int bfin_serial_request_port(struct uart_port *port)
916{
917 return 0;
918}
919
920/*
921 * Configure/autoconfigure the port.
922 */
923static void bfin_serial_config_port(struct uart_port *port, int flags)
924{
925 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
926
927 if (flags & UART_CONFIG_TYPE &&
928 bfin_serial_request_port(&uart->port) == 0)
929 uart->port.type = PORT_BFIN;
930}
931
932/*
933 * Verify the new serial_struct (for TIOCSSERIAL).
934 * The only change we allow are to the flags and type, and
935 * even then only between PORT_BFIN and PORT_UNKNOWN
936 */
937static int
938bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
939{
940 return 0;
941}
942
Graf Yang7d01b472008-02-29 11:31:08 +0800943/*
944 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
945 * In other cases, disable IrDA function.
946 */
Peter Hurley732a84a2014-11-05 13:11:43 -0500947static void bfin_serial_set_ldisc(struct uart_port *port,
948 struct ktermios *termios)
Graf Yang7d01b472008-02-29 11:31:08 +0800949{
Sonic Zhang57afb392009-09-09 10:46:19 +0000950 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800951 unsigned int val;
Graf Yang7d01b472008-02-29 11:31:08 +0800952
Peter Hurley732a84a2014-11-05 13:11:43 -0500953 switch (termios->c_line) {
Graf Yang7d01b472008-02-29 11:31:08 +0800954 case N_IRDA:
Sonic Zhang57afb392009-09-09 10:46:19 +0000955 val = UART_GET_GCTL(uart);
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800956 val |= (UMOD_IRDA | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000957 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800958 break;
959 default:
Sonic Zhang57afb392009-09-09 10:46:19 +0000960 val = UART_GET_GCTL(uart);
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800961 val &= ~(UMOD_MASK | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000962 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800963 }
964}
965
Sonic Zhang6f955702009-04-07 16:51:15 +0100966static void bfin_serial_reset_irda(struct uart_port *port)
967{
Sonic Zhang57afb392009-09-09 10:46:19 +0000968 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800969 unsigned int val;
Sonic Zhang6f955702009-04-07 16:51:15 +0100970
Sonic Zhang57afb392009-09-09 10:46:19 +0000971 val = UART_GET_GCTL(uart);
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800972 val &= ~(UMOD_MASK | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000973 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100974 SSYNC();
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800975 val |= (UMOD_IRDA | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000976 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100977 SSYNC();
978}
979
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000980#ifdef CONFIG_CONSOLE_POLL
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100981/* Anomaly notes:
982 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
983 * losing other bits of UART_LSR is not a problem here.
984 */
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000985static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
986{
987 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
988
989 while (!(UART_GET_LSR(uart) & THRE))
990 cpu_relax();
991
992 UART_CLEAR_DLAB(uart);
993 UART_PUT_CHAR(uart, (unsigned char)chr);
994}
995
996static int bfin_serial_poll_get_char(struct uart_port *port)
997{
998 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
999 unsigned char chr;
1000
1001 while (!(UART_GET_LSR(uart) & DR))
1002 cpu_relax();
1003
1004 UART_CLEAR_DLAB(uart);
1005 chr = UART_GET_CHAR(uart);
1006
1007 return chr;
1008}
1009#endif
1010
Bryan Wu194de562007-05-06 14:50:30 -07001011static struct uart_ops bfin_serial_pops = {
1012 .tx_empty = bfin_serial_tx_empty,
1013 .set_mctrl = bfin_serial_set_mctrl,
1014 .get_mctrl = bfin_serial_get_mctrl,
1015 .stop_tx = bfin_serial_stop_tx,
1016 .start_tx = bfin_serial_start_tx,
1017 .stop_rx = bfin_serial_stop_rx,
Bryan Wu194de562007-05-06 14:50:30 -07001018 .break_ctl = bfin_serial_break_ctl,
1019 .startup = bfin_serial_startup,
1020 .shutdown = bfin_serial_shutdown,
1021 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +08001022 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -07001023 .type = bfin_serial_type,
1024 .release_port = bfin_serial_release_port,
1025 .request_port = bfin_serial_request_port,
1026 .config_port = bfin_serial_config_port,
1027 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001028#ifdef CONFIG_CONSOLE_POLL
1029 .poll_put_char = bfin_serial_poll_put_char,
1030 .poll_get_char = bfin_serial_poll_get_char,
1031#endif
Bryan Wu194de562007-05-06 14:50:30 -07001032};
1033
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001034#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -07001035/*
1036 * If the port was already initialised (eg, by a boot loader),
1037 * try to determine the current setup.
1038 */
1039static void __init
1040bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1041 int *parity, int *bits)
1042{
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +08001043 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -07001044
1045 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1046 if (status == (ERBFI | ETBEI)) {
1047 /* ok, the port was enabled */
Sonic Zhang59bd2342012-05-16 14:22:26 +08001048 u32 lcr, clk;
Bryan Wu194de562007-05-06 14:50:30 -07001049
1050 lcr = UART_GET_LCR(uart);
1051
1052 *parity = 'n';
1053 if (lcr & PEN) {
1054 if (lcr & EPS)
1055 *parity = 'e';
1056 else
1057 *parity = 'o';
1058 }
Sonic Zhang59bd2342012-05-16 14:22:26 +08001059 *bits = ((lcr & WLS_MASK) >> WLS_OFFSET) + 5;
1060
Sonic Zhangb06d2f22012-05-16 14:22:23 +08001061 /* Set DLAB in LCR to Access CLK */
Mike Frysinger45828b82008-05-07 11:41:26 +08001062 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001063
Sonic Zhangb06d2f22012-05-16 14:22:23 +08001064 clk = UART_GET_CLK(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001065
1066 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001067 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001068
Sonic Zhangb06d2f22012-05-16 14:22:23 +08001069 *baud = get_sclk() / (16*clk);
Bryan Wu194de562007-05-06 14:50:30 -07001070 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001071 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001072}
Robin Getz0ae53642007-10-09 17:24:49 +08001073
Robin Getz0ae53642007-10-09 17:24:49 +08001074static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001075
Sonic Zhang57afb392009-09-09 10:46:19 +00001076static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1077{
1078 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1079 while (!(UART_GET_LSR(uart) & THRE))
1080 barrier();
1081 UART_PUT_CHAR(uart, ch);
1082}
1083
1084#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1085 defined (CONFIG_EARLY_PRINTK) */
1086
1087#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1088#define CLASS_BFIN_CONSOLE "bfin-console"
1089/*
1090 * Interrupts are disabled on entering
1091 */
1092static void
1093bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1094{
1095 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1096 unsigned long flags;
1097
1098 spin_lock_irqsave(&uart->port.lock, flags);
1099 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1100 spin_unlock_irqrestore(&uart->port.lock, flags);
1101
1102}
1103
Bryan Wu194de562007-05-06 14:50:30 -07001104static int __init
1105bfin_serial_console_setup(struct console *co, char *options)
1106{
1107 struct bfin_serial_port *uart;
1108 int baud = 57600;
1109 int bits = 8;
1110 int parity = 'n';
Sonic Zhangd307d362009-04-07 16:52:26 +01001111# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1112 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
Bryan Wu194de562007-05-06 14:50:30 -07001113 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001114# else
Bryan Wu194de562007-05-06 14:50:30 -07001115 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001116# endif
Bryan Wu194de562007-05-06 14:50:30 -07001117
1118 /*
1119 * Check whether an invalid uart number has been specified, and
1120 * if so, search for the first available port that does have
1121 * console support.
1122 */
Sonic Zhang57afb392009-09-09 10:46:19 +00001123 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1124 return -ENODEV;
1125
1126 uart = bfin_serial_ports[co->index];
1127 if (!uart)
1128 return -ENODEV;
Bryan Wu194de562007-05-06 14:50:30 -07001129
1130 if (options)
1131 uart_parse_options(options, &baud, &parity, &bits, &flow);
1132 else
1133 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1134
1135 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001136}
Robin Getz0ae53642007-10-09 17:24:49 +08001137
Bryan Wu194de562007-05-06 14:50:30 -07001138static struct console bfin_serial_console = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001139 .name = BFIN_SERIAL_DEV_NAME,
Bryan Wu194de562007-05-06 14:50:30 -07001140 .write = bfin_serial_console_write,
1141 .device = uart_console_device,
1142 .setup = bfin_serial_console_setup,
1143 .flags = CON_PRINTBUFFER,
1144 .index = -1,
1145 .data = &bfin_serial_reg,
1146};
Sonic Zhangbb7e58f2011-07-19 18:09:24 +08001147#define BFIN_SERIAL_CONSOLE (&bfin_serial_console)
Bryan Wu194de562007-05-06 14:50:30 -07001148#else
1149#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001150#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1151
Sonic Zhang57afb392009-09-09 10:46:19 +00001152#ifdef CONFIG_EARLY_PRINTK
1153static struct bfin_serial_port bfin_earlyprintk_port;
1154#define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
Robin Getz0ae53642007-10-09 17:24:49 +08001155
Sonic Zhang57afb392009-09-09 10:46:19 +00001156/*
1157 * Interrupts are disabled on entering
1158 */
1159static void
1160bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
Robin Getz0ae53642007-10-09 17:24:49 +08001161{
Sonic Zhang57afb392009-09-09 10:46:19 +00001162 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001163
Sonic Zhang57afb392009-09-09 10:46:19 +00001164 if (bfin_earlyprintk_port.port.line != co->index)
1165 return;
Robin Getz0ae53642007-10-09 17:24:49 +08001166
Sonic Zhang57afb392009-09-09 10:46:19 +00001167 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1168 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1169 bfin_serial_console_putchar);
1170 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
Robin Getz0ae53642007-10-09 17:24:49 +08001171}
1172
Robin Getz7de7c552009-06-11 13:38:57 +01001173/*
1174 * This should have a .setup or .early_setup in it, but then things get called
1175 * without the command line options, and the baud rate gets messed up - so
1176 * don't let the common infrastructure play with things. (see calls to setup
1177 * & earlysetup in ./kernel/printk.c:register_console()
1178 */
Sachin Kamat6734a832013-08-08 17:10:51 +05301179static struct console bfin_early_serial_console __initdata = {
Robin Getz0ae53642007-10-09 17:24:49 +08001180 .name = "early_BFuart",
Sonic Zhang57afb392009-09-09 10:46:19 +00001181 .write = bfin_earlyprintk_console_write,
Robin Getz0ae53642007-10-09 17:24:49 +08001182 .device = uart_console_device,
1183 .flags = CON_PRINTBUFFER,
Robin Getz0ae53642007-10-09 17:24:49 +08001184 .index = -1,
1185 .data = &bfin_serial_reg,
1186};
Sonic Zhang57afb392009-09-09 10:46:19 +00001187#endif
1188
1189static struct uart_driver bfin_serial_reg = {
1190 .owner = THIS_MODULE,
1191 .driver_name = DRIVER_NAME,
1192 .dev_name = BFIN_SERIAL_DEV_NAME,
1193 .major = BFIN_SERIAL_MAJOR,
1194 .minor = BFIN_SERIAL_MINOR,
1195 .nr = BFIN_UART_NR_PORTS,
1196 .cons = BFIN_SERIAL_CONSOLE,
1197};
1198
1199static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1200{
1201 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1202
1203 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1204}
1205
1206static int bfin_serial_resume(struct platform_device *pdev)
1207{
1208 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1209
1210 return uart_resume_port(&bfin_serial_reg, &uart->port);
1211}
1212
1213static int bfin_serial_probe(struct platform_device *pdev)
1214{
1215 struct resource *res;
1216 struct bfin_serial_port *uart = NULL;
1217 int ret = 0;
1218
1219 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1220 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1221 return -ENOENT;
1222 }
1223
1224 if (bfin_serial_ports[pdev->id] == NULL) {
1225
1226 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1227 if (!uart) {
1228 dev_err(&pdev->dev,
1229 "fail to malloc bfin_serial_port\n");
1230 return -ENOMEM;
1231 }
1232 bfin_serial_ports[pdev->id] = uart;
1233
1234#ifdef CONFIG_EARLY_PRINTK
1235 if (!(bfin_earlyprintk_port.port.membase
1236 && bfin_earlyprintk_port.port.line == pdev->id)) {
1237 /*
1238 * If the peripheral PINs of current port is allocated
1239 * in earlyprintk probe stage, don't do it again.
1240 */
1241#endif
1242 ret = peripheral_request_list(
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001243 dev_get_platdata(&pdev->dev),
Jingoo Han574de552013-07-30 17:06:57 +09001244 DRIVER_NAME);
Sonic Zhang57afb392009-09-09 10:46:19 +00001245 if (ret) {
1246 dev_err(&pdev->dev,
1247 "fail to request bfin serial peripherals\n");
1248 goto out_error_free_mem;
1249 }
1250#ifdef CONFIG_EARLY_PRINTK
1251 }
1252#endif
1253
1254 spin_lock_init(&uart->port.lock);
1255 uart->port.uartclk = get_sclk();
1256 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1257 uart->port.ops = &bfin_serial_pops;
1258 uart->port.line = pdev->id;
1259 uart->port.iotype = UPIO_MEM;
1260 uart->port.flags = UPF_BOOT_AUTOCONF;
1261
1262 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1263 if (res == NULL) {
1264 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1265 ret = -ENOENT;
1266 goto out_error_free_peripherals;
1267 }
1268
Joe Perches28f65c112011-06-09 09:13:32 -07001269 uart->port.membase = ioremap(res->start, resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001270 if (!uart->port.membase) {
1271 dev_err(&pdev->dev, "Cannot map uart IO\n");
1272 ret = -ENXIO;
1273 goto out_error_free_peripherals;
1274 }
1275 uart->port.mapbase = res->start;
1276
Sonic Zhang47918f02011-08-12 11:15:51 +08001277 uart->tx_irq = platform_get_irq(pdev, 0);
1278 if (uart->tx_irq < 0) {
1279 dev_err(&pdev->dev, "No uart TX IRQ specified\n");
Sonic Zhang57afb392009-09-09 10:46:19 +00001280 ret = -ENOENT;
1281 goto out_error_unmap;
1282 }
1283
Sonic Zhang47918f02011-08-12 11:15:51 +08001284 uart->rx_irq = platform_get_irq(pdev, 1);
1285 if (uart->rx_irq < 0) {
1286 dev_err(&pdev->dev, "No uart RX IRQ specified\n");
1287 ret = -ENOENT;
1288 goto out_error_unmap;
1289 }
1290 uart->port.irq = uart->rx_irq;
1291
1292 uart->status_irq = platform_get_irq(pdev, 2);
Sonic Zhang57afb392009-09-09 10:46:19 +00001293 if (uart->status_irq < 0) {
1294 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1295 ret = -ENOENT;
1296 goto out_error_unmap;
1297 }
1298
1299#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0f66e502011-01-11 00:16:43 -05001300 spin_lock_init(&uart->rx_lock);
Sonic Zhang57afb392009-09-09 10:46:19 +00001301 uart->tx_done = 1;
1302 uart->tx_count = 0;
1303
1304 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1305 if (res == NULL) {
1306 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1307 ret = -ENOENT;
1308 goto out_error_unmap;
1309 }
1310 uart->tx_dma_channel = res->start;
1311
1312 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1313 if (res == NULL) {
1314 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1315 ret = -ENOENT;
1316 goto out_error_unmap;
1317 }
1318 uart->rx_dma_channel = res->start;
1319
1320 init_timer(&(uart->rx_dma_timer));
1321#endif
1322
1323#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1324 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1325 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1326 if (res == NULL)
1327 uart->cts_pin = -1;
Peter Hurley8bd67d7d22014-06-16 09:17:10 -04001328 else
Sonic Zhang57afb392009-09-09 10:46:19 +00001329 uart->cts_pin = res->start;
1330
1331 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1332 if (res == NULL)
1333 uart->rts_pin = -1;
1334 else
1335 uart->rts_pin = res->start;
Sonic Zhang57afb392009-09-09 10:46:19 +00001336#endif
1337 }
1338
1339#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1340 if (!is_early_platform_device(pdev)) {
1341#endif
1342 uart = bfin_serial_ports[pdev->id];
1343 uart->port.dev = &pdev->dev;
1344 dev_set_drvdata(&pdev->dev, uart);
1345 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1346#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1347 }
1348#endif
1349
1350 if (!ret)
1351 return 0;
1352
1353 if (uart) {
1354out_error_unmap:
1355 iounmap(uart->port.membase);
1356out_error_free_peripherals:
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001357 peripheral_free_list(dev_get_platdata(&pdev->dev));
Sonic Zhang57afb392009-09-09 10:46:19 +00001358out_error_free_mem:
1359 kfree(uart);
1360 bfin_serial_ports[pdev->id] = NULL;
1361 }
1362
1363 return ret;
1364}
1365
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001366static int bfin_serial_remove(struct platform_device *pdev)
Sonic Zhang57afb392009-09-09 10:46:19 +00001367{
1368 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1369
1370 dev_set_drvdata(&pdev->dev, NULL);
1371
1372 if (uart) {
1373 uart_remove_one_port(&bfin_serial_reg, &uart->port);
Sonic Zhang57afb392009-09-09 10:46:19 +00001374 iounmap(uart->port.membase);
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001375 peripheral_free_list(dev_get_platdata(&pdev->dev));
Sonic Zhang57afb392009-09-09 10:46:19 +00001376 kfree(uart);
1377 bfin_serial_ports[pdev->id] = NULL;
1378 }
1379
1380 return 0;
1381}
1382
1383static struct platform_driver bfin_serial_driver = {
1384 .probe = bfin_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001385 .remove = bfin_serial_remove,
Sonic Zhang57afb392009-09-09 10:46:19 +00001386 .suspend = bfin_serial_suspend,
1387 .resume = bfin_serial_resume,
1388 .driver = {
1389 .name = DRIVER_NAME,
1390 .owner = THIS_MODULE,
1391 },
1392};
1393
1394#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
Sachin Kamat6734a832013-08-08 17:10:51 +05301395static struct early_platform_driver early_bfin_serial_driver __initdata = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001396 .class_str = CLASS_BFIN_CONSOLE,
1397 .pdrv = &bfin_serial_driver,
1398 .requested_id = EARLY_PLATFORM_ID_UNSET,
1399};
1400
1401static int __init bfin_serial_rs_console_init(void)
1402{
1403 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1404
1405 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1406
1407 register_console(&bfin_serial_console);
1408
1409 return 0;
1410}
1411console_initcall(bfin_serial_rs_console_init);
1412#endif
1413
1414#ifdef CONFIG_EARLY_PRINTK
1415/*
1416 * Memory can't be allocated dynamically during earlyprink init stage.
1417 * So, do individual probe for earlyprink with a static uart port variable.
1418 */
1419static int bfin_earlyprintk_probe(struct platform_device *pdev)
1420{
1421 struct resource *res;
1422 int ret;
1423
1424 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1425 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1426 return -ENOENT;
1427 }
1428
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001429 ret = peripheral_request_list(dev_get_platdata(&pdev->dev),
1430 DRIVER_NAME);
Sonic Zhang57afb392009-09-09 10:46:19 +00001431 if (ret) {
1432 dev_err(&pdev->dev,
1433 "fail to request bfin serial peripherals\n");
1434 return ret;
1435 }
1436
1437 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1438 if (res == NULL) {
1439 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1440 ret = -ENOENT;
1441 goto out_error_free_peripherals;
1442 }
1443
1444 bfin_earlyprintk_port.port.membase = ioremap(res->start,
Joe Perches28f65c112011-06-09 09:13:32 -07001445 resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001446 if (!bfin_earlyprintk_port.port.membase) {
1447 dev_err(&pdev->dev, "Cannot map uart IO\n");
1448 ret = -ENXIO;
1449 goto out_error_free_peripherals;
1450 }
1451 bfin_earlyprintk_port.port.mapbase = res->start;
1452 bfin_earlyprintk_port.port.line = pdev->id;
1453 bfin_earlyprintk_port.port.uartclk = get_sclk();
1454 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1455 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1456
1457 return 0;
1458
1459out_error_free_peripherals:
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001460 peripheral_free_list(dev_get_platdata(&pdev->dev));
Sonic Zhang57afb392009-09-09 10:46:19 +00001461
1462 return ret;
1463}
1464
1465static struct platform_driver bfin_earlyprintk_driver = {
1466 .probe = bfin_earlyprintk_probe,
1467 .driver = {
1468 .name = DRIVER_NAME,
1469 .owner = THIS_MODULE,
1470 },
1471};
1472
Sachin Kamat6734a832013-08-08 17:10:51 +05301473static struct early_platform_driver early_bfin_earlyprintk_driver __initdata = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001474 .class_str = CLASS_BFIN_EARLYPRINTK,
1475 .pdrv = &bfin_earlyprintk_driver,
1476 .requested_id = EARLY_PLATFORM_ID_UNSET,
1477};
Robin Getz0ae53642007-10-09 17:24:49 +08001478
1479struct console __init *bfin_earlyserial_init(unsigned int port,
1480 unsigned int cflag)
1481{
Robin Getz0ae53642007-10-09 17:24:49 +08001482 struct ktermios t;
Sonic Zhang57afb392009-09-09 10:46:19 +00001483 char port_name[20];
1484
1485 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1486 return NULL;
1487
1488 /*
1489 * Only probe resource of the given port in earlyprintk boot arg.
1490 * The expected port id should be indicated in port name string.
1491 */
1492 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1493 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1494 port_name);
1495 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1496
1497 if (!bfin_earlyprintk_port.port.membase)
1498 return NULL;
Robin Getz0ae53642007-10-09 17:24:49 +08001499
Sonic Zhang6d9e4492010-10-27 04:16:49 -04001500#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1501 /*
1502 * If we are using early serial, don't let the normal console rewind
1503 * log buffer, since that causes things to be printed multiple times
1504 */
1505 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1506#endif
1507
Robin Getz0ae53642007-10-09 17:24:49 +08001508 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001509 t.c_cflag = cflag;
1510 t.c_iflag = 0;
1511 t.c_oflag = 0;
1512 t.c_lflag = ICANON;
1513 t.c_line = port;
Sonic Zhang57afb392009-09-09 10:46:19 +00001514 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1515
Robin Getz0ae53642007-10-09 17:24:49 +08001516 return &bfin_early_serial_console;
1517}
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001518#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001519
Bryan Wu194de562007-05-06 14:50:30 -07001520static int __init bfin_serial_init(void)
1521{
1522 int ret;
1523
Sonic Zhang57afb392009-09-09 10:46:19 +00001524 pr_info("Blackfin serial driver\n");
Bryan Wu194de562007-05-06 14:50:30 -07001525
1526 ret = uart_register_driver(&bfin_serial_reg);
Sonic Zhang57afb392009-09-09 10:46:19 +00001527 if (ret) {
1528 pr_err("failed to register %s:%d\n",
1529 bfin_serial_reg.driver_name, ret);
Bryan Wu194de562007-05-06 14:50:30 -07001530 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001531
1532 ret = platform_driver_register(&bfin_serial_driver);
1533 if (ret) {
1534 pr_err("fail to register bfin uart\n");
1535 uart_unregister_driver(&bfin_serial_reg);
1536 }
1537
Bryan Wu194de562007-05-06 14:50:30 -07001538 return ret;
1539}
1540
1541static void __exit bfin_serial_exit(void)
1542{
1543 platform_driver_unregister(&bfin_serial_driver);
1544 uart_unregister_driver(&bfin_serial_reg);
1545}
1546
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001547
Bryan Wu194de562007-05-06 14:50:30 -07001548module_init(bfin_serial_init);
1549module_exit(bfin_serial_exit);
1550
Sonic Zhang57afb392009-09-09 10:46:19 +00001551MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
Bryan Wu194de562007-05-06 14:50:30 -07001552MODULE_DESCRIPTION("Blackfin generic serial port driver");
1553MODULE_LICENSE("GPL");
1554MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001555MODULE_ALIAS("platform:bfin-uart");