aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/serial
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/serial
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/serial')
-rw-r--r--drivers/serial/21285.c541
-rw-r--r--drivers/serial/68328serial.c1614
-rw-r--r--drivers/serial/68328serial.h194
-rw-r--r--drivers/serial/68360serial.c3027
-rw-r--r--drivers/serial/8250.c2632
-rw-r--r--drivers/serial/8250.h87
-rw-r--r--drivers/serial/8250_acorn.c142
-rw-r--r--drivers/serial/8250_acpi.c171
-rw-r--r--drivers/serial/8250_early.c255
-rw-r--r--drivers/serial/8250_gsc.c120
-rw-r--r--drivers/serial/8250_hp300.c329
-rw-r--r--drivers/serial/8250_pci.c2303
-rw-r--r--drivers/serial/8250_pnp.c457
-rw-r--r--drivers/serial/Kconfig846
-rw-r--r--drivers/serial/Makefile54
-rw-r--r--drivers/serial/amba-pl010.c840
-rw-r--r--drivers/serial/amba-pl011.c869
-rw-r--r--drivers/serial/au1x00_uart.c1312
-rw-r--r--drivers/serial/bast_sio.c80
-rw-r--r--drivers/serial/clps711x.c609
-rw-r--r--drivers/serial/cpm_uart/Makefile11
-rw-r--r--drivers/serial/cpm_uart/cpm_uart.h89
-rw-r--r--drivers/serial/cpm_uart/cpm_uart_core.c1177
-rw-r--r--drivers/serial/cpm_uart/cpm_uart_cpm1.c290
-rw-r--r--drivers/serial/cpm_uart/cpm_uart_cpm1.h45
-rw-r--r--drivers/serial/cpm_uart/cpm_uart_cpm2.c328
-rw-r--r--drivers/serial/cpm_uart/cpm_uart_cpm2.h45
-rw-r--r--drivers/serial/crisv10.c5059
-rw-r--r--drivers/serial/crisv10.h137
-rw-r--r--drivers/serial/dz.c822
-rw-r--r--drivers/serial/dz.h118
-rw-r--r--drivers/serial/icom.c1691
-rw-r--r--drivers/serial/icom.h290
-rw-r--r--drivers/serial/imx.c909
-rw-r--r--drivers/serial/ioc4_serial.c2909
-rw-r--r--drivers/serial/ip22zilog.c1271
-rw-r--r--drivers/serial/ip22zilog.h281
-rw-r--r--drivers/serial/jsm/Makefile8
-rw-r--r--drivers/serial/jsm/jsm.h437
-rw-r--r--drivers/serial/jsm/jsm_driver.c404
-rw-r--r--drivers/serial/jsm/jsm_neo.c1427
-rw-r--r--drivers/serial/jsm/jsm_tty.c1038
-rw-r--r--drivers/serial/m32r_sio.c1218
-rw-r--r--drivers/serial/m32r_sio.h55
-rw-r--r--drivers/serial/m32r_sio_reg.h153
-rw-r--r--drivers/serial/mcfserial.c1883
-rw-r--r--drivers/serial/mcfserial.h75
-rw-r--r--drivers/serial/mpc52xx_uart.c852
-rw-r--r--drivers/serial/mpsc.c1832
-rw-r--r--drivers/serial/mpsc.h289
-rw-r--r--drivers/serial/mux.c539
-rw-r--r--drivers/serial/pmac_zilog.c2048
-rw-r--r--drivers/serial/pmac_zilog.h382
-rw-r--r--drivers/serial/pxa.c877
-rw-r--r--drivers/serial/s3c2410.c1831
-rw-r--r--drivers/serial/sa1100.c952
-rw-r--r--drivers/serial/serial_core.c2395
-rw-r--r--drivers/serial/serial_cs.c747
-rw-r--r--drivers/serial/serial_lh7a40x.c711
-rw-r--r--drivers/serial/serial_txx9.c1171
-rw-r--r--drivers/serial/sh-sci.c1692
-rw-r--r--drivers/serial/sh-sci.h573
-rw-r--r--drivers/serial/sn_console.c1124
-rw-r--r--drivers/serial/suncore.c218
-rw-r--r--drivers/serial/suncore.h29
-rw-r--r--drivers/serial/sunsab.c1171
-rw-r--r--drivers/serial/sunsab.h321
-rw-r--r--drivers/serial/sunsu.c1742
-rw-r--r--drivers/serial/sunzilog.c1773
-rw-r--r--drivers/serial/sunzilog.h272
-rw-r--r--drivers/serial/uart00.c782
-rw-r--r--drivers/serial/v850e_uart.c549
-rw-r--r--drivers/serial/vr41xx_siu.c1100
73 files changed, 64624 insertions, 0 deletions
diff --git a/drivers/serial/21285.c b/drivers/serial/21285.c
new file mode 100644
index 000000000000..f8504b0adebc
--- /dev/null
+++ b/drivers/serial/21285.c
@@ -0,0 +1,541 @@
1/*
2 * linux/drivers/char/21285.c
3 *
4 * Driver for the serial port on the 21285 StrongArm-110 core logic chip.
5 *
6 * Based on drivers/char/serial.c
7 *
8 * $Id: 21285.c,v 1.37 2002/07/28 10:03:27 rmk Exp $
9 */
10#include <linux/config.h>
11#include <linux/module.h>
12#include <linux/tty.h>
13#include <linux/ioport.h>
14#include <linux/init.h>
15#include <linux/console.h>
16#include <linux/device.h>
17#include <linux/tty_flip.h>
18#include <linux/serial_core.h>
19#include <linux/serial.h>
20
21#include <asm/io.h>
22#include <asm/irq.h>
23#include <asm/mach-types.h>
24#include <asm/hardware/dec21285.h>
25#include <asm/hardware.h>
26
27#define BAUD_BASE (mem_fclk_21285/64)
28
29#define SERIAL_21285_NAME "ttyFB"
30#define SERIAL_21285_MAJOR 204
31#define SERIAL_21285_MINOR 4
32
33#define RXSTAT_DUMMY_READ 0x80000000
34#define RXSTAT_FRAME (1 << 0)
35#define RXSTAT_PARITY (1 << 1)
36#define RXSTAT_OVERRUN (1 << 2)
37#define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
38
39#define H_UBRLCR_BREAK (1 << 0)
40#define H_UBRLCR_PARENB (1 << 1)
41#define H_UBRLCR_PAREVN (1 << 2)
42#define H_UBRLCR_STOPB (1 << 3)
43#define H_UBRLCR_FIFO (1 << 4)
44
45static const char serial21285_name[] = "Footbridge UART";
46
47#define tx_enabled(port) ((port)->unused[0])
48#define rx_enabled(port) ((port)->unused[1])
49
50/*
51 * The documented expression for selecting the divisor is:
52 * BAUD_BASE / baud - 1
53 * However, typically BAUD_BASE is not divisible by baud, so
54 * we want to select the divisor that gives us the minimum
55 * error. Therefore, we want:
56 * int(BAUD_BASE / baud - 0.5) ->
57 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
58 * int((BAUD_BASE - (baud >> 1)) / baud)
59 */
60
61static void
62serial21285_stop_tx(struct uart_port *port, unsigned int tty_stop)
63{
64 if (tx_enabled(port)) {
65 disable_irq(IRQ_CONTX);
66 tx_enabled(port) = 0;
67 }
68}
69
70static void
71serial21285_start_tx(struct uart_port *port, unsigned int tty_start)
72{
73 if (!tx_enabled(port)) {
74 enable_irq(IRQ_CONTX);
75 tx_enabled(port) = 1;
76 }
77}
78
79static void serial21285_stop_rx(struct uart_port *port)
80{
81 if (rx_enabled(port)) {
82 disable_irq(IRQ_CONRX);
83 rx_enabled(port) = 0;
84 }
85}
86
87static void serial21285_enable_ms(struct uart_port *port)
88{
89}
90
91static irqreturn_t serial21285_rx_chars(int irq, void *dev_id, struct pt_regs *regs)
92{
93 struct uart_port *port = dev_id;
94 struct tty_struct *tty = port->info->tty;
95 unsigned int status, ch, flag, rxs, max_count = 256;
96
97 status = *CSR_UARTFLG;
98 while (!(status & 0x10) && max_count--) {
99 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
100 if (tty->low_latency)
101 tty_flip_buffer_push(tty);
102 /*
103 * If this failed then we will throw away the
104 * bytes but must do so to clear interrupts
105 */
106 }
107
108 ch = *CSR_UARTDR;
109 flag = TTY_NORMAL;
110 port->icount.rx++;
111
112 rxs = *CSR_RXSTAT | RXSTAT_DUMMY_READ;
113 if (rxs & RXSTAT_ANYERR) {
114 if (rxs & RXSTAT_PARITY)
115 port->icount.parity++;
116 else if (rxs & RXSTAT_FRAME)
117 port->icount.frame++;
118 if (rxs & RXSTAT_OVERRUN)
119 port->icount.overrun++;
120
121 rxs &= port->read_status_mask;
122
123 if (rxs & RXSTAT_PARITY)
124 flag = TTY_PARITY;
125 else if (rxs & RXSTAT_FRAME)
126 flag = TTY_FRAME;
127 }
128
129 if ((rxs & port->ignore_status_mask) == 0) {
130 tty_insert_flip_char(tty, ch, flag);
131 }
132 if ((rxs & RXSTAT_OVERRUN) &&
133 tty->flip.count < TTY_FLIPBUF_SIZE) {
134 /*
135 * Overrun is special, since it's reported
136 * immediately, and doesn't affect the current
137 * character.
138 */
139 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
140 }
141 status = *CSR_UARTFLG;
142 }
143 tty_flip_buffer_push(tty);
144
145 return IRQ_HANDLED;
146}
147
148static irqreturn_t serial21285_tx_chars(int irq, void *dev_id, struct pt_regs *regs)
149{
150 struct uart_port *port = dev_id;
151 struct circ_buf *xmit = &port->info->xmit;
152 int count = 256;
153
154 if (port->x_char) {
155 *CSR_UARTDR = port->x_char;
156 port->icount.tx++;
157 port->x_char = 0;
158 goto out;
159 }
160 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
161 serial21285_stop_tx(port, 0);
162 goto out;
163 }
164
165 do {
166 *CSR_UARTDR = xmit->buf[xmit->tail];
167 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
168 port->icount.tx++;
169 if (uart_circ_empty(xmit))
170 break;
171 } while (--count > 0 && !(*CSR_UARTFLG & 0x20));
172
173 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
174 uart_write_wakeup(port);
175
176 if (uart_circ_empty(xmit))
177 serial21285_stop_tx(port, 0);
178
179 out:
180 return IRQ_HANDLED;
181}
182
183static unsigned int serial21285_tx_empty(struct uart_port *port)
184{
185 return (*CSR_UARTFLG & 8) ? 0 : TIOCSER_TEMT;
186}
187
188/* no modem control lines */
189static unsigned int serial21285_get_mctrl(struct uart_port *port)
190{
191 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
192}
193
194static void serial21285_set_mctrl(struct uart_port *port, unsigned int mctrl)
195{
196}
197
198static void serial21285_break_ctl(struct uart_port *port, int break_state)
199{
200 unsigned long flags;
201 unsigned int h_lcr;
202
203 spin_lock_irqsave(&port->lock, flags);
204 h_lcr = *CSR_H_UBRLCR;
205 if (break_state)
206 h_lcr |= H_UBRLCR_BREAK;
207 else
208 h_lcr &= ~H_UBRLCR_BREAK;
209 *CSR_H_UBRLCR = h_lcr;
210 spin_unlock_irqrestore(&port->lock, flags);
211}
212
213static int serial21285_startup(struct uart_port *port)
214{
215 int ret;
216
217 tx_enabled(port) = 1;
218 rx_enabled(port) = 1;
219
220 ret = request_irq(IRQ_CONRX, serial21285_rx_chars, 0,
221 serial21285_name, port);
222 if (ret == 0) {
223 ret = request_irq(IRQ_CONTX, serial21285_tx_chars, 0,
224 serial21285_name, port);
225 if (ret)
226 free_irq(IRQ_CONRX, port);
227 }
228
229 return ret;
230}
231
232static void serial21285_shutdown(struct uart_port *port)
233{
234 free_irq(IRQ_CONTX, port);
235 free_irq(IRQ_CONRX, port);
236}
237
238static void
239serial21285_set_termios(struct uart_port *port, struct termios *termios,
240 struct termios *old)
241{
242 unsigned long flags;
243 unsigned int baud, quot, h_lcr;
244
245 /*
246 * We don't support modem control lines.
247 */
248 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
249 termios->c_cflag |= CLOCAL;
250
251 /*
252 * We don't support BREAK character recognition.
253 */
254 termios->c_iflag &= ~(IGNBRK | BRKINT);
255
256 /*
257 * Ask the core to calculate the divisor for us.
258 */
259 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
260 quot = uart_get_divisor(port, baud);
261
262 switch (termios->c_cflag & CSIZE) {
263 case CS5:
264 h_lcr = 0x00;
265 break;
266 case CS6:
267 h_lcr = 0x20;
268 break;
269 case CS7:
270 h_lcr = 0x40;
271 break;
272 default: /* CS8 */
273 h_lcr = 0x60;
274 break;
275 }
276
277 if (termios->c_cflag & CSTOPB)
278 h_lcr |= H_UBRLCR_STOPB;
279 if (termios->c_cflag & PARENB) {
280 h_lcr |= H_UBRLCR_PARENB;
281 if (!(termios->c_cflag & PARODD))
282 h_lcr |= H_UBRLCR_PAREVN;
283 }
284
285 if (port->fifosize)
286 h_lcr |= H_UBRLCR_FIFO;
287
288 spin_lock_irqsave(&port->lock, flags);
289
290 /*
291 * Update the per-port timeout.
292 */
293 uart_update_timeout(port, termios->c_cflag, baud);
294
295 /*
296 * Which character status flags are we interested in?
297 */
298 port->read_status_mask = RXSTAT_OVERRUN;
299 if (termios->c_iflag & INPCK)
300 port->read_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
301
302 /*
303 * Which character status flags should we ignore?
304 */
305 port->ignore_status_mask = 0;
306 if (termios->c_iflag & IGNPAR)
307 port->ignore_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
308 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
309 port->ignore_status_mask |= RXSTAT_OVERRUN;
310
311 /*
312 * Ignore all characters if CREAD is not set.
313 */
314 if ((termios->c_cflag & CREAD) == 0)
315 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
316
317 quot -= 1;
318
319 *CSR_UARTCON = 0;
320 *CSR_L_UBRLCR = quot & 0xff;
321 *CSR_M_UBRLCR = (quot >> 8) & 0x0f;
322 *CSR_H_UBRLCR = h_lcr;
323 *CSR_UARTCON = 1;
324
325 spin_unlock_irqrestore(&port->lock, flags);
326}
327
328static const char *serial21285_type(struct uart_port *port)
329{
330 return port->type == PORT_21285 ? "DC21285" : NULL;
331}
332
333static void serial21285_release_port(struct uart_port *port)
334{
335 release_mem_region(port->mapbase, 32);
336}
337
338static int serial21285_request_port(struct uart_port *port)
339{
340 return request_mem_region(port->mapbase, 32, serial21285_name)
341 != NULL ? 0 : -EBUSY;
342}
343
344static void serial21285_config_port(struct uart_port *port, int flags)
345{
346 if (flags & UART_CONFIG_TYPE && serial21285_request_port(port) == 0)
347 port->type = PORT_21285;
348}
349
350/*
351 * verify the new serial_struct (for TIOCSSERIAL).
352 */
353static int serial21285_verify_port(struct uart_port *port, struct serial_struct *ser)
354{
355 int ret = 0;
356 if (ser->type != PORT_UNKNOWN && ser->type != PORT_21285)
357 ret = -EINVAL;
358 if (ser->irq != NO_IRQ)
359 ret = -EINVAL;
360 if (ser->baud_base != port->uartclk / 16)
361 ret = -EINVAL;
362 return ret;
363}
364
365static struct uart_ops serial21285_ops = {
366 .tx_empty = serial21285_tx_empty,
367 .get_mctrl = serial21285_get_mctrl,
368 .set_mctrl = serial21285_set_mctrl,
369 .stop_tx = serial21285_stop_tx,
370 .start_tx = serial21285_start_tx,
371 .stop_rx = serial21285_stop_rx,
372 .enable_ms = serial21285_enable_ms,
373 .break_ctl = serial21285_break_ctl,
374 .startup = serial21285_startup,
375 .shutdown = serial21285_shutdown,
376 .set_termios = serial21285_set_termios,
377 .type = serial21285_type,
378 .release_port = serial21285_release_port,
379 .request_port = serial21285_request_port,
380 .config_port = serial21285_config_port,
381 .verify_port = serial21285_verify_port,
382};
383
384static struct uart_port serial21285_port = {
385 .mapbase = 0x42000160,
386 .iotype = SERIAL_IO_MEM,
387 .irq = NO_IRQ,
388 .fifosize = 16,
389 .ops = &serial21285_ops,
390 .flags = ASYNC_BOOT_AUTOCONF,
391};
392
393static void serial21285_setup_ports(void)
394{
395 serial21285_port.uartclk = mem_fclk_21285 / 4;
396}
397
398#ifdef CONFIG_SERIAL_21285_CONSOLE
399
400static void
401serial21285_console_write(struct console *co, const char *s,
402 unsigned int count)
403{
404 int i;
405
406 for (i = 0; i < count; i++) {
407 while (*CSR_UARTFLG & 0x20)
408 barrier();
409 *CSR_UARTDR = s[i];
410 if (s[i] == '\n') {
411 while (*CSR_UARTFLG & 0x20)
412 barrier();
413 *CSR_UARTDR = '\r';
414 }
415 }
416}
417
418static void __init
419serial21285_get_options(struct uart_port *port, int *baud,
420 int *parity, int *bits)
421{
422 if (*CSR_UARTCON == 1) {
423 unsigned int tmp;
424
425 tmp = *CSR_H_UBRLCR;
426 switch (tmp & 0x60) {
427 case 0x00:
428 *bits = 5;
429 break;
430 case 0x20:
431 *bits = 6;
432 break;
433 case 0x40:
434 *bits = 7;
435 break;
436 default:
437 case 0x60:
438 *bits = 8;
439 break;
440 }
441
442 if (tmp & H_UBRLCR_PARENB) {
443 *parity = 'o';
444 if (tmp & H_UBRLCR_PAREVN)
445 *parity = 'e';
446 }
447
448 tmp = *CSR_L_UBRLCR | (*CSR_M_UBRLCR << 8);
449
450 *baud = port->uartclk / (16 * (tmp + 1));
451 }
452}
453
454static int __init serial21285_console_setup(struct console *co, char *options)
455{
456 struct uart_port *port = &serial21285_port;
457 int baud = 9600;
458 int bits = 8;
459 int parity = 'n';
460 int flow = 'n';
461
462 if (machine_is_personal_server())
463 baud = 57600;
464
465 /*
466 * Check whether an invalid uart number has been specified, and
467 * if so, search for the first available port that does have
468 * console support.
469 */
470 if (options)
471 uart_parse_options(options, &baud, &parity, &bits, &flow);
472 else
473 serial21285_get_options(port, &baud, &parity, &bits);
474
475 return uart_set_options(port, co, baud, parity, bits, flow);
476}
477
478extern struct uart_driver serial21285_reg;
479
480static struct console serial21285_console =
481{
482 .name = SERIAL_21285_NAME,
483 .write = serial21285_console_write,
484 .device = uart_console_device,
485 .setup = serial21285_console_setup,
486 .flags = CON_PRINTBUFFER,
487 .index = -1,
488 .data = &serial21285_reg,
489};
490
491static int __init rs285_console_init(void)
492{
493 serial21285_setup_ports();
494 register_console(&serial21285_console);
495 return 0;
496}
497console_initcall(rs285_console_init);
498
499#define SERIAL_21285_CONSOLE &serial21285_console
500#else
501#define SERIAL_21285_CONSOLE NULL
502#endif
503
504static struct uart_driver serial21285_reg = {
505 .owner = THIS_MODULE,
506 .driver_name = "ttyFB",
507 .dev_name = "ttyFB",
508 .devfs_name = "ttyFB",
509 .major = SERIAL_21285_MAJOR,
510 .minor = SERIAL_21285_MINOR,
511 .nr = 1,
512 .cons = SERIAL_21285_CONSOLE,
513};
514
515static int __init serial21285_init(void)
516{
517 int ret;
518
519 printk(KERN_INFO "Serial: 21285 driver $Revision: 1.37 $\n");
520
521 serial21285_setup_ports();
522
523 ret = uart_register_driver(&serial21285_reg);
524 if (ret == 0)
525 uart_add_one_port(&serial21285_reg, &serial21285_port);
526
527 return ret;
528}
529
530static void __exit serial21285_exit(void)
531{
532 uart_remove_one_port(&serial21285_reg, &serial21285_port);
533 uart_unregister_driver(&serial21285_reg);
534}
535
536module_init(serial21285_init);
537module_exit(serial21285_exit);
538
539MODULE_LICENSE("GPL");
540MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver $Revision: 1.37 $");
541MODULE_ALIAS_CHARDEV(SERIAL_21285_MAJOR, SERIAL_21285_MINOR);
diff --git a/drivers/serial/68328serial.c b/drivers/serial/68328serial.c
new file mode 100644
index 000000000000..db92a0ceda79
--- /dev/null
+++ b/drivers/serial/68328serial.c
@@ -0,0 +1,1614 @@
1/* 68328serial.c: Serial port driver for 68328 microcontroller
2 *
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com>
8 * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
9 *
10 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
11 * Multiple UART support Daniel Potts <danielp@cse.unsw.edu.au>
12 * Power management support Daniel Potts <danielp@cse.unsw.edu.au>
13 * VZ Second Serial Port enable Phil Wilshire
14 * 2.4/2.5 port David McCullough
15 */
16
17#include <asm/dbg.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/signal.h>
21#include <linux/sched.h>
22#include <linux/timer.h>
23#include <linux/interrupt.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/config.h>
27#include <linux/major.h>
28#include <linux/string.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/kernel.h>
32#include <linux/console.h>
33#include <linux/reboot.h>
34#include <linux/keyboard.h>
35#include <linux/init.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/delay.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/system.h>
43#include <asm/segment.h>
44#include <asm/delay.h>
45#include <asm/uaccess.h>
46
47/* (es) */
48/* note: perhaps we can murge these files, so that you can just
49 * define 1 of them, and they can sort that out for themselves
50 */
51#if defined(CONFIG_M68EZ328)
52#include <asm/MC68EZ328.h>
53#else
54#if defined(CONFIG_M68VZ328)
55#include <asm/MC68VZ328.h>
56#else
57#include <asm/MC68328.h>
58#endif /* CONFIG_M68VZ328 */
59#endif /* CONFIG_M68EZ328 */
60
61#include "68328serial.h"
62
63/* Turn off usage of real serial interrupt code, to "support" Copilot */
64#ifdef CONFIG_XCOPILOT_BUGS
65#undef USE_INTS
66#else
67#define USE_INTS
68#endif
69
70static struct m68k_serial m68k_soft[NR_PORTS];
71struct m68k_serial *IRQ_ports[NR_IRQS];
72
73static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
74
75/* multiple ports are contiguous in memory */
76m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
77
78struct tty_struct m68k_ttys;
79struct m68k_serial *m68k_consinfo = 0;
80
81#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
82
83#ifdef CONFIG_CONSOLE
84extern wait_queue_head_t keypress_wait;
85#endif
86
87struct tty_driver *serial_driver;
88
89/* serial subtype definitions */
90#define SERIAL_TYPE_NORMAL 1
91
92/* number of characters left in xmit buffer before we ask for more */
93#define WAKEUP_CHARS 256
94
95/* Debugging... DEBUG_INTR is bad to use when one of the zs
96 * lines is your console ;(
97 */
98#undef SERIAL_DEBUG_INTR
99#undef SERIAL_DEBUG_OPEN
100#undef SERIAL_DEBUG_FLOW
101
102#define RS_ISR_PASS_LIMIT 256
103
104#define _INLINE_ inline
105
106static void change_speed(struct m68k_serial *info);
107
108/*
109 * Setup for console. Argument comes from the boot command line.
110 */
111
112#if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
113#define CONSOLE_BAUD_RATE 115200
114#define DEFAULT_CBAUD B115200
115#else
116 /* (es) */
117 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
118 #ifdef CONFIG_M68VZ328
119 #define CONSOLE_BAUD_RATE 19200
120 #define DEFAULT_CBAUD B19200
121 #endif
122 /* (/es) */
123#endif
124
125#ifndef CONSOLE_BAUD_RATE
126#define CONSOLE_BAUD_RATE 9600
127#define DEFAULT_CBAUD B9600
128#endif
129
130
131static int m68328_console_initted = 0;
132static int m68328_console_baud = CONSOLE_BAUD_RATE;
133static int m68328_console_cbaud = DEFAULT_CBAUD;
134
135
136/*
137 * tmp_buf is used as a temporary buffer by serial_write. We need to
138 * lock it in case the memcpy_fromfs blocks while swapping in a page,
139 * and some other program tries to do a serial write at the same time.
140 * Since the lock will only come under contention when the system is
141 * swapping and available memory is low, it makes sense to share one
142 * buffer across all the serial ports, since it significantly saves
143 * memory if large numbers of serial ports are open.
144 */
145static unsigned char tmp_buf[SERIAL_XMIT_SIZE]; /* This is cheating */
146DECLARE_MUTEX(tmp_buf_sem);
147
148static inline int serial_paranoia_check(struct m68k_serial *info,
149 char *name, const char *routine)
150{
151#ifdef SERIAL_PARANOIA_CHECK
152 static const char *badmagic =
153 "Warning: bad magic number for serial struct %s in %s\n";
154 static const char *badinfo =
155 "Warning: null m68k_serial for %s in %s\n";
156
157 if (!info) {
158 printk(badinfo, name, routine);
159 return 1;
160 }
161 if (info->magic != SERIAL_MAGIC) {
162 printk(badmagic, name, routine);
163 return 1;
164 }
165#endif
166 return 0;
167}
168
169/*
170 * This is used to figure out the divisor speeds and the timeouts
171 */
172static int baud_table[] = {
173 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
174 9600, 19200, 38400, 57600, 115200, 0 };
175
176#define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
177
178/* Sets or clears DTR/RTS on the requested line */
179static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
180{
181 if (set) {
182 /* set the RTS/CTS line */
183 } else {
184 /* clear it */
185 }
186 return;
187}
188
189/* Utility routines */
190static inline int get_baud(struct m68k_serial *ss)
191{
192 unsigned long result = 115200;
193 unsigned short int baud = uart_addr[ss->line].ubaud;
194 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
195 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
196
197 return result;
198}
199
200/*
201 * ------------------------------------------------------------
202 * rs_stop() and rs_start()
203 *
204 * This routines are called before setting or resetting tty->stopped.
205 * They enable or disable transmitter interrupts, as necessary.
206 * ------------------------------------------------------------
207 */
208static void rs_stop(struct tty_struct *tty)
209{
210 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
211 m68328_uart *uart = &uart_addr[info->line];
212 unsigned long flags;
213
214 if (serial_paranoia_check(info, tty->name, "rs_stop"))
215 return;
216
217 save_flags(flags); cli();
218 uart->ustcnt &= ~USTCNT_TXEN;
219 restore_flags(flags);
220}
221
222static void rs_put_char(char ch)
223{
224 int flags, loops = 0;
225
226 save_flags(flags); cli();
227
228 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
229 loops++;
230 udelay(5);
231 }
232
233 UTX_TXDATA = ch;
234 udelay(5);
235 restore_flags(flags);
236}
237
238static void rs_start(struct tty_struct *tty)
239{
240 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
241 m68328_uart *uart = &uart_addr[info->line];
242 unsigned long flags;
243
244 if (serial_paranoia_check(info, tty->name, "rs_start"))
245 return;
246
247 save_flags(flags); cli();
248 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
249#ifdef USE_INTS
250 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
251#else
252 uart->ustcnt |= USTCNT_TXEN;
253#endif
254 }
255 restore_flags(flags);
256}
257
258/* Drop into either the boot monitor or kadb upon receiving a break
259 * from keyboard/console input.
260 */
261static void batten_down_hatches(void)
262{
263 /* Drop into the debugger */
264}
265
266static _INLINE_ void status_handle(struct m68k_serial *info, unsigned short status)
267{
268#if 0
269 if(status & DCD) {
270 if((info->tty->termios->c_cflag & CRTSCTS) &&
271 ((info->curregs[3] & AUTO_ENAB)==0)) {
272 info->curregs[3] |= AUTO_ENAB;
273 info->pendregs[3] |= AUTO_ENAB;
274 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
275 }
276 } else {
277 if((info->curregs[3] & AUTO_ENAB)) {
278 info->curregs[3] &= ~AUTO_ENAB;
279 info->pendregs[3] &= ~AUTO_ENAB;
280 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
281 }
282 }
283#endif
284 /* If this is console input and this is a
285 * 'break asserted' status change interrupt
286 * see if we can drop into the debugger
287 */
288 if((status & URX_BREAK) && info->break_abort)
289 batten_down_hatches();
290 return;
291}
292
293static _INLINE_ void receive_chars(struct m68k_serial *info, struct pt_regs *regs, unsigned short rx)
294{
295 struct tty_struct *tty = info->tty;
296 m68328_uart *uart = &uart_addr[info->line];
297 unsigned char ch;
298
299 /*
300 * This do { } while() loop will get ALL chars out of Rx FIFO
301 */
302#ifndef CONFIG_XCOPILOT_BUGS
303 do {
304#endif
305 ch = GET_FIELD(rx, URX_RXDATA);
306
307 if(info->is_cons) {
308 if(URX_BREAK & rx) { /* whee, break received */
309 status_handle(info, rx);
310 return;
311#ifdef CONFIG_MAGIC_SYSRQ
312 } else if (ch == 0x10) { /* ^P */
313 show_state();
314 show_free_areas();
315 show_buffers();
316/* show_net_buffers(); */
317 return;
318 } else if (ch == 0x12) { /* ^R */
319 machine_restart(NULL);
320 return;
321#endif /* CONFIG_MAGIC_SYSRQ */
322 }
323 /* It is a 'keyboard interrupt' ;-) */
324#ifdef CONFIG_CONSOLE
325 wake_up(&keypress_wait);
326#endif
327 }
328
329 if(!tty)
330 goto clear_and_exit;
331
332 /*
333 * Make sure that we do not overflow the buffer
334 */
335 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
336 schedule_work(&tty->flip.work);
337 return;
338 }
339
340 if(rx & URX_PARITY_ERROR) {
341 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
342 status_handle(info, rx);
343 } else if(rx & URX_OVRUN) {
344 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
345 status_handle(info, rx);
346 } else if(rx & URX_FRAME_ERROR) {
347 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
348 status_handle(info, rx);
349 } else {
350 *tty->flip.flag_buf_ptr++ = 0; /* XXX */
351 }
352 *tty->flip.char_buf_ptr++ = ch;
353 tty->flip.count++;
354
355#ifndef CONFIG_XCOPILOT_BUGS
356 } while((rx = uart->urx.w) & URX_DATA_READY);
357#endif
358
359 schedule_work(&tty->flip.work);
360
361clear_and_exit:
362 return;
363}
364
365static _INLINE_ void transmit_chars(struct m68k_serial *info)
366{
367 m68328_uart *uart = &uart_addr[info->line];
368
369 if (info->x_char) {
370 /* Send next char */
371 uart->utx.b.txdata = info->x_char;
372 info->x_char = 0;
373 goto clear_and_return;
374 }
375
376 if((info->xmit_cnt <= 0) || info->tty->stopped) {
377 /* That's peculiar... TX ints off */
378 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
379 goto clear_and_return;
380 }
381
382 /* Send char */
383 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
384 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
385 info->xmit_cnt--;
386
387 if (info->xmit_cnt < WAKEUP_CHARS)
388 schedule_work(&info->tqueue);
389
390 if(info->xmit_cnt <= 0) {
391 /* All done for now... TX ints off */
392 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
393 goto clear_and_return;
394 }
395
396clear_and_return:
397 /* Clear interrupt (should be auto)*/
398 return;
399}
400
401/*
402 * This is the serial driver's generic interrupt routine
403 */
404irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
405{
406 struct m68k_serial * info;
407 m68328_uart *uart;
408 unsigned short rx;
409 unsigned short tx;
410
411 info = IRQ_ports[irq];
412 if(!info)
413 return IRQ_NONE;
414
415 uart = &uart_addr[info->line];
416 rx = uart->urx.w;
417
418#ifdef USE_INTS
419 tx = uart->utx.w;
420
421 if (rx & URX_DATA_READY) receive_chars(info, regs, rx);
422 if (tx & UTX_TX_AVAIL) transmit_chars(info);
423#else
424 receive_chars(info, regs, rx);
425#endif
426 return IRQ_HANDLED;
427}
428
429static void do_softint(void *private)
430{
431 struct m68k_serial *info = (struct m68k_serial *) private;
432 struct tty_struct *tty;
433
434 tty = info->tty;
435 if (!tty)
436 return;
437#if 0
438 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
439 tty_wakeup(tty);
440 }
441#endif
442}
443
444/*
445 * This routine is called from the scheduler tqueue when the interrupt
446 * routine has signalled that a hangup has occurred. The path of
447 * hangup processing is:
448 *
449 * serial interrupt routine -> (scheduler tqueue) ->
450 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
451 *
452 */
453static void do_serial_hangup(void *private)
454{
455 struct m68k_serial *info = (struct m68k_serial *) private;
456 struct tty_struct *tty;
457
458 tty = info->tty;
459 if (!tty)
460 return;
461
462 tty_hangup(tty);
463}
464
465
466static int startup(struct m68k_serial * info)
467{
468 m68328_uart *uart = &uart_addr[info->line];
469 unsigned long flags;
470
471 if (info->flags & S_INITIALIZED)
472 return 0;
473
474 if (!info->xmit_buf) {
475 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
476 if (!info->xmit_buf)
477 return -ENOMEM;
478 }
479
480 save_flags(flags); cli();
481
482 /*
483 * Clear the FIFO buffers and disable them
484 * (they will be reenabled in change_speed())
485 */
486
487 uart->ustcnt = USTCNT_UEN;
488 info->xmit_fifo_size = 1;
489 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
490 (void)uart->urx.w;
491
492 /*
493 * Finally, enable sequencing and interrupts
494 */
495#ifdef USE_INTS
496 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
497 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
498#else
499 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
500#endif
501
502 if (info->tty)
503 clear_bit(TTY_IO_ERROR, &info->tty->flags);
504 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
505
506 /*
507 * and set the speed of the serial port
508 */
509
510 change_speed(info);
511
512 info->flags |= S_INITIALIZED;
513 restore_flags(flags);
514 return 0;
515}
516
517/*
518 * This routine will shutdown a serial port; interrupts are disabled, and
519 * DTR is dropped if the hangup on close termio flag is on.
520 */
521static void shutdown(struct m68k_serial * info)
522{
523 m68328_uart *uart = &uart_addr[info->line];
524 unsigned long flags;
525
526 uart->ustcnt = 0; /* All off! */
527 if (!(info->flags & S_INITIALIZED))
528 return;
529
530 save_flags(flags); cli(); /* Disable interrupts */
531
532 if (info->xmit_buf) {
533 free_page((unsigned long) info->xmit_buf);
534 info->xmit_buf = 0;
535 }
536
537 if (info->tty)
538 set_bit(TTY_IO_ERROR, &info->tty->flags);
539
540 info->flags &= ~S_INITIALIZED;
541 restore_flags(flags);
542}
543
544struct {
545 int divisor, prescale;
546}
547#ifndef CONFIG_M68VZ328
548 hw_baud_table[18] = {
549 {0,0}, /* 0 */
550 {0,0}, /* 50 */
551 {0,0}, /* 75 */
552 {0,0}, /* 110 */
553 {0,0}, /* 134 */
554 {0,0}, /* 150 */
555 {0,0}, /* 200 */
556 {7,0x26}, /* 300 */
557 {6,0x26}, /* 600 */
558 {5,0x26}, /* 1200 */
559 {0,0}, /* 1800 */
560 {4,0x26}, /* 2400 */
561 {3,0x26}, /* 4800 */
562 {2,0x26}, /* 9600 */
563 {1,0x26}, /* 19200 */
564 {0,0x26}, /* 38400 */
565 {1,0x38}, /* 57600 */
566 {0,0x38}, /* 115200 */
567};
568#else
569 hw_baud_table[18] = {
570 {0,0}, /* 0 */
571 {0,0}, /* 50 */
572 {0,0}, /* 75 */
573 {0,0}, /* 110 */
574 {0,0}, /* 134 */
575 {0,0}, /* 150 */
576 {0,0}, /* 200 */
577 {0,0}, /* 300 */
578 {7,0x26}, /* 600 */
579 {6,0x26}, /* 1200 */
580 {0,0}, /* 1800 */
581 {5,0x26}, /* 2400 */
582 {4,0x26}, /* 4800 */
583 {3,0x26}, /* 9600 */
584 {2,0x26}, /* 19200 */
585 {1,0x26}, /* 38400 */
586 {0,0x26}, /* 57600 */
587 {1,0x38}, /* 115200 */
588};
589#endif
590/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
591
592/*
593 * This routine is called to set the UART divisor registers to match
594 * the specified baud rate for a serial port.
595 */
596static void change_speed(struct m68k_serial *info)
597{
598 m68328_uart *uart = &uart_addr[info->line];
599 unsigned short port;
600 unsigned short ustcnt;
601 unsigned cflag;
602 int i;
603
604 if (!info->tty || !info->tty->termios)
605 return;
606 cflag = info->tty->termios->c_cflag;
607 if (!(port = info->port))
608 return;
609
610 ustcnt = uart->ustcnt;
611 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
612
613 i = cflag & CBAUD;
614 if (i & CBAUDEX) {
615 i = (i & ~CBAUDEX) + B38400;
616 }
617
618 info->baud = baud_table[i];
619 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
620 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
621
622 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
623
624 if ((cflag & CSIZE) == CS8)
625 ustcnt |= USTCNT_8_7;
626
627 if (cflag & CSTOPB)
628 ustcnt |= USTCNT_STOP;
629
630 if (cflag & PARENB)
631 ustcnt |= USTCNT_PARITYEN;
632 if (cflag & PARODD)
633 ustcnt |= USTCNT_ODD_EVEN;
634
635#ifdef CONFIG_SERIAL_68328_RTS_CTS
636 if (cflag & CRTSCTS) {
637 uart->utx.w &= ~ UTX_NOCTS;
638 } else {
639 uart->utx.w |= UTX_NOCTS;
640 }
641#endif
642
643 ustcnt |= USTCNT_TXEN;
644
645 uart->ustcnt = ustcnt;
646 return;
647}
648
649/*
650 * Fair output driver allows a process to speak.
651 */
652static void rs_fair_output(void)
653{
654 int left; /* Output no more than that */
655 unsigned long flags;
656 struct m68k_serial *info = &m68k_soft[0];
657 char c;
658
659 if (info == 0) return;
660 if (info->xmit_buf == 0) return;
661
662 save_flags(flags); cli();
663 left = info->xmit_cnt;
664 while (left != 0) {
665 c = info->xmit_buf[info->xmit_tail];
666 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
667 info->xmit_cnt--;
668 restore_flags(flags);
669
670 rs_put_char(c);
671
672 save_flags(flags); cli();
673 left = min(info->xmit_cnt, left-1);
674 }
675
676 /* Last character is being transmitted now (hopefully). */
677 udelay(5);
678
679 restore_flags(flags);
680 return;
681}
682
683/*
684 * m68k_console_print is registered for printk.
685 */
686void console_print_68328(const char *p)
687{
688 char c;
689
690 while((c=*(p++)) != 0) {
691 if(c == '\n')
692 rs_put_char('\r');
693 rs_put_char(c);
694 }
695
696 /* Comment this if you want to have a strict interrupt-driven output */
697 rs_fair_output();
698
699 return;
700}
701
702static void rs_set_ldisc(struct tty_struct *tty)
703{
704 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
705
706 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
707 return;
708
709 info->is_cons = (tty->termios->c_line == N_TTY);
710
711 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
712}
713
714static void rs_flush_chars(struct tty_struct *tty)
715{
716 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
717 m68328_uart *uart = &uart_addr[info->line];
718 unsigned long flags;
719
720 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
721 return;
722#ifndef USE_INTS
723 for(;;) {
724#endif
725
726 /* Enable transmitter */
727 save_flags(flags); cli();
728
729 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
730 !info->xmit_buf) {
731 restore_flags(flags);
732 return;
733 }
734
735#ifdef USE_INTS
736 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
737#else
738 uart->ustcnt |= USTCNT_TXEN;
739#endif
740
741#ifdef USE_INTS
742 if (uart->utx.w & UTX_TX_AVAIL) {
743#else
744 if (1) {
745#endif
746 /* Send char */
747 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
748 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
749 info->xmit_cnt--;
750 }
751
752#ifndef USE_INTS
753 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
754 }
755#endif
756 restore_flags(flags);
757}
758
759extern void console_printn(const char * b, int count);
760
761static int rs_write(struct tty_struct * tty,
762 const unsigned char *buf, int count)
763{
764 int c, total = 0;
765 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
766 m68328_uart *uart = &uart_addr[info->line];
767 unsigned long flags;
768
769 if (serial_paranoia_check(info, tty->name, "rs_write"))
770 return 0;
771
772 if (!tty || !info->xmit_buf)
773 return 0;
774
775 save_flags(flags);
776 while (1) {
777 cli();
778 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
779 SERIAL_XMIT_SIZE - info->xmit_head));
780 if (c <= 0)
781 break;
782
783 memcpy(info->xmit_buf + info->xmit_head, buf, c);
784 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
785 info->xmit_cnt += c;
786 restore_flags(flags);
787 buf += c;
788 count -= c;
789 total += c;
790 }
791
792 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
793 /* Enable transmitter */
794 cli();
795#ifndef USE_INTS
796 while(info->xmit_cnt) {
797#endif
798
799 uart->ustcnt |= USTCNT_TXEN;
800#ifdef USE_INTS
801 uart->ustcnt |= USTCNT_TX_INTR_MASK;
802#else
803 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
804#endif
805 if (uart->utx.w & UTX_TX_AVAIL) {
806 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
807 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
808 info->xmit_cnt--;
809 }
810
811#ifndef USE_INTS
812 }
813#endif
814 restore_flags(flags);
815 }
816 restore_flags(flags);
817 return total;
818}
819
820static int rs_write_room(struct tty_struct *tty)
821{
822 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
823 int ret;
824
825 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
826 return 0;
827 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
828 if (ret < 0)
829 ret = 0;
830 return ret;
831}
832
833static int rs_chars_in_buffer(struct tty_struct *tty)
834{
835 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
836
837 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
838 return 0;
839 return info->xmit_cnt;
840}
841
842static void rs_flush_buffer(struct tty_struct *tty)
843{
844 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
845
846 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
847 return;
848 cli();
849 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
850 sti();
851 tty_wakeup(tty);
852}
853
854/*
855 * ------------------------------------------------------------
856 * rs_throttle()
857 *
858 * This routine is called by the upper-layer tty layer to signal that
859 * incoming characters should be throttled.
860 * ------------------------------------------------------------
861 */
862static void rs_throttle(struct tty_struct * tty)
863{
864 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
865
866 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
867 return;
868
869 if (I_IXOFF(tty))
870 info->x_char = STOP_CHAR(tty);
871
872 /* Turn off RTS line (do this atomic) */
873}
874
875static void rs_unthrottle(struct tty_struct * tty)
876{
877 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
878
879 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
880 return;
881
882 if (I_IXOFF(tty)) {
883 if (info->x_char)
884 info->x_char = 0;
885 else
886 info->x_char = START_CHAR(tty);
887 }
888
889 /* Assert RTS line (do this atomic) */
890}
891
892/*
893 * ------------------------------------------------------------
894 * rs_ioctl() and friends
895 * ------------------------------------------------------------
896 */
897
898static int get_serial_info(struct m68k_serial * info,
899 struct serial_struct * retinfo)
900{
901 struct serial_struct tmp;
902
903 if (!retinfo)
904 return -EFAULT;
905 memset(&tmp, 0, sizeof(tmp));
906 tmp.type = info->type;
907 tmp.line = info->line;
908 tmp.port = info->port;
909 tmp.irq = info->irq;
910 tmp.flags = info->flags;
911 tmp.baud_base = info->baud_base;
912 tmp.close_delay = info->close_delay;
913 tmp.closing_wait = info->closing_wait;
914 tmp.custom_divisor = info->custom_divisor;
915 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
916 return 0;
917}
918
919static int set_serial_info(struct m68k_serial * info,
920 struct serial_struct * new_info)
921{
922 struct serial_struct new_serial;
923 struct m68k_serial old_info;
924 int retval = 0;
925
926 if (!new_info)
927 return -EFAULT;
928 copy_from_user(&new_serial,new_info,sizeof(new_serial));
929 old_info = *info;
930
931 if (!capable(CAP_SYS_ADMIN)) {
932 if ((new_serial.baud_base != info->baud_base) ||
933 (new_serial.type != info->type) ||
934 (new_serial.close_delay != info->close_delay) ||
935 ((new_serial.flags & ~S_USR_MASK) !=
936 (info->flags & ~S_USR_MASK)))
937 return -EPERM;
938 info->flags = ((info->flags & ~S_USR_MASK) |
939 (new_serial.flags & S_USR_MASK));
940 info->custom_divisor = new_serial.custom_divisor;
941 goto check_and_exit;
942 }
943
944 if (info->count > 1)
945 return -EBUSY;
946
947 /*
948 * OK, past this point, all the error checking has been done.
949 * At this point, we start making changes.....
950 */
951
952 info->baud_base = new_serial.baud_base;
953 info->flags = ((info->flags & ~S_FLAGS) |
954 (new_serial.flags & S_FLAGS));
955 info->type = new_serial.type;
956 info->close_delay = new_serial.close_delay;
957 info->closing_wait = new_serial.closing_wait;
958
959check_and_exit:
960 retval = startup(info);
961 return retval;
962}
963
964/*
965 * get_lsr_info - get line status register info
966 *
967 * Purpose: Let user call ioctl() to get info when the UART physically
968 * is emptied. On bus types like RS485, the transmitter must
969 * release the bus after transmitting. This must be done when
970 * the transmit shift register is empty, not be done when the
971 * transmit holding register is empty. This functionality
972 * allows an RS485 driver to be written in user space.
973 */
974static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
975{
976#ifdef CONFIG_SERIAL_68328_RTS_CTS
977 m68328_uart *uart = &uart_addr[info->line];
978#endif
979 unsigned char status;
980
981 cli();
982#ifdef CONFIG_SERIAL_68328_RTS_CTS
983 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
984#else
985 status = 0;
986#endif
987 sti();
988 put_user(status,value);
989 return 0;
990}
991
992/*
993 * This routine sends a break character out the serial port.
994 */
995static void send_break( struct m68k_serial * info, int duration)
996{
997 m68328_uart *uart = &uart_addr[info->line];
998 unsigned long flags;
999 if (!info->port)
1000 return;
1001 set_current_state(TASK_INTERRUPTIBLE);
1002 save_flags(flags);
1003 cli();
1004#ifdef USE_INTS
1005 uart->utx.w |= UTX_SEND_BREAK;
1006 schedule_timeout(duration);
1007 uart->utx.w &= ~UTX_SEND_BREAK;
1008#endif
1009 restore_flags(flags);
1010}
1011
1012static int rs_ioctl(struct tty_struct *tty, struct file * file,
1013 unsigned int cmd, unsigned long arg)
1014{
1015 int error;
1016 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1017 int retval;
1018
1019 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1020 return -ENODEV;
1021
1022 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1023 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1024 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1025 if (tty->flags & (1 << TTY_IO_ERROR))
1026 return -EIO;
1027 }
1028
1029 switch (cmd) {
1030 case TCSBRK: /* SVID version: non-zero arg --> no break */
1031 retval = tty_check_change(tty);
1032 if (retval)
1033 return retval;
1034 tty_wait_until_sent(tty, 0);
1035 if (!arg)
1036 send_break(info, HZ/4); /* 1/4 second */
1037 return 0;
1038 case TCSBRKP: /* support for POSIX tcsendbreak() */
1039 retval = tty_check_change(tty);
1040 if (retval)
1041 return retval;
1042 tty_wait_until_sent(tty, 0);
1043 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1044 return 0;
1045 case TIOCGSOFTCAR:
1046 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1047 (unsigned long *) arg);
1048 if (error)
1049 return error;
1050 return 0;
1051 case TIOCSSOFTCAR:
1052 get_user(arg, (unsigned long *) arg);
1053 tty->termios->c_cflag =
1054 ((tty->termios->c_cflag & ~CLOCAL) |
1055 (arg ? CLOCAL : 0));
1056 return 0;
1057 case TIOCGSERIAL:
1058 if (access_ok(VERIFY_WRITE, (void *) arg,
1059 sizeof(struct serial_struct)))
1060 return get_serial_info(info,
1061 (struct serial_struct *) arg);
1062 return -EFAULT;
1063 case TIOCSSERIAL:
1064 return set_serial_info(info,
1065 (struct serial_struct *) arg);
1066 case TIOCSERGETLSR: /* Get line status register */
1067 if (access_ok(VERIFY_WRITE, (void *) arg,
1068 sizeof(unsigned int));
1069 return get_lsr_info(info, (unsigned int *) arg);
1070 return -EFAULT;
1071 case TIOCSERGSTRUCT:
1072 if (!access_ok(VERIFY_WRITE, (void *) arg,
1073 sizeof(struct m68k_serial)))
1074 return -EFAULT;
1075 copy_to_user((struct m68k_serial *) arg,
1076 info, sizeof(struct m68k_serial));
1077 return 0;
1078
1079 default:
1080 return -ENOIOCTLCMD;
1081 }
1082 return 0;
1083}
1084
1085static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1086{
1087 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1088
1089 if (tty->termios->c_cflag == old_termios->c_cflag)
1090 return;
1091
1092 change_speed(info);
1093
1094 if ((old_termios->c_cflag & CRTSCTS) &&
1095 !(tty->termios->c_cflag & CRTSCTS)) {
1096 tty->hw_stopped = 0;
1097 rs_start(tty);
1098 }
1099
1100}
1101
1102/*
1103 * ------------------------------------------------------------
1104 * rs_close()
1105 *
1106 * This routine is called when the serial port gets closed. First, we
1107 * wait for the last remaining data to be sent. Then, we unlink its
1108 * S structure from the interrupt chain if necessary, and we free
1109 * that IRQ if nothing is left in the chain.
1110 * ------------------------------------------------------------
1111 */
1112static void rs_close(struct tty_struct *tty, struct file * filp)
1113{
1114 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1115 m68328_uart *uart = &uart_addr[info->line];
1116 unsigned long flags;
1117
1118 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1119 return;
1120
1121 save_flags(flags); cli();
1122
1123 if (tty_hung_up_p(filp)) {
1124 restore_flags(flags);
1125 return;
1126 }
1127
1128 if ((tty->count == 1) && (info->count != 1)) {
1129 /*
1130 * Uh, oh. tty->count is 1, which means that the tty
1131 * structure will be freed. Info->count should always
1132 * be one in these conditions. If it's greater than
1133 * one, we've got real problems, since it means the
1134 * serial port won't be shutdown.
1135 */
1136 printk("rs_close: bad serial port count; tty->count is 1, "
1137 "info->count is %d\n", info->count);
1138 info->count = 1;
1139 }
1140 if (--info->count < 0) {
1141 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1142 info->line, info->count);
1143 info->count = 0;
1144 }
1145 if (info->count) {
1146 restore_flags(flags);
1147 return;
1148 }
1149 info->flags |= S_CLOSING;
1150 /*
1151 * Now we wait for the transmit buffer to clear; and we notify
1152 * the line discipline to only process XON/XOFF characters.
1153 */
1154 tty->closing = 1;
1155 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1156 tty_wait_until_sent(tty, info->closing_wait);
1157 /*
1158 * At this point we stop accepting input. To do this, we
1159 * disable the receive line status interrupts, and tell the
1160 * interrupt driver to stop checking the data ready bit in the
1161 * line status register.
1162 */
1163
1164 uart->ustcnt &= ~USTCNT_RXEN;
1165 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1166
1167 shutdown(info);
1168 if (tty->driver->flush_buffer)
1169 tty->driver->flush_buffer(tty);
1170
1171 tty_ldisc_flush(tty);
1172 tty->closing = 0;
1173 info->event = 0;
1174 info->tty = 0;
1175#warning "This is not and has never been valid so fix it"
1176#if 0
1177 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1178 if (tty->ldisc.close)
1179 (tty->ldisc.close)(tty);
1180 tty->ldisc = ldiscs[N_TTY];
1181 tty->termios->c_line = N_TTY;
1182 if (tty->ldisc.open)
1183 (tty->ldisc.open)(tty);
1184 }
1185#endif
1186 if (info->blocked_open) {
1187 if (info->close_delay) {
1188 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1189 }
1190 wake_up_interruptible(&info->open_wait);
1191 }
1192 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1193 wake_up_interruptible(&info->close_wait);
1194 restore_flags(flags);
1195}
1196
1197/*
1198 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1199 */
1200void rs_hangup(struct tty_struct *tty)
1201{
1202 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1203
1204 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1205 return;
1206
1207 rs_flush_buffer(tty);
1208 shutdown(info);
1209 info->event = 0;
1210 info->count = 0;
1211 info->flags &= ~S_NORMAL_ACTIVE;
1212 info->tty = 0;
1213 wake_up_interruptible(&info->open_wait);
1214}
1215
1216/*
1217 * ------------------------------------------------------------
1218 * rs_open() and friends
1219 * ------------------------------------------------------------
1220 */
1221static int block_til_ready(struct tty_struct *tty, struct file * filp,
1222 struct m68k_serial *info)
1223{
1224 DECLARE_WAITQUEUE(wait, current);
1225 int retval;
1226 int do_clocal = 0;
1227
1228 /*
1229 * If the device is in the middle of being closed, then block
1230 * until it's done, and then try again.
1231 */
1232 if (info->flags & S_CLOSING) {
1233 interruptible_sleep_on(&info->close_wait);
1234#ifdef SERIAL_DO_RESTART
1235 if (info->flags & S_HUP_NOTIFY)
1236 return -EAGAIN;
1237 else
1238 return -ERESTARTSYS;
1239#else
1240 return -EAGAIN;
1241#endif
1242 }
1243
1244 /*
1245 * If non-blocking mode is set, or the port is not enabled,
1246 * then make the check up front and then exit.
1247 */
1248 if ((filp->f_flags & O_NONBLOCK) ||
1249 (tty->flags & (1 << TTY_IO_ERROR))) {
1250 info->flags |= S_NORMAL_ACTIVE;
1251 return 0;
1252 }
1253
1254 if (tty->termios->c_cflag & CLOCAL)
1255 do_clocal = 1;
1256
1257 /*
1258 * Block waiting for the carrier detect and the line to become
1259 * free (i.e., not in use by the callout). While we are in
1260 * this loop, info->count is dropped by one, so that
1261 * rs_close() knows when to free things. We restore it upon
1262 * exit, either normal or abnormal.
1263 */
1264 retval = 0;
1265 add_wait_queue(&info->open_wait, &wait);
1266
1267 info->count--;
1268 info->blocked_open++;
1269 while (1) {
1270 cli();
1271 m68k_rtsdtr(info, 1);
1272 sti();
1273 current->state = TASK_INTERRUPTIBLE;
1274 if (tty_hung_up_p(filp) ||
1275 !(info->flags & S_INITIALIZED)) {
1276#ifdef SERIAL_DO_RESTART
1277 if (info->flags & S_HUP_NOTIFY)
1278 retval = -EAGAIN;
1279 else
1280 retval = -ERESTARTSYS;
1281#else
1282 retval = -EAGAIN;
1283#endif
1284 break;
1285 }
1286 if (!(info->flags & S_CLOSING) && do_clocal)
1287 break;
1288 if (signal_pending(current)) {
1289 retval = -ERESTARTSYS;
1290 break;
1291 }
1292 schedule();
1293 }
1294 current->state = TASK_RUNNING;
1295 remove_wait_queue(&info->open_wait, &wait);
1296 if (!tty_hung_up_p(filp))
1297 info->count++;
1298 info->blocked_open--;
1299
1300 if (retval)
1301 return retval;
1302 info->flags |= S_NORMAL_ACTIVE;
1303 return 0;
1304}
1305
1306/*
1307 * This routine is called whenever a serial port is opened. It
1308 * enables interrupts for a serial port, linking in its S structure into
1309 * the IRQ chain. It also performs the serial-specific
1310 * initialization for the tty structure.
1311 */
1312int rs_open(struct tty_struct *tty, struct file * filp)
1313{
1314 struct m68k_serial *info;
1315 int retval, line;
1316
1317 line = tty->index;
1318
1319 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1320 return -ENODEV;
1321
1322 info = &m68k_soft[line];
1323
1324 if (serial_paranoia_check(info, tty->name, "rs_open"))
1325 return -ENODEV;
1326
1327 info->count++;
1328 tty->driver_data = info;
1329 info->tty = tty;
1330
1331 /*
1332 * Start up serial port
1333 */
1334 retval = startup(info);
1335 if (retval)
1336 return retval;
1337
1338 return block_til_ready(tty, filp, info);
1339}
1340
1341/* Finally, routines used to initialize the serial driver. */
1342
1343static void show_serial_version(void)
1344{
1345 printk("MC68328 serial driver version 1.00\n");
1346}
1347
1348#ifdef CONFIG_PM
1349/* Serial Power management
1350 * The console (currently fixed at line 0) is a special case for power
1351 * management because the kernel is so chatty. The console will be
1352 * explicitly disabled my our power manager as the last minute, so we won't
1353 * mess with it here.
1354 */
1355static struct pm_dev *serial_pm[NR_PORTS];
1356
1357static int serial_pm_callback(struct pm_dev *dev, pm_request_t request, void *data)
1358{
1359 struct m68k_serial *info = (struct m68k_serial *)dev->data;
1360
1361 if(info == NULL)
1362 return -1;
1363
1364 /* special case for line 0 - pm restores it */
1365 if(info->line == 0)
1366 return 0;
1367
1368 switch (request) {
1369 case PM_SUSPEND:
1370 shutdown(info);
1371 break;
1372
1373 case PM_RESUME:
1374 startup(info);
1375 break;
1376 }
1377 return 0;
1378}
1379
1380void shutdown_console(void)
1381{
1382 struct m68k_serial *info = &m68k_soft[0];
1383
1384 /* HACK: wait a bit for any pending printk's to be dumped */
1385 {
1386 int i = 10000;
1387 while(i--);
1388 }
1389
1390 shutdown(info);
1391}
1392
1393void startup_console(void)
1394{
1395 struct m68k_serial *info = &m68k_soft[0];
1396 startup(info);
1397}
1398#endif
1399
1400
1401static struct tty_operations rs_ops = {
1402 .open = rs_open,
1403 .close = rs_close,
1404 .write = rs_write,
1405 .flush_chars = rs_flush_chars,
1406 .write_room = rs_write_room,
1407 .chars_in_buffer = rs_chars_in_buffer,
1408 .flush_buffer = rs_flush_buffer,
1409 .ioctl = rs_ioctl,
1410 .throttle = rs_throttle,
1411 .unthrottle = rs_unthrottle,
1412 .set_termios = rs_set_termios,
1413 .stop = rs_stop,
1414 .start = rs_start,
1415 .hangup = rs_hangup,
1416 .set_ldisc = rs_set_ldisc,
1417};
1418
1419/* rs_init inits the driver */
1420static int __init
1421rs68328_init(void)
1422{
1423 int flags, i;
1424 struct m68k_serial *info;
1425
1426 serial_driver = alloc_tty_driver(NR_PORTS);
1427 if (!serial_driver)
1428 return -ENOMEM;
1429
1430 show_serial_version();
1431
1432 /* Initialize the tty_driver structure */
1433 /* SPARC: Not all of this is exactly right for us. */
1434
1435 serial_driver->name = "ttyS";
1436 serial_driver->major = TTY_MAJOR;
1437 serial_driver->minor_start = 64;
1438 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1439 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1440 serial_driver->init_termios = tty_std_termios;
1441 serial_driver->init_termios.c_cflag =
1442 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1443 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1444 tty_set_operations(serial_driver, &rs_ops);
1445
1446 if (tty_register_driver(serial_driver)) {
1447 put_tty_driver(serial_driver);
1448 printk(KERN_ERR "Couldn't register serial driver\n");
1449 return -ENOMEM;
1450 }
1451
1452 save_flags(flags); cli();
1453
1454 for(i=0;i<NR_PORTS;i++) {
1455
1456 info = &m68k_soft[i];
1457 info->magic = SERIAL_MAGIC;
1458 info->port = (int) &uart_addr[i];
1459 info->tty = 0;
1460 info->irq = uart_irqs[i];
1461 info->custom_divisor = 16;
1462 info->close_delay = 50;
1463 info->closing_wait = 3000;
1464 info->x_char = 0;
1465 info->event = 0;
1466 info->count = 0;
1467 info->blocked_open = 0;
1468 INIT_WORK(&info->tqueue, do_softint, info);
1469 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1470 init_waitqueue_head(&info->open_wait);
1471 init_waitqueue_head(&info->close_wait);
1472 info->line = i;
1473 info->is_cons = 1; /* Means shortcuts work */
1474
1475 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1476 info->port, info->irq);
1477 printk(" is a builtin MC68328 UART\n");
1478
1479 IRQ_ports[info->irq] = info; /* waste of space */
1480
1481#ifdef CONFIG_M68VZ328
1482 if (i > 0 )
1483 PJSEL &= 0xCF; /* PSW enable second port output */
1484#endif
1485
1486 if (request_irq(uart_irqs[i],
1487 rs_interrupt,
1488 IRQ_FLG_STD,
1489 "M68328_UART", NULL))
1490 panic("Unable to attach 68328 serial interrupt\n");
1491#ifdef CONFIG_PM
1492 serial_pm[i] = pm_register(PM_SYS_DEV, PM_SYS_COM, serial_pm_callback);
1493 if (serial_pm[i])
1494 serial_pm[i]->data = info;
1495#endif
1496 }
1497 restore_flags(flags);
1498 return 0;
1499}
1500
1501
1502
1503/*
1504 * register_serial and unregister_serial allows for serial ports to be
1505 * configured at run-time, to support PCMCIA modems.
1506 */
1507/* SPARC: Unused at this time, just here to make things link. */
1508int register_serial(struct serial_struct *req)
1509{
1510 return -1;
1511}
1512
1513void unregister_serial(int line)
1514{
1515 return;
1516}
1517
1518module_init(rs68328_init);
1519
1520
1521
1522static void m68328_set_baud(void)
1523{
1524 unsigned short ustcnt;
1525 int i;
1526
1527 ustcnt = USTCNT;
1528 USTCNT = ustcnt & ~USTCNT_TXEN;
1529
1530again:
1531 for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
1532 if (baud_table[i] == m68328_console_baud)
1533 break;
1534 if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
1535 m68328_console_baud = 9600;
1536 goto again;
1537 }
1538
1539 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1540 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1541 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1542 ustcnt |= USTCNT_8_7;
1543 ustcnt |= USTCNT_TXEN;
1544 USTCNT = ustcnt;
1545 m68328_console_initted = 1;
1546 return;
1547}
1548
1549
1550int m68328_console_setup(struct console *cp, char *arg)
1551{
1552 int i, n = CONSOLE_BAUD_RATE;
1553
1554 if (!cp)
1555 return(-1);
1556
1557 if (arg)
1558 n = simple_strtoul(arg,NULL,0);
1559
1560 for (i = 0; i < BAUD_TABLE_SIZE; i++)
1561 if (baud_table[i] == n)
1562 break;
1563 if (i < BAUD_TABLE_SIZE) {
1564 m68328_console_baud = n;
1565 m68328_console_cbaud = 0;
1566 if (i > 15) {
1567 m68328_console_cbaud |= CBAUDEX;
1568 i -= 15;
1569 }
1570 m68328_console_cbaud |= i;
1571 }
1572
1573 m68328_set_baud(); /* make sure baud rate changes */
1574 return(0);
1575}
1576
1577
1578static struct tty_driver *m68328_console_device(struct console *c, int *index)
1579{
1580 *index = c->index;
1581 return serial_driver;
1582}
1583
1584
1585void m68328_console_write (struct console *co, const char *str,
1586 unsigned int count)
1587{
1588 if (!m68328_console_initted)
1589 m68328_set_baud();
1590 while (count--) {
1591 if (*str == '\n')
1592 rs_put_char('\r');
1593 rs_put_char( *str++ );
1594 }
1595}
1596
1597
1598static struct console m68328_driver = {
1599 .name = "ttyS",
1600 .write = m68328_console_write,
1601 .device = m68328_console_device,
1602 .setup = m68328_console_setup,
1603 .flags = CON_PRINTBUFFER,
1604 .index = -1,
1605};
1606
1607
1608static int __init m68328_console_init(void)
1609{
1610 register_console(&m68328_driver);
1611 return 0;
1612}
1613
1614console_initcall(m68328_console_init);
diff --git a/drivers/serial/68328serial.h b/drivers/serial/68328serial.h
new file mode 100644
index 000000000000..978f8a609f3d
--- /dev/null
+++ b/drivers/serial/68328serial.h
@@ -0,0 +1,194 @@
1/* 68328serial.h: Definitions for the mc68328 serial driver.
2 *
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 *
8 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
9 */
10
11#ifndef _MC683XX_SERIAL_H
12#define _MC683XX_SERIAL_H
13
14#include <linux/config.h>
15
16struct serial_struct {
17 int type;
18 int line;
19 int port;
20 int irq;
21 int flags;
22 int xmit_fifo_size;
23 int custom_divisor;
24 int baud_base;
25 unsigned short close_delay;
26 char reserved_char[2];
27 int hub6; /* FIXME: We don't have AT&T Hub6 boards! */
28 unsigned short closing_wait; /* time to wait before closing */
29 unsigned short closing_wait2; /* no longer used... */
30 int reserved[4];
31};
32
33/*
34 * For the close wait times, 0 means wait forever for serial port to
35 * flush its output. 65535 means don't wait at all.
36 */
37#define S_CLOSING_WAIT_INF 0
38#define S_CLOSING_WAIT_NONE 65535
39
40/*
41 * Definitions for S_struct (and serial_struct) flags field
42 */
43#define S_HUP_NOTIFY 0x0001 /* Notify getty on hangups and closes
44 on the callout port */
45#define S_FOURPORT 0x0002 /* Set OU1, OUT2 per AST Fourport settings */
46#define S_SAK 0x0004 /* Secure Attention Key (Orange book) */
47#define S_SPLIT_TERMIOS 0x0008 /* Separate termios for dialin/callout */
48
49#define S_SPD_MASK 0x0030
50#define S_SPD_HI 0x0010 /* Use 56000 instead of 38400 bps */
51
52#define S_SPD_VHI 0x0020 /* Use 115200 instead of 38400 bps */
53#define S_SPD_CUST 0x0030 /* Use user-specified divisor */
54
55#define S_SKIP_TEST 0x0040 /* Skip UART test during autoconfiguration */
56#define S_AUTO_IRQ 0x0080 /* Do automatic IRQ during autoconfiguration */
57#define S_SESSION_LOCKOUT 0x0100 /* Lock out cua opens based on session */
58#define S_PGRP_LOCKOUT 0x0200 /* Lock out cua opens based on pgrp */
59#define S_CALLOUT_NOHUP 0x0400 /* Don't do hangups for cua device */
60
61#define S_FLAGS 0x0FFF /* Possible legal S flags */
62#define S_USR_MASK 0x0430 /* Legal flags that non-privileged
63 * users can set or reset */
64
65/* Internal flags used only by kernel/chr_drv/serial.c */
66#define S_INITIALIZED 0x80000000 /* Serial port was initialized */
67#define S_CALLOUT_ACTIVE 0x40000000 /* Call out device is active */
68#define S_NORMAL_ACTIVE 0x20000000 /* Normal device is active */
69#define S_BOOT_AUTOCONF 0x10000000 /* Autoconfigure port on bootup */
70#define S_CLOSING 0x08000000 /* Serial port is closing */
71#define S_CTS_FLOW 0x04000000 /* Do CTS flow control */
72#define S_CHECK_CD 0x02000000 /* i.e., CLOCAL */
73
74/* Software state per channel */
75
76#ifdef __KERNEL__
77
78/*
79 * I believe this is the optimal setting that reduces the number of interrupts.
80 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
81 * if that bothers you), but in most cases it will not, since we try to
82 * transmit characters every time rs_interrupt is called. Thus, quite often
83 * you'll see that a receive interrupt occures before the transmit one.
84 * -- Vladimir Gurevich
85 */
86#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
87
88/*
89 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
90 * "Old data interrupt" which occures whenever the data stay in the FIFO
91 * longer than 30 bits time. This allows us to use FIFO without compromising
92 * latency. '328 does not have this feature and without the real 328-based
93 * board I would assume that RXRE is the safest setting.
94 *
95 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
96 * interrupts. RXFE (receive queue full) causes the system to lose data
97 * at least at 115200 baud
98 *
99 * If your board is busy doing other stuff, you might consider to use
100 * RXRE (data ready intrrupt) instead.
101 *
102 * The other option is to make these INTR masks run-time configurable, so
103 * that people can dynamically adapt them according to the current usage.
104 * -- Vladimir Gurevich
105 */
106
107/* (es) */
108#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
109#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
110#elif defined(CONFIG_M68328)
111#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
112#else
113#error Please, define the Rx interrupt events for your CPU
114#endif
115/* (/es) */
116
117/*
118 * This is our internal structure for each serial port's state.
119 *
120 * Many fields are paralleled by the structure used by the serial_struct
121 * structure.
122 *
123 * For definitions of the flags field, see tty.h
124 */
125
126struct m68k_serial {
127 char soft_carrier; /* Use soft carrier on this channel */
128 char break_abort; /* Is serial console in, so process brk/abrt */
129 char is_cons; /* Is this our console. */
130
131 /* We need to know the current clock divisor
132 * to read the bps rate the chip has currently
133 * loaded.
134 */
135 unsigned char clk_divisor; /* May be 1, 16, 32, or 64 */
136 int baud;
137 int magic;
138 int baud_base;
139 int port;
140 int irq;
141 int flags; /* defined in tty.h */
142 int type; /* UART type */
143 struct tty_struct *tty;
144 int read_status_mask;
145 int ignore_status_mask;
146 int timeout;
147 int xmit_fifo_size;
148 int custom_divisor;
149 int x_char; /* xon/xoff character */
150 int close_delay;
151 unsigned short closing_wait;
152 unsigned short closing_wait2;
153 unsigned long event;
154 unsigned long last_active;
155 int line;
156 int count; /* # of fd on device */
157 int blocked_open; /* # of blocked opens */
158 unsigned char *xmit_buf;
159 int xmit_head;
160 int xmit_tail;
161 int xmit_cnt;
162 struct work_struct tqueue;
163 struct work_struct tqueue_hangup;
164 wait_queue_head_t open_wait;
165 wait_queue_head_t close_wait;
166};
167
168
169#define SERIAL_MAGIC 0x5301
170
171/*
172 * The size of the serial xmit buffer is 1 page, or 4096 bytes
173 */
174#define SERIAL_XMIT_SIZE 4096
175
176/*
177 * Events are used to schedule things to happen at timer-interrupt
178 * time, instead of at rs interrupt time.
179 */
180#define RS_EVENT_WRITE_WAKEUP 0
181
182/*
183 * Define the number of ports supported and their irqs.
184 */
185#ifndef CONFIG_68328_SERIAL_UART2
186#define NR_PORTS 1
187#define UART_IRQ_DEFNS {UART_IRQ_NUM}
188#else
189#define NR_PORTS 2
190#define UART_IRQ_DEFNS {UART1_IRQ_NUM, UART2_IRQ_NUM}
191#endif
192
193#endif /* __KERNEL__ */
194#endif /* !(_MC683XX_SERIAL_H) */
diff --git a/drivers/serial/68360serial.c b/drivers/serial/68360serial.c
new file mode 100644
index 000000000000..f148022b6b4e
--- /dev/null
+++ b/drivers/serial/68360serial.c
@@ -0,0 +1,3027 @@
1/*
2 * UART driver for 68360 CPM SCC or SMC
3 * Copyright (c) 2000 D. Jeff Dionne <jeff@uclinux.org>,
4 * Copyright (c) 2000 Michael Leslie <mleslie@lineo.ca>
5 * Copyright (c) 1997 Dan Malek <dmalek@jlc.net>
6 *
7 * I used the serial.c driver as the framework for this driver.
8 * Give credit to those guys.
9 * The original code was written for the MBX860 board. I tried to make
10 * it generic, but there may be some assumptions in the structures that
11 * have to be fixed later.
12 * To save porting time, I did not bother to change any object names
13 * that are not accessed outside of this file.
14 * It still needs lots of work........When it was easy, I included code
15 * to support the SCCs, but this has never been tested, nor is it complete.
16 * Only the SCCs support modem control, so that is not complete either.
17 *
18 * This module exports the following rs232 io functions:
19 *
20 * int rs_360_init(void);
21 */
22
23#include <linux/config.h>
24#include <linux/module.h>
25#include <linux/errno.h>
26#include <linux/signal.h>
27#include <linux/sched.h>
28#include <linux/timer.h>
29#include <linux/interrupt.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial.h>
33#include <linux/serialP.h>
34#include <linux/major.h>
35#include <linux/string.h>
36#include <linux/fcntl.h>
37#include <linux/ptrace.h>
38#include <linux/mm.h>
39#include <linux/init.h>
40#include <linux/delay.h>
41#include <asm/irq.h>
42#include <asm/m68360.h>
43#include <asm/commproc.h>
44
45
46#ifdef CONFIG_KGDB
47extern void breakpoint(void);
48extern void set_debug_traps(void);
49extern int kgdb_output_string (const char* s, unsigned int count);
50#endif
51
52
53/* #ifdef CONFIG_SERIAL_CONSOLE */ /* This seems to be a post 2.0 thing - mles */
54#include <linux/console.h>
55
56/* this defines the index into rs_table for the port to use
57 */
58#ifndef CONFIG_SERIAL_CONSOLE_PORT
59#define CONFIG_SERIAL_CONSOLE_PORT 1 /* ie SMC2 - note USE_SMC2 must be defined */
60#endif
61/* #endif */
62
63#if 0
64/* SCC2 for console
65 */
66#undef CONFIG_SERIAL_CONSOLE_PORT
67#define CONFIG_SERIAL_CONSOLE_PORT 2
68#endif
69
70
71#define TX_WAKEUP ASYNC_SHARE_IRQ
72
73static char *serial_name = "CPM UART driver";
74static char *serial_version = "0.03";
75
76static struct tty_driver *serial_driver;
77int serial_console_setup(struct console *co, char *options);
78
79/*
80 * Serial driver configuration section. Here are the various options:
81 */
82#define SERIAL_PARANOIA_CHECK
83#define CONFIG_SERIAL_NOPAUSE_IO
84#define SERIAL_DO_RESTART
85
86/* Set of debugging defines */
87
88#undef SERIAL_DEBUG_INTR
89#undef SERIAL_DEBUG_OPEN
90#undef SERIAL_DEBUG_FLOW
91#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
92
93#define _INLINE_ inline
94
95#define DBG_CNT(s)
96
97/* We overload some of the items in the data structure to meet our
98 * needs. For example, the port address is the CPM parameter ram
99 * offset for the SCC or SMC. The maximum number of ports is 4 SCCs and
100 * 2 SMCs. The "hub6" field is used to indicate the channel number, with
101 * a flag indicating SCC or SMC, and the number is used as an index into
102 * the CPM parameter area for this device.
103 * The "type" field is currently set to 0, for PORT_UNKNOWN. It is
104 * not currently used. I should probably use it to indicate the port
105 * type of SMC or SCC.
106 * The SMCs do not support any modem control signals.
107 */
108#define smc_scc_num hub6
109#define NUM_IS_SCC ((int)0x00010000)
110#define PORT_NUM(P) ((P) & 0x0000ffff)
111
112
113#if defined (CONFIG_UCQUICC)
114
115volatile extern void *_periph_base;
116/* sipex transceiver
117 * mode bits for are on pins
118 *
119 * SCC2 d16..19
120 * SCC3 d20..23
121 * SCC4 d24..27
122 */
123#define SIPEX_MODE(n,m) ((m & 0x0f)<<(16+4*(n-1)))
124
125static uint sipex_mode_bits = 0x00000000;
126
127#endif
128
129/* There is no `serial_state' defined back here in 2.0.
130 * Try to get by with serial_struct
131 */
132/* #define serial_state serial_struct */
133
134/* 2.4 -> 2.0 portability problem: async_icount in 2.4 has a few
135 * extras: */
136
137#if 0
138struct async_icount_24 {
139 __u32 cts, dsr, rng, dcd, tx, rx;
140 __u32 frame, parity, overrun, brk;
141 __u32 buf_overrun;
142} icount;
143#endif
144
145#if 0
146
147struct serial_state {
148 int magic;
149 int baud_base;
150 unsigned long port;
151 int irq;
152 int flags;
153 int hub6;
154 int type;
155 int line;
156 int revision; /* Chip revision (950) */
157 int xmit_fifo_size;
158 int custom_divisor;
159 int count;
160 u8 *iomem_base;
161 u16 iomem_reg_shift;
162 unsigned short close_delay;
163 unsigned short closing_wait; /* time to wait before closing */
164 struct async_icount_24 icount;
165 int io_type;
166 struct async_struct *info;
167};
168#endif
169
170#define SSTATE_MAGIC 0x5302
171
172
173
174/* SMC2 is sometimes used for low performance TDM interfaces. Define
175 * this as 1 if you want SMC2 as a serial port UART managed by this driver.
176 * Define this as 0 if you wish to use SMC2 for something else.
177 */
178#define USE_SMC2 1
179
180#if 0
181/* Define SCC to ttySx mapping. */
182#define SCC_NUM_BASE (USE_SMC2 + 1) /* SCC base tty "number" */
183
184/* Define which SCC is the first one to use for a serial port. These
185 * are 0-based numbers, i.e. this assumes the first SCC (SCC1) is used
186 * for Ethernet, and the first available SCC for serial UART is SCC2.
187 * NOTE: IF YOU CHANGE THIS, you have to change the PROFF_xxx and
188 * interrupt vectors in the table below to match.
189 */
190#define SCC_IDX_BASE 1 /* table index */
191#endif
192
193
194/* Processors other than the 860 only get SMCs configured by default.
195 * Either they don't have SCCs or they are allocated somewhere else.
196 * Of course, there are now 860s without some SCCs, so we will need to
197 * address that someday.
198 * The Embedded Planet Multimedia I/O cards use TDM interfaces to the
199 * stereo codec parts, and we use SMC2 to help support that.
200 */
201static struct serial_state rs_table[] = {
202/* type line PORT IRQ FLAGS smc_scc_num (F.K.A. hub6) */
203 { 0, 0, PRSLOT_SMC1, CPMVEC_SMC1, 0, 0 } /* SMC1 ttyS0 */
204#if USE_SMC2
205 ,{ 0, 0, PRSLOT_SMC2, CPMVEC_SMC2, 0, 1 } /* SMC2 ttyS1 */
206#endif
207
208#if defined(CONFIG_SERIAL_68360_SCC)
209 ,{ 0, 0, PRSLOT_SCC2, CPMVEC_SCC2, 0, (NUM_IS_SCC | 1) } /* SCC2 ttyS2 */
210 ,{ 0, 0, PRSLOT_SCC3, CPMVEC_SCC3, 0, (NUM_IS_SCC | 2) } /* SCC3 ttyS3 */
211 ,{ 0, 0, PRSLOT_SCC4, CPMVEC_SCC4, 0, (NUM_IS_SCC | 3) } /* SCC4 ttyS4 */
212#endif
213};
214
215#define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
216
217/* The number of buffer descriptors and their sizes.
218 */
219#define RX_NUM_FIFO 4
220#define RX_BUF_SIZE 32
221#define TX_NUM_FIFO 4
222#define TX_BUF_SIZE 32
223
224#define CONSOLE_NUM_FIFO 2
225#define CONSOLE_BUF_SIZE 4
226
227char *console_fifos[CONSOLE_NUM_FIFO * CONSOLE_BUF_SIZE];
228
229/* The async_struct in serial.h does not really give us what we
230 * need, so define our own here.
231 */
232typedef struct serial_info {
233 int magic;
234 int flags;
235
236 struct serial_state *state;
237 /* struct serial_struct *state; */
238 /* struct async_struct *state; */
239
240 struct tty_struct *tty;
241 int read_status_mask;
242 int ignore_status_mask;
243 int timeout;
244 int line;
245 int x_char; /* xon/xoff character */
246 int close_delay;
247 unsigned short closing_wait;
248 unsigned short closing_wait2;
249 unsigned long event;
250 unsigned long last_active;
251 int blocked_open; /* # of blocked opens */
252 struct work_struct tqueue;
253 struct work_struct tqueue_hangup;
254 wait_queue_head_t open_wait;
255 wait_queue_head_t close_wait;
256
257
258/* CPM Buffer Descriptor pointers.
259 */
260 QUICC_BD *rx_bd_base;
261 QUICC_BD *rx_cur;
262 QUICC_BD *tx_bd_base;
263 QUICC_BD *tx_cur;
264} ser_info_t;
265
266
267/* since kmalloc_init() does not get called until much after this initialization: */
268static ser_info_t quicc_ser_info[NR_PORTS];
269static char rx_buf_pool[NR_PORTS * RX_NUM_FIFO * RX_BUF_SIZE];
270static char tx_buf_pool[NR_PORTS * TX_NUM_FIFO * TX_BUF_SIZE];
271
272static void change_speed(ser_info_t *info);
273static void rs_360_wait_until_sent(struct tty_struct *tty, int timeout);
274
275static inline int serial_paranoia_check(ser_info_t *info,
276 char *name, const char *routine)
277{
278#ifdef SERIAL_PARANOIA_CHECK
279 static const char *badmagic =
280 "Warning: bad magic number for serial struct (%s) in %s\n";
281 static const char *badinfo =
282 "Warning: null async_struct for (%s) in %s\n";
283
284 if (!info) {
285 printk(badinfo, name, routine);
286 return 1;
287 }
288 if (info->magic != SERIAL_MAGIC) {
289 printk(badmagic, name, routine);
290 return 1;
291 }
292#endif
293 return 0;
294}
295
296/*
297 * This is used to figure out the divisor speeds and the timeouts,
298 * indexed by the termio value. The generic CPM functions are responsible
299 * for setting and assigning baud rate generators for us.
300 */
301static int baud_table[] = {
302 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
303 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
304
305/* This sucks. There is a better way: */
306#if defined(CONFIG_CONSOLE_9600)
307 #define CONSOLE_BAUDRATE 9600
308#elif defined(CONFIG_CONSOLE_19200)
309 #define CONSOLE_BAUDRATE 19200
310#elif defined(CONFIG_CONSOLE_115200)
311 #define CONSOLE_BAUDRATE 115200
312#else
313 #warning "console baud rate undefined"
314 #define CONSOLE_BAUDRATE 9600
315#endif
316
317/*
318 * ------------------------------------------------------------
319 * rs_stop() and rs_start()
320 *
321 * This routines are called before setting or resetting tty->stopped.
322 * They enable or disable transmitter interrupts, as necessary.
323 * ------------------------------------------------------------
324 */
325static void rs_360_stop(struct tty_struct *tty)
326{
327 ser_info_t *info = (ser_info_t *)tty->driver_data;
328 int idx;
329 unsigned long flags;
330 volatile struct scc_regs *sccp;
331 volatile struct smc_regs *smcp;
332
333 if (serial_paranoia_check(info, tty->name, "rs_stop"))
334 return;
335
336 local_irq_save(flags);
337 idx = PORT_NUM(info->state->smc_scc_num);
338 if (info->state->smc_scc_num & NUM_IS_SCC) {
339 sccp = &pquicc->scc_regs[idx];
340 sccp->scc_sccm &= ~UART_SCCM_TX;
341 } else {
342 /* smcp = &cpmp->cp_smc[idx]; */
343 smcp = &pquicc->smc_regs[idx];
344 smcp->smc_smcm &= ~SMCM_TX;
345 }
346 local_irq_restore(flags);
347}
348
349
350static void rs_360_start(struct tty_struct *tty)
351{
352 ser_info_t *info = (ser_info_t *)tty->driver_data;
353 int idx;
354 unsigned long flags;
355 volatile struct scc_regs *sccp;
356 volatile struct smc_regs *smcp;
357
358 if (serial_paranoia_check(info, tty->name, "rs_stop"))
359 return;
360
361 local_irq_save(flags);
362 idx = PORT_NUM(info->state->smc_scc_num);
363 if (info->state->smc_scc_num & NUM_IS_SCC) {
364 sccp = &pquicc->scc_regs[idx];
365 sccp->scc_sccm |= UART_SCCM_TX;
366 } else {
367 smcp = &pquicc->smc_regs[idx];
368 smcp->smc_smcm |= SMCM_TX;
369 }
370 local_irq_restore(flags);
371}
372
373/*
374 * ----------------------------------------------------------------------
375 *
376 * Here starts the interrupt handling routines. All of the following
377 * subroutines are declared as inline and are folded into
378 * rs_interrupt(). They were separated out for readability's sake.
379 *
380 * Note: rs_interrupt() is a "fast" interrupt, which means that it
381 * runs with interrupts turned off. People who may want to modify
382 * rs_interrupt() should try to keep the interrupt handler as fast as
383 * possible. After you are done making modifications, it is not a bad
384 * idea to do:
385 *
386 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
387 *
388 * and look at the resulting assemble code in serial.s.
389 *
390 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
391 * -----------------------------------------------------------------------
392 */
393
394static _INLINE_ void receive_chars(ser_info_t *info)
395{
396 struct tty_struct *tty = info->tty;
397 unsigned char ch, *cp;
398 /*int ignored = 0;*/
399 int i;
400 ushort status;
401 struct async_icount *icount;
402 /* struct async_icount_24 *icount; */
403 volatile QUICC_BD *bdp;
404
405 icount = &info->state->icount;
406
407 /* Just loop through the closed BDs and copy the characters into
408 * the buffer.
409 */
410 bdp = info->rx_cur;
411 for (;;) {
412 if (bdp->status & BD_SC_EMPTY) /* If this one is empty */
413 break; /* we are all done */
414
415 /* The read status mask tell us what we should do with
416 * incoming characters, especially if errors occur.
417 * One special case is the use of BD_SC_EMPTY. If
418 * this is not set, we are supposed to be ignoring
419 * inputs. In this case, just mark the buffer empty and
420 * continue.
421 */
422 if (!(info->read_status_mask & BD_SC_EMPTY)) {
423 bdp->status |= BD_SC_EMPTY;
424 bdp->status &=
425 ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
426
427 if (bdp->status & BD_SC_WRAP)
428 bdp = info->rx_bd_base;
429 else
430 bdp++;
431 continue;
432 }
433
434 /* Get the number of characters and the buffer pointer.
435 */
436 i = bdp->length;
437 /* cp = (unsigned char *)__va(bdp->buf); */
438 cp = (char *)bdp->buf;
439 status = bdp->status;
440
441 /* Check to see if there is room in the tty buffer for
442 * the characters in our BD buffer. If not, we exit
443 * now, leaving the BD with the characters. We'll pick
444 * them up again on the next receive interrupt (which could
445 * be a timeout).
446 */
447 if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE)
448 break;
449
450 while (i-- > 0) {
451 ch = *cp++;
452 *tty->flip.char_buf_ptr = ch;
453 icount->rx++;
454
455#ifdef SERIAL_DEBUG_INTR
456 printk("DR%02x:%02x...", ch, status);
457#endif
458 *tty->flip.flag_buf_ptr = 0;
459 if (status & (BD_SC_BR | BD_SC_FR |
460 BD_SC_PR | BD_SC_OV)) {
461 /*
462 * For statistics only
463 */
464 if (status & BD_SC_BR)
465 icount->brk++;
466 else if (status & BD_SC_PR)
467 icount->parity++;
468 else if (status & BD_SC_FR)
469 icount->frame++;
470 if (status & BD_SC_OV)
471 icount->overrun++;
472
473 /*
474 * Now check to see if character should be
475 * ignored, and mask off conditions which
476 * should be ignored.
477 if (status & info->ignore_status_mask) {
478 if (++ignored > 100)
479 break;
480 continue;
481 }
482 */
483 status &= info->read_status_mask;
484
485 if (status & (BD_SC_BR)) {
486#ifdef SERIAL_DEBUG_INTR
487 printk("handling break....");
488#endif
489 *tty->flip.flag_buf_ptr = TTY_BREAK;
490 if (info->flags & ASYNC_SAK)
491 do_SAK(tty);
492 } else if (status & BD_SC_PR)
493 *tty->flip.flag_buf_ptr = TTY_PARITY;
494 else if (status & BD_SC_FR)
495 *tty->flip.flag_buf_ptr = TTY_FRAME;
496 if (status & BD_SC_OV) {
497 /*
498 * Overrun is special, since it's
499 * reported immediately, and doesn't
500 * affect the current character
501 */
502 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
503 tty->flip.count++;
504 tty->flip.flag_buf_ptr++;
505 tty->flip.char_buf_ptr++;
506 *tty->flip.flag_buf_ptr =
507 TTY_OVERRUN;
508 }
509 }
510 }
511 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
512 break;
513
514 tty->flip.flag_buf_ptr++;
515 tty->flip.char_buf_ptr++;
516 tty->flip.count++;
517 }
518
519 /* This BD is ready to be used again. Clear status.
520 * Get next BD.
521 */
522 bdp->status |= BD_SC_EMPTY;
523 bdp->status &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
524
525 if (bdp->status & BD_SC_WRAP)
526 bdp = info->rx_bd_base;
527 else
528 bdp++;
529 }
530
531 info->rx_cur = (QUICC_BD *)bdp;
532
533 schedule_work(&tty->flip.work);
534}
535
536static _INLINE_ void receive_break(ser_info_t *info)
537{
538 struct tty_struct *tty = info->tty;
539
540 info->state->icount.brk++;
541 /* Check to see if there is room in the tty buffer for
542 * the break. If not, we exit now, losing the break. FIXME
543 */
544 if ((tty->flip.count + 1) >= TTY_FLIPBUF_SIZE)
545 return;
546 *(tty->flip.flag_buf_ptr++) = TTY_BREAK;
547 *(tty->flip.char_buf_ptr++) = 0;
548 tty->flip.count++;
549
550 schedule_work(&tty->flip.work);
551}
552
553static _INLINE_ void transmit_chars(ser_info_t *info)
554{
555
556 if ((info->flags & TX_WAKEUP) ||
557 (info->tty->flags & (1 << TTY_DO_WRITE_WAKEUP))) {
558 schedule_work(&info->tqueue);
559 }
560
561#ifdef SERIAL_DEBUG_INTR
562 printk("THRE...");
563#endif
564}
565
566#ifdef notdef
567 /* I need to do this for the SCCs, so it is left as a reminder.
568 */
569static _INLINE_ void check_modem_status(struct async_struct *info)
570{
571 int status;
572 /* struct async_icount *icount; */
573 struct async_icount_24 *icount;
574
575 status = serial_in(info, UART_MSR);
576
577 if (status & UART_MSR_ANY_DELTA) {
578 icount = &info->state->icount;
579 /* update input line counters */
580 if (status & UART_MSR_TERI)
581 icount->rng++;
582 if (status & UART_MSR_DDSR)
583 icount->dsr++;
584 if (status & UART_MSR_DDCD) {
585 icount->dcd++;
586#ifdef CONFIG_HARD_PPS
587 if ((info->flags & ASYNC_HARDPPS_CD) &&
588 (status & UART_MSR_DCD))
589 hardpps();
590#endif
591 }
592 if (status & UART_MSR_DCTS)
593 icount->cts++;
594 wake_up_interruptible(&info->delta_msr_wait);
595 }
596
597 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
598#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
599 printk("ttys%d CD now %s...", info->line,
600 (status & UART_MSR_DCD) ? "on" : "off");
601#endif
602 if (status & UART_MSR_DCD)
603 wake_up_interruptible(&info->open_wait);
604 else {
605#ifdef SERIAL_DEBUG_OPEN
606 printk("scheduling hangup...");
607#endif
608 queue_task(&info->tqueue_hangup,
609 &tq_scheduler);
610 }
611 }
612 if (info->flags & ASYNC_CTS_FLOW) {
613 if (info->tty->hw_stopped) {
614 if (status & UART_MSR_CTS) {
615#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
616 printk("CTS tx start...");
617#endif
618 info->tty->hw_stopped = 0;
619 info->IER |= UART_IER_THRI;
620 serial_out(info, UART_IER, info->IER);
621 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
622 return;
623 }
624 } else {
625 if (!(status & UART_MSR_CTS)) {
626#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
627 printk("CTS tx stop...");
628#endif
629 info->tty->hw_stopped = 1;
630 info->IER &= ~UART_IER_THRI;
631 serial_out(info, UART_IER, info->IER);
632 }
633 }
634 }
635}
636#endif
637
638/*
639 * This is the serial driver's interrupt routine for a single port
640 */
641/* static void rs_360_interrupt(void *dev_id) */ /* until and if we start servicing irqs here */
642static void rs_360_interrupt(int vec, void *dev_id, struct pt_regs *fp)
643{
644 u_char events;
645 int idx;
646 ser_info_t *info;
647 volatile struct smc_regs *smcp;
648 volatile struct scc_regs *sccp;
649
650 info = (ser_info_t *)dev_id;
651
652 idx = PORT_NUM(info->state->smc_scc_num);
653 if (info->state->smc_scc_num & NUM_IS_SCC) {
654 sccp = &pquicc->scc_regs[idx];
655 events = sccp->scc_scce;
656 if (events & SCCM_RX)
657 receive_chars(info);
658 if (events & SCCM_TX)
659 transmit_chars(info);
660 sccp->scc_scce = events;
661 } else {
662 smcp = &pquicc->smc_regs[idx];
663 events = smcp->smc_smce;
664 if (events & SMCM_BRKE)
665 receive_break(info);
666 if (events & SMCM_RX)
667 receive_chars(info);
668 if (events & SMCM_TX)
669 transmit_chars(info);
670 smcp->smc_smce = events;
671 }
672
673#ifdef SERIAL_DEBUG_INTR
674 printk("rs_interrupt_single(%d, %x)...",
675 info->state->smc_scc_num, events);
676#endif
677#ifdef modem_control
678 check_modem_status(info);
679#endif
680 info->last_active = jiffies;
681#ifdef SERIAL_DEBUG_INTR
682 printk("end.\n");
683#endif
684}
685
686
687/*
688 * -------------------------------------------------------------------
689 * Here ends the serial interrupt routines.
690 * -------------------------------------------------------------------
691 */
692
693
694static void do_softint(void *private_)
695{
696 ser_info_t *info = (ser_info_t *) private_;
697 struct tty_struct *tty;
698
699 tty = info->tty;
700 if (!tty)
701 return;
702
703 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
704 tty_wakeup(tty);
705}
706
707
708/*
709 * This routine is called from the scheduler tqueue when the interrupt
710 * routine has signalled that a hangup has occurred. The path of
711 * hangup processing is:
712 *
713 * serial interrupt routine -> (scheduler tqueue) ->
714 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
715 *
716 */
717static void do_serial_hangup(void *private_)
718{
719 struct async_struct *info = (struct async_struct *) private_;
720 struct tty_struct *tty;
721
722 tty = info->tty;
723 if (!tty)
724 return;
725
726 tty_hangup(tty);
727}
728
729
730static int startup(ser_info_t *info)
731{
732 unsigned long flags;
733 int retval=0;
734 int idx;
735 /*struct serial_state *state = info->state;*/
736 volatile struct smc_regs *smcp;
737 volatile struct scc_regs *sccp;
738 volatile struct smc_uart_pram *up;
739 volatile struct uart_pram *scup;
740
741
742 local_irq_save(flags);
743
744 if (info->flags & ASYNC_INITIALIZED) {
745 goto errout;
746 }
747
748#ifdef maybe
749 if (!state->port || !state->type) {
750 if (info->tty)
751 set_bit(TTY_IO_ERROR, &info->tty->flags);
752 goto errout;
753 }
754#endif
755
756#ifdef SERIAL_DEBUG_OPEN
757 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
758#endif
759
760
761#ifdef modem_control
762 info->MCR = 0;
763 if (info->tty->termios->c_cflag & CBAUD)
764 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
765#endif
766
767 if (info->tty)
768 clear_bit(TTY_IO_ERROR, &info->tty->flags);
769
770 /*
771 * and set the speed of the serial port
772 */
773 change_speed(info);
774
775 idx = PORT_NUM(info->state->smc_scc_num);
776 if (info->state->smc_scc_num & NUM_IS_SCC) {
777 sccp = &pquicc->scc_regs[idx];
778 scup = &pquicc->pram[info->state->port].scc.pscc.u;
779
780 scup->mrblr = RX_BUF_SIZE;
781 scup->max_idl = RX_BUF_SIZE;
782
783 sccp->scc_sccm |= (UART_SCCM_TX | UART_SCCM_RX);
784 sccp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
785
786 } else {
787 smcp = &pquicc->smc_regs[idx];
788
789 /* Enable interrupts and I/O.
790 */
791 smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
792 smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
793
794 /* We can tune the buffer length and idle characters
795 * to take advantage of the entire incoming buffer size.
796 * If mrblr is something other than 1, maxidl has to be
797 * non-zero or we never get an interrupt. The maxidl
798 * is the number of character times we wait after reception
799 * of the last character before we decide no more characters
800 * are coming.
801 */
802 /* up = (smc_uart_t *)&pquicc->cp_dparam[state->port]; */
803 /* holy unionized structures, Batman: */
804 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
805
806 up->mrblr = RX_BUF_SIZE;
807 up->max_idl = RX_BUF_SIZE;
808
809 up->brkcr = 1; /* number of break chars */
810 }
811
812 info->flags |= ASYNC_INITIALIZED;
813 local_irq_restore(flags);
814 return 0;
815
816errout:
817 local_irq_restore(flags);
818 return retval;
819}
820
821/*
822 * This routine will shutdown a serial port; interrupts are disabled, and
823 * DTR is dropped if the hangup on close termio flag is on.
824 */
825static void shutdown(ser_info_t *info)
826{
827 unsigned long flags;
828 struct serial_state *state;
829 int idx;
830 volatile struct smc_regs *smcp;
831 volatile struct scc_regs *sccp;
832
833 if (!(info->flags & ASYNC_INITIALIZED))
834 return;
835
836 state = info->state;
837
838#ifdef SERIAL_DEBUG_OPEN
839 printk("Shutting down serial port %d (irq %d)....", info->line,
840 state->irq);
841#endif
842
843 local_irq_save(flags);
844
845 idx = PORT_NUM(state->smc_scc_num);
846 if (state->smc_scc_num & NUM_IS_SCC) {
847 sccp = &pquicc->scc_regs[idx];
848 sccp->scc_gsmr.w.low &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
849#ifdef CONFIG_SERIAL_CONSOLE
850 /* We can't disable the transmitter if this is the
851 * system console.
852 */
853 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
854#endif
855 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
856 } else {
857 smcp = &pquicc->smc_regs[idx];
858
859 /* Disable interrupts and I/O.
860 */
861 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
862#ifdef CONFIG_SERIAL_CONSOLE
863 /* We can't disable the transmitter if this is the
864 * system console.
865 */
866 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
867#endif
868 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
869 }
870
871 if (info->tty)
872 set_bit(TTY_IO_ERROR, &info->tty->flags);
873
874 info->flags &= ~ASYNC_INITIALIZED;
875 local_irq_restore(flags);
876}
877
878/*
879 * This routine is called to set the UART divisor registers to match
880 * the specified baud rate for a serial port.
881 */
882static void change_speed(ser_info_t *info)
883{
884 int baud_rate;
885 unsigned cflag, cval, scval, prev_mode;
886 int i, bits, sbits, idx;
887 unsigned long flags;
888 struct serial_state *state;
889 volatile struct smc_regs *smcp;
890 volatile struct scc_regs *sccp;
891
892 if (!info->tty || !info->tty->termios)
893 return;
894 cflag = info->tty->termios->c_cflag;
895
896 state = info->state;
897
898 /* Character length programmed into the mode register is the
899 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
900 * 1 or 2 stop bits, minus 1.
901 * The value 'bits' counts this for us.
902 */
903 cval = 0;
904 scval = 0;
905
906 /* byte size and parity */
907 switch (cflag & CSIZE) {
908 case CS5: bits = 5; break;
909 case CS6: bits = 6; break;
910 case CS7: bits = 7; break;
911 case CS8: bits = 8; break;
912 /* Never happens, but GCC is too dumb to figure it out */
913 default: bits = 8; break;
914 }
915 sbits = bits - 5;
916
917 if (cflag & CSTOPB) {
918 cval |= SMCMR_SL; /* Two stops */
919 scval |= SCU_PMSR_SL;
920 bits++;
921 }
922 if (cflag & PARENB) {
923 cval |= SMCMR_PEN;
924 scval |= SCU_PMSR_PEN;
925 bits++;
926 }
927 if (!(cflag & PARODD)) {
928 cval |= SMCMR_PM_EVEN;
929 scval |= (SCU_PMSR_REVP | SCU_PMSR_TEVP);
930 }
931
932 /* Determine divisor based on baud rate */
933 i = cflag & CBAUD;
934 if (i >= (sizeof(baud_table)/sizeof(int)))
935 baud_rate = 9600;
936 else
937 baud_rate = baud_table[i];
938
939 info->timeout = (TX_BUF_SIZE*HZ*bits);
940 info->timeout += HZ/50; /* Add .02 seconds of slop */
941
942#ifdef modem_control
943 /* CTS flow control flag and modem status interrupts */
944 info->IER &= ~UART_IER_MSI;
945 if (info->flags & ASYNC_HARDPPS_CD)
946 info->IER |= UART_IER_MSI;
947 if (cflag & CRTSCTS) {
948 info->flags |= ASYNC_CTS_FLOW;
949 info->IER |= UART_IER_MSI;
950 } else
951 info->flags &= ~ASYNC_CTS_FLOW;
952 if (cflag & CLOCAL)
953 info->flags &= ~ASYNC_CHECK_CD;
954 else {
955 info->flags |= ASYNC_CHECK_CD;
956 info->IER |= UART_IER_MSI;
957 }
958 serial_out(info, UART_IER, info->IER);
959#endif
960
961 /*
962 * Set up parity check flag
963 */
964#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
965
966 info->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
967 if (I_INPCK(info->tty))
968 info->read_status_mask |= BD_SC_FR | BD_SC_PR;
969 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
970 info->read_status_mask |= BD_SC_BR;
971
972 /*
973 * Characters to ignore
974 */
975 info->ignore_status_mask = 0;
976 if (I_IGNPAR(info->tty))
977 info->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
978 if (I_IGNBRK(info->tty)) {
979 info->ignore_status_mask |= BD_SC_BR;
980 /*
981 * If we're ignore parity and break indicators, ignore
982 * overruns too. (For real raw support).
983 */
984 if (I_IGNPAR(info->tty))
985 info->ignore_status_mask |= BD_SC_OV;
986 }
987 /*
988 * !!! ignore all characters if CREAD is not set
989 */
990 if ((cflag & CREAD) == 0)
991 info->read_status_mask &= ~BD_SC_EMPTY;
992 local_irq_save(flags);
993
994 /* Start bit has not been added (so don't, because we would just
995 * subtract it later), and we need to add one for the number of
996 * stops bits (there is always at least one).
997 */
998 bits++;
999 idx = PORT_NUM(state->smc_scc_num);
1000 if (state->smc_scc_num & NUM_IS_SCC) {
1001 sccp = &pquicc->scc_regs[idx];
1002 sccp->scc_psmr = (sbits << 12) | scval;
1003 } else {
1004 smcp = &pquicc->smc_regs[idx];
1005
1006 /* Set the mode register. We want to keep a copy of the
1007 * enables, because we want to put them back if they were
1008 * present.
1009 */
1010 prev_mode = smcp->smc_smcmr;
1011 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
1012 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
1013 }
1014
1015 m360_cpm_setbrg((state - rs_table), baud_rate);
1016
1017 local_irq_restore(flags);
1018}
1019
1020static void rs_360_put_char(struct tty_struct *tty, unsigned char ch)
1021{
1022 ser_info_t *info = (ser_info_t *)tty->driver_data;
1023 volatile QUICC_BD *bdp;
1024
1025 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1026 return;
1027
1028 if (!tty)
1029 return;
1030
1031 bdp = info->tx_cur;
1032 while (bdp->status & BD_SC_READY);
1033
1034 /* *((char *)__va(bdp->buf)) = ch; */
1035 *((char *)bdp->buf) = ch;
1036 bdp->length = 1;
1037 bdp->status |= BD_SC_READY;
1038
1039 /* Get next BD.
1040 */
1041 if (bdp->status & BD_SC_WRAP)
1042 bdp = info->tx_bd_base;
1043 else
1044 bdp++;
1045
1046 info->tx_cur = (QUICC_BD *)bdp;
1047
1048}
1049
1050static int rs_360_write(struct tty_struct * tty,
1051 const unsigned char *buf, int count)
1052{
1053 int c, ret = 0;
1054 ser_info_t *info = (ser_info_t *)tty->driver_data;
1055 volatile QUICC_BD *bdp;
1056
1057#ifdef CONFIG_KGDB
1058 /* Try to let stub handle output. Returns true if it did. */
1059 if (kgdb_output_string(buf, count))
1060 return ret;
1061#endif
1062
1063 if (serial_paranoia_check(info, tty->name, "rs_write"))
1064 return 0;
1065
1066 if (!tty)
1067 return 0;
1068
1069 bdp = info->tx_cur;
1070
1071 while (1) {
1072 c = min(count, TX_BUF_SIZE);
1073
1074 if (c <= 0)
1075 break;
1076
1077 if (bdp->status & BD_SC_READY) {
1078 info->flags |= TX_WAKEUP;
1079 break;
1080 }
1081
1082 /* memcpy(__va(bdp->buf), buf, c); */
1083 memcpy((void *)bdp->buf, buf, c);
1084
1085 bdp->length = c;
1086 bdp->status |= BD_SC_READY;
1087
1088 buf += c;
1089 count -= c;
1090 ret += c;
1091
1092 /* Get next BD.
1093 */
1094 if (bdp->status & BD_SC_WRAP)
1095 bdp = info->tx_bd_base;
1096 else
1097 bdp++;
1098 info->tx_cur = (QUICC_BD *)bdp;
1099 }
1100 return ret;
1101}
1102
1103static int rs_360_write_room(struct tty_struct *tty)
1104{
1105 ser_info_t *info = (ser_info_t *)tty->driver_data;
1106 int ret;
1107
1108 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1109 return 0;
1110
1111 if ((info->tx_cur->status & BD_SC_READY) == 0) {
1112 info->flags &= ~TX_WAKEUP;
1113 ret = TX_BUF_SIZE;
1114 }
1115 else {
1116 info->flags |= TX_WAKEUP;
1117 ret = 0;
1118 }
1119 return ret;
1120}
1121
1122/* I could track this with transmit counters....maybe later.
1123*/
1124static int rs_360_chars_in_buffer(struct tty_struct *tty)
1125{
1126 ser_info_t *info = (ser_info_t *)tty->driver_data;
1127
1128 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1129 return 0;
1130 return 0;
1131}
1132
1133static void rs_360_flush_buffer(struct tty_struct *tty)
1134{
1135 ser_info_t *info = (ser_info_t *)tty->driver_data;
1136
1137 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1138 return;
1139
1140 /* There is nothing to "flush", whatever we gave the CPM
1141 * is on its way out.
1142 */
1143 tty_wakeup(tty);
1144 info->flags &= ~TX_WAKEUP;
1145}
1146
1147/*
1148 * This function is used to send a high-priority XON/XOFF character to
1149 * the device
1150 */
1151static void rs_360_send_xchar(struct tty_struct *tty, char ch)
1152{
1153 volatile QUICC_BD *bdp;
1154
1155 ser_info_t *info = (ser_info_t *)tty->driver_data;
1156
1157 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
1158 return;
1159
1160 bdp = info->tx_cur;
1161 while (bdp->status & BD_SC_READY);
1162
1163 /* *((char *)__va(bdp->buf)) = ch; */
1164 *((char *)bdp->buf) = ch;
1165 bdp->length = 1;
1166 bdp->status |= BD_SC_READY;
1167
1168 /* Get next BD.
1169 */
1170 if (bdp->status & BD_SC_WRAP)
1171 bdp = info->tx_bd_base;
1172 else
1173 bdp++;
1174
1175 info->tx_cur = (QUICC_BD *)bdp;
1176}
1177
1178/*
1179 * ------------------------------------------------------------
1180 * rs_throttle()
1181 *
1182 * This routine is called by the upper-layer tty layer to signal that
1183 * incoming characters should be throttled.
1184 * ------------------------------------------------------------
1185 */
1186static void rs_360_throttle(struct tty_struct * tty)
1187{
1188 ser_info_t *info = (ser_info_t *)tty->driver_data;
1189#ifdef SERIAL_DEBUG_THROTTLE
1190 char buf[64];
1191
1192 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1193 tty->ldisc.chars_in_buffer(tty));
1194#endif
1195
1196 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1197 return;
1198
1199 if (I_IXOFF(tty))
1200 rs_360_send_xchar(tty, STOP_CHAR(tty));
1201
1202#ifdef modem_control
1203 if (tty->termios->c_cflag & CRTSCTS)
1204 info->MCR &= ~UART_MCR_RTS;
1205
1206 local_irq_disable();
1207 serial_out(info, UART_MCR, info->MCR);
1208 local_irq_enable();
1209#endif
1210}
1211
1212static void rs_360_unthrottle(struct tty_struct * tty)
1213{
1214 ser_info_t *info = (ser_info_t *)tty->driver_data;
1215#ifdef SERIAL_DEBUG_THROTTLE
1216 char buf[64];
1217
1218 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1219 tty->ldisc.chars_in_buffer(tty));
1220#endif
1221
1222 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1223 return;
1224
1225 if (I_IXOFF(tty)) {
1226 if (info->x_char)
1227 info->x_char = 0;
1228 else
1229 rs_360_send_xchar(tty, START_CHAR(tty));
1230 }
1231#ifdef modem_control
1232 if (tty->termios->c_cflag & CRTSCTS)
1233 info->MCR |= UART_MCR_RTS;
1234 local_irq_disable();
1235 serial_out(info, UART_MCR, info->MCR);
1236 local_irq_enable();
1237#endif
1238}
1239
1240/*
1241 * ------------------------------------------------------------
1242 * rs_ioctl() and friends
1243 * ------------------------------------------------------------
1244 */
1245
1246#ifdef maybe
1247/*
1248 * get_lsr_info - get line status register info
1249 *
1250 * Purpose: Let user call ioctl() to get info when the UART physically
1251 * is emptied. On bus types like RS485, the transmitter must
1252 * release the bus after transmitting. This must be done when
1253 * the transmit shift register is empty, not be done when the
1254 * transmit holding register is empty. This functionality
1255 * allows an RS485 driver to be written in user space.
1256 */
1257static int get_lsr_info(struct async_struct * info, unsigned int *value)
1258{
1259 unsigned char status;
1260 unsigned int result;
1261
1262 local_irq_disable();
1263 status = serial_in(info, UART_LSR);
1264 local_irq_enable();
1265 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1266 return put_user(result,value);
1267}
1268#endif
1269
1270static int rs_360_tiocmget(struct tty_struct *tty, struct file *file)
1271{
1272 ser_info_t *info = (ser_info_t *)tty->driver_data;
1273 unsigned int result = 0;
1274#ifdef modem_control
1275 unsigned char control, status;
1276
1277 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1278 return -ENODEV;
1279
1280 if (tty->flags & (1 << TTY_IO_ERROR))
1281 return -EIO;
1282
1283 control = info->MCR;
1284 local_irq_disable();
1285 status = serial_in(info, UART_MSR);
1286 local_irq_enable();
1287 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1288 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1289#ifdef TIOCM_OUT1
1290 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1291 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1292#endif
1293 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1294 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1295 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1296 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1297#endif
1298 return result;
1299}
1300
1301static int rs_360_tiocmset(struct tty_struct *tty, struct file *file,
1302 unsigned int set, unsigned int clear)
1303{
1304#ifdef modem_control
1305 ser_info_t *info = (ser_info_t *)tty->driver_data;
1306 unsigned int arg;
1307
1308 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1309 return -ENODEV;
1310
1311 if (tty->flags & (1 << TTY_IO_ERROR))
1312 return -EIO;
1313
1314 if (set & TIOCM_RTS)
1315 info->mcr |= UART_MCR_RTS;
1316 if (set & TIOCM_DTR)
1317 info->mcr |= UART_MCR_DTR;
1318 if (clear & TIOCM_RTS)
1319 info->MCR &= ~UART_MCR_RTS;
1320 if (clear & TIOCM_DTR)
1321 info->MCR &= ~UART_MCR_DTR;
1322
1323#ifdef TIOCM_OUT1
1324 if (set & TIOCM_OUT1)
1325 info->MCR |= UART_MCR_OUT1;
1326 if (set & TIOCM_OUT2)
1327 info->MCR |= UART_MCR_OUT2;
1328 if (clear & TIOCM_OUT1)
1329 info->MCR &= ~UART_MCR_OUT1;
1330 if (clear & TIOCM_OUT2)
1331 info->MCR &= ~UART_MCR_OUT2;
1332#endif
1333
1334 local_irq_disable();
1335 serial_out(info, UART_MCR, info->MCR);
1336 local_irq_enable();
1337#endif
1338 return 0;
1339}
1340
1341/* Sending a break is a two step process on the SMC/SCC. It is accomplished
1342 * by sending a STOP TRANSMIT command followed by a RESTART TRANSMIT
1343 * command. We take advantage of the begin/end functions to make this
1344 * happen.
1345 */
1346static ushort smc_chan_map[] = {
1347 CPM_CR_CH_SMC1,
1348 CPM_CR_CH_SMC2
1349};
1350
1351static ushort scc_chan_map[] = {
1352 CPM_CR_CH_SCC1,
1353 CPM_CR_CH_SCC2,
1354 CPM_CR_CH_SCC3,
1355 CPM_CR_CH_SCC4
1356};
1357
1358static void begin_break(ser_info_t *info)
1359{
1360 volatile QUICC *cp;
1361 ushort chan;
1362 int idx;
1363
1364 cp = pquicc;
1365
1366 idx = PORT_NUM(info->state->smc_scc_num);
1367 if (info->state->smc_scc_num & NUM_IS_SCC)
1368 chan = scc_chan_map[idx];
1369 else
1370 chan = smc_chan_map[idx];
1371
1372 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG;
1373 while (cp->cp_cr & CPM_CR_FLG);
1374}
1375
1376static void end_break(ser_info_t *info)
1377{
1378 volatile QUICC *cp;
1379 ushort chan;
1380 int idx;
1381
1382 cp = pquicc;
1383
1384 idx = PORT_NUM(info->state->smc_scc_num);
1385 if (info->state->smc_scc_num & NUM_IS_SCC)
1386 chan = scc_chan_map[idx];
1387 else
1388 chan = smc_chan_map[idx];
1389
1390 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_RESTART_TX) | CPM_CR_FLG;
1391 while (cp->cp_cr & CPM_CR_FLG);
1392}
1393
1394/*
1395 * This routine sends a break character out the serial port.
1396 */
1397static void send_break(ser_info_t *info, int duration)
1398{
1399 set_current_state(TASK_INTERRUPTIBLE);
1400#ifdef SERIAL_DEBUG_SEND_BREAK
1401 printk("rs_send_break(%d) jiff=%lu...", duration, jiffies);
1402#endif
1403 begin_break(info);
1404 schedule_timeout(duration);
1405 end_break(info);
1406#ifdef SERIAL_DEBUG_SEND_BREAK
1407 printk("done jiffies=%lu\n", jiffies);
1408#endif
1409}
1410
1411
1412static int rs_360_ioctl(struct tty_struct *tty, struct file * file,
1413 unsigned int cmd, unsigned long arg)
1414{
1415 int error;
1416 ser_info_t *info = (ser_info_t *)tty->driver_data;
1417 int retval;
1418 struct async_icount cnow;
1419 /* struct async_icount_24 cnow;*/ /* kernel counter temps */
1420 struct serial_icounter_struct *p_cuser; /* user space */
1421
1422 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1423 return -ENODEV;
1424
1425 if ((cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1426 if (tty->flags & (1 << TTY_IO_ERROR))
1427 return -EIO;
1428 }
1429
1430 switch (cmd) {
1431 case TCSBRK: /* SVID version: non-zero arg --> no break */
1432 retval = tty_check_change(tty);
1433 if (retval)
1434 return retval;
1435 tty_wait_until_sent(tty, 0);
1436 if (signal_pending(current))
1437 return -EINTR;
1438 if (!arg) {
1439 send_break(info, HZ/4); /* 1/4 second */
1440 if (signal_pending(current))
1441 return -EINTR;
1442 }
1443 return 0;
1444 case TCSBRKP: /* support for POSIX tcsendbreak() */
1445 retval = tty_check_change(tty);
1446 if (retval)
1447 return retval;
1448 tty_wait_until_sent(tty, 0);
1449 if (signal_pending(current))
1450 return -EINTR;
1451 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1452 if (signal_pending(current))
1453 return -EINTR;
1454 return 0;
1455 case TIOCSBRK:
1456 retval = tty_check_change(tty);
1457 if (retval)
1458 return retval;
1459 tty_wait_until_sent(tty, 0);
1460 begin_break(info);
1461 return 0;
1462 case TIOCCBRK:
1463 retval = tty_check_change(tty);
1464 if (retval)
1465 return retval;
1466 end_break(info);
1467 return 0;
1468 case TIOCGSOFTCAR:
1469 /* return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg); */
1470 put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1471 return 0;
1472 case TIOCSSOFTCAR:
1473 error = get_user(arg, (unsigned int *) arg);
1474 if (error)
1475 return error;
1476 tty->termios->c_cflag =
1477 ((tty->termios->c_cflag & ~CLOCAL) |
1478 (arg ? CLOCAL : 0));
1479 return 0;
1480#ifdef maybe
1481 case TIOCSERGETLSR: /* Get line status register */
1482 return get_lsr_info(info, (unsigned int *) arg);
1483#endif
1484 /*
1485 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1486 * - mask passed in arg for lines of interest
1487 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1488 * Caller should use TIOCGICOUNT to see which one it was
1489 */
1490 case TIOCMIWAIT:
1491#ifdef modem_control
1492 local_irq_disable();
1493 /* note the counters on entry */
1494 cprev = info->state->icount;
1495 local_irq_enable();
1496 while (1) {
1497 interruptible_sleep_on(&info->delta_msr_wait);
1498 /* see if a signal did it */
1499 if (signal_pending(current))
1500 return -ERESTARTSYS;
1501 local_irq_disable();
1502 cnow = info->state->icount; /* atomic copy */
1503 local_irq_enable();
1504 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1505 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1506 return -EIO; /* no change => error */
1507 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1508 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1509 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1510 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1511 return 0;
1512 }
1513 cprev = cnow;
1514 }
1515 /* NOTREACHED */
1516#else
1517 return 0;
1518#endif
1519
1520 /*
1521 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1522 * Return: write counters to the user passed counter struct
1523 * NB: both 1->0 and 0->1 transitions are counted except for
1524 * RI where only 0->1 is counted.
1525 */
1526 case TIOCGICOUNT:
1527 local_irq_disable();
1528 cnow = info->state->icount;
1529 local_irq_enable();
1530 p_cuser = (struct serial_icounter_struct *) arg;
1531/* error = put_user(cnow.cts, &p_cuser->cts); */
1532/* if (error) return error; */
1533/* error = put_user(cnow.dsr, &p_cuser->dsr); */
1534/* if (error) return error; */
1535/* error = put_user(cnow.rng, &p_cuser->rng); */
1536/* if (error) return error; */
1537/* error = put_user(cnow.dcd, &p_cuser->dcd); */
1538/* if (error) return error; */
1539
1540 put_user(cnow.cts, &p_cuser->cts);
1541 put_user(cnow.dsr, &p_cuser->dsr);
1542 put_user(cnow.rng, &p_cuser->rng);
1543 put_user(cnow.dcd, &p_cuser->dcd);
1544 return 0;
1545
1546 default:
1547 return -ENOIOCTLCMD;
1548 }
1549 return 0;
1550}
1551
1552/* FIX UP modem control here someday......
1553*/
1554static void rs_360_set_termios(struct tty_struct *tty, struct termios *old_termios)
1555{
1556 ser_info_t *info = (ser_info_t *)tty->driver_data;
1557
1558 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1559 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1560 == RELEVANT_IFLAG(old_termios->c_iflag)))
1561 return;
1562
1563 change_speed(info);
1564
1565#ifdef modem_control
1566 /* Handle transition to B0 status */
1567 if ((old_termios->c_cflag & CBAUD) &&
1568 !(tty->termios->c_cflag & CBAUD)) {
1569 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1570 local_irq_disable();
1571 serial_out(info, UART_MCR, info->MCR);
1572 local_irq_enable();
1573 }
1574
1575 /* Handle transition away from B0 status */
1576 if (!(old_termios->c_cflag & CBAUD) &&
1577 (tty->termios->c_cflag & CBAUD)) {
1578 info->MCR |= UART_MCR_DTR;
1579 if (!tty->hw_stopped ||
1580 !(tty->termios->c_cflag & CRTSCTS)) {
1581 info->MCR |= UART_MCR_RTS;
1582 }
1583 local_irq_disable();
1584 serial_out(info, UART_MCR, info->MCR);
1585 local_irq_enable();
1586 }
1587
1588 /* Handle turning off CRTSCTS */
1589 if ((old_termios->c_cflag & CRTSCTS) &&
1590 !(tty->termios->c_cflag & CRTSCTS)) {
1591 tty->hw_stopped = 0;
1592 rs_360_start(tty);
1593 }
1594#endif
1595
1596#if 0
1597 /*
1598 * No need to wake up processes in open wait, since they
1599 * sample the CLOCAL flag once, and don't recheck it.
1600 * XXX It's not clear whether the current behavior is correct
1601 * or not. Hence, this may change.....
1602 */
1603 if (!(old_termios->c_cflag & CLOCAL) &&
1604 (tty->termios->c_cflag & CLOCAL))
1605 wake_up_interruptible(&info->open_wait);
1606#endif
1607}
1608
1609/*
1610 * ------------------------------------------------------------
1611 * rs_close()
1612 *
1613 * This routine is called when the serial port gets closed. First, we
1614 * wait for the last remaining data to be sent. Then, we unlink its
1615 * async structure from the interrupt chain if necessary, and we free
1616 * that IRQ if nothing is left in the chain.
1617 * ------------------------------------------------------------
1618 */
1619static void rs_360_close(struct tty_struct *tty, struct file * filp)
1620{
1621 ser_info_t *info = (ser_info_t *)tty->driver_data;
1622 /* struct async_state *state; */
1623 struct serial_state *state;
1624 unsigned long flags;
1625 int idx;
1626 volatile struct smc_regs *smcp;
1627 volatile struct scc_regs *sccp;
1628
1629 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1630 return;
1631
1632 state = info->state;
1633
1634 local_irq_save(flags);
1635
1636 if (tty_hung_up_p(filp)) {
1637 DBG_CNT("before DEC-hung");
1638 local_irq_restore(flags);
1639 return;
1640 }
1641
1642#ifdef SERIAL_DEBUG_OPEN
1643 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1644#endif
1645 if ((tty->count == 1) && (state->count != 1)) {
1646 /*
1647 * Uh, oh. tty->count is 1, which means that the tty
1648 * structure will be freed. state->count should always
1649 * be one in these conditions. If it's greater than
1650 * one, we've got real problems, since it means the
1651 * serial port won't be shutdown.
1652 */
1653 printk("rs_close: bad serial port count; tty->count is 1, "
1654 "state->count is %d\n", state->count);
1655 state->count = 1;
1656 }
1657 if (--state->count < 0) {
1658 printk("rs_close: bad serial port count for ttys%d: %d\n",
1659 info->line, state->count);
1660 state->count = 0;
1661 }
1662 if (state->count) {
1663 DBG_CNT("before DEC-2");
1664 local_irq_restore(flags);
1665 return;
1666 }
1667 info->flags |= ASYNC_CLOSING;
1668 /*
1669 * Now we wait for the transmit buffer to clear; and we notify
1670 * the line discipline to only process XON/XOFF characters.
1671 */
1672 tty->closing = 1;
1673 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1674 tty_wait_until_sent(tty, info->closing_wait);
1675 /*
1676 * At this point we stop accepting input. To do this, we
1677 * disable the receive line status interrupts, and tell the
1678 * interrupt driver to stop checking the data ready bit in the
1679 * line status register.
1680 */
1681 info->read_status_mask &= ~BD_SC_EMPTY;
1682 if (info->flags & ASYNC_INITIALIZED) {
1683
1684 idx = PORT_NUM(info->state->smc_scc_num);
1685 if (info->state->smc_scc_num & NUM_IS_SCC) {
1686 sccp = &pquicc->scc_regs[idx];
1687 sccp->scc_sccm &= ~UART_SCCM_RX;
1688 sccp->scc_gsmr.w.low &= ~SCC_GSMRL_ENR;
1689 } else {
1690 smcp = &pquicc->smc_regs[idx];
1691 smcp->smc_smcm &= ~SMCM_RX;
1692 smcp->smc_smcmr &= ~SMCMR_REN;
1693 }
1694 /*
1695 * Before we drop DTR, make sure the UART transmitter
1696 * has completely drained; this is especially
1697 * important if there is a transmit FIFO!
1698 */
1699 rs_360_wait_until_sent(tty, info->timeout);
1700 }
1701 shutdown(info);
1702 if (tty->driver->flush_buffer)
1703 tty->driver->flush_buffer(tty);
1704 tty_ldisc_flush(tty);
1705 tty->closing = 0;
1706 info->event = 0;
1707 info->tty = 0;
1708 if (info->blocked_open) {
1709 if (info->close_delay) {
1710 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1711 }
1712 wake_up_interruptible(&info->open_wait);
1713 }
1714 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1715 wake_up_interruptible(&info->close_wait);
1716 local_irq_restore(flags);
1717}
1718
1719/*
1720 * rs_wait_until_sent() --- wait until the transmitter is empty
1721 */
1722static void rs_360_wait_until_sent(struct tty_struct *tty, int timeout)
1723{
1724 ser_info_t *info = (ser_info_t *)tty->driver_data;
1725 unsigned long orig_jiffies, char_time;
1726 /*int lsr;*/
1727 volatile QUICC_BD *bdp;
1728
1729 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1730 return;
1731
1732#ifdef maybe
1733 if (info->state->type == PORT_UNKNOWN)
1734 return;
1735#endif
1736
1737 orig_jiffies = jiffies;
1738 /*
1739 * Set the check interval to be 1/5 of the estimated time to
1740 * send a single character, and make it at least 1. The check
1741 * interval should also be less than the timeout.
1742 *
1743 * Note: we have to use pretty tight timings here to satisfy
1744 * the NIST-PCTS.
1745 */
1746 char_time = 1;
1747 if (timeout)
1748 char_time = min(char_time, (unsigned long)timeout);
1749#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1750 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1751 printk("jiff=%lu...", jiffies);
1752#endif
1753
1754 /* We go through the loop at least once because we can't tell
1755 * exactly when the last character exits the shifter. There can
1756 * be at least two characters waiting to be sent after the buffers
1757 * are empty.
1758 */
1759 do {
1760#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1761 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1762#endif
1763/* current->counter = 0; make us low-priority */
1764 msleep_interruptible(jiffies_to_msecs(char_time));
1765 if (signal_pending(current))
1766 break;
1767 if (timeout && ((orig_jiffies + timeout) < jiffies))
1768 break;
1769 /* The 'tx_cur' is really the next buffer to send. We
1770 * have to back up to the previous BD and wait for it
1771 * to go. This isn't perfect, because all this indicates
1772 * is the buffer is available. There are still characters
1773 * in the CPM FIFO.
1774 */
1775 bdp = info->tx_cur;
1776 if (bdp == info->tx_bd_base)
1777 bdp += (TX_NUM_FIFO-1);
1778 else
1779 bdp--;
1780 } while (bdp->status & BD_SC_READY);
1781 current->state = TASK_RUNNING;
1782#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1783 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1784#endif
1785}
1786
1787/*
1788 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1789 */
1790static void rs_360_hangup(struct tty_struct *tty)
1791{
1792 ser_info_t *info = (ser_info_t *)tty->driver_data;
1793 struct serial_state *state = info->state;
1794
1795 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1796 return;
1797
1798 state = info->state;
1799
1800 rs_360_flush_buffer(tty);
1801 shutdown(info);
1802 info->event = 0;
1803 state->count = 0;
1804 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1805 info->tty = 0;
1806 wake_up_interruptible(&info->open_wait);
1807}
1808
1809/*
1810 * ------------------------------------------------------------
1811 * rs_open() and friends
1812 * ------------------------------------------------------------
1813 */
1814static int block_til_ready(struct tty_struct *tty, struct file * filp,
1815 ser_info_t *info)
1816{
1817#ifdef DO_THIS_LATER
1818 DECLARE_WAITQUEUE(wait, current);
1819#endif
1820 struct serial_state *state = info->state;
1821 int retval;
1822 int do_clocal = 0;
1823
1824 /*
1825 * If the device is in the middle of being closed, then block
1826 * until it's done, and then try again.
1827 */
1828 if (tty_hung_up_p(filp) ||
1829 (info->flags & ASYNC_CLOSING)) {
1830 if (info->flags & ASYNC_CLOSING)
1831 interruptible_sleep_on(&info->close_wait);
1832#ifdef SERIAL_DO_RESTART
1833 if (info->flags & ASYNC_HUP_NOTIFY)
1834 return -EAGAIN;
1835 else
1836 return -ERESTARTSYS;
1837#else
1838 return -EAGAIN;
1839#endif
1840 }
1841
1842 /*
1843 * If non-blocking mode is set, or the port is not enabled,
1844 * then make the check up front and then exit.
1845 * If this is an SMC port, we don't have modem control to wait
1846 * for, so just get out here.
1847 */
1848 if ((filp->f_flags & O_NONBLOCK) ||
1849 (tty->flags & (1 << TTY_IO_ERROR)) ||
1850 !(info->state->smc_scc_num & NUM_IS_SCC)) {
1851 info->flags |= ASYNC_NORMAL_ACTIVE;
1852 return 0;
1853 }
1854
1855 if (tty->termios->c_cflag & CLOCAL)
1856 do_clocal = 1;
1857
1858 /*
1859 * Block waiting for the carrier detect and the line to become
1860 * free (i.e., not in use by the callout). While we are in
1861 * this loop, state->count is dropped by one, so that
1862 * rs_close() knows when to free things. We restore it upon
1863 * exit, either normal or abnormal.
1864 */
1865 retval = 0;
1866#ifdef DO_THIS_LATER
1867 add_wait_queue(&info->open_wait, &wait);
1868#ifdef SERIAL_DEBUG_OPEN
1869 printk("block_til_ready before block: ttys%d, count = %d\n",
1870 state->line, state->count);
1871#endif
1872 local_irq_disable();
1873 if (!tty_hung_up_p(filp))
1874 state->count--;
1875 local_irq_enable();
1876 info->blocked_open++;
1877 while (1) {
1878 local_irq_disable();
1879 if (tty->termios->c_cflag & CBAUD)
1880 serial_out(info, UART_MCR,
1881 serial_inp(info, UART_MCR) |
1882 (UART_MCR_DTR | UART_MCR_RTS));
1883 local_irq_enable();
1884 set_current_state(TASK_INTERRUPTIBLE);
1885 if (tty_hung_up_p(filp) ||
1886 !(info->flags & ASYNC_INITIALIZED)) {
1887#ifdef SERIAL_DO_RESTART
1888 if (info->flags & ASYNC_HUP_NOTIFY)
1889 retval = -EAGAIN;
1890 else
1891 retval = -ERESTARTSYS;
1892#else
1893 retval = -EAGAIN;
1894#endif
1895 break;
1896 }
1897 if (!(info->flags & ASYNC_CLOSING) &&
1898 (do_clocal || (serial_in(info, UART_MSR) &
1899 UART_MSR_DCD)))
1900 break;
1901 if (signal_pending(current)) {
1902 retval = -ERESTARTSYS;
1903 break;
1904 }
1905#ifdef SERIAL_DEBUG_OPEN
1906 printk("block_til_ready blocking: ttys%d, count = %d\n",
1907 info->line, state->count);
1908#endif
1909 schedule();
1910 }
1911 current->state = TASK_RUNNING;
1912 remove_wait_queue(&info->open_wait, &wait);
1913 if (!tty_hung_up_p(filp))
1914 state->count++;
1915 info->blocked_open--;
1916#ifdef SERIAL_DEBUG_OPEN
1917 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1918 info->line, state->count);
1919#endif
1920#endif /* DO_THIS_LATER */
1921 if (retval)
1922 return retval;
1923 info->flags |= ASYNC_NORMAL_ACTIVE;
1924 return 0;
1925}
1926
1927static int get_async_struct(int line, ser_info_t **ret_info)
1928{
1929 struct serial_state *sstate;
1930
1931 sstate = rs_table + line;
1932 if (sstate->info) {
1933 sstate->count++;
1934 *ret_info = (ser_info_t *)sstate->info;
1935 return 0;
1936 }
1937 else {
1938 return -ENOMEM;
1939 }
1940}
1941
1942/*
1943 * This routine is called whenever a serial port is opened. It
1944 * enables interrupts for a serial port, linking in its async structure into
1945 * the IRQ chain. It also performs the serial-specific
1946 * initialization for the tty structure.
1947 */
1948static int rs_360_open(struct tty_struct *tty, struct file * filp)
1949{
1950 ser_info_t *info;
1951 int retval, line;
1952
1953 line = tty->index;
1954 if ((line < 0) || (line >= NR_PORTS))
1955 return -ENODEV;
1956 retval = get_async_struct(line, &info);
1957 if (retval)
1958 return retval;
1959 if (serial_paranoia_check(info, tty->name, "rs_open"))
1960 return -ENODEV;
1961
1962#ifdef SERIAL_DEBUG_OPEN
1963 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1964#endif
1965 tty->driver_data = info;
1966 info->tty = tty;
1967
1968 /*
1969 * Start up serial port
1970 */
1971 retval = startup(info);
1972 if (retval)
1973 return retval;
1974
1975 retval = block_til_ready(tty, filp, info);
1976 if (retval) {
1977#ifdef SERIAL_DEBUG_OPEN
1978 printk("rs_open returning after block_til_ready with %d\n",
1979 retval);
1980#endif
1981 return retval;
1982 }
1983
1984#ifdef SERIAL_DEBUG_OPEN
1985 printk("rs_open %s successful...", tty->name);
1986#endif
1987 return 0;
1988}
1989
1990/*
1991 * /proc fs routines....
1992 */
1993
1994static inline int line_info(char *buf, struct serial_state *state)
1995{
1996#ifdef notdef
1997 struct async_struct *info = state->info, scr_info;
1998 char stat_buf[30], control, status;
1999#endif
2000 int ret;
2001
2002 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
2003 state->line,
2004 (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC",
2005 (unsigned int)(state->port), state->irq);
2006
2007 if (!state->port || (state->type == PORT_UNKNOWN)) {
2008 ret += sprintf(buf+ret, "\n");
2009 return ret;
2010 }
2011
2012#ifdef notdef
2013 /*
2014 * Figure out the current RS-232 lines
2015 */
2016 if (!info) {
2017 info = &scr_info; /* This is just for serial_{in,out} */
2018
2019 info->magic = SERIAL_MAGIC;
2020 info->port = state->port;
2021 info->flags = state->flags;
2022 info->quot = 0;
2023 info->tty = 0;
2024 }
2025 local_irq_disable();
2026 status = serial_in(info, UART_MSR);
2027 control = info ? info->MCR : serial_in(info, UART_MCR);
2028 local_irq_enable();
2029
2030 stat_buf[0] = 0;
2031 stat_buf[1] = 0;
2032 if (control & UART_MCR_RTS)
2033 strcat(stat_buf, "|RTS");
2034 if (status & UART_MSR_CTS)
2035 strcat(stat_buf, "|CTS");
2036 if (control & UART_MCR_DTR)
2037 strcat(stat_buf, "|DTR");
2038 if (status & UART_MSR_DSR)
2039 strcat(stat_buf, "|DSR");
2040 if (status & UART_MSR_DCD)
2041 strcat(stat_buf, "|CD");
2042 if (status & UART_MSR_RI)
2043 strcat(stat_buf, "|RI");
2044
2045 if (info->quot) {
2046 ret += sprintf(buf+ret, " baud:%d",
2047 state->baud_base / info->quot);
2048 }
2049
2050 ret += sprintf(buf+ret, " tx:%d rx:%d",
2051 state->icount.tx, state->icount.rx);
2052
2053 if (state->icount.frame)
2054 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2055
2056 if (state->icount.parity)
2057 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2058
2059 if (state->icount.brk)
2060 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2061
2062 if (state->icount.overrun)
2063 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2064
2065 /*
2066 * Last thing is the RS-232 status lines
2067 */
2068 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2069#endif
2070 return ret;
2071}
2072
2073int rs_360_read_proc(char *page, char **start, off_t off, int count,
2074 int *eof, void *data)
2075{
2076 int i, len = 0;
2077 off_t begin = 0;
2078
2079 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2080 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2081 len += line_info(page + len, &rs_table[i]);
2082 if (len+begin > off+count)
2083 goto done;
2084 if (len+begin < off) {
2085 begin += len;
2086 len = 0;
2087 }
2088 }
2089 *eof = 1;
2090done:
2091 if (off >= len+begin)
2092 return 0;
2093 *start = page + (begin-off);
2094 return ((count < begin+len-off) ? count : begin+len-off);
2095}
2096
2097/*
2098 * ---------------------------------------------------------------------
2099 * rs_init() and friends
2100 *
2101 * rs_init() is called at boot-time to initialize the serial driver.
2102 * ---------------------------------------------------------------------
2103 */
2104
2105/*
2106 * This routine prints out the appropriate serial driver version
2107 * number, and identifies which options were configured into this
2108 * driver.
2109 */
2110static _INLINE_ void show_serial_version(void)
2111{
2112 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2113}
2114
2115
2116/*
2117 * The serial console driver used during boot. Note that these names
2118 * clash with those found in "serial.c", so we currently can't support
2119 * the 16xxx uarts and these at the same time. I will fix this to become
2120 * an indirect function call from tty_io.c (or something).
2121 */
2122
2123#ifdef CONFIG_SERIAL_CONSOLE
2124
2125/*
2126 * Print a string to the serial port trying not to disturb any possible
2127 * real use of the port...
2128 */
2129static void my_console_write(int idx, const char *s,
2130 unsigned count)
2131{
2132 struct serial_state *ser;
2133 ser_info_t *info;
2134 unsigned i;
2135 QUICC_BD *bdp, *bdbase;
2136 volatile struct smc_uart_pram *up;
2137 volatile u_char *cp;
2138
2139 ser = rs_table + idx;
2140
2141
2142 /* If the port has been initialized for general use, we have
2143 * to use the buffer descriptors allocated there. Otherwise,
2144 * we simply use the single buffer allocated.
2145 */
2146 if ((info = (ser_info_t *)ser->info) != NULL) {
2147 bdp = info->tx_cur;
2148 bdbase = info->tx_bd_base;
2149 }
2150 else {
2151 /* Pointer to UART in parameter ram.
2152 */
2153 /* up = (smc_uart_t *)&cpmp->cp_dparam[ser->port]; */
2154 up = &pquicc->pram[ser->port].scc.pothers.idma_smc.psmc.u;
2155
2156 /* Get the address of the host memory buffer.
2157 */
2158 bdp = bdbase = (QUICC_BD *)((uint)pquicc + (uint)up->tbase);
2159 }
2160
2161 /*
2162 * We need to gracefully shut down the transmitter, disable
2163 * interrupts, then send our bytes out.
2164 */
2165
2166 /*
2167 * Now, do each character. This is not as bad as it looks
2168 * since this is a holding FIFO and not a transmitting FIFO.
2169 * We could add the complexity of filling the entire transmit
2170 * buffer, but we would just wait longer between accesses......
2171 */
2172 for (i = 0; i < count; i++, s++) {
2173 /* Wait for transmitter fifo to empty.
2174 * Ready indicates output is ready, and xmt is doing
2175 * that, not that it is ready for us to send.
2176 */
2177 while (bdp->status & BD_SC_READY);
2178
2179 /* Send the character out.
2180 */
2181 cp = bdp->buf;
2182 *cp = *s;
2183
2184 bdp->length = 1;
2185 bdp->status |= BD_SC_READY;
2186
2187 if (bdp->status & BD_SC_WRAP)
2188 bdp = bdbase;
2189 else
2190 bdp++;
2191
2192 /* if a LF, also do CR... */
2193 if (*s == 10) {
2194 while (bdp->status & BD_SC_READY);
2195 /* cp = __va(bdp->buf); */
2196 cp = bdp->buf;
2197 *cp = 13;
2198 bdp->length = 1;
2199 bdp->status |= BD_SC_READY;
2200
2201 if (bdp->status & BD_SC_WRAP) {
2202 bdp = bdbase;
2203 }
2204 else {
2205 bdp++;
2206 }
2207 }
2208 }
2209
2210 /*
2211 * Finally, Wait for transmitter & holding register to empty
2212 * and restore the IER
2213 */
2214 while (bdp->status & BD_SC_READY);
2215
2216 if (info)
2217 info->tx_cur = (QUICC_BD *)bdp;
2218}
2219
2220static void serial_console_write(struct console *c, const char *s,
2221 unsigned count)
2222{
2223#ifdef CONFIG_KGDB
2224 /* Try to let stub handle output. Returns true if it did. */
2225 if (kgdb_output_string(s, count))
2226 return;
2227#endif
2228 my_console_write(c->index, s, count);
2229}
2230
2231
2232
2233/*void console_print_68360(const char *p)
2234{
2235 const char *cp = p;
2236 int i;
2237
2238 for (i=0;cp[i]!=0;i++);
2239
2240 serial_console_write (p, i);
2241
2242 //Comment this if you want to have a strict interrupt-driven output
2243 //rs_fair_output();
2244
2245 return;
2246}*/
2247
2248
2249
2250
2251
2252
2253#ifdef CONFIG_XMON
2254int
2255xmon_360_write(const char *s, unsigned count)
2256{
2257 my_console_write(0, s, count);
2258 return(count);
2259}
2260#endif
2261
2262#ifdef CONFIG_KGDB
2263void
2264putDebugChar(char ch)
2265{
2266 my_console_write(0, &ch, 1);
2267}
2268#endif
2269
2270/*
2271 * Receive character from the serial port. This only works well
2272 * before the port is initialized for real use.
2273 */
2274static int my_console_wait_key(int idx, int xmon, char *obuf)
2275{
2276 struct serial_state *ser;
2277 u_char c, *cp;
2278 ser_info_t *info;
2279 QUICC_BD *bdp;
2280 volatile struct smc_uart_pram *up;
2281 int i;
2282
2283 ser = rs_table + idx;
2284
2285 /* Get the address of the host memory buffer.
2286 * If the port has been initialized for general use, we must
2287 * use information from the port structure.
2288 */
2289 if ((info = (ser_info_t *)ser->info))
2290 bdp = info->rx_cur;
2291 else
2292 /* bdp = (QUICC_BD *)&cpmp->cp_dpmem[up->smc_rbase]; */
2293 bdp = (QUICC_BD *)((uint)pquicc + (uint)up->tbase);
2294
2295 /* Pointer to UART in parameter ram.
2296 */
2297 /* up = (smc_uart_t *)&cpmp->cp_dparam[ser->port]; */
2298 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
2299
2300 /*
2301 * We need to gracefully shut down the receiver, disable
2302 * interrupts, then read the input.
2303 * XMON just wants a poll. If no character, return -1, else
2304 * return the character.
2305 */
2306 if (!xmon) {
2307 while (bdp->status & BD_SC_EMPTY);
2308 }
2309 else {
2310 if (bdp->status & BD_SC_EMPTY)
2311 return -1;
2312 }
2313
2314 cp = (char *)bdp->buf;
2315
2316 if (obuf) {
2317 i = c = bdp->length;
2318 while (i-- > 0)
2319 *obuf++ = *cp++;
2320 }
2321 else {
2322 c = *cp;
2323 }
2324 bdp->status |= BD_SC_EMPTY;
2325
2326 if (info) {
2327 if (bdp->status & BD_SC_WRAP) {
2328 bdp = info->rx_bd_base;
2329 }
2330 else {
2331 bdp++;
2332 }
2333 info->rx_cur = (QUICC_BD *)bdp;
2334 }
2335
2336 return((int)c);
2337}
2338
2339static int serial_console_wait_key(struct console *co)
2340{
2341 return(my_console_wait_key(co->index, 0, NULL));
2342}
2343
2344#ifdef CONFIG_XMON
2345int
2346xmon_360_read_poll(void)
2347{
2348 return(my_console_wait_key(0, 1, NULL));
2349}
2350
2351int
2352xmon_360_read_char(void)
2353{
2354 return(my_console_wait_key(0, 0, NULL));
2355}
2356#endif
2357
2358#ifdef CONFIG_KGDB
2359static char kgdb_buf[RX_BUF_SIZE], *kgdp;
2360static int kgdb_chars;
2361
2362unsigned char
2363getDebugChar(void)
2364{
2365 if (kgdb_chars <= 0) {
2366 kgdb_chars = my_console_wait_key(0, 0, kgdb_buf);
2367 kgdp = kgdb_buf;
2368 }
2369 kgdb_chars--;
2370
2371 return(*kgdp++);
2372}
2373
2374void kgdb_interruptible(int state)
2375{
2376}
2377void kgdb_map_scc(void)
2378{
2379 struct serial_state *ser;
2380 uint mem_addr;
2381 volatile QUICC_BD *bdp;
2382 volatile smc_uart_t *up;
2383
2384 cpmp = (cpm360_t *)&(((immap_t *)IMAP_ADDR)->im_cpm);
2385
2386 /* To avoid data cache CPM DMA coherency problems, allocate a
2387 * buffer in the CPM DPRAM. This will work until the CPM and
2388 * serial ports are initialized. At that time a memory buffer
2389 * will be allocated.
2390 * The port is already initialized from the boot procedure, all
2391 * we do here is give it a different buffer and make it a FIFO.
2392 */
2393
2394 ser = rs_table;
2395
2396 /* Right now, assume we are using SMCs.
2397 */
2398 up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2399
2400 /* Allocate space for an input FIFO, plus a few bytes for output.
2401 * Allocate bytes to maintain word alignment.
2402 */
2403 mem_addr = (uint)(&cpmp->cp_dpmem[0x1000]);
2404
2405 /* Set the physical address of the host memory buffers in
2406 * the buffer descriptors.
2407 */
2408 bdp = (QUICC_BD *)&cpmp->cp_dpmem[up->smc_rbase];
2409 bdp->buf = mem_addr;
2410
2411 bdp = (QUICC_BD *)&cpmp->cp_dpmem[up->smc_tbase];
2412 bdp->buf = mem_addr+RX_BUF_SIZE;
2413
2414 up->smc_mrblr = RX_BUF_SIZE; /* receive buffer length */
2415 up->smc_maxidl = RX_BUF_SIZE;
2416}
2417#endif
2418
2419static struct tty_struct *serial_console_device(struct console *c, int *index)
2420{
2421 *index = c->index;
2422 return serial_driver;
2423}
2424
2425
2426struct console sercons = {
2427 .name = "ttyS",
2428 .write = serial_console_write,
2429 .device = serial_console_device,
2430 .wait_key = serial_console_wait_key,
2431 .setup = serial_console_setup,
2432 .flags = CON_PRINTBUFFER,
2433 .index = CONFIG_SERIAL_CONSOLE_PORT,
2434};
2435
2436
2437
2438/*
2439 * Register console.
2440 */
2441long console_360_init(long kmem_start, long kmem_end)
2442{
2443 register_console(&sercons);
2444 /*register_console (console_print_68360); - 2.0.38 only required a write
2445 function pointer. */
2446 return kmem_start;
2447}
2448
2449#endif
2450
2451/* Index in baud rate table of the default console baud rate.
2452*/
2453static int baud_idx;
2454
2455static struct tty_operations rs_360_ops = {
2456 .owner = THIS_MODULE,
2457 .open = rs_360_open,
2458 .close = rs_360_close,
2459 .write = rs_360_write,
2460 .put_char = rs_360_put_char,
2461 .write_room = rs_360_write_room,
2462 .chars_in_buffer = rs_360_chars_in_buffer,
2463 .flush_buffer = rs_360_flush_buffer,
2464 .ioctl = rs_360_ioctl,
2465 .throttle = rs_360_throttle,
2466 .unthrottle = rs_360_unthrottle,
2467 /* .send_xchar = rs_360_send_xchar, */
2468 .set_termios = rs_360_set_termios,
2469 .stop = rs_360_stop,
2470 .start = rs_360_start,
2471 .hangup = rs_360_hangup,
2472 /* .wait_until_sent = rs_360_wait_until_sent, */
2473 /* .read_proc = rs_360_read_proc, */
2474 .tiocmget = rs_360_tiocmget,
2475 .tiocmset = rs_360_tiocmset,
2476};
2477
2478/* int __init rs_360_init(void) */
2479int rs_360_init(void)
2480{
2481 struct serial_state * state;
2482 ser_info_t *info;
2483 void *mem_addr;
2484 uint dp_addr, iobits;
2485 int i, j, idx;
2486 ushort chan;
2487 QUICC_BD *bdp;
2488 volatile QUICC *cp;
2489 volatile struct smc_regs *sp;
2490 volatile struct smc_uart_pram *up;
2491 volatile struct scc_regs *scp;
2492 volatile struct uart_pram *sup;
2493 /* volatile immap_t *immap; */
2494
2495 serial_driver = alloc_tty_driver(NR_PORTS);
2496 if (!serial_driver)
2497 return -1;
2498
2499 show_serial_version();
2500
2501 serial_driver->name = "ttyS";
2502 serial_driver->major = TTY_MAJOR;
2503 serial_driver->minor_start = 64;
2504 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2505 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2506 serial_driver->init_termios = tty_std_termios;
2507 serial_driver->init_termios.c_cflag =
2508 baud_idx | CS8 | CREAD | HUPCL | CLOCAL;
2509 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2510 tty_set_operations(serial_driver, &rs_360_ops);
2511
2512 if (tty_register_driver(serial_driver))
2513 panic("Couldn't register serial driver\n");
2514
2515 cp = pquicc; /* Get pointer to Communication Processor */
2516 /* immap = (immap_t *)IMAP_ADDR; */ /* and to internal registers */
2517
2518
2519 /* Configure SCC2, SCC3, and SCC4 instead of port A parallel I/O.
2520 */
2521 /* The "standard" configuration through the 860.
2522 */
2523/* immap->im_ioport.iop_papar |= 0x00fc; */
2524/* immap->im_ioport.iop_padir &= ~0x00fc; */
2525/* immap->im_ioport.iop_paodr &= ~0x00fc; */
2526 cp->pio_papar |= 0x00fc;
2527 cp->pio_padir &= ~0x00fc;
2528 /* cp->pio_paodr &= ~0x00fc; */
2529
2530
2531 /* Since we don't yet do modem control, connect the port C pins
2532 * as general purpose I/O. This will assert CTS and CD for the
2533 * SCC ports.
2534 */
2535 /* FIXME: see 360um p.7-365 and 860um p.34-12
2536 * I can't make sense of these bits - mleslie*/
2537/* immap->im_ioport.iop_pcdir |= 0x03c6; */
2538/* immap->im_ioport.iop_pcpar &= ~0x03c6; */
2539
2540/* cp->pio_pcdir |= 0x03c6; */
2541/* cp->pio_pcpar &= ~0x03c6; */
2542
2543
2544
2545 /* Connect SCC2 and SCC3 to NMSI. Connect BRG3 to SCC2 and
2546 * BRG4 to SCC3.
2547 */
2548 cp->si_sicr &= ~0x00ffff00;
2549 cp->si_sicr |= 0x001b1200;
2550
2551#ifdef CONFIG_PP04
2552 /* Frequentis PP04 forced to RS-232 until we know better.
2553 * Port C 12 and 13 low enables RS-232 on SCC3 and SCC4.
2554 */
2555 immap->im_ioport.iop_pcdir |= 0x000c;
2556 immap->im_ioport.iop_pcpar &= ~0x000c;
2557 immap->im_ioport.iop_pcdat &= ~0x000c;
2558
2559 /* This enables the TX driver.
2560 */
2561 cp->cp_pbpar &= ~0x6000;
2562 cp->cp_pbdat &= ~0x6000;
2563#endif
2564
2565 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2566 state->magic = SSTATE_MAGIC;
2567 state->line = i;
2568 state->type = PORT_UNKNOWN;
2569 state->custom_divisor = 0;
2570 state->close_delay = 5*HZ/10;
2571 state->closing_wait = 30*HZ;
2572 state->icount.cts = state->icount.dsr =
2573 state->icount.rng = state->icount.dcd = 0;
2574 state->icount.rx = state->icount.tx = 0;
2575 state->icount.frame = state->icount.parity = 0;
2576 state->icount.overrun = state->icount.brk = 0;
2577 printk(KERN_INFO "ttyS%d at irq 0x%02x is an %s\n",
2578 i, (unsigned int)(state->irq),
2579 (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC");
2580
2581#ifdef CONFIG_SERIAL_CONSOLE
2582 /* If we just printed the message on the console port, and
2583 * we are about to initialize it for general use, we have
2584 * to wait a couple of character times for the CR/NL to
2585 * make it out of the transmit buffer.
2586 */
2587 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2588 mdelay(8);
2589
2590
2591/* idx = PORT_NUM(info->state->smc_scc_num); */
2592/* if (info->state->smc_scc_num & NUM_IS_SCC) */
2593/* chan = scc_chan_map[idx]; */
2594/* else */
2595/* chan = smc_chan_map[idx]; */
2596
2597/* cp->cp_cr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG; */
2598/* while (cp->cp_cr & CPM_CR_FLG); */
2599
2600#endif
2601 /* info = kmalloc(sizeof(ser_info_t), GFP_KERNEL); */
2602 info = &quicc_ser_info[i];
2603 if (info) {
2604 memset (info, 0, sizeof(ser_info_t));
2605 info->magic = SERIAL_MAGIC;
2606 info->line = i;
2607 info->flags = state->flags;
2608 INIT_WORK(&info->tqueue, do_softint, info);
2609 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2610 init_waitqueue_head(&info->open_wait);
2611 init_waitqueue_head(&info->close_wait);
2612 info->state = state;
2613 state->info = (struct async_struct *)info;
2614
2615 /* We need to allocate a transmit and receive buffer
2616 * descriptors from dual port ram, and a character
2617 * buffer area from host mem.
2618 */
2619 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * RX_NUM_FIFO);
2620
2621 /* Allocate space for FIFOs in the host memory.
2622 * (for now this is from a static array of buffers :(
2623 */
2624 /* mem_addr = m360_cpm_hostalloc(RX_NUM_FIFO * RX_BUF_SIZE); */
2625 /* mem_addr = kmalloc (RX_NUM_FIFO * RX_BUF_SIZE, GFP_BUFFER); */
2626 mem_addr = &rx_buf_pool[i * RX_NUM_FIFO * RX_BUF_SIZE];
2627
2628 /* Set the physical address of the host memory
2629 * buffers in the buffer descriptors, and the
2630 * virtual address for us to work with.
2631 */
2632 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2633 info->rx_cur = info->rx_bd_base = bdp;
2634
2635 /* initialize rx buffer descriptors */
2636 for (j=0; j<(RX_NUM_FIFO-1); j++) {
2637 bdp->buf = &rx_buf_pool[(i * RX_NUM_FIFO + j ) * RX_BUF_SIZE];
2638 bdp->status = BD_SC_EMPTY | BD_SC_INTRPT;
2639 mem_addr += RX_BUF_SIZE;
2640 bdp++;
2641 }
2642 bdp->buf = &rx_buf_pool[(i * RX_NUM_FIFO + j ) * RX_BUF_SIZE];
2643 bdp->status = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2644
2645
2646 idx = PORT_NUM(info->state->smc_scc_num);
2647 if (info->state->smc_scc_num & NUM_IS_SCC) {
2648
2649#if defined (CONFIG_UCQUICC) && 1
2650 /* set the transceiver mode to RS232 */
2651 sipex_mode_bits &= ~(uint)SIPEX_MODE(idx,0x0f); /* clear current mode */
2652 sipex_mode_bits |= (uint)SIPEX_MODE(idx,0x02);
2653 *(uint *)_periph_base = sipex_mode_bits;
2654 /* printk ("sipex bits = 0x%08x\n", sipex_mode_bits); */
2655#endif
2656 }
2657
2658 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * TX_NUM_FIFO);
2659
2660 /* Allocate space for FIFOs in the host memory.
2661 */
2662 /* mem_addr = m360_cpm_hostalloc(TX_NUM_FIFO * TX_BUF_SIZE); */
2663 /* mem_addr = kmalloc (TX_NUM_FIFO * TX_BUF_SIZE, GFP_BUFFER); */
2664 mem_addr = &tx_buf_pool[i * TX_NUM_FIFO * TX_BUF_SIZE];
2665
2666 /* Set the physical address of the host memory
2667 * buffers in the buffer descriptors, and the
2668 * virtual address for us to work with.
2669 */
2670 /* bdp = (QUICC_BD *)&cp->cp_dpmem[dp_addr]; */
2671 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2672 info->tx_cur = info->tx_bd_base = (QUICC_BD *)bdp;
2673
2674 /* initialize tx buffer descriptors */
2675 for (j=0; j<(TX_NUM_FIFO-1); j++) {
2676 bdp->buf = &tx_buf_pool[(i * TX_NUM_FIFO + j ) * TX_BUF_SIZE];
2677 bdp->status = BD_SC_INTRPT;
2678 mem_addr += TX_BUF_SIZE;
2679 bdp++;
2680 }
2681 bdp->buf = &tx_buf_pool[(i * TX_NUM_FIFO + j ) * TX_BUF_SIZE];
2682 bdp->status = (BD_SC_WRAP | BD_SC_INTRPT);
2683
2684 if (info->state->smc_scc_num & NUM_IS_SCC) {
2685 scp = &pquicc->scc_regs[idx];
2686 sup = &pquicc->pram[info->state->port].scc.pscc.u;
2687 sup->rbase = dp_addr;
2688 sup->tbase = dp_addr;
2689
2690 /* Set up the uart parameters in the
2691 * parameter ram.
2692 */
2693 sup->rfcr = SMC_EB;
2694 sup->tfcr = SMC_EB;
2695
2696 /* Set this to 1 for now, so we get single
2697 * character interrupts. Using idle charater
2698 * time requires some additional tuning.
2699 */
2700 sup->mrblr = 1;
2701 sup->max_idl = 0;
2702 sup->brkcr = 1;
2703 sup->parec = 0;
2704 sup->frmer = 0;
2705 sup->nosec = 0;
2706 sup->brkec = 0;
2707 sup->uaddr1 = 0;
2708 sup->uaddr2 = 0;
2709 sup->toseq = 0;
2710 {
2711 int i;
2712 for (i=0;i<8;i++)
2713 sup->cc[i] = 0x8000;
2714 }
2715 sup->rccm = 0xc0ff;
2716
2717 /* Send the CPM an initialize command.
2718 */
2719 chan = scc_chan_map[idx];
2720
2721 /* execute the INIT RX & TX PARAMS command for this channel. */
2722 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2723 while (cp->cp_cr & CPM_CR_FLG);
2724
2725 /* Set UART mode, 8 bit, no parity, one stop.
2726 * Enable receive and transmit.
2727 */
2728 scp->scc_gsmr.w.high = 0;
2729 scp->scc_gsmr.w.low =
2730 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2731
2732 /* Disable all interrupts and clear all pending
2733 * events.
2734 */
2735 scp->scc_sccm = 0;
2736 scp->scc_scce = 0xffff;
2737 scp->scc_dsr = 0x7e7e;
2738 scp->scc_psmr = 0x3000;
2739
2740 /* If the port is the console, enable Rx and Tx.
2741 */
2742#ifdef CONFIG_SERIAL_CONSOLE
2743 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2744 scp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2745#endif
2746 }
2747 else {
2748 /* Configure SMCs Tx/Rx instead of port B
2749 * parallel I/O.
2750 */
2751 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
2752 up->rbase = dp_addr;
2753
2754 iobits = 0xc0 << (idx * 4);
2755 cp->pip_pbpar |= iobits;
2756 cp->pip_pbdir &= ~iobits;
2757 cp->pip_pbodr &= ~iobits;
2758
2759
2760 /* Connect the baud rate generator to the
2761 * SMC based upon index in rs_table. Also
2762 * make sure it is connected to NMSI.
2763 */
2764 cp->si_simode &= ~(0xffff << (idx * 16));
2765 cp->si_simode |= (i << ((idx * 16) + 12));
2766
2767 up->tbase = dp_addr;
2768
2769 /* Set up the uart parameters in the
2770 * parameter ram.
2771 */
2772 up->rfcr = SMC_EB;
2773 up->tfcr = SMC_EB;
2774
2775 /* Set this to 1 for now, so we get single
2776 * character interrupts. Using idle charater
2777 * time requires some additional tuning.
2778 */
2779 up->mrblr = 1;
2780 up->max_idl = 0;
2781 up->brkcr = 1;
2782
2783 /* Send the CPM an initialize command.
2784 */
2785 chan = smc_chan_map[idx];
2786
2787 cp->cp_cr = mk_cr_cmd(chan,
2788 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2789#ifdef CONFIG_SERIAL_CONSOLE
2790 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2791 printk("");
2792#endif
2793 while (cp->cp_cr & CPM_CR_FLG);
2794
2795 /* Set UART mode, 8 bit, no parity, one stop.
2796 * Enable receive and transmit.
2797 */
2798 sp = &cp->smc_regs[idx];
2799 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2800
2801 /* Disable all interrupts and clear all pending
2802 * events.
2803 */
2804 sp->smc_smcm = 0;
2805 sp->smc_smce = 0xff;
2806
2807 /* If the port is the console, enable Rx and Tx.
2808 */
2809#ifdef CONFIG_SERIAL_CONSOLE
2810 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2811 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2812#endif
2813 }
2814
2815 /* Install interrupt handler.
2816 */
2817 /* cpm_install_handler(IRQ_MACHSPEC | state->irq, rs_360_interrupt, info); */
2818 /*request_irq(IRQ_MACHSPEC | state->irq, rs_360_interrupt, */
2819 request_irq(state->irq, rs_360_interrupt,
2820 IRQ_FLG_LOCK, "ttyS", (void *)info);
2821
2822 /* Set up the baud rate generator.
2823 */
2824 m360_cpm_setbrg(i, baud_table[baud_idx]);
2825
2826 }
2827 }
2828
2829 return 0;
2830}
2831
2832
2833
2834
2835
2836/* This must always be called before the rs_360_init() function, otherwise
2837 * it blows away the port control information.
2838 */
2839//static int __init serial_console_setup( struct console *co, char *options)
2840int serial_console_setup( struct console *co, char *options)
2841{
2842 struct serial_state *ser;
2843 uint mem_addr, dp_addr, bidx, idx, iobits;
2844 ushort chan;
2845 QUICC_BD *bdp;
2846 volatile QUICC *cp;
2847 volatile struct smc_regs *sp;
2848 volatile struct scc_regs *scp;
2849 volatile struct smc_uart_pram *up;
2850 volatile struct uart_pram *sup;
2851
2852/* mleslie TODO:
2853 * add something to the 68k bootloader to store a desired initial console baud rate */
2854
2855/* bd_t *bd; */ /* a board info struct used by EPPC-bug */
2856/* bd = (bd_t *)__res; */
2857
2858 for (bidx = 0; bidx < (sizeof(baud_table) / sizeof(int)); bidx++)
2859 /* if (bd->bi_baudrate == baud_table[bidx]) */
2860 if (CONSOLE_BAUDRATE == baud_table[bidx])
2861 break;
2862
2863 /* co->cflag = CREAD|CLOCAL|bidx|CS8; */
2864 baud_idx = bidx;
2865
2866 ser = rs_table + CONFIG_SERIAL_CONSOLE_PORT;
2867
2868 cp = pquicc; /* Get pointer to Communication Processor */
2869
2870 idx = PORT_NUM(ser->smc_scc_num);
2871 if (ser->smc_scc_num & NUM_IS_SCC) {
2872
2873 /* TODO: need to set up SCC pin assignment etc. here */
2874
2875 }
2876 else {
2877 iobits = 0xc0 << (idx * 4);
2878 cp->pip_pbpar |= iobits;
2879 cp->pip_pbdir &= ~iobits;
2880 cp->pip_pbodr &= ~iobits;
2881
2882 /* Connect the baud rate generator to the
2883 * SMC based upon index in rs_table. Also
2884 * make sure it is connected to NMSI.
2885 */
2886 cp->si_simode &= ~(0xffff << (idx * 16));
2887 cp->si_simode |= (idx << ((idx * 16) + 12));
2888 }
2889
2890 /* When we get here, the CPM has been reset, so we need
2891 * to configure the port.
2892 * We need to allocate a transmit and receive buffer descriptor
2893 * from dual port ram, and a character buffer area from host mem.
2894 */
2895
2896 /* Allocate space for two buffer descriptors in the DP ram.
2897 */
2898 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * CONSOLE_NUM_FIFO);
2899
2900 /* Allocate space for two 2 byte FIFOs in the host memory.
2901 */
2902 /* mem_addr = m360_cpm_hostalloc(8); */
2903 mem_addr = (uint)console_fifos;
2904
2905
2906 /* Set the physical address of the host memory buffers in
2907 * the buffer descriptors.
2908 */
2909 /* bdp = (QUICC_BD *)&cp->cp_dpmem[dp_addr]; */
2910 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2911 bdp->buf = (char *)mem_addr;
2912 (bdp+1)->buf = (char *)(mem_addr+4);
2913
2914 /* For the receive, set empty and wrap.
2915 * For transmit, set wrap.
2916 */
2917 bdp->status = BD_SC_EMPTY | BD_SC_WRAP;
2918 (bdp+1)->status = BD_SC_WRAP;
2919
2920 /* Set up the uart parameters in the parameter ram.
2921 */
2922 if (ser->smc_scc_num & NUM_IS_SCC) {
2923 scp = &cp->scc_regs[idx];
2924 /* sup = (scc_uart_t *)&cp->cp_dparam[ser->port]; */
2925 sup = &pquicc->pram[ser->port].scc.pscc.u;
2926
2927 sup->rbase = dp_addr;
2928 sup->tbase = dp_addr + sizeof(QUICC_BD);
2929
2930 /* Set up the uart parameters in the
2931 * parameter ram.
2932 */
2933 sup->rfcr = SMC_EB;
2934 sup->tfcr = SMC_EB;
2935
2936 /* Set this to 1 for now, so we get single
2937 * character interrupts. Using idle charater
2938 * time requires some additional tuning.
2939 */
2940 sup->mrblr = 1;
2941 sup->max_idl = 0;
2942 sup->brkcr = 1;
2943 sup->parec = 0;
2944 sup->frmer = 0;
2945 sup->nosec = 0;
2946 sup->brkec = 0;
2947 sup->uaddr1 = 0;
2948 sup->uaddr2 = 0;
2949 sup->toseq = 0;
2950 {
2951 int i;
2952 for (i=0;i<8;i++)
2953 sup->cc[i] = 0x8000;
2954 }
2955 sup->rccm = 0xc0ff;
2956
2957 /* Send the CPM an initialize command.
2958 */
2959 chan = scc_chan_map[idx];
2960
2961 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2962 while (cp->cp_cr & CPM_CR_FLG);
2963
2964 /* Set UART mode, 8 bit, no parity, one stop.
2965 * Enable receive and transmit.
2966 */
2967 scp->scc_gsmr.w.high = 0;
2968 scp->scc_gsmr.w.low =
2969 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2970
2971 /* Disable all interrupts and clear all pending
2972 * events.
2973 */
2974 scp->scc_sccm = 0;
2975 scp->scc_scce = 0xffff;
2976 scp->scc_dsr = 0x7e7e;
2977 scp->scc_psmr = 0x3000;
2978
2979 scp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2980
2981 }
2982 else {
2983 /* up = (smc_uart_t *)&cp->cp_dparam[ser->port]; */
2984 up = &pquicc->pram[ser->port].scc.pothers.idma_smc.psmc.u;
2985
2986 up->rbase = dp_addr; /* Base of receive buffer desc. */
2987 up->tbase = dp_addr+sizeof(QUICC_BD); /* Base of xmt buffer desc. */
2988 up->rfcr = SMC_EB;
2989 up->tfcr = SMC_EB;
2990
2991 /* Set this to 1 for now, so we get single character interrupts.
2992 */
2993 up->mrblr = 1; /* receive buffer length */
2994 up->max_idl = 0; /* wait forever for next char */
2995
2996 /* Send the CPM an initialize command.
2997 */
2998 chan = smc_chan_map[idx];
2999 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
3000 while (cp->cp_cr & CPM_CR_FLG);
3001
3002 /* Set UART mode, 8 bit, no parity, one stop.
3003 * Enable receive and transmit.
3004 */
3005 sp = &cp->smc_regs[idx];
3006 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
3007
3008 /* And finally, enable Rx and Tx.
3009 */
3010 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
3011 }
3012
3013 /* Set up the baud rate generator.
3014 */
3015 /* m360_cpm_setbrg((ser - rs_table), bd->bi_baudrate); */
3016 m360_cpm_setbrg((ser - rs_table), CONSOLE_BAUDRATE);
3017
3018 return 0;
3019}
3020
3021/*
3022 * Local variables:
3023 * c-indent-level: 4
3024 * c-basic-offset: 4
3025 * tab-width: 4
3026 * End:
3027 */
diff --git a/drivers/serial/8250.c b/drivers/serial/8250.c
new file mode 100644
index 000000000000..201c3b9924f4
--- /dev/null
+++ b/drivers/serial/8250.c
@@ -0,0 +1,2632 @@
1/*
2 * linux/drivers/char/8250.c
3 *
4 * Driver for 8250/16550-type serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright (C) 2001 Russell King.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * $Id: 8250.c,v 1.90 2002/07/28 10:03:27 rmk Exp $
16 *
17 * A note about mapbase / membase
18 *
19 * mapbase is the physical address of the IO port.
20 * membase is an 'ioremapped' cookie.
21 */
22#include <linux/config.h>
23
24#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25#define SUPPORT_SYSRQ
26#endif
27
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/ioport.h>
31#include <linux/init.h>
32#include <linux/console.h>
33#include <linux/sysrq.h>
34#include <linux/mca.h>
35#include <linux/delay.h>
36#include <linux/device.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39#include <linux/serial_reg.h>
40#include <linux/serial_core.h>
41#include <linux/serial.h>
42#include <linux/serial_8250.h>
43
44#include <asm/io.h>
45#include <asm/irq.h>
46
47#include "8250.h"
48
49/*
50 * Configuration:
51 * share_irqs - whether we pass SA_SHIRQ to request_irq(). This option
52 * is unsafe when used on edge-triggered interrupts.
53 */
54unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
55
56/*
57 * Debugging.
58 */
59#if 0
60#define DEBUG_AUTOCONF(fmt...) printk(fmt)
61#else
62#define DEBUG_AUTOCONF(fmt...) do { } while (0)
63#endif
64
65#if 0
66#define DEBUG_INTR(fmt...) printk(fmt)
67#else
68#define DEBUG_INTR(fmt...) do { } while (0)
69#endif
70
71#define PASS_LIMIT 256
72
73/*
74 * We default to IRQ0 for the "no irq" hack. Some
75 * machine types want others as well - they're free
76 * to redefine this in their header file.
77 */
78#define is_real_interrupt(irq) ((irq) != 0)
79
80/*
81 * This converts from our new CONFIG_ symbols to the symbols
82 * that asm/serial.h expects. You _NEED_ to comment out the
83 * linux/config.h include contained inside asm/serial.h for
84 * this to work.
85 */
86#undef CONFIG_SERIAL_MANY_PORTS
87#undef CONFIG_SERIAL_DETECT_IRQ
88#undef CONFIG_SERIAL_MULTIPORT
89#undef CONFIG_HUB6
90
91#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
92#define CONFIG_SERIAL_DETECT_IRQ 1
93#endif
94#ifdef CONFIG_SERIAL_8250_MULTIPORT
95#define CONFIG_SERIAL_MULTIPORT 1
96#endif
97#ifdef CONFIG_SERIAL_8250_MANY_PORTS
98#define CONFIG_SERIAL_MANY_PORTS 1
99#endif
100
101/*
102 * HUB6 is always on. This will be removed once the header
103 * files have been cleaned.
104 */
105#define CONFIG_HUB6 1
106
107#include <asm/serial.h>
108
109/*
110 * SERIAL_PORT_DFNS tells us about built-in ports that have no
111 * standard enumeration mechanism. Platforms that can find all
112 * serial ports via mechanisms like ACPI or PCI need not supply it.
113 */
114#ifndef SERIAL_PORT_DFNS
115#define SERIAL_PORT_DFNS
116#endif
117
118static struct old_serial_port old_serial_port[] = {
119 SERIAL_PORT_DFNS /* defined in asm/serial.h */
120};
121
122#define UART_NR (ARRAY_SIZE(old_serial_port) + CONFIG_SERIAL_8250_NR_UARTS)
123
124#ifdef CONFIG_SERIAL_8250_RSA
125
126#define PORT_RSA_MAX 4
127static unsigned long probe_rsa[PORT_RSA_MAX];
128static unsigned int probe_rsa_count;
129#endif /* CONFIG_SERIAL_8250_RSA */
130
131struct uart_8250_port {
132 struct uart_port port;
133 struct timer_list timer; /* "no irq" timer */
134 struct list_head list; /* ports on this IRQ */
135 unsigned int capabilities; /* port capabilities */
136 unsigned int tx_loadsz; /* transmit fifo load size */
137 unsigned short rev;
138 unsigned char acr;
139 unsigned char ier;
140 unsigned char lcr;
141 unsigned char mcr;
142 unsigned char mcr_mask; /* mask of user bits */
143 unsigned char mcr_force; /* mask of forced bits */
144 unsigned char lsr_break_flag;
145
146 /*
147 * We provide a per-port pm hook.
148 */
149 void (*pm)(struct uart_port *port,
150 unsigned int state, unsigned int old);
151};
152
153struct irq_info {
154 spinlock_t lock;
155 struct list_head *head;
156};
157
158static struct irq_info irq_lists[NR_IRQS];
159
160/*
161 * Here we define the default xmit fifo size used for each type of UART.
162 */
163static const struct serial8250_config uart_config[] = {
164 [PORT_UNKNOWN] = {
165 .name = "unknown",
166 .fifo_size = 1,
167 .tx_loadsz = 1,
168 },
169 [PORT_8250] = {
170 .name = "8250",
171 .fifo_size = 1,
172 .tx_loadsz = 1,
173 },
174 [PORT_16450] = {
175 .name = "16450",
176 .fifo_size = 1,
177 .tx_loadsz = 1,
178 },
179 [PORT_16550] = {
180 .name = "16550",
181 .fifo_size = 1,
182 .tx_loadsz = 1,
183 },
184 [PORT_16550A] = {
185 .name = "16550A",
186 .fifo_size = 16,
187 .tx_loadsz = 16,
188 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
189 .flags = UART_CAP_FIFO,
190 },
191 [PORT_CIRRUS] = {
192 .name = "Cirrus",
193 .fifo_size = 1,
194 .tx_loadsz = 1,
195 },
196 [PORT_16650] = {
197 .name = "ST16650",
198 .fifo_size = 1,
199 .tx_loadsz = 1,
200 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
201 },
202 [PORT_16650V2] = {
203 .name = "ST16650V2",
204 .fifo_size = 32,
205 .tx_loadsz = 16,
206 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
207 UART_FCR_T_TRIG_00,
208 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
209 },
210 [PORT_16750] = {
211 .name = "TI16750",
212 .fifo_size = 64,
213 .tx_loadsz = 64,
214 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
215 UART_FCR7_64BYTE,
216 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
217 },
218 [PORT_STARTECH] = {
219 .name = "Startech",
220 .fifo_size = 1,
221 .tx_loadsz = 1,
222 },
223 [PORT_16C950] = {
224 .name = "16C950/954",
225 .fifo_size = 128,
226 .tx_loadsz = 128,
227 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
228 .flags = UART_CAP_FIFO,
229 },
230 [PORT_16654] = {
231 .name = "ST16654",
232 .fifo_size = 64,
233 .tx_loadsz = 32,
234 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
235 UART_FCR_T_TRIG_10,
236 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
237 },
238 [PORT_16850] = {
239 .name = "XR16850",
240 .fifo_size = 128,
241 .tx_loadsz = 128,
242 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
243 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
244 },
245 [PORT_RSA] = {
246 .name = "RSA",
247 .fifo_size = 2048,
248 .tx_loadsz = 2048,
249 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
250 .flags = UART_CAP_FIFO,
251 },
252 [PORT_NS16550A] = {
253 .name = "NS16550A",
254 .fifo_size = 16,
255 .tx_loadsz = 16,
256 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
257 .flags = UART_CAP_FIFO | UART_NATSEMI,
258 },
259 [PORT_XSCALE] = {
260 .name = "XScale",
261 .fifo_size = 32,
262 .tx_loadsz = 32,
263 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
264 .flags = UART_CAP_FIFO | UART_CAP_UUE,
265 },
266};
267
268static _INLINE_ unsigned int serial_in(struct uart_8250_port *up, int offset)
269{
270 offset <<= up->port.regshift;
271
272 switch (up->port.iotype) {
273 case UPIO_HUB6:
274 outb(up->port.hub6 - 1 + offset, up->port.iobase);
275 return inb(up->port.iobase + 1);
276
277 case UPIO_MEM:
278 return readb(up->port.membase + offset);
279
280 case UPIO_MEM32:
281 return readl(up->port.membase + offset);
282
283 default:
284 return inb(up->port.iobase + offset);
285 }
286}
287
288static _INLINE_ void
289serial_out(struct uart_8250_port *up, int offset, int value)
290{
291 offset <<= up->port.regshift;
292
293 switch (up->port.iotype) {
294 case UPIO_HUB6:
295 outb(up->port.hub6 - 1 + offset, up->port.iobase);
296 outb(value, up->port.iobase + 1);
297 break;
298
299 case UPIO_MEM:
300 writeb(value, up->port.membase + offset);
301 break;
302
303 case UPIO_MEM32:
304 writel(value, up->port.membase + offset);
305 break;
306
307 default:
308 outb(value, up->port.iobase + offset);
309 }
310}
311
312/*
313 * We used to support using pause I/O for certain machines. We
314 * haven't supported this for a while, but just in case it's badly
315 * needed for certain old 386 machines, I've left these #define's
316 * in....
317 */
318#define serial_inp(up, offset) serial_in(up, offset)
319#define serial_outp(up, offset, value) serial_out(up, offset, value)
320
321
322/*
323 * For the 16C950
324 */
325static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
326{
327 serial_out(up, UART_SCR, offset);
328 serial_out(up, UART_ICR, value);
329}
330
331static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
332{
333 unsigned int value;
334
335 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
336 serial_out(up, UART_SCR, offset);
337 value = serial_in(up, UART_ICR);
338 serial_icr_write(up, UART_ACR, up->acr);
339
340 return value;
341}
342
343/*
344 * FIFO support.
345 */
346static inline void serial8250_clear_fifos(struct uart_8250_port *p)
347{
348 if (p->capabilities & UART_CAP_FIFO) {
349 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
350 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
351 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
352 serial_outp(p, UART_FCR, 0);
353 }
354}
355
356/*
357 * IER sleep support. UARTs which have EFRs need the "extended
358 * capability" bit enabled. Note that on XR16C850s, we need to
359 * reset LCR to write to IER.
360 */
361static inline void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
362{
363 if (p->capabilities & UART_CAP_SLEEP) {
364 if (p->capabilities & UART_CAP_EFR) {
365 serial_outp(p, UART_LCR, 0xBF);
366 serial_outp(p, UART_EFR, UART_EFR_ECB);
367 serial_outp(p, UART_LCR, 0);
368 }
369 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
370 if (p->capabilities & UART_CAP_EFR) {
371 serial_outp(p, UART_LCR, 0xBF);
372 serial_outp(p, UART_EFR, 0);
373 serial_outp(p, UART_LCR, 0);
374 }
375 }
376}
377
378#ifdef CONFIG_SERIAL_8250_RSA
379/*
380 * Attempts to turn on the RSA FIFO. Returns zero on failure.
381 * We set the port uart clock rate if we succeed.
382 */
383static int __enable_rsa(struct uart_8250_port *up)
384{
385 unsigned char mode;
386 int result;
387
388 mode = serial_inp(up, UART_RSA_MSR);
389 result = mode & UART_RSA_MSR_FIFO;
390
391 if (!result) {
392 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
393 mode = serial_inp(up, UART_RSA_MSR);
394 result = mode & UART_RSA_MSR_FIFO;
395 }
396
397 if (result)
398 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
399
400 return result;
401}
402
403static void enable_rsa(struct uart_8250_port *up)
404{
405 if (up->port.type == PORT_RSA) {
406 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
407 spin_lock_irq(&up->port.lock);
408 __enable_rsa(up);
409 spin_unlock_irq(&up->port.lock);
410 }
411 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
412 serial_outp(up, UART_RSA_FRR, 0);
413 }
414}
415
416/*
417 * Attempts to turn off the RSA FIFO. Returns zero on failure.
418 * It is unknown why interrupts were disabled in here. However,
419 * the caller is expected to preserve this behaviour by grabbing
420 * the spinlock before calling this function.
421 */
422static void disable_rsa(struct uart_8250_port *up)
423{
424 unsigned char mode;
425 int result;
426
427 if (up->port.type == PORT_RSA &&
428 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
429 spin_lock_irq(&up->port.lock);
430
431 mode = serial_inp(up, UART_RSA_MSR);
432 result = !(mode & UART_RSA_MSR_FIFO);
433
434 if (!result) {
435 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
436 mode = serial_inp(up, UART_RSA_MSR);
437 result = !(mode & UART_RSA_MSR_FIFO);
438 }
439
440 if (result)
441 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
442 spin_unlock_irq(&up->port.lock);
443 }
444}
445#endif /* CONFIG_SERIAL_8250_RSA */
446
447/*
448 * This is a quickie test to see how big the FIFO is.
449 * It doesn't work at all the time, more's the pity.
450 */
451static int size_fifo(struct uart_8250_port *up)
452{
453 unsigned char old_fcr, old_mcr, old_dll, old_dlm, old_lcr;
454 int count;
455
456 old_lcr = serial_inp(up, UART_LCR);
457 serial_outp(up, UART_LCR, 0);
458 old_fcr = serial_inp(up, UART_FCR);
459 old_mcr = serial_inp(up, UART_MCR);
460 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
461 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
462 serial_outp(up, UART_MCR, UART_MCR_LOOP);
463 serial_outp(up, UART_LCR, UART_LCR_DLAB);
464 old_dll = serial_inp(up, UART_DLL);
465 old_dlm = serial_inp(up, UART_DLM);
466 serial_outp(up, UART_DLL, 0x01);
467 serial_outp(up, UART_DLM, 0x00);
468 serial_outp(up, UART_LCR, 0x03);
469 for (count = 0; count < 256; count++)
470 serial_outp(up, UART_TX, count);
471 mdelay(20);/* FIXME - schedule_timeout */
472 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
473 (count < 256); count++)
474 serial_inp(up, UART_RX);
475 serial_outp(up, UART_FCR, old_fcr);
476 serial_outp(up, UART_MCR, old_mcr);
477 serial_outp(up, UART_LCR, UART_LCR_DLAB);
478 serial_outp(up, UART_DLL, old_dll);
479 serial_outp(up, UART_DLM, old_dlm);
480 serial_outp(up, UART_LCR, old_lcr);
481
482 return count;
483}
484
485/*
486 * Read UART ID using the divisor method - set DLL and DLM to zero
487 * and the revision will be in DLL and device type in DLM. We
488 * preserve the device state across this.
489 */
490static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
491{
492 unsigned char old_dll, old_dlm, old_lcr;
493 unsigned int id;
494
495 old_lcr = serial_inp(p, UART_LCR);
496 serial_outp(p, UART_LCR, UART_LCR_DLAB);
497
498 old_dll = serial_inp(p, UART_DLL);
499 old_dlm = serial_inp(p, UART_DLM);
500
501 serial_outp(p, UART_DLL, 0);
502 serial_outp(p, UART_DLM, 0);
503
504 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
505
506 serial_outp(p, UART_DLL, old_dll);
507 serial_outp(p, UART_DLM, old_dlm);
508 serial_outp(p, UART_LCR, old_lcr);
509
510 return id;
511}
512
513/*
514 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
515 * When this function is called we know it is at least a StarTech
516 * 16650 V2, but it might be one of several StarTech UARTs, or one of
517 * its clones. (We treat the broken original StarTech 16650 V1 as a
518 * 16550, and why not? Startech doesn't seem to even acknowledge its
519 * existence.)
520 *
521 * What evil have men's minds wrought...
522 */
523static void autoconfig_has_efr(struct uart_8250_port *up)
524{
525 unsigned int id1, id2, id3, rev;
526
527 /*
528 * Everything with an EFR has SLEEP
529 */
530 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
531
532 /*
533 * First we check to see if it's an Oxford Semiconductor UART.
534 *
535 * If we have to do this here because some non-National
536 * Semiconductor clone chips lock up if you try writing to the
537 * LSR register (which serial_icr_read does)
538 */
539
540 /*
541 * Check for Oxford Semiconductor 16C950.
542 *
543 * EFR [4] must be set else this test fails.
544 *
545 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
546 * claims that it's needed for 952 dual UART's (which are not
547 * recommended for new designs).
548 */
549 up->acr = 0;
550 serial_out(up, UART_LCR, 0xBF);
551 serial_out(up, UART_EFR, UART_EFR_ECB);
552 serial_out(up, UART_LCR, 0x00);
553 id1 = serial_icr_read(up, UART_ID1);
554 id2 = serial_icr_read(up, UART_ID2);
555 id3 = serial_icr_read(up, UART_ID3);
556 rev = serial_icr_read(up, UART_REV);
557
558 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
559
560 if (id1 == 0x16 && id2 == 0xC9 &&
561 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
562 up->port.type = PORT_16C950;
563 up->rev = rev | (id3 << 8);
564 return;
565 }
566
567 /*
568 * We check for a XR16C850 by setting DLL and DLM to 0, and then
569 * reading back DLL and DLM. The chip type depends on the DLM
570 * value read back:
571 * 0x10 - XR16C850 and the DLL contains the chip revision.
572 * 0x12 - XR16C2850.
573 * 0x14 - XR16C854.
574 */
575 id1 = autoconfig_read_divisor_id(up);
576 DEBUG_AUTOCONF("850id=%04x ", id1);
577
578 id2 = id1 >> 8;
579 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
580 if (id2 == 0x10)
581 up->rev = id1 & 255;
582 up->port.type = PORT_16850;
583 return;
584 }
585
586 /*
587 * It wasn't an XR16C850.
588 *
589 * We distinguish between the '654 and the '650 by counting
590 * how many bytes are in the FIFO. I'm using this for now,
591 * since that's the technique that was sent to me in the
592 * serial driver update, but I'm not convinced this works.
593 * I've had problems doing this in the past. -TYT
594 */
595 if (size_fifo(up) == 64)
596 up->port.type = PORT_16654;
597 else
598 up->port.type = PORT_16650V2;
599}
600
601/*
602 * We detected a chip without a FIFO. Only two fall into
603 * this category - the original 8250 and the 16450. The
604 * 16450 has a scratch register (accessible with LCR=0)
605 */
606static void autoconfig_8250(struct uart_8250_port *up)
607{
608 unsigned char scratch, status1, status2;
609
610 up->port.type = PORT_8250;
611
612 scratch = serial_in(up, UART_SCR);
613 serial_outp(up, UART_SCR, 0xa5);
614 status1 = serial_in(up, UART_SCR);
615 serial_outp(up, UART_SCR, 0x5a);
616 status2 = serial_in(up, UART_SCR);
617 serial_outp(up, UART_SCR, scratch);
618
619 if (status1 == 0xa5 && status2 == 0x5a)
620 up->port.type = PORT_16450;
621}
622
623static int broken_efr(struct uart_8250_port *up)
624{
625 /*
626 * Exar ST16C2550 "A2" devices incorrectly detect as
627 * having an EFR, and report an ID of 0x0201. See
628 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
629 */
630 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
631 return 1;
632
633 return 0;
634}
635
636/*
637 * We know that the chip has FIFOs. Does it have an EFR? The
638 * EFR is located in the same register position as the IIR and
639 * we know the top two bits of the IIR are currently set. The
640 * EFR should contain zero. Try to read the EFR.
641 */
642static void autoconfig_16550a(struct uart_8250_port *up)
643{
644 unsigned char status1, status2;
645 unsigned int iersave;
646
647 up->port.type = PORT_16550A;
648 up->capabilities |= UART_CAP_FIFO;
649
650 /*
651 * Check for presence of the EFR when DLAB is set.
652 * Only ST16C650V1 UARTs pass this test.
653 */
654 serial_outp(up, UART_LCR, UART_LCR_DLAB);
655 if (serial_in(up, UART_EFR) == 0) {
656 serial_outp(up, UART_EFR, 0xA8);
657 if (serial_in(up, UART_EFR) != 0) {
658 DEBUG_AUTOCONF("EFRv1 ");
659 up->port.type = PORT_16650;
660 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
661 } else {
662 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
663 }
664 serial_outp(up, UART_EFR, 0);
665 return;
666 }
667
668 /*
669 * Maybe it requires 0xbf to be written to the LCR.
670 * (other ST16C650V2 UARTs, TI16C752A, etc)
671 */
672 serial_outp(up, UART_LCR, 0xBF);
673 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
674 DEBUG_AUTOCONF("EFRv2 ");
675 autoconfig_has_efr(up);
676 return;
677 }
678
679 /*
680 * Check for a National Semiconductor SuperIO chip.
681 * Attempt to switch to bank 2, read the value of the LOOP bit
682 * from EXCR1. Switch back to bank 0, change it in MCR. Then
683 * switch back to bank 2, read it from EXCR1 again and check
684 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
685 * On PowerPC we don't want to change baud_base, as we have
686 * a number of different divisors. -- Tom Rini
687 */
688 serial_outp(up, UART_LCR, 0);
689 status1 = serial_in(up, UART_MCR);
690 serial_outp(up, UART_LCR, 0xE0);
691 status2 = serial_in(up, 0x02); /* EXCR1 */
692
693 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
694 serial_outp(up, UART_LCR, 0);
695 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
696 serial_outp(up, UART_LCR, 0xE0);
697 status2 = serial_in(up, 0x02); /* EXCR1 */
698 serial_outp(up, UART_LCR, 0);
699 serial_outp(up, UART_MCR, status1);
700
701 if ((status2 ^ status1) & UART_MCR_LOOP) {
702#ifndef CONFIG_PPC
703 serial_outp(up, UART_LCR, 0xE0);
704 status1 = serial_in(up, 0x04); /* EXCR1 */
705 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
706 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
707 serial_outp(up, 0x04, status1);
708 serial_outp(up, UART_LCR, 0);
709 up->port.uartclk = 921600*16;
710#endif
711
712 up->port.type = PORT_NS16550A;
713 up->capabilities |= UART_NATSEMI;
714 return;
715 }
716 }
717
718 /*
719 * No EFR. Try to detect a TI16750, which only sets bit 5 of
720 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
721 * Try setting it with and without DLAB set. Cheap clones
722 * set bit 5 without DLAB set.
723 */
724 serial_outp(up, UART_LCR, 0);
725 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
726 status1 = serial_in(up, UART_IIR) >> 5;
727 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
728 serial_outp(up, UART_LCR, UART_LCR_DLAB);
729 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
730 status2 = serial_in(up, UART_IIR) >> 5;
731 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
732 serial_outp(up, UART_LCR, 0);
733
734 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
735
736 if (status1 == 6 && status2 == 7) {
737 up->port.type = PORT_16750;
738 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
739 return;
740 }
741
742 /*
743 * Try writing and reading the UART_IER_UUE bit (b6).
744 * If it works, this is probably one of the Xscale platform's
745 * internal UARTs.
746 * We're going to explicitly set the UUE bit to 0 before
747 * trying to write and read a 1 just to make sure it's not
748 * already a 1 and maybe locked there before we even start start.
749 */
750 iersave = serial_in(up, UART_IER);
751 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
752 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
753 /*
754 * OK it's in a known zero state, try writing and reading
755 * without disturbing the current state of the other bits.
756 */
757 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
758 if (serial_in(up, UART_IER) & UART_IER_UUE) {
759 /*
760 * It's an Xscale.
761 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
762 */
763 DEBUG_AUTOCONF("Xscale ");
764 up->port.type = PORT_XSCALE;
765 up->capabilities |= UART_CAP_UUE;
766 return;
767 }
768 } else {
769 /*
770 * If we got here we couldn't force the IER_UUE bit to 0.
771 * Log it and continue.
772 */
773 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
774 }
775 serial_outp(up, UART_IER, iersave);
776}
777
778/*
779 * This routine is called by rs_init() to initialize a specific serial
780 * port. It determines what type of UART chip this serial port is
781 * using: 8250, 16450, 16550, 16550A. The important question is
782 * whether or not this UART is a 16550A or not, since this will
783 * determine whether or not we can use its FIFO features or not.
784 */
785static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
786{
787 unsigned char status1, scratch, scratch2, scratch3;
788 unsigned char save_lcr, save_mcr;
789 unsigned long flags;
790
791 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
792 return;
793
794 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
795 up->port.line, up->port.iobase, up->port.membase);
796
797 /*
798 * We really do need global IRQs disabled here - we're going to
799 * be frobbing the chips IRQ enable register to see if it exists.
800 */
801 spin_lock_irqsave(&up->port.lock, flags);
802// save_flags(flags); cli();
803
804 up->capabilities = 0;
805
806 if (!(up->port.flags & UPF_BUGGY_UART)) {
807 /*
808 * Do a simple existence test first; if we fail this,
809 * there's no point trying anything else.
810 *
811 * 0x80 is used as a nonsense port to prevent against
812 * false positives due to ISA bus float. The
813 * assumption is that 0x80 is a non-existent port;
814 * which should be safe since include/asm/io.h also
815 * makes this assumption.
816 *
817 * Note: this is safe as long as MCR bit 4 is clear
818 * and the device is in "PC" mode.
819 */
820 scratch = serial_inp(up, UART_IER);
821 serial_outp(up, UART_IER, 0);
822#ifdef __i386__
823 outb(0xff, 0x080);
824#endif
825 scratch2 = serial_inp(up, UART_IER);
826 serial_outp(up, UART_IER, 0x0F);
827#ifdef __i386__
828 outb(0, 0x080);
829#endif
830 scratch3 = serial_inp(up, UART_IER);
831 serial_outp(up, UART_IER, scratch);
832 if (scratch2 != 0 || scratch3 != 0x0F) {
833 /*
834 * We failed; there's nothing here
835 */
836 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
837 scratch2, scratch3);
838 goto out;
839 }
840 }
841
842 save_mcr = serial_in(up, UART_MCR);
843 save_lcr = serial_in(up, UART_LCR);
844
845 /*
846 * Check to see if a UART is really there. Certain broken
847 * internal modems based on the Rockwell chipset fail this
848 * test, because they apparently don't implement the loopback
849 * test mode. So this test is skipped on the COM 1 through
850 * COM 4 ports. This *should* be safe, since no board
851 * manufacturer would be stupid enough to design a board
852 * that conflicts with COM 1-4 --- we hope!
853 */
854 if (!(up->port.flags & UPF_SKIP_TEST)) {
855 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
856 status1 = serial_inp(up, UART_MSR) & 0xF0;
857 serial_outp(up, UART_MCR, save_mcr);
858 if (status1 != 0x90) {
859 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
860 status1);
861 goto out;
862 }
863 }
864
865 /*
866 * We're pretty sure there's a port here. Lets find out what
867 * type of port it is. The IIR top two bits allows us to find
868 * out if its 8250 or 16450, 16550, 16550A or later. This
869 * determines what we test for next.
870 *
871 * We also initialise the EFR (if any) to zero for later. The
872 * EFR occupies the same register location as the FCR and IIR.
873 */
874 serial_outp(up, UART_LCR, 0xBF);
875 serial_outp(up, UART_EFR, 0);
876 serial_outp(up, UART_LCR, 0);
877
878 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
879 scratch = serial_in(up, UART_IIR) >> 6;
880
881 DEBUG_AUTOCONF("iir=%d ", scratch);
882
883 switch (scratch) {
884 case 0:
885 autoconfig_8250(up);
886 break;
887 case 1:
888 up->port.type = PORT_UNKNOWN;
889 break;
890 case 2:
891 up->port.type = PORT_16550;
892 break;
893 case 3:
894 autoconfig_16550a(up);
895 break;
896 }
897
898#ifdef CONFIG_SERIAL_8250_RSA
899 /*
900 * Only probe for RSA ports if we got the region.
901 */
902 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
903 int i;
904
905 for (i = 0 ; i < probe_rsa_count; ++i) {
906 if (probe_rsa[i] == up->port.iobase &&
907 __enable_rsa(up)) {
908 up->port.type = PORT_RSA;
909 break;
910 }
911 }
912 }
913#endif
914 serial_outp(up, UART_LCR, save_lcr);
915
916 if (up->capabilities != uart_config[up->port.type].flags) {
917 printk(KERN_WARNING
918 "ttyS%d: detected caps %08x should be %08x\n",
919 up->port.line, up->capabilities,
920 uart_config[up->port.type].flags);
921 }
922
923 up->port.fifosize = uart_config[up->port.type].fifo_size;
924 up->capabilities = uart_config[up->port.type].flags;
925 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
926
927 if (up->port.type == PORT_UNKNOWN)
928 goto out;
929
930 /*
931 * Reset the UART.
932 */
933#ifdef CONFIG_SERIAL_8250_RSA
934 if (up->port.type == PORT_RSA)
935 serial_outp(up, UART_RSA_FRR, 0);
936#endif
937 serial_outp(up, UART_MCR, save_mcr);
938 serial8250_clear_fifos(up);
939 (void)serial_in(up, UART_RX);
940 serial_outp(up, UART_IER, 0);
941
942 out:
943 spin_unlock_irqrestore(&up->port.lock, flags);
944// restore_flags(flags);
945 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
946}
947
948static void autoconfig_irq(struct uart_8250_port *up)
949{
950 unsigned char save_mcr, save_ier;
951 unsigned char save_ICP = 0;
952 unsigned int ICP = 0;
953 unsigned long irqs;
954 int irq;
955
956 if (up->port.flags & UPF_FOURPORT) {
957 ICP = (up->port.iobase & 0xfe0) | 0x1f;
958 save_ICP = inb_p(ICP);
959 outb_p(0x80, ICP);
960 (void) inb_p(ICP);
961 }
962
963 /* forget possible initially masked and pending IRQ */
964 probe_irq_off(probe_irq_on());
965 save_mcr = serial_inp(up, UART_MCR);
966 save_ier = serial_inp(up, UART_IER);
967 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
968
969 irqs = probe_irq_on();
970 serial_outp(up, UART_MCR, 0);
971 udelay (10);
972 if (up->port.flags & UPF_FOURPORT) {
973 serial_outp(up, UART_MCR,
974 UART_MCR_DTR | UART_MCR_RTS);
975 } else {
976 serial_outp(up, UART_MCR,
977 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
978 }
979 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
980 (void)serial_inp(up, UART_LSR);
981 (void)serial_inp(up, UART_RX);
982 (void)serial_inp(up, UART_IIR);
983 (void)serial_inp(up, UART_MSR);
984 serial_outp(up, UART_TX, 0xFF);
985 udelay (20);
986 irq = probe_irq_off(irqs);
987
988 serial_outp(up, UART_MCR, save_mcr);
989 serial_outp(up, UART_IER, save_ier);
990
991 if (up->port.flags & UPF_FOURPORT)
992 outb_p(save_ICP, ICP);
993
994 up->port.irq = (irq > 0) ? irq : 0;
995}
996
997static void serial8250_stop_tx(struct uart_port *port, unsigned int tty_stop)
998{
999 struct uart_8250_port *up = (struct uart_8250_port *)port;
1000
1001 if (up->ier & UART_IER_THRI) {
1002 up->ier &= ~UART_IER_THRI;
1003 serial_out(up, UART_IER, up->ier);
1004 }
1005
1006 /*
1007 * We only do this from uart_stop - if we run out of
1008 * characters to send, we don't want to prevent the
1009 * FIFO from emptying.
1010 */
1011 if (up->port.type == PORT_16C950 && tty_stop) {
1012 up->acr |= UART_ACR_TXDIS;
1013 serial_icr_write(up, UART_ACR, up->acr);
1014 }
1015}
1016
1017static void serial8250_start_tx(struct uart_port *port, unsigned int tty_start)
1018{
1019 struct uart_8250_port *up = (struct uart_8250_port *)port;
1020
1021 if (!(up->ier & UART_IER_THRI)) {
1022 up->ier |= UART_IER_THRI;
1023 serial_out(up, UART_IER, up->ier);
1024 }
1025 /*
1026 * We only do this from uart_start
1027 */
1028 if (tty_start && up->port.type == PORT_16C950) {
1029 up->acr &= ~UART_ACR_TXDIS;
1030 serial_icr_write(up, UART_ACR, up->acr);
1031 }
1032}
1033
1034static void serial8250_stop_rx(struct uart_port *port)
1035{
1036 struct uart_8250_port *up = (struct uart_8250_port *)port;
1037
1038 up->ier &= ~UART_IER_RLSI;
1039 up->port.read_status_mask &= ~UART_LSR_DR;
1040 serial_out(up, UART_IER, up->ier);
1041}
1042
1043static void serial8250_enable_ms(struct uart_port *port)
1044{
1045 struct uart_8250_port *up = (struct uart_8250_port *)port;
1046
1047 up->ier |= UART_IER_MSI;
1048 serial_out(up, UART_IER, up->ier);
1049}
1050
1051static _INLINE_ void
1052receive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs)
1053{
1054 struct tty_struct *tty = up->port.info->tty;
1055 unsigned char ch, lsr = *status;
1056 int max_count = 256;
1057 char flag;
1058
1059 do {
1060 /* The following is not allowed by the tty layer and
1061 unsafe. It should be fixed ASAP */
1062 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
1063 if (tty->low_latency) {
1064 spin_unlock(&up->port.lock);
1065 tty_flip_buffer_push(tty);
1066 spin_lock(&up->port.lock);
1067 }
1068 /* If this failed then we will throw away the
1069 bytes but must do so to clear interrupts */
1070 }
1071 ch = serial_inp(up, UART_RX);
1072 flag = TTY_NORMAL;
1073 up->port.icount.rx++;
1074
1075#ifdef CONFIG_SERIAL_8250_CONSOLE
1076 /*
1077 * Recover the break flag from console xmit
1078 */
1079 if (up->port.line == up->port.cons->index) {
1080 lsr |= up->lsr_break_flag;
1081 up->lsr_break_flag = 0;
1082 }
1083#endif
1084
1085 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
1086 UART_LSR_FE | UART_LSR_OE))) {
1087 /*
1088 * For statistics only
1089 */
1090 if (lsr & UART_LSR_BI) {
1091 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1092 up->port.icount.brk++;
1093 /*
1094 * We do the SysRQ and SAK checking
1095 * here because otherwise the break
1096 * may get masked by ignore_status_mask
1097 * or read_status_mask.
1098 */
1099 if (uart_handle_break(&up->port))
1100 goto ignore_char;
1101 } else if (lsr & UART_LSR_PE)
1102 up->port.icount.parity++;
1103 else if (lsr & UART_LSR_FE)
1104 up->port.icount.frame++;
1105 if (lsr & UART_LSR_OE)
1106 up->port.icount.overrun++;
1107
1108 /*
1109 * Mask off conditions which should be ingored.
1110 */
1111 lsr &= up->port.read_status_mask;
1112
1113 if (lsr & UART_LSR_BI) {
1114 DEBUG_INTR("handling break....");
1115 flag = TTY_BREAK;
1116 } else if (lsr & UART_LSR_PE)
1117 flag = TTY_PARITY;
1118 else if (lsr & UART_LSR_FE)
1119 flag = TTY_FRAME;
1120 }
1121 if (uart_handle_sysrq_char(&up->port, ch, regs))
1122 goto ignore_char;
1123 if ((lsr & up->port.ignore_status_mask) == 0) {
1124 tty_insert_flip_char(tty, ch, flag);
1125 }
1126 if ((lsr & UART_LSR_OE) &&
1127 tty->flip.count < TTY_FLIPBUF_SIZE) {
1128 /*
1129 * Overrun is special, since it's reported
1130 * immediately, and doesn't affect the current
1131 * character.
1132 */
1133 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1134 }
1135 ignore_char:
1136 lsr = serial_inp(up, UART_LSR);
1137 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
1138 spin_unlock(&up->port.lock);
1139 tty_flip_buffer_push(tty);
1140 spin_lock(&up->port.lock);
1141 *status = lsr;
1142}
1143
1144static _INLINE_ void transmit_chars(struct uart_8250_port *up)
1145{
1146 struct circ_buf *xmit = &up->port.info->xmit;
1147 int count;
1148
1149 if (up->port.x_char) {
1150 serial_outp(up, UART_TX, up->port.x_char);
1151 up->port.icount.tx++;
1152 up->port.x_char = 0;
1153 return;
1154 }
1155 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
1156 serial8250_stop_tx(&up->port, 0);
1157 return;
1158 }
1159
1160 count = up->tx_loadsz;
1161 do {
1162 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1163 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1164 up->port.icount.tx++;
1165 if (uart_circ_empty(xmit))
1166 break;
1167 } while (--count > 0);
1168
1169 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1170 uart_write_wakeup(&up->port);
1171
1172 DEBUG_INTR("THRE...");
1173
1174 if (uart_circ_empty(xmit))
1175 serial8250_stop_tx(&up->port, 0);
1176}
1177
1178static _INLINE_ void check_modem_status(struct uart_8250_port *up)
1179{
1180 int status;
1181
1182 status = serial_in(up, UART_MSR);
1183
1184 if ((status & UART_MSR_ANY_DELTA) == 0)
1185 return;
1186
1187 if (status & UART_MSR_TERI)
1188 up->port.icount.rng++;
1189 if (status & UART_MSR_DDSR)
1190 up->port.icount.dsr++;
1191 if (status & UART_MSR_DDCD)
1192 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1193 if (status & UART_MSR_DCTS)
1194 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1195
1196 wake_up_interruptible(&up->port.info->delta_msr_wait);
1197}
1198
1199/*
1200 * This handles the interrupt from one port.
1201 */
1202static inline void
1203serial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs)
1204{
1205 unsigned int status = serial_inp(up, UART_LSR);
1206
1207 DEBUG_INTR("status = %x...", status);
1208
1209 if (status & UART_LSR_DR)
1210 receive_chars(up, &status, regs);
1211 check_modem_status(up);
1212 if (status & UART_LSR_THRE)
1213 transmit_chars(up);
1214}
1215
1216/*
1217 * This is the serial driver's interrupt routine.
1218 *
1219 * Arjan thinks the old way was overly complex, so it got simplified.
1220 * Alan disagrees, saying that need the complexity to handle the weird
1221 * nature of ISA shared interrupts. (This is a special exception.)
1222 *
1223 * In order to handle ISA shared interrupts properly, we need to check
1224 * that all ports have been serviced, and therefore the ISA interrupt
1225 * line has been de-asserted.
1226 *
1227 * This means we need to loop through all ports. checking that they
1228 * don't have an interrupt pending.
1229 */
1230static irqreturn_t serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1231{
1232 struct irq_info *i = dev_id;
1233 struct list_head *l, *end = NULL;
1234 int pass_counter = 0, handled = 0;
1235
1236 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1237
1238 spin_lock(&i->lock);
1239
1240 l = i->head;
1241 do {
1242 struct uart_8250_port *up;
1243 unsigned int iir;
1244
1245 up = list_entry(l, struct uart_8250_port, list);
1246
1247 iir = serial_in(up, UART_IIR);
1248 if (!(iir & UART_IIR_NO_INT)) {
1249 spin_lock(&up->port.lock);
1250 serial8250_handle_port(up, regs);
1251 spin_unlock(&up->port.lock);
1252
1253 handled = 1;
1254
1255 end = NULL;
1256 } else if (end == NULL)
1257 end = l;
1258
1259 l = l->next;
1260
1261 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1262 /* If we hit this, we're dead. */
1263 printk(KERN_ERR "serial8250: too much work for "
1264 "irq%d\n", irq);
1265 break;
1266 }
1267 } while (l != end);
1268
1269 spin_unlock(&i->lock);
1270
1271 DEBUG_INTR("end.\n");
1272
1273 return IRQ_RETVAL(handled);
1274}
1275
1276/*
1277 * To support ISA shared interrupts, we need to have one interrupt
1278 * handler that ensures that the IRQ line has been deasserted
1279 * before returning. Failing to do this will result in the IRQ
1280 * line being stuck active, and, since ISA irqs are edge triggered,
1281 * no more IRQs will be seen.
1282 */
1283static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1284{
1285 spin_lock_irq(&i->lock);
1286
1287 if (!list_empty(i->head)) {
1288 if (i->head == &up->list)
1289 i->head = i->head->next;
1290 list_del(&up->list);
1291 } else {
1292 BUG_ON(i->head != &up->list);
1293 i->head = NULL;
1294 }
1295
1296 spin_unlock_irq(&i->lock);
1297}
1298
1299static int serial_link_irq_chain(struct uart_8250_port *up)
1300{
1301 struct irq_info *i = irq_lists + up->port.irq;
1302 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
1303
1304 spin_lock_irq(&i->lock);
1305
1306 if (i->head) {
1307 list_add(&up->list, i->head);
1308 spin_unlock_irq(&i->lock);
1309
1310 ret = 0;
1311 } else {
1312 INIT_LIST_HEAD(&up->list);
1313 i->head = &up->list;
1314 spin_unlock_irq(&i->lock);
1315
1316 ret = request_irq(up->port.irq, serial8250_interrupt,
1317 irq_flags, "serial", i);
1318 if (ret < 0)
1319 serial_do_unlink(i, up);
1320 }
1321
1322 return ret;
1323}
1324
1325static void serial_unlink_irq_chain(struct uart_8250_port *up)
1326{
1327 struct irq_info *i = irq_lists + up->port.irq;
1328
1329 BUG_ON(i->head == NULL);
1330
1331 if (list_empty(i->head))
1332 free_irq(up->port.irq, i);
1333
1334 serial_do_unlink(i, up);
1335}
1336
1337/*
1338 * This function is used to handle ports that do not have an
1339 * interrupt. This doesn't work very well for 16450's, but gives
1340 * barely passable results for a 16550A. (Although at the expense
1341 * of much CPU overhead).
1342 */
1343static void serial8250_timeout(unsigned long data)
1344{
1345 struct uart_8250_port *up = (struct uart_8250_port *)data;
1346 unsigned int timeout;
1347 unsigned int iir;
1348
1349 iir = serial_in(up, UART_IIR);
1350 if (!(iir & UART_IIR_NO_INT)) {
1351 spin_lock(&up->port.lock);
1352 serial8250_handle_port(up, NULL);
1353 spin_unlock(&up->port.lock);
1354 }
1355
1356 timeout = up->port.timeout;
1357 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1358 mod_timer(&up->timer, jiffies + timeout);
1359}
1360
1361static unsigned int serial8250_tx_empty(struct uart_port *port)
1362{
1363 struct uart_8250_port *up = (struct uart_8250_port *)port;
1364 unsigned long flags;
1365 unsigned int ret;
1366
1367 spin_lock_irqsave(&up->port.lock, flags);
1368 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1369 spin_unlock_irqrestore(&up->port.lock, flags);
1370
1371 return ret;
1372}
1373
1374static unsigned int serial8250_get_mctrl(struct uart_port *port)
1375{
1376 struct uart_8250_port *up = (struct uart_8250_port *)port;
1377 unsigned long flags;
1378 unsigned char status;
1379 unsigned int ret;
1380
1381 spin_lock_irqsave(&up->port.lock, flags);
1382 status = serial_in(up, UART_MSR);
1383 spin_unlock_irqrestore(&up->port.lock, flags);
1384
1385 ret = 0;
1386 if (status & UART_MSR_DCD)
1387 ret |= TIOCM_CAR;
1388 if (status & UART_MSR_RI)
1389 ret |= TIOCM_RNG;
1390 if (status & UART_MSR_DSR)
1391 ret |= TIOCM_DSR;
1392 if (status & UART_MSR_CTS)
1393 ret |= TIOCM_CTS;
1394 return ret;
1395}
1396
1397static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1398{
1399 struct uart_8250_port *up = (struct uart_8250_port *)port;
1400 unsigned char mcr = 0;
1401
1402 if (mctrl & TIOCM_RTS)
1403 mcr |= UART_MCR_RTS;
1404 if (mctrl & TIOCM_DTR)
1405 mcr |= UART_MCR_DTR;
1406 if (mctrl & TIOCM_OUT1)
1407 mcr |= UART_MCR_OUT1;
1408 if (mctrl & TIOCM_OUT2)
1409 mcr |= UART_MCR_OUT2;
1410 if (mctrl & TIOCM_LOOP)
1411 mcr |= UART_MCR_LOOP;
1412
1413 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1414
1415 serial_out(up, UART_MCR, mcr);
1416}
1417
1418static void serial8250_break_ctl(struct uart_port *port, int break_state)
1419{
1420 struct uart_8250_port *up = (struct uart_8250_port *)port;
1421 unsigned long flags;
1422
1423 spin_lock_irqsave(&up->port.lock, flags);
1424 if (break_state == -1)
1425 up->lcr |= UART_LCR_SBC;
1426 else
1427 up->lcr &= ~UART_LCR_SBC;
1428 serial_out(up, UART_LCR, up->lcr);
1429 spin_unlock_irqrestore(&up->port.lock, flags);
1430}
1431
1432static int serial8250_startup(struct uart_port *port)
1433{
1434 struct uart_8250_port *up = (struct uart_8250_port *)port;
1435 unsigned long flags;
1436 int retval;
1437
1438 up->capabilities = uart_config[up->port.type].flags;
1439 up->mcr = 0;
1440
1441 if (up->port.type == PORT_16C950) {
1442 /* Wake up and initialize UART */
1443 up->acr = 0;
1444 serial_outp(up, UART_LCR, 0xBF);
1445 serial_outp(up, UART_EFR, UART_EFR_ECB);
1446 serial_outp(up, UART_IER, 0);
1447 serial_outp(up, UART_LCR, 0);
1448 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1449 serial_outp(up, UART_LCR, 0xBF);
1450 serial_outp(up, UART_EFR, UART_EFR_ECB);
1451 serial_outp(up, UART_LCR, 0);
1452 }
1453
1454#ifdef CONFIG_SERIAL_8250_RSA
1455 /*
1456 * If this is an RSA port, see if we can kick it up to the
1457 * higher speed clock.
1458 */
1459 enable_rsa(up);
1460#endif
1461
1462 /*
1463 * Clear the FIFO buffers and disable them.
1464 * (they will be reeanbled in set_termios())
1465 */
1466 serial8250_clear_fifos(up);
1467
1468 /*
1469 * Clear the interrupt registers.
1470 */
1471 (void) serial_inp(up, UART_LSR);
1472 (void) serial_inp(up, UART_RX);
1473 (void) serial_inp(up, UART_IIR);
1474 (void) serial_inp(up, UART_MSR);
1475
1476 /*
1477 * At this point, there's no way the LSR could still be 0xff;
1478 * if it is, then bail out, because there's likely no UART
1479 * here.
1480 */
1481 if (!(up->port.flags & UPF_BUGGY_UART) &&
1482 (serial_inp(up, UART_LSR) == 0xff)) {
1483 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
1484 return -ENODEV;
1485 }
1486
1487 /*
1488 * For a XR16C850, we need to set the trigger levels
1489 */
1490 if (up->port.type == PORT_16850) {
1491 unsigned char fctr;
1492
1493 serial_outp(up, UART_LCR, 0xbf);
1494
1495 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1496 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1497 serial_outp(up, UART_TRG, UART_TRG_96);
1498 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1499 serial_outp(up, UART_TRG, UART_TRG_96);
1500
1501 serial_outp(up, UART_LCR, 0);
1502 }
1503
1504 /*
1505 * If the "interrupt" for this port doesn't correspond with any
1506 * hardware interrupt, we use a timer-based system. The original
1507 * driver used to do this with IRQ0.
1508 */
1509 if (!is_real_interrupt(up->port.irq)) {
1510 unsigned int timeout = up->port.timeout;
1511
1512 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1513
1514 up->timer.data = (unsigned long)up;
1515 mod_timer(&up->timer, jiffies + timeout);
1516 } else {
1517 retval = serial_link_irq_chain(up);
1518 if (retval)
1519 return retval;
1520 }
1521
1522 /*
1523 * Now, initialize the UART
1524 */
1525 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
1526
1527 spin_lock_irqsave(&up->port.lock, flags);
1528 if (up->port.flags & UPF_FOURPORT) {
1529 if (!is_real_interrupt(up->port.irq))
1530 up->port.mctrl |= TIOCM_OUT1;
1531 } else
1532 /*
1533 * Most PC uarts need OUT2 raised to enable interrupts.
1534 */
1535 if (is_real_interrupt(up->port.irq))
1536 up->port.mctrl |= TIOCM_OUT2;
1537
1538 serial8250_set_mctrl(&up->port, up->port.mctrl);
1539 spin_unlock_irqrestore(&up->port.lock, flags);
1540
1541 /*
1542 * Finally, enable interrupts. Note: Modem status interrupts
1543 * are set via set_termios(), which will be occurring imminently
1544 * anyway, so we don't enable them here.
1545 */
1546 up->ier = UART_IER_RLSI | UART_IER_RDI;
1547 serial_outp(up, UART_IER, up->ier);
1548
1549 if (up->port.flags & UPF_FOURPORT) {
1550 unsigned int icp;
1551 /*
1552 * Enable interrupts on the AST Fourport board
1553 */
1554 icp = (up->port.iobase & 0xfe0) | 0x01f;
1555 outb_p(0x80, icp);
1556 (void) inb_p(icp);
1557 }
1558
1559 /*
1560 * And clear the interrupt registers again for luck.
1561 */
1562 (void) serial_inp(up, UART_LSR);
1563 (void) serial_inp(up, UART_RX);
1564 (void) serial_inp(up, UART_IIR);
1565 (void) serial_inp(up, UART_MSR);
1566
1567 return 0;
1568}
1569
1570static void serial8250_shutdown(struct uart_port *port)
1571{
1572 struct uart_8250_port *up = (struct uart_8250_port *)port;
1573 unsigned long flags;
1574
1575 /*
1576 * Disable interrupts from this port
1577 */
1578 up->ier = 0;
1579 serial_outp(up, UART_IER, 0);
1580
1581 spin_lock_irqsave(&up->port.lock, flags);
1582 if (up->port.flags & UPF_FOURPORT) {
1583 /* reset interrupts on the AST Fourport board */
1584 inb((up->port.iobase & 0xfe0) | 0x1f);
1585 up->port.mctrl |= TIOCM_OUT1;
1586 } else
1587 up->port.mctrl &= ~TIOCM_OUT2;
1588
1589 serial8250_set_mctrl(&up->port, up->port.mctrl);
1590 spin_unlock_irqrestore(&up->port.lock, flags);
1591
1592 /*
1593 * Disable break condition and FIFOs
1594 */
1595 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
1596 serial8250_clear_fifos(up);
1597
1598#ifdef CONFIG_SERIAL_8250_RSA
1599 /*
1600 * Reset the RSA board back to 115kbps compat mode.
1601 */
1602 disable_rsa(up);
1603#endif
1604
1605 /*
1606 * Read data port to reset things, and then unlink from
1607 * the IRQ chain.
1608 */
1609 (void) serial_in(up, UART_RX);
1610
1611 if (!is_real_interrupt(up->port.irq))
1612 del_timer_sync(&up->timer);
1613 else
1614 serial_unlink_irq_chain(up);
1615}
1616
1617static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
1618{
1619 unsigned int quot;
1620
1621 /*
1622 * Handle magic divisors for baud rates above baud_base on
1623 * SMSC SuperIO chips.
1624 */
1625 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1626 baud == (port->uartclk/4))
1627 quot = 0x8001;
1628 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1629 baud == (port->uartclk/8))
1630 quot = 0x8002;
1631 else
1632 quot = uart_get_divisor(port, baud);
1633
1634 return quot;
1635}
1636
1637static void
1638serial8250_set_termios(struct uart_port *port, struct termios *termios,
1639 struct termios *old)
1640{
1641 struct uart_8250_port *up = (struct uart_8250_port *)port;
1642 unsigned char cval, fcr = 0;
1643 unsigned long flags;
1644 unsigned int baud, quot;
1645
1646 switch (termios->c_cflag & CSIZE) {
1647 case CS5:
1648 cval = 0x00;
1649 break;
1650 case CS6:
1651 cval = 0x01;
1652 break;
1653 case CS7:
1654 cval = 0x02;
1655 break;
1656 default:
1657 case CS8:
1658 cval = 0x03;
1659 break;
1660 }
1661
1662 if (termios->c_cflag & CSTOPB)
1663 cval |= 0x04;
1664 if (termios->c_cflag & PARENB)
1665 cval |= UART_LCR_PARITY;
1666 if (!(termios->c_cflag & PARODD))
1667 cval |= UART_LCR_EPAR;
1668#ifdef CMSPAR
1669 if (termios->c_cflag & CMSPAR)
1670 cval |= UART_LCR_SPAR;
1671#endif
1672
1673 /*
1674 * Ask the core to calculate the divisor for us.
1675 */
1676 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
1677 quot = serial8250_get_divisor(port, baud);
1678
1679 /*
1680 * Work around a bug in the Oxford Semiconductor 952 rev B
1681 * chip which causes it to seriously miscalculate baud rates
1682 * when DLL is 0.
1683 */
1684 if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
1685 up->rev == 0x5201)
1686 quot ++;
1687
1688 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
1689 if (baud < 2400)
1690 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1691 else
1692 fcr = uart_config[up->port.type].fcr;
1693 }
1694
1695 /*
1696 * MCR-based auto flow control. When AFE is enabled, RTS will be
1697 * deasserted when the receive FIFO contains more characters than
1698 * the trigger, or the MCR RTS bit is cleared. In the case where
1699 * the remote UART is not using CTS auto flow control, we must
1700 * have sufficient FIFO entries for the latency of the remote
1701 * UART to respond. IOW, at least 32 bytes of FIFO.
1702 */
1703 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
1704 up->mcr &= ~UART_MCR_AFE;
1705 if (termios->c_cflag & CRTSCTS)
1706 up->mcr |= UART_MCR_AFE;
1707 }
1708
1709 /*
1710 * Ok, we're now changing the port state. Do it with
1711 * interrupts disabled.
1712 */
1713 spin_lock_irqsave(&up->port.lock, flags);
1714
1715 /*
1716 * Update the per-port timeout.
1717 */
1718 uart_update_timeout(port, termios->c_cflag, baud);
1719
1720 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1721 if (termios->c_iflag & INPCK)
1722 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1723 if (termios->c_iflag & (BRKINT | PARMRK))
1724 up->port.read_status_mask |= UART_LSR_BI;
1725
1726 /*
1727 * Characteres to ignore
1728 */
1729 up->port.ignore_status_mask = 0;
1730 if (termios->c_iflag & IGNPAR)
1731 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1732 if (termios->c_iflag & IGNBRK) {
1733 up->port.ignore_status_mask |= UART_LSR_BI;
1734 /*
1735 * If we're ignoring parity and break indicators,
1736 * ignore overruns too (for real raw support).
1737 */
1738 if (termios->c_iflag & IGNPAR)
1739 up->port.ignore_status_mask |= UART_LSR_OE;
1740 }
1741
1742 /*
1743 * ignore all characters if CREAD is not set
1744 */
1745 if ((termios->c_cflag & CREAD) == 0)
1746 up->port.ignore_status_mask |= UART_LSR_DR;
1747
1748 /*
1749 * CTS flow control flag and modem status interrupts
1750 */
1751 up->ier &= ~UART_IER_MSI;
1752 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
1753 up->ier |= UART_IER_MSI;
1754 if (up->capabilities & UART_CAP_UUE)
1755 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
1756
1757 serial_out(up, UART_IER, up->ier);
1758
1759 if (up->capabilities & UART_CAP_EFR) {
1760 unsigned char efr = 0;
1761 /*
1762 * TI16C752/Startech hardware flow control. FIXME:
1763 * - TI16C752 requires control thresholds to be set.
1764 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
1765 */
1766 if (termios->c_cflag & CRTSCTS)
1767 efr |= UART_EFR_CTS;
1768
1769 serial_outp(up, UART_LCR, 0xBF);
1770 serial_outp(up, UART_EFR, efr);
1771 }
1772
1773 if (up->capabilities & UART_NATSEMI) {
1774 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
1775 serial_outp(up, UART_LCR, 0xe0);
1776 } else {
1777 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
1778 }
1779
1780 serial_outp(up, UART_DLL, quot & 0xff); /* LS of divisor */
1781 serial_outp(up, UART_DLM, quot >> 8); /* MS of divisor */
1782
1783 /*
1784 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
1785 * is written without DLAB set, this mode will be disabled.
1786 */
1787 if (up->port.type == PORT_16750)
1788 serial_outp(up, UART_FCR, fcr);
1789
1790 serial_outp(up, UART_LCR, cval); /* reset DLAB */
1791 up->lcr = cval; /* Save LCR */
1792 if (up->port.type != PORT_16750) {
1793 if (fcr & UART_FCR_ENABLE_FIFO) {
1794 /* emulated UARTs (Lucent Venus 167x) need two steps */
1795 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1796 }
1797 serial_outp(up, UART_FCR, fcr); /* set fcr */
1798 }
1799 serial8250_set_mctrl(&up->port, up->port.mctrl);
1800 spin_unlock_irqrestore(&up->port.lock, flags);
1801}
1802
1803static void
1804serial8250_pm(struct uart_port *port, unsigned int state,
1805 unsigned int oldstate)
1806{
1807 struct uart_8250_port *p = (struct uart_8250_port *)port;
1808
1809 serial8250_set_sleep(p, state != 0);
1810
1811 if (p->pm)
1812 p->pm(port, state, oldstate);
1813}
1814
1815/*
1816 * Resource handling.
1817 */
1818static int serial8250_request_std_resource(struct uart_8250_port *up)
1819{
1820 unsigned int size = 8 << up->port.regshift;
1821 int ret = 0;
1822
1823 switch (up->port.iotype) {
1824 case UPIO_MEM:
1825 if (!up->port.mapbase)
1826 break;
1827
1828 if (!request_mem_region(up->port.mapbase, size, "serial")) {
1829 ret = -EBUSY;
1830 break;
1831 }
1832
1833 if (up->port.flags & UPF_IOREMAP) {
1834 up->port.membase = ioremap(up->port.mapbase, size);
1835 if (!up->port.membase) {
1836 release_mem_region(up->port.mapbase, size);
1837 ret = -ENOMEM;
1838 }
1839 }
1840 break;
1841
1842 case UPIO_HUB6:
1843 case UPIO_PORT:
1844 if (!request_region(up->port.iobase, size, "serial"))
1845 ret = -EBUSY;
1846 break;
1847 }
1848 return ret;
1849}
1850
1851static void serial8250_release_std_resource(struct uart_8250_port *up)
1852{
1853 unsigned int size = 8 << up->port.regshift;
1854
1855 switch (up->port.iotype) {
1856 case UPIO_MEM:
1857 if (!up->port.mapbase)
1858 break;
1859
1860 if (up->port.flags & UPF_IOREMAP) {
1861 iounmap(up->port.membase);
1862 up->port.membase = NULL;
1863 }
1864
1865 release_mem_region(up->port.mapbase, size);
1866 break;
1867
1868 case UPIO_HUB6:
1869 case UPIO_PORT:
1870 release_region(up->port.iobase, size);
1871 break;
1872 }
1873}
1874
1875static int serial8250_request_rsa_resource(struct uart_8250_port *up)
1876{
1877 unsigned long start = UART_RSA_BASE << up->port.regshift;
1878 unsigned int size = 8 << up->port.regshift;
1879 int ret = 0;
1880
1881 switch (up->port.iotype) {
1882 case UPIO_MEM:
1883 ret = -EINVAL;
1884 break;
1885
1886 case UPIO_HUB6:
1887 case UPIO_PORT:
1888 start += up->port.iobase;
1889 if (!request_region(start, size, "serial-rsa"))
1890 ret = -EBUSY;
1891 break;
1892 }
1893
1894 return ret;
1895}
1896
1897static void serial8250_release_rsa_resource(struct uart_8250_port *up)
1898{
1899 unsigned long offset = UART_RSA_BASE << up->port.regshift;
1900 unsigned int size = 8 << up->port.regshift;
1901
1902 switch (up->port.iotype) {
1903 case UPIO_MEM:
1904 break;
1905
1906 case UPIO_HUB6:
1907 case UPIO_PORT:
1908 release_region(up->port.iobase + offset, size);
1909 break;
1910 }
1911}
1912
1913static void serial8250_release_port(struct uart_port *port)
1914{
1915 struct uart_8250_port *up = (struct uart_8250_port *)port;
1916
1917 serial8250_release_std_resource(up);
1918 if (up->port.type == PORT_RSA)
1919 serial8250_release_rsa_resource(up);
1920}
1921
1922static int serial8250_request_port(struct uart_port *port)
1923{
1924 struct uart_8250_port *up = (struct uart_8250_port *)port;
1925 int ret = 0;
1926
1927 ret = serial8250_request_std_resource(up);
1928 if (ret == 0 && up->port.type == PORT_RSA) {
1929 ret = serial8250_request_rsa_resource(up);
1930 if (ret < 0)
1931 serial8250_release_std_resource(up);
1932 }
1933
1934 return ret;
1935}
1936
1937static void serial8250_config_port(struct uart_port *port, int flags)
1938{
1939 struct uart_8250_port *up = (struct uart_8250_port *)port;
1940 int probeflags = PROBE_ANY;
1941 int ret;
1942
1943 /*
1944 * Don't probe for MCA ports on non-MCA machines.
1945 */
1946 if (up->port.flags & UPF_BOOT_ONLYMCA && !MCA_bus)
1947 return;
1948
1949 /*
1950 * Find the region that we can probe for. This in turn
1951 * tells us whether we can probe for the type of port.
1952 */
1953 ret = serial8250_request_std_resource(up);
1954 if (ret < 0)
1955 return;
1956
1957 ret = serial8250_request_rsa_resource(up);
1958 if (ret < 0)
1959 probeflags &= ~PROBE_RSA;
1960
1961 if (flags & UART_CONFIG_TYPE)
1962 autoconfig(up, probeflags);
1963 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
1964 autoconfig_irq(up);
1965
1966 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
1967 serial8250_release_rsa_resource(up);
1968 if (up->port.type == PORT_UNKNOWN)
1969 serial8250_release_std_resource(up);
1970}
1971
1972static int
1973serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
1974{
1975 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
1976 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
1977 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
1978 ser->type == PORT_STARTECH)
1979 return -EINVAL;
1980 return 0;
1981}
1982
1983static const char *
1984serial8250_type(struct uart_port *port)
1985{
1986 int type = port->type;
1987
1988 if (type >= ARRAY_SIZE(uart_config))
1989 type = 0;
1990 return uart_config[type].name;
1991}
1992
1993static struct uart_ops serial8250_pops = {
1994 .tx_empty = serial8250_tx_empty,
1995 .set_mctrl = serial8250_set_mctrl,
1996 .get_mctrl = serial8250_get_mctrl,
1997 .stop_tx = serial8250_stop_tx,
1998 .start_tx = serial8250_start_tx,
1999 .stop_rx = serial8250_stop_rx,
2000 .enable_ms = serial8250_enable_ms,
2001 .break_ctl = serial8250_break_ctl,
2002 .startup = serial8250_startup,
2003 .shutdown = serial8250_shutdown,
2004 .set_termios = serial8250_set_termios,
2005 .pm = serial8250_pm,
2006 .type = serial8250_type,
2007 .release_port = serial8250_release_port,
2008 .request_port = serial8250_request_port,
2009 .config_port = serial8250_config_port,
2010 .verify_port = serial8250_verify_port,
2011};
2012
2013static struct uart_8250_port serial8250_ports[UART_NR];
2014
2015static void __init serial8250_isa_init_ports(void)
2016{
2017 struct uart_8250_port *up;
2018 static int first = 1;
2019 int i;
2020
2021 if (!first)
2022 return;
2023 first = 0;
2024
2025 for (i = 0; i < UART_NR; i++) {
2026 struct uart_8250_port *up = &serial8250_ports[i];
2027
2028 up->port.line = i;
2029 spin_lock_init(&up->port.lock);
2030
2031 init_timer(&up->timer);
2032 up->timer.function = serial8250_timeout;
2033
2034 /*
2035 * ALPHA_KLUDGE_MCR needs to be killed.
2036 */
2037 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2038 up->mcr_force = ALPHA_KLUDGE_MCR;
2039
2040 up->port.ops = &serial8250_pops;
2041 }
2042
2043 for (i = 0, up = serial8250_ports; i < ARRAY_SIZE(old_serial_port);
2044 i++, up++) {
2045 up->port.iobase = old_serial_port[i].port;
2046 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2047 up->port.uartclk = old_serial_port[i].baud_base * 16;
2048 up->port.flags = old_serial_port[i].flags;
2049 up->port.hub6 = old_serial_port[i].hub6;
2050 up->port.membase = old_serial_port[i].iomem_base;
2051 up->port.iotype = old_serial_port[i].io_type;
2052 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2053 if (share_irqs)
2054 up->port.flags |= UPF_SHARE_IRQ;
2055 }
2056}
2057
2058static void __init
2059serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2060{
2061 int i;
2062
2063 serial8250_isa_init_ports();
2064
2065 for (i = 0; i < UART_NR; i++) {
2066 struct uart_8250_port *up = &serial8250_ports[i];
2067
2068 up->port.dev = dev;
2069 uart_add_one_port(drv, &up->port);
2070 }
2071}
2072
2073#ifdef CONFIG_SERIAL_8250_CONSOLE
2074
2075#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2076
2077/*
2078 * Wait for transmitter & holding register to empty
2079 */
2080static inline void wait_for_xmitr(struct uart_8250_port *up)
2081{
2082 unsigned int status, tmout = 10000;
2083
2084 /* Wait up to 10ms for the character(s) to be sent. */
2085 do {
2086 status = serial_in(up, UART_LSR);
2087
2088 if (status & UART_LSR_BI)
2089 up->lsr_break_flag = UART_LSR_BI;
2090
2091 if (--tmout == 0)
2092 break;
2093 udelay(1);
2094 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
2095
2096 /* Wait up to 1s for flow control if necessary */
2097 if (up->port.flags & UPF_CONS_FLOW) {
2098 tmout = 1000000;
2099 while (--tmout &&
2100 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
2101 udelay(1);
2102 }
2103}
2104
2105/*
2106 * Print a string to the serial port trying not to disturb
2107 * any possible real use of the port...
2108 *
2109 * The console_lock must be held when we get here.
2110 */
2111static void
2112serial8250_console_write(struct console *co, const char *s, unsigned int count)
2113{
2114 struct uart_8250_port *up = &serial8250_ports[co->index];
2115 unsigned int ier;
2116 int i;
2117
2118 /*
2119 * First save the UER then disable the interrupts
2120 */
2121 ier = serial_in(up, UART_IER);
2122
2123 if (up->capabilities & UART_CAP_UUE)
2124 serial_out(up, UART_IER, UART_IER_UUE);
2125 else
2126 serial_out(up, UART_IER, 0);
2127
2128 /*
2129 * Now, do each character
2130 */
2131 for (i = 0; i < count; i++, s++) {
2132 wait_for_xmitr(up);
2133
2134 /*
2135 * Send the character out.
2136 * If a LF, also do CR...
2137 */
2138 serial_out(up, UART_TX, *s);
2139 if (*s == 10) {
2140 wait_for_xmitr(up);
2141 serial_out(up, UART_TX, 13);
2142 }
2143 }
2144
2145 /*
2146 * Finally, wait for transmitter to become empty
2147 * and restore the IER
2148 */
2149 wait_for_xmitr(up);
2150 serial_out(up, UART_IER, ier);
2151}
2152
2153static int serial8250_console_setup(struct console *co, char *options)
2154{
2155 struct uart_port *port;
2156 int baud = 9600;
2157 int bits = 8;
2158 int parity = 'n';
2159 int flow = 'n';
2160
2161 /*
2162 * Check whether an invalid uart number has been specified, and
2163 * if so, search for the first available port that does have
2164 * console support.
2165 */
2166 if (co->index >= UART_NR)
2167 co->index = 0;
2168 port = &serial8250_ports[co->index].port;
2169 if (!port->iobase && !port->membase)
2170 return -ENODEV;
2171
2172 if (options)
2173 uart_parse_options(options, &baud, &parity, &bits, &flow);
2174
2175 return uart_set_options(port, co, baud, parity, bits, flow);
2176}
2177
2178static struct uart_driver serial8250_reg;
2179static struct console serial8250_console = {
2180 .name = "ttyS",
2181 .write = serial8250_console_write,
2182 .device = uart_console_device,
2183 .setup = serial8250_console_setup,
2184 .flags = CON_PRINTBUFFER,
2185 .index = -1,
2186 .data = &serial8250_reg,
2187};
2188
2189static int __init serial8250_console_init(void)
2190{
2191 serial8250_isa_init_ports();
2192 register_console(&serial8250_console);
2193 return 0;
2194}
2195console_initcall(serial8250_console_init);
2196
2197static int __init find_port(struct uart_port *p)
2198{
2199 int line;
2200 struct uart_port *port;
2201
2202 for (line = 0; line < UART_NR; line++) {
2203 port = &serial8250_ports[line].port;
2204 if (p->iotype == port->iotype &&
2205 p->iobase == port->iobase &&
2206 p->membase == port->membase)
2207 return line;
2208 }
2209 return -ENODEV;
2210}
2211
2212int __init serial8250_start_console(struct uart_port *port, char *options)
2213{
2214 int line;
2215
2216 line = find_port(port);
2217 if (line < 0)
2218 return -ENODEV;
2219
2220 add_preferred_console("ttyS", line, options);
2221 printk("Adding console on ttyS%d at %s 0x%lx (options '%s')\n",
2222 line, port->iotype == UPIO_MEM ? "MMIO" : "I/O port",
2223 port->iotype == UPIO_MEM ? (unsigned long) port->mapbase :
2224 (unsigned long) port->iobase, options);
2225 if (!(serial8250_console.flags & CON_ENABLED)) {
2226 serial8250_console.flags &= ~CON_PRINTBUFFER;
2227 register_console(&serial8250_console);
2228 }
2229 return line;
2230}
2231
2232#define SERIAL8250_CONSOLE &serial8250_console
2233#else
2234#define SERIAL8250_CONSOLE NULL
2235#endif
2236
2237static struct uart_driver serial8250_reg = {
2238 .owner = THIS_MODULE,
2239 .driver_name = "serial",
2240 .devfs_name = "tts/",
2241 .dev_name = "ttyS",
2242 .major = TTY_MAJOR,
2243 .minor = 64,
2244 .nr = UART_NR,
2245 .cons = SERIAL8250_CONSOLE,
2246};
2247
2248int __init early_serial_setup(struct uart_port *port)
2249{
2250 if (port->line >= ARRAY_SIZE(serial8250_ports))
2251 return -ENODEV;
2252
2253 serial8250_isa_init_ports();
2254 serial8250_ports[port->line].port = *port;
2255 serial8250_ports[port->line].port.ops = &serial8250_pops;
2256 return 0;
2257}
2258
2259/**
2260 * serial8250_suspend_port - suspend one serial port
2261 * @line: serial line number
2262 * @level: the level of port suspension, as per uart_suspend_port
2263 *
2264 * Suspend one serial port.
2265 */
2266void serial8250_suspend_port(int line)
2267{
2268 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2269}
2270
2271/**
2272 * serial8250_resume_port - resume one serial port
2273 * @line: serial line number
2274 * @level: the level of port resumption, as per uart_resume_port
2275 *
2276 * Resume one serial port.
2277 */
2278void serial8250_resume_port(int line)
2279{
2280 uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
2281}
2282
2283/*
2284 * Register a set of serial devices attached to a platform device. The
2285 * list is terminated with a zero flags entry, which means we expect
2286 * all entries to have at least UPF_BOOT_AUTOCONF set.
2287 */
2288static int __devinit serial8250_probe(struct device *dev)
2289{
2290 struct plat_serial8250_port *p = dev->platform_data;
2291 struct uart_port port;
2292
2293 memset(&port, 0, sizeof(struct uart_port));
2294
2295 for (; p && p->flags != 0; p++) {
2296 port.iobase = p->iobase;
2297 port.membase = p->membase;
2298 port.irq = p->irq;
2299 port.uartclk = p->uartclk;
2300 port.regshift = p->regshift;
2301 port.iotype = p->iotype;
2302 port.flags = p->flags;
2303 port.mapbase = p->mapbase;
2304 port.dev = dev;
2305 if (share_irqs)
2306 port.flags |= UPF_SHARE_IRQ;
2307 serial8250_register_port(&port);
2308 }
2309 return 0;
2310}
2311
2312/*
2313 * Remove serial ports registered against a platform device.
2314 */
2315static int __devexit serial8250_remove(struct device *dev)
2316{
2317 int i;
2318
2319 for (i = 0; i < UART_NR; i++) {
2320 struct uart_8250_port *up = &serial8250_ports[i];
2321
2322 if (up->port.dev == dev)
2323 serial8250_unregister_port(i);
2324 }
2325 return 0;
2326}
2327
2328static int serial8250_suspend(struct device *dev, pm_message_t state, u32 level)
2329{
2330 int i;
2331
2332 if (level != SUSPEND_DISABLE)
2333 return 0;
2334
2335 for (i = 0; i < UART_NR; i++) {
2336 struct uart_8250_port *up = &serial8250_ports[i];
2337
2338 if (up->port.type != PORT_UNKNOWN && up->port.dev == dev)
2339 uart_suspend_port(&serial8250_reg, &up->port);
2340 }
2341
2342 return 0;
2343}
2344
2345static int serial8250_resume(struct device *dev, u32 level)
2346{
2347 int i;
2348
2349 if (level != RESUME_ENABLE)
2350 return 0;
2351
2352 for (i = 0; i < UART_NR; i++) {
2353 struct uart_8250_port *up = &serial8250_ports[i];
2354
2355 if (up->port.type != PORT_UNKNOWN && up->port.dev == dev)
2356 uart_resume_port(&serial8250_reg, &up->port);
2357 }
2358
2359 return 0;
2360}
2361
2362static struct device_driver serial8250_isa_driver = {
2363 .name = "serial8250",
2364 .bus = &platform_bus_type,
2365 .probe = serial8250_probe,
2366 .remove = __devexit_p(serial8250_remove),
2367 .suspend = serial8250_suspend,
2368 .resume = serial8250_resume,
2369};
2370
2371/*
2372 * This "device" covers _all_ ISA 8250-compatible serial devices listed
2373 * in the table in include/asm/serial.h
2374 */
2375static struct platform_device *serial8250_isa_devs;
2376
2377/*
2378 * serial8250_register_port and serial8250_unregister_port allows for
2379 * 16x50 serial ports to be configured at run-time, to support PCMCIA
2380 * modems and PCI multiport cards.
2381 */
2382static DECLARE_MUTEX(serial_sem);
2383
2384static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2385{
2386 int i;
2387
2388 /*
2389 * First, find a port entry which matches.
2390 */
2391 for (i = 0; i < UART_NR; i++)
2392 if (uart_match_port(&serial8250_ports[i].port, port))
2393 return &serial8250_ports[i];
2394
2395 /*
2396 * We didn't find a matching entry, so look for the first
2397 * free entry. We look for one which hasn't been previously
2398 * used (indicated by zero iobase).
2399 */
2400 for (i = 0; i < UART_NR; i++)
2401 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2402 serial8250_ports[i].port.iobase == 0)
2403 return &serial8250_ports[i];
2404
2405 /*
2406 * That also failed. Last resort is to find any entry which
2407 * doesn't have a real port associated with it.
2408 */
2409 for (i = 0; i < UART_NR; i++)
2410 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2411 return &serial8250_ports[i];
2412
2413 return NULL;
2414}
2415
2416/**
2417 * serial8250_register_port - register a serial port
2418 * @port: serial port template
2419 *
2420 * Configure the serial port specified by the request. If the
2421 * port exists and is in use, it is hung up and unregistered
2422 * first.
2423 *
2424 * The port is then probed and if necessary the IRQ is autodetected
2425 * If this fails an error is returned.
2426 *
2427 * On success the port is ready to use and the line number is returned.
2428 */
2429int serial8250_register_port(struct uart_port *port)
2430{
2431 struct uart_8250_port *uart;
2432 int ret = -ENOSPC;
2433
2434 if (port->uartclk == 0)
2435 return -EINVAL;
2436
2437 down(&serial_sem);
2438
2439 uart = serial8250_find_match_or_unused(port);
2440 if (uart) {
2441 uart_remove_one_port(&serial8250_reg, &uart->port);
2442
2443 uart->port.iobase = port->iobase;
2444 uart->port.membase = port->membase;
2445 uart->port.irq = port->irq;
2446 uart->port.uartclk = port->uartclk;
2447 uart->port.fifosize = port->fifosize;
2448 uart->port.regshift = port->regshift;
2449 uart->port.iotype = port->iotype;
2450 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
2451 uart->port.mapbase = port->mapbase;
2452 if (port->dev)
2453 uart->port.dev = port->dev;
2454
2455 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2456 if (ret == 0)
2457 ret = uart->port.line;
2458 }
2459 up(&serial_sem);
2460
2461 return ret;
2462}
2463EXPORT_SYMBOL(serial8250_register_port);
2464
2465/**
2466 * serial8250_unregister_port - remove a 16x50 serial port at runtime
2467 * @line: serial line number
2468 *
2469 * Remove one serial port. This may not be called from interrupt
2470 * context. We hand the port back to the our control.
2471 */
2472void serial8250_unregister_port(int line)
2473{
2474 struct uart_8250_port *uart = &serial8250_ports[line];
2475
2476 down(&serial_sem);
2477 uart_remove_one_port(&serial8250_reg, &uart->port);
2478 if (serial8250_isa_devs) {
2479 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
2480 uart->port.type = PORT_UNKNOWN;
2481 uart->port.dev = &serial8250_isa_devs->dev;
2482 uart_add_one_port(&serial8250_reg, &uart->port);
2483 } else {
2484 uart->port.dev = NULL;
2485 }
2486 up(&serial_sem);
2487}
2488EXPORT_SYMBOL(serial8250_unregister_port);
2489
2490static int __init serial8250_init(void)
2491{
2492 int ret, i;
2493
2494 printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ "
2495 "%d ports, IRQ sharing %sabled\n", (int) UART_NR,
2496 share_irqs ? "en" : "dis");
2497
2498 for (i = 0; i < NR_IRQS; i++)
2499 spin_lock_init(&irq_lists[i].lock);
2500
2501 ret = uart_register_driver(&serial8250_reg);
2502 if (ret)
2503 goto out;
2504
2505 serial8250_isa_devs = platform_device_register_simple("serial8250",
2506 -1, NULL, 0);
2507 if (IS_ERR(serial8250_isa_devs)) {
2508 ret = PTR_ERR(serial8250_isa_devs);
2509 goto unreg;
2510 }
2511
2512 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
2513
2514 ret = driver_register(&serial8250_isa_driver);
2515 if (ret == 0)
2516 goto out;
2517
2518 platform_device_unregister(serial8250_isa_devs);
2519 unreg:
2520 uart_unregister_driver(&serial8250_reg);
2521 out:
2522 return ret;
2523}
2524
2525static void __exit serial8250_exit(void)
2526{
2527 struct platform_device *isa_dev = serial8250_isa_devs;
2528
2529 /*
2530 * This tells serial8250_unregister_port() not to re-register
2531 * the ports (thereby making serial8250_isa_driver permanently
2532 * in use.)
2533 */
2534 serial8250_isa_devs = NULL;
2535
2536 driver_unregister(&serial8250_isa_driver);
2537 platform_device_unregister(isa_dev);
2538
2539 uart_unregister_driver(&serial8250_reg);
2540}
2541
2542module_init(serial8250_init);
2543module_exit(serial8250_exit);
2544
2545EXPORT_SYMBOL(serial8250_suspend_port);
2546EXPORT_SYMBOL(serial8250_resume_port);
2547
2548MODULE_LICENSE("GPL");
2549MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
2550
2551module_param(share_irqs, uint, 0644);
2552MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
2553 " (unsafe)");
2554
2555#ifdef CONFIG_SERIAL_8250_RSA
2556module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
2557MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
2558#endif
2559MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
2560
2561/**
2562 * register_serial - configure a 16x50 serial port at runtime
2563 * @req: request structure
2564 *
2565 * Configure the serial port specified by the request. If the
2566 * port exists and is in use an error is returned. If the port
2567 * is not currently in the table it is added.
2568 *
2569 * The port is then probed and if necessary the IRQ is autodetected
2570 * If this fails an error is returned.
2571 *
2572 * On success the port is ready to use and the line number is returned.
2573 */
2574int register_serial(struct serial_struct *req)
2575{
2576 struct uart_port port;
2577
2578 port.iobase = req->port;
2579 port.membase = req->iomem_base;
2580 port.irq = req->irq;
2581 port.uartclk = req->baud_base * 16;
2582 port.fifosize = req->xmit_fifo_size;
2583 port.regshift = req->iomem_reg_shift;
2584 port.iotype = req->io_type;
2585 port.flags = req->flags | UPF_BOOT_AUTOCONF;
2586 port.mapbase = req->iomap_base;
2587 port.dev = NULL;
2588
2589 if (share_irqs)
2590 port.flags |= UPF_SHARE_IRQ;
2591
2592 if (HIGH_BITS_OFFSET)
2593 port.iobase |= (long) req->port_high << HIGH_BITS_OFFSET;
2594
2595 /*
2596 * If a clock rate wasn't specified by the low level driver, then
2597 * default to the standard clock rate. This should be 115200 (*16)
2598 * and should not depend on the architecture's BASE_BAUD definition.
2599 * However, since this API will be deprecated, it's probably a
2600 * better idea to convert the drivers to use the new API
2601 * (serial8250_register_port and serial8250_unregister_port).
2602 */
2603 if (port.uartclk == 0) {
2604 printk(KERN_WARNING
2605 "Serial: registering port at [%08x,%08lx,%p] irq %d with zero baud_base\n",
2606 port.iobase, port.mapbase, port.membase, port.irq);
2607 printk(KERN_WARNING "Serial: see %s:%d for more information\n",
2608 __FILE__, __LINE__);
2609 dump_stack();
2610
2611 /*
2612 * Fix it up for now, but this is only a temporary measure.
2613 */
2614 port.uartclk = BASE_BAUD * 16;
2615 }
2616
2617 return serial8250_register_port(&port);
2618}
2619EXPORT_SYMBOL(register_serial);
2620
2621/**
2622 * unregister_serial - remove a 16x50 serial port at runtime
2623 * @line: serial line number
2624 *
2625 * Remove one serial port. This may not be called from interrupt
2626 * context. We hand the port back to our local PM control.
2627 */
2628void unregister_serial(int line)
2629{
2630 serial8250_unregister_port(line);
2631}
2632EXPORT_SYMBOL(unregister_serial);
diff --git a/drivers/serial/8250.h b/drivers/serial/8250.h
new file mode 100644
index 000000000000..4f3d62f222f4
--- /dev/null
+++ b/drivers/serial/8250.h
@@ -0,0 +1,87 @@
1/*
2 * linux/drivers/char/8250.h
3 *
4 * Driver for 8250/16550-type serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright (C) 2001 Russell King.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * $Id: 8250.h,v 1.8 2002/07/21 21:32:30 rmk Exp $
16 */
17
18#include <linux/config.h>
19
20int serial8250_register_port(struct uart_port *);
21void serial8250_unregister_port(int line);
22void serial8250_suspend_port(int line);
23void serial8250_resume_port(int line);
24
25struct old_serial_port {
26 unsigned int uart;
27 unsigned int baud_base;
28 unsigned int port;
29 unsigned int irq;
30 unsigned int flags;
31 unsigned char hub6;
32 unsigned char io_type;
33 unsigned char *iomem_base;
34 unsigned short iomem_reg_shift;
35};
36
37/*
38 * This replaces serial_uart_config in include/linux/serial.h
39 */
40struct serial8250_config {
41 const char *name;
42 unsigned short fifo_size;
43 unsigned short tx_loadsz;
44 unsigned char fcr;
45 unsigned int flags;
46};
47
48#define UART_CAP_FIFO (1 << 8) /* UART has FIFO */
49#define UART_CAP_EFR (1 << 9) /* UART has EFR */
50#define UART_CAP_SLEEP (1 << 10) /* UART has IER sleep */
51#define UART_CAP_AFE (1 << 11) /* MCR-based hw flow control */
52#define UART_CAP_UUE (1 << 12) /* UART needs IER bit 6 set (Xscale) */
53
54#if defined(__i386__) && (defined(CONFIG_M386) || defined(CONFIG_M486))
55#define _INLINE_ inline
56#else
57#define _INLINE_
58#endif
59
60#define PROBE_RSA (1 << 0)
61#define PROBE_ANY (~0)
62
63#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
64
65#ifdef CONFIG_SERIAL_8250_SHARE_IRQ
66#define SERIAL8250_SHARE_IRQS 1
67#else
68#define SERIAL8250_SHARE_IRQS 0
69#endif
70
71#if defined(__alpha__) && !defined(CONFIG_PCI)
72/*
73 * Digital did something really horribly wrong with the OUT1 and OUT2
74 * lines on at least some ALPHA's. The failure mode is that if either
75 * is cleared, the machine locks up with endless interrupts.
76 */
77#define ALPHA_KLUDGE_MCR (UART_MCR_OUT2 | UART_MCR_OUT1)
78#elif defined(CONFIG_SBC8560)
79/*
80 * WindRiver did something similarly broken on their SBC8560 board. The
81 * UART tristates its IRQ output while OUT2 is clear, but they pulled
82 * the interrupt line _up_ instead of down, so if we register the IRQ
83 * while the UART is in that state, we die in an IRQ storm. */
84#define ALPHA_KLUDGE_MCR (UART_MCR_OUT2)
85#else
86#define ALPHA_KLUDGE_MCR 0
87#endif
diff --git a/drivers/serial/8250_acorn.c b/drivers/serial/8250_acorn.c
new file mode 100644
index 000000000000..32af3650e8b4
--- /dev/null
+++ b/drivers/serial/8250_acorn.c
@@ -0,0 +1,142 @@
1/*
2 * linux/drivers/serial/acorn.c
3 *
4 * Copyright (C) 1996-2003 Russell King.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/tty.h>
13#include <linux/serial_core.h>
14#include <linux/errno.h>
15#include <linux/ioport.h>
16#include <linux/slab.h>
17#include <linux/device.h>
18#include <linux/init.h>
19
20#include <asm/io.h>
21#include <asm/ecard.h>
22#include <asm/string.h>
23
24#include "8250.h"
25
26#define MAX_PORTS 3
27
28struct serial_card_type {
29 unsigned int num_ports;
30 unsigned int uartclk;
31 unsigned int type;
32 unsigned int offset[MAX_PORTS];
33};
34
35struct serial_card_info {
36 unsigned int num_ports;
37 int ports[MAX_PORTS];
38};
39
40static int __devinit
41serial_card_probe(struct expansion_card *ec, const struct ecard_id *id)
42{
43 struct serial_card_info *info;
44 struct serial_card_type *type = id->data;
45 struct uart_port port;
46 unsigned long bus_addr;
47 unsigned char __iomem *virt_addr;
48 unsigned int i;
49
50 info = kmalloc(sizeof(struct serial_card_info), GFP_KERNEL);
51 if (!info)
52 return -ENOMEM;
53
54 memset(info, 0, sizeof(struct serial_card_info));
55 info->num_ports = type->num_ports;
56
57 bus_addr = ecard_resource_start(ec, type->type);
58 virt_addr = ioremap(bus_addr, ecard_resource_len(ec, type->type));
59 if (!virt_addr) {
60 kfree(info);
61 return -ENOMEM;
62 }
63
64 ecard_set_drvdata(ec, info);
65
66 memset(&port, 0, sizeof(struct uart_port));
67 port.irq = ec->irq;
68 port.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
69 port.uartclk = type->uartclk;
70 port.iotype = UPIO_MEM;
71 port.regshift = 2;
72 port.dev = &ec->dev;
73
74 for (i = 0; i < info->num_ports; i ++) {
75 port.membase = virt_addr + type->offset[i];
76 port.mapbase = bus_addr + type->offset[i];
77
78 info->ports[i] = serial8250_register_port(&port);
79 }
80
81 return 0;
82}
83
84static void __devexit serial_card_remove(struct expansion_card *ec)
85{
86 struct serial_card_info *info = ecard_get_drvdata(ec);
87 int i;
88
89 ecard_set_drvdata(ec, NULL);
90
91 for (i = 0; i < info->num_ports; i++)
92 if (info->ports[i] > 0)
93 serial8250_unregister_port(info->ports[i]);
94
95 kfree(info);
96}
97
98static struct serial_card_type atomwide_type = {
99 .num_ports = 3,
100 .uartclk = 7372800,
101 .type = ECARD_RES_IOCSLOW,
102 .offset = { 0x2800, 0x2400, 0x2000 },
103};
104
105static struct serial_card_type serport_type = {
106 .num_ports = 2,
107 .uartclk = 3686400,
108 .type = ECARD_RES_IOCSLOW,
109 .offset = { 0x2000, 0x2020 },
110};
111
112static const struct ecard_id serial_cids[] = {
113 { MANU_ATOMWIDE, PROD_ATOMWIDE_3PSERIAL, &atomwide_type },
114 { MANU_SERPORT, PROD_SERPORT_DSPORT, &serport_type },
115 { 0xffff, 0xffff }
116};
117
118static struct ecard_driver serial_card_driver = {
119 .probe = serial_card_probe,
120 .remove = __devexit_p(serial_card_remove),
121 .id_table = serial_cids,
122 .drv = {
123 .name = "8250_acorn",
124 },
125};
126
127static int __init serial_card_init(void)
128{
129 return ecard_register_driver(&serial_card_driver);
130}
131
132static void __exit serial_card_exit(void)
133{
134 ecard_remove_driver(&serial_card_driver);
135}
136
137MODULE_AUTHOR("Russell King");
138MODULE_DESCRIPTION("Acorn 8250-compatible serial port expansion card driver");
139MODULE_LICENSE("GPL");
140
141module_init(serial_card_init);
142module_exit(serial_card_exit);
diff --git a/drivers/serial/8250_acpi.c b/drivers/serial/8250_acpi.c
new file mode 100644
index 000000000000..6b9ead288517
--- /dev/null
+++ b/drivers/serial/8250_acpi.c
@@ -0,0 +1,171 @@
1/*
2 * Copyright (c) 2002-2003 Matthew Wilcox for Hewlett-Packard
3 * Copyright (C) 2004 Hewlett-Packard Co
4 * Bjorn Helgaas <bjorn.helgaas@hp.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/acpi.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/serial_core.h>
16
17#include <acpi/acpi_bus.h>
18
19#include <asm/io.h>
20
21#include "8250.h"
22
23struct serial_private {
24 int line;
25};
26
27static acpi_status acpi_serial_mmio(struct uart_port *port,
28 struct acpi_resource_address64 *addr)
29{
30 port->mapbase = addr->min_address_range;
31 port->iotype = UPIO_MEM;
32 port->flags |= UPF_IOREMAP;
33 return AE_OK;
34}
35
36static acpi_status acpi_serial_port(struct uart_port *port,
37 struct acpi_resource_io *io)
38{
39 if (io->range_length) {
40 port->iobase = io->min_base_address;
41 port->iotype = UPIO_PORT;
42 } else
43 printk(KERN_ERR "%s: zero-length IO port range?\n", __FUNCTION__);
44 return AE_OK;
45}
46
47static acpi_status acpi_serial_ext_irq(struct uart_port *port,
48 struct acpi_resource_ext_irq *ext_irq)
49{
50 if (ext_irq->number_of_interrupts > 0)
51 port->irq = acpi_register_gsi(ext_irq->interrupts[0],
52 ext_irq->edge_level, ext_irq->active_high_low);
53 return AE_OK;
54}
55
56static acpi_status acpi_serial_irq(struct uart_port *port,
57 struct acpi_resource_irq *irq)
58{
59 if (irq->number_of_interrupts > 0)
60 port->irq = acpi_register_gsi(irq->interrupts[0],
61 irq->edge_level, irq->active_high_low);
62 return AE_OK;
63}
64
65static acpi_status acpi_serial_resource(struct acpi_resource *res, void *data)
66{
67 struct uart_port *port = (struct uart_port *) data;
68 struct acpi_resource_address64 addr;
69 acpi_status status;
70
71 status = acpi_resource_to_address64(res, &addr);
72 if (ACPI_SUCCESS(status))
73 return acpi_serial_mmio(port, &addr);
74 else if (res->id == ACPI_RSTYPE_IO)
75 return acpi_serial_port(port, &res->data.io);
76 else if (res->id == ACPI_RSTYPE_EXT_IRQ)
77 return acpi_serial_ext_irq(port, &res->data.extended_irq);
78 else if (res->id == ACPI_RSTYPE_IRQ)
79 return acpi_serial_irq(port, &res->data.irq);
80 return AE_OK;
81}
82
83static int acpi_serial_add(struct acpi_device *device)
84{
85 struct serial_private *priv;
86 acpi_status status;
87 struct uart_port port;
88 int result;
89
90 memset(&port, 0, sizeof(struct uart_port));
91
92 port.uartclk = 1843200;
93 port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
94
95 priv = kmalloc(sizeof(struct serial_private), GFP_KERNEL);
96 if (!priv) {
97 result = -ENOMEM;
98 goto fail;
99 }
100 memset(priv, 0, sizeof(*priv));
101
102 status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
103 acpi_serial_resource, &port);
104 if (ACPI_FAILURE(status)) {
105 result = -ENODEV;
106 goto fail;
107 }
108
109 if (!port.mapbase && !port.iobase) {
110 printk(KERN_ERR "%s: no iomem or port address in %s _CRS\n",
111 __FUNCTION__, device->pnp.bus_id);
112 result = -ENODEV;
113 goto fail;
114 }
115
116 priv->line = serial8250_register_port(&port);
117 if (priv->line < 0) {
118 printk(KERN_WARNING "Couldn't register serial port %s: %d\n",
119 device->pnp.bus_id, priv->line);
120 result = -ENODEV;
121 goto fail;
122 }
123
124 acpi_driver_data(device) = priv;
125 return 0;
126
127fail:
128 kfree(priv);
129
130 return result;
131}
132
133static int acpi_serial_remove(struct acpi_device *device, int type)
134{
135 struct serial_private *priv;
136
137 if (!device || !acpi_driver_data(device))
138 return -EINVAL;
139
140 priv = acpi_driver_data(device);
141 serial8250_unregister_port(priv->line);
142 kfree(priv);
143
144 return 0;
145}
146
147static struct acpi_driver acpi_serial_driver = {
148 .name = "serial",
149 .class = "",
150 .ids = "PNP0501",
151 .ops = {
152 .add = acpi_serial_add,
153 .remove = acpi_serial_remove,
154 },
155};
156
157static int __init acpi_serial_init(void)
158{
159 return acpi_bus_register_driver(&acpi_serial_driver);
160}
161
162static void __exit acpi_serial_exit(void)
163{
164 acpi_bus_unregister_driver(&acpi_serial_driver);
165}
166
167module_init(acpi_serial_init);
168module_exit(acpi_serial_exit);
169
170MODULE_LICENSE("GPL");
171MODULE_DESCRIPTION("Generic 8250/16x50 ACPI serial driver");
diff --git a/drivers/serial/8250_early.c b/drivers/serial/8250_early.c
new file mode 100644
index 000000000000..b7a5dd710228
--- /dev/null
+++ b/drivers/serial/8250_early.c
@@ -0,0 +1,255 @@
1/*
2 * Early serial console for 8250/16550 devices
3 *
4 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
5 * Bjorn Helgaas <bjorn.helgaas@hp.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Based on the 8250.c serial driver, Copyright (C) 2001 Russell King,
12 * and on early_printk.c by Andi Kleen.
13 *
14 * This is for use before the serial driver has initialized, in
15 * particular, before the UARTs have been discovered and named.
16 * Instead of specifying the console device as, e.g., "ttyS0",
17 * we locate the device directly by its MMIO or I/O port address.
18 *
19 * The user can specify the device directly, e.g.,
20 * console=uart,io,0x3f8,9600n8
21 * console=uart,mmio,0xff5e0000,115200n8
22 * or platform code can call early_uart_console_init() to set
23 * the early UART device.
24 *
25 * After the normal serial driver starts, we try to locate the
26 * matching ttyS device and start a console there.
27 */
28
29#include <linux/tty.h>
30#include <linux/init.h>
31#include <linux/console.h>
32#include <linux/serial_core.h>
33#include <linux/serial_reg.h>
34#include <linux/serial.h>
35#include <asm/io.h>
36#include <asm/serial.h>
37
38struct early_uart_device {
39 struct uart_port port;
40 char options[16]; /* e.g., 115200n8 */
41 unsigned int baud;
42};
43
44static struct early_uart_device early_device __initdata;
45static int early_uart_registered __initdata;
46
47static unsigned int __init serial_in(struct uart_port *port, int offset)
48{
49 if (port->iotype == UPIO_MEM)
50 return readb(port->membase + offset);
51 else
52 return inb(port->iobase + offset);
53}
54
55static void __init serial_out(struct uart_port *port, int offset, int value)
56{
57 if (port->iotype == UPIO_MEM)
58 writeb(value, port->membase + offset);
59 else
60 outb(value, port->iobase + offset);
61}
62
63#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
64
65static void __init wait_for_xmitr(struct uart_port *port)
66{
67 unsigned int status;
68
69 for (;;) {
70 status = serial_in(port, UART_LSR);
71 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
72 return;
73 cpu_relax();
74 }
75}
76
77static void __init putc(struct uart_port *port, unsigned char c)
78{
79 wait_for_xmitr(port);
80 serial_out(port, UART_TX, c);
81}
82
83static void __init early_uart_write(struct console *console, const char *s, unsigned int count)
84{
85 struct uart_port *port = &early_device.port;
86 unsigned int ier;
87
88 /* Save the IER and disable interrupts */
89 ier = serial_in(port, UART_IER);
90 serial_out(port, UART_IER, 0);
91
92 while (*s && count-- > 0) {
93 putc(port, *s);
94 if (*s == '\n')
95 putc(port, '\r');
96 s++;
97 }
98
99 /* Wait for transmitter to become empty and restore the IER */
100 wait_for_xmitr(port);
101 serial_out(port, UART_IER, ier);
102}
103
104static unsigned int __init probe_baud(struct uart_port *port)
105{
106 unsigned char lcr, dll, dlm;
107 unsigned int quot;
108
109 lcr = serial_in(port, UART_LCR);
110 serial_out(port, UART_LCR, lcr | UART_LCR_DLAB);
111 dll = serial_in(port, UART_DLL);
112 dlm = serial_in(port, UART_DLM);
113 serial_out(port, UART_LCR, lcr);
114
115 quot = (dlm << 8) | dll;
116 return (port->uartclk / 16) / quot;
117}
118
119static void __init init_port(struct early_uart_device *device)
120{
121 struct uart_port *port = &device->port;
122 unsigned int divisor;
123 unsigned char c;
124
125 serial_out(port, UART_LCR, 0x3); /* 8n1 */
126 serial_out(port, UART_IER, 0); /* no interrupt */
127 serial_out(port, UART_FCR, 0); /* no fifo */
128 serial_out(port, UART_MCR, 0x3); /* DTR + RTS */
129
130 divisor = port->uartclk / (16 * device->baud);
131 c = serial_in(port, UART_LCR);
132 serial_out(port, UART_LCR, c | UART_LCR_DLAB);
133 serial_out(port, UART_DLL, divisor & 0xff);
134 serial_out(port, UART_DLM, (divisor >> 8) & 0xff);
135 serial_out(port, UART_LCR, c & ~UART_LCR_DLAB);
136}
137
138static int __init parse_options(struct early_uart_device *device, char *options)
139{
140 struct uart_port *port = &device->port;
141 int mapsize = 64;
142 int mmio, length;
143
144 if (!options)
145 return -ENODEV;
146
147 port->uartclk = BASE_BAUD * 16;
148 if (!strncmp(options, "mmio,", 5)) {
149 port->iotype = UPIO_MEM;
150 port->mapbase = simple_strtoul(options + 5, &options, 0);
151 port->membase = ioremap(port->mapbase, mapsize);
152 if (!port->membase) {
153 printk(KERN_ERR "%s: Couldn't ioremap 0x%lx\n",
154 __FUNCTION__, port->mapbase);
155 return -ENOMEM;
156 }
157 mmio = 1;
158 } else if (!strncmp(options, "io,", 3)) {
159 port->iotype = UPIO_PORT;
160 port->iobase = simple_strtoul(options + 3, &options, 0);
161 mmio = 0;
162 } else
163 return -EINVAL;
164
165 if ((options = strchr(options, ','))) {
166 options++;
167 device->baud = simple_strtoul(options, 0, 0);
168 length = min(strcspn(options, " "), sizeof(device->options));
169 strncpy(device->options, options, length);
170 } else {
171 device->baud = probe_baud(port);
172 snprintf(device->options, sizeof(device->options), "%u",
173 device->baud);
174 }
175
176 printk(KERN_INFO "Early serial console at %s 0x%lx (options '%s')\n",
177 mmio ? "MMIO" : "I/O port",
178 mmio ? port->mapbase : (unsigned long) port->iobase,
179 device->options);
180 return 0;
181}
182
183static int __init early_uart_setup(struct console *console, char *options)
184{
185 struct early_uart_device *device = &early_device;
186 int err;
187
188 if (device->port.membase || device->port.iobase)
189 return 0;
190
191 if ((err = parse_options(device, options)) < 0)
192 return err;
193
194 init_port(device);
195 return 0;
196}
197
198static struct console early_uart_console __initdata = {
199 .name = "uart",
200 .write = early_uart_write,
201 .setup = early_uart_setup,
202 .flags = CON_PRINTBUFFER,
203 .index = -1,
204};
205
206static int __init early_uart_console_init(void)
207{
208 if (!early_uart_registered) {
209 register_console(&early_uart_console);
210 early_uart_registered = 1;
211 }
212 return 0;
213}
214console_initcall(early_uart_console_init);
215
216int __init early_serial_console_init(char *cmdline)
217{
218 char *options;
219 int err;
220
221 options = strstr(cmdline, "console=uart,");
222 if (!options)
223 return -ENODEV;
224
225 options = strchr(cmdline, ',') + 1;
226 if ((err = early_uart_setup(NULL, options)) < 0)
227 return err;
228 return early_uart_console_init();
229}
230
231static int __init early_uart_console_switch(void)
232{
233 struct early_uart_device *device = &early_device;
234 struct uart_port *port = &device->port;
235 int mmio, line;
236
237 if (!(early_uart_console.flags & CON_ENABLED))
238 return 0;
239
240 /* Try to start the normal driver on a matching line. */
241 mmio = (port->iotype == UPIO_MEM);
242 line = serial8250_start_console(port, device->options);
243 if (line < 0)
244 printk("No ttyS device at %s 0x%lx for console\n",
245 mmio ? "MMIO" : "I/O port",
246 mmio ? port->mapbase :
247 (unsigned long) port->iobase);
248
249 unregister_console(&early_uart_console);
250 if (mmio)
251 iounmap(port->membase);
252
253 return 0;
254}
255late_initcall(early_uart_console_switch);
diff --git a/drivers/serial/8250_gsc.c b/drivers/serial/8250_gsc.c
new file mode 100644
index 000000000000..431aa5761a7a
--- /dev/null
+++ b/drivers/serial/8250_gsc.c
@@ -0,0 +1,120 @@
1/*
2 * Serial Device Initialisation for Lasi/Asp/Wax/Dino
3 *
4 * (c) Copyright Matthew Wilcox <willy@debian.org> 2001-2002
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/errno.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/ioport.h>
16#include <linux/module.h>
17#include <linux/serial_core.h>
18#include <linux/signal.h>
19#include <linux/slab.h>
20#include <linux/types.h>
21
22#include <asm/hardware.h>
23#include <asm/parisc-device.h>
24#include <asm/io.h>
25#include <asm/serial.h> /* for LASI_BASE_BAUD */
26
27#include "8250.h"
28
29static int __init
30serial_init_chip(struct parisc_device *dev)
31{
32 static int serial_line_nr;
33 struct uart_port port;
34 unsigned long address;
35 int err;
36
37 if (!dev->irq) {
38 /* We find some unattached serial ports by walking native
39 * busses. These should be silently ignored. Otherwise,
40 * what we have here is a missing parent device, so tell
41 * the user what they're missing.
42 */
43 if (parisc_parent(dev)->id.hw_type != HPHW_IOA) {
44 printk(KERN_INFO "Serial: device 0x%lx not configured.\n"
45 "Enable support for Wax, Lasi, Asp or Dino.\n", dev->hpa);
46 }
47 return -ENODEV;
48 }
49
50 address = dev->hpa;
51 if (dev->id.sversion != 0x8d) {
52 address += 0x800;
53 }
54
55 memset(&port, 0, sizeof(struct uart_port));
56 port.mapbase = address;
57 port.irq = dev->irq;
58 port.iotype = UPIO_MEM;
59 port.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
60 port.uartclk = LASI_BASE_BAUD * 16;
61 port.dev = &dev->dev;
62
63 err = serial8250_register_port(&port);
64 if (err < 0) {
65 printk(KERN_WARNING "serial8250_register_port returned error %d\n", err);
66 return err;
67 }
68
69 return 0;
70}
71
72static struct parisc_device_id serial_tbl[] = {
73 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00075 },
74 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0008c },
75 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0008d },
76 { 0 }
77};
78
79/* Hack. Some machines have SERIAL_0 attached to Lasi and SERIAL_1
80 * attached to Dino. Unfortunately, Dino appears before Lasi in the device
81 * tree. To ensure that ttyS0 == SERIAL_0, we register two drivers; one
82 * which only knows about Lasi and then a second which will find all the
83 * other serial ports. HPUX ignores this problem.
84 */
85static struct parisc_device_id lasi_tbl[] = {
86 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03B, 0x0008C }, /* C1xx/C1xxL */
87 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03C, 0x0008C }, /* B132L */
88 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03D, 0x0008C }, /* B160L */
89 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03E, 0x0008C }, /* B132L+ */
90 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03F, 0x0008C }, /* B180L+ */
91 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x046, 0x0008C }, /* Rocky2 120 */
92 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x047, 0x0008C }, /* Rocky2 150 */
93 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x04E, 0x0008C }, /* Kiji L2 132 */
94 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x056, 0x0008C }, /* Raven+ */
95 { 0 }
96};
97
98
99MODULE_DEVICE_TABLE(parisc, serial_tbl);
100
101static struct parisc_driver lasi_driver = {
102 .name = "serial_1",
103 .id_table = lasi_tbl,
104 .probe = serial_init_chip,
105};
106
107static struct parisc_driver serial_driver = {
108 .name = "serial",
109 .id_table = serial_tbl,
110 .probe = serial_init_chip,
111};
112
113int __init probe_serial_gsc(void)
114{
115 register_parisc_driver(&lasi_driver);
116 register_parisc_driver(&serial_driver);
117 return 0;
118}
119
120module_init(probe_serial_gsc);
diff --git a/drivers/serial/8250_hp300.c b/drivers/serial/8250_hp300.c
new file mode 100644
index 000000000000..b8d51eb56bff
--- /dev/null
+++ b/drivers/serial/8250_hp300.c
@@ -0,0 +1,329 @@
1/*
2 * Driver for the 98626/98644/internal serial interface on hp300/hp400
3 * (based on the National Semiconductor INS8250/NS16550AF/WD16C552 UARTs)
4 *
5 * Ported from 2.2 and modified to use the normal 8250 driver
6 * by Kars de Jong <jongk@linux-m68k.org>, May 2004.
7 */
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/string.h>
11#include <linux/kernel.h>
12#include <linux/tty.h>
13#include <linux/serial.h>
14#include <linux/serialP.h>
15#include <linux/serial_core.h>
16#include <linux/delay.h>
17#include <linux/dio.h>
18#include <linux/console.h>
19#include <asm/io.h>
20
21#if !defined(CONFIG_HPDCA) && !defined(CONFIG_HPAPCI)
22#warning CONFIG_8250 defined but neither CONFIG_HPDCA nor CONFIG_HPAPCI defined, are you sure?
23#endif
24
25#ifdef CONFIG_HPAPCI
26struct hp300_port
27{
28 struct hp300_port *next; /* next port */
29 int line; /* line (tty) number */
30};
31
32static struct hp300_port *hp300_ports;
33#endif
34
35#ifdef CONFIG_HPDCA
36
37static int __devinit hpdca_init_one(struct dio_dev *d,
38 const struct dio_device_id *ent);
39static void __devexit hpdca_remove_one(struct dio_dev *d);
40
41static struct dio_device_id hpdca_dio_tbl[] = {
42 { DIO_ID_DCA0 },
43 { DIO_ID_DCA0REM },
44 { DIO_ID_DCA1 },
45 { DIO_ID_DCA1REM },
46 { 0 }
47};
48
49static struct dio_driver hpdca_driver = {
50 .name = "hpdca",
51 .id_table = hpdca_dio_tbl,
52 .probe = hpdca_init_one,
53 .remove = __devexit_p(hpdca_remove_one),
54};
55
56#endif
57
58extern int hp300_uart_scode;
59
60/* Offset to UART registers from base of DCA */
61#define UART_OFFSET 17
62
63#define DCA_ID 0x01 /* ID (read), reset (write) */
64#define DCA_IC 0x03 /* Interrupt control */
65
66/* Interrupt control */
67#define DCA_IC_IE 0x80 /* Master interrupt enable */
68
69#define HPDCA_BAUD_BASE 153600
70
71/* Base address of the Frodo part */
72#define FRODO_BASE (0x41c000)
73
74/*
75 * Where we find the 8250-like APCI ports, and how far apart they are.
76 */
77#define FRODO_APCIBASE 0x0
78#define FRODO_APCISPACE 0x20
79#define FRODO_APCI_OFFSET(x) (FRODO_APCIBASE + ((x) * FRODO_APCISPACE))
80
81#define HPAPCI_BAUD_BASE 500400
82
83#ifdef CONFIG_SERIAL_8250_CONSOLE
84/*
85 * Parse the bootinfo to find descriptions for headless console and
86 * debug serial ports and register them with the 8250 driver.
87 * This function should be called before serial_console_init() is called
88 * to make sure the serial console will be available for use. IA-64 kernel
89 * calls this function from setup_arch() after the EFI and ACPI tables have
90 * been parsed.
91 */
92int __init hp300_setup_serial_console(void)
93{
94 int scode;
95 struct uart_port port;
96
97 memset(&port, 0, sizeof(port));
98
99 if (hp300_uart_scode < 0 || hp300_uart_scode > DIO_SCMAX)
100 return 0;
101
102 if (DIO_SCINHOLE(hp300_uart_scode))
103 return 0;
104
105 scode = hp300_uart_scode;
106
107 /* Memory mapped I/O */
108 port.iotype = UPIO_MEM;
109 port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF;
110 port.type = PORT_UNKNOWN;
111
112 /* Check for APCI console */
113 if (scode == 256) {
114#ifdef CONFIG_HPAPCI
115 printk(KERN_INFO "Serial console is HP APCI 1\n");
116
117 port.uartclk = HPAPCI_BAUD_BASE * 16;
118 port.mapbase = (FRODO_BASE + FRODO_APCI_OFFSET(1));
119 port.membase = (char *)(port.mapbase + DIO_VIRADDRBASE);
120 port.regshift = 2;
121 add_preferred_console("ttyS", port.line, "9600n8");
122#else
123 printk(KERN_WARNING "Serial console is APCI but support is disabled (CONFIG_HPAPCI)!\n");
124 return 0;
125#endif
126 }
127 else {
128#ifdef CONFIG_HPDCA
129 unsigned long pa = dio_scodetophysaddr(scode);
130 if (!pa) {
131 return 0;
132 }
133
134 printk(KERN_INFO "Serial console is HP DCA at select code %d\n", scode);
135
136 port.uartclk = HPDCA_BAUD_BASE * 16;
137 port.mapbase = (pa + UART_OFFSET);
138 port.membase = (char *)(port.mapbase + DIO_VIRADDRBASE);
139 port.regshift = 1;
140 port.irq = DIO_IPL(pa + DIO_VIRADDRBASE);
141
142 /* Enable board-interrupts */
143 out_8(pa + DIO_VIRADDRBASE + DCA_IC, DCA_IC_IE);
144
145 if (DIO_ID(pa + DIO_VIRADDRBASE) & 0x80) {
146 add_preferred_console("ttyS", port.line, "9600n8");
147 }
148#else
149 printk(KERN_WARNING "Serial console is DCA but support is disabled (CONFIG_HPDCA)!\n");
150 return 0;
151#endif
152 }
153
154 if (early_serial_setup(&port) < 0) {
155 printk(KERN_WARNING "hp300_setup_serial_console(): early_serial_setup() failed.\n");
156 }
157
158 return 0;
159}
160#endif /* CONFIG_SERIAL_8250_CONSOLE */
161
162#ifdef CONFIG_HPDCA
163static int __devinit hpdca_init_one(struct dio_dev *d,
164 const struct dio_device_id *ent)
165{
166 struct serial_struct serial_req;
167 int line;
168
169#ifdef CONFIG_SERIAL_8250_CONSOLE
170 if (hp300_uart_scode == d->scode) {
171 /* Already got it. */
172 return 0;
173 }
174#endif
175 memset(&serial_req, 0, sizeof(struct serial_struct));
176
177 /* Memory mapped I/O */
178 serial_req.io_type = SERIAL_IO_MEM;
179 serial_req.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF;
180 serial_req.irq = d->ipl;
181 serial_req.baud_base = HPDCA_BAUD_BASE;
182 serial_req.iomap_base = (d->resource.start + UART_OFFSET);
183 serial_req.iomem_base = (char *)(serial_req.iomap_base + DIO_VIRADDRBASE);
184 serial_req.iomem_reg_shift = 1;
185 line = register_serial(&serial_req);
186
187 if (line < 0) {
188 printk(KERN_NOTICE "8250_hp300: register_serial() DCA scode %d"
189 " irq %d failed\n", d->scode, serial_req.irq);
190 return -ENOMEM;
191 }
192
193 /* Enable board-interrupts */
194 out_8(d->resource.start + DIO_VIRADDRBASE + DCA_IC, DCA_IC_IE);
195 dio_set_drvdata(d, (void *)line);
196
197 /* Reset the DCA */
198 out_8(d->resource.start + DIO_VIRADDRBASE + DCA_ID, 0xff);
199 udelay(100);
200
201 return 0;
202}
203#endif
204
205static int __init hp300_8250_init(void)
206{
207 static int called = 0;
208 int num_ports;
209#ifdef CONFIG_HPAPCI
210 int line;
211 unsigned long base;
212 struct serial_struct serial_req;
213 struct hp300_port *port;
214 int i;
215#endif
216 if (called)
217 return -ENODEV;
218 called = 1;
219
220 if (!MACH_IS_HP300)
221 return -ENODEV;
222
223 num_ports = 0;
224
225#ifdef CONFIG_HPDCA
226 if (dio_module_init(&hpdca_driver) == 0)
227 num_ports++;
228#endif
229#ifdef CONFIG_HPAPCI
230 if (hp300_model < HP_400) {
231 if (!num_ports)
232 return -ENODEV;
233 return 0;
234 }
235 /* These models have the Frodo chip.
236 * Port 0 is reserved for the Apollo Domain keyboard.
237 * Port 1 is either the console or the DCA.
238 */
239 for (i = 1; i < 4; i++) {
240 /* Port 1 is the console on a 425e, on other machines it's mapped to
241 * DCA.
242 */
243#ifdef CONFIG_SERIAL_8250_CONSOLE
244 if (i == 1) {
245 continue;
246 }
247#endif
248
249 /* Create new serial device */
250 port = kmalloc(sizeof(struct hp300_port), GFP_KERNEL);
251 if (!port)
252 return -ENOMEM;
253
254 memset(&serial_req, 0, sizeof(struct serial_struct));
255
256 base = (FRODO_BASE + FRODO_APCI_OFFSET(i));
257
258 /* Memory mapped I/O */
259 serial_req.io_type = SERIAL_IO_MEM;
260 serial_req.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF;
261 /* XXX - no interrupt support yet */
262 serial_req.irq = 0;
263 serial_req.baud_base = HPAPCI_BAUD_BASE;
264 serial_req.iomap_base = base;
265 serial_req.iomem_base = (char *)(serial_req.iomap_base + DIO_VIRADDRBASE);
266 serial_req.iomem_reg_shift = 2;
267
268 line = register_serial(&serial_req);
269
270 if (line < 0) {
271 printk(KERN_NOTICE "8250_hp300: register_serial() APCI %d"
272 " irq %d failed\n", i, serial_req.irq);
273 kfree(port);
274 continue;
275 }
276
277 port->line = line;
278 port->next = hp300_ports;
279 hp300_ports = port;
280
281 num_ports++;
282 }
283#endif
284
285 /* Any boards found? */
286 if (!num_ports)
287 return -ENODEV;
288
289 return 0;
290}
291
292#ifdef CONFIG_HPDCA
293static void __devexit hpdca_remove_one(struct dio_dev *d)
294{
295 int line;
296
297 line = (int) dio_get_drvdata(d);
298 if (d->resource.start) {
299 /* Disable board-interrupts */
300 out_8(d->resource.start + DIO_VIRADDRBASE + DCA_IC, 0);
301 }
302 unregister_serial(line);
303}
304#endif
305
306static void __exit hp300_8250_exit(void)
307{
308#ifdef CONFIG_HPAPCI
309 struct hp300_port *port, *to_free;
310
311 for (port = hp300_ports; port; ) {
312 unregister_serial(port->line);
313 to_free = port;
314 port = port->next;
315 kfree(to_free);
316 }
317
318 hp300_ports = NULL;
319#endif
320#ifdef CONFIG_HPDCA
321 dio_unregister_driver(&hpdca_driver);
322#endif
323}
324
325module_init(hp300_8250_init);
326module_exit(hp300_8250_exit);
327MODULE_DESCRIPTION("HP DCA/APCI serial driver");
328MODULE_AUTHOR("Kars de Jong <jongk@linux-m68k.org>");
329MODULE_LICENSE("GPL");
diff --git a/drivers/serial/8250_pci.c b/drivers/serial/8250_pci.c
new file mode 100644
index 000000000000..f8d90d0ecfea
--- /dev/null
+++ b/drivers/serial/8250_pci.c
@@ -0,0 +1,2303 @@
1/*
2 * linux/drivers/char/8250_pci.c
3 *
4 * Probe module for 8250/16550-type PCI serial ports.
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright (C) 2001 Russell King, All Rights Reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License.
13 *
14 * $Id: 8250_pci.c,v 1.28 2002/11/02 11:14:18 rmk Exp $
15 */
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/pci.h>
19#include <linux/sched.h>
20#include <linux/string.h>
21#include <linux/kernel.h>
22#include <linux/slab.h>
23#include <linux/delay.h>
24#include <linux/tty.h>
25#include <linux/serial_core.h>
26#include <linux/8250_pci.h>
27#include <linux/bitops.h>
28
29#include <asm/byteorder.h>
30#include <asm/io.h>
31
32#include "8250.h"
33
34#undef SERIAL_DEBUG_PCI
35
36/*
37 * Definitions for PCI support.
38 */
39#define FL_BASE_MASK 0x0007
40#define FL_BASE0 0x0000
41#define FL_BASE1 0x0001
42#define FL_BASE2 0x0002
43#define FL_BASE3 0x0003
44#define FL_BASE4 0x0004
45#define FL_GET_BASE(x) (x & FL_BASE_MASK)
46
47/* Use successive BARs (PCI base address registers),
48 else use offset into some specified BAR */
49#define FL_BASE_BARS 0x0008
50
51/* do not assign an irq */
52#define FL_NOIRQ 0x0080
53
54/* Use the Base address register size to cap number of ports */
55#define FL_REGION_SZ_CAP 0x0100
56
57struct pci_board {
58 unsigned int flags;
59 unsigned int num_ports;
60 unsigned int base_baud;
61 unsigned int uart_offset;
62 unsigned int reg_shift;
63 unsigned int first_offset;
64};
65
66/*
67 * init function returns:
68 * > 0 - number of ports
69 * = 0 - use board->num_ports
70 * < 0 - error
71 */
72struct pci_serial_quirk {
73 u32 vendor;
74 u32 device;
75 u32 subvendor;
76 u32 subdevice;
77 int (*init)(struct pci_dev *dev);
78 int (*setup)(struct pci_dev *dev, struct pci_board *board,
79 struct uart_port *port, int idx);
80 void (*exit)(struct pci_dev *dev);
81};
82
83#define PCI_NUM_BAR_RESOURCES 6
84
85struct serial_private {
86 unsigned int nr;
87 void __iomem *remapped_bar[PCI_NUM_BAR_RESOURCES];
88 struct pci_serial_quirk *quirk;
89 int line[0];
90};
91
92static void moan_device(const char *str, struct pci_dev *dev)
93{
94 printk(KERN_WARNING "%s: %s\n"
95 KERN_WARNING "Please send the output of lspci -vv, this\n"
96 KERN_WARNING "message (0x%04x,0x%04x,0x%04x,0x%04x), the\n"
97 KERN_WARNING "manufacturer and name of serial board or\n"
98 KERN_WARNING "modem board to rmk+serial@arm.linux.org.uk.\n",
99 pci_name(dev), str, dev->vendor, dev->device,
100 dev->subsystem_vendor, dev->subsystem_device);
101}
102
103static int
104setup_port(struct pci_dev *dev, struct uart_port *port,
105 int bar, int offset, int regshift)
106{
107 struct serial_private *priv = pci_get_drvdata(dev);
108 unsigned long base, len;
109
110 if (bar >= PCI_NUM_BAR_RESOURCES)
111 return -EINVAL;
112
113 if (pci_resource_flags(dev, bar) & IORESOURCE_MEM) {
114 base = pci_resource_start(dev, bar);
115 len = pci_resource_len(dev, bar);
116
117 if (!priv->remapped_bar[bar])
118 priv->remapped_bar[bar] = ioremap(base, len);
119 if (!priv->remapped_bar[bar])
120 return -ENOMEM;
121
122 port->iotype = UPIO_MEM;
123 port->mapbase = base + offset;
124 port->membase = priv->remapped_bar[bar] + offset;
125 port->regshift = regshift;
126 } else {
127 base = pci_resource_start(dev, bar) + offset;
128 port->iotype = UPIO_PORT;
129 port->iobase = base;
130 }
131 return 0;
132}
133
134/*
135 * AFAVLAB uses a different mixture of BARs and offsets
136 * Not that ugly ;) -- HW
137 */
138static int
139afavlab_setup(struct pci_dev *dev, struct pci_board *board,
140 struct uart_port *port, int idx)
141{
142 unsigned int bar, offset = board->first_offset;
143
144 bar = FL_GET_BASE(board->flags);
145 if (idx < 4)
146 bar += idx;
147 else {
148 bar = 4;
149 offset += (idx - 4) * board->uart_offset;
150 }
151
152 return setup_port(dev, port, bar, offset, board->reg_shift);
153}
154
155/*
156 * HP's Remote Management Console. The Diva chip came in several
157 * different versions. N-class, L2000 and A500 have two Diva chips, each
158 * with 3 UARTs (the third UART on the second chip is unused). Superdome
159 * and Keystone have one Diva chip with 3 UARTs. Some later machines have
160 * one Diva chip, but it has been expanded to 5 UARTs.
161 */
162static int __devinit pci_hp_diva_init(struct pci_dev *dev)
163{
164 int rc = 0;
165
166 switch (dev->subsystem_device) {
167 case PCI_DEVICE_ID_HP_DIVA_TOSCA1:
168 case PCI_DEVICE_ID_HP_DIVA_HALFDOME:
169 case PCI_DEVICE_ID_HP_DIVA_KEYSTONE:
170 case PCI_DEVICE_ID_HP_DIVA_EVEREST:
171 rc = 3;
172 break;
173 case PCI_DEVICE_ID_HP_DIVA_TOSCA2:
174 rc = 2;
175 break;
176 case PCI_DEVICE_ID_HP_DIVA_MAESTRO:
177 rc = 4;
178 break;
179 case PCI_DEVICE_ID_HP_DIVA_POWERBAR:
180 rc = 1;
181 break;
182 }
183
184 return rc;
185}
186
187/*
188 * HP's Diva chip puts the 4th/5th serial port further out, and
189 * some serial ports are supposed to be hidden on certain models.
190 */
191static int
192pci_hp_diva_setup(struct pci_dev *dev, struct pci_board *board,
193 struct uart_port *port, int idx)
194{
195 unsigned int offset = board->first_offset;
196 unsigned int bar = FL_GET_BASE(board->flags);
197
198 switch (dev->subsystem_device) {
199 case PCI_DEVICE_ID_HP_DIVA_MAESTRO:
200 if (idx == 3)
201 idx++;
202 break;
203 case PCI_DEVICE_ID_HP_DIVA_EVEREST:
204 if (idx > 0)
205 idx++;
206 if (idx > 2)
207 idx++;
208 break;
209 }
210 if (idx > 2)
211 offset = 0x18;
212
213 offset += idx * board->uart_offset;
214
215 return setup_port(dev, port, bar, offset, board->reg_shift);
216}
217
218/*
219 * Added for EKF Intel i960 serial boards
220 */
221static int __devinit pci_inteli960ni_init(struct pci_dev *dev)
222{
223 unsigned long oldval;
224
225 if (!(dev->subsystem_device & 0x1000))
226 return -ENODEV;
227
228 /* is firmware started? */
229 pci_read_config_dword(dev, 0x44, (void*) &oldval);
230 if (oldval == 0x00001000L) { /* RESET value */
231 printk(KERN_DEBUG "Local i960 firmware missing");
232 return -ENODEV;
233 }
234 return 0;
235}
236
237/*
238 * Some PCI serial cards using the PLX 9050 PCI interface chip require
239 * that the card interrupt be explicitly enabled or disabled. This
240 * seems to be mainly needed on card using the PLX which also use I/O
241 * mapped memory.
242 */
243static int __devinit pci_plx9050_init(struct pci_dev *dev)
244{
245 u8 irq_config;
246 void __iomem *p;
247
248 if ((pci_resource_flags(dev, 0) & IORESOURCE_MEM) == 0) {
249 moan_device("no memory in bar 0", dev);
250 return 0;
251 }
252
253 irq_config = 0x41;
254 if (dev->vendor == PCI_VENDOR_ID_PANACOM)
255 irq_config = 0x43;
256 if ((dev->vendor == PCI_VENDOR_ID_PLX) &&
257 (dev->device == PCI_DEVICE_ID_PLX_ROMULUS)) {
258 /*
259 * As the megawolf cards have the int pins active
260 * high, and have 2 UART chips, both ints must be
261 * enabled on the 9050. Also, the UARTS are set in
262 * 16450 mode by default, so we have to enable the
263 * 16C950 'enhanced' mode so that we can use the
264 * deep FIFOs
265 */
266 irq_config = 0x5b;
267 }
268
269 /*
270 * enable/disable interrupts
271 */
272 p = ioremap(pci_resource_start(dev, 0), 0x80);
273 if (p == NULL)
274 return -ENOMEM;
275 writel(irq_config, p + 0x4c);
276
277 /*
278 * Read the register back to ensure that it took effect.
279 */
280 readl(p + 0x4c);
281 iounmap(p);
282
283 return 0;
284}
285
286static void __devexit pci_plx9050_exit(struct pci_dev *dev)
287{
288 u8 __iomem *p;
289
290 if ((pci_resource_flags(dev, 0) & IORESOURCE_MEM) == 0)
291 return;
292
293 /*
294 * disable interrupts
295 */
296 p = ioremap(pci_resource_start(dev, 0), 0x80);
297 if (p != NULL) {
298 writel(0, p + 0x4c);
299
300 /*
301 * Read the register back to ensure that it took effect.
302 */
303 readl(p + 0x4c);
304 iounmap(p);
305 }
306}
307
308/* SBS Technologies Inc. PMC-OCTPRO and P-OCTAL cards */
309static int
310sbs_setup(struct pci_dev *dev, struct pci_board *board,
311 struct uart_port *port, int idx)
312{
313 unsigned int bar, offset = board->first_offset;
314
315 bar = 0;
316
317 if (idx < 4) {
318 /* first four channels map to 0, 0x100, 0x200, 0x300 */
319 offset += idx * board->uart_offset;
320 } else if (idx < 8) {
321 /* last four channels map to 0x1000, 0x1100, 0x1200, 0x1300 */
322 offset += idx * board->uart_offset + 0xC00;
323 } else /* we have only 8 ports on PMC-OCTALPRO */
324 return 1;
325
326 return setup_port(dev, port, bar, offset, board->reg_shift);
327}
328
329/*
330* This does initialization for PMC OCTALPRO cards:
331* maps the device memory, resets the UARTs (needed, bc
332* if the module is removed and inserted again, the card
333* is in the sleep mode) and enables global interrupt.
334*/
335
336/* global control register offset for SBS PMC-OctalPro */
337#define OCT_REG_CR_OFF 0x500
338
339static int __devinit sbs_init(struct pci_dev *dev)
340{
341 u8 __iomem *p;
342
343 p = ioremap(pci_resource_start(dev, 0),pci_resource_len(dev,0));
344
345 if (p == NULL)
346 return -ENOMEM;
347 /* Set bit-4 Control Register (UART RESET) in to reset the uarts */
348 writeb(0x10,p + OCT_REG_CR_OFF);
349 udelay(50);
350 writeb(0x0,p + OCT_REG_CR_OFF);
351
352 /* Set bit-2 (INTENABLE) of Control Register */
353 writeb(0x4, p + OCT_REG_CR_OFF);
354 iounmap(p);
355
356 return 0;
357}
358
359/*
360 * Disables the global interrupt of PMC-OctalPro
361 */
362
363static void __devexit sbs_exit(struct pci_dev *dev)
364{
365 u8 __iomem *p;
366
367 p = ioremap(pci_resource_start(dev, 0),pci_resource_len(dev,0));
368 if (p != NULL) {
369 writeb(0, p + OCT_REG_CR_OFF);
370 }
371 iounmap(p);
372}
373
374/*
375 * SIIG serial cards have an PCI interface chip which also controls
376 * the UART clocking frequency. Each UART can be clocked independently
377 * (except cards equiped with 4 UARTs) and initial clocking settings
378 * are stored in the EEPROM chip. It can cause problems because this
379 * version of serial driver doesn't support differently clocked UART's
380 * on single PCI card. To prevent this, initialization functions set
381 * high frequency clocking for all UART's on given card. It is safe (I
382 * hope) because it doesn't touch EEPROM settings to prevent conflicts
383 * with other OSes (like M$ DOS).
384 *
385 * SIIG support added by Andrey Panin <pazke@donpac.ru>, 10/1999
386 *
387 * There is two family of SIIG serial cards with different PCI
388 * interface chip and different configuration methods:
389 * - 10x cards have control registers in IO and/or memory space;
390 * - 20x cards have control registers in standard PCI configuration space.
391 *
392 * Note: some SIIG cards are probed by the parport_serial object.
393 */
394
395#define PCI_DEVICE_ID_SIIG_1S_10x (PCI_DEVICE_ID_SIIG_1S_10x_550 & 0xfffc)
396#define PCI_DEVICE_ID_SIIG_2S_10x (PCI_DEVICE_ID_SIIG_2S_10x_550 & 0xfff8)
397
398static int pci_siig10x_init(struct pci_dev *dev)
399{
400 u16 data;
401 void __iomem *p;
402
403 switch (dev->device & 0xfff8) {
404 case PCI_DEVICE_ID_SIIG_1S_10x: /* 1S */
405 data = 0xffdf;
406 break;
407 case PCI_DEVICE_ID_SIIG_2S_10x: /* 2S, 2S1P */
408 data = 0xf7ff;
409 break;
410 default: /* 1S1P, 4S */
411 data = 0xfffb;
412 break;
413 }
414
415 p = ioremap(pci_resource_start(dev, 0), 0x80);
416 if (p == NULL)
417 return -ENOMEM;
418
419 writew(readw(p + 0x28) & data, p + 0x28);
420 readw(p + 0x28);
421 iounmap(p);
422 return 0;
423}
424
425#define PCI_DEVICE_ID_SIIG_2S_20x (PCI_DEVICE_ID_SIIG_2S_20x_550 & 0xfffc)
426#define PCI_DEVICE_ID_SIIG_2S1P_20x (PCI_DEVICE_ID_SIIG_2S1P_20x_550 & 0xfffc)
427
428static int pci_siig20x_init(struct pci_dev *dev)
429{
430 u8 data;
431
432 /* Change clock frequency for the first UART. */
433 pci_read_config_byte(dev, 0x6f, &data);
434 pci_write_config_byte(dev, 0x6f, data & 0xef);
435
436 /* If this card has 2 UART, we have to do the same with second UART. */
437 if (((dev->device & 0xfffc) == PCI_DEVICE_ID_SIIG_2S_20x) ||
438 ((dev->device & 0xfffc) == PCI_DEVICE_ID_SIIG_2S1P_20x)) {
439 pci_read_config_byte(dev, 0x73, &data);
440 pci_write_config_byte(dev, 0x73, data & 0xef);
441 }
442 return 0;
443}
444
445int pci_siig10x_fn(struct pci_dev *dev, int enable)
446{
447 int ret = 0;
448 if (enable)
449 ret = pci_siig10x_init(dev);
450 return ret;
451}
452
453int pci_siig20x_fn(struct pci_dev *dev, int enable)
454{
455 int ret = 0;
456 if (enable)
457 ret = pci_siig20x_init(dev);
458 return ret;
459}
460
461EXPORT_SYMBOL(pci_siig10x_fn);
462EXPORT_SYMBOL(pci_siig20x_fn);
463
464/*
465 * Timedia has an explosion of boards, and to avoid the PCI table from
466 * growing *huge*, we use this function to collapse some 70 entries
467 * in the PCI table into one, for sanity's and compactness's sake.
468 */
469static unsigned short timedia_single_port[] = {
470 0x4025, 0x4027, 0x4028, 0x5025, 0x5027, 0
471};
472
473static unsigned short timedia_dual_port[] = {
474 0x0002, 0x4036, 0x4037, 0x4038, 0x4078, 0x4079, 0x4085,
475 0x4088, 0x4089, 0x5037, 0x5078, 0x5079, 0x5085, 0x6079,
476 0x7079, 0x8079, 0x8137, 0x8138, 0x8237, 0x8238, 0x9079,
477 0x9137, 0x9138, 0x9237, 0x9238, 0xA079, 0xB079, 0xC079,
478 0xD079, 0
479};
480
481static unsigned short timedia_quad_port[] = {
482 0x4055, 0x4056, 0x4095, 0x4096, 0x5056, 0x8156, 0x8157,
483 0x8256, 0x8257, 0x9056, 0x9156, 0x9157, 0x9158, 0x9159,
484 0x9256, 0x9257, 0xA056, 0xA157, 0xA158, 0xA159, 0xB056,
485 0xB157, 0
486};
487
488static unsigned short timedia_eight_port[] = {
489 0x4065, 0x4066, 0x5065, 0x5066, 0x8166, 0x9066, 0x9166,
490 0x9167, 0x9168, 0xA066, 0xA167, 0xA168, 0
491};
492
493static struct timedia_struct {
494 int num;
495 unsigned short *ids;
496} timedia_data[] = {
497 { 1, timedia_single_port },
498 { 2, timedia_dual_port },
499 { 4, timedia_quad_port },
500 { 8, timedia_eight_port },
501 { 0, NULL }
502};
503
504static int __devinit pci_timedia_init(struct pci_dev *dev)
505{
506 unsigned short *ids;
507 int i, j;
508
509 for (i = 0; timedia_data[i].num; i++) {
510 ids = timedia_data[i].ids;
511 for (j = 0; ids[j]; j++)
512 if (dev->subsystem_device == ids[j])
513 return timedia_data[i].num;
514 }
515 return 0;
516}
517
518/*
519 * Timedia/SUNIX uses a mixture of BARs and offsets
520 * Ugh, this is ugly as all hell --- TYT
521 */
522static int
523pci_timedia_setup(struct pci_dev *dev, struct pci_board *board,
524 struct uart_port *port, int idx)
525{
526 unsigned int bar = 0, offset = board->first_offset;
527
528 switch (idx) {
529 case 0:
530 bar = 0;
531 break;
532 case 1:
533 offset = board->uart_offset;
534 bar = 0;
535 break;
536 case 2:
537 bar = 1;
538 break;
539 case 3:
540 offset = board->uart_offset;
541 bar = 1;
542 case 4: /* BAR 2 */
543 case 5: /* BAR 3 */
544 case 6: /* BAR 4 */
545 case 7: /* BAR 5 */
546 bar = idx - 2;
547 }
548
549 return setup_port(dev, port, bar, offset, board->reg_shift);
550}
551
552/*
553 * Some Titan cards are also a little weird
554 */
555static int
556titan_400l_800l_setup(struct pci_dev *dev, struct pci_board *board,
557 struct uart_port *port, int idx)
558{
559 unsigned int bar, offset = board->first_offset;
560
561 switch (idx) {
562 case 0:
563 bar = 1;
564 break;
565 case 1:
566 bar = 2;
567 break;
568 default:
569 bar = 4;
570 offset = (idx - 2) * board->uart_offset;
571 }
572
573 return setup_port(dev, port, bar, offset, board->reg_shift);
574}
575
576static int __devinit pci_xircom_init(struct pci_dev *dev)
577{
578 msleep(100);
579 return 0;
580}
581
582static int __devinit pci_netmos_init(struct pci_dev *dev)
583{
584 /* subdevice 0x00PS means <P> parallel, <S> serial */
585 unsigned int num_serial = dev->subsystem_device & 0xf;
586
587 if (num_serial == 0)
588 return -ENODEV;
589 return num_serial;
590}
591
592static int
593pci_default_setup(struct pci_dev *dev, struct pci_board *board,
594 struct uart_port *port, int idx)
595{
596 unsigned int bar, offset = board->first_offset, maxnr;
597
598 bar = FL_GET_BASE(board->flags);
599 if (board->flags & FL_BASE_BARS)
600 bar += idx;
601 else
602 offset += idx * board->uart_offset;
603
604 maxnr = (pci_resource_len(dev, bar) - board->first_offset) /
605 (8 << board->reg_shift);
606
607 if (board->flags & FL_REGION_SZ_CAP && idx >= maxnr)
608 return 1;
609
610 return setup_port(dev, port, bar, offset, board->reg_shift);
611}
612
613/* This should be in linux/pci_ids.h */
614#define PCI_VENDOR_ID_SBSMODULARIO 0x124B
615#define PCI_SUBVENDOR_ID_SBSMODULARIO 0x124B
616#define PCI_DEVICE_ID_OCTPRO 0x0001
617#define PCI_SUBDEVICE_ID_OCTPRO232 0x0108
618#define PCI_SUBDEVICE_ID_OCTPRO422 0x0208
619#define PCI_SUBDEVICE_ID_POCTAL232 0x0308
620#define PCI_SUBDEVICE_ID_POCTAL422 0x0408
621
622/*
623 * Master list of serial port init/setup/exit quirks.
624 * This does not describe the general nature of the port.
625 * (ie, baud base, number and location of ports, etc)
626 *
627 * This list is ordered alphabetically by vendor then device.
628 * Specific entries must come before more generic entries.
629 */
630static struct pci_serial_quirk pci_serial_quirks[] = {
631 /*
632 * AFAVLAB cards.
633 * It is not clear whether this applies to all products.
634 */
635 {
636 .vendor = PCI_VENDOR_ID_AFAVLAB,
637 .device = PCI_ANY_ID,
638 .subvendor = PCI_ANY_ID,
639 .subdevice = PCI_ANY_ID,
640 .setup = afavlab_setup,
641 },
642 /*
643 * HP Diva
644 */
645 {
646 .vendor = PCI_VENDOR_ID_HP,
647 .device = PCI_DEVICE_ID_HP_DIVA,
648 .subvendor = PCI_ANY_ID,
649 .subdevice = PCI_ANY_ID,
650 .init = pci_hp_diva_init,
651 .setup = pci_hp_diva_setup,
652 },
653 /*
654 * Intel
655 */
656 {
657 .vendor = PCI_VENDOR_ID_INTEL,
658 .device = PCI_DEVICE_ID_INTEL_80960_RP,
659 .subvendor = 0xe4bf,
660 .subdevice = PCI_ANY_ID,
661 .init = pci_inteli960ni_init,
662 .setup = pci_default_setup,
663 },
664 /*
665 * Panacom
666 */
667 {
668 .vendor = PCI_VENDOR_ID_PANACOM,
669 .device = PCI_DEVICE_ID_PANACOM_QUADMODEM,
670 .subvendor = PCI_ANY_ID,
671 .subdevice = PCI_ANY_ID,
672 .init = pci_plx9050_init,
673 .setup = pci_default_setup,
674 .exit = __devexit_p(pci_plx9050_exit),
675 },
676 {
677 .vendor = PCI_VENDOR_ID_PANACOM,
678 .device = PCI_DEVICE_ID_PANACOM_DUALMODEM,
679 .subvendor = PCI_ANY_ID,
680 .subdevice = PCI_ANY_ID,
681 .init = pci_plx9050_init,
682 .setup = pci_default_setup,
683 .exit = __devexit_p(pci_plx9050_exit),
684 },
685 /*
686 * PLX
687 */
688 {
689 .vendor = PCI_VENDOR_ID_PLX,
690 .device = PCI_DEVICE_ID_PLX_9050,
691 .subvendor = PCI_SUBVENDOR_ID_KEYSPAN,
692 .subdevice = PCI_SUBDEVICE_ID_KEYSPAN_SX2,
693 .init = pci_plx9050_init,
694 .setup = pci_default_setup,
695 .exit = __devexit_p(pci_plx9050_exit),
696 },
697 {
698 .vendor = PCI_VENDOR_ID_PLX,
699 .device = PCI_DEVICE_ID_PLX_ROMULUS,
700 .subvendor = PCI_VENDOR_ID_PLX,
701 .subdevice = PCI_DEVICE_ID_PLX_ROMULUS,
702 .init = pci_plx9050_init,
703 .setup = pci_default_setup,
704 .exit = __devexit_p(pci_plx9050_exit),
705 },
706 /*
707 * SBS Technologies, Inc., PMC-OCTALPRO 232
708 */
709 {
710 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
711 .device = PCI_DEVICE_ID_OCTPRO,
712 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
713 .subdevice = PCI_SUBDEVICE_ID_OCTPRO232,
714 .init = sbs_init,
715 .setup = sbs_setup,
716 .exit = __devexit_p(sbs_exit),
717 },
718 /*
719 * SBS Technologies, Inc., PMC-OCTALPRO 422
720 */
721 {
722 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
723 .device = PCI_DEVICE_ID_OCTPRO,
724 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
725 .subdevice = PCI_SUBDEVICE_ID_OCTPRO422,
726 .init = sbs_init,
727 .setup = sbs_setup,
728 .exit = __devexit_p(sbs_exit),
729 },
730 /*
731 * SBS Technologies, Inc., P-Octal 232
732 */
733 {
734 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
735 .device = PCI_DEVICE_ID_OCTPRO,
736 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
737 .subdevice = PCI_SUBDEVICE_ID_POCTAL232,
738 .init = sbs_init,
739 .setup = sbs_setup,
740 .exit = __devexit_p(sbs_exit),
741 },
742 /*
743 * SBS Technologies, Inc., P-Octal 422
744 */
745 {
746 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
747 .device = PCI_DEVICE_ID_OCTPRO,
748 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
749 .subdevice = PCI_SUBDEVICE_ID_POCTAL422,
750 .init = sbs_init,
751 .setup = sbs_setup,
752 .exit = __devexit_p(sbs_exit),
753 },
754
755 /*
756 * SIIG cards.
757 * It is not clear whether these could be collapsed.
758 */
759 {
760 .vendor = PCI_VENDOR_ID_SIIG,
761 .device = PCI_DEVICE_ID_SIIG_1S_10x_550,
762 .subvendor = PCI_ANY_ID,
763 .subdevice = PCI_ANY_ID,
764 .init = pci_siig10x_init,
765 .setup = pci_default_setup,
766 },
767 {
768 .vendor = PCI_VENDOR_ID_SIIG,
769 .device = PCI_DEVICE_ID_SIIG_1S_10x_650,
770 .subvendor = PCI_ANY_ID,
771 .subdevice = PCI_ANY_ID,
772 .init = pci_siig10x_init,
773 .setup = pci_default_setup,
774 },
775 {
776 .vendor = PCI_VENDOR_ID_SIIG,
777 .device = PCI_DEVICE_ID_SIIG_1S_10x_850,
778 .subvendor = PCI_ANY_ID,
779 .subdevice = PCI_ANY_ID,
780 .init = pci_siig10x_init,
781 .setup = pci_default_setup,
782 },
783 {
784 .vendor = PCI_VENDOR_ID_SIIG,
785 .device = PCI_DEVICE_ID_SIIG_2S_10x_550,
786 .subvendor = PCI_ANY_ID,
787 .subdevice = PCI_ANY_ID,
788 .init = pci_siig10x_init,
789 .setup = pci_default_setup,
790 },
791 {
792 .vendor = PCI_VENDOR_ID_SIIG,
793 .device = PCI_DEVICE_ID_SIIG_2S_10x_650,
794 .subvendor = PCI_ANY_ID,
795 .subdevice = PCI_ANY_ID,
796 .init = pci_siig10x_init,
797 .setup = pci_default_setup,
798 },
799 {
800 .vendor = PCI_VENDOR_ID_SIIG,
801 .device = PCI_DEVICE_ID_SIIG_2S_10x_850,
802 .subvendor = PCI_ANY_ID,
803 .subdevice = PCI_ANY_ID,
804 .init = pci_siig10x_init,
805 .setup = pci_default_setup,
806 },
807 {
808 .vendor = PCI_VENDOR_ID_SIIG,
809 .device = PCI_DEVICE_ID_SIIG_4S_10x_550,
810 .subvendor = PCI_ANY_ID,
811 .subdevice = PCI_ANY_ID,
812 .init = pci_siig10x_init,
813 .setup = pci_default_setup,
814 },
815 {
816 .vendor = PCI_VENDOR_ID_SIIG,
817 .device = PCI_DEVICE_ID_SIIG_4S_10x_650,
818 .subvendor = PCI_ANY_ID,
819 .subdevice = PCI_ANY_ID,
820 .init = pci_siig10x_init,
821 .setup = pci_default_setup,
822 },
823 {
824 .vendor = PCI_VENDOR_ID_SIIG,
825 .device = PCI_DEVICE_ID_SIIG_4S_10x_850,
826 .subvendor = PCI_ANY_ID,
827 .subdevice = PCI_ANY_ID,
828 .init = pci_siig10x_init,
829 .setup = pci_default_setup,
830 },
831 {
832 .vendor = PCI_VENDOR_ID_SIIG,
833 .device = PCI_DEVICE_ID_SIIG_1S_20x_550,
834 .subvendor = PCI_ANY_ID,
835 .subdevice = PCI_ANY_ID,
836 .init = pci_siig20x_init,
837 .setup = pci_default_setup,
838 },
839 {
840 .vendor = PCI_VENDOR_ID_SIIG,
841 .device = PCI_DEVICE_ID_SIIG_1S_20x_650,
842 .subvendor = PCI_ANY_ID,
843 .subdevice = PCI_ANY_ID,
844 .init = pci_siig20x_init,
845 .setup = pci_default_setup,
846 },
847 {
848 .vendor = PCI_VENDOR_ID_SIIG,
849 .device = PCI_DEVICE_ID_SIIG_1S_20x_850,
850 .subvendor = PCI_ANY_ID,
851 .subdevice = PCI_ANY_ID,
852 .init = pci_siig20x_init,
853 .setup = pci_default_setup,
854 },
855 {
856 .vendor = PCI_VENDOR_ID_SIIG,
857 .device = PCI_DEVICE_ID_SIIG_2S_20x_550,
858 .subvendor = PCI_ANY_ID,
859 .subdevice = PCI_ANY_ID,
860 .init = pci_siig20x_init,
861 .setup = pci_default_setup,
862 },
863 { .vendor = PCI_VENDOR_ID_SIIG,
864 .device = PCI_DEVICE_ID_SIIG_2S_20x_650,
865 .subvendor = PCI_ANY_ID,
866 .subdevice = PCI_ANY_ID,
867 .init = pci_siig20x_init,
868 .setup = pci_default_setup,
869 },
870 {
871 .vendor = PCI_VENDOR_ID_SIIG,
872 .device = PCI_DEVICE_ID_SIIG_2S_20x_850,
873 .subvendor = PCI_ANY_ID,
874 .subdevice = PCI_ANY_ID,
875 .init = pci_siig20x_init,
876 .setup = pci_default_setup,
877 },
878 {
879 .vendor = PCI_VENDOR_ID_SIIG,
880 .device = PCI_DEVICE_ID_SIIG_4S_20x_550,
881 .subvendor = PCI_ANY_ID,
882 .subdevice = PCI_ANY_ID,
883 .init = pci_siig20x_init,
884 .setup = pci_default_setup,
885 },
886 {
887 .vendor = PCI_VENDOR_ID_SIIG,
888 .device = PCI_DEVICE_ID_SIIG_4S_20x_650,
889 .subvendor = PCI_ANY_ID,
890 .subdevice = PCI_ANY_ID,
891 .init = pci_siig20x_init,
892 .setup = pci_default_setup,
893 },
894 {
895 .vendor = PCI_VENDOR_ID_SIIG,
896 .device = PCI_DEVICE_ID_SIIG_4S_20x_850,
897 .subvendor = PCI_ANY_ID,
898 .subdevice = PCI_ANY_ID,
899 .init = pci_siig20x_init,
900 .setup = pci_default_setup,
901 },
902 /*
903 * Titan cards
904 */
905 {
906 .vendor = PCI_VENDOR_ID_TITAN,
907 .device = PCI_DEVICE_ID_TITAN_400L,
908 .subvendor = PCI_ANY_ID,
909 .subdevice = PCI_ANY_ID,
910 .setup = titan_400l_800l_setup,
911 },
912 {
913 .vendor = PCI_VENDOR_ID_TITAN,
914 .device = PCI_DEVICE_ID_TITAN_800L,
915 .subvendor = PCI_ANY_ID,
916 .subdevice = PCI_ANY_ID,
917 .setup = titan_400l_800l_setup,
918 },
919 /*
920 * Timedia cards
921 */
922 {
923 .vendor = PCI_VENDOR_ID_TIMEDIA,
924 .device = PCI_DEVICE_ID_TIMEDIA_1889,
925 .subvendor = PCI_VENDOR_ID_TIMEDIA,
926 .subdevice = PCI_ANY_ID,
927 .init = pci_timedia_init,
928 .setup = pci_timedia_setup,
929 },
930 {
931 .vendor = PCI_VENDOR_ID_TIMEDIA,
932 .device = PCI_ANY_ID,
933 .subvendor = PCI_ANY_ID,
934 .subdevice = PCI_ANY_ID,
935 .setup = pci_timedia_setup,
936 },
937 /*
938 * Xircom cards
939 */
940 {
941 .vendor = PCI_VENDOR_ID_XIRCOM,
942 .device = PCI_DEVICE_ID_XIRCOM_X3201_MDM,
943 .subvendor = PCI_ANY_ID,
944 .subdevice = PCI_ANY_ID,
945 .init = pci_xircom_init,
946 .setup = pci_default_setup,
947 },
948 /*
949 * Netmos cards
950 */
951 {
952 .vendor = PCI_VENDOR_ID_NETMOS,
953 .device = PCI_ANY_ID,
954 .subvendor = PCI_ANY_ID,
955 .subdevice = PCI_ANY_ID,
956 .init = pci_netmos_init,
957 .setup = pci_default_setup,
958 },
959 /*
960 * Default "match everything" terminator entry
961 */
962 {
963 .vendor = PCI_ANY_ID,
964 .device = PCI_ANY_ID,
965 .subvendor = PCI_ANY_ID,
966 .subdevice = PCI_ANY_ID,
967 .setup = pci_default_setup,
968 }
969};
970
971static inline int quirk_id_matches(u32 quirk_id, u32 dev_id)
972{
973 return quirk_id == PCI_ANY_ID || quirk_id == dev_id;
974}
975
976static struct pci_serial_quirk *find_quirk(struct pci_dev *dev)
977{
978 struct pci_serial_quirk *quirk;
979
980 for (quirk = pci_serial_quirks; ; quirk++)
981 if (quirk_id_matches(quirk->vendor, dev->vendor) &&
982 quirk_id_matches(quirk->device, dev->device) &&
983 quirk_id_matches(quirk->subvendor, dev->subsystem_vendor) &&
984 quirk_id_matches(quirk->subdevice, dev->subsystem_device))
985 break;
986 return quirk;
987}
988
989static _INLINE_ int
990get_pci_irq(struct pci_dev *dev, struct pci_board *board, int idx)
991{
992 if (board->flags & FL_NOIRQ)
993 return 0;
994 else
995 return dev->irq;
996}
997
998/*
999 * This is the configuration table for all of the PCI serial boards
1000 * which we support. It is directly indexed by the pci_board_num_t enum
1001 * value, which is encoded in the pci_device_id PCI probe table's
1002 * driver_data member.
1003 *
1004 * The makeup of these names are:
1005 * pbn_bn{_bt}_n_baud
1006 *
1007 * bn = PCI BAR number
1008 * bt = Index using PCI BARs
1009 * n = number of serial ports
1010 * baud = baud rate
1011 *
1012 * Please note: in theory if n = 1, _bt infix should make no difference.
1013 * ie, pbn_b0_1_115200 is the same as pbn_b0_bt_1_115200
1014 */
1015enum pci_board_num_t {
1016 pbn_default = 0,
1017
1018 pbn_b0_1_115200,
1019 pbn_b0_2_115200,
1020 pbn_b0_4_115200,
1021 pbn_b0_5_115200,
1022
1023 pbn_b0_1_921600,
1024 pbn_b0_2_921600,
1025 pbn_b0_4_921600,
1026
1027 pbn_b0_bt_1_115200,
1028 pbn_b0_bt_2_115200,
1029 pbn_b0_bt_8_115200,
1030
1031 pbn_b0_bt_1_460800,
1032 pbn_b0_bt_2_460800,
1033 pbn_b0_bt_4_460800,
1034
1035 pbn_b0_bt_1_921600,
1036 pbn_b0_bt_2_921600,
1037 pbn_b0_bt_4_921600,
1038 pbn_b0_bt_8_921600,
1039
1040 pbn_b1_1_115200,
1041 pbn_b1_2_115200,
1042 pbn_b1_4_115200,
1043 pbn_b1_8_115200,
1044
1045 pbn_b1_1_921600,
1046 pbn_b1_2_921600,
1047 pbn_b1_4_921600,
1048 pbn_b1_8_921600,
1049
1050 pbn_b1_bt_2_921600,
1051
1052 pbn_b1_1_1382400,
1053 pbn_b1_2_1382400,
1054 pbn_b1_4_1382400,
1055 pbn_b1_8_1382400,
1056
1057 pbn_b2_1_115200,
1058 pbn_b2_8_115200,
1059
1060 pbn_b2_1_460800,
1061 pbn_b2_4_460800,
1062 pbn_b2_8_460800,
1063 pbn_b2_16_460800,
1064
1065 pbn_b2_1_921600,
1066 pbn_b2_4_921600,
1067 pbn_b2_8_921600,
1068
1069 pbn_b2_bt_1_115200,
1070 pbn_b2_bt_2_115200,
1071 pbn_b2_bt_4_115200,
1072
1073 pbn_b2_bt_2_921600,
1074 pbn_b2_bt_4_921600,
1075
1076 pbn_b3_4_115200,
1077 pbn_b3_8_115200,
1078
1079 /*
1080 * Board-specific versions.
1081 */
1082 pbn_panacom,
1083 pbn_panacom2,
1084 pbn_panacom4,
1085 pbn_plx_romulus,
1086 pbn_oxsemi,
1087 pbn_intel_i960,
1088 pbn_sgi_ioc3,
1089 pbn_nec_nile4,
1090 pbn_computone_4,
1091 pbn_computone_6,
1092 pbn_computone_8,
1093 pbn_sbsxrsio,
1094 pbn_exar_XR17C152,
1095 pbn_exar_XR17C154,
1096 pbn_exar_XR17C158,
1097};
1098
1099/*
1100 * uart_offset - the space between channels
1101 * reg_shift - describes how the UART registers are mapped
1102 * to PCI memory by the card.
1103 * For example IER register on SBS, Inc. PMC-OctPro is located at
1104 * offset 0x10 from the UART base, while UART_IER is defined as 1
1105 * in include/linux/serial_reg.h,
1106 * see first lines of serial_in() and serial_out() in 8250.c
1107*/
1108
1109static struct pci_board pci_boards[] __devinitdata = {
1110 [pbn_default] = {
1111 .flags = FL_BASE0,
1112 .num_ports = 1,
1113 .base_baud = 115200,
1114 .uart_offset = 8,
1115 },
1116 [pbn_b0_1_115200] = {
1117 .flags = FL_BASE0,
1118 .num_ports = 1,
1119 .base_baud = 115200,
1120 .uart_offset = 8,
1121 },
1122 [pbn_b0_2_115200] = {
1123 .flags = FL_BASE0,
1124 .num_ports = 2,
1125 .base_baud = 115200,
1126 .uart_offset = 8,
1127 },
1128 [pbn_b0_4_115200] = {
1129 .flags = FL_BASE0,
1130 .num_ports = 4,
1131 .base_baud = 115200,
1132 .uart_offset = 8,
1133 },
1134 [pbn_b0_5_115200] = {
1135 .flags = FL_BASE0,
1136 .num_ports = 5,
1137 .base_baud = 115200,
1138 .uart_offset = 8,
1139 },
1140
1141 [pbn_b0_1_921600] = {
1142 .flags = FL_BASE0,
1143 .num_ports = 1,
1144 .base_baud = 921600,
1145 .uart_offset = 8,
1146 },
1147 [pbn_b0_2_921600] = {
1148 .flags = FL_BASE0,
1149 .num_ports = 2,
1150 .base_baud = 921600,
1151 .uart_offset = 8,
1152 },
1153 [pbn_b0_4_921600] = {
1154 .flags = FL_BASE0,
1155 .num_ports = 4,
1156 .base_baud = 921600,
1157 .uart_offset = 8,
1158 },
1159
1160 [pbn_b0_bt_1_115200] = {
1161 .flags = FL_BASE0|FL_BASE_BARS,
1162 .num_ports = 1,
1163 .base_baud = 115200,
1164 .uart_offset = 8,
1165 },
1166 [pbn_b0_bt_2_115200] = {
1167 .flags = FL_BASE0|FL_BASE_BARS,
1168 .num_ports = 2,
1169 .base_baud = 115200,
1170 .uart_offset = 8,
1171 },
1172 [pbn_b0_bt_8_115200] = {
1173 .flags = FL_BASE0|FL_BASE_BARS,
1174 .num_ports = 8,
1175 .base_baud = 115200,
1176 .uart_offset = 8,
1177 },
1178
1179 [pbn_b0_bt_1_460800] = {
1180 .flags = FL_BASE0|FL_BASE_BARS,
1181 .num_ports = 1,
1182 .base_baud = 460800,
1183 .uart_offset = 8,
1184 },
1185 [pbn_b0_bt_2_460800] = {
1186 .flags = FL_BASE0|FL_BASE_BARS,
1187 .num_ports = 2,
1188 .base_baud = 460800,
1189 .uart_offset = 8,
1190 },
1191 [pbn_b0_bt_4_460800] = {
1192 .flags = FL_BASE0|FL_BASE_BARS,
1193 .num_ports = 4,
1194 .base_baud = 460800,
1195 .uart_offset = 8,
1196 },
1197
1198 [pbn_b0_bt_1_921600] = {
1199 .flags = FL_BASE0|FL_BASE_BARS,
1200 .num_ports = 1,
1201 .base_baud = 921600,
1202 .uart_offset = 8,
1203 },
1204 [pbn_b0_bt_2_921600] = {
1205 .flags = FL_BASE0|FL_BASE_BARS,
1206 .num_ports = 2,
1207 .base_baud = 921600,
1208 .uart_offset = 8,
1209 },
1210 [pbn_b0_bt_4_921600] = {
1211 .flags = FL_BASE0|FL_BASE_BARS,
1212 .num_ports = 4,
1213 .base_baud = 921600,
1214 .uart_offset = 8,
1215 },
1216 [pbn_b0_bt_8_921600] = {
1217 .flags = FL_BASE0|FL_BASE_BARS,
1218 .num_ports = 8,
1219 .base_baud = 921600,
1220 .uart_offset = 8,
1221 },
1222
1223 [pbn_b1_1_115200] = {
1224 .flags = FL_BASE1,
1225 .num_ports = 1,
1226 .base_baud = 115200,
1227 .uart_offset = 8,
1228 },
1229 [pbn_b1_2_115200] = {
1230 .flags = FL_BASE1,
1231 .num_ports = 2,
1232 .base_baud = 115200,
1233 .uart_offset = 8,
1234 },
1235 [pbn_b1_4_115200] = {
1236 .flags = FL_BASE1,
1237 .num_ports = 4,
1238 .base_baud = 115200,
1239 .uart_offset = 8,
1240 },
1241 [pbn_b1_8_115200] = {
1242 .flags = FL_BASE1,
1243 .num_ports = 8,
1244 .base_baud = 115200,
1245 .uart_offset = 8,
1246 },
1247
1248 [pbn_b1_1_921600] = {
1249 .flags = FL_BASE1,
1250 .num_ports = 1,
1251 .base_baud = 921600,
1252 .uart_offset = 8,
1253 },
1254 [pbn_b1_2_921600] = {
1255 .flags = FL_BASE1,
1256 .num_ports = 2,
1257 .base_baud = 921600,
1258 .uart_offset = 8,
1259 },
1260 [pbn_b1_4_921600] = {
1261 .flags = FL_BASE1,
1262 .num_ports = 4,
1263 .base_baud = 921600,
1264 .uart_offset = 8,
1265 },
1266 [pbn_b1_8_921600] = {
1267 .flags = FL_BASE1,
1268 .num_ports = 8,
1269 .base_baud = 921600,
1270 .uart_offset = 8,
1271 },
1272
1273 [pbn_b1_bt_2_921600] = {
1274 .flags = FL_BASE1|FL_BASE_BARS,
1275 .num_ports = 2,
1276 .base_baud = 921600,
1277 .uart_offset = 8,
1278 },
1279
1280 [pbn_b1_1_1382400] = {
1281 .flags = FL_BASE1,
1282 .num_ports = 1,
1283 .base_baud = 1382400,
1284 .uart_offset = 8,
1285 },
1286 [pbn_b1_2_1382400] = {
1287 .flags = FL_BASE1,
1288 .num_ports = 2,
1289 .base_baud = 1382400,
1290 .uart_offset = 8,
1291 },
1292 [pbn_b1_4_1382400] = {
1293 .flags = FL_BASE1,
1294 .num_ports = 4,
1295 .base_baud = 1382400,
1296 .uart_offset = 8,
1297 },
1298 [pbn_b1_8_1382400] = {
1299 .flags = FL_BASE1,
1300 .num_ports = 8,
1301 .base_baud = 1382400,
1302 .uart_offset = 8,
1303 },
1304
1305 [pbn_b2_1_115200] = {
1306 .flags = FL_BASE2,
1307 .num_ports = 1,
1308 .base_baud = 115200,
1309 .uart_offset = 8,
1310 },
1311 [pbn_b2_8_115200] = {
1312 .flags = FL_BASE2,
1313 .num_ports = 8,
1314 .base_baud = 115200,
1315 .uart_offset = 8,
1316 },
1317
1318 [pbn_b2_1_460800] = {
1319 .flags = FL_BASE2,
1320 .num_ports = 1,
1321 .base_baud = 460800,
1322 .uart_offset = 8,
1323 },
1324 [pbn_b2_4_460800] = {
1325 .flags = FL_BASE2,
1326 .num_ports = 4,
1327 .base_baud = 460800,
1328 .uart_offset = 8,
1329 },
1330 [pbn_b2_8_460800] = {
1331 .flags = FL_BASE2,
1332 .num_ports = 8,
1333 .base_baud = 460800,
1334 .uart_offset = 8,
1335 },
1336 [pbn_b2_16_460800] = {
1337 .flags = FL_BASE2,
1338 .num_ports = 16,
1339 .base_baud = 460800,
1340 .uart_offset = 8,
1341 },
1342
1343 [pbn_b2_1_921600] = {
1344 .flags = FL_BASE2,
1345 .num_ports = 1,
1346 .base_baud = 921600,
1347 .uart_offset = 8,
1348 },
1349 [pbn_b2_4_921600] = {
1350 .flags = FL_BASE2,
1351 .num_ports = 4,
1352 .base_baud = 921600,
1353 .uart_offset = 8,
1354 },
1355 [pbn_b2_8_921600] = {
1356 .flags = FL_BASE2,
1357 .num_ports = 8,
1358 .base_baud = 921600,
1359 .uart_offset = 8,
1360 },
1361
1362 [pbn_b2_bt_1_115200] = {
1363 .flags = FL_BASE2|FL_BASE_BARS,
1364 .num_ports = 1,
1365 .base_baud = 115200,
1366 .uart_offset = 8,
1367 },
1368 [pbn_b2_bt_2_115200] = {
1369 .flags = FL_BASE2|FL_BASE_BARS,
1370 .num_ports = 2,
1371 .base_baud = 115200,
1372 .uart_offset = 8,
1373 },
1374 [pbn_b2_bt_4_115200] = {
1375 .flags = FL_BASE2|FL_BASE_BARS,
1376 .num_ports = 4,
1377 .base_baud = 115200,
1378 .uart_offset = 8,
1379 },
1380
1381 [pbn_b2_bt_2_921600] = {
1382 .flags = FL_BASE2|FL_BASE_BARS,
1383 .num_ports = 2,
1384 .base_baud = 921600,
1385 .uart_offset = 8,
1386 },
1387 [pbn_b2_bt_4_921600] = {
1388 .flags = FL_BASE2|FL_BASE_BARS,
1389 .num_ports = 4,
1390 .base_baud = 921600,
1391 .uart_offset = 8,
1392 },
1393
1394 [pbn_b3_4_115200] = {
1395 .flags = FL_BASE3,
1396 .num_ports = 4,
1397 .base_baud = 115200,
1398 .uart_offset = 8,
1399 },
1400 [pbn_b3_8_115200] = {
1401 .flags = FL_BASE3,
1402 .num_ports = 8,
1403 .base_baud = 115200,
1404 .uart_offset = 8,
1405 },
1406
1407 /*
1408 * Entries following this are board-specific.
1409 */
1410
1411 /*
1412 * Panacom - IOMEM
1413 */
1414 [pbn_panacom] = {
1415 .flags = FL_BASE2,
1416 .num_ports = 2,
1417 .base_baud = 921600,
1418 .uart_offset = 0x400,
1419 .reg_shift = 7,
1420 },
1421 [pbn_panacom2] = {
1422 .flags = FL_BASE2|FL_BASE_BARS,
1423 .num_ports = 2,
1424 .base_baud = 921600,
1425 .uart_offset = 0x400,
1426 .reg_shift = 7,
1427 },
1428 [pbn_panacom4] = {
1429 .flags = FL_BASE2|FL_BASE_BARS,
1430 .num_ports = 4,
1431 .base_baud = 921600,
1432 .uart_offset = 0x400,
1433 .reg_shift = 7,
1434 },
1435
1436 /* I think this entry is broken - the first_offset looks wrong --rmk */
1437 [pbn_plx_romulus] = {
1438 .flags = FL_BASE2,
1439 .num_ports = 4,
1440 .base_baud = 921600,
1441 .uart_offset = 8 << 2,
1442 .reg_shift = 2,
1443 .first_offset = 0x03,
1444 },
1445
1446 /*
1447 * This board uses the size of PCI Base region 0 to
1448 * signal now many ports are available
1449 */
1450 [pbn_oxsemi] = {
1451 .flags = FL_BASE0|FL_REGION_SZ_CAP,
1452 .num_ports = 32,
1453 .base_baud = 115200,
1454 .uart_offset = 8,
1455 },
1456
1457 /*
1458 * EKF addition for i960 Boards form EKF with serial port.
1459 * Max 256 ports.
1460 */
1461 [pbn_intel_i960] = {
1462 .flags = FL_BASE0,
1463 .num_ports = 32,
1464 .base_baud = 921600,
1465 .uart_offset = 8 << 2,
1466 .reg_shift = 2,
1467 .first_offset = 0x10000,
1468 },
1469 [pbn_sgi_ioc3] = {
1470 .flags = FL_BASE0|FL_NOIRQ,
1471 .num_ports = 1,
1472 .base_baud = 458333,
1473 .uart_offset = 8,
1474 .reg_shift = 0,
1475 .first_offset = 0x20178,
1476 },
1477
1478 /*
1479 * NEC Vrc-5074 (Nile 4) builtin UART.
1480 */
1481 [pbn_nec_nile4] = {
1482 .flags = FL_BASE0,
1483 .num_ports = 1,
1484 .base_baud = 520833,
1485 .uart_offset = 8 << 3,
1486 .reg_shift = 3,
1487 .first_offset = 0x300,
1488 },
1489
1490 /*
1491 * Computone - uses IOMEM.
1492 */
1493 [pbn_computone_4] = {
1494 .flags = FL_BASE0,
1495 .num_ports = 4,
1496 .base_baud = 921600,
1497 .uart_offset = 0x40,
1498 .reg_shift = 2,
1499 .first_offset = 0x200,
1500 },
1501 [pbn_computone_6] = {
1502 .flags = FL_BASE0,
1503 .num_ports = 6,
1504 .base_baud = 921600,
1505 .uart_offset = 0x40,
1506 .reg_shift = 2,
1507 .first_offset = 0x200,
1508 },
1509 [pbn_computone_8] = {
1510 .flags = FL_BASE0,
1511 .num_ports = 8,
1512 .base_baud = 921600,
1513 .uart_offset = 0x40,
1514 .reg_shift = 2,
1515 .first_offset = 0x200,
1516 },
1517 [pbn_sbsxrsio] = {
1518 .flags = FL_BASE0,
1519 .num_ports = 8,
1520 .base_baud = 460800,
1521 .uart_offset = 256,
1522 .reg_shift = 4,
1523 },
1524 /*
1525 * Exar Corp. XR17C15[248] Dual/Quad/Octal UART
1526 * Only basic 16550A support.
1527 * XR17C15[24] are not tested, but they should work.
1528 */
1529 [pbn_exar_XR17C152] = {
1530 .flags = FL_BASE0,
1531 .num_ports = 2,
1532 .base_baud = 921600,
1533 .uart_offset = 0x200,
1534 },
1535 [pbn_exar_XR17C154] = {
1536 .flags = FL_BASE0,
1537 .num_ports = 4,
1538 .base_baud = 921600,
1539 .uart_offset = 0x200,
1540 },
1541 [pbn_exar_XR17C158] = {
1542 .flags = FL_BASE0,
1543 .num_ports = 8,
1544 .base_baud = 921600,
1545 .uart_offset = 0x200,
1546 },
1547};
1548
1549/*
1550 * Given a complete unknown PCI device, try to use some heuristics to
1551 * guess what the configuration might be, based on the pitiful PCI
1552 * serial specs. Returns 0 on success, 1 on failure.
1553 */
1554static int __devinit
1555serial_pci_guess_board(struct pci_dev *dev, struct pci_board *board)
1556{
1557 int num_iomem, num_port, first_port = -1, i;
1558
1559 /*
1560 * If it is not a communications device or the programming
1561 * interface is greater than 6, give up.
1562 *
1563 * (Should we try to make guesses for multiport serial devices
1564 * later?)
1565 */
1566 if ((((dev->class >> 8) != PCI_CLASS_COMMUNICATION_SERIAL) &&
1567 ((dev->class >> 8) != PCI_CLASS_COMMUNICATION_MODEM)) ||
1568 (dev->class & 0xff) > 6)
1569 return -ENODEV;
1570
1571 num_iomem = num_port = 0;
1572 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
1573 if (pci_resource_flags(dev, i) & IORESOURCE_IO) {
1574 num_port++;
1575 if (first_port == -1)
1576 first_port = i;
1577 }
1578 if (pci_resource_flags(dev, i) & IORESOURCE_MEM)
1579 num_iomem++;
1580 }
1581
1582 /*
1583 * If there is 1 or 0 iomem regions, and exactly one port,
1584 * use it. We guess the number of ports based on the IO
1585 * region size.
1586 */
1587 if (num_iomem <= 1 && num_port == 1) {
1588 board->flags = first_port;
1589 board->num_ports = pci_resource_len(dev, first_port) / 8;
1590 return 0;
1591 }
1592
1593 /*
1594 * Now guess if we've got a board which indexes by BARs.
1595 * Each IO BAR should be 8 bytes, and they should follow
1596 * consecutively.
1597 */
1598 first_port = -1;
1599 num_port = 0;
1600 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
1601 if (pci_resource_flags(dev, i) & IORESOURCE_IO &&
1602 pci_resource_len(dev, i) == 8 &&
1603 (first_port == -1 || (first_port + num_port) == i)) {
1604 num_port++;
1605 if (first_port == -1)
1606 first_port = i;
1607 }
1608 }
1609
1610 if (num_port > 1) {
1611 board->flags = first_port | FL_BASE_BARS;
1612 board->num_ports = num_port;
1613 return 0;
1614 }
1615
1616 return -ENODEV;
1617}
1618
1619static inline int
1620serial_pci_matches(struct pci_board *board, struct pci_board *guessed)
1621{
1622 return
1623 board->num_ports == guessed->num_ports &&
1624 board->base_baud == guessed->base_baud &&
1625 board->uart_offset == guessed->uart_offset &&
1626 board->reg_shift == guessed->reg_shift &&
1627 board->first_offset == guessed->first_offset;
1628}
1629
1630/*
1631 * Probe one serial board. Unfortunately, there is no rhyme nor reason
1632 * to the arrangement of serial ports on a PCI card.
1633 */
1634static int __devinit
1635pciserial_init_one(struct pci_dev *dev, const struct pci_device_id *ent)
1636{
1637 struct serial_private *priv;
1638 struct pci_board *board, tmp;
1639 struct pci_serial_quirk *quirk;
1640 int rc, nr_ports, i;
1641
1642 if (ent->driver_data >= ARRAY_SIZE(pci_boards)) {
1643 printk(KERN_ERR "pci_init_one: invalid driver_data: %ld\n",
1644 ent->driver_data);
1645 return -EINVAL;
1646 }
1647
1648 board = &pci_boards[ent->driver_data];
1649
1650 rc = pci_enable_device(dev);
1651 if (rc)
1652 return rc;
1653
1654 if (ent->driver_data == pbn_default) {
1655 /*
1656 * Use a copy of the pci_board entry for this;
1657 * avoid changing entries in the table.
1658 */
1659 memcpy(&tmp, board, sizeof(struct pci_board));
1660 board = &tmp;
1661
1662 /*
1663 * We matched one of our class entries. Try to
1664 * determine the parameters of this board.
1665 */
1666 rc = serial_pci_guess_board(dev, board);
1667 if (rc)
1668 goto disable;
1669 } else {
1670 /*
1671 * We matched an explicit entry. If we are able to
1672 * detect this boards settings with our heuristic,
1673 * then we no longer need this entry.
1674 */
1675 memcpy(&tmp, &pci_boards[pbn_default], sizeof(struct pci_board));
1676 rc = serial_pci_guess_board(dev, &tmp);
1677 if (rc == 0 && serial_pci_matches(board, &tmp))
1678 moan_device("Redundant entry in serial pci_table.",
1679 dev);
1680 }
1681
1682 nr_ports = board->num_ports;
1683
1684 /*
1685 * Find an init and setup quirks.
1686 */
1687 quirk = find_quirk(dev);
1688
1689 /*
1690 * Run the new-style initialization function.
1691 * The initialization function returns:
1692 * <0 - error
1693 * 0 - use board->num_ports
1694 * >0 - number of ports
1695 */
1696 if (quirk->init) {
1697 rc = quirk->init(dev);
1698 if (rc < 0)
1699 goto disable;
1700 if (rc)
1701 nr_ports = rc;
1702 }
1703
1704 priv = kmalloc(sizeof(struct serial_private) +
1705 sizeof(unsigned int) * nr_ports,
1706 GFP_KERNEL);
1707 if (!priv) {
1708 rc = -ENOMEM;
1709 goto deinit;
1710 }
1711
1712 memset(priv, 0, sizeof(struct serial_private) +
1713 sizeof(unsigned int) * nr_ports);
1714
1715 priv->quirk = quirk;
1716 pci_set_drvdata(dev, priv);
1717
1718 for (i = 0; i < nr_ports; i++) {
1719 struct uart_port serial_port;
1720 memset(&serial_port, 0, sizeof(struct uart_port));
1721
1722 serial_port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF |
1723 UPF_SHARE_IRQ;
1724 serial_port.uartclk = board->base_baud * 16;
1725 serial_port.irq = get_pci_irq(dev, board, i);
1726 serial_port.dev = &dev->dev;
1727 if (quirk->setup(dev, board, &serial_port, i))
1728 break;
1729#ifdef SERIAL_DEBUG_PCI
1730 printk("Setup PCI port: port %x, irq %d, type %d\n",
1731 serial_port.iobase, serial_port.irq, serial_port.iotype);
1732#endif
1733
1734 priv->line[i] = serial8250_register_port(&serial_port);
1735 if (priv->line[i] < 0) {
1736 printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), priv->line[i]);
1737 break;
1738 }
1739 }
1740
1741 priv->nr = i;
1742
1743 return 0;
1744
1745 deinit:
1746 if (quirk->exit)
1747 quirk->exit(dev);
1748 disable:
1749 pci_disable_device(dev);
1750 return rc;
1751}
1752
1753static void __devexit pciserial_remove_one(struct pci_dev *dev)
1754{
1755 struct serial_private *priv = pci_get_drvdata(dev);
1756
1757 pci_set_drvdata(dev, NULL);
1758
1759 if (priv) {
1760 struct pci_serial_quirk *quirk;
1761 int i;
1762
1763 for (i = 0; i < priv->nr; i++)
1764 serial8250_unregister_port(priv->line[i]);
1765
1766 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
1767 if (priv->remapped_bar[i])
1768 iounmap(priv->remapped_bar[i]);
1769 priv->remapped_bar[i] = NULL;
1770 }
1771
1772 /*
1773 * Find the exit quirks.
1774 */
1775 quirk = find_quirk(dev);
1776 if (quirk->exit)
1777 quirk->exit(dev);
1778
1779 pci_disable_device(dev);
1780
1781 kfree(priv);
1782 }
1783}
1784
1785static int pciserial_suspend_one(struct pci_dev *dev, pm_message_t state)
1786{
1787 struct serial_private *priv = pci_get_drvdata(dev);
1788
1789 if (priv) {
1790 int i;
1791
1792 for (i = 0; i < priv->nr; i++)
1793 serial8250_suspend_port(priv->line[i]);
1794 }
1795 pci_save_state(dev);
1796 pci_set_power_state(dev, pci_choose_state(dev, state));
1797 return 0;
1798}
1799
1800static int pciserial_resume_one(struct pci_dev *dev)
1801{
1802 struct serial_private *priv = pci_get_drvdata(dev);
1803
1804 pci_set_power_state(dev, PCI_D0);
1805 pci_restore_state(dev);
1806
1807 if (priv) {
1808 int i;
1809
1810 /*
1811 * The device may have been disabled. Re-enable it.
1812 */
1813 pci_enable_device(dev);
1814
1815 /*
1816 * Ensure that the board is correctly configured.
1817 */
1818 if (priv->quirk->init)
1819 priv->quirk->init(dev);
1820
1821 for (i = 0; i < priv->nr; i++)
1822 serial8250_resume_port(priv->line[i]);
1823 }
1824 return 0;
1825}
1826
1827static struct pci_device_id serial_pci_tbl[] = {
1828 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
1829 PCI_SUBVENDOR_ID_CONNECT_TECH,
1830 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232, 0, 0,
1831 pbn_b1_8_1382400 },
1832 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
1833 PCI_SUBVENDOR_ID_CONNECT_TECH,
1834 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232, 0, 0,
1835 pbn_b1_4_1382400 },
1836 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
1837 PCI_SUBVENDOR_ID_CONNECT_TECH,
1838 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232, 0, 0,
1839 pbn_b1_2_1382400 },
1840 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1841 PCI_SUBVENDOR_ID_CONNECT_TECH,
1842 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232, 0, 0,
1843 pbn_b1_8_1382400 },
1844 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1845 PCI_SUBVENDOR_ID_CONNECT_TECH,
1846 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232, 0, 0,
1847 pbn_b1_4_1382400 },
1848 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1849 PCI_SUBVENDOR_ID_CONNECT_TECH,
1850 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232, 0, 0,
1851 pbn_b1_2_1382400 },
1852 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1853 PCI_SUBVENDOR_ID_CONNECT_TECH,
1854 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485, 0, 0,
1855 pbn_b1_8_921600 },
1856 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1857 PCI_SUBVENDOR_ID_CONNECT_TECH,
1858 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_4_4, 0, 0,
1859 pbn_b1_8_921600 },
1860 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1861 PCI_SUBVENDOR_ID_CONNECT_TECH,
1862 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485, 0, 0,
1863 pbn_b1_4_921600 },
1864 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1865 PCI_SUBVENDOR_ID_CONNECT_TECH,
1866 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485_2_2, 0, 0,
1867 pbn_b1_4_921600 },
1868 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1869 PCI_SUBVENDOR_ID_CONNECT_TECH,
1870 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_485, 0, 0,
1871 pbn_b1_2_921600 },
1872 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1873 PCI_SUBVENDOR_ID_CONNECT_TECH,
1874 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_2_6, 0, 0,
1875 pbn_b1_8_921600 },
1876 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1877 PCI_SUBVENDOR_ID_CONNECT_TECH,
1878 PCI_SUBDEVICE_ID_CONNECT_TECH_BH081101V1, 0, 0,
1879 pbn_b1_8_921600 },
1880 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
1881 PCI_SUBVENDOR_ID_CONNECT_TECH,
1882 PCI_SUBDEVICE_ID_CONNECT_TECH_BH041101V1, 0, 0,
1883 pbn_b1_4_921600 },
1884
1885 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_U530,
1886 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1887 pbn_b2_bt_1_115200 },
1888 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM2,
1889 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1890 pbn_b2_bt_2_115200 },
1891 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM422,
1892 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1893 pbn_b2_bt_4_115200 },
1894 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM232,
1895 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1896 pbn_b2_bt_2_115200 },
1897 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM4,
1898 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1899 pbn_b2_bt_4_115200 },
1900 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM8,
1901 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1902 pbn_b2_8_115200 },
1903 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM8,
1904 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1905 pbn_b2_8_115200 },
1906
1907 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_GTEK_SERIAL2,
1908 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1909 pbn_b2_bt_2_115200 },
1910 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM200,
1911 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1912 pbn_b2_bt_2_921600 },
1913 /*
1914 * VScom SPCOM800, from sl@s.pl
1915 */
1916 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM800,
1917 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1918 pbn_b2_8_921600 },
1919 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_1077,
1920 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1921 pbn_b2_4_921600 },
1922 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
1923 PCI_SUBVENDOR_ID_KEYSPAN,
1924 PCI_SUBDEVICE_ID_KEYSPAN_SX2, 0, 0,
1925 pbn_panacom },
1926 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_QUADMODEM,
1927 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1928 pbn_panacom4 },
1929 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_DUALMODEM,
1930 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1931 pbn_panacom2 },
1932 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
1933 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
1934 PCI_SUBDEVICE_ID_CHASE_PCIFAST4, 0, 0,
1935 pbn_b2_4_460800 },
1936 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
1937 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
1938 PCI_SUBDEVICE_ID_CHASE_PCIFAST8, 0, 0,
1939 pbn_b2_8_460800 },
1940 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
1941 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
1942 PCI_SUBDEVICE_ID_CHASE_PCIFAST16, 0, 0,
1943 pbn_b2_16_460800 },
1944 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
1945 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
1946 PCI_SUBDEVICE_ID_CHASE_PCIFAST16FMC, 0, 0,
1947 pbn_b2_16_460800 },
1948 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
1949 PCI_SUBVENDOR_ID_CHASE_PCIRAS,
1950 PCI_SUBDEVICE_ID_CHASE_PCIRAS4, 0, 0,
1951 pbn_b2_4_460800 },
1952 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
1953 PCI_SUBVENDOR_ID_CHASE_PCIRAS,
1954 PCI_SUBDEVICE_ID_CHASE_PCIRAS8, 0, 0,
1955 pbn_b2_8_460800 },
1956 /*
1957 * Megawolf Romulus PCI Serial Card, from Mike Hudson
1958 * (Exoray@isys.ca)
1959 */
1960 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_ROMULUS,
1961 0x10b5, 0x106a, 0, 0,
1962 pbn_plx_romulus },
1963 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_QSC100,
1964 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1965 pbn_b1_4_115200 },
1966 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_DSC100,
1967 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1968 pbn_b1_2_115200 },
1969 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_ESC100D,
1970 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1971 pbn_b1_8_115200 },
1972 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_ESC100M,
1973 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1974 pbn_b1_8_115200 },
1975 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_OXSEMI_16PCI954,
1976 PCI_VENDOR_ID_SPECIALIX, PCI_SUBDEVICE_ID_SPECIALIX_SPEED4, 0, 0,
1977 pbn_b0_4_921600 },
1978 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
1979 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1980 pbn_b0_4_115200 },
1981 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI952,
1982 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1983 pbn_b0_bt_2_921600 },
1984
1985 /*
1986 * SBS Technologies, Inc. P-Octal and PMC-OCTPRO cards,
1987 * from skokodyn@yahoo.com
1988 */
1989 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
1990 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_OCTPRO232, 0, 0,
1991 pbn_sbsxrsio },
1992 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
1993 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_OCTPRO422, 0, 0,
1994 pbn_sbsxrsio },
1995 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
1996 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_POCTAL232, 0, 0,
1997 pbn_sbsxrsio },
1998 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
1999 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_POCTAL422, 0, 0,
2000 pbn_sbsxrsio },
2001
2002 /*
2003 * Digitan DS560-558, from jimd@esoft.com
2004 */
2005 { PCI_VENDOR_ID_ATT, PCI_DEVICE_ID_ATT_VENUS_MODEM,
2006 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2007 pbn_b1_1_115200 },
2008
2009 /*
2010 * Titan Electronic cards
2011 * The 400L and 800L have a custom setup quirk.
2012 */
2013 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100,
2014 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2015 pbn_b0_1_921600 },
2016 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200,
2017 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2018 pbn_b0_2_921600 },
2019 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400,
2020 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2021 pbn_b0_4_921600 },
2022 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800B,
2023 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2024 pbn_b0_4_921600 },
2025 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100L,
2026 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2027 pbn_b1_1_921600 },
2028 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200L,
2029 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2030 pbn_b1_bt_2_921600 },
2031 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400L,
2032 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2033 pbn_b0_bt_4_921600 },
2034 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800L,
2035 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2036 pbn_b0_bt_8_921600 },
2037
2038 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_550,
2039 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2040 pbn_b2_1_460800 },
2041 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_650,
2042 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2043 pbn_b2_1_460800 },
2044 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_850,
2045 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2046 pbn_b2_1_460800 },
2047 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_550,
2048 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2049 pbn_b2_bt_2_921600 },
2050 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_650,
2051 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2052 pbn_b2_bt_2_921600 },
2053 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_850,
2054 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2055 pbn_b2_bt_2_921600 },
2056 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_550,
2057 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2058 pbn_b2_bt_4_921600 },
2059 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_650,
2060 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2061 pbn_b2_bt_4_921600 },
2062 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_850,
2063 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2064 pbn_b2_bt_4_921600 },
2065 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_550,
2066 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2067 pbn_b0_1_921600 },
2068 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_650,
2069 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2070 pbn_b0_1_921600 },
2071 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_850,
2072 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2073 pbn_b0_1_921600 },
2074 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_550,
2075 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2076 pbn_b0_bt_2_921600 },
2077 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_650,
2078 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2079 pbn_b0_bt_2_921600 },
2080 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_850,
2081 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2082 pbn_b0_bt_2_921600 },
2083 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_550,
2084 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2085 pbn_b0_bt_4_921600 },
2086 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_650,
2087 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2088 pbn_b0_bt_4_921600 },
2089 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_850,
2090 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2091 pbn_b0_bt_4_921600 },
2092
2093 /*
2094 * Computone devices submitted by Doug McNash dmcnash@computone.com
2095 */
2096 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
2097 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG4,
2098 0, 0, pbn_computone_4 },
2099 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
2100 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG8,
2101 0, 0, pbn_computone_8 },
2102 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
2103 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG6,
2104 0, 0, pbn_computone_6 },
2105
2106 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI95N,
2107 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2108 pbn_oxsemi },
2109 { PCI_VENDOR_ID_TIMEDIA, PCI_DEVICE_ID_TIMEDIA_1889,
2110 PCI_VENDOR_ID_TIMEDIA, PCI_ANY_ID, 0, 0,
2111 pbn_b0_bt_1_921600 },
2112
2113 /*
2114 * AFAVLAB serial card, from Harald Welte <laforge@gnumonks.org>
2115 */
2116 { PCI_VENDOR_ID_AFAVLAB, PCI_DEVICE_ID_AFAVLAB_P028,
2117 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2118 pbn_b0_bt_8_115200 },
2119 { PCI_VENDOR_ID_AFAVLAB, PCI_DEVICE_ID_AFAVLAB_P030,
2120 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2121 pbn_b0_bt_8_115200 },
2122
2123 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DSERIAL,
2124 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2125 pbn_b0_bt_2_115200 },
2126 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATRO_A,
2127 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2128 pbn_b0_bt_2_115200 },
2129 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATRO_B,
2130 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2131 pbn_b0_bt_2_115200 },
2132 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_OCTO_A,
2133 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2134 pbn_b0_bt_4_460800 },
2135 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_OCTO_B,
2136 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2137 pbn_b0_bt_4_460800 },
2138 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PORT_PLUS,
2139 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2140 pbn_b0_bt_2_460800 },
2141 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUAD_A,
2142 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2143 pbn_b0_bt_2_460800 },
2144 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUAD_B,
2145 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2146 pbn_b0_bt_2_460800 },
2147 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_SSERIAL,
2148 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2149 pbn_b0_bt_1_115200 },
2150 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PORT_650,
2151 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2152 pbn_b0_bt_1_460800 },
2153
2154 /*
2155 * Dell Remote Access Card 4 - Tim_T_Murphy@Dell.com
2156 */
2157 { PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_RAC4,
2158 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2159 pbn_b1_1_1382400 },
2160
2161 /*
2162 * Dell Remote Access Card III - Tim_T_Murphy@Dell.com
2163 */
2164 { PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_RACIII,
2165 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2166 pbn_b1_1_1382400 },
2167
2168 /*
2169 * RAStel 2 port modem, gerg@moreton.com.au
2170 */
2171 { PCI_VENDOR_ID_MORETON, PCI_DEVICE_ID_RASTEL_2PORT,
2172 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2173 pbn_b2_bt_2_115200 },
2174
2175 /*
2176 * EKF addition for i960 Boards form EKF with serial port
2177 */
2178 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80960_RP,
2179 0xE4BF, PCI_ANY_ID, 0, 0,
2180 pbn_intel_i960 },
2181
2182 /*
2183 * Xircom Cardbus/Ethernet combos
2184 */
2185 { PCI_VENDOR_ID_XIRCOM, PCI_DEVICE_ID_XIRCOM_X3201_MDM,
2186 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2187 pbn_b0_1_115200 },
2188 /*
2189 * Xircom RBM56G cardbus modem - Dirk Arnold (temp entry)
2190 */
2191 { PCI_VENDOR_ID_XIRCOM, PCI_DEVICE_ID_XIRCOM_RBM56G,
2192 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2193 pbn_b0_1_115200 },
2194
2195 /*
2196 * Untested PCI modems, sent in from various folks...
2197 */
2198
2199 /*
2200 * Elsa Model 56K PCI Modem, from Andreas Rath <arh@01019freenet.de>
2201 */
2202 { PCI_VENDOR_ID_ROCKWELL, 0x1004,
2203 0x1048, 0x1500, 0, 0,
2204 pbn_b1_1_115200 },
2205
2206 { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,
2207 0xFF00, 0, 0, 0,
2208 pbn_sgi_ioc3 },
2209
2210 /*
2211 * HP Diva card
2212 */
2213 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA,
2214 PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA_RMP3, 0, 0,
2215 pbn_b1_1_115200 },
2216 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA,
2217 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2218 pbn_b0_5_115200 },
2219 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA_AUX,
2220 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2221 pbn_b2_1_115200 },
2222
2223 /*
2224 * NEC Vrc-5074 (Nile 4) builtin UART.
2225 */
2226 { PCI_VENDOR_ID_NEC, PCI_DEVICE_ID_NEC_NILE4,
2227 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2228 pbn_nec_nile4 },
2229
2230 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM4,
2231 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2232 pbn_b3_4_115200 },
2233 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM8,
2234 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2235 pbn_b3_8_115200 },
2236
2237 /*
2238 * Exar Corp. XR17C15[248] Dual/Quad/Octal UART
2239 */
2240 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2241 PCI_ANY_ID, PCI_ANY_ID,
2242 0,
2243 0, pbn_exar_XR17C152 },
2244 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2245 PCI_ANY_ID, PCI_ANY_ID,
2246 0,
2247 0, pbn_exar_XR17C154 },
2248 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2249 PCI_ANY_ID, PCI_ANY_ID,
2250 0,
2251 0, pbn_exar_XR17C158 },
2252
2253 /*
2254 * Topic TP560 Data/Fax/Voice 56k modem (reported by Evan Clarke)
2255 */
2256 { PCI_VENDOR_ID_TOPIC, PCI_DEVICE_ID_TOPIC_TP560,
2257 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2258 pbn_b0_1_115200 },
2259
2260 /*
2261 * These entries match devices with class COMMUNICATION_SERIAL,
2262 * COMMUNICATION_MODEM or COMMUNICATION_MULTISERIAL
2263 */
2264 { PCI_ANY_ID, PCI_ANY_ID,
2265 PCI_ANY_ID, PCI_ANY_ID,
2266 PCI_CLASS_COMMUNICATION_SERIAL << 8,
2267 0xffff00, pbn_default },
2268 { PCI_ANY_ID, PCI_ANY_ID,
2269 PCI_ANY_ID, PCI_ANY_ID,
2270 PCI_CLASS_COMMUNICATION_MODEM << 8,
2271 0xffff00, pbn_default },
2272 { PCI_ANY_ID, PCI_ANY_ID,
2273 PCI_ANY_ID, PCI_ANY_ID,
2274 PCI_CLASS_COMMUNICATION_MULTISERIAL << 8,
2275 0xffff00, pbn_default },
2276 { 0, }
2277};
2278
2279static struct pci_driver serial_pci_driver = {
2280 .name = "serial",
2281 .probe = pciserial_init_one,
2282 .remove = __devexit_p(pciserial_remove_one),
2283 .suspend = pciserial_suspend_one,
2284 .resume = pciserial_resume_one,
2285 .id_table = serial_pci_tbl,
2286};
2287
2288static int __init serial8250_pci_init(void)
2289{
2290 return pci_register_driver(&serial_pci_driver);
2291}
2292
2293static void __exit serial8250_pci_exit(void)
2294{
2295 pci_unregister_driver(&serial_pci_driver);
2296}
2297
2298module_init(serial8250_pci_init);
2299module_exit(serial8250_pci_exit);
2300
2301MODULE_LICENSE("GPL");
2302MODULE_DESCRIPTION("Generic 8250/16x50 PCI serial probe module");
2303MODULE_DEVICE_TABLE(pci, serial_pci_tbl);
diff --git a/drivers/serial/8250_pnp.c b/drivers/serial/8250_pnp.c
new file mode 100644
index 000000000000..18c58fb73899
--- /dev/null
+++ b/drivers/serial/8250_pnp.c
@@ -0,0 +1,457 @@
1/*
2 * linux/drivers/char/8250_pnp.c
3 *
4 * Probe module for 8250/16550-type ISAPNP serial ports.
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright (C) 2001 Russell King, All Rights Reserved.
9 *
10 * Ported to the Linux PnP Layer - (C) Adam Belay.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License.
15 *
16 * $Id: 8250_pnp.c,v 1.10 2002/07/21 21:32:30 rmk Exp $
17 */
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/pci.h>
21#include <linux/pnp.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/serial_core.h>
25#include <linux/bitops.h>
26
27#include <asm/byteorder.h>
28
29#include "8250.h"
30
31#define UNKNOWN_DEV 0x3000
32
33
34static const struct pnp_device_id pnp_dev_table[] = {
35 /* Archtek America Corp. */
36 /* Archtek SmartLink Modem 3334BT Plug & Play */
37 { "AAC000F", 0 },
38 /* Anchor Datacomm BV */
39 /* SXPro 144 External Data Fax Modem Plug & Play */
40 { "ADC0001", 0 },
41 /* SXPro 288 External Data Fax Modem Plug & Play */
42 { "ADC0002", 0 },
43 /* PROLiNK 1456VH ISA PnP K56flex Fax Modem */
44 { "AEI0250", 0 },
45 /* Actiontec ISA PNP 56K X2 Fax Modem */
46 { "AEI1240", 0 },
47 /* Rockwell 56K ACF II Fax+Data+Voice Modem */
48 { "AKY1021", 0 /*SPCI_FL_NO_SHIRQ*/ },
49 /* AZT3005 PnP SOUND DEVICE */
50 { "AZT4001", 0 },
51 /* Best Data Products Inc. Smart One 336F PnP Modem */
52 { "BDP3336", 0 },
53 /* Boca Research */
54 /* Boca Complete Ofc Communicator 14.4 Data-FAX */
55 { "BRI0A49", 0 },
56 /* Boca Research 33,600 ACF Modem */
57 { "BRI1400", 0 },
58 /* Boca 33.6 Kbps Internal FD34FSVD */
59 { "BRI3400", 0 },
60 /* Boca 33.6 Kbps Internal FD34FSVD */
61 { "BRI0A49", 0 },
62 /* Best Data Products Inc. Smart One 336F PnP Modem */
63 { "BDP3336", 0 },
64 /* Computer Peripherals Inc */
65 /* EuroViVa CommCenter-33.6 SP PnP */
66 { "CPI4050", 0 },
67 /* Creative Labs */
68 /* Creative Labs Phone Blaster 28.8 DSVD PnP Voice */
69 { "CTL3001", 0 },
70 /* Creative Labs Modem Blaster 28.8 DSVD PnP Voice */
71 { "CTL3011", 0 },
72 /* Creative */
73 /* Creative Modem Blaster Flash56 DI5601-1 */
74 { "DMB1032", 0 },
75 /* Creative Modem Blaster V.90 DI5660 */
76 { "DMB2001", 0 },
77 /* E-Tech */
78 /* E-Tech CyberBULLET PC56RVP */
79 { "ETT0002", 0 },
80 /* FUJITSU */
81 /* Fujitsu 33600 PnP-I2 R Plug & Play */
82 { "FUJ0202", 0 },
83 /* Fujitsu FMV-FX431 Plug & Play */
84 { "FUJ0205", 0 },
85 /* Fujitsu 33600 PnP-I4 R Plug & Play */
86 { "FUJ0206", 0 },
87 /* Fujitsu Fax Voice 33600 PNP-I5 R Plug & Play */
88 { "FUJ0209", 0 },
89 /* Archtek America Corp. */
90 /* Archtek SmartLink Modem 3334BT Plug & Play */
91 { "GVC000F", 0 },
92 /* Hayes */
93 /* Hayes Optima 288 V.34-V.FC + FAX + Voice Plug & Play */
94 { "HAY0001", 0 },
95 /* Hayes Optima 336 V.34 + FAX + Voice PnP */
96 { "HAY000C", 0 },
97 /* Hayes Optima 336B V.34 + FAX + Voice PnP */
98 { "HAY000D", 0 },
99 /* Hayes Accura 56K Ext Fax Modem PnP */
100 { "HAY5670", 0 },
101 /* Hayes Accura 56K Ext Fax Modem PnP */
102 { "HAY5674", 0 },
103 /* Hayes Accura 56K Fax Modem PnP */
104 { "HAY5675", 0 },
105 /* Hayes 288, V.34 + FAX */
106 { "HAYF000", 0 },
107 /* Hayes Optima 288 V.34 + FAX + Voice, Plug & Play */
108 { "HAYF001", 0 },
109 /* IBM */
110 /* IBM Thinkpad 701 Internal Modem Voice */
111 { "IBM0033", 0 },
112 /* Intertex */
113 /* Intertex 28k8 33k6 Voice EXT PnP */
114 { "IXDC801", 0 },
115 /* Intertex 33k6 56k Voice EXT PnP */
116 { "IXDC901", 0 },
117 /* Intertex 28k8 33k6 Voice SP EXT PnP */
118 { "IXDD801", 0 },
119 /* Intertex 33k6 56k Voice SP EXT PnP */
120 { "IXDD901", 0 },
121 /* Intertex 28k8 33k6 Voice SP INT PnP */
122 { "IXDF401", 0 },
123 /* Intertex 28k8 33k6 Voice SP EXT PnP */
124 { "IXDF801", 0 },
125 /* Intertex 33k6 56k Voice SP EXT PnP */
126 { "IXDF901", 0 },
127 /* Kortex International */
128 /* KORTEX 28800 Externe PnP */
129 { "KOR4522", 0 },
130 /* KXPro 33.6 Vocal ASVD PnP */
131 { "KORF661", 0 },
132 /* Lasat */
133 /* LASAT Internet 33600 PnP */
134 { "LAS4040", 0 },
135 /* Lasat Safire 560 PnP */
136 { "LAS4540", 0 },
137 /* Lasat Safire 336 PnP */
138 { "LAS5440", 0 },
139 /* Microcom, Inc. */
140 /* Microcom TravelPorte FAST V.34 Plug & Play */
141 { "MNP0281", 0 },
142 /* Microcom DeskPorte V.34 FAST or FAST+ Plug & Play */
143 { "MNP0336", 0 },
144 /* Microcom DeskPorte FAST EP 28.8 Plug & Play */
145 { "MNP0339", 0 },
146 /* Microcom DeskPorte 28.8P Plug & Play */
147 { "MNP0342", 0 },
148 /* Microcom DeskPorte FAST ES 28.8 Plug & Play */
149 { "MNP0500", 0 },
150 /* Microcom DeskPorte FAST ES 28.8 Plug & Play */
151 { "MNP0501", 0 },
152 /* Microcom DeskPorte 28.8S Internal Plug & Play */
153 { "MNP0502", 0 },
154 /* Motorola */
155 /* Motorola BitSURFR Plug & Play */
156 { "MOT1105", 0 },
157 /* Motorola TA210 Plug & Play */
158 { "MOT1111", 0 },
159 /* Motorola HMTA 200 (ISDN) Plug & Play */
160 { "MOT1114", 0 },
161 /* Motorola BitSURFR Plug & Play */
162 { "MOT1115", 0 },
163 /* Motorola Lifestyle 28.8 Internal */
164 { "MOT1190", 0 },
165 /* Motorola V.3400 Plug & Play */
166 { "MOT1501", 0 },
167 /* Motorola Lifestyle 28.8 V.34 Plug & Play */
168 { "MOT1502", 0 },
169 /* Motorola Power 28.8 V.34 Plug & Play */
170 { "MOT1505", 0 },
171 /* Motorola ModemSURFR External 28.8 Plug & Play */
172 { "MOT1509", 0 },
173 /* Motorola Premier 33.6 Desktop Plug & Play */
174 { "MOT150A", 0 },
175 /* Motorola VoiceSURFR 56K External PnP */
176 { "MOT150F", 0 },
177 /* Motorola ModemSURFR 56K External PnP */
178 { "MOT1510", 0 },
179 /* Motorola ModemSURFR 56K Internal PnP */
180 { "MOT1550", 0 },
181 /* Motorola ModemSURFR Internal 28.8 Plug & Play */
182 { "MOT1560", 0 },
183 /* Motorola Premier 33.6 Internal Plug & Play */
184 { "MOT1580", 0 },
185 /* Motorola OnlineSURFR 28.8 Internal Plug & Play */
186 { "MOT15B0", 0 },
187 /* Motorola VoiceSURFR 56K Internal PnP */
188 { "MOT15F0", 0 },
189 /* Com 1 */
190 /* Deskline K56 Phone System PnP */
191 { "MVX00A1", 0 },
192 /* PC Rider K56 Phone System PnP */
193 { "MVX00F2", 0 },
194 /* NEC 98NOTE SPEAKER PHONE FAX MODEM(33600bps) */
195 { "nEC8241", 0 },
196 /* Pace 56 Voice Internal Plug & Play Modem */
197 { "PMC2430", 0 },
198 /* Generic */
199 /* Generic standard PC COM port */
200 { "PNP0500", 0 },
201 /* Generic 16550A-compatible COM port */
202 { "PNP0501", 0 },
203 /* Compaq 14400 Modem */
204 { "PNPC000", 0 },
205 /* Compaq 2400/9600 Modem */
206 { "PNPC001", 0 },
207 /* Dial-Up Networking Serial Cable between 2 PCs */
208 { "PNPC031", 0 },
209 /* Dial-Up Networking Parallel Cable between 2 PCs */
210 { "PNPC032", 0 },
211 /* Standard 9600 bps Modem */
212 { "PNPC100", 0 },
213 /* Standard 14400 bps Modem */
214 { "PNPC101", 0 },
215 /* Standard 28800 bps Modem*/
216 { "PNPC102", 0 },
217 /* Standard Modem*/
218 { "PNPC103", 0 },
219 /* Standard 9600 bps Modem*/
220 { "PNPC104", 0 },
221 /* Standard 14400 bps Modem*/
222 { "PNPC105", 0 },
223 /* Standard 28800 bps Modem*/
224 { "PNPC106", 0 },
225 /* Standard Modem */
226 { "PNPC107", 0 },
227 /* Standard 9600 bps Modem */
228 { "PNPC108", 0 },
229 /* Standard 14400 bps Modem */
230 { "PNPC109", 0 },
231 /* Standard 28800 bps Modem */
232 { "PNPC10A", 0 },
233 /* Standard Modem */
234 { "PNPC10B", 0 },
235 /* Standard 9600 bps Modem */
236 { "PNPC10C", 0 },
237 /* Standard 14400 bps Modem */
238 { "PNPC10D", 0 },
239 /* Standard 28800 bps Modem */
240 { "PNPC10E", 0 },
241 /* Standard Modem */
242 { "PNPC10F", 0 },
243 /* Standard PCMCIA Card Modem */
244 { "PNP2000", 0 },
245 /* Rockwell */
246 /* Modular Technology */
247 /* Rockwell 33.6 DPF Internal PnP */
248 /* Modular Technology 33.6 Internal PnP */
249 { "ROK0030", 0 },
250 /* Kortex International */
251 /* KORTEX 14400 Externe PnP */
252 { "ROK0100", 0 },
253 /* Rockwell 28.8 */
254 { "ROK4120", 0 },
255 /* Viking Components, Inc */
256 /* Viking 28.8 INTERNAL Fax+Data+Voice PnP */
257 { "ROK4920", 0 },
258 /* Rockwell */
259 /* British Telecom */
260 /* Modular Technology */
261 /* Rockwell 33.6 DPF External PnP */
262 /* BT Prologue 33.6 External PnP */
263 /* Modular Technology 33.6 External PnP */
264 { "RSS00A0", 0 },
265 /* Viking 56K FAX INT */
266 { "RSS0262", 0 },
267 /* K56 par,VV,Voice,Speakphone,AudioSpan,PnP */
268 { "RSS0250", 0 },
269 /* SupraExpress 28.8 Data/Fax PnP modem */
270 { "SUP1310", 0 },
271 /* SupraExpress 33.6 Data/Fax PnP modem */
272 { "SUP1421", 0 },
273 /* SupraExpress 33.6 Data/Fax PnP modem */
274 { "SUP1590", 0 },
275 /* SupraExpress 33.6 Data/Fax PnP modem */
276 { "SUP1760", 0 },
277 /* Phoebe Micro */
278 /* Phoebe Micro 33.6 Data Fax 1433VQH Plug & Play */
279 { "TEX0011", 0 },
280 /* Archtek America Corp. */
281 /* Archtek SmartLink Modem 3334BT Plug & Play */
282 { "UAC000F", 0 },
283 /* 3Com Corp. */
284 /* Gateway Telepath IIvi 33.6 */
285 { "USR0000", 0 },
286 /* U.S. Robotics Sporster 33.6K Fax INT PnP */
287 { "USR0002", 0 },
288 /* Sportster Vi 14.4 PnP FAX Voicemail */
289 { "USR0004", 0 },
290 /* U.S. Robotics 33.6K Voice INT PnP */
291 { "USR0006", 0 },
292 /* U.S. Robotics 33.6K Voice EXT PnP */
293 { "USR0007", 0 },
294 /* U.S. Robotics Courier V.Everything INT PnP */
295 { "USR0009", 0 },
296 /* U.S. Robotics 33.6K Voice INT PnP */
297 { "USR2002", 0 },
298 /* U.S. Robotics 56K Voice INT PnP */
299 { "USR2070", 0 },
300 /* U.S. Robotics 56K Voice EXT PnP */
301 { "USR2080", 0 },
302 /* U.S. Robotics 56K FAX INT */
303 { "USR3031", 0 },
304 /* U.S. Robotics 56K FAX INT */
305 { "USR3050", 0 },
306 /* U.S. Robotics 56K Voice INT PnP */
307 { "USR3070", 0 },
308 /* U.S. Robotics 56K Voice EXT PnP */
309 { "USR3080", 0 },
310 /* U.S. Robotics 56K Voice INT PnP */
311 { "USR3090", 0 },
312 /* U.S. Robotics 56K Message */
313 { "USR9100", 0 },
314 /* U.S. Robotics 56K FAX EXT PnP*/
315 { "USR9160", 0 },
316 /* U.S. Robotics 56K FAX INT PnP*/
317 { "USR9170", 0 },
318 /* U.S. Robotics 56K Voice EXT PnP*/
319 { "USR9180", 0 },
320 /* U.S. Robotics 56K Voice INT PnP*/
321 { "USR9190", 0 },
322 /* Rockwell's (PORALiNK) 33600 INT PNP */
323 { "WCI0003", 0 },
324 /* Unkown PnP modems */
325 { "PNPCXXX", UNKNOWN_DEV },
326 /* More unkown PnP modems */
327 { "PNPDXXX", UNKNOWN_DEV },
328 { "", 0 }
329};
330
331MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
332
333static char *modem_names[] __devinitdata = {
334 "MODEM", "Modem", "modem", "FAX", "Fax", "fax",
335 "56K", "56k", "K56", "33.6", "28.8", "14.4",
336 "33,600", "28,800", "14,400", "33.600", "28.800", "14.400",
337 "33600", "28800", "14400", "V.90", "V.34", "V.32", NULL
338};
339
340static int __devinit check_name(char *name)
341{
342 char **tmp;
343
344 for (tmp = modem_names; *tmp; tmp++)
345 if (strstr(name, *tmp))
346 return 1;
347
348 return 0;
349}
350
351static int __devinit check_resources(struct pnp_option *option)
352{
353 struct pnp_option *tmp;
354 if (!option)
355 return 0;
356
357 for (tmp = option; tmp; tmp = tmp->next) {
358 struct pnp_port *port;
359 for (port = tmp->port; port; port = port->next)
360 if ((port->size == 8) &&
361 ((port->min == 0x2f8) ||
362 (port->min == 0x3f8) ||
363 (port->min == 0x2e8) ||
364 (port->min == 0x3e8)))
365 return 1;
366 }
367
368 return 0;
369}
370
371/*
372 * Given a complete unknown PnP device, try to use some heuristics to
373 * detect modems. Currently use such heuristic set:
374 * - dev->name or dev->bus->name must contain "modem" substring;
375 * - device must have only one IO region (8 byte long) with base address
376 * 0x2e8, 0x3e8, 0x2f8 or 0x3f8.
377 *
378 * Such detection looks very ugly, but can detect at least some of numerous
379 * PnP modems, alternatively we must hardcode all modems in pnp_devices[]
380 * table.
381 */
382static int __devinit serial_pnp_guess_board(struct pnp_dev *dev, int *flags)
383{
384 if (!(check_name(pnp_dev_name(dev)) || (dev->card && check_name(dev->card->name))))
385 return -ENODEV;
386
387 if (check_resources(dev->independent))
388 return 0;
389
390 if (check_resources(dev->dependent))
391 return 0;
392
393 return -ENODEV;
394}
395
396static int __devinit
397serial_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id *dev_id)
398{
399 struct uart_port port;
400 int ret, line, flags = dev_id->driver_data;
401
402 if (flags & UNKNOWN_DEV) {
403 ret = serial_pnp_guess_board(dev, &flags);
404 if (ret < 0)
405 return ret;
406 }
407
408 memset(&port, 0, sizeof(struct uart_port));
409 port.irq = pnp_irq(dev,0);
410 port.iobase = pnp_port_start(dev, 0);
411
412#ifdef SERIAL_DEBUG_PNP
413 printk("Setup PNP port: port %x, irq %d, type %d\n",
414 port.iobase, port.irq, port.iotype);
415#endif
416
417 port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
418 port.uartclk = 1843200;
419 port.dev = &dev->dev;
420
421 line = serial8250_register_port(&port);
422
423 if (line >= 0)
424 pnp_set_drvdata(dev, (void *)((long)line + 1));
425 return line >= 0 ? 0 : -ENODEV;
426
427}
428
429static void __devexit serial_pnp_remove(struct pnp_dev * dev)
430{
431 long line = (long)pnp_get_drvdata(dev);
432 if (line)
433 serial8250_unregister_port(line - 1);
434}
435
436static struct pnp_driver serial_pnp_driver = {
437 .name = "serial",
438 .id_table = pnp_dev_table,
439 .probe = serial_pnp_probe,
440 .remove = __devexit_p(serial_pnp_remove),
441};
442
443static int __init serial8250_pnp_init(void)
444{
445 return pnp_register_driver(&serial_pnp_driver);
446}
447
448static void __exit serial8250_pnp_exit(void)
449{
450 pnp_unregister_driver(&serial_pnp_driver);
451}
452
453module_init(serial8250_pnp_init);
454module_exit(serial8250_pnp_exit);
455
456MODULE_LICENSE("GPL");
457MODULE_DESCRIPTION("Generic 8250/16x50 PnP serial driver");
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
new file mode 100644
index 000000000000..6e44b46c9e9d
--- /dev/null
+++ b/drivers/serial/Kconfig
@@ -0,0 +1,846 @@
1#
2# Serial device configuration
3#
4# $Id: Kconfig,v 1.11 2004/03/11 18:08:04 lethal Exp $
5#
6
7menu "Serial drivers"
8
9#
10# The new 8250/16550 serial drivers
11config SERIAL_8250
12 tristate "8250/16550 and compatible serial support"
13 depends on (BROKEN || !(SPARC64 || SPARC32))
14 select SERIAL_CORE
15 ---help---
16 This selects whether you want to include the driver for the standard
17 serial ports. The standard answer is Y. People who might say N
18 here are those that are setting up dedicated Ethernet WWW/FTP
19 servers, or users that have one of the various bus mice instead of a
20 serial mouse and don't intend to use their machine's standard serial
21 port for anything. (Note that the Cyclades and Stallion multi
22 serial port drivers do not need this driver built in for them to
23 work.)
24
25 To compile this driver as a module, choose M here: the
26 module will be called serial.
27 [WARNING: Do not compile this driver as a module if you are using
28 non-standard serial ports, since the configuration information will
29 be lost when the driver is unloaded. This limitation may be lifted
30 in the future.]
31
32 BTW1: If you have a mouseman serial mouse which is not recognized by
33 the X window system, try running gpm first.
34
35 BTW2: If you intend to use a software modem (also called Winmodem)
36 under Linux, forget it. These modems are crippled and require
37 proprietary drivers which are only available under Windows.
38
39 Most people will say Y or M here, so that they can use serial mice,
40 modems and similar devices connecting to the standard serial ports.
41
42config SERIAL_8250_CONSOLE
43 bool "Console on 8250/16550 and compatible serial port"
44 depends on SERIAL_8250=y
45 select SERIAL_CORE_CONSOLE
46 ---help---
47 If you say Y here, it will be possible to use a serial port as the
48 system console (the system console is the device which receives all
49 kernel messages and warnings and which allows logins in single user
50 mode). This could be useful if some terminal or printer is connected
51 to that serial port.
52
53 Even if you say Y here, the currently visible virtual console
54 (/dev/tty0) will still be used as the system console by default, but
55 you can alter that using a kernel command line option such as
56 "console=ttyS1". (Try "man bootparam" or see the documentation of
57 your boot loader (grub or lilo or loadlin) about how to pass options
58 to the kernel at boot time.)
59
60 If you don't have a VGA card installed and you say Y here, the
61 kernel will automatically use the first serial line, /dev/ttyS0, as
62 system console.
63
64 If unsure, say N.
65
66config SERIAL_8250_CS
67 tristate "8250/16550 PCMCIA device support"
68 depends on PCMCIA && SERIAL_8250
69 ---help---
70 Say Y here to enable support for 16-bit PCMCIA serial devices,
71 including serial port cards, modems, and the modem functions of
72 multi-function Ethernet/modem cards. (PCMCIA- or PC-cards are
73 credit-card size devices often used with laptops.)
74
75 To compile this driver as a module, choose M here: the
76 module will be called serial_cs.
77
78 If unsure, say N.
79
80config SERIAL_8250_ACPI
81 bool "8250/16550 device discovery via ACPI namespace"
82 default y if IA64
83 depends on ACPI_BUS && SERIAL_8250
84 ---help---
85 If you wish to enable serial port discovery via the ACPI
86 namespace, say Y here. If unsure, say N.
87
88config SERIAL_8250_NR_UARTS
89 int "Maximum number of non-legacy 8250/16550 serial ports"
90 depends on SERIAL_8250
91 default "4"
92 ---help---
93 Set this to the number of non-legacy serial ports you want
94 the driver to support. This includes any ports discovered
95 via ACPI or PCI enumeration and any ports that may be added
96 at run-time via hot-plug.
97
98config SERIAL_8250_EXTENDED
99 bool "Extended 8250/16550 serial driver options"
100 depends on SERIAL_8250
101 help
102 If you wish to use any non-standard features of the standard "dumb"
103 driver, say Y here. This includes HUB6 support, shared serial
104 interrupts, special multiport support, support for more than the
105 four COM 1/2/3/4 boards, etc.
106
107 Note that the answer to this question won't directly affect the
108 kernel: saying N will just cause the configurator to skip all
109 the questions about serial driver options. If unsure, say N.
110
111config SERIAL_8250_MANY_PORTS
112 bool "Support more than 4 legacy serial ports"
113 depends on SERIAL_8250_EXTENDED && !IA64
114 help
115 Say Y here if you have dumb serial boards other than the four
116 standard COM 1/2/3/4 ports. This may happen if you have an AST
117 FourPort, Accent Async, Boca (read the Boca mini-HOWTO, available
118 from <http://www.tldp.org/docs.html#howto>), or other custom
119 serial port hardware which acts similar to standard serial port
120 hardware. If you only use the standard COM 1/2/3/4 ports, you can
121 say N here to save some memory. You can also say Y if you have an
122 "intelligent" multiport card such as Cyclades, Digiboards, etc.
123
124config SERIAL_8250_SHARE_IRQ
125 bool "Support for sharing serial interrupts"
126 depends on SERIAL_8250_EXTENDED
127 help
128 Some serial boards have hardware support which allows multiple dumb
129 serial ports on the same board to share a single IRQ. To enable
130 support for this in the serial driver, say Y here.
131
132config SERIAL_8250_DETECT_IRQ
133 bool "Autodetect IRQ on standard ports (unsafe)"
134 depends on SERIAL_8250_EXTENDED
135 help
136 Say Y here if you want the kernel to try to guess which IRQ
137 to use for your serial port.
138
139 This is considered unsafe; it is far better to configure the IRQ in
140 a boot script using the setserial command.
141
142 If unsure, say N.
143
144config SERIAL_8250_MULTIPORT
145 bool "Support special multiport boards"
146 depends on SERIAL_8250_EXTENDED
147 help
148 Some multiport serial ports have special ports which are used to
149 signal when there are any serial ports on the board which need
150 servicing. Say Y here to enable the serial driver to take advantage
151 of those special I/O ports.
152
153config SERIAL_8250_RSA
154 bool "Support RSA serial ports"
155 depends on SERIAL_8250_EXTENDED
156 help
157 ::: To be written :::
158
159comment "Non-8250 serial port support"
160
161config SERIAL_8250_ACORN
162 tristate "Acorn expansion card serial port support"
163 depends on ARM && ARCH_ACORN && SERIAL_8250
164 help
165 If you have an Atomwide Serial card or Serial Port card for an Acorn
166 system, say Y to this option. The driver can handle 1, 2, or 3 port
167 cards. If unsure, say N.
168
169config SERIAL_AMBA_PL010
170 tristate "ARM AMBA PL010 serial port support"
171 depends on ARM_AMBA
172 select SERIAL_CORE
173 help
174 This selects the ARM(R) AMBA(R) PrimeCell PL010 UART. If you have
175 an Integrator/AP or Integrator/PP2 platform, say Y or M here.
176
177 If unsure, say N.
178
179config SERIAL_AMBA_PL010_CONSOLE
180 bool "Support for console on AMBA serial port"
181 depends on SERIAL_AMBA_PL010=y
182 select SERIAL_CORE_CONSOLE
183 ---help---
184 Say Y here if you wish to use an AMBA PrimeCell UART as the system
185 console (the system console is the device which receives all kernel
186 messages and warnings and which allows logins in single user mode).
187
188 Even if you say Y here, the currently visible framebuffer console
189 (/dev/tty0) will still be used as the system console by default, but
190 you can alter that using a kernel command line option such as
191 "console=ttyAM0". (Try "man bootparam" or see the documentation of
192 your boot loader (lilo or loadlin) about how to pass options to the
193 kernel at boot time.)
194
195config SERIAL_AMBA_PL011
196 tristate "ARM AMBA PL011 serial port support"
197 depends on ARM_AMBA
198 select SERIAL_CORE
199 help
200 This selects the ARM(R) AMBA(R) PrimeCell PL011 UART. If you have
201 an Integrator/PP2, Integrator/CP or Versatile platform, say Y or M
202 here.
203
204 If unsure, say N.
205
206config SERIAL_AMBA_PL011_CONSOLE
207 bool "Support for console on AMBA serial port"
208 depends on SERIAL_AMBA_PL011=y
209 select SERIAL_CORE_CONSOLE
210 ---help---
211 Say Y here if you wish to use an AMBA PrimeCell UART as the system
212 console (the system console is the device which receives all kernel
213 messages and warnings and which allows logins in single user mode).
214
215 Even if you say Y here, the currently visible framebuffer console
216 (/dev/tty0) will still be used as the system console by default, but
217 you can alter that using a kernel command line option such as
218 "console=ttyAM0". (Try "man bootparam" or see the documentation of
219 your boot loader (lilo or loadlin) about how to pass options to the
220 kernel at boot time.)
221
222config SERIAL_CLPS711X
223 tristate "CLPS711X serial port support"
224 depends on ARM && ARCH_CLPS711X
225 select SERIAL_CORE
226 help
227 ::: To be written :::
228
229config SERIAL_CLPS711X_CONSOLE
230 bool "Support for console on CLPS711X serial port"
231 depends on SERIAL_CLPS711X=y
232 select SERIAL_CORE_CONSOLE
233 help
234 Even if you say Y here, the currently visible virtual console
235 (/dev/tty0) will still be used as the system console by default, but
236 you can alter that using a kernel command line option such as
237 "console=ttyCL1". (Try "man bootparam" or see the documentation of
238 your boot loader (lilo or loadlin) about how to pass options to the
239 kernel at boot time.)
240
241config SERIAL_S3C2410
242 tristate "Samsung S3C2410 Serial port support"
243 depends on ARM && ARCH_S3C2410
244 select SERIAL_CORE
245 help
246 Support for the on-chip UARTs on the Samsung S3C2410X CPU,
247 providing /dev/ttySAC0, 1 and 2 (note, some machines may not
248 provide all of these ports, depending on how the serial port
249 pins are configured.
250
251config SERIAL_S3C2410_CONSOLE
252 bool "Support for console on S3C2410 serial port"
253 depends on SERIAL_S3C2410=y
254 select SERIAL_CORE_CONSOLE
255 help
256 Allow selection of the S3C2410 on-board serial ports for use as
257 an virtual console.
258
259 Even if you say Y here, the currently visible virtual console
260 (/dev/tty0) will still be used as the system console by default, but
261 you can alter that using a kernel command line option such as
262 "console=ttySACx". (Try "man bootparam" or see the documentation of
263 your boot loader about how to pass options to the kernel at
264 boot time.)
265
266config SERIAL_BAST_SIO
267 bool "Support for BAST SuperIO serial ports"
268 depends on ARCH_BAST && SERIAL_8250=y
269 help
270 Support for registerin the SuperIO chip on BAST board with
271 the 8250/16550 uart code.
272
273config SERIAL_DZ
274 bool "DECstation DZ serial driver"
275 depends on MACH_DECSTATION && MIPS32
276 select SERIAL_CORE
277 help
278 DZ11-family serial controllers for VAXstations, including the
279 DC7085, M7814, and M7819.
280
281config SERIAL_DZ_CONSOLE
282 bool "Support console on DECstation DZ serial driver"
283 depends on SERIAL_DZ=y
284 select SERIAL_CORE_CONSOLE
285 help
286 If you say Y here, it will be possible to use a serial port as the
287 system console (the system console is the device which receives all
288 kernel messages and warnings and which allows logins in single user
289 mode). Note that the firmware uses ttyS0 as the serial console on
290 the Maxine and ttyS2 on the others.
291
292 If unsure, say Y.
293
294config SERIAL_21285
295 tristate "DC21285 serial port support"
296 depends on ARM && FOOTBRIDGE
297 select SERIAL_CORE
298 help
299 If you have a machine based on a 21285 (Footbridge) StrongARM(R)/
300 PCI bridge you can enable its onboard serial port by enabling this
301 option.
302
303config SERIAL_21285_CONSOLE
304 bool "Console on DC21285 serial port"
305 depends on SERIAL_21285=y
306 select SERIAL_CORE_CONSOLE
307 help
308 If you have enabled the serial port on the 21285 footbridge you can
309 make it the console by answering Y to this option.
310
311 Even if you say Y here, the currently visible virtual console
312 (/dev/tty0) will still be used as the system console by default, but
313 you can alter that using a kernel command line option such as
314 "console=ttyFB". (Try "man bootparam" or see the documentation of
315 your boot loader (lilo or loadlin) about how to pass options to the
316 kernel at boot time.)
317
318config SERIAL_UART00
319 bool "Excalibur serial port (uart00) support"
320 depends on ARM && ARCH_CAMELOT
321 select SERIAL_CORE
322 help
323 Say Y here if you want to use the hard logic uart on Excalibur. This
324 driver also supports soft logic implementations of this uart core.
325
326config SERIAL_UART00_CONSOLE
327 bool "Support for console on Excalibur serial port"
328 depends on SERIAL_UART00
329 select SERIAL_CORE_CONSOLE
330 help
331 Say Y here if you want to support a serial console on an Excalibur
332 hard logic uart or uart00 IP core.
333
334 Even if you say Y here, the currently visible virtual console
335 (/dev/tty0) will still be used as the system console by default, but
336 you can alter that using a kernel command line option such as
337 "console=ttyS1". (Try "man bootparam" or see the documentation of
338 your boot loader (lilo or loadlin) about how to pass options to the
339 kernel at boot time.)
340
341config SERIAL_MPSC
342 bool "Marvell MPSC serial port support"
343 depends on PPC32 && MV64X60
344 select SERIAL_CORE
345 help
346 Say Y here if you want to use the Marvell MPSC serial controller.
347
348config SERIAL_MPSC_CONSOLE
349 bool "Support for console on Marvell MPSC serial port"
350 depends on SERIAL_MPSC
351 select SERIAL_CORE_CONSOLE
352 help
353 Say Y here if you want to support a serial console on a Marvell MPSC.
354
355config SERIAL_PXA
356 bool "PXA serial port support"
357 depends on ARM && ARCH_PXA
358 select SERIAL_CORE
359 help
360 If you have a machine based on an Intel XScale PXA2xx CPU you
361 can enable its onboard serial ports by enabling this option.
362
363config SERIAL_PXA_CONSOLE
364 bool "Console on PXA serial port"
365 depends on SERIAL_PXA
366 select SERIAL_CORE_CONSOLE
367 help
368 If you have enabled the serial port on the Intel XScale PXA
369 CPU you can make it the console by answering Y to this option.
370
371 Even if you say Y here, the currently visible virtual console
372 (/dev/tty0) will still be used as the system console by default, but
373 you can alter that using a kernel command line option such as
374 "console=ttySA0". (Try "man bootparam" or see the documentation of
375 your boot loader (lilo or loadlin) about how to pass options to the
376 kernel at boot time.)
377
378config SERIAL_SA1100
379 bool "SA1100 serial port support"
380 depends on ARM && ARCH_SA1100
381 select SERIAL_CORE
382 help
383 If you have a machine based on a SA1100/SA1110 StrongARM(R) CPU you
384 can enable its onboard serial port by enabling this option.
385 Please read <file:Documentation/arm/SA1100/serial_UART> for further
386 info.
387
388config SERIAL_SA1100_CONSOLE
389 bool "Console on SA1100 serial port"
390 depends on SERIAL_SA1100
391 select SERIAL_CORE_CONSOLE
392 help
393 If you have enabled the serial port on the SA1100/SA1110 StrongARM
394 CPU you can make it the console by answering Y to this option.
395
396 Even if you say Y here, the currently visible virtual console
397 (/dev/tty0) will still be used as the system console by default, but
398 you can alter that using a kernel command line option such as
399 "console=ttySA0". (Try "man bootparam" or see the documentation of
400 your boot loader (lilo or loadlin) about how to pass options to the
401 kernel at boot time.)
402
403config SERIAL_IMX
404 bool "IMX serial port support"
405 depends on ARM && ARCH_IMX
406 select SERIAL_CORE
407 help
408 If you have a machine based on a Motorola IMX CPU you
409 can enable its onboard serial port by enabling this option.
410
411config SERIAL_IMX_CONSOLE
412 bool "Console on IMX serial port"
413 depends on SERIAL_IMX
414 select SERIAL_CORE_CONSOLE
415 help
416 If you have enabled the serial port on the Motorola IMX
417 CPU you can make it the console by answering Y to this option.
418
419 Even if you say Y here, the currently visible virtual console
420 (/dev/tty0) will still be used as the system console by default, but
421 you can alter that using a kernel command line option such as
422 "console=ttySA0". (Try "man bootparam" or see the documentation of
423 your boot loader (lilo or loadlin) about how to pass options to the
424 kernel at boot time.)
425
426config SERIAL_SUNCORE
427 bool
428 depends on SPARC32 || SPARC64
429 select SERIAL_CORE
430 select SERIAL_CORE_CONSOLE
431 default y
432
433config SERIAL_SUNZILOG
434 tristate "Sun Zilog8530 serial support"
435 depends on SPARC32 || SPARC64
436 help
437 This driver supports the Zilog8530 serial ports found on many Sparc
438 systems. Say Y or M if you want to be able to these serial ports.
439
440config SERIAL_SUNZILOG_CONSOLE
441 bool "Console on Sun Zilog8530 serial port"
442 depends on SERIAL_SUNZILOG=y
443 help
444 If you would like to be able to use the Zilog8530 serial port
445 on your Sparc system as the console, you can do so by answering
446 Y to this option.
447
448config SERIAL_SUNSU
449 tristate "Sun SU serial support"
450 depends on (SPARC32 || SPARC64) && PCI
451 help
452 This driver supports the 8250 serial ports that run the keyboard and
453 mouse on (PCI) UltraSPARC systems. Say Y or M if you want to be able
454 to these serial ports.
455
456config SERIAL_SUNSU_CONSOLE
457 bool "Console on Sun SU serial port"
458 depends on SERIAL_SUNSU=y
459 help
460 If you would like to be able to use the SU serial port
461 on your Sparc system as the console, you can do so by answering
462 Y to this option.
463
464config SERIAL_MUX
465 tristate "Serial MUX support"
466 depends on PARISC
467 select SERIAL_CORE
468 default y
469 ---help---
470 Saying Y here will enable the hardware MUX serial driver for
471 the Nova and K class systems. The hardware MUX is not 8250/16550
472 compatible therefore the /dev/ttyB0 device is shared between the
473 Serial MUX and the PDC software console. The following steps
474 need to be completed to use the Serial MUX:
475
476 1. create the device entry (mknod /dev/ttyB0 c 11 0)
477 2. Edit the /etc/inittab to start a getty listening on /dev/ttyB0
478 3. Add device ttyB0 to /etc/securetty (if you want to log on as
479 root on this console.)
480 4. Change the kernel command console parameter to: console=ttyB0
481
482config SERIAL_MUX_CONSOLE
483 bool "Support for console on serial MUX"
484 depends on SERIAL_MUX
485 select SERIAL_CORE_CONSOLE
486 default y
487
488config PDC_CONSOLE
489 bool "PDC software console support"
490 depends on PARISC && !SERIAL_MUX && VT
491 default n
492 help
493 Saying Y here will enable the software based PDC console to be
494 used as the system console. This is useful for machines in
495 which the hardware based console has not been written yet. The
496 following steps must be competed to use the PDC console:
497
498 1. create the device entry (mknod /dev/ttyB0 c 11 0)
499 2. Edit the /etc/inittab to start a getty listening on /dev/ttyB0
500 3. Add device ttyB0 to /etc/securetty (if you want to log on as
501 root on this console.)
502 4. Change the kernel command console parameter to: console=ttyB0
503
504config SERIAL_SUNSAB
505 tristate "Sun Siemens SAB82532 serial support"
506 depends on (SPARC32 || SPARC64) && PCI
507 help
508 This driver supports the Siemens SAB82532 DUSCC serial ports on newer
509 (PCI) UltraSPARC systems. Say Y or M if you want to be able to these
510 serial ports.
511
512config SERIAL_SUNSAB_CONSOLE
513 bool "Console on Sun Siemens SAB82532 serial port"
514 depends on SERIAL_SUNSAB=y
515 help
516 If you would like to be able to use the SAB82532 serial port
517 on your Sparc system as the console, you can do so by answering
518 Y to this option.
519
520config SERIAL_IP22_ZILOG
521 tristate "IP22 Zilog8530 serial support"
522 depends on SGI_IP22
523 select SERIAL_CORE
524 help
525 This driver supports the Zilog8530 serial ports found on SGI IP22
526 systems. Say Y or M if you want to be able to these serial ports.
527
528config SERIAL_IP22_ZILOG_CONSOLE
529 bool "Console on IP22 Zilog8530 serial port"
530 depends on SERIAL_IP22_ZILOG=y
531 select SERIAL_CORE_CONSOLE
532
533config V850E_UART
534 bool "NEC V850E on-chip UART support"
535 depends on V850E_MA1 || V850E_ME2 || V850E_TEG || V850E2_ANNA || V850E_AS85EP1
536 select SERIAL_CORE
537 default y
538
539config V850E_UARTB
540 bool
541 depends V850E_UART && V850E_ME2
542 default y
543
544config V850E_UART_CONSOLE
545 bool "Use NEC V850E on-chip UART for console"
546 depends on V850E_UART
547 select SERIAL_CORE_CONSOLE
548
549config SERIAL_SH_SCI
550 tristate "SH SCI(F) serial port support"
551 depends on SUPERH || H8300
552 select SERIAL_CORE
553
554config SERIAL_SH_SCI_CONSOLE
555 bool "Support for console on SH SCI(F)"
556 depends on SERIAL_SH_SCI=y
557 select SERIAL_CORE_CONSOLE
558
559config SERIAL_AU1X00
560 bool "Enable Au1x00 UART Support"
561 depends on MIPS && SOC_AU1X00
562 select SERIAL_CORE
563 help
564 If you have an Alchemy AU1X00 processor (MIPS based) and you want
565 to use serial ports, say Y. Otherwise, say N.
566
567config SERIAL_AU1X00_CONSOLE
568 bool "Enable Au1x00 serial console"
569 depends on SERIAL_AU1X00
570 select SERIAL_CORE_CONSOLE
571 help
572 If you have an Alchemy AU1X00 processor (MIPS based) and you want
573 to use a console on a serial port, say Y. Otherwise, say N.
574
575config SERIAL_CORE
576 tristate
577
578config SERIAL_CORE_CONSOLE
579 bool
580
581config SERIAL_68328
582 bool "68328 serial support"
583 depends on M68328 || M68EZ328 || M68VZ328
584 help
585 This driver supports the built-in serial port of the Motorola 68328
586 (standard, EZ and VZ varities).
587
588config SERIAL_68328_RTS_CTS
589 bool "Support RTS/CTS on 68328 serial port"
590 depends on SERIAL_68328
591
592config SERIAL_COLDFIRE
593 bool "ColdFire serial support"
594 depends on COLDFIRE
595 help
596 This driver supports the built-in serial ports of the Motorola ColdFire
597 family of CPUs.
598
599config SERIAL_68360_SMC
600 bool "68360 SMC uart support"
601 depends on M68360
602 help
603 This driver supports the SMC serial ports of the Motorola 68360 CPU.
604
605config SERIAL_68360_SCC
606 bool "68360 SCC uart support"
607 depends on M68360
608 help
609 This driver supports the SCC serial ports of the Motorola 68360 CPU.
610
611config SERIAL_68360
612 bool
613 depends on SERIAL_68360_SMC || SERIAL_68360_SCC
614 default y
615
616config SERIAL_PMACZILOG
617 tristate "PowerMac z85c30 ESCC support"
618 depends on PPC_OF && PPC_PMAC
619 select SERIAL_CORE
620 help
621 This driver supports the Zilog z85C30 serial ports found on
622 PowerMac machines.
623 Say Y or M if you want to be able to these serial ports.
624
625config SERIAL_PMACZILOG_CONSOLE
626 bool "Console on PowerMac z85c30 serial port"
627 depends on SERIAL_PMACZILOG=y
628 select SERIAL_CORE_CONSOLE
629 help
630 If you would like to be able to use the z85c30 serial port
631 on your PowerMac as the console, you can do so by answering
632 Y to this option.
633
634config SERIAL_LH7A40X
635 tristate "Sharp LH7A40X embedded UART support"
636 depends on ARM && ARCH_LH7A40X
637 select SERIAL_CORE
638 help
639 This enables support for the three on-board UARTs of the
640 Sharp LH7A40X series CPUs. Choose Y or M.
641
642config SERIAL_LH7A40X_CONSOLE
643 bool "Support for console on Sharp LH7A40X serial port"
644 depends on SERIAL_LH7A40X=y
645 select SERIAL_CORE_CONSOLE
646 help
647 Say Y here if you wish to use one of the serial ports as the
648 system console--the system console is the device which
649 receives all kernel messages and warnings and which allows
650 logins in single user mode.
651
652 Even if you say Y here, the currently visible framebuffer console
653 (/dev/tty0) will still be used as the default system console, but
654 you can alter that using a kernel command line, for example
655 "console=ttyAM1".
656
657config SERIAL_CPM
658 tristate "CPM SCC/SMC serial port support"
659 depends on CPM2 || 8xx
660 select SERIAL_CORE
661 help
662 This driver supports the SCC and SMC serial ports on Motorola
663 embedded PowerPC that contain a CPM1 (8xx) or CPM2 (8xxx)
664
665config SERIAL_CPM_CONSOLE
666 bool "Support for console on CPM SCC/SMC serial port"
667 depends on SERIAL_CPM=y
668 select SERIAL_CORE_CONSOLE
669 help
670 Say Y here if you wish to use a SCC or SMC CPM UART as the system
671 console (the system console is the device which receives all kernel
672 messages and warnings and which allows logins in single user mode).
673
674 Even if you say Y here, the currently visible framebuffer console
675 (/dev/tty0) will still be used as the system console by default, but
676 you can alter that using a kernel command line option such as
677 "console=ttyCPM0". (Try "man bootparam" or see the documentation of
678 your boot loader (lilo or loadlin) about how to pass options to the
679 kernel at boot time.)
680
681config SERIAL_CPM_SCC1
682 bool "Support for SCC1 serial port"
683 depends on SERIAL_CPM=y
684 help
685 Select the is option to use SCC1 as a serial port
686
687config SERIAL_CPM_SCC2
688 bool "Support for SCC2 serial port"
689 depends on SERIAL_CPM=y
690 help
691 Select the is option to use SCC2 as a serial port
692
693config SERIAL_CPM_SCC3
694 bool "Support for SCC3 serial port"
695 depends on SERIAL_CPM=y
696 help
697 Select the is option to use SCC3 as a serial port
698
699config SERIAL_CPM_SCC4
700 bool "Support for SCC4 serial port"
701 depends on SERIAL_CPM=y
702 help
703 Select the is option to use SCC4 as a serial port
704
705config SERIAL_CPM_SMC1
706 bool "Support for SMC1 serial port"
707 depends on SERIAL_CPM=y
708 help
709 Select the is option to use SMC1 as a serial port
710
711config SERIAL_CPM_SMC2
712 bool "Support for SMC2 serial port"
713 depends on SERIAL_CPM=y
714 help
715 Select the is option to use SMC2 as a serial port
716
717config SERIAL_SGI_L1_CONSOLE
718 bool "SGI Altix L1 serial console support"
719 depends on IA64_GENERIC || IA64_SGI_SN2
720 select SERIAL_CORE
721 select SERIAL_CORE_CONSOLE
722 help
723 If you have an SGI Altix and you would like to use the system
724 controller serial port as your console (you want this!),
725 say Y. Otherwise, say N.
726
727config SERIAL_MPC52xx
728 tristate "Freescale MPC52xx family PSC serial support"
729 depends on PPC_MPC52xx
730 select SERIAL_CORE
731 help
732 This drivers support the MPC52xx PSC serial ports. If you would
733 like to use them, you must answer Y or M to this option. Not that
734 for use as console, it must be included in kernel and not as a
735 module.
736
737config SERIAL_MPC52xx_CONSOLE
738 bool "Console on a Freescale MPC52xx family PSC serial port"
739 depends on SERIAL_MPC52xx=y
740 select SERIAL_CORE_CONSOLE
741 help
742 Select this options if you'd like to use one of the PSC serial port
743 of the Freescale MPC52xx family as a console.
744
745config SERIAL_MPC52xx_CONSOLE_BAUD
746 int "Freescale MPC52xx family PSC serial port baud"
747 depends on SERIAL_MPC52xx_CONSOLE=y
748 default "9600"
749 help
750 Select the MPC52xx console baud rate.
751 This value is only used if the bootloader doesn't pass in the
752 console baudrate
753
754config SERIAL_ICOM
755 tristate "IBM Multiport Serial Adapter"
756 depends on PPC_ISERIES || PPC_PSERIES
757 select SERIAL_CORE
758 help
759 This driver is for a family of multiport serial adapters
760 including 2 port RVX, 2 port internal modem, 4 port internal
761 modem and a split 1 port RVX and 1 port internal modem.
762
763 This driver can also be built as a module. If so, the module
764 will be called icom.
765
766config SERIAL_M32R_SIO
767 bool "M32R SIO I/F"
768 depends on M32R
769 default y
770 select SERIAL_CORE
771 help
772 Say Y here if you want to use the M32R serial controller.
773
774config SERIAL_M32R_SIO_CONSOLE
775 bool "use SIO console"
776 depends on SERIAL_M32R_SIO=y
777 select SERIAL_CORE_CONSOLE
778 help
779 Say Y here if you want to support a serial console.
780
781 If you use an M3T-M32700UT or an OPSPUT platform,
782 please say also y for SERIAL_M32R_PLDSIO.
783
784config SERIAL_M32R_PLDSIO
785 bool "M32R SIO I/F on a PLD"
786 depends on SERIAL_M32R_SIO=y
787 default n
788 help
789 Say Y here if you want to use the M32R serial controller
790 on a PLD (Programmable Logic Device).
791
792 If you use an M3T-M32700UT or an OPSPUT platform,
793 please say Y.
794
795config SERIAL_TXX9
796 bool "TMPTX39XX/49XX SIO support"
797 depends HAS_TXX9_SERIAL
798 select SERIAL_CORE
799 default y
800
801config HAS_TXX9_SERIAL
802 bool
803
804config SERIAL_TXX9_CONSOLE
805 bool "TMPTX39XX/49XX SIO Console support"
806 depends on SERIAL_TXX9=y
807 select SERIAL_CORE_CONSOLE
808
809config SERIAL_TXX9_STDSERIAL
810 bool "TX39XX/49XX SIO act as standard serial"
811 depends on !SERIAL_8250 && SERIAL_TXX9
812
813config SERIAL_VR41XX
814 tristate "NEC VR4100 series Serial Interface Unit support"
815 depends on CPU_VR41XX
816 select SERIAL_CORE
817 help
818 If you have a NEC VR4100 series processor and you want to use
819 Serial Interface Unit(SIU) or Debug Serial Interface Unit(DSIU)
820 (not include VR4111/VR4121 DSIU), say Y. Otherwise, say N.
821
822config SERIAL_VR41XX_CONSOLE
823 bool "Enable NEC VR4100 series Serial Interface Unit console"
824 depends on SERIAL_VR41XX
825 select SERIAL_CORE_CONSOLE
826 help
827 If you have a NEC VR4100 series processor and you want to use
828 a console on a serial port, say Y. Otherwise, say N.
829
830config SERIAL_JSM
831 tristate "Digi International NEO PCI Support"
832 depends on PCI
833 select SERIAL_CORE
834 help
835 This is a driver for Digi International's Neo series
836 of cards which provide multiple serial ports. You would need
837 something like this to connect more than two modems to your Linux
838 box, for instance in order to become a dial-in server. This driver
839 supports PCI boards only.
840 If you have a card like this, say Y here and read the file
841 <file:Documentation/jsm.txt>.
842
843 To compile this driver as a module, choose M here: the
844 module will be called jsm.
845
846endmenu
diff --git a/drivers/serial/Makefile b/drivers/serial/Makefile
new file mode 100644
index 000000000000..81b77d769b84
--- /dev/null
+++ b/drivers/serial/Makefile
@@ -0,0 +1,54 @@
1#
2# Makefile for the kernel serial device drivers.
3#
4# $Id: Makefile,v 1.8 2002/07/21 21:32:30 rmk Exp $
5#
6
7serial-8250-y :=
8serial-8250-$(CONFIG_SERIAL_8250_ACPI) += 8250_acpi.o
9serial-8250-$(CONFIG_PNP) += 8250_pnp.o
10serial-8250-$(CONFIG_GSC) += 8250_gsc.o
11serial-8250-$(CONFIG_PCI) += 8250_pci.o
12serial-8250-$(CONFIG_HP300) += 8250_hp300.o
13
14obj-$(CONFIG_SERIAL_CORE) += serial_core.o
15obj-$(CONFIG_SERIAL_21285) += 21285.o
16obj-$(CONFIG_SERIAL_8250) += 8250.o $(serial-8250-y)
17obj-$(CONFIG_SERIAL_8250_CS) += serial_cs.o
18obj-$(CONFIG_SERIAL_8250_ACORN) += 8250_acorn.o
19obj-$(CONFIG_SERIAL_8250_CONSOLE) += 8250_early.o
20obj-$(CONFIG_SERIAL_AMBA_PL010) += amba-pl010.o
21obj-$(CONFIG_SERIAL_AMBA_PL011) += amba-pl011.o
22obj-$(CONFIG_SERIAL_CLPS711X) += clps711x.o
23obj-$(CONFIG_SERIAL_PXA) += pxa.o
24obj-$(CONFIG_SERIAL_SA1100) += sa1100.o
25obj-$(CONFIG_SERIAL_S3C2410) += s3c2410.o
26obj-$(CONFIG_SERIAL_UART00) += uart00.o
27obj-$(CONFIG_SERIAL_SUNCORE) += suncore.o
28obj-$(CONFIG_SERIAL_SUNZILOG) += sunzilog.o
29obj-$(CONFIG_SERIAL_IP22_ZILOG) += ip22zilog.o
30obj-$(CONFIG_SERIAL_SUNSU) += sunsu.o
31obj-$(CONFIG_SERIAL_SUNSAB) += sunsab.o
32obj-$(CONFIG_SERIAL_MUX) += mux.o
33obj-$(CONFIG_SERIAL_68328) += 68328serial.o
34obj-$(CONFIG_SERIAL_68360) += 68360serial.o
35obj-$(CONFIG_SERIAL_COLDFIRE) += mcfserial.o
36obj-$(CONFIG_V850E_UART) += v850e_uart.o
37obj-$(CONFIG_SERIAL_PMACZILOG) += pmac_zilog.o
38obj-$(CONFIG_SERIAL_LH7A40X) += serial_lh7a40x.o
39obj-$(CONFIG_SERIAL_AU1X00) += au1x00_uart.o
40obj-$(CONFIG_SERIAL_DZ) += dz.o
41obj-$(CONFIG_SERIAL_SH_SCI) += sh-sci.o
42obj-$(CONFIG_SERIAL_BAST_SIO) += bast_sio.o
43obj-$(CONFIG_SERIAL_SGI_L1_CONSOLE) += sn_console.o
44obj-$(CONFIG_SERIAL_CPM) += cpm_uart/
45obj-$(CONFIG_SERIAL_IMX) += imx.o
46obj-$(CONFIG_SERIAL_MPC52xx) += mpc52xx_uart.o
47obj-$(CONFIG_SERIAL_ICOM) += icom.o
48obj-$(CONFIG_SERIAL_M32R_SIO) += m32r_sio.o
49obj-$(CONFIG_SERIAL_MPSC) += mpsc.o
50obj-$(CONFIG_ETRAX_SERIAL) += crisv10.o
51obj-$(CONFIG_SERIAL_JSM) += jsm/
52obj-$(CONFIG_SERIAL_TXX9) += serial_txx9.o
53obj-$(CONFIG_SERIAL_VR41XX) += vr41xx_siu.o
54obj-$(CONFIG_BLK_DEV_SGIIOC4) += ioc4_serial.o
diff --git a/drivers/serial/amba-pl010.c b/drivers/serial/amba-pl010.c
new file mode 100644
index 000000000000..ac57fdc8711b
--- /dev/null
+++ b/drivers/serial/amba-pl010.c
@@ -0,0 +1,840 @@
1/*
2 * linux/drivers/char/amba.c
3 *
4 * Driver for AMBA serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * $Id: amba.c,v 1.41 2002/07/28 10:03:27 rmk Exp $
26 *
27 * This is a generic driver for ARM AMBA-type serial ports. They
28 * have a lot of 16550-like features, but are not register compatible.
29 * Note that although they do have CTS, DCD and DSR inputs, they do
30 * not have an RI input, nor do they have DTR or RTS outputs. If
31 * required, these have to be supplied via some other means (eg, GPIO)
32 * and hooked into this driver.
33 */
34#include <linux/config.h>
35
36#if defined(CONFIG_SERIAL_AMBA_PL010_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
37#define SUPPORT_SYSRQ
38#endif
39
40#include <linux/module.h>
41#include <linux/ioport.h>
42#include <linux/init.h>
43#include <linux/console.h>
44#include <linux/sysrq.h>
45#include <linux/device.h>
46#include <linux/tty.h>
47#include <linux/tty_flip.h>
48#include <linux/serial_core.h>
49#include <linux/serial.h>
50
51#include <asm/io.h>
52#include <asm/irq.h>
53#include <asm/hardware/amba.h>
54#include <asm/hardware/amba_serial.h>
55
56#define UART_NR 2
57
58#define SERIAL_AMBA_MAJOR 204
59#define SERIAL_AMBA_MINOR 16
60#define SERIAL_AMBA_NR UART_NR
61
62#define AMBA_ISR_PASS_LIMIT 256
63
64/*
65 * Access macros for the AMBA UARTs
66 */
67#define UART_GET_INT_STATUS(p) readb((p)->membase + UART010_IIR)
68#define UART_PUT_ICR(p, c) writel((c), (p)->membase + UART010_ICR)
69#define UART_GET_FR(p) readb((p)->membase + UART01x_FR)
70#define UART_GET_CHAR(p) readb((p)->membase + UART01x_DR)
71#define UART_PUT_CHAR(p, c) writel((c), (p)->membase + UART01x_DR)
72#define UART_GET_RSR(p) readb((p)->membase + UART01x_RSR)
73#define UART_GET_CR(p) readb((p)->membase + UART010_CR)
74#define UART_PUT_CR(p,c) writel((c), (p)->membase + UART010_CR)
75#define UART_GET_LCRL(p) readb((p)->membase + UART010_LCRL)
76#define UART_PUT_LCRL(p,c) writel((c), (p)->membase + UART010_LCRL)
77#define UART_GET_LCRM(p) readb((p)->membase + UART010_LCRM)
78#define UART_PUT_LCRM(p,c) writel((c), (p)->membase + UART010_LCRM)
79#define UART_GET_LCRH(p) readb((p)->membase + UART010_LCRH)
80#define UART_PUT_LCRH(p,c) writel((c), (p)->membase + UART010_LCRH)
81#define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0)
82#define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0)
83#define UART_TX_EMPTY(p) ((UART_GET_FR(p) & UART01x_FR_TMSK) == 0)
84
85#define UART_DUMMY_RSR_RX /*256*/0
86#define UART_PORT_SIZE 64
87
88/*
89 * On the Integrator platform, the port RTS and DTR are provided by
90 * bits in the following SC_CTRLS register bits:
91 * RTS DTR
92 * UART0 7 6
93 * UART1 5 4
94 */
95#define SC_CTRLC (IO_ADDRESS(INTEGRATOR_SC_BASE) + INTEGRATOR_SC_CTRLC_OFFSET)
96#define SC_CTRLS (IO_ADDRESS(INTEGRATOR_SC_BASE) + INTEGRATOR_SC_CTRLS_OFFSET)
97
98/*
99 * We wrap our port structure around the generic uart_port.
100 */
101struct uart_amba_port {
102 struct uart_port port;
103 unsigned int dtr_mask;
104 unsigned int rts_mask;
105 unsigned int old_status;
106};
107
108static void pl010_stop_tx(struct uart_port *port, unsigned int tty_stop)
109{
110 unsigned int cr;
111
112 cr = UART_GET_CR(port);
113 cr &= ~UART010_CR_TIE;
114 UART_PUT_CR(port, cr);
115}
116
117static void pl010_start_tx(struct uart_port *port, unsigned int tty_start)
118{
119 unsigned int cr;
120
121 cr = UART_GET_CR(port);
122 cr |= UART010_CR_TIE;
123 UART_PUT_CR(port, cr);
124}
125
126static void pl010_stop_rx(struct uart_port *port)
127{
128 unsigned int cr;
129
130 cr = UART_GET_CR(port);
131 cr &= ~(UART010_CR_RIE | UART010_CR_RTIE);
132 UART_PUT_CR(port, cr);
133}
134
135static void pl010_enable_ms(struct uart_port *port)
136{
137 unsigned int cr;
138
139 cr = UART_GET_CR(port);
140 cr |= UART010_CR_MSIE;
141 UART_PUT_CR(port, cr);
142}
143
144static void
145#ifdef SUPPORT_SYSRQ
146pl010_rx_chars(struct uart_port *port, struct pt_regs *regs)
147#else
148pl010_rx_chars(struct uart_port *port)
149#endif
150{
151 struct tty_struct *tty = port->info->tty;
152 unsigned int status, ch, flag, rsr, max_count = 256;
153
154 status = UART_GET_FR(port);
155 while (UART_RX_DATA(status) && max_count--) {
156 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
157 if (tty->low_latency)
158 tty_flip_buffer_push(tty);
159 /*
160 * If this failed then we will throw away the
161 * bytes but must do so to clear interrupts.
162 */
163 }
164
165 ch = UART_GET_CHAR(port);
166 flag = TTY_NORMAL;
167
168 port->icount.rx++;
169
170 /*
171 * Note that the error handling code is
172 * out of the main execution path
173 */
174 rsr = UART_GET_RSR(port) | UART_DUMMY_RSR_RX;
175 if (rsr & UART01x_RSR_ANY) {
176 if (rsr & UART01x_RSR_BE) {
177 rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
178 port->icount.brk++;
179 if (uart_handle_break(port))
180 goto ignore_char;
181 } else if (rsr & UART01x_RSR_PE)
182 port->icount.parity++;
183 else if (rsr & UART01x_RSR_FE)
184 port->icount.frame++;
185 if (rsr & UART01x_RSR_OE)
186 port->icount.overrun++;
187
188 rsr &= port->read_status_mask;
189
190 if (rsr & UART01x_RSR_BE)
191 flag = TTY_BREAK;
192 else if (rsr & UART01x_RSR_PE)
193 flag = TTY_PARITY;
194 else if (rsr & UART01x_RSR_FE)
195 flag = TTY_FRAME;
196 }
197
198 if (uart_handle_sysrq_char(port, ch, regs))
199 goto ignore_char;
200
201 if ((rsr & port->ignore_status_mask) == 0) {
202 tty_insert_flip_char(tty, ch, flag);
203 }
204 if ((rsr & UART01x_RSR_OE) &&
205 tty->flip.count < TTY_FLIPBUF_SIZE) {
206 /*
207 * Overrun is special, since it's reported
208 * immediately, and doesn't affect the current
209 * character
210 */
211 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
212 }
213 ignore_char:
214 status = UART_GET_FR(port);
215 }
216 tty_flip_buffer_push(tty);
217 return;
218}
219
220static void pl010_tx_chars(struct uart_port *port)
221{
222 struct circ_buf *xmit = &port->info->xmit;
223 int count;
224
225 if (port->x_char) {
226 UART_PUT_CHAR(port, port->x_char);
227 port->icount.tx++;
228 port->x_char = 0;
229 return;
230 }
231 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
232 pl010_stop_tx(port, 0);
233 return;
234 }
235
236 count = port->fifosize >> 1;
237 do {
238 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
239 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
240 port->icount.tx++;
241 if (uart_circ_empty(xmit))
242 break;
243 } while (--count > 0);
244
245 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
246 uart_write_wakeup(port);
247
248 if (uart_circ_empty(xmit))
249 pl010_stop_tx(port, 0);
250}
251
252static void pl010_modem_status(struct uart_port *port)
253{
254 struct uart_amba_port *uap = (struct uart_amba_port *)port;
255 unsigned int status, delta;
256
257 UART_PUT_ICR(&uap->port, 0);
258
259 status = UART_GET_FR(&uap->port) & UART01x_FR_MODEM_ANY;
260
261 delta = status ^ uap->old_status;
262 uap->old_status = status;
263
264 if (!delta)
265 return;
266
267 if (delta & UART01x_FR_DCD)
268 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
269
270 if (delta & UART01x_FR_DSR)
271 uap->port.icount.dsr++;
272
273 if (delta & UART01x_FR_CTS)
274 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
275
276 wake_up_interruptible(&uap->port.info->delta_msr_wait);
277}
278
279static irqreturn_t pl010_int(int irq, void *dev_id, struct pt_regs *regs)
280{
281 struct uart_port *port = dev_id;
282 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
283 int handled = 0;
284
285 spin_lock(&port->lock);
286
287 status = UART_GET_INT_STATUS(port);
288 if (status) {
289 do {
290 if (status & (UART010_IIR_RTIS | UART010_IIR_RIS))
291#ifdef SUPPORT_SYSRQ
292 pl010_rx_chars(port, regs);
293#else
294 pl010_rx_chars(port);
295#endif
296 if (status & UART010_IIR_MIS)
297 pl010_modem_status(port);
298 if (status & UART010_IIR_TIS)
299 pl010_tx_chars(port);
300
301 if (pass_counter-- == 0)
302 break;
303
304 status = UART_GET_INT_STATUS(port);
305 } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS |
306 UART010_IIR_TIS));
307 handled = 1;
308 }
309
310 spin_unlock(&port->lock);
311
312 return IRQ_RETVAL(handled);
313}
314
315static unsigned int pl010_tx_empty(struct uart_port *port)
316{
317 return UART_GET_FR(port) & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT;
318}
319
320static unsigned int pl010_get_mctrl(struct uart_port *port)
321{
322 unsigned int result = 0;
323 unsigned int status;
324
325 status = UART_GET_FR(port);
326 if (status & UART01x_FR_DCD)
327 result |= TIOCM_CAR;
328 if (status & UART01x_FR_DSR)
329 result |= TIOCM_DSR;
330 if (status & UART01x_FR_CTS)
331 result |= TIOCM_CTS;
332
333 return result;
334}
335
336static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl)
337{
338 struct uart_amba_port *uap = (struct uart_amba_port *)port;
339 unsigned int ctrls = 0, ctrlc = 0;
340
341 if (mctrl & TIOCM_RTS)
342 ctrlc |= uap->rts_mask;
343 else
344 ctrls |= uap->rts_mask;
345
346 if (mctrl & TIOCM_DTR)
347 ctrlc |= uap->dtr_mask;
348 else
349 ctrls |= uap->dtr_mask;
350
351 __raw_writel(ctrls, SC_CTRLS);
352 __raw_writel(ctrlc, SC_CTRLC);
353}
354
355static void pl010_break_ctl(struct uart_port *port, int break_state)
356{
357 unsigned long flags;
358 unsigned int lcr_h;
359
360 spin_lock_irqsave(&port->lock, flags);
361 lcr_h = UART_GET_LCRH(port);
362 if (break_state == -1)
363 lcr_h |= UART01x_LCRH_BRK;
364 else
365 lcr_h &= ~UART01x_LCRH_BRK;
366 UART_PUT_LCRH(port, lcr_h);
367 spin_unlock_irqrestore(&port->lock, flags);
368}
369
370static int pl010_startup(struct uart_port *port)
371{
372 struct uart_amba_port *uap = (struct uart_amba_port *)port;
373 int retval;
374
375 /*
376 * Allocate the IRQ
377 */
378 retval = request_irq(port->irq, pl010_int, 0, "uart-pl010", port);
379 if (retval)
380 return retval;
381
382 /*
383 * initialise the old status of the modem signals
384 */
385 uap->old_status = UART_GET_FR(port) & UART01x_FR_MODEM_ANY;
386
387 /*
388 * Finally, enable interrupts
389 */
390 UART_PUT_CR(port, UART01x_CR_UARTEN | UART010_CR_RIE |
391 UART010_CR_RTIE);
392
393 return 0;
394}
395
396static void pl010_shutdown(struct uart_port *port)
397{
398 /*
399 * Free the interrupt
400 */
401 free_irq(port->irq, port);
402
403 /*
404 * disable all interrupts, disable the port
405 */
406 UART_PUT_CR(port, 0);
407
408 /* disable break condition and fifos */
409 UART_PUT_LCRH(port, UART_GET_LCRH(port) &
410 ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN));
411}
412
413static void
414pl010_set_termios(struct uart_port *port, struct termios *termios,
415 struct termios *old)
416{
417 unsigned int lcr_h, old_cr;
418 unsigned long flags;
419 unsigned int baud, quot;
420
421 /*
422 * Ask the core to calculate the divisor for us.
423 */
424 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
425 quot = uart_get_divisor(port, baud);
426
427 switch (termios->c_cflag & CSIZE) {
428 case CS5:
429 lcr_h = UART01x_LCRH_WLEN_5;
430 break;
431 case CS6:
432 lcr_h = UART01x_LCRH_WLEN_6;
433 break;
434 case CS7:
435 lcr_h = UART01x_LCRH_WLEN_7;
436 break;
437 default: // CS8
438 lcr_h = UART01x_LCRH_WLEN_8;
439 break;
440 }
441 if (termios->c_cflag & CSTOPB)
442 lcr_h |= UART01x_LCRH_STP2;
443 if (termios->c_cflag & PARENB) {
444 lcr_h |= UART01x_LCRH_PEN;
445 if (!(termios->c_cflag & PARODD))
446 lcr_h |= UART01x_LCRH_EPS;
447 }
448 if (port->fifosize > 1)
449 lcr_h |= UART01x_LCRH_FEN;
450
451 spin_lock_irqsave(&port->lock, flags);
452
453 /*
454 * Update the per-port timeout.
455 */
456 uart_update_timeout(port, termios->c_cflag, baud);
457
458 port->read_status_mask = UART01x_RSR_OE;
459 if (termios->c_iflag & INPCK)
460 port->read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
461 if (termios->c_iflag & (BRKINT | PARMRK))
462 port->read_status_mask |= UART01x_RSR_BE;
463
464 /*
465 * Characters to ignore
466 */
467 port->ignore_status_mask = 0;
468 if (termios->c_iflag & IGNPAR)
469 port->ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
470 if (termios->c_iflag & IGNBRK) {
471 port->ignore_status_mask |= UART01x_RSR_BE;
472 /*
473 * If we're ignoring parity and break indicators,
474 * ignore overruns too (for real raw support).
475 */
476 if (termios->c_iflag & IGNPAR)
477 port->ignore_status_mask |= UART01x_RSR_OE;
478 }
479
480 /*
481 * Ignore all characters if CREAD is not set.
482 */
483 if ((termios->c_cflag & CREAD) == 0)
484 port->ignore_status_mask |= UART_DUMMY_RSR_RX;
485
486 /* first, disable everything */
487 old_cr = UART_GET_CR(port) & ~UART010_CR_MSIE;
488
489 if (UART_ENABLE_MS(port, termios->c_cflag))
490 old_cr |= UART010_CR_MSIE;
491
492 UART_PUT_CR(port, 0);
493
494 /* Set baud rate */
495 quot -= 1;
496 UART_PUT_LCRM(port, ((quot & 0xf00) >> 8));
497 UART_PUT_LCRL(port, (quot & 0xff));
498
499 /*
500 * ----------v----------v----------v----------v-----
501 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
502 * ----------^----------^----------^----------^-----
503 */
504 UART_PUT_LCRH(port, lcr_h);
505 UART_PUT_CR(port, old_cr);
506
507 spin_unlock_irqrestore(&port->lock, flags);
508}
509
510static const char *pl010_type(struct uart_port *port)
511{
512 return port->type == PORT_AMBA ? "AMBA" : NULL;
513}
514
515/*
516 * Release the memory region(s) being used by 'port'
517 */
518static void pl010_release_port(struct uart_port *port)
519{
520 release_mem_region(port->mapbase, UART_PORT_SIZE);
521}
522
523/*
524 * Request the memory region(s) being used by 'port'
525 */
526static int pl010_request_port(struct uart_port *port)
527{
528 return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
529 != NULL ? 0 : -EBUSY;
530}
531
532/*
533 * Configure/autoconfigure the port.
534 */
535static void pl010_config_port(struct uart_port *port, int flags)
536{
537 if (flags & UART_CONFIG_TYPE) {
538 port->type = PORT_AMBA;
539 pl010_request_port(port);
540 }
541}
542
543/*
544 * verify the new serial_struct (for TIOCSSERIAL).
545 */
546static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
547{
548 int ret = 0;
549 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
550 ret = -EINVAL;
551 if (ser->irq < 0 || ser->irq >= NR_IRQS)
552 ret = -EINVAL;
553 if (ser->baud_base < 9600)
554 ret = -EINVAL;
555 return ret;
556}
557
558static struct uart_ops amba_pl010_pops = {
559 .tx_empty = pl010_tx_empty,
560 .set_mctrl = pl010_set_mctrl,
561 .get_mctrl = pl010_get_mctrl,
562 .stop_tx = pl010_stop_tx,
563 .start_tx = pl010_start_tx,
564 .stop_rx = pl010_stop_rx,
565 .enable_ms = pl010_enable_ms,
566 .break_ctl = pl010_break_ctl,
567 .startup = pl010_startup,
568 .shutdown = pl010_shutdown,
569 .set_termios = pl010_set_termios,
570 .type = pl010_type,
571 .release_port = pl010_release_port,
572 .request_port = pl010_request_port,
573 .config_port = pl010_config_port,
574 .verify_port = pl010_verify_port,
575};
576
577static struct uart_amba_port amba_ports[UART_NR] = {
578 {
579 .port = {
580 .membase = (void *)IO_ADDRESS(INTEGRATOR_UART0_BASE),
581 .mapbase = INTEGRATOR_UART0_BASE,
582 .iotype = SERIAL_IO_MEM,
583 .irq = IRQ_UARTINT0,
584 .uartclk = 14745600,
585 .fifosize = 16,
586 .ops = &amba_pl010_pops,
587 .flags = ASYNC_BOOT_AUTOCONF,
588 .line = 0,
589 },
590 .dtr_mask = 1 << 5,
591 .rts_mask = 1 << 4,
592 },
593 {
594 .port = {
595 .membase = (void *)IO_ADDRESS(INTEGRATOR_UART1_BASE),
596 .mapbase = INTEGRATOR_UART1_BASE,
597 .iotype = SERIAL_IO_MEM,
598 .irq = IRQ_UARTINT1,
599 .uartclk = 14745600,
600 .fifosize = 16,
601 .ops = &amba_pl010_pops,
602 .flags = ASYNC_BOOT_AUTOCONF,
603 .line = 1,
604 },
605 .dtr_mask = 1 << 7,
606 .rts_mask = 1 << 6,
607 }
608};
609
610#ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
611
612static void
613pl010_console_write(struct console *co, const char *s, unsigned int count)
614{
615 struct uart_port *port = &amba_ports[co->index].port;
616 unsigned int status, old_cr;
617 int i;
618
619 /*
620 * First save the CR then disable the interrupts
621 */
622 old_cr = UART_GET_CR(port);
623 UART_PUT_CR(port, UART01x_CR_UARTEN);
624
625 /*
626 * Now, do each character
627 */
628 for (i = 0; i < count; i++) {
629 do {
630 status = UART_GET_FR(port);
631 } while (!UART_TX_READY(status));
632 UART_PUT_CHAR(port, s[i]);
633 if (s[i] == '\n') {
634 do {
635 status = UART_GET_FR(port);
636 } while (!UART_TX_READY(status));
637 UART_PUT_CHAR(port, '\r');
638 }
639 }
640
641 /*
642 * Finally, wait for transmitter to become empty
643 * and restore the TCR
644 */
645 do {
646 status = UART_GET_FR(port);
647 } while (status & UART01x_FR_BUSY);
648 UART_PUT_CR(port, old_cr);
649}
650
651static void __init
652pl010_console_get_options(struct uart_port *port, int *baud,
653 int *parity, int *bits)
654{
655 if (UART_GET_CR(port) & UART01x_CR_UARTEN) {
656 unsigned int lcr_h, quot;
657 lcr_h = UART_GET_LCRH(port);
658
659 *parity = 'n';
660 if (lcr_h & UART01x_LCRH_PEN) {
661 if (lcr_h & UART01x_LCRH_EPS)
662 *parity = 'e';
663 else
664 *parity = 'o';
665 }
666
667 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
668 *bits = 7;
669 else
670 *bits = 8;
671
672 quot = UART_GET_LCRL(port) | UART_GET_LCRM(port) << 8;
673 *baud = port->uartclk / (16 * (quot + 1));
674 }
675}
676
677static int __init pl010_console_setup(struct console *co, char *options)
678{
679 struct uart_port *port;
680 int baud = 38400;
681 int bits = 8;
682 int parity = 'n';
683 int flow = 'n';
684
685 /*
686 * Check whether an invalid uart number has been specified, and
687 * if so, search for the first available port that does have
688 * console support.
689 */
690 if (co->index >= UART_NR)
691 co->index = 0;
692 port = &amba_ports[co->index].port;
693
694 if (options)
695 uart_parse_options(options, &baud, &parity, &bits, &flow);
696 else
697 pl010_console_get_options(port, &baud, &parity, &bits);
698
699 return uart_set_options(port, co, baud, parity, bits, flow);
700}
701
702extern struct uart_driver amba_reg;
703static struct console amba_console = {
704 .name = "ttyAM",
705 .write = pl010_console_write,
706 .device = uart_console_device,
707 .setup = pl010_console_setup,
708 .flags = CON_PRINTBUFFER,
709 .index = -1,
710 .data = &amba_reg,
711};
712
713static int __init amba_console_init(void)
714{
715 /*
716 * All port initializations are done statically
717 */
718 register_console(&amba_console);
719 return 0;
720}
721console_initcall(amba_console_init);
722
723static int __init amba_late_console_init(void)
724{
725 if (!(amba_console.flags & CON_ENABLED))
726 register_console(&amba_console);
727 return 0;
728}
729late_initcall(amba_late_console_init);
730
731#define AMBA_CONSOLE &amba_console
732#else
733#define AMBA_CONSOLE NULL
734#endif
735
736static struct uart_driver amba_reg = {
737 .owner = THIS_MODULE,
738 .driver_name = "ttyAM",
739 .dev_name = "ttyAM",
740 .major = SERIAL_AMBA_MAJOR,
741 .minor = SERIAL_AMBA_MINOR,
742 .nr = UART_NR,
743 .cons = AMBA_CONSOLE,
744};
745
746static int pl010_probe(struct amba_device *dev, void *id)
747{
748 int i;
749
750 for (i = 0; i < UART_NR; i++) {
751 if (amba_ports[i].port.mapbase != dev->res.start)
752 continue;
753
754 amba_ports[i].port.dev = &dev->dev;
755 uart_add_one_port(&amba_reg, &amba_ports[i].port);
756 amba_set_drvdata(dev, &amba_ports[i]);
757 break;
758 }
759
760 return 0;
761}
762
763static int pl010_remove(struct amba_device *dev)
764{
765 struct uart_amba_port *uap = amba_get_drvdata(dev);
766
767 if (uap)
768 uart_remove_one_port(&amba_reg, &uap->port);
769
770 amba_set_drvdata(dev, NULL);
771
772 return 0;
773}
774
775static int pl010_suspend(struct amba_device *dev, u32 state)
776{
777 struct uart_amba_port *uap = amba_get_drvdata(dev);
778
779 if (uap)
780 uart_suspend_port(&amba_reg, &uap->port);
781
782 return 0;
783}
784
785static int pl010_resume(struct amba_device *dev)
786{
787 struct uart_amba_port *uap = amba_get_drvdata(dev);
788
789 if (uap)
790 uart_resume_port(&amba_reg, &uap->port);
791
792 return 0;
793}
794
795static struct amba_id pl010_ids[] __initdata = {
796 {
797 .id = 0x00041010,
798 .mask = 0x000fffff,
799 },
800 { 0, 0 },
801};
802
803static struct amba_driver pl010_driver = {
804 .drv = {
805 .name = "uart-pl010",
806 },
807 .id_table = pl010_ids,
808 .probe = pl010_probe,
809 .remove = pl010_remove,
810 .suspend = pl010_suspend,
811 .resume = pl010_resume,
812};
813
814static int __init pl010_init(void)
815{
816 int ret;
817
818 printk(KERN_INFO "Serial: AMBA driver $Revision: 1.41 $\n");
819
820 ret = uart_register_driver(&amba_reg);
821 if (ret == 0) {
822 ret = amba_driver_register(&pl010_driver);
823 if (ret)
824 uart_unregister_driver(&amba_reg);
825 }
826 return ret;
827}
828
829static void __exit pl010_exit(void)
830{
831 amba_driver_unregister(&pl010_driver);
832 uart_unregister_driver(&amba_reg);
833}
834
835module_init(pl010_init);
836module_exit(pl010_exit);
837
838MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
839MODULE_DESCRIPTION("ARM AMBA serial port driver $Revision: 1.41 $");
840MODULE_LICENSE("GPL");
diff --git a/drivers/serial/amba-pl011.c b/drivers/serial/amba-pl011.c
new file mode 100644
index 000000000000..ff658a830f34
--- /dev/null
+++ b/drivers/serial/amba-pl011.c
@@ -0,0 +1,869 @@
1/*
2 * linux/drivers/char/amba.c
3 *
4 * Driver for AMBA serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * $Id: amba.c,v 1.41 2002/07/28 10:03:27 rmk Exp $
26 *
27 * This is a generic driver for ARM AMBA-type serial ports. They
28 * have a lot of 16550-like features, but are not register compatible.
29 * Note that although they do have CTS, DCD and DSR inputs, they do
30 * not have an RI input, nor do they have DTR or RTS outputs. If
31 * required, these have to be supplied via some other means (eg, GPIO)
32 * and hooked into this driver.
33 */
34#include <linux/config.h>
35
36#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
37#define SUPPORT_SYSRQ
38#endif
39
40#include <linux/module.h>
41#include <linux/ioport.h>
42#include <linux/init.h>
43#include <linux/console.h>
44#include <linux/sysrq.h>
45#include <linux/device.h>
46#include <linux/tty.h>
47#include <linux/tty_flip.h>
48#include <linux/serial_core.h>
49#include <linux/serial.h>
50
51#include <asm/io.h>
52#include <asm/irq.h>
53#include <asm/hardware/amba.h>
54#include <asm/hardware/clock.h>
55#include <asm/hardware/amba_serial.h>
56
57#define UART_NR 14
58
59#define SERIAL_AMBA_MAJOR 204
60#define SERIAL_AMBA_MINOR 64
61#define SERIAL_AMBA_NR UART_NR
62
63#define AMBA_ISR_PASS_LIMIT 256
64
65#define UART_DUMMY_RSR_RX 256
66
67/*
68 * We wrap our port structure around the generic uart_port.
69 */
70struct uart_amba_port {
71 struct uart_port port;
72 struct clk *clk;
73 unsigned int im; /* interrupt mask */
74 unsigned int old_status;
75};
76
77static void pl011_stop_tx(struct uart_port *port, unsigned int tty_stop)
78{
79 struct uart_amba_port *uap = (struct uart_amba_port *)port;
80
81 uap->im &= ~UART011_TXIM;
82 writew(uap->im, uap->port.membase + UART011_IMSC);
83}
84
85static void pl011_start_tx(struct uart_port *port, unsigned int tty_start)
86{
87 struct uart_amba_port *uap = (struct uart_amba_port *)port;
88
89 uap->im |= UART011_TXIM;
90 writew(uap->im, uap->port.membase + UART011_IMSC);
91}
92
93static void pl011_stop_rx(struct uart_port *port)
94{
95 struct uart_amba_port *uap = (struct uart_amba_port *)port;
96
97 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
98 UART011_PEIM|UART011_BEIM|UART011_OEIM);
99 writew(uap->im, uap->port.membase + UART011_IMSC);
100}
101
102static void pl011_enable_ms(struct uart_port *port)
103{
104 struct uart_amba_port *uap = (struct uart_amba_port *)port;
105
106 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
107 writew(uap->im, uap->port.membase + UART011_IMSC);
108}
109
110static void
111#ifdef SUPPORT_SYSRQ
112pl011_rx_chars(struct uart_amba_port *uap, struct pt_regs *regs)
113#else
114pl011_rx_chars(struct uart_amba_port *uap)
115#endif
116{
117 struct tty_struct *tty = uap->port.info->tty;
118 unsigned int status, ch, flag, rsr, max_count = 256;
119
120 status = readw(uap->port.membase + UART01x_FR);
121 while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
122 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
123 if (tty->low_latency)
124 tty_flip_buffer_push(tty);
125 /*
126 * If this failed then we will throw away the
127 * bytes but must do so to clear interrupts
128 */
129 }
130
131 ch = readw(uap->port.membase + UART01x_DR);
132 flag = TTY_NORMAL;
133 uap->port.icount.rx++;
134
135 /*
136 * Note that the error handling code is
137 * out of the main execution path
138 */
139 rsr = readw(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX;
140 if (rsr & UART01x_RSR_ANY) {
141 if (rsr & UART01x_RSR_BE) {
142 rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
143 uap->port.icount.brk++;
144 if (uart_handle_break(&uap->port))
145 goto ignore_char;
146 } else if (rsr & UART01x_RSR_PE)
147 uap->port.icount.parity++;
148 else if (rsr & UART01x_RSR_FE)
149 uap->port.icount.frame++;
150 if (rsr & UART01x_RSR_OE)
151 uap->port.icount.overrun++;
152
153 rsr &= uap->port.read_status_mask;
154
155 if (rsr & UART01x_RSR_BE)
156 flag = TTY_BREAK;
157 else if (rsr & UART01x_RSR_PE)
158 flag = TTY_PARITY;
159 else if (rsr & UART01x_RSR_FE)
160 flag = TTY_FRAME;
161 }
162
163 if (uart_handle_sysrq_char(&uap->port, ch, regs))
164 goto ignore_char;
165
166 if ((rsr & uap->port.ignore_status_mask) == 0) {
167 tty_insert_flip_char(tty, ch, flag);
168 }
169 if ((rsr & UART01x_RSR_OE) &&
170 tty->flip.count < TTY_FLIPBUF_SIZE) {
171 /*
172 * Overrun is special, since it's reported
173 * immediately, and doesn't affect the current
174 * character
175 */
176 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
177 }
178 ignore_char:
179 status = readw(uap->port.membase + UART01x_FR);
180 }
181 tty_flip_buffer_push(tty);
182 return;
183}
184
185static void pl011_tx_chars(struct uart_amba_port *uap)
186{
187 struct circ_buf *xmit = &uap->port.info->xmit;
188 int count;
189
190 if (uap->port.x_char) {
191 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
192 uap->port.icount.tx++;
193 uap->port.x_char = 0;
194 return;
195 }
196 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
197 pl011_stop_tx(&uap->port, 0);
198 return;
199 }
200
201 count = uap->port.fifosize >> 1;
202 do {
203 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
204 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
205 uap->port.icount.tx++;
206 if (uart_circ_empty(xmit))
207 break;
208 } while (--count > 0);
209
210 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
211 uart_write_wakeup(&uap->port);
212
213 if (uart_circ_empty(xmit))
214 pl011_stop_tx(&uap->port, 0);
215}
216
217static void pl011_modem_status(struct uart_amba_port *uap)
218{
219 unsigned int status, delta;
220
221 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
222
223 delta = status ^ uap->old_status;
224 uap->old_status = status;
225
226 if (!delta)
227 return;
228
229 if (delta & UART01x_FR_DCD)
230 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
231
232 if (delta & UART01x_FR_DSR)
233 uap->port.icount.dsr++;
234
235 if (delta & UART01x_FR_CTS)
236 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
237
238 wake_up_interruptible(&uap->port.info->delta_msr_wait);
239}
240
241static irqreturn_t pl011_int(int irq, void *dev_id, struct pt_regs *regs)
242{
243 struct uart_amba_port *uap = dev_id;
244 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
245 int handled = 0;
246
247 spin_lock(&uap->port.lock);
248
249 status = readw(uap->port.membase + UART011_MIS);
250 if (status) {
251 do {
252 writew(status & ~(UART011_TXIS|UART011_RTIS|
253 UART011_RXIS),
254 uap->port.membase + UART011_ICR);
255
256 if (status & (UART011_RTIS|UART011_RXIS))
257#ifdef SUPPORT_SYSRQ
258 pl011_rx_chars(uap, regs);
259#else
260 pl011_rx_chars(uap);
261#endif
262 if (status & (UART011_DSRMIS|UART011_DCDMIS|
263 UART011_CTSMIS|UART011_RIMIS))
264 pl011_modem_status(uap);
265 if (status & UART011_TXIS)
266 pl011_tx_chars(uap);
267
268 if (pass_counter-- == 0)
269 break;
270
271 status = readw(uap->port.membase + UART011_MIS);
272 } while (status != 0);
273 handled = 1;
274 }
275
276 spin_unlock(&uap->port.lock);
277
278 return IRQ_RETVAL(handled);
279}
280
281static unsigned int pl01x_tx_empty(struct uart_port *port)
282{
283 struct uart_amba_port *uap = (struct uart_amba_port *)port;
284 unsigned int status = readw(uap->port.membase + UART01x_FR);
285 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
286}
287
288static unsigned int pl01x_get_mctrl(struct uart_port *port)
289{
290 struct uart_amba_port *uap = (struct uart_amba_port *)port;
291 unsigned int result = 0;
292 unsigned int status = readw(uap->port.membase + UART01x_FR);
293
294#define BIT(uartbit, tiocmbit) \
295 if (status & uartbit) \
296 result |= tiocmbit
297
298 BIT(UART01x_FR_DCD, TIOCM_CAR);
299 BIT(UART01x_FR_DSR, TIOCM_DSR);
300 BIT(UART01x_FR_CTS, TIOCM_CTS);
301 BIT(UART011_FR_RI, TIOCM_RNG);
302#undef BIT
303 return result;
304}
305
306static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
307{
308 struct uart_amba_port *uap = (struct uart_amba_port *)port;
309 unsigned int cr;
310
311 cr = readw(uap->port.membase + UART011_CR);
312
313#define BIT(tiocmbit, uartbit) \
314 if (mctrl & tiocmbit) \
315 cr |= uartbit; \
316 else \
317 cr &= ~uartbit
318
319 BIT(TIOCM_RTS, UART011_CR_RTS);
320 BIT(TIOCM_DTR, UART011_CR_DTR);
321 BIT(TIOCM_OUT1, UART011_CR_OUT1);
322 BIT(TIOCM_OUT2, UART011_CR_OUT2);
323 BIT(TIOCM_LOOP, UART011_CR_LBE);
324#undef BIT
325
326 writew(cr, uap->port.membase + UART011_CR);
327}
328
329static void pl011_break_ctl(struct uart_port *port, int break_state)
330{
331 struct uart_amba_port *uap = (struct uart_amba_port *)port;
332 unsigned long flags;
333 unsigned int lcr_h;
334
335 spin_lock_irqsave(&uap->port.lock, flags);
336 lcr_h = readw(uap->port.membase + UART011_LCRH);
337 if (break_state == -1)
338 lcr_h |= UART01x_LCRH_BRK;
339 else
340 lcr_h &= ~UART01x_LCRH_BRK;
341 writew(lcr_h, uap->port.membase + UART011_LCRH);
342 spin_unlock_irqrestore(&uap->port.lock, flags);
343}
344
345static int pl011_startup(struct uart_port *port)
346{
347 struct uart_amba_port *uap = (struct uart_amba_port *)port;
348 unsigned int cr;
349 int retval;
350
351 /*
352 * Try to enable the clock producer.
353 */
354 retval = clk_enable(uap->clk);
355 if (retval)
356 goto out;
357
358 uap->port.uartclk = clk_get_rate(uap->clk);
359
360 /*
361 * Allocate the IRQ
362 */
363 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
364 if (retval)
365 goto clk_dis;
366
367 writew(UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
368 uap->port.membase + UART011_IFLS);
369
370 /*
371 * Provoke TX FIFO interrupt into asserting.
372 */
373 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
374 writew(cr, uap->port.membase + UART011_CR);
375 writew(0, uap->port.membase + UART011_FBRD);
376 writew(1, uap->port.membase + UART011_IBRD);
377 writew(0, uap->port.membase + UART011_LCRH);
378 writew(0, uap->port.membase + UART01x_DR);
379 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
380 barrier();
381
382 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
383 writew(cr, uap->port.membase + UART011_CR);
384
385 /*
386 * initialise the old status of the modem signals
387 */
388 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
389
390 /*
391 * Finally, enable interrupts
392 */
393 spin_lock_irq(&uap->port.lock);
394 uap->im = UART011_RXIM | UART011_RTIM;
395 writew(uap->im, uap->port.membase + UART011_IMSC);
396 spin_unlock_irq(&uap->port.lock);
397
398 return 0;
399
400 clk_dis:
401 clk_disable(uap->clk);
402 out:
403 return retval;
404}
405
406static void pl011_shutdown(struct uart_port *port)
407{
408 struct uart_amba_port *uap = (struct uart_amba_port *)port;
409 unsigned long val;
410
411 /*
412 * disable all interrupts
413 */
414 spin_lock_irq(&uap->port.lock);
415 uap->im = 0;
416 writew(uap->im, uap->port.membase + UART011_IMSC);
417 writew(0xffff, uap->port.membase + UART011_ICR);
418 spin_unlock_irq(&uap->port.lock);
419
420 /*
421 * Free the interrupt
422 */
423 free_irq(uap->port.irq, uap);
424
425 /*
426 * disable the port
427 */
428 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
429
430 /*
431 * disable break condition and fifos
432 */
433 val = readw(uap->port.membase + UART011_LCRH);
434 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
435 writew(val, uap->port.membase + UART011_LCRH);
436
437 /*
438 * Shut down the clock producer
439 */
440 clk_disable(uap->clk);
441}
442
443static void
444pl011_set_termios(struct uart_port *port, struct termios *termios,
445 struct termios *old)
446{
447 unsigned int lcr_h, old_cr;
448 unsigned long flags;
449 unsigned int baud, quot;
450
451 /*
452 * Ask the core to calculate the divisor for us.
453 */
454 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
455 quot = port->uartclk * 4 / baud;
456
457 switch (termios->c_cflag & CSIZE) {
458 case CS5:
459 lcr_h = UART01x_LCRH_WLEN_5;
460 break;
461 case CS6:
462 lcr_h = UART01x_LCRH_WLEN_6;
463 break;
464 case CS7:
465 lcr_h = UART01x_LCRH_WLEN_7;
466 break;
467 default: // CS8
468 lcr_h = UART01x_LCRH_WLEN_8;
469 break;
470 }
471 if (termios->c_cflag & CSTOPB)
472 lcr_h |= UART01x_LCRH_STP2;
473 if (termios->c_cflag & PARENB) {
474 lcr_h |= UART01x_LCRH_PEN;
475 if (!(termios->c_cflag & PARODD))
476 lcr_h |= UART01x_LCRH_EPS;
477 }
478 if (port->fifosize > 1)
479 lcr_h |= UART01x_LCRH_FEN;
480
481 spin_lock_irqsave(&port->lock, flags);
482
483 /*
484 * Update the per-port timeout.
485 */
486 uart_update_timeout(port, termios->c_cflag, baud);
487
488 port->read_status_mask = UART01x_RSR_OE;
489 if (termios->c_iflag & INPCK)
490 port->read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
491 if (termios->c_iflag & (BRKINT | PARMRK))
492 port->read_status_mask |= UART01x_RSR_BE;
493
494 /*
495 * Characters to ignore
496 */
497 port->ignore_status_mask = 0;
498 if (termios->c_iflag & IGNPAR)
499 port->ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
500 if (termios->c_iflag & IGNBRK) {
501 port->ignore_status_mask |= UART01x_RSR_BE;
502 /*
503 * If we're ignoring parity and break indicators,
504 * ignore overruns too (for real raw support).
505 */
506 if (termios->c_iflag & IGNPAR)
507 port->ignore_status_mask |= UART01x_RSR_OE;
508 }
509
510 /*
511 * Ignore all characters if CREAD is not set.
512 */
513 if ((termios->c_cflag & CREAD) == 0)
514 port->ignore_status_mask |= UART_DUMMY_RSR_RX;
515
516 if (UART_ENABLE_MS(port, termios->c_cflag))
517 pl011_enable_ms(port);
518
519 /* first, disable everything */
520 old_cr = readw(port->membase + UART011_CR);
521 writew(0, port->membase + UART011_CR);
522
523 /* Set baud rate */
524 writew(quot & 0x3f, port->membase + UART011_FBRD);
525 writew(quot >> 6, port->membase + UART011_IBRD);
526
527 /*
528 * ----------v----------v----------v----------v-----
529 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
530 * ----------^----------^----------^----------^-----
531 */
532 writew(lcr_h, port->membase + UART011_LCRH);
533 writew(old_cr, port->membase + UART011_CR);
534
535 spin_unlock_irqrestore(&port->lock, flags);
536}
537
538static const char *pl011_type(struct uart_port *port)
539{
540 return port->type == PORT_AMBA ? "AMBA/PL011" : NULL;
541}
542
543/*
544 * Release the memory region(s) being used by 'port'
545 */
546static void pl010_release_port(struct uart_port *port)
547{
548 release_mem_region(port->mapbase, SZ_4K);
549}
550
551/*
552 * Request the memory region(s) being used by 'port'
553 */
554static int pl010_request_port(struct uart_port *port)
555{
556 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
557 != NULL ? 0 : -EBUSY;
558}
559
560/*
561 * Configure/autoconfigure the port.
562 */
563static void pl010_config_port(struct uart_port *port, int flags)
564{
565 if (flags & UART_CONFIG_TYPE) {
566 port->type = PORT_AMBA;
567 pl010_request_port(port);
568 }
569}
570
571/*
572 * verify the new serial_struct (for TIOCSSERIAL).
573 */
574static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
575{
576 int ret = 0;
577 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
578 ret = -EINVAL;
579 if (ser->irq < 0 || ser->irq >= NR_IRQS)
580 ret = -EINVAL;
581 if (ser->baud_base < 9600)
582 ret = -EINVAL;
583 return ret;
584}
585
586static struct uart_ops amba_pl011_pops = {
587 .tx_empty = pl01x_tx_empty,
588 .set_mctrl = pl011_set_mctrl,
589 .get_mctrl = pl01x_get_mctrl,
590 .stop_tx = pl011_stop_tx,
591 .start_tx = pl011_start_tx,
592 .stop_rx = pl011_stop_rx,
593 .enable_ms = pl011_enable_ms,
594 .break_ctl = pl011_break_ctl,
595 .startup = pl011_startup,
596 .shutdown = pl011_shutdown,
597 .set_termios = pl011_set_termios,
598 .type = pl011_type,
599 .release_port = pl010_release_port,
600 .request_port = pl010_request_port,
601 .config_port = pl010_config_port,
602 .verify_port = pl010_verify_port,
603};
604
605static struct uart_amba_port *amba_ports[UART_NR];
606
607#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
608
609static inline void
610pl011_console_write_char(struct uart_amba_port *uap, char ch)
611{
612 unsigned int status;
613
614 do {
615 status = readw(uap->port.membase + UART01x_FR);
616 } while (status & UART01x_FR_TXFF);
617 writew(ch, uap->port.membase + UART01x_DR);
618}
619
620static void
621pl011_console_write(struct console *co, const char *s, unsigned int count)
622{
623 struct uart_amba_port *uap = amba_ports[co->index];
624 unsigned int status, old_cr, new_cr;
625 int i;
626
627 clk_enable(uap->clk);
628
629 /*
630 * First save the CR then disable the interrupts
631 */
632 old_cr = readw(uap->port.membase + UART011_CR);
633 new_cr = old_cr & ~UART011_CR_CTSEN;
634 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
635 writew(new_cr, uap->port.membase + UART011_CR);
636
637 /*
638 * Now, do each character
639 */
640 for (i = 0; i < count; i++) {
641 pl011_console_write_char(uap, s[i]);
642 if (s[i] == '\n')
643 pl011_console_write_char(uap, '\r');
644 }
645
646 /*
647 * Finally, wait for transmitter to become empty
648 * and restore the TCR
649 */
650 do {
651 status = readw(uap->port.membase + UART01x_FR);
652 } while (status & UART01x_FR_BUSY);
653 writew(old_cr, uap->port.membase + UART011_CR);
654
655 clk_disable(uap->clk);
656}
657
658static void __init
659pl011_console_get_options(struct uart_amba_port *uap, int *baud,
660 int *parity, int *bits)
661{
662 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
663 unsigned int lcr_h, ibrd, fbrd;
664
665 lcr_h = readw(uap->port.membase + UART011_LCRH);
666
667 *parity = 'n';
668 if (lcr_h & UART01x_LCRH_PEN) {
669 if (lcr_h & UART01x_LCRH_EPS)
670 *parity = 'e';
671 else
672 *parity = 'o';
673 }
674
675 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
676 *bits = 7;
677 else
678 *bits = 8;
679
680 ibrd = readw(uap->port.membase + UART011_IBRD);
681 fbrd = readw(uap->port.membase + UART011_FBRD);
682
683 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
684 }
685}
686
687static int __init pl011_console_setup(struct console *co, char *options)
688{
689 struct uart_amba_port *uap;
690 int baud = 38400;
691 int bits = 8;
692 int parity = 'n';
693 int flow = 'n';
694
695 /*
696 * Check whether an invalid uart number has been specified, and
697 * if so, search for the first available port that does have
698 * console support.
699 */
700 if (co->index >= UART_NR)
701 co->index = 0;
702 uap = amba_ports[co->index];
703
704 uap->port.uartclk = clk_get_rate(uap->clk);
705
706 if (options)
707 uart_parse_options(options, &baud, &parity, &bits, &flow);
708 else
709 pl011_console_get_options(uap, &baud, &parity, &bits);
710
711 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
712}
713
714extern struct uart_driver amba_reg;
715static struct console amba_console = {
716 .name = "ttyAMA",
717 .write = pl011_console_write,
718 .device = uart_console_device,
719 .setup = pl011_console_setup,
720 .flags = CON_PRINTBUFFER,
721 .index = -1,
722 .data = &amba_reg,
723};
724
725#define AMBA_CONSOLE (&amba_console)
726#else
727#define AMBA_CONSOLE NULL
728#endif
729
730static struct uart_driver amba_reg = {
731 .owner = THIS_MODULE,
732 .driver_name = "ttyAMA",
733 .dev_name = "ttyAMA",
734 .major = SERIAL_AMBA_MAJOR,
735 .minor = SERIAL_AMBA_MINOR,
736 .nr = UART_NR,
737 .cons = AMBA_CONSOLE,
738};
739
740static int pl011_probe(struct amba_device *dev, void *id)
741{
742 struct uart_amba_port *uap;
743 void __iomem *base;
744 int i, ret;
745
746 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
747 if (amba_ports[i] == NULL)
748 break;
749
750 if (i == ARRAY_SIZE(amba_ports)) {
751 ret = -EBUSY;
752 goto out;
753 }
754
755 uap = kmalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
756 if (uap == NULL) {
757 ret = -ENOMEM;
758 goto out;
759 }
760
761 base = ioremap(dev->res.start, PAGE_SIZE);
762 if (!base) {
763 ret = -ENOMEM;
764 goto free;
765 }
766
767 memset(uap, 0, sizeof(struct uart_amba_port));
768 uap->clk = clk_get(&dev->dev, "UARTCLK");
769 if (IS_ERR(uap->clk)) {
770 ret = PTR_ERR(uap->clk);
771 goto unmap;
772 }
773
774 ret = clk_use(uap->clk);
775 if (ret)
776 goto putclk;
777
778 uap->port.dev = &dev->dev;
779 uap->port.mapbase = dev->res.start;
780 uap->port.membase = base;
781 uap->port.iotype = UPIO_MEM;
782 uap->port.irq = dev->irq[0];
783 uap->port.fifosize = 16;
784 uap->port.ops = &amba_pl011_pops;
785 uap->port.flags = UPF_BOOT_AUTOCONF;
786 uap->port.line = i;
787
788 amba_ports[i] = uap;
789
790 amba_set_drvdata(dev, uap);
791 ret = uart_add_one_port(&amba_reg, &uap->port);
792 if (ret) {
793 amba_set_drvdata(dev, NULL);
794 amba_ports[i] = NULL;
795 clk_unuse(uap->clk);
796 putclk:
797 clk_put(uap->clk);
798 unmap:
799 iounmap(base);
800 free:
801 kfree(uap);
802 }
803 out:
804 return ret;
805}
806
807static int pl011_remove(struct amba_device *dev)
808{
809 struct uart_amba_port *uap = amba_get_drvdata(dev);
810 int i;
811
812 amba_set_drvdata(dev, NULL);
813
814 uart_remove_one_port(&amba_reg, &uap->port);
815
816 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
817 if (amba_ports[i] == uap)
818 amba_ports[i] = NULL;
819
820 iounmap(uap->port.membase);
821 clk_unuse(uap->clk);
822 clk_put(uap->clk);
823 kfree(uap);
824 return 0;
825}
826
827static struct amba_id pl011_ids[] __initdata = {
828 {
829 .id = 0x00041011,
830 .mask = 0x000fffff,
831 },
832 { 0, 0 },
833};
834
835static struct amba_driver pl011_driver = {
836 .drv = {
837 .name = "uart-pl011",
838 },
839 .id_table = pl011_ids,
840 .probe = pl011_probe,
841 .remove = pl011_remove,
842};
843
844static int __init pl011_init(void)
845{
846 int ret;
847 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
848
849 ret = uart_register_driver(&amba_reg);
850 if (ret == 0) {
851 ret = amba_driver_register(&pl011_driver);
852 if (ret)
853 uart_unregister_driver(&amba_reg);
854 }
855 return ret;
856}
857
858static void __exit pl011_exit(void)
859{
860 amba_driver_unregister(&pl011_driver);
861 uart_unregister_driver(&amba_reg);
862}
863
864module_init(pl011_init);
865module_exit(pl011_exit);
866
867MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
868MODULE_DESCRIPTION("ARM AMBA serial port driver");
869MODULE_LICENSE("GPL");
diff --git a/drivers/serial/au1x00_uart.c b/drivers/serial/au1x00_uart.c
new file mode 100644
index 000000000000..b6d3d5034940
--- /dev/null
+++ b/drivers/serial/au1x00_uart.c
@@ -0,0 +1,1312 @@
1/*
2 * Driver for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * A note about mapbase / membase
14 *
15 * mapbase is the physical address of the IO port. Currently, we don't
16 * support this very well, and it may well be dropped from this driver
17 * in future. As such, mapbase should be NULL.
18 *
19 * membase is an 'ioremapped' cookie. This is compatible with the old
20 * serial.c driver, and is currently the preferred form.
21 */
22#include <linux/config.h>
23#include <linux/module.h>
24#include <linux/tty.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
29#include <linux/serial.h>
30#include <linux/serialP.h>
31#include <linux/delay.h>
32
33#include <asm/serial.h>
34#include <asm/io.h>
35#include <asm/irq.h>
36#include <asm/mach-au1x00/au1000.h>
37
38#if defined(CONFIG_SERIAL_AU1X00_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
39#define SUPPORT_SYSRQ
40#endif
41
42#include <linux/serial_core.h>
43#include "8250.h"
44
45/*
46 * Debugging.
47 */
48#if 0
49#define DEBUG_AUTOCONF(fmt...) printk(fmt)
50#else
51#define DEBUG_AUTOCONF(fmt...) do { } while (0)
52#endif
53
54#if 0
55#define DEBUG_INTR(fmt...) printk(fmt)
56#else
57#define DEBUG_INTR(fmt...) do { } while (0)
58#endif
59
60#define PASS_LIMIT 256
61
62/*
63 * We default to IRQ0 for the "no irq" hack. Some
64 * machine types want others as well - they're free
65 * to redefine this in their header file.
66 */
67#define is_real_interrupt(irq) ((irq) != 0)
68
69static struct old_serial_port old_serial_port[] = {
70 { .baud_base = 0,
71 .iomem_base = (u8 *)UART0_ADDR,
72 .irq = AU1000_UART0_INT,
73 .flags = STD_COM_FLAGS,
74 .iomem_reg_shift = 2,
75 }, {
76 .baud_base = 0,
77 .iomem_base = (u8 *)UART1_ADDR,
78 .irq = AU1000_UART1_INT,
79 .flags = STD_COM_FLAGS,
80 .iomem_reg_shift = 2
81 }, {
82 .baud_base = 0,
83 .iomem_base = (u8 *)UART2_ADDR,
84 .irq = AU1000_UART2_INT,
85 .flags = STD_COM_FLAGS,
86 .iomem_reg_shift = 2
87 }, {
88 .baud_base = 0,
89 .iomem_base = (u8 *)UART3_ADDR,
90 .irq = AU1000_UART3_INT,
91 .flags = STD_COM_FLAGS,
92 .iomem_reg_shift = 2
93 }
94};
95
96#define UART_NR ARRAY_SIZE(old_serial_port)
97
98struct uart_8250_port {
99 struct uart_port port;
100 struct timer_list timer; /* "no irq" timer */
101 struct list_head list; /* ports on this IRQ */
102 unsigned short rev;
103 unsigned char acr;
104 unsigned char ier;
105 unsigned char lcr;
106 unsigned char mcr_mask; /* mask of user bits */
107 unsigned char mcr_force; /* mask of forced bits */
108 unsigned char lsr_break_flag;
109
110 /*
111 * We provide a per-port pm hook.
112 */
113 void (*pm)(struct uart_port *port,
114 unsigned int state, unsigned int old);
115};
116
117struct irq_info {
118 spinlock_t lock;
119 struct list_head *head;
120};
121
122static struct irq_info irq_lists[NR_IRQS];
123
124/*
125 * Here we define the default xmit fifo size used for each type of UART.
126 */
127static const struct serial_uart_config uart_config[PORT_MAX_8250+1] = {
128 { "unknown", 1, 0 },
129 { "8250", 1, 0 },
130 { "16450", 1, 0 },
131 { "16550", 1, 0 },
132 /* PORT_16550A */
133 { "AU1X00_UART",16, UART_CLEAR_FIFO | UART_USE_FIFO },
134};
135
136static _INLINE_ unsigned int serial_in(struct uart_8250_port *up, int offset)
137{
138 return au_readl((unsigned long)up->port.membase + offset);
139}
140
141static _INLINE_ void
142serial_out(struct uart_8250_port *up, int offset, int value)
143{
144 au_writel(value, (unsigned long)up->port.membase + offset);
145}
146
147#define serial_inp(up, offset) serial_in(up, offset)
148#define serial_outp(up, offset, value) serial_out(up, offset, value)
149
150/*
151 * This routine is called by rs_init() to initialize a specific serial
152 * port. It determines what type of UART chip this serial port is
153 * using: 8250, 16450, 16550, 16550A. The important question is
154 * whether or not this UART is a 16550A or not, since this will
155 * determine whether or not we can use its FIFO features or not.
156 */
157static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
158{
159 unsigned char save_lcr, save_mcr;
160 unsigned long flags;
161
162 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
163 return;
164
165 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%08lx): ",
166 up->port.line, up->port.iobase, up->port.membase);
167
168 /*
169 * We really do need global IRQs disabled here - we're going to
170 * be frobbing the chips IRQ enable register to see if it exists.
171 */
172 spin_lock_irqsave(&up->port.lock, flags);
173// save_flags(flags); cli();
174
175 save_mcr = serial_in(up, UART_MCR);
176 save_lcr = serial_in(up, UART_LCR);
177
178 up->port.type = PORT_16550A;
179 serial_outp(up, UART_LCR, save_lcr);
180
181 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
182
183 if (up->port.type == PORT_UNKNOWN)
184 goto out;
185
186 /*
187 * Reset the UART.
188 */
189 serial_outp(up, UART_MCR, save_mcr);
190 serial_outp(up, UART_FCR, (UART_FCR_ENABLE_FIFO |
191 UART_FCR_CLEAR_RCVR |
192 UART_FCR_CLEAR_XMIT));
193 serial_outp(up, UART_FCR, 0);
194 (void)serial_in(up, UART_RX);
195 serial_outp(up, UART_IER, 0);
196
197 out:
198 spin_unlock_irqrestore(&up->port.lock, flags);
199// restore_flags(flags);
200 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
201}
202
203static void serial8250_stop_tx(struct uart_port *port, unsigned int tty_stop)
204{
205 struct uart_8250_port *up = (struct uart_8250_port *)port;
206
207 if (up->ier & UART_IER_THRI) {
208 up->ier &= ~UART_IER_THRI;
209 serial_out(up, UART_IER, up->ier);
210 }
211}
212
213static void serial8250_start_tx(struct uart_port *port, unsigned int tty_start)
214{
215 struct uart_8250_port *up = (struct uart_8250_port *)port;
216
217 if (!(up->ier & UART_IER_THRI)) {
218 up->ier |= UART_IER_THRI;
219 serial_out(up, UART_IER, up->ier);
220 }
221}
222
223static void serial8250_stop_rx(struct uart_port *port)
224{
225 struct uart_8250_port *up = (struct uart_8250_port *)port;
226
227 up->ier &= ~UART_IER_RLSI;
228 up->port.read_status_mask &= ~UART_LSR_DR;
229 serial_out(up, UART_IER, up->ier);
230}
231
232static void serial8250_enable_ms(struct uart_port *port)
233{
234 struct uart_8250_port *up = (struct uart_8250_port *)port;
235
236 up->ier |= UART_IER_MSI;
237 serial_out(up, UART_IER, up->ier);
238}
239
240static _INLINE_ void
241receive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs)
242{
243 struct tty_struct *tty = up->port.info->tty;
244 unsigned char ch;
245 int max_count = 256;
246
247 do {
248 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
249 tty->flip.work.func((void *)tty);
250 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
251 return; // if TTY_DONT_FLIP is set
252 }
253 ch = serial_inp(up, UART_RX);
254 *tty->flip.char_buf_ptr = ch;
255 *tty->flip.flag_buf_ptr = TTY_NORMAL;
256 up->port.icount.rx++;
257
258 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
259 UART_LSR_FE | UART_LSR_OE))) {
260 /*
261 * For statistics only
262 */
263 if (*status & UART_LSR_BI) {
264 *status &= ~(UART_LSR_FE | UART_LSR_PE);
265 up->port.icount.brk++;
266 /*
267 * We do the SysRQ and SAK checking
268 * here because otherwise the break
269 * may get masked by ignore_status_mask
270 * or read_status_mask.
271 */
272 if (uart_handle_break(&up->port))
273 goto ignore_char;
274 } else if (*status & UART_LSR_PE)
275 up->port.icount.parity++;
276 else if (*status & UART_LSR_FE)
277 up->port.icount.frame++;
278 if (*status & UART_LSR_OE)
279 up->port.icount.overrun++;
280
281 /*
282 * Mask off conditions which should be ingored.
283 */
284 *status &= up->port.read_status_mask;
285
286#ifdef CONFIG_SERIAL_AU1X00_CONSOLE
287 if (up->port.line == up->port.cons->index) {
288 /* Recover the break flag from console xmit */
289 *status |= up->lsr_break_flag;
290 up->lsr_break_flag = 0;
291 }
292#endif
293 if (*status & UART_LSR_BI) {
294 DEBUG_INTR("handling break....");
295 *tty->flip.flag_buf_ptr = TTY_BREAK;
296 } else if (*status & UART_LSR_PE)
297 *tty->flip.flag_buf_ptr = TTY_PARITY;
298 else if (*status & UART_LSR_FE)
299 *tty->flip.flag_buf_ptr = TTY_FRAME;
300 }
301 if (uart_handle_sysrq_char(&up->port, ch, regs))
302 goto ignore_char;
303 if ((*status & up->port.ignore_status_mask) == 0) {
304 tty->flip.flag_buf_ptr++;
305 tty->flip.char_buf_ptr++;
306 tty->flip.count++;
307 }
308 if ((*status & UART_LSR_OE) &&
309 tty->flip.count < TTY_FLIPBUF_SIZE) {
310 /*
311 * Overrun is special, since it's reported
312 * immediately, and doesn't affect the current
313 * character.
314 */
315 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
316 tty->flip.flag_buf_ptr++;
317 tty->flip.char_buf_ptr++;
318 tty->flip.count++;
319 }
320 ignore_char:
321 *status = serial_inp(up, UART_LSR);
322 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
323 spin_unlock(&up->port.lock);
324 tty_flip_buffer_push(tty);
325 spin_lock(&up->port.lock);
326}
327
328static _INLINE_ void transmit_chars(struct uart_8250_port *up)
329{
330 struct circ_buf *xmit = &up->port.info->xmit;
331 int count;
332
333 if (up->port.x_char) {
334 serial_outp(up, UART_TX, up->port.x_char);
335 up->port.icount.tx++;
336 up->port.x_char = 0;
337 return;
338 }
339 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
340 serial8250_stop_tx(&up->port, 0);
341 return;
342 }
343
344 count = up->port.fifosize;
345 do {
346 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
347 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
348 up->port.icount.tx++;
349 if (uart_circ_empty(xmit))
350 break;
351 } while (--count > 0);
352
353 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
354 uart_write_wakeup(&up->port);
355
356 DEBUG_INTR("THRE...");
357
358 if (uart_circ_empty(xmit))
359 serial8250_stop_tx(&up->port, 0);
360}
361
362static _INLINE_ void check_modem_status(struct uart_8250_port *up)
363{
364 int status;
365
366 status = serial_in(up, UART_MSR);
367
368 if ((status & UART_MSR_ANY_DELTA) == 0)
369 return;
370
371 if (status & UART_MSR_TERI)
372 up->port.icount.rng++;
373 if (status & UART_MSR_DDSR)
374 up->port.icount.dsr++;
375 if (status & UART_MSR_DDCD)
376 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
377 if (status & UART_MSR_DCTS)
378 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
379
380 wake_up_interruptible(&up->port.info->delta_msr_wait);
381}
382
383/*
384 * This handles the interrupt from one port.
385 */
386static inline void
387serial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs)
388{
389 unsigned int status = serial_inp(up, UART_LSR);
390
391 DEBUG_INTR("status = %x...", status);
392
393 if (status & UART_LSR_DR)
394 receive_chars(up, &status, regs);
395 check_modem_status(up);
396 if (status & UART_LSR_THRE)
397 transmit_chars(up);
398}
399
400/*
401 * This is the serial driver's interrupt routine.
402 *
403 * Arjan thinks the old way was overly complex, so it got simplified.
404 * Alan disagrees, saying that need the complexity to handle the weird
405 * nature of ISA shared interrupts. (This is a special exception.)
406 *
407 * In order to handle ISA shared interrupts properly, we need to check
408 * that all ports have been serviced, and therefore the ISA interrupt
409 * line has been de-asserted.
410 *
411 * This means we need to loop through all ports. checking that they
412 * don't have an interrupt pending.
413 */
414static irqreturn_t serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs)
415{
416 struct irq_info *i = dev_id;
417 struct list_head *l, *end = NULL;
418 int pass_counter = 0;
419
420 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
421
422 spin_lock(&i->lock);
423
424 l = i->head;
425 do {
426 struct uart_8250_port *up;
427 unsigned int iir;
428
429 up = list_entry(l, struct uart_8250_port, list);
430
431 iir = serial_in(up, UART_IIR);
432 if (!(iir & UART_IIR_NO_INT)) {
433 spin_lock(&up->port.lock);
434 serial8250_handle_port(up, regs);
435 spin_unlock(&up->port.lock);
436
437 end = NULL;
438 } else if (end == NULL)
439 end = l;
440
441 l = l->next;
442
443 if (l == i->head && pass_counter++ > PASS_LIMIT) {
444 /* If we hit this, we're dead. */
445 printk(KERN_ERR "serial8250: too much work for "
446 "irq%d\n", irq);
447 break;
448 }
449 } while (l != end);
450
451 spin_unlock(&i->lock);
452
453 DEBUG_INTR("end.\n");
454 /* FIXME! Was it really ours? */
455 return IRQ_HANDLED;
456}
457
458/*
459 * To support ISA shared interrupts, we need to have one interrupt
460 * handler that ensures that the IRQ line has been deasserted
461 * before returning. Failing to do this will result in the IRQ
462 * line being stuck active, and, since ISA irqs are edge triggered,
463 * no more IRQs will be seen.
464 */
465static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
466{
467 spin_lock_irq(&i->lock);
468
469 if (!list_empty(i->head)) {
470 if (i->head == &up->list)
471 i->head = i->head->next;
472 list_del(&up->list);
473 } else {
474 BUG_ON(i->head != &up->list);
475 i->head = NULL;
476 }
477
478 spin_unlock_irq(&i->lock);
479}
480
481static int serial_link_irq_chain(struct uart_8250_port *up)
482{
483 struct irq_info *i = irq_lists + up->port.irq;
484 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
485
486 spin_lock_irq(&i->lock);
487
488 if (i->head) {
489 list_add(&up->list, i->head);
490 spin_unlock_irq(&i->lock);
491
492 ret = 0;
493 } else {
494 INIT_LIST_HEAD(&up->list);
495 i->head = &up->list;
496 spin_unlock_irq(&i->lock);
497
498 ret = request_irq(up->port.irq, serial8250_interrupt,
499 irq_flags, "serial", i);
500 if (ret < 0)
501 serial_do_unlink(i, up);
502 }
503
504 return ret;
505}
506
507static void serial_unlink_irq_chain(struct uart_8250_port *up)
508{
509 struct irq_info *i = irq_lists + up->port.irq;
510
511 BUG_ON(i->head == NULL);
512
513 if (list_empty(i->head))
514 free_irq(up->port.irq, i);
515
516 serial_do_unlink(i, up);
517}
518
519/*
520 * This function is used to handle ports that do not have an
521 * interrupt. This doesn't work very well for 16450's, but gives
522 * barely passable results for a 16550A. (Although at the expense
523 * of much CPU overhead).
524 */
525static void serial8250_timeout(unsigned long data)
526{
527 struct uart_8250_port *up = (struct uart_8250_port *)data;
528 unsigned int timeout;
529 unsigned int iir;
530
531 iir = serial_in(up, UART_IIR);
532 if (!(iir & UART_IIR_NO_INT)) {
533 spin_lock(&up->port.lock);
534 serial8250_handle_port(up, NULL);
535 spin_unlock(&up->port.lock);
536 }
537
538 timeout = up->port.timeout;
539 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
540 mod_timer(&up->timer, jiffies + timeout);
541}
542
543static unsigned int serial8250_tx_empty(struct uart_port *port)
544{
545 struct uart_8250_port *up = (struct uart_8250_port *)port;
546 unsigned long flags;
547 unsigned int ret;
548
549 spin_lock_irqsave(&up->port.lock, flags);
550 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
551 spin_unlock_irqrestore(&up->port.lock, flags);
552
553 return ret;
554}
555
556static unsigned int serial8250_get_mctrl(struct uart_port *port)
557{
558 struct uart_8250_port *up = (struct uart_8250_port *)port;
559 unsigned long flags;
560 unsigned char status;
561 unsigned int ret;
562
563 spin_lock_irqsave(&up->port.lock, flags);
564 status = serial_in(up, UART_MSR);
565 spin_unlock_irqrestore(&up->port.lock, flags);
566
567 ret = 0;
568 if (status & UART_MSR_DCD)
569 ret |= TIOCM_CAR;
570 if (status & UART_MSR_RI)
571 ret |= TIOCM_RNG;
572 if (status & UART_MSR_DSR)
573 ret |= TIOCM_DSR;
574 if (status & UART_MSR_CTS)
575 ret |= TIOCM_CTS;
576 return ret;
577}
578
579static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
580{
581 struct uart_8250_port *up = (struct uart_8250_port *)port;
582 unsigned char mcr = 0;
583
584 if (mctrl & TIOCM_RTS)
585 mcr |= UART_MCR_RTS;
586 if (mctrl & TIOCM_DTR)
587 mcr |= UART_MCR_DTR;
588 if (mctrl & TIOCM_OUT1)
589 mcr |= UART_MCR_OUT1;
590 if (mctrl & TIOCM_OUT2)
591 mcr |= UART_MCR_OUT2;
592 if (mctrl & TIOCM_LOOP)
593 mcr |= UART_MCR_LOOP;
594
595 mcr = (mcr & up->mcr_mask) | up->mcr_force;
596
597 serial_out(up, UART_MCR, mcr);
598}
599
600static void serial8250_break_ctl(struct uart_port *port, int break_state)
601{
602 struct uart_8250_port *up = (struct uart_8250_port *)port;
603 unsigned long flags;
604
605 spin_lock_irqsave(&up->port.lock, flags);
606 if (break_state == -1)
607 up->lcr |= UART_LCR_SBC;
608 else
609 up->lcr &= ~UART_LCR_SBC;
610 serial_out(up, UART_LCR, up->lcr);
611 spin_unlock_irqrestore(&up->port.lock, flags);
612}
613
614static int serial8250_startup(struct uart_port *port)
615{
616 struct uart_8250_port *up = (struct uart_8250_port *)port;
617 unsigned long flags;
618 int retval;
619
620 /*
621 * Clear the FIFO buffers and disable them.
622 * (they will be reeanbled in set_termios())
623 */
624 if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
625 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
626 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
627 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
628 serial_outp(up, UART_FCR, 0);
629 }
630
631 /*
632 * Clear the interrupt registers.
633 */
634 (void) serial_inp(up, UART_LSR);
635 (void) serial_inp(up, UART_RX);
636 (void) serial_inp(up, UART_IIR);
637 (void) serial_inp(up, UART_MSR);
638
639 /*
640 * At this point, there's no way the LSR could still be 0xff;
641 * if it is, then bail out, because there's likely no UART
642 * here.
643 */
644 if (!(up->port.flags & UPF_BUGGY_UART) &&
645 (serial_inp(up, UART_LSR) == 0xff)) {
646 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
647 return -ENODEV;
648 }
649
650 retval = serial_link_irq_chain(up);
651 if (retval)
652 return retval;
653
654 /*
655 * Now, initialize the UART
656 */
657 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
658
659 spin_lock_irqsave(&up->port.lock, flags);
660 if (up->port.flags & UPF_FOURPORT) {
661 if (!is_real_interrupt(up->port.irq))
662 up->port.mctrl |= TIOCM_OUT1;
663 } else
664 /*
665 * Most PC uarts need OUT2 raised to enable interrupts.
666 */
667 if (is_real_interrupt(up->port.irq))
668 up->port.mctrl |= TIOCM_OUT2;
669
670 serial8250_set_mctrl(&up->port, up->port.mctrl);
671 spin_unlock_irqrestore(&up->port.lock, flags);
672
673 /*
674 * Finally, enable interrupts. Note: Modem status interrupts
675 * are set via set_termios(), which will be occurring imminently
676 * anyway, so we don't enable them here.
677 */
678 up->ier = UART_IER_RLSI | UART_IER_RDI;
679 serial_outp(up, UART_IER, up->ier);
680
681 if (up->port.flags & UPF_FOURPORT) {
682 unsigned int icp;
683 /*
684 * Enable interrupts on the AST Fourport board
685 */
686 icp = (up->port.iobase & 0xfe0) | 0x01f;
687 outb_p(0x80, icp);
688 (void) inb_p(icp);
689 }
690
691 /*
692 * And clear the interrupt registers again for luck.
693 */
694 (void) serial_inp(up, UART_LSR);
695 (void) serial_inp(up, UART_RX);
696 (void) serial_inp(up, UART_IIR);
697 (void) serial_inp(up, UART_MSR);
698
699 return 0;
700}
701
702static void serial8250_shutdown(struct uart_port *port)
703{
704 struct uart_8250_port *up = (struct uart_8250_port *)port;
705 unsigned long flags;
706
707 /*
708 * Disable interrupts from this port
709 */
710 up->ier = 0;
711 serial_outp(up, UART_IER, 0);
712
713 spin_lock_irqsave(&up->port.lock, flags);
714 if (up->port.flags & UPF_FOURPORT) {
715 /* reset interrupts on the AST Fourport board */
716 inb((up->port.iobase & 0xfe0) | 0x1f);
717 up->port.mctrl |= TIOCM_OUT1;
718 } else
719 up->port.mctrl &= ~TIOCM_OUT2;
720
721 serial8250_set_mctrl(&up->port, up->port.mctrl);
722 spin_unlock_irqrestore(&up->port.lock, flags);
723
724 /*
725 * Disable break condition and FIFOs
726 */
727 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
728 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
729 UART_FCR_CLEAR_RCVR |
730 UART_FCR_CLEAR_XMIT);
731 serial_outp(up, UART_FCR, 0);
732
733 /*
734 * Read data port to reset things, and then unlink from
735 * the IRQ chain.
736 */
737 (void) serial_in(up, UART_RX);
738
739 if (!is_real_interrupt(up->port.irq))
740 del_timer_sync(&up->timer);
741 else
742 serial_unlink_irq_chain(up);
743}
744
745static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
746{
747 unsigned int quot;
748
749 /*
750 * Handle magic divisors for baud rates above baud_base on
751 * SMSC SuperIO chips.
752 */
753 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
754 baud == (port->uartclk/4))
755 quot = 0x8001;
756 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
757 baud == (port->uartclk/8))
758 quot = 0x8002;
759 else
760 quot = uart_get_divisor(port, baud);
761
762 return quot;
763}
764
765static void
766serial8250_set_termios(struct uart_port *port, struct termios *termios,
767 struct termios *old)
768{
769 struct uart_8250_port *up = (struct uart_8250_port *)port;
770 unsigned char cval, fcr = 0;
771 unsigned long flags;
772 unsigned int baud, quot;
773
774 switch (termios->c_cflag & CSIZE) {
775 case CS5:
776 cval = 0x00;
777 break;
778 case CS6:
779 cval = 0x01;
780 break;
781 case CS7:
782 cval = 0x02;
783 break;
784 default:
785 case CS8:
786 cval = 0x03;
787 break;
788 }
789
790 if (termios->c_cflag & CSTOPB)
791 cval |= 0x04;
792 if (termios->c_cflag & PARENB)
793 cval |= UART_LCR_PARITY;
794 if (!(termios->c_cflag & PARODD))
795 cval |= UART_LCR_EPAR;
796#ifdef CMSPAR
797 if (termios->c_cflag & CMSPAR)
798 cval |= UART_LCR_SPAR;
799#endif
800
801 /*
802 * Ask the core to calculate the divisor for us.
803 */
804 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
805 quot = serial8250_get_divisor(port, baud);
806 quot = 0x35; /* FIXME */
807
808 /*
809 * Work around a bug in the Oxford Semiconductor 952 rev B
810 * chip which causes it to seriously miscalculate baud rates
811 * when DLL is 0.
812 */
813 if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
814 up->rev == 0x5201)
815 quot ++;
816
817 if (uart_config[up->port.type].flags & UART_USE_FIFO) {
818 if (baud < 2400)
819 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_1;
820 else
821 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_8;
822 }
823
824 /*
825 * Ok, we're now changing the port state. Do it with
826 * interrupts disabled.
827 */
828 spin_lock_irqsave(&up->port.lock, flags);
829
830 /*
831 * Update the per-port timeout.
832 */
833 uart_update_timeout(port, termios->c_cflag, baud);
834
835 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
836 if (termios->c_iflag & INPCK)
837 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
838 if (termios->c_iflag & (BRKINT | PARMRK))
839 up->port.read_status_mask |= UART_LSR_BI;
840
841 /*
842 * Characteres to ignore
843 */
844 up->port.ignore_status_mask = 0;
845 if (termios->c_iflag & IGNPAR)
846 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
847 if (termios->c_iflag & IGNBRK) {
848 up->port.ignore_status_mask |= UART_LSR_BI;
849 /*
850 * If we're ignoring parity and break indicators,
851 * ignore overruns too (for real raw support).
852 */
853 if (termios->c_iflag & IGNPAR)
854 up->port.ignore_status_mask |= UART_LSR_OE;
855 }
856
857 /*
858 * ignore all characters if CREAD is not set
859 */
860 if ((termios->c_cflag & CREAD) == 0)
861 up->port.ignore_status_mask |= UART_LSR_DR;
862
863 /*
864 * CTS flow control flag and modem status interrupts
865 */
866 up->ier &= ~UART_IER_MSI;
867 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
868 up->ier |= UART_IER_MSI;
869
870 serial_out(up, UART_IER, up->ier);
871 serial_outp(up, 0x28, quot & 0xffff);
872 up->lcr = cval; /* Save LCR */
873 if (up->port.type != PORT_16750) {
874 if (fcr & UART_FCR_ENABLE_FIFO) {
875 /* emulated UARTs (Lucent Venus 167x) need two steps */
876 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
877 }
878 serial_outp(up, UART_FCR, fcr); /* set fcr */
879 }
880 spin_unlock_irqrestore(&up->port.lock, flags);
881}
882
883static void
884serial8250_pm(struct uart_port *port, unsigned int state,
885 unsigned int oldstate)
886{
887 struct uart_8250_port *up = (struct uart_8250_port *)port;
888 if (state) {
889 /* sleep */
890 if (up->pm)
891 up->pm(port, state, oldstate);
892 } else {
893 /* wake */
894 if (up->pm)
895 up->pm(port, state, oldstate);
896 }
897}
898
899/*
900 * Resource handling. This is complicated by the fact that resources
901 * depend on the port type. Maybe we should be claiming the standard
902 * 8250 ports, and then trying to get other resources as necessary?
903 */
904static int
905serial8250_request_std_resource(struct uart_8250_port *up, struct resource **res)
906{
907 unsigned int size = 8 << up->port.regshift;
908 int ret = 0;
909
910 switch (up->port.iotype) {
911 case SERIAL_IO_MEM:
912 if (up->port.mapbase) {
913 *res = request_mem_region(up->port.mapbase, size, "serial");
914 if (!*res)
915 ret = -EBUSY;
916 }
917 break;
918
919 case SERIAL_IO_HUB6:
920 case SERIAL_IO_PORT:
921 *res = request_region(up->port.iobase, size, "serial");
922 if (!*res)
923 ret = -EBUSY;
924 break;
925 }
926 return ret;
927}
928
929
930static void serial8250_release_port(struct uart_port *port)
931{
932 struct uart_8250_port *up = (struct uart_8250_port *)port;
933 unsigned long start, offset = 0, size = 0;
934
935 size <<= up->port.regshift;
936
937 switch (up->port.iotype) {
938 case SERIAL_IO_MEM:
939 if (up->port.mapbase) {
940 /*
941 * Unmap the area.
942 */
943 iounmap(up->port.membase);
944 up->port.membase = NULL;
945
946 start = up->port.mapbase;
947
948 if (size)
949 release_mem_region(start + offset, size);
950 release_mem_region(start, 8 << up->port.regshift);
951 }
952 break;
953
954 case SERIAL_IO_HUB6:
955 case SERIAL_IO_PORT:
956 start = up->port.iobase;
957
958 if (size)
959 release_region(start + offset, size);
960 release_region(start + offset, 8 << up->port.regshift);
961 break;
962
963 default:
964 break;
965 }
966}
967
968static int serial8250_request_port(struct uart_port *port)
969{
970 struct uart_8250_port *up = (struct uart_8250_port *)port;
971 struct resource *res = NULL, *res_rsa = NULL;
972 int ret = 0;
973
974 ret = serial8250_request_std_resource(up, &res);
975
976 /*
977 * If we have a mapbase, then request that as well.
978 */
979 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
980 int size = res->end - res->start + 1;
981
982 up->port.membase = ioremap(up->port.mapbase, size);
983 if (!up->port.membase)
984 ret = -ENOMEM;
985 }
986
987 if (ret < 0) {
988 if (res_rsa)
989 release_resource(res_rsa);
990 if (res)
991 release_resource(res);
992 }
993 return ret;
994}
995
996static void serial8250_config_port(struct uart_port *port, int flags)
997{
998 struct uart_8250_port *up = (struct uart_8250_port *)port;
999 struct resource *res_std = NULL, *res_rsa = NULL;
1000 int probeflags = PROBE_ANY;
1001
1002 probeflags &= ~PROBE_RSA;
1003
1004 if (flags & UART_CONFIG_TYPE)
1005 autoconfig(up, probeflags);
1006
1007 /*
1008 * If the port wasn't an RSA port, release the resource.
1009 */
1010 if (up->port.type != PORT_RSA && res_rsa)
1011 release_resource(res_rsa);
1012
1013 if (up->port.type == PORT_UNKNOWN && res_std)
1014 release_resource(res_std);
1015}
1016
1017static int
1018serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
1019{
1020 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
1021 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
1022 ser->type > PORT_MAX_8250 || ser->type == PORT_CIRRUS ||
1023 ser->type == PORT_STARTECH)
1024 return -EINVAL;
1025 return 0;
1026}
1027
1028static const char *
1029serial8250_type(struct uart_port *port)
1030{
1031 int type = port->type;
1032
1033 if (type >= ARRAY_SIZE(uart_config))
1034 type = 0;
1035 return uart_config[type].name;
1036}
1037
1038static struct uart_ops serial8250_pops = {
1039 .tx_empty = serial8250_tx_empty,
1040 .set_mctrl = serial8250_set_mctrl,
1041 .get_mctrl = serial8250_get_mctrl,
1042 .stop_tx = serial8250_stop_tx,
1043 .start_tx = serial8250_start_tx,
1044 .stop_rx = serial8250_stop_rx,
1045 .enable_ms = serial8250_enable_ms,
1046 .break_ctl = serial8250_break_ctl,
1047 .startup = serial8250_startup,
1048 .shutdown = serial8250_shutdown,
1049 .set_termios = serial8250_set_termios,
1050 .pm = serial8250_pm,
1051 .type = serial8250_type,
1052 .release_port = serial8250_release_port,
1053 .request_port = serial8250_request_port,
1054 .config_port = serial8250_config_port,
1055 .verify_port = serial8250_verify_port,
1056};
1057
1058static struct uart_8250_port serial8250_ports[UART_NR];
1059
1060static void __init serial8250_isa_init_ports(void)
1061{
1062 struct uart_8250_port *up;
1063 static int first = 1;
1064 int i;
1065
1066 if (!first)
1067 return;
1068 first = 0;
1069
1070 for (i = 0, up = serial8250_ports; i < ARRAY_SIZE(old_serial_port);
1071 i++, up++) {
1072 up->port.iobase = old_serial_port[i].port;
1073 up->port.irq = old_serial_port[i].irq;
1074 up->port.uartclk = get_au1x00_uart_baud_base();
1075 up->port.flags = old_serial_port[i].flags;
1076 up->port.hub6 = old_serial_port[i].hub6;
1077 up->port.membase = old_serial_port[i].iomem_base;
1078 up->port.iotype = old_serial_port[i].io_type;
1079 up->port.regshift = old_serial_port[i].iomem_reg_shift;
1080 up->port.ops = &serial8250_pops;
1081 }
1082}
1083
1084static void __init serial8250_register_ports(struct uart_driver *drv)
1085{
1086 int i;
1087
1088 serial8250_isa_init_ports();
1089
1090 for (i = 0; i < UART_NR; i++) {
1091 struct uart_8250_port *up = &serial8250_ports[i];
1092
1093 up->port.line = i;
1094 up->port.ops = &serial8250_pops;
1095 init_timer(&up->timer);
1096 up->timer.function = serial8250_timeout;
1097
1098 /*
1099 * ALPHA_KLUDGE_MCR needs to be killed.
1100 */
1101 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1102 up->mcr_force = ALPHA_KLUDGE_MCR;
1103
1104 uart_add_one_port(drv, &up->port);
1105 }
1106}
1107
1108#ifdef CONFIG_SERIAL_AU1X00_CONSOLE
1109
1110#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1111
1112/*
1113 * Wait for transmitter & holding register to empty
1114 */
1115static inline void wait_for_xmitr(struct uart_8250_port *up)
1116{
1117 unsigned int status, tmout = 10000;
1118
1119 /* Wait up to 10ms for the character(s) to be sent. */
1120 do {
1121 status = serial_in(up, UART_LSR);
1122
1123 if (status & UART_LSR_BI)
1124 up->lsr_break_flag = UART_LSR_BI;
1125
1126 if (--tmout == 0)
1127 break;
1128 udelay(1);
1129 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1130
1131 /* Wait up to 1s for flow control if necessary */
1132 if (up->port.flags & UPF_CONS_FLOW) {
1133 tmout = 1000000;
1134 while (--tmout &&
1135 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1136 udelay(1);
1137 }
1138}
1139
1140/*
1141 * Print a string to the serial port trying not to disturb
1142 * any possible real use of the port...
1143 *
1144 * The console_lock must be held when we get here.
1145 */
1146static void
1147serial8250_console_write(struct console *co, const char *s, unsigned int count)
1148{
1149 struct uart_8250_port *up = &serial8250_ports[co->index];
1150 unsigned int ier;
1151 int i;
1152
1153 /*
1154 * First save the UER then disable the interrupts
1155 */
1156 ier = serial_in(up, UART_IER);
1157 serial_out(up, UART_IER, 0);
1158
1159 /*
1160 * Now, do each character
1161 */
1162 for (i = 0; i < count; i++, s++) {
1163 wait_for_xmitr(up);
1164
1165 /*
1166 * Send the character out.
1167 * If a LF, also do CR...
1168 */
1169 serial_out(up, UART_TX, *s);
1170 if (*s == 10) {
1171 wait_for_xmitr(up);
1172 serial_out(up, UART_TX, 13);
1173 }
1174 }
1175
1176 /*
1177 * Finally, wait for transmitter to become empty
1178 * and restore the IER
1179 */
1180 wait_for_xmitr(up);
1181 serial_out(up, UART_IER, ier);
1182}
1183
1184static int __init serial8250_console_setup(struct console *co, char *options)
1185{
1186 struct uart_port *port;
1187 int baud = 9600;
1188 int bits = 8;
1189 int parity = 'n';
1190 int flow = 'n';
1191
1192 /*
1193 * Check whether an invalid uart number has been specified, and
1194 * if so, search for the first available port that does have
1195 * console support.
1196 */
1197 if (co->index >= UART_NR)
1198 co->index = 0;
1199 port = &serial8250_ports[co->index].port;
1200
1201 /*
1202 * Temporary fix.
1203 */
1204 spin_lock_init(&port->lock);
1205
1206 if (options)
1207 uart_parse_options(options, &baud, &parity, &bits, &flow);
1208
1209 return uart_set_options(port, co, baud, parity, bits, flow);
1210}
1211
1212extern struct uart_driver serial8250_reg;
1213static struct console serial8250_console = {
1214 .name = "ttyS",
1215 .write = serial8250_console_write,
1216 .device = uart_console_device,
1217 .setup = serial8250_console_setup,
1218 .flags = CON_PRINTBUFFER,
1219 .index = -1,
1220 .data = &serial8250_reg,
1221};
1222
1223static int __init serial8250_console_init(void)
1224{
1225 serial8250_isa_init_ports();
1226 register_console(&serial8250_console);
1227 return 0;
1228}
1229console_initcall(serial8250_console_init);
1230
1231#define SERIAL8250_CONSOLE &serial8250_console
1232#else
1233#define SERIAL8250_CONSOLE NULL
1234#endif
1235
1236static struct uart_driver serial8250_reg = {
1237 .owner = THIS_MODULE,
1238 .driver_name = "serial",
1239 .devfs_name = "tts/",
1240 .dev_name = "ttyS",
1241 .major = TTY_MAJOR,
1242 .minor = 64,
1243 .nr = UART_NR,
1244 .cons = SERIAL8250_CONSOLE,
1245};
1246
1247int __init early_serial_setup(struct uart_port *port)
1248{
1249 serial8250_isa_init_ports();
1250 serial8250_ports[port->line].port = *port;
1251 serial8250_ports[port->line].port.ops = &serial8250_pops;
1252 return 0;
1253}
1254
1255/**
1256 * serial8250_suspend_port - suspend one serial port
1257 * @line: serial line number
1258 * @level: the level of port suspension, as per uart_suspend_port
1259 *
1260 * Suspend one serial port.
1261 */
1262void serial8250_suspend_port(int line)
1263{
1264 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
1265}
1266
1267/**
1268 * serial8250_resume_port - resume one serial port
1269 * @line: serial line number
1270 * @level: the level of port resumption, as per uart_resume_port
1271 *
1272 * Resume one serial port.
1273 */
1274void serial8250_resume_port(int line)
1275{
1276 uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
1277}
1278
1279static int __init serial8250_init(void)
1280{
1281 int ret, i;
1282
1283 printk(KERN_INFO "Serial: Au1x00 driver\n");
1284
1285 for (i = 0; i < NR_IRQS; i++)
1286 spin_lock_init(&irq_lists[i].lock);
1287
1288 ret = uart_register_driver(&serial8250_reg);
1289 if (ret >= 0)
1290 serial8250_register_ports(&serial8250_reg);
1291
1292 return ret;
1293}
1294
1295static void __exit serial8250_exit(void)
1296{
1297 int i;
1298
1299 for (i = 0; i < UART_NR; i++)
1300 uart_remove_one_port(&serial8250_reg, &serial8250_ports[i].port);
1301
1302 uart_unregister_driver(&serial8250_reg);
1303}
1304
1305module_init(serial8250_init);
1306module_exit(serial8250_exit);
1307
1308EXPORT_SYMBOL(serial8250_suspend_port);
1309EXPORT_SYMBOL(serial8250_resume_port);
1310
1311MODULE_LICENSE("GPL");
1312MODULE_DESCRIPTION("Au1x00 serial driver\n");
diff --git a/drivers/serial/bast_sio.c b/drivers/serial/bast_sio.c
new file mode 100644
index 000000000000..2b48fab6f0c6
--- /dev/null
+++ b/drivers/serial/bast_sio.c
@@ -0,0 +1,80 @@
1/* linux/drivers/serial/bast_sio.c
2 *
3 * Copyright (c) 2004 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
5 *
6 * http://www.simtec.co.uk/products/EB2410ITX/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Modifications:
13 * 23-Sep-2004 BJD Added copyright header
14 * 23-Sep-2004 BJD Added serial port remove code
15*/
16
17#include <linux/module.h>
18#include <linux/config.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/tty.h>
22#include <linux/serial.h>
23#include <linux/serial_core.h>
24#include <linux/types.h>
25
26#include <asm/io.h>
27#include <asm/serial.h>
28#include <asm/mach-types.h>
29
30#include <asm/arch/map.h>
31#include <asm/arch/irqs.h>
32#include <asm/arch/bast-map.h>
33#include <asm/arch/bast-irq.h>
34
35static int __init serial_bast_register(unsigned long port, unsigned int irq)
36{
37 struct serial_struct serial_req;
38
39 serial_req.flags = UPF_AUTOPROBE | UPF_SHARE_IRQ;
40 serial_req.baud_base = BASE_BAUD;
41 serial_req.irq = irq;
42 serial_req.io_type = UPIO_MEM;
43 serial_req.iomap_base = port;
44 serial_req.iomem_base = ioremap(port, 0x10);
45 serial_req.iomem_reg_shift = 0;
46
47 return register_serial(&serial_req);
48}
49
50#define SERIAL_BASE (S3C2410_CS2 + BAST_PA_SUPERIO)
51
52static int port[2] = { -1, -1 };
53
54static int __init serial_bast_init(void)
55{
56 if (machine_is_bast()) {
57 port[0] = serial_bast_register(SERIAL_BASE + 0x2f8, IRQ_PCSERIAL1);
58 port[1] = serial_bast_register(SERIAL_BASE + 0x3f8, IRQ_PCSERIAL2);
59 }
60
61 return 0;
62}
63
64static void __exit serial_bast_exit(void)
65{
66 if (port[0] != -1)
67 unregister_serial(port[0]);
68 if (port[1] != -1)
69 unregister_serial(port[1]);
70}
71
72
73module_init(serial_bast_init);
74module_exit(serial_bast_exit);
75
76MODULE_LICENSE("GPL");
77MODULE_AUTHOR("Ben Dooks, ben@simtec.co.uk");
78MODULE_DESCRIPTION("BAST Onboard Serial setup");
79
80
diff --git a/drivers/serial/clps711x.c b/drivers/serial/clps711x.c
new file mode 100644
index 000000000000..16592fae47f3
--- /dev/null
+++ b/drivers/serial/clps711x.c
@@ -0,0 +1,609 @@
1/*
2 * linux/drivers/char/clps711x.c
3 *
4 * Driver for CLPS711x serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * $Id: clps711x.c,v 1.42 2002/07/28 10:03:28 rmk Exp $
26 *
27 */
28#include <linux/config.h>
29
30#if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31#define SUPPORT_SYSRQ
32#endif
33
34#include <linux/module.h>
35#include <linux/ioport.h>
36#include <linux/init.h>
37#include <linux/console.h>
38#include <linux/sysrq.h>
39#include <linux/spinlock.h>
40#include <linux/device.h>
41#include <linux/tty.h>
42#include <linux/tty_flip.h>
43#include <linux/serial_core.h>
44#include <linux/serial.h>
45
46#include <asm/hardware.h>
47#include <asm/io.h>
48#include <asm/irq.h>
49#include <asm/hardware/clps7111.h>
50
51#define UART_NR 2
52
53#define SERIAL_CLPS711X_MAJOR 204
54#define SERIAL_CLPS711X_MINOR 40
55#define SERIAL_CLPS711X_NR UART_NR
56
57/*
58 * We use the relevant SYSCON register as a base address for these ports.
59 */
60#define UBRLCR(port) ((port)->iobase + UBRLCR1 - SYSCON1)
61#define UARTDR(port) ((port)->iobase + UARTDR1 - SYSCON1)
62#define SYSFLG(port) ((port)->iobase + SYSFLG1 - SYSCON1)
63#define SYSCON(port) ((port)->iobase + SYSCON1 - SYSCON1)
64
65#define TX_IRQ(port) ((port)->irq)
66#define RX_IRQ(port) ((port)->irq + 1)
67
68#define UART_ANY_ERR (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)
69
70#define tx_enabled(port) ((port)->unused[0])
71
72static void
73clps711xuart_stop_tx(struct uart_port *port, unsigned int tty_stop)
74{
75 if (tx_enabled(port)) {
76 disable_irq(TX_IRQ(port));
77 tx_enabled(port) = 0;
78 }
79}
80
81static void
82clps711xuart_start_tx(struct uart_port *port, unsigned int tty_start)
83{
84 if (!tx_enabled(port)) {
85 enable_irq(TX_IRQ(port));
86 tx_enabled(port) = 1;
87 }
88}
89
90static void clps711xuart_stop_rx(struct uart_port *port)
91{
92 disable_irq(RX_IRQ(port));
93}
94
95static void clps711xuart_enable_ms(struct uart_port *port)
96{
97}
98
99static irqreturn_t clps711xuart_int_rx(int irq, void *dev_id, struct pt_regs *regs)
100{
101 struct uart_port *port = dev_id;
102 struct tty_struct *tty = port->info->tty;
103 unsigned int status, ch, flg, ignored = 0;
104
105 status = clps_readl(SYSFLG(port));
106 while (!(status & SYSFLG_URXFE)) {
107 ch = clps_readl(UARTDR(port));
108
109 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
110 goto ignore_char;
111 port->icount.rx++;
112
113 flg = TTY_NORMAL;
114
115 /*
116 * Note that the error handling code is
117 * out of the main execution path
118 */
119 if (ch & UART_ANY_ERR)
120 goto handle_error;
121
122 if (uart_handle_sysrq_char(port, ch, regs))
123 goto ignore_char;
124
125 error_return:
126 tty_insert_flip_char(tty, ch, flg);
127 ignore_char:
128 status = clps_readl(SYSFLG(port));
129 }
130 out:
131 tty_flip_buffer_push(tty);
132 return IRQ_HANDLED;
133
134 handle_error:
135 if (ch & UARTDR_PARERR)
136 port->icount.parity++;
137 else if (ch & UARTDR_FRMERR)
138 port->icount.frame++;
139 if (ch & UARTDR_OVERR)
140 port->icount.overrun++;
141
142 if (ch & port->ignore_status_mask) {
143 if (++ignored > 100)
144 goto out;
145 goto ignore_char;
146 }
147 ch &= port->read_status_mask;
148
149 if (ch & UARTDR_PARERR)
150 flg = TTY_PARITY;
151 else if (ch & UARTDR_FRMERR)
152 flg = TTY_FRAME;
153
154 if (ch & UARTDR_OVERR) {
155 /*
156 * CHECK: does overrun affect the current character?
157 * ASSUMPTION: it does not.
158 */
159 tty_insert_flip_char(tty, ch, flg);
160 ch = 0;
161 flg = TTY_OVERRUN;
162 }
163#ifdef SUPPORT_SYSRQ
164 port->sysrq = 0;
165#endif
166 goto error_return;
167}
168
169static irqreturn_t clps711xuart_int_tx(int irq, void *dev_id, struct pt_regs *regs)
170{
171 struct uart_port *port = dev_id;
172 struct circ_buf *xmit = &port->info->xmit;
173 int count;
174
175 if (port->x_char) {
176 clps_writel(port->x_char, UARTDR(port));
177 port->icount.tx++;
178 port->x_char = 0;
179 return IRQ_HANDLED;
180 }
181 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
182 clps711xuart_stop_tx(port, 0);
183 return IRQ_HANDLED;
184 }
185
186 count = port->fifosize >> 1;
187 do {
188 clps_writel(xmit->buf[xmit->tail], UARTDR(port));
189 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
190 port->icount.tx++;
191 if (uart_circ_empty(xmit))
192 break;
193 } while (--count > 0);
194
195 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
196 uart_write_wakeup(port);
197
198 if (uart_circ_empty(xmit))
199 clps711xuart_stop_tx(port, 0);
200
201 return IRQ_HANDLED;
202}
203
204static unsigned int clps711xuart_tx_empty(struct uart_port *port)
205{
206 unsigned int status = clps_readl(SYSFLG(port));
207 return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT;
208}
209
210static unsigned int clps711xuart_get_mctrl(struct uart_port *port)
211{
212 unsigned int port_addr;
213 unsigned int result = 0;
214 unsigned int status;
215
216 port_addr = SYSFLG(port);
217 if (port_addr == SYSFLG1) {
218 status = clps_readl(SYSFLG1);
219 if (status & SYSFLG1_DCD)
220 result |= TIOCM_CAR;
221 if (status & SYSFLG1_DSR)
222 result |= TIOCM_DSR;
223 if (status & SYSFLG1_CTS)
224 result |= TIOCM_CTS;
225 }
226
227 return result;
228}
229
230static void
231clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
232{
233}
234
235static void clps711xuart_break_ctl(struct uart_port *port, int break_state)
236{
237 unsigned long flags;
238 unsigned int ubrlcr;
239
240 spin_lock_irqsave(&port->lock, flags);
241 ubrlcr = clps_readl(UBRLCR(port));
242 if (break_state == -1)
243 ubrlcr |= UBRLCR_BREAK;
244 else
245 ubrlcr &= ~UBRLCR_BREAK;
246 clps_writel(ubrlcr, UBRLCR(port));
247 spin_unlock_irqrestore(&port->lock, flags);
248}
249
250static int clps711xuart_startup(struct uart_port *port)
251{
252 unsigned int syscon;
253 int retval;
254
255 tx_enabled(port) = 1;
256
257 /*
258 * Allocate the IRQs
259 */
260 retval = request_irq(TX_IRQ(port), clps711xuart_int_tx, 0,
261 "clps711xuart_tx", port);
262 if (retval)
263 return retval;
264
265 retval = request_irq(RX_IRQ(port), clps711xuart_int_rx, 0,
266 "clps711xuart_rx", port);
267 if (retval) {
268 free_irq(TX_IRQ(port), port);
269 return retval;
270 }
271
272 /*
273 * enable the port
274 */
275 syscon = clps_readl(SYSCON(port));
276 syscon |= SYSCON_UARTEN;
277 clps_writel(syscon, SYSCON(port));
278
279 return 0;
280}
281
282static void clps711xuart_shutdown(struct uart_port *port)
283{
284 unsigned int ubrlcr, syscon;
285
286 /*
287 * Free the interrupt
288 */
289 free_irq(TX_IRQ(port), port); /* TX interrupt */
290 free_irq(RX_IRQ(port), port); /* RX interrupt */
291
292 /*
293 * disable the port
294 */
295 syscon = clps_readl(SYSCON(port));
296 syscon &= ~SYSCON_UARTEN;
297 clps_writel(syscon, SYSCON(port));
298
299 /*
300 * disable break condition and fifos
301 */
302 ubrlcr = clps_readl(UBRLCR(port));
303 ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK);
304 clps_writel(ubrlcr, UBRLCR(port));
305}
306
307static void
308clps711xuart_set_termios(struct uart_port *port, struct termios *termios,
309 struct termios *old)
310{
311 unsigned int ubrlcr, baud, quot;
312 unsigned long flags;
313
314 /*
315 * We don't implement CREAD.
316 */
317 termios->c_cflag |= CREAD;
318
319 /*
320 * Ask the core to calculate the divisor for us.
321 */
322 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
323 quot = uart_get_divisor(port, baud);
324
325 switch (termios->c_cflag & CSIZE) {
326 case CS5:
327 ubrlcr = UBRLCR_WRDLEN5;
328 break;
329 case CS6:
330 ubrlcr = UBRLCR_WRDLEN6;
331 break;
332 case CS7:
333 ubrlcr = UBRLCR_WRDLEN7;
334 break;
335 default: // CS8
336 ubrlcr = UBRLCR_WRDLEN8;
337 break;
338 }
339 if (termios->c_cflag & CSTOPB)
340 ubrlcr |= UBRLCR_XSTOP;
341 if (termios->c_cflag & PARENB) {
342 ubrlcr |= UBRLCR_PRTEN;
343 if (!(termios->c_cflag & PARODD))
344 ubrlcr |= UBRLCR_EVENPRT;
345 }
346 if (port->fifosize > 1)
347 ubrlcr |= UBRLCR_FIFOEN;
348
349 spin_lock_irqsave(&port->lock, flags);
350
351 /*
352 * Update the per-port timeout.
353 */
354 uart_update_timeout(port, termios->c_cflag, baud);
355
356 port->read_status_mask = UARTDR_OVERR;
357 if (termios->c_iflag & INPCK)
358 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
359
360 /*
361 * Characters to ignore
362 */
363 port->ignore_status_mask = 0;
364 if (termios->c_iflag & IGNPAR)
365 port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR;
366 if (termios->c_iflag & IGNBRK) {
367 /*
368 * If we're ignoring parity and break indicators,
369 * ignore overruns to (for real raw support).
370 */
371 if (termios->c_iflag & IGNPAR)
372 port->ignore_status_mask |= UARTDR_OVERR;
373 }
374
375 quot -= 1;
376
377 clps_writel(ubrlcr | quot, UBRLCR(port));
378
379 spin_unlock_irqrestore(&port->lock, flags);
380}
381
382static const char *clps711xuart_type(struct uart_port *port)
383{
384 return port->type == PORT_CLPS711X ? "CLPS711x" : NULL;
385}
386
387/*
388 * Configure/autoconfigure the port.
389 */
390static void clps711xuart_config_port(struct uart_port *port, int flags)
391{
392 if (flags & UART_CONFIG_TYPE)
393 port->type = PORT_CLPS711X;
394}
395
396static void clps711xuart_release_port(struct uart_port *port)
397{
398}
399
400static int clps711xuart_request_port(struct uart_port *port)
401{
402 return 0;
403}
404
405static struct uart_ops clps711x_pops = {
406 .tx_empty = clps711xuart_tx_empty,
407 .set_mctrl = clps711xuart_set_mctrl_null,
408 .get_mctrl = clps711xuart_get_mctrl,
409 .stop_tx = clps711xuart_stop_tx,
410 .start_tx = clps711xuart_start_tx,
411 .stop_rx = clps711xuart_stop_rx,
412 .enable_ms = clps711xuart_enable_ms,
413 .break_ctl = clps711xuart_break_ctl,
414 .startup = clps711xuart_startup,
415 .shutdown = clps711xuart_shutdown,
416 .set_termios = clps711xuart_set_termios,
417 .type = clps711xuart_type,
418 .config_port = clps711xuart_config_port,
419 .release_port = clps711xuart_release_port,
420 .request_port = clps711xuart_request_port,
421};
422
423static struct uart_port clps711x_ports[UART_NR] = {
424 {
425 .iobase = SYSCON1,
426 .irq = IRQ_UTXINT1, /* IRQ_URXINT1, IRQ_UMSINT */
427 .uartclk = 3686400,
428 .fifosize = 16,
429 .ops = &clps711x_pops,
430 .line = 0,
431 .flags = ASYNC_BOOT_AUTOCONF,
432 },
433 {
434 .iobase = SYSCON2,
435 .irq = IRQ_UTXINT2, /* IRQ_URXINT2 */
436 .uartclk = 3686400,
437 .fifosize = 16,
438 .ops = &clps711x_pops,
439 .line = 1,
440 .flags = ASYNC_BOOT_AUTOCONF,
441 }
442};
443
444#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
445/*
446 * Print a string to the serial port trying not to disturb
447 * any possible real use of the port...
448 *
449 * The console_lock must be held when we get here.
450 *
451 * Note that this is called with interrupts already disabled
452 */
453static void
454clps711xuart_console_write(struct console *co, const char *s,
455 unsigned int count)
456{
457 struct uart_port *port = clps711x_ports + co->index;
458 unsigned int status, syscon;
459 int i;
460
461 /*
462 * Ensure that the port is enabled.
463 */
464 syscon = clps_readl(SYSCON(port));
465 clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
466
467 /*
468 * Now, do each character
469 */
470 for (i = 0; i < count; i++) {
471 do {
472 status = clps_readl(SYSFLG(port));
473 } while (status & SYSFLG_UTXFF);
474 clps_writel(s[i], UARTDR(port));
475 if (s[i] == '\n') {
476 do {
477 status = clps_readl(SYSFLG(port));
478 } while (status & SYSFLG_UTXFF);
479 clps_writel('\r', UARTDR(port));
480 }
481 }
482
483 /*
484 * Finally, wait for transmitter to become empty
485 * and restore the uart state.
486 */
487 do {
488 status = clps_readl(SYSFLG(port));
489 } while (status & SYSFLG_UBUSY);
490
491 clps_writel(syscon, SYSCON(port));
492}
493
494static void __init
495clps711xuart_console_get_options(struct uart_port *port, int *baud,
496 int *parity, int *bits)
497{
498 if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
499 unsigned int ubrlcr, quot;
500
501 ubrlcr = clps_readl(UBRLCR(port));
502
503 *parity = 'n';
504 if (ubrlcr & UBRLCR_PRTEN) {
505 if (ubrlcr & UBRLCR_EVENPRT)
506 *parity = 'e';
507 else
508 *parity = 'o';
509 }
510
511 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
512 *bits = 7;
513 else
514 *bits = 8;
515
516 quot = ubrlcr & UBRLCR_BAUD_MASK;
517 *baud = port->uartclk / (16 * (quot + 1));
518 }
519}
520
521static int __init clps711xuart_console_setup(struct console *co, char *options)
522{
523 struct uart_port *port;
524 int baud = 38400;
525 int bits = 8;
526 int parity = 'n';
527 int flow = 'n';
528
529 /*
530 * Check whether an invalid uart number has been specified, and
531 * if so, search for the first available port that does have
532 * console support.
533 */
534 port = uart_get_console(clps711x_ports, UART_NR, co);
535
536 if (options)
537 uart_parse_options(options, &baud, &parity, &bits, &flow);
538 else
539 clps711xuart_console_get_options(port, &baud, &parity, &bits);
540
541 return uart_set_options(port, co, baud, parity, bits, flow);
542}
543
544extern struct uart_driver clps711x_reg;
545static struct console clps711x_console = {
546 .name = "ttyCL",
547 .write = clps711xuart_console_write,
548 .device = uart_console_device,
549 .setup = clps711xuart_console_setup,
550 .flags = CON_PRINTBUFFER,
551 .index = -1,
552 .data = &clps711x_reg,
553};
554
555static int __init clps711xuart_console_init(void)
556{
557 register_console(&clps711x_console);
558 return 0;
559}
560console_initcall(clps711xuart_console_init);
561
562#define CLPS711X_CONSOLE &clps711x_console
563#else
564#define CLPS711X_CONSOLE NULL
565#endif
566
567static struct uart_driver clps711x_reg = {
568 .driver_name = "ttyCL",
569 .dev_name = "ttyCL",
570 .major = SERIAL_CLPS711X_MAJOR,
571 .minor = SERIAL_CLPS711X_MINOR,
572 .nr = UART_NR,
573
574 .cons = CLPS711X_CONSOLE,
575};
576
577static int __init clps711xuart_init(void)
578{
579 int ret, i;
580
581 printk(KERN_INFO "Serial: CLPS711x driver $Revision: 1.42 $\n");
582
583 ret = uart_register_driver(&clps711x_reg);
584 if (ret)
585 return ret;
586
587 for (i = 0; i < UART_NR; i++)
588 uart_add_one_port(&clps711x_reg, &clps711x_ports[i]);
589
590 return 0;
591}
592
593static void __exit clps711xuart_exit(void)
594{
595 int i;
596
597 for (i = 0; i < UART_NR; i++)
598 uart_remove_one_port(&clps711x_reg, &clps711x_ports[i]);
599
600 uart_unregister_driver(&clps711x_reg);
601}
602
603module_init(clps711xuart_init);
604module_exit(clps711xuart_exit);
605
606MODULE_AUTHOR("Deep Blue Solutions Ltd");
607MODULE_DESCRIPTION("CLPS-711x generic serial driver $Revision: 1.42 $");
608MODULE_LICENSE("GPL");
609MODULE_ALIAS_CHARDEV(SERIAL_CLPS711X_MAJOR, SERIAL_CLPS711X_MINOR);
diff --git a/drivers/serial/cpm_uart/Makefile b/drivers/serial/cpm_uart/Makefile
new file mode 100644
index 000000000000..e072724ea754
--- /dev/null
+++ b/drivers/serial/cpm_uart/Makefile
@@ -0,0 +1,11 @@
1#
2# Makefile for the Motorola 8xx FEC ethernet controller
3#
4
5obj-$(CONFIG_SERIAL_CPM) += cpm_uart.o
6
7# Select the correct platform objects.
8cpm_uart-objs-$(CONFIG_CPM2) += cpm_uart_cpm2.o
9cpm_uart-objs-$(CONFIG_8xx) += cpm_uart_cpm1.o
10
11cpm_uart-objs := cpm_uart_core.o $(cpm_uart-objs-y)
diff --git a/drivers/serial/cpm_uart/cpm_uart.h b/drivers/serial/cpm_uart/cpm_uart.h
new file mode 100644
index 000000000000..5f6187baad86
--- /dev/null
+++ b/drivers/serial/cpm_uart/cpm_uart.h
@@ -0,0 +1,89 @@
1/*
2 * linux/drivers/serial/cpm_uart.h
3 *
4 * Driver for CPM (SCC/SMC) serial ports
5 *
6 * Copyright (C) 2004 Freescale Semiconductor, Inc.
7 *
8 */
9#ifndef CPM_UART_H
10#define CPM_UART_H
11
12#include <linux/config.h>
13
14#if defined(CONFIG_CPM2)
15#include "cpm_uart_cpm2.h"
16#elif defined(CONFIG_8xx)
17#include "cpm_uart_cpm1.h"
18#endif
19
20#define SERIAL_CPM_MAJOR 204
21#define SERIAL_CPM_MINOR 46
22
23#define IS_SMC(pinfo) (pinfo->flags & FLAG_SMC)
24#define IS_DISCARDING(pinfo) (pinfo->flags & FLAG_DISCARDING)
25#define FLAG_DISCARDING 0x00000004 /* when set, don't discard */
26#define FLAG_SMC 0x00000002
27#define FLAG_CONSOLE 0x00000001
28
29#define UART_SMC1 0
30#define UART_SMC2 1
31#define UART_SCC1 2
32#define UART_SCC2 3
33#define UART_SCC3 4
34#define UART_SCC4 5
35
36#define UART_NR 6
37
38#define RX_NUM_FIFO 4
39#define RX_BUF_SIZE 32
40#define TX_NUM_FIFO 4
41#define TX_BUF_SIZE 32
42
43struct uart_cpm_port {
44 struct uart_port port;
45 u16 rx_nrfifos;
46 u16 rx_fifosize;
47 u16 tx_nrfifos;
48 u16 tx_fifosize;
49 smc_t *smcp;
50 smc_uart_t *smcup;
51 scc_t *sccp;
52 scc_uart_t *sccup;
53 volatile cbd_t *rx_bd_base;
54 volatile cbd_t *rx_cur;
55 volatile cbd_t *tx_bd_base;
56 volatile cbd_t *tx_cur;
57 unsigned char *tx_buf;
58 unsigned char *rx_buf;
59 u32 flags;
60 void (*set_lineif)(struct uart_cpm_port *);
61 u8 brg;
62 uint dp_addr;
63 void *mem_addr;
64 dma_addr_t dma_addr;
65 /* helpers */
66 int baud;
67 int bits;
68 /* Keep track of 'odd' SMC2 wirings */
69 int is_portb;
70};
71
72extern int cpm_uart_port_map[UART_NR];
73extern int cpm_uart_nr;
74extern struct uart_cpm_port cpm_uart_ports[UART_NR];
75
76/* these are located in their respective files */
77void cpm_line_cr_cmd(int line, int cmd);
78int cpm_uart_init_portdesc(void);
79int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con);
80void cpm_uart_freebuf(struct uart_cpm_port *pinfo);
81
82void smc1_lineif(struct uart_cpm_port *pinfo);
83void smc2_lineif(struct uart_cpm_port *pinfo);
84void scc1_lineif(struct uart_cpm_port *pinfo);
85void scc2_lineif(struct uart_cpm_port *pinfo);
86void scc3_lineif(struct uart_cpm_port *pinfo);
87void scc4_lineif(struct uart_cpm_port *pinfo);
88
89#endif /* CPM_UART_H */
diff --git a/drivers/serial/cpm_uart/cpm_uart_core.c b/drivers/serial/cpm_uart/cpm_uart_core.c
new file mode 100644
index 000000000000..29db677d4284
--- /dev/null
+++ b/drivers/serial/cpm_uart/cpm_uart_core.c
@@ -0,0 +1,1177 @@
1/*
2 * linux/drivers/serial/cpm_uart.c
3 *
4 * Driver for CPM (SCC/SMC) serial ports; core driver
5 *
6 * Based on arch/ppc/cpm2_io/uart.c by Dan Malek
7 * Based on ppc8xx.c by Thomas Gleixner
8 * Based on drivers/serial/amba.c by Russell King
9 *
10 * Maintainer: Kumar Gala (kumar.gala@freescale.com) (CPM2)
11 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
12 *
13 * Copyright (C) 2004 Freescale Semiconductor, Inc.
14 * (C) 2004 Intracom, S.A.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 */
31
32#include <linux/config.h>
33#include <linux/module.h>
34#include <linux/tty.h>
35#include <linux/ioport.h>
36#include <linux/init.h>
37#include <linux/serial.h>
38#include <linux/console.h>
39#include <linux/sysrq.h>
40#include <linux/device.h>
41#include <linux/bootmem.h>
42#include <linux/dma-mapping.h>
43
44#include <asm/io.h>
45#include <asm/irq.h>
46#include <asm/delay.h>
47
48#if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
49#define SUPPORT_SYSRQ
50#endif
51
52#include <linux/serial_core.h>
53#include <linux/kernel.h>
54
55#include "cpm_uart.h"
56
57/***********************************************************************/
58
59/* Track which ports are configured as uarts */
60int cpm_uart_port_map[UART_NR];
61/* How many ports did we config as uarts */
62int cpm_uart_nr;
63
64/**************************************************************/
65
66static int cpm_uart_tx_pump(struct uart_port *port);
67static void cpm_uart_init_smc(struct uart_cpm_port *pinfo);
68static void cpm_uart_init_scc(struct uart_cpm_port *pinfo);
69static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
70
71/**************************************************************/
72
73/*
74 * Check, if transmit buffers are processed
75*/
76static unsigned int cpm_uart_tx_empty(struct uart_port *port)
77{
78 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
79 volatile cbd_t *bdp = pinfo->tx_bd_base;
80 int ret = 0;
81
82 while (1) {
83 if (bdp->cbd_sc & BD_SC_READY)
84 break;
85
86 if (bdp->cbd_sc & BD_SC_WRAP) {
87 ret = TIOCSER_TEMT;
88 break;
89 }
90 bdp++;
91 }
92
93 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
94
95 return ret;
96}
97
98static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
99{
100 /* Whee. Do nothing. */
101}
102
103static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
104{
105 /* Whee. Do nothing. */
106 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
107}
108
109/*
110 * Stop transmitter
111 */
112static void cpm_uart_stop_tx(struct uart_port *port, unsigned int tty_stop)
113{
114 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
115 volatile smc_t *smcp = pinfo->smcp;
116 volatile scc_t *sccp = pinfo->sccp;
117
118 pr_debug("CPM uart[%d]:stop tx\n", port->line);
119
120 if (IS_SMC(pinfo))
121 smcp->smc_smcm &= ~SMCM_TX;
122 else
123 sccp->scc_sccm &= ~UART_SCCM_TX;
124}
125
126/*
127 * Start transmitter
128 */
129static void cpm_uart_start_tx(struct uart_port *port, unsigned int tty_start)
130{
131 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
132 volatile smc_t *smcp = pinfo->smcp;
133 volatile scc_t *sccp = pinfo->sccp;
134
135 pr_debug("CPM uart[%d]:start tx\n", port->line);
136
137 if (IS_SMC(pinfo)) {
138 if (smcp->smc_smcm & SMCM_TX)
139 return;
140 } else {
141 if (sccp->scc_sccm & UART_SCCM_TX)
142 return;
143 }
144
145 if (cpm_uart_tx_pump(port) != 0) {
146 if (IS_SMC(pinfo))
147 smcp->smc_smcm |= SMCM_TX;
148 else
149 sccp->scc_sccm |= UART_SCCM_TX;
150 }
151}
152
153/*
154 * Stop receiver
155 */
156static void cpm_uart_stop_rx(struct uart_port *port)
157{
158 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
159 volatile smc_t *smcp = pinfo->smcp;
160 volatile scc_t *sccp = pinfo->sccp;
161
162 pr_debug("CPM uart[%d]:stop rx\n", port->line);
163
164 if (IS_SMC(pinfo))
165 smcp->smc_smcm &= ~SMCM_RX;
166 else
167 sccp->scc_sccm &= ~UART_SCCM_RX;
168}
169
170/*
171 * Enable Modem status interrupts
172 */
173static void cpm_uart_enable_ms(struct uart_port *port)
174{
175 pr_debug("CPM uart[%d]:enable ms\n", port->line);
176}
177
178/*
179 * Generate a break.
180 */
181static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
182{
183 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
184 int line = pinfo - cpm_uart_ports;
185
186 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
187 break_state);
188
189 if (break_state)
190 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
191 else
192 cpm_line_cr_cmd(line, CPM_CR_RESTART_TX);
193}
194
195/*
196 * Transmit characters, refill buffer descriptor, if possible
197 */
198static void cpm_uart_int_tx(struct uart_port *port, struct pt_regs *regs)
199{
200 pr_debug("CPM uart[%d]:TX INT\n", port->line);
201
202 cpm_uart_tx_pump(port);
203}
204
205/*
206 * Receive characters
207 */
208static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
209{
210 int i;
211 unsigned char ch, *cp;
212 struct tty_struct *tty = port->info->tty;
213 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
214 volatile cbd_t *bdp;
215 u16 status;
216 unsigned int flg;
217
218 pr_debug("CPM uart[%d]:RX INT\n", port->line);
219
220 /* Just loop through the closed BDs and copy the characters into
221 * the buffer.
222 */
223 bdp = pinfo->rx_cur;
224 for (;;) {
225 /* get status */
226 status = bdp->cbd_sc;
227 /* If this one is empty, return happy */
228 if (status & BD_SC_EMPTY)
229 break;
230
231 /* get number of characters, and check spce in flip-buffer */
232 i = bdp->cbd_datlen;
233
234 /* If we have not enough room in tty flip buffer, then we try
235 * later, which will be the next rx-interrupt or a timeout
236 */
237 if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE) {
238 tty->flip.work.func((void *)tty);
239 if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE) {
240 printk(KERN_WARNING "TTY_DONT_FLIP set\n");
241 return;
242 }
243 }
244
245 /* get pointer */
246 cp = (unsigned char *)bus_to_virt(bdp->cbd_bufaddr);
247
248 /* loop through the buffer */
249 while (i-- > 0) {
250 ch = *cp++;
251 port->icount.rx++;
252 flg = TTY_NORMAL;
253
254 if (status &
255 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
256 goto handle_error;
257 if (uart_handle_sysrq_char(port, ch, regs))
258 continue;
259
260 error_return:
261 *tty->flip.char_buf_ptr++ = ch;
262 *tty->flip.flag_buf_ptr++ = flg;
263 tty->flip.count++;
264
265 } /* End while (i--) */
266
267 /* This BD is ready to be used again. Clear status. get next */
268 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
269 bdp->cbd_sc |= BD_SC_EMPTY;
270
271 if (bdp->cbd_sc & BD_SC_WRAP)
272 bdp = pinfo->rx_bd_base;
273 else
274 bdp++;
275 } /* End for (;;) */
276
277 /* Write back buffer pointer */
278 pinfo->rx_cur = (volatile cbd_t *) bdp;
279
280 /* activate BH processing */
281 tty_flip_buffer_push(tty);
282
283 return;
284
285 /* Error processing */
286
287 handle_error:
288 /* Statistics */
289 if (status & BD_SC_BR)
290 port->icount.brk++;
291 if (status & BD_SC_PR)
292 port->icount.parity++;
293 if (status & BD_SC_FR)
294 port->icount.frame++;
295 if (status & BD_SC_OV)
296 port->icount.overrun++;
297
298 /* Mask out ignored conditions */
299 status &= port->read_status_mask;
300
301 /* Handle the remaining ones */
302 if (status & BD_SC_BR)
303 flg = TTY_BREAK;
304 else if (status & BD_SC_PR)
305 flg = TTY_PARITY;
306 else if (status & BD_SC_FR)
307 flg = TTY_FRAME;
308
309 /* overrun does not affect the current character ! */
310 if (status & BD_SC_OV) {
311 ch = 0;
312 flg = TTY_OVERRUN;
313 /* We skip this buffer */
314 /* CHECK: Is really nothing senseful there */
315 /* ASSUMPTION: it contains nothing valid */
316 i = 0;
317 }
318#ifdef SUPPORT_SYSRQ
319 port->sysrq = 0;
320#endif
321 goto error_return;
322}
323
324/*
325 * Asynchron mode interrupt handler
326 */
327static irqreturn_t cpm_uart_int(int irq, void *data, struct pt_regs *regs)
328{
329 u8 events;
330 struct uart_port *port = (struct uart_port *)data;
331 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
332 volatile smc_t *smcp = pinfo->smcp;
333 volatile scc_t *sccp = pinfo->sccp;
334
335 pr_debug("CPM uart[%d]:IRQ\n", port->line);
336
337 if (IS_SMC(pinfo)) {
338 events = smcp->smc_smce;
339 if (events & SMCM_BRKE)
340 uart_handle_break(port);
341 if (events & SMCM_RX)
342 cpm_uart_int_rx(port, regs);
343 if (events & SMCM_TX)
344 cpm_uart_int_tx(port, regs);
345 smcp->smc_smce = events;
346 } else {
347 events = sccp->scc_scce;
348 if (events & UART_SCCM_BRKE)
349 uart_handle_break(port);
350 if (events & UART_SCCM_RX)
351 cpm_uart_int_rx(port, regs);
352 if (events & UART_SCCM_TX)
353 cpm_uart_int_tx(port, regs);
354 sccp->scc_scce = events;
355 }
356 return (events) ? IRQ_HANDLED : IRQ_NONE;
357}
358
359static int cpm_uart_startup(struct uart_port *port)
360{
361 int retval;
362 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
363
364 pr_debug("CPM uart[%d]:startup\n", port->line);
365
366 /* Install interrupt handler. */
367 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
368 if (retval)
369 return retval;
370
371 /* Startup rx-int */
372 if (IS_SMC(pinfo)) {
373 pinfo->smcp->smc_smcm |= SMCM_RX;
374 pinfo->smcp->smc_smcmr |= SMCMR_REN;
375 } else {
376 pinfo->sccp->scc_sccm |= UART_SCCM_RX;
377 }
378
379 return 0;
380}
381
382/*
383 * Shutdown the uart
384 */
385static void cpm_uart_shutdown(struct uart_port *port)
386{
387 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
388 int line = pinfo - cpm_uart_ports;
389
390 pr_debug("CPM uart[%d]:shutdown\n", port->line);
391
392 /* free interrupt handler */
393 free_irq(port->irq, port);
394
395 /* If the port is not the console, disable Rx and Tx. */
396 if (!(pinfo->flags & FLAG_CONSOLE)) {
397 /* Stop uarts */
398 if (IS_SMC(pinfo)) {
399 volatile smc_t *smcp = pinfo->smcp;
400 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
401 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
402 } else {
403 volatile scc_t *sccp = pinfo->sccp;
404 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
405 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
406 }
407
408 /* Shut them really down and reinit buffer descriptors */
409 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
410 cpm_uart_initbd(pinfo);
411 }
412}
413
414static void cpm_uart_set_termios(struct uart_port *port,
415 struct termios *termios, struct termios *old)
416{
417 int baud;
418 unsigned long flags;
419 u16 cval, scval, prev_mode;
420 int bits, sbits;
421 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
422 volatile smc_t *smcp = pinfo->smcp;
423 volatile scc_t *sccp = pinfo->sccp;
424
425 pr_debug("CPM uart[%d]:set_termios\n", port->line);
426
427 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
428
429 /* Character length programmed into the mode register is the
430 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
431 * 1 or 2 stop bits, minus 1.
432 * The value 'bits' counts this for us.
433 */
434 cval = 0;
435 scval = 0;
436
437 /* byte size */
438 switch (termios->c_cflag & CSIZE) {
439 case CS5:
440 bits = 5;
441 break;
442 case CS6:
443 bits = 6;
444 break;
445 case CS7:
446 bits = 7;
447 break;
448 case CS8:
449 bits = 8;
450 break;
451 /* Never happens, but GCC is too dumb to figure it out */
452 default:
453 bits = 8;
454 break;
455 }
456 sbits = bits - 5;
457
458 if (termios->c_cflag & CSTOPB) {
459 cval |= SMCMR_SL; /* Two stops */
460 scval |= SCU_PSMR_SL;
461 bits++;
462 }
463
464 if (termios->c_cflag & PARENB) {
465 cval |= SMCMR_PEN;
466 scval |= SCU_PSMR_PEN;
467 bits++;
468 if (!(termios->c_cflag & PARODD)) {
469 cval |= SMCMR_PM_EVEN;
470 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
471 }
472 }
473
474 /*
475 * Set up parity check flag
476 */
477#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
478
479 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
480 if (termios->c_iflag & INPCK)
481 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
482 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
483 port->read_status_mask |= BD_SC_BR;
484
485 /*
486 * Characters to ignore
487 */
488 port->ignore_status_mask = 0;
489 if (termios->c_iflag & IGNPAR)
490 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
491 if (termios->c_iflag & IGNBRK) {
492 port->ignore_status_mask |= BD_SC_BR;
493 /*
494 * If we're ignore parity and break indicators, ignore
495 * overruns too. (For real raw support).
496 */
497 if (termios->c_iflag & IGNPAR)
498 port->ignore_status_mask |= BD_SC_OV;
499 }
500 /*
501 * !!! ignore all characters if CREAD is not set
502 */
503 if ((termios->c_cflag & CREAD) == 0)
504 port->read_status_mask &= ~BD_SC_EMPTY;
505
506 spin_lock_irqsave(&port->lock, flags);
507
508 /* Start bit has not been added (so don't, because we would just
509 * subtract it later), and we need to add one for the number of
510 * stops bits (there is always at least one).
511 */
512 bits++;
513 if (IS_SMC(pinfo)) {
514 /* Set the mode register. We want to keep a copy of the
515 * enables, because we want to put them back if they were
516 * present.
517 */
518 prev_mode = smcp->smc_smcmr;
519 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
520 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
521 } else {
522 sccp->scc_psmr = (sbits << 12) | scval;
523 }
524
525 cpm_set_brg(pinfo->brg - 1, baud);
526 spin_unlock_irqrestore(&port->lock, flags);
527
528}
529
530static const char *cpm_uart_type(struct uart_port *port)
531{
532 pr_debug("CPM uart[%d]:uart_type\n", port->line);
533
534 return port->type == PORT_CPM ? "CPM UART" : NULL;
535}
536
537/*
538 * verify the new serial_struct (for TIOCSSERIAL).
539 */
540static int cpm_uart_verify_port(struct uart_port *port,
541 struct serial_struct *ser)
542{
543 int ret = 0;
544
545 pr_debug("CPM uart[%d]:verify_port\n", port->line);
546
547 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
548 ret = -EINVAL;
549 if (ser->irq < 0 || ser->irq >= NR_IRQS)
550 ret = -EINVAL;
551 if (ser->baud_base < 9600)
552 ret = -EINVAL;
553 return ret;
554}
555
556/*
557 * Transmit characters, refill buffer descriptor, if possible
558 */
559static int cpm_uart_tx_pump(struct uart_port *port)
560{
561 volatile cbd_t *bdp;
562 unsigned char *p;
563 int count;
564 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
565 struct circ_buf *xmit = &port->info->xmit;
566
567 /* Handle xon/xoff */
568 if (port->x_char) {
569 /* Pick next descriptor and fill from buffer */
570 bdp = pinfo->tx_cur;
571
572 p = bus_to_virt(bdp->cbd_bufaddr);
573 *p++ = xmit->buf[xmit->tail];
574 bdp->cbd_datlen = 1;
575 bdp->cbd_sc |= BD_SC_READY;
576 /* Get next BD. */
577 if (bdp->cbd_sc & BD_SC_WRAP)
578 bdp = pinfo->tx_bd_base;
579 else
580 bdp++;
581 pinfo->tx_cur = bdp;
582
583 port->icount.tx++;
584 port->x_char = 0;
585 return 1;
586 }
587
588 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
589 cpm_uart_stop_tx(port, 0);
590 return 0;
591 }
592
593 /* Pick next descriptor and fill from buffer */
594 bdp = pinfo->tx_cur;
595
596 while (!(bdp->cbd_sc & BD_SC_READY) && (xmit->tail != xmit->head)) {
597 count = 0;
598 p = bus_to_virt(bdp->cbd_bufaddr);
599 while (count < pinfo->tx_fifosize) {
600 *p++ = xmit->buf[xmit->tail];
601 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
602 port->icount.tx++;
603 count++;
604 if (xmit->head == xmit->tail)
605 break;
606 }
607 bdp->cbd_datlen = count;
608 bdp->cbd_sc |= BD_SC_READY;
609 /* Get next BD. */
610 if (bdp->cbd_sc & BD_SC_WRAP)
611 bdp = pinfo->tx_bd_base;
612 else
613 bdp++;
614 }
615 pinfo->tx_cur = bdp;
616
617 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
618 uart_write_wakeup(port);
619
620 if (uart_circ_empty(xmit)) {
621 cpm_uart_stop_tx(port, 0);
622 return 0;
623 }
624
625 return 1;
626}
627
628/*
629 * init buffer descriptors
630 */
631static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
632{
633 int i;
634 u8 *mem_addr;
635 volatile cbd_t *bdp;
636
637 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
638
639 /* Set the physical address of the host memory
640 * buffers in the buffer descriptors, and the
641 * virtual address for us to work with.
642 */
643 mem_addr = pinfo->mem_addr;
644 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
645 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
646 bdp->cbd_bufaddr = virt_to_bus(mem_addr);
647 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
648 mem_addr += pinfo->rx_fifosize;
649 }
650
651 bdp->cbd_bufaddr = virt_to_bus(mem_addr);
652 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
653
654 /* Set the physical address of the host memory
655 * buffers in the buffer descriptors, and the
656 * virtual address for us to work with.
657 */
658 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
659 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
660 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
661 bdp->cbd_bufaddr = virt_to_bus(mem_addr);
662 bdp->cbd_sc = BD_SC_INTRPT;
663 mem_addr += pinfo->tx_fifosize;
664 }
665
666 bdp->cbd_bufaddr = virt_to_bus(mem_addr);
667 bdp->cbd_sc = BD_SC_WRAP | BD_SC_INTRPT;
668}
669
670static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
671{
672 int line = pinfo - cpm_uart_ports;
673 volatile scc_t *scp;
674 volatile scc_uart_t *sup;
675
676 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
677
678 scp = pinfo->sccp;
679 sup = pinfo->sccup;
680
681 /* Store address */
682 pinfo->sccup->scc_genscc.scc_rbase = (unsigned char *)pinfo->rx_bd_base - DPRAM_BASE;
683 pinfo->sccup->scc_genscc.scc_tbase = (unsigned char *)pinfo->tx_bd_base - DPRAM_BASE;
684
685 /* Set up the uart parameters in the
686 * parameter ram.
687 */
688
689 cpm_set_scc_fcr(sup);
690
691 sup->scc_genscc.scc_mrblr = pinfo->rx_fifosize;
692 sup->scc_maxidl = pinfo->rx_fifosize;
693 sup->scc_brkcr = 1;
694 sup->scc_parec = 0;
695 sup->scc_frmec = 0;
696 sup->scc_nosec = 0;
697 sup->scc_brkec = 0;
698 sup->scc_uaddr1 = 0;
699 sup->scc_uaddr2 = 0;
700 sup->scc_toseq = 0;
701 sup->scc_char1 = 0x8000;
702 sup->scc_char2 = 0x8000;
703 sup->scc_char3 = 0x8000;
704 sup->scc_char4 = 0x8000;
705 sup->scc_char5 = 0x8000;
706 sup->scc_char6 = 0x8000;
707 sup->scc_char7 = 0x8000;
708 sup->scc_char8 = 0x8000;
709 sup->scc_rccm = 0xc0ff;
710
711 /* Send the CPM an initialize command.
712 */
713 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
714
715 /* Set UART mode, 8 bit, no parity, one stop.
716 * Enable receive and transmit.
717 */
718 scp->scc_gsmrh = 0;
719 scp->scc_gsmrl =
720 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
721
722 /* Enable rx interrupts and clear all pending events. */
723 scp->scc_sccm = 0;
724 scp->scc_scce = 0xffff;
725 scp->scc_dsr = 0x7e7e;
726 scp->scc_psmr = 0x3000;
727
728 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
729}
730
731static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
732{
733 int line = pinfo - cpm_uart_ports;
734 volatile smc_t *sp;
735 volatile smc_uart_t *up;
736
737 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
738
739 sp = pinfo->smcp;
740 up = pinfo->smcup;
741
742 /* Store address */
743 pinfo->smcup->smc_rbase = (u_char *)pinfo->rx_bd_base - DPRAM_BASE;
744 pinfo->smcup->smc_tbase = (u_char *)pinfo->tx_bd_base - DPRAM_BASE;
745
746/*
747 * In case SMC1 is being relocated...
748 */
749#if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
750 up->smc_rbptr = pinfo->smcup->smc_rbase;
751 up->smc_tbptr = pinfo->smcup->smc_tbase;
752 up->smc_rstate = 0;
753 up->smc_tstate = 0;
754 up->smc_brkcr = 1; /* number of break chars */
755 up->smc_brkec = 0;
756#endif
757
758 /* Set up the uart parameters in the
759 * parameter ram.
760 */
761 cpm_set_smc_fcr(up);
762
763 /* Using idle charater time requires some additional tuning. */
764 up->smc_mrblr = pinfo->rx_fifosize;
765 up->smc_maxidl = pinfo->rx_fifosize;
766 up->smc_brkcr = 1;
767
768 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
769
770 /* Set UART mode, 8 bit, no parity, one stop.
771 * Enable receive and transmit.
772 */
773 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
774
775 /* Enable only rx interrupts clear all pending events. */
776 sp->smc_smcm = 0;
777 sp->smc_smce = 0xff;
778
779 sp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
780}
781
782/*
783 * Initialize port. This is called from early_console stuff
784 * so we have to be careful here !
785 */
786static int cpm_uart_request_port(struct uart_port *port)
787{
788 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
789 int ret;
790
791 pr_debug("CPM uart[%d]:request port\n", port->line);
792
793 if (pinfo->flags & FLAG_CONSOLE)
794 return 0;
795
796 /*
797 * Setup any port IO, connect any baud rate generators,
798 * etc. This is expected to be handled by board
799 * dependant code
800 */
801 if (pinfo->set_lineif)
802 pinfo->set_lineif(pinfo);
803
804 if (IS_SMC(pinfo)) {
805 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
806 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
807 } else {
808 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
809 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
810 }
811
812 ret = cpm_uart_allocbuf(pinfo, 0);
813
814 if (ret)
815 return ret;
816
817 cpm_uart_initbd(pinfo);
818
819 return 0;
820}
821
822static void cpm_uart_release_port(struct uart_port *port)
823{
824 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
825
826 if (!(pinfo->flags & FLAG_CONSOLE))
827 cpm_uart_freebuf(pinfo);
828}
829
830/*
831 * Configure/autoconfigure the port.
832 */
833static void cpm_uart_config_port(struct uart_port *port, int flags)
834{
835 pr_debug("CPM uart[%d]:config_port\n", port->line);
836
837 if (flags & UART_CONFIG_TYPE) {
838 port->type = PORT_CPM;
839 cpm_uart_request_port(port);
840 }
841}
842static struct uart_ops cpm_uart_pops = {
843 .tx_empty = cpm_uart_tx_empty,
844 .set_mctrl = cpm_uart_set_mctrl,
845 .get_mctrl = cpm_uart_get_mctrl,
846 .stop_tx = cpm_uart_stop_tx,
847 .start_tx = cpm_uart_start_tx,
848 .stop_rx = cpm_uart_stop_rx,
849 .enable_ms = cpm_uart_enable_ms,
850 .break_ctl = cpm_uart_break_ctl,
851 .startup = cpm_uart_startup,
852 .shutdown = cpm_uart_shutdown,
853 .set_termios = cpm_uart_set_termios,
854 .type = cpm_uart_type,
855 .release_port = cpm_uart_release_port,
856 .request_port = cpm_uart_request_port,
857 .config_port = cpm_uart_config_port,
858 .verify_port = cpm_uart_verify_port,
859};
860
861struct uart_cpm_port cpm_uart_ports[UART_NR] = {
862 [UART_SMC1] = {
863 .port = {
864 .irq = SMC1_IRQ,
865 .ops = &cpm_uart_pops,
866 .iotype = SERIAL_IO_MEM,
867 .lock = SPIN_LOCK_UNLOCKED,
868 },
869 .flags = FLAG_SMC,
870 .tx_nrfifos = TX_NUM_FIFO,
871 .tx_fifosize = TX_BUF_SIZE,
872 .rx_nrfifos = RX_NUM_FIFO,
873 .rx_fifosize = RX_BUF_SIZE,
874 .set_lineif = smc1_lineif,
875 },
876 [UART_SMC2] = {
877 .port = {
878 .irq = SMC2_IRQ,
879 .ops = &cpm_uart_pops,
880 .iotype = SERIAL_IO_MEM,
881 .lock = SPIN_LOCK_UNLOCKED,
882 },
883 .flags = FLAG_SMC,
884 .tx_nrfifos = TX_NUM_FIFO,
885 .tx_fifosize = TX_BUF_SIZE,
886 .rx_nrfifos = RX_NUM_FIFO,
887 .rx_fifosize = RX_BUF_SIZE,
888 .set_lineif = smc2_lineif,
889#ifdef CONFIG_SERIAL_CPM_ALT_SMC2
890 .is_portb = 1,
891#endif
892 },
893 [UART_SCC1] = {
894 .port = {
895 .irq = SCC1_IRQ,
896 .ops = &cpm_uart_pops,
897 .iotype = SERIAL_IO_MEM,
898 .lock = SPIN_LOCK_UNLOCKED,
899 },
900 .tx_nrfifos = TX_NUM_FIFO,
901 .tx_fifosize = TX_BUF_SIZE,
902 .rx_nrfifos = RX_NUM_FIFO,
903 .rx_fifosize = RX_BUF_SIZE,
904 .set_lineif = scc1_lineif,
905 },
906 [UART_SCC2] = {
907 .port = {
908 .irq = SCC2_IRQ,
909 .ops = &cpm_uart_pops,
910 .iotype = SERIAL_IO_MEM,
911 .lock = SPIN_LOCK_UNLOCKED,
912 },
913 .tx_nrfifos = TX_NUM_FIFO,
914 .tx_fifosize = TX_BUF_SIZE,
915 .rx_nrfifos = RX_NUM_FIFO,
916 .rx_fifosize = RX_BUF_SIZE,
917 .set_lineif = scc2_lineif,
918 },
919 [UART_SCC3] = {
920 .port = {
921 .irq = SCC3_IRQ,
922 .ops = &cpm_uart_pops,
923 .iotype = SERIAL_IO_MEM,
924 .lock = SPIN_LOCK_UNLOCKED,
925 },
926 .tx_nrfifos = TX_NUM_FIFO,
927 .tx_fifosize = TX_BUF_SIZE,
928 .rx_nrfifos = RX_NUM_FIFO,
929 .rx_fifosize = RX_BUF_SIZE,
930 .set_lineif = scc3_lineif,
931 },
932 [UART_SCC4] = {
933 .port = {
934 .irq = SCC4_IRQ,
935 .ops = &cpm_uart_pops,
936 .iotype = SERIAL_IO_MEM,
937 .lock = SPIN_LOCK_UNLOCKED,
938 },
939 .tx_nrfifos = TX_NUM_FIFO,
940 .tx_fifosize = TX_BUF_SIZE,
941 .rx_nrfifos = RX_NUM_FIFO,
942 .rx_fifosize = RX_BUF_SIZE,
943 .set_lineif = scc4_lineif,
944 },
945};
946
947#ifdef CONFIG_SERIAL_CPM_CONSOLE
948/*
949 * Print a string to the serial port trying not to disturb
950 * any possible real use of the port...
951 *
952 * Note that this is called with interrupts already disabled
953 */
954static void cpm_uart_console_write(struct console *co, const char *s,
955 u_int count)
956{
957 struct uart_cpm_port *pinfo =
958 &cpm_uart_ports[cpm_uart_port_map[co->index]];
959 unsigned int i;
960 volatile cbd_t *bdp, *bdbase;
961 volatile unsigned char *cp;
962
963 /* Get the address of the host memory buffer.
964 */
965 bdp = pinfo->tx_cur;
966 bdbase = pinfo->tx_bd_base;
967
968 /*
969 * Now, do each character. This is not as bad as it looks
970 * since this is a holding FIFO and not a transmitting FIFO.
971 * We could add the complexity of filling the entire transmit
972 * buffer, but we would just wait longer between accesses......
973 */
974 for (i = 0; i < count; i++, s++) {
975 /* Wait for transmitter fifo to empty.
976 * Ready indicates output is ready, and xmt is doing
977 * that, not that it is ready for us to send.
978 */
979 while ((bdp->cbd_sc & BD_SC_READY) != 0)
980 ;
981
982 /* Send the character out.
983 * If the buffer address is in the CPM DPRAM, don't
984 * convert it.
985 */
986 if ((uint) (bdp->cbd_bufaddr) > (uint) CPM_ADDR)
987 cp = (unsigned char *) (bdp->cbd_bufaddr);
988 else
989 cp = bus_to_virt(bdp->cbd_bufaddr);
990
991 *cp = *s;
992
993 bdp->cbd_datlen = 1;
994 bdp->cbd_sc |= BD_SC_READY;
995
996 if (bdp->cbd_sc & BD_SC_WRAP)
997 bdp = bdbase;
998 else
999 bdp++;
1000
1001 /* if a LF, also do CR... */
1002 if (*s == 10) {
1003 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1004 ;
1005
1006 if ((uint) (bdp->cbd_bufaddr) > (uint) CPM_ADDR)
1007 cp = (unsigned char *) (bdp->cbd_bufaddr);
1008 else
1009 cp = bus_to_virt(bdp->cbd_bufaddr);
1010
1011 *cp = 13;
1012 bdp->cbd_datlen = 1;
1013 bdp->cbd_sc |= BD_SC_READY;
1014
1015 if (bdp->cbd_sc & BD_SC_WRAP)
1016 bdp = bdbase;
1017 else
1018 bdp++;
1019 }
1020 }
1021
1022 /*
1023 * Finally, Wait for transmitter & holding register to empty
1024 * and restore the IER
1025 */
1026 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1027 ;
1028
1029 pinfo->tx_cur = (volatile cbd_t *) bdp;
1030}
1031
1032/*
1033 * Setup console. Be careful is called early !
1034 */
1035static int __init cpm_uart_console_setup(struct console *co, char *options)
1036{
1037 struct uart_port *port;
1038 struct uart_cpm_port *pinfo;
1039 int baud = 38400;
1040 int bits = 8;
1041 int parity = 'n';
1042 int flow = 'n';
1043 int ret;
1044
1045 port =
1046 (struct uart_port *)&cpm_uart_ports[cpm_uart_port_map[co->index]];
1047 pinfo = (struct uart_cpm_port *)port;
1048
1049 pinfo->flags |= FLAG_CONSOLE;
1050
1051 if (options) {
1052 uart_parse_options(options, &baud, &parity, &bits, &flow);
1053 } else {
1054 bd_t *bd = (bd_t *) __res;
1055
1056 if (bd->bi_baudrate)
1057 baud = bd->bi_baudrate;
1058 else
1059 baud = 9600;
1060 }
1061
1062 /*
1063 * Setup any port IO, connect any baud rate generators,
1064 * etc. This is expected to be handled by board
1065 * dependant code
1066 */
1067 if (pinfo->set_lineif)
1068 pinfo->set_lineif(pinfo);
1069
1070 if (IS_SMC(pinfo)) {
1071 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
1072 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
1073 } else {
1074 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
1075 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1076 }
1077
1078 ret = cpm_uart_allocbuf(pinfo, 1);
1079
1080 if (ret)
1081 return ret;
1082
1083 cpm_uart_initbd(pinfo);
1084
1085 if (IS_SMC(pinfo))
1086 cpm_uart_init_smc(pinfo);
1087 else
1088 cpm_uart_init_scc(pinfo);
1089
1090 uart_set_options(port, co, baud, parity, bits, flow);
1091
1092 return 0;
1093}
1094
1095extern struct uart_driver cpm_reg;
1096static struct console cpm_scc_uart_console = {
1097 .name "ttyCPM",
1098 .write cpm_uart_console_write,
1099 .device uart_console_device,
1100 .setup cpm_uart_console_setup,
1101 .flags CON_PRINTBUFFER,
1102 .index -1,
1103 .data = &cpm_reg,
1104};
1105
1106int __init cpm_uart_console_init(void)
1107{
1108 int ret = cpm_uart_init_portdesc();
1109
1110 if (!ret)
1111 register_console(&cpm_scc_uart_console);
1112 return ret;
1113}
1114
1115console_initcall(cpm_uart_console_init);
1116
1117#define CPM_UART_CONSOLE &cpm_scc_uart_console
1118#else
1119#define CPM_UART_CONSOLE NULL
1120#endif
1121
1122static struct uart_driver cpm_reg = {
1123 .owner = THIS_MODULE,
1124 .driver_name = "ttyCPM",
1125 .dev_name = "ttyCPM",
1126 .major = SERIAL_CPM_MAJOR,
1127 .minor = SERIAL_CPM_MINOR,
1128 .cons = CPM_UART_CONSOLE,
1129};
1130
1131static int __init cpm_uart_init(void)
1132{
1133 int ret, i;
1134
1135 printk(KERN_INFO "Serial: CPM driver $Revision: 0.01 $\n");
1136
1137#ifndef CONFIG_SERIAL_CPM_CONSOLE
1138 ret = cpm_uart_init_portdesc();
1139 if (ret)
1140 return ret;
1141#endif
1142
1143 cpm_reg.nr = cpm_uart_nr;
1144 ret = uart_register_driver(&cpm_reg);
1145
1146 if (ret)
1147 return ret;
1148
1149 for (i = 0; i < cpm_uart_nr; i++) {
1150 int con = cpm_uart_port_map[i];
1151 cpm_uart_ports[con].port.line = i;
1152 cpm_uart_ports[con].port.flags = UPF_BOOT_AUTOCONF;
1153 uart_add_one_port(&cpm_reg, &cpm_uart_ports[con].port);
1154 }
1155
1156 return ret;
1157}
1158
1159static void __exit cpm_uart_exit(void)
1160{
1161 int i;
1162
1163 for (i = 0; i < cpm_uart_nr; i++) {
1164 int con = cpm_uart_port_map[i];
1165 uart_remove_one_port(&cpm_reg, &cpm_uart_ports[con].port);
1166 }
1167
1168 uart_unregister_driver(&cpm_reg);
1169}
1170
1171module_init(cpm_uart_init);
1172module_exit(cpm_uart_exit);
1173
1174MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1175MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1176MODULE_LICENSE("GPL");
1177MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);
diff --git a/drivers/serial/cpm_uart/cpm_uart_cpm1.c b/drivers/serial/cpm_uart/cpm_uart_cpm1.c
new file mode 100644
index 000000000000..de26cf7b003c
--- /dev/null
+++ b/drivers/serial/cpm_uart/cpm_uart_cpm1.c
@@ -0,0 +1,290 @@
1/*
2 * linux/drivers/serial/cpm_uart.c
3 *
4 * Driver for CPM (SCC/SMC) serial ports; CPM1 definitions
5 *
6 * Maintainer: Kumar Gala (kumar.gala@freescale.com) (CPM2)
7 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
8 *
9 * Copyright (C) 2004 Freescale Semiconductor, Inc.
10 * (C) 2004 Intracom, S.A.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/config.h>
29#include <linux/module.h>
30#include <linux/tty.h>
31#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/serial.h>
34#include <linux/console.h>
35#include <linux/sysrq.h>
36#include <linux/device.h>
37#include <linux/bootmem.h>
38#include <linux/dma-mapping.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42
43#include <linux/serial_core.h>
44#include <linux/kernel.h>
45
46#include "cpm_uart.h"
47
48/**************************************************************/
49
50void cpm_line_cr_cmd(int line, int cmd)
51{
52 ushort val;
53 volatile cpm8xx_t *cp = cpmp;
54
55 switch (line) {
56 case UART_SMC1:
57 val = mk_cr_cmd(CPM_CR_CH_SMC1, cmd) | CPM_CR_FLG;
58 break;
59 case UART_SMC2:
60 val = mk_cr_cmd(CPM_CR_CH_SMC2, cmd) | CPM_CR_FLG;
61 break;
62 case UART_SCC1:
63 val = mk_cr_cmd(CPM_CR_CH_SCC1, cmd) | CPM_CR_FLG;
64 break;
65 case UART_SCC2:
66 val = mk_cr_cmd(CPM_CR_CH_SCC2, cmd) | CPM_CR_FLG;
67 break;
68 case UART_SCC3:
69 val = mk_cr_cmd(CPM_CR_CH_SCC3, cmd) | CPM_CR_FLG;
70 break;
71 case UART_SCC4:
72 val = mk_cr_cmd(CPM_CR_CH_SCC4, cmd) | CPM_CR_FLG;
73 break;
74 default:
75 return;
76
77 }
78 cp->cp_cpcr = val;
79 while (cp->cp_cpcr & CPM_CR_FLG) ;
80}
81
82void smc1_lineif(struct uart_cpm_port *pinfo)
83{
84 volatile cpm8xx_t *cp = cpmp;
85 unsigned int iobits = 0x000000c0;
86
87 if (!pinfo->is_portb) {
88 cp->cp_pbpar |= iobits;
89 cp->cp_pbdir &= ~iobits;
90 cp->cp_pbodr &= ~iobits;
91 } else {
92 ((immap_t *)IMAP_ADDR)->im_ioport.iop_papar |= iobits;
93 ((immap_t *)IMAP_ADDR)->im_ioport.iop_padir &= ~iobits;
94 ((immap_t *)IMAP_ADDR)->im_ioport.iop_paodr &= ~iobits;
95 }
96
97 pinfo->brg = 1;
98}
99
100void smc2_lineif(struct uart_cpm_port *pinfo)
101{
102 /* XXX SMC2: insert port configuration here */
103 pinfo->brg = 2;
104}
105
106void scc1_lineif(struct uart_cpm_port *pinfo)
107{
108 /* XXX SCC1: insert port configuration here */
109 pinfo->brg = 1;
110}
111
112void scc2_lineif(struct uart_cpm_port *pinfo)
113{
114 /* XXX SCC2: insert port configuration here */
115 pinfo->brg = 2;
116}
117
118void scc3_lineif(struct uart_cpm_port *pinfo)
119{
120 /* XXX SCC3: insert port configuration here */
121 pinfo->brg = 3;
122}
123
124void scc4_lineif(struct uart_cpm_port *pinfo)
125{
126 /* XXX SCC4: insert port configuration here */
127 pinfo->brg = 4;
128}
129
130/*
131 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
132 * receive buffer descriptors from dual port ram, and a character
133 * buffer area from host mem. If we are allocating for the console we need
134 * to do it from bootmem
135 */
136int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
137{
138 int dpmemsz, memsz;
139 u8 *dp_mem;
140 uint dp_offset;
141 u8 *mem_addr;
142 dma_addr_t dma_addr = 0;
143
144 pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
145
146 dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
147 dp_offset = cpm_dpalloc(dpmemsz, 8);
148 if (IS_DPERR(dp_offset)) {
149 printk(KERN_ERR
150 "cpm_uart_cpm1.c: could not allocate buffer descriptors\n");
151 return -ENOMEM;
152 }
153 dp_mem = cpm_dpram_addr(dp_offset);
154
155 memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
156 L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
157 if (is_con) {
158 mem_addr = (u8 *) m8xx_cpm_hostalloc(memsz);
159 dma_addr = 0;
160 } else
161 mem_addr = dma_alloc_coherent(NULL, memsz, &dma_addr,
162 GFP_KERNEL);
163
164 if (mem_addr == NULL) {
165 cpm_dpfree(dp_offset);
166 printk(KERN_ERR
167 "cpm_uart_cpm1.c: could not allocate coherent memory\n");
168 return -ENOMEM;
169 }
170
171 pinfo->dp_addr = dp_offset;
172 pinfo->mem_addr = mem_addr;
173 pinfo->dma_addr = dma_addr;
174
175 pinfo->rx_buf = mem_addr;
176 pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
177 * pinfo->rx_fifosize);
178
179 pinfo->rx_bd_base = (volatile cbd_t *)dp_mem;
180 pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
181
182 return 0;
183}
184
185void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
186{
187 dma_free_coherent(NULL, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
188 pinfo->rx_fifosize) +
189 L1_CACHE_ALIGN(pinfo->tx_nrfifos *
190 pinfo->tx_fifosize), pinfo->mem_addr,
191 pinfo->dma_addr);
192
193 cpm_dpfree(pinfo->dp_addr);
194}
195
196/* Setup any dynamic params in the uart desc */
197int cpm_uart_init_portdesc(void)
198{
199 pr_debug("CPM uart[-]:init portdesc\n");
200
201 cpm_uart_nr = 0;
202#ifdef CONFIG_SERIAL_CPM_SMC1
203 cpm_uart_ports[UART_SMC1].smcp = &cpmp->cp_smc[0];
204/*
205 * Is SMC1 being relocated?
206 */
207# ifdef CONFIG_I2C_SPI_SMC1_UCODE_PATCH
208 cpm_uart_ports[UART_SMC1].smcup =
209 (smc_uart_t *) & cpmp->cp_dparam[0x3C0];
210# else
211 cpm_uart_ports[UART_SMC1].smcup =
212 (smc_uart_t *) & cpmp->cp_dparam[PROFF_SMC1];
213# endif
214 cpm_uart_ports[UART_SMC1].port.mapbase =
215 (unsigned long)&cpmp->cp_smc[0];
216 cpm_uart_ports[UART_SMC1].smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
217 cpm_uart_ports[UART_SMC1].smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
218 cpm_uart_ports[UART_SMC1].port.uartclk = (((bd_t *) __res)->bi_intfreq);
219 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC1;
220#endif
221
222#ifdef CONFIG_SERIAL_CPM_SMC2
223 cpm_uart_ports[UART_SMC2].smcp = &cpmp->cp_smc[1];
224 cpm_uart_ports[UART_SMC2].smcup =
225 (smc_uart_t *) & cpmp->cp_dparam[PROFF_SMC2];
226 cpm_uart_ports[UART_SMC2].port.mapbase =
227 (unsigned long)&cpmp->cp_smc[1];
228 cpm_uart_ports[UART_SMC2].smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
229 cpm_uart_ports[UART_SMC2].smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
230 cpm_uart_ports[UART_SMC2].port.uartclk = (((bd_t *) __res)->bi_intfreq);
231 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC2;
232#endif
233
234#ifdef CONFIG_SERIAL_CPM_SCC1
235 cpm_uart_ports[UART_SCC1].sccp = &cpmp->cp_scc[0];
236 cpm_uart_ports[UART_SCC1].sccup =
237 (scc_uart_t *) & cpmp->cp_dparam[PROFF_SCC1];
238 cpm_uart_ports[UART_SCC1].port.mapbase =
239 (unsigned long)&cpmp->cp_scc[0];
240 cpm_uart_ports[UART_SCC1].sccp->scc_sccm &=
241 ~(UART_SCCM_TX | UART_SCCM_RX);
242 cpm_uart_ports[UART_SCC1].sccp->scc_gsmrl &=
243 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
244 cpm_uart_ports[UART_SCC1].port.uartclk = (((bd_t *) __res)->bi_intfreq);
245 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC1;
246#endif
247
248#ifdef CONFIG_SERIAL_CPM_SCC2
249 cpm_uart_ports[UART_SCC2].sccp = &cpmp->cp_scc[1];
250 cpm_uart_ports[UART_SCC2].sccup =
251 (scc_uart_t *) & cpmp->cp_dparam[PROFF_SCC2];
252 cpm_uart_ports[UART_SCC2].port.mapbase =
253 (unsigned long)&cpmp->cp_scc[1];
254 cpm_uart_ports[UART_SCC2].sccp->scc_sccm &=
255 ~(UART_SCCM_TX | UART_SCCM_RX);
256 cpm_uart_ports[UART_SCC2].sccp->scc_gsmrl &=
257 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
258 cpm_uart_ports[UART_SCC2].port.uartclk = (((bd_t *) __res)->bi_intfreq);
259 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC2;
260#endif
261
262#ifdef CONFIG_SERIAL_CPM_SCC3
263 cpm_uart_ports[UART_SCC3].sccp = &cpmp->cp_scc[2];
264 cpm_uart_ports[UART_SCC3].sccup =
265 (scc_uart_t *) & cpmp->cp_dparam[PROFF_SCC3];
266 cpm_uart_ports[UART_SCC3].port.mapbase =
267 (unsigned long)&cpmp->cp_scc[2];
268 cpm_uart_ports[UART_SCC3].sccp->scc_sccm &=
269 ~(UART_SCCM_TX | UART_SCCM_RX);
270 cpm_uart_ports[UART_SCC3].sccp->scc_gsmrl &=
271 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
272 cpm_uart_ports[UART_SCC3].port.uartclk = (((bd_t *) __res)->bi_intfreq);
273 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC3;
274#endif
275
276#ifdef CONFIG_SERIAL_CPM_SCC4
277 cpm_uart_ports[UART_SCC4].sccp = &cpmp->cp_scc[3];
278 cpm_uart_ports[UART_SCC4].sccup =
279 (scc_uart_t *) & cpmp->cp_dparam[PROFF_SCC4];
280 cpm_uart_ports[UART_SCC4].port.mapbase =
281 (unsigned long)&cpmp->cp_scc[3];
282 cpm_uart_ports[UART_SCC4].sccp->scc_sccm &=
283 ~(UART_SCCM_TX | UART_SCCM_RX);
284 cpm_uart_ports[UART_SCC4].sccp->scc_gsmrl &=
285 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
286 cpm_uart_ports[UART_SCC4].port.uartclk = (((bd_t *) __res)->bi_intfreq);
287 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC4;
288#endif
289 return 0;
290}
diff --git a/drivers/serial/cpm_uart/cpm_uart_cpm1.h b/drivers/serial/cpm_uart/cpm_uart_cpm1.h
new file mode 100644
index 000000000000..5d867ab581b7
--- /dev/null
+++ b/drivers/serial/cpm_uart/cpm_uart_cpm1.h
@@ -0,0 +1,45 @@
1/*
2 * linux/drivers/serial/cpm_uart_cpm1.h
3 *
4 * Driver for CPM (SCC/SMC) serial ports
5 *
6 * definitions for cpm1
7 *
8 */
9
10#ifndef CPM_UART_CPM1_H
11#define CPM_UART_CPM1_H
12
13#include <asm/commproc.h>
14
15/* defines for IRQs */
16#define SMC1_IRQ (CPM_IRQ_OFFSET + CPMVEC_SMC1)
17#define SMC2_IRQ (CPM_IRQ_OFFSET + CPMVEC_SMC2)
18#define SCC1_IRQ (CPM_IRQ_OFFSET + CPMVEC_SCC1)
19#define SCC2_IRQ (CPM_IRQ_OFFSET + CPMVEC_SCC2)
20#define SCC3_IRQ (CPM_IRQ_OFFSET + CPMVEC_SCC3)
21#define SCC4_IRQ (CPM_IRQ_OFFSET + CPMVEC_SCC4)
22
23/* the CPM address */
24#define CPM_ADDR IMAP_ADDR
25
26static inline void cpm_set_brg(int brg, int baud)
27{
28 cpm_setbrg(brg, baud);
29}
30
31static inline void cpm_set_scc_fcr(volatile scc_uart_t * sup)
32{
33 sup->scc_genscc.scc_rfcr = SMC_EB;
34 sup->scc_genscc.scc_tfcr = SMC_EB;
35}
36
37static inline void cpm_set_smc_fcr(volatile smc_uart_t * up)
38{
39 up->smc_rfcr = SMC_EB;
40 up->smc_tfcr = SMC_EB;
41}
42
43#define DPRAM_BASE ((unsigned char *)&cpmp->cp_dpmem[0])
44
45#endif
diff --git a/drivers/serial/cpm_uart/cpm_uart_cpm2.c b/drivers/serial/cpm_uart/cpm_uart_cpm2.c
new file mode 100644
index 000000000000..b422c3abfba6
--- /dev/null
+++ b/drivers/serial/cpm_uart/cpm_uart_cpm2.c
@@ -0,0 +1,328 @@
1/*
2 * linux/drivers/serial/cpm_uart_cpm2.c
3 *
4 * Driver for CPM (SCC/SMC) serial ports; CPM2 definitions
5 *
6 * Maintainer: Kumar Gala (kumar.gala@freescale.com) (CPM2)
7 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
8 *
9 * Copyright (C) 2004 Freescale Semiconductor, Inc.
10 * (C) 2004 Intracom, S.A.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/config.h>
29#include <linux/module.h>
30#include <linux/tty.h>
31#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/serial.h>
34#include <linux/console.h>
35#include <linux/sysrq.h>
36#include <linux/device.h>
37#include <linux/bootmem.h>
38#include <linux/dma-mapping.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42
43#include <linux/serial_core.h>
44#include <linux/kernel.h>
45
46#include "cpm_uart.h"
47
48/**************************************************************/
49
50void cpm_line_cr_cmd(int line, int cmd)
51{
52 volatile cpm_cpm2_t *cp = cpmp;
53 ulong val;
54
55 switch (line) {
56 case UART_SMC1:
57 val = mk_cr_cmd(CPM_CR_SMC1_PAGE, CPM_CR_SMC1_SBLOCK, 0,
58 cmd) | CPM_CR_FLG;
59 break;
60 case UART_SMC2:
61 val = mk_cr_cmd(CPM_CR_SMC2_PAGE, CPM_CR_SMC2_SBLOCK, 0,
62 cmd) | CPM_CR_FLG;
63 break;
64 case UART_SCC1:
65 val = mk_cr_cmd(CPM_CR_SCC1_PAGE, CPM_CR_SCC1_SBLOCK, 0,
66 cmd) | CPM_CR_FLG;
67 break;
68 case UART_SCC2:
69 val = mk_cr_cmd(CPM_CR_SCC2_PAGE, CPM_CR_SCC2_SBLOCK, 0,
70 cmd) | CPM_CR_FLG;
71 break;
72 case UART_SCC3:
73 val = mk_cr_cmd(CPM_CR_SCC3_PAGE, CPM_CR_SCC3_SBLOCK, 0,
74 cmd) | CPM_CR_FLG;
75 break;
76 case UART_SCC4:
77 val = mk_cr_cmd(CPM_CR_SCC4_PAGE, CPM_CR_SCC4_SBLOCK, 0,
78 cmd) | CPM_CR_FLG;
79 break;
80 default:
81 return;
82
83 }
84 cp->cp_cpcr = val;
85 while (cp->cp_cpcr & CPM_CR_FLG) ;
86}
87
88void smc1_lineif(struct uart_cpm_port *pinfo)
89{
90 volatile iop_cpm2_t *io = &cpm2_immr->im_ioport;
91
92 /* SMC1 is only on port D */
93 io->iop_ppard |= 0x00c00000;
94 io->iop_pdird |= 0x00400000;
95 io->iop_pdird &= ~0x00800000;
96 io->iop_psord &= ~0x00c00000;
97
98 /* Wire BRG1 to SMC1 */
99 cpm2_immr->im_cpmux.cmx_smr &= 0x0f;
100 pinfo->brg = 1;
101}
102
103void smc2_lineif(struct uart_cpm_port *pinfo)
104{
105 volatile iop_cpm2_t *io = &cpm2_immr->im_ioport;
106
107 /* SMC2 is only on port A */
108 io->iop_ppara |= 0x00c00000;
109 io->iop_pdira |= 0x00400000;
110 io->iop_pdira &= ~0x00800000;
111 io->iop_psora &= ~0x00c00000;
112
113 /* Wire BRG2 to SMC2 */
114 cpm2_immr->im_cpmux.cmx_smr &= 0xf0;
115 pinfo->brg = 2;
116}
117
118void scc1_lineif(struct uart_cpm_port *pinfo)
119{
120 volatile iop_cpm2_t *io = &cpm2_immr->im_ioport;
121
122 /* Use Port D for SCC1 instead of other functions. */
123 io->iop_ppard |= 0x00000003;
124 io->iop_psord &= ~0x00000001; /* Rx */
125 io->iop_psord |= 0x00000002; /* Tx */
126 io->iop_pdird &= ~0x00000001; /* Rx */
127 io->iop_pdird |= 0x00000002; /* Tx */
128
129 /* Wire BRG1 to SCC1 */
130 cpm2_immr->im_cpmux.cmx_scr &= 0x00ffffff;
131 cpm2_immr->im_cpmux.cmx_scr |= 0x00000000;
132 pinfo->brg = 1;
133}
134
135void scc2_lineif(struct uart_cpm_port *pinfo)
136{
137 volatile iop_cpm2_t *io = &cpm2_immr->im_ioport;
138 io->iop_pparb |= 0x008b0000;
139 io->iop_pdirb |= 0x00880000;
140 io->iop_psorb |= 0x00880000;
141 io->iop_pdirb &= ~0x00030000;
142 io->iop_psorb &= ~0x00030000;
143 cpm2_immr->im_cpmux.cmx_scr &= 0xff00ffff;
144 cpm2_immr->im_cpmux.cmx_scr |= 0x00090000;
145 pinfo->brg = 2;
146}
147
148void scc3_lineif(struct uart_cpm_port *pinfo)
149{
150 volatile iop_cpm2_t *io = &cpm2_immr->im_ioport;
151 io->iop_pparb |= 0x008b0000;
152 io->iop_pdirb |= 0x00880000;
153 io->iop_psorb |= 0x00880000;
154 io->iop_pdirb &= ~0x00030000;
155 io->iop_psorb &= ~0x00030000;
156 cpm2_immr->im_cpmux.cmx_scr &= 0xffff00ff;
157 cpm2_immr->im_cpmux.cmx_scr |= 0x00001200;
158 pinfo->brg = 3;
159}
160
161void scc4_lineif(struct uart_cpm_port *pinfo)
162{
163 volatile iop_cpm2_t *io = &cpm2_immr->im_ioport;
164
165 io->iop_ppard |= 0x00000600;
166 io->iop_psord &= ~0x00000600; /* Tx/Rx */
167 io->iop_pdird &= ~0x00000200; /* Rx */
168 io->iop_pdird |= 0x00000400; /* Tx */
169
170 cpm2_immr->im_cpmux.cmx_scr &= 0xffffff00;
171 cpm2_immr->im_cpmux.cmx_scr |= 0x0000001b;
172 pinfo->brg = 4;
173}
174
175/*
176 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
177 * receive buffer descriptors from dual port ram, and a character
178 * buffer area from host mem. If we are allocating for the console we need
179 * to do it from bootmem
180 */
181int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
182{
183 int dpmemsz, memsz;
184 u8 *dp_mem;
185 uint dp_offset;
186 u8 *mem_addr;
187 dma_addr_t dma_addr = 0;
188
189 pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
190
191 dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
192 dp_offset = cpm_dpalloc(dpmemsz, 8);
193 if (IS_DPERR(dp_offset)) {
194 printk(KERN_ERR
195 "cpm_uart_cpm.c: could not allocate buffer descriptors\n");
196 return -ENOMEM;
197 }
198
199 dp_mem = cpm_dpram_addr(dp_offset);
200
201 memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
202 L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
203 if (is_con)
204 mem_addr = alloc_bootmem(memsz);
205 else
206 mem_addr = dma_alloc_coherent(NULL, memsz, &dma_addr,
207 GFP_KERNEL);
208
209 if (mem_addr == NULL) {
210 cpm_dpfree(dp_offset);
211 printk(KERN_ERR
212 "cpm_uart_cpm.c: could not allocate coherent memory\n");
213 return -ENOMEM;
214 }
215
216 pinfo->dp_addr = dp_offset;
217 pinfo->mem_addr = mem_addr;
218 pinfo->dma_addr = dma_addr;
219
220 pinfo->rx_buf = mem_addr;
221 pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
222 * pinfo->rx_fifosize);
223
224 pinfo->rx_bd_base = (volatile cbd_t *)dp_mem;
225 pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
226
227 return 0;
228}
229
230void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
231{
232 dma_free_coherent(NULL, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
233 pinfo->rx_fifosize) +
234 L1_CACHE_ALIGN(pinfo->tx_nrfifos *
235 pinfo->tx_fifosize), pinfo->mem_addr,
236 pinfo->dma_addr);
237
238 cpm_dpfree(pinfo->dp_addr);
239}
240
241/* Setup any dynamic params in the uart desc */
242int cpm_uart_init_portdesc(void)
243{
244 pr_debug("CPM uart[-]:init portdesc\n");
245
246 cpm_uart_nr = 0;
247#ifdef CONFIG_SERIAL_CPM_SMC1
248 cpm_uart_ports[UART_SMC1].smcp = (smc_t *) & cpm2_immr->im_smc[0];
249 cpm_uart_ports[UART_SMC1].smcup =
250 (smc_uart_t *) & cpm2_immr->im_dprambase[PROFF_SMC1];
251 cpm_uart_ports[UART_SMC1].port.mapbase =
252 (unsigned long)&cpm2_immr->im_smc[0];
253 cpm_uart_ports[UART_SMC1].smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
254 cpm_uart_ports[UART_SMC1].smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
255 cpm_uart_ports[UART_SMC1].port.uartclk = (((bd_t *) __res)->bi_intfreq);
256 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC1;
257#endif
258
259#ifdef CONFIG_SERIAL_CPM_SMC2
260 cpm_uart_ports[UART_SMC2].smcp = (smc_t *) & cpm2_immr->im_smc[1];
261 cpm_uart_ports[UART_SMC2].smcup =
262 (smc_uart_t *) & cpm2_immr->im_dprambase[PROFF_SMC2];
263 cpm_uart_ports[UART_SMC2].port.mapbase =
264 (unsigned long)&cpm2_immr->im_smc[1];
265 cpm_uart_ports[UART_SMC2].smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
266 cpm_uart_ports[UART_SMC2].smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
267 cpm_uart_ports[UART_SMC2].port.uartclk = (((bd_t *) __res)->bi_intfreq);
268 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC2;
269#endif
270
271#ifdef CONFIG_SERIAL_CPM_SCC1
272 cpm_uart_ports[UART_SCC1].sccp = (scc_t *) & cpm2_immr->im_scc[0];
273 cpm_uart_ports[UART_SCC1].sccup =
274 (scc_uart_t *) & cpm2_immr->im_dprambase[PROFF_SCC1];
275 cpm_uart_ports[UART_SCC1].port.mapbase =
276 (unsigned long)&cpm2_immr->im_scc[0];
277 cpm_uart_ports[UART_SCC1].sccp->scc_sccm &=
278 ~(UART_SCCM_TX | UART_SCCM_RX);
279 cpm_uart_ports[UART_SCC1].sccp->scc_gsmrl &=
280 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
281 cpm_uart_ports[UART_SCC1].port.uartclk = (((bd_t *) __res)->bi_intfreq);
282 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC1;
283#endif
284
285#ifdef CONFIG_SERIAL_CPM_SCC2
286 cpm_uart_ports[UART_SCC2].sccp = (scc_t *) & cpm2_immr->im_scc[1];
287 cpm_uart_ports[UART_SCC2].sccup =
288 (scc_uart_t *) & cpm2_immr->im_dprambase[PROFF_SCC2];
289 cpm_uart_ports[UART_SCC2].port.mapbase =
290 (unsigned long)&cpm2_immr->im_scc[1];
291 cpm_uart_ports[UART_SCC2].sccp->scc_sccm &=
292 ~(UART_SCCM_TX | UART_SCCM_RX);
293 cpm_uart_ports[UART_SCC2].sccp->scc_gsmrl &=
294 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
295 cpm_uart_ports[UART_SCC2].port.uartclk = (((bd_t *) __res)->bi_intfreq);
296 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC2;
297#endif
298
299#ifdef CONFIG_SERIAL_CPM_SCC3
300 cpm_uart_ports[UART_SCC3].sccp = (scc_t *) & cpm2_immr->im_scc[2];
301 cpm_uart_ports[UART_SCC3].sccup =
302 (scc_uart_t *) & cpm2_immr->im_dprambase[PROFF_SCC3];
303 cpm_uart_ports[UART_SCC3].port.mapbase =
304 (unsigned long)&cpm2_immr->im_scc[2];
305 cpm_uart_ports[UART_SCC3].sccp->scc_sccm &=
306 ~(UART_SCCM_TX | UART_SCCM_RX);
307 cpm_uart_ports[UART_SCC3].sccp->scc_gsmrl &=
308 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
309 cpm_uart_ports[UART_SCC3].port.uartclk = (((bd_t *) __res)->bi_intfreq);
310 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC3;
311#endif
312
313#ifdef CONFIG_SERIAL_CPM_SCC4
314 cpm_uart_ports[UART_SCC4].sccp = (scc_t *) & cpm2_immr->im_scc[3];
315 cpm_uart_ports[UART_SCC4].sccup =
316 (scc_uart_t *) & cpm2_immr->im_dprambase[PROFF_SCC4];
317 cpm_uart_ports[UART_SCC4].port.mapbase =
318 (unsigned long)&cpm2_immr->im_scc[3];
319 cpm_uart_ports[UART_SCC4].sccp->scc_sccm &=
320 ~(UART_SCCM_TX | UART_SCCM_RX);
321 cpm_uart_ports[UART_SCC4].sccp->scc_gsmrl &=
322 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
323 cpm_uart_ports[UART_SCC4].port.uartclk = (((bd_t *) __res)->bi_intfreq);
324 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC4;
325#endif
326
327 return 0;
328}
diff --git a/drivers/serial/cpm_uart/cpm_uart_cpm2.h b/drivers/serial/cpm_uart/cpm_uart_cpm2.h
new file mode 100644
index 000000000000..4793fecf8ece
--- /dev/null
+++ b/drivers/serial/cpm_uart/cpm_uart_cpm2.h
@@ -0,0 +1,45 @@
1/*
2 * linux/drivers/serial/cpm_uart_cpm2.h
3 *
4 * Driver for CPM (SCC/SMC) serial ports
5 *
6 * definitions for cpm2
7 *
8 */
9
10#ifndef CPM_UART_CPM2_H
11#define CPM_UART_CPM2_H
12
13#include <asm/cpm2.h>
14
15/* defines for IRQs */
16#define SMC1_IRQ SIU_INT_SMC1
17#define SMC2_IRQ SIU_INT_SMC2
18#define SCC1_IRQ SIU_INT_SCC1
19#define SCC2_IRQ SIU_INT_SCC2
20#define SCC3_IRQ SIU_INT_SCC3
21#define SCC4_IRQ SIU_INT_SCC4
22
23/* the CPM address */
24#define CPM_ADDR CPM_MAP_ADDR
25
26static inline void cpm_set_brg(int brg, int baud)
27{
28 cpm_setbrg(brg, baud);
29}
30
31static inline void cpm_set_scc_fcr(volatile scc_uart_t * sup)
32{
33 sup->scc_genscc.scc_rfcr = CPMFCR_GBL | CPMFCR_EB;
34 sup->scc_genscc.scc_tfcr = CPMFCR_GBL | CPMFCR_EB;
35}
36
37static inline void cpm_set_smc_fcr(volatile smc_uart_t * up)
38{
39 up->smc_rfcr = CPMFCR_GBL | CPMFCR_EB;
40 up->smc_tfcr = CPMFCR_GBL | CPMFCR_EB;
41}
42
43#define DPRAM_BASE ((unsigned char *)&cpm2_immr->im_dprambase[0])
44
45#endif
diff --git a/drivers/serial/crisv10.c b/drivers/serial/crisv10.c
new file mode 100644
index 000000000000..3da5494953af
--- /dev/null
+++ b/drivers/serial/crisv10.c
@@ -0,0 +1,5059 @@
1/* $Id: serial.c,v 1.25 2004/09/29 10:33:49 starvik Exp $
2 *
3 * Serial port driver for the ETRAX 100LX chip
4 *
5 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Axis Communications AB
6 *
7 * Many, many authors. Based once upon a time on serial.c for 16x50.
8 *
9 * $Log: serial.c,v $
10 * Revision 1.25 2004/09/29 10:33:49 starvik
11 * Resolved a dealock when printing debug from kernel.
12 *
13 * Revision 1.24 2004/08/27 23:25:59 johana
14 * rs_set_termios() must call change_speed() if c_iflag has changed or
15 * automatic XOFF handling will be enabled and transmitter will stop
16 * if 0x13 is received.
17 *
18 * Revision 1.23 2004/08/24 06:57:13 starvik
19 * More whitespace cleanup
20 *
21 * Revision 1.22 2004/08/24 06:12:20 starvik
22 * Whitespace cleanup
23 *
24 * Revision 1.20 2004/05/24 12:00:20 starvik
25 * Big merge of stuff from Linux 2.4 (e.g. manual mode for the serial port).
26 *
27 * Revision 1.19 2004/05/17 13:12:15 starvik
28 * Kernel console hook
29 * Big merge from Linux 2.4 still pending.
30 *
31 * Revision 1.18 2003/10/28 07:18:30 starvik
32 * Compiles with debug info
33 *
34 * Revision 1.17 2003/07/04 08:27:37 starvik
35 * Merge of Linux 2.5.74
36 *
37 * Revision 1.16 2003/06/13 10:05:19 johana
38 * Help the user to avoid trouble by:
39 * Forcing mixed mode for status/control lines if not all pins are used.
40 *
41 * Revision 1.15 2003/06/13 09:43:01 johana
42 * Merged in the following changes from os/linux/arch/cris/drivers/serial.c
43 * + some minor changes to reduce diff.
44 *
45 * Revision 1.49 2003/05/30 11:31:54 johana
46 * Merged in change-branch--serial9bit that adds CMSPAR support for sticky
47 * parity (mark/space)
48 *
49 * Revision 1.48 2003/05/30 11:03:57 johana
50 * Implemented rs_send_xchar() by disabling the DMA and writing manually.
51 * Added e100_disable_txdma_channel() and e100_enable_txdma_channel().
52 * Fixed rs_throttle() and rs_unthrottle() to properly call rs_send_xchar
53 * instead of setting info->x_char and check the CRTSCTS flag before
54 * controlling the rts pin.
55 *
56 * Revision 1.14 2003/04/09 08:12:44 pkj
57 * Corrected typo changes made upstream.
58 *
59 * Revision 1.13 2003/04/09 05:20:47 starvik
60 * Merge of Linux 2.5.67
61 *
62 * Revision 1.11 2003/01/22 06:48:37 starvik
63 * Fixed warnings issued by GCC 3.2.1
64 *
65 * Revision 1.9 2002/12/13 09:07:47 starvik
66 * Alert user that RX_TIMEOUT_TICKS==0 doesn't work
67 *
68 * Revision 1.8 2002/12/11 13:13:57 starvik
69 * Added arch/ to v10 specific includes
70 * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
71 *
72 * Revision 1.7 2002/12/06 07:13:57 starvik
73 * Corrected work queue stuff
74 * Removed CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST
75 *
76 * Revision 1.6 2002/11/21 07:17:46 starvik
77 * Change static inline to extern inline where otherwise outlined with gcc-3.2
78 *
79 * Revision 1.5 2002/11/14 15:59:49 starvik
80 * Linux 2.5 port of the latest serial driver from 2.4. The work queue stuff
81 * probably doesn't work yet.
82 *
83 * Revision 1.42 2002/11/05 09:08:47 johana
84 * Better implementation of rs_stop() and rs_start() that uses the XOFF
85 * register to start/stop transmission.
86 * change_speed() also initilises XOFF register correctly so that
87 * auto_xoff is enabled when IXON flag is set by user.
88 * This gives fast XOFF response times.
89 *
90 * Revision 1.41 2002/11/04 18:40:57 johana
91 * Implemented rs_stop() and rs_start().
92 * Simple tests using hwtestserial indicates that this should be enough
93 * to make it work.
94 *
95 * Revision 1.40 2002/10/14 05:33:18 starvik
96 * RS-485 uses fast timers even if SERIAL_FAST_TIMER is disabled
97 *
98 * Revision 1.39 2002/09/30 21:00:57 johana
99 * Support for CONFIG_ETRAX_SERx_DTR_RI_DSR_CD_MIXED where the status and
100 * control pins can be mixed between PA and PB.
101 * If no serial port uses MIXED old solution is used
102 * (saves a few bytes and cycles).
103 * control_pins struct uses masks instead of bit numbers.
104 * Corrected dummy values and polarity in line_info() so
105 * /proc/tty/driver/serial is now correct.
106 * (the E100_xxx_GET() macros is really active low - perhaps not obvious)
107 *
108 * Revision 1.38 2002/08/23 11:01:36 starvik
109 * Check that serial port is enabled in all interrupt handlers to avoid
110 * restarts of DMA channels not assigned to serial ports
111 *
112 * Revision 1.37 2002/08/13 13:02:37 bjornw
113 * Removed some warnings because of unused code
114 *
115 * Revision 1.36 2002/08/08 12:50:01 starvik
116 * Serial interrupt is shared with synchronous serial port driver
117 *
118 * Revision 1.35 2002/06/03 10:40:49 starvik
119 * Increased RS-485 RTS toggle timer to 2 characters
120 *
121 * Revision 1.34 2002/05/28 18:59:36 johana
122 * Whitespace and comment fixing to be more like etrax100ser.c 1.71.
123 *
124 * Revision 1.33 2002/05/28 17:55:43 johana
125 * RS-485 uses FAST_TIMER if enabled, and starts a short (one char time)
126 * timer from tranismit_chars (interrupt context).
127 * The timer toggles RTS in interrupt context when expired giving minimum
128 * latencies.
129 *
130 * Revision 1.32 2002/05/22 13:58:00 johana
131 * Renamed rs_write() to raw_write() and made it inline.
132 * New rs_write() handles RS-485 if configured and enabled
133 * (moved code from e100_write_rs485()).
134 * RS-485 ioctl's uses copy_from_user() instead of verify_area().
135 *
136 * Revision 1.31 2002/04/22 11:20:03 johana
137 * Updated copyright years.
138 *
139 * Revision 1.30 2002/04/22 09:39:12 johana
140 * RS-485 support compiles.
141 *
142 * Revision 1.29 2002/01/14 16:10:01 pkj
143 * Allocate the receive buffers dynamically. The static 4kB buffer was
144 * too small for the peaks. This means that we can get rid of the extra
145 * buffer and the copying to it. It also means we require less memory
146 * under normal operations, but can use more when needed (there is a
147 * cap at 64kB for safety reasons). If there is no memory available
148 * we panic(), and die a horrible death...
149 *
150 * Revision 1.28 2001/12/18 15:04:53 johana
151 * Cleaned up write_rs485() - now it works correctly without padding extra
152 * char.
153 * Added sane default initialisation of rs485.
154 * Added #ifdef around dummy variables.
155 *
156 * Revision 1.27 2001/11/29 17:00:41 pkj
157 * 2kB seems to be too small a buffer when using 921600 bps,
158 * so increase it to 4kB (this was already done for the elinux
159 * version of the serial driver).
160 *
161 * Revision 1.26 2001/11/19 14:20:41 pkj
162 * Minor changes to comments and unused code.
163 *
164 * Revision 1.25 2001/11/12 20:03:43 pkj
165 * Fixed compiler warnings.
166 *
167 * Revision 1.24 2001/11/12 15:10:05 pkj
168 * Total redesign of the receiving part of the serial driver.
169 * Uses eight chained descriptors to write to a 4kB buffer.
170 * This data is then serialised into a 2kB buffer. From there it
171 * is copied into the TTY's flip buffers when they become available.
172 * A lot of copying, and the sizes of the buffers might need to be
173 * tweaked, but all in all it should work better than the previous
174 * version, without the need to modify the TTY code in any way.
175 * Also note that erroneous bytes are now correctly marked in the
176 * flag buffers (instead of always marking the first byte).
177 *
178 * Revision 1.23 2001/10/30 17:53:26 pkj
179 * * Set info->uses_dma to 0 when a port is closed.
180 * * Mark the timer1 interrupt as a fast one (SA_INTERRUPT).
181 * * Call start_flush_timer() in start_receive() if
182 * CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST is defined.
183 *
184 * Revision 1.22 2001/10/30 17:44:03 pkj
185 * Use %lu for received and transmitted counters in line_info().
186 *
187 * Revision 1.21 2001/10/30 17:40:34 pkj
188 * Clean-up. The only change to functionality is that
189 * CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS(=5) is used instead of
190 * MAX_FLUSH_TIME(=8).
191 *
192 * Revision 1.20 2001/10/30 15:24:49 johana
193 * Added char_time stuff from 2.0 driver.
194 *
195 * Revision 1.19 2001/10/30 15:23:03 johana
196 * Merged with 1.13.2 branch + fixed indentation
197 * and changed CONFIG_ETRAX100_XYS to CONFIG_ETRAX_XYZ
198 *
199 * Revision 1.18 2001/09/24 09:27:22 pkj
200 * Completed ext_baud_table[] in cflag_to_baud() and cflag_to_etrax_baud().
201 *
202 * Revision 1.17 2001/08/24 11:32:49 ronny
203 * More fixes for the CONFIG_ETRAX_SERIAL_PORT0 define.
204 *
205 * Revision 1.16 2001/08/24 07:56:22 ronny
206 * Added config ifdefs around ser0 irq requests.
207 *
208 * Revision 1.15 2001/08/16 09:10:31 bjarne
209 * serial.c - corrected the initialization of rs_table, the wrong defines
210 * where used.
211 * Corrected a test in timed_flush_handler.
212 * Changed configured to enabled.
213 * serial.h - Changed configured to enabled.
214 *
215 * Revision 1.14 2001/08/15 07:31:23 bjarne
216 * Introduced two new members to the e100_serial struct.
217 * configured - Will be set to 1 if the port has been configured in .config
218 * uses_dma - Should be set to 1 if the port uses DMA. Currently it is set
219 * to 1
220 * when a port is opened. This is used to limit the DMA interrupt
221 * routines to only manipulate DMA channels actually used by the
222 * serial driver.
223 *
224 * Revision 1.13.2.2 2001/10/17 13:57:13 starvik
225 * Receiver was broken by the break fixes
226 *
227 * Revision 1.13.2.1 2001/07/20 13:57:39 ronny
228 * Merge with new stuff from etrax100ser.c. Works but haven't checked stuff
229 * like break handling.
230 *
231 * Revision 1.13 2001/05/09 12:40:31 johana
232 * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
233 *
234 * Revision 1.12 2001/04/19 12:23:07 bjornw
235 * CONFIG_RS485 -> CONFIG_ETRAX_RS485
236 *
237 * Revision 1.11 2001/04/05 14:29:48 markusl
238 * Updated according to review remarks i.e.
239 * -Use correct types in port structure to avoid compiler warnings
240 * -Try to use IO_* macros whenever possible
241 * -Open should never return -EBUSY
242 *
243 * Revision 1.10 2001/03/05 13:14:07 bjornw
244 * Another spelling fix
245 *
246 * Revision 1.9 2001/02/23 13:46:38 bjornw
247 * Spellling check
248 *
249 * Revision 1.8 2001/01/23 14:56:35 markusl
250 * Made use of ser1 optional
251 * Needed by USB
252 *
253 * Revision 1.7 2001/01/19 16:14:48 perf
254 * Added kernel options for serial ports 234.
255 * Changed option names from CONFIG_ETRAX100_XYZ to CONFIG_ETRAX_XYZ.
256 *
257 * Revision 1.6 2000/11/22 16:36:09 bjornw
258 * Please marketing by using the correct case when spelling Etrax.
259 *
260 * Revision 1.5 2000/11/21 16:43:37 bjornw
261 * Fixed so it compiles under CONFIG_SVINTO_SIM
262 *
263 * Revision 1.4 2000/11/15 17:34:12 bjornw
264 * Added a timeout timer for flushing input channels. The interrupt-based
265 * fast flush system should be easy to merge with this later (works the same
266 * way, only with an irq instead of a system timer_list)
267 *
268 * Revision 1.3 2000/11/13 17:19:57 bjornw
269 * * Incredibly, this almost complete rewrite of serial.c worked (at least
270 * for output) the first time.
271 *
272 * Items worth noticing:
273 *
274 * No Etrax100 port 1 workarounds (does only compile on 2.4 anyway now)
275 * RS485 is not ported (why can't it be done in userspace as on x86 ?)
276 * Statistics done through async_icount - if any more stats are needed,
277 * that's the place to put them or in an arch-dep version of it.
278 * timeout_interrupt and the other fast timeout stuff not ported yet
279 * There be dragons in this 3k+ line driver
280 *
281 * Revision 1.2 2000/11/10 16:50:28 bjornw
282 * First shot at a 2.4 port, does not compile totally yet
283 *
284 * Revision 1.1 2000/11/10 16:47:32 bjornw
285 * Added verbatim copy of rev 1.49 etrax100ser.c from elinux
286 *
287 * Revision 1.49 2000/10/30 15:47:14 tobiasa
288 * Changed version number.
289 *
290 * Revision 1.48 2000/10/25 11:02:43 johana
291 * Changed %ul to %lu in printf's
292 *
293 * Revision 1.47 2000/10/18 15:06:53 pkj
294 * Compile correctly with CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST and
295 * CONFIG_ETRAX_SERIAL_PROC_ENTRY together.
296 * Some clean-up of the /proc/serial file.
297 *
298 * Revision 1.46 2000/10/16 12:59:40 johana
299 * Added CONFIG_ETRAX_SERIAL_PROC_ENTRY for statistics and debug info.
300 *
301 * Revision 1.45 2000/10/13 17:10:59 pkj
302 * Do not flush DMAs while flipping TTY buffers.
303 *
304 * Revision 1.44 2000/10/13 16:34:29 pkj
305 * Added a delay in ser_interrupt() for 2.3ms when an error is detected.
306 * We do not know why this delay is required yet, but without it the
307 * irmaflash program does not work (this was the program that needed
308 * the ser_interrupt() to be needed in the first place). This should not
309 * affect normal use of the serial ports.
310 *
311 * Revision 1.43 2000/10/13 16:30:44 pkj
312 * New version of the fast flush of serial buffers code. This time
313 * it is localized to the serial driver and uses a fast timer to
314 * do the work.
315 *
316 * Revision 1.42 2000/10/13 14:54:26 bennyo
317 * Fix for switching RTS when using rs485
318 *
319 * Revision 1.41 2000/10/12 11:43:44 pkj
320 * Cleaned up a number of comments.
321 *
322 * Revision 1.40 2000/10/10 11:58:39 johana
323 * Made RS485 support generic for all ports.
324 * Toggle rts in interrupt if no delay wanted.
325 * WARNING: No true transmitter empty check??
326 * Set d_wait bit when sending data so interrupt is delayed until
327 * fifo flushed. (Fix tcdrain() problem)
328 *
329 * Revision 1.39 2000/10/04 16:08:02 bjornw
330 * * Use virt_to_phys etc. for DMA addresses
331 * * Removed CONFIG_FLUSH_DMA_FAST hacks
332 * * Indentation fix
333 *
334 * Revision 1.38 2000/10/02 12:27:10 mattias
335 * * added variable used when using fast flush on serial dma.
336 * (CONFIG_FLUSH_DMA_FAST)
337 *
338 * Revision 1.37 2000/09/27 09:44:24 pkj
339 * Uncomment definition of SERIAL_HANDLE_EARLY_ERRORS.
340 *
341 * Revision 1.36 2000/09/20 13:12:52 johana
342 * Support for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS:
343 * Number of timer ticks between flush of receive fifo (1 tick = 10ms).
344 * Try 0-3 for low latency applications. Approx 5 for high load
345 * applications (e.g. PPP). Maybe this should be more adaptive some day...
346 *
347 * Revision 1.35 2000/09/20 10:36:08 johana
348 * Typo in get_lsr_info()
349 *
350 * Revision 1.34 2000/09/20 10:29:59 johana
351 * Let rs_chars_in_buffer() check fifo content as well.
352 * get_lsr_info() might work now (not tested).
353 * Easier to change the port to debug.
354 *
355 * Revision 1.33 2000/09/13 07:52:11 torbjore
356 * Support RS485
357 *
358 * Revision 1.32 2000/08/31 14:45:37 bjornw
359 * After sending a break we need to reset the transmit DMA channel
360 *
361 * Revision 1.31 2000/06/21 12:13:29 johana
362 * Fixed wait for all chars sent when closing port.
363 * (Used to always take 1 second!)
364 * Added shadows for directions of status/ctrl signals.
365 *
366 * Revision 1.30 2000/05/29 16:27:55 bjornw
367 * Simulator ifdef moved a bit
368 *
369 * Revision 1.29 2000/05/09 09:40:30 mattias
370 * * Added description of dma registers used in timeout_interrupt
371 * * Removed old code
372 *
373 * Revision 1.28 2000/05/08 16:38:58 mattias
374 * * Bugfix for flushing fifo in timeout_interrupt
375 * Problem occurs when bluetooth stack waits for a small number of bytes
376 * containing an event acknowledging free buffers in bluetooth HW
377 * As before, data was stuck in fifo until more data came on uart and
378 * flushed it up to the stack.
379 *
380 * Revision 1.27 2000/05/02 09:52:28 jonasd
381 * Added fix for peculiar etrax behaviour when eop is forced on an empty
382 * fifo. This is used when flashing the IRMA chip. Disabled by default.
383 *
384 * Revision 1.26 2000/03/29 15:32:02 bjornw
385 * 2.0.34 updates
386 *
387 * Revision 1.25 2000/02/16 16:59:36 bjornw
388 * * Receive DMA directly into the flip-buffer, eliminating an intermediary
389 * receive buffer and a memcpy. Will avoid some overruns.
390 * * Error message on debug port if an overrun or flip buffer overrun occurs.
391 * * Just use the first byte in the flag flip buffer for errors.
392 * * Check for timeout on the serial ports only each 5/100 s, not 1/100.
393 *
394 * Revision 1.24 2000/02/09 18:02:28 bjornw
395 * * Clear serial errors (overrun, framing, parity) correctly. Before, the
396 * receiver would get stuck if an error occurred and we did not restart
397 * the input DMA.
398 * * Cosmetics (indentation, some code made into inlines)
399 * * Some more debug options
400 * * Actually shut down the serial port (DMA irq, DMA reset, receiver stop)
401 * when the last open is closed. Corresponding fixes in startup().
402 * * rs_close() "tx FIFO wait" code moved into right place, bug & -> && fixed
403 * and make a special case out of port 1 (R_DMA_CHx_STATUS is broken for that)
404 * * e100_disable_rx/enable_rx just disables/enables the receiver, not RTS
405 *
406 * Revision 1.23 2000/01/24 17:46:19 johana
407 * Wait for flush of DMA/FIFO when closing port.
408 *
409 * Revision 1.22 2000/01/20 18:10:23 johana
410 * Added TIOCMGET ioctl to return modem status.
411 * Implemented modem status/control that works with the extra signals
412 * (DTR, DSR, RI,CD) as well.
413 * 3 different modes supported:
414 * ser0 on PB (Bundy), ser1 on PB (Lisa) and ser2 on PA (Bundy)
415 * Fixed DEF_TX value that caused the serial transmitter pin (txd) to go to 0 when
416 * closing the last filehandle, NASTY!.
417 * Added break generation, not tested though!
418 * Use SA_SHIRQ when request_irq() for ser2 and ser3 (shared with) par0 and par1.
419 * You can't use them at the same time (yet..), but you can hopefully switch
420 * between ser2/par0, ser3/par1 with the same kernel config.
421 * Replaced some magic constants with defines
422 *
423 *
424 */
425
426static char *serial_version = "$Revision: 1.25 $";
427
428#include <linux/config.h>
429#include <linux/version.h>
430
431#include <linux/types.h>
432#include <linux/errno.h>
433#include <linux/signal.h>
434#include <linux/sched.h>
435#include <linux/timer.h>
436#include <linux/interrupt.h>
437#include <linux/tty.h>
438#include <linux/tty_flip.h>
439#include <linux/major.h>
440#include <linux/string.h>
441#include <linux/fcntl.h>
442#include <linux/mm.h>
443#include <linux/slab.h>
444#include <linux/init.h>
445#include <asm/uaccess.h>
446#include <linux/kernel.h>
447
448#include <asm/io.h>
449#include <asm/irq.h>
450#include <asm/system.h>
451#include <asm/segment.h>
452#include <asm/bitops.h>
453#include <linux/delay.h>
454
455#include <asm/arch/svinto.h>
456
457/* non-arch dependent serial structures are in linux/serial.h */
458#include <linux/serial.h>
459/* while we keep our own stuff (struct e100_serial) in a local .h file */
460#include "serial.h"
461#include <asm/fasttimer.h>
462
463#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
464#ifndef CONFIG_ETRAX_FAST_TIMER
465#error "Enable FAST_TIMER to use SERIAL_FAST_TIMER"
466#endif
467#endif
468
469#if defined(CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS) && \
470 (CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS == 0)
471#error "RX_TIMEOUT_TICKS == 0 not allowed, use 1"
472#endif
473
474#if defined(CONFIG_ETRAX_RS485_ON_PA) && defined(CONFIG_ETRAX_RS485_ON_PORT_G)
475#error "Disable either CONFIG_ETRAX_RS485_ON_PA or CONFIG_ETRAX_RS485_ON_PORT_G"
476#endif
477
478/*
479 * All of the compatibilty code so we can compile serial.c against
480 * older kernels is hidden in serial_compat.h
481 */
482#if defined(LOCAL_HEADERS)
483#include "serial_compat.h"
484#endif
485
486#define _INLINE_ inline
487
488struct tty_driver *serial_driver;
489
490/* serial subtype definitions */
491#ifndef SERIAL_TYPE_NORMAL
492#define SERIAL_TYPE_NORMAL 1
493#endif
494
495/* number of characters left in xmit buffer before we ask for more */
496#define WAKEUP_CHARS 256
497
498//#define SERIAL_DEBUG_INTR
499//#define SERIAL_DEBUG_OPEN
500//#define SERIAL_DEBUG_FLOW
501//#define SERIAL_DEBUG_DATA
502//#define SERIAL_DEBUG_THROTTLE
503//#define SERIAL_DEBUG_IO /* Debug for Extra control and status pins */
504//#define SERIAL_DEBUG_LINE 0 /* What serport we want to debug */
505
506/* Enable this to use serial interrupts to handle when you
507 expect the first received event on the serial port to
508 be an error, break or similar. Used to be able to flash IRMA
509 from eLinux */
510#define SERIAL_HANDLE_EARLY_ERRORS
511
512/* Defined and used in n_tty.c, but we need it here as well */
513#define TTY_THRESHOLD_THROTTLE 128
514
515/* Due to buffersizes and threshold values, our SERIAL_DESCR_BUF_SIZE
516 * must not be to high or flow control won't work if we leave it to the tty
517 * layer so we have our own throttling in flush_to_flip
518 * TTY_FLIPBUF_SIZE=512,
519 * TTY_THRESHOLD_THROTTLE/UNTHROTTLE=128
520 * BUF_SIZE can't be > 128
521 */
522/* Currently 16 descriptors x 128 bytes = 2048 bytes */
523#define SERIAL_DESCR_BUF_SIZE 256
524
525#define SERIAL_PRESCALE_BASE 3125000 /* 3.125MHz */
526#define DEF_BAUD_BASE SERIAL_PRESCALE_BASE
527
528/* We don't want to load the system with massive fast timer interrupt
529 * on high baudrates so limit it to 250 us (4kHz) */
530#define MIN_FLUSH_TIME_USEC 250
531
532/* Add an x here to log a lot of timer stuff */
533#define TIMERD(x)
534/* Debug details of interrupt handling */
535#define DINTR1(x) /* irq on/off, errors */
536#define DINTR2(x) /* tx and rx */
537/* Debug flip buffer stuff */
538#define DFLIP(x)
539/* Debug flow control and overview of data flow */
540#define DFLOW(x)
541#define DBAUD(x)
542#define DLOG_INT_TRIG(x)
543
544//#define DEBUG_LOG_INCLUDED
545#ifndef DEBUG_LOG_INCLUDED
546#define DEBUG_LOG(line, string, value)
547#else
548struct debug_log_info
549{
550 unsigned long time;
551 unsigned long timer_data;
552// int line;
553 const char *string;
554 int value;
555};
556#define DEBUG_LOG_SIZE 4096
557
558struct debug_log_info debug_log[DEBUG_LOG_SIZE];
559int debug_log_pos = 0;
560
561#define DEBUG_LOG(_line, _string, _value) do { \
562 if ((_line) == SERIAL_DEBUG_LINE) {\
563 debug_log_func(_line, _string, _value); \
564 }\
565}while(0)
566
567void debug_log_func(int line, const char *string, int value)
568{
569 if (debug_log_pos < DEBUG_LOG_SIZE) {
570 debug_log[debug_log_pos].time = jiffies;
571 debug_log[debug_log_pos].timer_data = *R_TIMER_DATA;
572// debug_log[debug_log_pos].line = line;
573 debug_log[debug_log_pos].string = string;
574 debug_log[debug_log_pos].value = value;
575 debug_log_pos++;
576 }
577 /*printk(string, value);*/
578}
579#endif
580
581#ifndef CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
582/* Default number of timer ticks before flushing rx fifo
583 * When using "little data, low latency applications: use 0
584 * When using "much data applications (PPP)" use ~5
585 */
586#define CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS 5
587#endif
588
589unsigned long timer_data_to_ns(unsigned long timer_data);
590
591static void change_speed(struct e100_serial *info);
592static void rs_throttle(struct tty_struct * tty);
593static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
594static int rs_write(struct tty_struct * tty, int from_user,
595 const unsigned char *buf, int count);
596extern _INLINE_ int rs_raw_write(struct tty_struct * tty, int from_user,
597 const unsigned char *buf, int count);
598#ifdef CONFIG_ETRAX_RS485
599static int e100_write_rs485(struct tty_struct * tty, int from_user,
600 const unsigned char *buf, int count);
601#endif
602static int get_lsr_info(struct e100_serial * info, unsigned int *value);
603
604
605#define DEF_BAUD 115200 /* 115.2 kbit/s */
606#define STD_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
607#define DEF_RX 0x20 /* or SERIAL_CTRL_W >> 8 */
608/* Default value of tx_ctrl register: has txd(bit 7)=1 (idle) as default */
609#define DEF_TX 0x80 /* or SERIAL_CTRL_B */
610
611/* offsets from R_SERIALx_CTRL */
612
613#define REG_DATA 0
614#define REG_DATA_STATUS32 0 /* this is the 32 bit register R_SERIALx_READ */
615#define REG_TR_DATA 0
616#define REG_STATUS 1
617#define REG_TR_CTRL 1
618#define REG_REC_CTRL 2
619#define REG_BAUD 3
620#define REG_XOFF 4 /* this is a 32 bit register */
621
622/* The bitfields are the same for all serial ports */
623#define SER_RXD_MASK IO_MASK(R_SERIAL0_STATUS, rxd)
624#define SER_DATA_AVAIL_MASK IO_MASK(R_SERIAL0_STATUS, data_avail)
625#define SER_FRAMING_ERR_MASK IO_MASK(R_SERIAL0_STATUS, framing_err)
626#define SER_PAR_ERR_MASK IO_MASK(R_SERIAL0_STATUS, par_err)
627#define SER_OVERRUN_MASK IO_MASK(R_SERIAL0_STATUS, overrun)
628
629#define SER_ERROR_MASK (SER_OVERRUN_MASK | SER_PAR_ERR_MASK | SER_FRAMING_ERR_MASK)
630
631/* Values for info->errorcode */
632#define ERRCODE_SET_BREAK (TTY_BREAK)
633#define ERRCODE_INSERT 0x100
634#define ERRCODE_INSERT_BREAK (ERRCODE_INSERT | TTY_BREAK)
635
636#define FORCE_EOP(info) *R_SET_EOP = 1U << info->iseteop;
637
638/*
639 * General note regarding the use of IO_* macros in this file:
640 *
641 * We will use the bits defined for DMA channel 6 when using various
642 * IO_* macros (e.g. IO_STATE, IO_MASK, IO_EXTRACT) and _assume_ they are
643 * the same for all channels (which of course they are).
644 *
645 * We will also use the bits defined for serial port 0 when writing commands
646 * to the different ports, as these bits too are the same for all ports.
647 */
648
649
650/* Mask for the irqs possibly enabled in R_IRQ_MASK1_RD etc. */
651static const unsigned long e100_ser_int_mask = 0
652#ifdef CONFIG_ETRAX_SERIAL_PORT0
653| IO_MASK(R_IRQ_MASK1_RD, ser0_data) | IO_MASK(R_IRQ_MASK1_RD, ser0_ready)
654#endif
655#ifdef CONFIG_ETRAX_SERIAL_PORT1
656| IO_MASK(R_IRQ_MASK1_RD, ser1_data) | IO_MASK(R_IRQ_MASK1_RD, ser1_ready)
657#endif
658#ifdef CONFIG_ETRAX_SERIAL_PORT2
659| IO_MASK(R_IRQ_MASK1_RD, ser2_data) | IO_MASK(R_IRQ_MASK1_RD, ser2_ready)
660#endif
661#ifdef CONFIG_ETRAX_SERIAL_PORT3
662| IO_MASK(R_IRQ_MASK1_RD, ser3_data) | IO_MASK(R_IRQ_MASK1_RD, ser3_ready)
663#endif
664;
665unsigned long r_alt_ser_baudrate_shadow = 0;
666
667/* this is the data for the four serial ports in the etrax100 */
668/* DMA2(ser2), DMA4(ser3), DMA6(ser0) or DMA8(ser1) */
669/* R_DMA_CHx_CLR_INTR, R_DMA_CHx_FIRST, R_DMA_CHx_CMD */
670
671static struct e100_serial rs_table[] = {
672 { .baud = DEF_BAUD,
673 .port = (unsigned char *)R_SERIAL0_CTRL,
674 .irq = 1U << 12, /* uses DMA 6 and 7 */
675 .oclrintradr = R_DMA_CH6_CLR_INTR,
676 .ofirstadr = R_DMA_CH6_FIRST,
677 .ocmdadr = R_DMA_CH6_CMD,
678 .ostatusadr = R_DMA_CH6_STATUS,
679 .iclrintradr = R_DMA_CH7_CLR_INTR,
680 .ifirstadr = R_DMA_CH7_FIRST,
681 .icmdadr = R_DMA_CH7_CMD,
682 .idescradr = R_DMA_CH7_DESCR,
683 .flags = STD_FLAGS,
684 .rx_ctrl = DEF_RX,
685 .tx_ctrl = DEF_TX,
686 .iseteop = 2,
687#ifdef CONFIG_ETRAX_SERIAL_PORT0
688 .enabled = 1,
689#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
690 .dma_out_enabled = 1,
691#else
692 .dma_out_enabled = 0,
693#endif
694#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
695 .dma_in_enabled = 1,
696#else
697 .dma_in_enabled = 0
698#endif
699#else
700 .enabled = 0,
701 .dma_out_enabled = 0,
702 .dma_in_enabled = 0
703#endif
704
705}, /* ttyS0 */
706#ifndef CONFIG_SVINTO_SIM
707 { .baud = DEF_BAUD,
708 .port = (unsigned char *)R_SERIAL1_CTRL,
709 .irq = 1U << 16, /* uses DMA 8 and 9 */
710 .oclrintradr = R_DMA_CH8_CLR_INTR,
711 .ofirstadr = R_DMA_CH8_FIRST,
712 .ocmdadr = R_DMA_CH8_CMD,
713 .ostatusadr = R_DMA_CH8_STATUS,
714 .iclrintradr = R_DMA_CH9_CLR_INTR,
715 .ifirstadr = R_DMA_CH9_FIRST,
716 .icmdadr = R_DMA_CH9_CMD,
717 .idescradr = R_DMA_CH9_DESCR,
718 .flags = STD_FLAGS,
719 .rx_ctrl = DEF_RX,
720 .tx_ctrl = DEF_TX,
721 .iseteop = 3,
722#ifdef CONFIG_ETRAX_SERIAL_PORT1
723 .enabled = 1,
724#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA8_OUT
725 .dma_out_enabled = 1,
726#else
727 .dma_out_enabled = 0,
728#endif
729#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA9_IN
730 .dma_in_enabled = 1,
731#else
732 .dma_in_enabled = 0
733#endif
734#else
735 .enabled = 0,
736 .dma_out_enabled = 0,
737 .dma_in_enabled = 0
738#endif
739}, /* ttyS1 */
740
741 { .baud = DEF_BAUD,
742 .port = (unsigned char *)R_SERIAL2_CTRL,
743 .irq = 1U << 4, /* uses DMA 2 and 3 */
744 .oclrintradr = R_DMA_CH2_CLR_INTR,
745 .ofirstadr = R_DMA_CH2_FIRST,
746 .ocmdadr = R_DMA_CH2_CMD,
747 .ostatusadr = R_DMA_CH2_STATUS,
748 .iclrintradr = R_DMA_CH3_CLR_INTR,
749 .ifirstadr = R_DMA_CH3_FIRST,
750 .icmdadr = R_DMA_CH3_CMD,
751 .idescradr = R_DMA_CH3_DESCR,
752 .flags = STD_FLAGS,
753 .rx_ctrl = DEF_RX,
754 .tx_ctrl = DEF_TX,
755 .iseteop = 0,
756#ifdef CONFIG_ETRAX_SERIAL_PORT2
757 .enabled = 1,
758#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
759 .dma_out_enabled = 1,
760#else
761 .dma_out_enabled = 0,
762#endif
763#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
764 .dma_in_enabled = 1,
765#else
766 .dma_in_enabled = 0
767#endif
768#else
769 .enabled = 0,
770 .dma_out_enabled = 0,
771 .dma_in_enabled = 0
772#endif
773 }, /* ttyS2 */
774
775 { .baud = DEF_BAUD,
776 .port = (unsigned char *)R_SERIAL3_CTRL,
777 .irq = 1U << 8, /* uses DMA 4 and 5 */
778 .oclrintradr = R_DMA_CH4_CLR_INTR,
779 .ofirstadr = R_DMA_CH4_FIRST,
780 .ocmdadr = R_DMA_CH4_CMD,
781 .ostatusadr = R_DMA_CH4_STATUS,
782 .iclrintradr = R_DMA_CH5_CLR_INTR,
783 .ifirstadr = R_DMA_CH5_FIRST,
784 .icmdadr = R_DMA_CH5_CMD,
785 .idescradr = R_DMA_CH5_DESCR,
786 .flags = STD_FLAGS,
787 .rx_ctrl = DEF_RX,
788 .tx_ctrl = DEF_TX,
789 .iseteop = 1,
790#ifdef CONFIG_ETRAX_SERIAL_PORT3
791 .enabled = 1,
792#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA4_OUT
793 .dma_out_enabled = 1,
794#else
795 .dma_out_enabled = 0,
796#endif
797#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA5_IN
798 .dma_in_enabled = 1,
799#else
800 .dma_in_enabled = 0
801#endif
802#else
803 .enabled = 0,
804 .dma_out_enabled = 0,
805 .dma_in_enabled = 0
806#endif
807 } /* ttyS3 */
808#endif
809};
810
811
812#define NR_PORTS (sizeof(rs_table)/sizeof(struct e100_serial))
813
814static struct termios *serial_termios[NR_PORTS];
815static struct termios *serial_termios_locked[NR_PORTS];
816#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
817static struct fast_timer fast_timers[NR_PORTS];
818#endif
819
820#ifdef CONFIG_ETRAX_SERIAL_PROC_ENTRY
821#define PROCSTAT(x) x
822struct ser_statistics_type {
823 int overrun_cnt;
824 int early_errors_cnt;
825 int ser_ints_ok_cnt;
826 int errors_cnt;
827 unsigned long int processing_flip;
828 unsigned long processing_flip_still_room;
829 unsigned long int timeout_flush_cnt;
830 int rx_dma_ints;
831 int tx_dma_ints;
832 int rx_tot;
833 int tx_tot;
834};
835
836static struct ser_statistics_type ser_stat[NR_PORTS];
837
838#else
839
840#define PROCSTAT(x)
841
842#endif /* CONFIG_ETRAX_SERIAL_PROC_ENTRY */
843
844/* RS-485 */
845#if defined(CONFIG_ETRAX_RS485)
846#ifdef CONFIG_ETRAX_FAST_TIMER
847static struct fast_timer fast_timers_rs485[NR_PORTS];
848#endif
849#if defined(CONFIG_ETRAX_RS485_ON_PA)
850static int rs485_pa_bit = CONFIG_ETRAX_RS485_ON_PA_BIT;
851#endif
852#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
853static int rs485_port_g_bit = CONFIG_ETRAX_RS485_ON_PORT_G_BIT;
854#endif
855#endif
856
857/* Info and macros needed for each ports extra control/status signals. */
858#define E100_STRUCT_PORT(line, pinname) \
859 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
860 (R_PORT_PA_DATA): ( \
861 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
862 (R_PORT_PB_DATA):&dummy_ser[line]))
863
864#define E100_STRUCT_SHADOW(line, pinname) \
865 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
866 (&port_pa_data_shadow): ( \
867 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
868 (&port_pb_data_shadow):&dummy_ser[line]))
869#define E100_STRUCT_MASK(line, pinname) \
870 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
871 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT): ( \
872 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
873 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT):DUMMY_##pinname##_MASK))
874
875#define DUMMY_DTR_MASK 1
876#define DUMMY_RI_MASK 2
877#define DUMMY_DSR_MASK 4
878#define DUMMY_CD_MASK 8
879static unsigned char dummy_ser[NR_PORTS] = {0xFF, 0xFF, 0xFF,0xFF};
880
881/* If not all status pins are used or disabled, use mixed mode */
882#ifdef CONFIG_ETRAX_SERIAL_PORT0
883
884#define SER0_PA_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PA_BIT+CONFIG_ETRAX_SER0_RI_ON_PA_BIT+CONFIG_ETRAX_SER0_DSR_ON_PA_BIT+CONFIG_ETRAX_SER0_CD_ON_PA_BIT)
885
886#if SER0_PA_BITSUM != -4
887# if CONFIG_ETRAX_SER0_DTR_ON_PA_BIT == -1
888# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
889# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
890# endif
891# endif
892# if CONFIG_ETRAX_SER0_RI_ON_PA_BIT == -1
893# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
894# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
895# endif
896# endif
897# if CONFIG_ETRAX_SER0_DSR_ON_PA_BIT == -1
898# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
899# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
900# endif
901# endif
902# if CONFIG_ETRAX_SER0_CD_ON_PA_BIT == -1
903# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
904# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
905# endif
906# endif
907#endif
908
909#define SER0_PB_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PB_BIT+CONFIG_ETRAX_SER0_RI_ON_PB_BIT+CONFIG_ETRAX_SER0_DSR_ON_PB_BIT+CONFIG_ETRAX_SER0_CD_ON_PB_BIT)
910
911#if SER0_PB_BITSUM != -4
912# if CONFIG_ETRAX_SER0_DTR_ON_PB_BIT == -1
913# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
914# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
915# endif
916# endif
917# if CONFIG_ETRAX_SER0_RI_ON_PB_BIT == -1
918# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
919# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
920# endif
921# endif
922# if CONFIG_ETRAX_SER0_DSR_ON_PB_BIT == -1
923# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
924# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
925# endif
926# endif
927# if CONFIG_ETRAX_SER0_CD_ON_PB_BIT == -1
928# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
929# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
930# endif
931# endif
932#endif
933
934#endif /* PORT0 */
935
936
937#ifdef CONFIG_ETRAX_SERIAL_PORT1
938
939#define SER1_PA_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PA_BIT+CONFIG_ETRAX_SER1_RI_ON_PA_BIT+CONFIG_ETRAX_SER1_DSR_ON_PA_BIT+CONFIG_ETRAX_SER1_CD_ON_PA_BIT)
940
941#if SER1_PA_BITSUM != -4
942# if CONFIG_ETRAX_SER1_DTR_ON_PA_BIT == -1
943# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
944# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
945# endif
946# endif
947# if CONFIG_ETRAX_SER1_RI_ON_PA_BIT == -1
948# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
949# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
950# endif
951# endif
952# if CONFIG_ETRAX_SER1_DSR_ON_PA_BIT == -1
953# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
954# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
955# endif
956# endif
957# if CONFIG_ETRAX_SER1_CD_ON_PA_BIT == -1
958# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
959# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
960# endif
961# endif
962#endif
963
964#define SER1_PB_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PB_BIT+CONFIG_ETRAX_SER1_RI_ON_PB_BIT+CONFIG_ETRAX_SER1_DSR_ON_PB_BIT+CONFIG_ETRAX_SER1_CD_ON_PB_BIT)
965
966#if SER1_PB_BITSUM != -4
967# if CONFIG_ETRAX_SER1_DTR_ON_PB_BIT == -1
968# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
969# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
970# endif
971# endif
972# if CONFIG_ETRAX_SER1_RI_ON_PB_BIT == -1
973# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
974# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
975# endif
976# endif
977# if CONFIG_ETRAX_SER1_DSR_ON_PB_BIT == -1
978# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
979# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
980# endif
981# endif
982# if CONFIG_ETRAX_SER1_CD_ON_PB_BIT == -1
983# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
984# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
985# endif
986# endif
987#endif
988
989#endif /* PORT1 */
990
991#ifdef CONFIG_ETRAX_SERIAL_PORT2
992
993#define SER2_PA_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PA_BIT+CONFIG_ETRAX_SER2_RI_ON_PA_BIT+CONFIG_ETRAX_SER2_DSR_ON_PA_BIT+CONFIG_ETRAX_SER2_CD_ON_PA_BIT)
994
995#if SER2_PA_BITSUM != -4
996# if CONFIG_ETRAX_SER2_DTR_ON_PA_BIT == -1
997# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
998# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
999# endif
1000# endif
1001# if CONFIG_ETRAX_SER2_RI_ON_PA_BIT == -1
1002# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
1003# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
1004# endif
1005# endif
1006# if CONFIG_ETRAX_SER2_DSR_ON_PA_BIT == -1
1007# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
1008# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
1009# endif
1010# endif
1011# if CONFIG_ETRAX_SER2_CD_ON_PA_BIT == -1
1012# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
1013# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
1014# endif
1015# endif
1016#endif
1017
1018#define SER2_PB_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PB_BIT+CONFIG_ETRAX_SER2_RI_ON_PB_BIT+CONFIG_ETRAX_SER2_DSR_ON_PB_BIT+CONFIG_ETRAX_SER2_CD_ON_PB_BIT)
1019
1020#if SER2_PB_BITSUM != -4
1021# if CONFIG_ETRAX_SER2_DTR_ON_PB_BIT == -1
1022# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
1023# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
1024# endif
1025# endif
1026# if CONFIG_ETRAX_SER2_RI_ON_PB_BIT == -1
1027# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
1028# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
1029# endif
1030# endif
1031# if CONFIG_ETRAX_SER2_DSR_ON_PB_BIT == -1
1032# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
1033# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
1034# endif
1035# endif
1036# if CONFIG_ETRAX_SER2_CD_ON_PB_BIT == -1
1037# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
1038# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
1039# endif
1040# endif
1041#endif
1042
1043#endif /* PORT2 */
1044
1045#ifdef CONFIG_ETRAX_SERIAL_PORT3
1046
1047#define SER3_PA_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PA_BIT+CONFIG_ETRAX_SER3_RI_ON_PA_BIT+CONFIG_ETRAX_SER3_DSR_ON_PA_BIT+CONFIG_ETRAX_SER3_CD_ON_PA_BIT)
1048
1049#if SER3_PA_BITSUM != -4
1050# if CONFIG_ETRAX_SER3_DTR_ON_PA_BIT == -1
1051# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
1052# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
1053# endif
1054# endif
1055# if CONFIG_ETRAX_SER3_RI_ON_PA_BIT == -1
1056# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
1057# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
1058# endif
1059# endif
1060# if CONFIG_ETRAX_SER3_DSR_ON_PA_BIT == -1
1061# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
1062# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
1063# endif
1064# endif
1065# if CONFIG_ETRAX_SER3_CD_ON_PA_BIT == -1
1066# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
1067# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
1068# endif
1069# endif
1070#endif
1071
1072#define SER3_PB_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PB_BIT+CONFIG_ETRAX_SER3_RI_ON_PB_BIT+CONFIG_ETRAX_SER3_DSR_ON_PB_BIT+CONFIG_ETRAX_SER3_CD_ON_PB_BIT)
1073
1074#if SER3_PB_BITSUM != -4
1075# if CONFIG_ETRAX_SER3_DTR_ON_PB_BIT == -1
1076# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
1077# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
1078# endif
1079# endif
1080# if CONFIG_ETRAX_SER3_RI_ON_PB_BIT == -1
1081# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
1082# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
1083# endif
1084# endif
1085# if CONFIG_ETRAX_SER3_DSR_ON_PB_BIT == -1
1086# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
1087# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
1088# endif
1089# endif
1090# if CONFIG_ETRAX_SER3_CD_ON_PB_BIT == -1
1091# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
1092# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
1093# endif
1094# endif
1095#endif
1096
1097#endif /* PORT3 */
1098
1099
1100#if defined(CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED) || \
1101 defined(CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED) || \
1102 defined(CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED) || \
1103 defined(CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED)
1104#define CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
1105#endif
1106
1107#ifdef CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
1108/* The pins can be mixed on PA and PB */
1109#define CONTROL_PINS_PORT_NOT_USED(line) \
1110 &dummy_ser[line], &dummy_ser[line], \
1111 &dummy_ser[line], &dummy_ser[line], \
1112 &dummy_ser[line], &dummy_ser[line], \
1113 &dummy_ser[line], &dummy_ser[line], \
1114 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
1115
1116
1117struct control_pins
1118{
1119 volatile unsigned char *dtr_port;
1120 unsigned char *dtr_shadow;
1121 volatile unsigned char *ri_port;
1122 unsigned char *ri_shadow;
1123 volatile unsigned char *dsr_port;
1124 unsigned char *dsr_shadow;
1125 volatile unsigned char *cd_port;
1126 unsigned char *cd_shadow;
1127
1128 unsigned char dtr_mask;
1129 unsigned char ri_mask;
1130 unsigned char dsr_mask;
1131 unsigned char cd_mask;
1132};
1133
1134static const struct control_pins e100_modem_pins[NR_PORTS] =
1135{
1136 /* Ser 0 */
1137 {
1138#ifdef CONFIG_ETRAX_SERIAL_PORT0
1139 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
1140 E100_STRUCT_PORT(0,RI), E100_STRUCT_SHADOW(0,RI),
1141 E100_STRUCT_PORT(0,DSR), E100_STRUCT_SHADOW(0,DSR),
1142 E100_STRUCT_PORT(0,CD), E100_STRUCT_SHADOW(0,CD),
1143 E100_STRUCT_MASK(0,DTR),
1144 E100_STRUCT_MASK(0,RI),
1145 E100_STRUCT_MASK(0,DSR),
1146 E100_STRUCT_MASK(0,CD)
1147#else
1148 CONTROL_PINS_PORT_NOT_USED(0)
1149#endif
1150 },
1151
1152 /* Ser 1 */
1153 {
1154#ifdef CONFIG_ETRAX_SERIAL_PORT1
1155 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
1156 E100_STRUCT_PORT(1,RI), E100_STRUCT_SHADOW(1,RI),
1157 E100_STRUCT_PORT(1,DSR), E100_STRUCT_SHADOW(1,DSR),
1158 E100_STRUCT_PORT(1,CD), E100_STRUCT_SHADOW(1,CD),
1159 E100_STRUCT_MASK(1,DTR),
1160 E100_STRUCT_MASK(1,RI),
1161 E100_STRUCT_MASK(1,DSR),
1162 E100_STRUCT_MASK(1,CD)
1163#else
1164 CONTROL_PINS_PORT_NOT_USED(1)
1165#endif
1166 },
1167
1168 /* Ser 2 */
1169 {
1170#ifdef CONFIG_ETRAX_SERIAL_PORT2
1171 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
1172 E100_STRUCT_PORT(2,RI), E100_STRUCT_SHADOW(2,RI),
1173 E100_STRUCT_PORT(2,DSR), E100_STRUCT_SHADOW(2,DSR),
1174 E100_STRUCT_PORT(2,CD), E100_STRUCT_SHADOW(2,CD),
1175 E100_STRUCT_MASK(2,DTR),
1176 E100_STRUCT_MASK(2,RI),
1177 E100_STRUCT_MASK(2,DSR),
1178 E100_STRUCT_MASK(2,CD)
1179#else
1180 CONTROL_PINS_PORT_NOT_USED(2)
1181#endif
1182 },
1183
1184 /* Ser 3 */
1185 {
1186#ifdef CONFIG_ETRAX_SERIAL_PORT3
1187 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
1188 E100_STRUCT_PORT(3,RI), E100_STRUCT_SHADOW(3,RI),
1189 E100_STRUCT_PORT(3,DSR), E100_STRUCT_SHADOW(3,DSR),
1190 E100_STRUCT_PORT(3,CD), E100_STRUCT_SHADOW(3,CD),
1191 E100_STRUCT_MASK(3,DTR),
1192 E100_STRUCT_MASK(3,RI),
1193 E100_STRUCT_MASK(3,DSR),
1194 E100_STRUCT_MASK(3,CD)
1195#else
1196 CONTROL_PINS_PORT_NOT_USED(3)
1197#endif
1198 }
1199};
1200#else /* CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
1201
1202/* All pins are on either PA or PB for each serial port */
1203#define CONTROL_PINS_PORT_NOT_USED(line) \
1204 &dummy_ser[line], &dummy_ser[line], \
1205 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
1206
1207
1208struct control_pins
1209{
1210 volatile unsigned char *port;
1211 unsigned char *shadow;
1212
1213 unsigned char dtr_mask;
1214 unsigned char ri_mask;
1215 unsigned char dsr_mask;
1216 unsigned char cd_mask;
1217};
1218
1219#define dtr_port port
1220#define dtr_shadow shadow
1221#define ri_port port
1222#define ri_shadow shadow
1223#define dsr_port port
1224#define dsr_shadow shadow
1225#define cd_port port
1226#define cd_shadow shadow
1227
1228static const struct control_pins e100_modem_pins[NR_PORTS] =
1229{
1230 /* Ser 0 */
1231 {
1232#ifdef CONFIG_ETRAX_SERIAL_PORT0
1233 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
1234 E100_STRUCT_MASK(0,DTR),
1235 E100_STRUCT_MASK(0,RI),
1236 E100_STRUCT_MASK(0,DSR),
1237 E100_STRUCT_MASK(0,CD)
1238#else
1239 CONTROL_PINS_PORT_NOT_USED(0)
1240#endif
1241 },
1242
1243 /* Ser 1 */
1244 {
1245#ifdef CONFIG_ETRAX_SERIAL_PORT1
1246 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
1247 E100_STRUCT_MASK(1,DTR),
1248 E100_STRUCT_MASK(1,RI),
1249 E100_STRUCT_MASK(1,DSR),
1250 E100_STRUCT_MASK(1,CD)
1251#else
1252 CONTROL_PINS_PORT_NOT_USED(1)
1253#endif
1254 },
1255
1256 /* Ser 2 */
1257 {
1258#ifdef CONFIG_ETRAX_SERIAL_PORT2
1259 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
1260 E100_STRUCT_MASK(2,DTR),
1261 E100_STRUCT_MASK(2,RI),
1262 E100_STRUCT_MASK(2,DSR),
1263 E100_STRUCT_MASK(2,CD)
1264#else
1265 CONTROL_PINS_PORT_NOT_USED(2)
1266#endif
1267 },
1268
1269 /* Ser 3 */
1270 {
1271#ifdef CONFIG_ETRAX_SERIAL_PORT3
1272 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
1273 E100_STRUCT_MASK(3,DTR),
1274 E100_STRUCT_MASK(3,RI),
1275 E100_STRUCT_MASK(3,DSR),
1276 E100_STRUCT_MASK(3,CD)
1277#else
1278 CONTROL_PINS_PORT_NOT_USED(3)
1279#endif
1280 }
1281};
1282#endif /* !CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
1283
1284#define E100_RTS_MASK 0x20
1285#define E100_CTS_MASK 0x40
1286
1287/* All serial port signals are active low:
1288 * active = 0 -> 3.3V to RS-232 driver -> -12V on RS-232 level
1289 * inactive = 1 -> 0V to RS-232 driver -> +12V on RS-232 level
1290 *
1291 * These macros returns the pin value: 0=0V, >=1 = 3.3V on ETRAX chip
1292 */
1293
1294/* Output */
1295#define E100_RTS_GET(info) ((info)->rx_ctrl & E100_RTS_MASK)
1296/* Input */
1297#define E100_CTS_GET(info) ((info)->port[REG_STATUS] & E100_CTS_MASK)
1298
1299/* These are typically PA or PB and 0 means 0V, 1 means 3.3V */
1300/* Is an output */
1301#define E100_DTR_GET(info) ((*e100_modem_pins[(info)->line].dtr_shadow) & e100_modem_pins[(info)->line].dtr_mask)
1302
1303/* Normally inputs */
1304#define E100_RI_GET(info) ((*e100_modem_pins[(info)->line].ri_port) & e100_modem_pins[(info)->line].ri_mask)
1305#define E100_CD_GET(info) ((*e100_modem_pins[(info)->line].cd_port) & e100_modem_pins[(info)->line].cd_mask)
1306
1307/* Input */
1308#define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask)
1309
1310
1311/*
1312 * tmp_buf is used as a temporary buffer by serial_write. We need to
1313 * lock it in case the memcpy_fromfs blocks while swapping in a page,
1314 * and some other program tries to do a serial write at the same time.
1315 * Since the lock will only come under contention when the system is
1316 * swapping and available memory is low, it makes sense to share one
1317 * buffer across all the serial ports, since it significantly saves
1318 * memory if large numbers of serial ports are open.
1319 */
1320static unsigned char *tmp_buf;
1321#ifdef DECLARE_MUTEX
1322static DECLARE_MUTEX(tmp_buf_sem);
1323#else
1324static struct semaphore tmp_buf_sem = MUTEX;
1325#endif
1326
1327/* Calculate the chartime depending on baudrate, numbor of bits etc. */
1328static void update_char_time(struct e100_serial * info)
1329{
1330 tcflag_t cflags = info->tty->termios->c_cflag;
1331 int bits;
1332
1333 /* calc. number of bits / data byte */
1334 /* databits + startbit and 1 stopbit */
1335 if ((cflags & CSIZE) == CS7)
1336 bits = 9;
1337 else
1338 bits = 10;
1339
1340 if (cflags & CSTOPB) /* 2 stopbits ? */
1341 bits++;
1342
1343 if (cflags & PARENB) /* parity bit ? */
1344 bits++;
1345
1346 /* calc timeout */
1347 info->char_time_usec = ((bits * 1000000) / info->baud) + 1;
1348 info->flush_time_usec = 4*info->char_time_usec;
1349 if (info->flush_time_usec < MIN_FLUSH_TIME_USEC)
1350 info->flush_time_usec = MIN_FLUSH_TIME_USEC;
1351
1352}
1353
1354/*
1355 * This function maps from the Bxxxx defines in asm/termbits.h into real
1356 * baud rates.
1357 */
1358
1359static int
1360cflag_to_baud(unsigned int cflag)
1361{
1362 static int baud_table[] = {
1363 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
1364 4800, 9600, 19200, 38400 };
1365
1366 static int ext_baud_table[] = {
1367 0, 57600, 115200, 230400, 460800, 921600, 1843200, 6250000,
1368 0, 0, 0, 0, 0, 0, 0, 0 };
1369
1370 if (cflag & CBAUDEX)
1371 return ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1372 else
1373 return baud_table[cflag & CBAUD];
1374}
1375
1376/* and this maps to an etrax100 hardware baud constant */
1377
1378static unsigned char
1379cflag_to_etrax_baud(unsigned int cflag)
1380{
1381 char retval;
1382
1383 static char baud_table[] = {
1384 -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, 4, 5, 6, 7 };
1385
1386 static char ext_baud_table[] = {
1387 -1, 8, 9, 10, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 };
1388
1389 if (cflag & CBAUDEX)
1390 retval = ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1391 else
1392 retval = baud_table[cflag & CBAUD];
1393
1394 if (retval < 0) {
1395 printk(KERN_WARNING "serdriver tried setting invalid baud rate, flags %x.\n", cflag);
1396 retval = 5; /* choose default 9600 instead */
1397 }
1398
1399 return retval | (retval << 4); /* choose same for both TX and RX */
1400}
1401
1402
1403/* Various static support functions */
1404
1405/* Functions to set or clear DTR/RTS on the requested line */
1406/* It is complicated by the fact that RTS is a serial port register, while
1407 * DTR might not be implemented in the HW at all, and if it is, it can be on
1408 * any general port.
1409 */
1410
1411
1412static inline void
1413e100_dtr(struct e100_serial *info, int set)
1414{
1415#ifndef CONFIG_SVINTO_SIM
1416 unsigned char mask = e100_modem_pins[info->line].dtr_mask;
1417
1418#ifdef SERIAL_DEBUG_IO
1419 printk("ser%i dtr %i mask: 0x%02X\n", info->line, set, mask);
1420 printk("ser%i shadow before 0x%02X get: %i\n",
1421 info->line, *e100_modem_pins[info->line].dtr_shadow,
1422 E100_DTR_GET(info));
1423#endif
1424 /* DTR is active low */
1425 {
1426 unsigned long flags;
1427
1428 save_flags(flags);
1429 cli();
1430 *e100_modem_pins[info->line].dtr_shadow &= ~mask;
1431 *e100_modem_pins[info->line].dtr_shadow |= (set ? 0 : mask);
1432 *e100_modem_pins[info->line].dtr_port = *e100_modem_pins[info->line].dtr_shadow;
1433 restore_flags(flags);
1434 }
1435
1436#ifdef SERIAL_DEBUG_IO
1437 printk("ser%i shadow after 0x%02X get: %i\n",
1438 info->line, *e100_modem_pins[info->line].dtr_shadow,
1439 E100_DTR_GET(info));
1440#endif
1441#endif
1442}
1443
1444/* set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
1445 * 0=0V , 1=3.3V
1446 */
1447static inline void
1448e100_rts(struct e100_serial *info, int set)
1449{
1450#ifndef CONFIG_SVINTO_SIM
1451 unsigned long flags;
1452 save_flags(flags);
1453 cli();
1454 info->rx_ctrl &= ~E100_RTS_MASK;
1455 info->rx_ctrl |= (set ? 0 : E100_RTS_MASK); /* RTS is active low */
1456 info->port[REG_REC_CTRL] = info->rx_ctrl;
1457 restore_flags(flags);
1458#ifdef SERIAL_DEBUG_IO
1459 printk("ser%i rts %i\n", info->line, set);
1460#endif
1461#endif
1462}
1463
1464
1465/* If this behaves as a modem, RI and CD is an output */
1466static inline void
1467e100_ri_out(struct e100_serial *info, int set)
1468{
1469#ifndef CONFIG_SVINTO_SIM
1470 /* RI is active low */
1471 {
1472 unsigned char mask = e100_modem_pins[info->line].ri_mask;
1473 unsigned long flags;
1474
1475 save_flags(flags);
1476 cli();
1477 *e100_modem_pins[info->line].ri_shadow &= ~mask;
1478 *e100_modem_pins[info->line].ri_shadow |= (set ? 0 : mask);
1479 *e100_modem_pins[info->line].ri_port = *e100_modem_pins[info->line].ri_shadow;
1480 restore_flags(flags);
1481 }
1482#endif
1483}
1484static inline void
1485e100_cd_out(struct e100_serial *info, int set)
1486{
1487#ifndef CONFIG_SVINTO_SIM
1488 /* CD is active low */
1489 {
1490 unsigned char mask = e100_modem_pins[info->line].cd_mask;
1491 unsigned long flags;
1492
1493 save_flags(flags);
1494 cli();
1495 *e100_modem_pins[info->line].cd_shadow &= ~mask;
1496 *e100_modem_pins[info->line].cd_shadow |= (set ? 0 : mask);
1497 *e100_modem_pins[info->line].cd_port = *e100_modem_pins[info->line].cd_shadow;
1498 restore_flags(flags);
1499 }
1500#endif
1501}
1502
1503static inline void
1504e100_disable_rx(struct e100_serial *info)
1505{
1506#ifndef CONFIG_SVINTO_SIM
1507 /* disable the receiver */
1508 info->port[REG_REC_CTRL] =
1509 (info->rx_ctrl &= ~IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1510#endif
1511}
1512
1513static inline void
1514e100_enable_rx(struct e100_serial *info)
1515{
1516#ifndef CONFIG_SVINTO_SIM
1517 /* enable the receiver */
1518 info->port[REG_REC_CTRL] =
1519 (info->rx_ctrl |= IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1520#endif
1521}
1522
1523/* the rx DMA uses both the dma_descr and the dma_eop interrupts */
1524
1525static inline void
1526e100_disable_rxdma_irq(struct e100_serial *info)
1527{
1528#ifdef SERIAL_DEBUG_INTR
1529 printk("rxdma_irq(%d): 0\n",info->line);
1530#endif
1531 DINTR1(DEBUG_LOG(info->line,"IRQ disable_rxdma_irq %i\n", info->line));
1532 *R_IRQ_MASK2_CLR = (info->irq << 2) | (info->irq << 3);
1533}
1534
1535static inline void
1536e100_enable_rxdma_irq(struct e100_serial *info)
1537{
1538#ifdef SERIAL_DEBUG_INTR
1539 printk("rxdma_irq(%d): 1\n",info->line);
1540#endif
1541 DINTR1(DEBUG_LOG(info->line,"IRQ enable_rxdma_irq %i\n", info->line));
1542 *R_IRQ_MASK2_SET = (info->irq << 2) | (info->irq << 3);
1543}
1544
1545/* the tx DMA uses only dma_descr interrupt */
1546
1547static _INLINE_ void
1548e100_disable_txdma_irq(struct e100_serial *info)
1549{
1550#ifdef SERIAL_DEBUG_INTR
1551 printk("txdma_irq(%d): 0\n",info->line);
1552#endif
1553 DINTR1(DEBUG_LOG(info->line,"IRQ disable_txdma_irq %i\n", info->line));
1554 *R_IRQ_MASK2_CLR = info->irq;
1555}
1556
1557static _INLINE_ void
1558e100_enable_txdma_irq(struct e100_serial *info)
1559{
1560#ifdef SERIAL_DEBUG_INTR
1561 printk("txdma_irq(%d): 1\n",info->line);
1562#endif
1563 DINTR1(DEBUG_LOG(info->line,"IRQ enable_txdma_irq %i\n", info->line));
1564 *R_IRQ_MASK2_SET = info->irq;
1565}
1566
1567static _INLINE_ void
1568e100_disable_txdma_channel(struct e100_serial *info)
1569{
1570 unsigned long flags;
1571
1572 /* Disable output DMA channel for the serial port in question
1573 * ( set to something other then serialX)
1574 */
1575 save_flags(flags);
1576 cli();
1577 DFLOW(DEBUG_LOG(info->line, "disable_txdma_channel %i\n", info->line));
1578 if (info->line == 0) {
1579 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma6)) ==
1580 IO_STATE(R_GEN_CONFIG, dma6, serial0)) {
1581 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1582 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
1583 }
1584 } else if (info->line == 1) {
1585 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma8)) ==
1586 IO_STATE(R_GEN_CONFIG, dma8, serial1)) {
1587 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1588 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
1589 }
1590 } else if (info->line == 2) {
1591 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma2)) ==
1592 IO_STATE(R_GEN_CONFIG, dma2, serial2)) {
1593 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1594 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
1595 }
1596 } else if (info->line == 3) {
1597 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma4)) ==
1598 IO_STATE(R_GEN_CONFIG, dma4, serial3)) {
1599 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1600 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
1601 }
1602 }
1603 *R_GEN_CONFIG = genconfig_shadow;
1604 restore_flags(flags);
1605}
1606
1607
1608static _INLINE_ void
1609e100_enable_txdma_channel(struct e100_serial *info)
1610{
1611 unsigned long flags;
1612
1613 save_flags(flags);
1614 cli();
1615 DFLOW(DEBUG_LOG(info->line, "enable_txdma_channel %i\n", info->line));
1616 /* Enable output DMA channel for the serial port in question */
1617 if (info->line == 0) {
1618 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1619 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, serial0);
1620 } else if (info->line == 1) {
1621 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1622 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, serial1);
1623 } else if (info->line == 2) {
1624 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1625 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, serial2);
1626 } else if (info->line == 3) {
1627 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1628 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, serial3);
1629 }
1630 *R_GEN_CONFIG = genconfig_shadow;
1631 restore_flags(flags);
1632}
1633
1634static _INLINE_ void
1635e100_disable_rxdma_channel(struct e100_serial *info)
1636{
1637 unsigned long flags;
1638
1639 /* Disable input DMA channel for the serial port in question
1640 * ( set to something other then serialX)
1641 */
1642 save_flags(flags);
1643 cli();
1644 if (info->line == 0) {
1645 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma7)) ==
1646 IO_STATE(R_GEN_CONFIG, dma7, serial0)) {
1647 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1648 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, unused);
1649 }
1650 } else if (info->line == 1) {
1651 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma9)) ==
1652 IO_STATE(R_GEN_CONFIG, dma9, serial1)) {
1653 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1654 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, usb);
1655 }
1656 } else if (info->line == 2) {
1657 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma3)) ==
1658 IO_STATE(R_GEN_CONFIG, dma3, serial2)) {
1659 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1660 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
1661 }
1662 } else if (info->line == 3) {
1663 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma5)) ==
1664 IO_STATE(R_GEN_CONFIG, dma5, serial3)) {
1665 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1666 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
1667 }
1668 }
1669 *R_GEN_CONFIG = genconfig_shadow;
1670 restore_flags(flags);
1671}
1672
1673
1674static _INLINE_ void
1675e100_enable_rxdma_channel(struct e100_serial *info)
1676{
1677 unsigned long flags;
1678
1679 save_flags(flags);
1680 cli();
1681 /* Enable input DMA channel for the serial port in question */
1682 if (info->line == 0) {
1683 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1684 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, serial0);
1685 } else if (info->line == 1) {
1686 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1687 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, serial1);
1688 } else if (info->line == 2) {
1689 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1690 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, serial2);
1691 } else if (info->line == 3) {
1692 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1693 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, serial3);
1694 }
1695 *R_GEN_CONFIG = genconfig_shadow;
1696 restore_flags(flags);
1697}
1698
1699#ifdef SERIAL_HANDLE_EARLY_ERRORS
1700/* in order to detect and fix errors on the first byte
1701 we have to use the serial interrupts as well. */
1702
1703static inline void
1704e100_disable_serial_data_irq(struct e100_serial *info)
1705{
1706#ifdef SERIAL_DEBUG_INTR
1707 printk("ser_irq(%d): 0\n",info->line);
1708#endif
1709 DINTR1(DEBUG_LOG(info->line,"IRQ disable data_irq %i\n", info->line));
1710 *R_IRQ_MASK1_CLR = (1U << (8+2*info->line));
1711}
1712
1713static inline void
1714e100_enable_serial_data_irq(struct e100_serial *info)
1715{
1716#ifdef SERIAL_DEBUG_INTR
1717 printk("ser_irq(%d): 1\n",info->line);
1718 printk("**** %d = %d\n",
1719 (8+2*info->line),
1720 (1U << (8+2*info->line)));
1721#endif
1722 DINTR1(DEBUG_LOG(info->line,"IRQ enable data_irq %i\n", info->line));
1723 *R_IRQ_MASK1_SET = (1U << (8+2*info->line));
1724}
1725#endif
1726
1727static inline void
1728e100_disable_serial_tx_ready_irq(struct e100_serial *info)
1729{
1730#ifdef SERIAL_DEBUG_INTR
1731 printk("ser_tx_irq(%d): 0\n",info->line);
1732#endif
1733 DINTR1(DEBUG_LOG(info->line,"IRQ disable ready_irq %i\n", info->line));
1734 *R_IRQ_MASK1_CLR = (1U << (8+1+2*info->line));
1735}
1736
1737static inline void
1738e100_enable_serial_tx_ready_irq(struct e100_serial *info)
1739{
1740#ifdef SERIAL_DEBUG_INTR
1741 printk("ser_tx_irq(%d): 1\n",info->line);
1742 printk("**** %d = %d\n",
1743 (8+1+2*info->line),
1744 (1U << (8+1+2*info->line)));
1745#endif
1746 DINTR2(DEBUG_LOG(info->line,"IRQ enable ready_irq %i\n", info->line));
1747 *R_IRQ_MASK1_SET = (1U << (8+1+2*info->line));
1748}
1749
1750static inline void e100_enable_rx_irq(struct e100_serial *info)
1751{
1752 if (info->uses_dma_in)
1753 e100_enable_rxdma_irq(info);
1754 else
1755 e100_enable_serial_data_irq(info);
1756}
1757static inline void e100_disable_rx_irq(struct e100_serial *info)
1758{
1759 if (info->uses_dma_in)
1760 e100_disable_rxdma_irq(info);
1761 else
1762 e100_disable_serial_data_irq(info);
1763}
1764
1765#if defined(CONFIG_ETRAX_RS485)
1766/* Enable RS-485 mode on selected port. This is UGLY. */
1767static int
1768e100_enable_rs485(struct tty_struct *tty,struct rs485_control *r)
1769{
1770 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1771
1772#if defined(CONFIG_ETRAX_RS485_ON_PA)
1773 *R_PORT_PA_DATA = port_pa_data_shadow |= (1 << rs485_pa_bit);
1774#endif
1775#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
1776 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1777 rs485_port_g_bit, 1);
1778#endif
1779#if defined(CONFIG_ETRAX_RS485_LTC1387)
1780 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1781 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT, 1);
1782 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1783 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT, 1);
1784#endif
1785
1786 info->rs485.rts_on_send = 0x01 & r->rts_on_send;
1787 info->rs485.rts_after_sent = 0x01 & r->rts_after_sent;
1788 if (r->delay_rts_before_send >= 1000)
1789 info->rs485.delay_rts_before_send = 1000;
1790 else
1791 info->rs485.delay_rts_before_send = r->delay_rts_before_send;
1792 info->rs485.enabled = r->enabled;
1793/* printk("rts: on send = %i, after = %i, enabled = %i",
1794 info->rs485.rts_on_send,
1795 info->rs485.rts_after_sent,
1796 info->rs485.enabled
1797 );
1798*/
1799 return 0;
1800}
1801
1802static int
1803e100_write_rs485(struct tty_struct *tty, int from_user,
1804 const unsigned char *buf, int count)
1805{
1806 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1807 int old_enabled = info->rs485.enabled;
1808
1809 /* rs485 is always implicitly enabled if we're using the ioctl()
1810 * but it doesn't have to be set in the rs485_control
1811 * (to be backward compatible with old apps)
1812 * So we store, set and restore it.
1813 */
1814 info->rs485.enabled = 1;
1815 /* rs_write now deals with RS485 if enabled */
1816 count = rs_write(tty, from_user, buf, count);
1817 info->rs485.enabled = old_enabled;
1818 return count;
1819}
1820
1821#ifdef CONFIG_ETRAX_FAST_TIMER
1822/* Timer function to toggle RTS when using FAST_TIMER */
1823static void rs485_toggle_rts_timer_function(unsigned long data)
1824{
1825 struct e100_serial *info = (struct e100_serial *)data;
1826
1827 fast_timers_rs485[info->line].function = NULL;
1828 e100_rts(info, info->rs485.rts_after_sent);
1829#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
1830 e100_enable_rx(info);
1831 e100_enable_rx_irq(info);
1832#endif
1833}
1834#endif
1835#endif /* CONFIG_ETRAX_RS485 */
1836
1837/*
1838 * ------------------------------------------------------------
1839 * rs_stop() and rs_start()
1840 *
1841 * This routines are called before setting or resetting tty->stopped.
1842 * They enable or disable transmitter using the XOFF registers, as necessary.
1843 * ------------------------------------------------------------
1844 */
1845
1846static void
1847rs_stop(struct tty_struct *tty)
1848{
1849 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1850 if (info) {
1851 unsigned long flags;
1852 unsigned long xoff;
1853
1854 save_flags(flags); cli();
1855 DFLOW(DEBUG_LOG(info->line, "XOFF rs_stop xmit %i\n",
1856 CIRC_CNT(info->xmit.head,
1857 info->xmit.tail,SERIAL_XMIT_SIZE)));
1858
1859 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->tty));
1860 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, stop);
1861 if (tty->termios->c_iflag & IXON ) {
1862 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1863 }
1864
1865 *((unsigned long *)&info->port[REG_XOFF]) = xoff;
1866 restore_flags(flags);
1867 }
1868}
1869
1870static void
1871rs_start(struct tty_struct *tty)
1872{
1873 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1874 if (info) {
1875 unsigned long flags;
1876 unsigned long xoff;
1877
1878 save_flags(flags); cli();
1879 DFLOW(DEBUG_LOG(info->line, "XOFF rs_start xmit %i\n",
1880 CIRC_CNT(info->xmit.head,
1881 info->xmit.tail,SERIAL_XMIT_SIZE)));
1882 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(tty));
1883 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
1884 if (tty->termios->c_iflag & IXON ) {
1885 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1886 }
1887
1888 *((unsigned long *)&info->port[REG_XOFF]) = xoff;
1889 if (!info->uses_dma_out &&
1890 info->xmit.head != info->xmit.tail && info->xmit.buf)
1891 e100_enable_serial_tx_ready_irq(info);
1892
1893 restore_flags(flags);
1894 }
1895}
1896
1897/*
1898 * ----------------------------------------------------------------------
1899 *
1900 * Here starts the interrupt handling routines. All of the following
1901 * subroutines are declared as inline and are folded into
1902 * rs_interrupt(). They were separated out for readability's sake.
1903 *
1904 * Note: rs_interrupt() is a "fast" interrupt, which means that it
1905 * runs with interrupts turned off. People who may want to modify
1906 * rs_interrupt() should try to keep the interrupt handler as fast as
1907 * possible. After you are done making modifications, it is not a bad
1908 * idea to do:
1909 *
1910 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
1911 *
1912 * and look at the resulting assemble code in serial.s.
1913 *
1914 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
1915 * -----------------------------------------------------------------------
1916 */
1917
1918/*
1919 * This routine is used by the interrupt handler to schedule
1920 * processing in the software interrupt portion of the driver.
1921 */
1922static _INLINE_ void
1923rs_sched_event(struct e100_serial *info,
1924 int event)
1925{
1926 if (info->event & (1 << event))
1927 return;
1928 info->event |= 1 << event;
1929 schedule_work(&info->work);
1930}
1931
1932/* The output DMA channel is free - use it to send as many chars as possible
1933 * NOTES:
1934 * We don't pay attention to info->x_char, which means if the TTY wants to
1935 * use XON/XOFF it will set info->x_char but we won't send any X char!
1936 *
1937 * To implement this, we'd just start a DMA send of 1 byte pointing at a
1938 * buffer containing the X char, and skip updating xmit. We'd also have to
1939 * check if the last sent char was the X char when we enter this function
1940 * the next time, to avoid updating xmit with the sent X value.
1941 */
1942
1943static void
1944transmit_chars_dma(struct e100_serial *info)
1945{
1946 unsigned int c, sentl;
1947 struct etrax_dma_descr *descr;
1948
1949#ifdef CONFIG_SVINTO_SIM
1950 /* This will output too little if tail is not 0 always since
1951 * we don't reloop to send the other part. Anyway this SHOULD be a
1952 * no-op - transmit_chars_dma would never really be called during sim
1953 * since rs_write does not write into the xmit buffer then.
1954 */
1955 if (info->xmit.tail)
1956 printk("Error in serial.c:transmit_chars-dma(), tail!=0\n");
1957 if (info->xmit.head != info->xmit.tail) {
1958 SIMCOUT(info->xmit.buf + info->xmit.tail,
1959 CIRC_CNT(info->xmit.head,
1960 info->xmit.tail,
1961 SERIAL_XMIT_SIZE));
1962 info->xmit.head = info->xmit.tail; /* move back head */
1963 info->tr_running = 0;
1964 }
1965 return;
1966#endif
1967 /* acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1968 *info->oclrintradr =
1969 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1970 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1971
1972#ifdef SERIAL_DEBUG_INTR
1973 if (info->line == SERIAL_DEBUG_LINE)
1974 printk("tc\n");
1975#endif
1976 if (!info->tr_running) {
1977 /* weirdo... we shouldn't get here! */
1978 printk(KERN_WARNING "Achtung: transmit_chars_dma with !tr_running\n");
1979 return;
1980 }
1981
1982 descr = &info->tr_descr;
1983
1984 /* first get the amount of bytes sent during the last DMA transfer,
1985 and update xmit accordingly */
1986
1987 /* if the stop bit was not set, all data has been sent */
1988 if (!(descr->status & d_stop)) {
1989 sentl = descr->sw_len;
1990 } else
1991 /* otherwise we find the amount of data sent here */
1992 sentl = descr->hw_len;
1993
1994 DFLOW(DEBUG_LOG(info->line, "TX %i done\n", sentl));
1995
1996 /* update stats */
1997 info->icount.tx += sentl;
1998
1999 /* update xmit buffer */
2000 info->xmit.tail = (info->xmit.tail + sentl) & (SERIAL_XMIT_SIZE - 1);
2001
2002 /* if there is only a few chars left in the buf, wake up the blocked
2003 write if any */
2004 if (CIRC_CNT(info->xmit.head,
2005 info->xmit.tail,
2006 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
2007 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
2008
2009 /* find out the largest amount of consecutive bytes we want to send now */
2010
2011 c = CIRC_CNT_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
2012
2013 /* Don't send all in one DMA transfer - divide it so we wake up
2014 * application before all is sent
2015 */
2016
2017 if (c >= 4*WAKEUP_CHARS)
2018 c = c/2;
2019
2020 if (c <= 0) {
2021 /* our job here is done, don't schedule any new DMA transfer */
2022 info->tr_running = 0;
2023
2024#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
2025 if (info->rs485.enabled) {
2026 /* Set a short timer to toggle RTS */
2027 start_one_shot_timer(&fast_timers_rs485[info->line],
2028 rs485_toggle_rts_timer_function,
2029 (unsigned long)info,
2030 info->char_time_usec*2,
2031 "RS-485");
2032 }
2033#endif /* RS485 */
2034 return;
2035 }
2036
2037 /* ok we can schedule a dma send of c chars starting at info->xmit.tail */
2038 /* set up the descriptor correctly for output */
2039 DFLOW(DEBUG_LOG(info->line, "TX %i\n", c));
2040 descr->ctrl = d_int | d_eol | d_wait; /* Wait needed for tty_wait_until_sent() */
2041 descr->sw_len = c;
2042 descr->buf = virt_to_phys(info->xmit.buf + info->xmit.tail);
2043 descr->status = 0;
2044
2045 *info->ofirstadr = virt_to_phys(descr); /* write to R_DMAx_FIRST */
2046 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
2047
2048 /* DMA is now running (hopefully) */
2049} /* transmit_chars_dma */
2050
2051static void
2052start_transmit(struct e100_serial *info)
2053{
2054#if 0
2055 if (info->line == SERIAL_DEBUG_LINE)
2056 printk("x\n");
2057#endif
2058
2059 info->tr_descr.sw_len = 0;
2060 info->tr_descr.hw_len = 0;
2061 info->tr_descr.status = 0;
2062 info->tr_running = 1;
2063 if (info->uses_dma_out)
2064 transmit_chars_dma(info);
2065 else
2066 e100_enable_serial_tx_ready_irq(info);
2067} /* start_transmit */
2068
2069#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
2070static int serial_fast_timer_started = 0;
2071static int serial_fast_timer_expired = 0;
2072static void flush_timeout_function(unsigned long data);
2073#define START_FLUSH_FAST_TIMER_TIME(info, string, usec) {\
2074 unsigned long timer_flags; \
2075 save_flags(timer_flags); \
2076 cli(); \
2077 if (fast_timers[info->line].function == NULL) { \
2078 serial_fast_timer_started++; \
2079 TIMERD(DEBUG_LOG(info->line, "start_timer %i ", info->line)); \
2080 TIMERD(DEBUG_LOG(info->line, "num started: %i\n", serial_fast_timer_started)); \
2081 start_one_shot_timer(&fast_timers[info->line], \
2082 flush_timeout_function, \
2083 (unsigned long)info, \
2084 (usec), \
2085 string); \
2086 } \
2087 else { \
2088 TIMERD(DEBUG_LOG(info->line, "timer %i already running\n", info->line)); \
2089 } \
2090 restore_flags(timer_flags); \
2091}
2092#define START_FLUSH_FAST_TIMER(info, string) START_FLUSH_FAST_TIMER_TIME(info, string, info->flush_time_usec)
2093
2094#else
2095#define START_FLUSH_FAST_TIMER_TIME(info, string, usec)
2096#define START_FLUSH_FAST_TIMER(info, string)
2097#endif
2098
2099static struct etrax_recv_buffer *
2100alloc_recv_buffer(unsigned int size)
2101{
2102 struct etrax_recv_buffer *buffer;
2103
2104 if (!(buffer = kmalloc(sizeof *buffer + size, GFP_ATOMIC)))
2105 return NULL;
2106
2107 buffer->next = NULL;
2108 buffer->length = 0;
2109 buffer->error = TTY_NORMAL;
2110
2111 return buffer;
2112}
2113
2114static void
2115append_recv_buffer(struct e100_serial *info, struct etrax_recv_buffer *buffer)
2116{
2117 unsigned long flags;
2118
2119 save_flags(flags);
2120 cli();
2121
2122 if (!info->first_recv_buffer)
2123 info->first_recv_buffer = buffer;
2124 else
2125 info->last_recv_buffer->next = buffer;
2126
2127 info->last_recv_buffer = buffer;
2128
2129 info->recv_cnt += buffer->length;
2130 if (info->recv_cnt > info->max_recv_cnt)
2131 info->max_recv_cnt = info->recv_cnt;
2132
2133 restore_flags(flags);
2134}
2135
2136static int
2137add_char_and_flag(struct e100_serial *info, unsigned char data, unsigned char flag)
2138{
2139 struct etrax_recv_buffer *buffer;
2140 if (info->uses_dma_in) {
2141 if (!(buffer = alloc_recv_buffer(4)))
2142 return 0;
2143
2144 buffer->length = 1;
2145 buffer->error = flag;
2146 buffer->buffer[0] = data;
2147
2148 append_recv_buffer(info, buffer);
2149
2150 info->icount.rx++;
2151 } else {
2152 struct tty_struct *tty = info->tty;
2153 *tty->flip.char_buf_ptr = data;
2154 *tty->flip.flag_buf_ptr = flag;
2155 tty->flip.flag_buf_ptr++;
2156 tty->flip.char_buf_ptr++;
2157 tty->flip.count++;
2158 info->icount.rx++;
2159 }
2160
2161 return 1;
2162}
2163
2164extern _INLINE_ unsigned int
2165handle_descr_data(struct e100_serial *info, struct etrax_dma_descr *descr, unsigned int recvl)
2166{
2167 struct etrax_recv_buffer *buffer = phys_to_virt(descr->buf) - sizeof *buffer;
2168
2169 if (info->recv_cnt + recvl > 65536) {
2170 printk(KERN_CRIT
2171 "%s: Too much pending incoming serial data! Dropping %u bytes.\n", __FUNCTION__, recvl);
2172 return 0;
2173 }
2174
2175 buffer->length = recvl;
2176
2177 if (info->errorcode == ERRCODE_SET_BREAK)
2178 buffer->error = TTY_BREAK;
2179 info->errorcode = 0;
2180
2181 append_recv_buffer(info, buffer);
2182
2183 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
2184 panic("%s: Failed to allocate memory for receive buffer!\n", __FUNCTION__);
2185
2186 descr->buf = virt_to_phys(buffer->buffer);
2187
2188 return recvl;
2189}
2190
2191static _INLINE_ unsigned int
2192handle_all_descr_data(struct e100_serial *info)
2193{
2194 struct etrax_dma_descr *descr;
2195 unsigned int recvl;
2196 unsigned int ret = 0;
2197
2198 while (1)
2199 {
2200 descr = &info->rec_descr[info->cur_rec_descr];
2201
2202 if (descr == phys_to_virt(*info->idescradr))
2203 break;
2204
2205 if (++info->cur_rec_descr == SERIAL_RECV_DESCRIPTORS)
2206 info->cur_rec_descr = 0;
2207
2208 /* find out how many bytes were read */
2209
2210 /* if the eop bit was not set, all data has been received */
2211 if (!(descr->status & d_eop)) {
2212 recvl = descr->sw_len;
2213 } else {
2214 /* otherwise we find the amount of data received here */
2215 recvl = descr->hw_len;
2216 }
2217
2218 /* Reset the status information */
2219 descr->status = 0;
2220
2221 DFLOW( DEBUG_LOG(info->line, "RX %lu\n", recvl);
2222 if (info->tty->stopped) {
2223 unsigned char *buf = phys_to_virt(descr->buf);
2224 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[0]);
2225 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[1]);
2226 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[2]);
2227 }
2228 );
2229
2230 /* update stats */
2231 info->icount.rx += recvl;
2232
2233 ret += handle_descr_data(info, descr, recvl);
2234 }
2235
2236 return ret;
2237}
2238
2239static _INLINE_ void
2240receive_chars_dma(struct e100_serial *info)
2241{
2242 struct tty_struct *tty;
2243 unsigned char rstat;
2244
2245#ifdef CONFIG_SVINTO_SIM
2246 /* No receive in the simulator. Will probably be when the rest of
2247 * the serial interface works, and this piece will just be removed.
2248 */
2249 return;
2250#endif
2251
2252 /* Acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
2253 *info->iclrintradr =
2254 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2255 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2256
2257 tty = info->tty;
2258 if (!tty) /* Something wrong... */
2259 return;
2260
2261#ifdef SERIAL_HANDLE_EARLY_ERRORS
2262 if (info->uses_dma_in)
2263 e100_enable_serial_data_irq(info);
2264#endif
2265
2266 if (info->errorcode == ERRCODE_INSERT_BREAK)
2267 add_char_and_flag(info, '\0', TTY_BREAK);
2268
2269 handle_all_descr_data(info);
2270
2271 /* Read the status register to detect errors */
2272 rstat = info->port[REG_STATUS];
2273 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
2274 DFLOW(DEBUG_LOG(info->line, "XOFF detect stat %x\n", rstat));
2275 }
2276
2277 if (rstat & SER_ERROR_MASK) {
2278 /* If we got an error, we must reset it by reading the
2279 * data_in field
2280 */
2281 unsigned char data = info->port[REG_DATA];
2282
2283 PROCSTAT(ser_stat[info->line].errors_cnt++);
2284 DEBUG_LOG(info->line, "#dERR: s d 0x%04X\n",
2285 ((rstat & SER_ERROR_MASK) << 8) | data);
2286
2287 if (rstat & SER_PAR_ERR_MASK)
2288 add_char_and_flag(info, data, TTY_PARITY);
2289 else if (rstat & SER_OVERRUN_MASK)
2290 add_char_and_flag(info, data, TTY_OVERRUN);
2291 else if (rstat & SER_FRAMING_ERR_MASK)
2292 add_char_and_flag(info, data, TTY_FRAME);
2293 }
2294
2295 START_FLUSH_FAST_TIMER(info, "receive_chars");
2296
2297 /* Restart the receiving DMA */
2298 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
2299}
2300
2301static _INLINE_ int
2302start_recv_dma(struct e100_serial *info)
2303{
2304 struct etrax_dma_descr *descr = info->rec_descr;
2305 struct etrax_recv_buffer *buffer;
2306 int i;
2307
2308 /* Set up the receiving descriptors */
2309 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++) {
2310 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
2311 panic("%s: Failed to allocate memory for receive buffer!\n", __FUNCTION__);
2312
2313 descr[i].ctrl = d_int;
2314 descr[i].buf = virt_to_phys(buffer->buffer);
2315 descr[i].sw_len = SERIAL_DESCR_BUF_SIZE;
2316 descr[i].hw_len = 0;
2317 descr[i].status = 0;
2318 descr[i].next = virt_to_phys(&descr[i+1]);
2319 }
2320
2321 /* Link the last descriptor to the first */
2322 descr[i-1].next = virt_to_phys(&descr[0]);
2323
2324 /* Start with the first descriptor in the list */
2325 info->cur_rec_descr = 0;
2326
2327 /* Start the DMA */
2328 *info->ifirstadr = virt_to_phys(&descr[info->cur_rec_descr]);
2329 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
2330
2331 /* Input DMA should be running now */
2332 return 1;
2333}
2334
2335static void
2336start_receive(struct e100_serial *info)
2337{
2338#ifdef CONFIG_SVINTO_SIM
2339 /* No receive in the simulator. Will probably be when the rest of
2340 * the serial interface works, and this piece will just be removed.
2341 */
2342 return;
2343#endif
2344 info->tty->flip.count = 0;
2345 if (info->uses_dma_in) {
2346 /* reset the input dma channel to be sure it works */
2347
2348 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2349 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
2350 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2351
2352 start_recv_dma(info);
2353 }
2354}
2355
2356
2357static _INLINE_ void
2358status_handle(struct e100_serial *info, unsigned short status)
2359{
2360}
2361
2362/* the bits in the MASK2 register are laid out like this:
2363 DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
2364 where I is the input channel and O is the output channel for the port.
2365 info->irq is the bit number for the DMAO_DESCR so to check the others we
2366 shift info->irq to the left.
2367*/
2368
2369/* dma output channel interrupt handler
2370 this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
2371 DMA8(ser1) when they have finished a descriptor with the intr flag set.
2372*/
2373
2374static irqreturn_t
2375tr_interrupt(int irq, void *dev_id, struct pt_regs * regs)
2376{
2377 struct e100_serial *info;
2378 unsigned long ireg;
2379 int i;
2380 int handled = 0;
2381
2382#ifdef CONFIG_SVINTO_SIM
2383 /* No receive in the simulator. Will probably be when the rest of
2384 * the serial interface works, and this piece will just be removed.
2385 */
2386 {
2387 const char *s = "What? tr_interrupt in simulator??\n";
2388 SIMCOUT(s,strlen(s));
2389 }
2390 return IRQ_HANDLED;
2391#endif
2392
2393 /* find out the line that caused this irq and get it from rs_table */
2394
2395 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
2396
2397 for (i = 0; i < NR_PORTS; i++) {
2398 info = rs_table + i;
2399 if (!info->enabled || !info->uses_dma_out)
2400 continue;
2401 /* check for dma_descr (don't need to check for dma_eop in output dma for serial */
2402 if (ireg & info->irq) {
2403 handled = 1;
2404 /* we can send a new dma bunch. make it so. */
2405 DINTR2(DEBUG_LOG(info->line, "tr_interrupt %i\n", i));
2406 /* Read jiffies_usec first,
2407 * we want this time to be as late as possible
2408 */
2409 PROCSTAT(ser_stat[info->line].tx_dma_ints++);
2410 info->last_tx_active_usec = GET_JIFFIES_USEC();
2411 info->last_tx_active = jiffies;
2412 transmit_chars_dma(info);
2413 }
2414
2415 /* FIXME: here we should really check for a change in the
2416 status lines and if so call status_handle(info) */
2417 }
2418 return IRQ_RETVAL(handled);
2419} /* tr_interrupt */
2420
2421/* dma input channel interrupt handler */
2422
2423static irqreturn_t
2424rec_interrupt(int irq, void *dev_id, struct pt_regs * regs)
2425{
2426 struct e100_serial *info;
2427 unsigned long ireg;
2428 int i;
2429 int handled = 0;
2430
2431#ifdef CONFIG_SVINTO_SIM
2432 /* No receive in the simulator. Will probably be when the rest of
2433 * the serial interface works, and this piece will just be removed.
2434 */
2435 {
2436 const char *s = "What? rec_interrupt in simulator??\n";
2437 SIMCOUT(s,strlen(s));
2438 }
2439 return IRQ_HANDLED;
2440#endif
2441
2442 /* find out the line that caused this irq and get it from rs_table */
2443
2444 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
2445
2446 for (i = 0; i < NR_PORTS; i++) {
2447 info = rs_table + i;
2448 if (!info->enabled || !info->uses_dma_in)
2449 continue;
2450 /* check for both dma_eop and dma_descr for the input dma channel */
2451 if (ireg & ((info->irq << 2) | (info->irq << 3))) {
2452 handled = 1;
2453 /* we have received something */
2454 receive_chars_dma(info);
2455 }
2456
2457 /* FIXME: here we should really check for a change in the
2458 status lines and if so call status_handle(info) */
2459 }
2460 return IRQ_RETVAL(handled);
2461} /* rec_interrupt */
2462
2463static _INLINE_ int
2464force_eop_if_needed(struct e100_serial *info)
2465{
2466 /* We check data_avail bit to determine if data has
2467 * arrived since last time
2468 */
2469 unsigned char rstat = info->port[REG_STATUS];
2470
2471 /* error or datavail? */
2472 if (rstat & SER_ERROR_MASK) {
2473 /* Some error has occurred. If there has been valid data, an
2474 * EOP interrupt will be made automatically. If no data, the
2475 * normal ser_interrupt should be enabled and handle it.
2476 * So do nothing!
2477 */
2478 DEBUG_LOG(info->line, "timeout err: rstat 0x%03X\n",
2479 rstat | (info->line << 8));
2480 return 0;
2481 }
2482
2483 if (rstat & SER_DATA_AVAIL_MASK) {
2484 /* Ok data, no error, count it */
2485 TIMERD(DEBUG_LOG(info->line, "timeout: rstat 0x%03X\n",
2486 rstat | (info->line << 8)));
2487 /* Read data to clear status flags */
2488 (void)info->port[REG_DATA];
2489
2490 info->forced_eop = 0;
2491 START_FLUSH_FAST_TIMER(info, "magic");
2492 return 0;
2493 }
2494
2495 /* hit the timeout, force an EOP for the input
2496 * dma channel if we haven't already
2497 */
2498 if (!info->forced_eop) {
2499 info->forced_eop = 1;
2500 PROCSTAT(ser_stat[info->line].timeout_flush_cnt++);
2501 TIMERD(DEBUG_LOG(info->line, "timeout EOP %i\n", info->line));
2502 FORCE_EOP(info);
2503 }
2504
2505 return 1;
2506}
2507
2508extern _INLINE_ void
2509flush_to_flip_buffer(struct e100_serial *info)
2510{
2511 struct tty_struct *tty;
2512 struct etrax_recv_buffer *buffer;
2513 unsigned int length;
2514 unsigned long flags;
2515 int max_flip_size;
2516
2517 if (!info->first_recv_buffer)
2518 return;
2519
2520 save_flags(flags);
2521 cli();
2522
2523 if (!(tty = info->tty)) {
2524 restore_flags(flags);
2525 return;
2526 }
2527
2528 length = tty->flip.count;
2529 /* Don't flip more than the ldisc has room for.
2530 * The return value from ldisc.receive_room(tty) - might not be up to
2531 * date, the previous flip of up to TTY_FLIPBUF_SIZE might be on the
2532 * processed and not accounted for yet.
2533 * Since we use DMA, 1 SERIAL_DESCR_BUF_SIZE could be on the way.
2534 * Lets buffer data here and let flow control take care of it.
2535 * Since we normally flip large chunks, the ldisc don't react
2536 * with throttle until too late if we flip to much.
2537 */
2538 max_flip_size = tty->ldisc.receive_room(tty);
2539 if (max_flip_size < 0)
2540 max_flip_size = 0;
2541 if (max_flip_size <= (TTY_FLIPBUF_SIZE + /* Maybe not accounted for */
2542 length + info->recv_cnt + /* We have this queued */
2543 2*SERIAL_DESCR_BUF_SIZE + /* This could be on the way */
2544 TTY_THRESHOLD_THROTTLE)) { /* Some slack */
2545 /* check TTY_THROTTLED first so it indicates our state */
2546 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags)) {
2547 DFLOW(DEBUG_LOG(info->line,"flush_to_flip throttles room %lu\n", max_flip_size));
2548 rs_throttle(tty);
2549 }
2550#if 0
2551 else if (max_flip_size <= (TTY_FLIPBUF_SIZE + /* Maybe not accounted for */
2552 length + info->recv_cnt + /* We have this queued */
2553 SERIAL_DESCR_BUF_SIZE + /* This could be on the way */
2554 TTY_THRESHOLD_THROTTLE)) { /* Some slack */
2555 DFLOW(DEBUG_LOG(info->line,"flush_to_flip throttles again! %lu\n", max_flip_size));
2556 rs_throttle(tty);
2557 }
2558#endif
2559 }
2560
2561 if (max_flip_size > TTY_FLIPBUF_SIZE)
2562 max_flip_size = TTY_FLIPBUF_SIZE;
2563
2564 while ((buffer = info->first_recv_buffer) && length < max_flip_size) {
2565 unsigned int count = buffer->length;
2566
2567 if (length + count > max_flip_size)
2568 count = max_flip_size - length;
2569
2570 memcpy(tty->flip.char_buf_ptr + length, buffer->buffer, count);
2571 memset(tty->flip.flag_buf_ptr + length, TTY_NORMAL, count);
2572 tty->flip.flag_buf_ptr[length] = buffer->error;
2573
2574 length += count;
2575 info->recv_cnt -= count;
2576 DFLIP(DEBUG_LOG(info->line,"flip: %i\n", length));
2577
2578 if (count == buffer->length) {
2579 info->first_recv_buffer = buffer->next;
2580 kfree(buffer);
2581 } else {
2582 buffer->length -= count;
2583 memmove(buffer->buffer, buffer->buffer + count, buffer->length);
2584 buffer->error = TTY_NORMAL;
2585 }
2586 }
2587
2588 if (!info->first_recv_buffer)
2589 info->last_recv_buffer = NULL;
2590
2591 tty->flip.count = length;
2592 DFLIP(if (tty->ldisc.chars_in_buffer(tty) > 3500) {
2593 DEBUG_LOG(info->line, "ldisc %lu\n",
2594 tty->ldisc.chars_in_buffer(tty));
2595 DEBUG_LOG(info->line, "flip.count %lu\n",
2596 tty->flip.count);
2597 }
2598 );
2599 restore_flags(flags);
2600
2601 DFLIP(
2602 if (1) {
2603
2604 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
2605 DEBUG_LOG(info->line, "*** TTY_DONT_FLIP set flip.count %i ***\n", tty->flip.count);
2606 DEBUG_LOG(info->line, "*** recv_cnt %i\n", info->recv_cnt);
2607 } else {
2608 }
2609 DEBUG_LOG(info->line, "*** rxtot %i\n", info->icount.rx);
2610 DEBUG_LOG(info->line, "ldisc %lu\n", tty->ldisc.chars_in_buffer(tty));
2611 DEBUG_LOG(info->line, "room %lu\n", tty->ldisc.receive_room(tty));
2612 }
2613
2614 );
2615
2616 /* this includes a check for low-latency */
2617 tty_flip_buffer_push(tty);
2618}
2619
2620static _INLINE_ void
2621check_flush_timeout(struct e100_serial *info)
2622{
2623 /* Flip what we've got (if we can) */
2624 flush_to_flip_buffer(info);
2625
2626 /* We might need to flip later, but not to fast
2627 * since the system is busy processing input... */
2628 if (info->first_recv_buffer)
2629 START_FLUSH_FAST_TIMER_TIME(info, "flip", 2000);
2630
2631 /* Force eop last, since data might have come while we're processing
2632 * and if we started the slow timer above, we won't start a fast
2633 * below.
2634 */
2635 force_eop_if_needed(info);
2636}
2637
2638#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
2639static void flush_timeout_function(unsigned long data)
2640{
2641 struct e100_serial *info = (struct e100_serial *)data;
2642
2643 fast_timers[info->line].function = NULL;
2644 serial_fast_timer_expired++;
2645 TIMERD(DEBUG_LOG(info->line, "flush_timout %i ", info->line));
2646 TIMERD(DEBUG_LOG(info->line, "num expired: %i\n", serial_fast_timer_expired));
2647 check_flush_timeout(info);
2648}
2649
2650#else
2651
2652/* dma fifo/buffer timeout handler
2653 forces an end-of-packet for the dma input channel if no chars
2654 have been received for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS/100 s.
2655*/
2656
2657static struct timer_list flush_timer;
2658
2659static void
2660timed_flush_handler(unsigned long ptr)
2661{
2662 struct e100_serial *info;
2663 int i;
2664
2665#ifdef CONFIG_SVINTO_SIM
2666 return;
2667#endif
2668
2669 for (i = 0; i < NR_PORTS; i++) {
2670 info = rs_table + i;
2671 if (info->uses_dma_in)
2672 check_flush_timeout(info);
2673 }
2674
2675 /* restart flush timer */
2676 mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
2677}
2678#endif
2679
2680#ifdef SERIAL_HANDLE_EARLY_ERRORS
2681
2682/* If there is an error (ie break) when the DMA is running and
2683 * there are no bytes in the fifo the DMA is stopped and we get no
2684 * eop interrupt. Thus we have to monitor the first bytes on a DMA
2685 * transfer, and if it is without error we can turn the serial
2686 * interrupts off.
2687 */
2688
2689/*
2690BREAK handling on ETRAX 100:
2691ETRAX will generate interrupt although there is no stop bit between the
2692characters.
2693
2694Depending on how long the break sequence is, the end of the breaksequence
2695will look differently:
2696| indicates start/end of a character.
2697
2698B= Break character (0x00) with framing error.
2699E= Error byte with parity error received after B characters.
2700F= "Faked" valid byte received immediately after B characters.
2701V= Valid byte
2702
27031.
2704 B BL ___________________________ V
2705.._|__________|__________| |valid data |
2706
2707Multiple frame errors with data == 0x00 (B),
2708the timing matches up "perfectly" so no extra ending char is detected.
2709The RXD pin is 1 in the last interrupt, in that case
2710we set info->errorcode = ERRCODE_INSERT_BREAK, but we can't really
2711know if another byte will come and this really is case 2. below
2712(e.g F=0xFF or 0xFE)
2713If RXD pin is 0 we can expect another character (see 2. below).
2714
2715
27162.
2717
2718 B B E or F__________________..__ V
2719.._|__________|__________|______ | |valid data
2720 "valid" or
2721 parity error
2722
2723Multiple frame errors with data == 0x00 (B),
2724but the part of the break trigs is interpreted as a start bit (and possibly
2725some 0 bits followed by a number of 1 bits and a stop bit).
2726Depending on parity settings etc. this last character can be either
2727a fake "valid" char (F) or have a parity error (E).
2728
2729If the character is valid it will be put in the buffer,
2730we set info->errorcode = ERRCODE_SET_BREAK so the receive interrupt
2731will set the flags so the tty will handle it,
2732if it's an error byte it will not be put in the buffer
2733and we set info->errorcode = ERRCODE_INSERT_BREAK.
2734
2735To distinguish a V byte in 1. from an F byte in 2. we keep a timestamp
2736of the last faulty char (B) and compares it with the current time:
2737If the time elapsed time is less then 2*char_time_usec we will assume
2738it's a faked F char and not a Valid char and set
2739info->errorcode = ERRCODE_SET_BREAK.
2740
2741Flaws in the above solution:
2742~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2743We use the timer to distinguish a F character from a V character,
2744if a V character is to close after the break we might make the wrong decision.
2745
2746TODO: The break will be delayed until an F or V character is received.
2747
2748*/
2749
2750extern _INLINE_
2751struct e100_serial * handle_ser_rx_interrupt_no_dma(struct e100_serial *info)
2752{
2753 unsigned long data_read;
2754 struct tty_struct *tty = info->tty;
2755
2756 if (!tty) {
2757 printk("!NO TTY!\n");
2758 return info;
2759 }
2760 if (tty->flip.count >= TTY_FLIPBUF_SIZE - TTY_THRESHOLD_THROTTLE) {
2761 /* check TTY_THROTTLED first so it indicates our state */
2762 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags)) {
2763 DFLOW(DEBUG_LOG(info->line, "rs_throttle flip.count: %i\n", tty->flip.count));
2764 rs_throttle(tty);
2765 }
2766 }
2767 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
2768 DEBUG_LOG(info->line, "force FLIP! %i\n", tty->flip.count);
2769 tty->flip.work.func((void *) tty);
2770 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
2771 DEBUG_LOG(info->line, "FLIP FULL! %i\n", tty->flip.count);
2772 return info; /* if TTY_DONT_FLIP is set */
2773 }
2774 }
2775 /* Read data and status at the same time */
2776 data_read = *((unsigned long *)&info->port[REG_DATA_STATUS32]);
2777more_data:
2778 if (data_read & IO_MASK(R_SERIAL0_READ, xoff_detect) ) {
2779 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2780 }
2781 DINTR2(DEBUG_LOG(info->line, "ser_rx %c\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read)));
2782
2783 if (data_read & ( IO_MASK(R_SERIAL0_READ, framing_err) |
2784 IO_MASK(R_SERIAL0_READ, par_err) |
2785 IO_MASK(R_SERIAL0_READ, overrun) )) {
2786 /* An error */
2787 info->last_rx_active_usec = GET_JIFFIES_USEC();
2788 info->last_rx_active = jiffies;
2789 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat_data %04X\n", data_read));
2790 DLOG_INT_TRIG(
2791 if (!log_int_trig1_pos) {
2792 log_int_trig1_pos = log_int_pos;
2793 log_int(rdpc(), 0, 0);
2794 }
2795 );
2796
2797
2798 if ( ((data_read & IO_MASK(R_SERIAL0_READ, data_in)) == 0) &&
2799 (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) ) {
2800 /* Most likely a break, but we get interrupts over and
2801 * over again.
2802 */
2803
2804 if (!info->break_detected_cnt) {
2805 DEBUG_LOG(info->line, "#BRK start\n", 0);
2806 }
2807 if (data_read & IO_MASK(R_SERIAL0_READ, rxd)) {
2808 /* The RX pin is high now, so the break
2809 * must be over, but....
2810 * we can't really know if we will get another
2811 * last byte ending the break or not.
2812 * And we don't know if the byte (if any) will
2813 * have an error or look valid.
2814 */
2815 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2816 info->errorcode = ERRCODE_INSERT_BREAK;
2817 }
2818 info->break_detected_cnt++;
2819 } else {
2820 /* The error does not look like a break, but could be
2821 * the end of one
2822 */
2823 if (info->break_detected_cnt) {
2824 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2825 info->errorcode = ERRCODE_INSERT_BREAK;
2826 } else {
2827 if (info->errorcode == ERRCODE_INSERT_BREAK) {
2828 info->icount.brk++;
2829 *tty->flip.char_buf_ptr = 0;
2830 *tty->flip.flag_buf_ptr = TTY_BREAK;
2831 tty->flip.flag_buf_ptr++;
2832 tty->flip.char_buf_ptr++;
2833 tty->flip.count++;
2834 info->icount.rx++;
2835 }
2836 *tty->flip.char_buf_ptr = IO_EXTRACT(R_SERIAL0_READ, data_in, data_read);
2837
2838 if (data_read & IO_MASK(R_SERIAL0_READ, par_err)) {
2839 info->icount.parity++;
2840 *tty->flip.flag_buf_ptr = TTY_PARITY;
2841 } else if (data_read & IO_MASK(R_SERIAL0_READ, overrun)) {
2842 info->icount.overrun++;
2843 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
2844 } else if (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) {
2845 info->icount.frame++;
2846 *tty->flip.flag_buf_ptr = TTY_FRAME;
2847 }
2848 info->errorcode = 0;
2849 }
2850 info->break_detected_cnt = 0;
2851 }
2852 } else if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2853 /* No error */
2854 DLOG_INT_TRIG(
2855 if (!log_int_trig1_pos) {
2856 if (log_int_pos >= log_int_size) {
2857 log_int_pos = 0;
2858 }
2859 log_int_trig0_pos = log_int_pos;
2860 log_int(rdpc(), 0, 0);
2861 }
2862 );
2863 *tty->flip.char_buf_ptr = IO_EXTRACT(R_SERIAL0_READ, data_in, data_read);
2864 *tty->flip.flag_buf_ptr = 0;
2865 } else {
2866 DEBUG_LOG(info->line, "ser_rx int but no data_avail %08lX\n", data_read);
2867 }
2868
2869
2870 tty->flip.flag_buf_ptr++;
2871 tty->flip.char_buf_ptr++;
2872 tty->flip.count++;
2873 info->icount.rx++;
2874 data_read = *((unsigned long *)&info->port[REG_DATA_STATUS32]);
2875 if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2876 DEBUG_LOG(info->line, "ser_rx %c in loop\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read));
2877 goto more_data;
2878 }
2879
2880 tty_flip_buffer_push(info->tty);
2881 return info;
2882}
2883
2884extern _INLINE_
2885struct e100_serial* handle_ser_rx_interrupt(struct e100_serial *info)
2886{
2887 unsigned char rstat;
2888
2889#ifdef SERIAL_DEBUG_INTR
2890 printk("Interrupt from serport %d\n", i);
2891#endif
2892/* DEBUG_LOG(info->line, "ser_interrupt stat %03X\n", rstat | (i << 8)); */
2893 if (!info->uses_dma_in) {
2894 return handle_ser_rx_interrupt_no_dma(info);
2895 }
2896 /* DMA is used */
2897 rstat = info->port[REG_STATUS];
2898 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
2899 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2900 }
2901
2902 if (rstat & SER_ERROR_MASK) {
2903 unsigned char data;
2904
2905 info->last_rx_active_usec = GET_JIFFIES_USEC();
2906 info->last_rx_active = jiffies;
2907 /* If we got an error, we must reset it by reading the
2908 * data_in field
2909 */
2910 data = info->port[REG_DATA];
2911 DINTR1(DEBUG_LOG(info->line, "ser_rx! %c\n", data));
2912 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat %02X\n", rstat));
2913 if (!data && (rstat & SER_FRAMING_ERR_MASK)) {
2914 /* Most likely a break, but we get interrupts over and
2915 * over again.
2916 */
2917
2918 if (!info->break_detected_cnt) {
2919 DEBUG_LOG(info->line, "#BRK start\n", 0);
2920 }
2921 if (rstat & SER_RXD_MASK) {
2922 /* The RX pin is high now, so the break
2923 * must be over, but....
2924 * we can't really know if we will get another
2925 * last byte ending the break or not.
2926 * And we don't know if the byte (if any) will
2927 * have an error or look valid.
2928 */
2929 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2930 info->errorcode = ERRCODE_INSERT_BREAK;
2931 }
2932 info->break_detected_cnt++;
2933 } else {
2934 /* The error does not look like a break, but could be
2935 * the end of one
2936 */
2937 if (info->break_detected_cnt) {
2938 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2939 info->errorcode = ERRCODE_INSERT_BREAK;
2940 } else {
2941 if (info->errorcode == ERRCODE_INSERT_BREAK) {
2942 info->icount.brk++;
2943 add_char_and_flag(info, '\0', TTY_BREAK);
2944 }
2945
2946 if (rstat & SER_PAR_ERR_MASK) {
2947 info->icount.parity++;
2948 add_char_and_flag(info, data, TTY_PARITY);
2949 } else if (rstat & SER_OVERRUN_MASK) {
2950 info->icount.overrun++;
2951 add_char_and_flag(info, data, TTY_OVERRUN);
2952 } else if (rstat & SER_FRAMING_ERR_MASK) {
2953 info->icount.frame++;
2954 add_char_and_flag(info, data, TTY_FRAME);
2955 }
2956
2957 info->errorcode = 0;
2958 }
2959 info->break_detected_cnt = 0;
2960 DEBUG_LOG(info->line, "#iERR s d %04X\n",
2961 ((rstat & SER_ERROR_MASK) << 8) | data);
2962 }
2963 PROCSTAT(ser_stat[info->line].early_errors_cnt++);
2964 } else { /* It was a valid byte, now let the DMA do the rest */
2965 unsigned long curr_time_u = GET_JIFFIES_USEC();
2966 unsigned long curr_time = jiffies;
2967
2968 if (info->break_detected_cnt) {
2969 /* Detect if this character is a new valid char or the
2970 * last char in a break sequence: If LSBits are 0 and
2971 * MSBits are high AND the time is close to the
2972 * previous interrupt we should discard it.
2973 */
2974 long elapsed_usec =
2975 (curr_time - info->last_rx_active) * (1000000/HZ) +
2976 curr_time_u - info->last_rx_active_usec;
2977 if (elapsed_usec < 2*info->char_time_usec) {
2978 DEBUG_LOG(info->line, "FBRK %i\n", info->line);
2979 /* Report as BREAK (error) and let
2980 * receive_chars_dma() handle it
2981 */
2982 info->errorcode = ERRCODE_SET_BREAK;
2983 } else {
2984 DEBUG_LOG(info->line, "Not end of BRK (V)%i\n", info->line);
2985 }
2986 DEBUG_LOG(info->line, "num brk %i\n", info->break_detected_cnt);
2987 }
2988
2989#ifdef SERIAL_DEBUG_INTR
2990 printk("** OK, disabling ser_interrupts\n");
2991#endif
2992 e100_disable_serial_data_irq(info);
2993 DINTR2(DEBUG_LOG(info->line, "ser_rx OK %d\n", info->line));
2994 info->break_detected_cnt = 0;
2995
2996 PROCSTAT(ser_stat[info->line].ser_ints_ok_cnt++);
2997 }
2998 /* Restarting the DMA never hurts */
2999 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
3000 START_FLUSH_FAST_TIMER(info, "ser_int");
3001 return info;
3002} /* handle_ser_rx_interrupt */
3003
3004extern _INLINE_ void handle_ser_tx_interrupt(struct e100_serial *info)
3005{
3006 unsigned long flags;
3007
3008 if (info->x_char) {
3009 unsigned char rstat;
3010 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar 0x%02X\n", info->x_char));
3011 save_flags(flags); cli();
3012 rstat = info->port[REG_STATUS];
3013 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
3014
3015 info->port[REG_TR_DATA] = info->x_char;
3016 info->icount.tx++;
3017 info->x_char = 0;
3018 /* We must enable since it is disabled in ser_interrupt */
3019 e100_enable_serial_tx_ready_irq(info);
3020 restore_flags(flags);
3021 return;
3022 }
3023 if (info->uses_dma_out) {
3024 unsigned char rstat;
3025 int i;
3026 /* We only use normal tx interrupt when sending x_char */
3027 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar sent\n", 0));
3028 save_flags(flags); cli();
3029 rstat = info->port[REG_STATUS];
3030 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
3031 e100_disable_serial_tx_ready_irq(info);
3032 if (info->tty->stopped)
3033 rs_stop(info->tty);
3034 /* Enable the DMA channel and tell it to continue */
3035 e100_enable_txdma_channel(info);
3036 /* Wait 12 cycles before doing the DMA command */
3037 for(i = 6; i > 0; i--)
3038 nop();
3039
3040 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, continue);
3041 restore_flags(flags);
3042 return;
3043 }
3044 /* Normal char-by-char interrupt */
3045 if (info->xmit.head == info->xmit.tail
3046 || info->tty->stopped
3047 || info->tty->hw_stopped) {
3048 DFLOW(DEBUG_LOG(info->line, "tx_int: stopped %i\n", info->tty->stopped));
3049 e100_disable_serial_tx_ready_irq(info);
3050 info->tr_running = 0;
3051 return;
3052 }
3053 DINTR2(DEBUG_LOG(info->line, "tx_int %c\n", info->xmit.buf[info->xmit.tail]));
3054 /* Send a byte, rs485 timing is critical so turn of ints */
3055 save_flags(flags); cli();
3056 info->port[REG_TR_DATA] = info->xmit.buf[info->xmit.tail];
3057 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
3058 info->icount.tx++;
3059 if (info->xmit.head == info->xmit.tail) {
3060#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
3061 if (info->rs485.enabled) {
3062 /* Set a short timer to toggle RTS */
3063 start_one_shot_timer(&fast_timers_rs485[info->line],
3064 rs485_toggle_rts_timer_function,
3065 (unsigned long)info,
3066 info->char_time_usec*2,
3067 "RS-485");
3068 }
3069#endif /* RS485 */
3070 info->last_tx_active_usec = GET_JIFFIES_USEC();
3071 info->last_tx_active = jiffies;
3072 e100_disable_serial_tx_ready_irq(info);
3073 info->tr_running = 0;
3074 DFLOW(DEBUG_LOG(info->line, "tx_int: stop2\n", 0));
3075 } else {
3076 /* We must enable since it is disabled in ser_interrupt */
3077 e100_enable_serial_tx_ready_irq(info);
3078 }
3079 restore_flags(flags);
3080
3081 if (CIRC_CNT(info->xmit.head,
3082 info->xmit.tail,
3083 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
3084 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
3085
3086} /* handle_ser_tx_interrupt */
3087
3088/* result of time measurements:
3089 * RX duration 54-60 us when doing something, otherwise 6-9 us
3090 * ser_int duration: just sending: 8-15 us normally, up to 73 us
3091 */
3092static irqreturn_t
3093ser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3094{
3095 static volatile int tx_started = 0;
3096 struct e100_serial *info;
3097 int i;
3098 unsigned long flags;
3099 unsigned long irq_mask1_rd;
3100 unsigned long data_mask = (1 << (8+2*0)); /* ser0 data_avail */
3101 int handled = 0;
3102 static volatile unsigned long reentered_ready_mask = 0;
3103
3104 save_flags(flags); cli();
3105 irq_mask1_rd = *R_IRQ_MASK1_RD;
3106 /* First handle all rx interrupts with ints disabled */
3107 info = rs_table;
3108 irq_mask1_rd &= e100_ser_int_mask;
3109 for (i = 0; i < NR_PORTS; i++) {
3110 /* Which line caused the data irq? */
3111 if (irq_mask1_rd & data_mask) {
3112 handled = 1;
3113 handle_ser_rx_interrupt(info);
3114 }
3115 info += 1;
3116 data_mask <<= 2;
3117 }
3118 /* Handle tx interrupts with interrupts enabled so we
3119 * can take care of new data interrupts while transmitting
3120 * We protect the tx part with the tx_started flag.
3121 * We disable the tr_ready interrupts we are about to handle and
3122 * unblock the serial interrupt so new serial interrupts may come.
3123 *
3124 * If we get a new interrupt:
3125 * - it migth be due to synchronous serial ports.
3126 * - serial irq will be blocked by general irq handler.
3127 * - async data will be handled above (sync will be ignored).
3128 * - tx_started flag will prevent us from trying to send again and
3129 * we will exit fast - no need to unblock serial irq.
3130 * - Next (sync) serial interrupt handler will be runned with
3131 * disabled interrupt due to restore_flags() at end of function,
3132 * so sync handler will not be preempted or reentered.
3133 */
3134 if (!tx_started) {
3135 unsigned long ready_mask;
3136 unsigned long
3137 tx_started = 1;
3138 /* Only the tr_ready interrupts left */
3139 irq_mask1_rd &= (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
3140 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
3141 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
3142 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
3143 while (irq_mask1_rd) {
3144 /* Disable those we are about to handle */
3145 *R_IRQ_MASK1_CLR = irq_mask1_rd;
3146 /* Unblock the serial interrupt */
3147 *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
3148
3149 sti();
3150 ready_mask = (1 << (8+1+2*0)); /* ser0 tr_ready */
3151 info = rs_table;
3152 for (i = 0; i < NR_PORTS; i++) {
3153 /* Which line caused the ready irq? */
3154 if (irq_mask1_rd & ready_mask) {
3155 handled = 1;
3156 handle_ser_tx_interrupt(info);
3157 }
3158 info += 1;
3159 ready_mask <<= 2;
3160 }
3161 /* handle_ser_tx_interrupt enables tr_ready interrupts */
3162 cli();
3163 /* Handle reentered TX interrupt */
3164 irq_mask1_rd = reentered_ready_mask;
3165 }
3166 cli();
3167 tx_started = 0;
3168 } else {
3169 unsigned long ready_mask;
3170 ready_mask = irq_mask1_rd & (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
3171 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
3172 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
3173 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
3174 if (ready_mask) {
3175 reentered_ready_mask |= ready_mask;
3176 /* Disable those we are about to handle */
3177 *R_IRQ_MASK1_CLR = ready_mask;
3178 DFLOW(DEBUG_LOG(SERIAL_DEBUG_LINE, "ser_int reentered with TX %X\n", ready_mask));
3179 }
3180 }
3181
3182 restore_flags(flags);
3183 return IRQ_RETVAL(handled);
3184} /* ser_interrupt */
3185#endif
3186
3187/*
3188 * -------------------------------------------------------------------
3189 * Here ends the serial interrupt routines.
3190 * -------------------------------------------------------------------
3191 */
3192
3193/*
3194 * This routine is used to handle the "bottom half" processing for the
3195 * serial driver, known also the "software interrupt" processing.
3196 * This processing is done at the kernel interrupt level, after the
3197 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
3198 * is where time-consuming activities which can not be done in the
3199 * interrupt driver proper are done; the interrupt driver schedules
3200 * them using rs_sched_event(), and they get done here.
3201 */
3202static void
3203do_softint(void *private_)
3204{
3205 struct e100_serial *info = (struct e100_serial *) private_;
3206 struct tty_struct *tty;
3207
3208 tty = info->tty;
3209 if (!tty)
3210 return;
3211
3212 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
3213 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
3214 tty->ldisc.write_wakeup)
3215 (tty->ldisc.write_wakeup)(tty);
3216 wake_up_interruptible(&tty->write_wait);
3217 }
3218}
3219
3220static int
3221startup(struct e100_serial * info)
3222{
3223 unsigned long flags;
3224 unsigned long xmit_page;
3225 int i;
3226
3227 xmit_page = get_zeroed_page(GFP_KERNEL);
3228 if (!xmit_page)
3229 return -ENOMEM;
3230
3231 save_flags(flags);
3232 cli();
3233
3234 /* if it was already initialized, skip this */
3235
3236 if (info->flags & ASYNC_INITIALIZED) {
3237 restore_flags(flags);
3238 free_page(xmit_page);
3239 return 0;
3240 }
3241
3242 if (info->xmit.buf)
3243 free_page(xmit_page);
3244 else
3245 info->xmit.buf = (unsigned char *) xmit_page;
3246
3247#ifdef SERIAL_DEBUG_OPEN
3248 printk("starting up ttyS%d (xmit_buf 0x%p)...\n", info->line, info->xmit.buf);
3249#endif
3250
3251#ifdef CONFIG_SVINTO_SIM
3252 /* Bits and pieces collected from below. Better to have them
3253 in one ifdef:ed clause than to mix in a lot of ifdefs,
3254 right? */
3255 if (info->tty)
3256 clear_bit(TTY_IO_ERROR, &info->tty->flags);
3257
3258 info->xmit.head = info->xmit.tail = 0;
3259 info->first_recv_buffer = info->last_recv_buffer = NULL;
3260 info->recv_cnt = info->max_recv_cnt = 0;
3261
3262 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
3263 info->rec_descr[i].buf = NULL;
3264
3265 /* No real action in the simulator, but may set info important
3266 to ioctl. */
3267 change_speed(info);
3268#else
3269
3270 /*
3271 * Clear the FIFO buffers and disable them
3272 * (they will be reenabled in change_speed())
3273 */
3274
3275 /*
3276 * Reset the DMA channels and make sure their interrupts are cleared
3277 */
3278
3279 if (info->dma_in_enabled) {
3280 info->uses_dma_in = 1;
3281 e100_enable_rxdma_channel(info);
3282
3283 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
3284
3285 /* Wait until reset cycle is complete */
3286 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
3287 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
3288
3289 /* Make sure the irqs are cleared */
3290 *info->iclrintradr =
3291 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
3292 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
3293 } else {
3294 e100_disable_rxdma_channel(info);
3295 }
3296
3297 if (info->dma_out_enabled) {
3298 info->uses_dma_out = 1;
3299 e100_enable_txdma_channel(info);
3300 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
3301
3302 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) ==
3303 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
3304
3305 /* Make sure the irqs are cleared */
3306 *info->oclrintradr =
3307 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
3308 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
3309 } else {
3310 e100_disable_txdma_channel(info);
3311 }
3312
3313 if (info->tty)
3314 clear_bit(TTY_IO_ERROR, &info->tty->flags);
3315
3316 info->xmit.head = info->xmit.tail = 0;
3317 info->first_recv_buffer = info->last_recv_buffer = NULL;
3318 info->recv_cnt = info->max_recv_cnt = 0;
3319
3320 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
3321 info->rec_descr[i].buf = 0;
3322
3323 /*
3324 * and set the speed and other flags of the serial port
3325 * this will start the rx/tx as well
3326 */
3327#ifdef SERIAL_HANDLE_EARLY_ERRORS
3328 e100_enable_serial_data_irq(info);
3329#endif
3330 change_speed(info);
3331
3332 /* dummy read to reset any serial errors */
3333
3334 (void)info->port[REG_DATA];
3335
3336 /* enable the interrupts */
3337 if (info->uses_dma_out)
3338 e100_enable_txdma_irq(info);
3339
3340 e100_enable_rx_irq(info);
3341
3342 info->tr_running = 0; /* to be sure we don't lock up the transmitter */
3343
3344 /* setup the dma input descriptor and start dma */
3345
3346 start_receive(info);
3347
3348 /* for safety, make sure the descriptors last result is 0 bytes written */
3349
3350 info->tr_descr.sw_len = 0;
3351 info->tr_descr.hw_len = 0;
3352 info->tr_descr.status = 0;
3353
3354 /* enable RTS/DTR last */
3355
3356 e100_rts(info, 1);
3357 e100_dtr(info, 1);
3358
3359#endif /* CONFIG_SVINTO_SIM */
3360
3361 info->flags |= ASYNC_INITIALIZED;
3362
3363 restore_flags(flags);
3364 return 0;
3365}
3366
3367/*
3368 * This routine will shutdown a serial port; interrupts are disabled, and
3369 * DTR is dropped if the hangup on close termio flag is on.
3370 */
3371static void
3372shutdown(struct e100_serial * info)
3373{
3374 unsigned long flags;
3375 struct etrax_dma_descr *descr = info->rec_descr;
3376 struct etrax_recv_buffer *buffer;
3377 int i;
3378
3379#ifndef CONFIG_SVINTO_SIM
3380 /* shut down the transmitter and receiver */
3381 DFLOW(DEBUG_LOG(info->line, "shutdown %i\n", info->line));
3382 e100_disable_rx(info);
3383 info->port[REG_TR_CTRL] = (info->tx_ctrl &= ~0x40);
3384
3385 /* disable interrupts, reset dma channels */
3386 if (info->uses_dma_in) {
3387 e100_disable_rxdma_irq(info);
3388 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
3389 info->uses_dma_in = 0;
3390 } else {
3391 e100_disable_serial_data_irq(info);
3392 }
3393
3394 if (info->uses_dma_out) {
3395 e100_disable_txdma_irq(info);
3396 info->tr_running = 0;
3397 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
3398 info->uses_dma_out = 0;
3399 } else {
3400 e100_disable_serial_tx_ready_irq(info);
3401 info->tr_running = 0;
3402 }
3403
3404#endif /* CONFIG_SVINTO_SIM */
3405
3406 if (!(info->flags & ASYNC_INITIALIZED))
3407 return;
3408
3409#ifdef SERIAL_DEBUG_OPEN
3410 printk("Shutting down serial port %d (irq %d)....\n", info->line,
3411 info->irq);
3412#endif
3413
3414 save_flags(flags);
3415 cli(); /* Disable interrupts */
3416
3417 if (info->xmit.buf) {
3418 free_page((unsigned long)info->xmit.buf);
3419 info->xmit.buf = NULL;
3420 }
3421
3422 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
3423 if (descr[i].buf) {
3424 buffer = phys_to_virt(descr[i].buf) - sizeof *buffer;
3425 kfree(buffer);
3426 descr[i].buf = 0;
3427 }
3428
3429 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
3430 /* hang up DTR and RTS if HUPCL is enabled */
3431 e100_dtr(info, 0);
3432 e100_rts(info, 0); /* could check CRTSCTS before doing this */
3433 }
3434
3435 if (info->tty)
3436 set_bit(TTY_IO_ERROR, &info->tty->flags);
3437
3438 info->flags &= ~ASYNC_INITIALIZED;
3439 restore_flags(flags);
3440}
3441
3442
3443/* change baud rate and other assorted parameters */
3444
3445static void
3446change_speed(struct e100_serial *info)
3447{
3448 unsigned int cflag;
3449 unsigned long xoff;
3450 unsigned long flags;
3451 /* first some safety checks */
3452
3453 if (!info->tty || !info->tty->termios)
3454 return;
3455 if (!info->port)
3456 return;
3457
3458 cflag = info->tty->termios->c_cflag;
3459
3460 /* possibly, the tx/rx should be disabled first to do this safely */
3461
3462 /* change baud-rate and write it to the hardware */
3463 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) {
3464 /* Special baudrate */
3465 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
3466 unsigned long alt_source =
3467 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
3468 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
3469 /* R_ALT_SER_BAUDRATE selects the source */
3470 DBAUD(printk("Custom baudrate: baud_base/divisor %lu/%i\n",
3471 (unsigned long)info->baud_base, info->custom_divisor));
3472 if (info->baud_base == SERIAL_PRESCALE_BASE) {
3473 /* 0, 2-65535 (0=65536) */
3474 u16 divisor = info->custom_divisor;
3475 /* R_SERIAL_PRESCALE (upper 16 bits of R_CLOCK_PRESCALE) */
3476 /* baudrate is 3.125MHz/custom_divisor */
3477 alt_source =
3478 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, prescale) |
3479 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, prescale);
3480 alt_source = 0x11;
3481 DBAUD(printk("Writing SERIAL_PRESCALE: divisor %i\n", divisor));
3482 *R_SERIAL_PRESCALE = divisor;
3483 info->baud = SERIAL_PRESCALE_BASE/divisor;
3484 }
3485#ifdef CONFIG_ETRAX_EXTERN_PB6CLK_ENABLED
3486 else if ((info->baud_base==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8 &&
3487 info->custom_divisor == 1) ||
3488 (info->baud_base==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ &&
3489 info->custom_divisor == 8)) {
3490 /* ext_clk selected */
3491 alt_source =
3492 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, extern) |
3493 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, extern);
3494 DBAUD(printk("using external baudrate: %lu\n", CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8));
3495 info->baud = CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8;
3496 }
3497 }
3498#endif
3499 else
3500 {
3501 /* Bad baudbase, we don't support using timer0
3502 * for baudrate.
3503 */
3504 printk(KERN_WARNING "Bad baud_base/custom_divisor: %lu/%i\n",
3505 (unsigned long)info->baud_base, info->custom_divisor);
3506 }
3507 r_alt_ser_baudrate_shadow &= ~mask;
3508 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
3509 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
3510 } else {
3511 /* Normal baudrate */
3512 /* Make sure we use normal baudrate */
3513 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
3514 unsigned long alt_source =
3515 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
3516 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
3517 r_alt_ser_baudrate_shadow &= ~mask;
3518 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
3519#ifndef CONFIG_SVINTO_SIM
3520 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
3521#endif /* CONFIG_SVINTO_SIM */
3522
3523 info->baud = cflag_to_baud(cflag);
3524#ifndef CONFIG_SVINTO_SIM
3525 info->port[REG_BAUD] = cflag_to_etrax_baud(cflag);
3526#endif /* CONFIG_SVINTO_SIM */
3527 }
3528
3529#ifndef CONFIG_SVINTO_SIM
3530 /* start with default settings and then fill in changes */
3531 save_flags(flags);
3532 cli();
3533 /* 8 bit, no/even parity */
3534 info->rx_ctrl &= ~(IO_MASK(R_SERIAL0_REC_CTRL, rec_bitnr) |
3535 IO_MASK(R_SERIAL0_REC_CTRL, rec_par_en) |
3536 IO_MASK(R_SERIAL0_REC_CTRL, rec_par));
3537
3538 /* 8 bit, no/even parity, 1 stop bit, no cts */
3539 info->tx_ctrl &= ~(IO_MASK(R_SERIAL0_TR_CTRL, tr_bitnr) |
3540 IO_MASK(R_SERIAL0_TR_CTRL, tr_par_en) |
3541 IO_MASK(R_SERIAL0_TR_CTRL, tr_par) |
3542 IO_MASK(R_SERIAL0_TR_CTRL, stop_bits) |
3543 IO_MASK(R_SERIAL0_TR_CTRL, auto_cts));
3544
3545 if ((cflag & CSIZE) == CS7) {
3546 /* set 7 bit mode */
3547 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
3548 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
3549 }
3550
3551 if (cflag & CSTOPB) {
3552 /* set 2 stop bit mode */
3553 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, two_bits);
3554 }
3555
3556 if (cflag & PARENB) {
3557 /* enable parity */
3558 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
3559 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
3560 }
3561
3562 if (cflag & CMSPAR) {
3563 /* enable stick parity, PARODD mean Mark which matches ETRAX */
3564 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, stick);
3565 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, stick);
3566 }
3567 if (cflag & PARODD) {
3568 /* set odd parity (or Mark if CMSPAR) */
3569 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
3570 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
3571 }
3572
3573 if (cflag & CRTSCTS) {
3574 /* enable automatic CTS handling */
3575 DFLOW(DEBUG_LOG(info->line, "FLOW auto_cts enabled\n", 0));
3576 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, active);
3577 }
3578
3579 /* make sure the tx and rx are enabled */
3580
3581 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable);
3582 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
3583
3584 /* actually write the control regs to the hardware */
3585
3586 info->port[REG_TR_CTRL] = info->tx_ctrl;
3587 info->port[REG_REC_CTRL] = info->rx_ctrl;
3588 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->tty));
3589 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
3590 if (info->tty->termios->c_iflag & IXON ) {
3591 DFLOW(DEBUG_LOG(info->line, "FLOW XOFF enabled 0x%02X\n", STOP_CHAR(info->tty)));
3592 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
3593 }
3594
3595 *((unsigned long *)&info->port[REG_XOFF]) = xoff;
3596 restore_flags(flags);
3597#endif /* !CONFIG_SVINTO_SIM */
3598
3599 update_char_time(info);
3600
3601} /* change_speed */
3602
3603/* start transmitting chars NOW */
3604
3605static void
3606rs_flush_chars(struct tty_struct *tty)
3607{
3608 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3609 unsigned long flags;
3610
3611 if (info->tr_running ||
3612 info->xmit.head == info->xmit.tail ||
3613 tty->stopped ||
3614 tty->hw_stopped ||
3615 !info->xmit.buf)
3616 return;
3617
3618#ifdef SERIAL_DEBUG_FLOW
3619 printk("rs_flush_chars\n");
3620#endif
3621
3622 /* this protection might not exactly be necessary here */
3623
3624 save_flags(flags);
3625 cli();
3626 start_transmit(info);
3627 restore_flags(flags);
3628}
3629
3630extern _INLINE_ int
3631rs_raw_write(struct tty_struct * tty, int from_user,
3632 const unsigned char *buf, int count)
3633{
3634 int c, ret = 0;
3635 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3636 unsigned long flags;
3637
3638 /* first some sanity checks */
3639
3640 if (!tty || !info->xmit.buf || !tmp_buf)
3641 return 0;
3642
3643#ifdef SERIAL_DEBUG_DATA
3644 if (info->line == SERIAL_DEBUG_LINE)
3645 printk("rs_raw_write (%d), status %d\n",
3646 count, info->port[REG_STATUS]);
3647#endif
3648
3649#ifdef CONFIG_SVINTO_SIM
3650 /* Really simple. The output is here and now. */
3651 SIMCOUT(buf, count);
3652 return count;
3653#endif
3654 save_flags(flags);
3655 DFLOW(DEBUG_LOG(info->line, "write count %i ", count));
3656 DFLOW(DEBUG_LOG(info->line, "ldisc %i\n", tty->ldisc.chars_in_buffer(tty)));
3657
3658
3659 /* the cli/restore_flags pairs below are needed because the
3660 * DMA interrupt handler moves the info->xmit values. the memcpy
3661 * needs to be in the critical region unfortunately, because we
3662 * need to read xmit values, memcpy, write xmit values in one
3663 * atomic operation... this could perhaps be avoided by more clever
3664 * design.
3665 */
3666 if (from_user) {
3667 down(&tmp_buf_sem);
3668 while (1) {
3669 int c1;
3670 c = CIRC_SPACE_TO_END(info->xmit.head,
3671 info->xmit.tail,
3672 SERIAL_XMIT_SIZE);
3673 if (count < c)
3674 c = count;
3675 if (c <= 0)
3676 break;
3677
3678 c -= copy_from_user(tmp_buf, buf, c);
3679 if (!c) {
3680 if (!ret)
3681 ret = -EFAULT;
3682 break;
3683 }
3684 cli();
3685 c1 = CIRC_SPACE_TO_END(info->xmit.head,
3686 info->xmit.tail,
3687 SERIAL_XMIT_SIZE);
3688 if (c1 < c)
3689 c = c1;
3690 memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
3691 info->xmit.head = ((info->xmit.head + c) &
3692 (SERIAL_XMIT_SIZE-1));
3693 restore_flags(flags);
3694 buf += c;
3695 count -= c;
3696 ret += c;
3697 }
3698 up(&tmp_buf_sem);
3699 } else {
3700 cli();
3701 while (count) {
3702 c = CIRC_SPACE_TO_END(info->xmit.head,
3703 info->xmit.tail,
3704 SERIAL_XMIT_SIZE);
3705
3706 if (count < c)
3707 c = count;
3708 if (c <= 0)
3709 break;
3710
3711 memcpy(info->xmit.buf + info->xmit.head, buf, c);
3712 info->xmit.head = (info->xmit.head + c) &
3713 (SERIAL_XMIT_SIZE-1);
3714 buf += c;
3715 count -= c;
3716 ret += c;
3717 }
3718 restore_flags(flags);
3719 }
3720
3721 /* enable transmitter if not running, unless the tty is stopped
3722 * this does not need IRQ protection since if tr_running == 0
3723 * the IRQ's are not running anyway for this port.
3724 */
3725 DFLOW(DEBUG_LOG(info->line, "write ret %i\n", ret));
3726
3727 if (info->xmit.head != info->xmit.tail &&
3728 !tty->stopped &&
3729 !tty->hw_stopped &&
3730 !info->tr_running) {
3731 start_transmit(info);
3732 }
3733
3734 return ret;
3735} /* raw_raw_write() */
3736
3737static int
3738rs_write(struct tty_struct * tty, int from_user,
3739 const unsigned char *buf, int count)
3740{
3741#if defined(CONFIG_ETRAX_RS485)
3742 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3743
3744 if (info->rs485.enabled)
3745 {
3746 /* If we are in RS-485 mode, we need to toggle RTS and disable
3747 * the receiver before initiating a DMA transfer
3748 */
3749#ifdef CONFIG_ETRAX_FAST_TIMER
3750 /* Abort any started timer */
3751 fast_timers_rs485[info->line].function = NULL;
3752 del_fast_timer(&fast_timers_rs485[info->line]);
3753#endif
3754 e100_rts(info, info->rs485.rts_on_send);
3755#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3756 e100_disable_rx(info);
3757 e100_enable_rx_irq(info);
3758#endif
3759
3760 if (info->rs485.delay_rts_before_send > 0)
3761 msleep(info->rs485.delay_rts_before_send);
3762 }
3763#endif /* CONFIG_ETRAX_RS485 */
3764
3765 count = rs_raw_write(tty, from_user, buf, count);
3766
3767#if defined(CONFIG_ETRAX_RS485)
3768 if (info->rs485.enabled)
3769 {
3770 unsigned int val;
3771 /* If we are in RS-485 mode the following has to be done:
3772 * wait until DMA is ready
3773 * wait on transmit shift register
3774 * toggle RTS
3775 * enable the receiver
3776 */
3777
3778 /* Sleep until all sent */
3779 tty_wait_until_sent(tty, 0);
3780#ifdef CONFIG_ETRAX_FAST_TIMER
3781 /* Now sleep a little more so that shift register is empty */
3782 schedule_usleep(info->char_time_usec * 2);
3783#endif
3784 /* wait on transmit shift register */
3785 do{
3786 get_lsr_info(info, &val);
3787 }while (!(val & TIOCSER_TEMT));
3788
3789 e100_rts(info, info->rs485.rts_after_sent);
3790
3791#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3792 e100_enable_rx(info);
3793 e100_enable_rxdma_irq(info);
3794#endif
3795 }
3796#endif /* CONFIG_ETRAX_RS485 */
3797
3798 return count;
3799} /* rs_write */
3800
3801
3802/* how much space is available in the xmit buffer? */
3803
3804static int
3805rs_write_room(struct tty_struct *tty)
3806{
3807 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3808
3809 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3810}
3811
3812/* How many chars are in the xmit buffer?
3813 * This does not include any chars in the transmitter FIFO.
3814 * Use wait_until_sent for waiting for FIFO drain.
3815 */
3816
3817static int
3818rs_chars_in_buffer(struct tty_struct *tty)
3819{
3820 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3821
3822 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3823}
3824
3825/* discard everything in the xmit buffer */
3826
3827static void
3828rs_flush_buffer(struct tty_struct *tty)
3829{
3830 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3831 unsigned long flags;
3832
3833 save_flags(flags);
3834 cli();
3835 info->xmit.head = info->xmit.tail = 0;
3836 restore_flags(flags);
3837
3838 wake_up_interruptible(&tty->write_wait);
3839
3840 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
3841 tty->ldisc.write_wakeup)
3842 (tty->ldisc.write_wakeup)(tty);
3843}
3844
3845/*
3846 * This function is used to send a high-priority XON/XOFF character to
3847 * the device
3848 *
3849 * Since we use DMA we don't check for info->x_char in transmit_chars_dma(),
3850 * but we do it in handle_ser_tx_interrupt().
3851 * We disable DMA channel and enable tx ready interrupt and write the
3852 * character when possible.
3853 */
3854static void rs_send_xchar(struct tty_struct *tty, char ch)
3855{
3856 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3857 unsigned long flags;
3858 save_flags(flags); cli();
3859 if (info->uses_dma_out) {
3860 /* Put the DMA on hold and disable the channel */
3861 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, hold);
3862 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) !=
3863 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, hold));
3864 e100_disable_txdma_channel(info);
3865 }
3866
3867 /* Must make sure transmitter is not stopped before we can transmit */
3868 if (tty->stopped)
3869 rs_start(tty);
3870
3871 /* Enable manual transmit interrupt and send from there */
3872 DFLOW(DEBUG_LOG(info->line, "rs_send_xchar 0x%02X\n", ch));
3873 info->x_char = ch;
3874 e100_enable_serial_tx_ready_irq(info);
3875 restore_flags(flags);
3876}
3877
3878/*
3879 * ------------------------------------------------------------
3880 * rs_throttle()
3881 *
3882 * This routine is called by the upper-layer tty layer to signal that
3883 * incoming characters should be throttled.
3884 * ------------------------------------------------------------
3885 */
3886static void
3887rs_throttle(struct tty_struct * tty)
3888{
3889 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3890#ifdef SERIAL_DEBUG_THROTTLE
3891 char buf[64];
3892
3893 printk("throttle %s: %lu....\n", tty_name(tty, buf),
3894 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3895#endif
3896 DFLOW(DEBUG_LOG(info->line,"rs_throttle %lu\n", tty->ldisc.chars_in_buffer(tty)));
3897
3898 /* Do RTS before XOFF since XOFF might take some time */
3899 if (tty->termios->c_cflag & CRTSCTS) {
3900 /* Turn off RTS line */
3901 e100_rts(info, 0);
3902 }
3903 if (I_IXOFF(tty))
3904 rs_send_xchar(tty, STOP_CHAR(tty));
3905
3906}
3907
3908static void
3909rs_unthrottle(struct tty_struct * tty)
3910{
3911 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3912#ifdef SERIAL_DEBUG_THROTTLE
3913 char buf[64];
3914
3915 printk("unthrottle %s: %lu....\n", tty_name(tty, buf),
3916 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3917#endif
3918 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle ldisc %d\n", tty->ldisc.chars_in_buffer(tty)));
3919 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle flip.count: %i\n", tty->flip.count));
3920 /* Do RTS before XOFF since XOFF might take some time */
3921 if (tty->termios->c_cflag & CRTSCTS) {
3922 /* Assert RTS line */
3923 e100_rts(info, 1);
3924 }
3925
3926 if (I_IXOFF(tty)) {
3927 if (info->x_char)
3928 info->x_char = 0;
3929 else
3930 rs_send_xchar(tty, START_CHAR(tty));
3931 }
3932
3933}
3934
3935/*
3936 * ------------------------------------------------------------
3937 * rs_ioctl() and friends
3938 * ------------------------------------------------------------
3939 */
3940
3941static int
3942get_serial_info(struct e100_serial * info,
3943 struct serial_struct * retinfo)
3944{
3945 struct serial_struct tmp;
3946
3947 /* this is all probably wrong, there are a lot of fields
3948 * here that we don't have in e100_serial and maybe we
3949 * should set them to something else than 0.
3950 */
3951
3952 if (!retinfo)
3953 return -EFAULT;
3954 memset(&tmp, 0, sizeof(tmp));
3955 tmp.type = info->type;
3956 tmp.line = info->line;
3957 tmp.port = (int)info->port;
3958 tmp.irq = info->irq;
3959 tmp.flags = info->flags;
3960 tmp.baud_base = info->baud_base;
3961 tmp.close_delay = info->close_delay;
3962 tmp.closing_wait = info->closing_wait;
3963 tmp.custom_divisor = info->custom_divisor;
3964 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3965 return -EFAULT;
3966 return 0;
3967}
3968
3969static int
3970set_serial_info(struct e100_serial *info,
3971 struct serial_struct *new_info)
3972{
3973 struct serial_struct new_serial;
3974 struct e100_serial old_info;
3975 int retval = 0;
3976
3977 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
3978 return -EFAULT;
3979
3980 old_info = *info;
3981
3982 if (!capable(CAP_SYS_ADMIN)) {
3983 if ((new_serial.type != info->type) ||
3984 (new_serial.close_delay != info->close_delay) ||
3985 ((new_serial.flags & ~ASYNC_USR_MASK) !=
3986 (info->flags & ~ASYNC_USR_MASK)))
3987 return -EPERM;
3988 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
3989 (new_serial.flags & ASYNC_USR_MASK));
3990 goto check_and_exit;
3991 }
3992
3993 if (info->count > 1)
3994 return -EBUSY;
3995
3996 /*
3997 * OK, past this point, all the error checking has been done.
3998 * At this point, we start making changes.....
3999 */
4000
4001 info->baud_base = new_serial.baud_base;
4002 info->flags = ((info->flags & ~ASYNC_FLAGS) |
4003 (new_serial.flags & ASYNC_FLAGS));
4004 info->custom_divisor = new_serial.custom_divisor;
4005 info->type = new_serial.type;
4006 info->close_delay = new_serial.close_delay;
4007 info->closing_wait = new_serial.closing_wait;
4008 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
4009
4010 check_and_exit:
4011 if (info->flags & ASYNC_INITIALIZED) {
4012 change_speed(info);
4013 } else
4014 retval = startup(info);
4015 return retval;
4016}
4017
4018/*
4019 * get_lsr_info - get line status register info
4020 *
4021 * Purpose: Let user call ioctl() to get info when the UART physically
4022 * is emptied. On bus types like RS485, the transmitter must
4023 * release the bus after transmitting. This must be done when
4024 * the transmit shift register is empty, not be done when the
4025 * transmit holding register is empty. This functionality
4026 * allows an RS485 driver to be written in user space.
4027 */
4028static int
4029get_lsr_info(struct e100_serial * info, unsigned int *value)
4030{
4031 unsigned int result = TIOCSER_TEMT;
4032#ifndef CONFIG_SVINTO_SIM
4033 unsigned long curr_time = jiffies;
4034 unsigned long curr_time_usec = GET_JIFFIES_USEC();
4035 unsigned long elapsed_usec =
4036 (curr_time - info->last_tx_active) * 1000000/HZ +
4037 curr_time_usec - info->last_tx_active_usec;
4038
4039 if (info->xmit.head != info->xmit.tail ||
4040 elapsed_usec < 2*info->char_time_usec) {
4041 result = 0;
4042 }
4043#endif
4044
4045 if (copy_to_user(value, &result, sizeof(int)))
4046 return -EFAULT;
4047 return 0;
4048}
4049
4050#ifdef SERIAL_DEBUG_IO
4051struct state_str
4052{
4053 int state;
4054 const char *str;
4055};
4056
4057const struct state_str control_state_str[] = {
4058 {TIOCM_DTR, "DTR" },
4059 {TIOCM_RTS, "RTS"},
4060 {TIOCM_ST, "ST?" },
4061 {TIOCM_SR, "SR?" },
4062 {TIOCM_CTS, "CTS" },
4063 {TIOCM_CD, "CD" },
4064 {TIOCM_RI, "RI" },
4065 {TIOCM_DSR, "DSR" },
4066 {0, NULL }
4067};
4068
4069char *get_control_state_str(int MLines, char *s)
4070{
4071 int i = 0;
4072
4073 s[0]='\0';
4074 while (control_state_str[i].str != NULL) {
4075 if (MLines & control_state_str[i].state) {
4076 if (s[0] != '\0') {
4077 strcat(s, ", ");
4078 }
4079 strcat(s, control_state_str[i].str);
4080 }
4081 i++;
4082 }
4083 return s;
4084}
4085#endif
4086
4087static int
4088get_modem_info(struct e100_serial * info, unsigned int *value)
4089{
4090 unsigned int result;
4091 /* Polarity isn't verified */
4092#if 0 /*def SERIAL_DEBUG_IO */
4093
4094 printk("get_modem_info: RTS: %i DTR: %i CD: %i RI: %i DSR: %i CTS: %i\n",
4095 E100_RTS_GET(info),
4096 E100_DTR_GET(info),
4097 E100_CD_GET(info),
4098 E100_RI_GET(info),
4099 E100_DSR_GET(info),
4100 E100_CTS_GET(info));
4101#endif
4102
4103 result =
4104 (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
4105 | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
4106 | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
4107 | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
4108 | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
4109 | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
4110
4111#ifdef SERIAL_DEBUG_IO
4112 printk("e100ser: modem state: %i 0x%08X\n", result, result);
4113 {
4114 char s[100];
4115
4116 get_control_state_str(result, s);
4117 printk("state: %s\n", s);
4118 }
4119#endif
4120 if (copy_to_user(value, &result, sizeof(int)))
4121 return -EFAULT;
4122 return 0;
4123}
4124
4125
4126static int
4127set_modem_info(struct e100_serial * info, unsigned int cmd,
4128 unsigned int *value)
4129{
4130 unsigned int arg;
4131
4132 if (copy_from_user(&arg, value, sizeof(int)))
4133 return -EFAULT;
4134
4135 switch (cmd) {
4136 case TIOCMBIS:
4137 if (arg & TIOCM_RTS) {
4138 e100_rts(info, 1);
4139 }
4140 if (arg & TIOCM_DTR) {
4141 e100_dtr(info, 1);
4142 }
4143 /* Handle FEMALE behaviour */
4144 if (arg & TIOCM_RI) {
4145 e100_ri_out(info, 1);
4146 }
4147 if (arg & TIOCM_CD) {
4148 e100_cd_out(info, 1);
4149 }
4150 break;
4151 case TIOCMBIC:
4152 if (arg & TIOCM_RTS) {
4153 e100_rts(info, 0);
4154 }
4155 if (arg & TIOCM_DTR) {
4156 e100_dtr(info, 0);
4157 }
4158 /* Handle FEMALE behaviour */
4159 if (arg & TIOCM_RI) {
4160 e100_ri_out(info, 0);
4161 }
4162 if (arg & TIOCM_CD) {
4163 e100_cd_out(info, 0);
4164 }
4165 break;
4166 case TIOCMSET:
4167 e100_rts(info, arg & TIOCM_RTS);
4168 e100_dtr(info, arg & TIOCM_DTR);
4169 /* Handle FEMALE behaviour */
4170 e100_ri_out(info, arg & TIOCM_RI);
4171 e100_cd_out(info, arg & TIOCM_CD);
4172 break;
4173 default:
4174 return -EINVAL;
4175 }
4176 return 0;
4177}
4178
4179
4180static void
4181rs_break(struct tty_struct *tty, int break_state)
4182{
4183 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
4184 unsigned long flags;
4185
4186 if (!info->port)
4187 return;
4188
4189 save_flags(flags);
4190 cli();
4191 if (break_state == -1) {
4192 /* Go to manual mode and set the txd pin to 0 */
4193 info->tx_ctrl &= 0x3F; /* Clear bit 7 (txd) and 6 (tr_enable) */
4194 } else {
4195 info->tx_ctrl |= (0x80 | 0x40); /* Set bit 7 (txd) and 6 (tr_enable) */
4196 }
4197 info->port[REG_TR_CTRL] = info->tx_ctrl;
4198 restore_flags(flags);
4199}
4200
4201static int
4202rs_ioctl(struct tty_struct *tty, struct file * file,
4203 unsigned int cmd, unsigned long arg)
4204{
4205 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
4206
4207 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
4208 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
4209 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
4210 if (tty->flags & (1 << TTY_IO_ERROR))
4211 return -EIO;
4212 }
4213
4214 switch (cmd) {
4215 case TIOCMGET:
4216 return get_modem_info(info, (unsigned int *) arg);
4217 case TIOCMBIS:
4218 case TIOCMBIC:
4219 case TIOCMSET:
4220 return set_modem_info(info, cmd, (unsigned int *) arg);
4221 case TIOCGSERIAL:
4222 return get_serial_info(info,
4223 (struct serial_struct *) arg);
4224 case TIOCSSERIAL:
4225 return set_serial_info(info,
4226 (struct serial_struct *) arg);
4227 case TIOCSERGETLSR: /* Get line status register */
4228 return get_lsr_info(info, (unsigned int *) arg);
4229
4230 case TIOCSERGSTRUCT:
4231 if (copy_to_user((struct e100_serial *) arg,
4232 info, sizeof(struct e100_serial)))
4233 return -EFAULT;
4234 return 0;
4235
4236#if defined(CONFIG_ETRAX_RS485)
4237 case TIOCSERSETRS485:
4238 {
4239 struct rs485_control rs485ctrl;
4240 if (copy_from_user(&rs485ctrl, (struct rs485_control*)arg, sizeof(rs485ctrl)))
4241 return -EFAULT;
4242
4243 return e100_enable_rs485(tty, &rs485ctrl);
4244 }
4245
4246 case TIOCSERWRRS485:
4247 {
4248 struct rs485_write rs485wr;
4249 if (copy_from_user(&rs485wr, (struct rs485_write*)arg, sizeof(rs485wr)))
4250 return -EFAULT;
4251
4252 return e100_write_rs485(tty, 1, rs485wr.outc, rs485wr.outc_size);
4253 }
4254#endif
4255
4256 default:
4257 return -ENOIOCTLCMD;
4258 }
4259 return 0;
4260}
4261
4262static void
4263rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
4264{
4265 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
4266
4267 if (tty->termios->c_cflag == old_termios->c_cflag &&
4268 tty->termios->c_iflag == old_termios->c_iflag)
4269 return;
4270
4271 change_speed(info);
4272
4273 /* Handle turning off CRTSCTS */
4274 if ((old_termios->c_cflag & CRTSCTS) &&
4275 !(tty->termios->c_cflag & CRTSCTS)) {
4276 tty->hw_stopped = 0;
4277 rs_start(tty);
4278 }
4279
4280}
4281
4282/* In debugport.c - register a console write function that uses the normal
4283 * serial driver
4284 */
4285typedef int (*debugport_write_function)(int i, const char *buf, unsigned int len);
4286
4287extern debugport_write_function debug_write_function;
4288
4289static int rs_debug_write_function(int i, const char *buf, unsigned int len)
4290{
4291 int cnt;
4292 int written = 0;
4293 struct tty_struct *tty;
4294 static int recurse_cnt = 0;
4295
4296 tty = rs_table[i].tty;
4297 if (tty) {
4298 unsigned long flags;
4299 if (recurse_cnt > 5) /* We skip this debug output */
4300 return 1;
4301
4302 local_irq_save(flags);
4303 recurse_cnt++;
4304 local_irq_restore(flags);
4305 do {
4306 cnt = rs_write(tty, 0, buf + written, len);
4307 if (cnt >= 0) {
4308 written += cnt;
4309 buf += cnt;
4310 len -= cnt;
4311 } else
4312 len = cnt;
4313 } while(len > 0);
4314 local_irq_save(flags);
4315 recurse_cnt--;
4316 local_irq_restore(flags);
4317 return 1;
4318 }
4319 return 0;
4320}
4321
4322/*
4323 * ------------------------------------------------------------
4324 * rs_close()
4325 *
4326 * This routine is called when the serial port gets closed. First, we
4327 * wait for the last remaining data to be sent. Then, we unlink its
4328 * S structure from the interrupt chain if necessary, and we free
4329 * that IRQ if nothing is left in the chain.
4330 * ------------------------------------------------------------
4331 */
4332static void
4333rs_close(struct tty_struct *tty, struct file * filp)
4334{
4335 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
4336 unsigned long flags;
4337
4338 if (!info)
4339 return;
4340
4341 /* interrupts are disabled for this entire function */
4342
4343 save_flags(flags);
4344 cli();
4345
4346 if (tty_hung_up_p(filp)) {
4347 restore_flags(flags);
4348 return;
4349 }
4350
4351#ifdef SERIAL_DEBUG_OPEN
4352 printk("[%d] rs_close ttyS%d, count = %d\n", current->pid,
4353 info->line, info->count);
4354#endif
4355 if ((tty->count == 1) && (info->count != 1)) {
4356 /*
4357 * Uh, oh. tty->count is 1, which means that the tty
4358 * structure will be freed. Info->count should always
4359 * be one in these conditions. If it's greater than
4360 * one, we've got real problems, since it means the
4361 * serial port won't be shutdown.
4362 */
4363 printk(KERN_CRIT
4364 "rs_close: bad serial port count; tty->count is 1, "
4365 "info->count is %d\n", info->count);
4366 info->count = 1;
4367 }
4368 if (--info->count < 0) {
4369 printk(KERN_CRIT "rs_close: bad serial port count for ttyS%d: %d\n",
4370 info->line, info->count);
4371 info->count = 0;
4372 }
4373 if (info->count) {
4374 restore_flags(flags);
4375 return;
4376 }
4377 info->flags |= ASYNC_CLOSING;
4378 /*
4379 * Save the termios structure, since this port may have
4380 * separate termios for callout and dialin.
4381 */
4382 if (info->flags & ASYNC_NORMAL_ACTIVE)
4383 info->normal_termios = *tty->termios;
4384 /*
4385 * Now we wait for the transmit buffer to clear; and we notify
4386 * the line discipline to only process XON/XOFF characters.
4387 */
4388 tty->closing = 1;
4389 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
4390 tty_wait_until_sent(tty, info->closing_wait);
4391 /*
4392 * At this point we stop accepting input. To do this, we
4393 * disable the serial receiver and the DMA receive interrupt.
4394 */
4395#ifdef SERIAL_HANDLE_EARLY_ERRORS
4396 e100_disable_serial_data_irq(info);
4397#endif
4398
4399#ifndef CONFIG_SVINTO_SIM
4400 e100_disable_rx(info);
4401 e100_disable_rx_irq(info);
4402
4403 if (info->flags & ASYNC_INITIALIZED) {
4404 /*
4405 * Before we drop DTR, make sure the UART transmitter
4406 * has completely drained; this is especially
4407 * important as we have a transmit FIFO!
4408 */
4409 rs_wait_until_sent(tty, HZ);
4410 }
4411#endif
4412
4413 shutdown(info);
4414 if (tty->driver->flush_buffer)
4415 tty->driver->flush_buffer(tty);
4416 if (tty->ldisc.flush_buffer)
4417 tty->ldisc.flush_buffer(tty);
4418 tty->closing = 0;
4419 info->event = 0;
4420 info->tty = 0;
4421 if (info->blocked_open) {
4422 if (info->close_delay) {
4423 set_current_state(TASK_INTERRUPTIBLE);
4424 schedule_timeout(info->close_delay);
4425 }
4426 wake_up_interruptible(&info->open_wait);
4427 }
4428 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
4429 wake_up_interruptible(&info->close_wait);
4430 restore_flags(flags);
4431
4432 /* port closed */
4433
4434#if defined(CONFIG_ETRAX_RS485)
4435 if (info->rs485.enabled) {
4436 info->rs485.enabled = 0;
4437#if defined(CONFIG_ETRAX_RS485_ON_PA)
4438 *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
4439#endif
4440#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
4441 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
4442 rs485_port_g_bit, 0);
4443#endif
4444#if defined(CONFIG_ETRAX_RS485_LTC1387)
4445 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
4446 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT, 0);
4447 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
4448 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT, 0);
4449#endif
4450 }
4451#endif
4452}
4453
4454/*
4455 * rs_wait_until_sent() --- wait until the transmitter is empty
4456 */
4457static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
4458{
4459 unsigned long orig_jiffies;
4460 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
4461 unsigned long curr_time = jiffies;
4462 unsigned long curr_time_usec = GET_JIFFIES_USEC();
4463 long elapsed_usec =
4464 (curr_time - info->last_tx_active) * (1000000/HZ) +
4465 curr_time_usec - info->last_tx_active_usec;
4466
4467 /*
4468 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
4469 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
4470 */
4471 orig_jiffies = jiffies;
4472 while (info->xmit.head != info->xmit.tail || /* More in send queue */
4473 (*info->ostatusadr & 0x007f) || /* more in FIFO */
4474 (elapsed_usec < 2*info->char_time_usec)) {
4475 set_current_state(TASK_INTERRUPTIBLE);
4476 schedule_timeout(1);
4477 if (signal_pending(current))
4478 break;
4479 if (timeout && time_after(jiffies, orig_jiffies + timeout))
4480 break;
4481 curr_time = jiffies;
4482 curr_time_usec = GET_JIFFIES_USEC();
4483 elapsed_usec =
4484 (curr_time - info->last_tx_active) * (1000000/HZ) +
4485 curr_time_usec - info->last_tx_active_usec;
4486 }
4487 set_current_state(TASK_RUNNING);
4488}
4489
4490/*
4491 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
4492 */
4493void
4494rs_hangup(struct tty_struct *tty)
4495{
4496 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
4497
4498 rs_flush_buffer(tty);
4499 shutdown(info);
4500 info->event = 0;
4501 info->count = 0;
4502 info->flags &= ~ASYNC_NORMAL_ACTIVE;
4503 info->tty = 0;
4504 wake_up_interruptible(&info->open_wait);
4505}
4506
4507/*
4508 * ------------------------------------------------------------
4509 * rs_open() and friends
4510 * ------------------------------------------------------------
4511 */
4512static int
4513block_til_ready(struct tty_struct *tty, struct file * filp,
4514 struct e100_serial *info)
4515{
4516 DECLARE_WAITQUEUE(wait, current);
4517 unsigned long flags;
4518 int retval;
4519 int do_clocal = 0, extra_count = 0;
4520
4521 /*
4522 * If the device is in the middle of being closed, then block
4523 * until it's done, and then try again.
4524 */
4525 if (tty_hung_up_p(filp) ||
4526 (info->flags & ASYNC_CLOSING)) {
4527 if (info->flags & ASYNC_CLOSING)
4528 interruptible_sleep_on(&info->close_wait);
4529#ifdef SERIAL_DO_RESTART
4530 if (info->flags & ASYNC_HUP_NOTIFY)
4531 return -EAGAIN;
4532 else
4533 return -ERESTARTSYS;
4534#else
4535 return -EAGAIN;
4536#endif
4537 }
4538
4539 /*
4540 * If non-blocking mode is set, or the port is not enabled,
4541 * then make the check up front and then exit.
4542 */
4543 if ((filp->f_flags & O_NONBLOCK) ||
4544 (tty->flags & (1 << TTY_IO_ERROR))) {
4545 info->flags |= ASYNC_NORMAL_ACTIVE;
4546 return 0;
4547 }
4548
4549 if (tty->termios->c_cflag & CLOCAL) {
4550 do_clocal = 1;
4551 }
4552
4553 /*
4554 * Block waiting for the carrier detect and the line to become
4555 * free (i.e., not in use by the callout). While we are in
4556 * this loop, info->count is dropped by one, so that
4557 * rs_close() knows when to free things. We restore it upon
4558 * exit, either normal or abnormal.
4559 */
4560 retval = 0;
4561 add_wait_queue(&info->open_wait, &wait);
4562#ifdef SERIAL_DEBUG_OPEN
4563 printk("block_til_ready before block: ttyS%d, count = %d\n",
4564 info->line, info->count);
4565#endif
4566 save_flags(flags);
4567 cli();
4568 if (!tty_hung_up_p(filp)) {
4569 extra_count++;
4570 info->count--;
4571 }
4572 restore_flags(flags);
4573 info->blocked_open++;
4574 while (1) {
4575 save_flags(flags);
4576 cli();
4577 /* assert RTS and DTR */
4578 e100_rts(info, 1);
4579 e100_dtr(info, 1);
4580 restore_flags(flags);
4581 set_current_state(TASK_INTERRUPTIBLE);
4582 if (tty_hung_up_p(filp) ||
4583 !(info->flags & ASYNC_INITIALIZED)) {
4584#ifdef SERIAL_DO_RESTART
4585 if (info->flags & ASYNC_HUP_NOTIFY)
4586 retval = -EAGAIN;
4587 else
4588 retval = -ERESTARTSYS;
4589#else
4590 retval = -EAGAIN;
4591#endif
4592 break;
4593 }
4594 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
4595 /* && (do_clocal || DCD_IS_ASSERTED) */
4596 break;
4597 if (signal_pending(current)) {
4598 retval = -ERESTARTSYS;
4599 break;
4600 }
4601#ifdef SERIAL_DEBUG_OPEN
4602 printk("block_til_ready blocking: ttyS%d, count = %d\n",
4603 info->line, info->count);
4604#endif
4605 schedule();
4606 }
4607 set_current_state(TASK_RUNNING);
4608 remove_wait_queue(&info->open_wait, &wait);
4609 if (extra_count)
4610 info->count++;
4611 info->blocked_open--;
4612#ifdef SERIAL_DEBUG_OPEN
4613 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
4614 info->line, info->count);
4615#endif
4616 if (retval)
4617 return retval;
4618 info->flags |= ASYNC_NORMAL_ACTIVE;
4619 return 0;
4620}
4621
4622/*
4623 * This routine is called whenever a serial port is opened.
4624 * It performs the serial-specific initialization for the tty structure.
4625 */
4626static int
4627rs_open(struct tty_struct *tty, struct file * filp)
4628{
4629 struct e100_serial *info;
4630 int retval, line;
4631 unsigned long page;
4632
4633 /* find which port we want to open */
4634
4635 line = tty->index;
4636
4637 if (line < 0 || line >= NR_PORTS)
4638 return -ENODEV;
4639
4640 /* find the corresponding e100_serial struct in the table */
4641 info = rs_table + line;
4642
4643 /* don't allow the opening of ports that are not enabled in the HW config */
4644 if (!info->enabled)
4645 return -ENODEV;
4646
4647#ifdef SERIAL_DEBUG_OPEN
4648 printk("[%d] rs_open %s, count = %d\n", current->pid, tty->name,
4649 info->count);
4650#endif
4651
4652 info->count++;
4653 tty->driver_data = info;
4654 info->tty = tty;
4655
4656 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
4657
4658 if (!tmp_buf) {
4659 page = get_zeroed_page(GFP_KERNEL);
4660 if (!page) {
4661 return -ENOMEM;
4662 }
4663 if (tmp_buf)
4664 free_page(page);
4665 else
4666 tmp_buf = (unsigned char *) page;
4667 }
4668
4669 /*
4670 * If the port is in the middle of closing, bail out now
4671 */
4672 if (tty_hung_up_p(filp) ||
4673 (info->flags & ASYNC_CLOSING)) {
4674 if (info->flags & ASYNC_CLOSING)
4675 interruptible_sleep_on(&info->close_wait);
4676#ifdef SERIAL_DO_RESTART
4677 return ((info->flags & ASYNC_HUP_NOTIFY) ?
4678 -EAGAIN : -ERESTARTSYS);
4679#else
4680 return -EAGAIN;
4681#endif
4682 }
4683
4684 /*
4685 * Start up the serial port
4686 */
4687
4688 retval = startup(info);
4689 if (retval)
4690 return retval;
4691
4692 retval = block_til_ready(tty, filp, info);
4693 if (retval) {
4694#ifdef SERIAL_DEBUG_OPEN
4695 printk("rs_open returning after block_til_ready with %d\n",
4696 retval);
4697#endif
4698 return retval;
4699 }
4700
4701 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
4702 *tty->termios = info->normal_termios;
4703 change_speed(info);
4704 }
4705
4706#ifdef SERIAL_DEBUG_OPEN
4707 printk("rs_open ttyS%d successful...\n", info->line);
4708#endif
4709 DLOG_INT_TRIG( log_int_pos = 0);
4710
4711 DFLIP( if (info->line == SERIAL_DEBUG_LINE) {
4712 info->icount.rx = 0;
4713 } );
4714
4715 return 0;
4716}
4717
4718/*
4719 * /proc fs routines....
4720 */
4721
4722extern _INLINE_ int line_info(char *buf, struct e100_serial *info)
4723{
4724 char stat_buf[30];
4725 int ret;
4726 unsigned long tmp;
4727
4728 ret = sprintf(buf, "%d: uart:E100 port:%lX irq:%d",
4729 info->line, (unsigned long)info->port, info->irq);
4730
4731 if (!info->port || (info->type == PORT_UNKNOWN)) {
4732 ret += sprintf(buf+ret, "\n");
4733 return ret;
4734 }
4735
4736 stat_buf[0] = 0;
4737 stat_buf[1] = 0;
4738 if (!E100_RTS_GET(info))
4739 strcat(stat_buf, "|RTS");
4740 if (!E100_CTS_GET(info))
4741 strcat(stat_buf, "|CTS");
4742 if (!E100_DTR_GET(info))
4743 strcat(stat_buf, "|DTR");
4744 if (!E100_DSR_GET(info))
4745 strcat(stat_buf, "|DSR");
4746 if (!E100_CD_GET(info))
4747 strcat(stat_buf, "|CD");
4748 if (!E100_RI_GET(info))
4749 strcat(stat_buf, "|RI");
4750
4751 ret += sprintf(buf+ret, " baud:%d", info->baud);
4752
4753 ret += sprintf(buf+ret, " tx:%lu rx:%lu",
4754 (unsigned long)info->icount.tx,
4755 (unsigned long)info->icount.rx);
4756 tmp = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
4757 if (tmp) {
4758 ret += sprintf(buf+ret, " tx_pend:%lu/%lu",
4759 (unsigned long)tmp,
4760 (unsigned long)SERIAL_XMIT_SIZE);
4761 }
4762
4763 ret += sprintf(buf+ret, " rx_pend:%lu/%lu",
4764 (unsigned long)info->recv_cnt,
4765 (unsigned long)info->max_recv_cnt);
4766
4767#if 1
4768 if (info->tty) {
4769
4770 if (info->tty->stopped)
4771 ret += sprintf(buf+ret, " stopped:%i",
4772 (int)info->tty->stopped);
4773 if (info->tty->hw_stopped)
4774 ret += sprintf(buf+ret, " hw_stopped:%i",
4775 (int)info->tty->hw_stopped);
4776 }
4777
4778 {
4779 unsigned char rstat = info->port[REG_STATUS];
4780 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) )
4781 ret += sprintf(buf+ret, " xoff_detect:1");
4782 }
4783
4784#endif
4785
4786
4787
4788
4789 if (info->icount.frame)
4790 ret += sprintf(buf+ret, " fe:%lu",
4791 (unsigned long)info->icount.frame);
4792
4793 if (info->icount.parity)
4794 ret += sprintf(buf+ret, " pe:%lu",
4795 (unsigned long)info->icount.parity);
4796
4797 if (info->icount.brk)
4798 ret += sprintf(buf+ret, " brk:%lu",
4799 (unsigned long)info->icount.brk);
4800
4801 if (info->icount.overrun)
4802 ret += sprintf(buf+ret, " oe:%lu",
4803 (unsigned long)info->icount.overrun);
4804
4805 /*
4806 * Last thing is the RS-232 status lines
4807 */
4808 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
4809 return ret;
4810}
4811
4812int rs_read_proc(char *page, char **start, off_t off, int count,
4813 int *eof, void *data)
4814{
4815 int i, len = 0, l;
4816 off_t begin = 0;
4817
4818 len += sprintf(page, "serinfo:1.0 driver:%s\n",
4819 serial_version);
4820 for (i = 0; i < NR_PORTS && len < 4000; i++) {
4821 if (!rs_table[i].enabled)
4822 continue;
4823 l = line_info(page + len, &rs_table[i]);
4824 len += l;
4825 if (len+begin > off+count)
4826 goto done;
4827 if (len+begin < off) {
4828 begin += len;
4829 len = 0;
4830 }
4831 }
4832#ifdef DEBUG_LOG_INCLUDED
4833 for (i = 0; i < debug_log_pos; i++) {
4834 len += sprintf(page + len, "%-4i %lu.%lu ", i, debug_log[i].time, timer_data_to_ns(debug_log[i].timer_data));
4835 len += sprintf(page + len, debug_log[i].string, debug_log[i].value);
4836 if (len+begin > off+count)
4837 goto done;
4838 if (len+begin < off) {
4839 begin += len;
4840 len = 0;
4841 }
4842 }
4843 len += sprintf(page + len, "debug_log %i/%i %li bytes\n",
4844 i, DEBUG_LOG_SIZE, begin+len);
4845 debug_log_pos = 0;
4846#endif
4847
4848 *eof = 1;
4849done:
4850 if (off >= len+begin)
4851 return 0;
4852 *start = page + (off-begin);
4853 return ((count < begin+len-off) ? count : begin+len-off);
4854}
4855
4856/* Finally, routines used to initialize the serial driver. */
4857
4858static void
4859show_serial_version(void)
4860{
4861 printk(KERN_INFO
4862 "ETRAX 100LX serial-driver %s, (c) 2000-2004 Axis Communications AB\r\n",
4863 &serial_version[11]); /* "$Revision: x.yy" */
4864}
4865
4866/* rs_init inits the driver at boot (using the module_init chain) */
4867
4868static struct tty_operations rs_ops = {
4869 .open = rs_open,
4870 .close = rs_close,
4871 .write = rs_write,
4872 .flush_chars = rs_flush_chars,
4873 .write_room = rs_write_room,
4874 .chars_in_buffer = rs_chars_in_buffer,
4875 .flush_buffer = rs_flush_buffer,
4876 .ioctl = rs_ioctl,
4877 .throttle = rs_throttle,
4878 .unthrottle = rs_unthrottle,
4879 .set_termios = rs_set_termios,
4880 .stop = rs_stop,
4881 .start = rs_start,
4882 .hangup = rs_hangup,
4883 .break_ctl = rs_break,
4884 .send_xchar = rs_send_xchar,
4885 .wait_until_sent = rs_wait_until_sent,
4886 .read_proc = rs_read_proc,
4887};
4888
4889static int __init
4890rs_init(void)
4891{
4892 int i;
4893 struct e100_serial *info;
4894 struct tty_driver *driver = alloc_tty_driver(NR_PORTS);
4895
4896 if (!driver)
4897 return -ENOMEM;
4898
4899 show_serial_version();
4900
4901 /* Setup the timed flush handler system */
4902
4903#if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
4904 init_timer(&flush_timer);
4905 flush_timer.function = timed_flush_handler;
4906 mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
4907#endif
4908
4909 /* Initialize the tty_driver structure */
4910
4911 driver->driver_name = "serial";
4912 driver->name = "ttyS";
4913 driver->major = TTY_MAJOR;
4914 driver->minor_start = 64;
4915 driver->type = TTY_DRIVER_TYPE_SERIAL;
4916 driver->subtype = SERIAL_TYPE_NORMAL;
4917 driver->init_termios = tty_std_termios;
4918 driver->init_termios.c_cflag =
4919 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
4920 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
4921 driver->termios = serial_termios;
4922 driver->termios_locked = serial_termios_locked;
4923
4924 tty_set_operations(driver, &rs_ops);
4925 serial_driver = driver;
4926 if (tty_register_driver(driver))
4927 panic("Couldn't register serial driver\n");
4928 /* do some initializing for the separate ports */
4929
4930 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
4931 info->uses_dma_in = 0;
4932 info->uses_dma_out = 0;
4933 info->line = i;
4934 info->tty = 0;
4935 info->type = PORT_ETRAX;
4936 info->tr_running = 0;
4937 info->forced_eop = 0;
4938 info->baud_base = DEF_BAUD_BASE;
4939 info->custom_divisor = 0;
4940 info->flags = 0;
4941 info->close_delay = 5*HZ/10;
4942 info->closing_wait = 30*HZ;
4943 info->x_char = 0;
4944 info->event = 0;
4945 info->count = 0;
4946 info->blocked_open = 0;
4947 info->normal_termios = driver->init_termios;
4948 init_waitqueue_head(&info->open_wait);
4949 init_waitqueue_head(&info->close_wait);
4950 info->xmit.buf = NULL;
4951 info->xmit.tail = info->xmit.head = 0;
4952 info->first_recv_buffer = info->last_recv_buffer = NULL;
4953 info->recv_cnt = info->max_recv_cnt = 0;
4954 info->last_tx_active_usec = 0;
4955 info->last_tx_active = 0;
4956
4957#if defined(CONFIG_ETRAX_RS485)
4958 /* Set sane defaults */
4959 info->rs485.rts_on_send = 0;
4960 info->rs485.rts_after_sent = 1;
4961 info->rs485.delay_rts_before_send = 0;
4962 info->rs485.enabled = 0;
4963#endif
4964 INIT_WORK(&info->work, do_softint, info);
4965
4966 if (info->enabled) {
4967 printk(KERN_INFO "%s%d at 0x%x is a builtin UART with DMA\n",
4968 serial_driver->name, info->line, (unsigned int)info->port);
4969 }
4970 }
4971#ifdef CONFIG_ETRAX_FAST_TIMER
4972#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
4973 memset(fast_timers, 0, sizeof(fast_timers));
4974#endif
4975#ifdef CONFIG_ETRAX_RS485
4976 memset(fast_timers_rs485, 0, sizeof(fast_timers_rs485));
4977#endif
4978 fast_timer_init();
4979#endif
4980
4981#ifndef CONFIG_SVINTO_SIM
4982 /* Not needed in simulator. May only complicate stuff. */
4983 /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
4984
4985 if (request_irq(SERIAL_IRQ_NBR, ser_interrupt, SA_SHIRQ | SA_INTERRUPT, "serial ", NULL))
4986 panic("irq8");
4987
4988#ifdef CONFIG_ETRAX_SERIAL_PORT0
4989#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
4990 if (request_irq(SER0_DMA_TX_IRQ_NBR, tr_interrupt, SA_INTERRUPT, "serial 0 dma tr", NULL))
4991 panic("irq22");
4992#endif
4993#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
4994 if (request_irq(SER0_DMA_RX_IRQ_NBR, rec_interrupt, SA_INTERRUPT, "serial 0 dma rec", NULL))
4995 panic("irq23");
4996#endif
4997#endif
4998
4999#ifdef CONFIG_ETRAX_SERIAL_PORT1
5000#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA8_OUT
5001 if (request_irq(SER1_DMA_TX_IRQ_NBR, tr_interrupt, SA_INTERRUPT, "serial 1 dma tr", NULL))
5002 panic("irq24");
5003#endif
5004#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA9_IN
5005 if (request_irq(SER1_DMA_RX_IRQ_NBR, rec_interrupt, SA_INTERRUPT, "serial 1 dma rec", NULL))
5006 panic("irq25");
5007#endif
5008#endif
5009#ifdef CONFIG_ETRAX_SERIAL_PORT2
5010 /* DMA Shared with par0 (and SCSI0 and ATA) */
5011#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
5012 if (request_irq(SER2_DMA_TX_IRQ_NBR, tr_interrupt, SA_SHIRQ | SA_INTERRUPT, "serial 2 dma tr", NULL))
5013 panic("irq18");
5014#endif
5015#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
5016 if (request_irq(SER2_DMA_RX_IRQ_NBR, rec_interrupt, SA_SHIRQ | SA_INTERRUPT, "serial 2 dma rec", NULL))
5017 panic("irq19");
5018#endif
5019#endif
5020#ifdef CONFIG_ETRAX_SERIAL_PORT3
5021 /* DMA Shared with par1 (and SCSI1 and Extern DMA 0) */
5022#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA4_OUT
5023 if (request_irq(SER3_DMA_TX_IRQ_NBR, tr_interrupt, SA_SHIRQ | SA_INTERRUPT, "serial 3 dma tr", NULL))
5024 panic("irq20");
5025#endif
5026#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA5_IN
5027 if (request_irq(SER3_DMA_RX_IRQ_NBR, rec_interrupt, SA_SHIRQ | SA_INTERRUPT, "serial 3 dma rec", NULL))
5028 panic("irq21");
5029#endif
5030#endif
5031
5032#ifdef CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST
5033 if (request_irq(TIMER1_IRQ_NBR, timeout_interrupt, SA_SHIRQ | SA_INTERRUPT,
5034 "fast serial dma timeout", NULL)) {
5035 printk(KERN_CRIT "err: timer1 irq\n");
5036 }
5037#endif
5038#endif /* CONFIG_SVINTO_SIM */
5039 debug_write_function = rs_debug_write_function;
5040 return 0;
5041}
5042
5043/* this makes sure that rs_init is called during kernel boot */
5044
5045module_init(rs_init);
5046
5047/*
5048 * register_serial and unregister_serial allows for serial ports to be
5049 * configured at run-time, to support PCMCIA modems.
5050 */
5051int
5052register_serial(struct serial_struct *req)
5053{
5054 return -1;
5055}
5056
5057void unregister_serial(int line)
5058{
5059}
diff --git a/drivers/serial/crisv10.h b/drivers/serial/crisv10.h
new file mode 100644
index 000000000000..1800c0e7531a
--- /dev/null
+++ b/drivers/serial/crisv10.h
@@ -0,0 +1,137 @@
1/*
2 * serial.h: Arch-dep definitions for the Etrax100 serial driver.
3 *
4 * Copyright (C) 1998, 1999, 2000 Axis Communications AB
5 */
6
7#ifndef _ETRAX_SERIAL_H
8#define _ETRAX_SERIAL_H
9
10#include <linux/config.h>
11#include <linux/circ_buf.h>
12#include <asm/termios.h>
13
14/* Software state per channel */
15
16#ifdef __KERNEL__
17/*
18 * This is our internal structure for each serial port's state.
19 *
20 * Many fields are paralleled by the structure used by the serial_struct
21 * structure.
22 *
23 * For definitions of the flags field, see tty.h
24 */
25
26#define SERIAL_RECV_DESCRIPTORS 8
27
28struct etrax_recv_buffer {
29 struct etrax_recv_buffer *next;
30 unsigned short length;
31 unsigned char error;
32 unsigned char pad;
33
34 unsigned char buffer[0];
35};
36
37struct e100_serial {
38 int baud;
39 volatile u8 *port; /* R_SERIALx_CTRL */
40 u32 irq; /* bitnr in R_IRQ_MASK2 for dmaX_descr */
41
42 /* Output registers */
43 volatile u8 *oclrintradr; /* adr to R_DMA_CHx_CLR_INTR */
44 volatile u32 *ofirstadr; /* adr to R_DMA_CHx_FIRST */
45 volatile u8 *ocmdadr; /* adr to R_DMA_CHx_CMD */
46 const volatile u8 *ostatusadr; /* adr to R_DMA_CHx_STATUS */
47
48 /* Input registers */
49 volatile u8 *iclrintradr; /* adr to R_DMA_CHx_CLR_INTR */
50 volatile u32 *ifirstadr; /* adr to R_DMA_CHx_FIRST */
51 volatile u8 *icmdadr; /* adr to R_DMA_CHx_CMD */
52 volatile u32 *idescradr; /* adr to R_DMA_CHx_DESCR */
53
54 int flags; /* defined in tty.h */
55
56 u8 rx_ctrl; /* shadow for R_SERIALx_REC_CTRL */
57 u8 tx_ctrl; /* shadow for R_SERIALx_TR_CTRL */
58 u8 iseteop; /* bit number for R_SET_EOP for the input dma */
59 int enabled; /* Set to 1 if the port is enabled in HW config */
60
61 u8 dma_out_enabled:1; /* Set to 1 if DMA should be used */
62 u8 dma_in_enabled:1; /* Set to 1 if DMA should be used */
63
64 /* end of fields defined in rs_table[] in .c-file */
65 u8 uses_dma_in; /* Set to 1 if DMA is used */
66 u8 uses_dma_out; /* Set to 1 if DMA is used */
67 u8 forced_eop; /* a fifo eop has been forced */
68 int baud_base; /* For special baudrates */
69 int custom_divisor; /* For special baudrates */
70 struct etrax_dma_descr tr_descr;
71 struct etrax_dma_descr rec_descr[SERIAL_RECV_DESCRIPTORS];
72 int cur_rec_descr;
73
74 volatile int tr_running; /* 1 if output is running */
75
76 struct tty_struct *tty;
77 int read_status_mask;
78 int ignore_status_mask;
79 int x_char; /* xon/xoff character */
80 int close_delay;
81 unsigned short closing_wait;
82 unsigned short closing_wait2;
83 unsigned long event;
84 unsigned long last_active;
85 int line;
86 int type; /* PORT_ETRAX */
87 int count; /* # of fd on device */
88 int blocked_open; /* # of blocked opens */
89 struct circ_buf xmit;
90 struct etrax_recv_buffer *first_recv_buffer;
91 struct etrax_recv_buffer *last_recv_buffer;
92 unsigned int recv_cnt;
93 unsigned int max_recv_cnt;
94
95 struct work_struct work;
96 struct async_icount icount; /* error-statistics etc.*/
97 struct termios normal_termios;
98 struct termios callout_termios;
99#ifdef DECLARE_WAITQUEUE
100 wait_queue_head_t open_wait;
101 wait_queue_head_t close_wait;
102#else
103 struct wait_queue *open_wait;
104 struct wait_queue *close_wait;
105#endif
106
107 unsigned long char_time_usec; /* The time for 1 char, in usecs */
108 unsigned long flush_time_usec; /* How often we should flush */
109 unsigned long last_tx_active_usec; /* Last tx usec in the jiffies */
110 unsigned long last_tx_active; /* Last tx time in jiffies */
111 unsigned long last_rx_active_usec; /* Last rx usec in the jiffies */
112 unsigned long last_rx_active; /* Last rx time in jiffies */
113
114 int break_detected_cnt;
115 int errorcode;
116
117#ifdef CONFIG_ETRAX_RS485
118 struct rs485_control rs485; /* RS-485 support */
119#endif
120};
121
122/* this PORT is not in the standard serial.h. it's not actually used for
123 * anything since we only have one type of async serial-port anyway in this
124 * system.
125 */
126
127#define PORT_ETRAX 1
128
129/*
130 * Events are used to schedule things to happen at timer-interrupt
131 * time, instead of at rs interrupt time.
132 */
133#define RS_EVENT_WRITE_WAKEUP 0
134
135#endif /* __KERNEL__ */
136
137#endif /* !_ETRAX_SERIAL_H */
diff --git a/drivers/serial/dz.c b/drivers/serial/dz.c
new file mode 100644
index 000000000000..97824eeeafae
--- /dev/null
+++ b/drivers/serial/dz.c
@@ -0,0 +1,822 @@
1/*
2 * dz.c: Serial port driver for DECStations equiped
3 * with the DZ chipset.
4 *
5 * Copyright (C) 1998 Olivier A. D. Lebaillif
6 *
7 * Email: olivier.lebaillif@ifrsys.com
8 *
9 * [31-AUG-98] triemer
10 * Changed IRQ to use Harald's dec internals interrupts.h
11 * removed base_addr code - moving address assignment to setup.c
12 * Changed name of dz_init to rs_init to be consistent with tc code
13 * [13-NOV-98] triemer fixed code to receive characters
14 * after patches by harald to irq code.
15 * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
16 * field from "current" - somewhere between 2.1.121 and 2.1.131
17 Qua Jun 27 15:02:26 BRT 2001
18 * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
19 *
20 * Parts (C) 1999 David Airlie, airlied@linux.ie
21 * [07-SEP-99] Bugfixes
22 *
23 * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
24 * Converted to new serial core
25 */
26
27#undef DEBUG_DZ
28
29#include <linux/config.h>
30#include <linux/module.h>
31#include <linux/interrupt.h>
32#include <linux/init.h>
33#include <linux/console.h>
34#include <linux/tty.h>
35#include <linux/tty_flip.h>
36#include <linux/serial_core.h>
37#include <linux/serial.h>
38
39#include <asm/bootinfo.h>
40#include <asm/dec/interrupts.h>
41#include <asm/dec/kn01.h>
42#include <asm/dec/kn02.h>
43#include <asm/dec/machtype.h>
44#include <asm/dec/prom.h>
45#include <asm/irq.h>
46#include <asm/system.h>
47#include <asm/uaccess.h>
48
49#define CONSOLE_LINE (3) /* for definition of struct console */
50
51#include "dz.h"
52
53#define DZ_INTR_DEBUG 1
54
55static char *dz_name = "DECstation DZ serial driver version ";
56static char *dz_version = "1.02";
57
58struct dz_port {
59 struct uart_port port;
60 unsigned int cflag;
61};
62
63static struct dz_port dz_ports[DZ_NB_PORT];
64
65#ifdef DEBUG_DZ
66/*
67 * debugging code to send out chars via prom
68 */
69static void debug_console(const char *s, int count)
70{
71 unsigned i;
72
73 for (i = 0; i < count; i++) {
74 if (*s == 10)
75 prom_printf("%c", 13);
76 prom_printf("%c", *s++);
77 }
78}
79#endif
80
81/*
82 * ------------------------------------------------------------
83 * dz_in () and dz_out ()
84 *
85 * These routines are used to access the registers of the DZ
86 * chip, hiding relocation differences between implementation.
87 * ------------------------------------------------------------
88 */
89
90static inline unsigned short dz_in(struct dz_port *dport, unsigned offset)
91{
92 volatile unsigned short *addr =
93 (volatile unsigned short *) (dport->port.membase + offset);
94 return *addr;
95}
96
97static inline void dz_out(struct dz_port *dport, unsigned offset,
98 unsigned short value)
99{
100 volatile unsigned short *addr =
101 (volatile unsigned short *) (dport->port.membase + offset);
102 *addr = value;
103}
104
105/*
106 * ------------------------------------------------------------
107 * rs_stop () and rs_start ()
108 *
109 * These routines are called before setting or resetting
110 * tty->stopped. They enable or disable transmitter interrupts,
111 * as necessary.
112 * ------------------------------------------------------------
113 */
114
115static void dz_stop_tx(struct uart_port *uport, unsigned int tty_stop)
116{
117 struct dz_port *dport = (struct dz_port *)uport;
118 unsigned short tmp, mask = 1 << dport->port.line;
119 unsigned long flags;
120
121 spin_lock_irqsave(&dport->port.lock, flags);
122 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
123 tmp &= ~mask; /* clear the TX flag */
124 dz_out(dport, DZ_TCR, tmp);
125 spin_unlock_irqrestore(&dport->port.lock, flags);
126}
127
128static void dz_start_tx(struct uart_port *uport, unsigned int tty_start)
129{
130 struct dz_port *dport = (struct dz_port *)uport;
131 unsigned short tmp, mask = 1 << dport->port.line;
132 unsigned long flags;
133
134 spin_lock_irqsave(&dport->port.lock, flags);
135 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
136 tmp |= mask; /* set the TX flag */
137 dz_out(dport, DZ_TCR, tmp);
138 spin_unlock_irqrestore(&dport->port.lock, flags);
139}
140
141static void dz_stop_rx(struct uart_port *uport)
142{
143 struct dz_port *dport = (struct dz_port *)uport;
144 unsigned long flags;
145
146 spin_lock_irqsave(&dport->port.lock, flags);
147 dport->cflag &= ~DZ_CREAD;
148 dz_out(dport, DZ_LPR, dport->cflag);
149 spin_unlock_irqrestore(&dport->port.lock, flags);
150}
151
152static void dz_enable_ms(struct uart_port *port)
153{
154 /* nothing to do */
155}
156
157/*
158 * ------------------------------------------------------------
159 * Here starts the interrupt handling routines. All of the
160 * following subroutines are declared as inline and are folded
161 * into dz_interrupt. They were separated out for readability's
162 * sake.
163 *
164 * Note: rs_interrupt() is a "fast" interrupt, which means that it
165 * runs with interrupts turned off. People who may want to modify
166 * rs_interrupt() should try to keep the interrupt handler as fast as
167 * possible. After you are done making modifications, it is not a bad
168 * idea to do:
169 *
170 * make drivers/serial/dz.s
171 *
172 * and look at the resulting assemble code in dz.s.
173 *
174 * ------------------------------------------------------------
175 */
176
177/*
178 * ------------------------------------------------------------
179 * receive_char ()
180 *
181 * This routine deals with inputs from any lines.
182 * ------------------------------------------------------------
183 */
184static inline void dz_receive_chars(struct dz_port *dport)
185{
186 struct tty_struct *tty = NULL;
187 struct uart_icount *icount;
188 int ignore = 0;
189 unsigned short status, tmp;
190 unsigned char ch, flag;
191
192 /* this code is going to be a problem...
193 the call to tty_flip_buffer is going to need
194 to be rethought...
195 */
196 do {
197 status = dz_in(dport, DZ_RBUF);
198
199 /* punt so we don't get duplicate characters */
200 if (!(status & DZ_DVAL))
201 goto ignore_char;
202
203
204 ch = UCHAR(status); /* grab the char */
205 flag = TTY_NORMAL;
206
207#if 0
208 if (info->is_console) {
209 if (ch == 0)
210 return; /* it's a break ... */
211 }
212#endif
213
214 tty = dport->port.info->tty;/* now tty points to the proper dev */
215 icount = &dport->port.icount;
216
217 if (!tty)
218 break;
219 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
220 break;
221
222 icount->rx++;
223
224 /* keep track of the statistics */
225 if (status & (DZ_OERR | DZ_FERR | DZ_PERR)) {
226 if (status & DZ_PERR) /* parity error */
227 icount->parity++;
228 else if (status & DZ_FERR) /* frame error */
229 icount->frame++;
230 if (status & DZ_OERR) /* overrun error */
231 icount->overrun++;
232
233 /* check to see if we should ignore the character
234 and mask off conditions that should be ignored
235 */
236
237 if (status & dport->port.ignore_status_mask) {
238 if (++ignore > 100)
239 break;
240 goto ignore_char;
241 }
242 /* mask off the error conditions we want to ignore */
243 tmp = status & dport->port.read_status_mask;
244
245 if (tmp & DZ_PERR) {
246 flag = TTY_PARITY;
247#ifdef DEBUG_DZ
248 debug_console("PERR\n", 5);
249#endif
250 } else if (tmp & DZ_FERR) {
251 flag = TTY_FRAME;
252#ifdef DEBUG_DZ
253 debug_console("FERR\n", 5);
254#endif
255 }
256 if (tmp & DZ_OERR) {
257#ifdef DEBUG_DZ
258 debug_console("OERR\n", 5);
259#endif
260 tty_insert_flip_char(tty, ch, flag);
261 ch = 0;
262 flag = TTY_OVERRUN;
263 }
264 }
265 tty_insert_flip_char(tty, ch, flag);
266 ignore_char:
267 } while (status & DZ_DVAL);
268
269 if (tty)
270 tty_flip_buffer_push(tty);
271}
272
273/*
274 * ------------------------------------------------------------
275 * transmit_char ()
276 *
277 * This routine deals with outputs to any lines.
278 * ------------------------------------------------------------
279 */
280static inline void dz_transmit_chars(struct dz_port *dport)
281{
282 struct circ_buf *xmit = &dport->port.info->xmit;
283 unsigned char tmp;
284
285 if (dport->port.x_char) { /* XON/XOFF chars */
286 dz_out(dport, DZ_TDR, dport->port.x_char);
287 dport->port.icount.tx++;
288 dport->port.x_char = 0;
289 return;
290 }
291 /* if nothing to do or stopped or hardware stopped */
292 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
293 dz_stop_tx(&dport->port, 0);
294 return;
295 }
296
297 /*
298 * if something to do ... (rember the dz has no output fifo so we go
299 * one char at a time :-<
300 */
301 tmp = xmit->buf[xmit->tail];
302 xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
303 dz_out(dport, DZ_TDR, tmp);
304 dport->port.icount.tx++;
305
306 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
307 uart_write_wakeup(&dport->port);
308
309 /* Are we done */
310 if (uart_circ_empty(xmit))
311 dz_stop_tx(&dport->port, 0);
312}
313
314/*
315 * ------------------------------------------------------------
316 * check_modem_status ()
317 *
318 * Only valid for the MODEM line duh !
319 * ------------------------------------------------------------
320 */
321static inline void check_modem_status(struct dz_port *dport)
322{
323 unsigned short status;
324
325 /* if not ne modem line just return */
326 if (dport->port.line != DZ_MODEM)
327 return;
328
329 status = dz_in(dport, DZ_MSR);
330
331 /* it's easy, since DSR2 is the only bit in the register */
332 if (status)
333 dport->port.icount.dsr++;
334}
335
336/*
337 * ------------------------------------------------------------
338 * dz_interrupt ()
339 *
340 * this is the main interrupt routine for the DZ chip.
341 * It deals with the multiple ports.
342 * ------------------------------------------------------------
343 */
344static irqreturn_t dz_interrupt(int irq, void *dev, struct pt_regs *regs)
345{
346 struct dz_port *dport;
347 unsigned short status;
348
349 /* get the reason why we just got an irq */
350 status = dz_in((struct dz_port *)dev, DZ_CSR);
351 dport = &dz_ports[LINE(status)];
352
353 if (status & DZ_RDONE)
354 dz_receive_chars(dport);
355
356 if (status & DZ_TRDY)
357 dz_transmit_chars(dport);
358
359 /* FIXME: what about check modem status??? --rmk */
360
361 return IRQ_HANDLED;
362}
363
364/*
365 * -------------------------------------------------------------------
366 * Here ends the DZ interrupt routines.
367 * -------------------------------------------------------------------
368 */
369
370static unsigned int dz_get_mctrl(struct uart_port *uport)
371{
372 struct dz_port *dport = (struct dz_port *)uport;
373 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
374
375 if (dport->port.line == DZ_MODEM) {
376 /*
377 * CHECKME: This is a guess from the other code... --rmk
378 */
379 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
380 mctrl &= ~TIOCM_DSR;
381 }
382
383 return mctrl;
384}
385
386static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
387{
388 struct dz_port *dport = (struct dz_port *)uport;
389 unsigned short tmp;
390
391 if (dport->port.line == DZ_MODEM) {
392 tmp = dz_in(dport, DZ_TCR);
393 if (mctrl & TIOCM_DTR)
394 tmp &= ~DZ_MODEM_DTR;
395 else
396 tmp |= DZ_MODEM_DTR;
397 dz_out(dport, DZ_TCR, tmp);
398 }
399}
400
401/*
402 * -------------------------------------------------------------------
403 * startup ()
404 *
405 * various initialization tasks
406 * -------------------------------------------------------------------
407 */
408static int dz_startup(struct uart_port *uport)
409{
410 struct dz_port *dport = (struct dz_port *)uport;
411 unsigned long flags;
412 unsigned short tmp;
413
414 /* The dz lines for the mouse/keyboard must be
415 * opened using their respective drivers.
416 */
417 if ((dport->port.line == DZ_KEYBOARD) ||
418 (dport->port.line == DZ_MOUSE))
419 return -ENODEV;
420
421 spin_lock_irqsave(&dport->port.lock, flags);
422
423 /* enable the interrupt and the scanning */
424 tmp = dz_in(dport, DZ_CSR);
425 tmp |= DZ_RIE | DZ_TIE | DZ_MSE;
426 dz_out(dport, DZ_CSR, tmp);
427
428 spin_unlock_irqrestore(&dport->port.lock, flags);
429
430 return 0;
431}
432
433/*
434 * -------------------------------------------------------------------
435 * shutdown ()
436 *
437 * This routine will shutdown a serial port; interrupts are disabled, and
438 * DTR is dropped if the hangup on close termio flag is on.
439 * -------------------------------------------------------------------
440 */
441static void dz_shutdown(struct uart_port *uport)
442{
443 dz_stop_tx(uport, 0);
444}
445
446/*
447 * get_lsr_info - get line status register info
448 *
449 * Purpose: Let user call ioctl() to get info when the UART physically
450 * is emptied. On bus types like RS485, the transmitter must
451 * release the bus after transmitting. This must be done when
452 * the transmit shift register is empty, not be done when the
453 * transmit holding register is empty. This functionality
454 * allows an RS485 driver to be written in user space.
455 */
456static unsigned int dz_tx_empty(struct uart_port *uport)
457{
458 struct dz_port *dport = (struct dz_port *)uport;
459 unsigned short status = dz_in(dport, DZ_LPR);
460
461 /* FIXME: this appears to be obviously broken --rmk. */
462 return status ? TIOCSER_TEMT : 0;
463}
464
465static void dz_break_ctl(struct uart_port *uport, int break_state)
466{
467 struct dz_port *dport = (struct dz_port *)uport;
468 unsigned long flags;
469 unsigned short tmp, mask = 1 << uport->line;
470
471 spin_lock_irqsave(&uport->lock, flags);
472 tmp = dz_in(dport, DZ_TCR);
473 if (break_state)
474 tmp |= mask;
475 else
476 tmp &= ~mask;
477 dz_out(dport, DZ_TCR, tmp);
478 spin_unlock_irqrestore(&uport->lock, flags);
479}
480
481static void dz_set_termios(struct uart_port *uport, struct termios *termios,
482 struct termios *old_termios)
483{
484 struct dz_port *dport = (struct dz_port *)uport;
485 unsigned long flags;
486 unsigned int cflag, baud;
487
488 cflag = dport->port.line;
489
490 switch (termios->c_cflag & CSIZE) {
491 case CS5:
492 cflag |= DZ_CS5;
493 break;
494 case CS6:
495 cflag |= DZ_CS6;
496 break;
497 case CS7:
498 cflag |= DZ_CS7;
499 break;
500 case CS8:
501 default:
502 cflag |= DZ_CS8;
503 }
504
505 if (termios->c_cflag & CSTOPB)
506 cflag |= DZ_CSTOPB;
507 if (termios->c_cflag & PARENB)
508 cflag |= DZ_PARENB;
509 if (termios->c_cflag & PARODD)
510 cflag |= DZ_PARODD;
511
512 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
513 switch (baud) {
514 case 50:
515 cflag |= DZ_B50;
516 break;
517 case 75:
518 cflag |= DZ_B75;
519 break;
520 case 110:
521 cflag |= DZ_B110;
522 break;
523 case 134:
524 cflag |= DZ_B134;
525 break;
526 case 150:
527 cflag |= DZ_B150;
528 break;
529 case 300:
530 cflag |= DZ_B300;
531 break;
532 case 600:
533 cflag |= DZ_B600;
534 break;
535 case 1200:
536 cflag |= DZ_B1200;
537 break;
538 case 1800:
539 cflag |= DZ_B1800;
540 break;
541 case 2000:
542 cflag |= DZ_B2000;
543 break;
544 case 2400:
545 cflag |= DZ_B2400;
546 break;
547 case 3600:
548 cflag |= DZ_B3600;
549 break;
550 case 4800:
551 cflag |= DZ_B4800;
552 break;
553 case 7200:
554 cflag |= DZ_B7200;
555 break;
556 case 9600:
557 default:
558 cflag |= DZ_B9600;
559 }
560
561 if (termios->c_cflag & CREAD)
562 cflag |= DZ_RXENAB;
563
564 spin_lock_irqsave(&dport->port.lock, flags);
565
566 dz_out(dport, DZ_LPR, cflag);
567 dport->cflag = cflag;
568
569 /* setup accept flag */
570 dport->port.read_status_mask = DZ_OERR;
571 if (termios->c_iflag & INPCK)
572 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
573
574 /* characters to ignore */
575 uport->ignore_status_mask = 0;
576 if (termios->c_iflag & IGNPAR)
577 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
578
579 spin_unlock_irqrestore(&dport->port.lock, flags);
580}
581
582static const char *dz_type(struct uart_port *port)
583{
584 return "DZ";
585}
586
587static void dz_release_port(struct uart_port *port)
588{
589 /* nothing to do */
590}
591
592static int dz_request_port(struct uart_port *port)
593{
594 return 0;
595}
596
597static void dz_config_port(struct uart_port *port, int flags)
598{
599 if (flags & UART_CONFIG_TYPE)
600 port->type = PORT_DZ;
601}
602
603/*
604 * verify the new serial_struct (for TIOCSSERIAL).
605 */
606static int dz_verify_port(struct uart_port *port, struct serial_struct *ser)
607{
608 int ret = 0;
609 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
610 ret = -EINVAL;
611 if (ser->irq != port->irq)
612 ret = -EINVAL;
613 return ret;
614}
615
616static struct uart_ops dz_ops = {
617 .tx_empty = dz_tx_empty,
618 .get_mctrl = dz_get_mctrl,
619 .set_mctrl = dz_set_mctrl,
620 .stop_tx = dz_stop_tx,
621 .start_tx = dz_start_tx,
622 .stop_rx = dz_stop_rx,
623 .enable_ms = dz_enable_ms,
624 .break_ctl = dz_break_ctl,
625 .startup = dz_startup,
626 .shutdown = dz_shutdown,
627 .set_termios = dz_set_termios,
628 .type = dz_type,
629 .release_port = dz_release_port,
630 .request_port = dz_request_port,
631 .config_port = dz_config_port,
632 .verify_port = dz_verify_port,
633};
634
635static void __init dz_init_ports(void)
636{
637 static int first = 1;
638 struct dz_port *dport;
639 unsigned long base;
640 int i;
641
642 if (!first)
643 return;
644 first = 0;
645
646 if (mips_machtype == MACH_DS23100 ||
647 mips_machtype == MACH_DS5100)
648 base = (unsigned long) KN01_DZ11_BASE;
649 else
650 base = (unsigned long) KN02_DZ11_BASE;
651
652 for (i = 0, dport = dz_ports; i < DZ_NB_PORT; i++, dport++) {
653 spin_lock_init(&dport->port.lock);
654 dport->port.membase = (char *) base;
655 dport->port.iotype = SERIAL_IO_PORT;
656 dport->port.irq = dec_interrupt[DEC_IRQ_DZ11];
657 dport->port.line = i;
658 dport->port.fifosize = 1;
659 dport->port.ops = &dz_ops;
660 dport->port.flags = UPF_BOOT_AUTOCONF;
661 }
662}
663
664static void dz_reset(struct dz_port *dport)
665{
666 dz_out(dport, DZ_CSR, DZ_CLR);
667
668 while (dz_in(dport, DZ_CSR) & DZ_CLR);
669 /* FIXME: cpu_relax? */
670
671 iob();
672
673 /* enable scanning */
674 dz_out(dport, DZ_CSR, DZ_MSE);
675}
676
677#ifdef CONFIG_SERIAL_DZ_CONSOLE
678static void dz_console_put_char(struct dz_port *dport, unsigned char ch)
679{
680 unsigned long flags;
681 int loops = 2500;
682 unsigned short tmp = ch;
683 /* this code sends stuff out to serial device - spinning its
684 wheels and waiting. */
685
686 spin_lock_irqsave(&dport->port.lock, flags);
687
688 /* spin our wheels */
689 while (((dz_in(dport, DZ_CSR) & DZ_TRDY) != DZ_TRDY) && loops--)
690 /* FIXME: cpu_relax, udelay? --rmk */
691 ;
692
693 /* Actually transmit the character. */
694 dz_out(dport, DZ_TDR, tmp);
695
696 spin_unlock_irqrestore(&dport->port.lock, flags);
697}
698/*
699 * -------------------------------------------------------------------
700 * dz_console_print ()
701 *
702 * dz_console_print is registered for printk.
703 * The console must be locked when we get here.
704 * -------------------------------------------------------------------
705 */
706static void dz_console_print(struct console *cons,
707 const char *str,
708 unsigned int count)
709{
710 struct dz_port *dport = &dz_ports[CONSOLE_LINE];
711#ifdef DEBUG_DZ
712 prom_printf((char *) str);
713#endif
714 while (count--) {
715 if (*str == '\n')
716 dz_console_put_char(dport, '\r');
717 dz_console_put_char(dport, *str++);
718 }
719}
720
721static int __init dz_console_setup(struct console *co, char *options)
722{
723 struct dz_port *dport = &dz_ports[CONSOLE_LINE];
724 int baud = 9600;
725 int bits = 8;
726 int parity = 'n';
727 int flow = 'n';
728 int ret;
729 unsigned short mask, tmp;
730
731 if (options)
732 uart_parse_options(options, &baud, &parity, &bits, &flow);
733
734 dz_reset(dport);
735
736 ret = uart_set_options(&dport->port, co, baud, parity, bits, flow);
737 if (ret == 0) {
738 mask = 1 << dport->port.line;
739 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
740 if (!(tmp & mask)) {
741 tmp |= mask; /* set the TX flag */
742 dz_out(dport, DZ_TCR, tmp);
743 }
744 }
745
746 return ret;
747}
748
749static struct console dz_sercons =
750{
751 .name = "ttyS",
752 .write = dz_console_print,
753 .device = uart_console_device,
754 .setup = dz_console_setup,
755 .flags = CON_CONSDEV | CON_PRINTBUFFER,
756 .index = CONSOLE_LINE,
757};
758
759void __init dz_serial_console_init(void)
760{
761 dz_init_ports();
762
763 register_console(&dz_sercons);
764}
765
766#define SERIAL_DZ_CONSOLE &dz_sercons
767#else
768#define SERIAL_DZ_CONSOLE NULL
769#endif /* CONFIG_SERIAL_DZ_CONSOLE */
770
771static struct uart_driver dz_reg = {
772 .owner = THIS_MODULE,
773 .driver_name = "serial",
774#ifdef CONFIG_DEVFS
775 .dev_name = "tts/%d",
776#else
777 .dev_name = "ttyS%d",
778#endif
779 .major = TTY_MAJOR,
780 .minor = 64,
781 .nr = DZ_NB_PORT,
782 .cons = SERIAL_DZ_CONSOLE,
783};
784
785int __init dz_init(void)
786{
787 unsigned long flags;
788 int ret, i;
789
790 printk("%s%s\n", dz_name, dz_version);
791
792 dz_init_ports();
793
794 save_flags(flags);
795 cli();
796
797#ifndef CONFIG_SERIAL_DZ_CONSOLE
798 /* reset the chip */
799 dz_reset(&dz_ports[0]);
800#endif
801
802 /* order matters here... the trick is that flags
803 is updated... in request_irq - to immediatedly obliterate
804 it is unwise. */
805 restore_flags(flags);
806
807 if (request_irq(dz_ports[0].port.irq, dz_interrupt,
808 SA_INTERRUPT, "DZ", &dz_ports[0]))
809 panic("Unable to register DZ interrupt");
810
811 ret = uart_register_driver(&dz_reg);
812 if (ret != 0)
813 return ret;
814
815 for (i = 0; i < DZ_NB_PORT; i++)
816 uart_add_one_port(&dz_reg, &dz_ports[i].port);
817
818 return ret;
819}
820
821MODULE_DESCRIPTION("DECstation DZ serial driver");
822MODULE_LICENSE("GPL");
diff --git a/drivers/serial/dz.h b/drivers/serial/dz.h
new file mode 100644
index 000000000000..86ef417382bb
--- /dev/null
+++ b/drivers/serial/dz.h
@@ -0,0 +1,118 @@
1/*
2 * dz.h: Serial port driver for DECStations equiped
3 * with the DZ chipset.
4 *
5 * Copyright (C) 1998 Olivier A. D. Lebaillif
6 *
7 * Email: olivier.lebaillif@ifrsys.com
8 *
9 */
10#ifndef DZ_SERIAL_H
11#define DZ_SERIAL_H
12
13/*
14 * Definitions for the Control and Status Received.
15 */
16#define DZ_TRDY 0x8000 /* Transmitter empty */
17#define DZ_TIE 0x4000 /* Transmitter Interrupt Enable */
18#define DZ_RDONE 0x0080 /* Receiver data ready */
19#define DZ_RIE 0x0040 /* Receive Interrupt Enable */
20#define DZ_MSE 0x0020 /* Master Scan Enable */
21#define DZ_CLR 0x0010 /* Master reset */
22#define DZ_MAINT 0x0008 /* Loop Back Mode */
23
24/*
25 * Definitions for the Received buffer.
26 */
27#define DZ_RBUF_MASK 0x00FF /* Data Mask in the Receive Buffer */
28#define DZ_LINE_MASK 0x0300 /* Line Mask in the Receive Buffer */
29#define DZ_DVAL 0x8000 /* Valid Data indicator */
30#define DZ_OERR 0x4000 /* Overrun error indicator */
31#define DZ_FERR 0x2000 /* Frame error indicator */
32#define DZ_PERR 0x1000 /* Parity error indicator */
33
34#define LINE(x) (x & DZ_LINE_MASK) >> 8 /* Get the line number from the input buffer */
35#define UCHAR(x) (unsigned char)(x & DZ_RBUF_MASK)
36
37/*
38 * Definitions for the Transmit Register.
39 */
40#define DZ_LINE_KEYBOARD 0x0001
41#define DZ_LINE_MOUSE 0x0002
42#define DZ_LINE_MODEM 0x0004
43#define DZ_LINE_PRINTER 0x0008
44
45#define DZ_MODEM_DTR 0x0400 /* DTR for the modem line (2) */
46
47/*
48 * Definitions for the Modem Status Register.
49 */
50#define DZ_MODEM_DSR 0x0200 /* DSR for the modem line (2) */
51
52/*
53 * Definitions for the Transmit Data Register.
54 */
55#define DZ_BRK0 0x0100 /* Break assertion for line 0 */
56#define DZ_BRK1 0x0200 /* Break assertion for line 1 */
57#define DZ_BRK2 0x0400 /* Break assertion for line 2 */
58#define DZ_BRK3 0x0800 /* Break assertion for line 3 */
59
60/*
61 * Definitions for the Line Parameter Register.
62 */
63#define DZ_KEYBOARD 0x0000 /* line 0 = keyboard */
64#define DZ_MOUSE 0x0001 /* line 1 = mouse */
65#define DZ_MODEM 0x0002 /* line 2 = modem */
66#define DZ_PRINTER 0x0003 /* line 3 = printer */
67
68#define DZ_CSIZE 0x0018 /* Number of bits per byte (mask) */
69#define DZ_CS5 0x0000 /* 5 bits per byte */
70#define DZ_CS6 0x0008 /* 6 bits per byte */
71#define DZ_CS7 0x0010 /* 7 bits per byte */
72#define DZ_CS8 0x0018 /* 8 bits per byte */
73
74#define DZ_CSTOPB 0x0020 /* 2 stop bits instead of one */
75
76#define DZ_PARENB 0x0040 /* Parity enable */
77#define DZ_PARODD 0x0080 /* Odd parity instead of even */
78
79#define DZ_CBAUD 0x0E00 /* Baud Rate (mask) */
80#define DZ_B50 0x0000
81#define DZ_B75 0x0100
82#define DZ_B110 0x0200
83#define DZ_B134 0x0300
84#define DZ_B150 0x0400
85#define DZ_B300 0x0500
86#define DZ_B600 0x0600
87#define DZ_B1200 0x0700
88#define DZ_B1800 0x0800
89#define DZ_B2000 0x0900
90#define DZ_B2400 0x0A00
91#define DZ_B3600 0x0B00
92#define DZ_B4800 0x0C00
93#define DZ_B7200 0x0D00
94#define DZ_B9600 0x0E00
95
96#define DZ_CREAD 0x1000 /* Enable receiver */
97#define DZ_RXENAB 0x1000 /* enable receive char */
98/*
99 * Addresses for the DZ registers
100 */
101#define DZ_CSR 0x00 /* Control and Status Register */
102#define DZ_RBUF 0x08 /* Receive Buffer */
103#define DZ_LPR 0x08 /* Line Parameters Register */
104#define DZ_TCR 0x10 /* Transmitter Control Register */
105#define DZ_MSR 0x18 /* Modem Status Register */
106#define DZ_TDR 0x18 /* Transmit Data Register */
107
108#define DZ_NB_PORT 4
109
110#define DZ_XMIT_SIZE 4096 /* buffer size */
111#define DZ_WAKEUP_CHARS DZ_XMIT_SIZE/4
112
113#ifdef MODULE
114int init_module (void)
115void cleanup_module (void)
116#endif
117
118#endif /* DZ_SERIAL_H */
diff --git a/drivers/serial/icom.c b/drivers/serial/icom.c
new file mode 100644
index 000000000000..546a0bc77e1e
--- /dev/null
+++ b/drivers/serial/icom.c
@@ -0,0 +1,1691 @@
1/*
2 * icom.c
3 *
4 * Copyright (C) 2001 IBM Corporation. All rights reserved.
5 *
6 * Serial device driver.
7 *
8 * Based on code from serial.c
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25#define SERIAL_DO_RESTART
26#include <linux/module.h>
27#include <linux/config.h>
28#include <linux/version.h>
29#include <linux/kernel.h>
30#include <linux/errno.h>
31#include <linux/signal.h>
32#include <linux/sched.h>
33#include <linux/timer.h>
34#include <linux/interrupt.h>
35#include <linux/tty.h>
36#include <linux/termios.h>
37#include <linux/fs.h>
38#include <linux/tty_flip.h>
39#include <linux/serial.h>
40#include <linux/serial_reg.h>
41#include <linux/major.h>
42#include <linux/string.h>
43#include <linux/fcntl.h>
44#include <linux/ptrace.h>
45#include <linux/ioport.h>
46#include <linux/mm.h>
47#include <linux/slab.h>
48#include <linux/init.h>
49#include <linux/delay.h>
50#include <linux/pci.h>
51#include <linux/vmalloc.h>
52#include <linux/smp.h>
53#include <linux/smp_lock.h>
54#include <linux/spinlock.h>
55#include <linux/kobject.h>
56#include <linux/firmware.h>
57#include <linux/bitops.h>
58
59#include <asm/system.h>
60#include <asm/segment.h>
61#include <asm/io.h>
62#include <asm/irq.h>
63#include <asm/uaccess.h>
64
65#include "icom.h"
66
67/*#define ICOM_TRACE enable port trace capabilities */
68
69#define ICOM_DRIVER_NAME "icom"
70#define ICOM_VERSION_STR "1.3.1"
71#define NR_PORTS 128
72#define ICOM_PORT ((struct icom_port *)port)
73#define to_icom_adapter(d) container_of(d, struct icom_adapter, kobj)
74
75static const struct pci_device_id icom_pci_table[] = {
76 {
77 .vendor = PCI_VENDOR_ID_IBM,
78 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_1,
79 .subvendor = PCI_ANY_ID,
80 .subdevice = PCI_ANY_ID,
81 .driver_data = ADAPTER_V1,
82 },
83 {
84 .vendor = PCI_VENDOR_ID_IBM,
85 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
86 .subvendor = PCI_VENDOR_ID_IBM,
87 .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_TWO_PORTS_RVX,
88 .driver_data = ADAPTER_V2,
89 },
90 {
91 .vendor = PCI_VENDOR_ID_IBM,
92 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
93 .subvendor = PCI_VENDOR_ID_IBM,
94 .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM,
95 .driver_data = ADAPTER_V2,
96 },
97 {
98 .vendor = PCI_VENDOR_ID_IBM,
99 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
100 .subvendor = PCI_VENDOR_ID_IBM,
101 .subdevice = PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL,
102 .driver_data = ADAPTER_V2,
103 },
104 {}
105};
106
107struct lookup_proc_table start_proc[4] = {
108 {NULL, ICOM_CONTROL_START_A},
109 {NULL, ICOM_CONTROL_START_B},
110 {NULL, ICOM_CONTROL_START_C},
111 {NULL, ICOM_CONTROL_START_D}
112};
113
114
115struct lookup_proc_table stop_proc[4] = {
116 {NULL, ICOM_CONTROL_STOP_A},
117 {NULL, ICOM_CONTROL_STOP_B},
118 {NULL, ICOM_CONTROL_STOP_C},
119 {NULL, ICOM_CONTROL_STOP_D}
120};
121
122struct lookup_int_table int_mask_tbl[4] = {
123 {NULL, ICOM_INT_MASK_PRC_A},
124 {NULL, ICOM_INT_MASK_PRC_B},
125 {NULL, ICOM_INT_MASK_PRC_C},
126 {NULL, ICOM_INT_MASK_PRC_D},
127};
128
129
130MODULE_DEVICE_TABLE(pci, icom_pci_table);
131
132static LIST_HEAD(icom_adapter_head);
133
134/* spinlock for adapter initialization and changing adapter operations */
135static spinlock_t icom_lock;
136
137#ifdef ICOM_TRACE
138static inline void trace(struct icom_port *, char *, unsigned long) {};
139#else
140static inline void trace(struct icom_port *icom_port, char *trace_pt, unsigned long trace_data) {};
141#endif
142
143static void free_port_memory(struct icom_port *icom_port)
144{
145 struct pci_dev *dev = icom_port->adapter->pci_dev;
146
147 trace(icom_port, "RET_PORT_MEM", 0);
148 if (icom_port->recv_buf) {
149 pci_free_consistent(dev, 4096, icom_port->recv_buf,
150 icom_port->recv_buf_pci);
151 icom_port->recv_buf = NULL;
152 }
153 if (icom_port->xmit_buf) {
154 pci_free_consistent(dev, 4096, icom_port->xmit_buf,
155 icom_port->xmit_buf_pci);
156 icom_port->xmit_buf = NULL;
157 }
158 if (icom_port->statStg) {
159 pci_free_consistent(dev, 4096, icom_port->statStg,
160 icom_port->statStg_pci);
161 icom_port->statStg = NULL;
162 }
163
164 if (icom_port->xmitRestart) {
165 pci_free_consistent(dev, 4096, icom_port->xmitRestart,
166 icom_port->xmitRestart_pci);
167 icom_port->xmitRestart = NULL;
168 }
169}
170
171static int __init get_port_memory(struct icom_port *icom_port)
172{
173 int index;
174 unsigned long stgAddr;
175 unsigned long startStgAddr;
176 unsigned long offset;
177 struct pci_dev *dev = icom_port->adapter->pci_dev;
178
179 icom_port->xmit_buf =
180 pci_alloc_consistent(dev, 4096, &icom_port->xmit_buf_pci);
181 if (!icom_port->xmit_buf) {
182 dev_err(&dev->dev, "Can not allocate Transmit buffer\n");
183 return -ENOMEM;
184 }
185
186 trace(icom_port, "GET_PORT_MEM",
187 (unsigned long) icom_port->xmit_buf);
188
189 icom_port->recv_buf =
190 pci_alloc_consistent(dev, 4096, &icom_port->recv_buf_pci);
191 if (!icom_port->recv_buf) {
192 dev_err(&dev->dev, "Can not allocate Receive buffer\n");
193 free_port_memory(icom_port);
194 return -ENOMEM;
195 }
196 trace(icom_port, "GET_PORT_MEM",
197 (unsigned long) icom_port->recv_buf);
198
199 icom_port->statStg =
200 pci_alloc_consistent(dev, 4096, &icom_port->statStg_pci);
201 if (!icom_port->statStg) {
202 dev_err(&dev->dev, "Can not allocate Status buffer\n");
203 free_port_memory(icom_port);
204 return -ENOMEM;
205 }
206 trace(icom_port, "GET_PORT_MEM",
207 (unsigned long) icom_port->statStg);
208
209 icom_port->xmitRestart =
210 pci_alloc_consistent(dev, 4096, &icom_port->xmitRestart_pci);
211 if (!icom_port->xmitRestart) {
212 dev_err(&dev->dev,
213 "Can not allocate xmit Restart buffer\n");
214 free_port_memory(icom_port);
215 return -ENOMEM;
216 }
217
218 memset(icom_port->statStg, 0, 4096);
219
220 /* FODs: Frame Out Descriptor Queue, this is a FIFO queue that
221 indicates that frames are to be transmitted
222 */
223
224 stgAddr = (unsigned long) icom_port->statStg;
225 for (index = 0; index < NUM_XBUFFS; index++) {
226 trace(icom_port, "FOD_ADDR", stgAddr);
227 stgAddr = stgAddr + sizeof(icom_port->statStg->xmit[0]);
228 if (index < (NUM_XBUFFS - 1)) {
229 memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
230 icom_port->statStg->xmit[index].leLengthASD =
231 (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ);
232 trace(icom_port, "FOD_ADDR", stgAddr);
233 trace(icom_port, "FOD_XBUFF",
234 (unsigned long) icom_port->xmit_buf);
235 icom_port->statStg->xmit[index].leBuffer =
236 cpu_to_le32(icom_port->xmit_buf_pci);
237 } else if (index == (NUM_XBUFFS - 1)) {
238 memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
239 icom_port->statStg->xmit[index].leLengthASD =
240 (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ);
241 trace(icom_port, "FOD_XBUFF",
242 (unsigned long) icom_port->xmit_buf);
243 icom_port->statStg->xmit[index].leBuffer =
244 cpu_to_le32(icom_port->xmit_buf_pci);
245 } else {
246 memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
247 }
248 }
249 /* FIDs */
250 startStgAddr = stgAddr;
251
252 /* fill in every entry, even if no buffer */
253 for (index = 0; index < NUM_RBUFFS; index++) {
254 trace(icom_port, "FID_ADDR", stgAddr);
255 stgAddr = stgAddr + sizeof(icom_port->statStg->rcv[0]);
256 icom_port->statStg->rcv[index].leLength = 0;
257 icom_port->statStg->rcv[index].WorkingLength =
258 (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
259 if (index < (NUM_RBUFFS - 1) ) {
260 offset = stgAddr - (unsigned long) icom_port->statStg;
261 icom_port->statStg->rcv[index].leNext =
262 cpu_to_le32(icom_port-> statStg_pci + offset);
263 trace(icom_port, "FID_RBUFF",
264 (unsigned long) icom_port->recv_buf);
265 icom_port->statStg->rcv[index].leBuffer =
266 cpu_to_le32(icom_port->recv_buf_pci);
267 } else if (index == (NUM_RBUFFS -1) ) {
268 offset = startStgAddr - (unsigned long) icom_port->statStg;
269 icom_port->statStg->rcv[index].leNext =
270 cpu_to_le32(icom_port-> statStg_pci + offset);
271 trace(icom_port, "FID_RBUFF",
272 (unsigned long) icom_port->recv_buf + 2048);
273 icom_port->statStg->rcv[index].leBuffer =
274 cpu_to_le32(icom_port->recv_buf_pci + 2048);
275 } else {
276 icom_port->statStg->rcv[index].leNext = 0;
277 icom_port->statStg->rcv[index].leBuffer = 0;
278 }
279 }
280
281 return 0;
282}
283
284static void stop_processor(struct icom_port *icom_port)
285{
286 unsigned long temp;
287 unsigned long flags;
288 int port;
289
290 spin_lock_irqsave(&icom_lock, flags);
291
292 port = icom_port->port;
293 if (port == 0 || port == 1)
294 stop_proc[port].global_control_reg = &icom_port->global_reg->control;
295 else
296 stop_proc[port].global_control_reg = &icom_port->global_reg->control_2;
297
298
299 if (port < 4) {
300 temp = readl(stop_proc[port].global_control_reg);
301 temp =
302 (temp & ~start_proc[port].processor_id) | stop_proc[port].processor_id;
303 writel(temp, stop_proc[port].global_control_reg);
304
305 /* write flush */
306 readl(stop_proc[port].global_control_reg);
307 } else {
308 dev_err(&icom_port->adapter->pci_dev->dev,
309 "Invalid port assignment\n");
310 }
311
312 spin_unlock_irqrestore(&icom_lock, flags);
313}
314
315static void start_processor(struct icom_port *icom_port)
316{
317 unsigned long temp;
318 unsigned long flags;
319 int port;
320
321 spin_lock_irqsave(&icom_lock, flags);
322
323 port = icom_port->port;
324 if (port == 0 || port == 1)
325 start_proc[port].global_control_reg = &icom_port->global_reg->control;
326 else
327 start_proc[port].global_control_reg = &icom_port->global_reg->control_2;
328 if (port < 4) {
329 temp = readl(start_proc[port].global_control_reg);
330 temp =
331 (temp & ~stop_proc[port].processor_id) | start_proc[port].processor_id;
332 writel(temp, start_proc[port].global_control_reg);
333
334 /* write flush */
335 readl(start_proc[port].global_control_reg);
336 } else {
337 dev_err(&icom_port->adapter->pci_dev->dev,
338 "Invalid port assignment\n");
339 }
340
341 spin_unlock_irqrestore(&icom_lock, flags);
342}
343
344static void load_code(struct icom_port *icom_port)
345{
346 const struct firmware *fw;
347 char __iomem *iram_ptr;
348 int index;
349 int status = 0;
350 void __iomem *dram_ptr = icom_port->dram;
351 dma_addr_t temp_pci;
352 unsigned char *new_page = NULL;
353 unsigned char cable_id = NO_CABLE;
354 struct pci_dev *dev = icom_port->adapter->pci_dev;
355
356 /* Clear out any pending interrupts */
357 writew(0x3FFF, icom_port->int_reg);
358
359 trace(icom_port, "CLEAR_INTERRUPTS", 0);
360
361 /* Stop processor */
362 stop_processor(icom_port);
363
364 /* Zero out DRAM */
365 memset_io(dram_ptr, 0, 512);
366
367 /* Load Call Setup into Adapter */
368 if (request_firmware(&fw, "icom_call_setup.bin", &dev->dev) < 0) {
369 dev_err(&dev->dev,"Unable to load icom_call_setup.bin firmware image\n");
370 status = -1;
371 goto load_code_exit;
372 }
373
374 if (fw->size > ICOM_DCE_IRAM_OFFSET) {
375 dev_err(&dev->dev, "Invalid firmware image for icom_call_setup.bin found.\n");
376 release_firmware(fw);
377 status = -1;
378 goto load_code_exit;
379 }
380
381 iram_ptr = (char __iomem *)icom_port->dram + ICOM_IRAM_OFFSET;
382 for (index = 0; index < fw->size; index++)
383 writeb(fw->data[index], &iram_ptr[index]);
384
385 release_firmware(fw);
386
387 /* Load Resident DCE portion of Adapter */
388 if (request_firmware(&fw, "icom_res_dce.bin", &dev->dev) < 0) {
389 dev_err(&dev->dev,"Unable to load icom_res_dce.bin firmware image\n");
390 status = -1;
391 goto load_code_exit;
392 }
393
394 if (fw->size > ICOM_IRAM_SIZE) {
395 dev_err(&dev->dev, "Invalid firmware image for icom_res_dce.bin found.\n");
396 release_firmware(fw);
397 status = -1;
398 goto load_code_exit;
399 }
400
401 iram_ptr = (char __iomem *) icom_port->dram + ICOM_IRAM_OFFSET;
402 for (index = ICOM_DCE_IRAM_OFFSET; index < fw->size; index++)
403 writeb(fw->data[index], &iram_ptr[index]);
404
405 release_firmware(fw);
406
407 /* Set Hardware level */
408 if ((icom_port->adapter->version | ADAPTER_V2) == ADAPTER_V2)
409 writeb(V2_HARDWARE, &(icom_port->dram->misc_flags));
410
411 /* Start the processor in Adapter */
412 start_processor(icom_port);
413
414 writeb((HDLC_PPP_PURE_ASYNC | HDLC_FF_FILL),
415 &(icom_port->dram->HDLCConfigReg));
416 writeb(0x04, &(icom_port->dram->FlagFillIdleTimer)); /* 0.5 seconds */
417 writeb(0x00, &(icom_port->dram->CmdReg));
418 writeb(0x10, &(icom_port->dram->async_config3));
419 writeb((ICOM_ACFG_DRIVE1 | ICOM_ACFG_NO_PARITY | ICOM_ACFG_8BPC |
420 ICOM_ACFG_1STOP_BIT), &(icom_port->dram->async_config2));
421
422 /*Set up data in icom DRAM to indicate where personality
423 *code is located and its length.
424 */
425 new_page = pci_alloc_consistent(dev, 4096, &temp_pci);
426
427 if (!new_page) {
428 dev_err(&dev->dev, "Can not allocate DMA buffer\n");
429 status = -1;
430 goto load_code_exit;
431 }
432
433 if (request_firmware(&fw, "icom_asc.bin", &dev->dev) < 0) {
434 dev_err(&dev->dev,"Unable to load icom_asc.bin firmware image\n");
435 status = -1;
436 goto load_code_exit;
437 }
438
439 if (fw->size > ICOM_DCE_IRAM_OFFSET) {
440 dev_err(&dev->dev, "Invalid firmware image for icom_asc.bin found.\n");
441 release_firmware(fw);
442 status = -1;
443 goto load_code_exit;
444 }
445
446 for (index = 0; index < fw->size; index++)
447 new_page[index] = fw->data[index];
448
449 release_firmware(fw);
450
451 writeb((char) ((fw->size + 16)/16), &icom_port->dram->mac_length);
452 writel(temp_pci, &icom_port->dram->mac_load_addr);
453
454 /*Setting the syncReg to 0x80 causes adapter to start downloading
455 the personality code into adapter instruction RAM.
456 Once code is loaded, it will begin executing and, based on
457 information provided above, will start DMAing data from
458 shared memory to adapter DRAM.
459 */
460 /* the wait loop below verifies this write operation has been done
461 and processed
462 */
463 writeb(START_DOWNLOAD, &icom_port->dram->sync);
464
465 /* Wait max 1 Sec for data download and processor to start */
466 for (index = 0; index < 10; index++) {
467 msleep(100);
468 if (readb(&icom_port->dram->misc_flags) & ICOM_HDW_ACTIVE)
469 break;
470 }
471
472 if (index == 10)
473 status = -1;
474
475 /*
476 * check Cable ID
477 */
478 cable_id = readb(&icom_port->dram->cable_id);
479
480 if (cable_id & ICOM_CABLE_ID_VALID) {
481 /* Get cable ID into the lower 4 bits (standard form) */
482 cable_id = (cable_id & ICOM_CABLE_ID_MASK) >> 4;
483 icom_port->cable_id = cable_id;
484 } else {
485 dev_err(&dev->dev,"Invalid or no cable attached\n");
486 icom_port->cable_id = NO_CABLE;
487 }
488
489 load_code_exit:
490
491 if (status != 0) {
492 /* Clear out any pending interrupts */
493 writew(0x3FFF, icom_port->int_reg);
494
495 /* Turn off port */
496 writeb(ICOM_DISABLE, &(icom_port->dram->disable));
497
498 /* Stop processor */
499 stop_processor(icom_port);
500
501 dev_err(&icom_port->adapter->pci_dev->dev,"Port not opertional\n");
502 }
503
504 if (new_page != NULL)
505 pci_free_consistent(dev, 4096, new_page, temp_pci);
506}
507
508static int startup(struct icom_port *icom_port)
509{
510 unsigned long temp;
511 unsigned char cable_id, raw_cable_id;
512 unsigned long flags;
513 int port;
514
515 trace(icom_port, "STARTUP", 0);
516
517 if (!icom_port->dram) {
518 /* should NEVER be NULL */
519 dev_err(&icom_port->adapter->pci_dev->dev,
520 "Unusable Port, port configuration missing\n");
521 return -ENODEV;
522 }
523
524 /*
525 * check Cable ID
526 */
527 raw_cable_id = readb(&icom_port->dram->cable_id);
528 trace(icom_port, "CABLE_ID", raw_cable_id);
529
530 /* Get cable ID into the lower 4 bits (standard form) */
531 cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4;
532
533 /* Check for valid Cable ID */
534 if (!(raw_cable_id & ICOM_CABLE_ID_VALID) ||
535 (cable_id != icom_port->cable_id)) {
536
537 /* reload adapter code, pick up any potential changes in cable id */
538 load_code(icom_port);
539
540 /* still no sign of cable, error out */
541 raw_cable_id = readb(&icom_port->dram->cable_id);
542 cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4;
543 if (!(raw_cable_id & ICOM_CABLE_ID_VALID) ||
544 (icom_port->cable_id == NO_CABLE))
545 return -EIO;
546 }
547
548 /*
549 * Finally, clear and enable interrupts
550 */
551 spin_lock_irqsave(&icom_lock, flags);
552 port = icom_port->port;
553 if (port == 0 || port == 1)
554 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
555 else
556 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2;
557
558 if (port == 0 || port == 2)
559 writew(0x00FF, icom_port->int_reg);
560 else
561 writew(0x3F00, icom_port->int_reg);
562 if (port < 4) {
563 temp = readl(int_mask_tbl[port].global_int_mask);
564 writel(temp & ~int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
565
566 /* write flush */
567 readl(int_mask_tbl[port].global_int_mask);
568 } else {
569 dev_err(&icom_port->adapter->pci_dev->dev,
570 "Invalid port assignment\n");
571 }
572
573 spin_unlock_irqrestore(&icom_lock, flags);
574 return 0;
575}
576
577static void shutdown(struct icom_port *icom_port)
578{
579 unsigned long temp;
580 unsigned char cmdReg;
581 unsigned long flags;
582 int port;
583
584 spin_lock_irqsave(&icom_lock, flags);
585 trace(icom_port, "SHUTDOWN", 0);
586
587 /*
588 * disable all interrupts
589 */
590 port = icom_port->port;
591 if (port == 0 || port == 1)
592 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
593 else
594 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2;
595
596 if (port < 4) {
597 temp = readl(int_mask_tbl[port].global_int_mask);
598 writel(temp | int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
599
600 /* write flush */
601 readl(int_mask_tbl[port].global_int_mask);
602 } else {
603 dev_err(&icom_port->adapter->pci_dev->dev,
604 "Invalid port assignment\n");
605 }
606 spin_unlock_irqrestore(&icom_lock, flags);
607
608 /*
609 * disable break condition
610 */
611 cmdReg = readb(&icom_port->dram->CmdReg);
612 if ((cmdReg | CMD_SND_BREAK) == CMD_SND_BREAK) {
613 writeb(cmdReg & ~CMD_SND_BREAK, &icom_port->dram->CmdReg);
614 }
615}
616
617static int icom_write(struct uart_port *port)
618{
619 unsigned long data_count;
620 unsigned char cmdReg;
621 unsigned long offset;
622 int temp_tail = port->info->xmit.tail;
623
624 trace(ICOM_PORT, "WRITE", 0);
625
626 if (cpu_to_le16(ICOM_PORT->statStg->xmit[0].flags) &
627 SA_FLAGS_READY_TO_XMIT) {
628 trace(ICOM_PORT, "WRITE_FULL", 0);
629 return 0;
630 }
631
632 data_count = 0;
633 while ((port->info->xmit.head != temp_tail) &&
634 (data_count <= XMIT_BUFF_SZ)) {
635
636 ICOM_PORT->xmit_buf[data_count++] =
637 port->info->xmit.buf[temp_tail];
638
639 temp_tail++;
640 temp_tail &= (UART_XMIT_SIZE - 1);
641 }
642
643 if (data_count) {
644 ICOM_PORT->statStg->xmit[0].flags =
645 cpu_to_le16(SA_FLAGS_READY_TO_XMIT);
646 ICOM_PORT->statStg->xmit[0].leLength =
647 cpu_to_le16(data_count);
648 offset =
649 (unsigned long) &ICOM_PORT->statStg->xmit[0] -
650 (unsigned long) ICOM_PORT->statStg;
651 *ICOM_PORT->xmitRestart =
652 cpu_to_le32(ICOM_PORT->statStg_pci + offset);
653 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
654 writeb(cmdReg | CMD_XMIT_RCV_ENABLE,
655 &ICOM_PORT->dram->CmdReg);
656 writeb(START_XMIT, &ICOM_PORT->dram->StartXmitCmd);
657 trace(ICOM_PORT, "WRITE_START", data_count);
658 /* write flush */
659 readb(&ICOM_PORT->dram->StartXmitCmd);
660 }
661
662 return data_count;
663}
664
665static inline void check_modem_status(struct icom_port *icom_port)
666{
667 static char old_status = 0;
668 char delta_status;
669 unsigned char status;
670
671 spin_lock(&icom_port->uart_port.lock);
672
673 /*modem input register */
674 status = readb(&icom_port->dram->isr);
675 trace(icom_port, "CHECK_MODEM", status);
676 delta_status = status ^ old_status;
677 if (delta_status) {
678 if (delta_status & ICOM_RI)
679 icom_port->uart_port.icount.rng++;
680 if (delta_status & ICOM_DSR)
681 icom_port->uart_port.icount.dsr++;
682 if (delta_status & ICOM_DCD)
683 uart_handle_dcd_change(&icom_port->uart_port,
684 delta_status & ICOM_DCD);
685 if (delta_status & ICOM_CTS)
686 uart_handle_cts_change(&icom_port->uart_port,
687 delta_status & ICOM_CTS);
688
689 wake_up_interruptible(&icom_port->uart_port.info->
690 delta_msr_wait);
691 old_status = status;
692 }
693 spin_unlock(&icom_port->uart_port.lock);
694}
695
696static void xmit_interrupt(u16 port_int_reg, struct icom_port *icom_port)
697{
698 unsigned short int count;
699 int i;
700
701 if (port_int_reg & (INT_XMIT_COMPLETED)) {
702 trace(icom_port, "XMIT_COMPLETE", 0);
703
704 /* clear buffer in use bit */
705 icom_port->statStg->xmit[0].flags &=
706 cpu_to_le16(~SA_FLAGS_READY_TO_XMIT);
707
708 count = (unsigned short int)
709 cpu_to_le16(icom_port->statStg->xmit[0].leLength);
710 icom_port->uart_port.icount.tx += count;
711
712 for (i=0; i<count &&
713 !uart_circ_empty(&icom_port->uart_port.info->xmit); i++) {
714
715 icom_port->uart_port.info->xmit.tail++;
716 icom_port->uart_port.info->xmit.tail &=
717 (UART_XMIT_SIZE - 1);
718 }
719
720 if (!icom_write(&icom_port->uart_port))
721 /* activate write queue */
722 uart_write_wakeup(&icom_port->uart_port);
723 } else
724 trace(icom_port, "XMIT_DISABLED", 0);
725}
726
727static void recv_interrupt(u16 port_int_reg, struct icom_port *icom_port)
728{
729 short int count, rcv_buff;
730 struct tty_struct *tty = icom_port->uart_port.info->tty;
731 unsigned short int status;
732 struct uart_icount *icount;
733 unsigned long offset;
734
735 trace(icom_port, "RCV_COMPLETE", 0);
736 rcv_buff = icom_port->next_rcv;
737
738 status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags);
739 while (status & SA_FL_RCV_DONE) {
740
741 trace(icom_port, "FID_STATUS", status);
742 count = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].leLength);
743
744 trace(icom_port, "RCV_COUNT", count);
745 if (count > (TTY_FLIPBUF_SIZE - tty->flip.count))
746 count = TTY_FLIPBUF_SIZE - tty->flip.count;
747
748 trace(icom_port, "REAL_COUNT", count);
749
750 offset =
751 cpu_to_le32(icom_port->statStg->rcv[rcv_buff].leBuffer) -
752 icom_port->recv_buf_pci;
753
754 memcpy(tty->flip.char_buf_ptr,(unsigned char *)
755 ((unsigned long)icom_port->recv_buf + offset), count);
756
757 if (count > 0) {
758 tty->flip.count += count - 1;
759 tty->flip.char_buf_ptr += count - 1;
760
761 memset(tty->flip.flag_buf_ptr, 0, count);
762 tty->flip.flag_buf_ptr += count - 1;
763 }
764
765 icount = &icom_port->uart_port.icount;
766 icount->rx += count;
767
768 /* Break detect logic */
769 if ((status & SA_FLAGS_FRAME_ERROR)
770 && (tty->flip.char_buf_ptr[0] == 0x00)) {
771 status &= ~SA_FLAGS_FRAME_ERROR;
772 status |= SA_FLAGS_BREAK_DET;
773 trace(icom_port, "BREAK_DET", 0);
774 }
775
776 if (status &
777 (SA_FLAGS_BREAK_DET | SA_FLAGS_PARITY_ERROR |
778 SA_FLAGS_FRAME_ERROR | SA_FLAGS_OVERRUN)) {
779
780 if (status & SA_FLAGS_BREAK_DET)
781 icount->brk++;
782 if (status & SA_FLAGS_PARITY_ERROR)
783 icount->parity++;
784 if (status & SA_FLAGS_FRAME_ERROR)
785 icount->frame++;
786 if (status & SA_FLAGS_OVERRUN)
787 icount->overrun++;
788
789 /*
790 * Now check to see if character should be
791 * ignored, and mask off conditions which
792 * should be ignored.
793 */
794 if (status & icom_port->ignore_status_mask) {
795 trace(icom_port, "IGNORE_CHAR", 0);
796 goto ignore_char;
797 }
798
799 status &= icom_port->read_status_mask;
800
801 if (status & SA_FLAGS_BREAK_DET) {
802 *tty->flip.flag_buf_ptr = TTY_BREAK;
803 } else if (status & SA_FLAGS_PARITY_ERROR) {
804 trace(icom_port, "PARITY_ERROR", 0);
805 *tty->flip.flag_buf_ptr = TTY_PARITY;
806 } else if (status & SA_FLAGS_FRAME_ERROR)
807 *tty->flip.flag_buf_ptr = TTY_FRAME;
808
809 if (status & SA_FLAGS_OVERRUN) {
810 /*
811 * Overrun is special, since it's
812 * reported immediately, and doesn't
813 * affect the current character
814 */
815 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
816 tty->flip.count++;
817 tty->flip.flag_buf_ptr++;
818 tty->flip.char_buf_ptr++;
819 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
820 }
821 }
822 }
823
824 tty->flip.flag_buf_ptr++;
825 tty->flip.char_buf_ptr++;
826 tty->flip.count++;
827 ignore_char:
828 icom_port->statStg->rcv[rcv_buff].flags = 0;
829 icom_port->statStg->rcv[rcv_buff].leLength = 0;
830 icom_port->statStg->rcv[rcv_buff].WorkingLength =
831 (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
832
833 rcv_buff++;
834 if (rcv_buff == NUM_RBUFFS)
835 rcv_buff = 0;
836
837 status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags);
838 }
839 icom_port->next_rcv = rcv_buff;
840 tty_flip_buffer_push(tty);
841}
842
843static void process_interrupt(u16 port_int_reg,
844 struct icom_port *icom_port)
845{
846
847 spin_lock(&icom_port->uart_port.lock);
848 trace(icom_port, "INTERRUPT", port_int_reg);
849
850 if (port_int_reg & (INT_XMIT_COMPLETED | INT_XMIT_DISABLED))
851 xmit_interrupt(port_int_reg, icom_port);
852
853 if (port_int_reg & INT_RCV_COMPLETED)
854 recv_interrupt(port_int_reg, icom_port);
855
856 spin_unlock(&icom_port->uart_port.lock);
857}
858
859static irqreturn_t icom_interrupt(int irq, void *dev_id,
860 struct pt_regs *regs)
861{
862 void __iomem * int_reg;
863 u32 adapter_interrupts;
864 u16 port_int_reg;
865 struct icom_adapter *icom_adapter;
866 struct icom_port *icom_port;
867
868 /* find icom_port for this interrupt */
869 icom_adapter = (struct icom_adapter *) dev_id;
870
871 if ((icom_adapter->version | ADAPTER_V2) == ADAPTER_V2) {
872 int_reg = icom_adapter->base_addr + 0x8024;
873
874 adapter_interrupts = readl(int_reg);
875
876 if (adapter_interrupts & 0x00003FFF) {
877 /* port 2 interrupt, NOTE: for all ADAPTER_V2, port 2 will be active */
878 icom_port = &icom_adapter->port_info[2];
879 port_int_reg = (u16) adapter_interrupts;
880 process_interrupt(port_int_reg, icom_port);
881 check_modem_status(icom_port);
882 }
883 if (adapter_interrupts & 0x3FFF0000) {
884 /* port 3 interrupt */
885 icom_port = &icom_adapter->port_info[3];
886 if (icom_port->status == ICOM_PORT_ACTIVE) {
887 port_int_reg =
888 (u16) (adapter_interrupts >> 16);
889 process_interrupt(port_int_reg, icom_port);
890 check_modem_status(icom_port);
891 }
892 }
893
894 /* Clear out any pending interrupts */
895 writel(adapter_interrupts, int_reg);
896
897 int_reg = icom_adapter->base_addr + 0x8004;
898 } else {
899 int_reg = icom_adapter->base_addr + 0x4004;
900 }
901
902 adapter_interrupts = readl(int_reg);
903
904 if (adapter_interrupts & 0x00003FFF) {
905 /* port 0 interrupt, NOTE: for all adapters, port 0 will be active */
906 icom_port = &icom_adapter->port_info[0];
907 port_int_reg = (u16) adapter_interrupts;
908 process_interrupt(port_int_reg, icom_port);
909 check_modem_status(icom_port);
910 }
911 if (adapter_interrupts & 0x3FFF0000) {
912 /* port 1 interrupt */
913 icom_port = &icom_adapter->port_info[1];
914 if (icom_port->status == ICOM_PORT_ACTIVE) {
915 port_int_reg = (u16) (adapter_interrupts >> 16);
916 process_interrupt(port_int_reg, icom_port);
917 check_modem_status(icom_port);
918 }
919 }
920
921 /* Clear out any pending interrupts */
922 writel(adapter_interrupts, int_reg);
923
924 /* flush the write */
925 adapter_interrupts = readl(int_reg);
926
927 return IRQ_HANDLED;
928}
929
930/*
931 * ------------------------------------------------------------------
932 * Begin serial-core API
933 * ------------------------------------------------------------------
934 */
935static unsigned int icom_tx_empty(struct uart_port *port)
936{
937 int ret;
938 unsigned long flags;
939
940 spin_lock_irqsave(&port->lock, flags);
941 if (cpu_to_le16(ICOM_PORT->statStg->xmit[0].flags) &
942 SA_FLAGS_READY_TO_XMIT)
943 ret = TIOCSER_TEMT;
944 else
945 ret = 0;
946
947 spin_unlock_irqrestore(&port->lock, flags);
948 return ret;
949}
950
951static void icom_set_mctrl(struct uart_port *port, unsigned int mctrl)
952{
953 unsigned char local_osr;
954
955 trace(ICOM_PORT, "SET_MODEM", 0);
956 local_osr = readb(&ICOM_PORT->dram->osr);
957
958 if (mctrl & TIOCM_RTS) {
959 trace(ICOM_PORT, "RAISE_RTS", 0);
960 local_osr |= ICOM_RTS;
961 } else {
962 trace(ICOM_PORT, "LOWER_RTS", 0);
963 local_osr &= ~ICOM_RTS;
964 }
965
966 if (mctrl & TIOCM_DTR) {
967 trace(ICOM_PORT, "RAISE_DTR", 0);
968 local_osr |= ICOM_DTR;
969 } else {
970 trace(ICOM_PORT, "LOWER_DTR", 0);
971 local_osr &= ~ICOM_DTR;
972 }
973
974 writeb(local_osr, &ICOM_PORT->dram->osr);
975}
976
977static unsigned int icom_get_mctrl(struct uart_port *port)
978{
979 unsigned char status;
980 unsigned int result;
981
982 trace(ICOM_PORT, "GET_MODEM", 0);
983
984 status = readb(&ICOM_PORT->dram->isr);
985
986 result = ((status & ICOM_DCD) ? TIOCM_CAR : 0)
987 | ((status & ICOM_RI) ? TIOCM_RNG : 0)
988 | ((status & ICOM_DSR) ? TIOCM_DSR : 0)
989 | ((status & ICOM_CTS) ? TIOCM_CTS : 0);
990 return result;
991}
992
993static void icom_stop_tx(struct uart_port *port, unsigned int tty_stop)
994{
995 unsigned char cmdReg;
996
997 if (tty_stop) {
998 trace(ICOM_PORT, "STOP", 0);
999 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1000 writeb(cmdReg | CMD_HOLD_XMIT, &ICOM_PORT->dram->CmdReg);
1001 }
1002}
1003
1004static void icom_start_tx(struct uart_port *port, unsigned int tty_start)
1005{
1006 unsigned char cmdReg;
1007
1008 trace(ICOM_PORT, "START", 0);
1009 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1010 if ((cmdReg & CMD_HOLD_XMIT) == CMD_HOLD_XMIT)
1011 writeb(cmdReg & ~CMD_HOLD_XMIT,
1012 &ICOM_PORT->dram->CmdReg);
1013
1014 icom_write(port);
1015}
1016
1017static void icom_send_xchar(struct uart_port *port, char ch)
1018{
1019 unsigned char xdata;
1020 int index;
1021 unsigned long flags;
1022
1023 trace(ICOM_PORT, "SEND_XCHAR", ch);
1024
1025 /* wait .1 sec to send char */
1026 for (index = 0; index < 10; index++) {
1027 spin_lock_irqsave(&port->lock, flags);
1028 xdata = readb(&ICOM_PORT->dram->xchar);
1029 if (xdata == 0x00) {
1030 trace(ICOM_PORT, "QUICK_WRITE", 0);
1031 writeb(ch, &ICOM_PORT->dram->xchar);
1032
1033 /* flush write operation */
1034 xdata = readb(&ICOM_PORT->dram->xchar);
1035 spin_unlock_irqrestore(&port->lock, flags);
1036 break;
1037 }
1038 spin_unlock_irqrestore(&port->lock, flags);
1039 msleep(10);
1040 }
1041}
1042
1043static void icom_stop_rx(struct uart_port *port)
1044{
1045 unsigned char cmdReg;
1046
1047 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1048 writeb(cmdReg & ~CMD_RCV_ENABLE, &ICOM_PORT->dram->CmdReg);
1049}
1050
1051static void icom_enable_ms(struct uart_port *port)
1052{
1053 /* no-op */
1054}
1055
1056static void icom_break(struct uart_port *port, int break_state)
1057{
1058 unsigned char cmdReg;
1059 unsigned long flags;
1060
1061 spin_lock_irqsave(&port->lock, flags);
1062 trace(ICOM_PORT, "BREAK", 0);
1063 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1064 if (break_state == -1) {
1065 writeb(cmdReg | CMD_SND_BREAK, &ICOM_PORT->dram->CmdReg);
1066 } else {
1067 writeb(cmdReg & ~CMD_SND_BREAK, &ICOM_PORT->dram->CmdReg);
1068 }
1069 spin_unlock_irqrestore(&port->lock, flags);
1070}
1071
1072static int icom_open(struct uart_port *port)
1073{
1074 int retval;
1075
1076 kobject_get(&ICOM_PORT->adapter->kobj);
1077 retval = startup(ICOM_PORT);
1078
1079 if (retval) {
1080 kobject_put(&ICOM_PORT->adapter->kobj);
1081 trace(ICOM_PORT, "STARTUP_ERROR", 0);
1082 return retval;
1083 }
1084
1085 return 0;
1086}
1087
1088static void icom_close(struct uart_port *port)
1089{
1090 unsigned char cmdReg;
1091
1092 trace(ICOM_PORT, "CLOSE", 0);
1093
1094 /* stop receiver */
1095 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1096 writeb(cmdReg & (unsigned char) ~CMD_RCV_ENABLE,
1097 &ICOM_PORT->dram->CmdReg);
1098
1099 shutdown(ICOM_PORT);
1100
1101 kobject_put(&ICOM_PORT->adapter->kobj);
1102}
1103
1104static void icom_set_termios(struct uart_port *port,
1105 struct termios *termios,
1106 struct termios *old_termios)
1107{
1108 int baud;
1109 unsigned cflag, iflag;
1110 int bits;
1111 char new_config2;
1112 char new_config3 = 0;
1113 char tmp_byte;
1114 int index;
1115 int rcv_buff, xmit_buff;
1116 unsigned long offset;
1117 unsigned long flags;
1118
1119 spin_lock_irqsave(&port->lock, flags);
1120 trace(ICOM_PORT, "CHANGE_SPEED", 0);
1121
1122 cflag = termios->c_cflag;
1123 iflag = termios->c_iflag;
1124
1125 new_config2 = ICOM_ACFG_DRIVE1;
1126
1127 /* byte size and parity */
1128 switch (cflag & CSIZE) {
1129 case CS5: /* 5 bits/char */
1130 new_config2 |= ICOM_ACFG_5BPC;
1131 bits = 7;
1132 break;
1133 case CS6: /* 6 bits/char */
1134 new_config2 |= ICOM_ACFG_6BPC;
1135 bits = 8;
1136 break;
1137 case CS7: /* 7 bits/char */
1138 new_config2 |= ICOM_ACFG_7BPC;
1139 bits = 9;
1140 break;
1141 case CS8: /* 8 bits/char */
1142 new_config2 |= ICOM_ACFG_8BPC;
1143 bits = 10;
1144 break;
1145 default:
1146 bits = 10;
1147 break;
1148 }
1149 if (cflag & CSTOPB) {
1150 /* 2 stop bits */
1151 new_config2 |= ICOM_ACFG_2STOP_BIT;
1152 bits++;
1153 }
1154 if (cflag & PARENB) {
1155 /* parity bit enabled */
1156 new_config2 |= ICOM_ACFG_PARITY_ENAB;
1157 trace(ICOM_PORT, "PARENB", 0);
1158 bits++;
1159 }
1160 if (cflag & PARODD) {
1161 /* odd parity */
1162 new_config2 |= ICOM_ACFG_PARITY_ODD;
1163 trace(ICOM_PORT, "PARODD", 0);
1164 }
1165
1166 /* Determine divisor based on baud rate */
1167 baud = uart_get_baud_rate(port, termios, old_termios,
1168 icom_acfg_baud[0],
1169 icom_acfg_baud[BAUD_TABLE_LIMIT]);
1170 if (!baud)
1171 baud = 9600; /* B0 transition handled in rs_set_termios */
1172
1173 for (index = 0; index < BAUD_TABLE_LIMIT; index++) {
1174 if (icom_acfg_baud[index] == baud) {
1175 new_config3 = index;
1176 break;
1177 }
1178 }
1179
1180 uart_update_timeout(port, cflag, baud);
1181
1182 /* CTS flow control flag and modem status interrupts */
1183 tmp_byte = readb(&(ICOM_PORT->dram->HDLCConfigReg));
1184 if (cflag & CRTSCTS)
1185 tmp_byte |= HDLC_HDW_FLOW;
1186 else
1187 tmp_byte &= ~HDLC_HDW_FLOW;
1188 writeb(tmp_byte, &(ICOM_PORT->dram->HDLCConfigReg));
1189
1190 /*
1191 * Set up parity check flag
1192 */
1193 ICOM_PORT->read_status_mask = SA_FLAGS_OVERRUN | SA_FL_RCV_DONE;
1194 if (iflag & INPCK)
1195 ICOM_PORT->read_status_mask |=
1196 SA_FLAGS_FRAME_ERROR | SA_FLAGS_PARITY_ERROR;
1197
1198 if ((iflag & BRKINT) || (iflag & PARMRK))
1199 ICOM_PORT->read_status_mask |= SA_FLAGS_BREAK_DET;
1200
1201 /*
1202 * Characters to ignore
1203 */
1204 ICOM_PORT->ignore_status_mask = 0;
1205 if (iflag & IGNPAR)
1206 ICOM_PORT->ignore_status_mask |=
1207 SA_FLAGS_PARITY_ERROR | SA_FLAGS_FRAME_ERROR;
1208 if (iflag & IGNBRK) {
1209 ICOM_PORT->ignore_status_mask |= SA_FLAGS_BREAK_DET;
1210 /*
1211 * If we're ignore parity and break indicators, ignore
1212 * overruns too. (For real raw support).
1213 */
1214 if (iflag & IGNPAR)
1215 ICOM_PORT->ignore_status_mask |= SA_FLAGS_OVERRUN;
1216 }
1217
1218 /*
1219 * !!! ignore all characters if CREAD is not set
1220 */
1221 if ((cflag & CREAD) == 0)
1222 ICOM_PORT->ignore_status_mask |= SA_FL_RCV_DONE;
1223
1224 /* Turn off Receiver to prepare for reset */
1225 writeb(CMD_RCV_DISABLE, &ICOM_PORT->dram->CmdReg);
1226
1227 for (index = 0; index < 10; index++) {
1228 if (readb(&ICOM_PORT->dram->PrevCmdReg) == 0x00) {
1229 break;
1230 }
1231 }
1232
1233 /* clear all current buffers of data */
1234 for (rcv_buff = 0; rcv_buff < NUM_RBUFFS; rcv_buff++) {
1235 ICOM_PORT->statStg->rcv[rcv_buff].flags = 0;
1236 ICOM_PORT->statStg->rcv[rcv_buff].leLength = 0;
1237 ICOM_PORT->statStg->rcv[rcv_buff].WorkingLength =
1238 (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
1239 }
1240
1241 for (xmit_buff = 0; xmit_buff < NUM_XBUFFS; xmit_buff++) {
1242 ICOM_PORT->statStg->xmit[xmit_buff].flags = 0;
1243 }
1244
1245 /* activate changes and start xmit and receiver here */
1246 /* Enable the receiver */
1247 writeb(new_config3, &(ICOM_PORT->dram->async_config3));
1248 writeb(new_config2, &(ICOM_PORT->dram->async_config2));
1249 tmp_byte = readb(&(ICOM_PORT->dram->HDLCConfigReg));
1250 tmp_byte |= HDLC_PPP_PURE_ASYNC | HDLC_FF_FILL;
1251 writeb(tmp_byte, &(ICOM_PORT->dram->HDLCConfigReg));
1252 writeb(0x04, &(ICOM_PORT->dram->FlagFillIdleTimer)); /* 0.5 seconds */
1253 writeb(0xFF, &(ICOM_PORT->dram->ier)); /* enable modem signal interrupts */
1254
1255 /* reset processor */
1256 writeb(CMD_RESTART, &ICOM_PORT->dram->CmdReg);
1257
1258 for (index = 0; index < 10; index++) {
1259 if (readb(&ICOM_PORT->dram->CmdReg) == 0x00) {
1260 break;
1261 }
1262 }
1263
1264 /* Enable Transmitter and Reciever */
1265 offset =
1266 (unsigned long) &ICOM_PORT->statStg->rcv[0] -
1267 (unsigned long) ICOM_PORT->statStg;
1268 writel(ICOM_PORT->statStg_pci + offset,
1269 &ICOM_PORT->dram->RcvStatusAddr);
1270 ICOM_PORT->next_rcv = 0;
1271 ICOM_PORT->put_length = 0;
1272 *ICOM_PORT->xmitRestart = 0;
1273 writel(ICOM_PORT->xmitRestart_pci,
1274 &ICOM_PORT->dram->XmitStatusAddr);
1275 trace(ICOM_PORT, "XR_ENAB", 0);
1276 writeb(CMD_XMIT_RCV_ENABLE, &ICOM_PORT->dram->CmdReg);
1277
1278 spin_unlock_irqrestore(&port->lock, flags);
1279}
1280
1281static const char *icom_type(struct uart_port *port)
1282{
1283 return "icom";
1284}
1285
1286static void icom_release_port(struct uart_port *port)
1287{
1288}
1289
1290static int icom_request_port(struct uart_port *port)
1291{
1292 return 0;
1293}
1294
1295static void icom_config_port(struct uart_port *port, int flags)
1296{
1297 port->type = PORT_ICOM;
1298}
1299
1300static struct uart_ops icom_ops = {
1301 .tx_empty = icom_tx_empty,
1302 .set_mctrl = icom_set_mctrl,
1303 .get_mctrl = icom_get_mctrl,
1304 .stop_tx = icom_stop_tx,
1305 .start_tx = icom_start_tx,
1306 .send_xchar = icom_send_xchar,
1307 .stop_rx = icom_stop_rx,
1308 .enable_ms = icom_enable_ms,
1309 .break_ctl = icom_break,
1310 .startup = icom_open,
1311 .shutdown = icom_close,
1312 .set_termios = icom_set_termios,
1313 .type = icom_type,
1314 .release_port = icom_release_port,
1315 .request_port = icom_request_port,
1316 .config_port = icom_config_port,
1317};
1318
1319#define ICOM_CONSOLE NULL
1320
1321static struct uart_driver icom_uart_driver = {
1322 .owner = THIS_MODULE,
1323 .driver_name = ICOM_DRIVER_NAME,
1324 .dev_name = "ttyA",
1325 .major = ICOM_MAJOR,
1326 .minor = ICOM_MINOR_START,
1327 .nr = NR_PORTS,
1328 .cons = ICOM_CONSOLE,
1329};
1330
1331static int __devinit icom_init_ports(struct icom_adapter *icom_adapter)
1332{
1333 u32 subsystem_id = icom_adapter->subsystem_id;
1334 int retval = 0;
1335 int i;
1336 struct icom_port *icom_port;
1337
1338 if (icom_adapter->version == ADAPTER_V1) {
1339 icom_adapter->numb_ports = 2;
1340
1341 for (i = 0; i < 2; i++) {
1342 icom_port = &icom_adapter->port_info[i];
1343 icom_port->port = i;
1344 icom_port->status = ICOM_PORT_ACTIVE;
1345 icom_port->imbed_modem = ICOM_UNKNOWN;
1346 }
1347 } else {
1348 if (subsystem_id == PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL) {
1349 icom_adapter->numb_ports = 4;
1350
1351 for (i = 0; i < 4; i++) {
1352 icom_port = &icom_adapter->port_info[i];
1353
1354 icom_port->port = i;
1355 icom_port->status = ICOM_PORT_ACTIVE;
1356 icom_port->imbed_modem = ICOM_IMBED_MODEM;
1357 }
1358 } else {
1359 icom_adapter->numb_ports = 4;
1360
1361 icom_adapter->port_info[0].port = 0;
1362 icom_adapter->port_info[0].status = ICOM_PORT_ACTIVE;
1363
1364 if (subsystem_id ==
1365 PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM) {
1366 icom_adapter->port_info[0].imbed_modem = ICOM_IMBED_MODEM;
1367 } else {
1368 icom_adapter->port_info[0].imbed_modem = ICOM_RVX;
1369 }
1370
1371 icom_adapter->port_info[1].status = ICOM_PORT_OFF;
1372
1373 icom_adapter->port_info[2].port = 2;
1374 icom_adapter->port_info[2].status = ICOM_PORT_ACTIVE;
1375 icom_adapter->port_info[2].imbed_modem = ICOM_RVX;
1376 icom_adapter->port_info[3].status = ICOM_PORT_OFF;
1377 }
1378 }
1379
1380 return retval;
1381}
1382
1383static void icom_port_active(struct icom_port *icom_port, struct icom_adapter *icom_adapter, int port_num)
1384{
1385 if (icom_adapter->version == ADAPTER_V1) {
1386 icom_port->global_reg = icom_adapter->base_addr + 0x4000;
1387 icom_port->int_reg = icom_adapter->base_addr +
1388 0x4004 + 2 - 2 * port_num;
1389 } else {
1390 icom_port->global_reg = icom_adapter->base_addr + 0x8000;
1391 if (icom_port->port < 2)
1392 icom_port->int_reg = icom_adapter->base_addr +
1393 0x8004 + 2 - 2 * icom_port->port;
1394 else
1395 icom_port->int_reg = icom_adapter->base_addr +
1396 0x8024 + 2 - 2 * (icom_port->port - 2);
1397 }
1398}
1399static int __init icom_load_ports(struct icom_adapter *icom_adapter)
1400{
1401 struct icom_port *icom_port;
1402 int port_num;
1403 int retval;
1404
1405 for (port_num = 0; port_num < icom_adapter->numb_ports; port_num++) {
1406
1407 icom_port = &icom_adapter->port_info[port_num];
1408
1409 if (icom_port->status == ICOM_PORT_ACTIVE) {
1410 icom_port_active(icom_port, icom_adapter, port_num);
1411 icom_port->dram = icom_adapter->base_addr +
1412 0x2000 * icom_port->port;
1413
1414 icom_port->adapter = icom_adapter;
1415
1416 /* get port memory */
1417 if ((retval = get_port_memory(icom_port)) != 0) {
1418 dev_err(&icom_port->adapter->pci_dev->dev,
1419 "Memory allocation for port FAILED\n");
1420 }
1421 }
1422 }
1423 return 0;
1424}
1425
1426static int __devinit icom_alloc_adapter(struct icom_adapter
1427 **icom_adapter_ref)
1428{
1429 int adapter_count = 0;
1430 struct icom_adapter *icom_adapter;
1431 struct icom_adapter *cur_adapter_entry;
1432 struct list_head *tmp;
1433
1434 icom_adapter = (struct icom_adapter *)
1435 kmalloc(sizeof(struct icom_adapter), GFP_KERNEL);
1436
1437 if (!icom_adapter) {
1438 return -ENOMEM;
1439 }
1440
1441 memset(icom_adapter, 0, sizeof(struct icom_adapter));
1442
1443 list_for_each(tmp, &icom_adapter_head) {
1444 cur_adapter_entry =
1445 list_entry(tmp, struct icom_adapter,
1446 icom_adapter_entry);
1447 if (cur_adapter_entry->index != adapter_count) {
1448 break;
1449 }
1450 adapter_count++;
1451 }
1452
1453 icom_adapter->index = adapter_count;
1454 list_add_tail(&icom_adapter->icom_adapter_entry, tmp);
1455
1456 *icom_adapter_ref = icom_adapter;
1457 return 0;
1458}
1459
1460static void icom_free_adapter(struct icom_adapter *icom_adapter)
1461{
1462 list_del(&icom_adapter->icom_adapter_entry);
1463 kfree(icom_adapter);
1464}
1465
1466static void icom_remove_adapter(struct icom_adapter *icom_adapter)
1467{
1468 struct icom_port *icom_port;
1469 int index;
1470
1471 for (index = 0; index < icom_adapter->numb_ports; index++) {
1472 icom_port = &icom_adapter->port_info[index];
1473
1474 if (icom_port->status == ICOM_PORT_ACTIVE) {
1475 dev_info(&icom_adapter->pci_dev->dev,
1476 "Device removed\n");
1477
1478 uart_remove_one_port(&icom_uart_driver,
1479 &icom_port->uart_port);
1480
1481 /* be sure that DTR and RTS are dropped */
1482 writeb(0x00, &icom_port->dram->osr);
1483
1484 /* Wait 0.1 Sec for simple Init to complete */
1485 msleep(100);
1486
1487 /* Stop proccessor */
1488 stop_processor(icom_port);
1489
1490 free_port_memory(icom_port);
1491 }
1492 }
1493
1494 free_irq(icom_adapter->irq_number, (void *) icom_adapter);
1495 iounmap(icom_adapter->base_addr);
1496 icom_free_adapter(icom_adapter);
1497 pci_release_regions(icom_adapter->pci_dev);
1498}
1499
1500static void icom_kobj_release(struct kobject *kobj)
1501{
1502 struct icom_adapter *icom_adapter;
1503
1504 icom_adapter = to_icom_adapter(kobj);
1505 icom_remove_adapter(icom_adapter);
1506}
1507
1508static struct kobj_type icom_kobj_type = {
1509 .release = icom_kobj_release,
1510};
1511
1512static int __devinit icom_probe(struct pci_dev *dev,
1513 const struct pci_device_id *ent)
1514{
1515 int index;
1516 unsigned int command_reg;
1517 int retval;
1518 struct icom_adapter *icom_adapter;
1519 struct icom_port *icom_port;
1520
1521 retval = pci_enable_device(dev);
1522 if (retval) {
1523 dev_err(&dev->dev, "Device enable FAILED\n");
1524 return retval;
1525 }
1526
1527 if ( (retval = pci_request_regions(dev, "icom"))) {
1528 dev_err(&dev->dev, "pci_request_region FAILED\n");
1529 pci_disable_device(dev);
1530 return retval;
1531 }
1532
1533 pci_set_master(dev);
1534
1535 if ( (retval = pci_read_config_dword(dev, PCI_COMMAND, &command_reg))) {
1536 dev_err(&dev->dev, "PCI Config read FAILED\n");
1537 return retval;
1538 }
1539
1540 pci_write_config_dword(dev, PCI_COMMAND,
1541 command_reg | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1542 | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1543
1544 if (ent->driver_data == ADAPTER_V1) {
1545 pci_write_config_dword(dev, 0x44, 0x8300830A);
1546 } else {
1547 pci_write_config_dword(dev, 0x44, 0x42004200);
1548 pci_write_config_dword(dev, 0x48, 0x42004200);
1549 }
1550
1551
1552 retval = icom_alloc_adapter(&icom_adapter);
1553 if (retval) {
1554 dev_err(&dev->dev, "icom_alloc_adapter FAILED\n");
1555 retval = -EIO;
1556 goto probe_exit0;
1557 }
1558
1559 icom_adapter->base_addr_pci = pci_resource_start(dev, 0);
1560 icom_adapter->irq_number = dev->irq;
1561 icom_adapter->pci_dev = dev;
1562 icom_adapter->version = ent->driver_data;
1563 icom_adapter->subsystem_id = ent->subdevice;
1564
1565
1566 retval = icom_init_ports(icom_adapter);
1567 if (retval) {
1568 dev_err(&dev->dev, "Port configuration failed\n");
1569 goto probe_exit1;
1570 }
1571
1572 icom_adapter->base_addr = ioremap(icom_adapter->base_addr_pci,
1573 pci_resource_len(dev, 0));
1574
1575 if (!icom_adapter->base_addr)
1576 goto probe_exit1;
1577
1578 /* save off irq and request irq line */
1579 if ( (retval = request_irq(dev->irq, icom_interrupt,
1580 SA_INTERRUPT | SA_SHIRQ, ICOM_DRIVER_NAME,
1581 (void *) icom_adapter))) {
1582 goto probe_exit2;
1583 }
1584
1585 retval = icom_load_ports(icom_adapter);
1586
1587 for (index = 0; index < icom_adapter->numb_ports; index++) {
1588 icom_port = &icom_adapter->port_info[index];
1589
1590 if (icom_port->status == ICOM_PORT_ACTIVE) {
1591 icom_port->uart_port.irq = icom_port->adapter->irq_number;
1592 icom_port->uart_port.type = PORT_ICOM;
1593 icom_port->uart_port.iotype = UPIO_MEM;
1594 icom_port->uart_port.membase =
1595 (char *) icom_adapter->base_addr_pci;
1596 icom_port->uart_port.fifosize = 16;
1597 icom_port->uart_port.ops = &icom_ops;
1598 icom_port->uart_port.line =
1599 icom_port->port + icom_adapter->index * 4;
1600 if (uart_add_one_port (&icom_uart_driver, &icom_port->uart_port)) {
1601 icom_port->status = ICOM_PORT_OFF;
1602 dev_err(&dev->dev, "Device add failed\n");
1603 } else
1604 dev_info(&dev->dev, "Device added\n");
1605 }
1606 }
1607
1608 kobject_init(&icom_adapter->kobj);
1609 icom_adapter->kobj.ktype = &icom_kobj_type;
1610 return 0;
1611
1612probe_exit2:
1613 iounmap(icom_adapter->base_addr);
1614probe_exit1:
1615 icom_free_adapter(icom_adapter);
1616
1617probe_exit0:
1618 pci_release_regions(dev);
1619 pci_disable_device(dev);
1620
1621 return retval;
1622
1623
1624}
1625
1626static void __devexit icom_remove(struct pci_dev *dev)
1627{
1628 struct icom_adapter *icom_adapter;
1629 struct list_head *tmp;
1630
1631 list_for_each(tmp, &icom_adapter_head) {
1632 icom_adapter = list_entry(tmp, struct icom_adapter,
1633 icom_adapter_entry);
1634 if (icom_adapter->pci_dev == dev) {
1635 kobject_put(&icom_adapter->kobj);
1636 return;
1637 }
1638 }
1639
1640 dev_err(&dev->dev, "Unable to find device to remove\n");
1641}
1642
1643static struct pci_driver icom_pci_driver = {
1644 .name = ICOM_DRIVER_NAME,
1645 .id_table = icom_pci_table,
1646 .probe = icom_probe,
1647 .remove = __devexit_p(icom_remove),
1648};
1649
1650static int __init icom_init(void)
1651{
1652 int ret;
1653
1654 spin_lock_init(&icom_lock);
1655
1656 ret = uart_register_driver(&icom_uart_driver);
1657 if (ret)
1658 return ret;
1659
1660 ret = pci_register_driver(&icom_pci_driver);
1661
1662 if (ret < 0)
1663 uart_unregister_driver(&icom_uart_driver);
1664
1665 return ret;
1666}
1667
1668static void __exit icom_exit(void)
1669{
1670 pci_unregister_driver(&icom_pci_driver);
1671 uart_unregister_driver(&icom_uart_driver);
1672}
1673
1674module_init(icom_init);
1675module_exit(icom_exit);
1676
1677#ifdef ICOM_TRACE
1678static inline void trace(struct icom_port *icom_port, char *trace_pt,
1679 unsigned long trace_data)
1680{
1681 dev_info(&icom_port->adapter->pci_dev->dev, ":%d:%s - %lx\n",
1682 icom_port->port, trace_pt, trace_data);
1683}
1684#endif
1685
1686MODULE_AUTHOR("Michael Anderson <mjanders@us.ibm.com>");
1687MODULE_DESCRIPTION("IBM iSeries Serial IOA driver");
1688MODULE_SUPPORTED_DEVICE
1689 ("IBM iSeries 2745, 2771, 2772, 2742, 2793 and 2805 Communications adapters");
1690MODULE_LICENSE("GPL");
1691
diff --git a/drivers/serial/icom.h b/drivers/serial/icom.h
new file mode 100644
index 000000000000..23dc0f7ddf8b
--- /dev/null
+++ b/drivers/serial/icom.h
@@ -0,0 +1,290 @@
1/*
2 * icom.h
3 *
4 * Copyright (C) 2001 Michael Anderson, IBM Corporation
5 *
6 * Serial device driver include file.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include<linux/serial_core.h>
24
25#define BAUD_TABLE_LIMIT ((sizeof(icom_acfg_baud)/sizeof(int)) - 1)
26static int icom_acfg_baud[] = {
27 300,
28 600,
29 900,
30 1200,
31 1800,
32 2400,
33 3600,
34 4800,
35 7200,
36 9600,
37 14400,
38 19200,
39 28800,
40 38400,
41 57600,
42 76800,
43 115200,
44 153600,
45 230400,
46 307200,
47 460800,
48};
49
50struct icom_regs {
51 u32 control; /* Adapter Control Register */
52 u32 interrupt; /* Adapter Interrupt Register */
53 u32 int_mask; /* Adapter Interrupt Mask Reg */
54 u32 int_pri; /* Adapter Interrupt Priority r */
55 u32 int_reg_b; /* Adapter non-masked Interrupt */
56 u32 resvd01;
57 u32 resvd02;
58 u32 resvd03;
59 u32 control_2; /* Adapter Control Register 2 */
60 u32 interrupt_2; /* Adapter Interrupt Register 2 */
61 u32 int_mask_2; /* Adapter Interrupt Mask 2 */
62 u32 int_pri_2; /* Adapter Interrupt Prior 2 */
63 u32 int_reg_2b; /* Adapter non-masked 2 */
64};
65
66struct func_dram {
67 u32 reserved[108]; /* 0-1B0 reserved by personality code */
68 u32 RcvStatusAddr; /* 1B0-1B3 Status Address for Next rcv */
69 u8 RcvStnAddr; /* 1B4 Receive Station Addr */
70 u8 IdleState; /* 1B5 Idle State */
71 u8 IdleMonitor; /* 1B6 Idle Monitor */
72 u8 FlagFillIdleTimer; /* 1B7 Flag Fill Idle Timer */
73 u32 XmitStatusAddr; /* 1B8-1BB Transmit Status Address */
74 u8 StartXmitCmd; /* 1BC Start Xmit Command */
75 u8 HDLCConfigReg; /* 1BD Reserved */
76 u8 CauseCode; /* 1BE Cause code for fatal error */
77 u8 xchar; /* 1BF High priority send */
78 u32 reserved3; /* 1C0-1C3 Reserved */
79 u8 PrevCmdReg; /* 1C4 Reserved */
80 u8 CmdReg; /* 1C5 Command Register */
81 u8 async_config2; /* 1C6 Async Config Byte 2 */
82 u8 async_config3; /* 1C7 Async Config Byte 3 */
83 u8 dce_resvd[20]; /* 1C8-1DB DCE Rsvd */
84 u8 dce_resvd21; /* 1DC DCE Rsvd (21st byte */
85 u8 misc_flags; /* 1DD misc flags */
86#define V2_HARDWARE 0x40
87#define ICOM_HDW_ACTIVE 0x01
88 u8 call_length; /* 1DE Phone #/CFI buff ln */
89 u8 call_length2; /* 1DF Upper byte (unused) */
90 u32 call_addr; /* 1E0-1E3 Phn #/CFI buff addr */
91 u16 timer_value; /* 1E4-1E5 general timer value */
92 u8 timer_command; /* 1E6 general timer cmd */
93 u8 dce_command; /* 1E7 dce command reg */
94 u8 dce_cmd_status; /* 1E8 dce command stat */
95 u8 x21_r1_ioff; /* 1E9 dce ready counter */
96 u8 x21_r0_ioff; /* 1EA dce not ready ctr */
97 u8 x21_ralt_ioff; /* 1EB dce CNR counter */
98 u8 x21_r1_ion; /* 1EC dce ready I on ctr */
99 u8 rsvd_ier; /* 1ED Rsvd for IER (if ne */
100 u8 ier; /* 1EE Interrupt Enable */
101 u8 isr; /* 1EF Input Signal Reg */
102 u8 osr; /* 1F0 Output Signal Reg */
103 u8 reset; /* 1F1 Reset/Reload Reg */
104 u8 disable; /* 1F2 Disable Reg */
105 u8 sync; /* 1F3 Sync Reg */
106 u8 error_stat; /* 1F4 Error Status */
107 u8 cable_id; /* 1F5 Cable ID */
108 u8 cs_length; /* 1F6 CS Load Length */
109 u8 mac_length; /* 1F7 Mac Load Length */
110 u32 cs_load_addr; /* 1F8-1FB Call Load PCI Addr */
111 u32 mac_load_addr; /* 1FC-1FF Mac Load PCI Addr */
112};
113
114/*
115 * adapter defines and structures
116 */
117#define ICOM_CONTROL_START_A 0x00000008
118#define ICOM_CONTROL_STOP_A 0x00000004
119#define ICOM_CONTROL_START_B 0x00000002
120#define ICOM_CONTROL_STOP_B 0x00000001
121#define ICOM_CONTROL_START_C 0x00000008
122#define ICOM_CONTROL_STOP_C 0x00000004
123#define ICOM_CONTROL_START_D 0x00000002
124#define ICOM_CONTROL_STOP_D 0x00000001
125#define ICOM_IRAM_OFFSET 0x1000
126#define ICOM_IRAM_SIZE 0x0C00
127#define ICOM_DCE_IRAM_OFFSET 0x0A00
128#define ICOM_CABLE_ID_VALID 0x01
129#define ICOM_CABLE_ID_MASK 0xF0
130#define ICOM_DISABLE 0x80
131#define CMD_XMIT_RCV_ENABLE 0xC0
132#define CMD_XMIT_ENABLE 0x40
133#define CMD_RCV_DISABLE 0x00
134#define CMD_RCV_ENABLE 0x80
135#define CMD_RESTART 0x01
136#define CMD_HOLD_XMIT 0x02
137#define CMD_SND_BREAK 0x04
138#define RS232_CABLE 0x06
139#define V24_CABLE 0x0E
140#define V35_CABLE 0x0C
141#define V36_CABLE 0x02
142#define NO_CABLE 0x00
143#define START_DOWNLOAD 0x80
144#define ICOM_INT_MASK_PRC_A 0x00003FFF
145#define ICOM_INT_MASK_PRC_B 0x3FFF0000
146#define ICOM_INT_MASK_PRC_C 0x00003FFF
147#define ICOM_INT_MASK_PRC_D 0x3FFF0000
148#define INT_RCV_COMPLETED 0x1000
149#define INT_XMIT_COMPLETED 0x2000
150#define INT_IDLE_DETECT 0x0800
151#define INT_RCV_DISABLED 0x0400
152#define INT_XMIT_DISABLED 0x0200
153#define INT_RCV_XMIT_SHUTDOWN 0x0100
154#define INT_FATAL_ERROR 0x0080
155#define INT_CABLE_PULL 0x0020
156#define INT_SIGNAL_CHANGE 0x0010
157#define HDLC_PPP_PURE_ASYNC 0x02
158#define HDLC_FF_FILL 0x00
159#define HDLC_HDW_FLOW 0x01
160#define START_XMIT 0x80
161#define ICOM_ACFG_DRIVE1 0x20
162#define ICOM_ACFG_NO_PARITY 0x00
163#define ICOM_ACFG_PARITY_ENAB 0x02
164#define ICOM_ACFG_PARITY_ODD 0x01
165#define ICOM_ACFG_8BPC 0x00
166#define ICOM_ACFG_7BPC 0x04
167#define ICOM_ACFG_6BPC 0x08
168#define ICOM_ACFG_5BPC 0x0C
169#define ICOM_ACFG_1STOP_BIT 0x00
170#define ICOM_ACFG_2STOP_BIT 0x10
171#define ICOM_DTR 0x80
172#define ICOM_RTS 0x40
173#define ICOM_RI 0x08
174#define ICOM_DSR 0x80
175#define ICOM_DCD 0x20
176#define ICOM_CTS 0x40
177
178#define NUM_XBUFFS 1
179#define NUM_RBUFFS 2
180#define RCV_BUFF_SZ 0x0200
181#define XMIT_BUFF_SZ 0x1000
182struct statusArea {
183 /**********************************************/
184 /* Transmit Status Area */
185 /**********************************************/
186 struct xmit_status_area{
187 u32 leNext; /* Next entry in Little Endian on Adapter */
188 u32 leNextASD;
189 u32 leBuffer; /* Buffer for entry in LE for Adapter */
190 u16 leLengthASD;
191 u16 leOffsetASD;
192 u16 leLength; /* Length of data in segment */
193 u16 flags;
194#define SA_FLAGS_DONE 0x0080 /* Done with Segment */
195#define SA_FLAGS_CONTINUED 0x8000 /* More Segments */
196#define SA_FLAGS_IDLE 0x4000 /* Mark IDLE after frm */
197#define SA_FLAGS_READY_TO_XMIT 0x0800
198#define SA_FLAGS_STAT_MASK 0x007F
199 } xmit[NUM_XBUFFS];
200
201 /**********************************************/
202 /* Receive Status Area */
203 /**********************************************/
204 struct {
205 u32 leNext; /* Next entry in Little Endian on Adapter */
206 u32 leNextASD;
207 u32 leBuffer; /* Buffer for entry in LE for Adapter */
208 u16 WorkingLength; /* size of segment */
209 u16 reserv01;
210 u16 leLength; /* Length of data in segment */
211 u16 flags;
212#define SA_FL_RCV_DONE 0x0010 /* Data ready */
213#define SA_FLAGS_OVERRUN 0x0040
214#define SA_FLAGS_PARITY_ERROR 0x0080
215#define SA_FLAGS_FRAME_ERROR 0x0001
216#define SA_FLAGS_FRAME_TRUNC 0x0002
217#define SA_FLAGS_BREAK_DET 0x0004 /* set conditionally by device driver, not hardware */
218#define SA_FLAGS_RCV_MASK 0xFFE6
219 } rcv[NUM_RBUFFS];
220};
221
222struct icom_adapter;
223
224
225#define ICOM_MAJOR 243
226#define ICOM_MINOR_START 0
227
228struct icom_port {
229 struct uart_port uart_port;
230 u8 imbed_modem;
231#define ICOM_UNKNOWN 1
232#define ICOM_RVX 2
233#define ICOM_IMBED_MODEM 3
234 unsigned char cable_id;
235 unsigned char read_status_mask;
236 unsigned char ignore_status_mask;
237 void __iomem * int_reg;
238 struct icom_regs __iomem *global_reg;
239 struct func_dram __iomem *dram;
240 int port;
241 struct statusArea *statStg;
242 dma_addr_t statStg_pci;
243 u32 *xmitRestart;
244 dma_addr_t xmitRestart_pci;
245 unsigned char *xmit_buf;
246 dma_addr_t xmit_buf_pci;
247 unsigned char *recv_buf;
248 dma_addr_t recv_buf_pci;
249 int next_rcv;
250 int put_length;
251 int status;
252#define ICOM_PORT_ACTIVE 1 /* Port exists. */
253#define ICOM_PORT_OFF 0 /* Port does not exist. */
254 int load_in_progress;
255 struct icom_adapter *adapter;
256};
257
258struct icom_adapter {
259 void __iomem * base_addr;
260 unsigned long base_addr_pci;
261 unsigned char irq_number;
262 struct pci_dev *pci_dev;
263 struct icom_port port_info[4];
264 int index;
265 int version;
266#define ADAPTER_V1 0x0001
267#define ADAPTER_V2 0x0002
268 u32 subsystem_id;
269#define FOUR_PORT_MODEL 0x0252
270#define V2_TWO_PORTS_RVX 0x021A
271#define V2_ONE_PORT_RVX_ONE_PORT_IMBED_MDM 0x0251
272 int numb_ports;
273 struct list_head icom_adapter_entry;
274 struct kobject kobj;
275};
276
277/* prototype */
278extern void iCom_sercons_init(void);
279
280struct lookup_proc_table {
281 u32 __iomem *global_control_reg;
282 unsigned long processor_id;
283};
284
285struct lookup_int_table {
286 u32 __iomem *global_int_mask;
287 unsigned long processor_id;
288};
289
290#define MSECS_TO_JIFFIES(ms) (((ms)*HZ+999)/1000)
diff --git a/drivers/serial/imx.c b/drivers/serial/imx.c
new file mode 100644
index 000000000000..d273134f9e6c
--- /dev/null
+++ b/drivers/serial/imx.c
@@ -0,0 +1,909 @@
1/*
2 * linux/drivers/serial/imx.c
3 *
4 * Driver for Motorola IMX serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Author: Sascha Hauer <sascha@saschahauer.de>
9 * Copyright (C) 2004 Pengutronix
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * [29-Mar-2005] Mike Lee
26 * Added hardware handshake
27 */
28#include <linux/config.h>
29
30#if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31#define SUPPORT_SYSRQ
32#endif
33
34#include <linux/module.h>
35#include <linux/ioport.h>
36#include <linux/init.h>
37#include <linux/console.h>
38#include <linux/sysrq.h>
39#include <linux/device.h>
40#include <linux/tty.h>
41#include <linux/tty_flip.h>
42#include <linux/serial_core.h>
43#include <linux/serial.h>
44
45#include <asm/io.h>
46#include <asm/irq.h>
47#include <asm/hardware.h>
48
49/* We've been assigned a range on the "Low-density serial ports" major */
50#define SERIAL_IMX_MAJOR 204
51#define MINOR_START 41
52
53#define NR_PORTS 2
54
55#define IMX_ISR_PASS_LIMIT 256
56
57/*
58 * This is the size of our serial port register set.
59 */
60#define UART_PORT_SIZE 0x100
61
62/*
63 * This determines how often we check the modem status signals
64 * for any change. They generally aren't connected to an IRQ
65 * so we have to poll them. We also check immediately before
66 * filling the TX fifo incase CTS has been dropped.
67 */
68#define MCTRL_TIMEOUT (250*HZ/1000)
69
70#define DRIVER_NAME "IMX-uart"
71
72struct imx_port {
73 struct uart_port port;
74 struct timer_list timer;
75 unsigned int old_status;
76 int txirq,rxirq;
77};
78
79/*
80 * Handle any change of modem status signal since we were last called.
81 */
82static void imx_mctrl_check(struct imx_port *sport)
83{
84 unsigned int status, changed;
85
86 status = sport->port.ops->get_mctrl(&sport->port);
87 changed = status ^ sport->old_status;
88
89 if (changed == 0)
90 return;
91
92 sport->old_status = status;
93
94 if (changed & TIOCM_RI)
95 sport->port.icount.rng++;
96 if (changed & TIOCM_DSR)
97 sport->port.icount.dsr++;
98 if (changed & TIOCM_CAR)
99 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
100 if (changed & TIOCM_CTS)
101 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
102
103 wake_up_interruptible(&sport->port.info->delta_msr_wait);
104}
105
106/*
107 * This is our per-port timeout handler, for checking the
108 * modem status signals.
109 */
110static void imx_timeout(unsigned long data)
111{
112 struct imx_port *sport = (struct imx_port *)data;
113 unsigned long flags;
114
115 if (sport->port.info) {
116 spin_lock_irqsave(&sport->port.lock, flags);
117 imx_mctrl_check(sport);
118 spin_unlock_irqrestore(&sport->port.lock, flags);
119
120 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
121 }
122}
123
124/*
125 * interrupts disabled on entry
126 */
127static void imx_stop_tx(struct uart_port *port, unsigned int tty_stop)
128{
129 struct imx_port *sport = (struct imx_port *)port;
130 UCR1((u32)sport->port.membase) &= ~UCR1_TXMPTYEN;
131}
132
133/*
134 * interrupts disabled on entry
135 */
136static void imx_stop_rx(struct uart_port *port)
137{
138 struct imx_port *sport = (struct imx_port *)port;
139 UCR2((u32)sport->port.membase) &= ~UCR2_RXEN;
140}
141
142/*
143 * Set the modem control timer to fire immediately.
144 */
145static void imx_enable_ms(struct uart_port *port)
146{
147 struct imx_port *sport = (struct imx_port *)port;
148
149 mod_timer(&sport->timer, jiffies);
150}
151
152static inline void imx_transmit_buffer(struct imx_port *sport)
153{
154 struct circ_buf *xmit = &sport->port.info->xmit;
155
156 do {
157 /* send xmit->buf[xmit->tail]
158 * out the port here */
159 URTX0((u32)sport->port.membase) = xmit->buf[xmit->tail];
160 xmit->tail = (xmit->tail + 1) &
161 (UART_XMIT_SIZE - 1);
162 sport->port.icount.tx++;
163 if (uart_circ_empty(xmit))
164 break;
165 } while (!(UTS((u32)sport->port.membase) & UTS_TXFULL));
166
167 if (uart_circ_empty(xmit))
168 imx_stop_tx(&sport->port, 0);
169}
170
171/*
172 * interrupts disabled on entry
173 */
174static void imx_start_tx(struct uart_port *port, unsigned int tty_start)
175{
176 struct imx_port *sport = (struct imx_port *)port;
177
178 UCR1((u32)sport->port.membase) |= UCR1_TXMPTYEN;
179
180 if(UTS((u32)sport->port.membase) & UTS_TXEMPTY)
181 imx_transmit_buffer(sport);
182}
183
184static irqreturn_t imx_txint(int irq, void *dev_id, struct pt_regs *regs)
185{
186 struct imx_port *sport = (struct imx_port *)dev_id;
187 struct circ_buf *xmit = &sport->port.info->xmit;
188 unsigned long flags;
189
190 spin_lock_irqsave(&sport->port.lock,flags);
191 if (sport->port.x_char)
192 {
193 /* Send next char */
194 URTX0((u32)sport->port.membase) = sport->port.x_char;
195 goto out;
196 }
197
198 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
199 imx_stop_tx(&sport->port, 0);
200 goto out;
201 }
202
203 imx_transmit_buffer(sport);
204
205 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
206 uart_write_wakeup(&sport->port);
207
208out:
209 spin_unlock_irqrestore(&sport->port.lock,flags);
210 return IRQ_HANDLED;
211}
212
213static irqreturn_t imx_rxint(int irq, void *dev_id, struct pt_regs *regs)
214{
215 struct imx_port *sport = dev_id;
216 unsigned int rx,flg,ignored = 0;
217 struct tty_struct *tty = sport->port.info->tty;
218 unsigned long flags;
219
220 rx = URXD0((u32)sport->port.membase);
221 spin_lock_irqsave(&sport->port.lock,flags);
222
223 do {
224 flg = TTY_NORMAL;
225 sport->port.icount.rx++;
226
227 if( USR2((u32)sport->port.membase) & USR2_BRCD ) {
228 USR2((u32)sport->port.membase) |= USR2_BRCD;
229 if(uart_handle_break(&sport->port))
230 goto ignore_char;
231 }
232
233 if (uart_handle_sysrq_char
234 (&sport->port, (unsigned char)rx, regs))
235 goto ignore_char;
236
237 if( rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) )
238 goto handle_error;
239
240 error_return:
241 tty_insert_flip_char(tty, rx, flg);
242
243 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
244 goto out;
245
246 ignore_char:
247 rx = URXD0((u32)sport->port.membase);
248 } while(rx & URXD_CHARRDY);
249
250out:
251 spin_unlock_irqrestore(&sport->port.lock,flags);
252 tty_flip_buffer_push(tty);
253 return IRQ_HANDLED;
254
255handle_error:
256 if (rx & URXD_PRERR)
257 sport->port.icount.parity++;
258 else if (rx & URXD_FRMERR)
259 sport->port.icount.frame++;
260 if (rx & URXD_OVRRUN)
261 sport->port.icount.overrun++;
262
263 if (rx & sport->port.ignore_status_mask) {
264 if (++ignored > 100)
265 goto out;
266 goto ignore_char;
267 }
268
269 rx &= sport->port.read_status_mask;
270
271 if (rx & URXD_PRERR)
272 flg = TTY_PARITY;
273 else if (rx & URXD_FRMERR)
274 flg = TTY_FRAME;
275 if (rx & URXD_OVRRUN)
276 flg = TTY_OVERRUN;
277
278#ifdef SUPPORT_SYSRQ
279 sport->port.sysrq = 0;
280#endif
281 goto error_return;
282}
283
284/*
285 * Return TIOCSER_TEMT when transmitter is not busy.
286 */
287static unsigned int imx_tx_empty(struct uart_port *port)
288{
289 struct imx_port *sport = (struct imx_port *)port;
290
291 return USR2((u32)sport->port.membase) & USR2_TXDC ? TIOCSER_TEMT : 0;
292}
293
294static unsigned int imx_get_mctrl(struct uart_port *port)
295{
296 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
297}
298
299static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
300{
301}
302
303/*
304 * Interrupts always disabled.
305 */
306static void imx_break_ctl(struct uart_port *port, int break_state)
307{
308 struct imx_port *sport = (struct imx_port *)port;
309 unsigned long flags;
310
311 spin_lock_irqsave(&sport->port.lock, flags);
312
313 if ( break_state != 0 )
314 UCR1((u32)sport->port.membase) |= UCR1_SNDBRK;
315 else
316 UCR1((u32)sport->port.membase) &= ~UCR1_SNDBRK;
317
318 spin_unlock_irqrestore(&sport->port.lock, flags);
319}
320
321#define TXTL 2 /* reset default */
322#define RXTL 1 /* reset default */
323
324static int imx_startup(struct uart_port *port)
325{
326 struct imx_port *sport = (struct imx_port *)port;
327 int retval;
328 unsigned int val;
329 unsigned long flags;
330
331 /* set receiver / transmitter trigger level. We assume
332 * that RFDIV has been set by the arch setup or by the bootloader.
333 */
334 val = (UFCR((u32)sport->port.membase) & UFCR_RFDIV) | TXTL<<10 | RXTL;
335 UFCR((u32)sport->port.membase) = val;
336
337 /* disable the DREN bit (Data Ready interrupt enable) before
338 * requesting IRQs
339 */
340 UCR4((u32)sport->port.membase) &= ~UCR4_DREN;
341
342 /*
343 * Allocate the IRQ
344 */
345 retval = request_irq(sport->rxirq, imx_rxint, 0,
346 DRIVER_NAME, sport);
347 if (retval) goto error_out2;
348
349 retval = request_irq(sport->txirq, imx_txint, 0,
350 "imx-uart", sport);
351 if (retval) goto error_out1;
352
353 /*
354 * Finally, clear and enable interrupts
355 */
356
357 UCR1((u32)sport->port.membase) |=
358 (UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_UARTEN);
359
360 UCR2((u32)sport->port.membase) |= (UCR2_RXEN | UCR2_TXEN);
361 /*
362 * Enable modem status interrupts
363 */
364 spin_lock_irqsave(&sport->port.lock,flags);
365 imx_enable_ms(&sport->port);
366 spin_unlock_irqrestore(&sport->port.lock,flags);
367
368 return 0;
369
370error_out1:
371 free_irq(sport->rxirq, sport);
372error_out2:
373 free_irq(sport->txirq, sport);
374 return retval;
375}
376
377static void imx_shutdown(struct uart_port *port)
378{
379 struct imx_port *sport = (struct imx_port *)port;
380
381 /*
382 * Stop our timer.
383 */
384 del_timer_sync(&sport->timer);
385
386 /*
387 * Free the interrupts
388 */
389 free_irq(sport->txirq, sport);
390 free_irq(sport->rxirq, sport);
391
392 /*
393 * Disable all interrupts, port and break condition.
394 */
395
396 UCR1((u32)sport->port.membase) &=
397 ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_UARTEN);
398}
399
400static void
401imx_set_termios(struct uart_port *port, struct termios *termios,
402 struct termios *old)
403{
404 struct imx_port *sport = (struct imx_port *)port;
405 unsigned long flags;
406 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
407 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
408
409 /*
410 * If we don't support modem control lines, don't allow
411 * these to be set.
412 */
413 if (0) {
414 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
415 termios->c_cflag |= CLOCAL;
416 }
417
418 /*
419 * We only support CS7 and CS8.
420 */
421 while ((termios->c_cflag & CSIZE) != CS7 &&
422 (termios->c_cflag & CSIZE) != CS8) {
423 termios->c_cflag &= ~CSIZE;
424 termios->c_cflag |= old_csize;
425 old_csize = CS8;
426 }
427
428 if ((termios->c_cflag & CSIZE) == CS8)
429 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
430 else
431 ucr2 = UCR2_SRST | UCR2_IRTS;
432
433 if (termios->c_cflag & CRTSCTS) {
434 ucr2 &= ~UCR2_IRTS;
435 ucr2 |= UCR2_CTSC;
436 }
437
438 if (termios->c_cflag & CSTOPB)
439 ucr2 |= UCR2_STPB;
440 if (termios->c_cflag & PARENB) {
441 ucr2 |= UCR2_PREN;
442 if (!(termios->c_cflag & PARODD))
443 ucr2 |= UCR2_PROE;
444 }
445
446 /*
447 * Ask the core to calculate the divisor for us.
448 */
449 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
450 quot = uart_get_divisor(port, baud);
451
452 spin_lock_irqsave(&sport->port.lock, flags);
453
454 sport->port.read_status_mask = 0;
455 if (termios->c_iflag & INPCK)
456 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
457 if (termios->c_iflag & (BRKINT | PARMRK))
458 sport->port.read_status_mask |= URXD_BRK;
459
460 /*
461 * Characters to ignore
462 */
463 sport->port.ignore_status_mask = 0;
464 if (termios->c_iflag & IGNPAR)
465 sport->port.ignore_status_mask |= URXD_PRERR;
466 if (termios->c_iflag & IGNBRK) {
467 sport->port.ignore_status_mask |= URXD_BRK;
468 /*
469 * If we're ignoring parity and break indicators,
470 * ignore overruns too (for real raw support).
471 */
472 if (termios->c_iflag & IGNPAR)
473 sport->port.ignore_status_mask |= URXD_OVRRUN;
474 }
475
476 del_timer_sync(&sport->timer);
477
478 /*
479 * Update the per-port timeout.
480 */
481 uart_update_timeout(port, termios->c_cflag, baud);
482
483 /*
484 * disable interrupts and drain transmitter
485 */
486 old_ucr1 = UCR1((u32)sport->port.membase);
487 UCR1((u32)sport->port.membase) &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN);
488
489 while ( !(USR2((u32)sport->port.membase) & USR2_TXDC))
490 barrier();
491
492 /* then, disable everything */
493 old_txrxen = UCR2((u32)sport->port.membase) & ( UCR2_TXEN | UCR2_RXEN );
494 UCR2((u32)sport->port.membase) &= ~( UCR2_TXEN | UCR2_RXEN);
495
496 /* set the parity, stop bits and data size */
497 UCR2((u32)sport->port.membase) = ucr2;
498
499 /* set the baud rate. We assume uartclk = 16 MHz
500 *
501 * baud * 16 UBIR - 1
502 * --------- = --------
503 * uartclk UBMR - 1
504 */
505 UBIR((u32)sport->port.membase) = (baud / 100) - 1;
506 UBMR((u32)sport->port.membase) = 10000 - 1;
507
508 UCR1((u32)sport->port.membase) = old_ucr1;
509 UCR2((u32)sport->port.membase) |= old_txrxen;
510
511 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
512 imx_enable_ms(&sport->port);
513
514 spin_unlock_irqrestore(&sport->port.lock, flags);
515}
516
517static const char *imx_type(struct uart_port *port)
518{
519 struct imx_port *sport = (struct imx_port *)port;
520
521 return sport->port.type == PORT_IMX ? "IMX" : NULL;
522}
523
524/*
525 * Release the memory region(s) being used by 'port'.
526 */
527static void imx_release_port(struct uart_port *port)
528{
529 struct imx_port *sport = (struct imx_port *)port;
530
531 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
532}
533
534/*
535 * Request the memory region(s) being used by 'port'.
536 */
537static int imx_request_port(struct uart_port *port)
538{
539 struct imx_port *sport = (struct imx_port *)port;
540
541 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
542 "imx-uart") != NULL ? 0 : -EBUSY;
543}
544
545/*
546 * Configure/autoconfigure the port.
547 */
548static void imx_config_port(struct uart_port *port, int flags)
549{
550 struct imx_port *sport = (struct imx_port *)port;
551
552 if (flags & UART_CONFIG_TYPE &&
553 imx_request_port(&sport->port) == 0)
554 sport->port.type = PORT_IMX;
555}
556
557/*
558 * Verify the new serial_struct (for TIOCSSERIAL).
559 * The only change we allow are to the flags and type, and
560 * even then only between PORT_IMX and PORT_UNKNOWN
561 */
562static int
563imx_verify_port(struct uart_port *port, struct serial_struct *ser)
564{
565 struct imx_port *sport = (struct imx_port *)port;
566 int ret = 0;
567
568 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
569 ret = -EINVAL;
570 if (sport->port.irq != ser->irq)
571 ret = -EINVAL;
572 if (ser->io_type != UPIO_MEM)
573 ret = -EINVAL;
574 if (sport->port.uartclk / 16 != ser->baud_base)
575 ret = -EINVAL;
576 if ((void *)sport->port.mapbase != ser->iomem_base)
577 ret = -EINVAL;
578 if (sport->port.iobase != ser->port)
579 ret = -EINVAL;
580 if (ser->hub6 != 0)
581 ret = -EINVAL;
582 return ret;
583}
584
585static struct uart_ops imx_pops = {
586 .tx_empty = imx_tx_empty,
587 .set_mctrl = imx_set_mctrl,
588 .get_mctrl = imx_get_mctrl,
589 .stop_tx = imx_stop_tx,
590 .start_tx = imx_start_tx,
591 .stop_rx = imx_stop_rx,
592 .enable_ms = imx_enable_ms,
593 .break_ctl = imx_break_ctl,
594 .startup = imx_startup,
595 .shutdown = imx_shutdown,
596 .set_termios = imx_set_termios,
597 .type = imx_type,
598 .release_port = imx_release_port,
599 .request_port = imx_request_port,
600 .config_port = imx_config_port,
601 .verify_port = imx_verify_port,
602};
603
604static struct imx_port imx_ports[] = {
605 {
606 .txirq = UART1_MINT_TX,
607 .rxirq = UART1_MINT_RX,
608 .port = {
609 .type = PORT_IMX,
610 .iotype = SERIAL_IO_MEM,
611 .membase = (void *)IMX_UART1_BASE,
612 .mapbase = IMX_UART1_BASE, /* FIXME */
613 .irq = UART1_MINT_RX,
614 .uartclk = 16000000,
615 .fifosize = 8,
616 .flags = ASYNC_BOOT_AUTOCONF,
617 .ops = &imx_pops,
618 .line = 0,
619 },
620 }, {
621 .txirq = UART2_MINT_TX,
622 .rxirq = UART2_MINT_RX,
623 .port = {
624 .type = PORT_IMX,
625 .iotype = SERIAL_IO_MEM,
626 .membase = (void *)IMX_UART2_BASE,
627 .mapbase = IMX_UART2_BASE, /* FIXME */
628 .irq = UART2_MINT_RX,
629 .uartclk = 16000000,
630 .fifosize = 8,
631 .flags = ASYNC_BOOT_AUTOCONF,
632 .ops = &imx_pops,
633 .line = 1,
634 },
635 }
636};
637
638/*
639 * Setup the IMX serial ports.
640 * Note also that we support "console=ttySMXx" where "x" is either 0 or 1.
641 * Which serial port this ends up being depends on the machine you're
642 * running this kernel on. I'm not convinced that this is a good idea,
643 * but that's the way it traditionally works.
644 *
645 */
646static void __init imx_init_ports(void)
647{
648 static int first = 1;
649 int i;
650
651 if (!first)
652 return;
653 first = 0;
654
655 for (i = 0; i < ARRAY_SIZE(imx_ports); i++) {
656 init_timer(&imx_ports[i].timer);
657 imx_ports[i].timer.function = imx_timeout;
658 imx_ports[i].timer.data = (unsigned long)&imx_ports[i];
659 }
660
661 imx_gpio_mode(PC9_PF_UART1_CTS);
662 imx_gpio_mode(PC10_PF_UART1_RTS);
663 imx_gpio_mode(PC11_PF_UART1_TXD);
664 imx_gpio_mode(PC12_PF_UART1_RXD);
665 imx_gpio_mode(PB28_PF_UART2_CTS);
666 imx_gpio_mode(PB29_PF_UART2_RTS);
667
668 imx_gpio_mode(PB30_PF_UART2_TXD);
669 imx_gpio_mode(PB31_PF_UART2_RXD);
670
671#if 0 /* We don't need these, on the mx1 the _modem_ side of the uart
672 * is implemented.
673 */
674 imx_gpio_mode(PD7_AF_UART2_DTR);
675 imx_gpio_mode(PD8_AF_UART2_DCD);
676 imx_gpio_mode(PD9_AF_UART2_RI);
677 imx_gpio_mode(PD10_AF_UART2_DSR);
678#endif
679
680
681}
682
683#ifdef CONFIG_SERIAL_IMX_CONSOLE
684
685/*
686 * Interrupts are disabled on entering
687 */
688static void
689imx_console_write(struct console *co, const char *s, unsigned int count)
690{
691 struct imx_port *sport = &imx_ports[co->index];
692 unsigned int old_ucr1, old_ucr2, i;
693
694 /*
695 * First, save UCR1/2 and then disable interrupts
696 */
697 old_ucr1 = UCR1((u32)sport->port.membase);
698 old_ucr2 = UCR2((u32)sport->port.membase);
699
700 UCR1((u32)sport->port.membase) =
701 (old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN)
702 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN);
703 UCR2((u32)sport->port.membase) = old_ucr2 | UCR2_TXEN;
704
705 /*
706 * Now, do each character
707 */
708 for (i = 0; i < count; i++) {
709
710 while ((UTS((u32)sport->port.membase) & UTS_TXFULL))
711 barrier();
712
713 URTX0((u32)sport->port.membase) = s[i];
714
715 if (s[i] == '\n') {
716 while ((UTS((u32)sport->port.membase) & UTS_TXFULL))
717 barrier();
718 URTX0((u32)sport->port.membase) = '\r';
719 }
720 }
721
722 /*
723 * Finally, wait for transmitter to become empty
724 * and restore UCR1/2
725 */
726 while (!(USR2((u32)sport->port.membase) & USR2_TXDC));
727
728 UCR1((u32)sport->port.membase) = old_ucr1;
729 UCR2((u32)sport->port.membase) = old_ucr2;
730}
731
732/*
733 * If the port was already initialised (eg, by a boot loader),
734 * try to determine the current setup.
735 */
736static void __init
737imx_console_get_options(struct imx_port *sport, int *baud,
738 int *parity, int *bits)
739{
740 if ( UCR1((u32)sport->port.membase) | UCR1_UARTEN ) {
741 /* ok, the port was enabled */
742 unsigned int ucr2, ubir,ubmr, uartclk;
743
744 ucr2 = UCR2((u32)sport->port.membase);
745
746 *parity = 'n';
747 if (ucr2 & UCR2_PREN) {
748 if (ucr2 & UCR2_PROE)
749 *parity = 'o';
750 else
751 *parity = 'e';
752 }
753
754 if (ucr2 & UCR2_WS)
755 *bits = 8;
756 else
757 *bits = 7;
758
759 ubir = UBIR((u32)sport->port.membase) & 0xffff;
760 ubmr = UBMR((u32)sport->port.membase) & 0xffff;
761 uartclk = sport->port.uartclk;
762
763 *baud = ((uartclk/16) * (ubir + 1)) / (ubmr + 1);
764 }
765}
766
767static int __init
768imx_console_setup(struct console *co, char *options)
769{
770 struct imx_port *sport;
771 int baud = 9600;
772 int bits = 8;
773 int parity = 'n';
774 int flow = 'n';
775
776 /*
777 * Check whether an invalid uart number has been specified, and
778 * if so, search for the first available port that does have
779 * console support.
780 */
781 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
782 co->index = 0;
783 sport = &imx_ports[co->index];
784
785 if (options)
786 uart_parse_options(options, &baud, &parity, &bits, &flow);
787 else
788 imx_console_get_options(sport, &baud, &parity, &bits);
789
790 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
791}
792
793extern struct uart_driver imx_reg;
794static struct console imx_console = {
795 .name = "ttySMX",
796 .write = imx_console_write,
797 .device = uart_console_device,
798 .setup = imx_console_setup,
799 .flags = CON_PRINTBUFFER,
800 .index = -1,
801 .data = &imx_reg,
802};
803
804static int __init imx_rs_console_init(void)
805{
806 imx_init_ports();
807 register_console(&imx_console);
808 return 0;
809}
810console_initcall(imx_rs_console_init);
811
812#define IMX_CONSOLE &imx_console
813#else
814#define IMX_CONSOLE NULL
815#endif
816
817static struct uart_driver imx_reg = {
818 .owner = THIS_MODULE,
819 .driver_name = DRIVER_NAME,
820 .dev_name = "ttySMX",
821 .devfs_name = "ttsmx/",
822 .major = SERIAL_IMX_MAJOR,
823 .minor = MINOR_START,
824 .nr = ARRAY_SIZE(imx_ports),
825 .cons = IMX_CONSOLE,
826};
827
828static int serial_imx_suspend(struct device *_dev, u32 state, u32 level)
829{
830 struct imx_port *sport = dev_get_drvdata(_dev);
831
832 if (sport && level == SUSPEND_DISABLE)
833 uart_suspend_port(&imx_reg, &sport->port);
834
835 return 0;
836}
837
838static int serial_imx_resume(struct device *_dev, u32 level)
839{
840 struct imx_port *sport = dev_get_drvdata(_dev);
841
842 if (sport && level == RESUME_ENABLE)
843 uart_resume_port(&imx_reg, &sport->port);
844
845 return 0;
846}
847
848static int serial_imx_probe(struct device *_dev)
849{
850 struct platform_device *dev = to_platform_device(_dev);
851
852 imx_ports[dev->id].port.dev = _dev;
853 uart_add_one_port(&imx_reg, &imx_ports[dev->id].port);
854 dev_set_drvdata(_dev, &imx_ports[dev->id]);
855 return 0;
856}
857
858static int serial_imx_remove(struct device *_dev)
859{
860 struct imx_port *sport = dev_get_drvdata(_dev);
861
862 dev_set_drvdata(_dev, NULL);
863
864 if (sport)
865 uart_remove_one_port(&imx_reg, &sport->port);
866
867 return 0;
868}
869
870static struct device_driver serial_imx_driver = {
871 .name = "imx-uart",
872 .bus = &platform_bus_type,
873 .probe = serial_imx_probe,
874 .remove = serial_imx_remove,
875
876 .suspend = serial_imx_suspend,
877 .resume = serial_imx_resume,
878};
879
880static int __init imx_serial_init(void)
881{
882 int ret;
883
884 printk(KERN_INFO "Serial: IMX driver\n");
885
886 imx_init_ports();
887
888 ret = uart_register_driver(&imx_reg);
889 if (ret)
890 return ret;
891
892 ret = driver_register(&serial_imx_driver);
893 if (ret != 0)
894 uart_unregister_driver(&imx_reg);
895
896 return 0;
897}
898
899static void __exit imx_serial_exit(void)
900{
901 uart_unregister_driver(&imx_reg);
902}
903
904module_init(imx_serial_init);
905module_exit(imx_serial_exit);
906
907MODULE_AUTHOR("Sascha Hauer");
908MODULE_DESCRIPTION("IMX generic serial port driver");
909MODULE_LICENSE("GPL");
diff --git a/drivers/serial/ioc4_serial.c b/drivers/serial/ioc4_serial.c
new file mode 100644
index 000000000000..d054f1265701
--- /dev/null
+++ b/drivers/serial/ioc4_serial.c
@@ -0,0 +1,2909 @@
1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2003-2005 Silicon Graphics, Inc. All Rights Reserved.
7 */
8
9
10/*
11 * This file contains a module version of the ioc4 serial driver. This
12 * includes all the support functions needed (support functions, etc.)
13 * and the serial driver itself.
14 */
15#include <linux/errno.h>
16#include <linux/tty.h>
17#include <linux/serial.h>
18#include <linux/serialP.h>
19#include <linux/circ_buf.h>
20#include <linux/serial_reg.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/ioc4_common.h>
24#include <linux/serial_core.h>
25
26/*
27 * interesting things about the ioc4
28 */
29
30#define IOC4_NUM_SERIAL_PORTS 4 /* max ports per card */
31#define IOC4_NUM_CARDS 8 /* max cards per partition */
32
33#define GET_SIO_IR(_n) (_n == 0) ? (IOC4_SIO_IR_S0) : \
34 (_n == 1) ? (IOC4_SIO_IR_S1) : \
35 (_n == 2) ? (IOC4_SIO_IR_S2) : \
36 (IOC4_SIO_IR_S3)
37
38#define GET_OTHER_IR(_n) (_n == 0) ? (IOC4_OTHER_IR_S0_MEMERR) : \
39 (_n == 1) ? (IOC4_OTHER_IR_S1_MEMERR) : \
40 (_n == 2) ? (IOC4_OTHER_IR_S2_MEMERR) : \
41 (IOC4_OTHER_IR_S3_MEMERR)
42
43
44/*
45 * All IOC4 registers are 32 bits wide.
46 */
47
48/*
49 * PCI Memory Space Map
50 */
51#define IOC4_PCI_ERR_ADDR_L 0x000 /* Low Error Address */
52#define IOC4_PCI_ERR_ADDR_VLD (0x1 << 0)
53#define IOC4_PCI_ERR_ADDR_MST_ID_MSK (0xf << 1)
54#define IOC4_PCI_ERR_ADDR_MST_NUM_MSK (0xe << 1)
55#define IOC4_PCI_ERR_ADDR_MST_TYP_MSK (0x1 << 1)
56#define IOC4_PCI_ERR_ADDR_MUL_ERR (0x1 << 5)
57#define IOC4_PCI_ERR_ADDR_ADDR_MSK (0x3ffffff << 6)
58
59/* Interrupt types */
60#define IOC4_SIO_INTR_TYPE 0
61#define IOC4_OTHER_INTR_TYPE 1
62#define IOC4_NUM_INTR_TYPES 2
63
64/* Bitmasks for IOC4_SIO_IR, IOC4_SIO_IEC, and IOC4_SIO_IES */
65#define IOC4_SIO_IR_S0_TX_MT 0x00000001 /* Serial port 0 TX empty */
66#define IOC4_SIO_IR_S0_RX_FULL 0x00000002 /* Port 0 RX buf full */
67#define IOC4_SIO_IR_S0_RX_HIGH 0x00000004 /* Port 0 RX hiwat */
68#define IOC4_SIO_IR_S0_RX_TIMER 0x00000008 /* Port 0 RX timeout */
69#define IOC4_SIO_IR_S0_DELTA_DCD 0x00000010 /* Port 0 delta DCD */
70#define IOC4_SIO_IR_S0_DELTA_CTS 0x00000020 /* Port 0 delta CTS */
71#define IOC4_SIO_IR_S0_INT 0x00000040 /* Port 0 pass-thru intr */
72#define IOC4_SIO_IR_S0_TX_EXPLICIT 0x00000080 /* Port 0 explicit TX thru */
73#define IOC4_SIO_IR_S1_TX_MT 0x00000100 /* Serial port 1 */
74#define IOC4_SIO_IR_S1_RX_FULL 0x00000200 /* */
75#define IOC4_SIO_IR_S1_RX_HIGH 0x00000400 /* */
76#define IOC4_SIO_IR_S1_RX_TIMER 0x00000800 /* */
77#define IOC4_SIO_IR_S1_DELTA_DCD 0x00001000 /* */
78#define IOC4_SIO_IR_S1_DELTA_CTS 0x00002000 /* */
79#define IOC4_SIO_IR_S1_INT 0x00004000 /* */
80#define IOC4_SIO_IR_S1_TX_EXPLICIT 0x00008000 /* */
81#define IOC4_SIO_IR_S2_TX_MT 0x00010000 /* Serial port 2 */
82#define IOC4_SIO_IR_S2_RX_FULL 0x00020000 /* */
83#define IOC4_SIO_IR_S2_RX_HIGH 0x00040000 /* */
84#define IOC4_SIO_IR_S2_RX_TIMER 0x00080000 /* */
85#define IOC4_SIO_IR_S2_DELTA_DCD 0x00100000 /* */
86#define IOC4_SIO_IR_S2_DELTA_CTS 0x00200000 /* */
87#define IOC4_SIO_IR_S2_INT 0x00400000 /* */
88#define IOC4_SIO_IR_S2_TX_EXPLICIT 0x00800000 /* */
89#define IOC4_SIO_IR_S3_TX_MT 0x01000000 /* Serial port 3 */
90#define IOC4_SIO_IR_S3_RX_FULL 0x02000000 /* */
91#define IOC4_SIO_IR_S3_RX_HIGH 0x04000000 /* */
92#define IOC4_SIO_IR_S3_RX_TIMER 0x08000000 /* */
93#define IOC4_SIO_IR_S3_DELTA_DCD 0x10000000 /* */
94#define IOC4_SIO_IR_S3_DELTA_CTS 0x20000000 /* */
95#define IOC4_SIO_IR_S3_INT 0x40000000 /* */
96#define IOC4_SIO_IR_S3_TX_EXPLICIT 0x80000000 /* */
97
98/* Per device interrupt masks */
99#define IOC4_SIO_IR_S0 (IOC4_SIO_IR_S0_TX_MT | \
100 IOC4_SIO_IR_S0_RX_FULL | \
101 IOC4_SIO_IR_S0_RX_HIGH | \
102 IOC4_SIO_IR_S0_RX_TIMER | \
103 IOC4_SIO_IR_S0_DELTA_DCD | \
104 IOC4_SIO_IR_S0_DELTA_CTS | \
105 IOC4_SIO_IR_S0_INT | \
106 IOC4_SIO_IR_S0_TX_EXPLICIT)
107#define IOC4_SIO_IR_S1 (IOC4_SIO_IR_S1_TX_MT | \
108 IOC4_SIO_IR_S1_RX_FULL | \
109 IOC4_SIO_IR_S1_RX_HIGH | \
110 IOC4_SIO_IR_S1_RX_TIMER | \
111 IOC4_SIO_IR_S1_DELTA_DCD | \
112 IOC4_SIO_IR_S1_DELTA_CTS | \
113 IOC4_SIO_IR_S1_INT | \
114 IOC4_SIO_IR_S1_TX_EXPLICIT)
115#define IOC4_SIO_IR_S2 (IOC4_SIO_IR_S2_TX_MT | \
116 IOC4_SIO_IR_S2_RX_FULL | \
117 IOC4_SIO_IR_S2_RX_HIGH | \
118 IOC4_SIO_IR_S2_RX_TIMER | \
119 IOC4_SIO_IR_S2_DELTA_DCD | \
120 IOC4_SIO_IR_S2_DELTA_CTS | \
121 IOC4_SIO_IR_S2_INT | \
122 IOC4_SIO_IR_S2_TX_EXPLICIT)
123#define IOC4_SIO_IR_S3 (IOC4_SIO_IR_S3_TX_MT | \
124 IOC4_SIO_IR_S3_RX_FULL | \
125 IOC4_SIO_IR_S3_RX_HIGH | \
126 IOC4_SIO_IR_S3_RX_TIMER | \
127 IOC4_SIO_IR_S3_DELTA_DCD | \
128 IOC4_SIO_IR_S3_DELTA_CTS | \
129 IOC4_SIO_IR_S3_INT | \
130 IOC4_SIO_IR_S3_TX_EXPLICIT)
131
132/* Bitmasks for IOC4_OTHER_IR, IOC4_OTHER_IEC, and IOC4_OTHER_IES */
133#define IOC4_OTHER_IR_ATA_INT 0x00000001 /* ATAPI intr pass-thru */
134#define IOC4_OTHER_IR_ATA_MEMERR 0x00000002 /* ATAPI DMA PCI error */
135#define IOC4_OTHER_IR_S0_MEMERR 0x00000004 /* Port 0 PCI error */
136#define IOC4_OTHER_IR_S1_MEMERR 0x00000008 /* Port 1 PCI error */
137#define IOC4_OTHER_IR_S2_MEMERR 0x00000010 /* Port 2 PCI error */
138#define IOC4_OTHER_IR_S3_MEMERR 0x00000020 /* Port 3 PCI error */
139
140/* Bitmasks for IOC4_SIO_CR */
141#define IOC4_SIO_CR_CMD_PULSE_SHIFT 0 /* byte bus strobe shift */
142#define IOC4_SIO_CR_ARB_DIAG_TX0 0x00000000
143#define IOC4_SIO_CR_ARB_DIAG_RX0 0x00000010
144#define IOC4_SIO_CR_ARB_DIAG_TX1 0x00000020
145#define IOC4_SIO_CR_ARB_DIAG_RX1 0x00000030
146#define IOC4_SIO_CR_ARB_DIAG_TX2 0x00000040
147#define IOC4_SIO_CR_ARB_DIAG_RX2 0x00000050
148#define IOC4_SIO_CR_ARB_DIAG_TX3 0x00000060
149#define IOC4_SIO_CR_ARB_DIAG_RX3 0x00000070
150#define IOC4_SIO_CR_SIO_DIAG_IDLE 0x00000080 /* 0 -> active request among
151 serial ports (ro) */
152/* Defs for some of the generic I/O pins */
153#define IOC4_GPCR_UART0_MODESEL 0x10 /* Pin is output to port 0
154 mode sel */
155#define IOC4_GPCR_UART1_MODESEL 0x20 /* Pin is output to port 1
156 mode sel */
157#define IOC4_GPCR_UART2_MODESEL 0x40 /* Pin is output to port 2
158 mode sel */
159#define IOC4_GPCR_UART3_MODESEL 0x80 /* Pin is output to port 3
160 mode sel */
161
162#define IOC4_GPPR_UART0_MODESEL_PIN 4 /* GIO pin controlling
163 uart 0 mode select */
164#define IOC4_GPPR_UART1_MODESEL_PIN 5 /* GIO pin controlling
165 uart 1 mode select */
166#define IOC4_GPPR_UART2_MODESEL_PIN 6 /* GIO pin controlling
167 uart 2 mode select */
168#define IOC4_GPPR_UART3_MODESEL_PIN 7 /* GIO pin controlling
169 uart 3 mode select */
170
171/* Bitmasks for serial RX status byte */
172#define IOC4_RXSB_OVERRUN 0x01 /* Char(s) lost */
173#define IOC4_RXSB_PAR_ERR 0x02 /* Parity error */
174#define IOC4_RXSB_FRAME_ERR 0x04 /* Framing error */
175#define IOC4_RXSB_BREAK 0x08 /* Break character */
176#define IOC4_RXSB_CTS 0x10 /* State of CTS */
177#define IOC4_RXSB_DCD 0x20 /* State of DCD */
178#define IOC4_RXSB_MODEM_VALID 0x40 /* DCD, CTS, and OVERRUN are valid */
179#define IOC4_RXSB_DATA_VALID 0x80 /* Data byte, FRAME_ERR PAR_ERR
180 * & BREAK valid */
181
182/* Bitmasks for serial TX control byte */
183#define IOC4_TXCB_INT_WHEN_DONE 0x20 /* Interrupt after this byte is sent */
184#define IOC4_TXCB_INVALID 0x00 /* Byte is invalid */
185#define IOC4_TXCB_VALID 0x40 /* Byte is valid */
186#define IOC4_TXCB_MCR 0x80 /* Data<7:0> to modem control reg */
187#define IOC4_TXCB_DELAY 0xc0 /* Delay data<7:0> mSec */
188
189/* Bitmasks for IOC4_SBBR_L */
190#define IOC4_SBBR_L_SIZE 0x00000001 /* 0 == 1KB rings, 1 == 4KB rings */
191
192/* Bitmasks for IOC4_SSCR_<3:0> */
193#define IOC4_SSCR_RX_THRESHOLD 0x000001ff /* Hiwater mark */
194#define IOC4_SSCR_TX_TIMER_BUSY 0x00010000 /* TX timer in progress */
195#define IOC4_SSCR_HFC_EN 0x00020000 /* Hardware flow control enabled */
196#define IOC4_SSCR_RX_RING_DCD 0x00040000 /* Post RX record on delta-DCD */
197#define IOC4_SSCR_RX_RING_CTS 0x00080000 /* Post RX record on delta-CTS */
198#define IOC4_SSCR_DIAG 0x00200000 /* Bypass clock divider for sim */
199#define IOC4_SSCR_RX_DRAIN 0x08000000 /* Drain RX buffer to memory */
200#define IOC4_SSCR_DMA_EN 0x10000000 /* Enable ring buffer DMA */
201#define IOC4_SSCR_DMA_PAUSE 0x20000000 /* Pause DMA */
202#define IOC4_SSCR_PAUSE_STATE 0x40000000 /* Sets when PAUSE takes effect */
203#define IOC4_SSCR_RESET 0x80000000 /* Reset DMA channels */
204
205/* All producer/comsumer pointers are the same bitfield */
206#define IOC4_PROD_CONS_PTR_4K 0x00000ff8 /* For 4K buffers */
207#define IOC4_PROD_CONS_PTR_1K 0x000003f8 /* For 1K buffers */
208#define IOC4_PROD_CONS_PTR_OFF 3
209
210/* Bitmasks for IOC4_SRCIR_<3:0> */
211#define IOC4_SRCIR_ARM 0x80000000 /* Arm RX timer */
212
213/* Bitmasks for IOC4_SHADOW_<3:0> */
214#define IOC4_SHADOW_DR 0x00000001 /* Data ready */
215#define IOC4_SHADOW_OE 0x00000002 /* Overrun error */
216#define IOC4_SHADOW_PE 0x00000004 /* Parity error */
217#define IOC4_SHADOW_FE 0x00000008 /* Framing error */
218#define IOC4_SHADOW_BI 0x00000010 /* Break interrupt */
219#define IOC4_SHADOW_THRE 0x00000020 /* Xmit holding register empty */
220#define IOC4_SHADOW_TEMT 0x00000040 /* Xmit shift register empty */
221#define IOC4_SHADOW_RFCE 0x00000080 /* Char in RX fifo has an error */
222#define IOC4_SHADOW_DCTS 0x00010000 /* Delta clear to send */
223#define IOC4_SHADOW_DDCD 0x00080000 /* Delta data carrier detect */
224#define IOC4_SHADOW_CTS 0x00100000 /* Clear to send */
225#define IOC4_SHADOW_DCD 0x00800000 /* Data carrier detect */
226#define IOC4_SHADOW_DTR 0x01000000 /* Data terminal ready */
227#define IOC4_SHADOW_RTS 0x02000000 /* Request to send */
228#define IOC4_SHADOW_OUT1 0x04000000 /* 16550 OUT1 bit */
229#define IOC4_SHADOW_OUT2 0x08000000 /* 16550 OUT2 bit */
230#define IOC4_SHADOW_LOOP 0x10000000 /* Loopback enabled */
231
232/* Bitmasks for IOC4_SRTR_<3:0> */
233#define IOC4_SRTR_CNT 0x00000fff /* Reload value for RX timer */
234#define IOC4_SRTR_CNT_VAL 0x0fff0000 /* Current value of RX timer */
235#define IOC4_SRTR_CNT_VAL_SHIFT 16
236#define IOC4_SRTR_HZ 16000 /* SRTR clock frequency */
237
238/* Serial port register map used for DMA and PIO serial I/O */
239struct ioc4_serialregs {
240 uint32_t sscr;
241 uint32_t stpir;
242 uint32_t stcir;
243 uint32_t srpir;
244 uint32_t srcir;
245 uint32_t srtr;
246 uint32_t shadow;
247};
248
249/* IOC4 UART register map */
250struct ioc4_uartregs {
251 char i4u_lcr;
252 union {
253 char iir; /* read only */
254 char fcr; /* write only */
255 } u3;
256 union {
257 char ier; /* DLAB == 0 */
258 char dlm; /* DLAB == 1 */
259 } u2;
260 union {
261 char rbr; /* read only, DLAB == 0 */
262 char thr; /* write only, DLAB == 0 */
263 char dll; /* DLAB == 1 */
264 } u1;
265 char i4u_scr;
266 char i4u_msr;
267 char i4u_lsr;
268 char i4u_mcr;
269};
270
271/* short names */
272#define i4u_dll u1.dll
273#define i4u_ier u2.ier
274#define i4u_dlm u2.dlm
275#define i4u_fcr u3.fcr
276
277/* PCI memory space register map addressed using pci_bar0 */
278struct ioc4_memregs {
279 struct ioc4_mem {
280 /* Miscellaneous IOC4 registers */
281 uint32_t pci_err_addr_l;
282 uint32_t pci_err_addr_h;
283 uint32_t sio_ir;
284 uint32_t other_ir;
285
286 /* These registers are read-only for general kernel code. */
287 uint32_t sio_ies_ro;
288 uint32_t other_ies_ro;
289 uint32_t sio_iec_ro;
290 uint32_t other_iec_ro;
291 uint32_t sio_cr;
292 uint32_t misc_fill1;
293 uint32_t int_out;
294 uint32_t misc_fill2;
295 uint32_t gpcr_s;
296 uint32_t gpcr_c;
297 uint32_t gpdr;
298 uint32_t misc_fill3;
299 uint32_t gppr_0;
300 uint32_t gppr_1;
301 uint32_t gppr_2;
302 uint32_t gppr_3;
303 uint32_t gppr_4;
304 uint32_t gppr_5;
305 uint32_t gppr_6;
306 uint32_t gppr_7;
307 } ioc4_mem;
308
309 char misc_fill4[0x100 - 0x5C - 4];
310
311 /* ATA/ATAP registers */
312 uint32_t ata_notused[9];
313 char ata_fill1[0x140 - 0x120 - 4];
314 uint32_t ata_notused1[8];
315 char ata_fill2[0x200 - 0x15C - 4];
316
317 /* Keyboard and mouse registers */
318 uint32_t km_notused[5];;
319 char km_fill1[0x300 - 0x210 - 4];
320
321 /* Serial port registers used for DMA serial I/O */
322 struct ioc4_serial {
323 uint32_t sbbr01_l;
324 uint32_t sbbr01_h;
325 uint32_t sbbr23_l;
326 uint32_t sbbr23_h;
327
328 struct ioc4_serialregs port_0;
329 struct ioc4_serialregs port_1;
330 struct ioc4_serialregs port_2;
331 struct ioc4_serialregs port_3;
332 struct ioc4_uartregs uart_0;
333 struct ioc4_uartregs uart_1;
334 struct ioc4_uartregs uart_2;
335 struct ioc4_uartregs uart_3;
336 } ioc4_serial;
337};
338
339/* UART clock speed */
340#define IOC4_SER_XIN_CLK IOC4_SER_XIN_CLK_66
341#define IOC4_SER_XIN_CLK_66 66666667
342#define IOC4_SER_XIN_CLK_33 33333333
343
344#define IOC4_W_IES 0
345#define IOC4_W_IEC 1
346
347typedef void ioc4_intr_func_f(void *, uint32_t);
348typedef ioc4_intr_func_f *ioc4_intr_func_t;
349
350/* defining this will get you LOTS of great debug info */
351//#define DEBUG_INTERRUPTS
352#define DPRINT_CONFIG(_x...) ;
353//#define DPRINT_CONFIG(_x...) printk _x
354
355/* number of characters left in xmit buffer before we ask for more */
356#define WAKEUP_CHARS 256
357
358/* number of characters we want to transmit to the lower level at a time */
359#define IOC4_MAX_CHARS 128
360
361/* Device name we're using */
362#define DEVICE_NAME "ttyIOC"
363#define DEVICE_MAJOR 204
364#define DEVICE_MINOR 50
365
366/* register offsets */
367#define IOC4_SERIAL_OFFSET 0x300
368
369/* flags for next_char_state */
370#define NCS_BREAK 0x1
371#define NCS_PARITY 0x2
372#define NCS_FRAMING 0x4
373#define NCS_OVERRUN 0x8
374
375/* cause we need SOME parameters ... */
376#define MIN_BAUD_SUPPORTED 1200
377#define MAX_BAUD_SUPPORTED 115200
378
379/* protocol types supported */
380enum sio_proto {
381 PROTO_RS232,
382 PROTO_RS422
383};
384
385/* Notification types */
386#define N_DATA_READY 0x01
387#define N_OUTPUT_LOWAT 0x02
388#define N_BREAK 0x04
389#define N_PARITY_ERROR 0x08
390#define N_FRAMING_ERROR 0x10
391#define N_OVERRUN_ERROR 0x20
392#define N_DDCD 0x40
393#define N_DCTS 0x80
394
395#define N_ALL_INPUT (N_DATA_READY | N_BREAK | \
396 N_PARITY_ERROR | N_FRAMING_ERROR | \
397 N_OVERRUN_ERROR | N_DDCD | N_DCTS)
398
399#define N_ALL_OUTPUT N_OUTPUT_LOWAT
400
401#define N_ALL_ERRORS (N_PARITY_ERROR | N_FRAMING_ERROR | N_OVERRUN_ERROR)
402
403#define N_ALL (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK | \
404 N_PARITY_ERROR | N_FRAMING_ERROR | \
405 N_OVERRUN_ERROR | N_DDCD | N_DCTS)
406
407#define SER_DIVISOR(_x, clk) (((clk) + (_x) * 8) / ((_x) * 16))
408#define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
409
410/* Some masks */
411#define LCR_MASK_BITS_CHAR (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
412 | UART_LCR_WLEN7 | UART_LCR_WLEN8)
413#define LCR_MASK_STOP_BITS (UART_LCR_STOP)
414
415#define PENDING(_p) (readl(&(_p)->ip_mem->sio_ir) & _p->ip_ienb)
416#define READ_SIO_IR(_p) readl(&(_p)->ip_mem->sio_ir)
417
418/* Default to 4k buffers */
419#ifdef IOC4_1K_BUFFERS
420#define RING_BUF_SIZE 1024
421#define IOC4_BUF_SIZE_BIT 0
422#define PROD_CONS_MASK IOC4_PROD_CONS_PTR_1K
423#else
424#define RING_BUF_SIZE 4096
425#define IOC4_BUF_SIZE_BIT IOC4_SBBR_L_SIZE
426#define PROD_CONS_MASK IOC4_PROD_CONS_PTR_4K
427#endif
428
429#define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
430
431/*
432 * This is the entry saved by the driver - one per card
433 */
434struct ioc4_control {
435 int ic_irq;
436 struct {
437 /* uart ports are allocated here */
438 struct uart_port icp_uart_port;
439 /* Handy reference material */
440 struct ioc4_port *icp_port;
441 } ic_port[IOC4_NUM_SERIAL_PORTS];
442 struct ioc4_soft *ic_soft;
443};
444
445/*
446 * per-IOC4 data structure
447 */
448#define MAX_IOC4_INTR_ENTS (8 * sizeof(uint32_t))
449struct ioc4_soft {
450 struct ioc4_mem __iomem *is_ioc4_mem_addr;
451 struct ioc4_serial __iomem *is_ioc4_serial_addr;
452
453 /* Each interrupt type has an entry in the array */
454 struct ioc4_intr_type {
455
456 /*
457 * Each in-use entry in this array contains at least
458 * one nonzero bit in sd_bits; no two entries in this
459 * array have overlapping sd_bits values.
460 */
461 struct ioc4_intr_info {
462 uint32_t sd_bits;
463 ioc4_intr_func_f *sd_intr;
464 void *sd_info;
465 } is_intr_info[MAX_IOC4_INTR_ENTS];
466
467 /* Number of entries active in the above array */
468 atomic_t is_num_intrs;
469 } is_intr_type[IOC4_NUM_INTR_TYPES];
470
471 /* is_ir_lock must be held while
472 * modifying sio_ie values, so
473 * we can be sure that sio_ie is
474 * not changing when we read it
475 * along with sio_ir.
476 */
477 spinlock_t is_ir_lock; /* SIO_IE[SC] mod lock */
478};
479
480/* Local port info for each IOC4 serial ports */
481struct ioc4_port {
482 struct uart_port *ip_port;
483 /* Back ptrs for this port */
484 struct ioc4_control *ip_control;
485 struct pci_dev *ip_pdev;
486 struct ioc4_soft *ip_ioc4_soft;
487
488 /* pci mem addresses */
489 struct ioc4_mem __iomem *ip_mem;
490 struct ioc4_serial __iomem *ip_serial;
491 struct ioc4_serialregs __iomem *ip_serial_regs;
492 struct ioc4_uartregs __iomem *ip_uart_regs;
493
494 /* Ring buffer page for this port */
495 dma_addr_t ip_dma_ringbuf;
496 /* vaddr of ring buffer */
497 struct ring_buffer *ip_cpu_ringbuf;
498
499 /* Rings for this port */
500 struct ring *ip_inring;
501 struct ring *ip_outring;
502
503 /* Hook to port specific values */
504 struct hooks *ip_hooks;
505
506 spinlock_t ip_lock;
507
508 /* Various rx/tx parameters */
509 int ip_baud;
510 int ip_tx_lowat;
511 int ip_rx_timeout;
512
513 /* Copy of notification bits */
514 int ip_notify;
515
516 /* Shadow copies of various registers so we don't need to PIO
517 * read them constantly
518 */
519 uint32_t ip_ienb; /* Enabled interrupts */
520 uint32_t ip_sscr;
521 uint32_t ip_tx_prod;
522 uint32_t ip_rx_cons;
523 int ip_pci_bus_speed;
524 unsigned char ip_flags;
525};
526
527/* tx low water mark. We need to notify the driver whenever tx is getting
528 * close to empty so it can refill the tx buffer and keep things going.
529 * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
530 * have no trouble getting in more chars in time (I certainly hope so).
531 */
532#define TX_LOWAT_LATENCY 1000
533#define TX_LOWAT_HZ (1000000 / TX_LOWAT_LATENCY)
534#define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
535
536/* Flags per port */
537#define INPUT_HIGH 0x01
538#define DCD_ON 0x02
539#define LOWAT_WRITTEN 0x04
540#define READ_ABORTED 0x08
541
542/* Since each port has different register offsets and bitmasks
543 * for everything, we'll store those that we need in tables so we
544 * don't have to be constantly checking the port we are dealing with.
545 */
546struct hooks {
547 uint32_t intr_delta_dcd;
548 uint32_t intr_delta_cts;
549 uint32_t intr_tx_mt;
550 uint32_t intr_rx_timer;
551 uint32_t intr_rx_high;
552 uint32_t intr_tx_explicit;
553 uint32_t intr_dma_error;
554 uint32_t intr_clear;
555 uint32_t intr_all;
556 char rs422_select_pin;
557};
558
559static struct hooks hooks_array[IOC4_NUM_SERIAL_PORTS] = {
560 /* Values for port 0 */
561 {
562 IOC4_SIO_IR_S0_DELTA_DCD, IOC4_SIO_IR_S0_DELTA_CTS,
563 IOC4_SIO_IR_S0_TX_MT, IOC4_SIO_IR_S0_RX_TIMER,
564 IOC4_SIO_IR_S0_RX_HIGH, IOC4_SIO_IR_S0_TX_EXPLICIT,
565 IOC4_OTHER_IR_S0_MEMERR,
566 (IOC4_SIO_IR_S0_TX_MT | IOC4_SIO_IR_S0_RX_FULL |
567 IOC4_SIO_IR_S0_RX_HIGH | IOC4_SIO_IR_S0_RX_TIMER |
568 IOC4_SIO_IR_S0_DELTA_DCD | IOC4_SIO_IR_S0_DELTA_CTS |
569 IOC4_SIO_IR_S0_INT | IOC4_SIO_IR_S0_TX_EXPLICIT),
570 IOC4_SIO_IR_S0, IOC4_GPPR_UART0_MODESEL_PIN,
571 },
572
573 /* Values for port 1 */
574 {
575 IOC4_SIO_IR_S1_DELTA_DCD, IOC4_SIO_IR_S1_DELTA_CTS,
576 IOC4_SIO_IR_S1_TX_MT, IOC4_SIO_IR_S1_RX_TIMER,
577 IOC4_SIO_IR_S1_RX_HIGH, IOC4_SIO_IR_S1_TX_EXPLICIT,
578 IOC4_OTHER_IR_S1_MEMERR,
579 (IOC4_SIO_IR_S1_TX_MT | IOC4_SIO_IR_S1_RX_FULL |
580 IOC4_SIO_IR_S1_RX_HIGH | IOC4_SIO_IR_S1_RX_TIMER |
581 IOC4_SIO_IR_S1_DELTA_DCD | IOC4_SIO_IR_S1_DELTA_CTS |
582 IOC4_SIO_IR_S1_INT | IOC4_SIO_IR_S1_TX_EXPLICIT),
583 IOC4_SIO_IR_S1, IOC4_GPPR_UART1_MODESEL_PIN,
584 },
585
586 /* Values for port 2 */
587 {
588 IOC4_SIO_IR_S2_DELTA_DCD, IOC4_SIO_IR_S2_DELTA_CTS,
589 IOC4_SIO_IR_S2_TX_MT, IOC4_SIO_IR_S2_RX_TIMER,
590 IOC4_SIO_IR_S2_RX_HIGH, IOC4_SIO_IR_S2_TX_EXPLICIT,
591 IOC4_OTHER_IR_S2_MEMERR,
592 (IOC4_SIO_IR_S2_TX_MT | IOC4_SIO_IR_S2_RX_FULL |
593 IOC4_SIO_IR_S2_RX_HIGH | IOC4_SIO_IR_S2_RX_TIMER |
594 IOC4_SIO_IR_S2_DELTA_DCD | IOC4_SIO_IR_S2_DELTA_CTS |
595 IOC4_SIO_IR_S2_INT | IOC4_SIO_IR_S2_TX_EXPLICIT),
596 IOC4_SIO_IR_S2, IOC4_GPPR_UART2_MODESEL_PIN,
597 },
598
599 /* Values for port 3 */
600 {
601 IOC4_SIO_IR_S3_DELTA_DCD, IOC4_SIO_IR_S3_DELTA_CTS,
602 IOC4_SIO_IR_S3_TX_MT, IOC4_SIO_IR_S3_RX_TIMER,
603 IOC4_SIO_IR_S3_RX_HIGH, IOC4_SIO_IR_S3_TX_EXPLICIT,
604 IOC4_OTHER_IR_S3_MEMERR,
605 (IOC4_SIO_IR_S3_TX_MT | IOC4_SIO_IR_S3_RX_FULL |
606 IOC4_SIO_IR_S3_RX_HIGH | IOC4_SIO_IR_S3_RX_TIMER |
607 IOC4_SIO_IR_S3_DELTA_DCD | IOC4_SIO_IR_S3_DELTA_CTS |
608 IOC4_SIO_IR_S3_INT | IOC4_SIO_IR_S3_TX_EXPLICIT),
609 IOC4_SIO_IR_S3, IOC4_GPPR_UART3_MODESEL_PIN,
610 }
611};
612
613/* A ring buffer entry */
614struct ring_entry {
615 union {
616 struct {
617 uint32_t alldata;
618 uint32_t allsc;
619 } all;
620 struct {
621 char data[4]; /* data bytes */
622 char sc[4]; /* status/control */
623 } s;
624 } u;
625};
626
627/* Test the valid bits in any of the 4 sc chars using "allsc" member */
628#define RING_ANY_VALID \
629 ((uint32_t)(IOC4_RXSB_MODEM_VALID | IOC4_RXSB_DATA_VALID) * 0x01010101)
630
631#define ring_sc u.s.sc
632#define ring_data u.s.data
633#define ring_allsc u.all.allsc
634
635/* Number of entries per ring buffer. */
636#define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
637
638/* An individual ring */
639struct ring {
640 struct ring_entry entries[ENTRIES_PER_RING];
641};
642
643/* The whole enchilada */
644struct ring_buffer {
645 struct ring TX_0_OR_2;
646 struct ring RX_0_OR_2;
647 struct ring TX_1_OR_3;
648 struct ring RX_1_OR_3;
649};
650
651/* Get a ring from a port struct */
652#define RING(_p, _wh) &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
653
654/* Infinite loop detection.
655 */
656#define MAXITER 10000000
657
658/* Prototypes */
659static void receive_chars(struct uart_port *);
660static void handle_intr(void *arg, uint32_t sio_ir);
661
662/**
663 * write_ireg - write the interrupt regs
664 * @ioc4_soft: ptr to soft struct for this port
665 * @val: value to write
666 * @which: which register
667 * @type: which ireg set
668 */
669static inline void
670write_ireg(struct ioc4_soft *ioc4_soft, uint32_t val, int which, int type)
671{
672 struct ioc4_mem __iomem *mem = ioc4_soft->is_ioc4_mem_addr;
673 unsigned long flags;
674
675 spin_lock_irqsave(&ioc4_soft->is_ir_lock, flags);
676
677 switch (type) {
678 case IOC4_SIO_INTR_TYPE:
679 switch (which) {
680 case IOC4_W_IES:
681 writel(val, &mem->sio_ies_ro);
682 break;
683
684 case IOC4_W_IEC:
685 writel(val, &mem->sio_iec_ro);
686 break;
687 }
688 break;
689
690 case IOC4_OTHER_INTR_TYPE:
691 switch (which) {
692 case IOC4_W_IES:
693 writel(val, &mem->other_ies_ro);
694 break;
695
696 case IOC4_W_IEC:
697 writel(val, &mem->other_iec_ro);
698 break;
699 }
700 break;
701
702 default:
703 break;
704 }
705 spin_unlock_irqrestore(&ioc4_soft->is_ir_lock, flags);
706}
707
708/**
709 * set_baud - Baud rate setting code
710 * @port: port to set
711 * @baud: baud rate to use
712 */
713static int set_baud(struct ioc4_port *port, int baud)
714{
715 int actual_baud;
716 int diff;
717 int lcr;
718 unsigned short divisor;
719 struct ioc4_uartregs __iomem *uart;
720
721 divisor = SER_DIVISOR(baud, port->ip_pci_bus_speed);
722 if (!divisor)
723 return 1;
724 actual_baud = DIVISOR_TO_BAUD(divisor, port->ip_pci_bus_speed);
725
726 diff = actual_baud - baud;
727 if (diff < 0)
728 diff = -diff;
729
730 /* If we're within 1%, we've found a match */
731 if (diff * 100 > actual_baud)
732 return 1;
733
734 uart = port->ip_uart_regs;
735 lcr = readb(&uart->i4u_lcr);
736 writeb(lcr | UART_LCR_DLAB, &uart->i4u_lcr);
737 writeb((unsigned char)divisor, &uart->i4u_dll);
738 writeb((unsigned char)(divisor >> 8), &uart->i4u_dlm);
739 writeb(lcr, &uart->i4u_lcr);
740 return 0;
741}
742
743
744/**
745 * get_ioc4_port - given a uart port, return the control structure
746 * @port: uart port
747 */
748static struct ioc4_port *get_ioc4_port(struct uart_port *the_port)
749{
750 struct ioc4_control *control = dev_get_drvdata(the_port->dev);
751 int ii;
752
753 if (control) {
754 for ( ii = 0; ii < IOC4_NUM_SERIAL_PORTS; ii++ ) {
755 if (!control->ic_port[ii].icp_port)
756 continue;
757 if (the_port == control->ic_port[ii].icp_port->ip_port)
758 return control->ic_port[ii].icp_port;
759 }
760 }
761 return NULL;
762}
763
764/* The IOC4 hardware provides no atomic way to determine if interrupts
765 * are pending since two reads are required to do so. The handler must
766 * read the SIO_IR and the SIO_IES, and take the logical and of the
767 * two. When this value is zero, all interrupts have been serviced and
768 * the handler may return.
769 *
770 * This has the unfortunate "hole" that, if some other CPU or
771 * some other thread or some higher level interrupt manages to
772 * modify SIO_IE between our reads of SIO_IR and SIO_IE, we may
773 * think we have observed SIO_IR&SIO_IE==0 when in fact this
774 * condition never really occurred.
775 *
776 * To solve this, we use a simple spinlock that must be held
777 * whenever modifying SIO_IE; holding this lock while observing
778 * both SIO_IR and SIO_IE guarantees that we do not falsely
779 * conclude that no enabled interrupts are pending.
780 */
781
782static inline uint32_t
783pending_intrs(struct ioc4_soft *soft, int type)
784{
785 struct ioc4_mem __iomem *mem = soft->is_ioc4_mem_addr;
786 unsigned long flag;
787 uint32_t intrs = 0;
788
789 BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
790 || (type == IOC4_OTHER_INTR_TYPE)));
791
792 spin_lock_irqsave(&soft->is_ir_lock, flag);
793
794 switch (type) {
795 case IOC4_SIO_INTR_TYPE:
796 intrs = readl(&mem->sio_ir) & readl(&mem->sio_ies_ro);
797 break;
798
799 case IOC4_OTHER_INTR_TYPE:
800 intrs = readl(&mem->other_ir) & readl(&mem->other_ies_ro);
801
802 /* Don't process any ATA interrupte */
803 intrs &= ~(IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
804 break;
805
806 default:
807 break;
808 }
809 spin_unlock_irqrestore(&soft->is_ir_lock, flag);
810 return intrs;
811}
812
813/**
814 * port_init - Initialize the sio and ioc4 hardware for a given port
815 * called per port from attach...
816 * @port: port to initialize
817 */
818static int inline port_init(struct ioc4_port *port)
819{
820 uint32_t sio_cr;
821 struct hooks *hooks = port->ip_hooks;
822 struct ioc4_uartregs __iomem *uart;
823
824 /* Idle the IOC4 serial interface */
825 writel(IOC4_SSCR_RESET, &port->ip_serial_regs->sscr);
826
827 /* Wait until any pending bus activity for this port has ceased */
828 do
829 sio_cr = readl(&port->ip_mem->sio_cr);
830 while (!(sio_cr & IOC4_SIO_CR_SIO_DIAG_IDLE));
831
832 /* Finish reset sequence */
833 writel(0, &port->ip_serial_regs->sscr);
834
835 /* Once RESET is done, reload cached tx_prod and rx_cons values
836 * and set rings to empty by making prod == cons
837 */
838 port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
839 writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
840 port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
841 writel(port->ip_rx_cons, &port->ip_serial_regs->srcir);
842
843 /* Disable interrupts for this 16550 */
844 uart = port->ip_uart_regs;
845 writeb(0, &uart->i4u_lcr);
846 writeb(0, &uart->i4u_ier);
847
848 /* Set the default baud */
849 set_baud(port, port->ip_baud);
850
851 /* Set line control to 8 bits no parity */
852 writeb(UART_LCR_WLEN8 | 0, &uart->i4u_lcr);
853 /* UART_LCR_STOP == 1 stop */
854
855 /* Enable the FIFOs */
856 writeb(UART_FCR_ENABLE_FIFO, &uart->i4u_fcr);
857 /* then reset 16550 FIFOs */
858 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
859 &uart->i4u_fcr);
860
861 /* Clear modem control register */
862 writeb(0, &uart->i4u_mcr);
863
864 /* Clear deltas in modem status register */
865 readb(&uart->i4u_msr);
866
867 /* Only do this once per port pair */
868 if (port->ip_hooks == &hooks_array[0]
869 || port->ip_hooks == &hooks_array[2]) {
870 unsigned long ring_pci_addr;
871 uint32_t __iomem *sbbr_l;
872 uint32_t __iomem *sbbr_h;
873
874 if (port->ip_hooks == &hooks_array[0]) {
875 sbbr_l = &port->ip_serial->sbbr01_l;
876 sbbr_h = &port->ip_serial->sbbr01_h;
877 } else {
878 sbbr_l = &port->ip_serial->sbbr23_l;
879 sbbr_h = &port->ip_serial->sbbr23_h;
880 }
881
882 ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
883 DPRINT_CONFIG(("%s: ring_pci_addr 0x%lx\n",
884 __FUNCTION__, ring_pci_addr));
885
886 writel((unsigned int)((uint64_t)ring_pci_addr >> 32), sbbr_h);
887 writel((unsigned int)ring_pci_addr | IOC4_BUF_SIZE_BIT, sbbr_l);
888 }
889
890 /* Set the receive timeout value to 10 msec */
891 writel(IOC4_SRTR_HZ / 100, &port->ip_serial_regs->srtr);
892
893 /* Set rx threshold, enable DMA */
894 /* Set high water mark at 3/4 of full ring */
895 port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
896 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
897
898 /* Disable and clear all serial related interrupt bits */
899 write_ireg(port->ip_ioc4_soft, hooks->intr_clear,
900 IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
901 port->ip_ienb &= ~hooks->intr_clear;
902 writel(hooks->intr_clear, &port->ip_mem->sio_ir);
903 return 0;
904}
905
906/**
907 * handle_dma_error_intr - service any pending DMA error interrupts for the
908 * given port - 2nd level called via sd_intr
909 * @arg: handler arg
910 * @other_ir: ioc4regs
911 */
912static void handle_dma_error_intr(void *arg, uint32_t other_ir)
913{
914 struct ioc4_port *port = (struct ioc4_port *)arg;
915 struct hooks *hooks = port->ip_hooks;
916 unsigned int flags;
917
918 spin_lock_irqsave(&port->ip_lock, flags);
919
920 /* ACK the interrupt */
921 writel(hooks->intr_dma_error, &port->ip_mem->other_ir);
922
923 if (readl(&port->ip_mem->pci_err_addr_l) & IOC4_PCI_ERR_ADDR_VLD) {
924 printk(KERN_ERR
925 "PCI error address is 0x%lx, "
926 "master is serial port %c %s\n",
927 (((uint64_t)readl(&port->ip_mem->pci_err_addr_h)
928 << 32)
929 | readl(&port->ip_mem->pci_err_addr_l))
930 & IOC4_PCI_ERR_ADDR_ADDR_MSK, '1' +
931 ((char)(readl(&port->ip_mem-> pci_err_addr_l) &
932 IOC4_PCI_ERR_ADDR_MST_NUM_MSK) >> 1),
933 (readl(&port->ip_mem->pci_err_addr_l)
934 & IOC4_PCI_ERR_ADDR_MST_TYP_MSK)
935 ? "RX" : "TX");
936
937 if (readl(&port->ip_mem->pci_err_addr_l)
938 & IOC4_PCI_ERR_ADDR_MUL_ERR) {
939 printk(KERN_ERR
940 "Multiple errors occurred\n");
941 }
942 }
943 spin_unlock_irqrestore(&port->ip_lock, flags);
944
945 /* Re-enable DMA error interrupts */
946 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error, IOC4_W_IES,
947 IOC4_OTHER_INTR_TYPE);
948}
949
950/**
951 * intr_connect - interrupt connect function
952 * @soft: soft struct for this card
953 * @type: interrupt type
954 * @intrbits: bit pattern to set
955 * @intr: handler function
956 * @info: handler arg
957 */
958static void
959intr_connect(struct ioc4_soft *soft, int type,
960 uint32_t intrbits, ioc4_intr_func_f * intr, void *info)
961{
962 int i;
963 struct ioc4_intr_info *intr_ptr;
964
965 BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
966 || (type == IOC4_OTHER_INTR_TYPE)));
967
968 i = atomic_inc(&soft-> is_intr_type[type].is_num_intrs) - 1;
969 BUG_ON(!(i < MAX_IOC4_INTR_ENTS || (printk("i %d\n", i), 0)));
970
971 /* Save off the lower level interrupt handler */
972 intr_ptr = &soft->is_intr_type[type].is_intr_info[i];
973 intr_ptr->sd_bits = intrbits;
974 intr_ptr->sd_intr = intr;
975 intr_ptr->sd_info = info;
976}
977
978/**
979 * ioc4_intr - Top level IOC4 interrupt handler.
980 * @irq: irq value
981 * @arg: handler arg
982 * @regs: registers
983 */
984static irqreturn_t ioc4_intr(int irq, void *arg, struct pt_regs *regs)
985{
986 struct ioc4_soft *soft;
987 uint32_t this_ir, this_mir;
988 int xx, num_intrs = 0;
989 int intr_type;
990 int handled = 0;
991 struct ioc4_intr_info *ii;
992
993 soft = arg;
994 for (intr_type = 0; intr_type < IOC4_NUM_INTR_TYPES; intr_type++) {
995 num_intrs = (int)atomic_read(
996 &soft->is_intr_type[intr_type].is_num_intrs);
997
998 this_mir = this_ir = pending_intrs(soft, intr_type);
999
1000 /* Farm out the interrupt to the various drivers depending on
1001 * which interrupt bits are set.
1002 */
1003 for (xx = 0; xx < num_intrs; xx++) {
1004 ii = &soft->is_intr_type[intr_type].is_intr_info[xx];
1005 if ((this_mir = this_ir & ii->sd_bits)) {
1006 /* Disable owned interrupts, call handler */
1007 handled++;
1008 write_ireg(soft, ii->sd_bits, IOC4_W_IEC,
1009 intr_type);
1010 ii->sd_intr(ii->sd_info, this_mir);
1011 this_ir &= ~this_mir;
1012 }
1013 }
1014 if (this_ir) {
1015 printk(KERN_ERR
1016 "unknown IOC4 %s interrupt 0x%x, sio_ir = 0x%x,"
1017 " sio_ies = 0x%x, other_ir = 0x%x :"
1018 "other_ies = 0x%x\n",
1019 (intr_type == IOC4_SIO_INTR_TYPE) ? "sio" :
1020 "other", this_ir,
1021 readl(&soft->is_ioc4_mem_addr->sio_ir),
1022 readl(&soft->is_ioc4_mem_addr->sio_ies_ro),
1023 readl(&soft->is_ioc4_mem_addr->other_ir),
1024 readl(&soft->is_ioc4_mem_addr->other_ies_ro));
1025 }
1026 }
1027#ifdef DEBUG_INTERRUPTS
1028 {
1029 struct ioc4_mem __iomem *mem = soft->is_ioc4_mem_addr;
1030 spinlock_t *lp = &soft->is_ir_lock;
1031 unsigned long flag;
1032
1033 spin_lock_irqsave(&soft->is_ir_lock, flag);
1034 printk ("%s : %d : mem 0x%p sio_ir 0x%x sio_ies_ro 0x%x "
1035 "other_ir 0x%x other_ies_ro 0x%x mask 0x%x\n",
1036 __FUNCTION__, __LINE__,
1037 (void *)mem, readl(&mem->sio_ir),
1038 readl(&mem->sio_ies_ro),
1039 readl(&mem->other_ir),
1040 readl(&mem->other_ies_ro),
1041 IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
1042 spin_unlock_irqrestore(&soft->is_ir_lock, flag);
1043 }
1044#endif
1045 return handled ? IRQ_HANDLED : IRQ_NONE;
1046}
1047
1048/**
1049 * ioc4_attach_local - Device initialization.
1050 * Called at *_attach() time for each
1051 * IOC4 with serial ports in the system.
1052 * @control: ioc4_control ptr
1053 * @pdev: PCI handle for this device
1054 * @soft: soft struct for this device
1055 * @ioc4: ioc4 mem space
1056 */
1057static int inline ioc4_attach_local(struct pci_dev *pdev,
1058 struct ioc4_control *control,
1059 struct ioc4_soft *soft, void __iomem *ioc4_mem,
1060 void __iomem *ioc4_serial)
1061{
1062 struct ioc4_port *port;
1063 struct ioc4_port *ports[IOC4_NUM_SERIAL_PORTS];
1064 int port_number;
1065 uint16_t ioc4_revid_min = 62;
1066 uint16_t ioc4_revid;
1067
1068 /* IOC4 firmware must be at least rev 62 */
1069 pci_read_config_word(pdev, PCI_COMMAND_SPECIAL, &ioc4_revid);
1070
1071 printk(KERN_INFO "IOC4 firmware revision %d\n", ioc4_revid);
1072 if (ioc4_revid < ioc4_revid_min) {
1073 printk(KERN_WARNING
1074 "IOC4 serial not supported on firmware rev %d, "
1075 "please upgrade to rev %d or higher\n",
1076 ioc4_revid, ioc4_revid_min);
1077 return -EPERM;
1078 }
1079 BUG_ON(ioc4_mem == NULL);
1080 BUG_ON(ioc4_serial == NULL);
1081
1082 /* Create port structures for each port */
1083 for (port_number = 0; port_number < IOC4_NUM_SERIAL_PORTS;
1084 port_number++) {
1085 port = kmalloc(sizeof(struct ioc4_port), GFP_KERNEL);
1086 if (!port) {
1087 printk(KERN_WARNING
1088 "IOC4 serial memory not available for port\n");
1089 return -ENOMEM;
1090 }
1091 memset(port, 0, sizeof(struct ioc4_port));
1092
1093 /* we need to remember the previous ones, to point back to
1094 * them farther down - setting up the ring buffers.
1095 */
1096 ports[port_number] = port;
1097
1098 /* Allocate buffers and jumpstart the hardware. */
1099 control->ic_port[port_number].icp_port = port;
1100 port->ip_ioc4_soft = soft;
1101 port->ip_pdev = pdev;
1102 port->ip_ienb = 0;
1103 port->ip_pci_bus_speed = IOC4_SER_XIN_CLK;
1104 port->ip_baud = 9600;
1105 port->ip_control = control;
1106 port->ip_mem = ioc4_mem;
1107 port->ip_serial = ioc4_serial;
1108
1109 /* point to the right hook */
1110 port->ip_hooks = &hooks_array[port_number];
1111
1112 /* Get direct hooks to the serial regs and uart regs
1113 * for this port
1114 */
1115 switch (port_number) {
1116 case 0:
1117 port->ip_serial_regs = &(port->ip_serial->port_0);
1118 port->ip_uart_regs = &(port->ip_serial->uart_0);
1119 break;
1120 case 1:
1121 port->ip_serial_regs = &(port->ip_serial->port_1);
1122 port->ip_uart_regs = &(port->ip_serial->uart_1);
1123 break;
1124 case 2:
1125 port->ip_serial_regs = &(port->ip_serial->port_2);
1126 port->ip_uart_regs = &(port->ip_serial->uart_2);
1127 break;
1128 default:
1129 case 3:
1130 port->ip_serial_regs = &(port->ip_serial->port_3);
1131 port->ip_uart_regs = &(port->ip_serial->uart_3);
1132 break;
1133 }
1134
1135 /* ring buffers are 1 to a pair of ports */
1136 if (port_number && (port_number & 1)) {
1137 /* odd use the evens buffer */
1138 port->ip_dma_ringbuf =
1139 ports[port_number - 1]->ip_dma_ringbuf;
1140 port->ip_cpu_ringbuf =
1141 ports[port_number - 1]->ip_cpu_ringbuf;
1142 port->ip_inring = RING(port, RX_1_OR_3);
1143 port->ip_outring = RING(port, TX_1_OR_3);
1144
1145 } else {
1146 if (port->ip_dma_ringbuf == 0) {
1147 port->ip_cpu_ringbuf = pci_alloc_consistent
1148 (pdev, TOTAL_RING_BUF_SIZE,
1149 &port->ip_dma_ringbuf);
1150
1151 }
1152 BUG_ON(!((((int64_t)port->ip_dma_ringbuf) &
1153 (TOTAL_RING_BUF_SIZE - 1)) == 0));
1154 DPRINT_CONFIG(("%s : ip_cpu_ringbuf 0x%p "
1155 "ip_dma_ringbuf 0x%p\n",
1156 __FUNCTION__,
1157 (void *)port->ip_cpu_ringbuf,
1158 (void *)port->ip_dma_ringbuf));
1159 port->ip_inring = RING(port, RX_0_OR_2);
1160 port->ip_outring = RING(port, TX_0_OR_2);
1161 }
1162 DPRINT_CONFIG(("%s : port %d [addr 0x%p] control 0x%p",
1163 __FUNCTION__,
1164 port_number, (void *)port, (void *)control));
1165 DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
1166 (void *)port->ip_serial_regs,
1167 (void *)port->ip_uart_regs));
1168
1169 /* Initialize the hardware for IOC4 */
1170 port_init(port);
1171
1172 DPRINT_CONFIG(("%s: port_number %d port 0x%p inring 0x%p "
1173 "outring 0x%p\n",
1174 __FUNCTION__,
1175 port_number, (void *)port,
1176 (void *)port->ip_inring,
1177 (void *)port->ip_outring));
1178
1179 /* Attach interrupt handlers */
1180 intr_connect(soft, IOC4_SIO_INTR_TYPE,
1181 GET_SIO_IR(port_number),
1182 handle_intr, port);
1183
1184 intr_connect(soft, IOC4_OTHER_INTR_TYPE,
1185 GET_OTHER_IR(port_number),
1186 handle_dma_error_intr, port);
1187 }
1188 return 0;
1189}
1190
1191/**
1192 * enable_intrs - enable interrupts
1193 * @port: port to enable
1194 * @mask: mask to use
1195 */
1196static void enable_intrs(struct ioc4_port *port, uint32_t mask)
1197{
1198 struct hooks *hooks = port->ip_hooks;
1199
1200 if ((port->ip_ienb & mask) != mask) {
1201 write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IES,
1202 IOC4_SIO_INTR_TYPE);
1203 port->ip_ienb |= mask;
1204 }
1205
1206 if (port->ip_ienb)
1207 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
1208 IOC4_W_IES, IOC4_OTHER_INTR_TYPE);
1209}
1210
1211/**
1212 * local_open - local open a port
1213 * @port: port to open
1214 */
1215static inline int local_open(struct ioc4_port *port)
1216{
1217 int spiniter = 0;
1218
1219 port->ip_flags = 0;
1220
1221 /* Pause the DMA interface if necessary */
1222 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1223 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1224 &port->ip_serial_regs->sscr);
1225 while((readl(&port->ip_serial_regs-> sscr)
1226 & IOC4_SSCR_PAUSE_STATE) == 0) {
1227 spiniter++;
1228 if (spiniter > MAXITER) {
1229 return -1;
1230 }
1231 }
1232 }
1233
1234 /* Reset the input fifo. If the uart received chars while the port
1235 * was closed and DMA is not enabled, the uart may have a bunch of
1236 * chars hanging around in its rx fifo which will not be discarded
1237 * by rclr in the upper layer. We must get rid of them here.
1238 */
1239 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
1240 &port->ip_uart_regs->i4u_fcr);
1241
1242 writeb(UART_LCR_WLEN8, &port->ip_uart_regs->i4u_lcr);
1243 /* UART_LCR_STOP == 1 stop */
1244
1245 /* Re-enable DMA, set default threshold to intr whenever there is
1246 * data available.
1247 */
1248 port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
1249 port->ip_sscr |= 1; /* default threshold */
1250
1251 /* Plug in the new sscr. This implicitly clears the DMA_PAUSE
1252 * flag if it was set above
1253 */
1254 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1255 port->ip_tx_lowat = 1;
1256 return 0;
1257}
1258
1259/**
1260 * set_rx_timeout - Set rx timeout and threshold values.
1261 * @port: port to use
1262 * @timeout: timeout value in ticks
1263 */
1264static inline int set_rx_timeout(struct ioc4_port *port, int timeout)
1265{
1266 int threshold;
1267
1268 port->ip_rx_timeout = timeout;
1269
1270 /* Timeout is in ticks. Let's figure out how many chars we
1271 * can receive at the current baud rate in that interval
1272 * and set the rx threshold to that amount. There are 4 chars
1273 * per ring entry, so we'll divide the number of chars that will
1274 * arrive in timeout by 4.
1275 */
1276 threshold = timeout * port->ip_baud / 10 / HZ / 4;
1277 if (threshold == 0)
1278 threshold = 1; /* otherwise we'll intr all the time! */
1279
1280 if ((unsigned)threshold > (unsigned)IOC4_SSCR_RX_THRESHOLD)
1281 return 1;
1282
1283 port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
1284 port->ip_sscr |= threshold;
1285
1286 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1287
1288 /* Now set the rx timeout to the given value */
1289 timeout = timeout * IOC4_SRTR_HZ / HZ;
1290 if (timeout > IOC4_SRTR_CNT)
1291 timeout = IOC4_SRTR_CNT;
1292
1293 writel(timeout, &port->ip_serial_regs->srtr);
1294 return 0;
1295}
1296
1297/**
1298 * config_port - config the hardware
1299 * @port: port to config
1300 * @baud: baud rate for the port
1301 * @byte_size: data size
1302 * @stop_bits: number of stop bits
1303 * @parenb: parity enable ?
1304 * @parodd: odd parity ?
1305 */
1306static inline int
1307config_port(struct ioc4_port *port,
1308 int baud, int byte_size, int stop_bits, int parenb, int parodd)
1309{
1310 char lcr, sizebits;
1311 int spiniter = 0;
1312
1313 DPRINT_CONFIG(("%s: baud %d byte_size %d stop %d parenb %d parodd %d\n",
1314 __FUNCTION__, baud, byte_size, stop_bits, parenb, parodd));
1315
1316 if (set_baud(port, baud))
1317 return 1;
1318
1319 switch (byte_size) {
1320 case 5:
1321 sizebits = UART_LCR_WLEN5;
1322 break;
1323 case 6:
1324 sizebits = UART_LCR_WLEN6;
1325 break;
1326 case 7:
1327 sizebits = UART_LCR_WLEN7;
1328 break;
1329 case 8:
1330 sizebits = UART_LCR_WLEN8;
1331 break;
1332 default:
1333 return 1;
1334 }
1335
1336 /* Pause the DMA interface if necessary */
1337 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1338 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1339 &port->ip_serial_regs->sscr);
1340 while((readl(&port->ip_serial_regs->sscr)
1341 & IOC4_SSCR_PAUSE_STATE) == 0) {
1342 spiniter++;
1343 if (spiniter > MAXITER)
1344 return -1;
1345 }
1346 }
1347
1348 /* Clear relevant fields in lcr */
1349 lcr = readb(&port->ip_uart_regs->i4u_lcr);
1350 lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
1351 UART_LCR_PARITY | LCR_MASK_STOP_BITS);
1352
1353 /* Set byte size in lcr */
1354 lcr |= sizebits;
1355
1356 /* Set parity */
1357 if (parenb) {
1358 lcr |= UART_LCR_PARITY;
1359 if (!parodd)
1360 lcr |= UART_LCR_EPAR;
1361 }
1362
1363 /* Set stop bits */
1364 if (stop_bits)
1365 lcr |= UART_LCR_STOP /* 2 stop bits */ ;
1366
1367 writeb(lcr, &port->ip_uart_regs->i4u_lcr);
1368
1369 /* Re-enable the DMA interface if necessary */
1370 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1371 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1372 }
1373 port->ip_baud = baud;
1374
1375 /* When we get within this number of ring entries of filling the
1376 * entire ring on tx, place an EXPLICIT intr to generate a lowat
1377 * notification when output has drained.
1378 */
1379 port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
1380 if (port->ip_tx_lowat == 0)
1381 port->ip_tx_lowat = 1;
1382
1383 set_rx_timeout(port, port->ip_rx_timeout);
1384
1385 return 0;
1386}
1387
1388/**
1389 * do_write - Write bytes to the port. Returns the number of bytes
1390 * actually written. Called from transmit_chars
1391 * @port: port to use
1392 * @buf: the stuff to write
1393 * @len: how many bytes in 'buf'
1394 */
1395static inline int do_write(struct ioc4_port *port, char *buf, int len)
1396{
1397 int prod_ptr, cons_ptr, total = 0;
1398 struct ring *outring;
1399 struct ring_entry *entry;
1400 struct hooks *hooks = port->ip_hooks;
1401
1402 BUG_ON(!(len >= 0));
1403
1404 prod_ptr = port->ip_tx_prod;
1405 cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
1406 outring = port->ip_outring;
1407
1408 /* Maintain a 1-entry red-zone. The ring buffer is full when
1409 * (cons - prod) % ring_size is 1. Rather than do this subtraction
1410 * in the body of the loop, I'll do it now.
1411 */
1412 cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
1413
1414 /* Stuff the bytes into the output */
1415 while ((prod_ptr != cons_ptr) && (len > 0)) {
1416 int xx;
1417
1418 /* Get 4 bytes (one ring entry) at a time */
1419 entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
1420
1421 /* Invalidate all entries */
1422 entry->ring_allsc = 0;
1423
1424 /* Copy in some bytes */
1425 for (xx = 0; (xx < 4) && (len > 0); xx++) {
1426 entry->ring_data[xx] = *buf++;
1427 entry->ring_sc[xx] = IOC4_TXCB_VALID;
1428 len--;
1429 total++;
1430 }
1431
1432 /* If we are within some small threshold of filling up the
1433 * entire ring buffer, we must place an EXPLICIT intr here
1434 * to generate a lowat interrupt in case we subsequently
1435 * really do fill up the ring and the caller goes to sleep.
1436 * No need to place more than one though.
1437 */
1438 if (!(port->ip_flags & LOWAT_WRITTEN) &&
1439 ((cons_ptr - prod_ptr) & PROD_CONS_MASK)
1440 <= port->ip_tx_lowat
1441 * (int)sizeof(struct ring_entry)) {
1442 port->ip_flags |= LOWAT_WRITTEN;
1443 entry->ring_sc[0] |= IOC4_TXCB_INT_WHEN_DONE;
1444 }
1445
1446 /* Go on to next entry */
1447 prod_ptr += sizeof(struct ring_entry);
1448 prod_ptr &= PROD_CONS_MASK;
1449 }
1450
1451 /* If we sent something, start DMA if necessary */
1452 if (total > 0 && !(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
1453 port->ip_sscr |= IOC4_SSCR_DMA_EN;
1454 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1455 }
1456
1457 /* Store the new producer pointer. If tx is disabled, we stuff the
1458 * data into the ring buffer, but we don't actually start tx.
1459 */
1460 if (!uart_tx_stopped(port->ip_port)) {
1461 writel(prod_ptr, &port->ip_serial_regs->stpir);
1462
1463 /* If we are now transmitting, enable tx_mt interrupt so we
1464 * can disable DMA if necessary when the tx finishes.
1465 */
1466 if (total > 0)
1467 enable_intrs(port, hooks->intr_tx_mt);
1468 }
1469 port->ip_tx_prod = prod_ptr;
1470 return total;
1471}
1472
1473/**
1474 * disable_intrs - disable interrupts
1475 * @port: port to enable
1476 * @mask: mask to use
1477 */
1478static void disable_intrs(struct ioc4_port *port, uint32_t mask)
1479{
1480 struct hooks *hooks = port->ip_hooks;
1481
1482 if (port->ip_ienb & mask) {
1483 write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IEC,
1484 IOC4_SIO_INTR_TYPE);
1485 port->ip_ienb &= ~mask;
1486 }
1487
1488 if (!port->ip_ienb)
1489 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
1490 IOC4_W_IEC, IOC4_OTHER_INTR_TYPE);
1491}
1492
1493/**
1494 * set_notification - Modify event notification
1495 * @port: port to use
1496 * @mask: events mask
1497 * @set_on: set ?
1498 */
1499static int set_notification(struct ioc4_port *port, int mask, int set_on)
1500{
1501 struct hooks *hooks = port->ip_hooks;
1502 uint32_t intrbits, sscrbits;
1503
1504 BUG_ON(!mask);
1505
1506 intrbits = sscrbits = 0;
1507
1508 if (mask & N_DATA_READY)
1509 intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
1510 if (mask & N_OUTPUT_LOWAT)
1511 intrbits |= hooks->intr_tx_explicit;
1512 if (mask & N_DDCD) {
1513 intrbits |= hooks->intr_delta_dcd;
1514 sscrbits |= IOC4_SSCR_RX_RING_DCD;
1515 }
1516 if (mask & N_DCTS)
1517 intrbits |= hooks->intr_delta_cts;
1518
1519 if (set_on) {
1520 enable_intrs(port, intrbits);
1521 port->ip_notify |= mask;
1522 port->ip_sscr |= sscrbits;
1523 } else {
1524 disable_intrs(port, intrbits);
1525 port->ip_notify &= ~mask;
1526 port->ip_sscr &= ~sscrbits;
1527 }
1528
1529 /* We require DMA if either DATA_READY or DDCD notification is
1530 * currently requested. If neither of these is requested and
1531 * there is currently no tx in progress, DMA may be disabled.
1532 */
1533 if (port->ip_notify & (N_DATA_READY | N_DDCD))
1534 port->ip_sscr |= IOC4_SSCR_DMA_EN;
1535 else if (!(port->ip_ienb & hooks->intr_tx_mt))
1536 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
1537
1538 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1539 return 0;
1540}
1541
1542/**
1543 * set_mcr - set the master control reg
1544 * @the_port: port to use
1545 * @set: set ?
1546 * @mask1: mcr mask
1547 * @mask2: shadow mask
1548 */
1549static inline int set_mcr(struct uart_port *the_port, int set,
1550 int mask1, int mask2)
1551{
1552 struct ioc4_port *port = get_ioc4_port(the_port);
1553 uint32_t shadow;
1554 int spiniter = 0;
1555 char mcr;
1556
1557 if (!port)
1558 return -1;
1559
1560 /* Pause the DMA interface if necessary */
1561 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1562 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1563 &port->ip_serial_regs->sscr);
1564 while ((readl(&port->ip_serial_regs->sscr)
1565 & IOC4_SSCR_PAUSE_STATE) == 0) {
1566 spiniter++;
1567 if (spiniter > MAXITER)
1568 return -1;
1569 }
1570 }
1571 shadow = readl(&port->ip_serial_regs->shadow);
1572 mcr = (shadow & 0xff000000) >> 24;
1573
1574 /* Set new value */
1575 if (set) {
1576 mcr |= mask1;
1577 shadow |= mask2;
1578 } else {
1579 mcr &= ~mask1;
1580 shadow &= ~mask2;
1581 }
1582 writeb(mcr, &port->ip_uart_regs->i4u_mcr);
1583 writel(shadow, &port->ip_serial_regs->shadow);
1584
1585 /* Re-enable the DMA interface if necessary */
1586 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1587 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1588 }
1589 return 0;
1590}
1591
1592/**
1593 * ioc4_set_proto - set the protocol for the port
1594 * @port: port to use
1595 * @proto: protocol to use
1596 */
1597static int ioc4_set_proto(struct ioc4_port *port, enum sio_proto proto)
1598{
1599 struct hooks *hooks = port->ip_hooks;
1600
1601 switch (proto) {
1602 case PROTO_RS232:
1603 /* Clear the appropriate GIO pin */
1604 writel(0, (&port->ip_mem->gppr_0 +
1605 hooks->rs422_select_pin));
1606 break;
1607
1608 case PROTO_RS422:
1609 /* Set the appropriate GIO pin */
1610 writel(1, (&port->ip_mem->gppr_0 +
1611 hooks->rs422_select_pin));
1612 break;
1613
1614 default:
1615 return 1;
1616 }
1617 return 0;
1618}
1619
1620/**
1621 * transmit_chars - upper level write, called with ip_lock
1622 * @the_port: port to write
1623 */
1624static void transmit_chars(struct uart_port *the_port)
1625{
1626 int xmit_count, tail, head;
1627 int result;
1628 char *start;
1629 struct tty_struct *tty;
1630 struct ioc4_port *port = get_ioc4_port(the_port);
1631 struct uart_info *info;
1632
1633 if (!the_port)
1634 return;
1635 if (!port)
1636 return;
1637
1638 info = the_port->info;
1639 tty = info->tty;
1640
1641 if (uart_circ_empty(&info->xmit) || uart_tx_stopped(the_port)) {
1642 /* Nothing to do or hw stopped */
1643 set_notification(port, N_ALL_OUTPUT, 0);
1644 return;
1645 }
1646
1647 head = info->xmit.head;
1648 tail = info->xmit.tail;
1649 start = (char *)&info->xmit.buf[tail];
1650
1651 /* write out all the data or until the end of the buffer */
1652 xmit_count = (head < tail) ? (UART_XMIT_SIZE - tail) : (head - tail);
1653 if (xmit_count > 0) {
1654 result = do_write(port, start, xmit_count);
1655 if (result > 0) {
1656 /* booking */
1657 xmit_count -= result;
1658 the_port->icount.tx += result;
1659 /* advance the pointers */
1660 tail += result;
1661 tail &= UART_XMIT_SIZE - 1;
1662 info->xmit.tail = tail;
1663 start = (char *)&info->xmit.buf[tail];
1664 }
1665 }
1666 if (uart_circ_chars_pending(&info->xmit) < WAKEUP_CHARS)
1667 uart_write_wakeup(the_port);
1668
1669 if (uart_circ_empty(&info->xmit)) {
1670 set_notification(port, N_OUTPUT_LOWAT, 0);
1671 } else {
1672 set_notification(port, N_OUTPUT_LOWAT, 1);
1673 }
1674}
1675
1676/**
1677 * ioc4_change_speed - change the speed of the port
1678 * @the_port: port to change
1679 * @new_termios: new termios settings
1680 * @old_termios: old termios settings
1681 */
1682static void
1683ioc4_change_speed(struct uart_port *the_port,
1684 struct termios *new_termios, struct termios *old_termios)
1685{
1686 struct ioc4_port *port = get_ioc4_port(the_port);
1687 int baud, bits;
1688 unsigned cflag, cval;
1689 int new_parity = 0, new_parity_enable = 0, new_stop = 1, new_data = 8;
1690 struct uart_info *info = the_port->info;
1691
1692 cflag = new_termios->c_cflag;
1693
1694 switch (cflag & CSIZE) {
1695 case CS5:
1696 new_data = 5;
1697 cval = 0x00;
1698 bits = 7;
1699 break;
1700 case CS6:
1701 new_data = 6;
1702 cval = 0x01;
1703 bits = 8;
1704 break;
1705 case CS7:
1706 new_data = 7;
1707 cval = 0x02;
1708 bits = 9;
1709 break;
1710 case CS8:
1711 new_data = 8;
1712 cval = 0x03;
1713 bits = 10;
1714 break;
1715 default:
1716 /* cuz we always need a default ... */
1717 new_data = 5;
1718 cval = 0x00;
1719 bits = 7;
1720 break;
1721 }
1722 if (cflag & CSTOPB) {
1723 cval |= 0x04;
1724 bits++;
1725 new_stop = 1;
1726 }
1727 if (cflag & PARENB) {
1728 cval |= UART_LCR_PARITY;
1729 bits++;
1730 new_parity_enable = 1;
1731 }
1732 if (cflag & PARODD) {
1733 cval |= UART_LCR_EPAR;
1734 new_parity = 1;
1735 }
1736 if (cflag & IGNPAR) {
1737 cval &= ~UART_LCR_PARITY;
1738 new_parity_enable = 0;
1739 }
1740 baud = uart_get_baud_rate(the_port, new_termios, old_termios,
1741 MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
1742 DPRINT_CONFIG(("%s: returned baud %d\n", __FUNCTION__, baud));
1743
1744 /* default is 9600 */
1745 if (!baud)
1746 baud = 9600;
1747
1748 if (!the_port->fifosize)
1749 the_port->fifosize = IOC4_MAX_CHARS;
1750 the_port->timeout = ((the_port->fifosize * HZ * bits) / (baud / 10));
1751 the_port->timeout += HZ / 50; /* Add .02 seconds of slop */
1752
1753 the_port->ignore_status_mask = N_ALL_INPUT;
1754
1755 if (I_IGNPAR(info->tty))
1756 the_port->ignore_status_mask &= ~(N_PARITY_ERROR
1757 | N_FRAMING_ERROR);
1758 if (I_IGNBRK(info->tty)) {
1759 the_port->ignore_status_mask &= ~N_BREAK;
1760 if (I_IGNPAR(info->tty))
1761 the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
1762 }
1763 if (!(cflag & CREAD)) {
1764 /* ignore everything */
1765 the_port->ignore_status_mask &= ~N_DATA_READY;
1766 }
1767
1768 if (cflag & CRTSCTS)
1769 info->flags |= ASYNC_CTS_FLOW;
1770 else
1771 info->flags &= ~ASYNC_CTS_FLOW;
1772
1773 /* Set the configuration and proper notification call */
1774 DPRINT_CONFIG(("%s : port 0x%p cflag 0%o "
1775 "config_port(baud %d data %d stop %d p enable %d parity %d),"
1776 " notification 0x%x\n",
1777 __FUNCTION__, (void *)port, cflag, baud, new_data, new_stop,
1778 new_parity_enable, new_parity, the_port->ignore_status_mask));
1779
1780 if ((config_port(port, baud, /* baud */
1781 new_data, /* byte size */
1782 new_stop, /* stop bits */
1783 new_parity_enable, /* set parity */
1784 new_parity)) >= 0) { /* parity 1==odd */
1785 set_notification(port, the_port->ignore_status_mask, 1);
1786 }
1787}
1788
1789/**
1790 * ic4_startup_local - Start up the serial port - returns >= 0 if no errors
1791 * @the_port: Port to operate on
1792 */
1793static inline int ic4_startup_local(struct uart_port *the_port)
1794{
1795 int retval = 0;
1796 struct ioc4_port *port;
1797 struct uart_info *info;
1798
1799 if (!the_port)
1800 return -1;
1801
1802 port = get_ioc4_port(the_port);
1803 if (!port)
1804 return -1;
1805
1806 info = the_port->info;
1807 if (info->flags & UIF_INITIALIZED) {
1808 return retval;
1809 }
1810
1811 if (info->tty) {
1812 set_bit(TTY_IO_ERROR, &info->tty->flags);
1813 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1814 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1815 info->tty->alt_speed = 57600;
1816 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1817 info->tty->alt_speed = 115200;
1818 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1819 info->tty->alt_speed = 230400;
1820 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1821 info->tty->alt_speed = 460800;
1822 }
1823 local_open(port);
1824
1825 /* set the speed of the serial port */
1826 ioc4_change_speed(the_port, info->tty->termios, (struct termios *)0);
1827
1828 /* enable hardware flow control - after ioc4_change_speed because
1829 * ASYNC_CTS_FLOW is set there */
1830 if (info->flags & ASYNC_CTS_FLOW) {
1831 port->ip_sscr |= IOC4_SSCR_HFC_EN;
1832 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1833 }
1834 info->flags |= UIF_INITIALIZED;
1835 return 0;
1836}
1837
1838/*
1839 * ioc4_cb_output_lowat - called when the output low water mark is hit
1840 * @port: port to output
1841 */
1842static void ioc4_cb_output_lowat(struct ioc4_port *port)
1843{
1844 /* ip_lock is set on the call here */
1845 if (port->ip_port) {
1846 transmit_chars(port->ip_port);
1847 }
1848}
1849
1850
1851/**
1852 * handle_intr - service any interrupts for the given port - 2nd level
1853 * called via sd_intr
1854 * @arg: handler arg
1855 * @sio_ir: ioc4regs
1856 */
1857static void handle_intr(void *arg, uint32_t sio_ir)
1858{
1859 struct ioc4_port *port = (struct ioc4_port *)arg;
1860 struct hooks *hooks = port->ip_hooks;
1861 unsigned int rx_high_rd_aborted = 0;
1862 unsigned int flags;
1863 struct uart_port *the_port;
1864 int loop_counter;
1865
1866 /* Possible race condition here: The tx_mt interrupt bit may be
1867 * cleared without the intervention of the interrupt handler,
1868 * e.g. by a write. If the top level interrupt handler reads a
1869 * tx_mt, then some other processor does a write, starting up
1870 * output, then we come in here, see the tx_mt and stop DMA, the
1871 * output started by the other processor will hang. Thus we can
1872 * only rely on tx_mt being legitimate if it is read while the
1873 * port lock is held. Therefore this bit must be ignored in the
1874 * passed in interrupt mask which was read by the top level
1875 * interrupt handler since the port lock was not held at the time
1876 * it was read. We can only rely on this bit being accurate if it
1877 * is read while the port lock is held. So we'll clear it for now,
1878 * and reload it later once we have the port lock.
1879 */
1880 sio_ir &= ~(hooks->intr_tx_mt);
1881
1882 spin_lock_irqsave(&port->ip_lock, flags);
1883
1884 loop_counter = MAXITER; /* to avoid hangs */
1885
1886 do {
1887 uint32_t shadow;
1888
1889 if ( loop_counter-- <= 0 ) {
1890 printk(KERN_WARNING "IOC4 serial: "
1891 "possible hang condition/"
1892 "port stuck on interrupt.\n");
1893 break;
1894 }
1895
1896 /* Handle a DCD change */
1897 if (sio_ir & hooks->intr_delta_dcd) {
1898 /* ACK the interrupt */
1899 writel(hooks->intr_delta_dcd,
1900 &port->ip_mem->sio_ir);
1901
1902 shadow = readl(&port->ip_serial_regs->shadow);
1903
1904 if ((port->ip_notify & N_DDCD)
1905 && (shadow & IOC4_SHADOW_DCD)
1906 && (port->ip_port)) {
1907 the_port = port->ip_port;
1908 the_port->icount.dcd = 1;
1909 wake_up_interruptible
1910 (&the_port-> info->delta_msr_wait);
1911 } else if ((port->ip_notify & N_DDCD)
1912 && !(shadow & IOC4_SHADOW_DCD)) {
1913 /* Flag delta DCD/no DCD */
1914 port->ip_flags |= DCD_ON;
1915 }
1916 }
1917
1918 /* Handle a CTS change */
1919 if (sio_ir & hooks->intr_delta_cts) {
1920 /* ACK the interrupt */
1921 writel(hooks->intr_delta_cts,
1922 &port->ip_mem->sio_ir);
1923
1924 shadow = readl(&port->ip_serial_regs->shadow);
1925
1926 if ((port->ip_notify & N_DCTS)
1927 && (port->ip_port)) {
1928 the_port = port->ip_port;
1929 the_port->icount.cts =
1930 (shadow & IOC4_SHADOW_CTS) ? 1 : 0;
1931 wake_up_interruptible
1932 (&the_port->info->delta_msr_wait);
1933 }
1934 }
1935
1936 /* rx timeout interrupt. Must be some data available. Put this
1937 * before the check for rx_high since servicing this condition
1938 * may cause that condition to clear.
1939 */
1940 if (sio_ir & hooks->intr_rx_timer) {
1941 /* ACK the interrupt */
1942 writel(hooks->intr_rx_timer,
1943 &port->ip_mem->sio_ir);
1944
1945 if ((port->ip_notify & N_DATA_READY)
1946 && (port->ip_port)) {
1947 /* ip_lock is set on call here */
1948 receive_chars(port->ip_port);
1949 }
1950 }
1951
1952 /* rx high interrupt. Must be after rx_timer. */
1953 else if (sio_ir & hooks->intr_rx_high) {
1954 /* Data available, notify upper layer */
1955 if ((port->ip_notify & N_DATA_READY)
1956 && port->ip_port) {
1957 /* ip_lock is set on call here */
1958 receive_chars(port->ip_port);
1959 }
1960
1961 /* We can't ACK this interrupt. If receive_chars didn't
1962 * cause the condition to clear, we'll have to disable
1963 * the interrupt until the data is drained.
1964 * If the read was aborted, don't disable the interrupt
1965 * as this may cause us to hang indefinitely. An
1966 * aborted read generally means that this interrupt
1967 * hasn't been delivered to the cpu yet anyway, even
1968 * though we see it as asserted when we read the sio_ir.
1969 */
1970 if ((sio_ir = PENDING(port)) & hooks->intr_rx_high) {
1971 if ((port->ip_flags & READ_ABORTED) == 0) {
1972 port->ip_ienb &= ~hooks->intr_rx_high;
1973 port->ip_flags |= INPUT_HIGH;
1974 } else {
1975 rx_high_rd_aborted++;
1976 }
1977 }
1978 }
1979
1980 /* We got a low water interrupt: notify upper layer to
1981 * send more data. Must come before tx_mt since servicing
1982 * this condition may cause that condition to clear.
1983 */
1984 if (sio_ir & hooks->intr_tx_explicit) {
1985 port->ip_flags &= ~LOWAT_WRITTEN;
1986
1987 /* ACK the interrupt */
1988 writel(hooks->intr_tx_explicit,
1989 &port->ip_mem->sio_ir);
1990
1991 if (port->ip_notify & N_OUTPUT_LOWAT)
1992 ioc4_cb_output_lowat(port);
1993 }
1994
1995 /* Handle tx_mt. Must come after tx_explicit. */
1996 else if (sio_ir & hooks->intr_tx_mt) {
1997 /* If we are expecting a lowat notification
1998 * and we get to this point it probably means that for
1999 * some reason the tx_explicit didn't work as expected
2000 * (that can legitimately happen if the output buffer is
2001 * filled up in just the right way).
2002 * So send the notification now.
2003 */
2004 if (port->ip_notify & N_OUTPUT_LOWAT) {
2005 ioc4_cb_output_lowat(port);
2006
2007 /* We need to reload the sio_ir since the lowat
2008 * call may have caused another write to occur,
2009 * clearing the tx_mt condition.
2010 */
2011 sio_ir = PENDING(port);
2012 }
2013
2014 /* If the tx_mt condition still persists even after the
2015 * lowat call, we've got some work to do.
2016 */
2017 if (sio_ir & hooks->intr_tx_mt) {
2018
2019 /* If we are not currently expecting DMA input,
2020 * and the transmitter has just gone idle,
2021 * there is no longer any reason for DMA, so
2022 * disable it.
2023 */
2024 if (!(port->ip_notify
2025 & (N_DATA_READY | N_DDCD))) {
2026 BUG_ON(!(port->ip_sscr
2027 & IOC4_SSCR_DMA_EN));
2028 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
2029 writel(port->ip_sscr,
2030 &port->ip_serial_regs->sscr);
2031 }
2032
2033 /* Prevent infinite tx_mt interrupt */
2034 port->ip_ienb &= ~hooks->intr_tx_mt;
2035 }
2036 }
2037 sio_ir = PENDING(port);
2038
2039 /* if the read was aborted and only hooks->intr_rx_high,
2040 * clear hooks->intr_rx_high, so we do not loop forever.
2041 */
2042
2043 if (rx_high_rd_aborted && (sio_ir == hooks->intr_rx_high)) {
2044 sio_ir &= ~hooks->intr_rx_high;
2045 }
2046 } while (sio_ir & hooks->intr_all);
2047
2048 spin_unlock_irqrestore(&port->ip_lock, flags);
2049
2050 /* Re-enable interrupts before returning from interrupt handler.
2051 * Getting interrupted here is okay. It'll just v() our semaphore, and
2052 * we'll come through the loop again.
2053 */
2054
2055 write_ireg(port->ip_ioc4_soft, port->ip_ienb, IOC4_W_IES,
2056 IOC4_SIO_INTR_TYPE);
2057}
2058
2059/*
2060 * ioc4_cb_post_ncs - called for some basic errors
2061 * @port: port to use
2062 * @ncs: event
2063 */
2064static void ioc4_cb_post_ncs(struct uart_port *the_port, int ncs)
2065{
2066 struct uart_icount *icount;
2067
2068 icount = &the_port->icount;
2069
2070 if (ncs & NCS_BREAK)
2071 icount->brk++;
2072 if (ncs & NCS_FRAMING)
2073 icount->frame++;
2074 if (ncs & NCS_OVERRUN)
2075 icount->overrun++;
2076 if (ncs & NCS_PARITY)
2077 icount->parity++;
2078}
2079
2080/**
2081 * do_read - Read in bytes from the port. Return the number of bytes
2082 * actually read.
2083 * @the_port: port to use
2084 * @buf: place to put the stuff we read
2085 * @len: how big 'buf' is
2086 */
2087
2088static inline int do_read(struct uart_port *the_port, unsigned char *buf,
2089 int len)
2090{
2091 int prod_ptr, cons_ptr, total;
2092 struct ioc4_port *port = get_ioc4_port(the_port);
2093 struct ring *inring;
2094 struct ring_entry *entry;
2095 struct hooks *hooks = port->ip_hooks;
2096 int byte_num;
2097 char *sc;
2098 int loop_counter;
2099
2100 BUG_ON(!(len >= 0));
2101 BUG_ON(!port);
2102
2103 /* There is a nasty timing issue in the IOC4. When the rx_timer
2104 * expires or the rx_high condition arises, we take an interrupt.
2105 * At some point while servicing the interrupt, we read bytes from
2106 * the ring buffer and re-arm the rx_timer. However the rx_timer is
2107 * not started until the first byte is received *after* it is armed,
2108 * and any bytes pending in the rx construction buffers are not drained
2109 * to memory until either there are 4 bytes available or the rx_timer
2110 * expires. This leads to a potential situation where data is left
2111 * in the construction buffers forever - 1 to 3 bytes were received
2112 * after the interrupt was generated but before the rx_timer was
2113 * re-armed. At that point as long as no subsequent bytes are received
2114 * the timer will never be started and the bytes will remain in the
2115 * construction buffer forever. The solution is to execute a DRAIN
2116 * command after rearming the timer. This way any bytes received before
2117 * the DRAIN will be drained to memory, and any bytes received after
2118 * the DRAIN will start the TIMER and be drained when it expires.
2119 * Luckily, this only needs to be done when the DMA buffer is empty
2120 * since there is no requirement that this function return all
2121 * available data as long as it returns some.
2122 */
2123 /* Re-arm the timer */
2124 writel(port->ip_rx_cons | IOC4_SRCIR_ARM,
2125 &port->ip_serial_regs->srcir);
2126
2127 prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
2128 cons_ptr = port->ip_rx_cons;
2129
2130 if (prod_ptr == cons_ptr) {
2131 int reset_dma = 0;
2132
2133 /* Input buffer appears empty, do a flush. */
2134
2135 /* DMA must be enabled for this to work. */
2136 if (!(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
2137 port->ip_sscr |= IOC4_SSCR_DMA_EN;
2138 reset_dma = 1;
2139 }
2140
2141 /* Potential race condition: we must reload the srpir after
2142 * issuing the drain command, otherwise we could think the rx
2143 * buffer is empty, then take a very long interrupt, and when
2144 * we come back it's full and we wait forever for the drain to
2145 * complete.
2146 */
2147 writel(port->ip_sscr | IOC4_SSCR_RX_DRAIN,
2148 &port->ip_serial_regs->sscr);
2149 prod_ptr = readl(&port->ip_serial_regs->srpir)
2150 & PROD_CONS_MASK;
2151
2152 /* We must not wait for the DRAIN to complete unless there are
2153 * at least 8 bytes (2 ring entries) available to receive the
2154 * data otherwise the DRAIN will never complete and we'll
2155 * deadlock here.
2156 * In fact, to make things easier, I'll just ignore the flush if
2157 * there is any data at all now available.
2158 */
2159 if (prod_ptr == cons_ptr) {
2160 loop_counter = 0;
2161 while (readl(&port->ip_serial_regs->sscr) &
2162 IOC4_SSCR_RX_DRAIN) {
2163 loop_counter++;
2164 if (loop_counter > MAXITER)
2165 return -1;
2166 }
2167
2168 /* SIGH. We have to reload the prod_ptr *again* since
2169 * the drain may have caused it to change
2170 */
2171 prod_ptr = readl(&port->ip_serial_regs->srpir)
2172 & PROD_CONS_MASK;
2173 }
2174 if (reset_dma) {
2175 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
2176 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
2177 }
2178 }
2179 inring = port->ip_inring;
2180 port->ip_flags &= ~READ_ABORTED;
2181
2182 total = 0;
2183 loop_counter = 0xfffff; /* to avoid hangs */
2184
2185 /* Grab bytes from the hardware */
2186 while ((prod_ptr != cons_ptr) && (len > 0)) {
2187 entry = (struct ring_entry *)((caddr_t)inring + cons_ptr);
2188
2189 if ( loop_counter-- <= 0 ) {
2190 printk(KERN_WARNING "IOC4 serial: "
2191 "possible hang condition/"
2192 "port stuck on read.\n");
2193 break;
2194 }
2195
2196 /* According to the producer pointer, this ring entry
2197 * must contain some data. But if the PIO happened faster
2198 * than the DMA, the data may not be available yet, so let's
2199 * wait until it arrives.
2200 */
2201 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
2202 /* Indicate the read is aborted so we don't disable
2203 * the interrupt thinking that the consumer is
2204 * congested.
2205 */
2206 port->ip_flags |= READ_ABORTED;
2207 len = 0;
2208 break;
2209 }
2210
2211 /* Load the bytes/status out of the ring entry */
2212 for (byte_num = 0; byte_num < 4 && len > 0; byte_num++) {
2213 sc = &(entry->ring_sc[byte_num]);
2214
2215 /* Check for change in modem state or overrun */
2216 if ((*sc & IOC4_RXSB_MODEM_VALID)
2217 && (port->ip_notify & N_DDCD)) {
2218 /* Notify upper layer if DCD dropped */
2219
2220 if ((port->ip_flags & DCD_ON)
2221 && !(*sc & IOC4_RXSB_DCD)) {
2222
2223 /* If we have already copied some data,
2224 * return it. We'll pick up the carrier
2225 * drop on the next pass. That way we
2226 * don't throw away the data that has
2227 * already been copied back to
2228 * the caller's buffer.
2229 */
2230 if (total > 0) {
2231 len = 0;
2232 break;
2233 }
2234 port->ip_flags &= ~DCD_ON;
2235
2236 /* Turn off this notification so the
2237 * carrier drop protocol won't see it
2238 * again when it does a read.
2239 */
2240 *sc &= ~IOC4_RXSB_MODEM_VALID;
2241
2242 /* To keep things consistent, we need
2243 * to update the consumer pointer so
2244 * the next reader won't come in and
2245 * try to read the same ring entries
2246 * again. This must be done here before
2247 * the dcd change.
2248 */
2249
2250 if ((entry->ring_allsc & RING_ANY_VALID)
2251 == 0) {
2252 cons_ptr += (int)sizeof
2253 (struct ring_entry);
2254 cons_ptr &= PROD_CONS_MASK;
2255 }
2256 writel(cons_ptr,
2257 &port->ip_serial_regs->srcir);
2258 port->ip_rx_cons = cons_ptr;
2259
2260 /* Notify upper layer of carrier drop */
2261 if ((port->ip_notify & N_DDCD)
2262 && port->ip_port) {
2263 the_port->icount.dcd = 0;
2264 wake_up_interruptible
2265 (&the_port->info->
2266 delta_msr_wait);
2267 }
2268
2269 /* If we had any data to return, we
2270 * would have returned it above.
2271 */
2272 return 0;
2273 }
2274 }
2275 if (*sc & IOC4_RXSB_MODEM_VALID) {
2276 /* Notify that an input overrun occurred */
2277 if ((*sc & IOC4_RXSB_OVERRUN)
2278 && (port->ip_notify & N_OVERRUN_ERROR)) {
2279 ioc4_cb_post_ncs(the_port, NCS_OVERRUN);
2280 }
2281 /* Don't look at this byte again */
2282 *sc &= ~IOC4_RXSB_MODEM_VALID;
2283 }
2284
2285 /* Check for valid data or RX errors */
2286 if ((*sc & IOC4_RXSB_DATA_VALID) &&
2287 ((*sc & (IOC4_RXSB_PAR_ERR
2288 | IOC4_RXSB_FRAME_ERR
2289 | IOC4_RXSB_BREAK))
2290 && (port->ip_notify & (N_PARITY_ERROR
2291 | N_FRAMING_ERROR
2292 | N_BREAK)))) {
2293 /* There is an error condition on the next byte.
2294 * If we have already transferred some bytes,
2295 * we'll stop here. Otherwise if this is the
2296 * first byte to be read, we'll just transfer
2297 * it alone after notifying the
2298 * upper layer of its status.
2299 */
2300 if (total > 0) {
2301 len = 0;
2302 break;
2303 } else {
2304 if ((*sc & IOC4_RXSB_PAR_ERR) &&
2305 (port->ip_notify & N_PARITY_ERROR)) {
2306 ioc4_cb_post_ncs(the_port,
2307 NCS_PARITY);
2308 }
2309 if ((*sc & IOC4_RXSB_FRAME_ERR) &&
2310 (port->ip_notify & N_FRAMING_ERROR)){
2311 ioc4_cb_post_ncs(the_port,
2312 NCS_FRAMING);
2313 }
2314 if ((*sc & IOC4_RXSB_BREAK)
2315 && (port->ip_notify & N_BREAK)) {
2316 ioc4_cb_post_ncs
2317 (the_port,
2318 NCS_BREAK);
2319 }
2320 len = 1;
2321 }
2322 }
2323 if (*sc & IOC4_RXSB_DATA_VALID) {
2324 *sc &= ~IOC4_RXSB_DATA_VALID;
2325 *buf = entry->ring_data[byte_num];
2326 buf++;
2327 len--;
2328 total++;
2329 }
2330 }
2331
2332 /* If we used up this entry entirely, go on to the next one,
2333 * otherwise we must have run out of buffer space, so
2334 * leave the consumer pointer here for the next read in case
2335 * there are still unread bytes in this entry.
2336 */
2337 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
2338 cons_ptr += (int)sizeof(struct ring_entry);
2339 cons_ptr &= PROD_CONS_MASK;
2340 }
2341 }
2342
2343 /* Update consumer pointer and re-arm rx timer interrupt */
2344 writel(cons_ptr, &port->ip_serial_regs->srcir);
2345 port->ip_rx_cons = cons_ptr;
2346
2347 /* If we have now dipped below the rx high water mark and we have
2348 * rx_high interrupt turned off, we can now turn it back on again.
2349 */
2350 if ((port->ip_flags & INPUT_HIGH) && (((prod_ptr - cons_ptr)
2351 & PROD_CONS_MASK) < ((port->ip_sscr &
2352 IOC4_SSCR_RX_THRESHOLD)
2353 << IOC4_PROD_CONS_PTR_OFF))) {
2354 port->ip_flags &= ~INPUT_HIGH;
2355 enable_intrs(port, hooks->intr_rx_high);
2356 }
2357 return total;
2358}
2359/**
2360 * receive_chars - upper level read. Called with ip_lock.
2361 * @the_port: port to read from
2362 */
2363static void receive_chars(struct uart_port *the_port)
2364{
2365 struct tty_struct *tty;
2366 unsigned char ch[IOC4_MAX_CHARS];
2367 int read_count, request_count;
2368 struct uart_icount *icount;
2369 struct uart_info *info = the_port->info;
2370
2371 /* Make sure all the pointers are "good" ones */
2372 if (!info)
2373 return;
2374 if (!info->tty)
2375 return;
2376
2377 tty = info->tty;
2378
2379 request_count = TTY_FLIPBUF_SIZE - tty->flip.count - 1;
2380
2381 if (request_count > 0) {
2382 if (request_count > IOC4_MAX_CHARS - 2)
2383 request_count = IOC4_MAX_CHARS - 2;
2384 icount = &the_port->icount;
2385 read_count = do_read(the_port, ch, request_count);
2386 if (read_count > 0) {
2387 memcpy(tty->flip.char_buf_ptr, ch, read_count);
2388 memset(tty->flip.flag_buf_ptr, TTY_NORMAL, read_count);
2389 tty->flip.char_buf_ptr += read_count;
2390 tty->flip.flag_buf_ptr += read_count;
2391 tty->flip.count += read_count;
2392 icount->rx += read_count;
2393 }
2394 }
2395 tty_flip_buffer_push(tty);
2396}
2397
2398/**
2399 * ic4_type - What type of console are we?
2400 * @port: Port to operate with (we ignore since we only have one port)
2401 *
2402 */
2403static const char *ic4_type(struct uart_port *the_port)
2404{
2405 return "SGI IOC4 Serial";
2406}
2407
2408/**
2409 * ic4_tx_empty - Is the transmitter empty? We pretend we're always empty
2410 * @port: Port to operate on (we ignore since we always return 1)
2411 *
2412 */
2413static unsigned int ic4_tx_empty(struct uart_port *the_port)
2414{
2415 return 1;
2416}
2417
2418/**
2419 * ic4_stop_tx - stop the transmitter
2420 * @port: Port to operate on
2421 * @tty_stop: Set to 1 if called via uart_stop
2422 *
2423 */
2424static void ic4_stop_tx(struct uart_port *the_port, unsigned int tty_stop)
2425{
2426}
2427
2428/**
2429 * null_void_function -
2430 * @port: Port to operate on
2431 *
2432 */
2433static void null_void_function(struct uart_port *the_port)
2434{
2435}
2436
2437/**
2438 * ic4_shutdown - shut down the port - free irq and disable
2439 * @port: Port to shut down
2440 *
2441 */
2442static void ic4_shutdown(struct uart_port *the_port)
2443{
2444 unsigned long port_flags;
2445 struct ioc4_port *port;
2446 struct uart_info *info;
2447
2448 port = get_ioc4_port(the_port);
2449 if (!port)
2450 return;
2451
2452 info = the_port->info;
2453
2454 if (!(info->flags & UIF_INITIALIZED))
2455 return;
2456
2457 wake_up_interruptible(&info->delta_msr_wait);
2458
2459 if (info->tty)
2460 set_bit(TTY_IO_ERROR, &info->tty->flags);
2461
2462 spin_lock_irqsave(&port->ip_lock, port_flags);
2463 set_notification(port, N_ALL, 0);
2464 info->flags &= ~UIF_INITIALIZED;
2465 spin_unlock_irqrestore(&port->ip_lock, port_flags);
2466}
2467
2468/**
2469 * ic4_set_mctrl - set control lines (dtr, rts, etc)
2470 * @port: Port to operate on
2471 * @mctrl: Lines to set/unset
2472 *
2473 */
2474static void ic4_set_mctrl(struct uart_port *the_port, unsigned int mctrl)
2475{
2476 unsigned char mcr = 0;
2477
2478 if (mctrl & TIOCM_RTS)
2479 mcr |= UART_MCR_RTS;
2480 if (mctrl & TIOCM_DTR)
2481 mcr |= UART_MCR_DTR;
2482 if (mctrl & TIOCM_OUT1)
2483 mcr |= UART_MCR_OUT1;
2484 if (mctrl & TIOCM_OUT2)
2485 mcr |= UART_MCR_OUT2;
2486 if (mctrl & TIOCM_LOOP)
2487 mcr |= UART_MCR_LOOP;
2488
2489 set_mcr(the_port, 1, mcr, IOC4_SHADOW_DTR);
2490}
2491
2492/**
2493 * ic4_get_mctrl - get control line info
2494 * @port: port to operate on
2495 *
2496 */
2497static unsigned int ic4_get_mctrl(struct uart_port *the_port)
2498{
2499 struct ioc4_port *port = get_ioc4_port(the_port);
2500 uint32_t shadow;
2501 unsigned int ret = 0;
2502
2503 if (!port)
2504 return 0;
2505
2506 shadow = readl(&port->ip_serial_regs->shadow);
2507 if (shadow & IOC4_SHADOW_DCD)
2508 ret |= TIOCM_CAR;
2509 if (shadow & IOC4_SHADOW_DR)
2510 ret |= TIOCM_DSR;
2511 if (shadow & IOC4_SHADOW_CTS)
2512 ret |= TIOCM_CTS;
2513 return ret;
2514}
2515
2516/**
2517 * ic4_start_tx - Start transmitter, flush any output
2518 * @port: Port to operate on
2519 * @tty_stop: Set to 1 if called via uart_start
2520 *
2521 */
2522static void ic4_start_tx(struct uart_port *the_port, unsigned int tty_stop)
2523{
2524 struct ioc4_port *port = get_ioc4_port(the_port);
2525 unsigned long flags;
2526
2527 if (port) {
2528 spin_lock_irqsave(&port->ip_lock, flags);
2529 transmit_chars(the_port);
2530 spin_unlock_irqrestore(&port->ip_lock, flags);
2531 }
2532}
2533
2534/**
2535 * ic4_break_ctl - handle breaks
2536 * @port: Port to operate on
2537 * @break_state: Break state
2538 *
2539 */
2540static void ic4_break_ctl(struct uart_port *the_port, int break_state)
2541{
2542}
2543
2544/**
2545 * ic4_startup - Start up the serial port - always return 0 (We're always on)
2546 * @port: Port to operate on
2547 *
2548 */
2549static int ic4_startup(struct uart_port *the_port)
2550{
2551 int retval;
2552 struct ioc4_port *port;
2553 struct ioc4_control *control;
2554 struct uart_info *info;
2555 unsigned long port_flags;
2556
2557 if (!the_port) {
2558 return -ENODEV;
2559 }
2560 port = get_ioc4_port(the_port);
2561 if (!port) {
2562 return -ENODEV;
2563 }
2564 info = the_port->info;
2565
2566 control = port->ip_control;
2567 if (!control) {
2568 return -ENODEV;
2569 }
2570
2571 /* Start up the serial port */
2572 spin_lock_irqsave(&port->ip_lock, port_flags);
2573 retval = ic4_startup_local(the_port);
2574 spin_unlock_irqrestore(&port->ip_lock, port_flags);
2575 return retval;
2576}
2577
2578/**
2579 * ic4_set_termios - set termios stuff
2580 * @port: port to operate on
2581 * @termios: New settings
2582 * @termios: Old
2583 *
2584 */
2585static void
2586ic4_set_termios(struct uart_port *the_port,
2587 struct termios *termios, struct termios *old_termios)
2588{
2589 struct ioc4_port *port = get_ioc4_port(the_port);
2590 unsigned long port_flags;
2591
2592 spin_lock_irqsave(&port->ip_lock, port_flags);
2593 ioc4_change_speed(the_port, termios, old_termios);
2594 spin_unlock_irqrestore(&port->ip_lock, port_flags);
2595}
2596
2597/**
2598 * ic4_request_port - allocate resources for port - no op....
2599 * @port: port to operate on
2600 *
2601 */
2602static int ic4_request_port(struct uart_port *port)
2603{
2604 return 0;
2605}
2606
2607/* Associate the uart functions above - given to serial core */
2608
2609static struct uart_ops ioc4_ops = {
2610 .tx_empty = ic4_tx_empty,
2611 .set_mctrl = ic4_set_mctrl,
2612 .get_mctrl = ic4_get_mctrl,
2613 .stop_tx = ic4_stop_tx,
2614 .start_tx = ic4_start_tx,
2615 .stop_rx = null_void_function,
2616 .enable_ms = null_void_function,
2617 .break_ctl = ic4_break_ctl,
2618 .startup = ic4_startup,
2619 .shutdown = ic4_shutdown,
2620 .set_termios = ic4_set_termios,
2621 .type = ic4_type,
2622 .release_port = null_void_function,
2623 .request_port = ic4_request_port,
2624};
2625
2626/*
2627 * Boot-time initialization code
2628 */
2629
2630static struct uart_driver ioc4_uart = {
2631 .owner = THIS_MODULE,
2632 .driver_name = "ioc4_serial",
2633 .dev_name = DEVICE_NAME,
2634 .major = DEVICE_MAJOR,
2635 .minor = DEVICE_MINOR,
2636 .nr = IOC4_NUM_CARDS * IOC4_NUM_SERIAL_PORTS,
2637};
2638
2639/**
2640 * ioc4_serial_core_attach - register with serial core
2641 * This is done during pci probing
2642 * @pdev: handle for this card
2643 */
2644static inline int
2645ioc4_serial_core_attach(struct pci_dev *pdev)
2646{
2647 struct ioc4_port *port;
2648 struct uart_port *the_port;
2649 struct ioc4_control *control = pci_get_drvdata(pdev);
2650 int ii;
2651
2652 DPRINT_CONFIG(("%s: attach pdev 0x%p - control 0x%p\n",
2653 __FUNCTION__, pdev, (void *)control));
2654
2655 if (!control)
2656 return -ENODEV;
2657
2658 /* once around for each port on this card */
2659 for (ii = 0; ii < IOC4_NUM_SERIAL_PORTS; ii++) {
2660 the_port = &control->ic_port[ii].icp_uart_port;
2661 port = control->ic_port[ii].icp_port;
2662 port->ip_port = the_port;
2663
2664 DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p\n",
2665 __FUNCTION__, (void *)the_port,
2666 (void *)port));
2667
2668 spin_lock_init(&the_port->lock);
2669 /* membase, iobase and mapbase just need to be non-0 */
2670 the_port->membase = (unsigned char __iomem *)1;
2671 the_port->line = the_port->iobase = ii;
2672 the_port->mapbase = 1;
2673 the_port->type = PORT_16550A;
2674 the_port->fifosize = IOC4_MAX_CHARS;
2675 the_port->ops = &ioc4_ops;
2676 the_port->irq = control->ic_irq;
2677 the_port->dev = &pdev->dev;
2678 if (uart_add_one_port(&ioc4_uart, the_port) < 0) {
2679 printk(KERN_WARNING
2680 "%s: unable to add port %d\n",
2681 __FUNCTION__, the_port->line);
2682 } else {
2683 DPRINT_CONFIG(
2684 ("IOC4 serial driver port %d irq = %d\n",
2685 the_port->line, the_port->irq));
2686 }
2687 /* all ports are rs232 for now */
2688 ioc4_set_proto(port, PROTO_RS232);
2689 }
2690 return 0;
2691}
2692
2693/**
2694 * ioc4_serial_attach_one - register attach function
2695 * called per card found from ioc4_serial_detect as part
2696 * of module_init().
2697 * @pdev: handle for this card
2698 * @pci_id: pci id for this card
2699 */
2700int
2701ioc4_serial_attach_one(struct pci_dev *pdev, const struct pci_device_id *pci_id)
2702{
2703 struct ioc4_mem __iomem *mem;
2704 unsigned long tmp_addr, tmp_addr1;
2705 struct ioc4_serial __iomem *serial;
2706 struct ioc4_soft *soft;
2707 struct ioc4_control *control;
2708 int tmp, ret = 0;
2709
2710
2711 DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __FUNCTION__, pdev, pci_id));
2712
2713 /* Map in the ioc4 memory */
2714 tmp_addr = pci_resource_start(pdev, 0);
2715 if (!tmp_addr) {
2716 printk(KERN_WARNING
2717 "ioc4 (%p) : unable to get PIO mapping for "
2718 "MEM space\n", (void *)pdev);
2719 return -ENODEV;
2720 }
2721 if (!request_region(tmp_addr, sizeof(struct ioc4_mem), "sioc4_mem")) {
2722 printk(KERN_ALERT
2723 "ioc4 (%p): unable to get request region for "
2724 "MEM space\n", (void *)pdev);
2725 return -ENODEV;
2726 }
2727 mem = ioremap(tmp_addr, sizeof(struct ioc4_mem));
2728 if (!mem) {
2729 printk(KERN_WARNING
2730 "ioc4 (%p) : unable to remap ioc4 memory\n",
2731 (void *)pdev);
2732 ret = -ENODEV;
2733 goto out1;
2734 }
2735
2736 /* request serial registers */
2737 tmp_addr1 = pci_resource_start(pdev, 0) + IOC4_SERIAL_OFFSET;
2738
2739 if (!request_region(tmp_addr1, sizeof(struct ioc4_serial),
2740 "sioc4_uart")) {
2741 printk(KERN_WARNING
2742 "ioc4 (%p): unable to get request region for "
2743 "uart space\n", (void *)pdev);
2744 ret = -ENODEV;
2745 goto out1;
2746 }
2747 serial = ioremap(tmp_addr1, sizeof(struct ioc4_serial));
2748 if (!serial) {
2749 printk(KERN_WARNING
2750 "ioc4 (%p) : unable to remap ioc4 serial register\n",
2751 (void *)pdev);
2752 ret = -ENODEV;
2753 goto out2;
2754 }
2755 DPRINT_CONFIG(("%s : mem 0x%p, serial 0x%p\n",
2756 __FUNCTION__, (void *)mem, (void *)serial));
2757
2758 /* Get memory for the new card */
2759 control = kmalloc(sizeof(struct ioc4_control) * IOC4_NUM_SERIAL_PORTS,
2760 GFP_KERNEL);
2761
2762 if (!control) {
2763 printk(KERN_WARNING "ioc4_attach_one"
2764 ": unable to get memory for the IOC4\n");
2765 ret = -ENOMEM;
2766 goto out2;
2767 }
2768 memset(control, 0, sizeof(struct ioc4_control));
2769 pci_set_drvdata(pdev, control);
2770
2771 /* Allocate the soft structure */
2772 soft = kmalloc(sizeof(struct ioc4_soft), GFP_KERNEL);
2773 if (!soft) {
2774 printk(KERN_WARNING
2775 "ioc4 (%p): unable to get memory for the soft struct\n",
2776 (void *)pdev);
2777 ret = -ENOMEM;
2778 goto out3;
2779 }
2780 memset(soft, 0, sizeof(struct ioc4_soft));
2781
2782 spin_lock_init(&soft->is_ir_lock);
2783 soft->is_ioc4_mem_addr = mem;
2784 soft->is_ioc4_serial_addr = serial;
2785
2786 /* Init the IOC4 */
2787 pci_read_config_dword(pdev, PCI_COMMAND, &tmp);
2788 pci_write_config_dword(pdev, PCI_COMMAND,
2789 tmp | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2790
2791 writel(0xf << IOC4_SIO_CR_CMD_PULSE_SHIFT, &mem->sio_cr);
2792
2793 /* Enable serial port mode select generic PIO pins as outputs */
2794 writel(IOC4_GPCR_UART0_MODESEL | IOC4_GPCR_UART1_MODESEL
2795 | IOC4_GPCR_UART2_MODESEL | IOC4_GPCR_UART3_MODESEL,
2796 &mem->gpcr_s);
2797
2798 /* Clear and disable all interrupts */
2799 write_ireg(soft, ~0, IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
2800 writel(~0, &mem->sio_ir);
2801 write_ireg(soft, ~(IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR),
2802 IOC4_W_IEC, IOC4_OTHER_INTR_TYPE);
2803 writel(~(IOC4_OTHER_IR_ATA_MEMERR | IOC4_OTHER_IR_ATA_MEMERR),
2804 &mem->other_ir);
2805 control->ic_soft = soft;
2806 if (!request_irq(pdev->irq, ioc4_intr, SA_SHIRQ,
2807 "sgi-ioc4serial", (void *)soft)) {
2808 control->ic_irq = pdev->irq;
2809 } else {
2810 printk(KERN_WARNING
2811 "%s : request_irq fails for IRQ 0x%x\n ",
2812 __FUNCTION__, pdev->irq);
2813 }
2814 if ((ret = ioc4_attach_local(pdev, control, soft,
2815 soft->is_ioc4_mem_addr,
2816 soft->is_ioc4_serial_addr)))
2817 goto out4;
2818
2819 /* register port with the serial core */
2820
2821 if ((ret = ioc4_serial_core_attach(pdev)))
2822 goto out4;
2823
2824 return ret;
2825
2826 /* error exits that give back resources */
2827out4:
2828 kfree(soft);
2829out3:
2830 kfree(control);
2831out2:
2832 release_region(tmp_addr1, sizeof(struct ioc4_serial));
2833out1:
2834 release_region(tmp_addr, sizeof(struct ioc4_mem));
2835
2836 return ret;
2837}
2838
2839
2840/**
2841 * ioc4_serial_remove_one - detach function
2842 *
2843 * @pdev: handle for this card
2844 */
2845
2846#if 0
2847void ioc4_serial_remove_one(struct pci_dev *pdev)
2848{
2849 int ii;
2850 struct ioc4_control *control;
2851 struct uart_port *the_port;
2852 struct ioc4_port *port;
2853 struct ioc4_soft *soft;
2854
2855 control = pci_get_drvdata(pdev);
2856
2857 for (ii = 0; ii < IOC4_NUM_SERIAL_PORTS; ii++) {
2858 the_port = &control->ic_port[ii].icp_uart_port;
2859 if (the_port) {
2860 uart_remove_one_port(&ioc4_uart, the_port);
2861 }
2862 port = control->ic_port[ii].icp_port;
2863 if (!(ii & 1) && port) {
2864 pci_free_consistent(port->ip_pdev,
2865 TOTAL_RING_BUF_SIZE,
2866 (void *)port->ip_cpu_ringbuf,
2867 port->ip_dma_ringbuf);
2868 kfree(port);
2869 }
2870 }
2871 soft = control->ic_soft;
2872 if (soft) {
2873 free_irq(control->ic_irq, (void *)soft);
2874 if (soft->is_ioc4_serial_addr) {
2875 release_region((unsigned long)
2876 soft->is_ioc4_serial_addr,
2877 sizeof(struct ioc4_serial));
2878 }
2879 kfree(soft);
2880 }
2881 kfree(control);
2882 pci_set_drvdata(pdev, NULL);
2883 uart_unregister_driver(&ioc4_uart);
2884}
2885#endif
2886
2887/**
2888 * ioc4_serial_init - module init
2889 */
2890int ioc4_serial_init(void)
2891{
2892 int ret;
2893
2894 /* register with serial core */
2895 if ((ret = uart_register_driver(&ioc4_uart)) < 0) {
2896 printk(KERN_WARNING
2897 "%s: Couldn't register IOC4 serial driver\n",
2898 __FUNCTION__);
2899 return ret;
2900 }
2901 return 0;
2902}
2903
2904MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
2905MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC4 Base-IO Card");
2906MODULE_LICENSE("GPL");
2907
2908EXPORT_SYMBOL(ioc4_serial_init);
2909EXPORT_SYMBOL(ioc4_serial_attach_one);
diff --git a/drivers/serial/ip22zilog.c b/drivers/serial/ip22zilog.c
new file mode 100644
index 000000000000..3ea46c069f6f
--- /dev/null
+++ b/drivers/serial/ip22zilog.c
@@ -0,0 +1,1271 @@
1/*
2 * Driver for Zilog serial chips found on SGI workstations and
3 * servers. This driver could actually be made more generic.
4 *
5 * This is based on the drivers/serial/sunzilog.c code as of 2.6.0-test7 and the
6 * old drivers/sgi/char/sgiserial.c code which itself is based of the original
7 * drivers/sbus/char/zs.c code. A lot of code has been simply moved over
8 * directly from there but much has been rewritten. Credits therefore go out
9 * to David S. Miller, Eddie C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell
10 * for their work there.
11 *
12 * Copyright (C) 2002 Ralf Baechle (ralf@linux-mips.org)
13 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
14 */
15#include <linux/config.h>
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/errno.h>
20#include <linux/delay.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/major.h>
24#include <linux/string.h>
25#include <linux/ptrace.h>
26#include <linux/ioport.h>
27#include <linux/slab.h>
28#include <linux/circ_buf.h>
29#include <linux/serial.h>
30#include <linux/sysrq.h>
31#include <linux/console.h>
32#include <linux/spinlock.h>
33#include <linux/init.h>
34
35#include <asm/io.h>
36#include <asm/irq.h>
37#include <asm/sgialib.h>
38#include <asm/sgi/ioc.h>
39#include <asm/sgi/hpc3.h>
40#include <asm/sgi/ip22.h>
41
42#if defined(CONFIG_SERIAL_IP22_ZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
43#define SUPPORT_SYSRQ
44#endif
45
46#include <linux/serial_core.h>
47
48#include "ip22zilog.h"
49
50void ip22_do_break(void);
51
52/*
53 * On IP22 we need to delay after register accesses but we do not need to
54 * flush writes.
55 */
56#define ZSDELAY() udelay(5)
57#define ZSDELAY_LONG() udelay(20)
58#define ZS_WSYNC(channel) do { } while (0)
59
60#define NUM_IP22ZILOG 1
61#define NUM_CHANNELS (NUM_IP22ZILOG * 2)
62
63#define ZS_CLOCK 3672000 /* Zilog input clock rate. */
64#define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
65
66/*
67 * We wrap our port structure around the generic uart_port.
68 */
69struct uart_ip22zilog_port {
70 struct uart_port port;
71
72 /* IRQ servicing chain. */
73 struct uart_ip22zilog_port *next;
74
75 /* Current values of Zilog write registers. */
76 unsigned char curregs[NUM_ZSREGS];
77
78 unsigned int flags;
79#define IP22ZILOG_FLAG_IS_CONS 0x00000004
80#define IP22ZILOG_FLAG_IS_KGDB 0x00000008
81#define IP22ZILOG_FLAG_MODEM_STATUS 0x00000010
82#define IP22ZILOG_FLAG_IS_CHANNEL_A 0x00000020
83#define IP22ZILOG_FLAG_REGS_HELD 0x00000040
84#define IP22ZILOG_FLAG_TX_STOPPED 0x00000080
85#define IP22ZILOG_FLAG_TX_ACTIVE 0x00000100
86
87 unsigned int cflag;
88
89 /* L1-A keyboard break state. */
90 int kbd_id;
91 int l1_down;
92
93 unsigned char parity_mask;
94 unsigned char prev_status;
95};
96
97#define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel *)((PORT)->membase))
98#define UART_ZILOG(PORT) ((struct uart_ip22zilog_port *)(PORT))
99#define IP22ZILOG_GET_CURR_REG(PORT, REGNUM) \
100 (UART_ZILOG(PORT)->curregs[REGNUM])
101#define IP22ZILOG_SET_CURR_REG(PORT, REGNUM, REGVAL) \
102 ((UART_ZILOG(PORT)->curregs[REGNUM]) = (REGVAL))
103#define ZS_IS_CONS(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_CONS)
104#define ZS_IS_KGDB(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_KGDB)
105#define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & IP22ZILOG_FLAG_MODEM_STATUS)
106#define ZS_IS_CHANNEL_A(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_CHANNEL_A)
107#define ZS_REGS_HELD(UP) ((UP)->flags & IP22ZILOG_FLAG_REGS_HELD)
108#define ZS_TX_STOPPED(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_STOPPED)
109#define ZS_TX_ACTIVE(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_ACTIVE)
110
111/* Reading and writing Zilog8530 registers. The delays are to make this
112 * driver work on the IP22 which needs a settling delay after each chip
113 * register access, other machines handle this in hardware via auxiliary
114 * flip-flops which implement the settle time we do in software.
115 *
116 * The port lock must be held and local IRQs must be disabled
117 * when {read,write}_zsreg is invoked.
118 */
119static unsigned char read_zsreg(struct zilog_channel *channel,
120 unsigned char reg)
121{
122 unsigned char retval;
123
124 writeb(reg, &channel->control);
125 ZSDELAY();
126 retval = readb(&channel->control);
127 ZSDELAY();
128
129 return retval;
130}
131
132static void write_zsreg(struct zilog_channel *channel,
133 unsigned char reg, unsigned char value)
134{
135 writeb(reg, &channel->control);
136 ZSDELAY();
137 writeb(value, &channel->control);
138 ZSDELAY();
139}
140
141static void ip22zilog_clear_fifo(struct zilog_channel *channel)
142{
143 int i;
144
145 for (i = 0; i < 32; i++) {
146 unsigned char regval;
147
148 regval = readb(&channel->control);
149 ZSDELAY();
150 if (regval & Rx_CH_AV)
151 break;
152
153 regval = read_zsreg(channel, R1);
154 readb(&channel->data);
155 ZSDELAY();
156
157 if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
158 writeb(ERR_RES, &channel->control);
159 ZSDELAY();
160 ZS_WSYNC(channel);
161 }
162 }
163}
164
165/* This function must only be called when the TX is not busy. The UART
166 * port lock must be held and local interrupts disabled.
167 */
168static void __load_zsregs(struct zilog_channel *channel, unsigned char *regs)
169{
170 int i;
171
172 /* Let pending transmits finish. */
173 for (i = 0; i < 1000; i++) {
174 unsigned char stat = read_zsreg(channel, R1);
175 if (stat & ALL_SNT)
176 break;
177 udelay(100);
178 }
179
180 writeb(ERR_RES, &channel->control);
181 ZSDELAY();
182 ZS_WSYNC(channel);
183
184 ip22zilog_clear_fifo(channel);
185
186 /* Disable all interrupts. */
187 write_zsreg(channel, R1,
188 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
189
190 /* Set parity, sync config, stop bits, and clock divisor. */
191 write_zsreg(channel, R4, regs[R4]);
192
193 /* Set misc. TX/RX control bits. */
194 write_zsreg(channel, R10, regs[R10]);
195
196 /* Set TX/RX controls sans the enable bits. */
197 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
198 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
199
200 /* Synchronous mode config. */
201 write_zsreg(channel, R6, regs[R6]);
202 write_zsreg(channel, R7, regs[R7]);
203
204 /* Don't mess with the interrupt vector (R2, unused by us) and
205 * master interrupt control (R9). We make sure this is setup
206 * properly at probe time then never touch it again.
207 */
208
209 /* Disable baud generator. */
210 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
211
212 /* Clock mode control. */
213 write_zsreg(channel, R11, regs[R11]);
214
215 /* Lower and upper byte of baud rate generator divisor. */
216 write_zsreg(channel, R12, regs[R12]);
217 write_zsreg(channel, R13, regs[R13]);
218
219 /* Now rewrite R14, with BRENAB (if set). */
220 write_zsreg(channel, R14, regs[R14]);
221
222 /* External status interrupt control. */
223 write_zsreg(channel, R15, regs[R15]);
224
225 /* Reset external status interrupts. */
226 write_zsreg(channel, R0, RES_EXT_INT);
227 write_zsreg(channel, R0, RES_EXT_INT);
228
229 /* Rewrite R3/R5, this time without enables masked. */
230 write_zsreg(channel, R3, regs[R3]);
231 write_zsreg(channel, R5, regs[R5]);
232
233 /* Rewrite R1, this time without IRQ enabled masked. */
234 write_zsreg(channel, R1, regs[R1]);
235}
236
237/* Reprogram the Zilog channel HW registers with the copies found in the
238 * software state struct. If the transmitter is busy, we defer this update
239 * until the next TX complete interrupt. Else, we do it right now.
240 *
241 * The UART port lock must be held and local interrupts disabled.
242 */
243static void ip22zilog_maybe_update_regs(struct uart_ip22zilog_port *up,
244 struct zilog_channel *channel)
245{
246 if (!ZS_REGS_HELD(up)) {
247 if (ZS_TX_ACTIVE(up)) {
248 up->flags |= IP22ZILOG_FLAG_REGS_HELD;
249 } else {
250 __load_zsregs(channel, up->curregs);
251 }
252 }
253}
254
255static void ip22zilog_receive_chars(struct uart_ip22zilog_port *up,
256 struct zilog_channel *channel,
257 struct pt_regs *regs)
258{
259 struct tty_struct *tty = up->port.info->tty; /* XXX info==NULL? */
260
261 while (1) {
262 unsigned char ch, r1;
263
264 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
265 tty->flip.work.func((void *)tty);
266 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
267 return; /* XXX Ignores SysRq when we need it most. Fix. */
268 }
269
270 r1 = read_zsreg(channel, R1);
271 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
272 writeb(ERR_RES, &channel->control);
273 ZSDELAY();
274 ZS_WSYNC(channel);
275 }
276
277 ch = readb(&channel->control);
278 ZSDELAY();
279
280 /* This funny hack depends upon BRK_ABRT not interfering
281 * with the other bits we care about in R1.
282 */
283 if (ch & BRK_ABRT)
284 r1 |= BRK_ABRT;
285
286 ch = readb(&channel->data);
287 ZSDELAY();
288
289 ch &= up->parity_mask;
290
291 if (ZS_IS_CONS(up) && (r1 & BRK_ABRT)) {
292 /* Wait for BREAK to deassert to avoid potentially
293 * confusing the PROM.
294 */
295 while (1) {
296 ch = readb(&channel->control);
297 ZSDELAY();
298 if (!(ch & BRK_ABRT))
299 break;
300 }
301 ip22_do_break();
302 return;
303 }
304
305 /* A real serial line, record the character and status. */
306 *tty->flip.char_buf_ptr = ch;
307 *tty->flip.flag_buf_ptr = TTY_NORMAL;
308 up->port.icount.rx++;
309 if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
310 if (r1 & BRK_ABRT) {
311 r1 &= ~(PAR_ERR | CRC_ERR);
312 up->port.icount.brk++;
313 if (uart_handle_break(&up->port))
314 goto next_char;
315 }
316 else if (r1 & PAR_ERR)
317 up->port.icount.parity++;
318 else if (r1 & CRC_ERR)
319 up->port.icount.frame++;
320 if (r1 & Rx_OVR)
321 up->port.icount.overrun++;
322 r1 &= up->port.read_status_mask;
323 if (r1 & BRK_ABRT)
324 *tty->flip.flag_buf_ptr = TTY_BREAK;
325 else if (r1 & PAR_ERR)
326 *tty->flip.flag_buf_ptr = TTY_PARITY;
327 else if (r1 & CRC_ERR)
328 *tty->flip.flag_buf_ptr = TTY_FRAME;
329 }
330 if (uart_handle_sysrq_char(&up->port, ch, regs))
331 goto next_char;
332
333 if (up->port.ignore_status_mask == 0xff ||
334 (r1 & up->port.ignore_status_mask) == 0) {
335 tty->flip.flag_buf_ptr++;
336 tty->flip.char_buf_ptr++;
337 tty->flip.count++;
338 }
339 if ((r1 & Rx_OVR) &&
340 tty->flip.count < TTY_FLIPBUF_SIZE) {
341 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
342 tty->flip.flag_buf_ptr++;
343 tty->flip.char_buf_ptr++;
344 tty->flip.count++;
345 }
346 next_char:
347 ch = readb(&channel->control);
348 ZSDELAY();
349 if (!(ch & Rx_CH_AV))
350 break;
351 }
352
353 tty_flip_buffer_push(tty);
354}
355
356static void ip22zilog_status_handle(struct uart_ip22zilog_port *up,
357 struct zilog_channel *channel,
358 struct pt_regs *regs)
359{
360 unsigned char status;
361
362 status = readb(&channel->control);
363 ZSDELAY();
364
365 writeb(RES_EXT_INT, &channel->control);
366 ZSDELAY();
367 ZS_WSYNC(channel);
368
369 if (ZS_WANTS_MODEM_STATUS(up)) {
370 if (status & SYNC)
371 up->port.icount.dsr++;
372
373 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
374 * But it does not tell us which bit has changed, we have to keep
375 * track of this ourselves.
376 */
377 if ((status & DCD) ^ up->prev_status)
378 uart_handle_dcd_change(&up->port,
379 (status & DCD));
380 if ((status & CTS) ^ up->prev_status)
381 uart_handle_cts_change(&up->port,
382 (status & CTS));
383
384 wake_up_interruptible(&up->port.info->delta_msr_wait);
385 }
386
387 up->prev_status = status;
388}
389
390static void ip22zilog_transmit_chars(struct uart_ip22zilog_port *up,
391 struct zilog_channel *channel)
392{
393 struct circ_buf *xmit;
394
395 if (ZS_IS_CONS(up)) {
396 unsigned char status = readb(&channel->control);
397 ZSDELAY();
398
399 /* TX still busy? Just wait for the next TX done interrupt.
400 *
401 * It can occur because of how we do serial console writes. It would
402 * be nice to transmit console writes just like we normally would for
403 * a TTY line. (ie. buffered and TX interrupt driven). That is not
404 * easy because console writes cannot sleep. One solution might be
405 * to poll on enough port->xmit space becomming free. -DaveM
406 */
407 if (!(status & Tx_BUF_EMP))
408 return;
409 }
410
411 up->flags &= ~IP22ZILOG_FLAG_TX_ACTIVE;
412
413 if (ZS_REGS_HELD(up)) {
414 __load_zsregs(channel, up->curregs);
415 up->flags &= ~IP22ZILOG_FLAG_REGS_HELD;
416 }
417
418 if (ZS_TX_STOPPED(up)) {
419 up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
420 goto ack_tx_int;
421 }
422
423 if (up->port.x_char) {
424 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
425 writeb(up->port.x_char, &channel->data);
426 ZSDELAY();
427 ZS_WSYNC(channel);
428
429 up->port.icount.tx++;
430 up->port.x_char = 0;
431 return;
432 }
433
434 if (up->port.info == NULL)
435 goto ack_tx_int;
436 xmit = &up->port.info->xmit;
437 if (uart_circ_empty(xmit)) {
438 uart_write_wakeup(&up->port);
439 goto ack_tx_int;
440 }
441 if (uart_tx_stopped(&up->port))
442 goto ack_tx_int;
443
444 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
445 writeb(xmit->buf[xmit->tail], &channel->data);
446 ZSDELAY();
447 ZS_WSYNC(channel);
448
449 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
450 up->port.icount.tx++;
451
452 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
453 uart_write_wakeup(&up->port);
454
455 return;
456
457ack_tx_int:
458 writeb(RES_Tx_P, &channel->control);
459 ZSDELAY();
460 ZS_WSYNC(channel);
461}
462
463static irqreturn_t ip22zilog_interrupt(int irq, void *dev_id, struct pt_regs *regs)
464{
465 struct uart_ip22zilog_port *up = dev_id;
466
467 while (up) {
468 struct zilog_channel *channel
469 = ZILOG_CHANNEL_FROM_PORT(&up->port);
470 unsigned char r3;
471
472 spin_lock(&up->port.lock);
473 r3 = read_zsreg(channel, R3);
474
475 /* Channel A */
476 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
477 writeb(RES_H_IUS, &channel->control);
478 ZSDELAY();
479 ZS_WSYNC(channel);
480
481 if (r3 & CHARxIP)
482 ip22zilog_receive_chars(up, channel, regs);
483 if (r3 & CHAEXT)
484 ip22zilog_status_handle(up, channel, regs);
485 if (r3 & CHATxIP)
486 ip22zilog_transmit_chars(up, channel);
487 }
488 spin_unlock(&up->port.lock);
489
490 /* Channel B */
491 up = up->next;
492 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
493
494 spin_lock(&up->port.lock);
495 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
496 writeb(RES_H_IUS, &channel->control);
497 ZSDELAY();
498 ZS_WSYNC(channel);
499
500 if (r3 & CHBRxIP)
501 ip22zilog_receive_chars(up, channel, regs);
502 if (r3 & CHBEXT)
503 ip22zilog_status_handle(up, channel, regs);
504 if (r3 & CHBTxIP)
505 ip22zilog_transmit_chars(up, channel);
506 }
507 spin_unlock(&up->port.lock);
508
509 up = up->next;
510 }
511
512 return IRQ_HANDLED;
513}
514
515/* A convenient way to quickly get R0 status. The caller must _not_ hold the
516 * port lock, it is acquired here.
517 */
518static __inline__ unsigned char ip22zilog_read_channel_status(struct uart_port *port)
519{
520 struct zilog_channel *channel;
521 unsigned long flags;
522 unsigned char status;
523
524 spin_lock_irqsave(&port->lock, flags);
525
526 channel = ZILOG_CHANNEL_FROM_PORT(port);
527 status = readb(&channel->control);
528 ZSDELAY();
529
530 spin_unlock_irqrestore(&port->lock, flags);
531
532 return status;
533}
534
535/* The port lock is not held. */
536static unsigned int ip22zilog_tx_empty(struct uart_port *port)
537{
538 unsigned char status;
539 unsigned int ret;
540
541 status = ip22zilog_read_channel_status(port);
542 if (status & Tx_BUF_EMP)
543 ret = TIOCSER_TEMT;
544 else
545 ret = 0;
546
547 return ret;
548}
549
550/* The port lock is not held. */
551static unsigned int ip22zilog_get_mctrl(struct uart_port *port)
552{
553 unsigned char status;
554 unsigned int ret;
555
556 status = ip22zilog_read_channel_status(port);
557
558 ret = 0;
559 if (status & DCD)
560 ret |= TIOCM_CAR;
561 if (status & SYNC)
562 ret |= TIOCM_DSR;
563 if (status & CTS)
564 ret |= TIOCM_CTS;
565
566 return ret;
567}
568
569/* The port lock is held and interrupts are disabled. */
570static void ip22zilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
571{
572 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
573 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
574 unsigned char set_bits, clear_bits;
575
576 set_bits = clear_bits = 0;
577
578 if (mctrl & TIOCM_RTS)
579 set_bits |= RTS;
580 else
581 clear_bits |= RTS;
582 if (mctrl & TIOCM_DTR)
583 set_bits |= DTR;
584 else
585 clear_bits |= DTR;
586
587 /* NOTE: Not subject to 'transmitter active' rule. */
588 up->curregs[R5] |= set_bits;
589 up->curregs[R5] &= ~clear_bits;
590 write_zsreg(channel, R5, up->curregs[R5]);
591}
592
593/* The port lock is held and interrupts are disabled. */
594static void ip22zilog_stop_tx(struct uart_port *port, unsigned int tty_stop)
595{
596 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
597
598 up->flags |= IP22ZILOG_FLAG_TX_STOPPED;
599}
600
601/* The port lock is held and interrupts are disabled. */
602static void ip22zilog_start_tx(struct uart_port *port, unsigned int tty_start)
603{
604 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
605 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
606 unsigned char status;
607
608 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
609 up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
610
611 status = readb(&channel->control);
612 ZSDELAY();
613
614 /* TX busy? Just wait for the TX done interrupt. */
615 if (!(status & Tx_BUF_EMP))
616 return;
617
618 /* Send the first character to jump-start the TX done
619 * IRQ sending engine.
620 */
621 if (port->x_char) {
622 writeb(port->x_char, &channel->data);
623 ZSDELAY();
624 ZS_WSYNC(channel);
625
626 port->icount.tx++;
627 port->x_char = 0;
628 } else {
629 struct circ_buf *xmit = &port->info->xmit;
630
631 writeb(xmit->buf[xmit->tail], &channel->data);
632 ZSDELAY();
633 ZS_WSYNC(channel);
634
635 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
636 port->icount.tx++;
637
638 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
639 uart_write_wakeup(&up->port);
640 }
641}
642
643/* The port lock is held and interrupts are disabled. */
644static void ip22zilog_stop_rx(struct uart_port *port)
645{
646 struct uart_ip22zilog_port *up = UART_ZILOG(port);
647 struct zilog_channel *channel;
648
649 if (ZS_IS_CONS(up))
650 return;
651
652 channel = ZILOG_CHANNEL_FROM_PORT(port);
653
654 /* Disable all RX interrupts. */
655 up->curregs[R1] &= ~RxINT_MASK;
656 ip22zilog_maybe_update_regs(up, channel);
657}
658
659/* The port lock is held. */
660static void ip22zilog_enable_ms(struct uart_port *port)
661{
662 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
663 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
664 unsigned char new_reg;
665
666 new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
667 if (new_reg != up->curregs[R15]) {
668 up->curregs[R15] = new_reg;
669
670 /* NOTE: Not subject to 'transmitter active' rule. */
671 write_zsreg(channel, R15, up->curregs[R15]);
672 }
673}
674
675/* The port lock is not held. */
676static void ip22zilog_break_ctl(struct uart_port *port, int break_state)
677{
678 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
679 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
680 unsigned char set_bits, clear_bits, new_reg;
681 unsigned long flags;
682
683 set_bits = clear_bits = 0;
684
685 if (break_state)
686 set_bits |= SND_BRK;
687 else
688 clear_bits |= SND_BRK;
689
690 spin_lock_irqsave(&port->lock, flags);
691
692 new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
693 if (new_reg != up->curregs[R5]) {
694 up->curregs[R5] = new_reg;
695
696 /* NOTE: Not subject to 'transmitter active' rule. */
697 write_zsreg(channel, R5, up->curregs[R5]);
698 }
699
700 spin_unlock_irqrestore(&port->lock, flags);
701}
702
703static void __ip22zilog_startup(struct uart_ip22zilog_port *up)
704{
705 struct zilog_channel *channel;
706
707 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
708 up->prev_status = readb(&channel->control);
709
710 /* Enable receiver and transmitter. */
711 up->curregs[R3] |= RxENAB;
712 up->curregs[R5] |= TxENAB;
713
714 up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
715 ip22zilog_maybe_update_regs(up, channel);
716}
717
718static int ip22zilog_startup(struct uart_port *port)
719{
720 struct uart_ip22zilog_port *up = UART_ZILOG(port);
721 unsigned long flags;
722
723 if (ZS_IS_CONS(up))
724 return 0;
725
726 spin_lock_irqsave(&port->lock, flags);
727 __ip22zilog_startup(up);
728 spin_unlock_irqrestore(&port->lock, flags);
729 return 0;
730}
731
732/*
733 * The test for ZS_IS_CONS is explained by the following e-mail:
734 *****
735 * From: Russell King <rmk@arm.linux.org.uk>
736 * Date: Sun, 8 Dec 2002 10:18:38 +0000
737 *
738 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
739 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
740 * > and I noticed that something is not right with reference
741 * > counting in this case. It seems that when the console
742 * > is open by kernel initially, this is not accounted
743 * > as an open, and uart_startup is not called.
744 *
745 * That is correct. We are unable to call uart_startup when the serial
746 * console is initialised because it may need to allocate memory (as
747 * request_irq does) and the memory allocators may not have been
748 * initialised.
749 *
750 * 1. initialise the port into a state where it can send characters in the
751 * console write method.
752 *
753 * 2. don't do the actual hardware shutdown in your shutdown() method (but
754 * do the normal software shutdown - ie, free irqs etc)
755 *****
756 */
757static void ip22zilog_shutdown(struct uart_port *port)
758{
759 struct uart_ip22zilog_port *up = UART_ZILOG(port);
760 struct zilog_channel *channel;
761 unsigned long flags;
762
763 if (ZS_IS_CONS(up))
764 return;
765
766 spin_lock_irqsave(&port->lock, flags);
767
768 channel = ZILOG_CHANNEL_FROM_PORT(port);
769
770 /* Disable receiver and transmitter. */
771 up->curregs[R3] &= ~RxENAB;
772 up->curregs[R5] &= ~TxENAB;
773
774 /* Disable all interrupts and BRK assertion. */
775 up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
776 up->curregs[R5] &= ~SND_BRK;
777 ip22zilog_maybe_update_regs(up, channel);
778
779 spin_unlock_irqrestore(&port->lock, flags);
780}
781
782/* Shared by TTY driver and serial console setup. The port lock is held
783 * and local interrupts are disabled.
784 */
785static void
786ip22zilog_convert_to_zs(struct uart_ip22zilog_port *up, unsigned int cflag,
787 unsigned int iflag, int brg)
788{
789
790 up->curregs[R10] = NRZ;
791 up->curregs[R11] = TCBR | RCBR;
792
793 /* Program BAUD and clock source. */
794 up->curregs[R4] &= ~XCLK_MASK;
795 up->curregs[R4] |= X16CLK;
796 up->curregs[R12] = brg & 0xff;
797 up->curregs[R13] = (brg >> 8) & 0xff;
798 up->curregs[R14] = BRENAB;
799
800 /* Character size, stop bits, and parity. */
801 up->curregs[3] &= ~RxN_MASK;
802 up->curregs[5] &= ~TxN_MASK;
803 switch (cflag & CSIZE) {
804 case CS5:
805 up->curregs[3] |= Rx5;
806 up->curregs[5] |= Tx5;
807 up->parity_mask = 0x1f;
808 break;
809 case CS6:
810 up->curregs[3] |= Rx6;
811 up->curregs[5] |= Tx6;
812 up->parity_mask = 0x3f;
813 break;
814 case CS7:
815 up->curregs[3] |= Rx7;
816 up->curregs[5] |= Tx7;
817 up->parity_mask = 0x7f;
818 break;
819 case CS8:
820 default:
821 up->curregs[3] |= Rx8;
822 up->curregs[5] |= Tx8;
823 up->parity_mask = 0xff;
824 break;
825 };
826 up->curregs[4] &= ~0x0c;
827 if (cflag & CSTOPB)
828 up->curregs[4] |= SB2;
829 else
830 up->curregs[4] |= SB1;
831 if (cflag & PARENB)
832 up->curregs[4] |= PAR_ENAB;
833 else
834 up->curregs[4] &= ~PAR_ENAB;
835 if (!(cflag & PARODD))
836 up->curregs[4] |= PAR_EVEN;
837 else
838 up->curregs[4] &= ~PAR_EVEN;
839
840 up->port.read_status_mask = Rx_OVR;
841 if (iflag & INPCK)
842 up->port.read_status_mask |= CRC_ERR | PAR_ERR;
843 if (iflag & (BRKINT | PARMRK))
844 up->port.read_status_mask |= BRK_ABRT;
845
846 up->port.ignore_status_mask = 0;
847 if (iflag & IGNPAR)
848 up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
849 if (iflag & IGNBRK) {
850 up->port.ignore_status_mask |= BRK_ABRT;
851 if (iflag & IGNPAR)
852 up->port.ignore_status_mask |= Rx_OVR;
853 }
854
855 if ((cflag & CREAD) == 0)
856 up->port.ignore_status_mask = 0xff;
857}
858
859/* The port lock is not held. */
860static void
861ip22zilog_set_termios(struct uart_port *port, struct termios *termios,
862 struct termios *old)
863{
864 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
865 unsigned long flags;
866 int baud, brg;
867
868 baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
869
870 spin_lock_irqsave(&up->port.lock, flags);
871
872 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
873
874 ip22zilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
875
876 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
877 up->flags |= IP22ZILOG_FLAG_MODEM_STATUS;
878 else
879 up->flags &= ~IP22ZILOG_FLAG_MODEM_STATUS;
880
881 up->cflag = termios->c_cflag;
882
883 ip22zilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
884
885 spin_unlock_irqrestore(&up->port.lock, flags);
886}
887
888static const char *ip22zilog_type(struct uart_port *port)
889{
890 return "IP22-Zilog";
891}
892
893/* We do not request/release mappings of the registers here, this
894 * happens at early serial probe time.
895 */
896static void ip22zilog_release_port(struct uart_port *port)
897{
898}
899
900static int ip22zilog_request_port(struct uart_port *port)
901{
902 return 0;
903}
904
905/* These do not need to do anything interesting either. */
906static void ip22zilog_config_port(struct uart_port *port, int flags)
907{
908}
909
910/* We do not support letting the user mess with the divisor, IRQ, etc. */
911static int ip22zilog_verify_port(struct uart_port *port, struct serial_struct *ser)
912{
913 return -EINVAL;
914}
915
916static struct uart_ops ip22zilog_pops = {
917 .tx_empty = ip22zilog_tx_empty,
918 .set_mctrl = ip22zilog_set_mctrl,
919 .get_mctrl = ip22zilog_get_mctrl,
920 .stop_tx = ip22zilog_stop_tx,
921 .start_tx = ip22zilog_start_tx,
922 .stop_rx = ip22zilog_stop_rx,
923 .enable_ms = ip22zilog_enable_ms,
924 .break_ctl = ip22zilog_break_ctl,
925 .startup = ip22zilog_startup,
926 .shutdown = ip22zilog_shutdown,
927 .set_termios = ip22zilog_set_termios,
928 .type = ip22zilog_type,
929 .release_port = ip22zilog_release_port,
930 .request_port = ip22zilog_request_port,
931 .config_port = ip22zilog_config_port,
932 .verify_port = ip22zilog_verify_port,
933};
934
935static struct uart_ip22zilog_port *ip22zilog_port_table;
936static struct zilog_layout **ip22zilog_chip_regs;
937
938static struct uart_ip22zilog_port *ip22zilog_irq_chain;
939static int zilog_irq = -1;
940
941static void * __init alloc_one_table(unsigned long size)
942{
943 void *ret;
944
945 ret = kmalloc(size, GFP_KERNEL);
946 if (ret != NULL)
947 memset(ret, 0, size);
948
949 return ret;
950}
951
952static void __init ip22zilog_alloc_tables(void)
953{
954 ip22zilog_port_table = (struct uart_ip22zilog_port *)
955 alloc_one_table(NUM_CHANNELS * sizeof(struct uart_ip22zilog_port));
956 ip22zilog_chip_regs = (struct zilog_layout **)
957 alloc_one_table(NUM_IP22ZILOG * sizeof(struct zilog_layout *));
958
959 if (ip22zilog_port_table == NULL || ip22zilog_chip_regs == NULL) {
960 panic("IP22-Zilog: Cannot allocate IP22-Zilog tables.");
961 }
962}
963
964/* Get the address of the registers for IP22-Zilog instance CHIP. */
965static struct zilog_layout * __init get_zs(int chip)
966{
967 unsigned long base;
968
969 if (chip < 0 || chip >= NUM_IP22ZILOG) {
970 panic("IP22-Zilog: Illegal chip number %d in get_zs.", chip);
971 }
972
973 /* Not probe-able, hard code it. */
974 base = (unsigned long) &sgioc->uart;
975
976 zilog_irq = SGI_SERIAL_IRQ;
977 request_mem_region(base, 8, "IP22-Zilog");
978
979 return (struct zilog_layout *) base;
980}
981
982#define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
983
984#ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
985static void ip22zilog_put_char(struct zilog_channel *channel, unsigned char ch)
986{
987 int loops = ZS_PUT_CHAR_MAX_DELAY;
988
989 /* This is a timed polling loop so do not switch the explicit
990 * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
991 */
992 do {
993 unsigned char val = readb(&channel->control);
994 if (val & Tx_BUF_EMP) {
995 ZSDELAY();
996 break;
997 }
998 udelay(5);
999 } while (--loops);
1000
1001 writeb(ch, &channel->data);
1002 ZSDELAY();
1003 ZS_WSYNC(channel);
1004}
1005
1006static void
1007ip22zilog_console_write(struct console *con, const char *s, unsigned int count)
1008{
1009 struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
1010 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1011 unsigned long flags;
1012 int i;
1013
1014 spin_lock_irqsave(&up->port.lock, flags);
1015 for (i = 0; i < count; i++, s++) {
1016 ip22zilog_put_char(channel, *s);
1017 if (*s == 10)
1018 ip22zilog_put_char(channel, 13);
1019 }
1020 udelay(2);
1021 spin_unlock_irqrestore(&up->port.lock, flags);
1022}
1023
1024void
1025ip22serial_console_termios(struct console *con, char *options)
1026{
1027 int baud = 9600, bits = 8, cflag;
1028 int parity = 'n';
1029 int flow = 'n';
1030
1031 if (options)
1032 uart_parse_options(options, &baud, &parity, &bits, &flow);
1033
1034 cflag = CREAD | HUPCL | CLOCAL;
1035
1036 switch (baud) {
1037 case 150: cflag |= B150; break;
1038 case 300: cflag |= B300; break;
1039 case 600: cflag |= B600; break;
1040 case 1200: cflag |= B1200; break;
1041 case 2400: cflag |= B2400; break;
1042 case 4800: cflag |= B4800; break;
1043 case 9600: cflag |= B9600; break;
1044 case 19200: cflag |= B19200; break;
1045 case 38400: cflag |= B38400; break;
1046 default: baud = 9600; cflag |= B9600; break;
1047 }
1048
1049 con->cflag = cflag | CS8; /* 8N1 */
1050}
1051
1052static int __init ip22zilog_console_setup(struct console *con, char *options)
1053{
1054 struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
1055 unsigned long flags;
1056 int baud, brg;
1057
1058 printk("Console: ttyS%d (IP22-Zilog)\n", con->index);
1059
1060 /* Get firmware console settings. */
1061 ip22serial_console_termios(con, options);
1062
1063 /* Firmware console speed is limited to 150-->38400 baud so
1064 * this hackish cflag thing is OK.
1065 */
1066 switch (con->cflag & CBAUD) {
1067 case B150: baud = 150; break;
1068 case B300: baud = 300; break;
1069 case B600: baud = 600; break;
1070 case B1200: baud = 1200; break;
1071 case B2400: baud = 2400; break;
1072 case B4800: baud = 4800; break;
1073 default: case B9600: baud = 9600; break;
1074 case B19200: baud = 19200; break;
1075 case B38400: baud = 38400; break;
1076 };
1077
1078 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1079
1080 spin_lock_irqsave(&up->port.lock, flags);
1081
1082 up->curregs[R15] = BRKIE;
1083 ip22zilog_convert_to_zs(up, con->cflag, 0, brg);
1084
1085 __ip22zilog_startup(up);
1086
1087 spin_unlock_irqrestore(&up->port.lock, flags);
1088
1089 return 0;
1090}
1091
1092static struct uart_driver ip22zilog_reg;
1093
1094static struct console ip22zilog_console = {
1095 .name = "ttyS",
1096 .write = ip22zilog_console_write,
1097 .device = uart_console_device,
1098 .setup = ip22zilog_console_setup,
1099 .flags = CON_PRINTBUFFER,
1100 .index = -1,
1101 .data = &ip22zilog_reg,
1102};
1103#endif /* CONFIG_SERIAL_IP22_ZILOG_CONSOLE */
1104
1105static struct uart_driver ip22zilog_reg = {
1106 .owner = THIS_MODULE,
1107 .driver_name = "serial",
1108 .devfs_name = "tts/",
1109 .dev_name = "ttyS",
1110 .major = TTY_MAJOR,
1111 .minor = 64,
1112 .nr = NUM_CHANNELS,
1113#ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
1114 .cons = &ip22zilog_console,
1115#endif
1116};
1117
1118static void __init ip22zilog_prepare(void)
1119{
1120 struct uart_ip22zilog_port *up;
1121 struct zilog_layout *rp;
1122 int channel, chip;
1123
1124 /*
1125 * Temporary fix.
1126 */
1127 for (channel = 0; channel < NUM_CHANNELS; channel++)
1128 spin_lock_init(&ip22zilog_port_table[channel].port.lock);
1129
1130 ip22zilog_irq_chain = &ip22zilog_port_table[NUM_CHANNELS - 1];
1131 up = &ip22zilog_port_table[0];
1132 for (channel = NUM_CHANNELS - 1 ; channel > 0; channel--)
1133 up[channel].next = &up[channel - 1];
1134 up[channel].next = NULL;
1135
1136 for (chip = 0; chip < NUM_IP22ZILOG; chip++) {
1137 if (!ip22zilog_chip_regs[chip]) {
1138 ip22zilog_chip_regs[chip] = rp = get_zs(chip);
1139
1140 up[(chip * 2) + 0].port.membase = (char *) &rp->channelB;
1141 up[(chip * 2) + 1].port.membase = (char *) &rp->channelA;
1142
1143 /* In theory mapbase is the physical address ... */
1144 up[(chip * 2) + 0].port.mapbase =
1145 (unsigned long) ioremap((unsigned long) &rp->channelB, 8);
1146 up[(chip * 2) + 1].port.mapbase =
1147 (unsigned long) ioremap((unsigned long) &rp->channelA, 8);
1148 }
1149
1150 /* Channel A */
1151 up[(chip * 2) + 0].port.iotype = UPIO_MEM;
1152 up[(chip * 2) + 0].port.irq = zilog_irq;
1153 up[(chip * 2) + 0].port.uartclk = ZS_CLOCK;
1154 up[(chip * 2) + 0].port.fifosize = 1;
1155 up[(chip * 2) + 0].port.ops = &ip22zilog_pops;
1156 up[(chip * 2) + 0].port.type = PORT_IP22ZILOG;
1157 up[(chip * 2) + 0].port.flags = 0;
1158 up[(chip * 2) + 0].port.line = (chip * 2) + 0;
1159 up[(chip * 2) + 0].flags = 0;
1160
1161 /* Channel B */
1162 up[(chip * 2) + 1].port.iotype = UPIO_MEM;
1163 up[(chip * 2) + 1].port.irq = zilog_irq;
1164 up[(chip * 2) + 1].port.uartclk = ZS_CLOCK;
1165 up[(chip * 2) + 1].port.fifosize = 1;
1166 up[(chip * 2) + 1].port.ops = &ip22zilog_pops;
1167 up[(chip * 2) + 1].port.type = PORT_IP22ZILOG;
1168 up[(chip * 2) + 1].port.flags |= IP22ZILOG_FLAG_IS_CHANNEL_A;
1169 up[(chip * 2) + 1].port.line = (chip * 2) + 1;
1170 up[(chip * 2) + 1].flags = 0;
1171 }
1172}
1173
1174static void __init ip22zilog_init_hw(void)
1175{
1176 int i;
1177
1178 for (i = 0; i < NUM_CHANNELS; i++) {
1179 struct uart_ip22zilog_port *up = &ip22zilog_port_table[i];
1180 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1181 unsigned long flags;
1182 int baud, brg;
1183
1184 spin_lock_irqsave(&up->port.lock, flags);
1185
1186 if (ZS_IS_CHANNEL_A(up)) {
1187 write_zsreg(channel, R9, FHWRES);
1188 ZSDELAY_LONG();
1189 (void) read_zsreg(channel, R0);
1190 }
1191
1192 /* Normal serial TTY. */
1193 up->parity_mask = 0xff;
1194 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1195 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1196 up->curregs[R3] = RxENAB | Rx8;
1197 up->curregs[R5] = TxENAB | Tx8;
1198 up->curregs[R9] = NV | MIE;
1199 up->curregs[R10] = NRZ;
1200 up->curregs[R11] = TCBR | RCBR;
1201 baud = 9600;
1202 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1203 up->curregs[R12] = (brg & 0xff);
1204 up->curregs[R13] = (brg >> 8) & 0xff;
1205 up->curregs[R14] = BRENAB;
1206 __load_zsregs(channel, up->curregs);
1207 /* set master interrupt enable */
1208 write_zsreg(channel, R9, up->curregs[R9]);
1209
1210 spin_unlock_irqrestore(&up->port.lock, flags);
1211 }
1212}
1213
1214static int __init ip22zilog_ports_init(void)
1215{
1216 int ret;
1217
1218 printk(KERN_INFO "Serial: IP22 Zilog driver (%d chips).\n", NUM_IP22ZILOG);
1219
1220 ip22zilog_prepare();
1221
1222 if (request_irq(zilog_irq, ip22zilog_interrupt, 0,
1223 "IP22-Zilog", ip22zilog_irq_chain)) {
1224 panic("IP22-Zilog: Unable to register zs interrupt handler.\n");
1225 }
1226
1227 ip22zilog_init_hw();
1228
1229 ret = uart_register_driver(&ip22zilog_reg);
1230 if (ret == 0) {
1231 int i;
1232
1233 for (i = 0; i < NUM_CHANNELS; i++) {
1234 struct uart_ip22zilog_port *up = &ip22zilog_port_table[i];
1235
1236 uart_add_one_port(&ip22zilog_reg, &up->port);
1237 }
1238 }
1239
1240 return ret;
1241}
1242
1243static int __init ip22zilog_init(void)
1244{
1245 /* IP22 Zilog setup is hard coded, no probing to do. */
1246 ip22zilog_alloc_tables();
1247 ip22zilog_ports_init();
1248
1249 return 0;
1250}
1251
1252static void __exit ip22zilog_exit(void)
1253{
1254 int i;
1255
1256 for (i = 0; i < NUM_CHANNELS; i++) {
1257 struct uart_ip22zilog_port *up = &ip22zilog_port_table[i];
1258
1259 uart_remove_one_port(&ip22zilog_reg, &up->port);
1260 }
1261
1262 uart_unregister_driver(&ip22zilog_reg);
1263}
1264
1265module_init(ip22zilog_init);
1266module_exit(ip22zilog_exit);
1267
1268/* David wrote it but I'm to blame for the bugs ... */
1269MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1270MODULE_DESCRIPTION("SGI Zilog serial port driver");
1271MODULE_LICENSE("GPL");
diff --git a/drivers/serial/ip22zilog.h b/drivers/serial/ip22zilog.h
new file mode 100644
index 000000000000..a59a9a8341d2
--- /dev/null
+++ b/drivers/serial/ip22zilog.h
@@ -0,0 +1,281 @@
1#ifndef _IP22_ZILOG_H
2#define _IP22_ZILOG_H
3
4#include <asm/byteorder.h>
5
6struct zilog_channel {
7#ifdef __BIG_ENDIAN
8 volatile unsigned char unused0[3];
9 volatile unsigned char control;
10 volatile unsigned char unused1[3];
11 volatile unsigned char data;
12#else /* __LITTLE_ENDIAN */
13 volatile unsigned char control;
14 volatile unsigned char unused0[3];
15 volatile unsigned char data;
16 volatile unsigned char unused1[3];
17#endif
18};
19
20struct zilog_layout {
21 struct zilog_channel channelB;
22 struct zilog_channel channelA;
23};
24
25#define NUM_ZSREGS 16
26
27/* Conversion routines to/from brg time constants from/to bits
28 * per second.
29 */
30#define BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
31#define BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
32
33/* The Zilog register set */
34
35#define FLAG 0x7e
36
37/* Write Register 0 */
38#define R0 0 /* Register selects */
39#define R1 1
40#define R2 2
41#define R3 3
42#define R4 4
43#define R5 5
44#define R6 6
45#define R7 7
46#define R8 8
47#define R9 9
48#define R10 10
49#define R11 11
50#define R12 12
51#define R13 13
52#define R14 14
53#define R15 15
54
55#define NULLCODE 0 /* Null Code */
56#define POINT_HIGH 0x8 /* Select upper half of registers */
57#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
58#define SEND_ABORT 0x18 /* HDLC Abort */
59#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
60#define RES_Tx_P 0x28 /* Reset TxINT Pending */
61#define ERR_RES 0x30 /* Error Reset */
62#define RES_H_IUS 0x38 /* Reset highest IUS */
63
64#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
65#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
66#define RES_EOM_L 0xC0 /* Reset EOM latch */
67
68/* Write Register 1 */
69
70#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
71#define TxINT_ENAB 0x2 /* Tx Int Enable */
72#define PAR_SPEC 0x4 /* Parity is special condition */
73
74#define RxINT_DISAB 0 /* Rx Int Disable */
75#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
76#define INT_ALL_Rx 0x10 /* Int on all Rx Characters or error */
77#define INT_ERR_Rx 0x18 /* Int on error only */
78#define RxINT_MASK 0x18
79
80#define WT_RDY_RT 0x20 /* Wait/Ready on R/T */
81#define WT_FN_RDYFN 0x40 /* Wait/FN/Ready FN */
82#define WT_RDY_ENAB 0x80 /* Wait/Ready Enable */
83
84/* Write Register #2 (Interrupt Vector) */
85
86/* Write Register 3 */
87
88#define RxENAB 0x1 /* Rx Enable */
89#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
90#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
91#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
92#define ENT_HM 0x10 /* Enter Hunt Mode */
93#define AUTO_ENAB 0x20 /* Auto Enables */
94#define Rx5 0x0 /* Rx 5 Bits/Character */
95#define Rx7 0x40 /* Rx 7 Bits/Character */
96#define Rx6 0x80 /* Rx 6 Bits/Character */
97#define Rx8 0xc0 /* Rx 8 Bits/Character */
98#define RxN_MASK 0xc0
99
100/* Write Register 4 */
101
102#define PAR_ENAB 0x1 /* Parity Enable */
103#define PAR_EVEN 0x2 /* Parity Even/Odd* */
104
105#define SYNC_ENAB 0 /* Sync Modes Enable */
106#define SB1 0x4 /* 1 stop bit/char */
107#define SB15 0x8 /* 1.5 stop bits/char */
108#define SB2 0xc /* 2 stop bits/char */
109
110#define MONSYNC 0 /* 8 Bit Sync character */
111#define BISYNC 0x10 /* 16 bit sync character */
112#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
113#define EXTSYNC 0x30 /* External Sync Mode */
114
115#define X1CLK 0x0 /* x1 clock mode */
116#define X16CLK 0x40 /* x16 clock mode */
117#define X32CLK 0x80 /* x32 clock mode */
118#define X64CLK 0xC0 /* x64 clock mode */
119#define XCLK_MASK 0xC0
120
121/* Write Register 5 */
122
123#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
124#define RTS 0x2 /* RTS */
125#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
126#define TxENAB 0x8 /* Tx Enable */
127#define SND_BRK 0x10 /* Send Break */
128#define Tx5 0x0 /* Tx 5 bits (or less)/character */
129#define Tx7 0x20 /* Tx 7 bits/character */
130#define Tx6 0x40 /* Tx 6 bits/character */
131#define Tx8 0x60 /* Tx 8 bits/character */
132#define TxN_MASK 0x60
133#define DTR 0x80 /* DTR */
134
135/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
136
137/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
138
139/* Write Register 8 (transmit buffer) */
140
141/* Write Register 9 (Master interrupt control) */
142#define VIS 1 /* Vector Includes Status */
143#define NV 2 /* No Vector */
144#define DLC 4 /* Disable Lower Chain */
145#define MIE 8 /* Master Interrupt Enable */
146#define STATHI 0x10 /* Status high */
147#define NORESET 0 /* No reset on write to R9 */
148#define CHRB 0x40 /* Reset channel B */
149#define CHRA 0x80 /* Reset channel A */
150#define FHWRES 0xc0 /* Force hardware reset */
151
152/* Write Register 10 (misc control bits) */
153#define BIT6 1 /* 6 bit/8bit sync */
154#define LOOPMODE 2 /* SDLC Loop mode */
155#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
156#define MARKIDLE 8 /* Mark/flag on idle */
157#define GAOP 0x10 /* Go active on poll */
158#define NRZ 0 /* NRZ mode */
159#define NRZI 0x20 /* NRZI mode */
160#define FM1 0x40 /* FM1 (transition = 1) */
161#define FM0 0x60 /* FM0 (transition = 0) */
162#define CRCPS 0x80 /* CRC Preset I/O */
163
164/* Write Register 11 (Clock Mode control) */
165#define TRxCXT 0 /* TRxC = Xtal output */
166#define TRxCTC 1 /* TRxC = Transmit clock */
167#define TRxCBR 2 /* TRxC = BR Generator Output */
168#define TRxCDP 3 /* TRxC = DPLL output */
169#define TRxCOI 4 /* TRxC O/I */
170#define TCRTxCP 0 /* Transmit clock = RTxC pin */
171#define TCTRxCP 8 /* Transmit clock = TRxC pin */
172#define TCBR 0x10 /* Transmit clock = BR Generator output */
173#define TCDPLL 0x18 /* Transmit clock = DPLL output */
174#define RCRTxCP 0 /* Receive clock = RTxC pin */
175#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
176#define RCBR 0x40 /* Receive clock = BR Generator output */
177#define RCDPLL 0x60 /* Receive clock = DPLL output */
178#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
179
180/* Write Register 12 (lower byte of baud rate generator time constant) */
181
182/* Write Register 13 (upper byte of baud rate generator time constant) */
183
184/* Write Register 14 (Misc control bits) */
185#define BRENAB 1 /* Baud rate generator enable */
186#define BRSRC 2 /* Baud rate generator source */
187#define DTRREQ 4 /* DTR/Request function */
188#define AUTOECHO 8 /* Auto Echo */
189#define LOOPBAK 0x10 /* Local loopback */
190#define SEARCH 0x20 /* Enter search mode */
191#define RMC 0x40 /* Reset missing clock */
192#define DISDPLL 0x60 /* Disable DPLL */
193#define SSBR 0x80 /* Set DPLL source = BR generator */
194#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
195#define SFMM 0xc0 /* Set FM mode */
196#define SNRZI 0xe0 /* Set NRZI mode */
197
198/* Write Register 15 (external/status interrupt control) */
199#define ZCIE 2 /* Zero count IE */
200#define DCDIE 8 /* DCD IE */
201#define SYNCIE 0x10 /* Sync/hunt IE */
202#define CTSIE 0x20 /* CTS IE */
203#define TxUIE 0x40 /* Tx Underrun/EOM IE */
204#define BRKIE 0x80 /* Break/Abort IE */
205
206
207/* Read Register 0 */
208#define Rx_CH_AV 0x1 /* Rx Character Available */
209#define ZCOUNT 0x2 /* Zero count */
210#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
211#define DCD 0x8 /* DCD */
212#define SYNC 0x10 /* Sync/hunt */
213#define CTS 0x20 /* CTS */
214#define TxEOM 0x40 /* Tx underrun */
215#define BRK_ABRT 0x80 /* Break/Abort */
216
217/* Read Register 1 */
218#define ALL_SNT 0x1 /* All sent */
219/* Residue Data for 8 Rx bits/char programmed */
220#define RES3 0x8 /* 0/3 */
221#define RES4 0x4 /* 0/4 */
222#define RES5 0xc /* 0/5 */
223#define RES6 0x2 /* 0/6 */
224#define RES7 0xa /* 0/7 */
225#define RES8 0x6 /* 0/8 */
226#define RES18 0xe /* 1/8 */
227#define RES28 0x0 /* 2/8 */
228/* Special Rx Condition Interrupts */
229#define PAR_ERR 0x10 /* Parity error */
230#define Rx_OVR 0x20 /* Rx Overrun Error */
231#define CRC_ERR 0x40 /* CRC/Framing Error */
232#define END_FR 0x80 /* End of Frame (SDLC) */
233
234/* Read Register 2 (channel b only) - Interrupt vector */
235#define CHB_Tx_EMPTY 0x00
236#define CHB_EXT_STAT 0x02
237#define CHB_Rx_AVAIL 0x04
238#define CHB_SPECIAL 0x06
239#define CHA_Tx_EMPTY 0x08
240#define CHA_EXT_STAT 0x0a
241#define CHA_Rx_AVAIL 0x0c
242#define CHA_SPECIAL 0x0e
243#define STATUS_MASK 0x0e
244
245/* Read Register 3 (interrupt pending register) ch a only */
246#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
247#define CHBTxIP 0x2 /* Channel B Tx IP */
248#define CHBRxIP 0x4 /* Channel B Rx IP */
249#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
250#define CHATxIP 0x10 /* Channel A Tx IP */
251#define CHARxIP 0x20 /* Channel A Rx IP */
252
253/* Read Register 8 (receive data register) */
254
255/* Read Register 10 (misc status bits) */
256#define ONLOOP 2 /* On loop */
257#define LOOPSEND 0x10 /* Loop sending */
258#define CLK2MIS 0x40 /* Two clocks missing */
259#define CLK1MIS 0x80 /* One clock missing */
260
261/* Read Register 12 (lower byte of baud rate generator constant) */
262
263/* Read Register 13 (upper byte of baud rate generator constant) */
264
265/* Read Register 15 (value of WR 15) */
266
267/* Misc macros */
268#define ZS_CLEARERR(channel) do { writeb(ERR_RES, &channel->control); \
269 udelay(5); } while(0)
270
271#define ZS_CLEARSTAT(channel) do { writeb(RES_EXT_INT, &channel->control); \
272 udelay(5); } while(0)
273
274#define ZS_CLEARFIFO(channel) do { readb(&channel->data); \
275 udelay(2); \
276 readb(&channel->data); \
277 udelay(2); \
278 readb(&channel->data); \
279 udelay(2); } while(0)
280
281#endif /* _IP22_ZILOG_H */
diff --git a/drivers/serial/jsm/Makefile b/drivers/serial/jsm/Makefile
new file mode 100644
index 000000000000..e46b6e0f8b18
--- /dev/null
+++ b/drivers/serial/jsm/Makefile
@@ -0,0 +1,8 @@
1#
2# Makefile for Jasmine adapter
3#
4
5obj-$(CONFIG_SERIAL_JSM) += jsm.o
6
7jsm-objs := jsm_driver.o jsm_neo.o jsm_tty.o
8
diff --git a/drivers/serial/jsm/jsm.h b/drivers/serial/jsm/jsm.h
new file mode 100644
index 000000000000..b12ee02a0f7c
--- /dev/null
+++ b/drivers/serial/jsm/jsm.h
@@ -0,0 +1,437 @@
1/************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
3 *
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
23 * Wendy Xiong <wendyx@us.ltcfwd.linux.ibm.com>
24 *
25 ***********************************************************************/
26
27#ifndef __JSM_DRIVER_H
28#define __JSM_DRIVER_H
29
30#include <linux/kernel.h>
31#include <linux/version.h>
32#include <linux/types.h> /* To pick up the varions Linux types */
33#include <linux/tty.h>
34#include <linux/serial_core.h>
35#include <linux/device.h>
36
37/*
38 * Debugging levels can be set using debug insmod variable
39 * They can also be compiled out completely.
40 */
41enum {
42 DBG_INIT = 0x01,
43 DBG_BASIC = 0x02,
44 DBG_CORE = 0x04,
45 DBG_OPEN = 0x08,
46 DBG_CLOSE = 0x10,
47 DBG_READ = 0x20,
48 DBG_WRITE = 0x40,
49 DBG_IOCTL = 0x80,
50 DBG_PROC = 0x100,
51 DBG_PARAM = 0x200,
52 DBG_PSCAN = 0x400,
53 DBG_EVENT = 0x800,
54 DBG_DRAIN = 0x1000,
55 DBG_MSIGS = 0x2000,
56 DBG_MGMT = 0x4000,
57 DBG_INTR = 0x8000,
58 DBG_CARR = 0x10000,
59};
60
61#define jsm_printk(nlevel, klevel, pdev, fmt, args...) \
62 if ((DBG_##nlevel & jsm_debug)) \
63 dev_printk(KERN_##klevel, pdev->dev, fmt, ## args)
64
65#define MAXPORTS 8
66#define MAX_STOPS_SENT 5
67
68/* Board type definitions */
69
70#define T_NEO 0000
71#define T_CLASSIC 0001
72#define T_PCIBUS 0400
73
74/* Board State Definitions */
75
76#define BD_RUNNING 0x0
77#define BD_REASON 0x7f
78#define BD_NOTFOUND 0x1
79#define BD_NOIOPORT 0x2
80#define BD_NOMEM 0x3
81#define BD_NOBIOS 0x4
82#define BD_NOFEP 0x5
83#define BD_FAILED 0x6
84#define BD_ALLOCATED 0x7
85#define BD_TRIBOOT 0x8
86#define BD_BADKME 0x80
87
88
89/* 4 extra for alignment play space */
90#define WRITEBUFLEN ((4096) + 4)
91#define MYFLIPLEN N_TTY_BUF_SIZE
92
93#define JSM_VERSION "jsm: 1.1-1-INKERNEL"
94#define JSM_PARTNUM "40002438_A-INKERNEL"
95
96/*
97 * All the possible states the driver can be while being loaded.
98 */
99enum {
100 DRIVER_INITIALIZED = 0,
101 DRIVER_READY
102};
103
104/*
105 * All the possible states the board can be while booting up.
106 */
107enum {
108 BOARD_FAILED = 0,
109 BOARD_FOUND,
110 BOARD_READY
111};
112
113struct board_id {
114 u8 *name;
115 u32 maxports;
116};
117
118struct jsm_board;
119struct jsm_channel;
120
121/************************************************************************
122 * Per board operations structure *
123 ************************************************************************/
124struct board_ops {
125 irqreturn_t (*intr) (int irq, void *voidbrd, struct pt_regs *regs);
126 void (*uart_init) (struct jsm_channel *ch);
127 void (*uart_off) (struct jsm_channel *ch);
128 void (*param) (struct jsm_channel *ch);
129 void (*assert_modem_signals) (struct jsm_channel *ch);
130 void (*flush_uart_write) (struct jsm_channel *ch);
131 void (*flush_uart_read) (struct jsm_channel *ch);
132 void (*disable_receiver) (struct jsm_channel *ch);
133 void (*enable_receiver) (struct jsm_channel *ch);
134 void (*send_break) (struct jsm_channel *ch);
135 void (*clear_break) (struct jsm_channel *ch, int);
136 void (*send_start_character) (struct jsm_channel *ch);
137 void (*send_stop_character) (struct jsm_channel *ch);
138 void (*copy_data_from_queue_to_uart) (struct jsm_channel *ch);
139 u32 (*get_uart_bytes_left) (struct jsm_channel *ch);
140 void (*send_immediate_char) (struct jsm_channel *ch, unsigned char);
141};
142
143
144/*
145 * Per-board information
146 */
147struct jsm_board
148{
149 int boardnum; /* Board number: 0-32 */
150
151 int type; /* Type of board */
152 char *name; /* Product Name */
153 u8 rev; /* PCI revision ID */
154 struct pci_dev *pci_dev;
155 u32 maxports; /* MAX ports this board can handle */
156
157 spinlock_t bd_lock; /* Used to protect board */
158
159 spinlock_t bd_intr_lock; /* Used to protect the poller tasklet and
160 * the interrupt routine from each other.
161 */
162
163 u32 state; /* State of card. */
164 wait_queue_head_t state_wait; /* Place to sleep on for state change */
165
166 u32 nasync; /* Number of ports on card */
167
168 u32 irq; /* Interrupt request number */
169 u64 intr_count; /* Count of interrupts */
170
171 u64 membase; /* Start of base memory of the card */
172 u64 membase_end; /* End of base memory of the card */
173
174 u8 __iomem *re_map_membase;/* Remapped memory of the card */
175
176 u64 iobase; /* Start of io base of the card */
177 u64 iobase_end; /* End of io base of the card */
178
179 u32 bd_uart_offset; /* Space between each UART */
180
181 struct jsm_channel *channels[MAXPORTS]; /* array of pointers to our channels. */
182 char *flipbuf; /* Our flip buffer, alloced if board is found */
183
184 u16 dpatype; /* The board "type", as defined by DPA */
185 u16 dpastatus; /* The board "status", as defined by DPA */
186
187 u32 bd_dividend; /* Board/UARTs specific dividend */
188
189 struct board_ops *bd_ops;
190
191 struct list_head jsm_board_entry;
192};
193
194/************************************************************************
195 * Device flag definitions for ch_flags.
196 ************************************************************************/
197#define CH_PRON 0x0001 /* Printer on string */
198#define CH_STOP 0x0002 /* Output is stopped */
199#define CH_STOPI 0x0004 /* Input is stopped */
200#define CH_CD 0x0008 /* Carrier is present */
201#define CH_FCAR 0x0010 /* Carrier forced on */
202#define CH_HANGUP 0x0020 /* Hangup received */
203
204#define CH_RECEIVER_OFF 0x0040 /* Receiver is off */
205#define CH_OPENING 0x0080 /* Port in fragile open state */
206#define CH_CLOSING 0x0100 /* Port in fragile close state */
207#define CH_FIFO_ENABLED 0x0200 /* Port has FIFOs enabled */
208#define CH_TX_FIFO_EMPTY 0x0400 /* TX Fifo is completely empty */
209#define CH_TX_FIFO_LWM 0x0800 /* TX Fifo is below Low Water */
210#define CH_BREAK_SENDING 0x1000 /* Break is being sent */
211#define CH_LOOPBACK 0x2000 /* Channel is in lookback mode */
212#define CH_FLIPBUF_IN_USE 0x4000 /* Channel's flipbuf is in use */
213#define CH_BAUD0 0x08000 /* Used for checking B0 transitions */
214
215/* Our Read/Error/Write queue sizes */
216#define RQUEUEMASK 0x1FFF /* 8 K - 1 */
217#define EQUEUEMASK 0x1FFF /* 8 K - 1 */
218#define WQUEUEMASK 0x0FFF /* 4 K - 1 */
219#define RQUEUESIZE (RQUEUEMASK + 1)
220#define EQUEUESIZE RQUEUESIZE
221#define WQUEUESIZE (WQUEUEMASK + 1)
222
223
224/************************************************************************
225 * Channel information structure.
226 ************************************************************************/
227struct jsm_channel {
228 struct uart_port uart_port;
229 struct jsm_board *ch_bd; /* Board structure pointer */
230
231 spinlock_t ch_lock; /* provide for serialization */
232 wait_queue_head_t ch_flags_wait;
233
234 u32 ch_portnum; /* Port number, 0 offset. */
235 u32 ch_open_count; /* open count */
236 u32 ch_flags; /* Channel flags */
237
238 u64 ch_close_delay; /* How long we should drop RTS/DTR for */
239
240 u64 ch_cpstime; /* Time for CPS calculations */
241
242 tcflag_t ch_c_iflag; /* channel iflags */
243 tcflag_t ch_c_cflag; /* channel cflags */
244 tcflag_t ch_c_oflag; /* channel oflags */
245 tcflag_t ch_c_lflag; /* channel lflags */
246 u8 ch_stopc; /* Stop character */
247 u8 ch_startc; /* Start character */
248
249 u32 ch_old_baud; /* Cache of the current baud */
250 u32 ch_custom_speed;/* Custom baud, if set */
251
252 u32 ch_wopen; /* Waiting for open process cnt */
253
254 u8 ch_mostat; /* FEP output modem status */
255 u8 ch_mistat; /* FEP input modem status */
256
257 struct neo_uart_struct __iomem *ch_neo_uart; /* Pointer to the "mapped" UART struct */
258 u8 ch_cached_lsr; /* Cached value of the LSR register */
259
260 u8 *ch_rqueue; /* Our read queue buffer - malloc'ed */
261 u16 ch_r_head; /* Head location of the read queue */
262 u16 ch_r_tail; /* Tail location of the read queue */
263
264 u8 *ch_equeue; /* Our error queue buffer - malloc'ed */
265 u16 ch_e_head; /* Head location of the error queue */
266 u16 ch_e_tail; /* Tail location of the error queue */
267
268 u8 *ch_wqueue; /* Our write queue buffer - malloc'ed */
269 u16 ch_w_head; /* Head location of the write queue */
270 u16 ch_w_tail; /* Tail location of the write queue */
271
272 u64 ch_rxcount; /* total of data received so far */
273 u64 ch_txcount; /* total of data transmitted so far */
274
275 u8 ch_r_tlevel; /* Receive Trigger level */
276 u8 ch_t_tlevel; /* Transmit Trigger level */
277
278 u8 ch_r_watermark; /* Receive Watermark */
279
280
281 u32 ch_stops_sent; /* How many times I have sent a stop character
282 * to try to stop the other guy sending.
283 */
284 u64 ch_err_parity; /* Count of parity errors on channel */
285 u64 ch_err_frame; /* Count of framing errors on channel */
286 u64 ch_err_break; /* Count of breaks on channel */
287 u64 ch_err_overrun; /* Count of overruns on channel */
288
289 u64 ch_xon_sends; /* Count of xons transmitted */
290 u64 ch_xoff_sends; /* Count of xoffs transmitted */
291};
292
293
294/************************************************************************
295 * Per channel/port NEO UART structure *
296 ************************************************************************
297 * Base Structure Entries Usage Meanings to Host *
298 * *
299 * W = read write R = read only *
300 * U = Unused. *
301 ************************************************************************/
302
303struct neo_uart_struct {
304 u8 txrx; /* WR RHR/THR - Holding Reg */
305 u8 ier; /* WR IER - Interrupt Enable Reg */
306 u8 isr_fcr; /* WR ISR/FCR - Interrupt Status Reg/Fifo Control Reg */
307 u8 lcr; /* WR LCR - Line Control Reg */
308 u8 mcr; /* WR MCR - Modem Control Reg */
309 u8 lsr; /* WR LSR - Line Status Reg */
310 u8 msr; /* WR MSR - Modem Status Reg */
311 u8 spr; /* WR SPR - Scratch Pad Reg */
312 u8 fctr; /* WR FCTR - Feature Control Reg */
313 u8 efr; /* WR EFR - Enhanced Function Reg */
314 u8 tfifo; /* WR TXCNT/TXTRG - Transmit FIFO Reg */
315 u8 rfifo; /* WR RXCNT/RXTRG - Recieve FIFO Reg */
316 u8 xoffchar1; /* WR XOFF 1 - XOff Character 1 Reg */
317 u8 xoffchar2; /* WR XOFF 2 - XOff Character 2 Reg */
318 u8 xonchar1; /* WR XON 1 - Xon Character 1 Reg */
319 u8 xonchar2; /* WR XON 2 - XOn Character 2 Reg */
320
321 u8 reserved1[0x2ff - 0x200]; /* U Reserved by Exar */
322 u8 txrxburst[64]; /* RW 64 bytes of RX/TX FIFO Data */
323 u8 reserved2[0x37f - 0x340]; /* U Reserved by Exar */
324 u8 rxburst_with_errors[64]; /* R 64 bytes of RX FIFO Data + LSR */
325};
326
327/* Where to read the extended interrupt register (32bits instead of 8bits) */
328#define UART_17158_POLL_ADDR_OFFSET 0x80
329
330/*
331 * These are the redefinitions for the FCTR on the XR17C158, since
332 * Exar made them different than their earlier design. (XR16C854)
333 */
334
335/* These are only applicable when table D is selected */
336#define UART_17158_FCTR_RTS_NODELAY 0x00
337#define UART_17158_FCTR_RTS_4DELAY 0x01
338#define UART_17158_FCTR_RTS_6DELAY 0x02
339#define UART_17158_FCTR_RTS_8DELAY 0x03
340#define UART_17158_FCTR_RTS_12DELAY 0x12
341#define UART_17158_FCTR_RTS_16DELAY 0x05
342#define UART_17158_FCTR_RTS_20DELAY 0x13
343#define UART_17158_FCTR_RTS_24DELAY 0x06
344#define UART_17158_FCTR_RTS_28DELAY 0x14
345#define UART_17158_FCTR_RTS_32DELAY 0x07
346#define UART_17158_FCTR_RTS_36DELAY 0x16
347#define UART_17158_FCTR_RTS_40DELAY 0x08
348#define UART_17158_FCTR_RTS_44DELAY 0x09
349#define UART_17158_FCTR_RTS_48DELAY 0x10
350#define UART_17158_FCTR_RTS_52DELAY 0x11
351
352#define UART_17158_FCTR_RTS_IRDA 0x10
353#define UART_17158_FCTR_RS485 0x20
354#define UART_17158_FCTR_TRGA 0x00
355#define UART_17158_FCTR_TRGB 0x40
356#define UART_17158_FCTR_TRGC 0x80
357#define UART_17158_FCTR_TRGD 0xC0
358
359/* 17158 trigger table selects.. */
360#define UART_17158_FCTR_BIT6 0x40
361#define UART_17158_FCTR_BIT7 0x80
362
363/* 17158 TX/RX memmapped buffer offsets */
364#define UART_17158_RX_FIFOSIZE 64
365#define UART_17158_TX_FIFOSIZE 64
366
367/* 17158 Extended IIR's */
368#define UART_17158_IIR_RDI_TIMEOUT 0x0C /* Receiver data TIMEOUT */
369#define UART_17158_IIR_XONXOFF 0x10 /* Received an XON/XOFF char */
370#define UART_17158_IIR_HWFLOW_STATE_CHANGE 0x20 /* CTS/DSR or RTS/DTR state change */
371#define UART_17158_IIR_FIFO_ENABLED 0xC0 /* 16550 FIFOs are Enabled */
372
373/*
374 * These are the extended interrupts that get sent
375 * back to us from the UART's 32bit interrupt register
376 */
377#define UART_17158_RX_LINE_STATUS 0x1 /* RX Ready */
378#define UART_17158_RXRDY_TIMEOUT 0x2 /* RX Ready Timeout */
379#define UART_17158_TXRDY 0x3 /* TX Ready */
380#define UART_17158_MSR 0x4 /* Modem State Change */
381#define UART_17158_TX_AND_FIFO_CLR 0x40 /* Transmitter Holding Reg Empty */
382#define UART_17158_RX_FIFO_DATA_ERROR 0x80 /* UART detected an RX FIFO Data error */
383
384/*
385 * These are the EXTENDED definitions for the 17C158's Interrupt
386 * Enable Register.
387 */
388#define UART_17158_EFR_ECB 0x10 /* Enhanced control bit */
389#define UART_17158_EFR_IXON 0x2 /* Receiver compares Xon1/Xoff1 */
390#define UART_17158_EFR_IXOFF 0x8 /* Transmit Xon1/Xoff1 */
391#define UART_17158_EFR_RTSDTR 0x40 /* Auto RTS/DTR Flow Control Enable */
392#define UART_17158_EFR_CTSDSR 0x80 /* Auto CTS/DSR Flow COntrol Enable */
393
394#define UART_17158_XOFF_DETECT 0x1 /* Indicates whether chip saw an incoming XOFF char */
395#define UART_17158_XON_DETECT 0x2 /* Indicates whether chip saw an incoming XON char */
396
397#define UART_17158_IER_RSVD1 0x10 /* Reserved by Exar */
398#define UART_17158_IER_XOFF 0x20 /* Xoff Interrupt Enable */
399#define UART_17158_IER_RTSDTR 0x40 /* Output Interrupt Enable */
400#define UART_17158_IER_CTSDSR 0x80 /* Input Interrupt Enable */
401
402#define PCI_DEVICE_NEO_2DB9_PCI_NAME "Neo 2 - DB9 Universal PCI"
403#define PCI_DEVICE_NEO_2DB9PRI_PCI_NAME "Neo 2 - DB9 Universal PCI - Powered Ring Indicator"
404#define PCI_DEVICE_NEO_2RJ45_PCI_NAME "Neo 2 - RJ45 Universal PCI"
405#define PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME "Neo 2 - RJ45 Universal PCI - Powered Ring Indicator"
406
407/*
408 * Our Global Variables.
409 */
410extern struct uart_driver jsm_uart_driver;
411extern struct board_ops jsm_neo_ops;
412extern int jsm_debug;
413extern int jsm_rawreadok;
414
415extern int jsm_driver_state; /* The state of the driver */
416extern char *jsm_driver_state_text[];/* Array of driver state text */
417
418extern spinlock_t jsm_board_head_lock;
419extern struct list_head jsm_board_head;
420
421/*************************************************************************
422 *
423 * Prototypes for non-static functions used in more than one module
424 *
425 *************************************************************************/
426int jsm_tty_write(struct uart_port *port);
427int jsm_tty_init(struct jsm_board *);
428int jsm_uart_port_init(struct jsm_board *);
429int jsm_remove_uart_port(struct jsm_board *);
430void jsm_input(struct jsm_channel *ch);
431void jsm_carrier(struct jsm_channel *ch);
432void jsm_check_queue_flow_control(struct jsm_channel *ch);
433
434void jsm_create_driver_sysfiles(struct device_driver *);
435void jsm_remove_driver_sysfiles(struct device_driver *);
436
437#endif
diff --git a/drivers/serial/jsm/jsm_driver.c b/drivers/serial/jsm/jsm_driver.c
new file mode 100644
index 000000000000..d4847d4f1473
--- /dev/null
+++ b/drivers/serial/jsm/jsm_driver.c
@@ -0,0 +1,404 @@
1/************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
3 *
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
23 * Wendy Xiong <wendyx@us.ltcfwd.linux.ibm.com>
24 *
25 ***********************************************************************/
26#include <linux/moduleparam.h>
27#include <linux/pci.h>
28
29#include "jsm.h"
30
31MODULE_AUTHOR("Digi International, http://www.digi.com");
32MODULE_DESCRIPTION("Driver for the Digi International Neo PCI based product line");
33MODULE_SUPPORTED_DEVICE("jsm");
34
35#define JSM_DRIVER_NAME "jsm"
36#define NR_PORTS 32
37#define JSM_MINOR_START 0
38
39struct uart_driver jsm_uart_driver = {
40 .owner = THIS_MODULE,
41 .driver_name = JSM_DRIVER_NAME,
42 .dev_name = "ttyn",
43 .major = 253,
44 .minor = JSM_MINOR_START,
45 .nr = NR_PORTS,
46 .cons = NULL,
47};
48
49int jsm_debug;
50int jsm_rawreadok;
51module_param(jsm_debug, int, 0);
52module_param(jsm_rawreadok, int, 0);
53MODULE_PARM_DESC(jsm_debug, "Driver debugging level");
54MODULE_PARM_DESC(jsm_rawreadok, "Bypass flip buffers on input");
55
56/*
57 * Globals
58 */
59int jsm_driver_state = DRIVER_INITIALIZED;
60spinlock_t jsm_board_head_lock = SPIN_LOCK_UNLOCKED;
61LIST_HEAD(jsm_board_head);
62
63static struct pci_device_id jsm_pci_tbl[] = {
64 { PCI_DEVICE (PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
65 { PCI_DEVICE (PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
66 { PCI_DEVICE (PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
67 { PCI_DEVICE (PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
68 { 0,} /* 0 terminated list. */
69};
70MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
71
72static struct board_id jsm_Ids[] = {
73 { PCI_DEVICE_NEO_2DB9_PCI_NAME, 2 },
74 { PCI_DEVICE_NEO_2DB9PRI_PCI_NAME, 2 },
75 { PCI_DEVICE_NEO_2RJ45_PCI_NAME, 2 },
76 { PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME, 2 },
77 { NULL, 0 }
78};
79
80char *jsm_driver_state_text[] = {
81 "Driver Initialized",
82 "Driver Ready."
83};
84
85static int jsm_finalize_board_init(struct jsm_board *brd)
86{
87 int rc = 0;
88
89 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
90
91 if (brd->irq) {
92 rc = request_irq(brd->irq, brd->bd_ops->intr, SA_INTERRUPT|SA_SHIRQ, "JSM", brd);
93
94 if (rc) {
95 printk(KERN_WARNING "Failed to hook IRQ %d\n",brd->irq);
96 brd->state = BOARD_FAILED;
97 brd->dpastatus = BD_NOFEP;
98 rc = -ENODEV;
99 } else
100 jsm_printk(INIT, INFO, &brd->pci_dev,
101 "Requested and received usage of IRQ %d\n", brd->irq);
102 }
103 return rc;
104}
105
106/*
107 * jsm_found_board()
108 *
109 * A board has been found, init it.
110 */
111static int jsm_found_board(struct pci_dev *pdev, int id)
112{
113 struct jsm_board *brd;
114 int i = 0;
115 int rc = 0;
116 struct list_head *tmp;
117 struct jsm_board *cur_board_entry;
118 unsigned long lock_flags;
119 int adapter_count = 0;
120 int retval;
121
122 brd = kmalloc(sizeof(struct jsm_board), GFP_KERNEL);
123 if (!brd) {
124 dev_err(&pdev->dev, "memory allocation for board structure failed\n");
125 return -ENOMEM;
126 }
127 memset(brd, 0, sizeof(struct jsm_board));
128
129 spin_lock_irqsave(&jsm_board_head_lock, lock_flags);
130 list_for_each(tmp, &jsm_board_head) {
131 cur_board_entry =
132 list_entry(tmp, struct jsm_board,
133 jsm_board_entry);
134 if (cur_board_entry->boardnum != adapter_count) {
135 break;
136 }
137 adapter_count++;
138 }
139
140 list_add_tail(&brd->jsm_board_entry, &jsm_board_head);
141 spin_unlock_irqrestore(&jsm_board_head_lock, lock_flags);
142
143 /* store the info for the board we've found */
144 brd->boardnum = adapter_count;
145 brd->pci_dev = pdev;
146 brd->name = jsm_Ids[id].name;
147 brd->maxports = jsm_Ids[id].maxports;
148 brd->dpastatus = BD_NOFEP;
149 init_waitqueue_head(&brd->state_wait);
150
151 spin_lock_init(&brd->bd_lock);
152 spin_lock_init(&brd->bd_intr_lock);
153
154 brd->state = BOARD_FOUND;
155
156 for (i = 0; i < brd->maxports; i++)
157 brd->channels[i] = NULL;
158
159 /* store which revision we have */
160 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
161
162 brd->irq = pdev->irq;
163
164 switch(brd->pci_dev->device) {
165
166 case PCI_DEVICE_ID_NEO_2DB9:
167 case PCI_DEVICE_ID_NEO_2DB9PRI:
168 case PCI_DEVICE_ID_NEO_2RJ45:
169 case PCI_DEVICE_ID_NEO_2RJ45PRI:
170
171 /*
172 * This chip is set up 100% when we get to it.
173 * No need to enable global interrupts or anything.
174 */
175 brd->dpatype = T_NEO | T_PCIBUS;
176
177 jsm_printk(INIT, INFO, &brd->pci_dev,
178 "jsm_found_board - NEO adapter\n");
179
180 /* get the PCI Base Address Registers */
181 brd->membase = pci_resource_start(pdev, 0);
182 brd->membase_end = pci_resource_end(pdev, 0);
183
184 if (brd->membase & 1)
185 brd->membase &= ~3;
186 else
187 brd->membase &= ~15;
188
189 /* Assign the board_ops struct */
190 brd->bd_ops = &jsm_neo_ops;
191
192 brd->bd_uart_offset = 0x200;
193 brd->bd_dividend = 921600;
194
195 brd->re_map_membase = ioremap(brd->membase, 0x1000);
196 jsm_printk(INIT, INFO, &brd->pci_dev,
197 "remapped mem: 0x%p\n", brd->re_map_membase);
198 if (!brd->re_map_membase) {
199 kfree(brd);
200 dev_err(&pdev->dev, "card has no PCI Memory resources, failing board.\n");
201 return -ENOMEM;
202 }
203 break;
204
205 default:
206 dev_err(&pdev->dev, "Did not find any compatible Neo or Classic PCI boards in system.\n");
207 kfree(brd);
208 return -ENXIO;
209 }
210
211 /*
212 * Do tty device initialization.
213 */
214 rc = jsm_finalize_board_init(brd);
215 if (rc < 0) {
216 dev_err(&pdev->dev, "Can't finalize board init (%d)\n", rc);
217 brd->state = BOARD_FAILED;
218 retval = -ENXIO;
219 goto failed0;
220 }
221
222 rc = jsm_tty_init(brd);
223 if (rc < 0) {
224 dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
225 brd->state = BOARD_FAILED;
226 retval = -ENXIO;
227 goto failed1;
228 }
229
230 rc = jsm_uart_port_init(brd);
231 if (rc < 0) {
232 dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
233 brd->state = BOARD_FAILED;
234 retval = -ENXIO;
235 goto failed1;
236 }
237
238 brd->state = BOARD_READY;
239 brd->dpastatus = BD_RUNNING;
240
241 /* Log the information about the board */
242 dev_info(&pdev->dev, "board %d: %s (rev %d), irq %d\n",adapter_count, brd->name, brd->rev, brd->irq);
243
244 /*
245 * allocate flip buffer for board.
246 *
247 * Okay to malloc with GFP_KERNEL, we are not at interrupt
248 * context, and there are no locks held.
249 */
250 brd->flipbuf = kmalloc(MYFLIPLEN, GFP_KERNEL);
251 if (!brd->flipbuf) {
252 dev_err(&pdev->dev, "memory allocation for flipbuf failed\n");
253 brd->state = BOARD_FAILED;
254 retval = -ENOMEM;
255 goto failed1;
256 }
257 memset(brd->flipbuf, 0, MYFLIPLEN);
258
259 jsm_create_driver_sysfiles(pdev->dev.driver);
260
261 wake_up_interruptible(&brd->state_wait);
262 return 0;
263failed1:
264 free_irq(brd->irq, brd);
265failed0:
266 kfree(brd);
267 iounmap(brd->re_map_membase);
268 return retval;
269}
270
271/* returns count (>= 0), or negative on error */
272static int jsm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
273{
274 int rc;
275
276 rc = pci_enable_device(pdev);
277 if (rc) {
278 dev_err(&pdev->dev, "Device enable FAILED\n");
279 return rc;
280 }
281
282 if ((rc = pci_request_regions(pdev, "jsm"))) {
283 dev_err(&pdev->dev, "pci_request_region FAILED\n");
284 pci_disable_device(pdev);
285 return rc;
286 }
287
288 if ((rc = jsm_found_board(pdev, ent->driver_data))) {
289 dev_err(&pdev->dev, "jsm_found_board FAILED\n");
290 pci_release_regions(pdev);
291 pci_disable_device(pdev);
292 return rc;
293 }
294 return rc;
295}
296
297
298/*
299 * jsm_cleanup_board()
300 *
301 * Free all the memory associated with a board
302 */
303static void jsm_cleanup_board(struct jsm_board *brd)
304{
305 int i = 0;
306
307 free_irq(brd->irq, brd);
308 iounmap(brd->re_map_membase);
309
310 /* Free all allocated channels structs */
311 for (i = 0; i < brd->maxports; i++) {
312 if (brd->channels[i]) {
313 if (brd->channels[i]->ch_rqueue)
314 kfree(brd->channels[i]->ch_rqueue);
315 if (brd->channels[i]->ch_equeue)
316 kfree(brd->channels[i]->ch_equeue);
317 if (brd->channels[i]->ch_wqueue)
318 kfree(brd->channels[i]->ch_wqueue);
319 kfree(brd->channels[i]);
320 }
321 }
322
323 pci_release_regions(brd->pci_dev);
324 pci_disable_device(brd->pci_dev);
325 kfree(brd->flipbuf);
326 kfree(brd);
327}
328
329static void jsm_remove_one(struct pci_dev *dev)
330{
331 unsigned long lock_flags;
332 struct list_head *tmp;
333 struct jsm_board *brd;
334
335 spin_lock_irqsave(&jsm_board_head_lock, lock_flags);
336 list_for_each(tmp, &jsm_board_head) {
337 brd = list_entry(tmp, struct jsm_board,
338 jsm_board_entry);
339 if ( brd != NULL && brd->pci_dev == dev) {
340 jsm_remove_uart_port(brd);
341 jsm_cleanup_board(brd);
342 list_del(&brd->jsm_board_entry);
343 break;
344 }
345 }
346 spin_unlock_irqrestore(&jsm_board_head_lock, lock_flags);
347 return;
348}
349
350struct pci_driver jsm_driver = {
351 .name = "jsm",
352 .probe = jsm_init_one,
353 .id_table = jsm_pci_tbl,
354 .remove = __devexit_p(jsm_remove_one),
355};
356
357/*
358 * jsm_init_module()
359 *
360 * Module load. This is where it all starts.
361 */
362static int __init jsm_init_module(void)
363{
364 int rc = 0;
365
366 printk(KERN_INFO "%s, Digi International Part Number %s\n",
367 JSM_VERSION, JSM_VERSION);
368
369 /*
370 * Initialize global stuff
371 */
372
373 rc = uart_register_driver(&jsm_uart_driver);
374 if (rc < 0) {
375 return rc;
376 }
377
378 rc = pci_register_driver(&jsm_driver);
379 if (rc < 0) {
380 uart_unregister_driver(&jsm_uart_driver);
381 return rc;
382 }
383 jsm_driver_state = DRIVER_READY;
384
385 return rc;
386}
387
388module_init(jsm_init_module);
389
390/*
391 * jsm_exit_module()
392 *
393 * Module unload. This is where it all ends.
394 */
395static void __exit jsm_exit_module(void)
396{
397 jsm_remove_driver_sysfiles(&jsm_driver.driver);
398
399 pci_unregister_driver(&jsm_driver);
400
401 uart_unregister_driver(&jsm_uart_driver);
402}
403module_exit(jsm_exit_module);
404MODULE_LICENSE("GPL");
diff --git a/drivers/serial/jsm/jsm_neo.c b/drivers/serial/jsm/jsm_neo.c
new file mode 100644
index 000000000000..9b79c1ff6c72
--- /dev/null
+++ b/drivers/serial/jsm/jsm_neo.c
@@ -0,0 +1,1427 @@
1/************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
3 *
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
23 * Wendy Xiong <wendyx@us.ltcfwd.linux.ibm.com>
24 *
25 ***********************************************************************/
26#include <linux/delay.h> /* For udelay */
27#include <linux/serial_reg.h> /* For the various UART offsets */
28#include <linux/tty.h>
29#include <linux/pci.h>
30#include <asm/io.h>
31
32#include "jsm.h" /* Driver main header file */
33
34static u32 jsm_offset_table[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
35
36/*
37 * This function allows calls to ensure that all outstanding
38 * PCI writes have been completed, by doing a PCI read against
39 * a non-destructive, read-only location on the Neo card.
40 *
41 * In this case, we are reading the DVID (Read-only Device Identification)
42 * value of the Neo card.
43 */
44static inline void neo_pci_posting_flush(struct jsm_board *bd)
45{
46 readb(bd->re_map_membase + 0x8D);
47}
48
49static void neo_set_cts_flow_control(struct jsm_channel *ch)
50{
51 u8 ier = readb(&ch->ch_neo_uart->ier);
52 u8 efr = readb(&ch->ch_neo_uart->efr);
53
54 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting CTSFLOW\n");
55
56 /* Turn on auto CTS flow control */
57 ier |= (UART_17158_IER_CTSDSR);
58 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_CTSDSR);
59
60 /* Turn off auto Xon flow control */
61 efr &= ~(UART_17158_EFR_IXON);
62
63 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
64 writeb(0, &ch->ch_neo_uart->efr);
65
66 /* Turn on UART enhanced bits */
67 writeb(efr, &ch->ch_neo_uart->efr);
68
69 /* Turn on table D, with 8 char hi/low watermarks */
70 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY), &ch->ch_neo_uart->fctr);
71
72 /* Feed the UART our trigger levels */
73 writeb(8, &ch->ch_neo_uart->tfifo);
74 ch->ch_t_tlevel = 8;
75
76 writeb(ier, &ch->ch_neo_uart->ier);
77}
78
79static void neo_set_rts_flow_control(struct jsm_channel *ch)
80{
81 u8 ier = readb(&ch->ch_neo_uart->ier);
82 u8 efr = readb(&ch->ch_neo_uart->efr);
83
84 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting RTSFLOW\n");
85
86 /* Turn on auto RTS flow control */
87 ier |= (UART_17158_IER_RTSDTR);
88 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_RTSDTR);
89
90 /* Turn off auto Xoff flow control */
91 ier &= ~(UART_17158_IER_XOFF);
92 efr &= ~(UART_17158_EFR_IXOFF);
93
94 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
95 writeb(0, &ch->ch_neo_uart->efr);
96
97 /* Turn on UART enhanced bits */
98 writeb(efr, &ch->ch_neo_uart->efr);
99
100 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY), &ch->ch_neo_uart->fctr);
101 ch->ch_r_watermark = 4;
102
103 writeb(56, &ch->ch_neo_uart->rfifo);
104 ch->ch_r_tlevel = 56;
105
106 writeb(ier, &ch->ch_neo_uart->ier);
107
108 /*
109 * From the Neo UART spec sheet:
110 * The auto RTS/DTR function must be started by asserting
111 * RTS/DTR# output pin (MCR bit-0 or 1 to logic 1 after
112 * it is enabled.
113 */
114 ch->ch_mostat |= (UART_MCR_RTS);
115}
116
117
118static void neo_set_ixon_flow_control(struct jsm_channel *ch)
119{
120 u8 ier = readb(&ch->ch_neo_uart->ier);
121 u8 efr = readb(&ch->ch_neo_uart->efr);
122
123 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting IXON FLOW\n");
124
125 /* Turn off auto CTS flow control */
126 ier &= ~(UART_17158_IER_CTSDSR);
127 efr &= ~(UART_17158_EFR_CTSDSR);
128
129 /* Turn on auto Xon flow control */
130 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXON);
131
132 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
133 writeb(0, &ch->ch_neo_uart->efr);
134
135 /* Turn on UART enhanced bits */
136 writeb(efr, &ch->ch_neo_uart->efr);
137
138 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
139 ch->ch_r_watermark = 4;
140
141 writeb(32, &ch->ch_neo_uart->rfifo);
142 ch->ch_r_tlevel = 32;
143
144 /* Tell UART what start/stop chars it should be looking for */
145 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
146 writeb(0, &ch->ch_neo_uart->xonchar2);
147
148 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
149 writeb(0, &ch->ch_neo_uart->xoffchar2);
150
151 writeb(ier, &ch->ch_neo_uart->ier);
152}
153
154static void neo_set_ixoff_flow_control(struct jsm_channel *ch)
155{
156 u8 ier = readb(&ch->ch_neo_uart->ier);
157 u8 efr = readb(&ch->ch_neo_uart->efr);
158
159 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting IXOFF FLOW\n");
160
161 /* Turn off auto RTS flow control */
162 ier &= ~(UART_17158_IER_RTSDTR);
163 efr &= ~(UART_17158_EFR_RTSDTR);
164
165 /* Turn on auto Xoff flow control */
166 ier |= (UART_17158_IER_XOFF);
167 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
168
169 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
170 writeb(0, &ch->ch_neo_uart->efr);
171
172 /* Turn on UART enhanced bits */
173 writeb(efr, &ch->ch_neo_uart->efr);
174
175 /* Turn on table D, with 8 char hi/low watermarks */
176 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
177
178 writeb(8, &ch->ch_neo_uart->tfifo);
179 ch->ch_t_tlevel = 8;
180
181 /* Tell UART what start/stop chars it should be looking for */
182 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
183 writeb(0, &ch->ch_neo_uart->xonchar2);
184
185 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
186 writeb(0, &ch->ch_neo_uart->xoffchar2);
187
188 writeb(ier, &ch->ch_neo_uart->ier);
189}
190
191static void neo_set_no_input_flow_control(struct jsm_channel *ch)
192{
193 u8 ier = readb(&ch->ch_neo_uart->ier);
194 u8 efr = readb(&ch->ch_neo_uart->efr);
195
196 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Unsetting Input FLOW\n");
197
198 /* Turn off auto RTS flow control */
199 ier &= ~(UART_17158_IER_RTSDTR);
200 efr &= ~(UART_17158_EFR_RTSDTR);
201
202 /* Turn off auto Xoff flow control */
203 ier &= ~(UART_17158_IER_XOFF);
204 if (ch->ch_c_iflag & IXON)
205 efr &= ~(UART_17158_EFR_IXOFF);
206 else
207 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
208
209 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
210 writeb(0, &ch->ch_neo_uart->efr);
211
212 /* Turn on UART enhanced bits */
213 writeb(efr, &ch->ch_neo_uart->efr);
214
215 /* Turn on table D, with 8 char hi/low watermarks */
216 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
217
218 ch->ch_r_watermark = 0;
219
220 writeb(16, &ch->ch_neo_uart->tfifo);
221 ch->ch_t_tlevel = 16;
222
223 writeb(16, &ch->ch_neo_uart->rfifo);
224 ch->ch_r_tlevel = 16;
225
226 writeb(ier, &ch->ch_neo_uart->ier);
227}
228
229static void neo_set_no_output_flow_control(struct jsm_channel *ch)
230{
231 u8 ier = readb(&ch->ch_neo_uart->ier);
232 u8 efr = readb(&ch->ch_neo_uart->efr);
233
234 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Unsetting Output FLOW\n");
235
236 /* Turn off auto CTS flow control */
237 ier &= ~(UART_17158_IER_CTSDSR);
238 efr &= ~(UART_17158_EFR_CTSDSR);
239
240 /* Turn off auto Xon flow control */
241 if (ch->ch_c_iflag & IXOFF)
242 efr &= ~(UART_17158_EFR_IXON);
243 else
244 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXON);
245
246 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
247 writeb(0, &ch->ch_neo_uart->efr);
248
249 /* Turn on UART enhanced bits */
250 writeb(efr, &ch->ch_neo_uart->efr);
251
252 /* Turn on table D, with 8 char hi/low watermarks */
253 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
254
255 ch->ch_r_watermark = 0;
256
257 writeb(16, &ch->ch_neo_uart->tfifo);
258 ch->ch_t_tlevel = 16;
259
260 writeb(16, &ch->ch_neo_uart->rfifo);
261 ch->ch_r_tlevel = 16;
262
263 writeb(ier, &ch->ch_neo_uart->ier);
264}
265
266static inline void neo_set_new_start_stop_chars(struct jsm_channel *ch)
267{
268
269 /* if hardware flow control is set, then skip this whole thing */
270 if (ch->ch_c_cflag & CRTSCTS)
271 return;
272
273 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "start\n");
274
275 /* Tell UART what start/stop chars it should be looking for */
276 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
277 writeb(0, &ch->ch_neo_uart->xonchar2);
278
279 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
280 writeb(0, &ch->ch_neo_uart->xoffchar2);
281}
282
283static void neo_copy_data_from_uart_to_queue(struct jsm_channel *ch)
284{
285 int qleft = 0;
286 u8 linestatus = 0;
287 u8 error_mask = 0;
288 int n = 0;
289 int total = 0;
290 u16 head;
291 u16 tail;
292
293 if (!ch)
294 return;
295
296 /* cache head and tail of queue */
297 head = ch->ch_r_head & RQUEUEMASK;
298 tail = ch->ch_r_tail & RQUEUEMASK;
299
300 /* Get our cached LSR */
301 linestatus = ch->ch_cached_lsr;
302 ch->ch_cached_lsr = 0;
303
304 /* Store how much space we have left in the queue */
305 if ((qleft = tail - head - 1) < 0)
306 qleft += RQUEUEMASK + 1;
307
308 /*
309 * If the UART is not in FIFO mode, force the FIFO copy to
310 * NOT be run, by setting total to 0.
311 *
312 * On the other hand, if the UART IS in FIFO mode, then ask
313 * the UART to give us an approximation of data it has RX'ed.
314 */
315 if (!(ch->ch_flags & CH_FIFO_ENABLED))
316 total = 0;
317 else {
318 total = readb(&ch->ch_neo_uart->rfifo);
319
320 /*
321 * EXAR chip bug - RX FIFO COUNT - Fudge factor.
322 *
323 * This resolves a problem/bug with the Exar chip that sometimes
324 * returns a bogus value in the rfifo register.
325 * The count can be any where from 0-3 bytes "off".
326 * Bizarre, but true.
327 */
328 total -= 3;
329 }
330
331 /*
332 * Finally, bound the copy to make sure we don't overflow
333 * our own queue...
334 * The byte by byte copy loop below this loop this will
335 * deal with the queue overflow possibility.
336 */
337 total = min(total, qleft);
338
339 while (total > 0) {
340 /*
341 * Grab the linestatus register, we need to check
342 * to see if there are any errors in the FIFO.
343 */
344 linestatus = readb(&ch->ch_neo_uart->lsr);
345
346 /*
347 * Break out if there is a FIFO error somewhere.
348 * This will allow us to go byte by byte down below,
349 * finding the exact location of the error.
350 */
351 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
352 break;
353
354 /* Make sure we don't go over the end of our queue */
355 n = min(((u32) total), (RQUEUESIZE - (u32) head));
356
357 /*
358 * Cut down n even further if needed, this is to fix
359 * a problem with memcpy_fromio() with the Neo on the
360 * IBM pSeries platform.
361 * 15 bytes max appears to be the magic number.
362 */
363 n = min((u32) n, (u32) 12);
364
365 /*
366 * Since we are grabbing the linestatus register, which
367 * will reset some bits after our read, we need to ensure
368 * we don't miss our TX FIFO emptys.
369 */
370 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR))
371 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
372
373 linestatus = 0;
374
375 /* Copy data from uart to the queue */
376 memcpy_fromio(ch->ch_rqueue + head, &ch->ch_neo_uart->txrxburst, n);
377 /*
378 * Since RX_FIFO_DATA_ERROR was 0, we are guarenteed
379 * that all the data currently in the FIFO is free of
380 * breaks and parity/frame/orun errors.
381 */
382 memset(ch->ch_equeue + head, 0, n);
383
384 /* Add to and flip head if needed */
385 head = (head + n) & RQUEUEMASK;
386 total -= n;
387 qleft -= n;
388 ch->ch_rxcount += n;
389 }
390
391 /*
392 * Create a mask to determine whether we should
393 * insert the character (if any) into our queue.
394 */
395 if (ch->ch_c_iflag & IGNBRK)
396 error_mask |= UART_LSR_BI;
397
398 /*
399 * Now cleanup any leftover bytes still in the UART.
400 * Also deal with any possible queue overflow here as well.
401 */
402 while (1) {
403
404 /*
405 * Its possible we have a linestatus from the loop above
406 * this, so we "OR" on any extra bits.
407 */
408 linestatus |= readb(&ch->ch_neo_uart->lsr);
409
410 /*
411 * If the chip tells us there is no more data pending to
412 * be read, we can then leave.
413 * But before we do, cache the linestatus, just in case.
414 */
415 if (!(linestatus & UART_LSR_DR)) {
416 ch->ch_cached_lsr = linestatus;
417 break;
418 }
419
420 /* No need to store this bit */
421 linestatus &= ~UART_LSR_DR;
422
423 /*
424 * Since we are grabbing the linestatus register, which
425 * will reset some bits after our read, we need to ensure
426 * we don't miss our TX FIFO emptys.
427 */
428 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR)) {
429 linestatus &= ~(UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR);
430 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
431 }
432
433 /*
434 * Discard character if we are ignoring the error mask.
435 */
436 if (linestatus & error_mask) {
437 u8 discard;
438 linestatus = 0;
439 memcpy_fromio(&discard, &ch->ch_neo_uart->txrxburst, 1);
440 continue;
441 }
442
443 /*
444 * If our queue is full, we have no choice but to drop some data.
445 * The assumption is that HWFLOW or SWFLOW should have stopped
446 * things way way before we got to this point.
447 *
448 * I decided that I wanted to ditch the oldest data first,
449 * I hope thats okay with everyone? Yes? Good.
450 */
451 while (qleft < 1) {
452 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
453 "Queue full, dropping DATA:%x LSR:%x\n",
454 ch->ch_rqueue[tail], ch->ch_equeue[tail]);
455
456 ch->ch_r_tail = tail = (tail + 1) & RQUEUEMASK;
457 ch->ch_err_overrun++;
458 qleft++;
459 }
460
461 memcpy_fromio(ch->ch_rqueue + head, &ch->ch_neo_uart->txrxburst, 1);
462 ch->ch_equeue[head] = (u8) linestatus;
463
464 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
465 "DATA/LSR pair: %x %x\n", ch->ch_rqueue[head], ch->ch_equeue[head]);
466
467 /* Ditch any remaining linestatus value. */
468 linestatus = 0;
469
470 /* Add to and flip head if needed */
471 head = (head + 1) & RQUEUEMASK;
472
473 qleft--;
474 ch->ch_rxcount++;
475 }
476
477 /*
478 * Write new final heads to channel structure.
479 */
480 ch->ch_r_head = head & RQUEUEMASK;
481 ch->ch_e_head = head & EQUEUEMASK;
482 jsm_input(ch);
483}
484
485static void neo_copy_data_from_queue_to_uart(struct jsm_channel *ch)
486{
487 u16 head;
488 u16 tail;
489 int n;
490 int s;
491 int qlen;
492 u32 len_written = 0;
493
494 if (!ch)
495 return;
496
497 /* No data to write to the UART */
498 if (ch->ch_w_tail == ch->ch_w_head)
499 return;
500
501 /* If port is "stopped", don't send any data to the UART */
502 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_BREAK_SENDING))
503 return;
504 /*
505 * If FIFOs are disabled. Send data directly to txrx register
506 */
507 if (!(ch->ch_flags & CH_FIFO_ENABLED)) {
508 u8 lsrbits = readb(&ch->ch_neo_uart->lsr);
509
510 ch->ch_cached_lsr |= lsrbits;
511 if (ch->ch_cached_lsr & UART_LSR_THRE) {
512 ch->ch_cached_lsr &= ~(UART_LSR_THRE);
513
514 writeb(ch->ch_wqueue[ch->ch_w_tail], &ch->ch_neo_uart->txrx);
515 jsm_printk(WRITE, INFO, &ch->ch_bd->pci_dev,
516 "Tx data: %x\n", ch->ch_wqueue[ch->ch_w_head]);
517 ch->ch_w_tail++;
518 ch->ch_w_tail &= WQUEUEMASK;
519 ch->ch_txcount++;
520 }
521 return;
522 }
523
524 /*
525 * We have to do it this way, because of the EXAR TXFIFO count bug.
526 */
527 if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM)))
528 return;
529
530 len_written = 0;
531 n = UART_17158_TX_FIFOSIZE - ch->ch_t_tlevel;
532
533 /* cache head and tail of queue */
534 head = ch->ch_w_head & WQUEUEMASK;
535 tail = ch->ch_w_tail & WQUEUEMASK;
536 qlen = (head - tail) & WQUEUEMASK;
537
538 /* Find minimum of the FIFO space, versus queue length */
539 n = min(n, qlen);
540
541 while (n > 0) {
542
543 s = ((head >= tail) ? head : WQUEUESIZE) - tail;
544 s = min(s, n);
545
546 if (s <= 0)
547 break;
548
549 memcpy_toio(&ch->ch_neo_uart->txrxburst, ch->ch_wqueue + tail, s);
550 /* Add and flip queue if needed */
551 tail = (tail + s) & WQUEUEMASK;
552 n -= s;
553 ch->ch_txcount += s;
554 len_written += s;
555 }
556
557 /* Update the final tail */
558 ch->ch_w_tail = tail & WQUEUEMASK;
559
560 if (len_written >= ch->ch_t_tlevel)
561 ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
562
563 if (!jsm_tty_write(&ch->uart_port))
564 uart_write_wakeup(&ch->uart_port);
565}
566
567static void neo_parse_modem(struct jsm_channel *ch, u8 signals)
568{
569 u8 msignals = signals;
570
571 jsm_printk(MSIGS, INFO, &ch->ch_bd->pci_dev,
572 "neo_parse_modem: port: %d msignals: %x\n", ch->ch_portnum, msignals);
573
574 if (!ch)
575 return;
576
577 /* Scrub off lower bits. They signify delta's, which I don't care about */
578 msignals &= 0xf0;
579
580 if (msignals & UART_MSR_DCD)
581 ch->ch_mistat |= UART_MSR_DCD;
582 else
583 ch->ch_mistat &= ~UART_MSR_DCD;
584
585 if (msignals & UART_MSR_DSR)
586 ch->ch_mistat |= UART_MSR_DSR;
587 else
588 ch->ch_mistat &= ~UART_MSR_DSR;
589
590 if (msignals & UART_MSR_RI)
591 ch->ch_mistat |= UART_MSR_RI;
592 else
593 ch->ch_mistat &= ~UART_MSR_RI;
594
595 if (msignals & UART_MSR_CTS)
596 ch->ch_mistat |= UART_MSR_CTS;
597 else
598 ch->ch_mistat &= ~UART_MSR_CTS;
599
600 jsm_printk(MSIGS, INFO, &ch->ch_bd->pci_dev,
601 "Port: %d DTR: %d RTS: %d CTS: %d DSR: %d " "RI: %d CD: %d\n",
602 ch->ch_portnum,
603 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_DTR),
604 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_RTS),
605 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_CTS),
606 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DSR),
607 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_RI),
608 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DCD));
609}
610
611/* Make the UART raise any of the output signals we want up */
612static void neo_assert_modem_signals(struct jsm_channel *ch)
613{
614 u8 out;
615
616 if (!ch)
617 return;
618
619 out = ch->ch_mostat;
620
621 writeb(out, &ch->ch_neo_uart->mcr);
622
623 /* flush write operation */
624 neo_pci_posting_flush(ch->ch_bd);
625}
626
627/*
628 * Flush the WRITE FIFO on the Neo.
629 *
630 * NOTE: Channel lock MUST be held before calling this function!
631 */
632static void neo_flush_uart_write(struct jsm_channel *ch)
633{
634 u8 tmp = 0;
635 int i = 0;
636
637 if (!ch)
638 return;
639
640 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT), &ch->ch_neo_uart->isr_fcr);
641
642 for (i = 0; i < 10; i++) {
643
644 /* Check to see if the UART feels it completely flushed the FIFO. */
645 tmp = readb(&ch->ch_neo_uart->isr_fcr);
646 if (tmp & 4) {
647 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
648 "Still flushing TX UART... i: %d\n", i);
649 udelay(10);
650 }
651 else
652 break;
653 }
654
655 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
656}
657
658
659/*
660 * Flush the READ FIFO on the Neo.
661 *
662 * NOTE: Channel lock MUST be held before calling this function!
663 */
664static void neo_flush_uart_read(struct jsm_channel *ch)
665{
666 u8 tmp = 0;
667 int i = 0;
668
669 if (!ch)
670 return;
671
672 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR), &ch->ch_neo_uart->isr_fcr);
673
674 for (i = 0; i < 10; i++) {
675
676 /* Check to see if the UART feels it completely flushed the FIFO. */
677 tmp = readb(&ch->ch_neo_uart->isr_fcr);
678 if (tmp & 2) {
679 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
680 "Still flushing RX UART... i: %d\n", i);
681 udelay(10);
682 }
683 else
684 break;
685 }
686}
687
688/*
689 * No locks are assumed to be held when calling this function.
690 */
691void neo_clear_break(struct jsm_channel *ch, int force)
692{
693 unsigned long lock_flags;
694
695 spin_lock_irqsave(&ch->ch_lock, lock_flags);
696
697 /* Turn break off, and unset some variables */
698 if (ch->ch_flags & CH_BREAK_SENDING) {
699 u8 temp = readb(&ch->ch_neo_uart->lcr);
700 writeb((temp & ~UART_LCR_SBC), &ch->ch_neo_uart->lcr);
701
702 ch->ch_flags &= ~(CH_BREAK_SENDING);
703 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
704 "clear break Finishing UART_LCR_SBC! finished: %lx\n", jiffies);
705
706 /* flush write operation */
707 neo_pci_posting_flush(ch->ch_bd);
708 }
709 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
710}
711
712/*
713 * Parse the ISR register.
714 */
715static inline void neo_parse_isr(struct jsm_board *brd, u32 port)
716{
717 struct jsm_channel *ch;
718 u8 isr;
719 u8 cause;
720 unsigned long lock_flags;
721
722 if (!brd)
723 return;
724
725 if (port > brd->maxports)
726 return;
727
728 ch = brd->channels[port];
729 if (!ch)
730 return;
731
732 /* Here we try to figure out what caused the interrupt to happen */
733 while (1) {
734
735 isr = readb(&ch->ch_neo_uart->isr_fcr);
736
737 /* Bail if no pending interrupt */
738 if (isr & UART_IIR_NO_INT)
739 break;
740
741 /*
742 * Yank off the upper 2 bits, which just show that the FIFO's are enabled.
743 */
744 isr &= ~(UART_17158_IIR_FIFO_ENABLED);
745
746 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
747 "%s:%d isr: %x\n", __FILE__, __LINE__, isr);
748
749 if (isr & (UART_17158_IIR_RDI_TIMEOUT | UART_IIR_RDI)) {
750 /* Read data from uart -> queue */
751 neo_copy_data_from_uart_to_queue(ch);
752
753 /* Call our tty layer to enforce queue flow control if needed. */
754 spin_lock_irqsave(&ch->ch_lock, lock_flags);
755 jsm_check_queue_flow_control(ch);
756 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
757 }
758
759 if (isr & UART_IIR_THRI) {
760 /* Transfer data (if any) from Write Queue -> UART. */
761 spin_lock_irqsave(&ch->ch_lock, lock_flags);
762 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
763 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
764 neo_copy_data_from_queue_to_uart(ch);
765 }
766
767 if (isr & UART_17158_IIR_XONXOFF) {
768 cause = readb(&ch->ch_neo_uart->xoffchar1);
769
770 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
771 "Port %d. Got ISR_XONXOFF: cause:%x\n", port, cause);
772
773 /*
774 * Since the UART detected either an XON or
775 * XOFF match, we need to figure out which
776 * one it was, so we can suspend or resume data flow.
777 */
778 spin_lock_irqsave(&ch->ch_lock, lock_flags);
779 if (cause == UART_17158_XON_DETECT) {
780 /* Is output stopped right now, if so, resume it */
781 if (brd->channels[port]->ch_flags & CH_STOP) {
782 ch->ch_flags &= ~(CH_STOP);
783 }
784 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
785 "Port %d. XON detected in incoming data\n", port);
786 }
787 else if (cause == UART_17158_XOFF_DETECT) {
788 if (!(brd->channels[port]->ch_flags & CH_STOP)) {
789 ch->ch_flags |= CH_STOP;
790 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
791 "Setting CH_STOP\n");
792 }
793 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
794 "Port: %d. XOFF detected in incoming data\n", port);
795 }
796 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
797 }
798
799 if (isr & UART_17158_IIR_HWFLOW_STATE_CHANGE) {
800 /*
801 * If we get here, this means the hardware is doing auto flow control.
802 * Check to see whether RTS/DTR or CTS/DSR caused this interrupt.
803 */
804 cause = readb(&ch->ch_neo_uart->mcr);
805
806 /* Which pin is doing auto flow? RTS or DTR? */
807 spin_lock_irqsave(&ch->ch_lock, lock_flags);
808 if ((cause & 0x4) == 0) {
809 if (cause & UART_MCR_RTS)
810 ch->ch_mostat |= UART_MCR_RTS;
811 else
812 ch->ch_mostat &= ~(UART_MCR_RTS);
813 } else {
814 if (cause & UART_MCR_DTR)
815 ch->ch_mostat |= UART_MCR_DTR;
816 else
817 ch->ch_mostat &= ~(UART_MCR_DTR);
818 }
819 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
820 }
821
822 /* Parse any modem signal changes */
823 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
824 "MOD_STAT: sending to parse_modem_sigs\n");
825 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
826 }
827}
828
829static inline void neo_parse_lsr(struct jsm_board *brd, u32 port)
830{
831 struct jsm_channel *ch;
832 int linestatus;
833 unsigned long lock_flags;
834
835 if (!brd)
836 return;
837
838 if (port > brd->maxports)
839 return;
840
841 ch = brd->channels[port];
842 if (!ch)
843 return;
844
845 linestatus = readb(&ch->ch_neo_uart->lsr);
846
847 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
848 "%s:%d port: %d linestatus: %x\n", __FILE__, __LINE__, port, linestatus);
849
850 ch->ch_cached_lsr |= linestatus;
851
852 if (ch->ch_cached_lsr & UART_LSR_DR) {
853 /* Read data from uart -> queue */
854 neo_copy_data_from_uart_to_queue(ch);
855 spin_lock_irqsave(&ch->ch_lock, lock_flags);
856 jsm_check_queue_flow_control(ch);
857 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
858 }
859
860 /*
861 * This is a special flag. It indicates that at least 1
862 * RX error (parity, framing, or break) has happened.
863 * Mark this in our struct, which will tell me that I have
864 *to do the special RX+LSR read for this FIFO load.
865 */
866 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
867 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
868 "%s:%d Port: %d Got an RX error, need to parse LSR\n",
869 __FILE__, __LINE__, port);
870
871 /*
872 * The next 3 tests should *NOT* happen, as the above test
873 * should encapsulate all 3... At least, thats what Exar says.
874 */
875
876 if (linestatus & UART_LSR_PE) {
877 ch->ch_err_parity++;
878 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
879 "%s:%d Port: %d. PAR ERR!\n", __FILE__, __LINE__, port);
880 }
881
882 if (linestatus & UART_LSR_FE) {
883 ch->ch_err_frame++;
884 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
885 "%s:%d Port: %d. FRM ERR!\n", __FILE__, __LINE__, port);
886 }
887
888 if (linestatus & UART_LSR_BI) {
889 ch->ch_err_break++;
890 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
891 "%s:%d Port: %d. BRK INTR!\n", __FILE__, __LINE__, port);
892 }
893
894 if (linestatus & UART_LSR_OE) {
895 /*
896 * Rx Oruns. Exar says that an orun will NOT corrupt
897 * the FIFO. It will just replace the holding register
898 * with this new data byte. So basically just ignore this.
899 * Probably we should eventually have an orun stat in our driver...
900 */
901 ch->ch_err_overrun++;
902 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
903 "%s:%d Port: %d. Rx Overrun!\n", __FILE__, __LINE__, port);
904 }
905
906 if (linestatus & UART_LSR_THRE) {
907 spin_lock_irqsave(&ch->ch_lock, lock_flags);
908 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
909 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
910
911 /* Transfer data (if any) from Write Queue -> UART. */
912 neo_copy_data_from_queue_to_uart(ch);
913 }
914 else if (linestatus & UART_17158_TX_AND_FIFO_CLR) {
915 spin_lock_irqsave(&ch->ch_lock, lock_flags);
916 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
917 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
918
919 /* Transfer data (if any) from Write Queue -> UART. */
920 neo_copy_data_from_queue_to_uart(ch);
921 }
922}
923
924/*
925 * neo_param()
926 * Send any/all changes to the line to the UART.
927 */
928static void neo_param(struct jsm_channel *ch)
929{
930 u8 lcr = 0;
931 u8 uart_lcr = 0;
932 u8 ier = 0;
933 u32 baud = 9600;
934 int quot = 0;
935 struct jsm_board *bd;
936
937 bd = ch->ch_bd;
938 if (!bd)
939 return;
940
941 /*
942 * If baud rate is zero, flush queues, and set mval to drop DTR.
943 */
944 if ((ch->ch_c_cflag & (CBAUD)) == 0) {
945 ch->ch_r_head = ch->ch_r_tail = 0;
946 ch->ch_e_head = ch->ch_e_tail = 0;
947 ch->ch_w_head = ch->ch_w_tail = 0;
948
949 neo_flush_uart_write(ch);
950 neo_flush_uart_read(ch);
951
952 ch->ch_flags |= (CH_BAUD0);
953 ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
954 neo_assert_modem_signals(ch);
955 ch->ch_old_baud = 0;
956 return;
957
958 } else if (ch->ch_custom_speed) {
959 baud = ch->ch_custom_speed;
960 if (ch->ch_flags & CH_BAUD0)
961 ch->ch_flags &= ~(CH_BAUD0);
962 } else {
963 int iindex = 0;
964 int jindex = 0;
965
966 const u64 bauds[4][16] = {
967 {
968 0, 50, 75, 110,
969 134, 150, 200, 300,
970 600, 1200, 1800, 2400,
971 4800, 9600, 19200, 38400 },
972 {
973 0, 57600, 115200, 230400,
974 460800, 150, 200, 921600,
975 600, 1200, 1800, 2400,
976 4800, 9600, 19200, 38400 },
977 {
978 0, 57600, 76800, 115200,
979 131657, 153600, 230400, 460800,
980 921600, 1200, 1800, 2400,
981 4800, 9600, 19200, 38400 },
982 {
983 0, 57600, 115200, 230400,
984 460800, 150, 200, 921600,
985 600, 1200, 1800, 2400,
986 4800, 9600, 19200, 38400 }
987 };
988
989 baud = C_BAUD(ch->uart_port.info->tty) & 0xff;
990
991 if (ch->ch_c_cflag & CBAUDEX)
992 iindex = 1;
993
994 jindex = baud;
995
996 if ((iindex >= 0) && (iindex < 4) && (jindex >= 0) && (jindex < 16))
997 baud = bauds[iindex][jindex];
998 else {
999 jsm_printk(IOCTL, DEBUG, &ch->ch_bd->pci_dev,
1000 "baud indices were out of range (%d)(%d)",
1001 iindex, jindex);
1002 baud = 0;
1003 }
1004
1005 if (baud == 0)
1006 baud = 9600;
1007
1008 if (ch->ch_flags & CH_BAUD0)
1009 ch->ch_flags &= ~(CH_BAUD0);
1010 }
1011
1012 if (ch->ch_c_cflag & PARENB)
1013 lcr |= UART_LCR_PARITY;
1014
1015 if (!(ch->ch_c_cflag & PARODD))
1016 lcr |= UART_LCR_EPAR;
1017
1018 /*
1019 * Not all platforms support mark/space parity,
1020 * so this will hide behind an ifdef.
1021 */
1022#ifdef CMSPAR
1023 if (ch->ch_c_cflag & CMSPAR)
1024 lcr |= UART_LCR_SPAR;
1025#endif
1026
1027 if (ch->ch_c_cflag & CSTOPB)
1028 lcr |= UART_LCR_STOP;
1029
1030 switch (ch->ch_c_cflag & CSIZE) {
1031 case CS5:
1032 lcr |= UART_LCR_WLEN5;
1033 break;
1034 case CS6:
1035 lcr |= UART_LCR_WLEN6;
1036 break;
1037 case CS7:
1038 lcr |= UART_LCR_WLEN7;
1039 break;
1040 case CS8:
1041 default:
1042 lcr |= UART_LCR_WLEN8;
1043 break;
1044 }
1045
1046 ier = readb(&ch->ch_neo_uart->ier);
1047 uart_lcr = readb(&ch->ch_neo_uart->lcr);
1048
1049 if (baud == 0)
1050 baud = 9600;
1051
1052 quot = ch->ch_bd->bd_dividend / baud;
1053
1054 if (quot != 0) {
1055 ch->ch_old_baud = baud;
1056 writeb(UART_LCR_DLAB, &ch->ch_neo_uart->lcr);
1057 writeb((quot & 0xff), &ch->ch_neo_uart->txrx);
1058 writeb((quot >> 8), &ch->ch_neo_uart->ier);
1059 writeb(lcr, &ch->ch_neo_uart->lcr);
1060 }
1061
1062 if (uart_lcr != lcr)
1063 writeb(lcr, &ch->ch_neo_uart->lcr);
1064
1065 if (ch->ch_c_cflag & CREAD)
1066 ier |= (UART_IER_RDI | UART_IER_RLSI);
1067
1068 ier |= (UART_IER_THRI | UART_IER_MSI);
1069
1070 writeb(ier, &ch->ch_neo_uart->ier);
1071
1072 /* Set new start/stop chars */
1073 neo_set_new_start_stop_chars(ch);
1074
1075 if (ch->ch_c_cflag & CRTSCTS)
1076 neo_set_cts_flow_control(ch);
1077 else if (ch->ch_c_iflag & IXON) {
1078 /* If start/stop is set to disable, then we should disable flow control */
1079 if ((ch->ch_startc == __DISABLED_CHAR) || (ch->ch_stopc == __DISABLED_CHAR))
1080 neo_set_no_output_flow_control(ch);
1081 else
1082 neo_set_ixon_flow_control(ch);
1083 }
1084 else
1085 neo_set_no_output_flow_control(ch);
1086
1087 if (ch->ch_c_cflag & CRTSCTS)
1088 neo_set_rts_flow_control(ch);
1089 else if (ch->ch_c_iflag & IXOFF) {
1090 /* If start/stop is set to disable, then we should disable flow control */
1091 if ((ch->ch_startc == __DISABLED_CHAR) || (ch->ch_stopc == __DISABLED_CHAR))
1092 neo_set_no_input_flow_control(ch);
1093 else
1094 neo_set_ixoff_flow_control(ch);
1095 }
1096 else
1097 neo_set_no_input_flow_control(ch);
1098 /*
1099 * Adjust the RX FIFO Trigger level if baud is less than 9600.
1100 * Not exactly elegant, but this is needed because of the Exar chip's
1101 * delay on firing off the RX FIFO interrupt on slower baud rates.
1102 */
1103 if (baud < 9600) {
1104 writeb(1, &ch->ch_neo_uart->rfifo);
1105 ch->ch_r_tlevel = 1;
1106 }
1107
1108 neo_assert_modem_signals(ch);
1109
1110 /* Get current status of the modem signals now */
1111 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
1112 return;
1113}
1114
1115/*
1116 * jsm_neo_intr()
1117 *
1118 * Neo specific interrupt handler.
1119 */
1120static irqreturn_t neo_intr(int irq, void *voidbrd, struct pt_regs *regs)
1121{
1122 struct jsm_board *brd = (struct jsm_board *) voidbrd;
1123 struct jsm_channel *ch;
1124 int port = 0;
1125 int type = 0;
1126 int current_port;
1127 u32 tmp;
1128 u32 uart_poll;
1129 unsigned long lock_flags;
1130 unsigned long lock_flags2;
1131 int outofloop_count = 0;
1132
1133 brd->intr_count++;
1134
1135 /* Lock out the slow poller from running on this board. */
1136 spin_lock_irqsave(&brd->bd_intr_lock, lock_flags);
1137
1138 /*
1139 * Read in "extended" IRQ information from the 32bit Neo register.
1140 * Bits 0-7: What port triggered the interrupt.
1141 * Bits 8-31: Each 3bits indicate what type of interrupt occurred.
1142 */
1143 uart_poll = readl(brd->re_map_membase + UART_17158_POLL_ADDR_OFFSET);
1144
1145 jsm_printk(INTR, INFO, &brd->pci_dev,
1146 "%s:%d uart_poll: %x\n", __FILE__, __LINE__, uart_poll);
1147
1148 if (!uart_poll) {
1149 jsm_printk(INTR, INFO, &brd->pci_dev,
1150 "Kernel interrupted to me, but no pending interrupts...\n");
1151 spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
1152 return IRQ_NONE;
1153 }
1154
1155 /* At this point, we have at least SOMETHING to service, dig further... */
1156
1157 current_port = 0;
1158
1159 /* Loop on each port */
1160 while (((uart_poll & 0xff) != 0) && (outofloop_count < 0xff)){
1161
1162 tmp = uart_poll;
1163 outofloop_count++;
1164
1165 /* Check current port to see if it has interrupt pending */
1166 if ((tmp & jsm_offset_table[current_port]) != 0) {
1167 port = current_port;
1168 type = tmp >> (8 + (port * 3));
1169 type &= 0x7;
1170 } else {
1171 current_port++;
1172 continue;
1173 }
1174
1175 jsm_printk(INTR, INFO, &brd->pci_dev,
1176 "%s:%d port: %x type: %x\n", __FILE__, __LINE__, port, type);
1177
1178 /* Remove this port + type from uart_poll */
1179 uart_poll &= ~(jsm_offset_table[port]);
1180
1181 if (!type) {
1182 /* If no type, just ignore it, and move onto next port */
1183 jsm_printk(INTR, ERR, &brd->pci_dev,
1184 "Interrupt with no type! port: %d\n", port);
1185 continue;
1186 }
1187
1188 /* Switch on type of interrupt we have */
1189 switch (type) {
1190
1191 case UART_17158_RXRDY_TIMEOUT:
1192 /*
1193 * RXRDY Time-out is cleared by reading data in the
1194 * RX FIFO until it falls below the trigger level.
1195 */
1196
1197 /* Verify the port is in range. */
1198 if (port > brd->nasync)
1199 continue;
1200
1201 ch = brd->channels[port];
1202 neo_copy_data_from_uart_to_queue(ch);
1203
1204 /* Call our tty layer to enforce queue flow control if needed. */
1205 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1206 jsm_check_queue_flow_control(ch);
1207 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1208
1209 continue;
1210
1211 case UART_17158_RX_LINE_STATUS:
1212 /*
1213 * RXRDY and RX LINE Status (logic OR of LSR[4:1])
1214 */
1215 neo_parse_lsr(brd, port);
1216 continue;
1217
1218 case UART_17158_TXRDY:
1219 /*
1220 * TXRDY interrupt clears after reading ISR register for the UART channel.
1221 */
1222
1223 /*
1224 * Yes, this is odd...
1225 * Why would I check EVERY possibility of type of
1226 * interrupt, when we know its TXRDY???
1227 * Becuz for some reason, even tho we got triggered for TXRDY,
1228 * it seems to be occassionally wrong. Instead of TX, which
1229 * it should be, I was getting things like RXDY too. Weird.
1230 */
1231 neo_parse_isr(brd, port);
1232 continue;
1233
1234 case UART_17158_MSR:
1235 /*
1236 * MSR or flow control was seen.
1237 */
1238 neo_parse_isr(brd, port);
1239 continue;
1240
1241 default:
1242 /*
1243 * The UART triggered us with a bogus interrupt type.
1244 * It appears the Exar chip, when REALLY bogged down, will throw
1245 * these once and awhile.
1246 * Its harmless, just ignore it and move on.
1247 */
1248 jsm_printk(INTR, ERR, &brd->pci_dev,
1249 "%s:%d Unknown Interrupt type: %x\n", __FILE__, __LINE__, type);
1250 continue;
1251 }
1252 }
1253
1254 spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
1255
1256 jsm_printk(INTR, INFO, &brd->pci_dev, "finish.\n");
1257 return IRQ_HANDLED;
1258}
1259
1260/*
1261 * Neo specific way of turning off the receiver.
1262 * Used as a way to enforce queue flow control when in
1263 * hardware flow control mode.
1264 */
1265static void neo_disable_receiver(struct jsm_channel *ch)
1266{
1267 u8 tmp = readb(&ch->ch_neo_uart->ier);
1268 tmp &= ~(UART_IER_RDI);
1269 writeb(tmp, &ch->ch_neo_uart->ier);
1270
1271 /* flush write operation */
1272 neo_pci_posting_flush(ch->ch_bd);
1273}
1274
1275
1276/*
1277 * Neo specific way of turning on the receiver.
1278 * Used as a way to un-enforce queue flow control when in
1279 * hardware flow control mode.
1280 */
1281static void neo_enable_receiver(struct jsm_channel *ch)
1282{
1283 u8 tmp = readb(&ch->ch_neo_uart->ier);
1284 tmp |= (UART_IER_RDI);
1285 writeb(tmp, &ch->ch_neo_uart->ier);
1286
1287 /* flush write operation */
1288 neo_pci_posting_flush(ch->ch_bd);
1289}
1290
1291static void neo_send_start_character(struct jsm_channel *ch)
1292{
1293 if (!ch)
1294 return;
1295
1296 if (ch->ch_startc != __DISABLED_CHAR) {
1297 ch->ch_xon_sends++;
1298 writeb(ch->ch_startc, &ch->ch_neo_uart->txrx);
1299
1300 /* flush write operation */
1301 neo_pci_posting_flush(ch->ch_bd);
1302 }
1303}
1304
1305static void neo_send_stop_character(struct jsm_channel *ch)
1306{
1307 if (!ch)
1308 return;
1309
1310 if (ch->ch_stopc != __DISABLED_CHAR) {
1311 ch->ch_xoff_sends++;
1312 writeb(ch->ch_stopc, &ch->ch_neo_uart->txrx);
1313
1314 /* flush write operation */
1315 neo_pci_posting_flush(ch->ch_bd);
1316 }
1317}
1318
1319/*
1320 * neo_uart_init
1321 */
1322static void neo_uart_init(struct jsm_channel *ch)
1323{
1324 writeb(0, &ch->ch_neo_uart->ier);
1325 writeb(0, &ch->ch_neo_uart->efr);
1326 writeb(UART_EFR_ECB, &ch->ch_neo_uart->efr);
1327
1328 /* Clear out UART and FIFO */
1329 readb(&ch->ch_neo_uart->txrx);
1330 writeb((UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT), &ch->ch_neo_uart->isr_fcr);
1331 readb(&ch->ch_neo_uart->lsr);
1332 readb(&ch->ch_neo_uart->msr);
1333
1334 ch->ch_flags |= CH_FIFO_ENABLED;
1335
1336 /* Assert any signals we want up */
1337 writeb(ch->ch_mostat, &ch->ch_neo_uart->mcr);
1338}
1339
1340/*
1341 * Make the UART completely turn off.
1342 */
1343static void neo_uart_off(struct jsm_channel *ch)
1344{
1345 /* Turn off UART enhanced bits */
1346 writeb(0, &ch->ch_neo_uart->efr);
1347
1348 /* Stop all interrupts from occurring. */
1349 writeb(0, &ch->ch_neo_uart->ier);
1350}
1351
1352static u32 neo_get_uart_bytes_left(struct jsm_channel *ch)
1353{
1354 u8 left = 0;
1355 u8 lsr = readb(&ch->ch_neo_uart->lsr);
1356
1357 /* We must cache the LSR as some of the bits get reset once read... */
1358 ch->ch_cached_lsr |= lsr;
1359
1360 /* Determine whether the Transmitter is empty or not */
1361 if (!(lsr & UART_LSR_TEMT))
1362 left = 1;
1363 else {
1364 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1365 left = 0;
1366 }
1367
1368 return left;
1369}
1370
1371/* Channel lock MUST be held by the calling function! */
1372static void neo_send_break(struct jsm_channel *ch)
1373{
1374 /*
1375 * Set the time we should stop sending the break.
1376 * If we are already sending a break, toss away the existing
1377 * time to stop, and use this new value instead.
1378 */
1379
1380 /* Tell the UART to start sending the break */
1381 if (!(ch->ch_flags & CH_BREAK_SENDING)) {
1382 u8 temp = readb(&ch->ch_neo_uart->lcr);
1383 writeb((temp | UART_LCR_SBC), &ch->ch_neo_uart->lcr);
1384 ch->ch_flags |= (CH_BREAK_SENDING);
1385
1386 /* flush write operation */
1387 neo_pci_posting_flush(ch->ch_bd);
1388 }
1389}
1390
1391/*
1392 * neo_send_immediate_char.
1393 *
1394 * Sends a specific character as soon as possible to the UART,
1395 * jumping over any bytes that might be in the write queue.
1396 *
1397 * The channel lock MUST be held by the calling function.
1398 */
1399static void neo_send_immediate_char(struct jsm_channel *ch, unsigned char c)
1400{
1401 if (!ch)
1402 return;
1403
1404 writeb(c, &ch->ch_neo_uart->txrx);
1405
1406 /* flush write operation */
1407 neo_pci_posting_flush(ch->ch_bd);
1408}
1409
1410struct board_ops jsm_neo_ops = {
1411 .intr = neo_intr,
1412 .uart_init = neo_uart_init,
1413 .uart_off = neo_uart_off,
1414 .param = neo_param,
1415 .assert_modem_signals = neo_assert_modem_signals,
1416 .flush_uart_write = neo_flush_uart_write,
1417 .flush_uart_read = neo_flush_uart_read,
1418 .disable_receiver = neo_disable_receiver,
1419 .enable_receiver = neo_enable_receiver,
1420 .send_break = neo_send_break,
1421 .clear_break = neo_clear_break,
1422 .send_start_character = neo_send_start_character,
1423 .send_stop_character = neo_send_stop_character,
1424 .copy_data_from_queue_to_uart = neo_copy_data_from_queue_to_uart,
1425 .get_uart_bytes_left = neo_get_uart_bytes_left,
1426 .send_immediate_char = neo_send_immediate_char
1427};
diff --git a/drivers/serial/jsm/jsm_tty.c b/drivers/serial/jsm/jsm_tty.c
new file mode 100644
index 000000000000..7fb7cc07074b
--- /dev/null
+++ b/drivers/serial/jsm/jsm_tty.c
@@ -0,0 +1,1038 @@
1/************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
3 *
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
23 * Wendy Xiong <wendyx@us.ltcfwd.linux.ibm.com>
24 *
25 ***********************************************************************/
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/serial_reg.h>
29#include <linux/delay.h> /* For udelay */
30#include <linux/pci.h>
31
32#include "jsm.h"
33
34static inline int jsm_get_mstat(struct jsm_channel *ch)
35{
36 unsigned char mstat;
37 unsigned result;
38
39 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "start\n");
40
41 mstat = (ch->ch_mostat | ch->ch_mistat);
42
43 result = 0;
44
45 if (mstat & UART_MCR_DTR)
46 result |= TIOCM_DTR;
47 if (mstat & UART_MCR_RTS)
48 result |= TIOCM_RTS;
49 if (mstat & UART_MSR_CTS)
50 result |= TIOCM_CTS;
51 if (mstat & UART_MSR_DSR)
52 result |= TIOCM_DSR;
53 if (mstat & UART_MSR_RI)
54 result |= TIOCM_RI;
55 if (mstat & UART_MSR_DCD)
56 result |= TIOCM_CD;
57
58 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
59 return result;
60}
61
62static unsigned int jsm_tty_tx_empty(struct uart_port *port)
63{
64 return TIOCSER_TEMT;
65}
66
67/*
68 * Return modem signals to ld.
69 */
70static unsigned int jsm_tty_get_mctrl(struct uart_port *port)
71{
72 int result;
73 struct jsm_channel *channel = (struct jsm_channel *)port;
74
75 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
76
77 result = jsm_get_mstat(channel);
78
79 if (result < 0)
80 return -ENXIO;
81
82 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
83
84 return result;
85}
86
87/*
88 * jsm_set_modem_info()
89 *
90 * Set modem signals, called by ld.
91 */
92static void jsm_tty_set_mctrl(struct uart_port *port, unsigned int mctrl)
93{
94 struct jsm_channel *channel = (struct jsm_channel *)port;
95
96 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
97
98 if (mctrl & TIOCM_RTS)
99 channel->ch_mostat |= UART_MCR_RTS;
100 else
101 channel->ch_mostat &= ~UART_MCR_RTS;
102
103 if (mctrl & TIOCM_DTR)
104 channel->ch_mostat |= UART_MCR_DTR;
105 else
106 channel->ch_mostat &= ~UART_MCR_DTR;
107
108 channel->ch_bd->bd_ops->assert_modem_signals(channel);
109
110 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
111 udelay(10);
112}
113
114static void jsm_tty_start_tx(struct uart_port *port, unsigned int tty_start)
115{
116 struct jsm_channel *channel = (struct jsm_channel *)port;
117
118 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
119
120 channel->ch_flags &= ~(CH_STOP);
121 jsm_tty_write(port);
122
123 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
124}
125
126static void jsm_tty_stop_tx(struct uart_port *port, unsigned int tty_stop)
127{
128 struct jsm_channel *channel = (struct jsm_channel *)port;
129
130 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
131
132 channel->ch_flags |= (CH_STOP);
133
134 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
135}
136
137static void jsm_tty_send_xchar(struct uart_port *port, char ch)
138{
139 unsigned long lock_flags;
140 struct jsm_channel *channel = (struct jsm_channel *)port;
141
142 spin_lock_irqsave(&port->lock, lock_flags);
143 if (ch == port->info->tty->termios->c_cc[VSTART])
144 channel->ch_bd->bd_ops->send_start_character(channel);
145
146 if (ch == port->info->tty->termios->c_cc[VSTOP])
147 channel->ch_bd->bd_ops->send_stop_character(channel);
148 spin_unlock_irqrestore(&port->lock, lock_flags);
149}
150
151static void jsm_tty_stop_rx(struct uart_port *port)
152{
153 struct jsm_channel *channel = (struct jsm_channel *)port;
154
155 channel->ch_bd->bd_ops->disable_receiver(channel);
156}
157
158static void jsm_tty_break(struct uart_port *port, int break_state)
159{
160 unsigned long lock_flags;
161 struct jsm_channel *channel = (struct jsm_channel *)port;
162
163 spin_lock_irqsave(&port->lock, lock_flags);
164 if (break_state == -1)
165 channel->ch_bd->bd_ops->send_break(channel);
166 else
167 channel->ch_bd->bd_ops->clear_break(channel, 0);
168
169 spin_unlock_irqrestore(&port->lock, lock_flags);
170}
171
172static int jsm_tty_open(struct uart_port *port)
173{
174 struct jsm_board *brd;
175 int rc = 0;
176 struct jsm_channel *channel = (struct jsm_channel *)port;
177
178 /* Get board pointer from our array of majors we have allocated */
179 brd = channel->ch_bd;
180
181 /*
182 * Allocate channel buffers for read/write/error.
183 * Set flag, so we don't get trounced on.
184 */
185 channel->ch_flags |= (CH_OPENING);
186
187 /* Drop locks, as malloc with GFP_KERNEL can sleep */
188
189 if (!channel->ch_rqueue) {
190 channel->ch_rqueue = (u8 *) kmalloc(RQUEUESIZE, GFP_KERNEL);
191 if (!channel->ch_rqueue) {
192 jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
193 "unable to allocate read queue buf");
194 return -ENOMEM;
195 }
196 memset(channel->ch_rqueue, 0, RQUEUESIZE);
197 }
198 if (!channel->ch_equeue) {
199 channel->ch_equeue = (u8 *) kmalloc(EQUEUESIZE, GFP_KERNEL);
200 if (!channel->ch_equeue) {
201 jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
202 "unable to allocate error queue buf");
203 return -ENOMEM;
204 }
205 memset(channel->ch_equeue, 0, EQUEUESIZE);
206 }
207 if (!channel->ch_wqueue) {
208 channel->ch_wqueue = (u8 *) kmalloc(WQUEUESIZE, GFP_KERNEL);
209 if (!channel->ch_wqueue) {
210 jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
211 "unable to allocate write queue buf");
212 return -ENOMEM;
213 }
214 memset(channel->ch_wqueue, 0, WQUEUESIZE);
215 }
216
217 channel->ch_flags &= ~(CH_OPENING);
218 /*
219 * Initialize if neither terminal is open.
220 */
221 jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev,
222 "jsm_open: initializing channel in open...\n");
223
224 /*
225 * Flush input queues.
226 */
227 channel->ch_r_head = channel->ch_r_tail = 0;
228 channel->ch_e_head = channel->ch_e_tail = 0;
229 channel->ch_w_head = channel->ch_w_tail = 0;
230
231 brd->bd_ops->flush_uart_write(channel);
232 brd->bd_ops->flush_uart_read(channel);
233
234 channel->ch_flags = 0;
235 channel->ch_cached_lsr = 0;
236 channel->ch_stops_sent = 0;
237
238 channel->ch_c_cflag = port->info->tty->termios->c_cflag;
239 channel->ch_c_iflag = port->info->tty->termios->c_iflag;
240 channel->ch_c_oflag = port->info->tty->termios->c_oflag;
241 channel->ch_c_lflag = port->info->tty->termios->c_lflag;
242 channel->ch_startc = port->info->tty->termios->c_cc[VSTART];
243 channel->ch_stopc = port->info->tty->termios->c_cc[VSTOP];
244
245 /* Tell UART to init itself */
246 brd->bd_ops->uart_init(channel);
247
248 /*
249 * Run param in case we changed anything
250 */
251 brd->bd_ops->param(channel);
252
253 jsm_carrier(channel);
254
255 channel->ch_open_count++;
256
257 jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev, "finish\n");
258 return rc;
259}
260
261static void jsm_tty_close(struct uart_port *port)
262{
263 struct jsm_board *bd;
264 struct termios *ts;
265 struct jsm_channel *channel = (struct jsm_channel *)port;
266
267 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "start\n");
268
269 bd = channel->ch_bd;
270 ts = channel->uart_port.info->tty->termios;
271
272 channel->ch_flags &= ~(CH_STOPI);
273
274 channel->ch_open_count--;
275
276 /*
277 * If we have HUPCL set, lower DTR and RTS
278 */
279 if (channel->ch_c_cflag & HUPCL) {
280 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev,
281 "Close. HUPCL set, dropping DTR/RTS\n");
282
283 /* Drop RTS/DTR */
284 channel->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
285 bd->bd_ops->assert_modem_signals(channel);
286 }
287
288 channel->ch_old_baud = 0;
289
290 /* Turn off UART interrupts for this port */
291 channel->ch_bd->bd_ops->uart_off(channel);
292
293 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "finish\n");
294}
295
296static void jsm_tty_set_termios(struct uart_port *port,
297 struct termios *termios,
298 struct termios *old_termios)
299{
300 unsigned long lock_flags;
301 struct jsm_channel *channel = (struct jsm_channel *)port;
302
303 spin_lock_irqsave(&port->lock, lock_flags);
304 channel->ch_c_cflag = termios->c_cflag;
305 channel->ch_c_iflag = termios->c_iflag;
306 channel->ch_c_oflag = termios->c_oflag;
307 channel->ch_c_lflag = termios->c_lflag;
308 channel->ch_startc = termios->c_cc[VSTART];
309 channel->ch_stopc = termios->c_cc[VSTOP];
310
311 channel->ch_bd->bd_ops->param(channel);
312 jsm_carrier(channel);
313 spin_unlock_irqrestore(&port->lock, lock_flags);
314}
315
316static const char *jsm_tty_type(struct uart_port *port)
317{
318 return "jsm";
319}
320
321static void jsm_tty_release_port(struct uart_port *port)
322{
323}
324
325static int jsm_tty_request_port(struct uart_port *port)
326{
327 return 0;
328}
329
330static void jsm_config_port(struct uart_port *port, int flags)
331{
332 port->type = PORT_JSM;
333}
334
335static struct uart_ops jsm_ops = {
336 .tx_empty = jsm_tty_tx_empty,
337 .set_mctrl = jsm_tty_set_mctrl,
338 .get_mctrl = jsm_tty_get_mctrl,
339 .stop_tx = jsm_tty_stop_tx,
340 .start_tx = jsm_tty_start_tx,
341 .send_xchar = jsm_tty_send_xchar,
342 .stop_rx = jsm_tty_stop_rx,
343 .break_ctl = jsm_tty_break,
344 .startup = jsm_tty_open,
345 .shutdown = jsm_tty_close,
346 .set_termios = jsm_tty_set_termios,
347 .type = jsm_tty_type,
348 .release_port = jsm_tty_release_port,
349 .request_port = jsm_tty_request_port,
350 .config_port = jsm_config_port,
351};
352
353/*
354 * jsm_tty_init()
355 *
356 * Init the tty subsystem. Called once per board after board has been
357 * downloaded and init'ed.
358 */
359int jsm_tty_init(struct jsm_board *brd)
360{
361 int i;
362 void __iomem *vaddr;
363 struct jsm_channel *ch;
364
365 if (!brd)
366 return -ENXIO;
367
368 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
369
370 /*
371 * Initialize board structure elements.
372 */
373
374 brd->nasync = brd->maxports;
375
376 /*
377 * Allocate channel memory that might not have been allocated
378 * when the driver was first loaded.
379 */
380 for (i = 0; i < brd->nasync; i++) {
381 if (!brd->channels[i]) {
382
383 /*
384 * Okay to malloc with GFP_KERNEL, we are not at
385 * interrupt context, and there are no locks held.
386 */
387 brd->channels[i] = kmalloc(sizeof(struct jsm_channel), GFP_KERNEL);
388 if (!brd->channels[i]) {
389 jsm_printk(CORE, ERR, &brd->pci_dev,
390 "%s:%d Unable to allocate memory for channel struct\n",
391 __FILE__, __LINE__);
392 }
393 memset(brd->channels[i], 0, sizeof(struct jsm_channel));
394 }
395 }
396
397 ch = brd->channels[0];
398 vaddr = brd->re_map_membase;
399
400 /* Set up channel variables */
401 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
402
403 if (!brd->channels[i])
404 continue;
405
406 spin_lock_init(&ch->ch_lock);
407
408 if (brd->bd_uart_offset == 0x200)
409 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
410
411 ch->ch_bd = brd;
412 ch->ch_portnum = i;
413
414 /* .25 second delay */
415 ch->ch_close_delay = 250;
416
417 init_waitqueue_head(&ch->ch_flags_wait);
418 }
419
420 jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
421 return 0;
422}
423
424int jsm_uart_port_init(struct jsm_board *brd)
425{
426 int i;
427 struct jsm_channel *ch;
428
429 if (!brd)
430 return -ENXIO;
431
432 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
433
434 /*
435 * Initialize board structure elements.
436 */
437
438 brd->nasync = brd->maxports;
439
440 /* Set up channel variables */
441 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
442
443 if (!brd->channels[i])
444 continue;
445
446 brd->channels[i]->uart_port.irq = brd->irq;
447 brd->channels[i]->uart_port.type = PORT_JSM;
448 brd->channels[i]->uart_port.iotype = UPIO_MEM;
449 brd->channels[i]->uart_port.membase = brd->re_map_membase;
450 brd->channels[i]->uart_port.fifosize = 16;
451 brd->channels[i]->uart_port.ops = &jsm_ops;
452 brd->channels[i]->uart_port.line = brd->channels[i]->ch_portnum + brd->boardnum * 2;
453 if (uart_add_one_port (&jsm_uart_driver, &brd->channels[i]->uart_port))
454 printk(KERN_INFO "Added device failed\n");
455 else
456 printk(KERN_INFO "Added device \n");
457 }
458
459 jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
460 return 0;
461}
462
463int jsm_remove_uart_port(struct jsm_board *brd)
464{
465 int i;
466 struct jsm_channel *ch;
467
468 if (!brd)
469 return -ENXIO;
470
471 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
472
473 /*
474 * Initialize board structure elements.
475 */
476
477 brd->nasync = brd->maxports;
478
479 /* Set up channel variables */
480 for (i = 0; i < brd->nasync; i++) {
481
482 if (!brd->channels[i])
483 continue;
484
485 ch = brd->channels[i];
486
487 uart_remove_one_port(&jsm_uart_driver, &brd->channels[i]->uart_port);
488 }
489
490 jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
491 return 0;
492}
493
494void jsm_input(struct jsm_channel *ch)
495{
496 struct jsm_board *bd;
497 struct tty_struct *tp;
498 u32 rmask;
499 u16 head;
500 u16 tail;
501 int data_len;
502 unsigned long lock_flags;
503 int flip_len;
504 int len = 0;
505 int n = 0;
506 char *buf = NULL;
507 char *buf2 = NULL;
508 int s = 0;
509 int i = 0;
510
511 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
512
513 if (!ch)
514 return;
515
516 tp = ch->uart_port.info->tty;
517
518 bd = ch->ch_bd;
519 if(!bd)
520 return;
521
522 spin_lock_irqsave(&ch->ch_lock, lock_flags);
523
524 /*
525 *Figure the number of characters in the buffer.
526 *Exit immediately if none.
527 */
528
529 rmask = RQUEUEMASK;
530
531 head = ch->ch_r_head & rmask;
532 tail = ch->ch_r_tail & rmask;
533
534 data_len = (head - tail) & rmask;
535 if (data_len == 0) {
536 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
537 return;
538 }
539
540 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
541
542 /*
543 *If the device is not open, or CREAD is off, flush
544 *input data and return immediately.
545 */
546 if (!tp ||
547 !(tp->termios->c_cflag & CREAD) ) {
548
549 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
550 "input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum);
551 ch->ch_r_head = tail;
552
553 /* Force queue flow control to be released, if needed */
554 jsm_check_queue_flow_control(ch);
555
556 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
557 return;
558 }
559
560 /*
561 * If we are throttled, simply don't read any data.
562 */
563 if (ch->ch_flags & CH_STOPI) {
564 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
565 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
566 "Port %d throttled, not reading any data. head: %x tail: %x\n",
567 ch->ch_portnum, head, tail);
568 return;
569 }
570
571 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start 2\n");
572
573 /*
574 * If the rxbuf is empty and we are not throttled, put as much
575 * as we can directly into the linux TTY flip buffer.
576 * The jsm_rawreadok case takes advantage of carnal knowledge that
577 * the char_buf and the flag_buf are next to each other and
578 * are each of (2 * TTY_FLIPBUF_SIZE) size.
579 *
580 * NOTE: if(!tty->real_raw), the call to ldisc.receive_buf
581 *actually still uses the flag buffer, so you can't
582 *use it for input data
583 */
584 if (jsm_rawreadok) {
585 if (tp->real_raw)
586 flip_len = MYFLIPLEN;
587 else
588 flip_len = 2 * TTY_FLIPBUF_SIZE;
589 } else
590 flip_len = TTY_FLIPBUF_SIZE - tp->flip.count;
591
592 len = min(data_len, flip_len);
593 len = min(len, (N_TTY_BUF_SIZE - 1) - tp->read_cnt);
594
595 if (len <= 0) {
596 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
597 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "jsm_input 1\n");
598 return;
599 }
600
601 /*
602 * If we're bypassing flip buffers on rx, we can blast it
603 * right into the beginning of the buffer.
604 */
605 if (jsm_rawreadok) {
606 if (tp->real_raw) {
607 if (ch->ch_flags & CH_FLIPBUF_IN_USE) {
608 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
609 "JSM - FLIPBUF in use. delaying input\n");
610 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
611 return;
612 }
613 ch->ch_flags |= CH_FLIPBUF_IN_USE;
614 buf = ch->ch_bd->flipbuf;
615 buf2 = NULL;
616 } else {
617 buf = tp->flip.char_buf;
618 buf2 = tp->flip.flag_buf;
619 }
620 } else {
621 buf = tp->flip.char_buf_ptr;
622 buf2 = tp->flip.flag_buf_ptr;
623 }
624
625 n = len;
626
627 /*
628 * n now contains the most amount of data we can copy,
629 * bounded either by the flip buffer size or the amount
630 * of data the card actually has pending...
631 */
632 while (n) {
633 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
634 s = min(s, n);
635
636 if (s <= 0)
637 break;
638
639 memcpy(buf, ch->ch_rqueue + tail, s);
640
641 /* buf2 is only set when port isn't raw */
642 if (buf2)
643 memcpy(buf2, ch->ch_equeue + tail, s);
644
645 tail += s;
646 buf += s;
647 if (buf2)
648 buf2 += s;
649 n -= s;
650 /* Flip queue if needed */
651 tail &= rmask;
652 }
653
654 /*
655 * In high performance mode, we don't have to update
656 * flag_buf or any of the counts or pointers into flip buf.
657 */
658 if (!jsm_rawreadok) {
659 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
660 for (i = 0; i < len; i++) {
661 /*
662 * Give the Linux ld the flags in the
663 * format it likes.
664 */
665 if (tp->flip.flag_buf_ptr[i] & UART_LSR_BI)
666 tp->flip.flag_buf_ptr[i] = TTY_BREAK;
667 else if (tp->flip.flag_buf_ptr[i] & UART_LSR_PE)
668 tp->flip.flag_buf_ptr[i] = TTY_PARITY;
669 else if (tp->flip.flag_buf_ptr[i] & UART_LSR_FE)
670 tp->flip.flag_buf_ptr[i] = TTY_FRAME;
671 else
672 tp->flip.flag_buf_ptr[i] = TTY_NORMAL;
673 }
674 } else {
675 memset(tp->flip.flag_buf_ptr, 0, len);
676 }
677
678 tp->flip.char_buf_ptr += len;
679 tp->flip.flag_buf_ptr += len;
680 tp->flip.count += len;
681 }
682 else if (!tp->real_raw) {
683 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
684 for (i = 0; i < len; i++) {
685 /*
686 * Give the Linux ld the flags in the
687 * format it likes.
688 */
689 if (tp->flip.flag_buf_ptr[i] & UART_LSR_BI)
690 tp->flip.flag_buf_ptr[i] = TTY_BREAK;
691 else if (tp->flip.flag_buf_ptr[i] & UART_LSR_PE)
692 tp->flip.flag_buf_ptr[i] = TTY_PARITY;
693 else if (tp->flip.flag_buf_ptr[i] & UART_LSR_FE)
694 tp->flip.flag_buf_ptr[i] = TTY_FRAME;
695 else
696 tp->flip.flag_buf_ptr[i] = TTY_NORMAL;
697 }
698 } else
699 memset(tp->flip.flag_buf, 0, len);
700 }
701
702 /*
703 * If we're doing raw reads, jam it right into the
704 * line disc bypassing the flip buffers.
705 */
706 if (jsm_rawreadok) {
707 if (tp->real_raw) {
708 ch->ch_r_tail = tail & rmask;
709 ch->ch_e_tail = tail & rmask;
710
711 jsm_check_queue_flow_control(ch);
712
713 /* !!! WE *MUST* LET GO OF ALL LOCKS BEFORE CALLING RECEIVE BUF !!! */
714
715 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
716
717 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
718 "jsm_input. %d real_raw len:%d calling receive_buf for board %d\n",
719 __LINE__, len, ch->ch_bd->boardnum);
720 tp->ldisc.receive_buf(tp, ch->ch_bd->flipbuf, NULL, len);
721
722 /* Allow use of channel flip buffer again */
723 spin_lock_irqsave(&ch->ch_lock, lock_flags);
724 ch->ch_flags &= ~CH_FLIPBUF_IN_USE;
725 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
726
727 } else {
728 ch->ch_r_tail = tail & rmask;
729 ch->ch_e_tail = tail & rmask;
730
731 jsm_check_queue_flow_control(ch);
732
733 /* !!! WE *MUST* LET GO OF ALL LOCKS BEFORE CALLING RECEIVE BUF !!! */
734 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
735
736 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
737 "jsm_input. %d not real_raw len:%d calling receive_buf for board %d\n",
738 __LINE__, len, ch->ch_bd->boardnum);
739
740 tp->ldisc.receive_buf(tp, tp->flip.char_buf, tp->flip.flag_buf, len);
741 }
742 } else {
743 ch->ch_r_tail = tail & rmask;
744 ch->ch_e_tail = tail & rmask;
745
746 jsm_check_queue_flow_control(ch);
747
748 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
749
750 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
751 "jsm_input. %d not jsm_read raw okay scheduling flip\n", __LINE__);
752 tty_schedule_flip(tp);
753 }
754
755 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
756}
757
758void jsm_carrier(struct jsm_channel *ch)
759{
760 struct jsm_board *bd;
761
762 int virt_carrier = 0;
763 int phys_carrier = 0;
764
765 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev, "start\n");
766 if (!ch)
767 return;
768
769 bd = ch->ch_bd;
770
771 if (!bd)
772 return;
773
774 if (ch->ch_mistat & UART_MSR_DCD) {
775 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
776 "mistat: %x D_CD: %x\n", ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD);
777 phys_carrier = 1;
778 }
779
780 if (ch->ch_c_cflag & CLOCAL)
781 virt_carrier = 1;
782
783 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
784 "DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier);
785
786 /*
787 * Test for a VIRTUAL carrier transition to HIGH.
788 */
789 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
790
791 /*
792 * When carrier rises, wake any threads waiting
793 * for carrier in the open routine.
794 */
795
796 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
797 "carrier: virt DCD rose\n");
798
799 if (waitqueue_active(&(ch->ch_flags_wait)))
800 wake_up_interruptible(&ch->ch_flags_wait);
801 }
802
803 /*
804 * Test for a PHYSICAL carrier transition to HIGH.
805 */
806 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
807
808 /*
809 * When carrier rises, wake any threads waiting
810 * for carrier in the open routine.
811 */
812
813 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
814 "carrier: physical DCD rose\n");
815
816 if (waitqueue_active(&(ch->ch_flags_wait)))
817 wake_up_interruptible(&ch->ch_flags_wait);
818 }
819
820 /*
821 * Test for a PHYSICAL transition to low, so long as we aren't
822 * currently ignoring physical transitions (which is what "virtual
823 * carrier" indicates).
824 *
825 * The transition of the virtual carrier to low really doesn't
826 * matter... it really only means "ignore carrier state", not
827 * "make pretend that carrier is there".
828 */
829 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0)
830 && (phys_carrier == 0)) {
831 /*
832 * When carrier drops:
833 *
834 * Drop carrier on all open units.
835 *
836 * Flush queues, waking up any task waiting in the
837 * line discipline.
838 *
839 * Send a hangup to the control terminal.
840 *
841 * Enable all select calls.
842 */
843 if (waitqueue_active(&(ch->ch_flags_wait)))
844 wake_up_interruptible(&ch->ch_flags_wait);
845 }
846
847 /*
848 * Make sure that our cached values reflect the current reality.
849 */
850 if (virt_carrier == 1)
851 ch->ch_flags |= CH_FCAR;
852 else
853 ch->ch_flags &= ~CH_FCAR;
854
855 if (phys_carrier == 1)
856 ch->ch_flags |= CH_CD;
857 else
858 ch->ch_flags &= ~CH_CD;
859}
860
861
862void jsm_check_queue_flow_control(struct jsm_channel *ch)
863{
864 int qleft = 0;
865
866 /* Store how much space we have left in the queue */
867 if ((qleft = ch->ch_r_tail - ch->ch_r_head - 1) < 0)
868 qleft += RQUEUEMASK + 1;
869
870 /*
871 * Check to see if we should enforce flow control on our queue because
872 * the ld (or user) isn't reading data out of our queue fast enuf.
873 *
874 * NOTE: This is done based on what the current flow control of the
875 * port is set for.
876 *
877 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
878 * This will cause the UART's FIFO to back up, and force
879 * the RTS signal to be dropped.
880 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
881 * the other side, in hopes it will stop sending data to us.
882 * 3) NONE - Nothing we can do. We will simply drop any extra data
883 * that gets sent into us when the queue fills up.
884 */
885 if (qleft < 256) {
886 /* HWFLOW */
887 if (ch->ch_c_cflag & CRTSCTS) {
888 if(!(ch->ch_flags & CH_RECEIVER_OFF)) {
889 ch->ch_bd->bd_ops->disable_receiver(ch);
890 ch->ch_flags |= (CH_RECEIVER_OFF);
891 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
892 "Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
893 qleft);
894 }
895 }
896 /* SWFLOW */
897 else if (ch->ch_c_iflag & IXOFF) {
898 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
899 ch->ch_bd->bd_ops->send_stop_character(ch);
900 ch->ch_stops_sent++;
901 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
902 "Sending stop char! Times sent: %x\n", ch->ch_stops_sent);
903 }
904 }
905 }
906
907 /*
908 * Check to see if we should unenforce flow control because
909 * ld (or user) finally read enuf data out of our queue.
910 *
911 * NOTE: This is done based on what the current flow control of the
912 * port is set for.
913 *
914 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
915 * This will cause the UART's FIFO to raise RTS back up,
916 * which will allow the other side to start sending data again.
917 * 2) SWFLOW (IXOFF) - Send a start character to
918 * the other side, so it will start sending data to us again.
919 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
920 * other side, we don't need to do anything now.
921 */
922 if (qleft > (RQUEUESIZE / 2)) {
923 /* HWFLOW */
924 if (ch->ch_c_cflag & CRTSCTS) {
925 if (ch->ch_flags & CH_RECEIVER_OFF) {
926 ch->ch_bd->bd_ops->enable_receiver(ch);
927 ch->ch_flags &= ~(CH_RECEIVER_OFF);
928 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
929 "Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
930 qleft);
931 }
932 }
933 /* SWFLOW */
934 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
935 ch->ch_stops_sent = 0;
936 ch->ch_bd->bd_ops->send_start_character(ch);
937 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "Sending start char!\n");
938 }
939 }
940}
941
942/*
943 * jsm_tty_write()
944 *
945 * Take data from the user or kernel and send it out to the FEP.
946 * In here exists all the Transparent Print magic as well.
947 */
948int jsm_tty_write(struct uart_port *port)
949{
950 int bufcount = 0, n = 0;
951 int data_count = 0,data_count1 =0;
952 u16 head;
953 u16 tail;
954 u16 tmask;
955 u32 remain;
956 int temp_tail = port->info->xmit.tail;
957 struct jsm_channel *channel = (struct jsm_channel *)port;
958
959 tmask = WQUEUEMASK;
960 head = (channel->ch_w_head) & tmask;
961 tail = (channel->ch_w_tail) & tmask;
962
963 if ((bufcount = tail - head - 1) < 0)
964 bufcount += WQUEUESIZE;
965
966 n = bufcount;
967
968 n = min(n, 56);
969 remain = WQUEUESIZE - head;
970
971 data_count = 0;
972 if (n >= remain) {
973 n -= remain;
974 while ((port->info->xmit.head != temp_tail) &&
975 (data_count < remain)) {
976 channel->ch_wqueue[head++] =
977 port->info->xmit.buf[temp_tail];
978
979 temp_tail++;
980 temp_tail &= (UART_XMIT_SIZE - 1);
981 data_count++;
982 }
983 if (data_count == remain) head = 0;
984 }
985
986 data_count1 = 0;
987 if (n > 0) {
988 remain = n;
989 while ((port->info->xmit.head != temp_tail) &&
990 (data_count1 < remain)) {
991 channel->ch_wqueue[head++] =
992 port->info->xmit.buf[temp_tail];
993
994 temp_tail++;
995 temp_tail &= (UART_XMIT_SIZE - 1);
996 data_count1++;
997
998 }
999 }
1000
1001 port->info->xmit.tail = temp_tail;
1002
1003 data_count += data_count1;
1004 if (data_count) {
1005 head &= tmask;
1006 channel->ch_w_head = head;
1007 }
1008
1009 if (data_count) {
1010 channel->ch_bd->bd_ops->copy_data_from_queue_to_uart(channel);
1011 }
1012
1013 return data_count;
1014}
1015
1016static ssize_t jsm_driver_version_show(struct device_driver *ddp, char *buf)
1017{
1018 return snprintf(buf, PAGE_SIZE, "%s\n", JSM_VERSION);
1019}
1020static DRIVER_ATTR(version, S_IRUSR, jsm_driver_version_show, NULL);
1021
1022static ssize_t jsm_driver_state_show(struct device_driver *ddp, char *buf)
1023{
1024 return snprintf(buf, PAGE_SIZE, "%s\n", jsm_driver_state_text[jsm_driver_state]);
1025}
1026static DRIVER_ATTR(state, S_IRUSR, jsm_driver_state_show, NULL);
1027
1028void jsm_create_driver_sysfiles(struct device_driver *driverfs)
1029{
1030 driver_create_file(driverfs, &driver_attr_version);
1031 driver_create_file(driverfs, &driver_attr_state);
1032}
1033
1034void jsm_remove_driver_sysfiles(struct device_driver *driverfs)
1035{
1036 driver_remove_file(driverfs, &driver_attr_version);
1037 driver_remove_file(driverfs, &driver_attr_state);
1038}
diff --git a/drivers/serial/m32r_sio.c b/drivers/serial/m32r_sio.c
new file mode 100644
index 000000000000..08d61f13edc6
--- /dev/null
+++ b/drivers/serial/m32r_sio.c
@@ -0,0 +1,1218 @@
1/*
2 * m32r_sio.c
3 *
4 * Driver for M32R serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Based on drivers/serial/8250.c.
8 *
9 * Copyright (C) 2001 Russell King.
10 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18/*
19 * A note about mapbase / membase
20 *
21 * mapbase is the physical address of the IO port. Currently, we don't
22 * support this very well, and it may well be dropped from this driver
23 * in future. As such, mapbase should be NULL.
24 *
25 * membase is an 'ioremapped' cookie. This is compatible with the old
26 * serial.c driver, and is currently the preferred form.
27 */
28#include <linux/config.h>
29
30#if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31#define SUPPORT_SYSRQ
32#endif
33
34#include <linux/module.h>
35#include <linux/tty.h>
36#include <linux/ioport.h>
37#include <linux/init.h>
38#include <linux/console.h>
39#include <linux/sysrq.h>
40#include <linux/serial.h>
41#include <linux/serialP.h>
42#include <linux/delay.h>
43
44#include <asm/m32r.h>
45#include <asm/io.h>
46#include <asm/irq.h>
47
48#define PORT_M32R_BASE PORT_M32R_SIO
49#define PORT_INDEX(x) (x - PORT_M32R_BASE + 1)
50#define BAUD_RATE 115200
51
52#include <linux/serial_core.h>
53#include "m32r_sio.h"
54#include "m32r_sio_reg.h"
55
56/*
57 * Debugging.
58 */
59#if 0
60#define DEBUG_AUTOCONF(fmt...) printk(fmt)
61#else
62#define DEBUG_AUTOCONF(fmt...) do { } while (0)
63#endif
64
65#if 0
66#define DEBUG_INTR(fmt...) printk(fmt)
67#else
68#define DEBUG_INTR(fmt...) do { } while (0)
69#endif
70
71#define PASS_LIMIT 256
72
73/*
74 * We default to IRQ0 for the "no irq" hack. Some
75 * machine types want others as well - they're free
76 * to redefine this in their header file.
77 */
78#define is_real_interrupt(irq) ((irq) != 0)
79
80#include <asm/serial.h>
81
82/* Standard COM flags */
83#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
84
85/*
86 * SERIAL_PORT_DFNS tells us about built-in ports that have no
87 * standard enumeration mechanism. Platforms that can find all
88 * serial ports via mechanisms like ACPI or PCI need not supply it.
89 */
90#undef SERIAL_PORT_DFNS
91#if defined(CONFIG_PLAT_USRV)
92
93#define SERIAL_PORT_DFNS \
94 /* UART CLK PORT IRQ FLAGS */ \
95 { 0, BASE_BAUD, 0x3F8, PLD_IRQ_UART0, STD_COM_FLAGS }, /* ttyS0 */ \
96 { 0, BASE_BAUD, 0x2F8, PLD_IRQ_UART1, STD_COM_FLAGS }, /* ttyS1 */
97
98#else /* !CONFIG_PLAT_USRV */
99
100#if defined(CONFIG_SERIAL_M32R_PLDSIO)
101#define SERIAL_PORT_DFNS \
102 { 0, BASE_BAUD, ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV, \
103 STD_COM_FLAGS }, /* ttyS0 */
104#else
105#define SERIAL_PORT_DFNS \
106 { 0, BASE_BAUD, M32R_SIO_OFFSET, M32R_IRQ_SIO0_R, \
107 STD_COM_FLAGS }, /* ttyS0 */
108#endif
109
110#endif /* !CONFIG_PLAT_USRV */
111
112static struct old_serial_port old_serial_port[] = {
113 SERIAL_PORT_DFNS /* defined in asm/serial.h */
114};
115
116#define UART_NR ARRAY_SIZE(old_serial_port)
117
118struct uart_sio_port {
119 struct uart_port port;
120 struct timer_list timer; /* "no irq" timer */
121 struct list_head list; /* ports on this IRQ */
122 unsigned short rev;
123 unsigned char acr;
124 unsigned char ier;
125 unsigned char lcr;
126 unsigned char mcr_mask; /* mask of user bits */
127 unsigned char mcr_force; /* mask of forced bits */
128 unsigned char lsr_break_flag;
129
130 /*
131 * We provide a per-port pm hook.
132 */
133 void (*pm)(struct uart_port *port,
134 unsigned int state, unsigned int old);
135};
136
137struct irq_info {
138 spinlock_t lock;
139 struct list_head *head;
140};
141
142static struct irq_info irq_lists[NR_IRQS];
143
144/*
145 * Here we define the default xmit fifo size used for each type of UART.
146 */
147static const struct serial_uart_config uart_config[] = {
148 [PORT_UNKNOWN] = {
149 .name = "unknown",
150 .dfl_xmit_fifo_size = 1,
151 .flags = 0,
152 },
153 [PORT_INDEX(PORT_M32R_SIO)] = {
154 .name = "M32RSIO",
155 .dfl_xmit_fifo_size = 1,
156 .flags = 0,
157 },
158};
159
160#ifdef CONFIG_SERIAL_M32R_PLDSIO
161
162#define __sio_in(x) inw((unsigned long)(x))
163#define __sio_out(v,x) outw((v),(unsigned long)(x))
164
165static inline void sio_set_baud_rate(unsigned long baud)
166{
167 unsigned short sbaud;
168 sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
169 __sio_out(sbaud, PLD_ESIO0BAUR);
170}
171
172static void sio_reset(void)
173{
174 unsigned short tmp;
175
176 tmp = __sio_in(PLD_ESIO0RXB);
177 tmp = __sio_in(PLD_ESIO0RXB);
178 tmp = __sio_in(PLD_ESIO0CR);
179 sio_set_baud_rate(BAUD_RATE);
180 __sio_out(0x0300, PLD_ESIO0CR);
181 __sio_out(0x0003, PLD_ESIO0CR);
182}
183
184static void sio_init(void)
185{
186 unsigned short tmp;
187
188 tmp = __sio_in(PLD_ESIO0RXB);
189 tmp = __sio_in(PLD_ESIO0RXB);
190 tmp = __sio_in(PLD_ESIO0CR);
191 __sio_out(0x0300, PLD_ESIO0CR);
192 __sio_out(0x0003, PLD_ESIO0CR);
193}
194
195static void sio_error(int *status)
196{
197 printk("SIO0 error[%04x]\n", *status);
198 do {
199 sio_init();
200 } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
201}
202
203#else /* not CONFIG_SERIAL_M32R_PLDSIO */
204
205#define __sio_in(x) inl(x)
206#define __sio_out(v,x) outl((v),(x))
207
208static inline void sio_set_baud_rate(unsigned long baud)
209{
210 unsigned long i, j;
211
212 i = boot_cpu_data.bus_clock / (baud * 16);
213 j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
214 i -= 1;
215 j = (j + 1) >> 1;
216
217 __sio_out(i, M32R_SIO0_BAUR_PORTL);
218 __sio_out(j, M32R_SIO0_RBAUR_PORTL);
219}
220
221static void sio_reset(void)
222{
223 __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */
224 __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */
225 __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */
226 sio_set_baud_rate(BAUD_RATE);
227 __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
228 __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */
229}
230
231static void sio_init(void)
232{
233 unsigned int tmp;
234
235 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
236 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
237 tmp = __sio_in(M32R_SIO0_STS_PORTL);
238 __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
239}
240
241static void sio_error(int *status)
242{
243 printk("SIO0 error[%04x]\n", *status);
244 do {
245 sio_init();
246 } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
247}
248
249#endif /* CONFIG_SERIAL_M32R_PLDSIO */
250
251static _INLINE_ unsigned int sio_in(struct uart_sio_port *up, int offset)
252{
253 return __sio_in(up->port.iobase + offset);
254}
255
256static _INLINE_ void sio_out(struct uart_sio_port *up, int offset, int value)
257{
258 __sio_out(value, up->port.iobase + offset);
259}
260
261static _INLINE_ unsigned int serial_in(struct uart_sio_port *up, int offset)
262{
263 if (!offset)
264 return 0;
265
266 return __sio_in(offset);
267}
268
269static _INLINE_ void
270serial_out(struct uart_sio_port *up, int offset, int value)
271{
272 if (!offset)
273 return;
274
275 __sio_out(value, offset);
276}
277
278static void m32r_sio_stop_tx(struct uart_port *port, unsigned int tty_stop)
279{
280 struct uart_sio_port *up = (struct uart_sio_port *)port;
281
282 if (up->ier & UART_IER_THRI) {
283 up->ier &= ~UART_IER_THRI;
284 serial_out(up, UART_IER, up->ier);
285 }
286}
287
288static void m32r_sio_start_tx(struct uart_port *port, unsigned int tty_start)
289{
290#ifdef CONFIG_SERIAL_M32R_PLDSIO
291 struct uart_sio_port *up = (struct uart_sio_port *)port;
292 struct circ_buf *xmit = &up->port.info->xmit;
293
294 if (!(up->ier & UART_IER_THRI)) {
295 up->ier |= UART_IER_THRI;
296 serial_out(up, UART_IER, up->ier);
297 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
298 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
299 up->port.icount.tx++;
300 }
301 while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
302#else
303 struct uart_sio_port *up = (struct uart_sio_port *)port;
304
305 if (!(up->ier & UART_IER_THRI)) {
306 up->ier |= UART_IER_THRI;
307 serial_out(up, UART_IER, up->ier);
308 }
309#endif
310}
311
312static void m32r_sio_stop_rx(struct uart_port *port)
313{
314 struct uart_sio_port *up = (struct uart_sio_port *)port;
315
316 up->ier &= ~UART_IER_RLSI;
317 up->port.read_status_mask &= ~UART_LSR_DR;
318 serial_out(up, UART_IER, up->ier);
319}
320
321static void m32r_sio_enable_ms(struct uart_port *port)
322{
323 struct uart_sio_port *up = (struct uart_sio_port *)port;
324
325 up->ier |= UART_IER_MSI;
326 serial_out(up, UART_IER, up->ier);
327}
328
329static _INLINE_ void receive_chars(struct uart_sio_port *up, int *status,
330 struct pt_regs *regs)
331{
332 struct tty_struct *tty = up->port.info->tty;
333 unsigned char ch;
334 int max_count = 256;
335
336 do {
337 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
338 tty->flip.work.func((void *)tty);
339 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
340 return; // if TTY_DONT_FLIP is set
341 }
342 ch = sio_in(up, SIORXB);
343 *tty->flip.char_buf_ptr = ch;
344 *tty->flip.flag_buf_ptr = TTY_NORMAL;
345 up->port.icount.rx++;
346
347 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
348 UART_LSR_FE | UART_LSR_OE))) {
349 /*
350 * For statistics only
351 */
352 if (*status & UART_LSR_BI) {
353 *status &= ~(UART_LSR_FE | UART_LSR_PE);
354 up->port.icount.brk++;
355 /*
356 * We do the SysRQ and SAK checking
357 * here because otherwise the break
358 * may get masked by ignore_status_mask
359 * or read_status_mask.
360 */
361 if (uart_handle_break(&up->port))
362 goto ignore_char;
363 } else if (*status & UART_LSR_PE)
364 up->port.icount.parity++;
365 else if (*status & UART_LSR_FE)
366 up->port.icount.frame++;
367 if (*status & UART_LSR_OE)
368 up->port.icount.overrun++;
369
370 /*
371 * Mask off conditions which should be ingored.
372 */
373 *status &= up->port.read_status_mask;
374
375 if (up->port.line == up->port.cons->index) {
376 /* Recover the break flag from console xmit */
377 *status |= up->lsr_break_flag;
378 up->lsr_break_flag = 0;
379 }
380
381 if (*status & UART_LSR_BI) {
382 DEBUG_INTR("handling break....");
383 *tty->flip.flag_buf_ptr = TTY_BREAK;
384 } else if (*status & UART_LSR_PE)
385 *tty->flip.flag_buf_ptr = TTY_PARITY;
386 else if (*status & UART_LSR_FE)
387 *tty->flip.flag_buf_ptr = TTY_FRAME;
388 }
389 if (uart_handle_sysrq_char(&up->port, ch, regs))
390 goto ignore_char;
391 if ((*status & up->port.ignore_status_mask) == 0) {
392 tty->flip.flag_buf_ptr++;
393 tty->flip.char_buf_ptr++;
394 tty->flip.count++;
395 }
396 if ((*status & UART_LSR_OE) &&
397 tty->flip.count < TTY_FLIPBUF_SIZE) {
398 /*
399 * Overrun is special, since it's reported
400 * immediately, and doesn't affect the current
401 * character.
402 */
403 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
404 tty->flip.flag_buf_ptr++;
405 tty->flip.char_buf_ptr++;
406 tty->flip.count++;
407 }
408 ignore_char:
409 *status = serial_in(up, UART_LSR);
410 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
411 tty_flip_buffer_push(tty);
412}
413
414static _INLINE_ void transmit_chars(struct uart_sio_port *up)
415{
416 struct circ_buf *xmit = &up->port.info->xmit;
417 int count;
418
419 if (up->port.x_char) {
420#ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
421 serial_out(up, UART_TX, up->port.x_char);
422#endif
423 up->port.icount.tx++;
424 up->port.x_char = 0;
425 return;
426 }
427 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
428 m32r_sio_stop_tx(&up->port, 0);
429 return;
430 }
431
432 count = up->port.fifosize;
433 do {
434 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
435 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
436 up->port.icount.tx++;
437 if (uart_circ_empty(xmit))
438 break;
439 while (!serial_in(up, UART_LSR) & UART_LSR_THRE);
440
441 } while (--count > 0);
442
443 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
444 uart_write_wakeup(&up->port);
445
446 DEBUG_INTR("THRE...");
447
448 if (uart_circ_empty(xmit))
449 m32r_sio_stop_tx(&up->port, 0);
450}
451
452/*
453 * This handles the interrupt from one port.
454 */
455static inline void m32r_sio_handle_port(struct uart_sio_port *up,
456 unsigned int status, struct pt_regs *regs)
457{
458 DEBUG_INTR("status = %x...", status);
459
460 if (status & 0x04)
461 receive_chars(up, &status, regs);
462 if (status & 0x01)
463 transmit_chars(up);
464}
465
466/*
467 * This is the serial driver's interrupt routine.
468 *
469 * Arjan thinks the old way was overly complex, so it got simplified.
470 * Alan disagrees, saying that need the complexity to handle the weird
471 * nature of ISA shared interrupts. (This is a special exception.)
472 *
473 * In order to handle ISA shared interrupts properly, we need to check
474 * that all ports have been serviced, and therefore the ISA interrupt
475 * line has been de-asserted.
476 *
477 * This means we need to loop through all ports. checking that they
478 * don't have an interrupt pending.
479 */
480static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id,
481 struct pt_regs *regs)
482{
483 struct irq_info *i = dev_id;
484 struct list_head *l, *end = NULL;
485 int pass_counter = 0;
486
487 DEBUG_INTR("m32r_sio_interrupt(%d)...", irq);
488
489#ifdef CONFIG_SERIAL_M32R_PLDSIO
490// if (irq == PLD_IRQ_SIO0_SND)
491// irq = PLD_IRQ_SIO0_RCV;
492#else
493 if (irq == M32R_IRQ_SIO0_S)
494 irq = M32R_IRQ_SIO0_R;
495#endif
496
497 spin_lock(&i->lock);
498
499 l = i->head;
500 do {
501 struct uart_sio_port *up;
502 unsigned int sts;
503
504 up = list_entry(l, struct uart_sio_port, list);
505
506 sts = sio_in(up, SIOSTS);
507 if (sts & 0x5) {
508 spin_lock(&up->port.lock);
509 m32r_sio_handle_port(up, sts, regs);
510 spin_unlock(&up->port.lock);
511
512 end = NULL;
513 } else if (end == NULL)
514 end = l;
515
516 l = l->next;
517
518 if (l == i->head && pass_counter++ > PASS_LIMIT) {
519 if (sts & 0xe0)
520 sio_error(&sts);
521 break;
522 }
523 } while (l != end);
524
525 spin_unlock(&i->lock);
526
527 DEBUG_INTR("end.\n");
528
529 return IRQ_HANDLED;
530}
531
532/*
533 * To support ISA shared interrupts, we need to have one interrupt
534 * handler that ensures that the IRQ line has been deasserted
535 * before returning. Failing to do this will result in the IRQ
536 * line being stuck active, and, since ISA irqs are edge triggered,
537 * no more IRQs will be seen.
538 */
539static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
540{
541 spin_lock_irq(&i->lock);
542
543 if (!list_empty(i->head)) {
544 if (i->head == &up->list)
545 i->head = i->head->next;
546 list_del(&up->list);
547 } else {
548 BUG_ON(i->head != &up->list);
549 i->head = NULL;
550 }
551
552 spin_unlock_irq(&i->lock);
553}
554
555static int serial_link_irq_chain(struct uart_sio_port *up)
556{
557 struct irq_info *i = irq_lists + up->port.irq;
558 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
559
560 spin_lock_irq(&i->lock);
561
562 if (i->head) {
563 list_add(&up->list, i->head);
564 spin_unlock_irq(&i->lock);
565
566 ret = 0;
567 } else {
568 INIT_LIST_HEAD(&up->list);
569 i->head = &up->list;
570 spin_unlock_irq(&i->lock);
571
572 ret = request_irq(up->port.irq, m32r_sio_interrupt,
573 irq_flags, "SIO0-RX", i);
574 ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
575 irq_flags, "SIO0-TX", i);
576 if (ret < 0)
577 serial_do_unlink(i, up);
578 }
579
580 return ret;
581}
582
583static void serial_unlink_irq_chain(struct uart_sio_port *up)
584{
585 struct irq_info *i = irq_lists + up->port.irq;
586
587 BUG_ON(i->head == NULL);
588
589 if (list_empty(i->head)) {
590 free_irq(up->port.irq, i);
591 free_irq(up->port.irq + 1, i);
592 }
593
594 serial_do_unlink(i, up);
595}
596
597/*
598 * This function is used to handle ports that do not have an interrupt.
599 */
600static void m32r_sio_timeout(unsigned long data)
601{
602 struct uart_sio_port *up = (struct uart_sio_port *)data;
603 unsigned int timeout;
604 unsigned int sts;
605
606 sts = sio_in(up, SIOSTS);
607 if (sts & 0x5) {
608 spin_lock(&up->port.lock);
609 m32r_sio_handle_port(up, sts, NULL);
610 spin_unlock(&up->port.lock);
611 }
612
613 timeout = up->port.timeout;
614 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
615 mod_timer(&up->timer, jiffies + timeout);
616}
617
618static unsigned int m32r_sio_tx_empty(struct uart_port *port)
619{
620 struct uart_sio_port *up = (struct uart_sio_port *)port;
621 unsigned long flags;
622 unsigned int ret;
623
624 spin_lock_irqsave(&up->port.lock, flags);
625 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
626 spin_unlock_irqrestore(&up->port.lock, flags);
627
628 return ret;
629}
630
631static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
632{
633 return 0;
634}
635
636static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
637{
638
639}
640
641static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
642{
643
644}
645
646static int m32r_sio_startup(struct uart_port *port)
647{
648 struct uart_sio_port *up = (struct uart_sio_port *)port;
649 int retval;
650
651 sio_init();
652
653 /*
654 * If the "interrupt" for this port doesn't correspond with any
655 * hardware interrupt, we use a timer-based system. The original
656 * driver used to do this with IRQ0.
657 */
658 if (!is_real_interrupt(up->port.irq)) {
659 unsigned int timeout = up->port.timeout;
660
661 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
662
663 up->timer.data = (unsigned long)up;
664 mod_timer(&up->timer, jiffies + timeout);
665 } else {
666 retval = serial_link_irq_chain(up);
667 if (retval)
668 return retval;
669 }
670
671 /*
672 * Finally, enable interrupts. Note: Modem status interrupts
673 * are set via set_termios(), which will be occurring imminently
674 * anyway, so we don't enable them here.
675 * - M32R_SIO: 0x0c
676 * - M32R_PLDSIO: 0x04
677 */
678 up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
679 sio_out(up, SIOTRCR, up->ier);
680
681 /*
682 * And clear the interrupt registers again for luck.
683 */
684 sio_reset();
685
686 return 0;
687}
688
689static void m32r_sio_shutdown(struct uart_port *port)
690{
691 struct uart_sio_port *up = (struct uart_sio_port *)port;
692
693 /*
694 * Disable interrupts from this port
695 */
696 up->ier = 0;
697 sio_out(up, SIOTRCR, 0);
698
699 /*
700 * Disable break condition and FIFOs
701 */
702
703 sio_init();
704
705 if (!is_real_interrupt(up->port.irq))
706 del_timer_sync(&up->timer);
707 else
708 serial_unlink_irq_chain(up);
709}
710
711static unsigned int m32r_sio_get_divisor(struct uart_port *port,
712 unsigned int baud)
713{
714 return uart_get_divisor(port, baud);
715}
716
717static void m32r_sio_set_termios(struct uart_port *port,
718 struct termios *termios, struct termios *old)
719{
720 struct uart_sio_port *up = (struct uart_sio_port *)port;
721 unsigned char cval = 0;
722 unsigned long flags;
723 unsigned int baud, quot;
724
725 switch (termios->c_cflag & CSIZE) {
726 case CS5:
727 cval = 0x00;
728 break;
729 case CS6:
730 cval = 0x01;
731 break;
732 case CS7:
733 cval = 0x02;
734 break;
735 default:
736 case CS8:
737 cval = 0x03;
738 break;
739 }
740
741 if (termios->c_cflag & CSTOPB)
742 cval |= 0x04;
743 if (termios->c_cflag & PARENB)
744 cval |= UART_LCR_PARITY;
745 if (!(termios->c_cflag & PARODD))
746 cval |= UART_LCR_EPAR;
747#ifdef CMSPAR
748 if (termios->c_cflag & CMSPAR)
749 cval |= UART_LCR_SPAR;
750#endif
751
752 /*
753 * Ask the core to calculate the divisor for us.
754 */
755#ifdef CONFIG_SERIAL_M32R_PLDSIO
756 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
757#else
758 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
759#endif
760 quot = m32r_sio_get_divisor(port, baud);
761
762 /*
763 * Ok, we're now changing the port state. Do it with
764 * interrupts disabled.
765 */
766 spin_lock_irqsave(&up->port.lock, flags);
767
768 sio_set_baud_rate(baud);
769
770 /*
771 * Update the per-port timeout.
772 */
773 uart_update_timeout(port, termios->c_cflag, baud);
774
775 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
776 if (termios->c_iflag & INPCK)
777 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
778 if (termios->c_iflag & (BRKINT | PARMRK))
779 up->port.read_status_mask |= UART_LSR_BI;
780
781 /*
782 * Characteres to ignore
783 */
784 up->port.ignore_status_mask = 0;
785 if (termios->c_iflag & IGNPAR)
786 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
787 if (termios->c_iflag & IGNBRK) {
788 up->port.ignore_status_mask |= UART_LSR_BI;
789 /*
790 * If we're ignoring parity and break indicators,
791 * ignore overruns too (for real raw support).
792 */
793 if (termios->c_iflag & IGNPAR)
794 up->port.ignore_status_mask |= UART_LSR_OE;
795 }
796
797 /*
798 * ignore all characters if CREAD is not set
799 */
800 if ((termios->c_cflag & CREAD) == 0)
801 up->port.ignore_status_mask |= UART_LSR_DR;
802
803 /*
804 * CTS flow control flag and modem status interrupts
805 */
806 up->ier &= ~UART_IER_MSI;
807 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
808 up->ier |= UART_IER_MSI;
809
810 serial_out(up, UART_IER, up->ier);
811
812 up->lcr = cval; /* Save LCR */
813 spin_unlock_irqrestore(&up->port.lock, flags);
814}
815
816static void m32r_sio_pm(struct uart_port *port, unsigned int state,
817 unsigned int oldstate)
818{
819 struct uart_sio_port *up = (struct uart_sio_port *)port;
820
821 if (up->pm)
822 up->pm(port, state, oldstate);
823}
824
825/*
826 * Resource handling. This is complicated by the fact that resources
827 * depend on the port type. Maybe we should be claiming the standard
828 * 8250 ports, and then trying to get other resources as necessary?
829 */
830static int
831m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
832{
833 unsigned int size = 8 << up->port.regshift;
834#ifndef CONFIG_SERIAL_M32R_PLDSIO
835 unsigned long start;
836#endif
837 int ret = 0;
838
839 switch (up->port.iotype) {
840 case UPIO_MEM:
841 if (up->port.mapbase) {
842#ifdef CONFIG_SERIAL_M32R_PLDSIO
843 *res = request_mem_region(up->port.mapbase, size, "serial");
844#else
845 start = up->port.mapbase;
846 *res = request_mem_region(start, size, "serial");
847#endif
848 if (!*res)
849 ret = -EBUSY;
850 }
851 break;
852
853 case UPIO_PORT:
854 *res = request_region(up->port.iobase, size, "serial");
855 if (!*res)
856 ret = -EBUSY;
857 break;
858 }
859 return ret;
860}
861
862static void m32r_sio_release_port(struct uart_port *port)
863{
864 struct uart_sio_port *up = (struct uart_sio_port *)port;
865 unsigned long start, offset = 0, size = 0;
866
867 size <<= up->port.regshift;
868
869 switch (up->port.iotype) {
870 case UPIO_MEM:
871 if (up->port.mapbase) {
872 /*
873 * Unmap the area.
874 */
875 iounmap(up->port.membase);
876 up->port.membase = NULL;
877
878 start = up->port.mapbase;
879
880 if (size)
881 release_mem_region(start + offset, size);
882 release_mem_region(start, 8 << up->port.regshift);
883 }
884 break;
885
886 case UPIO_PORT:
887 start = up->port.iobase;
888
889 if (size)
890 release_region(start + offset, size);
891 release_region(start + offset, 8 << up->port.regshift);
892 break;
893
894 default:
895 break;
896 }
897}
898
899static int m32r_sio_request_port(struct uart_port *port)
900{
901 struct uart_sio_port *up = (struct uart_sio_port *)port;
902 struct resource *res = NULL;
903 int ret = 0;
904
905 ret = m32r_sio_request_std_resource(up, &res);
906
907 /*
908 * If we have a mapbase, then request that as well.
909 */
910 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
911 int size = res->end - res->start + 1;
912
913 up->port.membase = ioremap(up->port.mapbase, size);
914 if (!up->port.membase)
915 ret = -ENOMEM;
916 }
917
918 if (ret < 0) {
919 if (res)
920 release_resource(res);
921 }
922
923 return ret;
924}
925
926static void m32r_sio_config_port(struct uart_port *port, int flags)
927{
928 struct uart_sio_port *up = (struct uart_sio_port *)port;
929
930 spin_lock_irqsave(&up->port.lock, flags);
931
932 up->port.type = (PORT_M32R_SIO - PORT_M32R_BASE + 1);
933 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
934
935 spin_unlock_irqrestore(&up->port.lock, flags);
936}
937
938static int
939m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
940{
941 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
942 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
943 ser->type >= ARRAY_SIZE(uart_config))
944 return -EINVAL;
945 return 0;
946}
947
948static const char *
949m32r_sio_type(struct uart_port *port)
950{
951 int type = port->type;
952
953 if (type >= ARRAY_SIZE(uart_config))
954 type = 0;
955 return uart_config[type].name;
956}
957
958static struct uart_ops m32r_sio_pops = {
959 .tx_empty = m32r_sio_tx_empty,
960 .set_mctrl = m32r_sio_set_mctrl,
961 .get_mctrl = m32r_sio_get_mctrl,
962 .stop_tx = m32r_sio_stop_tx,
963 .start_tx = m32r_sio_start_tx,
964 .stop_rx = m32r_sio_stop_rx,
965 .enable_ms = m32r_sio_enable_ms,
966 .break_ctl = m32r_sio_break_ctl,
967 .startup = m32r_sio_startup,
968 .shutdown = m32r_sio_shutdown,
969 .set_termios = m32r_sio_set_termios,
970 .pm = m32r_sio_pm,
971 .type = m32r_sio_type,
972 .release_port = m32r_sio_release_port,
973 .request_port = m32r_sio_request_port,
974 .config_port = m32r_sio_config_port,
975 .verify_port = m32r_sio_verify_port,
976};
977
978static struct uart_sio_port m32r_sio_ports[UART_NR];
979
980static void __init m32r_sio_init_ports(void)
981{
982 struct uart_sio_port *up;
983 static int first = 1;
984 int i;
985
986 if (!first)
987 return;
988 first = 0;
989
990 for (i = 0, up = m32r_sio_ports; i < ARRAY_SIZE(old_serial_port);
991 i++, up++) {
992 up->port.iobase = old_serial_port[i].port;
993 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
994 up->port.uartclk = old_serial_port[i].baud_base * 16;
995 up->port.flags = old_serial_port[i].flags;
996 up->port.membase = old_serial_port[i].iomem_base;
997 up->port.iotype = old_serial_port[i].io_type;
998 up->port.regshift = old_serial_port[i].iomem_reg_shift;
999 up->port.ops = &m32r_sio_pops;
1000 }
1001}
1002
1003static void __init m32r_sio_register_ports(struct uart_driver *drv)
1004{
1005 int i;
1006
1007 m32r_sio_init_ports();
1008
1009 for (i = 0; i < UART_NR; i++) {
1010 struct uart_sio_port *up = &m32r_sio_ports[i];
1011
1012 up->port.line = i;
1013 up->port.ops = &m32r_sio_pops;
1014 init_timer(&up->timer);
1015 up->timer.function = m32r_sio_timeout;
1016
1017 /*
1018 * ALPHA_KLUDGE_MCR needs to be killed.
1019 */
1020 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1021 up->mcr_force = ALPHA_KLUDGE_MCR;
1022
1023 uart_add_one_port(drv, &up->port);
1024 }
1025}
1026
1027#ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
1028
1029/*
1030 * Wait for transmitter & holding register to empty
1031 */
1032static inline void wait_for_xmitr(struct uart_sio_port *up)
1033{
1034 unsigned int status, tmout = 10000;
1035
1036 /* Wait up to 10ms for the character(s) to be sent. */
1037 do {
1038 status = sio_in(up, SIOSTS);
1039
1040 if (--tmout == 0)
1041 break;
1042 udelay(1);
1043 } while ((status & UART_EMPTY) != UART_EMPTY);
1044
1045 /* Wait up to 1s for flow control if necessary */
1046 if (up->port.flags & UPF_CONS_FLOW) {
1047 tmout = 1000000;
1048 while (--tmout)
1049 udelay(1);
1050 }
1051}
1052
1053/*
1054 * Print a string to the serial port trying not to disturb
1055 * any possible real use of the port...
1056 *
1057 * The console_lock must be held when we get here.
1058 */
1059static void m32r_sio_console_write(struct console *co, const char *s,
1060 unsigned int count)
1061{
1062 struct uart_sio_port *up = &m32r_sio_ports[co->index];
1063 unsigned int ier;
1064 int i;
1065
1066 /*
1067 * First save the UER then disable the interrupts
1068 */
1069 ier = sio_in(up, SIOTRCR);
1070 sio_out(up, SIOTRCR, 0);
1071
1072 /*
1073 * Now, do each character
1074 */
1075 for (i = 0; i < count; i++, s++) {
1076 wait_for_xmitr(up);
1077
1078 /*
1079 * Send the character out.
1080 * If a LF, also do CR...
1081 */
1082 sio_out(up, SIOTXB, *s);
1083
1084 if (*s == 10) {
1085 wait_for_xmitr(up);
1086 sio_out(up, SIOTXB, 13);
1087 }
1088 }
1089
1090 /*
1091 * Finally, wait for transmitter to become empty
1092 * and restore the IER
1093 */
1094 wait_for_xmitr(up);
1095 sio_out(up, SIOTRCR, ier);
1096}
1097
1098static int __init m32r_sio_console_setup(struct console *co, char *options)
1099{
1100 struct uart_port *port;
1101 int baud = 9600;
1102 int bits = 8;
1103 int parity = 'n';
1104 int flow = 'n';
1105
1106 /*
1107 * Check whether an invalid uart number has been specified, and
1108 * if so, search for the first available port that does have
1109 * console support.
1110 */
1111 if (co->index >= UART_NR)
1112 co->index = 0;
1113 port = &m32r_sio_ports[co->index].port;
1114
1115 /*
1116 * Temporary fix.
1117 */
1118 spin_lock_init(&port->lock);
1119
1120 if (options)
1121 uart_parse_options(options, &baud, &parity, &bits, &flow);
1122
1123 return uart_set_options(port, co, baud, parity, bits, flow);
1124}
1125
1126extern struct uart_driver m32r_sio_reg;
1127static struct console m32r_sio_console = {
1128 .name = "ttyS",
1129 .write = m32r_sio_console_write,
1130 .device = uart_console_device,
1131 .setup = m32r_sio_console_setup,
1132 .flags = CON_PRINTBUFFER,
1133 .index = -1,
1134 .data = &m32r_sio_reg,
1135};
1136
1137static int __init m32r_sio_console_init(void)
1138{
1139 sio_reset();
1140 sio_init();
1141 m32r_sio_init_ports();
1142 register_console(&m32r_sio_console);
1143 return 0;
1144}
1145console_initcall(m32r_sio_console_init);
1146
1147#define M32R_SIO_CONSOLE &m32r_sio_console
1148#else
1149#define M32R_SIO_CONSOLE NULL
1150#endif
1151
1152static struct uart_driver m32r_sio_reg = {
1153 .owner = THIS_MODULE,
1154 .driver_name = "sio",
1155 .devfs_name = "tts/",
1156 .dev_name = "ttyS",
1157 .major = TTY_MAJOR,
1158 .minor = 64,
1159 .nr = UART_NR,
1160 .cons = M32R_SIO_CONSOLE,
1161};
1162
1163/**
1164 * m32r_sio_suspend_port - suspend one serial port
1165 * @line: serial line number
1166 *
1167 * Suspend one serial port.
1168 */
1169void m32r_sio_suspend_port(int line)
1170{
1171 uart_suspend_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1172}
1173
1174/**
1175 * m32r_sio_resume_port - resume one serial port
1176 * @line: serial line number
1177 *
1178 * Resume one serial port.
1179 */
1180void m32r_sio_resume_port(int line)
1181{
1182 uart_resume_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1183}
1184
1185static int __init m32r_sio_init(void)
1186{
1187 int ret, i;
1188
1189 printk(KERN_INFO "Serial: M32R SIO driver $Revision: 1.11 $ ");
1190
1191 for (i = 0; i < NR_IRQS; i++)
1192 spin_lock_init(&irq_lists[i].lock);
1193
1194 ret = uart_register_driver(&m32r_sio_reg);
1195 if (ret >= 0)
1196 m32r_sio_register_ports(&m32r_sio_reg);
1197
1198 return ret;
1199}
1200
1201static void __exit m32r_sio_exit(void)
1202{
1203 int i;
1204
1205 for (i = 0; i < UART_NR; i++)
1206 uart_remove_one_port(&m32r_sio_reg, &m32r_sio_ports[i].port);
1207
1208 uart_unregister_driver(&m32r_sio_reg);
1209}
1210
1211module_init(m32r_sio_init);
1212module_exit(m32r_sio_exit);
1213
1214EXPORT_SYMBOL(m32r_sio_suspend_port);
1215EXPORT_SYMBOL(m32r_sio_resume_port);
1216
1217MODULE_LICENSE("GPL");
1218MODULE_DESCRIPTION("Generic M32R SIO serial driver $Revision: 1.11 $");
diff --git a/drivers/serial/m32r_sio.h b/drivers/serial/m32r_sio.h
new file mode 100644
index 000000000000..07d0dd80aa3d
--- /dev/null
+++ b/drivers/serial/m32r_sio.h
@@ -0,0 +1,55 @@
1/*
2 * m32r_sio.h
3 *
4 * Driver for M32R serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Based on drivers/serial/8250.h.
8 *
9 * Copyright (C) 2001 Russell King.
10 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18#include <linux/config.h>
19
20struct m32r_sio_probe {
21 struct module *owner;
22 int (*pci_init_one)(struct pci_dev *dev);
23 void (*pci_remove_one)(struct pci_dev *dev);
24 void (*pnp_init)(void);
25};
26
27int m32r_sio_register_probe(struct m32r_sio_probe *probe);
28void m32r_sio_unregister_probe(struct m32r_sio_probe *probe);
29void m32r_sio_get_irq_map(unsigned int *map);
30void m32r_sio_suspend_port(int line);
31void m32r_sio_resume_port(int line);
32
33struct old_serial_port {
34 unsigned int uart;
35 unsigned int baud_base;
36 unsigned int port;
37 unsigned int irq;
38 unsigned int flags;
39 unsigned char io_type;
40 unsigned char *iomem_base;
41 unsigned short iomem_reg_shift;
42};
43
44#define _INLINE_ inline
45
46#define PROBE_RSA (1 << 0)
47#define PROBE_ANY (~0)
48
49#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
50
51#ifdef CONFIG_SERIAL_SIO_SHARE_IRQ
52#define M32R_SIO_SHARE_IRQS 1
53#else
54#define M32R_SIO_SHARE_IRQS 0
55#endif
diff --git a/drivers/serial/m32r_sio_reg.h b/drivers/serial/m32r_sio_reg.h
new file mode 100644
index 000000000000..9c864529451b
--- /dev/null
+++ b/drivers/serial/m32r_sio_reg.h
@@ -0,0 +1,153 @@
1/*
2 * m32r_sio_reg.h
3 *
4 * Copyright (C) 1992, 1994 by Theodore Ts'o.
5 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
6 *
7 * Redistribution of this file is permitted under the terms of the GNU
8 * Public License (GPL)
9 *
10 * These are the UART port assignments, expressed as offsets from the base
11 * register. These assignments should hold for any serial port based on
12 * a 8250, 16450, or 16550(A).
13 */
14
15#ifndef _M32R_SIO_REG_H
16#define _M32R_SIO_REG_H
17
18#include <linux/config.h>
19
20#ifdef CONFIG_SERIAL_M32R_PLDSIO
21
22#define SIOCR 0x000
23#define SIOMOD0 0x002
24#define SIOMOD1 0x004
25#define SIOSTS 0x006
26#define SIOTRCR 0x008
27#define SIOBAUR 0x00a
28// #define SIORBAUR 0x018
29#define SIOTXB 0x00c
30#define SIORXB 0x00e
31
32#define UART_RX ((unsigned long) PLD_ESIO0RXB)
33 /* In: Receive buffer (DLAB=0) */
34#define UART_TX ((unsigned long) PLD_ESIO0TXB)
35 /* Out: Transmit buffer (DLAB=0) */
36#define UART_DLL 0 /* Out: Divisor Latch Low (DLAB=1) */
37#define UART_TRG 0 /* (LCR=BF) FCTR bit 7 selects Rx or Tx
38 * In: Fifo count
39 * Out: Fifo custom trigger levels
40 * XR16C85x only */
41
42#define UART_DLM 0 /* Out: Divisor Latch High (DLAB=1) */
43#define UART_IER ((unsigned long) PLD_ESIO0INTCR)
44 /* Out: Interrupt Enable Register */
45#define UART_FCTR 0 /* (LCR=BF) Feature Control Register
46 * XR16C85x only */
47
48#define UART_IIR 0 /* In: Interrupt ID Register */
49#define UART_FCR 0 /* Out: FIFO Control Register */
50#define UART_EFR 0 /* I/O: Extended Features Register */
51 /* (DLAB=1, 16C660 only) */
52
53#define UART_LCR 0 /* Out: Line Control Register */
54#define UART_MCR 0 /* Out: Modem Control Register */
55#define UART_LSR ((unsigned long) PLD_ESIO0STS)
56 /* In: Line Status Register */
57#define UART_MSR 0 /* In: Modem Status Register */
58#define UART_SCR 0 /* I/O: Scratch Register */
59#define UART_EMSR 0 /* (LCR=BF) Extended Mode Select Register
60 * FCTR bit 6 selects SCR or EMSR
61 * XR16c85x only */
62
63#else /* not CONFIG_SERIAL_M32R_PLDSIO */
64
65#define SIOCR 0x000
66#define SIOMOD0 0x004
67#define SIOMOD1 0x008
68#define SIOSTS 0x00c
69#define SIOTRCR 0x010
70#define SIOBAUR 0x014
71#define SIORBAUR 0x018
72#define SIOTXB 0x01c
73#define SIORXB 0x020
74
75#define UART_RX M32R_SIO0_RXB_PORTL /* In: Receive buffer (DLAB=0) */
76#define UART_TX M32R_SIO0_TXB_PORTL /* Out: Transmit buffer (DLAB=0) */
77#define UART_DLL 0 /* Out: Divisor Latch Low (DLAB=1) */
78#define UART_TRG 0 /* (LCR=BF) FCTR bit 7 selects Rx or Tx
79 * In: Fifo count
80 * Out: Fifo custom trigger levels
81 * XR16C85x only */
82
83#define UART_DLM 0 /* Out: Divisor Latch High (DLAB=1) */
84#define UART_IER M32R_SIO0_TRCR_PORTL /* Out: Interrupt Enable Register */
85#define UART_FCTR 0 /* (LCR=BF) Feature Control Register
86 * XR16C85x only */
87
88#define UART_IIR 0 /* In: Interrupt ID Register */
89#define UART_FCR 0 /* Out: FIFO Control Register */
90#define UART_EFR 0 /* I/O: Extended Features Register */
91 /* (DLAB=1, 16C660 only) */
92
93#define UART_LCR 0 /* Out: Line Control Register */
94#define UART_MCR 0 /* Out: Modem Control Register */
95#define UART_LSR M32R_SIO0_STS_PORTL /* In: Line Status Register */
96#define UART_MSR 0 /* In: Modem Status Register */
97#define UART_SCR 0 /* I/O: Scratch Register */
98#define UART_EMSR 0 /* (LCR=BF) Extended Mode Select Register
99 * FCTR bit 6 selects SCR or EMSR
100 * XR16c85x only */
101
102#endif /* CONFIG_SERIAL_M32R_PLDSIO */
103
104#define UART_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
105
106/*
107 * These are the definitions for the Line Control Register
108 *
109 * Note: if the word length is 5 bits (UART_LCR_WLEN5), then setting
110 * UART_LCR_STOP will select 1.5 stop bits, not 2 stop bits.
111 */
112#define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
113#define UART_LCR_SBC 0x40 /* Set break control */
114#define UART_LCR_SPAR 0x20 /* Stick parity (?) */
115#define UART_LCR_EPAR 0x10 /* Even parity select */
116#define UART_LCR_PARITY 0x08 /* Parity Enable */
117#define UART_LCR_STOP 0x04 /* Stop bits: 0=1 stop bit, 1= 2 stop bits */
118#define UART_LCR_WLEN5 0x00 /* Wordlength: 5 bits */
119#define UART_LCR_WLEN6 0x01 /* Wordlength: 6 bits */
120#define UART_LCR_WLEN7 0x02 /* Wordlength: 7 bits */
121#define UART_LCR_WLEN8 0x03 /* Wordlength: 8 bits */
122
123/*
124 * These are the definitions for the Line Status Register
125 */
126#define UART_LSR_TEMT 0x02 /* Transmitter empty */
127#define UART_LSR_THRE 0x01 /* Transmit-hold-register empty */
128#define UART_LSR_BI 0x00 /* Break interrupt indicator */
129#define UART_LSR_FE 0x80 /* Frame error indicator */
130#define UART_LSR_PE 0x40 /* Parity error indicator */
131#define UART_LSR_OE 0x20 /* Overrun error indicator */
132#define UART_LSR_DR 0x04 /* Receiver data ready */
133
134/*
135 * These are the definitions for the Interrupt Identification Register
136 */
137#define UART_IIR_NO_INT 0x01 /* No interrupts pending */
138#define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
139
140#define UART_IIR_MSI 0x00 /* Modem status interrupt */
141#define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
142#define UART_IIR_RDI 0x04 /* Receiver data interrupt */
143#define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
144
145/*
146 * These are the definitions for the Interrupt Enable Register
147 */
148#define UART_IER_MSI 0x00 /* Enable Modem status interrupt */
149#define UART_IER_RLSI 0x08 /* Enable receiver line status interrupt */
150#define UART_IER_THRI 0x03 /* Enable Transmitter holding register int. */
151#define UART_IER_RDI 0x04 /* Enable receiver data interrupt */
152
153#endif /* _M32R_SIO_REG_H */
diff --git a/drivers/serial/mcfserial.c b/drivers/serial/mcfserial.c
new file mode 100644
index 000000000000..8c40167778de
--- /dev/null
+++ b/drivers/serial/mcfserial.c
@@ -0,0 +1,1883 @@
1/*
2 * mcfserial.c -- serial driver for ColdFire internal UARTS.
3 *
4 * Copyright (C) 1999-2003 Greg Ungerer <gerg@snapgear.com>
5 * Copyright (c) 2000-2001 Lineo, Inc. <www.lineo.com>
6 * Copyright (C) 2001-2002 SnapGear Inc. <www.snapgear.com>
7 *
8 * Based on code from 68332serial.c which was:
9 *
10 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
11 * Copyright (C) 1998 TSHG
12 * Copyright (c) 1999 Rt-Control Inc. <jeff@uclinux.org>
13 *
14 * Changes:
15 * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
16 * some cleanups in mcfrs_write.
17 *
18 */
19
20#include <linux/module.h>
21#include <linux/errno.h>
22#include <linux/signal.h>
23#include <linux/sched.h>
24#include <linux/timer.h>
25#include <linux/wait.h>
26#include <linux/interrupt.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/string.h>
30#include <linux/fcntl.h>
31#include <linux/mm.h>
32#include <linux/kernel.h>
33#include <linux/serial.h>
34#include <linux/serialP.h>
35#include <linux/console.h>
36#include <linux/init.h>
37#include <linux/bitops.h>
38#include <linux/delay.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/system.h>
43#include <asm/segment.h>
44#include <asm/semaphore.h>
45#include <asm/delay.h>
46#include <asm/coldfire.h>
47#include <asm/mcfsim.h>
48#include <asm/mcfuart.h>
49#include <asm/nettel.h>
50#include <asm/uaccess.h>
51#include "mcfserial.h"
52
53struct timer_list mcfrs_timer_struct;
54
55/*
56 * Default console baud rate, we use this as the default
57 * for all ports so init can just open /dev/console and
58 * keep going. Perhaps one day the cflag settings for the
59 * console can be used instead.
60 */
61#if defined(CONFIG_ARNEWSH) || defined(CONFIG_MOTOROLA) || defined(CONFIG_senTec) || defined(CONFIG_SNEHA)
62#define CONSOLE_BAUD_RATE 19200
63#define DEFAULT_CBAUD B19200
64#endif
65
66#if defined(CONFIG_HW_FEITH)
67 #define CONSOLE_BAUD_RATE 38400
68 #define DEFAULT_CBAUD B38400
69#endif
70
71#ifndef CONSOLE_BAUD_RATE
72#define CONSOLE_BAUD_RATE 9600
73#define DEFAULT_CBAUD B9600
74#endif
75
76int mcfrs_console_inited = 0;
77int mcfrs_console_port = -1;
78int mcfrs_console_baud = CONSOLE_BAUD_RATE;
79int mcfrs_console_cbaud = DEFAULT_CBAUD;
80
81/*
82 * Driver data structures.
83 */
84static struct tty_driver *mcfrs_serial_driver;
85
86/* number of characters left in xmit buffer before we ask for more */
87#define WAKEUP_CHARS 256
88
89/* Debugging...
90 */
91#undef SERIAL_DEBUG_OPEN
92#undef SERIAL_DEBUG_FLOW
93
94#if defined(CONFIG_M527x) || defined(CONFIG_M528x)
95#define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
96#else
97#define IRQBASE 73
98#endif
99
100/*
101 * Configuration table, UARTs to look for at startup.
102 */
103static struct mcf_serial mcfrs_table[] = {
104 { /* ttyS0 */
105 .magic = 0,
106 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
107 .irq = IRQBASE,
108 .flags = ASYNC_BOOT_AUTOCONF,
109 },
110 { /* ttyS1 */
111 .magic = 0,
112 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
113 .irq = IRQBASE+1,
114 .flags = ASYNC_BOOT_AUTOCONF,
115 },
116};
117
118
119#define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
120
121/*
122 * This is used to figure out the divisor speeds and the timeouts.
123 */
124static int mcfrs_baud_table[] = {
125 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
126 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
127};
128#define MCFRS_BAUD_TABLE_SIZE \
129 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
130
131
132#ifdef CONFIG_MAGIC_SYSRQ
133/*
134 * Magic system request keys. Used for debugging...
135 */
136extern int magic_sysrq_key(int ch);
137#endif
138
139
140/*
141 * Forware declarations...
142 */
143static void mcfrs_change_speed(struct mcf_serial *info);
144static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
145
146
147static inline int serial_paranoia_check(struct mcf_serial *info,
148 char *name, const char *routine)
149{
150#ifdef SERIAL_PARANOIA_CHECK
151 static const char badmagic[] =
152 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
153 static const char badinfo[] =
154 "MCFRS(warning): null mcf_serial for %s in %s\n";
155
156 if (!info) {
157 printk(badinfo, name, routine);
158 return 1;
159 }
160 if (info->magic != SERIAL_MAGIC) {
161 printk(badmagic, name, routine);
162 return 1;
163 }
164#endif
165 return 0;
166}
167
168/*
169 * Sets or clears DTR and RTS on the requested line.
170 */
171static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
172{
173 volatile unsigned char *uartp;
174 unsigned long flags;
175
176#if 0
177 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
178 __FILE__, __LINE__, info, dtr, rts);
179#endif
180
181 local_irq_save(flags);
182 if (dtr >= 0) {
183#ifdef MCFPP_DTR0
184 if (info->line)
185 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
186 else
187 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
188#endif
189 }
190 if (rts >= 0) {
191 uartp = info->addr;
192 if (rts) {
193 info->sigs |= TIOCM_RTS;
194 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
195 } else {
196 info->sigs &= ~TIOCM_RTS;
197 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
198 }
199 }
200 local_irq_restore(flags);
201 return;
202}
203
204/*
205 * Gets values of serial signals.
206 */
207static int mcfrs_getsignals(struct mcf_serial *info)
208{
209 volatile unsigned char *uartp;
210 unsigned long flags;
211 int sigs;
212#if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
213 unsigned short ppdata;
214#endif
215
216#if 0
217 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
218#endif
219
220 local_irq_save(flags);
221 uartp = info->addr;
222 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
223 sigs |= (info->sigs & TIOCM_RTS);
224
225#ifdef MCFPP_DCD0
226{
227 unsigned int ppdata;
228 ppdata = mcf_getppdata();
229 if (info->line == 0) {
230 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
231 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
232 } else if (info->line == 1) {
233 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
234 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
235 }
236}
237#endif
238
239 local_irq_restore(flags);
240 return(sigs);
241}
242
243/*
244 * ------------------------------------------------------------
245 * mcfrs_stop() and mcfrs_start()
246 *
247 * This routines are called before setting or resetting tty->stopped.
248 * They enable or disable transmitter interrupts, as necessary.
249 * ------------------------------------------------------------
250 */
251static void mcfrs_stop(struct tty_struct *tty)
252{
253 volatile unsigned char *uartp;
254 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
255 unsigned long flags;
256
257 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
258 return;
259
260 local_irq_save(flags);
261 uartp = info->addr;
262 info->imr &= ~MCFUART_UIR_TXREADY;
263 uartp[MCFUART_UIMR] = info->imr;
264 local_irq_restore(flags);
265}
266
267static void mcfrs_start(struct tty_struct *tty)
268{
269 volatile unsigned char *uartp;
270 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
271 unsigned long flags;
272
273 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
274 return;
275
276 local_irq_save(flags);
277 if (info->xmit_cnt && info->xmit_buf) {
278 uartp = info->addr;
279 info->imr |= MCFUART_UIR_TXREADY;
280 uartp[MCFUART_UIMR] = info->imr;
281 }
282 local_irq_restore(flags);
283}
284
285/*
286 * ----------------------------------------------------------------------
287 *
288 * Here starts the interrupt handling routines. All of the following
289 * subroutines are declared as inline and are folded into
290 * mcfrs_interrupt(). They were separated out for readability's sake.
291 *
292 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
293 * runs with interrupts turned off. People who may want to modify
294 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
295 * possible. After you are done making modifications, it is not a bad
296 * idea to do:
297 *
298 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
299 *
300 * and look at the resulting assemble code in serial.s.
301 *
302 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
303 * -----------------------------------------------------------------------
304 */
305
306static inline void receive_chars(struct mcf_serial *info)
307{
308 volatile unsigned char *uartp;
309 struct tty_struct *tty = info->tty;
310 unsigned char status, ch;
311
312 if (!tty)
313 return;
314
315 uartp = info->addr;
316
317 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
318
319 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
320 break;
321
322 ch = uartp[MCFUART_URB];
323 info->stats.rx++;
324
325#ifdef CONFIG_MAGIC_SYSRQ
326 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
327 if (magic_sysrq_key(ch))
328 continue;
329 }
330#endif
331
332 tty->flip.count++;
333 if (status & MCFUART_USR_RXERR) {
334 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
335 if (status & MCFUART_USR_RXBREAK) {
336 info->stats.rxbreak++;
337 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
338 } else if (status & MCFUART_USR_RXPARITY) {
339 info->stats.rxparity++;
340 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
341 } else if (status & MCFUART_USR_RXOVERRUN) {
342 info->stats.rxoverrun++;
343 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
344 } else if (status & MCFUART_USR_RXFRAMING) {
345 info->stats.rxframing++;
346 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
347 } else {
348 /* This should never happen... */
349 *tty->flip.flag_buf_ptr++ = 0;
350 }
351 } else {
352 *tty->flip.flag_buf_ptr++ = 0;
353 }
354 *tty->flip.char_buf_ptr++ = ch;
355 }
356
357 schedule_work(&tty->flip.work);
358 return;
359}
360
361static inline void transmit_chars(struct mcf_serial *info)
362{
363 volatile unsigned char *uartp;
364
365 uartp = info->addr;
366
367 if (info->x_char) {
368 /* Send special char - probably flow control */
369 uartp[MCFUART_UTB] = info->x_char;
370 info->x_char = 0;
371 info->stats.tx++;
372 }
373
374 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
375 info->imr &= ~MCFUART_UIR_TXREADY;
376 uartp[MCFUART_UIMR] = info->imr;
377 return;
378 }
379
380 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
381 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
382 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
383 info->stats.tx++;
384 if (--info->xmit_cnt <= 0)
385 break;
386 }
387
388 if (info->xmit_cnt < WAKEUP_CHARS)
389 schedule_work(&info->tqueue);
390 return;
391}
392
393/*
394 * This is the serial driver's generic interrupt routine
395 */
396irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
397{
398 struct mcf_serial *info;
399 unsigned char isr;
400
401 info = &mcfrs_table[(irq - IRQBASE)];
402 isr = info->addr[MCFUART_UISR] & info->imr;
403
404 if (isr & MCFUART_UIR_RXREADY)
405 receive_chars(info);
406 if (isr & MCFUART_UIR_TXREADY)
407 transmit_chars(info);
408 return IRQ_HANDLED;
409}
410
411/*
412 * -------------------------------------------------------------------
413 * Here ends the serial interrupt routines.
414 * -------------------------------------------------------------------
415 */
416
417static void mcfrs_offintr(void *private)
418{
419 struct mcf_serial *info = (struct mcf_serial *) private;
420 struct tty_struct *tty;
421
422 tty = info->tty;
423 if (!tty)
424 return;
425 tty_wakeup(tty);
426}
427
428
429/*
430 * Change of state on a DCD line.
431 */
432void mcfrs_modem_change(struct mcf_serial *info, int dcd)
433{
434 if (info->count == 0)
435 return;
436
437 if (info->flags & ASYNC_CHECK_CD) {
438 if (dcd)
439 wake_up_interruptible(&info->open_wait);
440 else
441 schedule_work(&info->tqueue_hangup);
442 }
443}
444
445
446#ifdef MCFPP_DCD0
447
448unsigned short mcfrs_ppstatus;
449
450/*
451 * This subroutine is called when the RS_TIMER goes off. It is used
452 * to monitor the state of the DCD lines - since they have no edge
453 * sensors and interrupt generators.
454 */
455static void mcfrs_timer(void)
456{
457 unsigned int ppstatus, dcdval, i;
458
459 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
460
461 if (ppstatus != mcfrs_ppstatus) {
462 for (i = 0; (i < 2); i++) {
463 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
464 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
465 mcfrs_modem_change(&mcfrs_table[i],
466 ((ppstatus & dcdval) ? 0 : 1));
467 }
468 }
469 }
470 mcfrs_ppstatus = ppstatus;
471
472 /* Re-arm timer */
473 mcfrs_timer_struct.expires = jiffies + HZ/25;
474 add_timer(&mcfrs_timer_struct);
475}
476
477#endif /* MCFPP_DCD0 */
478
479
480/*
481 * This routine is called from the scheduler tqueue when the interrupt
482 * routine has signalled that a hangup has occurred. The path of
483 * hangup processing is:
484 *
485 * serial interrupt routine -> (scheduler tqueue) ->
486 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
487 *
488 */
489static void do_serial_hangup(void *private)
490{
491 struct mcf_serial *info = (struct mcf_serial *) private;
492 struct tty_struct *tty;
493
494 tty = info->tty;
495 if (!tty)
496 return;
497
498 tty_hangup(tty);
499}
500
501static int startup(struct mcf_serial * info)
502{
503 volatile unsigned char *uartp;
504 unsigned long flags;
505
506 if (info->flags & ASYNC_INITIALIZED)
507 return 0;
508
509 if (!info->xmit_buf) {
510 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
511 if (!info->xmit_buf)
512 return -ENOMEM;
513 }
514
515 local_irq_save(flags);
516
517#ifdef SERIAL_DEBUG_OPEN
518 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
519#endif
520
521 /*
522 * Reset UART, get it into known state...
523 */
524 uartp = info->addr;
525 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
526 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
527 mcfrs_setsignals(info, 1, 1);
528
529 if (info->tty)
530 clear_bit(TTY_IO_ERROR, &info->tty->flags);
531 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
532
533 /*
534 * and set the speed of the serial port
535 */
536 mcfrs_change_speed(info);
537
538 /*
539 * Lastly enable the UART transmitter and receiver, and
540 * interrupt enables.
541 */
542 info->imr = MCFUART_UIR_RXREADY;
543 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
544 uartp[MCFUART_UIMR] = info->imr;
545
546 info->flags |= ASYNC_INITIALIZED;
547 local_irq_restore(flags);
548 return 0;
549}
550
551/*
552 * This routine will shutdown a serial port; interrupts are disabled, and
553 * DTR is dropped if the hangup on close termio flag is on.
554 */
555static void shutdown(struct mcf_serial * info)
556{
557 volatile unsigned char *uartp;
558 unsigned long flags;
559
560 if (!(info->flags & ASYNC_INITIALIZED))
561 return;
562
563#ifdef SERIAL_DEBUG_OPEN
564 printk("Shutting down serial port %d (irq %d)....\n", info->line,
565 info->irq);
566#endif
567
568 local_irq_save(flags);
569
570 uartp = info->addr;
571 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
572 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
573 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
574
575 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
576 mcfrs_setsignals(info, 0, 0);
577
578 if (info->xmit_buf) {
579 free_page((unsigned long) info->xmit_buf);
580 info->xmit_buf = 0;
581 }
582
583 if (info->tty)
584 set_bit(TTY_IO_ERROR, &info->tty->flags);
585
586 info->flags &= ~ASYNC_INITIALIZED;
587 local_irq_restore(flags);
588}
589
590
591/*
592 * This routine is called to set the UART divisor registers to match
593 * the specified baud rate for a serial port.
594 */
595static void mcfrs_change_speed(struct mcf_serial *info)
596{
597 volatile unsigned char *uartp;
598 unsigned int baudclk, cflag;
599 unsigned long flags;
600 unsigned char mr1, mr2;
601 int i;
602#ifdef CONFIG_M5272
603 unsigned int fraction;
604#endif
605
606 if (!info->tty || !info->tty->termios)
607 return;
608 cflag = info->tty->termios->c_cflag;
609 if (info->addr == 0)
610 return;
611
612#if 0
613 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
614#endif
615
616 i = cflag & CBAUD;
617 if (i & CBAUDEX) {
618 i &= ~CBAUDEX;
619 if (i < 1 || i > 4)
620 info->tty->termios->c_cflag &= ~CBAUDEX;
621 else
622 i += 15;
623 }
624 if (i == 0) {
625 mcfrs_setsignals(info, 0, -1);
626 return;
627 }
628
629 /* compute the baudrate clock */
630#ifdef CONFIG_M5272
631 /*
632 * For the MCF5272, also compute the baudrate fraction.
633 */
634 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
635 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
636 fraction *= 16;
637 fraction /= (32 * mcfrs_baud_table[i]);
638#else
639 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
640#endif
641
642 info->baud = mcfrs_baud_table[i];
643
644 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
645 mr2 = 0;
646
647 switch (cflag & CSIZE) {
648 case CS5: mr1 |= MCFUART_MR1_CS5; break;
649 case CS6: mr1 |= MCFUART_MR1_CS6; break;
650 case CS7: mr1 |= MCFUART_MR1_CS7; break;
651 case CS8:
652 default: mr1 |= MCFUART_MR1_CS8; break;
653 }
654
655 if (cflag & PARENB) {
656 if (cflag & CMSPAR) {
657 if (cflag & PARODD)
658 mr1 |= MCFUART_MR1_PARITYMARK;
659 else
660 mr1 |= MCFUART_MR1_PARITYSPACE;
661 } else {
662 if (cflag & PARODD)
663 mr1 |= MCFUART_MR1_PARITYODD;
664 else
665 mr1 |= MCFUART_MR1_PARITYEVEN;
666 }
667 } else {
668 mr1 |= MCFUART_MR1_PARITYNONE;
669 }
670
671 if (cflag & CSTOPB)
672 mr2 |= MCFUART_MR2_STOP2;
673 else
674 mr2 |= MCFUART_MR2_STOP1;
675
676 if (cflag & CRTSCTS) {
677 mr1 |= MCFUART_MR1_RXRTS;
678 mr2 |= MCFUART_MR2_TXCTS;
679 }
680
681 if (cflag & CLOCAL)
682 info->flags &= ~ASYNC_CHECK_CD;
683 else
684 info->flags |= ASYNC_CHECK_CD;
685
686 uartp = info->addr;
687
688 local_irq_save(flags);
689#if 0
690 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
691 mr1, mr2, baudclk);
692#endif
693 /*
694 Note: pg 12-16 of MCF5206e User's Manual states that a
695 software reset should be performed prior to changing
696 UMR1,2, UCSR, UACR, bit 7
697 */
698 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
699 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
700 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
701 uartp[MCFUART_UMR] = mr1;
702 uartp[MCFUART_UMR] = mr2;
703 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
704 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
705#ifdef CONFIG_M5272
706 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
707#endif
708 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
709 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
710 mcfrs_setsignals(info, 1, -1);
711 local_irq_restore(flags);
712 return;
713}
714
715static void mcfrs_flush_chars(struct tty_struct *tty)
716{
717 volatile unsigned char *uartp;
718 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
719 unsigned long flags;
720
721 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
722 return;
723
724 uartp = (volatile unsigned char *) info->addr;
725
726 /*
727 * re-enable receiver interrupt
728 */
729 local_irq_save(flags);
730 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
731 (info->flags & ASYNC_INITIALIZED) ) {
732 info->imr |= MCFUART_UIR_RXREADY;
733 uartp[MCFUART_UIMR] = info->imr;
734 }
735 local_irq_restore(flags);
736
737 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
738 !info->xmit_buf)
739 return;
740
741 /* Enable transmitter */
742 local_irq_save(flags);
743 info->imr |= MCFUART_UIR_TXREADY;
744 uartp[MCFUART_UIMR] = info->imr;
745 local_irq_restore(flags);
746}
747
748static int mcfrs_write(struct tty_struct * tty,
749 const unsigned char *buf, int count)
750{
751 volatile unsigned char *uartp;
752 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
753 unsigned long flags;
754 int c, total = 0;
755
756#if 0
757 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
758 __FILE__, __LINE__, (int)tty, (int)buf, count);
759#endif
760
761 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
762 return 0;
763
764 if (!tty || !info->xmit_buf)
765 return 0;
766
767 local_save_flags(flags);
768 while (1) {
769 local_irq_disable();
770 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
771 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
772 local_irq_restore(flags);
773
774 if (c <= 0)
775 break;
776
777 memcpy(info->xmit_buf + info->xmit_head, buf, c);
778
779 local_irq_disable();
780 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
781 info->xmit_cnt += c;
782 local_irq_restore(flags);
783
784 buf += c;
785 count -= c;
786 total += c;
787 }
788
789 local_irq_disable();
790 uartp = info->addr;
791 info->imr |= MCFUART_UIR_TXREADY;
792 uartp[MCFUART_UIMR] = info->imr;
793 local_irq_restore(flags);
794
795 return total;
796}
797
798static int mcfrs_write_room(struct tty_struct *tty)
799{
800 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
801 int ret;
802
803 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
804 return 0;
805 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
806 if (ret < 0)
807 ret = 0;
808 return ret;
809}
810
811static int mcfrs_chars_in_buffer(struct tty_struct *tty)
812{
813 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
814
815 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
816 return 0;
817 return info->xmit_cnt;
818}
819
820static void mcfrs_flush_buffer(struct tty_struct *tty)
821{
822 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
823 unsigned long flags;
824
825 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
826 return;
827
828 local_irq_save(flags);
829 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
830 local_irq_restore(flags);
831
832 tty_wakeup(tty);
833}
834
835/*
836 * ------------------------------------------------------------
837 * mcfrs_throttle()
838 *
839 * This routine is called by the upper-layer tty layer to signal that
840 * incoming characters should be throttled.
841 * ------------------------------------------------------------
842 */
843static void mcfrs_throttle(struct tty_struct * tty)
844{
845 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
846#ifdef SERIAL_DEBUG_THROTTLE
847 char buf[64];
848
849 printk("throttle %s: %d....\n", _tty_name(tty, buf),
850 tty->ldisc.chars_in_buffer(tty));
851#endif
852
853 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
854 return;
855
856 if (I_IXOFF(tty))
857 info->x_char = STOP_CHAR(tty);
858
859 /* Turn off RTS line (do this atomic) */
860}
861
862static void mcfrs_unthrottle(struct tty_struct * tty)
863{
864 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
865#ifdef SERIAL_DEBUG_THROTTLE
866 char buf[64];
867
868 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
869 tty->ldisc.chars_in_buffer(tty));
870#endif
871
872 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
873 return;
874
875 if (I_IXOFF(tty)) {
876 if (info->x_char)
877 info->x_char = 0;
878 else
879 info->x_char = START_CHAR(tty);
880 }
881
882 /* Assert RTS line (do this atomic) */
883}
884
885/*
886 * ------------------------------------------------------------
887 * mcfrs_ioctl() and friends
888 * ------------------------------------------------------------
889 */
890
891static int get_serial_info(struct mcf_serial * info,
892 struct serial_struct * retinfo)
893{
894 struct serial_struct tmp;
895
896 if (!retinfo)
897 return -EFAULT;
898 memset(&tmp, 0, sizeof(tmp));
899 tmp.type = info->type;
900 tmp.line = info->line;
901 tmp.port = (unsigned int) info->addr;
902 tmp.irq = info->irq;
903 tmp.flags = info->flags;
904 tmp.baud_base = info->baud_base;
905 tmp.close_delay = info->close_delay;
906 tmp.closing_wait = info->closing_wait;
907 tmp.custom_divisor = info->custom_divisor;
908 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
909}
910
911static int set_serial_info(struct mcf_serial * info,
912 struct serial_struct * new_info)
913{
914 struct serial_struct new_serial;
915 struct mcf_serial old_info;
916 int retval = 0;
917
918 if (!new_info)
919 return -EFAULT;
920 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
921 return -EFAULT;
922 old_info = *info;
923
924 if (!capable(CAP_SYS_ADMIN)) {
925 if ((new_serial.baud_base != info->baud_base) ||
926 (new_serial.type != info->type) ||
927 (new_serial.close_delay != info->close_delay) ||
928 ((new_serial.flags & ~ASYNC_USR_MASK) !=
929 (info->flags & ~ASYNC_USR_MASK)))
930 return -EPERM;
931 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
932 (new_serial.flags & ASYNC_USR_MASK));
933 info->custom_divisor = new_serial.custom_divisor;
934 goto check_and_exit;
935 }
936
937 if (info->count > 1)
938 return -EBUSY;
939
940 /*
941 * OK, past this point, all the error checking has been done.
942 * At this point, we start making changes.....
943 */
944
945 info->baud_base = new_serial.baud_base;
946 info->flags = ((info->flags & ~ASYNC_FLAGS) |
947 (new_serial.flags & ASYNC_FLAGS));
948 info->type = new_serial.type;
949 info->close_delay = new_serial.close_delay;
950 info->closing_wait = new_serial.closing_wait;
951
952check_and_exit:
953 retval = startup(info);
954 return retval;
955}
956
957/*
958 * get_lsr_info - get line status register info
959 *
960 * Purpose: Let user call ioctl() to get info when the UART physically
961 * is emptied. On bus types like RS485, the transmitter must
962 * release the bus after transmitting. This must be done when
963 * the transmit shift register is empty, not be done when the
964 * transmit holding register is empty. This functionality
965 * allows an RS485 driver to be written in user space.
966 */
967static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
968{
969 volatile unsigned char *uartp;
970 unsigned long flags;
971 unsigned char status;
972
973 local_irq_save(flags);
974 uartp = info->addr;
975 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
976 local_irq_restore(flags);
977
978 return put_user(status,value);
979}
980
981/*
982 * This routine sends a break character out the serial port.
983 */
984static void send_break( struct mcf_serial * info, int duration)
985{
986 volatile unsigned char *uartp;
987 unsigned long flags;
988
989 if (!info->addr)
990 return;
991 set_current_state(TASK_INTERRUPTIBLE);
992 uartp = info->addr;
993
994 local_irq_save(flags);
995 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
996 schedule_timeout(duration);
997 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
998 local_irq_restore(flags);
999}
1000
1001static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1002{
1003 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1004
1005 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1006 return -ENODEV;
1007 if (tty->flags & (1 << TTY_IO_ERROR))
1008 return -EIO;
1009
1010 return mcfrs_getsignals(info);
1011}
1012
1013static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1014 unsigned int set, unsigned int clear)
1015{
1016 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1017 int rts = -1, dtr = -1;
1018
1019 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1020 return -ENODEV;
1021 if (tty->flags & (1 << TTY_IO_ERROR))
1022 return -EIO;
1023
1024 if (set & TIOCM_RTS)
1025 rts = 1;
1026 if (set & TIOCM_DTR)
1027 dtr = 1;
1028 if (clear & TIOCM_RTS)
1029 rts = 0;
1030 if (clear & TIOCM_DTR)
1031 dtr = 0;
1032
1033 mcfrs_setsignals(info, dtr, rts);
1034
1035 return 0;
1036}
1037
1038static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1039 unsigned int cmd, unsigned long arg)
1040{
1041 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1042 int retval, error;
1043
1044 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1045 return -ENODEV;
1046
1047 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1048 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1049 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1050 if (tty->flags & (1 << TTY_IO_ERROR))
1051 return -EIO;
1052 }
1053
1054 switch (cmd) {
1055 case TCSBRK: /* SVID version: non-zero arg --> no break */
1056 retval = tty_check_change(tty);
1057 if (retval)
1058 return retval;
1059 tty_wait_until_sent(tty, 0);
1060 if (!arg)
1061 send_break(info, HZ/4); /* 1/4 second */
1062 return 0;
1063 case TCSBRKP: /* support for POSIX tcsendbreak() */
1064 retval = tty_check_change(tty);
1065 if (retval)
1066 return retval;
1067 tty_wait_until_sent(tty, 0);
1068 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1069 return 0;
1070 case TIOCGSOFTCAR:
1071 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1072 (unsigned long *) arg);
1073 if (error)
1074 return error;
1075 return 0;
1076 case TIOCSSOFTCAR:
1077 get_user(arg, (unsigned long *) arg);
1078 tty->termios->c_cflag =
1079 ((tty->termios->c_cflag & ~CLOCAL) |
1080 (arg ? CLOCAL : 0));
1081 return 0;
1082 case TIOCGSERIAL:
1083 if (access_ok(VERIFY_WRITE, (void *) arg,
1084 sizeof(struct serial_struct)))
1085 return get_serial_info(info,
1086 (struct serial_struct *) arg);
1087 return -EFAULT;
1088 case TIOCSSERIAL:
1089 return set_serial_info(info,
1090 (struct serial_struct *) arg);
1091 case TIOCSERGETLSR: /* Get line status register */
1092 if (access_ok(VERIFY_WRITE, (void *) arg,
1093 sizeof(unsigned int)))
1094 return get_lsr_info(info, (unsigned int *) arg);
1095 return -EFAULT;
1096 case TIOCSERGSTRUCT:
1097 error = copy_to_user((struct mcf_serial *) arg,
1098 info, sizeof(struct mcf_serial));
1099 if (error)
1100 return -EFAULT;
1101 return 0;
1102
1103#ifdef TIOCSET422
1104 case TIOCSET422: {
1105 unsigned int val;
1106 get_user(val, (unsigned int *) arg);
1107 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1108 break;
1109 }
1110 case TIOCGET422: {
1111 unsigned int val;
1112 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1113 put_user(val, (unsigned int *) arg);
1114 break;
1115 }
1116#endif
1117
1118 default:
1119 return -ENOIOCTLCMD;
1120 }
1121 return 0;
1122}
1123
1124static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1125{
1126 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1127
1128 if (tty->termios->c_cflag == old_termios->c_cflag)
1129 return;
1130
1131 mcfrs_change_speed(info);
1132
1133 if ((old_termios->c_cflag & CRTSCTS) &&
1134 !(tty->termios->c_cflag & CRTSCTS)) {
1135 tty->hw_stopped = 0;
1136 mcfrs_setsignals(info, -1, 1);
1137#if 0
1138 mcfrs_start(tty);
1139#endif
1140 }
1141}
1142
1143/*
1144 * ------------------------------------------------------------
1145 * mcfrs_close()
1146 *
1147 * This routine is called when the serial port gets closed. First, we
1148 * wait for the last remaining data to be sent. Then, we unlink its
1149 * S structure from the interrupt chain if necessary, and we free
1150 * that IRQ if nothing is left in the chain.
1151 * ------------------------------------------------------------
1152 */
1153static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1154{
1155 volatile unsigned char *uartp;
1156 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1157 unsigned long flags;
1158
1159 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1160 return;
1161
1162 local_irq_save(flags);
1163
1164 if (tty_hung_up_p(filp)) {
1165 local_irq_restore(flags);
1166 return;
1167 }
1168
1169#ifdef SERIAL_DEBUG_OPEN
1170 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1171#endif
1172 if ((tty->count == 1) && (info->count != 1)) {
1173 /*
1174 * Uh, oh. tty->count is 1, which means that the tty
1175 * structure will be freed. Info->count should always
1176 * be one in these conditions. If it's greater than
1177 * one, we've got real problems, since it means the
1178 * serial port won't be shutdown.
1179 */
1180 printk("MCFRS: bad serial port count; tty->count is 1, "
1181 "info->count is %d\n", info->count);
1182 info->count = 1;
1183 }
1184 if (--info->count < 0) {
1185 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1186 info->line, info->count);
1187 info->count = 0;
1188 }
1189 if (info->count) {
1190 local_irq_restore(flags);
1191 return;
1192 }
1193 info->flags |= ASYNC_CLOSING;
1194
1195 /*
1196 * Now we wait for the transmit buffer to clear; and we notify
1197 * the line discipline to only process XON/XOFF characters.
1198 */
1199 tty->closing = 1;
1200 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1201 tty_wait_until_sent(tty, info->closing_wait);
1202
1203 /*
1204 * At this point we stop accepting input. To do this, we
1205 * disable the receive line status interrupts, and tell the
1206 * interrupt driver to stop checking the data ready bit in the
1207 * line status register.
1208 */
1209 info->imr &= ~MCFUART_UIR_RXREADY;
1210 uartp = info->addr;
1211 uartp[MCFUART_UIMR] = info->imr;
1212
1213#if 0
1214 /* FIXME: do we need to keep this enabled for console?? */
1215 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1216 /* Do not disable the UART */ ;
1217 } else
1218#endif
1219 shutdown(info);
1220 if (tty->driver->flush_buffer)
1221 tty->driver->flush_buffer(tty);
1222 tty_ldisc_flush(tty);
1223
1224 tty->closing = 0;
1225 info->event = 0;
1226 info->tty = 0;
1227#if 0
1228 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1229 if (tty->ldisc.close)
1230 (tty->ldisc.close)(tty);
1231 tty->ldisc = ldiscs[N_TTY];
1232 tty->termios->c_line = N_TTY;
1233 if (tty->ldisc.open)
1234 (tty->ldisc.open)(tty);
1235 }
1236#endif
1237 if (info->blocked_open) {
1238 if (info->close_delay) {
1239 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1240 }
1241 wake_up_interruptible(&info->open_wait);
1242 }
1243 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1244 wake_up_interruptible(&info->close_wait);
1245 local_irq_restore(flags);
1246}
1247
1248/*
1249 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1250 */
1251static void
1252mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1253{
1254#ifdef CONFIG_M5272
1255#define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1256
1257 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1258 volatile unsigned char *uartp;
1259 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1260
1261 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1262 return;
1263
1264 orig_jiffies = jiffies;
1265
1266 /*
1267 * Set the check interval to be 1/5 of the approximate time
1268 * to send the entire fifo, and make it at least 1. The check
1269 * interval should also be less than the timeout.
1270 *
1271 * Note: we have to use pretty tight timings here to satisfy
1272 * the NIST-PCTS.
1273 */
1274 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1275 char_time = fifo_time / 5;
1276 if (char_time == 0)
1277 char_time = 1;
1278 if (timeout && timeout < char_time)
1279 char_time = timeout;
1280
1281 /*
1282 * Clamp the timeout period at 2 * the time to empty the
1283 * fifo. Just to be safe, set the minimum at .5 seconds.
1284 */
1285 fifo_time *= 2;
1286 if (fifo_time < (HZ/2))
1287 fifo_time = HZ/2;
1288 if (!timeout || timeout > fifo_time)
1289 timeout = fifo_time;
1290
1291 /*
1292 * Account for the number of bytes in the UART
1293 * transmitter FIFO plus any byte being shifted out.
1294 */
1295 uartp = (volatile unsigned char *) info->addr;
1296 for (;;) {
1297 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1298 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1299 MCFUART_USR_TXEMPTY)) ==
1300 MCFUART_USR_TXREADY)
1301 fifo_cnt++;
1302 if (fifo_cnt == 0)
1303 break;
1304 msleep_interruptible(jiffies_to_msecs(char_time));
1305 if (signal_pending(current))
1306 break;
1307 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1308 break;
1309 }
1310#else
1311 /*
1312 * For the other coldfire models, assume all data has been sent
1313 */
1314#endif
1315}
1316
1317/*
1318 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1319 */
1320void mcfrs_hangup(struct tty_struct *tty)
1321{
1322 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1323
1324 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1325 return;
1326
1327 mcfrs_flush_buffer(tty);
1328 shutdown(info);
1329 info->event = 0;
1330 info->count = 0;
1331 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1332 info->tty = 0;
1333 wake_up_interruptible(&info->open_wait);
1334}
1335
1336/*
1337 * ------------------------------------------------------------
1338 * mcfrs_open() and friends
1339 * ------------------------------------------------------------
1340 */
1341static int block_til_ready(struct tty_struct *tty, struct file * filp,
1342 struct mcf_serial *info)
1343{
1344 DECLARE_WAITQUEUE(wait, current);
1345 int retval;
1346 int do_clocal = 0;
1347
1348 /*
1349 * If the device is in the middle of being closed, then block
1350 * until it's done, and then try again.
1351 */
1352 if (info->flags & ASYNC_CLOSING) {
1353 interruptible_sleep_on(&info->close_wait);
1354#ifdef SERIAL_DO_RESTART
1355 if (info->flags & ASYNC_HUP_NOTIFY)
1356 return -EAGAIN;
1357 else
1358 return -ERESTARTSYS;
1359#else
1360 return -EAGAIN;
1361#endif
1362 }
1363
1364 /*
1365 * If non-blocking mode is set, or the port is not enabled,
1366 * then make the check up front and then exit.
1367 */
1368 if ((filp->f_flags & O_NONBLOCK) ||
1369 (tty->flags & (1 << TTY_IO_ERROR))) {
1370 info->flags |= ASYNC_NORMAL_ACTIVE;
1371 return 0;
1372 }
1373
1374 if (tty->termios->c_cflag & CLOCAL)
1375 do_clocal = 1;
1376
1377 /*
1378 * Block waiting for the carrier detect and the line to become
1379 * free (i.e., not in use by the callout). While we are in
1380 * this loop, info->count is dropped by one, so that
1381 * mcfrs_close() knows when to free things. We restore it upon
1382 * exit, either normal or abnormal.
1383 */
1384 retval = 0;
1385 add_wait_queue(&info->open_wait, &wait);
1386#ifdef SERIAL_DEBUG_OPEN
1387 printk("block_til_ready before block: ttyS%d, count = %d\n",
1388 info->line, info->count);
1389#endif
1390 info->count--;
1391 info->blocked_open++;
1392 while (1) {
1393 local_irq_disable();
1394 mcfrs_setsignals(info, 1, 1);
1395 local_irq_enable();
1396 current->state = TASK_INTERRUPTIBLE;
1397 if (tty_hung_up_p(filp) ||
1398 !(info->flags & ASYNC_INITIALIZED)) {
1399#ifdef SERIAL_DO_RESTART
1400 if (info->flags & ASYNC_HUP_NOTIFY)
1401 retval = -EAGAIN;
1402 else
1403 retval = -ERESTARTSYS;
1404#else
1405 retval = -EAGAIN;
1406#endif
1407 break;
1408 }
1409 if (!(info->flags & ASYNC_CLOSING) &&
1410 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1411 break;
1412 if (signal_pending(current)) {
1413 retval = -ERESTARTSYS;
1414 break;
1415 }
1416#ifdef SERIAL_DEBUG_OPEN
1417 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1418 info->line, info->count);
1419#endif
1420 schedule();
1421 }
1422 current->state = TASK_RUNNING;
1423 remove_wait_queue(&info->open_wait, &wait);
1424 if (!tty_hung_up_p(filp))
1425 info->count++;
1426 info->blocked_open--;
1427#ifdef SERIAL_DEBUG_OPEN
1428 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1429 info->line, info->count);
1430#endif
1431 if (retval)
1432 return retval;
1433 info->flags |= ASYNC_NORMAL_ACTIVE;
1434 return 0;
1435}
1436
1437/*
1438 * This routine is called whenever a serial port is opened. It
1439 * enables interrupts for a serial port, linking in its structure into
1440 * the IRQ chain. It also performs the serial-specific
1441 * initialization for the tty structure.
1442 */
1443int mcfrs_open(struct tty_struct *tty, struct file * filp)
1444{
1445 struct mcf_serial *info;
1446 int retval, line;
1447
1448 line = tty->index;
1449 if ((line < 0) || (line >= NR_PORTS))
1450 return -ENODEV;
1451 info = mcfrs_table + line;
1452 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1453 return -ENODEV;
1454#ifdef SERIAL_DEBUG_OPEN
1455 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1456#endif
1457 info->count++;
1458 tty->driver_data = info;
1459 info->tty = tty;
1460
1461 /*
1462 * Start up serial port
1463 */
1464 retval = startup(info);
1465 if (retval)
1466 return retval;
1467
1468 retval = block_til_ready(tty, filp, info);
1469 if (retval) {
1470#ifdef SERIAL_DEBUG_OPEN
1471 printk("mcfrs_open returning after block_til_ready with %d\n",
1472 retval);
1473#endif
1474 return retval;
1475 }
1476
1477#ifdef SERIAL_DEBUG_OPEN
1478 printk("mcfrs_open %s successful...\n", tty->name);
1479#endif
1480 return 0;
1481}
1482
1483/*
1484 * Based on the line number set up the internal interrupt stuff.
1485 */
1486static void mcfrs_irqinit(struct mcf_serial *info)
1487{
1488#if defined(CONFIG_M5272)
1489 volatile unsigned long *icrp;
1490 volatile unsigned long *portp;
1491 volatile unsigned char *uartp;
1492
1493 uartp = info->addr;
1494 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1495
1496 switch (info->line) {
1497 case 0:
1498 *icrp = 0xe0000000;
1499 break;
1500 case 1:
1501 *icrp = 0x0e000000;
1502 break;
1503 default:
1504 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1505 info->line);
1506 return;
1507 }
1508
1509 /* Enable the output lines for the serial ports */
1510 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1511 *portp = (*portp & ~0x000000ff) | 0x00000055;
1512 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1513 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1514#elif defined(CONFIG_M527x) || defined(CONFIG_M528x)
1515 volatile unsigned char *icrp, *uartp;
1516 volatile unsigned long *imrp;
1517
1518 uartp = info->addr;
1519
1520 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1521 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1522 *icrp = 0x33; /* UART0 with level 6, priority 3 */
1523
1524 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1525 MCFINTC_IMRL);
1526 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1527#else
1528 volatile unsigned char *icrp, *uartp;
1529
1530 switch (info->line) {
1531 case 0:
1532 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1533 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1534 MCFSIM_ICR_PRI1;
1535 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1536 break;
1537 case 1:
1538 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1539 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1540 MCFSIM_ICR_PRI2;
1541 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1542 break;
1543 default:
1544 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1545 info->line);
1546 return;
1547 }
1548
1549 uartp = info->addr;
1550 uartp[MCFUART_UIVR] = info->irq;
1551#endif
1552
1553 /* Clear mask, so no surprise interrupts. */
1554 uartp[MCFUART_UIMR] = 0;
1555
1556 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1557 "ColdFire UART", NULL)) {
1558 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1559 "vector=%d\n", info->line, info->irq);
1560 }
1561
1562 return;
1563}
1564
1565
1566char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1567
1568
1569/*
1570 * Serial stats reporting...
1571 */
1572int mcfrs_readproc(char *page, char **start, off_t off, int count,
1573 int *eof, void *data)
1574{
1575 struct mcf_serial *info;
1576 char str[20];
1577 int len, sigs, i;
1578
1579 len = sprintf(page, mcfrs_drivername);
1580 for (i = 0; (i < NR_PORTS); i++) {
1581 info = &mcfrs_table[i];
1582 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1583 i, (unsigned int) info->addr, info->irq, info->baud);
1584 if (info->stats.rx || info->stats.tx)
1585 len += sprintf((page + len), "tx:%d rx:%d ",
1586 info->stats.tx, info->stats.rx);
1587 if (info->stats.rxframing)
1588 len += sprintf((page + len), "fe:%d ",
1589 info->stats.rxframing);
1590 if (info->stats.rxparity)
1591 len += sprintf((page + len), "pe:%d ",
1592 info->stats.rxparity);
1593 if (info->stats.rxbreak)
1594 len += sprintf((page + len), "brk:%d ",
1595 info->stats.rxbreak);
1596 if (info->stats.rxoverrun)
1597 len += sprintf((page + len), "oe:%d ",
1598 info->stats.rxoverrun);
1599
1600 str[0] = str[1] = 0;
1601 if ((sigs = mcfrs_getsignals(info))) {
1602 if (sigs & TIOCM_RTS)
1603 strcat(str, "|RTS");
1604 if (sigs & TIOCM_CTS)
1605 strcat(str, "|CTS");
1606 if (sigs & TIOCM_DTR)
1607 strcat(str, "|DTR");
1608 if (sigs & TIOCM_CD)
1609 strcat(str, "|CD");
1610 }
1611
1612 len += sprintf((page + len), "%s\n", &str[1]);
1613 }
1614
1615 return(len);
1616}
1617
1618
1619/* Finally, routines used to initialize the serial driver. */
1620
1621static void show_serial_version(void)
1622{
1623 printk(mcfrs_drivername);
1624}
1625
1626static struct tty_operations mcfrs_ops = {
1627 .open = mcfrs_open,
1628 .close = mcfrs_close,
1629 .write = mcfrs_write,
1630 .flush_chars = mcfrs_flush_chars,
1631 .write_room = mcfrs_write_room,
1632 .chars_in_buffer = mcfrs_chars_in_buffer,
1633 .flush_buffer = mcfrs_flush_buffer,
1634 .ioctl = mcfrs_ioctl,
1635 .throttle = mcfrs_throttle,
1636 .unthrottle = mcfrs_unthrottle,
1637 .set_termios = mcfrs_set_termios,
1638 .stop = mcfrs_stop,
1639 .start = mcfrs_start,
1640 .hangup = mcfrs_hangup,
1641 .read_proc = mcfrs_readproc,
1642 .wait_until_sent = mcfrs_wait_until_sent,
1643 .tiocmget = mcfrs_tiocmget,
1644 .tiocmset = mcfrs_tiocmset,
1645};
1646
1647/* mcfrs_init inits the driver */
1648static int __init
1649mcfrs_init(void)
1650{
1651 struct mcf_serial *info;
1652 unsigned long flags;
1653 int i;
1654
1655 /* Setup base handler, and timer table. */
1656#ifdef MCFPP_DCD0
1657 init_timer(&mcfrs_timer_struct);
1658 mcfrs_timer_struct.function = mcfrs_timer;
1659 mcfrs_timer_struct.data = 0;
1660 mcfrs_timer_struct.expires = jiffies + HZ/25;
1661 add_timer(&mcfrs_timer_struct);
1662 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1663#endif
1664 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1665 if (!mcfrs_serial_driver)
1666 return -ENOMEM;
1667
1668 show_serial_version();
1669
1670 /* Initialize the tty_driver structure */
1671 mcfrs_serial_driver->owner = THIS_MODULE;
1672 mcfrs_serial_driver->name = "ttyS";
1673 mcfrs_serial_driver->devfs_name = "ttys/";
1674 mcfrs_serial_driver->driver_name = "serial";
1675 mcfrs_serial_driver->major = TTY_MAJOR;
1676 mcfrs_serial_driver->minor_start = 64;
1677 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1678 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1679 mcfrs_serial_driver->init_termios = tty_std_termios;
1680
1681 mcfrs_serial_driver->init_termios.c_cflag =
1682 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1683 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1684
1685 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1686
1687 if (tty_register_driver(mcfrs_serial_driver)) {
1688 printk("MCFRS: Couldn't register serial driver\n");
1689 put_tty_driver(mcfrs_serial_driver);
1690 return(-EBUSY);
1691 }
1692
1693 local_irq_save(flags);
1694
1695 /*
1696 * Configure all the attached serial ports.
1697 */
1698 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1699 info->magic = SERIAL_MAGIC;
1700 info->line = i;
1701 info->tty = 0;
1702 info->custom_divisor = 16;
1703 info->close_delay = 50;
1704 info->closing_wait = 3000;
1705 info->x_char = 0;
1706 info->event = 0;
1707 info->count = 0;
1708 info->blocked_open = 0;
1709 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1710 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1711 init_waitqueue_head(&info->open_wait);
1712 init_waitqueue_head(&info->close_wait);
1713
1714 info->imr = 0;
1715 mcfrs_setsignals(info, 0, 0);
1716 mcfrs_irqinit(info);
1717
1718 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1719 (unsigned int) info->addr, info->irq);
1720 printk(" is a builtin ColdFire UART\n");
1721 }
1722
1723 local_irq_restore(flags);
1724 return 0;
1725}
1726
1727module_init(mcfrs_init);
1728
1729/****************************************************************************/
1730/* Serial Console */
1731/****************************************************************************/
1732
1733/*
1734 * Quick and dirty UART initialization, for console output.
1735 */
1736
1737void mcfrs_init_console(void)
1738{
1739 volatile unsigned char *uartp;
1740 unsigned int clk;
1741
1742 /*
1743 * Reset UART, get it into known state...
1744 */
1745 uartp = (volatile unsigned char *) (MCF_MBAR +
1746 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1747
1748 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1749 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1750 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1751
1752 /*
1753 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1754 */
1755 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1756 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1757
1758 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1759 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1760 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1761
1762 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1763 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1764
1765 mcfrs_console_inited++;
1766 return;
1767}
1768
1769
1770/*
1771 * Setup for console. Argument comes from the boot command line.
1772 */
1773
1774int mcfrs_console_setup(struct console *cp, char *arg)
1775{
1776 int i, n = CONSOLE_BAUD_RATE;
1777
1778 if (!cp)
1779 return(-1);
1780
1781 if (!strncmp(cp->name, "ttyS", 4))
1782 mcfrs_console_port = cp->index;
1783 else if (!strncmp(cp->name, "cua", 3))
1784 mcfrs_console_port = cp->index;
1785 else
1786 return(-1);
1787
1788 if (arg)
1789 n = simple_strtoul(arg,NULL,0);
1790 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1791 if (mcfrs_baud_table[i] == n)
1792 break;
1793 if (i < MCFRS_BAUD_TABLE_SIZE) {
1794 mcfrs_console_baud = n;
1795 mcfrs_console_cbaud = 0;
1796 if (i > 15) {
1797 mcfrs_console_cbaud |= CBAUDEX;
1798 i -= 15;
1799 }
1800 mcfrs_console_cbaud |= i;
1801 }
1802 mcfrs_init_console(); /* make sure baud rate changes */
1803 return(0);
1804}
1805
1806
1807static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1808{
1809 *index = c->index;
1810 return mcfrs_serial_driver;
1811}
1812
1813
1814/*
1815 * Output a single character, using UART polled mode.
1816 * This is used for console output.
1817 */
1818
1819void mcfrs_put_char(char ch)
1820{
1821 volatile unsigned char *uartp;
1822 unsigned long flags;
1823 int i;
1824
1825 uartp = (volatile unsigned char *) (MCF_MBAR +
1826 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1827
1828 local_irq_save(flags);
1829 for (i = 0; (i < 0x10000); i++) {
1830 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1831 break;
1832 }
1833 if (i < 0x10000) {
1834 uartp[MCFUART_UTB] = ch;
1835 for (i = 0; (i < 0x10000); i++)
1836 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1837 break;
1838 }
1839 if (i >= 0x10000)
1840 mcfrs_init_console(); /* try and get it back */
1841 local_irq_restore(flags);
1842
1843 return;
1844}
1845
1846
1847/*
1848 * rs_console_write is registered for printk output.
1849 */
1850
1851void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1852{
1853 if (!mcfrs_console_inited)
1854 mcfrs_init_console();
1855 while (len-- > 0) {
1856 if (*p == '\n')
1857 mcfrs_put_char('\r');
1858 mcfrs_put_char(*p++);
1859 }
1860}
1861
1862/*
1863 * declare our consoles
1864 */
1865
1866struct console mcfrs_console = {
1867 .name = "ttyS",
1868 .write = mcfrs_console_write,
1869 .device = mcfrs_console_device,
1870 .setup = mcfrs_console_setup,
1871 .flags = CON_PRINTBUFFER,
1872 .index = -1,
1873};
1874
1875static int __init mcfrs_console_init(void)
1876{
1877 register_console(&mcfrs_console);
1878 return 0;
1879}
1880
1881console_initcall(mcfrs_console_init);
1882
1883/****************************************************************************/
diff --git a/drivers/serial/mcfserial.h b/drivers/serial/mcfserial.h
new file mode 100644
index 000000000000..a2b28e8629f9
--- /dev/null
+++ b/drivers/serial/mcfserial.h
@@ -0,0 +1,75 @@
1/*
2 * mcfserial.c -- serial driver for ColdFire internal UARTS.
3 *
4 * Copyright (c) 1999 Greg Ungerer <gerg@snapgear.com>
5 * Copyright (c) 2000-2001 Lineo, Inc. <www.lineo.com>
6 * Copyright (c) 2002 SnapGear Inc., <www.snapgear.com>
7 *
8 * Based on code from 68332serial.c which was:
9 *
10 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
11 * Copyright (C) 1998 TSHG
12 * Copyright (c) 1999 Rt-Control Inc. <jeff@uclinux.org>
13 */
14#ifndef _MCF_SERIAL_H
15#define _MCF_SERIAL_H
16
17#include <linux/config.h>
18#include <linux/serial.h>
19
20#ifdef __KERNEL__
21
22/*
23 * Define a local serial stats structure.
24 */
25
26struct mcf_stats {
27 unsigned int rx;
28 unsigned int tx;
29 unsigned int rxbreak;
30 unsigned int rxframing;
31 unsigned int rxparity;
32 unsigned int rxoverrun;
33};
34
35
36/*
37 * This is our internal structure for each serial port's state.
38 * Each serial port has one of these structures associated with it.
39 */
40
41struct mcf_serial {
42 int magic;
43 volatile unsigned char *addr; /* UART memory address */
44 int irq;
45 int flags; /* defined in tty.h */
46 int type; /* UART type */
47 struct tty_struct *tty;
48 unsigned char imr; /* Software imr register */
49 unsigned int baud;
50 int sigs;
51 int custom_divisor;
52 int x_char; /* xon/xoff character */
53 int baud_base;
54 int close_delay;
55 unsigned short closing_wait;
56 unsigned short closing_wait2;
57 unsigned long event;
58 int line;
59 int count; /* # of fd on device */
60 int blocked_open; /* # of blocked opens */
61 unsigned char *xmit_buf;
62 int xmit_head;
63 int xmit_tail;
64 int xmit_cnt;
65 struct mcf_stats stats;
66 struct work_struct tqueue;
67 struct work_struct tqueue_hangup;
68 wait_queue_head_t open_wait;
69 wait_queue_head_t close_wait;
70
71};
72
73#endif /* __KERNEL__ */
74
75#endif /* _MCF_SERIAL_H */
diff --git a/drivers/serial/mpc52xx_uart.c b/drivers/serial/mpc52xx_uart.c
new file mode 100644
index 000000000000..2a5cf174ca30
--- /dev/null
+++ b/drivers/serial/mpc52xx_uart.c
@@ -0,0 +1,852 @@
1/*
2 * drivers/serial/mpc52xx_uart.c
3 *
4 * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
5 *
6 * FIXME According to the usermanual the status bits in the status register
7 * are only updated when the peripherals access the FIFO and not when the
8 * CPU access them. So since we use this bits to know when we stop writing
9 * and reading, they may not be updated in-time and a race condition may
10 * exists. But I haven't be able to prove this and I don't care. But if
11 * any problem arises, it might worth checking. The TX/RX FIFO Stats
12 * registers should be used in addition.
13 * Update: Actually, they seem updated ... At least the bits we use.
14 *
15 *
16 * Maintainer : Sylvain Munaut <tnt@246tNt.com>
17 *
18 * Some of the code has been inspired/copied from the 2.4 code written
19 * by Dale Farnsworth <dfarnsworth@mvista.com>.
20 *
21 * Copyright (C) 2004-2005 Sylvain Munaut <tnt@246tNt.com>
22 * Copyright (C) 2003 MontaVista, Software, Inc.
23 *
24 * This file is licensed under the terms of the GNU General Public License
25 * version 2. This program is licensed "as is" without any warranty of any
26 * kind, whether express or implied.
27 */
28
29/* Platform device Usage :
30 *
31 * Since PSCs can have multiple function, the correct driver for each one
32 * is selected by calling mpc52xx_match_psc_function(...). The function
33 * handled by this driver is "uart".
34 *
35 * The driver init all necessary registers to place the PSC in uart mode without
36 * DCD. However, the pin multiplexing aren't changed and should be set either
37 * by the bootloader or in the platform init code.
38 *
39 * The idx field must be equal to the PSC index ( e.g. 0 for PSC1, 1 for PSC2,
40 * and so on). So the PSC1 is mapped to /dev/ttyS0, PSC2 to /dev/ttyS1 and so
41 * on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly for
42 * the console code : without this 1:1 mapping, at early boot time, when we are
43 * parsing the kernel args console=ttyS?, we wouldn't know wich PSC it will be
44 * mapped to.
45 */
46
47#include <linux/config.h>
48#include <linux/device.h>
49#include <linux/module.h>
50#include <linux/tty.h>
51#include <linux/serial.h>
52#include <linux/sysrq.h>
53#include <linux/console.h>
54
55#include <asm/delay.h>
56#include <asm/io.h>
57
58#include <asm/mpc52xx.h>
59#include <asm/mpc52xx_psc.h>
60
61#if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
62#define SUPPORT_SYSRQ
63#endif
64
65#include <linux/serial_core.h>
66
67
68
69#define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
70
71
72static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
73 /* Rem: - We use the read_status_mask as a shadow of
74 * psc->mpc52xx_psc_imr
75 * - It's important that is array is all zero on start as we
76 * use it to know if it's initialized or not ! If it's not sure
77 * it's cleared, then a memset(...,0,...) should be added to
78 * the console_init
79 */
80
81#define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
82
83
84/* Forward declaration of the interruption handling routine */
85static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id,struct pt_regs *regs);
86
87
88/* Simple macro to test if a port is console or not. This one is taken
89 * for serial_core.c and maybe should be moved to serial_core.h ? */
90#ifdef CONFIG_SERIAL_CORE_CONSOLE
91#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
92#else
93#define uart_console(port) (0)
94#endif
95
96
97/* ======================================================================== */
98/* UART operations */
99/* ======================================================================== */
100
101static unsigned int
102mpc52xx_uart_tx_empty(struct uart_port *port)
103{
104 int status = in_be16(&PSC(port)->mpc52xx_psc_status);
105 return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
106}
107
108static void
109mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
110{
111 /* Not implemented */
112}
113
114static unsigned int
115mpc52xx_uart_get_mctrl(struct uart_port *port)
116{
117 /* Not implemented */
118 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
119}
120
121static void
122mpc52xx_uart_stop_tx(struct uart_port *port, unsigned int tty_stop)
123{
124 /* port->lock taken by caller */
125 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
126 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
127}
128
129static void
130mpc52xx_uart_start_tx(struct uart_port *port, unsigned int tty_start)
131{
132 /* port->lock taken by caller */
133 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
134 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
135}
136
137static void
138mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
139{
140 unsigned long flags;
141 spin_lock_irqsave(&port->lock, flags);
142
143 port->x_char = ch;
144 if (ch) {
145 /* Make sure tx interrupts are on */
146 /* Truly necessary ??? They should be anyway */
147 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
148 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
149 }
150
151 spin_unlock_irqrestore(&port->lock, flags);
152}
153
154static void
155mpc52xx_uart_stop_rx(struct uart_port *port)
156{
157 /* port->lock taken by caller */
158 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
159 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
160}
161
162static void
163mpc52xx_uart_enable_ms(struct uart_port *port)
164{
165 /* Not implemented */
166}
167
168static void
169mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
170{
171 unsigned long flags;
172 spin_lock_irqsave(&port->lock, flags);
173
174 if ( ctl == -1 )
175 out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
176 else
177 out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
178
179 spin_unlock_irqrestore(&port->lock, flags);
180}
181
182static int
183mpc52xx_uart_startup(struct uart_port *port)
184{
185 struct mpc52xx_psc __iomem *psc = PSC(port);
186 int ret;
187
188 /* Request IRQ */
189 ret = request_irq(port->irq, mpc52xx_uart_int,
190 SA_INTERRUPT | SA_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
191 if (ret)
192 return ret;
193
194 /* Reset/activate the port, clear and enable interrupts */
195 out_8(&psc->command,MPC52xx_PSC_RST_RX);
196 out_8(&psc->command,MPC52xx_PSC_RST_TX);
197
198 out_be32(&psc->sicr,0); /* UART mode DCD ignored */
199
200 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
201
202 out_8(&psc->rfcntl, 0x00);
203 out_be16(&psc->rfalarm, 0x1ff);
204 out_8(&psc->tfcntl, 0x07);
205 out_be16(&psc->tfalarm, 0x80);
206
207 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
208 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
209
210 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
211 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
212
213 return 0;
214}
215
216static void
217mpc52xx_uart_shutdown(struct uart_port *port)
218{
219 struct mpc52xx_psc __iomem *psc = PSC(port);
220
221 /* Shut down the port, interrupt and all */
222 out_8(&psc->command,MPC52xx_PSC_RST_RX);
223 out_8(&psc->command,MPC52xx_PSC_RST_TX);
224
225 port->read_status_mask = 0;
226 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
227
228 /* Release interrupt */
229 free_irq(port->irq, port);
230}
231
232static void
233mpc52xx_uart_set_termios(struct uart_port *port, struct termios *new,
234 struct termios *old)
235{
236 struct mpc52xx_psc __iomem *psc = PSC(port);
237 unsigned long flags;
238 unsigned char mr1, mr2;
239 unsigned short ctr;
240 unsigned int j, baud, quot;
241
242 /* Prepare what we're gonna write */
243 mr1 = 0;
244
245 switch (new->c_cflag & CSIZE) {
246 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
247 break;
248 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
249 break;
250 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
251 break;
252 case CS8:
253 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
254 }
255
256 if (new->c_cflag & PARENB) {
257 mr1 |= (new->c_cflag & PARODD) ?
258 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
259 } else
260 mr1 |= MPC52xx_PSC_MODE_PARNONE;
261
262
263 mr2 = 0;
264
265 if (new->c_cflag & CSTOPB)
266 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
267 else
268 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
269 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
270 MPC52xx_PSC_MODE_ONE_STOP;
271
272
273 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
274 quot = uart_get_divisor(port, baud);
275 ctr = quot & 0xffff;
276
277 /* Get the lock */
278 spin_lock_irqsave(&port->lock, flags);
279
280 /* Update the per-port timeout */
281 uart_update_timeout(port, new->c_cflag, baud);
282
283 /* Do our best to flush TX & RX, so we don't loose anything */
284 /* But we don't wait indefinitly ! */
285 j = 5000000; /* Maximum wait */
286 /* FIXME Can't receive chars since set_termios might be called at early
287 * boot for the console, all stuff is not yet ready to receive at that
288 * time and that just makes the kernel oops */
289 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
290 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
291 --j)
292 udelay(1);
293
294 if (!j)
295 printk( KERN_ERR "mpc52xx_uart.c: "
296 "Unable to flush RX & TX fifos in-time in set_termios."
297 "Some chars may have been lost.\n" );
298
299 /* Reset the TX & RX */
300 out_8(&psc->command,MPC52xx_PSC_RST_RX);
301 out_8(&psc->command,MPC52xx_PSC_RST_TX);
302
303 /* Send new mode settings */
304 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
305 out_8(&psc->mode,mr1);
306 out_8(&psc->mode,mr2);
307 out_8(&psc->ctur,ctr >> 8);
308 out_8(&psc->ctlr,ctr & 0xff);
309
310 /* Reenable TX & RX */
311 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
312 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
313
314 /* We're all set, release the lock */
315 spin_unlock_irqrestore(&port->lock, flags);
316}
317
318static const char *
319mpc52xx_uart_type(struct uart_port *port)
320{
321 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
322}
323
324static void
325mpc52xx_uart_release_port(struct uart_port *port)
326{
327 if (port->flags & UPF_IOREMAP) { /* remapped by us ? */
328 iounmap(port->membase);
329 port->membase = NULL;
330 }
331
332 release_mem_region(port->mapbase, MPC52xx_PSC_SIZE);
333}
334
335static int
336mpc52xx_uart_request_port(struct uart_port *port)
337{
338 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
339 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
340
341 if (!port->membase)
342 return -EINVAL;
343
344 return request_mem_region(port->mapbase, MPC52xx_PSC_SIZE,
345 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
346}
347
348static void
349mpc52xx_uart_config_port(struct uart_port *port, int flags)
350{
351 if ( (flags & UART_CONFIG_TYPE) &&
352 (mpc52xx_uart_request_port(port) == 0) )
353 port->type = PORT_MPC52xx;
354}
355
356static int
357mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
358{
359 if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
360 return -EINVAL;
361
362 if ( (ser->irq != port->irq) ||
363 (ser->io_type != SERIAL_IO_MEM) ||
364 (ser->baud_base != port->uartclk) ||
365 (ser->iomem_base != (void*)port->mapbase) ||
366 (ser->hub6 != 0 ) )
367 return -EINVAL;
368
369 return 0;
370}
371
372
373static struct uart_ops mpc52xx_uart_ops = {
374 .tx_empty = mpc52xx_uart_tx_empty,
375 .set_mctrl = mpc52xx_uart_set_mctrl,
376 .get_mctrl = mpc52xx_uart_get_mctrl,
377 .stop_tx = mpc52xx_uart_stop_tx,
378 .start_tx = mpc52xx_uart_start_tx,
379 .send_xchar = mpc52xx_uart_send_xchar,
380 .stop_rx = mpc52xx_uart_stop_rx,
381 .enable_ms = mpc52xx_uart_enable_ms,
382 .break_ctl = mpc52xx_uart_break_ctl,
383 .startup = mpc52xx_uart_startup,
384 .shutdown = mpc52xx_uart_shutdown,
385 .set_termios = mpc52xx_uart_set_termios,
386/* .pm = mpc52xx_uart_pm, Not supported yet */
387/* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
388 .type = mpc52xx_uart_type,
389 .release_port = mpc52xx_uart_release_port,
390 .request_port = mpc52xx_uart_request_port,
391 .config_port = mpc52xx_uart_config_port,
392 .verify_port = mpc52xx_uart_verify_port
393};
394
395
396/* ======================================================================== */
397/* Interrupt handling */
398/* ======================================================================== */
399
400static inline int
401mpc52xx_uart_int_rx_chars(struct uart_port *port, struct pt_regs *regs)
402{
403 struct tty_struct *tty = port->info->tty;
404 unsigned char ch;
405 unsigned short status;
406
407 /* While we can read, do so ! */
408 while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
409 MPC52xx_PSC_SR_RXRDY) {
410
411 /* If we are full, just stop reading */
412 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
413 break;
414
415 /* Get the char */
416 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
417
418 /* Handle sysreq char */
419#ifdef SUPPORT_SYSRQ
420 if (uart_handle_sysrq_char(port, ch, regs)) {
421 port->sysrq = 0;
422 continue;
423 }
424#endif
425
426 /* Store it */
427 *tty->flip.char_buf_ptr = ch;
428 *tty->flip.flag_buf_ptr = 0;
429 port->icount.rx++;
430
431 if ( status & (MPC52xx_PSC_SR_PE |
432 MPC52xx_PSC_SR_FE |
433 MPC52xx_PSC_SR_RB |
434 MPC52xx_PSC_SR_OE) ) {
435
436 if (status & MPC52xx_PSC_SR_RB) {
437 *tty->flip.flag_buf_ptr = TTY_BREAK;
438 uart_handle_break(port);
439 } else if (status & MPC52xx_PSC_SR_PE)
440 *tty->flip.flag_buf_ptr = TTY_PARITY;
441 else if (status & MPC52xx_PSC_SR_FE)
442 *tty->flip.flag_buf_ptr = TTY_FRAME;
443 if (status & MPC52xx_PSC_SR_OE) {
444 /*
445 * Overrun is special, since it's
446 * reported immediately, and doesn't
447 * affect the current character
448 */
449 if (tty->flip.count < (TTY_FLIPBUF_SIZE-1)) {
450 tty->flip.flag_buf_ptr++;
451 tty->flip.char_buf_ptr++;
452 tty->flip.count++;
453 }
454 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
455 }
456
457 /* Clear error condition */
458 out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
459
460 }
461
462 tty->flip.char_buf_ptr++;
463 tty->flip.flag_buf_ptr++;
464 tty->flip.count++;
465
466 }
467
468 tty_flip_buffer_push(tty);
469
470 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
471}
472
473static inline int
474mpc52xx_uart_int_tx_chars(struct uart_port *port)
475{
476 struct circ_buf *xmit = &port->info->xmit;
477
478 /* Process out of band chars */
479 if (port->x_char) {
480 out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
481 port->icount.tx++;
482 port->x_char = 0;
483 return 1;
484 }
485
486 /* Nothing to do ? */
487 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
488 mpc52xx_uart_stop_tx(port,0);
489 return 0;
490 }
491
492 /* Send chars */
493 while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
494 out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
495 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
496 port->icount.tx++;
497 if (uart_circ_empty(xmit))
498 break;
499 }
500
501 /* Wake up */
502 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
503 uart_write_wakeup(port);
504
505 /* Maybe we're done after all */
506 if (uart_circ_empty(xmit)) {
507 mpc52xx_uart_stop_tx(port,0);
508 return 0;
509 }
510
511 return 1;
512}
513
514static irqreturn_t
515mpc52xx_uart_int(int irq, void *dev_id, struct pt_regs *regs)
516{
517 struct uart_port *port = (struct uart_port *) dev_id;
518 unsigned long pass = ISR_PASS_LIMIT;
519 unsigned int keepgoing;
520 unsigned short status;
521
522 if ( irq != port->irq ) {
523 printk( KERN_WARNING
524 "mpc52xx_uart_int : " \
525 "Received wrong int %d. Waiting for %d\n",
526 irq, port->irq);
527 return IRQ_NONE;
528 }
529
530 spin_lock(&port->lock);
531
532 /* While we have stuff to do, we continue */
533 do {
534 /* If we don't find anything to do, we stop */
535 keepgoing = 0;
536
537 /* Read status */
538 status = in_be16(&PSC(port)->mpc52xx_psc_isr);
539 status &= port->read_status_mask;
540
541 /* Do we need to receive chars ? */
542 /* For this RX interrupts must be on and some chars waiting */
543 if ( status & MPC52xx_PSC_IMR_RXRDY )
544 keepgoing |= mpc52xx_uart_int_rx_chars(port, regs);
545
546 /* Do we need to send chars ? */
547 /* For this, TX must be ready and TX interrupt enabled */
548 if ( status & MPC52xx_PSC_IMR_TXRDY )
549 keepgoing |= mpc52xx_uart_int_tx_chars(port);
550
551 /* Limit number of iteration */
552 if ( !(--pass) )
553 keepgoing = 0;
554
555 } while (keepgoing);
556
557 spin_unlock(&port->lock);
558
559 return IRQ_HANDLED;
560}
561
562
563/* ======================================================================== */
564/* Console ( if applicable ) */
565/* ======================================================================== */
566
567#ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
568
569static void __init
570mpc52xx_console_get_options(struct uart_port *port,
571 int *baud, int *parity, int *bits, int *flow)
572{
573 struct mpc52xx_psc __iomem *psc = PSC(port);
574 unsigned char mr1;
575
576 /* Read the mode registers */
577 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
578 mr1 = in_8(&psc->mode);
579
580 /* CT{U,L}R are write-only ! */
581 *baud = __res.bi_baudrate ?
582 __res.bi_baudrate : CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
583
584 /* Parse them */
585 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
586 case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
587 case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
588 case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
589 case MPC52xx_PSC_MODE_8_BITS:
590 default: *bits = 8;
591 }
592
593 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
594 *parity = 'n';
595 else
596 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
597}
598
599static void
600mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
601{
602 struct uart_port *port = &mpc52xx_uart_ports[co->index];
603 struct mpc52xx_psc __iomem *psc = PSC(port);
604 unsigned int i, j;
605
606 /* Disable interrupts */
607 out_be16(&psc->mpc52xx_psc_imr, 0);
608
609 /* Wait the TX buffer to be empty */
610 j = 5000000; /* Maximum wait */
611 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
612 --j)
613 udelay(1);
614
615 /* Write all the chars */
616 for ( i=0 ; i<count ; i++ ) {
617
618 /* Send the char */
619 out_8(&psc->mpc52xx_psc_buffer_8, *s);
620
621 /* Line return handling */
622 if ( *s++ == '\n' )
623 out_8(&psc->mpc52xx_psc_buffer_8, '\r');
624
625 /* Wait the TX buffer to be empty */
626 j = 20000; /* Maximum wait */
627 while (!(in_be16(&psc->mpc52xx_psc_status) &
628 MPC52xx_PSC_SR_TXEMP) && --j)
629 udelay(1);
630 }
631
632 /* Restore interrupt state */
633 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
634}
635
636static int __init
637mpc52xx_console_setup(struct console *co, char *options)
638{
639 struct uart_port *port = &mpc52xx_uart_ports[co->index];
640
641 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
642 int bits = 8;
643 int parity = 'n';
644 int flow = 'n';
645
646 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
647 return -EINVAL;
648
649 /* Basic port init. Needed since we use some uart_??? func before
650 * real init for early access */
651 spin_lock_init(&port->lock);
652 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
653 port->ops = &mpc52xx_uart_ops;
654 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
655
656 /* We ioremap ourself */
657 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
658 if (port->membase == NULL)
659 return -EINVAL;
660
661 /* Setup the port parameters accoding to options */
662 if (options)
663 uart_parse_options(options, &baud, &parity, &bits, &flow);
664 else
665 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
666
667 return uart_set_options(port, co, baud, parity, bits, flow);
668}
669
670
671extern struct uart_driver mpc52xx_uart_driver;
672
673static struct console mpc52xx_console = {
674 .name = "ttyS",
675 .write = mpc52xx_console_write,
676 .device = uart_console_device,
677 .setup = mpc52xx_console_setup,
678 .flags = CON_PRINTBUFFER,
679 .index = -1, /* Specified on the cmdline (e.g. console=ttyS0 ) */
680 .data = &mpc52xx_uart_driver,
681};
682
683
684static int __init
685mpc52xx_console_init(void)
686{
687 register_console(&mpc52xx_console);
688 return 0;
689}
690
691console_initcall(mpc52xx_console_init);
692
693#define MPC52xx_PSC_CONSOLE &mpc52xx_console
694#else
695#define MPC52xx_PSC_CONSOLE NULL
696#endif
697
698
699/* ======================================================================== */
700/* UART Driver */
701/* ======================================================================== */
702
703static struct uart_driver mpc52xx_uart_driver = {
704 .owner = THIS_MODULE,
705 .driver_name = "mpc52xx_psc_uart",
706 .dev_name = "ttyS",
707 .devfs_name = "ttyS",
708 .major = TTY_MAJOR,
709 .minor = 64,
710 .nr = MPC52xx_PSC_MAXNUM,
711 .cons = MPC52xx_PSC_CONSOLE,
712};
713
714
715/* ======================================================================== */
716/* Platform Driver */
717/* ======================================================================== */
718
719static int __devinit
720mpc52xx_uart_probe(struct device *dev)
721{
722 struct platform_device *pdev = to_platform_device(dev);
723 struct resource *res = pdev->resource;
724
725 struct uart_port *port = NULL;
726 int i, idx, ret;
727
728 /* Check validity & presence */
729 idx = pdev->id;
730 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
731 return -EINVAL;
732
733 if (!mpc52xx_match_psc_function(idx,"uart"))
734 return -ENODEV;
735
736 /* Init the port structure */
737 port = &mpc52xx_uart_ports[idx];
738
739 memset(port, 0x00, sizeof(struct uart_port));
740
741 spin_lock_init(&port->lock);
742 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
743 port->fifosize = 255; /* Should be 512 ! But it can't be */
744 /* stored in a unsigned char */
745 port->iotype = UPIO_MEM;
746 port->flags = UPF_BOOT_AUTOCONF |
747 ( uart_console(port) ? 0 : UPF_IOREMAP );
748 port->line = idx;
749 port->ops = &mpc52xx_uart_ops;
750
751 /* Search for IRQ and mapbase */
752 for (i=0 ; i<pdev->num_resources ; i++, res++) {
753 if (res->flags & IORESOURCE_MEM)
754 port->mapbase = res->start;
755 else if (res->flags & IORESOURCE_IRQ)
756 port->irq = res->start;
757 }
758 if (!port->irq || !port->mapbase)
759 return -EINVAL;
760
761 /* Add the port to the uart sub-system */
762 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
763 if (!ret)
764 dev_set_drvdata(dev, (void*)port);
765
766 return ret;
767}
768
769static int
770mpc52xx_uart_remove(struct device *dev)
771{
772 struct uart_port *port = (struct uart_port *) dev_get_drvdata(dev);
773
774 dev_set_drvdata(dev, NULL);
775
776 if (port)
777 uart_remove_one_port(&mpc52xx_uart_driver, port);
778
779 return 0;
780}
781
782#ifdef CONFIG_PM
783static int
784mpc52xx_uart_suspend(struct device *dev, u32 state, u32 level)
785{
786 struct uart_port *port = (struct uart_port *) dev_get_drvdata(dev);
787
788 if (sport && level == SUSPEND_DISABLE)
789 uart_suspend_port(&mpc52xx_uart_driver, port);
790
791 return 0;
792}
793
794static int
795mpc52xx_uart_resume(struct device *dev, u32 level)
796{
797 struct uart_port *port = (struct uart_port *) dev_get_drvdata(dev);
798
799 if (port && level == RESUME_ENABLE)
800 uart_resume_port(&mpc52xx_uart_driver, port);
801
802 return 0;
803}
804#endif
805
806static struct device_driver mpc52xx_uart_platform_driver = {
807 .name = "mpc52xx-psc",
808 .bus = &platform_bus_type,
809 .probe = mpc52xx_uart_probe,
810 .remove = mpc52xx_uart_remove,
811#ifdef CONFIG_PM
812 .suspend = mpc52xx_uart_suspend,
813 .resume = mpc52xx_uart_resume,
814#endif
815};
816
817
818/* ======================================================================== */
819/* Module */
820/* ======================================================================== */
821
822static int __init
823mpc52xx_uart_init(void)
824{
825 int ret;
826
827 printk(KERN_INFO "Serial: MPC52xx PSC driver\n");
828
829 ret = uart_register_driver(&mpc52xx_uart_driver);
830 if (ret == 0) {
831 ret = driver_register(&mpc52xx_uart_platform_driver);
832 if (ret)
833 uart_unregister_driver(&mpc52xx_uart_driver);
834 }
835
836 return ret;
837}
838
839static void __exit
840mpc52xx_uart_exit(void)
841{
842 driver_unregister(&mpc52xx_uart_platform_driver);
843 uart_unregister_driver(&mpc52xx_uart_driver);
844}
845
846
847module_init(mpc52xx_uart_init);
848module_exit(mpc52xx_uart_exit);
849
850MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
851MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
852MODULE_LICENSE("GPL");
diff --git a/drivers/serial/mpsc.c b/drivers/serial/mpsc.c
new file mode 100644
index 000000000000..d0dfc3cf9245
--- /dev/null
+++ b/drivers/serial/mpsc.c
@@ -0,0 +1,1832 @@
1/*
2 * drivers/serial/mpsc.c
3 *
4 * Generic driver for the MPSC (UART mode) on Marvell parts (e.g., GT64240,
5 * GT64260, MV64340, MV64360, GT96100, ... ).
6 *
7 * Author: Mark A. Greer <mgreer@mvista.com>
8 *
9 * Based on an old MPSC driver that was in the linuxppc tree. It appears to
10 * have been created by Chris Zankel (formerly of MontaVista) but there
11 * is no proper Copyright so I'm not sure. Apparently, parts were also
12 * taken from PPCBoot (now U-Boot). Also based on drivers/serial/8250.c
13 * by Russell King.
14 *
15 * 2004 (c) MontaVista, Software, Inc. This file is licensed under
16 * the terms of the GNU General Public License version 2. This program
17 * is licensed "as is" without any warranty of any kind, whether express
18 * or implied.
19 */
20/*
21 * The MPSC interface is much like a typical network controller's interface.
22 * That is, you set up separate rings of descriptors for transmitting and
23 * receiving data. There is also a pool of buffers with (one buffer per
24 * descriptor) that incoming data are dma'd into or outgoing data are dma'd
25 * out of.
26 *
27 * The MPSC requires two other controllers to be able to work. The Baud Rate
28 * Generator (BRG) provides a clock at programmable frequencies which determines
29 * the baud rate. The Serial DMA Controller (SDMA) takes incoming data from the
30 * MPSC and DMA's it into memory or DMA's outgoing data and passes it to the
31 * MPSC. It is actually the SDMA interrupt that the driver uses to keep the
32 * transmit and receive "engines" going (i.e., indicate data has been
33 * transmitted or received).
34 *
35 * NOTES:
36 *
37 * 1) Some chips have an erratum where several regs cannot be
38 * read. To work around that, we keep a local copy of those regs in
39 * 'mpsc_port_info'.
40 *
41 * 2) Some chips have an erratum where the ctlr will hang when the SDMA ctlr
42 * accesses system mem with coherency enabled. For that reason, the driver
43 * assumes that coherency for that ctlr has been disabled. This means
44 * that when in a cache coherent system, the driver has to manually manage
45 * the data cache on the areas that it touches because the dma_* macro are
46 * basically no-ops.
47 *
48 * 3) There is an erratum (on PPC) where you can't use the instruction to do
49 * a DMA_TO_DEVICE/cache clean so DMA_BIDIRECTIONAL/flushes are used in places
50 * where a DMA_TO_DEVICE/clean would have [otherwise] sufficed.
51 *
52 * 4) AFAICT, hardware flow control isn't supported by the controller --MAG.
53 */
54
55#include "mpsc.h"
56
57/*
58 * Define how this driver is known to the outside (we've been assigned a
59 * range on the "Low-density serial ports" major).
60 */
61#define MPSC_MAJOR 204
62#define MPSC_MINOR_START 44
63#define MPSC_DRIVER_NAME "MPSC"
64#define MPSC_DEVFS_NAME "ttymm/"
65#define MPSC_DEV_NAME "ttyMM"
66#define MPSC_VERSION "1.00"
67
68static struct mpsc_port_info mpsc_ports[MPSC_NUM_CTLRS];
69static struct mpsc_shared_regs mpsc_shared_regs;
70
71/*
72 ******************************************************************************
73 *
74 * Baud Rate Generator Routines (BRG)
75 *
76 ******************************************************************************
77 */
78static void
79mpsc_brg_init(struct mpsc_port_info *pi, u32 clk_src)
80{
81 u32 v;
82
83 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
84 v = (v & ~(0xf << 18)) | ((clk_src & 0xf) << 18);
85
86 if (pi->brg_can_tune)
87 v &= ~(1 << 25);
88
89 if (pi->mirror_regs)
90 pi->BRG_BCR_m = v;
91 writel(v, pi->brg_base + BRG_BCR);
92
93 writel(readl(pi->brg_base + BRG_BTR) & 0xffff0000,
94 pi->brg_base + BRG_BTR);
95 return;
96}
97
98static void
99mpsc_brg_enable(struct mpsc_port_info *pi)
100{
101 u32 v;
102
103 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
104 v |= (1 << 16);
105
106 if (pi->mirror_regs)
107 pi->BRG_BCR_m = v;
108 writel(v, pi->brg_base + BRG_BCR);
109 return;
110}
111
112static void
113mpsc_brg_disable(struct mpsc_port_info *pi)
114{
115 u32 v;
116
117 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
118 v &= ~(1 << 16);
119
120 if (pi->mirror_regs)
121 pi->BRG_BCR_m = v;
122 writel(v, pi->brg_base + BRG_BCR);
123 return;
124}
125
126static inline void
127mpsc_set_baudrate(struct mpsc_port_info *pi, u32 baud)
128{
129 /*
130 * To set the baud, we adjust the CDV field in the BRG_BCR reg.
131 * From manual: Baud = clk / ((CDV+1)*2) ==> CDV = (clk / (baud*2)) - 1.
132 * However, the input clock is divided by 16 in the MPSC b/c of how
133 * 'MPSC_MMCRH' was set up so we have to divide the 'clk' used in our
134 * calculation by 16 to account for that. So the real calculation
135 * that accounts for the way the mpsc is set up is:
136 * CDV = (clk / (baud*2*16)) - 1 ==> CDV = (clk / (baud << 5)) - 1.
137 */
138 u32 cdv = (pi->port.uartclk / (baud << 5)) - 1;
139 u32 v;
140
141 mpsc_brg_disable(pi);
142 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
143 v = (v & 0xffff0000) | (cdv & 0xffff);
144
145 if (pi->mirror_regs)
146 pi->BRG_BCR_m = v;
147 writel(v, pi->brg_base + BRG_BCR);
148 mpsc_brg_enable(pi);
149
150 return;
151}
152
153/*
154 ******************************************************************************
155 *
156 * Serial DMA Routines (SDMA)
157 *
158 ******************************************************************************
159 */
160
161static void
162mpsc_sdma_burstsize(struct mpsc_port_info *pi, u32 burst_size)
163{
164 u32 v;
165
166 pr_debug("mpsc_sdma_burstsize[%d]: burst_size: %d\n",
167 pi->port.line, burst_size);
168
169 burst_size >>= 3; /* Divide by 8 b/c reg values are 8-byte chunks */
170
171 if (burst_size < 2)
172 v = 0x0; /* 1 64-bit word */
173 else if (burst_size < 4)
174 v = 0x1; /* 2 64-bit words */
175 else if (burst_size < 8)
176 v = 0x2; /* 4 64-bit words */
177 else
178 v = 0x3; /* 8 64-bit words */
179
180 writel((readl(pi->sdma_base + SDMA_SDC) & (0x3 << 12)) | (v << 12),
181 pi->sdma_base + SDMA_SDC);
182 return;
183}
184
185static void
186mpsc_sdma_init(struct mpsc_port_info *pi, u32 burst_size)
187{
188 pr_debug("mpsc_sdma_init[%d]: burst_size: %d\n", pi->port.line,
189 burst_size);
190
191 writel((readl(pi->sdma_base + SDMA_SDC) & 0x3ff) | 0x03f,
192 pi->sdma_base + SDMA_SDC);
193 mpsc_sdma_burstsize(pi, burst_size);
194 return;
195}
196
197static inline u32
198mpsc_sdma_intr_mask(struct mpsc_port_info *pi, u32 mask)
199{
200 u32 old, v;
201
202 pr_debug("mpsc_sdma_intr_mask[%d]: mask: 0x%x\n", pi->port.line, mask);
203
204 old = v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
205 readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
206
207 mask &= 0xf;
208 if (pi->port.line)
209 mask <<= 8;
210 v &= ~mask;
211
212 if (pi->mirror_regs)
213 pi->shared_regs->SDMA_INTR_MASK_m = v;
214 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
215
216 if (pi->port.line)
217 old >>= 8;
218 return old & 0xf;
219}
220
221static inline void
222mpsc_sdma_intr_unmask(struct mpsc_port_info *pi, u32 mask)
223{
224 u32 v;
225
226 pr_debug("mpsc_sdma_intr_unmask[%d]: mask: 0x%x\n", pi->port.line,mask);
227
228 v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
229 readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
230
231 mask &= 0xf;
232 if (pi->port.line)
233 mask <<= 8;
234 v |= mask;
235
236 if (pi->mirror_regs)
237 pi->shared_regs->SDMA_INTR_MASK_m = v;
238 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
239 return;
240}
241
242static inline void
243mpsc_sdma_intr_ack(struct mpsc_port_info *pi)
244{
245 pr_debug("mpsc_sdma_intr_ack[%d]: Acknowledging IRQ\n", pi->port.line);
246
247 if (pi->mirror_regs)
248 pi->shared_regs->SDMA_INTR_CAUSE_m = 0;
249 writel(0, pi->shared_regs->sdma_intr_base + SDMA_INTR_CAUSE);
250 return;
251}
252
253static inline void
254mpsc_sdma_set_rx_ring(struct mpsc_port_info *pi, struct mpsc_rx_desc *rxre_p)
255{
256 pr_debug("mpsc_sdma_set_rx_ring[%d]: rxre_p: 0x%x\n",
257 pi->port.line, (u32) rxre_p);
258
259 writel((u32)rxre_p, pi->sdma_base + SDMA_SCRDP);
260 return;
261}
262
263static inline void
264mpsc_sdma_set_tx_ring(struct mpsc_port_info *pi, struct mpsc_tx_desc *txre_p)
265{
266 writel((u32)txre_p, pi->sdma_base + SDMA_SFTDP);
267 writel((u32)txre_p, pi->sdma_base + SDMA_SCTDP);
268 return;
269}
270
271static inline void
272mpsc_sdma_cmd(struct mpsc_port_info *pi, u32 val)
273{
274 u32 v;
275
276 v = readl(pi->sdma_base + SDMA_SDCM);
277 if (val)
278 v |= val;
279 else
280 v = 0;
281 wmb();
282 writel(v, pi->sdma_base + SDMA_SDCM);
283 wmb();
284 return;
285}
286
287static inline uint
288mpsc_sdma_tx_active(struct mpsc_port_info *pi)
289{
290 return readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_TXD;
291}
292
293static inline void
294mpsc_sdma_start_tx(struct mpsc_port_info *pi)
295{
296 struct mpsc_tx_desc *txre, *txre_p;
297
298 /* If tx isn't running & there's a desc ready to go, start it */
299 if (!mpsc_sdma_tx_active(pi)) {
300 txre = (struct mpsc_tx_desc *)(pi->txr +
301 (pi->txr_tail * MPSC_TXRE_SIZE));
302 dma_cache_sync((void *) txre, MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
303#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
304 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
305 invalidate_dcache_range((ulong)txre,
306 (ulong)txre + MPSC_TXRE_SIZE);
307#endif
308
309 if (be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O) {
310 txre_p = (struct mpsc_tx_desc *)(pi->txr_p +
311 (pi->txr_tail *
312 MPSC_TXRE_SIZE));
313
314 mpsc_sdma_set_tx_ring(pi, txre_p);
315 mpsc_sdma_cmd(pi, SDMA_SDCM_STD | SDMA_SDCM_TXD);
316 }
317 }
318
319 return;
320}
321
322static inline void
323mpsc_sdma_stop(struct mpsc_port_info *pi)
324{
325 pr_debug("mpsc_sdma_stop[%d]: Stopping SDMA\n", pi->port.line);
326
327 /* Abort any SDMA transfers */
328 mpsc_sdma_cmd(pi, 0);
329 mpsc_sdma_cmd(pi, SDMA_SDCM_AR | SDMA_SDCM_AT);
330
331 /* Clear the SDMA current and first TX and RX pointers */
332 mpsc_sdma_set_tx_ring(pi, 0);
333 mpsc_sdma_set_rx_ring(pi, 0);
334
335 /* Disable interrupts */
336 mpsc_sdma_intr_mask(pi, 0xf);
337 mpsc_sdma_intr_ack(pi);
338
339 return;
340}
341
342/*
343 ******************************************************************************
344 *
345 * Multi-Protocol Serial Controller Routines (MPSC)
346 *
347 ******************************************************************************
348 */
349
350static void
351mpsc_hw_init(struct mpsc_port_info *pi)
352{
353 u32 v;
354
355 pr_debug("mpsc_hw_init[%d]: Initializing hardware\n", pi->port.line);
356
357 /* Set up clock routing */
358 if (pi->mirror_regs) {
359 v = pi->shared_regs->MPSC_MRR_m;
360 v &= ~0x1c7;
361 pi->shared_regs->MPSC_MRR_m = v;
362 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
363
364 v = pi->shared_regs->MPSC_RCRR_m;
365 v = (v & ~0xf0f) | 0x100;
366 pi->shared_regs->MPSC_RCRR_m = v;
367 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
368
369 v = pi->shared_regs->MPSC_TCRR_m;
370 v = (v & ~0xf0f) | 0x100;
371 pi->shared_regs->MPSC_TCRR_m = v;
372 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
373 }
374 else {
375 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_MRR);
376 v &= ~0x1c7;
377 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
378
379 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
380 v = (v & ~0xf0f) | 0x100;
381 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
382
383 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
384 v = (v & ~0xf0f) | 0x100;
385 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
386 }
387
388 /* Put MPSC in UART mode & enabel Tx/Rx egines */
389 writel(0x000004c4, pi->mpsc_base + MPSC_MMCRL);
390
391 /* No preamble, 16x divider, low-latency, */
392 writel(0x04400400, pi->mpsc_base + MPSC_MMCRH);
393
394 if (pi->mirror_regs) {
395 pi->MPSC_CHR_1_m = 0;
396 pi->MPSC_CHR_2_m = 0;
397 }
398 writel(0, pi->mpsc_base + MPSC_CHR_1);
399 writel(0, pi->mpsc_base + MPSC_CHR_2);
400 writel(pi->mpsc_max_idle, pi->mpsc_base + MPSC_CHR_3);
401 writel(0, pi->mpsc_base + MPSC_CHR_4);
402 writel(0, pi->mpsc_base + MPSC_CHR_5);
403 writel(0, pi->mpsc_base + MPSC_CHR_6);
404 writel(0, pi->mpsc_base + MPSC_CHR_7);
405 writel(0, pi->mpsc_base + MPSC_CHR_8);
406 writel(0, pi->mpsc_base + MPSC_CHR_9);
407 writel(0, pi->mpsc_base + MPSC_CHR_10);
408
409 return;
410}
411
412static inline void
413mpsc_enter_hunt(struct mpsc_port_info *pi)
414{
415 pr_debug("mpsc_enter_hunt[%d]: Hunting...\n", pi->port.line);
416
417 if (pi->mirror_regs) {
418 writel(pi->MPSC_CHR_2_m | MPSC_CHR_2_EH,
419 pi->mpsc_base + MPSC_CHR_2);
420 /* Erratum prevents reading CHR_2 so just delay for a while */
421 udelay(100);
422 }
423 else {
424 writel(readl(pi->mpsc_base + MPSC_CHR_2) | MPSC_CHR_2_EH,
425 pi->mpsc_base + MPSC_CHR_2);
426
427 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_EH)
428 udelay(10);
429 }
430
431 return;
432}
433
434static inline void
435mpsc_freeze(struct mpsc_port_info *pi)
436{
437 u32 v;
438
439 pr_debug("mpsc_freeze[%d]: Freezing\n", pi->port.line);
440
441 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
442 readl(pi->mpsc_base + MPSC_MPCR);
443 v |= MPSC_MPCR_FRZ;
444
445 if (pi->mirror_regs)
446 pi->MPSC_MPCR_m = v;
447 writel(v, pi->mpsc_base + MPSC_MPCR);
448 return;
449}
450
451static inline void
452mpsc_unfreeze(struct mpsc_port_info *pi)
453{
454 u32 v;
455
456 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
457 readl(pi->mpsc_base + MPSC_MPCR);
458 v &= ~MPSC_MPCR_FRZ;
459
460 if (pi->mirror_regs)
461 pi->MPSC_MPCR_m = v;
462 writel(v, pi->mpsc_base + MPSC_MPCR);
463
464 pr_debug("mpsc_unfreeze[%d]: Unfrozen\n", pi->port.line);
465 return;
466}
467
468static inline void
469mpsc_set_char_length(struct mpsc_port_info *pi, u32 len)
470{
471 u32 v;
472
473 pr_debug("mpsc_set_char_length[%d]: char len: %d\n", pi->port.line,len);
474
475 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
476 readl(pi->mpsc_base + MPSC_MPCR);
477 v = (v & ~(0x3 << 12)) | ((len & 0x3) << 12);
478
479 if (pi->mirror_regs)
480 pi->MPSC_MPCR_m = v;
481 writel(v, pi->mpsc_base + MPSC_MPCR);
482 return;
483}
484
485static inline void
486mpsc_set_stop_bit_length(struct mpsc_port_info *pi, u32 len)
487{
488 u32 v;
489
490 pr_debug("mpsc_set_stop_bit_length[%d]: stop bits: %d\n",
491 pi->port.line, len);
492
493 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
494 readl(pi->mpsc_base + MPSC_MPCR);
495
496 v = (v & ~(1 << 14)) | ((len & 0x1) << 14);
497
498 if (pi->mirror_regs)
499 pi->MPSC_MPCR_m = v;
500 writel(v, pi->mpsc_base + MPSC_MPCR);
501 return;
502}
503
504static inline void
505mpsc_set_parity(struct mpsc_port_info *pi, u32 p)
506{
507 u32 v;
508
509 pr_debug("mpsc_set_parity[%d]: parity bits: 0x%x\n", pi->port.line, p);
510
511 v = (pi->mirror_regs) ? pi->MPSC_CHR_2_m :
512 readl(pi->mpsc_base + MPSC_CHR_2);
513
514 p &= 0x3;
515 v = (v & ~0xc000c) | (p << 18) | (p << 2);
516
517 if (pi->mirror_regs)
518 pi->MPSC_CHR_2_m = v;
519 writel(v, pi->mpsc_base + MPSC_CHR_2);
520 return;
521}
522
523/*
524 ******************************************************************************
525 *
526 * Driver Init Routines
527 *
528 ******************************************************************************
529 */
530
531static void
532mpsc_init_hw(struct mpsc_port_info *pi)
533{
534 pr_debug("mpsc_init_hw[%d]: Initializing\n", pi->port.line);
535
536 mpsc_brg_init(pi, pi->brg_clk_src);
537 mpsc_brg_enable(pi);
538 mpsc_sdma_init(pi, dma_get_cache_alignment()); /* burst a cacheline */
539 mpsc_sdma_stop(pi);
540 mpsc_hw_init(pi);
541
542 return;
543}
544
545static int
546mpsc_alloc_ring_mem(struct mpsc_port_info *pi)
547{
548 int rc = 0;
549 static void mpsc_free_ring_mem(struct mpsc_port_info *pi);
550
551 pr_debug("mpsc_alloc_ring_mem[%d]: Allocating ring mem\n",
552 pi->port.line);
553
554 if (!pi->dma_region) {
555 if (!dma_supported(pi->port.dev, 0xffffffff)) {
556 printk(KERN_ERR "MPSC: Inadequate DMA support\n");
557 rc = -ENXIO;
558 }
559 else if ((pi->dma_region = dma_alloc_noncoherent(pi->port.dev,
560 MPSC_DMA_ALLOC_SIZE, &pi->dma_region_p, GFP_KERNEL))
561 == NULL) {
562
563 printk(KERN_ERR "MPSC: Can't alloc Desc region\n");
564 rc = -ENOMEM;
565 }
566 }
567
568 return rc;
569}
570
571static void
572mpsc_free_ring_mem(struct mpsc_port_info *pi)
573{
574 pr_debug("mpsc_free_ring_mem[%d]: Freeing ring mem\n", pi->port.line);
575
576 if (pi->dma_region) {
577 dma_free_noncoherent(pi->port.dev, MPSC_DMA_ALLOC_SIZE,
578 pi->dma_region, pi->dma_region_p);
579 pi->dma_region = NULL;
580 pi->dma_region_p = (dma_addr_t) NULL;
581 }
582
583 return;
584}
585
586static void
587mpsc_init_rings(struct mpsc_port_info *pi)
588{
589 struct mpsc_rx_desc *rxre;
590 struct mpsc_tx_desc *txre;
591 dma_addr_t dp, dp_p;
592 u8 *bp, *bp_p;
593 int i;
594
595 pr_debug("mpsc_init_rings[%d]: Initializing rings\n", pi->port.line);
596
597 BUG_ON(pi->dma_region == NULL);
598
599 memset(pi->dma_region, 0, MPSC_DMA_ALLOC_SIZE);
600
601 /*
602 * Descriptors & buffers are multiples of cacheline size and must be
603 * cacheline aligned.
604 */
605 dp = ALIGN((u32) pi->dma_region, dma_get_cache_alignment());
606 dp_p = ALIGN((u32) pi->dma_region_p, dma_get_cache_alignment());
607
608 /*
609 * Partition dma region into rx ring descriptor, rx buffers,
610 * tx ring descriptors, and tx buffers.
611 */
612 pi->rxr = dp;
613 pi->rxr_p = dp_p;
614 dp += MPSC_RXR_SIZE;
615 dp_p += MPSC_RXR_SIZE;
616
617 pi->rxb = (u8 *) dp;
618 pi->rxb_p = (u8 *) dp_p;
619 dp += MPSC_RXB_SIZE;
620 dp_p += MPSC_RXB_SIZE;
621
622 pi->rxr_posn = 0;
623
624 pi->txr = dp;
625 pi->txr_p = dp_p;
626 dp += MPSC_TXR_SIZE;
627 dp_p += MPSC_TXR_SIZE;
628
629 pi->txb = (u8 *) dp;
630 pi->txb_p = (u8 *) dp_p;
631
632 pi->txr_head = 0;
633 pi->txr_tail = 0;
634
635 /* Init rx ring descriptors */
636 dp = pi->rxr;
637 dp_p = pi->rxr_p;
638 bp = pi->rxb;
639 bp_p = pi->rxb_p;
640
641 for (i = 0; i < MPSC_RXR_ENTRIES; i++) {
642 rxre = (struct mpsc_rx_desc *)dp;
643
644 rxre->bufsize = cpu_to_be16(MPSC_RXBE_SIZE);
645 rxre->bytecnt = cpu_to_be16(0);
646 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
647 SDMA_DESC_CMDSTAT_EI |
648 SDMA_DESC_CMDSTAT_F |
649 SDMA_DESC_CMDSTAT_L);
650 rxre->link = cpu_to_be32(dp_p + MPSC_RXRE_SIZE);
651 rxre->buf_ptr = cpu_to_be32(bp_p);
652
653 dp += MPSC_RXRE_SIZE;
654 dp_p += MPSC_RXRE_SIZE;
655 bp += MPSC_RXBE_SIZE;
656 bp_p += MPSC_RXBE_SIZE;
657 }
658 rxre->link = cpu_to_be32(pi->rxr_p); /* Wrap last back to first */
659
660 /* Init tx ring descriptors */
661 dp = pi->txr;
662 dp_p = pi->txr_p;
663 bp = pi->txb;
664 bp_p = pi->txb_p;
665
666 for (i = 0; i < MPSC_TXR_ENTRIES; i++) {
667 txre = (struct mpsc_tx_desc *)dp;
668
669 txre->link = cpu_to_be32(dp_p + MPSC_TXRE_SIZE);
670 txre->buf_ptr = cpu_to_be32(bp_p);
671
672 dp += MPSC_TXRE_SIZE;
673 dp_p += MPSC_TXRE_SIZE;
674 bp += MPSC_TXBE_SIZE;
675 bp_p += MPSC_TXBE_SIZE;
676 }
677 txre->link = cpu_to_be32(pi->txr_p); /* Wrap last back to first */
678
679 dma_cache_sync((void *) pi->dma_region, MPSC_DMA_ALLOC_SIZE,
680 DMA_BIDIRECTIONAL);
681#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
682 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
683 flush_dcache_range((ulong)pi->dma_region,
684 (ulong)pi->dma_region + MPSC_DMA_ALLOC_SIZE);
685#endif
686
687 return;
688}
689
690static void
691mpsc_uninit_rings(struct mpsc_port_info *pi)
692{
693 pr_debug("mpsc_uninit_rings[%d]: Uninitializing rings\n",pi->port.line);
694
695 BUG_ON(pi->dma_region == NULL);
696
697 pi->rxr = 0;
698 pi->rxr_p = 0;
699 pi->rxb = NULL;
700 pi->rxb_p = NULL;
701 pi->rxr_posn = 0;
702
703 pi->txr = 0;
704 pi->txr_p = 0;
705 pi->txb = NULL;
706 pi->txb_p = NULL;
707 pi->txr_head = 0;
708 pi->txr_tail = 0;
709
710 return;
711}
712
713static int
714mpsc_make_ready(struct mpsc_port_info *pi)
715{
716 int rc;
717
718 pr_debug("mpsc_make_ready[%d]: Making cltr ready\n", pi->port.line);
719
720 if (!pi->ready) {
721 mpsc_init_hw(pi);
722 if ((rc = mpsc_alloc_ring_mem(pi)))
723 return rc;
724 mpsc_init_rings(pi);
725 pi->ready = 1;
726 }
727
728 return 0;
729}
730
731/*
732 ******************************************************************************
733 *
734 * Interrupt Handling Routines
735 *
736 ******************************************************************************
737 */
738
739static inline int
740mpsc_rx_intr(struct mpsc_port_info *pi, struct pt_regs *regs)
741{
742 struct mpsc_rx_desc *rxre;
743 struct tty_struct *tty = pi->port.info->tty;
744 u32 cmdstat, bytes_in, i;
745 int rc = 0;
746 u8 *bp;
747 char flag = TTY_NORMAL;
748 static void mpsc_start_rx(struct mpsc_port_info *pi);
749
750 pr_debug("mpsc_rx_intr[%d]: Handling Rx intr\n", pi->port.line);
751
752 rxre = (struct mpsc_rx_desc *)(pi->rxr + (pi->rxr_posn*MPSC_RXRE_SIZE));
753
754 dma_cache_sync((void *)rxre, MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
755#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
756 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
757 invalidate_dcache_range((ulong)rxre,
758 (ulong)rxre + MPSC_RXRE_SIZE);
759#endif
760
761 /*
762 * Loop through Rx descriptors handling ones that have been completed.
763 */
764 while (!((cmdstat = be32_to_cpu(rxre->cmdstat)) & SDMA_DESC_CMDSTAT_O)){
765 bytes_in = be16_to_cpu(rxre->bytecnt);
766
767 /* Following use of tty struct directly is deprecated */
768 if (unlikely((tty->flip.count + bytes_in) >= TTY_FLIPBUF_SIZE)){
769 if (tty->low_latency)
770 tty_flip_buffer_push(tty);
771 /*
772 * If this failed then we will throw awa the bytes
773 * but mst do so to clear interrupts.
774 */
775 }
776
777 bp = pi->rxb + (pi->rxr_posn * MPSC_RXBE_SIZE);
778 dma_cache_sync((void *) bp, MPSC_RXBE_SIZE, DMA_FROM_DEVICE);
779#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
780 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
781 invalidate_dcache_range((ulong)bp,
782 (ulong)bp + MPSC_RXBE_SIZE);
783#endif
784
785 /*
786 * Other than for parity error, the manual provides little
787 * info on what data will be in a frame flagged by any of
788 * these errors. For parity error, it is the last byte in
789 * the buffer that had the error. As for the rest, I guess
790 * we'll assume there is no data in the buffer.
791 * If there is...it gets lost.
792 */
793 if (unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
794 SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) {
795
796 pi->port.icount.rx++;
797
798 if (cmdstat & SDMA_DESC_CMDSTAT_BR) { /* Break */
799 pi->port.icount.brk++;
800
801 if (uart_handle_break(&pi->port))
802 goto next_frame;
803 }
804 else if (cmdstat & SDMA_DESC_CMDSTAT_FR)/* Framing */
805 pi->port.icount.frame++;
806 else if (cmdstat & SDMA_DESC_CMDSTAT_OR) /* Overrun */
807 pi->port.icount.overrun++;
808
809 cmdstat &= pi->port.read_status_mask;
810
811 if (cmdstat & SDMA_DESC_CMDSTAT_BR)
812 flag = TTY_BREAK;
813 else if (cmdstat & SDMA_DESC_CMDSTAT_FR)
814 flag = TTY_FRAME;
815 else if (cmdstat & SDMA_DESC_CMDSTAT_OR)
816 flag = TTY_OVERRUN;
817 else if (cmdstat & SDMA_DESC_CMDSTAT_PE)
818 flag = TTY_PARITY;
819 }
820
821 if (uart_handle_sysrq_char(&pi->port, *bp, regs)) {
822 bp++;
823 bytes_in--;
824 goto next_frame;
825 }
826
827 if ((unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
828 SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) &&
829 !(cmdstat & pi->port.ignore_status_mask))
830
831 tty_insert_flip_char(tty, *bp, flag);
832 else {
833 for (i=0; i<bytes_in; i++)
834 tty_insert_flip_char(tty, *bp++, TTY_NORMAL);
835
836 pi->port.icount.rx += bytes_in;
837 }
838
839next_frame:
840 rxre->bytecnt = cpu_to_be16(0);
841 wmb();
842 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
843 SDMA_DESC_CMDSTAT_EI |
844 SDMA_DESC_CMDSTAT_F |
845 SDMA_DESC_CMDSTAT_L);
846 wmb();
847 dma_cache_sync((void *)rxre, MPSC_RXRE_SIZE, DMA_BIDIRECTIONAL);
848#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
849 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
850 flush_dcache_range((ulong)rxre,
851 (ulong)rxre + MPSC_RXRE_SIZE);
852#endif
853
854 /* Advance to next descriptor */
855 pi->rxr_posn = (pi->rxr_posn + 1) & (MPSC_RXR_ENTRIES - 1);
856 rxre = (struct mpsc_rx_desc *)(pi->rxr +
857 (pi->rxr_posn * MPSC_RXRE_SIZE));
858 dma_cache_sync((void *)rxre, MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
859#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
860 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
861 invalidate_dcache_range((ulong)rxre,
862 (ulong)rxre + MPSC_RXRE_SIZE);
863#endif
864
865 rc = 1;
866 }
867
868 /* Restart rx engine, if its stopped */
869 if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0)
870 mpsc_start_rx(pi);
871
872 tty_flip_buffer_push(tty);
873 return rc;
874}
875
876static inline void
877mpsc_setup_tx_desc(struct mpsc_port_info *pi, u32 count, u32 intr)
878{
879 struct mpsc_tx_desc *txre;
880
881 txre = (struct mpsc_tx_desc *)(pi->txr +
882 (pi->txr_head * MPSC_TXRE_SIZE));
883
884 txre->bytecnt = cpu_to_be16(count);
885 txre->shadow = txre->bytecnt;
886 wmb(); /* ensure cmdstat is last field updated */
887 txre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O | SDMA_DESC_CMDSTAT_F |
888 SDMA_DESC_CMDSTAT_L | ((intr) ?
889 SDMA_DESC_CMDSTAT_EI
890 : 0));
891 wmb();
892 dma_cache_sync((void *) txre, MPSC_TXRE_SIZE, DMA_BIDIRECTIONAL);
893#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
894 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
895 flush_dcache_range((ulong)txre,
896 (ulong)txre + MPSC_TXRE_SIZE);
897#endif
898
899 return;
900}
901
902static inline void
903mpsc_copy_tx_data(struct mpsc_port_info *pi)
904{
905 struct circ_buf *xmit = &pi->port.info->xmit;
906 u8 *bp;
907 u32 i;
908
909 /* Make sure the desc ring isn't full */
910 while (CIRC_CNT(pi->txr_head, pi->txr_tail, MPSC_TXR_ENTRIES) <
911 (MPSC_TXR_ENTRIES - 1)) {
912 if (pi->port.x_char) {
913 /*
914 * Ideally, we should use the TCS field in
915 * CHR_1 to put the x_char out immediately but
916 * errata prevents us from being able to read
917 * CHR_2 to know that its safe to write to
918 * CHR_1. Instead, just put it in-band with
919 * all the other Tx data.
920 */
921 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
922 *bp = pi->port.x_char;
923 pi->port.x_char = 0;
924 i = 1;
925 }
926 else if (!uart_circ_empty(xmit) && !uart_tx_stopped(&pi->port)){
927 i = min((u32) MPSC_TXBE_SIZE,
928 (u32) uart_circ_chars_pending(xmit));
929 i = min(i, (u32) CIRC_CNT_TO_END(xmit->head, xmit->tail,
930 UART_XMIT_SIZE));
931 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
932 memcpy(bp, &xmit->buf[xmit->tail], i);
933 xmit->tail = (xmit->tail + i) & (UART_XMIT_SIZE - 1);
934
935 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
936 uart_write_wakeup(&pi->port);
937 }
938 else /* All tx data copied into ring bufs */
939 return;
940
941 dma_cache_sync((void *) bp, MPSC_TXBE_SIZE, DMA_BIDIRECTIONAL);
942#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
943 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
944 flush_dcache_range((ulong)bp,
945 (ulong)bp + MPSC_TXBE_SIZE);
946#endif
947 mpsc_setup_tx_desc(pi, i, 1);
948
949 /* Advance to next descriptor */
950 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
951 }
952
953 return;
954}
955
956static inline int
957mpsc_tx_intr(struct mpsc_port_info *pi)
958{
959 struct mpsc_tx_desc *txre;
960 int rc = 0;
961
962 if (!mpsc_sdma_tx_active(pi)) {
963 txre = (struct mpsc_tx_desc *)(pi->txr +
964 (pi->txr_tail * MPSC_TXRE_SIZE));
965
966 dma_cache_sync((void *) txre, MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
967#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
968 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
969 invalidate_dcache_range((ulong)txre,
970 (ulong)txre + MPSC_TXRE_SIZE);
971#endif
972
973 while (!(be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O)) {
974 rc = 1;
975 pi->port.icount.tx += be16_to_cpu(txre->bytecnt);
976 pi->txr_tail = (pi->txr_tail+1) & (MPSC_TXR_ENTRIES-1);
977
978 /* If no more data to tx, fall out of loop */
979 if (pi->txr_head == pi->txr_tail)
980 break;
981
982 txre = (struct mpsc_tx_desc *)(pi->txr +
983 (pi->txr_tail * MPSC_TXRE_SIZE));
984 dma_cache_sync((void *) txre, MPSC_TXRE_SIZE,
985 DMA_FROM_DEVICE);
986#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
987 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
988 invalidate_dcache_range((ulong)txre,
989 (ulong)txre + MPSC_TXRE_SIZE);
990#endif
991 }
992
993 mpsc_copy_tx_data(pi);
994 mpsc_sdma_start_tx(pi); /* start next desc if ready */
995 }
996
997 return rc;
998}
999
1000/*
1001 * This is the driver's interrupt handler. To avoid a race, we first clear
1002 * the interrupt, then handle any completed Rx/Tx descriptors. When done
1003 * handling those descriptors, we restart the Rx/Tx engines if they're stopped.
1004 */
1005static irqreturn_t
1006mpsc_sdma_intr(int irq, void *dev_id, struct pt_regs *regs)
1007{
1008 struct mpsc_port_info *pi = dev_id;
1009 ulong iflags;
1010 int rc = IRQ_NONE;
1011
1012 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Received\n",pi->port.line);
1013
1014 spin_lock_irqsave(&pi->port.lock, iflags);
1015 mpsc_sdma_intr_ack(pi);
1016 if (mpsc_rx_intr(pi, regs))
1017 rc = IRQ_HANDLED;
1018 if (mpsc_tx_intr(pi))
1019 rc = IRQ_HANDLED;
1020 spin_unlock_irqrestore(&pi->port.lock, iflags);
1021
1022 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Handled\n", pi->port.line);
1023 return rc;
1024}
1025
1026/*
1027 ******************************************************************************
1028 *
1029 * serial_core.c Interface routines
1030 *
1031 ******************************************************************************
1032 */
1033static uint
1034mpsc_tx_empty(struct uart_port *port)
1035{
1036 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1037 ulong iflags;
1038 uint rc;
1039
1040 spin_lock_irqsave(&pi->port.lock, iflags);
1041 rc = mpsc_sdma_tx_active(pi) ? 0 : TIOCSER_TEMT;
1042 spin_unlock_irqrestore(&pi->port.lock, iflags);
1043
1044 return rc;
1045}
1046
1047static void
1048mpsc_set_mctrl(struct uart_port *port, uint mctrl)
1049{
1050 /* Have no way to set modem control lines AFAICT */
1051 return;
1052}
1053
1054static uint
1055mpsc_get_mctrl(struct uart_port *port)
1056{
1057 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1058 u32 mflags, status;
1059 ulong iflags;
1060
1061 spin_lock_irqsave(&pi->port.lock, iflags);
1062 status = (pi->mirror_regs) ? pi->MPSC_CHR_10_m :
1063 readl(pi->mpsc_base + MPSC_CHR_10);
1064 spin_unlock_irqrestore(&pi->port.lock, iflags);
1065
1066 mflags = 0;
1067 if (status & 0x1)
1068 mflags |= TIOCM_CTS;
1069 if (status & 0x2)
1070 mflags |= TIOCM_CAR;
1071
1072 return mflags | TIOCM_DSR; /* No way to tell if DSR asserted */
1073}
1074
1075static void
1076mpsc_stop_tx(struct uart_port *port, uint tty_start)
1077{
1078 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1079
1080 pr_debug("mpsc_stop_tx[%d]: tty_start: %d\n", port->line, tty_start);
1081
1082 mpsc_freeze(pi);
1083 return;
1084}
1085
1086static void
1087mpsc_start_tx(struct uart_port *port, uint tty_start)
1088{
1089 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1090
1091 mpsc_unfreeze(pi);
1092 mpsc_copy_tx_data(pi);
1093 mpsc_sdma_start_tx(pi);
1094
1095 pr_debug("mpsc_start_tx[%d]: tty_start: %d\n", port->line, tty_start);
1096 return;
1097}
1098
1099static void
1100mpsc_start_rx(struct mpsc_port_info *pi)
1101{
1102 pr_debug("mpsc_start_rx[%d]: Starting...\n", pi->port.line);
1103
1104 if (pi->rcv_data) {
1105 mpsc_enter_hunt(pi);
1106 mpsc_sdma_cmd(pi, SDMA_SDCM_ERD);
1107 }
1108 return;
1109}
1110
1111static void
1112mpsc_stop_rx(struct uart_port *port)
1113{
1114 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1115
1116 pr_debug("mpsc_stop_rx[%d]: Stopping...\n", port->line);
1117
1118 mpsc_sdma_cmd(pi, SDMA_SDCM_AR);
1119 return;
1120}
1121
1122static void
1123mpsc_enable_ms(struct uart_port *port)
1124{
1125 return; /* Not supported */
1126}
1127
1128static void
1129mpsc_break_ctl(struct uart_port *port, int ctl)
1130{
1131 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1132 ulong flags;
1133 u32 v;
1134
1135 v = ctl ? 0x00ff0000 : 0;
1136
1137 spin_lock_irqsave(&pi->port.lock, flags);
1138 if (pi->mirror_regs)
1139 pi->MPSC_CHR_1_m = v;
1140 writel(v, pi->mpsc_base + MPSC_CHR_1);
1141 spin_unlock_irqrestore(&pi->port.lock, flags);
1142
1143 return;
1144}
1145
1146static int
1147mpsc_startup(struct uart_port *port)
1148{
1149 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1150 u32 flag = 0;
1151 int rc;
1152
1153 pr_debug("mpsc_startup[%d]: Starting up MPSC, irq: %d\n",
1154 port->line, pi->port.irq);
1155
1156 if ((rc = mpsc_make_ready(pi)) == 0) {
1157 /* Setup IRQ handler */
1158 mpsc_sdma_intr_ack(pi);
1159
1160 /* If irq's are shared, need to set flag */
1161 if (mpsc_ports[0].port.irq == mpsc_ports[1].port.irq)
1162 flag = SA_SHIRQ;
1163
1164 if (request_irq(pi->port.irq, mpsc_sdma_intr, flag,
1165 "mpsc/sdma", pi))
1166 printk(KERN_ERR "MPSC: Can't get SDMA IRQ %d\n",
1167 pi->port.irq);
1168
1169 mpsc_sdma_intr_unmask(pi, 0xf);
1170 mpsc_sdma_set_rx_ring(pi, (struct mpsc_rx_desc *)(pi->rxr_p +
1171 (pi->rxr_posn * MPSC_RXRE_SIZE)));
1172 }
1173
1174 return rc;
1175}
1176
1177static void
1178mpsc_shutdown(struct uart_port *port)
1179{
1180 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1181 static void mpsc_release_port(struct uart_port *port);
1182
1183 pr_debug("mpsc_shutdown[%d]: Shutting down MPSC\n", port->line);
1184
1185 mpsc_sdma_stop(pi);
1186 free_irq(pi->port.irq, pi);
1187 return;
1188}
1189
1190static void
1191mpsc_set_termios(struct uart_port *port, struct termios *termios,
1192 struct termios *old)
1193{
1194 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1195 u32 baud;
1196 ulong flags;
1197 u32 chr_bits, stop_bits, par;
1198
1199 pi->c_iflag = termios->c_iflag;
1200 pi->c_cflag = termios->c_cflag;
1201
1202 switch (termios->c_cflag & CSIZE) {
1203 case CS5:
1204 chr_bits = MPSC_MPCR_CL_5;
1205 break;
1206 case CS6:
1207 chr_bits = MPSC_MPCR_CL_6;
1208 break;
1209 case CS7:
1210 chr_bits = MPSC_MPCR_CL_7;
1211 break;
1212 case CS8:
1213 default:
1214 chr_bits = MPSC_MPCR_CL_8;
1215 break;
1216 }
1217
1218 if (termios->c_cflag & CSTOPB)
1219 stop_bits = MPSC_MPCR_SBL_2;
1220 else
1221 stop_bits = MPSC_MPCR_SBL_1;
1222
1223 par = MPSC_CHR_2_PAR_EVEN;
1224 if (termios->c_cflag & PARENB)
1225 if (termios->c_cflag & PARODD)
1226 par = MPSC_CHR_2_PAR_ODD;
1227#ifdef CMSPAR
1228 if (termios->c_cflag & CMSPAR) {
1229 if (termios->c_cflag & PARODD)
1230 par = MPSC_CHR_2_PAR_MARK;
1231 else
1232 par = MPSC_CHR_2_PAR_SPACE;
1233 }
1234#endif
1235
1236 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk);
1237
1238 spin_lock_irqsave(&pi->port.lock, flags);
1239
1240 uart_update_timeout(port, termios->c_cflag, baud);
1241
1242 mpsc_set_char_length(pi, chr_bits);
1243 mpsc_set_stop_bit_length(pi, stop_bits);
1244 mpsc_set_parity(pi, par);
1245 mpsc_set_baudrate(pi, baud);
1246
1247 /* Characters/events to read */
1248 pi->rcv_data = 1;
1249 pi->port.read_status_mask = SDMA_DESC_CMDSTAT_OR;
1250
1251 if (termios->c_iflag & INPCK)
1252 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_PE |
1253 SDMA_DESC_CMDSTAT_FR;
1254
1255 if (termios->c_iflag & (BRKINT | PARMRK))
1256 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_BR;
1257
1258 /* Characters/events to ignore */
1259 pi->port.ignore_status_mask = 0;
1260
1261 if (termios->c_iflag & IGNPAR)
1262 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_PE |
1263 SDMA_DESC_CMDSTAT_FR;
1264
1265 if (termios->c_iflag & IGNBRK) {
1266 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_BR;
1267
1268 if (termios->c_iflag & IGNPAR)
1269 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_OR;
1270 }
1271
1272 /* Ignore all chars if CREAD not set */
1273 if (!(termios->c_cflag & CREAD))
1274 pi->rcv_data = 0;
1275 else
1276 mpsc_start_rx(pi);
1277
1278 spin_unlock_irqrestore(&pi->port.lock, flags);
1279 return;
1280}
1281
1282static const char *
1283mpsc_type(struct uart_port *port)
1284{
1285 pr_debug("mpsc_type[%d]: port type: %s\n", port->line,MPSC_DRIVER_NAME);
1286 return MPSC_DRIVER_NAME;
1287}
1288
1289static int
1290mpsc_request_port(struct uart_port *port)
1291{
1292 /* Should make chip/platform specific call */
1293 return 0;
1294}
1295
1296static void
1297mpsc_release_port(struct uart_port *port)
1298{
1299 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1300
1301 if (pi->ready) {
1302 mpsc_uninit_rings(pi);
1303 mpsc_free_ring_mem(pi);
1304 pi->ready = 0;
1305 }
1306
1307 return;
1308}
1309
1310static void
1311mpsc_config_port(struct uart_port *port, int flags)
1312{
1313 return;
1314}
1315
1316static int
1317mpsc_verify_port(struct uart_port *port, struct serial_struct *ser)
1318{
1319 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1320 int rc = 0;
1321
1322 pr_debug("mpsc_verify_port[%d]: Verifying port data\n", pi->port.line);
1323
1324 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPSC)
1325 rc = -EINVAL;
1326 else if (pi->port.irq != ser->irq)
1327 rc = -EINVAL;
1328 else if (ser->io_type != SERIAL_IO_MEM)
1329 rc = -EINVAL;
1330 else if (pi->port.uartclk / 16 != ser->baud_base) /* Not sure */
1331 rc = -EINVAL;
1332 else if ((void *)pi->port.mapbase != ser->iomem_base)
1333 rc = -EINVAL;
1334 else if (pi->port.iobase != ser->port)
1335 rc = -EINVAL;
1336 else if (ser->hub6 != 0)
1337 rc = -EINVAL;
1338
1339 return rc;
1340}
1341
1342static struct uart_ops mpsc_pops = {
1343 .tx_empty = mpsc_tx_empty,
1344 .set_mctrl = mpsc_set_mctrl,
1345 .get_mctrl = mpsc_get_mctrl,
1346 .stop_tx = mpsc_stop_tx,
1347 .start_tx = mpsc_start_tx,
1348 .stop_rx = mpsc_stop_rx,
1349 .enable_ms = mpsc_enable_ms,
1350 .break_ctl = mpsc_break_ctl,
1351 .startup = mpsc_startup,
1352 .shutdown = mpsc_shutdown,
1353 .set_termios = mpsc_set_termios,
1354 .type = mpsc_type,
1355 .release_port = mpsc_release_port,
1356 .request_port = mpsc_request_port,
1357 .config_port = mpsc_config_port,
1358 .verify_port = mpsc_verify_port,
1359};
1360
1361/*
1362 ******************************************************************************
1363 *
1364 * Console Interface Routines
1365 *
1366 ******************************************************************************
1367 */
1368
1369#ifdef CONFIG_SERIAL_MPSC_CONSOLE
1370static void
1371mpsc_console_write(struct console *co, const char *s, uint count)
1372{
1373 struct mpsc_port_info *pi = &mpsc_ports[co->index];
1374 u8 *bp, *dp, add_cr = 0;
1375 int i;
1376
1377 while (mpsc_sdma_tx_active(pi))
1378 udelay(100);
1379
1380 while (count > 0) {
1381 bp = dp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1382
1383 for (i = 0; i < MPSC_TXBE_SIZE; i++) {
1384 if (count == 0)
1385 break;
1386
1387 if (add_cr) {
1388 *(dp++) = '\r';
1389 add_cr = 0;
1390 }
1391 else {
1392 *(dp++) = *s;
1393
1394 if (*(s++) == '\n') { /* add '\r' after '\n' */
1395 add_cr = 1;
1396 count++;
1397 }
1398 }
1399
1400 count--;
1401 }
1402
1403 dma_cache_sync((void *) bp, MPSC_TXBE_SIZE, DMA_BIDIRECTIONAL);
1404#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1405 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1406 flush_dcache_range((ulong)bp,
1407 (ulong)bp + MPSC_TXBE_SIZE);
1408#endif
1409 mpsc_setup_tx_desc(pi, i, 0);
1410 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
1411 mpsc_sdma_start_tx(pi);
1412
1413 while (mpsc_sdma_tx_active(pi))
1414 udelay(100);
1415
1416 pi->txr_tail = (pi->txr_tail + 1) & (MPSC_TXR_ENTRIES - 1);
1417 }
1418
1419 return;
1420}
1421
1422static int __init
1423mpsc_console_setup(struct console *co, char *options)
1424{
1425 struct mpsc_port_info *pi;
1426 int baud, bits, parity, flow;
1427
1428 pr_debug("mpsc_console_setup[%d]: options: %s\n", co->index, options);
1429
1430 if (co->index >= MPSC_NUM_CTLRS)
1431 co->index = 0;
1432
1433 pi = &mpsc_ports[co->index];
1434
1435 baud = pi->default_baud;
1436 bits = pi->default_bits;
1437 parity = pi->default_parity;
1438 flow = pi->default_flow;
1439
1440 if (!pi->port.ops)
1441 return -ENODEV;
1442
1443 spin_lock_init(&pi->port.lock); /* Temporary fix--copied from 8250.c */
1444
1445 if (options)
1446 uart_parse_options(options, &baud, &parity, &bits, &flow);
1447
1448 return uart_set_options(&pi->port, co, baud, parity, bits, flow);
1449}
1450
1451extern struct uart_driver mpsc_reg;
1452static struct console mpsc_console = {
1453 .name = MPSC_DEV_NAME,
1454 .write = mpsc_console_write,
1455 .device = uart_console_device,
1456 .setup = mpsc_console_setup,
1457 .flags = CON_PRINTBUFFER,
1458 .index = -1,
1459 .data = &mpsc_reg,
1460};
1461
1462static int __init
1463mpsc_late_console_init(void)
1464{
1465 pr_debug("mpsc_late_console_init: Enter\n");
1466
1467 if (!(mpsc_console.flags & CON_ENABLED))
1468 register_console(&mpsc_console);
1469 return 0;
1470}
1471
1472late_initcall(mpsc_late_console_init);
1473
1474#define MPSC_CONSOLE &mpsc_console
1475#else
1476#define MPSC_CONSOLE NULL
1477#endif
1478/*
1479 ******************************************************************************
1480 *
1481 * Dummy Platform Driver to extract & map shared register regions
1482 *
1483 ******************************************************************************
1484 */
1485static void
1486mpsc_resource_err(char *s)
1487{
1488 printk(KERN_WARNING "MPSC: Platform device resource error in %s\n", s);
1489 return;
1490}
1491
1492static int
1493mpsc_shared_map_regs(struct platform_device *pd)
1494{
1495 struct resource *r;
1496
1497 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1498 MPSC_ROUTING_BASE_ORDER)) && request_mem_region(r->start,
1499 MPSC_ROUTING_REG_BLOCK_SIZE, "mpsc_routing_regs")) {
1500
1501 mpsc_shared_regs.mpsc_routing_base = ioremap(r->start,
1502 MPSC_ROUTING_REG_BLOCK_SIZE);
1503 mpsc_shared_regs.mpsc_routing_base_p = r->start;
1504 }
1505 else {
1506 mpsc_resource_err("MPSC routing base");
1507 return -ENOMEM;
1508 }
1509
1510 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1511 MPSC_SDMA_INTR_BASE_ORDER)) && request_mem_region(r->start,
1512 MPSC_SDMA_INTR_REG_BLOCK_SIZE, "sdma_intr_regs")) {
1513
1514 mpsc_shared_regs.sdma_intr_base = ioremap(r->start,
1515 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1516 mpsc_shared_regs.sdma_intr_base_p = r->start;
1517 }
1518 else {
1519 iounmap(mpsc_shared_regs.mpsc_routing_base);
1520 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1521 MPSC_ROUTING_REG_BLOCK_SIZE);
1522 mpsc_resource_err("SDMA intr base");
1523 return -ENOMEM;
1524 }
1525
1526 return 0;
1527}
1528
1529static void
1530mpsc_shared_unmap_regs(void)
1531{
1532 if (!mpsc_shared_regs.mpsc_routing_base) {
1533 iounmap(mpsc_shared_regs.mpsc_routing_base);
1534 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1535 MPSC_ROUTING_REG_BLOCK_SIZE);
1536 }
1537 if (!mpsc_shared_regs.sdma_intr_base) {
1538 iounmap(mpsc_shared_regs.sdma_intr_base);
1539 release_mem_region(mpsc_shared_regs.sdma_intr_base_p,
1540 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1541 }
1542
1543 mpsc_shared_regs.mpsc_routing_base = 0;
1544 mpsc_shared_regs.sdma_intr_base = 0;
1545
1546 mpsc_shared_regs.mpsc_routing_base_p = 0;
1547 mpsc_shared_regs.sdma_intr_base_p = 0;
1548
1549 return;
1550}
1551
1552static int
1553mpsc_shared_drv_probe(struct device *dev)
1554{
1555 struct platform_device *pd = to_platform_device(dev);
1556 struct mpsc_shared_pdata *pdata;
1557 int rc = -ENODEV;
1558
1559 if (pd->id == 0) {
1560 if (!(rc = mpsc_shared_map_regs(pd))) {
1561 pdata = (struct mpsc_shared_pdata *)dev->platform_data;
1562
1563 mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val;
1564 mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val;
1565 mpsc_shared_regs.MPSC_TCRR_m= pdata->tcrr_val;
1566 mpsc_shared_regs.SDMA_INTR_CAUSE_m =
1567 pdata->intr_cause_val;
1568 mpsc_shared_regs.SDMA_INTR_MASK_m =
1569 pdata->intr_mask_val;
1570
1571 rc = 0;
1572 }
1573 }
1574
1575 return rc;
1576}
1577
1578static int
1579mpsc_shared_drv_remove(struct device *dev)
1580{
1581 struct platform_device *pd = to_platform_device(dev);
1582 int rc = -ENODEV;
1583
1584 if (pd->id == 0) {
1585 mpsc_shared_unmap_regs();
1586 mpsc_shared_regs.MPSC_MRR_m = 0;
1587 mpsc_shared_regs.MPSC_RCRR_m = 0;
1588 mpsc_shared_regs.MPSC_TCRR_m = 0;
1589 mpsc_shared_regs.SDMA_INTR_CAUSE_m = 0;
1590 mpsc_shared_regs.SDMA_INTR_MASK_m = 0;
1591 rc = 0;
1592 }
1593
1594 return rc;
1595}
1596
1597static struct device_driver mpsc_shared_driver = {
1598 .name = MPSC_SHARED_NAME,
1599 .bus = &platform_bus_type,
1600 .probe = mpsc_shared_drv_probe,
1601 .remove = mpsc_shared_drv_remove,
1602};
1603
1604/*
1605 ******************************************************************************
1606 *
1607 * Driver Interface Routines
1608 *
1609 ******************************************************************************
1610 */
1611static struct uart_driver mpsc_reg = {
1612 .owner = THIS_MODULE,
1613 .driver_name = MPSC_DRIVER_NAME,
1614 .devfs_name = MPSC_DEVFS_NAME,
1615 .dev_name = MPSC_DEV_NAME,
1616 .major = MPSC_MAJOR,
1617 .minor = MPSC_MINOR_START,
1618 .nr = MPSC_NUM_CTLRS,
1619 .cons = MPSC_CONSOLE,
1620};
1621
1622static int
1623mpsc_drv_map_regs(struct mpsc_port_info *pi, struct platform_device *pd)
1624{
1625 struct resource *r;
1626
1627 if ((r = platform_get_resource(pd, IORESOURCE_MEM, MPSC_BASE_ORDER)) &&
1628 request_mem_region(r->start, MPSC_REG_BLOCK_SIZE, "mpsc_regs")){
1629
1630 pi->mpsc_base = ioremap(r->start, MPSC_REG_BLOCK_SIZE);
1631 pi->mpsc_base_p = r->start;
1632 }
1633 else {
1634 mpsc_resource_err("MPSC base");
1635 return -ENOMEM;
1636 }
1637
1638 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1639 MPSC_SDMA_BASE_ORDER)) && request_mem_region(r->start,
1640 MPSC_SDMA_REG_BLOCK_SIZE, "sdma_regs")) {
1641
1642 pi->sdma_base = ioremap(r->start,MPSC_SDMA_REG_BLOCK_SIZE);
1643 pi->sdma_base_p = r->start;
1644 }
1645 else {
1646 mpsc_resource_err("SDMA base");
1647 return -ENOMEM;
1648 }
1649
1650 if ((r = platform_get_resource(pd,IORESOURCE_MEM,MPSC_BRG_BASE_ORDER))
1651 && request_mem_region(r->start, MPSC_BRG_REG_BLOCK_SIZE,
1652 "brg_regs")) {
1653
1654 pi->brg_base = ioremap(r->start, MPSC_BRG_REG_BLOCK_SIZE);
1655 pi->brg_base_p = r->start;
1656 }
1657 else {
1658 mpsc_resource_err("BRG base");
1659 return -ENOMEM;
1660 }
1661
1662 return 0;
1663}
1664
1665static void
1666mpsc_drv_unmap_regs(struct mpsc_port_info *pi)
1667{
1668 if (!pi->mpsc_base) {
1669 iounmap(pi->mpsc_base);
1670 release_mem_region(pi->mpsc_base_p, MPSC_REG_BLOCK_SIZE);
1671 }
1672 if (!pi->sdma_base) {
1673 iounmap(pi->sdma_base);
1674 release_mem_region(pi->sdma_base_p, MPSC_SDMA_REG_BLOCK_SIZE);
1675 }
1676 if (!pi->brg_base) {
1677 iounmap(pi->brg_base);
1678 release_mem_region(pi->brg_base_p, MPSC_BRG_REG_BLOCK_SIZE);
1679 }
1680
1681 pi->mpsc_base = 0;
1682 pi->sdma_base = 0;
1683 pi->brg_base = 0;
1684
1685 pi->mpsc_base_p = 0;
1686 pi->sdma_base_p = 0;
1687 pi->brg_base_p = 0;
1688
1689 return;
1690}
1691
1692static void
1693mpsc_drv_get_platform_data(struct mpsc_port_info *pi,
1694 struct platform_device *pd, int num)
1695{
1696 struct mpsc_pdata *pdata;
1697
1698 pdata = (struct mpsc_pdata *)pd->dev.platform_data;
1699
1700 pi->port.uartclk = pdata->brg_clk_freq;
1701 pi->port.iotype = UPIO_MEM;
1702 pi->port.line = num;
1703 pi->port.type = PORT_MPSC;
1704 pi->port.fifosize = MPSC_TXBE_SIZE;
1705 pi->port.membase = pi->mpsc_base;
1706 pi->port.mapbase = (ulong)pi->mpsc_base;
1707 pi->port.ops = &mpsc_pops;
1708
1709 pi->mirror_regs = pdata->mirror_regs;
1710 pi->cache_mgmt = pdata->cache_mgmt;
1711 pi->brg_can_tune = pdata->brg_can_tune;
1712 pi->brg_clk_src = pdata->brg_clk_src;
1713 pi->mpsc_max_idle = pdata->max_idle;
1714 pi->default_baud = pdata->default_baud;
1715 pi->default_bits = pdata->default_bits;
1716 pi->default_parity = pdata->default_parity;
1717 pi->default_flow = pdata->default_flow;
1718
1719 /* Initial values of mirrored regs */
1720 pi->MPSC_CHR_1_m = pdata->chr_1_val;
1721 pi->MPSC_CHR_2_m = pdata->chr_2_val;
1722 pi->MPSC_CHR_10_m = pdata->chr_10_val;
1723 pi->MPSC_MPCR_m = pdata->mpcr_val;
1724 pi->BRG_BCR_m = pdata->bcr_val;
1725
1726 pi->shared_regs = &mpsc_shared_regs;
1727
1728 pi->port.irq = platform_get_irq(pd, 0);
1729
1730 return;
1731}
1732
1733static int
1734mpsc_drv_probe(struct device *dev)
1735{
1736 struct platform_device *pd = to_platform_device(dev);
1737 struct mpsc_port_info *pi;
1738 int rc = -ENODEV;
1739
1740 pr_debug("mpsc_drv_probe: Adding MPSC %d\n", pd->id);
1741
1742 if (pd->id < MPSC_NUM_CTLRS) {
1743 pi = &mpsc_ports[pd->id];
1744
1745 if (!(rc = mpsc_drv_map_regs(pi, pd))) {
1746 mpsc_drv_get_platform_data(pi, pd, pd->id);
1747
1748 if (!(rc = mpsc_make_ready(pi)))
1749 if (!(rc = uart_add_one_port(&mpsc_reg,
1750 &pi->port)))
1751 rc = 0;
1752 else {
1753 mpsc_release_port(
1754 (struct uart_port *)pi);
1755 mpsc_drv_unmap_regs(pi);
1756 }
1757 else
1758 mpsc_drv_unmap_regs(pi);
1759 }
1760 }
1761
1762 return rc;
1763}
1764
1765static int
1766mpsc_drv_remove(struct device *dev)
1767{
1768 struct platform_device *pd = to_platform_device(dev);
1769
1770 pr_debug("mpsc_drv_exit: Removing MPSC %d\n", pd->id);
1771
1772 if (pd->id < MPSC_NUM_CTLRS) {
1773 uart_remove_one_port(&mpsc_reg, &mpsc_ports[pd->id].port);
1774 mpsc_release_port((struct uart_port *)&mpsc_ports[pd->id].port);
1775 mpsc_drv_unmap_regs(&mpsc_ports[pd->id]);
1776 return 0;
1777 }
1778 else
1779 return -ENODEV;
1780}
1781
1782static struct device_driver mpsc_driver = {
1783 .name = MPSC_CTLR_NAME,
1784 .bus = &platform_bus_type,
1785 .probe = mpsc_drv_probe,
1786 .remove = mpsc_drv_remove,
1787};
1788
1789static int __init
1790mpsc_drv_init(void)
1791{
1792 int rc;
1793
1794 printk(KERN_INFO "Serial: MPSC driver $Revision: 1.00 $\n");
1795
1796 memset(mpsc_ports, 0, sizeof(mpsc_ports));
1797 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
1798
1799 if (!(rc = uart_register_driver(&mpsc_reg))) {
1800 if (!(rc = driver_register(&mpsc_shared_driver))) {
1801 if ((rc = driver_register(&mpsc_driver))) {
1802 driver_unregister(&mpsc_shared_driver);
1803 uart_unregister_driver(&mpsc_reg);
1804 }
1805 }
1806 else
1807 uart_unregister_driver(&mpsc_reg);
1808 }
1809
1810 return rc;
1811
1812}
1813
1814static void __exit
1815mpsc_drv_exit(void)
1816{
1817 driver_unregister(&mpsc_driver);
1818 driver_unregister(&mpsc_shared_driver);
1819 uart_unregister_driver(&mpsc_reg);
1820 memset(mpsc_ports, 0, sizeof(mpsc_ports));
1821 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
1822 return;
1823}
1824
1825module_init(mpsc_drv_init);
1826module_exit(mpsc_drv_exit);
1827
1828MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
1829MODULE_DESCRIPTION("Generic Marvell MPSC serial/UART driver $Revision: 1.00 $");
1830MODULE_VERSION(MPSC_VERSION);
1831MODULE_LICENSE("GPL");
1832MODULE_ALIAS_CHARDEV_MAJOR(MPSC_MAJOR);
diff --git a/drivers/serial/mpsc.h b/drivers/serial/mpsc.h
new file mode 100644
index 000000000000..1f7294b7095f
--- /dev/null
+++ b/drivers/serial/mpsc.h
@@ -0,0 +1,289 @@
1/*
2 * drivers/serial/mpsc.h
3 *
4 * Author: Mark A. Greer <mgreer@mvista.com>
5 *
6 * 2004 (c) MontaVista, Software, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
9 * or implied.
10 */
11
12#ifndef __MPSC_H__
13#define __MPSC_H__
14
15#include <linux/config.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/tty.h>
19#include <linux/tty_flip.h>
20#include <linux/ioport.h>
21#include <linux/init.h>
22#include <linux/console.h>
23#include <linux/sysrq.h>
24#include <linux/serial.h>
25#include <linux/serial_core.h>
26#include <linux/delay.h>
27#include <linux/device.h>
28#include <linux/dma-mapping.h>
29#include <linux/mv643xx.h>
30
31#include <asm/io.h>
32#include <asm/irq.h>
33
34#if defined(CONFIG_SERIAL_MPSC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
35#define SUPPORT_SYSRQ
36#endif
37
38#define MPSC_NUM_CTLRS 2
39
40/*
41 * Descriptors and buffers must be cache line aligned.
42 * Buffers lengths must be multiple of cache line size.
43 * Number of Tx & Rx descriptors must be powers of 2.
44 */
45#define MPSC_RXR_ENTRIES 32
46#define MPSC_RXRE_SIZE dma_get_cache_alignment()
47#define MPSC_RXR_SIZE (MPSC_RXR_ENTRIES * MPSC_RXRE_SIZE)
48#define MPSC_RXBE_SIZE dma_get_cache_alignment()
49#define MPSC_RXB_SIZE (MPSC_RXR_ENTRIES * MPSC_RXBE_SIZE)
50
51#define MPSC_TXR_ENTRIES 32
52#define MPSC_TXRE_SIZE dma_get_cache_alignment()
53#define MPSC_TXR_SIZE (MPSC_TXR_ENTRIES * MPSC_TXRE_SIZE)
54#define MPSC_TXBE_SIZE dma_get_cache_alignment()
55#define MPSC_TXB_SIZE (MPSC_TXR_ENTRIES * MPSC_TXBE_SIZE)
56
57#define MPSC_DMA_ALLOC_SIZE (MPSC_RXR_SIZE + MPSC_RXB_SIZE + \
58 MPSC_TXR_SIZE + MPSC_TXB_SIZE + \
59 dma_get_cache_alignment() /* for alignment */)
60
61/* Rx and Tx Ring entry descriptors -- assume entry size is <= cacheline size */
62struct mpsc_rx_desc {
63 u16 bufsize;
64 u16 bytecnt;
65 u32 cmdstat;
66 u32 link;
67 u32 buf_ptr;
68} __attribute((packed));
69
70struct mpsc_tx_desc {
71 u16 bytecnt;
72 u16 shadow;
73 u32 cmdstat;
74 u32 link;
75 u32 buf_ptr;
76} __attribute((packed));
77
78/*
79 * Some regs that have the erratum that you can't read them are are shared
80 * between the two MPSC controllers. This struct contains those shared regs.
81 */
82struct mpsc_shared_regs {
83 phys_addr_t mpsc_routing_base_p;
84 phys_addr_t sdma_intr_base_p;
85
86 void *mpsc_routing_base;
87 void *sdma_intr_base;
88
89 u32 MPSC_MRR_m;
90 u32 MPSC_RCRR_m;
91 u32 MPSC_TCRR_m;
92 u32 SDMA_INTR_CAUSE_m;
93 u32 SDMA_INTR_MASK_m;
94};
95
96/* The main driver data structure */
97struct mpsc_port_info {
98 struct uart_port port; /* Overlay uart_port structure */
99
100 /* Internal driver state for this ctlr */
101 u8 ready;
102 u8 rcv_data;
103 tcflag_t c_iflag; /* save termios->c_iflag */
104 tcflag_t c_cflag; /* save termios->c_cflag */
105
106 /* Info passed in from platform */
107 u8 mirror_regs; /* Need to mirror regs? */
108 u8 cache_mgmt; /* Need manual cache mgmt? */
109 u8 brg_can_tune; /* BRG has baud tuning? */
110 u32 brg_clk_src;
111 u16 mpsc_max_idle;
112 int default_baud;
113 int default_bits;
114 int default_parity;
115 int default_flow;
116
117 /* Physical addresses of various blocks of registers (from platform) */
118 phys_addr_t mpsc_base_p;
119 phys_addr_t sdma_base_p;
120 phys_addr_t brg_base_p;
121
122 /* Virtual addresses of various blocks of registers (from platform) */
123 void *mpsc_base;
124 void *sdma_base;
125 void *brg_base;
126
127 /* Descriptor ring and buffer allocations */
128 void *dma_region;
129 dma_addr_t dma_region_p;
130
131 dma_addr_t rxr; /* Rx descriptor ring */
132 dma_addr_t rxr_p; /* Phys addr of rxr */
133 u8 *rxb; /* Rx Ring I/O buf */
134 u8 *rxb_p; /* Phys addr of rxb */
135 u32 rxr_posn; /* First desc w/ Rx data */
136
137 dma_addr_t txr; /* Tx descriptor ring */
138 dma_addr_t txr_p; /* Phys addr of txr */
139 u8 *txb; /* Tx Ring I/O buf */
140 u8 *txb_p; /* Phys addr of txb */
141 int txr_head; /* Where new data goes */
142 int txr_tail; /* Where sent data comes off */
143
144 /* Mirrored values of regs we can't read (if 'mirror_regs' set) */
145 u32 MPSC_MPCR_m;
146 u32 MPSC_CHR_1_m;
147 u32 MPSC_CHR_2_m;
148 u32 MPSC_CHR_10_m;
149 u32 BRG_BCR_m;
150 struct mpsc_shared_regs *shared_regs;
151};
152
153/* Hooks to platform-specific code */
154int mpsc_platform_register_driver(void);
155void mpsc_platform_unregister_driver(void);
156
157/* Hooks back in to mpsc common to be called by platform-specific code */
158struct mpsc_port_info *mpsc_device_probe(int index);
159struct mpsc_port_info *mpsc_device_remove(int index);
160
161/*
162 *****************************************************************************
163 *
164 * Multi-Protocol Serial Controller Interface Registers
165 *
166 *****************************************************************************
167 */
168
169/* Main Configuratino Register Offsets */
170#define MPSC_MMCRL 0x0000
171#define MPSC_MMCRH 0x0004
172#define MPSC_MPCR 0x0008
173#define MPSC_CHR_1 0x000c
174#define MPSC_CHR_2 0x0010
175#define MPSC_CHR_3 0x0014
176#define MPSC_CHR_4 0x0018
177#define MPSC_CHR_5 0x001c
178#define MPSC_CHR_6 0x0020
179#define MPSC_CHR_7 0x0024
180#define MPSC_CHR_8 0x0028
181#define MPSC_CHR_9 0x002c
182#define MPSC_CHR_10 0x0030
183#define MPSC_CHR_11 0x0034
184
185#define MPSC_MPCR_FRZ (1 << 9)
186#define MPSC_MPCR_CL_5 0
187#define MPSC_MPCR_CL_6 1
188#define MPSC_MPCR_CL_7 2
189#define MPSC_MPCR_CL_8 3
190#define MPSC_MPCR_SBL_1 0
191#define MPSC_MPCR_SBL_2 1
192
193#define MPSC_CHR_2_TEV (1<<1)
194#define MPSC_CHR_2_TA (1<<7)
195#define MPSC_CHR_2_TTCS (1<<9)
196#define MPSC_CHR_2_REV (1<<17)
197#define MPSC_CHR_2_RA (1<<23)
198#define MPSC_CHR_2_CRD (1<<25)
199#define MPSC_CHR_2_EH (1<<31)
200#define MPSC_CHR_2_PAR_ODD 0
201#define MPSC_CHR_2_PAR_SPACE 1
202#define MPSC_CHR_2_PAR_EVEN 2
203#define MPSC_CHR_2_PAR_MARK 3
204
205/* MPSC Signal Routing */
206#define MPSC_MRR 0x0000
207#define MPSC_RCRR 0x0004
208#define MPSC_TCRR 0x0008
209
210/*
211 *****************************************************************************
212 *
213 * Serial DMA Controller Interface Registers
214 *
215 *****************************************************************************
216 */
217
218#define SDMA_SDC 0x0000
219#define SDMA_SDCM 0x0008
220#define SDMA_RX_DESC 0x0800
221#define SDMA_RX_BUF_PTR 0x0808
222#define SDMA_SCRDP 0x0810
223#define SDMA_TX_DESC 0x0c00
224#define SDMA_SCTDP 0x0c10
225#define SDMA_SFTDP 0x0c14
226
227#define SDMA_DESC_CMDSTAT_PE (1<<0)
228#define SDMA_DESC_CMDSTAT_CDL (1<<1)
229#define SDMA_DESC_CMDSTAT_FR (1<<3)
230#define SDMA_DESC_CMDSTAT_OR (1<<6)
231#define SDMA_DESC_CMDSTAT_BR (1<<9)
232#define SDMA_DESC_CMDSTAT_MI (1<<10)
233#define SDMA_DESC_CMDSTAT_A (1<<11)
234#define SDMA_DESC_CMDSTAT_AM (1<<12)
235#define SDMA_DESC_CMDSTAT_CT (1<<13)
236#define SDMA_DESC_CMDSTAT_C (1<<14)
237#define SDMA_DESC_CMDSTAT_ES (1<<15)
238#define SDMA_DESC_CMDSTAT_L (1<<16)
239#define SDMA_DESC_CMDSTAT_F (1<<17)
240#define SDMA_DESC_CMDSTAT_P (1<<18)
241#define SDMA_DESC_CMDSTAT_EI (1<<23)
242#define SDMA_DESC_CMDSTAT_O (1<<31)
243
244#define SDMA_DESC_DFLT (SDMA_DESC_CMDSTAT_O | \
245 SDMA_DESC_CMDSTAT_EI)
246
247#define SDMA_SDC_RFT (1<<0)
248#define SDMA_SDC_SFM (1<<1)
249#define SDMA_SDC_BLMR (1<<6)
250#define SDMA_SDC_BLMT (1<<7)
251#define SDMA_SDC_POVR (1<<8)
252#define SDMA_SDC_RIFB (1<<9)
253
254#define SDMA_SDCM_ERD (1<<7)
255#define SDMA_SDCM_AR (1<<15)
256#define SDMA_SDCM_STD (1<<16)
257#define SDMA_SDCM_TXD (1<<23)
258#define SDMA_SDCM_AT (1<<31)
259
260#define SDMA_0_CAUSE_RXBUF (1<<0)
261#define SDMA_0_CAUSE_RXERR (1<<1)
262#define SDMA_0_CAUSE_TXBUF (1<<2)
263#define SDMA_0_CAUSE_TXEND (1<<3)
264#define SDMA_1_CAUSE_RXBUF (1<<8)
265#define SDMA_1_CAUSE_RXERR (1<<9)
266#define SDMA_1_CAUSE_TXBUF (1<<10)
267#define SDMA_1_CAUSE_TXEND (1<<11)
268
269#define SDMA_CAUSE_RX_MASK (SDMA_0_CAUSE_RXBUF | SDMA_0_CAUSE_RXERR | \
270 SDMA_1_CAUSE_RXBUF | SDMA_1_CAUSE_RXERR)
271#define SDMA_CAUSE_TX_MASK (SDMA_0_CAUSE_TXBUF | SDMA_0_CAUSE_TXEND | \
272 SDMA_1_CAUSE_TXBUF | SDMA_1_CAUSE_TXEND)
273
274/* SDMA Interrupt registers */
275#define SDMA_INTR_CAUSE 0x0000
276#define SDMA_INTR_MASK 0x0080
277
278/*
279 *****************************************************************************
280 *
281 * Baud Rate Generator Interface Registers
282 *
283 *****************************************************************************
284 */
285
286#define BRG_BCR 0x0000
287#define BRG_BTR 0x0004
288
289#endif /* __MPSC_H__ */
diff --git a/drivers/serial/mux.c b/drivers/serial/mux.c
new file mode 100644
index 000000000000..dadd7e19714e
--- /dev/null
+++ b/drivers/serial/mux.c
@@ -0,0 +1,539 @@
1/*
2** mux.c:
3** serial driver for the Mux console found in some PA-RISC servers.
4**
5** (c) Copyright 2002 Ryan Bradetich
6** (c) Copyright 2002 Hewlett-Packard Company
7**
8** This program is free software; you can redistribute it and/or modify
9** it under the terms of the GNU General Public License as published by
10** the Free Software Foundation; either version 2 of the License, or
11** (at your option) any later version.
12**
13** This Driver currently only supports the console (port 0) on the MUX.
14** Additional work will be needed on this driver to enable the full
15** functionality of the MUX.
16**
17*/
18
19#include <linux/config.h>
20#include <linux/module.h>
21#include <linux/tty.h>
22#include <linux/ioport.h>
23#include <linux/init.h>
24#include <linux/serial.h>
25#include <linux/console.h>
26#include <linux/slab.h>
27#include <linux/delay.h> /* for udelay */
28#include <linux/device.h>
29#include <asm/io.h>
30#include <asm/parisc-device.h>
31
32#ifdef CONFIG_MAGIC_SYSRQ
33#include <linux/sysrq.h>
34#define SUPPORT_SYSRQ
35#endif
36
37#include <linux/serial_core.h>
38
39#define MUX_OFFSET 0x800
40#define MUX_LINE_OFFSET 0x80
41
42#define MUX_FIFO_SIZE 255
43#define MUX_POLL_DELAY (30 * HZ / 1000)
44
45#define IO_DATA_REG_OFFSET 0x3c
46#define IO_DCOUNT_REG_OFFSET 0x40
47
48#define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000)
49#define MUX_STATUS(status) ((status & 0xF000) == 0x8000)
50#define MUX_BREAK(status) ((status & 0xF000) == 0x2000)
51
52#define MUX_NR 256
53static unsigned int port_cnt = 0;
54static struct uart_port mux_ports[MUX_NR];
55
56static struct uart_driver mux_driver = {
57 .owner = THIS_MODULE,
58 .driver_name = "ttyB",
59 .dev_name = "ttyB",
60 .major = MUX_MAJOR,
61 .minor = 0,
62 .nr = MUX_NR,
63};
64
65static struct timer_list mux_timer;
66
67#define UART_PUT_CHAR(p, c) __raw_writel((c), (unsigned long)(p)->membase + IO_DATA_REG_OFFSET)
68#define UART_GET_FIFO_CNT(p) __raw_readl((unsigned long)(p)->membase + IO_DCOUNT_REG_OFFSET)
69#define GET_MUX_PORTS(iodc_data) ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8
70
71/**
72 * mux_tx_empty - Check if the transmitter fifo is empty.
73 * @port: Ptr to the uart_port.
74 *
75 * This function test if the transmitter fifo for the port
76 * described by 'port' is empty. If it is empty, this function
77 * should return TIOCSER_TEMT, otherwise return 0.
78 */
79static unsigned int mux_tx_empty(struct uart_port *port)
80{
81 unsigned int cnt = __raw_readl((unsigned long)port->membase
82 + IO_DCOUNT_REG_OFFSET);
83
84 return cnt ? 0 : TIOCSER_TEMT;
85}
86
87/**
88 * mux_set_mctrl - Set the current state of the modem control inputs.
89 * @ports: Ptr to the uart_port.
90 * @mctrl: Modem control bits.
91 *
92 * The Serial MUX does not support CTS, DCD or DSR so this function
93 * is ignored.
94 */
95static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
96{
97}
98
99/**
100 * mux_get_mctrl - Returns the current state of modem control inputs.
101 * @port: Ptr to the uart_port.
102 *
103 * The Serial MUX does not support CTS, DCD or DSR so these lines are
104 * treated as permanently active.
105 */
106static unsigned int mux_get_mctrl(struct uart_port *port)
107{
108 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
109}
110
111/**
112 * mux_stop_tx - Stop transmitting characters.
113 * @port: Ptr to the uart_port.
114 * @tty_stop: tty layer issue this command?
115 *
116 * The Serial MUX does not support this function.
117 */
118static void mux_stop_tx(struct uart_port *port, unsigned int tty_stop)
119{
120}
121
122/**
123 * mux_start_tx - Start transmitting characters.
124 * @port: Ptr to the uart_port.
125 * @tty_start: tty layer issue this command?
126 *
127 * The Serial Mux does not support this function.
128 */
129static void mux_start_tx(struct uart_port *port, unsigned int tty_start)
130{
131}
132
133/**
134 * mux_stop_rx - Stop receiving characters.
135 * @port: Ptr to the uart_port.
136 *
137 * The Serial Mux does not support this function.
138 */
139static void mux_stop_rx(struct uart_port *port)
140{
141}
142
143/**
144 * mux_enable_ms - Enable modum status interrupts.
145 * @port: Ptr to the uart_port.
146 *
147 * The Serial Mux does not support this function.
148 */
149static void mux_enable_ms(struct uart_port *port)
150{
151}
152
153/**
154 * mux_break_ctl - Control the transmitssion of a break signal.
155 * @port: Ptr to the uart_port.
156 * @break_state: Raise/Lower the break signal.
157 *
158 * The Serial Mux does not support this function.
159 */
160static void mux_break_ctl(struct uart_port *port, int break_state)
161{
162}
163
164/**
165 * mux_write - Write chars to the mux fifo.
166 * @port: Ptr to the uart_port.
167 *
168 * This function writes all the data from the uart buffer to
169 * the mux fifo.
170 */
171static void mux_write(struct uart_port *port)
172{
173 int count;
174 struct circ_buf *xmit = &port->info->xmit;
175
176 if(port->x_char) {
177 UART_PUT_CHAR(port, port->x_char);
178 port->icount.tx++;
179 port->x_char = 0;
180 return;
181 }
182
183 if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
184 mux_stop_tx(port, 0);
185 return;
186 }
187
188 count = (port->fifosize) - UART_GET_FIFO_CNT(port);
189 do {
190 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
191 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
192 port->icount.tx++;
193 if(uart_circ_empty(xmit))
194 break;
195
196 } while(--count > 0);
197
198 while(UART_GET_FIFO_CNT(port))
199 udelay(1);
200
201 if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
202 uart_write_wakeup(port);
203
204 if (uart_circ_empty(xmit))
205 mux_stop_tx(port, 0);
206}
207
208/**
209 * mux_read - Read chars from the mux fifo.
210 * @port: Ptr to the uart_port.
211 *
212 * This reads all available data from the mux's fifo and pushes
213 * the data to the tty layer.
214 */
215static void mux_read(struct uart_port *port)
216{
217 int data;
218 struct tty_struct *tty = port->info->tty;
219 __u32 start_count = port->icount.rx;
220
221 while(1) {
222 data = __raw_readl((unsigned long)port->membase
223 + IO_DATA_REG_OFFSET);
224
225 if (MUX_STATUS(data))
226 continue;
227
228 if (MUX_EOFIFO(data))
229 break;
230
231 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
232 continue;
233
234 *tty->flip.char_buf_ptr = data & 0xffu;
235 *tty->flip.flag_buf_ptr = TTY_NORMAL;
236 port->icount.rx++;
237
238 if (MUX_BREAK(data)) {
239 port->icount.brk++;
240 if(uart_handle_break(port))
241 continue;
242 }
243
244 if (uart_handle_sysrq_char(port, data & 0xffu, NULL))
245 continue;
246
247 tty->flip.flag_buf_ptr++;
248 tty->flip.char_buf_ptr++;
249 tty->flip.count++;
250 }
251
252 if (start_count != port->icount.rx) {
253 tty_flip_buffer_push(tty);
254 }
255}
256
257/**
258 * mux_startup - Initialize the port.
259 * @port: Ptr to the uart_port.
260 *
261 * Grab any resources needed for this port and start the
262 * mux timer.
263 */
264static int mux_startup(struct uart_port *port)
265{
266 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
267 return 0;
268}
269
270/**
271 * mux_shutdown - Disable the port.
272 * @port: Ptr to the uart_port.
273 *
274 * Release any resources needed for the port.
275 */
276static void mux_shutdown(struct uart_port *port)
277{
278}
279
280/**
281 * mux_set_termios - Chane port parameters.
282 * @port: Ptr to the uart_port.
283 * @termios: new termios settings.
284 * @old: old termios settings.
285 *
286 * The Serial Mux does not support this function.
287 */
288static void
289mux_set_termios(struct uart_port *port, struct termios *termios,
290 struct termios *old)
291{
292}
293
294/**
295 * mux_type - Describe the port.
296 * @port: Ptr to the uart_port.
297 *
298 * Return a pointer to a string constant describing the
299 * specified port.
300 */
301static const char *mux_type(struct uart_port *port)
302{
303 return "Mux";
304}
305
306/**
307 * mux_release_port - Release memory and IO regions.
308 * @port: Ptr to the uart_port.
309 *
310 * Release any memory and IO region resources currently in use by
311 * the port.
312 */
313static void mux_release_port(struct uart_port *port)
314{
315}
316
317/**
318 * mux_request_port - Request memory and IO regions.
319 * @port: Ptr to the uart_port.
320 *
321 * Request any memory and IO region resources required by the port.
322 * If any fail, no resources should be registered when this function
323 * returns, and it should return -EBUSY on failure.
324 */
325static int mux_request_port(struct uart_port *port)
326{
327 return 0;
328}
329
330/**
331 * mux_config_port - Perform port autoconfiguration.
332 * @port: Ptr to the uart_port.
333 * @type: Bitmask of required configurations.
334 *
335 * Perform any autoconfiguration steps for the port. This functino is
336 * called if the UPF_BOOT_AUTOCONF flag is specified for the port.
337 * [Note: This is required for now because of a bug in the Serial core.
338 * rmk has already submitted a patch to linus, should be available for
339 * 2.5.47.]
340 */
341static void mux_config_port(struct uart_port *port, int type)
342{
343 port->type = PORT_MUX;
344}
345
346/**
347 * mux_verify_port - Verify the port information.
348 * @port: Ptr to the uart_port.
349 * @ser: Ptr to the serial information.
350 *
351 * Verify the new serial port information contained within serinfo is
352 * suitable for this port type.
353 */
354static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
355{
356 if(port->membase == NULL)
357 return -EINVAL;
358
359 return 0;
360}
361
362/**
363 * mux_drv_poll - Mux poll function.
364 * @unused: Unused variable
365 *
366 * This function periodically polls the Serial MUX to check for new data.
367 */
368static void mux_poll(unsigned long unused)
369{
370 int i;
371
372 for(i = 0; i < port_cnt; ++i) {
373 if(!mux_ports[i].info)
374 continue;
375
376 mux_read(&mux_ports[i]);
377 mux_write(&mux_ports[i]);
378 }
379
380 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
381}
382
383
384#ifdef CONFIG_SERIAL_MUX_CONSOLE
385static void mux_console_write(struct console *co, const char *s, unsigned count)
386{
387 while(count--)
388 pdc_iodc_putc(*s++);
389}
390
391static int mux_console_setup(struct console *co, char *options)
392{
393 return 0;
394}
395
396struct tty_driver *mux_console_device(struct console *co, int *index)
397{
398 *index = co->index;
399 return mux_driver.tty_driver;
400}
401
402static struct console mux_console = {
403 .name = "ttyB",
404 .write = mux_console_write,
405 .device = mux_console_device,
406 .setup = mux_console_setup,
407 .flags = CON_ENABLED | CON_PRINTBUFFER,
408 .index = 0,
409};
410
411#define MUX_CONSOLE &mux_console
412#else
413#define MUX_CONSOLE NULL
414#endif
415
416static struct uart_ops mux_pops = {
417 .tx_empty = mux_tx_empty,
418 .set_mctrl = mux_set_mctrl,
419 .get_mctrl = mux_get_mctrl,
420 .stop_tx = mux_stop_tx,
421 .start_tx = mux_start_tx,
422 .stop_rx = mux_stop_rx,
423 .enable_ms = mux_enable_ms,
424 .break_ctl = mux_break_ctl,
425 .startup = mux_startup,
426 .shutdown = mux_shutdown,
427 .set_termios = mux_set_termios,
428 .type = mux_type,
429 .release_port = mux_release_port,
430 .request_port = mux_request_port,
431 .config_port = mux_config_port,
432 .verify_port = mux_verify_port,
433};
434
435/**
436 * mux_probe - Determine if the Serial Mux should claim this device.
437 * @dev: The parisc device.
438 *
439 * Deterimine if the Serial Mux should claim this chip (return 0)
440 * or not (return 1).
441 */
442static int __init mux_probe(struct parisc_device *dev)
443{
444 int i, status, ports;
445 u8 iodc_data[32];
446 unsigned long bytecnt;
447 struct uart_port *port;
448
449 status = pdc_iodc_read(&bytecnt, dev->hpa, 0, iodc_data, 32);
450 if(status != PDC_OK) {
451 printk(KERN_ERR "Serial mux: Unable to read IODC.\n");
452 return 1;
453 }
454
455 ports = GET_MUX_PORTS(iodc_data);
456 printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.3\n", ports);
457
458 if(!port_cnt) {
459 mux_driver.cons = MUX_CONSOLE;
460
461 status = uart_register_driver(&mux_driver);
462 if(status) {
463 printk(KERN_ERR "Serial mux: Unable to register driver.\n");
464 return 1;
465 }
466
467 init_timer(&mux_timer);
468 mux_timer.function = mux_poll;
469 }
470
471 for(i = 0; i < ports; ++i, ++port_cnt) {
472 port = &mux_ports[port_cnt];
473 port->iobase = 0;
474 port->mapbase = dev->hpa + MUX_OFFSET + (i * MUX_LINE_OFFSET);
475 port->membase = ioremap(port->mapbase, MUX_LINE_OFFSET);
476 port->iotype = SERIAL_IO_MEM;
477 port->type = PORT_MUX;
478 port->irq = SERIAL_IRQ_NONE;
479 port->uartclk = 0;
480 port->fifosize = MUX_FIFO_SIZE;
481 port->ops = &mux_pops;
482 port->flags = UPF_BOOT_AUTOCONF;
483 port->line = port_cnt;
484 status = uart_add_one_port(&mux_driver, port);
485 BUG_ON(status);
486 }
487
488#ifdef CONFIG_SERIAL_MUX_CONSOLE
489 register_console(&mux_console);
490#endif
491 return 0;
492}
493
494static struct parisc_device_id mux_tbl[] = {
495 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
496 { 0, }
497};
498
499MODULE_DEVICE_TABLE(parisc, mux_tbl);
500
501static struct parisc_driver serial_mux_driver = {
502 .name = "Serial MUX",
503 .id_table = mux_tbl,
504 .probe = mux_probe,
505};
506
507/**
508 * mux_init - Serial MUX initalization procedure.
509 *
510 * Register the Serial MUX driver.
511 */
512static int __init mux_init(void)
513{
514 return register_parisc_driver(&serial_mux_driver);
515}
516
517/**
518 * mux_exit - Serial MUX cleanup procedure.
519 *
520 * Unregister the Serial MUX driver from the tty layer.
521 */
522static void __exit mux_exit(void)
523{
524 int i;
525
526 for (i = 0; i < port_cnt; i++) {
527 uart_remove_one_port(&mux_driver, &mux_ports[i]);
528 }
529
530 uart_unregister_driver(&mux_driver);
531}
532
533module_init(mux_init);
534module_exit(mux_exit);
535
536MODULE_AUTHOR("Ryan Bradetich");
537MODULE_DESCRIPTION("Serial MUX driver");
538MODULE_LICENSE("GPL");
539MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);
diff --git a/drivers/serial/pmac_zilog.c b/drivers/serial/pmac_zilog.c
new file mode 100644
index 000000000000..0a6b9f0ed23f
--- /dev/null
+++ b/drivers/serial/pmac_zilog.c
@@ -0,0 +1,2048 @@
1/*
2 * linux/drivers/serial/pmac_zilog.c
3 *
4 * Driver for PowerMac Z85c30 based ESCC cell found in the
5 * "macio" ASICs of various PowerMac models
6 *
7 * Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org)
8 *
9 * Derived from drivers/macintosh/macserial.c by Paul Mackerras
10 * and drivers/serial/sunzilog.c by David S. Miller
11 *
12 * Hrm... actually, I ripped most of sunzilog (Thanks David !) and
13 * adapted special tweaks needed for us. I don't think it's worth
14 * merging back those though. The DMA code still has to get in
15 * and once done, I expect that driver to remain fairly stable in
16 * the long term, unless we change the driver model again...
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 *
32 * 2004-08-06 Harald Welte <laforge@gnumonks.org>
33 * - Enable BREAK interrupt
34 * - Add support for sysreq
35 *
36 * TODO: - Add DMA support
37 * - Defer port shutdown to a few seconds after close
38 * - maybe put something right into uap->clk_divisor
39 */
40
41#undef DEBUG
42#undef DEBUG_HARD
43#undef USE_CTRL_O_SYSRQ
44
45#include <linux/config.h>
46#include <linux/module.h>
47#include <linux/tty.h>
48
49#include <linux/tty_flip.h>
50#include <linux/major.h>
51#include <linux/string.h>
52#include <linux/fcntl.h>
53#include <linux/mm.h>
54#include <linux/kernel.h>
55#include <linux/delay.h>
56#include <linux/init.h>
57#include <linux/console.h>
58#include <linux/slab.h>
59#include <linux/adb.h>
60#include <linux/pmu.h>
61#include <linux/bitops.h>
62#include <linux/sysrq.h>
63#include <asm/sections.h>
64#include <asm/io.h>
65#include <asm/irq.h>
66#include <asm/prom.h>
67#include <asm/machdep.h>
68#include <asm/pmac_feature.h>
69#include <asm/dbdma.h>
70#include <asm/macio.h>
71#include <asm/semaphore.h>
72
73#if defined (CONFIG_SERIAL_PMACZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
74#define SUPPORT_SYSRQ
75#endif
76
77#include <linux/serial.h>
78#include <linux/serial_core.h>
79
80#include "pmac_zilog.h"
81
82/* Not yet implemented */
83#undef HAS_DBDMA
84
85static char version[] __initdata = "pmac_zilog: 0.6 (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
86MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
87MODULE_DESCRIPTION("Driver for the PowerMac serial ports.");
88MODULE_LICENSE("GPL");
89
90#define PWRDBG(fmt, arg...) printk(KERN_DEBUG fmt , ## arg)
91
92
93/*
94 * For the sake of early serial console, we can do a pre-probe
95 * (optional) of the ports at rather early boot time.
96 */
97static struct uart_pmac_port pmz_ports[MAX_ZS_PORTS];
98static int pmz_ports_count;
99static DECLARE_MUTEX(pmz_irq_sem);
100
101static struct uart_driver pmz_uart_reg = {
102 .owner = THIS_MODULE,
103 .driver_name = "ttyS",
104 .devfs_name = "tts/",
105 .dev_name = "ttyS",
106 .major = TTY_MAJOR,
107};
108
109
110/*
111 * Load all registers to reprogram the port
112 * This function must only be called when the TX is not busy. The UART
113 * port lock must be held and local interrupts disabled.
114 */
115static void pmz_load_zsregs(struct uart_pmac_port *uap, u8 *regs)
116{
117 int i;
118
119 if (ZS_IS_ASLEEP(uap))
120 return;
121
122 /* Let pending transmits finish. */
123 for (i = 0; i < 1000; i++) {
124 unsigned char stat = read_zsreg(uap, R1);
125 if (stat & ALL_SNT)
126 break;
127 udelay(100);
128 }
129
130 ZS_CLEARERR(uap);
131 zssync(uap);
132 ZS_CLEARFIFO(uap);
133 zssync(uap);
134 ZS_CLEARERR(uap);
135
136 /* Disable all interrupts. */
137 write_zsreg(uap, R1,
138 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
139
140 /* Set parity, sync config, stop bits, and clock divisor. */
141 write_zsreg(uap, R4, regs[R4]);
142
143 /* Set misc. TX/RX control bits. */
144 write_zsreg(uap, R10, regs[R10]);
145
146 /* Set TX/RX controls sans the enable bits. */
147 write_zsreg(uap, R3, regs[R3] & ~RxENABLE);
148 write_zsreg(uap, R5, regs[R5] & ~TxENABLE);
149
150 /* now set R7 "prime" on ESCC */
151 write_zsreg(uap, R15, regs[R15] | EN85C30);
152 write_zsreg(uap, R7, regs[R7P]);
153
154 /* make sure we use R7 "non-prime" on ESCC */
155 write_zsreg(uap, R15, regs[R15] & ~EN85C30);
156
157 /* Synchronous mode config. */
158 write_zsreg(uap, R6, regs[R6]);
159 write_zsreg(uap, R7, regs[R7]);
160
161 /* Disable baud generator. */
162 write_zsreg(uap, R14, regs[R14] & ~BRENAB);
163
164 /* Clock mode control. */
165 write_zsreg(uap, R11, regs[R11]);
166
167 /* Lower and upper byte of baud rate generator divisor. */
168 write_zsreg(uap, R12, regs[R12]);
169 write_zsreg(uap, R13, regs[R13]);
170
171 /* Now rewrite R14, with BRENAB (if set). */
172 write_zsreg(uap, R14, regs[R14]);
173
174 /* Reset external status interrupts. */
175 write_zsreg(uap, R0, RES_EXT_INT);
176 write_zsreg(uap, R0, RES_EXT_INT);
177
178 /* Rewrite R3/R5, this time without enables masked. */
179 write_zsreg(uap, R3, regs[R3]);
180 write_zsreg(uap, R5, regs[R5]);
181
182 /* Rewrite R1, this time without IRQ enabled masked. */
183 write_zsreg(uap, R1, regs[R1]);
184
185 /* Enable interrupts */
186 write_zsreg(uap, R9, regs[R9]);
187}
188
189/*
190 * We do like sunzilog to avoid disrupting pending Tx
191 * Reprogram the Zilog channel HW registers with the copies found in the
192 * software state struct. If the transmitter is busy, we defer this update
193 * until the next TX complete interrupt. Else, we do it right now.
194 *
195 * The UART port lock must be held and local interrupts disabled.
196 */
197static void pmz_maybe_update_regs(struct uart_pmac_port *uap)
198{
199 if (!ZS_REGS_HELD(uap)) {
200 if (ZS_TX_ACTIVE(uap)) {
201 uap->flags |= PMACZILOG_FLAG_REGS_HELD;
202 } else {
203 pmz_debug("pmz: maybe_update_regs: updating\n");
204 pmz_load_zsregs(uap, uap->curregs);
205 }
206 }
207}
208
209static struct tty_struct *pmz_receive_chars(struct uart_pmac_port *uap,
210 struct pt_regs *regs)
211{
212 struct tty_struct *tty = NULL;
213 unsigned char ch, r1, drop, error;
214 int loops = 0;
215
216 retry:
217 /* The interrupt can be enabled when the port isn't open, typically
218 * that happens when using one port is open and the other closed (stale
219 * interrupt) or when one port is used as a console.
220 */
221 if (!ZS_IS_OPEN(uap)) {
222 pmz_debug("pmz: draining input\n");
223 /* Port is closed, drain input data */
224 for (;;) {
225 if ((++loops) > 1000)
226 goto flood;
227 (void)read_zsreg(uap, R1);
228 write_zsreg(uap, R0, ERR_RES);
229 (void)read_zsdata(uap);
230 ch = read_zsreg(uap, R0);
231 if (!(ch & Rx_CH_AV))
232 break;
233 }
234 return NULL;
235 }
236
237 /* Sanity check, make sure the old bug is no longer happening */
238 if (uap->port.info == NULL || uap->port.info->tty == NULL) {
239 WARN_ON(1);
240 (void)read_zsdata(uap);
241 return NULL;
242 }
243 tty = uap->port.info->tty;
244
245 while (1) {
246 error = 0;
247 drop = 0;
248
249 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
250 /* Have to drop the lock here */
251 pmz_debug("pmz: flip overflow\n");
252 spin_unlock(&uap->port.lock);
253 tty->flip.work.func((void *)tty);
254 spin_lock(&uap->port.lock);
255 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
256 drop = 1;
257 if (ZS_IS_ASLEEP(uap))
258 return NULL;
259 if (!ZS_IS_OPEN(uap))
260 goto retry;
261 }
262
263 r1 = read_zsreg(uap, R1);
264 ch = read_zsdata(uap);
265
266 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
267 write_zsreg(uap, R0, ERR_RES);
268 zssync(uap);
269 }
270
271 ch &= uap->parity_mask;
272 if (ch == 0 && uap->flags & PMACZILOG_FLAG_BREAK) {
273 uap->flags &= ~PMACZILOG_FLAG_BREAK;
274 }
275
276#if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_SERIAL_CORE_CONSOLE)
277#ifdef USE_CTRL_O_SYSRQ
278 /* Handle the SysRq ^O Hack */
279 if (ch == '\x0f') {
280 uap->port.sysrq = jiffies + HZ*5;
281 goto next_char;
282 }
283#endif /* USE_CTRL_O_SYSRQ */
284 if (uap->port.sysrq) {
285 int swallow;
286 spin_unlock(&uap->port.lock);
287 swallow = uart_handle_sysrq_char(&uap->port, ch, regs);
288 spin_lock(&uap->port.lock);
289 if (swallow)
290 goto next_char;
291 }
292#endif /* CONFIG_MAGIC_SYSRQ && CONFIG_SERIAL_CORE_CONSOLE */
293
294 /* A real serial line, record the character and status. */
295 if (drop)
296 goto next_char;
297
298 *tty->flip.char_buf_ptr = ch;
299 *tty->flip.flag_buf_ptr = TTY_NORMAL;
300 uap->port.icount.rx++;
301
302 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | BRK_ABRT)) {
303 error = 1;
304 if (r1 & BRK_ABRT) {
305 pmz_debug("pmz: got break !\n");
306 r1 &= ~(PAR_ERR | CRC_ERR);
307 uap->port.icount.brk++;
308 if (uart_handle_break(&uap->port))
309 goto next_char;
310 }
311 else if (r1 & PAR_ERR)
312 uap->port.icount.parity++;
313 else if (r1 & CRC_ERR)
314 uap->port.icount.frame++;
315 if (r1 & Rx_OVR)
316 uap->port.icount.overrun++;
317 r1 &= uap->port.read_status_mask;
318 if (r1 & BRK_ABRT)
319 *tty->flip.flag_buf_ptr = TTY_BREAK;
320 else if (r1 & PAR_ERR)
321 *tty->flip.flag_buf_ptr = TTY_PARITY;
322 else if (r1 & CRC_ERR)
323 *tty->flip.flag_buf_ptr = TTY_FRAME;
324 }
325
326 if (uap->port.ignore_status_mask == 0xff ||
327 (r1 & uap->port.ignore_status_mask) == 0) {
328 tty->flip.flag_buf_ptr++;
329 tty->flip.char_buf_ptr++;
330 tty->flip.count++;
331 }
332 if ((r1 & Rx_OVR) &&
333 tty->flip.count < TTY_FLIPBUF_SIZE) {
334 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
335 tty->flip.flag_buf_ptr++;
336 tty->flip.char_buf_ptr++;
337 tty->flip.count++;
338 }
339 next_char:
340 /* We can get stuck in an infinite loop getting char 0 when the
341 * line is in a wrong HW state, we break that here.
342 * When that happens, I disable the receive side of the driver.
343 * Note that what I've been experiencing is a real irq loop where
344 * I'm getting flooded regardless of the actual port speed.
345 * Something stange is going on with the HW
346 */
347 if ((++loops) > 1000)
348 goto flood;
349 ch = read_zsreg(uap, R0);
350 if (!(ch & Rx_CH_AV))
351 break;
352 }
353
354 return tty;
355 flood:
356 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
357 write_zsreg(uap, R1, uap->curregs[R1]);
358 zssync(uap);
359 dev_err(&uap->dev->ofdev.dev, "pmz: rx irq flood !\n");
360 return tty;
361}
362
363static void pmz_status_handle(struct uart_pmac_port *uap, struct pt_regs *regs)
364{
365 unsigned char status;
366
367 status = read_zsreg(uap, R0);
368 write_zsreg(uap, R0, RES_EXT_INT);
369 zssync(uap);
370
371 if (ZS_IS_OPEN(uap) && ZS_WANTS_MODEM_STATUS(uap)) {
372 if (status & SYNC_HUNT)
373 uap->port.icount.dsr++;
374
375 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
376 * But it does not tell us which bit has changed, we have to keep
377 * track of this ourselves.
378 * The CTS input is inverted for some reason. -- paulus
379 */
380 if ((status ^ uap->prev_status) & DCD)
381 uart_handle_dcd_change(&uap->port,
382 (status & DCD));
383 if ((status ^ uap->prev_status) & CTS)
384 uart_handle_cts_change(&uap->port,
385 !(status & CTS));
386
387 wake_up_interruptible(&uap->port.info->delta_msr_wait);
388 }
389
390 if (status & BRK_ABRT)
391 uap->flags |= PMACZILOG_FLAG_BREAK;
392
393 uap->prev_status = status;
394}
395
396static void pmz_transmit_chars(struct uart_pmac_port *uap)
397{
398 struct circ_buf *xmit;
399
400 if (ZS_IS_ASLEEP(uap))
401 return;
402 if (ZS_IS_CONS(uap)) {
403 unsigned char status = read_zsreg(uap, R0);
404
405 /* TX still busy? Just wait for the next TX done interrupt.
406 *
407 * It can occur because of how we do serial console writes. It would
408 * be nice to transmit console writes just like we normally would for
409 * a TTY line. (ie. buffered and TX interrupt driven). That is not
410 * easy because console writes cannot sleep. One solution might be
411 * to poll on enough port->xmit space becomming free. -DaveM
412 */
413 if (!(status & Tx_BUF_EMP))
414 return;
415 }
416
417 uap->flags &= ~PMACZILOG_FLAG_TX_ACTIVE;
418
419 if (ZS_REGS_HELD(uap)) {
420 pmz_load_zsregs(uap, uap->curregs);
421 uap->flags &= ~PMACZILOG_FLAG_REGS_HELD;
422 }
423
424 if (ZS_TX_STOPPED(uap)) {
425 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
426 goto ack_tx_int;
427 }
428
429 if (uap->port.x_char) {
430 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
431 write_zsdata(uap, uap->port.x_char);
432 zssync(uap);
433 uap->port.icount.tx++;
434 uap->port.x_char = 0;
435 return;
436 }
437
438 if (uap->port.info == NULL)
439 goto ack_tx_int;
440 xmit = &uap->port.info->xmit;
441 if (uart_circ_empty(xmit)) {
442 uart_write_wakeup(&uap->port);
443 goto ack_tx_int;
444 }
445 if (uart_tx_stopped(&uap->port))
446 goto ack_tx_int;
447
448 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
449 write_zsdata(uap, xmit->buf[xmit->tail]);
450 zssync(uap);
451
452 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
453 uap->port.icount.tx++;
454
455 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
456 uart_write_wakeup(&uap->port);
457
458 return;
459
460ack_tx_int:
461 write_zsreg(uap, R0, RES_Tx_P);
462 zssync(uap);
463}
464
465/* Hrm... we register that twice, fixme later.... */
466static irqreturn_t pmz_interrupt(int irq, void *dev_id, struct pt_regs *regs)
467{
468 struct uart_pmac_port *uap = dev_id;
469 struct uart_pmac_port *uap_a;
470 struct uart_pmac_port *uap_b;
471 int rc = IRQ_NONE;
472 struct tty_struct *tty;
473 u8 r3;
474
475 uap_a = pmz_get_port_A(uap);
476 uap_b = uap_a->mate;
477
478 spin_lock(&uap_a->port.lock);
479 r3 = read_zsreg(uap_a, R3);
480
481#ifdef DEBUG_HARD
482 pmz_debug("irq, r3: %x\n", r3);
483#endif
484 /* Channel A */
485 tty = NULL;
486 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
487 write_zsreg(uap_a, R0, RES_H_IUS);
488 zssync(uap_a);
489 if (r3 & CHAEXT)
490 pmz_status_handle(uap_a, regs);
491 if (r3 & CHARxIP)
492 tty = pmz_receive_chars(uap_a, regs);
493 if (r3 & CHATxIP)
494 pmz_transmit_chars(uap_a);
495 rc = IRQ_HANDLED;
496 }
497 spin_unlock(&uap_a->port.lock);
498 if (tty != NULL)
499 tty_flip_buffer_push(tty);
500
501 if (uap_b->node == NULL)
502 goto out;
503
504 spin_lock(&uap_b->port.lock);
505 tty = NULL;
506 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
507 write_zsreg(uap_b, R0, RES_H_IUS);
508 zssync(uap_b);
509 if (r3 & CHBEXT)
510 pmz_status_handle(uap_b, regs);
511 if (r3 & CHBRxIP)
512 tty = pmz_receive_chars(uap_b, regs);
513 if (r3 & CHBTxIP)
514 pmz_transmit_chars(uap_b);
515 rc = IRQ_HANDLED;
516 }
517 spin_unlock(&uap_b->port.lock);
518 if (tty != NULL)
519 tty_flip_buffer_push(tty);
520
521 out:
522#ifdef DEBUG_HARD
523 pmz_debug("irq done.\n");
524#endif
525 return rc;
526}
527
528/*
529 * Peek the status register, lock not held by caller
530 */
531static inline u8 pmz_peek_status(struct uart_pmac_port *uap)
532{
533 unsigned long flags;
534 u8 status;
535
536 spin_lock_irqsave(&uap->port.lock, flags);
537 status = read_zsreg(uap, R0);
538 spin_unlock_irqrestore(&uap->port.lock, flags);
539
540 return status;
541}
542
543/*
544 * Check if transmitter is empty
545 * The port lock is not held.
546 */
547static unsigned int pmz_tx_empty(struct uart_port *port)
548{
549 struct uart_pmac_port *uap = to_pmz(port);
550 unsigned char status;
551
552 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
553 return TIOCSER_TEMT;
554
555 status = pmz_peek_status(to_pmz(port));
556 if (status & Tx_BUF_EMP)
557 return TIOCSER_TEMT;
558 return 0;
559}
560
561/*
562 * Set Modem Control (RTS & DTR) bits
563 * The port lock is held and interrupts are disabled.
564 * Note: Shall we really filter out RTS on external ports or
565 * should that be dealt at higher level only ?
566 */
567static void pmz_set_mctrl(struct uart_port *port, unsigned int mctrl)
568{
569 struct uart_pmac_port *uap = to_pmz(port);
570 unsigned char set_bits, clear_bits;
571
572 /* Do nothing for irda for now... */
573 if (ZS_IS_IRDA(uap))
574 return;
575 /* We get called during boot with a port not up yet */
576 if (ZS_IS_ASLEEP(uap) ||
577 !(ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)))
578 return;
579
580 set_bits = clear_bits = 0;
581
582 if (ZS_IS_INTMODEM(uap)) {
583 if (mctrl & TIOCM_RTS)
584 set_bits |= RTS;
585 else
586 clear_bits |= RTS;
587 }
588 if (mctrl & TIOCM_DTR)
589 set_bits |= DTR;
590 else
591 clear_bits |= DTR;
592
593 /* NOTE: Not subject to 'transmitter active' rule. */
594 uap->curregs[R5] |= set_bits;
595 uap->curregs[R5] &= ~clear_bits;
596 if (ZS_IS_ASLEEP(uap))
597 return;
598 write_zsreg(uap, R5, uap->curregs[R5]);
599 pmz_debug("pmz_set_mctrl: set bits: %x, clear bits: %x -> %x\n",
600 set_bits, clear_bits, uap->curregs[R5]);
601 zssync(uap);
602}
603
604/*
605 * Get Modem Control bits (only the input ones, the core will
606 * or that with a cached value of the control ones)
607 * The port lock is not held.
608 */
609static unsigned int pmz_get_mctrl(struct uart_port *port)
610{
611 struct uart_pmac_port *uap = to_pmz(port);
612 unsigned char status;
613 unsigned int ret;
614
615 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
616 return 0;
617
618 status = pmz_peek_status(to_pmz(port));
619
620 ret = 0;
621 if (status & DCD)
622 ret |= TIOCM_CAR;
623 if (status & SYNC_HUNT)
624 ret |= TIOCM_DSR;
625 if (!(status & CTS))
626 ret |= TIOCM_CTS;
627
628 return ret;
629}
630
631/*
632 * Stop TX side. Dealt like sunzilog at next Tx interrupt,
633 * though for DMA, we will have to do a bit more. What is
634 * the meaning of the tty_stop bit ? XXX
635 * The port lock is held and interrupts are disabled.
636 */
637static void pmz_stop_tx(struct uart_port *port, unsigned int tty_stop)
638{
639 to_pmz(port)->flags |= PMACZILOG_FLAG_TX_STOPPED;
640}
641
642/*
643 * Kick the Tx side.
644 * The port lock is held and interrupts are disabled.
645 */
646static void pmz_start_tx(struct uart_port *port, unsigned int tty_start)
647{
648 struct uart_pmac_port *uap = to_pmz(port);
649 unsigned char status;
650
651 pmz_debug("pmz: start_tx()\n");
652
653 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
654 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
655
656 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
657 return;
658
659 status = read_zsreg(uap, R0);
660
661 /* TX busy? Just wait for the TX done interrupt. */
662 if (!(status & Tx_BUF_EMP))
663 return;
664
665 /* Send the first character to jump-start the TX done
666 * IRQ sending engine.
667 */
668 if (port->x_char) {
669 write_zsdata(uap, port->x_char);
670 zssync(uap);
671 port->icount.tx++;
672 port->x_char = 0;
673 } else {
674 struct circ_buf *xmit = &port->info->xmit;
675
676 write_zsdata(uap, xmit->buf[xmit->tail]);
677 zssync(uap);
678 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
679 port->icount.tx++;
680
681 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
682 uart_write_wakeup(&uap->port);
683 }
684 pmz_debug("pmz: start_tx() done.\n");
685}
686
687/*
688 * Stop Rx side, basically disable emitting of
689 * Rx interrupts on the port. We don't disable the rx
690 * side of the chip proper though
691 * The port lock is held.
692 */
693static void pmz_stop_rx(struct uart_port *port)
694{
695 struct uart_pmac_port *uap = to_pmz(port);
696
697 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
698 return;
699
700 pmz_debug("pmz: stop_rx()()\n");
701
702 /* Disable all RX interrupts. */
703 uap->curregs[R1] &= ~RxINT_MASK;
704 pmz_maybe_update_regs(uap);
705
706 pmz_debug("pmz: stop_rx() done.\n");
707}
708
709/*
710 * Enable modem status change interrupts
711 * The port lock is held.
712 */
713static void pmz_enable_ms(struct uart_port *port)
714{
715 struct uart_pmac_port *uap = to_pmz(port);
716 unsigned char new_reg;
717
718 if (ZS_IS_IRDA(uap) || uap->node == NULL)
719 return;
720 new_reg = uap->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
721 if (new_reg != uap->curregs[R15]) {
722 uap->curregs[R15] = new_reg;
723
724 if (ZS_IS_ASLEEP(uap))
725 return;
726 /* NOTE: Not subject to 'transmitter active' rule. */
727 write_zsreg(uap, R15, uap->curregs[R15]);
728 }
729}
730
731/*
732 * Control break state emission
733 * The port lock is not held.
734 */
735static void pmz_break_ctl(struct uart_port *port, int break_state)
736{
737 struct uart_pmac_port *uap = to_pmz(port);
738 unsigned char set_bits, clear_bits, new_reg;
739 unsigned long flags;
740
741 if (uap->node == NULL)
742 return;
743 set_bits = clear_bits = 0;
744
745 if (break_state)
746 set_bits |= SND_BRK;
747 else
748 clear_bits |= SND_BRK;
749
750 spin_lock_irqsave(&port->lock, flags);
751
752 new_reg = (uap->curregs[R5] | set_bits) & ~clear_bits;
753 if (new_reg != uap->curregs[R5]) {
754 uap->curregs[R5] = new_reg;
755
756 /* NOTE: Not subject to 'transmitter active' rule. */
757 if (ZS_IS_ASLEEP(uap))
758 return;
759 write_zsreg(uap, R5, uap->curregs[R5]);
760 }
761
762 spin_unlock_irqrestore(&port->lock, flags);
763}
764
765/*
766 * Turn power on or off to the SCC and associated stuff
767 * (port drivers, modem, IR port, etc.)
768 * Returns the number of milliseconds we should wait before
769 * trying to use the port.
770 */
771static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
772{
773 int delay = 0;
774 int rc;
775
776 if (state) {
777 rc = pmac_call_feature(
778 PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 1);
779 pmz_debug("port power on result: %d\n", rc);
780 if (ZS_IS_INTMODEM(uap)) {
781 rc = pmac_call_feature(
782 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 1);
783 delay = 2500; /* wait for 2.5s before using */
784 pmz_debug("modem power result: %d\n", rc);
785 }
786 } else {
787 /* TODO: Make that depend on a timer, don't power down
788 * immediately
789 */
790 if (ZS_IS_INTMODEM(uap)) {
791 rc = pmac_call_feature(
792 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 0);
793 pmz_debug("port power off result: %d\n", rc);
794 }
795 pmac_call_feature(PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 0);
796 }
797 return delay;
798}
799
800/*
801 * FixZeroBug....Works around a bug in the SCC receving channel.
802 * Inspired from Darwin code, 15 Sept. 2000 -DanM
803 *
804 * The following sequence prevents a problem that is seen with O'Hare ASICs
805 * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
806 * at the input to the receiver becomes 'stuck' and locks up the receiver.
807 * This problem can occur as a result of a zero bit at the receiver input
808 * coincident with any of the following events:
809 *
810 * The SCC is initialized (hardware or software).
811 * A framing error is detected.
812 * The clocking option changes from synchronous or X1 asynchronous
813 * clocking to X16, X32, or X64 asynchronous clocking.
814 * The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
815 *
816 * This workaround attempts to recover from the lockup condition by placing
817 * the SCC in synchronous loopback mode with a fast clock before programming
818 * any of the asynchronous modes.
819 */
820static void pmz_fix_zero_bug_scc(struct uart_pmac_port *uap)
821{
822 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
823 zssync(uap);
824 udelay(10);
825 write_zsreg(uap, 9, (ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB) | NV);
826 zssync(uap);
827
828 write_zsreg(uap, 4, X1CLK | MONSYNC);
829 write_zsreg(uap, 3, Rx8);
830 write_zsreg(uap, 5, Tx8 | RTS);
831 write_zsreg(uap, 9, NV); /* Didn't we already do this? */
832 write_zsreg(uap, 11, RCBR | TCBR);
833 write_zsreg(uap, 12, 0);
834 write_zsreg(uap, 13, 0);
835 write_zsreg(uap, 14, (LOOPBAK | BRSRC));
836 write_zsreg(uap, 14, (LOOPBAK | BRSRC | BRENAB));
837 write_zsreg(uap, 3, Rx8 | RxENABLE);
838 write_zsreg(uap, 0, RES_EXT_INT);
839 write_zsreg(uap, 0, RES_EXT_INT);
840 write_zsreg(uap, 0, RES_EXT_INT); /* to kill some time */
841
842 /* The channel should be OK now, but it is probably receiving
843 * loopback garbage.
844 * Switch to asynchronous mode, disable the receiver,
845 * and discard everything in the receive buffer.
846 */
847 write_zsreg(uap, 9, NV);
848 write_zsreg(uap, 4, X16CLK | SB_MASK);
849 write_zsreg(uap, 3, Rx8);
850
851 while (read_zsreg(uap, 0) & Rx_CH_AV) {
852 (void)read_zsreg(uap, 8);
853 write_zsreg(uap, 0, RES_EXT_INT);
854 write_zsreg(uap, 0, ERR_RES);
855 }
856}
857
858/*
859 * Real startup routine, powers up the hardware and sets up
860 * the SCC. Returns a delay in ms where you need to wait before
861 * actually using the port, this is typically the internal modem
862 * powerup delay. This routine expect the lock to be taken.
863 */
864static int __pmz_startup(struct uart_pmac_port *uap)
865{
866 int pwr_delay = 0;
867
868 memset(&uap->curregs, 0, sizeof(uap->curregs));
869
870 /* Power up the SCC & underlying hardware (modem/irda) */
871 pwr_delay = pmz_set_scc_power(uap, 1);
872
873 /* Nice buggy HW ... */
874 pmz_fix_zero_bug_scc(uap);
875
876 /* Reset the channel */
877 uap->curregs[R9] = 0;
878 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
879 zssync(uap);
880 udelay(10);
881 write_zsreg(uap, 9, 0);
882 zssync(uap);
883
884 /* Clear the interrupt registers */
885 write_zsreg(uap, R1, 0);
886 write_zsreg(uap, R0, ERR_RES);
887 write_zsreg(uap, R0, ERR_RES);
888 write_zsreg(uap, R0, RES_H_IUS);
889 write_zsreg(uap, R0, RES_H_IUS);
890
891 /* Setup some valid baud rate */
892 uap->curregs[R4] = X16CLK | SB1;
893 uap->curregs[R3] = Rx8;
894 uap->curregs[R5] = Tx8 | RTS;
895 if (!ZS_IS_IRDA(uap))
896 uap->curregs[R5] |= DTR;
897 uap->curregs[R12] = 0;
898 uap->curregs[R13] = 0;
899 uap->curregs[R14] = BRENAB;
900
901 /* Clear handshaking, enable BREAK interrupts */
902 uap->curregs[R15] = BRKIE;
903
904 /* Master interrupt enable */
905 uap->curregs[R9] |= NV | MIE;
906
907 pmz_load_zsregs(uap, uap->curregs);
908
909 /* Enable receiver and transmitter. */
910 write_zsreg(uap, R3, uap->curregs[R3] |= RxENABLE);
911 write_zsreg(uap, R5, uap->curregs[R5] |= TxENABLE);
912
913 /* Remember status for DCD/CTS changes */
914 uap->prev_status = read_zsreg(uap, R0);
915
916
917 return pwr_delay;
918}
919
920static void pmz_irda_reset(struct uart_pmac_port *uap)
921{
922 uap->curregs[R5] |= DTR;
923 write_zsreg(uap, R5, uap->curregs[R5]);
924 zssync(uap);
925 mdelay(110);
926 uap->curregs[R5] &= ~DTR;
927 write_zsreg(uap, R5, uap->curregs[R5]);
928 zssync(uap);
929 mdelay(10);
930}
931
932/*
933 * This is the "normal" startup routine, using the above one
934 * wrapped with the lock and doing a schedule delay
935 */
936static int pmz_startup(struct uart_port *port)
937{
938 struct uart_pmac_port *uap = to_pmz(port);
939 unsigned long flags;
940 int pwr_delay = 0;
941
942 pmz_debug("pmz: startup()\n");
943
944 if (ZS_IS_ASLEEP(uap))
945 return -EAGAIN;
946 if (uap->node == NULL)
947 return -ENODEV;
948
949 down(&pmz_irq_sem);
950
951 uap->flags |= PMACZILOG_FLAG_IS_OPEN;
952
953 /* A console is never powered down. Else, power up and
954 * initialize the chip
955 */
956 if (!ZS_IS_CONS(uap)) {
957 spin_lock_irqsave(&port->lock, flags);
958 pwr_delay = __pmz_startup(uap);
959 spin_unlock_irqrestore(&port->lock, flags);
960 }
961
962 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
963 if (request_irq(uap->port.irq, pmz_interrupt, SA_SHIRQ, "PowerMac Zilog", uap)) {
964 dev_err(&uap->dev->ofdev.dev,
965 "Unable to register zs interrupt handler.\n");
966 pmz_set_scc_power(uap, 0);
967 up(&pmz_irq_sem);
968 return -ENXIO;
969 }
970
971 up(&pmz_irq_sem);
972
973 /* Right now, we deal with delay by blocking here, I'll be
974 * smarter later on
975 */
976 if (pwr_delay != 0) {
977 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
978 msleep(pwr_delay);
979 }
980
981 /* IrDA reset is done now */
982 if (ZS_IS_IRDA(uap))
983 pmz_irda_reset(uap);
984
985 /* Enable interrupts emission from the chip */
986 spin_lock_irqsave(&port->lock, flags);
987 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
988 if (!ZS_IS_EXTCLK(uap))
989 uap->curregs[R1] |= EXT_INT_ENAB;
990 write_zsreg(uap, R1, uap->curregs[R1]);
991 spin_unlock_irqrestore(&port->lock, flags);
992
993 pmz_debug("pmz: startup() done.\n");
994
995 return 0;
996}
997
998static void pmz_shutdown(struct uart_port *port)
999{
1000 struct uart_pmac_port *uap = to_pmz(port);
1001 unsigned long flags;
1002
1003 pmz_debug("pmz: shutdown()\n");
1004
1005 if (uap->node == NULL)
1006 return;
1007
1008 down(&pmz_irq_sem);
1009
1010 /* Release interrupt handler */
1011 free_irq(uap->port.irq, uap);
1012
1013 spin_lock_irqsave(&port->lock, flags);
1014
1015 uap->flags &= ~PMACZILOG_FLAG_IS_OPEN;
1016
1017 if (!ZS_IS_OPEN(uap->mate))
1018 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1019
1020 /* Disable interrupts */
1021 if (!ZS_IS_ASLEEP(uap)) {
1022 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1023 write_zsreg(uap, R1, uap->curregs[R1]);
1024 zssync(uap);
1025 }
1026
1027 if (ZS_IS_CONS(uap) || ZS_IS_ASLEEP(uap)) {
1028 spin_unlock_irqrestore(&port->lock, flags);
1029 up(&pmz_irq_sem);
1030 return;
1031 }
1032
1033 /* Disable receiver and transmitter. */
1034 uap->curregs[R3] &= ~RxENABLE;
1035 uap->curregs[R5] &= ~TxENABLE;
1036
1037 /* Disable all interrupts and BRK assertion. */
1038 uap->curregs[R5] &= ~SND_BRK;
1039 pmz_maybe_update_regs(uap);
1040
1041 /* Shut the chip down */
1042 pmz_set_scc_power(uap, 0);
1043
1044 spin_unlock_irqrestore(&port->lock, flags);
1045
1046 up(&pmz_irq_sem);
1047
1048 pmz_debug("pmz: shutdown() done.\n");
1049}
1050
1051/* Shared by TTY driver and serial console setup. The port lock is held
1052 * and local interrupts are disabled.
1053 */
1054static void pmz_convert_to_zs(struct uart_pmac_port *uap, unsigned int cflag,
1055 unsigned int iflag, unsigned long baud)
1056{
1057 int brg;
1058
1059
1060 /* Switch to external clocking for IrDA high clock rates. That
1061 * code could be re-used for Midi interfaces with different
1062 * multipliers
1063 */
1064 if (baud >= 115200 && ZS_IS_IRDA(uap)) {
1065 uap->curregs[R4] = X1CLK;
1066 uap->curregs[R11] = RCTRxCP | TCTRxCP;
1067 uap->curregs[R14] = 0; /* BRG off */
1068 uap->curregs[R12] = 0;
1069 uap->curregs[R13] = 0;
1070 uap->flags |= PMACZILOG_FLAG_IS_EXTCLK;
1071 } else {
1072 switch (baud) {
1073 case ZS_CLOCK/16: /* 230400 */
1074 uap->curregs[R4] = X16CLK;
1075 uap->curregs[R11] = 0;
1076 uap->curregs[R14] = 0;
1077 break;
1078 case ZS_CLOCK/32: /* 115200 */
1079 uap->curregs[R4] = X32CLK;
1080 uap->curregs[R11] = 0;
1081 uap->curregs[R14] = 0;
1082 break;
1083 default:
1084 uap->curregs[R4] = X16CLK;
1085 uap->curregs[R11] = TCBR | RCBR;
1086 brg = BPS_TO_BRG(baud, ZS_CLOCK / 16);
1087 uap->curregs[R12] = (brg & 255);
1088 uap->curregs[R13] = ((brg >> 8) & 255);
1089 uap->curregs[R14] = BRENAB;
1090 }
1091 uap->flags &= ~PMACZILOG_FLAG_IS_EXTCLK;
1092 }
1093
1094 /* Character size, stop bits, and parity. */
1095 uap->curregs[3] &= ~RxN_MASK;
1096 uap->curregs[5] &= ~TxN_MASK;
1097
1098 switch (cflag & CSIZE) {
1099 case CS5:
1100 uap->curregs[3] |= Rx5;
1101 uap->curregs[5] |= Tx5;
1102 uap->parity_mask = 0x1f;
1103 break;
1104 case CS6:
1105 uap->curregs[3] |= Rx6;
1106 uap->curregs[5] |= Tx6;
1107 uap->parity_mask = 0x3f;
1108 break;
1109 case CS7:
1110 uap->curregs[3] |= Rx7;
1111 uap->curregs[5] |= Tx7;
1112 uap->parity_mask = 0x7f;
1113 break;
1114 case CS8:
1115 default:
1116 uap->curregs[3] |= Rx8;
1117 uap->curregs[5] |= Tx8;
1118 uap->parity_mask = 0xff;
1119 break;
1120 };
1121 uap->curregs[4] &= ~(SB_MASK);
1122 if (cflag & CSTOPB)
1123 uap->curregs[4] |= SB2;
1124 else
1125 uap->curregs[4] |= SB1;
1126 if (cflag & PARENB)
1127 uap->curregs[4] |= PAR_ENAB;
1128 else
1129 uap->curregs[4] &= ~PAR_ENAB;
1130 if (!(cflag & PARODD))
1131 uap->curregs[4] |= PAR_EVEN;
1132 else
1133 uap->curregs[4] &= ~PAR_EVEN;
1134
1135 uap->port.read_status_mask = Rx_OVR;
1136 if (iflag & INPCK)
1137 uap->port.read_status_mask |= CRC_ERR | PAR_ERR;
1138 if (iflag & (BRKINT | PARMRK))
1139 uap->port.read_status_mask |= BRK_ABRT;
1140
1141 uap->port.ignore_status_mask = 0;
1142 if (iflag & IGNPAR)
1143 uap->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
1144 if (iflag & IGNBRK) {
1145 uap->port.ignore_status_mask |= BRK_ABRT;
1146 if (iflag & IGNPAR)
1147 uap->port.ignore_status_mask |= Rx_OVR;
1148 }
1149
1150 if ((cflag & CREAD) == 0)
1151 uap->port.ignore_status_mask = 0xff;
1152}
1153
1154
1155/*
1156 * Set the irda codec on the imac to the specified baud rate.
1157 */
1158static void pmz_irda_setup(struct uart_pmac_port *uap, unsigned long *baud)
1159{
1160 u8 cmdbyte;
1161 int t, version;
1162
1163 switch (*baud) {
1164 /* SIR modes */
1165 case 2400:
1166 cmdbyte = 0x53;
1167 break;
1168 case 4800:
1169 cmdbyte = 0x52;
1170 break;
1171 case 9600:
1172 cmdbyte = 0x51;
1173 break;
1174 case 19200:
1175 cmdbyte = 0x50;
1176 break;
1177 case 38400:
1178 cmdbyte = 0x4f;
1179 break;
1180 case 57600:
1181 cmdbyte = 0x4e;
1182 break;
1183 case 115200:
1184 cmdbyte = 0x4d;
1185 break;
1186 /* The FIR modes aren't really supported at this point, how
1187 * do we select the speed ? via the FCR on KeyLargo ?
1188 */
1189 case 1152000:
1190 cmdbyte = 0;
1191 break;
1192 case 4000000:
1193 cmdbyte = 0;
1194 break;
1195 default: /* 9600 */
1196 cmdbyte = 0x51;
1197 *baud = 9600;
1198 break;
1199 }
1200
1201 /* Wait for transmitter to drain */
1202 t = 10000;
1203 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0
1204 || (read_zsreg(uap, R1) & ALL_SNT) == 0) {
1205 if (--t <= 0) {
1206 dev_err(&uap->dev->ofdev.dev, "transmitter didn't drain\n");
1207 return;
1208 }
1209 udelay(10);
1210 }
1211
1212 /* Drain the receiver too */
1213 t = 100;
1214 (void)read_zsdata(uap);
1215 (void)read_zsdata(uap);
1216 (void)read_zsdata(uap);
1217 mdelay(10);
1218 while (read_zsreg(uap, R0) & Rx_CH_AV) {
1219 read_zsdata(uap);
1220 mdelay(10);
1221 if (--t <= 0) {
1222 dev_err(&uap->dev->ofdev.dev, "receiver didn't drain\n");
1223 return;
1224 }
1225 }
1226
1227 /* Switch to command mode */
1228 uap->curregs[R5] |= DTR;
1229 write_zsreg(uap, R5, uap->curregs[R5]);
1230 zssync(uap);
1231 mdelay(1);
1232
1233 /* Switch SCC to 19200 */
1234 pmz_convert_to_zs(uap, CS8, 0, 19200);
1235 pmz_load_zsregs(uap, uap->curregs);
1236 mdelay(1);
1237
1238 /* Write get_version command byte */
1239 write_zsdata(uap, 1);
1240 t = 5000;
1241 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1242 if (--t <= 0) {
1243 dev_err(&uap->dev->ofdev.dev,
1244 "irda_setup timed out on get_version byte\n");
1245 goto out;
1246 }
1247 udelay(10);
1248 }
1249 version = read_zsdata(uap);
1250
1251 if (version < 4) {
1252 dev_info(&uap->dev->ofdev.dev, "IrDA: dongle version %d not supported\n",
1253 version);
1254 goto out;
1255 }
1256
1257 /* Send speed mode */
1258 write_zsdata(uap, cmdbyte);
1259 t = 5000;
1260 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1261 if (--t <= 0) {
1262 dev_err(&uap->dev->ofdev.dev,
1263 "irda_setup timed out on speed mode byte\n");
1264 goto out;
1265 }
1266 udelay(10);
1267 }
1268 t = read_zsdata(uap);
1269 if (t != cmdbyte)
1270 dev_err(&uap->dev->ofdev.dev,
1271 "irda_setup speed mode byte = %x (%x)\n", t, cmdbyte);
1272
1273 dev_info(&uap->dev->ofdev.dev, "IrDA setup for %ld bps, dongle version: %d\n",
1274 *baud, version);
1275
1276 (void)read_zsdata(uap);
1277 (void)read_zsdata(uap);
1278 (void)read_zsdata(uap);
1279
1280 out:
1281 /* Switch back to data mode */
1282 uap->curregs[R5] &= ~DTR;
1283 write_zsreg(uap, R5, uap->curregs[R5]);
1284 zssync(uap);
1285
1286 (void)read_zsdata(uap);
1287 (void)read_zsdata(uap);
1288 (void)read_zsdata(uap);
1289}
1290
1291
1292static void __pmz_set_termios(struct uart_port *port, struct termios *termios,
1293 struct termios *old)
1294{
1295 struct uart_pmac_port *uap = to_pmz(port);
1296 unsigned long baud;
1297
1298 pmz_debug("pmz: set_termios()\n");
1299
1300 if (ZS_IS_ASLEEP(uap))
1301 return;
1302
1303 memcpy(&uap->termios_cache, termios, sizeof(struct termios));
1304
1305 /* XXX Check which revs of machines actually allow 1 and 4Mb speeds
1306 * on the IR dongle. Note that the IRTTY driver currently doesn't know
1307 * about the FIR mode and high speed modes. So these are unused. For
1308 * implementing proper support for these, we should probably add some
1309 * DMA as well, at least on the Rx side, which isn't a simple thing
1310 * at this point.
1311 */
1312 if (ZS_IS_IRDA(uap)) {
1313 /* Calc baud rate */
1314 baud = uart_get_baud_rate(port, termios, old, 1200, 4000000);
1315 pmz_debug("pmz: switch IRDA to %ld bauds\n", baud);
1316 /* Cet the irda codec to the right rate */
1317 pmz_irda_setup(uap, &baud);
1318 /* Set final baud rate */
1319 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1320 pmz_load_zsregs(uap, uap->curregs);
1321 zssync(uap);
1322 } else {
1323 baud = uart_get_baud_rate(port, termios, old, 1200, 230400);
1324 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1325 /* Make sure modem status interrupts are correctly configured */
1326 if (UART_ENABLE_MS(&uap->port, termios->c_cflag)) {
1327 uap->curregs[R15] |= DCDIE | SYNCIE | CTSIE;
1328 uap->flags |= PMACZILOG_FLAG_MODEM_STATUS;
1329 } else {
1330 uap->curregs[R15] &= ~(DCDIE | SYNCIE | CTSIE);
1331 uap->flags &= ~PMACZILOG_FLAG_MODEM_STATUS;
1332 }
1333
1334 /* Load registers to the chip */
1335 pmz_maybe_update_regs(uap);
1336 }
1337 uart_update_timeout(port, termios->c_cflag, baud);
1338
1339 pmz_debug("pmz: set_termios() done.\n");
1340}
1341
1342/* The port lock is not held. */
1343static void pmz_set_termios(struct uart_port *port, struct termios *termios,
1344 struct termios *old)
1345{
1346 struct uart_pmac_port *uap = to_pmz(port);
1347 unsigned long flags;
1348
1349 spin_lock_irqsave(&port->lock, flags);
1350
1351 /* Disable IRQs on the port */
1352 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1353 write_zsreg(uap, R1, uap->curregs[R1]);
1354
1355 /* Setup new port configuration */
1356 __pmz_set_termios(port, termios, old);
1357
1358 /* Re-enable IRQs on the port */
1359 if (ZS_IS_OPEN(uap)) {
1360 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1361 if (!ZS_IS_EXTCLK(uap))
1362 uap->curregs[R1] |= EXT_INT_ENAB;
1363 write_zsreg(uap, R1, uap->curregs[R1]);
1364 }
1365 spin_unlock_irqrestore(&port->lock, flags);
1366}
1367
1368static const char *pmz_type(struct uart_port *port)
1369{
1370 struct uart_pmac_port *uap = to_pmz(port);
1371
1372 if (ZS_IS_IRDA(uap))
1373 return "Z85c30 ESCC - Infrared port";
1374 else if (ZS_IS_INTMODEM(uap))
1375 return "Z85c30 ESCC - Internal modem";
1376 return "Z85c30 ESCC - Serial port";
1377}
1378
1379/* We do not request/release mappings of the registers here, this
1380 * happens at early serial probe time.
1381 */
1382static void pmz_release_port(struct uart_port *port)
1383{
1384}
1385
1386static int pmz_request_port(struct uart_port *port)
1387{
1388 return 0;
1389}
1390
1391/* These do not need to do anything interesting either. */
1392static void pmz_config_port(struct uart_port *port, int flags)
1393{
1394}
1395
1396/* We do not support letting the user mess with the divisor, IRQ, etc. */
1397static int pmz_verify_port(struct uart_port *port, struct serial_struct *ser)
1398{
1399 return -EINVAL;
1400}
1401
1402static struct uart_ops pmz_pops = {
1403 .tx_empty = pmz_tx_empty,
1404 .set_mctrl = pmz_set_mctrl,
1405 .get_mctrl = pmz_get_mctrl,
1406 .stop_tx = pmz_stop_tx,
1407 .start_tx = pmz_start_tx,
1408 .stop_rx = pmz_stop_rx,
1409 .enable_ms = pmz_enable_ms,
1410 .break_ctl = pmz_break_ctl,
1411 .startup = pmz_startup,
1412 .shutdown = pmz_shutdown,
1413 .set_termios = pmz_set_termios,
1414 .type = pmz_type,
1415 .release_port = pmz_release_port,
1416 .request_port = pmz_request_port,
1417 .config_port = pmz_config_port,
1418 .verify_port = pmz_verify_port,
1419};
1420
1421/*
1422 * Setup one port structure after probing, HW is down at this point,
1423 * Unlike sunzilog, we don't need to pre-init the spinlock as we don't
1424 * register our console before uart_add_one_port() is called
1425 */
1426static int __init pmz_init_port(struct uart_pmac_port *uap)
1427{
1428 struct device_node *np = uap->node;
1429 char *conn;
1430 struct slot_names_prop {
1431 int count;
1432 char name[1];
1433 } *slots;
1434 int len;
1435
1436 /*
1437 * Request & map chip registers
1438 */
1439 uap->port.mapbase = np->addrs[0].address;
1440 uap->port.membase = ioremap(uap->port.mapbase, 0x1000);
1441
1442 uap->control_reg = uap->port.membase;
1443 uap->data_reg = uap->control_reg + 0x10;
1444
1445 /*
1446 * Request & map DBDMA registers
1447 */
1448#ifdef HAS_DBDMA
1449 if (np->n_addrs >= 3 && np->n_intrs >= 3)
1450 uap->flags |= PMACZILOG_FLAG_HAS_DMA;
1451#endif
1452 if (ZS_HAS_DMA(uap)) {
1453 uap->tx_dma_regs = ioremap(np->addrs[np->n_addrs - 2].address, 0x1000);
1454 if (uap->tx_dma_regs == NULL) {
1455 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1456 goto no_dma;
1457 }
1458 uap->rx_dma_regs = ioremap(np->addrs[np->n_addrs - 1].address, 0x1000);
1459 if (uap->rx_dma_regs == NULL) {
1460 iounmap(uap->tx_dma_regs);
1461 uap->tx_dma_regs = NULL;
1462 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1463 goto no_dma;
1464 }
1465 uap->tx_dma_irq = np->intrs[1].line;
1466 uap->rx_dma_irq = np->intrs[2].line;
1467 }
1468no_dma:
1469
1470 /*
1471 * Detect port type
1472 */
1473 if (device_is_compatible(np, "cobalt"))
1474 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1475 conn = get_property(np, "AAPL,connector", &len);
1476 if (conn && (strcmp(conn, "infrared") == 0))
1477 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1478 uap->port_type = PMAC_SCC_ASYNC;
1479 /* 1999 Powerbook G3 has slot-names property instead */
1480 slots = (struct slot_names_prop *)get_property(np, "slot-names", &len);
1481 if (slots && slots->count > 0) {
1482 if (strcmp(slots->name, "IrDA") == 0)
1483 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1484 else if (strcmp(slots->name, "Modem") == 0)
1485 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1486 }
1487 if (ZS_IS_IRDA(uap))
1488 uap->port_type = PMAC_SCC_IRDA;
1489 if (ZS_IS_INTMODEM(uap)) {
1490 struct device_node* i2c_modem = find_devices("i2c-modem");
1491 if (i2c_modem) {
1492 char* mid = get_property(i2c_modem, "modem-id", NULL);
1493 if (mid) switch(*mid) {
1494 case 0x04 :
1495 case 0x05 :
1496 case 0x07 :
1497 case 0x08 :
1498 case 0x0b :
1499 case 0x0c :
1500 uap->port_type = PMAC_SCC_I2S1;
1501 }
1502 printk(KERN_INFO "pmac_zilog: i2c-modem detected, id: %d\n",
1503 mid ? (*mid) : 0);
1504 } else {
1505 printk(KERN_INFO "pmac_zilog: serial modem detected\n");
1506 }
1507 }
1508
1509 /*
1510 * Init remaining bits of "port" structure
1511 */
1512 uap->port.iotype = SERIAL_IO_MEM;
1513 uap->port.irq = np->intrs[0].line;
1514 uap->port.uartclk = ZS_CLOCK;
1515 uap->port.fifosize = 1;
1516 uap->port.ops = &pmz_pops;
1517 uap->port.type = PORT_PMAC_ZILOG;
1518 uap->port.flags = 0;
1519
1520 /* Setup some valid baud rate information in the register
1521 * shadows so we don't write crap there before baud rate is
1522 * first initialized.
1523 */
1524 pmz_convert_to_zs(uap, CS8, 0, 9600);
1525
1526 return 0;
1527}
1528
1529/*
1530 * Get rid of a port on module removal
1531 */
1532static void pmz_dispose_port(struct uart_pmac_port *uap)
1533{
1534 struct device_node *np;
1535
1536 np = uap->node;
1537 iounmap(uap->rx_dma_regs);
1538 iounmap(uap->tx_dma_regs);
1539 iounmap(uap->control_reg);
1540 uap->node = NULL;
1541 of_node_put(np);
1542 memset(uap, 0, sizeof(struct uart_pmac_port));
1543}
1544
1545/*
1546 * Called upon match with an escc node in the devive-tree.
1547 */
1548static int pmz_attach(struct macio_dev *mdev, const struct of_match *match)
1549{
1550 int i;
1551
1552 /* Iterate the pmz_ports array to find a matching entry
1553 */
1554 for (i = 0; i < MAX_ZS_PORTS; i++)
1555 if (pmz_ports[i].node == mdev->ofdev.node) {
1556 struct uart_pmac_port *uap = &pmz_ports[i];
1557
1558 uap->dev = mdev;
1559 dev_set_drvdata(&mdev->ofdev.dev, uap);
1560 if (macio_request_resources(uap->dev, "pmac_zilog"))
1561 printk(KERN_WARNING "%s: Failed to request resource"
1562 ", port still active\n",
1563 uap->node->name);
1564 else
1565 uap->flags |= PMACZILOG_FLAG_RSRC_REQUESTED;
1566 return 0;
1567 }
1568 return -ENODEV;
1569}
1570
1571/*
1572 * That one should not be called, macio isn't really a hotswap device,
1573 * we don't expect one of those serial ports to go away...
1574 */
1575static int pmz_detach(struct macio_dev *mdev)
1576{
1577 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1578
1579 if (!uap)
1580 return -ENODEV;
1581
1582 if (uap->flags & PMACZILOG_FLAG_RSRC_REQUESTED) {
1583 macio_release_resources(uap->dev);
1584 uap->flags &= ~PMACZILOG_FLAG_RSRC_REQUESTED;
1585 }
1586 dev_set_drvdata(&mdev->ofdev.dev, NULL);
1587 uap->dev = NULL;
1588
1589 return 0;
1590}
1591
1592
1593static int pmz_suspend(struct macio_dev *mdev, u32 pm_state)
1594{
1595 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1596 struct uart_state *state;
1597 unsigned long flags;
1598
1599 if (uap == NULL) {
1600 printk("HRM... pmz_suspend with NULL uap\n");
1601 return 0;
1602 }
1603
1604 if (pm_state == mdev->ofdev.dev.power.power_state || pm_state < 2)
1605 return 0;
1606
1607 pmz_debug("suspend, switching to state %d\n", pm_state);
1608
1609 state = pmz_uart_reg.state + uap->port.line;
1610
1611 down(&pmz_irq_sem);
1612 down(&state->sem);
1613
1614 spin_lock_irqsave(&uap->port.lock, flags);
1615
1616 if (ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)) {
1617 /* Disable receiver and transmitter. */
1618 uap->curregs[R3] &= ~RxENABLE;
1619 uap->curregs[R5] &= ~TxENABLE;
1620
1621 /* Disable all interrupts and BRK assertion. */
1622 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1623 uap->curregs[R5] &= ~SND_BRK;
1624 pmz_load_zsregs(uap, uap->curregs);
1625 uap->flags |= PMACZILOG_FLAG_IS_ASLEEP;
1626 mb();
1627 }
1628
1629 spin_unlock_irqrestore(&uap->port.lock, flags);
1630
1631 if (ZS_IS_OPEN(uap) || ZS_IS_OPEN(uap->mate))
1632 if (ZS_IS_ASLEEP(uap->mate) && ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1633 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1634 disable_irq(uap->port.irq);
1635 }
1636
1637 if (ZS_IS_CONS(uap))
1638 uap->port.cons->flags &= ~CON_ENABLED;
1639
1640 /* Shut the chip down */
1641 pmz_set_scc_power(uap, 0);
1642
1643 up(&state->sem);
1644 up(&pmz_irq_sem);
1645
1646 pmz_debug("suspend, switching complete\n");
1647
1648 mdev->ofdev.dev.power.power_state = pm_state;
1649
1650 return 0;
1651}
1652
1653
1654static int pmz_resume(struct macio_dev *mdev)
1655{
1656 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1657 struct uart_state *state;
1658 unsigned long flags;
1659 int pwr_delay = 0;
1660
1661 if (uap == NULL)
1662 return 0;
1663
1664 if (mdev->ofdev.dev.power.power_state == 0)
1665 return 0;
1666
1667 pmz_debug("resume, switching to state 0\n");
1668
1669 state = pmz_uart_reg.state + uap->port.line;
1670
1671 down(&pmz_irq_sem);
1672 down(&state->sem);
1673
1674 spin_lock_irqsave(&uap->port.lock, flags);
1675 if (!ZS_IS_OPEN(uap) && !ZS_IS_CONS(uap)) {
1676 spin_unlock_irqrestore(&uap->port.lock, flags);
1677 goto bail;
1678 }
1679 pwr_delay = __pmz_startup(uap);
1680
1681 /* Take care of config that may have changed while asleep */
1682 __pmz_set_termios(&uap->port, &uap->termios_cache, NULL);
1683
1684 if (ZS_IS_OPEN(uap)) {
1685 /* Enable interrupts */
1686 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1687 if (!ZS_IS_EXTCLK(uap))
1688 uap->curregs[R1] |= EXT_INT_ENAB;
1689 write_zsreg(uap, R1, uap->curregs[R1]);
1690 }
1691
1692 spin_unlock_irqrestore(&uap->port.lock, flags);
1693
1694 if (ZS_IS_CONS(uap))
1695 uap->port.cons->flags |= CON_ENABLED;
1696
1697 /* Re-enable IRQ on the controller */
1698 if (ZS_IS_OPEN(uap) && !ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1699 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
1700 enable_irq(uap->port.irq);
1701 }
1702
1703 bail:
1704 up(&state->sem);
1705 up(&pmz_irq_sem);
1706
1707 /* Right now, we deal with delay by blocking here, I'll be
1708 * smarter later on
1709 */
1710 if (pwr_delay != 0) {
1711 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
1712 msleep(pwr_delay);
1713 }
1714
1715 pmz_debug("resume, switching complete\n");
1716
1717 mdev->ofdev.dev.power.power_state = 0;
1718
1719 return 0;
1720}
1721
1722/*
1723 * Probe all ports in the system and build the ports array, we register
1724 * with the serial layer at this point, the macio-type probing is only
1725 * used later to "attach" to the sysfs tree so we get power management
1726 * events
1727 */
1728static int __init pmz_probe(void)
1729{
1730 struct device_node *node_p, *node_a, *node_b, *np;
1731 int count = 0;
1732 int rc;
1733
1734 /*
1735 * Find all escc chips in the system
1736 */
1737 node_p = of_find_node_by_name(NULL, "escc");
1738 while (node_p) {
1739 /*
1740 * First get channel A/B node pointers
1741 *
1742 * TODO: Add routines with proper locking to do that...
1743 */
1744 node_a = node_b = NULL;
1745 for (np = NULL; (np = of_get_next_child(node_p, np)) != NULL;) {
1746 if (strncmp(np->name, "ch-a", 4) == 0)
1747 node_a = of_node_get(np);
1748 else if (strncmp(np->name, "ch-b", 4) == 0)
1749 node_b = of_node_get(np);
1750 }
1751 if (!node_a && !node_b) {
1752 of_node_put(node_a);
1753 of_node_put(node_b);
1754 printk(KERN_ERR "pmac_zilog: missing node %c for escc %s\n",
1755 (!node_a) ? 'a' : 'b', node_p->full_name);
1756 goto next;
1757 }
1758
1759 /*
1760 * Fill basic fields in the port structures
1761 */
1762 pmz_ports[count].mate = &pmz_ports[count+1];
1763 pmz_ports[count+1].mate = &pmz_ports[count];
1764 pmz_ports[count].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1765 pmz_ports[count].node = node_a;
1766 pmz_ports[count+1].node = node_b;
1767 pmz_ports[count].port.line = count;
1768 pmz_ports[count+1].port.line = count+1;
1769
1770 /*
1771 * Setup the ports for real
1772 */
1773 rc = pmz_init_port(&pmz_ports[count]);
1774 if (rc == 0 && node_b != NULL)
1775 rc = pmz_init_port(&pmz_ports[count+1]);
1776 if (rc != 0) {
1777 of_node_put(node_a);
1778 of_node_put(node_b);
1779 memset(&pmz_ports[count], 0, sizeof(struct uart_pmac_port));
1780 memset(&pmz_ports[count+1], 0, sizeof(struct uart_pmac_port));
1781 goto next;
1782 }
1783 count += 2;
1784next:
1785 node_p = of_find_node_by_name(node_p, "escc");
1786 }
1787 pmz_ports_count = count;
1788
1789 return 0;
1790}
1791
1792#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1793
1794static void pmz_console_write(struct console *con, const char *s, unsigned int count);
1795static int __init pmz_console_setup(struct console *co, char *options);
1796
1797static struct console pmz_console = {
1798 .name = "ttyS",
1799 .write = pmz_console_write,
1800 .device = uart_console_device,
1801 .setup = pmz_console_setup,
1802 .flags = CON_PRINTBUFFER,
1803 .index = -1,
1804 .data = &pmz_uart_reg,
1805};
1806
1807#define PMACZILOG_CONSOLE &pmz_console
1808#else /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1809#define PMACZILOG_CONSOLE (NULL)
1810#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1811
1812/*
1813 * Register the driver, console driver and ports with the serial
1814 * core
1815 */
1816static int __init pmz_register(void)
1817{
1818 int i, rc;
1819
1820 pmz_uart_reg.nr = pmz_ports_count;
1821 pmz_uart_reg.cons = PMACZILOG_CONSOLE;
1822 pmz_uart_reg.minor = 64;
1823
1824 /*
1825 * Register this driver with the serial core
1826 */
1827 rc = uart_register_driver(&pmz_uart_reg);
1828 if (rc)
1829 return rc;
1830
1831 /*
1832 * Register each port with the serial core
1833 */
1834 for (i = 0; i < pmz_ports_count; i++) {
1835 struct uart_pmac_port *uport = &pmz_ports[i];
1836 /* NULL node may happen on wallstreet */
1837 if (uport->node != NULL)
1838 rc = uart_add_one_port(&pmz_uart_reg, &uport->port);
1839 if (rc)
1840 goto err_out;
1841 }
1842
1843 return 0;
1844err_out:
1845 while (i-- > 0) {
1846 struct uart_pmac_port *uport = &pmz_ports[i];
1847 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1848 }
1849 uart_unregister_driver(&pmz_uart_reg);
1850 return rc;
1851}
1852
1853static struct of_match pmz_match[] =
1854{
1855 {
1856 .name = "ch-a",
1857 .type = OF_ANY_MATCH,
1858 .compatible = OF_ANY_MATCH
1859 },
1860 {
1861 .name = "ch-b",
1862 .type = OF_ANY_MATCH,
1863 .compatible = OF_ANY_MATCH
1864 },
1865 {},
1866};
1867
1868static struct macio_driver pmz_driver =
1869{
1870 .name = "pmac_zilog",
1871 .match_table = pmz_match,
1872 .probe = pmz_attach,
1873 .remove = pmz_detach,
1874 .suspend = pmz_suspend,
1875 .resume = pmz_resume,
1876};
1877
1878static int __init init_pmz(void)
1879{
1880 int rc, i;
1881 printk(KERN_INFO "%s\n", version);
1882
1883 /*
1884 * First, we need to do a direct OF-based probe pass. We
1885 * do that because we want serial console up before the
1886 * macio stuffs calls us back, and since that makes it
1887 * easier to pass the proper number of channels to
1888 * uart_register_driver()
1889 */
1890 if (pmz_ports_count == 0)
1891 pmz_probe();
1892
1893 /*
1894 * Bail early if no port found
1895 */
1896 if (pmz_ports_count == 0)
1897 return -ENODEV;
1898
1899 /*
1900 * Now we register with the serial layer
1901 */
1902 rc = pmz_register();
1903 if (rc) {
1904 printk(KERN_ERR
1905 "pmac_zilog: Error registering serial device, disabling pmac_zilog.\n"
1906 "pmac_zilog: Did another serial driver already claim the minors?\n");
1907 /* effectively "pmz_unprobe()" */
1908 for (i=0; i < pmz_ports_count; i++)
1909 pmz_dispose_port(&pmz_ports[i]);
1910 return rc;
1911 }
1912
1913 /*
1914 * Then we register the macio driver itself
1915 */
1916 return macio_register_driver(&pmz_driver);
1917}
1918
1919static void __exit exit_pmz(void)
1920{
1921 int i;
1922
1923 /* Get rid of macio-driver (detach from macio) */
1924 macio_unregister_driver(&pmz_driver);
1925
1926 for (i = 0; i < pmz_ports_count; i++) {
1927 struct uart_pmac_port *uport = &pmz_ports[i];
1928 if (uport->node != NULL) {
1929 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1930 pmz_dispose_port(uport);
1931 }
1932 }
1933 /* Unregister UART driver */
1934 uart_unregister_driver(&pmz_uart_reg);
1935}
1936
1937#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1938
1939/*
1940 * Print a string to the serial port trying not to disturb
1941 * any possible real use of the port...
1942 */
1943static void pmz_console_write(struct console *con, const char *s, unsigned int count)
1944{
1945 struct uart_pmac_port *uap = &pmz_ports[con->index];
1946 unsigned long flags;
1947 int i;
1948
1949 if (ZS_IS_ASLEEP(uap))
1950 return;
1951 spin_lock_irqsave(&uap->port.lock, flags);
1952
1953 /* Turn of interrupts and enable the transmitter. */
1954 write_zsreg(uap, R1, uap->curregs[1] & ~TxINT_ENAB);
1955 write_zsreg(uap, R5, uap->curregs[5] | TxENABLE | RTS | DTR);
1956
1957 for (i = 0; i < count; i++) {
1958 /* Wait for the transmit buffer to empty. */
1959 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
1960 udelay(5);
1961 write_zsdata(uap, s[i]);
1962 if (s[i] == 10) {
1963 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
1964 udelay(5);
1965 write_zsdata(uap, R13);
1966 }
1967 }
1968
1969 /* Restore the values in the registers. */
1970 write_zsreg(uap, R1, uap->curregs[1]);
1971 /* Don't disable the transmitter. */
1972
1973 spin_unlock_irqrestore(&uap->port.lock, flags);
1974}
1975
1976/*
1977 * Setup the serial console
1978 */
1979static int __init pmz_console_setup(struct console *co, char *options)
1980{
1981 struct uart_pmac_port *uap;
1982 struct uart_port *port;
1983 int baud = 38400;
1984 int bits = 8;
1985 int parity = 'n';
1986 int flow = 'n';
1987 unsigned long pwr_delay;
1988
1989 /*
1990 * XServe's default to 57600 bps
1991 */
1992 if (machine_is_compatible("RackMac1,1")
1993 || machine_is_compatible("RackMac1,2")
1994 || machine_is_compatible("MacRISC4"))
1995 baud = 57600;
1996
1997 /*
1998 * Check whether an invalid uart number has been specified, and
1999 * if so, search for the first available port that does have
2000 * console support.
2001 */
2002 if (co->index >= pmz_ports_count)
2003 co->index = 0;
2004 uap = &pmz_ports[co->index];
2005 if (uap->node == NULL)
2006 return -ENODEV;
2007 port = &uap->port;
2008
2009 /*
2010 * Mark port as beeing a console
2011 */
2012 uap->flags |= PMACZILOG_FLAG_IS_CONS;
2013
2014 /*
2015 * Temporary fix for uart layer who didn't setup the spinlock yet
2016 */
2017 spin_lock_init(&port->lock);
2018
2019 /*
2020 * Enable the hardware
2021 */
2022 pwr_delay = __pmz_startup(uap);
2023 if (pwr_delay)
2024 mdelay(pwr_delay);
2025
2026 if (options)
2027 uart_parse_options(options, &baud, &parity, &bits, &flow);
2028
2029 return uart_set_options(port, co, baud, parity, bits, flow);
2030}
2031
2032static int __init pmz_console_init(void)
2033{
2034 /* Probe ports */
2035 pmz_probe();
2036
2037 /* TODO: Autoprobe console based on OF */
2038 /* pmz_console.index = i; */
2039 register_console(&pmz_console);
2040
2041 return 0;
2042
2043}
2044console_initcall(pmz_console_init);
2045#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
2046
2047module_init(init_pmz);
2048module_exit(exit_pmz);
diff --git a/drivers/serial/pmac_zilog.h b/drivers/serial/pmac_zilog.h
new file mode 100644
index 000000000000..c03f9bfacdd8
--- /dev/null
+++ b/drivers/serial/pmac_zilog.h
@@ -0,0 +1,382 @@
1#ifndef __PMAC_ZILOG_H__
2#define __PMAC_ZILOG_H__
3
4#define pmz_debug(fmt,arg...) dev_dbg(&uap->dev->ofdev.dev, fmt, ## arg)
5
6/*
7 * At most 2 ESCCs with 2 ports each
8 */
9#define MAX_ZS_PORTS 4
10
11/*
12 * We wrap our port structure around the generic uart_port.
13 */
14#define NUM_ZSREGS 17
15
16struct uart_pmac_port {
17 struct uart_port port;
18 struct uart_pmac_port *mate;
19
20 /* macio_dev for the escc holding this port (maybe be null on
21 * early inited port)
22 */
23 struct macio_dev *dev;
24 /* device node to this port, this points to one of 2 childs
25 * of "escc" node (ie. ch-a or ch-b)
26 */
27 struct device_node *node;
28
29 /* Port type as obtained from device tree (IRDA, modem, ...) */
30 int port_type;
31 u8 curregs[NUM_ZSREGS];
32
33 unsigned int flags;
34#define PMACZILOG_FLAG_IS_CONS 0x00000001
35#define PMACZILOG_FLAG_IS_KGDB 0x00000002
36#define PMACZILOG_FLAG_MODEM_STATUS 0x00000004
37#define PMACZILOG_FLAG_IS_CHANNEL_A 0x00000008
38#define PMACZILOG_FLAG_REGS_HELD 0x00000010
39#define PMACZILOG_FLAG_TX_STOPPED 0x00000020
40#define PMACZILOG_FLAG_TX_ACTIVE 0x00000040
41#define PMACZILOG_FLAG_ENABLED 0x00000080
42#define PMACZILOG_FLAG_IS_IRDA 0x00000100
43#define PMACZILOG_FLAG_IS_INTMODEM 0x00000200
44#define PMACZILOG_FLAG_HAS_DMA 0x00000400
45#define PMACZILOG_FLAG_RSRC_REQUESTED 0x00000800
46#define PMACZILOG_FLAG_IS_ASLEEP 0x00001000
47#define PMACZILOG_FLAG_IS_OPEN 0x00002000
48#define PMACZILOG_FLAG_IS_IRQ_ON 0x00004000
49#define PMACZILOG_FLAG_IS_EXTCLK 0x00008000
50#define PMACZILOG_FLAG_BREAK 0x00010000
51
52 unsigned char parity_mask;
53 unsigned char prev_status;
54
55 volatile u8 __iomem *control_reg;
56 volatile u8 __iomem *data_reg;
57
58 unsigned int tx_dma_irq;
59 unsigned int rx_dma_irq;
60 volatile struct dbdma_regs __iomem *tx_dma_regs;
61 volatile struct dbdma_regs __iomem *rx_dma_regs;
62
63 struct termios termios_cache;
64};
65
66#define to_pmz(p) ((struct uart_pmac_port *)(p))
67
68static inline struct uart_pmac_port *pmz_get_port_A(struct uart_pmac_port *uap)
69{
70 if (uap->flags & PMACZILOG_FLAG_IS_CHANNEL_A)
71 return uap;
72 return uap->mate;
73}
74
75/*
76 * Register acessors. Note that we don't need to enforce a recovery
77 * delay on PCI PowerMac hardware, it's dealt in HW by the MacIO chip,
78 * though if we try to use this driver on older machines, we might have
79 * to add it back
80 */
81static inline u8 read_zsreg(struct uart_pmac_port *port, u8 reg)
82{
83 if (reg != 0)
84 writeb(reg, port->control_reg);
85 return readb(port->control_reg);
86}
87
88static inline void write_zsreg(struct uart_pmac_port *port, u8 reg, u8 value)
89{
90 if (reg != 0)
91 writeb(reg, port->control_reg);
92 writeb(value, port->control_reg);
93}
94
95static inline u8 read_zsdata(struct uart_pmac_port *port)
96{
97 return readb(port->data_reg);
98}
99
100static inline void write_zsdata(struct uart_pmac_port *port, u8 data)
101{
102 writeb(data, port->data_reg);
103}
104
105static inline void zssync(struct uart_pmac_port *port)
106{
107 (void)readb(port->control_reg);
108}
109
110/* Conversion routines to/from brg time constants from/to bits
111 * per second.
112 */
113#define BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
114#define BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
115
116#define ZS_CLOCK 3686400 /* Z8530 RTxC input clock rate */
117
118/* The Zilog register set */
119
120#define FLAG 0x7e
121
122/* Write Register 0 */
123#define R0 0 /* Register selects */
124#define R1 1
125#define R2 2
126#define R3 3
127#define R4 4
128#define R5 5
129#define R6 6
130#define R7 7
131#define R8 8
132#define R9 9
133#define R10 10
134#define R11 11
135#define R12 12
136#define R13 13
137#define R14 14
138#define R15 15
139#define R7P 16
140
141#define NULLCODE 0 /* Null Code */
142#define POINT_HIGH 0x8 /* Select upper half of registers */
143#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
144#define SEND_ABORT 0x18 /* HDLC Abort */
145#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
146#define RES_Tx_P 0x28 /* Reset TxINT Pending */
147#define ERR_RES 0x30 /* Error Reset */
148#define RES_H_IUS 0x38 /* Reset highest IUS */
149
150#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
151#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
152#define RES_EOM_L 0xC0 /* Reset EOM latch */
153
154/* Write Register 1 */
155
156#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
157#define TxINT_ENAB 0x2 /* Tx Int Enable */
158#define PAR_SPEC 0x4 /* Parity is special condition */
159
160#define RxINT_DISAB 0 /* Rx Int Disable */
161#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
162#define INT_ALL_Rx 0x10 /* Int on all Rx Characters or error */
163#define INT_ERR_Rx 0x18 /* Int on error only */
164#define RxINT_MASK 0x18
165
166#define WT_RDY_RT 0x20 /* W/Req reflects recv if 1, xmit if 0 */
167#define WT_FN_RDYFN 0x40 /* W/Req pin is DMA request if 1, wait if 0 */
168#define WT_RDY_ENAB 0x80 /* Enable W/Req pin */
169
170/* Write Register #2 (Interrupt Vector) */
171
172/* Write Register 3 */
173
174#define RxENABLE 0x1 /* Rx Enable */
175#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
176#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
177#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
178#define ENT_HM 0x10 /* Enter Hunt Mode */
179#define AUTO_ENAB 0x20 /* Auto Enables */
180#define Rx5 0x0 /* Rx 5 Bits/Character */
181#define Rx7 0x40 /* Rx 7 Bits/Character */
182#define Rx6 0x80 /* Rx 6 Bits/Character */
183#define Rx8 0xc0 /* Rx 8 Bits/Character */
184#define RxN_MASK 0xc0
185
186/* Write Register 4 */
187
188#define PAR_ENAB 0x1 /* Parity Enable */
189#define PAR_EVEN 0x2 /* Parity Even/Odd* */
190
191#define SYNC_ENAB 0 /* Sync Modes Enable */
192#define SB1 0x4 /* 1 stop bit/char */
193#define SB15 0x8 /* 1.5 stop bits/char */
194#define SB2 0xc /* 2 stop bits/char */
195#define SB_MASK 0xc
196
197#define MONSYNC 0 /* 8 Bit Sync character */
198#define BISYNC 0x10 /* 16 bit sync character */
199#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
200#define EXTSYNC 0x30 /* External Sync Mode */
201
202#define X1CLK 0x0 /* x1 clock mode */
203#define X16CLK 0x40 /* x16 clock mode */
204#define X32CLK 0x80 /* x32 clock mode */
205#define X64CLK 0xC0 /* x64 clock mode */
206#define XCLK_MASK 0xC0
207
208/* Write Register 5 */
209
210#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
211#define RTS 0x2 /* RTS */
212#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
213#define TxENABLE 0x8 /* Tx Enable */
214#define SND_BRK 0x10 /* Send Break */
215#define Tx5 0x0 /* Tx 5 bits (or less)/character */
216#define Tx7 0x20 /* Tx 7 bits/character */
217#define Tx6 0x40 /* Tx 6 bits/character */
218#define Tx8 0x60 /* Tx 8 bits/character */
219#define TxN_MASK 0x60
220#define DTR 0x80 /* DTR */
221
222/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
223
224/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
225
226/* Write Register 7' (Some enhanced feature control) */
227#define ENEXREAD 0x40 /* Enable read of some write registers */
228
229/* Write Register 8 (transmit buffer) */
230
231/* Write Register 9 (Master interrupt control) */
232#define VIS 1 /* Vector Includes Status */
233#define NV 2 /* No Vector */
234#define DLC 4 /* Disable Lower Chain */
235#define MIE 8 /* Master Interrupt Enable */
236#define STATHI 0x10 /* Status high */
237#define NORESET 0 /* No reset on write to R9 */
238#define CHRB 0x40 /* Reset channel B */
239#define CHRA 0x80 /* Reset channel A */
240#define FHWRES 0xc0 /* Force hardware reset */
241
242/* Write Register 10 (misc control bits) */
243#define BIT6 1 /* 6 bit/8bit sync */
244#define LOOPMODE 2 /* SDLC Loop mode */
245#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
246#define MARKIDLE 8 /* Mark/flag on idle */
247#define GAOP 0x10 /* Go active on poll */
248#define NRZ 0 /* NRZ mode */
249#define NRZI 0x20 /* NRZI mode */
250#define FM1 0x40 /* FM1 (transition = 1) */
251#define FM0 0x60 /* FM0 (transition = 0) */
252#define CRCPS 0x80 /* CRC Preset I/O */
253
254/* Write Register 11 (Clock Mode control) */
255#define TRxCXT 0 /* TRxC = Xtal output */
256#define TRxCTC 1 /* TRxC = Transmit clock */
257#define TRxCBR 2 /* TRxC = BR Generator Output */
258#define TRxCDP 3 /* TRxC = DPLL output */
259#define TRxCOI 4 /* TRxC O/I */
260#define TCRTxCP 0 /* Transmit clock = RTxC pin */
261#define TCTRxCP 8 /* Transmit clock = TRxC pin */
262#define TCBR 0x10 /* Transmit clock = BR Generator output */
263#define TCDPLL 0x18 /* Transmit clock = DPLL output */
264#define RCRTxCP 0 /* Receive clock = RTxC pin */
265#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
266#define RCBR 0x40 /* Receive clock = BR Generator output */
267#define RCDPLL 0x60 /* Receive clock = DPLL output */
268#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
269
270/* Write Register 12 (lower byte of baud rate generator time constant) */
271
272/* Write Register 13 (upper byte of baud rate generator time constant) */
273
274/* Write Register 14 (Misc control bits) */
275#define BRENAB 1 /* Baud rate generator enable */
276#define BRSRC 2 /* Baud rate generator source */
277#define DTRREQ 4 /* DTR/Request function */
278#define AUTOECHO 8 /* Auto Echo */
279#define LOOPBAK 0x10 /* Local loopback */
280#define SEARCH 0x20 /* Enter search mode */
281#define RMC 0x40 /* Reset missing clock */
282#define DISDPLL 0x60 /* Disable DPLL */
283#define SSBR 0x80 /* Set DPLL source = BR generator */
284#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
285#define SFMM 0xc0 /* Set FM mode */
286#define SNRZI 0xe0 /* Set NRZI mode */
287
288/* Write Register 15 (external/status interrupt control) */
289#define EN85C30 1 /* Enable some 85c30-enhanced registers */
290#define ZCIE 2 /* Zero count IE */
291#define ENSTFIFO 4 /* Enable status FIFO (SDLC) */
292#define DCDIE 8 /* DCD IE */
293#define SYNCIE 0x10 /* Sync/hunt IE */
294#define CTSIE 0x20 /* CTS IE */
295#define TxUIE 0x40 /* Tx Underrun/EOM IE */
296#define BRKIE 0x80 /* Break/Abort IE */
297
298
299/* Read Register 0 */
300#define Rx_CH_AV 0x1 /* Rx Character Available */
301#define ZCOUNT 0x2 /* Zero count */
302#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
303#define DCD 0x8 /* DCD */
304#define SYNC_HUNT 0x10 /* Sync/hunt */
305#define CTS 0x20 /* CTS */
306#define TxEOM 0x40 /* Tx underrun */
307#define BRK_ABRT 0x80 /* Break/Abort */
308
309/* Read Register 1 */
310#define ALL_SNT 0x1 /* All sent */
311/* Residue Data for 8 Rx bits/char programmed */
312#define RES3 0x8 /* 0/3 */
313#define RES4 0x4 /* 0/4 */
314#define RES5 0xc /* 0/5 */
315#define RES6 0x2 /* 0/6 */
316#define RES7 0xa /* 0/7 */
317#define RES8 0x6 /* 0/8 */
318#define RES18 0xe /* 1/8 */
319#define RES28 0x0 /* 2/8 */
320/* Special Rx Condition Interrupts */
321#define PAR_ERR 0x10 /* Parity error */
322#define Rx_OVR 0x20 /* Rx Overrun Error */
323#define CRC_ERR 0x40 /* CRC/Framing Error */
324#define END_FR 0x80 /* End of Frame (SDLC) */
325
326/* Read Register 2 (channel b only) - Interrupt vector */
327#define CHB_Tx_EMPTY 0x00
328#define CHB_EXT_STAT 0x02
329#define CHB_Rx_AVAIL 0x04
330#define CHB_SPECIAL 0x06
331#define CHA_Tx_EMPTY 0x08
332#define CHA_EXT_STAT 0x0a
333#define CHA_Rx_AVAIL 0x0c
334#define CHA_SPECIAL 0x0e
335#define STATUS_MASK 0x06
336
337/* Read Register 3 (interrupt pending register) ch a only */
338#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
339#define CHBTxIP 0x2 /* Channel B Tx IP */
340#define CHBRxIP 0x4 /* Channel B Rx IP */
341#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
342#define CHATxIP 0x10 /* Channel A Tx IP */
343#define CHARxIP 0x20 /* Channel A Rx IP */
344
345/* Read Register 8 (receive data register) */
346
347/* Read Register 10 (misc status bits) */
348#define ONLOOP 2 /* On loop */
349#define LOOPSEND 0x10 /* Loop sending */
350#define CLK2MIS 0x40 /* Two clocks missing */
351#define CLK1MIS 0x80 /* One clock missing */
352
353/* Read Register 12 (lower byte of baud rate generator constant) */
354
355/* Read Register 13 (upper byte of baud rate generator constant) */
356
357/* Read Register 15 (value of WR 15) */
358
359/* Misc macros */
360#define ZS_CLEARERR(port) (write_zsreg(port, 0, ERR_RES))
361#define ZS_CLEARFIFO(port) do { volatile unsigned char garbage; \
362 garbage = read_zsdata(port); \
363 garbage = read_zsdata(port); \
364 garbage = read_zsdata(port); \
365 } while(0)
366
367#define ZS_IS_CONS(UP) ((UP)->flags & PMACZILOG_FLAG_IS_CONS)
368#define ZS_IS_KGDB(UP) ((UP)->flags & PMACZILOG_FLAG_IS_KGDB)
369#define ZS_IS_CHANNEL_A(UP) ((UP)->flags & PMACZILOG_FLAG_IS_CHANNEL_A)
370#define ZS_REGS_HELD(UP) ((UP)->flags & PMACZILOG_FLAG_REGS_HELD)
371#define ZS_TX_STOPPED(UP) ((UP)->flags & PMACZILOG_FLAG_TX_STOPPED)
372#define ZS_TX_ACTIVE(UP) ((UP)->flags & PMACZILOG_FLAG_TX_ACTIVE)
373#define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & PMACZILOG_FLAG_MODEM_STATUS)
374#define ZS_IS_IRDA(UP) ((UP)->flags & PMACZILOG_FLAG_IS_IRDA)
375#define ZS_IS_INTMODEM(UP) ((UP)->flags & PMACZILOG_FLAG_IS_INTMODEM)
376#define ZS_HAS_DMA(UP) ((UP)->flags & PMACZILOG_FLAG_HAS_DMA)
377#define ZS_IS_ASLEEP(UP) ((UP)->flags & PMACZILOG_FLAG_IS_ASLEEP)
378#define ZS_IS_OPEN(UP) ((UP)->flags & PMACZILOG_FLAG_IS_OPEN)
379#define ZS_IS_IRQ_ON(UP) ((UP)->flags & PMACZILOG_FLAG_IS_IRQ_ON)
380#define ZS_IS_EXTCLK(UP) ((UP)->flags & PMACZILOG_FLAG_IS_EXTCLK)
381
382#endif /* __PMAC_ZILOG_H__ */
diff --git a/drivers/serial/pxa.c b/drivers/serial/pxa.c
new file mode 100644
index 000000000000..68b25b2c26b1
--- /dev/null
+++ b/drivers/serial/pxa.c
@@ -0,0 +1,877 @@
1/*
2 * linux/drivers/serial/pxa.c
3 *
4 * Based on drivers/serial/8250.c by Russell King.
5 *
6 * Author: Nicolas Pitre
7 * Created: Feb 20, 2003
8 * Copyright: (C) 2003 Monta Vista Software, Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * Note 1: This driver is made separate from the already too overloaded
16 * 8250.c because it needs some kirks of its own and that'll make it
17 * easier to add DMA support.
18 *
19 * Note 2: I'm too sick of device allocation policies for serial ports.
20 * If someone else wants to request an "official" allocation of major/minor
21 * for this driver please be my guest. And don't forget that new hardware
22 * to come from Intel might have more than 3 or 4 of those UARTs. Let's
23 * hope for a better port registration and dynamic device allocation scheme
24 * with the serial core maintainer satisfaction to appear soon.
25 */
26
27#include <linux/config.h>
28
29#if defined(CONFIG_SERIAL_PXA_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
30#define SUPPORT_SYSRQ
31#endif
32
33#include <linux/module.h>
34#include <linux/ioport.h>
35#include <linux/init.h>
36#include <linux/console.h>
37#include <linux/sysrq.h>
38#include <linux/serial_reg.h>
39#include <linux/circ_buf.h>
40#include <linux/delay.h>
41#include <linux/interrupt.h>
42#include <linux/device.h>
43#include <linux/tty.h>
44#include <linux/tty_flip.h>
45#include <linux/serial_core.h>
46
47#include <asm/io.h>
48#include <asm/hardware.h>
49#include <asm/irq.h>
50#include <asm/arch/pxa-regs.h>
51
52
53struct uart_pxa_port {
54 struct uart_port port;
55 unsigned char ier;
56 unsigned char lcr;
57 unsigned char mcr;
58 unsigned int lsr_break_flag;
59 unsigned int cken;
60 char *name;
61};
62
63static inline unsigned int serial_in(struct uart_pxa_port *up, int offset)
64{
65 offset <<= 2;
66 return readl(up->port.membase + offset);
67}
68
69static inline void serial_out(struct uart_pxa_port *up, int offset, int value)
70{
71 offset <<= 2;
72 writel(value, up->port.membase + offset);
73}
74
75static void serial_pxa_enable_ms(struct uart_port *port)
76{
77 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
78
79 up->ier |= UART_IER_MSI;
80 serial_out(up, UART_IER, up->ier);
81}
82
83static void serial_pxa_stop_tx(struct uart_port *port, unsigned int tty_stop)
84{
85 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
86
87 if (up->ier & UART_IER_THRI) {
88 up->ier &= ~UART_IER_THRI;
89 serial_out(up, UART_IER, up->ier);
90 }
91}
92
93static void serial_pxa_stop_rx(struct uart_port *port)
94{
95 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
96
97 up->ier &= ~UART_IER_RLSI;
98 up->port.read_status_mask &= ~UART_LSR_DR;
99 serial_out(up, UART_IER, up->ier);
100}
101
102static inline void
103receive_chars(struct uart_pxa_port *up, int *status, struct pt_regs *regs)
104{
105 struct tty_struct *tty = up->port.info->tty;
106 unsigned int ch, flag;
107 int max_count = 256;
108
109 do {
110 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
111 if (tty->low_latency)
112 tty_flip_buffer_push(tty);
113 /*
114 * If this failed then we will throw away the
115 * bytes but must do so to clear interrupts
116 */
117 }
118 ch = serial_in(up, UART_RX);
119 flag = TTY_NORMAL;
120 up->port.icount.rx++;
121
122 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
123 UART_LSR_FE | UART_LSR_OE))) {
124 /*
125 * For statistics only
126 */
127 if (*status & UART_LSR_BI) {
128 *status &= ~(UART_LSR_FE | UART_LSR_PE);
129 up->port.icount.brk++;
130 /*
131 * We do the SysRQ and SAK checking
132 * here because otherwise the break
133 * may get masked by ignore_status_mask
134 * or read_status_mask.
135 */
136 if (uart_handle_break(&up->port))
137 goto ignore_char;
138 } else if (*status & UART_LSR_PE)
139 up->port.icount.parity++;
140 else if (*status & UART_LSR_FE)
141 up->port.icount.frame++;
142 if (*status & UART_LSR_OE)
143 up->port.icount.overrun++;
144
145 /*
146 * Mask off conditions which should be ignored.
147 */
148 *status &= up->port.read_status_mask;
149
150#ifdef CONFIG_SERIAL_PXA_CONSOLE
151 if (up->port.line == up->port.cons->index) {
152 /* Recover the break flag from console xmit */
153 *status |= up->lsr_break_flag;
154 up->lsr_break_flag = 0;
155 }
156#endif
157 if (*status & UART_LSR_BI) {
158 flag = TTY_BREAK;
159 } else if (*status & UART_LSR_PE)
160 flag = TTY_PARITY;
161 else if (*status & UART_LSR_FE)
162 flag = TTY_FRAME;
163 }
164 if (uart_handle_sysrq_char(&up->port, ch, regs))
165 goto ignore_char;
166 if ((*status & up->port.ignore_status_mask) == 0) {
167 tty_insert_flip_char(tty, ch, flag);
168 }
169 if ((*status & UART_LSR_OE) &&
170 tty->flip.count < TTY_FLIPBUF_SIZE) {
171 /*
172 * Overrun is special, since it's reported
173 * immediately, and doesn't affect the current
174 * character.
175 */
176 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
177 }
178 ignore_char:
179 *status = serial_in(up, UART_LSR);
180 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
181 tty_flip_buffer_push(tty);
182}
183
184static void transmit_chars(struct uart_pxa_port *up)
185{
186 struct circ_buf *xmit = &up->port.info->xmit;
187 int count;
188
189 if (up->port.x_char) {
190 serial_out(up, UART_TX, up->port.x_char);
191 up->port.icount.tx++;
192 up->port.x_char = 0;
193 return;
194 }
195 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
196 serial_pxa_stop_tx(&up->port, 0);
197 return;
198 }
199
200 count = up->port.fifosize / 2;
201 do {
202 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
203 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
204 up->port.icount.tx++;
205 if (uart_circ_empty(xmit))
206 break;
207 } while (--count > 0);
208
209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
210 uart_write_wakeup(&up->port);
211
212
213 if (uart_circ_empty(xmit))
214 serial_pxa_stop_tx(&up->port, 0);
215}
216
217static void serial_pxa_start_tx(struct uart_port *port, unsigned int tty_start)
218{
219 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
220
221 if (!(up->ier & UART_IER_THRI)) {
222 up->ier |= UART_IER_THRI;
223 serial_out(up, UART_IER, up->ier);
224 }
225}
226
227static inline void check_modem_status(struct uart_pxa_port *up)
228{
229 int status;
230
231 status = serial_in(up, UART_MSR);
232
233 if ((status & UART_MSR_ANY_DELTA) == 0)
234 return;
235
236 if (status & UART_MSR_TERI)
237 up->port.icount.rng++;
238 if (status & UART_MSR_DDSR)
239 up->port.icount.dsr++;
240 if (status & UART_MSR_DDCD)
241 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
242 if (status & UART_MSR_DCTS)
243 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
244
245 wake_up_interruptible(&up->port.info->delta_msr_wait);
246}
247
248/*
249 * This handles the interrupt from one port.
250 */
251static inline irqreturn_t
252serial_pxa_irq(int irq, void *dev_id, struct pt_regs *regs)
253{
254 struct uart_pxa_port *up = (struct uart_pxa_port *)dev_id;
255 unsigned int iir, lsr;
256
257 iir = serial_in(up, UART_IIR);
258 if (iir & UART_IIR_NO_INT)
259 return IRQ_NONE;
260 lsr = serial_in(up, UART_LSR);
261 if (lsr & UART_LSR_DR)
262 receive_chars(up, &lsr, regs);
263 check_modem_status(up);
264 if (lsr & UART_LSR_THRE)
265 transmit_chars(up);
266 return IRQ_HANDLED;
267}
268
269static unsigned int serial_pxa_tx_empty(struct uart_port *port)
270{
271 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
272 unsigned long flags;
273 unsigned int ret;
274
275 spin_lock_irqsave(&up->port.lock, flags);
276 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
277 spin_unlock_irqrestore(&up->port.lock, flags);
278
279 return ret;
280}
281
282static unsigned int serial_pxa_get_mctrl(struct uart_port *port)
283{
284 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
285 unsigned long flags;
286 unsigned char status;
287 unsigned int ret;
288
289return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
290 spin_lock_irqsave(&up->port.lock, flags);
291 status = serial_in(up, UART_MSR);
292 spin_unlock_irqrestore(&up->port.lock, flags);
293
294 ret = 0;
295 if (status & UART_MSR_DCD)
296 ret |= TIOCM_CAR;
297 if (status & UART_MSR_RI)
298 ret |= TIOCM_RNG;
299 if (status & UART_MSR_DSR)
300 ret |= TIOCM_DSR;
301 if (status & UART_MSR_CTS)
302 ret |= TIOCM_CTS;
303 return ret;
304}
305
306static void serial_pxa_set_mctrl(struct uart_port *port, unsigned int mctrl)
307{
308 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
309 unsigned char mcr = 0;
310
311 if (mctrl & TIOCM_RTS)
312 mcr |= UART_MCR_RTS;
313 if (mctrl & TIOCM_DTR)
314 mcr |= UART_MCR_DTR;
315 if (mctrl & TIOCM_OUT1)
316 mcr |= UART_MCR_OUT1;
317 if (mctrl & TIOCM_OUT2)
318 mcr |= UART_MCR_OUT2;
319 if (mctrl & TIOCM_LOOP)
320 mcr |= UART_MCR_LOOP;
321
322 mcr |= up->mcr;
323
324 serial_out(up, UART_MCR, mcr);
325}
326
327static void serial_pxa_break_ctl(struct uart_port *port, int break_state)
328{
329 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
330 unsigned long flags;
331
332 spin_lock_irqsave(&up->port.lock, flags);
333 if (break_state == -1)
334 up->lcr |= UART_LCR_SBC;
335 else
336 up->lcr &= ~UART_LCR_SBC;
337 serial_out(up, UART_LCR, up->lcr);
338 spin_unlock_irqrestore(&up->port.lock, flags);
339}
340
341#if 0
342static void serial_pxa_dma_init(struct pxa_uart *up)
343{
344 up->rxdma =
345 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_receive_dma, up);
346 if (up->rxdma < 0)
347 goto out;
348 up->txdma =
349 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_transmit_dma, up);
350 if (up->txdma < 0)
351 goto err_txdma;
352 up->dmadesc = kmalloc(4 * sizeof(pxa_dma_desc), GFP_KERNEL);
353 if (!up->dmadesc)
354 goto err_alloc;
355
356 /* ... */
357err_alloc:
358 pxa_free_dma(up->txdma);
359err_rxdma:
360 pxa_free_dma(up->rxdma);
361out:
362 return;
363}
364#endif
365
366static int serial_pxa_startup(struct uart_port *port)
367{
368 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
369 unsigned long flags;
370 int retval;
371
372 up->mcr = 0;
373
374 /*
375 * Allocate the IRQ
376 */
377 retval = request_irq(up->port.irq, serial_pxa_irq, 0, up->name, up);
378 if (retval)
379 return retval;
380
381 /*
382 * Clear the FIFO buffers and disable them.
383 * (they will be reenabled in set_termios())
384 */
385 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
386 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
387 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
388 serial_out(up, UART_FCR, 0);
389
390 /*
391 * Clear the interrupt registers.
392 */
393 (void) serial_in(up, UART_LSR);
394 (void) serial_in(up, UART_RX);
395 (void) serial_in(up, UART_IIR);
396 (void) serial_in(up, UART_MSR);
397
398 /*
399 * Now, initialize the UART
400 */
401 serial_out(up, UART_LCR, UART_LCR_WLEN8);
402
403 spin_lock_irqsave(&up->port.lock, flags);
404 up->port.mctrl |= TIOCM_OUT2;
405 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
406 spin_unlock_irqrestore(&up->port.lock, flags);
407
408 /*
409 * Finally, enable interrupts. Note: Modem status interrupts
410 * are set via set_termios(), which will be occuring imminently
411 * anyway, so we don't enable them here.
412 */
413 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
414 serial_out(up, UART_IER, up->ier);
415
416 /*
417 * And clear the interrupt registers again for luck.
418 */
419 (void) serial_in(up, UART_LSR);
420 (void) serial_in(up, UART_RX);
421 (void) serial_in(up, UART_IIR);
422 (void) serial_in(up, UART_MSR);
423
424 return 0;
425}
426
427static void serial_pxa_shutdown(struct uart_port *port)
428{
429 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
430 unsigned long flags;
431
432 free_irq(up->port.irq, up);
433
434 /*
435 * Disable interrupts from this port
436 */
437 up->ier = 0;
438 serial_out(up, UART_IER, 0);
439
440 spin_lock_irqsave(&up->port.lock, flags);
441 up->port.mctrl &= ~TIOCM_OUT2;
442 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
443 spin_unlock_irqrestore(&up->port.lock, flags);
444
445 /*
446 * Disable break condition and FIFOs
447 */
448 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
449 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
450 UART_FCR_CLEAR_RCVR |
451 UART_FCR_CLEAR_XMIT);
452 serial_out(up, UART_FCR, 0);
453}
454
455static void
456serial_pxa_set_termios(struct uart_port *port, struct termios *termios,
457 struct termios *old)
458{
459 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
460 unsigned char cval, fcr = 0;
461 unsigned long flags;
462 unsigned int baud, quot;
463
464 switch (termios->c_cflag & CSIZE) {
465 case CS5:
466 cval = 0x00;
467 break;
468 case CS6:
469 cval = 0x01;
470 break;
471 case CS7:
472 cval = 0x02;
473 break;
474 default:
475 case CS8:
476 cval = 0x03;
477 break;
478 }
479
480 if (termios->c_cflag & CSTOPB)
481 cval |= 0x04;
482 if (termios->c_cflag & PARENB)
483 cval |= UART_LCR_PARITY;
484 if (!(termios->c_cflag & PARODD))
485 cval |= UART_LCR_EPAR;
486
487 /*
488 * Ask the core to calculate the divisor for us.
489 */
490 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
491 quot = uart_get_divisor(port, baud);
492
493 if ((up->port.uartclk / quot) < (2400 * 16))
494 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR1;
495 else
496 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR8;
497
498 /*
499 * Ok, we're now changing the port state. Do it with
500 * interrupts disabled.
501 */
502 spin_lock_irqsave(&up->port.lock, flags);
503
504 /*
505 * Ensure the port will be enabled.
506 * This is required especially for serial console.
507 */
508 up->ier |= IER_UUE;
509
510 /*
511 * Update the per-port timeout.
512 */
513 uart_update_timeout(port, termios->c_cflag, quot);
514
515 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
516 if (termios->c_iflag & INPCK)
517 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
518 if (termios->c_iflag & (BRKINT | PARMRK))
519 up->port.read_status_mask |= UART_LSR_BI;
520
521 /*
522 * Characters to ignore
523 */
524 up->port.ignore_status_mask = 0;
525 if (termios->c_iflag & IGNPAR)
526 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
527 if (termios->c_iflag & IGNBRK) {
528 up->port.ignore_status_mask |= UART_LSR_BI;
529 /*
530 * If we're ignoring parity and break indicators,
531 * ignore overruns too (for real raw support).
532 */
533 if (termios->c_iflag & IGNPAR)
534 up->port.ignore_status_mask |= UART_LSR_OE;
535 }
536
537 /*
538 * ignore all characters if CREAD is not set
539 */
540 if ((termios->c_cflag & CREAD) == 0)
541 up->port.ignore_status_mask |= UART_LSR_DR;
542
543 /*
544 * CTS flow control flag and modem status interrupts
545 */
546 up->ier &= ~UART_IER_MSI;
547 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
548 up->ier |= UART_IER_MSI;
549
550 serial_out(up, UART_IER, up->ier);
551
552 serial_out(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
553 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
554 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
555 serial_out(up, UART_LCR, cval); /* reset DLAB */
556 up->lcr = cval; /* Save LCR */
557 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
558 serial_out(up, UART_FCR, fcr);
559 spin_unlock_irqrestore(&up->port.lock, flags);
560}
561
562static void
563serial_pxa_pm(struct uart_port *port, unsigned int state,
564 unsigned int oldstate)
565{
566 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
567 pxa_set_cken(up->cken, !state);
568 if (!state)
569 udelay(1);
570}
571
572static void serial_pxa_release_port(struct uart_port *port)
573{
574}
575
576static int serial_pxa_request_port(struct uart_port *port)
577{
578 return 0;
579}
580
581static void serial_pxa_config_port(struct uart_port *port, int flags)
582{
583 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
584 up->port.type = PORT_PXA;
585}
586
587static int
588serial_pxa_verify_port(struct uart_port *port, struct serial_struct *ser)
589{
590 /* we don't want the core code to modify any port params */
591 return -EINVAL;
592}
593
594static const char *
595serial_pxa_type(struct uart_port *port)
596{
597 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
598 return up->name;
599}
600
601#ifdef CONFIG_SERIAL_PXA_CONSOLE
602
603extern struct uart_pxa_port serial_pxa_ports[];
604extern struct uart_driver serial_pxa_reg;
605
606#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
607
608/*
609 * Wait for transmitter & holding register to empty
610 */
611static inline void wait_for_xmitr(struct uart_pxa_port *up)
612{
613 unsigned int status, tmout = 10000;
614
615 /* Wait up to 10ms for the character(s) to be sent. */
616 do {
617 status = serial_in(up, UART_LSR);
618
619 if (status & UART_LSR_BI)
620 up->lsr_break_flag = UART_LSR_BI;
621
622 if (--tmout == 0)
623 break;
624 udelay(1);
625 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
626
627 /* Wait up to 1s for flow control if necessary */
628 if (up->port.flags & UPF_CONS_FLOW) {
629 tmout = 1000000;
630 while (--tmout &&
631 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
632 udelay(1);
633 }
634}
635
636/*
637 * Print a string to the serial port trying not to disturb
638 * any possible real use of the port...
639 *
640 * The console_lock must be held when we get here.
641 */
642static void
643serial_pxa_console_write(struct console *co, const char *s, unsigned int count)
644{
645 struct uart_pxa_port *up = &serial_pxa_ports[co->index];
646 unsigned int ier;
647 int i;
648
649 /*
650 * First save the UER then disable the interrupts
651 */
652 ier = serial_in(up, UART_IER);
653 serial_out(up, UART_IER, UART_IER_UUE);
654
655 /*
656 * Now, do each character
657 */
658 for (i = 0; i < count; i++, s++) {
659 wait_for_xmitr(up);
660
661 /*
662 * Send the character out.
663 * If a LF, also do CR...
664 */
665 serial_out(up, UART_TX, *s);
666 if (*s == 10) {
667 wait_for_xmitr(up);
668 serial_out(up, UART_TX, 13);
669 }
670 }
671
672 /*
673 * Finally, wait for transmitter to become empty
674 * and restore the IER
675 */
676 wait_for_xmitr(up);
677 serial_out(up, UART_IER, ier);
678}
679
680static int __init
681serial_pxa_console_setup(struct console *co, char *options)
682{
683 struct uart_pxa_port *up;
684 int baud = 9600;
685 int bits = 8;
686 int parity = 'n';
687 int flow = 'n';
688
689 if (co->index == -1 || co->index >= serial_pxa_reg.nr)
690 co->index = 0;
691 up = &serial_pxa_ports[co->index];
692
693 if (options)
694 uart_parse_options(options, &baud, &parity, &bits, &flow);
695
696 return uart_set_options(&up->port, co, baud, parity, bits, flow);
697}
698
699static struct console serial_pxa_console = {
700 .name = "ttyS",
701 .write = serial_pxa_console_write,
702 .device = uart_console_device,
703 .setup = serial_pxa_console_setup,
704 .flags = CON_PRINTBUFFER,
705 .index = -1,
706 .data = &serial_pxa_reg,
707};
708
709static int __init
710serial_pxa_console_init(void)
711{
712 register_console(&serial_pxa_console);
713 return 0;
714}
715
716console_initcall(serial_pxa_console_init);
717
718#define PXA_CONSOLE &serial_pxa_console
719#else
720#define PXA_CONSOLE NULL
721#endif
722
723struct uart_ops serial_pxa_pops = {
724 .tx_empty = serial_pxa_tx_empty,
725 .set_mctrl = serial_pxa_set_mctrl,
726 .get_mctrl = serial_pxa_get_mctrl,
727 .stop_tx = serial_pxa_stop_tx,
728 .start_tx = serial_pxa_start_tx,
729 .stop_rx = serial_pxa_stop_rx,
730 .enable_ms = serial_pxa_enable_ms,
731 .break_ctl = serial_pxa_break_ctl,
732 .startup = serial_pxa_startup,
733 .shutdown = serial_pxa_shutdown,
734 .set_termios = serial_pxa_set_termios,
735 .pm = serial_pxa_pm,
736 .type = serial_pxa_type,
737 .release_port = serial_pxa_release_port,
738 .request_port = serial_pxa_request_port,
739 .config_port = serial_pxa_config_port,
740 .verify_port = serial_pxa_verify_port,
741};
742
743static struct uart_pxa_port serial_pxa_ports[] = {
744 { /* FFUART */
745 .name = "FFUART",
746 .cken = CKEN6_FFUART,
747 .port = {
748 .type = PORT_PXA,
749 .iotype = UPIO_MEM,
750 .membase = (void *)&FFUART,
751 .mapbase = __PREG(FFUART),
752 .irq = IRQ_FFUART,
753 .uartclk = 921600 * 16,
754 .fifosize = 64,
755 .ops = &serial_pxa_pops,
756 .line = 0,
757 },
758 }, { /* BTUART */
759 .name = "BTUART",
760 .cken = CKEN7_BTUART,
761 .port = {
762 .type = PORT_PXA,
763 .iotype = UPIO_MEM,
764 .membase = (void *)&BTUART,
765 .mapbase = __PREG(BTUART),
766 .irq = IRQ_BTUART,
767 .uartclk = 921600 * 16,
768 .fifosize = 64,
769 .ops = &serial_pxa_pops,
770 .line = 1,
771 },
772 }, { /* STUART */
773 .name = "STUART",
774 .cken = CKEN5_STUART,
775 .port = {
776 .type = PORT_PXA,
777 .iotype = UPIO_MEM,
778 .membase = (void *)&STUART,
779 .mapbase = __PREG(STUART),
780 .irq = IRQ_STUART,
781 .uartclk = 921600 * 16,
782 .fifosize = 64,
783 .ops = &serial_pxa_pops,
784 .line = 2,
785 },
786 }
787};
788
789static struct uart_driver serial_pxa_reg = {
790 .owner = THIS_MODULE,
791 .driver_name = "PXA serial",
792 .devfs_name = "tts/",
793 .dev_name = "ttyS",
794 .major = TTY_MAJOR,
795 .minor = 64,
796 .nr = ARRAY_SIZE(serial_pxa_ports),
797 .cons = PXA_CONSOLE,
798};
799
800static int serial_pxa_suspend(struct device *_dev, u32 state, u32 level)
801{
802 struct uart_pxa_port *sport = dev_get_drvdata(_dev);
803
804 if (sport && level == SUSPEND_DISABLE)
805 uart_suspend_port(&serial_pxa_reg, &sport->port);
806
807 return 0;
808}
809
810static int serial_pxa_resume(struct device *_dev, u32 level)
811{
812 struct uart_pxa_port *sport = dev_get_drvdata(_dev);
813
814 if (sport && level == RESUME_ENABLE)
815 uart_resume_port(&serial_pxa_reg, &sport->port);
816
817 return 0;
818}
819
820static int serial_pxa_probe(struct device *_dev)
821{
822 struct platform_device *dev = to_platform_device(_dev);
823
824 serial_pxa_ports[dev->id].port.dev = _dev;
825 uart_add_one_port(&serial_pxa_reg, &serial_pxa_ports[dev->id].port);
826 dev_set_drvdata(_dev, &serial_pxa_ports[dev->id]);
827 return 0;
828}
829
830static int serial_pxa_remove(struct device *_dev)
831{
832 struct uart_pxa_port *sport = dev_get_drvdata(_dev);
833
834 dev_set_drvdata(_dev, NULL);
835
836 if (sport)
837 uart_remove_one_port(&serial_pxa_reg, &sport->port);
838
839 return 0;
840}
841
842static struct device_driver serial_pxa_driver = {
843 .name = "pxa2xx-uart",
844 .bus = &platform_bus_type,
845 .probe = serial_pxa_probe,
846 .remove = serial_pxa_remove,
847
848 .suspend = serial_pxa_suspend,
849 .resume = serial_pxa_resume,
850};
851
852int __init serial_pxa_init(void)
853{
854 int ret;
855
856 ret = uart_register_driver(&serial_pxa_reg);
857 if (ret != 0)
858 return ret;
859
860 ret = driver_register(&serial_pxa_driver);
861 if (ret != 0)
862 uart_unregister_driver(&serial_pxa_reg);
863
864 return ret;
865}
866
867void __exit serial_pxa_exit(void)
868{
869 driver_unregister(&serial_pxa_driver);
870 uart_unregister_driver(&serial_pxa_reg);
871}
872
873module_init(serial_pxa_init);
874module_exit(serial_pxa_exit);
875
876MODULE_LICENSE("GPL");
877
diff --git a/drivers/serial/s3c2410.c b/drivers/serial/s3c2410.c
new file mode 100644
index 000000000000..ffc61389614b
--- /dev/null
+++ b/drivers/serial/s3c2410.c
@@ -0,0 +1,1831 @@
1/*
2 * linux/drivers/serial/s3c2410.c
3 *
4 * Driver for onboard UARTs on the Samsung S3C24XX
5 *
6 * Based on drivers/char/serial.c and drivers/char/21285.c
7 *
8 * Ben Dooks, (c) 2003-2005 Simtec Electronics
9 * http://www.simtec.co.uk/products/SWLINUX/
10 *
11 * Changelog:
12 *
13 * 22-Jul-2004 BJD Finished off device rewrite
14 *
15 * 21-Jul-2004 BJD Thanks to <herbet@13thfloor.at> for pointing out
16 * problems with baud rate and loss of IR settings. Update
17 * to add configuration via platform_device structure
18 *
19 * 28-Sep-2004 BJD Re-write for the following items
20 * - S3C2410 and S3C2440 serial support
21 * - Power Management support
22 * - Fix console via IrDA devices
23 * - SysReq (Herbert Pötzl)
24 * - Break character handling (Herbert Pötzl)
25 * - spin-lock initialisation (Dimitry Andric)
26 * - added clock control
27 * - updated init code to use platform_device info
28 *
29 * 06-Mar-2005 BJD Add s3c2440 fclk clock source
30 *
31 * 09-Mar-2005 BJD Add s3c2400 support
32 *
33 * 10-Mar-2005 LCVR Changed S3C2410_VA_UART to S3C24XX_VA_UART
34*/
35
36/* Note on 2440 fclk clock source handling
37 *
38 * Whilst it is possible to use the fclk as clock source, the method
39 * of properly switching too/from this is currently un-implemented, so
40 * whichever way is configured at startup is the one that will be used.
41*/
42
43/* Hote on 2410 error handling
44 *
45 * The s3c2410 manual has a love/hate affair with the contents of the
46 * UERSTAT register in the UART blocks, and keeps marking some of the
47 * error bits as reserved. Having checked with the s3c2410x01,
48 * it copes with BREAKs properly, so I am happy to ignore the RESERVED
49 * feature from the latter versions of the manual.
50 *
51 * If it becomes aparrent that latter versions of the 2410 remove these
52 * bits, then action will have to be taken to differentiate the versions
53 * and change the policy on BREAK
54 *
55 * BJD, 04-Nov-2004
56*/
57
58#include <linux/config.h>
59
60#if defined(CONFIG_SERIAL_S3C2410_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
61#define SUPPORT_SYSRQ
62#endif
63
64#include <linux/module.h>
65#include <linux/ioport.h>
66#include <linux/device.h>
67#include <linux/init.h>
68#include <linux/sysrq.h>
69#include <linux/console.h>
70#include <linux/tty.h>
71#include <linux/tty_flip.h>
72#include <linux/serial_core.h>
73#include <linux/serial.h>
74#include <linux/delay.h>
75
76#include <asm/io.h>
77#include <asm/irq.h>
78
79#include <asm/hardware.h>
80#include <asm/hardware/clock.h>
81
82#include <asm/arch/regs-serial.h>
83#include <asm/arch/regs-gpio.h>
84
85#include <asm/mach-types.h>
86
87/* structures */
88
89struct s3c24xx_uart_info {
90 char *name;
91 unsigned int type;
92 unsigned int fifosize;
93 unsigned long rx_fifomask;
94 unsigned long rx_fifoshift;
95 unsigned long rx_fifofull;
96 unsigned long tx_fifomask;
97 unsigned long tx_fifoshift;
98 unsigned long tx_fifofull;
99
100 /* clock source control */
101
102 int (*get_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
103 int (*set_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
104
105 /* uart controls */
106 int (*reset_port)(struct uart_port *, struct s3c2410_uartcfg *);
107};
108
109struct s3c24xx_uart_port {
110 unsigned char rx_claimed;
111 unsigned char tx_claimed;
112
113 struct s3c24xx_uart_info *info;
114 struct s3c24xx_uart_clksrc *clksrc;
115 struct clk *clk;
116 struct clk *baudclk;
117 struct uart_port port;
118};
119
120
121/* configuration defines */
122
123#if 0
124#if 1
125/* send debug to the low-level output routines */
126
127extern void printascii(const char *);
128
129static void
130s3c24xx_serial_dbg(const char *fmt, ...)
131{
132 va_list va;
133 char buff[256];
134
135 va_start(va, fmt);
136 vsprintf(buff, fmt, va);
137 va_end(va);
138
139 printascii(buff);
140}
141
142#define dbg(x...) s3c24xx_serial_dbg(x)
143
144#else
145#define dbg(x...) printk(KERN_DEBUG "s3c24xx: ");
146#endif
147#else /* no debug */
148#define dbg(x...) do {} while(0)
149#endif
150
151/* UART name and device definitions */
152
153#define S3C24XX_SERIAL_NAME "ttySAC"
154#define S3C24XX_SERIAL_DEVFS "tts/"
155#define S3C24XX_SERIAL_MAJOR 204
156#define S3C24XX_SERIAL_MINOR 64
157
158
159/* conversion functions */
160
161#define s3c24xx_dev_to_port(__dev) (struct uart_port *)dev_get_drvdata(__dev)
162#define s3c24xx_dev_to_cfg(__dev) (struct s3c2410_uartcfg *)((__dev)->platform_data)
163
164/* we can support 3 uarts, but not always use them */
165
166#define NR_PORTS (3)
167
168/* port irq numbers */
169
170#define TX_IRQ(port) ((port)->irq + 1)
171#define RX_IRQ(port) ((port)->irq)
172
173/* register access controls */
174
175#define portaddr(port, reg) ((port)->membase + (reg))
176
177#define rd_regb(port, reg) (__raw_readb(portaddr(port, reg)))
178#define rd_regl(port, reg) (__raw_readl(portaddr(port, reg)))
179
180#define wr_regb(port, reg, val) \
181 do { __raw_writeb(val, portaddr(port, reg)); } while(0)
182
183#define wr_regl(port, reg, val) \
184 do { __raw_writel(val, portaddr(port, reg)); } while(0)
185
186/* macros to change one thing to another */
187
188#define tx_enabled(port) ((port)->unused[0])
189#define rx_enabled(port) ((port)->unused[1])
190
191/* flag to ignore all characters comming in */
192#define RXSTAT_DUMMY_READ (0x10000000)
193
194static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
195{
196 return container_of(port, struct s3c24xx_uart_port, port);
197}
198
199/* translate a port to the device name */
200
201static inline char *s3c24xx_serial_portname(struct uart_port *port)
202{
203 return to_platform_device(port->dev)->name;
204}
205
206static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
207{
208 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
209}
210
211static void s3c24xx_serial_rx_enable(struct uart_port *port)
212{
213 unsigned long flags;
214 unsigned int ucon, ufcon;
215 int count = 10000;
216
217 spin_lock_irqsave(&port->lock, flags);
218
219 while (--count && !s3c24xx_serial_txempty_nofifo(port))
220 udelay(100);
221
222 ufcon = rd_regl(port, S3C2410_UFCON);
223 ufcon |= S3C2410_UFCON_RESETRX;
224 wr_regl(port, S3C2410_UFCON, ufcon);
225
226 ucon = rd_regl(port, S3C2410_UCON);
227 ucon |= S3C2410_UCON_RXIRQMODE;
228 wr_regl(port, S3C2410_UCON, ucon);
229
230 rx_enabled(port) = 1;
231 spin_unlock_irqrestore(&port->lock, flags);
232}
233
234static void s3c24xx_serial_rx_disable(struct uart_port *port)
235{
236 unsigned long flags;
237 unsigned int ucon;
238
239 spin_lock_irqsave(&port->lock, flags);
240
241 ucon = rd_regl(port, S3C2410_UCON);
242 ucon &= ~S3C2410_UCON_RXIRQMODE;
243 wr_regl(port, S3C2410_UCON, ucon);
244
245 rx_enabled(port) = 0;
246 spin_unlock_irqrestore(&port->lock, flags);
247}
248
249static void
250s3c24xx_serial_stop_tx(struct uart_port *port, unsigned int tty_stop)
251{
252 if (tx_enabled(port)) {
253 disable_irq(TX_IRQ(port));
254 tx_enabled(port) = 0;
255 if (port->flags & UPF_CONS_FLOW)
256 s3c24xx_serial_rx_enable(port);
257 }
258}
259
260static void
261s3c24xx_serial_start_tx(struct uart_port *port, unsigned int tty_start)
262{
263 if (!tx_enabled(port)) {
264 if (port->flags & UPF_CONS_FLOW)
265 s3c24xx_serial_rx_disable(port);
266
267 enable_irq(TX_IRQ(port));
268 tx_enabled(port) = 1;
269 }
270}
271
272
273static void s3c24xx_serial_stop_rx(struct uart_port *port)
274{
275 if (rx_enabled(port)) {
276 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
277 disable_irq(RX_IRQ(port));
278 rx_enabled(port) = 0;
279 }
280}
281
282static void s3c24xx_serial_enable_ms(struct uart_port *port)
283{
284}
285
286static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
287{
288 return to_ourport(port)->info;
289}
290
291static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
292{
293 if (port->dev == NULL)
294 return NULL;
295
296 return (struct s3c2410_uartcfg *)port->dev->platform_data;
297}
298
299static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
300 unsigned long ufstat)
301{
302 struct s3c24xx_uart_info *info = ourport->info;
303
304 if (ufstat & info->rx_fifofull)
305 return info->fifosize;
306
307 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
308}
309
310
311/* ? - where has parity gone?? */
312#define S3C2410_UERSTAT_PARITY (0x1000)
313
314static irqreturn_t
315s3c24xx_serial_rx_chars(int irq, void *dev_id, struct pt_regs *regs)
316{
317 struct s3c24xx_uart_port *ourport = dev_id;
318 struct uart_port *port = &ourport->port;
319 struct tty_struct *tty = port->info->tty;
320 unsigned int ufcon, ch, flag, ufstat, uerstat;
321 int max_count = 64;
322
323 while (max_count-- > 0) {
324 ufcon = rd_regl(port, S3C2410_UFCON);
325 ufstat = rd_regl(port, S3C2410_UFSTAT);
326
327 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
328 break;
329
330 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
331 if (tty->low_latency)
332 tty_flip_buffer_push(tty);
333
334 /*
335 * If this failed then we will throw away the
336 * bytes but must do so to clear interrupts
337 */
338 }
339
340 uerstat = rd_regl(port, S3C2410_UERSTAT);
341 ch = rd_regb(port, S3C2410_URXH);
342
343 if (port->flags & UPF_CONS_FLOW) {
344 int txe = s3c24xx_serial_txempty_nofifo(port);
345
346 if (rx_enabled(port)) {
347 if (!txe) {
348 rx_enabled(port) = 0;
349 continue;
350 }
351 } else {
352 if (txe) {
353 ufcon |= S3C2410_UFCON_RESETRX;
354 wr_regl(port, S3C2410_UFCON, ufcon);
355 rx_enabled(port) = 1;
356 goto out;
357 }
358 continue;
359 }
360 }
361
362 /* insert the character into the buffer */
363
364 flag = TTY_NORMAL;
365 port->icount.rx++;
366
367 if (uerstat & S3C2410_UERSTAT_ANY) {
368 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
369 ch, uerstat);
370
371 /* check for break */
372 if (uerstat & S3C2410_UERSTAT_BREAK) {
373 dbg("break!\n");
374 port->icount.brk++;
375 if (uart_handle_break(port))
376 goto ignore_char;
377 }
378
379 if (uerstat & S3C2410_UERSTAT_FRAME)
380 port->icount.frame++;
381 if (uerstat & S3C2410_UERSTAT_OVERRUN)
382 port->icount.overrun++;
383
384 uerstat &= port->read_status_mask;
385
386 if (uerstat & S3C2410_UERSTAT_BREAK)
387 flag = TTY_BREAK;
388 else if (uerstat & S3C2410_UERSTAT_PARITY)
389 flag = TTY_PARITY;
390 else if (uerstat & ( S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_OVERRUN))
391 flag = TTY_FRAME;
392 }
393
394 if (uart_handle_sysrq_char(port, ch, regs))
395 goto ignore_char;
396
397 if ((uerstat & port->ignore_status_mask) == 0) {
398 tty_insert_flip_char(tty, ch, flag);
399 }
400
401 if ((uerstat & S3C2410_UERSTAT_OVERRUN) &&
402 tty->flip.count < TTY_FLIPBUF_SIZE) {
403 /*
404 * Overrun is special, since it's reported
405 * immediately, and doesn't affect the current
406 * character.
407 */
408
409 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
410 }
411
412 ignore_char:
413 continue;
414 }
415 tty_flip_buffer_push(tty);
416
417 out:
418 return IRQ_HANDLED;
419}
420
421static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id, struct pt_regs *regs)
422{
423 struct s3c24xx_uart_port *ourport = id;
424 struct uart_port *port = &ourport->port;
425 struct circ_buf *xmit = &port->info->xmit;
426 int count = 256;
427
428 if (port->x_char) {
429 wr_regb(port, S3C2410_UTXH, port->x_char);
430 port->icount.tx++;
431 port->x_char = 0;
432 goto out;
433 }
434
435 /* if there isnt anything more to transmit, or the uart is now
436 * stopped, disable the uart and exit
437 */
438
439 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
440 s3c24xx_serial_stop_tx(port, 0);
441 goto out;
442 }
443
444 /* try and drain the buffer... */
445
446 while (!uart_circ_empty(xmit) && count-- > 0) {
447 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
448 break;
449
450 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
451 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
452 port->icount.tx++;
453 }
454
455 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
456 uart_write_wakeup(port);
457
458 if (uart_circ_empty(xmit))
459 s3c24xx_serial_stop_tx(port, 0);
460
461 out:
462 return IRQ_HANDLED;
463}
464
465static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
466{
467 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
468 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
469 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
470
471 if (ufcon & S3C2410_UFCON_FIFOMODE) {
472 if ((ufstat & info->tx_fifomask) != 0 ||
473 (ufstat & info->tx_fifofull))
474 return 0;
475
476 return 1;
477 }
478
479 return s3c24xx_serial_txempty_nofifo(port);
480}
481
482/* no modem control lines */
483static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
484{
485 unsigned int umstat = rd_regb(port,S3C2410_UMSTAT);
486
487 if (umstat & S3C2410_UMSTAT_CTS)
488 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
489 else
490 return TIOCM_CAR | TIOCM_DSR;
491}
492
493static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
494{
495 /* todo - possibly remove AFC and do manual CTS */
496}
497
498static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
499{
500 unsigned long flags;
501 unsigned int ucon;
502
503 spin_lock_irqsave(&port->lock, flags);
504
505 ucon = rd_regl(port, S3C2410_UCON);
506
507 if (break_state)
508 ucon |= S3C2410_UCON_SBREAK;
509 else
510 ucon &= ~S3C2410_UCON_SBREAK;
511
512 wr_regl(port, S3C2410_UCON, ucon);
513
514 spin_unlock_irqrestore(&port->lock, flags);
515}
516
517static void s3c24xx_serial_shutdown(struct uart_port *port)
518{
519 struct s3c24xx_uart_port *ourport = to_ourport(port);
520
521 if (ourport->tx_claimed) {
522 free_irq(TX_IRQ(port), ourport);
523 tx_enabled(port) = 0;
524 ourport->tx_claimed = 0;
525 }
526
527 if (ourport->rx_claimed) {
528 free_irq(RX_IRQ(port), ourport);
529 ourport->rx_claimed = 0;
530 rx_enabled(port) = 0;
531 }
532}
533
534
535static int s3c24xx_serial_startup(struct uart_port *port)
536{
537 struct s3c24xx_uart_port *ourport = to_ourport(port);
538 unsigned long flags;
539 int ret;
540
541 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
542 port->mapbase, port->membase);
543
544 local_irq_save(flags);
545
546 rx_enabled(port) = 1;
547
548 ret = request_irq(RX_IRQ(port),
549 s3c24xx_serial_rx_chars, 0,
550 s3c24xx_serial_portname(port), ourport);
551
552 if (ret != 0) {
553 printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
554 return ret;
555 }
556
557 ourport->rx_claimed = 1;
558
559 dbg("requesting tx irq...\n");
560
561 tx_enabled(port) = 1;
562
563 ret = request_irq(TX_IRQ(port),
564 s3c24xx_serial_tx_chars, 0,
565 s3c24xx_serial_portname(port), ourport);
566
567 if (ret) {
568 printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
569 goto err;
570 }
571
572 ourport->tx_claimed = 1;
573
574 dbg("s3c24xx_serial_startup ok\n");
575
576 /* the port reset code should have done the correct
577 * register setup for the port controls */
578
579 local_irq_restore(flags);
580 return ret;
581
582 err:
583 s3c24xx_serial_shutdown(port);
584 local_irq_restore(flags);
585 return ret;
586}
587
588/* power power management control */
589
590static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
591 unsigned int old)
592{
593 struct s3c24xx_uart_port *ourport = to_ourport(port);
594
595 switch (level) {
596 case 3:
597 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
598 clk_disable(ourport->baudclk);
599
600 clk_disable(ourport->clk);
601 break;
602
603 case 0:
604 clk_enable(ourport->clk);
605
606 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
607 clk_enable(ourport->baudclk);
608
609 break;
610 default:
611 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
612 }
613}
614
615/* baud rate calculation
616 *
617 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
618 * of different sources, including the peripheral clock ("pclk") and an
619 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
620 * with a programmable extra divisor.
621 *
622 * The following code goes through the clock sources, and calculates the
623 * baud clocks (and the resultant actual baud rates) and then tries to
624 * pick the closest one and select that.
625 *
626*/
627
628
629#define MAX_CLKS (8)
630
631static struct s3c24xx_uart_clksrc tmp_clksrc = {
632 .name = "pclk",
633 .min_baud = 0,
634 .max_baud = 0,
635 .divisor = 1,
636};
637
638static inline int
639s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
640{
641 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
642
643 return (info->get_clksrc)(port, c);
644}
645
646static inline int
647s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
648{
649 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
650
651 return (info->set_clksrc)(port, c);
652}
653
654struct baud_calc {
655 struct s3c24xx_uart_clksrc *clksrc;
656 unsigned int calc;
657 unsigned int quot;
658 struct clk *src;
659};
660
661static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
662 struct uart_port *port,
663 struct s3c24xx_uart_clksrc *clksrc,
664 unsigned int baud)
665{
666 unsigned long rate;
667
668 calc->src = clk_get(port->dev, clksrc->name);
669 if (calc->src == NULL || IS_ERR(calc->src))
670 return 0;
671
672 rate = clk_get_rate(calc->src);
673 rate /= clksrc->divisor;
674
675 calc->clksrc = clksrc;
676 calc->quot = (rate + (8 * baud)) / (16 * baud);
677 calc->calc = (rate / (calc->quot * 16));
678
679 calc->quot--;
680 return 1;
681}
682
683static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
684 struct s3c24xx_uart_clksrc **clksrc,
685 struct clk **clk,
686 unsigned int baud)
687{
688 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
689 struct s3c24xx_uart_clksrc *clkp;
690 struct baud_calc res[MAX_CLKS];
691 struct baud_calc *resptr, *best, *sptr;
692 int i;
693
694 clkp = cfg->clocks;
695 best = NULL;
696
697 if (cfg->clocks_size < 2) {
698 if (cfg->clocks_size == 0)
699 clkp = &tmp_clksrc;
700
701 /* check to see if we're sourcing fclk, and if so we're
702 * going to have to update the clock source
703 */
704
705 if (strcmp(clkp->name, "fclk") == 0) {
706 struct s3c24xx_uart_clksrc src;
707
708 s3c24xx_serial_getsource(port, &src);
709
710 /* check that the port already using fclk, and if
711 * not, then re-select fclk
712 */
713
714 if (strcmp(src.name, clkp->name) == 0) {
715 s3c24xx_serial_setsource(port, clkp);
716 s3c24xx_serial_getsource(port, &src);
717 }
718
719 clkp->divisor = src.divisor;
720 }
721
722 s3c24xx_serial_calcbaud(res, port, clkp, baud);
723 best = res;
724 resptr = best + 1;
725 } else {
726 resptr = res;
727
728 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
729 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
730 resptr++;
731 }
732 }
733
734 /* ok, we now need to select the best clock we found */
735
736 if (!best) {
737 unsigned int deviation = (1<<30)|((1<<30)-1);
738 int calc_deviation;
739
740 for (sptr = res; sptr < resptr; sptr++) {
741 printk(KERN_DEBUG
742 "found clk %p (%s) quot %d, calc %d\n",
743 sptr->clksrc, sptr->clksrc->name,
744 sptr->quot, sptr->calc);
745
746 calc_deviation = baud - sptr->calc;
747 if (calc_deviation < 0)
748 calc_deviation = -calc_deviation;
749
750 if (calc_deviation < deviation) {
751 best = sptr;
752 deviation = calc_deviation;
753 }
754 }
755
756 printk(KERN_DEBUG "best %p (deviation %d)\n", best, deviation);
757 }
758
759 printk(KERN_DEBUG "selected clock %p (%s) quot %d, calc %d\n",
760 best->clksrc, best->clksrc->name, best->quot, best->calc);
761
762 /* store results to pass back */
763
764 *clksrc = best->clksrc;
765 *clk = best->src;
766
767 return best->quot;
768}
769
770static void s3c24xx_serial_set_termios(struct uart_port *port,
771 struct termios *termios,
772 struct termios *old)
773{
774 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
775 struct s3c24xx_uart_port *ourport = to_ourport(port);
776 struct s3c24xx_uart_clksrc *clksrc;
777 struct clk *clk;
778 unsigned long flags;
779 unsigned int baud, quot;
780 unsigned int ulcon;
781 unsigned int umcon;
782
783 /*
784 * We don't support modem control lines.
785 */
786 termios->c_cflag &= ~(HUPCL | CMSPAR);
787 termios->c_cflag |= CLOCAL;
788
789 /*
790 * Ask the core to calculate the divisor for us.
791 */
792
793 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
794
795 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
796 quot = port->custom_divisor;
797 else
798 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
799
800 /* check to see if we need to change clock source */
801
802 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
803 s3c24xx_serial_setsource(port, clksrc);
804
805 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
806 clk_disable(ourport->baudclk);
807 clk_unuse(ourport->baudclk);
808 ourport->baudclk = NULL;
809 }
810
811 clk_use(clk);
812 clk_enable(clk);
813
814 ourport->clksrc = clksrc;
815 ourport->baudclk = clk;
816 }
817
818 switch (termios->c_cflag & CSIZE) {
819 case CS5:
820 dbg("config: 5bits/char\n");
821 ulcon = S3C2410_LCON_CS5;
822 break;
823 case CS6:
824 dbg("config: 6bits/char\n");
825 ulcon = S3C2410_LCON_CS6;
826 break;
827 case CS7:
828 dbg("config: 7bits/char\n");
829 ulcon = S3C2410_LCON_CS7;
830 break;
831 case CS8:
832 default:
833 dbg("config: 8bits/char\n");
834 ulcon = S3C2410_LCON_CS8;
835 break;
836 }
837
838 /* preserve original lcon IR settings */
839 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
840
841 if (termios->c_cflag & CSTOPB)
842 ulcon |= S3C2410_LCON_STOPB;
843
844 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
845
846 if (termios->c_cflag & PARENB) {
847 if (termios->c_cflag & PARODD)
848 ulcon |= S3C2410_LCON_PODD;
849 else
850 ulcon |= S3C2410_LCON_PEVEN;
851 } else {
852 ulcon |= S3C2410_LCON_PNONE;
853 }
854
855 spin_lock_irqsave(&port->lock, flags);
856
857 dbg("setting ulcon to %08x, brddiv to %d\n", ulcon, quot);
858
859 wr_regl(port, S3C2410_ULCON, ulcon);
860 wr_regl(port, S3C2410_UBRDIV, quot);
861 wr_regl(port, S3C2410_UMCON, umcon);
862
863 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
864 rd_regl(port, S3C2410_ULCON),
865 rd_regl(port, S3C2410_UCON),
866 rd_regl(port, S3C2410_UFCON));
867
868 /*
869 * Update the per-port timeout.
870 */
871 uart_update_timeout(port, termios->c_cflag, baud);
872
873 /*
874 * Which character status flags are we interested in?
875 */
876 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
877 if (termios->c_iflag & INPCK)
878 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
879
880 /*
881 * Which character status flags should we ignore?
882 */
883 port->ignore_status_mask = 0;
884 if (termios->c_iflag & IGNPAR)
885 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
886 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
887 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
888
889 /*
890 * Ignore all characters if CREAD is not set.
891 */
892 if ((termios->c_cflag & CREAD) == 0)
893 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
894
895 spin_unlock_irqrestore(&port->lock, flags);
896}
897
898static const char *s3c24xx_serial_type(struct uart_port *port)
899{
900 switch (port->type) {
901 case PORT_S3C2410:
902 return "S3C2410";
903 case PORT_S3C2440:
904 return "S3C2440";
905 default:
906 return NULL;
907 }
908}
909
910#define MAP_SIZE (0x100)
911
912static void s3c24xx_serial_release_port(struct uart_port *port)
913{
914 release_mem_region(port->mapbase, MAP_SIZE);
915}
916
917static int s3c24xx_serial_request_port(struct uart_port *port)
918{
919 char *name = s3c24xx_serial_portname(port);
920 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
921}
922
923static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
924{
925 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
926
927 if (flags & UART_CONFIG_TYPE &&
928 s3c24xx_serial_request_port(port) == 0)
929 port->type = info->type;
930}
931
932/*
933 * verify the new serial_struct (for TIOCSSERIAL).
934 */
935static int
936s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
937{
938 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
939
940 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
941 return -EINVAL;
942
943 return 0;
944}
945
946
947#ifdef CONFIG_SERIAL_S3C2410_CONSOLE
948
949static struct console s3c24xx_serial_console;
950
951#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
952#else
953#define S3C24XX_SERIAL_CONSOLE NULL
954#endif
955
956static struct uart_ops s3c24xx_serial_ops = {
957 .pm = s3c24xx_serial_pm,
958 .tx_empty = s3c24xx_serial_tx_empty,
959 .get_mctrl = s3c24xx_serial_get_mctrl,
960 .set_mctrl = s3c24xx_serial_set_mctrl,
961 .stop_tx = s3c24xx_serial_stop_tx,
962 .start_tx = s3c24xx_serial_start_tx,
963 .stop_rx = s3c24xx_serial_stop_rx,
964 .enable_ms = s3c24xx_serial_enable_ms,
965 .break_ctl = s3c24xx_serial_break_ctl,
966 .startup = s3c24xx_serial_startup,
967 .shutdown = s3c24xx_serial_shutdown,
968 .set_termios = s3c24xx_serial_set_termios,
969 .type = s3c24xx_serial_type,
970 .release_port = s3c24xx_serial_release_port,
971 .request_port = s3c24xx_serial_request_port,
972 .config_port = s3c24xx_serial_config_port,
973 .verify_port = s3c24xx_serial_verify_port,
974};
975
976
977static struct uart_driver s3c24xx_uart_drv = {
978 .owner = THIS_MODULE,
979 .dev_name = "s3c2410_serial",
980 .nr = 3,
981 .cons = S3C24XX_SERIAL_CONSOLE,
982 .driver_name = S3C24XX_SERIAL_NAME,
983 .devfs_name = S3C24XX_SERIAL_DEVFS,
984 .major = S3C24XX_SERIAL_MAJOR,
985 .minor = S3C24XX_SERIAL_MINOR,
986};
987
988static struct s3c24xx_uart_port s3c24xx_serial_ports[NR_PORTS] = {
989 [0] = {
990 .port = {
991 .lock = SPIN_LOCK_UNLOCKED,
992 .iotype = UPIO_MEM,
993 .irq = IRQ_S3CUART_RX0,
994 .uartclk = 0,
995 .fifosize = 16,
996 .ops = &s3c24xx_serial_ops,
997 .flags = UPF_BOOT_AUTOCONF,
998 .line = 0,
999 }
1000 },
1001 [1] = {
1002 .port = {
1003 .lock = SPIN_LOCK_UNLOCKED,
1004 .iotype = UPIO_MEM,
1005 .irq = IRQ_S3CUART_RX1,
1006 .uartclk = 0,
1007 .fifosize = 16,
1008 .ops = &s3c24xx_serial_ops,
1009 .flags = UPF_BOOT_AUTOCONF,
1010 .line = 1,
1011 }
1012 },
1013#if NR_PORTS > 2
1014
1015 [2] = {
1016 .port = {
1017 .lock = SPIN_LOCK_UNLOCKED,
1018 .iotype = UPIO_MEM,
1019 .irq = IRQ_S3CUART_RX2,
1020 .uartclk = 0,
1021 .fifosize = 16,
1022 .ops = &s3c24xx_serial_ops,
1023 .flags = UPF_BOOT_AUTOCONF,
1024 .line = 2,
1025 }
1026 }
1027#endif
1028};
1029
1030/* s3c24xx_serial_resetport
1031 *
1032 * wrapper to call the specific reset for this port (reset the fifos
1033 * and the settings)
1034*/
1035
1036static inline int s3c24xx_serial_resetport(struct uart_port * port,
1037 struct s3c2410_uartcfg *cfg)
1038{
1039 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1040
1041 return (info->reset_port)(port, cfg);
1042}
1043
1044/* s3c24xx_serial_init_port
1045 *
1046 * initialise a single serial port from the platform device given
1047 */
1048
1049static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1050 struct s3c24xx_uart_info *info,
1051 struct platform_device *platdev)
1052{
1053 struct uart_port *port = &ourport->port;
1054 struct s3c2410_uartcfg *cfg;
1055 struct resource *res;
1056
1057 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1058
1059 if (platdev == NULL)
1060 return -ENODEV;
1061
1062 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1063
1064 if (port->mapbase != 0)
1065 return 0;
1066
1067 if (cfg->hwport > 3)
1068 return -EINVAL;
1069
1070 /* setup info for port */
1071 port->dev = &platdev->dev;
1072 ourport->info = info;
1073
1074 /* copy the info in from provided structure */
1075 ourport->port.fifosize = info->fifosize;
1076
1077 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1078
1079 port->uartclk = 1;
1080
1081 if (cfg->uart_flags & UPF_CONS_FLOW) {
1082 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1083 port->flags |= UPF_CONS_FLOW;
1084 }
1085
1086 /* sort our the physical and virtual addresses for each UART */
1087
1088 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1089 if (res == NULL) {
1090 printk(KERN_ERR "failed to find memory resource for uart\n");
1091 return -EINVAL;
1092 }
1093
1094 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1095
1096 port->mapbase = res->start;
1097 port->membase = S3C24XX_VA_UART + (res->start - S3C2410_PA_UART);
1098 port->irq = platform_get_irq(platdev, 0);
1099
1100 ourport->clk = clk_get(&platdev->dev, "uart");
1101
1102 if (ourport->clk != NULL && !IS_ERR(ourport->clk))
1103 clk_use(ourport->clk);
1104
1105 dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
1106 port->mapbase, port->membase, port->irq, port->uartclk);
1107
1108 /* reset the fifos (and setup the uart) */
1109 s3c24xx_serial_resetport(port, cfg);
1110 return 0;
1111}
1112
1113/* Device driver serial port probe */
1114
1115static int probe_index = 0;
1116
1117int s3c24xx_serial_probe(struct device *_dev,
1118 struct s3c24xx_uart_info *info)
1119{
1120 struct s3c24xx_uart_port *ourport;
1121 struct platform_device *dev = to_platform_device(_dev);
1122 int ret;
1123
1124 dbg("s3c24xx_serial_probe(%p, %p) %d\n", _dev, info, probe_index);
1125
1126 ourport = &s3c24xx_serial_ports[probe_index];
1127 probe_index++;
1128
1129 dbg("%s: initialising port %p...\n", __FUNCTION__, ourport);
1130
1131 ret = s3c24xx_serial_init_port(ourport, info, dev);
1132 if (ret < 0)
1133 goto probe_err;
1134
1135 dbg("%s: adding port\n", __FUNCTION__);
1136 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1137 dev_set_drvdata(_dev, &ourport->port);
1138
1139 return 0;
1140
1141 probe_err:
1142 return ret;
1143}
1144
1145int s3c24xx_serial_remove(struct device *_dev)
1146{
1147 struct uart_port *port = s3c24xx_dev_to_port(_dev);
1148
1149 if (port)
1150 uart_remove_one_port(&s3c24xx_uart_drv, port);
1151
1152 return 0;
1153}
1154
1155/* UART power management code */
1156
1157#ifdef CONFIG_PM
1158
1159int s3c24xx_serial_suspend(struct device *dev, u32 state, u32 level)
1160{
1161 struct uart_port *port = s3c24xx_dev_to_port(dev);
1162
1163 if (port && level == SUSPEND_DISABLE)
1164 uart_suspend_port(&s3c24xx_uart_drv, port);
1165
1166 return 0;
1167}
1168
1169int s3c24xx_serial_resume(struct device *dev, u32 level)
1170{
1171 struct uart_port *port = s3c24xx_dev_to_port(dev);
1172 struct s3c24xx_uart_port *ourport = to_ourport(port);
1173
1174 if (port && level == RESUME_ENABLE) {
1175 clk_enable(ourport->clk);
1176 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1177 clk_disable(ourport->clk);
1178
1179 uart_resume_port(&s3c24xx_uart_drv, port);
1180 }
1181
1182 return 0;
1183}
1184
1185#else
1186#define s3c24xx_serial_suspend NULL
1187#define s3c24xx_serial_resume NULL
1188#endif
1189
1190int s3c24xx_serial_init(struct device_driver *drv,
1191 struct s3c24xx_uart_info *info)
1192{
1193 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1194 return driver_register(drv);
1195}
1196
1197
1198/* now comes the code to initialise either the s3c2410 or s3c2440 serial
1199 * port information
1200*/
1201
1202/* cpu specific variations on the serial port support */
1203
1204#ifdef CONFIG_CPU_S3C2400
1205
1206static int s3c2400_serial_getsource(struct uart_port *port,
1207 struct s3c24xx_uart_clksrc *clk)
1208{
1209 clk->divisor = 1;
1210 clk->name = "pclk";
1211
1212 return 0;
1213}
1214
1215static int s3c2400_serial_setsource(struct uart_port *port,
1216 struct s3c24xx_uart_clksrc *clk)
1217{
1218 return 0;
1219}
1220
1221static int s3c2400_serial_resetport(struct uart_port *port,
1222 struct s3c2410_uartcfg *cfg)
1223{
1224 dbg("s3c2400_serial_resetport: port=%p (%08lx), cfg=%p\n",
1225 port, port->mapbase, cfg);
1226
1227 wr_regl(port, S3C2410_UCON, cfg->ucon);
1228 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
1229
1230 /* reset both fifos */
1231
1232 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1233 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1234
1235 return 0;
1236}
1237
1238static struct s3c24xx_uart_info s3c2400_uart_inf = {
1239 .name = "Samsung S3C2400 UART",
1240 .type = PORT_S3C2400,
1241 .fifosize = 16,
1242 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
1243 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
1244 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
1245 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
1246 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
1247 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
1248 .get_clksrc = s3c2400_serial_getsource,
1249 .set_clksrc = s3c2400_serial_setsource,
1250 .reset_port = s3c2400_serial_resetport,
1251};
1252
1253static int s3c2400_serial_probe(struct device *dev)
1254{
1255 return s3c24xx_serial_probe(dev, &s3c2400_uart_inf);
1256}
1257
1258static struct device_driver s3c2400_serial_drv = {
1259 .name = "s3c2400-uart",
1260 .bus = &platform_bus_type,
1261 .probe = s3c2400_serial_probe,
1262 .remove = s3c24xx_serial_remove,
1263 .suspend = s3c24xx_serial_suspend,
1264 .resume = s3c24xx_serial_resume,
1265};
1266
1267static inline int s3c2400_serial_init(void)
1268{
1269 return s3c24xx_serial_init(&s3c2400_serial_drv, &s3c2400_uart_inf);
1270}
1271
1272static inline void s3c2400_serial_exit(void)
1273{
1274 driver_unregister(&s3c2400_serial_drv);
1275}
1276
1277#define s3c2400_uart_inf_at &s3c2400_uart_inf
1278#else
1279
1280static inline int s3c2400_serial_init(void)
1281{
1282 return 0;
1283}
1284
1285static inline void s3c2400_serial_exit(void)
1286{
1287}
1288
1289#define s3c2400_uart_inf_at NULL
1290
1291#endif /* CONFIG_CPU_S3C2400 */
1292
1293/* S3C2410 support */
1294
1295#ifdef CONFIG_CPU_S3C2410
1296
1297static int s3c2410_serial_setsource(struct uart_port *port,
1298 struct s3c24xx_uart_clksrc *clk)
1299{
1300 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1301
1302 if (strcmp(clk->name, "uclk") == 0)
1303 ucon |= S3C2410_UCON_UCLK;
1304 else
1305 ucon &= ~S3C2410_UCON_UCLK;
1306
1307 wr_regl(port, S3C2410_UCON, ucon);
1308 return 0;
1309}
1310
1311static int s3c2410_serial_getsource(struct uart_port *port,
1312 struct s3c24xx_uart_clksrc *clk)
1313{
1314 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1315
1316 clk->divisor = 1;
1317 clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
1318
1319 return 0;
1320}
1321
1322static int s3c2410_serial_resetport(struct uart_port *port,
1323 struct s3c2410_uartcfg *cfg)
1324{
1325 dbg("s3c2410_serial_resetport: port=%p (%08lx), cfg=%p\n",
1326 port, port->mapbase, cfg);
1327
1328 wr_regl(port, S3C2410_UCON, cfg->ucon);
1329 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
1330
1331 /* reset both fifos */
1332
1333 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1334 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1335
1336 return 0;
1337}
1338
1339static struct s3c24xx_uart_info s3c2410_uart_inf = {
1340 .name = "Samsung S3C2410 UART",
1341 .type = PORT_S3C2410,
1342 .fifosize = 16,
1343 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
1344 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
1345 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
1346 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
1347 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
1348 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
1349 .get_clksrc = s3c2410_serial_getsource,
1350 .set_clksrc = s3c2410_serial_setsource,
1351 .reset_port = s3c2410_serial_resetport,
1352};
1353
1354/* device management */
1355
1356static int s3c2410_serial_probe(struct device *dev)
1357{
1358 return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
1359}
1360
1361static struct device_driver s3c2410_serial_drv = {
1362 .name = "s3c2410-uart",
1363 .bus = &platform_bus_type,
1364 .probe = s3c2410_serial_probe,
1365 .remove = s3c24xx_serial_remove,
1366 .suspend = s3c24xx_serial_suspend,
1367 .resume = s3c24xx_serial_resume,
1368};
1369
1370static inline int s3c2410_serial_init(void)
1371{
1372 return s3c24xx_serial_init(&s3c2410_serial_drv, &s3c2410_uart_inf);
1373}
1374
1375static inline void s3c2410_serial_exit(void)
1376{
1377 driver_unregister(&s3c2410_serial_drv);
1378}
1379
1380#define s3c2410_uart_inf_at &s3c2410_uart_inf
1381#else
1382
1383static inline int s3c2410_serial_init(void)
1384{
1385 return 0;
1386}
1387
1388static inline void s3c2410_serial_exit(void)
1389{
1390}
1391
1392#define s3c2410_uart_inf_at NULL
1393
1394#endif /* CONFIG_CPU_S3C2410 */
1395
1396#ifdef CONFIG_CPU_S3C2440
1397
1398static int s3c2440_serial_setsource(struct uart_port *port,
1399 struct s3c24xx_uart_clksrc *clk)
1400{
1401 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1402
1403 // todo - proper fclk<>nonfclk switch //
1404
1405 ucon &= ~S3C2440_UCON_CLKMASK;
1406
1407 if (strcmp(clk->name, "uclk") == 0)
1408 ucon |= S3C2440_UCON_UCLK;
1409 else if (strcmp(clk->name, "pclk") == 0)
1410 ucon |= S3C2440_UCON_PCLK;
1411 else if (strcmp(clk->name, "fclk") == 0)
1412 ucon |= S3C2440_UCON_FCLK;
1413 else {
1414 printk(KERN_ERR "unknown clock source %s\n", clk->name);
1415 return -EINVAL;
1416 }
1417
1418 wr_regl(port, S3C2410_UCON, ucon);
1419 return 0;
1420}
1421
1422
1423static int s3c2440_serial_getsource(struct uart_port *port,
1424 struct s3c24xx_uart_clksrc *clk)
1425{
1426 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1427 unsigned long ucon0, ucon1, ucon2;
1428
1429 switch (ucon & S3C2440_UCON_CLKMASK) {
1430 case S3C2440_UCON_UCLK:
1431 clk->divisor = 1;
1432 clk->name = "uclk";
1433 break;
1434
1435 case S3C2440_UCON_PCLK:
1436 case S3C2440_UCON_PCLK2:
1437 clk->divisor = 1;
1438 clk->name = "pclk";
1439 break;
1440
1441 case S3C2440_UCON_FCLK:
1442 /* the fun of calculating the uart divisors on
1443 * the s3c2440 */
1444
1445 ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
1446 ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
1447 ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
1448
1449 printk("ucons: %08lx, %08lx, %08lx\n", ucon0, ucon1, ucon2);
1450
1451 ucon0 &= S3C2440_UCON0_DIVMASK;
1452 ucon1 &= S3C2440_UCON1_DIVMASK;
1453 ucon2 &= S3C2440_UCON2_DIVMASK;
1454
1455 if (ucon0 != 0) {
1456 clk->divisor = ucon0 >> S3C2440_UCON_DIVSHIFT;
1457 clk->divisor += 6;
1458 } else if (ucon1 != 0) {
1459 clk->divisor = ucon1 >> S3C2440_UCON_DIVSHIFT;
1460 clk->divisor += 21;
1461 } else if (ucon2 != 0) {
1462 clk->divisor = ucon2 >> S3C2440_UCON_DIVSHIFT;
1463 clk->divisor += 36;
1464 } else {
1465 /* manual calims 44, seems to be 9 */
1466 clk->divisor = 9;
1467 }
1468
1469 clk->name = "fclk";
1470 break;
1471 }
1472
1473 return 0;
1474}
1475
1476static int s3c2440_serial_resetport(struct uart_port *port,
1477 struct s3c2410_uartcfg *cfg)
1478{
1479 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1480
1481 dbg("s3c2440_serial_resetport: port=%p (%08lx), cfg=%p\n",
1482 port, port->mapbase, cfg);
1483
1484 /* ensure we don't change the clock settings... */
1485
1486 ucon &= (S3C2440_UCON0_DIVMASK | (3<<10));
1487
1488 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
1489 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
1490
1491 /* reset both fifos */
1492
1493 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1494 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1495
1496 return 0;
1497}
1498
1499static struct s3c24xx_uart_info s3c2440_uart_inf = {
1500 .name = "Samsung S3C2440 UART",
1501 .type = PORT_S3C2440,
1502 .fifosize = 64,
1503 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1504 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1505 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1506 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1507 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1508 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1509 .get_clksrc = s3c2440_serial_getsource,
1510 .set_clksrc = s3c2440_serial_setsource,
1511 .reset_port = s3c2440_serial_resetport,
1512};
1513
1514/* device management */
1515
1516static int s3c2440_serial_probe(struct device *dev)
1517{
1518 dbg("s3c2440_serial_probe: dev=%p\n", dev);
1519 return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
1520}
1521
1522static struct device_driver s3c2440_serial_drv = {
1523 .name = "s3c2440-uart",
1524 .bus = &platform_bus_type,
1525 .probe = s3c2440_serial_probe,
1526 .remove = s3c24xx_serial_remove,
1527 .suspend = s3c24xx_serial_suspend,
1528 .resume = s3c24xx_serial_resume,
1529};
1530
1531
1532static inline int s3c2440_serial_init(void)
1533{
1534 return s3c24xx_serial_init(&s3c2440_serial_drv, &s3c2440_uart_inf);
1535}
1536
1537static inline void s3c2440_serial_exit(void)
1538{
1539 driver_unregister(&s3c2440_serial_drv);
1540}
1541
1542#define s3c2440_uart_inf_at &s3c2440_uart_inf
1543#else
1544
1545static inline int s3c2440_serial_init(void)
1546{
1547 return 0;
1548}
1549
1550static inline void s3c2440_serial_exit(void)
1551{
1552}
1553
1554#define s3c2440_uart_inf_at NULL
1555#endif /* CONFIG_CPU_S3C2440 */
1556
1557/* module initialisation code */
1558
1559static int __init s3c24xx_serial_modinit(void)
1560{
1561 int ret;
1562
1563 ret = uart_register_driver(&s3c24xx_uart_drv);
1564 if (ret < 0) {
1565 printk(KERN_ERR "failed to register UART driver\n");
1566 return -1;
1567 }
1568
1569 s3c2400_serial_init();
1570 s3c2410_serial_init();
1571 s3c2440_serial_init();
1572
1573 return 0;
1574}
1575
1576static void __exit s3c24xx_serial_modexit(void)
1577{
1578 s3c2400_serial_exit();
1579 s3c2410_serial_exit();
1580 s3c2440_serial_exit();
1581
1582 uart_unregister_driver(&s3c24xx_uart_drv);
1583}
1584
1585
1586module_init(s3c24xx_serial_modinit);
1587module_exit(s3c24xx_serial_modexit);
1588
1589/* Console code */
1590
1591#ifdef CONFIG_SERIAL_S3C2410_CONSOLE
1592
1593static struct uart_port *cons_uart;
1594
1595static int
1596s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1597{
1598 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1599 unsigned long ufstat, utrstat;
1600
1601 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1602 /* fifo mode - check ammount of data in fifo registers... */
1603
1604 ufstat = rd_regl(port, S3C2410_UFSTAT);
1605 return (ufstat & info->tx_fifofull) ? 0 : 1;
1606 }
1607
1608 /* in non-fifo mode, we go and use the tx buffer empty */
1609
1610 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1611 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1612}
1613
1614static void
1615s3c24xx_serial_console_write(struct console *co, const char *s,
1616 unsigned int count)
1617{
1618 int i;
1619 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1620
1621 for (i = 0; i < count; i++) {
1622 while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
1623 barrier();
1624
1625 wr_regb(cons_uart, S3C2410_UTXH, s[i]);
1626
1627 if (s[i] == '\n') {
1628 while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
1629 barrier();
1630
1631 wr_regb(cons_uart, S3C2410_UTXH, '\r');
1632 }
1633 }
1634}
1635
1636static void __init
1637s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1638 int *parity, int *bits)
1639{
1640 struct s3c24xx_uart_clksrc clksrc;
1641 struct clk *clk;
1642 unsigned int ulcon;
1643 unsigned int ucon;
1644 unsigned int ubrdiv;
1645 unsigned long rate;
1646
1647 ulcon = rd_regl(port, S3C2410_ULCON);
1648 ucon = rd_regl(port, S3C2410_UCON);
1649 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1650
1651 dbg("s3c24xx_serial_get_options: port=%p\n"
1652 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1653 port, ulcon, ucon, ubrdiv);
1654
1655 if ((ucon & 0xf) != 0) {
1656 /* consider the serial port configured if the tx/rx mode set */
1657
1658 switch (ulcon & S3C2410_LCON_CSMASK) {
1659 case S3C2410_LCON_CS5:
1660 *bits = 5;
1661 break;
1662 case S3C2410_LCON_CS6:
1663 *bits = 6;
1664 break;
1665 case S3C2410_LCON_CS7:
1666 *bits = 7;
1667 break;
1668 default:
1669 case S3C2410_LCON_CS8:
1670 *bits = 8;
1671 break;
1672 }
1673
1674 switch (ulcon & S3C2410_LCON_PMASK) {
1675 case S3C2410_LCON_PEVEN:
1676 *parity = 'e';
1677 break;
1678
1679 case S3C2410_LCON_PODD:
1680 *parity = 'o';
1681 break;
1682
1683 case S3C2410_LCON_PNONE:
1684 default:
1685 *parity = 'n';
1686 }
1687
1688 /* now calculate the baud rate */
1689
1690 s3c24xx_serial_getsource(port, &clksrc);
1691
1692 clk = clk_get(port->dev, clksrc.name);
1693 if (!IS_ERR(clk) && clk != NULL)
1694 rate = clk_get_rate(clk) / clksrc.divisor;
1695 else
1696 rate = 1;
1697
1698
1699 *baud = rate / ( 16 * (ubrdiv + 1));
1700 dbg("calculated baud %d\n", *baud);
1701 }
1702
1703}
1704
1705/* s3c24xx_serial_init_ports
1706 *
1707 * initialise the serial ports from the machine provided initialisation
1708 * data.
1709*/
1710
1711static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
1712{
1713 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1714 struct platform_device **platdev_ptr;
1715 int i;
1716
1717 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1718
1719 platdev_ptr = s3c24xx_uart_devs;
1720
1721 for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
1722 s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
1723 }
1724
1725 return 0;
1726}
1727
1728static int __init
1729s3c24xx_serial_console_setup(struct console *co, char *options)
1730{
1731 struct uart_port *port;
1732 int baud = 9600;
1733 int bits = 8;
1734 int parity = 'n';
1735 int flow = 'n';
1736
1737 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1738 co, co->index, options);
1739
1740 /* is this a valid port */
1741
1742 if (co->index == -1 || co->index >= NR_PORTS)
1743 co->index = 0;
1744
1745 port = &s3c24xx_serial_ports[co->index].port;
1746
1747 /* is the port configured? */
1748
1749 if (port->mapbase == 0x0) {
1750 co->index = 0;
1751 port = &s3c24xx_serial_ports[co->index].port;
1752 }
1753
1754 cons_uart = port;
1755
1756 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1757
1758 /*
1759 * Check whether an invalid uart number has been specified, and
1760 * if so, search for the first available port that does have
1761 * console support.
1762 */
1763 if (options)
1764 uart_parse_options(options, &baud, &parity, &bits, &flow);
1765 else
1766 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1767
1768 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1769
1770 return uart_set_options(port, co, baud, parity, bits, flow);
1771}
1772
1773/* s3c24xx_serial_initconsole
1774 *
1775 * initialise the console from one of the uart drivers
1776*/
1777
1778static struct console s3c24xx_serial_console =
1779{
1780 .name = S3C24XX_SERIAL_NAME,
1781 .device = uart_console_device,
1782 .flags = CON_PRINTBUFFER,
1783 .index = -1,
1784 .write = s3c24xx_serial_console_write,
1785 .setup = s3c24xx_serial_console_setup
1786};
1787
1788static int s3c24xx_serial_initconsole(void)
1789{
1790 struct s3c24xx_uart_info *info;
1791 struct platform_device *dev = s3c24xx_uart_devs[0];
1792
1793 dbg("s3c24xx_serial_initconsole\n");
1794
1795 /* select driver based on the cpu */
1796
1797 if (dev == NULL) {
1798 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1799 return 0;
1800 }
1801
1802 if (strcmp(dev->name, "s3c2400-uart") == 0) {
1803 info = s3c2400_uart_inf_at;
1804 } else if (strcmp(dev->name, "s3c2410-uart") == 0) {
1805 info = s3c2410_uart_inf_at;
1806 } else if (strcmp(dev->name, "s3c2440-uart") == 0) {
1807 info = s3c2440_uart_inf_at;
1808 } else {
1809 printk(KERN_ERR "s3c24xx: no driver for %s\n", dev->name);
1810 return 0;
1811 }
1812
1813 if (info == NULL) {
1814 printk(KERN_ERR "s3c24xx: no driver for console\n");
1815 return 0;
1816 }
1817
1818 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1819 s3c24xx_serial_init_ports(info);
1820
1821 register_console(&s3c24xx_serial_console);
1822 return 0;
1823}
1824
1825console_initcall(s3c24xx_serial_initconsole);
1826
1827#endif /* CONFIG_SERIAL_S3C2410_CONSOLE */
1828
1829MODULE_LICENSE("GPL");
1830MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1831MODULE_DESCRIPTION("Samsung S3C2410/S3C2440 Serial port driver");
diff --git a/drivers/serial/sa1100.c b/drivers/serial/sa1100.c
new file mode 100644
index 000000000000..85f0af452f95
--- /dev/null
+++ b/drivers/serial/sa1100.c
@@ -0,0 +1,952 @@
1/*
2 * linux/drivers/char/sa1100.c
3 *
4 * Driver for SA11x0 serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright (C) 2000 Deep Blue Solutions Ltd.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * $Id: sa1100.c,v 1.50 2002/07/29 14:41:04 rmk Exp $
25 *
26 */
27#include <linux/config.h>
28
29#if defined(CONFIG_SERIAL_SA1100_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
30#define SUPPORT_SYSRQ
31#endif
32
33#include <linux/module.h>
34#include <linux/ioport.h>
35#include <linux/init.h>
36#include <linux/console.h>
37#include <linux/sysrq.h>
38#include <linux/device.h>
39#include <linux/tty.h>
40#include <linux/tty_flip.h>
41#include <linux/serial_core.h>
42#include <linux/serial.h>
43
44#include <asm/io.h>
45#include <asm/irq.h>
46#include <asm/hardware.h>
47#include <asm/mach/serial_sa1100.h>
48
49/* We've been assigned a range on the "Low-density serial ports" major */
50#define SERIAL_SA1100_MAJOR 204
51#define MINOR_START 5
52
53#define NR_PORTS 3
54
55#define SA1100_ISR_PASS_LIMIT 256
56
57/*
58 * Convert from ignore_status_mask or read_status_mask to UTSR[01]
59 */
60#define SM_TO_UTSR0(x) ((x) & 0xff)
61#define SM_TO_UTSR1(x) ((x) >> 8)
62#define UTSR0_TO_SM(x) ((x))
63#define UTSR1_TO_SM(x) ((x) << 8)
64
65#define UART_GET_UTCR0(sport) __raw_readl((sport)->port.membase + UTCR0)
66#define UART_GET_UTCR1(sport) __raw_readl((sport)->port.membase + UTCR1)
67#define UART_GET_UTCR2(sport) __raw_readl((sport)->port.membase + UTCR2)
68#define UART_GET_UTCR3(sport) __raw_readl((sport)->port.membase + UTCR3)
69#define UART_GET_UTSR0(sport) __raw_readl((sport)->port.membase + UTSR0)
70#define UART_GET_UTSR1(sport) __raw_readl((sport)->port.membase + UTSR1)
71#define UART_GET_CHAR(sport) __raw_readl((sport)->port.membase + UTDR)
72
73#define UART_PUT_UTCR0(sport,v) __raw_writel((v),(sport)->port.membase + UTCR0)
74#define UART_PUT_UTCR1(sport,v) __raw_writel((v),(sport)->port.membase + UTCR1)
75#define UART_PUT_UTCR2(sport,v) __raw_writel((v),(sport)->port.membase + UTCR2)
76#define UART_PUT_UTCR3(sport,v) __raw_writel((v),(sport)->port.membase + UTCR3)
77#define UART_PUT_UTSR0(sport,v) __raw_writel((v),(sport)->port.membase + UTSR0)
78#define UART_PUT_UTSR1(sport,v) __raw_writel((v),(sport)->port.membase + UTSR1)
79#define UART_PUT_CHAR(sport,v) __raw_writel((v),(sport)->port.membase + UTDR)
80
81/*
82 * This is the size of our serial port register set.
83 */
84#define UART_PORT_SIZE 0x24
85
86/*
87 * This determines how often we check the modem status signals
88 * for any change. They generally aren't connected to an IRQ
89 * so we have to poll them. We also check immediately before
90 * filling the TX fifo incase CTS has been dropped.
91 */
92#define MCTRL_TIMEOUT (250*HZ/1000)
93
94struct sa1100_port {
95 struct uart_port port;
96 struct timer_list timer;
97 unsigned int old_status;
98};
99
100/*
101 * Handle any change of modem status signal since we were last called.
102 */
103static void sa1100_mctrl_check(struct sa1100_port *sport)
104{
105 unsigned int status, changed;
106
107 status = sport->port.ops->get_mctrl(&sport->port);
108 changed = status ^ sport->old_status;
109
110 if (changed == 0)
111 return;
112
113 sport->old_status = status;
114
115 if (changed & TIOCM_RI)
116 sport->port.icount.rng++;
117 if (changed & TIOCM_DSR)
118 sport->port.icount.dsr++;
119 if (changed & TIOCM_CAR)
120 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
121 if (changed & TIOCM_CTS)
122 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
123
124 wake_up_interruptible(&sport->port.info->delta_msr_wait);
125}
126
127/*
128 * This is our per-port timeout handler, for checking the
129 * modem status signals.
130 */
131static void sa1100_timeout(unsigned long data)
132{
133 struct sa1100_port *sport = (struct sa1100_port *)data;
134 unsigned long flags;
135
136 if (sport->port.info) {
137 spin_lock_irqsave(&sport->port.lock, flags);
138 sa1100_mctrl_check(sport);
139 spin_unlock_irqrestore(&sport->port.lock, flags);
140
141 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
142 }
143}
144
145/*
146 * interrupts disabled on entry
147 */
148static void sa1100_stop_tx(struct uart_port *port, unsigned int tty_stop)
149{
150 struct sa1100_port *sport = (struct sa1100_port *)port;
151 u32 utcr3;
152
153 utcr3 = UART_GET_UTCR3(sport);
154 UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_TIE);
155 sport->port.read_status_mask &= ~UTSR0_TO_SM(UTSR0_TFS);
156}
157
158/*
159 * interrupts may not be disabled on entry
160 */
161static void sa1100_start_tx(struct uart_port *port, unsigned int tty_start)
162{
163 struct sa1100_port *sport = (struct sa1100_port *)port;
164 unsigned long flags;
165 u32 utcr3;
166
167 spin_lock_irqsave(&sport->port.lock, flags);
168 utcr3 = UART_GET_UTCR3(sport);
169 sport->port.read_status_mask |= UTSR0_TO_SM(UTSR0_TFS);
170 UART_PUT_UTCR3(sport, utcr3 | UTCR3_TIE);
171 spin_unlock_irqrestore(&sport->port.lock, flags);
172}
173
174/*
175 * Interrupts enabled
176 */
177static void sa1100_stop_rx(struct uart_port *port)
178{
179 struct sa1100_port *sport = (struct sa1100_port *)port;
180 u32 utcr3;
181
182 utcr3 = UART_GET_UTCR3(sport);
183 UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_RIE);
184}
185
186/*
187 * Set the modem control timer to fire immediately.
188 */
189static void sa1100_enable_ms(struct uart_port *port)
190{
191 struct sa1100_port *sport = (struct sa1100_port *)port;
192
193 mod_timer(&sport->timer, jiffies);
194}
195
196static void
197sa1100_rx_chars(struct sa1100_port *sport, struct pt_regs *regs)
198{
199 struct tty_struct *tty = sport->port.info->tty;
200 unsigned int status, ch, flg, ignored = 0;
201
202 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
203 UTSR0_TO_SM(UART_GET_UTSR0(sport));
204 while (status & UTSR1_TO_SM(UTSR1_RNE)) {
205 ch = UART_GET_CHAR(sport);
206
207 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
208 goto ignore_char;
209 sport->port.icount.rx++;
210
211 flg = TTY_NORMAL;
212
213 /*
214 * note that the error handling code is
215 * out of the main execution path
216 */
217 if (status & UTSR1_TO_SM(UTSR1_PRE | UTSR1_FRE | UTSR1_ROR))
218 goto handle_error;
219
220 if (uart_handle_sysrq_char(&sport->port, ch, regs))
221 goto ignore_char;
222
223 error_return:
224 tty_insert_flip_char(tty, ch, flg);
225 ignore_char:
226 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
227 UTSR0_TO_SM(UART_GET_UTSR0(sport));
228 }
229 out:
230 tty_flip_buffer_push(tty);
231 return;
232
233 handle_error:
234 if (status & UTSR1_TO_SM(UTSR1_PRE))
235 sport->port.icount.parity++;
236 else if (status & UTSR1_TO_SM(UTSR1_FRE))
237 sport->port.icount.frame++;
238 if (status & UTSR1_TO_SM(UTSR1_ROR))
239 sport->port.icount.overrun++;
240
241 if (status & sport->port.ignore_status_mask) {
242 if (++ignored > 100)
243 goto out;
244 goto ignore_char;
245 }
246
247 status &= sport->port.read_status_mask;
248
249 if (status & UTSR1_TO_SM(UTSR1_PRE))
250 flg = TTY_PARITY;
251 else if (status & UTSR1_TO_SM(UTSR1_FRE))
252 flg = TTY_FRAME;
253
254 if (status & UTSR1_TO_SM(UTSR1_ROR)) {
255 /*
256 * overrun does *not* affect the character
257 * we read from the FIFO
258 */
259 tty_insert_flip_char(tty, ch, flg);
260 ch = 0;
261 flg = TTY_OVERRUN;
262 }
263#ifdef SUPPORT_SYSRQ
264 sport->port.sysrq = 0;
265#endif
266 goto error_return;
267}
268
269static void sa1100_tx_chars(struct sa1100_port *sport)
270{
271 struct circ_buf *xmit = &sport->port.info->xmit;
272
273 if (sport->port.x_char) {
274 UART_PUT_CHAR(sport, sport->port.x_char);
275 sport->port.icount.tx++;
276 sport->port.x_char = 0;
277 return;
278 }
279
280 /*
281 * Check the modem control lines before
282 * transmitting anything.
283 */
284 sa1100_mctrl_check(sport);
285
286 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
287 sa1100_stop_tx(&sport->port, 0);
288 return;
289 }
290
291 /*
292 * Tried using FIFO (not checking TNF) for fifo fill:
293 * still had the '4 bytes repeated' problem.
294 */
295 while (UART_GET_UTSR1(sport) & UTSR1_TNF) {
296 UART_PUT_CHAR(sport, xmit->buf[xmit->tail]);
297 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
298 sport->port.icount.tx++;
299 if (uart_circ_empty(xmit))
300 break;
301 }
302
303 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
304 uart_write_wakeup(&sport->port);
305
306 if (uart_circ_empty(xmit))
307 sa1100_stop_tx(&sport->port, 0);
308}
309
310static irqreturn_t sa1100_int(int irq, void *dev_id, struct pt_regs *regs)
311{
312 struct sa1100_port *sport = dev_id;
313 unsigned int status, pass_counter = 0;
314
315 spin_lock(&sport->port.lock);
316 status = UART_GET_UTSR0(sport);
317 status &= SM_TO_UTSR0(sport->port.read_status_mask) | ~UTSR0_TFS;
318 do {
319 if (status & (UTSR0_RFS | UTSR0_RID)) {
320 /* Clear the receiver idle bit, if set */
321 if (status & UTSR0_RID)
322 UART_PUT_UTSR0(sport, UTSR0_RID);
323 sa1100_rx_chars(sport, regs);
324 }
325
326 /* Clear the relevant break bits */
327 if (status & (UTSR0_RBB | UTSR0_REB))
328 UART_PUT_UTSR0(sport, status & (UTSR0_RBB | UTSR0_REB));
329
330 if (status & UTSR0_RBB)
331 sport->port.icount.brk++;
332
333 if (status & UTSR0_REB)
334 uart_handle_break(&sport->port);
335
336 if (status & UTSR0_TFS)
337 sa1100_tx_chars(sport);
338 if (pass_counter++ > SA1100_ISR_PASS_LIMIT)
339 break;
340 status = UART_GET_UTSR0(sport);
341 status &= SM_TO_UTSR0(sport->port.read_status_mask) |
342 ~UTSR0_TFS;
343 } while (status & (UTSR0_TFS | UTSR0_RFS | UTSR0_RID));
344 spin_unlock(&sport->port.lock);
345
346 return IRQ_HANDLED;
347}
348
349/*
350 * Return TIOCSER_TEMT when transmitter is not busy.
351 */
352static unsigned int sa1100_tx_empty(struct uart_port *port)
353{
354 struct sa1100_port *sport = (struct sa1100_port *)port;
355
356 return UART_GET_UTSR1(sport) & UTSR1_TBY ? 0 : TIOCSER_TEMT;
357}
358
359static unsigned int sa1100_get_mctrl(struct uart_port *port)
360{
361 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
362}
363
364static void sa1100_set_mctrl(struct uart_port *port, unsigned int mctrl)
365{
366}
367
368/*
369 * Interrupts always disabled.
370 */
371static void sa1100_break_ctl(struct uart_port *port, int break_state)
372{
373 struct sa1100_port *sport = (struct sa1100_port *)port;
374 unsigned long flags;
375 unsigned int utcr3;
376
377 spin_lock_irqsave(&sport->port.lock, flags);
378 utcr3 = UART_GET_UTCR3(sport);
379 if (break_state == -1)
380 utcr3 |= UTCR3_BRK;
381 else
382 utcr3 &= ~UTCR3_BRK;
383 UART_PUT_UTCR3(sport, utcr3);
384 spin_unlock_irqrestore(&sport->port.lock, flags);
385}
386
387static int sa1100_startup(struct uart_port *port)
388{
389 struct sa1100_port *sport = (struct sa1100_port *)port;
390 int retval;
391
392 /*
393 * Allocate the IRQ
394 */
395 retval = request_irq(sport->port.irq, sa1100_int, 0,
396 "sa11x0-uart", sport);
397 if (retval)
398 return retval;
399
400 /*
401 * Finally, clear and enable interrupts
402 */
403 UART_PUT_UTSR0(sport, -1);
404 UART_PUT_UTCR3(sport, UTCR3_RXE | UTCR3_TXE | UTCR3_RIE);
405
406 /*
407 * Enable modem status interrupts
408 */
409 spin_lock_irq(&sport->port.lock);
410 sa1100_enable_ms(&sport->port);
411 spin_unlock_irq(&sport->port.lock);
412
413 return 0;
414}
415
416static void sa1100_shutdown(struct uart_port *port)
417{
418 struct sa1100_port *sport = (struct sa1100_port *)port;
419
420 /*
421 * Stop our timer.
422 */
423 del_timer_sync(&sport->timer);
424
425 /*
426 * Free the interrupt
427 */
428 free_irq(sport->port.irq, sport);
429
430 /*
431 * Disable all interrupts, port and break condition.
432 */
433 UART_PUT_UTCR3(sport, 0);
434}
435
436static void
437sa1100_set_termios(struct uart_port *port, struct termios *termios,
438 struct termios *old)
439{
440 struct sa1100_port *sport = (struct sa1100_port *)port;
441 unsigned long flags;
442 unsigned int utcr0, old_utcr3, baud, quot;
443 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
444
445 /*
446 * We only support CS7 and CS8.
447 */
448 while ((termios->c_cflag & CSIZE) != CS7 &&
449 (termios->c_cflag & CSIZE) != CS8) {
450 termios->c_cflag &= ~CSIZE;
451 termios->c_cflag |= old_csize;
452 old_csize = CS8;
453 }
454
455 if ((termios->c_cflag & CSIZE) == CS8)
456 utcr0 = UTCR0_DSS;
457 else
458 utcr0 = 0;
459
460 if (termios->c_cflag & CSTOPB)
461 utcr0 |= UTCR0_SBS;
462 if (termios->c_cflag & PARENB) {
463 utcr0 |= UTCR0_PE;
464 if (!(termios->c_cflag & PARODD))
465 utcr0 |= UTCR0_OES;
466 }
467
468 /*
469 * Ask the core to calculate the divisor for us.
470 */
471 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
472 quot = uart_get_divisor(port, baud);
473
474 spin_lock_irqsave(&sport->port.lock, flags);
475
476 sport->port.read_status_mask &= UTSR0_TO_SM(UTSR0_TFS);
477 sport->port.read_status_mask |= UTSR1_TO_SM(UTSR1_ROR);
478 if (termios->c_iflag & INPCK)
479 sport->port.read_status_mask |=
480 UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
481 if (termios->c_iflag & (BRKINT | PARMRK))
482 sport->port.read_status_mask |=
483 UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
484
485 /*
486 * Characters to ignore
487 */
488 sport->port.ignore_status_mask = 0;
489 if (termios->c_iflag & IGNPAR)
490 sport->port.ignore_status_mask |=
491 UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
492 if (termios->c_iflag & IGNBRK) {
493 sport->port.ignore_status_mask |=
494 UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
495 /*
496 * If we're ignoring parity and break indicators,
497 * ignore overruns too (for real raw support).
498 */
499 if (termios->c_iflag & IGNPAR)
500 sport->port.ignore_status_mask |=
501 UTSR1_TO_SM(UTSR1_ROR);
502 }
503
504 del_timer_sync(&sport->timer);
505
506 /*
507 * Update the per-port timeout.
508 */
509 uart_update_timeout(port, termios->c_cflag, baud);
510
511 /*
512 * disable interrupts and drain transmitter
513 */
514 old_utcr3 = UART_GET_UTCR3(sport);
515 UART_PUT_UTCR3(sport, old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE));
516
517 while (UART_GET_UTSR1(sport) & UTSR1_TBY)
518 barrier();
519
520 /* then, disable everything */
521 UART_PUT_UTCR3(sport, 0);
522
523 /* set the parity, stop bits and data size */
524 UART_PUT_UTCR0(sport, utcr0);
525
526 /* set the baud rate */
527 quot -= 1;
528 UART_PUT_UTCR1(sport, ((quot & 0xf00) >> 8));
529 UART_PUT_UTCR2(sport, (quot & 0xff));
530
531 UART_PUT_UTSR0(sport, -1);
532
533 UART_PUT_UTCR3(sport, old_utcr3);
534
535 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
536 sa1100_enable_ms(&sport->port);
537
538 spin_unlock_irqrestore(&sport->port.lock, flags);
539}
540
541static const char *sa1100_type(struct uart_port *port)
542{
543 struct sa1100_port *sport = (struct sa1100_port *)port;
544
545 return sport->port.type == PORT_SA1100 ? "SA1100" : NULL;
546}
547
548/*
549 * Release the memory region(s) being used by 'port'.
550 */
551static void sa1100_release_port(struct uart_port *port)
552{
553 struct sa1100_port *sport = (struct sa1100_port *)port;
554
555 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
556}
557
558/*
559 * Request the memory region(s) being used by 'port'.
560 */
561static int sa1100_request_port(struct uart_port *port)
562{
563 struct sa1100_port *sport = (struct sa1100_port *)port;
564
565 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
566 "sa11x0-uart") != NULL ? 0 : -EBUSY;
567}
568
569/*
570 * Configure/autoconfigure the port.
571 */
572static void sa1100_config_port(struct uart_port *port, int flags)
573{
574 struct sa1100_port *sport = (struct sa1100_port *)port;
575
576 if (flags & UART_CONFIG_TYPE &&
577 sa1100_request_port(&sport->port) == 0)
578 sport->port.type = PORT_SA1100;
579}
580
581/*
582 * Verify the new serial_struct (for TIOCSSERIAL).
583 * The only change we allow are to the flags and type, and
584 * even then only between PORT_SA1100 and PORT_UNKNOWN
585 */
586static int
587sa1100_verify_port(struct uart_port *port, struct serial_struct *ser)
588{
589 struct sa1100_port *sport = (struct sa1100_port *)port;
590 int ret = 0;
591
592 if (ser->type != PORT_UNKNOWN && ser->type != PORT_SA1100)
593 ret = -EINVAL;
594 if (sport->port.irq != ser->irq)
595 ret = -EINVAL;
596 if (ser->io_type != SERIAL_IO_MEM)
597 ret = -EINVAL;
598 if (sport->port.uartclk / 16 != ser->baud_base)
599 ret = -EINVAL;
600 if ((void *)sport->port.mapbase != ser->iomem_base)
601 ret = -EINVAL;
602 if (sport->port.iobase != ser->port)
603 ret = -EINVAL;
604 if (ser->hub6 != 0)
605 ret = -EINVAL;
606 return ret;
607}
608
609static struct uart_ops sa1100_pops = {
610 .tx_empty = sa1100_tx_empty,
611 .set_mctrl = sa1100_set_mctrl,
612 .get_mctrl = sa1100_get_mctrl,
613 .stop_tx = sa1100_stop_tx,
614 .start_tx = sa1100_start_tx,
615 .stop_rx = sa1100_stop_rx,
616 .enable_ms = sa1100_enable_ms,
617 .break_ctl = sa1100_break_ctl,
618 .startup = sa1100_startup,
619 .shutdown = sa1100_shutdown,
620 .set_termios = sa1100_set_termios,
621 .type = sa1100_type,
622 .release_port = sa1100_release_port,
623 .request_port = sa1100_request_port,
624 .config_port = sa1100_config_port,
625 .verify_port = sa1100_verify_port,
626};
627
628static struct sa1100_port sa1100_ports[NR_PORTS];
629
630/*
631 * Setup the SA1100 serial ports. Note that we don't include the IrDA
632 * port here since we have our own SIR/FIR driver (see drivers/net/irda)
633 *
634 * Note also that we support "console=ttySAx" where "x" is either 0 or 1.
635 * Which serial port this ends up being depends on the machine you're
636 * running this kernel on. I'm not convinced that this is a good idea,
637 * but that's the way it traditionally works.
638 *
639 * Note that NanoEngine UART3 becomes UART2, and UART2 is no longer
640 * used here.
641 */
642static void __init sa1100_init_ports(void)
643{
644 static int first = 1;
645 int i;
646
647 if (!first)
648 return;
649 first = 0;
650
651 for (i = 0; i < NR_PORTS; i++) {
652 sa1100_ports[i].port.uartclk = 3686400;
653 sa1100_ports[i].port.ops = &sa1100_pops;
654 sa1100_ports[i].port.fifosize = 8;
655 sa1100_ports[i].port.line = i;
656 sa1100_ports[i].port.iotype = SERIAL_IO_MEM;
657 init_timer(&sa1100_ports[i].timer);
658 sa1100_ports[i].timer.function = sa1100_timeout;
659 sa1100_ports[i].timer.data = (unsigned long)&sa1100_ports[i];
660 }
661
662 /*
663 * make transmit lines outputs, so that when the port
664 * is closed, the output is in the MARK state.
665 */
666 PPDR |= PPC_TXD1 | PPC_TXD3;
667 PPSR |= PPC_TXD1 | PPC_TXD3;
668}
669
670void __init sa1100_register_uart_fns(struct sa1100_port_fns *fns)
671{
672 if (fns->get_mctrl)
673 sa1100_pops.get_mctrl = fns->get_mctrl;
674 if (fns->set_mctrl)
675 sa1100_pops.set_mctrl = fns->set_mctrl;
676
677 sa1100_pops.pm = fns->pm;
678 sa1100_pops.set_wake = fns->set_wake;
679}
680
681void __init sa1100_register_uart(int idx, int port)
682{
683 if (idx >= NR_PORTS) {
684 printk(KERN_ERR "%s: bad index number %d\n", __FUNCTION__, idx);
685 return;
686 }
687
688 switch (port) {
689 case 1:
690 sa1100_ports[idx].port.membase = (void __iomem *)&Ser1UTCR0;
691 sa1100_ports[idx].port.mapbase = _Ser1UTCR0;
692 sa1100_ports[idx].port.irq = IRQ_Ser1UART;
693 sa1100_ports[idx].port.flags = ASYNC_BOOT_AUTOCONF;
694 break;
695
696 case 2:
697 sa1100_ports[idx].port.membase = (void __iomem *)&Ser2UTCR0;
698 sa1100_ports[idx].port.mapbase = _Ser2UTCR0;
699 sa1100_ports[idx].port.irq = IRQ_Ser2ICP;
700 sa1100_ports[idx].port.flags = ASYNC_BOOT_AUTOCONF;
701 break;
702
703 case 3:
704 sa1100_ports[idx].port.membase = (void __iomem *)&Ser3UTCR0;
705 sa1100_ports[idx].port.mapbase = _Ser3UTCR0;
706 sa1100_ports[idx].port.irq = IRQ_Ser3UART;
707 sa1100_ports[idx].port.flags = ASYNC_BOOT_AUTOCONF;
708 break;
709
710 default:
711 printk(KERN_ERR "%s: bad port number %d\n", __FUNCTION__, port);
712 }
713}
714
715
716#ifdef CONFIG_SERIAL_SA1100_CONSOLE
717
718/*
719 * Interrupts are disabled on entering
720 */
721static void
722sa1100_console_write(struct console *co, const char *s, unsigned int count)
723{
724 struct sa1100_port *sport = &sa1100_ports[co->index];
725 unsigned int old_utcr3, status, i;
726
727 /*
728 * First, save UTCR3 and then disable interrupts
729 */
730 old_utcr3 = UART_GET_UTCR3(sport);
731 UART_PUT_UTCR3(sport, (old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE)) |
732 UTCR3_TXE);
733
734 /*
735 * Now, do each character
736 */
737 for (i = 0; i < count; i++) {
738 do {
739 status = UART_GET_UTSR1(sport);
740 } while (!(status & UTSR1_TNF));
741 UART_PUT_CHAR(sport, s[i]);
742 if (s[i] == '\n') {
743 do {
744 status = UART_GET_UTSR1(sport);
745 } while (!(status & UTSR1_TNF));
746 UART_PUT_CHAR(sport, '\r');
747 }
748 }
749
750 /*
751 * Finally, wait for transmitter to become empty
752 * and restore UTCR3
753 */
754 do {
755 status = UART_GET_UTSR1(sport);
756 } while (status & UTSR1_TBY);
757 UART_PUT_UTCR3(sport, old_utcr3);
758}
759
760/*
761 * If the port was already initialised (eg, by a boot loader),
762 * try to determine the current setup.
763 */
764static void __init
765sa1100_console_get_options(struct sa1100_port *sport, int *baud,
766 int *parity, int *bits)
767{
768 unsigned int utcr3;
769
770 utcr3 = UART_GET_UTCR3(sport) & (UTCR3_RXE | UTCR3_TXE);
771 if (utcr3 == (UTCR3_RXE | UTCR3_TXE)) {
772 /* ok, the port was enabled */
773 unsigned int utcr0, quot;
774
775 utcr0 = UART_GET_UTCR0(sport);
776
777 *parity = 'n';
778 if (utcr0 & UTCR0_PE) {
779 if (utcr0 & UTCR0_OES)
780 *parity = 'e';
781 else
782 *parity = 'o';
783 }
784
785 if (utcr0 & UTCR0_DSS)
786 *bits = 8;
787 else
788 *bits = 7;
789
790 quot = UART_GET_UTCR2(sport) | UART_GET_UTCR1(sport) << 8;
791 quot &= 0xfff;
792 *baud = sport->port.uartclk / (16 * (quot + 1));
793 }
794}
795
796static int __init
797sa1100_console_setup(struct console *co, char *options)
798{
799 struct sa1100_port *sport;
800 int baud = 9600;
801 int bits = 8;
802 int parity = 'n';
803 int flow = 'n';
804
805 /*
806 * Check whether an invalid uart number has been specified, and
807 * if so, search for the first available port that does have
808 * console support.
809 */
810 if (co->index == -1 || co->index >= NR_PORTS)
811 co->index = 0;
812 sport = &sa1100_ports[co->index];
813
814 if (options)
815 uart_parse_options(options, &baud, &parity, &bits, &flow);
816 else
817 sa1100_console_get_options(sport, &baud, &parity, &bits);
818
819 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
820}
821
822extern struct uart_driver sa1100_reg;
823static struct console sa1100_console = {
824 .name = "ttySA",
825 .write = sa1100_console_write,
826 .device = uart_console_device,
827 .setup = sa1100_console_setup,
828 .flags = CON_PRINTBUFFER,
829 .index = -1,
830 .data = &sa1100_reg,
831};
832
833static int __init sa1100_rs_console_init(void)
834{
835 sa1100_init_ports();
836 register_console(&sa1100_console);
837 return 0;
838}
839console_initcall(sa1100_rs_console_init);
840
841#define SA1100_CONSOLE &sa1100_console
842#else
843#define SA1100_CONSOLE NULL
844#endif
845
846static struct uart_driver sa1100_reg = {
847 .owner = THIS_MODULE,
848 .driver_name = "ttySA",
849 .dev_name = "ttySA",
850 .devfs_name = "ttySA",
851 .major = SERIAL_SA1100_MAJOR,
852 .minor = MINOR_START,
853 .nr = NR_PORTS,
854 .cons = SA1100_CONSOLE,
855};
856
857static int sa1100_serial_suspend(struct device *_dev, u32 state, u32 level)
858{
859 struct sa1100_port *sport = dev_get_drvdata(_dev);
860
861 if (sport && level == SUSPEND_DISABLE)
862 uart_suspend_port(&sa1100_reg, &sport->port);
863
864 return 0;
865}
866
867static int sa1100_serial_resume(struct device *_dev, u32 level)
868{
869 struct sa1100_port *sport = dev_get_drvdata(_dev);
870
871 if (sport && level == RESUME_ENABLE)
872 uart_resume_port(&sa1100_reg, &sport->port);
873
874 return 0;
875}
876
877static int sa1100_serial_probe(struct device *_dev)
878{
879 struct platform_device *dev = to_platform_device(_dev);
880 struct resource *res = dev->resource;
881 int i;
882
883 for (i = 0; i < dev->num_resources; i++, res++)
884 if (res->flags & IORESOURCE_MEM)
885 break;
886
887 if (i < dev->num_resources) {
888 for (i = 0; i < NR_PORTS; i++) {
889 if (sa1100_ports[i].port.mapbase != res->start)
890 continue;
891
892 sa1100_ports[i].port.dev = _dev;
893 uart_add_one_port(&sa1100_reg, &sa1100_ports[i].port);
894 dev_set_drvdata(_dev, &sa1100_ports[i]);
895 break;
896 }
897 }
898
899 return 0;
900}
901
902static int sa1100_serial_remove(struct device *_dev)
903{
904 struct sa1100_port *sport = dev_get_drvdata(_dev);
905
906 dev_set_drvdata(_dev, NULL);
907
908 if (sport)
909 uart_remove_one_port(&sa1100_reg, &sport->port);
910
911 return 0;
912}
913
914static struct device_driver sa11x0_serial_driver = {
915 .name = "sa11x0-uart",
916 .bus = &platform_bus_type,
917 .probe = sa1100_serial_probe,
918 .remove = sa1100_serial_remove,
919 .suspend = sa1100_serial_suspend,
920 .resume = sa1100_serial_resume,
921};
922
923static int __init sa1100_serial_init(void)
924{
925 int ret;
926
927 printk(KERN_INFO "Serial: SA11x0 driver $Revision: 1.50 $\n");
928
929 sa1100_init_ports();
930
931 ret = uart_register_driver(&sa1100_reg);
932 if (ret == 0) {
933 ret = driver_register(&sa11x0_serial_driver);
934 if (ret)
935 uart_unregister_driver(&sa1100_reg);
936 }
937 return ret;
938}
939
940static void __exit sa1100_serial_exit(void)
941{
942 driver_unregister(&sa11x0_serial_driver);
943 uart_unregister_driver(&sa1100_reg);
944}
945
946module_init(sa1100_serial_init);
947module_exit(sa1100_serial_exit);
948
949MODULE_AUTHOR("Deep Blue Solutions Ltd");
950MODULE_DESCRIPTION("SA1100 generic serial port driver $Revision: 1.50 $");
951MODULE_LICENSE("GPL");
952MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_SA1100_MAJOR);
diff --git a/drivers/serial/serial_core.c b/drivers/serial/serial_core.c
new file mode 100644
index 000000000000..36b1ae083fb7
--- /dev/null
+++ b/drivers/serial/serial_core.c
@@ -0,0 +1,2395 @@
1/*
2 * linux/drivers/char/core.c
3 *
4 * Driver core for serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25#include <linux/config.h>
26#include <linux/module.h>
27#include <linux/tty.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/console.h>
31#include <linux/serial_core.h>
32#include <linux/smp_lock.h>
33#include <linux/device.h>
34#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
35#include <linux/delay.h>
36
37#include <asm/irq.h>
38#include <asm/uaccess.h>
39
40#undef DEBUG
41#ifdef DEBUG
42#define DPRINTK(x...) printk(x)
43#else
44#define DPRINTK(x...) do { } while (0)
45#endif
46
47/*
48 * This is used to lock changes in serial line configuration.
49 */
50static DECLARE_MUTEX(port_sem);
51
52#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53
54#define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
55
56#ifdef CONFIG_SERIAL_CORE_CONSOLE
57#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
58#else
59#define uart_console(port) (0)
60#endif
61
62static void uart_change_speed(struct uart_state *state, struct termios *old_termios);
63static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
64static void uart_change_pm(struct uart_state *state, int pm_state);
65
66/*
67 * This routine is used by the interrupt handler to schedule processing in
68 * the software interrupt portion of the driver.
69 */
70void uart_write_wakeup(struct uart_port *port)
71{
72 struct uart_info *info = port->info;
73 tasklet_schedule(&info->tlet);
74}
75
76static void uart_stop(struct tty_struct *tty)
77{
78 struct uart_state *state = tty->driver_data;
79 struct uart_port *port = state->port;
80 unsigned long flags;
81
82 spin_lock_irqsave(&port->lock, flags);
83 port->ops->stop_tx(port, 1);
84 spin_unlock_irqrestore(&port->lock, flags);
85}
86
87static void __uart_start(struct tty_struct *tty)
88{
89 struct uart_state *state = tty->driver_data;
90 struct uart_port *port = state->port;
91
92 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
93 !tty->stopped && !tty->hw_stopped)
94 port->ops->start_tx(port, 1);
95}
96
97static void uart_start(struct tty_struct *tty)
98{
99 struct uart_state *state = tty->driver_data;
100 struct uart_port *port = state->port;
101 unsigned long flags;
102
103 spin_lock_irqsave(&port->lock, flags);
104 __uart_start(tty);
105 spin_unlock_irqrestore(&port->lock, flags);
106}
107
108static void uart_tasklet_action(unsigned long data)
109{
110 struct uart_state *state = (struct uart_state *)data;
111 tty_wakeup(state->info->tty);
112}
113
114static inline void
115uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
116{
117 unsigned long flags;
118 unsigned int old;
119
120 spin_lock_irqsave(&port->lock, flags);
121 old = port->mctrl;
122 port->mctrl = (old & ~clear) | set;
123 if (old != port->mctrl)
124 port->ops->set_mctrl(port, port->mctrl);
125 spin_unlock_irqrestore(&port->lock, flags);
126}
127
128#define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
129#define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
130
131/*
132 * Startup the port. This will be called once per open. All calls
133 * will be serialised by the per-port semaphore.
134 */
135static int uart_startup(struct uart_state *state, int init_hw)
136{
137 struct uart_info *info = state->info;
138 struct uart_port *port = state->port;
139 unsigned long page;
140 int retval = 0;
141
142 if (info->flags & UIF_INITIALIZED)
143 return 0;
144
145 /*
146 * Set the TTY IO error marker - we will only clear this
147 * once we have successfully opened the port. Also set
148 * up the tty->alt_speed kludge
149 */
150 if (info->tty)
151 set_bit(TTY_IO_ERROR, &info->tty->flags);
152
153 if (port->type == PORT_UNKNOWN)
154 return 0;
155
156 /*
157 * Initialise and allocate the transmit and temporary
158 * buffer.
159 */
160 if (!info->xmit.buf) {
161 page = get_zeroed_page(GFP_KERNEL);
162 if (!page)
163 return -ENOMEM;
164
165 info->xmit.buf = (unsigned char *) page;
166 uart_circ_clear(&info->xmit);
167 }
168
169 retval = port->ops->startup(port);
170 if (retval == 0) {
171 if (init_hw) {
172 /*
173 * Initialise the hardware port settings.
174 */
175 uart_change_speed(state, NULL);
176
177 /*
178 * Setup the RTS and DTR signals once the
179 * port is open and ready to respond.
180 */
181 if (info->tty->termios->c_cflag & CBAUD)
182 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
183 }
184
185 info->flags |= UIF_INITIALIZED;
186
187 clear_bit(TTY_IO_ERROR, &info->tty->flags);
188 }
189
190 if (retval && capable(CAP_SYS_ADMIN))
191 retval = 0;
192
193 return retval;
194}
195
196/*
197 * This routine will shutdown a serial port; interrupts are disabled, and
198 * DTR is dropped if the hangup on close termio flag is on. Calls to
199 * uart_shutdown are serialised by the per-port semaphore.
200 */
201static void uart_shutdown(struct uart_state *state)
202{
203 struct uart_info *info = state->info;
204 struct uart_port *port = state->port;
205
206 if (!(info->flags & UIF_INITIALIZED))
207 return;
208
209 /*
210 * Turn off DTR and RTS early.
211 */
212 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
213 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
214
215 /*
216 * clear delta_msr_wait queue to avoid mem leaks: we may free
217 * the irq here so the queue might never be woken up. Note
218 * that we won't end up waiting on delta_msr_wait again since
219 * any outstanding file descriptors should be pointing at
220 * hung_up_tty_fops now.
221 */
222 wake_up_interruptible(&info->delta_msr_wait);
223
224 /*
225 * Free the IRQ and disable the port.
226 */
227 port->ops->shutdown(port);
228
229 /*
230 * Ensure that the IRQ handler isn't running on another CPU.
231 */
232 synchronize_irq(port->irq);
233
234 /*
235 * Free the transmit buffer page.
236 */
237 if (info->xmit.buf) {
238 free_page((unsigned long)info->xmit.buf);
239 info->xmit.buf = NULL;
240 }
241
242 /*
243 * kill off our tasklet
244 */
245 tasklet_kill(&info->tlet);
246 if (info->tty)
247 set_bit(TTY_IO_ERROR, &info->tty->flags);
248
249 info->flags &= ~UIF_INITIALIZED;
250}
251
252/**
253 * uart_update_timeout - update per-port FIFO timeout.
254 * @port: uart_port structure describing the port
255 * @cflag: termios cflag value
256 * @baud: speed of the port
257 *
258 * Set the port FIFO timeout value. The @cflag value should
259 * reflect the actual hardware settings.
260 */
261void
262uart_update_timeout(struct uart_port *port, unsigned int cflag,
263 unsigned int baud)
264{
265 unsigned int bits;
266
267 /* byte size and parity */
268 switch (cflag & CSIZE) {
269 case CS5:
270 bits = 7;
271 break;
272 case CS6:
273 bits = 8;
274 break;
275 case CS7:
276 bits = 9;
277 break;
278 default:
279 bits = 10;
280 break; // CS8
281 }
282
283 if (cflag & CSTOPB)
284 bits++;
285 if (cflag & PARENB)
286 bits++;
287
288 /*
289 * The total number of bits to be transmitted in the fifo.
290 */
291 bits = bits * port->fifosize;
292
293 /*
294 * Figure the timeout to send the above number of bits.
295 * Add .02 seconds of slop
296 */
297 port->timeout = (HZ * bits) / baud + HZ/50;
298}
299
300EXPORT_SYMBOL(uart_update_timeout);
301
302/**
303 * uart_get_baud_rate - return baud rate for a particular port
304 * @port: uart_port structure describing the port in question.
305 * @termios: desired termios settings.
306 * @old: old termios (or NULL)
307 * @min: minimum acceptable baud rate
308 * @max: maximum acceptable baud rate
309 *
310 * Decode the termios structure into a numeric baud rate,
311 * taking account of the magic 38400 baud rate (with spd_*
312 * flags), and mapping the %B0 rate to 9600 baud.
313 *
314 * If the new baud rate is invalid, try the old termios setting.
315 * If it's still invalid, we try 9600 baud.
316 *
317 * Update the @termios structure to reflect the baud rate
318 * we're actually going to be using.
319 */
320unsigned int
321uart_get_baud_rate(struct uart_port *port, struct termios *termios,
322 struct termios *old, unsigned int min, unsigned int max)
323{
324 unsigned int try, baud, altbaud = 38400;
325 unsigned int flags = port->flags & UPF_SPD_MASK;
326
327 if (flags == UPF_SPD_HI)
328 altbaud = 57600;
329 if (flags == UPF_SPD_VHI)
330 altbaud = 115200;
331 if (flags == UPF_SPD_SHI)
332 altbaud = 230400;
333 if (flags == UPF_SPD_WARP)
334 altbaud = 460800;
335
336 for (try = 0; try < 2; try++) {
337 baud = tty_termios_baud_rate(termios);
338
339 /*
340 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
341 * Die! Die! Die!
342 */
343 if (baud == 38400)
344 baud = altbaud;
345
346 /*
347 * Special case: B0 rate.
348 */
349 if (baud == 0)
350 baud = 9600;
351
352 if (baud >= min && baud <= max)
353 return baud;
354
355 /*
356 * Oops, the quotient was zero. Try again with
357 * the old baud rate if possible.
358 */
359 termios->c_cflag &= ~CBAUD;
360 if (old) {
361 termios->c_cflag |= old->c_cflag & CBAUD;
362 old = NULL;
363 continue;
364 }
365
366 /*
367 * As a last resort, if the quotient is zero,
368 * default to 9600 bps
369 */
370 termios->c_cflag |= B9600;
371 }
372
373 return 0;
374}
375
376EXPORT_SYMBOL(uart_get_baud_rate);
377
378/**
379 * uart_get_divisor - return uart clock divisor
380 * @port: uart_port structure describing the port.
381 * @baud: desired baud rate
382 *
383 * Calculate the uart clock divisor for the port.
384 */
385unsigned int
386uart_get_divisor(struct uart_port *port, unsigned int baud)
387{
388 unsigned int quot;
389
390 /*
391 * Old custom speed handling.
392 */
393 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
394 quot = port->custom_divisor;
395 else
396 quot = (port->uartclk + (8 * baud)) / (16 * baud);
397
398 return quot;
399}
400
401EXPORT_SYMBOL(uart_get_divisor);
402
403static void
404uart_change_speed(struct uart_state *state, struct termios *old_termios)
405{
406 struct tty_struct *tty = state->info->tty;
407 struct uart_port *port = state->port;
408 struct termios *termios;
409
410 /*
411 * If we have no tty, termios, or the port does not exist,
412 * then we can't set the parameters for this port.
413 */
414 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
415 return;
416
417 termios = tty->termios;
418
419 /*
420 * Set flags based on termios cflag
421 */
422 if (termios->c_cflag & CRTSCTS)
423 state->info->flags |= UIF_CTS_FLOW;
424 else
425 state->info->flags &= ~UIF_CTS_FLOW;
426
427 if (termios->c_cflag & CLOCAL)
428 state->info->flags &= ~UIF_CHECK_CD;
429 else
430 state->info->flags |= UIF_CHECK_CD;
431
432 port->ops->set_termios(port, termios, old_termios);
433}
434
435static inline void
436__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
437{
438 unsigned long flags;
439
440 if (!circ->buf)
441 return;
442
443 spin_lock_irqsave(&port->lock, flags);
444 if (uart_circ_chars_free(circ) != 0) {
445 circ->buf[circ->head] = c;
446 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
447 }
448 spin_unlock_irqrestore(&port->lock, flags);
449}
450
451static void uart_put_char(struct tty_struct *tty, unsigned char ch)
452{
453 struct uart_state *state = tty->driver_data;
454
455 __uart_put_char(state->port, &state->info->xmit, ch);
456}
457
458static void uart_flush_chars(struct tty_struct *tty)
459{
460 uart_start(tty);
461}
462
463static int
464uart_write(struct tty_struct *tty, const unsigned char * buf, int count)
465{
466 struct uart_state *state = tty->driver_data;
467 struct uart_port *port = state->port;
468 struct circ_buf *circ = &state->info->xmit;
469 unsigned long flags;
470 int c, ret = 0;
471
472 if (!circ->buf)
473 return 0;
474
475 spin_lock_irqsave(&port->lock, flags);
476 while (1) {
477 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
478 if (count < c)
479 c = count;
480 if (c <= 0)
481 break;
482 memcpy(circ->buf + circ->head, buf, c);
483 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
484 buf += c;
485 count -= c;
486 ret += c;
487 }
488 spin_unlock_irqrestore(&port->lock, flags);
489
490 uart_start(tty);
491 return ret;
492}
493
494static int uart_write_room(struct tty_struct *tty)
495{
496 struct uart_state *state = tty->driver_data;
497
498 return uart_circ_chars_free(&state->info->xmit);
499}
500
501static int uart_chars_in_buffer(struct tty_struct *tty)
502{
503 struct uart_state *state = tty->driver_data;
504
505 return uart_circ_chars_pending(&state->info->xmit);
506}
507
508static void uart_flush_buffer(struct tty_struct *tty)
509{
510 struct uart_state *state = tty->driver_data;
511 struct uart_port *port = state->port;
512 unsigned long flags;
513
514 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
515
516 spin_lock_irqsave(&port->lock, flags);
517 uart_circ_clear(&state->info->xmit);
518 spin_unlock_irqrestore(&port->lock, flags);
519 tty_wakeup(tty);
520}
521
522/*
523 * This function is used to send a high-priority XON/XOFF character to
524 * the device
525 */
526static void uart_send_xchar(struct tty_struct *tty, char ch)
527{
528 struct uart_state *state = tty->driver_data;
529 struct uart_port *port = state->port;
530 unsigned long flags;
531
532 if (port->ops->send_xchar)
533 port->ops->send_xchar(port, ch);
534 else {
535 port->x_char = ch;
536 if (ch) {
537 spin_lock_irqsave(&port->lock, flags);
538 port->ops->start_tx(port, 0);
539 spin_unlock_irqrestore(&port->lock, flags);
540 }
541 }
542}
543
544static void uart_throttle(struct tty_struct *tty)
545{
546 struct uart_state *state = tty->driver_data;
547
548 if (I_IXOFF(tty))
549 uart_send_xchar(tty, STOP_CHAR(tty));
550
551 if (tty->termios->c_cflag & CRTSCTS)
552 uart_clear_mctrl(state->port, TIOCM_RTS);
553}
554
555static void uart_unthrottle(struct tty_struct *tty)
556{
557 struct uart_state *state = tty->driver_data;
558 struct uart_port *port = state->port;
559
560 if (I_IXOFF(tty)) {
561 if (port->x_char)
562 port->x_char = 0;
563 else
564 uart_send_xchar(tty, START_CHAR(tty));
565 }
566
567 if (tty->termios->c_cflag & CRTSCTS)
568 uart_set_mctrl(port, TIOCM_RTS);
569}
570
571static int uart_get_info(struct uart_state *state,
572 struct serial_struct __user *retinfo)
573{
574 struct uart_port *port = state->port;
575 struct serial_struct tmp;
576
577 memset(&tmp, 0, sizeof(tmp));
578 tmp.type = port->type;
579 tmp.line = port->line;
580 tmp.port = port->iobase;
581 if (HIGH_BITS_OFFSET)
582 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
583 tmp.irq = port->irq;
584 tmp.flags = port->flags;
585 tmp.xmit_fifo_size = port->fifosize;
586 tmp.baud_base = port->uartclk / 16;
587 tmp.close_delay = state->close_delay / 10;
588 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
589 ASYNC_CLOSING_WAIT_NONE :
590 state->closing_wait / 10;
591 tmp.custom_divisor = port->custom_divisor;
592 tmp.hub6 = port->hub6;
593 tmp.io_type = port->iotype;
594 tmp.iomem_reg_shift = port->regshift;
595 tmp.iomem_base = (void *)port->mapbase;
596
597 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
598 return -EFAULT;
599 return 0;
600}
601
602static int uart_set_info(struct uart_state *state,
603 struct serial_struct __user *newinfo)
604{
605 struct serial_struct new_serial;
606 struct uart_port *port = state->port;
607 unsigned long new_port;
608 unsigned int change_irq, change_port, old_flags, closing_wait;
609 unsigned int old_custom_divisor, close_delay;
610 int retval = 0;
611
612 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
613 return -EFAULT;
614
615 new_port = new_serial.port;
616 if (HIGH_BITS_OFFSET)
617 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
618
619 new_serial.irq = irq_canonicalize(new_serial.irq);
620 close_delay = new_serial.close_delay * 10;
621 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
622 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
623
624 /*
625 * This semaphore protects state->count. It is also
626 * very useful to prevent opens. Also, take the
627 * port configuration semaphore to make sure that a
628 * module insertion/removal doesn't change anything
629 * under us.
630 */
631 down(&state->sem);
632
633 change_irq = new_serial.irq != port->irq;
634
635 /*
636 * Since changing the 'type' of the port changes its resource
637 * allocations, we should treat type changes the same as
638 * IO port changes.
639 */
640 change_port = new_port != port->iobase ||
641 (unsigned long)new_serial.iomem_base != port->mapbase ||
642 new_serial.hub6 != port->hub6 ||
643 new_serial.io_type != port->iotype ||
644 new_serial.iomem_reg_shift != port->regshift ||
645 new_serial.type != port->type;
646
647 old_flags = port->flags;
648 old_custom_divisor = port->custom_divisor;
649
650 if (!capable(CAP_SYS_ADMIN)) {
651 retval = -EPERM;
652 if (change_irq || change_port ||
653 (new_serial.baud_base != port->uartclk / 16) ||
654 (close_delay != state->close_delay) ||
655 (closing_wait != state->closing_wait) ||
656 (new_serial.xmit_fifo_size != port->fifosize) ||
657 (((new_serial.flags ^ old_flags) & ~UPF_USR_MASK) != 0))
658 goto exit;
659 port->flags = ((port->flags & ~UPF_USR_MASK) |
660 (new_serial.flags & UPF_USR_MASK));
661 port->custom_divisor = new_serial.custom_divisor;
662 goto check_and_exit;
663 }
664
665 /*
666 * Ask the low level driver to verify the settings.
667 */
668 if (port->ops->verify_port)
669 retval = port->ops->verify_port(port, &new_serial);
670
671 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
672 (new_serial.baud_base < 9600))
673 retval = -EINVAL;
674
675 if (retval)
676 goto exit;
677
678 if (change_port || change_irq) {
679 retval = -EBUSY;
680
681 /*
682 * Make sure that we are the sole user of this port.
683 */
684 if (uart_users(state) > 1)
685 goto exit;
686
687 /*
688 * We need to shutdown the serial port at the old
689 * port/type/irq combination.
690 */
691 uart_shutdown(state);
692 }
693
694 if (change_port) {
695 unsigned long old_iobase, old_mapbase;
696 unsigned int old_type, old_iotype, old_hub6, old_shift;
697
698 old_iobase = port->iobase;
699 old_mapbase = port->mapbase;
700 old_type = port->type;
701 old_hub6 = port->hub6;
702 old_iotype = port->iotype;
703 old_shift = port->regshift;
704
705 /*
706 * Free and release old regions
707 */
708 if (old_type != PORT_UNKNOWN)
709 port->ops->release_port(port);
710
711 port->iobase = new_port;
712 port->type = new_serial.type;
713 port->hub6 = new_serial.hub6;
714 port->iotype = new_serial.io_type;
715 port->regshift = new_serial.iomem_reg_shift;
716 port->mapbase = (unsigned long)new_serial.iomem_base;
717
718 /*
719 * Claim and map the new regions
720 */
721 if (port->type != PORT_UNKNOWN) {
722 retval = port->ops->request_port(port);
723 } else {
724 /* Always success - Jean II */
725 retval = 0;
726 }
727
728 /*
729 * If we fail to request resources for the
730 * new port, try to restore the old settings.
731 */
732 if (retval && old_type != PORT_UNKNOWN) {
733 port->iobase = old_iobase;
734 port->type = old_type;
735 port->hub6 = old_hub6;
736 port->iotype = old_iotype;
737 port->regshift = old_shift;
738 port->mapbase = old_mapbase;
739 retval = port->ops->request_port(port);
740 /*
741 * If we failed to restore the old settings,
742 * we fail like this.
743 */
744 if (retval)
745 port->type = PORT_UNKNOWN;
746
747 /*
748 * We failed anyway.
749 */
750 retval = -EBUSY;
751 }
752 }
753
754 port->irq = new_serial.irq;
755 port->uartclk = new_serial.baud_base * 16;
756 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
757 (new_serial.flags & UPF_CHANGE_MASK);
758 port->custom_divisor = new_serial.custom_divisor;
759 state->close_delay = close_delay;
760 state->closing_wait = closing_wait;
761 port->fifosize = new_serial.xmit_fifo_size;
762 if (state->info->tty)
763 state->info->tty->low_latency =
764 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
765
766 check_and_exit:
767 retval = 0;
768 if (port->type == PORT_UNKNOWN)
769 goto exit;
770 if (state->info->flags & UIF_INITIALIZED) {
771 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
772 old_custom_divisor != port->custom_divisor) {
773 /*
774 * If they're setting up a custom divisor or speed,
775 * instead of clearing it, then bitch about it. No
776 * need to rate-limit; it's CAP_SYS_ADMIN only.
777 */
778 if (port->flags & UPF_SPD_MASK) {
779 char buf[64];
780 printk(KERN_NOTICE
781 "%s sets custom speed on %s. This "
782 "is deprecated.\n", current->comm,
783 tty_name(state->info->tty, buf));
784 }
785 uart_change_speed(state, NULL);
786 }
787 } else
788 retval = uart_startup(state, 1);
789 exit:
790 up(&state->sem);
791 return retval;
792}
793
794
795/*
796 * uart_get_lsr_info - get line status register info.
797 * Note: uart_ioctl protects us against hangups.
798 */
799static int uart_get_lsr_info(struct uart_state *state,
800 unsigned int __user *value)
801{
802 struct uart_port *port = state->port;
803 unsigned int result;
804
805 result = port->ops->tx_empty(port);
806
807 /*
808 * If we're about to load something into the transmit
809 * register, we'll pretend the transmitter isn't empty to
810 * avoid a race condition (depending on when the transmit
811 * interrupt happens).
812 */
813 if (port->x_char ||
814 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
815 !state->info->tty->stopped && !state->info->tty->hw_stopped))
816 result &= ~TIOCSER_TEMT;
817
818 return put_user(result, value);
819}
820
821static int uart_tiocmget(struct tty_struct *tty, struct file *file)
822{
823 struct uart_state *state = tty->driver_data;
824 struct uart_port *port = state->port;
825 int result = -EIO;
826
827 down(&state->sem);
828 if ((!file || !tty_hung_up_p(file)) &&
829 !(tty->flags & (1 << TTY_IO_ERROR))) {
830 result = port->mctrl;
831 result |= port->ops->get_mctrl(port);
832 }
833 up(&state->sem);
834
835 return result;
836}
837
838static int
839uart_tiocmset(struct tty_struct *tty, struct file *file,
840 unsigned int set, unsigned int clear)
841{
842 struct uart_state *state = tty->driver_data;
843 struct uart_port *port = state->port;
844 int ret = -EIO;
845
846 down(&state->sem);
847 if ((!file || !tty_hung_up_p(file)) &&
848 !(tty->flags & (1 << TTY_IO_ERROR))) {
849 uart_update_mctrl(port, set, clear);
850 ret = 0;
851 }
852 up(&state->sem);
853 return ret;
854}
855
856static void uart_break_ctl(struct tty_struct *tty, int break_state)
857{
858 struct uart_state *state = tty->driver_data;
859 struct uart_port *port = state->port;
860
861 BUG_ON(!kernel_locked());
862
863 down(&state->sem);
864
865 if (port->type != PORT_UNKNOWN)
866 port->ops->break_ctl(port, break_state);
867
868 up(&state->sem);
869}
870
871static int uart_do_autoconfig(struct uart_state *state)
872{
873 struct uart_port *port = state->port;
874 int flags, ret;
875
876 if (!capable(CAP_SYS_ADMIN))
877 return -EPERM;
878
879 /*
880 * Take the per-port semaphore. This prevents count from
881 * changing, and hence any extra opens of the port while
882 * we're auto-configuring.
883 */
884 if (down_interruptible(&state->sem))
885 return -ERESTARTSYS;
886
887 ret = -EBUSY;
888 if (uart_users(state) == 1) {
889 uart_shutdown(state);
890
891 /*
892 * If we already have a port type configured,
893 * we must release its resources.
894 */
895 if (port->type != PORT_UNKNOWN)
896 port->ops->release_port(port);
897
898 flags = UART_CONFIG_TYPE;
899 if (port->flags & UPF_AUTO_IRQ)
900 flags |= UART_CONFIG_IRQ;
901
902 /*
903 * This will claim the ports resources if
904 * a port is found.
905 */
906 port->ops->config_port(port, flags);
907
908 ret = uart_startup(state, 1);
909 }
910 up(&state->sem);
911 return ret;
912}
913
914/*
915 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
916 * - mask passed in arg for lines of interest
917 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
918 * Caller should use TIOCGICOUNT to see which one it was
919 */
920static int
921uart_wait_modem_status(struct uart_state *state, unsigned long arg)
922{
923 struct uart_port *port = state->port;
924 DECLARE_WAITQUEUE(wait, current);
925 struct uart_icount cprev, cnow;
926 int ret;
927
928 /*
929 * note the counters on entry
930 */
931 spin_lock_irq(&port->lock);
932 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
933
934 /*
935 * Force modem status interrupts on
936 */
937 port->ops->enable_ms(port);
938 spin_unlock_irq(&port->lock);
939
940 add_wait_queue(&state->info->delta_msr_wait, &wait);
941 for (;;) {
942 spin_lock_irq(&port->lock);
943 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
944 spin_unlock_irq(&port->lock);
945
946 set_current_state(TASK_INTERRUPTIBLE);
947
948 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
949 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
950 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
951 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
952 ret = 0;
953 break;
954 }
955
956 schedule();
957
958 /* see if a signal did it */
959 if (signal_pending(current)) {
960 ret = -ERESTARTSYS;
961 break;
962 }
963
964 cprev = cnow;
965 }
966
967 current->state = TASK_RUNNING;
968 remove_wait_queue(&state->info->delta_msr_wait, &wait);
969
970 return ret;
971}
972
973/*
974 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
975 * Return: write counters to the user passed counter struct
976 * NB: both 1->0 and 0->1 transitions are counted except for
977 * RI where only 0->1 is counted.
978 */
979static int uart_get_count(struct uart_state *state,
980 struct serial_icounter_struct __user *icnt)
981{
982 struct serial_icounter_struct icount;
983 struct uart_icount cnow;
984 struct uart_port *port = state->port;
985
986 spin_lock_irq(&port->lock);
987 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
988 spin_unlock_irq(&port->lock);
989
990 icount.cts = cnow.cts;
991 icount.dsr = cnow.dsr;
992 icount.rng = cnow.rng;
993 icount.dcd = cnow.dcd;
994 icount.rx = cnow.rx;
995 icount.tx = cnow.tx;
996 icount.frame = cnow.frame;
997 icount.overrun = cnow.overrun;
998 icount.parity = cnow.parity;
999 icount.brk = cnow.brk;
1000 icount.buf_overrun = cnow.buf_overrun;
1001
1002 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1003}
1004
1005/*
1006 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1007 */
1008static int
1009uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1010 unsigned long arg)
1011{
1012 struct uart_state *state = tty->driver_data;
1013 void __user *uarg = (void __user *)arg;
1014 int ret = -ENOIOCTLCMD;
1015
1016 BUG_ON(!kernel_locked());
1017
1018 /*
1019 * These ioctls don't rely on the hardware to be present.
1020 */
1021 switch (cmd) {
1022 case TIOCGSERIAL:
1023 ret = uart_get_info(state, uarg);
1024 break;
1025
1026 case TIOCSSERIAL:
1027 ret = uart_set_info(state, uarg);
1028 break;
1029
1030 case TIOCSERCONFIG:
1031 ret = uart_do_autoconfig(state);
1032 break;
1033
1034 case TIOCSERGWILD: /* obsolete */
1035 case TIOCSERSWILD: /* obsolete */
1036 ret = 0;
1037 break;
1038 }
1039
1040 if (ret != -ENOIOCTLCMD)
1041 goto out;
1042
1043 if (tty->flags & (1 << TTY_IO_ERROR)) {
1044 ret = -EIO;
1045 goto out;
1046 }
1047
1048 /*
1049 * The following should only be used when hardware is present.
1050 */
1051 switch (cmd) {
1052 case TIOCMIWAIT:
1053 ret = uart_wait_modem_status(state, arg);
1054 break;
1055
1056 case TIOCGICOUNT:
1057 ret = uart_get_count(state, uarg);
1058 break;
1059 }
1060
1061 if (ret != -ENOIOCTLCMD)
1062 goto out;
1063
1064 down(&state->sem);
1065
1066 if (tty_hung_up_p(filp)) {
1067 ret = -EIO;
1068 goto out_up;
1069 }
1070
1071 /*
1072 * All these rely on hardware being present and need to be
1073 * protected against the tty being hung up.
1074 */
1075 switch (cmd) {
1076 case TIOCSERGETLSR: /* Get line status register */
1077 ret = uart_get_lsr_info(state, uarg);
1078 break;
1079
1080 default: {
1081 struct uart_port *port = state->port;
1082 if (port->ops->ioctl)
1083 ret = port->ops->ioctl(port, cmd, arg);
1084 break;
1085 }
1086 }
1087 out_up:
1088 up(&state->sem);
1089 out:
1090 return ret;
1091}
1092
1093static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1094{
1095 struct uart_state *state = tty->driver_data;
1096 unsigned long flags;
1097 unsigned int cflag = tty->termios->c_cflag;
1098
1099 BUG_ON(!kernel_locked());
1100
1101 /*
1102 * These are the bits that are used to setup various
1103 * flags in the low level driver.
1104 */
1105#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1106
1107 if ((cflag ^ old_termios->c_cflag) == 0 &&
1108 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1109 return;
1110
1111 uart_change_speed(state, old_termios);
1112
1113 /* Handle transition to B0 status */
1114 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1115 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1116
1117 /* Handle transition away from B0 status */
1118 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1119 unsigned int mask = TIOCM_DTR;
1120 if (!(cflag & CRTSCTS) ||
1121 !test_bit(TTY_THROTTLED, &tty->flags))
1122 mask |= TIOCM_RTS;
1123 uart_set_mctrl(state->port, mask);
1124 }
1125
1126 /* Handle turning off CRTSCTS */
1127 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1128 spin_lock_irqsave(&state->port->lock, flags);
1129 tty->hw_stopped = 0;
1130 __uart_start(tty);
1131 spin_unlock_irqrestore(&state->port->lock, flags);
1132 }
1133
1134#if 0
1135 /*
1136 * No need to wake up processes in open wait, since they
1137 * sample the CLOCAL flag once, and don't recheck it.
1138 * XXX It's not clear whether the current behavior is correct
1139 * or not. Hence, this may change.....
1140 */
1141 if (!(old_termios->c_cflag & CLOCAL) &&
1142 (tty->termios->c_cflag & CLOCAL))
1143 wake_up_interruptible(&state->info->open_wait);
1144#endif
1145}
1146
1147/*
1148 * In 2.4.5, calls to this will be serialized via the BKL in
1149 * linux/drivers/char/tty_io.c:tty_release()
1150 * linux/drivers/char/tty_io.c:do_tty_handup()
1151 */
1152static void uart_close(struct tty_struct *tty, struct file *filp)
1153{
1154 struct uart_state *state = tty->driver_data;
1155 struct uart_port *port;
1156
1157 BUG_ON(!kernel_locked());
1158
1159 if (!state || !state->port)
1160 return;
1161
1162 port = state->port;
1163
1164 DPRINTK("uart_close(%d) called\n", port->line);
1165
1166 down(&state->sem);
1167
1168 if (tty_hung_up_p(filp))
1169 goto done;
1170
1171 if ((tty->count == 1) && (state->count != 1)) {
1172 /*
1173 * Uh, oh. tty->count is 1, which means that the tty
1174 * structure will be freed. state->count should always
1175 * be one in these conditions. If it's greater than
1176 * one, we've got real problems, since it means the
1177 * serial port won't be shutdown.
1178 */
1179 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1180 "state->count is %d\n", state->count);
1181 state->count = 1;
1182 }
1183 if (--state->count < 0) {
1184 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1185 tty->name, state->count);
1186 state->count = 0;
1187 }
1188 if (state->count)
1189 goto done;
1190
1191 /*
1192 * Now we wait for the transmit buffer to clear; and we notify
1193 * the line discipline to only process XON/XOFF characters by
1194 * setting tty->closing.
1195 */
1196 tty->closing = 1;
1197
1198 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1199 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1200
1201 /*
1202 * At this point, we stop accepting input. To do this, we
1203 * disable the receive line status interrupts.
1204 */
1205 if (state->info->flags & UIF_INITIALIZED) {
1206 unsigned long flags;
1207 spin_lock_irqsave(&port->lock, flags);
1208 port->ops->stop_rx(port);
1209 spin_unlock_irqrestore(&port->lock, flags);
1210 /*
1211 * Before we drop DTR, make sure the UART transmitter
1212 * has completely drained; this is especially
1213 * important if there is a transmit FIFO!
1214 */
1215 uart_wait_until_sent(tty, port->timeout);
1216 }
1217
1218 uart_shutdown(state);
1219 uart_flush_buffer(tty);
1220
1221 tty_ldisc_flush(tty);
1222
1223 tty->closing = 0;
1224 state->info->tty = NULL;
1225
1226 if (state->info->blocked_open) {
1227 if (state->close_delay)
1228 msleep_interruptible(state->close_delay);
1229 } else if (!uart_console(port)) {
1230 uart_change_pm(state, 3);
1231 }
1232
1233 /*
1234 * Wake up anyone trying to open this port.
1235 */
1236 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1237 wake_up_interruptible(&state->info->open_wait);
1238
1239 done:
1240 up(&state->sem);
1241}
1242
1243static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1244{
1245 struct uart_state *state = tty->driver_data;
1246 struct uart_port *port = state->port;
1247 unsigned long char_time, expire;
1248
1249 BUG_ON(!kernel_locked());
1250
1251 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1252 return;
1253
1254 /*
1255 * Set the check interval to be 1/5 of the estimated time to
1256 * send a single character, and make it at least 1. The check
1257 * interval should also be less than the timeout.
1258 *
1259 * Note: we have to use pretty tight timings here to satisfy
1260 * the NIST-PCTS.
1261 */
1262 char_time = (port->timeout - HZ/50) / port->fifosize;
1263 char_time = char_time / 5;
1264 if (char_time == 0)
1265 char_time = 1;
1266 if (timeout && timeout < char_time)
1267 char_time = timeout;
1268
1269 /*
1270 * If the transmitter hasn't cleared in twice the approximate
1271 * amount of time to send the entire FIFO, it probably won't
1272 * ever clear. This assumes the UART isn't doing flow
1273 * control, which is currently the case. Hence, if it ever
1274 * takes longer than port->timeout, this is probably due to a
1275 * UART bug of some kind. So, we clamp the timeout parameter at
1276 * 2*port->timeout.
1277 */
1278 if (timeout == 0 || timeout > 2 * port->timeout)
1279 timeout = 2 * port->timeout;
1280
1281 expire = jiffies + timeout;
1282
1283 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1284 port->line, jiffies, expire);
1285
1286 /*
1287 * Check whether the transmitter is empty every 'char_time'.
1288 * 'timeout' / 'expire' give us the maximum amount of time
1289 * we wait.
1290 */
1291 while (!port->ops->tx_empty(port)) {
1292 msleep_interruptible(jiffies_to_msecs(char_time));
1293 if (signal_pending(current))
1294 break;
1295 if (time_after(jiffies, expire))
1296 break;
1297 }
1298 set_current_state(TASK_RUNNING); /* might not be needed */
1299}
1300
1301/*
1302 * This is called with the BKL held in
1303 * linux/drivers/char/tty_io.c:do_tty_hangup()
1304 * We're called from the eventd thread, so we can sleep for
1305 * a _short_ time only.
1306 */
1307static void uart_hangup(struct tty_struct *tty)
1308{
1309 struct uart_state *state = tty->driver_data;
1310
1311 BUG_ON(!kernel_locked());
1312 DPRINTK("uart_hangup(%d)\n", state->port->line);
1313
1314 down(&state->sem);
1315 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1316 uart_flush_buffer(tty);
1317 uart_shutdown(state);
1318 state->count = 0;
1319 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1320 state->info->tty = NULL;
1321 wake_up_interruptible(&state->info->open_wait);
1322 wake_up_interruptible(&state->info->delta_msr_wait);
1323 }
1324 up(&state->sem);
1325}
1326
1327/*
1328 * Copy across the serial console cflag setting into the termios settings
1329 * for the initial open of the port. This allows continuity between the
1330 * kernel settings, and the settings init adopts when it opens the port
1331 * for the first time.
1332 */
1333static void uart_update_termios(struct uart_state *state)
1334{
1335 struct tty_struct *tty = state->info->tty;
1336 struct uart_port *port = state->port;
1337
1338 if (uart_console(port) && port->cons->cflag) {
1339 tty->termios->c_cflag = port->cons->cflag;
1340 port->cons->cflag = 0;
1341 }
1342
1343 /*
1344 * If the device failed to grab its irq resources,
1345 * or some other error occurred, don't try to talk
1346 * to the port hardware.
1347 */
1348 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1349 /*
1350 * Make termios settings take effect.
1351 */
1352 uart_change_speed(state, NULL);
1353
1354 /*
1355 * And finally enable the RTS and DTR signals.
1356 */
1357 if (tty->termios->c_cflag & CBAUD)
1358 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1359 }
1360}
1361
1362/*
1363 * Block the open until the port is ready. We must be called with
1364 * the per-port semaphore held.
1365 */
1366static int
1367uart_block_til_ready(struct file *filp, struct uart_state *state)
1368{
1369 DECLARE_WAITQUEUE(wait, current);
1370 struct uart_info *info = state->info;
1371 struct uart_port *port = state->port;
1372
1373 info->blocked_open++;
1374 state->count--;
1375
1376 add_wait_queue(&info->open_wait, &wait);
1377 while (1) {
1378 set_current_state(TASK_INTERRUPTIBLE);
1379
1380 /*
1381 * If we have been hung up, tell userspace/restart open.
1382 */
1383 if (tty_hung_up_p(filp) || info->tty == NULL)
1384 break;
1385
1386 /*
1387 * If the port has been closed, tell userspace/restart open.
1388 */
1389 if (!(info->flags & UIF_INITIALIZED))
1390 break;
1391
1392 /*
1393 * If non-blocking mode is set, or CLOCAL mode is set,
1394 * we don't want to wait for the modem status lines to
1395 * indicate that the port is ready.
1396 *
1397 * Also, if the port is not enabled/configured, we want
1398 * to allow the open to succeed here. Note that we will
1399 * have set TTY_IO_ERROR for a non-existant port.
1400 */
1401 if ((filp->f_flags & O_NONBLOCK) ||
1402 (info->tty->termios->c_cflag & CLOCAL) ||
1403 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1404 break;
1405 }
1406
1407 /*
1408 * Set DTR to allow modem to know we're waiting. Do
1409 * not set RTS here - we want to make sure we catch
1410 * the data from the modem.
1411 */
1412 if (info->tty->termios->c_cflag & CBAUD)
1413 uart_set_mctrl(port, TIOCM_DTR);
1414
1415 /*
1416 * and wait for the carrier to indicate that the
1417 * modem is ready for us.
1418 */
1419 if (port->ops->get_mctrl(port) & TIOCM_CAR)
1420 break;
1421
1422 up(&state->sem);
1423 schedule();
1424 down(&state->sem);
1425
1426 if (signal_pending(current))
1427 break;
1428 }
1429 set_current_state(TASK_RUNNING);
1430 remove_wait_queue(&info->open_wait, &wait);
1431
1432 state->count++;
1433 info->blocked_open--;
1434
1435 if (signal_pending(current))
1436 return -ERESTARTSYS;
1437
1438 if (!info->tty || tty_hung_up_p(filp))
1439 return -EAGAIN;
1440
1441 return 0;
1442}
1443
1444static struct uart_state *uart_get(struct uart_driver *drv, int line)
1445{
1446 struct uart_state *state;
1447
1448 down(&port_sem);
1449 state = drv->state + line;
1450 if (down_interruptible(&state->sem)) {
1451 state = ERR_PTR(-ERESTARTSYS);
1452 goto out;
1453 }
1454
1455 state->count++;
1456 if (!state->port) {
1457 state->count--;
1458 up(&state->sem);
1459 state = ERR_PTR(-ENXIO);
1460 goto out;
1461 }
1462
1463 if (!state->info) {
1464 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1465 if (state->info) {
1466 memset(state->info, 0, sizeof(struct uart_info));
1467 init_waitqueue_head(&state->info->open_wait);
1468 init_waitqueue_head(&state->info->delta_msr_wait);
1469
1470 /*
1471 * Link the info into the other structures.
1472 */
1473 state->port->info = state->info;
1474
1475 tasklet_init(&state->info->tlet, uart_tasklet_action,
1476 (unsigned long)state);
1477 } else {
1478 state->count--;
1479 up(&state->sem);
1480 state = ERR_PTR(-ENOMEM);
1481 }
1482 }
1483
1484 out:
1485 up(&port_sem);
1486 return state;
1487}
1488
1489/*
1490 * In 2.4.5, calls to uart_open are serialised by the BKL in
1491 * linux/fs/devices.c:chrdev_open()
1492 * Note that if this fails, then uart_close() _will_ be called.
1493 *
1494 * In time, we want to scrap the "opening nonpresent ports"
1495 * behaviour and implement an alternative way for setserial
1496 * to set base addresses/ports/types. This will allow us to
1497 * get rid of a certain amount of extra tests.
1498 */
1499static int uart_open(struct tty_struct *tty, struct file *filp)
1500{
1501 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1502 struct uart_state *state;
1503 int retval, line = tty->index;
1504
1505 BUG_ON(!kernel_locked());
1506 DPRINTK("uart_open(%d) called\n", line);
1507
1508 /*
1509 * tty->driver->num won't change, so we won't fail here with
1510 * tty->driver_data set to something non-NULL (and therefore
1511 * we won't get caught by uart_close()).
1512 */
1513 retval = -ENODEV;
1514 if (line >= tty->driver->num)
1515 goto fail;
1516
1517 /*
1518 * We take the semaphore inside uart_get to guarantee that we won't
1519 * be re-entered while allocating the info structure, or while we
1520 * request any IRQs that the driver may need. This also has the nice
1521 * side-effect that it delays the action of uart_hangup, so we can
1522 * guarantee that info->tty will always contain something reasonable.
1523 */
1524 state = uart_get(drv, line);
1525 if (IS_ERR(state)) {
1526 retval = PTR_ERR(state);
1527 goto fail;
1528 }
1529
1530 /*
1531 * Once we set tty->driver_data here, we are guaranteed that
1532 * uart_close() will decrement the driver module use count.
1533 * Any failures from here onwards should not touch the count.
1534 */
1535 tty->driver_data = state;
1536 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1537 tty->alt_speed = 0;
1538 state->info->tty = tty;
1539
1540 /*
1541 * If the port is in the middle of closing, bail out now.
1542 */
1543 if (tty_hung_up_p(filp)) {
1544 retval = -EAGAIN;
1545 state->count--;
1546 up(&state->sem);
1547 goto fail;
1548 }
1549
1550 /*
1551 * Make sure the device is in D0 state.
1552 */
1553 if (state->count == 1)
1554 uart_change_pm(state, 0);
1555
1556 /*
1557 * Start up the serial port.
1558 */
1559 retval = uart_startup(state, 0);
1560
1561 /*
1562 * If we succeeded, wait until the port is ready.
1563 */
1564 if (retval == 0)
1565 retval = uart_block_til_ready(filp, state);
1566 up(&state->sem);
1567
1568 /*
1569 * If this is the first open to succeed, adjust things to suit.
1570 */
1571 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1572 state->info->flags |= UIF_NORMAL_ACTIVE;
1573
1574 uart_update_termios(state);
1575 }
1576
1577 fail:
1578 return retval;
1579}
1580
1581static const char *uart_type(struct uart_port *port)
1582{
1583 const char *str = NULL;
1584
1585 if (port->ops->type)
1586 str = port->ops->type(port);
1587
1588 if (!str)
1589 str = "unknown";
1590
1591 return str;
1592}
1593
1594#ifdef CONFIG_PROC_FS
1595
1596static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1597{
1598 struct uart_state *state = drv->state + i;
1599 struct uart_port *port = state->port;
1600 char stat_buf[32];
1601 unsigned int status;
1602 int ret;
1603
1604 if (!port)
1605 return 0;
1606
1607 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1608 port->line, uart_type(port),
1609 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
1610 port->iotype == UPIO_MEM ? port->mapbase :
1611 (unsigned long) port->iobase,
1612 port->irq);
1613
1614 if (port->type == PORT_UNKNOWN) {
1615 strcat(buf, "\n");
1616 return ret + 1;
1617 }
1618
1619 if(capable(CAP_SYS_ADMIN))
1620 {
1621 status = port->ops->get_mctrl(port);
1622
1623 ret += sprintf(buf + ret, " tx:%d rx:%d",
1624 port->icount.tx, port->icount.rx);
1625 if (port->icount.frame)
1626 ret += sprintf(buf + ret, " fe:%d",
1627 port->icount.frame);
1628 if (port->icount.parity)
1629 ret += sprintf(buf + ret, " pe:%d",
1630 port->icount.parity);
1631 if (port->icount.brk)
1632 ret += sprintf(buf + ret, " brk:%d",
1633 port->icount.brk);
1634 if (port->icount.overrun)
1635 ret += sprintf(buf + ret, " oe:%d",
1636 port->icount.overrun);
1637
1638#define INFOBIT(bit,str) \
1639 if (port->mctrl & (bit)) \
1640 strncat(stat_buf, (str), sizeof(stat_buf) - \
1641 strlen(stat_buf) - 2)
1642#define STATBIT(bit,str) \
1643 if (status & (bit)) \
1644 strncat(stat_buf, (str), sizeof(stat_buf) - \
1645 strlen(stat_buf) - 2)
1646
1647 stat_buf[0] = '\0';
1648 stat_buf[1] = '\0';
1649 INFOBIT(TIOCM_RTS, "|RTS");
1650 STATBIT(TIOCM_CTS, "|CTS");
1651 INFOBIT(TIOCM_DTR, "|DTR");
1652 STATBIT(TIOCM_DSR, "|DSR");
1653 STATBIT(TIOCM_CAR, "|CD");
1654 STATBIT(TIOCM_RNG, "|RI");
1655 if (stat_buf[0])
1656 stat_buf[0] = ' ';
1657 strcat(stat_buf, "\n");
1658
1659 ret += sprintf(buf + ret, stat_buf);
1660 } else {
1661 strcat(buf, "\n");
1662 ret++;
1663 }
1664#undef STATBIT
1665#undef INFOBIT
1666 return ret;
1667}
1668
1669static int uart_read_proc(char *page, char **start, off_t off,
1670 int count, int *eof, void *data)
1671{
1672 struct tty_driver *ttydrv = data;
1673 struct uart_driver *drv = ttydrv->driver_state;
1674 int i, len = 0, l;
1675 off_t begin = 0;
1676
1677 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1678 "", "", "");
1679 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1680 l = uart_line_info(page + len, drv, i);
1681 len += l;
1682 if (len + begin > off + count)
1683 goto done;
1684 if (len + begin < off) {
1685 begin += len;
1686 len = 0;
1687 }
1688 }
1689 *eof = 1;
1690 done:
1691 if (off >= len + begin)
1692 return 0;
1693 *start = page + (off - begin);
1694 return (count < begin + len - off) ? count : (begin + len - off);
1695}
1696#endif
1697
1698#ifdef CONFIG_SERIAL_CORE_CONSOLE
1699/*
1700 * Check whether an invalid uart number has been specified, and
1701 * if so, search for the first available port that does have
1702 * console support.
1703 */
1704struct uart_port * __init
1705uart_get_console(struct uart_port *ports, int nr, struct console *co)
1706{
1707 int idx = co->index;
1708
1709 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1710 ports[idx].membase == NULL))
1711 for (idx = 0; idx < nr; idx++)
1712 if (ports[idx].iobase != 0 ||
1713 ports[idx].membase != NULL)
1714 break;
1715
1716 co->index = idx;
1717
1718 return ports + idx;
1719}
1720
1721/**
1722 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1723 * @options: pointer to option string
1724 * @baud: pointer to an 'int' variable for the baud rate.
1725 * @parity: pointer to an 'int' variable for the parity.
1726 * @bits: pointer to an 'int' variable for the number of data bits.
1727 * @flow: pointer to an 'int' variable for the flow control character.
1728 *
1729 * uart_parse_options decodes a string containing the serial console
1730 * options. The format of the string is <baud><parity><bits><flow>,
1731 * eg: 115200n8r
1732 */
1733void __init
1734uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1735{
1736 char *s = options;
1737
1738 *baud = simple_strtoul(s, NULL, 10);
1739 while (*s >= '0' && *s <= '9')
1740 s++;
1741 if (*s)
1742 *parity = *s++;
1743 if (*s)
1744 *bits = *s++ - '0';
1745 if (*s)
1746 *flow = *s;
1747}
1748
1749struct baud_rates {
1750 unsigned int rate;
1751 unsigned int cflag;
1752};
1753
1754static struct baud_rates baud_rates[] = {
1755 { 921600, B921600 },
1756 { 460800, B460800 },
1757 { 230400, B230400 },
1758 { 115200, B115200 },
1759 { 57600, B57600 },
1760 { 38400, B38400 },
1761 { 19200, B19200 },
1762 { 9600, B9600 },
1763 { 4800, B4800 },
1764 { 2400, B2400 },
1765 { 1200, B1200 },
1766 { 0, B38400 }
1767};
1768
1769/**
1770 * uart_set_options - setup the serial console parameters
1771 * @port: pointer to the serial ports uart_port structure
1772 * @co: console pointer
1773 * @baud: baud rate
1774 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1775 * @bits: number of data bits
1776 * @flow: flow control character - 'r' (rts)
1777 */
1778int __init
1779uart_set_options(struct uart_port *port, struct console *co,
1780 int baud, int parity, int bits, int flow)
1781{
1782 struct termios termios;
1783 int i;
1784
1785 memset(&termios, 0, sizeof(struct termios));
1786
1787 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1788
1789 /*
1790 * Construct a cflag setting.
1791 */
1792 for (i = 0; baud_rates[i].rate; i++)
1793 if (baud_rates[i].rate <= baud)
1794 break;
1795
1796 termios.c_cflag |= baud_rates[i].cflag;
1797
1798 if (bits == 7)
1799 termios.c_cflag |= CS7;
1800 else
1801 termios.c_cflag |= CS8;
1802
1803 switch (parity) {
1804 case 'o': case 'O':
1805 termios.c_cflag |= PARODD;
1806 /*fall through*/
1807 case 'e': case 'E':
1808 termios.c_cflag |= PARENB;
1809 break;
1810 }
1811
1812 if (flow == 'r')
1813 termios.c_cflag |= CRTSCTS;
1814
1815 port->ops->set_termios(port, &termios, NULL);
1816 co->cflag = termios.c_cflag;
1817
1818 return 0;
1819}
1820#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1821
1822static void uart_change_pm(struct uart_state *state, int pm_state)
1823{
1824 struct uart_port *port = state->port;
1825 if (port->ops->pm)
1826 port->ops->pm(port, pm_state, state->pm_state);
1827 state->pm_state = pm_state;
1828}
1829
1830int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1831{
1832 struct uart_state *state = drv->state + port->line;
1833
1834 down(&state->sem);
1835
1836 if (state->info && state->info->flags & UIF_INITIALIZED) {
1837 struct uart_ops *ops = port->ops;
1838
1839 spin_lock_irq(&port->lock);
1840 ops->stop_tx(port, 0);
1841 ops->set_mctrl(port, 0);
1842 ops->stop_rx(port);
1843 spin_unlock_irq(&port->lock);
1844
1845 /*
1846 * Wait for the transmitter to empty.
1847 */
1848 while (!ops->tx_empty(port)) {
1849 msleep(10);
1850 }
1851
1852 ops->shutdown(port);
1853 }
1854
1855 /*
1856 * Disable the console device before suspending.
1857 */
1858 if (uart_console(port))
1859 console_stop(port->cons);
1860
1861 uart_change_pm(state, 3);
1862
1863 up(&state->sem);
1864
1865 return 0;
1866}
1867
1868int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1869{
1870 struct uart_state *state = drv->state + port->line;
1871
1872 down(&state->sem);
1873
1874 uart_change_pm(state, 0);
1875
1876 /*
1877 * Re-enable the console device after suspending.
1878 */
1879 if (uart_console(port)) {
1880 struct termios termios;
1881
1882 /*
1883 * First try to use the console cflag setting.
1884 */
1885 memset(&termios, 0, sizeof(struct termios));
1886 termios.c_cflag = port->cons->cflag;
1887
1888 /*
1889 * If that's unset, use the tty termios setting.
1890 */
1891 if (state->info && state->info->tty && termios.c_cflag == 0)
1892 termios = *state->info->tty->termios;
1893
1894 port->ops->set_termios(port, &termios, NULL);
1895 console_start(port->cons);
1896 }
1897
1898 if (state->info && state->info->flags & UIF_INITIALIZED) {
1899 struct uart_ops *ops = port->ops;
1900
1901 ops->set_mctrl(port, 0);
1902 ops->startup(port);
1903 uart_change_speed(state, NULL);
1904 spin_lock_irq(&port->lock);
1905 ops->set_mctrl(port, port->mctrl);
1906 ops->start_tx(port, 0);
1907 spin_unlock_irq(&port->lock);
1908 }
1909
1910 up(&state->sem);
1911
1912 return 0;
1913}
1914
1915static inline void
1916uart_report_port(struct uart_driver *drv, struct uart_port *port)
1917{
1918 printk("%s%d", drv->dev_name, port->line);
1919 printk(" at ");
1920 switch (port->iotype) {
1921 case UPIO_PORT:
1922 printk("I/O 0x%x", port->iobase);
1923 break;
1924 case UPIO_HUB6:
1925 printk("I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1926 break;
1927 case UPIO_MEM:
1928 case UPIO_MEM32:
1929 printk("MMIO 0x%lx", port->mapbase);
1930 break;
1931 }
1932 printk(" (irq = %d) is a %s\n", port->irq, uart_type(port));
1933}
1934
1935static void
1936uart_configure_port(struct uart_driver *drv, struct uart_state *state,
1937 struct uart_port *port)
1938{
1939 unsigned int flags;
1940
1941 /*
1942 * If there isn't a port here, don't do anything further.
1943 */
1944 if (!port->iobase && !port->mapbase && !port->membase)
1945 return;
1946
1947 /*
1948 * Now do the auto configuration stuff. Note that config_port
1949 * is expected to claim the resources and map the port for us.
1950 */
1951 flags = UART_CONFIG_TYPE;
1952 if (port->flags & UPF_AUTO_IRQ)
1953 flags |= UART_CONFIG_IRQ;
1954 if (port->flags & UPF_BOOT_AUTOCONF) {
1955 port->type = PORT_UNKNOWN;
1956 port->ops->config_port(port, flags);
1957 }
1958
1959 if (port->type != PORT_UNKNOWN) {
1960 unsigned long flags;
1961
1962 uart_report_port(drv, port);
1963
1964 /*
1965 * Ensure that the modem control lines are de-activated.
1966 * We probably don't need a spinlock around this, but
1967 */
1968 spin_lock_irqsave(&port->lock, flags);
1969 port->ops->set_mctrl(port, 0);
1970 spin_unlock_irqrestore(&port->lock, flags);
1971
1972 /*
1973 * Power down all ports by default, except the
1974 * console if we have one.
1975 */
1976 if (!uart_console(port))
1977 uart_change_pm(state, 3);
1978 }
1979}
1980
1981/*
1982 * This reverses the effects of uart_configure_port, hanging up the
1983 * port before removal.
1984 */
1985static void
1986uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
1987{
1988 struct uart_port *port = state->port;
1989 struct uart_info *info = state->info;
1990
1991 if (info && info->tty)
1992 tty_vhangup(info->tty);
1993
1994 down(&state->sem);
1995
1996 state->info = NULL;
1997
1998 /*
1999 * Free the port IO and memory resources, if any.
2000 */
2001 if (port->type != PORT_UNKNOWN)
2002 port->ops->release_port(port);
2003
2004 /*
2005 * Indicate that there isn't a port here anymore.
2006 */
2007 port->type = PORT_UNKNOWN;
2008
2009 /*
2010 * Kill the tasklet, and free resources.
2011 */
2012 if (info) {
2013 tasklet_kill(&info->tlet);
2014 kfree(info);
2015 }
2016
2017 up(&state->sem);
2018}
2019
2020static struct tty_operations uart_ops = {
2021 .open = uart_open,
2022 .close = uart_close,
2023 .write = uart_write,
2024 .put_char = uart_put_char,
2025 .flush_chars = uart_flush_chars,
2026 .write_room = uart_write_room,
2027 .chars_in_buffer= uart_chars_in_buffer,
2028 .flush_buffer = uart_flush_buffer,
2029 .ioctl = uart_ioctl,
2030 .throttle = uart_throttle,
2031 .unthrottle = uart_unthrottle,
2032 .send_xchar = uart_send_xchar,
2033 .set_termios = uart_set_termios,
2034 .stop = uart_stop,
2035 .start = uart_start,
2036 .hangup = uart_hangup,
2037 .break_ctl = uart_break_ctl,
2038 .wait_until_sent= uart_wait_until_sent,
2039#ifdef CONFIG_PROC_FS
2040 .read_proc = uart_read_proc,
2041#endif
2042 .tiocmget = uart_tiocmget,
2043 .tiocmset = uart_tiocmset,
2044};
2045
2046/**
2047 * uart_register_driver - register a driver with the uart core layer
2048 * @drv: low level driver structure
2049 *
2050 * Register a uart driver with the core driver. We in turn register
2051 * with the tty layer, and initialise the core driver per-port state.
2052 *
2053 * We have a proc file in /proc/tty/driver which is named after the
2054 * normal driver.
2055 *
2056 * drv->port should be NULL, and the per-port structures should be
2057 * registered using uart_add_one_port after this call has succeeded.
2058 */
2059int uart_register_driver(struct uart_driver *drv)
2060{
2061 struct tty_driver *normal = NULL;
2062 int i, retval;
2063
2064 BUG_ON(drv->state);
2065
2066 /*
2067 * Maybe we should be using a slab cache for this, especially if
2068 * we have a large number of ports to handle.
2069 */
2070 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2071 retval = -ENOMEM;
2072 if (!drv->state)
2073 goto out;
2074
2075 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2076
2077 normal = alloc_tty_driver(drv->nr);
2078 if (!normal)
2079 goto out;
2080
2081 drv->tty_driver = normal;
2082
2083 normal->owner = drv->owner;
2084 normal->driver_name = drv->driver_name;
2085 normal->devfs_name = drv->devfs_name;
2086 normal->name = drv->dev_name;
2087 normal->major = drv->major;
2088 normal->minor_start = drv->minor;
2089 normal->type = TTY_DRIVER_TYPE_SERIAL;
2090 normal->subtype = SERIAL_TYPE_NORMAL;
2091 normal->init_termios = tty_std_termios;
2092 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2093 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2094 normal->driver_state = drv;
2095 tty_set_operations(normal, &uart_ops);
2096
2097 /*
2098 * Initialise the UART state(s).
2099 */
2100 for (i = 0; i < drv->nr; i++) {
2101 struct uart_state *state = drv->state + i;
2102
2103 state->close_delay = 500; /* .5 seconds */
2104 state->closing_wait = 30000; /* 30 seconds */
2105
2106 init_MUTEX(&state->sem);
2107 }
2108
2109 retval = tty_register_driver(normal);
2110 out:
2111 if (retval < 0) {
2112 put_tty_driver(normal);
2113 kfree(drv->state);
2114 }
2115 return retval;
2116}
2117
2118/**
2119 * uart_unregister_driver - remove a driver from the uart core layer
2120 * @drv: low level driver structure
2121 *
2122 * Remove all references to a driver from the core driver. The low
2123 * level driver must have removed all its ports via the
2124 * uart_remove_one_port() if it registered them with uart_add_one_port().
2125 * (ie, drv->port == NULL)
2126 */
2127void uart_unregister_driver(struct uart_driver *drv)
2128{
2129 struct tty_driver *p = drv->tty_driver;
2130 tty_unregister_driver(p);
2131 put_tty_driver(p);
2132 kfree(drv->state);
2133 drv->tty_driver = NULL;
2134}
2135
2136struct tty_driver *uart_console_device(struct console *co, int *index)
2137{
2138 struct uart_driver *p = co->data;
2139 *index = co->index;
2140 return p->tty_driver;
2141}
2142
2143/**
2144 * uart_add_one_port - attach a driver-defined port structure
2145 * @drv: pointer to the uart low level driver structure for this port
2146 * @port: uart port structure to use for this port.
2147 *
2148 * This allows the driver to register its own uart_port structure
2149 * with the core driver. The main purpose is to allow the low
2150 * level uart drivers to expand uart_port, rather than having yet
2151 * more levels of structures.
2152 */
2153int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2154{
2155 struct uart_state *state;
2156 int ret = 0;
2157
2158 BUG_ON(in_interrupt());
2159
2160 if (port->line >= drv->nr)
2161 return -EINVAL;
2162
2163 state = drv->state + port->line;
2164
2165 down(&port_sem);
2166 if (state->port) {
2167 ret = -EINVAL;
2168 goto out;
2169 }
2170
2171 state->port = port;
2172
2173 spin_lock_init(&port->lock);
2174 port->cons = drv->cons;
2175 port->info = state->info;
2176
2177 uart_configure_port(drv, state, port);
2178
2179 /*
2180 * Register the port whether it's detected or not. This allows
2181 * setserial to be used to alter this ports parameters.
2182 */
2183 tty_register_device(drv->tty_driver, port->line, port->dev);
2184
2185 /*
2186 * If this driver supports console, and it hasn't been
2187 * successfully registered yet, try to re-register it.
2188 * It may be that the port was not available.
2189 */
2190 if (port->type != PORT_UNKNOWN &&
2191 port->cons && !(port->cons->flags & CON_ENABLED))
2192 register_console(port->cons);
2193
2194 out:
2195 up(&port_sem);
2196
2197 return ret;
2198}
2199
2200/**
2201 * uart_remove_one_port - detach a driver defined port structure
2202 * @drv: pointer to the uart low level driver structure for this port
2203 * @port: uart port structure for this port
2204 *
2205 * This unhooks (and hangs up) the specified port structure from the
2206 * core driver. No further calls will be made to the low-level code
2207 * for this port.
2208 */
2209int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2210{
2211 struct uart_state *state = drv->state + port->line;
2212
2213 BUG_ON(in_interrupt());
2214
2215 if (state->port != port)
2216 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2217 state->port, port);
2218
2219 down(&port_sem);
2220
2221 /*
2222 * Remove the devices from devfs
2223 */
2224 tty_unregister_device(drv->tty_driver, port->line);
2225
2226 uart_unconfigure_port(drv, state);
2227 state->port = NULL;
2228 up(&port_sem);
2229
2230 return 0;
2231}
2232
2233/*
2234 * Are the two ports equivalent?
2235 */
2236int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2237{
2238 if (port1->iotype != port2->iotype)
2239 return 0;
2240
2241 switch (port1->iotype) {
2242 case UPIO_PORT:
2243 return (port1->iobase == port2->iobase);
2244 case UPIO_HUB6:
2245 return (port1->iobase == port2->iobase) &&
2246 (port1->hub6 == port2->hub6);
2247 case UPIO_MEM:
2248 return (port1->membase == port2->membase);
2249 }
2250 return 0;
2251}
2252EXPORT_SYMBOL(uart_match_port);
2253
2254/*
2255 * Try to find an unused uart_state slot for a port.
2256 */
2257static struct uart_state *
2258uart_find_match_or_unused(struct uart_driver *drv, struct uart_port *port)
2259{
2260 int i;
2261
2262 /*
2263 * First, find a port entry which matches. Note: if we do
2264 * find a matching entry, and it has a non-zero use count,
2265 * then we can't register the port.
2266 */
2267 for (i = 0; i < drv->nr; i++)
2268 if (uart_match_port(drv->state[i].port, port))
2269 return &drv->state[i];
2270
2271 /*
2272 * We didn't find a matching entry, so look for the first
2273 * free entry. We look for one which hasn't been previously
2274 * used (indicated by zero iobase).
2275 */
2276 for (i = 0; i < drv->nr; i++)
2277 if (drv->state[i].port->type == PORT_UNKNOWN &&
2278 drv->state[i].port->iobase == 0 &&
2279 drv->state[i].count == 0)
2280 return &drv->state[i];
2281
2282 /*
2283 * That also failed. Last resort is to find any currently
2284 * entry which doesn't have a real port associated with it.
2285 */
2286 for (i = 0; i < drv->nr; i++)
2287 if (drv->state[i].port->type == PORT_UNKNOWN &&
2288 drv->state[i].count == 0)
2289 return &drv->state[i];
2290
2291 return NULL;
2292}
2293
2294/**
2295 * uart_register_port: register uart settings with a port
2296 * @drv: pointer to the uart low level driver structure for this port
2297 * @port: uart port structure describing the port
2298 *
2299 * Register UART settings with the specified low level driver. Detect
2300 * the type of the port if UPF_BOOT_AUTOCONF is set, and detect the
2301 * IRQ if UPF_AUTO_IRQ is set.
2302 *
2303 * We try to pick the same port for the same IO base address, so that
2304 * when a modem is plugged in, unplugged and plugged back in, it gets
2305 * allocated the same port.
2306 *
2307 * Returns negative error, or positive line number.
2308 */
2309int uart_register_port(struct uart_driver *drv, struct uart_port *port)
2310{
2311 struct uart_state *state;
2312 int ret;
2313
2314 down(&port_sem);
2315
2316 state = uart_find_match_or_unused(drv, port);
2317
2318 if (state) {
2319 /*
2320 * Ok, we've found a line that we can use.
2321 *
2322 * If we find a port that matches this one, and it appears
2323 * to be in-use (even if it doesn't have a type) we shouldn't
2324 * alter it underneath itself - the port may be open and
2325 * trying to do useful work.
2326 */
2327 if (uart_users(state) != 0) {
2328 ret = -EBUSY;
2329 goto out;
2330 }
2331
2332 /*
2333 * If the port is already initialised, don't touch it.
2334 */
2335 if (state->port->type == PORT_UNKNOWN) {
2336 state->port->iobase = port->iobase;
2337 state->port->membase = port->membase;
2338 state->port->irq = port->irq;
2339 state->port->uartclk = port->uartclk;
2340 state->port->fifosize = port->fifosize;
2341 state->port->regshift = port->regshift;
2342 state->port->iotype = port->iotype;
2343 state->port->flags = port->flags;
2344 state->port->line = state - drv->state;
2345 state->port->mapbase = port->mapbase;
2346
2347 uart_configure_port(drv, state, state->port);
2348 }
2349
2350 ret = state->port->line;
2351 } else
2352 ret = -ENOSPC;
2353 out:
2354 up(&port_sem);
2355 return ret;
2356}
2357
2358/**
2359 * uart_unregister_port - de-allocate a port
2360 * @drv: pointer to the uart low level driver structure for this port
2361 * @line: line index previously returned from uart_register_port()
2362 *
2363 * Hang up the specified line associated with the low level driver,
2364 * and mark the port as unused.
2365 */
2366void uart_unregister_port(struct uart_driver *drv, int line)
2367{
2368 struct uart_state *state;
2369
2370 if (line < 0 || line >= drv->nr) {
2371 printk(KERN_ERR "Attempt to unregister ");
2372 printk("%s%d", drv->dev_name, line);
2373 printk("\n");
2374 return;
2375 }
2376
2377 state = drv->state + line;
2378
2379 down(&port_sem);
2380 uart_unconfigure_port(drv, state);
2381 up(&port_sem);
2382}
2383
2384EXPORT_SYMBOL(uart_write_wakeup);
2385EXPORT_SYMBOL(uart_register_driver);
2386EXPORT_SYMBOL(uart_unregister_driver);
2387EXPORT_SYMBOL(uart_suspend_port);
2388EXPORT_SYMBOL(uart_resume_port);
2389EXPORT_SYMBOL(uart_register_port);
2390EXPORT_SYMBOL(uart_unregister_port);
2391EXPORT_SYMBOL(uart_add_one_port);
2392EXPORT_SYMBOL(uart_remove_one_port);
2393
2394MODULE_DESCRIPTION("Serial driver core");
2395MODULE_LICENSE("GPL");
diff --git a/drivers/serial/serial_cs.c b/drivers/serial/serial_cs.c
new file mode 100644
index 000000000000..9034f9ad37c7
--- /dev/null
+++ b/drivers/serial/serial_cs.c
@@ -0,0 +1,747 @@
1/*======================================================================
2
3 A driver for PCMCIA serial devices
4
5 serial_cs.c 1.134 2002/05/04 05:48:53
6
7 The contents of this file are subject to the Mozilla Public
8 License Version 1.1 (the "License"); you may not use this file
9 except in compliance with the License. You may obtain a copy of
10 the License at http://www.mozilla.org/MPL/
11
12 Software distributed under the License is distributed on an "AS
13 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 implied. See the License for the specific language governing
15 rights and limitations under the License.
16
17 The initial developer of the original code is David A. Hinds
18 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
19 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
20
21 Alternatively, the contents of this file may be used under the
22 terms of the GNU General Public License version 2 (the "GPL"), in which
23 case the provisions of the GPL are applicable instead of the
24 above. If you wish to allow the use of your version of this file
25 only under the terms of the GPL and not to allow others to use
26 your version of this file under the MPL, indicate your decision
27 by deleting the provisions above and replace them with the notice
28 and other provisions required by the GPL. If you do not delete
29 the provisions above, a recipient may use your version of this
30 file under either the MPL or the GPL.
31
32======================================================================*/
33
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/kernel.h>
37#include <linux/init.h>
38#include <linux/sched.h>
39#include <linux/ptrace.h>
40#include <linux/slab.h>
41#include <linux/string.h>
42#include <linux/timer.h>
43#include <linux/serial_core.h>
44#include <linux/major.h>
45#include <asm/io.h>
46#include <asm/system.h>
47
48#include <pcmcia/version.h>
49#include <pcmcia/cs_types.h>
50#include <pcmcia/cs.h>
51#include <pcmcia/cistpl.h>
52#include <pcmcia/ciscode.h>
53#include <pcmcia/ds.h>
54#include <pcmcia/cisreg.h>
55
56#include "8250.h"
57
58#ifdef PCMCIA_DEBUG
59static int pc_debug = PCMCIA_DEBUG;
60module_param(pc_debug, int, 0644);
61#define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
62static char *version = "serial_cs.c 1.134 2002/05/04 05:48:53 (David Hinds)";
63#else
64#define DEBUG(n, args...)
65#endif
66
67/*====================================================================*/
68
69/* Parameters that can be set with 'insmod' */
70
71/* Enable the speaker? */
72static int do_sound = 1;
73/* Skip strict UART tests? */
74static int buggy_uart;
75
76module_param(do_sound, int, 0444);
77module_param(buggy_uart, int, 0444);
78
79/*====================================================================*/
80
81/* Table of multi-port card ID's */
82
83struct multi_id {
84 u_short manfid;
85 u_short prodid;
86 int multi; /* 1 = multifunction, > 1 = # ports */
87};
88
89static struct multi_id multi_id[] = {
90 { MANFID_OMEGA, PRODID_OMEGA_QSP_100, 4 },
91 { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS232, 2 },
92 { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS232_D1, 2 },
93 { MANFID_QUATECH, PRODID_QUATECH_QUAD_RS232, 4 },
94 { MANFID_SOCKET, PRODID_SOCKET_DUAL_RS232, 2 },
95 { MANFID_INTEL, PRODID_INTEL_DUAL_RS232, 2 },
96 { MANFID_NATINST, PRODID_NATINST_QUAD_RS232, 4 }
97};
98#define MULTI_COUNT (sizeof(multi_id)/sizeof(struct multi_id))
99
100struct serial_info {
101 dev_link_t link;
102 int ndev;
103 int multi;
104 int slave;
105 int manfid;
106 dev_node_t node[4];
107 int line[4];
108};
109
110static void serial_config(dev_link_t * link);
111static int serial_event(event_t event, int priority,
112 event_callback_args_t * args);
113
114static dev_info_t dev_info = "serial_cs";
115
116static dev_link_t *serial_attach(void);
117static void serial_detach(dev_link_t *);
118
119static dev_link_t *dev_list = NULL;
120
121/*======================================================================
122
123 After a card is removed, serial_remove() will unregister
124 the serial device(s), and release the PCMCIA configuration.
125
126======================================================================*/
127
128static void serial_remove(dev_link_t *link)
129{
130 struct serial_info *info = link->priv;
131 int i;
132
133 link->state &= ~DEV_PRESENT;
134
135 DEBUG(0, "serial_release(0x%p)\n", link);
136
137 /*
138 * Recheck to see if the device is still configured.
139 */
140 if (info->link.state & DEV_CONFIG) {
141 for (i = 0; i < info->ndev; i++)
142 serial8250_unregister_port(info->line[i]);
143
144 info->link.dev = NULL;
145
146 if (!info->slave) {
147 pcmcia_release_configuration(info->link.handle);
148 pcmcia_release_io(info->link.handle, &info->link.io);
149 pcmcia_release_irq(info->link.handle, &info->link.irq);
150 }
151
152 info->link.state &= ~DEV_CONFIG;
153 }
154}
155
156static void serial_suspend(dev_link_t *link)
157{
158 link->state |= DEV_SUSPEND;
159
160 if (link->state & DEV_CONFIG) {
161 struct serial_info *info = link->priv;
162 int i;
163
164 for (i = 0; i < info->ndev; i++)
165 serial8250_suspend_port(info->line[i]);
166
167 if (!info->slave)
168 pcmcia_release_configuration(link->handle);
169 }
170}
171
172static void serial_resume(dev_link_t *link)
173{
174 link->state &= ~DEV_SUSPEND;
175
176 if (DEV_OK(link)) {
177 struct serial_info *info = link->priv;
178 int i;
179
180 if (!info->slave)
181 pcmcia_request_configuration(link->handle, &link->conf);
182
183 for (i = 0; i < info->ndev; i++)
184 serial8250_resume_port(info->line[i]);
185 }
186}
187
188/*======================================================================
189
190 serial_attach() creates an "instance" of the driver, allocating
191 local data structures for one device. The device is registered
192 with Card Services.
193
194======================================================================*/
195
196static dev_link_t *serial_attach(void)
197{
198 struct serial_info *info;
199 client_reg_t client_reg;
200 dev_link_t *link;
201 int ret;
202
203 DEBUG(0, "serial_attach()\n");
204
205 /* Create new serial device */
206 info = kmalloc(sizeof (*info), GFP_KERNEL);
207 if (!info)
208 return NULL;
209 memset(info, 0, sizeof (*info));
210 link = &info->link;
211 link->priv = info;
212
213 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
214 link->io.NumPorts1 = 8;
215 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
216 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
217 link->conf.Attributes = CONF_ENABLE_IRQ;
218 if (do_sound) {
219 link->conf.Attributes |= CONF_ENABLE_SPKR;
220 link->conf.Status = CCSR_AUDIO_ENA;
221 }
222 link->conf.IntType = INT_MEMORY_AND_IO;
223
224 /* Register with Card Services */
225 link->next = dev_list;
226 dev_list = link;
227 client_reg.dev_info = &dev_info;
228 client_reg.EventMask =
229 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
230 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
231 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
232 client_reg.event_handler = &serial_event;
233 client_reg.Version = 0x0210;
234 client_reg.event_callback_args.client_data = link;
235 ret = pcmcia_register_client(&link->handle, &client_reg);
236 if (ret != CS_SUCCESS) {
237 cs_error(link->handle, RegisterClient, ret);
238 serial_detach(link);
239 return NULL;
240 }
241
242 return link;
243}
244
245/*======================================================================
246
247 This deletes a driver "instance". The device is de-registered
248 with Card Services. If it has been released, all local data
249 structures are freed. Otherwise, the structures will be freed
250 when the device is released.
251
252======================================================================*/
253
254static void serial_detach(dev_link_t * link)
255{
256 struct serial_info *info = link->priv;
257 dev_link_t **linkp;
258 int ret;
259
260 DEBUG(0, "serial_detach(0x%p)\n", link);
261
262 /* Locate device structure */
263 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
264 if (*linkp == link)
265 break;
266 if (*linkp == NULL)
267 return;
268
269 /*
270 * Ensure any outstanding scheduled tasks are completed.
271 */
272 flush_scheduled_work();
273
274 /*
275 * Ensure that the ports have been released.
276 */
277 serial_remove(link);
278
279 if (link->handle) {
280 ret = pcmcia_deregister_client(link->handle);
281 if (ret != CS_SUCCESS)
282 cs_error(link->handle, DeregisterClient, ret);
283 }
284
285 /* Unlink device structure, free bits */
286 *linkp = link->next;
287 kfree(info);
288}
289
290/*====================================================================*/
291
292static int setup_serial(client_handle_t handle, struct serial_info * info,
293 kio_addr_t iobase, int irq)
294{
295 struct uart_port port;
296 int line;
297
298 memset(&port, 0, sizeof (struct uart_port));
299 port.iobase = iobase;
300 port.irq = irq;
301 port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
302 port.uartclk = 1843200;
303 port.dev = &handle_to_dev(handle);
304 if (buggy_uart)
305 port.flags |= UPF_BUGGY_UART;
306 line = serial8250_register_port(&port);
307 if (line < 0) {
308 printk(KERN_NOTICE "serial_cs: serial8250_register_port() at "
309 "0x%04lx, irq %d failed\n", (u_long)iobase, irq);
310 return -EINVAL;
311 }
312
313 info->line[info->ndev] = line;
314 sprintf(info->node[info->ndev].dev_name, "ttyS%d", line);
315 info->node[info->ndev].major = TTY_MAJOR;
316 info->node[info->ndev].minor = 0x40 + line;
317 if (info->ndev > 0)
318 info->node[info->ndev - 1].next = &info->node[info->ndev];
319 info->ndev++;
320
321 return 0;
322}
323
324/*====================================================================*/
325
326static int
327first_tuple(client_handle_t handle, tuple_t * tuple, cisparse_t * parse)
328{
329 int i;
330 i = pcmcia_get_first_tuple(handle, tuple);
331 if (i != CS_SUCCESS)
332 return CS_NO_MORE_ITEMS;
333 i = pcmcia_get_tuple_data(handle, tuple);
334 if (i != CS_SUCCESS)
335 return i;
336 return pcmcia_parse_tuple(handle, tuple, parse);
337}
338
339static int
340next_tuple(client_handle_t handle, tuple_t * tuple, cisparse_t * parse)
341{
342 int i;
343 i = pcmcia_get_next_tuple(handle, tuple);
344 if (i != CS_SUCCESS)
345 return CS_NO_MORE_ITEMS;
346 i = pcmcia_get_tuple_data(handle, tuple);
347 if (i != CS_SUCCESS)
348 return i;
349 return pcmcia_parse_tuple(handle, tuple, parse);
350}
351
352/*====================================================================*/
353
354static int simple_config(dev_link_t *link)
355{
356 static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
357 static int size_table[2] = { 8, 16 };
358 client_handle_t handle = link->handle;
359 struct serial_info *info = link->priv;
360 tuple_t tuple;
361 u_char buf[256];
362 cisparse_t parse;
363 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
364 config_info_t config;
365 int i, j, try;
366 int s;
367
368 /* If the card is already configured, look up the port and irq */
369 i = pcmcia_get_configuration_info(handle, &config);
370 if ((i == CS_SUCCESS) && (config.Attributes & CONF_VALID_CLIENT)) {
371 kio_addr_t port = 0;
372 if ((config.BasePort2 != 0) && (config.NumPorts2 == 8)) {
373 port = config.BasePort2;
374 info->slave = 1;
375 } else if ((info->manfid == MANFID_OSITECH) &&
376 (config.NumPorts1 == 0x40)) {
377 port = config.BasePort1 + 0x28;
378 info->slave = 1;
379 }
380 if (info->slave)
381 return setup_serial(handle, info, port, config.AssignedIRQ);
382 }
383 link->conf.Vcc = config.Vcc;
384
385 /* First pass: look for a config entry that looks normal. */
386 tuple.TupleData = (cisdata_t *) buf;
387 tuple.TupleOffset = 0;
388 tuple.TupleDataMax = 255;
389 tuple.Attributes = 0;
390 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
391 /* Two tries: without IO aliases, then with aliases */
392 for (s = 0; s < 2; s++) {
393 for (try = 0; try < 2; try++) {
394 i = first_tuple(handle, &tuple, &parse);
395 while (i != CS_NO_MORE_ITEMS) {
396 if (i != CS_SUCCESS)
397 goto next_entry;
398 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
399 link->conf.Vpp1 = link->conf.Vpp2 =
400 cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
401 if ((cf->io.nwin > 0) && (cf->io.win[0].len == size_table[s]) &&
402 (cf->io.win[0].base != 0)) {
403 link->conf.ConfigIndex = cf->index;
404 link->io.BasePort1 = cf->io.win[0].base;
405 link->io.IOAddrLines = (try == 0) ?
406 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
407 i = pcmcia_request_io(link->handle, &link->io);
408 if (i == CS_SUCCESS)
409 goto found_port;
410 }
411next_entry:
412 i = next_tuple(handle, &tuple, &parse);
413 }
414 }
415 }
416 /* Second pass: try to find an entry that isn't picky about
417 its base address, then try to grab any standard serial port
418 address, and finally try to get any free port. */
419 i = first_tuple(handle, &tuple, &parse);
420 while (i != CS_NO_MORE_ITEMS) {
421 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) &&
422 ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
423 link->conf.ConfigIndex = cf->index;
424 for (j = 0; j < 5; j++) {
425 link->io.BasePort1 = base[j];
426 link->io.IOAddrLines = base[j] ? 16 : 3;
427 i = pcmcia_request_io(link->handle, &link->io);
428 if (i == CS_SUCCESS)
429 goto found_port;
430 }
431 }
432 i = next_tuple(handle, &tuple, &parse);
433 }
434
435 found_port:
436 if (i != CS_SUCCESS) {
437 printk(KERN_NOTICE
438 "serial_cs: no usable port range found, giving up\n");
439 cs_error(link->handle, RequestIO, i);
440 return -1;
441 }
442
443 i = pcmcia_request_irq(link->handle, &link->irq);
444 if (i != CS_SUCCESS) {
445 cs_error(link->handle, RequestIRQ, i);
446 link->irq.AssignedIRQ = 0;
447 }
448 if (info->multi && (info->manfid == MANFID_3COM))
449 link->conf.ConfigIndex &= ~(0x08);
450 i = pcmcia_request_configuration(link->handle, &link->conf);
451 if (i != CS_SUCCESS) {
452 cs_error(link->handle, RequestConfiguration, i);
453 return -1;
454 }
455
456 return setup_serial(handle, info, link->io.BasePort1, link->irq.AssignedIRQ);
457}
458
459static int multi_config(dev_link_t * link)
460{
461 client_handle_t handle = link->handle;
462 struct serial_info *info = link->priv;
463 tuple_t tuple;
464 u_char buf[256];
465 cisparse_t parse;
466 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
467 config_info_t config;
468 int i, base2 = 0;
469
470 i = pcmcia_get_configuration_info(handle, &config);
471 if (i != CS_SUCCESS) {
472 cs_error(handle, GetConfigurationInfo, i);
473 return -1;
474 }
475 link->conf.Vcc = config.Vcc;
476
477 tuple.TupleData = (cisdata_t *) buf;
478 tuple.TupleOffset = 0;
479 tuple.TupleDataMax = 255;
480 tuple.Attributes = 0;
481 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
482
483 /* First, look for a generic full-sized window */
484 link->io.NumPorts1 = info->multi * 8;
485 i = first_tuple(handle, &tuple, &parse);
486 while (i != CS_NO_MORE_ITEMS) {
487 /* The quad port cards have bad CIS's, so just look for a
488 window larger than 8 ports and assume it will be right */
489 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) &&
490 (cf->io.win[0].len > 8)) {
491 link->conf.ConfigIndex = cf->index;
492 link->io.BasePort1 = cf->io.win[0].base;
493 link->io.IOAddrLines =
494 cf->io.flags & CISTPL_IO_LINES_MASK;
495 i = pcmcia_request_io(link->handle, &link->io);
496 base2 = link->io.BasePort1 + 8;
497 if (i == CS_SUCCESS)
498 break;
499 }
500 i = next_tuple(handle, &tuple, &parse);
501 }
502
503 /* If that didn't work, look for two windows */
504 if (i != CS_SUCCESS) {
505 link->io.NumPorts1 = link->io.NumPorts2 = 8;
506 info->multi = 2;
507 i = first_tuple(handle, &tuple, &parse);
508 while (i != CS_NO_MORE_ITEMS) {
509 if ((i == CS_SUCCESS) && (cf->io.nwin == 2)) {
510 link->conf.ConfigIndex = cf->index;
511 link->io.BasePort1 = cf->io.win[0].base;
512 link->io.BasePort2 = cf->io.win[1].base;
513 link->io.IOAddrLines =
514 cf->io.flags & CISTPL_IO_LINES_MASK;
515 i = pcmcia_request_io(link->handle, &link->io);
516 base2 = link->io.BasePort2;
517 if (i == CS_SUCCESS)
518 break;
519 }
520 i = next_tuple(handle, &tuple, &parse);
521 }
522 }
523
524 if (i != CS_SUCCESS) {
525 cs_error(link->handle, RequestIO, i);
526 return -1;
527 }
528
529 i = pcmcia_request_irq(link->handle, &link->irq);
530 if (i != CS_SUCCESS) {
531 printk(KERN_NOTICE
532 "serial_cs: no usable port range found, giving up\n");
533 cs_error(link->handle, RequestIRQ, i);
534 link->irq.AssignedIRQ = 0;
535 }
536 /* Socket Dual IO: this enables irq's for second port */
537 if (info->multi && (info->manfid == MANFID_SOCKET)) {
538 link->conf.Present |= PRESENT_EXT_STATUS;
539 link->conf.ExtStatus = ESR_REQ_ATTN_ENA;
540 }
541 i = pcmcia_request_configuration(link->handle, &link->conf);
542 if (i != CS_SUCCESS) {
543 cs_error(link->handle, RequestConfiguration, i);
544 return -1;
545 }
546
547 /* The Oxford Semiconductor OXCF950 cards are in fact single-port:
548 8 registers are for the UART, the others are extra registers */
549 if (info->manfid == MANFID_OXSEMI) {
550 if (cf->index == 1 || cf->index == 3) {
551 setup_serial(handle, info, base2, link->irq.AssignedIRQ);
552 outb(12, link->io.BasePort1 + 1);
553 } else {
554 setup_serial(handle, info, link->io.BasePort1, link->irq.AssignedIRQ);
555 outb(12, base2 + 1);
556 }
557 return 0;
558 }
559
560 setup_serial(handle, info, link->io.BasePort1, link->irq.AssignedIRQ);
561 /* The Nokia cards are not really multiport cards */
562 if (info->manfid == MANFID_NOKIA)
563 return 0;
564 for (i = 0; i < info->multi - 1; i++)
565 setup_serial(handle, info, base2 + (8 * i), link->irq.AssignedIRQ);
566
567 return 0;
568}
569
570/*======================================================================
571
572 serial_config() is scheduled to run after a CARD_INSERTION event
573 is received, to configure the PCMCIA socket, and to make the
574 serial device available to the system.
575
576======================================================================*/
577
578void serial_config(dev_link_t * link)
579{
580 client_handle_t handle = link->handle;
581 struct serial_info *info = link->priv;
582 tuple_t tuple;
583 u_short buf[128];
584 cisparse_t parse;
585 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
586 int i, last_ret, last_fn;
587
588 DEBUG(0, "serial_config(0x%p)\n", link);
589
590 tuple.TupleData = (cisdata_t *) buf;
591 tuple.TupleOffset = 0;
592 tuple.TupleDataMax = 255;
593 tuple.Attributes = 0;
594 /* Get configuration register information */
595 tuple.DesiredTuple = CISTPL_CONFIG;
596 last_ret = first_tuple(handle, &tuple, &parse);
597 if (last_ret != CS_SUCCESS) {
598 last_fn = ParseTuple;
599 goto cs_failed;
600 }
601 link->conf.ConfigBase = parse.config.base;
602 link->conf.Present = parse.config.rmask[0];
603
604 /* Configure card */
605 link->state |= DEV_CONFIG;
606
607 /* Is this a compliant multifunction card? */
608 tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
609 tuple.Attributes = TUPLE_RETURN_COMMON | TUPLE_RETURN_LINK;
610 info->multi = (first_tuple(handle, &tuple, &parse) == CS_SUCCESS);
611
612 /* Is this a multiport card? */
613 tuple.DesiredTuple = CISTPL_MANFID;
614 if (first_tuple(handle, &tuple, &parse) == CS_SUCCESS) {
615 info->manfid = le16_to_cpu(buf[0]);
616 for (i = 0; i < MULTI_COUNT; i++)
617 if ((info->manfid == multi_id[i].manfid) &&
618 (le16_to_cpu(buf[1]) == multi_id[i].prodid))
619 break;
620 if (i < MULTI_COUNT)
621 info->multi = multi_id[i].multi;
622 }
623
624 /* Another check for dual-serial cards: look for either serial or
625 multifunction cards that ask for appropriate IO port ranges */
626 tuple.DesiredTuple = CISTPL_FUNCID;
627 if ((info->multi == 0) &&
628 ((first_tuple(handle, &tuple, &parse) != CS_SUCCESS) ||
629 (parse.funcid.func == CISTPL_FUNCID_MULTI) ||
630 (parse.funcid.func == CISTPL_FUNCID_SERIAL))) {
631 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
632 if (first_tuple(handle, &tuple, &parse) == CS_SUCCESS) {
633 if ((cf->io.nwin == 1) && (cf->io.win[0].len % 8 == 0))
634 info->multi = cf->io.win[0].len >> 3;
635 if ((cf->io.nwin == 2) && (cf->io.win[0].len == 8) &&
636 (cf->io.win[1].len == 8))
637 info->multi = 2;
638 }
639 }
640
641 if (info->multi > 1)
642 multi_config(link);
643 else
644 simple_config(link);
645
646 if (info->ndev == 0)
647 goto failed;
648
649 if (info->manfid == MANFID_IBM) {
650 conf_reg_t reg = { 0, CS_READ, 0x800, 0 };
651 last_ret = pcmcia_access_configuration_register(link->handle, &reg);
652 if (last_ret) {
653 last_fn = AccessConfigurationRegister;
654 goto cs_failed;
655 }
656 reg.Action = CS_WRITE;
657 reg.Value = reg.Value | 1;
658 last_ret = pcmcia_access_configuration_register(link->handle, &reg);
659 if (last_ret) {
660 last_fn = AccessConfigurationRegister;
661 goto cs_failed;
662 }
663 }
664
665 link->dev = &info->node[0];
666 link->state &= ~DEV_CONFIG_PENDING;
667 return;
668
669 cs_failed:
670 cs_error(link->handle, last_fn, last_ret);
671 failed:
672 serial_remove(link);
673 link->state &= ~DEV_CONFIG_PENDING;
674}
675
676/*======================================================================
677
678 The card status event handler. Mostly, this schedules other
679 stuff to run after an event is received. A CARD_REMOVAL event
680 also sets some flags to discourage the serial drivers from
681 talking to the ports.
682
683======================================================================*/
684
685static int
686serial_event(event_t event, int priority, event_callback_args_t * args)
687{
688 dev_link_t *link = args->client_data;
689 struct serial_info *info = link->priv;
690
691 DEBUG(1, "serial_event(0x%06x)\n", event);
692
693 switch (event) {
694 case CS_EVENT_CARD_REMOVAL:
695 serial_remove(link);
696 break;
697
698 case CS_EVENT_CARD_INSERTION:
699 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
700 serial_config(link);
701 break;
702
703 case CS_EVENT_PM_SUSPEND:
704 serial_suspend(link);
705 break;
706
707 case CS_EVENT_RESET_PHYSICAL:
708 if ((link->state & DEV_CONFIG) && !info->slave)
709 pcmcia_release_configuration(link->handle);
710 break;
711
712 case CS_EVENT_PM_RESUME:
713 serial_resume(link);
714 break;
715
716 case CS_EVENT_CARD_RESET:
717 if (DEV_OK(link) && !info->slave)
718 pcmcia_request_configuration(link->handle, &link->conf);
719 break;
720 }
721 return 0;
722}
723
724static struct pcmcia_driver serial_cs_driver = {
725 .owner = THIS_MODULE,
726 .drv = {
727 .name = "serial_cs",
728 },
729 .attach = serial_attach,
730 .detach = serial_detach,
731};
732
733static int __init init_serial_cs(void)
734{
735 return pcmcia_register_driver(&serial_cs_driver);
736}
737
738static void __exit exit_serial_cs(void)
739{
740 pcmcia_unregister_driver(&serial_cs_driver);
741 BUG_ON(dev_list != NULL);
742}
743
744module_init(init_serial_cs);
745module_exit(exit_serial_cs);
746
747MODULE_LICENSE("GPL");
diff --git a/drivers/serial/serial_lh7a40x.c b/drivers/serial/serial_lh7a40x.c
new file mode 100644
index 000000000000..4ce3a41f1611
--- /dev/null
+++ b/drivers/serial/serial_lh7a40x.c
@@ -0,0 +1,711 @@
1/* drivers/serial/serial_lh7a40x.c
2 *
3 * Copyright (C) 2004 Coastal Environmental Systems
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 */
10
11/* Driver for Sharp LH7A40X embedded serial ports
12 *
13 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
14 * Based on drivers/serial/amba.c, by Deep Blue Solutions Ltd.
15 *
16 * ---
17 *
18 * This driver supports the embedded UARTs of the Sharp LH7A40X series
19 * CPUs. While similar to the 16550 and other UART chips, there is
20 * nothing close to register compatibility. Moreover, some of the
21 * modem control lines are not available, either in the chip or they
22 * are lacking in the board-level implementation.
23 *
24 * - Use of SIRDIS
25 * For simplicity, we disable the IR functions of any UART whenever
26 * we enable it.
27 *
28 */
29
30#include <linux/config.h>
31
32#if defined(CONFIG_SERIAL_LH7A40X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
33#define SUPPORT_SYSRQ
34#endif
35
36#include <linux/module.h>
37#include <linux/ioport.h>
38#include <linux/init.h>
39#include <linux/console.h>
40#include <linux/sysrq.h>
41#include <linux/tty.h>
42#include <linux/tty_flip.h>
43#include <linux/serial_core.h>
44#include <linux/serial.h>
45
46#include <asm/io.h>
47#include <asm/irq.h>
48
49#define DEV_MAJOR 204
50#define DEV_MINOR 16
51#define DEV_NR 3
52
53#define ISR_LOOP_LIMIT 256
54
55#define UR(p,o) _UR ((p)->membase, o)
56#define _UR(b,o) (*((volatile unsigned int*)(((unsigned char*) b) + (o))))
57#define BIT_CLR(p,o,m) UR(p,o) = UR(p,o) & (~(unsigned int)m)
58#define BIT_SET(p,o,m) UR(p,o) = UR(p,o) | ( (unsigned int)m)
59
60#define UART_REG_SIZE 32
61
62#define UART_R_DATA (0x00)
63#define UART_R_FCON (0x04)
64#define UART_R_BRCON (0x08)
65#define UART_R_CON (0x0c)
66#define UART_R_STATUS (0x10)
67#define UART_R_RAWISR (0x14)
68#define UART_R_INTEN (0x18)
69#define UART_R_ISR (0x1c)
70
71#define UARTEN (0x01) /* UART enable */
72#define SIRDIS (0x02) /* Serial IR disable (UART1 only) */
73
74#define RxEmpty (0x10)
75#define TxEmpty (0x80)
76#define TxFull (0x20)
77#define nRxRdy RxEmpty
78#define nTxRdy TxFull
79#define TxBusy (0x08)
80
81#define RxBreak (0x0800)
82#define RxOverrunError (0x0400)
83#define RxParityError (0x0200)
84#define RxFramingError (0x0100)
85#define RxError (RxBreak | RxOverrunError | RxParityError | RxFramingError)
86
87#define DCD (0x04)
88#define DSR (0x02)
89#define CTS (0x01)
90
91#define RxInt (0x01)
92#define TxInt (0x02)
93#define ModemInt (0x04)
94#define RxTimeoutInt (0x08)
95
96#define MSEOI (0x10)
97
98#define WLEN_8 (0x60)
99#define WLEN_7 (0x40)
100#define WLEN_6 (0x20)
101#define WLEN_5 (0x00)
102#define WLEN (0x60) /* Mask for all word-length bits */
103#define STP2 (0x08)
104#define PEN (0x02) /* Parity Enable */
105#define EPS (0x04) /* Even Parity Set */
106#define FEN (0x10) /* FIFO Enable */
107#define BRK (0x01) /* Send Break */
108
109
110struct uart_port_lh7a40x {
111 struct uart_port port;
112 unsigned int statusPrev; /* Most recently read modem status */
113};
114
115static void lh7a40xuart_stop_tx (struct uart_port* port, unsigned int tty_stop)
116{
117 BIT_CLR (port, UART_R_INTEN, TxInt);
118}
119
120static void lh7a40xuart_start_tx (struct uart_port* port,
121 unsigned int tty_start)
122{
123 BIT_SET (port, UART_R_INTEN, TxInt);
124
125 /* *** FIXME: do I need to check for startup of the
126 transmitter? The old driver did, but AMBA
127 doesn't . */
128}
129
130static void lh7a40xuart_stop_rx (struct uart_port* port)
131{
132 BIT_SET (port, UART_R_INTEN, RxTimeoutInt | RxInt);
133}
134
135static void lh7a40xuart_enable_ms (struct uart_port* port)
136{
137 BIT_SET (port, UART_R_INTEN, ModemInt);
138}
139
140static void
141#ifdef SUPPORT_SYSRQ
142lh7a40xuart_rx_chars (struct uart_port* port, struct pt_regs* regs)
143#else
144lh7a40xuart_rx_chars (struct uart_port* port)
145#endif
146{
147 struct tty_struct* tty = port->info->tty;
148 int cbRxMax = 256; /* (Gross) limit on receive */
149 unsigned int data, flag;/* Received data and status */
150
151 while (!(UR (port, UART_R_STATUS) & nRxRdy) && --cbRxMax) {
152 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
153 if (tty->low_latency)
154 tty_flip_buffer_push(tty);
155 /*
156 * If this failed then we will throw away the
157 * bytes but must do so to clear interrupts
158 */
159 }
160
161 data = UR (port, UART_R_DATA);
162 flag = TTY_NORMAL;
163 ++port->icount.rx;
164
165 if (data & RxError) { /* Quick check, short-circuit */
166 if (data & RxBreak) {
167 data &= ~(RxFramingError | RxParityError);
168 ++port->icount.brk;
169 if (uart_handle_break (port))
170 continue;
171 }
172 else if (data & RxParityError)
173 ++port->icount.parity;
174 else if (data & RxFramingError)
175 ++port->icount.frame;
176 if (data & RxOverrunError)
177 ++port->icount.overrun;
178
179 /* Mask by termios, leave Rx'd byte */
180 data &= port->read_status_mask | 0xff;
181
182 if (data & RxBreak)
183 flag = TTY_BREAK;
184 else if (data & RxParityError)
185 flag = TTY_PARITY;
186 else if (data & RxFramingError)
187 flag = TTY_FRAME;
188 }
189
190 if (uart_handle_sysrq_char (port, (unsigned char) data, regs))
191 continue;
192
193 if ((data & port->ignore_status_mask) == 0) {
194 tty_insert_flip_char(tty, data, flag);
195 }
196 if ((data & RxOverrunError)
197 && tty->flip.count < TTY_FLIPBUF_SIZE) {
198 /*
199 * Overrun is special, since it's reported
200 * immediately, and doesn't affect the current
201 * character
202 */
203 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
204 }
205 }
206 tty_flip_buffer_push (tty);
207 return;
208}
209
210static void lh7a40xuart_tx_chars (struct uart_port* port)
211{
212 struct circ_buf* xmit = &port->info->xmit;
213 int cbTxMax = port->fifosize;
214
215 if (port->x_char) {
216 UR (port, UART_R_DATA) = port->x_char;
217 ++port->icount.tx;
218 port->x_char = 0;
219 return;
220 }
221 if (uart_circ_empty (xmit) || uart_tx_stopped (port)) {
222 lh7a40xuart_stop_tx (port, 0);
223 return;
224 }
225
226 /* Unlike the AMBA UART, the lh7a40x UART does not guarantee
227 that at least half of the FIFO is empty. Instead, we check
228 status for every character. Using the AMBA method causes
229 the transmitter to drop characters. */
230
231 do {
232 UR (port, UART_R_DATA) = xmit->buf[xmit->tail];
233 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
234 ++port->icount.tx;
235 if (uart_circ_empty(xmit))
236 break;
237 } while (!(UR (port, UART_R_STATUS) & nTxRdy)
238 && cbTxMax--);
239
240 if (uart_circ_chars_pending (xmit) < WAKEUP_CHARS)
241 uart_write_wakeup (port);
242
243 if (uart_circ_empty (xmit))
244 lh7a40xuart_stop_tx (port, 0);
245}
246
247static void lh7a40xuart_modem_status (struct uart_port* port)
248{
249 unsigned int status = UR (port, UART_R_STATUS);
250 unsigned int delta
251 = status ^ ((struct uart_port_lh7a40x*) port)->statusPrev;
252
253 BIT_SET (port, UART_R_RAWISR, MSEOI); /* Clear modem status intr */
254
255 if (!delta) /* Only happens if we missed 2 transitions */
256 return;
257
258 ((struct uart_port_lh7a40x*) port)->statusPrev = status;
259
260 if (delta & DCD)
261 uart_handle_dcd_change (port, status & DCD);
262
263 if (delta & DSR)
264 ++port->icount.dsr;
265
266 if (delta & CTS)
267 uart_handle_cts_change (port, status & CTS);
268
269 wake_up_interruptible (&port->info->delta_msr_wait);
270}
271
272static irqreturn_t lh7a40xuart_int (int irq, void* dev_id,
273 struct pt_regs* regs)
274{
275 struct uart_port* port = dev_id;
276 unsigned int cLoopLimit = ISR_LOOP_LIMIT;
277 unsigned int isr = UR (port, UART_R_ISR);
278
279
280 do {
281 if (isr & (RxInt | RxTimeoutInt))
282#ifdef SUPPORT_SYSRQ
283 lh7a40xuart_rx_chars(port, regs);
284#else
285 lh7a40xuart_rx_chars(port);
286#endif
287 if (isr & ModemInt)
288 lh7a40xuart_modem_status (port);
289 if (isr & TxInt)
290 lh7a40xuart_tx_chars (port);
291
292 if (--cLoopLimit == 0)
293 break;
294
295 isr = UR (port, UART_R_ISR);
296 } while (isr & (RxInt | TxInt | RxTimeoutInt));
297
298 return IRQ_HANDLED;
299}
300
301static unsigned int lh7a40xuart_tx_empty (struct uart_port* port)
302{
303 return (UR (port, UART_R_STATUS) & TxEmpty) ? TIOCSER_TEMT : 0;
304}
305
306static unsigned int lh7a40xuart_get_mctrl (struct uart_port* port)
307{
308 unsigned int result = 0;
309 unsigned int status = UR (port, UART_R_STATUS);
310
311 if (status & DCD)
312 result |= TIOCM_CAR;
313 if (status & DSR)
314 result |= TIOCM_DSR;
315 if (status & CTS)
316 result |= TIOCM_CTS;
317
318 return result;
319}
320
321static void lh7a40xuart_set_mctrl (struct uart_port* port, unsigned int mctrl)
322{
323 /* None of the ports supports DTR. UART1 supports RTS through GPIO. */
324 /* Note, kernel appears to be setting DTR and RTS on console. */
325
326 /* *** FIXME: this deserves more work. There's some work in
327 tracing all of the IO pins. */
328#if 0
329 if( port->mapbase == UART1_PHYS) {
330 gpioRegs_t *gpio = (gpioRegs_t *)IO_ADDRESS(GPIO_PHYS);
331
332 if (mctrl & TIOCM_RTS)
333 gpio->pbdr &= ~GPIOB_UART1_RTS;
334 else
335 gpio->pbdr |= GPIOB_UART1_RTS;
336 }
337#endif
338}
339
340static void lh7a40xuart_break_ctl (struct uart_port* port, int break_state)
341{
342 unsigned long flags;
343
344 spin_lock_irqsave(&port->lock, flags);
345 if (break_state == -1)
346 BIT_SET (port, UART_R_FCON, BRK); /* Assert break */
347 else
348 BIT_CLR (port, UART_R_FCON, BRK); /* Deassert break */
349 spin_unlock_irqrestore(&port->lock, flags);
350}
351
352static int lh7a40xuart_startup (struct uart_port* port)
353{
354 int retval;
355
356 retval = request_irq (port->irq, lh7a40xuart_int, 0,
357 "serial_lh7a40x", port);
358 if (retval)
359 return retval;
360
361 /* Initial modem control-line settings */
362 ((struct uart_port_lh7a40x*) port)->statusPrev
363 = UR (port, UART_R_STATUS);
364
365 /* There is presently no configuration option to enable IR.
366 Thus, we always disable it. */
367
368 BIT_SET (port, UART_R_CON, UARTEN | SIRDIS);
369 BIT_SET (port, UART_R_INTEN, RxTimeoutInt | RxInt);
370
371 return 0;
372}
373
374static void lh7a40xuart_shutdown (struct uart_port* port)
375{
376 free_irq (port->irq, port);
377 BIT_CLR (port, UART_R_FCON, BRK | FEN);
378 BIT_CLR (port, UART_R_CON, UARTEN);
379}
380
381static void lh7a40xuart_set_termios (struct uart_port* port,
382 struct termios* termios,
383 struct termios* old)
384{
385 unsigned int con;
386 unsigned int inten;
387 unsigned int fcon;
388 unsigned long flags;
389 unsigned int baud;
390 unsigned int quot;
391
392 baud = uart_get_baud_rate (port, termios, old, 8, port->uartclk/16);
393 quot = uart_get_divisor (port, baud); /* -1 performed elsewhere */
394
395 switch (termios->c_cflag & CSIZE) {
396 case CS5:
397 fcon = WLEN_5;
398 break;
399 case CS6:
400 fcon = WLEN_6;
401 break;
402 case CS7:
403 fcon = WLEN_7;
404 break;
405 case CS8:
406 default:
407 fcon = WLEN_8;
408 break;
409 }
410 if (termios->c_cflag & CSTOPB)
411 fcon |= STP2;
412 if (termios->c_cflag & PARENB) {
413 fcon |= PEN;
414 if (!(termios->c_cflag & PARODD))
415 fcon |= EPS;
416 }
417 if (port->fifosize > 1)
418 fcon |= FEN;
419
420 spin_lock_irqsave (&port->lock, flags);
421
422 uart_update_timeout (port, termios->c_cflag, baud);
423
424 port->read_status_mask = RxOverrunError;
425 if (termios->c_iflag & INPCK)
426 port->read_status_mask |= RxFramingError | RxParityError;
427 if (termios->c_iflag & (BRKINT | PARMRK))
428 port->read_status_mask |= RxBreak;
429
430 /* Figure mask for status we ignore */
431 port->ignore_status_mask = 0;
432 if (termios->c_iflag & IGNPAR)
433 port->ignore_status_mask |= RxFramingError | RxParityError;
434 if (termios->c_iflag & IGNBRK) {
435 port->ignore_status_mask |= RxBreak;
436 /* Ignore overrun when ignorning parity */
437 /* *** FIXME: is this in the right place? */
438 if (termios->c_iflag & IGNPAR)
439 port->ignore_status_mask |= RxOverrunError;
440 }
441
442 /* Ignore all receive errors when receive disabled */
443 if ((termios->c_cflag & CREAD) == 0)
444 port->ignore_status_mask |= RxError;
445
446 con = UR (port, UART_R_CON);
447 inten = (UR (port, UART_R_INTEN) & ~ModemInt);
448
449 if (UART_ENABLE_MS (port, termios->c_cflag))
450 inten |= ModemInt;
451
452 BIT_CLR (port, UART_R_CON, UARTEN); /* Disable UART */
453 UR (port, UART_R_INTEN) = 0; /* Disable interrupts */
454 UR (port, UART_R_BRCON) = quot - 1; /* Set baud rate divisor */
455 UR (port, UART_R_FCON) = fcon; /* Set FIFO and frame ctrl */
456 UR (port, UART_R_INTEN) = inten; /* Enable interrupts */
457 UR (port, UART_R_CON) = con; /* Restore UART mode */
458
459 spin_unlock_irqrestore(&port->lock, flags);
460}
461
462static const char* lh7a40xuart_type (struct uart_port* port)
463{
464 return port->type == PORT_LH7A40X ? "LH7A40X" : NULL;
465}
466
467static void lh7a40xuart_release_port (struct uart_port* port)
468{
469 release_mem_region (port->mapbase, UART_REG_SIZE);
470}
471
472static int lh7a40xuart_request_port (struct uart_port* port)
473{
474 return request_mem_region (port->mapbase, UART_REG_SIZE,
475 "serial_lh7a40x") != NULL
476 ? 0 : -EBUSY;
477}
478
479static void lh7a40xuart_config_port (struct uart_port* port, int flags)
480{
481 if (flags & UART_CONFIG_TYPE) {
482 port->type = PORT_LH7A40X;
483 lh7a40xuart_request_port (port);
484 }
485}
486
487static int lh7a40xuart_verify_port (struct uart_port* port,
488 struct serial_struct* ser)
489{
490 int ret = 0;
491
492 if (ser->type != PORT_UNKNOWN && ser->type != PORT_LH7A40X)
493 ret = -EINVAL;
494 if (ser->irq < 0 || ser->irq >= NR_IRQS)
495 ret = -EINVAL;
496 if (ser->baud_base < 9600) /* *** FIXME: is this true? */
497 ret = -EINVAL;
498 return ret;
499}
500
501static struct uart_ops lh7a40x_uart_ops = {
502 .tx_empty = lh7a40xuart_tx_empty,
503 .set_mctrl = lh7a40xuart_set_mctrl,
504 .get_mctrl = lh7a40xuart_get_mctrl,
505 .stop_tx = lh7a40xuart_stop_tx,
506 .start_tx = lh7a40xuart_start_tx,
507 .stop_rx = lh7a40xuart_stop_rx,
508 .enable_ms = lh7a40xuart_enable_ms,
509 .break_ctl = lh7a40xuart_break_ctl,
510 .startup = lh7a40xuart_startup,
511 .shutdown = lh7a40xuart_shutdown,
512 .set_termios = lh7a40xuart_set_termios,
513 .type = lh7a40xuart_type,
514 .release_port = lh7a40xuart_release_port,
515 .request_port = lh7a40xuart_request_port,
516 .config_port = lh7a40xuart_config_port,
517 .verify_port = lh7a40xuart_verify_port,
518};
519
520static struct uart_port_lh7a40x lh7a40x_ports[DEV_NR] = {
521 {
522 .port = {
523 .membase = (void*) io_p2v (UART1_PHYS),
524 .mapbase = UART1_PHYS,
525 .iotype = SERIAL_IO_MEM,
526 .irq = IRQ_UART1INTR,
527 .uartclk = 14745600/2,
528 .fifosize = 16,
529 .ops = &lh7a40x_uart_ops,
530 .flags = ASYNC_BOOT_AUTOCONF,
531 .line = 0,
532 },
533 },
534 {
535 .port = {
536 .membase = (void*) io_p2v (UART2_PHYS),
537 .mapbase = UART2_PHYS,
538 .iotype = SERIAL_IO_MEM,
539 .irq = IRQ_UART2INTR,
540 .uartclk = 14745600/2,
541 .fifosize = 16,
542 .ops = &lh7a40x_uart_ops,
543 .flags = ASYNC_BOOT_AUTOCONF,
544 .line = 1,
545 },
546 },
547 {
548 .port = {
549 .membase = (void*) io_p2v (UART3_PHYS),
550 .mapbase = UART3_PHYS,
551 .iotype = SERIAL_IO_MEM,
552 .irq = IRQ_UART3INTR,
553 .uartclk = 14745600/2,
554 .fifosize = 16,
555 .ops = &lh7a40x_uart_ops,
556 .flags = ASYNC_BOOT_AUTOCONF,
557 .line = 2,
558 },
559 },
560};
561
562#ifndef CONFIG_SERIAL_LH7A40X_CONSOLE
563# define LH7A40X_CONSOLE NULL
564#else
565# define LH7A40X_CONSOLE &lh7a40x_console
566
567
568static void lh7a40xuart_console_write (struct console* co,
569 const char* s,
570 unsigned int count)
571{
572 struct uart_port* port = &lh7a40x_ports[co->index].port;
573 unsigned int con = UR (port, UART_R_CON);
574 unsigned int inten = UR (port, UART_R_INTEN);
575
576
577 UR (port, UART_R_INTEN) = 0; /* Disable all interrupts */
578 BIT_SET (port, UART_R_CON, UARTEN | SIRDIS); /* Enable UART */
579
580 for (; count-- > 0; ++s) {
581 while (UR (port, UART_R_STATUS) & nTxRdy)
582 ;
583 UR (port, UART_R_DATA) = *s;
584 if (*s == '\n') {
585 while ((UR (port, UART_R_STATUS) & TxBusy))
586 ;
587 UR (port, UART_R_DATA) = '\r';
588 }
589 }
590
591 /* Wait until all characters are sent */
592 while (UR (port, UART_R_STATUS) & TxBusy)
593 ;
594
595 /* Restore control and interrupt mask */
596 UR (port, UART_R_CON) = con;
597 UR (port, UART_R_INTEN) = inten;
598}
599
600static void __init lh7a40xuart_console_get_options (struct uart_port* port,
601 int* baud,
602 int* parity,
603 int* bits)
604{
605 if (UR (port, UART_R_CON) & UARTEN) {
606 unsigned int fcon = UR (port, UART_R_FCON);
607 unsigned int quot = UR (port, UART_R_BRCON) + 1;
608
609 switch (fcon & (PEN | EPS)) {
610 default: *parity = 'n'; break;
611 case PEN: *parity = 'o'; break;
612 case PEN | EPS: *parity = 'e'; break;
613 }
614
615 switch (fcon & WLEN) {
616 default:
617 case WLEN_8: *bits = 8; break;
618 case WLEN_7: *bits = 7; break;
619 case WLEN_6: *bits = 6; break;
620 case WLEN_5: *bits = 5; break;
621 }
622
623 *baud = port->uartclk/(16*quot);
624 }
625}
626
627static int __init lh7a40xuart_console_setup (struct console* co, char* options)
628{
629 struct uart_port* port;
630 int baud = 38400;
631 int bits = 8;
632 int parity = 'n';
633 int flow = 'n';
634
635 if (co->index >= DEV_NR) /* Bounds check on device number */
636 co->index = 0;
637 port = &lh7a40x_ports[co->index].port;
638
639 if (options)
640 uart_parse_options (options, &baud, &parity, &bits, &flow);
641 else
642 lh7a40xuart_console_get_options (port, &baud, &parity, &bits);
643
644 return uart_set_options (port, co, baud, parity, bits, flow);
645}
646
647extern struct uart_driver lh7a40x_reg;
648static struct console lh7a40x_console = {
649 .name = "ttyAM",
650 .write = lh7a40xuart_console_write,
651 .device = uart_console_device,
652 .setup = lh7a40xuart_console_setup,
653 .flags = CON_PRINTBUFFER,
654 .index = -1,
655 .data = &lh7a40x_reg,
656};
657
658static int __init lh7a40xuart_console_init(void)
659{
660 register_console (&lh7a40x_console);
661 return 0;
662}
663
664console_initcall (lh7a40xuart_console_init);
665
666#endif
667
668static struct uart_driver lh7a40x_reg = {
669 .owner = THIS_MODULE,
670 .driver_name = "ttyAM",
671 .dev_name = "ttyAM",
672 .major = DEV_MAJOR,
673 .minor = DEV_MINOR,
674 .nr = DEV_NR,
675 .cons = LH7A40X_CONSOLE,
676};
677
678static int __init lh7a40xuart_init(void)
679{
680 int ret;
681
682 printk (KERN_INFO "serial: LH7A40X serial driver\n");
683
684 ret = uart_register_driver (&lh7a40x_reg);
685
686 if (ret == 0) {
687 int i;
688
689 for (i = 0; i < DEV_NR; i++)
690 uart_add_one_port (&lh7a40x_reg,
691 &lh7a40x_ports[i].port);
692 }
693 return ret;
694}
695
696static void __exit lh7a40xuart_exit(void)
697{
698 int i;
699
700 for (i = 0; i < DEV_NR; i++)
701 uart_remove_one_port (&lh7a40x_reg, &lh7a40x_ports[i].port);
702
703 uart_unregister_driver (&lh7a40x_reg);
704}
705
706module_init (lh7a40xuart_init);
707module_exit (lh7a40xuart_exit);
708
709MODULE_AUTHOR ("Marc Singer");
710MODULE_DESCRIPTION ("Sharp LH7A40X serial port driver");
711MODULE_LICENSE ("GPL");
diff --git a/drivers/serial/serial_txx9.c b/drivers/serial/serial_txx9.c
new file mode 100644
index 000000000000..dfc987301f17
--- /dev/null
+++ b/drivers/serial/serial_txx9.c
@@ -0,0 +1,1171 @@
1/*
2 * drivers/serial/serial_txx9.c
3 *
4 * Derived from many drivers using generic_serial interface,
5 * especially serial_tx3912.c by Steven J. Hill and r39xx_serial.c
6 * (was in Linux/VR tree) by Jim Pick.
7 *
8 * Copyright (C) 1999 Harald Koerfgen
9 * Copyright (C) 2000 Jim Pick <jim@jimpick.com>
10 * Copyright (C) 2001 Steven J. Hill (sjhill@realitydiluted.com)
11 * Copyright (C) 2000-2002 Toshiba Corporation
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 * Serial driver for TX3927/TX4927/TX4925/TX4938 internal SIO controller
18 *
19 * Revision History:
20 * 0.30 Initial revision. (Renamed from serial_txx927.c)
21 * 0.31 Use save_flags instead of local_irq_save.
22 * 0.32 Support SCLK.
23 * 0.33 Switch TXX9_TTY_NAME by CONFIG_SERIAL_TXX9_STDSERIAL.
24 * Support TIOCSERGETLSR.
25 * 0.34 Support slow baudrate.
26 * 0.40 Merge codes from mainstream kernel (2.4.22).
27 * 0.41 Fix console checking in rs_shutdown_port().
28 * Disable flow-control in serial_console_write().
29 * 0.42 Fix minor compiler warning.
30 * 1.00 Kernel 2.6. Converted to new serial core (based on 8250.c).
31 * 1.01 Set fifosize to make tx_empry called properly.
32 * Use standard uart_get_divisor.
33 * 1.02 Cleanup. (import 8250.c changes)
34 */
35#include <linux/config.h>
36
37#if defined(CONFIG_SERIAL_TXX9_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
38#define SUPPORT_SYSRQ
39#endif
40
41#include <linux/module.h>
42#include <linux/ioport.h>
43#include <linux/init.h>
44#include <linux/console.h>
45#include <linux/sysrq.h>
46#include <linux/delay.h>
47#include <linux/device.h>
48#include <linux/pci.h>
49#include <linux/tty.h>
50#include <linux/tty_flip.h>
51#include <linux/serial_core.h>
52#include <linux/serial.h>
53
54#include <asm/io.h>
55#include <asm/irq.h>
56
57static char *serial_version = "1.02";
58static char *serial_name = "TX39/49 Serial driver";
59
60#define PASS_LIMIT 256
61
62#if !defined(CONFIG_SERIAL_TXX9_STDSERIAL)
63/* "ttyS" is used for standard serial driver */
64#define TXX9_TTY_NAME "ttyTX"
65#define TXX9_TTY_DEVFS_NAME "tttx/"
66#define TXX9_TTY_MINOR_START (64 + 64) /* ttyTX0(128), ttyTX1(129) */
67#else
68/* acts like standard serial driver */
69#define TXX9_TTY_NAME "ttyS"
70#define TXX9_TTY_DEVFS_NAME "tts/"
71#define TXX9_TTY_MINOR_START 64
72#endif
73#define TXX9_TTY_MAJOR TTY_MAJOR
74
75/* flag aliases */
76#define UPF_TXX9_HAVE_CTS_LINE UPF_BUGGY_UART
77#define UPF_TXX9_USE_SCLK UPF_MAGIC_MULTIPLIER
78
79#ifdef CONFIG_PCI
80/* support for Toshiba TC86C001 SIO */
81#define ENABLE_SERIAL_TXX9_PCI
82#endif
83
84/*
85 * Number of serial ports
86 */
87#ifdef ENABLE_SERIAL_TXX9_PCI
88#define NR_PCI_BOARDS 4
89#define UART_NR (2 + NR_PCI_BOARDS)
90#else
91#define UART_NR 2
92#endif
93
94struct uart_txx9_port {
95 struct uart_port port;
96
97 /*
98 * We provide a per-port pm hook.
99 */
100 void (*pm)(struct uart_port *port,
101 unsigned int state, unsigned int old);
102};
103
104#define TXX9_REGION_SIZE 0x24
105
106/* TXX9 Serial Registers */
107#define TXX9_SILCR 0x00
108#define TXX9_SIDICR 0x04
109#define TXX9_SIDISR 0x08
110#define TXX9_SICISR 0x0c
111#define TXX9_SIFCR 0x10
112#define TXX9_SIFLCR 0x14
113#define TXX9_SIBGR 0x18
114#define TXX9_SITFIFO 0x1c
115#define TXX9_SIRFIFO 0x20
116
117/* SILCR : Line Control */
118#define TXX9_SILCR_SCS_MASK 0x00000060
119#define TXX9_SILCR_SCS_IMCLK 0x00000000
120#define TXX9_SILCR_SCS_IMCLK_BG 0x00000020
121#define TXX9_SILCR_SCS_SCLK 0x00000040
122#define TXX9_SILCR_SCS_SCLK_BG 0x00000060
123#define TXX9_SILCR_UEPS 0x00000010
124#define TXX9_SILCR_UPEN 0x00000008
125#define TXX9_SILCR_USBL_MASK 0x00000004
126#define TXX9_SILCR_USBL_1BIT 0x00000000
127#define TXX9_SILCR_USBL_2BIT 0x00000004
128#define TXX9_SILCR_UMODE_MASK 0x00000003
129#define TXX9_SILCR_UMODE_8BIT 0x00000000
130#define TXX9_SILCR_UMODE_7BIT 0x00000001
131
132/* SIDICR : DMA/Int. Control */
133#define TXX9_SIDICR_TDE 0x00008000
134#define TXX9_SIDICR_RDE 0x00004000
135#define TXX9_SIDICR_TIE 0x00002000
136#define TXX9_SIDICR_RIE 0x00001000
137#define TXX9_SIDICR_SPIE 0x00000800
138#define TXX9_SIDICR_CTSAC 0x00000600
139#define TXX9_SIDICR_STIE_MASK 0x0000003f
140#define TXX9_SIDICR_STIE_OERS 0x00000020
141#define TXX9_SIDICR_STIE_CTSS 0x00000010
142#define TXX9_SIDICR_STIE_RBRKD 0x00000008
143#define TXX9_SIDICR_STIE_TRDY 0x00000004
144#define TXX9_SIDICR_STIE_TXALS 0x00000002
145#define TXX9_SIDICR_STIE_UBRKD 0x00000001
146
147/* SIDISR : DMA/Int. Status */
148#define TXX9_SIDISR_UBRK 0x00008000
149#define TXX9_SIDISR_UVALID 0x00004000
150#define TXX9_SIDISR_UFER 0x00002000
151#define TXX9_SIDISR_UPER 0x00001000
152#define TXX9_SIDISR_UOER 0x00000800
153#define TXX9_SIDISR_ERI 0x00000400
154#define TXX9_SIDISR_TOUT 0x00000200
155#define TXX9_SIDISR_TDIS 0x00000100
156#define TXX9_SIDISR_RDIS 0x00000080
157#define TXX9_SIDISR_STIS 0x00000040
158#define TXX9_SIDISR_RFDN_MASK 0x0000001f
159
160/* SICISR : Change Int. Status */
161#define TXX9_SICISR_OERS 0x00000020
162#define TXX9_SICISR_CTSS 0x00000010
163#define TXX9_SICISR_RBRKD 0x00000008
164#define TXX9_SICISR_TRDY 0x00000004
165#define TXX9_SICISR_TXALS 0x00000002
166#define TXX9_SICISR_UBRKD 0x00000001
167
168/* SIFCR : FIFO Control */
169#define TXX9_SIFCR_SWRST 0x00008000
170#define TXX9_SIFCR_RDIL_MASK 0x00000180
171#define TXX9_SIFCR_RDIL_1 0x00000000
172#define TXX9_SIFCR_RDIL_4 0x00000080
173#define TXX9_SIFCR_RDIL_8 0x00000100
174#define TXX9_SIFCR_RDIL_12 0x00000180
175#define TXX9_SIFCR_RDIL_MAX 0x00000180
176#define TXX9_SIFCR_TDIL_MASK 0x00000018
177#define TXX9_SIFCR_TDIL_MASK 0x00000018
178#define TXX9_SIFCR_TDIL_1 0x00000000
179#define TXX9_SIFCR_TDIL_4 0x00000001
180#define TXX9_SIFCR_TDIL_8 0x00000010
181#define TXX9_SIFCR_TDIL_MAX 0x00000010
182#define TXX9_SIFCR_TFRST 0x00000004
183#define TXX9_SIFCR_RFRST 0x00000002
184#define TXX9_SIFCR_FRSTE 0x00000001
185#define TXX9_SIO_TX_FIFO 8
186#define TXX9_SIO_RX_FIFO 16
187
188/* SIFLCR : Flow Control */
189#define TXX9_SIFLCR_RCS 0x00001000
190#define TXX9_SIFLCR_TES 0x00000800
191#define TXX9_SIFLCR_RTSSC 0x00000200
192#define TXX9_SIFLCR_RSDE 0x00000100
193#define TXX9_SIFLCR_TSDE 0x00000080
194#define TXX9_SIFLCR_RTSTL_MASK 0x0000001e
195#define TXX9_SIFLCR_RTSTL_MAX 0x0000001e
196#define TXX9_SIFLCR_TBRK 0x00000001
197
198/* SIBGR : Baudrate Control */
199#define TXX9_SIBGR_BCLK_MASK 0x00000300
200#define TXX9_SIBGR_BCLK_T0 0x00000000
201#define TXX9_SIBGR_BCLK_T2 0x00000100
202#define TXX9_SIBGR_BCLK_T4 0x00000200
203#define TXX9_SIBGR_BCLK_T6 0x00000300
204#define TXX9_SIBGR_BRD_MASK 0x000000ff
205
206static inline unsigned int sio_in(struct uart_txx9_port *up, int offset)
207{
208 switch (up->port.iotype) {
209 default:
210 return *(volatile u32 *)(up->port.membase + offset);
211 case UPIO_PORT:
212 return inl(up->port.iobase + offset);
213 }
214}
215
216static inline void
217sio_out(struct uart_txx9_port *up, int offset, int value)
218{
219 switch (up->port.iotype) {
220 default:
221 *(volatile u32 *)(up->port.membase + offset) = value;
222 break;
223 case UPIO_PORT:
224 outl(value, up->port.iobase + offset);
225 break;
226 }
227}
228
229static inline void
230sio_mask(struct uart_txx9_port *up, int offset, unsigned int value)
231{
232 sio_out(up, offset, sio_in(up, offset) & ~value);
233}
234static inline void
235sio_set(struct uart_txx9_port *up, int offset, unsigned int value)
236{
237 sio_out(up, offset, sio_in(up, offset) | value);
238}
239
240static inline void
241sio_quot_set(struct uart_txx9_port *up, int quot)
242{
243 quot >>= 1;
244 if (quot < 256)
245 sio_out(up, TXX9_SIBGR, quot | TXX9_SIBGR_BCLK_T0);
246 else if (quot < (256 << 2))
247 sio_out(up, TXX9_SIBGR, (quot >> 2) | TXX9_SIBGR_BCLK_T2);
248 else if (quot < (256 << 4))
249 sio_out(up, TXX9_SIBGR, (quot >> 4) | TXX9_SIBGR_BCLK_T4);
250 else if (quot < (256 << 6))
251 sio_out(up, TXX9_SIBGR, (quot >> 6) | TXX9_SIBGR_BCLK_T6);
252 else
253 sio_out(up, TXX9_SIBGR, 0xff | TXX9_SIBGR_BCLK_T6);
254}
255
256static void serial_txx9_stop_tx(struct uart_port *port, unsigned int tty_stop)
257{
258 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
259 unsigned long flags;
260
261 spin_lock_irqsave(&up->port.lock, flags);
262 sio_mask(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
263 spin_unlock_irqrestore(&up->port.lock, flags);
264}
265
266static void serial_txx9_start_tx(struct uart_port *port, unsigned int tty_start)
267{
268 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
269 unsigned long flags;
270
271 spin_lock_irqsave(&up->port.lock, flags);
272 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
273 spin_unlock_irqrestore(&up->port.lock, flags);
274}
275
276static void serial_txx9_stop_rx(struct uart_port *port)
277{
278 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
279 unsigned long flags;
280
281 spin_lock_irqsave(&up->port.lock, flags);
282 up->port.read_status_mask &= ~TXX9_SIDISR_RDIS;
283#if 0
284 sio_mask(up, TXX9_SIDICR, TXX9_SIDICR_RIE);
285#endif
286 spin_unlock_irqrestore(&up->port.lock, flags);
287}
288
289static void serial_txx9_enable_ms(struct uart_port *port)
290{
291 /* TXX9-SIO can not control DTR... */
292}
293
294static inline void
295receive_chars(struct uart_txx9_port *up, unsigned int *status, struct pt_regs *regs)
296{
297 struct tty_struct *tty = up->port.info->tty;
298 unsigned char ch;
299 unsigned int disr = *status;
300 int max_count = 256;
301 char flag;
302
303 do {
304 /* The following is not allowed by the tty layer and
305 unsafe. It should be fixed ASAP */
306 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
307 if(tty->low_latency)
308 tty_flip_buffer_push(tty);
309 /* If this failed then we will throw away the
310 bytes but must do so to clear interrupts */
311 }
312 ch = sio_in(up, TXX9_SIRFIFO);
313 flag = TTY_NORMAL;
314 up->port.icount.rx++;
315
316 if (unlikely(disr & (TXX9_SIDISR_UBRK | TXX9_SIDISR_UPER |
317 TXX9_SIDISR_UFER | TXX9_SIDISR_UOER))) {
318 /*
319 * For statistics only
320 */
321 if (disr & TXX9_SIDISR_UBRK) {
322 disr &= ~(TXX9_SIDISR_UFER | TXX9_SIDISR_UPER);
323 up->port.icount.brk++;
324 /*
325 * We do the SysRQ and SAK checking
326 * here because otherwise the break
327 * may get masked by ignore_status_mask
328 * or read_status_mask.
329 */
330 if (uart_handle_break(&up->port))
331 goto ignore_char;
332 } else if (disr & TXX9_SIDISR_UPER)
333 up->port.icount.parity++;
334 else if (disr & TXX9_SIDISR_UFER)
335 up->port.icount.frame++;
336 if (disr & TXX9_SIDISR_UOER)
337 up->port.icount.overrun++;
338
339 /*
340 * Mask off conditions which should be ingored.
341 */
342 disr &= up->port.read_status_mask;
343
344 if (disr & TXX9_SIDISR_UBRK) {
345 flag = TTY_BREAK;
346 } else if (disr & TXX9_SIDISR_UPER)
347 flag = TTY_PARITY;
348 else if (disr & TXX9_SIDISR_UFER)
349 flag = TTY_FRAME;
350 }
351 if (uart_handle_sysrq_char(&up->port, ch, regs))
352 goto ignore_char;
353 if ((disr & up->port.ignore_status_mask) == 0) {
354 tty_insert_flip_char(tty, ch, flag);
355 }
356 if ((disr & TXX9_SIDISR_UOER) &&
357 tty->flip.count < TTY_FLIPBUF_SIZE) {
358 /*
359 * Overrun is special, since it's reported
360 * immediately, and doesn't affect the current
361 * character.
362 */
363 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
364 }
365 ignore_char:
366 disr = sio_in(up, TXX9_SIDISR);
367 } while (!(disr & TXX9_SIDISR_UVALID) && (max_count-- > 0));
368 tty_flip_buffer_push(tty);
369 *status = disr;
370}
371
372static inline void transmit_chars(struct uart_txx9_port *up)
373{
374 struct circ_buf *xmit = &up->port.info->xmit;
375 int count;
376
377 if (up->port.x_char) {
378 sio_out(up, TXX9_SITFIFO, up->port.x_char);
379 up->port.icount.tx++;
380 up->port.x_char = 0;
381 return;
382 }
383 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
384 serial_txx9_stop_tx(&up->port, 0);
385 return;
386 }
387
388 count = TXX9_SIO_TX_FIFO;
389 do {
390 sio_out(up, TXX9_SITFIFO, xmit->buf[xmit->tail]);
391 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
392 up->port.icount.tx++;
393 if (uart_circ_empty(xmit))
394 break;
395 } while (--count > 0);
396
397 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
398 uart_write_wakeup(&up->port);
399
400 if (uart_circ_empty(xmit))
401 serial_txx9_stop_tx(&up->port, 0);
402}
403
404static irqreturn_t serial_txx9_interrupt(int irq, void *dev_id, struct pt_regs *regs)
405{
406 int pass_counter = 0;
407 struct uart_txx9_port *up = dev_id;
408 unsigned int status;
409
410 while (1) {
411 spin_lock(&up->port.lock);
412 status = sio_in(up, TXX9_SIDISR);
413 if (!(sio_in(up, TXX9_SIDICR) & TXX9_SIDICR_TIE))
414 status &= ~TXX9_SIDISR_TDIS;
415 if (!(status & (TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
416 TXX9_SIDISR_TOUT))) {
417 spin_unlock(&up->port.lock);
418 break;
419 }
420
421 if (status & TXX9_SIDISR_RDIS)
422 receive_chars(up, &status, regs);
423 if (status & TXX9_SIDISR_TDIS)
424 transmit_chars(up);
425 /* Clear TX/RX Int. Status */
426 sio_mask(up, TXX9_SIDISR,
427 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
428 TXX9_SIDISR_TOUT);
429 spin_unlock(&up->port.lock);
430
431 if (pass_counter++ > PASS_LIMIT)
432 break;
433 }
434
435 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
436}
437
438static unsigned int serial_txx9_tx_empty(struct uart_port *port)
439{
440 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
441 unsigned long flags;
442 unsigned int ret;
443
444 spin_lock_irqsave(&up->port.lock, flags);
445 ret = (sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS) ? TIOCSER_TEMT : 0;
446 spin_unlock_irqrestore(&up->port.lock, flags);
447
448 return ret;
449}
450
451static unsigned int serial_txx9_get_mctrl(struct uart_port *port)
452{
453 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
454 unsigned long flags;
455 unsigned int ret;
456
457 spin_lock_irqsave(&up->port.lock, flags);
458 ret = ((sio_in(up, TXX9_SIFLCR) & TXX9_SIFLCR_RTSSC) ? 0 : TIOCM_RTS)
459 | ((sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS) ? 0 : TIOCM_CTS);
460 spin_unlock_irqrestore(&up->port.lock, flags);
461
462 return ret;
463}
464
465static void serial_txx9_set_mctrl(struct uart_port *port, unsigned int mctrl)
466{
467 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
468 unsigned long flags;
469
470 spin_lock_irqsave(&up->port.lock, flags);
471 if (mctrl & TIOCM_RTS)
472 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
473 else
474 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
475 spin_unlock_irqrestore(&up->port.lock, flags);
476}
477
478static void serial_txx9_break_ctl(struct uart_port *port, int break_state)
479{
480 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
481 unsigned long flags;
482
483 spin_lock_irqsave(&up->port.lock, flags);
484 if (break_state == -1)
485 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
486 else
487 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
488 spin_unlock_irqrestore(&up->port.lock, flags);
489}
490
491static int serial_txx9_startup(struct uart_port *port)
492{
493 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
494 unsigned long flags;
495 int retval;
496
497 /*
498 * Clear the FIFO buffers and disable them.
499 * (they will be reeanbled in set_termios())
500 */
501 sio_set(up, TXX9_SIFCR,
502 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
503 /* clear reset */
504 sio_mask(up, TXX9_SIFCR,
505 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
506 sio_out(up, TXX9_SIDICR, 0);
507
508 /*
509 * Clear the interrupt registers.
510 */
511 sio_out(up, TXX9_SIDISR, 0);
512
513 retval = request_irq(up->port.irq, serial_txx9_interrupt,
514 SA_SHIRQ, "serial_txx9", up);
515 if (retval)
516 return retval;
517
518 /*
519 * Now, initialize the UART
520 */
521 spin_lock_irqsave(&up->port.lock, flags);
522 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
523 spin_unlock_irqrestore(&up->port.lock, flags);
524
525 /* Enable RX/TX */
526 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
527
528 /*
529 * Finally, enable interrupts.
530 */
531 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_RIE);
532
533 return 0;
534}
535
536static void serial_txx9_shutdown(struct uart_port *port)
537{
538 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
539 unsigned long flags;
540
541 /*
542 * Disable interrupts from this port
543 */
544 sio_out(up, TXX9_SIDICR, 0); /* disable all intrs */
545
546 spin_lock_irqsave(&up->port.lock, flags);
547 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
548 spin_unlock_irqrestore(&up->port.lock, flags);
549
550 /*
551 * Disable break condition
552 */
553 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
554
555#ifdef CONFIG_SERIAL_TXX9_CONSOLE
556 if (up->port.cons && up->port.line == up->port.cons->index) {
557 free_irq(up->port.irq, up);
558 return;
559 }
560#endif
561 /* reset FIFOs */
562 sio_set(up, TXX9_SIFCR,
563 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
564 /* clear reset */
565 sio_mask(up, TXX9_SIFCR,
566 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
567
568 /* Disable RX/TX */
569 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
570
571 free_irq(up->port.irq, up);
572}
573
574static void
575serial_txx9_set_termios(struct uart_port *port, struct termios *termios,
576 struct termios *old)
577{
578 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
579 unsigned int cval, fcr = 0;
580 unsigned long flags;
581 unsigned int baud, quot;
582
583 cval = sio_in(up, TXX9_SILCR);
584 /* byte size and parity */
585 cval &= ~TXX9_SILCR_UMODE_MASK;
586 switch (termios->c_cflag & CSIZE) {
587 case CS7:
588 cval |= TXX9_SILCR_UMODE_7BIT;
589 break;
590 default:
591 case CS5: /* not supported */
592 case CS6: /* not supported */
593 case CS8:
594 cval |= TXX9_SILCR_UMODE_8BIT;
595 break;
596 }
597
598 cval &= ~TXX9_SILCR_USBL_MASK;
599 if (termios->c_cflag & CSTOPB)
600 cval |= TXX9_SILCR_USBL_2BIT;
601 else
602 cval |= TXX9_SILCR_USBL_1BIT;
603 cval &= ~(TXX9_SILCR_UPEN | TXX9_SILCR_UEPS);
604 if (termios->c_cflag & PARENB)
605 cval |= TXX9_SILCR_UPEN;
606 if (!(termios->c_cflag & PARODD))
607 cval |= TXX9_SILCR_UEPS;
608
609 /*
610 * Ask the core to calculate the divisor for us.
611 */
612 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16/2);
613 quot = uart_get_divisor(port, baud);
614
615 /* Set up FIFOs */
616 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
617 fcr = TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1;
618
619 /*
620 * Ok, we're now changing the port state. Do it with
621 * interrupts disabled.
622 */
623 spin_lock_irqsave(&up->port.lock, flags);
624
625 /*
626 * Update the per-port timeout.
627 */
628 uart_update_timeout(port, termios->c_cflag, baud);
629
630 up->port.read_status_mask = TXX9_SIDISR_UOER |
631 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS;
632 if (termios->c_iflag & INPCK)
633 up->port.read_status_mask |= TXX9_SIDISR_UFER | TXX9_SIDISR_UPER;
634 if (termios->c_iflag & (BRKINT | PARMRK))
635 up->port.read_status_mask |= TXX9_SIDISR_UBRK;
636
637 /*
638 * Characteres to ignore
639 */
640 up->port.ignore_status_mask = 0;
641 if (termios->c_iflag & IGNPAR)
642 up->port.ignore_status_mask |= TXX9_SIDISR_UPER | TXX9_SIDISR_UFER;
643 if (termios->c_iflag & IGNBRK) {
644 up->port.ignore_status_mask |= TXX9_SIDISR_UBRK;
645 /*
646 * If we're ignoring parity and break indicators,
647 * ignore overruns too (for real raw support).
648 */
649 if (termios->c_iflag & IGNPAR)
650 up->port.ignore_status_mask |= TXX9_SIDISR_UOER;
651 }
652
653 /*
654 * ignore all characters if CREAD is not set
655 */
656 if ((termios->c_cflag & CREAD) == 0)
657 up->port.ignore_status_mask |= TXX9_SIDISR_RDIS;
658
659 /* CTS flow control flag */
660 if ((termios->c_cflag & CRTSCTS) &&
661 (up->port.flags & UPF_TXX9_HAVE_CTS_LINE)) {
662 sio_set(up, TXX9_SIFLCR,
663 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
664 } else {
665 sio_mask(up, TXX9_SIFLCR,
666 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
667 }
668
669 sio_out(up, TXX9_SILCR, cval);
670 sio_quot_set(up, quot);
671 sio_out(up, TXX9_SIFCR, fcr);
672
673 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
674 spin_unlock_irqrestore(&up->port.lock, flags);
675}
676
677static void
678serial_txx9_pm(struct uart_port *port, unsigned int state,
679 unsigned int oldstate)
680{
681 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
682 if (state) {
683 /* sleep */
684
685 if (up->pm)
686 up->pm(port, state, oldstate);
687 } else {
688 /* wake */
689
690 if (up->pm)
691 up->pm(port, state, oldstate);
692 }
693}
694
695static int serial_txx9_request_resource(struct uart_txx9_port *up)
696{
697 unsigned int size = TXX9_REGION_SIZE;
698 int ret = 0;
699
700 switch (up->port.iotype) {
701 default:
702 if (!up->port.mapbase)
703 break;
704
705 if (!request_mem_region(up->port.mapbase, size, "serial_txx9")) {
706 ret = -EBUSY;
707 break;
708 }
709
710 if (up->port.flags & UPF_IOREMAP) {
711 up->port.membase = ioremap(up->port.mapbase, size);
712 if (!up->port.membase) {
713 release_mem_region(up->port.mapbase, size);
714 ret = -ENOMEM;
715 }
716 }
717 break;
718
719 case UPIO_PORT:
720 if (!request_region(up->port.iobase, size, "serial_txx9"))
721 ret = -EBUSY;
722 break;
723 }
724 return ret;
725}
726
727static void serial_txx9_release_resource(struct uart_txx9_port *up)
728{
729 unsigned int size = TXX9_REGION_SIZE;
730
731 switch (up->port.iotype) {
732 default:
733 if (!up->port.mapbase)
734 break;
735
736 if (up->port.flags & UPF_IOREMAP) {
737 iounmap(up->port.membase);
738 up->port.membase = NULL;
739 }
740
741 release_mem_region(up->port.mapbase, size);
742 break;
743
744 case UPIO_PORT:
745 release_region(up->port.iobase, size);
746 break;
747 }
748}
749
750static void serial_txx9_release_port(struct uart_port *port)
751{
752 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
753 serial_txx9_release_resource(up);
754}
755
756static int serial_txx9_request_port(struct uart_port *port)
757{
758 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
759 return serial_txx9_request_resource(up);
760}
761
762static void serial_txx9_config_port(struct uart_port *port, int uflags)
763{
764 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
765 unsigned long flags;
766 int ret;
767
768 /*
769 * Find the region that we can probe for. This in turn
770 * tells us whether we can probe for the type of port.
771 */
772 ret = serial_txx9_request_resource(up);
773 if (ret < 0)
774 return;
775 port->type = PORT_TXX9;
776 up->port.fifosize = TXX9_SIO_TX_FIFO;
777
778#ifdef CONFIG_SERIAL_TXX9_CONSOLE
779 if (up->port.line == up->port.cons->index)
780 return;
781#endif
782 spin_lock_irqsave(&up->port.lock, flags);
783 /*
784 * Reset the UART.
785 */
786 sio_out(up, TXX9_SIFCR, TXX9_SIFCR_SWRST);
787#ifdef CONFIG_CPU_TX49XX
788 /* TX4925 BUG WORKAROUND. Accessing SIOC register
789 * immediately after soft reset causes bus error. */
790 iob();
791 udelay(1);
792#endif
793 while (sio_in(up, TXX9_SIFCR) & TXX9_SIFCR_SWRST)
794 ;
795 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
796 sio_set(up, TXX9_SIFCR,
797 TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1);
798 /* initial settings */
799 sio_out(up, TXX9_SILCR,
800 TXX9_SILCR_UMODE_8BIT | TXX9_SILCR_USBL_1BIT |
801 ((up->port.flags & UPF_TXX9_USE_SCLK) ?
802 TXX9_SILCR_SCS_SCLK_BG : TXX9_SILCR_SCS_IMCLK_BG));
803 sio_quot_set(up, uart_get_divisor(port, 9600));
804 sio_out(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSTL_MAX /* 15 */);
805 spin_unlock_irqrestore(&up->port.lock, flags);
806}
807
808static int
809serial_txx9_verify_port(struct uart_port *port, struct serial_struct *ser)
810{
811 if (ser->irq < 0 ||
812 ser->baud_base < 9600 || ser->type != PORT_TXX9)
813 return -EINVAL;
814 return 0;
815}
816
817static const char *
818serial_txx9_type(struct uart_port *port)
819{
820 return "txx9";
821}
822
823static struct uart_ops serial_txx9_pops = {
824 .tx_empty = serial_txx9_tx_empty,
825 .set_mctrl = serial_txx9_set_mctrl,
826 .get_mctrl = serial_txx9_get_mctrl,
827 .stop_tx = serial_txx9_stop_tx,
828 .start_tx = serial_txx9_start_tx,
829 .stop_rx = serial_txx9_stop_rx,
830 .enable_ms = serial_txx9_enable_ms,
831 .break_ctl = serial_txx9_break_ctl,
832 .startup = serial_txx9_startup,
833 .shutdown = serial_txx9_shutdown,
834 .set_termios = serial_txx9_set_termios,
835 .pm = serial_txx9_pm,
836 .type = serial_txx9_type,
837 .release_port = serial_txx9_release_port,
838 .request_port = serial_txx9_request_port,
839 .config_port = serial_txx9_config_port,
840 .verify_port = serial_txx9_verify_port,
841};
842
843static struct uart_txx9_port serial_txx9_ports[UART_NR];
844
845static void __init serial_txx9_register_ports(struct uart_driver *drv)
846{
847 int i;
848
849 for (i = 0; i < UART_NR; i++) {
850 struct uart_txx9_port *up = &serial_txx9_ports[i];
851
852 up->port.line = i;
853 up->port.ops = &serial_txx9_pops;
854 uart_add_one_port(drv, &up->port);
855 }
856}
857
858#ifdef CONFIG_SERIAL_TXX9_CONSOLE
859
860/*
861 * Wait for transmitter & holding register to empty
862 */
863static inline void wait_for_xmitr(struct uart_txx9_port *up)
864{
865 unsigned int tmout = 10000;
866
867 /* Wait up to 10ms for the character(s) to be sent. */
868 while (--tmout &&
869 !(sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS))
870 udelay(1);
871
872 /* Wait up to 1s for flow control if necessary */
873 if (up->port.flags & UPF_CONS_FLOW) {
874 tmout = 1000000;
875 while (--tmout &&
876 (sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS))
877 udelay(1);
878 }
879}
880
881/*
882 * Print a string to the serial port trying not to disturb
883 * any possible real use of the port...
884 *
885 * The console_lock must be held when we get here.
886 */
887static void
888serial_txx9_console_write(struct console *co, const char *s, unsigned int count)
889{
890 struct uart_txx9_port *up = &serial_txx9_ports[co->index];
891 unsigned int ier, flcr;
892 int i;
893
894 /*
895 * First save the UER then disable the interrupts
896 */
897 ier = sio_in(up, TXX9_SIDICR);
898 sio_out(up, TXX9_SIDICR, 0);
899 /*
900 * Disable flow-control if enabled (and unnecessary)
901 */
902 flcr = sio_in(up, TXX9_SIFLCR);
903 if (!(up->port.flags & UPF_CONS_FLOW) && (flcr & TXX9_SIFLCR_TES))
904 sio_out(up, TXX9_SIFLCR, flcr & ~TXX9_SIFLCR_TES);
905
906 /*
907 * Now, do each character
908 */
909 for (i = 0; i < count; i++, s++) {
910 wait_for_xmitr(up);
911
912 /*
913 * Send the character out.
914 * If a LF, also do CR...
915 */
916 sio_out(up, TXX9_SITFIFO, *s);
917 if (*s == 10) {
918 wait_for_xmitr(up);
919 sio_out(up, TXX9_SITFIFO, 13);
920 }
921 }
922
923 /*
924 * Finally, wait for transmitter to become empty
925 * and restore the IER
926 */
927 wait_for_xmitr(up);
928 sio_out(up, TXX9_SIFLCR, flcr);
929 sio_out(up, TXX9_SIDICR, ier);
930}
931
932static int serial_txx9_console_setup(struct console *co, char *options)
933{
934 struct uart_port *port;
935 struct uart_txx9_port *up;
936 int baud = 9600;
937 int bits = 8;
938 int parity = 'n';
939 int flow = 'n';
940
941 /*
942 * Check whether an invalid uart number has been specified, and
943 * if so, search for the first available port that does have
944 * console support.
945 */
946 if (co->index >= UART_NR)
947 co->index = 0;
948 up = &serial_txx9_ports[co->index];
949 port = &up->port;
950 if (!port->ops)
951 return -ENODEV;
952
953 /*
954 * Temporary fix.
955 */
956 spin_lock_init(&port->lock);
957
958 /*
959 * Disable UART interrupts, set DTR and RTS high
960 * and set speed.
961 */
962 sio_out(up, TXX9_SIDICR, 0);
963 /* initial settings */
964 sio_out(up, TXX9_SILCR,
965 TXX9_SILCR_UMODE_8BIT | TXX9_SILCR_USBL_1BIT |
966 ((port->flags & UPF_TXX9_USE_SCLK) ?
967 TXX9_SILCR_SCS_SCLK_BG : TXX9_SILCR_SCS_IMCLK_BG));
968 sio_out(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSTL_MAX /* 15 */);
969
970 if (options)
971 uart_parse_options(options, &baud, &parity, &bits, &flow);
972
973 return uart_set_options(port, co, baud, parity, bits, flow);
974}
975
976static struct uart_driver serial_txx9_reg;
977static struct console serial_txx9_console = {
978 .name = TXX9_TTY_NAME,
979 .write = serial_txx9_console_write,
980 .device = uart_console_device,
981 .setup = serial_txx9_console_setup,
982 .flags = CON_PRINTBUFFER,
983 .index = -1,
984 .data = &serial_txx9_reg,
985};
986
987static int __init serial_txx9_console_init(void)
988{
989 register_console(&serial_txx9_console);
990 return 0;
991}
992console_initcall(serial_txx9_console_init);
993
994static int __init serial_txx9_late_console_init(void)
995{
996 if (!(serial_txx9_console.flags & CON_ENABLED))
997 register_console(&serial_txx9_console);
998 return 0;
999}
1000late_initcall(serial_txx9_late_console_init);
1001
1002#define SERIAL_TXX9_CONSOLE &serial_txx9_console
1003#else
1004#define SERIAL_TXX9_CONSOLE NULL
1005#endif
1006
1007static struct uart_driver serial_txx9_reg = {
1008 .owner = THIS_MODULE,
1009 .driver_name = "serial_txx9",
1010 .devfs_name = TXX9_TTY_DEVFS_NAME,
1011 .dev_name = TXX9_TTY_NAME,
1012 .major = TXX9_TTY_MAJOR,
1013 .minor = TXX9_TTY_MINOR_START,
1014 .nr = UART_NR,
1015 .cons = SERIAL_TXX9_CONSOLE,
1016};
1017
1018int __init early_serial_txx9_setup(struct uart_port *port)
1019{
1020 if (port->line >= ARRAY_SIZE(serial_txx9_ports))
1021 return -ENODEV;
1022
1023 serial_txx9_ports[port->line].port = *port;
1024 serial_txx9_ports[port->line].port.ops = &serial_txx9_pops;
1025 serial_txx9_ports[port->line].port.flags |= UPF_BOOT_AUTOCONF;
1026 return 0;
1027}
1028
1029#ifdef ENABLE_SERIAL_TXX9_PCI
1030/**
1031 * serial_txx9_suspend_port - suspend one serial port
1032 * @line: serial line number
1033 * @level: the level of port suspension, as per uart_suspend_port
1034 *
1035 * Suspend one serial port.
1036 */
1037static void serial_txx9_suspend_port(int line)
1038{
1039 uart_suspend_port(&serial_txx9_reg, &serial_txx9_ports[line].port);
1040}
1041
1042/**
1043 * serial_txx9_resume_port - resume one serial port
1044 * @line: serial line number
1045 * @level: the level of port resumption, as per uart_resume_port
1046 *
1047 * Resume one serial port.
1048 */
1049static void serial_txx9_resume_port(int line)
1050{
1051 uart_resume_port(&serial_txx9_reg, &serial_txx9_ports[line].port);
1052}
1053
1054/*
1055 * Probe one serial board. Unfortunately, there is no rhyme nor reason
1056 * to the arrangement of serial ports on a PCI card.
1057 */
1058static int __devinit
1059pciserial_txx9_init_one(struct pci_dev *dev, const struct pci_device_id *ent)
1060{
1061 struct uart_port port;
1062 int line;
1063 int rc;
1064
1065 rc = pci_enable_device(dev);
1066 if (rc)
1067 return rc;
1068
1069 memset(&port, 0, sizeof(port));
1070 port.ops = &serial_txx9_pops;
1071 port.flags |= UPF_BOOT_AUTOCONF; /* uart_ops.config_port will be called */
1072 port.flags |= UPF_TXX9_HAVE_CTS_LINE;
1073 port.uartclk = 66670000;
1074 port.irq = dev->irq;
1075 port.iotype = UPIO_PORT;
1076 port.iobase = pci_resource_start(dev, 1);
1077 line = uart_register_port(&serial_txx9_reg, &port);
1078 if (line < 0) {
1079 printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), line);
1080 }
1081 pci_set_drvdata(dev, (void *)(long)line);
1082
1083 return 0;
1084}
1085
1086static void __devexit pciserial_txx9_remove_one(struct pci_dev *dev)
1087{
1088 int line = (int)(long)pci_get_drvdata(dev);
1089
1090 pci_set_drvdata(dev, NULL);
1091
1092 if (line) {
1093 uart_unregister_port(&serial_txx9_reg, line);
1094 pci_disable_device(dev);
1095 }
1096}
1097
1098static int pciserial_txx9_suspend_one(struct pci_dev *dev, u32 state)
1099{
1100 int line = (int)(long)pci_get_drvdata(dev);
1101
1102 if (line)
1103 serial_txx9_suspend_port(line);
1104 return 0;
1105}
1106
1107static int pciserial_txx9_resume_one(struct pci_dev *dev)
1108{
1109 int line = (int)(long)pci_get_drvdata(dev);
1110
1111 if (line)
1112 serial_txx9_resume_port(line);
1113 return 0;
1114}
1115
1116static struct pci_device_id serial_txx9_pci_tbl[] = {
1117 { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC86C001_MISC,
1118 PCI_ANY_ID, PCI_ANY_ID,
1119 0, 0, 0 },
1120 { 0, }
1121};
1122
1123static struct pci_driver serial_txx9_pci_driver = {
1124 .name = "serial_txx9",
1125 .probe = pciserial_txx9_init_one,
1126 .remove = __devexit_p(pciserial_txx9_remove_one),
1127 .suspend = pciserial_txx9_suspend_one,
1128 .resume = pciserial_txx9_resume_one,
1129 .id_table = serial_txx9_pci_tbl,
1130};
1131
1132MODULE_DEVICE_TABLE(pci, serial_txx9_pci_tbl);
1133#endif /* ENABLE_SERIAL_TXX9_PCI */
1134
1135static int __init serial_txx9_init(void)
1136{
1137 int ret;
1138
1139 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1140
1141 ret = uart_register_driver(&serial_txx9_reg);
1142 if (ret >= 0) {
1143 serial_txx9_register_ports(&serial_txx9_reg);
1144
1145#ifdef ENABLE_SERIAL_TXX9_PCI
1146 ret = pci_module_init(&serial_txx9_pci_driver);
1147#endif
1148 }
1149 return ret;
1150}
1151
1152static void __exit serial_txx9_exit(void)
1153{
1154 int i;
1155
1156#ifdef ENABLE_SERIAL_TXX9_PCI
1157 pci_unregister_driver(&serial_txx9_pci_driver);
1158#endif
1159 for (i = 0; i < UART_NR; i++)
1160 uart_remove_one_port(&serial_txx9_reg, &serial_txx9_ports[i].port);
1161
1162 uart_unregister_driver(&serial_txx9_reg);
1163}
1164
1165module_init(serial_txx9_init);
1166module_exit(serial_txx9_exit);
1167
1168MODULE_LICENSE("GPL");
1169MODULE_DESCRIPTION("TX39/49 serial driver");
1170
1171MODULE_ALIAS_CHARDEV_MAJOR(TXX9_TTY_MAJOR);
diff --git a/drivers/serial/sh-sci.c b/drivers/serial/sh-sci.c
new file mode 100644
index 000000000000..ad5b776d779b
--- /dev/null
+++ b/drivers/serial/sh-sci.c
@@ -0,0 +1,1692 @@
1/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
6 * Copyright (C) 2002, 2003, 2004 Paul Mundt
7 *
8 * based off of the old drivers/char/sh-sci.c by:
9 *
10 * Copyright (C) 1999, 2000 Niibe Yutaka
11 * Copyright (C) 2000 Sugioka Toshinobu
12 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
13 * Modified to support SecureEdge. David McCullough (2002)
14 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
15 *
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
18 * for more details.
19 */
20
21#undef DEBUG
22
23#include <linux/config.h>
24#include <linux/module.h>
25#include <linux/errno.h>
26#include <linux/signal.h>
27#include <linux/sched.h>
28#include <linux/timer.h>
29#include <linux/interrupt.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/sysrq.h>
36#include <linux/fcntl.h>
37#include <linux/ptrace.h>
38#include <linux/ioport.h>
39#include <linux/mm.h>
40#include <linux/slab.h>
41#include <linux/init.h>
42#include <linux/delay.h>
43#include <linux/console.h>
44#include <linux/bitops.h>
45
46#ifdef CONFIG_CPU_FREQ
47#include <linux/notifier.h>
48#include <linux/cpufreq.h>
49#endif
50
51#include <asm/system.h>
52#include <asm/io.h>
53#include <asm/irq.h>
54#include <asm/uaccess.h>
55
56#include <linux/generic_serial.h>
57
58#ifdef CONFIG_SH_STANDARD_BIOS
59#include <asm/sh_bios.h>
60#endif
61
62#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
63#define SUPPORT_SYSRQ
64#endif
65
66#include "sh-sci.h"
67
68#ifdef CONFIG_SH_KGDB
69#include <asm/kgdb.h>
70
71static int kgdb_get_char(struct sci_port *port);
72static void kgdb_put_char(struct sci_port *port, char c);
73static void kgdb_handle_error(struct sci_port *port);
74static struct sci_port *kgdb_sci_port;
75#endif /* CONFIG_SH_KGDB */
76
77#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
78static struct sci_port *serial_console_port = 0;
79#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
80
81/* Function prototypes */
82static void sci_stop_tx(struct uart_port *port, unsigned int tty_stop);
83static void sci_start_tx(struct uart_port *port, unsigned int tty_start);
84static void sci_start_rx(struct uart_port *port, unsigned int tty_start);
85static void sci_stop_rx(struct uart_port *port);
86static int sci_request_irq(struct sci_port *port);
87static void sci_free_irq(struct sci_port *port);
88
89static struct sci_port sci_ports[SCI_NPORTS];
90static struct uart_driver sci_uart_driver;
91
92#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
93
94static void handle_error(struct uart_port *port)
95{ /* Clear error flags */
96 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
97}
98
99static int get_char(struct uart_port *port)
100{
101 unsigned long flags;
102 unsigned short status;
103 int c;
104
105 local_irq_save(flags);
106 do {
107 status = sci_in(port, SCxSR);
108 if (status & SCxSR_ERRORS(port)) {
109 handle_error(port);
110 continue;
111 }
112 } while (!(status & SCxSR_RDxF(port)));
113 c = sci_in(port, SCxRDR);
114 sci_in(port, SCxSR); /* Dummy read */
115 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
116 local_irq_restore(flags);
117
118 return c;
119}
120
121/* Taken from sh-stub.c of GDB 4.18 */
122static const char hexchars[] = "0123456789abcdef";
123
124static __inline__ char highhex(int x)
125{
126 return hexchars[(x >> 4) & 0xf];
127}
128
129static __inline__ char lowhex(int x)
130{
131 return hexchars[x & 0xf];
132}
133
134#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
135
136/*
137 * Send the packet in buffer. The host gets one chance to read it.
138 * This routine does not wait for a positive acknowledge.
139 */
140
141#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
142static void put_char(struct uart_port *port, char c)
143{
144 unsigned long flags;
145 unsigned short status;
146
147 local_irq_save(flags);
148
149 do {
150 status = sci_in(port, SCxSR);
151 } while (!(status & SCxSR_TDxE(port)));
152
153 sci_out(port, SCxTDR, c);
154 sci_in(port, SCxSR); /* Dummy read */
155 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
156
157 local_irq_restore(flags);
158}
159
160static void put_string(struct sci_port *sci_port, const char *buffer, int count)
161{
162 struct uart_port *port = &sci_port->port;
163 const unsigned char *p = buffer;
164 int i;
165
166#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
167 int checksum;
168 int usegdb=0;
169
170#ifdef CONFIG_SH_STANDARD_BIOS
171 /* This call only does a trap the first time it is
172 * called, and so is safe to do here unconditionally
173 */
174 usegdb |= sh_bios_in_gdb_mode();
175#endif
176#ifdef CONFIG_SH_KGDB
177 usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));
178#endif
179
180 if (usegdb) {
181 /* $<packet info>#<checksum>. */
182 do {
183 unsigned char c;
184 put_char(port, '$');
185 put_char(port, 'O'); /* 'O'utput to console */
186 checksum = 'O';
187
188 for (i=0; i<count; i++) { /* Don't use run length encoding */
189 int h, l;
190
191 c = *p++;
192 h = highhex(c);
193 l = lowhex(c);
194 put_char(port, h);
195 put_char(port, l);
196 checksum += h + l;
197 }
198 put_char(port, '#');
199 put_char(port, highhex(checksum));
200 put_char(port, lowhex(checksum));
201 } while (get_char(port) != '+');
202 } else
203#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
204 for (i=0; i<count; i++) {
205 if (*p == 10)
206 put_char(port, '\r');
207 put_char(port, *p++);
208 }
209}
210#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
211
212
213#ifdef CONFIG_SH_KGDB
214
215/* Is the SCI ready, ie is there a char waiting? */
216static int kgdb_is_char_ready(struct sci_port *port)
217{
218 unsigned short status = sci_in(port, SCxSR);
219
220 if (status & (SCxSR_ERRORS(port) | SCxSR_BRK(port)))
221 kgdb_handle_error(port);
222
223 return (status & SCxSR_RDxF(port));
224}
225
226/* Write a char */
227static void kgdb_put_char(struct sci_port *port, char c)
228{
229 unsigned short status;
230
231 do
232 status = sci_in(port, SCxSR);
233 while (!(status & SCxSR_TDxE(port)));
234
235 sci_out(port, SCxTDR, c);
236 sci_in(port, SCxSR); /* Dummy read */
237 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
238}
239
240/* Get a char if there is one, else ret -1 */
241static int kgdb_get_char(struct sci_port *port)
242{
243 int c;
244
245 if (kgdb_is_char_ready(port) == 0)
246 c = -1;
247 else {
248 c = sci_in(port, SCxRDR);
249 sci_in(port, SCxSR); /* Dummy read */
250 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
251 }
252
253 return c;
254}
255
256/* Called from kgdbstub.c to get a character, i.e. is blocking */
257static int kgdb_sci_getchar(void)
258{
259 volatile int c;
260
261 /* Keep trying to read a character, this could be neater */
262 while ((c = kgdb_get_char(kgdb_sci_port)) < 0);
263
264 return c;
265}
266
267/* Called from kgdbstub.c to put a character, just a wrapper */
268static void kgdb_sci_putchar(int c)
269{
270
271 kgdb_put_char(kgdb_sci_port, c);
272}
273
274/* Clear any errors on the SCI */
275static void kgdb_handle_error(struct sci_port *port)
276{
277 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); /* Clear error flags */
278}
279
280/* Breakpoint if there's a break sent on the serial port */
281static void kgdb_break_interrupt(int irq, void *ptr, struct pt_regs *regs)
282{
283 struct sci_port *port = ptr;
284 unsigned short status = sci_in(port, SCxSR);
285
286 if (status & SCxSR_BRK(port)) {
287
288 /* Break into the debugger if a break is detected */
289 BREAKPOINT();
290
291 /* Clear */
292 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
293 }
294}
295
296#endif /* CONFIG_SH_KGDB */
297
298#if defined(__H8300S__)
299enum { sci_disable, sci_enable };
300
301static void h8300_sci_enable(struct uart_port* port, unsigned int ctrl)
302{
303 volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
304 int ch = (port->mapbase - SMR0) >> 3;
305 unsigned char mask = 1 << (ch+1);
306
307 if (ctrl == sci_disable) {
308 *mstpcrl |= mask;
309 } else {
310 *mstpcrl &= ~mask;
311 }
312}
313#endif
314
315#if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
316#if defined(__H8300H__) || defined(__H8300S__)
317static void sci_init_pins_sci(struct uart_port* port, unsigned int cflag)
318{
319 int ch = (port->mapbase - SMR0) >> 3;
320
321 /* set DDR regs */
322 H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].rx,H8300_GPIO_INPUT);
323 H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].tx,H8300_GPIO_OUTPUT);
324 /* tx mark output*/
325 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
326}
327#else
328static void sci_init_pins_sci(struct uart_port *port, unsigned int cflag)
329{
330}
331#endif
332#endif
333
334#if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
335#if defined(CONFIG_CPU_SH3)
336/* For SH7705, SH7707, SH7709, SH7709A, SH7729, SH7300*/
337static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
338{
339 unsigned int fcr_val = 0;
340#if !defined(CONFIG_CPU_SUBTYPE_SH7300) /* SH7300 doesn't use RTS/CTS */
341 {
342 unsigned short data;
343
344 /* We need to set SCPCR to enable RTS/CTS */
345 data = ctrl_inw(SCPCR);
346 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
347 ctrl_outw(data&0x0fcf, SCPCR);
348 }
349 if (cflag & CRTSCTS)
350 fcr_val |= SCFCR_MCE;
351 else {
352 unsigned short data;
353
354 /* We need to set SCPCR to enable RTS/CTS */
355 data = ctrl_inw(SCPCR);
356 /* Clear out SCP7MD1,0, SCP4MD1,0,
357 Set SCP6MD1,0 = {01} (output) */
358 ctrl_outw((data&0x0fcf)|0x1000, SCPCR);
359
360 data = ctrl_inb(SCPDR);
361 /* Set /RTS2 (bit6) = 0 */
362 ctrl_outb(data&0xbf, SCPDR);
363 }
364#endif
365 sci_out(port, SCFCR, fcr_val);
366}
367
368static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
369{
370 unsigned int fcr_val = 0;
371
372 if (cflag & CRTSCTS)
373 fcr_val |= SCFCR_MCE;
374
375 sci_out(port, SCFCR, fcr_val);
376}
377
378#else
379
380/* For SH7750 */
381static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
382{
383 unsigned int fcr_val = 0;
384
385 if (cflag & CRTSCTS) {
386 fcr_val |= SCFCR_MCE;
387 } else {
388 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
389 }
390 sci_out(port, SCFCR, fcr_val);
391}
392
393#endif
394#endif /* SCIF_ONLY || SCI_AND_SCIF */
395
396/* ********************************************************************** *
397 * the interrupt related routines *
398 * ********************************************************************** */
399
400static void sci_transmit_chars(struct uart_port *port)
401{
402 struct circ_buf *xmit = &port->info->xmit;
403 unsigned int stopped = uart_tx_stopped(port);
404 unsigned long flags;
405 unsigned short status;
406 unsigned short ctrl;
407 int count, txroom;
408
409 status = sci_in(port, SCxSR);
410 if (!(status & SCxSR_TDxE(port))) {
411 local_irq_save(flags);
412 ctrl = sci_in(port, SCSCR);
413 if (uart_circ_empty(xmit)) {
414 ctrl &= ~SCI_CTRL_FLAGS_TIE;
415 } else {
416 ctrl |= SCI_CTRL_FLAGS_TIE;
417 }
418 sci_out(port, SCSCR, ctrl);
419 local_irq_restore(flags);
420 return;
421 }
422
423#if !defined(SCI_ONLY)
424 if (port->type == PORT_SCIF) {
425 txroom = SCIF_TXROOM_MAX - (sci_in(port, SCFDR)>>8);
426 } else {
427 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
428 }
429#else
430 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
431#endif
432
433 count = txroom;
434
435 do {
436 unsigned char c;
437
438 if (port->x_char) {
439 c = port->x_char;
440 port->x_char = 0;
441 } else if (!uart_circ_empty(xmit) && !stopped) {
442 c = xmit->buf[xmit->tail];
443 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
444 } else {
445 break;
446 }
447
448 sci_out(port, SCxTDR, c);
449
450 port->icount.tx++;
451 } while (--count > 0);
452
453 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
454
455 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
456 uart_write_wakeup(port);
457 if (uart_circ_empty(xmit)) {
458 sci_stop_tx(port, 0);
459 } else {
460 local_irq_save(flags);
461 ctrl = sci_in(port, SCSCR);
462
463#if !defined(SCI_ONLY)
464 if (port->type == PORT_SCIF) {
465 sci_in(port, SCxSR); /* Dummy read */
466 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
467 }
468#endif
469
470 ctrl |= SCI_CTRL_FLAGS_TIE;
471 sci_out(port, SCSCR, ctrl);
472 local_irq_restore(flags);
473 }
474}
475
476/* On SH3, SCIF may read end-of-break as a space->mark char */
477#define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
478
479static inline void sci_receive_chars(struct uart_port *port,
480 struct pt_regs *regs)
481{
482 struct tty_struct *tty = port->info->tty;
483 int i, count, copied = 0;
484 unsigned short status;
485
486 status = sci_in(port, SCxSR);
487 if (!(status & SCxSR_RDxF(port)))
488 return;
489
490 while (1) {
491#if !defined(SCI_ONLY)
492 if (port->type == PORT_SCIF) {
493 count = sci_in(port, SCFDR)&SCIF_RFDC_MASK ;
494 } else {
495 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
496 }
497#else
498 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
499#endif
500
501 /* Don't copy more bytes than there is room for in the buffer */
502 if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
503 count = TTY_FLIPBUF_SIZE - tty->flip.count;
504
505 /* If for any reason we can't copy more data, we're done! */
506 if (count == 0)
507 break;
508
509 if (port->type == PORT_SCI) {
510 char c = sci_in(port, SCxRDR);
511 if(((struct sci_port *)port)->break_flag
512 || uart_handle_sysrq_char(port, c, regs)) {
513 count = 0;
514 } else {
515 tty->flip.char_buf_ptr[0] = c;
516 tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
517 }
518 } else {
519 for (i=0; i<count; i++) {
520 char c = sci_in(port, SCxRDR);
521 status = sci_in(port, SCxSR);
522#if defined(CONFIG_CPU_SH3)
523 /* Skip "chars" during break */
524 if (((struct sci_port *)port)->break_flag) {
525 if ((c == 0) &&
526 (status & SCxSR_FER(port))) {
527 count--; i--;
528 continue;
529 }
530 /* Nonzero => end-of-break */
531 pr_debug("scif: debounce<%02x>\n", c);
532 ((struct sci_port *)port)->break_flag = 0;
533 if (STEPFN(c)) {
534 count--; i--;
535 continue;
536 }
537 }
538#endif /* CONFIG_CPU_SH3 */
539 if (uart_handle_sysrq_char(port, c, regs)) {
540 count--; i--;
541 continue;
542 }
543
544 /* Store data and status */
545 tty->flip.char_buf_ptr[i] = c;
546 if (status&SCxSR_FER(port)) {
547 tty->flip.flag_buf_ptr[i] = TTY_FRAME;
548 pr_debug("sci: frame error\n");
549 } else if (status&SCxSR_PER(port)) {
550 tty->flip.flag_buf_ptr[i] = TTY_PARITY;
551 pr_debug("sci: parity error\n");
552 } else {
553 tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
554 }
555 }
556 }
557
558 sci_in(port, SCxSR); /* dummy read */
559 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
560
561 /* Update the kernel buffer end */
562 tty->flip.count += count;
563 tty->flip.char_buf_ptr += count;
564 tty->flip.flag_buf_ptr += count;
565 copied += count;
566 port->icount.rx += count;
567 }
568
569 if (copied) {
570 /* Tell the rest of the system the news. New characters! */
571 tty_flip_buffer_push(tty);
572 } else {
573 sci_in(port, SCxSR); /* dummy read */
574 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
575 }
576}
577
578#define SCI_BREAK_JIFFIES (HZ/20)
579/* The sci generates interrupts during the break,
580 * 1 per millisecond or so during the break period, for 9600 baud.
581 * So dont bother disabling interrupts.
582 * But dont want more than 1 break event.
583 * Use a kernel timer to periodically poll the rx line until
584 * the break is finished.
585 */
586static void sci_schedule_break_timer(struct sci_port *port)
587{
588 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
589 add_timer(&port->break_timer);
590}
591/* Ensure that two consecutive samples find the break over. */
592static void sci_break_timer(unsigned long data)
593{
594 struct sci_port * port = (struct sci_port *)data;
595 if(sci_rxd_in(&port->port) == 0) {
596 port->break_flag = 1;
597 sci_schedule_break_timer(port);
598 } else if(port->break_flag == 1){
599 /* break is over. */
600 port->break_flag = 2;
601 sci_schedule_break_timer(port);
602 } else port->break_flag = 0;
603}
604
605static inline int sci_handle_errors(struct uart_port *port)
606{
607 int copied = 0;
608 unsigned short status = sci_in(port, SCxSR);
609 struct tty_struct *tty = port->info->tty;
610
611 if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
612 /* overrun error */
613 copied++;
614 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
615 pr_debug("sci: overrun error\n");
616 }
617
618 if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
619 if (sci_rxd_in(port) == 0) {
620 /* Notify of BREAK */
621 struct sci_port * sci_port = (struct sci_port *)port;
622 if(!sci_port->break_flag) {
623 sci_port->break_flag = 1;
624 sci_schedule_break_timer((struct sci_port *)port);
625 /* Do sysrq handling. */
626 if(uart_handle_break(port)) {
627 return 0;
628 }
629 pr_debug("sci: BREAK detected\n");
630 copied++;
631 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
632 }
633 }
634 else {
635 /* frame error */
636 copied++;
637 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
638 pr_debug("sci: frame error\n");
639 }
640 }
641
642 if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
643 /* parity error */
644 copied++;
645 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
646 pr_debug("sci: parity error\n");
647 }
648
649 if (copied) {
650 tty->flip.count += copied;
651 tty_flip_buffer_push(tty);
652 }
653
654 return copied;
655}
656
657static inline int sci_handle_breaks(struct uart_port *port)
658{
659 int copied = 0;
660 unsigned short status = sci_in(port, SCxSR);
661 struct tty_struct *tty = port->info->tty;
662 struct sci_port *s = &sci_ports[port->line];
663
664 if (!s->break_flag && status & SCxSR_BRK(port) &&
665 tty->flip.count < TTY_FLIPBUF_SIZE) {
666#if defined(CONFIG_CPU_SH3)
667 /* Debounce break */
668 s->break_flag = 1;
669#endif
670 /* Notify of BREAK */
671 copied++;
672 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
673 pr_debug("sci: BREAK detected\n");
674 }
675
676#if defined(SCIF_ORER)
677 /* XXX: Handle SCIF overrun error */
678 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
679 sci_out(port, SCLSR, 0);
680 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
681 copied++;
682 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
683 pr_debug("sci: overrun error\n");
684 }
685 }
686#endif
687
688 if (copied) {
689 tty->flip.count += copied;
690 tty_flip_buffer_push(tty);
691 }
692
693 return copied;
694}
695
696static irqreturn_t sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
697{
698 struct uart_port *port = ptr;
699
700 /* I think sci_receive_chars has to be called irrespective
701 * of whether the I_IXOFF is set, otherwise, how is the interrupt
702 * to be disabled?
703 */
704 sci_receive_chars(port, regs);
705
706 return IRQ_HANDLED;
707}
708
709static irqreturn_t sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
710{
711 struct uart_port *port = ptr;
712
713 sci_transmit_chars(port);
714
715 return IRQ_HANDLED;
716}
717
718static irqreturn_t sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
719{
720 struct uart_port *port = ptr;
721
722 /* Handle errors */
723 if (port->type == PORT_SCI) {
724 if (sci_handle_errors(port)) {
725 /* discard character in rx buffer */
726 sci_in(port, SCxSR);
727 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
728 }
729 } else {
730#if defined(SCIF_ORER)
731 if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
732 struct tty_struct *tty = port->info->tty;
733
734 sci_out(port, SCLSR, 0);
735 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
736 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
737 tty->flip.count++;
738 tty_flip_buffer_push(tty);
739 pr_debug("scif: overrun error\n");
740 }
741 }
742#endif
743 sci_rx_interrupt(irq, ptr, regs);
744 }
745
746 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
747
748 /* Kick the transmission */
749 sci_tx_interrupt(irq, ptr, regs);
750
751 return IRQ_HANDLED;
752}
753
754static irqreturn_t sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
755{
756 struct uart_port *port = ptr;
757
758 /* Handle BREAKs */
759 sci_handle_breaks(port);
760 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
761
762 return IRQ_HANDLED;
763}
764
765static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr, struct pt_regs *regs)
766{
767 unsigned short ssr_status, scr_status;
768 struct uart_port *port = ptr;
769
770 ssr_status = sci_in(port,SCxSR);
771 scr_status = sci_in(port,SCSCR);
772
773 /* Tx Interrupt */
774 if ((ssr_status&0x0020) && (scr_status&0x0080))
775 sci_tx_interrupt(irq, ptr, regs);
776 /* Rx Interrupt */
777 if ((ssr_status&0x0002) && (scr_status&0x0040))
778 sci_rx_interrupt(irq, ptr, regs);
779 /* Error Interrupt */
780 if ((ssr_status&0x0080) && (scr_status&0x0400))
781 sci_er_interrupt(irq, ptr, regs);
782 /* Break Interrupt */
783 if ((ssr_status&0x0010) && (scr_status&0x0200))
784 sci_br_interrupt(irq, ptr, regs);
785
786 return IRQ_HANDLED;
787}
788
789#ifdef CONFIG_CPU_FREQ
790/*
791 * Here we define a transistion notifier so that we can update all of our
792 * ports' baud rate when the peripheral clock changes.
793 */
794static int sci_notifier(struct notifier_block *self, unsigned long phase, void *p)
795{
796 struct cpufreq_freqs *freqs = p;
797 int i;
798
799 if ((phase == CPUFREQ_POSTCHANGE) ||
800 (phase == CPUFREQ_RESUMECHANGE)){
801 for (i = 0; i < SCI_NPORTS; i++) {
802 struct uart_port *port = &sci_ports[i].port;
803
804 /*
805 * Update the uartclk per-port if frequency has
806 * changed, since it will no longer necessarily be
807 * consistent with the old frequency.
808 *
809 * Really we want to be able to do something like
810 * uart_change_speed() or something along those lines
811 * here to implicitly reset the per-port baud rate..
812 *
813 * Clean this up later..
814 */
815 port->uartclk = current_cpu_data.module_clock * 16;
816 }
817
818 printk("%s: got a postchange notification for cpu %d (old %d, new %d)\n",
819 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
820 }
821
822 return NOTIFY_OK;
823}
824
825static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
826#endif /* CONFIG_CPU_FREQ */
827
828static int sci_request_irq(struct sci_port *port)
829{
830 int i;
831 irqreturn_t (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
832 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
833 sci_br_interrupt,
834 };
835 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
836 "SCI Transmit Data Empty", "SCI Break" };
837
838 if (port->irqs[0] == port->irqs[1]) {
839 if (!port->irqs[0]) {
840 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
841 return -ENODEV;
842 }
843 if (request_irq(port->irqs[0], sci_mpxed_interrupt, SA_INTERRUPT,
844 "sci", port)) {
845 printk(KERN_ERR "sci: Cannot allocate irq.\n");
846 return -ENODEV;
847 }
848 } else {
849 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
850 if (!port->irqs[i])
851 continue;
852 if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
853 desc[i], port)) {
854 printk(KERN_ERR "sci: Cannot allocate irq.\n");
855 return -ENODEV;
856 }
857 }
858 }
859
860 return 0;
861}
862
863static void sci_free_irq(struct sci_port *port)
864{
865 int i;
866
867 if (port->irqs[0] == port->irqs[1]) {
868 if (!port->irqs[0])
869 printk("sci: sci_free_irq error\n");
870 else
871 free_irq(port->irqs[0], port);
872 } else {
873 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
874 if (!port->irqs[i])
875 continue;
876
877 free_irq(port->irqs[i], port);
878 }
879 }
880}
881
882static unsigned int sci_tx_empty(struct uart_port *port)
883{
884 /* Can't detect */
885 return TIOCSER_TEMT;
886}
887
888static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
889{
890 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
891 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
892 /* If you have signals for DTR and DCD, please implement here. */
893}
894
895static unsigned int sci_get_mctrl(struct uart_port *port)
896{
897 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
898 and CTS/RTS */
899
900 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
901}
902
903static void sci_start_tx(struct uart_port *port, unsigned int tty_start)
904{
905 struct sci_port *s = &sci_ports[port->line];
906
907 disable_irq(s->irqs[SCIx_TXI_IRQ]);
908 sci_transmit_chars(port);
909 enable_irq(s->irqs[SCIx_TXI_IRQ]);
910}
911
912static void sci_stop_tx(struct uart_port *port, unsigned int tty_stop)
913{
914 unsigned long flags;
915 unsigned short ctrl;
916
917 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
918 local_irq_save(flags);
919 ctrl = sci_in(port, SCSCR);
920 ctrl &= ~SCI_CTRL_FLAGS_TIE;
921 sci_out(port, SCSCR, ctrl);
922 local_irq_restore(flags);
923}
924
925static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
926{
927 unsigned long flags;
928 unsigned short ctrl;
929
930 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
931 local_irq_save(flags);
932 ctrl = sci_in(port, SCSCR);
933 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
934 sci_out(port, SCSCR, ctrl);
935 local_irq_restore(flags);
936}
937
938static void sci_stop_rx(struct uart_port *port)
939{
940 unsigned long flags;
941 unsigned short ctrl;
942
943 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
944 local_irq_save(flags);
945 ctrl = sci_in(port, SCSCR);
946 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
947 sci_out(port, SCSCR, ctrl);
948 local_irq_restore(flags);
949}
950
951static void sci_enable_ms(struct uart_port *port)
952{
953 /* Nothing here yet .. */
954}
955
956static void sci_break_ctl(struct uart_port *port, int break_state)
957{
958 /* Nothing here yet .. */
959}
960
961static int sci_startup(struct uart_port *port)
962{
963 struct sci_port *s = &sci_ports[port->line];
964
965#if defined(__H8300S__)
966 h8300_sci_enable(port, sci_enable);
967#endif
968
969 sci_request_irq(s);
970 sci_start_tx(port, 1);
971 sci_start_rx(port, 1);
972
973 return 0;
974}
975
976static void sci_shutdown(struct uart_port *port)
977{
978 struct sci_port *s = &sci_ports[port->line];
979
980 sci_stop_rx(port);
981 sci_stop_tx(port, 1);
982 sci_free_irq(s);
983
984#if defined(__H8300S__)
985 h8300_sci_enable(port, sci_disable);
986#endif
987}
988
989static void sci_set_termios(struct uart_port *port, struct termios *termios,
990 struct termios *old)
991{
992 struct sci_port *s = &sci_ports[port->line];
993 unsigned int status, baud, smr_val;
994 unsigned long flags;
995 int t;
996
997 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
998
999 spin_lock_irqsave(&port->lock, flags);
1000
1001 do {
1002 status = sci_in(port, SCxSR);
1003 } while (!(status & SCxSR_TEND(port)));
1004
1005 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1006
1007#if !defined(SCI_ONLY)
1008 if (port->type == PORT_SCIF) {
1009 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
1010 }
1011#endif
1012
1013 smr_val = sci_in(port, SCSMR) & 3;
1014 if ((termios->c_cflag & CSIZE) == CS7)
1015 smr_val |= 0x40;
1016 if (termios->c_cflag & PARENB)
1017 smr_val |= 0x20;
1018 if (termios->c_cflag & PARODD)
1019 smr_val |= 0x30;
1020 if (termios->c_cflag & CSTOPB)
1021 smr_val |= 0x08;
1022
1023 uart_update_timeout(port, termios->c_cflag, baud);
1024
1025 sci_out(port, SCSMR, smr_val);
1026
1027 switch (baud) {
1028 case 0: t = -1; break;
1029 case 2400: t = BPS_2400; break;
1030 case 4800: t = BPS_4800; break;
1031 case 9600: t = BPS_9600; break;
1032 case 19200: t = BPS_19200; break;
1033 case 38400: t = BPS_38400; break;
1034 case 57600: t = BPS_57600; break;
1035 case 115200: t = BPS_115200; break;
1036 default: t = SCBRR_VALUE(baud); break;
1037 }
1038
1039 if (t > 0) {
1040 if(t >= 256) {
1041 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1042 t >>= 2;
1043 } else {
1044 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1045 }
1046 sci_out(port, SCBRR, t);
1047 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1048 }
1049
1050 s->init_pins(port, termios->c_cflag);
1051 sci_out(port, SCSCR, SCSCR_INIT(port));
1052
1053 if ((termios->c_cflag & CREAD) != 0)
1054 sci_start_rx(port,0);
1055
1056 spin_unlock_irqrestore(&port->lock, flags);
1057}
1058
1059static const char *sci_type(struct uart_port *port)
1060{
1061 switch (port->type) {
1062 case PORT_SCI: return "sci";
1063 case PORT_SCIF: return "scif";
1064 case PORT_IRDA: return "irda";
1065 }
1066
1067 return 0;
1068}
1069
1070static void sci_release_port(struct uart_port *port)
1071{
1072 /* Nothing here yet .. */
1073}
1074
1075static int sci_request_port(struct uart_port *port)
1076{
1077 /* Nothing here yet .. */
1078 return 0;
1079}
1080
1081static void sci_config_port(struct uart_port *port, int flags)
1082{
1083 struct sci_port *s = &sci_ports[port->line];
1084
1085 port->type = s->type;
1086
1087#if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1088 if (port->mapbase == 0)
1089 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1090
1091 port->membase = (void *)port->mapbase;
1092#endif
1093}
1094
1095static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1096{
1097 struct sci_port *s = &sci_ports[port->line];
1098
1099 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
1100 return -EINVAL;
1101 if (ser->baud_base < 2400)
1102 /* No paper tape reader for Mitch.. */
1103 return -EINVAL;
1104
1105 return 0;
1106}
1107
1108static struct uart_ops sci_uart_ops = {
1109 .tx_empty = sci_tx_empty,
1110 .set_mctrl = sci_set_mctrl,
1111 .get_mctrl = sci_get_mctrl,
1112 .start_tx = sci_start_tx,
1113 .stop_tx = sci_stop_tx,
1114 .stop_rx = sci_stop_rx,
1115 .enable_ms = sci_enable_ms,
1116 .break_ctl = sci_break_ctl,
1117 .startup = sci_startup,
1118 .shutdown = sci_shutdown,
1119 .set_termios = sci_set_termios,
1120 .type = sci_type,
1121 .release_port = sci_release_port,
1122 .request_port = sci_request_port,
1123 .config_port = sci_config_port,
1124 .verify_port = sci_verify_port,
1125};
1126
1127static struct sci_port sci_ports[SCI_NPORTS] = {
1128#if defined(CONFIG_CPU_SUBTYPE_SH7708)
1129 {
1130 .port = {
1131 .membase = (void *)0xfffffe80,
1132 .mapbase = 0xfffffe80,
1133 .iotype = SERIAL_IO_MEM,
1134 .irq = 25,
1135 .ops = &sci_uart_ops,
1136 .flags = ASYNC_BOOT_AUTOCONF,
1137 .line = 0,
1138 },
1139 .type = PORT_SCI,
1140 .irqs = SCI_IRQS,
1141 .init_pins = sci_init_pins_sci,
1142 },
1143#elif defined(CONFIG_CPU_SUBTYPE_SH7705)
1144 {
1145 .port = {
1146 .membase = (void *)SCIF0,
1147 .mapbase = SCIF0,
1148 .iotype = SERIAL_IO_MEM,
1149 .irq = 55,
1150 .ops = &sci_uart_ops,
1151 .flags = ASYNC_BOOT_AUTOCONF,
1152 .line = 0,
1153 },
1154 .type = PORT_SCIF,
1155 .irqs = SH3_IRDA_IRQS,
1156 .init_pins = sci_init_pins_scif,
1157 },
1158 {
1159 .port = {
1160 .membase = (void *)SCIF2,
1161 .mapbase = SCIF2,
1162 .iotype = SERIAL_IO_MEM,
1163 .irq = 59,
1164 .ops = &sci_uart_ops,
1165 .flags = ASYNC_BOOT_AUTOCONF,
1166 .line = 1,
1167 },
1168 .type = PORT_SCIF,
1169 .irqs = SH3_SCIF_IRQS,
1170 .init_pins = sci_init_pins_scif,
1171 }
1172#elif defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
1173 {
1174 .port = {
1175 .membase = (void *)0xfffffe80,
1176 .mapbase = 0xfffffe80,
1177 .iotype = SERIAL_IO_MEM,
1178 .irq = 25,
1179 .ops = &sci_uart_ops,
1180 .flags = ASYNC_BOOT_AUTOCONF,
1181 .line = 0,
1182 },
1183 .type = PORT_SCI,
1184 .irqs = SCI_IRQS,
1185 .init_pins = sci_init_pins_sci,
1186 },
1187 {
1188 .port = {
1189 .membase = (void *)0xa4000150,
1190 .mapbase = 0xa4000150,
1191 .iotype = SERIAL_IO_MEM,
1192 .irq = 59,
1193 .ops = &sci_uart_ops,
1194 .flags = ASYNC_BOOT_AUTOCONF,
1195 .line = 1,
1196 },
1197 .type = PORT_SCIF,
1198 .irqs = SH3_SCIF_IRQS,
1199 .init_pins = sci_init_pins_scif,
1200 },
1201 {
1202 .port = {
1203 .membase = (void *)0xa4000140,
1204 .mapbase = 0xa4000140,
1205 .iotype = SERIAL_IO_MEM,
1206 .irq = 55,
1207 .ops = &sci_uart_ops,
1208 .flags = ASYNC_BOOT_AUTOCONF,
1209 .line = 2,
1210 },
1211 .type = PORT_IRDA,
1212 .irqs = SH3_IRDA_IRQS,
1213 .init_pins = sci_init_pins_irda,
1214 }
1215#elif defined(CONFIG_CPU_SUBTYPE_SH7300)
1216 {
1217 .port = {
1218 .membase = (void *)0xA4430000,
1219 .mapbase = 0xA4430000,
1220 .iotype = SERIAL_IO_MEM,
1221 .irq = 25,
1222 .ops = &sci_uart_ops,
1223 .flags = ASYNC_BOOT_AUTOCONF,
1224 .line = 0,
1225 },
1226 .type = PORT_SCIF,
1227 .irqs = SH7300_SCIF0_IRQS,
1228 .init_pins = sci_init_pins_scif,
1229 },
1230#elif defined(CONFIG_CPU_SUBTYPE_SH73180)
1231 {
1232 .port = {
1233 .membase = (void *)0xffe00000,
1234 .mapbase = 0xffe00000,
1235 .iotype = SERIAL_IO_MEM,
1236 .irq = 25,
1237 .ops = &sci_uart_ops,
1238 .flags = ASYNC_BOOT_AUTOCONF,
1239 .line = 0,
1240 },
1241 .type = PORT_SCIF,
1242 .irqs = SH73180_SCIF_IRQS,
1243 .init_pins = sci_init_pins_scif,
1244 },
1245#elif defined(CONFIG_SH_RTS7751R2D)
1246 {
1247 .port = {
1248 .membase = (void *)0xffe80000,
1249 .mapbase = 0xffe80000,
1250 .iotype = SERIAL_IO_MEM,
1251 .irq = 43,
1252 .ops = &sci_uart_ops,
1253 .flags = ASYNC_BOOT_AUTOCONF,
1254 .line = 0,
1255 },
1256 .type = PORT_SCIF,
1257 .irqs = SH4_SCIF_IRQS,
1258 .init_pins = sci_init_pins_scif,
1259 },
1260#elif defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_SH7751)
1261 {
1262 .port = {
1263 .membase = (void *)0xffe00000,
1264 .mapbase = 0xffe00000,
1265 .iotype = SERIAL_IO_MEM,
1266 .irq = 25,
1267 .ops = &sci_uart_ops,
1268 .flags = ASYNC_BOOT_AUTOCONF,
1269 .line = 0,
1270 },
1271 .type = PORT_SCI,
1272 .irqs = SCI_IRQS,
1273 .init_pins = sci_init_pins_sci,
1274 },
1275 {
1276 .port = {
1277 .membase = (void *)0xffe80000,
1278 .mapbase = 0xffe80000,
1279 .iotype = SERIAL_IO_MEM,
1280 .irq = 43,
1281 .ops = &sci_uart_ops,
1282 .flags = ASYNC_BOOT_AUTOCONF,
1283 .line = 1,
1284 },
1285 .type = PORT_SCIF,
1286 .irqs = SH4_SCIF_IRQS,
1287 .init_pins = sci_init_pins_scif,
1288 },
1289#elif defined(CONFIG_CPU_SUBTYPE_SH7760)
1290 {
1291 .port = {
1292 .membase = (void *)0xfe600000,
1293 .mapbase = 0xfe600000,
1294 .iotype = SERIAL_IO_MEM,
1295 .irq = 55,
1296 .ops = &sci_uart_ops,
1297 .flags = ASYNC_BOOT_AUTOCONF,
1298 .line = 0,
1299 },
1300 .type = PORT_SCIF,
1301 .irqs = SH7760_SCIF0_IRQS,
1302 .init_pins = sci_init_pins_scif,
1303 },
1304 {
1305 .port = {
1306 .membase = (void *)0xfe610000,
1307 .mapbase = 0xfe610000,
1308 .iotype = SERIAL_IO_MEM,
1309 .irq = 75,
1310 .ops = &sci_uart_ops,
1311 .flags = ASYNC_BOOT_AUTOCONF,
1312 .line = 1,
1313 },
1314 .type = PORT_SCIF,
1315 .irqs = SH7760_SCIF1_IRQS,
1316 .init_pins = sci_init_pins_scif,
1317 },
1318 {
1319 .port = {
1320 .membase = (void *)0xfe620000,
1321 .mapbase = 0xfe620000,
1322 .iotype = SERIAL_IO_MEM,
1323 .irq = 79,
1324 .ops = &sci_uart_ops,
1325 .flags = ASYNC_BOOT_AUTOCONF,
1326 .line = 2,
1327 },
1328 .type = PORT_SCIF,
1329 .irqs = SH7760_SCIF2_IRQS,
1330 .init_pins = sci_init_pins_scif,
1331 },
1332#elif defined(CONFIG_CPU_SUBTYPE_SH4_202)
1333 {
1334 .port = {
1335 .membase = (void *)0xffe80000,
1336 .mapbase = 0xffe80000,
1337 .iotype = SERIAL_IO_MEM,
1338 .irq = 43,
1339 .ops = &sci_uart_ops,
1340 .flags = ASYNC_BOOT_AUTOCONF,
1341 .line = 0,
1342 },
1343 .type = PORT_SCIF,
1344 .irqs = SH4_SCIF_IRQS,
1345 .init_pins = sci_init_pins_scif,
1346 },
1347#elif defined(CONFIG_CPU_SUBTYPE_ST40STB1)
1348 {
1349 .port = {
1350 .membase = (void *)0xffe00000,
1351 .mapbase = 0xffe00000,
1352 .iotype = SERIAL_IO_MEM,
1353 .irq = 26,
1354 .ops = &sci_uart_ops,
1355 .flags = ASYNC_BOOT_AUTOCONF,
1356 .line = 0,
1357 },
1358 .type = PORT_SCIF,
1359 .irqs = STB1_SCIF1_IRQS,
1360 .init_pins = sci_init_pins_scif,
1361 },
1362 {
1363 .port = {
1364 .membase = (void *)0xffe80000,
1365 .mapbase = 0xffe80000,
1366 .iotype = SERIAL_IO_MEM,
1367 .irq = 43,
1368 .ops = &sci_uart_ops,
1369 .flags = ASYNC_BOOT_AUTOCONF,
1370 .line = 1,
1371 },
1372 .type = PORT_SCIF,
1373 .irqs = SH4_SCIF_IRQS,
1374 .init_pins = sci_init_pins_scif,
1375 },
1376#elif defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1377 {
1378 .port = {
1379 .iotype = SERIAL_IO_MEM,
1380 .irq = 42,
1381 .ops = &sci_uart_ops,
1382 .flags = ASYNC_BOOT_AUTOCONF,
1383 .line = 0,
1384 },
1385 .type = PORT_SCIF,
1386 .irqs = SH5_SCIF_IRQS,
1387 .init_pins = sci_init_pins_scif,
1388 },
1389#elif defined(CONFIG_H83007) || defined(CONFIG_H83068)
1390 {
1391 .port = {
1392 .membase = (void *)0x00ffffb0,
1393 .mapbase = 0x00ffffb0,
1394 .iotype = SERIAL_IO_MEM,
1395 .irq = 54,
1396 .ops = &sci_uart_ops,
1397 .flags = ASYNC_BOOT_AUTOCONF,
1398 .line = 0,
1399 },
1400 .type = PORT_SCI,
1401 .irqs = H8300H_SCI_IRQS0,
1402 .init_pins = sci_init_pins_sci,
1403 },
1404 {
1405 .port = {
1406 .membase = (void *)0x00ffffb8,
1407 .mapbase = 0x00ffffb8,
1408 .iotype = SERIAL_IO_MEM,
1409 .irq = 58,
1410 .ops = &sci_uart_ops,
1411 .flags = ASYNC_BOOT_AUTOCONF,
1412 .line = 1,
1413 },
1414 .type = PORT_SCI,
1415 .irqs = H8300H_SCI_IRQS1,
1416 .init_pins = sci_init_pins_sci,
1417 },
1418 {
1419 .port = {
1420 .membase = (void *)0x00ffffc0,
1421 .mapbase = 0x00ffffc0,
1422 .iotype = SERIAL_IO_MEM,
1423 .irq = 62,
1424 .ops = &sci_uart_ops,
1425 .flags = ASYNC_BOOT_AUTOCONF,
1426 .line = 2,
1427 },
1428 .type = PORT_SCI,
1429 .irqs = H8300H_SCI_IRQS2,
1430 .init_pins = sci_init_pins_sci,
1431 },
1432#elif defined(CONFIG_H8S2678)
1433 {
1434 .port = {
1435 .membase = (void *)0x00ffff78,
1436 .mapbase = 0x00ffff78,
1437 .iotype = SERIAL_IO_MEM,
1438 .irq = 90,
1439 .ops = &sci_uart_ops,
1440 .flags = ASYNC_BOOT_AUTOCONF,
1441 .line = 0,
1442 },
1443 .type = PORT_SCI,
1444 .irqs = H8S_SCI_IRQS0,
1445 .init_pins = sci_init_pins_sci,
1446 },
1447 {
1448 .port = {
1449 .membase = (void *)0x00ffff80,
1450 .mapbase = 0x00ffff80,
1451 .iotype = SERIAL_IO_MEM,
1452 .irq = 94,
1453 .ops = &sci_uart_ops,
1454 .flags = ASYNC_BOOT_AUTOCONF,
1455 .line = 1,
1456 },
1457 .type = PORT_SCI,
1458 .irqs = H8S_SCI_IRQS1,
1459 .init_pins = sci_init_pins_sci,
1460 },
1461 {
1462 .port = {
1463 .membase = (void *)0x00ffff88,
1464 .mapbase = 0x00ffff88,
1465 .iotype = SERIAL_IO_MEM,
1466 .irq = 98,
1467 .ops = &sci_uart_ops,
1468 .flags = ASYNC_BOOT_AUTOCONF,
1469 .line = 2,
1470 },
1471 .type = PORT_SCI,
1472 .irqs = H8S_SCI_IRQS2,
1473 .init_pins = sci_init_pins_sci,
1474 },
1475#else
1476#error "CPU subtype not defined"
1477#endif
1478};
1479
1480#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1481/*
1482 * Print a string to the serial port trying not to disturb
1483 * any possible real use of the port...
1484 */
1485static void serial_console_write(struct console *co, const char *s,
1486 unsigned count)
1487{
1488 put_string(serial_console_port, s, count);
1489}
1490
1491static int __init serial_console_setup(struct console *co, char *options)
1492{
1493 struct uart_port *port;
1494 int baud = 115200;
1495 int bits = 8;
1496 int parity = 'n';
1497 int flow = 'n';
1498 int ret;
1499
1500 if (co->index >= SCI_NPORTS)
1501 co->index = 0;
1502
1503 serial_console_port = &sci_ports[co->index];
1504 port = &serial_console_port->port;
1505 port->type = serial_console_port->type;
1506
1507#ifdef CONFIG_SUPERH64
1508 /* This is especially needed on sh64 to remap the SCIF */
1509 sci_config_port(port, 0);
1510#endif
1511
1512 /*
1513 * We need to set the initial uartclk here, since otherwise it will
1514 * only ever be setup at sci_init() time.
1515 */
1516#if !defined(__H8300H__) && !defined(__H8300S__)
1517 port->uartclk = current_cpu_data.module_clock * 16;
1518#else
1519 port->uartclk = CONFIG_CPU_CLOCK;
1520#endif
1521#if defined(__H8300S__)
1522 h8300_sci_enable(port, sci_enable);
1523#endif
1524 if (options)
1525 uart_parse_options(options, &baud, &parity, &bits, &flow);
1526
1527 ret = uart_set_options(port, co, baud, parity, bits, flow);
1528#if defined(__H8300H__) || defined(__H8300S__)
1529 /* disable rx interrupt */
1530 if (ret == 0)
1531 sci_stop_rx(port);
1532#endif
1533 return ret;
1534}
1535
1536static struct console serial_console = {
1537 .name = "ttySC",
1538 .device = uart_console_device,
1539 .write = serial_console_write,
1540 .setup = serial_console_setup,
1541 .flags = CON_PRINTBUFFER,
1542 .index = -1,
1543 .data = &sci_uart_driver,
1544};
1545
1546static int __init sci_console_init(void)
1547{
1548 register_console(&serial_console);
1549 return 0;
1550}
1551
1552console_initcall(sci_console_init);
1553#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1554
1555#ifdef CONFIG_SH_KGDB
1556/*
1557 * FIXME: Most of this can go away.. at the moment, we rely on
1558 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1559 * most of that can easily be done here instead.
1560 *
1561 * For the time being, just accept the values that were parsed earlier..
1562 */
1563static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1564 int *parity, int *bits)
1565{
1566 *baud = kgdb_baud;
1567 *parity = tolower(kgdb_parity);
1568 *bits = kgdb_bits - '0';
1569}
1570
1571/*
1572 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1573 * care of the early-on initialization for kgdb, regardless of whether we
1574 * actually use kgdb as a console or not.
1575 *
1576 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1577 */
1578int __init kgdb_console_setup(struct console *co, char *options)
1579{
1580 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1581 int baud = 38400;
1582 int bits = 8;
1583 int parity = 'n';
1584 int flow = 'n';
1585
1586 if (co->index >= SCI_NPORTS || co->index != kgdb_portnum)
1587 co->index = kgdb_portnum;
1588
1589 if (options)
1590 uart_parse_options(options, &baud, &parity, &bits, &flow);
1591 else
1592 kgdb_console_get_options(port, &baud, &parity, &bits);
1593
1594 kgdb_getchar = kgdb_sci_getchar;
1595 kgdb_putchar = kgdb_sci_putchar;
1596
1597 return uart_set_options(port, co, baud, parity, bits, flow);
1598}
1599#endif /* CONFIG_SH_KGDB */
1600
1601#ifdef CONFIG_SH_KGDB_CONSOLE
1602static struct console kgdb_console = {
1603 .name = "ttySC",
1604 .write = kgdb_console_write,
1605 .setup = kgdb_console_setup,
1606 .flags = CON_PRINTBUFFER | CON_ENABLED,
1607 .index = -1,
1608 .data = &sci_uart_driver,
1609};
1610
1611/* Register the KGDB console so we get messages (d'oh!) */
1612static int __init kgdb_console_init(void)
1613{
1614 register_console(&kgdb_console);
1615 return 0;
1616}
1617
1618console_initcall(kgdb_console_init);
1619#endif /* CONFIG_SH_KGDB_CONSOLE */
1620
1621#if defined(CONFIG_SH_KGDB_CONSOLE)
1622#define SCI_CONSOLE &kgdb_console
1623#elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1624#define SCI_CONSOLE &serial_console
1625#else
1626#define SCI_CONSOLE 0
1627#endif
1628
1629static char banner[] __initdata =
1630 KERN_INFO "SuperH SCI(F) driver initialized\n";
1631
1632static struct uart_driver sci_uart_driver = {
1633 .owner = THIS_MODULE,
1634 .driver_name = "sci",
1635#ifdef CONFIG_DEVFS_FS
1636 .devfs_name = "ttsc/",
1637#endif
1638 .dev_name = "ttySC",
1639 .major = SCI_MAJOR,
1640 .minor = SCI_MINOR_START,
1641 .nr = SCI_NPORTS,
1642 .cons = SCI_CONSOLE,
1643};
1644
1645static int __init sci_init(void)
1646{
1647 int chan, ret;
1648
1649 printk("%s", banner);
1650
1651 ret = uart_register_driver(&sci_uart_driver);
1652 if (ret == 0) {
1653 for (chan = 0; chan < SCI_NPORTS; chan++) {
1654 struct sci_port *sciport = &sci_ports[chan];
1655
1656#if !defined(__H8300H__) && !defined(__H8300S__)
1657 sciport->port.uartclk = (current_cpu_data.module_clock * 16);
1658#else
1659 sciport->port.uartclk = CONFIG_CPU_CLOCK;
1660#endif
1661 uart_add_one_port(&sci_uart_driver, &sciport->port);
1662 sciport->break_timer.data = (unsigned long)sciport;
1663 sciport->break_timer.function = sci_break_timer;
1664 init_timer(&sciport->break_timer);
1665 }
1666 }
1667
1668#ifdef CONFIG_CPU_FREQ
1669 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
1670 printk("sci: CPU frequency notifier registered\n");
1671#endif
1672
1673#ifdef CONFIG_SH_STANDARD_BIOS
1674 sh_bios_gdb_detach();
1675#endif
1676
1677 return ret;
1678}
1679
1680static void __exit sci_exit(void)
1681{
1682 int chan;
1683
1684 for (chan = 0; chan < SCI_NPORTS; chan++)
1685 uart_remove_one_port(&sci_uart_driver, &sci_ports[chan].port);
1686
1687 uart_unregister_driver(&sci_uart_driver);
1688}
1689
1690module_init(sci_init);
1691module_exit(sci_exit);
1692
diff --git a/drivers/serial/sh-sci.h b/drivers/serial/sh-sci.h
new file mode 100644
index 000000000000..2892169eff05
--- /dev/null
+++ b/drivers/serial/sh-sci.h
@@ -0,0 +1,573 @@
1/* $Id: sh-sci.h,v 1.4 2004/02/19 16:43:56 lethal Exp $
2 *
3 * linux/drivers/serial/sh-sci.h
4 *
5 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
6 * Copyright (C) 1999, 2000 Niibe Yutaka
7 * Copyright (C) 2000 Greg Banks
8 * Copyright (C) 2002, 2003 Paul Mundt
9 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
10 * Modified to support SH7300(SH-Mobile) SCIF. Takashi Kusuda (Jun 2003).
11 * Modified to support H8/300 Series Yoshinori Sato (Feb 2004).
12 */
13#include <linux/config.h>
14#include <linux/serial_core.h>
15
16#if defined(__H8300H__) || defined(__H8300S__)
17#include <asm/gpio.h>
18#if defined(CONFIG_H83007) || defined(CONFIG_H83068)
19#include <asm/regs306x.h>
20#endif
21#if defined(CONFIG_H8S2678)
22#include <asm/regs267x.h>
23#endif
24#endif
25
26/* Offsets into the sci_port->irqs array */
27#define SCIx_ERI_IRQ 0
28#define SCIx_RXI_IRQ 1
29#define SCIx_TXI_IRQ 2
30
31/* ERI, RXI, TXI, BRI */
32#define SCI_IRQS { 23, 24, 25, 0 }
33#define SH3_SCIF_IRQS { 56, 57, 59, 58 }
34#define SH3_IRDA_IRQS { 52, 53, 55, 54 }
35#define SH4_SCIF_IRQS { 40, 41, 43, 42 }
36#define STB1_SCIF1_IRQS {23, 24, 26, 25 }
37#define SH7760_SCIF0_IRQS { 52, 53, 55, 54 }
38#define SH7760_SCIF1_IRQS { 72, 73, 75, 74 }
39#define SH7760_SCIF2_IRQS { 76, 77, 79, 78 }
40#define SH7300_SCIF0_IRQS {80, 80, 80, 80 }
41#define SH73180_SCIF_IRQS {80, 81, 83, 82 }
42#define H8300H_SCI_IRQS0 {52, 53, 54, 0 }
43#define H8300H_SCI_IRQS1 {56, 57, 58, 0 }
44#define H8300H_SCI_IRQS2 {60, 61, 62, 0 }
45#define H8S_SCI_IRQS0 {88, 89, 90, 0 }
46#define H8S_SCI_IRQS1 {92, 93, 94, 0 }
47#define H8S_SCI_IRQS2 {96, 97, 98, 0 }
48#define SH5_SCIF_IRQS {39, 40, 42, 0 }
49
50#if defined(CONFIG_CPU_SUBTYPE_SH7708)
51# define SCI_NPORTS 1
52# define SCSPTR 0xffffff7c /* 8 bit */
53# define SCSCR_INIT(port) 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */
54# define SCI_ONLY
55#elif defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
56# define SCI_NPORTS 3
57# define SCPCR 0xA4000116 /* 16 bit SCI and SCIF */
58# define SCPDR 0xA4000136 /* 8 bit SCI and SCIF */
59# define SCSCR_INIT(port) 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */
60# define SCI_AND_SCIF
61#elif defined(CONFIG_CPU_SUBTYPE_SH7705)
62# define SCIF0 0xA4400000
63# define SCIF2 0xA4410000
64# define SCSMR_Ir 0xA44A0000
65# define IRDA_SCIF SCIF0
66# define SCI_NPORTS 2
67# define SCPCR 0xA4000116
68# define SCPDR 0xA4000136
69
70/* Set the clock source,
71 * SCIF2 (0xA4410000) -> External clock, SCK pin used as clock input
72 * SCIF0 (0xA4400000) -> Internal clock, SCK pin as serial clock output
73 */
74# define SCSCR_INIT(port) (port->mapbase == SCIF2) ? 0xF3 : 0xF0
75# define SCIF_ONLY
76#elif defined(CONFIG_SH_RTS7751R2D)
77# define SCI_NPORTS 1
78# define SCSPTR1 0xffe0001c /* 8 bit SCI */
79# define SCSPTR2 0xFFE80020 /* 16 bit SCIF */
80# define SCIF_ORER 0x0001 /* overrun error bit */
81# define SCSCR_INIT(port) 0x3a /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
82# define SCIF_ONLY
83#elif defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_SH7751)
84# define SCI_NPORTS 2
85# define SCSPTR1 0xffe0001c /* 8 bit SCI */
86# define SCSPTR2 0xFFE80020 /* 16 bit SCIF */
87# define SCIF_ORER 0x0001 /* overrun error bit */
88# define SCSCR_INIT(port) (((port)->type == PORT_SCI) ? \
89 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */ : \
90 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */ )
91# define SCI_AND_SCIF
92#elif defined(CONFIG_CPU_SUBTYPE_SH7760)
93# define SCI_NPORTS 3
94# define SCSPTR0 0xfe600000 /* 16 bit SCIF */
95# define SCSPTR1 0xfe610000 /* 16 bit SCIF */
96# define SCSPTR2 0xfe620000 /* 16 bit SCIF */
97# define SCIF_ORER 0x0001 /* overrun error bit */
98# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
99# define SCIF_ONLY
100#elif defined(CONFIG_CPU_SUBTYPE_SH7300)
101# define SCI_NPORTS 1
102# define SCPCR 0xA4050116 /* 16 bit SCIF */
103# define SCPDR 0xA4050136 /* 16 bit SCIF */
104# define SCSCR_INIT(port) 0x0030 /* TIE=0,RIE=0,TE=1,RE=1 */
105# define SCIF_ONLY
106#elif defined(CONFIG_CPU_SUBTYPE_SH73180)
107# define SCI_NPORTS 1
108# define SCPDR 0xA4050138 /* 16 bit SCIF */
109# define SCSPTR2 SCPDR
110# define SCIF_ORER 0x0001 /* overrun error bit */
111# define SCSCR_INIT(port) 0x0038 /* TIE=0,RIE=0,TE=1,RE=1 */
112# define SCIF_ONLY
113#elif defined(CONFIG_CPU_SUBTYPE_SH4_202)
114# define SCI_NPORTS 1
115# define SCSPTR2 0xffe80020 /* 16 bit SCIF */
116# define SCIF_ORER 0x0001 /* overrun error bit */
117# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
118# define SCIF_ONLY
119#elif defined(CONFIG_CPU_SUBTYPE_ST40STB1)
120# define SCI_NPORTS 2
121# define SCSPTR1 0xffe00020 /* 16 bit SCIF */
122# define SCSPTR2 0xffe80020 /* 16 bit SCIF */
123# define SCIF_ORER 0x0001 /* overrun error bit */
124# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
125# define SCIF_ONLY
126#elif defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
127# include <asm/hardware.h>
128# define SCIF_BASE_ADDR 0x01030000
129# define SCIF_ADDR_SH5 PHYS_PERIPHERAL_BLOCK+SCIF_BASE_ADDR
130# define SCIF_PTR2_OFFS 0x0000020
131# define SCIF_LSR2_OFFS 0x0000024
132# define SCI_NPORTS 1
133# define SCI_INIT { \
134 { {}, PORT_SCIF, 0, \
135 SH5_SCIF_IRQS, sci_init_pins_scif } \
136}
137# define SCSPTR2 ((port->mapbase)+SCIF_PTR2_OFFS) /* 16 bit SCIF */
138# define SCLSR2 ((port->mapbase)+SCIF_LSR2_OFFS) /* 16 bit SCIF */
139# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,
140 TE=1,RE=1,REIE=1 */
141# define SCIF_ONLY
142#elif defined(CONFIG_H83007) || defined(CONFIG_H83068)
143# define SCI_NPORTS 3
144# define SCSCR_INIT(port) 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */
145# define SCI_ONLY
146# define H8300_SCI_DR(ch) *(volatile char *)(P1DR + h8300_sci_pins[ch].port)
147#elif defined(CONFIG_H8S2678)
148# define SCI_NPORTS 3
149# define SCSCR_INIT(port) 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */
150# define SCI_ONLY
151# define H8300_SCI_DR(ch) *(volatile char *)(P1DR + h8300_sci_pins[ch].port)
152#else
153# error CPU subtype not defined
154#endif
155
156/* SCSCR */
157#define SCI_CTRL_FLAGS_TIE 0x80 /* all */
158#define SCI_CTRL_FLAGS_RIE 0x40 /* all */
159#define SCI_CTRL_FLAGS_TE 0x20 /* all */
160#define SCI_CTRL_FLAGS_RE 0x10 /* all */
161#if defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_SH7751)
162#define SCI_CTRL_FLAGS_REIE 0x08 /* 7750 SCIF */
163#else
164#define SCI_CTRL_FLAGS_REIE 0
165#endif
166/* SCI_CTRL_FLAGS_MPIE 0x08 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
167/* SCI_CTRL_FLAGS_TEIE 0x04 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
168/* SCI_CTRL_FLAGS_CKE1 0x02 * all */
169/* SCI_CTRL_FLAGS_CKE0 0x01 * 7707 SCI/SCIF, 7708 SCI, 7709 SCI/SCIF, 7750 SCI */
170
171/* SCxSR SCI */
172#define SCI_TDRE 0x80 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
173#define SCI_RDRF 0x40 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
174#define SCI_ORER 0x20 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
175#define SCI_FER 0x10 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
176#define SCI_PER 0x08 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
177#define SCI_TEND 0x04 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
178/* SCI_MPB 0x02 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
179/* SCI_MPBT 0x01 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
180
181#define SCI_ERRORS ( SCI_PER | SCI_FER | SCI_ORER)
182
183/* SCxSR SCIF */
184#define SCIF_ER 0x0080 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
185#define SCIF_TEND 0x0040 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
186#define SCIF_TDFE 0x0020 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
187#define SCIF_BRK 0x0010 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
188#define SCIF_FER 0x0008 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
189#define SCIF_PER 0x0004 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
190#define SCIF_RDF 0x0002 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
191#define SCIF_DR 0x0001 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
192
193#if defined(CONFIG_CPU_SUBTYPE_SH7300) || defined(CONFIG_CPU_SUBTYPE_SH7705)
194#define SCIF_ORER 0x0200
195#define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK | SCIF_ORER)
196#define SCIF_RFDC_MASK 0x007f
197#define SCIF_TXROOM_MAX 64
198#else
199#define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK)
200#define SCIF_RFDC_MASK 0x001f
201#define SCIF_TXROOM_MAX 16
202#endif
203
204#if defined(SCI_ONLY)
205# define SCxSR_TEND(port) SCI_TEND
206# define SCxSR_ERRORS(port) SCI_ERRORS
207# define SCxSR_RDxF(port) SCI_RDRF
208# define SCxSR_TDxE(port) SCI_TDRE
209# define SCxSR_ORER(port) SCI_ORER
210# define SCxSR_FER(port) SCI_FER
211# define SCxSR_PER(port) SCI_PER
212# define SCxSR_BRK(port) 0x00
213# define SCxSR_RDxF_CLEAR(port) 0xbc
214# define SCxSR_ERROR_CLEAR(port) 0xc4
215# define SCxSR_TDxE_CLEAR(port) 0x78
216# define SCxSR_BREAK_CLEAR(port) 0xc4
217#elif defined(SCIF_ONLY)
218# define SCxSR_TEND(port) SCIF_TEND
219# define SCxSR_ERRORS(port) SCIF_ERRORS
220# define SCxSR_RDxF(port) SCIF_RDF
221# define SCxSR_TDxE(port) SCIF_TDFE
222#if defined(CONFIG_CPU_SUBTYPE_SH7300) || defined(CONFIG_CPU_SUBTYPE_SH7705)
223# define SCxSR_ORER(port) SCIF_ORER
224#else
225# define SCxSR_ORER(port) 0x0000
226#endif
227# define SCxSR_FER(port) SCIF_FER
228# define SCxSR_PER(port) SCIF_PER
229# define SCxSR_BRK(port) SCIF_BRK
230#if defined(CONFIG_CPU_SUBTYPE_SH7300) || defined(CONFIG_CPU_SUBTYPE_SH7705)
231# define SCxSR_RDxF_CLEAR(port) (sci_in(port,SCxSR)&0xfffc)
232# define SCxSR_ERROR_CLEAR(port) (sci_in(port,SCxSR)&0xfd73)
233# define SCxSR_TDxE_CLEAR(port) (sci_in(port,SCxSR)&0xffdf)
234# define SCxSR_BREAK_CLEAR(port) (sci_in(port,SCxSR)&0xffe3)
235#else
236/* SH7705 can also use this, clearing is same between 7705 and 7709 and 7300 */
237# define SCxSR_RDxF_CLEAR(port) 0x00fc
238# define SCxSR_ERROR_CLEAR(port) 0x0073
239# define SCxSR_TDxE_CLEAR(port) 0x00df
240# define SCxSR_BREAK_CLEAR(port) 0x00e3
241#endif
242#else
243# define SCxSR_TEND(port) (((port)->type == PORT_SCI) ? SCI_TEND : SCIF_TEND)
244# define SCxSR_ERRORS(port) (((port)->type == PORT_SCI) ? SCI_ERRORS : SCIF_ERRORS)
245# define SCxSR_RDxF(port) (((port)->type == PORT_SCI) ? SCI_RDRF : SCIF_RDF)
246# define SCxSR_TDxE(port) (((port)->type == PORT_SCI) ? SCI_TDRE : SCIF_TDFE)
247# define SCxSR_ORER(port) (((port)->type == PORT_SCI) ? SCI_ORER : 0x0000)
248# define SCxSR_FER(port) (((port)->type == PORT_SCI) ? SCI_FER : SCIF_FER)
249# define SCxSR_PER(port) (((port)->type == PORT_SCI) ? SCI_PER : SCIF_PER)
250# define SCxSR_BRK(port) (((port)->type == PORT_SCI) ? 0x00 : SCIF_BRK)
251# define SCxSR_RDxF_CLEAR(port) (((port)->type == PORT_SCI) ? 0xbc : 0x00fc)
252# define SCxSR_ERROR_CLEAR(port) (((port)->type == PORT_SCI) ? 0xc4 : 0x0073)
253# define SCxSR_TDxE_CLEAR(port) (((port)->type == PORT_SCI) ? 0x78 : 0x00df)
254# define SCxSR_BREAK_CLEAR(port) (((port)->type == PORT_SCI) ? 0xc4 : 0x00e3)
255#endif
256
257/* SCFCR */
258#define SCFCR_RFRST 0x0002
259#define SCFCR_TFRST 0x0004
260#define SCFCR_TCRST 0x4000
261#define SCFCR_MCE 0x0008
262
263#define SCI_MAJOR 204
264#define SCI_MINOR_START 8
265
266/* Generic serial flags */
267#define SCI_RX_THROTTLE 0x0000001
268
269#define SCI_MAGIC 0xbabeface
270
271/*
272 * Events are used to schedule things to happen at timer-interrupt
273 * time, instead of at rs interrupt time.
274 */
275#define SCI_EVENT_WRITE_WAKEUP 0
276
277struct sci_port {
278 struct uart_port port;
279 int type;
280 unsigned char irqs[4]; /* ERI, RXI, TXI, BRI */
281 void (*init_pins)(struct uart_port *port, unsigned int cflag);
282 int break_flag;
283 struct timer_list break_timer;
284};
285
286#define SCI_IN(size, offset) \
287 unsigned int addr = port->mapbase + (offset); \
288 if ((size) == 8) { \
289 return ctrl_inb(addr); \
290 } else { \
291 return ctrl_inw(addr); \
292 }
293#define SCI_OUT(size, offset, value) \
294 unsigned int addr = port->mapbase + (offset); \
295 if ((size) == 8) { \
296 ctrl_outb(value, addr); \
297 } else { \
298 ctrl_outw(value, addr); \
299 }
300
301#define CPU_SCIx_FNS(name, sci_offset, sci_size, scif_offset, scif_size)\
302 static inline unsigned int sci_##name##_in(struct uart_port *port) \
303 { \
304 if (port->type == PORT_SCI) { \
305 SCI_IN(sci_size, sci_offset) \
306 } else { \
307 SCI_IN(scif_size, scif_offset); \
308 } \
309 } \
310 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
311 { \
312 if (port->type == PORT_SCI) { \
313 SCI_OUT(sci_size, sci_offset, value) \
314 } else { \
315 SCI_OUT(scif_size, scif_offset, value); \
316 } \
317 }
318
319#define CPU_SCIF_FNS(name, scif_offset, scif_size) \
320 static inline unsigned int sci_##name##_in(struct uart_port *port) \
321 { \
322 SCI_IN(scif_size, scif_offset); \
323 } \
324 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
325 { \
326 SCI_OUT(scif_size, scif_offset, value); \
327 }
328
329#define CPU_SCI_FNS(name, sci_offset, sci_size) \
330 static inline unsigned int sci_##name##_in(struct uart_port* port) \
331 { \
332 SCI_IN(sci_size, sci_offset); \
333 } \
334 static inline void sci_##name##_out(struct uart_port* port, unsigned int value) \
335 { \
336 SCI_OUT(sci_size, sci_offset, value); \
337 }
338
339#ifdef CONFIG_CPU_SH3
340#if defined(CONFIG_CPU_SUBTYPE_SH7300) || defined(CONFIG_CPU_SUBTYPE_SH7705)
341#define SCIF_FNS(name, scif_offset, scif_size) \
342 CPU_SCIF_FNS(name, scif_offset, scif_size)
343#else
344#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
345 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
346 h8_sci_offset, h8_sci_size) \
347 CPU_SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh3_scif_offset, sh3_scif_size)
348#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
349 CPU_SCIF_FNS(name, sh3_scif_offset, sh3_scif_size)
350#endif
351#elif defined(__H8300H__) || defined(__H8300S__)
352#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
353 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
354 h8_sci_offset, h8_sci_size) \
355 CPU_SCI_FNS(name, h8_sci_offset, h8_sci_size)
356#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size)
357#else
358#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
359 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
360 h8_sci_offset, h8_sci_size) \
361 CPU_SCIx_FNS(name, sh4_sci_offset, sh4_sci_size, sh4_scif_offset, sh4_scif_size)
362#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
363 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
364#endif
365
366#if defined(CONFIG_CPU_SUBTYPE_SH7300) || defined(CONFIG_CPU_SUBTYPE_SH7705)
367SCIF_FNS(SCSMR, 0x00, 16)
368SCIF_FNS(SCBRR, 0x04, 8)
369SCIF_FNS(SCSCR, 0x08, 16)
370SCIF_FNS(SCTDSR, 0x0c, 8)
371SCIF_FNS(SCFER, 0x10, 16)
372SCIF_FNS(SCxSR, 0x14, 16)
373SCIF_FNS(SCFCR, 0x18, 16)
374SCIF_FNS(SCFDR, 0x1c, 16)
375SCIF_FNS(SCxTDR, 0x20, 8)
376SCIF_FNS(SCxRDR, 0x24, 8)
377SCIF_FNS(SCLSR, 0x24, 16)
378#else
379/* reg SCI/SH3 SCI/SH4 SCIF/SH3 SCIF/SH4 SCI/H8*/
380/* name off sz off sz off sz off sz off sz*/
381SCIx_FNS(SCSMR, 0x00, 8, 0x00, 8, 0x00, 8, 0x00, 16, 0x00, 8)
382SCIx_FNS(SCBRR, 0x02, 8, 0x04, 8, 0x02, 8, 0x04, 8, 0x01, 8)
383SCIx_FNS(SCSCR, 0x04, 8, 0x08, 8, 0x04, 8, 0x08, 16, 0x02, 8)
384SCIx_FNS(SCxTDR, 0x06, 8, 0x0c, 8, 0x06, 8, 0x0C, 8, 0x03, 8)
385SCIx_FNS(SCxSR, 0x08, 8, 0x10, 8, 0x08, 16, 0x10, 16, 0x04, 8)
386SCIx_FNS(SCxRDR, 0x0a, 8, 0x14, 8, 0x0A, 8, 0x14, 8, 0x05, 8)
387SCIF_FNS(SCFCR, 0x0c, 8, 0x18, 16)
388SCIF_FNS(SCFDR, 0x0e, 16, 0x1C, 16)
389SCIF_FNS(SCSPTR, 0, 0, 0x20, 16)
390SCIF_FNS(SCLSR, 0, 0, 0x24, 16)
391#endif
392#define sci_in(port, reg) sci_##reg##_in(port)
393#define sci_out(port, reg, value) sci_##reg##_out(port, value)
394
395/* H8/300 series SCI pins assignment */
396#if defined(__H8300H__) || defined(__H8300S__)
397static const struct __attribute__((packed)) {
398 int port; /* GPIO port no */
399 unsigned short rx,tx; /* GPIO bit no */
400} h8300_sci_pins[] = {
401#if defined(CONFIG_H83007) || defined(CONFIG_H83068)
402 { /* SCI0 */
403 .port = H8300_GPIO_P9,
404 .rx = H8300_GPIO_B2,
405 .tx = H8300_GPIO_B0,
406 },
407 { /* SCI1 */
408 .port = H8300_GPIO_P9,
409 .rx = H8300_GPIO_B3,
410 .tx = H8300_GPIO_B1,
411 },
412 { /* SCI2 */
413 .port = H8300_GPIO_PB,
414 .rx = H8300_GPIO_B7,
415 .tx = H8300_GPIO_B6,
416 }
417#elif defined(CONFIG_H8S2678)
418 { /* SCI0 */
419 .port = H8300_GPIO_P3,
420 .rx = H8300_GPIO_B2,
421 .tx = H8300_GPIO_B0,
422 },
423 { /* SCI1 */
424 .port = H8300_GPIO_P3,
425 .rx = H8300_GPIO_B3,
426 .tx = H8300_GPIO_B1,
427 },
428 { /* SCI2 */
429 .port = H8300_GPIO_P5,
430 .rx = H8300_GPIO_B1,
431 .tx = H8300_GPIO_B0,
432 }
433#endif
434};
435#endif
436
437#if defined(CONFIG_CPU_SUBTYPE_SH7708)
438static inline int sci_rxd_in(struct uart_port *port)
439{
440 if (port->mapbase == 0xfffffe80)
441 return ctrl_inb(SCSPTR)&0x01 ? 1 : 0; /* SCI */
442 return 1;
443}
444#elif defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
445static inline int sci_rxd_in(struct uart_port *port)
446{
447 if (port->mapbase == 0xfffffe80)
448 return ctrl_inb(SCPDR)&0x01 ? 1 : 0; /* SCI */
449 if (port->mapbase == 0xa4000150)
450 return ctrl_inb(SCPDR)&0x10 ? 1 : 0; /* SCIF */
451 if (port->mapbase == 0xa4000140)
452 return ctrl_inb(SCPDR)&0x04 ? 1 : 0; /* IRDA */
453 return 1;
454}
455#elif defined(CONFIG_CPU_SUBTYPE_SH7705)
456static inline int sci_rxd_in(struct uart_port *port)
457{
458 if (port->mapbase == SCIF0)
459 return ctrl_inb(SCPDR)&0x04 ? 1 : 0; /* IRDA */
460 if (port->mapbase == SCIF2)
461 return ctrl_inb(SCPDR)&0x10 ? 1 : 0; /* SCIF */
462 return 1;
463}
464#elif defined(CONFIG_CPU_SUBTYPE_SH7750) || \
465 defined(CONFIG_CPU_SUBTYPE_SH7751) || \
466 defined(CONFIG_CPU_SUBTYPE_SH4_202)
467static inline int sci_rxd_in(struct uart_port *port)
468{
469#ifndef SCIF_ONLY
470 if (port->mapbase == 0xffe00000)
471 return ctrl_inb(SCSPTR1)&0x01 ? 1 : 0; /* SCI */
472#endif
473#ifndef SCI_ONLY
474 if (port->mapbase == 0xffe80000)
475 return ctrl_inw(SCSPTR2)&0x0001 ? 1 : 0; /* SCIF */
476#endif
477 return 1;
478}
479#elif defined(CONFIG_CPU_SUBTYPE_SH7760)
480static inline int sci_rxd_in(struct uart_port *port)
481{
482 if (port->mapbase == 0xfe600000)
483 return ctrl_inw(SCSPTR0) & 0x0001 ? 1 : 0; /* SCIF */
484 if (port->mapbase == 0xfe610000)
485 return ctrl_inw(SCSPTR1) & 0x0001 ? 1 : 0; /* SCIF */
486 if (port->mapbase == 0xfe620000)
487 return ctrl_inw(SCSPTR2) & 0x0001 ? 1 : 0; /* SCIF */
488}
489#elif defined(CONFIG_CPU_SUBTYPE_SH7300)
490static inline int sci_rxd_in(struct uart_port *port)
491{
492 if (port->mapbase == 0xa4430000)
493 return ctrl_inb(SCPDR)&0x01 ? 1 : 0; /* SCIF0 */
494 return 1;
495}
496#elif defined(CONFIG_CPU_SUBTYPE_SH73180)
497static inline int sci_rxd_in(struct uart_port *port)
498{
499 return ctrl_inb(SCPDR)&0x01 ? 1 : 0; /* SCIF0 */
500}
501#elif defined(CONFIG_CPU_SUBTYPE_ST40STB1)
502static inline int sci_rxd_in(struct uart_port *port)
503{
504 if (port->mapbase == 0xffe00000)
505 return ctrl_inw(SCSPTR1)&0x0001 ? 1 : 0; /* SCIF */
506 else
507 return ctrl_inw(SCSPTR2)&0x0001 ? 1 : 0; /* SCIF */
508
509}
510#elif defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
511static inline int sci_rxd_in(struct uart_port *port)
512{
513 return sci_in(port, SCSPTR)&0x0001 ? 1 : 0; /* SCIF */
514}
515#elif defined(__H8300H__) || defined(__H8300S__)
516static inline int sci_rxd_in(struct uart_port *port)
517{
518 int ch = (port->mapbase - SMR0) >> 3;
519 return (H8300_SCI_DR(ch) & h8300_sci_pins[ch].rx) ? 1 : 0;
520}
521#endif
522
523/*
524 * Values for the BitRate Register (SCBRR)
525 *
526 * The values are actually divisors for a frequency which can
527 * be internal to the SH3 (14.7456MHz) or derived from an external
528 * clock source. This driver assumes the internal clock is used;
529 * to support using an external clock source, config options or
530 * possibly command-line options would need to be added.
531 *
532 * Also, to support speeds below 2400 (why?) the lower 2 bits of
533 * the SCSMR register would also need to be set to non-zero values.
534 *
535 * -- Greg Banks 27Feb2000
536 *
537 * Answer: The SCBRR register is only eight bits, and the value in
538 * it gets larger with lower baud rates. At around 2400 (depending on
539 * the peripherial module clock) you run out of bits. However the
540 * lower two bits of SCSMR allow the module clock to be divided down,
541 * scaling the value which is needed in SCBRR.
542 *
543 * -- Stuart Menefy - 23 May 2000
544 *
545 * I meant, why would anyone bother with bitrates below 2400.
546 *
547 * -- Greg Banks - 7Jul2000
548 *
549 * You "speedist"! How will I use my 110bps ASR-33 teletype with paper
550 * tape reader as a console!
551 *
552 * -- Mitch Davis - 15 Jul 2000
553 */
554
555#define PCLK (current_cpu_data.module_clock)
556
557#if defined(CONFIG_CPU_SUBTYPE_SH7300)
558#define SCBRR_VALUE(bps) ((PCLK+16*bps)/(16*bps)-1)
559#elif defined(CONFIG_CPU_SUBTYPE_SH7705)
560#define SCBRR_VALUE(bps) (((PCLK*2)+16*bps)/(32*bps)-1)
561#elif !defined(__H8300H__) && !defined(__H8300S__)
562#define SCBRR_VALUE(bps) ((PCLK+16*bps)/(32*bps)-1)
563#else
564#define SCBRR_VALUE(bps) (((CONFIG_CPU_CLOCK*1000/32)/bps)-1)
565#endif
566#define BPS_2400 SCBRR_VALUE(2400)
567#define BPS_4800 SCBRR_VALUE(4800)
568#define BPS_9600 SCBRR_VALUE(9600)
569#define BPS_19200 SCBRR_VALUE(19200)
570#define BPS_38400 SCBRR_VALUE(38400)
571#define BPS_57600 SCBRR_VALUE(57600)
572#define BPS_115200 SCBRR_VALUE(115200)
573
diff --git a/drivers/serial/sn_console.c b/drivers/serial/sn_console.c
new file mode 100644
index 000000000000..ffaab9b90fd8
--- /dev/null
+++ b/drivers/serial/sn_console.c
@@ -0,0 +1,1124 @@
1/*
2 * C-Brick Serial Port (and console) driver for SGI Altix machines.
3 *
4 * This driver is NOT suitable for talking to the l1-controller for
5 * anything other than 'console activities' --- please use the l1
6 * driver for that.
7 *
8 *
9 * Copyright (c) 2004-2005 Silicon Graphics, Inc. All Rights Reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of version 2 of the GNU General Public License
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it would be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * Further, this software is distributed without any warranty that it is
20 * free of the rightful claim of any third person regarding infringement
21 * or the like. Any license provided herein, whether implied or
22 * otherwise, applies only to this software file. Patent licenses, if
23 * any, provided herein do not apply to combinations of this program with
24 * other software, or any other product whatsoever.
25 *
26 * You should have received a copy of the GNU General Public
27 * License along with this program; if not, write the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
29 *
30 * Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
31 * Mountain View, CA 94043, or:
32 *
33 * http://www.sgi.com
34 *
35 * For further information regarding this notice, see:
36 *
37 * http://oss.sgi.com/projects/GenInfo/NoticeExplan
38 */
39
40#include <linux/config.h>
41#include <linux/interrupt.h>
42#include <linux/tty.h>
43#include <linux/serial.h>
44#include <linux/console.h>
45#include <linux/module.h>
46#include <linux/sysrq.h>
47#include <linux/circ_buf.h>
48#include <linux/serial_reg.h>
49#include <linux/delay.h> /* for mdelay */
50#include <linux/miscdevice.h>
51#include <linux/serial_core.h>
52
53#include <asm/io.h>
54#include <asm/sn/simulator.h>
55#include <asm/sn/sn_sal.h>
56
57/* number of characters we can transmit to the SAL console at a time */
58#define SN_SAL_MAX_CHARS 120
59
60/* 64K, when we're asynch, it must be at least printk's LOG_BUF_LEN to
61 * avoid losing chars, (always has to be a power of 2) */
62#define SN_SAL_BUFFER_SIZE (64 * (1 << 10))
63
64#define SN_SAL_UART_FIFO_DEPTH 16
65#define SN_SAL_UART_FIFO_SPEED_CPS 9600/10
66
67/* sn_transmit_chars() calling args */
68#define TRANSMIT_BUFFERED 0
69#define TRANSMIT_RAW 1
70
71/* To use dynamic numbers only and not use the assigned major and minor,
72 * define the following.. */
73 /* #define USE_DYNAMIC_MINOR 1 *//* use dynamic minor number */
74#define USE_DYNAMIC_MINOR 0 /* Don't rely on misc_register dynamic minor */
75
76/* Device name we're using */
77#define DEVICE_NAME "ttySG"
78#define DEVICE_NAME_DYNAMIC "ttySG0" /* need full name for misc_register */
79/* The major/minor we are using, ignored for USE_DYNAMIC_MINOR */
80#define DEVICE_MAJOR 204
81#define DEVICE_MINOR 40
82
83#ifdef CONFIG_MAGIC_SYSRQ
84static char sysrq_serial_str[] = "\eSYS";
85static char *sysrq_serial_ptr = sysrq_serial_str;
86static unsigned long sysrq_requested;
87#endif /* CONFIG_MAGIC_SYSRQ */
88
89/*
90 * Port definition - this kinda drives it all
91 */
92struct sn_cons_port {
93 struct timer_list sc_timer;
94 struct uart_port sc_port;
95 struct sn_sal_ops {
96 int (*sal_puts_raw) (const char *s, int len);
97 int (*sal_puts) (const char *s, int len);
98 int (*sal_getc) (void);
99 int (*sal_input_pending) (void);
100 void (*sal_wakeup_transmit) (struct sn_cons_port *, int);
101 } *sc_ops;
102 unsigned long sc_interrupt_timeout;
103 int sc_is_asynch;
104};
105
106static struct sn_cons_port sal_console_port;
107static int sn_process_input;
108
109/* Only used if USE_DYNAMIC_MINOR is set to 1 */
110static struct miscdevice misc; /* used with misc_register for dynamic */
111
112extern void early_sn_setup(void);
113
114#undef DEBUG
115#ifdef DEBUG
116static int sn_debug_printf(const char *fmt, ...);
117#define DPRINTF(x...) sn_debug_printf(x)
118#else
119#define DPRINTF(x...) do { } while (0)
120#endif
121
122/* Prototypes */
123static int snt_hw_puts_raw(const char *, int);
124static int snt_hw_puts_buffered(const char *, int);
125static int snt_poll_getc(void);
126static int snt_poll_input_pending(void);
127static int snt_intr_getc(void);
128static int snt_intr_input_pending(void);
129static void sn_transmit_chars(struct sn_cons_port *, int);
130
131/* A table for polling:
132 */
133static struct sn_sal_ops poll_ops = {
134 .sal_puts_raw = snt_hw_puts_raw,
135 .sal_puts = snt_hw_puts_raw,
136 .sal_getc = snt_poll_getc,
137 .sal_input_pending = snt_poll_input_pending
138};
139
140/* A table for interrupts enabled */
141static struct sn_sal_ops intr_ops = {
142 .sal_puts_raw = snt_hw_puts_raw,
143 .sal_puts = snt_hw_puts_buffered,
144 .sal_getc = snt_intr_getc,
145 .sal_input_pending = snt_intr_input_pending,
146 .sal_wakeup_transmit = sn_transmit_chars
147};
148
149/* the console does output in two distinctly different ways:
150 * synchronous (raw) and asynchronous (buffered). initally, early_printk
151 * does synchronous output. any data written goes directly to the SAL
152 * to be output (incidentally, it is internally buffered by the SAL)
153 * after interrupts and timers are initialized and available for use,
154 * the console init code switches to asynchronous output. this is
155 * also the earliest opportunity to begin polling for console input.
156 * after console initialization, console output and tty (serial port)
157 * output is buffered and sent to the SAL asynchronously (either by
158 * timer callback or by UART interrupt) */
159
160/* routines for running the console in polling mode */
161
162/**
163 * snt_poll_getc - Get a character from the console in polling mode
164 *
165 */
166static int snt_poll_getc(void)
167{
168 int ch;
169
170 ia64_sn_console_getc(&ch);
171 return ch;
172}
173
174/**
175 * snt_poll_input_pending - Check if any input is waiting - polling mode.
176 *
177 */
178static int snt_poll_input_pending(void)
179{
180 int status, input;
181
182 status = ia64_sn_console_check(&input);
183 return !status && input;
184}
185
186/* routines for an interrupt driven console (normal) */
187
188/**
189 * snt_intr_getc - Get a character from the console, interrupt mode
190 *
191 */
192static int snt_intr_getc(void)
193{
194 return ia64_sn_console_readc();
195}
196
197/**
198 * snt_intr_input_pending - Check if input is pending, interrupt mode
199 *
200 */
201static int snt_intr_input_pending(void)
202{
203 return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
204}
205
206/* these functions are polled and interrupt */
207
208/**
209 * snt_hw_puts_raw - Send raw string to the console, polled or interrupt mode
210 * @s: String
211 * @len: Length
212 *
213 */
214static int snt_hw_puts_raw(const char *s, int len)
215{
216 /* this will call the PROM and not return until this is done */
217 return ia64_sn_console_putb(s, len);
218}
219
220/**
221 * snt_hw_puts_buffered - Send string to console, polled or interrupt mode
222 * @s: String
223 * @len: Length
224 *
225 */
226static int snt_hw_puts_buffered(const char *s, int len)
227{
228 /* queue data to the PROM */
229 return ia64_sn_console_xmit_chars((char *)s, len);
230}
231
232/* uart interface structs
233 * These functions are associated with the uart_port that the serial core
234 * infrastructure calls.
235 *
236 * Note: Due to how the console works, many routines are no-ops.
237 */
238
239/**
240 * snp_type - What type of console are we?
241 * @port: Port to operate with (we ignore since we only have one port)
242 *
243 */
244static const char *snp_type(struct uart_port *port)
245{
246 return ("SGI SN L1");
247}
248
249/**
250 * snp_tx_empty - Is the transmitter empty? We pretend we're always empty
251 * @port: Port to operate on (we ignore since we only have one port)
252 *
253 */
254static unsigned int snp_tx_empty(struct uart_port *port)
255{
256 return 1;
257}
258
259/**
260 * snp_stop_tx - stop the transmitter - no-op for us
261 * @port: Port to operat eon - we ignore - no-op function
262 * @tty_stop: Set to 1 if called via uart_stop
263 *
264 */
265static void snp_stop_tx(struct uart_port *port, unsigned int tty_stop)
266{
267}
268
269/**
270 * snp_release_port - Free i/o and resources for port - no-op for us
271 * @port: Port to operate on - we ignore - no-op function
272 *
273 */
274static void snp_release_port(struct uart_port *port)
275{
276}
277
278/**
279 * snp_enable_ms - Force modem status interrupts on - no-op for us
280 * @port: Port to operate on - we ignore - no-op function
281 *
282 */
283static void snp_enable_ms(struct uart_port *port)
284{
285}
286
287/**
288 * snp_shutdown - shut down the port - free irq and disable - no-op for us
289 * @port: Port to shut down - we ignore
290 *
291 */
292static void snp_shutdown(struct uart_port *port)
293{
294}
295
296/**
297 * snp_set_mctrl - set control lines (dtr, rts, etc) - no-op for our console
298 * @port: Port to operate on - we ignore
299 * @mctrl: Lines to set/unset - we ignore
300 *
301 */
302static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
303{
304}
305
306/**
307 * snp_get_mctrl - get contorl line info, we just return a static value
308 * @port: port to operate on - we only have one port so we ignore this
309 *
310 */
311static unsigned int snp_get_mctrl(struct uart_port *port)
312{
313 return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
314}
315
316/**
317 * snp_stop_rx - Stop the receiver - we ignor ethis
318 * @port: Port to operate on - we ignore
319 *
320 */
321static void snp_stop_rx(struct uart_port *port)
322{
323}
324
325/**
326 * snp_start_tx - Start transmitter
327 * @port: Port to operate on
328 * @tty_stop: Set to 1 if called via uart_start
329 *
330 */
331static void snp_start_tx(struct uart_port *port, unsigned int tty_stop)
332{
333 if (sal_console_port.sc_ops->sal_wakeup_transmit)
334 sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
335 TRANSMIT_BUFFERED);
336
337}
338
339/**
340 * snp_break_ctl - handle breaks - ignored by us
341 * @port: Port to operate on
342 * @break_state: Break state
343 *
344 */
345static void snp_break_ctl(struct uart_port *port, int break_state)
346{
347}
348
349/**
350 * snp_startup - Start up the serial port - always return 0 (We're always on)
351 * @port: Port to operate on
352 *
353 */
354static int snp_startup(struct uart_port *port)
355{
356 return 0;
357}
358
359/**
360 * snp_set_termios - set termios stuff - we ignore these
361 * @port: port to operate on
362 * @termios: New settings
363 * @termios: Old
364 *
365 */
366static void
367snp_set_termios(struct uart_port *port, struct termios *termios,
368 struct termios *old)
369{
370}
371
372/**
373 * snp_request_port - allocate resources for port - ignored by us
374 * @port: port to operate on
375 *
376 */
377static int snp_request_port(struct uart_port *port)
378{
379 return 0;
380}
381
382/**
383 * snp_config_port - allocate resources, set up - we ignore, we're always on
384 * @port: Port to operate on
385 * @flags: flags used for port setup
386 *
387 */
388static void snp_config_port(struct uart_port *port, int flags)
389{
390}
391
392/* Associate the uart functions above - given to serial core */
393
394static struct uart_ops sn_console_ops = {
395 .tx_empty = snp_tx_empty,
396 .set_mctrl = snp_set_mctrl,
397 .get_mctrl = snp_get_mctrl,
398 .stop_tx = snp_stop_tx,
399 .start_tx = snp_start_tx,
400 .stop_rx = snp_stop_rx,
401 .enable_ms = snp_enable_ms,
402 .break_ctl = snp_break_ctl,
403 .startup = snp_startup,
404 .shutdown = snp_shutdown,
405 .set_termios = snp_set_termios,
406 .pm = NULL,
407 .type = snp_type,
408 .release_port = snp_release_port,
409 .request_port = snp_request_port,
410 .config_port = snp_config_port,
411 .verify_port = NULL,
412};
413
414/* End of uart struct functions and defines */
415
416#ifdef DEBUG
417
418/**
419 * sn_debug_printf - close to hardware debugging printf
420 * @fmt: printf format
421 *
422 * This is as "close to the metal" as we can get, used when the driver
423 * itself may be broken.
424 *
425 */
426static int sn_debug_printf(const char *fmt, ...)
427{
428 static char printk_buf[1024];
429 int printed_len;
430 va_list args;
431
432 va_start(args, fmt);
433 printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
434
435 if (!sal_console_port.sc_ops) {
436 sal_console_port.sc_ops = &poll_ops;
437 early_sn_setup();
438 }
439 sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
440
441 va_end(args);
442 return printed_len;
443}
444#endif /* DEBUG */
445
446/*
447 * Interrupt handling routines.
448 */
449
450/**
451 * sn_receive_chars - Grab characters, pass them to tty layer
452 * @port: Port to operate on
453 * @regs: Saved registers (needed by uart_handle_sysrq_char)
454 * @flags: irq flags
455 *
456 * Note: If we're not registered with the serial core infrastructure yet,
457 * we don't try to send characters to it...
458 *
459 */
460static void
461sn_receive_chars(struct sn_cons_port *port, struct pt_regs *regs,
462 unsigned long flags)
463{
464 int ch;
465 struct tty_struct *tty;
466
467 if (!port) {
468 printk(KERN_ERR "sn_receive_chars - port NULL so can't receieve\n");
469 return;
470 }
471
472 if (!port->sc_ops) {
473 printk(KERN_ERR "sn_receive_chars - port->sc_ops NULL so can't receieve\n");
474 return;
475 }
476
477 if (port->sc_port.info) {
478 /* The serial_core stuffs are initilized, use them */
479 tty = port->sc_port.info->tty;
480 }
481 else {
482 /* Not registered yet - can't pass to tty layer. */
483 tty = NULL;
484 }
485
486 while (port->sc_ops->sal_input_pending()) {
487 ch = port->sc_ops->sal_getc();
488 if (ch < 0) {
489 printk(KERN_ERR "sn_console: An error occured while "
490 "obtaining data from the console (0x%0x)\n", ch);
491 break;
492 }
493#ifdef CONFIG_MAGIC_SYSRQ
494 if (sysrq_requested) {
495 unsigned long sysrq_timeout = sysrq_requested + HZ*5;
496
497 sysrq_requested = 0;
498 if (ch && time_before(jiffies, sysrq_timeout)) {
499 spin_unlock_irqrestore(&port->sc_port.lock, flags);
500 handle_sysrq(ch, regs, NULL);
501 spin_lock_irqsave(&port->sc_port.lock, flags);
502 /* ignore actual sysrq command char */
503 continue;
504 }
505 }
506 if (ch == *sysrq_serial_ptr) {
507 if (!(*++sysrq_serial_ptr)) {
508 sysrq_requested = jiffies;
509 sysrq_serial_ptr = sysrq_serial_str;
510 }
511 /*
512 * ignore the whole sysrq string except for the
513 * leading escape
514 */
515 if (ch != '\e')
516 continue;
517 }
518 else
519 sysrq_serial_ptr = sysrq_serial_str;
520#endif /* CONFIG_MAGIC_SYSRQ */
521
522 /* record the character to pass up to the tty layer */
523 if (tty) {
524 *tty->flip.char_buf_ptr = ch;
525 *tty->flip.flag_buf_ptr = TTY_NORMAL;
526 tty->flip.char_buf_ptr++;
527 tty->flip.count++;
528 if (tty->flip.count == TTY_FLIPBUF_SIZE)
529 break;
530 }
531 port->sc_port.icount.rx++;
532 }
533
534 if (tty)
535 tty_flip_buffer_push(tty);
536}
537
538/**
539 * sn_transmit_chars - grab characters from serial core, send off
540 * @port: Port to operate on
541 * @raw: Transmit raw or buffered
542 *
543 * Note: If we're early, before we're registered with serial core, the
544 * writes are going through sn_sal_console_write because that's how
545 * register_console has been set up. We currently could have asynch
546 * polls calling this function due to sn_sal_switch_to_asynch but we can
547 * ignore them until we register with the serial core stuffs.
548 *
549 */
550static void sn_transmit_chars(struct sn_cons_port *port, int raw)
551{
552 int xmit_count, tail, head, loops, ii;
553 int result;
554 char *start;
555 struct circ_buf *xmit;
556
557 if (!port)
558 return;
559
560 BUG_ON(!port->sc_is_asynch);
561
562 if (port->sc_port.info) {
563 /* We're initilized, using serial core infrastructure */
564 xmit = &port->sc_port.info->xmit;
565 } else {
566 /* Probably sn_sal_switch_to_asynch has been run but serial core isn't
567 * initilized yet. Just return. Writes are going through
568 * sn_sal_console_write (due to register_console) at this time.
569 */
570 return;
571 }
572
573 if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
574 /* Nothing to do. */
575 return;
576 }
577
578 head = xmit->head;
579 tail = xmit->tail;
580 start = &xmit->buf[tail];
581
582 /* twice around gets the tail to the end of the buffer and
583 * then to the head, if needed */
584 loops = (head < tail) ? 2 : 1;
585
586 for (ii = 0; ii < loops; ii++) {
587 xmit_count = (head < tail) ?
588 (UART_XMIT_SIZE - tail) : (head - tail);
589
590 if (xmit_count > 0) {
591 if (raw == TRANSMIT_RAW)
592 result =
593 port->sc_ops->sal_puts_raw(start,
594 xmit_count);
595 else
596 result =
597 port->sc_ops->sal_puts(start, xmit_count);
598#ifdef DEBUG
599 if (!result)
600 DPRINTF("`");
601#endif
602 if (result > 0) {
603 xmit_count -= result;
604 port->sc_port.icount.tx += result;
605 tail += result;
606 tail &= UART_XMIT_SIZE - 1;
607 xmit->tail = tail;
608 start = &xmit->buf[tail];
609 }
610 }
611 }
612
613 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
614 uart_write_wakeup(&port->sc_port);
615
616 if (uart_circ_empty(xmit))
617 snp_stop_tx(&port->sc_port, 0); /* no-op for us */
618}
619
620/**
621 * sn_sal_interrupt - Handle console interrupts
622 * @irq: irq #, useful for debug statements
623 * @dev_id: our pointer to our port (sn_cons_port which contains the uart port)
624 * @regs: Saved registers, used by sn_receive_chars for uart_handle_sysrq_char
625 *
626 */
627static irqreturn_t sn_sal_interrupt(int irq, void *dev_id, struct pt_regs *regs)
628{
629 struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
630 unsigned long flags;
631 int status = ia64_sn_console_intr_status();
632
633 if (!port)
634 return IRQ_NONE;
635
636 spin_lock_irqsave(&port->sc_port.lock, flags);
637 if (status & SAL_CONSOLE_INTR_RECV) {
638 sn_receive_chars(port, regs, flags);
639 }
640 if (status & SAL_CONSOLE_INTR_XMIT) {
641 sn_transmit_chars(port, TRANSMIT_BUFFERED);
642 }
643 spin_unlock_irqrestore(&port->sc_port.lock, flags);
644 return IRQ_HANDLED;
645}
646
647/**
648 * sn_sal_connect_interrupt - Request interrupt, handled by sn_sal_interrupt
649 * @port: Our sn_cons_port (which contains the uart port)
650 *
651 * returns the console irq if interrupt is successfully registered, else 0
652 *
653 */
654static int sn_sal_connect_interrupt(struct sn_cons_port *port)
655{
656 if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
657 SA_INTERRUPT | SA_SHIRQ,
658 "SAL console driver", port) >= 0) {
659 return SGI_UART_VECTOR;
660 }
661
662 printk(KERN_INFO "sn_console: console proceeding in polled mode\n");
663 return 0;
664}
665
666/**
667 * sn_sal_timer_poll - this function handles polled console mode
668 * @data: A pointer to our sn_cons_port (which contains the uart port)
669 *
670 * data is the pointer that init_timer will store for us. This function is
671 * associated with init_timer to see if there is any console traffic.
672 * Obviously not used in interrupt mode
673 *
674 */
675static void sn_sal_timer_poll(unsigned long data)
676{
677 struct sn_cons_port *port = (struct sn_cons_port *)data;
678 unsigned long flags;
679
680 if (!port)
681 return;
682
683 if (!port->sc_port.irq) {
684 spin_lock_irqsave(&port->sc_port.lock, flags);
685 if (sn_process_input)
686 sn_receive_chars(port, NULL, flags);
687 sn_transmit_chars(port, TRANSMIT_RAW);
688 spin_unlock_irqrestore(&port->sc_port.lock, flags);
689 mod_timer(&port->sc_timer,
690 jiffies + port->sc_interrupt_timeout);
691 }
692}
693
694/*
695 * Boot-time initialization code
696 */
697
698/**
699 * sn_sal_switch_to_asynch - Switch to async mode (as opposed to synch)
700 * @port: Our sn_cons_port (which contains the uart port)
701 *
702 * So this is used by sn_sal_serial_console_init (early on, before we're
703 * registered with serial core). It's also used by sn_sal_module_init
704 * right after we've registered with serial core. The later only happens
705 * if we didn't already come through here via sn_sal_serial_console_init.
706 *
707 */
708static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
709{
710 unsigned long flags;
711
712 if (!port)
713 return;
714
715 DPRINTF("sn_console: about to switch to asynchronous console\n");
716
717 /* without early_printk, we may be invoked late enough to race
718 * with other cpus doing console IO at this point, however
719 * console interrupts will never be enabled */
720 spin_lock_irqsave(&port->sc_port.lock, flags);
721
722 /* early_printk invocation may have done this for us */
723 if (!port->sc_ops)
724 port->sc_ops = &poll_ops;
725
726 /* we can't turn on the console interrupt (as request_irq
727 * calls kmalloc, which isn't set up yet), so we rely on a
728 * timer to poll for input and push data from the console
729 * buffer.
730 */
731 init_timer(&port->sc_timer);
732 port->sc_timer.function = sn_sal_timer_poll;
733 port->sc_timer.data = (unsigned long)port;
734
735 if (IS_RUNNING_ON_SIMULATOR())
736 port->sc_interrupt_timeout = 6;
737 else {
738 /* 960cps / 16 char FIFO = 60HZ
739 * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
740 port->sc_interrupt_timeout =
741 HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
742 }
743 mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
744
745 port->sc_is_asynch = 1;
746 spin_unlock_irqrestore(&port->sc_port.lock, flags);
747}
748
749/**
750 * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
751 * @port: Our sn_cons_port (which contains the uart port)
752 *
753 * In sn_sal_module_init, after we're registered with serial core and
754 * the port is added, this function is called to switch us to interrupt
755 * mode. We were previously in asynch/polling mode (using init_timer).
756 *
757 * We attempt to switch to interrupt mode here by calling
758 * sn_sal_connect_interrupt. If that works out, we enable receive interrupts.
759 */
760static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
761{
762 int irq;
763 unsigned long flags;
764
765 if (!port)
766 return;
767
768 DPRINTF("sn_console: switching to interrupt driven console\n");
769
770 spin_lock_irqsave(&port->sc_port.lock, flags);
771
772 irq = sn_sal_connect_interrupt(port);
773
774 if (irq) {
775 port->sc_port.irq = irq;
776 port->sc_ops = &intr_ops;
777
778 /* turn on receive interrupts */
779 ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
780 }
781 spin_unlock_irqrestore(&port->sc_port.lock, flags);
782}
783
784/*
785 * Kernel console definitions
786 */
787
788static void sn_sal_console_write(struct console *, const char *, unsigned);
789static int __init sn_sal_console_setup(struct console *, char *);
790extern struct uart_driver sal_console_uart;
791extern struct tty_driver *uart_console_device(struct console *, int *);
792
793static struct console sal_console = {
794 .name = DEVICE_NAME,
795 .write = sn_sal_console_write,
796 .device = uart_console_device,
797 .setup = sn_sal_console_setup,
798 .index = -1, /* unspecified */
799 .data = &sal_console_uart,
800};
801
802#define SAL_CONSOLE &sal_console
803
804struct uart_driver sal_console_uart = {
805 .owner = THIS_MODULE,
806 .driver_name = "sn_console",
807 .dev_name = DEVICE_NAME,
808 .major = 0, /* major/minor set at registration time per USE_DYNAMIC_MINOR */
809 .minor = 0,
810 .nr = 1, /* one port */
811 .cons = SAL_CONSOLE,
812};
813
814/**
815 * sn_sal_module_init - When the kernel loads us, get us rolling w/ serial core
816 *
817 * Before this is called, we've been printing kernel messages in a special
818 * early mode not making use of the serial core infrastructure. When our
819 * driver is loaded for real, we register the driver and port with serial
820 * core and try to enable interrupt driven mode.
821 *
822 */
823static int __init sn_sal_module_init(void)
824{
825 int retval;
826
827 if (!ia64_platform_is("sn2"))
828 return -ENODEV;
829
830 printk(KERN_INFO "sn_console: Console driver init\n");
831
832 if (USE_DYNAMIC_MINOR == 1) {
833 misc.minor = MISC_DYNAMIC_MINOR;
834 misc.name = DEVICE_NAME_DYNAMIC;
835 retval = misc_register(&misc);
836 if (retval != 0) {
837 printk
838 ("Failed to register console device using misc_register.\n");
839 return -ENODEV;
840 }
841 sal_console_uart.major = MISC_MAJOR;
842 sal_console_uart.minor = misc.minor;
843 } else {
844 sal_console_uart.major = DEVICE_MAJOR;
845 sal_console_uart.minor = DEVICE_MINOR;
846 }
847
848 /* We register the driver and the port before switching to interrupts
849 * or async above so the proper uart structures are populated */
850
851 if (uart_register_driver(&sal_console_uart) < 0) {
852 printk
853 ("ERROR sn_sal_module_init failed uart_register_driver, line %d\n",
854 __LINE__);
855 return -ENODEV;
856 }
857
858 spin_lock_init(&sal_console_port.sc_port.lock);
859
860 /* Setup the port struct with the minimum needed */
861 sal_console_port.sc_port.membase = (char *)1; /* just needs to be non-zero */
862 sal_console_port.sc_port.type = PORT_16550A;
863 sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
864 sal_console_port.sc_port.ops = &sn_console_ops;
865 sal_console_port.sc_port.line = 0;
866
867 if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
868 /* error - not sure what I'd do - so I'll do nothing */
869 printk(KERN_ERR "%s: unable to add port\n", __FUNCTION__);
870 }
871
872 /* when this driver is compiled in, the console initialization
873 * will have already switched us into asynchronous operation
874 * before we get here through the module initcalls */
875 if (!sal_console_port.sc_is_asynch) {
876 sn_sal_switch_to_asynch(&sal_console_port);
877 }
878
879 /* at this point (module_init) we can try to turn on interrupts */
880 if (!IS_RUNNING_ON_SIMULATOR()) {
881 sn_sal_switch_to_interrupts(&sal_console_port);
882 }
883 sn_process_input = 1;
884 return 0;
885}
886
887/**
888 * sn_sal_module_exit - When we're unloaded, remove the driver/port
889 *
890 */
891static void __exit sn_sal_module_exit(void)
892{
893 del_timer_sync(&sal_console_port.sc_timer);
894 uart_remove_one_port(&sal_console_uart, &sal_console_port.sc_port);
895 uart_unregister_driver(&sal_console_uart);
896 misc_deregister(&misc);
897}
898
899module_init(sn_sal_module_init);
900module_exit(sn_sal_module_exit);
901
902/**
903 * puts_raw_fixed - sn_sal_console_write helper for adding \r's as required
904 * @puts_raw : puts function to do the writing
905 * @s: input string
906 * @count: length
907 *
908 * We need a \r ahead of every \n for direct writes through
909 * ia64_sn_console_putb (what sal_puts_raw below actually does).
910 *
911 */
912
913static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
914 const char *s, int count)
915{
916 const char *s1;
917
918 /* Output '\r' before each '\n' */
919 while ((s1 = memchr(s, '\n', count)) != NULL) {
920 puts_raw(s, s1 - s);
921 puts_raw("\r\n", 2);
922 count -= s1 + 1 - s;
923 s = s1 + 1;
924 }
925 puts_raw(s, count);
926}
927
928/**
929 * sn_sal_console_write - Print statements before serial core available
930 * @console: Console to operate on - we ignore since we have just one
931 * @s: String to send
932 * @count: length
933 *
934 * This is referenced in the console struct. It is used for early
935 * console printing before we register with serial core and for things
936 * such as kdb. The console_lock must be held when we get here.
937 *
938 * This function has some code for trying to print output even if the lock
939 * is held. We try to cover the case where a lock holder could have died.
940 * We don't use this special case code if we're not registered with serial
941 * core yet. After we're registered with serial core, the only time this
942 * function would be used is for high level kernel output like magic sys req,
943 * kdb, and printk's.
944 */
945static void
946sn_sal_console_write(struct console *co, const char *s, unsigned count)
947{
948 unsigned long flags = 0;
949 struct sn_cons_port *port = &sal_console_port;
950#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
951 static int stole_lock = 0;
952#endif
953
954 BUG_ON(!port->sc_is_asynch);
955
956 /* We can't look at the xmit buffer if we're not registered with serial core
957 * yet. So only do the fancy recovery after registering
958 */
959 if (port->sc_port.info) {
960
961 /* somebody really wants this output, might be an
962 * oops, kdb, panic, etc. make sure they get it. */
963#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
964 if (spin_is_locked(&port->sc_port.lock)) {
965 int lhead = port->sc_port.info->xmit.head;
966 int ltail = port->sc_port.info->xmit.tail;
967 int counter, got_lock = 0;
968
969 /*
970 * We attempt to determine if someone has died with the
971 * lock. We wait ~20 secs after the head and tail ptrs
972 * stop moving and assume the lock holder is not functional
973 * and plow ahead. If the lock is freed within the time out
974 * period we re-get the lock and go ahead normally. We also
975 * remember if we have plowed ahead so that we don't have
976 * to wait out the time out period again - the asumption
977 * is that we will time out again.
978 */
979
980 for (counter = 0; counter < 150; mdelay(125), counter++) {
981 if (!spin_is_locked(&port->sc_port.lock)
982 || stole_lock) {
983 if (!stole_lock) {
984 spin_lock_irqsave(&port->
985 sc_port.lock,
986 flags);
987 got_lock = 1;
988 }
989 break;
990 } else {
991 /* still locked */
992 if ((lhead !=
993 port->sc_port.info->xmit.head)
994 || (ltail !=
995 port->sc_port.info->xmit.
996 tail)) {
997 lhead =
998 port->sc_port.info->xmit.
999 head;
1000 ltail =
1001 port->sc_port.info->xmit.
1002 tail;
1003 counter = 0;
1004 }
1005 }
1006 }
1007 /* flush anything in the serial core xmit buffer, raw */
1008 sn_transmit_chars(port, 1);
1009 if (got_lock) {
1010 spin_unlock_irqrestore(&port->sc_port.lock,
1011 flags);
1012 stole_lock = 0;
1013 } else {
1014 /* fell thru */
1015 stole_lock = 1;
1016 }
1017 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
1018 } else {
1019 stole_lock = 0;
1020#endif
1021 spin_lock_irqsave(&port->sc_port.lock, flags);
1022 sn_transmit_chars(port, 1);
1023 spin_unlock_irqrestore(&port->sc_port.lock, flags);
1024
1025 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
1026#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
1027 }
1028#endif
1029 }
1030 else {
1031 /* Not yet registered with serial core - simple case */
1032 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
1033 }
1034}
1035
1036
1037/**
1038 * sn_sal_console_setup - Set up console for early printing
1039 * @co: Console to work with
1040 * @options: Options to set
1041 *
1042 * Altix console doesn't do anything with baud rates, etc, anyway.
1043 *
1044 * This isn't required since not providing the setup function in the
1045 * console struct is ok. However, other patches like KDB plop something
1046 * here so providing it is easier.
1047 *
1048 */
1049static int __init sn_sal_console_setup(struct console *co, char *options)
1050{
1051 return 0;
1052}
1053
1054/**
1055 * sn_sal_console_write_early - simple early output routine
1056 * @co - console struct
1057 * @s - string to print
1058 * @count - count
1059 *
1060 * Simple function to provide early output, before even
1061 * sn_sal_serial_console_init is called. Referenced in the
1062 * console struct registerd in sn_serial_console_early_setup.
1063 *
1064 */
1065static void __init
1066sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
1067{
1068 puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
1069}
1070
1071/* Used for very early console printing - again, before
1072 * sn_sal_serial_console_init is run */
1073static struct console sal_console_early __initdata = {
1074 .name = "sn_sal",
1075 .write = sn_sal_console_write_early,
1076 .flags = CON_PRINTBUFFER,
1077 .index = -1,
1078};
1079
1080/**
1081 * sn_serial_console_early_setup - Sets up early console output support
1082 *
1083 * Register a console early on... This is for output before even
1084 * sn_sal_serial_cosnole_init is called. This function is called from
1085 * setup.c. This allows us to do really early polled writes. When
1086 * sn_sal_serial_console_init is called, this console is unregistered
1087 * and a new one registered.
1088 */
1089int __init sn_serial_console_early_setup(void)
1090{
1091 if (!ia64_platform_is("sn2"))
1092 return -1;
1093
1094 sal_console_port.sc_ops = &poll_ops;
1095 early_sn_setup(); /* Find SAL entry points */
1096 register_console(&sal_console_early);
1097
1098 return 0;
1099}
1100
1101/**
1102 * sn_sal_serial_console_init - Early console output - set up for register
1103 *
1104 * This function is called when regular console init happens. Because we
1105 * support even earlier console output with sn_serial_console_early_setup
1106 * (called from setup.c directly), this function unregisters the really
1107 * early console.
1108 *
1109 * Note: Even if setup.c doesn't register sal_console_early, unregistering
1110 * it here doesn't hurt anything.
1111 *
1112 */
1113static int __init sn_sal_serial_console_init(void)
1114{
1115 if (ia64_platform_is("sn2")) {
1116 sn_sal_switch_to_asynch(&sal_console_port);
1117 DPRINTF("sn_sal_serial_console_init : register console\n");
1118 register_console(&sal_console);
1119 unregister_console(&sal_console_early);
1120 }
1121 return 0;
1122}
1123
1124console_initcall(sn_sal_serial_console_init);
diff --git a/drivers/serial/suncore.c b/drivers/serial/suncore.c
new file mode 100644
index 000000000000..5fc4a62173d9
--- /dev/null
+++ b/drivers/serial/suncore.c
@@ -0,0 +1,218 @@
1/* suncore.c
2 *
3 * Common SUN serial routines. Based entirely
4 * upon drivers/sbus/char/sunserial.c which is:
5 *
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 *
8 * Adaptation to new UART layer is:
9 *
10 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
11 */
12
13#include <linux/config.h>
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/console.h>
17#include <linux/tty.h>
18#include <linux/errno.h>
19#include <linux/string.h>
20#include <linux/init.h>
21
22#include <asm/oplib.h>
23
24#include "suncore.h"
25
26int sunserial_current_minor = 64;
27
28EXPORT_SYMBOL(sunserial_current_minor);
29
30void
31sunserial_console_termios(struct console *con)
32{
33 char mode[16], buf[16], *s;
34 char *mode_prop = "ttyX-mode";
35 char *cd_prop = "ttyX-ignore-cd";
36 char *dtr_prop = "ttyX-rts-dtr-off";
37 int baud, bits, stop, cflag;
38 char parity;
39 int carrier = 0;
40 int rtsdtr = 1;
41 int topnd, nd;
42
43 if (!serial_console)
44 return;
45
46 if (serial_console == 1) {
47 mode_prop[3] = 'a';
48 cd_prop[3] = 'a';
49 dtr_prop[3] = 'a';
50 } else {
51 mode_prop[3] = 'b';
52 cd_prop[3] = 'b';
53 dtr_prop[3] = 'b';
54 }
55
56 topnd = prom_getchild(prom_root_node);
57 nd = prom_searchsiblings(topnd, "options");
58 if (!nd) {
59 strcpy(mode, "9600,8,n,1,-");
60 goto no_options;
61 }
62
63 if (!prom_node_has_property(nd, mode_prop)) {
64 strcpy(mode, "9600,8,n,1,-");
65 goto no_options;
66 }
67
68 memset(mode, 0, sizeof(mode));
69 prom_getstring(nd, mode_prop, mode, sizeof(mode));
70
71 if (prom_node_has_property(nd, cd_prop)) {
72 memset(buf, 0, sizeof(buf));
73 prom_getstring(nd, cd_prop, buf, sizeof(buf));
74 if (!strcmp(buf, "false"))
75 carrier = 1;
76
77 /* XXX: this is unused below. */
78 }
79
80 if (prom_node_has_property(nd, dtr_prop)) {
81 memset(buf, 0, sizeof(buf));
82 prom_getstring(nd, dtr_prop, buf, sizeof(buf));
83 if (!strcmp(buf, "false"))
84 rtsdtr = 0;
85
86 /* XXX: this is unused below. */
87 }
88
89no_options:
90 cflag = CREAD | HUPCL | CLOCAL;
91
92 s = mode;
93 baud = simple_strtoul(s, NULL, 0);
94 s = strchr(s, ',');
95 bits = simple_strtoul(++s, NULL, 0);
96 s = strchr(s, ',');
97 parity = *(++s);
98 s = strchr(s, ',');
99 stop = simple_strtoul(++s, NULL, 0);
100 s = strchr(s, ',');
101 /* XXX handshake is not handled here. */
102
103 switch (baud) {
104 case 150: cflag |= B150; break;
105 case 300: cflag |= B300; break;
106 case 600: cflag |= B600; break;
107 case 1200: cflag |= B1200; break;
108 case 2400: cflag |= B2400; break;
109 case 4800: cflag |= B4800; break;
110 case 9600: cflag |= B9600; break;
111 case 19200: cflag |= B19200; break;
112 case 38400: cflag |= B38400; break;
113 default: baud = 9600; cflag |= B9600; break;
114 }
115
116 switch (bits) {
117 case 5: cflag |= CS5; break;
118 case 6: cflag |= CS6; break;
119 case 7: cflag |= CS7; break;
120 case 8: cflag |= CS8; break;
121 default: cflag |= CS8; break;
122 }
123
124 switch (parity) {
125 case 'o': cflag |= (PARENB | PARODD); break;
126 case 'e': cflag |= PARENB; break;
127 case 'n': default: break;
128 }
129
130 switch (stop) {
131 case 2: cflag |= CSTOPB; break;
132 case 1: default: break;
133 }
134
135 con->cflag = cflag;
136}
137
138EXPORT_SYMBOL(sunserial_console_termios);
139
140/* Sun serial MOUSE auto baud rate detection. */
141static struct mouse_baud_cflag {
142 int baud;
143 unsigned int cflag;
144} mouse_baud_table[] = {
145 { 1200, B1200 },
146 { 2400, B2400 },
147 { 4800, B4800 },
148 { 9600, B9600 },
149 { -1, ~0 },
150 { -1, ~0 },
151};
152
153unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
154{
155 int i;
156
157 for (i = 0; mouse_baud_table[i].baud != -1; i++)
158 if (mouse_baud_table[i].cflag == (cflag & CBAUD))
159 break;
160
161 i += 1;
162 if (mouse_baud_table[i].baud == -1)
163 i = 0;
164
165 *new_baud = mouse_baud_table[i].baud;
166 return mouse_baud_table[i].cflag;
167}
168
169EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
170
171/* Basically, when the baud rate is wrong the mouse spits out
172 * breaks to us.
173 */
174int suncore_mouse_baud_detection(unsigned char ch, int is_break)
175{
176 static int mouse_got_break = 0;
177 static int ctr = 0;
178
179 if (is_break) {
180 /* Let a few normal bytes go by before we jump the gun
181 * and say we need to try another baud rate.
182 */
183 if (mouse_got_break && ctr < 8)
184 return 1;
185
186 /* Ok, we need to try another baud. */
187 ctr = 0;
188 mouse_got_break = 1;
189 return 2;
190 }
191 if (mouse_got_break) {
192 ctr++;
193 if (ch == 0x87) {
194 /* Correct baud rate determined. */
195 mouse_got_break = 0;
196 }
197 return 1;
198 }
199 return 0;
200}
201
202EXPORT_SYMBOL(suncore_mouse_baud_detection);
203
204static int __init suncore_init(void)
205{
206 return 0;
207}
208
209static void __exit suncore_exit(void)
210{
211}
212
213module_init(suncore_init);
214module_exit(suncore_exit);
215
216MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
217MODULE_DESCRIPTION("Sun serial common layer");
218MODULE_LICENSE("GPL");
diff --git a/drivers/serial/suncore.h b/drivers/serial/suncore.h
new file mode 100644
index 000000000000..513916a8ce37
--- /dev/null
+++ b/drivers/serial/suncore.h
@@ -0,0 +1,29 @@
1/* suncore.h
2 *
3 * Generic SUN serial/kbd/ms layer. Based entirely
4 * upon drivers/sbus/char/sunserial.h which is:
5 *
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 *
8 * Port to new UART layer is:
9 *
10 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
11 */
12
13#ifndef _SERIAL_SUN_H
14#define _SERIAL_SUN_H
15
16/* Serial keyboard defines for L1-A processing... */
17#define SUNKBD_RESET 0xff
18#define SUNKBD_L1 0x01
19#define SUNKBD_UP 0x80
20#define SUNKBD_A 0x4d
21
22extern unsigned int suncore_mouse_baud_cflag_next(unsigned int, int *);
23extern int suncore_mouse_baud_detection(unsigned char, int);
24
25extern int sunserial_current_minor;
26
27extern void sunserial_console_termios(struct console *);
28
29#endif /* !(_SERIAL_SUN_H) */
diff --git a/drivers/serial/sunsab.c b/drivers/serial/sunsab.c
new file mode 100644
index 000000000000..8caaf2e5e47c
--- /dev/null
+++ b/drivers/serial/sunsab.c
@@ -0,0 +1,1171 @@
1/* sunsab.c: ASYNC Driver for the SIEMENS SAB82532 DUSCC.
2 *
3 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5 *
6 * Rewrote buffer handling to use CIRC(Circular Buffer) macros.
7 * Maxim Krasnyanskiy <maxk@qualcomm.com>
8 *
9 * Fixed to use tty_get_baud_rate, and to allow for arbitrary baud
10 * rates to be programmed into the UART. Also eliminated a lot of
11 * duplicated code in the console setup.
12 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
13 *
14 * Ported to new 2.5.x UART layer.
15 * David S. Miller <davem@redhat.com>
16 */
17
18#include <linux/config.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/sched.h>
22#include <linux/errno.h>
23#include <linux/tty.h>
24#include <linux/tty_flip.h>
25#include <linux/major.h>
26#include <linux/string.h>
27#include <linux/ptrace.h>
28#include <linux/ioport.h>
29#include <linux/circ_buf.h>
30#include <linux/serial.h>
31#include <linux/sysrq.h>
32#include <linux/console.h>
33#include <linux/spinlock.h>
34#include <linux/slab.h>
35#include <linux/delay.h>
36#include <linux/init.h>
37
38#include <asm/io.h>
39#include <asm/irq.h>
40#include <asm/oplib.h>
41#include <asm/ebus.h>
42
43#if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
44#define SUPPORT_SYSRQ
45#endif
46
47#include <linux/serial_core.h>
48
49#include "suncore.h"
50#include "sunsab.h"
51
52struct uart_sunsab_port {
53 struct uart_port port; /* Generic UART port */
54 union sab82532_async_regs __iomem *regs; /* Chip registers */
55 unsigned long irqflags; /* IRQ state flags */
56 int dsr; /* Current DSR state */
57 unsigned int cec_timeout; /* Chip poll timeout... */
58 unsigned int tec_timeout; /* likewise */
59 unsigned char interrupt_mask0;/* ISR0 masking */
60 unsigned char interrupt_mask1;/* ISR1 masking */
61 unsigned char pvr_dtr_bit; /* Which PVR bit is DTR */
62 unsigned char pvr_dsr_bit; /* Which PVR bit is DSR */
63 int type; /* SAB82532 version */
64};
65
66/*
67 * This assumes you have a 29.4912 MHz clock for your UART.
68 */
69#define SAB_BASE_BAUD ( 29491200 / 16 )
70
71static char *sab82532_version[16] = {
72 "V1.0", "V2.0", "V3.2", "V(0x03)",
73 "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
74 "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
75 "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
76};
77
78#define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
79#define SAB82532_MAX_CEC_TIMEOUT 50000 /* 2.5 TX CLKs (at 50 baud) */
80
81#define SAB82532_RECV_FIFO_SIZE 32 /* Standard async fifo sizes */
82#define SAB82532_XMIT_FIFO_SIZE 32
83
84static __inline__ void sunsab_tec_wait(struct uart_sunsab_port *up)
85{
86 int timeout = up->tec_timeout;
87
88 while ((readb(&up->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
89 udelay(1);
90}
91
92static __inline__ void sunsab_cec_wait(struct uart_sunsab_port *up)
93{
94 int timeout = up->cec_timeout;
95
96 while ((readb(&up->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
97 udelay(1);
98}
99
100static struct tty_struct *
101receive_chars(struct uart_sunsab_port *up,
102 union sab82532_irq_status *stat,
103 struct pt_regs *regs)
104{
105 struct tty_struct *tty = NULL;
106 unsigned char buf[32];
107 int saw_console_brk = 0;
108 int free_fifo = 0;
109 int count = 0;
110 int i;
111
112 if (up->port.info != NULL) /* Unopened serial console */
113 tty = up->port.info->tty;
114
115 /* Read number of BYTES (Character + Status) available. */
116 if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
117 count = SAB82532_RECV_FIFO_SIZE;
118 free_fifo++;
119 }
120
121 if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
122 count = readb(&up->regs->r.rbcl) & (SAB82532_RECV_FIFO_SIZE - 1);
123 free_fifo++;
124 }
125
126 /* Issue a FIFO read command in case we where idle. */
127 if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
128 sunsab_cec_wait(up);
129 writeb(SAB82532_CMDR_RFRD, &up->regs->w.cmdr);
130 return tty;
131 }
132
133 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
134 free_fifo++;
135
136 /* Read the FIFO. */
137 for (i = 0; i < count; i++)
138 buf[i] = readb(&up->regs->r.rfifo[i]);
139
140 /* Issue Receive Message Complete command. */
141 if (free_fifo) {
142 sunsab_cec_wait(up);
143 writeb(SAB82532_CMDR_RMC, &up->regs->w.cmdr);
144 }
145
146 /* Count may be zero for BRK, so we check for it here */
147 if ((stat->sreg.isr1 & SAB82532_ISR1_BRK) &&
148 (up->port.line == up->port.cons->index))
149 saw_console_brk = 1;
150
151 for (i = 0; i < count; i++) {
152 unsigned char ch = buf[i];
153
154 if (tty == NULL) {
155 uart_handle_sysrq_char(&up->port, ch, regs);
156 continue;
157 }
158
159 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
160 tty->flip.work.func((void *)tty);
161 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
162 return tty; // if TTY_DONT_FLIP is set
163 }
164
165 *tty->flip.char_buf_ptr = ch;
166 *tty->flip.flag_buf_ptr = TTY_NORMAL;
167 up->port.icount.rx++;
168
169 if (unlikely(stat->sreg.isr0 & (SAB82532_ISR0_PERR |
170 SAB82532_ISR0_FERR |
171 SAB82532_ISR0_RFO)) ||
172 unlikely(stat->sreg.isr1 & SAB82532_ISR1_BRK)) {
173 /*
174 * For statistics only
175 */
176 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
177 stat->sreg.isr0 &= ~(SAB82532_ISR0_PERR |
178 SAB82532_ISR0_FERR);
179 up->port.icount.brk++;
180 /*
181 * We do the SysRQ and SAK checking
182 * here because otherwise the break
183 * may get masked by ignore_status_mask
184 * or read_status_mask.
185 */
186 if (uart_handle_break(&up->port))
187 continue;
188 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
189 up->port.icount.parity++;
190 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
191 up->port.icount.frame++;
192 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
193 up->port.icount.overrun++;
194
195 /*
196 * Mask off conditions which should be ingored.
197 */
198 stat->sreg.isr0 &= (up->port.read_status_mask & 0xff);
199 stat->sreg.isr1 &= ((up->port.read_status_mask >> 8) & 0xff);
200
201 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
202 *tty->flip.flag_buf_ptr = TTY_BREAK;
203 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
204 *tty->flip.flag_buf_ptr = TTY_PARITY;
205 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
206 *tty->flip.flag_buf_ptr = TTY_FRAME;
207 }
208
209 if (uart_handle_sysrq_char(&up->port, ch, regs))
210 continue;
211
212 if ((stat->sreg.isr0 & (up->port.ignore_status_mask & 0xff)) == 0 &&
213 (stat->sreg.isr1 & ((up->port.ignore_status_mask >> 8) & 0xff)) == 0){
214 tty->flip.flag_buf_ptr++;
215 tty->flip.char_buf_ptr++;
216 tty->flip.count++;
217 }
218 if ((stat->sreg.isr0 & SAB82532_ISR0_RFO) &&
219 tty->flip.count < TTY_FLIPBUF_SIZE) {
220 /*
221 * Overrun is special, since it's reported
222 * immediately, and doesn't affect the current
223 * character.
224 */
225 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
226 tty->flip.flag_buf_ptr++;
227 tty->flip.char_buf_ptr++;
228 tty->flip.count++;
229 }
230 }
231
232 if (saw_console_brk)
233 sun_do_break();
234
235 return tty;
236}
237
238static void sunsab_stop_tx(struct uart_port *, unsigned int);
239
240static void transmit_chars(struct uart_sunsab_port *up,
241 union sab82532_irq_status *stat)
242{
243 struct circ_buf *xmit = &up->port.info->xmit;
244 int i;
245
246 if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
247 up->interrupt_mask1 |= SAB82532_IMR1_ALLS;
248 writeb(up->interrupt_mask1, &up->regs->w.imr1);
249 set_bit(SAB82532_ALLS, &up->irqflags);
250 }
251
252#if 0 /* bde@nwlink.com says this check causes problems */
253 if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
254 return;
255#endif
256
257 if (!(readb(&up->regs->r.star) & SAB82532_STAR_XFW))
258 return;
259
260 set_bit(SAB82532_XPR, &up->irqflags);
261
262 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
263 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
264 writeb(up->interrupt_mask1, &up->regs->w.imr1);
265 uart_write_wakeup(&up->port);
266 return;
267 }
268
269 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
270 writeb(up->interrupt_mask1, &up->regs->w.imr1);
271 clear_bit(SAB82532_ALLS, &up->irqflags);
272
273 /* Stuff 32 bytes into Transmit FIFO. */
274 clear_bit(SAB82532_XPR, &up->irqflags);
275 for (i = 0; i < up->port.fifosize; i++) {
276 writeb(xmit->buf[xmit->tail],
277 &up->regs->w.xfifo[i]);
278 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
279 up->port.icount.tx++;
280 if (uart_circ_empty(xmit))
281 break;
282 }
283
284 /* Issue a Transmit Frame command. */
285 sunsab_cec_wait(up);
286 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
287
288 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
289 uart_write_wakeup(&up->port);
290
291 if (uart_circ_empty(xmit))
292 sunsab_stop_tx(&up->port, 0);
293}
294
295static void check_status(struct uart_sunsab_port *up,
296 union sab82532_irq_status *stat)
297{
298 if (stat->sreg.isr0 & SAB82532_ISR0_CDSC)
299 uart_handle_dcd_change(&up->port,
300 !(readb(&up->regs->r.vstr) & SAB82532_VSTR_CD));
301
302 if (stat->sreg.isr1 & SAB82532_ISR1_CSC)
303 uart_handle_cts_change(&up->port,
304 (readb(&up->regs->r.star) & SAB82532_STAR_CTS));
305
306 if ((readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ^ up->dsr) {
307 up->dsr = (readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ? 0 : 1;
308 up->port.icount.dsr++;
309 }
310
311 wake_up_interruptible(&up->port.info->delta_msr_wait);
312}
313
314static irqreturn_t sunsab_interrupt(int irq, void *dev_id, struct pt_regs *regs)
315{
316 struct uart_sunsab_port *up = dev_id;
317 struct tty_struct *tty;
318 union sab82532_irq_status status;
319 unsigned long flags;
320
321 spin_lock_irqsave(&up->port.lock, flags);
322
323 status.stat = 0;
324 if (readb(&up->regs->r.gis) & SAB82532_GIS_ISA0)
325 status.sreg.isr0 = readb(&up->regs->r.isr0);
326 if (readb(&up->regs->r.gis) & SAB82532_GIS_ISA1)
327 status.sreg.isr1 = readb(&up->regs->r.isr1);
328
329 tty = NULL;
330 if (status.stat) {
331 if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
332 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
333 (status.sreg.isr1 & SAB82532_ISR1_BRK))
334 tty = receive_chars(up, &status, regs);
335 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
336 (status.sreg.isr1 & SAB82532_ISR1_CSC))
337 check_status(up, &status);
338 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
339 transmit_chars(up, &status);
340 }
341
342 spin_unlock(&up->port.lock);
343
344 if (tty)
345 tty_flip_buffer_push(tty);
346
347 up++;
348
349 spin_lock(&up->port.lock);
350
351 status.stat = 0;
352 if (readb(&up->regs->r.gis) & SAB82532_GIS_ISB0)
353 status.sreg.isr0 = readb(&up->regs->r.isr0);
354 if (readb(&up->regs->r.gis) & SAB82532_GIS_ISB1)
355 status.sreg.isr1 = readb(&up->regs->r.isr1);
356
357 tty = NULL;
358 if (status.stat) {
359 if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
360 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
361 (status.sreg.isr1 & SAB82532_ISR1_BRK))
362
363 tty = receive_chars(up, &status, regs);
364 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
365 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
366 check_status(up, &status);
367 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
368 transmit_chars(up, &status);
369 }
370
371 spin_unlock_irqrestore(&up->port.lock, flags);
372
373 if (tty)
374 tty_flip_buffer_push(tty);
375
376 return IRQ_HANDLED;
377}
378
379/* port->lock is not held. */
380static unsigned int sunsab_tx_empty(struct uart_port *port)
381{
382 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
383 int ret;
384
385 /* Do not need a lock for a state test like this. */
386 if (test_bit(SAB82532_ALLS, &up->irqflags))
387 ret = TIOCSER_TEMT;
388 else
389 ret = 0;
390
391 return ret;
392}
393
394/* port->lock held by caller. */
395static void sunsab_set_mctrl(struct uart_port *port, unsigned int mctrl)
396{
397 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
398
399 if (mctrl & TIOCM_RTS) {
400 writeb(readb(&up->regs->rw.mode) & ~SAB82532_MODE_FRTS,
401 &up->regs->rw.mode);
402 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RTS,
403 &up->regs->rw.mode);
404 } else {
405 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_FRTS,
406 &up->regs->rw.mode);
407 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RTS,
408 &up->regs->rw.mode);
409 }
410 if (mctrl & TIOCM_DTR) {
411 writeb(readb(&up->regs->rw.pvr) & ~(up->pvr_dtr_bit), &up->regs->rw.pvr);
412 } else {
413 writeb(readb(&up->regs->rw.pvr) | up->pvr_dtr_bit, &up->regs->rw.pvr);
414 }
415}
416
417/* port->lock is not held. */
418static unsigned int sunsab_get_mctrl(struct uart_port *port)
419{
420 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
421 unsigned long flags;
422 unsigned char val;
423 unsigned int result;
424
425 result = 0;
426
427 spin_lock_irqsave(&up->port.lock, flags);
428
429 val = readb(&up->regs->r.pvr);
430 result |= (val & up->pvr_dsr_bit) ? 0 : TIOCM_DSR;
431
432 val = readb(&up->regs->r.vstr);
433 result |= (val & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR;
434
435 val = readb(&up->regs->r.star);
436 result |= (val & SAB82532_STAR_CTS) ? TIOCM_CTS : 0;
437
438 spin_unlock_irqrestore(&up->port.lock, flags);
439
440 return result;
441}
442
443/* port->lock held by caller. */
444static void sunsab_stop_tx(struct uart_port *port, unsigned int tty_stop)
445{
446 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
447
448 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
449 writeb(up->interrupt_mask1, &up->regs->w.imr1);
450}
451
452/* port->lock held by caller. */
453static void sunsab_start_tx(struct uart_port *port, unsigned int tty_start)
454{
455 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
456 struct circ_buf *xmit = &up->port.info->xmit;
457 int i;
458
459 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
460 writeb(up->interrupt_mask1, &up->regs->w.imr1);
461
462 if (!test_bit(SAB82532_XPR, &up->irqflags))
463 return;
464
465 clear_bit(SAB82532_ALLS, &up->irqflags);
466 clear_bit(SAB82532_XPR, &up->irqflags);
467
468 for (i = 0; i < up->port.fifosize; i++) {
469 writeb(xmit->buf[xmit->tail],
470 &up->regs->w.xfifo[i]);
471 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
472 up->port.icount.tx++;
473 if (uart_circ_empty(xmit))
474 break;
475 }
476
477 /* Issue a Transmit Frame command. */
478 sunsab_cec_wait(up);
479 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
480}
481
482/* port->lock is not held. */
483static void sunsab_send_xchar(struct uart_port *port, char ch)
484{
485 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
486 unsigned long flags;
487
488 spin_lock_irqsave(&up->port.lock, flags);
489
490 sunsab_tec_wait(up);
491 writeb(ch, &up->regs->w.tic);
492
493 spin_unlock_irqrestore(&up->port.lock, flags);
494}
495
496/* port->lock held by caller. */
497static void sunsab_stop_rx(struct uart_port *port)
498{
499 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
500
501 up->interrupt_mask0 |= SAB82532_ISR0_TCD;
502 writeb(up->interrupt_mask1, &up->regs->w.imr0);
503}
504
505/* port->lock held by caller. */
506static void sunsab_enable_ms(struct uart_port *port)
507{
508 /* For now we always receive these interrupts. */
509}
510
511/* port->lock is not held. */
512static void sunsab_break_ctl(struct uart_port *port, int break_state)
513{
514 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
515 unsigned long flags;
516 unsigned char val;
517
518 spin_lock_irqsave(&up->port.lock, flags);
519
520 val = readb(&up->regs->rw.dafo);
521 if (break_state)
522 val |= SAB82532_DAFO_XBRK;
523 else
524 val &= ~SAB82532_DAFO_XBRK;
525 writeb(val, &up->regs->rw.dafo);
526
527 spin_unlock_irqrestore(&up->port.lock, flags);
528}
529
530/* port->lock is not held. */
531static int sunsab_startup(struct uart_port *port)
532{
533 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
534 unsigned long flags;
535 unsigned char tmp;
536
537 spin_lock_irqsave(&up->port.lock, flags);
538
539 /*
540 * Wait for any commands or immediate characters
541 */
542 sunsab_cec_wait(up);
543 sunsab_tec_wait(up);
544
545 /*
546 * Clear the FIFO buffers.
547 */
548 writeb(SAB82532_CMDR_RRES, &up->regs->w.cmdr);
549 sunsab_cec_wait(up);
550 writeb(SAB82532_CMDR_XRES, &up->regs->w.cmdr);
551
552 /*
553 * Clear the interrupt registers.
554 */
555 (void) readb(&up->regs->r.isr0);
556 (void) readb(&up->regs->r.isr1);
557
558 /*
559 * Now, initialize the UART
560 */
561 writeb(0, &up->regs->w.ccr0); /* power-down */
562 writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
563 SAB82532_CCR0_SM_ASYNC, &up->regs->w.ccr0);
564 writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &up->regs->w.ccr1);
565 writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
566 SAB82532_CCR2_TOE, &up->regs->w.ccr2);
567 writeb(0, &up->regs->w.ccr3);
568 writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &up->regs->w.ccr4);
569 writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
570 SAB82532_MODE_RAC, &up->regs->w.mode);
571 writeb(SAB82532_RFC_DPS|SAB82532_RFC_RFTH_32, &up->regs->w.rfc);
572
573 tmp = readb(&up->regs->rw.ccr0);
574 tmp |= SAB82532_CCR0_PU; /* power-up */
575 writeb(tmp, &up->regs->rw.ccr0);
576
577 /*
578 * Finally, enable interrupts
579 */
580 up->interrupt_mask0 = (SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
581 SAB82532_IMR0_PLLA);
582 writeb(up->interrupt_mask0, &up->regs->w.imr0);
583 up->interrupt_mask1 = (SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
584 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
585 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
586 SAB82532_IMR1_XPR);
587 writeb(up->interrupt_mask1, &up->regs->w.imr1);
588 set_bit(SAB82532_ALLS, &up->irqflags);
589 set_bit(SAB82532_XPR, &up->irqflags);
590
591 spin_unlock_irqrestore(&up->port.lock, flags);
592
593 return 0;
594}
595
596/* port->lock is not held. */
597static void sunsab_shutdown(struct uart_port *port)
598{
599 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
600 unsigned long flags;
601 unsigned char tmp;
602
603 spin_lock_irqsave(&up->port.lock, flags);
604
605 /* Disable Interrupts */
606 up->interrupt_mask0 = 0xff;
607 writeb(up->interrupt_mask0, &up->regs->w.imr0);
608 up->interrupt_mask1 = 0xff;
609 writeb(up->interrupt_mask1, &up->regs->w.imr1);
610
611 /* Disable break condition */
612 tmp = readb(&up->regs->rw.dafo);
613 tmp &= ~SAB82532_DAFO_XBRK;
614 writeb(tmp, &up->regs->rw.dafo);
615
616 /* Disable Receiver */
617 tmp = readb(&up->regs->rw.mode);
618 tmp &= ~SAB82532_MODE_RAC;
619 writeb(tmp, &up->regs->rw.mode);
620
621 /*
622 * XXX FIXME
623 *
624 * If the chip is powered down here the system hangs/crashes during
625 * reboot or shutdown. This needs to be investigated further,
626 * similar behaviour occurs in 2.4 when the driver is configured
627 * as a module only. One hint may be that data is sometimes
628 * transmitted at 9600 baud during shutdown (regardless of the
629 * speed the chip was configured for when the port was open).
630 */
631#if 0
632 /* Power Down */
633 tmp = readb(&up->regs->rw.ccr0);
634 tmp &= ~SAB82532_CCR0_PU;
635 writeb(tmp, &up->regs->rw.ccr0);
636#endif
637
638 spin_unlock_irqrestore(&up->port.lock, flags);
639}
640
641/*
642 * This is used to figure out the divisor speeds.
643 *
644 * The formula is: Baud = SAB_BASE_BAUD / ((N + 1) * (1 << M)),
645 *
646 * with 0 <= N < 64 and 0 <= M < 16
647 */
648
649static void calc_ebrg(int baud, int *n_ret, int *m_ret)
650{
651 int n, m;
652
653 if (baud == 0) {
654 *n_ret = 0;
655 *m_ret = 0;
656 return;
657 }
658
659 /*
660 * We scale numbers by 10 so that we get better accuracy
661 * without having to use floating point. Here we increment m
662 * until n is within the valid range.
663 */
664 n = (SAB_BASE_BAUD * 10) / baud;
665 m = 0;
666 while (n >= 640) {
667 n = n / 2;
668 m++;
669 }
670 n = (n+5) / 10;
671 /*
672 * We try very hard to avoid speeds with M == 0 since they may
673 * not work correctly for XTAL frequences above 10 MHz.
674 */
675 if ((m == 0) && ((n & 1) == 0)) {
676 n = n / 2;
677 m++;
678 }
679 *n_ret = n - 1;
680 *m_ret = m;
681}
682
683/* Internal routine, port->lock is held and local interrupts are disabled. */
684static void sunsab_convert_to_sab(struct uart_sunsab_port *up, unsigned int cflag,
685 unsigned int iflag, int baud)
686{
687 unsigned int ebrg;
688 unsigned char dafo;
689 int bits, n, m;
690
691 /* Byte size and parity */
692 switch (cflag & CSIZE) {
693 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
694 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
695 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
696 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
697 /* Never happens, but GCC is too dumb to figure it out */
698 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
699 }
700
701 if (cflag & CSTOPB) {
702 dafo |= SAB82532_DAFO_STOP;
703 bits++;
704 }
705
706 if (cflag & PARENB) {
707 dafo |= SAB82532_DAFO_PARE;
708 bits++;
709 }
710
711 if (cflag & PARODD) {
712 dafo |= SAB82532_DAFO_PAR_ODD;
713 } else {
714 dafo |= SAB82532_DAFO_PAR_EVEN;
715 }
716
717 calc_ebrg(baud, &n, &m);
718
719 ebrg = n | (m << 6);
720
721 up->tec_timeout = (10 * 1000000) / baud;
722 up->cec_timeout = up->tec_timeout >> 2;
723
724 /* CTS flow control flags */
725 /* We encode read_status_mask and ignore_status_mask like so:
726 *
727 * ---------------------
728 * | ... | ISR1 | ISR0 |
729 * ---------------------
730 * .. 15 8 7 0
731 */
732
733 up->port.read_status_mask = (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
734 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF |
735 SAB82532_ISR0_CDSC);
736 up->port.read_status_mask |= (SAB82532_ISR1_CSC |
737 SAB82532_ISR1_ALLS |
738 SAB82532_ISR1_XPR) << 8;
739 if (iflag & INPCK)
740 up->port.read_status_mask |= (SAB82532_ISR0_PERR |
741 SAB82532_ISR0_FERR);
742 if (iflag & (BRKINT | PARMRK))
743 up->port.read_status_mask |= (SAB82532_ISR1_BRK << 8);
744
745 /*
746 * Characteres to ignore
747 */
748 up->port.ignore_status_mask = 0;
749 if (iflag & IGNPAR)
750 up->port.ignore_status_mask |= (SAB82532_ISR0_PERR |
751 SAB82532_ISR0_FERR);
752 if (iflag & IGNBRK) {
753 up->port.ignore_status_mask |= (SAB82532_ISR1_BRK << 8);
754 /*
755 * If we're ignoring parity and break indicators,
756 * ignore overruns too (for real raw support).
757 */
758 if (iflag & IGNPAR)
759 up->port.ignore_status_mask |= SAB82532_ISR0_RFO;
760 }
761
762 /*
763 * ignore all characters if CREAD is not set
764 */
765 if ((cflag & CREAD) == 0)
766 up->port.ignore_status_mask |= (SAB82532_ISR0_RPF |
767 SAB82532_ISR0_TCD);
768
769 /* Now bang the new settings into the chip. */
770 sunsab_cec_wait(up);
771 sunsab_tec_wait(up);
772 writeb(dafo, &up->regs->w.dafo);
773 writeb(ebrg & 0xff, &up->regs->w.bgr);
774 writeb((readb(&up->regs->rw.ccr2) & ~0xc0) | ((ebrg >> 2) & 0xc0),
775 &up->regs->rw.ccr2);
776
777 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RAC, &up->regs->rw.mode);
778
779}
780
781/* port->lock is not held. */
782static void sunsab_set_termios(struct uart_port *port, struct termios *termios,
783 struct termios *old)
784{
785 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
786 unsigned long flags;
787 int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
788
789 spin_lock_irqsave(&up->port.lock, flags);
790 sunsab_convert_to_sab(up, termios->c_cflag, termios->c_iflag, baud);
791 spin_unlock_irqrestore(&up->port.lock, flags);
792}
793
794static const char *sunsab_type(struct uart_port *port)
795{
796 struct uart_sunsab_port *up = (void *)port;
797 static char buf[36];
798
799 sprintf(buf, "SAB82532 %s", sab82532_version[up->type]);
800 return buf;
801}
802
803static void sunsab_release_port(struct uart_port *port)
804{
805}
806
807static int sunsab_request_port(struct uart_port *port)
808{
809 return 0;
810}
811
812static void sunsab_config_port(struct uart_port *port, int flags)
813{
814}
815
816static int sunsab_verify_port(struct uart_port *port, struct serial_struct *ser)
817{
818 return -EINVAL;
819}
820
821static struct uart_ops sunsab_pops = {
822 .tx_empty = sunsab_tx_empty,
823 .set_mctrl = sunsab_set_mctrl,
824 .get_mctrl = sunsab_get_mctrl,
825 .stop_tx = sunsab_stop_tx,
826 .start_tx = sunsab_start_tx,
827 .send_xchar = sunsab_send_xchar,
828 .stop_rx = sunsab_stop_rx,
829 .enable_ms = sunsab_enable_ms,
830 .break_ctl = sunsab_break_ctl,
831 .startup = sunsab_startup,
832 .shutdown = sunsab_shutdown,
833 .set_termios = sunsab_set_termios,
834 .type = sunsab_type,
835 .release_port = sunsab_release_port,
836 .request_port = sunsab_request_port,
837 .config_port = sunsab_config_port,
838 .verify_port = sunsab_verify_port,
839};
840
841static struct uart_driver sunsab_reg = {
842 .owner = THIS_MODULE,
843 .driver_name = "serial",
844 .devfs_name = "tts/",
845 .dev_name = "ttyS",
846 .major = TTY_MAJOR,
847};
848
849static struct uart_sunsab_port *sunsab_ports;
850static int num_channels;
851
852#ifdef CONFIG_SERIAL_SUNSAB_CONSOLE
853
854static __inline__ void sunsab_console_putchar(struct uart_sunsab_port *up, char c)
855{
856 unsigned long flags;
857
858 spin_lock_irqsave(&up->port.lock, flags);
859
860 sunsab_tec_wait(up);
861 writeb(c, &up->regs->w.tic);
862
863 spin_unlock_irqrestore(&up->port.lock, flags);
864}
865
866static void sunsab_console_write(struct console *con, const char *s, unsigned n)
867{
868 struct uart_sunsab_port *up = &sunsab_ports[con->index];
869 int i;
870
871 for (i = 0; i < n; i++) {
872 if (*s == '\n')
873 sunsab_console_putchar(up, '\r');
874 sunsab_console_putchar(up, *s++);
875 }
876 sunsab_tec_wait(up);
877}
878
879static int sunsab_console_setup(struct console *con, char *options)
880{
881 struct uart_sunsab_port *up = &sunsab_ports[con->index];
882 unsigned long flags;
883 int baud;
884
885 printk("Console: ttyS%d (SAB82532)\n",
886 (sunsab_reg.minor - 64) + con->index);
887
888 sunserial_console_termios(con);
889
890 /* Firmware console speed is limited to 150-->38400 baud so
891 * this hackish cflag thing is OK.
892 */
893 switch (con->cflag & CBAUD) {
894 case B150: baud = 150; break;
895 case B300: baud = 300; break;
896 case B600: baud = 600; break;
897 case B1200: baud = 1200; break;
898 case B2400: baud = 2400; break;
899 case B4800: baud = 4800; break;
900 default: case B9600: baud = 9600; break;
901 case B19200: baud = 19200; break;
902 case B38400: baud = 38400; break;
903 };
904
905 /*
906 * Temporary fix.
907 */
908 spin_lock_init(&up->port.lock);
909
910 /*
911 * Initialize the hardware
912 */
913 sunsab_startup(&up->port);
914
915 spin_lock_irqsave(&up->port.lock, flags);
916
917 /*
918 * Finally, enable interrupts
919 */
920 up->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
921 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
922 writeb(up->interrupt_mask0, &up->regs->w.imr0);
923 up->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
924 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
925 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
926 SAB82532_IMR1_XPR;
927 writeb(up->interrupt_mask1, &up->regs->w.imr1);
928
929 sunsab_convert_to_sab(up, con->cflag, 0, baud);
930 sunsab_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
931
932 spin_unlock_irqrestore(&up->port.lock, flags);
933
934 return 0;
935}
936
937static struct console sunsab_console = {
938 .name = "ttyS",
939 .write = sunsab_console_write,
940 .device = uart_console_device,
941 .setup = sunsab_console_setup,
942 .flags = CON_PRINTBUFFER,
943 .index = -1,
944 .data = &sunsab_reg,
945};
946#define SUNSAB_CONSOLE (&sunsab_console)
947
948static void __init sunsab_console_init(void)
949{
950 int i;
951
952 if (con_is_present())
953 return;
954
955 for (i = 0; i < num_channels; i++) {
956 int this_minor = sunsab_reg.minor + i;
957
958 if ((this_minor - 64) == (serial_console - 1))
959 break;
960 }
961 if (i == num_channels)
962 return;
963
964 sunsab_console.index = i;
965 register_console(&sunsab_console);
966}
967#else
968#define SUNSAB_CONSOLE (NULL)
969#define sunsab_console_init() do { } while (0)
970#endif
971
972static void __init for_each_sab_edev(void (*callback)(struct linux_ebus_device *, void *), void *arg)
973{
974 struct linux_ebus *ebus;
975 struct linux_ebus_device *edev = NULL;
976
977 for_each_ebus(ebus) {
978 for_each_ebusdev(edev, ebus) {
979 if (!strcmp(edev->prom_name, "se")) {
980 callback(edev, arg);
981 continue;
982 } else if (!strcmp(edev->prom_name, "serial")) {
983 char compat[32];
984 int clen;
985
986 /* On RIO this can be an SE, check it. We could
987 * just check ebus->is_rio, but this is more portable.
988 */
989 clen = prom_getproperty(edev->prom_node, "compatible",
990 compat, sizeof(compat));
991 if (clen > 0) {
992 if (strncmp(compat, "sab82532", 8) == 0) {
993 callback(edev, arg);
994 continue;
995 }
996 }
997 }
998 }
999 }
1000}
1001
1002static void __init sab_count_callback(struct linux_ebus_device *edev, void *arg)
1003{
1004 int *count_p = arg;
1005
1006 (*count_p)++;
1007}
1008
1009static void __init sab_attach_callback(struct linux_ebus_device *edev, void *arg)
1010{
1011 int *instance_p = arg;
1012 struct uart_sunsab_port *up;
1013 unsigned long regs, offset;
1014 int i;
1015
1016 /* Note: ports are located in reverse order */
1017 regs = edev->resource[0].start;
1018 offset = sizeof(union sab82532_async_regs);
1019 for (i = 0; i < 2; i++) {
1020 up = &sunsab_ports[(*instance_p * 2) + 1 - i];
1021
1022 memset(up, 0, sizeof(*up));
1023 up->regs = ioremap(regs + offset, sizeof(union sab82532_async_regs));
1024 up->port.irq = edev->irqs[0];
1025 up->port.fifosize = SAB82532_XMIT_FIFO_SIZE;
1026 up->port.mapbase = (unsigned long)up->regs;
1027 up->port.iotype = SERIAL_IO_MEM;
1028
1029 writeb(SAB82532_IPC_IC_ACT_LOW, &up->regs->w.ipc);
1030
1031 offset -= sizeof(union sab82532_async_regs);
1032 }
1033
1034 (*instance_p)++;
1035}
1036
1037static int __init probe_for_sabs(void)
1038{
1039 int this_sab = 0;
1040
1041 /* Find device instances. */
1042 for_each_sab_edev(&sab_count_callback, &this_sab);
1043 if (!this_sab)
1044 return -ENODEV;
1045
1046 /* Allocate tables. */
1047 sunsab_ports = kmalloc(sizeof(struct uart_sunsab_port) * this_sab * 2,
1048 GFP_KERNEL);
1049 if (!sunsab_ports)
1050 return -ENOMEM;
1051
1052 num_channels = this_sab * 2;
1053
1054 this_sab = 0;
1055 for_each_sab_edev(&sab_attach_callback, &this_sab);
1056 return 0;
1057}
1058
1059static void __init sunsab_init_hw(void)
1060{
1061 int i;
1062
1063 for (i = 0; i < num_channels; i++) {
1064 struct uart_sunsab_port *up = &sunsab_ports[i];
1065
1066 up->port.line = i;
1067 up->port.ops = &sunsab_pops;
1068 up->port.type = PORT_SUNSAB;
1069 up->port.uartclk = SAB_BASE_BAUD;
1070
1071 up->type = readb(&up->regs->r.vstr) & 0x0f;
1072 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &up->regs->w.pcr);
1073 writeb(0xff, &up->regs->w.pim);
1074 if (up->port.line == 0) {
1075 up->pvr_dsr_bit = (1 << 0);
1076 up->pvr_dtr_bit = (1 << 1);
1077 } else {
1078 up->pvr_dsr_bit = (1 << 3);
1079 up->pvr_dtr_bit = (1 << 2);
1080 }
1081 writeb((1 << 1) | (1 << 2) | (1 << 4), &up->regs->w.pvr);
1082 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_FRTS,
1083 &up->regs->rw.mode);
1084 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RTS,
1085 &up->regs->rw.mode);
1086
1087 up->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
1088 up->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
1089
1090 if (!(up->port.line & 0x01)) {
1091 if (request_irq(up->port.irq, sunsab_interrupt,
1092 SA_SHIRQ, "serial(sab82532)", up)) {
1093 printk("sunsab%d: can't get IRQ %x\n",
1094 i, up->port.irq);
1095 continue;
1096 }
1097 }
1098 }
1099}
1100
1101static int __init sunsab_init(void)
1102{
1103 int ret = probe_for_sabs();
1104 int i;
1105
1106 if (ret < 0)
1107 return ret;
1108
1109 sunsab_init_hw();
1110
1111 sunsab_reg.minor = sunserial_current_minor;
1112 sunsab_reg.nr = num_channels;
1113 sunsab_reg.cons = SUNSAB_CONSOLE;
1114
1115 ret = uart_register_driver(&sunsab_reg);
1116 if (ret < 0) {
1117 int i;
1118
1119 for (i = 0; i < num_channels; i++) {
1120 struct uart_sunsab_port *up = &sunsab_ports[i];
1121
1122 if (!(up->port.line & 0x01))
1123 free_irq(up->port.irq, up);
1124 iounmap(up->regs);
1125 }
1126 kfree(sunsab_ports);
1127 sunsab_ports = NULL;
1128
1129 return ret;
1130 }
1131
1132 sunserial_current_minor += num_channels;
1133
1134 sunsab_console_init();
1135
1136 for (i = 0; i < num_channels; i++) {
1137 struct uart_sunsab_port *up = &sunsab_ports[i];
1138
1139 uart_add_one_port(&sunsab_reg, &up->port);
1140 }
1141
1142 return 0;
1143}
1144
1145static void __exit sunsab_exit(void)
1146{
1147 int i;
1148
1149 for (i = 0; i < num_channels; i++) {
1150 struct uart_sunsab_port *up = &sunsab_ports[i];
1151
1152 uart_remove_one_port(&sunsab_reg, &up->port);
1153
1154 if (!(up->port.line & 0x01))
1155 free_irq(up->port.irq, up);
1156 iounmap(up->regs);
1157 }
1158
1159 sunserial_current_minor -= num_channels;
1160 uart_unregister_driver(&sunsab_reg);
1161
1162 kfree(sunsab_ports);
1163 sunsab_ports = NULL;
1164}
1165
1166module_init(sunsab_init);
1167module_exit(sunsab_exit);
1168
1169MODULE_AUTHOR("Eddie C. Dost and David S. Miller");
1170MODULE_DESCRIPTION("Sun SAB82532 serial port driver");
1171MODULE_LICENSE("GPL");
diff --git a/drivers/serial/sunsab.h b/drivers/serial/sunsab.h
new file mode 100644
index 000000000000..686086fcbbf5
--- /dev/null
+++ b/drivers/serial/sunsab.h
@@ -0,0 +1,321 @@
1/* sunsab.h: Register Definitions for the Siemens SAB82532 DUSCC
2 *
3 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
4 */
5
6#ifndef _SUNSAB_H
7#define _SUNSAB_H
8
9struct sab82532_async_rd_regs {
10 u8 rfifo[0x20]; /* Receive FIFO */
11 u8 star; /* Status Register */
12 u8 __pad1;
13 u8 mode; /* Mode Register */
14 u8 timr; /* Timer Register */
15 u8 xon; /* XON Character */
16 u8 xoff; /* XOFF Character */
17 u8 tcr; /* Termination Character Register */
18 u8 dafo; /* Data Format */
19 u8 rfc; /* RFIFO Control Register */
20 u8 __pad2;
21 u8 rbcl; /* Receive Byte Count Low */
22 u8 rbch; /* Receive Byte Count High */
23 u8 ccr0; /* Channel Configuration Register 0 */
24 u8 ccr1; /* Channel Configuration Register 1 */
25 u8 ccr2; /* Channel Configuration Register 2 */
26 u8 ccr3; /* Channel Configuration Register 3 */
27 u8 __pad3[4];
28 u8 vstr; /* Version Status Register */
29 u8 __pad4[3];
30 u8 gis; /* Global Interrupt Status */
31 u8 ipc; /* Interrupt Port Configuration */
32 u8 isr0; /* Interrupt Status 0 */
33 u8 isr1; /* Interrupt Status 1 */
34 u8 pvr; /* Port Value Register */
35 u8 pis; /* Port Interrupt Status */
36 u8 pcr; /* Port Configuration Register */
37 u8 ccr4; /* Channel Configuration Register 4 */
38};
39
40struct sab82532_async_wr_regs {
41 u8 xfifo[0x20]; /* Transmit FIFO */
42 u8 cmdr; /* Command Register */
43 u8 __pad1;
44 u8 mode;
45 u8 timr;
46 u8 xon;
47 u8 xoff;
48 u8 tcr;
49 u8 dafo;
50 u8 rfc;
51 u8 __pad2;
52 u8 xbcl; /* Transmit Byte Count Low */
53 u8 xbch; /* Transmit Byte Count High */
54 u8 ccr0;
55 u8 ccr1;
56 u8 ccr2;
57 u8 ccr3;
58 u8 tsax; /* Time-Slot Assignment Reg. Transmit */
59 u8 tsar; /* Time-Slot Assignment Reg. Receive */
60 u8 xccr; /* Transmit Channel Capacity Register */
61 u8 rccr; /* Receive Channel Capacity Register */
62 u8 bgr; /* Baud Rate Generator Register */
63 u8 tic; /* Transmit Immediate Character */
64 u8 mxn; /* Mask XON Character */
65 u8 mxf; /* Mask XOFF Character */
66 u8 iva; /* Interrupt Vector Address */
67 u8 ipc;
68 u8 imr0; /* Interrupt Mask Register 0 */
69 u8 imr1; /* Interrupt Mask Register 1 */
70 u8 pvr;
71 u8 pim; /* Port Interrupt Mask */
72 u8 pcr;
73 u8 ccr4;
74};
75
76struct sab82532_async_rw_regs { /* Read/Write registers */
77 u8 __pad1[0x20];
78 u8 __pad2;
79 u8 __pad3;
80 u8 mode;
81 u8 timr;
82 u8 xon;
83 u8 xoff;
84 u8 tcr;
85 u8 dafo;
86 u8 rfc;
87 u8 __pad4;
88 u8 __pad5;
89 u8 __pad6;
90 u8 ccr0;
91 u8 ccr1;
92 u8 ccr2;
93 u8 ccr3;
94 u8 __pad7;
95 u8 __pad8;
96 u8 __pad9;
97 u8 __pad10;
98 u8 __pad11;
99 u8 __pad12;
100 u8 __pad13;
101 u8 __pad14;
102 u8 __pad15;
103 u8 ipc;
104 u8 __pad16;
105 u8 __pad17;
106 u8 pvr;
107 u8 __pad18;
108 u8 pcr;
109 u8 ccr4;
110};
111
112union sab82532_async_regs {
113 __volatile__ struct sab82532_async_rd_regs r;
114 __volatile__ struct sab82532_async_wr_regs w;
115 __volatile__ struct sab82532_async_rw_regs rw;
116};
117
118union sab82532_irq_status {
119 unsigned short stat;
120 struct {
121 unsigned char isr0;
122 unsigned char isr1;
123 } sreg;
124};
125
126/* irqflags bits */
127#define SAB82532_ALLS 0x00000001
128#define SAB82532_XPR 0x00000002
129
130/* RFIFO Status Byte */
131#define SAB82532_RSTAT_PE 0x80
132#define SAB82532_RSTAT_FE 0x40
133#define SAB82532_RSTAT_PARITY 0x01
134
135/* Status Register (STAR) */
136#define SAB82532_STAR_XDOV 0x80
137#define SAB82532_STAR_XFW 0x40
138#define SAB82532_STAR_RFNE 0x20
139#define SAB82532_STAR_FCS 0x10
140#define SAB82532_STAR_TEC 0x08
141#define SAB82532_STAR_CEC 0x04
142#define SAB82532_STAR_CTS 0x02
143
144/* Command Register (CMDR) */
145#define SAB82532_CMDR_RMC 0x80
146#define SAB82532_CMDR_RRES 0x40
147#define SAB82532_CMDR_RFRD 0x20
148#define SAB82532_CMDR_STI 0x10
149#define SAB82532_CMDR_XF 0x08
150#define SAB82532_CMDR_XRES 0x01
151
152/* Mode Register (MODE) */
153#define SAB82532_MODE_FRTS 0x40
154#define SAB82532_MODE_FCTS 0x20
155#define SAB82532_MODE_FLON 0x10
156#define SAB82532_MODE_RAC 0x08
157#define SAB82532_MODE_RTS 0x04
158#define SAB82532_MODE_TRS 0x02
159#define SAB82532_MODE_TLP 0x01
160
161/* Timer Register (TIMR) */
162#define SAB82532_TIMR_CNT_MASK 0xe0
163#define SAB82532_TIMR_VALUE_MASK 0x1f
164
165/* Data Format (DAFO) */
166#define SAB82532_DAFO_XBRK 0x40
167#define SAB82532_DAFO_STOP 0x20
168#define SAB82532_DAFO_PAR_SPACE 0x00
169#define SAB82532_DAFO_PAR_ODD 0x08
170#define SAB82532_DAFO_PAR_EVEN 0x10
171#define SAB82532_DAFO_PAR_MARK 0x18
172#define SAB82532_DAFO_PARE 0x04
173#define SAB82532_DAFO_CHL8 0x00
174#define SAB82532_DAFO_CHL7 0x01
175#define SAB82532_DAFO_CHL6 0x02
176#define SAB82532_DAFO_CHL5 0x03
177
178/* RFIFO Control Register (RFC) */
179#define SAB82532_RFC_DPS 0x40
180#define SAB82532_RFC_DXS 0x20
181#define SAB82532_RFC_RFDF 0x10
182#define SAB82532_RFC_RFTH_1 0x00
183#define SAB82532_RFC_RFTH_4 0x04
184#define SAB82532_RFC_RFTH_16 0x08
185#define SAB82532_RFC_RFTH_32 0x0c
186#define SAB82532_RFC_TCDE 0x01
187
188/* Received Byte Count High (RBCH) */
189#define SAB82532_RBCH_DMA 0x80
190#define SAB82532_RBCH_CAS 0x20
191
192/* Transmit Byte Count High (XBCH) */
193#define SAB82532_XBCH_DMA 0x80
194#define SAB82532_XBCH_CAS 0x20
195#define SAB82532_XBCH_XC 0x10
196
197/* Channel Configuration Register 0 (CCR0) */
198#define SAB82532_CCR0_PU 0x80
199#define SAB82532_CCR0_MCE 0x40
200#define SAB82532_CCR0_SC_NRZ 0x00
201#define SAB82532_CCR0_SC_NRZI 0x08
202#define SAB82532_CCR0_SC_FM0 0x10
203#define SAB82532_CCR0_SC_FM1 0x14
204#define SAB82532_CCR0_SC_MANCH 0x18
205#define SAB82532_CCR0_SM_HDLC 0x00
206#define SAB82532_CCR0_SM_SDLC_LOOP 0x01
207#define SAB82532_CCR0_SM_BISYNC 0x02
208#define SAB82532_CCR0_SM_ASYNC 0x03
209
210/* Channel Configuration Register 1 (CCR1) */
211#define SAB82532_CCR1_ODS 0x10
212#define SAB82532_CCR1_BCR 0x08
213#define SAB82532_CCR1_CM_MASK 0x07
214
215/* Channel Configuration Register 2 (CCR2) */
216#define SAB82532_CCR2_SOC1 0x80
217#define SAB82532_CCR2_SOC0 0x40
218#define SAB82532_CCR2_BR9 0x80
219#define SAB82532_CCR2_BR8 0x40
220#define SAB82532_CCR2_BDF 0x20
221#define SAB82532_CCR2_SSEL 0x10
222#define SAB82532_CCR2_XCS0 0x20
223#define SAB82532_CCR2_RCS0 0x10
224#define SAB82532_CCR2_TOE 0x08
225#define SAB82532_CCR2_RWX 0x04
226#define SAB82532_CCR2_DIV 0x01
227
228/* Channel Configuration Register 3 (CCR3) */
229#define SAB82532_CCR3_PSD 0x01
230
231/* Time Slot Assignment Register Transmit (TSAX) */
232#define SAB82532_TSAX_TSNX_MASK 0xfc
233#define SAB82532_TSAX_XCS2 0x02 /* see also CCR2 */
234#define SAB82532_TSAX_XCS1 0x01
235
236/* Time Slot Assignment Register Receive (TSAR) */
237#define SAB82532_TSAR_TSNR_MASK 0xfc
238#define SAB82532_TSAR_RCS2 0x02 /* see also CCR2 */
239#define SAB82532_TSAR_RCS1 0x01
240
241/* Version Status Register (VSTR) */
242#define SAB82532_VSTR_CD 0x80
243#define SAB82532_VSTR_DPLA 0x40
244#define SAB82532_VSTR_VN_MASK 0x0f
245#define SAB82532_VSTR_VN_1 0x00
246#define SAB82532_VSTR_VN_2 0x01
247#define SAB82532_VSTR_VN_3_2 0x02
248
249/* Global Interrupt Status Register (GIS) */
250#define SAB82532_GIS_PI 0x80
251#define SAB82532_GIS_ISA1 0x08
252#define SAB82532_GIS_ISA0 0x04
253#define SAB82532_GIS_ISB1 0x02
254#define SAB82532_GIS_ISB0 0x01
255
256/* Interrupt Vector Address (IVA) */
257#define SAB82532_IVA_MASK 0xf1
258
259/* Interrupt Port Configuration (IPC) */
260#define SAB82532_IPC_VIS 0x80
261#define SAB82532_IPC_SLA1 0x10
262#define SAB82532_IPC_SLA0 0x08
263#define SAB82532_IPC_CASM 0x04
264#define SAB82532_IPC_IC_OPEN_DRAIN 0x00
265#define SAB82532_IPC_IC_ACT_LOW 0x01
266#define SAB82532_IPC_IC_ACT_HIGH 0x03
267
268/* Interrupt Status Register 0 (ISR0) */
269#define SAB82532_ISR0_TCD 0x80
270#define SAB82532_ISR0_TIME 0x40
271#define SAB82532_ISR0_PERR 0x20
272#define SAB82532_ISR0_FERR 0x10
273#define SAB82532_ISR0_PLLA 0x08
274#define SAB82532_ISR0_CDSC 0x04
275#define SAB82532_ISR0_RFO 0x02
276#define SAB82532_ISR0_RPF 0x01
277
278/* Interrupt Status Register 1 (ISR1) */
279#define SAB82532_ISR1_BRK 0x80
280#define SAB82532_ISR1_BRKT 0x40
281#define SAB82532_ISR1_ALLS 0x20
282#define SAB82532_ISR1_XOFF 0x10
283#define SAB82532_ISR1_TIN 0x08
284#define SAB82532_ISR1_CSC 0x04
285#define SAB82532_ISR1_XON 0x02
286#define SAB82532_ISR1_XPR 0x01
287
288/* Interrupt Mask Register 0 (IMR0) */
289#define SAB82532_IMR0_TCD 0x80
290#define SAB82532_IMR0_TIME 0x40
291#define SAB82532_IMR0_PERR 0x20
292#define SAB82532_IMR0_FERR 0x10
293#define SAB82532_IMR0_PLLA 0x08
294#define SAB82532_IMR0_CDSC 0x04
295#define SAB82532_IMR0_RFO 0x02
296#define SAB82532_IMR0_RPF 0x01
297
298/* Interrupt Mask Register 1 (IMR1) */
299#define SAB82532_IMR1_BRK 0x80
300#define SAB82532_IMR1_BRKT 0x40
301#define SAB82532_IMR1_ALLS 0x20
302#define SAB82532_IMR1_XOFF 0x10
303#define SAB82532_IMR1_TIN 0x08
304#define SAB82532_IMR1_CSC 0x04
305#define SAB82532_IMR1_XON 0x02
306#define SAB82532_IMR1_XPR 0x01
307
308/* Port Interrupt Status Register (PIS) */
309#define SAB82532_PIS_SYNC_B 0x08
310#define SAB82532_PIS_DTR_B 0x04
311#define SAB82532_PIS_DTR_A 0x02
312#define SAB82532_PIS_SYNC_A 0x01
313
314/* Channel Configuration Register 4 (CCR4) */
315#define SAB82532_CCR4_MCK4 0x80
316#define SAB82532_CCR4_EBRG 0x40
317#define SAB82532_CCR4_TST1 0x20
318#define SAB82532_CCR4_ICD 0x10
319
320
321#endif /* !(_SUNSAB_H) */
diff --git a/drivers/serial/sunsu.c b/drivers/serial/sunsu.c
new file mode 100644
index 000000000000..23d19d394320
--- /dev/null
+++ b/drivers/serial/sunsu.c
@@ -0,0 +1,1742 @@
1/* $Id: su.c,v 1.55 2002/01/08 16:00:16 davem Exp $
2 * su.c: Small serial driver for keyboard/mouse interface on sparc32/PCI
3 *
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1998-1999 Pete Zaitcev (zaitcev@yahoo.com)
6 *
7 * This is mainly a variation of 8250.c, credits go to authors mentioned
8 * therein. In fact this driver should be merged into the generic 8250.c
9 * infrastructure perhaps using a 8250_sparc.c module.
10 *
11 * Fixed to use tty_get_baud_rate().
12 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
13 *
14 * Converted to new 2.5.x UART layer.
15 * David S. Miller (davem@redhat.com), 2002-Jul-29
16 */
17
18#include <linux/config.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/sched.h>
22#include <linux/spinlock.h>
23#include <linux/errno.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/major.h>
27#include <linux/string.h>
28#include <linux/ptrace.h>
29#include <linux/ioport.h>
30#include <linux/circ_buf.h>
31#include <linux/serial.h>
32#include <linux/sysrq.h>
33#include <linux/console.h>
34#ifdef CONFIG_SERIO
35#include <linux/serio.h>
36#endif
37#include <linux/serial_reg.h>
38#include <linux/init.h>
39#include <linux/delay.h>
40
41#include <asm/io.h>
42#include <asm/irq.h>
43#include <asm/oplib.h>
44#include <asm/ebus.h>
45#ifdef CONFIG_SPARC64
46#include <asm/isa.h>
47#endif
48
49#if defined(CONFIG_SERIAL_SUNSU_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
50#define SUPPORT_SYSRQ
51#endif
52
53#include <linux/serial_core.h>
54
55#include "suncore.h"
56
57/* We are on a NS PC87303 clocked with 24.0 MHz, which results
58 * in a UART clock of 1.8462 MHz.
59 */
60#define SU_BASE_BAUD (1846200 / 16)
61
62enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
63static char *su_typev[] = { "su(???)", "su(mouse)", "su(kbd)", "su(serial)" };
64
65/*
66 * Here we define the default xmit fifo size used for each type of UART.
67 */
68static const struct serial_uart_config uart_config[PORT_MAX_8250+1] = {
69 { "unknown", 1, 0 },
70 { "8250", 1, 0 },
71 { "16450", 1, 0 },
72 { "16550", 1, 0 },
73 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
74 { "Cirrus", 1, 0 },
75 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
76 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
77 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO },
78 { "Startech", 1, 0 },
79 { "16C950/954", 128, UART_CLEAR_FIFO | UART_USE_FIFO },
80 { "ST16654", 64, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
81 { "XR16850", 128, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
82 { "RSA", 2048, UART_CLEAR_FIFO | UART_USE_FIFO }
83};
84
85struct uart_sunsu_port {
86 struct uart_port port;
87 unsigned char acr;
88 unsigned char ier;
89 unsigned short rev;
90 unsigned char lcr;
91 unsigned int lsr_break_flag;
92 unsigned int cflag;
93
94 /* Probing information. */
95 enum su_type su_type;
96 unsigned int type_probed; /* XXX Stupid */
97 int port_node;
98
99#ifdef CONFIG_SERIO
100 struct serio *serio;
101 int serio_open;
102#endif
103};
104
105#define _INLINE_
106
107static _INLINE_ unsigned int serial_in(struct uart_sunsu_port *up, int offset)
108{
109 offset <<= up->port.regshift;
110
111 switch (up->port.iotype) {
112 case SERIAL_IO_HUB6:
113 outb(up->port.hub6 - 1 + offset, up->port.iobase);
114 return inb(up->port.iobase + 1);
115
116 case SERIAL_IO_MEM:
117 return readb(up->port.membase + offset);
118
119 default:
120 return inb(up->port.iobase + offset);
121 }
122}
123
124static _INLINE_ void
125serial_out(struct uart_sunsu_port *up, int offset, int value)
126{
127#ifndef CONFIG_SPARC64
128 /*
129 * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
130 * connected with a gate then go to SlavIO. When IRQ4 goes tristated
131 * gate outputs a logical one. Since we use level triggered interrupts
132 * we have lockup and watchdog reset. We cannot mask IRQ because
133 * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
134 * This problem is similar to what Alpha people suffer, see serial.c.
135 */
136 if (offset == UART_MCR)
137 value |= UART_MCR_OUT2;
138#endif
139 offset <<= up->port.regshift;
140
141 switch (up->port.iotype) {
142 case SERIAL_IO_HUB6:
143 outb(up->port.hub6 - 1 + offset, up->port.iobase);
144 outb(value, up->port.iobase + 1);
145 break;
146
147 case SERIAL_IO_MEM:
148 writeb(value, up->port.membase + offset);
149 break;
150
151 default:
152 outb(value, up->port.iobase + offset);
153 }
154}
155
156/*
157 * We used to support using pause I/O for certain machines. We
158 * haven't supported this for a while, but just in case it's badly
159 * needed for certain old 386 machines, I've left these #define's
160 * in....
161 */
162#define serial_inp(up, offset) serial_in(up, offset)
163#define serial_outp(up, offset, value) serial_out(up, offset, value)
164
165
166/*
167 * For the 16C950
168 */
169static void serial_icr_write(struct uart_sunsu_port *up, int offset, int value)
170{
171 serial_out(up, UART_SCR, offset);
172 serial_out(up, UART_ICR, value);
173}
174
175#if 0 /* Unused currently */
176static unsigned int serial_icr_read(struct uart_sunsu_port *up, int offset)
177{
178 unsigned int value;
179
180 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
181 serial_out(up, UART_SCR, offset);
182 value = serial_in(up, UART_ICR);
183 serial_icr_write(up, UART_ACR, up->acr);
184
185 return value;
186}
187#endif
188
189#ifdef CONFIG_SERIAL_8250_RSA
190/*
191 * Attempts to turn on the RSA FIFO. Returns zero on failure.
192 * We set the port uart clock rate if we succeed.
193 */
194static int __enable_rsa(struct uart_sunsu_port *up)
195{
196 unsigned char mode;
197 int result;
198
199 mode = serial_inp(up, UART_RSA_MSR);
200 result = mode & UART_RSA_MSR_FIFO;
201
202 if (!result) {
203 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
204 mode = serial_inp(up, UART_RSA_MSR);
205 result = mode & UART_RSA_MSR_FIFO;
206 }
207
208 if (result)
209 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
210
211 return result;
212}
213
214static void enable_rsa(struct uart_sunsu_port *up)
215{
216 if (up->port.type == PORT_RSA) {
217 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
218 spin_lock_irq(&up->port.lock);
219 __enable_rsa(up);
220 spin_unlock_irq(&up->port.lock);
221 }
222 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
223 serial_outp(up, UART_RSA_FRR, 0);
224 }
225}
226
227/*
228 * Attempts to turn off the RSA FIFO. Returns zero on failure.
229 * It is unknown why interrupts were disabled in here. However,
230 * the caller is expected to preserve this behaviour by grabbing
231 * the spinlock before calling this function.
232 */
233static void disable_rsa(struct uart_sunsu_port *up)
234{
235 unsigned char mode;
236 int result;
237
238 if (up->port.type == PORT_RSA &&
239 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
240 spin_lock_irq(&up->port.lock);
241
242 mode = serial_inp(up, UART_RSA_MSR);
243 result = !(mode & UART_RSA_MSR_FIFO);
244
245 if (!result) {
246 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
247 mode = serial_inp(up, UART_RSA_MSR);
248 result = !(mode & UART_RSA_MSR_FIFO);
249 }
250
251 if (result)
252 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
253 spin_unlock_irq(&up->port.lock);
254 }
255}
256#endif /* CONFIG_SERIAL_8250_RSA */
257
258static void sunsu_stop_tx(struct uart_port *port, unsigned int tty_stop)
259{
260 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
261
262 if (up->ier & UART_IER_THRI) {
263 up->ier &= ~UART_IER_THRI;
264 serial_out(up, UART_IER, up->ier);
265 }
266 if (up->port.type == PORT_16C950 && tty_stop) {
267 up->acr |= UART_ACR_TXDIS;
268 serial_icr_write(up, UART_ACR, up->acr);
269 }
270}
271
272static void sunsu_start_tx(struct uart_port *port, unsigned int tty_start)
273{
274 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
275
276 if (!(up->ier & UART_IER_THRI)) {
277 up->ier |= UART_IER_THRI;
278 serial_out(up, UART_IER, up->ier);
279 }
280 /*
281 * We only do this from uart_start
282 */
283 if (tty_start && up->port.type == PORT_16C950) {
284 up->acr &= ~UART_ACR_TXDIS;
285 serial_icr_write(up, UART_ACR, up->acr);
286 }
287}
288
289static void sunsu_stop_rx(struct uart_port *port)
290{
291 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
292 unsigned long flags;
293
294 spin_lock_irqsave(&up->port.lock, flags);
295 up->ier &= ~UART_IER_RLSI;
296 up->port.read_status_mask &= ~UART_LSR_DR;
297 serial_out(up, UART_IER, up->ier);
298 spin_unlock_irqrestore(&up->port.lock, flags);
299}
300
301static void sunsu_enable_ms(struct uart_port *port)
302{
303 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
304 unsigned long flags;
305
306 spin_lock_irqsave(&up->port.lock, flags);
307 up->ier |= UART_IER_MSI;
308 serial_out(up, UART_IER, up->ier);
309 spin_unlock_irqrestore(&up->port.lock, flags);
310}
311
312static _INLINE_ struct tty_struct *
313receive_chars(struct uart_sunsu_port *up, unsigned char *status, struct pt_regs *regs)
314{
315 struct tty_struct *tty = up->port.info->tty;
316 unsigned char ch;
317 int max_count = 256;
318 int saw_console_brk = 0;
319
320 do {
321 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
322 tty->flip.work.func((void *)tty);
323 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
324 return tty; // if TTY_DONT_FLIP is set
325 }
326 ch = serial_inp(up, UART_RX);
327 *tty->flip.char_buf_ptr = ch;
328 *tty->flip.flag_buf_ptr = TTY_NORMAL;
329 up->port.icount.rx++;
330
331 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
332 UART_LSR_FE | UART_LSR_OE))) {
333 /*
334 * For statistics only
335 */
336 if (*status & UART_LSR_BI) {
337 *status &= ~(UART_LSR_FE | UART_LSR_PE);
338 up->port.icount.brk++;
339 if (up->port.cons != NULL &&
340 up->port.line == up->port.cons->index)
341 saw_console_brk = 1;
342 /*
343 * We do the SysRQ and SAK checking
344 * here because otherwise the break
345 * may get masked by ignore_status_mask
346 * or read_status_mask.
347 */
348 if (uart_handle_break(&up->port))
349 goto ignore_char;
350 } else if (*status & UART_LSR_PE)
351 up->port.icount.parity++;
352 else if (*status & UART_LSR_FE)
353 up->port.icount.frame++;
354 if (*status & UART_LSR_OE)
355 up->port.icount.overrun++;
356
357 /*
358 * Mask off conditions which should be ingored.
359 */
360 *status &= up->port.read_status_mask;
361
362 if (up->port.cons != NULL &&
363 up->port.line == up->port.cons->index) {
364 /* Recover the break flag from console xmit */
365 *status |= up->lsr_break_flag;
366 up->lsr_break_flag = 0;
367 }
368
369 if (*status & UART_LSR_BI) {
370 *tty->flip.flag_buf_ptr = TTY_BREAK;
371 } else if (*status & UART_LSR_PE)
372 *tty->flip.flag_buf_ptr = TTY_PARITY;
373 else if (*status & UART_LSR_FE)
374 *tty->flip.flag_buf_ptr = TTY_FRAME;
375 }
376 if (uart_handle_sysrq_char(&up->port, ch, regs))
377 goto ignore_char;
378 if ((*status & up->port.ignore_status_mask) == 0) {
379 tty->flip.flag_buf_ptr++;
380 tty->flip.char_buf_ptr++;
381 tty->flip.count++;
382 }
383 if ((*status & UART_LSR_OE) &&
384 tty->flip.count < TTY_FLIPBUF_SIZE) {
385 /*
386 * Overrun is special, since it's reported
387 * immediately, and doesn't affect the current
388 * character.
389 */
390 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
391 tty->flip.flag_buf_ptr++;
392 tty->flip.char_buf_ptr++;
393 tty->flip.count++;
394 }
395 ignore_char:
396 *status = serial_inp(up, UART_LSR);
397 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
398
399 if (saw_console_brk)
400 sun_do_break();
401
402 return tty;
403}
404
405static _INLINE_ void transmit_chars(struct uart_sunsu_port *up)
406{
407 struct circ_buf *xmit = &up->port.info->xmit;
408 int count;
409
410 if (up->port.x_char) {
411 serial_outp(up, UART_TX, up->port.x_char);
412 up->port.icount.tx++;
413 up->port.x_char = 0;
414 return;
415 }
416 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
417 sunsu_stop_tx(&up->port, 0);
418 return;
419 }
420
421 count = up->port.fifosize;
422 do {
423 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
424 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
425 up->port.icount.tx++;
426 if (uart_circ_empty(xmit))
427 break;
428 } while (--count > 0);
429
430 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
431 uart_write_wakeup(&up->port);
432
433 if (uart_circ_empty(xmit))
434 sunsu_stop_tx(&up->port, 0);
435}
436
437static _INLINE_ void check_modem_status(struct uart_sunsu_port *up)
438{
439 int status;
440
441 status = serial_in(up, UART_MSR);
442
443 if ((status & UART_MSR_ANY_DELTA) == 0)
444 return;
445
446 if (status & UART_MSR_TERI)
447 up->port.icount.rng++;
448 if (status & UART_MSR_DDSR)
449 up->port.icount.dsr++;
450 if (status & UART_MSR_DDCD)
451 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
452 if (status & UART_MSR_DCTS)
453 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
454
455 wake_up_interruptible(&up->port.info->delta_msr_wait);
456}
457
458static irqreturn_t sunsu_serial_interrupt(int irq, void *dev_id, struct pt_regs *regs)
459{
460 struct uart_sunsu_port *up = dev_id;
461 unsigned long flags;
462 unsigned char status;
463
464 spin_lock_irqsave(&up->port.lock, flags);
465
466 do {
467 struct tty_struct *tty;
468
469 status = serial_inp(up, UART_LSR);
470 tty = NULL;
471 if (status & UART_LSR_DR)
472 tty = receive_chars(up, &status, regs);
473 check_modem_status(up);
474 if (status & UART_LSR_THRE)
475 transmit_chars(up);
476
477 spin_unlock_irqrestore(&up->port.lock, flags);
478
479 if (tty)
480 tty_flip_buffer_push(tty);
481
482 spin_lock_irqsave(&up->port.lock, flags);
483
484 } while (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT));
485
486 spin_unlock_irqrestore(&up->port.lock, flags);
487
488 return IRQ_HANDLED;
489}
490
491/* Separate interrupt handling path for keyboard/mouse ports. */
492
493static void
494sunsu_change_speed(struct uart_port *port, unsigned int cflag,
495 unsigned int iflag, unsigned int quot);
496
497static void sunsu_change_mouse_baud(struct uart_sunsu_port *up)
498{
499 unsigned int cur_cflag = up->cflag;
500 int quot, new_baud;
501
502 up->cflag &= ~CBAUD;
503 up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
504
505 quot = up->port.uartclk / (16 * new_baud);
506
507 spin_unlock(&up->port.lock);
508
509 sunsu_change_speed(&up->port, up->cflag, 0, quot);
510
511 spin_lock(&up->port.lock);
512}
513
514static void receive_kbd_ms_chars(struct uart_sunsu_port *up, struct pt_regs *regs, int is_break)
515{
516 do {
517 unsigned char ch = serial_inp(up, UART_RX);
518
519 /* Stop-A is handled by drivers/char/keyboard.c now. */
520 if (up->su_type == SU_PORT_KBD) {
521#ifdef CONFIG_SERIO
522 serio_interrupt(up->serio, ch, 0, regs);
523#endif
524 } else if (up->su_type == SU_PORT_MS) {
525 int ret = suncore_mouse_baud_detection(ch, is_break);
526
527 switch (ret) {
528 case 2:
529 sunsu_change_mouse_baud(up);
530 /* fallthru */
531 case 1:
532 break;
533
534 case 0:
535#ifdef CONFIG_SERIO
536 serio_interrupt(up->serio, ch, 0, regs);
537#endif
538 break;
539 };
540 }
541 } while (serial_in(up, UART_LSR) & UART_LSR_DR);
542}
543
544static irqreturn_t sunsu_kbd_ms_interrupt(int irq, void *dev_id, struct pt_regs *regs)
545{
546 struct uart_sunsu_port *up = dev_id;
547
548 if (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT)) {
549 unsigned char status = serial_inp(up, UART_LSR);
550
551 if ((status & UART_LSR_DR) || (status & UART_LSR_BI))
552 receive_kbd_ms_chars(up, regs,
553 (status & UART_LSR_BI) != 0);
554 }
555
556 return IRQ_HANDLED;
557}
558
559static unsigned int sunsu_tx_empty(struct uart_port *port)
560{
561 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
562 unsigned long flags;
563 unsigned int ret;
564
565 spin_lock_irqsave(&up->port.lock, flags);
566 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
567 spin_unlock_irqrestore(&up->port.lock, flags);
568
569 return ret;
570}
571
572static unsigned int sunsu_get_mctrl(struct uart_port *port)
573{
574 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
575 unsigned long flags;
576 unsigned char status;
577 unsigned int ret;
578
579 spin_lock_irqsave(&up->port.lock, flags);
580 status = serial_in(up, UART_MSR);
581 spin_unlock_irqrestore(&up->port.lock, flags);
582
583 ret = 0;
584 if (status & UART_MSR_DCD)
585 ret |= TIOCM_CAR;
586 if (status & UART_MSR_RI)
587 ret |= TIOCM_RNG;
588 if (status & UART_MSR_DSR)
589 ret |= TIOCM_DSR;
590 if (status & UART_MSR_CTS)
591 ret |= TIOCM_CTS;
592 return ret;
593}
594
595static void sunsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
596{
597 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
598 unsigned char mcr = 0;
599
600 if (mctrl & TIOCM_RTS)
601 mcr |= UART_MCR_RTS;
602 if (mctrl & TIOCM_DTR)
603 mcr |= UART_MCR_DTR;
604 if (mctrl & TIOCM_OUT1)
605 mcr |= UART_MCR_OUT1;
606 if (mctrl & TIOCM_OUT2)
607 mcr |= UART_MCR_OUT2;
608 if (mctrl & TIOCM_LOOP)
609 mcr |= UART_MCR_LOOP;
610
611 serial_out(up, UART_MCR, mcr);
612}
613
614static void sunsu_break_ctl(struct uart_port *port, int break_state)
615{
616 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
617 unsigned long flags;
618
619 spin_lock_irqsave(&up->port.lock, flags);
620 if (break_state == -1)
621 up->lcr |= UART_LCR_SBC;
622 else
623 up->lcr &= ~UART_LCR_SBC;
624 serial_out(up, UART_LCR, up->lcr);
625 spin_unlock_irqrestore(&up->port.lock, flags);
626}
627
628static int sunsu_startup(struct uart_port *port)
629{
630 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
631 unsigned long flags;
632 int retval;
633
634 if (up->port.type == PORT_16C950) {
635 /* Wake up and initialize UART */
636 up->acr = 0;
637 serial_outp(up, UART_LCR, 0xBF);
638 serial_outp(up, UART_EFR, UART_EFR_ECB);
639 serial_outp(up, UART_IER, 0);
640 serial_outp(up, UART_LCR, 0);
641 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
642 serial_outp(up, UART_LCR, 0xBF);
643 serial_outp(up, UART_EFR, UART_EFR_ECB);
644 serial_outp(up, UART_LCR, 0);
645 }
646
647#ifdef CONFIG_SERIAL_8250_RSA
648 /*
649 * If this is an RSA port, see if we can kick it up to the
650 * higher speed clock.
651 */
652 enable_rsa(up);
653#endif
654
655 /*
656 * Clear the FIFO buffers and disable them.
657 * (they will be reeanbled in set_termios())
658 */
659 if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
660 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
661 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
662 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
663 serial_outp(up, UART_FCR, 0);
664 }
665
666 /*
667 * Clear the interrupt registers.
668 */
669 (void) serial_inp(up, UART_LSR);
670 (void) serial_inp(up, UART_RX);
671 (void) serial_inp(up, UART_IIR);
672 (void) serial_inp(up, UART_MSR);
673
674 /*
675 * At this point, there's no way the LSR could still be 0xff;
676 * if it is, then bail out, because there's likely no UART
677 * here.
678 */
679 if (!(up->port.flags & ASYNC_BUGGY_UART) &&
680 (serial_inp(up, UART_LSR) == 0xff)) {
681 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
682 return -ENODEV;
683 }
684
685 if (up->su_type != SU_PORT_PORT) {
686 retval = request_irq(up->port.irq, sunsu_kbd_ms_interrupt,
687 SA_SHIRQ, su_typev[up->su_type], up);
688 } else {
689 retval = request_irq(up->port.irq, sunsu_serial_interrupt,
690 SA_SHIRQ, su_typev[up->su_type], up);
691 }
692 if (retval) {
693 printk("su: Cannot register IRQ %d\n", up->port.irq);
694 return retval;
695 }
696
697 /*
698 * Now, initialize the UART
699 */
700 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
701
702 spin_lock_irqsave(&up->port.lock, flags);
703
704 up->port.mctrl |= TIOCM_OUT2;
705
706 sunsu_set_mctrl(&up->port, up->port.mctrl);
707 spin_unlock_irqrestore(&up->port.lock, flags);
708
709 /*
710 * Finally, enable interrupts. Note: Modem status interrupts
711 * are set via set_termios(), which will be occurring imminently
712 * anyway, so we don't enable them here.
713 */
714 up->ier = UART_IER_RLSI | UART_IER_RDI;
715 serial_outp(up, UART_IER, up->ier);
716
717 if (up->port.flags & ASYNC_FOURPORT) {
718 unsigned int icp;
719 /*
720 * Enable interrupts on the AST Fourport board
721 */
722 icp = (up->port.iobase & 0xfe0) | 0x01f;
723 outb_p(0x80, icp);
724 (void) inb_p(icp);
725 }
726
727 /*
728 * And clear the interrupt registers again for luck.
729 */
730 (void) serial_inp(up, UART_LSR);
731 (void) serial_inp(up, UART_RX);
732 (void) serial_inp(up, UART_IIR);
733 (void) serial_inp(up, UART_MSR);
734
735 return 0;
736}
737
738static void sunsu_shutdown(struct uart_port *port)
739{
740 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
741 unsigned long flags;
742
743 /*
744 * Disable interrupts from this port
745 */
746 up->ier = 0;
747 serial_outp(up, UART_IER, 0);
748
749 spin_lock_irqsave(&up->port.lock, flags);
750 if (up->port.flags & ASYNC_FOURPORT) {
751 /* reset interrupts on the AST Fourport board */
752 inb((up->port.iobase & 0xfe0) | 0x1f);
753 up->port.mctrl |= TIOCM_OUT1;
754 } else
755 up->port.mctrl &= ~TIOCM_OUT2;
756
757 sunsu_set_mctrl(&up->port, up->port.mctrl);
758 spin_unlock_irqrestore(&up->port.lock, flags);
759
760 /*
761 * Disable break condition and FIFOs
762 */
763 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
764 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
765 UART_FCR_CLEAR_RCVR |
766 UART_FCR_CLEAR_XMIT);
767 serial_outp(up, UART_FCR, 0);
768
769#ifdef CONFIG_SERIAL_8250_RSA
770 /*
771 * Reset the RSA board back to 115kbps compat mode.
772 */
773 disable_rsa(up);
774#endif
775
776 /*
777 * Read data port to reset things.
778 */
779 (void) serial_in(up, UART_RX);
780
781 free_irq(up->port.irq, up);
782}
783
784static void
785sunsu_change_speed(struct uart_port *port, unsigned int cflag,
786 unsigned int iflag, unsigned int quot)
787{
788 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
789 unsigned char cval, fcr = 0;
790 unsigned long flags;
791
792 switch (cflag & CSIZE) {
793 case CS5:
794 cval = 0x00;
795 break;
796 case CS6:
797 cval = 0x01;
798 break;
799 case CS7:
800 cval = 0x02;
801 break;
802 default:
803 case CS8:
804 cval = 0x03;
805 break;
806 }
807
808 if (cflag & CSTOPB)
809 cval |= 0x04;
810 if (cflag & PARENB)
811 cval |= UART_LCR_PARITY;
812 if (!(cflag & PARODD))
813 cval |= UART_LCR_EPAR;
814#ifdef CMSPAR
815 if (cflag & CMSPAR)
816 cval |= UART_LCR_SPAR;
817#endif
818
819 /*
820 * Work around a bug in the Oxford Semiconductor 952 rev B
821 * chip which causes it to seriously miscalculate baud rates
822 * when DLL is 0.
823 */
824 if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
825 up->rev == 0x5201)
826 quot ++;
827
828 if (uart_config[up->port.type].flags & UART_USE_FIFO) {
829 if ((up->port.uartclk / quot) < (2400 * 16))
830 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
831#ifdef CONFIG_SERIAL_8250_RSA
832 else if (up->port.type == PORT_RSA)
833 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_14;
834#endif
835 else
836 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
837 }
838 if (up->port.type == PORT_16750)
839 fcr |= UART_FCR7_64BYTE;
840
841 /*
842 * Ok, we're now changing the port state. Do it with
843 * interrupts disabled.
844 */
845 spin_lock_irqsave(&up->port.lock, flags);
846
847 /*
848 * Update the per-port timeout.
849 */
850 uart_update_timeout(port, cflag, (port->uartclk / (16 * quot)));
851
852 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
853 if (iflag & INPCK)
854 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
855 if (iflag & (BRKINT | PARMRK))
856 up->port.read_status_mask |= UART_LSR_BI;
857
858 /*
859 * Characteres to ignore
860 */
861 up->port.ignore_status_mask = 0;
862 if (iflag & IGNPAR)
863 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
864 if (iflag & IGNBRK) {
865 up->port.ignore_status_mask |= UART_LSR_BI;
866 /*
867 * If we're ignoring parity and break indicators,
868 * ignore overruns too (for real raw support).
869 */
870 if (iflag & IGNPAR)
871 up->port.ignore_status_mask |= UART_LSR_OE;
872 }
873
874 /*
875 * ignore all characters if CREAD is not set
876 */
877 if ((cflag & CREAD) == 0)
878 up->port.ignore_status_mask |= UART_LSR_DR;
879
880 /*
881 * CTS flow control flag and modem status interrupts
882 */
883 up->ier &= ~UART_IER_MSI;
884 if (UART_ENABLE_MS(&up->port, cflag))
885 up->ier |= UART_IER_MSI;
886
887 serial_out(up, UART_IER, up->ier);
888
889 if (uart_config[up->port.type].flags & UART_STARTECH) {
890 serial_outp(up, UART_LCR, 0xBF);
891 serial_outp(up, UART_EFR, cflag & CRTSCTS ? UART_EFR_CTS :0);
892 }
893 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
894 serial_outp(up, UART_DLL, quot & 0xff); /* LS of divisor */
895 serial_outp(up, UART_DLM, quot >> 8); /* MS of divisor */
896 if (up->port.type == PORT_16750)
897 serial_outp(up, UART_FCR, fcr); /* set fcr */
898 serial_outp(up, UART_LCR, cval); /* reset DLAB */
899 up->lcr = cval; /* Save LCR */
900 if (up->port.type != PORT_16750) {
901 if (fcr & UART_FCR_ENABLE_FIFO) {
902 /* emulated UARTs (Lucent Venus 167x) need two steps */
903 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
904 }
905 serial_outp(up, UART_FCR, fcr); /* set fcr */
906 }
907
908 up->cflag = cflag;
909
910 spin_unlock_irqrestore(&up->port.lock, flags);
911}
912
913static void
914sunsu_set_termios(struct uart_port *port, struct termios *termios,
915 struct termios *old)
916{
917 unsigned int baud, quot;
918
919 /*
920 * Ask the core to calculate the divisor for us.
921 */
922 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
923 quot = uart_get_divisor(port, baud);
924
925 sunsu_change_speed(port, termios->c_cflag, termios->c_iflag, quot);
926}
927
928static void sunsu_release_port(struct uart_port *port)
929{
930}
931
932static int sunsu_request_port(struct uart_port *port)
933{
934 return 0;
935}
936
937static void sunsu_config_port(struct uart_port *port, int flags)
938{
939 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
940
941 if (flags & UART_CONFIG_TYPE) {
942 /*
943 * We are supposed to call autoconfig here, but this requires
944 * splitting all the OBP probing crap from the UART probing.
945 * We'll do it when we kill sunsu.c altogether.
946 */
947 port->type = up->type_probed; /* XXX */
948 }
949}
950
951static int
952sunsu_verify_port(struct uart_port *port, struct serial_struct *ser)
953{
954 return -EINVAL;
955}
956
957static const char *
958sunsu_type(struct uart_port *port)
959{
960 int type = port->type;
961
962 if (type >= ARRAY_SIZE(uart_config))
963 type = 0;
964 return uart_config[type].name;
965}
966
967static struct uart_ops sunsu_pops = {
968 .tx_empty = sunsu_tx_empty,
969 .set_mctrl = sunsu_set_mctrl,
970 .get_mctrl = sunsu_get_mctrl,
971 .stop_tx = sunsu_stop_tx,
972 .start_tx = sunsu_start_tx,
973 .stop_rx = sunsu_stop_rx,
974 .enable_ms = sunsu_enable_ms,
975 .break_ctl = sunsu_break_ctl,
976 .startup = sunsu_startup,
977 .shutdown = sunsu_shutdown,
978 .set_termios = sunsu_set_termios,
979 .type = sunsu_type,
980 .release_port = sunsu_release_port,
981 .request_port = sunsu_request_port,
982 .config_port = sunsu_config_port,
983 .verify_port = sunsu_verify_port,
984};
985
986#define UART_NR 4
987
988static struct uart_sunsu_port sunsu_ports[UART_NR];
989
990#ifdef CONFIG_SERIO
991
992static DEFINE_SPINLOCK(sunsu_serio_lock);
993
994static int sunsu_serio_write(struct serio *serio, unsigned char ch)
995{
996 struct uart_sunsu_port *up = serio->port_data;
997 unsigned long flags;
998 int lsr;
999
1000 spin_lock_irqsave(&sunsu_serio_lock, flags);
1001
1002 do {
1003 lsr = serial_in(up, UART_LSR);
1004 } while (!(lsr & UART_LSR_THRE));
1005
1006 /* Send the character out. */
1007 serial_out(up, UART_TX, ch);
1008
1009 spin_unlock_irqrestore(&sunsu_serio_lock, flags);
1010
1011 return 0;
1012}
1013
1014static int sunsu_serio_open(struct serio *serio)
1015{
1016 struct uart_sunsu_port *up = serio->port_data;
1017 unsigned long flags;
1018 int ret;
1019
1020 spin_lock_irqsave(&sunsu_serio_lock, flags);
1021 if (!up->serio_open) {
1022 up->serio_open = 1;
1023 ret = 0;
1024 } else
1025 ret = -EBUSY;
1026 spin_unlock_irqrestore(&sunsu_serio_lock, flags);
1027
1028 return ret;
1029}
1030
1031static void sunsu_serio_close(struct serio *serio)
1032{
1033 struct uart_sunsu_port *up = serio->port_data;
1034 unsigned long flags;
1035
1036 spin_lock_irqsave(&sunsu_serio_lock, flags);
1037 up->serio_open = 0;
1038 spin_unlock_irqrestore(&sunsu_serio_lock, flags);
1039}
1040
1041#endif /* CONFIG_SERIO */
1042
1043static void sunsu_autoconfig(struct uart_sunsu_port *up)
1044{
1045 unsigned char status1, status2, scratch, scratch2, scratch3;
1046 unsigned char save_lcr, save_mcr;
1047 struct linux_ebus_device *dev = NULL;
1048 struct linux_ebus *ebus;
1049#ifdef CONFIG_SPARC64
1050 struct sparc_isa_bridge *isa_br;
1051 struct sparc_isa_device *isa_dev;
1052#endif
1053#ifndef CONFIG_SPARC64
1054 struct linux_prom_registers reg0;
1055#endif
1056 unsigned long flags;
1057
1058 if (!up->port_node || !up->su_type)
1059 return;
1060
1061 up->type_probed = PORT_UNKNOWN;
1062 up->port.iotype = SERIAL_IO_MEM;
1063
1064 /*
1065 * First we look for Ebus-bases su's
1066 */
1067 for_each_ebus(ebus) {
1068 for_each_ebusdev(dev, ebus) {
1069 if (dev->prom_node == up->port_node) {
1070 /*
1071 * The EBus is broken on sparc; it delivers
1072 * virtual addresses in resources. Oh well...
1073 * This is correct on sparc64, though.
1074 */
1075 up->port.membase = (char *) dev->resource[0].start;
1076 /*
1077 * This is correct on both architectures.
1078 */
1079 up->port.mapbase = dev->resource[0].start;
1080 up->port.irq = dev->irqs[0];
1081 goto ebus_done;
1082 }
1083 }
1084 }
1085
1086#ifdef CONFIG_SPARC64
1087 for_each_isa(isa_br) {
1088 for_each_isadev(isa_dev, isa_br) {
1089 if (isa_dev->prom_node == up->port_node) {
1090 /* Same on sparc64. Cool architecure... */
1091 up->port.membase = (char *) isa_dev->resource.start;
1092 up->port.mapbase = isa_dev->resource.start;
1093 up->port.irq = isa_dev->irq;
1094 goto ebus_done;
1095 }
1096 }
1097 }
1098#endif
1099
1100#ifdef CONFIG_SPARC64
1101 /*
1102 * Not on Ebus, bailing.
1103 */
1104 return;
1105#else
1106 /*
1107 * Not on Ebus, must be OBIO.
1108 */
1109 if (prom_getproperty(up->port_node, "reg",
1110 (char *)&reg0, sizeof(reg0)) == -1) {
1111 prom_printf("sunsu: no \"reg\" property\n");
1112 return;
1113 }
1114 prom_apply_obio_ranges(&reg0, 1);
1115 if (reg0.which_io != 0) { /* Just in case... */
1116 prom_printf("sunsu: bus number nonzero: 0x%x:%x\n",
1117 reg0.which_io, reg0.phys_addr);
1118 return;
1119 }
1120 up->port.mapbase = reg0.phys_addr;
1121 if ((up->port.membase = ioremap(reg0.phys_addr, reg0.reg_size)) == 0) {
1122 prom_printf("sunsu: Cannot map registers.\n");
1123 return;
1124 }
1125
1126 /*
1127 * 0x20 is sun4m thing, Dave Redman heritage.
1128 * See arch/sparc/kernel/irq.c.
1129 */
1130#define IRQ_4M(n) ((n)|0x20)
1131
1132 /*
1133 * There is no intr property on MrCoffee, so hardwire it.
1134 */
1135 up->port.irq = IRQ_4M(13);
1136#endif
1137
1138ebus_done:
1139
1140 spin_lock_irqsave(&up->port.lock, flags);
1141
1142 if (!(up->port.flags & ASYNC_BUGGY_UART)) {
1143 /*
1144 * Do a simple existence test first; if we fail this, there's
1145 * no point trying anything else.
1146 *
1147 * 0x80 is used as a nonsense port to prevent against false
1148 * positives due to ISA bus float. The assumption is that
1149 * 0x80 is a non-existent port; which should be safe since
1150 * include/asm/io.h also makes this assumption.
1151 */
1152 scratch = serial_inp(up, UART_IER);
1153 serial_outp(up, UART_IER, 0);
1154#ifdef __i386__
1155 outb(0xff, 0x080);
1156#endif
1157 scratch2 = serial_inp(up, UART_IER);
1158 serial_outp(up, UART_IER, 0x0f);
1159#ifdef __i386__
1160 outb(0, 0x080);
1161#endif
1162 scratch3 = serial_inp(up, UART_IER);
1163 serial_outp(up, UART_IER, scratch);
1164 if (scratch2 != 0 || scratch3 != 0x0F)
1165 goto out; /* We failed; there's nothing here */
1166 }
1167
1168 save_mcr = serial_in(up, UART_MCR);
1169 save_lcr = serial_in(up, UART_LCR);
1170
1171 /*
1172 * Check to see if a UART is really there. Certain broken
1173 * internal modems based on the Rockwell chipset fail this
1174 * test, because they apparently don't implement the loopback
1175 * test mode. So this test is skipped on the COM 1 through
1176 * COM 4 ports. This *should* be safe, since no board
1177 * manufacturer would be stupid enough to design a board
1178 * that conflicts with COM 1-4 --- we hope!
1179 */
1180 if (!(up->port.flags & ASYNC_SKIP_TEST)) {
1181 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1182 status1 = serial_inp(up, UART_MSR) & 0xF0;
1183 serial_outp(up, UART_MCR, save_mcr);
1184 if (status1 != 0x90)
1185 goto out; /* We failed loopback test */
1186 }
1187 serial_outp(up, UART_LCR, 0xBF); /* set up for StarTech test */
1188 serial_outp(up, UART_EFR, 0); /* EFR is the same as FCR */
1189 serial_outp(up, UART_LCR, 0);
1190 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1191 scratch = serial_in(up, UART_IIR) >> 6;
1192 switch (scratch) {
1193 case 0:
1194 up->port.type = PORT_16450;
1195 break;
1196 case 1:
1197 up->port.type = PORT_UNKNOWN;
1198 break;
1199 case 2:
1200 up->port.type = PORT_16550;
1201 break;
1202 case 3:
1203 up->port.type = PORT_16550A;
1204 break;
1205 }
1206 if (up->port.type == PORT_16550A) {
1207 /* Check for Startech UART's */
1208 serial_outp(up, UART_LCR, UART_LCR_DLAB);
1209 if (serial_in(up, UART_EFR) == 0) {
1210 up->port.type = PORT_16650;
1211 } else {
1212 serial_outp(up, UART_LCR, 0xBF);
1213 if (serial_in(up, UART_EFR) == 0)
1214 up->port.type = PORT_16650V2;
1215 }
1216 }
1217 if (up->port.type == PORT_16550A) {
1218 /* Check for TI 16750 */
1219 serial_outp(up, UART_LCR, save_lcr | UART_LCR_DLAB);
1220 serial_outp(up, UART_FCR,
1221 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1222 scratch = serial_in(up, UART_IIR) >> 5;
1223 if (scratch == 7) {
1224 /*
1225 * If this is a 16750, and not a cheap UART
1226 * clone, then it should only go into 64 byte
1227 * mode if the UART_FCR7_64BYTE bit was set
1228 * while UART_LCR_DLAB was latched.
1229 */
1230 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1231 serial_outp(up, UART_LCR, 0);
1232 serial_outp(up, UART_FCR,
1233 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1234 scratch = serial_in(up, UART_IIR) >> 5;
1235 if (scratch == 6)
1236 up->port.type = PORT_16750;
1237 }
1238 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1239 }
1240 serial_outp(up, UART_LCR, save_lcr);
1241 if (up->port.type == PORT_16450) {
1242 scratch = serial_in(up, UART_SCR);
1243 serial_outp(up, UART_SCR, 0xa5);
1244 status1 = serial_in(up, UART_SCR);
1245 serial_outp(up, UART_SCR, 0x5a);
1246 status2 = serial_in(up, UART_SCR);
1247 serial_outp(up, UART_SCR, scratch);
1248
1249 if ((status1 != 0xa5) || (status2 != 0x5a))
1250 up->port.type = PORT_8250;
1251 }
1252
1253 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
1254
1255 if (up->port.type == PORT_UNKNOWN)
1256 goto out;
1257 up->type_probed = up->port.type; /* XXX */
1258
1259 /*
1260 * Reset the UART.
1261 */
1262#ifdef CONFIG_SERIAL_8250_RSA
1263 if (up->port.type == PORT_RSA)
1264 serial_outp(up, UART_RSA_FRR, 0);
1265#endif
1266 serial_outp(up, UART_MCR, save_mcr);
1267 serial_outp(up, UART_FCR, (UART_FCR_ENABLE_FIFO |
1268 UART_FCR_CLEAR_RCVR |
1269 UART_FCR_CLEAR_XMIT));
1270 serial_outp(up, UART_FCR, 0);
1271 (void)serial_in(up, UART_RX);
1272 serial_outp(up, UART_IER, 0);
1273
1274out:
1275 spin_unlock_irqrestore(&up->port.lock, flags);
1276}
1277
1278static struct uart_driver sunsu_reg = {
1279 .owner = THIS_MODULE,
1280 .driver_name = "serial",
1281 .devfs_name = "tts/",
1282 .dev_name = "ttyS",
1283 .major = TTY_MAJOR,
1284};
1285
1286static int __init sunsu_kbd_ms_init(struct uart_sunsu_port *up, int channel)
1287{
1288#ifdef CONFIG_SERIO
1289 struct serio *serio;
1290#endif
1291
1292 up->port.line = channel;
1293 up->port.type = PORT_UNKNOWN;
1294 up->port.uartclk = (SU_BASE_BAUD * 16);
1295
1296 if (up->su_type == SU_PORT_KBD)
1297 up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1298 else
1299 up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1300
1301 sunsu_autoconfig(up);
1302 if (up->port.type == PORT_UNKNOWN)
1303 return -1;
1304
1305 printk(KERN_INFO "su%d at 0x%p (irq = %s) is a %s\n",
1306 channel,
1307 up->port.membase, __irq_itoa(up->port.irq),
1308 sunsu_type(&up->port));
1309
1310#ifdef CONFIG_SERIO
1311 up->serio = serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
1312 if (serio) {
1313 memset(serio, 0, sizeof(*serio));
1314
1315 serio->port_data = up;
1316
1317 serio->id.type = SERIO_RS232;
1318 if (up->su_type == SU_PORT_KBD) {
1319 serio->id.proto = SERIO_SUNKBD;
1320 strlcpy(serio->name, "sukbd", sizeof(serio->name));
1321 } else {
1322 serio->id.proto = SERIO_SUN;
1323 serio->id.extra = 1;
1324 strlcpy(serio->name, "sums", sizeof(serio->name));
1325 }
1326 strlcpy(serio->phys, (channel == 0 ? "su/serio0" : "su/serio1"),
1327 sizeof(serio->phys));
1328
1329 serio->write = sunsu_serio_write;
1330 serio->open = sunsu_serio_open;
1331 serio->close = sunsu_serio_close;
1332
1333 serio_register_port(serio);
1334 } else {
1335 printk(KERN_WARNING "su%d: not enough memory for serio port\n",
1336 channel);
1337 }
1338#endif
1339
1340 sunsu_startup(&up->port);
1341 return 0;
1342}
1343
1344/*
1345 * ------------------------------------------------------------
1346 * Serial console driver
1347 * ------------------------------------------------------------
1348 */
1349
1350#ifdef CONFIG_SERIAL_SUNSU_CONSOLE
1351
1352#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1353
1354/*
1355 * Wait for transmitter & holding register to empty
1356 */
1357static __inline__ void wait_for_xmitr(struct uart_sunsu_port *up)
1358{
1359 unsigned int status, tmout = 10000;
1360
1361 /* Wait up to 10ms for the character(s) to be sent. */
1362 do {
1363 status = serial_in(up, UART_LSR);
1364
1365 if (status & UART_LSR_BI)
1366 up->lsr_break_flag = UART_LSR_BI;
1367
1368 if (--tmout == 0)
1369 break;
1370 udelay(1);
1371 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1372
1373 /* Wait up to 1s for flow control if necessary */
1374 if (up->port.flags & ASYNC_CONS_FLOW) {
1375 tmout = 1000000;
1376 while (--tmout &&
1377 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1378 udelay(1);
1379 }
1380}
1381
1382/*
1383 * Print a string to the serial port trying not to disturb
1384 * any possible real use of the port...
1385 */
1386static void sunsu_console_write(struct console *co, const char *s,
1387 unsigned int count)
1388{
1389 struct uart_sunsu_port *up = &sunsu_ports[co->index];
1390 unsigned int ier;
1391 int i;
1392
1393 /*
1394 * First save the UER then disable the interrupts
1395 */
1396 ier = serial_in(up, UART_IER);
1397 serial_out(up, UART_IER, 0);
1398
1399 /*
1400 * Now, do each character
1401 */
1402 for (i = 0; i < count; i++, s++) {
1403 wait_for_xmitr(up);
1404
1405 /*
1406 * Send the character out.
1407 * If a LF, also do CR...
1408 */
1409 serial_out(up, UART_TX, *s);
1410 if (*s == 10) {
1411 wait_for_xmitr(up);
1412 serial_out(up, UART_TX, 13);
1413 }
1414 }
1415
1416 /*
1417 * Finally, wait for transmitter to become empty
1418 * and restore the IER
1419 */
1420 wait_for_xmitr(up);
1421 serial_out(up, UART_IER, ier);
1422}
1423
1424/*
1425 * Setup initial baud/bits/parity. We do two things here:
1426 * - construct a cflag setting for the first su_open()
1427 * - initialize the serial port
1428 * Return non-zero if we didn't find a serial port.
1429 */
1430static int __init sunsu_console_setup(struct console *co, char *options)
1431{
1432 struct uart_port *port;
1433 int baud = 9600;
1434 int bits = 8;
1435 int parity = 'n';
1436 int flow = 'n';
1437
1438 printk("Console: ttyS%d (SU)\n",
1439 (sunsu_reg.minor - 64) + co->index);
1440
1441 /*
1442 * Check whether an invalid uart number has been specified, and
1443 * if so, search for the first available port that does have
1444 * console support.
1445 */
1446 if (co->index >= UART_NR)
1447 co->index = 0;
1448 port = &sunsu_ports[co->index].port;
1449
1450 /*
1451 * Temporary fix.
1452 */
1453 spin_lock_init(&port->lock);
1454
1455 if (options)
1456 uart_parse_options(options, &baud, &parity, &bits, &flow);
1457
1458 return uart_set_options(port, co, baud, parity, bits, flow);
1459}
1460
1461static struct console sunsu_cons = {
1462 .name = "ttyS",
1463 .write = sunsu_console_write,
1464 .device = uart_console_device,
1465 .setup = sunsu_console_setup,
1466 .flags = CON_PRINTBUFFER,
1467 .index = -1,
1468 .data = &sunsu_reg,
1469};
1470#define SUNSU_CONSOLE (&sunsu_cons)
1471
1472/*
1473 * Register console.
1474 */
1475
1476static int __init sunsu_serial_console_init(void)
1477{
1478 int i;
1479
1480 if (con_is_present())
1481 return 0;
1482
1483 for (i = 0; i < UART_NR; i++) {
1484 int this_minor = sunsu_reg.minor + i;
1485
1486 if ((this_minor - 64) == (serial_console - 1))
1487 break;
1488 }
1489 if (i == UART_NR)
1490 return 0;
1491 if (sunsu_ports[i].port_node == 0)
1492 return 0;
1493
1494 sunsu_cons.index = i;
1495 register_console(&sunsu_cons);
1496 return 0;
1497}
1498#else
1499#define SUNSU_CONSOLE (NULL)
1500#define sunsu_serial_console_init() do { } while (0)
1501#endif
1502
1503static int __init sunsu_serial_init(void)
1504{
1505 int instance, ret, i;
1506
1507 /* How many instances do we need? */
1508 instance = 0;
1509 for (i = 0; i < UART_NR; i++) {
1510 struct uart_sunsu_port *up = &sunsu_ports[i];
1511
1512 if (up->su_type == SU_PORT_MS ||
1513 up->su_type == SU_PORT_KBD)
1514 continue;
1515
1516 up->port.flags |= ASYNC_BOOT_AUTOCONF;
1517 up->port.type = PORT_UNKNOWN;
1518 up->port.uartclk = (SU_BASE_BAUD * 16);
1519
1520 sunsu_autoconfig(up);
1521 if (up->port.type == PORT_UNKNOWN)
1522 continue;
1523
1524 up->port.line = instance++;
1525 up->port.ops = &sunsu_pops;
1526 }
1527
1528 sunsu_reg.minor = sunserial_current_minor;
1529 sunserial_current_minor += instance;
1530
1531 sunsu_reg.nr = instance;
1532 sunsu_reg.cons = SUNSU_CONSOLE;
1533
1534 ret = uart_register_driver(&sunsu_reg);
1535 if (ret < 0)
1536 return ret;
1537
1538 sunsu_serial_console_init();
1539 for (i = 0; i < UART_NR; i++) {
1540 struct uart_sunsu_port *up = &sunsu_ports[i];
1541
1542 /* Do not register Keyboard/Mouse lines with UART
1543 * layer.
1544 */
1545 if (up->su_type == SU_PORT_MS ||
1546 up->su_type == SU_PORT_KBD)
1547 continue;
1548
1549 if (up->port.type == PORT_UNKNOWN)
1550 continue;
1551
1552 uart_add_one_port(&sunsu_reg, &up->port);
1553 }
1554
1555 return 0;
1556}
1557
1558static int su_node_ok(int node, char *name, int namelen)
1559{
1560 if (strncmp(name, "su", namelen) == 0 ||
1561 strncmp(name, "su_pnp", namelen) == 0)
1562 return 1;
1563
1564 if (strncmp(name, "serial", namelen) == 0) {
1565 char compat[32];
1566 int clen;
1567
1568 /* Is it _really_ a 'su' device? */
1569 clen = prom_getproperty(node, "compatible", compat, sizeof(compat));
1570 if (clen > 0) {
1571 if (strncmp(compat, "sab82532", 8) == 0) {
1572 /* Nope, Siemens serial, not for us. */
1573 return 0;
1574 }
1575 }
1576 return 1;
1577 }
1578
1579 return 0;
1580}
1581
1582#define SU_PROPSIZE 128
1583
1584/*
1585 * Scan status structure.
1586 * "prop" is a local variable but it eats stack to keep it in each
1587 * stack frame of a recursive procedure.
1588 */
1589struct su_probe_scan {
1590 int msnode, kbnode; /* PROM nodes for mouse and keyboard */
1591 int msx, kbx; /* minors for mouse and keyboard */
1592 int devices; /* scan index */
1593 char prop[SU_PROPSIZE];
1594};
1595
1596/*
1597 * We have several platforms which present 'su' in different parts
1598 * of the device tree. 'su' may be found under obio, ebus, isa and pci.
1599 * We walk over the tree and find them wherever PROM hides them.
1600 */
1601static void __init su_probe_any(struct su_probe_scan *t, int sunode)
1602{
1603 struct uart_sunsu_port *up;
1604 int len;
1605
1606 if (t->devices >= UART_NR)
1607 return;
1608
1609 for (; sunode != 0; sunode = prom_getsibling(sunode)) {
1610 len = prom_getproperty(sunode, "name", t->prop, SU_PROPSIZE);
1611 if (len <= 1)
1612 continue; /* Broken PROM node */
1613
1614 if (su_node_ok(sunode, t->prop, len)) {
1615 up = &sunsu_ports[t->devices];
1616 if (t->kbnode != 0 && sunode == t->kbnode) {
1617 t->kbx = t->devices;
1618 up->su_type = SU_PORT_KBD;
1619 } else if (t->msnode != 0 && sunode == t->msnode) {
1620 t->msx = t->devices;
1621 up->su_type = SU_PORT_MS;
1622 } else {
1623#ifdef CONFIG_SPARC64
1624 /*
1625 * Do not attempt to use the truncated
1626 * keyboard/mouse ports as serial ports
1627 * on Ultras with PC keyboard attached.
1628 */
1629 if (prom_getbool(sunode, "mouse"))
1630 continue;
1631 if (prom_getbool(sunode, "keyboard"))
1632 continue;
1633#endif
1634 up->su_type = SU_PORT_PORT;
1635 }
1636 up->port_node = sunode;
1637 ++t->devices;
1638 } else {
1639 su_probe_any(t, prom_getchild(sunode));
1640 }
1641 }
1642}
1643
1644static int __init sunsu_probe(void)
1645{
1646 int node;
1647 int len;
1648 struct su_probe_scan scan;
1649
1650 /*
1651 * First, we scan the tree.
1652 */
1653 scan.devices = 0;
1654 scan.msx = -1;
1655 scan.kbx = -1;
1656 scan.kbnode = 0;
1657 scan.msnode = 0;
1658
1659 /*
1660 * Get the nodes for keyboard and mouse from 'aliases'...
1661 */
1662 node = prom_getchild(prom_root_node);
1663 node = prom_searchsiblings(node, "aliases");
1664 if (node != 0) {
1665 len = prom_getproperty(node, "keyboard", scan.prop, SU_PROPSIZE);
1666 if (len > 0) {
1667 scan.prop[len] = 0;
1668 scan.kbnode = prom_finddevice(scan.prop);
1669 }
1670
1671 len = prom_getproperty(node, "mouse", scan.prop, SU_PROPSIZE);
1672 if (len > 0) {
1673 scan.prop[len] = 0;
1674 scan.msnode = prom_finddevice(scan.prop);
1675 }
1676 }
1677
1678 su_probe_any(&scan, prom_getchild(prom_root_node));
1679
1680 /*
1681 * Second, we process the special case of keyboard and mouse.
1682 *
1683 * Currently if we got keyboard and mouse hooked to "su" ports
1684 * we do not use any possible remaining "su" as a serial port.
1685 * Thus, we ignore values of .msx and .kbx, then compact ports.
1686 */
1687 if (scan.msx != -1 && scan.kbx != -1) {
1688 sunsu_ports[0].su_type = SU_PORT_MS;
1689 sunsu_ports[0].port_node = scan.msnode;
1690 sunsu_kbd_ms_init(&sunsu_ports[0], 0);
1691
1692 sunsu_ports[1].su_type = SU_PORT_KBD;
1693 sunsu_ports[1].port_node = scan.kbnode;
1694 sunsu_kbd_ms_init(&sunsu_ports[1], 1);
1695
1696 return 0;
1697 }
1698
1699 if (scan.msx != -1 || scan.kbx != -1) {
1700 printk("sunsu_probe: cannot match keyboard and mouse, confused\n");
1701 return -ENODEV;
1702 }
1703
1704 if (scan.devices == 0)
1705 return -ENODEV;
1706
1707 /*
1708 * Console must be initiated after the generic initialization.
1709 */
1710 sunsu_serial_init();
1711
1712 return 0;
1713}
1714
1715static void __exit sunsu_exit(void)
1716{
1717 int i, saw_uart;
1718
1719 saw_uart = 0;
1720 for (i = 0; i < UART_NR; i++) {
1721 struct uart_sunsu_port *up = &sunsu_ports[i];
1722
1723 if (up->su_type == SU_PORT_MS ||
1724 up->su_type == SU_PORT_KBD) {
1725#ifdef CONFIG_SERIO
1726 if (up->serio) {
1727 serio_unregister_port(up->serio);
1728 up->serio = NULL;
1729 }
1730#endif
1731 } else if (up->port.type != PORT_UNKNOWN) {
1732 uart_remove_one_port(&sunsu_reg, &up->port);
1733 saw_uart++;
1734 }
1735 }
1736
1737 if (saw_uart)
1738 uart_unregister_driver(&sunsu_reg);
1739}
1740
1741module_init(sunsu_probe);
1742module_exit(sunsu_exit);
diff --git a/drivers/serial/sunzilog.c b/drivers/serial/sunzilog.c
new file mode 100644
index 000000000000..5c4231ae295b
--- /dev/null
+++ b/drivers/serial/sunzilog.c
@@ -0,0 +1,1773 @@
1/*
2 * sunzilog.c
3 *
4 * Driver for Zilog serial chips found on Sun workstations and
5 * servers. This driver could actually be made more generic.
6 *
7 * This is based on the old drivers/sbus/char/zs.c code. A lot
8 * of code has been simply moved over directly from there but
9 * much has been rewritten. Credits therefore go out to Eddie
10 * C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell for their
11 * work there.
12 *
13 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
14 */
15
16#include <linux/config.h>
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/sched.h>
20#include <linux/errno.h>
21#include <linux/delay.h>
22#include <linux/tty.h>
23#include <linux/tty_flip.h>
24#include <linux/major.h>
25#include <linux/string.h>
26#include <linux/ptrace.h>
27#include <linux/ioport.h>
28#include <linux/slab.h>
29#include <linux/circ_buf.h>
30#include <linux/serial.h>
31#include <linux/sysrq.h>
32#include <linux/console.h>
33#include <linux/spinlock.h>
34#ifdef CONFIG_SERIO
35#include <linux/serio.h>
36#endif
37#include <linux/init.h>
38
39#include <asm/io.h>
40#include <asm/irq.h>
41#ifdef CONFIG_SPARC64
42#include <asm/fhc.h>
43#endif
44#include <asm/sbus.h>
45
46#if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
47#define SUPPORT_SYSRQ
48#endif
49
50#include <linux/serial_core.h>
51
52#include "suncore.h"
53#include "sunzilog.h"
54
55/* On 32-bit sparcs we need to delay after register accesses
56 * to accommodate sun4 systems, but we do not need to flush writes.
57 * On 64-bit sparc we only need to flush single writes to ensure
58 * completion.
59 */
60#ifndef CONFIG_SPARC64
61#define ZSDELAY() udelay(5)
62#define ZSDELAY_LONG() udelay(20)
63#define ZS_WSYNC(channel) do { } while (0)
64#else
65#define ZSDELAY()
66#define ZSDELAY_LONG()
67#define ZS_WSYNC(__channel) \
68 sbus_readb(&((__channel)->control))
69#endif
70
71static int num_sunzilog;
72#define NUM_SUNZILOG num_sunzilog
73#define NUM_CHANNELS (NUM_SUNZILOG * 2)
74
75#define KEYBOARD_LINE 0x2
76#define MOUSE_LINE 0x3
77
78#define ZS_CLOCK 4915200 /* Zilog input clock rate. */
79#define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
80
81/*
82 * We wrap our port structure around the generic uart_port.
83 */
84struct uart_sunzilog_port {
85 struct uart_port port;
86
87 /* IRQ servicing chain. */
88 struct uart_sunzilog_port *next;
89
90 /* Current values of Zilog write registers. */
91 unsigned char curregs[NUM_ZSREGS];
92
93 unsigned int flags;
94#define SUNZILOG_FLAG_CONS_KEYB 0x00000001
95#define SUNZILOG_FLAG_CONS_MOUSE 0x00000002
96#define SUNZILOG_FLAG_IS_CONS 0x00000004
97#define SUNZILOG_FLAG_IS_KGDB 0x00000008
98#define SUNZILOG_FLAG_MODEM_STATUS 0x00000010
99#define SUNZILOG_FLAG_IS_CHANNEL_A 0x00000020
100#define SUNZILOG_FLAG_REGS_HELD 0x00000040
101#define SUNZILOG_FLAG_TX_STOPPED 0x00000080
102#define SUNZILOG_FLAG_TX_ACTIVE 0x00000100
103
104 unsigned int cflag;
105
106 unsigned char parity_mask;
107 unsigned char prev_status;
108
109#ifdef CONFIG_SERIO
110 struct serio *serio;
111 int serio_open;
112#endif
113};
114
115#define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel __iomem *)((PORT)->membase))
116#define UART_ZILOG(PORT) ((struct uart_sunzilog_port *)(PORT))
117
118#define ZS_IS_KEYB(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_KEYB)
119#define ZS_IS_MOUSE(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_MOUSE)
120#define ZS_IS_CONS(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CONS)
121#define ZS_IS_KGDB(UP) ((UP)->flags & SUNZILOG_FLAG_IS_KGDB)
122#define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & SUNZILOG_FLAG_MODEM_STATUS)
123#define ZS_IS_CHANNEL_A(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CHANNEL_A)
124#define ZS_REGS_HELD(UP) ((UP)->flags & SUNZILOG_FLAG_REGS_HELD)
125#define ZS_TX_STOPPED(UP) ((UP)->flags & SUNZILOG_FLAG_TX_STOPPED)
126#define ZS_TX_ACTIVE(UP) ((UP)->flags & SUNZILOG_FLAG_TX_ACTIVE)
127
128/* Reading and writing Zilog8530 registers. The delays are to make this
129 * driver work on the Sun4 which needs a settling delay after each chip
130 * register access, other machines handle this in hardware via auxiliary
131 * flip-flops which implement the settle time we do in software.
132 *
133 * The port lock must be held and local IRQs must be disabled
134 * when {read,write}_zsreg is invoked.
135 */
136static unsigned char read_zsreg(struct zilog_channel __iomem *channel,
137 unsigned char reg)
138{
139 unsigned char retval;
140
141 sbus_writeb(reg, &channel->control);
142 ZSDELAY();
143 retval = sbus_readb(&channel->control);
144 ZSDELAY();
145
146 return retval;
147}
148
149static void write_zsreg(struct zilog_channel __iomem *channel,
150 unsigned char reg, unsigned char value)
151{
152 sbus_writeb(reg, &channel->control);
153 ZSDELAY();
154 sbus_writeb(value, &channel->control);
155 ZSDELAY();
156}
157
158static void sunzilog_clear_fifo(struct zilog_channel __iomem *channel)
159{
160 int i;
161
162 for (i = 0; i < 32; i++) {
163 unsigned char regval;
164
165 regval = sbus_readb(&channel->control);
166 ZSDELAY();
167 if (regval & Rx_CH_AV)
168 break;
169
170 regval = read_zsreg(channel, R1);
171 sbus_readb(&channel->data);
172 ZSDELAY();
173
174 if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
175 sbus_writeb(ERR_RES, &channel->control);
176 ZSDELAY();
177 ZS_WSYNC(channel);
178 }
179 }
180}
181
182/* This function must only be called when the TX is not busy. The UART
183 * port lock must be held and local interrupts disabled.
184 */
185static void __load_zsregs(struct zilog_channel __iomem *channel, unsigned char *regs)
186{
187 int i;
188
189 /* Let pending transmits finish. */
190 for (i = 0; i < 1000; i++) {
191 unsigned char stat = read_zsreg(channel, R1);
192 if (stat & ALL_SNT)
193 break;
194 udelay(100);
195 }
196
197 sbus_writeb(ERR_RES, &channel->control);
198 ZSDELAY();
199 ZS_WSYNC(channel);
200
201 sunzilog_clear_fifo(channel);
202
203 /* Disable all interrupts. */
204 write_zsreg(channel, R1,
205 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
206
207 /* Set parity, sync config, stop bits, and clock divisor. */
208 write_zsreg(channel, R4, regs[R4]);
209
210 /* Set misc. TX/RX control bits. */
211 write_zsreg(channel, R10, regs[R10]);
212
213 /* Set TX/RX controls sans the enable bits. */
214 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
215 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
216
217 /* Synchronous mode config. */
218 write_zsreg(channel, R6, regs[R6]);
219 write_zsreg(channel, R7, regs[R7]);
220
221 /* Don't mess with the interrupt vector (R2, unused by us) and
222 * master interrupt control (R9). We make sure this is setup
223 * properly at probe time then never touch it again.
224 */
225
226 /* Disable baud generator. */
227 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
228
229 /* Clock mode control. */
230 write_zsreg(channel, R11, regs[R11]);
231
232 /* Lower and upper byte of baud rate generator divisor. */
233 write_zsreg(channel, R12, regs[R12]);
234 write_zsreg(channel, R13, regs[R13]);
235
236 /* Now rewrite R14, with BRENAB (if set). */
237 write_zsreg(channel, R14, regs[R14]);
238
239 /* External status interrupt control. */
240 write_zsreg(channel, R15, regs[R15]);
241
242 /* Reset external status interrupts. */
243 write_zsreg(channel, R0, RES_EXT_INT);
244 write_zsreg(channel, R0, RES_EXT_INT);
245
246 /* Rewrite R3/R5, this time without enables masked. */
247 write_zsreg(channel, R3, regs[R3]);
248 write_zsreg(channel, R5, regs[R5]);
249
250 /* Rewrite R1, this time without IRQ enabled masked. */
251 write_zsreg(channel, R1, regs[R1]);
252}
253
254/* Reprogram the Zilog channel HW registers with the copies found in the
255 * software state struct. If the transmitter is busy, we defer this update
256 * until the next TX complete interrupt. Else, we do it right now.
257 *
258 * The UART port lock must be held and local interrupts disabled.
259 */
260static void sunzilog_maybe_update_regs(struct uart_sunzilog_port *up,
261 struct zilog_channel __iomem *channel)
262{
263 if (!ZS_REGS_HELD(up)) {
264 if (ZS_TX_ACTIVE(up)) {
265 up->flags |= SUNZILOG_FLAG_REGS_HELD;
266 } else {
267 __load_zsregs(channel, up->curregs);
268 }
269 }
270}
271
272static void sunzilog_change_mouse_baud(struct uart_sunzilog_port *up)
273{
274 unsigned int cur_cflag = up->cflag;
275 int brg, new_baud;
276
277 up->cflag &= ~CBAUD;
278 up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
279
280 brg = BPS_TO_BRG(new_baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
281 up->curregs[R12] = (brg & 0xff);
282 up->curregs[R13] = (brg >> 8) & 0xff;
283 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(&up->port));
284}
285
286static void sunzilog_kbdms_receive_chars(struct uart_sunzilog_port *up,
287 unsigned char ch, int is_break,
288 struct pt_regs *regs)
289{
290 if (ZS_IS_KEYB(up)) {
291 /* Stop-A is handled by drivers/char/keyboard.c now. */
292#ifdef CONFIG_SERIO
293 if (up->serio_open)
294 serio_interrupt(up->serio, ch, 0, regs);
295#endif
296 } else if (ZS_IS_MOUSE(up)) {
297 int ret = suncore_mouse_baud_detection(ch, is_break);
298
299 switch (ret) {
300 case 2:
301 sunzilog_change_mouse_baud(up);
302 /* fallthru */
303 case 1:
304 break;
305
306 case 0:
307#ifdef CONFIG_SERIO
308 if (up->serio_open)
309 serio_interrupt(up->serio, ch, 0, regs);
310#endif
311 break;
312 };
313 }
314}
315
316static struct tty_struct *
317sunzilog_receive_chars(struct uart_sunzilog_port *up,
318 struct zilog_channel __iomem *channel,
319 struct pt_regs *regs)
320{
321 struct tty_struct *tty;
322 unsigned char ch, r1;
323
324 tty = NULL;
325 if (up->port.info != NULL && /* Unopened serial console */
326 up->port.info->tty != NULL) /* Keyboard || mouse */
327 tty = up->port.info->tty;
328
329 for (;;) {
330
331 r1 = read_zsreg(channel, R1);
332 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
333 sbus_writeb(ERR_RES, &channel->control);
334 ZSDELAY();
335 ZS_WSYNC(channel);
336 }
337
338 ch = sbus_readb(&channel->control);
339 ZSDELAY();
340
341 /* This funny hack depends upon BRK_ABRT not interfering
342 * with the other bits we care about in R1.
343 */
344 if (ch & BRK_ABRT)
345 r1 |= BRK_ABRT;
346
347 if (!(ch & Rx_CH_AV))
348 break;
349
350 ch = sbus_readb(&channel->data);
351 ZSDELAY();
352
353 ch &= up->parity_mask;
354
355 if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
356 sunzilog_kbdms_receive_chars(up, ch, 0, regs);
357 continue;
358 }
359
360 if (tty == NULL) {
361 uart_handle_sysrq_char(&up->port, ch, regs);
362 continue;
363 }
364
365 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
366 tty->flip.work.func((void *)tty);
367 /*
368 * The 8250 bails out of the loop here,
369 * but we need to read everything, or die.
370 */
371 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
372 continue;
373 }
374
375 /* A real serial line, record the character and status. */
376 *tty->flip.char_buf_ptr = ch;
377 *tty->flip.flag_buf_ptr = TTY_NORMAL;
378 up->port.icount.rx++;
379 if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
380 if (r1 & BRK_ABRT) {
381 r1 &= ~(PAR_ERR | CRC_ERR);
382 up->port.icount.brk++;
383 if (uart_handle_break(&up->port))
384 continue;
385 }
386 else if (r1 & PAR_ERR)
387 up->port.icount.parity++;
388 else if (r1 & CRC_ERR)
389 up->port.icount.frame++;
390 if (r1 & Rx_OVR)
391 up->port.icount.overrun++;
392 r1 &= up->port.read_status_mask;
393 if (r1 & BRK_ABRT)
394 *tty->flip.flag_buf_ptr = TTY_BREAK;
395 else if (r1 & PAR_ERR)
396 *tty->flip.flag_buf_ptr = TTY_PARITY;
397 else if (r1 & CRC_ERR)
398 *tty->flip.flag_buf_ptr = TTY_FRAME;
399 }
400 if (uart_handle_sysrq_char(&up->port, ch, regs))
401 continue;
402
403 if (up->port.ignore_status_mask == 0xff ||
404 (r1 & up->port.ignore_status_mask) == 0) {
405 tty->flip.flag_buf_ptr++;
406 tty->flip.char_buf_ptr++;
407 tty->flip.count++;
408 }
409 if ((r1 & Rx_OVR) &&
410 tty->flip.count < TTY_FLIPBUF_SIZE) {
411 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
412 tty->flip.flag_buf_ptr++;
413 tty->flip.char_buf_ptr++;
414 tty->flip.count++;
415 }
416 }
417
418 return tty;
419}
420
421static void sunzilog_status_handle(struct uart_sunzilog_port *up,
422 struct zilog_channel __iomem *channel,
423 struct pt_regs *regs)
424{
425 unsigned char status;
426
427 status = sbus_readb(&channel->control);
428 ZSDELAY();
429
430 sbus_writeb(RES_EXT_INT, &channel->control);
431 ZSDELAY();
432 ZS_WSYNC(channel);
433
434 if (status & BRK_ABRT) {
435 if (ZS_IS_MOUSE(up))
436 sunzilog_kbdms_receive_chars(up, 0, 1, regs);
437 if (ZS_IS_CONS(up)) {
438 /* Wait for BREAK to deassert to avoid potentially
439 * confusing the PROM.
440 */
441 while (1) {
442 status = sbus_readb(&channel->control);
443 ZSDELAY();
444 if (!(status & BRK_ABRT))
445 break;
446 }
447 sun_do_break();
448 return;
449 }
450 }
451
452 if (ZS_WANTS_MODEM_STATUS(up)) {
453 if (status & SYNC)
454 up->port.icount.dsr++;
455
456 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
457 * But it does not tell us which bit has changed, we have to keep
458 * track of this ourselves.
459 */
460 if ((status ^ up->prev_status) ^ DCD)
461 uart_handle_dcd_change(&up->port,
462 (status & DCD));
463 if ((status ^ up->prev_status) ^ CTS)
464 uart_handle_cts_change(&up->port,
465 (status & CTS));
466
467 wake_up_interruptible(&up->port.info->delta_msr_wait);
468 }
469
470 up->prev_status = status;
471}
472
473static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
474 struct zilog_channel __iomem *channel)
475{
476 struct circ_buf *xmit;
477
478 if (ZS_IS_CONS(up)) {
479 unsigned char status = sbus_readb(&channel->control);
480 ZSDELAY();
481
482 /* TX still busy? Just wait for the next TX done interrupt.
483 *
484 * It can occur because of how we do serial console writes. It would
485 * be nice to transmit console writes just like we normally would for
486 * a TTY line. (ie. buffered and TX interrupt driven). That is not
487 * easy because console writes cannot sleep. One solution might be
488 * to poll on enough port->xmit space becomming free. -DaveM
489 */
490 if (!(status & Tx_BUF_EMP))
491 return;
492 }
493
494 up->flags &= ~SUNZILOG_FLAG_TX_ACTIVE;
495
496 if (ZS_REGS_HELD(up)) {
497 __load_zsregs(channel, up->curregs);
498 up->flags &= ~SUNZILOG_FLAG_REGS_HELD;
499 }
500
501 if (ZS_TX_STOPPED(up)) {
502 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
503 goto ack_tx_int;
504 }
505
506 if (up->port.x_char) {
507 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
508 sbus_writeb(up->port.x_char, &channel->data);
509 ZSDELAY();
510 ZS_WSYNC(channel);
511
512 up->port.icount.tx++;
513 up->port.x_char = 0;
514 return;
515 }
516
517 if (up->port.info == NULL)
518 goto ack_tx_int;
519 xmit = &up->port.info->xmit;
520 if (uart_circ_empty(xmit)) {
521 uart_write_wakeup(&up->port);
522 goto ack_tx_int;
523 }
524 if (uart_tx_stopped(&up->port))
525 goto ack_tx_int;
526
527 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
528 sbus_writeb(xmit->buf[xmit->tail], &channel->data);
529 ZSDELAY();
530 ZS_WSYNC(channel);
531
532 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
533 up->port.icount.tx++;
534
535 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
536 uart_write_wakeup(&up->port);
537
538 return;
539
540ack_tx_int:
541 sbus_writeb(RES_Tx_P, &channel->control);
542 ZSDELAY();
543 ZS_WSYNC(channel);
544}
545
546static irqreturn_t sunzilog_interrupt(int irq, void *dev_id, struct pt_regs *regs)
547{
548 struct uart_sunzilog_port *up = dev_id;
549
550 while (up) {
551 struct zilog_channel __iomem *channel
552 = ZILOG_CHANNEL_FROM_PORT(&up->port);
553 struct tty_struct *tty;
554 unsigned char r3;
555
556 spin_lock(&up->port.lock);
557 r3 = read_zsreg(channel, R3);
558
559 /* Channel A */
560 tty = NULL;
561 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
562 sbus_writeb(RES_H_IUS, &channel->control);
563 ZSDELAY();
564 ZS_WSYNC(channel);
565
566 if (r3 & CHARxIP)
567 tty = sunzilog_receive_chars(up, channel, regs);
568 if (r3 & CHAEXT)
569 sunzilog_status_handle(up, channel, regs);
570 if (r3 & CHATxIP)
571 sunzilog_transmit_chars(up, channel);
572 }
573 spin_unlock(&up->port.lock);
574
575 if (tty)
576 tty_flip_buffer_push(tty);
577
578 /* Channel B */
579 up = up->next;
580 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
581
582 spin_lock(&up->port.lock);
583 tty = NULL;
584 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
585 sbus_writeb(RES_H_IUS, &channel->control);
586 ZSDELAY();
587 ZS_WSYNC(channel);
588
589 if (r3 & CHBRxIP)
590 tty = sunzilog_receive_chars(up, channel, regs);
591 if (r3 & CHBEXT)
592 sunzilog_status_handle(up, channel, regs);
593 if (r3 & CHBTxIP)
594 sunzilog_transmit_chars(up, channel);
595 }
596 spin_unlock(&up->port.lock);
597
598 if (tty)
599 tty_flip_buffer_push(tty);
600
601 up = up->next;
602 }
603
604 return IRQ_HANDLED;
605}
606
607/* A convenient way to quickly get R0 status. The caller must _not_ hold the
608 * port lock, it is acquired here.
609 */
610static __inline__ unsigned char sunzilog_read_channel_status(struct uart_port *port)
611{
612 struct zilog_channel __iomem *channel;
613 unsigned long flags;
614 unsigned char status;
615
616 spin_lock_irqsave(&port->lock, flags);
617
618 channel = ZILOG_CHANNEL_FROM_PORT(port);
619 status = sbus_readb(&channel->control);
620 ZSDELAY();
621
622 spin_unlock_irqrestore(&port->lock, flags);
623
624 return status;
625}
626
627/* The port lock is not held. */
628static unsigned int sunzilog_tx_empty(struct uart_port *port)
629{
630 unsigned char status;
631 unsigned int ret;
632
633 status = sunzilog_read_channel_status(port);
634 if (status & Tx_BUF_EMP)
635 ret = TIOCSER_TEMT;
636 else
637 ret = 0;
638
639 return ret;
640}
641
642/* The port lock is not held. */
643static unsigned int sunzilog_get_mctrl(struct uart_port *port)
644{
645 unsigned char status;
646 unsigned int ret;
647
648 status = sunzilog_read_channel_status(port);
649
650 ret = 0;
651 if (status & DCD)
652 ret |= TIOCM_CAR;
653 if (status & SYNC)
654 ret |= TIOCM_DSR;
655 if (status & CTS)
656 ret |= TIOCM_CTS;
657
658 return ret;
659}
660
661/* The port lock is held and interrupts are disabled. */
662static void sunzilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
663{
664 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
665 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
666 unsigned char set_bits, clear_bits;
667
668 set_bits = clear_bits = 0;
669
670 if (mctrl & TIOCM_RTS)
671 set_bits |= RTS;
672 else
673 clear_bits |= RTS;
674 if (mctrl & TIOCM_DTR)
675 set_bits |= DTR;
676 else
677 clear_bits |= DTR;
678
679 /* NOTE: Not subject to 'transmitter active' rule. */
680 up->curregs[R5] |= set_bits;
681 up->curregs[R5] &= ~clear_bits;
682 write_zsreg(channel, R5, up->curregs[R5]);
683}
684
685/* The port lock is held and interrupts are disabled. */
686static void sunzilog_stop_tx(struct uart_port *port, unsigned int tty_stop)
687{
688 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
689
690 up->flags |= SUNZILOG_FLAG_TX_STOPPED;
691}
692
693/* The port lock is held and interrupts are disabled. */
694static void sunzilog_start_tx(struct uart_port *port, unsigned int tty_start)
695{
696 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
697 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
698 unsigned char status;
699
700 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
701 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
702
703 status = sbus_readb(&channel->control);
704 ZSDELAY();
705
706 /* TX busy? Just wait for the TX done interrupt. */
707 if (!(status & Tx_BUF_EMP))
708 return;
709
710 /* Send the first character to jump-start the TX done
711 * IRQ sending engine.
712 */
713 if (port->x_char) {
714 sbus_writeb(port->x_char, &channel->data);
715 ZSDELAY();
716 ZS_WSYNC(channel);
717
718 port->icount.tx++;
719 port->x_char = 0;
720 } else {
721 struct circ_buf *xmit = &port->info->xmit;
722
723 sbus_writeb(xmit->buf[xmit->tail], &channel->data);
724 ZSDELAY();
725 ZS_WSYNC(channel);
726
727 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
728 port->icount.tx++;
729
730 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
731 uart_write_wakeup(&up->port);
732 }
733}
734
735/* The port lock is held. */
736static void sunzilog_stop_rx(struct uart_port *port)
737{
738 struct uart_sunzilog_port *up = UART_ZILOG(port);
739 struct zilog_channel __iomem *channel;
740
741 if (ZS_IS_CONS(up))
742 return;
743
744 channel = ZILOG_CHANNEL_FROM_PORT(port);
745
746 /* Disable all RX interrupts. */
747 up->curregs[R1] &= ~RxINT_MASK;
748 sunzilog_maybe_update_regs(up, channel);
749}
750
751/* The port lock is held. */
752static void sunzilog_enable_ms(struct uart_port *port)
753{
754 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
755 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
756 unsigned char new_reg;
757
758 new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
759 if (new_reg != up->curregs[R15]) {
760 up->curregs[R15] = new_reg;
761
762 /* NOTE: Not subject to 'transmitter active' rule. */
763 write_zsreg(channel, R15, up->curregs[R15]);
764 }
765}
766
767/* The port lock is not held. */
768static void sunzilog_break_ctl(struct uart_port *port, int break_state)
769{
770 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
771 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
772 unsigned char set_bits, clear_bits, new_reg;
773 unsigned long flags;
774
775 set_bits = clear_bits = 0;
776
777 if (break_state)
778 set_bits |= SND_BRK;
779 else
780 clear_bits |= SND_BRK;
781
782 spin_lock_irqsave(&port->lock, flags);
783
784 new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
785 if (new_reg != up->curregs[R5]) {
786 up->curregs[R5] = new_reg;
787
788 /* NOTE: Not subject to 'transmitter active' rule. */
789 write_zsreg(channel, R5, up->curregs[R5]);
790 }
791
792 spin_unlock_irqrestore(&port->lock, flags);
793}
794
795static void __sunzilog_startup(struct uart_sunzilog_port *up)
796{
797 struct zilog_channel __iomem *channel;
798
799 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
800 up->prev_status = sbus_readb(&channel->control);
801
802 /* Enable receiver and transmitter. */
803 up->curregs[R3] |= RxENAB;
804 up->curregs[R5] |= TxENAB;
805
806 up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
807 sunzilog_maybe_update_regs(up, channel);
808}
809
810static int sunzilog_startup(struct uart_port *port)
811{
812 struct uart_sunzilog_port *up = UART_ZILOG(port);
813 unsigned long flags;
814
815 if (ZS_IS_CONS(up))
816 return 0;
817
818 spin_lock_irqsave(&port->lock, flags);
819 __sunzilog_startup(up);
820 spin_unlock_irqrestore(&port->lock, flags);
821 return 0;
822}
823
824/*
825 * The test for ZS_IS_CONS is explained by the following e-mail:
826 *****
827 * From: Russell King <rmk@arm.linux.org.uk>
828 * Date: Sun, 8 Dec 2002 10:18:38 +0000
829 *
830 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
831 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
832 * > and I noticed that something is not right with reference
833 * > counting in this case. It seems that when the console
834 * > is open by kernel initially, this is not accounted
835 * > as an open, and uart_startup is not called.
836 *
837 * That is correct. We are unable to call uart_startup when the serial
838 * console is initialised because it may need to allocate memory (as
839 * request_irq does) and the memory allocators may not have been
840 * initialised.
841 *
842 * 1. initialise the port into a state where it can send characters in the
843 * console write method.
844 *
845 * 2. don't do the actual hardware shutdown in your shutdown() method (but
846 * do the normal software shutdown - ie, free irqs etc)
847 *****
848 */
849static void sunzilog_shutdown(struct uart_port *port)
850{
851 struct uart_sunzilog_port *up = UART_ZILOG(port);
852 struct zilog_channel __iomem *channel;
853 unsigned long flags;
854
855 if (ZS_IS_CONS(up))
856 return;
857
858 spin_lock_irqsave(&port->lock, flags);
859
860 channel = ZILOG_CHANNEL_FROM_PORT(port);
861
862 /* Disable receiver and transmitter. */
863 up->curregs[R3] &= ~RxENAB;
864 up->curregs[R5] &= ~TxENAB;
865
866 /* Disable all interrupts and BRK assertion. */
867 up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
868 up->curregs[R5] &= ~SND_BRK;
869 sunzilog_maybe_update_regs(up, channel);
870
871 spin_unlock_irqrestore(&port->lock, flags);
872}
873
874/* Shared by TTY driver and serial console setup. The port lock is held
875 * and local interrupts are disabled.
876 */
877static void
878sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
879 unsigned int iflag, int brg)
880{
881
882 up->curregs[R10] = NRZ;
883 up->curregs[R11] = TCBR | RCBR;
884
885 /* Program BAUD and clock source. */
886 up->curregs[R4] &= ~XCLK_MASK;
887 up->curregs[R4] |= X16CLK;
888 up->curregs[R12] = brg & 0xff;
889 up->curregs[R13] = (brg >> 8) & 0xff;
890 up->curregs[R14] = BRSRC | BRENAB;
891
892 /* Character size, stop bits, and parity. */
893 up->curregs[3] &= ~RxN_MASK;
894 up->curregs[5] &= ~TxN_MASK;
895 switch (cflag & CSIZE) {
896 case CS5:
897 up->curregs[3] |= Rx5;
898 up->curregs[5] |= Tx5;
899 up->parity_mask = 0x1f;
900 break;
901 case CS6:
902 up->curregs[3] |= Rx6;
903 up->curregs[5] |= Tx6;
904 up->parity_mask = 0x3f;
905 break;
906 case CS7:
907 up->curregs[3] |= Rx7;
908 up->curregs[5] |= Tx7;
909 up->parity_mask = 0x7f;
910 break;
911 case CS8:
912 default:
913 up->curregs[3] |= Rx8;
914 up->curregs[5] |= Tx8;
915 up->parity_mask = 0xff;
916 break;
917 };
918 up->curregs[4] &= ~0x0c;
919 if (cflag & CSTOPB)
920 up->curregs[4] |= SB2;
921 else
922 up->curregs[4] |= SB1;
923 if (cflag & PARENB)
924 up->curregs[4] |= PAR_ENAB;
925 else
926 up->curregs[4] &= ~PAR_ENAB;
927 if (!(cflag & PARODD))
928 up->curregs[4] |= PAR_EVEN;
929 else
930 up->curregs[4] &= ~PAR_EVEN;
931
932 up->port.read_status_mask = Rx_OVR;
933 if (iflag & INPCK)
934 up->port.read_status_mask |= CRC_ERR | PAR_ERR;
935 if (iflag & (BRKINT | PARMRK))
936 up->port.read_status_mask |= BRK_ABRT;
937
938 up->port.ignore_status_mask = 0;
939 if (iflag & IGNPAR)
940 up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
941 if (iflag & IGNBRK) {
942 up->port.ignore_status_mask |= BRK_ABRT;
943 if (iflag & IGNPAR)
944 up->port.ignore_status_mask |= Rx_OVR;
945 }
946
947 if ((cflag & CREAD) == 0)
948 up->port.ignore_status_mask = 0xff;
949}
950
951/* The port lock is not held. */
952static void
953sunzilog_set_termios(struct uart_port *port, struct termios *termios,
954 struct termios *old)
955{
956 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
957 unsigned long flags;
958 int baud, brg;
959
960 baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
961
962 spin_lock_irqsave(&up->port.lock, flags);
963
964 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
965
966 sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
967
968 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
969 up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
970 else
971 up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;
972
973 up->cflag = termios->c_cflag;
974
975 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
976
977 uart_update_timeout(port, termios->c_cflag, baud);
978
979 spin_unlock_irqrestore(&up->port.lock, flags);
980}
981
982static const char *sunzilog_type(struct uart_port *port)
983{
984 return "SunZilog";
985}
986
987/* We do not request/release mappings of the registers here, this
988 * happens at early serial probe time.
989 */
990static void sunzilog_release_port(struct uart_port *port)
991{
992}
993
994static int sunzilog_request_port(struct uart_port *port)
995{
996 return 0;
997}
998
999/* These do not need to do anything interesting either. */
1000static void sunzilog_config_port(struct uart_port *port, int flags)
1001{
1002}
1003
1004/* We do not support letting the user mess with the divisor, IRQ, etc. */
1005static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
1006{
1007 return -EINVAL;
1008}
1009
1010static struct uart_ops sunzilog_pops = {
1011 .tx_empty = sunzilog_tx_empty,
1012 .set_mctrl = sunzilog_set_mctrl,
1013 .get_mctrl = sunzilog_get_mctrl,
1014 .stop_tx = sunzilog_stop_tx,
1015 .start_tx = sunzilog_start_tx,
1016 .stop_rx = sunzilog_stop_rx,
1017 .enable_ms = sunzilog_enable_ms,
1018 .break_ctl = sunzilog_break_ctl,
1019 .startup = sunzilog_startup,
1020 .shutdown = sunzilog_shutdown,
1021 .set_termios = sunzilog_set_termios,
1022 .type = sunzilog_type,
1023 .release_port = sunzilog_release_port,
1024 .request_port = sunzilog_request_port,
1025 .config_port = sunzilog_config_port,
1026 .verify_port = sunzilog_verify_port,
1027};
1028
1029static struct uart_sunzilog_port *sunzilog_port_table;
1030static struct zilog_layout __iomem **sunzilog_chip_regs;
1031
1032static struct uart_sunzilog_port *sunzilog_irq_chain;
1033static int zilog_irq = -1;
1034
1035static struct uart_driver sunzilog_reg = {
1036 .owner = THIS_MODULE,
1037 .driver_name = "ttyS",
1038 .devfs_name = "tts/",
1039 .dev_name = "ttyS",
1040 .major = TTY_MAJOR,
1041};
1042
1043static void * __init alloc_one_table(unsigned long size)
1044{
1045 void *ret;
1046
1047 ret = kmalloc(size, GFP_KERNEL);
1048 if (ret != NULL)
1049 memset(ret, 0, size);
1050
1051 return ret;
1052}
1053
1054static void __init sunzilog_alloc_tables(void)
1055{
1056 sunzilog_port_table =
1057 alloc_one_table(NUM_CHANNELS * sizeof(struct uart_sunzilog_port));
1058 sunzilog_chip_regs =
1059 alloc_one_table(NUM_SUNZILOG * sizeof(struct zilog_layout __iomem *));
1060
1061 if (sunzilog_port_table == NULL || sunzilog_chip_regs == NULL) {
1062 prom_printf("SunZilog: Cannot allocate tables.\n");
1063 prom_halt();
1064 }
1065}
1066
1067#ifdef CONFIG_SPARC64
1068
1069/* We used to attempt to use the address property of the Zilog device node
1070 * but that totally is not necessary on sparc64.
1071 */
1072static struct zilog_layout __iomem * __init get_zs_sun4u(int chip, int zsnode)
1073{
1074 unsigned long mapped_addr;
1075 unsigned int sun4u_ino;
1076 struct sbus_bus *sbus = NULL;
1077 struct sbus_dev *sdev = NULL;
1078 int err;
1079
1080 if (central_bus == NULL) {
1081 for_each_sbus(sbus) {
1082 for_each_sbusdev(sdev, sbus) {
1083 if (sdev->prom_node == zsnode)
1084 goto found;
1085 }
1086 }
1087 }
1088 found:
1089 if (sdev == NULL && central_bus == NULL) {
1090 prom_printf("SunZilog: sdev&&central == NULL for "
1091 "Zilog %d in get_zs_sun4u.\n", chip);
1092 prom_halt();
1093 }
1094 if (central_bus == NULL) {
1095 mapped_addr =
1096 sbus_ioremap(&sdev->resource[0], 0,
1097 PAGE_SIZE,
1098 "Zilog Registers");
1099 } else {
1100 struct linux_prom_registers zsregs[1];
1101
1102 err = prom_getproperty(zsnode, "reg",
1103 (char *) &zsregs[0],
1104 sizeof(zsregs));
1105 if (err == -1) {
1106 prom_printf("SunZilog: Cannot map "
1107 "Zilog %d regs on "
1108 "central bus.\n", chip);
1109 prom_halt();
1110 }
1111 apply_fhc_ranges(central_bus->child,
1112 &zsregs[0], 1);
1113 apply_central_ranges(central_bus, &zsregs[0], 1);
1114 mapped_addr =
1115 (((u64)zsregs[0].which_io)<<32UL) |
1116 ((u64)zsregs[0].phys_addr);
1117 }
1118
1119 if (zilog_irq == -1) {
1120 if (central_bus) {
1121 unsigned long iclr, imap;
1122
1123 iclr = central_bus->child->fhc_regs.uregs
1124 + FHC_UREGS_ICLR;
1125 imap = central_bus->child->fhc_regs.uregs
1126 + FHC_UREGS_IMAP;
1127 zilog_irq = build_irq(12, 0, iclr, imap);
1128 } else {
1129 err = prom_getproperty(zsnode, "interrupts",
1130 (char *) &sun4u_ino,
1131 sizeof(sun4u_ino));
1132 zilog_irq = sbus_build_irq(sbus_root, sun4u_ino);
1133 }
1134 }
1135
1136 return (struct zilog_layout __iomem *) mapped_addr;
1137}
1138#else /* CONFIG_SPARC64 */
1139
1140/*
1141 * XXX The sun4d case is utterly screwed: it tries to re-walk the tree
1142 * (for the 3rd time) in order to find bootbus and cpu. Streamline it.
1143 */
1144static struct zilog_layout __iomem * __init get_zs_sun4cmd(int chip, int node)
1145{
1146 struct linux_prom_irqs irq_info[2];
1147 void __iomem *mapped_addr = NULL;
1148 int zsnode, cpunode, bbnode;
1149 struct linux_prom_registers zsreg[4];
1150 struct resource res;
1151
1152 if (sparc_cpu_model == sun4d) {
1153 int walk;
1154
1155 zsnode = 0;
1156 bbnode = 0;
1157 cpunode = 0;
1158 for (walk = prom_getchild(prom_root_node);
1159 (walk = prom_searchsiblings(walk, "cpu-unit")) != 0;
1160 walk = prom_getsibling(walk)) {
1161 bbnode = prom_getchild(walk);
1162 if (bbnode &&
1163 (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
1164 if ((zsnode = prom_getchild(bbnode)) == node) {
1165 cpunode = walk;
1166 break;
1167 }
1168 }
1169 }
1170 if (!walk) {
1171 prom_printf("SunZilog: Cannot find the %d'th bootbus on sun4d.\n",
1172 (chip / 2));
1173 prom_halt();
1174 }
1175
1176 if (prom_getproperty(zsnode, "reg",
1177 (char *) zsreg, sizeof(zsreg)) == -1) {
1178 prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
1179 prom_halt();
1180 }
1181 /* XXX Looks like an off by one? */
1182 prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
1183 res.start = zsreg[0].phys_addr;
1184 res.end = res.start + (8 - 1);
1185 res.flags = zsreg[0].which_io | IORESOURCE_IO;
1186 mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
1187
1188 } else {
1189 zsnode = node;
1190
1191#if 0 /* XXX When was this used? */
1192 if (prom_getintdefault(zsnode, "slave", -1) != chipid) {
1193 zsnode = prom_getsibling(zsnode);
1194 continue;
1195 }
1196#endif
1197
1198 /*
1199 * "address" is only present on ports that OBP opened
1200 * (from Mitch Bradley's "Hitchhiker's Guide to OBP").
1201 * We do not use it.
1202 */
1203
1204 if (prom_getproperty(zsnode, "reg",
1205 (char *) zsreg, sizeof(zsreg)) == -1) {
1206 prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
1207 prom_halt();
1208 }
1209 if (sparc_cpu_model == sun4m) /* Crude. Pass parent. XXX */
1210 prom_apply_obio_ranges(zsreg, 1);
1211 res.start = zsreg[0].phys_addr;
1212 res.end = res.start + (8 - 1);
1213 res.flags = zsreg[0].which_io | IORESOURCE_IO;
1214 mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
1215 }
1216
1217 if (prom_getproperty(zsnode, "intr",
1218 (char *) irq_info, sizeof(irq_info))
1219 % sizeof(struct linux_prom_irqs)) {
1220 prom_printf("SunZilog: Cannot get IRQ property for Zilog %d.\n",
1221 chip);
1222 prom_halt();
1223 }
1224 if (zilog_irq == -1) {
1225 zilog_irq = irq_info[0].pri;
1226 } else if (zilog_irq != irq_info[0].pri) {
1227 /* XXX. Dumb. Should handle per-chip IRQ, for add-ons. */
1228 prom_printf("SunZilog: Inconsistent IRQ layout for Zilog %d.\n",
1229 chip);
1230 prom_halt();
1231 }
1232
1233 return (struct zilog_layout __iomem *) mapped_addr;
1234}
1235#endif /* !(CONFIG_SPARC64) */
1236
1237/* Get the address of the registers for SunZilog instance CHIP. */
1238static struct zilog_layout __iomem * __init get_zs(int chip, int node)
1239{
1240 if (chip < 0 || chip >= NUM_SUNZILOG) {
1241 prom_printf("SunZilog: Illegal chip number %d in get_zs.\n", chip);
1242 prom_halt();
1243 }
1244
1245#ifdef CONFIG_SPARC64
1246 return get_zs_sun4u(chip, node);
1247#else
1248
1249 if (sparc_cpu_model == sun4) {
1250 struct resource res;
1251
1252 /* Not probe-able, hard code it. */
1253 switch (chip) {
1254 case 0:
1255 res.start = 0xf1000000;
1256 break;
1257 case 1:
1258 res.start = 0xf0000000;
1259 break;
1260 };
1261 zilog_irq = 12;
1262 res.end = (res.start + (8 - 1));
1263 res.flags = IORESOURCE_IO;
1264 return sbus_ioremap(&res, 0, 8, "SunZilog");
1265 }
1266
1267 return get_zs_sun4cmd(chip, node);
1268#endif
1269}
1270
1271#define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
1272
1273static void sunzilog_put_char(struct zilog_channel __iomem *channel, unsigned char ch)
1274{
1275 int loops = ZS_PUT_CHAR_MAX_DELAY;
1276
1277 /* This is a timed polling loop so do not switch the explicit
1278 * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
1279 */
1280 do {
1281 unsigned char val = sbus_readb(&channel->control);
1282 if (val & Tx_BUF_EMP) {
1283 ZSDELAY();
1284 break;
1285 }
1286 udelay(5);
1287 } while (--loops);
1288
1289 sbus_writeb(ch, &channel->data);
1290 ZSDELAY();
1291 ZS_WSYNC(channel);
1292}
1293
1294#ifdef CONFIG_SERIO
1295
1296static DEFINE_SPINLOCK(sunzilog_serio_lock);
1297
1298static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
1299{
1300 struct uart_sunzilog_port *up = serio->port_data;
1301 unsigned long flags;
1302
1303 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1304
1305 sunzilog_put_char(ZILOG_CHANNEL_FROM_PORT(&up->port), ch);
1306
1307 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1308
1309 return 0;
1310}
1311
1312static int sunzilog_serio_open(struct serio *serio)
1313{
1314 struct uart_sunzilog_port *up = serio->port_data;
1315 unsigned long flags;
1316 int ret;
1317
1318 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1319 if (!up->serio_open) {
1320 up->serio_open = 1;
1321 ret = 0;
1322 } else
1323 ret = -EBUSY;
1324 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1325
1326 return ret;
1327}
1328
1329static void sunzilog_serio_close(struct serio *serio)
1330{
1331 struct uart_sunzilog_port *up = serio->port_data;
1332 unsigned long flags;
1333
1334 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1335 up->serio_open = 0;
1336 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1337}
1338
1339#endif /* CONFIG_SERIO */
1340
1341#ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
1342static void
1343sunzilog_console_write(struct console *con, const char *s, unsigned int count)
1344{
1345 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1346 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1347 unsigned long flags;
1348 int i;
1349
1350 spin_lock_irqsave(&up->port.lock, flags);
1351 for (i = 0; i < count; i++, s++) {
1352 sunzilog_put_char(channel, *s);
1353 if (*s == 10)
1354 sunzilog_put_char(channel, 13);
1355 }
1356 udelay(2);
1357 spin_unlock_irqrestore(&up->port.lock, flags);
1358}
1359
1360static int __init sunzilog_console_setup(struct console *con, char *options)
1361{
1362 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1363 unsigned long flags;
1364 int baud, brg;
1365
1366 printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
1367 (sunzilog_reg.minor - 64) + con->index, con->index);
1368
1369 /* Get firmware console settings. */
1370 sunserial_console_termios(con);
1371
1372 /* Firmware console speed is limited to 150-->38400 baud so
1373 * this hackish cflag thing is OK.
1374 */
1375 switch (con->cflag & CBAUD) {
1376 case B150: baud = 150; break;
1377 case B300: baud = 300; break;
1378 case B600: baud = 600; break;
1379 case B1200: baud = 1200; break;
1380 case B2400: baud = 2400; break;
1381 case B4800: baud = 4800; break;
1382 default: case B9600: baud = 9600; break;
1383 case B19200: baud = 19200; break;
1384 case B38400: baud = 38400; break;
1385 };
1386
1387 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1388
1389 spin_lock_irqsave(&up->port.lock, flags);
1390
1391 up->curregs[R15] = BRKIE;
1392 sunzilog_convert_to_zs(up, con->cflag, 0, brg);
1393
1394 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1395 __sunzilog_startup(up);
1396
1397 spin_unlock_irqrestore(&up->port.lock, flags);
1398
1399 return 0;
1400}
1401
1402static struct console sunzilog_console = {
1403 .name = "ttyS",
1404 .write = sunzilog_console_write,
1405 .device = uart_console_device,
1406 .setup = sunzilog_console_setup,
1407 .flags = CON_PRINTBUFFER,
1408 .index = -1,
1409 .data = &sunzilog_reg,
1410};
1411#define SUNZILOG_CONSOLE (&sunzilog_console)
1412
1413static int __init sunzilog_console_init(void)
1414{
1415 int i;
1416
1417 if (con_is_present())
1418 return 0;
1419
1420 for (i = 0; i < NUM_CHANNELS; i++) {
1421 int this_minor = sunzilog_reg.minor + i;
1422
1423 if ((this_minor - 64) == (serial_console - 1))
1424 break;
1425 }
1426 if (i == NUM_CHANNELS)
1427 return 0;
1428
1429 sunzilog_console.index = i;
1430 sunzilog_port_table[i].flags |= SUNZILOG_FLAG_IS_CONS;
1431 register_console(&sunzilog_console);
1432 return 0;
1433}
1434#else
1435#define SUNZILOG_CONSOLE (NULL)
1436#define sunzilog_console_init() do { } while (0)
1437#endif
1438
1439/*
1440 * We scan the PROM tree recursively. This is the most reliable way
1441 * to find Zilog nodes on various platforms. However, we face an extreme
1442 * shortage of kernel stack, so we must be very careful. To that end,
1443 * we scan only to a certain depth, and we use a common property buffer
1444 * in the scan structure.
1445 */
1446#define ZS_PROPSIZE 128
1447#define ZS_SCAN_DEPTH 5
1448
1449struct zs_probe_scan {
1450 int depth;
1451 void (*scanner)(struct zs_probe_scan *t, int node);
1452
1453 int devices;
1454 char prop[ZS_PROPSIZE];
1455};
1456
1457static int __inline__ sunzilog_node_ok(int node, const char *name, int len)
1458{
1459 if (strncmp(name, "zs", len) == 0)
1460 return 1;
1461 /* Don't fold this procedure just yet. Compare to su_node_ok(). */
1462 return 0;
1463}
1464
1465static void __init sunzilog_scan(struct zs_probe_scan *t, int node)
1466{
1467 int len;
1468
1469 for (; node != 0; node = prom_getsibling(node)) {
1470 len = prom_getproperty(node, "name", t->prop, ZS_PROPSIZE);
1471 if (len <= 1)
1472 continue; /* Broken PROM node */
1473 if (sunzilog_node_ok(node, t->prop, len)) {
1474 (*t->scanner)(t, node);
1475 } else {
1476 if (t->depth < ZS_SCAN_DEPTH) {
1477 t->depth++;
1478 sunzilog_scan(t, prom_getchild(node));
1479 --t->depth;
1480 }
1481 }
1482 }
1483}
1484
1485static void __init sunzilog_prepare(void)
1486{
1487 struct uart_sunzilog_port *up;
1488 struct zilog_layout __iomem *rp;
1489 int channel, chip;
1490
1491 /*
1492 * Temporary fix.
1493 */
1494 for (channel = 0; channel < NUM_CHANNELS; channel++)
1495 spin_lock_init(&sunzilog_port_table[channel].port.lock);
1496
1497 sunzilog_irq_chain = up = &sunzilog_port_table[0];
1498 for (channel = 0; channel < NUM_CHANNELS - 1; channel++)
1499 up[channel].next = &up[channel + 1];
1500 up[channel].next = NULL;
1501
1502 for (chip = 0; chip < NUM_SUNZILOG; chip++) {
1503 rp = sunzilog_chip_regs[chip];
1504 up[(chip * 2) + 0].port.membase = (void __iomem *)&rp->channelA;
1505 up[(chip * 2) + 1].port.membase = (void __iomem *)&rp->channelB;
1506
1507 /* Channel A */
1508 up[(chip * 2) + 0].port.iotype = SERIAL_IO_MEM;
1509 up[(chip * 2) + 0].port.irq = zilog_irq;
1510 up[(chip * 2) + 0].port.uartclk = ZS_CLOCK;
1511 up[(chip * 2) + 0].port.fifosize = 1;
1512 up[(chip * 2) + 0].port.ops = &sunzilog_pops;
1513 up[(chip * 2) + 0].port.type = PORT_SUNZILOG;
1514 up[(chip * 2) + 0].port.flags = 0;
1515 up[(chip * 2) + 0].port.line = (chip * 2) + 0;
1516 up[(chip * 2) + 0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
1517
1518 /* Channel B */
1519 up[(chip * 2) + 1].port.iotype = SERIAL_IO_MEM;
1520 up[(chip * 2) + 1].port.irq = zilog_irq;
1521 up[(chip * 2) + 1].port.uartclk = ZS_CLOCK;
1522 up[(chip * 2) + 1].port.fifosize = 1;
1523 up[(chip * 2) + 1].port.ops = &sunzilog_pops;
1524 up[(chip * 2) + 1].port.type = PORT_SUNZILOG;
1525 up[(chip * 2) + 1].port.flags = 0;
1526 up[(chip * 2) + 1].port.line = (chip * 2) + 1;
1527 up[(chip * 2) + 1].flags |= 0;
1528 }
1529}
1530
1531static void __init sunzilog_init_kbdms(struct uart_sunzilog_port *up, int channel)
1532{
1533 int baud, brg;
1534
1535 if (channel == KEYBOARD_LINE) {
1536 up->flags |= SUNZILOG_FLAG_CONS_KEYB;
1537 up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1538 baud = 1200;
1539 } else {
1540 up->flags |= SUNZILOG_FLAG_CONS_MOUSE;
1541 up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1542 baud = 4800;
1543 }
1544 printk(KERN_INFO "zs%d at 0x%p (irq = %s) is a SunZilog\n",
1545 channel, up->port.membase, __irq_itoa(zilog_irq));
1546
1547 up->curregs[R15] = BRKIE;
1548 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1549 sunzilog_convert_to_zs(up, up->cflag, 0, brg);
1550 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1551 __sunzilog_startup(up);
1552}
1553
1554#ifdef CONFIG_SERIO
1555static void __init sunzilog_register_serio(struct uart_sunzilog_port *up, int channel)
1556{
1557 struct serio *serio;
1558
1559 up->serio = serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
1560 if (serio) {
1561 memset(serio, 0, sizeof(*serio));
1562
1563 serio->port_data = up;
1564
1565 serio->id.type = SERIO_RS232;
1566 if (channel == KEYBOARD_LINE) {
1567 serio->id.proto = SERIO_SUNKBD;
1568 strlcpy(serio->name, "zskbd", sizeof(serio->name));
1569 } else {
1570 serio->id.proto = SERIO_SUN;
1571 serio->id.extra = 1;
1572 strlcpy(serio->name, "zsms", sizeof(serio->name));
1573 }
1574 strlcpy(serio->phys,
1575 (channel == KEYBOARD_LINE ? "zs/serio0" : "zs/serio1"),
1576 sizeof(serio->phys));
1577
1578 serio->write = sunzilog_serio_write;
1579 serio->open = sunzilog_serio_open;
1580 serio->close = sunzilog_serio_close;
1581
1582 serio_register_port(serio);
1583 } else {
1584 printk(KERN_WARNING "zs%d: not enough memory for serio port\n",
1585 channel);
1586 }
1587}
1588#endif
1589
1590static void __init sunzilog_init_hw(void)
1591{
1592 int i;
1593
1594 for (i = 0; i < NUM_CHANNELS; i++) {
1595 struct uart_sunzilog_port *up = &sunzilog_port_table[i];
1596 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1597 unsigned long flags;
1598 int baud, brg;
1599
1600 spin_lock_irqsave(&up->port.lock, flags);
1601
1602 if (ZS_IS_CHANNEL_A(up)) {
1603 write_zsreg(channel, R9, FHWRES);
1604 ZSDELAY_LONG();
1605 (void) read_zsreg(channel, R0);
1606 }
1607
1608 if (i == KEYBOARD_LINE || i == MOUSE_LINE) {
1609 sunzilog_init_kbdms(up, i);
1610 up->curregs[R9] |= (NV | MIE);
1611 write_zsreg(channel, R9, up->curregs[R9]);
1612 } else {
1613 /* Normal serial TTY. */
1614 up->parity_mask = 0xff;
1615 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1616 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1617 up->curregs[R3] = RxENAB | Rx8;
1618 up->curregs[R5] = TxENAB | Tx8;
1619 up->curregs[R9] = NV | MIE;
1620 up->curregs[R10] = NRZ;
1621 up->curregs[R11] = TCBR | RCBR;
1622 baud = 9600;
1623 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1624 up->curregs[R12] = (brg & 0xff);
1625 up->curregs[R13] = (brg >> 8) & 0xff;
1626 up->curregs[R14] = BRSRC | BRENAB;
1627 __load_zsregs(channel, up->curregs);
1628 write_zsreg(channel, R9, up->curregs[R9]);
1629 }
1630
1631 spin_unlock_irqrestore(&up->port.lock, flags);
1632
1633#ifdef CONFIG_SERIO
1634 if (i == KEYBOARD_LINE || i == MOUSE_LINE)
1635 sunzilog_register_serio(up, i);
1636#endif
1637 }
1638}
1639
1640static struct zilog_layout __iomem * __init get_zs(int chip, int node);
1641
1642static void __init sunzilog_scan_probe(struct zs_probe_scan *t, int node)
1643{
1644 sunzilog_chip_regs[t->devices] = get_zs(t->devices, node);
1645 t->devices++;
1646}
1647
1648static int __init sunzilog_ports_init(void)
1649{
1650 struct zs_probe_scan scan;
1651 int ret;
1652 int uart_count;
1653 int i;
1654
1655 printk(KERN_DEBUG "SunZilog: %d chips.\n", NUM_SUNZILOG);
1656
1657 scan.scanner = sunzilog_scan_probe;
1658 scan.depth = 0;
1659 scan.devices = 0;
1660 sunzilog_scan(&scan, prom_getchild(prom_root_node));
1661
1662 sunzilog_prepare();
1663
1664 if (request_irq(zilog_irq, sunzilog_interrupt, SA_SHIRQ,
1665 "SunZilog", sunzilog_irq_chain)) {
1666 prom_printf("SunZilog: Unable to register zs interrupt handler.\n");
1667 prom_halt();
1668 }
1669
1670 sunzilog_init_hw();
1671
1672 /* We can only init this once we have probed the Zilogs
1673 * in the system. Do not count channels assigned to keyboards
1674 * or mice when we are deciding how many ports to register.
1675 */
1676 uart_count = 0;
1677 for (i = 0; i < NUM_CHANNELS; i++) {
1678 struct uart_sunzilog_port *up = &sunzilog_port_table[i];
1679
1680 if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
1681 continue;
1682
1683 uart_count++;
1684 }
1685
1686 sunzilog_reg.nr = uart_count;
1687 sunzilog_reg.cons = SUNZILOG_CONSOLE;
1688
1689 sunzilog_reg.minor = sunserial_current_minor;
1690 sunserial_current_minor += uart_count;
1691
1692 ret = uart_register_driver(&sunzilog_reg);
1693 if (ret == 0) {
1694 sunzilog_console_init();
1695 for (i = 0; i < NUM_CHANNELS; i++) {
1696 struct uart_sunzilog_port *up = &sunzilog_port_table[i];
1697
1698 if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
1699 continue;
1700
1701 if (uart_add_one_port(&sunzilog_reg, &up->port)) {
1702 printk(KERN_ERR
1703 "SunZilog: failed to add port zs%d\n", i);
1704 }
1705 }
1706 }
1707
1708 return ret;
1709}
1710
1711static void __init sunzilog_scan_count(struct zs_probe_scan *t, int node)
1712{
1713 t->devices++;
1714}
1715
1716static int __init sunzilog_ports_count(void)
1717{
1718 struct zs_probe_scan scan;
1719
1720 /* Sun4 Zilog setup is hard coded, no probing to do. */
1721 if (sparc_cpu_model == sun4)
1722 return 2;
1723
1724 scan.scanner = sunzilog_scan_count;
1725 scan.depth = 0;
1726 scan.devices = 0;
1727
1728 sunzilog_scan(&scan, prom_getchild(prom_root_node));
1729
1730 return scan.devices;
1731}
1732
1733static int __init sunzilog_init(void)
1734{
1735
1736 NUM_SUNZILOG = sunzilog_ports_count();
1737 if (NUM_SUNZILOG == 0)
1738 return -ENODEV;
1739
1740 sunzilog_alloc_tables();
1741
1742 sunzilog_ports_init();
1743
1744 return 0;
1745}
1746
1747static void __exit sunzilog_exit(void)
1748{
1749 int i;
1750
1751 for (i = 0; i < NUM_CHANNELS; i++) {
1752 struct uart_sunzilog_port *up = &sunzilog_port_table[i];
1753
1754 if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
1755#ifdef CONFIG_SERIO
1756 if (up->serio) {
1757 serio_unregister_port(up->serio);
1758 up->serio = NULL;
1759 }
1760#endif
1761 } else
1762 uart_remove_one_port(&sunzilog_reg, &up->port);
1763 }
1764
1765 uart_unregister_driver(&sunzilog_reg);
1766}
1767
1768module_init(sunzilog_init);
1769module_exit(sunzilog_exit);
1770
1771MODULE_AUTHOR("David S. Miller");
1772MODULE_DESCRIPTION("Sun Zilog serial port driver");
1773MODULE_LICENSE("GPL");
diff --git a/drivers/serial/sunzilog.h b/drivers/serial/sunzilog.h
new file mode 100644
index 000000000000..7939b6d71270
--- /dev/null
+++ b/drivers/serial/sunzilog.h
@@ -0,0 +1,272 @@
1#ifndef _SUNZILOG_H
2#define _SUNZILOG_H
3
4struct zilog_channel {
5 volatile unsigned char control;
6 volatile unsigned char __pad1;
7 volatile unsigned char data;
8 volatile unsigned char __pad2;
9};
10
11struct zilog_layout {
12 struct zilog_channel channelB;
13 struct zilog_channel channelA;
14};
15
16#define NUM_ZSREGS 16
17
18/* Conversion routines to/from brg time constants from/to bits
19 * per second.
20 */
21#define BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
22#define BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
23
24/* The Zilog register set */
25
26#define FLAG 0x7e
27
28/* Write Register 0 */
29#define R0 0 /* Register selects */
30#define R1 1
31#define R2 2
32#define R3 3
33#define R4 4
34#define R5 5
35#define R6 6
36#define R7 7
37#define R8 8
38#define R9 9
39#define R10 10
40#define R11 11
41#define R12 12
42#define R13 13
43#define R14 14
44#define R15 15
45
46#define NULLCODE 0 /* Null Code */
47#define POINT_HIGH 0x8 /* Select upper half of registers */
48#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
49#define SEND_ABORT 0x18 /* HDLC Abort */
50#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
51#define RES_Tx_P 0x28 /* Reset TxINT Pending */
52#define ERR_RES 0x30 /* Error Reset */
53#define RES_H_IUS 0x38 /* Reset highest IUS */
54
55#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
56#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
57#define RES_EOM_L 0xC0 /* Reset EOM latch */
58
59/* Write Register 1 */
60
61#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
62#define TxINT_ENAB 0x2 /* Tx Int Enable */
63#define PAR_SPEC 0x4 /* Parity is special condition */
64
65#define RxINT_DISAB 0 /* Rx Int Disable */
66#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
67#define INT_ALL_Rx 0x10 /* Int on all Rx Characters or error */
68#define INT_ERR_Rx 0x18 /* Int on error only */
69#define RxINT_MASK 0x18
70
71#define WT_RDY_RT 0x20 /* Wait/Ready on R/T */
72#define WT_FN_RDYFN 0x40 /* Wait/FN/Ready FN */
73#define WT_RDY_ENAB 0x80 /* Wait/Ready Enable */
74
75/* Write Register #2 (Interrupt Vector) */
76
77/* Write Register 3 */
78
79#define RxENAB 0x1 /* Rx Enable */
80#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
81#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
82#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
83#define ENT_HM 0x10 /* Enter Hunt Mode */
84#define AUTO_ENAB 0x20 /* Auto Enables */
85#define Rx5 0x0 /* Rx 5 Bits/Character */
86#define Rx7 0x40 /* Rx 7 Bits/Character */
87#define Rx6 0x80 /* Rx 6 Bits/Character */
88#define Rx8 0xc0 /* Rx 8 Bits/Character */
89#define RxN_MASK 0xc0
90
91/* Write Register 4 */
92
93#define PAR_ENAB 0x1 /* Parity Enable */
94#define PAR_EVEN 0x2 /* Parity Even/Odd* */
95
96#define SYNC_ENAB 0 /* Sync Modes Enable */
97#define SB1 0x4 /* 1 stop bit/char */
98#define SB15 0x8 /* 1.5 stop bits/char */
99#define SB2 0xc /* 2 stop bits/char */
100
101#define MONSYNC 0 /* 8 Bit Sync character */
102#define BISYNC 0x10 /* 16 bit sync character */
103#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
104#define EXTSYNC 0x30 /* External Sync Mode */
105
106#define X1CLK 0x0 /* x1 clock mode */
107#define X16CLK 0x40 /* x16 clock mode */
108#define X32CLK 0x80 /* x32 clock mode */
109#define X64CLK 0xC0 /* x64 clock mode */
110#define XCLK_MASK 0xC0
111
112/* Write Register 5 */
113
114#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
115#define RTS 0x2 /* RTS */
116#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
117#define TxENAB 0x8 /* Tx Enable */
118#define SND_BRK 0x10 /* Send Break */
119#define Tx5 0x0 /* Tx 5 bits (or less)/character */
120#define Tx7 0x20 /* Tx 7 bits/character */
121#define Tx6 0x40 /* Tx 6 bits/character */
122#define Tx8 0x60 /* Tx 8 bits/character */
123#define TxN_MASK 0x60
124#define DTR 0x80 /* DTR */
125
126/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
127
128/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
129
130/* Write Register 8 (transmit buffer) */
131
132/* Write Register 9 (Master interrupt control) */
133#define VIS 1 /* Vector Includes Status */
134#define NV 2 /* No Vector */
135#define DLC 4 /* Disable Lower Chain */
136#define MIE 8 /* Master Interrupt Enable */
137#define STATHI 0x10 /* Status high */
138#define NORESET 0 /* No reset on write to R9 */
139#define CHRB 0x40 /* Reset channel B */
140#define CHRA 0x80 /* Reset channel A */
141#define FHWRES 0xc0 /* Force hardware reset */
142
143/* Write Register 10 (misc control bits) */
144#define BIT6 1 /* 6 bit/8bit sync */
145#define LOOPMODE 2 /* SDLC Loop mode */
146#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
147#define MARKIDLE 8 /* Mark/flag on idle */
148#define GAOP 0x10 /* Go active on poll */
149#define NRZ 0 /* NRZ mode */
150#define NRZI 0x20 /* NRZI mode */
151#define FM1 0x40 /* FM1 (transition = 1) */
152#define FM0 0x60 /* FM0 (transition = 0) */
153#define CRCPS 0x80 /* CRC Preset I/O */
154
155/* Write Register 11 (Clock Mode control) */
156#define TRxCXT 0 /* TRxC = Xtal output */
157#define TRxCTC 1 /* TRxC = Transmit clock */
158#define TRxCBR 2 /* TRxC = BR Generator Output */
159#define TRxCDP 3 /* TRxC = DPLL output */
160#define TRxCOI 4 /* TRxC O/I */
161#define TCRTxCP 0 /* Transmit clock = RTxC pin */
162#define TCTRxCP 8 /* Transmit clock = TRxC pin */
163#define TCBR 0x10 /* Transmit clock = BR Generator output */
164#define TCDPLL 0x18 /* Transmit clock = DPLL output */
165#define RCRTxCP 0 /* Receive clock = RTxC pin */
166#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
167#define RCBR 0x40 /* Receive clock = BR Generator output */
168#define RCDPLL 0x60 /* Receive clock = DPLL output */
169#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
170
171/* Write Register 12 (lower byte of baud rate generator time constant) */
172
173/* Write Register 13 (upper byte of baud rate generator time constant) */
174
175/* Write Register 14 (Misc control bits) */
176#define BRENAB 1 /* Baud rate generator enable */
177#define BRSRC 2 /* Baud rate generator source */
178#define DTRREQ 4 /* DTR/Request function */
179#define AUTOECHO 8 /* Auto Echo */
180#define LOOPBAK 0x10 /* Local loopback */
181#define SEARCH 0x20 /* Enter search mode */
182#define RMC 0x40 /* Reset missing clock */
183#define DISDPLL 0x60 /* Disable DPLL */
184#define SSBR 0x80 /* Set DPLL source = BR generator */
185#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
186#define SFMM 0xc0 /* Set FM mode */
187#define SNRZI 0xe0 /* Set NRZI mode */
188
189/* Write Register 15 (external/status interrupt control) */
190#define ZCIE 2 /* Zero count IE */
191#define DCDIE 8 /* DCD IE */
192#define SYNCIE 0x10 /* Sync/hunt IE */
193#define CTSIE 0x20 /* CTS IE */
194#define TxUIE 0x40 /* Tx Underrun/EOM IE */
195#define BRKIE 0x80 /* Break/Abort IE */
196
197
198/* Read Register 0 */
199#define Rx_CH_AV 0x1 /* Rx Character Available */
200#define ZCOUNT 0x2 /* Zero count */
201#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
202#define DCD 0x8 /* DCD */
203#define SYNC 0x10 /* Sync/hunt */
204#define CTS 0x20 /* CTS */
205#define TxEOM 0x40 /* Tx underrun */
206#define BRK_ABRT 0x80 /* Break/Abort */
207
208/* Read Register 1 */
209#define ALL_SNT 0x1 /* All sent */
210/* Residue Data for 8 Rx bits/char programmed */
211#define RES3 0x8 /* 0/3 */
212#define RES4 0x4 /* 0/4 */
213#define RES5 0xc /* 0/5 */
214#define RES6 0x2 /* 0/6 */
215#define RES7 0xa /* 0/7 */
216#define RES8 0x6 /* 0/8 */
217#define RES18 0xe /* 1/8 */
218#define RES28 0x0 /* 2/8 */
219/* Special Rx Condition Interrupts */
220#define PAR_ERR 0x10 /* Parity error */
221#define Rx_OVR 0x20 /* Rx Overrun Error */
222#define CRC_ERR 0x40 /* CRC/Framing Error */
223#define END_FR 0x80 /* End of Frame (SDLC) */
224
225/* Read Register 2 (channel b only) - Interrupt vector */
226#define CHB_Tx_EMPTY 0x00
227#define CHB_EXT_STAT 0x02
228#define CHB_Rx_AVAIL 0x04
229#define CHB_SPECIAL 0x06
230#define CHA_Tx_EMPTY 0x08
231#define CHA_EXT_STAT 0x0a
232#define CHA_Rx_AVAIL 0x0c
233#define CHA_SPECIAL 0x0e
234#define STATUS_MASK 0x0e
235
236/* Read Register 3 (interrupt pending register) ch a only */
237#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
238#define CHBTxIP 0x2 /* Channel B Tx IP */
239#define CHBRxIP 0x4 /* Channel B Rx IP */
240#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
241#define CHATxIP 0x10 /* Channel A Tx IP */
242#define CHARxIP 0x20 /* Channel A Rx IP */
243
244/* Read Register 8 (receive data register) */
245
246/* Read Register 10 (misc status bits) */
247#define ONLOOP 2 /* On loop */
248#define LOOPSEND 0x10 /* Loop sending */
249#define CLK2MIS 0x40 /* Two clocks missing */
250#define CLK1MIS 0x80 /* One clock missing */
251
252/* Read Register 12 (lower byte of baud rate generator constant) */
253
254/* Read Register 13 (upper byte of baud rate generator constant) */
255
256/* Read Register 15 (value of WR 15) */
257
258/* Misc macros */
259#define ZS_CLEARERR(channel) do { sbus_writeb(ERR_RES, &channel->control); \
260 udelay(5); } while(0)
261
262#define ZS_CLEARSTAT(channel) do { sbus_writeb(RES_EXT_INT, &channel->control); \
263 udelay(5); } while(0)
264
265#define ZS_CLEARFIFO(channel) do { sbus_readb(&channel->data); \
266 udelay(2); \
267 sbus_readb(&channel->data); \
268 udelay(2); \
269 sbus_readb(&channel->data); \
270 udelay(2); } while(0)
271
272#endif /* _SUNZILOG_H */
diff --git a/drivers/serial/uart00.c b/drivers/serial/uart00.c
new file mode 100644
index 000000000000..186f1300cead
--- /dev/null
+++ b/drivers/serial/uart00.c
@@ -0,0 +1,782 @@
1/*
2 * linux/drivers/serial/uart00.c
3 *
4 * Driver for UART00 serial ports
5 *
6 * Based on drivers/char/serial_amba.c, by ARM Limited &
7 * Deep Blue Solutions Ltd.
8 * Copyright 2001 Altera Corporation
9 *
10 * Update for 2.6.4 by Dirk Behme <dirk.behme@de.bosch.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * $Id: uart00.c,v 1.35 2002/07/28 10:03:28 rmk Exp $
27 *
28 */
29#include <linux/config.h>
30
31#if defined(CONFIG_SERIAL_UART00_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
32#define SUPPORT_SYSRQ
33#endif
34
35#include <linux/module.h>
36#include <linux/ioport.h>
37#include <linux/init.h>
38#include <linux/console.h>
39#include <linux/sysrq.h>
40#include <linux/tty.h>
41#include <linux/tty_flip.h>
42#include <linux/serial_core.h>
43#include <linux/serial.h>
44
45#include <asm/io.h>
46#include <asm/irq.h>
47#include <asm/sizes.h>
48
49#include <asm/arch/excalibur.h>
50#define UART00_TYPE (volatile unsigned int*)
51#include <asm/arch/uart00.h>
52#include <asm/arch/int_ctrl00.h>
53
54#define UART_NR 2
55
56#define SERIAL_UART00_NAME "ttyUA"
57#define SERIAL_UART00_MAJOR 204
58#define SERIAL_UART00_MINOR 16 /* Temporary - will change in future */
59#define SERIAL_UART00_NR UART_NR
60#define UART_PORT_SIZE 0x50
61
62#define UART00_ISR_PASS_LIMIT 256
63
64/*
65 * Access macros for the UART00 UARTs
66 */
67#define UART_GET_INT_STATUS(p) inl(UART_ISR((p)->membase))
68#define UART_PUT_IES(p, c) outl(c,UART_IES((p)->membase))
69#define UART_GET_IES(p) inl(UART_IES((p)->membase))
70#define UART_PUT_IEC(p, c) outl(c,UART_IEC((p)->membase))
71#define UART_GET_IEC(p) inl(UART_IEC((p)->membase))
72#define UART_PUT_CHAR(p, c) outl(c,UART_TD((p)->membase))
73#define UART_GET_CHAR(p) inl(UART_RD((p)->membase))
74#define UART_GET_RSR(p) inl(UART_RSR((p)->membase))
75#define UART_GET_RDS(p) inl(UART_RDS((p)->membase))
76#define UART_GET_MSR(p) inl(UART_MSR((p)->membase))
77#define UART_GET_MCR(p) inl(UART_MCR((p)->membase))
78#define UART_PUT_MCR(p, c) outl(c,UART_MCR((p)->membase))
79#define UART_GET_MC(p) inl(UART_MC((p)->membase))
80#define UART_PUT_MC(p, c) outl(c,UART_MC((p)->membase))
81#define UART_GET_TSR(p) inl(UART_TSR((p)->membase))
82#define UART_GET_DIV_HI(p) inl(UART_DIV_HI((p)->membase))
83#define UART_PUT_DIV_HI(p,c) outl(c,UART_DIV_HI((p)->membase))
84#define UART_GET_DIV_LO(p) inl(UART_DIV_LO((p)->membase))
85#define UART_PUT_DIV_LO(p,c) outl(c,UART_DIV_LO((p)->membase))
86#define UART_RX_DATA(s) ((s) & UART_RSR_RX_LEVEL_MSK)
87#define UART_TX_READY(s) (((s) & UART_TSR_TX_LEVEL_MSK) < 15)
88//#define UART_TX_EMPTY(p) ((UART_GET_FR(p) & UART00_UARTFR_TMSK) == 0)
89
90static void uart00_stop_tx(struct uart_port *port, unsigned int tty_stop)
91{
92 UART_PUT_IEC(port, UART_IEC_TIE_MSK);
93}
94
95static void uart00_stop_rx(struct uart_port *port)
96{
97 UART_PUT_IEC(port, UART_IEC_RE_MSK);
98}
99
100static void uart00_enable_ms(struct uart_port *port)
101{
102 UART_PUT_IES(port, UART_IES_ME_MSK);
103}
104
105static void
106uart00_rx_chars(struct uart_port *port, struct pt_regs *regs)
107{
108 struct tty_struct *tty = port->info->tty;
109 unsigned int status, ch, rds, flg, ignored = 0;
110
111 status = UART_GET_RSR(port);
112 while (UART_RX_DATA(status)) {
113 /*
114 * We need to read rds before reading the
115 * character from the fifo
116 */
117 rds = UART_GET_RDS(port);
118 ch = UART_GET_CHAR(port);
119 port->icount.rx++;
120
121 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
122 goto ignore_char;
123
124 flg = TTY_NORMAL;
125
126 /*
127 * Note that the error handling code is
128 * out of the main execution path
129 */
130 if (rds & (UART_RDS_BI_MSK |UART_RDS_FE_MSK|
131 UART_RDS_PE_MSK |UART_RDS_PE_MSK))
132 goto handle_error;
133 if (uart_handle_sysrq_char(port, ch, regs))
134 goto ignore_char;
135
136 error_return:
137 tty_insert_flip_char(tty, ch, flg);
138
139 ignore_char:
140 status = UART_GET_RSR(port);
141 }
142 out:
143 tty_flip_buffer_push(tty);
144 return;
145
146 handle_error:
147 if (rds & UART_RDS_BI_MSK) {
148 status &= ~(UART_RDS_FE_MSK | UART_RDS_PE_MSK);
149 port->icount.brk++;
150 if (uart_handle_break(port))
151 goto ignore_char;
152 } else if (rds & UART_RDS_PE_MSK)
153 port->icount.parity++;
154 else if (rds & UART_RDS_FE_MSK)
155 port->icount.frame++;
156 if (rds & UART_RDS_OE_MSK)
157 port->icount.overrun++;
158
159 if (rds & port->ignore_status_mask) {
160 if (++ignored > 100)
161 goto out;
162 goto ignore_char;
163 }
164 rds &= port->read_status_mask;
165
166 if (rds & UART_RDS_BI_MSK)
167 flg = TTY_BREAK;
168 else if (rds & UART_RDS_PE_MSK)
169 flg = TTY_PARITY;
170 else if (rds & UART_RDS_FE_MSK)
171 flg = TTY_FRAME;
172
173 if (rds & UART_RDS_OE_MSK) {
174 /*
175 * CHECK: does overrun affect the current character?
176 * ASSUMPTION: it does not.
177 */
178 tty_insert_flip_char(tty, ch, flg);
179 ch = 0;
180 flg = TTY_OVERRUN;
181 }
182#ifdef SUPPORT_SYSRQ
183 port->sysrq = 0;
184#endif
185 goto error_return;
186}
187
188static void uart00_tx_chars(struct uart_port *port)
189{
190 struct circ_buf *xmit = &port->info->xmit;
191 int count;
192
193 if (port->x_char) {
194 while ((UART_GET_TSR(port) & UART_TSR_TX_LEVEL_MSK) == 15)
195 barrier();
196 UART_PUT_CHAR(port, port->x_char);
197 port->icount.tx++;
198 port->x_char = 0;
199 return;
200 }
201 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
202 uart00_stop_tx(port, 0);
203 return;
204 }
205
206 count = port->fifosize >> 1;
207 do {
208 while ((UART_GET_TSR(port) & UART_TSR_TX_LEVEL_MSK) == 15)
209 barrier();
210 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
211 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
212 port->icount.tx++;
213 if (uart_circ_empty(xmit))
214 break;
215 } while (--count > 0);
216
217 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
218 uart_write_wakeup(port);
219
220 if (uart_circ_empty(xmit))
221 uart00_stop_tx(port, 0);
222}
223
224static void uart00_start_tx(struct uart_port *port, unsigned int tty_start)
225{
226 UART_PUT_IES(port, UART_IES_TIE_MSK);
227 uart00_tx_chars(port);
228}
229
230static void uart00_modem_status(struct uart_port *port)
231{
232 unsigned int status;
233
234 status = UART_GET_MSR(port);
235
236 if (!(status & (UART_MSR_DCTS_MSK | UART_MSR_DDSR_MSK |
237 UART_MSR_TERI_MSK | UART_MSR_DDCD_MSK)))
238 return;
239
240 if (status & UART_MSR_DDCD_MSK)
241 uart_handle_dcd_change(port, status & UART_MSR_DCD_MSK);
242
243 if (status & UART_MSR_DDSR_MSK)
244 port->icount.dsr++;
245
246 if (status & UART_MSR_DCTS_MSK)
247 uart_handle_cts_change(port, status & UART_MSR_CTS_MSK);
248
249 wake_up_interruptible(&port->info->delta_msr_wait);
250}
251
252static irqreturn_t uart00_int(int irq, void *dev_id, struct pt_regs *regs)
253{
254 struct uart_port *port = dev_id;
255 unsigned int status, pass_counter = 0;
256
257 status = UART_GET_INT_STATUS(port);
258 do {
259 if (status & UART_ISR_RI_MSK)
260 uart00_rx_chars(port, regs);
261 if (status & UART_ISR_MI_MSK)
262 uart00_modem_status(port);
263 if (status & (UART_ISR_TI_MSK | UART_ISR_TII_MSK))
264 uart00_tx_chars(port);
265 if (pass_counter++ > UART00_ISR_PASS_LIMIT)
266 break;
267
268 status = UART_GET_INT_STATUS(port);
269 } while (status);
270
271 return IRQ_HANDLED;
272}
273
274static unsigned int uart00_tx_empty(struct uart_port *port)
275{
276 return UART_GET_TSR(port) & UART_TSR_TX_LEVEL_MSK? 0 : TIOCSER_TEMT;
277}
278
279static unsigned int uart00_get_mctrl(struct uart_port *port)
280{
281 unsigned int result = 0;
282 unsigned int status;
283
284 status = UART_GET_MSR(port);
285 if (status & UART_MSR_DCD_MSK)
286 result |= TIOCM_CAR;
287 if (status & UART_MSR_DSR_MSK)
288 result |= TIOCM_DSR;
289 if (status & UART_MSR_CTS_MSK)
290 result |= TIOCM_CTS;
291 if (status & UART_MSR_RI_MSK)
292 result |= TIOCM_RI;
293
294 return result;
295}
296
297static void uart00_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
298{
299}
300
301static void uart00_break_ctl(struct uart_port *port, int break_state)
302{
303 unsigned long flags;
304 unsigned int mcr;
305
306 spin_lock_irqsave(&port->lock, flags);
307 mcr = UART_GET_MCR(port);
308 if (break_state == -1)
309 mcr |= UART_MCR_BR_MSK;
310 else
311 mcr &= ~UART_MCR_BR_MSK;
312 UART_PUT_MCR(port, mcr);
313 spin_unlock_irqrestore(&port->lock, flags);
314}
315
316static void
317uart00_set_termios(struct uart_port *port, struct termios *termios,
318 struct termios *old)
319{
320 unsigned int uart_mc, old_ies, baud, quot;
321 unsigned long flags;
322
323 /*
324 * We don't support CREAD (yet)
325 */
326 termios->c_cflag |= CREAD;
327
328 /*
329 * Ask the core to calculate the divisor for us.
330 */
331 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
332 quot = uart_get_divisor(port, baud);
333
334 /* byte size and parity */
335 switch (termios->c_cflag & CSIZE) {
336 case CS5:
337 uart_mc = UART_MC_CLS_CHARLEN_5;
338 break;
339 case CS6:
340 uart_mc = UART_MC_CLS_CHARLEN_6;
341 break;
342 case CS7:
343 uart_mc = UART_MC_CLS_CHARLEN_7;
344 break;
345 default: // CS8
346 uart_mc = UART_MC_CLS_CHARLEN_8;
347 break;
348 }
349 if (termios->c_cflag & CSTOPB)
350 uart_mc|= UART_MC_ST_TWO;
351 if (termios->c_cflag & PARENB) {
352 uart_mc |= UART_MC_PE_MSK;
353 if (!(termios->c_cflag & PARODD))
354 uart_mc |= UART_MC_EP_MSK;
355 }
356
357 spin_lock_irqsave(&port->lock, flags);
358
359 /*
360 * Update the per-port timeout.
361 */
362 uart_update_timeout(port, termios->c_cflag, baud);
363
364 port->read_status_mask = UART_RDS_OE_MSK;
365 if (termios->c_iflag & INPCK)
366 port->read_status_mask |= UART_RDS_FE_MSK | UART_RDS_PE_MSK;
367 if (termios->c_iflag & (BRKINT | PARMRK))
368 port->read_status_mask |= UART_RDS_BI_MSK;
369
370 /*
371 * Characters to ignore
372 */
373 port->ignore_status_mask = 0;
374 if (termios->c_iflag & IGNPAR)
375 port->ignore_status_mask |= UART_RDS_FE_MSK | UART_RDS_PE_MSK;
376 if (termios->c_iflag & IGNBRK) {
377 port->ignore_status_mask |= UART_RDS_BI_MSK;
378 /*
379 * If we're ignoring parity and break indicators,
380 * ignore overruns to (for real raw support).
381 */
382 if (termios->c_iflag & IGNPAR)
383 port->ignore_status_mask |= UART_RDS_OE_MSK;
384 }
385
386 /* first, disable everything */
387 old_ies = UART_GET_IES(port);
388
389 if (UART_ENABLE_MS(port, termios->c_cflag))
390 old_ies |= UART_IES_ME_MSK;
391
392 /* Set baud rate */
393 UART_PUT_DIV_LO(port, (quot & 0xff));
394 UART_PUT_DIV_HI(port, ((quot & 0xf00) >> 8));
395
396 UART_PUT_MC(port, uart_mc);
397 UART_PUT_IES(port, old_ies);
398
399 spin_unlock_irqrestore(&port->lock, flags);
400}
401
402static int uart00_startup(struct uart_port *port)
403{
404 int result;
405
406 /*
407 * Allocate the IRQ
408 */
409 result = request_irq(port->irq, uart00_int, 0, "uart00", port);
410 if (result) {
411 printk(KERN_ERR "Request of irq %d failed\n", port->irq);
412 return result;
413 }
414
415 /*
416 * Finally, enable interrupts. Use the TII interrupt to minimise
417 * the number of interrupts generated. If higher performance is
418 * needed, consider using the TI interrupt with a suitable FIFO
419 * threshold
420 */
421 UART_PUT_IES(port, UART_IES_RE_MSK | UART_IES_TIE_MSK);
422
423 return 0;
424}
425
426static void uart00_shutdown(struct uart_port *port)
427{
428 /*
429 * disable all interrupts, disable the port
430 */
431 UART_PUT_IEC(port, 0xff);
432
433 /* disable break condition and fifos */
434 UART_PUT_MCR(port, UART_GET_MCR(port) &~UART_MCR_BR_MSK);
435
436 /*
437 * Free the interrupt
438 */
439 free_irq(port->irq, port);
440}
441
442static const char *uart00_type(struct uart_port *port)
443{
444 return port->type == PORT_UART00 ? "Altera UART00" : NULL;
445}
446
447/*
448 * Release the memory region(s) being used by 'port'
449 */
450static void uart00_release_port(struct uart_port *port)
451{
452 release_mem_region(port->mapbase, UART_PORT_SIZE);
453
454#ifdef CONFIG_ARCH_CAMELOT
455 if (port->membase != (void*)IO_ADDRESS(EXC_UART00_BASE)) {
456 iounmap(port->membase);
457 }
458#endif
459}
460
461/*
462 * Request the memory region(s) being used by 'port'
463 */
464static int uart00_request_port(struct uart_port *port)
465{
466 return request_mem_region(port->mapbase, UART_PORT_SIZE, "serial_uart00")
467 != NULL ? 0 : -EBUSY;
468}
469
470/*
471 * Configure/autoconfigure the port.
472 */
473static void uart00_config_port(struct uart_port *port, int flags)
474{
475
476 /*
477 * Map the io memory if this is a soft uart
478 */
479 if (!port->membase)
480 port->membase = ioremap_nocache(port->mapbase,SZ_4K);
481
482 if (!port->membase)
483 printk(KERN_ERR "serial00: cannot map io memory\n");
484 else
485 port->type = PORT_UART00;
486
487}
488
489/*
490 * verify the new serial_struct (for TIOCSSERIAL).
491 */
492static int uart00_verify_port(struct uart_port *port, struct serial_struct *ser)
493{
494 int ret = 0;
495 if (ser->type != PORT_UNKNOWN && ser->type != PORT_UART00)
496 ret = -EINVAL;
497 if (ser->irq < 0 || ser->irq >= NR_IRQS)
498 ret = -EINVAL;
499 if (ser->baud_base < 9600)
500 ret = -EINVAL;
501 return ret;
502}
503
504static struct uart_ops uart00_pops = {
505 .tx_empty = uart00_tx_empty,
506 .set_mctrl = uart00_set_mctrl_null,
507 .get_mctrl = uart00_get_mctrl,
508 .stop_tx = uart00_stop_tx,
509 .start_tx = uart00_start_tx,
510 .stop_rx = uart00_stop_rx,
511 .enable_ms = uart00_enable_ms,
512 .break_ctl = uart00_break_ctl,
513 .startup = uart00_startup,
514 .shutdown = uart00_shutdown,
515 .set_termios = uart00_set_termios,
516 .type = uart00_type,
517 .release_port = uart00_release_port,
518 .request_port = uart00_request_port,
519 .config_port = uart00_config_port,
520 .verify_port = uart00_verify_port,
521};
522
523
524#ifdef CONFIG_ARCH_CAMELOT
525static struct uart_port epxa10db_port = {
526 .membase = (void*)IO_ADDRESS(EXC_UART00_BASE),
527 .mapbase = EXC_UART00_BASE,
528 .iotype = SERIAL_IO_MEM,
529 .irq = IRQ_UART,
530 .uartclk = EXC_AHB2_CLK_FREQUENCY,
531 .fifosize = 16,
532 .ops = &uart00_pops,
533 .flags = ASYNC_BOOT_AUTOCONF,
534};
535#endif
536
537
538#ifdef CONFIG_SERIAL_UART00_CONSOLE
539static void uart00_console_write(struct console *co, const char *s, unsigned count)
540{
541#ifdef CONFIG_ARCH_CAMELOT
542 struct uart_port *port = &epxa10db_port;
543 unsigned int status, old_ies;
544 int i;
545
546 /*
547 * First save the CR then disable the interrupts
548 */
549 old_ies = UART_GET_IES(port);
550 UART_PUT_IEC(port,0xff);
551
552 /*
553 * Now, do each character
554 */
555 for (i = 0; i < count; i++) {
556 do {
557 status = UART_GET_TSR(port);
558 } while (!UART_TX_READY(status));
559 UART_PUT_CHAR(port, s[i]);
560 if (s[i] == '\n') {
561 do {
562 status = UART_GET_TSR(port);
563 } while (!UART_TX_READY(status));
564 UART_PUT_CHAR(port, '\r');
565 }
566 }
567
568 /*
569 * Finally, wait for transmitter to become empty
570 * and restore the IES
571 */
572 do {
573 status = UART_GET_TSR(port);
574 } while (status & UART_TSR_TX_LEVEL_MSK);
575 UART_PUT_IES(port, old_ies);
576#endif
577}
578
579static void __init
580uart00_console_get_options(struct uart_port *port, int *baud,
581 int *parity, int *bits)
582{
583 unsigned int uart_mc, quot;
584
585 uart_mc = UART_GET_MC(port);
586
587 *parity = 'n';
588 if (uart_mc & UART_MC_PE_MSK) {
589 if (uart_mc & UART_MC_EP_MSK)
590 *parity = 'e';
591 else
592 *parity = 'o';
593 }
594
595 switch (uart_mc & UART_MC_CLS_MSK) {
596 case UART_MC_CLS_CHARLEN_5:
597 *bits = 5;
598 break;
599 case UART_MC_CLS_CHARLEN_6:
600 *bits = 6;
601 break;
602 case UART_MC_CLS_CHARLEN_7:
603 *bits = 7;
604 break;
605 case UART_MC_CLS_CHARLEN_8:
606 *bits = 8;
607 break;
608 }
609 quot = UART_GET_DIV_LO(port) | (UART_GET_DIV_HI(port) << 8);
610 *baud = port->uartclk / (16 *quot );
611}
612
613static int __init uart00_console_setup(struct console *co, char *options)
614{
615 struct uart_port *port;
616 int baud = 115200;
617 int bits = 8;
618 int parity = 'n';
619 int flow = 'n';
620
621#ifdef CONFIG_ARCH_CAMELOT
622 port = &epxa10db_port; ;
623#else
624 return -ENODEV;
625#endif
626 if (options)
627 uart_parse_options(options, &baud, &parity, &bits, &flow);
628 else
629 uart00_console_get_options(port, &baud, &parity, &bits);
630
631 return uart_set_options(port, co, baud, parity, bits, flow);
632}
633
634extern struct uart_driver uart00_reg;
635static struct console uart00_console = {
636 .name = SERIAL_UART00_NAME,
637 .write = uart00_console_write,
638 .device = uart_console_device,
639 .setup = uart00_console_setup,
640 .flags = CON_PRINTBUFFER,
641 .index = 0,
642 .data = &uart00_reg,
643};
644
645static int __init uart00_console_init(void)
646{
647 register_console(&uart00_console);
648 return 0;
649}
650console_initcall(uart00_console_init);
651
652#define UART00_CONSOLE &uart00_console
653#else
654#define UART00_CONSOLE NULL
655#endif
656
657static struct uart_driver uart00_reg = {
658 .owner = NULL,
659 .driver_name = SERIAL_UART00_NAME,
660 .dev_name = SERIAL_UART00_NAME,
661 .major = SERIAL_UART00_MAJOR,
662 .minor = SERIAL_UART00_MINOR,
663 .nr = UART_NR,
664 .cons = UART00_CONSOLE,
665};
666
667struct dev_port_entry{
668 unsigned int base_addr;
669 struct uart_port *port;
670};
671
672#ifdef CONFIG_PLD_HOTSWAP
673
674static struct dev_port_entry dev_port_map[UART_NR];
675
676/*
677 * Keep a mapping of dev_info addresses -> port lines to use when
678 * removing ports dev==NULL indicates unused entry
679 */
680
681struct uart00_ps_data{
682 unsigned int clk;
683 unsigned int fifosize;
684};
685
686int uart00_add_device(struct pldhs_dev_info* dev_info, void* dev_ps_data)
687{
688 struct uart00_ps_data* dev_ps=dev_ps_data;
689 struct uart_port * port;
690 int i,result;
691
692 i=0;
693 while(dev_port_map[i].port)
694 i++;
695
696 if(i==UART_NR){
697 printk(KERN_WARNING "uart00: Maximum number of ports reached\n");
698 return 0;
699 }
700
701 port=kmalloc(sizeof(struct uart_port),GFP_KERNEL);
702 if(!port)
703 return -ENOMEM;
704
705 printk("clk=%d fifo=%d\n",dev_ps->clk,dev_ps->fifosize);
706 port->membase=0;
707 port->mapbase=dev_info->base_addr;
708 port->iotype=SERIAL_IO_MEM;
709 port->irq=dev_info->irq;
710 port->uartclk=dev_ps->clk;
711 port->fifosize=dev_ps->fifosize;
712 port->ops=&uart00_pops;
713 port->line=i;
714 port->flags=ASYNC_BOOT_AUTOCONF;
715
716 result=uart_add_one_port(&uart00_reg, port);
717 if(result){
718 printk("uart_add_one_port returned %d\n",result);
719 return result;
720 }
721 dev_port_map[i].base_addr=dev_info->base_addr;
722 dev_port_map[i].port=port;
723 printk("uart00: added device at %x as ttyUA%d\n",dev_port_map[i].base_addr,i);
724 return 0;
725
726}
727
728int uart00_remove_devices(void)
729{
730 int i,result;
731
732
733 result=0;
734 for(i=1;i<UART_NR;i++){
735 if(dev_port_map[i].base_addr){
736 result=uart_remove_one_port(&uart00_reg, dev_port_map[i].port);
737 if(result)
738 return result;
739
740 /* port removed sucessfully, so now tidy up */
741 kfree(dev_port_map[i].port);
742 dev_port_map[i].base_addr=0;
743 dev_port_map[i].port=NULL;
744 }
745 }
746 return 0;
747
748}
749
750struct pld_hotswap_ops uart00_pldhs_ops={
751 .name = "uart00",
752 .add_device = uart00_add_device,
753 .remove_devices = uart00_remove_devices,
754};
755
756#endif
757
758static int __init uart00_init(void)
759{
760 int result;
761
762 printk(KERN_INFO "Serial: UART00 driver $Revision: 1.35 $\n");
763
764 printk(KERN_WARNING "serial_uart00:Using temporary major/minor pairs"
765 " - these WILL change in the future\n");
766
767 result = uart_register_driver(&uart00_reg);
768 if (result)
769 return result;
770#ifdef CONFIG_ARCH_CAMELOT
771 result = uart_add_one_port(&uart00_reg,&epxa10db_port);
772#endif
773 if (result)
774 uart_unregister_driver(&uart00_reg);
775
776#ifdef CONFIG_PLD_HOTSWAP
777 pldhs_register_driver(&uart00_pldhs_ops);
778#endif
779 return result;
780}
781
782__initcall(uart00_init);
diff --git a/drivers/serial/v850e_uart.c b/drivers/serial/v850e_uart.c
new file mode 100644
index 000000000000..bb482780a41d
--- /dev/null
+++ b/drivers/serial/v850e_uart.c
@@ -0,0 +1,549 @@
1/*
2 * drivers/serial/v850e_uart.c -- Serial I/O using V850E on-chip UART or UARTB
3 *
4 * Copyright (C) 2001,02,03 NEC Electronics Corporation
5 * Copyright (C) 2001,02,03 Miles Bader <miles@gnu.org>
6 *
7 * This file is subject to the terms and conditions of the GNU General
8 * Public License. See the file COPYING in the main directory of this
9 * archive for more details.
10 *
11 * Written by Miles Bader <miles@gnu.org>
12 */
13
14/* This driver supports both the original V850E UART interface (called
15 merely `UART' in the docs) and the newer `UARTB' interface, which is
16 roughly a superset of the first one. The selection is made at
17 configure time -- if CONFIG_V850E_UARTB is defined, then UARTB is
18 presumed, otherwise the old UART -- as these are on-CPU UARTS, a system
19 can never have both.
20
21 The UARTB interface also has a 16-entry FIFO mode, which is not
22 yet supported by this driver. */
23
24#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/console.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial.h>
31#include <linux/serial_core.h>
32
33#include <asm/v850e_uart.h>
34
35/* Initial UART state. This may be overridden by machine-dependent headers. */
36#ifndef V850E_UART_INIT_BAUD
37#define V850E_UART_INIT_BAUD 115200
38#endif
39#ifndef V850E_UART_INIT_CFLAGS
40#define V850E_UART_INIT_CFLAGS (B115200 | CS8 | CREAD)
41#endif
42
43/* A string used for prefixing printed descriptions; since the same UART
44 macro is actually used on other chips than the V850E. This must be a
45 constant string. */
46#ifndef V850E_UART_CHIP_NAME
47#define V850E_UART_CHIP_NAME "V850E"
48#endif
49
50#define V850E_UART_MINOR_BASE 64 /* First tty minor number */
51
52
53/* Low-level UART functions. */
54
55/* Configure and turn on uart channel CHAN, using the termios `control
56 modes' bits in CFLAGS, and a baud-rate of BAUD. */
57void v850e_uart_configure (unsigned chan, unsigned cflags, unsigned baud)
58{
59 int flags;
60 v850e_uart_speed_t old_speed;
61 v850e_uart_config_t old_config;
62 v850e_uart_speed_t new_speed = v850e_uart_calc_speed (baud);
63 v850e_uart_config_t new_config = v850e_uart_calc_config (cflags);
64
65 /* Disable interrupts while we're twiddling the hardware. */
66 local_irq_save (flags);
67
68#ifdef V850E_UART_PRE_CONFIGURE
69 V850E_UART_PRE_CONFIGURE (chan, cflags, baud);
70#endif
71
72 old_config = V850E_UART_CONFIG (chan);
73 old_speed = v850e_uart_speed (chan);
74
75 if (! v850e_uart_speed_eq (old_speed, new_speed)) {
76 /* The baud rate has changed. First, disable the UART. */
77 V850E_UART_CONFIG (chan) = V850E_UART_CONFIG_FINI;
78 old_config = 0; /* Force the uart to be re-initialized. */
79
80 /* Reprogram the baud-rate generator. */
81 v850e_uart_set_speed (chan, new_speed);
82 }
83
84 if (! (old_config & V850E_UART_CONFIG_ENABLED)) {
85 /* If we are using the uart for the first time, start by
86 enabling it, which must be done before turning on any
87 other bits. */
88 V850E_UART_CONFIG (chan) = V850E_UART_CONFIG_INIT;
89 /* See the initial state. */
90 old_config = V850E_UART_CONFIG (chan);
91 }
92
93 if (new_config != old_config) {
94 /* Which of the TXE/RXE bits we'll temporarily turn off
95 before changing other control bits. */
96 unsigned temp_disable = 0;
97 /* Which of the TXE/RXE bits will be enabled. */
98 unsigned enable = 0;
99 unsigned changed_bits = new_config ^ old_config;
100
101 /* Which of RX/TX will be enabled in the new configuration. */
102 if (new_config & V850E_UART_CONFIG_RX_BITS)
103 enable |= (new_config & V850E_UART_CONFIG_RX_ENABLE);
104 if (new_config & V850E_UART_CONFIG_TX_BITS)
105 enable |= (new_config & V850E_UART_CONFIG_TX_ENABLE);
106
107 /* Figure out which of RX/TX needs to be disabled; note
108 that this will only happen if they're not already
109 disabled. */
110 if (changed_bits & V850E_UART_CONFIG_RX_BITS)
111 temp_disable
112 |= (old_config & V850E_UART_CONFIG_RX_ENABLE);
113 if (changed_bits & V850E_UART_CONFIG_TX_BITS)
114 temp_disable
115 |= (old_config & V850E_UART_CONFIG_TX_ENABLE);
116
117 /* We have to turn off RX and/or TX mode before changing
118 any associated control bits. */
119 if (temp_disable)
120 V850E_UART_CONFIG (chan) = old_config & ~temp_disable;
121
122 /* Write the new control bits, while RX/TX are disabled. */
123 if (changed_bits & ~enable)
124 V850E_UART_CONFIG (chan) = new_config & ~enable;
125
126 v850e_uart_config_delay (new_config, new_speed);
127
128 /* Write the final version, with enable bits turned on. */
129 V850E_UART_CONFIG (chan) = new_config;
130 }
131
132 local_irq_restore (flags);
133}
134
135
136/* Low-level console. */
137
138#ifdef CONFIG_V850E_UART_CONSOLE
139
140static void v850e_uart_cons_write (struct console *co,
141 const char *s, unsigned count)
142{
143 if (count > 0) {
144 unsigned chan = co->index;
145 unsigned irq = V850E_UART_TX_IRQ (chan);
146 int irq_was_enabled, irq_was_pending, flags;
147
148 /* We don't want to get `transmission completed'
149 interrupts, since we're busy-waiting, so we disable them
150 while sending (we don't disable interrupts entirely
151 because sending over a serial line is really slow). We
152 save the status of the tx interrupt and restore it when
153 we're done so that using printk doesn't interfere with
154 normal serial transmission (other than interleaving the
155 output, of course!). This should work correctly even if
156 this function is interrupted and the interrupt printks
157 something. */
158
159 /* Disable interrupts while fiddling with tx interrupt. */
160 local_irq_save (flags);
161 /* Get current tx interrupt status. */
162 irq_was_enabled = v850e_intc_irq_enabled (irq);
163 irq_was_pending = v850e_intc_irq_pending (irq);
164 /* Disable tx interrupt if necessary. */
165 if (irq_was_enabled)
166 v850e_intc_disable_irq (irq);
167 /* Turn interrupts back on. */
168 local_irq_restore (flags);
169
170 /* Send characters. */
171 while (count > 0) {
172 int ch = *s++;
173
174 if (ch == '\n') {
175 /* We don't have the benefit of a tty
176 driver, so translate NL into CR LF. */
177 v850e_uart_wait_for_xmit_ok (chan);
178 v850e_uart_putc (chan, '\r');
179 }
180
181 v850e_uart_wait_for_xmit_ok (chan);
182 v850e_uart_putc (chan, ch);
183
184 count--;
185 }
186
187 /* Restore saved tx interrupt status. */
188 if (irq_was_enabled) {
189 /* Wait for the last character we sent to be
190 completely transmitted (as we'll get an
191 interrupt interrupt at that point). */
192 v850e_uart_wait_for_xmit_done (chan);
193 /* Clear pending interrupts received due
194 to our transmission, unless there was already
195 one pending, in which case we want the
196 handler to be called. */
197 if (! irq_was_pending)
198 v850e_intc_clear_pending_irq (irq);
199 /* ... and then turn back on handling. */
200 v850e_intc_enable_irq (irq);
201 }
202 }
203}
204
205extern struct uart_driver v850e_uart_driver;
206static struct console v850e_uart_cons =
207{
208 .name = "ttyS",
209 .write = v850e_uart_cons_write,
210 .device = uart_console_device,
211 .flags = CON_PRINTBUFFER,
212 .cflag = V850E_UART_INIT_CFLAGS,
213 .index = -1,
214 .data = &v850e_uart_driver,
215};
216
217void v850e_uart_cons_init (unsigned chan)
218{
219 v850e_uart_configure (chan, V850E_UART_INIT_CFLAGS,
220 V850E_UART_INIT_BAUD);
221 v850e_uart_cons.index = chan;
222 register_console (&v850e_uart_cons);
223 printk ("Console: %s on-chip UART channel %d\n",
224 V850E_UART_CHIP_NAME, chan);
225}
226
227/* This is what the init code actually calls. */
228static int v850e_uart_console_init (void)
229{
230 v850e_uart_cons_init (V850E_UART_CONSOLE_CHANNEL);
231 return 0;
232}
233console_initcall(v850e_uart_console_init);
234
235#define V850E_UART_CONSOLE &v850e_uart_cons
236
237#else /* !CONFIG_V850E_UART_CONSOLE */
238#define V850E_UART_CONSOLE 0
239#endif /* CONFIG_V850E_UART_CONSOLE */
240
241/* TX/RX interrupt handlers. */
242
243static void v850e_uart_stop_tx (struct uart_port *port, unsigned tty_stop);
244
245void v850e_uart_tx (struct uart_port *port)
246{
247 struct circ_buf *xmit = &port->info->xmit;
248 int stopped = uart_tx_stopped (port);
249
250 if (v850e_uart_xmit_ok (port->line)) {
251 int tx_ch;
252
253 if (port->x_char) {
254 tx_ch = port->x_char;
255 port->x_char = 0;
256 } else if (!uart_circ_empty (xmit) && !stopped) {
257 tx_ch = xmit->buf[xmit->tail];
258 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
259 } else
260 goto no_xmit;
261
262 v850e_uart_putc (port->line, tx_ch);
263 port->icount.tx++;
264
265 if (uart_circ_chars_pending (xmit) < WAKEUP_CHARS)
266 uart_write_wakeup (port);
267 }
268
269 no_xmit:
270 if (uart_circ_empty (xmit) || stopped)
271 v850e_uart_stop_tx (port, stopped);
272}
273
274static irqreturn_t v850e_uart_tx_irq(int irq, void *data, struct pt_regs *regs)
275{
276 struct uart_port *port = data;
277 v850e_uart_tx (port);
278 return IRQ_HANDLED;
279}
280
281static irqreturn_t v850e_uart_rx_irq(int irq, void *data, struct pt_regs *regs)
282{
283 struct uart_port *port = data;
284 unsigned ch_stat = TTY_NORMAL;
285 unsigned ch = v850e_uart_getc (port->line);
286 unsigned err = v850e_uart_err (port->line);
287
288 if (err) {
289 if (err & V850E_UART_ERR_OVERRUN) {
290 ch_stat = TTY_OVERRUN;
291 port->icount.overrun++;
292 } else if (err & V850E_UART_ERR_FRAME) {
293 ch_stat = TTY_FRAME;
294 port->icount.frame++;
295 } else if (err & V850E_UART_ERR_PARITY) {
296 ch_stat = TTY_PARITY;
297 port->icount.parity++;
298 }
299 }
300
301 port->icount.rx++;
302
303 tty_insert_flip_char (port->info->tty, ch, ch_stat);
304 tty_schedule_flip (port->info->tty);
305
306 return IRQ_HANDLED;
307}
308
309
310/* Control functions for the serial framework. */
311
312static void v850e_uart_nop (struct uart_port *port) { }
313static int v850e_uart_success (struct uart_port *port) { return 0; }
314
315static unsigned v850e_uart_tx_empty (struct uart_port *port)
316{
317 return TIOCSER_TEMT; /* Can't detect. */
318}
319
320static void v850e_uart_set_mctrl (struct uart_port *port, unsigned mctrl)
321{
322#ifdef V850E_UART_SET_RTS
323 V850E_UART_SET_RTS (port->line, (mctrl & TIOCM_RTS));
324#endif
325}
326
327static unsigned v850e_uart_get_mctrl (struct uart_port *port)
328{
329 /* We don't support DCD or DSR, so consider them permanently active. */
330 int mctrl = TIOCM_CAR | TIOCM_DSR;
331
332 /* We may support CTS. */
333#ifdef V850E_UART_CTS
334 mctrl |= V850E_UART_CTS(port->line) ? TIOCM_CTS : 0;
335#else
336 mctrl |= TIOCM_CTS;
337#endif
338
339 return mctrl;
340}
341
342static void v850e_uart_start_tx (struct uart_port *port, unsigned tty_start)
343{
344 v850e_intc_disable_irq (V850E_UART_TX_IRQ (port->line));
345 v850e_uart_tx (port);
346 v850e_intc_enable_irq (V850E_UART_TX_IRQ (port->line));
347}
348
349static void v850e_uart_stop_tx (struct uart_port *port, unsigned tty_stop)
350{
351 v850e_intc_disable_irq (V850E_UART_TX_IRQ (port->line));
352}
353
354static void v850e_uart_start_rx (struct uart_port *port)
355{
356 v850e_intc_enable_irq (V850E_UART_RX_IRQ (port->line));
357}
358
359static void v850e_uart_stop_rx (struct uart_port *port)
360{
361 v850e_intc_disable_irq (V850E_UART_RX_IRQ (port->line));
362}
363
364static void v850e_uart_break_ctl (struct uart_port *port, int break_ctl)
365{
366 /* Umm, do this later. */
367}
368
369static int v850e_uart_startup (struct uart_port *port)
370{
371 int err;
372
373 /* Alloc RX irq. */
374 err = request_irq (V850E_UART_RX_IRQ (port->line), v850e_uart_rx_irq,
375 SA_INTERRUPT, "v850e_uart", port);
376 if (err)
377 return err;
378
379 /* Alloc TX irq. */
380 err = request_irq (V850E_UART_TX_IRQ (port->line), v850e_uart_tx_irq,
381 SA_INTERRUPT, "v850e_uart", port);
382 if (err) {
383 free_irq (V850E_UART_RX_IRQ (port->line), port);
384 return err;
385 }
386
387 v850e_uart_start_rx (port);
388
389 return 0;
390}
391
392static void v850e_uart_shutdown (struct uart_port *port)
393{
394 /* Disable port interrupts. */
395 free_irq (V850E_UART_TX_IRQ (port->line), port);
396 free_irq (V850E_UART_RX_IRQ (port->line), port);
397
398 /* Turn off xmit/recv enable bits. */
399 V850E_UART_CONFIG (port->line)
400 &= ~(V850E_UART_CONFIG_TX_ENABLE
401 | V850E_UART_CONFIG_RX_ENABLE);
402 /* Then reset the channel. */
403 V850E_UART_CONFIG (port->line) = 0;
404}
405
406static void
407v850e_uart_set_termios (struct uart_port *port, struct termios *termios,
408 struct termios *old)
409{
410 unsigned cflags = termios->c_cflag;
411
412 /* Restrict flags to legal values. */
413 if ((cflags & CSIZE) != CS7 && (cflags & CSIZE) != CS8)
414 /* The new value of CSIZE is invalid, use the old value. */
415 cflags = (cflags & ~CSIZE)
416 | (old ? (old->c_cflag & CSIZE) : CS8);
417
418 termios->c_cflag = cflags;
419
420 v850e_uart_configure (port->line, cflags,
421 uart_get_baud_rate (port, termios, old,
422 v850e_uart_min_baud(),
423 v850e_uart_max_baud()));
424}
425
426static const char *v850e_uart_type (struct uart_port *port)
427{
428 return port->type == PORT_V850E_UART ? "v850e_uart" : 0;
429}
430
431static void v850e_uart_config_port (struct uart_port *port, int flags)
432{
433 if (flags & UART_CONFIG_TYPE)
434 port->type = PORT_V850E_UART;
435}
436
437static int
438v850e_uart_verify_port (struct uart_port *port, struct serial_struct *ser)
439{
440 if (ser->type != PORT_UNKNOWN && ser->type != PORT_V850E_UART)
441 return -EINVAL;
442 if (ser->irq != V850E_UART_TX_IRQ (port->line))
443 return -EINVAL;
444 return 0;
445}
446
447static struct uart_ops v850e_uart_ops = {
448 .tx_empty = v850e_uart_tx_empty,
449 .get_mctrl = v850e_uart_get_mctrl,
450 .set_mctrl = v850e_uart_set_mctrl,
451 .start_tx = v850e_uart_start_tx,
452 .stop_tx = v850e_uart_stop_tx,
453 .stop_rx = v850e_uart_stop_rx,
454 .enable_ms = v850e_uart_nop,
455 .break_ctl = v850e_uart_break_ctl,
456 .startup = v850e_uart_startup,
457 .shutdown = v850e_uart_shutdown,
458 .set_termios = v850e_uart_set_termios,
459 .type = v850e_uart_type,
460 .release_port = v850e_uart_nop,
461 .request_port = v850e_uart_success,
462 .config_port = v850e_uart_config_port,
463 .verify_port = v850e_uart_verify_port,
464};
465
466/* Initialization and cleanup. */
467
468static struct uart_driver v850e_uart_driver = {
469 .owner = THIS_MODULE,
470 .driver_name = "v850e_uart",
471 .devfs_name = "tts/",
472 .dev_name = "ttyS",
473 .major = TTY_MAJOR,
474 .minor = V850E_UART_MINOR_BASE,
475 .nr = V850E_UART_NUM_CHANNELS,
476 .cons = V850E_UART_CONSOLE,
477};
478
479
480static struct uart_port v850e_uart_ports[V850E_UART_NUM_CHANNELS];
481
482static int __init v850e_uart_init (void)
483{
484 int rval;
485
486 printk (KERN_INFO "%s on-chip UART\n", V850E_UART_CHIP_NAME);
487
488 rval = uart_register_driver (&v850e_uart_driver);
489 if (rval == 0) {
490 unsigned chan;
491
492 for (chan = 0; chan < V850E_UART_NUM_CHANNELS; chan++) {
493 struct uart_port *port = &v850e_uart_ports[chan];
494
495 memset (port, 0, sizeof *port);
496
497 port->ops = &v850e_uart_ops;
498 port->line = chan;
499 port->iotype = SERIAL_IO_MEM;
500 port->flags = UPF_BOOT_AUTOCONF;
501
502 /* We actually use multiple IRQs, but the serial
503 framework seems to mainly use this for
504 informational purposes anyway. Here we use the TX
505 irq. */
506 port->irq = V850E_UART_TX_IRQ (chan);
507
508 /* The serial framework doesn't really use these
509 membase/mapbase fields for anything useful, but
510 it requires that they be something non-zero to
511 consider the port `valid', and also uses them
512 for informational purposes. */
513 port->membase = (void *)V850E_UART_BASE_ADDR (chan);
514 port->mapbase = V850E_UART_BASE_ADDR (chan);
515
516 /* The framework insists on knowing the uart's master
517 clock freq, though it doesn't seem to do anything
518 useful for us with it. We must make it at least
519 higher than (the maximum baud rate * 16), otherwise
520 the framework will puke during its internal
521 calculations, and force the baud rate to be 9600.
522 To be accurate though, just repeat the calculation
523 we use when actually setting the speed. */
524 port->uartclk = v850e_uart_max_clock() * 16;
525
526 uart_add_one_port (&v850e_uart_driver, port);
527 }
528 }
529
530 return rval;
531}
532
533static void __exit v850e_uart_exit (void)
534{
535 unsigned chan;
536
537 for (chan = 0; chan < V850E_UART_NUM_CHANNELS; chan++)
538 uart_remove_one_port (&v850e_uart_driver,
539 &v850e_uart_ports[chan]);
540
541 uart_unregister_driver (&v850e_uart_driver);
542}
543
544module_init (v850e_uart_init);
545module_exit (v850e_uart_exit);
546
547MODULE_AUTHOR ("Miles Bader");
548MODULE_DESCRIPTION ("NEC " V850E_UART_CHIP_NAME " on-chip UART");
549MODULE_LICENSE ("GPL");
diff --git a/drivers/serial/vr41xx_siu.c b/drivers/serial/vr41xx_siu.c
new file mode 100644
index 000000000000..ebc59c27a85c
--- /dev/null
+++ b/drivers/serial/vr41xx_siu.c
@@ -0,0 +1,1100 @@
1/*
2 * Driver for NEC VR4100 series Serial Interface Unit.
3 *
4 * Copyright (C) 2004-2005 Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
5 *
6 * Based on drivers/serial/8250.c, by Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <linux/config.h>
23
24#if defined(CONFIG_SERIAL_VR41XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25#define SUPPORT_SYSRQ
26#endif
27
28#include <linux/console.h>
29#include <linux/device.h>
30#include <linux/err.h>
31#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/interrupt.h>
34#include <linux/module.h>
35#include <linux/serial.h>
36#include <linux/serial_core.h>
37#include <linux/serial_reg.h>
38#include <linux/tty.h>
39#include <linux/tty_flip.h>
40
41#include <asm/io.h>
42#include <asm/vr41xx/siu.h>
43#include <asm/vr41xx/vr41xx.h>
44
45#define SIU_PORTS_MAX 2
46#define SIU_BAUD_BASE 1152000
47#define SIU_MAJOR 204
48#define SIU_MINOR_BASE 82
49
50#define RX_MAX_COUNT 256
51#define TX_MAX_COUNT 15
52
53#define SIUIRSEL 0x08
54 #define TMICMODE 0x20
55 #define TMICTX 0x10
56 #define IRMSEL 0x0c
57 #define IRMSEL_HP 0x08
58 #define IRMSEL_TEMIC 0x04
59 #define IRMSEL_SHARP 0x00
60 #define IRUSESEL 0x02
61 #define SIRSEL 0x01
62
63struct siu_port {
64 unsigned int type;
65 unsigned int irq;
66 unsigned long start;
67};
68
69static const struct siu_port siu_type1_ports[] = {
70 { .type = PORT_VR41XX_SIU,
71 .irq = SIU_IRQ,
72 .start = 0x0c000000UL, },
73};
74
75#define SIU_TYPE1_NR_PORTS (sizeof(siu_type1_ports) / sizeof(struct siu_port))
76
77static const struct siu_port siu_type2_ports[] = {
78 { .type = PORT_VR41XX_SIU,
79 .irq = SIU_IRQ,
80 .start = 0x0f000800UL, },
81 { .type = PORT_VR41XX_DSIU,
82 .irq = DSIU_IRQ,
83 .start = 0x0f000820UL, },
84};
85
86#define SIU_TYPE2_NR_PORTS (sizeof(siu_type2_ports) / sizeof(struct siu_port))
87
88static struct uart_port siu_uart_ports[SIU_PORTS_MAX];
89static uint8_t lsr_break_flag[SIU_PORTS_MAX];
90
91#define siu_read(port, offset) readb((port)->membase + (offset))
92#define siu_write(port, offset, value) writeb((value), (port)->membase + (offset))
93
94void vr41xx_select_siu_interface(siu_interface_t interface)
95{
96 struct uart_port *port;
97 unsigned long flags;
98 uint8_t irsel;
99
100 port = &siu_uart_ports[0];
101
102 spin_lock_irqsave(&port->lock, flags);
103
104 irsel = siu_read(port, SIUIRSEL);
105 if (interface == SIU_INTERFACE_IRDA)
106 irsel |= SIRSEL;
107 else
108 irsel &= ~SIRSEL;
109 siu_write(port, SIUIRSEL, irsel);
110
111 spin_unlock_irqrestore(&port->lock, flags);
112}
113
114EXPORT_SYMBOL_GPL(vr41xx_select_siu_interface);
115
116void vr41xx_use_irda(irda_use_t use)
117{
118 struct uart_port *port;
119 unsigned long flags;
120 uint8_t irsel;
121
122 port = &siu_uart_ports[0];
123
124 spin_lock_irqsave(&port->lock, flags);
125
126 irsel = siu_read(port, SIUIRSEL);
127 if (use == FIR_USE_IRDA)
128 irsel |= IRUSESEL;
129 else
130 irsel &= ~IRUSESEL;
131 siu_write(port, SIUIRSEL, irsel);
132
133 spin_unlock_irqrestore(&port->lock, flags);
134}
135
136EXPORT_SYMBOL_GPL(vr41xx_use_irda);
137
138void vr41xx_select_irda_module(irda_module_t module, irda_speed_t speed)
139{
140 struct uart_port *port;
141 unsigned long flags;
142 uint8_t irsel;
143
144 port = &siu_uart_ports[0];
145
146 spin_lock_irqsave(&port->lock, flags);
147
148 irsel = siu_read(port, SIUIRSEL);
149 irsel &= ~(IRMSEL | TMICTX | TMICMODE);
150 switch (module) {
151 case SHARP_IRDA:
152 irsel |= IRMSEL_SHARP;
153 break;
154 case TEMIC_IRDA:
155 irsel |= IRMSEL_TEMIC | TMICMODE;
156 if (speed == IRDA_TX_4MBPS)
157 irsel |= TMICTX;
158 break;
159 case HP_IRDA:
160 irsel |= IRMSEL_HP;
161 break;
162 default:
163 break;
164 }
165 siu_write(port, SIUIRSEL, irsel);
166
167 spin_unlock_irqrestore(&port->lock, flags);
168}
169
170EXPORT_SYMBOL_GPL(vr41xx_select_irda_module);
171
172static inline void siu_clear_fifo(struct uart_port *port)
173{
174 siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO);
175 siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
176 UART_FCR_CLEAR_XMIT);
177 siu_write(port, UART_FCR, 0);
178}
179
180static inline int siu_probe_ports(void)
181{
182 switch (current_cpu_data.cputype) {
183 case CPU_VR4111:
184 case CPU_VR4121:
185 return SIU_TYPE1_NR_PORTS;
186 case CPU_VR4122:
187 case CPU_VR4131:
188 case CPU_VR4133:
189 return SIU_TYPE2_NR_PORTS;
190 }
191
192 return 0;
193}
194
195static inline unsigned long siu_port_size(struct uart_port *port)
196{
197 switch (port->type) {
198 case PORT_VR41XX_SIU:
199 return 11UL;
200 case PORT_VR41XX_DSIU:
201 return 8UL;
202 }
203
204 return 0;
205}
206
207static inline unsigned int siu_check_type(struct uart_port *port)
208{
209 switch (current_cpu_data.cputype) {
210 case CPU_VR4111:
211 case CPU_VR4121:
212 if (port->line == 0)
213 return PORT_VR41XX_SIU;
214 break;
215 case CPU_VR4122:
216 case CPU_VR4131:
217 case CPU_VR4133:
218 if (port->line == 0)
219 return PORT_VR41XX_SIU;
220 else if (port->line == 1)
221 return PORT_VR41XX_DSIU;
222 break;
223 }
224
225 return PORT_UNKNOWN;
226}
227
228static inline const char *siu_type_name(struct uart_port *port)
229{
230 switch (port->type) {
231 case PORT_VR41XX_SIU:
232 return "SIU";
233 case PORT_VR41XX_DSIU:
234 return "DSIU";
235 }
236
237 return "unknown";
238}
239
240static unsigned int siu_tx_empty(struct uart_port *port)
241{
242 uint8_t lsr;
243
244 lsr = siu_read(port, UART_LSR);
245 if (lsr & UART_LSR_TEMT)
246 return TIOCSER_TEMT;
247
248 return 0;
249}
250
251static void siu_set_mctrl(struct uart_port *port, unsigned int mctrl)
252{
253 uint8_t mcr = 0;
254
255 if (mctrl & TIOCM_DTR)
256 mcr |= UART_MCR_DTR;
257 if (mctrl & TIOCM_RTS)
258 mcr |= UART_MCR_RTS;
259 if (mctrl & TIOCM_OUT1)
260 mcr |= UART_MCR_OUT1;
261 if (mctrl & TIOCM_OUT2)
262 mcr |= UART_MCR_OUT2;
263 if (mctrl & TIOCM_LOOP)
264 mcr |= UART_MCR_LOOP;
265
266 siu_write(port, UART_MCR, mcr);
267}
268
269static unsigned int siu_get_mctrl(struct uart_port *port)
270{
271 uint8_t msr;
272 unsigned int mctrl = 0;
273
274 msr = siu_read(port, UART_MSR);
275 if (msr & UART_MSR_DCD)
276 mctrl |= TIOCM_CAR;
277 if (msr & UART_MSR_RI)
278 mctrl |= TIOCM_RNG;
279 if (msr & UART_MSR_DSR)
280 mctrl |= TIOCM_DSR;
281 if (msr & UART_MSR_CTS)
282 mctrl |= TIOCM_CTS;
283
284 return mctrl;
285}
286
287static void siu_stop_tx(struct uart_port *port, unsigned int tty_stop)
288{
289 unsigned long flags;
290 uint8_t ier;
291
292 spin_lock_irqsave(&port->lock, flags);
293
294 ier = siu_read(port, UART_IER);
295 ier &= ~UART_IER_THRI;
296 siu_write(port, UART_IER, ier);
297
298 spin_unlock_irqrestore(&port->lock, flags);
299}
300
301static void siu_start_tx(struct uart_port *port, unsigned int tty_start)
302{
303 unsigned long flags;
304 uint8_t ier;
305
306 spin_lock_irqsave(&port->lock, flags);
307
308 ier = siu_read(port, UART_IER);
309 ier |= UART_IER_THRI;
310 siu_write(port, UART_IER, ier);
311
312 spin_unlock_irqrestore(&port->lock, flags);
313}
314
315static void siu_stop_rx(struct uart_port *port)
316{
317 unsigned long flags;
318 uint8_t ier;
319
320 spin_lock_irqsave(&port->lock, flags);
321
322 ier = siu_read(port, UART_IER);
323 ier &= ~UART_IER_RLSI;
324 siu_write(port, UART_IER, ier);
325
326 port->read_status_mask &= ~UART_LSR_DR;
327
328 spin_unlock_irqrestore(&port->lock, flags);
329}
330
331static void siu_enable_ms(struct uart_port *port)
332{
333 unsigned long flags;
334 uint8_t ier;
335
336 spin_lock_irqsave(&port->lock, flags);
337
338 ier = siu_read(port, UART_IER);
339 ier |= UART_IER_MSI;
340 siu_write(port, UART_IER, ier);
341
342 spin_unlock_irqrestore(&port->lock, flags);
343}
344
345static void siu_break_ctl(struct uart_port *port, int ctl)
346{
347 unsigned long flags;
348 uint8_t lcr;
349
350 spin_lock_irqsave(&port->lock, flags);
351
352 lcr = siu_read(port, UART_LCR);
353 if (ctl == -1)
354 lcr |= UART_LCR_SBC;
355 else
356 lcr &= ~UART_LCR_SBC;
357 siu_write(port, UART_LCR, lcr);
358
359 spin_unlock_irqrestore(&port->lock, flags);
360}
361
362static inline void receive_chars(struct uart_port *port, uint8_t *status,
363 struct pt_regs *regs)
364{
365 struct tty_struct *tty;
366 uint8_t lsr, ch;
367 char flag;
368 int max_count = RX_MAX_COUNT;
369
370 tty = port->info->tty;
371 lsr = *status;
372
373 do {
374 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
375 if (tty->low_latency)
376 tty_flip_buffer_push(tty);
377 }
378
379 ch = siu_read(port, UART_RX);
380 port->icount.rx++;
381 flag = TTY_NORMAL;
382
383#ifdef CONFIG_SERIAL_VR41XX_CONSOLE
384 lsr |= lsr_break_flag[port->line];
385 lsr_break_flag[port->line] = 0;
386#endif
387 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_FE |
388 UART_LSR_PE | UART_LSR_OE))) {
389 if (lsr & UART_LSR_BI) {
390 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
391 port->icount.brk++;
392
393 if (uart_handle_break(port))
394 goto ignore_char;
395 }
396
397 if (lsr & UART_LSR_FE)
398 port->icount.frame++;
399 if (lsr & UART_LSR_PE)
400 port->icount.parity++;
401 if (lsr & UART_LSR_OE)
402 port->icount.overrun++;
403
404 lsr &= port->read_status_mask;
405 if (lsr & UART_LSR_BI)
406 flag = TTY_BREAK;
407 if (lsr & UART_LSR_FE)
408 flag = TTY_FRAME;
409 if (lsr & UART_LSR_PE)
410 flag = TTY_PARITY;
411 }
412
413 if (uart_handle_sysrq_char(port, ch, regs))
414 goto ignore_char;
415 if ((lsr & port->ignore_status_mask) == 0)
416 tty_insert_flip_char(tty, ch, flag);
417 if ((lsr & UART_LSR_OE) && (tty->flip.count < TTY_FLIPBUF_SIZE))
418 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
419
420 ignore_char:
421 lsr = siu_read(port, UART_LSR);
422 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
423
424 tty_flip_buffer_push(tty);
425
426 *status = lsr;
427}
428
429static inline void check_modem_status(struct uart_port *port)
430{
431 uint8_t msr;
432
433 msr = siu_read(port, UART_MSR);
434 if ((msr & UART_MSR_ANY_DELTA) == 0)
435 return;
436 if (msr & UART_MSR_DDCD)
437 uart_handle_dcd_change(port, msr & UART_MSR_DCD);
438 if (msr & UART_MSR_TERI)
439 port->icount.rng++;
440 if (msr & UART_MSR_DDSR)
441 port->icount.dsr++;
442 if (msr & UART_MSR_DCTS)
443 uart_handle_cts_change(port, msr & UART_MSR_CTS);
444
445 wake_up_interruptible(&port->info->delta_msr_wait);
446}
447
448static inline void transmit_chars(struct uart_port *port)
449{
450 struct circ_buf *xmit;
451 int max_count = TX_MAX_COUNT;
452
453 xmit = &port->info->xmit;
454
455 if (port->x_char) {
456 siu_write(port, UART_TX, port->x_char);
457 port->icount.tx++;
458 port->x_char = 0;
459 return;
460 }
461
462 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
463 siu_stop_tx(port, 0);
464 return;
465 }
466
467 do {
468 siu_write(port, UART_TX, xmit->buf[xmit->tail]);
469 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
470 port->icount.tx++;
471 if (uart_circ_empty(xmit))
472 break;
473 } while (max_count-- > 0);
474
475 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
476 uart_write_wakeup(port);
477
478 if (uart_circ_empty(xmit))
479 siu_stop_tx(port, 0);
480}
481
482static irqreturn_t siu_interrupt(int irq, void *dev_id, struct pt_regs *regs)
483{
484 struct uart_port *port;
485 uint8_t iir, lsr;
486
487 if (dev_id == NULL)
488 return IRQ_NONE;
489
490 port = (struct uart_port *)dev_id;
491
492 iir = siu_read(port, UART_IIR);
493 if (iir & UART_IIR_NO_INT)
494 return IRQ_NONE;
495
496 lsr = siu_read(port, UART_LSR);
497 if (lsr & UART_LSR_DR)
498 receive_chars(port, &lsr, regs);
499
500 check_modem_status(port);
501
502 if (lsr & UART_LSR_THRE)
503 transmit_chars(port);
504
505 return IRQ_HANDLED;
506}
507
508static int siu_startup(struct uart_port *port)
509{
510 int retval;
511
512 siu_clear_fifo(port);
513
514 (void)siu_read(port, UART_LSR);
515 (void)siu_read(port, UART_RX);
516 (void)siu_read(port, UART_IIR);
517 (void)siu_read(port, UART_MSR);
518
519 if (siu_read(port, UART_LSR) == 0xff)
520 return -ENODEV;
521
522 retval = request_irq(port->irq, siu_interrupt, 0, siu_type_name(port), port);
523 if (retval)
524 return retval;
525
526 if (port->type == PORT_VR41XX_DSIU)
527 vr41xx_enable_dsiuint(DSIUINT_ALL);
528
529 siu_write(port, UART_LCR, UART_LCR_WLEN8);
530
531 spin_lock_irq(&port->lock);
532 siu_set_mctrl(port, port->mctrl);
533 spin_unlock_irq(&port->lock);
534
535 siu_write(port, UART_IER, UART_IER_RLSI | UART_IER_RDI);
536
537 (void)siu_read(port, UART_LSR);
538 (void)siu_read(port, UART_RX);
539 (void)siu_read(port, UART_IIR);
540 (void)siu_read(port, UART_MSR);
541
542 return 0;
543}
544
545static void siu_shutdown(struct uart_port *port)
546{
547 unsigned long flags;
548 uint8_t lcr;
549
550 if (port->membase == NULL)
551 return;
552
553 siu_write(port, UART_IER, 0);
554
555 spin_lock_irqsave(&port->lock, flags);
556
557 port->mctrl &= ~TIOCM_OUT2;
558 siu_set_mctrl(port, port->mctrl);
559
560 spin_unlock_irqrestore(&port->lock, flags);
561
562 lcr = siu_read(port, UART_LCR);
563 lcr &= ~UART_LCR_SBC;
564 siu_write(port, UART_LCR, lcr);
565
566 siu_clear_fifo(port);
567
568 (void)siu_read(port, UART_RX);
569
570 if (port->type == PORT_VR41XX_DSIU)
571 vr41xx_disable_dsiuint(DSIUINT_ALL);
572
573 free_irq(port->irq, port);
574}
575
576static void siu_set_termios(struct uart_port *port, struct termios *new,
577 struct termios *old)
578{
579 tcflag_t c_cflag, c_iflag;
580 uint8_t lcr, fcr, ier;
581 unsigned int baud, quot;
582 unsigned long flags;
583
584 c_cflag = new->c_cflag;
585 switch (c_cflag & CSIZE) {
586 case CS5:
587 lcr = UART_LCR_WLEN5;
588 break;
589 case CS6:
590 lcr = UART_LCR_WLEN6;
591 break;
592 case CS7:
593 lcr = UART_LCR_WLEN7;
594 break;
595 default:
596 lcr = UART_LCR_WLEN8;
597 break;
598 }
599
600 if (c_cflag & CSTOPB)
601 lcr |= UART_LCR_STOP;
602 if (c_cflag & PARENB)
603 lcr |= UART_LCR_PARITY;
604 if ((c_cflag & PARODD) != PARODD)
605 lcr |= UART_LCR_EPAR;
606 if (c_cflag & CMSPAR)
607 lcr |= UART_LCR_SPAR;
608
609 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
610 quot = uart_get_divisor(port, baud);
611
612 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
613
614 spin_lock_irqsave(&port->lock, flags);
615
616 uart_update_timeout(port, c_cflag, baud);
617
618 c_iflag = new->c_iflag;
619
620 port->read_status_mask = UART_LSR_THRE | UART_LSR_OE | UART_LSR_DR;
621 if (c_iflag & INPCK)
622 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
623 if (c_iflag & (BRKINT | PARMRK))
624 port->read_status_mask |= UART_LSR_BI;
625
626 port->ignore_status_mask = 0;
627 if (c_iflag & IGNPAR)
628 port->ignore_status_mask |= UART_LSR_FE | UART_LSR_PE;
629 if (c_iflag & IGNBRK) {
630 port->ignore_status_mask |= UART_LSR_BI;
631 if (c_iflag & IGNPAR)
632 port->ignore_status_mask |= UART_LSR_OE;
633 }
634
635 if ((c_cflag & CREAD) == 0)
636 port->ignore_status_mask |= UART_LSR_DR;
637
638 ier = siu_read(port, UART_IER);
639 ier &= ~UART_IER_MSI;
640 if (UART_ENABLE_MS(port, c_cflag))
641 ier |= UART_IER_MSI;
642 siu_write(port, UART_IER, ier);
643
644 siu_write(port, UART_LCR, lcr | UART_LCR_DLAB);
645
646 siu_write(port, UART_DLL, (uint8_t)quot);
647 siu_write(port, UART_DLM, (uint8_t)(quot >> 8));
648
649 siu_write(port, UART_LCR, lcr);
650
651 siu_write(port, UART_FCR, fcr);
652
653 siu_set_mctrl(port, port->mctrl);
654
655 spin_unlock_irqrestore(&port->lock, flags);
656}
657
658static void siu_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
659{
660 switch (state) {
661 case 0:
662 switch (port->type) {
663 case PORT_VR41XX_SIU:
664 vr41xx_supply_clock(SIU_CLOCK);
665 break;
666 case PORT_VR41XX_DSIU:
667 vr41xx_supply_clock(DSIU_CLOCK);
668 break;
669 }
670 break;
671 case 3:
672 switch (port->type) {
673 case PORT_VR41XX_SIU:
674 vr41xx_mask_clock(SIU_CLOCK);
675 break;
676 case PORT_VR41XX_DSIU:
677 vr41xx_mask_clock(DSIU_CLOCK);
678 break;
679 }
680 break;
681 }
682}
683
684static const char *siu_type(struct uart_port *port)
685{
686 return siu_type_name(port);
687}
688
689static void siu_release_port(struct uart_port *port)
690{
691 unsigned long size;
692
693 if (port->flags & UPF_IOREMAP) {
694 iounmap(port->membase);
695 port->membase = NULL;
696 }
697
698 size = siu_port_size(port);
699 release_mem_region(port->mapbase, size);
700}
701
702static int siu_request_port(struct uart_port *port)
703{
704 unsigned long size;
705 struct resource *res;
706
707 size = siu_port_size(port);
708 res = request_mem_region(port->mapbase, size, siu_type_name(port));
709 if (res == NULL)
710 return -EBUSY;
711
712 if (port->flags & UPF_IOREMAP) {
713 port->membase = ioremap(port->mapbase, size);
714 if (port->membase == NULL) {
715 release_resource(res);
716 return -ENOMEM;
717 }
718 }
719
720 return 0;
721}
722
723static void siu_config_port(struct uart_port *port, int flags)
724{
725 if (flags & UART_CONFIG_TYPE) {
726 port->type = siu_check_type(port);
727 (void)siu_request_port(port);
728 }
729}
730
731static int siu_verify_port(struct uart_port *port, struct serial_struct *serial)
732{
733 if (port->type != PORT_VR41XX_SIU && port->type != PORT_VR41XX_DSIU)
734 return -EINVAL;
735 if (port->irq != serial->irq)
736 return -EINVAL;
737 if (port->iotype != serial->io_type)
738 return -EINVAL;
739 if (port->mapbase != (unsigned long)serial->iomem_base)
740 return -EINVAL;
741
742 return 0;
743}
744
745static struct uart_ops siu_uart_ops = {
746 .tx_empty = siu_tx_empty,
747 .set_mctrl = siu_set_mctrl,
748 .get_mctrl = siu_get_mctrl,
749 .stop_tx = siu_stop_tx,
750 .start_tx = siu_start_tx,
751 .stop_rx = siu_stop_rx,
752 .enable_ms = siu_enable_ms,
753 .break_ctl = siu_break_ctl,
754 .startup = siu_startup,
755 .shutdown = siu_shutdown,
756 .set_termios = siu_set_termios,
757 .pm = siu_pm,
758 .type = siu_type,
759 .release_port = siu_release_port,
760 .request_port = siu_request_port,
761 .config_port = siu_config_port,
762 .verify_port = siu_verify_port,
763};
764
765static int siu_init_ports(void)
766{
767 const struct siu_port *siu;
768 struct uart_port *port;
769 int i, num;
770
771 switch (current_cpu_data.cputype) {
772 case CPU_VR4111:
773 case CPU_VR4121:
774 siu = siu_type1_ports;
775 break;
776 case CPU_VR4122:
777 case CPU_VR4131:
778 case CPU_VR4133:
779 siu = siu_type2_ports;
780 break;
781 default:
782 return 0;
783 }
784
785 port = siu_uart_ports;
786 num = siu_probe_ports();
787 for (i = 0; i < num; i++) {
788 spin_lock_init(&port->lock);
789 port->irq = siu->irq;
790 port->uartclk = SIU_BAUD_BASE * 16;
791 port->fifosize = 16;
792 port->regshift = 0;
793 port->iotype = UPIO_MEM;
794 port->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
795 port->type = siu->type;
796 port->line = i;
797 port->mapbase = siu->start;
798 siu++;
799 port++;
800 }
801
802 return num;
803}
804
805#ifdef CONFIG_SERIAL_VR41XX_CONSOLE
806
807static void early_set_termios(struct uart_port *port, struct termios *new,
808 struct termios *old)
809{
810 tcflag_t c_cflag;
811 uint8_t lcr;
812 unsigned int baud, quot;
813
814 c_cflag = new->c_cflag;
815 switch (c_cflag & CSIZE) {
816 case CS5:
817 lcr = UART_LCR_WLEN5;
818 break;
819 case CS6:
820 lcr = UART_LCR_WLEN6;
821 break;
822 case CS7:
823 lcr = UART_LCR_WLEN7;
824 break;
825 default:
826 lcr = UART_LCR_WLEN8;
827 break;
828 }
829
830 if (c_cflag & CSTOPB)
831 lcr |= UART_LCR_STOP;
832 if (c_cflag & PARENB)
833 lcr |= UART_LCR_PARITY;
834 if ((c_cflag & PARODD) != PARODD)
835 lcr |= UART_LCR_EPAR;
836 if (c_cflag & CMSPAR)
837 lcr |= UART_LCR_SPAR;
838
839 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
840 quot = uart_get_divisor(port, baud);
841
842 siu_write(port, UART_LCR, lcr | UART_LCR_DLAB);
843
844 siu_write(port, UART_DLL, (uint8_t)quot);
845 siu_write(port, UART_DLM, (uint8_t)(quot >> 8));
846
847 siu_write(port, UART_LCR, lcr);
848}
849
850static struct uart_ops early_uart_ops = {
851 .set_termios = early_set_termios,
852};
853
854#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
855
856static void wait_for_xmitr(struct uart_port *port)
857{
858 int timeout = 10000;
859 uint8_t lsr, msr;
860
861 do {
862 lsr = siu_read(port, UART_LSR);
863 if (lsr & UART_LSR_BI)
864 lsr_break_flag[port->line] = UART_LSR_BI;
865
866 if ((lsr & BOTH_EMPTY) == BOTH_EMPTY)
867 break;
868 } while (timeout-- > 0);
869
870 if (port->flags & UPF_CONS_FLOW) {
871 timeout = 1000000;
872
873 do {
874 msr = siu_read(port, UART_MSR);
875 if ((msr & UART_MSR_CTS) != 0)
876 break;
877 } while (timeout-- > 0);
878 }
879}
880
881static void siu_console_write(struct console *con, const char *s, unsigned count)
882{
883 struct uart_port *port;
884 uint8_t ier;
885 unsigned i;
886
887 port = &siu_uart_ports[con->index];
888
889 ier = siu_read(port, UART_IER);
890 siu_write(port, UART_IER, 0);
891
892 for (i = 0; i < count && *s != '\0'; i++, s++) {
893 wait_for_xmitr(port);
894 siu_write(port, UART_TX, *s);
895 if (*s == '\n') {
896 wait_for_xmitr(port);
897 siu_write(port, UART_TX, '\r');
898 }
899 }
900
901 wait_for_xmitr(port);
902 siu_write(port, UART_IER, ier);
903}
904
905static int siu_console_setup(struct console *con, char *options)
906{
907 struct uart_port *port;
908 int baud = 9600;
909 int parity = 'n';
910 int bits = 8;
911 int flow = 'n';
912
913 if (con->index >= SIU_PORTS_MAX)
914 con->index = 0;
915
916 port = &siu_uart_ports[con->index];
917 if (port->membase == NULL) {
918 if (port->mapbase == 0)
919 return -ENODEV;
920 port->membase = (unsigned char __iomem *)KSEG1ADDR(port->mapbase);
921 }
922
923 vr41xx_select_siu_interface(SIU_INTERFACE_RS232C);
924
925 if (options != NULL)
926 uart_parse_options(options, &baud, &parity, &bits, &flow);
927
928 return uart_set_options(port, con, baud, parity, bits, flow);
929}
930
931static struct uart_driver siu_uart_driver;
932
933static struct console siu_console = {
934 .name = "ttyVR",
935 .write = siu_console_write,
936 .device = uart_console_device,
937 .setup = siu_console_setup,
938 .flags = CON_PRINTBUFFER,
939 .index = -1,
940 .data = &siu_uart_driver,
941};
942
943static int __devinit siu_console_init(void)
944{
945 struct uart_port *port;
946 int num, i;
947
948 num = siu_init_ports();
949 if (num <= 0)
950 return -ENODEV;
951
952 for (i = 0; i < num; i++) {
953 port = &siu_uart_ports[i];
954 port->ops = &early_uart_ops;
955 }
956
957 register_console(&siu_console);
958
959 return 0;
960}
961
962console_initcall(siu_console_init);
963
964#define SERIAL_VR41XX_CONSOLE &siu_console
965#else
966#define SERIAL_VR41XX_CONSOLE NULL
967#endif
968
969static struct uart_driver siu_uart_driver = {
970 .owner = THIS_MODULE,
971 .driver_name = "SIU",
972 .dev_name = "ttyVR",
973 .devfs_name = "ttvr/",
974 .major = SIU_MAJOR,
975 .minor = SIU_MINOR_BASE,
976 .cons = SERIAL_VR41XX_CONSOLE,
977};
978
979static int siu_probe(struct device *dev)
980{
981 struct uart_port *port;
982 int num, i, retval;
983
984 num = siu_init_ports();
985 if (num <= 0)
986 return -ENODEV;
987
988 siu_uart_driver.nr = num;
989 retval = uart_register_driver(&siu_uart_driver);
990 if (retval)
991 return retval;
992
993 for (i = 0; i < num; i++) {
994 port = &siu_uart_ports[i];
995 port->ops = &siu_uart_ops;
996 port->dev = dev;
997
998 retval = uart_add_one_port(&siu_uart_driver, port);
999 if (retval)
1000 break;
1001 }
1002
1003 if (i == 0 && retval < 0) {
1004 uart_unregister_driver(&siu_uart_driver);
1005 return retval;
1006 }
1007
1008 return 0;
1009}
1010
1011static int siu_remove(struct device *dev)
1012{
1013 struct uart_port *port;
1014 int i;
1015
1016 for (i = 0; i < siu_uart_driver.nr; i++) {
1017 port = &siu_uart_ports[i];
1018 if (port->dev == dev) {
1019 uart_remove_one_port(&siu_uart_driver, port);
1020 port->dev = NULL;
1021 }
1022 }
1023
1024 uart_unregister_driver(&siu_uart_driver);
1025
1026 return 0;
1027}
1028
1029static int siu_suspend(struct device *dev, u32 state, u32 level)
1030{
1031 struct uart_port *port;
1032 int i;
1033
1034 if (level != SUSPEND_DISABLE)
1035 return 0;
1036
1037 for (i = 0; i < siu_uart_driver.nr; i++) {
1038 port = &siu_uart_ports[i];
1039 if ((port->type == PORT_VR41XX_SIU ||
1040 port->type == PORT_VR41XX_DSIU) && port->dev == dev)
1041 uart_suspend_port(&siu_uart_driver, port);
1042
1043 }
1044
1045 return 0;
1046}
1047
1048static int siu_resume(struct device *dev, u32 level)
1049{
1050 struct uart_port *port;
1051 int i;
1052
1053 if (level != RESUME_ENABLE)
1054 return 0;
1055
1056 for (i = 0; i < siu_uart_driver.nr; i++) {
1057 port = &siu_uart_ports[i];
1058 if ((port->type == PORT_VR41XX_SIU ||
1059 port->type == PORT_VR41XX_DSIU) && port->dev == dev)
1060 uart_resume_port(&siu_uart_driver, port);
1061 }
1062
1063 return 0;
1064}
1065
1066static struct platform_device *siu_platform_device;
1067
1068static struct device_driver siu_device_driver = {
1069 .name = "SIU",
1070 .bus = &platform_bus_type,
1071 .probe = siu_probe,
1072 .remove = siu_remove,
1073 .suspend = siu_suspend,
1074 .resume = siu_resume,
1075};
1076
1077static int __devinit vr41xx_siu_init(void)
1078{
1079 int retval;
1080
1081 siu_platform_device = platform_device_register_simple("SIU", -1, NULL, 0);
1082 if (IS_ERR(siu_platform_device))
1083 return PTR_ERR(siu_platform_device);
1084
1085 retval = driver_register(&siu_device_driver);
1086 if (retval < 0)
1087 platform_device_unregister(siu_platform_device);
1088
1089 return retval;
1090}
1091
1092static void __devexit vr41xx_siu_exit(void)
1093{
1094 driver_unregister(&siu_device_driver);
1095
1096 platform_device_unregister(siu_platform_device);
1097}
1098
1099module_init(vr41xx_siu_init);
1100module_exit(vr41xx_siu_exit);