blob: a5cf0e70b3f6bcbeafe851385fdfc816adf3b491 [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 *
Mike Frysingerd273e2012008-10-13 10:33:06 +01004 * Copyright 2006-2008 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
15#include <linux/module.h>
16#include <linux/ioport.h>
17#include <linux/init.h>
18#include <linux/console.h>
19#include <linux/sysrq.h>
20#include <linux/platform_device.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/serial_core.h>
24
Sonic Zhang474f1a62007-06-29 16:35:17 +080025#ifdef CONFIG_KGDB_UART
26#include <linux/kgdb.h>
27#include <asm/irq_regs.h>
28#endif
29
Bryan Wu194de562007-05-06 14:50:30 -070030#include <asm/gpio.h>
Bryan Wu639f6572008-08-27 10:51:02 +080031#include <mach/bfin_serial_5xx.h>
Bryan Wu194de562007-05-06 14:50:30 -070032
33#ifdef CONFIG_SERIAL_BFIN_DMA
34#include <linux/dma-mapping.h>
35#include <asm/io.h>
36#include <asm/irq.h>
37#include <asm/cacheflush.h>
38#endif
39
40/* UART name and device definitions */
41#define BFIN_SERIAL_NAME "ttyBF"
42#define BFIN_SERIAL_MAJOR 204
43#define BFIN_SERIAL_MINOR 64
44
Mike Frysingerc9607ec2008-10-13 10:33:16 +010045static struct bfin_serial_port bfin_serial_ports[BFIN_UART_NR_PORTS];
46static int nr_active_ports = ARRAY_SIZE(bfin_serial_resource);
47
Bryan Wu194de562007-05-06 14:50:30 -070048/*
49 * Setup for console. Argument comes from the menuconfig
50 */
51#define DMA_RX_XCOUNT 512
52#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
53
Sonic Zhang0aef4562008-02-29 12:08:42 +080054#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
Sonic Zhangf30ac0c2008-06-19 17:46:39 +080055#define CTS_CHECK_JIFFIES (HZ / 50)
Bryan Wu194de562007-05-06 14:50:30 -070056
57#ifdef CONFIG_SERIAL_BFIN_DMA
58static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
59#else
Bryan Wu194de562007-05-06 14:50:30 -070060static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070061#endif
62
63static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
64
65/*
66 * interrupts are disabled on entry
67 */
68static void bfin_serial_stop_tx(struct uart_port *port)
69{
70 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang0711d852008-02-25 15:16:50 +080071 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -070072
Roy Huangf4d640c92007-07-12 16:43:46 +080073 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +080074 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +080075
Bryan Wu194de562007-05-06 14:50:30 -070076#ifdef CONFIG_SERIAL_BFIN_DMA
77 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +080078 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
79 uart->port.icount.tx += uart->tx_count;
80 uart->tx_count = 0;
81 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -070082#else
Roy Huangf4d640c92007-07-12 16:43:46 +080083#ifdef CONFIG_BF54x
Roy Huangf4d640c92007-07-12 16:43:46 +080084 /* Clear TFI bit */
85 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -070086#endif
Mike Frysinger89bf6dc52008-05-07 11:41:26 +080087 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c92007-07-12 16:43:46 +080088#endif
Bryan Wu194de562007-05-06 14:50:30 -070089}
90
91/*
92 * port is locked and interrupts are disabled
93 */
94static void bfin_serial_start_tx(struct uart_port *port)
95{
96 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
97
98#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +080099 if (uart->tx_done)
100 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700101#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800102 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800103 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800104#endif
Bryan Wu194de562007-05-06 14:50:30 -0700105}
106
107/*
108 * Interrupts are enabled
109 */
110static void bfin_serial_stop_rx(struct uart_port *port)
111{
112 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Mike Frysinger89bf6dc52008-05-07 11:41:26 +0800113#ifdef CONFIG_KGDB_UART
114 if (uart->port.line != CONFIG_KGDB_UART_PORT)
Sonic Zhanga359cca2007-10-10 16:47:58 +0800115#endif
Roy Huangf4d640c92007-07-12 16:43:46 +0800116 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700117}
118
119/*
120 * Set the modem control timer to fire immediately.
121 */
122static void bfin_serial_enable_ms(struct uart_port *port)
123{
124}
125
Sonic Zhang474f1a62007-06-29 16:35:17 +0800126#ifdef CONFIG_KGDB_UART
127static int kgdb_entry_state;
128
129void kgdb_put_debug_char(int chr)
130{
131 struct bfin_serial_port *uart;
Mike Frysingerd273e2012008-10-13 10:33:06 +0100132
Graf Yang2ade9722008-04-25 02:55:49 +0800133 if (CONFIG_KGDB_UART_PORT < 0
134 || CONFIG_KGDB_UART_PORT >= BFIN_UART_NR_PORTS)
Sonic Zhang474f1a62007-06-29 16:35:17 +0800135 uart = &bfin_serial_ports[0];
136 else
137 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
Mike Frysingerd273e2012008-10-13 10:33:06 +0100138
Sonic Zhang474f1a62007-06-29 16:35:17 +0800139 while (!(UART_GET_LSR(uart) & THRE)) {
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800140 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800141 }
Sonic Zhanga359cca2007-10-10 16:47:58 +0800142
Mike Frysinger45828b82008-05-07 11:41:26 +0800143 UART_CLEAR_DLAB(uart);
Sonic Zhang474f1a62007-06-29 16:35:17 +0800144 UART_PUT_CHAR(uart, (unsigned char)chr);
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800145 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800146}
147
148int kgdb_get_debug_char(void)
149{
150 struct bfin_serial_port *uart;
151 unsigned char chr;
152
Graf Yang2ade9722008-04-25 02:55:49 +0800153 if (CONFIG_KGDB_UART_PORT < 0
154 || CONFIG_KGDB_UART_PORT >= BFIN_UART_NR_PORTS)
Sonic Zhang474f1a62007-06-29 16:35:17 +0800155 uart = &bfin_serial_ports[0];
156 else
157 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
Mike Frysingerd273e2012008-10-13 10:33:06 +0100158
Sonic Zhang474f1a62007-06-29 16:35:17 +0800159 while(!(UART_GET_LSR(uart) & DR)) {
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800160 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800161 }
Mike Frysinger45828b82008-05-07 11:41:26 +0800162 UART_CLEAR_DLAB(uart);
Sonic Zhang474f1a62007-06-29 16:35:17 +0800163 chr = UART_GET_CHAR(uart);
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800164 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800165
166 return chr;
167}
168#endif
169
Mike Frysinger50e2e152008-04-25 03:03:03 +0800170#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800171# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
172# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
173#else
174# define UART_GET_ANOMALY_THRESHOLD(uart) 0
175# define UART_SET_ANOMALY_THRESHOLD(uart, v)
176#endif
177
Bryan Wu194de562007-05-06 14:50:30 -0700178#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700179static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
180{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100181 struct tty_struct *tty = uart->port.info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700182 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800183 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700184
Sonic Zhang759eb042007-11-21 17:00:32 +0800185 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800186 UART_CLEAR_LSR(uart);
187
188 ch = UART_GET_CHAR(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700189 uart->port.icount.rx++;
190
Sonic Zhang474f1a62007-06-29 16:35:17 +0800191#ifdef CONFIG_KGDB_UART
192 if (uart->port.line == CONFIG_KGDB_UART_PORT) {
Mike Frysinger89bf6dc52008-05-07 11:41:26 +0800193 struct pt_regs *regs = get_irq_regs();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800194 if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
195 kgdb_breakkey_pressed(regs);
196 return;
197 } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
198 kgdb_entry_state = 1;
199 } else if (kgdb_entry_state == 1 && ch == 'q') {
200 kgdb_entry_state = 0;
201 kgdb_breakkey_pressed(regs);
202 return;
203 } else if (ch == 0x3) {/* Ctrl + C */
204 kgdb_entry_state = 0;
205 kgdb_breakkey_pressed(regs);
206 return;
207 } else {
208 kgdb_entry_state = 0;
209 }
210 }
211#endif
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800212
Mike Frysinger50e2e152008-04-25 03:03:03 +0800213 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800214 /* The BF533 (and BF561) family of processors have a nice anomaly
215 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800216 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800217 * character comes across. Due to the nature of the flood, it is
218 * not possible to reliably catch bytes that are sent too quickly
219 * after this break. So application code talking to the Blackfin
220 * which sends a break signal must allow at least 1.5 character
221 * times after the end of the break for things to stabilize. This
222 * timeout was picked as it must absolutely be larger than 1
223 * character time +/- some percent. So 1.5 sounds good. All other
224 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800225 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800226 if (anomaly_start.tv_sec) {
227 struct timeval curr;
228 suseconds_t usecs;
229
230 if ((~ch & (~ch + 1)) & 0xff)
231 goto known_good_char;
232
233 do_gettimeofday(&curr);
234 if (curr.tv_sec - anomaly_start.tv_sec > 1)
235 goto known_good_char;
236
237 usecs = 0;
238 if (curr.tv_sec != anomaly_start.tv_sec)
239 usecs += USEC_PER_SEC;
240 usecs += curr.tv_usec - anomaly_start.tv_usec;
241
242 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
243 goto known_good_char;
244
245 if (ch)
246 anomaly_start.tv_sec = 0;
247 else
248 anomaly_start = curr;
249
250 return;
251
252 known_good_char:
253 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800254 }
Bryan Wu194de562007-05-06 14:50:30 -0700255 }
Bryan Wu194de562007-05-06 14:50:30 -0700256
257 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800258 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800259 if (bfin_revid() < 5)
260 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700261 uart->port.icount.brk++;
262 if (uart_handle_break(&uart->port))
263 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800264 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800265 }
266 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700267 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800268 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700269 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800270 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700271 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800272
273 status &= uart->port.read_status_mask;
274
275 if (status & BI)
276 flg = TTY_BREAK;
277 else if (status & PE)
278 flg = TTY_PARITY;
279 else if (status & FE)
280 flg = TTY_FRAME;
281 else
Bryan Wu194de562007-05-06 14:50:30 -0700282 flg = TTY_NORMAL;
283
284 if (uart_handle_sysrq_char(&uart->port, ch))
285 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700286
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800287 uart_insert_char(&uart->port, status, OE, ch, flg);
288
289 ignore_char:
290 tty_flip_buffer_push(tty);
Bryan Wu194de562007-05-06 14:50:30 -0700291}
292
293static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
294{
295 struct circ_buf *xmit = &uart->port.info->xmit;
296
Bryan Wu194de562007-05-06 14:50:30 -0700297 /*
298 * Check the modem control lines before
299 * transmitting anything.
300 */
301 bfin_serial_mctrl_check(uart);
302
303 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100304#ifdef CONFIG_BF54x
305 /* Clear TFI bit */
306 UART_PUT_LSR(uart, TFI);
307#endif
308 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700309 return;
310 }
311
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800312 if (uart->port.x_char) {
313 UART_PUT_CHAR(uart, uart->port.x_char);
314 uart->port.icount.tx++;
315 uart->port.x_char = 0;
316 }
317
Sonic Zhang759eb042007-11-21 17:00:32 +0800318 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
319 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
320 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
321 uart->port.icount.tx++;
322 SSYNC();
323 }
Bryan Wu194de562007-05-06 14:50:30 -0700324
325 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
326 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700327}
328
Aubrey Li5c4e4722007-05-21 18:09:38 +0800329static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
330{
331 struct bfin_serial_port *uart = dev_id;
332
Roy Huangf4d640c92007-07-12 16:43:46 +0800333 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800334 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800335 bfin_serial_rx_chars(uart);
336 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800337
Aubrey Li5c4e4722007-05-21 18:09:38 +0800338 return IRQ_HANDLED;
339}
340
341static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700342{
343 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700344
Roy Huangf4d640c92007-07-12 16:43:46 +0800345 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800346 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800347 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700348 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800349
Bryan Wu194de562007-05-06 14:50:30 -0700350 return IRQ_HANDLED;
351}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800352#endif
Bryan Wu194de562007-05-06 14:50:30 -0700353
Bryan Wu194de562007-05-06 14:50:30 -0700354#ifdef CONFIG_SERIAL_BFIN_DMA
355static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
356{
357 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700358
Bryan Wu194de562007-05-06 14:50:30 -0700359 uart->tx_done = 0;
360
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800361 /*
362 * Check the modem control lines before
363 * transmitting anything.
364 */
365 bfin_serial_mctrl_check(uart);
366
Bryan Wu194de562007-05-06 14:50:30 -0700367 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800368 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700369 uart->tx_done = 1;
370 return;
371 }
372
Sonic Zhang1b733512007-12-21 16:45:12 +0800373 if (uart->port.x_char) {
374 UART_PUT_CHAR(uart, uart->port.x_char);
375 uart->port.icount.tx++;
376 uart->port.x_char = 0;
377 }
378
Bryan Wu194de562007-05-06 14:50:30 -0700379 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
380 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
381 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
382 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
383 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
384 set_dma_config(uart->tx_dma_channel,
385 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
386 INTR_ON_BUF,
387 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800388 DATA_SIZE_8,
389 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700390 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
391 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
392 set_dma_x_modify(uart->tx_dma_channel, 1);
393 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800394
Roy Huangf4d640c92007-07-12 16:43:46 +0800395 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700396}
397
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800398static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700399{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100400 struct tty_struct *tty = uart->port.info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700401 int i, flg, status;
402
403 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800404 UART_CLEAR_LSR(uart);
405
Sonic Zhang56f5de82008-02-25 15:19:09 +0800406 uart->port.icount.rx +=
407 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
408 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700409
410 if (status & BI) {
411 uart->port.icount.brk++;
412 if (uart_handle_break(&uart->port))
413 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800414 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800415 }
416 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700417 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800418 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700419 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800420 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700421 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800422
423 status &= uart->port.read_status_mask;
424
425 if (status & BI)
426 flg = TTY_BREAK;
427 else if (status & PE)
428 flg = TTY_PARITY;
429 else if (status & FE)
430 flg = TTY_FRAME;
431 else
Bryan Wu194de562007-05-06 14:50:30 -0700432 flg = TTY_NORMAL;
433
Sonic Zhang56f5de82008-02-25 15:19:09 +0800434 for (i = uart->rx_dma_buf.tail; i != uart->rx_dma_buf.head; i++) {
435 if (i >= UART_XMIT_SIZE)
436 i = 0;
437 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
438 uart_insert_char(&uart->port, status, OE,
439 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700440 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800441
442 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700443 tty_flip_buffer_push(tty);
444}
445
446void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
447{
448 int x_pos, pos;
Bryan Wu194de562007-05-06 14:50:30 -0700449
Sonic Zhang56f5de82008-02-25 15:19:09 +0800450 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
451 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
452 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
453 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
454 uart->rx_dma_nrows = 0;
455 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700456 if (x_pos == DMA_RX_XCOUNT)
457 x_pos = 0;
458
459 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800460 if (pos != uart->rx_dma_buf.tail) {
461 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700462 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800463 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700464 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800465
Sonic Zhang0a278422008-04-25 04:36:47 +0800466 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700467}
468
469static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
470{
471 struct bfin_serial_port *uart = dev_id;
472 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700473
474 spin_lock(&uart->port.lock);
475 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700476 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800477 clear_dma_irqstat(uart->tx_dma_channel);
Roy Huangf4d640c92007-07-12 16:43:46 +0800478 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800479 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
480 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800481
Sonic Zhang56f5de82008-02-25 15:19:09 +0800482 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
483 uart_write_wakeup(&uart->port);
484
Sonic Zhang1b733512007-12-21 16:45:12 +0800485 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700486 }
487
488 spin_unlock(&uart->port.lock);
489 return IRQ_HANDLED;
490}
491
492static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
493{
494 struct bfin_serial_port *uart = dev_id;
495 unsigned short irqstat;
Sonic Zhang0711d852008-02-25 15:16:50 +0800496
Bryan Wu194de562007-05-06 14:50:30 -0700497 spin_lock(&uart->port.lock);
498 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
499 clear_dma_irqstat(uart->rx_dma_channel);
Bryan Wu194de562007-05-06 14:50:30 -0700500 spin_unlock(&uart->port.lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800501
Sonic Zhang0a278422008-04-25 04:36:47 +0800502 mod_timer(&(uart->rx_dma_timer), jiffies);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800503
Bryan Wu194de562007-05-06 14:50:30 -0700504 return IRQ_HANDLED;
505}
506#endif
507
508/*
509 * Return TIOCSER_TEMT when transmitter is not busy.
510 */
511static unsigned int bfin_serial_tx_empty(struct uart_port *port)
512{
513 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
514 unsigned short lsr;
515
516 lsr = UART_GET_LSR(uart);
517 if (lsr & TEMT)
518 return TIOCSER_TEMT;
519 else
520 return 0;
521}
522
523static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
524{
525#ifdef CONFIG_SERIAL_BFIN_CTSRTS
526 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
527 if (uart->cts_pin < 0)
528 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
529
Sonic Zhang1feaa512008-06-03 12:19:45 +0800530 if (UART_GET_CTS(uart))
Bryan Wu194de562007-05-06 14:50:30 -0700531 return TIOCM_DSR | TIOCM_CAR;
532 else
533#endif
534 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
535}
536
537static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
538{
539#ifdef CONFIG_SERIAL_BFIN_CTSRTS
540 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
541 if (uart->rts_pin < 0)
542 return;
543
544 if (mctrl & TIOCM_RTS)
Sonic Zhang1feaa512008-06-03 12:19:45 +0800545 UART_CLEAR_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700546 else
Sonic Zhang1feaa512008-06-03 12:19:45 +0800547 UART_SET_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700548#endif
549}
550
551/*
552 * Handle any change of modem status signal since we were last called.
553 */
554static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
555{
556#ifdef CONFIG_SERIAL_BFIN_CTSRTS
557 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -0700558 struct uart_info *info = uart->port.info;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100559 struct tty_struct *tty = info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700560
561 status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800562 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Bryan Wu194de562007-05-06 14:50:30 -0700563 if (!(status & TIOCM_CTS)) {
564 tty->hw_stopped = 1;
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800565 uart->cts_timer.data = (unsigned long)(uart);
566 uart->cts_timer.function = (void *)bfin_serial_mctrl_check;
567 uart->cts_timer.expires = jiffies + CTS_CHECK_JIFFIES;
568 add_timer(&(uart->cts_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700569 } else {
570 tty->hw_stopped = 0;
571 }
Bryan Wu194de562007-05-06 14:50:30 -0700572#endif
573}
574
575/*
576 * Interrupts are always disabled.
577 */
578static 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;
581 u16 lcr = UART_GET_LCR(uart);
582 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 Zhanga359cca2007-10-10 16:47:58 +0800633 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700634 "BFIN_UART_RX", uart)) {
Sonic Zhanga359cca2007-10-10 16:47:58 +0800635# ifdef CONFIG_KGDB_UART
636 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
637# endif
Bryan Wu194de562007-05-06 14:50:30 -0700638 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
639 return -EBUSY;
Sonic Zhanga359cca2007-10-10 16:47:58 +0800640# ifdef CONFIG_KGDB_UART
641 }
642# endif
Bryan Wu194de562007-05-06 14:50:30 -0700643 }
644
645 if (request_irq
Aubrey Li5c4e4722007-05-21 18:09:38 +0800646 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700647 "BFIN_UART_TX", uart)) {
648 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
649 free_irq(uart->port.irq, uart);
650 return -EBUSY;
651 }
652#endif
Roy Huangf4d640c92007-07-12 16:43:46 +0800653 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700654 return 0;
655}
656
657static void bfin_serial_shutdown(struct uart_port *port)
658{
659 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
660
661#ifdef CONFIG_SERIAL_BFIN_DMA
662 disable_dma(uart->tx_dma_channel);
663 free_dma(uart->tx_dma_channel);
664 disable_dma(uart->rx_dma_channel);
665 free_dma(uart->rx_dma_channel);
666 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800667 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700668#else
Sonic Zhang474f1a62007-06-29 16:35:17 +0800669#ifdef CONFIG_KGDB_UART
670 if (uart->port.line != CONFIG_KGDB_UART_PORT)
671#endif
Bryan Wu194de562007-05-06 14:50:30 -0700672 free_irq(uart->port.irq, uart);
673 free_irq(uart->port.irq+1, uart);
674#endif
675}
676
677static void
678bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
679 struct ktermios *old)
680{
681 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
682 unsigned long flags;
683 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800684 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700685
686 switch (termios->c_cflag & CSIZE) {
687 case CS8:
688 lcr = WLS(8);
689 break;
690 case CS7:
691 lcr = WLS(7);
692 break;
693 case CS6:
694 lcr = WLS(6);
695 break;
696 case CS5:
697 lcr = WLS(5);
698 break;
699 default:
700 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700701 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700702 }
703
704 if (termios->c_cflag & CSTOPB)
705 lcr |= STB;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800706 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700707 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800708 if (!(termios->c_cflag & PARODD))
709 lcr |= EPS;
710 if (termios->c_cflag & CMSPAR)
711 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700712
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800713 port->read_status_mask = OE;
714 if (termios->c_iflag & INPCK)
715 port->read_status_mask |= (FE | PE);
716 if (termios->c_iflag & (BRKINT | PARMRK))
717 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700718
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800719 /*
720 * Characters to ignore
721 */
722 port->ignore_status_mask = 0;
723 if (termios->c_iflag & IGNPAR)
724 port->ignore_status_mask |= FE | PE;
725 if (termios->c_iflag & IGNBRK) {
726 port->ignore_status_mask |= BI;
727 /*
728 * If we're ignoring parity and break indicators,
729 * ignore overruns too (for real raw support).
730 */
731 if (termios->c_iflag & IGNPAR)
732 port->ignore_status_mask |= OE;
733 }
Bryan Wu194de562007-05-06 14:50:30 -0700734
735 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
736 quot = uart_get_divisor(port, baud);
737 spin_lock_irqsave(&uart->port.lock, flags);
738
Mike Frysinger8851c712007-12-24 19:48:04 +0800739 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
740
Bryan Wu194de562007-05-06 14:50:30 -0700741 /* Disable UART */
742 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800743 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700744
745 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800746 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700747
748 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700749 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
750 SSYNC();
751
752 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800753 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700754
755 UART_PUT_LCR(uart, lcr);
756
757 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800758 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700759
760 val = UART_GET_GCTL(uart);
761 val |= UCEN;
762 UART_PUT_GCTL(uart, val);
763
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100764 /* Port speed changed, update the per-port timeout. */
765 uart_update_timeout(port, termios->c_cflag, baud);
766
Bryan Wu194de562007-05-06 14:50:30 -0700767 spin_unlock_irqrestore(&uart->port.lock, flags);
768}
769
770static const char *bfin_serial_type(struct uart_port *port)
771{
772 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
773
774 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
775}
776
777/*
778 * Release the memory region(s) being used by 'port'.
779 */
780static void bfin_serial_release_port(struct uart_port *port)
781{
782}
783
784/*
785 * Request the memory region(s) being used by 'port'.
786 */
787static int bfin_serial_request_port(struct uart_port *port)
788{
789 return 0;
790}
791
792/*
793 * Configure/autoconfigure the port.
794 */
795static void bfin_serial_config_port(struct uart_port *port, int flags)
796{
797 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
798
799 if (flags & UART_CONFIG_TYPE &&
800 bfin_serial_request_port(&uart->port) == 0)
801 uart->port.type = PORT_BFIN;
802}
803
804/*
805 * Verify the new serial_struct (for TIOCSSERIAL).
806 * The only change we allow are to the flags and type, and
807 * even then only between PORT_BFIN and PORT_UNKNOWN
808 */
809static int
810bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
811{
812 return 0;
813}
814
Graf Yang7d01b472008-02-29 11:31:08 +0800815/*
816 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
817 * In other cases, disable IrDA function.
818 */
Graf Yang3b8458a2008-06-07 15:36:33 +0800819static void bfin_serial_set_ldisc(struct uart_port *port)
Graf Yang7d01b472008-02-29 11:31:08 +0800820{
Graf Yang3b8458a2008-06-07 15:36:33 +0800821 int line = port->line;
Graf Yang7d01b472008-02-29 11:31:08 +0800822 unsigned short val;
823
Takashi Iwaia88487c2008-07-16 21:54:42 +0100824 if (line >= port->info->port.tty->driver->num)
Graf Yang7d01b472008-02-29 11:31:08 +0800825 return;
826
Alan Coxb1cbefe2008-08-04 17:22:11 +0100827 switch (port->info->port.tty->termios->c_line) {
Graf Yang7d01b472008-02-29 11:31:08 +0800828 case N_IRDA:
829 val = UART_GET_GCTL(&bfin_serial_ports[line]);
830 val |= (IREN | RPOLC);
831 UART_PUT_GCTL(&bfin_serial_ports[line], val);
832 break;
833 default:
834 val = UART_GET_GCTL(&bfin_serial_ports[line]);
835 val &= ~(IREN | RPOLC);
836 UART_PUT_GCTL(&bfin_serial_ports[line], val);
837 }
838}
839
Bryan Wu194de562007-05-06 14:50:30 -0700840static struct uart_ops bfin_serial_pops = {
841 .tx_empty = bfin_serial_tx_empty,
842 .set_mctrl = bfin_serial_set_mctrl,
843 .get_mctrl = bfin_serial_get_mctrl,
844 .stop_tx = bfin_serial_stop_tx,
845 .start_tx = bfin_serial_start_tx,
846 .stop_rx = bfin_serial_stop_rx,
847 .enable_ms = bfin_serial_enable_ms,
848 .break_ctl = bfin_serial_break_ctl,
849 .startup = bfin_serial_startup,
850 .shutdown = bfin_serial_shutdown,
851 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +0800852 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -0700853 .type = bfin_serial_type,
854 .release_port = bfin_serial_release_port,
855 .request_port = bfin_serial_request_port,
856 .config_port = bfin_serial_config_port,
857 .verify_port = bfin_serial_verify_port,
858};
859
860static void __init bfin_serial_init_ports(void)
861{
862 static int first = 1;
863 int i;
864
865 if (!first)
866 return;
867 first = 0;
868
Mike Frysingerc9607ec2008-10-13 10:33:16 +0100869 for (i = 0; i < nr_active_ports; i++) {
Bryan Wu194de562007-05-06 14:50:30 -0700870 bfin_serial_ports[i].port.uartclk = get_sclk();
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100871 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
Bryan Wu194de562007-05-06 14:50:30 -0700872 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
873 bfin_serial_ports[i].port.line = i;
874 bfin_serial_ports[i].port.iotype = UPIO_MEM;
875 bfin_serial_ports[i].port.membase =
876 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
877 bfin_serial_ports[i].port.mapbase =
878 bfin_serial_resource[i].uart_base_addr;
879 bfin_serial_ports[i].port.irq =
880 bfin_serial_resource[i].uart_irq;
881 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
882#ifdef CONFIG_SERIAL_BFIN_DMA
883 bfin_serial_ports[i].tx_done = 1;
884 bfin_serial_ports[i].tx_count = 0;
885 bfin_serial_ports[i].tx_dma_channel =
886 bfin_serial_resource[i].uart_tx_dma_channel;
887 bfin_serial_ports[i].rx_dma_channel =
888 bfin_serial_resource[i].uart_rx_dma_channel;
889 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700890#endif
891#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800892 init_timer(&(bfin_serial_ports[i].cts_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700893 bfin_serial_ports[i].cts_pin =
894 bfin_serial_resource[i].uart_cts_pin;
895 bfin_serial_ports[i].rts_pin =
896 bfin_serial_resource[i].uart_rts_pin;
897#endif
898 bfin_serial_hw_init(&bfin_serial_ports[i]);
Bryan Wu194de562007-05-06 14:50:30 -0700899 }
Roy Huangf4d640c92007-07-12 16:43:46 +0800900
Bryan Wu194de562007-05-06 14:50:30 -0700901}
902
903#ifdef CONFIG_SERIAL_BFIN_CONSOLE
Bryan Wu194de562007-05-06 14:50:30 -0700904/*
905 * If the port was already initialised (eg, by a boot loader),
906 * try to determine the current setup.
907 */
908static void __init
909bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
910 int *parity, int *bits)
911{
912 unsigned short status;
913
914 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
915 if (status == (ERBFI | ETBEI)) {
916 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +0800917 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -0700918
919 lcr = UART_GET_LCR(uart);
920
921 *parity = 'n';
922 if (lcr & PEN) {
923 if (lcr & EPS)
924 *parity = 'e';
925 else
926 *parity = 'o';
927 }
928 switch (lcr & 0x03) {
929 case 0: *bits = 5; break;
930 case 1: *bits = 6; break;
931 case 2: *bits = 7; break;
932 case 3: *bits = 8; break;
933 }
934 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800935 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700936
937 dll = UART_GET_DLL(uart);
938 dlh = UART_GET_DLH(uart);
939
940 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800941 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700942
943 *baud = get_sclk() / (16*(dll | dlh << 8));
944 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700945 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -0700946}
Robin Getz0ae53642007-10-09 17:24:49 +0800947#endif
948
949#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
950static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -0700951
952static int __init
953bfin_serial_console_setup(struct console *co, char *options)
954{
955 struct bfin_serial_port *uart;
Robin Getz0ae53642007-10-09 17:24:49 +0800956# ifdef CONFIG_SERIAL_BFIN_CONSOLE
Bryan Wu194de562007-05-06 14:50:30 -0700957 int baud = 57600;
958 int bits = 8;
959 int parity = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +0800960# ifdef CONFIG_SERIAL_BFIN_CTSRTS
Bryan Wu194de562007-05-06 14:50:30 -0700961 int flow = 'r';
Robin Getz0ae53642007-10-09 17:24:49 +0800962# else
Bryan Wu194de562007-05-06 14:50:30 -0700963 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +0800964# endif
965# endif
Bryan Wu194de562007-05-06 14:50:30 -0700966
967 /*
968 * Check whether an invalid uart number has been specified, and
969 * if so, search for the first available port that does have
970 * console support.
971 */
Mike Frysingerc9607ec2008-10-13 10:33:16 +0100972 if (co->index == -1 || co->index >= nr_active_ports)
Bryan Wu194de562007-05-06 14:50:30 -0700973 co->index = 0;
974 uart = &bfin_serial_ports[co->index];
975
Robin Getz0ae53642007-10-09 17:24:49 +0800976# ifdef CONFIG_SERIAL_BFIN_CONSOLE
Bryan Wu194de562007-05-06 14:50:30 -0700977 if (options)
978 uart_parse_options(options, &baud, &parity, &bits, &flow);
979 else
980 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
981
982 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +0800983# else
984 return 0;
985# endif
986}
987#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
988 defined (CONFIG_EARLY_PRINTK) */
989
990#ifdef CONFIG_SERIAL_BFIN_CONSOLE
991static void bfin_serial_console_putchar(struct uart_port *port, int ch)
992{
993 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
994 while (!(UART_GET_LSR(uart) & THRE))
995 barrier();
996 UART_PUT_CHAR(uart, ch);
997 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700998}
999
Robin Getz0ae53642007-10-09 17:24:49 +08001000/*
1001 * Interrupts are disabled on entering
1002 */
1003static void
1004bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1005{
1006 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1007 int flags = 0;
1008
1009 spin_lock_irqsave(&uart->port.lock, flags);
1010 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1011 spin_unlock_irqrestore(&uart->port.lock, flags);
1012
1013}
1014
Bryan Wu194de562007-05-06 14:50:30 -07001015static struct console bfin_serial_console = {
1016 .name = BFIN_SERIAL_NAME,
1017 .write = bfin_serial_console_write,
1018 .device = uart_console_device,
1019 .setup = bfin_serial_console_setup,
1020 .flags = CON_PRINTBUFFER,
1021 .index = -1,
1022 .data = &bfin_serial_reg,
1023};
1024
1025static int __init bfin_serial_rs_console_init(void)
1026{
1027 bfin_serial_init_ports();
1028 register_console(&bfin_serial_console);
Sonic Zhang474f1a62007-06-29 16:35:17 +08001029#ifdef CONFIG_KGDB_UART
1030 kgdb_entry_state = 0;
1031 init_kgdb_uart();
1032#endif
Bryan Wu194de562007-05-06 14:50:30 -07001033 return 0;
1034}
1035console_initcall(bfin_serial_rs_console_init);
1036
1037#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1038#else
1039#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001040#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1041
1042
1043#ifdef CONFIG_EARLY_PRINTK
1044static __init void early_serial_putc(struct uart_port *port, int ch)
1045{
1046 unsigned timeout = 0xffff;
1047 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1048
1049 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1050 cpu_relax();
1051 UART_PUT_CHAR(uart, ch);
1052}
1053
1054static __init void early_serial_write(struct console *con, const char *s,
1055 unsigned int n)
1056{
1057 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1058 unsigned int i;
1059
1060 for (i = 0; i < n; i++, s++) {
1061 if (*s == '\n')
1062 early_serial_putc(&uart->port, '\r');
1063 early_serial_putc(&uart->port, *s);
1064 }
1065}
1066
Mike Frysingerc1113402008-10-13 10:32:35 +01001067static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001068 .name = "early_BFuart",
1069 .write = early_serial_write,
1070 .device = uart_console_device,
1071 .flags = CON_PRINTBUFFER,
1072 .setup = bfin_serial_console_setup,
1073 .index = -1,
1074 .data = &bfin_serial_reg,
1075};
1076
1077struct console __init *bfin_earlyserial_init(unsigned int port,
1078 unsigned int cflag)
1079{
1080 struct bfin_serial_port *uart;
1081 struct ktermios t;
1082
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001083 if (port == -1 || port >= nr_active_ports)
Robin Getz0ae53642007-10-09 17:24:49 +08001084 port = 0;
1085 bfin_serial_init_ports();
1086 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001087 uart = &bfin_serial_ports[port];
1088 t.c_cflag = cflag;
1089 t.c_iflag = 0;
1090 t.c_oflag = 0;
1091 t.c_lflag = ICANON;
1092 t.c_line = port;
1093 bfin_serial_set_termios(&uart->port, &t, &t);
1094 return &bfin_early_serial_console;
1095}
1096
1097#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
Bryan Wu194de562007-05-06 14:50:30 -07001098
1099static struct uart_driver bfin_serial_reg = {
1100 .owner = THIS_MODULE,
1101 .driver_name = "bfin-uart",
1102 .dev_name = BFIN_SERIAL_NAME,
1103 .major = BFIN_SERIAL_MAJOR,
1104 .minor = BFIN_SERIAL_MINOR,
Graf Yang2ade9722008-04-25 02:55:49 +08001105 .nr = BFIN_UART_NR_PORTS,
Bryan Wu194de562007-05-06 14:50:30 -07001106 .cons = BFIN_SERIAL_CONSOLE,
1107};
1108
1109static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1110{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001111 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001112
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001113 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001114 if (bfin_serial_ports[i].port.dev != &dev->dev)
1115 continue;
1116 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1117 }
Bryan Wu194de562007-05-06 14:50:30 -07001118
1119 return 0;
1120}
1121
1122static int bfin_serial_resume(struct platform_device *dev)
1123{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001124 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001125
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001126 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001127 if (bfin_serial_ports[i].port.dev != &dev->dev)
1128 continue;
1129 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1130 }
Bryan Wu194de562007-05-06 14:50:30 -07001131
1132 return 0;
1133}
1134
1135static int bfin_serial_probe(struct platform_device *dev)
1136{
1137 struct resource *res = dev->resource;
1138 int i;
1139
1140 for (i = 0; i < dev->num_resources; i++, res++)
1141 if (res->flags & IORESOURCE_MEM)
1142 break;
1143
1144 if (i < dev->num_resources) {
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001145 for (i = 0; i < nr_active_ports; i++, res++) {
Bryan Wu194de562007-05-06 14:50:30 -07001146 if (bfin_serial_ports[i].port.mapbase != res->start)
1147 continue;
1148 bfin_serial_ports[i].port.dev = &dev->dev;
1149 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
Bryan Wu194de562007-05-06 14:50:30 -07001150 }
1151 }
1152
1153 return 0;
1154}
1155
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001156static int bfin_serial_remove(struct platform_device *dev)
Bryan Wu194de562007-05-06 14:50:30 -07001157{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001158 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001159
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001160 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001161 if (bfin_serial_ports[i].port.dev != &dev->dev)
1162 continue;
1163 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1164 bfin_serial_ports[i].port.dev = NULL;
Bryan Wu194de562007-05-06 14:50:30 -07001165#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001166 gpio_free(bfin_serial_ports[i].cts_pin);
1167 gpio_free(bfin_serial_ports[i].rts_pin);
Bryan Wu194de562007-05-06 14:50:30 -07001168#endif
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001169 }
Bryan Wu194de562007-05-06 14:50:30 -07001170
1171 return 0;
1172}
1173
1174static struct platform_driver bfin_serial_driver = {
1175 .probe = bfin_serial_probe,
1176 .remove = bfin_serial_remove,
1177 .suspend = bfin_serial_suspend,
1178 .resume = bfin_serial_resume,
1179 .driver = {
1180 .name = "bfin-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001181 .owner = THIS_MODULE,
Bryan Wu194de562007-05-06 14:50:30 -07001182 },
1183};
1184
1185static int __init bfin_serial_init(void)
1186{
1187 int ret;
Sonic Zhang474f1a62007-06-29 16:35:17 +08001188#ifdef CONFIG_KGDB_UART
1189 struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
Sonic Zhanga359cca2007-10-10 16:47:58 +08001190 struct ktermios t;
Sonic Zhang474f1a62007-06-29 16:35:17 +08001191#endif
Bryan Wu194de562007-05-06 14:50:30 -07001192
1193 pr_info("Serial: Blackfin serial driver\n");
1194
1195 bfin_serial_init_ports();
1196
1197 ret = uart_register_driver(&bfin_serial_reg);
1198 if (ret == 0) {
1199 ret = platform_driver_register(&bfin_serial_driver);
1200 if (ret) {
1201 pr_debug("uart register failed\n");
1202 uart_unregister_driver(&bfin_serial_reg);
1203 }
1204 }
Sonic Zhang474f1a62007-06-29 16:35:17 +08001205#ifdef CONFIG_KGDB_UART
1206 if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
Sonic Zhanga359cca2007-10-10 16:47:58 +08001207 request_irq(uart->port.irq, bfin_serial_rx_int,
Sonic Zhang474f1a62007-06-29 16:35:17 +08001208 IRQF_DISABLED, "BFIN_UART_RX", uart);
1209 pr_info("Request irq for kgdb uart port\n");
Sonic Zhanga359cca2007-10-10 16:47:58 +08001210 UART_SET_IER(uart, ERBFI);
Mike Frysingerd5148ff2007-07-25 11:57:42 +08001211 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +08001212 t.c_cflag = CS8|B57600;
1213 t.c_iflag = 0;
1214 t.c_oflag = 0;
1215 t.c_lflag = ICANON;
1216 t.c_line = CONFIG_KGDB_UART_PORT;
1217 bfin_serial_set_termios(&uart->port, &t, &t);
1218 }
1219#endif
Bryan Wu194de562007-05-06 14:50:30 -07001220 return ret;
1221}
1222
1223static void __exit bfin_serial_exit(void)
1224{
1225 platform_driver_unregister(&bfin_serial_driver);
1226 uart_unregister_driver(&bfin_serial_reg);
1227}
1228
1229module_init(bfin_serial_init);
1230module_exit(bfin_serial_exit);
1231
1232MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1233MODULE_DESCRIPTION("Blackfin generic serial port driver");
1234MODULE_LICENSE("GPL");
1235MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001236MODULE_ALIAS("platform:bfin-uart");