blob: 8dab6644297c39a9992d7d61505d0213d34edde1 [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 Frysinger1ba7a3e2008-01-11 15:56:26 +08004 * Copyright 2006-2007 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>
31#include <asm/mach/bfin_serial_5xx.h>
32
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
45/*
46 * Setup for console. Argument comes from the menuconfig
47 */
48#define DMA_RX_XCOUNT 512
49#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
50
Sonic Zhang0aef4562008-02-29 12:08:42 +080051#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
Bryan Wu194de562007-05-06 14:50:30 -070052
53#ifdef CONFIG_SERIAL_BFIN_DMA
54static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
55#else
Bryan Wu194de562007-05-06 14:50:30 -070056static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070057#endif
58
59static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
60
61/*
62 * interrupts are disabled on entry
63 */
64static void bfin_serial_stop_tx(struct uart_port *port)
65{
66 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang0711d852008-02-25 15:16:50 +080067 struct circ_buf *xmit = &uart->port.info->xmit;
Sonic Zhang1b733512007-12-21 16:45:12 +080068#if !defined(CONFIG_BF54x) && !defined(CONFIG_SERIAL_BFIN_DMA)
Sonic Zhang759eb042007-11-21 17:00:32 +080069 unsigned short ier;
70#endif
Bryan Wu194de562007-05-06 14:50:30 -070071
Roy Huangf4d640c92007-07-12 16:43:46 +080072 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +080073 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +080074
Bryan Wu194de562007-05-06 14:50:30 -070075#ifdef CONFIG_SERIAL_BFIN_DMA
76 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +080077 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
78 uart->port.icount.tx += uart->tx_count;
79 uart->tx_count = 0;
80 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -070081#else
Roy Huangf4d640c92007-07-12 16:43:46 +080082#ifdef CONFIG_BF54x
Roy Huangf4d640c92007-07-12 16:43:46 +080083 /* Clear TFI bit */
84 UART_PUT_LSR(uart, TFI);
85 UART_CLEAR_IER(uart, ETBEI);
86#else
Bryan Wu194de562007-05-06 14:50:30 -070087 ier = UART_GET_IER(uart);
88 ier &= ~ETBEI;
89 UART_PUT_IER(uart, ier);
90#endif
Roy Huangf4d640c92007-07-12 16:43:46 +080091#endif
Bryan Wu194de562007-05-06 14:50:30 -070092}
93
94/*
95 * port is locked and interrupts are disabled
96 */
97static void bfin_serial_start_tx(struct uart_port *port)
98{
99 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
100
101#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800102 if (uart->tx_done)
103 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700104#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800105#ifdef CONFIG_BF54x
106 UART_SET_IER(uart, ETBEI);
107#else
Bryan Wu194de562007-05-06 14:50:30 -0700108 unsigned short ier;
109 ier = UART_GET_IER(uart);
110 ier |= ETBEI;
111 UART_PUT_IER(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700112#endif
Sonic Zhanga359cca2007-10-10 16:47:58 +0800113 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800114#endif
Bryan Wu194de562007-05-06 14:50:30 -0700115}
116
117/*
118 * Interrupts are enabled
119 */
120static void bfin_serial_stop_rx(struct uart_port *port)
121{
122 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhanga359cca2007-10-10 16:47:58 +0800123#ifdef CONFIG_KGDB_UART
124 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
125#endif
Roy Huangf4d640c92007-07-12 16:43:46 +0800126#ifdef CONFIG_BF54x
127 UART_CLEAR_IER(uart, ERBFI);
128#else
Bryan Wu194de562007-05-06 14:50:30 -0700129 unsigned short ier;
130
131 ier = UART_GET_IER(uart);
132 ier &= ~ERBFI;
133 UART_PUT_IER(uart, ier);
Roy Huangf4d640c92007-07-12 16:43:46 +0800134#endif
Sonic Zhanga359cca2007-10-10 16:47:58 +0800135#ifdef CONFIG_KGDB_UART
136 }
137#endif
Bryan Wu194de562007-05-06 14:50:30 -0700138}
139
140/*
141 * Set the modem control timer to fire immediately.
142 */
143static void bfin_serial_enable_ms(struct uart_port *port)
144{
145}
146
Sonic Zhang474f1a62007-06-29 16:35:17 +0800147#ifdef CONFIG_KGDB_UART
148static int kgdb_entry_state;
149
150void kgdb_put_debug_char(int chr)
151{
152 struct bfin_serial_port *uart;
153
Graf Yang2ade9722008-04-25 02:55:49 +0800154 if (CONFIG_KGDB_UART_PORT < 0
155 || CONFIG_KGDB_UART_PORT >= BFIN_UART_NR_PORTS)
Sonic Zhang474f1a62007-06-29 16:35:17 +0800156 uart = &bfin_serial_ports[0];
157 else
158 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
159
160 while (!(UART_GET_LSR(uart) & THRE)) {
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800161 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800162 }
Sonic Zhanga359cca2007-10-10 16:47:58 +0800163
164#ifndef CONFIG_BF54x
Sonic Zhang474f1a62007-06-29 16:35:17 +0800165 UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800166 SSYNC();
Sonic Zhanga359cca2007-10-10 16:47:58 +0800167#endif
Sonic Zhang474f1a62007-06-29 16:35:17 +0800168 UART_PUT_CHAR(uart, (unsigned char)chr);
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800169 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800170}
171
172int kgdb_get_debug_char(void)
173{
174 struct bfin_serial_port *uart;
175 unsigned char chr;
176
Graf Yang2ade9722008-04-25 02:55:49 +0800177 if (CONFIG_KGDB_UART_PORT < 0
178 || CONFIG_KGDB_UART_PORT >= BFIN_UART_NR_PORTS)
Sonic Zhang474f1a62007-06-29 16:35:17 +0800179 uart = &bfin_serial_ports[0];
180 else
181 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
182
183 while(!(UART_GET_LSR(uart) & DR)) {
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800184 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800185 }
Sonic Zhanga359cca2007-10-10 16:47:58 +0800186#ifndef CONFIG_BF54x
Sonic Zhang474f1a62007-06-29 16:35:17 +0800187 UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800188 SSYNC();
Sonic Zhanga359cca2007-10-10 16:47:58 +0800189#endif
Sonic Zhang474f1a62007-06-29 16:35:17 +0800190 chr = UART_GET_CHAR(uart);
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800191 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800192
193 return chr;
194}
195#endif
196
Mike Frysinger8851c712007-12-24 19:48:04 +0800197#if ANOMALY_05000230 && defined(CONFIG_SERIAL_BFIN_PIO)
198# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
199# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
200#else
201# define UART_GET_ANOMALY_THRESHOLD(uart) 0
202# define UART_SET_ANOMALY_THRESHOLD(uart, v)
203#endif
204
Bryan Wu194de562007-05-06 14:50:30 -0700205#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700206static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
207{
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800208 struct tty_struct *tty = uart->port.info->tty;
Bryan Wu194de562007-05-06 14:50:30 -0700209 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800210 static struct timeval anomaly_start = { .tv_sec = 0 };
Sonic Zhang474f1a62007-06-29 16:35:17 +0800211#ifdef CONFIG_KGDB_UART
212 struct pt_regs *regs = get_irq_regs();
213#endif
Bryan Wu194de562007-05-06 14:50:30 -0700214
Sonic Zhang759eb042007-11-21 17:00:32 +0800215 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800216 UART_CLEAR_LSR(uart);
217
218 ch = UART_GET_CHAR(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700219 uart->port.icount.rx++;
220
Sonic Zhang474f1a62007-06-29 16:35:17 +0800221#ifdef CONFIG_KGDB_UART
222 if (uart->port.line == CONFIG_KGDB_UART_PORT) {
223 if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
224 kgdb_breakkey_pressed(regs);
225 return;
226 } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
227 kgdb_entry_state = 1;
228 } else if (kgdb_entry_state == 1 && ch == 'q') {
229 kgdb_entry_state = 0;
230 kgdb_breakkey_pressed(regs);
231 return;
232 } else if (ch == 0x3) {/* Ctrl + C */
233 kgdb_entry_state = 0;
234 kgdb_breakkey_pressed(regs);
235 return;
236 } else {
237 kgdb_entry_state = 0;
238 }
239 }
240#endif
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800241
242 if (ANOMALY_05000230) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800243 /* The BF533 (and BF561) family of processors have a nice anomaly
244 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800245 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800246 * character comes across. Due to the nature of the flood, it is
247 * not possible to reliably catch bytes that are sent too quickly
248 * after this break. So application code talking to the Blackfin
249 * which sends a break signal must allow at least 1.5 character
250 * times after the end of the break for things to stabilize. This
251 * timeout was picked as it must absolutely be larger than 1
252 * character time +/- some percent. So 1.5 sounds good. All other
253 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800254 * Note: While Anomaly 05000230 does not directly address this,
255 * the changes that went in for it also fixed this issue.
Mike Frysinger8851c712007-12-24 19:48:04 +0800256 * That anomaly was fixed in 0.5+ silicon. I like bunnies.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800257 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800258 if (anomaly_start.tv_sec) {
259 struct timeval curr;
260 suseconds_t usecs;
261
262 if ((~ch & (~ch + 1)) & 0xff)
263 goto known_good_char;
264
265 do_gettimeofday(&curr);
266 if (curr.tv_sec - anomaly_start.tv_sec > 1)
267 goto known_good_char;
268
269 usecs = 0;
270 if (curr.tv_sec != anomaly_start.tv_sec)
271 usecs += USEC_PER_SEC;
272 usecs += curr.tv_usec - anomaly_start.tv_usec;
273
274 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
275 goto known_good_char;
276
277 if (ch)
278 anomaly_start.tv_sec = 0;
279 else
280 anomaly_start = curr;
281
282 return;
283
284 known_good_char:
285 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800286 }
Bryan Wu194de562007-05-06 14:50:30 -0700287 }
Bryan Wu194de562007-05-06 14:50:30 -0700288
289 if (status & BI) {
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800290 if (ANOMALY_05000230)
Mike Frysinger8851c712007-12-24 19:48:04 +0800291 if (bfin_revid() < 5)
292 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700293 uart->port.icount.brk++;
294 if (uart_handle_break(&uart->port))
295 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800296 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800297 }
298 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700299 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800300 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700301 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800302 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700303 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800304
305 status &= uart->port.read_status_mask;
306
307 if (status & BI)
308 flg = TTY_BREAK;
309 else if (status & PE)
310 flg = TTY_PARITY;
311 else if (status & FE)
312 flg = TTY_FRAME;
313 else
Bryan Wu194de562007-05-06 14:50:30 -0700314 flg = TTY_NORMAL;
315
316 if (uart_handle_sysrq_char(&uart->port, ch))
317 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700318
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800319 uart_insert_char(&uart->port, status, OE, ch, flg);
320
321 ignore_char:
322 tty_flip_buffer_push(tty);
Bryan Wu194de562007-05-06 14:50:30 -0700323}
324
325static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
326{
327 struct circ_buf *xmit = &uart->port.info->xmit;
328
329 if (uart->port.x_char) {
330 UART_PUT_CHAR(uart, uart->port.x_char);
331 uart->port.icount.tx++;
332 uart->port.x_char = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700333 }
334 /*
335 * Check the modem control lines before
336 * transmitting anything.
337 */
338 bfin_serial_mctrl_check(uart);
339
340 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
341 bfin_serial_stop_tx(&uart->port);
342 return;
343 }
344
Sonic Zhang759eb042007-11-21 17:00:32 +0800345 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
346 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
347 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
348 uart->port.icount.tx++;
349 SSYNC();
350 }
Bryan Wu194de562007-05-06 14:50:30 -0700351
352 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
353 uart_write_wakeup(&uart->port);
354
355 if (uart_circ_empty(xmit))
356 bfin_serial_stop_tx(&uart->port);
357}
358
Aubrey Li5c4e4722007-05-21 18:09:38 +0800359static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
360{
361 struct bfin_serial_port *uart = dev_id;
362
Roy Huangf4d640c92007-07-12 16:43:46 +0800363 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800364 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800365 bfin_serial_rx_chars(uart);
366 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800367
Aubrey Li5c4e4722007-05-21 18:09:38 +0800368 return IRQ_HANDLED;
369}
370
371static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700372{
373 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700374
Roy Huangf4d640c92007-07-12 16:43:46 +0800375 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800376 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800377 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700378 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800379
Bryan Wu194de562007-05-06 14:50:30 -0700380 return IRQ_HANDLED;
381}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800382#endif
Bryan Wu194de562007-05-06 14:50:30 -0700383
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800384#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Bryan Wu194de562007-05-06 14:50:30 -0700385static void bfin_serial_do_work(struct work_struct *work)
386{
387 struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
388
389 bfin_serial_mctrl_check(uart);
390}
Bryan Wu194de562007-05-06 14:50:30 -0700391#endif
392
393#ifdef CONFIG_SERIAL_BFIN_DMA
394static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
395{
396 struct circ_buf *xmit = &uart->port.info->xmit;
397 unsigned short ier;
Bryan Wu194de562007-05-06 14:50:30 -0700398
Bryan Wu194de562007-05-06 14:50:30 -0700399 uart->tx_done = 0;
400
Bryan Wu194de562007-05-06 14:50:30 -0700401 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800402 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700403 uart->tx_done = 1;
404 return;
405 }
406
Sonic Zhang1b733512007-12-21 16:45:12 +0800407 if (uart->port.x_char) {
408 UART_PUT_CHAR(uart, uart->port.x_char);
409 uart->port.icount.tx++;
410 uart->port.x_char = 0;
411 }
412
413 /*
414 * Check the modem control lines before
415 * transmitting anything.
416 */
417 bfin_serial_mctrl_check(uart);
418
Bryan Wu194de562007-05-06 14:50:30 -0700419 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
420 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
421 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
422 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
423 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
424 set_dma_config(uart->tx_dma_channel,
425 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
426 INTR_ON_BUF,
427 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800428 DATA_SIZE_8,
429 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700430 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
431 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
432 set_dma_x_modify(uart->tx_dma_channel, 1);
433 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800434
Roy Huangf4d640c92007-07-12 16:43:46 +0800435#ifdef CONFIG_BF54x
436 UART_SET_IER(uart, ETBEI);
437#else
Bryan Wu194de562007-05-06 14:50:30 -0700438 ier = UART_GET_IER(uart);
439 ier |= ETBEI;
440 UART_PUT_IER(uart, ier);
Roy Huangf4d640c92007-07-12 16:43:46 +0800441#endif
Bryan Wu194de562007-05-06 14:50:30 -0700442}
443
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800444static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700445{
446 struct tty_struct *tty = uart->port.info->tty;
447 int i, flg, status;
448
449 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800450 UART_CLEAR_LSR(uart);
451
Sonic Zhang56f5de82008-02-25 15:19:09 +0800452 uart->port.icount.rx +=
453 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
454 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700455
456 if (status & BI) {
457 uart->port.icount.brk++;
458 if (uart_handle_break(&uart->port))
459 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800460 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800461 }
462 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700463 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800464 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700465 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800466 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700467 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800468
469 status &= uart->port.read_status_mask;
470
471 if (status & BI)
472 flg = TTY_BREAK;
473 else if (status & PE)
474 flg = TTY_PARITY;
475 else if (status & FE)
476 flg = TTY_FRAME;
477 else
Bryan Wu194de562007-05-06 14:50:30 -0700478 flg = TTY_NORMAL;
479
Sonic Zhang56f5de82008-02-25 15:19:09 +0800480 for (i = uart->rx_dma_buf.tail; i != uart->rx_dma_buf.head; i++) {
481 if (i >= UART_XMIT_SIZE)
482 i = 0;
483 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
484 uart_insert_char(&uart->port, status, OE,
485 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700486 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800487
488 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700489 tty_flip_buffer_push(tty);
490}
491
492void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
493{
494 int x_pos, pos;
Bryan Wu194de562007-05-06 14:50:30 -0700495
Sonic Zhang56f5de82008-02-25 15:19:09 +0800496 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
497 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
498 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
499 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
500 uart->rx_dma_nrows = 0;
501 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700502 if (x_pos == DMA_RX_XCOUNT)
503 x_pos = 0;
504
505 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800506 if (pos != uart->rx_dma_buf.tail) {
507 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700508 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800509 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700510 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800511
Bryan Wu194de562007-05-06 14:50:30 -0700512 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
513 add_timer(&(uart->rx_dma_timer));
514}
515
516static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
517{
518 struct bfin_serial_port *uart = dev_id;
519 struct circ_buf *xmit = &uart->port.info->xmit;
520 unsigned short ier;
521
522 spin_lock(&uart->port.lock);
523 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700524 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800525 clear_dma_irqstat(uart->tx_dma_channel);
Roy Huangf4d640c92007-07-12 16:43:46 +0800526#ifdef CONFIG_BF54x
527 UART_CLEAR_IER(uart, ETBEI);
528#else
Bryan Wu194de562007-05-06 14:50:30 -0700529 ier = UART_GET_IER(uart);
530 ier &= ~ETBEI;
531 UART_PUT_IER(uart, ier);
Roy Huangf4d640c92007-07-12 16:43:46 +0800532#endif
Sonic Zhang0711d852008-02-25 15:16:50 +0800533 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
534 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800535
Sonic Zhang56f5de82008-02-25 15:19:09 +0800536 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
537 uart_write_wakeup(&uart->port);
538
Sonic Zhang1b733512007-12-21 16:45:12 +0800539 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700540 }
541
542 spin_unlock(&uart->port.lock);
543 return IRQ_HANDLED;
544}
545
546static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
547{
548 struct bfin_serial_port *uart = dev_id;
549 unsigned short irqstat;
Sonic Zhang0711d852008-02-25 15:16:50 +0800550
Bryan Wu194de562007-05-06 14:50:30 -0700551 spin_lock(&uart->port.lock);
552 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
553 clear_dma_irqstat(uart->rx_dma_channel);
Bryan Wu194de562007-05-06 14:50:30 -0700554 spin_unlock(&uart->port.lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800555
556 del_timer(&(uart->rx_dma_timer));
557 uart->rx_dma_timer.expires = jiffies;
558 add_timer(&(uart->rx_dma_timer));
559
Bryan Wu194de562007-05-06 14:50:30 -0700560 return IRQ_HANDLED;
561}
562#endif
563
564/*
565 * Return TIOCSER_TEMT when transmitter is not busy.
566 */
567static unsigned int bfin_serial_tx_empty(struct uart_port *port)
568{
569 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
570 unsigned short lsr;
571
572 lsr = UART_GET_LSR(uart);
573 if (lsr & TEMT)
574 return TIOCSER_TEMT;
575 else
576 return 0;
577}
578
579static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
580{
581#ifdef CONFIG_SERIAL_BFIN_CTSRTS
582 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
583 if (uart->cts_pin < 0)
584 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
585
Sonic Zhangdb288382008-02-02 17:05:02 +0800586# ifdef BF54x
587 if (UART_GET_MSR(uart) & CTS)
588# else
Bryan Wu194de562007-05-06 14:50:30 -0700589 if (gpio_get_value(uart->cts_pin))
Sonic Zhangdb288382008-02-02 17:05:02 +0800590# endif
Bryan Wu194de562007-05-06 14:50:30 -0700591 return TIOCM_DSR | TIOCM_CAR;
592 else
593#endif
594 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
595}
596
597static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
598{
599#ifdef CONFIG_SERIAL_BFIN_CTSRTS
600 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
601 if (uart->rts_pin < 0)
602 return;
603
604 if (mctrl & TIOCM_RTS)
Sonic Zhangdb288382008-02-02 17:05:02 +0800605# ifdef BF54x
606 UART_PUT_MCR(uart, UART_GET_MCR(uart) & ~MRTS);
607# else
Bryan Wu194de562007-05-06 14:50:30 -0700608 gpio_set_value(uart->rts_pin, 0);
Sonic Zhangdb288382008-02-02 17:05:02 +0800609# endif
Bryan Wu194de562007-05-06 14:50:30 -0700610 else
Sonic Zhangdb288382008-02-02 17:05:02 +0800611# ifdef BF54x
612 UART_PUT_MCR(uart, UART_GET_MCR(uart) | MRTS);
613# else
Bryan Wu194de562007-05-06 14:50:30 -0700614 gpio_set_value(uart->rts_pin, 1);
Sonic Zhangdb288382008-02-02 17:05:02 +0800615# endif
Bryan Wu194de562007-05-06 14:50:30 -0700616#endif
617}
618
619/*
620 * Handle any change of modem status signal since we were last called.
621 */
622static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
623{
624#ifdef CONFIG_SERIAL_BFIN_CTSRTS
625 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -0700626 struct uart_info *info = uart->port.info;
627 struct tty_struct *tty = info->tty;
628
629 status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800630 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Bryan Wu194de562007-05-06 14:50:30 -0700631 if (!(status & TIOCM_CTS)) {
632 tty->hw_stopped = 1;
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800633 schedule_work(&uart->cts_workqueue);
Bryan Wu194de562007-05-06 14:50:30 -0700634 } else {
635 tty->hw_stopped = 0;
636 }
Bryan Wu194de562007-05-06 14:50:30 -0700637#endif
638}
639
640/*
641 * Interrupts are always disabled.
642 */
643static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
644{
Mike Frysingercf686762007-06-11 16:12:49 +0800645 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
646 u16 lcr = UART_GET_LCR(uart);
647 if (break_state)
648 lcr |= SB;
649 else
650 lcr &= ~SB;
651 UART_PUT_LCR(uart, lcr);
652 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700653}
654
655static int bfin_serial_startup(struct uart_port *port)
656{
657 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
658
659#ifdef CONFIG_SERIAL_BFIN_DMA
660 dma_addr_t dma_handle;
661
662 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
663 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
664 return -EBUSY;
665 }
666
667 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
668 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
669 free_dma(uart->rx_dma_channel);
670 return -EBUSY;
671 }
672
673 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
674 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
675
676 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
677 uart->rx_dma_buf.head = 0;
678 uart->rx_dma_buf.tail = 0;
679 uart->rx_dma_nrows = 0;
680
681 set_dma_config(uart->rx_dma_channel,
682 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
683 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800684 DATA_SIZE_8,
685 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700686 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
687 set_dma_x_modify(uart->rx_dma_channel, 1);
688 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
689 set_dma_y_modify(uart->rx_dma_channel, 1);
690 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
691 enable_dma(uart->rx_dma_channel);
692
693 uart->rx_dma_timer.data = (unsigned long)(uart);
694 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
695 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
696 add_timer(&(uart->rx_dma_timer));
697#else
Sonic Zhanga359cca2007-10-10 16:47:58 +0800698 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700699 "BFIN_UART_RX", uart)) {
Sonic Zhanga359cca2007-10-10 16:47:58 +0800700# ifdef CONFIG_KGDB_UART
701 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
702# endif
Bryan Wu194de562007-05-06 14:50:30 -0700703 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
704 return -EBUSY;
Sonic Zhanga359cca2007-10-10 16:47:58 +0800705# ifdef CONFIG_KGDB_UART
706 }
707# endif
Bryan Wu194de562007-05-06 14:50:30 -0700708 }
709
Sonic Zhanga359cca2007-10-10 16:47:58 +0800710
Bryan Wu194de562007-05-06 14:50:30 -0700711 if (request_irq
Aubrey Li5c4e4722007-05-21 18:09:38 +0800712 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700713 "BFIN_UART_TX", uart)) {
714 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
715 free_irq(uart->port.irq, uart);
716 return -EBUSY;
717 }
718#endif
Roy Huangf4d640c92007-07-12 16:43:46 +0800719#ifdef CONFIG_BF54x
720 UART_SET_IER(uart, ERBFI);
721#else
Bryan Wu194de562007-05-06 14:50:30 -0700722 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
Roy Huangf4d640c92007-07-12 16:43:46 +0800723#endif
Bryan Wu194de562007-05-06 14:50:30 -0700724 return 0;
725}
726
727static void bfin_serial_shutdown(struct uart_port *port)
728{
729 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
730
731#ifdef CONFIG_SERIAL_BFIN_DMA
732 disable_dma(uart->tx_dma_channel);
733 free_dma(uart->tx_dma_channel);
734 disable_dma(uart->rx_dma_channel);
735 free_dma(uart->rx_dma_channel);
736 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800737 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700738#else
Sonic Zhang474f1a62007-06-29 16:35:17 +0800739#ifdef CONFIG_KGDB_UART
740 if (uart->port.line != CONFIG_KGDB_UART_PORT)
741#endif
Bryan Wu194de562007-05-06 14:50:30 -0700742 free_irq(uart->port.irq, uart);
743 free_irq(uart->port.irq+1, uart);
744#endif
745}
746
747static void
748bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
749 struct ktermios *old)
750{
751 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
752 unsigned long flags;
753 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800754 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700755
756 switch (termios->c_cflag & CSIZE) {
757 case CS8:
758 lcr = WLS(8);
759 break;
760 case CS7:
761 lcr = WLS(7);
762 break;
763 case CS6:
764 lcr = WLS(6);
765 break;
766 case CS5:
767 lcr = WLS(5);
768 break;
769 default:
770 printk(KERN_ERR "%s: word lengh not supported\n",
771 __FUNCTION__);
772 }
773
774 if (termios->c_cflag & CSTOPB)
775 lcr |= STB;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800776 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700777 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800778 if (!(termios->c_cflag & PARODD))
779 lcr |= EPS;
780 if (termios->c_cflag & CMSPAR)
781 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700782
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800783 port->read_status_mask = OE;
784 if (termios->c_iflag & INPCK)
785 port->read_status_mask |= (FE | PE);
786 if (termios->c_iflag & (BRKINT | PARMRK))
787 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700788
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800789 /*
790 * Characters to ignore
791 */
792 port->ignore_status_mask = 0;
793 if (termios->c_iflag & IGNPAR)
794 port->ignore_status_mask |= FE | PE;
795 if (termios->c_iflag & IGNBRK) {
796 port->ignore_status_mask |= BI;
797 /*
798 * If we're ignoring parity and break indicators,
799 * ignore overruns too (for real raw support).
800 */
801 if (termios->c_iflag & IGNPAR)
802 port->ignore_status_mask |= OE;
803 }
Bryan Wu194de562007-05-06 14:50:30 -0700804
805 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
806 quot = uart_get_divisor(port, baud);
807 spin_lock_irqsave(&uart->port.lock, flags);
808
Mike Frysinger8851c712007-12-24 19:48:04 +0800809 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
810
Bryan Wu194de562007-05-06 14:50:30 -0700811 /* Disable UART */
812 ier = UART_GET_IER(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800813#ifdef CONFIG_BF54x
814 UART_CLEAR_IER(uart, 0xF);
815#else
Bryan Wu194de562007-05-06 14:50:30 -0700816 UART_PUT_IER(uart, 0);
Roy Huangf4d640c92007-07-12 16:43:46 +0800817#endif
Bryan Wu194de562007-05-06 14:50:30 -0700818
Roy Huangf4d640c92007-07-12 16:43:46 +0800819#ifndef CONFIG_BF54x
Bryan Wu194de562007-05-06 14:50:30 -0700820 /* Set DLAB in LCR to Access DLL and DLH */
821 val = UART_GET_LCR(uart);
822 val |= DLAB;
823 UART_PUT_LCR(uart, val);
824 SSYNC();
Roy Huangf4d640c92007-07-12 16:43:46 +0800825#endif
Bryan Wu194de562007-05-06 14:50:30 -0700826
827 UART_PUT_DLL(uart, quot & 0xFF);
828 SSYNC();
829 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
830 SSYNC();
831
Roy Huangf4d640c92007-07-12 16:43:46 +0800832#ifndef CONFIG_BF54x
Bryan Wu194de562007-05-06 14:50:30 -0700833 /* Clear DLAB in LCR to Access THR RBR IER */
834 val = UART_GET_LCR(uart);
835 val &= ~DLAB;
836 UART_PUT_LCR(uart, val);
837 SSYNC();
Roy Huangf4d640c92007-07-12 16:43:46 +0800838#endif
Bryan Wu194de562007-05-06 14:50:30 -0700839
840 UART_PUT_LCR(uart, lcr);
841
842 /* Enable UART */
Roy Huangf4d640c92007-07-12 16:43:46 +0800843#ifdef CONFIG_BF54x
844 UART_SET_IER(uart, ier);
845#else
Bryan Wu194de562007-05-06 14:50:30 -0700846 UART_PUT_IER(uart, ier);
Roy Huangf4d640c92007-07-12 16:43:46 +0800847#endif
Bryan Wu194de562007-05-06 14:50:30 -0700848
849 val = UART_GET_GCTL(uart);
850 val |= UCEN;
851 UART_PUT_GCTL(uart, val);
852
853 spin_unlock_irqrestore(&uart->port.lock, flags);
854}
855
856static const char *bfin_serial_type(struct uart_port *port)
857{
858 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
859
860 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
861}
862
863/*
864 * Release the memory region(s) being used by 'port'.
865 */
866static void bfin_serial_release_port(struct uart_port *port)
867{
868}
869
870/*
871 * Request the memory region(s) being used by 'port'.
872 */
873static int bfin_serial_request_port(struct uart_port *port)
874{
875 return 0;
876}
877
878/*
879 * Configure/autoconfigure the port.
880 */
881static void bfin_serial_config_port(struct uart_port *port, int flags)
882{
883 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
884
885 if (flags & UART_CONFIG_TYPE &&
886 bfin_serial_request_port(&uart->port) == 0)
887 uart->port.type = PORT_BFIN;
888}
889
890/*
891 * Verify the new serial_struct (for TIOCSSERIAL).
892 * The only change we allow are to the flags and type, and
893 * even then only between PORT_BFIN and PORT_UNKNOWN
894 */
895static int
896bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
897{
898 return 0;
899}
900
Graf Yang7d01b472008-02-29 11:31:08 +0800901/*
902 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
903 * In other cases, disable IrDA function.
904 */
905static void bfin_set_ldisc(struct tty_struct *tty)
906{
907 int line = tty->index;
908 unsigned short val;
909
910 if (line >= tty->driver->num)
911 return;
912
913 switch (tty->ldisc.num) {
914 case N_IRDA:
915 val = UART_GET_GCTL(&bfin_serial_ports[line]);
916 val |= (IREN | RPOLC);
917 UART_PUT_GCTL(&bfin_serial_ports[line], val);
918 break;
919 default:
920 val = UART_GET_GCTL(&bfin_serial_ports[line]);
921 val &= ~(IREN | RPOLC);
922 UART_PUT_GCTL(&bfin_serial_ports[line], val);
923 }
924}
925
Bryan Wu194de562007-05-06 14:50:30 -0700926static struct uart_ops bfin_serial_pops = {
927 .tx_empty = bfin_serial_tx_empty,
928 .set_mctrl = bfin_serial_set_mctrl,
929 .get_mctrl = bfin_serial_get_mctrl,
930 .stop_tx = bfin_serial_stop_tx,
931 .start_tx = bfin_serial_start_tx,
932 .stop_rx = bfin_serial_stop_rx,
933 .enable_ms = bfin_serial_enable_ms,
934 .break_ctl = bfin_serial_break_ctl,
935 .startup = bfin_serial_startup,
936 .shutdown = bfin_serial_shutdown,
937 .set_termios = bfin_serial_set_termios,
938 .type = bfin_serial_type,
939 .release_port = bfin_serial_release_port,
940 .request_port = bfin_serial_request_port,
941 .config_port = bfin_serial_config_port,
942 .verify_port = bfin_serial_verify_port,
943};
944
945static void __init bfin_serial_init_ports(void)
946{
947 static int first = 1;
948 int i;
949
950 if (!first)
951 return;
952 first = 0;
953
954 for (i = 0; i < nr_ports; i++) {
955 bfin_serial_ports[i].port.uartclk = get_sclk();
956 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
957 bfin_serial_ports[i].port.line = i;
958 bfin_serial_ports[i].port.iotype = UPIO_MEM;
959 bfin_serial_ports[i].port.membase =
960 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
961 bfin_serial_ports[i].port.mapbase =
962 bfin_serial_resource[i].uart_base_addr;
963 bfin_serial_ports[i].port.irq =
964 bfin_serial_resource[i].uart_irq;
965 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
966#ifdef CONFIG_SERIAL_BFIN_DMA
967 bfin_serial_ports[i].tx_done = 1;
968 bfin_serial_ports[i].tx_count = 0;
969 bfin_serial_ports[i].tx_dma_channel =
970 bfin_serial_resource[i].uart_tx_dma_channel;
971 bfin_serial_ports[i].rx_dma_channel =
972 bfin_serial_resource[i].uart_rx_dma_channel;
973 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700974#endif
975#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800976 INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
Bryan Wu194de562007-05-06 14:50:30 -0700977 bfin_serial_ports[i].cts_pin =
978 bfin_serial_resource[i].uart_cts_pin;
979 bfin_serial_ports[i].rts_pin =
980 bfin_serial_resource[i].uart_rts_pin;
981#endif
982 bfin_serial_hw_init(&bfin_serial_ports[i]);
Bryan Wu194de562007-05-06 14:50:30 -0700983 }
Roy Huangf4d640c92007-07-12 16:43:46 +0800984
Bryan Wu194de562007-05-06 14:50:30 -0700985}
986
987#ifdef CONFIG_SERIAL_BFIN_CONSOLE
Bryan Wu194de562007-05-06 14:50:30 -0700988/*
989 * If the port was already initialised (eg, by a boot loader),
990 * try to determine the current setup.
991 */
992static void __init
993bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
994 int *parity, int *bits)
995{
996 unsigned short status;
997
998 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
999 if (status == (ERBFI | ETBEI)) {
1000 /* ok, the port was enabled */
1001 unsigned short lcr, val;
1002 unsigned short dlh, dll;
1003
1004 lcr = UART_GET_LCR(uart);
1005
1006 *parity = 'n';
1007 if (lcr & PEN) {
1008 if (lcr & EPS)
1009 *parity = 'e';
1010 else
1011 *parity = 'o';
1012 }
1013 switch (lcr & 0x03) {
1014 case 0: *bits = 5; break;
1015 case 1: *bits = 6; break;
1016 case 2: *bits = 7; break;
1017 case 3: *bits = 8; break;
1018 }
Roy Huangf4d640c92007-07-12 16:43:46 +08001019#ifndef CONFIG_BF54x
Bryan Wu194de562007-05-06 14:50:30 -07001020 /* Set DLAB in LCR to Access DLL and DLH */
1021 val = UART_GET_LCR(uart);
1022 val |= DLAB;
1023 UART_PUT_LCR(uart, val);
Roy Huangf4d640c92007-07-12 16:43:46 +08001024#endif
Bryan Wu194de562007-05-06 14:50:30 -07001025
1026 dll = UART_GET_DLL(uart);
1027 dlh = UART_GET_DLH(uart);
1028
Roy Huangf4d640c92007-07-12 16:43:46 +08001029#ifndef CONFIG_BF54x
Bryan Wu194de562007-05-06 14:50:30 -07001030 /* Clear DLAB in LCR to Access THR RBR IER */
1031 val = UART_GET_LCR(uart);
1032 val &= ~DLAB;
1033 UART_PUT_LCR(uart, val);
Roy Huangf4d640c92007-07-12 16:43:46 +08001034#endif
Bryan Wu194de562007-05-06 14:50:30 -07001035
1036 *baud = get_sclk() / (16*(dll | dlh << 8));
1037 }
1038 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
1039}
Robin Getz0ae53642007-10-09 17:24:49 +08001040#endif
1041
1042#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1043static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001044
1045static int __init
1046bfin_serial_console_setup(struct console *co, char *options)
1047{
1048 struct bfin_serial_port *uart;
Robin Getz0ae53642007-10-09 17:24:49 +08001049# ifdef CONFIG_SERIAL_BFIN_CONSOLE
Bryan Wu194de562007-05-06 14:50:30 -07001050 int baud = 57600;
1051 int bits = 8;
1052 int parity = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001053# ifdef CONFIG_SERIAL_BFIN_CTSRTS
Bryan Wu194de562007-05-06 14:50:30 -07001054 int flow = 'r';
Robin Getz0ae53642007-10-09 17:24:49 +08001055# else
Bryan Wu194de562007-05-06 14:50:30 -07001056 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001057# endif
1058# endif
Bryan Wu194de562007-05-06 14:50:30 -07001059
1060 /*
1061 * Check whether an invalid uart number has been specified, and
1062 * if so, search for the first available port that does have
1063 * console support.
1064 */
1065 if (co->index == -1 || co->index >= nr_ports)
1066 co->index = 0;
1067 uart = &bfin_serial_ports[co->index];
1068
Robin Getz0ae53642007-10-09 17:24:49 +08001069# ifdef CONFIG_SERIAL_BFIN_CONSOLE
Bryan Wu194de562007-05-06 14:50:30 -07001070 if (options)
1071 uart_parse_options(options, &baud, &parity, &bits, &flow);
1072 else
1073 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1074
1075 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001076# else
1077 return 0;
1078# endif
1079}
1080#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1081 defined (CONFIG_EARLY_PRINTK) */
1082
1083#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1084static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1085{
1086 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1087 while (!(UART_GET_LSR(uart) & THRE))
1088 barrier();
1089 UART_PUT_CHAR(uart, ch);
1090 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -07001091}
1092
Robin Getz0ae53642007-10-09 17:24:49 +08001093/*
1094 * Interrupts are disabled on entering
1095 */
1096static void
1097bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1098{
1099 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1100 int flags = 0;
1101
1102 spin_lock_irqsave(&uart->port.lock, flags);
1103 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1104 spin_unlock_irqrestore(&uart->port.lock, flags);
1105
1106}
1107
Bryan Wu194de562007-05-06 14:50:30 -07001108static struct console bfin_serial_console = {
1109 .name = BFIN_SERIAL_NAME,
1110 .write = bfin_serial_console_write,
1111 .device = uart_console_device,
1112 .setup = bfin_serial_console_setup,
1113 .flags = CON_PRINTBUFFER,
1114 .index = -1,
1115 .data = &bfin_serial_reg,
1116};
1117
1118static int __init bfin_serial_rs_console_init(void)
1119{
1120 bfin_serial_init_ports();
1121 register_console(&bfin_serial_console);
Sonic Zhang474f1a62007-06-29 16:35:17 +08001122#ifdef CONFIG_KGDB_UART
1123 kgdb_entry_state = 0;
1124 init_kgdb_uart();
1125#endif
Bryan Wu194de562007-05-06 14:50:30 -07001126 return 0;
1127}
1128console_initcall(bfin_serial_rs_console_init);
1129
1130#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1131#else
1132#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001133#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1134
1135
1136#ifdef CONFIG_EARLY_PRINTK
1137static __init void early_serial_putc(struct uart_port *port, int ch)
1138{
1139 unsigned timeout = 0xffff;
1140 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1141
1142 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1143 cpu_relax();
1144 UART_PUT_CHAR(uart, ch);
1145}
1146
1147static __init void early_serial_write(struct console *con, const char *s,
1148 unsigned int n)
1149{
1150 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1151 unsigned int i;
1152
1153 for (i = 0; i < n; i++, s++) {
1154 if (*s == '\n')
1155 early_serial_putc(&uart->port, '\r');
1156 early_serial_putc(&uart->port, *s);
1157 }
1158}
1159
1160static struct __init console bfin_early_serial_console = {
1161 .name = "early_BFuart",
1162 .write = early_serial_write,
1163 .device = uart_console_device,
1164 .flags = CON_PRINTBUFFER,
1165 .setup = bfin_serial_console_setup,
1166 .index = -1,
1167 .data = &bfin_serial_reg,
1168};
1169
1170struct console __init *bfin_earlyserial_init(unsigned int port,
1171 unsigned int cflag)
1172{
1173 struct bfin_serial_port *uart;
1174 struct ktermios t;
1175
1176 if (port == -1 || port >= nr_ports)
1177 port = 0;
1178 bfin_serial_init_ports();
1179 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001180 uart = &bfin_serial_ports[port];
1181 t.c_cflag = cflag;
1182 t.c_iflag = 0;
1183 t.c_oflag = 0;
1184 t.c_lflag = ICANON;
1185 t.c_line = port;
1186 bfin_serial_set_termios(&uart->port, &t, &t);
1187 return &bfin_early_serial_console;
1188}
1189
1190#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
Bryan Wu194de562007-05-06 14:50:30 -07001191
1192static struct uart_driver bfin_serial_reg = {
1193 .owner = THIS_MODULE,
1194 .driver_name = "bfin-uart",
1195 .dev_name = BFIN_SERIAL_NAME,
1196 .major = BFIN_SERIAL_MAJOR,
1197 .minor = BFIN_SERIAL_MINOR,
Graf Yang2ade9722008-04-25 02:55:49 +08001198 .nr = BFIN_UART_NR_PORTS,
Bryan Wu194de562007-05-06 14:50:30 -07001199 .cons = BFIN_SERIAL_CONSOLE,
1200};
1201
1202static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1203{
1204 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1205
1206 if (uart)
1207 uart_suspend_port(&bfin_serial_reg, &uart->port);
1208
1209 return 0;
1210}
1211
1212static int bfin_serial_resume(struct platform_device *dev)
1213{
1214 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1215
1216 if (uart)
1217 uart_resume_port(&bfin_serial_reg, &uart->port);
1218
1219 return 0;
1220}
1221
1222static int bfin_serial_probe(struct platform_device *dev)
1223{
1224 struct resource *res = dev->resource;
1225 int i;
1226
1227 for (i = 0; i < dev->num_resources; i++, res++)
1228 if (res->flags & IORESOURCE_MEM)
1229 break;
1230
1231 if (i < dev->num_resources) {
1232 for (i = 0; i < nr_ports; i++, res++) {
1233 if (bfin_serial_ports[i].port.mapbase != res->start)
1234 continue;
1235 bfin_serial_ports[i].port.dev = &dev->dev;
1236 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1237 platform_set_drvdata(dev, &bfin_serial_ports[i]);
1238 }
1239 }
1240
1241 return 0;
1242}
1243
1244static int bfin_serial_remove(struct platform_device *pdev)
1245{
1246 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1247
1248
1249#ifdef CONFIG_SERIAL_BFIN_CTSRTS
1250 gpio_free(uart->cts_pin);
1251 gpio_free(uart->rts_pin);
1252#endif
1253
1254 platform_set_drvdata(pdev, NULL);
1255
1256 if (uart)
1257 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1258
1259 return 0;
1260}
1261
1262static struct platform_driver bfin_serial_driver = {
1263 .probe = bfin_serial_probe,
1264 .remove = bfin_serial_remove,
1265 .suspend = bfin_serial_suspend,
1266 .resume = bfin_serial_resume,
1267 .driver = {
1268 .name = "bfin-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001269 .owner = THIS_MODULE,
Bryan Wu194de562007-05-06 14:50:30 -07001270 },
1271};
1272
1273static int __init bfin_serial_init(void)
1274{
1275 int ret;
Sonic Zhang474f1a62007-06-29 16:35:17 +08001276#ifdef CONFIG_KGDB_UART
1277 struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
Sonic Zhanga359cca2007-10-10 16:47:58 +08001278 struct ktermios t;
Sonic Zhang474f1a62007-06-29 16:35:17 +08001279#endif
Bryan Wu194de562007-05-06 14:50:30 -07001280
1281 pr_info("Serial: Blackfin serial driver\n");
1282
1283 bfin_serial_init_ports();
1284
1285 ret = uart_register_driver(&bfin_serial_reg);
1286 if (ret == 0) {
Graf Yang7d01b472008-02-29 11:31:08 +08001287 bfin_serial_reg.tty_driver->set_ldisc = bfin_set_ldisc;
Bryan Wu194de562007-05-06 14:50:30 -07001288 ret = platform_driver_register(&bfin_serial_driver);
1289 if (ret) {
1290 pr_debug("uart register failed\n");
1291 uart_unregister_driver(&bfin_serial_reg);
1292 }
1293 }
Sonic Zhang474f1a62007-06-29 16:35:17 +08001294#ifdef CONFIG_KGDB_UART
1295 if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
Sonic Zhanga359cca2007-10-10 16:47:58 +08001296 request_irq(uart->port.irq, bfin_serial_rx_int,
Sonic Zhang474f1a62007-06-29 16:35:17 +08001297 IRQF_DISABLED, "BFIN_UART_RX", uart);
1298 pr_info("Request irq for kgdb uart port\n");
Sonic Zhanga359cca2007-10-10 16:47:58 +08001299#ifdef CONFIG_BF54x
1300 UART_SET_IER(uart, ERBFI);
1301#else
Sonic Zhang474f1a62007-06-29 16:35:17 +08001302 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
Sonic Zhanga359cca2007-10-10 16:47:58 +08001303#endif
Mike Frysingerd5148ff2007-07-25 11:57:42 +08001304 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +08001305 t.c_cflag = CS8|B57600;
1306 t.c_iflag = 0;
1307 t.c_oflag = 0;
1308 t.c_lflag = ICANON;
1309 t.c_line = CONFIG_KGDB_UART_PORT;
1310 bfin_serial_set_termios(&uart->port, &t, &t);
1311 }
1312#endif
Bryan Wu194de562007-05-06 14:50:30 -07001313 return ret;
1314}
1315
1316static void __exit bfin_serial_exit(void)
1317{
1318 platform_driver_unregister(&bfin_serial_driver);
1319 uart_unregister_driver(&bfin_serial_reg);
1320}
1321
1322module_init(bfin_serial_init);
1323module_exit(bfin_serial_exit);
1324
1325MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1326MODULE_DESCRIPTION("Blackfin generic serial port driver");
1327MODULE_LICENSE("GPL");
1328MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001329MODULE_ALIAS("platform:bfin-uart");