aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/tty')
-rw-r--r--drivers/tty/Makefile1
-rw-r--r--drivers/tty/serial/21285.c513
-rw-r--r--drivers/tty/serial/68328serial.c1472
-rw-r--r--drivers/tty/serial/68328serial.h188
-rw-r--r--drivers/tty/serial/68360serial.c2978
-rw-r--r--drivers/tty/serial/8250.c3377
-rw-r--r--drivers/tty/serial/8250.h80
-rw-r--r--drivers/tty/serial/8250_accent.c47
-rw-r--r--drivers/tty/serial/8250_acorn.c141
-rw-r--r--drivers/tty/serial/8250_boca.c61
-rw-r--r--drivers/tty/serial/8250_early.c287
-rw-r--r--drivers/tty/serial/8250_exar_st16c554.c52
-rw-r--r--drivers/tty/serial/8250_fourport.c53
-rw-r--r--drivers/tty/serial/8250_gsc.c122
-rw-r--r--drivers/tty/serial/8250_hp300.c327
-rw-r--r--drivers/tty/serial/8250_hub6.c58
-rw-r--r--drivers/tty/serial/8250_mca.c63
-rw-r--r--drivers/tty/serial/8250_pci.c3850
-rw-r--r--drivers/tty/serial/8250_pnp.c523
-rw-r--r--drivers/tty/serial/Kconfig1598
-rw-r--r--drivers/tty/serial/Makefile94
-rw-r--r--drivers/tty/serial/altera_jtaguart.c504
-rw-r--r--drivers/tty/serial/altera_uart.c608
-rw-r--r--drivers/tty/serial/amba-pl010.c825
-rw-r--r--drivers/tty/serial/amba-pl011.c1519
-rw-r--r--drivers/tty/serial/apbuart.c709
-rw-r--r--drivers/tty/serial/apbuart.h64
-rw-r--r--drivers/tty/serial/atmel_serial.c1808
-rw-r--r--drivers/tty/serial/bcm63xx_uart.c891
-rw-r--r--drivers/tty/serial/bfin_5xx.c1600
-rw-r--r--drivers/tty/serial/bfin_sport_uart.c935
-rw-r--r--drivers/tty/serial/bfin_sport_uart.h86
-rw-r--r--drivers/tty/serial/clps711x.c579
-rw-r--r--drivers/tty/serial/cpm_uart/Makefile11
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart.h147
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_core.c1443
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c138
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h34
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c174
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h34
-rw-r--r--drivers/tty/serial/crisv10.c4573
-rw-r--r--drivers/tty/serial/crisv10.h147
-rw-r--r--drivers/tty/serial/dz.c955
-rw-r--r--drivers/tty/serial/dz.h129
-rw-r--r--drivers/tty/serial/icom.c1658
-rw-r--r--drivers/tty/serial/icom.h287
-rw-r--r--drivers/tty/serial/ifx6x60.c1406
-rw-r--r--drivers/tty/serial/ifx6x60.h129
-rw-r--r--drivers/tty/serial/imx.c1380
-rw-r--r--drivers/tty/serial/ioc3_serial.c2199
-rw-r--r--drivers/tty/serial/ioc4_serial.c2953
-rw-r--r--drivers/tty/serial/ip22zilog.c1221
-rw-r--r--drivers/tty/serial/ip22zilog.h281
-rw-r--r--drivers/tty/serial/jsm/Makefile8
-rw-r--r--drivers/tty/serial/jsm/jsm.h388
-rw-r--r--drivers/tty/serial/jsm/jsm_driver.c297
-rw-r--r--drivers/tty/serial/jsm/jsm_neo.c1412
-rw-r--r--drivers/tty/serial/jsm/jsm_tty.c910
-rw-r--r--drivers/tty/serial/kgdboc.c328
-rw-r--r--drivers/tty/serial/m32r_sio.c1192
-rw-r--r--drivers/tty/serial/m32r_sio.h48
-rw-r--r--drivers/tty/serial/m32r_sio_reg.h152
-rw-r--r--drivers/tty/serial/max3100.c926
-rw-r--r--drivers/tty/serial/max3107-aava.c344
-rw-r--r--drivers/tty/serial/max3107.c1213
-rw-r--r--drivers/tty/serial/max3107.h441
-rw-r--r--drivers/tty/serial/mcf.c662
-rw-r--r--drivers/tty/serial/mfd.c1513
-rw-r--r--drivers/tty/serial/mpc52xx_uart.c1527
-rw-r--r--drivers/tty/serial/mpsc.c2159
-rw-r--r--drivers/tty/serial/mrst_max3110.c919
-rw-r--r--drivers/tty/serial/mrst_max3110.h60
-rw-r--r--drivers/tty/serial/msm_serial.c758
-rw-r--r--drivers/tty/serial/msm_serial.h173
-rw-r--r--drivers/tty/serial/mux.c633
-rw-r--r--drivers/tty/serial/netx-serial.c750
-rw-r--r--drivers/tty/serial/nwpserial.c477
-rw-r--r--drivers/tty/serial/of_serial.c201
-rw-r--r--drivers/tty/serial/omap-serial.c1359
-rw-r--r--drivers/tty/serial/pch_uart.c1451
-rw-r--r--drivers/tty/serial/pmac_zilog.c2208
-rw-r--r--drivers/tty/serial/pmac_zilog.h396
-rw-r--r--drivers/tty/serial/pnx8xxx_uart.c854
-rw-r--r--drivers/tty/serial/pxa.c879
-rw-r--r--drivers/tty/serial/s3c2400.c106
-rw-r--r--drivers/tty/serial/s3c2410.c118
-rw-r--r--drivers/tty/serial/s3c2412.c152
-rw-r--r--drivers/tty/serial/s3c2440.c181
-rw-r--r--drivers/tty/serial/s3c24a0.c118
-rw-r--r--drivers/tty/serial/s3c6400.c152
-rw-r--r--drivers/tty/serial/s5pv210.c162
-rw-r--r--drivers/tty/serial/sa1100.c918
-rw-r--r--drivers/tty/serial/samsung.c1487
-rw-r--r--drivers/tty/serial/samsung.h120
-rw-r--r--drivers/tty/serial/sb1250-duart.c976
-rw-r--r--drivers/tty/serial/sc26xx.c757
-rw-r--r--drivers/tty/serial/serial_core.c2578
-rw-r--r--drivers/tty/serial/serial_cs.c869
-rw-r--r--drivers/tty/serial/serial_ks8695.c705
-rw-r--r--drivers/tty/serial/serial_lh7a40x.c682
-rw-r--r--drivers/tty/serial/serial_txx9.c1344
-rw-r--r--drivers/tty/serial/sh-sci.c2027
-rw-r--r--drivers/tty/serial/sh-sci.h660
-rw-r--r--drivers/tty/serial/sn_console.c1085
-rw-r--r--drivers/tty/serial/suncore.c247
-rw-r--r--drivers/tty/serial/suncore.h33
-rw-r--r--drivers/tty/serial/sunhv.c661
-rw-r--r--drivers/tty/serial/sunsab.c1152
-rw-r--r--drivers/tty/serial/sunsab.h322
-rw-r--r--drivers/tty/serial/sunsu.c1608
-rw-r--r--drivers/tty/serial/sunzilog.c1655
-rw-r--r--drivers/tty/serial/sunzilog.h289
-rw-r--r--drivers/tty/serial/timbuart.c531
-rw-r--r--drivers/tty/serial/timbuart.h58
-rw-r--r--drivers/tty/serial/uartlite.c709
-rw-r--r--drivers/tty/serial/ucc_uart.c1537
-rw-r--r--drivers/tty/serial/vr41xx_siu.c978
-rw-r--r--drivers/tty/serial/vt8500_serial.c648
-rw-r--r--drivers/tty/serial/zs.c1304
-rw-r--r--drivers/tty/serial/zs.h284
120 files changed, 97163 insertions, 0 deletions
diff --git a/drivers/tty/Makefile b/drivers/tty/Makefile
index d3685f071b8d..396277216e4f 100644
--- a/drivers/tty/Makefile
+++ b/drivers/tty/Makefile
@@ -10,3 +10,4 @@ obj-$(CONFIG_R3964) += n_r3964.o
10 10
11obj-y += vt/ 11obj-y += vt/
12obj-$(CONFIG_HVC_DRIVER) += hvc/ 12obj-$(CONFIG_HVC_DRIVER) += hvc/
13obj-y += serial/
diff --git a/drivers/tty/serial/21285.c b/drivers/tty/serial/21285.c
new file mode 100644
index 000000000000..d89aa38c5cf0
--- /dev/null
+++ b/drivers/tty/serial/21285.c
@@ -0,0 +1,513 @@
1/*
2 * linux/drivers/serial/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#include <linux/module.h>
9#include <linux/tty.h>
10#include <linux/ioport.h>
11#include <linux/init.h>
12#include <linux/console.h>
13#include <linux/device.h>
14#include <linux/tty_flip.h>
15#include <linux/serial_core.h>
16#include <linux/serial.h>
17#include <linux/io.h>
18
19#include <asm/irq.h>
20#include <asm/mach-types.h>
21#include <asm/hardware/dec21285.h>
22#include <mach/hardware.h>
23
24#define BAUD_BASE (mem_fclk_21285/64)
25
26#define SERIAL_21285_NAME "ttyFB"
27#define SERIAL_21285_MAJOR 204
28#define SERIAL_21285_MINOR 4
29
30#define RXSTAT_DUMMY_READ 0x80000000
31#define RXSTAT_FRAME (1 << 0)
32#define RXSTAT_PARITY (1 << 1)
33#define RXSTAT_OVERRUN (1 << 2)
34#define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
35
36#define H_UBRLCR_BREAK (1 << 0)
37#define H_UBRLCR_PARENB (1 << 1)
38#define H_UBRLCR_PAREVN (1 << 2)
39#define H_UBRLCR_STOPB (1 << 3)
40#define H_UBRLCR_FIFO (1 << 4)
41
42static const char serial21285_name[] = "Footbridge UART";
43
44#define tx_enabled(port) ((port)->unused[0])
45#define rx_enabled(port) ((port)->unused[1])
46
47/*
48 * The documented expression for selecting the divisor is:
49 * BAUD_BASE / baud - 1
50 * However, typically BAUD_BASE is not divisible by baud, so
51 * we want to select the divisor that gives us the minimum
52 * error. Therefore, we want:
53 * int(BAUD_BASE / baud - 0.5) ->
54 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
55 * int((BAUD_BASE - (baud >> 1)) / baud)
56 */
57
58static void serial21285_stop_tx(struct uart_port *port)
59{
60 if (tx_enabled(port)) {
61 disable_irq_nosync(IRQ_CONTX);
62 tx_enabled(port) = 0;
63 }
64}
65
66static void serial21285_start_tx(struct uart_port *port)
67{
68 if (!tx_enabled(port)) {
69 enable_irq(IRQ_CONTX);
70 tx_enabled(port) = 1;
71 }
72}
73
74static void serial21285_stop_rx(struct uart_port *port)
75{
76 if (rx_enabled(port)) {
77 disable_irq_nosync(IRQ_CONRX);
78 rx_enabled(port) = 0;
79 }
80}
81
82static void serial21285_enable_ms(struct uart_port *port)
83{
84}
85
86static irqreturn_t serial21285_rx_chars(int irq, void *dev_id)
87{
88 struct uart_port *port = dev_id;
89 struct tty_struct *tty = port->state->port.tty;
90 unsigned int status, ch, flag, rxs, max_count = 256;
91
92 status = *CSR_UARTFLG;
93 while (!(status & 0x10) && max_count--) {
94 ch = *CSR_UARTDR;
95 flag = TTY_NORMAL;
96 port->icount.rx++;
97
98 rxs = *CSR_RXSTAT | RXSTAT_DUMMY_READ;
99 if (unlikely(rxs & RXSTAT_ANYERR)) {
100 if (rxs & RXSTAT_PARITY)
101 port->icount.parity++;
102 else if (rxs & RXSTAT_FRAME)
103 port->icount.frame++;
104 if (rxs & RXSTAT_OVERRUN)
105 port->icount.overrun++;
106
107 rxs &= port->read_status_mask;
108
109 if (rxs & RXSTAT_PARITY)
110 flag = TTY_PARITY;
111 else if (rxs & RXSTAT_FRAME)
112 flag = TTY_FRAME;
113 }
114
115 uart_insert_char(port, rxs, RXSTAT_OVERRUN, ch, flag);
116
117 status = *CSR_UARTFLG;
118 }
119 tty_flip_buffer_push(tty);
120
121 return IRQ_HANDLED;
122}
123
124static irqreturn_t serial21285_tx_chars(int irq, void *dev_id)
125{
126 struct uart_port *port = dev_id;
127 struct circ_buf *xmit = &port->state->xmit;
128 int count = 256;
129
130 if (port->x_char) {
131 *CSR_UARTDR = port->x_char;
132 port->icount.tx++;
133 port->x_char = 0;
134 goto out;
135 }
136 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
137 serial21285_stop_tx(port);
138 goto out;
139 }
140
141 do {
142 *CSR_UARTDR = xmit->buf[xmit->tail];
143 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
144 port->icount.tx++;
145 if (uart_circ_empty(xmit))
146 break;
147 } while (--count > 0 && !(*CSR_UARTFLG & 0x20));
148
149 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
150 uart_write_wakeup(port);
151
152 if (uart_circ_empty(xmit))
153 serial21285_stop_tx(port);
154
155 out:
156 return IRQ_HANDLED;
157}
158
159static unsigned int serial21285_tx_empty(struct uart_port *port)
160{
161 return (*CSR_UARTFLG & 8) ? 0 : TIOCSER_TEMT;
162}
163
164/* no modem control lines */
165static unsigned int serial21285_get_mctrl(struct uart_port *port)
166{
167 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
168}
169
170static void serial21285_set_mctrl(struct uart_port *port, unsigned int mctrl)
171{
172}
173
174static void serial21285_break_ctl(struct uart_port *port, int break_state)
175{
176 unsigned long flags;
177 unsigned int h_lcr;
178
179 spin_lock_irqsave(&port->lock, flags);
180 h_lcr = *CSR_H_UBRLCR;
181 if (break_state)
182 h_lcr |= H_UBRLCR_BREAK;
183 else
184 h_lcr &= ~H_UBRLCR_BREAK;
185 *CSR_H_UBRLCR = h_lcr;
186 spin_unlock_irqrestore(&port->lock, flags);
187}
188
189static int serial21285_startup(struct uart_port *port)
190{
191 int ret;
192
193 tx_enabled(port) = 1;
194 rx_enabled(port) = 1;
195
196 ret = request_irq(IRQ_CONRX, serial21285_rx_chars, 0,
197 serial21285_name, port);
198 if (ret == 0) {
199 ret = request_irq(IRQ_CONTX, serial21285_tx_chars, 0,
200 serial21285_name, port);
201 if (ret)
202 free_irq(IRQ_CONRX, port);
203 }
204
205 return ret;
206}
207
208static void serial21285_shutdown(struct uart_port *port)
209{
210 free_irq(IRQ_CONTX, port);
211 free_irq(IRQ_CONRX, port);
212}
213
214static void
215serial21285_set_termios(struct uart_port *port, struct ktermios *termios,
216 struct ktermios *old)
217{
218 unsigned long flags;
219 unsigned int baud, quot, h_lcr, b;
220
221 /*
222 * We don't support modem control lines.
223 */
224 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
225 termios->c_cflag |= CLOCAL;
226
227 /*
228 * We don't support BREAK character recognition.
229 */
230 termios->c_iflag &= ~(IGNBRK | BRKINT);
231
232 /*
233 * Ask the core to calculate the divisor for us.
234 */
235 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
236 quot = uart_get_divisor(port, baud);
237 b = port->uartclk / (16 * quot);
238 tty_termios_encode_baud_rate(termios, b, b);
239
240 switch (termios->c_cflag & CSIZE) {
241 case CS5:
242 h_lcr = 0x00;
243 break;
244 case CS6:
245 h_lcr = 0x20;
246 break;
247 case CS7:
248 h_lcr = 0x40;
249 break;
250 default: /* CS8 */
251 h_lcr = 0x60;
252 break;
253 }
254
255 if (termios->c_cflag & CSTOPB)
256 h_lcr |= H_UBRLCR_STOPB;
257 if (termios->c_cflag & PARENB) {
258 h_lcr |= H_UBRLCR_PARENB;
259 if (!(termios->c_cflag & PARODD))
260 h_lcr |= H_UBRLCR_PAREVN;
261 }
262
263 if (port->fifosize)
264 h_lcr |= H_UBRLCR_FIFO;
265
266 spin_lock_irqsave(&port->lock, flags);
267
268 /*
269 * Update the per-port timeout.
270 */
271 uart_update_timeout(port, termios->c_cflag, baud);
272
273 /*
274 * Which character status flags are we interested in?
275 */
276 port->read_status_mask = RXSTAT_OVERRUN;
277 if (termios->c_iflag & INPCK)
278 port->read_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
279
280 /*
281 * Which character status flags should we ignore?
282 */
283 port->ignore_status_mask = 0;
284 if (termios->c_iflag & IGNPAR)
285 port->ignore_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
286 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
287 port->ignore_status_mask |= RXSTAT_OVERRUN;
288
289 /*
290 * Ignore all characters if CREAD is not set.
291 */
292 if ((termios->c_cflag & CREAD) == 0)
293 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
294
295 quot -= 1;
296
297 *CSR_UARTCON = 0;
298 *CSR_L_UBRLCR = quot & 0xff;
299 *CSR_M_UBRLCR = (quot >> 8) & 0x0f;
300 *CSR_H_UBRLCR = h_lcr;
301 *CSR_UARTCON = 1;
302
303 spin_unlock_irqrestore(&port->lock, flags);
304}
305
306static const char *serial21285_type(struct uart_port *port)
307{
308 return port->type == PORT_21285 ? "DC21285" : NULL;
309}
310
311static void serial21285_release_port(struct uart_port *port)
312{
313 release_mem_region(port->mapbase, 32);
314}
315
316static int serial21285_request_port(struct uart_port *port)
317{
318 return request_mem_region(port->mapbase, 32, serial21285_name)
319 != NULL ? 0 : -EBUSY;
320}
321
322static void serial21285_config_port(struct uart_port *port, int flags)
323{
324 if (flags & UART_CONFIG_TYPE && serial21285_request_port(port) == 0)
325 port->type = PORT_21285;
326}
327
328/*
329 * verify the new serial_struct (for TIOCSSERIAL).
330 */
331static int serial21285_verify_port(struct uart_port *port, struct serial_struct *ser)
332{
333 int ret = 0;
334 if (ser->type != PORT_UNKNOWN && ser->type != PORT_21285)
335 ret = -EINVAL;
336 if (ser->irq != NO_IRQ)
337 ret = -EINVAL;
338 if (ser->baud_base != port->uartclk / 16)
339 ret = -EINVAL;
340 return ret;
341}
342
343static struct uart_ops serial21285_ops = {
344 .tx_empty = serial21285_tx_empty,
345 .get_mctrl = serial21285_get_mctrl,
346 .set_mctrl = serial21285_set_mctrl,
347 .stop_tx = serial21285_stop_tx,
348 .start_tx = serial21285_start_tx,
349 .stop_rx = serial21285_stop_rx,
350 .enable_ms = serial21285_enable_ms,
351 .break_ctl = serial21285_break_ctl,
352 .startup = serial21285_startup,
353 .shutdown = serial21285_shutdown,
354 .set_termios = serial21285_set_termios,
355 .type = serial21285_type,
356 .release_port = serial21285_release_port,
357 .request_port = serial21285_request_port,
358 .config_port = serial21285_config_port,
359 .verify_port = serial21285_verify_port,
360};
361
362static struct uart_port serial21285_port = {
363 .mapbase = 0x42000160,
364 .iotype = UPIO_MEM,
365 .irq = NO_IRQ,
366 .fifosize = 16,
367 .ops = &serial21285_ops,
368 .flags = UPF_BOOT_AUTOCONF,
369};
370
371static void serial21285_setup_ports(void)
372{
373 serial21285_port.uartclk = mem_fclk_21285 / 4;
374}
375
376#ifdef CONFIG_SERIAL_21285_CONSOLE
377static void serial21285_console_putchar(struct uart_port *port, int ch)
378{
379 while (*CSR_UARTFLG & 0x20)
380 barrier();
381 *CSR_UARTDR = ch;
382}
383
384static void
385serial21285_console_write(struct console *co, const char *s,
386 unsigned int count)
387{
388 uart_console_write(&serial21285_port, s, count, serial21285_console_putchar);
389}
390
391static void __init
392serial21285_get_options(struct uart_port *port, int *baud,
393 int *parity, int *bits)
394{
395 if (*CSR_UARTCON == 1) {
396 unsigned int tmp;
397
398 tmp = *CSR_H_UBRLCR;
399 switch (tmp & 0x60) {
400 case 0x00:
401 *bits = 5;
402 break;
403 case 0x20:
404 *bits = 6;
405 break;
406 case 0x40:
407 *bits = 7;
408 break;
409 default:
410 case 0x60:
411 *bits = 8;
412 break;
413 }
414
415 if (tmp & H_UBRLCR_PARENB) {
416 *parity = 'o';
417 if (tmp & H_UBRLCR_PAREVN)
418 *parity = 'e';
419 }
420
421 tmp = *CSR_L_UBRLCR | (*CSR_M_UBRLCR << 8);
422
423 *baud = port->uartclk / (16 * (tmp + 1));
424 }
425}
426
427static int __init serial21285_console_setup(struct console *co, char *options)
428{
429 struct uart_port *port = &serial21285_port;
430 int baud = 9600;
431 int bits = 8;
432 int parity = 'n';
433 int flow = 'n';
434
435 if (machine_is_personal_server())
436 baud = 57600;
437
438 /*
439 * Check whether an invalid uart number has been specified, and
440 * if so, search for the first available port that does have
441 * console support.
442 */
443 if (options)
444 uart_parse_options(options, &baud, &parity, &bits, &flow);
445 else
446 serial21285_get_options(port, &baud, &parity, &bits);
447
448 return uart_set_options(port, co, baud, parity, bits, flow);
449}
450
451static struct uart_driver serial21285_reg;
452
453static struct console serial21285_console =
454{
455 .name = SERIAL_21285_NAME,
456 .write = serial21285_console_write,
457 .device = uart_console_device,
458 .setup = serial21285_console_setup,
459 .flags = CON_PRINTBUFFER,
460 .index = -1,
461 .data = &serial21285_reg,
462};
463
464static int __init rs285_console_init(void)
465{
466 serial21285_setup_ports();
467 register_console(&serial21285_console);
468 return 0;
469}
470console_initcall(rs285_console_init);
471
472#define SERIAL_21285_CONSOLE &serial21285_console
473#else
474#define SERIAL_21285_CONSOLE NULL
475#endif
476
477static struct uart_driver serial21285_reg = {
478 .owner = THIS_MODULE,
479 .driver_name = "ttyFB",
480 .dev_name = "ttyFB",
481 .major = SERIAL_21285_MAJOR,
482 .minor = SERIAL_21285_MINOR,
483 .nr = 1,
484 .cons = SERIAL_21285_CONSOLE,
485};
486
487static int __init serial21285_init(void)
488{
489 int ret;
490
491 printk(KERN_INFO "Serial: 21285 driver\n");
492
493 serial21285_setup_ports();
494
495 ret = uart_register_driver(&serial21285_reg);
496 if (ret == 0)
497 uart_add_one_port(&serial21285_reg, &serial21285_port);
498
499 return ret;
500}
501
502static void __exit serial21285_exit(void)
503{
504 uart_remove_one_port(&serial21285_reg, &serial21285_port);
505 uart_unregister_driver(&serial21285_reg);
506}
507
508module_init(serial21285_init);
509module_exit(serial21285_exit);
510
511MODULE_LICENSE("GPL");
512MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver");
513MODULE_ALIAS_CHARDEV(SERIAL_21285_MAJOR, SERIAL_21285_MINOR);
diff --git a/drivers/tty/serial/68328serial.c b/drivers/tty/serial/68328serial.c
new file mode 100644
index 000000000000..be0ebce36e54
--- /dev/null
+++ b/drivers/tty/serial/68328serial.c
@@ -0,0 +1,1472 @@
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/major.h>
27#include <linux/string.h>
28#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/kernel.h>
31#include <linux/console.h>
32#include <linux/reboot.h>
33#include <linux/keyboard.h>
34#include <linux/init.h>
35#include <linux/pm.h>
36#include <linux/bitops.h>
37#include <linux/delay.h>
38#include <linux/gfp.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/system.h>
43#include <asm/delay.h>
44#include <asm/uaccess.h>
45
46/* (es) */
47/* note: perhaps we can murge these files, so that you can just
48 * define 1 of them, and they can sort that out for themselves
49 */
50#if defined(CONFIG_M68EZ328)
51#include <asm/MC68EZ328.h>
52#else
53#if defined(CONFIG_M68VZ328)
54#include <asm/MC68VZ328.h>
55#else
56#include <asm/MC68328.h>
57#endif /* CONFIG_M68VZ328 */
58#endif /* CONFIG_M68EZ328 */
59
60#include "68328serial.h"
61
62/* Turn off usage of real serial interrupt code, to "support" Copilot */
63#ifdef CONFIG_XCOPILOT_BUGS
64#undef USE_INTS
65#else
66#define USE_INTS
67#endif
68
69static struct m68k_serial m68k_soft[NR_PORTS];
70
71static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
72
73/* multiple ports are contiguous in memory */
74m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
75
76struct tty_struct m68k_ttys;
77struct m68k_serial *m68k_consinfo = 0;
78
79#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
80
81struct tty_driver *serial_driver;
82
83/* number of characters left in xmit buffer before we ask for more */
84#define WAKEUP_CHARS 256
85
86/* Debugging... DEBUG_INTR is bad to use when one of the zs
87 * lines is your console ;(
88 */
89#undef SERIAL_DEBUG_INTR
90#undef SERIAL_DEBUG_OPEN
91#undef SERIAL_DEBUG_FLOW
92
93#define RS_ISR_PASS_LIMIT 256
94
95static void change_speed(struct m68k_serial *info);
96
97/*
98 * Setup for console. Argument comes from the boot command line.
99 */
100
101/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
102#ifdef CONFIG_M68VZ328
103#define CONSOLE_BAUD_RATE 19200
104#define DEFAULT_CBAUD B19200
105#endif
106
107
108#ifndef CONSOLE_BAUD_RATE
109#define CONSOLE_BAUD_RATE 9600
110#define DEFAULT_CBAUD B9600
111#endif
112
113
114static int m68328_console_initted = 0;
115static int m68328_console_baud = CONSOLE_BAUD_RATE;
116static int m68328_console_cbaud = DEFAULT_CBAUD;
117
118
119static inline int serial_paranoia_check(struct m68k_serial *info,
120 char *name, const char *routine)
121{
122#ifdef SERIAL_PARANOIA_CHECK
123 static const char *badmagic =
124 "Warning: bad magic number for serial struct %s in %s\n";
125 static const char *badinfo =
126 "Warning: null m68k_serial for %s in %s\n";
127
128 if (!info) {
129 printk(badinfo, name, routine);
130 return 1;
131 }
132 if (info->magic != SERIAL_MAGIC) {
133 printk(badmagic, name, routine);
134 return 1;
135 }
136#endif
137 return 0;
138}
139
140/*
141 * This is used to figure out the divisor speeds and the timeouts
142 */
143static int baud_table[] = {
144 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
145 9600, 19200, 38400, 57600, 115200, 0 };
146
147/* Sets or clears DTR/RTS on the requested line */
148static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
149{
150 if (set) {
151 /* set the RTS/CTS line */
152 } else {
153 /* clear it */
154 }
155 return;
156}
157
158/* Utility routines */
159static inline int get_baud(struct m68k_serial *ss)
160{
161 unsigned long result = 115200;
162 unsigned short int baud = uart_addr[ss->line].ubaud;
163 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
164 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
165
166 return result;
167}
168
169/*
170 * ------------------------------------------------------------
171 * rs_stop() and rs_start()
172 *
173 * This routines are called before setting or resetting tty->stopped.
174 * They enable or disable transmitter interrupts, as necessary.
175 * ------------------------------------------------------------
176 */
177static void rs_stop(struct tty_struct *tty)
178{
179 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
180 m68328_uart *uart = &uart_addr[info->line];
181 unsigned long flags;
182
183 if (serial_paranoia_check(info, tty->name, "rs_stop"))
184 return;
185
186 local_irq_save(flags);
187 uart->ustcnt &= ~USTCNT_TXEN;
188 local_irq_restore(flags);
189}
190
191static int rs_put_char(char ch)
192{
193 int flags, loops = 0;
194
195 local_irq_save(flags);
196
197 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
198 loops++;
199 udelay(5);
200 }
201
202 UTX_TXDATA = ch;
203 udelay(5);
204 local_irq_restore(flags);
205 return 1;
206}
207
208static void rs_start(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_start"))
215 return;
216
217 local_irq_save(flags);
218 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
219#ifdef USE_INTS
220 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
221#else
222 uart->ustcnt |= USTCNT_TXEN;
223#endif
224 }
225 local_irq_restore(flags);
226}
227
228/* Drop into either the boot monitor or kadb upon receiving a break
229 * from keyboard/console input.
230 */
231static void batten_down_hatches(void)
232{
233 /* Drop into the debugger */
234}
235
236static void status_handle(struct m68k_serial *info, unsigned short status)
237{
238#if 0
239 if(status & DCD) {
240 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
241 ((info->curregs[3] & AUTO_ENAB)==0)) {
242 info->curregs[3] |= AUTO_ENAB;
243 info->pendregs[3] |= AUTO_ENAB;
244 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
245 }
246 } else {
247 if((info->curregs[3] & AUTO_ENAB)) {
248 info->curregs[3] &= ~AUTO_ENAB;
249 info->pendregs[3] &= ~AUTO_ENAB;
250 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
251 }
252 }
253#endif
254 /* If this is console input and this is a
255 * 'break asserted' status change interrupt
256 * see if we can drop into the debugger
257 */
258 if((status & URX_BREAK) && info->break_abort)
259 batten_down_hatches();
260 return;
261}
262
263static void receive_chars(struct m68k_serial *info, unsigned short rx)
264{
265 struct tty_struct *tty = info->port.tty;
266 m68328_uart *uart = &uart_addr[info->line];
267 unsigned char ch, flag;
268
269 /*
270 * This do { } while() loop will get ALL chars out of Rx FIFO
271 */
272#ifndef CONFIG_XCOPILOT_BUGS
273 do {
274#endif
275 ch = GET_FIELD(rx, URX_RXDATA);
276
277 if(info->is_cons) {
278 if(URX_BREAK & rx) { /* whee, break received */
279 status_handle(info, rx);
280 return;
281#ifdef CONFIG_MAGIC_SYSRQ
282 } else if (ch == 0x10) { /* ^P */
283 show_state();
284 show_free_areas();
285 show_buffers();
286/* show_net_buffers(); */
287 return;
288 } else if (ch == 0x12) { /* ^R */
289 emergency_restart();
290 return;
291#endif /* CONFIG_MAGIC_SYSRQ */
292 }
293 }
294
295 if(!tty)
296 goto clear_and_exit;
297
298 flag = TTY_NORMAL;
299
300 if(rx & URX_PARITY_ERROR) {
301 flag = TTY_PARITY;
302 status_handle(info, rx);
303 } else if(rx & URX_OVRUN) {
304 flag = TTY_OVERRUN;
305 status_handle(info, rx);
306 } else if(rx & URX_FRAME_ERROR) {
307 flag = TTY_FRAME;
308 status_handle(info, rx);
309 }
310 tty_insert_flip_char(tty, ch, flag);
311#ifndef CONFIG_XCOPILOT_BUGS
312 } while((rx = uart->urx.w) & URX_DATA_READY);
313#endif
314
315 tty_schedule_flip(tty);
316
317clear_and_exit:
318 return;
319}
320
321static void transmit_chars(struct m68k_serial *info)
322{
323 m68328_uart *uart = &uart_addr[info->line];
324
325 if (info->x_char) {
326 /* Send next char */
327 uart->utx.b.txdata = info->x_char;
328 info->x_char = 0;
329 goto clear_and_return;
330 }
331
332 if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
333 /* That's peculiar... TX ints off */
334 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
335 goto clear_and_return;
336 }
337
338 /* Send char */
339 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
340 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
341 info->xmit_cnt--;
342
343 if (info->xmit_cnt < WAKEUP_CHARS)
344 schedule_work(&info->tqueue);
345
346 if(info->xmit_cnt <= 0) {
347 /* All done for now... TX ints off */
348 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
349 goto clear_and_return;
350 }
351
352clear_and_return:
353 /* Clear interrupt (should be auto)*/
354 return;
355}
356
357/*
358 * This is the serial driver's generic interrupt routine
359 */
360irqreturn_t rs_interrupt(int irq, void *dev_id)
361{
362 struct m68k_serial *info = dev_id;
363 m68328_uart *uart;
364 unsigned short rx;
365 unsigned short tx;
366
367 uart = &uart_addr[info->line];
368 rx = uart->urx.w;
369
370#ifdef USE_INTS
371 tx = uart->utx.w;
372
373 if (rx & URX_DATA_READY) receive_chars(info, rx);
374 if (tx & UTX_TX_AVAIL) transmit_chars(info);
375#else
376 receive_chars(info, rx);
377#endif
378 return IRQ_HANDLED;
379}
380
381static void do_softint(struct work_struct *work)
382{
383 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
384 struct tty_struct *tty;
385
386 tty = info->port.tty;
387 if (!tty)
388 return;
389#if 0
390 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
391 tty_wakeup(tty);
392 }
393#endif
394}
395
396/*
397 * This routine is called from the scheduler tqueue when the interrupt
398 * routine has signalled that a hangup has occurred. The path of
399 * hangup processing is:
400 *
401 * serial interrupt routine -> (scheduler tqueue) ->
402 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
403 *
404 */
405static void do_serial_hangup(struct work_struct *work)
406{
407 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
408 struct tty_struct *tty;
409
410 tty = info->port.tty;
411 if (!tty)
412 return;
413
414 tty_hangup(tty);
415}
416
417
418static int startup(struct m68k_serial * info)
419{
420 m68328_uart *uart = &uart_addr[info->line];
421 unsigned long flags;
422
423 if (info->flags & S_INITIALIZED)
424 return 0;
425
426 if (!info->xmit_buf) {
427 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
428 if (!info->xmit_buf)
429 return -ENOMEM;
430 }
431
432 local_irq_save(flags);
433
434 /*
435 * Clear the FIFO buffers and disable them
436 * (they will be reenabled in change_speed())
437 */
438
439 uart->ustcnt = USTCNT_UEN;
440 info->xmit_fifo_size = 1;
441 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
442 (void)uart->urx.w;
443
444 /*
445 * Finally, enable sequencing and interrupts
446 */
447#ifdef USE_INTS
448 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
449 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
450#else
451 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
452#endif
453
454 if (info->port.tty)
455 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
456 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
457
458 /*
459 * and set the speed of the serial port
460 */
461
462 change_speed(info);
463
464 info->flags |= S_INITIALIZED;
465 local_irq_restore(flags);
466 return 0;
467}
468
469/*
470 * This routine will shutdown a serial port; interrupts are disabled, and
471 * DTR is dropped if the hangup on close termio flag is on.
472 */
473static void shutdown(struct m68k_serial * info)
474{
475 m68328_uart *uart = &uart_addr[info->line];
476 unsigned long flags;
477
478 uart->ustcnt = 0; /* All off! */
479 if (!(info->flags & S_INITIALIZED))
480 return;
481
482 local_irq_save(flags);
483
484 if (info->xmit_buf) {
485 free_page((unsigned long) info->xmit_buf);
486 info->xmit_buf = 0;
487 }
488
489 if (info->port.tty)
490 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
491
492 info->flags &= ~S_INITIALIZED;
493 local_irq_restore(flags);
494}
495
496struct {
497 int divisor, prescale;
498}
499#ifndef CONFIG_M68VZ328
500 hw_baud_table[18] = {
501 {0,0}, /* 0 */
502 {0,0}, /* 50 */
503 {0,0}, /* 75 */
504 {0,0}, /* 110 */
505 {0,0}, /* 134 */
506 {0,0}, /* 150 */
507 {0,0}, /* 200 */
508 {7,0x26}, /* 300 */
509 {6,0x26}, /* 600 */
510 {5,0x26}, /* 1200 */
511 {0,0}, /* 1800 */
512 {4,0x26}, /* 2400 */
513 {3,0x26}, /* 4800 */
514 {2,0x26}, /* 9600 */
515 {1,0x26}, /* 19200 */
516 {0,0x26}, /* 38400 */
517 {1,0x38}, /* 57600 */
518 {0,0x38}, /* 115200 */
519};
520#else
521 hw_baud_table[18] = {
522 {0,0}, /* 0 */
523 {0,0}, /* 50 */
524 {0,0}, /* 75 */
525 {0,0}, /* 110 */
526 {0,0}, /* 134 */
527 {0,0}, /* 150 */
528 {0,0}, /* 200 */
529 {0,0}, /* 300 */
530 {7,0x26}, /* 600 */
531 {6,0x26}, /* 1200 */
532 {0,0}, /* 1800 */
533 {5,0x26}, /* 2400 */
534 {4,0x26}, /* 4800 */
535 {3,0x26}, /* 9600 */
536 {2,0x26}, /* 19200 */
537 {1,0x26}, /* 38400 */
538 {0,0x26}, /* 57600 */
539 {1,0x38}, /* 115200 */
540};
541#endif
542/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
543
544/*
545 * This routine is called to set the UART divisor registers to match
546 * the specified baud rate for a serial port.
547 */
548static void change_speed(struct m68k_serial *info)
549{
550 m68328_uart *uart = &uart_addr[info->line];
551 unsigned short port;
552 unsigned short ustcnt;
553 unsigned cflag;
554 int i;
555
556 if (!info->port.tty || !info->port.tty->termios)
557 return;
558 cflag = info->port.tty->termios->c_cflag;
559 if (!(port = info->port))
560 return;
561
562 ustcnt = uart->ustcnt;
563 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
564
565 i = cflag & CBAUD;
566 if (i & CBAUDEX) {
567 i = (i & ~CBAUDEX) + B38400;
568 }
569
570 info->baud = baud_table[i];
571 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
572 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
573
574 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
575
576 if ((cflag & CSIZE) == CS8)
577 ustcnt |= USTCNT_8_7;
578
579 if (cflag & CSTOPB)
580 ustcnt |= USTCNT_STOP;
581
582 if (cflag & PARENB)
583 ustcnt |= USTCNT_PARITYEN;
584 if (cflag & PARODD)
585 ustcnt |= USTCNT_ODD_EVEN;
586
587#ifdef CONFIG_SERIAL_68328_RTS_CTS
588 if (cflag & CRTSCTS) {
589 uart->utx.w &= ~ UTX_NOCTS;
590 } else {
591 uart->utx.w |= UTX_NOCTS;
592 }
593#endif
594
595 ustcnt |= USTCNT_TXEN;
596
597 uart->ustcnt = ustcnt;
598 return;
599}
600
601/*
602 * Fair output driver allows a process to speak.
603 */
604static void rs_fair_output(void)
605{
606 int left; /* Output no more than that */
607 unsigned long flags;
608 struct m68k_serial *info = &m68k_soft[0];
609 char c;
610
611 if (info == 0) return;
612 if (info->xmit_buf == 0) return;
613
614 local_irq_save(flags);
615 left = info->xmit_cnt;
616 while (left != 0) {
617 c = info->xmit_buf[info->xmit_tail];
618 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
619 info->xmit_cnt--;
620 local_irq_restore(flags);
621
622 rs_put_char(c);
623
624 local_irq_save(flags);
625 left = min(info->xmit_cnt, left-1);
626 }
627
628 /* Last character is being transmitted now (hopefully). */
629 udelay(5);
630
631 local_irq_restore(flags);
632 return;
633}
634
635/*
636 * m68k_console_print is registered for printk.
637 */
638void console_print_68328(const char *p)
639{
640 char c;
641
642 while((c=*(p++)) != 0) {
643 if(c == '\n')
644 rs_put_char('\r');
645 rs_put_char(c);
646 }
647
648 /* Comment this if you want to have a strict interrupt-driven output */
649 rs_fair_output();
650
651 return;
652}
653
654static void rs_set_ldisc(struct tty_struct *tty)
655{
656 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
657
658 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
659 return;
660
661 info->is_cons = (tty->termios->c_line == N_TTY);
662
663 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
664}
665
666static void rs_flush_chars(struct tty_struct *tty)
667{
668 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
669 m68328_uart *uart = &uart_addr[info->line];
670 unsigned long flags;
671
672 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
673 return;
674#ifndef USE_INTS
675 for(;;) {
676#endif
677
678 /* Enable transmitter */
679 local_irq_save(flags);
680
681 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
682 !info->xmit_buf) {
683 local_irq_restore(flags);
684 return;
685 }
686
687#ifdef USE_INTS
688 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
689#else
690 uart->ustcnt |= USTCNT_TXEN;
691#endif
692
693#ifdef USE_INTS
694 if (uart->utx.w & UTX_TX_AVAIL) {
695#else
696 if (1) {
697#endif
698 /* Send char */
699 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
700 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
701 info->xmit_cnt--;
702 }
703
704#ifndef USE_INTS
705 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
706 }
707#endif
708 local_irq_restore(flags);
709}
710
711extern void console_printn(const char * b, int count);
712
713static int rs_write(struct tty_struct * tty,
714 const unsigned char *buf, int count)
715{
716 int c, total = 0;
717 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
718 m68328_uart *uart = &uart_addr[info->line];
719 unsigned long flags;
720
721 if (serial_paranoia_check(info, tty->name, "rs_write"))
722 return 0;
723
724 if (!tty || !info->xmit_buf)
725 return 0;
726
727 local_save_flags(flags);
728 while (1) {
729 local_irq_disable();
730 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
731 SERIAL_XMIT_SIZE - info->xmit_head));
732 local_irq_restore(flags);
733
734 if (c <= 0)
735 break;
736
737 memcpy(info->xmit_buf + info->xmit_head, buf, c);
738
739 local_irq_disable();
740 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
741 info->xmit_cnt += c;
742 local_irq_restore(flags);
743 buf += c;
744 count -= c;
745 total += c;
746 }
747
748 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
749 /* Enable transmitter */
750 local_irq_disable();
751#ifndef USE_INTS
752 while(info->xmit_cnt) {
753#endif
754
755 uart->ustcnt |= USTCNT_TXEN;
756#ifdef USE_INTS
757 uart->ustcnt |= USTCNT_TX_INTR_MASK;
758#else
759 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
760#endif
761 if (uart->utx.w & UTX_TX_AVAIL) {
762 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
763 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
764 info->xmit_cnt--;
765 }
766
767#ifndef USE_INTS
768 }
769#endif
770 local_irq_restore(flags);
771 }
772
773 return total;
774}
775
776static int rs_write_room(struct tty_struct *tty)
777{
778 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
779 int ret;
780
781 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
782 return 0;
783 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
784 if (ret < 0)
785 ret = 0;
786 return ret;
787}
788
789static int rs_chars_in_buffer(struct tty_struct *tty)
790{
791 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
792
793 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
794 return 0;
795 return info->xmit_cnt;
796}
797
798static void rs_flush_buffer(struct tty_struct *tty)
799{
800 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
801 unsigned long flags;
802
803 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
804 return;
805 local_irq_save(flags);
806 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
807 local_irq_restore(flags);
808 tty_wakeup(tty);
809}
810
811/*
812 * ------------------------------------------------------------
813 * rs_throttle()
814 *
815 * This routine is called by the upper-layer tty layer to signal that
816 * incoming characters should be throttled.
817 * ------------------------------------------------------------
818 */
819static void rs_throttle(struct tty_struct * tty)
820{
821 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
822
823 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
824 return;
825
826 if (I_IXOFF(tty))
827 info->x_char = STOP_CHAR(tty);
828
829 /* Turn off RTS line (do this atomic) */
830}
831
832static void rs_unthrottle(struct tty_struct * tty)
833{
834 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
835
836 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
837 return;
838
839 if (I_IXOFF(tty)) {
840 if (info->x_char)
841 info->x_char = 0;
842 else
843 info->x_char = START_CHAR(tty);
844 }
845
846 /* Assert RTS line (do this atomic) */
847}
848
849/*
850 * ------------------------------------------------------------
851 * rs_ioctl() and friends
852 * ------------------------------------------------------------
853 */
854
855static int get_serial_info(struct m68k_serial * info,
856 struct serial_struct * retinfo)
857{
858 struct serial_struct tmp;
859
860 if (!retinfo)
861 return -EFAULT;
862 memset(&tmp, 0, sizeof(tmp));
863 tmp.type = info->type;
864 tmp.line = info->line;
865 tmp.port = info->port;
866 tmp.irq = info->irq;
867 tmp.flags = info->flags;
868 tmp.baud_base = info->baud_base;
869 tmp.close_delay = info->close_delay;
870 tmp.closing_wait = info->closing_wait;
871 tmp.custom_divisor = info->custom_divisor;
872 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
873 return -EFAULT;
874
875 return 0;
876}
877
878static int set_serial_info(struct m68k_serial * info,
879 struct serial_struct * new_info)
880{
881 struct serial_struct new_serial;
882 struct m68k_serial old_info;
883 int retval = 0;
884
885 if (!new_info)
886 return -EFAULT;
887 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
888 return -EFAULT;
889 old_info = *info;
890
891 if (!capable(CAP_SYS_ADMIN)) {
892 if ((new_serial.baud_base != info->baud_base) ||
893 (new_serial.type != info->type) ||
894 (new_serial.close_delay != info->close_delay) ||
895 ((new_serial.flags & ~S_USR_MASK) !=
896 (info->flags & ~S_USR_MASK)))
897 return -EPERM;
898 info->flags = ((info->flags & ~S_USR_MASK) |
899 (new_serial.flags & S_USR_MASK));
900 info->custom_divisor = new_serial.custom_divisor;
901 goto check_and_exit;
902 }
903
904 if (info->count > 1)
905 return -EBUSY;
906
907 /*
908 * OK, past this point, all the error checking has been done.
909 * At this point, we start making changes.....
910 */
911
912 info->baud_base = new_serial.baud_base;
913 info->flags = ((info->flags & ~S_FLAGS) |
914 (new_serial.flags & S_FLAGS));
915 info->type = new_serial.type;
916 info->close_delay = new_serial.close_delay;
917 info->closing_wait = new_serial.closing_wait;
918
919check_and_exit:
920 retval = startup(info);
921 return retval;
922}
923
924/*
925 * get_lsr_info - get line status register info
926 *
927 * Purpose: Let user call ioctl() to get info when the UART physically
928 * is emptied. On bus types like RS485, the transmitter must
929 * release the bus after transmitting. This must be done when
930 * the transmit shift register is empty, not be done when the
931 * transmit holding register is empty. This functionality
932 * allows an RS485 driver to be written in user space.
933 */
934static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
935{
936#ifdef CONFIG_SERIAL_68328_RTS_CTS
937 m68328_uart *uart = &uart_addr[info->line];
938#endif
939 unsigned char status;
940 unsigned long flags;
941
942 local_irq_save(flags);
943#ifdef CONFIG_SERIAL_68328_RTS_CTS
944 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
945#else
946 status = 0;
947#endif
948 local_irq_restore(flags);
949 return put_user(status, value);
950}
951
952/*
953 * This routine sends a break character out the serial port.
954 */
955static void send_break(struct m68k_serial * info, unsigned int duration)
956{
957 m68328_uart *uart = &uart_addr[info->line];
958 unsigned long flags;
959 if (!info->port)
960 return;
961 local_irq_save(flags);
962#ifdef USE_INTS
963 uart->utx.w |= UTX_SEND_BREAK;
964 msleep_interruptible(duration);
965 uart->utx.w &= ~UTX_SEND_BREAK;
966#endif
967 local_irq_restore(flags);
968}
969
970static int rs_ioctl(struct tty_struct *tty, struct file * file,
971 unsigned int cmd, unsigned long arg)
972{
973 int error;
974 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
975 int retval;
976
977 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
978 return -ENODEV;
979
980 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
981 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
982 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
983 if (tty->flags & (1 << TTY_IO_ERROR))
984 return -EIO;
985 }
986
987 switch (cmd) {
988 case TCSBRK: /* SVID version: non-zero arg --> no break */
989 retval = tty_check_change(tty);
990 if (retval)
991 return retval;
992 tty_wait_until_sent(tty, 0);
993 if (!arg)
994 send_break(info, 250); /* 1/4 second */
995 return 0;
996 case TCSBRKP: /* support for POSIX tcsendbreak() */
997 retval = tty_check_change(tty);
998 if (retval)
999 return retval;
1000 tty_wait_until_sent(tty, 0);
1001 send_break(info, arg ? arg*(100) : 250);
1002 return 0;
1003 case TIOCGSERIAL:
1004 return get_serial_info(info,
1005 (struct serial_struct *) arg);
1006 case TIOCSSERIAL:
1007 return set_serial_info(info,
1008 (struct serial_struct *) arg);
1009 case TIOCSERGETLSR: /* Get line status register */
1010 return get_lsr_info(info, (unsigned int *) arg);
1011 case TIOCSERGSTRUCT:
1012 if (copy_to_user((struct m68k_serial *) arg,
1013 info, sizeof(struct m68k_serial)))
1014 return -EFAULT;
1015 return 0;
1016 default:
1017 return -ENOIOCTLCMD;
1018 }
1019 return 0;
1020}
1021
1022static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1023{
1024 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1025
1026 change_speed(info);
1027
1028 if ((old_termios->c_cflag & CRTSCTS) &&
1029 !(tty->termios->c_cflag & CRTSCTS)) {
1030 tty->hw_stopped = 0;
1031 rs_start(tty);
1032 }
1033
1034}
1035
1036/*
1037 * ------------------------------------------------------------
1038 * rs_close()
1039 *
1040 * This routine is called when the serial port gets closed. First, we
1041 * wait for the last remaining data to be sent. Then, we unlink its
1042 * S structure from the interrupt chain if necessary, and we free
1043 * that IRQ if nothing is left in the chain.
1044 * ------------------------------------------------------------
1045 */
1046static void rs_close(struct tty_struct *tty, struct file * filp)
1047{
1048 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1049 m68328_uart *uart = &uart_addr[info->line];
1050 unsigned long flags;
1051
1052 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1053 return;
1054
1055 local_irq_save(flags);
1056
1057 if (tty_hung_up_p(filp)) {
1058 local_irq_restore(flags);
1059 return;
1060 }
1061
1062 if ((tty->count == 1) && (info->count != 1)) {
1063 /*
1064 * Uh, oh. tty->count is 1, which means that the tty
1065 * structure will be freed. Info->count should always
1066 * be one in these conditions. If it's greater than
1067 * one, we've got real problems, since it means the
1068 * serial port won't be shutdown.
1069 */
1070 printk("rs_close: bad serial port count; tty->count is 1, "
1071 "info->count is %d\n", info->count);
1072 info->count = 1;
1073 }
1074 if (--info->count < 0) {
1075 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1076 info->line, info->count);
1077 info->count = 0;
1078 }
1079 if (info->count) {
1080 local_irq_restore(flags);
1081 return;
1082 }
1083 info->flags |= S_CLOSING;
1084 /*
1085 * Now we wait for the transmit buffer to clear; and we notify
1086 * the line discipline to only process XON/XOFF characters.
1087 */
1088 tty->closing = 1;
1089 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1090 tty_wait_until_sent(tty, info->closing_wait);
1091 /*
1092 * At this point we stop accepting input. To do this, we
1093 * disable the receive line status interrupts, and tell the
1094 * interrupt driver to stop checking the data ready bit in the
1095 * line status register.
1096 */
1097
1098 uart->ustcnt &= ~USTCNT_RXEN;
1099 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1100
1101 shutdown(info);
1102 rs_flush_buffer(tty);
1103
1104 tty_ldisc_flush(tty);
1105 tty->closing = 0;
1106 info->event = 0;
1107 info->port.tty = NULL;
1108#warning "This is not and has never been valid so fix it"
1109#if 0
1110 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1111 if (tty->ldisc.close)
1112 (tty->ldisc.close)(tty);
1113 tty->ldisc = ldiscs[N_TTY];
1114 tty->termios->c_line = N_TTY;
1115 if (tty->ldisc.open)
1116 (tty->ldisc.open)(tty);
1117 }
1118#endif
1119 if (info->blocked_open) {
1120 if (info->close_delay) {
1121 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1122 }
1123 wake_up_interruptible(&info->open_wait);
1124 }
1125 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1126 wake_up_interruptible(&info->close_wait);
1127 local_irq_restore(flags);
1128}
1129
1130/*
1131 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1132 */
1133void rs_hangup(struct tty_struct *tty)
1134{
1135 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1136
1137 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1138 return;
1139
1140 rs_flush_buffer(tty);
1141 shutdown(info);
1142 info->event = 0;
1143 info->count = 0;
1144 info->flags &= ~S_NORMAL_ACTIVE;
1145 info->port.tty = NULL;
1146 wake_up_interruptible(&info->open_wait);
1147}
1148
1149/*
1150 * ------------------------------------------------------------
1151 * rs_open() and friends
1152 * ------------------------------------------------------------
1153 */
1154static int block_til_ready(struct tty_struct *tty, struct file * filp,
1155 struct m68k_serial *info)
1156{
1157 DECLARE_WAITQUEUE(wait, current);
1158 int retval;
1159 int do_clocal = 0;
1160
1161 /*
1162 * If the device is in the middle of being closed, then block
1163 * until it's done, and then try again.
1164 */
1165 if (info->flags & S_CLOSING) {
1166 interruptible_sleep_on(&info->close_wait);
1167#ifdef SERIAL_DO_RESTART
1168 if (info->flags & S_HUP_NOTIFY)
1169 return -EAGAIN;
1170 else
1171 return -ERESTARTSYS;
1172#else
1173 return -EAGAIN;
1174#endif
1175 }
1176
1177 /*
1178 * If non-blocking mode is set, or the port is not enabled,
1179 * then make the check up front and then exit.
1180 */
1181 if ((filp->f_flags & O_NONBLOCK) ||
1182 (tty->flags & (1 << TTY_IO_ERROR))) {
1183 info->flags |= S_NORMAL_ACTIVE;
1184 return 0;
1185 }
1186
1187 if (tty->termios->c_cflag & CLOCAL)
1188 do_clocal = 1;
1189
1190 /*
1191 * Block waiting for the carrier detect and the line to become
1192 * free (i.e., not in use by the callout). While we are in
1193 * this loop, info->count is dropped by one, so that
1194 * rs_close() knows when to free things. We restore it upon
1195 * exit, either normal or abnormal.
1196 */
1197 retval = 0;
1198 add_wait_queue(&info->open_wait, &wait);
1199
1200 info->count--;
1201 info->blocked_open++;
1202 while (1) {
1203 local_irq_disable();
1204 m68k_rtsdtr(info, 1);
1205 local_irq_enable();
1206 current->state = TASK_INTERRUPTIBLE;
1207 if (tty_hung_up_p(filp) ||
1208 !(info->flags & S_INITIALIZED)) {
1209#ifdef SERIAL_DO_RESTART
1210 if (info->flags & S_HUP_NOTIFY)
1211 retval = -EAGAIN;
1212 else
1213 retval = -ERESTARTSYS;
1214#else
1215 retval = -EAGAIN;
1216#endif
1217 break;
1218 }
1219 if (!(info->flags & S_CLOSING) && do_clocal)
1220 break;
1221 if (signal_pending(current)) {
1222 retval = -ERESTARTSYS;
1223 break;
1224 }
1225 tty_unlock();
1226 schedule();
1227 tty_lock();
1228 }
1229 current->state = TASK_RUNNING;
1230 remove_wait_queue(&info->open_wait, &wait);
1231 if (!tty_hung_up_p(filp))
1232 info->count++;
1233 info->blocked_open--;
1234
1235 if (retval)
1236 return retval;
1237 info->flags |= S_NORMAL_ACTIVE;
1238 return 0;
1239}
1240
1241/*
1242 * This routine is called whenever a serial port is opened. It
1243 * enables interrupts for a serial port, linking in its S structure into
1244 * the IRQ chain. It also performs the serial-specific
1245 * initialization for the tty structure.
1246 */
1247int rs_open(struct tty_struct *tty, struct file * filp)
1248{
1249 struct m68k_serial *info;
1250 int retval, line;
1251
1252 line = tty->index;
1253
1254 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1255 return -ENODEV;
1256
1257 info = &m68k_soft[line];
1258
1259 if (serial_paranoia_check(info, tty->name, "rs_open"))
1260 return -ENODEV;
1261
1262 info->count++;
1263 tty->driver_data = info;
1264 info->port.tty = tty;
1265
1266 /*
1267 * Start up serial port
1268 */
1269 retval = startup(info);
1270 if (retval)
1271 return retval;
1272
1273 return block_til_ready(tty, filp, info);
1274}
1275
1276/* Finally, routines used to initialize the serial driver. */
1277
1278static void show_serial_version(void)
1279{
1280 printk("MC68328 serial driver version 1.00\n");
1281}
1282
1283static const struct tty_operations rs_ops = {
1284 .open = rs_open,
1285 .close = rs_close,
1286 .write = rs_write,
1287 .flush_chars = rs_flush_chars,
1288 .write_room = rs_write_room,
1289 .chars_in_buffer = rs_chars_in_buffer,
1290 .flush_buffer = rs_flush_buffer,
1291 .ioctl = rs_ioctl,
1292 .throttle = rs_throttle,
1293 .unthrottle = rs_unthrottle,
1294 .set_termios = rs_set_termios,
1295 .stop = rs_stop,
1296 .start = rs_start,
1297 .hangup = rs_hangup,
1298 .set_ldisc = rs_set_ldisc,
1299};
1300
1301/* rs_init inits the driver */
1302static int __init
1303rs68328_init(void)
1304{
1305 int flags, i;
1306 struct m68k_serial *info;
1307
1308 serial_driver = alloc_tty_driver(NR_PORTS);
1309 if (!serial_driver)
1310 return -ENOMEM;
1311
1312 show_serial_version();
1313
1314 /* Initialize the tty_driver structure */
1315 /* SPARC: Not all of this is exactly right for us. */
1316
1317 serial_driver->name = "ttyS";
1318 serial_driver->major = TTY_MAJOR;
1319 serial_driver->minor_start = 64;
1320 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1321 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1322 serial_driver->init_termios = tty_std_termios;
1323 serial_driver->init_termios.c_cflag =
1324 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1325 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1326 tty_set_operations(serial_driver, &rs_ops);
1327
1328 if (tty_register_driver(serial_driver)) {
1329 put_tty_driver(serial_driver);
1330 printk(KERN_ERR "Couldn't register serial driver\n");
1331 return -ENOMEM;
1332 }
1333
1334 local_irq_save(flags);
1335
1336 for(i=0;i<NR_PORTS;i++) {
1337
1338 info = &m68k_soft[i];
1339 info->magic = SERIAL_MAGIC;
1340 info->port = (int) &uart_addr[i];
1341 info->port.tty = NULL;
1342 info->irq = uart_irqs[i];
1343 info->custom_divisor = 16;
1344 info->close_delay = 50;
1345 info->closing_wait = 3000;
1346 info->x_char = 0;
1347 info->event = 0;
1348 info->count = 0;
1349 info->blocked_open = 0;
1350 INIT_WORK(&info->tqueue, do_softint);
1351 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
1352 init_waitqueue_head(&info->open_wait);
1353 init_waitqueue_head(&info->close_wait);
1354 info->line = i;
1355 info->is_cons = 1; /* Means shortcuts work */
1356
1357 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1358 info->port, info->irq);
1359 printk(" is a builtin MC68328 UART\n");
1360
1361#ifdef CONFIG_M68VZ328
1362 if (i > 0 )
1363 PJSEL &= 0xCF; /* PSW enable second port output */
1364#endif
1365
1366 if (request_irq(uart_irqs[i],
1367 rs_interrupt,
1368 IRQF_DISABLED,
1369 "M68328_UART", info))
1370 panic("Unable to attach 68328 serial interrupt\n");
1371 }
1372 local_irq_restore(flags);
1373 return 0;
1374}
1375
1376module_init(rs68328_init);
1377
1378
1379
1380static void m68328_set_baud(void)
1381{
1382 unsigned short ustcnt;
1383 int i;
1384
1385 ustcnt = USTCNT;
1386 USTCNT = ustcnt & ~USTCNT_TXEN;
1387
1388again:
1389 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1390 if (baud_table[i] == m68328_console_baud)
1391 break;
1392 if (i >= ARRAY_SIZE(baud_table)) {
1393 m68328_console_baud = 9600;
1394 goto again;
1395 }
1396
1397 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1398 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1399 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1400 ustcnt |= USTCNT_8_7;
1401 ustcnt |= USTCNT_TXEN;
1402 USTCNT = ustcnt;
1403 m68328_console_initted = 1;
1404 return;
1405}
1406
1407
1408int m68328_console_setup(struct console *cp, char *arg)
1409{
1410 int i, n = CONSOLE_BAUD_RATE;
1411
1412 if (!cp)
1413 return(-1);
1414
1415 if (arg)
1416 n = simple_strtoul(arg,NULL,0);
1417
1418 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1419 if (baud_table[i] == n)
1420 break;
1421 if (i < ARRAY_SIZE(baud_table)) {
1422 m68328_console_baud = n;
1423 m68328_console_cbaud = 0;
1424 if (i > 15) {
1425 m68328_console_cbaud |= CBAUDEX;
1426 i -= 15;
1427 }
1428 m68328_console_cbaud |= i;
1429 }
1430
1431 m68328_set_baud(); /* make sure baud rate changes */
1432 return(0);
1433}
1434
1435
1436static struct tty_driver *m68328_console_device(struct console *c, int *index)
1437{
1438 *index = c->index;
1439 return serial_driver;
1440}
1441
1442
1443void m68328_console_write (struct console *co, const char *str,
1444 unsigned int count)
1445{
1446 if (!m68328_console_initted)
1447 m68328_set_baud();
1448 while (count--) {
1449 if (*str == '\n')
1450 rs_put_char('\r');
1451 rs_put_char( *str++ );
1452 }
1453}
1454
1455
1456static struct console m68328_driver = {
1457 .name = "ttyS",
1458 .write = m68328_console_write,
1459 .device = m68328_console_device,
1460 .setup = m68328_console_setup,
1461 .flags = CON_PRINTBUFFER,
1462 .index = -1,
1463};
1464
1465
1466static int __init m68328_console_init(void)
1467{
1468 register_console(&m68328_driver);
1469 return 0;
1470}
1471
1472console_initcall(m68328_console_init);
diff --git a/drivers/tty/serial/68328serial.h b/drivers/tty/serial/68328serial.h
new file mode 100644
index 000000000000..664ceb0a158c
--- /dev/null
+++ b/drivers/tty/serial/68328serial.h
@@ -0,0 +1,188 @@
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
15struct serial_struct {
16 int type;
17 int line;
18 int port;
19 int irq;
20 int flags;
21 int xmit_fifo_size;
22 int custom_divisor;
23 int baud_base;
24 unsigned short close_delay;
25 char reserved_char[2];
26 int hub6; /* FIXME: We don't have AT&T Hub6 boards! */
27 unsigned short closing_wait; /* time to wait before closing */
28 unsigned short closing_wait2; /* no longer used... */
29 int reserved[4];
30};
31
32/*
33 * For the close wait times, 0 means wait forever for serial port to
34 * flush its output. 65535 means don't wait at all.
35 */
36#define S_CLOSING_WAIT_INF 0
37#define S_CLOSING_WAIT_NONE 65535
38
39/*
40 * Definitions for S_struct (and serial_struct) flags field
41 */
42#define S_HUP_NOTIFY 0x0001 /* Notify getty on hangups and closes
43 on the callout port */
44#define S_FOURPORT 0x0002 /* Set OU1, OUT2 per AST Fourport settings */
45#define S_SAK 0x0004 /* Secure Attention Key (Orange book) */
46#define S_SPLIT_TERMIOS 0x0008 /* Separate termios for dialin/callout */
47
48#define S_SPD_MASK 0x0030
49#define S_SPD_HI 0x0010 /* Use 56000 instead of 38400 bps */
50
51#define S_SPD_VHI 0x0020 /* Use 115200 instead of 38400 bps */
52#define S_SPD_CUST 0x0030 /* Use user-specified divisor */
53
54#define S_SKIP_TEST 0x0040 /* Skip UART test during autoconfiguration */
55#define S_AUTO_IRQ 0x0080 /* Do automatic IRQ during autoconfiguration */
56#define S_SESSION_LOCKOUT 0x0100 /* Lock out cua opens based on session */
57#define S_PGRP_LOCKOUT 0x0200 /* Lock out cua opens based on pgrp */
58#define S_CALLOUT_NOHUP 0x0400 /* Don't do hangups for cua device */
59
60#define S_FLAGS 0x0FFF /* Possible legal S flags */
61#define S_USR_MASK 0x0430 /* Legal flags that non-privileged
62 * users can set or reset */
63
64/* Internal flags used only by kernel/chr_drv/serial.c */
65#define S_INITIALIZED 0x80000000 /* Serial port was initialized */
66#define S_CALLOUT_ACTIVE 0x40000000 /* Call out device is active */
67#define S_NORMAL_ACTIVE 0x20000000 /* Normal device is active */
68#define S_BOOT_AUTOCONF 0x10000000 /* Autoconfigure port on bootup */
69#define S_CLOSING 0x08000000 /* Serial port is closing */
70#define S_CTS_FLOW 0x04000000 /* Do CTS flow control */
71#define S_CHECK_CD 0x02000000 /* i.e., CLOCAL */
72
73/* Software state per channel */
74
75#ifdef __KERNEL__
76
77/*
78 * I believe this is the optimal setting that reduces the number of interrupts.
79 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
80 * if that bothers you), but in most cases it will not, since we try to
81 * transmit characters every time rs_interrupt is called. Thus, quite often
82 * you'll see that a receive interrupt occures before the transmit one.
83 * -- Vladimir Gurevich
84 */
85#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
86
87/*
88 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
89 * "Old data interrupt" which occures whenever the data stay in the FIFO
90 * longer than 30 bits time. This allows us to use FIFO without compromising
91 * latency. '328 does not have this feature and without the real 328-based
92 * board I would assume that RXRE is the safest setting.
93 *
94 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
95 * interrupts. RXFE (receive queue full) causes the system to lose data
96 * at least at 115200 baud
97 *
98 * If your board is busy doing other stuff, you might consider to use
99 * RXRE (data ready intrrupt) instead.
100 *
101 * The other option is to make these INTR masks run-time configurable, so
102 * that people can dynamically adapt them according to the current usage.
103 * -- Vladimir Gurevich
104 */
105
106/* (es) */
107#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
108#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
109#elif defined(CONFIG_M68328)
110#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
111#else
112#error Please, define the Rx interrupt events for your CPU
113#endif
114/* (/es) */
115
116/*
117 * This is our internal structure for each serial port's state.
118 *
119 * Many fields are paralleled by the structure used by the serial_struct
120 * structure.
121 *
122 * For definitions of the flags field, see tty.h
123 */
124
125struct m68k_serial {
126 char soft_carrier; /* Use soft carrier on this channel */
127 char break_abort; /* Is serial console in, so process brk/abrt */
128 char is_cons; /* Is this our console. */
129
130 /* We need to know the current clock divisor
131 * to read the bps rate the chip has currently
132 * loaded.
133 */
134 unsigned char clk_divisor; /* May be 1, 16, 32, or 64 */
135 int baud;
136 int magic;
137 int baud_base;
138 int port;
139 int irq;
140 int flags; /* defined in tty.h */
141 int type; /* UART type */
142 struct tty_struct *tty;
143 int read_status_mask;
144 int ignore_status_mask;
145 int timeout;
146 int xmit_fifo_size;
147 int custom_divisor;
148 int x_char; /* xon/xoff character */
149 int close_delay;
150 unsigned short closing_wait;
151 unsigned short closing_wait2;
152 unsigned long event;
153 unsigned long last_active;
154 int line;
155 int count; /* # of fd on device */
156 int blocked_open; /* # of blocked opens */
157 unsigned char *xmit_buf;
158 int xmit_head;
159 int xmit_tail;
160 int xmit_cnt;
161 struct work_struct tqueue;
162 struct work_struct tqueue_hangup;
163 wait_queue_head_t open_wait;
164 wait_queue_head_t close_wait;
165};
166
167
168#define SERIAL_MAGIC 0x5301
169
170/*
171 * The size of the serial xmit buffer is 1 page, or 4096 bytes
172 */
173#define SERIAL_XMIT_SIZE 4096
174
175/*
176 * Events are used to schedule things to happen at timer-interrupt
177 * time, instead of at rs interrupt time.
178 */
179#define RS_EVENT_WRITE_WAKEUP 0
180
181/*
182 * Define the number of ports supported and their irqs.
183 */
184#define NR_PORTS 1
185#define UART_IRQ_DEFNS {UART_IRQ_NUM}
186
187#endif /* __KERNEL__ */
188#endif /* !(_MC683XX_SERIAL_H) */
diff --git a/drivers/tty/serial/68360serial.c b/drivers/tty/serial/68360serial.c
new file mode 100644
index 000000000000..88b13356ec10
--- /dev/null
+++ b/drivers/tty/serial/68360serial.c
@@ -0,0 +1,2978 @@
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/module.h>
24#include <linux/errno.h>
25#include <linux/signal.h>
26#include <linux/sched.h>
27#include <linux/timer.h>
28#include <linux/interrupt.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial.h>
32#include <linux/serialP.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/fcntl.h>
36#include <linux/ptrace.h>
37#include <linux/mm.h>
38#include <linux/init.h>
39#include <linux/delay.h>
40#include <asm/irq.h>
41#include <asm/m68360.h>
42#include <asm/commproc.h>
43
44
45#ifdef CONFIG_KGDB
46extern void breakpoint(void);
47extern void set_debug_traps(void);
48extern int kgdb_output_string (const char* s, unsigned int count);
49#endif
50
51
52/* #ifdef CONFIG_SERIAL_CONSOLE */ /* This seems to be a post 2.0 thing - mles */
53#include <linux/console.h>
54#include <linux/jiffies.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->port.tty;
397 unsigned char ch, flag, *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 while (i-- > 0) {
442 ch = *cp++;
443 icount->rx++;
444
445#ifdef SERIAL_DEBUG_INTR
446 printk("DR%02x:%02x...", ch, status);
447#endif
448 flag = TTY_NORMAL;
449
450 if (status & (BD_SC_BR | BD_SC_FR |
451 BD_SC_PR | BD_SC_OV)) {
452 /*
453 * For statistics only
454 */
455 if (status & BD_SC_BR)
456 icount->brk++;
457 else if (status & BD_SC_PR)
458 icount->parity++;
459 else if (status & BD_SC_FR)
460 icount->frame++;
461 if (status & BD_SC_OV)
462 icount->overrun++;
463
464 /*
465 * Now check to see if character should be
466 * ignored, and mask off conditions which
467 * should be ignored.
468 if (status & info->ignore_status_mask) {
469 if (++ignored > 100)
470 break;
471 continue;
472 }
473 */
474 status &= info->read_status_mask;
475
476 if (status & (BD_SC_BR)) {
477#ifdef SERIAL_DEBUG_INTR
478 printk("handling break....");
479#endif
480 *tty->flip.flag_buf_ptr = TTY_BREAK;
481 if (info->flags & ASYNC_SAK)
482 do_SAK(tty);
483 } else if (status & BD_SC_PR)
484 flag = TTY_PARITY;
485 else if (status & BD_SC_FR)
486 flag = TTY_FRAME;
487 }
488 tty_insert_flip_char(tty, ch, flag);
489 if (status & BD_SC_OV)
490 /*
491 * Overrun is special, since it's
492 * reported immediately, and doesn't
493 * affect the current character
494 */
495 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
496 }
497
498 /* This BD is ready to be used again. Clear status.
499 * Get next BD.
500 */
501 bdp->status |= BD_SC_EMPTY;
502 bdp->status &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
503
504 if (bdp->status & BD_SC_WRAP)
505 bdp = info->rx_bd_base;
506 else
507 bdp++;
508 }
509
510 info->rx_cur = (QUICC_BD *)bdp;
511
512 tty_schedule_flip(tty);
513}
514
515static _INLINE_ void receive_break(ser_info_t *info)
516{
517 struct tty_struct *tty = info->port.tty;
518
519 info->state->icount.brk++;
520 /* Check to see if there is room in the tty buffer for
521 * the break. If not, we exit now, losing the break. FIXME
522 */
523 tty_insert_flip_char(tty, 0, TTY_BREAK);
524 tty_schedule_flip(tty);
525}
526
527static _INLINE_ void transmit_chars(ser_info_t *info)
528{
529
530 if ((info->flags & TX_WAKEUP) ||
531 (info->port.tty->flags & (1 << TTY_DO_WRITE_WAKEUP))) {
532 schedule_work(&info->tqueue);
533 }
534
535#ifdef SERIAL_DEBUG_INTR
536 printk("THRE...");
537#endif
538}
539
540#ifdef notdef
541 /* I need to do this for the SCCs, so it is left as a reminder.
542 */
543static _INLINE_ void check_modem_status(struct async_struct *info)
544{
545 int status;
546 /* struct async_icount *icount; */
547 struct async_icount_24 *icount;
548
549 status = serial_in(info, UART_MSR);
550
551 if (status & UART_MSR_ANY_DELTA) {
552 icount = &info->state->icount;
553 /* update input line counters */
554 if (status & UART_MSR_TERI)
555 icount->rng++;
556 if (status & UART_MSR_DDSR)
557 icount->dsr++;
558 if (status & UART_MSR_DDCD) {
559 icount->dcd++;
560#ifdef CONFIG_HARD_PPS
561 if ((info->flags & ASYNC_HARDPPS_CD) &&
562 (status & UART_MSR_DCD))
563 hardpps();
564#endif
565 }
566 if (status & UART_MSR_DCTS)
567 icount->cts++;
568 wake_up_interruptible(&info->delta_msr_wait);
569 }
570
571 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
572#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
573 printk("ttys%d CD now %s...", info->line,
574 (status & UART_MSR_DCD) ? "on" : "off");
575#endif
576 if (status & UART_MSR_DCD)
577 wake_up_interruptible(&info->open_wait);
578 else {
579#ifdef SERIAL_DEBUG_OPEN
580 printk("scheduling hangup...");
581#endif
582 queue_task(&info->tqueue_hangup,
583 &tq_scheduler);
584 }
585 }
586 if (info->flags & ASYNC_CTS_FLOW) {
587 if (info->port.tty->hw_stopped) {
588 if (status & UART_MSR_CTS) {
589#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
590 printk("CTS tx start...");
591#endif
592 info->port.tty->hw_stopped = 0;
593 info->IER |= UART_IER_THRI;
594 serial_out(info, UART_IER, info->IER);
595 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
596 return;
597 }
598 } else {
599 if (!(status & UART_MSR_CTS)) {
600#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
601 printk("CTS tx stop...");
602#endif
603 info->port.tty->hw_stopped = 1;
604 info->IER &= ~UART_IER_THRI;
605 serial_out(info, UART_IER, info->IER);
606 }
607 }
608 }
609}
610#endif
611
612/*
613 * This is the serial driver's interrupt routine for a single port
614 */
615/* static void rs_360_interrupt(void *dev_id) */ /* until and if we start servicing irqs here */
616static void rs_360_interrupt(int vec, void *dev_id)
617{
618 u_char events;
619 int idx;
620 ser_info_t *info;
621 volatile struct smc_regs *smcp;
622 volatile struct scc_regs *sccp;
623
624 info = dev_id;
625
626 idx = PORT_NUM(info->state->smc_scc_num);
627 if (info->state->smc_scc_num & NUM_IS_SCC) {
628 sccp = &pquicc->scc_regs[idx];
629 events = sccp->scc_scce;
630 if (events & SCCM_RX)
631 receive_chars(info);
632 if (events & SCCM_TX)
633 transmit_chars(info);
634 sccp->scc_scce = events;
635 } else {
636 smcp = &pquicc->smc_regs[idx];
637 events = smcp->smc_smce;
638 if (events & SMCM_BRKE)
639 receive_break(info);
640 if (events & SMCM_RX)
641 receive_chars(info);
642 if (events & SMCM_TX)
643 transmit_chars(info);
644 smcp->smc_smce = events;
645 }
646
647#ifdef SERIAL_DEBUG_INTR
648 printk("rs_interrupt_single(%d, %x)...",
649 info->state->smc_scc_num, events);
650#endif
651#ifdef modem_control
652 check_modem_status(info);
653#endif
654 info->last_active = jiffies;
655#ifdef SERIAL_DEBUG_INTR
656 printk("end.\n");
657#endif
658}
659
660
661/*
662 * -------------------------------------------------------------------
663 * Here ends the serial interrupt routines.
664 * -------------------------------------------------------------------
665 */
666
667
668static void do_softint(void *private_)
669{
670 ser_info_t *info = (ser_info_t *) private_;
671 struct tty_struct *tty;
672
673 tty = info->port.tty;
674 if (!tty)
675 return;
676
677 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
678 tty_wakeup(tty);
679}
680
681
682/*
683 * This routine is called from the scheduler tqueue when the interrupt
684 * routine has signalled that a hangup has occurred. The path of
685 * hangup processing is:
686 *
687 * serial interrupt routine -> (scheduler tqueue) ->
688 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
689 *
690 */
691static void do_serial_hangup(void *private_)
692{
693 struct async_struct *info = (struct async_struct *) private_;
694 struct tty_struct *tty;
695
696 tty = info->port.tty;
697 if (!tty)
698 return;
699
700 tty_hangup(tty);
701}
702
703
704static int startup(ser_info_t *info)
705{
706 unsigned long flags;
707 int retval=0;
708 int idx;
709 /*struct serial_state *state = info->state;*/
710 volatile struct smc_regs *smcp;
711 volatile struct scc_regs *sccp;
712 volatile struct smc_uart_pram *up;
713 volatile struct uart_pram *scup;
714
715
716 local_irq_save(flags);
717
718 if (info->flags & ASYNC_INITIALIZED) {
719 goto errout;
720 }
721
722#ifdef maybe
723 if (!state->port || !state->type) {
724 if (info->port.tty)
725 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
726 goto errout;
727 }
728#endif
729
730#ifdef SERIAL_DEBUG_OPEN
731 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
732#endif
733
734
735#ifdef modem_control
736 info->MCR = 0;
737 if (info->port.tty->termios->c_cflag & CBAUD)
738 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
739#endif
740
741 if (info->port.tty)
742 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
743
744 /*
745 * and set the speed of the serial port
746 */
747 change_speed(info);
748
749 idx = PORT_NUM(info->state->smc_scc_num);
750 if (info->state->smc_scc_num & NUM_IS_SCC) {
751 sccp = &pquicc->scc_regs[idx];
752 scup = &pquicc->pram[info->state->port].scc.pscc.u;
753
754 scup->mrblr = RX_BUF_SIZE;
755 scup->max_idl = RX_BUF_SIZE;
756
757 sccp->scc_sccm |= (UART_SCCM_TX | UART_SCCM_RX);
758 sccp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
759
760 } else {
761 smcp = &pquicc->smc_regs[idx];
762
763 /* Enable interrupts and I/O.
764 */
765 smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
766 smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
767
768 /* We can tune the buffer length and idle characters
769 * to take advantage of the entire incoming buffer size.
770 * If mrblr is something other than 1, maxidl has to be
771 * non-zero or we never get an interrupt. The maxidl
772 * is the number of character times we wait after reception
773 * of the last character before we decide no more characters
774 * are coming.
775 */
776 /* up = (smc_uart_t *)&pquicc->cp_dparam[state->port]; */
777 /* holy unionized structures, Batman: */
778 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
779
780 up->mrblr = RX_BUF_SIZE;
781 up->max_idl = RX_BUF_SIZE;
782
783 up->brkcr = 1; /* number of break chars */
784 }
785
786 info->flags |= ASYNC_INITIALIZED;
787 local_irq_restore(flags);
788 return 0;
789
790errout:
791 local_irq_restore(flags);
792 return retval;
793}
794
795/*
796 * This routine will shutdown a serial port; interrupts are disabled, and
797 * DTR is dropped if the hangup on close termio flag is on.
798 */
799static void shutdown(ser_info_t *info)
800{
801 unsigned long flags;
802 struct serial_state *state;
803 int idx;
804 volatile struct smc_regs *smcp;
805 volatile struct scc_regs *sccp;
806
807 if (!(info->flags & ASYNC_INITIALIZED))
808 return;
809
810 state = info->state;
811
812#ifdef SERIAL_DEBUG_OPEN
813 printk("Shutting down serial port %d (irq %d)....", info->line,
814 state->irq);
815#endif
816
817 local_irq_save(flags);
818
819 idx = PORT_NUM(state->smc_scc_num);
820 if (state->smc_scc_num & NUM_IS_SCC) {
821 sccp = &pquicc->scc_regs[idx];
822 sccp->scc_gsmr.w.low &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
823#ifdef CONFIG_SERIAL_CONSOLE
824 /* We can't disable the transmitter if this is the
825 * system console.
826 */
827 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
828#endif
829 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
830 } else {
831 smcp = &pquicc->smc_regs[idx];
832
833 /* Disable interrupts and I/O.
834 */
835 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
836#ifdef CONFIG_SERIAL_CONSOLE
837 /* We can't disable the transmitter if this is the
838 * system console.
839 */
840 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
841#endif
842 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
843 }
844
845 if (info->port.tty)
846 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
847
848 info->flags &= ~ASYNC_INITIALIZED;
849 local_irq_restore(flags);
850}
851
852/*
853 * This routine is called to set the UART divisor registers to match
854 * the specified baud rate for a serial port.
855 */
856static void change_speed(ser_info_t *info)
857{
858 int baud_rate;
859 unsigned cflag, cval, scval, prev_mode;
860 int i, bits, sbits, idx;
861 unsigned long flags;
862 struct serial_state *state;
863 volatile struct smc_regs *smcp;
864 volatile struct scc_regs *sccp;
865
866 if (!info->port.tty || !info->port.tty->termios)
867 return;
868 cflag = info->port.tty->termios->c_cflag;
869
870 state = info->state;
871
872 /* Character length programmed into the mode register is the
873 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
874 * 1 or 2 stop bits, minus 1.
875 * The value 'bits' counts this for us.
876 */
877 cval = 0;
878 scval = 0;
879
880 /* byte size and parity */
881 switch (cflag & CSIZE) {
882 case CS5: bits = 5; break;
883 case CS6: bits = 6; break;
884 case CS7: bits = 7; break;
885 case CS8: bits = 8; break;
886 /* Never happens, but GCC is too dumb to figure it out */
887 default: bits = 8; break;
888 }
889 sbits = bits - 5;
890
891 if (cflag & CSTOPB) {
892 cval |= SMCMR_SL; /* Two stops */
893 scval |= SCU_PMSR_SL;
894 bits++;
895 }
896 if (cflag & PARENB) {
897 cval |= SMCMR_PEN;
898 scval |= SCU_PMSR_PEN;
899 bits++;
900 }
901 if (!(cflag & PARODD)) {
902 cval |= SMCMR_PM_EVEN;
903 scval |= (SCU_PMSR_REVP | SCU_PMSR_TEVP);
904 }
905
906 /* Determine divisor based on baud rate */
907 i = cflag & CBAUD;
908 if (i >= (sizeof(baud_table)/sizeof(int)))
909 baud_rate = 9600;
910 else
911 baud_rate = baud_table[i];
912
913 info->timeout = (TX_BUF_SIZE*HZ*bits);
914 info->timeout += HZ/50; /* Add .02 seconds of slop */
915
916#ifdef modem_control
917 /* CTS flow control flag and modem status interrupts */
918 info->IER &= ~UART_IER_MSI;
919 if (info->flags & ASYNC_HARDPPS_CD)
920 info->IER |= UART_IER_MSI;
921 if (cflag & CRTSCTS) {
922 info->flags |= ASYNC_CTS_FLOW;
923 info->IER |= UART_IER_MSI;
924 } else
925 info->flags &= ~ASYNC_CTS_FLOW;
926 if (cflag & CLOCAL)
927 info->flags &= ~ASYNC_CHECK_CD;
928 else {
929 info->flags |= ASYNC_CHECK_CD;
930 info->IER |= UART_IER_MSI;
931 }
932 serial_out(info, UART_IER, info->IER);
933#endif
934
935 /*
936 * Set up parity check flag
937 */
938 info->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
939 if (I_INPCK(info->port.tty))
940 info->read_status_mask |= BD_SC_FR | BD_SC_PR;
941 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
942 info->read_status_mask |= BD_SC_BR;
943
944 /*
945 * Characters to ignore
946 */
947 info->ignore_status_mask = 0;
948 if (I_IGNPAR(info->port.tty))
949 info->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
950 if (I_IGNBRK(info->port.tty)) {
951 info->ignore_status_mask |= BD_SC_BR;
952 /*
953 * If we're ignore parity and break indicators, ignore
954 * overruns too. (For real raw support).
955 */
956 if (I_IGNPAR(info->port.tty))
957 info->ignore_status_mask |= BD_SC_OV;
958 }
959 /*
960 * !!! ignore all characters if CREAD is not set
961 */
962 if ((cflag & CREAD) == 0)
963 info->read_status_mask &= ~BD_SC_EMPTY;
964 local_irq_save(flags);
965
966 /* Start bit has not been added (so don't, because we would just
967 * subtract it later), and we need to add one for the number of
968 * stops bits (there is always at least one).
969 */
970 bits++;
971 idx = PORT_NUM(state->smc_scc_num);
972 if (state->smc_scc_num & NUM_IS_SCC) {
973 sccp = &pquicc->scc_regs[idx];
974 sccp->scc_psmr = (sbits << 12) | scval;
975 } else {
976 smcp = &pquicc->smc_regs[idx];
977
978 /* Set the mode register. We want to keep a copy of the
979 * enables, because we want to put them back if they were
980 * present.
981 */
982 prev_mode = smcp->smc_smcmr;
983 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
984 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
985 }
986
987 m360_cpm_setbrg((state - rs_table), baud_rate);
988
989 local_irq_restore(flags);
990}
991
992static void rs_360_put_char(struct tty_struct *tty, unsigned char ch)
993{
994 ser_info_t *info = (ser_info_t *)tty->driver_data;
995 volatile QUICC_BD *bdp;
996
997 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
998 return 0;
999
1000 if (!tty)
1001 return 0;
1002
1003 bdp = info->tx_cur;
1004 while (bdp->status & BD_SC_READY);
1005
1006 /* *((char *)__va(bdp->buf)) = ch; */
1007 *((char *)bdp->buf) = ch;
1008 bdp->length = 1;
1009 bdp->status |= BD_SC_READY;
1010
1011 /* Get next BD.
1012 */
1013 if (bdp->status & BD_SC_WRAP)
1014 bdp = info->tx_bd_base;
1015 else
1016 bdp++;
1017
1018 info->tx_cur = (QUICC_BD *)bdp;
1019 return 1;
1020
1021}
1022
1023static int rs_360_write(struct tty_struct * tty,
1024 const unsigned char *buf, int count)
1025{
1026 int c, ret = 0;
1027 ser_info_t *info = (ser_info_t *)tty->driver_data;
1028 volatile QUICC_BD *bdp;
1029
1030#ifdef CONFIG_KGDB
1031 /* Try to let stub handle output. Returns true if it did. */
1032 if (kgdb_output_string(buf, count))
1033 return ret;
1034#endif
1035
1036 if (serial_paranoia_check(info, tty->name, "rs_write"))
1037 return 0;
1038
1039 if (!tty)
1040 return 0;
1041
1042 bdp = info->tx_cur;
1043
1044 while (1) {
1045 c = min(count, TX_BUF_SIZE);
1046
1047 if (c <= 0)
1048 break;
1049
1050 if (bdp->status & BD_SC_READY) {
1051 info->flags |= TX_WAKEUP;
1052 break;
1053 }
1054
1055 /* memcpy(__va(bdp->buf), buf, c); */
1056 memcpy((void *)bdp->buf, buf, c);
1057
1058 bdp->length = c;
1059 bdp->status |= BD_SC_READY;
1060
1061 buf += c;
1062 count -= c;
1063 ret += c;
1064
1065 /* Get next BD.
1066 */
1067 if (bdp->status & BD_SC_WRAP)
1068 bdp = info->tx_bd_base;
1069 else
1070 bdp++;
1071 info->tx_cur = (QUICC_BD *)bdp;
1072 }
1073 return ret;
1074}
1075
1076static int rs_360_write_room(struct tty_struct *tty)
1077{
1078 ser_info_t *info = (ser_info_t *)tty->driver_data;
1079 int ret;
1080
1081 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1082 return 0;
1083
1084 if ((info->tx_cur->status & BD_SC_READY) == 0) {
1085 info->flags &= ~TX_WAKEUP;
1086 ret = TX_BUF_SIZE;
1087 }
1088 else {
1089 info->flags |= TX_WAKEUP;
1090 ret = 0;
1091 }
1092 return ret;
1093}
1094
1095/* I could track this with transmit counters....maybe later.
1096*/
1097static int rs_360_chars_in_buffer(struct tty_struct *tty)
1098{
1099 ser_info_t *info = (ser_info_t *)tty->driver_data;
1100
1101 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1102 return 0;
1103 return 0;
1104}
1105
1106static void rs_360_flush_buffer(struct tty_struct *tty)
1107{
1108 ser_info_t *info = (ser_info_t *)tty->driver_data;
1109
1110 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1111 return;
1112
1113 /* There is nothing to "flush", whatever we gave the CPM
1114 * is on its way out.
1115 */
1116 tty_wakeup(tty);
1117 info->flags &= ~TX_WAKEUP;
1118}
1119
1120/*
1121 * This function is used to send a high-priority XON/XOFF character to
1122 * the device
1123 */
1124static void rs_360_send_xchar(struct tty_struct *tty, char ch)
1125{
1126 volatile QUICC_BD *bdp;
1127
1128 ser_info_t *info = (ser_info_t *)tty->driver_data;
1129
1130 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
1131 return;
1132
1133 bdp = info->tx_cur;
1134 while (bdp->status & BD_SC_READY);
1135
1136 /* *((char *)__va(bdp->buf)) = ch; */
1137 *((char *)bdp->buf) = ch;
1138 bdp->length = 1;
1139 bdp->status |= BD_SC_READY;
1140
1141 /* Get next BD.
1142 */
1143 if (bdp->status & BD_SC_WRAP)
1144 bdp = info->tx_bd_base;
1145 else
1146 bdp++;
1147
1148 info->tx_cur = (QUICC_BD *)bdp;
1149}
1150
1151/*
1152 * ------------------------------------------------------------
1153 * rs_throttle()
1154 *
1155 * This routine is called by the upper-layer tty layer to signal that
1156 * incoming characters should be throttled.
1157 * ------------------------------------------------------------
1158 */
1159static void rs_360_throttle(struct tty_struct * tty)
1160{
1161 ser_info_t *info = (ser_info_t *)tty->driver_data;
1162#ifdef SERIAL_DEBUG_THROTTLE
1163 char buf[64];
1164
1165 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1166 tty->ldisc.chars_in_buffer(tty));
1167#endif
1168
1169 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1170 return;
1171
1172 if (I_IXOFF(tty))
1173 rs_360_send_xchar(tty, STOP_CHAR(tty));
1174
1175#ifdef modem_control
1176 if (tty->termios->c_cflag & CRTSCTS)
1177 info->MCR &= ~UART_MCR_RTS;
1178
1179 local_irq_disable();
1180 serial_out(info, UART_MCR, info->MCR);
1181 local_irq_enable();
1182#endif
1183}
1184
1185static void rs_360_unthrottle(struct tty_struct * tty)
1186{
1187 ser_info_t *info = (ser_info_t *)tty->driver_data;
1188#ifdef SERIAL_DEBUG_THROTTLE
1189 char buf[64];
1190
1191 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1192 tty->ldisc.chars_in_buffer(tty));
1193#endif
1194
1195 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1196 return;
1197
1198 if (I_IXOFF(tty)) {
1199 if (info->x_char)
1200 info->x_char = 0;
1201 else
1202 rs_360_send_xchar(tty, START_CHAR(tty));
1203 }
1204#ifdef modem_control
1205 if (tty->termios->c_cflag & CRTSCTS)
1206 info->MCR |= UART_MCR_RTS;
1207 local_irq_disable();
1208 serial_out(info, UART_MCR, info->MCR);
1209 local_irq_enable();
1210#endif
1211}
1212
1213/*
1214 * ------------------------------------------------------------
1215 * rs_ioctl() and friends
1216 * ------------------------------------------------------------
1217 */
1218
1219#ifdef maybe
1220/*
1221 * get_lsr_info - get line status register info
1222 *
1223 * Purpose: Let user call ioctl() to get info when the UART physically
1224 * is emptied. On bus types like RS485, the transmitter must
1225 * release the bus after transmitting. This must be done when
1226 * the transmit shift register is empty, not be done when the
1227 * transmit holding register is empty. This functionality
1228 * allows an RS485 driver to be written in user space.
1229 */
1230static int get_lsr_info(struct async_struct * info, unsigned int *value)
1231{
1232 unsigned char status;
1233 unsigned int result;
1234
1235 local_irq_disable();
1236 status = serial_in(info, UART_LSR);
1237 local_irq_enable();
1238 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1239 return put_user(result,value);
1240}
1241#endif
1242
1243static int rs_360_tiocmget(struct tty_struct *tty, struct file *file)
1244{
1245 ser_info_t *info = (ser_info_t *)tty->driver_data;
1246 unsigned int result = 0;
1247#ifdef modem_control
1248 unsigned char control, status;
1249
1250 if (serial_paranoia_check(info, tty->name, __func__))
1251 return -ENODEV;
1252
1253 if (tty->flags & (1 << TTY_IO_ERROR))
1254 return -EIO;
1255
1256 control = info->MCR;
1257 local_irq_disable();
1258 status = serial_in(info, UART_MSR);
1259 local_irq_enable();
1260 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1261 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1262#ifdef TIOCM_OUT1
1263 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1264 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1265#endif
1266 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1267 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1268 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1269 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1270#endif
1271 return result;
1272}
1273
1274static int rs_360_tiocmset(struct tty_struct *tty, struct file *file,
1275 unsigned int set, unsigned int clear)
1276{
1277#ifdef modem_control
1278 ser_info_t *info = (ser_info_t *)tty->driver_data;
1279 unsigned int arg;
1280
1281 if (serial_paranoia_check(info, tty->name, __func__))
1282 return -ENODEV;
1283
1284 if (tty->flags & (1 << TTY_IO_ERROR))
1285 return -EIO;
1286 /* FIXME: locking on info->mcr */
1287 if (set & TIOCM_RTS)
1288 info->mcr |= UART_MCR_RTS;
1289 if (set & TIOCM_DTR)
1290 info->mcr |= UART_MCR_DTR;
1291 if (clear & TIOCM_RTS)
1292 info->MCR &= ~UART_MCR_RTS;
1293 if (clear & TIOCM_DTR)
1294 info->MCR &= ~UART_MCR_DTR;
1295
1296#ifdef TIOCM_OUT1
1297 if (set & TIOCM_OUT1)
1298 info->MCR |= UART_MCR_OUT1;
1299 if (set & TIOCM_OUT2)
1300 info->MCR |= UART_MCR_OUT2;
1301 if (clear & TIOCM_OUT1)
1302 info->MCR &= ~UART_MCR_OUT1;
1303 if (clear & TIOCM_OUT2)
1304 info->MCR &= ~UART_MCR_OUT2;
1305#endif
1306
1307 local_irq_disable();
1308 serial_out(info, UART_MCR, info->MCR);
1309 local_irq_enable();
1310#endif
1311 return 0;
1312}
1313
1314/* Sending a break is a two step process on the SMC/SCC. It is accomplished
1315 * by sending a STOP TRANSMIT command followed by a RESTART TRANSMIT
1316 * command. We take advantage of the begin/end functions to make this
1317 * happen.
1318 */
1319static ushort smc_chan_map[] = {
1320 CPM_CR_CH_SMC1,
1321 CPM_CR_CH_SMC2
1322};
1323
1324static ushort scc_chan_map[] = {
1325 CPM_CR_CH_SCC1,
1326 CPM_CR_CH_SCC2,
1327 CPM_CR_CH_SCC3,
1328 CPM_CR_CH_SCC4
1329};
1330
1331static void begin_break(ser_info_t *info)
1332{
1333 volatile QUICC *cp;
1334 ushort chan;
1335 int idx;
1336
1337 cp = pquicc;
1338
1339 idx = PORT_NUM(info->state->smc_scc_num);
1340 if (info->state->smc_scc_num & NUM_IS_SCC)
1341 chan = scc_chan_map[idx];
1342 else
1343 chan = smc_chan_map[idx];
1344
1345 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG;
1346 while (cp->cp_cr & CPM_CR_FLG);
1347}
1348
1349static void end_break(ser_info_t *info)
1350{
1351 volatile QUICC *cp;
1352 ushort chan;
1353 int idx;
1354
1355 cp = pquicc;
1356
1357 idx = PORT_NUM(info->state->smc_scc_num);
1358 if (info->state->smc_scc_num & NUM_IS_SCC)
1359 chan = scc_chan_map[idx];
1360 else
1361 chan = smc_chan_map[idx];
1362
1363 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_RESTART_TX) | CPM_CR_FLG;
1364 while (cp->cp_cr & CPM_CR_FLG);
1365}
1366
1367/*
1368 * This routine sends a break character out the serial port.
1369 */
1370static void send_break(ser_info_t *info, unsigned int duration)
1371{
1372#ifdef SERIAL_DEBUG_SEND_BREAK
1373 printk("rs_send_break(%d) jiff=%lu...", duration, jiffies);
1374#endif
1375 begin_break(info);
1376 msleep_interruptible(duration);
1377 end_break(info);
1378#ifdef SERIAL_DEBUG_SEND_BREAK
1379 printk("done jiffies=%lu\n", jiffies);
1380#endif
1381}
1382
1383
1384/*
1385 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1386 * Return: write counters to the user passed counter struct
1387 * NB: both 1->0 and 0->1 transitions are counted except for
1388 * RI where only 0->1 is counted.
1389 */
1390static int rs_360_get_icount(struct tty_struct *tty,
1391 struct serial_icounter_struct *icount)
1392{
1393 ser_info_t *info = (ser_info_t *)tty->driver_data;
1394 struct async_icount cnow;
1395
1396 local_irq_disable();
1397 cnow = info->state->icount;
1398 local_irq_enable();
1399
1400 icount->cts = cnow.cts;
1401 icount->dsr = cnow.dsr;
1402 icount->rng = cnow.rng;
1403 icount->dcd = cnow.dcd;
1404
1405 return 0;
1406}
1407
1408static int rs_360_ioctl(struct tty_struct *tty, struct file * file,
1409 unsigned int cmd, unsigned long arg)
1410{
1411 int error;
1412 ser_info_t *info = (ser_info_t *)tty->driver_data;
1413 int retval;
1414 struct async_icount cnow;
1415 /* struct async_icount_24 cnow;*/ /* kernel counter temps */
1416 struct serial_icounter_struct *p_cuser; /* user space */
1417
1418 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1419 return -ENODEV;
1420
1421 if (cmd != TIOCMIWAIT) {
1422 if (tty->flags & (1 << TTY_IO_ERROR))
1423 return -EIO;
1424 }
1425
1426 switch (cmd) {
1427 case TCSBRK: /* SVID version: non-zero arg --> no break */
1428 retval = tty_check_change(tty);
1429 if (retval)
1430 return retval;
1431 tty_wait_until_sent(tty, 0);
1432 if (signal_pending(current))
1433 return -EINTR;
1434 if (!arg) {
1435 send_break(info, 250); /* 1/4 second */
1436 if (signal_pending(current))
1437 return -EINTR;
1438 }
1439 return 0;
1440 case TCSBRKP: /* support for POSIX tcsendbreak() */
1441 retval = tty_check_change(tty);
1442 if (retval)
1443 return retval;
1444 tty_wait_until_sent(tty, 0);
1445 if (signal_pending(current))
1446 return -EINTR;
1447 send_break(info, arg ? arg*100 : 250);
1448 if (signal_pending(current))
1449 return -EINTR;
1450 return 0;
1451 case TIOCSBRK:
1452 retval = tty_check_change(tty);
1453 if (retval)
1454 return retval;
1455 tty_wait_until_sent(tty, 0);
1456 begin_break(info);
1457 return 0;
1458 case TIOCCBRK:
1459 retval = tty_check_change(tty);
1460 if (retval)
1461 return retval;
1462 end_break(info);
1463 return 0;
1464#ifdef maybe
1465 case TIOCSERGETLSR: /* Get line status register */
1466 return get_lsr_info(info, (unsigned int *) arg);
1467#endif
1468 /*
1469 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1470 * - mask passed in arg for lines of interest
1471 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1472 * Caller should use TIOCGICOUNT to see which one it was
1473 */
1474 case TIOCMIWAIT:
1475#ifdef modem_control
1476 local_irq_disable();
1477 /* note the counters on entry */
1478 cprev = info->state->icount;
1479 local_irq_enable();
1480 while (1) {
1481 interruptible_sleep_on(&info->delta_msr_wait);
1482 /* see if a signal did it */
1483 if (signal_pending(current))
1484 return -ERESTARTSYS;
1485 local_irq_disable();
1486 cnow = info->state->icount; /* atomic copy */
1487 local_irq_enable();
1488 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1489 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1490 return -EIO; /* no change => error */
1491 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1492 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1493 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1494 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1495 return 0;
1496 }
1497 cprev = cnow;
1498 }
1499 /* NOTREACHED */
1500#else
1501 return 0;
1502#endif
1503
1504
1505 default:
1506 return -ENOIOCTLCMD;
1507 }
1508 return 0;
1509}
1510
1511/* FIX UP modem control here someday......
1512*/
1513static void rs_360_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1514{
1515 ser_info_t *info = (ser_info_t *)tty->driver_data;
1516
1517 change_speed(info);
1518
1519#ifdef modem_control
1520 /* Handle transition to B0 status */
1521 if ((old_termios->c_cflag & CBAUD) &&
1522 !(tty->termios->c_cflag & CBAUD)) {
1523 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1524 local_irq_disable();
1525 serial_out(info, UART_MCR, info->MCR);
1526 local_irq_enable();
1527 }
1528
1529 /* Handle transition away from B0 status */
1530 if (!(old_termios->c_cflag & CBAUD) &&
1531 (tty->termios->c_cflag & CBAUD)) {
1532 info->MCR |= UART_MCR_DTR;
1533 if (!tty->hw_stopped ||
1534 !(tty->termios->c_cflag & CRTSCTS)) {
1535 info->MCR |= UART_MCR_RTS;
1536 }
1537 local_irq_disable();
1538 serial_out(info, UART_MCR, info->MCR);
1539 local_irq_enable();
1540 }
1541
1542 /* Handle turning off CRTSCTS */
1543 if ((old_termios->c_cflag & CRTSCTS) &&
1544 !(tty->termios->c_cflag & CRTSCTS)) {
1545 tty->hw_stopped = 0;
1546 rs_360_start(tty);
1547 }
1548#endif
1549
1550#if 0
1551 /*
1552 * No need to wake up processes in open wait, since they
1553 * sample the CLOCAL flag once, and don't recheck it.
1554 * XXX It's not clear whether the current behavior is correct
1555 * or not. Hence, this may change.....
1556 */
1557 if (!(old_termios->c_cflag & CLOCAL) &&
1558 (tty->termios->c_cflag & CLOCAL))
1559 wake_up_interruptible(&info->open_wait);
1560#endif
1561}
1562
1563/*
1564 * ------------------------------------------------------------
1565 * rs_close()
1566 *
1567 * This routine is called when the serial port gets closed. First, we
1568 * wait for the last remaining data to be sent. Then, we unlink its
1569 * async structure from the interrupt chain if necessary, and we free
1570 * that IRQ if nothing is left in the chain.
1571 * ------------------------------------------------------------
1572 */
1573static void rs_360_close(struct tty_struct *tty, struct file * filp)
1574{
1575 ser_info_t *info = (ser_info_t *)tty->driver_data;
1576 /* struct async_state *state; */
1577 struct serial_state *state;
1578 unsigned long flags;
1579 int idx;
1580 volatile struct smc_regs *smcp;
1581 volatile struct scc_regs *sccp;
1582
1583 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1584 return;
1585
1586 state = info->state;
1587
1588 local_irq_save(flags);
1589
1590 if (tty_hung_up_p(filp)) {
1591 DBG_CNT("before DEC-hung");
1592 local_irq_restore(flags);
1593 return;
1594 }
1595
1596#ifdef SERIAL_DEBUG_OPEN
1597 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1598#endif
1599 if ((tty->count == 1) && (state->count != 1)) {
1600 /*
1601 * Uh, oh. tty->count is 1, which means that the tty
1602 * structure will be freed. state->count should always
1603 * be one in these conditions. If it's greater than
1604 * one, we've got real problems, since it means the
1605 * serial port won't be shutdown.
1606 */
1607 printk("rs_close: bad serial port count; tty->count is 1, "
1608 "state->count is %d\n", state->count);
1609 state->count = 1;
1610 }
1611 if (--state->count < 0) {
1612 printk("rs_close: bad serial port count for ttys%d: %d\n",
1613 info->line, state->count);
1614 state->count = 0;
1615 }
1616 if (state->count) {
1617 DBG_CNT("before DEC-2");
1618 local_irq_restore(flags);
1619 return;
1620 }
1621 info->flags |= ASYNC_CLOSING;
1622 /*
1623 * Now we wait for the transmit buffer to clear; and we notify
1624 * the line discipline to only process XON/XOFF characters.
1625 */
1626 tty->closing = 1;
1627 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1628 tty_wait_until_sent(tty, info->closing_wait);
1629 /*
1630 * At this point we stop accepting input. To do this, we
1631 * disable the receive line status interrupts, and tell the
1632 * interrupt driver to stop checking the data ready bit in the
1633 * line status register.
1634 */
1635 info->read_status_mask &= ~BD_SC_EMPTY;
1636 if (info->flags & ASYNC_INITIALIZED) {
1637
1638 idx = PORT_NUM(info->state->smc_scc_num);
1639 if (info->state->smc_scc_num & NUM_IS_SCC) {
1640 sccp = &pquicc->scc_regs[idx];
1641 sccp->scc_sccm &= ~UART_SCCM_RX;
1642 sccp->scc_gsmr.w.low &= ~SCC_GSMRL_ENR;
1643 } else {
1644 smcp = &pquicc->smc_regs[idx];
1645 smcp->smc_smcm &= ~SMCM_RX;
1646 smcp->smc_smcmr &= ~SMCMR_REN;
1647 }
1648 /*
1649 * Before we drop DTR, make sure the UART transmitter
1650 * has completely drained; this is especially
1651 * important if there is a transmit FIFO!
1652 */
1653 rs_360_wait_until_sent(tty, info->timeout);
1654 }
1655 shutdown(info);
1656 rs_360_flush_buffer(tty);
1657 tty_ldisc_flush(tty);
1658 tty->closing = 0;
1659 info->event = 0;
1660 info->port.tty = NULL;
1661 if (info->blocked_open) {
1662 if (info->close_delay) {
1663 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1664 }
1665 wake_up_interruptible(&info->open_wait);
1666 }
1667 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1668 wake_up_interruptible(&info->close_wait);
1669 local_irq_restore(flags);
1670}
1671
1672/*
1673 * rs_wait_until_sent() --- wait until the transmitter is empty
1674 */
1675static void rs_360_wait_until_sent(struct tty_struct *tty, int timeout)
1676{
1677 ser_info_t *info = (ser_info_t *)tty->driver_data;
1678 unsigned long orig_jiffies, char_time;
1679 /*int lsr;*/
1680 volatile QUICC_BD *bdp;
1681
1682 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1683 return;
1684
1685#ifdef maybe
1686 if (info->state->type == PORT_UNKNOWN)
1687 return;
1688#endif
1689
1690 orig_jiffies = jiffies;
1691 /*
1692 * Set the check interval to be 1/5 of the estimated time to
1693 * send a single character, and make it at least 1. The check
1694 * interval should also be less than the timeout.
1695 *
1696 * Note: we have to use pretty tight timings here to satisfy
1697 * the NIST-PCTS.
1698 */
1699 char_time = 1;
1700 if (timeout)
1701 char_time = min(char_time, (unsigned long)timeout);
1702#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1703 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1704 printk("jiff=%lu...", jiffies);
1705#endif
1706
1707 /* We go through the loop at least once because we can't tell
1708 * exactly when the last character exits the shifter. There can
1709 * be at least two characters waiting to be sent after the buffers
1710 * are empty.
1711 */
1712 do {
1713#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1714 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1715#endif
1716/* current->counter = 0; make us low-priority */
1717 msleep_interruptible(jiffies_to_msecs(char_time));
1718 if (signal_pending(current))
1719 break;
1720 if (timeout && (time_after(jiffies, orig_jiffies + timeout)))
1721 break;
1722 /* The 'tx_cur' is really the next buffer to send. We
1723 * have to back up to the previous BD and wait for it
1724 * to go. This isn't perfect, because all this indicates
1725 * is the buffer is available. There are still characters
1726 * in the CPM FIFO.
1727 */
1728 bdp = info->tx_cur;
1729 if (bdp == info->tx_bd_base)
1730 bdp += (TX_NUM_FIFO-1);
1731 else
1732 bdp--;
1733 } while (bdp->status & BD_SC_READY);
1734 current->state = TASK_RUNNING;
1735#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1736 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1737#endif
1738}
1739
1740/*
1741 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1742 */
1743static void rs_360_hangup(struct tty_struct *tty)
1744{
1745 ser_info_t *info = (ser_info_t *)tty->driver_data;
1746 struct serial_state *state = info->state;
1747
1748 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1749 return;
1750
1751 state = info->state;
1752
1753 rs_360_flush_buffer(tty);
1754 shutdown(info);
1755 info->event = 0;
1756 state->count = 0;
1757 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1758 info->port.tty = NULL;
1759 wake_up_interruptible(&info->open_wait);
1760}
1761
1762/*
1763 * ------------------------------------------------------------
1764 * rs_open() and friends
1765 * ------------------------------------------------------------
1766 */
1767static int block_til_ready(struct tty_struct *tty, struct file * filp,
1768 ser_info_t *info)
1769{
1770#ifdef DO_THIS_LATER
1771 DECLARE_WAITQUEUE(wait, current);
1772#endif
1773 struct serial_state *state = info->state;
1774 int retval;
1775 int do_clocal = 0;
1776
1777 /*
1778 * If the device is in the middle of being closed, then block
1779 * until it's done, and then try again.
1780 */
1781 if (tty_hung_up_p(filp) ||
1782 (info->flags & ASYNC_CLOSING)) {
1783 if (info->flags & ASYNC_CLOSING)
1784 interruptible_sleep_on(&info->close_wait);
1785#ifdef SERIAL_DO_RESTART
1786 if (info->flags & ASYNC_HUP_NOTIFY)
1787 return -EAGAIN;
1788 else
1789 return -ERESTARTSYS;
1790#else
1791 return -EAGAIN;
1792#endif
1793 }
1794
1795 /*
1796 * If non-blocking mode is set, or the port is not enabled,
1797 * then make the check up front and then exit.
1798 * If this is an SMC port, we don't have modem control to wait
1799 * for, so just get out here.
1800 */
1801 if ((filp->f_flags & O_NONBLOCK) ||
1802 (tty->flags & (1 << TTY_IO_ERROR)) ||
1803 !(info->state->smc_scc_num & NUM_IS_SCC)) {
1804 info->flags |= ASYNC_NORMAL_ACTIVE;
1805 return 0;
1806 }
1807
1808 if (tty->termios->c_cflag & CLOCAL)
1809 do_clocal = 1;
1810
1811 /*
1812 * Block waiting for the carrier detect and the line to become
1813 * free (i.e., not in use by the callout). While we are in
1814 * this loop, state->count is dropped by one, so that
1815 * rs_close() knows when to free things. We restore it upon
1816 * exit, either normal or abnormal.
1817 */
1818 retval = 0;
1819#ifdef DO_THIS_LATER
1820 add_wait_queue(&info->open_wait, &wait);
1821#ifdef SERIAL_DEBUG_OPEN
1822 printk("block_til_ready before block: ttys%d, count = %d\n",
1823 state->line, state->count);
1824#endif
1825 local_irq_disable();
1826 if (!tty_hung_up_p(filp))
1827 state->count--;
1828 local_irq_enable();
1829 info->blocked_open++;
1830 while (1) {
1831 local_irq_disable();
1832 if (tty->termios->c_cflag & CBAUD)
1833 serial_out(info, UART_MCR,
1834 serial_inp(info, UART_MCR) |
1835 (UART_MCR_DTR | UART_MCR_RTS));
1836 local_irq_enable();
1837 set_current_state(TASK_INTERRUPTIBLE);
1838 if (tty_hung_up_p(filp) ||
1839 !(info->flags & ASYNC_INITIALIZED)) {
1840#ifdef SERIAL_DO_RESTART
1841 if (info->flags & ASYNC_HUP_NOTIFY)
1842 retval = -EAGAIN;
1843 else
1844 retval = -ERESTARTSYS;
1845#else
1846 retval = -EAGAIN;
1847#endif
1848 break;
1849 }
1850 if (!(info->flags & ASYNC_CLOSING) &&
1851 (do_clocal || (serial_in(info, UART_MSR) &
1852 UART_MSR_DCD)))
1853 break;
1854 if (signal_pending(current)) {
1855 retval = -ERESTARTSYS;
1856 break;
1857 }
1858#ifdef SERIAL_DEBUG_OPEN
1859 printk("block_til_ready blocking: ttys%d, count = %d\n",
1860 info->line, state->count);
1861#endif
1862 tty_unlock();
1863 schedule();
1864 tty_lock();
1865 }
1866 current->state = TASK_RUNNING;
1867 remove_wait_queue(&info->open_wait, &wait);
1868 if (!tty_hung_up_p(filp))
1869 state->count++;
1870 info->blocked_open--;
1871#ifdef SERIAL_DEBUG_OPEN
1872 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1873 info->line, state->count);
1874#endif
1875#endif /* DO_THIS_LATER */
1876 if (retval)
1877 return retval;
1878 info->flags |= ASYNC_NORMAL_ACTIVE;
1879 return 0;
1880}
1881
1882static int get_async_struct(int line, ser_info_t **ret_info)
1883{
1884 struct serial_state *sstate;
1885
1886 sstate = rs_table + line;
1887 if (sstate->info) {
1888 sstate->count++;
1889 *ret_info = (ser_info_t *)sstate->info;
1890 return 0;
1891 }
1892 else {
1893 return -ENOMEM;
1894 }
1895}
1896
1897/*
1898 * This routine is called whenever a serial port is opened. It
1899 * enables interrupts for a serial port, linking in its async structure into
1900 * the IRQ chain. It also performs the serial-specific
1901 * initialization for the tty structure.
1902 */
1903static int rs_360_open(struct tty_struct *tty, struct file * filp)
1904{
1905 ser_info_t *info;
1906 int retval, line;
1907
1908 line = tty->index;
1909 if ((line < 0) || (line >= NR_PORTS))
1910 return -ENODEV;
1911 retval = get_async_struct(line, &info);
1912 if (retval)
1913 return retval;
1914 if (serial_paranoia_check(info, tty->name, "rs_open"))
1915 return -ENODEV;
1916
1917#ifdef SERIAL_DEBUG_OPEN
1918 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1919#endif
1920 tty->driver_data = info;
1921 info->port.tty = tty;
1922
1923 /*
1924 * Start up serial port
1925 */
1926 retval = startup(info);
1927 if (retval)
1928 return retval;
1929
1930 retval = block_til_ready(tty, filp, info);
1931 if (retval) {
1932#ifdef SERIAL_DEBUG_OPEN
1933 printk("rs_open returning after block_til_ready with %d\n",
1934 retval);
1935#endif
1936 return retval;
1937 }
1938
1939#ifdef SERIAL_DEBUG_OPEN
1940 printk("rs_open %s successful...", tty->name);
1941#endif
1942 return 0;
1943}
1944
1945/*
1946 * /proc fs routines....
1947 */
1948
1949static inline int line_info(char *buf, struct serial_state *state)
1950{
1951#ifdef notdef
1952 struct async_struct *info = state->info, scr_info;
1953 char stat_buf[30], control, status;
1954#endif
1955 int ret;
1956
1957 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
1958 state->line,
1959 (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC",
1960 (unsigned int)(state->port), state->irq);
1961
1962 if (!state->port || (state->type == PORT_UNKNOWN)) {
1963 ret += sprintf(buf+ret, "\n");
1964 return ret;
1965 }
1966
1967#ifdef notdef
1968 /*
1969 * Figure out the current RS-232 lines
1970 */
1971 if (!info) {
1972 info = &scr_info; /* This is just for serial_{in,out} */
1973
1974 info->magic = SERIAL_MAGIC;
1975 info->port = state->port;
1976 info->flags = state->flags;
1977 info->quot = 0;
1978 info->port.tty = NULL;
1979 }
1980 local_irq_disable();
1981 status = serial_in(info, UART_MSR);
1982 control = info ? info->MCR : serial_in(info, UART_MCR);
1983 local_irq_enable();
1984
1985 stat_buf[0] = 0;
1986 stat_buf[1] = 0;
1987 if (control & UART_MCR_RTS)
1988 strcat(stat_buf, "|RTS");
1989 if (status & UART_MSR_CTS)
1990 strcat(stat_buf, "|CTS");
1991 if (control & UART_MCR_DTR)
1992 strcat(stat_buf, "|DTR");
1993 if (status & UART_MSR_DSR)
1994 strcat(stat_buf, "|DSR");
1995 if (status & UART_MSR_DCD)
1996 strcat(stat_buf, "|CD");
1997 if (status & UART_MSR_RI)
1998 strcat(stat_buf, "|RI");
1999
2000 if (info->quot) {
2001 ret += sprintf(buf+ret, " baud:%d",
2002 state->baud_base / info->quot);
2003 }
2004
2005 ret += sprintf(buf+ret, " tx:%d rx:%d",
2006 state->icount.tx, state->icount.rx);
2007
2008 if (state->icount.frame)
2009 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2010
2011 if (state->icount.parity)
2012 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2013
2014 if (state->icount.brk)
2015 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2016
2017 if (state->icount.overrun)
2018 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2019
2020 /*
2021 * Last thing is the RS-232 status lines
2022 */
2023 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2024#endif
2025 return ret;
2026}
2027
2028int rs_360_read_proc(char *page, char **start, off_t off, int count,
2029 int *eof, void *data)
2030{
2031 int i, len = 0;
2032 off_t begin = 0;
2033
2034 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2035 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2036 len += line_info(page + len, &rs_table[i]);
2037 if (len+begin > off+count)
2038 goto done;
2039 if (len+begin < off) {
2040 begin += len;
2041 len = 0;
2042 }
2043 }
2044 *eof = 1;
2045done:
2046 if (off >= len+begin)
2047 return 0;
2048 *start = page + (begin-off);
2049 return ((count < begin+len-off) ? count : begin+len-off);
2050}
2051
2052/*
2053 * ---------------------------------------------------------------------
2054 * rs_init() and friends
2055 *
2056 * rs_init() is called at boot-time to initialize the serial driver.
2057 * ---------------------------------------------------------------------
2058 */
2059
2060/*
2061 * This routine prints out the appropriate serial driver version
2062 * number, and identifies which options were configured into this
2063 * driver.
2064 */
2065static _INLINE_ void show_serial_version(void)
2066{
2067 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2068}
2069
2070
2071/*
2072 * The serial console driver used during boot. Note that these names
2073 * clash with those found in "serial.c", so we currently can't support
2074 * the 16xxx uarts and these at the same time. I will fix this to become
2075 * an indirect function call from tty_io.c (or something).
2076 */
2077
2078#ifdef CONFIG_SERIAL_CONSOLE
2079
2080/*
2081 * Print a string to the serial port trying not to disturb any possible
2082 * real use of the port...
2083 */
2084static void my_console_write(int idx, const char *s,
2085 unsigned count)
2086{
2087 struct serial_state *ser;
2088 ser_info_t *info;
2089 unsigned i;
2090 QUICC_BD *bdp, *bdbase;
2091 volatile struct smc_uart_pram *up;
2092 volatile u_char *cp;
2093
2094 ser = rs_table + idx;
2095
2096
2097 /* If the port has been initialized for general use, we have
2098 * to use the buffer descriptors allocated there. Otherwise,
2099 * we simply use the single buffer allocated.
2100 */
2101 if ((info = (ser_info_t *)ser->info) != NULL) {
2102 bdp = info->tx_cur;
2103 bdbase = info->tx_bd_base;
2104 }
2105 else {
2106 /* Pointer to UART in parameter ram.
2107 */
2108 /* up = (smc_uart_t *)&cpmp->cp_dparam[ser->port]; */
2109 up = &pquicc->pram[ser->port].scc.pothers.idma_smc.psmc.u;
2110
2111 /* Get the address of the host memory buffer.
2112 */
2113 bdp = bdbase = (QUICC_BD *)((uint)pquicc + (uint)up->tbase);
2114 }
2115
2116 /*
2117 * We need to gracefully shut down the transmitter, disable
2118 * interrupts, then send our bytes out.
2119 */
2120
2121 /*
2122 * Now, do each character. This is not as bad as it looks
2123 * since this is a holding FIFO and not a transmitting FIFO.
2124 * We could add the complexity of filling the entire transmit
2125 * buffer, but we would just wait longer between accesses......
2126 */
2127 for (i = 0; i < count; i++, s++) {
2128 /* Wait for transmitter fifo to empty.
2129 * Ready indicates output is ready, and xmt is doing
2130 * that, not that it is ready for us to send.
2131 */
2132 while (bdp->status & BD_SC_READY);
2133
2134 /* Send the character out.
2135 */
2136 cp = bdp->buf;
2137 *cp = *s;
2138
2139 bdp->length = 1;
2140 bdp->status |= BD_SC_READY;
2141
2142 if (bdp->status & BD_SC_WRAP)
2143 bdp = bdbase;
2144 else
2145 bdp++;
2146
2147 /* if a LF, also do CR... */
2148 if (*s == 10) {
2149 while (bdp->status & BD_SC_READY);
2150 /* cp = __va(bdp->buf); */
2151 cp = bdp->buf;
2152 *cp = 13;
2153 bdp->length = 1;
2154 bdp->status |= BD_SC_READY;
2155
2156 if (bdp->status & BD_SC_WRAP) {
2157 bdp = bdbase;
2158 }
2159 else {
2160 bdp++;
2161 }
2162 }
2163 }
2164
2165 /*
2166 * Finally, Wait for transmitter & holding register to empty
2167 * and restore the IER
2168 */
2169 while (bdp->status & BD_SC_READY);
2170
2171 if (info)
2172 info->tx_cur = (QUICC_BD *)bdp;
2173}
2174
2175static void serial_console_write(struct console *c, const char *s,
2176 unsigned count)
2177{
2178#ifdef CONFIG_KGDB
2179 /* Try to let stub handle output. Returns true if it did. */
2180 if (kgdb_output_string(s, count))
2181 return;
2182#endif
2183 my_console_write(c->index, s, count);
2184}
2185
2186
2187
2188/*void console_print_68360(const char *p)
2189{
2190 const char *cp = p;
2191 int i;
2192
2193 for (i=0;cp[i]!=0;i++);
2194
2195 serial_console_write (p, i);
2196
2197 //Comment this if you want to have a strict interrupt-driven output
2198 //rs_fair_output();
2199
2200 return;
2201}*/
2202
2203
2204
2205
2206
2207
2208#ifdef CONFIG_XMON
2209int
2210xmon_360_write(const char *s, unsigned count)
2211{
2212 my_console_write(0, s, count);
2213 return(count);
2214}
2215#endif
2216
2217#ifdef CONFIG_KGDB
2218void
2219putDebugChar(char ch)
2220{
2221 my_console_write(0, &ch, 1);
2222}
2223#endif
2224
2225/*
2226 * Receive character from the serial port. This only works well
2227 * before the port is initialized for real use.
2228 */
2229static int my_console_wait_key(int idx, int xmon, char *obuf)
2230{
2231 struct serial_state *ser;
2232 u_char c, *cp;
2233 ser_info_t *info;
2234 QUICC_BD *bdp;
2235 volatile struct smc_uart_pram *up;
2236 int i;
2237
2238 ser = rs_table + idx;
2239
2240 /* Get the address of the host memory buffer.
2241 * If the port has been initialized for general use, we must
2242 * use information from the port structure.
2243 */
2244 if ((info = (ser_info_t *)ser->info))
2245 bdp = info->rx_cur;
2246 else
2247 /* bdp = (QUICC_BD *)&cpmp->cp_dpmem[up->smc_rbase]; */
2248 bdp = (QUICC_BD *)((uint)pquicc + (uint)up->tbase);
2249
2250 /* Pointer to UART in parameter ram.
2251 */
2252 /* up = (smc_uart_t *)&cpmp->cp_dparam[ser->port]; */
2253 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
2254
2255 /*
2256 * We need to gracefully shut down the receiver, disable
2257 * interrupts, then read the input.
2258 * XMON just wants a poll. If no character, return -1, else
2259 * return the character.
2260 */
2261 if (!xmon) {
2262 while (bdp->status & BD_SC_EMPTY);
2263 }
2264 else {
2265 if (bdp->status & BD_SC_EMPTY)
2266 return -1;
2267 }
2268
2269 cp = (char *)bdp->buf;
2270
2271 if (obuf) {
2272 i = c = bdp->length;
2273 while (i-- > 0)
2274 *obuf++ = *cp++;
2275 }
2276 else {
2277 c = *cp;
2278 }
2279 bdp->status |= BD_SC_EMPTY;
2280
2281 if (info) {
2282 if (bdp->status & BD_SC_WRAP) {
2283 bdp = info->rx_bd_base;
2284 }
2285 else {
2286 bdp++;
2287 }
2288 info->rx_cur = (QUICC_BD *)bdp;
2289 }
2290
2291 return((int)c);
2292}
2293
2294static int serial_console_wait_key(struct console *co)
2295{
2296 return(my_console_wait_key(co->index, 0, NULL));
2297}
2298
2299#ifdef CONFIG_XMON
2300int
2301xmon_360_read_poll(void)
2302{
2303 return(my_console_wait_key(0, 1, NULL));
2304}
2305
2306int
2307xmon_360_read_char(void)
2308{
2309 return(my_console_wait_key(0, 0, NULL));
2310}
2311#endif
2312
2313#ifdef CONFIG_KGDB
2314static char kgdb_buf[RX_BUF_SIZE], *kgdp;
2315static int kgdb_chars;
2316
2317unsigned char
2318getDebugChar(void)
2319{
2320 if (kgdb_chars <= 0) {
2321 kgdb_chars = my_console_wait_key(0, 0, kgdb_buf);
2322 kgdp = kgdb_buf;
2323 }
2324 kgdb_chars--;
2325
2326 return(*kgdp++);
2327}
2328
2329void kgdb_interruptible(int state)
2330{
2331}
2332void kgdb_map_scc(void)
2333{
2334 struct serial_state *ser;
2335 uint mem_addr;
2336 volatile QUICC_BD *bdp;
2337 volatile smc_uart_t *up;
2338
2339 cpmp = (cpm360_t *)&(((immap_t *)IMAP_ADDR)->im_cpm);
2340
2341 /* To avoid data cache CPM DMA coherency problems, allocate a
2342 * buffer in the CPM DPRAM. This will work until the CPM and
2343 * serial ports are initialized. At that time a memory buffer
2344 * will be allocated.
2345 * The port is already initialized from the boot procedure, all
2346 * we do here is give it a different buffer and make it a FIFO.
2347 */
2348
2349 ser = rs_table;
2350
2351 /* Right now, assume we are using SMCs.
2352 */
2353 up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2354
2355 /* Allocate space for an input FIFO, plus a few bytes for output.
2356 * Allocate bytes to maintain word alignment.
2357 */
2358 mem_addr = (uint)(&cpmp->cp_dpmem[0x1000]);
2359
2360 /* Set the physical address of the host memory buffers in
2361 * the buffer descriptors.
2362 */
2363 bdp = (QUICC_BD *)&cpmp->cp_dpmem[up->smc_rbase];
2364 bdp->buf = mem_addr;
2365
2366 bdp = (QUICC_BD *)&cpmp->cp_dpmem[up->smc_tbase];
2367 bdp->buf = mem_addr+RX_BUF_SIZE;
2368
2369 up->smc_mrblr = RX_BUF_SIZE; /* receive buffer length */
2370 up->smc_maxidl = RX_BUF_SIZE;
2371}
2372#endif
2373
2374static struct tty_struct *serial_console_device(struct console *c, int *index)
2375{
2376 *index = c->index;
2377 return serial_driver;
2378}
2379
2380
2381struct console sercons = {
2382 .name = "ttyS",
2383 .write = serial_console_write,
2384 .device = serial_console_device,
2385 .wait_key = serial_console_wait_key,
2386 .setup = serial_console_setup,
2387 .flags = CON_PRINTBUFFER,
2388 .index = CONFIG_SERIAL_CONSOLE_PORT,
2389};
2390
2391
2392
2393/*
2394 * Register console.
2395 */
2396long console_360_init(long kmem_start, long kmem_end)
2397{
2398 register_console(&sercons);
2399 /*register_console (console_print_68360); - 2.0.38 only required a write
2400 function pointer. */
2401 return kmem_start;
2402}
2403
2404#endif
2405
2406/* Index in baud rate table of the default console baud rate.
2407*/
2408static int baud_idx;
2409
2410static const struct tty_operations rs_360_ops = {
2411 .owner = THIS_MODULE,
2412 .open = rs_360_open,
2413 .close = rs_360_close,
2414 .write = rs_360_write,
2415 .put_char = rs_360_put_char,
2416 .write_room = rs_360_write_room,
2417 .chars_in_buffer = rs_360_chars_in_buffer,
2418 .flush_buffer = rs_360_flush_buffer,
2419 .ioctl = rs_360_ioctl,
2420 .throttle = rs_360_throttle,
2421 .unthrottle = rs_360_unthrottle,
2422 /* .send_xchar = rs_360_send_xchar, */
2423 .set_termios = rs_360_set_termios,
2424 .stop = rs_360_stop,
2425 .start = rs_360_start,
2426 .hangup = rs_360_hangup,
2427 /* .wait_until_sent = rs_360_wait_until_sent, */
2428 /* .read_proc = rs_360_read_proc, */
2429 .tiocmget = rs_360_tiocmget,
2430 .tiocmset = rs_360_tiocmset,
2431};
2432
2433static int __init rs_360_init(void)
2434{
2435 struct serial_state * state;
2436 ser_info_t *info;
2437 void *mem_addr;
2438 uint dp_addr, iobits;
2439 int i, j, idx;
2440 ushort chan;
2441 QUICC_BD *bdp;
2442 volatile QUICC *cp;
2443 volatile struct smc_regs *sp;
2444 volatile struct smc_uart_pram *up;
2445 volatile struct scc_regs *scp;
2446 volatile struct uart_pram *sup;
2447 /* volatile immap_t *immap; */
2448
2449 serial_driver = alloc_tty_driver(NR_PORTS);
2450 if (!serial_driver)
2451 return -1;
2452
2453 show_serial_version();
2454
2455 serial_driver->name = "ttyS";
2456 serial_driver->major = TTY_MAJOR;
2457 serial_driver->minor_start = 64;
2458 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2459 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2460 serial_driver->init_termios = tty_std_termios;
2461 serial_driver->init_termios.c_cflag =
2462 baud_idx | CS8 | CREAD | HUPCL | CLOCAL;
2463 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2464 tty_set_operations(serial_driver, &rs_360_ops);
2465
2466 if (tty_register_driver(serial_driver))
2467 panic("Couldn't register serial driver\n");
2468
2469 cp = pquicc; /* Get pointer to Communication Processor */
2470 /* immap = (immap_t *)IMAP_ADDR; */ /* and to internal registers */
2471
2472
2473 /* Configure SCC2, SCC3, and SCC4 instead of port A parallel I/O.
2474 */
2475 /* The "standard" configuration through the 860.
2476 */
2477/* immap->im_ioport.iop_papar |= 0x00fc; */
2478/* immap->im_ioport.iop_padir &= ~0x00fc; */
2479/* immap->im_ioport.iop_paodr &= ~0x00fc; */
2480 cp->pio_papar |= 0x00fc;
2481 cp->pio_padir &= ~0x00fc;
2482 /* cp->pio_paodr &= ~0x00fc; */
2483
2484
2485 /* Since we don't yet do modem control, connect the port C pins
2486 * as general purpose I/O. This will assert CTS and CD for the
2487 * SCC ports.
2488 */
2489 /* FIXME: see 360um p.7-365 and 860um p.34-12
2490 * I can't make sense of these bits - mleslie*/
2491/* immap->im_ioport.iop_pcdir |= 0x03c6; */
2492/* immap->im_ioport.iop_pcpar &= ~0x03c6; */
2493
2494/* cp->pio_pcdir |= 0x03c6; */
2495/* cp->pio_pcpar &= ~0x03c6; */
2496
2497
2498
2499 /* Connect SCC2 and SCC3 to NMSI. Connect BRG3 to SCC2 and
2500 * BRG4 to SCC3.
2501 */
2502 cp->si_sicr &= ~0x00ffff00;
2503 cp->si_sicr |= 0x001b1200;
2504
2505#ifdef CONFIG_PP04
2506 /* Frequentis PP04 forced to RS-232 until we know better.
2507 * Port C 12 and 13 low enables RS-232 on SCC3 and SCC4.
2508 */
2509 immap->im_ioport.iop_pcdir |= 0x000c;
2510 immap->im_ioport.iop_pcpar &= ~0x000c;
2511 immap->im_ioport.iop_pcdat &= ~0x000c;
2512
2513 /* This enables the TX driver.
2514 */
2515 cp->cp_pbpar &= ~0x6000;
2516 cp->cp_pbdat &= ~0x6000;
2517#endif
2518
2519 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2520 state->magic = SSTATE_MAGIC;
2521 state->line = i;
2522 state->type = PORT_UNKNOWN;
2523 state->custom_divisor = 0;
2524 state->close_delay = 5*HZ/10;
2525 state->closing_wait = 30*HZ;
2526 state->icount.cts = state->icount.dsr =
2527 state->icount.rng = state->icount.dcd = 0;
2528 state->icount.rx = state->icount.tx = 0;
2529 state->icount.frame = state->icount.parity = 0;
2530 state->icount.overrun = state->icount.brk = 0;
2531 printk(KERN_INFO "ttyS%d at irq 0x%02x is an %s\n",
2532 i, (unsigned int)(state->irq),
2533 (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC");
2534
2535#ifdef CONFIG_SERIAL_CONSOLE
2536 /* If we just printed the message on the console port, and
2537 * we are about to initialize it for general use, we have
2538 * to wait a couple of character times for the CR/NL to
2539 * make it out of the transmit buffer.
2540 */
2541 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2542 mdelay(8);
2543
2544
2545/* idx = PORT_NUM(info->state->smc_scc_num); */
2546/* if (info->state->smc_scc_num & NUM_IS_SCC) */
2547/* chan = scc_chan_map[idx]; */
2548/* else */
2549/* chan = smc_chan_map[idx]; */
2550
2551/* cp->cp_cr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG; */
2552/* while (cp->cp_cr & CPM_CR_FLG); */
2553
2554#endif
2555 /* info = kmalloc(sizeof(ser_info_t), GFP_KERNEL); */
2556 info = &quicc_ser_info[i];
2557 if (info) {
2558 memset (info, 0, sizeof(ser_info_t));
2559 info->magic = SERIAL_MAGIC;
2560 info->line = i;
2561 info->flags = state->flags;
2562 INIT_WORK(&info->tqueue, do_softint, info);
2563 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2564 init_waitqueue_head(&info->open_wait);
2565 init_waitqueue_head(&info->close_wait);
2566 info->state = state;
2567 state->info = (struct async_struct *)info;
2568
2569 /* We need to allocate a transmit and receive buffer
2570 * descriptors from dual port ram, and a character
2571 * buffer area from host mem.
2572 */
2573 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * RX_NUM_FIFO);
2574
2575 /* Allocate space for FIFOs in the host memory.
2576 * (for now this is from a static array of buffers :(
2577 */
2578 /* mem_addr = m360_cpm_hostalloc(RX_NUM_FIFO * RX_BUF_SIZE); */
2579 /* mem_addr = kmalloc (RX_NUM_FIFO * RX_BUF_SIZE, GFP_BUFFER); */
2580 mem_addr = &rx_buf_pool[i * RX_NUM_FIFO * RX_BUF_SIZE];
2581
2582 /* Set the physical address of the host memory
2583 * buffers in the buffer descriptors, and the
2584 * virtual address for us to work with.
2585 */
2586 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2587 info->rx_cur = info->rx_bd_base = bdp;
2588
2589 /* initialize rx buffer descriptors */
2590 for (j=0; j<(RX_NUM_FIFO-1); j++) {
2591 bdp->buf = &rx_buf_pool[(i * RX_NUM_FIFO + j ) * RX_BUF_SIZE];
2592 bdp->status = BD_SC_EMPTY | BD_SC_INTRPT;
2593 mem_addr += RX_BUF_SIZE;
2594 bdp++;
2595 }
2596 bdp->buf = &rx_buf_pool[(i * RX_NUM_FIFO + j ) * RX_BUF_SIZE];
2597 bdp->status = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2598
2599
2600 idx = PORT_NUM(info->state->smc_scc_num);
2601 if (info->state->smc_scc_num & NUM_IS_SCC) {
2602
2603#if defined (CONFIG_UCQUICC) && 1
2604 /* set the transceiver mode to RS232 */
2605 sipex_mode_bits &= ~(uint)SIPEX_MODE(idx,0x0f); /* clear current mode */
2606 sipex_mode_bits |= (uint)SIPEX_MODE(idx,0x02);
2607 *(uint *)_periph_base = sipex_mode_bits;
2608 /* printk ("sipex bits = 0x%08x\n", sipex_mode_bits); */
2609#endif
2610 }
2611
2612 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * TX_NUM_FIFO);
2613
2614 /* Allocate space for FIFOs in the host memory.
2615 */
2616 /* mem_addr = m360_cpm_hostalloc(TX_NUM_FIFO * TX_BUF_SIZE); */
2617 /* mem_addr = kmalloc (TX_NUM_FIFO * TX_BUF_SIZE, GFP_BUFFER); */
2618 mem_addr = &tx_buf_pool[i * TX_NUM_FIFO * TX_BUF_SIZE];
2619
2620 /* Set the physical address of the host memory
2621 * buffers in the buffer descriptors, and the
2622 * virtual address for us to work with.
2623 */
2624 /* bdp = (QUICC_BD *)&cp->cp_dpmem[dp_addr]; */
2625 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2626 info->tx_cur = info->tx_bd_base = (QUICC_BD *)bdp;
2627
2628 /* initialize tx buffer descriptors */
2629 for (j=0; j<(TX_NUM_FIFO-1); j++) {
2630 bdp->buf = &tx_buf_pool[(i * TX_NUM_FIFO + j ) * TX_BUF_SIZE];
2631 bdp->status = BD_SC_INTRPT;
2632 mem_addr += TX_BUF_SIZE;
2633 bdp++;
2634 }
2635 bdp->buf = &tx_buf_pool[(i * TX_NUM_FIFO + j ) * TX_BUF_SIZE];
2636 bdp->status = (BD_SC_WRAP | BD_SC_INTRPT);
2637
2638 if (info->state->smc_scc_num & NUM_IS_SCC) {
2639 scp = &pquicc->scc_regs[idx];
2640 sup = &pquicc->pram[info->state->port].scc.pscc.u;
2641 sup->rbase = dp_addr;
2642 sup->tbase = dp_addr;
2643
2644 /* Set up the uart parameters in the
2645 * parameter ram.
2646 */
2647 sup->rfcr = SMC_EB;
2648 sup->tfcr = SMC_EB;
2649
2650 /* Set this to 1 for now, so we get single
2651 * character interrupts. Using idle character
2652 * time requires some additional tuning.
2653 */
2654 sup->mrblr = 1;
2655 sup->max_idl = 0;
2656 sup->brkcr = 1;
2657 sup->parec = 0;
2658 sup->frmer = 0;
2659 sup->nosec = 0;
2660 sup->brkec = 0;
2661 sup->uaddr1 = 0;
2662 sup->uaddr2 = 0;
2663 sup->toseq = 0;
2664 {
2665 int i;
2666 for (i=0;i<8;i++)
2667 sup->cc[i] = 0x8000;
2668 }
2669 sup->rccm = 0xc0ff;
2670
2671 /* Send the CPM an initialize command.
2672 */
2673 chan = scc_chan_map[idx];
2674
2675 /* execute the INIT RX & TX PARAMS command for this channel. */
2676 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2677 while (cp->cp_cr & CPM_CR_FLG);
2678
2679 /* Set UART mode, 8 bit, no parity, one stop.
2680 * Enable receive and transmit.
2681 */
2682 scp->scc_gsmr.w.high = 0;
2683 scp->scc_gsmr.w.low =
2684 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2685
2686 /* Disable all interrupts and clear all pending
2687 * events.
2688 */
2689 scp->scc_sccm = 0;
2690 scp->scc_scce = 0xffff;
2691 scp->scc_dsr = 0x7e7e;
2692 scp->scc_psmr = 0x3000;
2693
2694 /* If the port is the console, enable Rx and Tx.
2695 */
2696#ifdef CONFIG_SERIAL_CONSOLE
2697 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2698 scp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2699#endif
2700 }
2701 else {
2702 /* Configure SMCs Tx/Rx instead of port B
2703 * parallel I/O.
2704 */
2705 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
2706 up->rbase = dp_addr;
2707
2708 iobits = 0xc0 << (idx * 4);
2709 cp->pip_pbpar |= iobits;
2710 cp->pip_pbdir &= ~iobits;
2711 cp->pip_pbodr &= ~iobits;
2712
2713
2714 /* Connect the baud rate generator to the
2715 * SMC based upon index in rs_table. Also
2716 * make sure it is connected to NMSI.
2717 */
2718 cp->si_simode &= ~(0xffff << (idx * 16));
2719 cp->si_simode |= (i << ((idx * 16) + 12));
2720
2721 up->tbase = dp_addr;
2722
2723 /* Set up the uart parameters in the
2724 * parameter ram.
2725 */
2726 up->rfcr = SMC_EB;
2727 up->tfcr = SMC_EB;
2728
2729 /* Set this to 1 for now, so we get single
2730 * character interrupts. Using idle character
2731 * time requires some additional tuning.
2732 */
2733 up->mrblr = 1;
2734 up->max_idl = 0;
2735 up->brkcr = 1;
2736
2737 /* Send the CPM an initialize command.
2738 */
2739 chan = smc_chan_map[idx];
2740
2741 cp->cp_cr = mk_cr_cmd(chan,
2742 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2743#ifdef CONFIG_SERIAL_CONSOLE
2744 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2745 printk("");
2746#endif
2747 while (cp->cp_cr & CPM_CR_FLG);
2748
2749 /* Set UART mode, 8 bit, no parity, one stop.
2750 * Enable receive and transmit.
2751 */
2752 sp = &cp->smc_regs[idx];
2753 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2754
2755 /* Disable all interrupts and clear all pending
2756 * events.
2757 */
2758 sp->smc_smcm = 0;
2759 sp->smc_smce = 0xff;
2760
2761 /* If the port is the console, enable Rx and Tx.
2762 */
2763#ifdef CONFIG_SERIAL_CONSOLE
2764 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2765 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2766#endif
2767 }
2768
2769 /* Install interrupt handler.
2770 */
2771 /* cpm_install_handler(IRQ_MACHSPEC | state->irq, rs_360_interrupt, info); */
2772 /*request_irq(IRQ_MACHSPEC | state->irq, rs_360_interrupt, */
2773 request_irq(state->irq, rs_360_interrupt,
2774 IRQ_FLG_LOCK, "ttyS", (void *)info);
2775
2776 /* Set up the baud rate generator.
2777 */
2778 m360_cpm_setbrg(i, baud_table[baud_idx]);
2779
2780 }
2781 }
2782
2783 return 0;
2784}
2785module_init(rs_360_init);
2786
2787/* This must always be called before the rs_360_init() function, otherwise
2788 * it blows away the port control information.
2789 */
2790//static int __init serial_console_setup( struct console *co, char *options)
2791int serial_console_setup( struct console *co, char *options)
2792{
2793 struct serial_state *ser;
2794 uint mem_addr, dp_addr, bidx, idx, iobits;
2795 ushort chan;
2796 QUICC_BD *bdp;
2797 volatile QUICC *cp;
2798 volatile struct smc_regs *sp;
2799 volatile struct scc_regs *scp;
2800 volatile struct smc_uart_pram *up;
2801 volatile struct uart_pram *sup;
2802
2803/* mleslie TODO:
2804 * add something to the 68k bootloader to store a desired initial console baud rate */
2805
2806/* bd_t *bd; */ /* a board info struct used by EPPC-bug */
2807/* bd = (bd_t *)__res; */
2808
2809 for (bidx = 0; bidx < (sizeof(baud_table) / sizeof(int)); bidx++)
2810 /* if (bd->bi_baudrate == baud_table[bidx]) */
2811 if (CONSOLE_BAUDRATE == baud_table[bidx])
2812 break;
2813
2814 /* co->cflag = CREAD|CLOCAL|bidx|CS8; */
2815 baud_idx = bidx;
2816
2817 ser = rs_table + CONFIG_SERIAL_CONSOLE_PORT;
2818
2819 cp = pquicc; /* Get pointer to Communication Processor */
2820
2821 idx = PORT_NUM(ser->smc_scc_num);
2822 if (ser->smc_scc_num & NUM_IS_SCC) {
2823
2824 /* TODO: need to set up SCC pin assignment etc. here */
2825
2826 }
2827 else {
2828 iobits = 0xc0 << (idx * 4);
2829 cp->pip_pbpar |= iobits;
2830 cp->pip_pbdir &= ~iobits;
2831 cp->pip_pbodr &= ~iobits;
2832
2833 /* Connect the baud rate generator to the
2834 * SMC based upon index in rs_table. Also
2835 * make sure it is connected to NMSI.
2836 */
2837 cp->si_simode &= ~(0xffff << (idx * 16));
2838 cp->si_simode |= (idx << ((idx * 16) + 12));
2839 }
2840
2841 /* When we get here, the CPM has been reset, so we need
2842 * to configure the port.
2843 * We need to allocate a transmit and receive buffer descriptor
2844 * from dual port ram, and a character buffer area from host mem.
2845 */
2846
2847 /* Allocate space for two buffer descriptors in the DP ram.
2848 */
2849 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * CONSOLE_NUM_FIFO);
2850
2851 /* Allocate space for two 2 byte FIFOs in the host memory.
2852 */
2853 /* mem_addr = m360_cpm_hostalloc(8); */
2854 mem_addr = (uint)console_fifos;
2855
2856
2857 /* Set the physical address of the host memory buffers in
2858 * the buffer descriptors.
2859 */
2860 /* bdp = (QUICC_BD *)&cp->cp_dpmem[dp_addr]; */
2861 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2862 bdp->buf = (char *)mem_addr;
2863 (bdp+1)->buf = (char *)(mem_addr+4);
2864
2865 /* For the receive, set empty and wrap.
2866 * For transmit, set wrap.
2867 */
2868 bdp->status = BD_SC_EMPTY | BD_SC_WRAP;
2869 (bdp+1)->status = BD_SC_WRAP;
2870
2871 /* Set up the uart parameters in the parameter ram.
2872 */
2873 if (ser->smc_scc_num & NUM_IS_SCC) {
2874 scp = &cp->scc_regs[idx];
2875 /* sup = (scc_uart_t *)&cp->cp_dparam[ser->port]; */
2876 sup = &pquicc->pram[ser->port].scc.pscc.u;
2877
2878 sup->rbase = dp_addr;
2879 sup->tbase = dp_addr + sizeof(QUICC_BD);
2880
2881 /* Set up the uart parameters in the
2882 * parameter ram.
2883 */
2884 sup->rfcr = SMC_EB;
2885 sup->tfcr = SMC_EB;
2886
2887 /* Set this to 1 for now, so we get single
2888 * character interrupts. Using idle character
2889 * time requires some additional tuning.
2890 */
2891 sup->mrblr = 1;
2892 sup->max_idl = 0;
2893 sup->brkcr = 1;
2894 sup->parec = 0;
2895 sup->frmer = 0;
2896 sup->nosec = 0;
2897 sup->brkec = 0;
2898 sup->uaddr1 = 0;
2899 sup->uaddr2 = 0;
2900 sup->toseq = 0;
2901 {
2902 int i;
2903 for (i=0;i<8;i++)
2904 sup->cc[i] = 0x8000;
2905 }
2906 sup->rccm = 0xc0ff;
2907
2908 /* Send the CPM an initialize command.
2909 */
2910 chan = scc_chan_map[idx];
2911
2912 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2913 while (cp->cp_cr & CPM_CR_FLG);
2914
2915 /* Set UART mode, 8 bit, no parity, one stop.
2916 * Enable receive and transmit.
2917 */
2918 scp->scc_gsmr.w.high = 0;
2919 scp->scc_gsmr.w.low =
2920 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2921
2922 /* Disable all interrupts and clear all pending
2923 * events.
2924 */
2925 scp->scc_sccm = 0;
2926 scp->scc_scce = 0xffff;
2927 scp->scc_dsr = 0x7e7e;
2928 scp->scc_psmr = 0x3000;
2929
2930 scp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2931
2932 }
2933 else {
2934 /* up = (smc_uart_t *)&cp->cp_dparam[ser->port]; */
2935 up = &pquicc->pram[ser->port].scc.pothers.idma_smc.psmc.u;
2936
2937 up->rbase = dp_addr; /* Base of receive buffer desc. */
2938 up->tbase = dp_addr+sizeof(QUICC_BD); /* Base of xmt buffer desc. */
2939 up->rfcr = SMC_EB;
2940 up->tfcr = SMC_EB;
2941
2942 /* Set this to 1 for now, so we get single character interrupts.
2943 */
2944 up->mrblr = 1; /* receive buffer length */
2945 up->max_idl = 0; /* wait forever for next char */
2946
2947 /* Send the CPM an initialize command.
2948 */
2949 chan = smc_chan_map[idx];
2950 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2951 while (cp->cp_cr & CPM_CR_FLG);
2952
2953 /* Set UART mode, 8 bit, no parity, one stop.
2954 * Enable receive and transmit.
2955 */
2956 sp = &cp->smc_regs[idx];
2957 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2958
2959 /* And finally, enable Rx and Tx.
2960 */
2961 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2962 }
2963
2964 /* Set up the baud rate generator.
2965 */
2966 /* m360_cpm_setbrg((ser - rs_table), bd->bi_baudrate); */
2967 m360_cpm_setbrg((ser - rs_table), CONSOLE_BAUDRATE);
2968
2969 return 0;
2970}
2971
2972/*
2973 * Local variables:
2974 * c-indent-level: 4
2975 * c-basic-offset: 4
2976 * tab-width: 4
2977 * End:
2978 */
diff --git a/drivers/tty/serial/8250.c b/drivers/tty/serial/8250.c
new file mode 100644
index 000000000000..b25e6e490530
--- /dev/null
+++ b/drivers/tty/serial/8250.c
@@ -0,0 +1,3377 @@
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 * A note about mapbase / membase
16 *
17 * mapbase is the physical address of the IO port.
18 * membase is an 'ioremapped' cookie.
19 */
20
21#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
22#define SUPPORT_SYSRQ
23#endif
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/ioport.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/sysrq.h>
31#include <linux/delay.h>
32#include <linux/platform_device.h>
33#include <linux/tty.h>
34#include <linux/ratelimit.h>
35#include <linux/tty_flip.h>
36#include <linux/serial_reg.h>
37#include <linux/serial_core.h>
38#include <linux/serial.h>
39#include <linux/serial_8250.h>
40#include <linux/nmi.h>
41#include <linux/mutex.h>
42#include <linux/slab.h>
43
44#include <asm/io.h>
45#include <asm/irq.h>
46
47#include "8250.h"
48
49#ifdef CONFIG_SPARC
50#include "suncore.h"
51#endif
52
53/*
54 * Configuration:
55 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
56 * is unsafe when used on edge-triggered interrupts.
57 */
58static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
59
60static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
61
62static struct uart_driver serial8250_reg;
63
64static int serial_index(struct uart_port *port)
65{
66 return (serial8250_reg.minor - 64) + port->line;
67}
68
69static unsigned int skip_txen_test; /* force skip of txen test at init time */
70
71/*
72 * Debugging.
73 */
74#if 0
75#define DEBUG_AUTOCONF(fmt...) printk(fmt)
76#else
77#define DEBUG_AUTOCONF(fmt...) do { } while (0)
78#endif
79
80#if 0
81#define DEBUG_INTR(fmt...) printk(fmt)
82#else
83#define DEBUG_INTR(fmt...) do { } while (0)
84#endif
85
86#define PASS_LIMIT 256
87
88#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
89
90
91/*
92 * We default to IRQ0 for the "no irq" hack. Some
93 * machine types want others as well - they're free
94 * to redefine this in their header file.
95 */
96#define is_real_interrupt(irq) ((irq) != 0)
97
98#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
99#define CONFIG_SERIAL_DETECT_IRQ 1
100#endif
101#ifdef CONFIG_SERIAL_8250_MANY_PORTS
102#define CONFIG_SERIAL_MANY_PORTS 1
103#endif
104
105/*
106 * HUB6 is always on. This will be removed once the header
107 * files have been cleaned.
108 */
109#define CONFIG_HUB6 1
110
111#include <asm/serial.h>
112/*
113 * SERIAL_PORT_DFNS tells us about built-in ports that have no
114 * standard enumeration mechanism. Platforms that can find all
115 * serial ports via mechanisms like ACPI or PCI need not supply it.
116 */
117#ifndef SERIAL_PORT_DFNS
118#define SERIAL_PORT_DFNS
119#endif
120
121static const struct old_serial_port old_serial_port[] = {
122 SERIAL_PORT_DFNS /* defined in asm/serial.h */
123};
124
125#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
126
127#ifdef CONFIG_SERIAL_8250_RSA
128
129#define PORT_RSA_MAX 4
130static unsigned long probe_rsa[PORT_RSA_MAX];
131static unsigned int probe_rsa_count;
132#endif /* CONFIG_SERIAL_8250_RSA */
133
134struct uart_8250_port {
135 struct uart_port port;
136 struct timer_list timer; /* "no irq" timer */
137 struct list_head list; /* ports on this IRQ */
138 unsigned short capabilities; /* port capabilities */
139 unsigned short bugs; /* port bugs */
140 unsigned int tx_loadsz; /* transmit fifo load size */
141 unsigned char acr;
142 unsigned char ier;
143 unsigned char lcr;
144 unsigned char mcr;
145 unsigned char mcr_mask; /* mask of user bits */
146 unsigned char mcr_force; /* mask of forced bits */
147 unsigned char cur_iotype; /* Running I/O type */
148
149 /*
150 * Some bits in registers are cleared on a read, so they must
151 * be saved whenever the register is read but the bits will not
152 * be immediately processed.
153 */
154#define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
155 unsigned char lsr_saved_flags;
156#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
157 unsigned char msr_saved_flags;
158};
159
160struct irq_info {
161 struct hlist_node node;
162 int irq;
163 spinlock_t lock; /* Protects list not the hash */
164 struct list_head *head;
165};
166
167#define NR_IRQ_HASH 32 /* Can be adjusted later */
168static struct hlist_head irq_lists[NR_IRQ_HASH];
169static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
170
171/*
172 * Here we define the default xmit fifo size used for each type of UART.
173 */
174static const struct serial8250_config uart_config[] = {
175 [PORT_UNKNOWN] = {
176 .name = "unknown",
177 .fifo_size = 1,
178 .tx_loadsz = 1,
179 },
180 [PORT_8250] = {
181 .name = "8250",
182 .fifo_size = 1,
183 .tx_loadsz = 1,
184 },
185 [PORT_16450] = {
186 .name = "16450",
187 .fifo_size = 1,
188 .tx_loadsz = 1,
189 },
190 [PORT_16550] = {
191 .name = "16550",
192 .fifo_size = 1,
193 .tx_loadsz = 1,
194 },
195 [PORT_16550A] = {
196 .name = "16550A",
197 .fifo_size = 16,
198 .tx_loadsz = 16,
199 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
200 .flags = UART_CAP_FIFO,
201 },
202 [PORT_CIRRUS] = {
203 .name = "Cirrus",
204 .fifo_size = 1,
205 .tx_loadsz = 1,
206 },
207 [PORT_16650] = {
208 .name = "ST16650",
209 .fifo_size = 1,
210 .tx_loadsz = 1,
211 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
212 },
213 [PORT_16650V2] = {
214 .name = "ST16650V2",
215 .fifo_size = 32,
216 .tx_loadsz = 16,
217 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
218 UART_FCR_T_TRIG_00,
219 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
220 },
221 [PORT_16750] = {
222 .name = "TI16750",
223 .fifo_size = 64,
224 .tx_loadsz = 64,
225 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
226 UART_FCR7_64BYTE,
227 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
228 },
229 [PORT_STARTECH] = {
230 .name = "Startech",
231 .fifo_size = 1,
232 .tx_loadsz = 1,
233 },
234 [PORT_16C950] = {
235 .name = "16C950/954",
236 .fifo_size = 128,
237 .tx_loadsz = 128,
238 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
239 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
240 },
241 [PORT_16654] = {
242 .name = "ST16654",
243 .fifo_size = 64,
244 .tx_loadsz = 32,
245 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
246 UART_FCR_T_TRIG_10,
247 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
248 },
249 [PORT_16850] = {
250 .name = "XR16850",
251 .fifo_size = 128,
252 .tx_loadsz = 128,
253 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
254 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
255 },
256 [PORT_RSA] = {
257 .name = "RSA",
258 .fifo_size = 2048,
259 .tx_loadsz = 2048,
260 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
261 .flags = UART_CAP_FIFO,
262 },
263 [PORT_NS16550A] = {
264 .name = "NS16550A",
265 .fifo_size = 16,
266 .tx_loadsz = 16,
267 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
268 .flags = UART_CAP_FIFO | UART_NATSEMI,
269 },
270 [PORT_XSCALE] = {
271 .name = "XScale",
272 .fifo_size = 32,
273 .tx_loadsz = 32,
274 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
275 .flags = UART_CAP_FIFO | UART_CAP_UUE,
276 },
277 [PORT_RM9000] = {
278 .name = "RM9000",
279 .fifo_size = 16,
280 .tx_loadsz = 16,
281 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
282 .flags = UART_CAP_FIFO,
283 },
284 [PORT_OCTEON] = {
285 .name = "OCTEON",
286 .fifo_size = 64,
287 .tx_loadsz = 64,
288 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
289 .flags = UART_CAP_FIFO,
290 },
291 [PORT_AR7] = {
292 .name = "AR7",
293 .fifo_size = 16,
294 .tx_loadsz = 16,
295 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
296 .flags = UART_CAP_FIFO | UART_CAP_AFE,
297 },
298 [PORT_U6_16550A] = {
299 .name = "U6_16550A",
300 .fifo_size = 64,
301 .tx_loadsz = 64,
302 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
303 .flags = UART_CAP_FIFO | UART_CAP_AFE,
304 },
305};
306
307#if defined(CONFIG_MIPS_ALCHEMY)
308
309/* Au1x00 UART hardware has a weird register layout */
310static const u8 au_io_in_map[] = {
311 [UART_RX] = 0,
312 [UART_IER] = 2,
313 [UART_IIR] = 3,
314 [UART_LCR] = 5,
315 [UART_MCR] = 6,
316 [UART_LSR] = 7,
317 [UART_MSR] = 8,
318};
319
320static const u8 au_io_out_map[] = {
321 [UART_TX] = 1,
322 [UART_IER] = 2,
323 [UART_FCR] = 4,
324 [UART_LCR] = 5,
325 [UART_MCR] = 6,
326};
327
328/* sane hardware needs no mapping */
329static inline int map_8250_in_reg(struct uart_port *p, int offset)
330{
331 if (p->iotype != UPIO_AU)
332 return offset;
333 return au_io_in_map[offset];
334}
335
336static inline int map_8250_out_reg(struct uart_port *p, int offset)
337{
338 if (p->iotype != UPIO_AU)
339 return offset;
340 return au_io_out_map[offset];
341}
342
343#elif defined(CONFIG_SERIAL_8250_RM9K)
344
345static const u8
346 regmap_in[8] = {
347 [UART_RX] = 0x00,
348 [UART_IER] = 0x0c,
349 [UART_IIR] = 0x14,
350 [UART_LCR] = 0x1c,
351 [UART_MCR] = 0x20,
352 [UART_LSR] = 0x24,
353 [UART_MSR] = 0x28,
354 [UART_SCR] = 0x2c
355 },
356 regmap_out[8] = {
357 [UART_TX] = 0x04,
358 [UART_IER] = 0x0c,
359 [UART_FCR] = 0x18,
360 [UART_LCR] = 0x1c,
361 [UART_MCR] = 0x20,
362 [UART_LSR] = 0x24,
363 [UART_MSR] = 0x28,
364 [UART_SCR] = 0x2c
365 };
366
367static inline int map_8250_in_reg(struct uart_port *p, int offset)
368{
369 if (p->iotype != UPIO_RM9000)
370 return offset;
371 return regmap_in[offset];
372}
373
374static inline int map_8250_out_reg(struct uart_port *p, int offset)
375{
376 if (p->iotype != UPIO_RM9000)
377 return offset;
378 return regmap_out[offset];
379}
380
381#else
382
383/* sane hardware needs no mapping */
384#define map_8250_in_reg(up, offset) (offset)
385#define map_8250_out_reg(up, offset) (offset)
386
387#endif
388
389static unsigned int hub6_serial_in(struct uart_port *p, int offset)
390{
391 offset = map_8250_in_reg(p, offset) << p->regshift;
392 outb(p->hub6 - 1 + offset, p->iobase);
393 return inb(p->iobase + 1);
394}
395
396static void hub6_serial_out(struct uart_port *p, int offset, int value)
397{
398 offset = map_8250_out_reg(p, offset) << p->regshift;
399 outb(p->hub6 - 1 + offset, p->iobase);
400 outb(value, p->iobase + 1);
401}
402
403static unsigned int mem_serial_in(struct uart_port *p, int offset)
404{
405 offset = map_8250_in_reg(p, offset) << p->regshift;
406 return readb(p->membase + offset);
407}
408
409static void mem_serial_out(struct uart_port *p, int offset, int value)
410{
411 offset = map_8250_out_reg(p, offset) << p->regshift;
412 writeb(value, p->membase + offset);
413}
414
415static void mem32_serial_out(struct uart_port *p, int offset, int value)
416{
417 offset = map_8250_out_reg(p, offset) << p->regshift;
418 writel(value, p->membase + offset);
419}
420
421static unsigned int mem32_serial_in(struct uart_port *p, int offset)
422{
423 offset = map_8250_in_reg(p, offset) << p->regshift;
424 return readl(p->membase + offset);
425}
426
427static unsigned int au_serial_in(struct uart_port *p, int offset)
428{
429 offset = map_8250_in_reg(p, offset) << p->regshift;
430 return __raw_readl(p->membase + offset);
431}
432
433static void au_serial_out(struct uart_port *p, int offset, int value)
434{
435 offset = map_8250_out_reg(p, offset) << p->regshift;
436 __raw_writel(value, p->membase + offset);
437}
438
439static unsigned int tsi_serial_in(struct uart_port *p, int offset)
440{
441 unsigned int tmp;
442 offset = map_8250_in_reg(p, offset) << p->regshift;
443 if (offset == UART_IIR) {
444 tmp = readl(p->membase + (UART_IIR & ~3));
445 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
446 } else
447 return readb(p->membase + offset);
448}
449
450static void tsi_serial_out(struct uart_port *p, int offset, int value)
451{
452 offset = map_8250_out_reg(p, offset) << p->regshift;
453 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
454 writeb(value, p->membase + offset);
455}
456
457/* Save the LCR value so it can be re-written when a Busy Detect IRQ occurs. */
458static inline void dwapb_save_out_value(struct uart_port *p, int offset,
459 int value)
460{
461 struct uart_8250_port *up =
462 container_of(p, struct uart_8250_port, port);
463
464 if (offset == UART_LCR)
465 up->lcr = value;
466}
467
468/* Read the IER to ensure any interrupt is cleared before returning from ISR. */
469static inline void dwapb_check_clear_ier(struct uart_port *p, int offset)
470{
471 if (offset == UART_TX || offset == UART_IER)
472 p->serial_in(p, UART_IER);
473}
474
475static void dwapb_serial_out(struct uart_port *p, int offset, int value)
476{
477 int save_offset = offset;
478 offset = map_8250_out_reg(p, offset) << p->regshift;
479 dwapb_save_out_value(p, save_offset, value);
480 writeb(value, p->membase + offset);
481 dwapb_check_clear_ier(p, save_offset);
482}
483
484static void dwapb32_serial_out(struct uart_port *p, int offset, int value)
485{
486 int save_offset = offset;
487 offset = map_8250_out_reg(p, offset) << p->regshift;
488 dwapb_save_out_value(p, save_offset, value);
489 writel(value, p->membase + offset);
490 dwapb_check_clear_ier(p, save_offset);
491}
492
493static unsigned int io_serial_in(struct uart_port *p, int offset)
494{
495 offset = map_8250_in_reg(p, offset) << p->regshift;
496 return inb(p->iobase + offset);
497}
498
499static void io_serial_out(struct uart_port *p, int offset, int value)
500{
501 offset = map_8250_out_reg(p, offset) << p->regshift;
502 outb(value, p->iobase + offset);
503}
504
505static void set_io_from_upio(struct uart_port *p)
506{
507 struct uart_8250_port *up =
508 container_of(p, struct uart_8250_port, port);
509 switch (p->iotype) {
510 case UPIO_HUB6:
511 p->serial_in = hub6_serial_in;
512 p->serial_out = hub6_serial_out;
513 break;
514
515 case UPIO_MEM:
516 p->serial_in = mem_serial_in;
517 p->serial_out = mem_serial_out;
518 break;
519
520 case UPIO_RM9000:
521 case UPIO_MEM32:
522 p->serial_in = mem32_serial_in;
523 p->serial_out = mem32_serial_out;
524 break;
525
526 case UPIO_AU:
527 p->serial_in = au_serial_in;
528 p->serial_out = au_serial_out;
529 break;
530
531 case UPIO_TSI:
532 p->serial_in = tsi_serial_in;
533 p->serial_out = tsi_serial_out;
534 break;
535
536 case UPIO_DWAPB:
537 p->serial_in = mem_serial_in;
538 p->serial_out = dwapb_serial_out;
539 break;
540
541 case UPIO_DWAPB32:
542 p->serial_in = mem32_serial_in;
543 p->serial_out = dwapb32_serial_out;
544 break;
545
546 default:
547 p->serial_in = io_serial_in;
548 p->serial_out = io_serial_out;
549 break;
550 }
551 /* Remember loaded iotype */
552 up->cur_iotype = p->iotype;
553}
554
555static void
556serial_out_sync(struct uart_8250_port *up, int offset, int value)
557{
558 struct uart_port *p = &up->port;
559 switch (p->iotype) {
560 case UPIO_MEM:
561 case UPIO_MEM32:
562 case UPIO_AU:
563 case UPIO_DWAPB:
564 case UPIO_DWAPB32:
565 p->serial_out(p, offset, value);
566 p->serial_in(p, UART_LCR); /* safe, no side-effects */
567 break;
568 default:
569 p->serial_out(p, offset, value);
570 }
571}
572
573#define serial_in(up, offset) \
574 (up->port.serial_in(&(up)->port, (offset)))
575#define serial_out(up, offset, value) \
576 (up->port.serial_out(&(up)->port, (offset), (value)))
577/*
578 * We used to support using pause I/O for certain machines. We
579 * haven't supported this for a while, but just in case it's badly
580 * needed for certain old 386 machines, I've left these #define's
581 * in....
582 */
583#define serial_inp(up, offset) serial_in(up, offset)
584#define serial_outp(up, offset, value) serial_out(up, offset, value)
585
586/* Uart divisor latch read */
587static inline int _serial_dl_read(struct uart_8250_port *up)
588{
589 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
590}
591
592/* Uart divisor latch write */
593static inline void _serial_dl_write(struct uart_8250_port *up, int value)
594{
595 serial_outp(up, UART_DLL, value & 0xff);
596 serial_outp(up, UART_DLM, value >> 8 & 0xff);
597}
598
599#if defined(CONFIG_MIPS_ALCHEMY)
600/* Au1x00 haven't got a standard divisor latch */
601static int serial_dl_read(struct uart_8250_port *up)
602{
603 if (up->port.iotype == UPIO_AU)
604 return __raw_readl(up->port.membase + 0x28);
605 else
606 return _serial_dl_read(up);
607}
608
609static void serial_dl_write(struct uart_8250_port *up, int value)
610{
611 if (up->port.iotype == UPIO_AU)
612 __raw_writel(value, up->port.membase + 0x28);
613 else
614 _serial_dl_write(up, value);
615}
616#elif defined(CONFIG_SERIAL_8250_RM9K)
617static int serial_dl_read(struct uart_8250_port *up)
618{
619 return (up->port.iotype == UPIO_RM9000) ?
620 (((__raw_readl(up->port.membase + 0x10) << 8) |
621 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
622 _serial_dl_read(up);
623}
624
625static void serial_dl_write(struct uart_8250_port *up, int value)
626{
627 if (up->port.iotype == UPIO_RM9000) {
628 __raw_writel(value, up->port.membase + 0x08);
629 __raw_writel(value >> 8, up->port.membase + 0x10);
630 } else {
631 _serial_dl_write(up, value);
632 }
633}
634#else
635#define serial_dl_read(up) _serial_dl_read(up)
636#define serial_dl_write(up, value) _serial_dl_write(up, value)
637#endif
638
639/*
640 * For the 16C950
641 */
642static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
643{
644 serial_out(up, UART_SCR, offset);
645 serial_out(up, UART_ICR, value);
646}
647
648static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
649{
650 unsigned int value;
651
652 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
653 serial_out(up, UART_SCR, offset);
654 value = serial_in(up, UART_ICR);
655 serial_icr_write(up, UART_ACR, up->acr);
656
657 return value;
658}
659
660/*
661 * FIFO support.
662 */
663static void serial8250_clear_fifos(struct uart_8250_port *p)
664{
665 if (p->capabilities & UART_CAP_FIFO) {
666 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
667 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
668 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
669 serial_outp(p, UART_FCR, 0);
670 }
671}
672
673/*
674 * IER sleep support. UARTs which have EFRs need the "extended
675 * capability" bit enabled. Note that on XR16C850s, we need to
676 * reset LCR to write to IER.
677 */
678static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
679{
680 if (p->capabilities & UART_CAP_SLEEP) {
681 if (p->capabilities & UART_CAP_EFR) {
682 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
683 serial_outp(p, UART_EFR, UART_EFR_ECB);
684 serial_outp(p, UART_LCR, 0);
685 }
686 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
687 if (p->capabilities & UART_CAP_EFR) {
688 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
689 serial_outp(p, UART_EFR, 0);
690 serial_outp(p, UART_LCR, 0);
691 }
692 }
693}
694
695#ifdef CONFIG_SERIAL_8250_RSA
696/*
697 * Attempts to turn on the RSA FIFO. Returns zero on failure.
698 * We set the port uart clock rate if we succeed.
699 */
700static int __enable_rsa(struct uart_8250_port *up)
701{
702 unsigned char mode;
703 int result;
704
705 mode = serial_inp(up, UART_RSA_MSR);
706 result = mode & UART_RSA_MSR_FIFO;
707
708 if (!result) {
709 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
710 mode = serial_inp(up, UART_RSA_MSR);
711 result = mode & UART_RSA_MSR_FIFO;
712 }
713
714 if (result)
715 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
716
717 return result;
718}
719
720static void enable_rsa(struct uart_8250_port *up)
721{
722 if (up->port.type == PORT_RSA) {
723 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
724 spin_lock_irq(&up->port.lock);
725 __enable_rsa(up);
726 spin_unlock_irq(&up->port.lock);
727 }
728 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
729 serial_outp(up, UART_RSA_FRR, 0);
730 }
731}
732
733/*
734 * Attempts to turn off the RSA FIFO. Returns zero on failure.
735 * It is unknown why interrupts were disabled in here. However,
736 * the caller is expected to preserve this behaviour by grabbing
737 * the spinlock before calling this function.
738 */
739static void disable_rsa(struct uart_8250_port *up)
740{
741 unsigned char mode;
742 int result;
743
744 if (up->port.type == PORT_RSA &&
745 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
746 spin_lock_irq(&up->port.lock);
747
748 mode = serial_inp(up, UART_RSA_MSR);
749 result = !(mode & UART_RSA_MSR_FIFO);
750
751 if (!result) {
752 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
753 mode = serial_inp(up, UART_RSA_MSR);
754 result = !(mode & UART_RSA_MSR_FIFO);
755 }
756
757 if (result)
758 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
759 spin_unlock_irq(&up->port.lock);
760 }
761}
762#endif /* CONFIG_SERIAL_8250_RSA */
763
764/*
765 * This is a quickie test to see how big the FIFO is.
766 * It doesn't work at all the time, more's the pity.
767 */
768static int size_fifo(struct uart_8250_port *up)
769{
770 unsigned char old_fcr, old_mcr, old_lcr;
771 unsigned short old_dl;
772 int count;
773
774 old_lcr = serial_inp(up, UART_LCR);
775 serial_outp(up, UART_LCR, 0);
776 old_fcr = serial_inp(up, UART_FCR);
777 old_mcr = serial_inp(up, UART_MCR);
778 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
779 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
780 serial_outp(up, UART_MCR, UART_MCR_LOOP);
781 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
782 old_dl = serial_dl_read(up);
783 serial_dl_write(up, 0x0001);
784 serial_outp(up, UART_LCR, 0x03);
785 for (count = 0; count < 256; count++)
786 serial_outp(up, UART_TX, count);
787 mdelay(20);/* FIXME - schedule_timeout */
788 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
789 (count < 256); count++)
790 serial_inp(up, UART_RX);
791 serial_outp(up, UART_FCR, old_fcr);
792 serial_outp(up, UART_MCR, old_mcr);
793 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
794 serial_dl_write(up, old_dl);
795 serial_outp(up, UART_LCR, old_lcr);
796
797 return count;
798}
799
800/*
801 * Read UART ID using the divisor method - set DLL and DLM to zero
802 * and the revision will be in DLL and device type in DLM. We
803 * preserve the device state across this.
804 */
805static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
806{
807 unsigned char old_dll, old_dlm, old_lcr;
808 unsigned int id;
809
810 old_lcr = serial_inp(p, UART_LCR);
811 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_A);
812
813 old_dll = serial_inp(p, UART_DLL);
814 old_dlm = serial_inp(p, UART_DLM);
815
816 serial_outp(p, UART_DLL, 0);
817 serial_outp(p, UART_DLM, 0);
818
819 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
820
821 serial_outp(p, UART_DLL, old_dll);
822 serial_outp(p, UART_DLM, old_dlm);
823 serial_outp(p, UART_LCR, old_lcr);
824
825 return id;
826}
827
828/*
829 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
830 * When this function is called we know it is at least a StarTech
831 * 16650 V2, but it might be one of several StarTech UARTs, or one of
832 * its clones. (We treat the broken original StarTech 16650 V1 as a
833 * 16550, and why not? Startech doesn't seem to even acknowledge its
834 * existence.)
835 *
836 * What evil have men's minds wrought...
837 */
838static void autoconfig_has_efr(struct uart_8250_port *up)
839{
840 unsigned int id1, id2, id3, rev;
841
842 /*
843 * Everything with an EFR has SLEEP
844 */
845 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
846
847 /*
848 * First we check to see if it's an Oxford Semiconductor UART.
849 *
850 * If we have to do this here because some non-National
851 * Semiconductor clone chips lock up if you try writing to the
852 * LSR register (which serial_icr_read does)
853 */
854
855 /*
856 * Check for Oxford Semiconductor 16C950.
857 *
858 * EFR [4] must be set else this test fails.
859 *
860 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
861 * claims that it's needed for 952 dual UART's (which are not
862 * recommended for new designs).
863 */
864 up->acr = 0;
865 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
866 serial_out(up, UART_EFR, UART_EFR_ECB);
867 serial_out(up, UART_LCR, 0x00);
868 id1 = serial_icr_read(up, UART_ID1);
869 id2 = serial_icr_read(up, UART_ID2);
870 id3 = serial_icr_read(up, UART_ID3);
871 rev = serial_icr_read(up, UART_REV);
872
873 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
874
875 if (id1 == 0x16 && id2 == 0xC9 &&
876 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
877 up->port.type = PORT_16C950;
878
879 /*
880 * Enable work around for the Oxford Semiconductor 952 rev B
881 * chip which causes it to seriously miscalculate baud rates
882 * when DLL is 0.
883 */
884 if (id3 == 0x52 && rev == 0x01)
885 up->bugs |= UART_BUG_QUOT;
886 return;
887 }
888
889 /*
890 * We check for a XR16C850 by setting DLL and DLM to 0, and then
891 * reading back DLL and DLM. The chip type depends on the DLM
892 * value read back:
893 * 0x10 - XR16C850 and the DLL contains the chip revision.
894 * 0x12 - XR16C2850.
895 * 0x14 - XR16C854.
896 */
897 id1 = autoconfig_read_divisor_id(up);
898 DEBUG_AUTOCONF("850id=%04x ", id1);
899
900 id2 = id1 >> 8;
901 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
902 up->port.type = PORT_16850;
903 return;
904 }
905
906 /*
907 * It wasn't an XR16C850.
908 *
909 * We distinguish between the '654 and the '650 by counting
910 * how many bytes are in the FIFO. I'm using this for now,
911 * since that's the technique that was sent to me in the
912 * serial driver update, but I'm not convinced this works.
913 * I've had problems doing this in the past. -TYT
914 */
915 if (size_fifo(up) == 64)
916 up->port.type = PORT_16654;
917 else
918 up->port.type = PORT_16650V2;
919}
920
921/*
922 * We detected a chip without a FIFO. Only two fall into
923 * this category - the original 8250 and the 16450. The
924 * 16450 has a scratch register (accessible with LCR=0)
925 */
926static void autoconfig_8250(struct uart_8250_port *up)
927{
928 unsigned char scratch, status1, status2;
929
930 up->port.type = PORT_8250;
931
932 scratch = serial_in(up, UART_SCR);
933 serial_outp(up, UART_SCR, 0xa5);
934 status1 = serial_in(up, UART_SCR);
935 serial_outp(up, UART_SCR, 0x5a);
936 status2 = serial_in(up, UART_SCR);
937 serial_outp(up, UART_SCR, scratch);
938
939 if (status1 == 0xa5 && status2 == 0x5a)
940 up->port.type = PORT_16450;
941}
942
943static int broken_efr(struct uart_8250_port *up)
944{
945 /*
946 * Exar ST16C2550 "A2" devices incorrectly detect as
947 * having an EFR, and report an ID of 0x0201. See
948 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
949 */
950 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
951 return 1;
952
953 return 0;
954}
955
956/*
957 * We know that the chip has FIFOs. Does it have an EFR? The
958 * EFR is located in the same register position as the IIR and
959 * we know the top two bits of the IIR are currently set. The
960 * EFR should contain zero. Try to read the EFR.
961 */
962static void autoconfig_16550a(struct uart_8250_port *up)
963{
964 unsigned char status1, status2;
965 unsigned int iersave;
966
967 up->port.type = PORT_16550A;
968 up->capabilities |= UART_CAP_FIFO;
969
970 /*
971 * Check for presence of the EFR when DLAB is set.
972 * Only ST16C650V1 UARTs pass this test.
973 */
974 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
975 if (serial_in(up, UART_EFR) == 0) {
976 serial_outp(up, UART_EFR, 0xA8);
977 if (serial_in(up, UART_EFR) != 0) {
978 DEBUG_AUTOCONF("EFRv1 ");
979 up->port.type = PORT_16650;
980 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
981 } else {
982 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
983 }
984 serial_outp(up, UART_EFR, 0);
985 return;
986 }
987
988 /*
989 * Maybe it requires 0xbf to be written to the LCR.
990 * (other ST16C650V2 UARTs, TI16C752A, etc)
991 */
992 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
993 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
994 DEBUG_AUTOCONF("EFRv2 ");
995 autoconfig_has_efr(up);
996 return;
997 }
998
999 /*
1000 * Check for a National Semiconductor SuperIO chip.
1001 * Attempt to switch to bank 2, read the value of the LOOP bit
1002 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1003 * switch back to bank 2, read it from EXCR1 again and check
1004 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1005 */
1006 serial_outp(up, UART_LCR, 0);
1007 status1 = serial_in(up, UART_MCR);
1008 serial_outp(up, UART_LCR, 0xE0);
1009 status2 = serial_in(up, 0x02); /* EXCR1 */
1010
1011 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1012 serial_outp(up, UART_LCR, 0);
1013 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
1014 serial_outp(up, UART_LCR, 0xE0);
1015 status2 = serial_in(up, 0x02); /* EXCR1 */
1016 serial_outp(up, UART_LCR, 0);
1017 serial_outp(up, UART_MCR, status1);
1018
1019 if ((status2 ^ status1) & UART_MCR_LOOP) {
1020 unsigned short quot;
1021
1022 serial_outp(up, UART_LCR, 0xE0);
1023
1024 quot = serial_dl_read(up);
1025 quot <<= 3;
1026
1027 status1 = serial_in(up, 0x04); /* EXCR2 */
1028 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
1029 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
1030 serial_outp(up, 0x04, status1);
1031
1032 serial_dl_write(up, quot);
1033
1034 serial_outp(up, UART_LCR, 0);
1035
1036 up->port.uartclk = 921600*16;
1037 up->port.type = PORT_NS16550A;
1038 up->capabilities |= UART_NATSEMI;
1039 return;
1040 }
1041 }
1042
1043 /*
1044 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1045 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1046 * Try setting it with and without DLAB set. Cheap clones
1047 * set bit 5 without DLAB set.
1048 */
1049 serial_outp(up, UART_LCR, 0);
1050 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1051 status1 = serial_in(up, UART_IIR) >> 5;
1052 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1053 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
1054 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1055 status2 = serial_in(up, UART_IIR) >> 5;
1056 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1057 serial_outp(up, UART_LCR, 0);
1058
1059 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1060
1061 if (status1 == 6 && status2 == 7) {
1062 up->port.type = PORT_16750;
1063 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1064 return;
1065 }
1066
1067 /*
1068 * Try writing and reading the UART_IER_UUE bit (b6).
1069 * If it works, this is probably one of the Xscale platform's
1070 * internal UARTs.
1071 * We're going to explicitly set the UUE bit to 0 before
1072 * trying to write and read a 1 just to make sure it's not
1073 * already a 1 and maybe locked there before we even start start.
1074 */
1075 iersave = serial_in(up, UART_IER);
1076 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
1077 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1078 /*
1079 * OK it's in a known zero state, try writing and reading
1080 * without disturbing the current state of the other bits.
1081 */
1082 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
1083 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1084 /*
1085 * It's an Xscale.
1086 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1087 */
1088 DEBUG_AUTOCONF("Xscale ");
1089 up->port.type = PORT_XSCALE;
1090 up->capabilities |= UART_CAP_UUE;
1091 return;
1092 }
1093 } else {
1094 /*
1095 * If we got here we couldn't force the IER_UUE bit to 0.
1096 * Log it and continue.
1097 */
1098 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1099 }
1100 serial_outp(up, UART_IER, iersave);
1101
1102 /*
1103 * We distinguish between 16550A and U6 16550A by counting
1104 * how many bytes are in the FIFO.
1105 */
1106 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1107 up->port.type = PORT_U6_16550A;
1108 up->capabilities |= UART_CAP_AFE;
1109 }
1110}
1111
1112/*
1113 * This routine is called by rs_init() to initialize a specific serial
1114 * port. It determines what type of UART chip this serial port is
1115 * using: 8250, 16450, 16550, 16550A. The important question is
1116 * whether or not this UART is a 16550A or not, since this will
1117 * determine whether or not we can use its FIFO features or not.
1118 */
1119static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1120{
1121 unsigned char status1, scratch, scratch2, scratch3;
1122 unsigned char save_lcr, save_mcr;
1123 unsigned long flags;
1124
1125 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1126 return;
1127
1128 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1129 serial_index(&up->port), up->port.iobase, up->port.membase);
1130
1131 /*
1132 * We really do need global IRQs disabled here - we're going to
1133 * be frobbing the chips IRQ enable register to see if it exists.
1134 */
1135 spin_lock_irqsave(&up->port.lock, flags);
1136
1137 up->capabilities = 0;
1138 up->bugs = 0;
1139
1140 if (!(up->port.flags & UPF_BUGGY_UART)) {
1141 /*
1142 * Do a simple existence test first; if we fail this,
1143 * there's no point trying anything else.
1144 *
1145 * 0x80 is used as a nonsense port to prevent against
1146 * false positives due to ISA bus float. The
1147 * assumption is that 0x80 is a non-existent port;
1148 * which should be safe since include/asm/io.h also
1149 * makes this assumption.
1150 *
1151 * Note: this is safe as long as MCR bit 4 is clear
1152 * and the device is in "PC" mode.
1153 */
1154 scratch = serial_inp(up, UART_IER);
1155 serial_outp(up, UART_IER, 0);
1156#ifdef __i386__
1157 outb(0xff, 0x080);
1158#endif
1159 /*
1160 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1161 * 16C754B) allow only to modify them if an EFR bit is set.
1162 */
1163 scratch2 = serial_inp(up, UART_IER) & 0x0f;
1164 serial_outp(up, UART_IER, 0x0F);
1165#ifdef __i386__
1166 outb(0, 0x080);
1167#endif
1168 scratch3 = serial_inp(up, UART_IER) & 0x0f;
1169 serial_outp(up, UART_IER, scratch);
1170 if (scratch2 != 0 || scratch3 != 0x0F) {
1171 /*
1172 * We failed; there's nothing here
1173 */
1174 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1175 scratch2, scratch3);
1176 goto out;
1177 }
1178 }
1179
1180 save_mcr = serial_in(up, UART_MCR);
1181 save_lcr = serial_in(up, UART_LCR);
1182
1183 /*
1184 * Check to see if a UART is really there. Certain broken
1185 * internal modems based on the Rockwell chipset fail this
1186 * test, because they apparently don't implement the loopback
1187 * test mode. So this test is skipped on the COM 1 through
1188 * COM 4 ports. This *should* be safe, since no board
1189 * manufacturer would be stupid enough to design a board
1190 * that conflicts with COM 1-4 --- we hope!
1191 */
1192 if (!(up->port.flags & UPF_SKIP_TEST)) {
1193 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1194 status1 = serial_inp(up, UART_MSR) & 0xF0;
1195 serial_outp(up, UART_MCR, save_mcr);
1196 if (status1 != 0x90) {
1197 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1198 status1);
1199 goto out;
1200 }
1201 }
1202
1203 /*
1204 * We're pretty sure there's a port here. Lets find out what
1205 * type of port it is. The IIR top two bits allows us to find
1206 * out if it's 8250 or 16450, 16550, 16550A or later. This
1207 * determines what we test for next.
1208 *
1209 * We also initialise the EFR (if any) to zero for later. The
1210 * EFR occupies the same register location as the FCR and IIR.
1211 */
1212 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
1213 serial_outp(up, UART_EFR, 0);
1214 serial_outp(up, UART_LCR, 0);
1215
1216 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1217 scratch = serial_in(up, UART_IIR) >> 6;
1218
1219 DEBUG_AUTOCONF("iir=%d ", scratch);
1220
1221 switch (scratch) {
1222 case 0:
1223 autoconfig_8250(up);
1224 break;
1225 case 1:
1226 up->port.type = PORT_UNKNOWN;
1227 break;
1228 case 2:
1229 up->port.type = PORT_16550;
1230 break;
1231 case 3:
1232 autoconfig_16550a(up);
1233 break;
1234 }
1235
1236#ifdef CONFIG_SERIAL_8250_RSA
1237 /*
1238 * Only probe for RSA ports if we got the region.
1239 */
1240 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1241 int i;
1242
1243 for (i = 0 ; i < probe_rsa_count; ++i) {
1244 if (probe_rsa[i] == up->port.iobase &&
1245 __enable_rsa(up)) {
1246 up->port.type = PORT_RSA;
1247 break;
1248 }
1249 }
1250 }
1251#endif
1252
1253 serial_outp(up, UART_LCR, save_lcr);
1254
1255 if (up->capabilities != uart_config[up->port.type].flags) {
1256 printk(KERN_WARNING
1257 "ttyS%d: detected caps %08x should be %08x\n",
1258 serial_index(&up->port), up->capabilities,
1259 uart_config[up->port.type].flags);
1260 }
1261
1262 up->port.fifosize = uart_config[up->port.type].fifo_size;
1263 up->capabilities = uart_config[up->port.type].flags;
1264 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1265
1266 if (up->port.type == PORT_UNKNOWN)
1267 goto out;
1268
1269 /*
1270 * Reset the UART.
1271 */
1272#ifdef CONFIG_SERIAL_8250_RSA
1273 if (up->port.type == PORT_RSA)
1274 serial_outp(up, UART_RSA_FRR, 0);
1275#endif
1276 serial_outp(up, UART_MCR, save_mcr);
1277 serial8250_clear_fifos(up);
1278 serial_in(up, UART_RX);
1279 if (up->capabilities & UART_CAP_UUE)
1280 serial_outp(up, UART_IER, UART_IER_UUE);
1281 else
1282 serial_outp(up, UART_IER, 0);
1283
1284 out:
1285 spin_unlock_irqrestore(&up->port.lock, flags);
1286 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1287}
1288
1289static void autoconfig_irq(struct uart_8250_port *up)
1290{
1291 unsigned char save_mcr, save_ier;
1292 unsigned char save_ICP = 0;
1293 unsigned int ICP = 0;
1294 unsigned long irqs;
1295 int irq;
1296
1297 if (up->port.flags & UPF_FOURPORT) {
1298 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1299 save_ICP = inb_p(ICP);
1300 outb_p(0x80, ICP);
1301 (void) inb_p(ICP);
1302 }
1303
1304 /* forget possible initially masked and pending IRQ */
1305 probe_irq_off(probe_irq_on());
1306 save_mcr = serial_inp(up, UART_MCR);
1307 save_ier = serial_inp(up, UART_IER);
1308 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1309
1310 irqs = probe_irq_on();
1311 serial_outp(up, UART_MCR, 0);
1312 udelay(10);
1313 if (up->port.flags & UPF_FOURPORT) {
1314 serial_outp(up, UART_MCR,
1315 UART_MCR_DTR | UART_MCR_RTS);
1316 } else {
1317 serial_outp(up, UART_MCR,
1318 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1319 }
1320 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1321 (void)serial_inp(up, UART_LSR);
1322 (void)serial_inp(up, UART_RX);
1323 (void)serial_inp(up, UART_IIR);
1324 (void)serial_inp(up, UART_MSR);
1325 serial_outp(up, UART_TX, 0xFF);
1326 udelay(20);
1327 irq = probe_irq_off(irqs);
1328
1329 serial_outp(up, UART_MCR, save_mcr);
1330 serial_outp(up, UART_IER, save_ier);
1331
1332 if (up->port.flags & UPF_FOURPORT)
1333 outb_p(save_ICP, ICP);
1334
1335 up->port.irq = (irq > 0) ? irq : 0;
1336}
1337
1338static inline void __stop_tx(struct uart_8250_port *p)
1339{
1340 if (p->ier & UART_IER_THRI) {
1341 p->ier &= ~UART_IER_THRI;
1342 serial_out(p, UART_IER, p->ier);
1343 }
1344}
1345
1346static void serial8250_stop_tx(struct uart_port *port)
1347{
1348 struct uart_8250_port *up =
1349 container_of(port, struct uart_8250_port, port);
1350
1351 __stop_tx(up);
1352
1353 /*
1354 * We really want to stop the transmitter from sending.
1355 */
1356 if (up->port.type == PORT_16C950) {
1357 up->acr |= UART_ACR_TXDIS;
1358 serial_icr_write(up, UART_ACR, up->acr);
1359 }
1360}
1361
1362static void transmit_chars(struct uart_8250_port *up);
1363
1364static void serial8250_start_tx(struct uart_port *port)
1365{
1366 struct uart_8250_port *up =
1367 container_of(port, struct uart_8250_port, port);
1368
1369 if (!(up->ier & UART_IER_THRI)) {
1370 up->ier |= UART_IER_THRI;
1371 serial_out(up, UART_IER, up->ier);
1372
1373 if (up->bugs & UART_BUG_TXEN) {
1374 unsigned char lsr;
1375 lsr = serial_in(up, UART_LSR);
1376 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1377 if ((up->port.type == PORT_RM9000) ?
1378 (lsr & UART_LSR_THRE) :
1379 (lsr & UART_LSR_TEMT))
1380 transmit_chars(up);
1381 }
1382 }
1383
1384 /*
1385 * Re-enable the transmitter if we disabled it.
1386 */
1387 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1388 up->acr &= ~UART_ACR_TXDIS;
1389 serial_icr_write(up, UART_ACR, up->acr);
1390 }
1391}
1392
1393static void serial8250_stop_rx(struct uart_port *port)
1394{
1395 struct uart_8250_port *up =
1396 container_of(port, struct uart_8250_port, port);
1397
1398 up->ier &= ~UART_IER_RLSI;
1399 up->port.read_status_mask &= ~UART_LSR_DR;
1400 serial_out(up, UART_IER, up->ier);
1401}
1402
1403static void serial8250_enable_ms(struct uart_port *port)
1404{
1405 struct uart_8250_port *up =
1406 container_of(port, struct uart_8250_port, port);
1407
1408 /* no MSR capabilities */
1409 if (up->bugs & UART_BUG_NOMSR)
1410 return;
1411
1412 up->ier |= UART_IER_MSI;
1413 serial_out(up, UART_IER, up->ier);
1414}
1415
1416static void
1417receive_chars(struct uart_8250_port *up, unsigned int *status)
1418{
1419 struct tty_struct *tty = up->port.state->port.tty;
1420 unsigned char ch, lsr = *status;
1421 int max_count = 256;
1422 char flag;
1423
1424 do {
1425 if (likely(lsr & UART_LSR_DR))
1426 ch = serial_inp(up, UART_RX);
1427 else
1428 /*
1429 * Intel 82571 has a Serial Over Lan device that will
1430 * set UART_LSR_BI without setting UART_LSR_DR when
1431 * it receives a break. To avoid reading from the
1432 * receive buffer without UART_LSR_DR bit set, we
1433 * just force the read character to be 0
1434 */
1435 ch = 0;
1436
1437 flag = TTY_NORMAL;
1438 up->port.icount.rx++;
1439
1440 lsr |= up->lsr_saved_flags;
1441 up->lsr_saved_flags = 0;
1442
1443 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1444 /*
1445 * For statistics only
1446 */
1447 if (lsr & UART_LSR_BI) {
1448 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1449 up->port.icount.brk++;
1450 /*
1451 * We do the SysRQ and SAK checking
1452 * here because otherwise the break
1453 * may get masked by ignore_status_mask
1454 * or read_status_mask.
1455 */
1456 if (uart_handle_break(&up->port))
1457 goto ignore_char;
1458 } else if (lsr & UART_LSR_PE)
1459 up->port.icount.parity++;
1460 else if (lsr & UART_LSR_FE)
1461 up->port.icount.frame++;
1462 if (lsr & UART_LSR_OE)
1463 up->port.icount.overrun++;
1464
1465 /*
1466 * Mask off conditions which should be ignored.
1467 */
1468 lsr &= up->port.read_status_mask;
1469
1470 if (lsr & UART_LSR_BI) {
1471 DEBUG_INTR("handling break....");
1472 flag = TTY_BREAK;
1473 } else if (lsr & UART_LSR_PE)
1474 flag = TTY_PARITY;
1475 else if (lsr & UART_LSR_FE)
1476 flag = TTY_FRAME;
1477 }
1478 if (uart_handle_sysrq_char(&up->port, ch))
1479 goto ignore_char;
1480
1481 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1482
1483ignore_char:
1484 lsr = serial_inp(up, UART_LSR);
1485 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
1486 spin_unlock(&up->port.lock);
1487 tty_flip_buffer_push(tty);
1488 spin_lock(&up->port.lock);
1489 *status = lsr;
1490}
1491
1492static void transmit_chars(struct uart_8250_port *up)
1493{
1494 struct circ_buf *xmit = &up->port.state->xmit;
1495 int count;
1496
1497 if (up->port.x_char) {
1498 serial_outp(up, UART_TX, up->port.x_char);
1499 up->port.icount.tx++;
1500 up->port.x_char = 0;
1501 return;
1502 }
1503 if (uart_tx_stopped(&up->port)) {
1504 serial8250_stop_tx(&up->port);
1505 return;
1506 }
1507 if (uart_circ_empty(xmit)) {
1508 __stop_tx(up);
1509 return;
1510 }
1511
1512 count = up->tx_loadsz;
1513 do {
1514 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1515 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1516 up->port.icount.tx++;
1517 if (uart_circ_empty(xmit))
1518 break;
1519 } while (--count > 0);
1520
1521 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1522 uart_write_wakeup(&up->port);
1523
1524 DEBUG_INTR("THRE...");
1525
1526 if (uart_circ_empty(xmit))
1527 __stop_tx(up);
1528}
1529
1530static unsigned int check_modem_status(struct uart_8250_port *up)
1531{
1532 unsigned int status = serial_in(up, UART_MSR);
1533
1534 status |= up->msr_saved_flags;
1535 up->msr_saved_flags = 0;
1536 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1537 up->port.state != NULL) {
1538 if (status & UART_MSR_TERI)
1539 up->port.icount.rng++;
1540 if (status & UART_MSR_DDSR)
1541 up->port.icount.dsr++;
1542 if (status & UART_MSR_DDCD)
1543 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1544 if (status & UART_MSR_DCTS)
1545 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1546
1547 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
1548 }
1549
1550 return status;
1551}
1552
1553/*
1554 * This handles the interrupt from one port.
1555 */
1556static void serial8250_handle_port(struct uart_8250_port *up)
1557{
1558 unsigned int status;
1559 unsigned long flags;
1560
1561 spin_lock_irqsave(&up->port.lock, flags);
1562
1563 status = serial_inp(up, UART_LSR);
1564
1565 DEBUG_INTR("status = %x...", status);
1566
1567 if (status & (UART_LSR_DR | UART_LSR_BI))
1568 receive_chars(up, &status);
1569 check_modem_status(up);
1570 if (status & UART_LSR_THRE)
1571 transmit_chars(up);
1572
1573 spin_unlock_irqrestore(&up->port.lock, flags);
1574}
1575
1576/*
1577 * This is the serial driver's interrupt routine.
1578 *
1579 * Arjan thinks the old way was overly complex, so it got simplified.
1580 * Alan disagrees, saying that need the complexity to handle the weird
1581 * nature of ISA shared interrupts. (This is a special exception.)
1582 *
1583 * In order to handle ISA shared interrupts properly, we need to check
1584 * that all ports have been serviced, and therefore the ISA interrupt
1585 * line has been de-asserted.
1586 *
1587 * This means we need to loop through all ports. checking that they
1588 * don't have an interrupt pending.
1589 */
1590static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1591{
1592 struct irq_info *i = dev_id;
1593 struct list_head *l, *end = NULL;
1594 int pass_counter = 0, handled = 0;
1595
1596 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1597
1598 spin_lock(&i->lock);
1599
1600 l = i->head;
1601 do {
1602 struct uart_8250_port *up;
1603 unsigned int iir;
1604
1605 up = list_entry(l, struct uart_8250_port, list);
1606
1607 iir = serial_in(up, UART_IIR);
1608 if (!(iir & UART_IIR_NO_INT)) {
1609 serial8250_handle_port(up);
1610
1611 handled = 1;
1612
1613 end = NULL;
1614 } else if ((up->port.iotype == UPIO_DWAPB ||
1615 up->port.iotype == UPIO_DWAPB32) &&
1616 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
1617 /* The DesignWare APB UART has an Busy Detect (0x07)
1618 * interrupt meaning an LCR write attempt occured while the
1619 * UART was busy. The interrupt must be cleared by reading
1620 * the UART status register (USR) and the LCR re-written. */
1621 unsigned int status;
1622 status = *(volatile u32 *)up->port.private_data;
1623 serial_out(up, UART_LCR, up->lcr);
1624
1625 handled = 1;
1626
1627 end = NULL;
1628 } else if (end == NULL)
1629 end = l;
1630
1631 l = l->next;
1632
1633 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1634 /* If we hit this, we're dead. */
1635 printk_ratelimited(KERN_ERR
1636 "serial8250: too much work for irq%d\n", irq);
1637 break;
1638 }
1639 } while (l != end);
1640
1641 spin_unlock(&i->lock);
1642
1643 DEBUG_INTR("end.\n");
1644
1645 return IRQ_RETVAL(handled);
1646}
1647
1648/*
1649 * To support ISA shared interrupts, we need to have one interrupt
1650 * handler that ensures that the IRQ line has been deasserted
1651 * before returning. Failing to do this will result in the IRQ
1652 * line being stuck active, and, since ISA irqs are edge triggered,
1653 * no more IRQs will be seen.
1654 */
1655static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1656{
1657 spin_lock_irq(&i->lock);
1658
1659 if (!list_empty(i->head)) {
1660 if (i->head == &up->list)
1661 i->head = i->head->next;
1662 list_del(&up->list);
1663 } else {
1664 BUG_ON(i->head != &up->list);
1665 i->head = NULL;
1666 }
1667 spin_unlock_irq(&i->lock);
1668 /* List empty so throw away the hash node */
1669 if (i->head == NULL) {
1670 hlist_del(&i->node);
1671 kfree(i);
1672 }
1673}
1674
1675static int serial_link_irq_chain(struct uart_8250_port *up)
1676{
1677 struct hlist_head *h;
1678 struct hlist_node *n;
1679 struct irq_info *i;
1680 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1681
1682 mutex_lock(&hash_mutex);
1683
1684 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1685
1686 hlist_for_each(n, h) {
1687 i = hlist_entry(n, struct irq_info, node);
1688 if (i->irq == up->port.irq)
1689 break;
1690 }
1691
1692 if (n == NULL) {
1693 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1694 if (i == NULL) {
1695 mutex_unlock(&hash_mutex);
1696 return -ENOMEM;
1697 }
1698 spin_lock_init(&i->lock);
1699 i->irq = up->port.irq;
1700 hlist_add_head(&i->node, h);
1701 }
1702 mutex_unlock(&hash_mutex);
1703
1704 spin_lock_irq(&i->lock);
1705
1706 if (i->head) {
1707 list_add(&up->list, i->head);
1708 spin_unlock_irq(&i->lock);
1709
1710 ret = 0;
1711 } else {
1712 INIT_LIST_HEAD(&up->list);
1713 i->head = &up->list;
1714 spin_unlock_irq(&i->lock);
1715 irq_flags |= up->port.irqflags;
1716 ret = request_irq(up->port.irq, serial8250_interrupt,
1717 irq_flags, "serial", i);
1718 if (ret < 0)
1719 serial_do_unlink(i, up);
1720 }
1721
1722 return ret;
1723}
1724
1725static void serial_unlink_irq_chain(struct uart_8250_port *up)
1726{
1727 struct irq_info *i;
1728 struct hlist_node *n;
1729 struct hlist_head *h;
1730
1731 mutex_lock(&hash_mutex);
1732
1733 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1734
1735 hlist_for_each(n, h) {
1736 i = hlist_entry(n, struct irq_info, node);
1737 if (i->irq == up->port.irq)
1738 break;
1739 }
1740
1741 BUG_ON(n == NULL);
1742 BUG_ON(i->head == NULL);
1743
1744 if (list_empty(i->head))
1745 free_irq(up->port.irq, i);
1746
1747 serial_do_unlink(i, up);
1748 mutex_unlock(&hash_mutex);
1749}
1750
1751/*
1752 * This function is used to handle ports that do not have an
1753 * interrupt. This doesn't work very well for 16450's, but gives
1754 * barely passable results for a 16550A. (Although at the expense
1755 * of much CPU overhead).
1756 */
1757static void serial8250_timeout(unsigned long data)
1758{
1759 struct uart_8250_port *up = (struct uart_8250_port *)data;
1760 unsigned int iir;
1761
1762 iir = serial_in(up, UART_IIR);
1763 if (!(iir & UART_IIR_NO_INT))
1764 serial8250_handle_port(up);
1765 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
1766}
1767
1768static void serial8250_backup_timeout(unsigned long data)
1769{
1770 struct uart_8250_port *up = (struct uart_8250_port *)data;
1771 unsigned int iir, ier = 0, lsr;
1772 unsigned long flags;
1773
1774 /*
1775 * Must disable interrupts or else we risk racing with the interrupt
1776 * based handler.
1777 */
1778 if (is_real_interrupt(up->port.irq)) {
1779 ier = serial_in(up, UART_IER);
1780 serial_out(up, UART_IER, 0);
1781 }
1782
1783 iir = serial_in(up, UART_IIR);
1784
1785 /*
1786 * This should be a safe test for anyone who doesn't trust the
1787 * IIR bits on their UART, but it's specifically designed for
1788 * the "Diva" UART used on the management processor on many HP
1789 * ia64 and parisc boxes.
1790 */
1791 spin_lock_irqsave(&up->port.lock, flags);
1792 lsr = serial_in(up, UART_LSR);
1793 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1794 spin_unlock_irqrestore(&up->port.lock, flags);
1795 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1796 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
1797 (lsr & UART_LSR_THRE)) {
1798 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1799 iir |= UART_IIR_THRI;
1800 }
1801
1802 if (!(iir & UART_IIR_NO_INT))
1803 serial8250_handle_port(up);
1804
1805 if (is_real_interrupt(up->port.irq))
1806 serial_out(up, UART_IER, ier);
1807
1808 /* Standard timer interval plus 0.2s to keep the port running */
1809 mod_timer(&up->timer,
1810 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
1811}
1812
1813static unsigned int serial8250_tx_empty(struct uart_port *port)
1814{
1815 struct uart_8250_port *up =
1816 container_of(port, struct uart_8250_port, port);
1817 unsigned long flags;
1818 unsigned int lsr;
1819
1820 spin_lock_irqsave(&up->port.lock, flags);
1821 lsr = serial_in(up, UART_LSR);
1822 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1823 spin_unlock_irqrestore(&up->port.lock, flags);
1824
1825 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1826}
1827
1828static unsigned int serial8250_get_mctrl(struct uart_port *port)
1829{
1830 struct uart_8250_port *up =
1831 container_of(port, struct uart_8250_port, port);
1832 unsigned int status;
1833 unsigned int ret;
1834
1835 status = check_modem_status(up);
1836
1837 ret = 0;
1838 if (status & UART_MSR_DCD)
1839 ret |= TIOCM_CAR;
1840 if (status & UART_MSR_RI)
1841 ret |= TIOCM_RNG;
1842 if (status & UART_MSR_DSR)
1843 ret |= TIOCM_DSR;
1844 if (status & UART_MSR_CTS)
1845 ret |= TIOCM_CTS;
1846 return ret;
1847}
1848
1849static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1850{
1851 struct uart_8250_port *up =
1852 container_of(port, struct uart_8250_port, port);
1853 unsigned char mcr = 0;
1854
1855 if (mctrl & TIOCM_RTS)
1856 mcr |= UART_MCR_RTS;
1857 if (mctrl & TIOCM_DTR)
1858 mcr |= UART_MCR_DTR;
1859 if (mctrl & TIOCM_OUT1)
1860 mcr |= UART_MCR_OUT1;
1861 if (mctrl & TIOCM_OUT2)
1862 mcr |= UART_MCR_OUT2;
1863 if (mctrl & TIOCM_LOOP)
1864 mcr |= UART_MCR_LOOP;
1865
1866 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1867
1868 serial_out(up, UART_MCR, mcr);
1869}
1870
1871static void serial8250_break_ctl(struct uart_port *port, int break_state)
1872{
1873 struct uart_8250_port *up =
1874 container_of(port, struct uart_8250_port, port);
1875 unsigned long flags;
1876
1877 spin_lock_irqsave(&up->port.lock, flags);
1878 if (break_state == -1)
1879 up->lcr |= UART_LCR_SBC;
1880 else
1881 up->lcr &= ~UART_LCR_SBC;
1882 serial_out(up, UART_LCR, up->lcr);
1883 spin_unlock_irqrestore(&up->port.lock, flags);
1884}
1885
1886/*
1887 * Wait for transmitter & holding register to empty
1888 */
1889static void wait_for_xmitr(struct uart_8250_port *up, int bits)
1890{
1891 unsigned int status, tmout = 10000;
1892
1893 /* Wait up to 10ms for the character(s) to be sent. */
1894 for (;;) {
1895 status = serial_in(up, UART_LSR);
1896
1897 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
1898
1899 if ((status & bits) == bits)
1900 break;
1901 if (--tmout == 0)
1902 break;
1903 udelay(1);
1904 }
1905
1906 /* Wait up to 1s for flow control if necessary */
1907 if (up->port.flags & UPF_CONS_FLOW) {
1908 unsigned int tmout;
1909 for (tmout = 1000000; tmout; tmout--) {
1910 unsigned int msr = serial_in(up, UART_MSR);
1911 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1912 if (msr & UART_MSR_CTS)
1913 break;
1914 udelay(1);
1915 touch_nmi_watchdog();
1916 }
1917 }
1918}
1919
1920#ifdef CONFIG_CONSOLE_POLL
1921/*
1922 * Console polling routines for writing and reading from the uart while
1923 * in an interrupt or debug context.
1924 */
1925
1926static int serial8250_get_poll_char(struct uart_port *port)
1927{
1928 struct uart_8250_port *up =
1929 container_of(port, struct uart_8250_port, port);
1930 unsigned char lsr = serial_inp(up, UART_LSR);
1931
1932 if (!(lsr & UART_LSR_DR))
1933 return NO_POLL_CHAR;
1934
1935 return serial_inp(up, UART_RX);
1936}
1937
1938
1939static void serial8250_put_poll_char(struct uart_port *port,
1940 unsigned char c)
1941{
1942 unsigned int ier;
1943 struct uart_8250_port *up =
1944 container_of(port, struct uart_8250_port, port);
1945
1946 /*
1947 * First save the IER then disable the interrupts
1948 */
1949 ier = serial_in(up, UART_IER);
1950 if (up->capabilities & UART_CAP_UUE)
1951 serial_out(up, UART_IER, UART_IER_UUE);
1952 else
1953 serial_out(up, UART_IER, 0);
1954
1955 wait_for_xmitr(up, BOTH_EMPTY);
1956 /*
1957 * Send the character out.
1958 * If a LF, also do CR...
1959 */
1960 serial_out(up, UART_TX, c);
1961 if (c == 10) {
1962 wait_for_xmitr(up, BOTH_EMPTY);
1963 serial_out(up, UART_TX, 13);
1964 }
1965
1966 /*
1967 * Finally, wait for transmitter to become empty
1968 * and restore the IER
1969 */
1970 wait_for_xmitr(up, BOTH_EMPTY);
1971 serial_out(up, UART_IER, ier);
1972}
1973
1974#endif /* CONFIG_CONSOLE_POLL */
1975
1976static int serial8250_startup(struct uart_port *port)
1977{
1978 struct uart_8250_port *up =
1979 container_of(port, struct uart_8250_port, port);
1980 unsigned long flags;
1981 unsigned char lsr, iir;
1982 int retval;
1983
1984 up->port.fifosize = uart_config[up->port.type].fifo_size;
1985 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1986 up->capabilities = uart_config[up->port.type].flags;
1987 up->mcr = 0;
1988
1989 if (up->port.iotype != up->cur_iotype)
1990 set_io_from_upio(port);
1991
1992 if (up->port.type == PORT_16C950) {
1993 /* Wake up and initialize UART */
1994 up->acr = 0;
1995 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
1996 serial_outp(up, UART_EFR, UART_EFR_ECB);
1997 serial_outp(up, UART_IER, 0);
1998 serial_outp(up, UART_LCR, 0);
1999 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2000 serial_outp(up, UART_LCR, 0xBF);
2001 serial_outp(up, UART_EFR, UART_EFR_ECB);
2002 serial_outp(up, UART_LCR, 0);
2003 }
2004
2005#ifdef CONFIG_SERIAL_8250_RSA
2006 /*
2007 * If this is an RSA port, see if we can kick it up to the
2008 * higher speed clock.
2009 */
2010 enable_rsa(up);
2011#endif
2012
2013 /*
2014 * Clear the FIFO buffers and disable them.
2015 * (they will be reenabled in set_termios())
2016 */
2017 serial8250_clear_fifos(up);
2018
2019 /*
2020 * Clear the interrupt registers.
2021 */
2022 (void) serial_inp(up, UART_LSR);
2023 (void) serial_inp(up, UART_RX);
2024 (void) serial_inp(up, UART_IIR);
2025 (void) serial_inp(up, UART_MSR);
2026
2027 /*
2028 * At this point, there's no way the LSR could still be 0xff;
2029 * if it is, then bail out, because there's likely no UART
2030 * here.
2031 */
2032 if (!(up->port.flags & UPF_BUGGY_UART) &&
2033 (serial_inp(up, UART_LSR) == 0xff)) {
2034 printk(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2035 serial_index(&up->port));
2036 return -ENODEV;
2037 }
2038
2039 /*
2040 * For a XR16C850, we need to set the trigger levels
2041 */
2042 if (up->port.type == PORT_16850) {
2043 unsigned char fctr;
2044
2045 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
2046
2047 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2048 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2049 serial_outp(up, UART_TRG, UART_TRG_96);
2050 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2051 serial_outp(up, UART_TRG, UART_TRG_96);
2052
2053 serial_outp(up, UART_LCR, 0);
2054 }
2055
2056 if (is_real_interrupt(up->port.irq)) {
2057 unsigned char iir1;
2058 /*
2059 * Test for UARTs that do not reassert THRE when the
2060 * transmitter is idle and the interrupt has already
2061 * been cleared. Real 16550s should always reassert
2062 * this interrupt whenever the transmitter is idle and
2063 * the interrupt is enabled. Delays are necessary to
2064 * allow register changes to become visible.
2065 */
2066 spin_lock_irqsave(&up->port.lock, flags);
2067 if (up->port.irqflags & IRQF_SHARED)
2068 disable_irq_nosync(up->port.irq);
2069
2070 wait_for_xmitr(up, UART_LSR_THRE);
2071 serial_out_sync(up, UART_IER, UART_IER_THRI);
2072 udelay(1); /* allow THRE to set */
2073 iir1 = serial_in(up, UART_IIR);
2074 serial_out(up, UART_IER, 0);
2075 serial_out_sync(up, UART_IER, UART_IER_THRI);
2076 udelay(1); /* allow a working UART time to re-assert THRE */
2077 iir = serial_in(up, UART_IIR);
2078 serial_out(up, UART_IER, 0);
2079
2080 if (up->port.irqflags & IRQF_SHARED)
2081 enable_irq(up->port.irq);
2082 spin_unlock_irqrestore(&up->port.lock, flags);
2083
2084 /*
2085 * If the interrupt is not reasserted, setup a timer to
2086 * kick the UART on a regular basis.
2087 */
2088 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
2089 up->bugs |= UART_BUG_THRE;
2090 pr_debug("ttyS%d - using backup timer\n",
2091 serial_index(port));
2092 }
2093 }
2094
2095 /*
2096 * The above check will only give an accurate result the first time
2097 * the port is opened so this value needs to be preserved.
2098 */
2099 if (up->bugs & UART_BUG_THRE) {
2100 up->timer.function = serial8250_backup_timeout;
2101 up->timer.data = (unsigned long)up;
2102 mod_timer(&up->timer, jiffies +
2103 uart_poll_timeout(port) + HZ / 5);
2104 }
2105
2106 /*
2107 * If the "interrupt" for this port doesn't correspond with any
2108 * hardware interrupt, we use a timer-based system. The original
2109 * driver used to do this with IRQ0.
2110 */
2111 if (!is_real_interrupt(up->port.irq)) {
2112 up->timer.data = (unsigned long)up;
2113 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
2114 } else {
2115 retval = serial_link_irq_chain(up);
2116 if (retval)
2117 return retval;
2118 }
2119
2120 /*
2121 * Now, initialize the UART
2122 */
2123 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2124
2125 spin_lock_irqsave(&up->port.lock, flags);
2126 if (up->port.flags & UPF_FOURPORT) {
2127 if (!is_real_interrupt(up->port.irq))
2128 up->port.mctrl |= TIOCM_OUT1;
2129 } else
2130 /*
2131 * Most PC uarts need OUT2 raised to enable interrupts.
2132 */
2133 if (is_real_interrupt(up->port.irq))
2134 up->port.mctrl |= TIOCM_OUT2;
2135
2136 serial8250_set_mctrl(&up->port, up->port.mctrl);
2137
2138 /* Serial over Lan (SoL) hack:
2139 Intel 8257x Gigabit ethernet chips have a
2140 16550 emulation, to be used for Serial Over Lan.
2141 Those chips take a longer time than a normal
2142 serial device to signalize that a transmission
2143 data was queued. Due to that, the above test generally
2144 fails. One solution would be to delay the reading of
2145 iir. However, this is not reliable, since the timeout
2146 is variable. So, let's just don't test if we receive
2147 TX irq. This way, we'll never enable UART_BUG_TXEN.
2148 */
2149 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
2150 goto dont_test_tx_en;
2151
2152 /*
2153 * Do a quick test to see if we receive an
2154 * interrupt when we enable the TX irq.
2155 */
2156 serial_outp(up, UART_IER, UART_IER_THRI);
2157 lsr = serial_in(up, UART_LSR);
2158 iir = serial_in(up, UART_IIR);
2159 serial_outp(up, UART_IER, 0);
2160
2161 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2162 if (!(up->bugs & UART_BUG_TXEN)) {
2163 up->bugs |= UART_BUG_TXEN;
2164 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2165 serial_index(port));
2166 }
2167 } else {
2168 up->bugs &= ~UART_BUG_TXEN;
2169 }
2170
2171dont_test_tx_en:
2172 spin_unlock_irqrestore(&up->port.lock, flags);
2173
2174 /*
2175 * Clear the interrupt registers again for luck, and clear the
2176 * saved flags to avoid getting false values from polling
2177 * routines or the previous session.
2178 */
2179 serial_inp(up, UART_LSR);
2180 serial_inp(up, UART_RX);
2181 serial_inp(up, UART_IIR);
2182 serial_inp(up, UART_MSR);
2183 up->lsr_saved_flags = 0;
2184 up->msr_saved_flags = 0;
2185
2186 /*
2187 * Finally, enable interrupts. Note: Modem status interrupts
2188 * are set via set_termios(), which will be occurring imminently
2189 * anyway, so we don't enable them here.
2190 */
2191 up->ier = UART_IER_RLSI | UART_IER_RDI;
2192 serial_outp(up, UART_IER, up->ier);
2193
2194 if (up->port.flags & UPF_FOURPORT) {
2195 unsigned int icp;
2196 /*
2197 * Enable interrupts on the AST Fourport board
2198 */
2199 icp = (up->port.iobase & 0xfe0) | 0x01f;
2200 outb_p(0x80, icp);
2201 (void) inb_p(icp);
2202 }
2203
2204 return 0;
2205}
2206
2207static void serial8250_shutdown(struct uart_port *port)
2208{
2209 struct uart_8250_port *up =
2210 container_of(port, struct uart_8250_port, port);
2211 unsigned long flags;
2212
2213 /*
2214 * Disable interrupts from this port
2215 */
2216 up->ier = 0;
2217 serial_outp(up, UART_IER, 0);
2218
2219 spin_lock_irqsave(&up->port.lock, flags);
2220 if (up->port.flags & UPF_FOURPORT) {
2221 /* reset interrupts on the AST Fourport board */
2222 inb((up->port.iobase & 0xfe0) | 0x1f);
2223 up->port.mctrl |= TIOCM_OUT1;
2224 } else
2225 up->port.mctrl &= ~TIOCM_OUT2;
2226
2227 serial8250_set_mctrl(&up->port, up->port.mctrl);
2228 spin_unlock_irqrestore(&up->port.lock, flags);
2229
2230 /*
2231 * Disable break condition and FIFOs
2232 */
2233 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2234 serial8250_clear_fifos(up);
2235
2236#ifdef CONFIG_SERIAL_8250_RSA
2237 /*
2238 * Reset the RSA board back to 115kbps compat mode.
2239 */
2240 disable_rsa(up);
2241#endif
2242
2243 /*
2244 * Read data port to reset things, and then unlink from
2245 * the IRQ chain.
2246 */
2247 (void) serial_in(up, UART_RX);
2248
2249 del_timer_sync(&up->timer);
2250 up->timer.function = serial8250_timeout;
2251 if (is_real_interrupt(up->port.irq))
2252 serial_unlink_irq_chain(up);
2253}
2254
2255static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2256{
2257 unsigned int quot;
2258
2259 /*
2260 * Handle magic divisors for baud rates above baud_base on
2261 * SMSC SuperIO chips.
2262 */
2263 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2264 baud == (port->uartclk/4))
2265 quot = 0x8001;
2266 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2267 baud == (port->uartclk/8))
2268 quot = 0x8002;
2269 else
2270 quot = uart_get_divisor(port, baud);
2271
2272 return quot;
2273}
2274
2275void
2276serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2277 struct ktermios *old)
2278{
2279 struct uart_8250_port *up =
2280 container_of(port, struct uart_8250_port, port);
2281 unsigned char cval, fcr = 0;
2282 unsigned long flags;
2283 unsigned int baud, quot;
2284
2285 switch (termios->c_cflag & CSIZE) {
2286 case CS5:
2287 cval = UART_LCR_WLEN5;
2288 break;
2289 case CS6:
2290 cval = UART_LCR_WLEN6;
2291 break;
2292 case CS7:
2293 cval = UART_LCR_WLEN7;
2294 break;
2295 default:
2296 case CS8:
2297 cval = UART_LCR_WLEN8;
2298 break;
2299 }
2300
2301 if (termios->c_cflag & CSTOPB)
2302 cval |= UART_LCR_STOP;
2303 if (termios->c_cflag & PARENB)
2304 cval |= UART_LCR_PARITY;
2305 if (!(termios->c_cflag & PARODD))
2306 cval |= UART_LCR_EPAR;
2307#ifdef CMSPAR
2308 if (termios->c_cflag & CMSPAR)
2309 cval |= UART_LCR_SPAR;
2310#endif
2311
2312 /*
2313 * Ask the core to calculate the divisor for us.
2314 */
2315 baud = uart_get_baud_rate(port, termios, old,
2316 port->uartclk / 16 / 0xffff,
2317 port->uartclk / 16);
2318 quot = serial8250_get_divisor(port, baud);
2319
2320 /*
2321 * Oxford Semi 952 rev B workaround
2322 */
2323 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2324 quot++;
2325
2326 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2327 if (baud < 2400)
2328 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2329 else
2330 fcr = uart_config[up->port.type].fcr;
2331 }
2332
2333 /*
2334 * MCR-based auto flow control. When AFE is enabled, RTS will be
2335 * deasserted when the receive FIFO contains more characters than
2336 * the trigger, or the MCR RTS bit is cleared. In the case where
2337 * the remote UART is not using CTS auto flow control, we must
2338 * have sufficient FIFO entries for the latency of the remote
2339 * UART to respond. IOW, at least 32 bytes of FIFO.
2340 */
2341 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2342 up->mcr &= ~UART_MCR_AFE;
2343 if (termios->c_cflag & CRTSCTS)
2344 up->mcr |= UART_MCR_AFE;
2345 }
2346
2347 /*
2348 * Ok, we're now changing the port state. Do it with
2349 * interrupts disabled.
2350 */
2351 spin_lock_irqsave(&up->port.lock, flags);
2352
2353 /*
2354 * Update the per-port timeout.
2355 */
2356 uart_update_timeout(port, termios->c_cflag, baud);
2357
2358 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2359 if (termios->c_iflag & INPCK)
2360 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2361 if (termios->c_iflag & (BRKINT | PARMRK))
2362 up->port.read_status_mask |= UART_LSR_BI;
2363
2364 /*
2365 * Characteres to ignore
2366 */
2367 up->port.ignore_status_mask = 0;
2368 if (termios->c_iflag & IGNPAR)
2369 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2370 if (termios->c_iflag & IGNBRK) {
2371 up->port.ignore_status_mask |= UART_LSR_BI;
2372 /*
2373 * If we're ignoring parity and break indicators,
2374 * ignore overruns too (for real raw support).
2375 */
2376 if (termios->c_iflag & IGNPAR)
2377 up->port.ignore_status_mask |= UART_LSR_OE;
2378 }
2379
2380 /*
2381 * ignore all characters if CREAD is not set
2382 */
2383 if ((termios->c_cflag & CREAD) == 0)
2384 up->port.ignore_status_mask |= UART_LSR_DR;
2385
2386 /*
2387 * CTS flow control flag and modem status interrupts
2388 */
2389 up->ier &= ~UART_IER_MSI;
2390 if (!(up->bugs & UART_BUG_NOMSR) &&
2391 UART_ENABLE_MS(&up->port, termios->c_cflag))
2392 up->ier |= UART_IER_MSI;
2393 if (up->capabilities & UART_CAP_UUE)
2394 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
2395
2396 serial_out(up, UART_IER, up->ier);
2397
2398 if (up->capabilities & UART_CAP_EFR) {
2399 unsigned char efr = 0;
2400 /*
2401 * TI16C752/Startech hardware flow control. FIXME:
2402 * - TI16C752 requires control thresholds to be set.
2403 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2404 */
2405 if (termios->c_cflag & CRTSCTS)
2406 efr |= UART_EFR_CTS;
2407
2408 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
2409 serial_outp(up, UART_EFR, efr);
2410 }
2411
2412#ifdef CONFIG_ARCH_OMAP
2413 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2414 if (cpu_is_omap1510() && is_omap_port(up)) {
2415 if (baud == 115200) {
2416 quot = 1;
2417 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2418 } else
2419 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2420 }
2421#endif
2422
2423 if (up->capabilities & UART_NATSEMI) {
2424 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2425 serial_outp(up, UART_LCR, 0xe0);
2426 } else {
2427 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2428 }
2429
2430 serial_dl_write(up, quot);
2431
2432 /*
2433 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2434 * is written without DLAB set, this mode will be disabled.
2435 */
2436 if (up->port.type == PORT_16750)
2437 serial_outp(up, UART_FCR, fcr);
2438
2439 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2440 up->lcr = cval; /* Save LCR */
2441 if (up->port.type != PORT_16750) {
2442 if (fcr & UART_FCR_ENABLE_FIFO) {
2443 /* emulated UARTs (Lucent Venus 167x) need two steps */
2444 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2445 }
2446 serial_outp(up, UART_FCR, fcr); /* set fcr */
2447 }
2448 serial8250_set_mctrl(&up->port, up->port.mctrl);
2449 spin_unlock_irqrestore(&up->port.lock, flags);
2450 /* Don't rewrite B0 */
2451 if (tty_termios_baud_rate(termios))
2452 tty_termios_encode_baud_rate(termios, baud, baud);
2453}
2454EXPORT_SYMBOL(serial8250_do_set_termios);
2455
2456static void
2457serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2458 struct ktermios *old)
2459{
2460 if (port->set_termios)
2461 port->set_termios(port, termios, old);
2462 else
2463 serial8250_do_set_termios(port, termios, old);
2464}
2465
2466static void
2467serial8250_set_ldisc(struct uart_port *port, int new)
2468{
2469 if (new == N_PPS) {
2470 port->flags |= UPF_HARDPPS_CD;
2471 serial8250_enable_ms(port);
2472 } else
2473 port->flags &= ~UPF_HARDPPS_CD;
2474}
2475
2476
2477void serial8250_do_pm(struct uart_port *port, unsigned int state,
2478 unsigned int oldstate)
2479{
2480 struct uart_8250_port *p =
2481 container_of(port, struct uart_8250_port, port);
2482
2483 serial8250_set_sleep(p, state != 0);
2484}
2485EXPORT_SYMBOL(serial8250_do_pm);
2486
2487static void
2488serial8250_pm(struct uart_port *port, unsigned int state,
2489 unsigned int oldstate)
2490{
2491 if (port->pm)
2492 port->pm(port, state, oldstate);
2493 else
2494 serial8250_do_pm(port, state, oldstate);
2495}
2496
2497static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2498{
2499 if (pt->port.iotype == UPIO_AU)
2500 return 0x1000;
2501#ifdef CONFIG_ARCH_OMAP
2502 if (is_omap_port(pt))
2503 return 0x16 << pt->port.regshift;
2504#endif
2505 return 8 << pt->port.regshift;
2506}
2507
2508/*
2509 * Resource handling.
2510 */
2511static int serial8250_request_std_resource(struct uart_8250_port *up)
2512{
2513 unsigned int size = serial8250_port_size(up);
2514 int ret = 0;
2515
2516 switch (up->port.iotype) {
2517 case UPIO_AU:
2518 case UPIO_TSI:
2519 case UPIO_MEM32:
2520 case UPIO_MEM:
2521 case UPIO_DWAPB:
2522 case UPIO_DWAPB32:
2523 if (!up->port.mapbase)
2524 break;
2525
2526 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2527 ret = -EBUSY;
2528 break;
2529 }
2530
2531 if (up->port.flags & UPF_IOREMAP) {
2532 up->port.membase = ioremap_nocache(up->port.mapbase,
2533 size);
2534 if (!up->port.membase) {
2535 release_mem_region(up->port.mapbase, size);
2536 ret = -ENOMEM;
2537 }
2538 }
2539 break;
2540
2541 case UPIO_HUB6:
2542 case UPIO_PORT:
2543 if (!request_region(up->port.iobase, size, "serial"))
2544 ret = -EBUSY;
2545 break;
2546 }
2547 return ret;
2548}
2549
2550static void serial8250_release_std_resource(struct uart_8250_port *up)
2551{
2552 unsigned int size = serial8250_port_size(up);
2553
2554 switch (up->port.iotype) {
2555 case UPIO_AU:
2556 case UPIO_TSI:
2557 case UPIO_MEM32:
2558 case UPIO_MEM:
2559 case UPIO_DWAPB:
2560 case UPIO_DWAPB32:
2561 if (!up->port.mapbase)
2562 break;
2563
2564 if (up->port.flags & UPF_IOREMAP) {
2565 iounmap(up->port.membase);
2566 up->port.membase = NULL;
2567 }
2568
2569 release_mem_region(up->port.mapbase, size);
2570 break;
2571
2572 case UPIO_HUB6:
2573 case UPIO_PORT:
2574 release_region(up->port.iobase, size);
2575 break;
2576 }
2577}
2578
2579static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2580{
2581 unsigned long start = UART_RSA_BASE << up->port.regshift;
2582 unsigned int size = 8 << up->port.regshift;
2583 int ret = -EINVAL;
2584
2585 switch (up->port.iotype) {
2586 case UPIO_HUB6:
2587 case UPIO_PORT:
2588 start += up->port.iobase;
2589 if (request_region(start, size, "serial-rsa"))
2590 ret = 0;
2591 else
2592 ret = -EBUSY;
2593 break;
2594 }
2595
2596 return ret;
2597}
2598
2599static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2600{
2601 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2602 unsigned int size = 8 << up->port.regshift;
2603
2604 switch (up->port.iotype) {
2605 case UPIO_HUB6:
2606 case UPIO_PORT:
2607 release_region(up->port.iobase + offset, size);
2608 break;
2609 }
2610}
2611
2612static void serial8250_release_port(struct uart_port *port)
2613{
2614 struct uart_8250_port *up =
2615 container_of(port, struct uart_8250_port, port);
2616
2617 serial8250_release_std_resource(up);
2618 if (up->port.type == PORT_RSA)
2619 serial8250_release_rsa_resource(up);
2620}
2621
2622static int serial8250_request_port(struct uart_port *port)
2623{
2624 struct uart_8250_port *up =
2625 container_of(port, struct uart_8250_port, port);
2626 int ret = 0;
2627
2628 ret = serial8250_request_std_resource(up);
2629 if (ret == 0 && up->port.type == PORT_RSA) {
2630 ret = serial8250_request_rsa_resource(up);
2631 if (ret < 0)
2632 serial8250_release_std_resource(up);
2633 }
2634
2635 return ret;
2636}
2637
2638static void serial8250_config_port(struct uart_port *port, int flags)
2639{
2640 struct uart_8250_port *up =
2641 container_of(port, struct uart_8250_port, port);
2642 int probeflags = PROBE_ANY;
2643 int ret;
2644
2645 /*
2646 * Find the region that we can probe for. This in turn
2647 * tells us whether we can probe for the type of port.
2648 */
2649 ret = serial8250_request_std_resource(up);
2650 if (ret < 0)
2651 return;
2652
2653 ret = serial8250_request_rsa_resource(up);
2654 if (ret < 0)
2655 probeflags &= ~PROBE_RSA;
2656
2657 if (up->port.iotype != up->cur_iotype)
2658 set_io_from_upio(port);
2659
2660 if (flags & UART_CONFIG_TYPE)
2661 autoconfig(up, probeflags);
2662
2663 /* if access method is AU, it is a 16550 with a quirk */
2664 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
2665 up->bugs |= UART_BUG_NOMSR;
2666
2667 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2668 autoconfig_irq(up);
2669
2670 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2671 serial8250_release_rsa_resource(up);
2672 if (up->port.type == PORT_UNKNOWN)
2673 serial8250_release_std_resource(up);
2674}
2675
2676static int
2677serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2678{
2679 if (ser->irq >= nr_irqs || ser->irq < 0 ||
2680 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2681 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2682 ser->type == PORT_STARTECH)
2683 return -EINVAL;
2684 return 0;
2685}
2686
2687static const char *
2688serial8250_type(struct uart_port *port)
2689{
2690 int type = port->type;
2691
2692 if (type >= ARRAY_SIZE(uart_config))
2693 type = 0;
2694 return uart_config[type].name;
2695}
2696
2697static struct uart_ops serial8250_pops = {
2698 .tx_empty = serial8250_tx_empty,
2699 .set_mctrl = serial8250_set_mctrl,
2700 .get_mctrl = serial8250_get_mctrl,
2701 .stop_tx = serial8250_stop_tx,
2702 .start_tx = serial8250_start_tx,
2703 .stop_rx = serial8250_stop_rx,
2704 .enable_ms = serial8250_enable_ms,
2705 .break_ctl = serial8250_break_ctl,
2706 .startup = serial8250_startup,
2707 .shutdown = serial8250_shutdown,
2708 .set_termios = serial8250_set_termios,
2709 .set_ldisc = serial8250_set_ldisc,
2710 .pm = serial8250_pm,
2711 .type = serial8250_type,
2712 .release_port = serial8250_release_port,
2713 .request_port = serial8250_request_port,
2714 .config_port = serial8250_config_port,
2715 .verify_port = serial8250_verify_port,
2716#ifdef CONFIG_CONSOLE_POLL
2717 .poll_get_char = serial8250_get_poll_char,
2718 .poll_put_char = serial8250_put_poll_char,
2719#endif
2720};
2721
2722static struct uart_8250_port serial8250_ports[UART_NR];
2723
2724static void (*serial8250_isa_config)(int port, struct uart_port *up,
2725 unsigned short *capabilities);
2726
2727void serial8250_set_isa_configurator(
2728 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
2729{
2730 serial8250_isa_config = v;
2731}
2732EXPORT_SYMBOL(serial8250_set_isa_configurator);
2733
2734static void __init serial8250_isa_init_ports(void)
2735{
2736 struct uart_8250_port *up;
2737 static int first = 1;
2738 int i, irqflag = 0;
2739
2740 if (!first)
2741 return;
2742 first = 0;
2743
2744 for (i = 0; i < nr_uarts; i++) {
2745 struct uart_8250_port *up = &serial8250_ports[i];
2746
2747 up->port.line = i;
2748 spin_lock_init(&up->port.lock);
2749
2750 init_timer(&up->timer);
2751 up->timer.function = serial8250_timeout;
2752
2753 /*
2754 * ALPHA_KLUDGE_MCR needs to be killed.
2755 */
2756 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2757 up->mcr_force = ALPHA_KLUDGE_MCR;
2758
2759 up->port.ops = &serial8250_pops;
2760 }
2761
2762 if (share_irqs)
2763 irqflag = IRQF_SHARED;
2764
2765 for (i = 0, up = serial8250_ports;
2766 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2767 i++, up++) {
2768 up->port.iobase = old_serial_port[i].port;
2769 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2770 up->port.irqflags = old_serial_port[i].irqflags;
2771 up->port.uartclk = old_serial_port[i].baud_base * 16;
2772 up->port.flags = old_serial_port[i].flags;
2773 up->port.hub6 = old_serial_port[i].hub6;
2774 up->port.membase = old_serial_port[i].iomem_base;
2775 up->port.iotype = old_serial_port[i].io_type;
2776 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2777 set_io_from_upio(&up->port);
2778 up->port.irqflags |= irqflag;
2779 if (serial8250_isa_config != NULL)
2780 serial8250_isa_config(i, &up->port, &up->capabilities);
2781
2782 }
2783}
2784
2785static void
2786serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2787{
2788 up->port.type = type;
2789 up->port.fifosize = uart_config[type].fifo_size;
2790 up->capabilities = uart_config[type].flags;
2791 up->tx_loadsz = uart_config[type].tx_loadsz;
2792}
2793
2794static void __init
2795serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2796{
2797 int i;
2798
2799 for (i = 0; i < nr_uarts; i++) {
2800 struct uart_8250_port *up = &serial8250_ports[i];
2801 up->cur_iotype = 0xFF;
2802 }
2803
2804 serial8250_isa_init_ports();
2805
2806 for (i = 0; i < nr_uarts; i++) {
2807 struct uart_8250_port *up = &serial8250_ports[i];
2808
2809 up->port.dev = dev;
2810
2811 if (up->port.flags & UPF_FIXED_TYPE)
2812 serial8250_init_fixed_type_port(up, up->port.type);
2813
2814 uart_add_one_port(drv, &up->port);
2815 }
2816}
2817
2818#ifdef CONFIG_SERIAL_8250_CONSOLE
2819
2820static void serial8250_console_putchar(struct uart_port *port, int ch)
2821{
2822 struct uart_8250_port *up =
2823 container_of(port, struct uart_8250_port, port);
2824
2825 wait_for_xmitr(up, UART_LSR_THRE);
2826 serial_out(up, UART_TX, ch);
2827}
2828
2829/*
2830 * Print a string to the serial port trying not to disturb
2831 * any possible real use of the port...
2832 *
2833 * The console_lock must be held when we get here.
2834 */
2835static void
2836serial8250_console_write(struct console *co, const char *s, unsigned int count)
2837{
2838 struct uart_8250_port *up = &serial8250_ports[co->index];
2839 unsigned long flags;
2840 unsigned int ier;
2841 int locked = 1;
2842
2843 touch_nmi_watchdog();
2844
2845 local_irq_save(flags);
2846 if (up->port.sysrq) {
2847 /* serial8250_handle_port() already took the lock */
2848 locked = 0;
2849 } else if (oops_in_progress) {
2850 locked = spin_trylock(&up->port.lock);
2851 } else
2852 spin_lock(&up->port.lock);
2853
2854 /*
2855 * First save the IER then disable the interrupts
2856 */
2857 ier = serial_in(up, UART_IER);
2858
2859 if (up->capabilities & UART_CAP_UUE)
2860 serial_out(up, UART_IER, UART_IER_UUE);
2861 else
2862 serial_out(up, UART_IER, 0);
2863
2864 uart_console_write(&up->port, s, count, serial8250_console_putchar);
2865
2866 /*
2867 * Finally, wait for transmitter to become empty
2868 * and restore the IER
2869 */
2870 wait_for_xmitr(up, BOTH_EMPTY);
2871 serial_out(up, UART_IER, ier);
2872
2873 /*
2874 * The receive handling will happen properly because the
2875 * receive ready bit will still be set; it is not cleared
2876 * on read. However, modem control will not, we must
2877 * call it if we have saved something in the saved flags
2878 * while processing with interrupts off.
2879 */
2880 if (up->msr_saved_flags)
2881 check_modem_status(up);
2882
2883 if (locked)
2884 spin_unlock(&up->port.lock);
2885 local_irq_restore(flags);
2886}
2887
2888static int __init serial8250_console_setup(struct console *co, char *options)
2889{
2890 struct uart_port *port;
2891 int baud = 9600;
2892 int bits = 8;
2893 int parity = 'n';
2894 int flow = 'n';
2895
2896 /*
2897 * Check whether an invalid uart number has been specified, and
2898 * if so, search for the first available port that does have
2899 * console support.
2900 */
2901 if (co->index >= nr_uarts)
2902 co->index = 0;
2903 port = &serial8250_ports[co->index].port;
2904 if (!port->iobase && !port->membase)
2905 return -ENODEV;
2906
2907 if (options)
2908 uart_parse_options(options, &baud, &parity, &bits, &flow);
2909
2910 return uart_set_options(port, co, baud, parity, bits, flow);
2911}
2912
2913static int serial8250_console_early_setup(void)
2914{
2915 return serial8250_find_port_for_earlycon();
2916}
2917
2918static struct console serial8250_console = {
2919 .name = "ttyS",
2920 .write = serial8250_console_write,
2921 .device = uart_console_device,
2922 .setup = serial8250_console_setup,
2923 .early_setup = serial8250_console_early_setup,
2924 .flags = CON_PRINTBUFFER | CON_ANYTIME,
2925 .index = -1,
2926 .data = &serial8250_reg,
2927};
2928
2929static int __init serial8250_console_init(void)
2930{
2931 if (nr_uarts > UART_NR)
2932 nr_uarts = UART_NR;
2933
2934 serial8250_isa_init_ports();
2935 register_console(&serial8250_console);
2936 return 0;
2937}
2938console_initcall(serial8250_console_init);
2939
2940int serial8250_find_port(struct uart_port *p)
2941{
2942 int line;
2943 struct uart_port *port;
2944
2945 for (line = 0; line < nr_uarts; line++) {
2946 port = &serial8250_ports[line].port;
2947 if (uart_match_port(p, port))
2948 return line;
2949 }
2950 return -ENODEV;
2951}
2952
2953#define SERIAL8250_CONSOLE &serial8250_console
2954#else
2955#define SERIAL8250_CONSOLE NULL
2956#endif
2957
2958static struct uart_driver serial8250_reg = {
2959 .owner = THIS_MODULE,
2960 .driver_name = "serial",
2961 .dev_name = "ttyS",
2962 .major = TTY_MAJOR,
2963 .minor = 64,
2964 .cons = SERIAL8250_CONSOLE,
2965};
2966
2967/*
2968 * early_serial_setup - early registration for 8250 ports
2969 *
2970 * Setup an 8250 port structure prior to console initialisation. Use
2971 * after console initialisation will cause undefined behaviour.
2972 */
2973int __init early_serial_setup(struct uart_port *port)
2974{
2975 struct uart_port *p;
2976
2977 if (port->line >= ARRAY_SIZE(serial8250_ports))
2978 return -ENODEV;
2979
2980 serial8250_isa_init_ports();
2981 p = &serial8250_ports[port->line].port;
2982 p->iobase = port->iobase;
2983 p->membase = port->membase;
2984 p->irq = port->irq;
2985 p->irqflags = port->irqflags;
2986 p->uartclk = port->uartclk;
2987 p->fifosize = port->fifosize;
2988 p->regshift = port->regshift;
2989 p->iotype = port->iotype;
2990 p->flags = port->flags;
2991 p->mapbase = port->mapbase;
2992 p->private_data = port->private_data;
2993 p->type = port->type;
2994 p->line = port->line;
2995
2996 set_io_from_upio(p);
2997 if (port->serial_in)
2998 p->serial_in = port->serial_in;
2999 if (port->serial_out)
3000 p->serial_out = port->serial_out;
3001
3002 return 0;
3003}
3004
3005/**
3006 * serial8250_suspend_port - suspend one serial port
3007 * @line: serial line number
3008 *
3009 * Suspend one serial port.
3010 */
3011void serial8250_suspend_port(int line)
3012{
3013 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
3014}
3015
3016/**
3017 * serial8250_resume_port - resume one serial port
3018 * @line: serial line number
3019 *
3020 * Resume one serial port.
3021 */
3022void serial8250_resume_port(int line)
3023{
3024 struct uart_8250_port *up = &serial8250_ports[line];
3025
3026 if (up->capabilities & UART_NATSEMI) {
3027 unsigned char tmp;
3028
3029 /* Ensure it's still in high speed mode */
3030 serial_outp(up, UART_LCR, 0xE0);
3031
3032 tmp = serial_in(up, 0x04); /* EXCR2 */
3033 tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
3034 tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
3035 serial_outp(up, 0x04, tmp);
3036
3037 serial_outp(up, UART_LCR, 0);
3038 }
3039 uart_resume_port(&serial8250_reg, &up->port);
3040}
3041
3042/*
3043 * Register a set of serial devices attached to a platform device. The
3044 * list is terminated with a zero flags entry, which means we expect
3045 * all entries to have at least UPF_BOOT_AUTOCONF set.
3046 */
3047static int __devinit serial8250_probe(struct platform_device *dev)
3048{
3049 struct plat_serial8250_port *p = dev->dev.platform_data;
3050 struct uart_port port;
3051 int ret, i, irqflag = 0;
3052
3053 memset(&port, 0, sizeof(struct uart_port));
3054
3055 if (share_irqs)
3056 irqflag = IRQF_SHARED;
3057
3058 for (i = 0; p && p->flags != 0; p++, i++) {
3059 port.iobase = p->iobase;
3060 port.membase = p->membase;
3061 port.irq = p->irq;
3062 port.irqflags = p->irqflags;
3063 port.uartclk = p->uartclk;
3064 port.regshift = p->regshift;
3065 port.iotype = p->iotype;
3066 port.flags = p->flags;
3067 port.mapbase = p->mapbase;
3068 port.hub6 = p->hub6;
3069 port.private_data = p->private_data;
3070 port.type = p->type;
3071 port.serial_in = p->serial_in;
3072 port.serial_out = p->serial_out;
3073 port.set_termios = p->set_termios;
3074 port.pm = p->pm;
3075 port.dev = &dev->dev;
3076 port.irqflags |= irqflag;
3077 ret = serial8250_register_port(&port);
3078 if (ret < 0) {
3079 dev_err(&dev->dev, "unable to register port at index %d "
3080 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3081 p->iobase, (unsigned long long)p->mapbase,
3082 p->irq, ret);
3083 }
3084 }
3085 return 0;
3086}
3087
3088/*
3089 * Remove serial ports registered against a platform device.
3090 */
3091static int __devexit serial8250_remove(struct platform_device *dev)
3092{
3093 int i;
3094
3095 for (i = 0; i < nr_uarts; i++) {
3096 struct uart_8250_port *up = &serial8250_ports[i];
3097
3098 if (up->port.dev == &dev->dev)
3099 serial8250_unregister_port(i);
3100 }
3101 return 0;
3102}
3103
3104static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
3105{
3106 int i;
3107
3108 for (i = 0; i < UART_NR; i++) {
3109 struct uart_8250_port *up = &serial8250_ports[i];
3110
3111 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3112 uart_suspend_port(&serial8250_reg, &up->port);
3113 }
3114
3115 return 0;
3116}
3117
3118static int serial8250_resume(struct platform_device *dev)
3119{
3120 int i;
3121
3122 for (i = 0; i < UART_NR; i++) {
3123 struct uart_8250_port *up = &serial8250_ports[i];
3124
3125 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3126 serial8250_resume_port(i);
3127 }
3128
3129 return 0;
3130}
3131
3132static struct platform_driver serial8250_isa_driver = {
3133 .probe = serial8250_probe,
3134 .remove = __devexit_p(serial8250_remove),
3135 .suspend = serial8250_suspend,
3136 .resume = serial8250_resume,
3137 .driver = {
3138 .name = "serial8250",
3139 .owner = THIS_MODULE,
3140 },
3141};
3142
3143/*
3144 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3145 * in the table in include/asm/serial.h
3146 */
3147static struct platform_device *serial8250_isa_devs;
3148
3149/*
3150 * serial8250_register_port and serial8250_unregister_port allows for
3151 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3152 * modems and PCI multiport cards.
3153 */
3154static DEFINE_MUTEX(serial_mutex);
3155
3156static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3157{
3158 int i;
3159
3160 /*
3161 * First, find a port entry which matches.
3162 */
3163 for (i = 0; i < nr_uarts; i++)
3164 if (uart_match_port(&serial8250_ports[i].port, port))
3165 return &serial8250_ports[i];
3166
3167 /*
3168 * We didn't find a matching entry, so look for the first
3169 * free entry. We look for one which hasn't been previously
3170 * used (indicated by zero iobase).
3171 */
3172 for (i = 0; i < nr_uarts; i++)
3173 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3174 serial8250_ports[i].port.iobase == 0)
3175 return &serial8250_ports[i];
3176
3177 /*
3178 * That also failed. Last resort is to find any entry which
3179 * doesn't have a real port associated with it.
3180 */
3181 for (i = 0; i < nr_uarts; i++)
3182 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3183 return &serial8250_ports[i];
3184
3185 return NULL;
3186}
3187
3188/**
3189 * serial8250_register_port - register a serial port
3190 * @port: serial port template
3191 *
3192 * Configure the serial port specified by the request. If the
3193 * port exists and is in use, it is hung up and unregistered
3194 * first.
3195 *
3196 * The port is then probed and if necessary the IRQ is autodetected
3197 * If this fails an error is returned.
3198 *
3199 * On success the port is ready to use and the line number is returned.
3200 */
3201int serial8250_register_port(struct uart_port *port)
3202{
3203 struct uart_8250_port *uart;
3204 int ret = -ENOSPC;
3205
3206 if (port->uartclk == 0)
3207 return -EINVAL;
3208
3209 mutex_lock(&serial_mutex);
3210
3211 uart = serial8250_find_match_or_unused(port);
3212 if (uart) {
3213 uart_remove_one_port(&serial8250_reg, &uart->port);
3214
3215 uart->port.iobase = port->iobase;
3216 uart->port.membase = port->membase;
3217 uart->port.irq = port->irq;
3218 uart->port.irqflags = port->irqflags;
3219 uart->port.uartclk = port->uartclk;
3220 uart->port.fifosize = port->fifosize;
3221 uart->port.regshift = port->regshift;
3222 uart->port.iotype = port->iotype;
3223 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3224 uart->port.mapbase = port->mapbase;
3225 uart->port.private_data = port->private_data;
3226 if (port->dev)
3227 uart->port.dev = port->dev;
3228
3229 if (port->flags & UPF_FIXED_TYPE)
3230 serial8250_init_fixed_type_port(uart, port->type);
3231
3232 set_io_from_upio(&uart->port);
3233 /* Possibly override default I/O functions. */
3234 if (port->serial_in)
3235 uart->port.serial_in = port->serial_in;
3236 if (port->serial_out)
3237 uart->port.serial_out = port->serial_out;
3238 /* Possibly override set_termios call */
3239 if (port->set_termios)
3240 uart->port.set_termios = port->set_termios;
3241 if (port->pm)
3242 uart->port.pm = port->pm;
3243
3244 if (serial8250_isa_config != NULL)
3245 serial8250_isa_config(0, &uart->port,
3246 &uart->capabilities);
3247
3248 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3249 if (ret == 0)
3250 ret = uart->port.line;
3251 }
3252 mutex_unlock(&serial_mutex);
3253
3254 return ret;
3255}
3256EXPORT_SYMBOL(serial8250_register_port);
3257
3258/**
3259 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3260 * @line: serial line number
3261 *
3262 * Remove one serial port. This may not be called from interrupt
3263 * context. We hand the port back to the our control.
3264 */
3265void serial8250_unregister_port(int line)
3266{
3267 struct uart_8250_port *uart = &serial8250_ports[line];
3268
3269 mutex_lock(&serial_mutex);
3270 uart_remove_one_port(&serial8250_reg, &uart->port);
3271 if (serial8250_isa_devs) {
3272 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3273 uart->port.type = PORT_UNKNOWN;
3274 uart->port.dev = &serial8250_isa_devs->dev;
3275 uart_add_one_port(&serial8250_reg, &uart->port);
3276 } else {
3277 uart->port.dev = NULL;
3278 }
3279 mutex_unlock(&serial_mutex);
3280}
3281EXPORT_SYMBOL(serial8250_unregister_port);
3282
3283static int __init serial8250_init(void)
3284{
3285 int ret;
3286
3287 if (nr_uarts > UART_NR)
3288 nr_uarts = UART_NR;
3289
3290 printk(KERN_INFO "Serial: 8250/16550 driver, "
3291 "%d ports, IRQ sharing %sabled\n", nr_uarts,
3292 share_irqs ? "en" : "dis");
3293
3294#ifdef CONFIG_SPARC
3295 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3296#else
3297 serial8250_reg.nr = UART_NR;
3298 ret = uart_register_driver(&serial8250_reg);
3299#endif
3300 if (ret)
3301 goto out;
3302
3303 serial8250_isa_devs = platform_device_alloc("serial8250",
3304 PLAT8250_DEV_LEGACY);
3305 if (!serial8250_isa_devs) {
3306 ret = -ENOMEM;
3307 goto unreg_uart_drv;
3308 }
3309
3310 ret = platform_device_add(serial8250_isa_devs);
3311 if (ret)
3312 goto put_dev;
3313
3314 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3315
3316 ret = platform_driver_register(&serial8250_isa_driver);
3317 if (ret == 0)
3318 goto out;
3319
3320 platform_device_del(serial8250_isa_devs);
3321put_dev:
3322 platform_device_put(serial8250_isa_devs);
3323unreg_uart_drv:
3324#ifdef CONFIG_SPARC
3325 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3326#else
3327 uart_unregister_driver(&serial8250_reg);
3328#endif
3329out:
3330 return ret;
3331}
3332
3333static void __exit serial8250_exit(void)
3334{
3335 struct platform_device *isa_dev = serial8250_isa_devs;
3336
3337 /*
3338 * This tells serial8250_unregister_port() not to re-register
3339 * the ports (thereby making serial8250_isa_driver permanently
3340 * in use.)
3341 */
3342 serial8250_isa_devs = NULL;
3343
3344 platform_driver_unregister(&serial8250_isa_driver);
3345 platform_device_unregister(isa_dev);
3346
3347#ifdef CONFIG_SPARC
3348 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3349#else
3350 uart_unregister_driver(&serial8250_reg);
3351#endif
3352}
3353
3354module_init(serial8250_init);
3355module_exit(serial8250_exit);
3356
3357EXPORT_SYMBOL(serial8250_suspend_port);
3358EXPORT_SYMBOL(serial8250_resume_port);
3359
3360MODULE_LICENSE("GPL");
3361MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
3362
3363module_param(share_irqs, uint, 0644);
3364MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3365 " (unsafe)");
3366
3367module_param(nr_uarts, uint, 0644);
3368MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3369
3370module_param(skip_txen_test, uint, 0644);
3371MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3372
3373#ifdef CONFIG_SERIAL_8250_RSA
3374module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3375MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3376#endif
3377MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
diff --git a/drivers/tty/serial/8250.h b/drivers/tty/serial/8250.h
new file mode 100644
index 000000000000..6e19ea3e48d5
--- /dev/null
+++ b/drivers/tty/serial/8250.h
@@ -0,0 +1,80 @@
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
16#include <linux/serial_8250.h>
17
18struct old_serial_port {
19 unsigned int uart;
20 unsigned int baud_base;
21 unsigned int port;
22 unsigned int irq;
23 unsigned int flags;
24 unsigned char hub6;
25 unsigned char io_type;
26 unsigned char *iomem_base;
27 unsigned short iomem_reg_shift;
28 unsigned long irqflags;
29};
30
31/*
32 * This replaces serial_uart_config in include/linux/serial.h
33 */
34struct serial8250_config {
35 const char *name;
36 unsigned short fifo_size;
37 unsigned short tx_loadsz;
38 unsigned char fcr;
39 unsigned int flags;
40};
41
42#define UART_CAP_FIFO (1 << 8) /* UART has FIFO */
43#define UART_CAP_EFR (1 << 9) /* UART has EFR */
44#define UART_CAP_SLEEP (1 << 10) /* UART has IER sleep */
45#define UART_CAP_AFE (1 << 11) /* MCR-based hw flow control */
46#define UART_CAP_UUE (1 << 12) /* UART needs IER bit 6 set (Xscale) */
47
48#define UART_BUG_QUOT (1 << 0) /* UART has buggy quot LSB */
49#define UART_BUG_TXEN (1 << 1) /* UART has buggy TX IIR status */
50#define UART_BUG_NOMSR (1 << 2) /* UART has buggy MSR status bits (Au1x00) */
51#define UART_BUG_THRE (1 << 3) /* UART has buggy THRE reassertion */
52
53#define PROBE_RSA (1 << 0)
54#define PROBE_ANY (~0)
55
56#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
57
58#ifdef CONFIG_SERIAL_8250_SHARE_IRQ
59#define SERIAL8250_SHARE_IRQS 1
60#else
61#define SERIAL8250_SHARE_IRQS 0
62#endif
63
64#if defined(__alpha__) && !defined(CONFIG_PCI)
65/*
66 * Digital did something really horribly wrong with the OUT1 and OUT2
67 * lines on at least some ALPHA's. The failure mode is that if either
68 * is cleared, the machine locks up with endless interrupts.
69 */
70#define ALPHA_KLUDGE_MCR (UART_MCR_OUT2 | UART_MCR_OUT1)
71#elif defined(CONFIG_SBC8560)
72/*
73 * WindRiver did something similarly broken on their SBC8560 board. The
74 * UART tristates its IRQ output while OUT2 is clear, but they pulled
75 * the interrupt line _up_ instead of down, so if we register the IRQ
76 * while the UART is in that state, we die in an IRQ storm. */
77#define ALPHA_KLUDGE_MCR (UART_MCR_OUT2)
78#else
79#define ALPHA_KLUDGE_MCR 0
80#endif
diff --git a/drivers/tty/serial/8250_accent.c b/drivers/tty/serial/8250_accent.c
new file mode 100644
index 000000000000..9c10262f2469
--- /dev/null
+++ b/drivers/tty/serial/8250_accent.c
@@ -0,0 +1,47 @@
1/*
2 * linux/drivers/serial/8250_accent.c
3 *
4 * Copyright (C) 2005 Russell King.
5 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/serial_8250.h>
14
15#define PORT(_base,_irq) \
16 { \
17 .iobase = _base, \
18 .irq = _irq, \
19 .uartclk = 1843200, \
20 .iotype = UPIO_PORT, \
21 .flags = UPF_BOOT_AUTOCONF, \
22 }
23
24static struct plat_serial8250_port accent_data[] = {
25 PORT(0x330, 4),
26 PORT(0x338, 4),
27 { },
28};
29
30static struct platform_device accent_device = {
31 .name = "serial8250",
32 .id = PLAT8250_DEV_ACCENT,
33 .dev = {
34 .platform_data = accent_data,
35 },
36};
37
38static int __init accent_init(void)
39{
40 return platform_device_register(&accent_device);
41}
42
43module_init(accent_init);
44
45MODULE_AUTHOR("Russell King");
46MODULE_DESCRIPTION("8250 serial probe module for Accent Async cards");
47MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_acorn.c b/drivers/tty/serial/8250_acorn.c
new file mode 100644
index 000000000000..b0ce8c56f1a4
--- /dev/null
+++ b/drivers/tty/serial/8250_acorn.c
@@ -0,0 +1,141 @@
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 void __iomem *vaddr;
39};
40
41static int __devinit
42serial_card_probe(struct expansion_card *ec, const struct ecard_id *id)
43{
44 struct serial_card_info *info;
45 struct serial_card_type *type = id->data;
46 struct uart_port port;
47 unsigned long bus_addr;
48 unsigned int i;
49
50 info = kzalloc(sizeof(struct serial_card_info), GFP_KERNEL);
51 if (!info)
52 return -ENOMEM;
53
54 info->num_ports = type->num_ports;
55
56 bus_addr = ecard_resource_start(ec, type->type);
57 info->vaddr = ecardm_iomap(ec, type->type, 0, 0);
58 if (!info->vaddr) {
59 kfree(info);
60 return -ENOMEM;
61 }
62
63 ecard_set_drvdata(ec, info);
64
65 memset(&port, 0, sizeof(struct uart_port));
66 port.irq = ec->irq;
67 port.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
68 port.uartclk = type->uartclk;
69 port.iotype = UPIO_MEM;
70 port.regshift = 2;
71 port.dev = &ec->dev;
72
73 for (i = 0; i < info->num_ports; i ++) {
74 port.membase = info->vaddr + type->offset[i];
75 port.mapbase = bus_addr + type->offset[i];
76
77 info->ports[i] = serial8250_register_port(&port);
78 }
79
80 return 0;
81}
82
83static void __devexit serial_card_remove(struct expansion_card *ec)
84{
85 struct serial_card_info *info = ecard_get_drvdata(ec);
86 int i;
87
88 ecard_set_drvdata(ec, NULL);
89
90 for (i = 0; i < info->num_ports; i++)
91 if (info->ports[i] > 0)
92 serial8250_unregister_port(info->ports[i]);
93
94 kfree(info);
95}
96
97static struct serial_card_type atomwide_type = {
98 .num_ports = 3,
99 .uartclk = 7372800,
100 .type = ECARD_RES_IOCSLOW,
101 .offset = { 0x2800, 0x2400, 0x2000 },
102};
103
104static struct serial_card_type serport_type = {
105 .num_ports = 2,
106 .uartclk = 3686400,
107 .type = ECARD_RES_IOCSLOW,
108 .offset = { 0x2000, 0x2020 },
109};
110
111static const struct ecard_id serial_cids[] = {
112 { MANU_ATOMWIDE, PROD_ATOMWIDE_3PSERIAL, &atomwide_type },
113 { MANU_SERPORT, PROD_SERPORT_DSPORT, &serport_type },
114 { 0xffff, 0xffff }
115};
116
117static struct ecard_driver serial_card_driver = {
118 .probe = serial_card_probe,
119 .remove = __devexit_p(serial_card_remove),
120 .id_table = serial_cids,
121 .drv = {
122 .name = "8250_acorn",
123 },
124};
125
126static int __init serial_card_init(void)
127{
128 return ecard_register_driver(&serial_card_driver);
129}
130
131static void __exit serial_card_exit(void)
132{
133 ecard_remove_driver(&serial_card_driver);
134}
135
136MODULE_AUTHOR("Russell King");
137MODULE_DESCRIPTION("Acorn 8250-compatible serial port expansion card driver");
138MODULE_LICENSE("GPL");
139
140module_init(serial_card_init);
141module_exit(serial_card_exit);
diff --git a/drivers/tty/serial/8250_boca.c b/drivers/tty/serial/8250_boca.c
new file mode 100644
index 000000000000..3bfe0f7b26fb
--- /dev/null
+++ b/drivers/tty/serial/8250_boca.c
@@ -0,0 +1,61 @@
1/*
2 * linux/drivers/serial/8250_boca.c
3 *
4 * Copyright (C) 2005 Russell King.
5 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/serial_8250.h>
14
15#define PORT(_base,_irq) \
16 { \
17 .iobase = _base, \
18 .irq = _irq, \
19 .uartclk = 1843200, \
20 .iotype = UPIO_PORT, \
21 .flags = UPF_BOOT_AUTOCONF, \
22 }
23
24static struct plat_serial8250_port boca_data[] = {
25 PORT(0x100, 12),
26 PORT(0x108, 12),
27 PORT(0x110, 12),
28 PORT(0x118, 12),
29 PORT(0x120, 12),
30 PORT(0x128, 12),
31 PORT(0x130, 12),
32 PORT(0x138, 12),
33 PORT(0x140, 12),
34 PORT(0x148, 12),
35 PORT(0x150, 12),
36 PORT(0x158, 12),
37 PORT(0x160, 12),
38 PORT(0x168, 12),
39 PORT(0x170, 12),
40 PORT(0x178, 12),
41 { },
42};
43
44static struct platform_device boca_device = {
45 .name = "serial8250",
46 .id = PLAT8250_DEV_BOCA,
47 .dev = {
48 .platform_data = boca_data,
49 },
50};
51
52static int __init boca_init(void)
53{
54 return platform_device_register(&boca_device);
55}
56
57module_init(boca_init);
58
59MODULE_AUTHOR("Russell King");
60MODULE_DESCRIPTION("8250 serial probe module for Boca cards");
61MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_early.c b/drivers/tty/serial/8250_early.c
new file mode 100644
index 000000000000..eaafb98debed
--- /dev/null
+++ b/drivers/tty/serial/8250_early.c
@@ -0,0 +1,287 @@
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 * earlycon=uart8250,io,0x3f8,9600n8
21 * earlycon=uart8250,mmio,0xff5e0000,115200n8
22 * earlycon=uart8250,mmio32,0xff5e0000,115200n8
23 * or
24 * console=uart8250,io,0x3f8,9600n8
25 * console=uart8250,mmio,0xff5e0000,115200n8
26 * console=uart8250,mmio32,0xff5e0000,115200n8
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 <linux/serial_8250.h>
36#include <asm/io.h>
37#include <asm/serial.h>
38#ifdef CONFIG_FIX_EARLYCON_MEM
39#include <asm/pgtable.h>
40#include <asm/fixmap.h>
41#endif
42
43struct early_serial8250_device {
44 struct uart_port port;
45 char options[16]; /* e.g., 115200n8 */
46 unsigned int baud;
47};
48
49static struct early_serial8250_device early_device;
50
51static unsigned int __init serial_in(struct uart_port *port, int offset)
52{
53 switch (port->iotype) {
54 case UPIO_MEM:
55 return readb(port->membase + offset);
56 case UPIO_MEM32:
57 return readl(port->membase + (offset << 2));
58 case UPIO_PORT:
59 return inb(port->iobase + offset);
60 default:
61 return 0;
62 }
63}
64
65static void __init serial_out(struct uart_port *port, int offset, int value)
66{
67 switch (port->iotype) {
68 case UPIO_MEM:
69 writeb(value, port->membase + offset);
70 break;
71 case UPIO_MEM32:
72 writel(value, port->membase + (offset << 2));
73 break;
74 case UPIO_PORT:
75 outb(value, port->iobase + offset);
76 break;
77 }
78}
79
80#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
81
82static void __init wait_for_xmitr(struct uart_port *port)
83{
84 unsigned int status;
85
86 for (;;) {
87 status = serial_in(port, UART_LSR);
88 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
89 return;
90 cpu_relax();
91 }
92}
93
94static void __init serial_putc(struct uart_port *port, int c)
95{
96 wait_for_xmitr(port);
97 serial_out(port, UART_TX, c);
98}
99
100static void __init early_serial8250_write(struct console *console,
101 const char *s, unsigned int count)
102{
103 struct uart_port *port = &early_device.port;
104 unsigned int ier;
105
106 /* Save the IER and disable interrupts */
107 ier = serial_in(port, UART_IER);
108 serial_out(port, UART_IER, 0);
109
110 uart_console_write(port, s, count, serial_putc);
111
112 /* Wait for transmitter to become empty and restore the IER */
113 wait_for_xmitr(port);
114 serial_out(port, UART_IER, ier);
115}
116
117static unsigned int __init probe_baud(struct uart_port *port)
118{
119 unsigned char lcr, dll, dlm;
120 unsigned int quot;
121
122 lcr = serial_in(port, UART_LCR);
123 serial_out(port, UART_LCR, lcr | UART_LCR_DLAB);
124 dll = serial_in(port, UART_DLL);
125 dlm = serial_in(port, UART_DLM);
126 serial_out(port, UART_LCR, lcr);
127
128 quot = (dlm << 8) | dll;
129 return (port->uartclk / 16) / quot;
130}
131
132static void __init init_port(struct early_serial8250_device *device)
133{
134 struct uart_port *port = &device->port;
135 unsigned int divisor;
136 unsigned char c;
137
138 serial_out(port, UART_LCR, 0x3); /* 8n1 */
139 serial_out(port, UART_IER, 0); /* no interrupt */
140 serial_out(port, UART_FCR, 0); /* no fifo */
141 serial_out(port, UART_MCR, 0x3); /* DTR + RTS */
142
143 divisor = port->uartclk / (16 * device->baud);
144 c = serial_in(port, UART_LCR);
145 serial_out(port, UART_LCR, c | UART_LCR_DLAB);
146 serial_out(port, UART_DLL, divisor & 0xff);
147 serial_out(port, UART_DLM, (divisor >> 8) & 0xff);
148 serial_out(port, UART_LCR, c & ~UART_LCR_DLAB);
149}
150
151static int __init parse_options(struct early_serial8250_device *device,
152 char *options)
153{
154 struct uart_port *port = &device->port;
155 int mmio, mmio32, length;
156
157 if (!options)
158 return -ENODEV;
159
160 port->uartclk = BASE_BAUD * 16;
161
162 mmio = !strncmp(options, "mmio,", 5);
163 mmio32 = !strncmp(options, "mmio32,", 7);
164 if (mmio || mmio32) {
165 port->iotype = (mmio ? UPIO_MEM : UPIO_MEM32);
166 port->mapbase = simple_strtoul(options + (mmio ? 5 : 7),
167 &options, 0);
168 if (mmio32)
169 port->regshift = 2;
170#ifdef CONFIG_FIX_EARLYCON_MEM
171 set_fixmap_nocache(FIX_EARLYCON_MEM_BASE,
172 port->mapbase & PAGE_MASK);
173 port->membase =
174 (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
175 port->membase += port->mapbase & ~PAGE_MASK;
176#else
177 port->membase = ioremap_nocache(port->mapbase, 64);
178 if (!port->membase) {
179 printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n",
180 __func__,
181 (unsigned long long) port->mapbase);
182 return -ENOMEM;
183 }
184#endif
185 } else if (!strncmp(options, "io,", 3)) {
186 port->iotype = UPIO_PORT;
187 port->iobase = simple_strtoul(options + 3, &options, 0);
188 mmio = 0;
189 } else
190 return -EINVAL;
191
192 options = strchr(options, ',');
193 if (options) {
194 options++;
195 device->baud = simple_strtoul(options, NULL, 0);
196 length = min(strcspn(options, " "), sizeof(device->options));
197 strncpy(device->options, options, length);
198 } else {
199 device->baud = probe_baud(port);
200 snprintf(device->options, sizeof(device->options), "%u",
201 device->baud);
202 }
203
204 if (mmio || mmio32)
205 printk(KERN_INFO
206 "Early serial console at MMIO%s 0x%llx (options '%s')\n",
207 mmio32 ? "32" : "",
208 (unsigned long long)port->mapbase,
209 device->options);
210 else
211 printk(KERN_INFO
212 "Early serial console at I/O port 0x%lx (options '%s')\n",
213 port->iobase,
214 device->options);
215
216 return 0;
217}
218
219static struct console early_serial8250_console __initdata = {
220 .name = "uart",
221 .write = early_serial8250_write,
222 .flags = CON_PRINTBUFFER | CON_BOOT,
223 .index = -1,
224};
225
226static int __init early_serial8250_setup(char *options)
227{
228 struct early_serial8250_device *device = &early_device;
229 int err;
230
231 if (device->port.membase || device->port.iobase)
232 return 0;
233
234 err = parse_options(device, options);
235 if (err < 0)
236 return err;
237
238 init_port(device);
239 return 0;
240}
241
242int __init setup_early_serial8250_console(char *cmdline)
243{
244 char *options;
245 int err;
246
247 options = strstr(cmdline, "uart8250,");
248 if (!options) {
249 options = strstr(cmdline, "uart,");
250 if (!options)
251 return 0;
252 }
253
254 options = strchr(cmdline, ',') + 1;
255 err = early_serial8250_setup(options);
256 if (err < 0)
257 return err;
258
259 register_console(&early_serial8250_console);
260
261 return 0;
262}
263
264int serial8250_find_port_for_earlycon(void)
265{
266 struct early_serial8250_device *device = &early_device;
267 struct uart_port *port = &device->port;
268 int line;
269 int ret;
270
271 if (!device->port.membase && !device->port.iobase)
272 return -ENODEV;
273
274 line = serial8250_find_port(port);
275 if (line < 0)
276 return -ENODEV;
277
278 ret = update_console_cmdline("uart", 8250,
279 "ttyS", line, device->options);
280 if (ret < 0)
281 ret = update_console_cmdline("uart", 0,
282 "ttyS", line, device->options);
283
284 return ret;
285}
286
287early_param("earlycon", setup_early_serial8250_console);
diff --git a/drivers/tty/serial/8250_exar_st16c554.c b/drivers/tty/serial/8250_exar_st16c554.c
new file mode 100644
index 000000000000..567143ace159
--- /dev/null
+++ b/drivers/tty/serial/8250_exar_st16c554.c
@@ -0,0 +1,52 @@
1/*
2 * linux/drivers/serial/8250_exar.c
3 *
4 * Written by Paul B Schroeder < pschroeder "at" uplogix "dot" com >
5 * Based on 8250_boca.
6 *
7 * Copyright (C) 2005 Russell King.
8 * Data taken from include/asm-i386/serial.h
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 version 2 as
12 * published by the Free Software Foundation.
13 */
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/serial_8250.h>
17
18#define PORT(_base,_irq) \
19 { \
20 .iobase = _base, \
21 .irq = _irq, \
22 .uartclk = 1843200, \
23 .iotype = UPIO_PORT, \
24 .flags = UPF_BOOT_AUTOCONF, \
25 }
26
27static struct plat_serial8250_port exar_data[] = {
28 PORT(0x100, 5),
29 PORT(0x108, 5),
30 PORT(0x110, 5),
31 PORT(0x118, 5),
32 { },
33};
34
35static struct platform_device exar_device = {
36 .name = "serial8250",
37 .id = PLAT8250_DEV_EXAR_ST16C554,
38 .dev = {
39 .platform_data = exar_data,
40 },
41};
42
43static int __init exar_init(void)
44{
45 return platform_device_register(&exar_device);
46}
47
48module_init(exar_init);
49
50MODULE_AUTHOR("Paul B Schroeder");
51MODULE_DESCRIPTION("8250 serial probe module for Exar cards");
52MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_fourport.c b/drivers/tty/serial/8250_fourport.c
new file mode 100644
index 000000000000..6375d68b7913
--- /dev/null
+++ b/drivers/tty/serial/8250_fourport.c
@@ -0,0 +1,53 @@
1/*
2 * linux/drivers/serial/8250_fourport.c
3 *
4 * Copyright (C) 2005 Russell King.
5 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/serial_8250.h>
14
15#define PORT(_base,_irq) \
16 { \
17 .iobase = _base, \
18 .irq = _irq, \
19 .uartclk = 1843200, \
20 .iotype = UPIO_PORT, \
21 .flags = UPF_BOOT_AUTOCONF | UPF_FOURPORT, \
22 }
23
24static struct plat_serial8250_port fourport_data[] = {
25 PORT(0x1a0, 9),
26 PORT(0x1a8, 9),
27 PORT(0x1b0, 9),
28 PORT(0x1b8, 9),
29 PORT(0x2a0, 5),
30 PORT(0x2a8, 5),
31 PORT(0x2b0, 5),
32 PORT(0x2b8, 5),
33 { },
34};
35
36static struct platform_device fourport_device = {
37 .name = "serial8250",
38 .id = PLAT8250_DEV_FOURPORT,
39 .dev = {
40 .platform_data = fourport_data,
41 },
42};
43
44static int __init fourport_init(void)
45{
46 return platform_device_register(&fourport_device);
47}
48
49module_init(fourport_init);
50
51MODULE_AUTHOR("Russell King");
52MODULE_DESCRIPTION("8250 serial probe module for AST Fourport cards");
53MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_gsc.c b/drivers/tty/serial/8250_gsc.c
new file mode 100644
index 000000000000..d8c0ffbfa6e3
--- /dev/null
+++ b/drivers/tty/serial/8250_gsc.c
@@ -0,0 +1,122 @@
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/types.h>
20
21#include <asm/hardware.h>
22#include <asm/parisc-device.h>
23#include <asm/io.h>
24
25#include "8250.h"
26
27static int __init serial_init_chip(struct parisc_device *dev)
28{
29 struct uart_port port;
30 unsigned long address;
31 int err;
32
33 if (!dev->irq) {
34 /* We find some unattached serial ports by walking native
35 * busses. These should be silently ignored. Otherwise,
36 * what we have here is a missing parent device, so tell
37 * the user what they're missing.
38 */
39 if (parisc_parent(dev)->id.hw_type != HPHW_IOA)
40 printk(KERN_INFO
41 "Serial: device 0x%llx not configured.\n"
42 "Enable support for Wax, Lasi, Asp or Dino.\n",
43 (unsigned long long)dev->hpa.start);
44 return -ENODEV;
45 }
46
47 address = dev->hpa.start;
48 if (dev->id.sversion != 0x8d)
49 address += 0x800;
50
51 memset(&port, 0, sizeof(port));
52 port.iotype = UPIO_MEM;
53 /* 7.272727MHz on Lasi. Assumed the same for Dino, Wax and Timi. */
54 port.uartclk = 7272727;
55 port.mapbase = address;
56 port.membase = ioremap_nocache(address, 16);
57 port.irq = dev->irq;
58 port.flags = UPF_BOOT_AUTOCONF;
59 port.dev = &dev->dev;
60
61 err = serial8250_register_port(&port);
62 if (err < 0) {
63 printk(KERN_WARNING
64 "serial8250_register_port returned error %d\n", err);
65 iounmap(port.membase);
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
113static int __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);
121
122MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_hp300.c b/drivers/tty/serial/8250_hp300.c
new file mode 100644
index 000000000000..c13438c93012
--- /dev/null
+++ b/drivers/tty/serial/8250_hp300.c
@@ -0,0 +1,327 @@
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/serial.h>
13#include <linux/serial_core.h>
14#include <linux/serial_8250.h>
15#include <linux/delay.h>
16#include <linux/dio.h>
17#include <linux/console.h>
18#include <linux/slab.h>
19#include <asm/io.h>
20
21#include "8250.h"
22
23#if !defined(CONFIG_HPDCA) && !defined(CONFIG_HPAPCI)
24#warning CONFIG_8250 defined but neither CONFIG_HPDCA nor CONFIG_HPAPCI defined, are you sure?
25#endif
26
27#ifdef CONFIG_HPAPCI
28struct hp300_port
29{
30 struct hp300_port *next; /* next port */
31 int line; /* line (tty) number */
32};
33
34static struct hp300_port *hp300_ports;
35#endif
36
37#ifdef CONFIG_HPDCA
38
39static int __devinit hpdca_init_one(struct dio_dev *d,
40 const struct dio_device_id *ent);
41static void __devexit hpdca_remove_one(struct dio_dev *d);
42
43static struct dio_device_id hpdca_dio_tbl[] = {
44 { DIO_ID_DCA0 },
45 { DIO_ID_DCA0REM },
46 { DIO_ID_DCA1 },
47 { DIO_ID_DCA1REM },
48 { 0 }
49};
50
51static struct dio_driver hpdca_driver = {
52 .name = "hpdca",
53 .id_table = hpdca_dio_tbl,
54 .probe = hpdca_init_one,
55 .remove = __devexit_p(hpdca_remove_one),
56};
57
58#endif
59
60static unsigned int num_ports;
61
62extern int hp300_uart_scode;
63
64/* Offset to UART registers from base of DCA */
65#define UART_OFFSET 17
66
67#define DCA_ID 0x01 /* ID (read), reset (write) */
68#define DCA_IC 0x03 /* Interrupt control */
69
70/* Interrupt control */
71#define DCA_IC_IE 0x80 /* Master interrupt enable */
72
73#define HPDCA_BAUD_BASE 153600
74
75/* Base address of the Frodo part */
76#define FRODO_BASE (0x41c000)
77
78/*
79 * Where we find the 8250-like APCI ports, and how far apart they are.
80 */
81#define FRODO_APCIBASE 0x0
82#define FRODO_APCISPACE 0x20
83#define FRODO_APCI_OFFSET(x) (FRODO_APCIBASE + ((x) * FRODO_APCISPACE))
84
85#define HPAPCI_BAUD_BASE 500400
86
87#ifdef CONFIG_SERIAL_8250_CONSOLE
88/*
89 * Parse the bootinfo to find descriptions for headless console and
90 * debug serial ports and register them with the 8250 driver.
91 * This function should be called before serial_console_init() is called
92 * to make sure the serial console will be available for use. IA-64 kernel
93 * calls this function from setup_arch() after the EFI and ACPI tables have
94 * been parsed.
95 */
96int __init hp300_setup_serial_console(void)
97{
98 int scode;
99 struct uart_port port;
100
101 memset(&port, 0, sizeof(port));
102
103 if (hp300_uart_scode < 0 || hp300_uart_scode > DIO_SCMAX)
104 return 0;
105
106 if (DIO_SCINHOLE(hp300_uart_scode))
107 return 0;
108
109 scode = hp300_uart_scode;
110
111 /* Memory mapped I/O */
112 port.iotype = UPIO_MEM;
113 port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF;
114 port.type = PORT_UNKNOWN;
115
116 /* Check for APCI console */
117 if (scode == 256) {
118#ifdef CONFIG_HPAPCI
119 printk(KERN_INFO "Serial console is HP APCI 1\n");
120
121 port.uartclk = HPAPCI_BAUD_BASE * 16;
122 port.mapbase = (FRODO_BASE + FRODO_APCI_OFFSET(1));
123 port.membase = (char *)(port.mapbase + DIO_VIRADDRBASE);
124 port.regshift = 2;
125 add_preferred_console("ttyS", port.line, "9600n8");
126#else
127 printk(KERN_WARNING "Serial console is APCI but support is disabled (CONFIG_HPAPCI)!\n");
128 return 0;
129#endif
130 } else {
131#ifdef CONFIG_HPDCA
132 unsigned long pa = dio_scodetophysaddr(scode);
133 if (!pa)
134 return 0;
135
136 printk(KERN_INFO "Serial console is HP DCA at select code %d\n", scode);
137
138 port.uartclk = HPDCA_BAUD_BASE * 16;
139 port.mapbase = (pa + UART_OFFSET);
140 port.membase = (char *)(port.mapbase + DIO_VIRADDRBASE);
141 port.regshift = 1;
142 port.irq = DIO_IPL(pa + DIO_VIRADDRBASE);
143
144 /* Enable board-interrupts */
145 out_8(pa + DIO_VIRADDRBASE + DCA_IC, DCA_IC_IE);
146
147 if (DIO_ID(pa + DIO_VIRADDRBASE) & 0x80)
148 add_preferred_console("ttyS", port.line, "9600n8");
149#else
150 printk(KERN_WARNING "Serial console is DCA but support is disabled (CONFIG_HPDCA)!\n");
151 return 0;
152#endif
153 }
154
155 if (early_serial_setup(&port) < 0)
156 printk(KERN_WARNING "hp300_setup_serial_console(): early_serial_setup() failed.\n");
157 return 0;
158}
159#endif /* CONFIG_SERIAL_8250_CONSOLE */
160
161#ifdef CONFIG_HPDCA
162static int __devinit hpdca_init_one(struct dio_dev *d,
163 const struct dio_device_id *ent)
164{
165 struct uart_port port;
166 int line;
167
168#ifdef CONFIG_SERIAL_8250_CONSOLE
169 if (hp300_uart_scode == d->scode) {
170 /* Already got it. */
171 return 0;
172 }
173#endif
174 memset(&port, 0, sizeof(struct uart_port));
175
176 /* Memory mapped I/O */
177 port.iotype = UPIO_MEM;
178 port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF;
179 port.irq = d->ipl;
180 port.uartclk = HPDCA_BAUD_BASE * 16;
181 port.mapbase = (d->resource.start + UART_OFFSET);
182 port.membase = (char *)(port.mapbase + DIO_VIRADDRBASE);
183 port.regshift = 1;
184 port.dev = &d->dev;
185 line = serial8250_register_port(&port);
186
187 if (line < 0) {
188 printk(KERN_NOTICE "8250_hp300: register_serial() DCA scode %d"
189 " irq %d failed\n", d->scode, port.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 num_ports++;
202
203 return 0;
204}
205#endif
206
207static int __init hp300_8250_init(void)
208{
209 static int called;
210#ifdef CONFIG_HPAPCI
211 int line;
212 unsigned long base;
213 struct uart_port uport;
214 struct hp300_port *port;
215 int i;
216#endif
217 if (called)
218 return -ENODEV;
219 called = 1;
220
221 if (!MACH_IS_HP300)
222 return -ENODEV;
223
224#ifdef CONFIG_HPDCA
225 dio_register_driver(&hpdca_driver);
226#endif
227#ifdef CONFIG_HPAPCI
228 if (hp300_model < HP_400) {
229 if (!num_ports)
230 return -ENODEV;
231 return 0;
232 }
233 /* These models have the Frodo chip.
234 * Port 0 is reserved for the Apollo Domain keyboard.
235 * Port 1 is either the console or the DCA.
236 */
237 for (i = 1; i < 4; i++) {
238 /* Port 1 is the console on a 425e, on other machines it's
239 * mapped to DCA.
240 */
241#ifdef CONFIG_SERIAL_8250_CONSOLE
242 if (i == 1)
243 continue;
244#endif
245
246 /* Create new serial device */
247 port = kmalloc(sizeof(struct hp300_port), GFP_KERNEL);
248 if (!port)
249 return -ENOMEM;
250
251 memset(&uport, 0, sizeof(struct uart_port));
252
253 base = (FRODO_BASE + FRODO_APCI_OFFSET(i));
254
255 /* Memory mapped I/O */
256 uport.iotype = UPIO_MEM;
257 uport.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ \
258 | UPF_BOOT_AUTOCONF;
259 /* XXX - no interrupt support yet */
260 uport.irq = 0;
261 uport.uartclk = HPAPCI_BAUD_BASE * 16;
262 uport.mapbase = base;
263 uport.membase = (char *)(base + DIO_VIRADDRBASE);
264 uport.regshift = 2;
265
266 line = serial8250_register_port(&uport);
267
268 if (line < 0) {
269 printk(KERN_NOTICE "8250_hp300: register_serial() APCI"
270 " %d irq %d failed\n", i, uport.irq);
271 kfree(port);
272 continue;
273 }
274
275 port->line = line;
276 port->next = hp300_ports;
277 hp300_ports = port;
278
279 num_ports++;
280 }
281#endif
282
283 /* Any boards found? */
284 if (!num_ports)
285 return -ENODEV;
286
287 return 0;
288}
289
290#ifdef CONFIG_HPDCA
291static void __devexit hpdca_remove_one(struct dio_dev *d)
292{
293 int line;
294
295 line = (int) dio_get_drvdata(d);
296 if (d->resource.start) {
297 /* Disable board-interrupts */
298 out_8(d->resource.start + DIO_VIRADDRBASE + DCA_IC, 0);
299 }
300 serial8250_unregister_port(line);
301}
302#endif
303
304static void __exit hp300_8250_exit(void)
305{
306#ifdef CONFIG_HPAPCI
307 struct hp300_port *port, *to_free;
308
309 for (port = hp300_ports; port; ) {
310 serial8250_unregister_port(port->line);
311 to_free = port;
312 port = port->next;
313 kfree(to_free);
314 }
315
316 hp300_ports = NULL;
317#endif
318#ifdef CONFIG_HPDCA
319 dio_unregister_driver(&hpdca_driver);
320#endif
321}
322
323module_init(hp300_8250_init);
324module_exit(hp300_8250_exit);
325MODULE_DESCRIPTION("HP DCA/APCI serial driver");
326MODULE_AUTHOR("Kars de Jong <jongk@linux-m68k.org>");
327MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_hub6.c b/drivers/tty/serial/8250_hub6.c
new file mode 100644
index 000000000000..7609150e7d5e
--- /dev/null
+++ b/drivers/tty/serial/8250_hub6.c
@@ -0,0 +1,58 @@
1/*
2 * linux/drivers/serial/8250_hub6.c
3 *
4 * Copyright (C) 2005 Russell King.
5 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/serial_8250.h>
14
15#define HUB6(card,port) \
16 { \
17 .iobase = 0x302, \
18 .irq = 3, \
19 .uartclk = 1843200, \
20 .iotype = UPIO_HUB6, \
21 .flags = UPF_BOOT_AUTOCONF, \
22 .hub6 = (card) << 6 | (port) << 3 | 1, \
23 }
24
25static struct plat_serial8250_port hub6_data[] = {
26 HUB6(0, 0),
27 HUB6(0, 1),
28 HUB6(0, 2),
29 HUB6(0, 3),
30 HUB6(0, 4),
31 HUB6(0, 5),
32 HUB6(1, 0),
33 HUB6(1, 1),
34 HUB6(1, 2),
35 HUB6(1, 3),
36 HUB6(1, 4),
37 HUB6(1, 5),
38 { },
39};
40
41static struct platform_device hub6_device = {
42 .name = "serial8250",
43 .id = PLAT8250_DEV_HUB6,
44 .dev = {
45 .platform_data = hub6_data,
46 },
47};
48
49static int __init hub6_init(void)
50{
51 return platform_device_register(&hub6_device);
52}
53
54module_init(hub6_init);
55
56MODULE_AUTHOR("Russell King");
57MODULE_DESCRIPTION("8250 serial probe module for Hub6 cards");
58MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_mca.c b/drivers/tty/serial/8250_mca.c
new file mode 100644
index 000000000000..d10be944ad44
--- /dev/null
+++ b/drivers/tty/serial/8250_mca.c
@@ -0,0 +1,63 @@
1/*
2 * linux/drivers/serial/8250_mca.c
3 *
4 * Copyright (C) 2005 Russell King.
5 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/mca.h>
14#include <linux/serial_8250.h>
15
16/*
17 * FIXME: Should we be doing AUTO_IRQ here?
18 */
19#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
20#define MCA_FLAGS UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_AUTO_IRQ
21#else
22#define MCA_FLAGS UPF_BOOT_AUTOCONF | UPF_SKIP_TEST
23#endif
24
25#define PORT(_base,_irq) \
26 { \
27 .iobase = _base, \
28 .irq = _irq, \
29 .uartclk = 1843200, \
30 .iotype = UPIO_PORT, \
31 .flags = MCA_FLAGS, \
32 }
33
34static struct plat_serial8250_port mca_data[] = {
35 PORT(0x3220, 3),
36 PORT(0x3228, 3),
37 PORT(0x4220, 3),
38 PORT(0x4228, 3),
39 PORT(0x5220, 3),
40 PORT(0x5228, 3),
41 { },
42};
43
44static struct platform_device mca_device = {
45 .name = "serial8250",
46 .id = PLAT8250_DEV_MCA,
47 .dev = {
48 .platform_data = mca_data,
49 },
50};
51
52static int __init mca_init(void)
53{
54 if (!MCA_bus)
55 return -ENODEV;
56 return platform_device_register(&mca_device);
57}
58
59module_init(mca_init);
60
61MODULE_AUTHOR("Russell King");
62MODULE_DESCRIPTION("8250 serial probe module for MCA ports");
63MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_pci.c b/drivers/tty/serial/8250_pci.c
new file mode 100644
index 000000000000..8b8930f700b5
--- /dev/null
+++ b/drivers/tty/serial/8250_pci.c
@@ -0,0 +1,3850 @@
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#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/pci.h>
17#include <linux/string.h>
18#include <linux/kernel.h>
19#include <linux/slab.h>
20#include <linux/delay.h>
21#include <linux/tty.h>
22#include <linux/serial_core.h>
23#include <linux/8250_pci.h>
24#include <linux/bitops.h>
25
26#include <asm/byteorder.h>
27#include <asm/io.h>
28
29#include "8250.h"
30
31#undef SERIAL_DEBUG_PCI
32
33/*
34 * init function returns:
35 * > 0 - number of ports
36 * = 0 - use board->num_ports
37 * < 0 - error
38 */
39struct pci_serial_quirk {
40 u32 vendor;
41 u32 device;
42 u32 subvendor;
43 u32 subdevice;
44 int (*init)(struct pci_dev *dev);
45 int (*setup)(struct serial_private *,
46 const struct pciserial_board *,
47 struct uart_port *, int);
48 void (*exit)(struct pci_dev *dev);
49};
50
51#define PCI_NUM_BAR_RESOURCES 6
52
53struct serial_private {
54 struct pci_dev *dev;
55 unsigned int nr;
56 void __iomem *remapped_bar[PCI_NUM_BAR_RESOURCES];
57 struct pci_serial_quirk *quirk;
58 int line[0];
59};
60
61static void moan_device(const char *str, struct pci_dev *dev)
62{
63 printk(KERN_WARNING
64 "%s: %s\n"
65 "Please send the output of lspci -vv, this\n"
66 "message (0x%04x,0x%04x,0x%04x,0x%04x), the\n"
67 "manufacturer and name of serial board or\n"
68 "modem board to rmk+serial@arm.linux.org.uk.\n",
69 pci_name(dev), str, dev->vendor, dev->device,
70 dev->subsystem_vendor, dev->subsystem_device);
71}
72
73static int
74setup_port(struct serial_private *priv, struct uart_port *port,
75 int bar, int offset, int regshift)
76{
77 struct pci_dev *dev = priv->dev;
78 unsigned long base, len;
79
80 if (bar >= PCI_NUM_BAR_RESOURCES)
81 return -EINVAL;
82
83 base = pci_resource_start(dev, bar);
84
85 if (pci_resource_flags(dev, bar) & IORESOURCE_MEM) {
86 len = pci_resource_len(dev, bar);
87
88 if (!priv->remapped_bar[bar])
89 priv->remapped_bar[bar] = ioremap_nocache(base, len);
90 if (!priv->remapped_bar[bar])
91 return -ENOMEM;
92
93 port->iotype = UPIO_MEM;
94 port->iobase = 0;
95 port->mapbase = base + offset;
96 port->membase = priv->remapped_bar[bar] + offset;
97 port->regshift = regshift;
98 } else {
99 port->iotype = UPIO_PORT;
100 port->iobase = base + offset;
101 port->mapbase = 0;
102 port->membase = NULL;
103 port->regshift = 0;
104 }
105 return 0;
106}
107
108/*
109 * ADDI-DATA GmbH communication cards <info@addi-data.com>
110 */
111static int addidata_apci7800_setup(struct serial_private *priv,
112 const struct pciserial_board *board,
113 struct uart_port *port, int idx)
114{
115 unsigned int bar = 0, offset = board->first_offset;
116 bar = FL_GET_BASE(board->flags);
117
118 if (idx < 2) {
119 offset += idx * board->uart_offset;
120 } else if ((idx >= 2) && (idx < 4)) {
121 bar += 1;
122 offset += ((idx - 2) * board->uart_offset);
123 } else if ((idx >= 4) && (idx < 6)) {
124 bar += 2;
125 offset += ((idx - 4) * board->uart_offset);
126 } else if (idx >= 6) {
127 bar += 3;
128 offset += ((idx - 6) * board->uart_offset);
129 }
130
131 return setup_port(priv, port, bar, offset, board->reg_shift);
132}
133
134/*
135 * AFAVLAB uses a different mixture of BARs and offsets
136 * Not that ugly ;) -- HW
137 */
138static int
139afavlab_setup(struct serial_private *priv, const struct pciserial_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(priv, 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 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 case PCI_DEVICE_ID_HP_DIVA_HURRICANE:
181 rc = 1;
182 break;
183 }
184
185 return rc;
186}
187
188/*
189 * HP's Diva chip puts the 4th/5th serial port further out, and
190 * some serial ports are supposed to be hidden on certain models.
191 */
192static int
193pci_hp_diva_setup(struct serial_private *priv,
194 const struct pciserial_board *board,
195 struct uart_port *port, int idx)
196{
197 unsigned int offset = board->first_offset;
198 unsigned int bar = FL_GET_BASE(board->flags);
199
200 switch (priv->dev->subsystem_device) {
201 case PCI_DEVICE_ID_HP_DIVA_MAESTRO:
202 if (idx == 3)
203 idx++;
204 break;
205 case PCI_DEVICE_ID_HP_DIVA_EVEREST:
206 if (idx > 0)
207 idx++;
208 if (idx > 2)
209 idx++;
210 break;
211 }
212 if (idx > 2)
213 offset = 0x18;
214
215 offset += idx * board->uart_offset;
216
217 return setup_port(priv, port, bar, offset, board->reg_shift);
218}
219
220/*
221 * Added for EKF Intel i960 serial boards
222 */
223static int pci_inteli960ni_init(struct pci_dev *dev)
224{
225 unsigned long oldval;
226
227 if (!(dev->subsystem_device & 0x1000))
228 return -ENODEV;
229
230 /* is firmware started? */
231 pci_read_config_dword(dev, 0x44, (void *)&oldval);
232 if (oldval == 0x00001000L) { /* RESET value */
233 printk(KERN_DEBUG "Local i960 firmware missing");
234 return -ENODEV;
235 }
236 return 0;
237}
238
239/*
240 * Some PCI serial cards using the PLX 9050 PCI interface chip require
241 * that the card interrupt be explicitly enabled or disabled. This
242 * seems to be mainly needed on card using the PLX which also use I/O
243 * mapped memory.
244 */
245static int pci_plx9050_init(struct pci_dev *dev)
246{
247 u8 irq_config;
248 void __iomem *p;
249
250 if ((pci_resource_flags(dev, 0) & IORESOURCE_MEM) == 0) {
251 moan_device("no memory in bar 0", dev);
252 return 0;
253 }
254
255 irq_config = 0x41;
256 if (dev->vendor == PCI_VENDOR_ID_PANACOM ||
257 dev->subsystem_vendor == PCI_SUBVENDOR_ID_EXSYS)
258 irq_config = 0x43;
259
260 if ((dev->vendor == PCI_VENDOR_ID_PLX) &&
261 (dev->device == PCI_DEVICE_ID_PLX_ROMULUS))
262 /*
263 * As the megawolf cards have the int pins active
264 * high, and have 2 UART chips, both ints must be
265 * enabled on the 9050. Also, the UARTS are set in
266 * 16450 mode by default, so we have to enable the
267 * 16C950 'enhanced' mode so that we can use the
268 * deep FIFOs
269 */
270 irq_config = 0x5b;
271 /*
272 * enable/disable interrupts
273 */
274 p = ioremap_nocache(pci_resource_start(dev, 0), 0x80);
275 if (p == NULL)
276 return -ENOMEM;
277 writel(irq_config, p + 0x4c);
278
279 /*
280 * Read the register back to ensure that it took effect.
281 */
282 readl(p + 0x4c);
283 iounmap(p);
284
285 return 0;
286}
287
288static void __devexit pci_plx9050_exit(struct pci_dev *dev)
289{
290 u8 __iomem *p;
291
292 if ((pci_resource_flags(dev, 0) & IORESOURCE_MEM) == 0)
293 return;
294
295 /*
296 * disable interrupts
297 */
298 p = ioremap_nocache(pci_resource_start(dev, 0), 0x80);
299 if (p != NULL) {
300 writel(0, p + 0x4c);
301
302 /*
303 * Read the register back to ensure that it took effect.
304 */
305 readl(p + 0x4c);
306 iounmap(p);
307 }
308}
309
310#define NI8420_INT_ENABLE_REG 0x38
311#define NI8420_INT_ENABLE_BIT 0x2000
312
313static void __devexit pci_ni8420_exit(struct pci_dev *dev)
314{
315 void __iomem *p;
316 unsigned long base, len;
317 unsigned int bar = 0;
318
319 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
320 moan_device("no memory in bar", dev);
321 return;
322 }
323
324 base = pci_resource_start(dev, bar);
325 len = pci_resource_len(dev, bar);
326 p = ioremap_nocache(base, len);
327 if (p == NULL)
328 return;
329
330 /* Disable the CPU Interrupt */
331 writel(readl(p + NI8420_INT_ENABLE_REG) & ~(NI8420_INT_ENABLE_BIT),
332 p + NI8420_INT_ENABLE_REG);
333 iounmap(p);
334}
335
336
337/* MITE registers */
338#define MITE_IOWBSR1 0xc4
339#define MITE_IOWCR1 0xf4
340#define MITE_LCIMR1 0x08
341#define MITE_LCIMR2 0x10
342
343#define MITE_LCIMR2_CLR_CPU_IE (1 << 30)
344
345static void __devexit pci_ni8430_exit(struct pci_dev *dev)
346{
347 void __iomem *p;
348 unsigned long base, len;
349 unsigned int bar = 0;
350
351 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
352 moan_device("no memory in bar", dev);
353 return;
354 }
355
356 base = pci_resource_start(dev, bar);
357 len = pci_resource_len(dev, bar);
358 p = ioremap_nocache(base, len);
359 if (p == NULL)
360 return;
361
362 /* Disable the CPU Interrupt */
363 writel(MITE_LCIMR2_CLR_CPU_IE, p + MITE_LCIMR2);
364 iounmap(p);
365}
366
367/* SBS Technologies Inc. PMC-OCTPRO and P-OCTAL cards */
368static int
369sbs_setup(struct serial_private *priv, const struct pciserial_board *board,
370 struct uart_port *port, int idx)
371{
372 unsigned int bar, offset = board->first_offset;
373
374 bar = 0;
375
376 if (idx < 4) {
377 /* first four channels map to 0, 0x100, 0x200, 0x300 */
378 offset += idx * board->uart_offset;
379 } else if (idx < 8) {
380 /* last four channels map to 0x1000, 0x1100, 0x1200, 0x1300 */
381 offset += idx * board->uart_offset + 0xC00;
382 } else /* we have only 8 ports on PMC-OCTALPRO */
383 return 1;
384
385 return setup_port(priv, port, bar, offset, board->reg_shift);
386}
387
388/*
389* This does initialization for PMC OCTALPRO cards:
390* maps the device memory, resets the UARTs (needed, bc
391* if the module is removed and inserted again, the card
392* is in the sleep mode) and enables global interrupt.
393*/
394
395/* global control register offset for SBS PMC-OctalPro */
396#define OCT_REG_CR_OFF 0x500
397
398static int sbs_init(struct pci_dev *dev)
399{
400 u8 __iomem *p;
401
402 p = pci_ioremap_bar(dev, 0);
403
404 if (p == NULL)
405 return -ENOMEM;
406 /* Set bit-4 Control Register (UART RESET) in to reset the uarts */
407 writeb(0x10, p + OCT_REG_CR_OFF);
408 udelay(50);
409 writeb(0x0, p + OCT_REG_CR_OFF);
410
411 /* Set bit-2 (INTENABLE) of Control Register */
412 writeb(0x4, p + OCT_REG_CR_OFF);
413 iounmap(p);
414
415 return 0;
416}
417
418/*
419 * Disables the global interrupt of PMC-OctalPro
420 */
421
422static void __devexit sbs_exit(struct pci_dev *dev)
423{
424 u8 __iomem *p;
425
426 p = pci_ioremap_bar(dev, 0);
427 /* FIXME: What if resource_len < OCT_REG_CR_OFF */
428 if (p != NULL)
429 writeb(0, p + OCT_REG_CR_OFF);
430 iounmap(p);
431}
432
433/*
434 * SIIG serial cards have an PCI interface chip which also controls
435 * the UART clocking frequency. Each UART can be clocked independently
436 * (except cards equiped with 4 UARTs) and initial clocking settings
437 * are stored in the EEPROM chip. It can cause problems because this
438 * version of serial driver doesn't support differently clocked UART's
439 * on single PCI card. To prevent this, initialization functions set
440 * high frequency clocking for all UART's on given card. It is safe (I
441 * hope) because it doesn't touch EEPROM settings to prevent conflicts
442 * with other OSes (like M$ DOS).
443 *
444 * SIIG support added by Andrey Panin <pazke@donpac.ru>, 10/1999
445 *
446 * There is two family of SIIG serial cards with different PCI
447 * interface chip and different configuration methods:
448 * - 10x cards have control registers in IO and/or memory space;
449 * - 20x cards have control registers in standard PCI configuration space.
450 *
451 * Note: all 10x cards have PCI device ids 0x10..
452 * all 20x cards have PCI device ids 0x20..
453 *
454 * There are also Quartet Serial cards which use Oxford Semiconductor
455 * 16954 quad UART PCI chip clocked by 18.432 MHz quartz.
456 *
457 * Note: some SIIG cards are probed by the parport_serial object.
458 */
459
460#define PCI_DEVICE_ID_SIIG_1S_10x (PCI_DEVICE_ID_SIIG_1S_10x_550 & 0xfffc)
461#define PCI_DEVICE_ID_SIIG_2S_10x (PCI_DEVICE_ID_SIIG_2S_10x_550 & 0xfff8)
462
463static int pci_siig10x_init(struct pci_dev *dev)
464{
465 u16 data;
466 void __iomem *p;
467
468 switch (dev->device & 0xfff8) {
469 case PCI_DEVICE_ID_SIIG_1S_10x: /* 1S */
470 data = 0xffdf;
471 break;
472 case PCI_DEVICE_ID_SIIG_2S_10x: /* 2S, 2S1P */
473 data = 0xf7ff;
474 break;
475 default: /* 1S1P, 4S */
476 data = 0xfffb;
477 break;
478 }
479
480 p = ioremap_nocache(pci_resource_start(dev, 0), 0x80);
481 if (p == NULL)
482 return -ENOMEM;
483
484 writew(readw(p + 0x28) & data, p + 0x28);
485 readw(p + 0x28);
486 iounmap(p);
487 return 0;
488}
489
490#define PCI_DEVICE_ID_SIIG_2S_20x (PCI_DEVICE_ID_SIIG_2S_20x_550 & 0xfffc)
491#define PCI_DEVICE_ID_SIIG_2S1P_20x (PCI_DEVICE_ID_SIIG_2S1P_20x_550 & 0xfffc)
492
493static int pci_siig20x_init(struct pci_dev *dev)
494{
495 u8 data;
496
497 /* Change clock frequency for the first UART. */
498 pci_read_config_byte(dev, 0x6f, &data);
499 pci_write_config_byte(dev, 0x6f, data & 0xef);
500
501 /* If this card has 2 UART, we have to do the same with second UART. */
502 if (((dev->device & 0xfffc) == PCI_DEVICE_ID_SIIG_2S_20x) ||
503 ((dev->device & 0xfffc) == PCI_DEVICE_ID_SIIG_2S1P_20x)) {
504 pci_read_config_byte(dev, 0x73, &data);
505 pci_write_config_byte(dev, 0x73, data & 0xef);
506 }
507 return 0;
508}
509
510static int pci_siig_init(struct pci_dev *dev)
511{
512 unsigned int type = dev->device & 0xff00;
513
514 if (type == 0x1000)
515 return pci_siig10x_init(dev);
516 else if (type == 0x2000)
517 return pci_siig20x_init(dev);
518
519 moan_device("Unknown SIIG card", dev);
520 return -ENODEV;
521}
522
523static int pci_siig_setup(struct serial_private *priv,
524 const struct pciserial_board *board,
525 struct uart_port *port, int idx)
526{
527 unsigned int bar = FL_GET_BASE(board->flags) + idx, offset = 0;
528
529 if (idx > 3) {
530 bar = 4;
531 offset = (idx - 4) * 8;
532 }
533
534 return setup_port(priv, port, bar, offset, 0);
535}
536
537/*
538 * Timedia has an explosion of boards, and to avoid the PCI table from
539 * growing *huge*, we use this function to collapse some 70 entries
540 * in the PCI table into one, for sanity's and compactness's sake.
541 */
542static const unsigned short timedia_single_port[] = {
543 0x4025, 0x4027, 0x4028, 0x5025, 0x5027, 0
544};
545
546static const unsigned short timedia_dual_port[] = {
547 0x0002, 0x4036, 0x4037, 0x4038, 0x4078, 0x4079, 0x4085,
548 0x4088, 0x4089, 0x5037, 0x5078, 0x5079, 0x5085, 0x6079,
549 0x7079, 0x8079, 0x8137, 0x8138, 0x8237, 0x8238, 0x9079,
550 0x9137, 0x9138, 0x9237, 0x9238, 0xA079, 0xB079, 0xC079,
551 0xD079, 0
552};
553
554static const unsigned short timedia_quad_port[] = {
555 0x4055, 0x4056, 0x4095, 0x4096, 0x5056, 0x8156, 0x8157,
556 0x8256, 0x8257, 0x9056, 0x9156, 0x9157, 0x9158, 0x9159,
557 0x9256, 0x9257, 0xA056, 0xA157, 0xA158, 0xA159, 0xB056,
558 0xB157, 0
559};
560
561static const unsigned short timedia_eight_port[] = {
562 0x4065, 0x4066, 0x5065, 0x5066, 0x8166, 0x9066, 0x9166,
563 0x9167, 0x9168, 0xA066, 0xA167, 0xA168, 0
564};
565
566static const struct timedia_struct {
567 int num;
568 const unsigned short *ids;
569} timedia_data[] = {
570 { 1, timedia_single_port },
571 { 2, timedia_dual_port },
572 { 4, timedia_quad_port },
573 { 8, timedia_eight_port }
574};
575
576static int pci_timedia_init(struct pci_dev *dev)
577{
578 const unsigned short *ids;
579 int i, j;
580
581 for (i = 0; i < ARRAY_SIZE(timedia_data); i++) {
582 ids = timedia_data[i].ids;
583 for (j = 0; ids[j]; j++)
584 if (dev->subsystem_device == ids[j])
585 return timedia_data[i].num;
586 }
587 return 0;
588}
589
590/*
591 * Timedia/SUNIX uses a mixture of BARs and offsets
592 * Ugh, this is ugly as all hell --- TYT
593 */
594static int
595pci_timedia_setup(struct serial_private *priv,
596 const struct pciserial_board *board,
597 struct uart_port *port, int idx)
598{
599 unsigned int bar = 0, offset = board->first_offset;
600
601 switch (idx) {
602 case 0:
603 bar = 0;
604 break;
605 case 1:
606 offset = board->uart_offset;
607 bar = 0;
608 break;
609 case 2:
610 bar = 1;
611 break;
612 case 3:
613 offset = board->uart_offset;
614 /* FALLTHROUGH */
615 case 4: /* BAR 2 */
616 case 5: /* BAR 3 */
617 case 6: /* BAR 4 */
618 case 7: /* BAR 5 */
619 bar = idx - 2;
620 }
621
622 return setup_port(priv, port, bar, offset, board->reg_shift);
623}
624
625/*
626 * Some Titan cards are also a little weird
627 */
628static int
629titan_400l_800l_setup(struct serial_private *priv,
630 const struct pciserial_board *board,
631 struct uart_port *port, int idx)
632{
633 unsigned int bar, offset = board->first_offset;
634
635 switch (idx) {
636 case 0:
637 bar = 1;
638 break;
639 case 1:
640 bar = 2;
641 break;
642 default:
643 bar = 4;
644 offset = (idx - 2) * board->uart_offset;
645 }
646
647 return setup_port(priv, port, bar, offset, board->reg_shift);
648}
649
650static int pci_xircom_init(struct pci_dev *dev)
651{
652 msleep(100);
653 return 0;
654}
655
656static int pci_ni8420_init(struct pci_dev *dev)
657{
658 void __iomem *p;
659 unsigned long base, len;
660 unsigned int bar = 0;
661
662 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
663 moan_device("no memory in bar", dev);
664 return 0;
665 }
666
667 base = pci_resource_start(dev, bar);
668 len = pci_resource_len(dev, bar);
669 p = ioremap_nocache(base, len);
670 if (p == NULL)
671 return -ENOMEM;
672
673 /* Enable CPU Interrupt */
674 writel(readl(p + NI8420_INT_ENABLE_REG) | NI8420_INT_ENABLE_BIT,
675 p + NI8420_INT_ENABLE_REG);
676
677 iounmap(p);
678 return 0;
679}
680
681#define MITE_IOWBSR1_WSIZE 0xa
682#define MITE_IOWBSR1_WIN_OFFSET 0x800
683#define MITE_IOWBSR1_WENAB (1 << 7)
684#define MITE_LCIMR1_IO_IE_0 (1 << 24)
685#define MITE_LCIMR2_SET_CPU_IE (1 << 31)
686#define MITE_IOWCR1_RAMSEL_MASK 0xfffffffe
687
688static int pci_ni8430_init(struct pci_dev *dev)
689{
690 void __iomem *p;
691 unsigned long base, len;
692 u32 device_window;
693 unsigned int bar = 0;
694
695 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
696 moan_device("no memory in bar", dev);
697 return 0;
698 }
699
700 base = pci_resource_start(dev, bar);
701 len = pci_resource_len(dev, bar);
702 p = ioremap_nocache(base, len);
703 if (p == NULL)
704 return -ENOMEM;
705
706 /* Set device window address and size in BAR0 */
707 device_window = ((base + MITE_IOWBSR1_WIN_OFFSET) & 0xffffff00)
708 | MITE_IOWBSR1_WENAB | MITE_IOWBSR1_WSIZE;
709 writel(device_window, p + MITE_IOWBSR1);
710
711 /* Set window access to go to RAMSEL IO address space */
712 writel((readl(p + MITE_IOWCR1) & MITE_IOWCR1_RAMSEL_MASK),
713 p + MITE_IOWCR1);
714
715 /* Enable IO Bus Interrupt 0 */
716 writel(MITE_LCIMR1_IO_IE_0, p + MITE_LCIMR1);
717
718 /* Enable CPU Interrupt */
719 writel(MITE_LCIMR2_SET_CPU_IE, p + MITE_LCIMR2);
720
721 iounmap(p);
722 return 0;
723}
724
725/* UART Port Control Register */
726#define NI8430_PORTCON 0x0f
727#define NI8430_PORTCON_TXVR_ENABLE (1 << 3)
728
729static int
730pci_ni8430_setup(struct serial_private *priv,
731 const struct pciserial_board *board,
732 struct uart_port *port, int idx)
733{
734 void __iomem *p;
735 unsigned long base, len;
736 unsigned int bar, offset = board->first_offset;
737
738 if (idx >= board->num_ports)
739 return 1;
740
741 bar = FL_GET_BASE(board->flags);
742 offset += idx * board->uart_offset;
743
744 base = pci_resource_start(priv->dev, bar);
745 len = pci_resource_len(priv->dev, bar);
746 p = ioremap_nocache(base, len);
747
748 /* enable the transciever */
749 writeb(readb(p + offset + NI8430_PORTCON) | NI8430_PORTCON_TXVR_ENABLE,
750 p + offset + NI8430_PORTCON);
751
752 iounmap(p);
753
754 return setup_port(priv, port, bar, offset, board->reg_shift);
755}
756
757
758static int pci_netmos_init(struct pci_dev *dev)
759{
760 /* subdevice 0x00PS means <P> parallel, <S> serial */
761 unsigned int num_serial = dev->subsystem_device & 0xf;
762
763 if ((dev->device == PCI_DEVICE_ID_NETMOS_9901) ||
764 (dev->device == PCI_DEVICE_ID_NETMOS_9865))
765 return 0;
766 if (dev->subsystem_vendor == PCI_VENDOR_ID_IBM &&
767 dev->subsystem_device == 0x0299)
768 return 0;
769
770 if (num_serial == 0)
771 return -ENODEV;
772 return num_serial;
773}
774
775/*
776 * These chips are available with optionally one parallel port and up to
777 * two serial ports. Unfortunately they all have the same product id.
778 *
779 * Basic configuration is done over a region of 32 I/O ports. The base
780 * ioport is called INTA or INTC, depending on docs/other drivers.
781 *
782 * The region of the 32 I/O ports is configured in POSIO0R...
783 */
784
785/* registers */
786#define ITE_887x_MISCR 0x9c
787#define ITE_887x_INTCBAR 0x78
788#define ITE_887x_UARTBAR 0x7c
789#define ITE_887x_PS0BAR 0x10
790#define ITE_887x_POSIO0 0x60
791
792/* I/O space size */
793#define ITE_887x_IOSIZE 32
794/* I/O space size (bits 26-24; 8 bytes = 011b) */
795#define ITE_887x_POSIO_IOSIZE_8 (3 << 24)
796/* I/O space size (bits 26-24; 32 bytes = 101b) */
797#define ITE_887x_POSIO_IOSIZE_32 (5 << 24)
798/* Decoding speed (1 = slow, 2 = medium, 3 = fast) */
799#define ITE_887x_POSIO_SPEED (3 << 29)
800/* enable IO_Space bit */
801#define ITE_887x_POSIO_ENABLE (1 << 31)
802
803static int pci_ite887x_init(struct pci_dev *dev)
804{
805 /* inta_addr are the configuration addresses of the ITE */
806 static const short inta_addr[] = { 0x2a0, 0x2c0, 0x220, 0x240, 0x1e0,
807 0x200, 0x280, 0 };
808 int ret, i, type;
809 struct resource *iobase = NULL;
810 u32 miscr, uartbar, ioport;
811
812 /* search for the base-ioport */
813 i = 0;
814 while (inta_addr[i] && iobase == NULL) {
815 iobase = request_region(inta_addr[i], ITE_887x_IOSIZE,
816 "ite887x");
817 if (iobase != NULL) {
818 /* write POSIO0R - speed | size | ioport */
819 pci_write_config_dword(dev, ITE_887x_POSIO0,
820 ITE_887x_POSIO_ENABLE | ITE_887x_POSIO_SPEED |
821 ITE_887x_POSIO_IOSIZE_32 | inta_addr[i]);
822 /* write INTCBAR - ioport */
823 pci_write_config_dword(dev, ITE_887x_INTCBAR,
824 inta_addr[i]);
825 ret = inb(inta_addr[i]);
826 if (ret != 0xff) {
827 /* ioport connected */
828 break;
829 }
830 release_region(iobase->start, ITE_887x_IOSIZE);
831 iobase = NULL;
832 }
833 i++;
834 }
835
836 if (!inta_addr[i]) {
837 printk(KERN_ERR "ite887x: could not find iobase\n");
838 return -ENODEV;
839 }
840
841 /* start of undocumented type checking (see parport_pc.c) */
842 type = inb(iobase->start + 0x18) & 0x0f;
843
844 switch (type) {
845 case 0x2: /* ITE8871 (1P) */
846 case 0xa: /* ITE8875 (1P) */
847 ret = 0;
848 break;
849 case 0xe: /* ITE8872 (2S1P) */
850 ret = 2;
851 break;
852 case 0x6: /* ITE8873 (1S) */
853 ret = 1;
854 break;
855 case 0x8: /* ITE8874 (2S) */
856 ret = 2;
857 break;
858 default:
859 moan_device("Unknown ITE887x", dev);
860 ret = -ENODEV;
861 }
862
863 /* configure all serial ports */
864 for (i = 0; i < ret; i++) {
865 /* read the I/O port from the device */
866 pci_read_config_dword(dev, ITE_887x_PS0BAR + (0x4 * (i + 1)),
867 &ioport);
868 ioport &= 0x0000FF00; /* the actual base address */
869 pci_write_config_dword(dev, ITE_887x_POSIO0 + (0x4 * (i + 1)),
870 ITE_887x_POSIO_ENABLE | ITE_887x_POSIO_SPEED |
871 ITE_887x_POSIO_IOSIZE_8 | ioport);
872
873 /* write the ioport to the UARTBAR */
874 pci_read_config_dword(dev, ITE_887x_UARTBAR, &uartbar);
875 uartbar &= ~(0xffff << (16 * i)); /* clear half the reg */
876 uartbar |= (ioport << (16 * i)); /* set the ioport */
877 pci_write_config_dword(dev, ITE_887x_UARTBAR, uartbar);
878
879 /* get current config */
880 pci_read_config_dword(dev, ITE_887x_MISCR, &miscr);
881 /* disable interrupts (UARTx_Routing[3:0]) */
882 miscr &= ~(0xf << (12 - 4 * i));
883 /* activate the UART (UARTx_En) */
884 miscr |= 1 << (23 - i);
885 /* write new config with activated UART */
886 pci_write_config_dword(dev, ITE_887x_MISCR, miscr);
887 }
888
889 if (ret <= 0) {
890 /* the device has no UARTs if we get here */
891 release_region(iobase->start, ITE_887x_IOSIZE);
892 }
893
894 return ret;
895}
896
897static void __devexit pci_ite887x_exit(struct pci_dev *dev)
898{
899 u32 ioport;
900 /* the ioport is bit 0-15 in POSIO0R */
901 pci_read_config_dword(dev, ITE_887x_POSIO0, &ioport);
902 ioport &= 0xffff;
903 release_region(ioport, ITE_887x_IOSIZE);
904}
905
906/*
907 * Oxford Semiconductor Inc.
908 * Check that device is part of the Tornado range of devices, then determine
909 * the number of ports available on the device.
910 */
911static int pci_oxsemi_tornado_init(struct pci_dev *dev)
912{
913 u8 __iomem *p;
914 unsigned long deviceID;
915 unsigned int number_uarts = 0;
916
917 /* OxSemi Tornado devices are all 0xCxxx */
918 if (dev->vendor == PCI_VENDOR_ID_OXSEMI &&
919 (dev->device & 0xF000) != 0xC000)
920 return 0;
921
922 p = pci_iomap(dev, 0, 5);
923 if (p == NULL)
924 return -ENOMEM;
925
926 deviceID = ioread32(p);
927 /* Tornado device */
928 if (deviceID == 0x07000200) {
929 number_uarts = ioread8(p + 4);
930 printk(KERN_DEBUG
931 "%d ports detected on Oxford PCI Express device\n",
932 number_uarts);
933 }
934 pci_iounmap(dev, p);
935 return number_uarts;
936}
937
938static int
939pci_default_setup(struct serial_private *priv,
940 const struct pciserial_board *board,
941 struct uart_port *port, int idx)
942{
943 unsigned int bar, offset = board->first_offset, maxnr;
944
945 bar = FL_GET_BASE(board->flags);
946 if (board->flags & FL_BASE_BARS)
947 bar += idx;
948 else
949 offset += idx * board->uart_offset;
950
951 maxnr = (pci_resource_len(priv->dev, bar) - board->first_offset) >>
952 (board->reg_shift + 3);
953
954 if (board->flags & FL_REGION_SZ_CAP && idx >= maxnr)
955 return 1;
956
957 return setup_port(priv, port, bar, offset, board->reg_shift);
958}
959
960static int
961ce4100_serial_setup(struct serial_private *priv,
962 const struct pciserial_board *board,
963 struct uart_port *port, int idx)
964{
965 int ret;
966
967 ret = setup_port(priv, port, 0, 0, board->reg_shift);
968 port->iotype = UPIO_MEM32;
969 port->type = PORT_XSCALE;
970 port->flags = (port->flags | UPF_FIXED_PORT | UPF_FIXED_TYPE);
971 port->regshift = 2;
972
973 return ret;
974}
975
976static int skip_tx_en_setup(struct serial_private *priv,
977 const struct pciserial_board *board,
978 struct uart_port *port, int idx)
979{
980 port->flags |= UPF_NO_TXEN_TEST;
981 printk(KERN_DEBUG "serial8250: skipping TxEn test for device "
982 "[%04x:%04x] subsystem [%04x:%04x]\n",
983 priv->dev->vendor,
984 priv->dev->device,
985 priv->dev->subsystem_vendor,
986 priv->dev->subsystem_device);
987
988 return pci_default_setup(priv, board, port, idx);
989}
990
991/* This should be in linux/pci_ids.h */
992#define PCI_VENDOR_ID_SBSMODULARIO 0x124B
993#define PCI_SUBVENDOR_ID_SBSMODULARIO 0x124B
994#define PCI_DEVICE_ID_OCTPRO 0x0001
995#define PCI_SUBDEVICE_ID_OCTPRO232 0x0108
996#define PCI_SUBDEVICE_ID_OCTPRO422 0x0208
997#define PCI_SUBDEVICE_ID_POCTAL232 0x0308
998#define PCI_SUBDEVICE_ID_POCTAL422 0x0408
999#define PCI_VENDOR_ID_ADVANTECH 0x13fe
1000#define PCI_DEVICE_ID_INTEL_CE4100_UART 0x2e66
1001#define PCI_DEVICE_ID_ADVANTECH_PCI3620 0x3620
1002#define PCI_DEVICE_ID_TITAN_200I 0x8028
1003#define PCI_DEVICE_ID_TITAN_400I 0x8048
1004#define PCI_DEVICE_ID_TITAN_800I 0x8088
1005#define PCI_DEVICE_ID_TITAN_800EH 0xA007
1006#define PCI_DEVICE_ID_TITAN_800EHB 0xA008
1007#define PCI_DEVICE_ID_TITAN_400EH 0xA009
1008#define PCI_DEVICE_ID_TITAN_100E 0xA010
1009#define PCI_DEVICE_ID_TITAN_200E 0xA012
1010#define PCI_DEVICE_ID_TITAN_400E 0xA013
1011#define PCI_DEVICE_ID_TITAN_800E 0xA014
1012#define PCI_DEVICE_ID_TITAN_200EI 0xA016
1013#define PCI_DEVICE_ID_TITAN_200EISI 0xA017
1014#define PCI_DEVICE_ID_OXSEMI_16PCI958 0x9538
1015
1016/* Unknown vendors/cards - this should not be in linux/pci_ids.h */
1017#define PCI_SUBDEVICE_ID_UNKNOWN_0x1584 0x1584
1018
1019/*
1020 * Master list of serial port init/setup/exit quirks.
1021 * This does not describe the general nature of the port.
1022 * (ie, baud base, number and location of ports, etc)
1023 *
1024 * This list is ordered alphabetically by vendor then device.
1025 * Specific entries must come before more generic entries.
1026 */
1027static struct pci_serial_quirk pci_serial_quirks[] __refdata = {
1028 /*
1029 * ADDI-DATA GmbH communication cards <info@addi-data.com>
1030 */
1031 {
1032 .vendor = PCI_VENDOR_ID_ADDIDATA_OLD,
1033 .device = PCI_DEVICE_ID_ADDIDATA_APCI7800,
1034 .subvendor = PCI_ANY_ID,
1035 .subdevice = PCI_ANY_ID,
1036 .setup = addidata_apci7800_setup,
1037 },
1038 /*
1039 * AFAVLAB cards - these may be called via parport_serial
1040 * It is not clear whether this applies to all products.
1041 */
1042 {
1043 .vendor = PCI_VENDOR_ID_AFAVLAB,
1044 .device = PCI_ANY_ID,
1045 .subvendor = PCI_ANY_ID,
1046 .subdevice = PCI_ANY_ID,
1047 .setup = afavlab_setup,
1048 },
1049 /*
1050 * HP Diva
1051 */
1052 {
1053 .vendor = PCI_VENDOR_ID_HP,
1054 .device = PCI_DEVICE_ID_HP_DIVA,
1055 .subvendor = PCI_ANY_ID,
1056 .subdevice = PCI_ANY_ID,
1057 .init = pci_hp_diva_init,
1058 .setup = pci_hp_diva_setup,
1059 },
1060 /*
1061 * Intel
1062 */
1063 {
1064 .vendor = PCI_VENDOR_ID_INTEL,
1065 .device = PCI_DEVICE_ID_INTEL_80960_RP,
1066 .subvendor = 0xe4bf,
1067 .subdevice = PCI_ANY_ID,
1068 .init = pci_inteli960ni_init,
1069 .setup = pci_default_setup,
1070 },
1071 {
1072 .vendor = PCI_VENDOR_ID_INTEL,
1073 .device = PCI_DEVICE_ID_INTEL_8257X_SOL,
1074 .subvendor = PCI_ANY_ID,
1075 .subdevice = PCI_ANY_ID,
1076 .setup = skip_tx_en_setup,
1077 },
1078 {
1079 .vendor = PCI_VENDOR_ID_INTEL,
1080 .device = PCI_DEVICE_ID_INTEL_82573L_SOL,
1081 .subvendor = PCI_ANY_ID,
1082 .subdevice = PCI_ANY_ID,
1083 .setup = skip_tx_en_setup,
1084 },
1085 {
1086 .vendor = PCI_VENDOR_ID_INTEL,
1087 .device = PCI_DEVICE_ID_INTEL_82573E_SOL,
1088 .subvendor = PCI_ANY_ID,
1089 .subdevice = PCI_ANY_ID,
1090 .setup = skip_tx_en_setup,
1091 },
1092 {
1093 .vendor = PCI_VENDOR_ID_INTEL,
1094 .device = PCI_DEVICE_ID_INTEL_CE4100_UART,
1095 .subvendor = PCI_ANY_ID,
1096 .subdevice = PCI_ANY_ID,
1097 .setup = ce4100_serial_setup,
1098 },
1099 /*
1100 * ITE
1101 */
1102 {
1103 .vendor = PCI_VENDOR_ID_ITE,
1104 .device = PCI_DEVICE_ID_ITE_8872,
1105 .subvendor = PCI_ANY_ID,
1106 .subdevice = PCI_ANY_ID,
1107 .init = pci_ite887x_init,
1108 .setup = pci_default_setup,
1109 .exit = __devexit_p(pci_ite887x_exit),
1110 },
1111 /*
1112 * National Instruments
1113 */
1114 {
1115 .vendor = PCI_VENDOR_ID_NI,
1116 .device = PCI_DEVICE_ID_NI_PCI23216,
1117 .subvendor = PCI_ANY_ID,
1118 .subdevice = PCI_ANY_ID,
1119 .init = pci_ni8420_init,
1120 .setup = pci_default_setup,
1121 .exit = __devexit_p(pci_ni8420_exit),
1122 },
1123 {
1124 .vendor = PCI_VENDOR_ID_NI,
1125 .device = PCI_DEVICE_ID_NI_PCI2328,
1126 .subvendor = PCI_ANY_ID,
1127 .subdevice = PCI_ANY_ID,
1128 .init = pci_ni8420_init,
1129 .setup = pci_default_setup,
1130 .exit = __devexit_p(pci_ni8420_exit),
1131 },
1132 {
1133 .vendor = PCI_VENDOR_ID_NI,
1134 .device = PCI_DEVICE_ID_NI_PCI2324,
1135 .subvendor = PCI_ANY_ID,
1136 .subdevice = PCI_ANY_ID,
1137 .init = pci_ni8420_init,
1138 .setup = pci_default_setup,
1139 .exit = __devexit_p(pci_ni8420_exit),
1140 },
1141 {
1142 .vendor = PCI_VENDOR_ID_NI,
1143 .device = PCI_DEVICE_ID_NI_PCI2322,
1144 .subvendor = PCI_ANY_ID,
1145 .subdevice = PCI_ANY_ID,
1146 .init = pci_ni8420_init,
1147 .setup = pci_default_setup,
1148 .exit = __devexit_p(pci_ni8420_exit),
1149 },
1150 {
1151 .vendor = PCI_VENDOR_ID_NI,
1152 .device = PCI_DEVICE_ID_NI_PCI2324I,
1153 .subvendor = PCI_ANY_ID,
1154 .subdevice = PCI_ANY_ID,
1155 .init = pci_ni8420_init,
1156 .setup = pci_default_setup,
1157 .exit = __devexit_p(pci_ni8420_exit),
1158 },
1159 {
1160 .vendor = PCI_VENDOR_ID_NI,
1161 .device = PCI_DEVICE_ID_NI_PCI2322I,
1162 .subvendor = PCI_ANY_ID,
1163 .subdevice = PCI_ANY_ID,
1164 .init = pci_ni8420_init,
1165 .setup = pci_default_setup,
1166 .exit = __devexit_p(pci_ni8420_exit),
1167 },
1168 {
1169 .vendor = PCI_VENDOR_ID_NI,
1170 .device = PCI_DEVICE_ID_NI_PXI8420_23216,
1171 .subvendor = PCI_ANY_ID,
1172 .subdevice = PCI_ANY_ID,
1173 .init = pci_ni8420_init,
1174 .setup = pci_default_setup,
1175 .exit = __devexit_p(pci_ni8420_exit),
1176 },
1177 {
1178 .vendor = PCI_VENDOR_ID_NI,
1179 .device = PCI_DEVICE_ID_NI_PXI8420_2328,
1180 .subvendor = PCI_ANY_ID,
1181 .subdevice = PCI_ANY_ID,
1182 .init = pci_ni8420_init,
1183 .setup = pci_default_setup,
1184 .exit = __devexit_p(pci_ni8420_exit),
1185 },
1186 {
1187 .vendor = PCI_VENDOR_ID_NI,
1188 .device = PCI_DEVICE_ID_NI_PXI8420_2324,
1189 .subvendor = PCI_ANY_ID,
1190 .subdevice = PCI_ANY_ID,
1191 .init = pci_ni8420_init,
1192 .setup = pci_default_setup,
1193 .exit = __devexit_p(pci_ni8420_exit),
1194 },
1195 {
1196 .vendor = PCI_VENDOR_ID_NI,
1197 .device = PCI_DEVICE_ID_NI_PXI8420_2322,
1198 .subvendor = PCI_ANY_ID,
1199 .subdevice = PCI_ANY_ID,
1200 .init = pci_ni8420_init,
1201 .setup = pci_default_setup,
1202 .exit = __devexit_p(pci_ni8420_exit),
1203 },
1204 {
1205 .vendor = PCI_VENDOR_ID_NI,
1206 .device = PCI_DEVICE_ID_NI_PXI8422_2324,
1207 .subvendor = PCI_ANY_ID,
1208 .subdevice = PCI_ANY_ID,
1209 .init = pci_ni8420_init,
1210 .setup = pci_default_setup,
1211 .exit = __devexit_p(pci_ni8420_exit),
1212 },
1213 {
1214 .vendor = PCI_VENDOR_ID_NI,
1215 .device = PCI_DEVICE_ID_NI_PXI8422_2322,
1216 .subvendor = PCI_ANY_ID,
1217 .subdevice = PCI_ANY_ID,
1218 .init = pci_ni8420_init,
1219 .setup = pci_default_setup,
1220 .exit = __devexit_p(pci_ni8420_exit),
1221 },
1222 {
1223 .vendor = PCI_VENDOR_ID_NI,
1224 .device = PCI_ANY_ID,
1225 .subvendor = PCI_ANY_ID,
1226 .subdevice = PCI_ANY_ID,
1227 .init = pci_ni8430_init,
1228 .setup = pci_ni8430_setup,
1229 .exit = __devexit_p(pci_ni8430_exit),
1230 },
1231 /*
1232 * Panacom
1233 */
1234 {
1235 .vendor = PCI_VENDOR_ID_PANACOM,
1236 .device = PCI_DEVICE_ID_PANACOM_QUADMODEM,
1237 .subvendor = PCI_ANY_ID,
1238 .subdevice = PCI_ANY_ID,
1239 .init = pci_plx9050_init,
1240 .setup = pci_default_setup,
1241 .exit = __devexit_p(pci_plx9050_exit),
1242 },
1243 {
1244 .vendor = PCI_VENDOR_ID_PANACOM,
1245 .device = PCI_DEVICE_ID_PANACOM_DUALMODEM,
1246 .subvendor = PCI_ANY_ID,
1247 .subdevice = PCI_ANY_ID,
1248 .init = pci_plx9050_init,
1249 .setup = pci_default_setup,
1250 .exit = __devexit_p(pci_plx9050_exit),
1251 },
1252 /*
1253 * PLX
1254 */
1255 {
1256 .vendor = PCI_VENDOR_ID_PLX,
1257 .device = PCI_DEVICE_ID_PLX_9030,
1258 .subvendor = PCI_SUBVENDOR_ID_PERLE,
1259 .subdevice = PCI_ANY_ID,
1260 .setup = pci_default_setup,
1261 },
1262 {
1263 .vendor = PCI_VENDOR_ID_PLX,
1264 .device = PCI_DEVICE_ID_PLX_9050,
1265 .subvendor = PCI_SUBVENDOR_ID_EXSYS,
1266 .subdevice = PCI_SUBDEVICE_ID_EXSYS_4055,
1267 .init = pci_plx9050_init,
1268 .setup = pci_default_setup,
1269 .exit = __devexit_p(pci_plx9050_exit),
1270 },
1271 {
1272 .vendor = PCI_VENDOR_ID_PLX,
1273 .device = PCI_DEVICE_ID_PLX_9050,
1274 .subvendor = PCI_SUBVENDOR_ID_KEYSPAN,
1275 .subdevice = PCI_SUBDEVICE_ID_KEYSPAN_SX2,
1276 .init = pci_plx9050_init,
1277 .setup = pci_default_setup,
1278 .exit = __devexit_p(pci_plx9050_exit),
1279 },
1280 {
1281 .vendor = PCI_VENDOR_ID_PLX,
1282 .device = PCI_DEVICE_ID_PLX_9050,
1283 .subvendor = PCI_VENDOR_ID_PLX,
1284 .subdevice = PCI_SUBDEVICE_ID_UNKNOWN_0x1584,
1285 .init = pci_plx9050_init,
1286 .setup = pci_default_setup,
1287 .exit = __devexit_p(pci_plx9050_exit),
1288 },
1289 {
1290 .vendor = PCI_VENDOR_ID_PLX,
1291 .device = PCI_DEVICE_ID_PLX_ROMULUS,
1292 .subvendor = PCI_VENDOR_ID_PLX,
1293 .subdevice = PCI_DEVICE_ID_PLX_ROMULUS,
1294 .init = pci_plx9050_init,
1295 .setup = pci_default_setup,
1296 .exit = __devexit_p(pci_plx9050_exit),
1297 },
1298 /*
1299 * SBS Technologies, Inc., PMC-OCTALPRO 232
1300 */
1301 {
1302 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1303 .device = PCI_DEVICE_ID_OCTPRO,
1304 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1305 .subdevice = PCI_SUBDEVICE_ID_OCTPRO232,
1306 .init = sbs_init,
1307 .setup = sbs_setup,
1308 .exit = __devexit_p(sbs_exit),
1309 },
1310 /*
1311 * SBS Technologies, Inc., PMC-OCTALPRO 422
1312 */
1313 {
1314 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1315 .device = PCI_DEVICE_ID_OCTPRO,
1316 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1317 .subdevice = PCI_SUBDEVICE_ID_OCTPRO422,
1318 .init = sbs_init,
1319 .setup = sbs_setup,
1320 .exit = __devexit_p(sbs_exit),
1321 },
1322 /*
1323 * SBS Technologies, Inc., P-Octal 232
1324 */
1325 {
1326 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1327 .device = PCI_DEVICE_ID_OCTPRO,
1328 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1329 .subdevice = PCI_SUBDEVICE_ID_POCTAL232,
1330 .init = sbs_init,
1331 .setup = sbs_setup,
1332 .exit = __devexit_p(sbs_exit),
1333 },
1334 /*
1335 * SBS Technologies, Inc., P-Octal 422
1336 */
1337 {
1338 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1339 .device = PCI_DEVICE_ID_OCTPRO,
1340 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1341 .subdevice = PCI_SUBDEVICE_ID_POCTAL422,
1342 .init = sbs_init,
1343 .setup = sbs_setup,
1344 .exit = __devexit_p(sbs_exit),
1345 },
1346 /*
1347 * SIIG cards - these may be called via parport_serial
1348 */
1349 {
1350 .vendor = PCI_VENDOR_ID_SIIG,
1351 .device = PCI_ANY_ID,
1352 .subvendor = PCI_ANY_ID,
1353 .subdevice = PCI_ANY_ID,
1354 .init = pci_siig_init,
1355 .setup = pci_siig_setup,
1356 },
1357 /*
1358 * Titan cards
1359 */
1360 {
1361 .vendor = PCI_VENDOR_ID_TITAN,
1362 .device = PCI_DEVICE_ID_TITAN_400L,
1363 .subvendor = PCI_ANY_ID,
1364 .subdevice = PCI_ANY_ID,
1365 .setup = titan_400l_800l_setup,
1366 },
1367 {
1368 .vendor = PCI_VENDOR_ID_TITAN,
1369 .device = PCI_DEVICE_ID_TITAN_800L,
1370 .subvendor = PCI_ANY_ID,
1371 .subdevice = PCI_ANY_ID,
1372 .setup = titan_400l_800l_setup,
1373 },
1374 /*
1375 * Timedia cards
1376 */
1377 {
1378 .vendor = PCI_VENDOR_ID_TIMEDIA,
1379 .device = PCI_DEVICE_ID_TIMEDIA_1889,
1380 .subvendor = PCI_VENDOR_ID_TIMEDIA,
1381 .subdevice = PCI_ANY_ID,
1382 .init = pci_timedia_init,
1383 .setup = pci_timedia_setup,
1384 },
1385 {
1386 .vendor = PCI_VENDOR_ID_TIMEDIA,
1387 .device = PCI_ANY_ID,
1388 .subvendor = PCI_ANY_ID,
1389 .subdevice = PCI_ANY_ID,
1390 .setup = pci_timedia_setup,
1391 },
1392 /*
1393 * Xircom cards
1394 */
1395 {
1396 .vendor = PCI_VENDOR_ID_XIRCOM,
1397 .device = PCI_DEVICE_ID_XIRCOM_X3201_MDM,
1398 .subvendor = PCI_ANY_ID,
1399 .subdevice = PCI_ANY_ID,
1400 .init = pci_xircom_init,
1401 .setup = pci_default_setup,
1402 },
1403 /*
1404 * Netmos cards - these may be called via parport_serial
1405 */
1406 {
1407 .vendor = PCI_VENDOR_ID_NETMOS,
1408 .device = PCI_ANY_ID,
1409 .subvendor = PCI_ANY_ID,
1410 .subdevice = PCI_ANY_ID,
1411 .init = pci_netmos_init,
1412 .setup = pci_default_setup,
1413 },
1414 /*
1415 * For Oxford Semiconductor and Mainpine
1416 */
1417 {
1418 .vendor = PCI_VENDOR_ID_OXSEMI,
1419 .device = PCI_ANY_ID,
1420 .subvendor = PCI_ANY_ID,
1421 .subdevice = PCI_ANY_ID,
1422 .init = pci_oxsemi_tornado_init,
1423 .setup = pci_default_setup,
1424 },
1425 {
1426 .vendor = PCI_VENDOR_ID_MAINPINE,
1427 .device = PCI_ANY_ID,
1428 .subvendor = PCI_ANY_ID,
1429 .subdevice = PCI_ANY_ID,
1430 .init = pci_oxsemi_tornado_init,
1431 .setup = pci_default_setup,
1432 },
1433 /*
1434 * Default "match everything" terminator entry
1435 */
1436 {
1437 .vendor = PCI_ANY_ID,
1438 .device = PCI_ANY_ID,
1439 .subvendor = PCI_ANY_ID,
1440 .subdevice = PCI_ANY_ID,
1441 .setup = pci_default_setup,
1442 }
1443};
1444
1445static inline int quirk_id_matches(u32 quirk_id, u32 dev_id)
1446{
1447 return quirk_id == PCI_ANY_ID || quirk_id == dev_id;
1448}
1449
1450static struct pci_serial_quirk *find_quirk(struct pci_dev *dev)
1451{
1452 struct pci_serial_quirk *quirk;
1453
1454 for (quirk = pci_serial_quirks; ; quirk++)
1455 if (quirk_id_matches(quirk->vendor, dev->vendor) &&
1456 quirk_id_matches(quirk->device, dev->device) &&
1457 quirk_id_matches(quirk->subvendor, dev->subsystem_vendor) &&
1458 quirk_id_matches(quirk->subdevice, dev->subsystem_device))
1459 break;
1460 return quirk;
1461}
1462
1463static inline int get_pci_irq(struct pci_dev *dev,
1464 const struct pciserial_board *board)
1465{
1466 if (board->flags & FL_NOIRQ)
1467 return 0;
1468 else
1469 return dev->irq;
1470}
1471
1472/*
1473 * This is the configuration table for all of the PCI serial boards
1474 * which we support. It is directly indexed by the pci_board_num_t enum
1475 * value, which is encoded in the pci_device_id PCI probe table's
1476 * driver_data member.
1477 *
1478 * The makeup of these names are:
1479 * pbn_bn{_bt}_n_baud{_offsetinhex}
1480 *
1481 * bn = PCI BAR number
1482 * bt = Index using PCI BARs
1483 * n = number of serial ports
1484 * baud = baud rate
1485 * offsetinhex = offset for each sequential port (in hex)
1486 *
1487 * This table is sorted by (in order): bn, bt, baud, offsetindex, n.
1488 *
1489 * Please note: in theory if n = 1, _bt infix should make no difference.
1490 * ie, pbn_b0_1_115200 is the same as pbn_b0_bt_1_115200
1491 */
1492enum pci_board_num_t {
1493 pbn_default = 0,
1494
1495 pbn_b0_1_115200,
1496 pbn_b0_2_115200,
1497 pbn_b0_4_115200,
1498 pbn_b0_5_115200,
1499 pbn_b0_8_115200,
1500
1501 pbn_b0_1_921600,
1502 pbn_b0_2_921600,
1503 pbn_b0_4_921600,
1504
1505 pbn_b0_2_1130000,
1506
1507 pbn_b0_4_1152000,
1508
1509 pbn_b0_2_1843200,
1510 pbn_b0_4_1843200,
1511
1512 pbn_b0_2_1843200_200,
1513 pbn_b0_4_1843200_200,
1514 pbn_b0_8_1843200_200,
1515
1516 pbn_b0_1_4000000,
1517
1518 pbn_b0_bt_1_115200,
1519 pbn_b0_bt_2_115200,
1520 pbn_b0_bt_4_115200,
1521 pbn_b0_bt_8_115200,
1522
1523 pbn_b0_bt_1_460800,
1524 pbn_b0_bt_2_460800,
1525 pbn_b0_bt_4_460800,
1526
1527 pbn_b0_bt_1_921600,
1528 pbn_b0_bt_2_921600,
1529 pbn_b0_bt_4_921600,
1530 pbn_b0_bt_8_921600,
1531
1532 pbn_b1_1_115200,
1533 pbn_b1_2_115200,
1534 pbn_b1_4_115200,
1535 pbn_b1_8_115200,
1536 pbn_b1_16_115200,
1537
1538 pbn_b1_1_921600,
1539 pbn_b1_2_921600,
1540 pbn_b1_4_921600,
1541 pbn_b1_8_921600,
1542
1543 pbn_b1_2_1250000,
1544
1545 pbn_b1_bt_1_115200,
1546 pbn_b1_bt_2_115200,
1547 pbn_b1_bt_4_115200,
1548
1549 pbn_b1_bt_2_921600,
1550
1551 pbn_b1_1_1382400,
1552 pbn_b1_2_1382400,
1553 pbn_b1_4_1382400,
1554 pbn_b1_8_1382400,
1555
1556 pbn_b2_1_115200,
1557 pbn_b2_2_115200,
1558 pbn_b2_4_115200,
1559 pbn_b2_8_115200,
1560
1561 pbn_b2_1_460800,
1562 pbn_b2_4_460800,
1563 pbn_b2_8_460800,
1564 pbn_b2_16_460800,
1565
1566 pbn_b2_1_921600,
1567 pbn_b2_4_921600,
1568 pbn_b2_8_921600,
1569
1570 pbn_b2_8_1152000,
1571
1572 pbn_b2_bt_1_115200,
1573 pbn_b2_bt_2_115200,
1574 pbn_b2_bt_4_115200,
1575
1576 pbn_b2_bt_2_921600,
1577 pbn_b2_bt_4_921600,
1578
1579 pbn_b3_2_115200,
1580 pbn_b3_4_115200,
1581 pbn_b3_8_115200,
1582
1583 pbn_b4_bt_2_921600,
1584 pbn_b4_bt_4_921600,
1585 pbn_b4_bt_8_921600,
1586
1587 /*
1588 * Board-specific versions.
1589 */
1590 pbn_panacom,
1591 pbn_panacom2,
1592 pbn_panacom4,
1593 pbn_exsys_4055,
1594 pbn_plx_romulus,
1595 pbn_oxsemi,
1596 pbn_oxsemi_1_4000000,
1597 pbn_oxsemi_2_4000000,
1598 pbn_oxsemi_4_4000000,
1599 pbn_oxsemi_8_4000000,
1600 pbn_intel_i960,
1601 pbn_sgi_ioc3,
1602 pbn_computone_4,
1603 pbn_computone_6,
1604 pbn_computone_8,
1605 pbn_sbsxrsio,
1606 pbn_exar_XR17C152,
1607 pbn_exar_XR17C154,
1608 pbn_exar_XR17C158,
1609 pbn_exar_ibm_saturn,
1610 pbn_pasemi_1682M,
1611 pbn_ni8430_2,
1612 pbn_ni8430_4,
1613 pbn_ni8430_8,
1614 pbn_ni8430_16,
1615 pbn_ADDIDATA_PCIe_1_3906250,
1616 pbn_ADDIDATA_PCIe_2_3906250,
1617 pbn_ADDIDATA_PCIe_4_3906250,
1618 pbn_ADDIDATA_PCIe_8_3906250,
1619 pbn_ce4100_1_115200,
1620};
1621
1622/*
1623 * uart_offset - the space between channels
1624 * reg_shift - describes how the UART registers are mapped
1625 * to PCI memory by the card.
1626 * For example IER register on SBS, Inc. PMC-OctPro is located at
1627 * offset 0x10 from the UART base, while UART_IER is defined as 1
1628 * in include/linux/serial_reg.h,
1629 * see first lines of serial_in() and serial_out() in 8250.c
1630*/
1631
1632static struct pciserial_board pci_boards[] __devinitdata = {
1633 [pbn_default] = {
1634 .flags = FL_BASE0,
1635 .num_ports = 1,
1636 .base_baud = 115200,
1637 .uart_offset = 8,
1638 },
1639 [pbn_b0_1_115200] = {
1640 .flags = FL_BASE0,
1641 .num_ports = 1,
1642 .base_baud = 115200,
1643 .uart_offset = 8,
1644 },
1645 [pbn_b0_2_115200] = {
1646 .flags = FL_BASE0,
1647 .num_ports = 2,
1648 .base_baud = 115200,
1649 .uart_offset = 8,
1650 },
1651 [pbn_b0_4_115200] = {
1652 .flags = FL_BASE0,
1653 .num_ports = 4,
1654 .base_baud = 115200,
1655 .uart_offset = 8,
1656 },
1657 [pbn_b0_5_115200] = {
1658 .flags = FL_BASE0,
1659 .num_ports = 5,
1660 .base_baud = 115200,
1661 .uart_offset = 8,
1662 },
1663 [pbn_b0_8_115200] = {
1664 .flags = FL_BASE0,
1665 .num_ports = 8,
1666 .base_baud = 115200,
1667 .uart_offset = 8,
1668 },
1669 [pbn_b0_1_921600] = {
1670 .flags = FL_BASE0,
1671 .num_ports = 1,
1672 .base_baud = 921600,
1673 .uart_offset = 8,
1674 },
1675 [pbn_b0_2_921600] = {
1676 .flags = FL_BASE0,
1677 .num_ports = 2,
1678 .base_baud = 921600,
1679 .uart_offset = 8,
1680 },
1681 [pbn_b0_4_921600] = {
1682 .flags = FL_BASE0,
1683 .num_ports = 4,
1684 .base_baud = 921600,
1685 .uart_offset = 8,
1686 },
1687
1688 [pbn_b0_2_1130000] = {
1689 .flags = FL_BASE0,
1690 .num_ports = 2,
1691 .base_baud = 1130000,
1692 .uart_offset = 8,
1693 },
1694
1695 [pbn_b0_4_1152000] = {
1696 .flags = FL_BASE0,
1697 .num_ports = 4,
1698 .base_baud = 1152000,
1699 .uart_offset = 8,
1700 },
1701
1702 [pbn_b0_2_1843200] = {
1703 .flags = FL_BASE0,
1704 .num_ports = 2,
1705 .base_baud = 1843200,
1706 .uart_offset = 8,
1707 },
1708 [pbn_b0_4_1843200] = {
1709 .flags = FL_BASE0,
1710 .num_ports = 4,
1711 .base_baud = 1843200,
1712 .uart_offset = 8,
1713 },
1714
1715 [pbn_b0_2_1843200_200] = {
1716 .flags = FL_BASE0,
1717 .num_ports = 2,
1718 .base_baud = 1843200,
1719 .uart_offset = 0x200,
1720 },
1721 [pbn_b0_4_1843200_200] = {
1722 .flags = FL_BASE0,
1723 .num_ports = 4,
1724 .base_baud = 1843200,
1725 .uart_offset = 0x200,
1726 },
1727 [pbn_b0_8_1843200_200] = {
1728 .flags = FL_BASE0,
1729 .num_ports = 8,
1730 .base_baud = 1843200,
1731 .uart_offset = 0x200,
1732 },
1733 [pbn_b0_1_4000000] = {
1734 .flags = FL_BASE0,
1735 .num_ports = 1,
1736 .base_baud = 4000000,
1737 .uart_offset = 8,
1738 },
1739
1740 [pbn_b0_bt_1_115200] = {
1741 .flags = FL_BASE0|FL_BASE_BARS,
1742 .num_ports = 1,
1743 .base_baud = 115200,
1744 .uart_offset = 8,
1745 },
1746 [pbn_b0_bt_2_115200] = {
1747 .flags = FL_BASE0|FL_BASE_BARS,
1748 .num_ports = 2,
1749 .base_baud = 115200,
1750 .uart_offset = 8,
1751 },
1752 [pbn_b0_bt_4_115200] = {
1753 .flags = FL_BASE0|FL_BASE_BARS,
1754 .num_ports = 4,
1755 .base_baud = 115200,
1756 .uart_offset = 8,
1757 },
1758 [pbn_b0_bt_8_115200] = {
1759 .flags = FL_BASE0|FL_BASE_BARS,
1760 .num_ports = 8,
1761 .base_baud = 115200,
1762 .uart_offset = 8,
1763 },
1764
1765 [pbn_b0_bt_1_460800] = {
1766 .flags = FL_BASE0|FL_BASE_BARS,
1767 .num_ports = 1,
1768 .base_baud = 460800,
1769 .uart_offset = 8,
1770 },
1771 [pbn_b0_bt_2_460800] = {
1772 .flags = FL_BASE0|FL_BASE_BARS,
1773 .num_ports = 2,
1774 .base_baud = 460800,
1775 .uart_offset = 8,
1776 },
1777 [pbn_b0_bt_4_460800] = {
1778 .flags = FL_BASE0|FL_BASE_BARS,
1779 .num_ports = 4,
1780 .base_baud = 460800,
1781 .uart_offset = 8,
1782 },
1783
1784 [pbn_b0_bt_1_921600] = {
1785 .flags = FL_BASE0|FL_BASE_BARS,
1786 .num_ports = 1,
1787 .base_baud = 921600,
1788 .uart_offset = 8,
1789 },
1790 [pbn_b0_bt_2_921600] = {
1791 .flags = FL_BASE0|FL_BASE_BARS,
1792 .num_ports = 2,
1793 .base_baud = 921600,
1794 .uart_offset = 8,
1795 },
1796 [pbn_b0_bt_4_921600] = {
1797 .flags = FL_BASE0|FL_BASE_BARS,
1798 .num_ports = 4,
1799 .base_baud = 921600,
1800 .uart_offset = 8,
1801 },
1802 [pbn_b0_bt_8_921600] = {
1803 .flags = FL_BASE0|FL_BASE_BARS,
1804 .num_ports = 8,
1805 .base_baud = 921600,
1806 .uart_offset = 8,
1807 },
1808
1809 [pbn_b1_1_115200] = {
1810 .flags = FL_BASE1,
1811 .num_ports = 1,
1812 .base_baud = 115200,
1813 .uart_offset = 8,
1814 },
1815 [pbn_b1_2_115200] = {
1816 .flags = FL_BASE1,
1817 .num_ports = 2,
1818 .base_baud = 115200,
1819 .uart_offset = 8,
1820 },
1821 [pbn_b1_4_115200] = {
1822 .flags = FL_BASE1,
1823 .num_ports = 4,
1824 .base_baud = 115200,
1825 .uart_offset = 8,
1826 },
1827 [pbn_b1_8_115200] = {
1828 .flags = FL_BASE1,
1829 .num_ports = 8,
1830 .base_baud = 115200,
1831 .uart_offset = 8,
1832 },
1833 [pbn_b1_16_115200] = {
1834 .flags = FL_BASE1,
1835 .num_ports = 16,
1836 .base_baud = 115200,
1837 .uart_offset = 8,
1838 },
1839
1840 [pbn_b1_1_921600] = {
1841 .flags = FL_BASE1,
1842 .num_ports = 1,
1843 .base_baud = 921600,
1844 .uart_offset = 8,
1845 },
1846 [pbn_b1_2_921600] = {
1847 .flags = FL_BASE1,
1848 .num_ports = 2,
1849 .base_baud = 921600,
1850 .uart_offset = 8,
1851 },
1852 [pbn_b1_4_921600] = {
1853 .flags = FL_BASE1,
1854 .num_ports = 4,
1855 .base_baud = 921600,
1856 .uart_offset = 8,
1857 },
1858 [pbn_b1_8_921600] = {
1859 .flags = FL_BASE1,
1860 .num_ports = 8,
1861 .base_baud = 921600,
1862 .uart_offset = 8,
1863 },
1864 [pbn_b1_2_1250000] = {
1865 .flags = FL_BASE1,
1866 .num_ports = 2,
1867 .base_baud = 1250000,
1868 .uart_offset = 8,
1869 },
1870
1871 [pbn_b1_bt_1_115200] = {
1872 .flags = FL_BASE1|FL_BASE_BARS,
1873 .num_ports = 1,
1874 .base_baud = 115200,
1875 .uart_offset = 8,
1876 },
1877 [pbn_b1_bt_2_115200] = {
1878 .flags = FL_BASE1|FL_BASE_BARS,
1879 .num_ports = 2,
1880 .base_baud = 115200,
1881 .uart_offset = 8,
1882 },
1883 [pbn_b1_bt_4_115200] = {
1884 .flags = FL_BASE1|FL_BASE_BARS,
1885 .num_ports = 4,
1886 .base_baud = 115200,
1887 .uart_offset = 8,
1888 },
1889
1890 [pbn_b1_bt_2_921600] = {
1891 .flags = FL_BASE1|FL_BASE_BARS,
1892 .num_ports = 2,
1893 .base_baud = 921600,
1894 .uart_offset = 8,
1895 },
1896
1897 [pbn_b1_1_1382400] = {
1898 .flags = FL_BASE1,
1899 .num_ports = 1,
1900 .base_baud = 1382400,
1901 .uart_offset = 8,
1902 },
1903 [pbn_b1_2_1382400] = {
1904 .flags = FL_BASE1,
1905 .num_ports = 2,
1906 .base_baud = 1382400,
1907 .uart_offset = 8,
1908 },
1909 [pbn_b1_4_1382400] = {
1910 .flags = FL_BASE1,
1911 .num_ports = 4,
1912 .base_baud = 1382400,
1913 .uart_offset = 8,
1914 },
1915 [pbn_b1_8_1382400] = {
1916 .flags = FL_BASE1,
1917 .num_ports = 8,
1918 .base_baud = 1382400,
1919 .uart_offset = 8,
1920 },
1921
1922 [pbn_b2_1_115200] = {
1923 .flags = FL_BASE2,
1924 .num_ports = 1,
1925 .base_baud = 115200,
1926 .uart_offset = 8,
1927 },
1928 [pbn_b2_2_115200] = {
1929 .flags = FL_BASE2,
1930 .num_ports = 2,
1931 .base_baud = 115200,
1932 .uart_offset = 8,
1933 },
1934 [pbn_b2_4_115200] = {
1935 .flags = FL_BASE2,
1936 .num_ports = 4,
1937 .base_baud = 115200,
1938 .uart_offset = 8,
1939 },
1940 [pbn_b2_8_115200] = {
1941 .flags = FL_BASE2,
1942 .num_ports = 8,
1943 .base_baud = 115200,
1944 .uart_offset = 8,
1945 },
1946
1947 [pbn_b2_1_460800] = {
1948 .flags = FL_BASE2,
1949 .num_ports = 1,
1950 .base_baud = 460800,
1951 .uart_offset = 8,
1952 },
1953 [pbn_b2_4_460800] = {
1954 .flags = FL_BASE2,
1955 .num_ports = 4,
1956 .base_baud = 460800,
1957 .uart_offset = 8,
1958 },
1959 [pbn_b2_8_460800] = {
1960 .flags = FL_BASE2,
1961 .num_ports = 8,
1962 .base_baud = 460800,
1963 .uart_offset = 8,
1964 },
1965 [pbn_b2_16_460800] = {
1966 .flags = FL_BASE2,
1967 .num_ports = 16,
1968 .base_baud = 460800,
1969 .uart_offset = 8,
1970 },
1971
1972 [pbn_b2_1_921600] = {
1973 .flags = FL_BASE2,
1974 .num_ports = 1,
1975 .base_baud = 921600,
1976 .uart_offset = 8,
1977 },
1978 [pbn_b2_4_921600] = {
1979 .flags = FL_BASE2,
1980 .num_ports = 4,
1981 .base_baud = 921600,
1982 .uart_offset = 8,
1983 },
1984 [pbn_b2_8_921600] = {
1985 .flags = FL_BASE2,
1986 .num_ports = 8,
1987 .base_baud = 921600,
1988 .uart_offset = 8,
1989 },
1990
1991 [pbn_b2_8_1152000] = {
1992 .flags = FL_BASE2,
1993 .num_ports = 8,
1994 .base_baud = 1152000,
1995 .uart_offset = 8,
1996 },
1997
1998 [pbn_b2_bt_1_115200] = {
1999 .flags = FL_BASE2|FL_BASE_BARS,
2000 .num_ports = 1,
2001 .base_baud = 115200,
2002 .uart_offset = 8,
2003 },
2004 [pbn_b2_bt_2_115200] = {
2005 .flags = FL_BASE2|FL_BASE_BARS,
2006 .num_ports = 2,
2007 .base_baud = 115200,
2008 .uart_offset = 8,
2009 },
2010 [pbn_b2_bt_4_115200] = {
2011 .flags = FL_BASE2|FL_BASE_BARS,
2012 .num_ports = 4,
2013 .base_baud = 115200,
2014 .uart_offset = 8,
2015 },
2016
2017 [pbn_b2_bt_2_921600] = {
2018 .flags = FL_BASE2|FL_BASE_BARS,
2019 .num_ports = 2,
2020 .base_baud = 921600,
2021 .uart_offset = 8,
2022 },
2023 [pbn_b2_bt_4_921600] = {
2024 .flags = FL_BASE2|FL_BASE_BARS,
2025 .num_ports = 4,
2026 .base_baud = 921600,
2027 .uart_offset = 8,
2028 },
2029
2030 [pbn_b3_2_115200] = {
2031 .flags = FL_BASE3,
2032 .num_ports = 2,
2033 .base_baud = 115200,
2034 .uart_offset = 8,
2035 },
2036 [pbn_b3_4_115200] = {
2037 .flags = FL_BASE3,
2038 .num_ports = 4,
2039 .base_baud = 115200,
2040 .uart_offset = 8,
2041 },
2042 [pbn_b3_8_115200] = {
2043 .flags = FL_BASE3,
2044 .num_ports = 8,
2045 .base_baud = 115200,
2046 .uart_offset = 8,
2047 },
2048
2049 [pbn_b4_bt_2_921600] = {
2050 .flags = FL_BASE4,
2051 .num_ports = 2,
2052 .base_baud = 921600,
2053 .uart_offset = 8,
2054 },
2055 [pbn_b4_bt_4_921600] = {
2056 .flags = FL_BASE4,
2057 .num_ports = 4,
2058 .base_baud = 921600,
2059 .uart_offset = 8,
2060 },
2061 [pbn_b4_bt_8_921600] = {
2062 .flags = FL_BASE4,
2063 .num_ports = 8,
2064 .base_baud = 921600,
2065 .uart_offset = 8,
2066 },
2067
2068 /*
2069 * Entries following this are board-specific.
2070 */
2071
2072 /*
2073 * Panacom - IOMEM
2074 */
2075 [pbn_panacom] = {
2076 .flags = FL_BASE2,
2077 .num_ports = 2,
2078 .base_baud = 921600,
2079 .uart_offset = 0x400,
2080 .reg_shift = 7,
2081 },
2082 [pbn_panacom2] = {
2083 .flags = FL_BASE2|FL_BASE_BARS,
2084 .num_ports = 2,
2085 .base_baud = 921600,
2086 .uart_offset = 0x400,
2087 .reg_shift = 7,
2088 },
2089 [pbn_panacom4] = {
2090 .flags = FL_BASE2|FL_BASE_BARS,
2091 .num_ports = 4,
2092 .base_baud = 921600,
2093 .uart_offset = 0x400,
2094 .reg_shift = 7,
2095 },
2096
2097 [pbn_exsys_4055] = {
2098 .flags = FL_BASE2,
2099 .num_ports = 4,
2100 .base_baud = 115200,
2101 .uart_offset = 8,
2102 },
2103
2104 /* I think this entry is broken - the first_offset looks wrong --rmk */
2105 [pbn_plx_romulus] = {
2106 .flags = FL_BASE2,
2107 .num_ports = 4,
2108 .base_baud = 921600,
2109 .uart_offset = 8 << 2,
2110 .reg_shift = 2,
2111 .first_offset = 0x03,
2112 },
2113
2114 /*
2115 * This board uses the size of PCI Base region 0 to
2116 * signal now many ports are available
2117 */
2118 [pbn_oxsemi] = {
2119 .flags = FL_BASE0|FL_REGION_SZ_CAP,
2120 .num_ports = 32,
2121 .base_baud = 115200,
2122 .uart_offset = 8,
2123 },
2124 [pbn_oxsemi_1_4000000] = {
2125 .flags = FL_BASE0,
2126 .num_ports = 1,
2127 .base_baud = 4000000,
2128 .uart_offset = 0x200,
2129 .first_offset = 0x1000,
2130 },
2131 [pbn_oxsemi_2_4000000] = {
2132 .flags = FL_BASE0,
2133 .num_ports = 2,
2134 .base_baud = 4000000,
2135 .uart_offset = 0x200,
2136 .first_offset = 0x1000,
2137 },
2138 [pbn_oxsemi_4_4000000] = {
2139 .flags = FL_BASE0,
2140 .num_ports = 4,
2141 .base_baud = 4000000,
2142 .uart_offset = 0x200,
2143 .first_offset = 0x1000,
2144 },
2145 [pbn_oxsemi_8_4000000] = {
2146 .flags = FL_BASE0,
2147 .num_ports = 8,
2148 .base_baud = 4000000,
2149 .uart_offset = 0x200,
2150 .first_offset = 0x1000,
2151 },
2152
2153
2154 /*
2155 * EKF addition for i960 Boards form EKF with serial port.
2156 * Max 256 ports.
2157 */
2158 [pbn_intel_i960] = {
2159 .flags = FL_BASE0,
2160 .num_ports = 32,
2161 .base_baud = 921600,
2162 .uart_offset = 8 << 2,
2163 .reg_shift = 2,
2164 .first_offset = 0x10000,
2165 },
2166 [pbn_sgi_ioc3] = {
2167 .flags = FL_BASE0|FL_NOIRQ,
2168 .num_ports = 1,
2169 .base_baud = 458333,
2170 .uart_offset = 8,
2171 .reg_shift = 0,
2172 .first_offset = 0x20178,
2173 },
2174
2175 /*
2176 * Computone - uses IOMEM.
2177 */
2178 [pbn_computone_4] = {
2179 .flags = FL_BASE0,
2180 .num_ports = 4,
2181 .base_baud = 921600,
2182 .uart_offset = 0x40,
2183 .reg_shift = 2,
2184 .first_offset = 0x200,
2185 },
2186 [pbn_computone_6] = {
2187 .flags = FL_BASE0,
2188 .num_ports = 6,
2189 .base_baud = 921600,
2190 .uart_offset = 0x40,
2191 .reg_shift = 2,
2192 .first_offset = 0x200,
2193 },
2194 [pbn_computone_8] = {
2195 .flags = FL_BASE0,
2196 .num_ports = 8,
2197 .base_baud = 921600,
2198 .uart_offset = 0x40,
2199 .reg_shift = 2,
2200 .first_offset = 0x200,
2201 },
2202 [pbn_sbsxrsio] = {
2203 .flags = FL_BASE0,
2204 .num_ports = 8,
2205 .base_baud = 460800,
2206 .uart_offset = 256,
2207 .reg_shift = 4,
2208 },
2209 /*
2210 * Exar Corp. XR17C15[248] Dual/Quad/Octal UART
2211 * Only basic 16550A support.
2212 * XR17C15[24] are not tested, but they should work.
2213 */
2214 [pbn_exar_XR17C152] = {
2215 .flags = FL_BASE0,
2216 .num_ports = 2,
2217 .base_baud = 921600,
2218 .uart_offset = 0x200,
2219 },
2220 [pbn_exar_XR17C154] = {
2221 .flags = FL_BASE0,
2222 .num_ports = 4,
2223 .base_baud = 921600,
2224 .uart_offset = 0x200,
2225 },
2226 [pbn_exar_XR17C158] = {
2227 .flags = FL_BASE0,
2228 .num_ports = 8,
2229 .base_baud = 921600,
2230 .uart_offset = 0x200,
2231 },
2232 [pbn_exar_ibm_saturn] = {
2233 .flags = FL_BASE0,
2234 .num_ports = 1,
2235 .base_baud = 921600,
2236 .uart_offset = 0x200,
2237 },
2238
2239 /*
2240 * PA Semi PWRficient PA6T-1682M on-chip UART
2241 */
2242 [pbn_pasemi_1682M] = {
2243 .flags = FL_BASE0,
2244 .num_ports = 1,
2245 .base_baud = 8333333,
2246 },
2247 /*
2248 * National Instruments 843x
2249 */
2250 [pbn_ni8430_16] = {
2251 .flags = FL_BASE0,
2252 .num_ports = 16,
2253 .base_baud = 3686400,
2254 .uart_offset = 0x10,
2255 .first_offset = 0x800,
2256 },
2257 [pbn_ni8430_8] = {
2258 .flags = FL_BASE0,
2259 .num_ports = 8,
2260 .base_baud = 3686400,
2261 .uart_offset = 0x10,
2262 .first_offset = 0x800,
2263 },
2264 [pbn_ni8430_4] = {
2265 .flags = FL_BASE0,
2266 .num_ports = 4,
2267 .base_baud = 3686400,
2268 .uart_offset = 0x10,
2269 .first_offset = 0x800,
2270 },
2271 [pbn_ni8430_2] = {
2272 .flags = FL_BASE0,
2273 .num_ports = 2,
2274 .base_baud = 3686400,
2275 .uart_offset = 0x10,
2276 .first_offset = 0x800,
2277 },
2278 /*
2279 * ADDI-DATA GmbH PCI-Express communication cards <info@addi-data.com>
2280 */
2281 [pbn_ADDIDATA_PCIe_1_3906250] = {
2282 .flags = FL_BASE0,
2283 .num_ports = 1,
2284 .base_baud = 3906250,
2285 .uart_offset = 0x200,
2286 .first_offset = 0x1000,
2287 },
2288 [pbn_ADDIDATA_PCIe_2_3906250] = {
2289 .flags = FL_BASE0,
2290 .num_ports = 2,
2291 .base_baud = 3906250,
2292 .uart_offset = 0x200,
2293 .first_offset = 0x1000,
2294 },
2295 [pbn_ADDIDATA_PCIe_4_3906250] = {
2296 .flags = FL_BASE0,
2297 .num_ports = 4,
2298 .base_baud = 3906250,
2299 .uart_offset = 0x200,
2300 .first_offset = 0x1000,
2301 },
2302 [pbn_ADDIDATA_PCIe_8_3906250] = {
2303 .flags = FL_BASE0,
2304 .num_ports = 8,
2305 .base_baud = 3906250,
2306 .uart_offset = 0x200,
2307 .first_offset = 0x1000,
2308 },
2309 [pbn_ce4100_1_115200] = {
2310 .flags = FL_BASE0,
2311 .num_ports = 1,
2312 .base_baud = 921600,
2313 .reg_shift = 2,
2314 },
2315};
2316
2317static const struct pci_device_id softmodem_blacklist[] = {
2318 { PCI_VDEVICE(AL, 0x5457), }, /* ALi Corporation M5457 AC'97 Modem */
2319 { PCI_VDEVICE(MOTOROLA, 0x3052), }, /* Motorola Si3052-based modem */
2320 { PCI_DEVICE(0x1543, 0x3052), }, /* Si3052-based modem, default IDs */
2321};
2322
2323/*
2324 * Given a complete unknown PCI device, try to use some heuristics to
2325 * guess what the configuration might be, based on the pitiful PCI
2326 * serial specs. Returns 0 on success, 1 on failure.
2327 */
2328static int __devinit
2329serial_pci_guess_board(struct pci_dev *dev, struct pciserial_board *board)
2330{
2331 const struct pci_device_id *blacklist;
2332 int num_iomem, num_port, first_port = -1, i;
2333
2334 /*
2335 * If it is not a communications device or the programming
2336 * interface is greater than 6, give up.
2337 *
2338 * (Should we try to make guesses for multiport serial devices
2339 * later?)
2340 */
2341 if ((((dev->class >> 8) != PCI_CLASS_COMMUNICATION_SERIAL) &&
2342 ((dev->class >> 8) != PCI_CLASS_COMMUNICATION_MODEM)) ||
2343 (dev->class & 0xff) > 6)
2344 return -ENODEV;
2345
2346 /*
2347 * Do not access blacklisted devices that are known not to
2348 * feature serial ports.
2349 */
2350 for (blacklist = softmodem_blacklist;
2351 blacklist < softmodem_blacklist + ARRAY_SIZE(softmodem_blacklist);
2352 blacklist++) {
2353 if (dev->vendor == blacklist->vendor &&
2354 dev->device == blacklist->device)
2355 return -ENODEV;
2356 }
2357
2358 num_iomem = num_port = 0;
2359 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
2360 if (pci_resource_flags(dev, i) & IORESOURCE_IO) {
2361 num_port++;
2362 if (first_port == -1)
2363 first_port = i;
2364 }
2365 if (pci_resource_flags(dev, i) & IORESOURCE_MEM)
2366 num_iomem++;
2367 }
2368
2369 /*
2370 * If there is 1 or 0 iomem regions, and exactly one port,
2371 * use it. We guess the number of ports based on the IO
2372 * region size.
2373 */
2374 if (num_iomem <= 1 && num_port == 1) {
2375 board->flags = first_port;
2376 board->num_ports = pci_resource_len(dev, first_port) / 8;
2377 return 0;
2378 }
2379
2380 /*
2381 * Now guess if we've got a board which indexes by BARs.
2382 * Each IO BAR should be 8 bytes, and they should follow
2383 * consecutively.
2384 */
2385 first_port = -1;
2386 num_port = 0;
2387 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
2388 if (pci_resource_flags(dev, i) & IORESOURCE_IO &&
2389 pci_resource_len(dev, i) == 8 &&
2390 (first_port == -1 || (first_port + num_port) == i)) {
2391 num_port++;
2392 if (first_port == -1)
2393 first_port = i;
2394 }
2395 }
2396
2397 if (num_port > 1) {
2398 board->flags = first_port | FL_BASE_BARS;
2399 board->num_ports = num_port;
2400 return 0;
2401 }
2402
2403 return -ENODEV;
2404}
2405
2406static inline int
2407serial_pci_matches(const struct pciserial_board *board,
2408 const struct pciserial_board *guessed)
2409{
2410 return
2411 board->num_ports == guessed->num_ports &&
2412 board->base_baud == guessed->base_baud &&
2413 board->uart_offset == guessed->uart_offset &&
2414 board->reg_shift == guessed->reg_shift &&
2415 board->first_offset == guessed->first_offset;
2416}
2417
2418struct serial_private *
2419pciserial_init_ports(struct pci_dev *dev, const struct pciserial_board *board)
2420{
2421 struct uart_port serial_port;
2422 struct serial_private *priv;
2423 struct pci_serial_quirk *quirk;
2424 int rc, nr_ports, i;
2425
2426 nr_ports = board->num_ports;
2427
2428 /*
2429 * Find an init and setup quirks.
2430 */
2431 quirk = find_quirk(dev);
2432
2433 /*
2434 * Run the new-style initialization function.
2435 * The initialization function returns:
2436 * <0 - error
2437 * 0 - use board->num_ports
2438 * >0 - number of ports
2439 */
2440 if (quirk->init) {
2441 rc = quirk->init(dev);
2442 if (rc < 0) {
2443 priv = ERR_PTR(rc);
2444 goto err_out;
2445 }
2446 if (rc)
2447 nr_ports = rc;
2448 }
2449
2450 priv = kzalloc(sizeof(struct serial_private) +
2451 sizeof(unsigned int) * nr_ports,
2452 GFP_KERNEL);
2453 if (!priv) {
2454 priv = ERR_PTR(-ENOMEM);
2455 goto err_deinit;
2456 }
2457
2458 priv->dev = dev;
2459 priv->quirk = quirk;
2460
2461 memset(&serial_port, 0, sizeof(struct uart_port));
2462 serial_port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
2463 serial_port.uartclk = board->base_baud * 16;
2464 serial_port.irq = get_pci_irq(dev, board);
2465 serial_port.dev = &dev->dev;
2466
2467 for (i = 0; i < nr_ports; i++) {
2468 if (quirk->setup(priv, board, &serial_port, i))
2469 break;
2470
2471#ifdef SERIAL_DEBUG_PCI
2472 printk(KERN_DEBUG "Setup PCI port: port %lx, irq %d, type %d\n",
2473 serial_port.iobase, serial_port.irq, serial_port.iotype);
2474#endif
2475
2476 priv->line[i] = serial8250_register_port(&serial_port);
2477 if (priv->line[i] < 0) {
2478 printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), priv->line[i]);
2479 break;
2480 }
2481 }
2482 priv->nr = i;
2483 return priv;
2484
2485err_deinit:
2486 if (quirk->exit)
2487 quirk->exit(dev);
2488err_out:
2489 return priv;
2490}
2491EXPORT_SYMBOL_GPL(pciserial_init_ports);
2492
2493void pciserial_remove_ports(struct serial_private *priv)
2494{
2495 struct pci_serial_quirk *quirk;
2496 int i;
2497
2498 for (i = 0; i < priv->nr; i++)
2499 serial8250_unregister_port(priv->line[i]);
2500
2501 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
2502 if (priv->remapped_bar[i])
2503 iounmap(priv->remapped_bar[i]);
2504 priv->remapped_bar[i] = NULL;
2505 }
2506
2507 /*
2508 * Find the exit quirks.
2509 */
2510 quirk = find_quirk(priv->dev);
2511 if (quirk->exit)
2512 quirk->exit(priv->dev);
2513
2514 kfree(priv);
2515}
2516EXPORT_SYMBOL_GPL(pciserial_remove_ports);
2517
2518void pciserial_suspend_ports(struct serial_private *priv)
2519{
2520 int i;
2521
2522 for (i = 0; i < priv->nr; i++)
2523 if (priv->line[i] >= 0)
2524 serial8250_suspend_port(priv->line[i]);
2525}
2526EXPORT_SYMBOL_GPL(pciserial_suspend_ports);
2527
2528void pciserial_resume_ports(struct serial_private *priv)
2529{
2530 int i;
2531
2532 /*
2533 * Ensure that the board is correctly configured.
2534 */
2535 if (priv->quirk->init)
2536 priv->quirk->init(priv->dev);
2537
2538 for (i = 0; i < priv->nr; i++)
2539 if (priv->line[i] >= 0)
2540 serial8250_resume_port(priv->line[i]);
2541}
2542EXPORT_SYMBOL_GPL(pciserial_resume_ports);
2543
2544/*
2545 * Probe one serial board. Unfortunately, there is no rhyme nor reason
2546 * to the arrangement of serial ports on a PCI card.
2547 */
2548static int __devinit
2549pciserial_init_one(struct pci_dev *dev, const struct pci_device_id *ent)
2550{
2551 struct serial_private *priv;
2552 const struct pciserial_board *board;
2553 struct pciserial_board tmp;
2554 int rc;
2555
2556 if (ent->driver_data >= ARRAY_SIZE(pci_boards)) {
2557 printk(KERN_ERR "pci_init_one: invalid driver_data: %ld\n",
2558 ent->driver_data);
2559 return -EINVAL;
2560 }
2561
2562 board = &pci_boards[ent->driver_data];
2563
2564 rc = pci_enable_device(dev);
2565 if (rc)
2566 return rc;
2567
2568 if (ent->driver_data == pbn_default) {
2569 /*
2570 * Use a copy of the pci_board entry for this;
2571 * avoid changing entries in the table.
2572 */
2573 memcpy(&tmp, board, sizeof(struct pciserial_board));
2574 board = &tmp;
2575
2576 /*
2577 * We matched one of our class entries. Try to
2578 * determine the parameters of this board.
2579 */
2580 rc = serial_pci_guess_board(dev, &tmp);
2581 if (rc)
2582 goto disable;
2583 } else {
2584 /*
2585 * We matched an explicit entry. If we are able to
2586 * detect this boards settings with our heuristic,
2587 * then we no longer need this entry.
2588 */
2589 memcpy(&tmp, &pci_boards[pbn_default],
2590 sizeof(struct pciserial_board));
2591 rc = serial_pci_guess_board(dev, &tmp);
2592 if (rc == 0 && serial_pci_matches(board, &tmp))
2593 moan_device("Redundant entry in serial pci_table.",
2594 dev);
2595 }
2596
2597 priv = pciserial_init_ports(dev, board);
2598 if (!IS_ERR(priv)) {
2599 pci_set_drvdata(dev, priv);
2600 return 0;
2601 }
2602
2603 rc = PTR_ERR(priv);
2604
2605 disable:
2606 pci_disable_device(dev);
2607 return rc;
2608}
2609
2610static void __devexit pciserial_remove_one(struct pci_dev *dev)
2611{
2612 struct serial_private *priv = pci_get_drvdata(dev);
2613
2614 pci_set_drvdata(dev, NULL);
2615
2616 pciserial_remove_ports(priv);
2617
2618 pci_disable_device(dev);
2619}
2620
2621#ifdef CONFIG_PM
2622static int pciserial_suspend_one(struct pci_dev *dev, pm_message_t state)
2623{
2624 struct serial_private *priv = pci_get_drvdata(dev);
2625
2626 if (priv)
2627 pciserial_suspend_ports(priv);
2628
2629 pci_save_state(dev);
2630 pci_set_power_state(dev, pci_choose_state(dev, state));
2631 return 0;
2632}
2633
2634static int pciserial_resume_one(struct pci_dev *dev)
2635{
2636 int err;
2637 struct serial_private *priv = pci_get_drvdata(dev);
2638
2639 pci_set_power_state(dev, PCI_D0);
2640 pci_restore_state(dev);
2641
2642 if (priv) {
2643 /*
2644 * The device may have been disabled. Re-enable it.
2645 */
2646 err = pci_enable_device(dev);
2647 /* FIXME: We cannot simply error out here */
2648 if (err)
2649 printk(KERN_ERR "pciserial: Unable to re-enable ports, trying to continue.\n");
2650 pciserial_resume_ports(priv);
2651 }
2652 return 0;
2653}
2654#endif
2655
2656static struct pci_device_id serial_pci_tbl[] = {
2657 /* Advantech use PCI_DEVICE_ID_ADVANTECH_PCI3620 (0x3620) as 'PCI_SUBVENDOR_ID' */
2658 { PCI_VENDOR_ID_ADVANTECH, PCI_DEVICE_ID_ADVANTECH_PCI3620,
2659 PCI_DEVICE_ID_ADVANTECH_PCI3620, 0x0001, 0, 0,
2660 pbn_b2_8_921600 },
2661 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
2662 PCI_SUBVENDOR_ID_CONNECT_TECH,
2663 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232, 0, 0,
2664 pbn_b1_8_1382400 },
2665 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
2666 PCI_SUBVENDOR_ID_CONNECT_TECH,
2667 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232, 0, 0,
2668 pbn_b1_4_1382400 },
2669 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
2670 PCI_SUBVENDOR_ID_CONNECT_TECH,
2671 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232, 0, 0,
2672 pbn_b1_2_1382400 },
2673 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2674 PCI_SUBVENDOR_ID_CONNECT_TECH,
2675 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232, 0, 0,
2676 pbn_b1_8_1382400 },
2677 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2678 PCI_SUBVENDOR_ID_CONNECT_TECH,
2679 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232, 0, 0,
2680 pbn_b1_4_1382400 },
2681 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2682 PCI_SUBVENDOR_ID_CONNECT_TECH,
2683 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232, 0, 0,
2684 pbn_b1_2_1382400 },
2685 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2686 PCI_SUBVENDOR_ID_CONNECT_TECH,
2687 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485, 0, 0,
2688 pbn_b1_8_921600 },
2689 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2690 PCI_SUBVENDOR_ID_CONNECT_TECH,
2691 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_4_4, 0, 0,
2692 pbn_b1_8_921600 },
2693 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2694 PCI_SUBVENDOR_ID_CONNECT_TECH,
2695 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485, 0, 0,
2696 pbn_b1_4_921600 },
2697 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2698 PCI_SUBVENDOR_ID_CONNECT_TECH,
2699 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485_2_2, 0, 0,
2700 pbn_b1_4_921600 },
2701 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2702 PCI_SUBVENDOR_ID_CONNECT_TECH,
2703 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_485, 0, 0,
2704 pbn_b1_2_921600 },
2705 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2706 PCI_SUBVENDOR_ID_CONNECT_TECH,
2707 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_2_6, 0, 0,
2708 pbn_b1_8_921600 },
2709 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2710 PCI_SUBVENDOR_ID_CONNECT_TECH,
2711 PCI_SUBDEVICE_ID_CONNECT_TECH_BH081101V1, 0, 0,
2712 pbn_b1_8_921600 },
2713 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2714 PCI_SUBVENDOR_ID_CONNECT_TECH,
2715 PCI_SUBDEVICE_ID_CONNECT_TECH_BH041101V1, 0, 0,
2716 pbn_b1_4_921600 },
2717 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2718 PCI_SUBVENDOR_ID_CONNECT_TECH,
2719 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_20MHZ, 0, 0,
2720 pbn_b1_2_1250000 },
2721 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2722 PCI_SUBVENDOR_ID_CONNECT_TECH,
2723 PCI_SUBDEVICE_ID_CONNECT_TECH_TITAN_2, 0, 0,
2724 pbn_b0_2_1843200 },
2725 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2726 PCI_SUBVENDOR_ID_CONNECT_TECH,
2727 PCI_SUBDEVICE_ID_CONNECT_TECH_TITAN_4, 0, 0,
2728 pbn_b0_4_1843200 },
2729 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2730 PCI_VENDOR_ID_AFAVLAB,
2731 PCI_SUBDEVICE_ID_AFAVLAB_P061, 0, 0,
2732 pbn_b0_4_1152000 },
2733 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2734 PCI_SUBVENDOR_ID_CONNECT_TECH,
2735 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_232, 0, 0,
2736 pbn_b0_2_1843200_200 },
2737 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2738 PCI_SUBVENDOR_ID_CONNECT_TECH,
2739 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_232, 0, 0,
2740 pbn_b0_4_1843200_200 },
2741 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2742 PCI_SUBVENDOR_ID_CONNECT_TECH,
2743 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_232, 0, 0,
2744 pbn_b0_8_1843200_200 },
2745 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2746 PCI_SUBVENDOR_ID_CONNECT_TECH,
2747 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_1_1, 0, 0,
2748 pbn_b0_2_1843200_200 },
2749 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2750 PCI_SUBVENDOR_ID_CONNECT_TECH,
2751 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_2, 0, 0,
2752 pbn_b0_4_1843200_200 },
2753 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2754 PCI_SUBVENDOR_ID_CONNECT_TECH,
2755 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4, 0, 0,
2756 pbn_b0_8_1843200_200 },
2757 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2758 PCI_SUBVENDOR_ID_CONNECT_TECH,
2759 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2, 0, 0,
2760 pbn_b0_2_1843200_200 },
2761 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2762 PCI_SUBVENDOR_ID_CONNECT_TECH,
2763 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4, 0, 0,
2764 pbn_b0_4_1843200_200 },
2765 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2766 PCI_SUBVENDOR_ID_CONNECT_TECH,
2767 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8, 0, 0,
2768 pbn_b0_8_1843200_200 },
2769 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2770 PCI_SUBVENDOR_ID_CONNECT_TECH,
2771 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_485, 0, 0,
2772 pbn_b0_2_1843200_200 },
2773 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2774 PCI_SUBVENDOR_ID_CONNECT_TECH,
2775 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_485, 0, 0,
2776 pbn_b0_4_1843200_200 },
2777 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2778 PCI_SUBVENDOR_ID_CONNECT_TECH,
2779 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_485, 0, 0,
2780 pbn_b0_8_1843200_200 },
2781 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2782 PCI_VENDOR_ID_IBM, PCI_SUBDEVICE_ID_IBM_SATURN_SERIAL_ONE_PORT,
2783 0, 0, pbn_exar_ibm_saturn },
2784
2785 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_U530,
2786 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2787 pbn_b2_bt_1_115200 },
2788 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM2,
2789 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2790 pbn_b2_bt_2_115200 },
2791 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM422,
2792 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2793 pbn_b2_bt_4_115200 },
2794 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM232,
2795 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2796 pbn_b2_bt_2_115200 },
2797 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM4,
2798 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2799 pbn_b2_bt_4_115200 },
2800 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM8,
2801 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2802 pbn_b2_8_115200 },
2803 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_7803,
2804 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2805 pbn_b2_8_460800 },
2806 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM8,
2807 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2808 pbn_b2_8_115200 },
2809
2810 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_GTEK_SERIAL2,
2811 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2812 pbn_b2_bt_2_115200 },
2813 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM200,
2814 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2815 pbn_b2_bt_2_921600 },
2816 /*
2817 * VScom SPCOM800, from sl@s.pl
2818 */
2819 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM800,
2820 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2821 pbn_b2_8_921600 },
2822 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_1077,
2823 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2824 pbn_b2_4_921600 },
2825 /* Unknown card - subdevice 0x1584 */
2826 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2827 PCI_VENDOR_ID_PLX,
2828 PCI_SUBDEVICE_ID_UNKNOWN_0x1584, 0, 0,
2829 pbn_b0_4_115200 },
2830 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2831 PCI_SUBVENDOR_ID_KEYSPAN,
2832 PCI_SUBDEVICE_ID_KEYSPAN_SX2, 0, 0,
2833 pbn_panacom },
2834 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_QUADMODEM,
2835 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2836 pbn_panacom4 },
2837 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_DUALMODEM,
2838 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2839 pbn_panacom2 },
2840 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
2841 PCI_VENDOR_ID_ESDGMBH,
2842 PCI_DEVICE_ID_ESDGMBH_CPCIASIO4, 0, 0,
2843 pbn_b2_4_115200 },
2844 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2845 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2846 PCI_SUBDEVICE_ID_CHASE_PCIFAST4, 0, 0,
2847 pbn_b2_4_460800 },
2848 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2849 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2850 PCI_SUBDEVICE_ID_CHASE_PCIFAST8, 0, 0,
2851 pbn_b2_8_460800 },
2852 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2853 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2854 PCI_SUBDEVICE_ID_CHASE_PCIFAST16, 0, 0,
2855 pbn_b2_16_460800 },
2856 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2857 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2858 PCI_SUBDEVICE_ID_CHASE_PCIFAST16FMC, 0, 0,
2859 pbn_b2_16_460800 },
2860 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2861 PCI_SUBVENDOR_ID_CHASE_PCIRAS,
2862 PCI_SUBDEVICE_ID_CHASE_PCIRAS4, 0, 0,
2863 pbn_b2_4_460800 },
2864 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2865 PCI_SUBVENDOR_ID_CHASE_PCIRAS,
2866 PCI_SUBDEVICE_ID_CHASE_PCIRAS8, 0, 0,
2867 pbn_b2_8_460800 },
2868 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2869 PCI_SUBVENDOR_ID_EXSYS,
2870 PCI_SUBDEVICE_ID_EXSYS_4055, 0, 0,
2871 pbn_exsys_4055 },
2872 /*
2873 * Megawolf Romulus PCI Serial Card, from Mike Hudson
2874 * (Exoray@isys.ca)
2875 */
2876 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_ROMULUS,
2877 0x10b5, 0x106a, 0, 0,
2878 pbn_plx_romulus },
2879 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_QSC100,
2880 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2881 pbn_b1_4_115200 },
2882 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_DSC100,
2883 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2884 pbn_b1_2_115200 },
2885 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_ESC100D,
2886 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2887 pbn_b1_8_115200 },
2888 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_ESC100M,
2889 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2890 pbn_b1_8_115200 },
2891 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_OXSEMI_16PCI954,
2892 PCI_VENDOR_ID_SPECIALIX, PCI_SUBDEVICE_ID_SPECIALIX_SPEED4,
2893 0, 0,
2894 pbn_b0_4_921600 },
2895 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2896 PCI_SUBVENDOR_ID_SIIG, PCI_SUBDEVICE_ID_SIIG_QUARTET_SERIAL,
2897 0, 0,
2898 pbn_b0_4_1152000 },
2899 { PCI_VENDOR_ID_OXSEMI, 0x9505,
2900 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2901 pbn_b0_bt_2_921600 },
2902
2903 /*
2904 * The below card is a little controversial since it is the
2905 * subject of a PCI vendor/device ID clash. (See
2906 * www.ussg.iu.edu/hypermail/linux/kernel/0303.1/0516.html).
2907 * For now just used the hex ID 0x950a.
2908 */
2909 { PCI_VENDOR_ID_OXSEMI, 0x950a,
2910 PCI_SUBVENDOR_ID_SIIG, PCI_SUBDEVICE_ID_SIIG_DUAL_SERIAL, 0, 0,
2911 pbn_b0_2_115200 },
2912 { PCI_VENDOR_ID_OXSEMI, 0x950a,
2913 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2914 pbn_b0_2_1130000 },
2915 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_C950,
2916 PCI_VENDOR_ID_OXSEMI, PCI_SUBDEVICE_ID_OXSEMI_C950, 0, 0,
2917 pbn_b0_1_921600 },
2918 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2919 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2920 pbn_b0_4_115200 },
2921 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI952,
2922 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2923 pbn_b0_bt_2_921600 },
2924 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI958,
2925 PCI_ANY_ID , PCI_ANY_ID, 0, 0,
2926 pbn_b2_8_1152000 },
2927
2928 /*
2929 * Oxford Semiconductor Inc. Tornado PCI express device range.
2930 */
2931 { PCI_VENDOR_ID_OXSEMI, 0xc101, /* OXPCIe952 1 Legacy UART */
2932 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2933 pbn_b0_1_4000000 },
2934 { PCI_VENDOR_ID_OXSEMI, 0xc105, /* OXPCIe952 1 Legacy UART */
2935 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2936 pbn_b0_1_4000000 },
2937 { PCI_VENDOR_ID_OXSEMI, 0xc11b, /* OXPCIe952 1 Native UART */
2938 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2939 pbn_oxsemi_1_4000000 },
2940 { PCI_VENDOR_ID_OXSEMI, 0xc11f, /* OXPCIe952 1 Native UART */
2941 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2942 pbn_oxsemi_1_4000000 },
2943 { PCI_VENDOR_ID_OXSEMI, 0xc120, /* OXPCIe952 1 Legacy UART */
2944 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2945 pbn_b0_1_4000000 },
2946 { PCI_VENDOR_ID_OXSEMI, 0xc124, /* OXPCIe952 1 Legacy UART */
2947 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2948 pbn_b0_1_4000000 },
2949 { PCI_VENDOR_ID_OXSEMI, 0xc138, /* OXPCIe952 1 Native UART */
2950 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2951 pbn_oxsemi_1_4000000 },
2952 { PCI_VENDOR_ID_OXSEMI, 0xc13d, /* OXPCIe952 1 Native UART */
2953 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2954 pbn_oxsemi_1_4000000 },
2955 { PCI_VENDOR_ID_OXSEMI, 0xc140, /* OXPCIe952 1 Legacy UART */
2956 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2957 pbn_b0_1_4000000 },
2958 { PCI_VENDOR_ID_OXSEMI, 0xc141, /* OXPCIe952 1 Legacy UART */
2959 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2960 pbn_b0_1_4000000 },
2961 { PCI_VENDOR_ID_OXSEMI, 0xc144, /* OXPCIe952 1 Legacy UART */
2962 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2963 pbn_b0_1_4000000 },
2964 { PCI_VENDOR_ID_OXSEMI, 0xc145, /* OXPCIe952 1 Legacy UART */
2965 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2966 pbn_b0_1_4000000 },
2967 { PCI_VENDOR_ID_OXSEMI, 0xc158, /* OXPCIe952 2 Native UART */
2968 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2969 pbn_oxsemi_2_4000000 },
2970 { PCI_VENDOR_ID_OXSEMI, 0xc15d, /* OXPCIe952 2 Native UART */
2971 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2972 pbn_oxsemi_2_4000000 },
2973 { PCI_VENDOR_ID_OXSEMI, 0xc208, /* OXPCIe954 4 Native UART */
2974 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2975 pbn_oxsemi_4_4000000 },
2976 { PCI_VENDOR_ID_OXSEMI, 0xc20d, /* OXPCIe954 4 Native UART */
2977 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2978 pbn_oxsemi_4_4000000 },
2979 { PCI_VENDOR_ID_OXSEMI, 0xc308, /* OXPCIe958 8 Native UART */
2980 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2981 pbn_oxsemi_8_4000000 },
2982 { PCI_VENDOR_ID_OXSEMI, 0xc30d, /* OXPCIe958 8 Native UART */
2983 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2984 pbn_oxsemi_8_4000000 },
2985 { PCI_VENDOR_ID_OXSEMI, 0xc40b, /* OXPCIe200 1 Native UART */
2986 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2987 pbn_oxsemi_1_4000000 },
2988 { PCI_VENDOR_ID_OXSEMI, 0xc40f, /* OXPCIe200 1 Native UART */
2989 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2990 pbn_oxsemi_1_4000000 },
2991 { PCI_VENDOR_ID_OXSEMI, 0xc41b, /* OXPCIe200 1 Native UART */
2992 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2993 pbn_oxsemi_1_4000000 },
2994 { PCI_VENDOR_ID_OXSEMI, 0xc41f, /* OXPCIe200 1 Native UART */
2995 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2996 pbn_oxsemi_1_4000000 },
2997 { PCI_VENDOR_ID_OXSEMI, 0xc42b, /* OXPCIe200 1 Native UART */
2998 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2999 pbn_oxsemi_1_4000000 },
3000 { PCI_VENDOR_ID_OXSEMI, 0xc42f, /* OXPCIe200 1 Native UART */
3001 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3002 pbn_oxsemi_1_4000000 },
3003 { PCI_VENDOR_ID_OXSEMI, 0xc43b, /* OXPCIe200 1 Native UART */
3004 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3005 pbn_oxsemi_1_4000000 },
3006 { PCI_VENDOR_ID_OXSEMI, 0xc43f, /* OXPCIe200 1 Native UART */
3007 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3008 pbn_oxsemi_1_4000000 },
3009 { PCI_VENDOR_ID_OXSEMI, 0xc44b, /* OXPCIe200 1 Native UART */
3010 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3011 pbn_oxsemi_1_4000000 },
3012 { PCI_VENDOR_ID_OXSEMI, 0xc44f, /* OXPCIe200 1 Native UART */
3013 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3014 pbn_oxsemi_1_4000000 },
3015 { PCI_VENDOR_ID_OXSEMI, 0xc45b, /* OXPCIe200 1 Native UART */
3016 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3017 pbn_oxsemi_1_4000000 },
3018 { PCI_VENDOR_ID_OXSEMI, 0xc45f, /* OXPCIe200 1 Native UART */
3019 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3020 pbn_oxsemi_1_4000000 },
3021 { PCI_VENDOR_ID_OXSEMI, 0xc46b, /* OXPCIe200 1 Native UART */
3022 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3023 pbn_oxsemi_1_4000000 },
3024 { PCI_VENDOR_ID_OXSEMI, 0xc46f, /* OXPCIe200 1 Native UART */
3025 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3026 pbn_oxsemi_1_4000000 },
3027 { PCI_VENDOR_ID_OXSEMI, 0xc47b, /* OXPCIe200 1 Native UART */
3028 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3029 pbn_oxsemi_1_4000000 },
3030 { PCI_VENDOR_ID_OXSEMI, 0xc47f, /* OXPCIe200 1 Native UART */
3031 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3032 pbn_oxsemi_1_4000000 },
3033 { PCI_VENDOR_ID_OXSEMI, 0xc48b, /* OXPCIe200 1 Native UART */
3034 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3035 pbn_oxsemi_1_4000000 },
3036 { PCI_VENDOR_ID_OXSEMI, 0xc48f, /* OXPCIe200 1 Native UART */
3037 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3038 pbn_oxsemi_1_4000000 },
3039 { PCI_VENDOR_ID_OXSEMI, 0xc49b, /* OXPCIe200 1 Native UART */
3040 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3041 pbn_oxsemi_1_4000000 },
3042 { PCI_VENDOR_ID_OXSEMI, 0xc49f, /* OXPCIe200 1 Native UART */
3043 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3044 pbn_oxsemi_1_4000000 },
3045 { PCI_VENDOR_ID_OXSEMI, 0xc4ab, /* OXPCIe200 1 Native UART */
3046 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3047 pbn_oxsemi_1_4000000 },
3048 { PCI_VENDOR_ID_OXSEMI, 0xc4af, /* OXPCIe200 1 Native UART */
3049 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3050 pbn_oxsemi_1_4000000 },
3051 { PCI_VENDOR_ID_OXSEMI, 0xc4bb, /* OXPCIe200 1 Native UART */
3052 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3053 pbn_oxsemi_1_4000000 },
3054 { PCI_VENDOR_ID_OXSEMI, 0xc4bf, /* OXPCIe200 1 Native UART */
3055 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3056 pbn_oxsemi_1_4000000 },
3057 { PCI_VENDOR_ID_OXSEMI, 0xc4cb, /* OXPCIe200 1 Native UART */
3058 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3059 pbn_oxsemi_1_4000000 },
3060 { PCI_VENDOR_ID_OXSEMI, 0xc4cf, /* OXPCIe200 1 Native UART */
3061 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3062 pbn_oxsemi_1_4000000 },
3063 /*
3064 * Mainpine Inc. IQ Express "Rev3" utilizing OxSemi Tornado
3065 */
3066 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 1 Port V.34 Super-G3 Fax */
3067 PCI_VENDOR_ID_MAINPINE, 0x4001, 0, 0,
3068 pbn_oxsemi_1_4000000 },
3069 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 2 Port V.34 Super-G3 Fax */
3070 PCI_VENDOR_ID_MAINPINE, 0x4002, 0, 0,
3071 pbn_oxsemi_2_4000000 },
3072 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 4 Port V.34 Super-G3 Fax */
3073 PCI_VENDOR_ID_MAINPINE, 0x4004, 0, 0,
3074 pbn_oxsemi_4_4000000 },
3075 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 8 Port V.34 Super-G3 Fax */
3076 PCI_VENDOR_ID_MAINPINE, 0x4008, 0, 0,
3077 pbn_oxsemi_8_4000000 },
3078 /*
3079 * SBS Technologies, Inc. P-Octal and PMC-OCTPRO cards,
3080 * from skokodyn@yahoo.com
3081 */
3082 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3083 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_OCTPRO232, 0, 0,
3084 pbn_sbsxrsio },
3085 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3086 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_OCTPRO422, 0, 0,
3087 pbn_sbsxrsio },
3088 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3089 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_POCTAL232, 0, 0,
3090 pbn_sbsxrsio },
3091 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3092 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_POCTAL422, 0, 0,
3093 pbn_sbsxrsio },
3094
3095 /*
3096 * Digitan DS560-558, from jimd@esoft.com
3097 */
3098 { PCI_VENDOR_ID_ATT, PCI_DEVICE_ID_ATT_VENUS_MODEM,
3099 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3100 pbn_b1_1_115200 },
3101
3102 /*
3103 * Titan Electronic cards
3104 * The 400L and 800L have a custom setup quirk.
3105 */
3106 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100,
3107 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3108 pbn_b0_1_921600 },
3109 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200,
3110 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3111 pbn_b0_2_921600 },
3112 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400,
3113 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3114 pbn_b0_4_921600 },
3115 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800B,
3116 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3117 pbn_b0_4_921600 },
3118 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100L,
3119 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3120 pbn_b1_1_921600 },
3121 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200L,
3122 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3123 pbn_b1_bt_2_921600 },
3124 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400L,
3125 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3126 pbn_b0_bt_4_921600 },
3127 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800L,
3128 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3129 pbn_b0_bt_8_921600 },
3130 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200I,
3131 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3132 pbn_b4_bt_2_921600 },
3133 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400I,
3134 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3135 pbn_b4_bt_4_921600 },
3136 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800I,
3137 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3138 pbn_b4_bt_8_921600 },
3139 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400EH,
3140 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3141 pbn_b0_4_921600 },
3142 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800EH,
3143 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3144 pbn_b0_4_921600 },
3145 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800EHB,
3146 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3147 pbn_b0_4_921600 },
3148 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100E,
3149 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3150 pbn_oxsemi_1_4000000 },
3151 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200E,
3152 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3153 pbn_oxsemi_2_4000000 },
3154 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400E,
3155 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3156 pbn_oxsemi_4_4000000 },
3157 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800E,
3158 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3159 pbn_oxsemi_8_4000000 },
3160 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200EI,
3161 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3162 pbn_oxsemi_2_4000000 },
3163 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200EISI,
3164 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3165 pbn_oxsemi_2_4000000 },
3166
3167 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_550,
3168 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3169 pbn_b2_1_460800 },
3170 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_650,
3171 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3172 pbn_b2_1_460800 },
3173 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_850,
3174 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3175 pbn_b2_1_460800 },
3176 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_550,
3177 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3178 pbn_b2_bt_2_921600 },
3179 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_650,
3180 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3181 pbn_b2_bt_2_921600 },
3182 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_850,
3183 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3184 pbn_b2_bt_2_921600 },
3185 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_550,
3186 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3187 pbn_b2_bt_4_921600 },
3188 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_650,
3189 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3190 pbn_b2_bt_4_921600 },
3191 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_850,
3192 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3193 pbn_b2_bt_4_921600 },
3194 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_550,
3195 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3196 pbn_b0_1_921600 },
3197 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_650,
3198 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3199 pbn_b0_1_921600 },
3200 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_850,
3201 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3202 pbn_b0_1_921600 },
3203 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_550,
3204 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3205 pbn_b0_bt_2_921600 },
3206 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_650,
3207 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3208 pbn_b0_bt_2_921600 },
3209 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_850,
3210 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3211 pbn_b0_bt_2_921600 },
3212 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_550,
3213 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3214 pbn_b0_bt_4_921600 },
3215 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_650,
3216 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3217 pbn_b0_bt_4_921600 },
3218 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_850,
3219 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3220 pbn_b0_bt_4_921600 },
3221 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_8S_20x_550,
3222 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3223 pbn_b0_bt_8_921600 },
3224 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_8S_20x_650,
3225 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3226 pbn_b0_bt_8_921600 },
3227 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_8S_20x_850,
3228 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3229 pbn_b0_bt_8_921600 },
3230
3231 /*
3232 * Computone devices submitted by Doug McNash dmcnash@computone.com
3233 */
3234 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
3235 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG4,
3236 0, 0, pbn_computone_4 },
3237 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
3238 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG8,
3239 0, 0, pbn_computone_8 },
3240 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
3241 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG6,
3242 0, 0, pbn_computone_6 },
3243
3244 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI95N,
3245 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3246 pbn_oxsemi },
3247 { PCI_VENDOR_ID_TIMEDIA, PCI_DEVICE_ID_TIMEDIA_1889,
3248 PCI_VENDOR_ID_TIMEDIA, PCI_ANY_ID, 0, 0,
3249 pbn_b0_bt_1_921600 },
3250
3251 /*
3252 * AFAVLAB serial card, from Harald Welte <laforge@gnumonks.org>
3253 */
3254 { PCI_VENDOR_ID_AFAVLAB, PCI_DEVICE_ID_AFAVLAB_P028,
3255 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3256 pbn_b0_bt_8_115200 },
3257 { PCI_VENDOR_ID_AFAVLAB, PCI_DEVICE_ID_AFAVLAB_P030,
3258 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3259 pbn_b0_bt_8_115200 },
3260
3261 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DSERIAL,
3262 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3263 pbn_b0_bt_2_115200 },
3264 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATRO_A,
3265 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3266 pbn_b0_bt_2_115200 },
3267 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATRO_B,
3268 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3269 pbn_b0_bt_2_115200 },
3270 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATTRO_A,
3271 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3272 pbn_b0_bt_2_115200 },
3273 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATTRO_B,
3274 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3275 pbn_b0_bt_2_115200 },
3276 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_OCTO_A,
3277 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3278 pbn_b0_bt_4_460800 },
3279 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_OCTO_B,
3280 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3281 pbn_b0_bt_4_460800 },
3282 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PORT_PLUS,
3283 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3284 pbn_b0_bt_2_460800 },
3285 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUAD_A,
3286 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3287 pbn_b0_bt_2_460800 },
3288 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUAD_B,
3289 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3290 pbn_b0_bt_2_460800 },
3291 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_SSERIAL,
3292 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3293 pbn_b0_bt_1_115200 },
3294 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PORT_650,
3295 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3296 pbn_b0_bt_1_460800 },
3297
3298 /*
3299 * Korenix Jetcard F0/F1 cards (JC1204, JC1208, JC1404, JC1408).
3300 * Cards are identified by their subsystem vendor IDs, which
3301 * (in hex) match the model number.
3302 *
3303 * Note that JC140x are RS422/485 cards which require ox950
3304 * ACR = 0x10, and as such are not currently fully supported.
3305 */
3306 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3307 0x1204, 0x0004, 0, 0,
3308 pbn_b0_4_921600 },
3309 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3310 0x1208, 0x0004, 0, 0,
3311 pbn_b0_4_921600 },
3312/* { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3313 0x1402, 0x0002, 0, 0,
3314 pbn_b0_2_921600 }, */
3315/* { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3316 0x1404, 0x0004, 0, 0,
3317 pbn_b0_4_921600 }, */
3318 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF1,
3319 0x1208, 0x0004, 0, 0,
3320 pbn_b0_4_921600 },
3321
3322 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF2,
3323 0x1204, 0x0004, 0, 0,
3324 pbn_b0_4_921600 },
3325 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF2,
3326 0x1208, 0x0004, 0, 0,
3327 pbn_b0_4_921600 },
3328 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF3,
3329 0x1208, 0x0004, 0, 0,
3330 pbn_b0_4_921600 },
3331 /*
3332 * Dell Remote Access Card 4 - Tim_T_Murphy@Dell.com
3333 */
3334 { PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_RAC4,
3335 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3336 pbn_b1_1_1382400 },
3337
3338 /*
3339 * Dell Remote Access Card III - Tim_T_Murphy@Dell.com
3340 */
3341 { PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_RACIII,
3342 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3343 pbn_b1_1_1382400 },
3344
3345 /*
3346 * RAStel 2 port modem, gerg@moreton.com.au
3347 */
3348 { PCI_VENDOR_ID_MORETON, PCI_DEVICE_ID_RASTEL_2PORT,
3349 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3350 pbn_b2_bt_2_115200 },
3351
3352 /*
3353 * EKF addition for i960 Boards form EKF with serial port
3354 */
3355 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80960_RP,
3356 0xE4BF, PCI_ANY_ID, 0, 0,
3357 pbn_intel_i960 },
3358
3359 /*
3360 * Xircom Cardbus/Ethernet combos
3361 */
3362 { PCI_VENDOR_ID_XIRCOM, PCI_DEVICE_ID_XIRCOM_X3201_MDM,
3363 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3364 pbn_b0_1_115200 },
3365 /*
3366 * Xircom RBM56G cardbus modem - Dirk Arnold (temp entry)
3367 */
3368 { PCI_VENDOR_ID_XIRCOM, PCI_DEVICE_ID_XIRCOM_RBM56G,
3369 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3370 pbn_b0_1_115200 },
3371
3372 /*
3373 * Untested PCI modems, sent in from various folks...
3374 */
3375
3376 /*
3377 * Elsa Model 56K PCI Modem, from Andreas Rath <arh@01019freenet.de>
3378 */
3379 { PCI_VENDOR_ID_ROCKWELL, 0x1004,
3380 0x1048, 0x1500, 0, 0,
3381 pbn_b1_1_115200 },
3382
3383 { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,
3384 0xFF00, 0, 0, 0,
3385 pbn_sgi_ioc3 },
3386
3387 /*
3388 * HP Diva card
3389 */
3390 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA,
3391 PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA_RMP3, 0, 0,
3392 pbn_b1_1_115200 },
3393 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA,
3394 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3395 pbn_b0_5_115200 },
3396 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA_AUX,
3397 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3398 pbn_b2_1_115200 },
3399
3400 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM2,
3401 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3402 pbn_b3_2_115200 },
3403 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM4,
3404 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3405 pbn_b3_4_115200 },
3406 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM8,
3407 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3408 pbn_b3_8_115200 },
3409
3410 /*
3411 * Exar Corp. XR17C15[248] Dual/Quad/Octal UART
3412 */
3413 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
3414 PCI_ANY_ID, PCI_ANY_ID,
3415 0,
3416 0, pbn_exar_XR17C152 },
3417 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
3418 PCI_ANY_ID, PCI_ANY_ID,
3419 0,
3420 0, pbn_exar_XR17C154 },
3421 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
3422 PCI_ANY_ID, PCI_ANY_ID,
3423 0,
3424 0, pbn_exar_XR17C158 },
3425
3426 /*
3427 * Topic TP560 Data/Fax/Voice 56k modem (reported by Evan Clarke)
3428 */
3429 { PCI_VENDOR_ID_TOPIC, PCI_DEVICE_ID_TOPIC_TP560,
3430 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3431 pbn_b0_1_115200 },
3432 /*
3433 * ITE
3434 */
3435 { PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_8872,
3436 PCI_ANY_ID, PCI_ANY_ID,
3437 0, 0,
3438 pbn_b1_bt_1_115200 },
3439
3440 /*
3441 * IntaShield IS-200
3442 */
3443 { PCI_VENDOR_ID_INTASHIELD, PCI_DEVICE_ID_INTASHIELD_IS200,
3444 PCI_ANY_ID, PCI_ANY_ID, 0, 0, /* 135a.0811 */
3445 pbn_b2_2_115200 },
3446 /*
3447 * IntaShield IS-400
3448 */
3449 { PCI_VENDOR_ID_INTASHIELD, PCI_DEVICE_ID_INTASHIELD_IS400,
3450 PCI_ANY_ID, PCI_ANY_ID, 0, 0, /* 135a.0dc0 */
3451 pbn_b2_4_115200 },
3452 /*
3453 * Perle PCI-RAS cards
3454 */
3455 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
3456 PCI_SUBVENDOR_ID_PERLE, PCI_SUBDEVICE_ID_PCI_RAS4,
3457 0, 0, pbn_b2_4_921600 },
3458 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
3459 PCI_SUBVENDOR_ID_PERLE, PCI_SUBDEVICE_ID_PCI_RAS8,
3460 0, 0, pbn_b2_8_921600 },
3461
3462 /*
3463 * Mainpine series cards: Fairly standard layout but fools
3464 * parts of the autodetect in some cases and uses otherwise
3465 * unmatched communications subclasses in the PCI Express case
3466 */
3467
3468 { /* RockForceDUO */
3469 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3470 PCI_VENDOR_ID_MAINPINE, 0x0200,
3471 0, 0, pbn_b0_2_115200 },
3472 { /* RockForceQUATRO */
3473 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3474 PCI_VENDOR_ID_MAINPINE, 0x0300,
3475 0, 0, pbn_b0_4_115200 },
3476 { /* RockForceDUO+ */
3477 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3478 PCI_VENDOR_ID_MAINPINE, 0x0400,
3479 0, 0, pbn_b0_2_115200 },
3480 { /* RockForceQUATRO+ */
3481 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3482 PCI_VENDOR_ID_MAINPINE, 0x0500,
3483 0, 0, pbn_b0_4_115200 },
3484 { /* RockForce+ */
3485 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3486 PCI_VENDOR_ID_MAINPINE, 0x0600,
3487 0, 0, pbn_b0_2_115200 },
3488 { /* RockForce+ */
3489 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3490 PCI_VENDOR_ID_MAINPINE, 0x0700,
3491 0, 0, pbn_b0_4_115200 },
3492 { /* RockForceOCTO+ */
3493 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3494 PCI_VENDOR_ID_MAINPINE, 0x0800,
3495 0, 0, pbn_b0_8_115200 },
3496 { /* RockForceDUO+ */
3497 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3498 PCI_VENDOR_ID_MAINPINE, 0x0C00,
3499 0, 0, pbn_b0_2_115200 },
3500 { /* RockForceQUARTRO+ */
3501 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3502 PCI_VENDOR_ID_MAINPINE, 0x0D00,
3503 0, 0, pbn_b0_4_115200 },
3504 { /* RockForceOCTO+ */
3505 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3506 PCI_VENDOR_ID_MAINPINE, 0x1D00,
3507 0, 0, pbn_b0_8_115200 },
3508 { /* RockForceD1 */
3509 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3510 PCI_VENDOR_ID_MAINPINE, 0x2000,
3511 0, 0, pbn_b0_1_115200 },
3512 { /* RockForceF1 */
3513 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3514 PCI_VENDOR_ID_MAINPINE, 0x2100,
3515 0, 0, pbn_b0_1_115200 },
3516 { /* RockForceD2 */
3517 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3518 PCI_VENDOR_ID_MAINPINE, 0x2200,
3519 0, 0, pbn_b0_2_115200 },
3520 { /* RockForceF2 */
3521 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3522 PCI_VENDOR_ID_MAINPINE, 0x2300,
3523 0, 0, pbn_b0_2_115200 },
3524 { /* RockForceD4 */
3525 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3526 PCI_VENDOR_ID_MAINPINE, 0x2400,
3527 0, 0, pbn_b0_4_115200 },
3528 { /* RockForceF4 */
3529 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3530 PCI_VENDOR_ID_MAINPINE, 0x2500,
3531 0, 0, pbn_b0_4_115200 },
3532 { /* RockForceD8 */
3533 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3534 PCI_VENDOR_ID_MAINPINE, 0x2600,
3535 0, 0, pbn_b0_8_115200 },
3536 { /* RockForceF8 */
3537 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3538 PCI_VENDOR_ID_MAINPINE, 0x2700,
3539 0, 0, pbn_b0_8_115200 },
3540 { /* IQ Express D1 */
3541 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3542 PCI_VENDOR_ID_MAINPINE, 0x3000,
3543 0, 0, pbn_b0_1_115200 },
3544 { /* IQ Express F1 */
3545 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3546 PCI_VENDOR_ID_MAINPINE, 0x3100,
3547 0, 0, pbn_b0_1_115200 },
3548 { /* IQ Express D2 */
3549 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3550 PCI_VENDOR_ID_MAINPINE, 0x3200,
3551 0, 0, pbn_b0_2_115200 },
3552 { /* IQ Express F2 */
3553 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3554 PCI_VENDOR_ID_MAINPINE, 0x3300,
3555 0, 0, pbn_b0_2_115200 },
3556 { /* IQ Express D4 */
3557 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3558 PCI_VENDOR_ID_MAINPINE, 0x3400,
3559 0, 0, pbn_b0_4_115200 },
3560 { /* IQ Express F4 */
3561 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3562 PCI_VENDOR_ID_MAINPINE, 0x3500,
3563 0, 0, pbn_b0_4_115200 },
3564 { /* IQ Express D8 */
3565 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3566 PCI_VENDOR_ID_MAINPINE, 0x3C00,
3567 0, 0, pbn_b0_8_115200 },
3568 { /* IQ Express F8 */
3569 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3570 PCI_VENDOR_ID_MAINPINE, 0x3D00,
3571 0, 0, pbn_b0_8_115200 },
3572
3573
3574 /*
3575 * PA Semi PA6T-1682M on-chip UART
3576 */
3577 { PCI_VENDOR_ID_PASEMI, 0xa004,
3578 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3579 pbn_pasemi_1682M },
3580
3581 /*
3582 * National Instruments
3583 */
3584 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI23216,
3585 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3586 pbn_b1_16_115200 },
3587 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2328,
3588 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3589 pbn_b1_8_115200 },
3590 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2324,
3591 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3592 pbn_b1_bt_4_115200 },
3593 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2322,
3594 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3595 pbn_b1_bt_2_115200 },
3596 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2324I,
3597 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3598 pbn_b1_bt_4_115200 },
3599 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2322I,
3600 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3601 pbn_b1_bt_2_115200 },
3602 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_23216,
3603 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3604 pbn_b1_16_115200 },
3605 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_2328,
3606 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3607 pbn_b1_8_115200 },
3608 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_2324,
3609 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3610 pbn_b1_bt_4_115200 },
3611 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_2322,
3612 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3613 pbn_b1_bt_2_115200 },
3614 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8422_2324,
3615 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3616 pbn_b1_bt_4_115200 },
3617 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8422_2322,
3618 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3619 pbn_b1_bt_2_115200 },
3620 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_2322,
3621 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3622 pbn_ni8430_2 },
3623 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_2322,
3624 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3625 pbn_ni8430_2 },
3626 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_2324,
3627 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3628 pbn_ni8430_4 },
3629 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_2324,
3630 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3631 pbn_ni8430_4 },
3632 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_2328,
3633 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3634 pbn_ni8430_8 },
3635 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_2328,
3636 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3637 pbn_ni8430_8 },
3638 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_23216,
3639 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3640 pbn_ni8430_16 },
3641 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_23216,
3642 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3643 pbn_ni8430_16 },
3644 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8432_2322,
3645 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3646 pbn_ni8430_2 },
3647 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8432_2322,
3648 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3649 pbn_ni8430_2 },
3650 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8432_2324,
3651 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3652 pbn_ni8430_4 },
3653 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8432_2324,
3654 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3655 pbn_ni8430_4 },
3656
3657 /*
3658 * ADDI-DATA GmbH communication cards <info@addi-data.com>
3659 */
3660 { PCI_VENDOR_ID_ADDIDATA,
3661 PCI_DEVICE_ID_ADDIDATA_APCI7500,
3662 PCI_ANY_ID,
3663 PCI_ANY_ID,
3664 0,
3665 0,
3666 pbn_b0_4_115200 },
3667
3668 { PCI_VENDOR_ID_ADDIDATA,
3669 PCI_DEVICE_ID_ADDIDATA_APCI7420,
3670 PCI_ANY_ID,
3671 PCI_ANY_ID,
3672 0,
3673 0,
3674 pbn_b0_2_115200 },
3675
3676 { PCI_VENDOR_ID_ADDIDATA,
3677 PCI_DEVICE_ID_ADDIDATA_APCI7300,
3678 PCI_ANY_ID,
3679 PCI_ANY_ID,
3680 0,
3681 0,
3682 pbn_b0_1_115200 },
3683
3684 { PCI_VENDOR_ID_ADDIDATA_OLD,
3685 PCI_DEVICE_ID_ADDIDATA_APCI7800,
3686 PCI_ANY_ID,
3687 PCI_ANY_ID,
3688 0,
3689 0,
3690 pbn_b1_8_115200 },
3691
3692 { PCI_VENDOR_ID_ADDIDATA,
3693 PCI_DEVICE_ID_ADDIDATA_APCI7500_2,
3694 PCI_ANY_ID,
3695 PCI_ANY_ID,
3696 0,
3697 0,
3698 pbn_b0_4_115200 },
3699
3700 { PCI_VENDOR_ID_ADDIDATA,
3701 PCI_DEVICE_ID_ADDIDATA_APCI7420_2,
3702 PCI_ANY_ID,
3703 PCI_ANY_ID,
3704 0,
3705 0,
3706 pbn_b0_2_115200 },
3707
3708 { PCI_VENDOR_ID_ADDIDATA,
3709 PCI_DEVICE_ID_ADDIDATA_APCI7300_2,
3710 PCI_ANY_ID,
3711 PCI_ANY_ID,
3712 0,
3713 0,
3714 pbn_b0_1_115200 },
3715
3716 { PCI_VENDOR_ID_ADDIDATA,
3717 PCI_DEVICE_ID_ADDIDATA_APCI7500_3,
3718 PCI_ANY_ID,
3719 PCI_ANY_ID,
3720 0,
3721 0,
3722 pbn_b0_4_115200 },
3723
3724 { PCI_VENDOR_ID_ADDIDATA,
3725 PCI_DEVICE_ID_ADDIDATA_APCI7420_3,
3726 PCI_ANY_ID,
3727 PCI_ANY_ID,
3728 0,
3729 0,
3730 pbn_b0_2_115200 },
3731
3732 { PCI_VENDOR_ID_ADDIDATA,
3733 PCI_DEVICE_ID_ADDIDATA_APCI7300_3,
3734 PCI_ANY_ID,
3735 PCI_ANY_ID,
3736 0,
3737 0,
3738 pbn_b0_1_115200 },
3739
3740 { PCI_VENDOR_ID_ADDIDATA,
3741 PCI_DEVICE_ID_ADDIDATA_APCI7800_3,
3742 PCI_ANY_ID,
3743 PCI_ANY_ID,
3744 0,
3745 0,
3746 pbn_b0_8_115200 },
3747
3748 { PCI_VENDOR_ID_ADDIDATA,
3749 PCI_DEVICE_ID_ADDIDATA_APCIe7500,
3750 PCI_ANY_ID,
3751 PCI_ANY_ID,
3752 0,
3753 0,
3754 pbn_ADDIDATA_PCIe_4_3906250 },
3755
3756 { PCI_VENDOR_ID_ADDIDATA,
3757 PCI_DEVICE_ID_ADDIDATA_APCIe7420,
3758 PCI_ANY_ID,
3759 PCI_ANY_ID,
3760 0,
3761 0,
3762 pbn_ADDIDATA_PCIe_2_3906250 },
3763
3764 { PCI_VENDOR_ID_ADDIDATA,
3765 PCI_DEVICE_ID_ADDIDATA_APCIe7300,
3766 PCI_ANY_ID,
3767 PCI_ANY_ID,
3768 0,
3769 0,
3770 pbn_ADDIDATA_PCIe_1_3906250 },
3771
3772 { PCI_VENDOR_ID_ADDIDATA,
3773 PCI_DEVICE_ID_ADDIDATA_APCIe7800,
3774 PCI_ANY_ID,
3775 PCI_ANY_ID,
3776 0,
3777 0,
3778 pbn_ADDIDATA_PCIe_8_3906250 },
3779
3780 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9835,
3781 PCI_VENDOR_ID_IBM, 0x0299,
3782 0, 0, pbn_b0_bt_2_115200 },
3783
3784 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9901,
3785 0xA000, 0x1000,
3786 0, 0, pbn_b0_1_115200 },
3787
3788 /*
3789 * Best Connectivity PCI Multi I/O cards
3790 */
3791
3792 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9865,
3793 0xA000, 0x1000,
3794 0, 0, pbn_b0_1_115200 },
3795
3796 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9865,
3797 0xA000, 0x3004,
3798 0, 0, pbn_b0_bt_4_115200 },
3799 /* Intel CE4100 */
3800 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CE4100_UART,
3801 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3802 pbn_ce4100_1_115200 },
3803
3804
3805 /*
3806 * These entries match devices with class COMMUNICATION_SERIAL,
3807 * COMMUNICATION_MODEM or COMMUNICATION_MULTISERIAL
3808 */
3809 { PCI_ANY_ID, PCI_ANY_ID,
3810 PCI_ANY_ID, PCI_ANY_ID,
3811 PCI_CLASS_COMMUNICATION_SERIAL << 8,
3812 0xffff00, pbn_default },
3813 { PCI_ANY_ID, PCI_ANY_ID,
3814 PCI_ANY_ID, PCI_ANY_ID,
3815 PCI_CLASS_COMMUNICATION_MODEM << 8,
3816 0xffff00, pbn_default },
3817 { PCI_ANY_ID, PCI_ANY_ID,
3818 PCI_ANY_ID, PCI_ANY_ID,
3819 PCI_CLASS_COMMUNICATION_MULTISERIAL << 8,
3820 0xffff00, pbn_default },
3821 { 0, }
3822};
3823
3824static struct pci_driver serial_pci_driver = {
3825 .name = "serial",
3826 .probe = pciserial_init_one,
3827 .remove = __devexit_p(pciserial_remove_one),
3828#ifdef CONFIG_PM
3829 .suspend = pciserial_suspend_one,
3830 .resume = pciserial_resume_one,
3831#endif
3832 .id_table = serial_pci_tbl,
3833};
3834
3835static int __init serial8250_pci_init(void)
3836{
3837 return pci_register_driver(&serial_pci_driver);
3838}
3839
3840static void __exit serial8250_pci_exit(void)
3841{
3842 pci_unregister_driver(&serial_pci_driver);
3843}
3844
3845module_init(serial8250_pci_init);
3846module_exit(serial8250_pci_exit);
3847
3848MODULE_LICENSE("GPL");
3849MODULE_DESCRIPTION("Generic 8250/16x50 PCI serial probe module");
3850MODULE_DEVICE_TABLE(pci, serial_pci_tbl);
diff --git a/drivers/tty/serial/8250_pnp.c b/drivers/tty/serial/8250_pnp.c
new file mode 100644
index 000000000000..4822cb50cd0f
--- /dev/null
+++ b/drivers/tty/serial/8250_pnp.c
@@ -0,0 +1,523 @@
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#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/pci.h>
19#include <linux/pnp.h>
20#include <linux/string.h>
21#include <linux/kernel.h>
22#include <linux/serial_core.h>
23#include <linux/bitops.h>
24
25#include <asm/byteorder.h>
26
27#include "8250.h"
28
29#define UNKNOWN_DEV 0x3000
30
31
32static const struct pnp_device_id pnp_dev_table[] = {
33 /* Archtek America Corp. */
34 /* Archtek SmartLink Modem 3334BT Plug & Play */
35 { "AAC000F", 0 },
36 /* Anchor Datacomm BV */
37 /* SXPro 144 External Data Fax Modem Plug & Play */
38 { "ADC0001", 0 },
39 /* SXPro 288 External Data Fax Modem Plug & Play */
40 { "ADC0002", 0 },
41 /* PROLiNK 1456VH ISA PnP K56flex Fax Modem */
42 { "AEI0250", 0 },
43 /* Actiontec ISA PNP 56K X2 Fax Modem */
44 { "AEI1240", 0 },
45 /* Rockwell 56K ACF II Fax+Data+Voice Modem */
46 { "AKY1021", 0 /*SPCI_FL_NO_SHIRQ*/ },
47 /* AZT3005 PnP SOUND DEVICE */
48 { "AZT4001", 0 },
49 /* Best Data Products Inc. Smart One 336F PnP Modem */
50 { "BDP3336", 0 },
51 /* Boca Research */
52 /* Boca Complete Ofc Communicator 14.4 Data-FAX */
53 { "BRI0A49", 0 },
54 /* Boca Research 33,600 ACF Modem */
55 { "BRI1400", 0 },
56 /* Boca 33.6 Kbps Internal FD34FSVD */
57 { "BRI3400", 0 },
58 /* Boca 33.6 Kbps Internal FD34FSVD */
59 { "BRI0A49", 0 },
60 /* Best Data Products Inc. Smart One 336F PnP Modem */
61 { "BDP3336", 0 },
62 /* Computer Peripherals Inc */
63 /* EuroViVa CommCenter-33.6 SP PnP */
64 { "CPI4050", 0 },
65 /* Creative Labs */
66 /* Creative Labs Phone Blaster 28.8 DSVD PnP Voice */
67 { "CTL3001", 0 },
68 /* Creative Labs Modem Blaster 28.8 DSVD PnP Voice */
69 { "CTL3011", 0 },
70 /* Davicom ISA 33.6K Modem */
71 { "DAV0336", 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 /* Archtek SmartLink Modem 3334BRV 33.6K Data Fax Voice */
93 { "GVC0303", 0 },
94 /* Hayes */
95 /* Hayes Optima 288 V.34-V.FC + FAX + Voice Plug & Play */
96 { "HAY0001", 0 },
97 /* Hayes Optima 336 V.34 + FAX + Voice PnP */
98 { "HAY000C", 0 },
99 /* Hayes Optima 336B V.34 + FAX + Voice PnP */
100 { "HAY000D", 0 },
101 /* Hayes Accura 56K Ext Fax Modem PnP */
102 { "HAY5670", 0 },
103 /* Hayes Accura 56K Ext Fax Modem PnP */
104 { "HAY5674", 0 },
105 /* Hayes Accura 56K Fax Modem PnP */
106 { "HAY5675", 0 },
107 /* Hayes 288, V.34 + FAX */
108 { "HAYF000", 0 },
109 /* Hayes Optima 288 V.34 + FAX + Voice, Plug & Play */
110 { "HAYF001", 0 },
111 /* IBM */
112 /* IBM Thinkpad 701 Internal Modem Voice */
113 { "IBM0033", 0 },
114 /* Intertex */
115 /* Intertex 28k8 33k6 Voice EXT PnP */
116 { "IXDC801", 0 },
117 /* Intertex 33k6 56k Voice EXT PnP */
118 { "IXDC901", 0 },
119 /* Intertex 28k8 33k6 Voice SP EXT PnP */
120 { "IXDD801", 0 },
121 /* Intertex 33k6 56k Voice SP EXT PnP */
122 { "IXDD901", 0 },
123 /* Intertex 28k8 33k6 Voice SP INT PnP */
124 { "IXDF401", 0 },
125 /* Intertex 28k8 33k6 Voice SP EXT PnP */
126 { "IXDF801", 0 },
127 /* Intertex 33k6 56k Voice SP EXT PnP */
128 { "IXDF901", 0 },
129 /* Kortex International */
130 /* KORTEX 28800 Externe PnP */
131 { "KOR4522", 0 },
132 /* KXPro 33.6 Vocal ASVD PnP */
133 { "KORF661", 0 },
134 /* Lasat */
135 /* LASAT Internet 33600 PnP */
136 { "LAS4040", 0 },
137 /* Lasat Safire 560 PnP */
138 { "LAS4540", 0 },
139 /* Lasat Safire 336 PnP */
140 { "LAS5440", 0 },
141 /* Microcom, Inc. */
142 /* Microcom TravelPorte FAST V.34 Plug & Play */
143 { "MNP0281", 0 },
144 /* Microcom DeskPorte V.34 FAST or FAST+ Plug & Play */
145 { "MNP0336", 0 },
146 /* Microcom DeskPorte FAST EP 28.8 Plug & Play */
147 { "MNP0339", 0 },
148 /* Microcom DeskPorte 28.8P Plug & Play */
149 { "MNP0342", 0 },
150 /* Microcom DeskPorte FAST ES 28.8 Plug & Play */
151 { "MNP0500", 0 },
152 /* Microcom DeskPorte FAST ES 28.8 Plug & Play */
153 { "MNP0501", 0 },
154 /* Microcom DeskPorte 28.8S Internal Plug & Play */
155 { "MNP0502", 0 },
156 /* Motorola */
157 /* Motorola BitSURFR Plug & Play */
158 { "MOT1105", 0 },
159 /* Motorola TA210 Plug & Play */
160 { "MOT1111", 0 },
161 /* Motorola HMTA 200 (ISDN) Plug & Play */
162 { "MOT1114", 0 },
163 /* Motorola BitSURFR Plug & Play */
164 { "MOT1115", 0 },
165 /* Motorola Lifestyle 28.8 Internal */
166 { "MOT1190", 0 },
167 /* Motorola V.3400 Plug & Play */
168 { "MOT1501", 0 },
169 /* Motorola Lifestyle 28.8 V.34 Plug & Play */
170 { "MOT1502", 0 },
171 /* Motorola Power 28.8 V.34 Plug & Play */
172 { "MOT1505", 0 },
173 /* Motorola ModemSURFR External 28.8 Plug & Play */
174 { "MOT1509", 0 },
175 /* Motorola Premier 33.6 Desktop Plug & Play */
176 { "MOT150A", 0 },
177 /* Motorola VoiceSURFR 56K External PnP */
178 { "MOT150F", 0 },
179 /* Motorola ModemSURFR 56K External PnP */
180 { "MOT1510", 0 },
181 /* Motorola ModemSURFR 56K Internal PnP */
182 { "MOT1550", 0 },
183 /* Motorola ModemSURFR Internal 28.8 Plug & Play */
184 { "MOT1560", 0 },
185 /* Motorola Premier 33.6 Internal Plug & Play */
186 { "MOT1580", 0 },
187 /* Motorola OnlineSURFR 28.8 Internal Plug & Play */
188 { "MOT15B0", 0 },
189 /* Motorola VoiceSURFR 56K Internal PnP */
190 { "MOT15F0", 0 },
191 /* Com 1 */
192 /* Deskline K56 Phone System PnP */
193 { "MVX00A1", 0 },
194 /* PC Rider K56 Phone System PnP */
195 { "MVX00F2", 0 },
196 /* NEC 98NOTE SPEAKER PHONE FAX MODEM(33600bps) */
197 { "nEC8241", 0 },
198 /* Pace 56 Voice Internal Plug & Play Modem */
199 { "PMC2430", 0 },
200 /* Generic */
201 /* Generic standard PC COM port */
202 { "PNP0500", 0 },
203 /* Generic 16550A-compatible COM port */
204 { "PNP0501", 0 },
205 /* Compaq 14400 Modem */
206 { "PNPC000", 0 },
207 /* Compaq 2400/9600 Modem */
208 { "PNPC001", 0 },
209 /* Dial-Up Networking Serial Cable between 2 PCs */
210 { "PNPC031", 0 },
211 /* Dial-Up Networking Parallel Cable between 2 PCs */
212 { "PNPC032", 0 },
213 /* Standard 9600 bps Modem */
214 { "PNPC100", 0 },
215 /* Standard 14400 bps Modem */
216 { "PNPC101", 0 },
217 /* Standard 28800 bps Modem*/
218 { "PNPC102", 0 },
219 /* Standard Modem*/
220 { "PNPC103", 0 },
221 /* Standard 9600 bps Modem*/
222 { "PNPC104", 0 },
223 /* Standard 14400 bps Modem*/
224 { "PNPC105", 0 },
225 /* Standard 28800 bps Modem*/
226 { "PNPC106", 0 },
227 /* Standard Modem */
228 { "PNPC107", 0 },
229 /* Standard 9600 bps Modem */
230 { "PNPC108", 0 },
231 /* Standard 14400 bps Modem */
232 { "PNPC109", 0 },
233 /* Standard 28800 bps Modem */
234 { "PNPC10A", 0 },
235 /* Standard Modem */
236 { "PNPC10B", 0 },
237 /* Standard 9600 bps Modem */
238 { "PNPC10C", 0 },
239 /* Standard 14400 bps Modem */
240 { "PNPC10D", 0 },
241 /* Standard 28800 bps Modem */
242 { "PNPC10E", 0 },
243 /* Standard Modem */
244 { "PNPC10F", 0 },
245 /* Standard PCMCIA Card Modem */
246 { "PNP2000", 0 },
247 /* Rockwell */
248 /* Modular Technology */
249 /* Rockwell 33.6 DPF Internal PnP */
250 /* Modular Technology 33.6 Internal PnP */
251 { "ROK0030", 0 },
252 /* Kortex International */
253 /* KORTEX 14400 Externe PnP */
254 { "ROK0100", 0 },
255 /* Rockwell 28.8 */
256 { "ROK4120", 0 },
257 /* Viking Components, Inc */
258 /* Viking 28.8 INTERNAL Fax+Data+Voice PnP */
259 { "ROK4920", 0 },
260 /* Rockwell */
261 /* British Telecom */
262 /* Modular Technology */
263 /* Rockwell 33.6 DPF External PnP */
264 /* BT Prologue 33.6 External PnP */
265 /* Modular Technology 33.6 External PnP */
266 { "RSS00A0", 0 },
267 /* Viking 56K FAX INT */
268 { "RSS0262", 0 },
269 /* K56 par,VV,Voice,Speakphone,AudioSpan,PnP */
270 { "RSS0250", 0 },
271 /* SupraExpress 28.8 Data/Fax PnP modem */
272 { "SUP1310", 0 },
273 /* SupraExpress 336i PnP Voice Modem */
274 { "SUP1381", 0 },
275 /* SupraExpress 33.6 Data/Fax PnP modem */
276 { "SUP1421", 0 },
277 /* SupraExpress 33.6 Data/Fax PnP modem */
278 { "SUP1590", 0 },
279 /* SupraExpress 336i Sp ASVD */
280 { "SUP1620", 0 },
281 /* SupraExpress 33.6 Data/Fax PnP modem */
282 { "SUP1760", 0 },
283 /* SupraExpress 56i Sp Intl */
284 { "SUP2171", 0 },
285 /* Phoebe Micro */
286 /* Phoebe Micro 33.6 Data Fax 1433VQH Plug & Play */
287 { "TEX0011", 0 },
288 /* Archtek America Corp. */
289 /* Archtek SmartLink Modem 3334BT Plug & Play */
290 { "UAC000F", 0 },
291 /* 3Com Corp. */
292 /* Gateway Telepath IIvi 33.6 */
293 { "USR0000", 0 },
294 /* U.S. Robotics Sporster 33.6K Fax INT PnP */
295 { "USR0002", 0 },
296 /* Sportster Vi 14.4 PnP FAX Voicemail */
297 { "USR0004", 0 },
298 /* U.S. Robotics 33.6K Voice INT PnP */
299 { "USR0006", 0 },
300 /* U.S. Robotics 33.6K Voice EXT PnP */
301 { "USR0007", 0 },
302 /* U.S. Robotics Courier V.Everything INT PnP */
303 { "USR0009", 0 },
304 /* U.S. Robotics 33.6K Voice INT PnP */
305 { "USR2002", 0 },
306 /* U.S. Robotics 56K Voice INT PnP */
307 { "USR2070", 0 },
308 /* U.S. Robotics 56K Voice EXT PnP */
309 { "USR2080", 0 },
310 /* U.S. Robotics 56K FAX INT */
311 { "USR3031", 0 },
312 /* U.S. Robotics 56K FAX INT */
313 { "USR3050", 0 },
314 /* U.S. Robotics 56K Voice INT PnP */
315 { "USR3070", 0 },
316 /* U.S. Robotics 56K Voice EXT PnP */
317 { "USR3080", 0 },
318 /* U.S. Robotics 56K Voice INT PnP */
319 { "USR3090", 0 },
320 /* U.S. Robotics 56K Message */
321 { "USR9100", 0 },
322 /* U.S. Robotics 56K FAX EXT PnP*/
323 { "USR9160", 0 },
324 /* U.S. Robotics 56K FAX INT PnP*/
325 { "USR9170", 0 },
326 /* U.S. Robotics 56K Voice EXT PnP*/
327 { "USR9180", 0 },
328 /* U.S. Robotics 56K Voice INT PnP*/
329 { "USR9190", 0 },
330 /* Wacom tablets */
331 { "WACFXXX", 0 },
332 /* Compaq touchscreen */
333 { "FPI2002", 0 },
334 /* Fujitsu Stylistic touchscreens */
335 { "FUJ02B2", 0 },
336 { "FUJ02B3", 0 },
337 /* Fujitsu Stylistic LT touchscreens */
338 { "FUJ02B4", 0 },
339 /* Passive Fujitsu Stylistic touchscreens */
340 { "FUJ02B6", 0 },
341 { "FUJ02B7", 0 },
342 { "FUJ02B8", 0 },
343 { "FUJ02B9", 0 },
344 { "FUJ02BC", 0 },
345 /* Fujitsu Wacom Tablet PC device */
346 { "FUJ02E5", 0 },
347 /* Fujitsu P-series tablet PC device */
348 { "FUJ02E6", 0 },
349 /* Fujitsu Wacom 2FGT Tablet PC device */
350 { "FUJ02E7", 0 },
351 /* Fujitsu Wacom 1FGT Tablet PC device */
352 { "FUJ02E9", 0 },
353 /*
354 * LG C1 EXPRESS DUAL (C1-PB11A3) touch screen (actually a FUJ02E6 in
355 * disguise)
356 */
357 { "LTS0001", 0 },
358 /* Rockwell's (PORALiNK) 33600 INT PNP */
359 { "WCI0003", 0 },
360 /* Unknown PnP modems */
361 { "PNPCXXX", UNKNOWN_DEV },
362 /* More unknown PnP modems */
363 { "PNPDXXX", UNKNOWN_DEV },
364 { "", 0 }
365};
366
367MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
368
369static char *modem_names[] __devinitdata = {
370 "MODEM", "Modem", "modem", "FAX", "Fax", "fax",
371 "56K", "56k", "K56", "33.6", "28.8", "14.4",
372 "33,600", "28,800", "14,400", "33.600", "28.800", "14.400",
373 "33600", "28800", "14400", "V.90", "V.34", "V.32", NULL
374};
375
376static int __devinit check_name(char *name)
377{
378 char **tmp;
379
380 for (tmp = modem_names; *tmp; tmp++)
381 if (strstr(name, *tmp))
382 return 1;
383
384 return 0;
385}
386
387static int __devinit check_resources(struct pnp_dev *dev)
388{
389 resource_size_t base[] = {0x2f8, 0x3f8, 0x2e8, 0x3e8};
390 int i;
391
392 for (i = 0; i < ARRAY_SIZE(base); i++) {
393 if (pnp_possible_config(dev, IORESOURCE_IO, base[i], 8))
394 return 1;
395 }
396
397 return 0;
398}
399
400/*
401 * Given a complete unknown PnP device, try to use some heuristics to
402 * detect modems. Currently use such heuristic set:
403 * - dev->name or dev->bus->name must contain "modem" substring;
404 * - device must have only one IO region (8 byte long) with base address
405 * 0x2e8, 0x3e8, 0x2f8 or 0x3f8.
406 *
407 * Such detection looks very ugly, but can detect at least some of numerous
408 * PnP modems, alternatively we must hardcode all modems in pnp_devices[]
409 * table.
410 */
411static int __devinit serial_pnp_guess_board(struct pnp_dev *dev, int *flags)
412{
413 if (!(check_name(pnp_dev_name(dev)) ||
414 (dev->card && check_name(dev->card->name))))
415 return -ENODEV;
416
417 if (check_resources(dev))
418 return 0;
419
420 return -ENODEV;
421}
422
423static int __devinit
424serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
425{
426 struct uart_port port;
427 int ret, line, flags = dev_id->driver_data;
428
429 if (flags & UNKNOWN_DEV) {
430 ret = serial_pnp_guess_board(dev, &flags);
431 if (ret < 0)
432 return ret;
433 }
434
435 memset(&port, 0, sizeof(struct uart_port));
436 if (pnp_irq_valid(dev, 0))
437 port.irq = pnp_irq(dev, 0);
438 if (pnp_port_valid(dev, 0)) {
439 port.iobase = pnp_port_start(dev, 0);
440 port.iotype = UPIO_PORT;
441 } else if (pnp_mem_valid(dev, 0)) {
442 port.mapbase = pnp_mem_start(dev, 0);
443 port.iotype = UPIO_MEM;
444 port.flags = UPF_IOREMAP;
445 } else
446 return -ENODEV;
447
448#ifdef SERIAL_DEBUG_PNP
449 printk(KERN_DEBUG
450 "Setup PNP port: port %x, mem 0x%lx, irq %d, type %d\n",
451 port.iobase, port.mapbase, port.irq, port.iotype);
452#endif
453
454 port.flags |= UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
455 if (pnp_irq_flags(dev, 0) & IORESOURCE_IRQ_SHAREABLE)
456 port.flags |= UPF_SHARE_IRQ;
457 port.uartclk = 1843200;
458 port.dev = &dev->dev;
459
460 line = serial8250_register_port(&port);
461 if (line < 0)
462 return -ENODEV;
463
464 pnp_set_drvdata(dev, (void *)((long)line + 1));
465 return 0;
466}
467
468static void __devexit serial_pnp_remove(struct pnp_dev *dev)
469{
470 long line = (long)pnp_get_drvdata(dev);
471 if (line)
472 serial8250_unregister_port(line - 1);
473}
474
475#ifdef CONFIG_PM
476static int serial_pnp_suspend(struct pnp_dev *dev, pm_message_t state)
477{
478 long line = (long)pnp_get_drvdata(dev);
479
480 if (!line)
481 return -ENODEV;
482 serial8250_suspend_port(line - 1);
483 return 0;
484}
485
486static int serial_pnp_resume(struct pnp_dev *dev)
487{
488 long line = (long)pnp_get_drvdata(dev);
489
490 if (!line)
491 return -ENODEV;
492 serial8250_resume_port(line - 1);
493 return 0;
494}
495#else
496#define serial_pnp_suspend NULL
497#define serial_pnp_resume NULL
498#endif /* CONFIG_PM */
499
500static struct pnp_driver serial_pnp_driver = {
501 .name = "serial",
502 .probe = serial_pnp_probe,
503 .remove = __devexit_p(serial_pnp_remove),
504 .suspend = serial_pnp_suspend,
505 .resume = serial_pnp_resume,
506 .id_table = pnp_dev_table,
507};
508
509static int __init serial8250_pnp_init(void)
510{
511 return pnp_register_driver(&serial_pnp_driver);
512}
513
514static void __exit serial8250_pnp_exit(void)
515{
516 pnp_unregister_driver(&serial_pnp_driver);
517}
518
519module_init(serial8250_pnp_init);
520module_exit(serial8250_pnp_exit);
521
522MODULE_LICENSE("GPL");
523MODULE_DESCRIPTION("Generic 8250/16x50 PnP serial driver");
diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
new file mode 100644
index 000000000000..c1df7676a73d
--- /dev/null
+++ b/drivers/tty/serial/Kconfig
@@ -0,0 +1,1598 @@
1#
2# Serial device configuration
3#
4
5menu "Serial drivers"
6 depends on HAS_IOMEM
7
8#
9# The new 8250/16550 serial drivers
10config SERIAL_8250
11 tristate "8250/16550 and compatible serial support"
12 select SERIAL_CORE
13 ---help---
14 This selects whether you want to include the driver for the standard
15 serial ports. The standard answer is Y. People who might say N
16 here are those that are setting up dedicated Ethernet WWW/FTP
17 servers, or users that have one of the various bus mice instead of a
18 serial mouse and don't intend to use their machine's standard serial
19 port for anything. (Note that the Cyclades and Stallion multi
20 serial port drivers do not need this driver built in for them to
21 work.)
22
23 To compile this driver as a module, choose M here: the
24 module will be called 8250.
25 [WARNING: Do not compile this driver as a module if you are using
26 non-standard serial ports, since the configuration information will
27 be lost when the driver is unloaded. This limitation may be lifted
28 in the future.]
29
30 BTW1: If you have a mouseman serial mouse which is not recognized by
31 the X window system, try running gpm first.
32
33 BTW2: If you intend to use a software modem (also called Winmodem)
34 under Linux, forget it. These modems are crippled and require
35 proprietary drivers which are only available under Windows.
36
37 Most people will say Y or M here, so that they can use serial mice,
38 modems and similar devices connecting to the standard serial ports.
39
40config SERIAL_8250_CONSOLE
41 bool "Console on 8250/16550 and compatible serial port"
42 depends on SERIAL_8250=y
43 select SERIAL_CORE_CONSOLE
44 ---help---
45 If you say Y here, it will be possible to use a serial port as the
46 system console (the system console is the device which receives all
47 kernel messages and warnings and which allows logins in single user
48 mode). This could be useful if some terminal or printer is connected
49 to that serial port.
50
51 Even if you say Y here, the currently visible virtual console
52 (/dev/tty0) will still be used as the system console by default, but
53 you can alter that using a kernel command line option such as
54 "console=ttyS1". (Try "man bootparam" or see the documentation of
55 your boot loader (grub or lilo or loadlin) about how to pass options
56 to the kernel at boot time.)
57
58 If you don't have a VGA card installed and you say Y here, the
59 kernel will automatically use the first serial line, /dev/ttyS0, as
60 system console.
61
62 You can set that using a kernel command line option such as
63 "console=uart8250,io,0x3f8,9600n8"
64 "console=uart8250,mmio,0xff5e0000,115200n8".
65 and it will switch to normal serial console when the corresponding
66 port is ready.
67 "earlycon=uart8250,io,0x3f8,9600n8"
68 "earlycon=uart8250,mmio,0xff5e0000,115200n8".
69 it will not only setup early console.
70
71 If unsure, say N.
72
73config FIX_EARLYCON_MEM
74 bool
75 depends on X86
76 default y
77
78config SERIAL_8250_GSC
79 tristate
80 depends on SERIAL_8250 && GSC
81 default SERIAL_8250
82
83config SERIAL_8250_PCI
84 tristate "8250/16550 PCI device support" if EMBEDDED
85 depends on SERIAL_8250 && PCI
86 default SERIAL_8250
87 help
88 This builds standard PCI serial support. You may be able to
89 disable this feature if you only need legacy serial support.
90 Saves about 9K.
91
92config SERIAL_8250_PNP
93 tristate "8250/16550 PNP device support" if EMBEDDED
94 depends on SERIAL_8250 && PNP
95 default SERIAL_8250
96 help
97 This builds standard PNP serial support. You may be able to
98 disable this feature if you only need legacy serial support.
99
100config SERIAL_8250_HP300
101 tristate
102 depends on SERIAL_8250 && HP300
103 default SERIAL_8250
104
105config SERIAL_8250_CS
106 tristate "8250/16550 PCMCIA device support"
107 depends on PCMCIA && SERIAL_8250
108 ---help---
109 Say Y here to enable support for 16-bit PCMCIA serial devices,
110 including serial port cards, modems, and the modem functions of
111 multi-function Ethernet/modem cards. (PCMCIA- or PC-cards are
112 credit-card size devices often used with laptops.)
113
114 To compile this driver as a module, choose M here: the
115 module will be called serial_cs.
116
117 If unsure, say N.
118
119config SERIAL_8250_NR_UARTS
120 int "Maximum number of 8250/16550 serial ports"
121 depends on SERIAL_8250
122 default "4"
123 help
124 Set this to the number of serial ports you want the driver
125 to support. This includes any ports discovered via ACPI or
126 PCI enumeration and any ports that may be added at run-time
127 via hot-plug, or any ISA multi-port serial cards.
128
129config SERIAL_8250_RUNTIME_UARTS
130 int "Number of 8250/16550 serial ports to register at runtime"
131 depends on SERIAL_8250
132 range 0 SERIAL_8250_NR_UARTS
133 default "4"
134 help
135 Set this to the maximum number of serial ports you want
136 the kernel to register at boot time. This can be overridden
137 with the module parameter "nr_uarts", or boot-time parameter
138 8250.nr_uarts
139
140config SERIAL_8250_EXTENDED
141 bool "Extended 8250/16550 serial driver options"
142 depends on SERIAL_8250
143 help
144 If you wish to use any non-standard features of the standard "dumb"
145 driver, say Y here. This includes HUB6 support, shared serial
146 interrupts, special multiport support, support for more than the
147 four COM 1/2/3/4 boards, etc.
148
149 Note that the answer to this question won't directly affect the
150 kernel: saying N will just cause the configurator to skip all
151 the questions about serial driver options. If unsure, say N.
152
153config SERIAL_8250_MANY_PORTS
154 bool "Support more than 4 legacy serial ports"
155 depends on SERIAL_8250_EXTENDED && !IA64
156 help
157 Say Y here if you have dumb serial boards other than the four
158 standard COM 1/2/3/4 ports. This may happen if you have an AST
159 FourPort, Accent Async, Boca (read the Boca mini-HOWTO, available
160 from <http://www.tldp.org/docs.html#howto>), or other custom
161 serial port hardware which acts similar to standard serial port
162 hardware. If you only use the standard COM 1/2/3/4 ports, you can
163 say N here to save some memory. You can also say Y if you have an
164 "intelligent" multiport card such as Cyclades, Digiboards, etc.
165
166#
167# Multi-port serial cards
168#
169
170config SERIAL_8250_FOURPORT
171 tristate "Support Fourport cards"
172 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
173 help
174 Say Y here if you have an AST FourPort serial board.
175
176 To compile this driver as a module, choose M here: the module
177 will be called 8250_fourport.
178
179config SERIAL_8250_ACCENT
180 tristate "Support Accent cards"
181 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
182 help
183 Say Y here if you have an Accent Async serial board.
184
185 To compile this driver as a module, choose M here: the module
186 will be called 8250_accent.
187
188config SERIAL_8250_BOCA
189 tristate "Support Boca cards"
190 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
191 help
192 Say Y here if you have a Boca serial board. Please read the Boca
193 mini-HOWTO, available from <http://www.tldp.org/docs.html#howto>
194
195 To compile this driver as a module, choose M here: the module
196 will be called 8250_boca.
197
198config SERIAL_8250_EXAR_ST16C554
199 tristate "Support Exar ST16C554/554D Quad UART"
200 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
201 help
202 The Uplogix Envoy TU301 uses this Exar Quad UART. If you are
203 tinkering with your Envoy TU301, or have a machine with this UART,
204 say Y here.
205
206 To compile this driver as a module, choose M here: the module
207 will be called 8250_exar_st16c554.
208
209config SERIAL_8250_HUB6
210 tristate "Support Hub6 cards"
211 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
212 help
213 Say Y here if you have a HUB6 serial board.
214
215 To compile this driver as a module, choose M here: the module
216 will be called 8250_hub6.
217
218config SERIAL_8250_SHARE_IRQ
219 bool "Support for sharing serial interrupts"
220 depends on SERIAL_8250_EXTENDED
221 help
222 Some serial boards have hardware support which allows multiple dumb
223 serial ports on the same board to share a single IRQ. To enable
224 support for this in the serial driver, say Y here.
225
226config SERIAL_8250_DETECT_IRQ
227 bool "Autodetect IRQ on standard ports (unsafe)"
228 depends on SERIAL_8250_EXTENDED
229 help
230 Say Y here if you want the kernel to try to guess which IRQ
231 to use for your serial port.
232
233 This is considered unsafe; it is far better to configure the IRQ in
234 a boot script using the setserial command.
235
236 If unsure, say N.
237
238config SERIAL_8250_RSA
239 bool "Support RSA serial ports"
240 depends on SERIAL_8250_EXTENDED
241 help
242 ::: To be written :::
243
244config SERIAL_8250_MCA
245 tristate "Support 8250-type ports on MCA buses"
246 depends on SERIAL_8250 != n && MCA
247 help
248 Say Y here if you have a MCA serial ports.
249
250 To compile this driver as a module, choose M here: the module
251 will be called 8250_mca.
252
253config SERIAL_8250_ACORN
254 tristate "Acorn expansion card serial port support"
255 depends on ARCH_ACORN && SERIAL_8250
256 help
257 If you have an Atomwide Serial card or Serial Port card for an Acorn
258 system, say Y to this option. The driver can handle 1, 2, or 3 port
259 cards. If unsure, say N.
260
261config SERIAL_8250_RM9K
262 bool "Support for MIPS RM9xxx integrated serial port"
263 depends on SERIAL_8250 != n && SERIAL_RM9000
264 select SERIAL_8250_SHARE_IRQ
265 help
266 Selecting this option will add support for the integrated serial
267 port hardware found on MIPS RM9122 and similar processors.
268 If unsure, say N.
269
270comment "Non-8250 serial port support"
271
272config SERIAL_AMBA_PL010
273 tristate "ARM AMBA PL010 serial port support"
274 depends on ARM_AMBA && (BROKEN || !ARCH_VERSATILE)
275 select SERIAL_CORE
276 help
277 This selects the ARM(R) AMBA(R) PrimeCell PL010 UART. If you have
278 an Integrator/AP or Integrator/PP2 platform, or if you have a
279 Cirrus Logic EP93xx CPU, say Y or M here.
280
281 If unsure, say N.
282
283config SERIAL_AMBA_PL010_CONSOLE
284 bool "Support for console on AMBA serial port"
285 depends on SERIAL_AMBA_PL010=y
286 select SERIAL_CORE_CONSOLE
287 ---help---
288 Say Y here if you wish to use an AMBA PrimeCell UART as the system
289 console (the system console is the device which receives all kernel
290 messages and warnings and which allows logins in single user mode).
291
292 Even if you say Y here, the currently visible framebuffer console
293 (/dev/tty0) will still be used as the system console by default, but
294 you can alter that using a kernel command line option such as
295 "console=ttyAM0". (Try "man bootparam" or see the documentation of
296 your boot loader (lilo or loadlin) about how to pass options to the
297 kernel at boot time.)
298
299config SERIAL_AMBA_PL011
300 tristate "ARM AMBA PL011 serial port support"
301 depends on ARM_AMBA
302 select SERIAL_CORE
303 help
304 This selects the ARM(R) AMBA(R) PrimeCell PL011 UART. If you have
305 an Integrator/PP2, Integrator/CP or Versatile platform, say Y or M
306 here.
307
308 If unsure, say N.
309
310config SERIAL_AMBA_PL011_CONSOLE
311 bool "Support for console on AMBA serial port"
312 depends on SERIAL_AMBA_PL011=y
313 select SERIAL_CORE_CONSOLE
314 ---help---
315 Say Y here if you wish to use an AMBA PrimeCell UART as the system
316 console (the system console is the device which receives all kernel
317 messages and warnings and which allows logins in single user mode).
318
319 Even if you say Y here, the currently visible framebuffer console
320 (/dev/tty0) will still be used as the system console by default, but
321 you can alter that using a kernel command line option such as
322 "console=ttyAMA0". (Try "man bootparam" or see the documentation of
323 your boot loader (lilo or loadlin) about how to pass options to the
324 kernel at boot time.)
325
326config SERIAL_SB1250_DUART
327 tristate "BCM1xxx on-chip DUART serial support"
328 depends on SIBYTE_SB1xxx_SOC=y
329 select SERIAL_CORE
330 default y
331 ---help---
332 Support for the asynchronous serial interface (DUART) included in
333 the BCM1250 and derived System-On-a-Chip (SOC) devices. Note that
334 the letter D in DUART stands for "dual", which is how the device
335 is implemented. Depending on the SOC configuration there may be
336 one or more DUARTs available of which all are handled.
337
338 If unsure, say Y. To compile this driver as a module, choose M here:
339 the module will be called sb1250-duart.
340
341config SERIAL_SB1250_DUART_CONSOLE
342 bool "Support for console on a BCM1xxx DUART serial port"
343 depends on SERIAL_SB1250_DUART=y
344 select SERIAL_CORE_CONSOLE
345 default y
346 ---help---
347 If you say Y here, it will be possible to use a serial port as the
348 system console (the system console is the device which receives all
349 kernel messages and warnings and which allows logins in single user
350 mode).
351
352 If unsure, say Y.
353
354config SERIAL_ATMEL
355 bool "AT91 / AT32 on-chip serial port support"
356 depends on (ARM && ARCH_AT91) || AVR32
357 select SERIAL_CORE
358 help
359 This enables the driver for the on-chip UARTs of the Atmel
360 AT91 and AT32 processors.
361
362config SERIAL_ATMEL_CONSOLE
363 bool "Support for console on AT91 / AT32 serial port"
364 depends on SERIAL_ATMEL=y
365 select SERIAL_CORE_CONSOLE
366 help
367 Say Y here if you wish to use an on-chip UART on a Atmel
368 AT91 or AT32 processor as the system console (the system
369 console is the device which receives all kernel messages and
370 warnings and which allows logins in single user mode).
371
372config SERIAL_ATMEL_PDC
373 bool "Support DMA transfers on AT91 / AT32 serial port"
374 depends on SERIAL_ATMEL
375 default y
376 help
377 Say Y here if you wish to use the PDC to do DMA transfers to
378 and from the Atmel AT91 / AT32 serial port. In order to
379 actually use DMA transfers, make sure that the use_dma_tx
380 and use_dma_rx members in the atmel_uart_data struct is set
381 appropriately for each port.
382
383 Note that break and error handling currently doesn't work
384 properly when DMA is enabled. Make sure that ports where
385 this matters don't use DMA.
386
387config SERIAL_ATMEL_TTYAT
388 bool "Install as device ttyATn instead of ttySn"
389 depends on SERIAL_ATMEL=y
390 help
391 Say Y here if you wish to have the internal AT91 / AT32 UARTs
392 appear as /dev/ttyATn (major 204, minor starting at 154)
393 instead of the normal /dev/ttySn (major 4, minor starting at
394 64). This is necessary if you also want other UARTs, such as
395 external 8250/16C550 compatible UARTs.
396 The ttySn nodes are legally reserved for the 8250 serial driver
397 but are often misused by other serial drivers.
398
399 To use this, you should create suitable ttyATn device nodes in
400 /dev/, and pass "console=ttyATn" to the kernel.
401
402 Say Y if you have an external 8250/16C550 UART. If unsure, say N.
403
404config SERIAL_KS8695
405 bool "Micrel KS8695 (Centaur) serial port support"
406 depends on ARCH_KS8695
407 select SERIAL_CORE
408 help
409 This selects the Micrel Centaur KS8695 UART. Say Y here.
410
411config SERIAL_KS8695_CONSOLE
412 bool "Support for console on KS8695 (Centaur) serial port"
413 depends on SERIAL_KS8695=y
414 select SERIAL_CORE_CONSOLE
415 help
416 Say Y here if you wish to use a KS8695 (Centaur) UART as the
417 system console (the system console is the device which
418 receives all kernel messages and warnings and which allows
419 logins in single user mode).
420
421config SERIAL_CLPS711X
422 tristate "CLPS711X serial port support"
423 depends on ARM && ARCH_CLPS711X
424 select SERIAL_CORE
425 help
426 ::: To be written :::
427
428config SERIAL_CLPS711X_CONSOLE
429 bool "Support for console on CLPS711X serial port"
430 depends on SERIAL_CLPS711X=y
431 select SERIAL_CORE_CONSOLE
432 help
433 Even if you say Y here, the currently visible virtual console
434 (/dev/tty0) will still be used as the system console by default, but
435 you can alter that using a kernel command line option such as
436 "console=ttyCL1". (Try "man bootparam" or see the documentation of
437 your boot loader (lilo or loadlin) about how to pass options to the
438 kernel at boot time.)
439
440config SERIAL_SAMSUNG
441 tristate "Samsung SoC serial support"
442 depends on ARM && PLAT_SAMSUNG
443 select SERIAL_CORE
444 help
445 Support for the on-chip UARTs on the Samsung S3C24XX series CPUs,
446 providing /dev/ttySAC0, 1 and 2 (note, some machines may not
447 provide all of these ports, depending on how the serial port
448 pins are configured.
449
450config SERIAL_SAMSUNG_UARTS_4
451 bool
452 depends on ARM && PLAT_SAMSUNG
453 default y if CPU_S3C2443
454 help
455 Internal node for the common case of 4 Samsung compatible UARTs
456
457config SERIAL_SAMSUNG_UARTS
458 int
459 depends on ARM && PLAT_SAMSUNG
460 default 2 if ARCH_S3C2400
461 default 6 if ARCH_S5P6450
462 default 4 if SERIAL_SAMSUNG_UARTS_4
463 default 3
464 help
465 Select the number of available UART ports for the Samsung S3C
466 serial driver
467
468config SERIAL_SAMSUNG_DEBUG
469 bool "Samsung SoC serial debug"
470 depends on SERIAL_SAMSUNG && DEBUG_LL
471 help
472 Add support for debugging the serial driver. Since this is
473 generally being used as a console, we use our own output
474 routines that go via the low-level debug printascii()
475 function.
476
477config SERIAL_SAMSUNG_CONSOLE
478 bool "Support for console on Samsung SoC serial port"
479 depends on SERIAL_SAMSUNG=y
480 select SERIAL_CORE_CONSOLE
481 help
482 Allow selection of the S3C24XX on-board serial ports for use as
483 an virtual console.
484
485 Even if you say Y here, the currently visible virtual console
486 (/dev/tty0) will still be used as the system console by default, but
487 you can alter that using a kernel command line option such as
488 "console=ttySACx". (Try "man bootparam" or see the documentation of
489 your boot loader about how to pass options to the kernel at
490 boot time.)
491
492config SERIAL_S3C2400
493 tristate "Samsung S3C2410 Serial port support"
494 depends on ARM && SERIAL_SAMSUNG && CPU_S3C2400
495 default y if CPU_S3C2400
496 help
497 Serial port support for the Samsung S3C2400 SoC
498
499config SERIAL_S3C2410
500 tristate "Samsung S3C2410 Serial port support"
501 depends on SERIAL_SAMSUNG && CPU_S3C2410
502 default y if CPU_S3C2410
503 help
504 Serial port support for the Samsung S3C2410 SoC
505
506config SERIAL_S3C2412
507 tristate "Samsung S3C2412/S3C2413 Serial port support"
508 depends on SERIAL_SAMSUNG && CPU_S3C2412
509 default y if CPU_S3C2412
510 help
511 Serial port support for the Samsung S3C2412 and S3C2413 SoC
512
513config SERIAL_S3C2440
514 tristate "Samsung S3C2440/S3C2442/S3C2416 Serial port support"
515 depends on SERIAL_SAMSUNG && (CPU_S3C2440 || CPU_S3C2442 || CPU_S3C2416)
516 default y if CPU_S3C2440
517 default y if CPU_S3C2442
518 select SERIAL_SAMSUNG_UARTS_4 if CPU_S3C2416
519 help
520 Serial port support for the Samsung S3C2440, S3C2416 and S3C2442 SoC
521
522config SERIAL_S3C24A0
523 tristate "Samsung S3C24A0 Serial port support"
524 depends on SERIAL_SAMSUNG && CPU_S3C24A0
525 default y if CPU_S3C24A0
526 help
527 Serial port support for the Samsung S3C24A0 SoC
528
529config SERIAL_S3C6400
530 tristate "Samsung S3C6400/S3C6410/S5P6440/S5P6450/S5PC100 Serial port support"
531 depends on SERIAL_SAMSUNG && (CPU_S3C6400 || CPU_S3C6410 || CPU_S5P6440 || CPU_S5P6450 || CPU_S5PC100)
532 select SERIAL_SAMSUNG_UARTS_4
533 default y
534 help
535 Serial port support for the Samsung S3C6400, S3C6410, S5P6440, S5P6450
536 and S5PC100 SoCs
537
538config SERIAL_S5PV210
539 tristate "Samsung S5PV210 Serial port support"
540 depends on SERIAL_SAMSUNG && (CPU_S5PV210 || CPU_S5P6442 || CPU_S5PV310)
541 select SERIAL_SAMSUNG_UARTS_4 if (CPU_S5PV210 || CPU_S5PV310)
542 default y
543 help
544 Serial port support for Samsung's S5P Family of SoC's
545
546
547config SERIAL_MAX3100
548 tristate "MAX3100 support"
549 depends on SPI
550 select SERIAL_CORE
551 help
552 MAX3100 chip support
553
554config SERIAL_MAX3107
555 tristate "MAX3107 support"
556 depends on SPI
557 select SERIAL_CORE
558 help
559 MAX3107 chip support
560
561config SERIAL_MAX3107_AAVA
562 tristate "MAX3107 AAVA platform support"
563 depends on X86_MRST && SERIAL_MAX3107 && GPIOLIB
564 select SERIAL_CORE
565 help
566 Support for the MAX3107 chip configuration found on the AAVA
567 platform. Includes the extra initialisation and GPIO support
568 neded for this device.
569
570config SERIAL_DZ
571 bool "DECstation DZ serial driver"
572 depends on MACH_DECSTATION && 32BIT
573 select SERIAL_CORE
574 default y
575 ---help---
576 DZ11-family serial controllers for DECstations and VAXstations,
577 including the DC7085, M7814, and M7819.
578
579config SERIAL_DZ_CONSOLE
580 bool "Support console on DECstation DZ serial driver"
581 depends on SERIAL_DZ=y
582 select SERIAL_CORE_CONSOLE
583 default y
584 ---help---
585 If you say Y here, it will be possible to use a serial port as the
586 system console (the system console is the device which receives all
587 kernel messages and warnings and which allows logins in single user
588 mode).
589
590 Note that the firmware uses ttyS3 as the serial console on
591 DECstations that use this driver.
592
593 If unsure, say Y.
594
595config SERIAL_ZS
596 tristate "DECstation Z85C30 serial support"
597 depends on MACH_DECSTATION
598 select SERIAL_CORE
599 default y
600 ---help---
601 Support for the Zilog 85C350 serial communications controller used
602 for serial ports in newer DECstation systems. These include the
603 DECsystem 5900 and all models of the DECstation and DECsystem 5000
604 systems except from model 200.
605
606 If unsure, say Y. To compile this driver as a module, choose M here:
607 the module will be called zs.
608
609config SERIAL_ZS_CONSOLE
610 bool "Support for console on a DECstation Z85C30 serial port"
611 depends on SERIAL_ZS=y
612 select SERIAL_CORE_CONSOLE
613 default y
614 ---help---
615 If you say Y here, it will be possible to use a serial port as the
616 system console (the system console is the device which receives all
617 kernel messages and warnings and which allows logins in single user
618 mode).
619
620 Note that the firmware uses ttyS1 as the serial console on the
621 Maxine and ttyS3 on the others using this driver.
622
623 If unsure, say Y.
624
625config SERIAL_21285
626 tristate "DC21285 serial port support"
627 depends on ARM && FOOTBRIDGE
628 select SERIAL_CORE
629 help
630 If you have a machine based on a 21285 (Footbridge) StrongARM(R)/
631 PCI bridge you can enable its onboard serial port by enabling this
632 option.
633
634config SERIAL_21285_CONSOLE
635 bool "Console on DC21285 serial port"
636 depends on SERIAL_21285=y
637 select SERIAL_CORE_CONSOLE
638 help
639 If you have enabled the serial port on the 21285 footbridge you can
640 make it the console by answering Y to this option.
641
642 Even if you say Y here, the currently visible virtual console
643 (/dev/tty0) will still be used as the system console by default, but
644 you can alter that using a kernel command line option such as
645 "console=ttyFB". (Try "man bootparam" or see the documentation of
646 your boot loader (lilo or loadlin) about how to pass options to the
647 kernel at boot time.)
648
649config SERIAL_MPSC
650 bool "Marvell MPSC serial port support"
651 depends on PPC32 && MV64X60
652 select SERIAL_CORE
653 help
654 Say Y here if you want to use the Marvell MPSC serial controller.
655
656config SERIAL_MPSC_CONSOLE
657 bool "Support for console on Marvell MPSC serial port"
658 depends on SERIAL_MPSC
659 select SERIAL_CORE_CONSOLE
660 help
661 Say Y here if you want to support a serial console on a Marvell MPSC.
662
663config SERIAL_PXA
664 bool "PXA serial port support"
665 depends on ARCH_PXA || ARCH_MMP
666 select SERIAL_CORE
667 help
668 If you have a machine based on an Intel XScale PXA2xx CPU you
669 can enable its onboard serial ports by enabling this option.
670
671config SERIAL_PXA_CONSOLE
672 bool "Console on PXA serial port"
673 depends on SERIAL_PXA
674 select SERIAL_CORE_CONSOLE
675 help
676 If you have enabled the serial port on the Intel XScale PXA
677 CPU you can make it the console by answering Y to this option.
678
679 Even if you say Y here, the currently visible virtual console
680 (/dev/tty0) will still be used as the system console by default, but
681 you can alter that using a kernel command line option such as
682 "console=ttySA0". (Try "man bootparam" or see the documentation of
683 your boot loader (lilo or loadlin) about how to pass options to the
684 kernel at boot time.)
685
686config SERIAL_SA1100
687 bool "SA1100 serial port support"
688 depends on ARM && ARCH_SA1100
689 select SERIAL_CORE
690 help
691 If you have a machine based on a SA1100/SA1110 StrongARM(R) CPU you
692 can enable its onboard serial port by enabling this option.
693 Please read <file:Documentation/arm/SA1100/serial_UART> for further
694 info.
695
696config SERIAL_SA1100_CONSOLE
697 bool "Console on SA1100 serial port"
698 depends on SERIAL_SA1100
699 select SERIAL_CORE_CONSOLE
700 help
701 If you have enabled the serial port on the SA1100/SA1110 StrongARM
702 CPU you can make it the console by answering Y to this option.
703
704 Even if you say Y here, the currently visible virtual console
705 (/dev/tty0) will still be used as the system console by default, but
706 you can alter that using a kernel command line option such as
707 "console=ttySA0". (Try "man bootparam" or see the documentation of
708 your boot loader (lilo or loadlin) about how to pass options to the
709 kernel at boot time.)
710
711config SERIAL_MRST_MAX3110
712 tristate "SPI UART driver for Max3110"
713 depends on SPI_DW_PCI
714 select SERIAL_CORE
715 select SERIAL_CORE_CONSOLE
716 help
717 This is the UART protocol driver for the MAX3110 device on
718 the Intel Moorestown platform. On other systems use the max3100
719 driver.
720
721config SERIAL_MFD_HSU
722 tristate "Medfield High Speed UART support"
723 depends on PCI
724 select SERIAL_CORE
725
726config SERIAL_MFD_HSU_CONSOLE
727 boolean "Medfile HSU serial console support"
728 depends on SERIAL_MFD_HSU=y
729 select SERIAL_CORE_CONSOLE
730
731config SERIAL_BFIN
732 tristate "Blackfin serial port support"
733 depends on BLACKFIN
734 select SERIAL_CORE
735 select SERIAL_BFIN_UART0 if (BF531 || BF532 || BF533 || BF561)
736 help
737 Add support for the built-in UARTs on the Blackfin.
738
739 To compile this driver as a module, choose M here: the
740 module will be called bfin_5xx.
741
742config SERIAL_BFIN_CONSOLE
743 bool "Console on Blackfin serial port"
744 depends on SERIAL_BFIN=y
745 select SERIAL_CORE_CONSOLE
746
747choice
748 prompt "UART Mode"
749 depends on SERIAL_BFIN
750 default SERIAL_BFIN_DMA
751 help
752 This driver supports the built-in serial ports of the Blackfin family
753 of CPUs
754
755config SERIAL_BFIN_DMA
756 bool "DMA mode"
757 depends on !DMA_UNCACHED_NONE && KGDB_SERIAL_CONSOLE=n
758 help
759 This driver works under DMA mode. If this option is selected, the
760 blackfin simple dma driver is also enabled.
761
762config SERIAL_BFIN_PIO
763 bool "PIO mode"
764 help
765 This driver works under PIO mode.
766
767endchoice
768
769config SERIAL_BFIN_UART0
770 bool "Enable UART0"
771 depends on SERIAL_BFIN
772 help
773 Enable UART0
774
775config BFIN_UART0_CTSRTS
776 bool "Enable UART0 hardware flow control"
777 depends on SERIAL_BFIN_UART0
778 help
779 Enable hardware flow control in the driver.
780
781config SERIAL_BFIN_UART1
782 bool "Enable UART1"
783 depends on SERIAL_BFIN && (!BF531 && !BF532 && !BF533 && !BF561)
784 help
785 Enable UART1
786
787config BFIN_UART1_CTSRTS
788 bool "Enable UART1 hardware flow control"
789 depends on SERIAL_BFIN_UART1
790 help
791 Enable hardware flow control in the driver.
792
793config SERIAL_BFIN_UART2
794 bool "Enable UART2"
795 depends on SERIAL_BFIN && (BF54x || BF538 || BF539)
796 help
797 Enable UART2
798
799config BFIN_UART2_CTSRTS
800 bool "Enable UART2 hardware flow control"
801 depends on SERIAL_BFIN_UART2
802 help
803 Enable hardware flow control in the driver.
804
805config SERIAL_BFIN_UART3
806 bool "Enable UART3"
807 depends on SERIAL_BFIN && (BF54x)
808 help
809 Enable UART3
810
811config BFIN_UART3_CTSRTS
812 bool "Enable UART3 hardware flow control"
813 depends on SERIAL_BFIN_UART3
814 help
815 Enable hardware flow control in the driver.
816
817config SERIAL_IMX
818 bool "IMX serial port support"
819 depends on ARM && (ARCH_IMX || ARCH_MXC)
820 select SERIAL_CORE
821 select RATIONAL
822 help
823 If you have a machine based on a Motorola IMX CPU you
824 can enable its onboard serial port by enabling this option.
825
826config SERIAL_IMX_CONSOLE
827 bool "Console on IMX serial port"
828 depends on SERIAL_IMX
829 select SERIAL_CORE_CONSOLE
830 help
831 If you have enabled the serial port on the Motorola IMX
832 CPU you can make it the console by answering Y to this option.
833
834 Even if you say Y here, the currently visible virtual console
835 (/dev/tty0) will still be used as the system console by default, but
836 you can alter that using a kernel command line option such as
837 "console=ttySA0". (Try "man bootparam" or see the documentation of
838 your boot loader (lilo or loadlin) about how to pass options to the
839 kernel at boot time.)
840
841config SERIAL_UARTLITE
842 tristate "Xilinx uartlite serial port support"
843 depends on PPC32 || MICROBLAZE || MFD_TIMBERDALE
844 select SERIAL_CORE
845 help
846 Say Y here if you want to use the Xilinx uartlite serial controller.
847
848 To compile this driver as a module, choose M here: the
849 module will be called uartlite.
850
851config SERIAL_UARTLITE_CONSOLE
852 bool "Support for console on Xilinx uartlite serial port"
853 depends on SERIAL_UARTLITE=y
854 select SERIAL_CORE_CONSOLE
855 help
856 Say Y here if you wish to use a Xilinx uartlite as the system
857 console (the system console is the device which receives all kernel
858 messages and warnings and which allows logins in single user mode).
859
860config SERIAL_SUNCORE
861 bool
862 depends on SPARC
863 select SERIAL_CORE
864 select SERIAL_CORE_CONSOLE
865 default y
866
867config SERIAL_SUNZILOG
868 tristate "Sun Zilog8530 serial support"
869 depends on SPARC
870 help
871 This driver supports the Zilog8530 serial ports found on many Sparc
872 systems. Say Y or M if you want to be able to these serial ports.
873
874config SERIAL_SUNZILOG_CONSOLE
875 bool "Console on Sun Zilog8530 serial port"
876 depends on SERIAL_SUNZILOG=y
877 help
878 If you would like to be able to use the Zilog8530 serial port
879 on your Sparc system as the console, you can do so by answering
880 Y to this option.
881
882config SERIAL_SUNSU
883 tristate "Sun SU serial support"
884 depends on SPARC && PCI
885 help
886 This driver supports the 8250 serial ports that run the keyboard and
887 mouse on (PCI) UltraSPARC systems. Say Y or M if you want to be able
888 to these serial ports.
889
890config SERIAL_SUNSU_CONSOLE
891 bool "Console on Sun SU serial port"
892 depends on SERIAL_SUNSU=y
893 help
894 If you would like to be able to use the SU serial port
895 on your Sparc system as the console, you can do so by answering
896 Y to this option.
897
898config SERIAL_MUX
899 tristate "Serial MUX support"
900 depends on GSC
901 select SERIAL_CORE
902 default y
903 ---help---
904 Saying Y here will enable the hardware MUX serial driver for
905 the Nova, K class systems and D class with a 'remote control card'.
906 The hardware MUX is not 8250/16550 compatible therefore the
907 /dev/ttyB0 device is shared between the Serial MUX and the PDC
908 software console. The following steps need to be completed to use
909 the Serial MUX:
910
911 1. create the device entry (mknod /dev/ttyB0 c 11 0)
912 2. Edit the /etc/inittab to start a getty listening on /dev/ttyB0
913 3. Add device ttyB0 to /etc/securetty (if you want to log on as
914 root on this console.)
915 4. Change the kernel command console parameter to: console=ttyB0
916
917config SERIAL_MUX_CONSOLE
918 bool "Support for console on serial MUX"
919 depends on SERIAL_MUX=y
920 select SERIAL_CORE_CONSOLE
921 default y
922
923config PDC_CONSOLE
924 bool "PDC software console support"
925 depends on PARISC && !SERIAL_MUX && VT
926 default n
927 help
928 Saying Y here will enable the software based PDC console to be
929 used as the system console. This is useful for machines in
930 which the hardware based console has not been written yet. The
931 following steps must be competed to use the PDC console:
932
933 1. create the device entry (mknod /dev/ttyB0 c 11 0)
934 2. Edit the /etc/inittab to start a getty listening on /dev/ttyB0
935 3. Add device ttyB0 to /etc/securetty (if you want to log on as
936 root on this console.)
937 4. Change the kernel command console parameter to: console=ttyB0
938
939config SERIAL_SUNSAB
940 tristate "Sun Siemens SAB82532 serial support"
941 depends on SPARC && PCI
942 help
943 This driver supports the Siemens SAB82532 DUSCC serial ports on newer
944 (PCI) UltraSPARC systems. Say Y or M if you want to be able to these
945 serial ports.
946
947config SERIAL_SUNSAB_CONSOLE
948 bool "Console on Sun Siemens SAB82532 serial port"
949 depends on SERIAL_SUNSAB=y
950 help
951 If you would like to be able to use the SAB82532 serial port
952 on your Sparc system as the console, you can do so by answering
953 Y to this option.
954
955config SERIAL_SUNHV
956 bool "Sun4v Hypervisor Console support"
957 depends on SPARC64
958 help
959 This driver supports the console device found on SUN4V Sparc
960 systems. Say Y if you want to be able to use this device.
961
962config SERIAL_IP22_ZILOG
963 tristate "SGI Zilog8530 serial support"
964 depends on SGI_HAS_ZILOG
965 select SERIAL_CORE
966 help
967 This driver supports the Zilog8530 serial ports found on SGI
968 systems. Say Y or M if you want to be able to these serial ports.
969
970config SERIAL_IP22_ZILOG_CONSOLE
971 bool "Console on SGI Zilog8530 serial port"
972 depends on SERIAL_IP22_ZILOG=y
973 select SERIAL_CORE_CONSOLE
974
975config SERIAL_SH_SCI
976 tristate "SuperH SCI(F) serial port support"
977 depends on HAVE_CLK && (SUPERH || H8300 || ARCH_SHMOBILE)
978 select SERIAL_CORE
979
980config SERIAL_SH_SCI_NR_UARTS
981 int "Maximum number of SCI(F) serial ports"
982 depends on SERIAL_SH_SCI
983 default "2"
984
985config SERIAL_SH_SCI_CONSOLE
986 bool "Support for console on SuperH SCI(F)"
987 depends on SERIAL_SH_SCI=y
988 select SERIAL_CORE_CONSOLE
989
990config SERIAL_SH_SCI_DMA
991 bool "DMA support"
992 depends on SERIAL_SH_SCI && SH_DMAE && EXPERIMENTAL
993
994config SERIAL_PNX8XXX
995 bool "Enable PNX8XXX SoCs' UART Support"
996 depends on MIPS && (SOC_PNX8550 || SOC_PNX833X)
997 select SERIAL_CORE
998 help
999 If you have a MIPS-based Philips SoC such as PNX8550 or PNX8330
1000 and you want to use serial ports, say Y. Otherwise, say N.
1001
1002config SERIAL_PNX8XXX_CONSOLE
1003 bool "Enable PNX8XX0 serial console"
1004 depends on SERIAL_PNX8XXX
1005 select SERIAL_CORE_CONSOLE
1006 help
1007 If you have a MIPS-based Philips SoC such as PNX8550 or PNX8330
1008 and you want to use serial console, say Y. Otherwise, say N.
1009
1010config SERIAL_CORE
1011 tristate
1012
1013config SERIAL_CORE_CONSOLE
1014 bool
1015
1016config CONSOLE_POLL
1017 bool
1018
1019config SERIAL_68328
1020 bool "68328 serial support"
1021 depends on M68328 || M68EZ328 || M68VZ328
1022 help
1023 This driver supports the built-in serial port of the Motorola 68328
1024 (standard, EZ and VZ varieties).
1025
1026config SERIAL_68328_RTS_CTS
1027 bool "Support RTS/CTS on 68328 serial port"
1028 depends on SERIAL_68328
1029
1030config SERIAL_MCF
1031 bool "Coldfire serial support"
1032 depends on COLDFIRE
1033 select SERIAL_CORE
1034 help
1035 This serial driver supports the Freescale Coldfire serial ports.
1036
1037config SERIAL_MCF_BAUDRATE
1038 int "Default baudrate for Coldfire serial ports"
1039 depends on SERIAL_MCF
1040 default 19200
1041 help
1042 This setting lets you define what the default baudrate is for the
1043 ColdFire serial ports. The usual default varies from board to board,
1044 and this setting is a way of catering for that.
1045
1046config SERIAL_MCF_CONSOLE
1047 bool "Coldfire serial console support"
1048 depends on SERIAL_MCF
1049 select SERIAL_CORE_CONSOLE
1050 help
1051 Enable a ColdFire internal serial port to be the system console.
1052
1053config SERIAL_68360_SMC
1054 bool "68360 SMC uart support"
1055 depends on M68360
1056 help
1057 This driver supports the SMC serial ports of the Motorola 68360 CPU.
1058
1059config SERIAL_68360_SCC
1060 bool "68360 SCC uart support"
1061 depends on M68360
1062 help
1063 This driver supports the SCC serial ports of the Motorola 68360 CPU.
1064
1065config SERIAL_68360
1066 bool
1067 depends on SERIAL_68360_SMC || SERIAL_68360_SCC
1068 default y
1069
1070config SERIAL_PMACZILOG
1071 tristate "Mac or PowerMac z85c30 ESCC support"
1072 depends on (M68K && MAC) || (PPC_OF && PPC_PMAC)
1073 select SERIAL_CORE
1074 help
1075 This driver supports the Zilog z85C30 serial ports found on
1076 (Power)Mac machines.
1077 Say Y or M if you want to be able to these serial ports.
1078
1079config SERIAL_PMACZILOG_TTYS
1080 bool "Use ttySn device nodes for Zilog z85c30"
1081 depends on SERIAL_PMACZILOG
1082 help
1083 The pmac_zilog driver for the z85C30 chip on many powermacs
1084 historically used the device numbers for /dev/ttySn. The
1085 8250 serial port driver also uses these numbers, which means
1086 the two drivers being unable to coexist; you could not use
1087 both z85C30 and 8250 type ports at the same time.
1088
1089 If this option is not selected, the pmac_zilog driver will
1090 use the device numbers allocated for /dev/ttyPZn. This allows
1091 the pmac_zilog and 8250 drivers to co-exist, but may cause
1092 existing userspace setups to break. Programs that need to
1093 access the built-in serial ports on powermacs will need to
1094 be reconfigured to use /dev/ttyPZn instead of /dev/ttySn.
1095
1096 If you enable this option, any z85c30 ports in the system will
1097 be registered as ttyS0 onwards as in the past, and you will be
1098 unable to use the 8250 module for PCMCIA or other 16C550-style
1099 UARTs.
1100
1101 Say N unless you need the z85c30 ports on your (Power)Mac
1102 to appear as /dev/ttySn.
1103
1104config SERIAL_PMACZILOG_CONSOLE
1105 bool "Console on Mac or PowerMac z85c30 serial port"
1106 depends on SERIAL_PMACZILOG=y
1107 select SERIAL_CORE_CONSOLE
1108 help
1109 If you would like to be able to use the z85c30 serial port
1110 on your (Power)Mac as the console, you can do so by answering
1111 Y to this option.
1112
1113config SERIAL_LH7A40X
1114 tristate "Sharp LH7A40X embedded UART support"
1115 depends on ARM && ARCH_LH7A40X
1116 select SERIAL_CORE
1117 help
1118 This enables support for the three on-board UARTs of the
1119 Sharp LH7A40X series CPUs. Choose Y or M.
1120
1121config SERIAL_LH7A40X_CONSOLE
1122 bool "Support for console on Sharp LH7A40X serial port"
1123 depends on SERIAL_LH7A40X=y
1124 select SERIAL_CORE_CONSOLE
1125 help
1126 Say Y here if you wish to use one of the serial ports as the
1127 system console--the system console is the device which
1128 receives all kernel messages and warnings and which allows
1129 logins in single user mode.
1130
1131 Even if you say Y here, the currently visible framebuffer console
1132 (/dev/tty0) will still be used as the default system console, but
1133 you can alter that using a kernel command line, for example
1134 "console=ttyAM1".
1135
1136config SERIAL_CPM
1137 tristate "CPM SCC/SMC serial port support"
1138 depends on CPM2 || 8xx
1139 select SERIAL_CORE
1140 help
1141 This driver supports the SCC and SMC serial ports on Motorola
1142 embedded PowerPC that contain a CPM1 (8xx) or CPM2 (8xxx)
1143
1144config SERIAL_CPM_CONSOLE
1145 bool "Support for console on CPM SCC/SMC serial port"
1146 depends on SERIAL_CPM=y
1147 select SERIAL_CORE_CONSOLE
1148 help
1149 Say Y here if you wish to use a SCC or SMC CPM UART as the system
1150 console (the system console is the device which receives all kernel
1151 messages and warnings and which allows logins in single user mode).
1152
1153 Even if you say Y here, the currently visible framebuffer console
1154 (/dev/tty0) will still be used as the system console by default, but
1155 you can alter that using a kernel command line option such as
1156 "console=ttyCPM0". (Try "man bootparam" or see the documentation of
1157 your boot loader (lilo or loadlin) about how to pass options to the
1158 kernel at boot time.)
1159
1160config SERIAL_SGI_L1_CONSOLE
1161 bool "SGI Altix L1 serial console support"
1162 depends on IA64_GENERIC || IA64_SGI_SN2
1163 select SERIAL_CORE
1164 select SERIAL_CORE_CONSOLE
1165 help
1166 If you have an SGI Altix and you would like to use the system
1167 controller serial port as your console (you want this!),
1168 say Y. Otherwise, say N.
1169
1170config SERIAL_MPC52xx
1171 tristate "Freescale MPC52xx/MPC512x family PSC serial support"
1172 depends on PPC_MPC52xx || PPC_MPC512x
1173 select SERIAL_CORE
1174 help
1175 This driver supports MPC52xx and MPC512x PSC serial ports. If you would
1176 like to use them, you must answer Y or M to this option. Note that
1177 for use as console, it must be included in kernel and not as a
1178 module.
1179
1180config SERIAL_MPC52xx_CONSOLE
1181 bool "Console on a Freescale MPC52xx/MPC512x family PSC serial port"
1182 depends on SERIAL_MPC52xx=y
1183 select SERIAL_CORE_CONSOLE
1184 help
1185 Select this options if you'd like to use one of the PSC serial port
1186 of the Freescale MPC52xx family as a console.
1187
1188config SERIAL_MPC52xx_CONSOLE_BAUD
1189 int "Freescale MPC52xx/MPC512x family PSC serial port baud"
1190 depends on SERIAL_MPC52xx_CONSOLE=y
1191 default "9600"
1192 help
1193 Select the MPC52xx console baud rate.
1194 This value is only used if the bootloader doesn't pass in the
1195 console baudrate
1196
1197config SERIAL_ICOM
1198 tristate "IBM Multiport Serial Adapter"
1199 depends on PCI && (PPC_ISERIES || PPC_PSERIES)
1200 select SERIAL_CORE
1201 select FW_LOADER
1202 help
1203 This driver is for a family of multiport serial adapters
1204 including 2 port RVX, 2 port internal modem, 4 port internal
1205 modem and a split 1 port RVX and 1 port internal modem.
1206
1207 This driver can also be built as a module. If so, the module
1208 will be called icom.
1209
1210config SERIAL_M32R_SIO
1211 bool "M32R SIO I/F"
1212 depends on M32R
1213 default y
1214 select SERIAL_CORE
1215 help
1216 Say Y here if you want to use the M32R serial controller.
1217
1218config SERIAL_M32R_SIO_CONSOLE
1219 bool "use SIO console"
1220 depends on SERIAL_M32R_SIO=y
1221 select SERIAL_CORE_CONSOLE
1222 help
1223 Say Y here if you want to support a serial console.
1224
1225 If you use an M3T-M32700UT or an OPSPUT platform,
1226 please say also y for SERIAL_M32R_PLDSIO.
1227
1228config SERIAL_M32R_PLDSIO
1229 bool "M32R SIO I/F on a PLD"
1230 depends on SERIAL_M32R_SIO=y && (PLAT_OPSPUT || PLAT_USRV || PLAT_M32700UT)
1231 default n
1232 help
1233 Say Y here if you want to use the M32R serial controller
1234 on a PLD (Programmable Logic Device).
1235
1236 If you use an M3T-M32700UT or an OPSPUT platform,
1237 please say Y.
1238
1239config SERIAL_TXX9
1240 bool "TMPTX39XX/49XX SIO support"
1241 depends on HAS_TXX9_SERIAL
1242 select SERIAL_CORE
1243 default y
1244
1245config HAS_TXX9_SERIAL
1246 bool
1247
1248config SERIAL_TXX9_NR_UARTS
1249 int "Maximum number of TMPTX39XX/49XX SIO ports"
1250 depends on SERIAL_TXX9
1251 default "6"
1252
1253config SERIAL_TXX9_CONSOLE
1254 bool "TMPTX39XX/49XX SIO Console support"
1255 depends on SERIAL_TXX9=y
1256 select SERIAL_CORE_CONSOLE
1257
1258config SERIAL_TXX9_STDSERIAL
1259 bool "TX39XX/49XX SIO act as standard serial"
1260 depends on !SERIAL_8250 && SERIAL_TXX9
1261
1262config SERIAL_VR41XX
1263 tristate "NEC VR4100 series Serial Interface Unit support"
1264 depends on CPU_VR41XX
1265 select SERIAL_CORE
1266 help
1267 If you have a NEC VR4100 series processor and you want to use
1268 Serial Interface Unit(SIU) or Debug Serial Interface Unit(DSIU)
1269 (not include VR4111/VR4121 DSIU), say Y. Otherwise, say N.
1270
1271config SERIAL_VR41XX_CONSOLE
1272 bool "Enable NEC VR4100 series Serial Interface Unit console"
1273 depends on SERIAL_VR41XX=y
1274 select SERIAL_CORE_CONSOLE
1275 help
1276 If you have a NEC VR4100 series processor and you want to use
1277 a console on a serial port, say Y. Otherwise, say N.
1278
1279config SERIAL_JSM
1280 tristate "Digi International NEO PCI Support"
1281 depends on PCI
1282 select SERIAL_CORE
1283 help
1284 This is a driver for Digi International's Neo series
1285 of cards which provide multiple serial ports. You would need
1286 something like this to connect more than two modems to your Linux
1287 box, for instance in order to become a dial-in server. This driver
1288 supports PCI boards only.
1289
1290 If you have a card like this, say Y here, otherwise say N.
1291
1292 To compile this driver as a module, choose M here: the
1293 module will be called jsm.
1294
1295config SERIAL_SGI_IOC4
1296 tristate "SGI IOC4 controller serial support"
1297 depends on (IA64_GENERIC || IA64_SGI_SN2) && SGI_IOC4
1298 select SERIAL_CORE
1299 help
1300 If you have an SGI Altix with an IOC4 based Base IO card
1301 and wish to use the serial ports on this card, say Y.
1302 Otherwise, say N.
1303
1304config SERIAL_SGI_IOC3
1305 tristate "SGI Altix IOC3 serial support"
1306 depends on (IA64_GENERIC || IA64_SGI_SN2) && SGI_IOC3
1307 select SERIAL_CORE
1308 help
1309 If you have an SGI Altix with an IOC3 serial card,
1310 say Y or M. Otherwise, say N.
1311
1312config SERIAL_MSM
1313 bool "MSM on-chip serial port support"
1314 depends on ARM && ARCH_MSM
1315 select SERIAL_CORE
1316
1317config SERIAL_MSM_CONSOLE
1318 bool "MSM serial console support"
1319 depends on SERIAL_MSM=y
1320 select SERIAL_CORE_CONSOLE
1321
1322config SERIAL_VT8500
1323 bool "VIA VT8500 on-chip serial port support"
1324 depends on ARM && ARCH_VT8500
1325 select SERIAL_CORE
1326
1327config SERIAL_VT8500_CONSOLE
1328 bool "VIA VT8500 serial console support"
1329 depends on SERIAL_VT8500=y
1330 select SERIAL_CORE_CONSOLE
1331
1332config SERIAL_NETX
1333 tristate "NetX serial port support"
1334 depends on ARM && ARCH_NETX
1335 select SERIAL_CORE
1336 help
1337 If you have a machine based on a Hilscher NetX SoC you
1338 can enable its onboard serial port by enabling this option.
1339
1340 To compile this driver as a module, choose M here: the
1341 module will be called netx-serial.
1342
1343config SERIAL_NETX_CONSOLE
1344 bool "Console on NetX serial port"
1345 depends on SERIAL_NETX=y
1346 select SERIAL_CORE_CONSOLE
1347 help
1348 If you have enabled the serial port on the Hilscher NetX SoC
1349 you can make it the console by answering Y to this option.
1350
1351config SERIAL_OF_PLATFORM
1352 tristate "Serial port on Open Firmware platform bus"
1353 depends on OF
1354 depends on SERIAL_8250 || SERIAL_OF_PLATFORM_NWPSERIAL
1355 help
1356 If you have a PowerPC based system that has serial ports
1357 on a platform specific bus, you should enable this option.
1358 Currently, only 8250 compatible ports are supported, but
1359 others can easily be added.
1360
1361config SERIAL_OMAP
1362 tristate "OMAP serial port support"
1363 depends on ARCH_OMAP2 || ARCH_OMAP3 || ARCH_OMAP4
1364 select SERIAL_CORE
1365 help
1366 If you have a machine based on an Texas Instruments OMAP CPU you
1367 can enable its onboard serial ports by enabling this option.
1368
1369 By enabling this option you take advantage of dma feature available
1370 with the omap-serial driver. DMA support can be enabled from platform
1371 data.
1372
1373config SERIAL_OMAP_CONSOLE
1374 bool "Console on OMAP serial port"
1375 depends on SERIAL_OMAP
1376 select SERIAL_CORE_CONSOLE
1377 help
1378 Select this option if you would like to use omap serial port as
1379 console.
1380
1381 Even if you say Y here, the currently visible virtual console
1382 (/dev/tty0) will still be used as the system console by default, but
1383 you can alter that using a kernel command line option such as
1384 "console=ttyOx". (Try "man bootparam" or see the documentation of
1385 your boot loader about how to pass options to the kernel at
1386 boot time.)
1387
1388config SERIAL_OF_PLATFORM_NWPSERIAL
1389 tristate "NWP serial port driver"
1390 depends on PPC_OF && PPC_DCR
1391 select SERIAL_OF_PLATFORM
1392 select SERIAL_CORE_CONSOLE
1393 select SERIAL_CORE
1394 help
1395 This driver supports the cell network processor nwp serial
1396 device.
1397
1398config SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
1399 bool "Console on NWP serial port"
1400 depends on SERIAL_OF_PLATFORM_NWPSERIAL=y
1401 select SERIAL_CORE_CONSOLE
1402 help
1403 Support for Console on the NWP serial ports.
1404
1405config SERIAL_QE
1406 tristate "Freescale QUICC Engine serial port support"
1407 depends on QUICC_ENGINE
1408 select SERIAL_CORE
1409 select FW_LOADER
1410 default n
1411 help
1412 This driver supports the QE serial ports on Freescale embedded
1413 PowerPC that contain a QUICC Engine.
1414
1415config SERIAL_SC26XX
1416 tristate "SC2681/SC2692 serial port support"
1417 depends on SNI_RM
1418 select SERIAL_CORE
1419 help
1420 This is a driver for the onboard serial ports of
1421 older RM400 machines.
1422
1423config SERIAL_SC26XX_CONSOLE
1424 bool "Console on SC2681/SC2692 serial port"
1425 depends on SERIAL_SC26XX
1426 select SERIAL_CORE_CONSOLE
1427 help
1428 Support for Console on SC2681/SC2692 serial ports.
1429
1430config SERIAL_BFIN_SPORT
1431 tristate "Blackfin SPORT emulate UART"
1432 depends on BLACKFIN
1433 select SERIAL_CORE
1434 help
1435 Enable SPORT emulate UART on Blackfin series.
1436
1437 To compile this driver as a module, choose M here: the
1438 module will be called bfin_sport_uart.
1439
1440config SERIAL_BFIN_SPORT_CONSOLE
1441 bool "Console on Blackfin sport emulated uart"
1442 depends on SERIAL_BFIN_SPORT=y
1443 select SERIAL_CORE_CONSOLE
1444
1445config SERIAL_BFIN_SPORT0_UART
1446 bool "Enable UART over SPORT0"
1447 depends on SERIAL_BFIN_SPORT && !(BF542 || BF544)
1448 help
1449 Enable UART over SPORT0
1450
1451config SERIAL_BFIN_SPORT0_UART_CTSRTS
1452 bool "Enable UART over SPORT0 hardware flow control"
1453 depends on SERIAL_BFIN_SPORT0_UART
1454 help
1455 Enable hardware flow control in the driver.
1456
1457config SERIAL_BFIN_SPORT1_UART
1458 bool "Enable UART over SPORT1"
1459 depends on SERIAL_BFIN_SPORT
1460 help
1461 Enable UART over SPORT1
1462
1463config SERIAL_BFIN_SPORT1_UART_CTSRTS
1464 bool "Enable UART over SPORT1 hardware flow control"
1465 depends on SERIAL_BFIN_SPORT1_UART
1466 help
1467 Enable hardware flow control in the driver.
1468
1469config SERIAL_BFIN_SPORT2_UART
1470 bool "Enable UART over SPORT2"
1471 depends on SERIAL_BFIN_SPORT && (BF54x || BF538 || BF539)
1472 help
1473 Enable UART over SPORT2
1474
1475config SERIAL_BFIN_SPORT2_UART_CTSRTS
1476 bool "Enable UART over SPORT2 hardware flow control"
1477 depends on SERIAL_BFIN_SPORT2_UART
1478 help
1479 Enable hardware flow control in the driver.
1480
1481config SERIAL_BFIN_SPORT3_UART
1482 bool "Enable UART over SPORT3"
1483 depends on SERIAL_BFIN_SPORT && (BF54x || BF538 || BF539)
1484 help
1485 Enable UART over SPORT3
1486
1487config SERIAL_BFIN_SPORT3_UART_CTSRTS
1488 bool "Enable UART over SPORT3 hardware flow control"
1489 depends on SERIAL_BFIN_SPORT3_UART
1490 help
1491 Enable hardware flow control in the driver.
1492
1493config SERIAL_TIMBERDALE
1494 tristate "Support for timberdale UART"
1495 select SERIAL_CORE
1496 ---help---
1497 Add support for UART controller on timberdale.
1498
1499config SERIAL_BCM63XX
1500 tristate "bcm63xx serial port support"
1501 select SERIAL_CORE
1502 depends on BCM63XX
1503 help
1504 If you have a bcm63xx CPU, you can enable its onboard
1505 serial port by enabling this options.
1506
1507 To compile this driver as a module, choose M here: the
1508 module will be called bcm963xx_uart.
1509
1510config SERIAL_BCM63XX_CONSOLE
1511 bool "Console on bcm63xx serial port"
1512 depends on SERIAL_BCM63XX=y
1513 select SERIAL_CORE_CONSOLE
1514 help
1515 If you have enabled the serial port on the bcm63xx CPU
1516 you can make it the console by answering Y to this option.
1517
1518config SERIAL_GRLIB_GAISLER_APBUART
1519 tristate "GRLIB APBUART serial support"
1520 depends on OF
1521 ---help---
1522 Add support for the GRLIB APBUART serial port.
1523
1524config SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
1525 bool "Console on GRLIB APBUART serial port"
1526 depends on SERIAL_GRLIB_GAISLER_APBUART=y
1527 select SERIAL_CORE_CONSOLE
1528 help
1529 Support for running a console on the GRLIB APBUART
1530
1531config SERIAL_ALTERA_JTAGUART
1532 tristate "Altera JTAG UART support"
1533 select SERIAL_CORE
1534 help
1535 This driver supports the Altera JTAG UART port.
1536
1537config SERIAL_ALTERA_JTAGUART_CONSOLE
1538 bool "Altera JTAG UART console support"
1539 depends on SERIAL_ALTERA_JTAGUART=y
1540 select SERIAL_CORE_CONSOLE
1541 help
1542 Enable a Altera JTAG UART port to be the system console.
1543
1544config SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS
1545 bool "Bypass output when no connection"
1546 depends on SERIAL_ALTERA_JTAGUART_CONSOLE
1547 select SERIAL_CORE_CONSOLE
1548 help
1549 Bypass console output and keep going even if there is no
1550 JTAG terminal connection with the host.
1551
1552config SERIAL_ALTERA_UART
1553 tristate "Altera UART support"
1554 select SERIAL_CORE
1555 help
1556 This driver supports the Altera softcore UART port.
1557
1558config SERIAL_ALTERA_UART_MAXPORTS
1559 int "Maximum number of Altera UART ports"
1560 depends on SERIAL_ALTERA_UART
1561 default 4
1562 help
1563 This setting lets you define the maximum number of the Altera
1564 UART ports. The usual default varies from board to board, and
1565 this setting is a way of catering for that.
1566
1567config SERIAL_ALTERA_UART_BAUDRATE
1568 int "Default baudrate for Altera UART ports"
1569 depends on SERIAL_ALTERA_UART
1570 default 115200
1571 help
1572 This setting lets you define what the default baudrate is for the
1573 Altera UART ports. The usual default varies from board to board,
1574 and this setting is a way of catering for that.
1575
1576config SERIAL_ALTERA_UART_CONSOLE
1577 bool "Altera UART console support"
1578 depends on SERIAL_ALTERA_UART=y
1579 select SERIAL_CORE_CONSOLE
1580 help
1581 Enable a Altera UART port to be the system console.
1582
1583config SERIAL_IFX6X60
1584 tristate "SPI protocol driver for Infineon 6x60 modem (EXPERIMENTAL)"
1585 depends on GPIOLIB && SPI && EXPERIMENTAL
1586 help
1587 Support for the IFX6x60 modem devices on Intel MID platforms.
1588
1589config SERIAL_PCH_UART
1590 tristate "Intel EG20T PCH UART"
1591 depends on PCI && DMADEVICES
1592 select SERIAL_CORE
1593 select PCH_DMA
1594 help
1595 This driver is for PCH(Platform controller Hub) UART of Intel EG20T
1596 which is an IOH(Input/Output Hub) for x86 embedded processor.
1597 Enabling PCH_DMA, this PCH UART works as DMA mode.
1598endmenu
diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
new file mode 100644
index 000000000000..8ea92e9c73b0
--- /dev/null
+++ b/drivers/tty/serial/Makefile
@@ -0,0 +1,94 @@
1#
2# Makefile for the kernel serial device drivers.
3#
4
5obj-$(CONFIG_SERIAL_CORE) += serial_core.o
6obj-$(CONFIG_SERIAL_21285) += 21285.o
7
8# These Sparc drivers have to appear before others such as 8250
9# which share ttySx minor node space. Otherwise console device
10# names change and other unplesantries.
11obj-$(CONFIG_SERIAL_SUNCORE) += suncore.o
12obj-$(CONFIG_SERIAL_SUNHV) += sunhv.o
13obj-$(CONFIG_SERIAL_SUNZILOG) += sunzilog.o
14obj-$(CONFIG_SERIAL_SUNSU) += sunsu.o
15obj-$(CONFIG_SERIAL_SUNSAB) += sunsab.o
16
17obj-$(CONFIG_SERIAL_8250) += 8250.o
18obj-$(CONFIG_SERIAL_8250_PNP) += 8250_pnp.o
19obj-$(CONFIG_SERIAL_8250_GSC) += 8250_gsc.o
20obj-$(CONFIG_SERIAL_8250_PCI) += 8250_pci.o
21obj-$(CONFIG_SERIAL_8250_HP300) += 8250_hp300.o
22obj-$(CONFIG_SERIAL_8250_CS) += serial_cs.o
23obj-$(CONFIG_SERIAL_8250_ACORN) += 8250_acorn.o
24obj-$(CONFIG_SERIAL_8250_CONSOLE) += 8250_early.o
25obj-$(CONFIG_SERIAL_8250_FOURPORT) += 8250_fourport.o
26obj-$(CONFIG_SERIAL_8250_ACCENT) += 8250_accent.o
27obj-$(CONFIG_SERIAL_8250_BOCA) += 8250_boca.o
28obj-$(CONFIG_SERIAL_8250_EXAR_ST16C554) += 8250_exar_st16c554.o
29obj-$(CONFIG_SERIAL_8250_HUB6) += 8250_hub6.o
30obj-$(CONFIG_SERIAL_8250_MCA) += 8250_mca.o
31obj-$(CONFIG_SERIAL_AMBA_PL010) += amba-pl010.o
32obj-$(CONFIG_SERIAL_AMBA_PL011) += amba-pl011.o
33obj-$(CONFIG_SERIAL_CLPS711X) += clps711x.o
34obj-$(CONFIG_SERIAL_PXA) += pxa.o
35obj-$(CONFIG_SERIAL_PNX8XXX) += pnx8xxx_uart.o
36obj-$(CONFIG_SERIAL_SA1100) += sa1100.o
37obj-$(CONFIG_SERIAL_BCM63XX) += bcm63xx_uart.o
38obj-$(CONFIG_SERIAL_BFIN) += bfin_5xx.o
39obj-$(CONFIG_SERIAL_BFIN_SPORT) += bfin_sport_uart.o
40obj-$(CONFIG_SERIAL_SAMSUNG) += samsung.o
41obj-$(CONFIG_SERIAL_S3C2400) += s3c2400.o
42obj-$(CONFIG_SERIAL_S3C2410) += s3c2410.o
43obj-$(CONFIG_SERIAL_S3C2412) += s3c2412.o
44obj-$(CONFIG_SERIAL_S3C2440) += s3c2440.o
45obj-$(CONFIG_SERIAL_S3C24A0) += s3c24a0.o
46obj-$(CONFIG_SERIAL_S3C6400) += s3c6400.o
47obj-$(CONFIG_SERIAL_S5PV210) += s5pv210.o
48obj-$(CONFIG_SERIAL_MAX3100) += max3100.o
49obj-$(CONFIG_SERIAL_MAX3107) += max3107.o
50obj-$(CONFIG_SERIAL_MAX3107_AAVA) += max3107-aava.o
51obj-$(CONFIG_SERIAL_IP22_ZILOG) += ip22zilog.o
52obj-$(CONFIG_SERIAL_MUX) += mux.o
53obj-$(CONFIG_SERIAL_68328) += 68328serial.o
54obj-$(CONFIG_SERIAL_68360) += 68360serial.o
55obj-$(CONFIG_SERIAL_MCF) += mcf.o
56obj-$(CONFIG_SERIAL_PMACZILOG) += pmac_zilog.o
57obj-$(CONFIG_SERIAL_LH7A40X) += serial_lh7a40x.o
58obj-$(CONFIG_SERIAL_DZ) += dz.o
59obj-$(CONFIG_SERIAL_ZS) += zs.o
60obj-$(CONFIG_SERIAL_SH_SCI) += sh-sci.o
61obj-$(CONFIG_SERIAL_SGI_L1_CONSOLE) += sn_console.o
62obj-$(CONFIG_SERIAL_CPM) += cpm_uart/
63obj-$(CONFIG_SERIAL_IMX) += imx.o
64obj-$(CONFIG_SERIAL_MPC52xx) += mpc52xx_uart.o
65obj-$(CONFIG_SERIAL_ICOM) += icom.o
66obj-$(CONFIG_SERIAL_M32R_SIO) += m32r_sio.o
67obj-$(CONFIG_SERIAL_MPSC) += mpsc.o
68obj-$(CONFIG_SERIAL_SB1250_DUART) += sb1250-duart.o
69obj-$(CONFIG_ETRAX_SERIAL) += crisv10.o
70obj-$(CONFIG_SERIAL_SC26XX) += sc26xx.o
71obj-$(CONFIG_SERIAL_JSM) += jsm/
72obj-$(CONFIG_SERIAL_TXX9) += serial_txx9.o
73obj-$(CONFIG_SERIAL_VR41XX) += vr41xx_siu.o
74obj-$(CONFIG_SERIAL_SGI_IOC4) += ioc4_serial.o
75obj-$(CONFIG_SERIAL_SGI_IOC3) += ioc3_serial.o
76obj-$(CONFIG_SERIAL_ATMEL) += atmel_serial.o
77obj-$(CONFIG_SERIAL_UARTLITE) += uartlite.o
78obj-$(CONFIG_SERIAL_MSM) += msm_serial.o
79obj-$(CONFIG_SERIAL_NETX) += netx-serial.o
80obj-$(CONFIG_SERIAL_OF_PLATFORM) += of_serial.o
81obj-$(CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL) += nwpserial.o
82obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
83obj-$(CONFIG_SERIAL_OMAP) += omap-serial.o
84obj-$(CONFIG_KGDB_SERIAL_CONSOLE) += kgdboc.o
85obj-$(CONFIG_SERIAL_QE) += ucc_uart.o
86obj-$(CONFIG_SERIAL_TIMBERDALE) += timbuart.o
87obj-$(CONFIG_SERIAL_GRLIB_GAISLER_APBUART) += apbuart.o
88obj-$(CONFIG_SERIAL_ALTERA_JTAGUART) += altera_jtaguart.o
89obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o
90obj-$(CONFIG_SERIAL_VT8500) += vt8500_serial.o
91obj-$(CONFIG_SERIAL_MRST_MAX3110) += mrst_max3110.o
92obj-$(CONFIG_SERIAL_MFD_HSU) += mfd.o
93obj-$(CONFIG_SERIAL_IFX6X60) += ifx6x60.o
94obj-$(CONFIG_SERIAL_PCH_UART) += pch_uart.o
diff --git a/drivers/tty/serial/altera_jtaguart.c b/drivers/tty/serial/altera_jtaguart.c
new file mode 100644
index 000000000000..f9b49b5ff5e1
--- /dev/null
+++ b/drivers/tty/serial/altera_jtaguart.c
@@ -0,0 +1,504 @@
1/*
2 * altera_jtaguart.c -- Altera JTAG UART driver
3 *
4 * Based on mcf.c -- Freescale ColdFire UART driver
5 *
6 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
7 * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
8 * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
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
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/console.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/serial.h>
24#include <linux/serial_core.h>
25#include <linux/platform_device.h>
26#include <linux/io.h>
27#include <linux/altera_jtaguart.h>
28
29#define DRV_NAME "altera_jtaguart"
30
31/*
32 * Altera JTAG UART register definitions according to the Altera JTAG UART
33 * datasheet: http://www.altera.com/literature/hb/nios2/n2cpu_nii51009.pdf
34 */
35
36#define ALTERA_JTAGUART_SIZE 8
37
38#define ALTERA_JTAGUART_DATA_REG 0
39
40#define ALTERA_JTAGUART_DATA_DATA_MSK 0x000000FF
41#define ALTERA_JTAGUART_DATA_RVALID_MSK 0x00008000
42#define ALTERA_JTAGUART_DATA_RAVAIL_MSK 0xFFFF0000
43#define ALTERA_JTAGUART_DATA_RAVAIL_OFF 16
44
45#define ALTERA_JTAGUART_CONTROL_REG 4
46
47#define ALTERA_JTAGUART_CONTROL_RE_MSK 0x00000001
48#define ALTERA_JTAGUART_CONTROL_WE_MSK 0x00000002
49#define ALTERA_JTAGUART_CONTROL_RI_MSK 0x00000100
50#define ALTERA_JTAGUART_CONTROL_RI_OFF 8
51#define ALTERA_JTAGUART_CONTROL_WI_MSK 0x00000200
52#define ALTERA_JTAGUART_CONTROL_AC_MSK 0x00000400
53#define ALTERA_JTAGUART_CONTROL_WSPACE_MSK 0xFFFF0000
54#define ALTERA_JTAGUART_CONTROL_WSPACE_OFF 16
55
56/*
57 * Local per-uart structure.
58 */
59struct altera_jtaguart {
60 struct uart_port port;
61 unsigned int sigs; /* Local copy of line sigs */
62 unsigned long imr; /* Local IMR mirror */
63};
64
65static unsigned int altera_jtaguart_tx_empty(struct uart_port *port)
66{
67 return (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
68 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) ? TIOCSER_TEMT : 0;
69}
70
71static unsigned int altera_jtaguart_get_mctrl(struct uart_port *port)
72{
73 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
74}
75
76static void altera_jtaguart_set_mctrl(struct uart_port *port, unsigned int sigs)
77{
78}
79
80static void altera_jtaguart_start_tx(struct uart_port *port)
81{
82 struct altera_jtaguart *pp =
83 container_of(port, struct altera_jtaguart, port);
84
85 pp->imr |= ALTERA_JTAGUART_CONTROL_WE_MSK;
86 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
87}
88
89static void altera_jtaguart_stop_tx(struct uart_port *port)
90{
91 struct altera_jtaguart *pp =
92 container_of(port, struct altera_jtaguart, port);
93
94 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
95 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
96}
97
98static void altera_jtaguart_stop_rx(struct uart_port *port)
99{
100 struct altera_jtaguart *pp =
101 container_of(port, struct altera_jtaguart, port);
102
103 pp->imr &= ~ALTERA_JTAGUART_CONTROL_RE_MSK;
104 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
105}
106
107static void altera_jtaguart_break_ctl(struct uart_port *port, int break_state)
108{
109}
110
111static void altera_jtaguart_enable_ms(struct uart_port *port)
112{
113}
114
115static void altera_jtaguart_set_termios(struct uart_port *port,
116 struct ktermios *termios,
117 struct ktermios *old)
118{
119 /* Just copy the old termios settings back */
120 if (old)
121 tty_termios_copy_hw(termios, old);
122}
123
124static void altera_jtaguart_rx_chars(struct altera_jtaguart *pp)
125{
126 struct uart_port *port = &pp->port;
127 unsigned char ch, flag;
128 unsigned long status;
129
130 while ((status = readl(port->membase + ALTERA_JTAGUART_DATA_REG)) &
131 ALTERA_JTAGUART_DATA_RVALID_MSK) {
132 ch = status & ALTERA_JTAGUART_DATA_DATA_MSK;
133 flag = TTY_NORMAL;
134 port->icount.rx++;
135
136 if (uart_handle_sysrq_char(port, ch))
137 continue;
138 uart_insert_char(port, 0, 0, ch, flag);
139 }
140
141 tty_flip_buffer_push(port->state->port.tty);
142}
143
144static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp)
145{
146 struct uart_port *port = &pp->port;
147 struct circ_buf *xmit = &port->state->xmit;
148 unsigned int pending, count;
149
150 if (port->x_char) {
151 /* Send special char - probably flow control */
152 writel(port->x_char, port->membase + ALTERA_JTAGUART_DATA_REG);
153 port->x_char = 0;
154 port->icount.tx++;
155 return;
156 }
157
158 pending = uart_circ_chars_pending(xmit);
159 if (pending > 0) {
160 count = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
161 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) >>
162 ALTERA_JTAGUART_CONTROL_WSPACE_OFF;
163 if (count > pending)
164 count = pending;
165 if (count > 0) {
166 pending -= count;
167 while (count--) {
168 writel(xmit->buf[xmit->tail],
169 port->membase + ALTERA_JTAGUART_DATA_REG);
170 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
171 port->icount.tx++;
172 }
173 if (pending < WAKEUP_CHARS)
174 uart_write_wakeup(port);
175 }
176 }
177
178 if (pending == 0) {
179 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
180 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
181 }
182}
183
184static irqreturn_t altera_jtaguart_interrupt(int irq, void *data)
185{
186 struct uart_port *port = data;
187 struct altera_jtaguart *pp =
188 container_of(port, struct altera_jtaguart, port);
189 unsigned int isr;
190
191 isr = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) >>
192 ALTERA_JTAGUART_CONTROL_RI_OFF) & pp->imr;
193
194 spin_lock(&port->lock);
195
196 if (isr & ALTERA_JTAGUART_CONTROL_RE_MSK)
197 altera_jtaguart_rx_chars(pp);
198 if (isr & ALTERA_JTAGUART_CONTROL_WE_MSK)
199 altera_jtaguart_tx_chars(pp);
200
201 spin_unlock(&port->lock);
202
203 return IRQ_RETVAL(isr);
204}
205
206static void altera_jtaguart_config_port(struct uart_port *port, int flags)
207{
208 port->type = PORT_ALTERA_JTAGUART;
209
210 /* Clear mask, so no surprise interrupts. */
211 writel(0, port->membase + ALTERA_JTAGUART_CONTROL_REG);
212}
213
214static int altera_jtaguart_startup(struct uart_port *port)
215{
216 struct altera_jtaguart *pp =
217 container_of(port, struct altera_jtaguart, port);
218 unsigned long flags;
219 int ret;
220
221 ret = request_irq(port->irq, altera_jtaguart_interrupt, IRQF_DISABLED,
222 DRV_NAME, port);
223 if (ret) {
224 pr_err(DRV_NAME ": unable to attach Altera JTAG UART %d "
225 "interrupt vector=%d\n", port->line, port->irq);
226 return ret;
227 }
228
229 spin_lock_irqsave(&port->lock, flags);
230
231 /* Enable RX interrupts now */
232 pp->imr = ALTERA_JTAGUART_CONTROL_RE_MSK;
233 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
234
235 spin_unlock_irqrestore(&port->lock, flags);
236
237 return 0;
238}
239
240static void altera_jtaguart_shutdown(struct uart_port *port)
241{
242 struct altera_jtaguart *pp =
243 container_of(port, struct altera_jtaguart, port);
244 unsigned long flags;
245
246 spin_lock_irqsave(&port->lock, flags);
247
248 /* Disable all interrupts now */
249 pp->imr = 0;
250 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
251
252 spin_unlock_irqrestore(&port->lock, flags);
253
254 free_irq(port->irq, port);
255}
256
257static const char *altera_jtaguart_type(struct uart_port *port)
258{
259 return (port->type == PORT_ALTERA_JTAGUART) ? "Altera JTAG UART" : NULL;
260}
261
262static int altera_jtaguart_request_port(struct uart_port *port)
263{
264 /* UARTs always present */
265 return 0;
266}
267
268static void altera_jtaguart_release_port(struct uart_port *port)
269{
270 /* Nothing to release... */
271}
272
273static int altera_jtaguart_verify_port(struct uart_port *port,
274 struct serial_struct *ser)
275{
276 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ALTERA_JTAGUART)
277 return -EINVAL;
278 return 0;
279}
280
281/*
282 * Define the basic serial functions we support.
283 */
284static struct uart_ops altera_jtaguart_ops = {
285 .tx_empty = altera_jtaguart_tx_empty,
286 .get_mctrl = altera_jtaguart_get_mctrl,
287 .set_mctrl = altera_jtaguart_set_mctrl,
288 .start_tx = altera_jtaguart_start_tx,
289 .stop_tx = altera_jtaguart_stop_tx,
290 .stop_rx = altera_jtaguart_stop_rx,
291 .enable_ms = altera_jtaguart_enable_ms,
292 .break_ctl = altera_jtaguart_break_ctl,
293 .startup = altera_jtaguart_startup,
294 .shutdown = altera_jtaguart_shutdown,
295 .set_termios = altera_jtaguart_set_termios,
296 .type = altera_jtaguart_type,
297 .request_port = altera_jtaguart_request_port,
298 .release_port = altera_jtaguart_release_port,
299 .config_port = altera_jtaguart_config_port,
300 .verify_port = altera_jtaguart_verify_port,
301};
302
303#define ALTERA_JTAGUART_MAXPORTS 1
304static struct altera_jtaguart altera_jtaguart_ports[ALTERA_JTAGUART_MAXPORTS];
305
306#if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
307
308int __init early_altera_jtaguart_setup(struct altera_jtaguart_platform_uart
309 *platp)
310{
311 struct uart_port *port;
312 int i;
313
314 for (i = 0; i < ALTERA_JTAGUART_MAXPORTS && platp[i].mapbase; i++) {
315 port = &altera_jtaguart_ports[i].port;
316
317 port->line = i;
318 port->type = PORT_ALTERA_JTAGUART;
319 port->mapbase = platp[i].mapbase;
320 port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE);
321 port->iotype = SERIAL_IO_MEM;
322 port->irq = platp[i].irq;
323 port->flags = ASYNC_BOOT_AUTOCONF;
324 port->ops = &altera_jtaguart_ops;
325 }
326
327 return 0;
328}
329
330#if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
331static void altera_jtaguart_console_putc(struct console *co, const char c)
332{
333 struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
334 unsigned long status;
335 unsigned long flags;
336
337 spin_lock_irqsave(&port->lock, flags);
338 while (((status = readl(port->membase + ALTERA_JTAGUART_CONTROL_REG)) &
339 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
340 if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0) {
341 spin_unlock_irqrestore(&port->lock, flags);
342 return; /* no connection activity */
343 }
344 spin_unlock_irqrestore(&port->lock, flags);
345 cpu_relax();
346 spin_lock_irqsave(&port->lock, flags);
347 }
348 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
349 spin_unlock_irqrestore(&port->lock, flags);
350}
351#else
352static void altera_jtaguart_console_putc(struct console *co, const char c)
353{
354 struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
355 unsigned long flags;
356
357 spin_lock_irqsave(&port->lock, flags);
358 while ((readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
359 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
360 spin_unlock_irqrestore(&port->lock, flags);
361 cpu_relax();
362 spin_lock_irqsave(&port->lock, flags);
363 }
364 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
365 spin_unlock_irqrestore(&port->lock, flags);
366}
367#endif
368
369static void altera_jtaguart_console_write(struct console *co, const char *s,
370 unsigned int count)
371{
372 for (; count; count--, s++) {
373 altera_jtaguart_console_putc(co, *s);
374 if (*s == '\n')
375 altera_jtaguart_console_putc(co, '\r');
376 }
377}
378
379static int __init altera_jtaguart_console_setup(struct console *co,
380 char *options)
381{
382 struct uart_port *port;
383
384 if (co->index < 0 || co->index >= ALTERA_JTAGUART_MAXPORTS)
385 return -EINVAL;
386 port = &altera_jtaguart_ports[co->index].port;
387 if (port->membase == 0)
388 return -ENODEV;
389 return 0;
390}
391
392static struct uart_driver altera_jtaguart_driver;
393
394static struct console altera_jtaguart_console = {
395 .name = "ttyJ",
396 .write = altera_jtaguart_console_write,
397 .device = uart_console_device,
398 .setup = altera_jtaguart_console_setup,
399 .flags = CON_PRINTBUFFER,
400 .index = -1,
401 .data = &altera_jtaguart_driver,
402};
403
404static int __init altera_jtaguart_console_init(void)
405{
406 register_console(&altera_jtaguart_console);
407 return 0;
408}
409
410console_initcall(altera_jtaguart_console_init);
411
412#define ALTERA_JTAGUART_CONSOLE (&altera_jtaguart_console)
413
414#else
415
416#define ALTERA_JTAGUART_CONSOLE NULL
417
418#endif /* CONFIG_ALTERA_JTAGUART_CONSOLE */
419
420static struct uart_driver altera_jtaguart_driver = {
421 .owner = THIS_MODULE,
422 .driver_name = "altera_jtaguart",
423 .dev_name = "ttyJ",
424 .major = ALTERA_JTAGUART_MAJOR,
425 .minor = ALTERA_JTAGUART_MINOR,
426 .nr = ALTERA_JTAGUART_MAXPORTS,
427 .cons = ALTERA_JTAGUART_CONSOLE,
428};
429
430static int __devinit altera_jtaguart_probe(struct platform_device *pdev)
431{
432 struct altera_jtaguart_platform_uart *platp = pdev->dev.platform_data;
433 struct uart_port *port;
434 int i;
435
436 for (i = 0; i < ALTERA_JTAGUART_MAXPORTS && platp[i].mapbase; i++) {
437 port = &altera_jtaguart_ports[i].port;
438
439 port->line = i;
440 port->type = PORT_ALTERA_JTAGUART;
441 port->mapbase = platp[i].mapbase;
442 port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE);
443 port->iotype = SERIAL_IO_MEM;
444 port->irq = platp[i].irq;
445 port->ops = &altera_jtaguart_ops;
446 port->flags = ASYNC_BOOT_AUTOCONF;
447
448 uart_add_one_port(&altera_jtaguart_driver, port);
449 }
450
451 return 0;
452}
453
454static int __devexit altera_jtaguart_remove(struct platform_device *pdev)
455{
456 struct uart_port *port;
457 int i;
458
459 for (i = 0; i < ALTERA_JTAGUART_MAXPORTS; i++) {
460 port = &altera_jtaguart_ports[i].port;
461 if (port)
462 uart_remove_one_port(&altera_jtaguart_driver, port);
463 }
464
465 return 0;
466}
467
468static struct platform_driver altera_jtaguart_platform_driver = {
469 .probe = altera_jtaguart_probe,
470 .remove = __devexit_p(altera_jtaguart_remove),
471 .driver = {
472 .name = DRV_NAME,
473 .owner = THIS_MODULE,
474 },
475};
476
477static int __init altera_jtaguart_init(void)
478{
479 int rc;
480
481 rc = uart_register_driver(&altera_jtaguart_driver);
482 if (rc)
483 return rc;
484 rc = platform_driver_register(&altera_jtaguart_platform_driver);
485 if (rc) {
486 uart_unregister_driver(&altera_jtaguart_driver);
487 return rc;
488 }
489 return 0;
490}
491
492static void __exit altera_jtaguart_exit(void)
493{
494 platform_driver_unregister(&altera_jtaguart_platform_driver);
495 uart_unregister_driver(&altera_jtaguart_driver);
496}
497
498module_init(altera_jtaguart_init);
499module_exit(altera_jtaguart_exit);
500
501MODULE_DESCRIPTION("Altera JTAG UART driver");
502MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
503MODULE_LICENSE("GPL");
504MODULE_ALIAS("platform:" DRV_NAME);
diff --git a/drivers/tty/serial/altera_uart.c b/drivers/tty/serial/altera_uart.c
new file mode 100644
index 000000000000..721216292a50
--- /dev/null
+++ b/drivers/tty/serial/altera_uart.c
@@ -0,0 +1,608 @@
1/*
2 * altera_uart.c -- Altera UART driver
3 *
4 * Based on mcf.c -- Freescale ColdFire UART driver
5 *
6 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
7 * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
8 * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
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
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/timer.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/console.h>
22#include <linux/tty.h>
23#include <linux/tty_flip.h>
24#include <linux/serial.h>
25#include <linux/serial_core.h>
26#include <linux/platform_device.h>
27#include <linux/io.h>
28#include <linux/altera_uart.h>
29
30#define DRV_NAME "altera_uart"
31#define SERIAL_ALTERA_MAJOR 204
32#define SERIAL_ALTERA_MINOR 213
33
34/*
35 * Altera UART register definitions according to the Nios UART datasheet:
36 * http://www.altera.com/literature/ds/ds_nios_uart.pdf
37 */
38
39#define ALTERA_UART_SIZE 32
40
41#define ALTERA_UART_RXDATA_REG 0
42#define ALTERA_UART_TXDATA_REG 4
43#define ALTERA_UART_STATUS_REG 8
44#define ALTERA_UART_CONTROL_REG 12
45#define ALTERA_UART_DIVISOR_REG 16
46#define ALTERA_UART_EOP_REG 20
47
48#define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
49#define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
50#define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
51#define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
52#define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
53#define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
54#define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
55#define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
56#define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
57#define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
58#define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
59#define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
60
61 /* Enable interrupt on... */
62#define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
63#define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
64#define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
65#define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
66#define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
67#define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
68#define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
69#define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
70#define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
71
72#define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
73#define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
74#define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
75#define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
76
77/*
78 * Local per-uart structure.
79 */
80struct altera_uart {
81 struct uart_port port;
82 struct timer_list tmr;
83 unsigned int sigs; /* Local copy of line sigs */
84 unsigned short imr; /* Local IMR mirror */
85};
86
87static u32 altera_uart_readl(struct uart_port *port, int reg)
88{
89 struct altera_uart_platform_uart *platp = port->private_data;
90
91 return readl(port->membase + (reg << platp->bus_shift));
92}
93
94static void altera_uart_writel(struct uart_port *port, u32 dat, int reg)
95{
96 struct altera_uart_platform_uart *platp = port->private_data;
97
98 writel(dat, port->membase + (reg << platp->bus_shift));
99}
100
101static unsigned int altera_uart_tx_empty(struct uart_port *port)
102{
103 return (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
104 ALTERA_UART_STATUS_TMT_MSK) ? TIOCSER_TEMT : 0;
105}
106
107static unsigned int altera_uart_get_mctrl(struct uart_port *port)
108{
109 struct altera_uart *pp = container_of(port, struct altera_uart, port);
110 unsigned int sigs;
111
112 sigs = (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
113 ALTERA_UART_STATUS_CTS_MSK) ? TIOCM_CTS : 0;
114 sigs |= (pp->sigs & TIOCM_RTS);
115
116 return sigs;
117}
118
119static void altera_uart_set_mctrl(struct uart_port *port, unsigned int sigs)
120{
121 struct altera_uart *pp = container_of(port, struct altera_uart, port);
122
123 pp->sigs = sigs;
124 if (sigs & TIOCM_RTS)
125 pp->imr |= ALTERA_UART_CONTROL_RTS_MSK;
126 else
127 pp->imr &= ~ALTERA_UART_CONTROL_RTS_MSK;
128 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
129}
130
131static void altera_uart_start_tx(struct uart_port *port)
132{
133 struct altera_uart *pp = container_of(port, struct altera_uart, port);
134
135 pp->imr |= ALTERA_UART_CONTROL_TRDY_MSK;
136 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
137}
138
139static void altera_uart_stop_tx(struct uart_port *port)
140{
141 struct altera_uart *pp = container_of(port, struct altera_uart, port);
142
143 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
144 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
145}
146
147static void altera_uart_stop_rx(struct uart_port *port)
148{
149 struct altera_uart *pp = container_of(port, struct altera_uart, port);
150
151 pp->imr &= ~ALTERA_UART_CONTROL_RRDY_MSK;
152 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
153}
154
155static void altera_uart_break_ctl(struct uart_port *port, int break_state)
156{
157 struct altera_uart *pp = container_of(port, struct altera_uart, port);
158 unsigned long flags;
159
160 spin_lock_irqsave(&port->lock, flags);
161 if (break_state == -1)
162 pp->imr |= ALTERA_UART_CONTROL_TRBK_MSK;
163 else
164 pp->imr &= ~ALTERA_UART_CONTROL_TRBK_MSK;
165 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
166 spin_unlock_irqrestore(&port->lock, flags);
167}
168
169static void altera_uart_enable_ms(struct uart_port *port)
170{
171}
172
173static void altera_uart_set_termios(struct uart_port *port,
174 struct ktermios *termios,
175 struct ktermios *old)
176{
177 unsigned long flags;
178 unsigned int baud, baudclk;
179
180 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
181 baudclk = port->uartclk / baud;
182
183 if (old)
184 tty_termios_copy_hw(termios, old);
185 tty_termios_encode_baud_rate(termios, baud, baud);
186
187 spin_lock_irqsave(&port->lock, flags);
188 uart_update_timeout(port, termios->c_cflag, baud);
189 altera_uart_writel(port, baudclk, ALTERA_UART_DIVISOR_REG);
190 spin_unlock_irqrestore(&port->lock, flags);
191}
192
193static void altera_uart_rx_chars(struct altera_uart *pp)
194{
195 struct uart_port *port = &pp->port;
196 unsigned char ch, flag;
197 unsigned short status;
198
199 while ((status = altera_uart_readl(port, ALTERA_UART_STATUS_REG)) &
200 ALTERA_UART_STATUS_RRDY_MSK) {
201 ch = altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
202 flag = TTY_NORMAL;
203 port->icount.rx++;
204
205 if (status & ALTERA_UART_STATUS_E_MSK) {
206 altera_uart_writel(port, status,
207 ALTERA_UART_STATUS_REG);
208
209 if (status & ALTERA_UART_STATUS_BRK_MSK) {
210 port->icount.brk++;
211 if (uart_handle_break(port))
212 continue;
213 } else if (status & ALTERA_UART_STATUS_PE_MSK) {
214 port->icount.parity++;
215 } else if (status & ALTERA_UART_STATUS_ROE_MSK) {
216 port->icount.overrun++;
217 } else if (status & ALTERA_UART_STATUS_FE_MSK) {
218 port->icount.frame++;
219 }
220
221 status &= port->read_status_mask;
222
223 if (status & ALTERA_UART_STATUS_BRK_MSK)
224 flag = TTY_BREAK;
225 else if (status & ALTERA_UART_STATUS_PE_MSK)
226 flag = TTY_PARITY;
227 else if (status & ALTERA_UART_STATUS_FE_MSK)
228 flag = TTY_FRAME;
229 }
230
231 if (uart_handle_sysrq_char(port, ch))
232 continue;
233 uart_insert_char(port, status, ALTERA_UART_STATUS_ROE_MSK, ch,
234 flag);
235 }
236
237 tty_flip_buffer_push(port->state->port.tty);
238}
239
240static void altera_uart_tx_chars(struct altera_uart *pp)
241{
242 struct uart_port *port = &pp->port;
243 struct circ_buf *xmit = &port->state->xmit;
244
245 if (port->x_char) {
246 /* Send special char - probably flow control */
247 altera_uart_writel(port, port->x_char, ALTERA_UART_TXDATA_REG);
248 port->x_char = 0;
249 port->icount.tx++;
250 return;
251 }
252
253 while (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
254 ALTERA_UART_STATUS_TRDY_MSK) {
255 if (xmit->head == xmit->tail)
256 break;
257 altera_uart_writel(port, xmit->buf[xmit->tail],
258 ALTERA_UART_TXDATA_REG);
259 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
260 port->icount.tx++;
261 }
262
263 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
264 uart_write_wakeup(port);
265
266 if (xmit->head == xmit->tail) {
267 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
268 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
269 }
270}
271
272static irqreturn_t altera_uart_interrupt(int irq, void *data)
273{
274 struct uart_port *port = data;
275 struct altera_uart *pp = container_of(port, struct altera_uart, port);
276 unsigned int isr;
277
278 isr = altera_uart_readl(port, ALTERA_UART_STATUS_REG) & pp->imr;
279
280 spin_lock(&port->lock);
281 if (isr & ALTERA_UART_STATUS_RRDY_MSK)
282 altera_uart_rx_chars(pp);
283 if (isr & ALTERA_UART_STATUS_TRDY_MSK)
284 altera_uart_tx_chars(pp);
285 spin_unlock(&port->lock);
286
287 return IRQ_RETVAL(isr);
288}
289
290static void altera_uart_timer(unsigned long data)
291{
292 struct uart_port *port = (void *)data;
293 struct altera_uart *pp = container_of(port, struct altera_uart, port);
294
295 altera_uart_interrupt(0, port);
296 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
297}
298
299static void altera_uart_config_port(struct uart_port *port, int flags)
300{
301 port->type = PORT_ALTERA_UART;
302
303 /* Clear mask, so no surprise interrupts. */
304 altera_uart_writel(port, 0, ALTERA_UART_CONTROL_REG);
305 /* Clear status register */
306 altera_uart_writel(port, 0, ALTERA_UART_STATUS_REG);
307}
308
309static int altera_uart_startup(struct uart_port *port)
310{
311 struct altera_uart *pp = container_of(port, struct altera_uart, port);
312 unsigned long flags;
313 int ret;
314
315 if (!port->irq) {
316 setup_timer(&pp->tmr, altera_uart_timer, (unsigned long)port);
317 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
318 return 0;
319 }
320
321 ret = request_irq(port->irq, altera_uart_interrupt, IRQF_DISABLED,
322 DRV_NAME, port);
323 if (ret) {
324 pr_err(DRV_NAME ": unable to attach Altera UART %d "
325 "interrupt vector=%d\n", port->line, port->irq);
326 return ret;
327 }
328
329 spin_lock_irqsave(&port->lock, flags);
330
331 /* Enable RX interrupts now */
332 pp->imr = ALTERA_UART_CONTROL_RRDY_MSK;
333 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
334
335 spin_unlock_irqrestore(&port->lock, flags);
336
337 return 0;
338}
339
340static void altera_uart_shutdown(struct uart_port *port)
341{
342 struct altera_uart *pp = container_of(port, struct altera_uart, port);
343 unsigned long flags;
344
345 spin_lock_irqsave(&port->lock, flags);
346
347 /* Disable all interrupts now */
348 pp->imr = 0;
349 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
350
351 spin_unlock_irqrestore(&port->lock, flags);
352
353 if (port->irq)
354 free_irq(port->irq, port);
355 else
356 del_timer_sync(&pp->tmr);
357}
358
359static const char *altera_uart_type(struct uart_port *port)
360{
361 return (port->type == PORT_ALTERA_UART) ? "Altera UART" : NULL;
362}
363
364static int altera_uart_request_port(struct uart_port *port)
365{
366 /* UARTs always present */
367 return 0;
368}
369
370static void altera_uart_release_port(struct uart_port *port)
371{
372 /* Nothing to release... */
373}
374
375static int altera_uart_verify_port(struct uart_port *port,
376 struct serial_struct *ser)
377{
378 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ALTERA_UART))
379 return -EINVAL;
380 return 0;
381}
382
383/*
384 * Define the basic serial functions we support.
385 */
386static struct uart_ops altera_uart_ops = {
387 .tx_empty = altera_uart_tx_empty,
388 .get_mctrl = altera_uart_get_mctrl,
389 .set_mctrl = altera_uart_set_mctrl,
390 .start_tx = altera_uart_start_tx,
391 .stop_tx = altera_uart_stop_tx,
392 .stop_rx = altera_uart_stop_rx,
393 .enable_ms = altera_uart_enable_ms,
394 .break_ctl = altera_uart_break_ctl,
395 .startup = altera_uart_startup,
396 .shutdown = altera_uart_shutdown,
397 .set_termios = altera_uart_set_termios,
398 .type = altera_uart_type,
399 .request_port = altera_uart_request_port,
400 .release_port = altera_uart_release_port,
401 .config_port = altera_uart_config_port,
402 .verify_port = altera_uart_verify_port,
403};
404
405static struct altera_uart altera_uart_ports[CONFIG_SERIAL_ALTERA_UART_MAXPORTS];
406
407#if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
408
409int __init early_altera_uart_setup(struct altera_uart_platform_uart *platp)
410{
411 struct uart_port *port;
412 int i;
413
414 for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS && platp[i].mapbase; i++) {
415 port = &altera_uart_ports[i].port;
416
417 port->line = i;
418 port->type = PORT_ALTERA_UART;
419 port->mapbase = platp[i].mapbase;
420 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
421 port->iotype = SERIAL_IO_MEM;
422 port->irq = platp[i].irq;
423 port->uartclk = platp[i].uartclk;
424 port->flags = UPF_BOOT_AUTOCONF;
425 port->ops = &altera_uart_ops;
426 port->private_data = platp;
427 }
428
429 return 0;
430}
431
432static void altera_uart_console_putc(struct uart_port *port, const char c)
433{
434 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
435 ALTERA_UART_STATUS_TRDY_MSK))
436 cpu_relax();
437
438 writel(c, port->membase + ALTERA_UART_TXDATA_REG);
439}
440
441static void altera_uart_console_write(struct console *co, const char *s,
442 unsigned int count)
443{
444 struct uart_port *port = &(altera_uart_ports + co->index)->port;
445
446 for (; count; count--, s++) {
447 altera_uart_console_putc(port, *s);
448 if (*s == '\n')
449 altera_uart_console_putc(port, '\r');
450 }
451}
452
453static int __init altera_uart_console_setup(struct console *co, char *options)
454{
455 struct uart_port *port;
456 int baud = CONFIG_SERIAL_ALTERA_UART_BAUDRATE;
457 int bits = 8;
458 int parity = 'n';
459 int flow = 'n';
460
461 if (co->index < 0 || co->index >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
462 return -EINVAL;
463 port = &altera_uart_ports[co->index].port;
464 if (!port->membase)
465 return -ENODEV;
466
467 if (options)
468 uart_parse_options(options, &baud, &parity, &bits, &flow);
469
470 return uart_set_options(port, co, baud, parity, bits, flow);
471}
472
473static struct uart_driver altera_uart_driver;
474
475static struct console altera_uart_console = {
476 .name = "ttyAL",
477 .write = altera_uart_console_write,
478 .device = uart_console_device,
479 .setup = altera_uart_console_setup,
480 .flags = CON_PRINTBUFFER,
481 .index = -1,
482 .data = &altera_uart_driver,
483};
484
485static int __init altera_uart_console_init(void)
486{
487 register_console(&altera_uart_console);
488 return 0;
489}
490
491console_initcall(altera_uart_console_init);
492
493#define ALTERA_UART_CONSOLE (&altera_uart_console)
494
495#else
496
497#define ALTERA_UART_CONSOLE NULL
498
499#endif /* CONFIG_ALTERA_UART_CONSOLE */
500
501/*
502 * Define the altera_uart UART driver structure.
503 */
504static struct uart_driver altera_uart_driver = {
505 .owner = THIS_MODULE,
506 .driver_name = DRV_NAME,
507 .dev_name = "ttyAL",
508 .major = SERIAL_ALTERA_MAJOR,
509 .minor = SERIAL_ALTERA_MINOR,
510 .nr = CONFIG_SERIAL_ALTERA_UART_MAXPORTS,
511 .cons = ALTERA_UART_CONSOLE,
512};
513
514static int __devinit altera_uart_probe(struct platform_device *pdev)
515{
516 struct altera_uart_platform_uart *platp = pdev->dev.platform_data;
517 struct uart_port *port;
518 struct resource *res_mem;
519 struct resource *res_irq;
520 int i = pdev->id;
521
522 /* -1 emphasizes that the platform must have one port, no .N suffix */
523 if (i == -1)
524 i = 0;
525
526 if (i >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
527 return -EINVAL;
528
529 port = &altera_uart_ports[i].port;
530
531 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
532 if (res_mem)
533 port->mapbase = res_mem->start;
534 else if (platp->mapbase)
535 port->mapbase = platp->mapbase;
536 else
537 return -EINVAL;
538
539 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
540 if (res_irq)
541 port->irq = res_irq->start;
542 else if (platp->irq)
543 port->irq = platp->irq;
544
545 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
546 if (!port->membase)
547 return -ENOMEM;
548
549 port->line = i;
550 port->type = PORT_ALTERA_UART;
551 port->iotype = SERIAL_IO_MEM;
552 port->uartclk = platp->uartclk;
553 port->ops = &altera_uart_ops;
554 port->flags = UPF_BOOT_AUTOCONF;
555 port->private_data = platp;
556
557 uart_add_one_port(&altera_uart_driver, port);
558
559 return 0;
560}
561
562static int __devexit altera_uart_remove(struct platform_device *pdev)
563{
564 struct uart_port *port = &altera_uart_ports[pdev->id].port;
565
566 uart_remove_one_port(&altera_uart_driver, port);
567 return 0;
568}
569
570static struct platform_driver altera_uart_platform_driver = {
571 .probe = altera_uart_probe,
572 .remove = __devexit_p(altera_uart_remove),
573 .driver = {
574 .name = DRV_NAME,
575 .owner = THIS_MODULE,
576 .pm = NULL,
577 },
578};
579
580static int __init altera_uart_init(void)
581{
582 int rc;
583
584 rc = uart_register_driver(&altera_uart_driver);
585 if (rc)
586 return rc;
587 rc = platform_driver_register(&altera_uart_platform_driver);
588 if (rc) {
589 uart_unregister_driver(&altera_uart_driver);
590 return rc;
591 }
592 return 0;
593}
594
595static void __exit altera_uart_exit(void)
596{
597 platform_driver_unregister(&altera_uart_platform_driver);
598 uart_unregister_driver(&altera_uart_driver);
599}
600
601module_init(altera_uart_init);
602module_exit(altera_uart_exit);
603
604MODULE_DESCRIPTION("Altera UART driver");
605MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
606MODULE_LICENSE("GPL");
607MODULE_ALIAS("platform:" DRV_NAME);
608MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_ALTERA_MAJOR);
diff --git a/drivers/tty/serial/amba-pl010.c b/drivers/tty/serial/amba-pl010.c
new file mode 100644
index 000000000000..2904aa044126
--- /dev/null
+++ b/drivers/tty/serial/amba-pl010.c
@@ -0,0 +1,825 @@
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 * This is a generic driver for ARM AMBA-type serial ports. They
26 * have a lot of 16550-like features, but are not register compatible.
27 * Note that although they do have CTS, DCD and DSR inputs, they do
28 * not have an RI input, nor do they have DTR or RTS outputs. If
29 * required, these have to be supplied via some other means (eg, GPIO)
30 * and hooked into this driver.
31 */
32
33#if defined(CONFIG_SERIAL_AMBA_PL010_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34#define SUPPORT_SYSRQ
35#endif
36
37#include <linux/module.h>
38#include <linux/ioport.h>
39#include <linux/init.h>
40#include <linux/console.h>
41#include <linux/sysrq.h>
42#include <linux/device.h>
43#include <linux/tty.h>
44#include <linux/tty_flip.h>
45#include <linux/serial_core.h>
46#include <linux/serial.h>
47#include <linux/amba/bus.h>
48#include <linux/amba/serial.h>
49#include <linux/clk.h>
50#include <linux/slab.h>
51
52#include <asm/io.h>
53
54#define UART_NR 8
55
56#define SERIAL_AMBA_MAJOR 204
57#define SERIAL_AMBA_MINOR 16
58#define SERIAL_AMBA_NR UART_NR
59
60#define AMBA_ISR_PASS_LIMIT 256
61
62#define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0)
63#define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0)
64
65#define UART_DUMMY_RSR_RX 256
66#define UART_PORT_SIZE 64
67
68/*
69 * We wrap our port structure around the generic uart_port.
70 */
71struct uart_amba_port {
72 struct uart_port port;
73 struct clk *clk;
74 struct amba_device *dev;
75 struct amba_pl010_data *data;
76 unsigned int old_status;
77};
78
79static void pl010_stop_tx(struct uart_port *port)
80{
81 struct uart_amba_port *uap = (struct uart_amba_port *)port;
82 unsigned int cr;
83
84 cr = readb(uap->port.membase + UART010_CR);
85 cr &= ~UART010_CR_TIE;
86 writel(cr, uap->port.membase + UART010_CR);
87}
88
89static void pl010_start_tx(struct uart_port *port)
90{
91 struct uart_amba_port *uap = (struct uart_amba_port *)port;
92 unsigned int cr;
93
94 cr = readb(uap->port.membase + UART010_CR);
95 cr |= UART010_CR_TIE;
96 writel(cr, uap->port.membase + UART010_CR);
97}
98
99static void pl010_stop_rx(struct uart_port *port)
100{
101 struct uart_amba_port *uap = (struct uart_amba_port *)port;
102 unsigned int cr;
103
104 cr = readb(uap->port.membase + UART010_CR);
105 cr &= ~(UART010_CR_RIE | UART010_CR_RTIE);
106 writel(cr, uap->port.membase + UART010_CR);
107}
108
109static void pl010_enable_ms(struct uart_port *port)
110{
111 struct uart_amba_port *uap = (struct uart_amba_port *)port;
112 unsigned int cr;
113
114 cr = readb(uap->port.membase + UART010_CR);
115 cr |= UART010_CR_MSIE;
116 writel(cr, uap->port.membase + UART010_CR);
117}
118
119static void pl010_rx_chars(struct uart_amba_port *uap)
120{
121 struct tty_struct *tty = uap->port.state->port.tty;
122 unsigned int status, ch, flag, rsr, max_count = 256;
123
124 status = readb(uap->port.membase + UART01x_FR);
125 while (UART_RX_DATA(status) && max_count--) {
126 ch = readb(uap->port.membase + UART01x_DR);
127 flag = TTY_NORMAL;
128
129 uap->port.icount.rx++;
130
131 /*
132 * Note that the error handling code is
133 * out of the main execution path
134 */
135 rsr = readb(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX;
136 if (unlikely(rsr & UART01x_RSR_ANY)) {
137 writel(0, uap->port.membase + UART01x_ECR);
138
139 if (rsr & UART01x_RSR_BE) {
140 rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
141 uap->port.icount.brk++;
142 if (uart_handle_break(&uap->port))
143 goto ignore_char;
144 } else if (rsr & UART01x_RSR_PE)
145 uap->port.icount.parity++;
146 else if (rsr & UART01x_RSR_FE)
147 uap->port.icount.frame++;
148 if (rsr & UART01x_RSR_OE)
149 uap->port.icount.overrun++;
150
151 rsr &= uap->port.read_status_mask;
152
153 if (rsr & UART01x_RSR_BE)
154 flag = TTY_BREAK;
155 else if (rsr & UART01x_RSR_PE)
156 flag = TTY_PARITY;
157 else if (rsr & UART01x_RSR_FE)
158 flag = TTY_FRAME;
159 }
160
161 if (uart_handle_sysrq_char(&uap->port, ch))
162 goto ignore_char;
163
164 uart_insert_char(&uap->port, rsr, UART01x_RSR_OE, ch, flag);
165
166 ignore_char:
167 status = readb(uap->port.membase + UART01x_FR);
168 }
169 spin_unlock(&uap->port.lock);
170 tty_flip_buffer_push(tty);
171 spin_lock(&uap->port.lock);
172}
173
174static void pl010_tx_chars(struct uart_amba_port *uap)
175{
176 struct circ_buf *xmit = &uap->port.state->xmit;
177 int count;
178
179 if (uap->port.x_char) {
180 writel(uap->port.x_char, uap->port.membase + UART01x_DR);
181 uap->port.icount.tx++;
182 uap->port.x_char = 0;
183 return;
184 }
185 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
186 pl010_stop_tx(&uap->port);
187 return;
188 }
189
190 count = uap->port.fifosize >> 1;
191 do {
192 writel(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
193 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
194 uap->port.icount.tx++;
195 if (uart_circ_empty(xmit))
196 break;
197 } while (--count > 0);
198
199 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
200 uart_write_wakeup(&uap->port);
201
202 if (uart_circ_empty(xmit))
203 pl010_stop_tx(&uap->port);
204}
205
206static void pl010_modem_status(struct uart_amba_port *uap)
207{
208 unsigned int status, delta;
209
210 writel(0, uap->port.membase + UART010_ICR);
211
212 status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
213
214 delta = status ^ uap->old_status;
215 uap->old_status = status;
216
217 if (!delta)
218 return;
219
220 if (delta & UART01x_FR_DCD)
221 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
222
223 if (delta & UART01x_FR_DSR)
224 uap->port.icount.dsr++;
225
226 if (delta & UART01x_FR_CTS)
227 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
228
229 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
230}
231
232static irqreturn_t pl010_int(int irq, void *dev_id)
233{
234 struct uart_amba_port *uap = dev_id;
235 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
236 int handled = 0;
237
238 spin_lock(&uap->port.lock);
239
240 status = readb(uap->port.membase + UART010_IIR);
241 if (status) {
242 do {
243 if (status & (UART010_IIR_RTIS | UART010_IIR_RIS))
244 pl010_rx_chars(uap);
245 if (status & UART010_IIR_MIS)
246 pl010_modem_status(uap);
247 if (status & UART010_IIR_TIS)
248 pl010_tx_chars(uap);
249
250 if (pass_counter-- == 0)
251 break;
252
253 status = readb(uap->port.membase + UART010_IIR);
254 } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS |
255 UART010_IIR_TIS));
256 handled = 1;
257 }
258
259 spin_unlock(&uap->port.lock);
260
261 return IRQ_RETVAL(handled);
262}
263
264static unsigned int pl010_tx_empty(struct uart_port *port)
265{
266 struct uart_amba_port *uap = (struct uart_amba_port *)port;
267 unsigned int status = readb(uap->port.membase + UART01x_FR);
268 return status & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT;
269}
270
271static unsigned int pl010_get_mctrl(struct uart_port *port)
272{
273 struct uart_amba_port *uap = (struct uart_amba_port *)port;
274 unsigned int result = 0;
275 unsigned int status;
276
277 status = readb(uap->port.membase + UART01x_FR);
278 if (status & UART01x_FR_DCD)
279 result |= TIOCM_CAR;
280 if (status & UART01x_FR_DSR)
281 result |= TIOCM_DSR;
282 if (status & UART01x_FR_CTS)
283 result |= TIOCM_CTS;
284
285 return result;
286}
287
288static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl)
289{
290 struct uart_amba_port *uap = (struct uart_amba_port *)port;
291
292 if (uap->data)
293 uap->data->set_mctrl(uap->dev, uap->port.membase, mctrl);
294}
295
296static void pl010_break_ctl(struct uart_port *port, int break_state)
297{
298 struct uart_amba_port *uap = (struct uart_amba_port *)port;
299 unsigned long flags;
300 unsigned int lcr_h;
301
302 spin_lock_irqsave(&uap->port.lock, flags);
303 lcr_h = readb(uap->port.membase + UART010_LCRH);
304 if (break_state == -1)
305 lcr_h |= UART01x_LCRH_BRK;
306 else
307 lcr_h &= ~UART01x_LCRH_BRK;
308 writel(lcr_h, uap->port.membase + UART010_LCRH);
309 spin_unlock_irqrestore(&uap->port.lock, flags);
310}
311
312static int pl010_startup(struct uart_port *port)
313{
314 struct uart_amba_port *uap = (struct uart_amba_port *)port;
315 int retval;
316
317 /*
318 * Try to enable the clock producer.
319 */
320 retval = clk_enable(uap->clk);
321 if (retval)
322 goto out;
323
324 uap->port.uartclk = clk_get_rate(uap->clk);
325
326 /*
327 * Allocate the IRQ
328 */
329 retval = request_irq(uap->port.irq, pl010_int, 0, "uart-pl010", uap);
330 if (retval)
331 goto clk_dis;
332
333 /*
334 * initialise the old status of the modem signals
335 */
336 uap->old_status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
337
338 /*
339 * Finally, enable interrupts
340 */
341 writel(UART01x_CR_UARTEN | UART010_CR_RIE | UART010_CR_RTIE,
342 uap->port.membase + UART010_CR);
343
344 return 0;
345
346 clk_dis:
347 clk_disable(uap->clk);
348 out:
349 return retval;
350}
351
352static void pl010_shutdown(struct uart_port *port)
353{
354 struct uart_amba_port *uap = (struct uart_amba_port *)port;
355
356 /*
357 * Free the interrupt
358 */
359 free_irq(uap->port.irq, uap);
360
361 /*
362 * disable all interrupts, disable the port
363 */
364 writel(0, uap->port.membase + UART010_CR);
365
366 /* disable break condition and fifos */
367 writel(readb(uap->port.membase + UART010_LCRH) &
368 ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN),
369 uap->port.membase + UART010_LCRH);
370
371 /*
372 * Shut down the clock producer
373 */
374 clk_disable(uap->clk);
375}
376
377static void
378pl010_set_termios(struct uart_port *port, struct ktermios *termios,
379 struct ktermios *old)
380{
381 struct uart_amba_port *uap = (struct uart_amba_port *)port;
382 unsigned int lcr_h, old_cr;
383 unsigned long flags;
384 unsigned int baud, quot;
385
386 /*
387 * Ask the core to calculate the divisor for us.
388 */
389 baud = uart_get_baud_rate(port, termios, old, 0, uap->port.uartclk/16);
390 quot = uart_get_divisor(port, baud);
391
392 switch (termios->c_cflag & CSIZE) {
393 case CS5:
394 lcr_h = UART01x_LCRH_WLEN_5;
395 break;
396 case CS6:
397 lcr_h = UART01x_LCRH_WLEN_6;
398 break;
399 case CS7:
400 lcr_h = UART01x_LCRH_WLEN_7;
401 break;
402 default: // CS8
403 lcr_h = UART01x_LCRH_WLEN_8;
404 break;
405 }
406 if (termios->c_cflag & CSTOPB)
407 lcr_h |= UART01x_LCRH_STP2;
408 if (termios->c_cflag & PARENB) {
409 lcr_h |= UART01x_LCRH_PEN;
410 if (!(termios->c_cflag & PARODD))
411 lcr_h |= UART01x_LCRH_EPS;
412 }
413 if (uap->port.fifosize > 1)
414 lcr_h |= UART01x_LCRH_FEN;
415
416 spin_lock_irqsave(&uap->port.lock, flags);
417
418 /*
419 * Update the per-port timeout.
420 */
421 uart_update_timeout(port, termios->c_cflag, baud);
422
423 uap->port.read_status_mask = UART01x_RSR_OE;
424 if (termios->c_iflag & INPCK)
425 uap->port.read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
426 if (termios->c_iflag & (BRKINT | PARMRK))
427 uap->port.read_status_mask |= UART01x_RSR_BE;
428
429 /*
430 * Characters to ignore
431 */
432 uap->port.ignore_status_mask = 0;
433 if (termios->c_iflag & IGNPAR)
434 uap->port.ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
435 if (termios->c_iflag & IGNBRK) {
436 uap->port.ignore_status_mask |= UART01x_RSR_BE;
437 /*
438 * If we're ignoring parity and break indicators,
439 * ignore overruns too (for real raw support).
440 */
441 if (termios->c_iflag & IGNPAR)
442 uap->port.ignore_status_mask |= UART01x_RSR_OE;
443 }
444
445 /*
446 * Ignore all characters if CREAD is not set.
447 */
448 if ((termios->c_cflag & CREAD) == 0)
449 uap->port.ignore_status_mask |= UART_DUMMY_RSR_RX;
450
451 /* first, disable everything */
452 old_cr = readb(uap->port.membase + UART010_CR) & ~UART010_CR_MSIE;
453
454 if (UART_ENABLE_MS(port, termios->c_cflag))
455 old_cr |= UART010_CR_MSIE;
456
457 writel(0, uap->port.membase + UART010_CR);
458
459 /* Set baud rate */
460 quot -= 1;
461 writel((quot & 0xf00) >> 8, uap->port.membase + UART010_LCRM);
462 writel(quot & 0xff, uap->port.membase + UART010_LCRL);
463
464 /*
465 * ----------v----------v----------v----------v-----
466 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
467 * ----------^----------^----------^----------^-----
468 */
469 writel(lcr_h, uap->port.membase + UART010_LCRH);
470 writel(old_cr, uap->port.membase + UART010_CR);
471
472 spin_unlock_irqrestore(&uap->port.lock, flags);
473}
474
475static void pl010_set_ldisc(struct uart_port *port, int new)
476{
477 if (new == N_PPS) {
478 port->flags |= UPF_HARDPPS_CD;
479 pl010_enable_ms(port);
480 } else
481 port->flags &= ~UPF_HARDPPS_CD;
482}
483
484static const char *pl010_type(struct uart_port *port)
485{
486 return port->type == PORT_AMBA ? "AMBA" : NULL;
487}
488
489/*
490 * Release the memory region(s) being used by 'port'
491 */
492static void pl010_release_port(struct uart_port *port)
493{
494 release_mem_region(port->mapbase, UART_PORT_SIZE);
495}
496
497/*
498 * Request the memory region(s) being used by 'port'
499 */
500static int pl010_request_port(struct uart_port *port)
501{
502 return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
503 != NULL ? 0 : -EBUSY;
504}
505
506/*
507 * Configure/autoconfigure the port.
508 */
509static void pl010_config_port(struct uart_port *port, int flags)
510{
511 if (flags & UART_CONFIG_TYPE) {
512 port->type = PORT_AMBA;
513 pl010_request_port(port);
514 }
515}
516
517/*
518 * verify the new serial_struct (for TIOCSSERIAL).
519 */
520static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
521{
522 int ret = 0;
523 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
524 ret = -EINVAL;
525 if (ser->irq < 0 || ser->irq >= nr_irqs)
526 ret = -EINVAL;
527 if (ser->baud_base < 9600)
528 ret = -EINVAL;
529 return ret;
530}
531
532static struct uart_ops amba_pl010_pops = {
533 .tx_empty = pl010_tx_empty,
534 .set_mctrl = pl010_set_mctrl,
535 .get_mctrl = pl010_get_mctrl,
536 .stop_tx = pl010_stop_tx,
537 .start_tx = pl010_start_tx,
538 .stop_rx = pl010_stop_rx,
539 .enable_ms = pl010_enable_ms,
540 .break_ctl = pl010_break_ctl,
541 .startup = pl010_startup,
542 .shutdown = pl010_shutdown,
543 .set_termios = pl010_set_termios,
544 .set_ldisc = pl010_set_ldisc,
545 .type = pl010_type,
546 .release_port = pl010_release_port,
547 .request_port = pl010_request_port,
548 .config_port = pl010_config_port,
549 .verify_port = pl010_verify_port,
550};
551
552static struct uart_amba_port *amba_ports[UART_NR];
553
554#ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
555
556static void pl010_console_putchar(struct uart_port *port, int ch)
557{
558 struct uart_amba_port *uap = (struct uart_amba_port *)port;
559 unsigned int status;
560
561 do {
562 status = readb(uap->port.membase + UART01x_FR);
563 barrier();
564 } while (!UART_TX_READY(status));
565 writel(ch, uap->port.membase + UART01x_DR);
566}
567
568static void
569pl010_console_write(struct console *co, const char *s, unsigned int count)
570{
571 struct uart_amba_port *uap = amba_ports[co->index];
572 unsigned int status, old_cr;
573
574 clk_enable(uap->clk);
575
576 /*
577 * First save the CR then disable the interrupts
578 */
579 old_cr = readb(uap->port.membase + UART010_CR);
580 writel(UART01x_CR_UARTEN, uap->port.membase + UART010_CR);
581
582 uart_console_write(&uap->port, s, count, pl010_console_putchar);
583
584 /*
585 * Finally, wait for transmitter to become empty
586 * and restore the TCR
587 */
588 do {
589 status = readb(uap->port.membase + UART01x_FR);
590 barrier();
591 } while (status & UART01x_FR_BUSY);
592 writel(old_cr, uap->port.membase + UART010_CR);
593
594 clk_disable(uap->clk);
595}
596
597static void __init
598pl010_console_get_options(struct uart_amba_port *uap, int *baud,
599 int *parity, int *bits)
600{
601 if (readb(uap->port.membase + UART010_CR) & UART01x_CR_UARTEN) {
602 unsigned int lcr_h, quot;
603 lcr_h = readb(uap->port.membase + UART010_LCRH);
604
605 *parity = 'n';
606 if (lcr_h & UART01x_LCRH_PEN) {
607 if (lcr_h & UART01x_LCRH_EPS)
608 *parity = 'e';
609 else
610 *parity = 'o';
611 }
612
613 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
614 *bits = 7;
615 else
616 *bits = 8;
617
618 quot = readb(uap->port.membase + UART010_LCRL) |
619 readb(uap->port.membase + UART010_LCRM) << 8;
620 *baud = uap->port.uartclk / (16 * (quot + 1));
621 }
622}
623
624static int __init pl010_console_setup(struct console *co, char *options)
625{
626 struct uart_amba_port *uap;
627 int baud = 38400;
628 int bits = 8;
629 int parity = 'n';
630 int flow = 'n';
631
632 /*
633 * Check whether an invalid uart number has been specified, and
634 * if so, search for the first available port that does have
635 * console support.
636 */
637 if (co->index >= UART_NR)
638 co->index = 0;
639 uap = amba_ports[co->index];
640 if (!uap)
641 return -ENODEV;
642
643 uap->port.uartclk = clk_get_rate(uap->clk);
644
645 if (options)
646 uart_parse_options(options, &baud, &parity, &bits, &flow);
647 else
648 pl010_console_get_options(uap, &baud, &parity, &bits);
649
650 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
651}
652
653static struct uart_driver amba_reg;
654static struct console amba_console = {
655 .name = "ttyAM",
656 .write = pl010_console_write,
657 .device = uart_console_device,
658 .setup = pl010_console_setup,
659 .flags = CON_PRINTBUFFER,
660 .index = -1,
661 .data = &amba_reg,
662};
663
664#define AMBA_CONSOLE &amba_console
665#else
666#define AMBA_CONSOLE NULL
667#endif
668
669static struct uart_driver amba_reg = {
670 .owner = THIS_MODULE,
671 .driver_name = "ttyAM",
672 .dev_name = "ttyAM",
673 .major = SERIAL_AMBA_MAJOR,
674 .minor = SERIAL_AMBA_MINOR,
675 .nr = UART_NR,
676 .cons = AMBA_CONSOLE,
677};
678
679static int pl010_probe(struct amba_device *dev, struct amba_id *id)
680{
681 struct uart_amba_port *uap;
682 void __iomem *base;
683 int i, ret;
684
685 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
686 if (amba_ports[i] == NULL)
687 break;
688
689 if (i == ARRAY_SIZE(amba_ports)) {
690 ret = -EBUSY;
691 goto out;
692 }
693
694 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
695 if (!uap) {
696 ret = -ENOMEM;
697 goto out;
698 }
699
700 base = ioremap(dev->res.start, resource_size(&dev->res));
701 if (!base) {
702 ret = -ENOMEM;
703 goto free;
704 }
705
706 uap->clk = clk_get(&dev->dev, NULL);
707 if (IS_ERR(uap->clk)) {
708 ret = PTR_ERR(uap->clk);
709 goto unmap;
710 }
711
712 uap->port.dev = &dev->dev;
713 uap->port.mapbase = dev->res.start;
714 uap->port.membase = base;
715 uap->port.iotype = UPIO_MEM;
716 uap->port.irq = dev->irq[0];
717 uap->port.fifosize = 16;
718 uap->port.ops = &amba_pl010_pops;
719 uap->port.flags = UPF_BOOT_AUTOCONF;
720 uap->port.line = i;
721 uap->dev = dev;
722 uap->data = dev->dev.platform_data;
723
724 amba_ports[i] = uap;
725
726 amba_set_drvdata(dev, uap);
727 ret = uart_add_one_port(&amba_reg, &uap->port);
728 if (ret) {
729 amba_set_drvdata(dev, NULL);
730 amba_ports[i] = NULL;
731 clk_put(uap->clk);
732 unmap:
733 iounmap(base);
734 free:
735 kfree(uap);
736 }
737 out:
738 return ret;
739}
740
741static int pl010_remove(struct amba_device *dev)
742{
743 struct uart_amba_port *uap = amba_get_drvdata(dev);
744 int i;
745
746 amba_set_drvdata(dev, NULL);
747
748 uart_remove_one_port(&amba_reg, &uap->port);
749
750 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
751 if (amba_ports[i] == uap)
752 amba_ports[i] = NULL;
753
754 iounmap(uap->port.membase);
755 clk_put(uap->clk);
756 kfree(uap);
757 return 0;
758}
759
760static int pl010_suspend(struct amba_device *dev, pm_message_t state)
761{
762 struct uart_amba_port *uap = amba_get_drvdata(dev);
763
764 if (uap)
765 uart_suspend_port(&amba_reg, &uap->port);
766
767 return 0;
768}
769
770static int pl010_resume(struct amba_device *dev)
771{
772 struct uart_amba_port *uap = amba_get_drvdata(dev);
773
774 if (uap)
775 uart_resume_port(&amba_reg, &uap->port);
776
777 return 0;
778}
779
780static struct amba_id pl010_ids[] = {
781 {
782 .id = 0x00041010,
783 .mask = 0x000fffff,
784 },
785 { 0, 0 },
786};
787
788static struct amba_driver pl010_driver = {
789 .drv = {
790 .name = "uart-pl010",
791 },
792 .id_table = pl010_ids,
793 .probe = pl010_probe,
794 .remove = pl010_remove,
795 .suspend = pl010_suspend,
796 .resume = pl010_resume,
797};
798
799static int __init pl010_init(void)
800{
801 int ret;
802
803 printk(KERN_INFO "Serial: AMBA driver\n");
804
805 ret = uart_register_driver(&amba_reg);
806 if (ret == 0) {
807 ret = amba_driver_register(&pl010_driver);
808 if (ret)
809 uart_unregister_driver(&amba_reg);
810 }
811 return ret;
812}
813
814static void __exit pl010_exit(void)
815{
816 amba_driver_unregister(&pl010_driver);
817 uart_unregister_driver(&amba_reg);
818}
819
820module_init(pl010_init);
821module_exit(pl010_exit);
822
823MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
824MODULE_DESCRIPTION("ARM AMBA serial port driver");
825MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
new file mode 100644
index 000000000000..e76d7d000128
--- /dev/null
+++ b/drivers/tty/serial/amba-pl011.c
@@ -0,0 +1,1519 @@
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 * Copyright (C) 2010 ST-Ericsson SA
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 * This is a generic driver for ARM AMBA-type serial ports. They
27 * have a lot of 16550-like features, but are not register compatible.
28 * Note that although they do have CTS, DCD and DSR inputs, they do
29 * not have an RI input, nor do they have DTR or RTS outputs. If
30 * required, these have to be supplied via some other means (eg, GPIO)
31 * and hooked into this driver.
32 */
33
34#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
35#define SUPPORT_SYSRQ
36#endif
37
38#include <linux/module.h>
39#include <linux/ioport.h>
40#include <linux/init.h>
41#include <linux/console.h>
42#include <linux/sysrq.h>
43#include <linux/device.h>
44#include <linux/tty.h>
45#include <linux/tty_flip.h>
46#include <linux/serial_core.h>
47#include <linux/serial.h>
48#include <linux/amba/bus.h>
49#include <linux/amba/serial.h>
50#include <linux/clk.h>
51#include <linux/slab.h>
52#include <linux/dmaengine.h>
53#include <linux/dma-mapping.h>
54#include <linux/scatterlist.h>
55
56#include <asm/io.h>
57#include <asm/sizes.h>
58
59#define UART_NR 14
60
61#define SERIAL_AMBA_MAJOR 204
62#define SERIAL_AMBA_MINOR 64
63#define SERIAL_AMBA_NR UART_NR
64
65#define AMBA_ISR_PASS_LIMIT 256
66
67#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
68#define UART_DUMMY_DR_RX (1 << 16)
69
70/* There is by now at least one vendor with differing details, so handle it */
71struct vendor_data {
72 unsigned int ifls;
73 unsigned int fifosize;
74 unsigned int lcrh_tx;
75 unsigned int lcrh_rx;
76 bool oversampling;
77 bool dma_threshold;
78};
79
80static struct vendor_data vendor_arm = {
81 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
82 .fifosize = 16,
83 .lcrh_tx = UART011_LCRH,
84 .lcrh_rx = UART011_LCRH,
85 .oversampling = false,
86 .dma_threshold = false,
87};
88
89static struct vendor_data vendor_st = {
90 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
91 .fifosize = 64,
92 .lcrh_tx = ST_UART011_LCRH_TX,
93 .lcrh_rx = ST_UART011_LCRH_RX,
94 .oversampling = true,
95 .dma_threshold = true,
96};
97
98/* Deals with DMA transactions */
99struct pl011_dmatx_data {
100 struct dma_chan *chan;
101 struct scatterlist sg;
102 char *buf;
103 bool queued;
104};
105
106/*
107 * We wrap our port structure around the generic uart_port.
108 */
109struct uart_amba_port {
110 struct uart_port port;
111 struct clk *clk;
112 const struct vendor_data *vendor;
113 unsigned int dmacr; /* dma control reg */
114 unsigned int im; /* interrupt mask */
115 unsigned int old_status;
116 unsigned int fifosize; /* vendor-specific */
117 unsigned int lcrh_tx; /* vendor-specific */
118 unsigned int lcrh_rx; /* vendor-specific */
119 bool autorts;
120 char type[12];
121#ifdef CONFIG_DMA_ENGINE
122 /* DMA stuff */
123 bool using_dma;
124 struct pl011_dmatx_data dmatx;
125#endif
126};
127
128/*
129 * All the DMA operation mode stuff goes inside this ifdef.
130 * This assumes that you have a generic DMA device interface,
131 * no custom DMA interfaces are supported.
132 */
133#ifdef CONFIG_DMA_ENGINE
134
135#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
136
137static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
138{
139 /* DMA is the sole user of the platform data right now */
140 struct amba_pl011_data *plat = uap->port.dev->platform_data;
141 struct dma_slave_config tx_conf = {
142 .dst_addr = uap->port.mapbase + UART01x_DR,
143 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
144 .direction = DMA_TO_DEVICE,
145 .dst_maxburst = uap->fifosize >> 1,
146 };
147 struct dma_chan *chan;
148 dma_cap_mask_t mask;
149
150 /* We need platform data */
151 if (!plat || !plat->dma_filter) {
152 dev_info(uap->port.dev, "no DMA platform data\n");
153 return;
154 }
155
156 /* Try to acquire a generic DMA engine slave channel */
157 dma_cap_zero(mask);
158 dma_cap_set(DMA_SLAVE, mask);
159
160 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
161 if (!chan) {
162 dev_err(uap->port.dev, "no TX DMA channel!\n");
163 return;
164 }
165
166 dmaengine_slave_config(chan, &tx_conf);
167 uap->dmatx.chan = chan;
168
169 dev_info(uap->port.dev, "DMA channel TX %s\n",
170 dma_chan_name(uap->dmatx.chan));
171}
172
173#ifndef MODULE
174/*
175 * Stack up the UARTs and let the above initcall be done at device
176 * initcall time, because the serial driver is called as an arch
177 * initcall, and at this time the DMA subsystem is not yet registered.
178 * At this point the driver will switch over to using DMA where desired.
179 */
180struct dma_uap {
181 struct list_head node;
182 struct uart_amba_port *uap;
183};
184
185static LIST_HEAD(pl011_dma_uarts);
186
187static int __init pl011_dma_initcall(void)
188{
189 struct list_head *node, *tmp;
190
191 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
192 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
193 pl011_dma_probe_initcall(dmau->uap);
194 list_del(node);
195 kfree(dmau);
196 }
197 return 0;
198}
199
200device_initcall(pl011_dma_initcall);
201
202static void pl011_dma_probe(struct uart_amba_port *uap)
203{
204 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
205 if (dmau) {
206 dmau->uap = uap;
207 list_add_tail(&dmau->node, &pl011_dma_uarts);
208 }
209}
210#else
211static void pl011_dma_probe(struct uart_amba_port *uap)
212{
213 pl011_dma_probe_initcall(uap);
214}
215#endif
216
217static void pl011_dma_remove(struct uart_amba_port *uap)
218{
219 /* TODO: remove the initcall if it has not yet executed */
220 if (uap->dmatx.chan)
221 dma_release_channel(uap->dmatx.chan);
222}
223
224
225/* Forward declare this for the refill routine */
226static int pl011_dma_tx_refill(struct uart_amba_port *uap);
227
228/*
229 * The current DMA TX buffer has been sent.
230 * Try to queue up another DMA buffer.
231 */
232static void pl011_dma_tx_callback(void *data)
233{
234 struct uart_amba_port *uap = data;
235 struct pl011_dmatx_data *dmatx = &uap->dmatx;
236 unsigned long flags;
237 u16 dmacr;
238
239 spin_lock_irqsave(&uap->port.lock, flags);
240 if (uap->dmatx.queued)
241 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
242 DMA_TO_DEVICE);
243
244 dmacr = uap->dmacr;
245 uap->dmacr = dmacr & ~UART011_TXDMAE;
246 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
247
248 /*
249 * If TX DMA was disabled, it means that we've stopped the DMA for
250 * some reason (eg, XOFF received, or we want to send an X-char.)
251 *
252 * Note: we need to be careful here of a potential race between DMA
253 * and the rest of the driver - if the driver disables TX DMA while
254 * a TX buffer completing, we must update the tx queued status to
255 * get further refills (hence we check dmacr).
256 */
257 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
258 uart_circ_empty(&uap->port.state->xmit)) {
259 uap->dmatx.queued = false;
260 spin_unlock_irqrestore(&uap->port.lock, flags);
261 return;
262 }
263
264 if (pl011_dma_tx_refill(uap) <= 0) {
265 /*
266 * We didn't queue a DMA buffer for some reason, but we
267 * have data pending to be sent. Re-enable the TX IRQ.
268 */
269 uap->im |= UART011_TXIM;
270 writew(uap->im, uap->port.membase + UART011_IMSC);
271 }
272 spin_unlock_irqrestore(&uap->port.lock, flags);
273}
274
275/*
276 * Try to refill the TX DMA buffer.
277 * Locking: called with port lock held and IRQs disabled.
278 * Returns:
279 * 1 if we queued up a TX DMA buffer.
280 * 0 if we didn't want to handle this by DMA
281 * <0 on error
282 */
283static int pl011_dma_tx_refill(struct uart_amba_port *uap)
284{
285 struct pl011_dmatx_data *dmatx = &uap->dmatx;
286 struct dma_chan *chan = dmatx->chan;
287 struct dma_device *dma_dev = chan->device;
288 struct dma_async_tx_descriptor *desc;
289 struct circ_buf *xmit = &uap->port.state->xmit;
290 unsigned int count;
291
292 /*
293 * Try to avoid the overhead involved in using DMA if the
294 * transaction fits in the first half of the FIFO, by using
295 * the standard interrupt handling. This ensures that we
296 * issue a uart_write_wakeup() at the appropriate time.
297 */
298 count = uart_circ_chars_pending(xmit);
299 if (count < (uap->fifosize >> 1)) {
300 uap->dmatx.queued = false;
301 return 0;
302 }
303
304 /*
305 * Bodge: don't send the last character by DMA, as this
306 * will prevent XON from notifying us to restart DMA.
307 */
308 count -= 1;
309
310 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
311 if (count > PL011_DMA_BUFFER_SIZE)
312 count = PL011_DMA_BUFFER_SIZE;
313
314 if (xmit->tail < xmit->head)
315 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
316 else {
317 size_t first = UART_XMIT_SIZE - xmit->tail;
318 size_t second = xmit->head;
319
320 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
321 if (second)
322 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
323 }
324
325 dmatx->sg.length = count;
326
327 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
328 uap->dmatx.queued = false;
329 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
330 return -EBUSY;
331 }
332
333 desc = dma_dev->device_prep_slave_sg(chan, &dmatx->sg, 1, DMA_TO_DEVICE,
334 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
335 if (!desc) {
336 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
337 uap->dmatx.queued = false;
338 /*
339 * If DMA cannot be used right now, we complete this
340 * transaction via IRQ and let the TTY layer retry.
341 */
342 dev_dbg(uap->port.dev, "TX DMA busy\n");
343 return -EBUSY;
344 }
345
346 /* Some data to go along to the callback */
347 desc->callback = pl011_dma_tx_callback;
348 desc->callback_param = uap;
349
350 /* All errors should happen at prepare time */
351 dmaengine_submit(desc);
352
353 /* Fire the DMA transaction */
354 dma_dev->device_issue_pending(chan);
355
356 uap->dmacr |= UART011_TXDMAE;
357 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
358 uap->dmatx.queued = true;
359
360 /*
361 * Now we know that DMA will fire, so advance the ring buffer
362 * with the stuff we just dispatched.
363 */
364 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
365 uap->port.icount.tx += count;
366
367 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
368 uart_write_wakeup(&uap->port);
369
370 return 1;
371}
372
373/*
374 * We received a transmit interrupt without a pending X-char but with
375 * pending characters.
376 * Locking: called with port lock held and IRQs disabled.
377 * Returns:
378 * false if we want to use PIO to transmit
379 * true if we queued a DMA buffer
380 */
381static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
382{
383 if (!uap->using_dma)
384 return false;
385
386 /*
387 * If we already have a TX buffer queued, but received a
388 * TX interrupt, it will be because we've just sent an X-char.
389 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
390 */
391 if (uap->dmatx.queued) {
392 uap->dmacr |= UART011_TXDMAE;
393 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
394 uap->im &= ~UART011_TXIM;
395 writew(uap->im, uap->port.membase + UART011_IMSC);
396 return true;
397 }
398
399 /*
400 * We don't have a TX buffer queued, so try to queue one.
401 * If we succesfully queued a buffer, mask the TX IRQ.
402 */
403 if (pl011_dma_tx_refill(uap) > 0) {
404 uap->im &= ~UART011_TXIM;
405 writew(uap->im, uap->port.membase + UART011_IMSC);
406 return true;
407 }
408 return false;
409}
410
411/*
412 * Stop the DMA transmit (eg, due to received XOFF).
413 * Locking: called with port lock held and IRQs disabled.
414 */
415static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
416{
417 if (uap->dmatx.queued) {
418 uap->dmacr &= ~UART011_TXDMAE;
419 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
420 }
421}
422
423/*
424 * Try to start a DMA transmit, or in the case of an XON/OFF
425 * character queued for send, try to get that character out ASAP.
426 * Locking: called with port lock held and IRQs disabled.
427 * Returns:
428 * false if we want the TX IRQ to be enabled
429 * true if we have a buffer queued
430 */
431static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
432{
433 u16 dmacr;
434
435 if (!uap->using_dma)
436 return false;
437
438 if (!uap->port.x_char) {
439 /* no X-char, try to push chars out in DMA mode */
440 bool ret = true;
441
442 if (!uap->dmatx.queued) {
443 if (pl011_dma_tx_refill(uap) > 0) {
444 uap->im &= ~UART011_TXIM;
445 ret = true;
446 } else {
447 uap->im |= UART011_TXIM;
448 ret = false;
449 }
450 writew(uap->im, uap->port.membase + UART011_IMSC);
451 } else if (!(uap->dmacr & UART011_TXDMAE)) {
452 uap->dmacr |= UART011_TXDMAE;
453 writew(uap->dmacr,
454 uap->port.membase + UART011_DMACR);
455 }
456 return ret;
457 }
458
459 /*
460 * We have an X-char to send. Disable DMA to prevent it loading
461 * the TX fifo, and then see if we can stuff it into the FIFO.
462 */
463 dmacr = uap->dmacr;
464 uap->dmacr &= ~UART011_TXDMAE;
465 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
466
467 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
468 /*
469 * No space in the FIFO, so enable the transmit interrupt
470 * so we know when there is space. Note that once we've
471 * loaded the character, we should just re-enable DMA.
472 */
473 return false;
474 }
475
476 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
477 uap->port.icount.tx++;
478 uap->port.x_char = 0;
479
480 /* Success - restore the DMA state */
481 uap->dmacr = dmacr;
482 writew(dmacr, uap->port.membase + UART011_DMACR);
483
484 return true;
485}
486
487/*
488 * Flush the transmit buffer.
489 * Locking: called with port lock held and IRQs disabled.
490 */
491static void pl011_dma_flush_buffer(struct uart_port *port)
492{
493 struct uart_amba_port *uap = (struct uart_amba_port *)port;
494
495 if (!uap->using_dma)
496 return;
497
498 /* Avoid deadlock with the DMA engine callback */
499 spin_unlock(&uap->port.lock);
500 dmaengine_terminate_all(uap->dmatx.chan);
501 spin_lock(&uap->port.lock);
502 if (uap->dmatx.queued) {
503 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
504 DMA_TO_DEVICE);
505 uap->dmatx.queued = false;
506 uap->dmacr &= ~UART011_TXDMAE;
507 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
508 }
509}
510
511
512static void pl011_dma_startup(struct uart_amba_port *uap)
513{
514 if (!uap->dmatx.chan)
515 return;
516
517 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
518 if (!uap->dmatx.buf) {
519 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
520 uap->port.fifosize = uap->fifosize;
521 return;
522 }
523
524 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
525
526 /* The DMA buffer is now the FIFO the TTY subsystem can use */
527 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
528 uap->using_dma = true;
529
530 /* Turn on DMA error (RX/TX will be enabled on demand) */
531 uap->dmacr |= UART011_DMAONERR;
532 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
533
534 /*
535 * ST Micro variants has some specific dma burst threshold
536 * compensation. Set this to 16 bytes, so burst will only
537 * be issued above/below 16 bytes.
538 */
539 if (uap->vendor->dma_threshold)
540 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
541 uap->port.membase + ST_UART011_DMAWM);
542}
543
544static void pl011_dma_shutdown(struct uart_amba_port *uap)
545{
546 if (!uap->using_dma)
547 return;
548
549 /* Disable RX and TX DMA */
550 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
551 barrier();
552
553 spin_lock_irq(&uap->port.lock);
554 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
555 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
556 spin_unlock_irq(&uap->port.lock);
557
558 /* In theory, this should already be done by pl011_dma_flush_buffer */
559 dmaengine_terminate_all(uap->dmatx.chan);
560 if (uap->dmatx.queued) {
561 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
562 DMA_TO_DEVICE);
563 uap->dmatx.queued = false;
564 }
565
566 kfree(uap->dmatx.buf);
567
568 uap->using_dma = false;
569}
570
571#else
572/* Blank functions if the DMA engine is not available */
573static inline void pl011_dma_probe(struct uart_amba_port *uap)
574{
575}
576
577static inline void pl011_dma_remove(struct uart_amba_port *uap)
578{
579}
580
581static inline void pl011_dma_startup(struct uart_amba_port *uap)
582{
583}
584
585static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
586{
587}
588
589static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
590{
591 return false;
592}
593
594static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
595{
596}
597
598static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
599{
600 return false;
601}
602
603#define pl011_dma_flush_buffer NULL
604#endif
605
606
607static void pl011_stop_tx(struct uart_port *port)
608{
609 struct uart_amba_port *uap = (struct uart_amba_port *)port;
610
611 uap->im &= ~UART011_TXIM;
612 writew(uap->im, uap->port.membase + UART011_IMSC);
613 pl011_dma_tx_stop(uap);
614}
615
616static void pl011_start_tx(struct uart_port *port)
617{
618 struct uart_amba_port *uap = (struct uart_amba_port *)port;
619
620 if (!pl011_dma_tx_start(uap)) {
621 uap->im |= UART011_TXIM;
622 writew(uap->im, uap->port.membase + UART011_IMSC);
623 }
624}
625
626static void pl011_stop_rx(struct uart_port *port)
627{
628 struct uart_amba_port *uap = (struct uart_amba_port *)port;
629
630 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
631 UART011_PEIM|UART011_BEIM|UART011_OEIM);
632 writew(uap->im, uap->port.membase + UART011_IMSC);
633}
634
635static void pl011_enable_ms(struct uart_port *port)
636{
637 struct uart_amba_port *uap = (struct uart_amba_port *)port;
638
639 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
640 writew(uap->im, uap->port.membase + UART011_IMSC);
641}
642
643static void pl011_rx_chars(struct uart_amba_port *uap)
644{
645 struct tty_struct *tty = uap->port.state->port.tty;
646 unsigned int status, ch, flag, max_count = 256;
647
648 status = readw(uap->port.membase + UART01x_FR);
649 while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
650 ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
651 flag = TTY_NORMAL;
652 uap->port.icount.rx++;
653
654 /*
655 * Note that the error handling code is
656 * out of the main execution path
657 */
658 if (unlikely(ch & UART_DR_ERROR)) {
659 if (ch & UART011_DR_BE) {
660 ch &= ~(UART011_DR_FE | UART011_DR_PE);
661 uap->port.icount.brk++;
662 if (uart_handle_break(&uap->port))
663 goto ignore_char;
664 } else if (ch & UART011_DR_PE)
665 uap->port.icount.parity++;
666 else if (ch & UART011_DR_FE)
667 uap->port.icount.frame++;
668 if (ch & UART011_DR_OE)
669 uap->port.icount.overrun++;
670
671 ch &= uap->port.read_status_mask;
672
673 if (ch & UART011_DR_BE)
674 flag = TTY_BREAK;
675 else if (ch & UART011_DR_PE)
676 flag = TTY_PARITY;
677 else if (ch & UART011_DR_FE)
678 flag = TTY_FRAME;
679 }
680
681 if (uart_handle_sysrq_char(&uap->port, ch & 255))
682 goto ignore_char;
683
684 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
685
686 ignore_char:
687 status = readw(uap->port.membase + UART01x_FR);
688 }
689 spin_unlock(&uap->port.lock);
690 tty_flip_buffer_push(tty);
691 spin_lock(&uap->port.lock);
692}
693
694static void pl011_tx_chars(struct uart_amba_port *uap)
695{
696 struct circ_buf *xmit = &uap->port.state->xmit;
697 int count;
698
699 if (uap->port.x_char) {
700 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
701 uap->port.icount.tx++;
702 uap->port.x_char = 0;
703 return;
704 }
705 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
706 pl011_stop_tx(&uap->port);
707 return;
708 }
709
710 /* If we are using DMA mode, try to send some characters. */
711 if (pl011_dma_tx_irq(uap))
712 return;
713
714 count = uap->fifosize >> 1;
715 do {
716 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
717 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
718 uap->port.icount.tx++;
719 if (uart_circ_empty(xmit))
720 break;
721 } while (--count > 0);
722
723 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
724 uart_write_wakeup(&uap->port);
725
726 if (uart_circ_empty(xmit))
727 pl011_stop_tx(&uap->port);
728}
729
730static void pl011_modem_status(struct uart_amba_port *uap)
731{
732 unsigned int status, delta;
733
734 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
735
736 delta = status ^ uap->old_status;
737 uap->old_status = status;
738
739 if (!delta)
740 return;
741
742 if (delta & UART01x_FR_DCD)
743 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
744
745 if (delta & UART01x_FR_DSR)
746 uap->port.icount.dsr++;
747
748 if (delta & UART01x_FR_CTS)
749 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
750
751 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
752}
753
754static irqreturn_t pl011_int(int irq, void *dev_id)
755{
756 struct uart_amba_port *uap = dev_id;
757 unsigned long flags;
758 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
759 int handled = 0;
760
761 spin_lock_irqsave(&uap->port.lock, flags);
762
763 status = readw(uap->port.membase + UART011_MIS);
764 if (status) {
765 do {
766 writew(status & ~(UART011_TXIS|UART011_RTIS|
767 UART011_RXIS),
768 uap->port.membase + UART011_ICR);
769
770 if (status & (UART011_RTIS|UART011_RXIS))
771 pl011_rx_chars(uap);
772 if (status & (UART011_DSRMIS|UART011_DCDMIS|
773 UART011_CTSMIS|UART011_RIMIS))
774 pl011_modem_status(uap);
775 if (status & UART011_TXIS)
776 pl011_tx_chars(uap);
777
778 if (pass_counter-- == 0)
779 break;
780
781 status = readw(uap->port.membase + UART011_MIS);
782 } while (status != 0);
783 handled = 1;
784 }
785
786 spin_unlock_irqrestore(&uap->port.lock, flags);
787
788 return IRQ_RETVAL(handled);
789}
790
791static unsigned int pl01x_tx_empty(struct uart_port *port)
792{
793 struct uart_amba_port *uap = (struct uart_amba_port *)port;
794 unsigned int status = readw(uap->port.membase + UART01x_FR);
795 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
796}
797
798static unsigned int pl01x_get_mctrl(struct uart_port *port)
799{
800 struct uart_amba_port *uap = (struct uart_amba_port *)port;
801 unsigned int result = 0;
802 unsigned int status = readw(uap->port.membase + UART01x_FR);
803
804#define TIOCMBIT(uartbit, tiocmbit) \
805 if (status & uartbit) \
806 result |= tiocmbit
807
808 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
809 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
810 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
811 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
812#undef TIOCMBIT
813 return result;
814}
815
816static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
817{
818 struct uart_amba_port *uap = (struct uart_amba_port *)port;
819 unsigned int cr;
820
821 cr = readw(uap->port.membase + UART011_CR);
822
823#define TIOCMBIT(tiocmbit, uartbit) \
824 if (mctrl & tiocmbit) \
825 cr |= uartbit; \
826 else \
827 cr &= ~uartbit
828
829 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
830 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
831 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
832 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
833 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
834
835 if (uap->autorts) {
836 /* We need to disable auto-RTS if we want to turn RTS off */
837 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
838 }
839#undef TIOCMBIT
840
841 writew(cr, uap->port.membase + UART011_CR);
842}
843
844static void pl011_break_ctl(struct uart_port *port, int break_state)
845{
846 struct uart_amba_port *uap = (struct uart_amba_port *)port;
847 unsigned long flags;
848 unsigned int lcr_h;
849
850 spin_lock_irqsave(&uap->port.lock, flags);
851 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
852 if (break_state == -1)
853 lcr_h |= UART01x_LCRH_BRK;
854 else
855 lcr_h &= ~UART01x_LCRH_BRK;
856 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
857 spin_unlock_irqrestore(&uap->port.lock, flags);
858}
859
860#ifdef CONFIG_CONSOLE_POLL
861static int pl010_get_poll_char(struct uart_port *port)
862{
863 struct uart_amba_port *uap = (struct uart_amba_port *)port;
864 unsigned int status;
865
866 status = readw(uap->port.membase + UART01x_FR);
867 if (status & UART01x_FR_RXFE)
868 return NO_POLL_CHAR;
869
870 return readw(uap->port.membase + UART01x_DR);
871}
872
873static void pl010_put_poll_char(struct uart_port *port,
874 unsigned char ch)
875{
876 struct uart_amba_port *uap = (struct uart_amba_port *)port;
877
878 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
879 barrier();
880
881 writew(ch, uap->port.membase + UART01x_DR);
882}
883
884#endif /* CONFIG_CONSOLE_POLL */
885
886static int pl011_startup(struct uart_port *port)
887{
888 struct uart_amba_port *uap = (struct uart_amba_port *)port;
889 unsigned int cr;
890 int retval;
891
892 /*
893 * Try to enable the clock producer.
894 */
895 retval = clk_enable(uap->clk);
896 if (retval)
897 goto out;
898
899 uap->port.uartclk = clk_get_rate(uap->clk);
900
901 /*
902 * Allocate the IRQ
903 */
904 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
905 if (retval)
906 goto clk_dis;
907
908 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
909
910 /*
911 * Provoke TX FIFO interrupt into asserting.
912 */
913 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
914 writew(cr, uap->port.membase + UART011_CR);
915 writew(0, uap->port.membase + UART011_FBRD);
916 writew(1, uap->port.membase + UART011_IBRD);
917 writew(0, uap->port.membase + uap->lcrh_rx);
918 if (uap->lcrh_tx != uap->lcrh_rx) {
919 int i;
920 /*
921 * Wait 10 PCLKs before writing LCRH_TX register,
922 * to get this delay write read only register 10 times
923 */
924 for (i = 0; i < 10; ++i)
925 writew(0xff, uap->port.membase + UART011_MIS);
926 writew(0, uap->port.membase + uap->lcrh_tx);
927 }
928 writew(0, uap->port.membase + UART01x_DR);
929 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
930 barrier();
931
932 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
933 writew(cr, uap->port.membase + UART011_CR);
934
935 /* Clear pending error interrupts */
936 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
937 uap->port.membase + UART011_ICR);
938
939 /*
940 * initialise the old status of the modem signals
941 */
942 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
943
944 /* Startup DMA */
945 pl011_dma_startup(uap);
946
947 /*
948 * Finally, enable interrupts
949 */
950 spin_lock_irq(&uap->port.lock);
951 uap->im = UART011_RXIM | UART011_RTIM;
952 writew(uap->im, uap->port.membase + UART011_IMSC);
953 spin_unlock_irq(&uap->port.lock);
954
955 return 0;
956
957 clk_dis:
958 clk_disable(uap->clk);
959 out:
960 return retval;
961}
962
963static void pl011_shutdown_channel(struct uart_amba_port *uap,
964 unsigned int lcrh)
965{
966 unsigned long val;
967
968 val = readw(uap->port.membase + lcrh);
969 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
970 writew(val, uap->port.membase + lcrh);
971}
972
973static void pl011_shutdown(struct uart_port *port)
974{
975 struct uart_amba_port *uap = (struct uart_amba_port *)port;
976
977 /*
978 * disable all interrupts
979 */
980 spin_lock_irq(&uap->port.lock);
981 uap->im = 0;
982 writew(uap->im, uap->port.membase + UART011_IMSC);
983 writew(0xffff, uap->port.membase + UART011_ICR);
984 spin_unlock_irq(&uap->port.lock);
985
986 pl011_dma_shutdown(uap);
987
988 /*
989 * Free the interrupt
990 */
991 free_irq(uap->port.irq, uap);
992
993 /*
994 * disable the port
995 */
996 uap->autorts = false;
997 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
998
999 /*
1000 * disable break condition and fifos
1001 */
1002 pl011_shutdown_channel(uap, uap->lcrh_rx);
1003 if (uap->lcrh_rx != uap->lcrh_tx)
1004 pl011_shutdown_channel(uap, uap->lcrh_tx);
1005
1006 /*
1007 * Shut down the clock producer
1008 */
1009 clk_disable(uap->clk);
1010}
1011
1012static void
1013pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1014 struct ktermios *old)
1015{
1016 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1017 unsigned int lcr_h, old_cr;
1018 unsigned long flags;
1019 unsigned int baud, quot, clkdiv;
1020
1021 if (uap->vendor->oversampling)
1022 clkdiv = 8;
1023 else
1024 clkdiv = 16;
1025
1026 /*
1027 * Ask the core to calculate the divisor for us.
1028 */
1029 baud = uart_get_baud_rate(port, termios, old, 0,
1030 port->uartclk / clkdiv);
1031
1032 if (baud > port->uartclk/16)
1033 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1034 else
1035 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
1036
1037 switch (termios->c_cflag & CSIZE) {
1038 case CS5:
1039 lcr_h = UART01x_LCRH_WLEN_5;
1040 break;
1041 case CS6:
1042 lcr_h = UART01x_LCRH_WLEN_6;
1043 break;
1044 case CS7:
1045 lcr_h = UART01x_LCRH_WLEN_7;
1046 break;
1047 default: // CS8
1048 lcr_h = UART01x_LCRH_WLEN_8;
1049 break;
1050 }
1051 if (termios->c_cflag & CSTOPB)
1052 lcr_h |= UART01x_LCRH_STP2;
1053 if (termios->c_cflag & PARENB) {
1054 lcr_h |= UART01x_LCRH_PEN;
1055 if (!(termios->c_cflag & PARODD))
1056 lcr_h |= UART01x_LCRH_EPS;
1057 }
1058 if (uap->fifosize > 1)
1059 lcr_h |= UART01x_LCRH_FEN;
1060
1061 spin_lock_irqsave(&port->lock, flags);
1062
1063 /*
1064 * Update the per-port timeout.
1065 */
1066 uart_update_timeout(port, termios->c_cflag, baud);
1067
1068 port->read_status_mask = UART011_DR_OE | 255;
1069 if (termios->c_iflag & INPCK)
1070 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1071 if (termios->c_iflag & (BRKINT | PARMRK))
1072 port->read_status_mask |= UART011_DR_BE;
1073
1074 /*
1075 * Characters to ignore
1076 */
1077 port->ignore_status_mask = 0;
1078 if (termios->c_iflag & IGNPAR)
1079 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1080 if (termios->c_iflag & IGNBRK) {
1081 port->ignore_status_mask |= UART011_DR_BE;
1082 /*
1083 * If we're ignoring parity and break indicators,
1084 * ignore overruns too (for real raw support).
1085 */
1086 if (termios->c_iflag & IGNPAR)
1087 port->ignore_status_mask |= UART011_DR_OE;
1088 }
1089
1090 /*
1091 * Ignore all characters if CREAD is not set.
1092 */
1093 if ((termios->c_cflag & CREAD) == 0)
1094 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1095
1096 if (UART_ENABLE_MS(port, termios->c_cflag))
1097 pl011_enable_ms(port);
1098
1099 /* first, disable everything */
1100 old_cr = readw(port->membase + UART011_CR);
1101 writew(0, port->membase + UART011_CR);
1102
1103 if (termios->c_cflag & CRTSCTS) {
1104 if (old_cr & UART011_CR_RTS)
1105 old_cr |= UART011_CR_RTSEN;
1106
1107 old_cr |= UART011_CR_CTSEN;
1108 uap->autorts = true;
1109 } else {
1110 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1111 uap->autorts = false;
1112 }
1113
1114 if (uap->vendor->oversampling) {
1115 if (baud > port->uartclk / 16)
1116 old_cr |= ST_UART011_CR_OVSFACT;
1117 else
1118 old_cr &= ~ST_UART011_CR_OVSFACT;
1119 }
1120
1121 /* Set baud rate */
1122 writew(quot & 0x3f, port->membase + UART011_FBRD);
1123 writew(quot >> 6, port->membase + UART011_IBRD);
1124
1125 /*
1126 * ----------v----------v----------v----------v-----
1127 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1128 * ----------^----------^----------^----------^-----
1129 */
1130 writew(lcr_h, port->membase + uap->lcrh_rx);
1131 if (uap->lcrh_rx != uap->lcrh_tx) {
1132 int i;
1133 /*
1134 * Wait 10 PCLKs before writing LCRH_TX register,
1135 * to get this delay write read only register 10 times
1136 */
1137 for (i = 0; i < 10; ++i)
1138 writew(0xff, uap->port.membase + UART011_MIS);
1139 writew(lcr_h, port->membase + uap->lcrh_tx);
1140 }
1141 writew(old_cr, port->membase + UART011_CR);
1142
1143 spin_unlock_irqrestore(&port->lock, flags);
1144}
1145
1146static const char *pl011_type(struct uart_port *port)
1147{
1148 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1149 return uap->port.type == PORT_AMBA ? uap->type : NULL;
1150}
1151
1152/*
1153 * Release the memory region(s) being used by 'port'
1154 */
1155static void pl010_release_port(struct uart_port *port)
1156{
1157 release_mem_region(port->mapbase, SZ_4K);
1158}
1159
1160/*
1161 * Request the memory region(s) being used by 'port'
1162 */
1163static int pl010_request_port(struct uart_port *port)
1164{
1165 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1166 != NULL ? 0 : -EBUSY;
1167}
1168
1169/*
1170 * Configure/autoconfigure the port.
1171 */
1172static void pl010_config_port(struct uart_port *port, int flags)
1173{
1174 if (flags & UART_CONFIG_TYPE) {
1175 port->type = PORT_AMBA;
1176 pl010_request_port(port);
1177 }
1178}
1179
1180/*
1181 * verify the new serial_struct (for TIOCSSERIAL).
1182 */
1183static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
1184{
1185 int ret = 0;
1186 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1187 ret = -EINVAL;
1188 if (ser->irq < 0 || ser->irq >= nr_irqs)
1189 ret = -EINVAL;
1190 if (ser->baud_base < 9600)
1191 ret = -EINVAL;
1192 return ret;
1193}
1194
1195static struct uart_ops amba_pl011_pops = {
1196 .tx_empty = pl01x_tx_empty,
1197 .set_mctrl = pl011_set_mctrl,
1198 .get_mctrl = pl01x_get_mctrl,
1199 .stop_tx = pl011_stop_tx,
1200 .start_tx = pl011_start_tx,
1201 .stop_rx = pl011_stop_rx,
1202 .enable_ms = pl011_enable_ms,
1203 .break_ctl = pl011_break_ctl,
1204 .startup = pl011_startup,
1205 .shutdown = pl011_shutdown,
1206 .flush_buffer = pl011_dma_flush_buffer,
1207 .set_termios = pl011_set_termios,
1208 .type = pl011_type,
1209 .release_port = pl010_release_port,
1210 .request_port = pl010_request_port,
1211 .config_port = pl010_config_port,
1212 .verify_port = pl010_verify_port,
1213#ifdef CONFIG_CONSOLE_POLL
1214 .poll_get_char = pl010_get_poll_char,
1215 .poll_put_char = pl010_put_poll_char,
1216#endif
1217};
1218
1219static struct uart_amba_port *amba_ports[UART_NR];
1220
1221#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1222
1223static void pl011_console_putchar(struct uart_port *port, int ch)
1224{
1225 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1226
1227 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1228 barrier();
1229 writew(ch, uap->port.membase + UART01x_DR);
1230}
1231
1232static void
1233pl011_console_write(struct console *co, const char *s, unsigned int count)
1234{
1235 struct uart_amba_port *uap = amba_ports[co->index];
1236 unsigned int status, old_cr, new_cr;
1237
1238 clk_enable(uap->clk);
1239
1240 /*
1241 * First save the CR then disable the interrupts
1242 */
1243 old_cr = readw(uap->port.membase + UART011_CR);
1244 new_cr = old_cr & ~UART011_CR_CTSEN;
1245 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1246 writew(new_cr, uap->port.membase + UART011_CR);
1247
1248 uart_console_write(&uap->port, s, count, pl011_console_putchar);
1249
1250 /*
1251 * Finally, wait for transmitter to become empty
1252 * and restore the TCR
1253 */
1254 do {
1255 status = readw(uap->port.membase + UART01x_FR);
1256 } while (status & UART01x_FR_BUSY);
1257 writew(old_cr, uap->port.membase + UART011_CR);
1258
1259 clk_disable(uap->clk);
1260}
1261
1262static void __init
1263pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1264 int *parity, int *bits)
1265{
1266 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1267 unsigned int lcr_h, ibrd, fbrd;
1268
1269 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
1270
1271 *parity = 'n';
1272 if (lcr_h & UART01x_LCRH_PEN) {
1273 if (lcr_h & UART01x_LCRH_EPS)
1274 *parity = 'e';
1275 else
1276 *parity = 'o';
1277 }
1278
1279 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1280 *bits = 7;
1281 else
1282 *bits = 8;
1283
1284 ibrd = readw(uap->port.membase + UART011_IBRD);
1285 fbrd = readw(uap->port.membase + UART011_FBRD);
1286
1287 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
1288
1289 if (uap->vendor->oversampling) {
1290 if (readw(uap->port.membase + UART011_CR)
1291 & ST_UART011_CR_OVSFACT)
1292 *baud *= 2;
1293 }
1294 }
1295}
1296
1297static int __init pl011_console_setup(struct console *co, char *options)
1298{
1299 struct uart_amba_port *uap;
1300 int baud = 38400;
1301 int bits = 8;
1302 int parity = 'n';
1303 int flow = 'n';
1304
1305 /*
1306 * Check whether an invalid uart number has been specified, and
1307 * if so, search for the first available port that does have
1308 * console support.
1309 */
1310 if (co->index >= UART_NR)
1311 co->index = 0;
1312 uap = amba_ports[co->index];
1313 if (!uap)
1314 return -ENODEV;
1315
1316 uap->port.uartclk = clk_get_rate(uap->clk);
1317
1318 if (options)
1319 uart_parse_options(options, &baud, &parity, &bits, &flow);
1320 else
1321 pl011_console_get_options(uap, &baud, &parity, &bits);
1322
1323 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1324}
1325
1326static struct uart_driver amba_reg;
1327static struct console amba_console = {
1328 .name = "ttyAMA",
1329 .write = pl011_console_write,
1330 .device = uart_console_device,
1331 .setup = pl011_console_setup,
1332 .flags = CON_PRINTBUFFER,
1333 .index = -1,
1334 .data = &amba_reg,
1335};
1336
1337#define AMBA_CONSOLE (&amba_console)
1338#else
1339#define AMBA_CONSOLE NULL
1340#endif
1341
1342static struct uart_driver amba_reg = {
1343 .owner = THIS_MODULE,
1344 .driver_name = "ttyAMA",
1345 .dev_name = "ttyAMA",
1346 .major = SERIAL_AMBA_MAJOR,
1347 .minor = SERIAL_AMBA_MINOR,
1348 .nr = UART_NR,
1349 .cons = AMBA_CONSOLE,
1350};
1351
1352static int pl011_probe(struct amba_device *dev, struct amba_id *id)
1353{
1354 struct uart_amba_port *uap;
1355 struct vendor_data *vendor = id->data;
1356 void __iomem *base;
1357 int i, ret;
1358
1359 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1360 if (amba_ports[i] == NULL)
1361 break;
1362
1363 if (i == ARRAY_SIZE(amba_ports)) {
1364 ret = -EBUSY;
1365 goto out;
1366 }
1367
1368 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
1369 if (uap == NULL) {
1370 ret = -ENOMEM;
1371 goto out;
1372 }
1373
1374 base = ioremap(dev->res.start, resource_size(&dev->res));
1375 if (!base) {
1376 ret = -ENOMEM;
1377 goto free;
1378 }
1379
1380 uap->clk = clk_get(&dev->dev, NULL);
1381 if (IS_ERR(uap->clk)) {
1382 ret = PTR_ERR(uap->clk);
1383 goto unmap;
1384 }
1385
1386 uap->vendor = vendor;
1387 uap->lcrh_rx = vendor->lcrh_rx;
1388 uap->lcrh_tx = vendor->lcrh_tx;
1389 uap->fifosize = vendor->fifosize;
1390 uap->port.dev = &dev->dev;
1391 uap->port.mapbase = dev->res.start;
1392 uap->port.membase = base;
1393 uap->port.iotype = UPIO_MEM;
1394 uap->port.irq = dev->irq[0];
1395 uap->port.fifosize = uap->fifosize;
1396 uap->port.ops = &amba_pl011_pops;
1397 uap->port.flags = UPF_BOOT_AUTOCONF;
1398 uap->port.line = i;
1399 pl011_dma_probe(uap);
1400
1401 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
1402
1403 amba_ports[i] = uap;
1404
1405 amba_set_drvdata(dev, uap);
1406 ret = uart_add_one_port(&amba_reg, &uap->port);
1407 if (ret) {
1408 amba_set_drvdata(dev, NULL);
1409 amba_ports[i] = NULL;
1410 pl011_dma_remove(uap);
1411 clk_put(uap->clk);
1412 unmap:
1413 iounmap(base);
1414 free:
1415 kfree(uap);
1416 }
1417 out:
1418 return ret;
1419}
1420
1421static int pl011_remove(struct amba_device *dev)
1422{
1423 struct uart_amba_port *uap = amba_get_drvdata(dev);
1424 int i;
1425
1426 amba_set_drvdata(dev, NULL);
1427
1428 uart_remove_one_port(&amba_reg, &uap->port);
1429
1430 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1431 if (amba_ports[i] == uap)
1432 amba_ports[i] = NULL;
1433
1434 pl011_dma_remove(uap);
1435 iounmap(uap->port.membase);
1436 clk_put(uap->clk);
1437 kfree(uap);
1438 return 0;
1439}
1440
1441#ifdef CONFIG_PM
1442static int pl011_suspend(struct amba_device *dev, pm_message_t state)
1443{
1444 struct uart_amba_port *uap = amba_get_drvdata(dev);
1445
1446 if (!uap)
1447 return -EINVAL;
1448
1449 return uart_suspend_port(&amba_reg, &uap->port);
1450}
1451
1452static int pl011_resume(struct amba_device *dev)
1453{
1454 struct uart_amba_port *uap = amba_get_drvdata(dev);
1455
1456 if (!uap)
1457 return -EINVAL;
1458
1459 return uart_resume_port(&amba_reg, &uap->port);
1460}
1461#endif
1462
1463static struct amba_id pl011_ids[] = {
1464 {
1465 .id = 0x00041011,
1466 .mask = 0x000fffff,
1467 .data = &vendor_arm,
1468 },
1469 {
1470 .id = 0x00380802,
1471 .mask = 0x00ffffff,
1472 .data = &vendor_st,
1473 },
1474 { 0, 0 },
1475};
1476
1477static struct amba_driver pl011_driver = {
1478 .drv = {
1479 .name = "uart-pl011",
1480 },
1481 .id_table = pl011_ids,
1482 .probe = pl011_probe,
1483 .remove = pl011_remove,
1484#ifdef CONFIG_PM
1485 .suspend = pl011_suspend,
1486 .resume = pl011_resume,
1487#endif
1488};
1489
1490static int __init pl011_init(void)
1491{
1492 int ret;
1493 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
1494
1495 ret = uart_register_driver(&amba_reg);
1496 if (ret == 0) {
1497 ret = amba_driver_register(&pl011_driver);
1498 if (ret)
1499 uart_unregister_driver(&amba_reg);
1500 }
1501 return ret;
1502}
1503
1504static void __exit pl011_exit(void)
1505{
1506 amba_driver_unregister(&pl011_driver);
1507 uart_unregister_driver(&amba_reg);
1508}
1509
1510/*
1511 * While this can be a module, if builtin it's most likely the console
1512 * So let's leave module_exit but move module_init to an earlier place
1513 */
1514arch_initcall(pl011_init);
1515module_exit(pl011_exit);
1516
1517MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
1518MODULE_DESCRIPTION("ARM AMBA serial port driver");
1519MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/apbuart.c b/drivers/tty/serial/apbuart.c
new file mode 100644
index 000000000000..095a5d562618
--- /dev/null
+++ b/drivers/tty/serial/apbuart.c
@@ -0,0 +1,709 @@
1/*
2 * Driver for GRLIB serial ports (APBUART)
3 *
4 * Based on linux/drivers/serial/amba.c
5 *
6 * Copyright (C) 2000 Deep Blue Solutions Ltd.
7 * Copyright (C) 2003 Konrad Eisele <eiselekd@web.de>
8 * Copyright (C) 2006 Daniel Hellstrom <daniel@gaisler.com>, Aeroflex Gaisler AB
9 * Copyright (C) 2008 Gilead Kutnick <kutnickg@zin-tech.com>
10 * Copyright (C) 2009 Kristoffer Glembo <kristoffer@gaisler.com>, Aeroflex Gaisler AB
11 */
12
13#if defined(CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
14#define SUPPORT_SYSRQ
15#endif
16
17#include <linux/module.h>
18#include <linux/tty.h>
19#include <linux/ioport.h>
20#include <linux/init.h>
21#include <linux/serial.h>
22#include <linux/console.h>
23#include <linux/sysrq.h>
24#include <linux/kthread.h>
25#include <linux/device.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/of_platform.h>
29#include <linux/of_irq.h>
30#include <linux/platform_device.h>
31#include <linux/io.h>
32#include <linux/serial_core.h>
33#include <asm/irq.h>
34
35#include "apbuart.h"
36
37#define SERIAL_APBUART_MAJOR TTY_MAJOR
38#define SERIAL_APBUART_MINOR 64
39#define UART_DUMMY_RSR_RX 0x8000 /* for ignore all read */
40
41static void apbuart_tx_chars(struct uart_port *port);
42
43static void apbuart_stop_tx(struct uart_port *port)
44{
45 unsigned int cr;
46
47 cr = UART_GET_CTRL(port);
48 cr &= ~UART_CTRL_TI;
49 UART_PUT_CTRL(port, cr);
50}
51
52static void apbuart_start_tx(struct uart_port *port)
53{
54 unsigned int cr;
55
56 cr = UART_GET_CTRL(port);
57 cr |= UART_CTRL_TI;
58 UART_PUT_CTRL(port, cr);
59
60 if (UART_GET_STATUS(port) & UART_STATUS_THE)
61 apbuart_tx_chars(port);
62}
63
64static void apbuart_stop_rx(struct uart_port *port)
65{
66 unsigned int cr;
67
68 cr = UART_GET_CTRL(port);
69 cr &= ~(UART_CTRL_RI);
70 UART_PUT_CTRL(port, cr);
71}
72
73static void apbuart_enable_ms(struct uart_port *port)
74{
75 /* No modem status change interrupts for APBUART */
76}
77
78static void apbuart_rx_chars(struct uart_port *port)
79{
80 struct tty_struct *tty = port->state->port.tty;
81 unsigned int status, ch, rsr, flag;
82 unsigned int max_chars = port->fifosize;
83
84 status = UART_GET_STATUS(port);
85
86 while (UART_RX_DATA(status) && (max_chars--)) {
87
88 ch = UART_GET_CHAR(port);
89 flag = TTY_NORMAL;
90
91 port->icount.rx++;
92
93 rsr = UART_GET_STATUS(port) | UART_DUMMY_RSR_RX;
94 UART_PUT_STATUS(port, 0);
95 if (rsr & UART_STATUS_ERR) {
96
97 if (rsr & UART_STATUS_BR) {
98 rsr &= ~(UART_STATUS_FE | UART_STATUS_PE);
99 port->icount.brk++;
100 if (uart_handle_break(port))
101 goto ignore_char;
102 } else if (rsr & UART_STATUS_PE) {
103 port->icount.parity++;
104 } else if (rsr & UART_STATUS_FE) {
105 port->icount.frame++;
106 }
107 if (rsr & UART_STATUS_OE)
108 port->icount.overrun++;
109
110 rsr &= port->read_status_mask;
111
112 if (rsr & UART_STATUS_PE)
113 flag = TTY_PARITY;
114 else if (rsr & UART_STATUS_FE)
115 flag = TTY_FRAME;
116 }
117
118 if (uart_handle_sysrq_char(port, ch))
119 goto ignore_char;
120
121 uart_insert_char(port, rsr, UART_STATUS_OE, ch, flag);
122
123
124 ignore_char:
125 status = UART_GET_STATUS(port);
126 }
127
128 tty_flip_buffer_push(tty);
129}
130
131static void apbuart_tx_chars(struct uart_port *port)
132{
133 struct circ_buf *xmit = &port->state->xmit;
134 int count;
135
136 if (port->x_char) {
137 UART_PUT_CHAR(port, port->x_char);
138 port->icount.tx++;
139 port->x_char = 0;
140 return;
141 }
142
143 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
144 apbuart_stop_tx(port);
145 return;
146 }
147
148 /* amba: fill FIFO */
149 count = port->fifosize >> 1;
150 do {
151 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
152 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
153 port->icount.tx++;
154 if (uart_circ_empty(xmit))
155 break;
156 } while (--count > 0);
157
158 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
159 uart_write_wakeup(port);
160
161 if (uart_circ_empty(xmit))
162 apbuart_stop_tx(port);
163}
164
165static irqreturn_t apbuart_int(int irq, void *dev_id)
166{
167 struct uart_port *port = dev_id;
168 unsigned int status;
169
170 spin_lock(&port->lock);
171
172 status = UART_GET_STATUS(port);
173 if (status & UART_STATUS_DR)
174 apbuart_rx_chars(port);
175 if (status & UART_STATUS_THE)
176 apbuart_tx_chars(port);
177
178 spin_unlock(&port->lock);
179
180 return IRQ_HANDLED;
181}
182
183static unsigned int apbuart_tx_empty(struct uart_port *port)
184{
185 unsigned int status = UART_GET_STATUS(port);
186 return status & UART_STATUS_THE ? TIOCSER_TEMT : 0;
187}
188
189static unsigned int apbuart_get_mctrl(struct uart_port *port)
190{
191 /* The GRLIB APBUART handles flow control in hardware */
192 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
193}
194
195static void apbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
196{
197 /* The GRLIB APBUART handles flow control in hardware */
198}
199
200static void apbuart_break_ctl(struct uart_port *port, int break_state)
201{
202 /* We don't support sending break */
203}
204
205static int apbuart_startup(struct uart_port *port)
206{
207 int retval;
208 unsigned int cr;
209
210 /* Allocate the IRQ */
211 retval = request_irq(port->irq, apbuart_int, 0, "apbuart", port);
212 if (retval)
213 return retval;
214
215 /* Finally, enable interrupts */
216 cr = UART_GET_CTRL(port);
217 UART_PUT_CTRL(port,
218 cr | UART_CTRL_RE | UART_CTRL_TE |
219 UART_CTRL_RI | UART_CTRL_TI);
220
221 return 0;
222}
223
224static void apbuart_shutdown(struct uart_port *port)
225{
226 unsigned int cr;
227
228 /* disable all interrupts, disable the port */
229 cr = UART_GET_CTRL(port);
230 UART_PUT_CTRL(port,
231 cr & ~(UART_CTRL_RE | UART_CTRL_TE |
232 UART_CTRL_RI | UART_CTRL_TI));
233
234 /* Free the interrupt */
235 free_irq(port->irq, port);
236}
237
238static void apbuart_set_termios(struct uart_port *port,
239 struct ktermios *termios, struct ktermios *old)
240{
241 unsigned int cr;
242 unsigned long flags;
243 unsigned int baud, quot;
244
245 /* Ask the core to calculate the divisor for us. */
246 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
247 if (baud == 0)
248 panic("invalid baudrate %i\n", port->uartclk / 16);
249
250 /* uart_get_divisor calc a *16 uart freq, apbuart is *8 */
251 quot = (uart_get_divisor(port, baud)) * 2;
252 cr = UART_GET_CTRL(port);
253 cr &= ~(UART_CTRL_PE | UART_CTRL_PS);
254
255 if (termios->c_cflag & PARENB) {
256 cr |= UART_CTRL_PE;
257 if ((termios->c_cflag & PARODD))
258 cr |= UART_CTRL_PS;
259 }
260
261 /* Enable flow control. */
262 if (termios->c_cflag & CRTSCTS)
263 cr |= UART_CTRL_FL;
264
265 spin_lock_irqsave(&port->lock, flags);
266
267 /* Update the per-port timeout. */
268 uart_update_timeout(port, termios->c_cflag, baud);
269
270 port->read_status_mask = UART_STATUS_OE;
271 if (termios->c_iflag & INPCK)
272 port->read_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
273
274 /* Characters to ignore */
275 port->ignore_status_mask = 0;
276 if (termios->c_iflag & IGNPAR)
277 port->ignore_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
278
279 /* Ignore all characters if CREAD is not set. */
280 if ((termios->c_cflag & CREAD) == 0)
281 port->ignore_status_mask |= UART_DUMMY_RSR_RX;
282
283 /* Set baud rate */
284 quot -= 1;
285 UART_PUT_SCAL(port, quot);
286 UART_PUT_CTRL(port, cr);
287
288 spin_unlock_irqrestore(&port->lock, flags);
289}
290
291static const char *apbuart_type(struct uart_port *port)
292{
293 return port->type == PORT_APBUART ? "GRLIB/APBUART" : NULL;
294}
295
296static void apbuart_release_port(struct uart_port *port)
297{
298 release_mem_region(port->mapbase, 0x100);
299}
300
301static int apbuart_request_port(struct uart_port *port)
302{
303 return request_mem_region(port->mapbase, 0x100, "grlib-apbuart")
304 != NULL ? 0 : -EBUSY;
305 return 0;
306}
307
308/* Configure/autoconfigure the port */
309static void apbuart_config_port(struct uart_port *port, int flags)
310{
311 if (flags & UART_CONFIG_TYPE) {
312 port->type = PORT_APBUART;
313 apbuart_request_port(port);
314 }
315}
316
317/* Verify the new serial_struct (for TIOCSSERIAL) */
318static int apbuart_verify_port(struct uart_port *port,
319 struct serial_struct *ser)
320{
321 int ret = 0;
322 if (ser->type != PORT_UNKNOWN && ser->type != PORT_APBUART)
323 ret = -EINVAL;
324 if (ser->irq < 0 || ser->irq >= NR_IRQS)
325 ret = -EINVAL;
326 if (ser->baud_base < 9600)
327 ret = -EINVAL;
328 return ret;
329}
330
331static struct uart_ops grlib_apbuart_ops = {
332 .tx_empty = apbuart_tx_empty,
333 .set_mctrl = apbuart_set_mctrl,
334 .get_mctrl = apbuart_get_mctrl,
335 .stop_tx = apbuart_stop_tx,
336 .start_tx = apbuart_start_tx,
337 .stop_rx = apbuart_stop_rx,
338 .enable_ms = apbuart_enable_ms,
339 .break_ctl = apbuart_break_ctl,
340 .startup = apbuart_startup,
341 .shutdown = apbuart_shutdown,
342 .set_termios = apbuart_set_termios,
343 .type = apbuart_type,
344 .release_port = apbuart_release_port,
345 .request_port = apbuart_request_port,
346 .config_port = apbuart_config_port,
347 .verify_port = apbuart_verify_port,
348};
349
350static struct uart_port grlib_apbuart_ports[UART_NR];
351static struct device_node *grlib_apbuart_nodes[UART_NR];
352
353static int apbuart_scan_fifo_size(struct uart_port *port, int portnumber)
354{
355 int ctrl, loop = 0;
356 int status;
357 int fifosize;
358 unsigned long flags;
359
360 ctrl = UART_GET_CTRL(port);
361
362 /*
363 * Enable the transceiver and wait for it to be ready to send data.
364 * Clear interrupts so that this process will not be externally
365 * interrupted in the middle (which can cause the transceiver to
366 * drain prematurely).
367 */
368
369 local_irq_save(flags);
370
371 UART_PUT_CTRL(port, ctrl | UART_CTRL_TE);
372
373 while (!UART_TX_READY(UART_GET_STATUS(port)))
374 loop++;
375
376 /*
377 * Disable the transceiver so data isn't actually sent during the
378 * actual test.
379 */
380
381 UART_PUT_CTRL(port, ctrl & ~(UART_CTRL_TE));
382
383 fifosize = 1;
384 UART_PUT_CHAR(port, 0);
385
386 /*
387 * So long as transmitting a character increments the tranceivier FIFO
388 * length the FIFO must be at least that big. These bytes will
389 * automatically drain off of the FIFO.
390 */
391
392 status = UART_GET_STATUS(port);
393 while (((status >> 20) & 0x3F) == fifosize) {
394 fifosize++;
395 UART_PUT_CHAR(port, 0);
396 status = UART_GET_STATUS(port);
397 }
398
399 fifosize--;
400
401 UART_PUT_CTRL(port, ctrl);
402 local_irq_restore(flags);
403
404 if (fifosize == 0)
405 fifosize = 1;
406
407 return fifosize;
408}
409
410static void apbuart_flush_fifo(struct uart_port *port)
411{
412 int i;
413
414 for (i = 0; i < port->fifosize; i++)
415 UART_GET_CHAR(port);
416}
417
418
419/* ======================================================================== */
420/* Console driver, if enabled */
421/* ======================================================================== */
422
423#ifdef CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
424
425static void apbuart_console_putchar(struct uart_port *port, int ch)
426{
427 unsigned int status;
428 do {
429 status = UART_GET_STATUS(port);
430 } while (!UART_TX_READY(status));
431 UART_PUT_CHAR(port, ch);
432}
433
434static void
435apbuart_console_write(struct console *co, const char *s, unsigned int count)
436{
437 struct uart_port *port = &grlib_apbuart_ports[co->index];
438 unsigned int status, old_cr, new_cr;
439
440 /* First save the CR then disable the interrupts */
441 old_cr = UART_GET_CTRL(port);
442 new_cr = old_cr & ~(UART_CTRL_RI | UART_CTRL_TI);
443 UART_PUT_CTRL(port, new_cr);
444
445 uart_console_write(port, s, count, apbuart_console_putchar);
446
447 /*
448 * Finally, wait for transmitter to become empty
449 * and restore the TCR
450 */
451 do {
452 status = UART_GET_STATUS(port);
453 } while (!UART_TX_READY(status));
454 UART_PUT_CTRL(port, old_cr);
455}
456
457static void __init
458apbuart_console_get_options(struct uart_port *port, int *baud,
459 int *parity, int *bits)
460{
461 if (UART_GET_CTRL(port) & (UART_CTRL_RE | UART_CTRL_TE)) {
462
463 unsigned int quot, status;
464 status = UART_GET_STATUS(port);
465
466 *parity = 'n';
467 if (status & UART_CTRL_PE) {
468 if ((status & UART_CTRL_PS) == 0)
469 *parity = 'e';
470 else
471 *parity = 'o';
472 }
473
474 *bits = 8;
475 quot = UART_GET_SCAL(port) / 8;
476 *baud = port->uartclk / (16 * (quot + 1));
477 }
478}
479
480static int __init apbuart_console_setup(struct console *co, char *options)
481{
482 struct uart_port *port;
483 int baud = 38400;
484 int bits = 8;
485 int parity = 'n';
486 int flow = 'n';
487
488 pr_debug("apbuart_console_setup co=%p, co->index=%i, options=%s\n",
489 co, co->index, options);
490
491 /*
492 * Check whether an invalid uart number has been specified, and
493 * if so, search for the first available port that does have
494 * console support.
495 */
496 if (co->index >= grlib_apbuart_port_nr)
497 co->index = 0;
498
499 port = &grlib_apbuart_ports[co->index];
500
501 spin_lock_init(&port->lock);
502
503 if (options)
504 uart_parse_options(options, &baud, &parity, &bits, &flow);
505 else
506 apbuart_console_get_options(port, &baud, &parity, &bits);
507
508 return uart_set_options(port, co, baud, parity, bits, flow);
509}
510
511static struct uart_driver grlib_apbuart_driver;
512
513static struct console grlib_apbuart_console = {
514 .name = "ttyS",
515 .write = apbuart_console_write,
516 .device = uart_console_device,
517 .setup = apbuart_console_setup,
518 .flags = CON_PRINTBUFFER,
519 .index = -1,
520 .data = &grlib_apbuart_driver,
521};
522
523
524static int grlib_apbuart_configure(void);
525
526static int __init apbuart_console_init(void)
527{
528 if (grlib_apbuart_configure())
529 return -ENODEV;
530 register_console(&grlib_apbuart_console);
531 return 0;
532}
533
534console_initcall(apbuart_console_init);
535
536#define APBUART_CONSOLE (&grlib_apbuart_console)
537#else
538#define APBUART_CONSOLE NULL
539#endif
540
541static struct uart_driver grlib_apbuart_driver = {
542 .owner = THIS_MODULE,
543 .driver_name = "serial",
544 .dev_name = "ttyS",
545 .major = SERIAL_APBUART_MAJOR,
546 .minor = SERIAL_APBUART_MINOR,
547 .nr = UART_NR,
548 .cons = APBUART_CONSOLE,
549};
550
551
552/* ======================================================================== */
553/* OF Platform Driver */
554/* ======================================================================== */
555
556static int __devinit apbuart_probe(struct platform_device *op,
557 const struct of_device_id *match)
558{
559 int i = -1;
560 struct uart_port *port = NULL;
561
562 i = 0;
563 for (i = 0; i < grlib_apbuart_port_nr; i++) {
564 if (op->dev.of_node == grlib_apbuart_nodes[i])
565 break;
566 }
567
568 port = &grlib_apbuart_ports[i];
569 port->dev = &op->dev;
570
571 uart_add_one_port(&grlib_apbuart_driver, (struct uart_port *) port);
572
573 apbuart_flush_fifo((struct uart_port *) port);
574
575 printk(KERN_INFO "grlib-apbuart at 0x%llx, irq %d\n",
576 (unsigned long long) port->mapbase, port->irq);
577 return 0;
578}
579
580static struct of_device_id __initdata apbuart_match[] = {
581 {
582 .name = "GAISLER_APBUART",
583 },
584 {
585 .name = "01_00c",
586 },
587 {},
588};
589
590static struct of_platform_driver grlib_apbuart_of_driver = {
591 .probe = apbuart_probe,
592 .driver = {
593 .owner = THIS_MODULE,
594 .name = "grlib-apbuart",
595 .of_match_table = apbuart_match,
596 },
597};
598
599
600static int grlib_apbuart_configure(void)
601{
602 struct device_node *np, *rp;
603 const u32 *prop;
604 int freq_khz, line = 0;
605
606 /* Get bus frequency */
607 rp = of_find_node_by_path("/");
608 if (!rp)
609 return -ENODEV;
610 rp = of_get_next_child(rp, NULL);
611 if (!rp)
612 return -ENODEV;
613 prop = of_get_property(rp, "clock-frequency", NULL);
614 if (!prop)
615 return -ENODEV;
616 freq_khz = *prop;
617
618 for_each_matching_node(np, apbuart_match) {
619 const int *irqs, *ampopts;
620 const struct amba_prom_registers *regs;
621 struct uart_port *port;
622 unsigned long addr;
623
624 ampopts = of_get_property(np, "ampopts", NULL);
625 if (ampopts && (*ampopts == 0))
626 continue; /* Ignore if used by another OS instance */
627
628 irqs = of_get_property(np, "interrupts", NULL);
629 regs = of_get_property(np, "reg", NULL);
630
631 if (!irqs || !regs)
632 continue;
633
634 grlib_apbuart_nodes[line] = np;
635
636 addr = regs->phys_addr;
637
638 port = &grlib_apbuart_ports[line];
639
640 port->mapbase = addr;
641 port->membase = ioremap(addr, sizeof(struct grlib_apbuart_regs_map));
642 port->irq = *irqs;
643 port->iotype = UPIO_MEM;
644 port->ops = &grlib_apbuart_ops;
645 port->flags = UPF_BOOT_AUTOCONF;
646 port->line = line;
647 port->uartclk = freq_khz * 1000;
648 port->fifosize = apbuart_scan_fifo_size((struct uart_port *) port, line);
649 line++;
650
651 /* We support maximum UART_NR uarts ... */
652 if (line == UART_NR)
653 break;
654 }
655
656 grlib_apbuart_driver.nr = grlib_apbuart_port_nr = line;
657 return line ? 0 : -ENODEV;
658}
659
660static int __init grlib_apbuart_init(void)
661{
662 int ret;
663
664 /* Find all APBUARTS in device the tree and initialize their ports */
665 ret = grlib_apbuart_configure();
666 if (ret)
667 return ret;
668
669 printk(KERN_INFO "Serial: GRLIB APBUART driver\n");
670
671 ret = uart_register_driver(&grlib_apbuart_driver);
672
673 if (ret) {
674 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
675 __FILE__, ret);
676 return ret;
677 }
678
679 ret = of_register_platform_driver(&grlib_apbuart_of_driver);
680 if (ret) {
681 printk(KERN_ERR
682 "%s: of_register_platform_driver failed (%i)\n",
683 __FILE__, ret);
684 uart_unregister_driver(&grlib_apbuart_driver);
685 return ret;
686 }
687
688 return ret;
689}
690
691static void __exit grlib_apbuart_exit(void)
692{
693 int i;
694
695 for (i = 0; i < grlib_apbuart_port_nr; i++)
696 uart_remove_one_port(&grlib_apbuart_driver,
697 &grlib_apbuart_ports[i]);
698
699 uart_unregister_driver(&grlib_apbuart_driver);
700 of_unregister_platform_driver(&grlib_apbuart_of_driver);
701}
702
703module_init(grlib_apbuart_init);
704module_exit(grlib_apbuart_exit);
705
706MODULE_AUTHOR("Aeroflex Gaisler AB");
707MODULE_DESCRIPTION("GRLIB APBUART serial driver");
708MODULE_VERSION("2.1");
709MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/apbuart.h b/drivers/tty/serial/apbuart.h
new file mode 100644
index 000000000000..5faf87c8d2bc
--- /dev/null
+++ b/drivers/tty/serial/apbuart.h
@@ -0,0 +1,64 @@
1#ifndef __GRLIB_APBUART_H__
2#define __GRLIB_APBUART_H__
3
4#include <asm/io.h>
5
6#define UART_NR 8
7static int grlib_apbuart_port_nr;
8
9struct grlib_apbuart_regs_map {
10 u32 data;
11 u32 status;
12 u32 ctrl;
13 u32 scaler;
14};
15
16struct amba_prom_registers {
17 unsigned int phys_addr;
18 unsigned int reg_size;
19};
20
21/*
22 * The following defines the bits in the APBUART Status Registers.
23 */
24#define UART_STATUS_DR 0x00000001 /* Data Ready */
25#define UART_STATUS_TSE 0x00000002 /* TX Send Register Empty */
26#define UART_STATUS_THE 0x00000004 /* TX Hold Register Empty */
27#define UART_STATUS_BR 0x00000008 /* Break Error */
28#define UART_STATUS_OE 0x00000010 /* RX Overrun Error */
29#define UART_STATUS_PE 0x00000020 /* RX Parity Error */
30#define UART_STATUS_FE 0x00000040 /* RX Framing Error */
31#define UART_STATUS_ERR 0x00000078 /* Error Mask */
32
33/*
34 * The following defines the bits in the APBUART Ctrl Registers.
35 */
36#define UART_CTRL_RE 0x00000001 /* Receiver enable */
37#define UART_CTRL_TE 0x00000002 /* Transmitter enable */
38#define UART_CTRL_RI 0x00000004 /* Receiver interrupt enable */
39#define UART_CTRL_TI 0x00000008 /* Transmitter irq */
40#define UART_CTRL_PS 0x00000010 /* Parity select */
41#define UART_CTRL_PE 0x00000020 /* Parity enable */
42#define UART_CTRL_FL 0x00000040 /* Flow control enable */
43#define UART_CTRL_LB 0x00000080 /* Loopback enable */
44
45#define APBBASE(port) ((struct grlib_apbuart_regs_map *)((port)->membase))
46
47#define APBBASE_DATA_P(port) (&(APBBASE(port)->data))
48#define APBBASE_STATUS_P(port) (&(APBBASE(port)->status))
49#define APBBASE_CTRL_P(port) (&(APBBASE(port)->ctrl))
50#define APBBASE_SCALAR_P(port) (&(APBBASE(port)->scaler))
51
52#define UART_GET_CHAR(port) (__raw_readl(APBBASE_DATA_P(port)))
53#define UART_PUT_CHAR(port, v) (__raw_writel(v, APBBASE_DATA_P(port)))
54#define UART_GET_STATUS(port) (__raw_readl(APBBASE_STATUS_P(port)))
55#define UART_PUT_STATUS(port, v)(__raw_writel(v, APBBASE_STATUS_P(port)))
56#define UART_GET_CTRL(port) (__raw_readl(APBBASE_CTRL_P(port)))
57#define UART_PUT_CTRL(port, v) (__raw_writel(v, APBBASE_CTRL_P(port)))
58#define UART_GET_SCAL(port) (__raw_readl(APBBASE_SCALAR_P(port)))
59#define UART_PUT_SCAL(port, v) (__raw_writel(v, APBBASE_SCALAR_P(port)))
60
61#define UART_RX_DATA(s) (((s) & UART_STATUS_DR) != 0)
62#define UART_TX_READY(s) (((s) & UART_STATUS_THE) != 0)
63
64#endif /* __GRLIB_APBUART_H__ */
diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c
new file mode 100644
index 000000000000..3892666b5fbd
--- /dev/null
+++ b/drivers/tty/serial/atmel_serial.c
@@ -0,0 +1,1808 @@
1/*
2 * linux/drivers/char/atmel_serial.c
3 *
4 * Driver for Atmel AT91 / AT32 Serial ports
5 * Copyright (C) 2003 Rick Bronson
6 *
7 * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
8 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
9 *
10 * DMA support added by Chip Coldwell.
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#include <linux/module.h>
28#include <linux/tty.h>
29#include <linux/ioport.h>
30#include <linux/slab.h>
31#include <linux/init.h>
32#include <linux/serial.h>
33#include <linux/clk.h>
34#include <linux/console.h>
35#include <linux/sysrq.h>
36#include <linux/tty_flip.h>
37#include <linux/platform_device.h>
38#include <linux/dma-mapping.h>
39#include <linux/atmel_pdc.h>
40#include <linux/atmel_serial.h>
41#include <linux/uaccess.h>
42
43#include <asm/io.h>
44#include <asm/ioctls.h>
45
46#include <asm/mach/serial_at91.h>
47#include <mach/board.h>
48
49#ifdef CONFIG_ARM
50#include <mach/cpu.h>
51#include <mach/gpio.h>
52#endif
53
54#define PDC_BUFFER_SIZE 512
55/* Revisit: We should calculate this based on the actual port settings */
56#define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
57
58#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
59#define SUPPORT_SYSRQ
60#endif
61
62#include <linux/serial_core.h>
63
64static void atmel_start_rx(struct uart_port *port);
65static void atmel_stop_rx(struct uart_port *port);
66
67#ifdef CONFIG_SERIAL_ATMEL_TTYAT
68
69/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
70 * should coexist with the 8250 driver, such as if we have an external 16C550
71 * UART. */
72#define SERIAL_ATMEL_MAJOR 204
73#define MINOR_START 154
74#define ATMEL_DEVICENAME "ttyAT"
75
76#else
77
78/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
79 * name, but it is legally reserved for the 8250 driver. */
80#define SERIAL_ATMEL_MAJOR TTY_MAJOR
81#define MINOR_START 64
82#define ATMEL_DEVICENAME "ttyS"
83
84#endif
85
86#define ATMEL_ISR_PASS_LIMIT 256
87
88/* UART registers. CR is write-only, hence no GET macro */
89#define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
90#define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
91#define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
92#define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
93#define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
94#define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
95#define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
96#define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
97#define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
98#define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
99#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
100#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
101#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
102
103 /* PDC registers */
104#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
105#define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
106
107#define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
108#define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
109#define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
110#define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
111#define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
112
113#define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
114#define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
115#define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
116
117static int (*atmel_open_hook)(struct uart_port *);
118static void (*atmel_close_hook)(struct uart_port *);
119
120struct atmel_dma_buffer {
121 unsigned char *buf;
122 dma_addr_t dma_addr;
123 unsigned int dma_size;
124 unsigned int ofs;
125};
126
127struct atmel_uart_char {
128 u16 status;
129 u16 ch;
130};
131
132#define ATMEL_SERIAL_RINGSIZE 1024
133
134/*
135 * We wrap our port structure around the generic uart_port.
136 */
137struct atmel_uart_port {
138 struct uart_port uart; /* uart */
139 struct clk *clk; /* uart clock */
140 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
141 u32 backup_imr; /* IMR saved during suspend */
142 int break_active; /* break being received */
143
144 short use_dma_rx; /* enable PDC receiver */
145 short pdc_rx_idx; /* current PDC RX buffer */
146 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
147
148 short use_dma_tx; /* enable PDC transmitter */
149 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
150
151 struct tasklet_struct tasklet;
152 unsigned int irq_status;
153 unsigned int irq_status_prev;
154
155 struct circ_buf rx_ring;
156
157 struct serial_rs485 rs485; /* rs485 settings */
158 unsigned int tx_done_mask;
159};
160
161static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
162
163#ifdef SUPPORT_SYSRQ
164static struct console atmel_console;
165#endif
166
167static inline struct atmel_uart_port *
168to_atmel_uart_port(struct uart_port *uart)
169{
170 return container_of(uart, struct atmel_uart_port, uart);
171}
172
173#ifdef CONFIG_SERIAL_ATMEL_PDC
174static bool atmel_use_dma_rx(struct uart_port *port)
175{
176 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
177
178 return atmel_port->use_dma_rx;
179}
180
181static bool atmel_use_dma_tx(struct uart_port *port)
182{
183 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
184
185 return atmel_port->use_dma_tx;
186}
187#else
188static bool atmel_use_dma_rx(struct uart_port *port)
189{
190 return false;
191}
192
193static bool atmel_use_dma_tx(struct uart_port *port)
194{
195 return false;
196}
197#endif
198
199/* Enable or disable the rs485 support */
200void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
201{
202 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
203 unsigned int mode;
204
205 spin_lock(&port->lock);
206
207 /* Disable interrupts */
208 UART_PUT_IDR(port, atmel_port->tx_done_mask);
209
210 mode = UART_GET_MR(port);
211
212 /* Resetting serial mode to RS232 (0x0) */
213 mode &= ~ATMEL_US_USMODE;
214
215 atmel_port->rs485 = *rs485conf;
216
217 if (rs485conf->flags & SER_RS485_ENABLED) {
218 dev_dbg(port->dev, "Setting UART to RS485\n");
219 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
220 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
221 UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
222 mode |= ATMEL_US_USMODE_RS485;
223 } else {
224 dev_dbg(port->dev, "Setting UART to RS232\n");
225 if (atmel_use_dma_tx(port))
226 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
227 ATMEL_US_TXBUFE;
228 else
229 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
230 }
231 UART_PUT_MR(port, mode);
232
233 /* Enable interrupts */
234 UART_PUT_IER(port, atmel_port->tx_done_mask);
235
236 spin_unlock(&port->lock);
237
238}
239
240/*
241 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
242 */
243static u_int atmel_tx_empty(struct uart_port *port)
244{
245 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
246}
247
248/*
249 * Set state of the modem control output lines
250 */
251static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
252{
253 unsigned int control = 0;
254 unsigned int mode;
255 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
256
257#ifdef CONFIG_ARCH_AT91RM9200
258 if (cpu_is_at91rm9200()) {
259 /*
260 * AT91RM9200 Errata #39: RTS0 is not internally connected
261 * to PA21. We need to drive the pin manually.
262 */
263 if (port->mapbase == AT91RM9200_BASE_US0) {
264 if (mctrl & TIOCM_RTS)
265 at91_set_gpio_value(AT91_PIN_PA21, 0);
266 else
267 at91_set_gpio_value(AT91_PIN_PA21, 1);
268 }
269 }
270#endif
271
272 if (mctrl & TIOCM_RTS)
273 control |= ATMEL_US_RTSEN;
274 else
275 control |= ATMEL_US_RTSDIS;
276
277 if (mctrl & TIOCM_DTR)
278 control |= ATMEL_US_DTREN;
279 else
280 control |= ATMEL_US_DTRDIS;
281
282 UART_PUT_CR(port, control);
283
284 /* Local loopback mode? */
285 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
286 if (mctrl & TIOCM_LOOP)
287 mode |= ATMEL_US_CHMODE_LOC_LOOP;
288 else
289 mode |= ATMEL_US_CHMODE_NORMAL;
290
291 /* Resetting serial mode to RS232 (0x0) */
292 mode &= ~ATMEL_US_USMODE;
293
294 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
295 dev_dbg(port->dev, "Setting UART to RS485\n");
296 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
297 UART_PUT_TTGR(port,
298 atmel_port->rs485.delay_rts_after_send);
299 mode |= ATMEL_US_USMODE_RS485;
300 } else {
301 dev_dbg(port->dev, "Setting UART to RS232\n");
302 }
303 UART_PUT_MR(port, mode);
304}
305
306/*
307 * Get state of the modem control input lines
308 */
309static u_int atmel_get_mctrl(struct uart_port *port)
310{
311 unsigned int status, ret = 0;
312
313 status = UART_GET_CSR(port);
314
315 /*
316 * The control signals are active low.
317 */
318 if (!(status & ATMEL_US_DCD))
319 ret |= TIOCM_CD;
320 if (!(status & ATMEL_US_CTS))
321 ret |= TIOCM_CTS;
322 if (!(status & ATMEL_US_DSR))
323 ret |= TIOCM_DSR;
324 if (!(status & ATMEL_US_RI))
325 ret |= TIOCM_RI;
326
327 return ret;
328}
329
330/*
331 * Stop transmitting.
332 */
333static void atmel_stop_tx(struct uart_port *port)
334{
335 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
336
337 if (atmel_use_dma_tx(port)) {
338 /* disable PDC transmit */
339 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
340 }
341 /* Disable interrupts */
342 UART_PUT_IDR(port, atmel_port->tx_done_mask);
343
344 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
345 atmel_start_rx(port);
346}
347
348/*
349 * Start transmitting.
350 */
351static void atmel_start_tx(struct uart_port *port)
352{
353 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
354
355 if (atmel_use_dma_tx(port)) {
356 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
357 /* The transmitter is already running. Yes, we
358 really need this.*/
359 return;
360
361 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
362 atmel_stop_rx(port);
363
364 /* re-enable PDC transmit */
365 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
366 }
367 /* Enable interrupts */
368 UART_PUT_IER(port, atmel_port->tx_done_mask);
369}
370
371/*
372 * start receiving - port is in process of being opened.
373 */
374static void atmel_start_rx(struct uart_port *port)
375{
376 UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
377
378 if (atmel_use_dma_rx(port)) {
379 /* enable PDC controller */
380 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
381 port->read_status_mask);
382 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
383 } else {
384 UART_PUT_IER(port, ATMEL_US_RXRDY);
385 }
386}
387
388/*
389 * Stop receiving - port is in process of being closed.
390 */
391static void atmel_stop_rx(struct uart_port *port)
392{
393 if (atmel_use_dma_rx(port)) {
394 /* disable PDC receive */
395 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
396 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
397 port->read_status_mask);
398 } else {
399 UART_PUT_IDR(port, ATMEL_US_RXRDY);
400 }
401}
402
403/*
404 * Enable modem status interrupts
405 */
406static void atmel_enable_ms(struct uart_port *port)
407{
408 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
409 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
410}
411
412/*
413 * Control the transmission of a break signal
414 */
415static void atmel_break_ctl(struct uart_port *port, int break_state)
416{
417 if (break_state != 0)
418 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
419 else
420 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
421}
422
423/*
424 * Stores the incoming character in the ring buffer
425 */
426static void
427atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
428 unsigned int ch)
429{
430 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
431 struct circ_buf *ring = &atmel_port->rx_ring;
432 struct atmel_uart_char *c;
433
434 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
435 /* Buffer overflow, ignore char */
436 return;
437
438 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
439 c->status = status;
440 c->ch = ch;
441
442 /* Make sure the character is stored before we update head. */
443 smp_wmb();
444
445 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
446}
447
448/*
449 * Deal with parity, framing and overrun errors.
450 */
451static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
452{
453 /* clear error */
454 UART_PUT_CR(port, ATMEL_US_RSTSTA);
455
456 if (status & ATMEL_US_RXBRK) {
457 /* ignore side-effect */
458 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
459 port->icount.brk++;
460 }
461 if (status & ATMEL_US_PARE)
462 port->icount.parity++;
463 if (status & ATMEL_US_FRAME)
464 port->icount.frame++;
465 if (status & ATMEL_US_OVRE)
466 port->icount.overrun++;
467}
468
469/*
470 * Characters received (called from interrupt handler)
471 */
472static void atmel_rx_chars(struct uart_port *port)
473{
474 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
475 unsigned int status, ch;
476
477 status = UART_GET_CSR(port);
478 while (status & ATMEL_US_RXRDY) {
479 ch = UART_GET_CHAR(port);
480
481 /*
482 * note that the error handling code is
483 * out of the main execution path
484 */
485 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
486 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
487 || atmel_port->break_active)) {
488
489 /* clear error */
490 UART_PUT_CR(port, ATMEL_US_RSTSTA);
491
492 if (status & ATMEL_US_RXBRK
493 && !atmel_port->break_active) {
494 atmel_port->break_active = 1;
495 UART_PUT_IER(port, ATMEL_US_RXBRK);
496 } else {
497 /*
498 * This is either the end-of-break
499 * condition or we've received at
500 * least one character without RXBRK
501 * being set. In both cases, the next
502 * RXBRK will indicate start-of-break.
503 */
504 UART_PUT_IDR(port, ATMEL_US_RXBRK);
505 status &= ~ATMEL_US_RXBRK;
506 atmel_port->break_active = 0;
507 }
508 }
509
510 atmel_buffer_rx_char(port, status, ch);
511 status = UART_GET_CSR(port);
512 }
513
514 tasklet_schedule(&atmel_port->tasklet);
515}
516
517/*
518 * Transmit characters (called from tasklet with TXRDY interrupt
519 * disabled)
520 */
521static void atmel_tx_chars(struct uart_port *port)
522{
523 struct circ_buf *xmit = &port->state->xmit;
524 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
525
526 if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
527 UART_PUT_CHAR(port, port->x_char);
528 port->icount.tx++;
529 port->x_char = 0;
530 }
531 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
532 return;
533
534 while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
535 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
536 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
537 port->icount.tx++;
538 if (uart_circ_empty(xmit))
539 break;
540 }
541
542 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
543 uart_write_wakeup(port);
544
545 if (!uart_circ_empty(xmit))
546 /* Enable interrupts */
547 UART_PUT_IER(port, atmel_port->tx_done_mask);
548}
549
550/*
551 * receive interrupt handler.
552 */
553static void
554atmel_handle_receive(struct uart_port *port, unsigned int pending)
555{
556 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
557
558 if (atmel_use_dma_rx(port)) {
559 /*
560 * PDC receive. Just schedule the tasklet and let it
561 * figure out the details.
562 *
563 * TODO: We're not handling error flags correctly at
564 * the moment.
565 */
566 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
567 UART_PUT_IDR(port, (ATMEL_US_ENDRX
568 | ATMEL_US_TIMEOUT));
569 tasklet_schedule(&atmel_port->tasklet);
570 }
571
572 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
573 ATMEL_US_FRAME | ATMEL_US_PARE))
574 atmel_pdc_rxerr(port, pending);
575 }
576
577 /* Interrupt receive */
578 if (pending & ATMEL_US_RXRDY)
579 atmel_rx_chars(port);
580 else if (pending & ATMEL_US_RXBRK) {
581 /*
582 * End of break detected. If it came along with a
583 * character, atmel_rx_chars will handle it.
584 */
585 UART_PUT_CR(port, ATMEL_US_RSTSTA);
586 UART_PUT_IDR(port, ATMEL_US_RXBRK);
587 atmel_port->break_active = 0;
588 }
589}
590
591/*
592 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
593 */
594static void
595atmel_handle_transmit(struct uart_port *port, unsigned int pending)
596{
597 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
598
599 if (pending & atmel_port->tx_done_mask) {
600 /* Either PDC or interrupt transmission */
601 UART_PUT_IDR(port, atmel_port->tx_done_mask);
602 tasklet_schedule(&atmel_port->tasklet);
603 }
604}
605
606/*
607 * status flags interrupt handler.
608 */
609static void
610atmel_handle_status(struct uart_port *port, unsigned int pending,
611 unsigned int status)
612{
613 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
614
615 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
616 | ATMEL_US_CTSIC)) {
617 atmel_port->irq_status = status;
618 tasklet_schedule(&atmel_port->tasklet);
619 }
620}
621
622/*
623 * Interrupt handler
624 */
625static irqreturn_t atmel_interrupt(int irq, void *dev_id)
626{
627 struct uart_port *port = dev_id;
628 unsigned int status, pending, pass_counter = 0;
629
630 do {
631 status = UART_GET_CSR(port);
632 pending = status & UART_GET_IMR(port);
633 if (!pending)
634 break;
635
636 atmel_handle_receive(port, pending);
637 atmel_handle_status(port, pending, status);
638 atmel_handle_transmit(port, pending);
639 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
640
641 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
642}
643
644/*
645 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
646 */
647static void atmel_tx_dma(struct uart_port *port)
648{
649 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
650 struct circ_buf *xmit = &port->state->xmit;
651 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
652 int count;
653
654 /* nothing left to transmit? */
655 if (UART_GET_TCR(port))
656 return;
657
658 xmit->tail += pdc->ofs;
659 xmit->tail &= UART_XMIT_SIZE - 1;
660
661 port->icount.tx += pdc->ofs;
662 pdc->ofs = 0;
663
664 /* more to transmit - setup next transfer */
665
666 /* disable PDC transmit */
667 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
668
669 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
670 dma_sync_single_for_device(port->dev,
671 pdc->dma_addr,
672 pdc->dma_size,
673 DMA_TO_DEVICE);
674
675 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
676 pdc->ofs = count;
677
678 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
679 UART_PUT_TCR(port, count);
680 /* re-enable PDC transmit */
681 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
682 /* Enable interrupts */
683 UART_PUT_IER(port, atmel_port->tx_done_mask);
684 } else {
685 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
686 /* DMA done, stop TX, start RX for RS485 */
687 atmel_start_rx(port);
688 }
689 }
690
691 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
692 uart_write_wakeup(port);
693}
694
695static void atmel_rx_from_ring(struct uart_port *port)
696{
697 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
698 struct circ_buf *ring = &atmel_port->rx_ring;
699 unsigned int flg;
700 unsigned int status;
701
702 while (ring->head != ring->tail) {
703 struct atmel_uart_char c;
704
705 /* Make sure c is loaded after head. */
706 smp_rmb();
707
708 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
709
710 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
711
712 port->icount.rx++;
713 status = c.status;
714 flg = TTY_NORMAL;
715
716 /*
717 * note that the error handling code is
718 * out of the main execution path
719 */
720 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
721 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
722 if (status & ATMEL_US_RXBRK) {
723 /* ignore side-effect */
724 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
725
726 port->icount.brk++;
727 if (uart_handle_break(port))
728 continue;
729 }
730 if (status & ATMEL_US_PARE)
731 port->icount.parity++;
732 if (status & ATMEL_US_FRAME)
733 port->icount.frame++;
734 if (status & ATMEL_US_OVRE)
735 port->icount.overrun++;
736
737 status &= port->read_status_mask;
738
739 if (status & ATMEL_US_RXBRK)
740 flg = TTY_BREAK;
741 else if (status & ATMEL_US_PARE)
742 flg = TTY_PARITY;
743 else if (status & ATMEL_US_FRAME)
744 flg = TTY_FRAME;
745 }
746
747
748 if (uart_handle_sysrq_char(port, c.ch))
749 continue;
750
751 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
752 }
753
754 /*
755 * Drop the lock here since it might end up calling
756 * uart_start(), which takes the lock.
757 */
758 spin_unlock(&port->lock);
759 tty_flip_buffer_push(port->state->port.tty);
760 spin_lock(&port->lock);
761}
762
763static void atmel_rx_from_dma(struct uart_port *port)
764{
765 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
766 struct tty_struct *tty = port->state->port.tty;
767 struct atmel_dma_buffer *pdc;
768 int rx_idx = atmel_port->pdc_rx_idx;
769 unsigned int head;
770 unsigned int tail;
771 unsigned int count;
772
773 do {
774 /* Reset the UART timeout early so that we don't miss one */
775 UART_PUT_CR(port, ATMEL_US_STTTO);
776
777 pdc = &atmel_port->pdc_rx[rx_idx];
778 head = UART_GET_RPR(port) - pdc->dma_addr;
779 tail = pdc->ofs;
780
781 /* If the PDC has switched buffers, RPR won't contain
782 * any address within the current buffer. Since head
783 * is unsigned, we just need a one-way comparison to
784 * find out.
785 *
786 * In this case, we just need to consume the entire
787 * buffer and resubmit it for DMA. This will clear the
788 * ENDRX bit as well, so that we can safely re-enable
789 * all interrupts below.
790 */
791 head = min(head, pdc->dma_size);
792
793 if (likely(head != tail)) {
794 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
795 pdc->dma_size, DMA_FROM_DEVICE);
796
797 /*
798 * head will only wrap around when we recycle
799 * the DMA buffer, and when that happens, we
800 * explicitly set tail to 0. So head will
801 * always be greater than tail.
802 */
803 count = head - tail;
804
805 tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
806
807 dma_sync_single_for_device(port->dev, pdc->dma_addr,
808 pdc->dma_size, DMA_FROM_DEVICE);
809
810 port->icount.rx += count;
811 pdc->ofs = head;
812 }
813
814 /*
815 * If the current buffer is full, we need to check if
816 * the next one contains any additional data.
817 */
818 if (head >= pdc->dma_size) {
819 pdc->ofs = 0;
820 UART_PUT_RNPR(port, pdc->dma_addr);
821 UART_PUT_RNCR(port, pdc->dma_size);
822
823 rx_idx = !rx_idx;
824 atmel_port->pdc_rx_idx = rx_idx;
825 }
826 } while (head >= pdc->dma_size);
827
828 /*
829 * Drop the lock here since it might end up calling
830 * uart_start(), which takes the lock.
831 */
832 spin_unlock(&port->lock);
833 tty_flip_buffer_push(tty);
834 spin_lock(&port->lock);
835
836 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
837}
838
839/*
840 * tasklet handling tty stuff outside the interrupt handler.
841 */
842static void atmel_tasklet_func(unsigned long data)
843{
844 struct uart_port *port = (struct uart_port *)data;
845 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
846 unsigned int status;
847 unsigned int status_change;
848
849 /* The interrupt handler does not take the lock */
850 spin_lock(&port->lock);
851
852 if (atmel_use_dma_tx(port))
853 atmel_tx_dma(port);
854 else
855 atmel_tx_chars(port);
856
857 status = atmel_port->irq_status;
858 status_change = status ^ atmel_port->irq_status_prev;
859
860 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
861 | ATMEL_US_DCD | ATMEL_US_CTS)) {
862 /* TODO: All reads to CSR will clear these interrupts! */
863 if (status_change & ATMEL_US_RI)
864 port->icount.rng++;
865 if (status_change & ATMEL_US_DSR)
866 port->icount.dsr++;
867 if (status_change & ATMEL_US_DCD)
868 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
869 if (status_change & ATMEL_US_CTS)
870 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
871
872 wake_up_interruptible(&port->state->port.delta_msr_wait);
873
874 atmel_port->irq_status_prev = status;
875 }
876
877 if (atmel_use_dma_rx(port))
878 atmel_rx_from_dma(port);
879 else
880 atmel_rx_from_ring(port);
881
882 spin_unlock(&port->lock);
883}
884
885/*
886 * Perform initialization and enable port for reception
887 */
888static int atmel_startup(struct uart_port *port)
889{
890 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
891 struct tty_struct *tty = port->state->port.tty;
892 int retval;
893
894 /*
895 * Ensure that no interrupts are enabled otherwise when
896 * request_irq() is called we could get stuck trying to
897 * handle an unexpected interrupt
898 */
899 UART_PUT_IDR(port, -1);
900
901 /*
902 * Allocate the IRQ
903 */
904 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
905 tty ? tty->name : "atmel_serial", port);
906 if (retval) {
907 printk("atmel_serial: atmel_startup - Can't get irq\n");
908 return retval;
909 }
910
911 /*
912 * Initialize DMA (if necessary)
913 */
914 if (atmel_use_dma_rx(port)) {
915 int i;
916
917 for (i = 0; i < 2; i++) {
918 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
919
920 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
921 if (pdc->buf == NULL) {
922 if (i != 0) {
923 dma_unmap_single(port->dev,
924 atmel_port->pdc_rx[0].dma_addr,
925 PDC_BUFFER_SIZE,
926 DMA_FROM_DEVICE);
927 kfree(atmel_port->pdc_rx[0].buf);
928 }
929 free_irq(port->irq, port);
930 return -ENOMEM;
931 }
932 pdc->dma_addr = dma_map_single(port->dev,
933 pdc->buf,
934 PDC_BUFFER_SIZE,
935 DMA_FROM_DEVICE);
936 pdc->dma_size = PDC_BUFFER_SIZE;
937 pdc->ofs = 0;
938 }
939
940 atmel_port->pdc_rx_idx = 0;
941
942 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
943 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
944
945 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
946 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
947 }
948 if (atmel_use_dma_tx(port)) {
949 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
950 struct circ_buf *xmit = &port->state->xmit;
951
952 pdc->buf = xmit->buf;
953 pdc->dma_addr = dma_map_single(port->dev,
954 pdc->buf,
955 UART_XMIT_SIZE,
956 DMA_TO_DEVICE);
957 pdc->dma_size = UART_XMIT_SIZE;
958 pdc->ofs = 0;
959 }
960
961 /*
962 * If there is a specific "open" function (to register
963 * control line interrupts)
964 */
965 if (atmel_open_hook) {
966 retval = atmel_open_hook(port);
967 if (retval) {
968 free_irq(port->irq, port);
969 return retval;
970 }
971 }
972
973 /* Save current CSR for comparison in atmel_tasklet_func() */
974 atmel_port->irq_status_prev = UART_GET_CSR(port);
975 atmel_port->irq_status = atmel_port->irq_status_prev;
976
977 /*
978 * Finally, enable the serial port
979 */
980 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
981 /* enable xmit & rcvr */
982 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
983
984 if (atmel_use_dma_rx(port)) {
985 /* set UART timeout */
986 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
987 UART_PUT_CR(port, ATMEL_US_STTTO);
988
989 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
990 /* enable PDC controller */
991 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
992 } else {
993 /* enable receive only */
994 UART_PUT_IER(port, ATMEL_US_RXRDY);
995 }
996
997 return 0;
998}
999
1000/*
1001 * Disable the port
1002 */
1003static void atmel_shutdown(struct uart_port *port)
1004{
1005 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1006 /*
1007 * Ensure everything is stopped.
1008 */
1009 atmel_stop_rx(port);
1010 atmel_stop_tx(port);
1011
1012 /*
1013 * Shut-down the DMA.
1014 */
1015 if (atmel_use_dma_rx(port)) {
1016 int i;
1017
1018 for (i = 0; i < 2; i++) {
1019 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1020
1021 dma_unmap_single(port->dev,
1022 pdc->dma_addr,
1023 pdc->dma_size,
1024 DMA_FROM_DEVICE);
1025 kfree(pdc->buf);
1026 }
1027 }
1028 if (atmel_use_dma_tx(port)) {
1029 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1030
1031 dma_unmap_single(port->dev,
1032 pdc->dma_addr,
1033 pdc->dma_size,
1034 DMA_TO_DEVICE);
1035 }
1036
1037 /*
1038 * Disable all interrupts, port and break condition.
1039 */
1040 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1041 UART_PUT_IDR(port, -1);
1042
1043 /*
1044 * Free the interrupt
1045 */
1046 free_irq(port->irq, port);
1047
1048 /*
1049 * If there is a specific "close" function (to unregister
1050 * control line interrupts)
1051 */
1052 if (atmel_close_hook)
1053 atmel_close_hook(port);
1054}
1055
1056/*
1057 * Flush any TX data submitted for DMA. Called when the TX circular
1058 * buffer is reset.
1059 */
1060static void atmel_flush_buffer(struct uart_port *port)
1061{
1062 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1063
1064 if (atmel_use_dma_tx(port)) {
1065 UART_PUT_TCR(port, 0);
1066 atmel_port->pdc_tx.ofs = 0;
1067 }
1068}
1069
1070/*
1071 * Power / Clock management.
1072 */
1073static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1074 unsigned int oldstate)
1075{
1076 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1077
1078 switch (state) {
1079 case 0:
1080 /*
1081 * Enable the peripheral clock for this serial port.
1082 * This is called on uart_open() or a resume event.
1083 */
1084 clk_enable(atmel_port->clk);
1085
1086 /* re-enable interrupts if we disabled some on suspend */
1087 UART_PUT_IER(port, atmel_port->backup_imr);
1088 break;
1089 case 3:
1090 /* Back up the interrupt mask and disable all interrupts */
1091 atmel_port->backup_imr = UART_GET_IMR(port);
1092 UART_PUT_IDR(port, -1);
1093
1094 /*
1095 * Disable the peripheral clock for this serial port.
1096 * This is called on uart_close() or a suspend event.
1097 */
1098 clk_disable(atmel_port->clk);
1099 break;
1100 default:
1101 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1102 }
1103}
1104
1105/*
1106 * Change the port parameters
1107 */
1108static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1109 struct ktermios *old)
1110{
1111 unsigned long flags;
1112 unsigned int mode, imr, quot, baud;
1113 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1114
1115 /* Get current mode register */
1116 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1117 | ATMEL_US_NBSTOP | ATMEL_US_PAR
1118 | ATMEL_US_USMODE);
1119
1120 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1121 quot = uart_get_divisor(port, baud);
1122
1123 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
1124 quot /= 8;
1125 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1126 }
1127
1128 /* byte size */
1129 switch (termios->c_cflag & CSIZE) {
1130 case CS5:
1131 mode |= ATMEL_US_CHRL_5;
1132 break;
1133 case CS6:
1134 mode |= ATMEL_US_CHRL_6;
1135 break;
1136 case CS7:
1137 mode |= ATMEL_US_CHRL_7;
1138 break;
1139 default:
1140 mode |= ATMEL_US_CHRL_8;
1141 break;
1142 }
1143
1144 /* stop bits */
1145 if (termios->c_cflag & CSTOPB)
1146 mode |= ATMEL_US_NBSTOP_2;
1147
1148 /* parity */
1149 if (termios->c_cflag & PARENB) {
1150 /* Mark or Space parity */
1151 if (termios->c_cflag & CMSPAR) {
1152 if (termios->c_cflag & PARODD)
1153 mode |= ATMEL_US_PAR_MARK;
1154 else
1155 mode |= ATMEL_US_PAR_SPACE;
1156 } else if (termios->c_cflag & PARODD)
1157 mode |= ATMEL_US_PAR_ODD;
1158 else
1159 mode |= ATMEL_US_PAR_EVEN;
1160 } else
1161 mode |= ATMEL_US_PAR_NONE;
1162
1163 /* hardware handshake (RTS/CTS) */
1164 if (termios->c_cflag & CRTSCTS)
1165 mode |= ATMEL_US_USMODE_HWHS;
1166 else
1167 mode |= ATMEL_US_USMODE_NORMAL;
1168
1169 spin_lock_irqsave(&port->lock, flags);
1170
1171 port->read_status_mask = ATMEL_US_OVRE;
1172 if (termios->c_iflag & INPCK)
1173 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1174 if (termios->c_iflag & (BRKINT | PARMRK))
1175 port->read_status_mask |= ATMEL_US_RXBRK;
1176
1177 if (atmel_use_dma_rx(port))
1178 /* need to enable error interrupts */
1179 UART_PUT_IER(port, port->read_status_mask);
1180
1181 /*
1182 * Characters to ignore
1183 */
1184 port->ignore_status_mask = 0;
1185 if (termios->c_iflag & IGNPAR)
1186 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1187 if (termios->c_iflag & IGNBRK) {
1188 port->ignore_status_mask |= ATMEL_US_RXBRK;
1189 /*
1190 * If we're ignoring parity and break indicators,
1191 * ignore overruns too (for real raw support).
1192 */
1193 if (termios->c_iflag & IGNPAR)
1194 port->ignore_status_mask |= ATMEL_US_OVRE;
1195 }
1196 /* TODO: Ignore all characters if CREAD is set.*/
1197
1198 /* update the per-port timeout */
1199 uart_update_timeout(port, termios->c_cflag, baud);
1200
1201 /*
1202 * save/disable interrupts. The tty layer will ensure that the
1203 * transmitter is empty if requested by the caller, so there's
1204 * no need to wait for it here.
1205 */
1206 imr = UART_GET_IMR(port);
1207 UART_PUT_IDR(port, -1);
1208
1209 /* disable receiver and transmitter */
1210 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1211
1212 /* Resetting serial mode to RS232 (0x0) */
1213 mode &= ~ATMEL_US_USMODE;
1214
1215 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1216 dev_dbg(port->dev, "Setting UART to RS485\n");
1217 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1218 UART_PUT_TTGR(port,
1219 atmel_port->rs485.delay_rts_after_send);
1220 mode |= ATMEL_US_USMODE_RS485;
1221 } else {
1222 dev_dbg(port->dev, "Setting UART to RS232\n");
1223 }
1224
1225 /* set the parity, stop bits and data size */
1226 UART_PUT_MR(port, mode);
1227
1228 /* set the baud rate */
1229 UART_PUT_BRGR(port, quot);
1230 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1231 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1232
1233 /* restore interrupts */
1234 UART_PUT_IER(port, imr);
1235
1236 /* CTS flow-control and modem-status interrupts */
1237 if (UART_ENABLE_MS(port, termios->c_cflag))
1238 port->ops->enable_ms(port);
1239
1240 spin_unlock_irqrestore(&port->lock, flags);
1241}
1242
1243/*
1244 * Return string describing the specified port
1245 */
1246static const char *atmel_type(struct uart_port *port)
1247{
1248 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1249}
1250
1251/*
1252 * Release the memory region(s) being used by 'port'.
1253 */
1254static void atmel_release_port(struct uart_port *port)
1255{
1256 struct platform_device *pdev = to_platform_device(port->dev);
1257 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1258
1259 release_mem_region(port->mapbase, size);
1260
1261 if (port->flags & UPF_IOREMAP) {
1262 iounmap(port->membase);
1263 port->membase = NULL;
1264 }
1265}
1266
1267/*
1268 * Request the memory region(s) being used by 'port'.
1269 */
1270static int atmel_request_port(struct uart_port *port)
1271{
1272 struct platform_device *pdev = to_platform_device(port->dev);
1273 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1274
1275 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1276 return -EBUSY;
1277
1278 if (port->flags & UPF_IOREMAP) {
1279 port->membase = ioremap(port->mapbase, size);
1280 if (port->membase == NULL) {
1281 release_mem_region(port->mapbase, size);
1282 return -ENOMEM;
1283 }
1284 }
1285
1286 return 0;
1287}
1288
1289/*
1290 * Configure/autoconfigure the port.
1291 */
1292static void atmel_config_port(struct uart_port *port, int flags)
1293{
1294 if (flags & UART_CONFIG_TYPE) {
1295 port->type = PORT_ATMEL;
1296 atmel_request_port(port);
1297 }
1298}
1299
1300/*
1301 * Verify the new serial_struct (for TIOCSSERIAL).
1302 */
1303static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1304{
1305 int ret = 0;
1306 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1307 ret = -EINVAL;
1308 if (port->irq != ser->irq)
1309 ret = -EINVAL;
1310 if (ser->io_type != SERIAL_IO_MEM)
1311 ret = -EINVAL;
1312 if (port->uartclk / 16 != ser->baud_base)
1313 ret = -EINVAL;
1314 if ((void *)port->mapbase != ser->iomem_base)
1315 ret = -EINVAL;
1316 if (port->iobase != ser->port)
1317 ret = -EINVAL;
1318 if (ser->hub6 != 0)
1319 ret = -EINVAL;
1320 return ret;
1321}
1322
1323#ifdef CONFIG_CONSOLE_POLL
1324static int atmel_poll_get_char(struct uart_port *port)
1325{
1326 while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1327 cpu_relax();
1328
1329 return UART_GET_CHAR(port);
1330}
1331
1332static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1333{
1334 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1335 cpu_relax();
1336
1337 UART_PUT_CHAR(port, ch);
1338}
1339#endif
1340
1341static int
1342atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1343{
1344 struct serial_rs485 rs485conf;
1345
1346 switch (cmd) {
1347 case TIOCSRS485:
1348 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1349 sizeof(rs485conf)))
1350 return -EFAULT;
1351
1352 atmel_config_rs485(port, &rs485conf);
1353 break;
1354
1355 case TIOCGRS485:
1356 if (copy_to_user((struct serial_rs485 *) arg,
1357 &(to_atmel_uart_port(port)->rs485),
1358 sizeof(rs485conf)))
1359 return -EFAULT;
1360 break;
1361
1362 default:
1363 return -ENOIOCTLCMD;
1364 }
1365 return 0;
1366}
1367
1368
1369
1370static struct uart_ops atmel_pops = {
1371 .tx_empty = atmel_tx_empty,
1372 .set_mctrl = atmel_set_mctrl,
1373 .get_mctrl = atmel_get_mctrl,
1374 .stop_tx = atmel_stop_tx,
1375 .start_tx = atmel_start_tx,
1376 .stop_rx = atmel_stop_rx,
1377 .enable_ms = atmel_enable_ms,
1378 .break_ctl = atmel_break_ctl,
1379 .startup = atmel_startup,
1380 .shutdown = atmel_shutdown,
1381 .flush_buffer = atmel_flush_buffer,
1382 .set_termios = atmel_set_termios,
1383 .type = atmel_type,
1384 .release_port = atmel_release_port,
1385 .request_port = atmel_request_port,
1386 .config_port = atmel_config_port,
1387 .verify_port = atmel_verify_port,
1388 .pm = atmel_serial_pm,
1389 .ioctl = atmel_ioctl,
1390#ifdef CONFIG_CONSOLE_POLL
1391 .poll_get_char = atmel_poll_get_char,
1392 .poll_put_char = atmel_poll_put_char,
1393#endif
1394};
1395
1396/*
1397 * Configure the port from the platform device resource info.
1398 */
1399static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
1400 struct platform_device *pdev)
1401{
1402 struct uart_port *port = &atmel_port->uart;
1403 struct atmel_uart_data *data = pdev->dev.platform_data;
1404
1405 port->iotype = UPIO_MEM;
1406 port->flags = UPF_BOOT_AUTOCONF;
1407 port->ops = &atmel_pops;
1408 port->fifosize = 1;
1409 port->line = pdev->id;
1410 port->dev = &pdev->dev;
1411 port->mapbase = pdev->resource[0].start;
1412 port->irq = pdev->resource[1].start;
1413
1414 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1415 (unsigned long)port);
1416
1417 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1418
1419 if (data->regs)
1420 /* Already mapped by setup code */
1421 port->membase = data->regs;
1422 else {
1423 port->flags |= UPF_IOREMAP;
1424 port->membase = NULL;
1425 }
1426
1427 /* for console, the clock could already be configured */
1428 if (!atmel_port->clk) {
1429 atmel_port->clk = clk_get(&pdev->dev, "usart");
1430 clk_enable(atmel_port->clk);
1431 port->uartclk = clk_get_rate(atmel_port->clk);
1432 clk_disable(atmel_port->clk);
1433 /* only enable clock when USART is in use */
1434 }
1435
1436 atmel_port->use_dma_rx = data->use_dma_rx;
1437 atmel_port->use_dma_tx = data->use_dma_tx;
1438 atmel_port->rs485 = data->rs485;
1439 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
1440 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
1441 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
1442 else if (atmel_use_dma_tx(port)) {
1443 port->fifosize = PDC_BUFFER_SIZE;
1444 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
1445 } else {
1446 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
1447 }
1448}
1449
1450/*
1451 * Register board-specific modem-control line handlers.
1452 */
1453void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
1454{
1455 if (fns->enable_ms)
1456 atmel_pops.enable_ms = fns->enable_ms;
1457 if (fns->get_mctrl)
1458 atmel_pops.get_mctrl = fns->get_mctrl;
1459 if (fns->set_mctrl)
1460 atmel_pops.set_mctrl = fns->set_mctrl;
1461 atmel_open_hook = fns->open;
1462 atmel_close_hook = fns->close;
1463 atmel_pops.pm = fns->pm;
1464 atmel_pops.set_wake = fns->set_wake;
1465}
1466
1467#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1468static void atmel_console_putchar(struct uart_port *port, int ch)
1469{
1470 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1471 cpu_relax();
1472 UART_PUT_CHAR(port, ch);
1473}
1474
1475/*
1476 * Interrupts are disabled on entering
1477 */
1478static void atmel_console_write(struct console *co, const char *s, u_int count)
1479{
1480 struct uart_port *port = &atmel_ports[co->index].uart;
1481 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1482 unsigned int status, imr;
1483 unsigned int pdc_tx;
1484
1485 /*
1486 * First, save IMR and then disable interrupts
1487 */
1488 imr = UART_GET_IMR(port);
1489 UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
1490
1491 /* Store PDC transmit status and disable it */
1492 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
1493 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1494
1495 uart_console_write(port, s, count, atmel_console_putchar);
1496
1497 /*
1498 * Finally, wait for transmitter to become empty
1499 * and restore IMR
1500 */
1501 do {
1502 status = UART_GET_CSR(port);
1503 } while (!(status & ATMEL_US_TXRDY));
1504
1505 /* Restore PDC transmit status */
1506 if (pdc_tx)
1507 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1508
1509 /* set interrupts back the way they were */
1510 UART_PUT_IER(port, imr);
1511}
1512
1513/*
1514 * If the port was already initialised (eg, by a boot loader),
1515 * try to determine the current setup.
1516 */
1517static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1518 int *parity, int *bits)
1519{
1520 unsigned int mr, quot;
1521
1522 /*
1523 * If the baud rate generator isn't running, the port wasn't
1524 * initialized by the boot loader.
1525 */
1526 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
1527 if (!quot)
1528 return;
1529
1530 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1531 if (mr == ATMEL_US_CHRL_8)
1532 *bits = 8;
1533 else
1534 *bits = 7;
1535
1536 mr = UART_GET_MR(port) & ATMEL_US_PAR;
1537 if (mr == ATMEL_US_PAR_EVEN)
1538 *parity = 'e';
1539 else if (mr == ATMEL_US_PAR_ODD)
1540 *parity = 'o';
1541
1542 /*
1543 * The serial core only rounds down when matching this to a
1544 * supported baud rate. Make sure we don't end up slightly
1545 * lower than one of those, as it would make us fall through
1546 * to a much lower baud rate than we really want.
1547 */
1548 *baud = port->uartclk / (16 * (quot - 1));
1549}
1550
1551static int __init atmel_console_setup(struct console *co, char *options)
1552{
1553 struct uart_port *port = &atmel_ports[co->index].uart;
1554 int baud = 115200;
1555 int bits = 8;
1556 int parity = 'n';
1557 int flow = 'n';
1558
1559 if (port->membase == NULL) {
1560 /* Port not initialized yet - delay setup */
1561 return -ENODEV;
1562 }
1563
1564 clk_enable(atmel_ports[co->index].clk);
1565
1566 UART_PUT_IDR(port, -1);
1567 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1568 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1569
1570 if (options)
1571 uart_parse_options(options, &baud, &parity, &bits, &flow);
1572 else
1573 atmel_console_get_options(port, &baud, &parity, &bits);
1574
1575 return uart_set_options(port, co, baud, parity, bits, flow);
1576}
1577
1578static struct uart_driver atmel_uart;
1579
1580static struct console atmel_console = {
1581 .name = ATMEL_DEVICENAME,
1582 .write = atmel_console_write,
1583 .device = uart_console_device,
1584 .setup = atmel_console_setup,
1585 .flags = CON_PRINTBUFFER,
1586 .index = -1,
1587 .data = &atmel_uart,
1588};
1589
1590#define ATMEL_CONSOLE_DEVICE (&atmel_console)
1591
1592/*
1593 * Early console initialization (before VM subsystem initialized).
1594 */
1595static int __init atmel_console_init(void)
1596{
1597 if (atmel_default_console_device) {
1598 add_preferred_console(ATMEL_DEVICENAME,
1599 atmel_default_console_device->id, NULL);
1600 atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1601 atmel_default_console_device);
1602 register_console(&atmel_console);
1603 }
1604
1605 return 0;
1606}
1607
1608console_initcall(atmel_console_init);
1609
1610/*
1611 * Late console initialization.
1612 */
1613static int __init atmel_late_console_init(void)
1614{
1615 if (atmel_default_console_device
1616 && !(atmel_console.flags & CON_ENABLED))
1617 register_console(&atmel_console);
1618
1619 return 0;
1620}
1621
1622core_initcall(atmel_late_console_init);
1623
1624static inline bool atmel_is_console_port(struct uart_port *port)
1625{
1626 return port->cons && port->cons->index == port->line;
1627}
1628
1629#else
1630#define ATMEL_CONSOLE_DEVICE NULL
1631
1632static inline bool atmel_is_console_port(struct uart_port *port)
1633{
1634 return false;
1635}
1636#endif
1637
1638static struct uart_driver atmel_uart = {
1639 .owner = THIS_MODULE,
1640 .driver_name = "atmel_serial",
1641 .dev_name = ATMEL_DEVICENAME,
1642 .major = SERIAL_ATMEL_MAJOR,
1643 .minor = MINOR_START,
1644 .nr = ATMEL_MAX_UART,
1645 .cons = ATMEL_CONSOLE_DEVICE,
1646};
1647
1648#ifdef CONFIG_PM
1649static bool atmel_serial_clk_will_stop(void)
1650{
1651#ifdef CONFIG_ARCH_AT91
1652 return at91_suspend_entering_slow_clock();
1653#else
1654 return false;
1655#endif
1656}
1657
1658static int atmel_serial_suspend(struct platform_device *pdev,
1659 pm_message_t state)
1660{
1661 struct uart_port *port = platform_get_drvdata(pdev);
1662 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1663
1664 if (atmel_is_console_port(port) && console_suspend_enabled) {
1665 /* Drain the TX shifter */
1666 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1667 cpu_relax();
1668 }
1669
1670 /* we can not wake up if we're running on slow clock */
1671 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
1672 if (atmel_serial_clk_will_stop())
1673 device_set_wakeup_enable(&pdev->dev, 0);
1674
1675 uart_suspend_port(&atmel_uart, port);
1676
1677 return 0;
1678}
1679
1680static int atmel_serial_resume(struct platform_device *pdev)
1681{
1682 struct uart_port *port = platform_get_drvdata(pdev);
1683 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1684
1685 uart_resume_port(&atmel_uart, port);
1686 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
1687
1688 return 0;
1689}
1690#else
1691#define atmel_serial_suspend NULL
1692#define atmel_serial_resume NULL
1693#endif
1694
1695static int __devinit atmel_serial_probe(struct platform_device *pdev)
1696{
1697 struct atmel_uart_port *port;
1698 void *data;
1699 int ret;
1700
1701 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
1702
1703 port = &atmel_ports[pdev->id];
1704 port->backup_imr = 0;
1705
1706 atmel_init_port(port, pdev);
1707
1708 if (!atmel_use_dma_rx(&port->uart)) {
1709 ret = -ENOMEM;
1710 data = kmalloc(sizeof(struct atmel_uart_char)
1711 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
1712 if (!data)
1713 goto err_alloc_ring;
1714 port->rx_ring.buf = data;
1715 }
1716
1717 ret = uart_add_one_port(&atmel_uart, &port->uart);
1718 if (ret)
1719 goto err_add_port;
1720
1721#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1722 if (atmel_is_console_port(&port->uart)
1723 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
1724 /*
1725 * The serial core enabled the clock for us, so undo
1726 * the clk_enable() in atmel_console_setup()
1727 */
1728 clk_disable(port->clk);
1729 }
1730#endif
1731
1732 device_init_wakeup(&pdev->dev, 1);
1733 platform_set_drvdata(pdev, port);
1734
1735 return 0;
1736
1737err_add_port:
1738 kfree(port->rx_ring.buf);
1739 port->rx_ring.buf = NULL;
1740err_alloc_ring:
1741 if (!atmel_is_console_port(&port->uart)) {
1742 clk_put(port->clk);
1743 port->clk = NULL;
1744 }
1745
1746 return ret;
1747}
1748
1749static int __devexit atmel_serial_remove(struct platform_device *pdev)
1750{
1751 struct uart_port *port = platform_get_drvdata(pdev);
1752 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1753 int ret = 0;
1754
1755 device_init_wakeup(&pdev->dev, 0);
1756 platform_set_drvdata(pdev, NULL);
1757
1758 ret = uart_remove_one_port(&atmel_uart, port);
1759
1760 tasklet_kill(&atmel_port->tasklet);
1761 kfree(atmel_port->rx_ring.buf);
1762
1763 /* "port" is allocated statically, so we shouldn't free it */
1764
1765 clk_put(atmel_port->clk);
1766
1767 return ret;
1768}
1769
1770static struct platform_driver atmel_serial_driver = {
1771 .probe = atmel_serial_probe,
1772 .remove = __devexit_p(atmel_serial_remove),
1773 .suspend = atmel_serial_suspend,
1774 .resume = atmel_serial_resume,
1775 .driver = {
1776 .name = "atmel_usart",
1777 .owner = THIS_MODULE,
1778 },
1779};
1780
1781static int __init atmel_serial_init(void)
1782{
1783 int ret;
1784
1785 ret = uart_register_driver(&atmel_uart);
1786 if (ret)
1787 return ret;
1788
1789 ret = platform_driver_register(&atmel_serial_driver);
1790 if (ret)
1791 uart_unregister_driver(&atmel_uart);
1792
1793 return ret;
1794}
1795
1796static void __exit atmel_serial_exit(void)
1797{
1798 platform_driver_unregister(&atmel_serial_driver);
1799 uart_unregister_driver(&atmel_uart);
1800}
1801
1802module_init(atmel_serial_init);
1803module_exit(atmel_serial_exit);
1804
1805MODULE_AUTHOR("Rick Bronson");
1806MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1807MODULE_LICENSE("GPL");
1808MODULE_ALIAS("platform:atmel_usart");
diff --git a/drivers/tty/serial/bcm63xx_uart.c b/drivers/tty/serial/bcm63xx_uart.c
new file mode 100644
index 000000000000..a1a0e55d0807
--- /dev/null
+++ b/drivers/tty/serial/bcm63xx_uart.c
@@ -0,0 +1,891 @@
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 * Derived from many drivers using generic_serial interface.
7 *
8 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
9 *
10 * Serial driver for BCM63xx integrated UART.
11 *
12 * Hardware flow control was _not_ tested since I only have RX/TX on
13 * my board.
14 */
15
16#if defined(CONFIG_SERIAL_BCM63XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
17#define SUPPORT_SYSRQ
18#endif
19
20#include <linux/kernel.h>
21#include <linux/platform_device.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/module.h>
25#include <linux/console.h>
26#include <linux/clk.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/sysrq.h>
30#include <linux/serial.h>
31#include <linux/serial_core.h>
32
33#include <bcm63xx_clk.h>
34#include <bcm63xx_irq.h>
35#include <bcm63xx_regs.h>
36#include <bcm63xx_io.h>
37
38#define BCM63XX_NR_UARTS 2
39
40static struct uart_port ports[BCM63XX_NR_UARTS];
41
42/*
43 * rx interrupt mask / stat
44 *
45 * mask:
46 * - rx fifo full
47 * - rx fifo above threshold
48 * - rx fifo not empty for too long
49 */
50#define UART_RX_INT_MASK (UART_IR_MASK(UART_IR_RXOVER) | \
51 UART_IR_MASK(UART_IR_RXTHRESH) | \
52 UART_IR_MASK(UART_IR_RXTIMEOUT))
53
54#define UART_RX_INT_STAT (UART_IR_STAT(UART_IR_RXOVER) | \
55 UART_IR_STAT(UART_IR_RXTHRESH) | \
56 UART_IR_STAT(UART_IR_RXTIMEOUT))
57
58/*
59 * tx interrupt mask / stat
60 *
61 * mask:
62 * - tx fifo empty
63 * - tx fifo below threshold
64 */
65#define UART_TX_INT_MASK (UART_IR_MASK(UART_IR_TXEMPTY) | \
66 UART_IR_MASK(UART_IR_TXTRESH))
67
68#define UART_TX_INT_STAT (UART_IR_STAT(UART_IR_TXEMPTY) | \
69 UART_IR_STAT(UART_IR_TXTRESH))
70
71/*
72 * external input interrupt
73 *
74 * mask: any edge on CTS, DCD
75 */
76#define UART_EXTINP_INT_MASK (UART_EXTINP_IRMASK(UART_EXTINP_IR_CTS) | \
77 UART_EXTINP_IRMASK(UART_EXTINP_IR_DCD))
78
79/*
80 * handy uart register accessor
81 */
82static inline unsigned int bcm_uart_readl(struct uart_port *port,
83 unsigned int offset)
84{
85 return bcm_readl(port->membase + offset);
86}
87
88static inline void bcm_uart_writel(struct uart_port *port,
89 unsigned int value, unsigned int offset)
90{
91 bcm_writel(value, port->membase + offset);
92}
93
94/*
95 * serial core request to check if uart tx fifo is empty
96 */
97static unsigned int bcm_uart_tx_empty(struct uart_port *port)
98{
99 unsigned int val;
100
101 val = bcm_uart_readl(port, UART_IR_REG);
102 return (val & UART_IR_STAT(UART_IR_TXEMPTY)) ? 1 : 0;
103}
104
105/*
106 * serial core request to set RTS and DTR pin state and loopback mode
107 */
108static void bcm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
109{
110 unsigned int val;
111
112 val = bcm_uart_readl(port, UART_MCTL_REG);
113 val &= ~(UART_MCTL_DTR_MASK | UART_MCTL_RTS_MASK);
114 /* invert of written value is reflected on the pin */
115 if (!(mctrl & TIOCM_DTR))
116 val |= UART_MCTL_DTR_MASK;
117 if (!(mctrl & TIOCM_RTS))
118 val |= UART_MCTL_RTS_MASK;
119 bcm_uart_writel(port, val, UART_MCTL_REG);
120
121 val = bcm_uart_readl(port, UART_CTL_REG);
122 if (mctrl & TIOCM_LOOP)
123 val |= UART_CTL_LOOPBACK_MASK;
124 else
125 val &= ~UART_CTL_LOOPBACK_MASK;
126 bcm_uart_writel(port, val, UART_CTL_REG);
127}
128
129/*
130 * serial core request to return RI, CTS, DCD and DSR pin state
131 */
132static unsigned int bcm_uart_get_mctrl(struct uart_port *port)
133{
134 unsigned int val, mctrl;
135
136 mctrl = 0;
137 val = bcm_uart_readl(port, UART_EXTINP_REG);
138 if (val & UART_EXTINP_RI_MASK)
139 mctrl |= TIOCM_RI;
140 if (val & UART_EXTINP_CTS_MASK)
141 mctrl |= TIOCM_CTS;
142 if (val & UART_EXTINP_DCD_MASK)
143 mctrl |= TIOCM_CD;
144 if (val & UART_EXTINP_DSR_MASK)
145 mctrl |= TIOCM_DSR;
146 return mctrl;
147}
148
149/*
150 * serial core request to disable tx ASAP (used for flow control)
151 */
152static void bcm_uart_stop_tx(struct uart_port *port)
153{
154 unsigned int val;
155
156 val = bcm_uart_readl(port, UART_CTL_REG);
157 val &= ~(UART_CTL_TXEN_MASK);
158 bcm_uart_writel(port, val, UART_CTL_REG);
159
160 val = bcm_uart_readl(port, UART_IR_REG);
161 val &= ~UART_TX_INT_MASK;
162 bcm_uart_writel(port, val, UART_IR_REG);
163}
164
165/*
166 * serial core request to (re)enable tx
167 */
168static void bcm_uart_start_tx(struct uart_port *port)
169{
170 unsigned int val;
171
172 val = bcm_uart_readl(port, UART_IR_REG);
173 val |= UART_TX_INT_MASK;
174 bcm_uart_writel(port, val, UART_IR_REG);
175
176 val = bcm_uart_readl(port, UART_CTL_REG);
177 val |= UART_CTL_TXEN_MASK;
178 bcm_uart_writel(port, val, UART_CTL_REG);
179}
180
181/*
182 * serial core request to stop rx, called before port shutdown
183 */
184static void bcm_uart_stop_rx(struct uart_port *port)
185{
186 unsigned int val;
187
188 val = bcm_uart_readl(port, UART_IR_REG);
189 val &= ~UART_RX_INT_MASK;
190 bcm_uart_writel(port, val, UART_IR_REG);
191}
192
193/*
194 * serial core request to enable modem status interrupt reporting
195 */
196static void bcm_uart_enable_ms(struct uart_port *port)
197{
198 unsigned int val;
199
200 val = bcm_uart_readl(port, UART_IR_REG);
201 val |= UART_IR_MASK(UART_IR_EXTIP);
202 bcm_uart_writel(port, val, UART_IR_REG);
203}
204
205/*
206 * serial core request to start/stop emitting break char
207 */
208static void bcm_uart_break_ctl(struct uart_port *port, int ctl)
209{
210 unsigned long flags;
211 unsigned int val;
212
213 spin_lock_irqsave(&port->lock, flags);
214
215 val = bcm_uart_readl(port, UART_CTL_REG);
216 if (ctl)
217 val |= UART_CTL_XMITBRK_MASK;
218 else
219 val &= ~UART_CTL_XMITBRK_MASK;
220 bcm_uart_writel(port, val, UART_CTL_REG);
221
222 spin_unlock_irqrestore(&port->lock, flags);
223}
224
225/*
226 * return port type in string format
227 */
228static const char *bcm_uart_type(struct uart_port *port)
229{
230 return (port->type == PORT_BCM63XX) ? "bcm63xx_uart" : NULL;
231}
232
233/*
234 * read all chars in rx fifo and send them to core
235 */
236static void bcm_uart_do_rx(struct uart_port *port)
237{
238 struct tty_struct *tty;
239 unsigned int max_count;
240
241 /* limit number of char read in interrupt, should not be
242 * higher than fifo size anyway since we're much faster than
243 * serial port */
244 max_count = 32;
245 tty = port->state->port.tty;
246 do {
247 unsigned int iestat, c, cstat;
248 char flag;
249
250 /* get overrun/fifo empty information from ier
251 * register */
252 iestat = bcm_uart_readl(port, UART_IR_REG);
253 if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
254 break;
255
256 cstat = c = bcm_uart_readl(port, UART_FIFO_REG);
257 port->icount.rx++;
258 flag = TTY_NORMAL;
259 c &= 0xff;
260
261 if (unlikely((cstat & UART_FIFO_ANYERR_MASK))) {
262 /* do stats first */
263 if (cstat & UART_FIFO_BRKDET_MASK) {
264 port->icount.brk++;
265 if (uart_handle_break(port))
266 continue;
267 }
268
269 if (cstat & UART_FIFO_PARERR_MASK)
270 port->icount.parity++;
271 if (cstat & UART_FIFO_FRAMEERR_MASK)
272 port->icount.frame++;
273
274 /* update flag wrt read_status_mask */
275 cstat &= port->read_status_mask;
276 if (cstat & UART_FIFO_BRKDET_MASK)
277 flag = TTY_BREAK;
278 if (cstat & UART_FIFO_FRAMEERR_MASK)
279 flag = TTY_FRAME;
280 if (cstat & UART_FIFO_PARERR_MASK)
281 flag = TTY_PARITY;
282 }
283
284 if (uart_handle_sysrq_char(port, c))
285 continue;
286
287 if (unlikely(iestat & UART_IR_STAT(UART_IR_RXOVER))) {
288 port->icount.overrun++;
289 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
290 }
291
292 if ((cstat & port->ignore_status_mask) == 0)
293 tty_insert_flip_char(tty, c, flag);
294
295 } while (--max_count);
296
297 tty_flip_buffer_push(tty);
298}
299
300/*
301 * fill tx fifo with chars to send, stop when fifo is about to be full
302 * or when all chars have been sent.
303 */
304static void bcm_uart_do_tx(struct uart_port *port)
305{
306 struct circ_buf *xmit;
307 unsigned int val, max_count;
308
309 if (port->x_char) {
310 bcm_uart_writel(port, port->x_char, UART_FIFO_REG);
311 port->icount.tx++;
312 port->x_char = 0;
313 return;
314 }
315
316 if (uart_tx_stopped(port)) {
317 bcm_uart_stop_tx(port);
318 return;
319 }
320
321 xmit = &port->state->xmit;
322 if (uart_circ_empty(xmit))
323 goto txq_empty;
324
325 val = bcm_uart_readl(port, UART_MCTL_REG);
326 val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT;
327 max_count = port->fifosize - val;
328
329 while (max_count--) {
330 unsigned int c;
331
332 c = xmit->buf[xmit->tail];
333 bcm_uart_writel(port, c, UART_FIFO_REG);
334 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
335 port->icount.tx++;
336 if (uart_circ_empty(xmit))
337 break;
338 }
339
340 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
341 uart_write_wakeup(port);
342
343 if (uart_circ_empty(xmit))
344 goto txq_empty;
345 return;
346
347txq_empty:
348 /* nothing to send, disable transmit interrupt */
349 val = bcm_uart_readl(port, UART_IR_REG);
350 val &= ~UART_TX_INT_MASK;
351 bcm_uart_writel(port, val, UART_IR_REG);
352 return;
353}
354
355/*
356 * process uart interrupt
357 */
358static irqreturn_t bcm_uart_interrupt(int irq, void *dev_id)
359{
360 struct uart_port *port;
361 unsigned int irqstat;
362
363 port = dev_id;
364 spin_lock(&port->lock);
365
366 irqstat = bcm_uart_readl(port, UART_IR_REG);
367 if (irqstat & UART_RX_INT_STAT)
368 bcm_uart_do_rx(port);
369
370 if (irqstat & UART_TX_INT_STAT)
371 bcm_uart_do_tx(port);
372
373 if (irqstat & UART_IR_MASK(UART_IR_EXTIP)) {
374 unsigned int estat;
375
376 estat = bcm_uart_readl(port, UART_EXTINP_REG);
377 if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_CTS))
378 uart_handle_cts_change(port,
379 estat & UART_EXTINP_CTS_MASK);
380 if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_DCD))
381 uart_handle_dcd_change(port,
382 estat & UART_EXTINP_DCD_MASK);
383 }
384
385 spin_unlock(&port->lock);
386 return IRQ_HANDLED;
387}
388
389/*
390 * enable rx & tx operation on uart
391 */
392static void bcm_uart_enable(struct uart_port *port)
393{
394 unsigned int val;
395
396 val = bcm_uart_readl(port, UART_CTL_REG);
397 val |= (UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK);
398 bcm_uart_writel(port, val, UART_CTL_REG);
399}
400
401/*
402 * disable rx & tx operation on uart
403 */
404static void bcm_uart_disable(struct uart_port *port)
405{
406 unsigned int val;
407
408 val = bcm_uart_readl(port, UART_CTL_REG);
409 val &= ~(UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK |
410 UART_CTL_RXEN_MASK);
411 bcm_uart_writel(port, val, UART_CTL_REG);
412}
413
414/*
415 * clear all unread data in rx fifo and unsent data in tx fifo
416 */
417static void bcm_uart_flush(struct uart_port *port)
418{
419 unsigned int val;
420
421 /* empty rx and tx fifo */
422 val = bcm_uart_readl(port, UART_CTL_REG);
423 val |= UART_CTL_RSTRXFIFO_MASK | UART_CTL_RSTTXFIFO_MASK;
424 bcm_uart_writel(port, val, UART_CTL_REG);
425
426 /* read any pending char to make sure all irq status are
427 * cleared */
428 (void)bcm_uart_readl(port, UART_FIFO_REG);
429}
430
431/*
432 * serial core request to initialize uart and start rx operation
433 */
434static int bcm_uart_startup(struct uart_port *port)
435{
436 unsigned int val;
437 int ret;
438
439 /* mask all irq and flush port */
440 bcm_uart_disable(port);
441 bcm_uart_writel(port, 0, UART_IR_REG);
442 bcm_uart_flush(port);
443
444 /* clear any pending external input interrupt */
445 (void)bcm_uart_readl(port, UART_EXTINP_REG);
446
447 /* set rx/tx fifo thresh to fifo half size */
448 val = bcm_uart_readl(port, UART_MCTL_REG);
449 val &= ~(UART_MCTL_RXFIFOTHRESH_MASK | UART_MCTL_TXFIFOTHRESH_MASK);
450 val |= (port->fifosize / 2) << UART_MCTL_RXFIFOTHRESH_SHIFT;
451 val |= (port->fifosize / 2) << UART_MCTL_TXFIFOTHRESH_SHIFT;
452 bcm_uart_writel(port, val, UART_MCTL_REG);
453
454 /* set rx fifo timeout to 1 char time */
455 val = bcm_uart_readl(port, UART_CTL_REG);
456 val &= ~UART_CTL_RXTMOUTCNT_MASK;
457 val |= 1 << UART_CTL_RXTMOUTCNT_SHIFT;
458 bcm_uart_writel(port, val, UART_CTL_REG);
459
460 /* report any edge on dcd and cts */
461 val = UART_EXTINP_INT_MASK;
462 val |= UART_EXTINP_DCD_NOSENSE_MASK;
463 val |= UART_EXTINP_CTS_NOSENSE_MASK;
464 bcm_uart_writel(port, val, UART_EXTINP_REG);
465
466 /* register irq and enable rx interrupts */
467 ret = request_irq(port->irq, bcm_uart_interrupt, 0,
468 bcm_uart_type(port), port);
469 if (ret)
470 return ret;
471 bcm_uart_writel(port, UART_RX_INT_MASK, UART_IR_REG);
472 bcm_uart_enable(port);
473 return 0;
474}
475
476/*
477 * serial core request to flush & disable uart
478 */
479static void bcm_uart_shutdown(struct uart_port *port)
480{
481 unsigned long flags;
482
483 spin_lock_irqsave(&port->lock, flags);
484 bcm_uart_writel(port, 0, UART_IR_REG);
485 spin_unlock_irqrestore(&port->lock, flags);
486
487 bcm_uart_disable(port);
488 bcm_uart_flush(port);
489 free_irq(port->irq, port);
490}
491
492/*
493 * serial core request to change current uart setting
494 */
495static void bcm_uart_set_termios(struct uart_port *port,
496 struct ktermios *new,
497 struct ktermios *old)
498{
499 unsigned int ctl, baud, quot, ier;
500 unsigned long flags;
501
502 spin_lock_irqsave(&port->lock, flags);
503
504 /* disable uart while changing speed */
505 bcm_uart_disable(port);
506 bcm_uart_flush(port);
507
508 /* update Control register */
509 ctl = bcm_uart_readl(port, UART_CTL_REG);
510 ctl &= ~UART_CTL_BITSPERSYM_MASK;
511
512 switch (new->c_cflag & CSIZE) {
513 case CS5:
514 ctl |= (0 << UART_CTL_BITSPERSYM_SHIFT);
515 break;
516 case CS6:
517 ctl |= (1 << UART_CTL_BITSPERSYM_SHIFT);
518 break;
519 case CS7:
520 ctl |= (2 << UART_CTL_BITSPERSYM_SHIFT);
521 break;
522 default:
523 ctl |= (3 << UART_CTL_BITSPERSYM_SHIFT);
524 break;
525 }
526
527 ctl &= ~UART_CTL_STOPBITS_MASK;
528 if (new->c_cflag & CSTOPB)
529 ctl |= UART_CTL_STOPBITS_2;
530 else
531 ctl |= UART_CTL_STOPBITS_1;
532
533 ctl &= ~(UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
534 if (new->c_cflag & PARENB)
535 ctl |= (UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
536 ctl &= ~(UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
537 if (new->c_cflag & PARODD)
538 ctl |= (UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
539 bcm_uart_writel(port, ctl, UART_CTL_REG);
540
541 /* update Baudword register */
542 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
543 quot = uart_get_divisor(port, baud) - 1;
544 bcm_uart_writel(port, quot, UART_BAUD_REG);
545
546 /* update Interrupt register */
547 ier = bcm_uart_readl(port, UART_IR_REG);
548
549 ier &= ~UART_IR_MASK(UART_IR_EXTIP);
550 if (UART_ENABLE_MS(port, new->c_cflag))
551 ier |= UART_IR_MASK(UART_IR_EXTIP);
552
553 bcm_uart_writel(port, ier, UART_IR_REG);
554
555 /* update read/ignore mask */
556 port->read_status_mask = UART_FIFO_VALID_MASK;
557 if (new->c_iflag & INPCK) {
558 port->read_status_mask |= UART_FIFO_FRAMEERR_MASK;
559 port->read_status_mask |= UART_FIFO_PARERR_MASK;
560 }
561 if (new->c_iflag & (BRKINT))
562 port->read_status_mask |= UART_FIFO_BRKDET_MASK;
563
564 port->ignore_status_mask = 0;
565 if (new->c_iflag & IGNPAR)
566 port->ignore_status_mask |= UART_FIFO_PARERR_MASK;
567 if (new->c_iflag & IGNBRK)
568 port->ignore_status_mask |= UART_FIFO_BRKDET_MASK;
569 if (!(new->c_cflag & CREAD))
570 port->ignore_status_mask |= UART_FIFO_VALID_MASK;
571
572 uart_update_timeout(port, new->c_cflag, baud);
573 bcm_uart_enable(port);
574 spin_unlock_irqrestore(&port->lock, flags);
575}
576
577/*
578 * serial core request to claim uart iomem
579 */
580static int bcm_uart_request_port(struct uart_port *port)
581{
582 unsigned int size;
583
584 size = RSET_UART_SIZE;
585 if (!request_mem_region(port->mapbase, size, "bcm63xx")) {
586 dev_err(port->dev, "Memory region busy\n");
587 return -EBUSY;
588 }
589
590 port->membase = ioremap(port->mapbase, size);
591 if (!port->membase) {
592 dev_err(port->dev, "Unable to map registers\n");
593 release_mem_region(port->mapbase, size);
594 return -EBUSY;
595 }
596 return 0;
597}
598
599/*
600 * serial core request to release uart iomem
601 */
602static void bcm_uart_release_port(struct uart_port *port)
603{
604 release_mem_region(port->mapbase, RSET_UART_SIZE);
605 iounmap(port->membase);
606}
607
608/*
609 * serial core request to do any port required autoconfiguration
610 */
611static void bcm_uart_config_port(struct uart_port *port, int flags)
612{
613 if (flags & UART_CONFIG_TYPE) {
614 if (bcm_uart_request_port(port))
615 return;
616 port->type = PORT_BCM63XX;
617 }
618}
619
620/*
621 * serial core request to check that port information in serinfo are
622 * suitable
623 */
624static int bcm_uart_verify_port(struct uart_port *port,
625 struct serial_struct *serinfo)
626{
627 if (port->type != PORT_BCM63XX)
628 return -EINVAL;
629 if (port->irq != serinfo->irq)
630 return -EINVAL;
631 if (port->iotype != serinfo->io_type)
632 return -EINVAL;
633 if (port->mapbase != (unsigned long)serinfo->iomem_base)
634 return -EINVAL;
635 return 0;
636}
637
638/* serial core callbacks */
639static struct uart_ops bcm_uart_ops = {
640 .tx_empty = bcm_uart_tx_empty,
641 .get_mctrl = bcm_uart_get_mctrl,
642 .set_mctrl = bcm_uart_set_mctrl,
643 .start_tx = bcm_uart_start_tx,
644 .stop_tx = bcm_uart_stop_tx,
645 .stop_rx = bcm_uart_stop_rx,
646 .enable_ms = bcm_uart_enable_ms,
647 .break_ctl = bcm_uart_break_ctl,
648 .startup = bcm_uart_startup,
649 .shutdown = bcm_uart_shutdown,
650 .set_termios = bcm_uart_set_termios,
651 .type = bcm_uart_type,
652 .release_port = bcm_uart_release_port,
653 .request_port = bcm_uart_request_port,
654 .config_port = bcm_uart_config_port,
655 .verify_port = bcm_uart_verify_port,
656};
657
658
659
660#ifdef CONFIG_SERIAL_BCM63XX_CONSOLE
661static inline void wait_for_xmitr(struct uart_port *port)
662{
663 unsigned int tmout;
664
665 /* Wait up to 10ms for the character(s) to be sent. */
666 tmout = 10000;
667 while (--tmout) {
668 unsigned int val;
669
670 val = bcm_uart_readl(port, UART_IR_REG);
671 if (val & UART_IR_STAT(UART_IR_TXEMPTY))
672 break;
673 udelay(1);
674 }
675
676 /* Wait up to 1s for flow control if necessary */
677 if (port->flags & UPF_CONS_FLOW) {
678 tmout = 1000000;
679 while (--tmout) {
680 unsigned int val;
681
682 val = bcm_uart_readl(port, UART_EXTINP_REG);
683 if (val & UART_EXTINP_CTS_MASK)
684 break;
685 udelay(1);
686 }
687 }
688}
689
690/*
691 * output given char
692 */
693static void bcm_console_putchar(struct uart_port *port, int ch)
694{
695 wait_for_xmitr(port);
696 bcm_uart_writel(port, ch, UART_FIFO_REG);
697}
698
699/*
700 * console core request to output given string
701 */
702static void bcm_console_write(struct console *co, const char *s,
703 unsigned int count)
704{
705 struct uart_port *port;
706 unsigned long flags;
707 int locked;
708
709 port = &ports[co->index];
710
711 local_irq_save(flags);
712 if (port->sysrq) {
713 /* bcm_uart_interrupt() already took the lock */
714 locked = 0;
715 } else if (oops_in_progress) {
716 locked = spin_trylock(&port->lock);
717 } else {
718 spin_lock(&port->lock);
719 locked = 1;
720 }
721
722 /* call helper to deal with \r\n */
723 uart_console_write(port, s, count, bcm_console_putchar);
724
725 /* and wait for char to be transmitted */
726 wait_for_xmitr(port);
727
728 if (locked)
729 spin_unlock(&port->lock);
730 local_irq_restore(flags);
731}
732
733/*
734 * console core request to setup given console, find matching uart
735 * port and setup it.
736 */
737static int bcm_console_setup(struct console *co, char *options)
738{
739 struct uart_port *port;
740 int baud = 9600;
741 int bits = 8;
742 int parity = 'n';
743 int flow = 'n';
744
745 if (co->index < 0 || co->index >= BCM63XX_NR_UARTS)
746 return -EINVAL;
747 port = &ports[co->index];
748 if (!port->membase)
749 return -ENODEV;
750 if (options)
751 uart_parse_options(options, &baud, &parity, &bits, &flow);
752
753 return uart_set_options(port, co, baud, parity, bits, flow);
754}
755
756static struct uart_driver bcm_uart_driver;
757
758static struct console bcm63xx_console = {
759 .name = "ttyS",
760 .write = bcm_console_write,
761 .device = uart_console_device,
762 .setup = bcm_console_setup,
763 .flags = CON_PRINTBUFFER,
764 .index = -1,
765 .data = &bcm_uart_driver,
766};
767
768static int __init bcm63xx_console_init(void)
769{
770 register_console(&bcm63xx_console);
771 return 0;
772}
773
774console_initcall(bcm63xx_console_init);
775
776#define BCM63XX_CONSOLE (&bcm63xx_console)
777#else
778#define BCM63XX_CONSOLE NULL
779#endif /* CONFIG_SERIAL_BCM63XX_CONSOLE */
780
781static struct uart_driver bcm_uart_driver = {
782 .owner = THIS_MODULE,
783 .driver_name = "bcm63xx_uart",
784 .dev_name = "ttyS",
785 .major = TTY_MAJOR,
786 .minor = 64,
787 .nr = BCM63XX_NR_UARTS,
788 .cons = BCM63XX_CONSOLE,
789};
790
791/*
792 * platform driver probe/remove callback
793 */
794static int __devinit bcm_uart_probe(struct platform_device *pdev)
795{
796 struct resource *res_mem, *res_irq;
797 struct uart_port *port;
798 struct clk *clk;
799 int ret;
800
801 if (pdev->id < 0 || pdev->id >= BCM63XX_NR_UARTS)
802 return -EINVAL;
803
804 if (ports[pdev->id].membase)
805 return -EBUSY;
806
807 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
808 if (!res_mem)
809 return -ENODEV;
810
811 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
812 if (!res_irq)
813 return -ENODEV;
814
815 clk = clk_get(&pdev->dev, "periph");
816 if (IS_ERR(clk))
817 return -ENODEV;
818
819 port = &ports[pdev->id];
820 memset(port, 0, sizeof(*port));
821 port->iotype = UPIO_MEM;
822 port->mapbase = res_mem->start;
823 port->irq = res_irq->start;
824 port->ops = &bcm_uart_ops;
825 port->flags = UPF_BOOT_AUTOCONF;
826 port->dev = &pdev->dev;
827 port->fifosize = 16;
828 port->uartclk = clk_get_rate(clk) / 2;
829 port->line = pdev->id;
830 clk_put(clk);
831
832 ret = uart_add_one_port(&bcm_uart_driver, port);
833 if (ret) {
834 ports[pdev->id].membase = 0;
835 return ret;
836 }
837 platform_set_drvdata(pdev, port);
838 return 0;
839}
840
841static int __devexit bcm_uart_remove(struct platform_device *pdev)
842{
843 struct uart_port *port;
844
845 port = platform_get_drvdata(pdev);
846 uart_remove_one_port(&bcm_uart_driver, port);
847 platform_set_drvdata(pdev, NULL);
848 /* mark port as free */
849 ports[pdev->id].membase = 0;
850 return 0;
851}
852
853/*
854 * platform driver stuff
855 */
856static struct platform_driver bcm_uart_platform_driver = {
857 .probe = bcm_uart_probe,
858 .remove = __devexit_p(bcm_uart_remove),
859 .driver = {
860 .owner = THIS_MODULE,
861 .name = "bcm63xx_uart",
862 },
863};
864
865static int __init bcm_uart_init(void)
866{
867 int ret;
868
869 ret = uart_register_driver(&bcm_uart_driver);
870 if (ret)
871 return ret;
872
873 ret = platform_driver_register(&bcm_uart_platform_driver);
874 if (ret)
875 uart_unregister_driver(&bcm_uart_driver);
876
877 return ret;
878}
879
880static void __exit bcm_uart_exit(void)
881{
882 platform_driver_unregister(&bcm_uart_platform_driver);
883 uart_unregister_driver(&bcm_uart_driver);
884}
885
886module_init(bcm_uart_init);
887module_exit(bcm_uart_exit);
888
889MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
890MODULE_DESCRIPTION("Broadcom 63<xx integrated uart driver");
891MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/bfin_5xx.c b/drivers/tty/serial/bfin_5xx.c
new file mode 100644
index 000000000000..e381b895b04d
--- /dev/null
+++ b/drivers/tty/serial/bfin_5xx.c
@@ -0,0 +1,1600 @@
1/*
2 * Blackfin On-Chip Serial Driver
3 *
4 * Copyright 2006-2010 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11#if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12#define SUPPORT_SYSRQ
13#endif
14
15#define DRIVER_NAME "bfin-uart"
16#define pr_fmt(fmt) DRIVER_NAME ": " fmt
17
18#include <linux/module.h>
19#include <linux/ioport.h>
20#include <linux/gfp.h>
21#include <linux/io.h>
22#include <linux/init.h>
23#include <linux/console.h>
24#include <linux/sysrq.h>
25#include <linux/platform_device.h>
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/serial_core.h>
29#include <linux/gpio.h>
30#include <linux/irq.h>
31#include <linux/kgdb.h>
32#include <linux/slab.h>
33#include <linux/dma-mapping.h>
34
35#include <asm/portmux.h>
36#include <asm/cacheflush.h>
37#include <asm/dma.h>
38
39#define port_membase(uart) (((struct bfin_serial_port *)(uart))->port.membase)
40#define get_lsr_cache(uart) (((struct bfin_serial_port *)(uart))->lsr)
41#define put_lsr_cache(uart, v) (((struct bfin_serial_port *)(uart))->lsr = (v))
42#include <asm/bfin_serial.h>
43
44#ifdef CONFIG_SERIAL_BFIN_MODULE
45# undef CONFIG_EARLY_PRINTK
46#endif
47
48#ifdef CONFIG_SERIAL_BFIN_MODULE
49# undef CONFIG_EARLY_PRINTK
50#endif
51
52/* UART name and device definitions */
53#define BFIN_SERIAL_DEV_NAME "ttyBF"
54#define BFIN_SERIAL_MAJOR 204
55#define BFIN_SERIAL_MINOR 64
56
57static struct bfin_serial_port *bfin_serial_ports[BFIN_UART_NR_PORTS];
58
59#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
60 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
61
62# ifndef CONFIG_SERIAL_BFIN_PIO
63# error KGDB only support UART in PIO mode.
64# endif
65
66static int kgdboc_port_line;
67static int kgdboc_break_enabled;
68#endif
69/*
70 * Setup for console. Argument comes from the menuconfig
71 */
72#define DMA_RX_XCOUNT 512
73#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
74
75#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
76
77#ifdef CONFIG_SERIAL_BFIN_DMA
78static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
79#else
80static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
81#endif
82
83static void bfin_serial_reset_irda(struct uart_port *port);
84
85#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
86 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
87static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
88{
89 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
90 if (uart->cts_pin < 0)
91 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
92
93 /* CTS PIN is negative assertive. */
94 if (UART_GET_CTS(uart))
95 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
96 else
97 return TIOCM_DSR | TIOCM_CAR;
98}
99
100static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
101{
102 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
103 if (uart->rts_pin < 0)
104 return;
105
106 /* RTS PIN is negative assertive. */
107 if (mctrl & TIOCM_RTS)
108 UART_ENABLE_RTS(uart);
109 else
110 UART_DISABLE_RTS(uart);
111}
112
113/*
114 * Handle any change of modem status signal.
115 */
116static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
117{
118 struct bfin_serial_port *uart = dev_id;
119 unsigned int status;
120
121 status = bfin_serial_get_mctrl(&uart->port);
122 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
123#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
124 uart->scts = 1;
125 UART_CLEAR_SCTS(uart);
126 UART_CLEAR_IER(uart, EDSSI);
127#endif
128
129 return IRQ_HANDLED;
130}
131#else
132static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
133{
134 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
135}
136
137static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
138{
139}
140#endif
141
142/*
143 * interrupts are disabled on entry
144 */
145static void bfin_serial_stop_tx(struct uart_port *port)
146{
147 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
148#ifdef CONFIG_SERIAL_BFIN_DMA
149 struct circ_buf *xmit = &uart->port.state->xmit;
150#endif
151
152 while (!(UART_GET_LSR(uart) & TEMT))
153 cpu_relax();
154
155#ifdef CONFIG_SERIAL_BFIN_DMA
156 disable_dma(uart->tx_dma_channel);
157 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
158 uart->port.icount.tx += uart->tx_count;
159 uart->tx_count = 0;
160 uart->tx_done = 1;
161#else
162#ifdef CONFIG_BF54x
163 /* Clear TFI bit */
164 UART_PUT_LSR(uart, TFI);
165#endif
166 UART_CLEAR_IER(uart, ETBEI);
167#endif
168}
169
170/*
171 * port is locked and interrupts are disabled
172 */
173static void bfin_serial_start_tx(struct uart_port *port)
174{
175 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
176 struct tty_struct *tty = uart->port.state->port.tty;
177
178#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
179 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
180 uart->scts = 0;
181 uart_handle_cts_change(&uart->port, uart->scts);
182 }
183#endif
184
185 /*
186 * To avoid losting RX interrupt, we reset IR function
187 * before sending data.
188 */
189 if (tty->termios->c_line == N_IRDA)
190 bfin_serial_reset_irda(port);
191
192#ifdef CONFIG_SERIAL_BFIN_DMA
193 if (uart->tx_done)
194 bfin_serial_dma_tx_chars(uart);
195#else
196 UART_SET_IER(uart, ETBEI);
197 bfin_serial_tx_chars(uart);
198#endif
199}
200
201/*
202 * Interrupts are enabled
203 */
204static void bfin_serial_stop_rx(struct uart_port *port)
205{
206 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
207
208 UART_CLEAR_IER(uart, ERBFI);
209}
210
211/*
212 * Set the modem control timer to fire immediately.
213 */
214static void bfin_serial_enable_ms(struct uart_port *port)
215{
216}
217
218
219#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
220# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
221# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
222#else
223# define UART_GET_ANOMALY_THRESHOLD(uart) 0
224# define UART_SET_ANOMALY_THRESHOLD(uart, v)
225#endif
226
227#ifdef CONFIG_SERIAL_BFIN_PIO
228static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
229{
230 struct tty_struct *tty = NULL;
231 unsigned int status, ch, flg;
232 static struct timeval anomaly_start = { .tv_sec = 0 };
233
234 status = UART_GET_LSR(uart);
235 UART_CLEAR_LSR(uart);
236
237 ch = UART_GET_CHAR(uart);
238 uart->port.icount.rx++;
239
240#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
241 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
242 if (kgdb_connected && kgdboc_port_line == uart->port.line
243 && kgdboc_break_enabled)
244 if (ch == 0x3) {/* Ctrl + C */
245 kgdb_breakpoint();
246 return;
247 }
248
249 if (!uart->port.state || !uart->port.state->port.tty)
250 return;
251#endif
252 tty = uart->port.state->port.tty;
253
254 if (ANOMALY_05000363) {
255 /* The BF533 (and BF561) family of processors have a nice anomaly
256 * where they continuously generate characters for a "single" break.
257 * We have to basically ignore this flood until the "next" valid
258 * character comes across. Due to the nature of the flood, it is
259 * not possible to reliably catch bytes that are sent too quickly
260 * after this break. So application code talking to the Blackfin
261 * which sends a break signal must allow at least 1.5 character
262 * times after the end of the break for things to stabilize. This
263 * timeout was picked as it must absolutely be larger than 1
264 * character time +/- some percent. So 1.5 sounds good. All other
265 * Blackfin families operate properly. Woo.
266 */
267 if (anomaly_start.tv_sec) {
268 struct timeval curr;
269 suseconds_t usecs;
270
271 if ((~ch & (~ch + 1)) & 0xff)
272 goto known_good_char;
273
274 do_gettimeofday(&curr);
275 if (curr.tv_sec - anomaly_start.tv_sec > 1)
276 goto known_good_char;
277
278 usecs = 0;
279 if (curr.tv_sec != anomaly_start.tv_sec)
280 usecs += USEC_PER_SEC;
281 usecs += curr.tv_usec - anomaly_start.tv_usec;
282
283 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
284 goto known_good_char;
285
286 if (ch)
287 anomaly_start.tv_sec = 0;
288 else
289 anomaly_start = curr;
290
291 return;
292
293 known_good_char:
294 status &= ~BI;
295 anomaly_start.tv_sec = 0;
296 }
297 }
298
299 if (status & BI) {
300 if (ANOMALY_05000363)
301 if (bfin_revid() < 5)
302 do_gettimeofday(&anomaly_start);
303 uart->port.icount.brk++;
304 if (uart_handle_break(&uart->port))
305 goto ignore_char;
306 status &= ~(PE | FE);
307 }
308 if (status & PE)
309 uart->port.icount.parity++;
310 if (status & OE)
311 uart->port.icount.overrun++;
312 if (status & FE)
313 uart->port.icount.frame++;
314
315 status &= uart->port.read_status_mask;
316
317 if (status & BI)
318 flg = TTY_BREAK;
319 else if (status & PE)
320 flg = TTY_PARITY;
321 else if (status & FE)
322 flg = TTY_FRAME;
323 else
324 flg = TTY_NORMAL;
325
326 if (uart_handle_sysrq_char(&uart->port, ch))
327 goto ignore_char;
328
329 uart_insert_char(&uart->port, status, OE, ch, flg);
330
331 ignore_char:
332 tty_flip_buffer_push(tty);
333}
334
335static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
336{
337 struct circ_buf *xmit = &uart->port.state->xmit;
338
339 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
340#ifdef CONFIG_BF54x
341 /* Clear TFI bit */
342 UART_PUT_LSR(uart, TFI);
343#endif
344 /* Anomaly notes:
345 * 05000215 - we always clear ETBEI within last UART TX
346 * interrupt to end a string. It is always set
347 * when start a new tx.
348 */
349 UART_CLEAR_IER(uart, ETBEI);
350 return;
351 }
352
353 if (uart->port.x_char) {
354 UART_PUT_CHAR(uart, uart->port.x_char);
355 uart->port.icount.tx++;
356 uart->port.x_char = 0;
357 }
358
359 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
360 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
361 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
362 uart->port.icount.tx++;
363 }
364
365 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
366 uart_write_wakeup(&uart->port);
367}
368
369static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
370{
371 struct bfin_serial_port *uart = dev_id;
372
373 spin_lock(&uart->port.lock);
374 while (UART_GET_LSR(uart) & DR)
375 bfin_serial_rx_chars(uart);
376 spin_unlock(&uart->port.lock);
377
378 return IRQ_HANDLED;
379}
380
381static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
382{
383 struct bfin_serial_port *uart = dev_id;
384
385#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
386 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
387 uart->scts = 0;
388 uart_handle_cts_change(&uart->port, uart->scts);
389 }
390#endif
391 spin_lock(&uart->port.lock);
392 if (UART_GET_LSR(uart) & THRE)
393 bfin_serial_tx_chars(uart);
394 spin_unlock(&uart->port.lock);
395
396 return IRQ_HANDLED;
397}
398#endif
399
400#ifdef CONFIG_SERIAL_BFIN_DMA
401static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
402{
403 struct circ_buf *xmit = &uart->port.state->xmit;
404
405 uart->tx_done = 0;
406
407 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
408 uart->tx_count = 0;
409 uart->tx_done = 1;
410 return;
411 }
412
413 if (uart->port.x_char) {
414 UART_PUT_CHAR(uart, uart->port.x_char);
415 uart->port.icount.tx++;
416 uart->port.x_char = 0;
417 }
418
419 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
420 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
421 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
422 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
423 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
424 set_dma_config(uart->tx_dma_channel,
425 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
426 INTR_ON_BUF,
427 DIMENSION_LINEAR,
428 DATA_SIZE_8,
429 DMA_SYNC_RESTART));
430 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
431 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
432 set_dma_x_modify(uart->tx_dma_channel, 1);
433 SSYNC();
434 enable_dma(uart->tx_dma_channel);
435
436 UART_SET_IER(uart, ETBEI);
437}
438
439static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
440{
441 struct tty_struct *tty = uart->port.state->port.tty;
442 int i, flg, status;
443
444 status = UART_GET_LSR(uart);
445 UART_CLEAR_LSR(uart);
446
447 uart->port.icount.rx +=
448 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
449 UART_XMIT_SIZE);
450
451 if (status & BI) {
452 uart->port.icount.brk++;
453 if (uart_handle_break(&uart->port))
454 goto dma_ignore_char;
455 status &= ~(PE | FE);
456 }
457 if (status & PE)
458 uart->port.icount.parity++;
459 if (status & OE)
460 uart->port.icount.overrun++;
461 if (status & FE)
462 uart->port.icount.frame++;
463
464 status &= uart->port.read_status_mask;
465
466 if (status & BI)
467 flg = TTY_BREAK;
468 else if (status & PE)
469 flg = TTY_PARITY;
470 else if (status & FE)
471 flg = TTY_FRAME;
472 else
473 flg = TTY_NORMAL;
474
475 for (i = uart->rx_dma_buf.tail; ; i++) {
476 if (i >= UART_XMIT_SIZE)
477 i = 0;
478 if (i == uart->rx_dma_buf.head)
479 break;
480 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
481 uart_insert_char(&uart->port, status, OE,
482 uart->rx_dma_buf.buf[i], flg);
483 }
484
485 dma_ignore_char:
486 tty_flip_buffer_push(tty);
487}
488
489void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
490{
491 int x_pos, pos;
492
493 dma_disable_irq(uart->tx_dma_channel);
494 dma_disable_irq(uart->rx_dma_channel);
495 spin_lock_bh(&uart->port.lock);
496
497 /* 2D DMA RX buffer ring is used. Because curr_y_count and
498 * curr_x_count can't be read as an atomic operation,
499 * curr_y_count should be read before curr_x_count. When
500 * curr_x_count is read, curr_y_count may already indicate
501 * next buffer line. But, the position calculated here is
502 * still indicate the old line. The wrong position data may
503 * be smaller than current buffer tail, which cause garbages
504 * are received if it is not prohibit.
505 */
506 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
507 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
508 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
509 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
510 uart->rx_dma_nrows = 0;
511 x_pos = DMA_RX_XCOUNT - x_pos;
512 if (x_pos == DMA_RX_XCOUNT)
513 x_pos = 0;
514
515 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
516 /* Ignore receiving data if new position is in the same line of
517 * current buffer tail and small.
518 */
519 if (pos > uart->rx_dma_buf.tail ||
520 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
521 uart->rx_dma_buf.head = pos;
522 bfin_serial_dma_rx_chars(uart);
523 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
524 }
525
526 spin_unlock_bh(&uart->port.lock);
527 dma_enable_irq(uart->tx_dma_channel);
528 dma_enable_irq(uart->rx_dma_channel);
529
530 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
531}
532
533static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
534{
535 struct bfin_serial_port *uart = dev_id;
536 struct circ_buf *xmit = &uart->port.state->xmit;
537
538#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
539 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port)&TIOCM_CTS)) {
540 uart->scts = 0;
541 uart_handle_cts_change(&uart->port, uart->scts);
542 }
543#endif
544
545 spin_lock(&uart->port.lock);
546 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
547 disable_dma(uart->tx_dma_channel);
548 clear_dma_irqstat(uart->tx_dma_channel);
549 /* Anomaly notes:
550 * 05000215 - we always clear ETBEI within last UART TX
551 * interrupt to end a string. It is always set
552 * when start a new tx.
553 */
554 UART_CLEAR_IER(uart, ETBEI);
555 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
556 uart->port.icount.tx += uart->tx_count;
557
558 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
559 uart_write_wakeup(&uart->port);
560
561 bfin_serial_dma_tx_chars(uart);
562 }
563
564 spin_unlock(&uart->port.lock);
565 return IRQ_HANDLED;
566}
567
568static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
569{
570 struct bfin_serial_port *uart = dev_id;
571 unsigned short irqstat;
572 int x_pos, pos;
573
574 spin_lock(&uart->port.lock);
575 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
576 clear_dma_irqstat(uart->rx_dma_channel);
577
578 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
579 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
580 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
581 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
582 uart->rx_dma_nrows = 0;
583
584 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
585 if (pos > uart->rx_dma_buf.tail ||
586 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
587 uart->rx_dma_buf.head = pos;
588 bfin_serial_dma_rx_chars(uart);
589 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
590 }
591
592 spin_unlock(&uart->port.lock);
593
594 return IRQ_HANDLED;
595}
596#endif
597
598/*
599 * Return TIOCSER_TEMT when transmitter is not busy.
600 */
601static unsigned int bfin_serial_tx_empty(struct uart_port *port)
602{
603 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
604 unsigned short lsr;
605
606 lsr = UART_GET_LSR(uart);
607 if (lsr & TEMT)
608 return TIOCSER_TEMT;
609 else
610 return 0;
611}
612
613static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
614{
615 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
616 u16 lcr = UART_GET_LCR(uart);
617 if (break_state)
618 lcr |= SB;
619 else
620 lcr &= ~SB;
621 UART_PUT_LCR(uart, lcr);
622 SSYNC();
623}
624
625static int bfin_serial_startup(struct uart_port *port)
626{
627 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
628
629#ifdef CONFIG_SERIAL_BFIN_DMA
630 dma_addr_t dma_handle;
631
632 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
633 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
634 return -EBUSY;
635 }
636
637 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
638 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
639 free_dma(uart->rx_dma_channel);
640 return -EBUSY;
641 }
642
643 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
644 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
645
646 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
647 uart->rx_dma_buf.head = 0;
648 uart->rx_dma_buf.tail = 0;
649 uart->rx_dma_nrows = 0;
650
651 set_dma_config(uart->rx_dma_channel,
652 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
653 INTR_ON_ROW, DIMENSION_2D,
654 DATA_SIZE_8,
655 DMA_SYNC_RESTART));
656 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
657 set_dma_x_modify(uart->rx_dma_channel, 1);
658 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
659 set_dma_y_modify(uart->rx_dma_channel, 1);
660 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
661 enable_dma(uart->rx_dma_channel);
662
663 uart->rx_dma_timer.data = (unsigned long)(uart);
664 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
665 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
666 add_timer(&(uart->rx_dma_timer));
667#else
668# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
669 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
670 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
671 kgdboc_break_enabled = 0;
672 else {
673# endif
674 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
675 "BFIN_UART_RX", uart)) {
676 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
677 return -EBUSY;
678 }
679
680 if (request_irq
681 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
682 "BFIN_UART_TX", uart)) {
683 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
684 free_irq(uart->port.irq, uart);
685 return -EBUSY;
686 }
687
688# ifdef CONFIG_BF54x
689 {
690 /*
691 * UART2 and UART3 on BF548 share interrupt PINs and DMA
692 * controllers with SPORT2 and SPORT3. UART rx and tx
693 * interrupts are generated in PIO mode only when configure
694 * their peripheral mapping registers properly, which means
695 * request corresponding DMA channels in PIO mode as well.
696 */
697 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
698
699 switch (uart->port.irq) {
700 case IRQ_UART3_RX:
701 uart_dma_ch_rx = CH_UART3_RX;
702 uart_dma_ch_tx = CH_UART3_TX;
703 break;
704 case IRQ_UART2_RX:
705 uart_dma_ch_rx = CH_UART2_RX;
706 uart_dma_ch_tx = CH_UART2_TX;
707 break;
708 default:
709 uart_dma_ch_rx = uart_dma_ch_tx = 0;
710 break;
711 };
712
713 if (uart_dma_ch_rx &&
714 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
715 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
716 free_irq(uart->port.irq, uart);
717 free_irq(uart->port.irq + 1, uart);
718 return -EBUSY;
719 }
720 if (uart_dma_ch_tx &&
721 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
722 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
723 free_dma(uart_dma_ch_rx);
724 free_irq(uart->port.irq, uart);
725 free_irq(uart->port.irq + 1, uart);
726 return -EBUSY;
727 }
728 }
729# endif
730# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
731 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
732 }
733# endif
734#endif
735
736#ifdef CONFIG_SERIAL_BFIN_CTSRTS
737 if (uart->cts_pin >= 0) {
738 if (request_irq(gpio_to_irq(uart->cts_pin),
739 bfin_serial_mctrl_cts_int,
740 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
741 IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
742 uart->cts_pin = -1;
743 pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
744 }
745 }
746 if (uart->rts_pin >= 0) {
747 gpio_direction_output(uart->rts_pin, 0);
748 }
749#endif
750#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
751 if (uart->cts_pin >= 0 && request_irq(uart->status_irq,
752 bfin_serial_mctrl_cts_int,
753 IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
754 uart->cts_pin = -1;
755 pr_info("Unable to attach BlackFin UART Modem Status interrupt.\n");
756 }
757
758 /* CTS RTS PINs are negative assertive. */
759 UART_PUT_MCR(uart, ACTS);
760 UART_SET_IER(uart, EDSSI);
761#endif
762
763 UART_SET_IER(uart, ERBFI);
764 return 0;
765}
766
767static void bfin_serial_shutdown(struct uart_port *port)
768{
769 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
770
771#ifdef CONFIG_SERIAL_BFIN_DMA
772 disable_dma(uart->tx_dma_channel);
773 free_dma(uart->tx_dma_channel);
774 disable_dma(uart->rx_dma_channel);
775 free_dma(uart->rx_dma_channel);
776 del_timer(&(uart->rx_dma_timer));
777 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
778#else
779#ifdef CONFIG_BF54x
780 switch (uart->port.irq) {
781 case IRQ_UART3_RX:
782 free_dma(CH_UART3_RX);
783 free_dma(CH_UART3_TX);
784 break;
785 case IRQ_UART2_RX:
786 free_dma(CH_UART2_RX);
787 free_dma(CH_UART2_TX);
788 break;
789 default:
790 break;
791 };
792#endif
793 free_irq(uart->port.irq, uart);
794 free_irq(uart->port.irq+1, uart);
795#endif
796
797#ifdef CONFIG_SERIAL_BFIN_CTSRTS
798 if (uart->cts_pin >= 0)
799 free_irq(gpio_to_irq(uart->cts_pin), uart);
800#endif
801#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
802 if (uart->cts_pin >= 0)
803 free_irq(uart->status_irq, uart);
804#endif
805}
806
807static void
808bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
809 struct ktermios *old)
810{
811 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
812 unsigned long flags;
813 unsigned int baud, quot;
814 unsigned short val, ier, lcr = 0;
815
816 switch (termios->c_cflag & CSIZE) {
817 case CS8:
818 lcr = WLS(8);
819 break;
820 case CS7:
821 lcr = WLS(7);
822 break;
823 case CS6:
824 lcr = WLS(6);
825 break;
826 case CS5:
827 lcr = WLS(5);
828 break;
829 default:
830 printk(KERN_ERR "%s: word lengh not supported\n",
831 __func__);
832 }
833
834 /* Anomaly notes:
835 * 05000231 - STOP bit is always set to 1 whatever the user is set.
836 */
837 if (termios->c_cflag & CSTOPB) {
838 if (ANOMALY_05000231)
839 printk(KERN_WARNING "STOP bits other than 1 is not "
840 "supported in case of anomaly 05000231.\n");
841 else
842 lcr |= STB;
843 }
844 if (termios->c_cflag & PARENB)
845 lcr |= PEN;
846 if (!(termios->c_cflag & PARODD))
847 lcr |= EPS;
848 if (termios->c_cflag & CMSPAR)
849 lcr |= STP;
850
851 spin_lock_irqsave(&uart->port.lock, flags);
852
853 port->read_status_mask = OE;
854 if (termios->c_iflag & INPCK)
855 port->read_status_mask |= (FE | PE);
856 if (termios->c_iflag & (BRKINT | PARMRK))
857 port->read_status_mask |= BI;
858
859 /*
860 * Characters to ignore
861 */
862 port->ignore_status_mask = 0;
863 if (termios->c_iflag & IGNPAR)
864 port->ignore_status_mask |= FE | PE;
865 if (termios->c_iflag & IGNBRK) {
866 port->ignore_status_mask |= BI;
867 /*
868 * If we're ignoring parity and break indicators,
869 * ignore overruns too (for real raw support).
870 */
871 if (termios->c_iflag & IGNPAR)
872 port->ignore_status_mask |= OE;
873 }
874
875 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
876 quot = uart_get_divisor(port, baud);
877
878 /* If discipline is not IRDA, apply ANOMALY_05000230 */
879 if (termios->c_line != N_IRDA)
880 quot -= ANOMALY_05000230;
881
882 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
883
884 /* Disable UART */
885 ier = UART_GET_IER(uart);
886 UART_DISABLE_INTS(uart);
887
888 /* Set DLAB in LCR to Access DLL and DLH */
889 UART_SET_DLAB(uart);
890
891 UART_PUT_DLL(uart, quot & 0xFF);
892 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
893 SSYNC();
894
895 /* Clear DLAB in LCR to Access THR RBR IER */
896 UART_CLEAR_DLAB(uart);
897
898 UART_PUT_LCR(uart, lcr);
899
900 /* Enable UART */
901 UART_ENABLE_INTS(uart, ier);
902
903 val = UART_GET_GCTL(uart);
904 val |= UCEN;
905 UART_PUT_GCTL(uart, val);
906
907 /* Port speed changed, update the per-port timeout. */
908 uart_update_timeout(port, termios->c_cflag, baud);
909
910 spin_unlock_irqrestore(&uart->port.lock, flags);
911}
912
913static const char *bfin_serial_type(struct uart_port *port)
914{
915 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
916
917 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
918}
919
920/*
921 * Release the memory region(s) being used by 'port'.
922 */
923static void bfin_serial_release_port(struct uart_port *port)
924{
925}
926
927/*
928 * Request the memory region(s) being used by 'port'.
929 */
930static int bfin_serial_request_port(struct uart_port *port)
931{
932 return 0;
933}
934
935/*
936 * Configure/autoconfigure the port.
937 */
938static void bfin_serial_config_port(struct uart_port *port, int flags)
939{
940 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
941
942 if (flags & UART_CONFIG_TYPE &&
943 bfin_serial_request_port(&uart->port) == 0)
944 uart->port.type = PORT_BFIN;
945}
946
947/*
948 * Verify the new serial_struct (for TIOCSSERIAL).
949 * The only change we allow are to the flags and type, and
950 * even then only between PORT_BFIN and PORT_UNKNOWN
951 */
952static int
953bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
954{
955 return 0;
956}
957
958/*
959 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
960 * In other cases, disable IrDA function.
961 */
962static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
963{
964 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
965 unsigned short val;
966
967 switch (ld) {
968 case N_IRDA:
969 val = UART_GET_GCTL(uart);
970 val |= (IREN | RPOLC);
971 UART_PUT_GCTL(uart, val);
972 break;
973 default:
974 val = UART_GET_GCTL(uart);
975 val &= ~(IREN | RPOLC);
976 UART_PUT_GCTL(uart, val);
977 }
978}
979
980static void bfin_serial_reset_irda(struct uart_port *port)
981{
982 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
983 unsigned short val;
984
985 val = UART_GET_GCTL(uart);
986 val &= ~(IREN | RPOLC);
987 UART_PUT_GCTL(uart, val);
988 SSYNC();
989 val |= (IREN | RPOLC);
990 UART_PUT_GCTL(uart, val);
991 SSYNC();
992}
993
994#ifdef CONFIG_CONSOLE_POLL
995/* Anomaly notes:
996 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
997 * losing other bits of UART_LSR is not a problem here.
998 */
999static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
1000{
1001 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1002
1003 while (!(UART_GET_LSR(uart) & THRE))
1004 cpu_relax();
1005
1006 UART_CLEAR_DLAB(uart);
1007 UART_PUT_CHAR(uart, (unsigned char)chr);
1008}
1009
1010static int bfin_serial_poll_get_char(struct uart_port *port)
1011{
1012 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1013 unsigned char chr;
1014
1015 while (!(UART_GET_LSR(uart) & DR))
1016 cpu_relax();
1017
1018 UART_CLEAR_DLAB(uart);
1019 chr = UART_GET_CHAR(uart);
1020
1021 return chr;
1022}
1023#endif
1024
1025#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1026 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1027static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1028{
1029 if (kgdboc_break_enabled) {
1030 kgdboc_break_enabled = 0;
1031 bfin_serial_shutdown(port);
1032 }
1033}
1034
1035static int bfin_kgdboc_port_startup(struct uart_port *port)
1036{
1037 kgdboc_port_line = port->line;
1038 kgdboc_break_enabled = !bfin_serial_startup(port);
1039 return 0;
1040}
1041#endif
1042
1043static struct uart_ops bfin_serial_pops = {
1044 .tx_empty = bfin_serial_tx_empty,
1045 .set_mctrl = bfin_serial_set_mctrl,
1046 .get_mctrl = bfin_serial_get_mctrl,
1047 .stop_tx = bfin_serial_stop_tx,
1048 .start_tx = bfin_serial_start_tx,
1049 .stop_rx = bfin_serial_stop_rx,
1050 .enable_ms = bfin_serial_enable_ms,
1051 .break_ctl = bfin_serial_break_ctl,
1052 .startup = bfin_serial_startup,
1053 .shutdown = bfin_serial_shutdown,
1054 .set_termios = bfin_serial_set_termios,
1055 .set_ldisc = bfin_serial_set_ldisc,
1056 .type = bfin_serial_type,
1057 .release_port = bfin_serial_release_port,
1058 .request_port = bfin_serial_request_port,
1059 .config_port = bfin_serial_config_port,
1060 .verify_port = bfin_serial_verify_port,
1061#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1062 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1063 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1064 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1065#endif
1066#ifdef CONFIG_CONSOLE_POLL
1067 .poll_put_char = bfin_serial_poll_put_char,
1068 .poll_get_char = bfin_serial_poll_get_char,
1069#endif
1070};
1071
1072#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1073/*
1074 * If the port was already initialised (eg, by a boot loader),
1075 * try to determine the current setup.
1076 */
1077static void __init
1078bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1079 int *parity, int *bits)
1080{
1081 unsigned short status;
1082
1083 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1084 if (status == (ERBFI | ETBEI)) {
1085 /* ok, the port was enabled */
1086 u16 lcr, dlh, dll;
1087
1088 lcr = UART_GET_LCR(uart);
1089
1090 *parity = 'n';
1091 if (lcr & PEN) {
1092 if (lcr & EPS)
1093 *parity = 'e';
1094 else
1095 *parity = 'o';
1096 }
1097 switch (lcr & 0x03) {
1098 case 0: *bits = 5; break;
1099 case 1: *bits = 6; break;
1100 case 2: *bits = 7; break;
1101 case 3: *bits = 8; break;
1102 }
1103 /* Set DLAB in LCR to Access DLL and DLH */
1104 UART_SET_DLAB(uart);
1105
1106 dll = UART_GET_DLL(uart);
1107 dlh = UART_GET_DLH(uart);
1108
1109 /* Clear DLAB in LCR to Access THR RBR IER */
1110 UART_CLEAR_DLAB(uart);
1111
1112 *baud = get_sclk() / (16*(dll | dlh << 8));
1113 }
1114 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1115}
1116
1117static struct uart_driver bfin_serial_reg;
1118
1119static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1120{
1121 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1122 while (!(UART_GET_LSR(uart) & THRE))
1123 barrier();
1124 UART_PUT_CHAR(uart, ch);
1125}
1126
1127#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1128 defined (CONFIG_EARLY_PRINTK) */
1129
1130#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1131#define CLASS_BFIN_CONSOLE "bfin-console"
1132/*
1133 * Interrupts are disabled on entering
1134 */
1135static void
1136bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1137{
1138 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1139 unsigned long flags;
1140
1141 spin_lock_irqsave(&uart->port.lock, flags);
1142 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1143 spin_unlock_irqrestore(&uart->port.lock, flags);
1144
1145}
1146
1147static int __init
1148bfin_serial_console_setup(struct console *co, char *options)
1149{
1150 struct bfin_serial_port *uart;
1151 int baud = 57600;
1152 int bits = 8;
1153 int parity = 'n';
1154# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1155 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1156 int flow = 'r';
1157# else
1158 int flow = 'n';
1159# endif
1160
1161 /*
1162 * Check whether an invalid uart number has been specified, and
1163 * if so, search for the first available port that does have
1164 * console support.
1165 */
1166 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1167 return -ENODEV;
1168
1169 uart = bfin_serial_ports[co->index];
1170 if (!uart)
1171 return -ENODEV;
1172
1173 if (options)
1174 uart_parse_options(options, &baud, &parity, &bits, &flow);
1175 else
1176 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1177
1178 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1179}
1180
1181static struct console bfin_serial_console = {
1182 .name = BFIN_SERIAL_DEV_NAME,
1183 .write = bfin_serial_console_write,
1184 .device = uart_console_device,
1185 .setup = bfin_serial_console_setup,
1186 .flags = CON_PRINTBUFFER,
1187 .index = -1,
1188 .data = &bfin_serial_reg,
1189};
1190#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1191#else
1192#define BFIN_SERIAL_CONSOLE NULL
1193#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1194
1195#ifdef CONFIG_EARLY_PRINTK
1196static struct bfin_serial_port bfin_earlyprintk_port;
1197#define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
1198
1199/*
1200 * Interrupts are disabled on entering
1201 */
1202static void
1203bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
1204{
1205 unsigned long flags;
1206
1207 if (bfin_earlyprintk_port.port.line != co->index)
1208 return;
1209
1210 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1211 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1212 bfin_serial_console_putchar);
1213 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
1214}
1215
1216/*
1217 * This should have a .setup or .early_setup in it, but then things get called
1218 * without the command line options, and the baud rate gets messed up - so
1219 * don't let the common infrastructure play with things. (see calls to setup
1220 * & earlysetup in ./kernel/printk.c:register_console()
1221 */
1222static struct __initdata console bfin_early_serial_console = {
1223 .name = "early_BFuart",
1224 .write = bfin_earlyprintk_console_write,
1225 .device = uart_console_device,
1226 .flags = CON_PRINTBUFFER,
1227 .index = -1,
1228 .data = &bfin_serial_reg,
1229};
1230#endif
1231
1232static struct uart_driver bfin_serial_reg = {
1233 .owner = THIS_MODULE,
1234 .driver_name = DRIVER_NAME,
1235 .dev_name = BFIN_SERIAL_DEV_NAME,
1236 .major = BFIN_SERIAL_MAJOR,
1237 .minor = BFIN_SERIAL_MINOR,
1238 .nr = BFIN_UART_NR_PORTS,
1239 .cons = BFIN_SERIAL_CONSOLE,
1240};
1241
1242static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1243{
1244 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1245
1246 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1247}
1248
1249static int bfin_serial_resume(struct platform_device *pdev)
1250{
1251 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1252
1253 return uart_resume_port(&bfin_serial_reg, &uart->port);
1254}
1255
1256static int bfin_serial_probe(struct platform_device *pdev)
1257{
1258 struct resource *res;
1259 struct bfin_serial_port *uart = NULL;
1260 int ret = 0;
1261
1262 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1263 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1264 return -ENOENT;
1265 }
1266
1267 if (bfin_serial_ports[pdev->id] == NULL) {
1268
1269 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1270 if (!uart) {
1271 dev_err(&pdev->dev,
1272 "fail to malloc bfin_serial_port\n");
1273 return -ENOMEM;
1274 }
1275 bfin_serial_ports[pdev->id] = uart;
1276
1277#ifdef CONFIG_EARLY_PRINTK
1278 if (!(bfin_earlyprintk_port.port.membase
1279 && bfin_earlyprintk_port.port.line == pdev->id)) {
1280 /*
1281 * If the peripheral PINs of current port is allocated
1282 * in earlyprintk probe stage, don't do it again.
1283 */
1284#endif
1285 ret = peripheral_request_list(
1286 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1287 if (ret) {
1288 dev_err(&pdev->dev,
1289 "fail to request bfin serial peripherals\n");
1290 goto out_error_free_mem;
1291 }
1292#ifdef CONFIG_EARLY_PRINTK
1293 }
1294#endif
1295
1296 spin_lock_init(&uart->port.lock);
1297 uart->port.uartclk = get_sclk();
1298 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1299 uart->port.ops = &bfin_serial_pops;
1300 uart->port.line = pdev->id;
1301 uart->port.iotype = UPIO_MEM;
1302 uart->port.flags = UPF_BOOT_AUTOCONF;
1303
1304 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1305 if (res == NULL) {
1306 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1307 ret = -ENOENT;
1308 goto out_error_free_peripherals;
1309 }
1310
1311 uart->port.membase = ioremap(res->start,
1312 res->end - res->start);
1313 if (!uart->port.membase) {
1314 dev_err(&pdev->dev, "Cannot map uart IO\n");
1315 ret = -ENXIO;
1316 goto out_error_free_peripherals;
1317 }
1318 uart->port.mapbase = res->start;
1319
1320 uart->port.irq = platform_get_irq(pdev, 0);
1321 if (uart->port.irq < 0) {
1322 dev_err(&pdev->dev, "No uart RX/TX IRQ specified\n");
1323 ret = -ENOENT;
1324 goto out_error_unmap;
1325 }
1326
1327 uart->status_irq = platform_get_irq(pdev, 1);
1328 if (uart->status_irq < 0) {
1329 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1330 ret = -ENOENT;
1331 goto out_error_unmap;
1332 }
1333
1334#ifdef CONFIG_SERIAL_BFIN_DMA
1335 uart->tx_done = 1;
1336 uart->tx_count = 0;
1337
1338 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1339 if (res == NULL) {
1340 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1341 ret = -ENOENT;
1342 goto out_error_unmap;
1343 }
1344 uart->tx_dma_channel = res->start;
1345
1346 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1347 if (res == NULL) {
1348 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1349 ret = -ENOENT;
1350 goto out_error_unmap;
1351 }
1352 uart->rx_dma_channel = res->start;
1353
1354 init_timer(&(uart->rx_dma_timer));
1355#endif
1356
1357#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1358 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1359 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1360 if (res == NULL)
1361 uart->cts_pin = -1;
1362 else
1363 uart->cts_pin = res->start;
1364
1365 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1366 if (res == NULL)
1367 uart->rts_pin = -1;
1368 else
1369 uart->rts_pin = res->start;
1370# if defined(CONFIG_SERIAL_BFIN_CTSRTS)
1371 if (uart->rts_pin >= 0)
1372 gpio_request(uart->rts_pin, DRIVER_NAME);
1373# endif
1374#endif
1375 }
1376
1377#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1378 if (!is_early_platform_device(pdev)) {
1379#endif
1380 uart = bfin_serial_ports[pdev->id];
1381 uart->port.dev = &pdev->dev;
1382 dev_set_drvdata(&pdev->dev, uart);
1383 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1384#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1385 }
1386#endif
1387
1388 if (!ret)
1389 return 0;
1390
1391 if (uart) {
1392out_error_unmap:
1393 iounmap(uart->port.membase);
1394out_error_free_peripherals:
1395 peripheral_free_list(
1396 (unsigned short *)pdev->dev.platform_data);
1397out_error_free_mem:
1398 kfree(uart);
1399 bfin_serial_ports[pdev->id] = NULL;
1400 }
1401
1402 return ret;
1403}
1404
1405static int __devexit bfin_serial_remove(struct platform_device *pdev)
1406{
1407 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1408
1409 dev_set_drvdata(&pdev->dev, NULL);
1410
1411 if (uart) {
1412 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1413#ifdef CONFIG_SERIAL_BFIN_CTSRTS
1414 if (uart->rts_pin >= 0)
1415 gpio_free(uart->rts_pin);
1416#endif
1417 iounmap(uart->port.membase);
1418 peripheral_free_list(
1419 (unsigned short *)pdev->dev.platform_data);
1420 kfree(uart);
1421 bfin_serial_ports[pdev->id] = NULL;
1422 }
1423
1424 return 0;
1425}
1426
1427static struct platform_driver bfin_serial_driver = {
1428 .probe = bfin_serial_probe,
1429 .remove = __devexit_p(bfin_serial_remove),
1430 .suspend = bfin_serial_suspend,
1431 .resume = bfin_serial_resume,
1432 .driver = {
1433 .name = DRIVER_NAME,
1434 .owner = THIS_MODULE,
1435 },
1436};
1437
1438#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1439static __initdata struct early_platform_driver early_bfin_serial_driver = {
1440 .class_str = CLASS_BFIN_CONSOLE,
1441 .pdrv = &bfin_serial_driver,
1442 .requested_id = EARLY_PLATFORM_ID_UNSET,
1443};
1444
1445static int __init bfin_serial_rs_console_init(void)
1446{
1447 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1448
1449 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1450
1451 register_console(&bfin_serial_console);
1452
1453 return 0;
1454}
1455console_initcall(bfin_serial_rs_console_init);
1456#endif
1457
1458#ifdef CONFIG_EARLY_PRINTK
1459/*
1460 * Memory can't be allocated dynamically during earlyprink init stage.
1461 * So, do individual probe for earlyprink with a static uart port variable.
1462 */
1463static int bfin_earlyprintk_probe(struct platform_device *pdev)
1464{
1465 struct resource *res;
1466 int ret;
1467
1468 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1469 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1470 return -ENOENT;
1471 }
1472
1473 ret = peripheral_request_list(
1474 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1475 if (ret) {
1476 dev_err(&pdev->dev,
1477 "fail to request bfin serial peripherals\n");
1478 return ret;
1479 }
1480
1481 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1482 if (res == NULL) {
1483 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1484 ret = -ENOENT;
1485 goto out_error_free_peripherals;
1486 }
1487
1488 bfin_earlyprintk_port.port.membase = ioremap(res->start,
1489 res->end - res->start);
1490 if (!bfin_earlyprintk_port.port.membase) {
1491 dev_err(&pdev->dev, "Cannot map uart IO\n");
1492 ret = -ENXIO;
1493 goto out_error_free_peripherals;
1494 }
1495 bfin_earlyprintk_port.port.mapbase = res->start;
1496 bfin_earlyprintk_port.port.line = pdev->id;
1497 bfin_earlyprintk_port.port.uartclk = get_sclk();
1498 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1499 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1500
1501 return 0;
1502
1503out_error_free_peripherals:
1504 peripheral_free_list(
1505 (unsigned short *)pdev->dev.platform_data);
1506
1507 return ret;
1508}
1509
1510static struct platform_driver bfin_earlyprintk_driver = {
1511 .probe = bfin_earlyprintk_probe,
1512 .driver = {
1513 .name = DRIVER_NAME,
1514 .owner = THIS_MODULE,
1515 },
1516};
1517
1518static __initdata struct early_platform_driver early_bfin_earlyprintk_driver = {
1519 .class_str = CLASS_BFIN_EARLYPRINTK,
1520 .pdrv = &bfin_earlyprintk_driver,
1521 .requested_id = EARLY_PLATFORM_ID_UNSET,
1522};
1523
1524struct console __init *bfin_earlyserial_init(unsigned int port,
1525 unsigned int cflag)
1526{
1527 struct ktermios t;
1528 char port_name[20];
1529
1530 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1531 return NULL;
1532
1533 /*
1534 * Only probe resource of the given port in earlyprintk boot arg.
1535 * The expected port id should be indicated in port name string.
1536 */
1537 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1538 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1539 port_name);
1540 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1541
1542 if (!bfin_earlyprintk_port.port.membase)
1543 return NULL;
1544
1545#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1546 /*
1547 * If we are using early serial, don't let the normal console rewind
1548 * log buffer, since that causes things to be printed multiple times
1549 */
1550 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1551#endif
1552
1553 bfin_early_serial_console.index = port;
1554 t.c_cflag = cflag;
1555 t.c_iflag = 0;
1556 t.c_oflag = 0;
1557 t.c_lflag = ICANON;
1558 t.c_line = port;
1559 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1560
1561 return &bfin_early_serial_console;
1562}
1563#endif /* CONFIG_EARLY_PRINTK */
1564
1565static int __init bfin_serial_init(void)
1566{
1567 int ret;
1568
1569 pr_info("Blackfin serial driver\n");
1570
1571 ret = uart_register_driver(&bfin_serial_reg);
1572 if (ret) {
1573 pr_err("failed to register %s:%d\n",
1574 bfin_serial_reg.driver_name, ret);
1575 }
1576
1577 ret = platform_driver_register(&bfin_serial_driver);
1578 if (ret) {
1579 pr_err("fail to register bfin uart\n");
1580 uart_unregister_driver(&bfin_serial_reg);
1581 }
1582
1583 return ret;
1584}
1585
1586static void __exit bfin_serial_exit(void)
1587{
1588 platform_driver_unregister(&bfin_serial_driver);
1589 uart_unregister_driver(&bfin_serial_reg);
1590}
1591
1592
1593module_init(bfin_serial_init);
1594module_exit(bfin_serial_exit);
1595
1596MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
1597MODULE_DESCRIPTION("Blackfin generic serial port driver");
1598MODULE_LICENSE("GPL");
1599MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1600MODULE_ALIAS("platform:bfin-uart");
diff --git a/drivers/tty/serial/bfin_sport_uart.c b/drivers/tty/serial/bfin_sport_uart.c
new file mode 100644
index 000000000000..e95c524d9d18
--- /dev/null
+++ b/drivers/tty/serial/bfin_sport_uart.c
@@ -0,0 +1,935 @@
1/*
2 * Blackfin On-Chip Sport Emulated UART Driver
3 *
4 * Copyright 2006-2009 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11/*
12 * This driver and the hardware supported are in term of EE-191 of ADI.
13 * http://www.analog.com/static/imported-files/application_notes/EE191.pdf
14 * This application note describe how to implement a UART on a Sharc DSP,
15 * but this driver is implemented on Blackfin Processor.
16 * Transmit Frame Sync is not used by this driver to transfer data out.
17 */
18
19/* #define DEBUG */
20
21#define DRV_NAME "bfin-sport-uart"
22#define DEVICE_NAME "ttySS"
23#define pr_fmt(fmt) DRV_NAME ": " fmt
24
25#include <linux/module.h>
26#include <linux/ioport.h>
27#include <linux/io.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/sysrq.h>
31#include <linux/slab.h>
32#include <linux/platform_device.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_core.h>
36
37#include <asm/bfin_sport.h>
38#include <asm/delay.h>
39#include <asm/portmux.h>
40
41#include "bfin_sport_uart.h"
42
43struct sport_uart_port {
44 struct uart_port port;
45 int err_irq;
46 unsigned short csize;
47 unsigned short rxmask;
48 unsigned short txmask1;
49 unsigned short txmask2;
50 unsigned char stopb;
51/* unsigned char parib; */
52#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
53 int cts_pin;
54 int rts_pin;
55#endif
56};
57
58static int sport_uart_tx_chars(struct sport_uart_port *up);
59static void sport_stop_tx(struct uart_port *port);
60
61static inline void tx_one_byte(struct sport_uart_port *up, unsigned int value)
62{
63 pr_debug("%s value:%x, mask1=0x%x, mask2=0x%x\n", __func__, value,
64 up->txmask1, up->txmask2);
65
66 /* Place Start and Stop bits */
67 __asm__ __volatile__ (
68 "%[val] <<= 1;"
69 "%[val] = %[val] & %[mask1];"
70 "%[val] = %[val] | %[mask2];"
71 : [val]"+d"(value)
72 : [mask1]"d"(up->txmask1), [mask2]"d"(up->txmask2)
73 : "ASTAT"
74 );
75 pr_debug("%s value:%x\n", __func__, value);
76
77 SPORT_PUT_TX(up, value);
78}
79
80static inline unsigned char rx_one_byte(struct sport_uart_port *up)
81{
82 unsigned int value;
83 unsigned char extract;
84 u32 tmp_mask1, tmp_mask2, tmp_shift, tmp;
85
86 if ((up->csize + up->stopb) > 7)
87 value = SPORT_GET_RX32(up);
88 else
89 value = SPORT_GET_RX(up);
90
91 pr_debug("%s value:%x, cs=%d, mask=0x%x\n", __func__, value,
92 up->csize, up->rxmask);
93
94 /* Extract data */
95 __asm__ __volatile__ (
96 "%[extr] = 0;"
97 "%[mask1] = %[rxmask];"
98 "%[mask2] = 0x0200(Z);"
99 "%[shift] = 0;"
100 "LSETUP(.Lloop_s, .Lloop_e) LC0 = %[lc];"
101 ".Lloop_s:"
102 "%[tmp] = extract(%[val], %[mask1].L)(Z);"
103 "%[tmp] <<= %[shift];"
104 "%[extr] = %[extr] | %[tmp];"
105 "%[mask1] = %[mask1] - %[mask2];"
106 ".Lloop_e:"
107 "%[shift] += 1;"
108 : [extr]"=&d"(extract), [shift]"=&d"(tmp_shift), [tmp]"=&d"(tmp),
109 [mask1]"=&d"(tmp_mask1), [mask2]"=&d"(tmp_mask2)
110 : [val]"d"(value), [rxmask]"d"(up->rxmask), [lc]"a"(up->csize)
111 : "ASTAT", "LB0", "LC0", "LT0"
112 );
113
114 pr_debug(" extract:%x\n", extract);
115 return extract;
116}
117
118static int sport_uart_setup(struct sport_uart_port *up, int size, int baud_rate)
119{
120 int tclkdiv, rclkdiv;
121 unsigned int sclk = get_sclk();
122
123 /* Set TCR1 and TCR2, TFSR is not enabled for uart */
124 SPORT_PUT_TCR1(up, (LATFS | ITFS | TFSR | TLSBIT | ITCLK));
125 SPORT_PUT_TCR2(up, size + 1);
126 pr_debug("%s TCR1:%x, TCR2:%x\n", __func__, SPORT_GET_TCR1(up), SPORT_GET_TCR2(up));
127
128 /* Set RCR1 and RCR2 */
129 SPORT_PUT_RCR1(up, (RCKFE | LARFS | LRFS | RFSR | IRCLK));
130 SPORT_PUT_RCR2(up, (size + 1) * 2 - 1);
131 pr_debug("%s RCR1:%x, RCR2:%x\n", __func__, SPORT_GET_RCR1(up), SPORT_GET_RCR2(up));
132
133 tclkdiv = sclk / (2 * baud_rate) - 1;
134 /* The actual uart baud rate of devices vary between +/-2%. The sport
135 * RX sample rate should be faster than the double of the worst case,
136 * otherwise, wrong data are received. So, set sport RX clock to be
137 * 3% faster.
138 */
139 rclkdiv = sclk / (2 * baud_rate * 2 * 97 / 100) - 1;
140 SPORT_PUT_TCLKDIV(up, tclkdiv);
141 SPORT_PUT_RCLKDIV(up, rclkdiv);
142 SSYNC();
143 pr_debug("%s sclk:%d, baud_rate:%d, tclkdiv:%d, rclkdiv:%d\n",
144 __func__, sclk, baud_rate, tclkdiv, rclkdiv);
145
146 return 0;
147}
148
149static irqreturn_t sport_uart_rx_irq(int irq, void *dev_id)
150{
151 struct sport_uart_port *up = dev_id;
152 struct tty_struct *tty = up->port.state->port.tty;
153 unsigned int ch;
154
155 spin_lock(&up->port.lock);
156
157 while (SPORT_GET_STAT(up) & RXNE) {
158 ch = rx_one_byte(up);
159 up->port.icount.rx++;
160
161 if (!uart_handle_sysrq_char(&up->port, ch))
162 tty_insert_flip_char(tty, ch, TTY_NORMAL);
163 }
164 tty_flip_buffer_push(tty);
165
166 spin_unlock(&up->port.lock);
167
168 return IRQ_HANDLED;
169}
170
171static irqreturn_t sport_uart_tx_irq(int irq, void *dev_id)
172{
173 struct sport_uart_port *up = dev_id;
174
175 spin_lock(&up->port.lock);
176 sport_uart_tx_chars(up);
177 spin_unlock(&up->port.lock);
178
179 return IRQ_HANDLED;
180}
181
182static irqreturn_t sport_uart_err_irq(int irq, void *dev_id)
183{
184 struct sport_uart_port *up = dev_id;
185 struct tty_struct *tty = up->port.state->port.tty;
186 unsigned int stat = SPORT_GET_STAT(up);
187
188 spin_lock(&up->port.lock);
189
190 /* Overflow in RX FIFO */
191 if (stat & ROVF) {
192 up->port.icount.overrun++;
193 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
194 SPORT_PUT_STAT(up, ROVF); /* Clear ROVF bit */
195 }
196 /* These should not happen */
197 if (stat & (TOVF | TUVF | RUVF)) {
198 pr_err("SPORT Error:%s %s %s\n",
199 (stat & TOVF) ? "TX overflow" : "",
200 (stat & TUVF) ? "TX underflow" : "",
201 (stat & RUVF) ? "RX underflow" : "");
202 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
203 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
204 }
205 SSYNC();
206
207 spin_unlock(&up->port.lock);
208 return IRQ_HANDLED;
209}
210
211#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
212static unsigned int sport_get_mctrl(struct uart_port *port)
213{
214 struct sport_uart_port *up = (struct sport_uart_port *)port;
215 if (up->cts_pin < 0)
216 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
217
218 /* CTS PIN is negative assertive. */
219 if (SPORT_UART_GET_CTS(up))
220 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
221 else
222 return TIOCM_DSR | TIOCM_CAR;
223}
224
225static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
226{
227 struct sport_uart_port *up = (struct sport_uart_port *)port;
228 if (up->rts_pin < 0)
229 return;
230
231 /* RTS PIN is negative assertive. */
232 if (mctrl & TIOCM_RTS)
233 SPORT_UART_ENABLE_RTS(up);
234 else
235 SPORT_UART_DISABLE_RTS(up);
236}
237
238/*
239 * Handle any change of modem status signal.
240 */
241static irqreturn_t sport_mctrl_cts_int(int irq, void *dev_id)
242{
243 struct sport_uart_port *up = (struct sport_uart_port *)dev_id;
244 unsigned int status;
245
246 status = sport_get_mctrl(&up->port);
247 uart_handle_cts_change(&up->port, status & TIOCM_CTS);
248
249 return IRQ_HANDLED;
250}
251#else
252static unsigned int sport_get_mctrl(struct uart_port *port)
253{
254 pr_debug("%s enter\n", __func__);
255 return TIOCM_CTS | TIOCM_CD | TIOCM_DSR;
256}
257
258static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
259{
260 pr_debug("%s enter\n", __func__);
261}
262#endif
263
264/* Reqeust IRQ, Setup clock */
265static int sport_startup(struct uart_port *port)
266{
267 struct sport_uart_port *up = (struct sport_uart_port *)port;
268 int ret;
269
270 pr_debug("%s enter\n", __func__);
271 ret = request_irq(up->port.irq, sport_uart_rx_irq, 0,
272 "SPORT_UART_RX", up);
273 if (ret) {
274 dev_err(port->dev, "unable to request SPORT RX interrupt\n");
275 return ret;
276 }
277
278 ret = request_irq(up->port.irq+1, sport_uart_tx_irq, 0,
279 "SPORT_UART_TX", up);
280 if (ret) {
281 dev_err(port->dev, "unable to request SPORT TX interrupt\n");
282 goto fail1;
283 }
284
285 ret = request_irq(up->err_irq, sport_uart_err_irq, 0,
286 "SPORT_UART_STATUS", up);
287 if (ret) {
288 dev_err(port->dev, "unable to request SPORT status interrupt\n");
289 goto fail2;
290 }
291
292#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
293 if (up->cts_pin >= 0) {
294 if (request_irq(gpio_to_irq(up->cts_pin),
295 sport_mctrl_cts_int,
296 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
297 IRQF_DISABLED, "BFIN_SPORT_UART_CTS", up)) {
298 up->cts_pin = -1;
299 dev_info(port->dev, "Unable to attach BlackFin UART \
300 over SPORT CTS interrupt. So, disable it.\n");
301 }
302 }
303 if (up->rts_pin >= 0)
304 gpio_direction_output(up->rts_pin, 0);
305#endif
306
307 return 0;
308 fail2:
309 free_irq(up->port.irq+1, up);
310 fail1:
311 free_irq(up->port.irq, up);
312
313 return ret;
314}
315
316/*
317 * sport_uart_tx_chars
318 *
319 * ret 1 means need to enable sport.
320 * ret 0 means do nothing.
321 */
322static int sport_uart_tx_chars(struct sport_uart_port *up)
323{
324 struct circ_buf *xmit = &up->port.state->xmit;
325
326 if (SPORT_GET_STAT(up) & TXF)
327 return 0;
328
329 if (up->port.x_char) {
330 tx_one_byte(up, up->port.x_char);
331 up->port.icount.tx++;
332 up->port.x_char = 0;
333 return 1;
334 }
335
336 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
337 /* The waiting loop to stop SPORT TX from TX interrupt is
338 * too long. This may block SPORT RX interrupts and cause
339 * RX FIFO overflow. So, do stop sport TX only after the last
340 * char in TX FIFO is moved into the shift register.
341 */
342 if (SPORT_GET_STAT(up) & TXHRE)
343 sport_stop_tx(&up->port);
344 return 0;
345 }
346
347 while(!(SPORT_GET_STAT(up) & TXF) && !uart_circ_empty(xmit)) {
348 tx_one_byte(up, xmit->buf[xmit->tail]);
349 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
350 up->port.icount.tx++;
351 }
352
353 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
354 uart_write_wakeup(&up->port);
355
356 return 1;
357}
358
359static unsigned int sport_tx_empty(struct uart_port *port)
360{
361 struct sport_uart_port *up = (struct sport_uart_port *)port;
362 unsigned int stat;
363
364 stat = SPORT_GET_STAT(up);
365 pr_debug("%s stat:%04x\n", __func__, stat);
366 if (stat & TXHRE) {
367 return TIOCSER_TEMT;
368 } else
369 return 0;
370}
371
372static void sport_stop_tx(struct uart_port *port)
373{
374 struct sport_uart_port *up = (struct sport_uart_port *)port;
375
376 pr_debug("%s enter\n", __func__);
377
378 if (!(SPORT_GET_TCR1(up) & TSPEN))
379 return;
380
381 /* Although the hold register is empty, last byte is still in shift
382 * register and not sent out yet. So, put a dummy data into TX FIFO.
383 * Then, sport tx stops when last byte is shift out and the dummy
384 * data is moved into the shift register.
385 */
386 SPORT_PUT_TX(up, 0xffff);
387 while (!(SPORT_GET_STAT(up) & TXHRE))
388 cpu_relax();
389
390 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
391 SSYNC();
392
393 return;
394}
395
396static void sport_start_tx(struct uart_port *port)
397{
398 struct sport_uart_port *up = (struct sport_uart_port *)port;
399
400 pr_debug("%s enter\n", __func__);
401
402 /* Write data into SPORT FIFO before enable SPROT to transmit */
403 if (sport_uart_tx_chars(up)) {
404 /* Enable transmit, then an interrupt will generated */
405 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
406 SSYNC();
407 }
408
409 pr_debug("%s exit\n", __func__);
410}
411
412static void sport_stop_rx(struct uart_port *port)
413{
414 struct sport_uart_port *up = (struct sport_uart_port *)port;
415
416 pr_debug("%s enter\n", __func__);
417 /* Disable sport to stop rx */
418 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
419 SSYNC();
420}
421
422static void sport_enable_ms(struct uart_port *port)
423{
424 pr_debug("%s enter\n", __func__);
425}
426
427static void sport_break_ctl(struct uart_port *port, int break_state)
428{
429 pr_debug("%s enter\n", __func__);
430}
431
432static void sport_shutdown(struct uart_port *port)
433{
434 struct sport_uart_port *up = (struct sport_uart_port *)port;
435
436 dev_dbg(port->dev, "%s enter\n", __func__);
437
438 /* Disable sport */
439 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
440 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
441 SSYNC();
442
443 free_irq(up->port.irq, up);
444 free_irq(up->port.irq+1, up);
445 free_irq(up->err_irq, up);
446#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
447 if (up->cts_pin >= 0)
448 free_irq(gpio_to_irq(up->cts_pin), up);
449#endif
450}
451
452static const char *sport_type(struct uart_port *port)
453{
454 struct sport_uart_port *up = (struct sport_uart_port *)port;
455
456 pr_debug("%s enter\n", __func__);
457 return up->port.type == PORT_BFIN_SPORT ? "BFIN-SPORT-UART" : NULL;
458}
459
460static void sport_release_port(struct uart_port *port)
461{
462 pr_debug("%s enter\n", __func__);
463}
464
465static int sport_request_port(struct uart_port *port)
466{
467 pr_debug("%s enter\n", __func__);
468 return 0;
469}
470
471static void sport_config_port(struct uart_port *port, int flags)
472{
473 struct sport_uart_port *up = (struct sport_uart_port *)port;
474
475 pr_debug("%s enter\n", __func__);
476 up->port.type = PORT_BFIN_SPORT;
477}
478
479static int sport_verify_port(struct uart_port *port, struct serial_struct *ser)
480{
481 pr_debug("%s enter\n", __func__);
482 return 0;
483}
484
485static void sport_set_termios(struct uart_port *port,
486 struct ktermios *termios, struct ktermios *old)
487{
488 struct sport_uart_port *up = (struct sport_uart_port *)port;
489 unsigned long flags;
490 int i;
491
492 pr_debug("%s enter, c_cflag:%08x\n", __func__, termios->c_cflag);
493
494 switch (termios->c_cflag & CSIZE) {
495 case CS8:
496 up->csize = 8;
497 break;
498 case CS7:
499 up->csize = 7;
500 break;
501 case CS6:
502 up->csize = 6;
503 break;
504 case CS5:
505 up->csize = 5;
506 break;
507 default:
508 pr_warning("requested word length not supported\n");
509 }
510
511 if (termios->c_cflag & CSTOPB) {
512 up->stopb = 1;
513 }
514 if (termios->c_cflag & PARENB) {
515 pr_warning("PAREN bits is not supported yet\n");
516 /* up->parib = 1; */
517 }
518
519 spin_lock_irqsave(&up->port.lock, flags);
520
521 port->read_status_mask = 0;
522
523 /*
524 * Characters to ignore
525 */
526 port->ignore_status_mask = 0;
527
528 /* RX extract mask */
529 up->rxmask = 0x01 | (((up->csize + up->stopb) * 2 - 1) << 0x8);
530 /* TX masks, 8 bit data and 1 bit stop for example:
531 * mask1 = b#0111111110
532 * mask2 = b#1000000000
533 */
534 for (i = 0, up->txmask1 = 0; i < up->csize; i++)
535 up->txmask1 |= (1<<i);
536 up->txmask2 = (1<<i);
537 if (up->stopb) {
538 ++i;
539 up->txmask2 |= (1<<i);
540 }
541 up->txmask1 <<= 1;
542 up->txmask2 <<= 1;
543 /* uart baud rate */
544 port->uartclk = uart_get_baud_rate(port, termios, old, 0, get_sclk()/16);
545
546 /* Disable UART */
547 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
548 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
549
550 sport_uart_setup(up, up->csize + up->stopb, port->uartclk);
551
552 /* driver TX line high after config, one dummy data is
553 * necessary to stop sport after shift one byte
554 */
555 SPORT_PUT_TX(up, 0xffff);
556 SPORT_PUT_TX(up, 0xffff);
557 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
558 SSYNC();
559 while (!(SPORT_GET_STAT(up) & TXHRE))
560 cpu_relax();
561 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
562 SSYNC();
563
564 /* Port speed changed, update the per-port timeout. */
565 uart_update_timeout(port, termios->c_cflag, port->uartclk);
566
567 /* Enable sport rx */
568 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) | RSPEN);
569 SSYNC();
570
571 spin_unlock_irqrestore(&up->port.lock, flags);
572}
573
574struct uart_ops sport_uart_ops = {
575 .tx_empty = sport_tx_empty,
576 .set_mctrl = sport_set_mctrl,
577 .get_mctrl = sport_get_mctrl,
578 .stop_tx = sport_stop_tx,
579 .start_tx = sport_start_tx,
580 .stop_rx = sport_stop_rx,
581 .enable_ms = sport_enable_ms,
582 .break_ctl = sport_break_ctl,
583 .startup = sport_startup,
584 .shutdown = sport_shutdown,
585 .set_termios = sport_set_termios,
586 .type = sport_type,
587 .release_port = sport_release_port,
588 .request_port = sport_request_port,
589 .config_port = sport_config_port,
590 .verify_port = sport_verify_port,
591};
592
593#define BFIN_SPORT_UART_MAX_PORTS 4
594
595static struct sport_uart_port *bfin_sport_uart_ports[BFIN_SPORT_UART_MAX_PORTS];
596
597#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
598#define CLASS_BFIN_SPORT_CONSOLE "bfin-sport-console"
599
600static int __init
601sport_uart_console_setup(struct console *co, char *options)
602{
603 struct sport_uart_port *up;
604 int baud = 57600;
605 int bits = 8;
606 int parity = 'n';
607# ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
608 int flow = 'r';
609# else
610 int flow = 'n';
611# endif
612
613 /* Check whether an invalid uart number has been specified */
614 if (co->index < 0 || co->index >= BFIN_SPORT_UART_MAX_PORTS)
615 return -ENODEV;
616
617 up = bfin_sport_uart_ports[co->index];
618 if (!up)
619 return -ENODEV;
620
621 if (options)
622 uart_parse_options(options, &baud, &parity, &bits, &flow);
623
624 return uart_set_options(&up->port, co, baud, parity, bits, flow);
625}
626
627static void sport_uart_console_putchar(struct uart_port *port, int ch)
628{
629 struct sport_uart_port *up = (struct sport_uart_port *)port;
630
631 while (SPORT_GET_STAT(up) & TXF)
632 barrier();
633
634 tx_one_byte(up, ch);
635}
636
637/*
638 * Interrupts are disabled on entering
639 */
640static void
641sport_uart_console_write(struct console *co, const char *s, unsigned int count)
642{
643 struct sport_uart_port *up = bfin_sport_uart_ports[co->index];
644 unsigned long flags;
645
646 spin_lock_irqsave(&up->port.lock, flags);
647
648 if (SPORT_GET_TCR1(up) & TSPEN)
649 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
650 else {
651 /* dummy data to start sport */
652 while (SPORT_GET_STAT(up) & TXF)
653 barrier();
654 SPORT_PUT_TX(up, 0xffff);
655 /* Enable transmit, then an interrupt will generated */
656 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
657 SSYNC();
658
659 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
660
661 /* Although the hold register is empty, last byte is still in shift
662 * register and not sent out yet. So, put a dummy data into TX FIFO.
663 * Then, sport tx stops when last byte is shift out and the dummy
664 * data is moved into the shift register.
665 */
666 while (SPORT_GET_STAT(up) & TXF)
667 barrier();
668 SPORT_PUT_TX(up, 0xffff);
669 while (!(SPORT_GET_STAT(up) & TXHRE))
670 barrier();
671
672 /* Stop sport tx transfer */
673 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
674 SSYNC();
675 }
676
677 spin_unlock_irqrestore(&up->port.lock, flags);
678}
679
680static struct uart_driver sport_uart_reg;
681
682static struct console sport_uart_console = {
683 .name = DEVICE_NAME,
684 .write = sport_uart_console_write,
685 .device = uart_console_device,
686 .setup = sport_uart_console_setup,
687 .flags = CON_PRINTBUFFER,
688 .index = -1,
689 .data = &sport_uart_reg,
690};
691
692#define SPORT_UART_CONSOLE (&sport_uart_console)
693#else
694#define SPORT_UART_CONSOLE NULL
695#endif /* CONFIG_SERIAL_BFIN_SPORT_CONSOLE */
696
697
698static struct uart_driver sport_uart_reg = {
699 .owner = THIS_MODULE,
700 .driver_name = DRV_NAME,
701 .dev_name = DEVICE_NAME,
702 .major = 204,
703 .minor = 84,
704 .nr = BFIN_SPORT_UART_MAX_PORTS,
705 .cons = SPORT_UART_CONSOLE,
706};
707
708#ifdef CONFIG_PM
709static int sport_uart_suspend(struct device *dev)
710{
711 struct sport_uart_port *sport = dev_get_drvdata(dev);
712
713 dev_dbg(dev, "%s enter\n", __func__);
714 if (sport)
715 uart_suspend_port(&sport_uart_reg, &sport->port);
716
717 return 0;
718}
719
720static int sport_uart_resume(struct device *dev)
721{
722 struct sport_uart_port *sport = dev_get_drvdata(dev);
723
724 dev_dbg(dev, "%s enter\n", __func__);
725 if (sport)
726 uart_resume_port(&sport_uart_reg, &sport->port);
727
728 return 0;
729}
730
731static struct dev_pm_ops bfin_sport_uart_dev_pm_ops = {
732 .suspend = sport_uart_suspend,
733 .resume = sport_uart_resume,
734};
735#endif
736
737static int __devinit sport_uart_probe(struct platform_device *pdev)
738{
739 struct resource *res;
740 struct sport_uart_port *sport;
741 int ret = 0;
742
743 dev_dbg(&pdev->dev, "%s enter\n", __func__);
744
745 if (pdev->id < 0 || pdev->id >= BFIN_SPORT_UART_MAX_PORTS) {
746 dev_err(&pdev->dev, "Wrong sport uart platform device id.\n");
747 return -ENOENT;
748 }
749
750 if (bfin_sport_uart_ports[pdev->id] == NULL) {
751 bfin_sport_uart_ports[pdev->id] =
752 kzalloc(sizeof(struct sport_uart_port), GFP_KERNEL);
753 sport = bfin_sport_uart_ports[pdev->id];
754 if (!sport) {
755 dev_err(&pdev->dev,
756 "Fail to malloc sport_uart_port\n");
757 return -ENOMEM;
758 }
759
760 ret = peripheral_request_list(
761 (unsigned short *)pdev->dev.platform_data, DRV_NAME);
762 if (ret) {
763 dev_err(&pdev->dev,
764 "Fail to request SPORT peripherals\n");
765 goto out_error_free_mem;
766 }
767
768 spin_lock_init(&sport->port.lock);
769 sport->port.fifosize = SPORT_TX_FIFO_SIZE,
770 sport->port.ops = &sport_uart_ops;
771 sport->port.line = pdev->id;
772 sport->port.iotype = UPIO_MEM;
773 sport->port.flags = UPF_BOOT_AUTOCONF;
774
775 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
776 if (res == NULL) {
777 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
778 ret = -ENOENT;
779 goto out_error_free_peripherals;
780 }
781
782 sport->port.membase = ioremap(res->start, resource_size(res));
783 if (!sport->port.membase) {
784 dev_err(&pdev->dev, "Cannot map sport IO\n");
785 ret = -ENXIO;
786 goto out_error_free_peripherals;
787 }
788 sport->port.mapbase = res->start;
789
790 sport->port.irq = platform_get_irq(pdev, 0);
791 if (sport->port.irq < 0) {
792 dev_err(&pdev->dev, "No sport RX/TX IRQ specified\n");
793 ret = -ENOENT;
794 goto out_error_unmap;
795 }
796
797 sport->err_irq = platform_get_irq(pdev, 1);
798 if (sport->err_irq < 0) {
799 dev_err(&pdev->dev, "No sport status IRQ specified\n");
800 ret = -ENOENT;
801 goto out_error_unmap;
802 }
803#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
804 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
805 if (res == NULL)
806 sport->cts_pin = -1;
807 else
808 sport->cts_pin = res->start;
809
810 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
811 if (res == NULL)
812 sport->rts_pin = -1;
813 else
814 sport->rts_pin = res->start;
815
816 if (sport->rts_pin >= 0)
817 gpio_request(sport->rts_pin, DRV_NAME);
818#endif
819 }
820
821#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
822 if (!is_early_platform_device(pdev)) {
823#endif
824 sport = bfin_sport_uart_ports[pdev->id];
825 sport->port.dev = &pdev->dev;
826 dev_set_drvdata(&pdev->dev, sport);
827 ret = uart_add_one_port(&sport_uart_reg, &sport->port);
828#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
829 }
830#endif
831 if (!ret)
832 return 0;
833
834 if (sport) {
835out_error_unmap:
836 iounmap(sport->port.membase);
837out_error_free_peripherals:
838 peripheral_free_list(
839 (unsigned short *)pdev->dev.platform_data);
840out_error_free_mem:
841 kfree(sport);
842 bfin_sport_uart_ports[pdev->id] = NULL;
843 }
844
845 return ret;
846}
847
848static int __devexit sport_uart_remove(struct platform_device *pdev)
849{
850 struct sport_uart_port *sport = platform_get_drvdata(pdev);
851
852 dev_dbg(&pdev->dev, "%s enter\n", __func__);
853 dev_set_drvdata(&pdev->dev, NULL);
854
855 if (sport) {
856 uart_remove_one_port(&sport_uart_reg, &sport->port);
857#ifdef CONFIG_SERIAL_BFIN_CTSRTS
858 if (sport->rts_pin >= 0)
859 gpio_free(sport->rts_pin);
860#endif
861 iounmap(sport->port.membase);
862 peripheral_free_list(
863 (unsigned short *)pdev->dev.platform_data);
864 kfree(sport);
865 bfin_sport_uart_ports[pdev->id] = NULL;
866 }
867
868 return 0;
869}
870
871static struct platform_driver sport_uart_driver = {
872 .probe = sport_uart_probe,
873 .remove = __devexit_p(sport_uart_remove),
874 .driver = {
875 .name = DRV_NAME,
876#ifdef CONFIG_PM
877 .pm = &bfin_sport_uart_dev_pm_ops,
878#endif
879 },
880};
881
882#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
883static __initdata struct early_platform_driver early_sport_uart_driver = {
884 .class_str = CLASS_BFIN_SPORT_CONSOLE,
885 .pdrv = &sport_uart_driver,
886 .requested_id = EARLY_PLATFORM_ID_UNSET,
887};
888
889static int __init sport_uart_rs_console_init(void)
890{
891 early_platform_driver_register(&early_sport_uart_driver, DRV_NAME);
892
893 early_platform_driver_probe(CLASS_BFIN_SPORT_CONSOLE,
894 BFIN_SPORT_UART_MAX_PORTS, 0);
895
896 register_console(&sport_uart_console);
897
898 return 0;
899}
900console_initcall(sport_uart_rs_console_init);
901#endif
902
903static int __init sport_uart_init(void)
904{
905 int ret;
906
907 pr_info("Blackfin uart over sport driver\n");
908
909 ret = uart_register_driver(&sport_uart_reg);
910 if (ret) {
911 pr_err("failed to register %s:%d\n",
912 sport_uart_reg.driver_name, ret);
913 return ret;
914 }
915
916 ret = platform_driver_register(&sport_uart_driver);
917 if (ret) {
918 pr_err("failed to register sport uart driver:%d\n", ret);
919 uart_unregister_driver(&sport_uart_reg);
920 }
921
922 return ret;
923}
924module_init(sport_uart_init);
925
926static void __exit sport_uart_exit(void)
927{
928 platform_driver_unregister(&sport_uart_driver);
929 uart_unregister_driver(&sport_uart_reg);
930}
931module_exit(sport_uart_exit);
932
933MODULE_AUTHOR("Sonic Zhang, Roy Huang");
934MODULE_DESCRIPTION("Blackfin serial over SPORT driver");
935MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/bfin_sport_uart.h b/drivers/tty/serial/bfin_sport_uart.h
new file mode 100644
index 000000000000..6d06ce1d5675
--- /dev/null
+++ b/drivers/tty/serial/bfin_sport_uart.h
@@ -0,0 +1,86 @@
1/*
2 * Blackfin On-Chip Sport Emulated UART Driver
3 *
4 * Copyright 2006-2008 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11/*
12 * This driver and the hardware supported are in term of EE-191 of ADI.
13 * http://www.analog.com/static/imported-files/application_notes/EE191.pdf
14 * This application note describe how to implement a UART on a Sharc DSP,
15 * but this driver is implemented on Blackfin Processor.
16 * Transmit Frame Sync is not used by this driver to transfer data out.
17 */
18
19#ifndef _BFIN_SPORT_UART_H
20#define _BFIN_SPORT_UART_H
21
22#define OFFSET_TCR1 0x00 /* Transmit Configuration 1 Register */
23#define OFFSET_TCR2 0x04 /* Transmit Configuration 2 Register */
24#define OFFSET_TCLKDIV 0x08 /* Transmit Serial Clock Divider Register */
25#define OFFSET_TFSDIV 0x0C /* Transmit Frame Sync Divider Register */
26#define OFFSET_TX 0x10 /* Transmit Data Register */
27#define OFFSET_RX 0x18 /* Receive Data Register */
28#define OFFSET_RCR1 0x20 /* Receive Configuration 1 Register */
29#define OFFSET_RCR2 0x24 /* Receive Configuration 2 Register */
30#define OFFSET_RCLKDIV 0x28 /* Receive Serial Clock Divider Register */
31#define OFFSET_RFSDIV 0x2c /* Receive Frame Sync Divider Register */
32#define OFFSET_STAT 0x30 /* Status Register */
33
34#define SPORT_GET_TCR1(sport) bfin_read16(((sport)->port.membase + OFFSET_TCR1))
35#define SPORT_GET_TCR2(sport) bfin_read16(((sport)->port.membase + OFFSET_TCR2))
36#define SPORT_GET_TCLKDIV(sport) bfin_read16(((sport)->port.membase + OFFSET_TCLKDIV))
37#define SPORT_GET_TFSDIV(sport) bfin_read16(((sport)->port.membase + OFFSET_TFSDIV))
38#define SPORT_GET_TX(sport) bfin_read16(((sport)->port.membase + OFFSET_TX))
39#define SPORT_GET_RX(sport) bfin_read16(((sport)->port.membase + OFFSET_RX))
40/*
41 * If another interrupt fires while doing a 32-bit read from RX FIFO,
42 * a fake RX underflow error will be generated. So disable interrupts
43 * to prevent interruption while reading the FIFO.
44 */
45#define SPORT_GET_RX32(sport) \
46({ \
47 unsigned int __ret; \
48 if (ANOMALY_05000473) \
49 local_irq_disable(); \
50 __ret = bfin_read32((sport)->port.membase + OFFSET_RX); \
51 if (ANOMALY_05000473) \
52 local_irq_enable(); \
53 __ret; \
54})
55#define SPORT_GET_RCR1(sport) bfin_read16(((sport)->port.membase + OFFSET_RCR1))
56#define SPORT_GET_RCR2(sport) bfin_read16(((sport)->port.membase + OFFSET_RCR2))
57#define SPORT_GET_RCLKDIV(sport) bfin_read16(((sport)->port.membase + OFFSET_RCLKDIV))
58#define SPORT_GET_RFSDIV(sport) bfin_read16(((sport)->port.membase + OFFSET_RFSDIV))
59#define SPORT_GET_STAT(sport) bfin_read16(((sport)->port.membase + OFFSET_STAT))
60
61#define SPORT_PUT_TCR1(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TCR1), v)
62#define SPORT_PUT_TCR2(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TCR2), v)
63#define SPORT_PUT_TCLKDIV(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TCLKDIV), v)
64#define SPORT_PUT_TFSDIV(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TFSDIV), v)
65#define SPORT_PUT_TX(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TX), v)
66#define SPORT_PUT_RX(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RX), v)
67#define SPORT_PUT_RCR1(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RCR1), v)
68#define SPORT_PUT_RCR2(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RCR2), v)
69#define SPORT_PUT_RCLKDIV(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RCLKDIV), v)
70#define SPORT_PUT_RFSDIV(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RFSDIV), v)
71#define SPORT_PUT_STAT(sport, v) bfin_write16(((sport)->port.membase + OFFSET_STAT), v)
72
73#define SPORT_TX_FIFO_SIZE 8
74
75#define SPORT_UART_GET_CTS(x) gpio_get_value(x->cts_pin)
76#define SPORT_UART_DISABLE_RTS(x) gpio_set_value(x->rts_pin, 1)
77#define SPORT_UART_ENABLE_RTS(x) gpio_set_value(x->rts_pin, 0)
78
79#if defined(CONFIG_SERIAL_BFIN_SPORT0_UART_CTSRTS) \
80 || defined(CONFIG_SERIAL_BFIN_SPORT1_UART_CTSRTS) \
81 || defined(CONFIG_SERIAL_BFIN_SPORT2_UART_CTSRTS) \
82 || defined(CONFIG_SERIAL_BFIN_SPORT3_UART_CTSRTS)
83# define CONFIG_SERIAL_BFIN_SPORT_CTSRTS
84#endif
85
86#endif /* _BFIN_SPORT_UART_H */
diff --git a/drivers/tty/serial/clps711x.c b/drivers/tty/serial/clps711x.c
new file mode 100644
index 000000000000..b6acd19b458e
--- /dev/null
+++ b/drivers/tty/serial/clps711x.c
@@ -0,0 +1,579 @@
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
26#if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
27#define SUPPORT_SYSRQ
28#endif
29
30#include <linux/module.h>
31#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/console.h>
34#include <linux/sysrq.h>
35#include <linux/spinlock.h>
36#include <linux/device.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39#include <linux/serial_core.h>
40#include <linux/serial.h>
41#include <linux/io.h>
42
43#include <mach/hardware.h>
44#include <asm/irq.h>
45#include <asm/hardware/clps7111.h>
46
47#define UART_NR 2
48
49#define SERIAL_CLPS711X_MAJOR 204
50#define SERIAL_CLPS711X_MINOR 40
51#define SERIAL_CLPS711X_NR UART_NR
52
53/*
54 * We use the relevant SYSCON register as a base address for these ports.
55 */
56#define UBRLCR(port) ((port)->iobase + UBRLCR1 - SYSCON1)
57#define UARTDR(port) ((port)->iobase + UARTDR1 - SYSCON1)
58#define SYSFLG(port) ((port)->iobase + SYSFLG1 - SYSCON1)
59#define SYSCON(port) ((port)->iobase + SYSCON1 - SYSCON1)
60
61#define TX_IRQ(port) ((port)->irq)
62#define RX_IRQ(port) ((port)->irq + 1)
63
64#define UART_ANY_ERR (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)
65
66#define tx_enabled(port) ((port)->unused[0])
67
68static void clps711xuart_stop_tx(struct uart_port *port)
69{
70 if (tx_enabled(port)) {
71 disable_irq(TX_IRQ(port));
72 tx_enabled(port) = 0;
73 }
74}
75
76static void clps711xuart_start_tx(struct uart_port *port)
77{
78 if (!tx_enabled(port)) {
79 enable_irq(TX_IRQ(port));
80 tx_enabled(port) = 1;
81 }
82}
83
84static void clps711xuart_stop_rx(struct uart_port *port)
85{
86 disable_irq(RX_IRQ(port));
87}
88
89static void clps711xuart_enable_ms(struct uart_port *port)
90{
91}
92
93static irqreturn_t clps711xuart_int_rx(int irq, void *dev_id)
94{
95 struct uart_port *port = dev_id;
96 struct tty_struct *tty = port->state->port.tty;
97 unsigned int status, ch, flg;
98
99 status = clps_readl(SYSFLG(port));
100 while (!(status & SYSFLG_URXFE)) {
101 ch = clps_readl(UARTDR(port));
102
103 port->icount.rx++;
104
105 flg = TTY_NORMAL;
106
107 /*
108 * Note that the error handling code is
109 * out of the main execution path
110 */
111 if (unlikely(ch & UART_ANY_ERR)) {
112 if (ch & UARTDR_PARERR)
113 port->icount.parity++;
114 else if (ch & UARTDR_FRMERR)
115 port->icount.frame++;
116 if (ch & UARTDR_OVERR)
117 port->icount.overrun++;
118
119 ch &= port->read_status_mask;
120
121 if (ch & UARTDR_PARERR)
122 flg = TTY_PARITY;
123 else if (ch & UARTDR_FRMERR)
124 flg = TTY_FRAME;
125
126#ifdef SUPPORT_SYSRQ
127 port->sysrq = 0;
128#endif
129 }
130
131 if (uart_handle_sysrq_char(port, ch))
132 goto ignore_char;
133
134 /*
135 * CHECK: does overrun affect the current character?
136 * ASSUMPTION: it does not.
137 */
138 uart_insert_char(port, ch, UARTDR_OVERR, ch, flg);
139
140 ignore_char:
141 status = clps_readl(SYSFLG(port));
142 }
143 tty_flip_buffer_push(tty);
144 return IRQ_HANDLED;
145}
146
147static irqreturn_t clps711xuart_int_tx(int irq, void *dev_id)
148{
149 struct uart_port *port = dev_id;
150 struct circ_buf *xmit = &port->state->xmit;
151 int count;
152
153 if (port->x_char) {
154 clps_writel(port->x_char, UARTDR(port));
155 port->icount.tx++;
156 port->x_char = 0;
157 return IRQ_HANDLED;
158 }
159 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
160 clps711xuart_stop_tx(port);
161 return IRQ_HANDLED;
162 }
163
164 count = port->fifosize >> 1;
165 do {
166 clps_writel(xmit->buf[xmit->tail], UARTDR(port));
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);
172
173 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
174 uart_write_wakeup(port);
175
176 if (uart_circ_empty(xmit))
177 clps711xuart_stop_tx(port);
178
179 return IRQ_HANDLED;
180}
181
182static unsigned int clps711xuart_tx_empty(struct uart_port *port)
183{
184 unsigned int status = clps_readl(SYSFLG(port));
185 return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT;
186}
187
188static unsigned int clps711xuart_get_mctrl(struct uart_port *port)
189{
190 unsigned int port_addr;
191 unsigned int result = 0;
192 unsigned int status;
193
194 port_addr = SYSFLG(port);
195 if (port_addr == SYSFLG1) {
196 status = clps_readl(SYSFLG1);
197 if (status & SYSFLG1_DCD)
198 result |= TIOCM_CAR;
199 if (status & SYSFLG1_DSR)
200 result |= TIOCM_DSR;
201 if (status & SYSFLG1_CTS)
202 result |= TIOCM_CTS;
203 }
204
205 return result;
206}
207
208static void
209clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
210{
211}
212
213static void clps711xuart_break_ctl(struct uart_port *port, int break_state)
214{
215 unsigned long flags;
216 unsigned int ubrlcr;
217
218 spin_lock_irqsave(&port->lock, flags);
219 ubrlcr = clps_readl(UBRLCR(port));
220 if (break_state == -1)
221 ubrlcr |= UBRLCR_BREAK;
222 else
223 ubrlcr &= ~UBRLCR_BREAK;
224 clps_writel(ubrlcr, UBRLCR(port));
225 spin_unlock_irqrestore(&port->lock, flags);
226}
227
228static int clps711xuart_startup(struct uart_port *port)
229{
230 unsigned int syscon;
231 int retval;
232
233 tx_enabled(port) = 1;
234
235 /*
236 * Allocate the IRQs
237 */
238 retval = request_irq(TX_IRQ(port), clps711xuart_int_tx, 0,
239 "clps711xuart_tx", port);
240 if (retval)
241 return retval;
242
243 retval = request_irq(RX_IRQ(port), clps711xuart_int_rx, 0,
244 "clps711xuart_rx", port);
245 if (retval) {
246 free_irq(TX_IRQ(port), port);
247 return retval;
248 }
249
250 /*
251 * enable the port
252 */
253 syscon = clps_readl(SYSCON(port));
254 syscon |= SYSCON_UARTEN;
255 clps_writel(syscon, SYSCON(port));
256
257 return 0;
258}
259
260static void clps711xuart_shutdown(struct uart_port *port)
261{
262 unsigned int ubrlcr, syscon;
263
264 /*
265 * Free the interrupt
266 */
267 free_irq(TX_IRQ(port), port); /* TX interrupt */
268 free_irq(RX_IRQ(port), port); /* RX interrupt */
269
270 /*
271 * disable the port
272 */
273 syscon = clps_readl(SYSCON(port));
274 syscon &= ~SYSCON_UARTEN;
275 clps_writel(syscon, SYSCON(port));
276
277 /*
278 * disable break condition and fifos
279 */
280 ubrlcr = clps_readl(UBRLCR(port));
281 ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK);
282 clps_writel(ubrlcr, UBRLCR(port));
283}
284
285static void
286clps711xuart_set_termios(struct uart_port *port, struct ktermios *termios,
287 struct ktermios *old)
288{
289 unsigned int ubrlcr, baud, quot;
290 unsigned long flags;
291
292 /*
293 * We don't implement CREAD.
294 */
295 termios->c_cflag |= CREAD;
296
297 /*
298 * Ask the core to calculate the divisor for us.
299 */
300 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
301 quot = uart_get_divisor(port, baud);
302
303 switch (termios->c_cflag & CSIZE) {
304 case CS5:
305 ubrlcr = UBRLCR_WRDLEN5;
306 break;
307 case CS6:
308 ubrlcr = UBRLCR_WRDLEN6;
309 break;
310 case CS7:
311 ubrlcr = UBRLCR_WRDLEN7;
312 break;
313 default: // CS8
314 ubrlcr = UBRLCR_WRDLEN8;
315 break;
316 }
317 if (termios->c_cflag & CSTOPB)
318 ubrlcr |= UBRLCR_XSTOP;
319 if (termios->c_cflag & PARENB) {
320 ubrlcr |= UBRLCR_PRTEN;
321 if (!(termios->c_cflag & PARODD))
322 ubrlcr |= UBRLCR_EVENPRT;
323 }
324 if (port->fifosize > 1)
325 ubrlcr |= UBRLCR_FIFOEN;
326
327 spin_lock_irqsave(&port->lock, flags);
328
329 /*
330 * Update the per-port timeout.
331 */
332 uart_update_timeout(port, termios->c_cflag, baud);
333
334 port->read_status_mask = UARTDR_OVERR;
335 if (termios->c_iflag & INPCK)
336 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
337
338 /*
339 * Characters to ignore
340 */
341 port->ignore_status_mask = 0;
342 if (termios->c_iflag & IGNPAR)
343 port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR;
344 if (termios->c_iflag & IGNBRK) {
345 /*
346 * If we're ignoring parity and break indicators,
347 * ignore overruns to (for real raw support).
348 */
349 if (termios->c_iflag & IGNPAR)
350 port->ignore_status_mask |= UARTDR_OVERR;
351 }
352
353 quot -= 1;
354
355 clps_writel(ubrlcr | quot, UBRLCR(port));
356
357 spin_unlock_irqrestore(&port->lock, flags);
358}
359
360static const char *clps711xuart_type(struct uart_port *port)
361{
362 return port->type == PORT_CLPS711X ? "CLPS711x" : NULL;
363}
364
365/*
366 * Configure/autoconfigure the port.
367 */
368static void clps711xuart_config_port(struct uart_port *port, int flags)
369{
370 if (flags & UART_CONFIG_TYPE)
371 port->type = PORT_CLPS711X;
372}
373
374static void clps711xuart_release_port(struct uart_port *port)
375{
376}
377
378static int clps711xuart_request_port(struct uart_port *port)
379{
380 return 0;
381}
382
383static struct uart_ops clps711x_pops = {
384 .tx_empty = clps711xuart_tx_empty,
385 .set_mctrl = clps711xuart_set_mctrl_null,
386 .get_mctrl = clps711xuart_get_mctrl,
387 .stop_tx = clps711xuart_stop_tx,
388 .start_tx = clps711xuart_start_tx,
389 .stop_rx = clps711xuart_stop_rx,
390 .enable_ms = clps711xuart_enable_ms,
391 .break_ctl = clps711xuart_break_ctl,
392 .startup = clps711xuart_startup,
393 .shutdown = clps711xuart_shutdown,
394 .set_termios = clps711xuart_set_termios,
395 .type = clps711xuart_type,
396 .config_port = clps711xuart_config_port,
397 .release_port = clps711xuart_release_port,
398 .request_port = clps711xuart_request_port,
399};
400
401static struct uart_port clps711x_ports[UART_NR] = {
402 {
403 .iobase = SYSCON1,
404 .irq = IRQ_UTXINT1, /* IRQ_URXINT1, IRQ_UMSINT */
405 .uartclk = 3686400,
406 .fifosize = 16,
407 .ops = &clps711x_pops,
408 .line = 0,
409 .flags = UPF_BOOT_AUTOCONF,
410 },
411 {
412 .iobase = SYSCON2,
413 .irq = IRQ_UTXINT2, /* IRQ_URXINT2 */
414 .uartclk = 3686400,
415 .fifosize = 16,
416 .ops = &clps711x_pops,
417 .line = 1,
418 .flags = UPF_BOOT_AUTOCONF,
419 }
420};
421
422#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
423static void clps711xuart_console_putchar(struct uart_port *port, int ch)
424{
425 while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF)
426 barrier();
427 clps_writel(ch, UARTDR(port));
428}
429
430/*
431 * Print a string to the serial port trying not to disturb
432 * any possible real use of the port...
433 *
434 * The console_lock must be held when we get here.
435 *
436 * Note that this is called with interrupts already disabled
437 */
438static void
439clps711xuart_console_write(struct console *co, const char *s,
440 unsigned int count)
441{
442 struct uart_port *port = clps711x_ports + co->index;
443 unsigned int status, syscon;
444
445 /*
446 * Ensure that the port is enabled.
447 */
448 syscon = clps_readl(SYSCON(port));
449 clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
450
451 uart_console_write(port, s, count, clps711xuart_console_putchar);
452
453 /*
454 * Finally, wait for transmitter to become empty
455 * and restore the uart state.
456 */
457 do {
458 status = clps_readl(SYSFLG(port));
459 } while (status & SYSFLG_UBUSY);
460
461 clps_writel(syscon, SYSCON(port));
462}
463
464static void __init
465clps711xuart_console_get_options(struct uart_port *port, int *baud,
466 int *parity, int *bits)
467{
468 if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
469 unsigned int ubrlcr, quot;
470
471 ubrlcr = clps_readl(UBRLCR(port));
472
473 *parity = 'n';
474 if (ubrlcr & UBRLCR_PRTEN) {
475 if (ubrlcr & UBRLCR_EVENPRT)
476 *parity = 'e';
477 else
478 *parity = 'o';
479 }
480
481 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
482 *bits = 7;
483 else
484 *bits = 8;
485
486 quot = ubrlcr & UBRLCR_BAUD_MASK;
487 *baud = port->uartclk / (16 * (quot + 1));
488 }
489}
490
491static int __init clps711xuart_console_setup(struct console *co, char *options)
492{
493 struct uart_port *port;
494 int baud = 38400;
495 int bits = 8;
496 int parity = 'n';
497 int flow = 'n';
498
499 /*
500 * Check whether an invalid uart number has been specified, and
501 * if so, search for the first available port that does have
502 * console support.
503 */
504 port = uart_get_console(clps711x_ports, UART_NR, co);
505
506 if (options)
507 uart_parse_options(options, &baud, &parity, &bits, &flow);
508 else
509 clps711xuart_console_get_options(port, &baud, &parity, &bits);
510
511 return uart_set_options(port, co, baud, parity, bits, flow);
512}
513
514static struct uart_driver clps711x_reg;
515static struct console clps711x_console = {
516 .name = "ttyCL",
517 .write = clps711xuart_console_write,
518 .device = uart_console_device,
519 .setup = clps711xuart_console_setup,
520 .flags = CON_PRINTBUFFER,
521 .index = -1,
522 .data = &clps711x_reg,
523};
524
525static int __init clps711xuart_console_init(void)
526{
527 register_console(&clps711x_console);
528 return 0;
529}
530console_initcall(clps711xuart_console_init);
531
532#define CLPS711X_CONSOLE &clps711x_console
533#else
534#define CLPS711X_CONSOLE NULL
535#endif
536
537static struct uart_driver clps711x_reg = {
538 .driver_name = "ttyCL",
539 .dev_name = "ttyCL",
540 .major = SERIAL_CLPS711X_MAJOR,
541 .minor = SERIAL_CLPS711X_MINOR,
542 .nr = UART_NR,
543
544 .cons = CLPS711X_CONSOLE,
545};
546
547static int __init clps711xuart_init(void)
548{
549 int ret, i;
550
551 printk(KERN_INFO "Serial: CLPS711x driver\n");
552
553 ret = uart_register_driver(&clps711x_reg);
554 if (ret)
555 return ret;
556
557 for (i = 0; i < UART_NR; i++)
558 uart_add_one_port(&clps711x_reg, &clps711x_ports[i]);
559
560 return 0;
561}
562
563static void __exit clps711xuart_exit(void)
564{
565 int i;
566
567 for (i = 0; i < UART_NR; i++)
568 uart_remove_one_port(&clps711x_reg, &clps711x_ports[i]);
569
570 uart_unregister_driver(&clps711x_reg);
571}
572
573module_init(clps711xuart_init);
574module_exit(clps711xuart_exit);
575
576MODULE_AUTHOR("Deep Blue Solutions Ltd");
577MODULE_DESCRIPTION("CLPS-711x generic serial driver");
578MODULE_LICENSE("GPL");
579MODULE_ALIAS_CHARDEV(SERIAL_CLPS711X_MAJOR, SERIAL_CLPS711X_MINOR);
diff --git a/drivers/tty/serial/cpm_uart/Makefile b/drivers/tty/serial/cpm_uart/Makefile
new file mode 100644
index 000000000000..e072724ea754
--- /dev/null
+++ b/drivers/tty/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/tty/serial/cpm_uart/cpm_uart.h b/drivers/tty/serial/cpm_uart/cpm_uart.h
new file mode 100644
index 000000000000..b754dcf0fda5
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart.h
@@ -0,0 +1,147 @@
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 * 2006 (c) MontaVista Software, Inc.
9 * Vitaly Bordug <vbordug@ru.mvista.com>
10 *
11 * This file is licensed under the terms of the GNU General Public License
12 * version 2. This program is licensed "as is" without any warranty of any
13 * kind, whether express or implied.
14 *
15 */
16#ifndef CPM_UART_H
17#define CPM_UART_H
18
19#include <linux/platform_device.h>
20#include <linux/fs_uart_pd.h>
21
22#if defined(CONFIG_CPM2)
23#include "cpm_uart_cpm2.h"
24#elif defined(CONFIG_8xx)
25#include "cpm_uart_cpm1.h"
26#endif
27
28#define SERIAL_CPM_MAJOR 204
29#define SERIAL_CPM_MINOR 46
30
31#define IS_SMC(pinfo) (pinfo->flags & FLAG_SMC)
32#define IS_DISCARDING(pinfo) (pinfo->flags & FLAG_DISCARDING)
33#define FLAG_DISCARDING 0x00000004 /* when set, don't discard */
34#define FLAG_SMC 0x00000002
35#define FLAG_CONSOLE 0x00000001
36
37#define UART_SMC1 fsid_smc1_uart
38#define UART_SMC2 fsid_smc2_uart
39#define UART_SCC1 fsid_scc1_uart
40#define UART_SCC2 fsid_scc2_uart
41#define UART_SCC3 fsid_scc3_uart
42#define UART_SCC4 fsid_scc4_uart
43
44#define UART_NR fs_uart_nr
45
46#define RX_NUM_FIFO 4
47#define RX_BUF_SIZE 32
48#define TX_NUM_FIFO 4
49#define TX_BUF_SIZE 32
50
51#define SCC_WAIT_CLOSING 100
52
53#define GPIO_CTS 0
54#define GPIO_RTS 1
55#define GPIO_DCD 2
56#define GPIO_DSR 3
57#define GPIO_DTR 4
58#define GPIO_RI 5
59
60#define NUM_GPIOS (GPIO_RI+1)
61
62struct uart_cpm_port {
63 struct uart_port port;
64 u16 rx_nrfifos;
65 u16 rx_fifosize;
66 u16 tx_nrfifos;
67 u16 tx_fifosize;
68 smc_t __iomem *smcp;
69 smc_uart_t __iomem *smcup;
70 scc_t __iomem *sccp;
71 scc_uart_t __iomem *sccup;
72 cbd_t __iomem *rx_bd_base;
73 cbd_t __iomem *rx_cur;
74 cbd_t __iomem *tx_bd_base;
75 cbd_t __iomem *tx_cur;
76 unsigned char *tx_buf;
77 unsigned char *rx_buf;
78 u32 flags;
79 struct clk *clk;
80 u8 brg;
81 uint dp_addr;
82 void *mem_addr;
83 dma_addr_t dma_addr;
84 u32 mem_size;
85 /* wait on close if needed */
86 int wait_closing;
87 /* value to combine with opcode to form cpm command */
88 u32 command;
89 int gpios[NUM_GPIOS];
90};
91
92extern int cpm_uart_nr;
93extern struct uart_cpm_port cpm_uart_ports[UART_NR];
94
95/* these are located in their respective files */
96void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd);
97void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
98 struct device_node *np);
99void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram);
100int cpm_uart_init_portdesc(void);
101int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con);
102void cpm_uart_freebuf(struct uart_cpm_port *pinfo);
103
104void smc1_lineif(struct uart_cpm_port *pinfo);
105void smc2_lineif(struct uart_cpm_port *pinfo);
106void scc1_lineif(struct uart_cpm_port *pinfo);
107void scc2_lineif(struct uart_cpm_port *pinfo);
108void scc3_lineif(struct uart_cpm_port *pinfo);
109void scc4_lineif(struct uart_cpm_port *pinfo);
110
111/*
112 virtual to phys transtalion
113*/
114static inline unsigned long cpu2cpm_addr(void *addr,
115 struct uart_cpm_port *pinfo)
116{
117 int offset;
118 u32 val = (u32)addr;
119 u32 mem = (u32)pinfo->mem_addr;
120 /* sane check */
121 if (likely(val >= mem && val < mem + pinfo->mem_size)) {
122 offset = val - mem;
123 return pinfo->dma_addr + offset;
124 }
125 /* something nasty happened */
126 BUG();
127 return 0;
128}
129
130static inline void *cpm2cpu_addr(unsigned long addr,
131 struct uart_cpm_port *pinfo)
132{
133 int offset;
134 u32 val = addr;
135 u32 dma = (u32)pinfo->dma_addr;
136 /* sane check */
137 if (likely(val >= dma && val < dma + pinfo->mem_size)) {
138 offset = val - dma;
139 return pinfo->mem_addr + offset;
140 }
141 /* something nasty happened */
142 BUG();
143 return NULL;
144}
145
146
147#endif /* CPM_UART_H */
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_core.c b/drivers/tty/serial/cpm_uart/cpm_uart_core.c
new file mode 100644
index 000000000000..8692ff98fc07
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_core.c
@@ -0,0 +1,1443 @@
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 (galak@kernel.crashing.org) (CPM2)
11 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
12 *
13 * Copyright (C) 2004, 2007 Freescale Semiconductor, Inc.
14 * (C) 2004 Intracom, S.A.
15 * (C) 2005-2006 MontaVista Software, Inc.
16 * Vitaly Bordug <vbordug@ru.mvista.com>
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 */
33
34#include <linux/module.h>
35#include <linux/tty.h>
36#include <linux/ioport.h>
37#include <linux/init.h>
38#include <linux/serial.h>
39#include <linux/console.h>
40#include <linux/sysrq.h>
41#include <linux/device.h>
42#include <linux/bootmem.h>
43#include <linux/dma-mapping.h>
44#include <linux/fs_uart_pd.h>
45#include <linux/of_platform.h>
46#include <linux/gpio.h>
47#include <linux/of_gpio.h>
48#include <linux/clk.h>
49
50#include <asm/io.h>
51#include <asm/irq.h>
52#include <asm/delay.h>
53#include <asm/fs_pd.h>
54#include <asm/udbg.h>
55
56#if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
57#define SUPPORT_SYSRQ
58#endif
59
60#include <linux/serial_core.h>
61#include <linux/kernel.h>
62
63#include "cpm_uart.h"
64
65
66/**************************************************************/
67
68static int cpm_uart_tx_pump(struct uart_port *port);
69static void cpm_uart_init_smc(struct uart_cpm_port *pinfo);
70static void cpm_uart_init_scc(struct uart_cpm_port *pinfo);
71static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
72
73/**************************************************************/
74
75#define HW_BUF_SPD_THRESHOLD 9600
76
77/*
78 * Check, if transmit buffers are processed
79*/
80static unsigned int cpm_uart_tx_empty(struct uart_port *port)
81{
82 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
83 cbd_t __iomem *bdp = pinfo->tx_bd_base;
84 int ret = 0;
85
86 while (1) {
87 if (in_be16(&bdp->cbd_sc) & BD_SC_READY)
88 break;
89
90 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {
91 ret = TIOCSER_TEMT;
92 break;
93 }
94 bdp++;
95 }
96
97 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
98
99 return ret;
100}
101
102static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
103{
104 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
105
106 if (pinfo->gpios[GPIO_RTS] >= 0)
107 gpio_set_value(pinfo->gpios[GPIO_RTS], !(mctrl & TIOCM_RTS));
108
109 if (pinfo->gpios[GPIO_DTR] >= 0)
110 gpio_set_value(pinfo->gpios[GPIO_DTR], !(mctrl & TIOCM_DTR));
111}
112
113static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
114{
115 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
116 unsigned int mctrl = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
117
118 if (pinfo->gpios[GPIO_CTS] >= 0) {
119 if (gpio_get_value(pinfo->gpios[GPIO_CTS]))
120 mctrl &= ~TIOCM_CTS;
121 }
122
123 if (pinfo->gpios[GPIO_DSR] >= 0) {
124 if (gpio_get_value(pinfo->gpios[GPIO_DSR]))
125 mctrl &= ~TIOCM_DSR;
126 }
127
128 if (pinfo->gpios[GPIO_DCD] >= 0) {
129 if (gpio_get_value(pinfo->gpios[GPIO_DCD]))
130 mctrl &= ~TIOCM_CAR;
131 }
132
133 if (pinfo->gpios[GPIO_RI] >= 0) {
134 if (!gpio_get_value(pinfo->gpios[GPIO_RI]))
135 mctrl |= TIOCM_RNG;
136 }
137
138 return mctrl;
139}
140
141/*
142 * Stop transmitter
143 */
144static void cpm_uart_stop_tx(struct uart_port *port)
145{
146 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
147 smc_t __iomem *smcp = pinfo->smcp;
148 scc_t __iomem *sccp = pinfo->sccp;
149
150 pr_debug("CPM uart[%d]:stop tx\n", port->line);
151
152 if (IS_SMC(pinfo))
153 clrbits8(&smcp->smc_smcm, SMCM_TX);
154 else
155 clrbits16(&sccp->scc_sccm, UART_SCCM_TX);
156}
157
158/*
159 * Start transmitter
160 */
161static void cpm_uart_start_tx(struct uart_port *port)
162{
163 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
164 smc_t __iomem *smcp = pinfo->smcp;
165 scc_t __iomem *sccp = pinfo->sccp;
166
167 pr_debug("CPM uart[%d]:start tx\n", port->line);
168
169 if (IS_SMC(pinfo)) {
170 if (in_8(&smcp->smc_smcm) & SMCM_TX)
171 return;
172 } else {
173 if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)
174 return;
175 }
176
177 if (cpm_uart_tx_pump(port) != 0) {
178 if (IS_SMC(pinfo)) {
179 setbits8(&smcp->smc_smcm, SMCM_TX);
180 } else {
181 setbits16(&sccp->scc_sccm, UART_SCCM_TX);
182 }
183 }
184}
185
186/*
187 * Stop receiver
188 */
189static void cpm_uart_stop_rx(struct uart_port *port)
190{
191 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
192 smc_t __iomem *smcp = pinfo->smcp;
193 scc_t __iomem *sccp = pinfo->sccp;
194
195 pr_debug("CPM uart[%d]:stop rx\n", port->line);
196
197 if (IS_SMC(pinfo))
198 clrbits8(&smcp->smc_smcm, SMCM_RX);
199 else
200 clrbits16(&sccp->scc_sccm, UART_SCCM_RX);
201}
202
203/*
204 * Enable Modem status interrupts
205 */
206static void cpm_uart_enable_ms(struct uart_port *port)
207{
208 pr_debug("CPM uart[%d]:enable ms\n", port->line);
209}
210
211/*
212 * Generate a break.
213 */
214static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
215{
216 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
217
218 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
219 break_state);
220
221 if (break_state)
222 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
223 else
224 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
225}
226
227/*
228 * Transmit characters, refill buffer descriptor, if possible
229 */
230static void cpm_uart_int_tx(struct uart_port *port)
231{
232 pr_debug("CPM uart[%d]:TX INT\n", port->line);
233
234 cpm_uart_tx_pump(port);
235}
236
237#ifdef CONFIG_CONSOLE_POLL
238static int serial_polled;
239#endif
240
241/*
242 * Receive characters
243 */
244static void cpm_uart_int_rx(struct uart_port *port)
245{
246 int i;
247 unsigned char ch;
248 u8 *cp;
249 struct tty_struct *tty = port->state->port.tty;
250 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
251 cbd_t __iomem *bdp;
252 u16 status;
253 unsigned int flg;
254
255 pr_debug("CPM uart[%d]:RX INT\n", port->line);
256
257 /* Just loop through the closed BDs and copy the characters into
258 * the buffer.
259 */
260 bdp = pinfo->rx_cur;
261 for (;;) {
262#ifdef CONFIG_CONSOLE_POLL
263 if (unlikely(serial_polled)) {
264 serial_polled = 0;
265 return;
266 }
267#endif
268 /* get status */
269 status = in_be16(&bdp->cbd_sc);
270 /* If this one is empty, return happy */
271 if (status & BD_SC_EMPTY)
272 break;
273
274 /* get number of characters, and check spce in flip-buffer */
275 i = in_be16(&bdp->cbd_datlen);
276
277 /* If we have not enough room in tty flip buffer, then we try
278 * later, which will be the next rx-interrupt or a timeout
279 */
280 if(tty_buffer_request_room(tty, i) < i) {
281 printk(KERN_WARNING "No room in flip buffer\n");
282 return;
283 }
284
285 /* get pointer */
286 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
287
288 /* loop through the buffer */
289 while (i-- > 0) {
290 ch = *cp++;
291 port->icount.rx++;
292 flg = TTY_NORMAL;
293
294 if (status &
295 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
296 goto handle_error;
297 if (uart_handle_sysrq_char(port, ch))
298 continue;
299#ifdef CONFIG_CONSOLE_POLL
300 if (unlikely(serial_polled)) {
301 serial_polled = 0;
302 return;
303 }
304#endif
305 error_return:
306 tty_insert_flip_char(tty, ch, flg);
307
308 } /* End while (i--) */
309
310 /* This BD is ready to be used again. Clear status. get next */
311 clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
312 BD_SC_OV | BD_SC_ID);
313 setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
314
315 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
316 bdp = pinfo->rx_bd_base;
317 else
318 bdp++;
319
320 } /* End for (;;) */
321
322 /* Write back buffer pointer */
323 pinfo->rx_cur = bdp;
324
325 /* activate BH processing */
326 tty_flip_buffer_push(tty);
327
328 return;
329
330 /* Error processing */
331
332 handle_error:
333 /* Statistics */
334 if (status & BD_SC_BR)
335 port->icount.brk++;
336 if (status & BD_SC_PR)
337 port->icount.parity++;
338 if (status & BD_SC_FR)
339 port->icount.frame++;
340 if (status & BD_SC_OV)
341 port->icount.overrun++;
342
343 /* Mask out ignored conditions */
344 status &= port->read_status_mask;
345
346 /* Handle the remaining ones */
347 if (status & BD_SC_BR)
348 flg = TTY_BREAK;
349 else if (status & BD_SC_PR)
350 flg = TTY_PARITY;
351 else if (status & BD_SC_FR)
352 flg = TTY_FRAME;
353
354 /* overrun does not affect the current character ! */
355 if (status & BD_SC_OV) {
356 ch = 0;
357 flg = TTY_OVERRUN;
358 /* We skip this buffer */
359 /* CHECK: Is really nothing senseful there */
360 /* ASSUMPTION: it contains nothing valid */
361 i = 0;
362 }
363#ifdef SUPPORT_SYSRQ
364 port->sysrq = 0;
365#endif
366 goto error_return;
367}
368
369/*
370 * Asynchron mode interrupt handler
371 */
372static irqreturn_t cpm_uart_int(int irq, void *data)
373{
374 u8 events;
375 struct uart_port *port = data;
376 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
377 smc_t __iomem *smcp = pinfo->smcp;
378 scc_t __iomem *sccp = pinfo->sccp;
379
380 pr_debug("CPM uart[%d]:IRQ\n", port->line);
381
382 if (IS_SMC(pinfo)) {
383 events = in_8(&smcp->smc_smce);
384 out_8(&smcp->smc_smce, events);
385 if (events & SMCM_BRKE)
386 uart_handle_break(port);
387 if (events & SMCM_RX)
388 cpm_uart_int_rx(port);
389 if (events & SMCM_TX)
390 cpm_uart_int_tx(port);
391 } else {
392 events = in_be16(&sccp->scc_scce);
393 out_be16(&sccp->scc_scce, events);
394 if (events & UART_SCCM_BRKE)
395 uart_handle_break(port);
396 if (events & UART_SCCM_RX)
397 cpm_uart_int_rx(port);
398 if (events & UART_SCCM_TX)
399 cpm_uart_int_tx(port);
400 }
401 return (events) ? IRQ_HANDLED : IRQ_NONE;
402}
403
404static int cpm_uart_startup(struct uart_port *port)
405{
406 int retval;
407 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
408
409 pr_debug("CPM uart[%d]:startup\n", port->line);
410
411 /* If the port is not the console, make sure rx is disabled. */
412 if (!(pinfo->flags & FLAG_CONSOLE)) {
413 /* Disable UART rx */
414 if (IS_SMC(pinfo)) {
415 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN);
416 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
417 } else {
418 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR);
419 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
420 }
421 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
422 }
423 /* Install interrupt handler. */
424 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
425 if (retval)
426 return retval;
427
428 /* Startup rx-int */
429 if (IS_SMC(pinfo)) {
430 setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
431 setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
432 } else {
433 setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
434 setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
435 }
436
437 return 0;
438}
439
440inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
441{
442 set_current_state(TASK_UNINTERRUPTIBLE);
443 schedule_timeout(pinfo->wait_closing);
444}
445
446/*
447 * Shutdown the uart
448 */
449static void cpm_uart_shutdown(struct uart_port *port)
450{
451 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
452
453 pr_debug("CPM uart[%d]:shutdown\n", port->line);
454
455 /* free interrupt handler */
456 free_irq(port->irq, port);
457
458 /* If the port is not the console, disable Rx and Tx. */
459 if (!(pinfo->flags & FLAG_CONSOLE)) {
460 /* Wait for all the BDs marked sent */
461 while(!cpm_uart_tx_empty(port)) {
462 set_current_state(TASK_UNINTERRUPTIBLE);
463 schedule_timeout(2);
464 }
465
466 if (pinfo->wait_closing)
467 cpm_uart_wait_until_send(pinfo);
468
469 /* Stop uarts */
470 if (IS_SMC(pinfo)) {
471 smc_t __iomem *smcp = pinfo->smcp;
472 clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
473 clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
474 } else {
475 scc_t __iomem *sccp = pinfo->sccp;
476 clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
477 clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
478 }
479
480 /* Shut them really down and reinit buffer descriptors */
481 if (IS_SMC(pinfo)) {
482 out_be16(&pinfo->smcup->smc_brkcr, 0);
483 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
484 } else {
485 out_be16(&pinfo->sccup->scc_brkcr, 0);
486 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
487 }
488
489 cpm_uart_initbd(pinfo);
490 }
491}
492
493static void cpm_uart_set_termios(struct uart_port *port,
494 struct ktermios *termios,
495 struct ktermios *old)
496{
497 int baud;
498 unsigned long flags;
499 u16 cval, scval, prev_mode;
500 int bits, sbits;
501 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
502 smc_t __iomem *smcp = pinfo->smcp;
503 scc_t __iomem *sccp = pinfo->sccp;
504
505 pr_debug("CPM uart[%d]:set_termios\n", port->line);
506
507 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
508 if (baud <= HW_BUF_SPD_THRESHOLD ||
509 (pinfo->port.state && pinfo->port.state->port.tty->low_latency))
510 pinfo->rx_fifosize = 1;
511 else
512 pinfo->rx_fifosize = RX_BUF_SIZE;
513
514 /* Character length programmed into the mode register is the
515 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
516 * 1 or 2 stop bits, minus 1.
517 * The value 'bits' counts this for us.
518 */
519 cval = 0;
520 scval = 0;
521
522 /* byte size */
523 switch (termios->c_cflag & CSIZE) {
524 case CS5:
525 bits = 5;
526 break;
527 case CS6:
528 bits = 6;
529 break;
530 case CS7:
531 bits = 7;
532 break;
533 case CS8:
534 bits = 8;
535 break;
536 /* Never happens, but GCC is too dumb to figure it out */
537 default:
538 bits = 8;
539 break;
540 }
541 sbits = bits - 5;
542
543 if (termios->c_cflag & CSTOPB) {
544 cval |= SMCMR_SL; /* Two stops */
545 scval |= SCU_PSMR_SL;
546 bits++;
547 }
548
549 if (termios->c_cflag & PARENB) {
550 cval |= SMCMR_PEN;
551 scval |= SCU_PSMR_PEN;
552 bits++;
553 if (!(termios->c_cflag & PARODD)) {
554 cval |= SMCMR_PM_EVEN;
555 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
556 }
557 }
558
559 /*
560 * Update the timeout
561 */
562 uart_update_timeout(port, termios->c_cflag, baud);
563
564 /*
565 * Set up parity check flag
566 */
567#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
568
569 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
570 if (termios->c_iflag & INPCK)
571 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
572 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
573 port->read_status_mask |= BD_SC_BR;
574
575 /*
576 * Characters to ignore
577 */
578 port->ignore_status_mask = 0;
579 if (termios->c_iflag & IGNPAR)
580 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
581 if (termios->c_iflag & IGNBRK) {
582 port->ignore_status_mask |= BD_SC_BR;
583 /*
584 * If we're ignore parity and break indicators, ignore
585 * overruns too. (For real raw support).
586 */
587 if (termios->c_iflag & IGNPAR)
588 port->ignore_status_mask |= BD_SC_OV;
589 }
590 /*
591 * !!! ignore all characters if CREAD is not set
592 */
593 if ((termios->c_cflag & CREAD) == 0)
594 port->read_status_mask &= ~BD_SC_EMPTY;
595
596 spin_lock_irqsave(&port->lock, flags);
597
598 /* Start bit has not been added (so don't, because we would just
599 * subtract it later), and we need to add one for the number of
600 * stops bits (there is always at least one).
601 */
602 bits++;
603 if (IS_SMC(pinfo)) {
604 /*
605 * MRBLR can be changed while an SMC/SCC is operating only
606 * if it is done in a single bus cycle with one 16-bit move
607 * (not two 8-bit bus cycles back-to-back). This occurs when
608 * the cp shifts control to the next RxBD, so the change does
609 * not take effect immediately. To guarantee the exact RxBD
610 * on which the change occurs, change MRBLR only while the
611 * SMC/SCC receiver is disabled.
612 */
613 out_be16(&pinfo->smcup->smc_mrblr, pinfo->rx_fifosize);
614
615 /* Set the mode register. We want to keep a copy of the
616 * enables, because we want to put them back if they were
617 * present.
618 */
619 prev_mode = in_be16(&smcp->smc_smcmr) & (SMCMR_REN | SMCMR_TEN);
620 /* Output in *one* operation, so we don't interrupt RX/TX if they
621 * were already enabled. */
622 out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval |
623 SMCMR_SM_UART | prev_mode);
624 } else {
625 out_be16(&pinfo->sccup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
626 out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
627 }
628
629 if (pinfo->clk)
630 clk_set_rate(pinfo->clk, baud);
631 else
632 cpm_set_brg(pinfo->brg - 1, baud);
633 spin_unlock_irqrestore(&port->lock, flags);
634}
635
636static const char *cpm_uart_type(struct uart_port *port)
637{
638 pr_debug("CPM uart[%d]:uart_type\n", port->line);
639
640 return port->type == PORT_CPM ? "CPM UART" : NULL;
641}
642
643/*
644 * verify the new serial_struct (for TIOCSSERIAL).
645 */
646static int cpm_uart_verify_port(struct uart_port *port,
647 struct serial_struct *ser)
648{
649 int ret = 0;
650
651 pr_debug("CPM uart[%d]:verify_port\n", port->line);
652
653 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
654 ret = -EINVAL;
655 if (ser->irq < 0 || ser->irq >= nr_irqs)
656 ret = -EINVAL;
657 if (ser->baud_base < 9600)
658 ret = -EINVAL;
659 return ret;
660}
661
662/*
663 * Transmit characters, refill buffer descriptor, if possible
664 */
665static int cpm_uart_tx_pump(struct uart_port *port)
666{
667 cbd_t __iomem *bdp;
668 u8 *p;
669 int count;
670 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
671 struct circ_buf *xmit = &port->state->xmit;
672
673 /* Handle xon/xoff */
674 if (port->x_char) {
675 /* Pick next descriptor and fill from buffer */
676 bdp = pinfo->tx_cur;
677
678 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
679
680 *p++ = port->x_char;
681
682 out_be16(&bdp->cbd_datlen, 1);
683 setbits16(&bdp->cbd_sc, BD_SC_READY);
684 /* Get next BD. */
685 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
686 bdp = pinfo->tx_bd_base;
687 else
688 bdp++;
689 pinfo->tx_cur = bdp;
690
691 port->icount.tx++;
692 port->x_char = 0;
693 return 1;
694 }
695
696 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
697 cpm_uart_stop_tx(port);
698 return 0;
699 }
700
701 /* Pick next descriptor and fill from buffer */
702 bdp = pinfo->tx_cur;
703
704 while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
705 xmit->tail != xmit->head) {
706 count = 0;
707 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
708 while (count < pinfo->tx_fifosize) {
709 *p++ = xmit->buf[xmit->tail];
710 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
711 port->icount.tx++;
712 count++;
713 if (xmit->head == xmit->tail)
714 break;
715 }
716 out_be16(&bdp->cbd_datlen, count);
717 setbits16(&bdp->cbd_sc, BD_SC_READY);
718 /* Get next BD. */
719 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
720 bdp = pinfo->tx_bd_base;
721 else
722 bdp++;
723 }
724 pinfo->tx_cur = bdp;
725
726 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
727 uart_write_wakeup(port);
728
729 if (uart_circ_empty(xmit)) {
730 cpm_uart_stop_tx(port);
731 return 0;
732 }
733
734 return 1;
735}
736
737/*
738 * init buffer descriptors
739 */
740static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
741{
742 int i;
743 u8 *mem_addr;
744 cbd_t __iomem *bdp;
745
746 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
747
748 /* Set the physical address of the host memory
749 * buffers in the buffer descriptors, and the
750 * virtual address for us to work with.
751 */
752 mem_addr = pinfo->mem_addr;
753 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
754 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
755 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
756 out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
757 mem_addr += pinfo->rx_fifosize;
758 }
759
760 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
761 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
762
763 /* Set the physical address of the host memory
764 * buffers in the buffer descriptors, and the
765 * virtual address for us to work with.
766 */
767 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
768 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
769 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
770 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
771 out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
772 mem_addr += pinfo->tx_fifosize;
773 }
774
775 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
776 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
777}
778
779static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
780{
781 scc_t __iomem *scp;
782 scc_uart_t __iomem *sup;
783
784 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
785
786 scp = pinfo->sccp;
787 sup = pinfo->sccup;
788
789 /* Store address */
790 out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
791 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
792 out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
793 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
794
795 /* Set up the uart parameters in the
796 * parameter ram.
797 */
798
799 cpm_set_scc_fcr(sup);
800
801 out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
802 out_be16(&sup->scc_maxidl, pinfo->rx_fifosize);
803 out_be16(&sup->scc_brkcr, 1);
804 out_be16(&sup->scc_parec, 0);
805 out_be16(&sup->scc_frmec, 0);
806 out_be16(&sup->scc_nosec, 0);
807 out_be16(&sup->scc_brkec, 0);
808 out_be16(&sup->scc_uaddr1, 0);
809 out_be16(&sup->scc_uaddr2, 0);
810 out_be16(&sup->scc_toseq, 0);
811 out_be16(&sup->scc_char1, 0x8000);
812 out_be16(&sup->scc_char2, 0x8000);
813 out_be16(&sup->scc_char3, 0x8000);
814 out_be16(&sup->scc_char4, 0x8000);
815 out_be16(&sup->scc_char5, 0x8000);
816 out_be16(&sup->scc_char6, 0x8000);
817 out_be16(&sup->scc_char7, 0x8000);
818 out_be16(&sup->scc_char8, 0x8000);
819 out_be16(&sup->scc_rccm, 0xc0ff);
820
821 /* Send the CPM an initialize command.
822 */
823 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
824
825 /* Set UART mode, 8 bit, no parity, one stop.
826 * Enable receive and transmit.
827 */
828 out_be32(&scp->scc_gsmrh, 0);
829 out_be32(&scp->scc_gsmrl,
830 SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
831
832 /* Enable rx interrupts and clear all pending events. */
833 out_be16(&scp->scc_sccm, 0);
834 out_be16(&scp->scc_scce, 0xffff);
835 out_be16(&scp->scc_dsr, 0x7e7e);
836 out_be16(&scp->scc_psmr, 0x3000);
837
838 setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
839}
840
841static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
842{
843 smc_t __iomem *sp;
844 smc_uart_t __iomem *up;
845
846 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
847
848 sp = pinfo->smcp;
849 up = pinfo->smcup;
850
851 /* Store address */
852 out_be16(&pinfo->smcup->smc_rbase,
853 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
854 out_be16(&pinfo->smcup->smc_tbase,
855 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
856
857/*
858 * In case SMC1 is being relocated...
859 */
860#if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
861 out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
862 out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
863 out_be32(&up->smc_rstate, 0);
864 out_be32(&up->smc_tstate, 0);
865 out_be16(&up->smc_brkcr, 1); /* number of break chars */
866 out_be16(&up->smc_brkec, 0);
867#endif
868
869 /* Set up the uart parameters in the
870 * parameter ram.
871 */
872 cpm_set_smc_fcr(up);
873
874 /* Using idle character time requires some additional tuning. */
875 out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
876 out_be16(&up->smc_maxidl, pinfo->rx_fifosize);
877 out_be16(&up->smc_brklen, 0);
878 out_be16(&up->smc_brkec, 0);
879 out_be16(&up->smc_brkcr, 1);
880
881 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
882
883 /* Set UART mode, 8 bit, no parity, one stop.
884 * Enable receive and transmit.
885 */
886 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
887
888 /* Enable only rx interrupts clear all pending events. */
889 out_8(&sp->smc_smcm, 0);
890 out_8(&sp->smc_smce, 0xff);
891
892 setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
893}
894
895/*
896 * Initialize port. This is called from early_console stuff
897 * so we have to be careful here !
898 */
899static int cpm_uart_request_port(struct uart_port *port)
900{
901 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
902 int ret;
903
904 pr_debug("CPM uart[%d]:request port\n", port->line);
905
906 if (pinfo->flags & FLAG_CONSOLE)
907 return 0;
908
909 if (IS_SMC(pinfo)) {
910 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
911 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
912 } else {
913 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
914 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
915 }
916
917 ret = cpm_uart_allocbuf(pinfo, 0);
918
919 if (ret)
920 return ret;
921
922 cpm_uart_initbd(pinfo);
923 if (IS_SMC(pinfo))
924 cpm_uart_init_smc(pinfo);
925 else
926 cpm_uart_init_scc(pinfo);
927
928 return 0;
929}
930
931static void cpm_uart_release_port(struct uart_port *port)
932{
933 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
934
935 if (!(pinfo->flags & FLAG_CONSOLE))
936 cpm_uart_freebuf(pinfo);
937}
938
939/*
940 * Configure/autoconfigure the port.
941 */
942static void cpm_uart_config_port(struct uart_port *port, int flags)
943{
944 pr_debug("CPM uart[%d]:config_port\n", port->line);
945
946 if (flags & UART_CONFIG_TYPE) {
947 port->type = PORT_CPM;
948 cpm_uart_request_port(port);
949 }
950}
951
952#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_CPM_CONSOLE)
953/*
954 * Write a string to the serial port
955 * Note that this is called with interrupts already disabled
956 */
957static void cpm_uart_early_write(struct uart_cpm_port *pinfo,
958 const char *string, u_int count)
959{
960 unsigned int i;
961 cbd_t __iomem *bdp, *bdbase;
962 unsigned char *cpm_outp_addr;
963
964 /* Get the address of the host memory buffer.
965 */
966 bdp = pinfo->tx_cur;
967 bdbase = pinfo->tx_bd_base;
968
969 /*
970 * Now, do each character. This is not as bad as it looks
971 * since this is a holding FIFO and not a transmitting FIFO.
972 * We could add the complexity of filling the entire transmit
973 * buffer, but we would just wait longer between accesses......
974 */
975 for (i = 0; i < count; i++, string++) {
976 /* Wait for transmitter fifo to empty.
977 * Ready indicates output is ready, and xmt is doing
978 * that, not that it is ready for us to send.
979 */
980 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
981 ;
982
983 /* Send the character out.
984 * If the buffer address is in the CPM DPRAM, don't
985 * convert it.
986 */
987 cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
988 pinfo);
989 *cpm_outp_addr = *string;
990
991 out_be16(&bdp->cbd_datlen, 1);
992 setbits16(&bdp->cbd_sc, BD_SC_READY);
993
994 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
995 bdp = bdbase;
996 else
997 bdp++;
998
999 /* if a LF, also do CR... */
1000 if (*string == 10) {
1001 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1002 ;
1003
1004 cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
1005 pinfo);
1006 *cpm_outp_addr = 13;
1007
1008 out_be16(&bdp->cbd_datlen, 1);
1009 setbits16(&bdp->cbd_sc, BD_SC_READY);
1010
1011 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1012 bdp = bdbase;
1013 else
1014 bdp++;
1015 }
1016 }
1017
1018 /*
1019 * Finally, Wait for transmitter & holding register to empty
1020 * and restore the IER
1021 */
1022 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1023 ;
1024
1025 pinfo->tx_cur = bdp;
1026}
1027#endif
1028
1029#ifdef CONFIG_CONSOLE_POLL
1030/* Serial polling routines for writing and reading from the uart while
1031 * in an interrupt or debug context.
1032 */
1033
1034#define GDB_BUF_SIZE 512 /* power of 2, please */
1035
1036static char poll_buf[GDB_BUF_SIZE];
1037static char *pollp;
1038static int poll_chars;
1039
1040static int poll_wait_key(char *obuf, struct uart_cpm_port *pinfo)
1041{
1042 u_char c, *cp;
1043 volatile cbd_t *bdp;
1044 int i;
1045
1046 /* Get the address of the host memory buffer.
1047 */
1048 bdp = pinfo->rx_cur;
1049 while (bdp->cbd_sc & BD_SC_EMPTY)
1050 ;
1051
1052 /* If the buffer address is in the CPM DPRAM, don't
1053 * convert it.
1054 */
1055 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1056
1057 if (obuf) {
1058 i = c = bdp->cbd_datlen;
1059 while (i-- > 0)
1060 *obuf++ = *cp++;
1061 } else
1062 c = *cp;
1063 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
1064 bdp->cbd_sc |= BD_SC_EMPTY;
1065
1066 if (bdp->cbd_sc & BD_SC_WRAP)
1067 bdp = pinfo->rx_bd_base;
1068 else
1069 bdp++;
1070 pinfo->rx_cur = (cbd_t *)bdp;
1071
1072 return (int)c;
1073}
1074
1075static int cpm_get_poll_char(struct uart_port *port)
1076{
1077 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
1078
1079 if (!serial_polled) {
1080 serial_polled = 1;
1081 poll_chars = 0;
1082 }
1083 if (poll_chars <= 0) {
1084 poll_chars = poll_wait_key(poll_buf, pinfo);
1085 pollp = poll_buf;
1086 }
1087 poll_chars--;
1088 return *pollp++;
1089}
1090
1091static void cpm_put_poll_char(struct uart_port *port,
1092 unsigned char c)
1093{
1094 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
1095 static char ch[2];
1096
1097 ch[0] = (char)c;
1098 cpm_uart_early_write(pinfo, ch, 1);
1099}
1100#endif /* CONFIG_CONSOLE_POLL */
1101
1102static struct uart_ops cpm_uart_pops = {
1103 .tx_empty = cpm_uart_tx_empty,
1104 .set_mctrl = cpm_uart_set_mctrl,
1105 .get_mctrl = cpm_uart_get_mctrl,
1106 .stop_tx = cpm_uart_stop_tx,
1107 .start_tx = cpm_uart_start_tx,
1108 .stop_rx = cpm_uart_stop_rx,
1109 .enable_ms = cpm_uart_enable_ms,
1110 .break_ctl = cpm_uart_break_ctl,
1111 .startup = cpm_uart_startup,
1112 .shutdown = cpm_uart_shutdown,
1113 .set_termios = cpm_uart_set_termios,
1114 .type = cpm_uart_type,
1115 .release_port = cpm_uart_release_port,
1116 .request_port = cpm_uart_request_port,
1117 .config_port = cpm_uart_config_port,
1118 .verify_port = cpm_uart_verify_port,
1119#ifdef CONFIG_CONSOLE_POLL
1120 .poll_get_char = cpm_get_poll_char,
1121 .poll_put_char = cpm_put_poll_char,
1122#endif
1123};
1124
1125struct uart_cpm_port cpm_uart_ports[UART_NR];
1126
1127static int cpm_uart_init_port(struct device_node *np,
1128 struct uart_cpm_port *pinfo)
1129{
1130 const u32 *data;
1131 void __iomem *mem, *pram;
1132 int len;
1133 int ret;
1134 int i;
1135
1136 data = of_get_property(np, "clock", NULL);
1137 if (data) {
1138 struct clk *clk = clk_get(NULL, (const char*)data);
1139 if (!IS_ERR(clk))
1140 pinfo->clk = clk;
1141 }
1142 if (!pinfo->clk) {
1143 data = of_get_property(np, "fsl,cpm-brg", &len);
1144 if (!data || len != 4) {
1145 printk(KERN_ERR "CPM UART %s has no/invalid "
1146 "fsl,cpm-brg property.\n", np->name);
1147 return -EINVAL;
1148 }
1149 pinfo->brg = *data;
1150 }
1151
1152 data = of_get_property(np, "fsl,cpm-command", &len);
1153 if (!data || len != 4) {
1154 printk(KERN_ERR "CPM UART %s has no/invalid "
1155 "fsl,cpm-command property.\n", np->name);
1156 return -EINVAL;
1157 }
1158 pinfo->command = *data;
1159
1160 mem = of_iomap(np, 0);
1161 if (!mem)
1162 return -ENOMEM;
1163
1164 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
1165 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
1166 pinfo->sccp = mem;
1167 pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
1168 } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
1169 of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
1170 pinfo->flags |= FLAG_SMC;
1171 pinfo->smcp = mem;
1172 pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
1173 } else {
1174 ret = -ENODEV;
1175 goto out_mem;
1176 }
1177
1178 if (!pram) {
1179 ret = -ENOMEM;
1180 goto out_mem;
1181 }
1182
1183 pinfo->tx_nrfifos = TX_NUM_FIFO;
1184 pinfo->tx_fifosize = TX_BUF_SIZE;
1185 pinfo->rx_nrfifos = RX_NUM_FIFO;
1186 pinfo->rx_fifosize = RX_BUF_SIZE;
1187
1188 pinfo->port.uartclk = ppc_proc_freq;
1189 pinfo->port.mapbase = (unsigned long)mem;
1190 pinfo->port.type = PORT_CPM;
1191 pinfo->port.ops = &cpm_uart_pops,
1192 pinfo->port.iotype = UPIO_MEM;
1193 pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
1194 spin_lock_init(&pinfo->port.lock);
1195
1196 pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
1197 if (pinfo->port.irq == NO_IRQ) {
1198 ret = -EINVAL;
1199 goto out_pram;
1200 }
1201
1202 for (i = 0; i < NUM_GPIOS; i++)
1203 pinfo->gpios[i] = of_get_gpio(np, i);
1204
1205#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1206 udbg_putc = NULL;
1207#endif
1208
1209 return cpm_uart_request_port(&pinfo->port);
1210
1211out_pram:
1212 cpm_uart_unmap_pram(pinfo, pram);
1213out_mem:
1214 iounmap(mem);
1215 return ret;
1216}
1217
1218#ifdef CONFIG_SERIAL_CPM_CONSOLE
1219/*
1220 * Print a string to the serial port trying not to disturb
1221 * any possible real use of the port...
1222 *
1223 * Note that this is called with interrupts already disabled
1224 */
1225static void cpm_uart_console_write(struct console *co, const char *s,
1226 u_int count)
1227{
1228 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
1229 unsigned long flags;
1230 int nolock = oops_in_progress;
1231
1232 if (unlikely(nolock)) {
1233 local_irq_save(flags);
1234 } else {
1235 spin_lock_irqsave(&pinfo->port.lock, flags);
1236 }
1237
1238 cpm_uart_early_write(pinfo, s, count);
1239
1240 if (unlikely(nolock)) {
1241 local_irq_restore(flags);
1242 } else {
1243 spin_unlock_irqrestore(&pinfo->port.lock, flags);
1244 }
1245}
1246
1247
1248static int __init cpm_uart_console_setup(struct console *co, char *options)
1249{
1250 int baud = 38400;
1251 int bits = 8;
1252 int parity = 'n';
1253 int flow = 'n';
1254 int ret;
1255 struct uart_cpm_port *pinfo;
1256 struct uart_port *port;
1257
1258 struct device_node *np = NULL;
1259 int i = 0;
1260
1261 if (co->index >= UART_NR) {
1262 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1263 co->index);
1264 return -ENODEV;
1265 }
1266
1267 do {
1268 np = of_find_node_by_type(np, "serial");
1269 if (!np)
1270 return -ENODEV;
1271
1272 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1273 !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1274 !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1275 !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1276 i--;
1277 } while (i++ != co->index);
1278
1279 pinfo = &cpm_uart_ports[co->index];
1280
1281 pinfo->flags |= FLAG_CONSOLE;
1282 port = &pinfo->port;
1283
1284 ret = cpm_uart_init_port(np, pinfo);
1285 of_node_put(np);
1286 if (ret)
1287 return ret;
1288
1289 if (options) {
1290 uart_parse_options(options, &baud, &parity, &bits, &flow);
1291 } else {
1292 if ((baud = uart_baudrate()) == -1)
1293 baud = 9600;
1294 }
1295
1296 if (IS_SMC(pinfo)) {
1297 out_be16(&pinfo->smcup->smc_brkcr, 0);
1298 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
1299 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
1300 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
1301 } else {
1302 out_be16(&pinfo->sccup->scc_brkcr, 0);
1303 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
1304 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
1305 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1306 }
1307
1308 ret = cpm_uart_allocbuf(pinfo, 1);
1309
1310 if (ret)
1311 return ret;
1312
1313 cpm_uart_initbd(pinfo);
1314
1315 if (IS_SMC(pinfo))
1316 cpm_uart_init_smc(pinfo);
1317 else
1318 cpm_uart_init_scc(pinfo);
1319
1320 uart_set_options(port, co, baud, parity, bits, flow);
1321 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
1322
1323 return 0;
1324}
1325
1326static struct uart_driver cpm_reg;
1327static struct console cpm_scc_uart_console = {
1328 .name = "ttyCPM",
1329 .write = cpm_uart_console_write,
1330 .device = uart_console_device,
1331 .setup = cpm_uart_console_setup,
1332 .flags = CON_PRINTBUFFER,
1333 .index = -1,
1334 .data = &cpm_reg,
1335};
1336
1337static int __init cpm_uart_console_init(void)
1338{
1339 register_console(&cpm_scc_uart_console);
1340 return 0;
1341}
1342
1343console_initcall(cpm_uart_console_init);
1344
1345#define CPM_UART_CONSOLE &cpm_scc_uart_console
1346#else
1347#define CPM_UART_CONSOLE NULL
1348#endif
1349
1350static struct uart_driver cpm_reg = {
1351 .owner = THIS_MODULE,
1352 .driver_name = "ttyCPM",
1353 .dev_name = "ttyCPM",
1354 .major = SERIAL_CPM_MAJOR,
1355 .minor = SERIAL_CPM_MINOR,
1356 .cons = CPM_UART_CONSOLE,
1357 .nr = UART_NR,
1358};
1359
1360static int probe_index;
1361
1362static int __devinit cpm_uart_probe(struct platform_device *ofdev,
1363 const struct of_device_id *match)
1364{
1365 int index = probe_index++;
1366 struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1367 int ret;
1368
1369 pinfo->port.line = index;
1370
1371 if (index >= UART_NR)
1372 return -ENODEV;
1373
1374 dev_set_drvdata(&ofdev->dev, pinfo);
1375
1376 /* initialize the device pointer for the port */
1377 pinfo->port.dev = &ofdev->dev;
1378
1379 ret = cpm_uart_init_port(ofdev->dev.of_node, pinfo);
1380 if (ret)
1381 return ret;
1382
1383 return uart_add_one_port(&cpm_reg, &pinfo->port);
1384}
1385
1386static int __devexit cpm_uart_remove(struct platform_device *ofdev)
1387{
1388 struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
1389 return uart_remove_one_port(&cpm_reg, &pinfo->port);
1390}
1391
1392static struct of_device_id cpm_uart_match[] = {
1393 {
1394 .compatible = "fsl,cpm1-smc-uart",
1395 },
1396 {
1397 .compatible = "fsl,cpm1-scc-uart",
1398 },
1399 {
1400 .compatible = "fsl,cpm2-smc-uart",
1401 },
1402 {
1403 .compatible = "fsl,cpm2-scc-uart",
1404 },
1405 {}
1406};
1407
1408static struct of_platform_driver cpm_uart_driver = {
1409 .driver = {
1410 .name = "cpm_uart",
1411 .owner = THIS_MODULE,
1412 .of_match_table = cpm_uart_match,
1413 },
1414 .probe = cpm_uart_probe,
1415 .remove = cpm_uart_remove,
1416 };
1417
1418static int __init cpm_uart_init(void)
1419{
1420 int ret = uart_register_driver(&cpm_reg);
1421 if (ret)
1422 return ret;
1423
1424 ret = of_register_platform_driver(&cpm_uart_driver);
1425 if (ret)
1426 uart_unregister_driver(&cpm_reg);
1427
1428 return ret;
1429}
1430
1431static void __exit cpm_uart_exit(void)
1432{
1433 of_unregister_platform_driver(&cpm_uart_driver);
1434 uart_unregister_driver(&cpm_reg);
1435}
1436
1437module_init(cpm_uart_init);
1438module_exit(cpm_uart_exit);
1439
1440MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1441MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1442MODULE_LICENSE("GPL");
1443MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c
new file mode 100644
index 000000000000..3fc1d66e32c6
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c
@@ -0,0 +1,138 @@
1/*
2 * linux/drivers/serial/cpm_uart.c
3 *
4 * Driver for CPM (SCC/SMC) serial ports; CPM1 definitions
5 *
6 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
7 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
8 *
9 * Copyright (C) 2004 Freescale Semiconductor, Inc.
10 * (C) 2004 Intracom, S.A.
11 * (C) 2006 MontaVista Software, Inc.
12 * Vitaly Bordug <vbordug@ru.mvista.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30#include <linux/module.h>
31#include <linux/tty.h>
32#include <linux/gfp.h>
33#include <linux/ioport.h>
34#include <linux/init.h>
35#include <linux/serial.h>
36#include <linux/console.h>
37#include <linux/sysrq.h>
38#include <linux/device.h>
39#include <linux/bootmem.h>
40#include <linux/dma-mapping.h>
41
42#include <asm/io.h>
43#include <asm/irq.h>
44#include <asm/fs_pd.h>
45
46#include <linux/serial_core.h>
47#include <linux/kernel.h>
48
49#include <linux/of.h>
50
51#include "cpm_uart.h"
52
53/**************************************************************/
54
55void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
56{
57 cpm_command(port->command, cmd);
58}
59
60void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
61 struct device_node *np)
62{
63 return of_iomap(np, 1);
64}
65
66void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
67{
68 iounmap(pram);
69}
70
71/*
72 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
73 * receive buffer descriptors from dual port ram, and a character
74 * buffer area from host mem. If we are allocating for the console we need
75 * to do it from bootmem
76 */
77int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
78{
79 int dpmemsz, memsz;
80 u8 *dp_mem;
81 unsigned long dp_offset;
82 u8 *mem_addr;
83 dma_addr_t dma_addr = 0;
84
85 pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
86
87 dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
88 dp_offset = cpm_dpalloc(dpmemsz, 8);
89 if (IS_ERR_VALUE(dp_offset)) {
90 printk(KERN_ERR
91 "cpm_uart_cpm1.c: could not allocate buffer descriptors\n");
92 return -ENOMEM;
93 }
94 dp_mem = cpm_dpram_addr(dp_offset);
95
96 memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
97 L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
98 if (is_con) {
99 /* was hostalloc but changed cause it blows away the */
100 /* large tlb mapping when pinning the kernel area */
101 mem_addr = (u8 *) cpm_dpram_addr(cpm_dpalloc(memsz, 8));
102 dma_addr = (u32)cpm_dpram_phys(mem_addr);
103 } else
104 mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
105 GFP_KERNEL);
106
107 if (mem_addr == NULL) {
108 cpm_dpfree(dp_offset);
109 printk(KERN_ERR
110 "cpm_uart_cpm1.c: could not allocate coherent memory\n");
111 return -ENOMEM;
112 }
113
114 pinfo->dp_addr = dp_offset;
115 pinfo->mem_addr = mem_addr; /* virtual address*/
116 pinfo->dma_addr = dma_addr; /* physical address*/
117 pinfo->mem_size = memsz;
118
119 pinfo->rx_buf = mem_addr;
120 pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
121 * pinfo->rx_fifosize);
122
123 pinfo->rx_bd_base = (cbd_t __iomem __force *)dp_mem;
124 pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
125
126 return 0;
127}
128
129void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
130{
131 dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
132 pinfo->rx_fifosize) +
133 L1_CACHE_ALIGN(pinfo->tx_nrfifos *
134 pinfo->tx_fifosize), pinfo->mem_addr,
135 pinfo->dma_addr);
136
137 cpm_dpfree(pinfo->dp_addr);
138}
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h
new file mode 100644
index 000000000000..10eecd6af6d4
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h
@@ -0,0 +1,34 @@
1/*
2 * linux/drivers/serial/cpm_uart/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/cpm1.h>
14
15static inline void cpm_set_brg(int brg, int baud)
16{
17 cpm_setbrg(brg, baud);
18}
19
20static inline void cpm_set_scc_fcr(scc_uart_t __iomem * sup)
21{
22 out_8(&sup->scc_genscc.scc_rfcr, SMC_EB);
23 out_8(&sup->scc_genscc.scc_tfcr, SMC_EB);
24}
25
26static inline void cpm_set_smc_fcr(smc_uart_t __iomem * up)
27{
28 out_8(&up->smc_rfcr, SMC_EB);
29 out_8(&up->smc_tfcr, SMC_EB);
30}
31
32#define DPRAM_BASE ((u8 __iomem __force *)cpm_dpram_addr(0))
33
34#endif
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c
new file mode 100644
index 000000000000..814ac006393f
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c
@@ -0,0 +1,174 @@
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 (galak@kernel.crashing.org) (CPM2)
7 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
8 *
9 * Copyright (C) 2004 Freescale Semiconductor, Inc.
10 * (C) 2004 Intracom, S.A.
11 * (C) 2006 MontaVista Software, Inc.
12 * Vitaly Bordug <vbordug@ru.mvista.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30#include <linux/module.h>
31#include <linux/tty.h>
32#include <linux/ioport.h>
33#include <linux/slab.h>
34#include <linux/init.h>
35#include <linux/serial.h>
36#include <linux/console.h>
37#include <linux/sysrq.h>
38#include <linux/device.h>
39#include <linux/bootmem.h>
40#include <linux/dma-mapping.h>
41
42#include <asm/io.h>
43#include <asm/irq.h>
44#include <asm/fs_pd.h>
45#include <asm/prom.h>
46
47#include <linux/serial_core.h>
48#include <linux/kernel.h>
49
50#include "cpm_uart.h"
51
52/**************************************************************/
53
54void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
55{
56 cpm_command(port->command, cmd);
57}
58
59void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
60 struct device_node *np)
61{
62 void __iomem *pram;
63 unsigned long offset;
64 struct resource res;
65 resource_size_t len;
66
67 /* Don't remap parameter RAM if it has already been initialized
68 * during console setup.
69 */
70 if (IS_SMC(port) && port->smcup)
71 return port->smcup;
72 else if (!IS_SMC(port) && port->sccup)
73 return port->sccup;
74
75 if (of_address_to_resource(np, 1, &res))
76 return NULL;
77
78 len = resource_size(&res);
79 pram = ioremap(res.start, len);
80 if (!pram)
81 return NULL;
82
83 if (!IS_SMC(port))
84 return pram;
85
86 if (len != 2) {
87 printk(KERN_WARNING "cpm_uart[%d]: device tree references "
88 "SMC pram, using boot loader/wrapper pram mapping. "
89 "Please fix your device tree to reference the pram "
90 "base register instead.\n",
91 port->port.line);
92 return pram;
93 }
94
95 offset = cpm_dpalloc(PROFF_SMC_SIZE, 64);
96 out_be16(pram, offset);
97 iounmap(pram);
98 return cpm_muram_addr(offset);
99}
100
101void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
102{
103 if (!IS_SMC(port))
104 iounmap(pram);
105}
106
107/*
108 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
109 * receive buffer descriptors from dual port ram, and a character
110 * buffer area from host mem. If we are allocating for the console we need
111 * to do it from bootmem
112 */
113int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
114{
115 int dpmemsz, memsz;
116 u8 __iomem *dp_mem;
117 unsigned long dp_offset;
118 u8 *mem_addr;
119 dma_addr_t dma_addr = 0;
120
121 pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
122
123 dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
124 dp_offset = cpm_dpalloc(dpmemsz, 8);
125 if (IS_ERR_VALUE(dp_offset)) {
126 printk(KERN_ERR
127 "cpm_uart_cpm.c: could not allocate buffer descriptors\n");
128 return -ENOMEM;
129 }
130
131 dp_mem = cpm_dpram_addr(dp_offset);
132
133 memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
134 L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
135 if (is_con) {
136 mem_addr = kzalloc(memsz, GFP_NOWAIT);
137 dma_addr = virt_to_bus(mem_addr);
138 }
139 else
140 mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
141 GFP_KERNEL);
142
143 if (mem_addr == NULL) {
144 cpm_dpfree(dp_offset);
145 printk(KERN_ERR
146 "cpm_uart_cpm.c: could not allocate coherent memory\n");
147 return -ENOMEM;
148 }
149
150 pinfo->dp_addr = dp_offset;
151 pinfo->mem_addr = mem_addr;
152 pinfo->dma_addr = dma_addr;
153 pinfo->mem_size = memsz;
154
155 pinfo->rx_buf = mem_addr;
156 pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
157 * pinfo->rx_fifosize);
158
159 pinfo->rx_bd_base = (cbd_t __iomem *)dp_mem;
160 pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
161
162 return 0;
163}
164
165void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
166{
167 dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
168 pinfo->rx_fifosize) +
169 L1_CACHE_ALIGN(pinfo->tx_nrfifos *
170 pinfo->tx_fifosize), (void __force *)pinfo->mem_addr,
171 pinfo->dma_addr);
172
173 cpm_dpfree(pinfo->dp_addr);
174}
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h
new file mode 100644
index 000000000000..7194c63dcf5f
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h
@@ -0,0 +1,34 @@
1/*
2 * linux/drivers/serial/cpm_uart/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
15static inline void cpm_set_brg(int brg, int baud)
16{
17 cpm_setbrg(brg, baud);
18}
19
20static inline void cpm_set_scc_fcr(scc_uart_t __iomem *sup)
21{
22 out_8(&sup->scc_genscc.scc_rfcr, CPMFCR_GBL | CPMFCR_EB);
23 out_8(&sup->scc_genscc.scc_tfcr, CPMFCR_GBL | CPMFCR_EB);
24}
25
26static inline void cpm_set_smc_fcr(smc_uart_t __iomem *up)
27{
28 out_8(&up->smc_rfcr, CPMFCR_GBL | CPMFCR_EB);
29 out_8(&up->smc_tfcr, CPMFCR_GBL | CPMFCR_EB);
30}
31
32#define DPRAM_BASE ((u8 __iomem __force *)cpm_dpram_addr(0))
33
34#endif
diff --git a/drivers/tty/serial/crisv10.c b/drivers/tty/serial/crisv10.c
new file mode 100644
index 000000000000..bcc31f2140ac
--- /dev/null
+++ b/drivers/tty/serial/crisv10.c
@@ -0,0 +1,4573 @@
1/*
2 * Serial port driver for the ETRAX 100LX chip
3 *
4 * Copyright (C) 1998-2007 Axis Communications AB
5 *
6 * Many, many authors. Based once upon a time on serial.c for 16x50.
7 *
8 */
9
10static char *serial_version = "$Revision: 1.25 $";
11
12#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/signal.h>
15#include <linux/sched.h>
16#include <linux/timer.h>
17#include <linux/interrupt.h>
18#include <linux/tty.h>
19#include <linux/tty_flip.h>
20#include <linux/major.h>
21#include <linux/string.h>
22#include <linux/fcntl.h>
23#include <linux/mm.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/kernel.h>
27#include <linux/mutex.h>
28#include <linux/bitops.h>
29#include <linux/seq_file.h>
30#include <linux/delay.h>
31#include <linux/module.h>
32#include <linux/uaccess.h>
33#include <linux/io.h>
34
35#include <asm/irq.h>
36#include <asm/dma.h>
37#include <asm/system.h>
38
39#include <arch/svinto.h>
40
41/* non-arch dependent serial structures are in linux/serial.h */
42#include <linux/serial.h>
43/* while we keep our own stuff (struct e100_serial) in a local .h file */
44#include "crisv10.h"
45#include <asm/fasttimer.h>
46#include <arch/io_interface_mux.h>
47
48#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
49#ifndef CONFIG_ETRAX_FAST_TIMER
50#error "Enable FAST_TIMER to use SERIAL_FAST_TIMER"
51#endif
52#endif
53
54#if defined(CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS) && \
55 (CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS == 0)
56#error "RX_TIMEOUT_TICKS == 0 not allowed, use 1"
57#endif
58
59#if defined(CONFIG_ETRAX_RS485_ON_PA) && defined(CONFIG_ETRAX_RS485_ON_PORT_G)
60#error "Disable either CONFIG_ETRAX_RS485_ON_PA or CONFIG_ETRAX_RS485_ON_PORT_G"
61#endif
62
63/*
64 * All of the compatibilty code so we can compile serial.c against
65 * older kernels is hidden in serial_compat.h
66 */
67#if defined(LOCAL_HEADERS)
68#include "serial_compat.h"
69#endif
70
71struct tty_driver *serial_driver;
72
73/* number of characters left in xmit buffer before we ask for more */
74#define WAKEUP_CHARS 256
75
76//#define SERIAL_DEBUG_INTR
77//#define SERIAL_DEBUG_OPEN
78//#define SERIAL_DEBUG_FLOW
79//#define SERIAL_DEBUG_DATA
80//#define SERIAL_DEBUG_THROTTLE
81//#define SERIAL_DEBUG_IO /* Debug for Extra control and status pins */
82//#define SERIAL_DEBUG_LINE 0 /* What serport we want to debug */
83
84/* Enable this to use serial interrupts to handle when you
85 expect the first received event on the serial port to
86 be an error, break or similar. Used to be able to flash IRMA
87 from eLinux */
88#define SERIAL_HANDLE_EARLY_ERRORS
89
90/* Currently 16 descriptors x 128 bytes = 2048 bytes */
91#define SERIAL_DESCR_BUF_SIZE 256
92
93#define SERIAL_PRESCALE_BASE 3125000 /* 3.125MHz */
94#define DEF_BAUD_BASE SERIAL_PRESCALE_BASE
95
96/* We don't want to load the system with massive fast timer interrupt
97 * on high baudrates so limit it to 250 us (4kHz) */
98#define MIN_FLUSH_TIME_USEC 250
99
100/* Add an x here to log a lot of timer stuff */
101#define TIMERD(x)
102/* Debug details of interrupt handling */
103#define DINTR1(x) /* irq on/off, errors */
104#define DINTR2(x) /* tx and rx */
105/* Debug flip buffer stuff */
106#define DFLIP(x)
107/* Debug flow control and overview of data flow */
108#define DFLOW(x)
109#define DBAUD(x)
110#define DLOG_INT_TRIG(x)
111
112//#define DEBUG_LOG_INCLUDED
113#ifndef DEBUG_LOG_INCLUDED
114#define DEBUG_LOG(line, string, value)
115#else
116struct debug_log_info
117{
118 unsigned long time;
119 unsigned long timer_data;
120// int line;
121 const char *string;
122 int value;
123};
124#define DEBUG_LOG_SIZE 4096
125
126struct debug_log_info debug_log[DEBUG_LOG_SIZE];
127int debug_log_pos = 0;
128
129#define DEBUG_LOG(_line, _string, _value) do { \
130 if ((_line) == SERIAL_DEBUG_LINE) {\
131 debug_log_func(_line, _string, _value); \
132 }\
133}while(0)
134
135void debug_log_func(int line, const char *string, int value)
136{
137 if (debug_log_pos < DEBUG_LOG_SIZE) {
138 debug_log[debug_log_pos].time = jiffies;
139 debug_log[debug_log_pos].timer_data = *R_TIMER_DATA;
140// debug_log[debug_log_pos].line = line;
141 debug_log[debug_log_pos].string = string;
142 debug_log[debug_log_pos].value = value;
143 debug_log_pos++;
144 }
145 /*printk(string, value);*/
146}
147#endif
148
149#ifndef CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
150/* Default number of timer ticks before flushing rx fifo
151 * When using "little data, low latency applications: use 0
152 * When using "much data applications (PPP)" use ~5
153 */
154#define CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS 5
155#endif
156
157unsigned long timer_data_to_ns(unsigned long timer_data);
158
159static void change_speed(struct e100_serial *info);
160static void rs_throttle(struct tty_struct * tty);
161static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
162static int rs_write(struct tty_struct *tty,
163 const unsigned char *buf, int count);
164#ifdef CONFIG_ETRAX_RS485
165static int e100_write_rs485(struct tty_struct *tty,
166 const unsigned char *buf, int count);
167#endif
168static int get_lsr_info(struct e100_serial *info, unsigned int *value);
169
170
171#define DEF_BAUD 115200 /* 115.2 kbit/s */
172#define STD_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
173#define DEF_RX 0x20 /* or SERIAL_CTRL_W >> 8 */
174/* Default value of tx_ctrl register: has txd(bit 7)=1 (idle) as default */
175#define DEF_TX 0x80 /* or SERIAL_CTRL_B */
176
177/* offsets from R_SERIALx_CTRL */
178
179#define REG_DATA 0
180#define REG_DATA_STATUS32 0 /* this is the 32 bit register R_SERIALx_READ */
181#define REG_TR_DATA 0
182#define REG_STATUS 1
183#define REG_TR_CTRL 1
184#define REG_REC_CTRL 2
185#define REG_BAUD 3
186#define REG_XOFF 4 /* this is a 32 bit register */
187
188/* The bitfields are the same for all serial ports */
189#define SER_RXD_MASK IO_MASK(R_SERIAL0_STATUS, rxd)
190#define SER_DATA_AVAIL_MASK IO_MASK(R_SERIAL0_STATUS, data_avail)
191#define SER_FRAMING_ERR_MASK IO_MASK(R_SERIAL0_STATUS, framing_err)
192#define SER_PAR_ERR_MASK IO_MASK(R_SERIAL0_STATUS, par_err)
193#define SER_OVERRUN_MASK IO_MASK(R_SERIAL0_STATUS, overrun)
194
195#define SER_ERROR_MASK (SER_OVERRUN_MASK | SER_PAR_ERR_MASK | SER_FRAMING_ERR_MASK)
196
197/* Values for info->errorcode */
198#define ERRCODE_SET_BREAK (TTY_BREAK)
199#define ERRCODE_INSERT 0x100
200#define ERRCODE_INSERT_BREAK (ERRCODE_INSERT | TTY_BREAK)
201
202#define FORCE_EOP(info) *R_SET_EOP = 1U << info->iseteop;
203
204/*
205 * General note regarding the use of IO_* macros in this file:
206 *
207 * We will use the bits defined for DMA channel 6 when using various
208 * IO_* macros (e.g. IO_STATE, IO_MASK, IO_EXTRACT) and _assume_ they are
209 * the same for all channels (which of course they are).
210 *
211 * We will also use the bits defined for serial port 0 when writing commands
212 * to the different ports, as these bits too are the same for all ports.
213 */
214
215
216/* Mask for the irqs possibly enabled in R_IRQ_MASK1_RD etc. */
217static const unsigned long e100_ser_int_mask = 0
218#ifdef CONFIG_ETRAX_SERIAL_PORT0
219| IO_MASK(R_IRQ_MASK1_RD, ser0_data) | IO_MASK(R_IRQ_MASK1_RD, ser0_ready)
220#endif
221#ifdef CONFIG_ETRAX_SERIAL_PORT1
222| IO_MASK(R_IRQ_MASK1_RD, ser1_data) | IO_MASK(R_IRQ_MASK1_RD, ser1_ready)
223#endif
224#ifdef CONFIG_ETRAX_SERIAL_PORT2
225| IO_MASK(R_IRQ_MASK1_RD, ser2_data) | IO_MASK(R_IRQ_MASK1_RD, ser2_ready)
226#endif
227#ifdef CONFIG_ETRAX_SERIAL_PORT3
228| IO_MASK(R_IRQ_MASK1_RD, ser3_data) | IO_MASK(R_IRQ_MASK1_RD, ser3_ready)
229#endif
230;
231unsigned long r_alt_ser_baudrate_shadow = 0;
232
233/* this is the data for the four serial ports in the etrax100 */
234/* DMA2(ser2), DMA4(ser3), DMA6(ser0) or DMA8(ser1) */
235/* R_DMA_CHx_CLR_INTR, R_DMA_CHx_FIRST, R_DMA_CHx_CMD */
236
237static struct e100_serial rs_table[] = {
238 { .baud = DEF_BAUD,
239 .ioport = (unsigned char *)R_SERIAL0_CTRL,
240 .irq = 1U << 12, /* uses DMA 6 and 7 */
241 .oclrintradr = R_DMA_CH6_CLR_INTR,
242 .ofirstadr = R_DMA_CH6_FIRST,
243 .ocmdadr = R_DMA_CH6_CMD,
244 .ostatusadr = R_DMA_CH6_STATUS,
245 .iclrintradr = R_DMA_CH7_CLR_INTR,
246 .ifirstadr = R_DMA_CH7_FIRST,
247 .icmdadr = R_DMA_CH7_CMD,
248 .idescradr = R_DMA_CH7_DESCR,
249 .flags = STD_FLAGS,
250 .rx_ctrl = DEF_RX,
251 .tx_ctrl = DEF_TX,
252 .iseteop = 2,
253 .dma_owner = dma_ser0,
254 .io_if = if_serial_0,
255#ifdef CONFIG_ETRAX_SERIAL_PORT0
256 .enabled = 1,
257#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
258 .dma_out_enabled = 1,
259 .dma_out_nbr = SER0_TX_DMA_NBR,
260 .dma_out_irq_nbr = SER0_DMA_TX_IRQ_NBR,
261 .dma_out_irq_flags = IRQF_DISABLED,
262 .dma_out_irq_description = "serial 0 dma tr",
263#else
264 .dma_out_enabled = 0,
265 .dma_out_nbr = UINT_MAX,
266 .dma_out_irq_nbr = 0,
267 .dma_out_irq_flags = 0,
268 .dma_out_irq_description = NULL,
269#endif
270#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
271 .dma_in_enabled = 1,
272 .dma_in_nbr = SER0_RX_DMA_NBR,
273 .dma_in_irq_nbr = SER0_DMA_RX_IRQ_NBR,
274 .dma_in_irq_flags = IRQF_DISABLED,
275 .dma_in_irq_description = "serial 0 dma rec",
276#else
277 .dma_in_enabled = 0,
278 .dma_in_nbr = UINT_MAX,
279 .dma_in_irq_nbr = 0,
280 .dma_in_irq_flags = 0,
281 .dma_in_irq_description = NULL,
282#endif
283#else
284 .enabled = 0,
285 .io_if_description = NULL,
286 .dma_out_enabled = 0,
287 .dma_in_enabled = 0
288#endif
289
290}, /* ttyS0 */
291#ifndef CONFIG_SVINTO_SIM
292 { .baud = DEF_BAUD,
293 .ioport = (unsigned char *)R_SERIAL1_CTRL,
294 .irq = 1U << 16, /* uses DMA 8 and 9 */
295 .oclrintradr = R_DMA_CH8_CLR_INTR,
296 .ofirstadr = R_DMA_CH8_FIRST,
297 .ocmdadr = R_DMA_CH8_CMD,
298 .ostatusadr = R_DMA_CH8_STATUS,
299 .iclrintradr = R_DMA_CH9_CLR_INTR,
300 .ifirstadr = R_DMA_CH9_FIRST,
301 .icmdadr = R_DMA_CH9_CMD,
302 .idescradr = R_DMA_CH9_DESCR,
303 .flags = STD_FLAGS,
304 .rx_ctrl = DEF_RX,
305 .tx_ctrl = DEF_TX,
306 .iseteop = 3,
307 .dma_owner = dma_ser1,
308 .io_if = if_serial_1,
309#ifdef CONFIG_ETRAX_SERIAL_PORT1
310 .enabled = 1,
311 .io_if_description = "ser1",
312#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA8_OUT
313 .dma_out_enabled = 1,
314 .dma_out_nbr = SER1_TX_DMA_NBR,
315 .dma_out_irq_nbr = SER1_DMA_TX_IRQ_NBR,
316 .dma_out_irq_flags = IRQF_DISABLED,
317 .dma_out_irq_description = "serial 1 dma tr",
318#else
319 .dma_out_enabled = 0,
320 .dma_out_nbr = UINT_MAX,
321 .dma_out_irq_nbr = 0,
322 .dma_out_irq_flags = 0,
323 .dma_out_irq_description = NULL,
324#endif
325#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA9_IN
326 .dma_in_enabled = 1,
327 .dma_in_nbr = SER1_RX_DMA_NBR,
328 .dma_in_irq_nbr = SER1_DMA_RX_IRQ_NBR,
329 .dma_in_irq_flags = IRQF_DISABLED,
330 .dma_in_irq_description = "serial 1 dma rec",
331#else
332 .dma_in_enabled = 0,
333 .dma_in_enabled = 0,
334 .dma_in_nbr = UINT_MAX,
335 .dma_in_irq_nbr = 0,
336 .dma_in_irq_flags = 0,
337 .dma_in_irq_description = NULL,
338#endif
339#else
340 .enabled = 0,
341 .io_if_description = NULL,
342 .dma_in_irq_nbr = 0,
343 .dma_out_enabled = 0,
344 .dma_in_enabled = 0
345#endif
346}, /* ttyS1 */
347
348 { .baud = DEF_BAUD,
349 .ioport = (unsigned char *)R_SERIAL2_CTRL,
350 .irq = 1U << 4, /* uses DMA 2 and 3 */
351 .oclrintradr = R_DMA_CH2_CLR_INTR,
352 .ofirstadr = R_DMA_CH2_FIRST,
353 .ocmdadr = R_DMA_CH2_CMD,
354 .ostatusadr = R_DMA_CH2_STATUS,
355 .iclrintradr = R_DMA_CH3_CLR_INTR,
356 .ifirstadr = R_DMA_CH3_FIRST,
357 .icmdadr = R_DMA_CH3_CMD,
358 .idescradr = R_DMA_CH3_DESCR,
359 .flags = STD_FLAGS,
360 .rx_ctrl = DEF_RX,
361 .tx_ctrl = DEF_TX,
362 .iseteop = 0,
363 .dma_owner = dma_ser2,
364 .io_if = if_serial_2,
365#ifdef CONFIG_ETRAX_SERIAL_PORT2
366 .enabled = 1,
367 .io_if_description = "ser2",
368#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
369 .dma_out_enabled = 1,
370 .dma_out_nbr = SER2_TX_DMA_NBR,
371 .dma_out_irq_nbr = SER2_DMA_TX_IRQ_NBR,
372 .dma_out_irq_flags = IRQF_DISABLED,
373 .dma_out_irq_description = "serial 2 dma tr",
374#else
375 .dma_out_enabled = 0,
376 .dma_out_nbr = UINT_MAX,
377 .dma_out_irq_nbr = 0,
378 .dma_out_irq_flags = 0,
379 .dma_out_irq_description = NULL,
380#endif
381#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
382 .dma_in_enabled = 1,
383 .dma_in_nbr = SER2_RX_DMA_NBR,
384 .dma_in_irq_nbr = SER2_DMA_RX_IRQ_NBR,
385 .dma_in_irq_flags = IRQF_DISABLED,
386 .dma_in_irq_description = "serial 2 dma rec",
387#else
388 .dma_in_enabled = 0,
389 .dma_in_nbr = UINT_MAX,
390 .dma_in_irq_nbr = 0,
391 .dma_in_irq_flags = 0,
392 .dma_in_irq_description = NULL,
393#endif
394#else
395 .enabled = 0,
396 .io_if_description = NULL,
397 .dma_out_enabled = 0,
398 .dma_in_enabled = 0
399#endif
400 }, /* ttyS2 */
401
402 { .baud = DEF_BAUD,
403 .ioport = (unsigned char *)R_SERIAL3_CTRL,
404 .irq = 1U << 8, /* uses DMA 4 and 5 */
405 .oclrintradr = R_DMA_CH4_CLR_INTR,
406 .ofirstadr = R_DMA_CH4_FIRST,
407 .ocmdadr = R_DMA_CH4_CMD,
408 .ostatusadr = R_DMA_CH4_STATUS,
409 .iclrintradr = R_DMA_CH5_CLR_INTR,
410 .ifirstadr = R_DMA_CH5_FIRST,
411 .icmdadr = R_DMA_CH5_CMD,
412 .idescradr = R_DMA_CH5_DESCR,
413 .flags = STD_FLAGS,
414 .rx_ctrl = DEF_RX,
415 .tx_ctrl = DEF_TX,
416 .iseteop = 1,
417 .dma_owner = dma_ser3,
418 .io_if = if_serial_3,
419#ifdef CONFIG_ETRAX_SERIAL_PORT3
420 .enabled = 1,
421 .io_if_description = "ser3",
422#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA4_OUT
423 .dma_out_enabled = 1,
424 .dma_out_nbr = SER3_TX_DMA_NBR,
425 .dma_out_irq_nbr = SER3_DMA_TX_IRQ_NBR,
426 .dma_out_irq_flags = IRQF_DISABLED,
427 .dma_out_irq_description = "serial 3 dma tr",
428#else
429 .dma_out_enabled = 0,
430 .dma_out_nbr = UINT_MAX,
431 .dma_out_irq_nbr = 0,
432 .dma_out_irq_flags = 0,
433 .dma_out_irq_description = NULL,
434#endif
435#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA5_IN
436 .dma_in_enabled = 1,
437 .dma_in_nbr = SER3_RX_DMA_NBR,
438 .dma_in_irq_nbr = SER3_DMA_RX_IRQ_NBR,
439 .dma_in_irq_flags = IRQF_DISABLED,
440 .dma_in_irq_description = "serial 3 dma rec",
441#else
442 .dma_in_enabled = 0,
443 .dma_in_nbr = UINT_MAX,
444 .dma_in_irq_nbr = 0,
445 .dma_in_irq_flags = 0,
446 .dma_in_irq_description = NULL
447#endif
448#else
449 .enabled = 0,
450 .io_if_description = NULL,
451 .dma_out_enabled = 0,
452 .dma_in_enabled = 0
453#endif
454 } /* ttyS3 */
455#endif
456};
457
458
459#define NR_PORTS (sizeof(rs_table)/sizeof(struct e100_serial))
460
461#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
462static struct fast_timer fast_timers[NR_PORTS];
463#endif
464
465#ifdef CONFIG_ETRAX_SERIAL_PROC_ENTRY
466#define PROCSTAT(x) x
467struct ser_statistics_type {
468 int overrun_cnt;
469 int early_errors_cnt;
470 int ser_ints_ok_cnt;
471 int errors_cnt;
472 unsigned long int processing_flip;
473 unsigned long processing_flip_still_room;
474 unsigned long int timeout_flush_cnt;
475 int rx_dma_ints;
476 int tx_dma_ints;
477 int rx_tot;
478 int tx_tot;
479};
480
481static struct ser_statistics_type ser_stat[NR_PORTS];
482
483#else
484
485#define PROCSTAT(x)
486
487#endif /* CONFIG_ETRAX_SERIAL_PROC_ENTRY */
488
489/* RS-485 */
490#if defined(CONFIG_ETRAX_RS485)
491#ifdef CONFIG_ETRAX_FAST_TIMER
492static struct fast_timer fast_timers_rs485[NR_PORTS];
493#endif
494#if defined(CONFIG_ETRAX_RS485_ON_PA)
495static int rs485_pa_bit = CONFIG_ETRAX_RS485_ON_PA_BIT;
496#endif
497#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
498static int rs485_port_g_bit = CONFIG_ETRAX_RS485_ON_PORT_G_BIT;
499#endif
500#endif
501
502/* Info and macros needed for each ports extra control/status signals. */
503#define E100_STRUCT_PORT(line, pinname) \
504 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
505 (R_PORT_PA_DATA): ( \
506 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
507 (R_PORT_PB_DATA):&dummy_ser[line]))
508
509#define E100_STRUCT_SHADOW(line, pinname) \
510 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
511 (&port_pa_data_shadow): ( \
512 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
513 (&port_pb_data_shadow):&dummy_ser[line]))
514#define E100_STRUCT_MASK(line, pinname) \
515 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
516 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT): ( \
517 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
518 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT):DUMMY_##pinname##_MASK))
519
520#define DUMMY_DTR_MASK 1
521#define DUMMY_RI_MASK 2
522#define DUMMY_DSR_MASK 4
523#define DUMMY_CD_MASK 8
524static unsigned char dummy_ser[NR_PORTS] = {0xFF, 0xFF, 0xFF,0xFF};
525
526/* If not all status pins are used or disabled, use mixed mode */
527#ifdef CONFIG_ETRAX_SERIAL_PORT0
528
529#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)
530
531#if SER0_PA_BITSUM != -4
532# if CONFIG_ETRAX_SER0_DTR_ON_PA_BIT == -1
533# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
534# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
535# endif
536# endif
537# if CONFIG_ETRAX_SER0_RI_ON_PA_BIT == -1
538# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
539# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
540# endif
541# endif
542# if CONFIG_ETRAX_SER0_DSR_ON_PA_BIT == -1
543# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
544# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
545# endif
546# endif
547# if CONFIG_ETRAX_SER0_CD_ON_PA_BIT == -1
548# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
549# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
550# endif
551# endif
552#endif
553
554#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)
555
556#if SER0_PB_BITSUM != -4
557# if CONFIG_ETRAX_SER0_DTR_ON_PB_BIT == -1
558# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
559# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
560# endif
561# endif
562# if CONFIG_ETRAX_SER0_RI_ON_PB_BIT == -1
563# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
564# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
565# endif
566# endif
567# if CONFIG_ETRAX_SER0_DSR_ON_PB_BIT == -1
568# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
569# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
570# endif
571# endif
572# if CONFIG_ETRAX_SER0_CD_ON_PB_BIT == -1
573# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
574# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
575# endif
576# endif
577#endif
578
579#endif /* PORT0 */
580
581
582#ifdef CONFIG_ETRAX_SERIAL_PORT1
583
584#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)
585
586#if SER1_PA_BITSUM != -4
587# if CONFIG_ETRAX_SER1_DTR_ON_PA_BIT == -1
588# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
589# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
590# endif
591# endif
592# if CONFIG_ETRAX_SER1_RI_ON_PA_BIT == -1
593# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
594# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
595# endif
596# endif
597# if CONFIG_ETRAX_SER1_DSR_ON_PA_BIT == -1
598# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
599# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
600# endif
601# endif
602# if CONFIG_ETRAX_SER1_CD_ON_PA_BIT == -1
603# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
604# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
605# endif
606# endif
607#endif
608
609#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)
610
611#if SER1_PB_BITSUM != -4
612# if CONFIG_ETRAX_SER1_DTR_ON_PB_BIT == -1
613# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
614# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
615# endif
616# endif
617# if CONFIG_ETRAX_SER1_RI_ON_PB_BIT == -1
618# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
619# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
620# endif
621# endif
622# if CONFIG_ETRAX_SER1_DSR_ON_PB_BIT == -1
623# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
624# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
625# endif
626# endif
627# if CONFIG_ETRAX_SER1_CD_ON_PB_BIT == -1
628# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
629# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
630# endif
631# endif
632#endif
633
634#endif /* PORT1 */
635
636#ifdef CONFIG_ETRAX_SERIAL_PORT2
637
638#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)
639
640#if SER2_PA_BITSUM != -4
641# if CONFIG_ETRAX_SER2_DTR_ON_PA_BIT == -1
642# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
643# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
644# endif
645# endif
646# if CONFIG_ETRAX_SER2_RI_ON_PA_BIT == -1
647# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
648# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
649# endif
650# endif
651# if CONFIG_ETRAX_SER2_DSR_ON_PA_BIT == -1
652# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
653# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
654# endif
655# endif
656# if CONFIG_ETRAX_SER2_CD_ON_PA_BIT == -1
657# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
658# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
659# endif
660# endif
661#endif
662
663#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)
664
665#if SER2_PB_BITSUM != -4
666# if CONFIG_ETRAX_SER2_DTR_ON_PB_BIT == -1
667# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
668# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
669# endif
670# endif
671# if CONFIG_ETRAX_SER2_RI_ON_PB_BIT == -1
672# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
673# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
674# endif
675# endif
676# if CONFIG_ETRAX_SER2_DSR_ON_PB_BIT == -1
677# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
678# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
679# endif
680# endif
681# if CONFIG_ETRAX_SER2_CD_ON_PB_BIT == -1
682# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
683# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
684# endif
685# endif
686#endif
687
688#endif /* PORT2 */
689
690#ifdef CONFIG_ETRAX_SERIAL_PORT3
691
692#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)
693
694#if SER3_PA_BITSUM != -4
695# if CONFIG_ETRAX_SER3_DTR_ON_PA_BIT == -1
696# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
697# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
698# endif
699# endif
700# if CONFIG_ETRAX_SER3_RI_ON_PA_BIT == -1
701# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
702# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
703# endif
704# endif
705# if CONFIG_ETRAX_SER3_DSR_ON_PA_BIT == -1
706# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
707# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
708# endif
709# endif
710# if CONFIG_ETRAX_SER3_CD_ON_PA_BIT == -1
711# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
712# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
713# endif
714# endif
715#endif
716
717#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)
718
719#if SER3_PB_BITSUM != -4
720# if CONFIG_ETRAX_SER3_DTR_ON_PB_BIT == -1
721# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
722# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
723# endif
724# endif
725# if CONFIG_ETRAX_SER3_RI_ON_PB_BIT == -1
726# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
727# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
728# endif
729# endif
730# if CONFIG_ETRAX_SER3_DSR_ON_PB_BIT == -1
731# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
732# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
733# endif
734# endif
735# if CONFIG_ETRAX_SER3_CD_ON_PB_BIT == -1
736# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
737# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
738# endif
739# endif
740#endif
741
742#endif /* PORT3 */
743
744
745#if defined(CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED) || \
746 defined(CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED) || \
747 defined(CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED) || \
748 defined(CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED)
749#define CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
750#endif
751
752#ifdef CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
753/* The pins can be mixed on PA and PB */
754#define CONTROL_PINS_PORT_NOT_USED(line) \
755 &dummy_ser[line], &dummy_ser[line], \
756 &dummy_ser[line], &dummy_ser[line], \
757 &dummy_ser[line], &dummy_ser[line], \
758 &dummy_ser[line], &dummy_ser[line], \
759 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
760
761
762struct control_pins
763{
764 volatile unsigned char *dtr_port;
765 unsigned char *dtr_shadow;
766 volatile unsigned char *ri_port;
767 unsigned char *ri_shadow;
768 volatile unsigned char *dsr_port;
769 unsigned char *dsr_shadow;
770 volatile unsigned char *cd_port;
771 unsigned char *cd_shadow;
772
773 unsigned char dtr_mask;
774 unsigned char ri_mask;
775 unsigned char dsr_mask;
776 unsigned char cd_mask;
777};
778
779static const struct control_pins e100_modem_pins[NR_PORTS] =
780{
781 /* Ser 0 */
782 {
783#ifdef CONFIG_ETRAX_SERIAL_PORT0
784 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
785 E100_STRUCT_PORT(0,RI), E100_STRUCT_SHADOW(0,RI),
786 E100_STRUCT_PORT(0,DSR), E100_STRUCT_SHADOW(0,DSR),
787 E100_STRUCT_PORT(0,CD), E100_STRUCT_SHADOW(0,CD),
788 E100_STRUCT_MASK(0,DTR),
789 E100_STRUCT_MASK(0,RI),
790 E100_STRUCT_MASK(0,DSR),
791 E100_STRUCT_MASK(0,CD)
792#else
793 CONTROL_PINS_PORT_NOT_USED(0)
794#endif
795 },
796
797 /* Ser 1 */
798 {
799#ifdef CONFIG_ETRAX_SERIAL_PORT1
800 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
801 E100_STRUCT_PORT(1,RI), E100_STRUCT_SHADOW(1,RI),
802 E100_STRUCT_PORT(1,DSR), E100_STRUCT_SHADOW(1,DSR),
803 E100_STRUCT_PORT(1,CD), E100_STRUCT_SHADOW(1,CD),
804 E100_STRUCT_MASK(1,DTR),
805 E100_STRUCT_MASK(1,RI),
806 E100_STRUCT_MASK(1,DSR),
807 E100_STRUCT_MASK(1,CD)
808#else
809 CONTROL_PINS_PORT_NOT_USED(1)
810#endif
811 },
812
813 /* Ser 2 */
814 {
815#ifdef CONFIG_ETRAX_SERIAL_PORT2
816 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
817 E100_STRUCT_PORT(2,RI), E100_STRUCT_SHADOW(2,RI),
818 E100_STRUCT_PORT(2,DSR), E100_STRUCT_SHADOW(2,DSR),
819 E100_STRUCT_PORT(2,CD), E100_STRUCT_SHADOW(2,CD),
820 E100_STRUCT_MASK(2,DTR),
821 E100_STRUCT_MASK(2,RI),
822 E100_STRUCT_MASK(2,DSR),
823 E100_STRUCT_MASK(2,CD)
824#else
825 CONTROL_PINS_PORT_NOT_USED(2)
826#endif
827 },
828
829 /* Ser 3 */
830 {
831#ifdef CONFIG_ETRAX_SERIAL_PORT3
832 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
833 E100_STRUCT_PORT(3,RI), E100_STRUCT_SHADOW(3,RI),
834 E100_STRUCT_PORT(3,DSR), E100_STRUCT_SHADOW(3,DSR),
835 E100_STRUCT_PORT(3,CD), E100_STRUCT_SHADOW(3,CD),
836 E100_STRUCT_MASK(3,DTR),
837 E100_STRUCT_MASK(3,RI),
838 E100_STRUCT_MASK(3,DSR),
839 E100_STRUCT_MASK(3,CD)
840#else
841 CONTROL_PINS_PORT_NOT_USED(3)
842#endif
843 }
844};
845#else /* CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
846
847/* All pins are on either PA or PB for each serial port */
848#define CONTROL_PINS_PORT_NOT_USED(line) \
849 &dummy_ser[line], &dummy_ser[line], \
850 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
851
852
853struct control_pins
854{
855 volatile unsigned char *port;
856 unsigned char *shadow;
857
858 unsigned char dtr_mask;
859 unsigned char ri_mask;
860 unsigned char dsr_mask;
861 unsigned char cd_mask;
862};
863
864#define dtr_port port
865#define dtr_shadow shadow
866#define ri_port port
867#define ri_shadow shadow
868#define dsr_port port
869#define dsr_shadow shadow
870#define cd_port port
871#define cd_shadow shadow
872
873static const struct control_pins e100_modem_pins[NR_PORTS] =
874{
875 /* Ser 0 */
876 {
877#ifdef CONFIG_ETRAX_SERIAL_PORT0
878 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
879 E100_STRUCT_MASK(0,DTR),
880 E100_STRUCT_MASK(0,RI),
881 E100_STRUCT_MASK(0,DSR),
882 E100_STRUCT_MASK(0,CD)
883#else
884 CONTROL_PINS_PORT_NOT_USED(0)
885#endif
886 },
887
888 /* Ser 1 */
889 {
890#ifdef CONFIG_ETRAX_SERIAL_PORT1
891 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
892 E100_STRUCT_MASK(1,DTR),
893 E100_STRUCT_MASK(1,RI),
894 E100_STRUCT_MASK(1,DSR),
895 E100_STRUCT_MASK(1,CD)
896#else
897 CONTROL_PINS_PORT_NOT_USED(1)
898#endif
899 },
900
901 /* Ser 2 */
902 {
903#ifdef CONFIG_ETRAX_SERIAL_PORT2
904 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
905 E100_STRUCT_MASK(2,DTR),
906 E100_STRUCT_MASK(2,RI),
907 E100_STRUCT_MASK(2,DSR),
908 E100_STRUCT_MASK(2,CD)
909#else
910 CONTROL_PINS_PORT_NOT_USED(2)
911#endif
912 },
913
914 /* Ser 3 */
915 {
916#ifdef CONFIG_ETRAX_SERIAL_PORT3
917 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
918 E100_STRUCT_MASK(3,DTR),
919 E100_STRUCT_MASK(3,RI),
920 E100_STRUCT_MASK(3,DSR),
921 E100_STRUCT_MASK(3,CD)
922#else
923 CONTROL_PINS_PORT_NOT_USED(3)
924#endif
925 }
926};
927#endif /* !CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
928
929#define E100_RTS_MASK 0x20
930#define E100_CTS_MASK 0x40
931
932/* All serial port signals are active low:
933 * active = 0 -> 3.3V to RS-232 driver -> -12V on RS-232 level
934 * inactive = 1 -> 0V to RS-232 driver -> +12V on RS-232 level
935 *
936 * These macros returns the pin value: 0=0V, >=1 = 3.3V on ETRAX chip
937 */
938
939/* Output */
940#define E100_RTS_GET(info) ((info)->rx_ctrl & E100_RTS_MASK)
941/* Input */
942#define E100_CTS_GET(info) ((info)->ioport[REG_STATUS] & E100_CTS_MASK)
943
944/* These are typically PA or PB and 0 means 0V, 1 means 3.3V */
945/* Is an output */
946#define E100_DTR_GET(info) ((*e100_modem_pins[(info)->line].dtr_shadow) & e100_modem_pins[(info)->line].dtr_mask)
947
948/* Normally inputs */
949#define E100_RI_GET(info) ((*e100_modem_pins[(info)->line].ri_port) & e100_modem_pins[(info)->line].ri_mask)
950#define E100_CD_GET(info) ((*e100_modem_pins[(info)->line].cd_port) & e100_modem_pins[(info)->line].cd_mask)
951
952/* Input */
953#define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask)
954
955
956/*
957 * tmp_buf is used as a temporary buffer by serial_write. We need to
958 * lock it in case the memcpy_fromfs blocks while swapping in a page,
959 * and some other program tries to do a serial write at the same time.
960 * Since the lock will only come under contention when the system is
961 * swapping and available memory is low, it makes sense to share one
962 * buffer across all the serial ports, since it significantly saves
963 * memory if large numbers of serial ports are open.
964 */
965static unsigned char *tmp_buf;
966static DEFINE_MUTEX(tmp_buf_mutex);
967
968/* Calculate the chartime depending on baudrate, numbor of bits etc. */
969static void update_char_time(struct e100_serial * info)
970{
971 tcflag_t cflags = info->port.tty->termios->c_cflag;
972 int bits;
973
974 /* calc. number of bits / data byte */
975 /* databits + startbit and 1 stopbit */
976 if ((cflags & CSIZE) == CS7)
977 bits = 9;
978 else
979 bits = 10;
980
981 if (cflags & CSTOPB) /* 2 stopbits ? */
982 bits++;
983
984 if (cflags & PARENB) /* parity bit ? */
985 bits++;
986
987 /* calc timeout */
988 info->char_time_usec = ((bits * 1000000) / info->baud) + 1;
989 info->flush_time_usec = 4*info->char_time_usec;
990 if (info->flush_time_usec < MIN_FLUSH_TIME_USEC)
991 info->flush_time_usec = MIN_FLUSH_TIME_USEC;
992
993}
994
995/*
996 * This function maps from the Bxxxx defines in asm/termbits.h into real
997 * baud rates.
998 */
999
1000static int
1001cflag_to_baud(unsigned int cflag)
1002{
1003 static int baud_table[] = {
1004 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
1005 4800, 9600, 19200, 38400 };
1006
1007 static int ext_baud_table[] = {
1008 0, 57600, 115200, 230400, 460800, 921600, 1843200, 6250000,
1009 0, 0, 0, 0, 0, 0, 0, 0 };
1010
1011 if (cflag & CBAUDEX)
1012 return ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1013 else
1014 return baud_table[cflag & CBAUD];
1015}
1016
1017/* and this maps to an etrax100 hardware baud constant */
1018
1019static unsigned char
1020cflag_to_etrax_baud(unsigned int cflag)
1021{
1022 char retval;
1023
1024 static char baud_table[] = {
1025 -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, 4, 5, 6, 7 };
1026
1027 static char ext_baud_table[] = {
1028 -1, 8, 9, 10, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 };
1029
1030 if (cflag & CBAUDEX)
1031 retval = ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1032 else
1033 retval = baud_table[cflag & CBAUD];
1034
1035 if (retval < 0) {
1036 printk(KERN_WARNING "serdriver tried setting invalid baud rate, flags %x.\n", cflag);
1037 retval = 5; /* choose default 9600 instead */
1038 }
1039
1040 return retval | (retval << 4); /* choose same for both TX and RX */
1041}
1042
1043
1044/* Various static support functions */
1045
1046/* Functions to set or clear DTR/RTS on the requested line */
1047/* It is complicated by the fact that RTS is a serial port register, while
1048 * DTR might not be implemented in the HW at all, and if it is, it can be on
1049 * any general port.
1050 */
1051
1052
1053static inline void
1054e100_dtr(struct e100_serial *info, int set)
1055{
1056#ifndef CONFIG_SVINTO_SIM
1057 unsigned char mask = e100_modem_pins[info->line].dtr_mask;
1058
1059#ifdef SERIAL_DEBUG_IO
1060 printk("ser%i dtr %i mask: 0x%02X\n", info->line, set, mask);
1061 printk("ser%i shadow before 0x%02X get: %i\n",
1062 info->line, *e100_modem_pins[info->line].dtr_shadow,
1063 E100_DTR_GET(info));
1064#endif
1065 /* DTR is active low */
1066 {
1067 unsigned long flags;
1068
1069 local_irq_save(flags);
1070 *e100_modem_pins[info->line].dtr_shadow &= ~mask;
1071 *e100_modem_pins[info->line].dtr_shadow |= (set ? 0 : mask);
1072 *e100_modem_pins[info->line].dtr_port = *e100_modem_pins[info->line].dtr_shadow;
1073 local_irq_restore(flags);
1074 }
1075
1076#ifdef SERIAL_DEBUG_IO
1077 printk("ser%i shadow after 0x%02X get: %i\n",
1078 info->line, *e100_modem_pins[info->line].dtr_shadow,
1079 E100_DTR_GET(info));
1080#endif
1081#endif
1082}
1083
1084/* set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
1085 * 0=0V , 1=3.3V
1086 */
1087static inline void
1088e100_rts(struct e100_serial *info, int set)
1089{
1090#ifndef CONFIG_SVINTO_SIM
1091 unsigned long flags;
1092 local_irq_save(flags);
1093 info->rx_ctrl &= ~E100_RTS_MASK;
1094 info->rx_ctrl |= (set ? 0 : E100_RTS_MASK); /* RTS is active low */
1095 info->ioport[REG_REC_CTRL] = info->rx_ctrl;
1096 local_irq_restore(flags);
1097#ifdef SERIAL_DEBUG_IO
1098 printk("ser%i rts %i\n", info->line, set);
1099#endif
1100#endif
1101}
1102
1103
1104/* If this behaves as a modem, RI and CD is an output */
1105static inline void
1106e100_ri_out(struct e100_serial *info, int set)
1107{
1108#ifndef CONFIG_SVINTO_SIM
1109 /* RI is active low */
1110 {
1111 unsigned char mask = e100_modem_pins[info->line].ri_mask;
1112 unsigned long flags;
1113
1114 local_irq_save(flags);
1115 *e100_modem_pins[info->line].ri_shadow &= ~mask;
1116 *e100_modem_pins[info->line].ri_shadow |= (set ? 0 : mask);
1117 *e100_modem_pins[info->line].ri_port = *e100_modem_pins[info->line].ri_shadow;
1118 local_irq_restore(flags);
1119 }
1120#endif
1121}
1122static inline void
1123e100_cd_out(struct e100_serial *info, int set)
1124{
1125#ifndef CONFIG_SVINTO_SIM
1126 /* CD is active low */
1127 {
1128 unsigned char mask = e100_modem_pins[info->line].cd_mask;
1129 unsigned long flags;
1130
1131 local_irq_save(flags);
1132 *e100_modem_pins[info->line].cd_shadow &= ~mask;
1133 *e100_modem_pins[info->line].cd_shadow |= (set ? 0 : mask);
1134 *e100_modem_pins[info->line].cd_port = *e100_modem_pins[info->line].cd_shadow;
1135 local_irq_restore(flags);
1136 }
1137#endif
1138}
1139
1140static inline void
1141e100_disable_rx(struct e100_serial *info)
1142{
1143#ifndef CONFIG_SVINTO_SIM
1144 /* disable the receiver */
1145 info->ioport[REG_REC_CTRL] =
1146 (info->rx_ctrl &= ~IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1147#endif
1148}
1149
1150static inline void
1151e100_enable_rx(struct e100_serial *info)
1152{
1153#ifndef CONFIG_SVINTO_SIM
1154 /* enable the receiver */
1155 info->ioport[REG_REC_CTRL] =
1156 (info->rx_ctrl |= IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1157#endif
1158}
1159
1160/* the rx DMA uses both the dma_descr and the dma_eop interrupts */
1161
1162static inline void
1163e100_disable_rxdma_irq(struct e100_serial *info)
1164{
1165#ifdef SERIAL_DEBUG_INTR
1166 printk("rxdma_irq(%d): 0\n",info->line);
1167#endif
1168 DINTR1(DEBUG_LOG(info->line,"IRQ disable_rxdma_irq %i\n", info->line));
1169 *R_IRQ_MASK2_CLR = (info->irq << 2) | (info->irq << 3);
1170}
1171
1172static inline void
1173e100_enable_rxdma_irq(struct e100_serial *info)
1174{
1175#ifdef SERIAL_DEBUG_INTR
1176 printk("rxdma_irq(%d): 1\n",info->line);
1177#endif
1178 DINTR1(DEBUG_LOG(info->line,"IRQ enable_rxdma_irq %i\n", info->line));
1179 *R_IRQ_MASK2_SET = (info->irq << 2) | (info->irq << 3);
1180}
1181
1182/* the tx DMA uses only dma_descr interrupt */
1183
1184static void e100_disable_txdma_irq(struct e100_serial *info)
1185{
1186#ifdef SERIAL_DEBUG_INTR
1187 printk("txdma_irq(%d): 0\n",info->line);
1188#endif
1189 DINTR1(DEBUG_LOG(info->line,"IRQ disable_txdma_irq %i\n", info->line));
1190 *R_IRQ_MASK2_CLR = info->irq;
1191}
1192
1193static void e100_enable_txdma_irq(struct e100_serial *info)
1194{
1195#ifdef SERIAL_DEBUG_INTR
1196 printk("txdma_irq(%d): 1\n",info->line);
1197#endif
1198 DINTR1(DEBUG_LOG(info->line,"IRQ enable_txdma_irq %i\n", info->line));
1199 *R_IRQ_MASK2_SET = info->irq;
1200}
1201
1202static void e100_disable_txdma_channel(struct e100_serial *info)
1203{
1204 unsigned long flags;
1205
1206 /* Disable output DMA channel for the serial port in question
1207 * ( set to something other than serialX)
1208 */
1209 local_irq_save(flags);
1210 DFLOW(DEBUG_LOG(info->line, "disable_txdma_channel %i\n", info->line));
1211 if (info->line == 0) {
1212 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma6)) ==
1213 IO_STATE(R_GEN_CONFIG, dma6, serial0)) {
1214 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1215 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
1216 }
1217 } else if (info->line == 1) {
1218 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma8)) ==
1219 IO_STATE(R_GEN_CONFIG, dma8, serial1)) {
1220 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1221 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
1222 }
1223 } else if (info->line == 2) {
1224 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma2)) ==
1225 IO_STATE(R_GEN_CONFIG, dma2, serial2)) {
1226 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1227 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
1228 }
1229 } else if (info->line == 3) {
1230 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma4)) ==
1231 IO_STATE(R_GEN_CONFIG, dma4, serial3)) {
1232 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1233 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
1234 }
1235 }
1236 *R_GEN_CONFIG = genconfig_shadow;
1237 local_irq_restore(flags);
1238}
1239
1240
1241static void e100_enable_txdma_channel(struct e100_serial *info)
1242{
1243 unsigned long flags;
1244
1245 local_irq_save(flags);
1246 DFLOW(DEBUG_LOG(info->line, "enable_txdma_channel %i\n", info->line));
1247 /* Enable output DMA channel for the serial port in question */
1248 if (info->line == 0) {
1249 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1250 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, serial0);
1251 } else if (info->line == 1) {
1252 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1253 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, serial1);
1254 } else if (info->line == 2) {
1255 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1256 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, serial2);
1257 } else if (info->line == 3) {
1258 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1259 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, serial3);
1260 }
1261 *R_GEN_CONFIG = genconfig_shadow;
1262 local_irq_restore(flags);
1263}
1264
1265static void e100_disable_rxdma_channel(struct e100_serial *info)
1266{
1267 unsigned long flags;
1268
1269 /* Disable input DMA channel for the serial port in question
1270 * ( set to something other than serialX)
1271 */
1272 local_irq_save(flags);
1273 if (info->line == 0) {
1274 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma7)) ==
1275 IO_STATE(R_GEN_CONFIG, dma7, serial0)) {
1276 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1277 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, unused);
1278 }
1279 } else if (info->line == 1) {
1280 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma9)) ==
1281 IO_STATE(R_GEN_CONFIG, dma9, serial1)) {
1282 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1283 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, usb);
1284 }
1285 } else if (info->line == 2) {
1286 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma3)) ==
1287 IO_STATE(R_GEN_CONFIG, dma3, serial2)) {
1288 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1289 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
1290 }
1291 } else if (info->line == 3) {
1292 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma5)) ==
1293 IO_STATE(R_GEN_CONFIG, dma5, serial3)) {
1294 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1295 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
1296 }
1297 }
1298 *R_GEN_CONFIG = genconfig_shadow;
1299 local_irq_restore(flags);
1300}
1301
1302
1303static void e100_enable_rxdma_channel(struct e100_serial *info)
1304{
1305 unsigned long flags;
1306
1307 local_irq_save(flags);
1308 /* Enable input DMA channel for the serial port in question */
1309 if (info->line == 0) {
1310 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1311 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, serial0);
1312 } else if (info->line == 1) {
1313 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1314 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, serial1);
1315 } else if (info->line == 2) {
1316 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1317 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, serial2);
1318 } else if (info->line == 3) {
1319 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1320 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, serial3);
1321 }
1322 *R_GEN_CONFIG = genconfig_shadow;
1323 local_irq_restore(flags);
1324}
1325
1326#ifdef SERIAL_HANDLE_EARLY_ERRORS
1327/* in order to detect and fix errors on the first byte
1328 we have to use the serial interrupts as well. */
1329
1330static inline void
1331e100_disable_serial_data_irq(struct e100_serial *info)
1332{
1333#ifdef SERIAL_DEBUG_INTR
1334 printk("ser_irq(%d): 0\n",info->line);
1335#endif
1336 DINTR1(DEBUG_LOG(info->line,"IRQ disable data_irq %i\n", info->line));
1337 *R_IRQ_MASK1_CLR = (1U << (8+2*info->line));
1338}
1339
1340static inline void
1341e100_enable_serial_data_irq(struct e100_serial *info)
1342{
1343#ifdef SERIAL_DEBUG_INTR
1344 printk("ser_irq(%d): 1\n",info->line);
1345 printk("**** %d = %d\n",
1346 (8+2*info->line),
1347 (1U << (8+2*info->line)));
1348#endif
1349 DINTR1(DEBUG_LOG(info->line,"IRQ enable data_irq %i\n", info->line));
1350 *R_IRQ_MASK1_SET = (1U << (8+2*info->line));
1351}
1352#endif
1353
1354static inline void
1355e100_disable_serial_tx_ready_irq(struct e100_serial *info)
1356{
1357#ifdef SERIAL_DEBUG_INTR
1358 printk("ser_tx_irq(%d): 0\n",info->line);
1359#endif
1360 DINTR1(DEBUG_LOG(info->line,"IRQ disable ready_irq %i\n", info->line));
1361 *R_IRQ_MASK1_CLR = (1U << (8+1+2*info->line));
1362}
1363
1364static inline void
1365e100_enable_serial_tx_ready_irq(struct e100_serial *info)
1366{
1367#ifdef SERIAL_DEBUG_INTR
1368 printk("ser_tx_irq(%d): 1\n",info->line);
1369 printk("**** %d = %d\n",
1370 (8+1+2*info->line),
1371 (1U << (8+1+2*info->line)));
1372#endif
1373 DINTR2(DEBUG_LOG(info->line,"IRQ enable ready_irq %i\n", info->line));
1374 *R_IRQ_MASK1_SET = (1U << (8+1+2*info->line));
1375}
1376
1377static inline void e100_enable_rx_irq(struct e100_serial *info)
1378{
1379 if (info->uses_dma_in)
1380 e100_enable_rxdma_irq(info);
1381 else
1382 e100_enable_serial_data_irq(info);
1383}
1384static inline void e100_disable_rx_irq(struct e100_serial *info)
1385{
1386 if (info->uses_dma_in)
1387 e100_disable_rxdma_irq(info);
1388 else
1389 e100_disable_serial_data_irq(info);
1390}
1391
1392#if defined(CONFIG_ETRAX_RS485)
1393/* Enable RS-485 mode on selected port. This is UGLY. */
1394static int
1395e100_enable_rs485(struct tty_struct *tty, struct serial_rs485 *r)
1396{
1397 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1398
1399#if defined(CONFIG_ETRAX_RS485_ON_PA)
1400 *R_PORT_PA_DATA = port_pa_data_shadow |= (1 << rs485_pa_bit);
1401#endif
1402#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
1403 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1404 rs485_port_g_bit, 1);
1405#endif
1406#if defined(CONFIG_ETRAX_RS485_LTC1387)
1407 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1408 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT, 1);
1409 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1410 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT, 1);
1411#endif
1412
1413 info->rs485 = *r;
1414
1415 /* Maximum delay before RTS equal to 1000 */
1416 if (info->rs485.delay_rts_before_send >= 1000)
1417 info->rs485.delay_rts_before_send = 1000;
1418
1419/* printk("rts: on send = %i, after = %i, enabled = %i",
1420 info->rs485.rts_on_send,
1421 info->rs485.rts_after_sent,
1422 info->rs485.enabled
1423 );
1424*/
1425 return 0;
1426}
1427
1428static int
1429e100_write_rs485(struct tty_struct *tty,
1430 const unsigned char *buf, int count)
1431{
1432 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1433 int old_value = (info->rs485.flags) & SER_RS485_ENABLED;
1434
1435 /* rs485 is always implicitly enabled if we're using the ioctl()
1436 * but it doesn't have to be set in the serial_rs485
1437 * (to be backward compatible with old apps)
1438 * So we store, set and restore it.
1439 */
1440 info->rs485.flags |= SER_RS485_ENABLED;
1441 /* rs_write now deals with RS485 if enabled */
1442 count = rs_write(tty, buf, count);
1443 if (!old_value)
1444 info->rs485.flags &= ~(SER_RS485_ENABLED);
1445 return count;
1446}
1447
1448#ifdef CONFIG_ETRAX_FAST_TIMER
1449/* Timer function to toggle RTS when using FAST_TIMER */
1450static void rs485_toggle_rts_timer_function(unsigned long data)
1451{
1452 struct e100_serial *info = (struct e100_serial *)data;
1453
1454 fast_timers_rs485[info->line].function = NULL;
1455 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
1456#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
1457 e100_enable_rx(info);
1458 e100_enable_rx_irq(info);
1459#endif
1460}
1461#endif
1462#endif /* CONFIG_ETRAX_RS485 */
1463
1464/*
1465 * ------------------------------------------------------------
1466 * rs_stop() and rs_start()
1467 *
1468 * This routines are called before setting or resetting tty->stopped.
1469 * They enable or disable transmitter using the XOFF registers, as necessary.
1470 * ------------------------------------------------------------
1471 */
1472
1473static void
1474rs_stop(struct tty_struct *tty)
1475{
1476 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1477 if (info) {
1478 unsigned long flags;
1479 unsigned long xoff;
1480
1481 local_irq_save(flags);
1482 DFLOW(DEBUG_LOG(info->line, "XOFF rs_stop xmit %i\n",
1483 CIRC_CNT(info->xmit.head,
1484 info->xmit.tail,SERIAL_XMIT_SIZE)));
1485
1486 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char,
1487 STOP_CHAR(info->port.tty));
1488 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, stop);
1489 if (tty->termios->c_iflag & IXON ) {
1490 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1491 }
1492
1493 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
1494 local_irq_restore(flags);
1495 }
1496}
1497
1498static void
1499rs_start(struct tty_struct *tty)
1500{
1501 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1502 if (info) {
1503 unsigned long flags;
1504 unsigned long xoff;
1505
1506 local_irq_save(flags);
1507 DFLOW(DEBUG_LOG(info->line, "XOFF rs_start xmit %i\n",
1508 CIRC_CNT(info->xmit.head,
1509 info->xmit.tail,SERIAL_XMIT_SIZE)));
1510 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(tty));
1511 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
1512 if (tty->termios->c_iflag & IXON ) {
1513 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1514 }
1515
1516 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
1517 if (!info->uses_dma_out &&
1518 info->xmit.head != info->xmit.tail && info->xmit.buf)
1519 e100_enable_serial_tx_ready_irq(info);
1520
1521 local_irq_restore(flags);
1522 }
1523}
1524
1525/*
1526 * ----------------------------------------------------------------------
1527 *
1528 * Here starts the interrupt handling routines. All of the following
1529 * subroutines are declared as inline and are folded into
1530 * rs_interrupt(). They were separated out for readability's sake.
1531 *
1532 * Note: rs_interrupt() is a "fast" interrupt, which means that it
1533 * runs with interrupts turned off. People who may want to modify
1534 * rs_interrupt() should try to keep the interrupt handler as fast as
1535 * possible. After you are done making modifications, it is not a bad
1536 * idea to do:
1537 *
1538 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
1539 *
1540 * and look at the resulting assemble code in serial.s.
1541 *
1542 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
1543 * -----------------------------------------------------------------------
1544 */
1545
1546/*
1547 * This routine is used by the interrupt handler to schedule
1548 * processing in the software interrupt portion of the driver.
1549 */
1550static void rs_sched_event(struct e100_serial *info, int event)
1551{
1552 if (info->event & (1 << event))
1553 return;
1554 info->event |= 1 << event;
1555 schedule_work(&info->work);
1556}
1557
1558/* The output DMA channel is free - use it to send as many chars as possible
1559 * NOTES:
1560 * We don't pay attention to info->x_char, which means if the TTY wants to
1561 * use XON/XOFF it will set info->x_char but we won't send any X char!
1562 *
1563 * To implement this, we'd just start a DMA send of 1 byte pointing at a
1564 * buffer containing the X char, and skip updating xmit. We'd also have to
1565 * check if the last sent char was the X char when we enter this function
1566 * the next time, to avoid updating xmit with the sent X value.
1567 */
1568
1569static void
1570transmit_chars_dma(struct e100_serial *info)
1571{
1572 unsigned int c, sentl;
1573 struct etrax_dma_descr *descr;
1574
1575#ifdef CONFIG_SVINTO_SIM
1576 /* This will output too little if tail is not 0 always since
1577 * we don't reloop to send the other part. Anyway this SHOULD be a
1578 * no-op - transmit_chars_dma would never really be called during sim
1579 * since rs_write does not write into the xmit buffer then.
1580 */
1581 if (info->xmit.tail)
1582 printk("Error in serial.c:transmit_chars-dma(), tail!=0\n");
1583 if (info->xmit.head != info->xmit.tail) {
1584 SIMCOUT(info->xmit.buf + info->xmit.tail,
1585 CIRC_CNT(info->xmit.head,
1586 info->xmit.tail,
1587 SERIAL_XMIT_SIZE));
1588 info->xmit.head = info->xmit.tail; /* move back head */
1589 info->tr_running = 0;
1590 }
1591 return;
1592#endif
1593 /* acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1594 *info->oclrintradr =
1595 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1596 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1597
1598#ifdef SERIAL_DEBUG_INTR
1599 if (info->line == SERIAL_DEBUG_LINE)
1600 printk("tc\n");
1601#endif
1602 if (!info->tr_running) {
1603 /* weirdo... we shouldn't get here! */
1604 printk(KERN_WARNING "Achtung: transmit_chars_dma with !tr_running\n");
1605 return;
1606 }
1607
1608 descr = &info->tr_descr;
1609
1610 /* first get the amount of bytes sent during the last DMA transfer,
1611 and update xmit accordingly */
1612
1613 /* if the stop bit was not set, all data has been sent */
1614 if (!(descr->status & d_stop)) {
1615 sentl = descr->sw_len;
1616 } else
1617 /* otherwise we find the amount of data sent here */
1618 sentl = descr->hw_len;
1619
1620 DFLOW(DEBUG_LOG(info->line, "TX %i done\n", sentl));
1621
1622 /* update stats */
1623 info->icount.tx += sentl;
1624
1625 /* update xmit buffer */
1626 info->xmit.tail = (info->xmit.tail + sentl) & (SERIAL_XMIT_SIZE - 1);
1627
1628 /* if there is only a few chars left in the buf, wake up the blocked
1629 write if any */
1630 if (CIRC_CNT(info->xmit.head,
1631 info->xmit.tail,
1632 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
1633 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
1634
1635 /* find out the largest amount of consecutive bytes we want to send now */
1636
1637 c = CIRC_CNT_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1638
1639 /* Don't send all in one DMA transfer - divide it so we wake up
1640 * application before all is sent
1641 */
1642
1643 if (c >= 4*WAKEUP_CHARS)
1644 c = c/2;
1645
1646 if (c <= 0) {
1647 /* our job here is done, don't schedule any new DMA transfer */
1648 info->tr_running = 0;
1649
1650#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
1651 if (info->rs485.flags & SER_RS485_ENABLED) {
1652 /* Set a short timer to toggle RTS */
1653 start_one_shot_timer(&fast_timers_rs485[info->line],
1654 rs485_toggle_rts_timer_function,
1655 (unsigned long)info,
1656 info->char_time_usec*2,
1657 "RS-485");
1658 }
1659#endif /* RS485 */
1660 return;
1661 }
1662
1663 /* ok we can schedule a dma send of c chars starting at info->xmit.tail */
1664 /* set up the descriptor correctly for output */
1665 DFLOW(DEBUG_LOG(info->line, "TX %i\n", c));
1666 descr->ctrl = d_int | d_eol | d_wait; /* Wait needed for tty_wait_until_sent() */
1667 descr->sw_len = c;
1668 descr->buf = virt_to_phys(info->xmit.buf + info->xmit.tail);
1669 descr->status = 0;
1670
1671 *info->ofirstadr = virt_to_phys(descr); /* write to R_DMAx_FIRST */
1672 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1673
1674 /* DMA is now running (hopefully) */
1675} /* transmit_chars_dma */
1676
1677static void
1678start_transmit(struct e100_serial *info)
1679{
1680#if 0
1681 if (info->line == SERIAL_DEBUG_LINE)
1682 printk("x\n");
1683#endif
1684
1685 info->tr_descr.sw_len = 0;
1686 info->tr_descr.hw_len = 0;
1687 info->tr_descr.status = 0;
1688 info->tr_running = 1;
1689 if (info->uses_dma_out)
1690 transmit_chars_dma(info);
1691 else
1692 e100_enable_serial_tx_ready_irq(info);
1693} /* start_transmit */
1694
1695#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
1696static int serial_fast_timer_started = 0;
1697static int serial_fast_timer_expired = 0;
1698static void flush_timeout_function(unsigned long data);
1699#define START_FLUSH_FAST_TIMER_TIME(info, string, usec) {\
1700 unsigned long timer_flags; \
1701 local_irq_save(timer_flags); \
1702 if (fast_timers[info->line].function == NULL) { \
1703 serial_fast_timer_started++; \
1704 TIMERD(DEBUG_LOG(info->line, "start_timer %i ", info->line)); \
1705 TIMERD(DEBUG_LOG(info->line, "num started: %i\n", serial_fast_timer_started)); \
1706 start_one_shot_timer(&fast_timers[info->line], \
1707 flush_timeout_function, \
1708 (unsigned long)info, \
1709 (usec), \
1710 string); \
1711 } \
1712 else { \
1713 TIMERD(DEBUG_LOG(info->line, "timer %i already running\n", info->line)); \
1714 } \
1715 local_irq_restore(timer_flags); \
1716}
1717#define START_FLUSH_FAST_TIMER(info, string) START_FLUSH_FAST_TIMER_TIME(info, string, info->flush_time_usec)
1718
1719#else
1720#define START_FLUSH_FAST_TIMER_TIME(info, string, usec)
1721#define START_FLUSH_FAST_TIMER(info, string)
1722#endif
1723
1724static struct etrax_recv_buffer *
1725alloc_recv_buffer(unsigned int size)
1726{
1727 struct etrax_recv_buffer *buffer;
1728
1729 if (!(buffer = kmalloc(sizeof *buffer + size, GFP_ATOMIC)))
1730 return NULL;
1731
1732 buffer->next = NULL;
1733 buffer->length = 0;
1734 buffer->error = TTY_NORMAL;
1735
1736 return buffer;
1737}
1738
1739static void
1740append_recv_buffer(struct e100_serial *info, struct etrax_recv_buffer *buffer)
1741{
1742 unsigned long flags;
1743
1744 local_irq_save(flags);
1745
1746 if (!info->first_recv_buffer)
1747 info->first_recv_buffer = buffer;
1748 else
1749 info->last_recv_buffer->next = buffer;
1750
1751 info->last_recv_buffer = buffer;
1752
1753 info->recv_cnt += buffer->length;
1754 if (info->recv_cnt > info->max_recv_cnt)
1755 info->max_recv_cnt = info->recv_cnt;
1756
1757 local_irq_restore(flags);
1758}
1759
1760static int
1761add_char_and_flag(struct e100_serial *info, unsigned char data, unsigned char flag)
1762{
1763 struct etrax_recv_buffer *buffer;
1764 if (info->uses_dma_in) {
1765 if (!(buffer = alloc_recv_buffer(4)))
1766 return 0;
1767
1768 buffer->length = 1;
1769 buffer->error = flag;
1770 buffer->buffer[0] = data;
1771
1772 append_recv_buffer(info, buffer);
1773
1774 info->icount.rx++;
1775 } else {
1776 struct tty_struct *tty = info->port.tty;
1777 tty_insert_flip_char(tty, data, flag);
1778 info->icount.rx++;
1779 }
1780
1781 return 1;
1782}
1783
1784static unsigned int handle_descr_data(struct e100_serial *info,
1785 struct etrax_dma_descr *descr,
1786 unsigned int recvl)
1787{
1788 struct etrax_recv_buffer *buffer = phys_to_virt(descr->buf) - sizeof *buffer;
1789
1790 if (info->recv_cnt + recvl > 65536) {
1791 printk(KERN_CRIT
1792 "%s: Too much pending incoming serial data! Dropping %u bytes.\n", __func__, recvl);
1793 return 0;
1794 }
1795
1796 buffer->length = recvl;
1797
1798 if (info->errorcode == ERRCODE_SET_BREAK)
1799 buffer->error = TTY_BREAK;
1800 info->errorcode = 0;
1801
1802 append_recv_buffer(info, buffer);
1803
1804 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
1805 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
1806
1807 descr->buf = virt_to_phys(buffer->buffer);
1808
1809 return recvl;
1810}
1811
1812static unsigned int handle_all_descr_data(struct e100_serial *info)
1813{
1814 struct etrax_dma_descr *descr;
1815 unsigned int recvl;
1816 unsigned int ret = 0;
1817
1818 while (1)
1819 {
1820 descr = &info->rec_descr[info->cur_rec_descr];
1821
1822 if (descr == phys_to_virt(*info->idescradr))
1823 break;
1824
1825 if (++info->cur_rec_descr == SERIAL_RECV_DESCRIPTORS)
1826 info->cur_rec_descr = 0;
1827
1828 /* find out how many bytes were read */
1829
1830 /* if the eop bit was not set, all data has been received */
1831 if (!(descr->status & d_eop)) {
1832 recvl = descr->sw_len;
1833 } else {
1834 /* otherwise we find the amount of data received here */
1835 recvl = descr->hw_len;
1836 }
1837
1838 /* Reset the status information */
1839 descr->status = 0;
1840
1841 DFLOW( DEBUG_LOG(info->line, "RX %lu\n", recvl);
1842 if (info->port.tty->stopped) {
1843 unsigned char *buf = phys_to_virt(descr->buf);
1844 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[0]);
1845 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[1]);
1846 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[2]);
1847 }
1848 );
1849
1850 /* update stats */
1851 info->icount.rx += recvl;
1852
1853 ret += handle_descr_data(info, descr, recvl);
1854 }
1855
1856 return ret;
1857}
1858
1859static void receive_chars_dma(struct e100_serial *info)
1860{
1861 struct tty_struct *tty;
1862 unsigned char rstat;
1863
1864#ifdef CONFIG_SVINTO_SIM
1865 /* No receive in the simulator. Will probably be when the rest of
1866 * the serial interface works, and this piece will just be removed.
1867 */
1868 return;
1869#endif
1870
1871 /* Acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1872 *info->iclrintradr =
1873 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1874 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1875
1876 tty = info->port.tty;
1877 if (!tty) /* Something wrong... */
1878 return;
1879
1880#ifdef SERIAL_HANDLE_EARLY_ERRORS
1881 if (info->uses_dma_in)
1882 e100_enable_serial_data_irq(info);
1883#endif
1884
1885 if (info->errorcode == ERRCODE_INSERT_BREAK)
1886 add_char_and_flag(info, '\0', TTY_BREAK);
1887
1888 handle_all_descr_data(info);
1889
1890 /* Read the status register to detect errors */
1891 rstat = info->ioport[REG_STATUS];
1892 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
1893 DFLOW(DEBUG_LOG(info->line, "XOFF detect stat %x\n", rstat));
1894 }
1895
1896 if (rstat & SER_ERROR_MASK) {
1897 /* If we got an error, we must reset it by reading the
1898 * data_in field
1899 */
1900 unsigned char data = info->ioport[REG_DATA];
1901
1902 PROCSTAT(ser_stat[info->line].errors_cnt++);
1903 DEBUG_LOG(info->line, "#dERR: s d 0x%04X\n",
1904 ((rstat & SER_ERROR_MASK) << 8) | data);
1905
1906 if (rstat & SER_PAR_ERR_MASK)
1907 add_char_and_flag(info, data, TTY_PARITY);
1908 else if (rstat & SER_OVERRUN_MASK)
1909 add_char_and_flag(info, data, TTY_OVERRUN);
1910 else if (rstat & SER_FRAMING_ERR_MASK)
1911 add_char_and_flag(info, data, TTY_FRAME);
1912 }
1913
1914 START_FLUSH_FAST_TIMER(info, "receive_chars");
1915
1916 /* Restart the receiving DMA */
1917 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
1918}
1919
1920static int start_recv_dma(struct e100_serial *info)
1921{
1922 struct etrax_dma_descr *descr = info->rec_descr;
1923 struct etrax_recv_buffer *buffer;
1924 int i;
1925
1926 /* Set up the receiving descriptors */
1927 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++) {
1928 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
1929 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
1930
1931 descr[i].ctrl = d_int;
1932 descr[i].buf = virt_to_phys(buffer->buffer);
1933 descr[i].sw_len = SERIAL_DESCR_BUF_SIZE;
1934 descr[i].hw_len = 0;
1935 descr[i].status = 0;
1936 descr[i].next = virt_to_phys(&descr[i+1]);
1937 }
1938
1939 /* Link the last descriptor to the first */
1940 descr[i-1].next = virt_to_phys(&descr[0]);
1941
1942 /* Start with the first descriptor in the list */
1943 info->cur_rec_descr = 0;
1944
1945 /* Start the DMA */
1946 *info->ifirstadr = virt_to_phys(&descr[info->cur_rec_descr]);
1947 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1948
1949 /* Input DMA should be running now */
1950 return 1;
1951}
1952
1953static void
1954start_receive(struct e100_serial *info)
1955{
1956#ifdef CONFIG_SVINTO_SIM
1957 /* No receive in the simulator. Will probably be when the rest of
1958 * the serial interface works, and this piece will just be removed.
1959 */
1960 return;
1961#endif
1962 if (info->uses_dma_in) {
1963 /* reset the input dma channel to be sure it works */
1964
1965 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1966 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
1967 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
1968
1969 start_recv_dma(info);
1970 }
1971}
1972
1973
1974/* the bits in the MASK2 register are laid out like this:
1975 DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
1976 where I is the input channel and O is the output channel for the port.
1977 info->irq is the bit number for the DMAO_DESCR so to check the others we
1978 shift info->irq to the left.
1979*/
1980
1981/* dma output channel interrupt handler
1982 this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
1983 DMA8(ser1) when they have finished a descriptor with the intr flag set.
1984*/
1985
1986static irqreturn_t
1987tr_interrupt(int irq, void *dev_id)
1988{
1989 struct e100_serial *info;
1990 unsigned long ireg;
1991 int i;
1992 int handled = 0;
1993
1994#ifdef CONFIG_SVINTO_SIM
1995 /* No receive in the simulator. Will probably be when the rest of
1996 * the serial interface works, and this piece will just be removed.
1997 */
1998 {
1999 const char *s = "What? tr_interrupt in simulator??\n";
2000 SIMCOUT(s,strlen(s));
2001 }
2002 return IRQ_HANDLED;
2003#endif
2004
2005 /* find out the line that caused this irq and get it from rs_table */
2006
2007 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
2008
2009 for (i = 0; i < NR_PORTS; i++) {
2010 info = rs_table + i;
2011 if (!info->enabled || !info->uses_dma_out)
2012 continue;
2013 /* check for dma_descr (don't need to check for dma_eop in output dma for serial */
2014 if (ireg & info->irq) {
2015 handled = 1;
2016 /* we can send a new dma bunch. make it so. */
2017 DINTR2(DEBUG_LOG(info->line, "tr_interrupt %i\n", i));
2018 /* Read jiffies_usec first,
2019 * we want this time to be as late as possible
2020 */
2021 PROCSTAT(ser_stat[info->line].tx_dma_ints++);
2022 info->last_tx_active_usec = GET_JIFFIES_USEC();
2023 info->last_tx_active = jiffies;
2024 transmit_chars_dma(info);
2025 }
2026
2027 /* FIXME: here we should really check for a change in the
2028 status lines and if so call status_handle(info) */
2029 }
2030 return IRQ_RETVAL(handled);
2031} /* tr_interrupt */
2032
2033/* dma input channel interrupt handler */
2034
2035static irqreturn_t
2036rec_interrupt(int irq, void *dev_id)
2037{
2038 struct e100_serial *info;
2039 unsigned long ireg;
2040 int i;
2041 int handled = 0;
2042
2043#ifdef CONFIG_SVINTO_SIM
2044 /* No receive in the simulator. Will probably be when the rest of
2045 * the serial interface works, and this piece will just be removed.
2046 */
2047 {
2048 const char *s = "What? rec_interrupt in simulator??\n";
2049 SIMCOUT(s,strlen(s));
2050 }
2051 return IRQ_HANDLED;
2052#endif
2053
2054 /* find out the line that caused this irq and get it from rs_table */
2055
2056 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
2057
2058 for (i = 0; i < NR_PORTS; i++) {
2059 info = rs_table + i;
2060 if (!info->enabled || !info->uses_dma_in)
2061 continue;
2062 /* check for both dma_eop and dma_descr for the input dma channel */
2063 if (ireg & ((info->irq << 2) | (info->irq << 3))) {
2064 handled = 1;
2065 /* we have received something */
2066 receive_chars_dma(info);
2067 }
2068
2069 /* FIXME: here we should really check for a change in the
2070 status lines and if so call status_handle(info) */
2071 }
2072 return IRQ_RETVAL(handled);
2073} /* rec_interrupt */
2074
2075static int force_eop_if_needed(struct e100_serial *info)
2076{
2077 /* We check data_avail bit to determine if data has
2078 * arrived since last time
2079 */
2080 unsigned char rstat = info->ioport[REG_STATUS];
2081
2082 /* error or datavail? */
2083 if (rstat & SER_ERROR_MASK) {
2084 /* Some error has occurred. If there has been valid data, an
2085 * EOP interrupt will be made automatically. If no data, the
2086 * normal ser_interrupt should be enabled and handle it.
2087 * So do nothing!
2088 */
2089 DEBUG_LOG(info->line, "timeout err: rstat 0x%03X\n",
2090 rstat | (info->line << 8));
2091 return 0;
2092 }
2093
2094 if (rstat & SER_DATA_AVAIL_MASK) {
2095 /* Ok data, no error, count it */
2096 TIMERD(DEBUG_LOG(info->line, "timeout: rstat 0x%03X\n",
2097 rstat | (info->line << 8)));
2098 /* Read data to clear status flags */
2099 (void)info->ioport[REG_DATA];
2100
2101 info->forced_eop = 0;
2102 START_FLUSH_FAST_TIMER(info, "magic");
2103 return 0;
2104 }
2105
2106 /* hit the timeout, force an EOP for the input
2107 * dma channel if we haven't already
2108 */
2109 if (!info->forced_eop) {
2110 info->forced_eop = 1;
2111 PROCSTAT(ser_stat[info->line].timeout_flush_cnt++);
2112 TIMERD(DEBUG_LOG(info->line, "timeout EOP %i\n", info->line));
2113 FORCE_EOP(info);
2114 }
2115
2116 return 1;
2117}
2118
2119static void flush_to_flip_buffer(struct e100_serial *info)
2120{
2121 struct tty_struct *tty;
2122 struct etrax_recv_buffer *buffer;
2123 unsigned long flags;
2124
2125 local_irq_save(flags);
2126 tty = info->port.tty;
2127
2128 if (!tty) {
2129 local_irq_restore(flags);
2130 return;
2131 }
2132
2133 while ((buffer = info->first_recv_buffer) != NULL) {
2134 unsigned int count = buffer->length;
2135
2136 tty_insert_flip_string(tty, buffer->buffer, count);
2137 info->recv_cnt -= count;
2138
2139 if (count == buffer->length) {
2140 info->first_recv_buffer = buffer->next;
2141 kfree(buffer);
2142 } else {
2143 buffer->length -= count;
2144 memmove(buffer->buffer, buffer->buffer + count, buffer->length);
2145 buffer->error = TTY_NORMAL;
2146 }
2147 }
2148
2149 if (!info->first_recv_buffer)
2150 info->last_recv_buffer = NULL;
2151
2152 local_irq_restore(flags);
2153
2154 /* This includes a check for low-latency */
2155 tty_flip_buffer_push(tty);
2156}
2157
2158static void check_flush_timeout(struct e100_serial *info)
2159{
2160 /* Flip what we've got (if we can) */
2161 flush_to_flip_buffer(info);
2162
2163 /* We might need to flip later, but not to fast
2164 * since the system is busy processing input... */
2165 if (info->first_recv_buffer)
2166 START_FLUSH_FAST_TIMER_TIME(info, "flip", 2000);
2167
2168 /* Force eop last, since data might have come while we're processing
2169 * and if we started the slow timer above, we won't start a fast
2170 * below.
2171 */
2172 force_eop_if_needed(info);
2173}
2174
2175#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
2176static void flush_timeout_function(unsigned long data)
2177{
2178 struct e100_serial *info = (struct e100_serial *)data;
2179
2180 fast_timers[info->line].function = NULL;
2181 serial_fast_timer_expired++;
2182 TIMERD(DEBUG_LOG(info->line, "flush_timout %i ", info->line));
2183 TIMERD(DEBUG_LOG(info->line, "num expired: %i\n", serial_fast_timer_expired));
2184 check_flush_timeout(info);
2185}
2186
2187#else
2188
2189/* dma fifo/buffer timeout handler
2190 forces an end-of-packet for the dma input channel if no chars
2191 have been received for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS/100 s.
2192*/
2193
2194static struct timer_list flush_timer;
2195
2196static void
2197timed_flush_handler(unsigned long ptr)
2198{
2199 struct e100_serial *info;
2200 int i;
2201
2202#ifdef CONFIG_SVINTO_SIM
2203 return;
2204#endif
2205
2206 for (i = 0; i < NR_PORTS; i++) {
2207 info = rs_table + i;
2208 if (info->uses_dma_in)
2209 check_flush_timeout(info);
2210 }
2211
2212 /* restart flush timer */
2213 mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
2214}
2215#endif
2216
2217#ifdef SERIAL_HANDLE_EARLY_ERRORS
2218
2219/* If there is an error (ie break) when the DMA is running and
2220 * there are no bytes in the fifo the DMA is stopped and we get no
2221 * eop interrupt. Thus we have to monitor the first bytes on a DMA
2222 * transfer, and if it is without error we can turn the serial
2223 * interrupts off.
2224 */
2225
2226/*
2227BREAK handling on ETRAX 100:
2228ETRAX will generate interrupt although there is no stop bit between the
2229characters.
2230
2231Depending on how long the break sequence is, the end of the breaksequence
2232will look differently:
2233| indicates start/end of a character.
2234
2235B= Break character (0x00) with framing error.
2236E= Error byte with parity error received after B characters.
2237F= "Faked" valid byte received immediately after B characters.
2238V= Valid byte
2239
22401.
2241 B BL ___________________________ V
2242.._|__________|__________| |valid data |
2243
2244Multiple frame errors with data == 0x00 (B),
2245the timing matches up "perfectly" so no extra ending char is detected.
2246The RXD pin is 1 in the last interrupt, in that case
2247we set info->errorcode = ERRCODE_INSERT_BREAK, but we can't really
2248know if another byte will come and this really is case 2. below
2249(e.g F=0xFF or 0xFE)
2250If RXD pin is 0 we can expect another character (see 2. below).
2251
2252
22532.
2254
2255 B B E or F__________________..__ V
2256.._|__________|__________|______ | |valid data
2257 "valid" or
2258 parity error
2259
2260Multiple frame errors with data == 0x00 (B),
2261but the part of the break trigs is interpreted as a start bit (and possibly
2262some 0 bits followed by a number of 1 bits and a stop bit).
2263Depending on parity settings etc. this last character can be either
2264a fake "valid" char (F) or have a parity error (E).
2265
2266If the character is valid it will be put in the buffer,
2267we set info->errorcode = ERRCODE_SET_BREAK so the receive interrupt
2268will set the flags so the tty will handle it,
2269if it's an error byte it will not be put in the buffer
2270and we set info->errorcode = ERRCODE_INSERT_BREAK.
2271
2272To distinguish a V byte in 1. from an F byte in 2. we keep a timestamp
2273of the last faulty char (B) and compares it with the current time:
2274If the time elapsed time is less then 2*char_time_usec we will assume
2275it's a faked F char and not a Valid char and set
2276info->errorcode = ERRCODE_SET_BREAK.
2277
2278Flaws in the above solution:
2279~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2280We use the timer to distinguish a F character from a V character,
2281if a V character is to close after the break we might make the wrong decision.
2282
2283TODO: The break will be delayed until an F or V character is received.
2284
2285*/
2286
2287static
2288struct e100_serial * handle_ser_rx_interrupt_no_dma(struct e100_serial *info)
2289{
2290 unsigned long data_read;
2291 struct tty_struct *tty = info->port.tty;
2292
2293 if (!tty) {
2294 printk("!NO TTY!\n");
2295 return info;
2296 }
2297
2298 /* Read data and status at the same time */
2299 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
2300more_data:
2301 if (data_read & IO_MASK(R_SERIAL0_READ, xoff_detect) ) {
2302 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2303 }
2304 DINTR2(DEBUG_LOG(info->line, "ser_rx %c\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read)));
2305
2306 if (data_read & ( IO_MASK(R_SERIAL0_READ, framing_err) |
2307 IO_MASK(R_SERIAL0_READ, par_err) |
2308 IO_MASK(R_SERIAL0_READ, overrun) )) {
2309 /* An error */
2310 info->last_rx_active_usec = GET_JIFFIES_USEC();
2311 info->last_rx_active = jiffies;
2312 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat_data %04X\n", data_read));
2313 DLOG_INT_TRIG(
2314 if (!log_int_trig1_pos) {
2315 log_int_trig1_pos = log_int_pos;
2316 log_int(rdpc(), 0, 0);
2317 }
2318 );
2319
2320
2321 if ( ((data_read & IO_MASK(R_SERIAL0_READ, data_in)) == 0) &&
2322 (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) ) {
2323 /* Most likely a break, but we get interrupts over and
2324 * over again.
2325 */
2326
2327 if (!info->break_detected_cnt) {
2328 DEBUG_LOG(info->line, "#BRK start\n", 0);
2329 }
2330 if (data_read & IO_MASK(R_SERIAL0_READ, rxd)) {
2331 /* The RX pin is high now, so the break
2332 * must be over, but....
2333 * we can't really know if we will get another
2334 * last byte ending the break or not.
2335 * And we don't know if the byte (if any) will
2336 * have an error or look valid.
2337 */
2338 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2339 info->errorcode = ERRCODE_INSERT_BREAK;
2340 }
2341 info->break_detected_cnt++;
2342 } else {
2343 /* The error does not look like a break, but could be
2344 * the end of one
2345 */
2346 if (info->break_detected_cnt) {
2347 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2348 info->errorcode = ERRCODE_INSERT_BREAK;
2349 } else {
2350 unsigned char data = IO_EXTRACT(R_SERIAL0_READ,
2351 data_in, data_read);
2352 char flag = TTY_NORMAL;
2353 if (info->errorcode == ERRCODE_INSERT_BREAK) {
2354 struct tty_struct *tty = info->port.tty;
2355 tty_insert_flip_char(tty, 0, flag);
2356 info->icount.rx++;
2357 }
2358
2359 if (data_read & IO_MASK(R_SERIAL0_READ, par_err)) {
2360 info->icount.parity++;
2361 flag = TTY_PARITY;
2362 } else if (data_read & IO_MASK(R_SERIAL0_READ, overrun)) {
2363 info->icount.overrun++;
2364 flag = TTY_OVERRUN;
2365 } else if (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) {
2366 info->icount.frame++;
2367 flag = TTY_FRAME;
2368 }
2369 tty_insert_flip_char(tty, data, flag);
2370 info->errorcode = 0;
2371 }
2372 info->break_detected_cnt = 0;
2373 }
2374 } else if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2375 /* No error */
2376 DLOG_INT_TRIG(
2377 if (!log_int_trig1_pos) {
2378 if (log_int_pos >= log_int_size) {
2379 log_int_pos = 0;
2380 }
2381 log_int_trig0_pos = log_int_pos;
2382 log_int(rdpc(), 0, 0);
2383 }
2384 );
2385 tty_insert_flip_char(tty,
2386 IO_EXTRACT(R_SERIAL0_READ, data_in, data_read),
2387 TTY_NORMAL);
2388 } else {
2389 DEBUG_LOG(info->line, "ser_rx int but no data_avail %08lX\n", data_read);
2390 }
2391
2392
2393 info->icount.rx++;
2394 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
2395 if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2396 DEBUG_LOG(info->line, "ser_rx %c in loop\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read));
2397 goto more_data;
2398 }
2399
2400 tty_flip_buffer_push(info->port.tty);
2401 return info;
2402}
2403
2404static struct e100_serial* handle_ser_rx_interrupt(struct e100_serial *info)
2405{
2406 unsigned char rstat;
2407
2408#ifdef SERIAL_DEBUG_INTR
2409 printk("Interrupt from serport %d\n", i);
2410#endif
2411/* DEBUG_LOG(info->line, "ser_interrupt stat %03X\n", rstat | (i << 8)); */
2412 if (!info->uses_dma_in) {
2413 return handle_ser_rx_interrupt_no_dma(info);
2414 }
2415 /* DMA is used */
2416 rstat = info->ioport[REG_STATUS];
2417 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
2418 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2419 }
2420
2421 if (rstat & SER_ERROR_MASK) {
2422 unsigned char data;
2423
2424 info->last_rx_active_usec = GET_JIFFIES_USEC();
2425 info->last_rx_active = jiffies;
2426 /* If we got an error, we must reset it by reading the
2427 * data_in field
2428 */
2429 data = info->ioport[REG_DATA];
2430 DINTR1(DEBUG_LOG(info->line, "ser_rx! %c\n", data));
2431 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat %02X\n", rstat));
2432 if (!data && (rstat & SER_FRAMING_ERR_MASK)) {
2433 /* Most likely a break, but we get interrupts over and
2434 * over again.
2435 */
2436
2437 if (!info->break_detected_cnt) {
2438 DEBUG_LOG(info->line, "#BRK start\n", 0);
2439 }
2440 if (rstat & SER_RXD_MASK) {
2441 /* The RX pin is high now, so the break
2442 * must be over, but....
2443 * we can't really know if we will get another
2444 * last byte ending the break or not.
2445 * And we don't know if the byte (if any) will
2446 * have an error or look valid.
2447 */
2448 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2449 info->errorcode = ERRCODE_INSERT_BREAK;
2450 }
2451 info->break_detected_cnt++;
2452 } else {
2453 /* The error does not look like a break, but could be
2454 * the end of one
2455 */
2456 if (info->break_detected_cnt) {
2457 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2458 info->errorcode = ERRCODE_INSERT_BREAK;
2459 } else {
2460 if (info->errorcode == ERRCODE_INSERT_BREAK) {
2461 info->icount.brk++;
2462 add_char_and_flag(info, '\0', TTY_BREAK);
2463 }
2464
2465 if (rstat & SER_PAR_ERR_MASK) {
2466 info->icount.parity++;
2467 add_char_and_flag(info, data, TTY_PARITY);
2468 } else if (rstat & SER_OVERRUN_MASK) {
2469 info->icount.overrun++;
2470 add_char_and_flag(info, data, TTY_OVERRUN);
2471 } else if (rstat & SER_FRAMING_ERR_MASK) {
2472 info->icount.frame++;
2473 add_char_and_flag(info, data, TTY_FRAME);
2474 }
2475
2476 info->errorcode = 0;
2477 }
2478 info->break_detected_cnt = 0;
2479 DEBUG_LOG(info->line, "#iERR s d %04X\n",
2480 ((rstat & SER_ERROR_MASK) << 8) | data);
2481 }
2482 PROCSTAT(ser_stat[info->line].early_errors_cnt++);
2483 } else { /* It was a valid byte, now let the DMA do the rest */
2484 unsigned long curr_time_u = GET_JIFFIES_USEC();
2485 unsigned long curr_time = jiffies;
2486
2487 if (info->break_detected_cnt) {
2488 /* Detect if this character is a new valid char or the
2489 * last char in a break sequence: If LSBits are 0 and
2490 * MSBits are high AND the time is close to the
2491 * previous interrupt we should discard it.
2492 */
2493 long elapsed_usec =
2494 (curr_time - info->last_rx_active) * (1000000/HZ) +
2495 curr_time_u - info->last_rx_active_usec;
2496 if (elapsed_usec < 2*info->char_time_usec) {
2497 DEBUG_LOG(info->line, "FBRK %i\n", info->line);
2498 /* Report as BREAK (error) and let
2499 * receive_chars_dma() handle it
2500 */
2501 info->errorcode = ERRCODE_SET_BREAK;
2502 } else {
2503 DEBUG_LOG(info->line, "Not end of BRK (V)%i\n", info->line);
2504 }
2505 DEBUG_LOG(info->line, "num brk %i\n", info->break_detected_cnt);
2506 }
2507
2508#ifdef SERIAL_DEBUG_INTR
2509 printk("** OK, disabling ser_interrupts\n");
2510#endif
2511 e100_disable_serial_data_irq(info);
2512 DINTR2(DEBUG_LOG(info->line, "ser_rx OK %d\n", info->line));
2513 info->break_detected_cnt = 0;
2514
2515 PROCSTAT(ser_stat[info->line].ser_ints_ok_cnt++);
2516 }
2517 /* Restarting the DMA never hurts */
2518 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
2519 START_FLUSH_FAST_TIMER(info, "ser_int");
2520 return info;
2521} /* handle_ser_rx_interrupt */
2522
2523static void handle_ser_tx_interrupt(struct e100_serial *info)
2524{
2525 unsigned long flags;
2526
2527 if (info->x_char) {
2528 unsigned char rstat;
2529 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar 0x%02X\n", info->x_char));
2530 local_irq_save(flags);
2531 rstat = info->ioport[REG_STATUS];
2532 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2533
2534 info->ioport[REG_TR_DATA] = info->x_char;
2535 info->icount.tx++;
2536 info->x_char = 0;
2537 /* We must enable since it is disabled in ser_interrupt */
2538 e100_enable_serial_tx_ready_irq(info);
2539 local_irq_restore(flags);
2540 return;
2541 }
2542 if (info->uses_dma_out) {
2543 unsigned char rstat;
2544 int i;
2545 /* We only use normal tx interrupt when sending x_char */
2546 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar sent\n", 0));
2547 local_irq_save(flags);
2548 rstat = info->ioport[REG_STATUS];
2549 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2550 e100_disable_serial_tx_ready_irq(info);
2551 if (info->port.tty->stopped)
2552 rs_stop(info->port.tty);
2553 /* Enable the DMA channel and tell it to continue */
2554 e100_enable_txdma_channel(info);
2555 /* Wait 12 cycles before doing the DMA command */
2556 for(i = 6; i > 0; i--)
2557 nop();
2558
2559 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, continue);
2560 local_irq_restore(flags);
2561 return;
2562 }
2563 /* Normal char-by-char interrupt */
2564 if (info->xmit.head == info->xmit.tail
2565 || info->port.tty->stopped
2566 || info->port.tty->hw_stopped) {
2567 DFLOW(DEBUG_LOG(info->line, "tx_int: stopped %i\n",
2568 info->port.tty->stopped));
2569 e100_disable_serial_tx_ready_irq(info);
2570 info->tr_running = 0;
2571 return;
2572 }
2573 DINTR2(DEBUG_LOG(info->line, "tx_int %c\n", info->xmit.buf[info->xmit.tail]));
2574 /* Send a byte, rs485 timing is critical so turn of ints */
2575 local_irq_save(flags);
2576 info->ioport[REG_TR_DATA] = info->xmit.buf[info->xmit.tail];
2577 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
2578 info->icount.tx++;
2579 if (info->xmit.head == info->xmit.tail) {
2580#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
2581 if (info->rs485.flags & SER_RS485_ENABLED) {
2582 /* Set a short timer to toggle RTS */
2583 start_one_shot_timer(&fast_timers_rs485[info->line],
2584 rs485_toggle_rts_timer_function,
2585 (unsigned long)info,
2586 info->char_time_usec*2,
2587 "RS-485");
2588 }
2589#endif /* RS485 */
2590 info->last_tx_active_usec = GET_JIFFIES_USEC();
2591 info->last_tx_active = jiffies;
2592 e100_disable_serial_tx_ready_irq(info);
2593 info->tr_running = 0;
2594 DFLOW(DEBUG_LOG(info->line, "tx_int: stop2\n", 0));
2595 } else {
2596 /* We must enable since it is disabled in ser_interrupt */
2597 e100_enable_serial_tx_ready_irq(info);
2598 }
2599 local_irq_restore(flags);
2600
2601 if (CIRC_CNT(info->xmit.head,
2602 info->xmit.tail,
2603 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
2604 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
2605
2606} /* handle_ser_tx_interrupt */
2607
2608/* result of time measurements:
2609 * RX duration 54-60 us when doing something, otherwise 6-9 us
2610 * ser_int duration: just sending: 8-15 us normally, up to 73 us
2611 */
2612static irqreturn_t
2613ser_interrupt(int irq, void *dev_id)
2614{
2615 static volatile int tx_started = 0;
2616 struct e100_serial *info;
2617 int i;
2618 unsigned long flags;
2619 unsigned long irq_mask1_rd;
2620 unsigned long data_mask = (1 << (8+2*0)); /* ser0 data_avail */
2621 int handled = 0;
2622 static volatile unsigned long reentered_ready_mask = 0;
2623
2624 local_irq_save(flags);
2625 irq_mask1_rd = *R_IRQ_MASK1_RD;
2626 /* First handle all rx interrupts with ints disabled */
2627 info = rs_table;
2628 irq_mask1_rd &= e100_ser_int_mask;
2629 for (i = 0; i < NR_PORTS; i++) {
2630 /* Which line caused the data irq? */
2631 if (irq_mask1_rd & data_mask) {
2632 handled = 1;
2633 handle_ser_rx_interrupt(info);
2634 }
2635 info += 1;
2636 data_mask <<= 2;
2637 }
2638 /* Handle tx interrupts with interrupts enabled so we
2639 * can take care of new data interrupts while transmitting
2640 * We protect the tx part with the tx_started flag.
2641 * We disable the tr_ready interrupts we are about to handle and
2642 * unblock the serial interrupt so new serial interrupts may come.
2643 *
2644 * If we get a new interrupt:
2645 * - it migth be due to synchronous serial ports.
2646 * - serial irq will be blocked by general irq handler.
2647 * - async data will be handled above (sync will be ignored).
2648 * - tx_started flag will prevent us from trying to send again and
2649 * we will exit fast - no need to unblock serial irq.
2650 * - Next (sync) serial interrupt handler will be runned with
2651 * disabled interrupt due to restore_flags() at end of function,
2652 * so sync handler will not be preempted or reentered.
2653 */
2654 if (!tx_started) {
2655 unsigned long ready_mask;
2656 unsigned long
2657 tx_started = 1;
2658 /* Only the tr_ready interrupts left */
2659 irq_mask1_rd &= (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2660 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2661 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2662 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2663 while (irq_mask1_rd) {
2664 /* Disable those we are about to handle */
2665 *R_IRQ_MASK1_CLR = irq_mask1_rd;
2666 /* Unblock the serial interrupt */
2667 *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
2668
2669 local_irq_enable();
2670 ready_mask = (1 << (8+1+2*0)); /* ser0 tr_ready */
2671 info = rs_table;
2672 for (i = 0; i < NR_PORTS; i++) {
2673 /* Which line caused the ready irq? */
2674 if (irq_mask1_rd & ready_mask) {
2675 handled = 1;
2676 handle_ser_tx_interrupt(info);
2677 }
2678 info += 1;
2679 ready_mask <<= 2;
2680 }
2681 /* handle_ser_tx_interrupt enables tr_ready interrupts */
2682 local_irq_disable();
2683 /* Handle reentered TX interrupt */
2684 irq_mask1_rd = reentered_ready_mask;
2685 }
2686 local_irq_disable();
2687 tx_started = 0;
2688 } else {
2689 unsigned long ready_mask;
2690 ready_mask = irq_mask1_rd & (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2691 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2692 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2693 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2694 if (ready_mask) {
2695 reentered_ready_mask |= ready_mask;
2696 /* Disable those we are about to handle */
2697 *R_IRQ_MASK1_CLR = ready_mask;
2698 DFLOW(DEBUG_LOG(SERIAL_DEBUG_LINE, "ser_int reentered with TX %X\n", ready_mask));
2699 }
2700 }
2701
2702 local_irq_restore(flags);
2703 return IRQ_RETVAL(handled);
2704} /* ser_interrupt */
2705#endif
2706
2707/*
2708 * -------------------------------------------------------------------
2709 * Here ends the serial interrupt routines.
2710 * -------------------------------------------------------------------
2711 */
2712
2713/*
2714 * This routine is used to handle the "bottom half" processing for the
2715 * serial driver, known also the "software interrupt" processing.
2716 * This processing is done at the kernel interrupt level, after the
2717 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
2718 * is where time-consuming activities which can not be done in the
2719 * interrupt driver proper are done; the interrupt driver schedules
2720 * them using rs_sched_event(), and they get done here.
2721 */
2722static void
2723do_softint(struct work_struct *work)
2724{
2725 struct e100_serial *info;
2726 struct tty_struct *tty;
2727
2728 info = container_of(work, struct e100_serial, work);
2729
2730 tty = info->port.tty;
2731 if (!tty)
2732 return;
2733
2734 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
2735 tty_wakeup(tty);
2736}
2737
2738static int
2739startup(struct e100_serial * info)
2740{
2741 unsigned long flags;
2742 unsigned long xmit_page;
2743 int i;
2744
2745 xmit_page = get_zeroed_page(GFP_KERNEL);
2746 if (!xmit_page)
2747 return -ENOMEM;
2748
2749 local_irq_save(flags);
2750
2751 /* if it was already initialized, skip this */
2752
2753 if (info->flags & ASYNC_INITIALIZED) {
2754 local_irq_restore(flags);
2755 free_page(xmit_page);
2756 return 0;
2757 }
2758
2759 if (info->xmit.buf)
2760 free_page(xmit_page);
2761 else
2762 info->xmit.buf = (unsigned char *) xmit_page;
2763
2764#ifdef SERIAL_DEBUG_OPEN
2765 printk("starting up ttyS%d (xmit_buf 0x%p)...\n", info->line, info->xmit.buf);
2766#endif
2767
2768#ifdef CONFIG_SVINTO_SIM
2769 /* Bits and pieces collected from below. Better to have them
2770 in one ifdef:ed clause than to mix in a lot of ifdefs,
2771 right? */
2772 if (info->port.tty)
2773 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2774
2775 info->xmit.head = info->xmit.tail = 0;
2776 info->first_recv_buffer = info->last_recv_buffer = NULL;
2777 info->recv_cnt = info->max_recv_cnt = 0;
2778
2779 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2780 info->rec_descr[i].buf = NULL;
2781
2782 /* No real action in the simulator, but may set info important
2783 to ioctl. */
2784 change_speed(info);
2785#else
2786
2787 /*
2788 * Clear the FIFO buffers and disable them
2789 * (they will be reenabled in change_speed())
2790 */
2791
2792 /*
2793 * Reset the DMA channels and make sure their interrupts are cleared
2794 */
2795
2796 if (info->dma_in_enabled) {
2797 info->uses_dma_in = 1;
2798 e100_enable_rxdma_channel(info);
2799
2800 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2801
2802 /* Wait until reset cycle is complete */
2803 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
2804 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2805
2806 /* Make sure the irqs are cleared */
2807 *info->iclrintradr =
2808 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2809 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2810 } else {
2811 e100_disable_rxdma_channel(info);
2812 }
2813
2814 if (info->dma_out_enabled) {
2815 info->uses_dma_out = 1;
2816 e100_enable_txdma_channel(info);
2817 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2818
2819 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) ==
2820 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2821
2822 /* Make sure the irqs are cleared */
2823 *info->oclrintradr =
2824 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2825 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2826 } else {
2827 e100_disable_txdma_channel(info);
2828 }
2829
2830 if (info->port.tty)
2831 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2832
2833 info->xmit.head = info->xmit.tail = 0;
2834 info->first_recv_buffer = info->last_recv_buffer = NULL;
2835 info->recv_cnt = info->max_recv_cnt = 0;
2836
2837 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2838 info->rec_descr[i].buf = 0;
2839
2840 /*
2841 * and set the speed and other flags of the serial port
2842 * this will start the rx/tx as well
2843 */
2844#ifdef SERIAL_HANDLE_EARLY_ERRORS
2845 e100_enable_serial_data_irq(info);
2846#endif
2847 change_speed(info);
2848
2849 /* dummy read to reset any serial errors */
2850
2851 (void)info->ioport[REG_DATA];
2852
2853 /* enable the interrupts */
2854 if (info->uses_dma_out)
2855 e100_enable_txdma_irq(info);
2856
2857 e100_enable_rx_irq(info);
2858
2859 info->tr_running = 0; /* to be sure we don't lock up the transmitter */
2860
2861 /* setup the dma input descriptor and start dma */
2862
2863 start_receive(info);
2864
2865 /* for safety, make sure the descriptors last result is 0 bytes written */
2866
2867 info->tr_descr.sw_len = 0;
2868 info->tr_descr.hw_len = 0;
2869 info->tr_descr.status = 0;
2870
2871 /* enable RTS/DTR last */
2872
2873 e100_rts(info, 1);
2874 e100_dtr(info, 1);
2875
2876#endif /* CONFIG_SVINTO_SIM */
2877
2878 info->flags |= ASYNC_INITIALIZED;
2879
2880 local_irq_restore(flags);
2881 return 0;
2882}
2883
2884/*
2885 * This routine will shutdown a serial port; interrupts are disabled, and
2886 * DTR is dropped if the hangup on close termio flag is on.
2887 */
2888static void
2889shutdown(struct e100_serial * info)
2890{
2891 unsigned long flags;
2892 struct etrax_dma_descr *descr = info->rec_descr;
2893 struct etrax_recv_buffer *buffer;
2894 int i;
2895
2896#ifndef CONFIG_SVINTO_SIM
2897 /* shut down the transmitter and receiver */
2898 DFLOW(DEBUG_LOG(info->line, "shutdown %i\n", info->line));
2899 e100_disable_rx(info);
2900 info->ioport[REG_TR_CTRL] = (info->tx_ctrl &= ~0x40);
2901
2902 /* disable interrupts, reset dma channels */
2903 if (info->uses_dma_in) {
2904 e100_disable_rxdma_irq(info);
2905 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2906 info->uses_dma_in = 0;
2907 } else {
2908 e100_disable_serial_data_irq(info);
2909 }
2910
2911 if (info->uses_dma_out) {
2912 e100_disable_txdma_irq(info);
2913 info->tr_running = 0;
2914 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2915 info->uses_dma_out = 0;
2916 } else {
2917 e100_disable_serial_tx_ready_irq(info);
2918 info->tr_running = 0;
2919 }
2920
2921#endif /* CONFIG_SVINTO_SIM */
2922
2923 if (!(info->flags & ASYNC_INITIALIZED))
2924 return;
2925
2926#ifdef SERIAL_DEBUG_OPEN
2927 printk("Shutting down serial port %d (irq %d)....\n", info->line,
2928 info->irq);
2929#endif
2930
2931 local_irq_save(flags);
2932
2933 if (info->xmit.buf) {
2934 free_page((unsigned long)info->xmit.buf);
2935 info->xmit.buf = NULL;
2936 }
2937
2938 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2939 if (descr[i].buf) {
2940 buffer = phys_to_virt(descr[i].buf) - sizeof *buffer;
2941 kfree(buffer);
2942 descr[i].buf = 0;
2943 }
2944
2945 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL)) {
2946 /* hang up DTR and RTS if HUPCL is enabled */
2947 e100_dtr(info, 0);
2948 e100_rts(info, 0); /* could check CRTSCTS before doing this */
2949 }
2950
2951 if (info->port.tty)
2952 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
2953
2954 info->flags &= ~ASYNC_INITIALIZED;
2955 local_irq_restore(flags);
2956}
2957
2958
2959/* change baud rate and other assorted parameters */
2960
2961static void
2962change_speed(struct e100_serial *info)
2963{
2964 unsigned int cflag;
2965 unsigned long xoff;
2966 unsigned long flags;
2967 /* first some safety checks */
2968
2969 if (!info->port.tty || !info->port.tty->termios)
2970 return;
2971 if (!info->ioport)
2972 return;
2973
2974 cflag = info->port.tty->termios->c_cflag;
2975
2976 /* possibly, the tx/rx should be disabled first to do this safely */
2977
2978 /* change baud-rate and write it to the hardware */
2979 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) {
2980 /* Special baudrate */
2981 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
2982 unsigned long alt_source =
2983 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
2984 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
2985 /* R_ALT_SER_BAUDRATE selects the source */
2986 DBAUD(printk("Custom baudrate: baud_base/divisor %lu/%i\n",
2987 (unsigned long)info->baud_base, info->custom_divisor));
2988 if (info->baud_base == SERIAL_PRESCALE_BASE) {
2989 /* 0, 2-65535 (0=65536) */
2990 u16 divisor = info->custom_divisor;
2991 /* R_SERIAL_PRESCALE (upper 16 bits of R_CLOCK_PRESCALE) */
2992 /* baudrate is 3.125MHz/custom_divisor */
2993 alt_source =
2994 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, prescale) |
2995 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, prescale);
2996 alt_source = 0x11;
2997 DBAUD(printk("Writing SERIAL_PRESCALE: divisor %i\n", divisor));
2998 *R_SERIAL_PRESCALE = divisor;
2999 info->baud = SERIAL_PRESCALE_BASE/divisor;
3000 }
3001#ifdef CONFIG_ETRAX_EXTERN_PB6CLK_ENABLED
3002 else if ((info->baud_base==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8 &&
3003 info->custom_divisor == 1) ||
3004 (info->baud_base==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ &&
3005 info->custom_divisor == 8)) {
3006 /* ext_clk selected */
3007 alt_source =
3008 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, extern) |
3009 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, extern);
3010 DBAUD(printk("using external baudrate: %lu\n", CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8));
3011 info->baud = CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8;
3012 }
3013#endif
3014 else
3015 {
3016 /* Bad baudbase, we don't support using timer0
3017 * for baudrate.
3018 */
3019 printk(KERN_WARNING "Bad baud_base/custom_divisor: %lu/%i\n",
3020 (unsigned long)info->baud_base, info->custom_divisor);
3021 }
3022 r_alt_ser_baudrate_shadow &= ~mask;
3023 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
3024 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
3025 } else {
3026 /* Normal baudrate */
3027 /* Make sure we use normal baudrate */
3028 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
3029 unsigned long alt_source =
3030 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
3031 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
3032 r_alt_ser_baudrate_shadow &= ~mask;
3033 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
3034#ifndef CONFIG_SVINTO_SIM
3035 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
3036#endif /* CONFIG_SVINTO_SIM */
3037
3038 info->baud = cflag_to_baud(cflag);
3039#ifndef CONFIG_SVINTO_SIM
3040 info->ioport[REG_BAUD] = cflag_to_etrax_baud(cflag);
3041#endif /* CONFIG_SVINTO_SIM */
3042 }
3043
3044#ifndef CONFIG_SVINTO_SIM
3045 /* start with default settings and then fill in changes */
3046 local_irq_save(flags);
3047 /* 8 bit, no/even parity */
3048 info->rx_ctrl &= ~(IO_MASK(R_SERIAL0_REC_CTRL, rec_bitnr) |
3049 IO_MASK(R_SERIAL0_REC_CTRL, rec_par_en) |
3050 IO_MASK(R_SERIAL0_REC_CTRL, rec_par));
3051
3052 /* 8 bit, no/even parity, 1 stop bit, no cts */
3053 info->tx_ctrl &= ~(IO_MASK(R_SERIAL0_TR_CTRL, tr_bitnr) |
3054 IO_MASK(R_SERIAL0_TR_CTRL, tr_par_en) |
3055 IO_MASK(R_SERIAL0_TR_CTRL, tr_par) |
3056 IO_MASK(R_SERIAL0_TR_CTRL, stop_bits) |
3057 IO_MASK(R_SERIAL0_TR_CTRL, auto_cts));
3058
3059 if ((cflag & CSIZE) == CS7) {
3060 /* set 7 bit mode */
3061 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
3062 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
3063 }
3064
3065 if (cflag & CSTOPB) {
3066 /* set 2 stop bit mode */
3067 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, two_bits);
3068 }
3069
3070 if (cflag & PARENB) {
3071 /* enable parity */
3072 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
3073 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
3074 }
3075
3076 if (cflag & CMSPAR) {
3077 /* enable stick parity, PARODD mean Mark which matches ETRAX */
3078 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, stick);
3079 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, stick);
3080 }
3081 if (cflag & PARODD) {
3082 /* set odd parity (or Mark if CMSPAR) */
3083 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
3084 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
3085 }
3086
3087 if (cflag & CRTSCTS) {
3088 /* enable automatic CTS handling */
3089 DFLOW(DEBUG_LOG(info->line, "FLOW auto_cts enabled\n", 0));
3090 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, active);
3091 }
3092
3093 /* make sure the tx and rx are enabled */
3094
3095 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable);
3096 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
3097
3098 /* actually write the control regs to the hardware */
3099
3100 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
3101 info->ioport[REG_REC_CTRL] = info->rx_ctrl;
3102 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->port.tty));
3103 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
3104 if (info->port.tty->termios->c_iflag & IXON ) {
3105 DFLOW(DEBUG_LOG(info->line, "FLOW XOFF enabled 0x%02X\n",
3106 STOP_CHAR(info->port.tty)));
3107 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
3108 }
3109
3110 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
3111 local_irq_restore(flags);
3112#endif /* !CONFIG_SVINTO_SIM */
3113
3114 update_char_time(info);
3115
3116} /* change_speed */
3117
3118/* start transmitting chars NOW */
3119
3120static void
3121rs_flush_chars(struct tty_struct *tty)
3122{
3123 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3124 unsigned long flags;
3125
3126 if (info->tr_running ||
3127 info->xmit.head == info->xmit.tail ||
3128 tty->stopped ||
3129 tty->hw_stopped ||
3130 !info->xmit.buf)
3131 return;
3132
3133#ifdef SERIAL_DEBUG_FLOW
3134 printk("rs_flush_chars\n");
3135#endif
3136
3137 /* this protection might not exactly be necessary here */
3138
3139 local_irq_save(flags);
3140 start_transmit(info);
3141 local_irq_restore(flags);
3142}
3143
3144static int rs_raw_write(struct tty_struct *tty,
3145 const unsigned char *buf, int count)
3146{
3147 int c, ret = 0;
3148 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3149 unsigned long flags;
3150
3151 /* first some sanity checks */
3152
3153 if (!tty || !info->xmit.buf || !tmp_buf)
3154 return 0;
3155
3156#ifdef SERIAL_DEBUG_DATA
3157 if (info->line == SERIAL_DEBUG_LINE)
3158 printk("rs_raw_write (%d), status %d\n",
3159 count, info->ioport[REG_STATUS]);
3160#endif
3161
3162#ifdef CONFIG_SVINTO_SIM
3163 /* Really simple. The output is here and now. */
3164 SIMCOUT(buf, count);
3165 return count;
3166#endif
3167 local_save_flags(flags);
3168 DFLOW(DEBUG_LOG(info->line, "write count %i ", count));
3169 DFLOW(DEBUG_LOG(info->line, "ldisc %i\n", tty->ldisc.chars_in_buffer(tty)));
3170
3171
3172 /* The local_irq_disable/restore_flags pairs below are needed
3173 * because the DMA interrupt handler moves the info->xmit values.
3174 * the memcpy needs to be in the critical region unfortunately,
3175 * because we need to read xmit values, memcpy, write xmit values
3176 * in one atomic operation... this could perhaps be avoided by
3177 * more clever design.
3178 */
3179 local_irq_disable();
3180 while (count) {
3181 c = CIRC_SPACE_TO_END(info->xmit.head,
3182 info->xmit.tail,
3183 SERIAL_XMIT_SIZE);
3184
3185 if (count < c)
3186 c = count;
3187 if (c <= 0)
3188 break;
3189
3190 memcpy(info->xmit.buf + info->xmit.head, buf, c);
3191 info->xmit.head = (info->xmit.head + c) &
3192 (SERIAL_XMIT_SIZE-1);
3193 buf += c;
3194 count -= c;
3195 ret += c;
3196 }
3197 local_irq_restore(flags);
3198
3199 /* enable transmitter if not running, unless the tty is stopped
3200 * this does not need IRQ protection since if tr_running == 0
3201 * the IRQ's are not running anyway for this port.
3202 */
3203 DFLOW(DEBUG_LOG(info->line, "write ret %i\n", ret));
3204
3205 if (info->xmit.head != info->xmit.tail &&
3206 !tty->stopped &&
3207 !tty->hw_stopped &&
3208 !info->tr_running) {
3209 start_transmit(info);
3210 }
3211
3212 return ret;
3213} /* raw_raw_write() */
3214
3215static int
3216rs_write(struct tty_struct *tty,
3217 const unsigned char *buf, int count)
3218{
3219#if defined(CONFIG_ETRAX_RS485)
3220 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3221
3222 if (info->rs485.flags & SER_RS485_ENABLED)
3223 {
3224 /* If we are in RS-485 mode, we need to toggle RTS and disable
3225 * the receiver before initiating a DMA transfer
3226 */
3227#ifdef CONFIG_ETRAX_FAST_TIMER
3228 /* Abort any started timer */
3229 fast_timers_rs485[info->line].function = NULL;
3230 del_fast_timer(&fast_timers_rs485[info->line]);
3231#endif
3232 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_ON_SEND));
3233#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3234 e100_disable_rx(info);
3235 e100_enable_rx_irq(info);
3236#endif
3237 if ((info->rs485.flags & SER_RS485_RTS_BEFORE_SEND) &&
3238 (info->rs485.delay_rts_before_send > 0))
3239 msleep(info->rs485.delay_rts_before_send);
3240 }
3241#endif /* CONFIG_ETRAX_RS485 */
3242
3243 count = rs_raw_write(tty, buf, count);
3244
3245#if defined(CONFIG_ETRAX_RS485)
3246 if (info->rs485.flags & SER_RS485_ENABLED)
3247 {
3248 unsigned int val;
3249 /* If we are in RS-485 mode the following has to be done:
3250 * wait until DMA is ready
3251 * wait on transmit shift register
3252 * toggle RTS
3253 * enable the receiver
3254 */
3255
3256 /* Sleep until all sent */
3257 tty_wait_until_sent(tty, 0);
3258#ifdef CONFIG_ETRAX_FAST_TIMER
3259 /* Now sleep a little more so that shift register is empty */
3260 schedule_usleep(info->char_time_usec * 2);
3261#endif
3262 /* wait on transmit shift register */
3263 do{
3264 get_lsr_info(info, &val);
3265 }while (!(val & TIOCSER_TEMT));
3266
3267 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
3268
3269#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3270 e100_enable_rx(info);
3271 e100_enable_rxdma_irq(info);
3272#endif
3273 }
3274#endif /* CONFIG_ETRAX_RS485 */
3275
3276 return count;
3277} /* rs_write */
3278
3279
3280/* how much space is available in the xmit buffer? */
3281
3282static int
3283rs_write_room(struct tty_struct *tty)
3284{
3285 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3286
3287 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3288}
3289
3290/* How many chars are in the xmit buffer?
3291 * This does not include any chars in the transmitter FIFO.
3292 * Use wait_until_sent for waiting for FIFO drain.
3293 */
3294
3295static int
3296rs_chars_in_buffer(struct tty_struct *tty)
3297{
3298 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3299
3300 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3301}
3302
3303/* discard everything in the xmit buffer */
3304
3305static void
3306rs_flush_buffer(struct tty_struct *tty)
3307{
3308 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3309 unsigned long flags;
3310
3311 local_irq_save(flags);
3312 info->xmit.head = info->xmit.tail = 0;
3313 local_irq_restore(flags);
3314
3315 tty_wakeup(tty);
3316}
3317
3318/*
3319 * This function is used to send a high-priority XON/XOFF character to
3320 * the device
3321 *
3322 * Since we use DMA we don't check for info->x_char in transmit_chars_dma(),
3323 * but we do it in handle_ser_tx_interrupt().
3324 * We disable DMA channel and enable tx ready interrupt and write the
3325 * character when possible.
3326 */
3327static void rs_send_xchar(struct tty_struct *tty, char ch)
3328{
3329 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3330 unsigned long flags;
3331 local_irq_save(flags);
3332 if (info->uses_dma_out) {
3333 /* Put the DMA on hold and disable the channel */
3334 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, hold);
3335 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) !=
3336 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, hold));
3337 e100_disable_txdma_channel(info);
3338 }
3339
3340 /* Must make sure transmitter is not stopped before we can transmit */
3341 if (tty->stopped)
3342 rs_start(tty);
3343
3344 /* Enable manual transmit interrupt and send from there */
3345 DFLOW(DEBUG_LOG(info->line, "rs_send_xchar 0x%02X\n", ch));
3346 info->x_char = ch;
3347 e100_enable_serial_tx_ready_irq(info);
3348 local_irq_restore(flags);
3349}
3350
3351/*
3352 * ------------------------------------------------------------
3353 * rs_throttle()
3354 *
3355 * This routine is called by the upper-layer tty layer to signal that
3356 * incoming characters should be throttled.
3357 * ------------------------------------------------------------
3358 */
3359static void
3360rs_throttle(struct tty_struct * tty)
3361{
3362 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3363#ifdef SERIAL_DEBUG_THROTTLE
3364 char buf[64];
3365
3366 printk("throttle %s: %lu....\n", tty_name(tty, buf),
3367 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3368#endif
3369 DFLOW(DEBUG_LOG(info->line,"rs_throttle %lu\n", tty->ldisc.chars_in_buffer(tty)));
3370
3371 /* Do RTS before XOFF since XOFF might take some time */
3372 if (tty->termios->c_cflag & CRTSCTS) {
3373 /* Turn off RTS line */
3374 e100_rts(info, 0);
3375 }
3376 if (I_IXOFF(tty))
3377 rs_send_xchar(tty, STOP_CHAR(tty));
3378
3379}
3380
3381static void
3382rs_unthrottle(struct tty_struct * tty)
3383{
3384 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3385#ifdef SERIAL_DEBUG_THROTTLE
3386 char buf[64];
3387
3388 printk("unthrottle %s: %lu....\n", tty_name(tty, buf),
3389 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3390#endif
3391 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle ldisc %d\n", tty->ldisc.chars_in_buffer(tty)));
3392 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle flip.count: %i\n", tty->flip.count));
3393 /* Do RTS before XOFF since XOFF might take some time */
3394 if (tty->termios->c_cflag & CRTSCTS) {
3395 /* Assert RTS line */
3396 e100_rts(info, 1);
3397 }
3398
3399 if (I_IXOFF(tty)) {
3400 if (info->x_char)
3401 info->x_char = 0;
3402 else
3403 rs_send_xchar(tty, START_CHAR(tty));
3404 }
3405
3406}
3407
3408/*
3409 * ------------------------------------------------------------
3410 * rs_ioctl() and friends
3411 * ------------------------------------------------------------
3412 */
3413
3414static int
3415get_serial_info(struct e100_serial * info,
3416 struct serial_struct * retinfo)
3417{
3418 struct serial_struct tmp;
3419
3420 /* this is all probably wrong, there are a lot of fields
3421 * here that we don't have in e100_serial and maybe we
3422 * should set them to something else than 0.
3423 */
3424
3425 if (!retinfo)
3426 return -EFAULT;
3427 memset(&tmp, 0, sizeof(tmp));
3428 tmp.type = info->type;
3429 tmp.line = info->line;
3430 tmp.port = (int)info->ioport;
3431 tmp.irq = info->irq;
3432 tmp.flags = info->flags;
3433 tmp.baud_base = info->baud_base;
3434 tmp.close_delay = info->close_delay;
3435 tmp.closing_wait = info->closing_wait;
3436 tmp.custom_divisor = info->custom_divisor;
3437 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3438 return -EFAULT;
3439 return 0;
3440}
3441
3442static int
3443set_serial_info(struct e100_serial *info,
3444 struct serial_struct *new_info)
3445{
3446 struct serial_struct new_serial;
3447 struct e100_serial old_info;
3448 int retval = 0;
3449
3450 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
3451 return -EFAULT;
3452
3453 old_info = *info;
3454
3455 if (!capable(CAP_SYS_ADMIN)) {
3456 if ((new_serial.type != info->type) ||
3457 (new_serial.close_delay != info->close_delay) ||
3458 ((new_serial.flags & ~ASYNC_USR_MASK) !=
3459 (info->flags & ~ASYNC_USR_MASK)))
3460 return -EPERM;
3461 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
3462 (new_serial.flags & ASYNC_USR_MASK));
3463 goto check_and_exit;
3464 }
3465
3466 if (info->count > 1)
3467 return -EBUSY;
3468
3469 /*
3470 * OK, past this point, all the error checking has been done.
3471 * At this point, we start making changes.....
3472 */
3473
3474 info->baud_base = new_serial.baud_base;
3475 info->flags = ((info->flags & ~ASYNC_FLAGS) |
3476 (new_serial.flags & ASYNC_FLAGS));
3477 info->custom_divisor = new_serial.custom_divisor;
3478 info->type = new_serial.type;
3479 info->close_delay = new_serial.close_delay;
3480 info->closing_wait = new_serial.closing_wait;
3481 info->port.tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
3482
3483 check_and_exit:
3484 if (info->flags & ASYNC_INITIALIZED) {
3485 change_speed(info);
3486 } else
3487 retval = startup(info);
3488 return retval;
3489}
3490
3491/*
3492 * get_lsr_info - get line status register info
3493 *
3494 * Purpose: Let user call ioctl() to get info when the UART physically
3495 * is emptied. On bus types like RS485, the transmitter must
3496 * release the bus after transmitting. This must be done when
3497 * the transmit shift register is empty, not be done when the
3498 * transmit holding register is empty. This functionality
3499 * allows an RS485 driver to be written in user space.
3500 */
3501static int
3502get_lsr_info(struct e100_serial * info, unsigned int *value)
3503{
3504 unsigned int result = TIOCSER_TEMT;
3505#ifndef CONFIG_SVINTO_SIM
3506 unsigned long curr_time = jiffies;
3507 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3508 unsigned long elapsed_usec =
3509 (curr_time - info->last_tx_active) * 1000000/HZ +
3510 curr_time_usec - info->last_tx_active_usec;
3511
3512 if (info->xmit.head != info->xmit.tail ||
3513 elapsed_usec < 2*info->char_time_usec) {
3514 result = 0;
3515 }
3516#endif
3517
3518 if (copy_to_user(value, &result, sizeof(int)))
3519 return -EFAULT;
3520 return 0;
3521}
3522
3523#ifdef SERIAL_DEBUG_IO
3524struct state_str
3525{
3526 int state;
3527 const char *str;
3528};
3529
3530const struct state_str control_state_str[] = {
3531 {TIOCM_DTR, "DTR" },
3532 {TIOCM_RTS, "RTS"},
3533 {TIOCM_ST, "ST?" },
3534 {TIOCM_SR, "SR?" },
3535 {TIOCM_CTS, "CTS" },
3536 {TIOCM_CD, "CD" },
3537 {TIOCM_RI, "RI" },
3538 {TIOCM_DSR, "DSR" },
3539 {0, NULL }
3540};
3541
3542char *get_control_state_str(int MLines, char *s)
3543{
3544 int i = 0;
3545
3546 s[0]='\0';
3547 while (control_state_str[i].str != NULL) {
3548 if (MLines & control_state_str[i].state) {
3549 if (s[0] != '\0') {
3550 strcat(s, ", ");
3551 }
3552 strcat(s, control_state_str[i].str);
3553 }
3554 i++;
3555 }
3556 return s;
3557}
3558#endif
3559
3560static int
3561rs_break(struct tty_struct *tty, int break_state)
3562{
3563 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3564 unsigned long flags;
3565
3566 if (!info->ioport)
3567 return -EIO;
3568
3569 local_irq_save(flags);
3570 if (break_state == -1) {
3571 /* Go to manual mode and set the txd pin to 0 */
3572 /* Clear bit 7 (txd) and 6 (tr_enable) */
3573 info->tx_ctrl &= 0x3F;
3574 } else {
3575 /* Set bit 7 (txd) and 6 (tr_enable) */
3576 info->tx_ctrl |= (0x80 | 0x40);
3577 }
3578 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
3579 local_irq_restore(flags);
3580 return 0;
3581}
3582
3583static int
3584rs_tiocmset(struct tty_struct *tty, struct file *file,
3585 unsigned int set, unsigned int clear)
3586{
3587 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3588 unsigned long flags;
3589
3590 local_irq_save(flags);
3591
3592 if (clear & TIOCM_RTS)
3593 e100_rts(info, 0);
3594 if (clear & TIOCM_DTR)
3595 e100_dtr(info, 0);
3596 /* Handle FEMALE behaviour */
3597 if (clear & TIOCM_RI)
3598 e100_ri_out(info, 0);
3599 if (clear & TIOCM_CD)
3600 e100_cd_out(info, 0);
3601
3602 if (set & TIOCM_RTS)
3603 e100_rts(info, 1);
3604 if (set & TIOCM_DTR)
3605 e100_dtr(info, 1);
3606 /* Handle FEMALE behaviour */
3607 if (set & TIOCM_RI)
3608 e100_ri_out(info, 1);
3609 if (set & TIOCM_CD)
3610 e100_cd_out(info, 1);
3611
3612 local_irq_restore(flags);
3613 return 0;
3614}
3615
3616static int
3617rs_tiocmget(struct tty_struct *tty, struct file *file)
3618{
3619 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3620 unsigned int result;
3621 unsigned long flags;
3622
3623 local_irq_save(flags);
3624
3625 result =
3626 (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
3627 | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
3628 | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
3629 | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
3630 | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
3631 | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
3632
3633 local_irq_restore(flags);
3634
3635#ifdef SERIAL_DEBUG_IO
3636 printk(KERN_DEBUG "ser%i: modem state: %i 0x%08X\n",
3637 info->line, result, result);
3638 {
3639 char s[100];
3640
3641 get_control_state_str(result, s);
3642 printk(KERN_DEBUG "state: %s\n", s);
3643 }
3644#endif
3645 return result;
3646
3647}
3648
3649
3650static int
3651rs_ioctl(struct tty_struct *tty, struct file * file,
3652 unsigned int cmd, unsigned long arg)
3653{
3654 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3655
3656 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
3657 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
3658 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
3659 if (tty->flags & (1 << TTY_IO_ERROR))
3660 return -EIO;
3661 }
3662
3663 switch (cmd) {
3664 case TIOCGSERIAL:
3665 return get_serial_info(info,
3666 (struct serial_struct *) arg);
3667 case TIOCSSERIAL:
3668 return set_serial_info(info,
3669 (struct serial_struct *) arg);
3670 case TIOCSERGETLSR: /* Get line status register */
3671 return get_lsr_info(info, (unsigned int *) arg);
3672
3673 case TIOCSERGSTRUCT:
3674 if (copy_to_user((struct e100_serial *) arg,
3675 info, sizeof(struct e100_serial)))
3676 return -EFAULT;
3677 return 0;
3678
3679#if defined(CONFIG_ETRAX_RS485)
3680 case TIOCSERSETRS485:
3681 {
3682 /* In this ioctl we still use the old structure
3683 * rs485_control for backward compatibility
3684 * (if we use serial_rs485, then old user-level code
3685 * wouldn't work anymore...).
3686 * The use of this ioctl is deprecated: use TIOCSRS485
3687 * instead.*/
3688 struct rs485_control rs485ctrl;
3689 struct serial_rs485 rs485data;
3690 printk(KERN_DEBUG "The use of this ioctl is deprecated. Use TIOCSRS485 instead\n");
3691 if (copy_from_user(&rs485ctrl, (struct rs485_control *)arg,
3692 sizeof(rs485ctrl)))
3693 return -EFAULT;
3694
3695 rs485data.delay_rts_before_send = rs485ctrl.delay_rts_before_send;
3696 rs485data.flags = 0;
3697 if (rs485data.delay_rts_before_send != 0)
3698 rs485data.flags |= SER_RS485_RTS_BEFORE_SEND;
3699 else
3700 rs485data.flags &= ~(SER_RS485_RTS_BEFORE_SEND);
3701
3702 if (rs485ctrl.enabled)
3703 rs485data.flags |= SER_RS485_ENABLED;
3704 else
3705 rs485data.flags &= ~(SER_RS485_ENABLED);
3706
3707 if (rs485ctrl.rts_on_send)
3708 rs485data.flags |= SER_RS485_RTS_ON_SEND;
3709 else
3710 rs485data.flags &= ~(SER_RS485_RTS_ON_SEND);
3711
3712 if (rs485ctrl.rts_after_sent)
3713 rs485data.flags |= SER_RS485_RTS_AFTER_SEND;
3714 else
3715 rs485data.flags &= ~(SER_RS485_RTS_AFTER_SEND);
3716
3717 return e100_enable_rs485(tty, &rs485data);
3718 }
3719
3720 case TIOCSRS485:
3721 {
3722 /* This is the new version of TIOCSRS485, with new
3723 * data structure serial_rs485 */
3724 struct serial_rs485 rs485data;
3725 if (copy_from_user(&rs485data, (struct rs485_control *)arg,
3726 sizeof(rs485data)))
3727 return -EFAULT;
3728
3729 return e100_enable_rs485(tty, &rs485data);
3730 }
3731
3732 case TIOCGRS485:
3733 {
3734 struct serial_rs485 *rs485data =
3735 &(((struct e100_serial *)tty->driver_data)->rs485);
3736 /* This is the ioctl to get RS485 data from user-space */
3737 if (copy_to_user((struct serial_rs485 *) arg,
3738 rs485data,
3739 sizeof(struct serial_rs485)))
3740 return -EFAULT;
3741 break;
3742 }
3743
3744 case TIOCSERWRRS485:
3745 {
3746 struct rs485_write rs485wr;
3747 if (copy_from_user(&rs485wr, (struct rs485_write *)arg,
3748 sizeof(rs485wr)))
3749 return -EFAULT;
3750
3751 return e100_write_rs485(tty, rs485wr.outc, rs485wr.outc_size);
3752 }
3753#endif
3754
3755 default:
3756 return -ENOIOCTLCMD;
3757 }
3758 return 0;
3759}
3760
3761static void
3762rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
3763{
3764 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3765
3766 change_speed(info);
3767
3768 /* Handle turning off CRTSCTS */
3769 if ((old_termios->c_cflag & CRTSCTS) &&
3770 !(tty->termios->c_cflag & CRTSCTS)) {
3771 tty->hw_stopped = 0;
3772 rs_start(tty);
3773 }
3774
3775}
3776
3777/*
3778 * ------------------------------------------------------------
3779 * rs_close()
3780 *
3781 * This routine is called when the serial port gets closed. First, we
3782 * wait for the last remaining data to be sent. Then, we unlink its
3783 * S structure from the interrupt chain if necessary, and we free
3784 * that IRQ if nothing is left in the chain.
3785 * ------------------------------------------------------------
3786 */
3787static void
3788rs_close(struct tty_struct *tty, struct file * filp)
3789{
3790 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3791 unsigned long flags;
3792
3793 if (!info)
3794 return;
3795
3796 /* interrupts are disabled for this entire function */
3797
3798 local_irq_save(flags);
3799
3800 if (tty_hung_up_p(filp)) {
3801 local_irq_restore(flags);
3802 return;
3803 }
3804
3805#ifdef SERIAL_DEBUG_OPEN
3806 printk("[%d] rs_close ttyS%d, count = %d\n", current->pid,
3807 info->line, info->count);
3808#endif
3809 if ((tty->count == 1) && (info->count != 1)) {
3810 /*
3811 * Uh, oh. tty->count is 1, which means that the tty
3812 * structure will be freed. Info->count should always
3813 * be one in these conditions. If it's greater than
3814 * one, we've got real problems, since it means the
3815 * serial port won't be shutdown.
3816 */
3817 printk(KERN_CRIT
3818 "rs_close: bad serial port count; tty->count is 1, "
3819 "info->count is %d\n", info->count);
3820 info->count = 1;
3821 }
3822 if (--info->count < 0) {
3823 printk(KERN_CRIT "rs_close: bad serial port count for ttyS%d: %d\n",
3824 info->line, info->count);
3825 info->count = 0;
3826 }
3827 if (info->count) {
3828 local_irq_restore(flags);
3829 return;
3830 }
3831 info->flags |= ASYNC_CLOSING;
3832 /*
3833 * Save the termios structure, since this port may have
3834 * separate termios for callout and dialin.
3835 */
3836 if (info->flags & ASYNC_NORMAL_ACTIVE)
3837 info->normal_termios = *tty->termios;
3838 /*
3839 * Now we wait for the transmit buffer to clear; and we notify
3840 * the line discipline to only process XON/XOFF characters.
3841 */
3842 tty->closing = 1;
3843 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
3844 tty_wait_until_sent(tty, info->closing_wait);
3845 /*
3846 * At this point we stop accepting input. To do this, we
3847 * disable the serial receiver and the DMA receive interrupt.
3848 */
3849#ifdef SERIAL_HANDLE_EARLY_ERRORS
3850 e100_disable_serial_data_irq(info);
3851#endif
3852
3853#ifndef CONFIG_SVINTO_SIM
3854 e100_disable_rx(info);
3855 e100_disable_rx_irq(info);
3856
3857 if (info->flags & ASYNC_INITIALIZED) {
3858 /*
3859 * Before we drop DTR, make sure the UART transmitter
3860 * has completely drained; this is especially
3861 * important as we have a transmit FIFO!
3862 */
3863 rs_wait_until_sent(tty, HZ);
3864 }
3865#endif
3866
3867 shutdown(info);
3868 rs_flush_buffer(tty);
3869 tty_ldisc_flush(tty);
3870 tty->closing = 0;
3871 info->event = 0;
3872 info->port.tty = NULL;
3873 if (info->blocked_open) {
3874 if (info->close_delay)
3875 schedule_timeout_interruptible(info->close_delay);
3876 wake_up_interruptible(&info->open_wait);
3877 }
3878 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
3879 wake_up_interruptible(&info->close_wait);
3880 local_irq_restore(flags);
3881
3882 /* port closed */
3883
3884#if defined(CONFIG_ETRAX_RS485)
3885 if (info->rs485.flags & SER_RS485_ENABLED) {
3886 info->rs485.flags &= ~(SER_RS485_ENABLED);
3887#if defined(CONFIG_ETRAX_RS485_ON_PA)
3888 *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
3889#endif
3890#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
3891 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3892 rs485_port_g_bit, 0);
3893#endif
3894#if defined(CONFIG_ETRAX_RS485_LTC1387)
3895 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3896 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT, 0);
3897 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3898 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT, 0);
3899#endif
3900 }
3901#endif
3902
3903 /*
3904 * Release any allocated DMA irq's.
3905 */
3906 if (info->dma_in_enabled) {
3907 free_irq(info->dma_in_irq_nbr, info);
3908 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
3909 info->uses_dma_in = 0;
3910#ifdef SERIAL_DEBUG_OPEN
3911 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3912 info->dma_in_irq_description);
3913#endif
3914 }
3915 if (info->dma_out_enabled) {
3916 free_irq(info->dma_out_irq_nbr, info);
3917 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
3918 info->uses_dma_out = 0;
3919#ifdef SERIAL_DEBUG_OPEN
3920 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3921 info->dma_out_irq_description);
3922#endif
3923 }
3924}
3925
3926/*
3927 * rs_wait_until_sent() --- wait until the transmitter is empty
3928 */
3929static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
3930{
3931 unsigned long orig_jiffies;
3932 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3933 unsigned long curr_time = jiffies;
3934 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3935 long elapsed_usec =
3936 (curr_time - info->last_tx_active) * (1000000/HZ) +
3937 curr_time_usec - info->last_tx_active_usec;
3938
3939 /*
3940 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
3941 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
3942 */
3943 orig_jiffies = jiffies;
3944 while (info->xmit.head != info->xmit.tail || /* More in send queue */
3945 (*info->ostatusadr & 0x007f) || /* more in FIFO */
3946 (elapsed_usec < 2*info->char_time_usec)) {
3947 schedule_timeout_interruptible(1);
3948 if (signal_pending(current))
3949 break;
3950 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3951 break;
3952 curr_time = jiffies;
3953 curr_time_usec = GET_JIFFIES_USEC();
3954 elapsed_usec =
3955 (curr_time - info->last_tx_active) * (1000000/HZ) +
3956 curr_time_usec - info->last_tx_active_usec;
3957 }
3958 set_current_state(TASK_RUNNING);
3959}
3960
3961/*
3962 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
3963 */
3964void
3965rs_hangup(struct tty_struct *tty)
3966{
3967 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3968
3969 rs_flush_buffer(tty);
3970 shutdown(info);
3971 info->event = 0;
3972 info->count = 0;
3973 info->flags &= ~ASYNC_NORMAL_ACTIVE;
3974 info->port.tty = NULL;
3975 wake_up_interruptible(&info->open_wait);
3976}
3977
3978/*
3979 * ------------------------------------------------------------
3980 * rs_open() and friends
3981 * ------------------------------------------------------------
3982 */
3983static int
3984block_til_ready(struct tty_struct *tty, struct file * filp,
3985 struct e100_serial *info)
3986{
3987 DECLARE_WAITQUEUE(wait, current);
3988 unsigned long flags;
3989 int retval;
3990 int do_clocal = 0, extra_count = 0;
3991
3992 /*
3993 * If the device is in the middle of being closed, then block
3994 * until it's done, and then try again.
3995 */
3996 if (tty_hung_up_p(filp) ||
3997 (info->flags & ASYNC_CLOSING)) {
3998 wait_event_interruptible_tty(info->close_wait,
3999 !(info->flags & ASYNC_CLOSING));
4000#ifdef SERIAL_DO_RESTART
4001 if (info->flags & ASYNC_HUP_NOTIFY)
4002 return -EAGAIN;
4003 else
4004 return -ERESTARTSYS;
4005#else
4006 return -EAGAIN;
4007#endif
4008 }
4009
4010 /*
4011 * If non-blocking mode is set, or the port is not enabled,
4012 * then make the check up front and then exit.
4013 */
4014 if ((filp->f_flags & O_NONBLOCK) ||
4015 (tty->flags & (1 << TTY_IO_ERROR))) {
4016 info->flags |= ASYNC_NORMAL_ACTIVE;
4017 return 0;
4018 }
4019
4020 if (tty->termios->c_cflag & CLOCAL) {
4021 do_clocal = 1;
4022 }
4023
4024 /*
4025 * Block waiting for the carrier detect and the line to become
4026 * free (i.e., not in use by the callout). While we are in
4027 * this loop, info->count is dropped by one, so that
4028 * rs_close() knows when to free things. We restore it upon
4029 * exit, either normal or abnormal.
4030 */
4031 retval = 0;
4032 add_wait_queue(&info->open_wait, &wait);
4033#ifdef SERIAL_DEBUG_OPEN
4034 printk("block_til_ready before block: ttyS%d, count = %d\n",
4035 info->line, info->count);
4036#endif
4037 local_irq_save(flags);
4038 if (!tty_hung_up_p(filp)) {
4039 extra_count++;
4040 info->count--;
4041 }
4042 local_irq_restore(flags);
4043 info->blocked_open++;
4044 while (1) {
4045 local_irq_save(flags);
4046 /* assert RTS and DTR */
4047 e100_rts(info, 1);
4048 e100_dtr(info, 1);
4049 local_irq_restore(flags);
4050 set_current_state(TASK_INTERRUPTIBLE);
4051 if (tty_hung_up_p(filp) ||
4052 !(info->flags & ASYNC_INITIALIZED)) {
4053#ifdef SERIAL_DO_RESTART
4054 if (info->flags & ASYNC_HUP_NOTIFY)
4055 retval = -EAGAIN;
4056 else
4057 retval = -ERESTARTSYS;
4058#else
4059 retval = -EAGAIN;
4060#endif
4061 break;
4062 }
4063 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
4064 /* && (do_clocal || DCD_IS_ASSERTED) */
4065 break;
4066 if (signal_pending(current)) {
4067 retval = -ERESTARTSYS;
4068 break;
4069 }
4070#ifdef SERIAL_DEBUG_OPEN
4071 printk("block_til_ready blocking: ttyS%d, count = %d\n",
4072 info->line, info->count);
4073#endif
4074 tty_unlock();
4075 schedule();
4076 tty_lock();
4077 }
4078 set_current_state(TASK_RUNNING);
4079 remove_wait_queue(&info->open_wait, &wait);
4080 if (extra_count)
4081 info->count++;
4082 info->blocked_open--;
4083#ifdef SERIAL_DEBUG_OPEN
4084 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
4085 info->line, info->count);
4086#endif
4087 if (retval)
4088 return retval;
4089 info->flags |= ASYNC_NORMAL_ACTIVE;
4090 return 0;
4091}
4092
4093static void
4094deinit_port(struct e100_serial *info)
4095{
4096 if (info->dma_out_enabled) {
4097 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
4098 free_irq(info->dma_out_irq_nbr, info);
4099 }
4100 if (info->dma_in_enabled) {
4101 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
4102 free_irq(info->dma_in_irq_nbr, info);
4103 }
4104}
4105
4106/*
4107 * This routine is called whenever a serial port is opened.
4108 * It performs the serial-specific initialization for the tty structure.
4109 */
4110static int
4111rs_open(struct tty_struct *tty, struct file * filp)
4112{
4113 struct e100_serial *info;
4114 int retval, line;
4115 unsigned long page;
4116 int allocated_resources = 0;
4117
4118 /* find which port we want to open */
4119 line = tty->index;
4120
4121 if (line < 0 || line >= NR_PORTS)
4122 return -ENODEV;
4123
4124 /* find the corresponding e100_serial struct in the table */
4125 info = rs_table + line;
4126
4127 /* don't allow the opening of ports that are not enabled in the HW config */
4128 if (!info->enabled)
4129 return -ENODEV;
4130
4131#ifdef SERIAL_DEBUG_OPEN
4132 printk("[%d] rs_open %s, count = %d\n", current->pid, tty->name,
4133 info->count);
4134#endif
4135
4136 info->count++;
4137 tty->driver_data = info;
4138 info->port.tty = tty;
4139
4140 info->port.tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
4141
4142 if (!tmp_buf) {
4143 page = get_zeroed_page(GFP_KERNEL);
4144 if (!page) {
4145 return -ENOMEM;
4146 }
4147 if (tmp_buf)
4148 free_page(page);
4149 else
4150 tmp_buf = (unsigned char *) page;
4151 }
4152
4153 /*
4154 * If the port is in the middle of closing, bail out now
4155 */
4156 if (tty_hung_up_p(filp) ||
4157 (info->flags & ASYNC_CLOSING)) {
4158 wait_event_interruptible_tty(info->close_wait,
4159 !(info->flags & ASYNC_CLOSING));
4160#ifdef SERIAL_DO_RESTART
4161 return ((info->flags & ASYNC_HUP_NOTIFY) ?
4162 -EAGAIN : -ERESTARTSYS);
4163#else
4164 return -EAGAIN;
4165#endif
4166 }
4167
4168 /*
4169 * If DMA is enabled try to allocate the irq's.
4170 */
4171 if (info->count == 1) {
4172 allocated_resources = 1;
4173 if (info->dma_in_enabled) {
4174 if (request_irq(info->dma_in_irq_nbr,
4175 rec_interrupt,
4176 info->dma_in_irq_flags,
4177 info->dma_in_irq_description,
4178 info)) {
4179 printk(KERN_WARNING "DMA irq '%s' busy; "
4180 "falling back to non-DMA mode\n",
4181 info->dma_in_irq_description);
4182 /* Make sure we never try to use DMA in */
4183 /* for the port again. */
4184 info->dma_in_enabled = 0;
4185 } else if (cris_request_dma(info->dma_in_nbr,
4186 info->dma_in_irq_description,
4187 DMA_VERBOSE_ON_ERROR,
4188 info->dma_owner)) {
4189 free_irq(info->dma_in_irq_nbr, info);
4190 printk(KERN_WARNING "DMA '%s' busy; "
4191 "falling back to non-DMA mode\n",
4192 info->dma_in_irq_description);
4193 /* Make sure we never try to use DMA in */
4194 /* for the port again. */
4195 info->dma_in_enabled = 0;
4196 }
4197#ifdef SERIAL_DEBUG_OPEN
4198 else
4199 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
4200 info->dma_in_irq_description);
4201#endif
4202 }
4203 if (info->dma_out_enabled) {
4204 if (request_irq(info->dma_out_irq_nbr,
4205 tr_interrupt,
4206 info->dma_out_irq_flags,
4207 info->dma_out_irq_description,
4208 info)) {
4209 printk(KERN_WARNING "DMA irq '%s' busy; "
4210 "falling back to non-DMA mode\n",
4211 info->dma_out_irq_description);
4212 /* Make sure we never try to use DMA out */
4213 /* for the port again. */
4214 info->dma_out_enabled = 0;
4215 } else if (cris_request_dma(info->dma_out_nbr,
4216 info->dma_out_irq_description,
4217 DMA_VERBOSE_ON_ERROR,
4218 info->dma_owner)) {
4219 free_irq(info->dma_out_irq_nbr, info);
4220 printk(KERN_WARNING "DMA '%s' busy; "
4221 "falling back to non-DMA mode\n",
4222 info->dma_out_irq_description);
4223 /* Make sure we never try to use DMA out */
4224 /* for the port again. */
4225 info->dma_out_enabled = 0;
4226 }
4227#ifdef SERIAL_DEBUG_OPEN
4228 else
4229 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
4230 info->dma_out_irq_description);
4231#endif
4232 }
4233 }
4234
4235 /*
4236 * Start up the serial port
4237 */
4238
4239 retval = startup(info);
4240 if (retval) {
4241 if (allocated_resources)
4242 deinit_port(info);
4243
4244 /* FIXME Decrease count info->count here too? */
4245 return retval;
4246 }
4247
4248
4249 retval = block_til_ready(tty, filp, info);
4250 if (retval) {
4251#ifdef SERIAL_DEBUG_OPEN
4252 printk("rs_open returning after block_til_ready with %d\n",
4253 retval);
4254#endif
4255 if (allocated_resources)
4256 deinit_port(info);
4257
4258 return retval;
4259 }
4260
4261 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
4262 *tty->termios = info->normal_termios;
4263 change_speed(info);
4264 }
4265
4266#ifdef SERIAL_DEBUG_OPEN
4267 printk("rs_open ttyS%d successful...\n", info->line);
4268#endif
4269 DLOG_INT_TRIG( log_int_pos = 0);
4270
4271 DFLIP( if (info->line == SERIAL_DEBUG_LINE) {
4272 info->icount.rx = 0;
4273 } );
4274
4275 return 0;
4276}
4277
4278#ifdef CONFIG_PROC_FS
4279/*
4280 * /proc fs routines....
4281 */
4282
4283static void seq_line_info(struct seq_file *m, struct e100_serial *info)
4284{
4285 unsigned long tmp;
4286
4287 seq_printf(m, "%d: uart:E100 port:%lX irq:%d",
4288 info->line, (unsigned long)info->ioport, info->irq);
4289
4290 if (!info->ioport || (info->type == PORT_UNKNOWN)) {
4291 seq_printf(m, "\n");
4292 return;
4293 }
4294
4295 seq_printf(m, " baud:%d", info->baud);
4296 seq_printf(m, " tx:%lu rx:%lu",
4297 (unsigned long)info->icount.tx,
4298 (unsigned long)info->icount.rx);
4299 tmp = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
4300 if (tmp)
4301 seq_printf(m, " tx_pend:%lu/%lu",
4302 (unsigned long)tmp,
4303 (unsigned long)SERIAL_XMIT_SIZE);
4304
4305 seq_printf(m, " rx_pend:%lu/%lu",
4306 (unsigned long)info->recv_cnt,
4307 (unsigned long)info->max_recv_cnt);
4308
4309#if 1
4310 if (info->port.tty) {
4311 if (info->port.tty->stopped)
4312 seq_printf(m, " stopped:%i",
4313 (int)info->port.tty->stopped);
4314 if (info->port.tty->hw_stopped)
4315 seq_printf(m, " hw_stopped:%i",
4316 (int)info->port.tty->hw_stopped);
4317 }
4318
4319 {
4320 unsigned char rstat = info->ioport[REG_STATUS];
4321 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect))
4322 seq_printf(m, " xoff_detect:1");
4323 }
4324
4325#endif
4326
4327 if (info->icount.frame)
4328 seq_printf(m, " fe:%lu", (unsigned long)info->icount.frame);
4329
4330 if (info->icount.parity)
4331 seq_printf(m, " pe:%lu", (unsigned long)info->icount.parity);
4332
4333 if (info->icount.brk)
4334 seq_printf(m, " brk:%lu", (unsigned long)info->icount.brk);
4335
4336 if (info->icount.overrun)
4337 seq_printf(m, " oe:%lu", (unsigned long)info->icount.overrun);
4338
4339 /*
4340 * Last thing is the RS-232 status lines
4341 */
4342 if (!E100_RTS_GET(info))
4343 seq_puts(m, "|RTS");
4344 if (!E100_CTS_GET(info))
4345 seq_puts(m, "|CTS");
4346 if (!E100_DTR_GET(info))
4347 seq_puts(m, "|DTR");
4348 if (!E100_DSR_GET(info))
4349 seq_puts(m, "|DSR");
4350 if (!E100_CD_GET(info))
4351 seq_puts(m, "|CD");
4352 if (!E100_RI_GET(info))
4353 seq_puts(m, "|RI");
4354 seq_puts(m, "\n");
4355}
4356
4357
4358static int crisv10_proc_show(struct seq_file *m, void *v)
4359{
4360 int i;
4361
4362 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
4363
4364 for (i = 0; i < NR_PORTS; i++) {
4365 if (!rs_table[i].enabled)
4366 continue;
4367 seq_line_info(m, &rs_table[i]);
4368 }
4369#ifdef DEBUG_LOG_INCLUDED
4370 for (i = 0; i < debug_log_pos; i++) {
4371 seq_printf(m, "%-4i %lu.%lu ",
4372 i, debug_log[i].time,
4373 timer_data_to_ns(debug_log[i].timer_data));
4374 seq_printf(m, debug_log[i].string, debug_log[i].value);
4375 }
4376 seq_printf(m, "debug_log %i/%i\n", i, DEBUG_LOG_SIZE);
4377 debug_log_pos = 0;
4378#endif
4379 return 0;
4380}
4381
4382static int crisv10_proc_open(struct inode *inode, struct file *file)
4383{
4384 return single_open(file, crisv10_proc_show, NULL);
4385}
4386
4387static const struct file_operations crisv10_proc_fops = {
4388 .owner = THIS_MODULE,
4389 .open = crisv10_proc_open,
4390 .read = seq_read,
4391 .llseek = seq_lseek,
4392 .release = single_release,
4393};
4394#endif
4395
4396
4397/* Finally, routines used to initialize the serial driver. */
4398
4399static void show_serial_version(void)
4400{
4401 printk(KERN_INFO
4402 "ETRAX 100LX serial-driver %s, "
4403 "(c) 2000-2004 Axis Communications AB\r\n",
4404 &serial_version[11]); /* "$Revision: x.yy" */
4405}
4406
4407/* rs_init inits the driver at boot (using the module_init chain) */
4408
4409static const struct tty_operations rs_ops = {
4410 .open = rs_open,
4411 .close = rs_close,
4412 .write = rs_write,
4413 .flush_chars = rs_flush_chars,
4414 .write_room = rs_write_room,
4415 .chars_in_buffer = rs_chars_in_buffer,
4416 .flush_buffer = rs_flush_buffer,
4417 .ioctl = rs_ioctl,
4418 .throttle = rs_throttle,
4419 .unthrottle = rs_unthrottle,
4420 .set_termios = rs_set_termios,
4421 .stop = rs_stop,
4422 .start = rs_start,
4423 .hangup = rs_hangup,
4424 .break_ctl = rs_break,
4425 .send_xchar = rs_send_xchar,
4426 .wait_until_sent = rs_wait_until_sent,
4427 .tiocmget = rs_tiocmget,
4428 .tiocmset = rs_tiocmset,
4429#ifdef CONFIG_PROC_FS
4430 .proc_fops = &crisv10_proc_fops,
4431#endif
4432};
4433
4434static int __init rs_init(void)
4435{
4436 int i;
4437 struct e100_serial *info;
4438 struct tty_driver *driver = alloc_tty_driver(NR_PORTS);
4439
4440 if (!driver)
4441 return -ENOMEM;
4442
4443 show_serial_version();
4444
4445 /* Setup the timed flush handler system */
4446
4447#if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
4448 setup_timer(&flush_timer, timed_flush_handler, 0);
4449 mod_timer(&flush_timer, jiffies + 5);
4450#endif
4451
4452#if defined(CONFIG_ETRAX_RS485)
4453#if defined(CONFIG_ETRAX_RS485_ON_PA)
4454 if (cris_io_interface_allocate_pins(if_ser0, 'a', rs485_pa_bit,
4455 rs485_pa_bit)) {
4456 printk(KERN_CRIT "ETRAX100LX serial: Could not allocate "
4457 "RS485 pin\n");
4458 put_tty_driver(driver);
4459 return -EBUSY;
4460 }
4461#endif
4462#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
4463 if (cris_io_interface_allocate_pins(if_ser0, 'g', rs485_pa_bit,
4464 rs485_port_g_bit)) {
4465 printk(KERN_CRIT "ETRAX100LX serial: Could not allocate "
4466 "RS485 pin\n");
4467 put_tty_driver(driver);
4468 return -EBUSY;
4469 }
4470#endif
4471#endif
4472
4473 /* Initialize the tty_driver structure */
4474
4475 driver->driver_name = "serial";
4476 driver->name = "ttyS";
4477 driver->major = TTY_MAJOR;
4478 driver->minor_start = 64;
4479 driver->type = TTY_DRIVER_TYPE_SERIAL;
4480 driver->subtype = SERIAL_TYPE_NORMAL;
4481 driver->init_termios = tty_std_termios;
4482 driver->init_termios.c_cflag =
4483 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
4484 driver->init_termios.c_ispeed = 115200;
4485 driver->init_termios.c_ospeed = 115200;
4486 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4487
4488 tty_set_operations(driver, &rs_ops);
4489 serial_driver = driver;
4490 if (tty_register_driver(driver))
4491 panic("Couldn't register serial driver\n");
4492 /* do some initializing for the separate ports */
4493
4494 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
4495 if (info->enabled) {
4496 if (cris_request_io_interface(info->io_if,
4497 info->io_if_description)) {
4498 printk(KERN_CRIT "ETRAX100LX async serial: "
4499 "Could not allocate IO pins for "
4500 "%s, port %d\n",
4501 info->io_if_description, i);
4502 info->enabled = 0;
4503 }
4504 }
4505 info->uses_dma_in = 0;
4506 info->uses_dma_out = 0;
4507 info->line = i;
4508 info->port.tty = NULL;
4509 info->type = PORT_ETRAX;
4510 info->tr_running = 0;
4511 info->forced_eop = 0;
4512 info->baud_base = DEF_BAUD_BASE;
4513 info->custom_divisor = 0;
4514 info->flags = 0;
4515 info->close_delay = 5*HZ/10;
4516 info->closing_wait = 30*HZ;
4517 info->x_char = 0;
4518 info->event = 0;
4519 info->count = 0;
4520 info->blocked_open = 0;
4521 info->normal_termios = driver->init_termios;
4522 init_waitqueue_head(&info->open_wait);
4523 init_waitqueue_head(&info->close_wait);
4524 info->xmit.buf = NULL;
4525 info->xmit.tail = info->xmit.head = 0;
4526 info->first_recv_buffer = info->last_recv_buffer = NULL;
4527 info->recv_cnt = info->max_recv_cnt = 0;
4528 info->last_tx_active_usec = 0;
4529 info->last_tx_active = 0;
4530
4531#if defined(CONFIG_ETRAX_RS485)
4532 /* Set sane defaults */
4533 info->rs485.flags &= ~(SER_RS485_RTS_ON_SEND);
4534 info->rs485.flags |= SER_RS485_RTS_AFTER_SEND;
4535 info->rs485.flags &= ~(SER_RS485_RTS_BEFORE_SEND);
4536 info->rs485.delay_rts_before_send = 0;
4537 info->rs485.flags &= ~(SER_RS485_ENABLED);
4538#endif
4539 INIT_WORK(&info->work, do_softint);
4540
4541 if (info->enabled) {
4542 printk(KERN_INFO "%s%d at %p is a builtin UART with DMA\n",
4543 serial_driver->name, info->line, info->ioport);
4544 }
4545 }
4546#ifdef CONFIG_ETRAX_FAST_TIMER
4547#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
4548 memset(fast_timers, 0, sizeof(fast_timers));
4549#endif
4550#ifdef CONFIG_ETRAX_RS485
4551 memset(fast_timers_rs485, 0, sizeof(fast_timers_rs485));
4552#endif
4553 fast_timer_init();
4554#endif
4555
4556#ifndef CONFIG_SVINTO_SIM
4557#ifndef CONFIG_ETRAX_KGDB
4558 /* Not needed in simulator. May only complicate stuff. */
4559 /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
4560
4561 if (request_irq(SERIAL_IRQ_NBR, ser_interrupt,
4562 IRQF_SHARED | IRQF_DISABLED, "serial ", driver))
4563 panic("%s: Failed to request irq8", __func__);
4564
4565#endif
4566#endif /* CONFIG_SVINTO_SIM */
4567
4568 return 0;
4569}
4570
4571/* this makes sure that rs_init is called during kernel boot */
4572
4573module_init(rs_init);
diff --git a/drivers/tty/serial/crisv10.h b/drivers/tty/serial/crisv10.h
new file mode 100644
index 000000000000..ea0beb46a10d
--- /dev/null
+++ b/drivers/tty/serial/crisv10.h
@@ -0,0 +1,147 @@
1/*
2 * serial.h: Arch-dep definitions for the Etrax100 serial driver.
3 *
4 * Copyright (C) 1998-2007 Axis Communications AB
5 */
6
7#ifndef _ETRAX_SERIAL_H
8#define _ETRAX_SERIAL_H
9
10#include <linux/circ_buf.h>
11#include <asm/termios.h>
12#include <asm/dma.h>
13#include <arch/io_interface_mux.h>
14
15/* Software state per channel */
16
17#ifdef __KERNEL__
18/*
19 * This is our internal structure for each serial port's state.
20 *
21 * Many fields are paralleled by the structure used by the serial_struct
22 * structure.
23 *
24 * For definitions of the flags field, see tty.h
25 */
26
27#define SERIAL_RECV_DESCRIPTORS 8
28
29struct etrax_recv_buffer {
30 struct etrax_recv_buffer *next;
31 unsigned short length;
32 unsigned char error;
33 unsigned char pad;
34
35 unsigned char buffer[0];
36};
37
38struct e100_serial {
39 struct tty_port port;
40 int baud;
41 volatile u8 *ioport; /* R_SERIALx_CTRL */
42 u32 irq; /* bitnr in R_IRQ_MASK2 for dmaX_descr */
43
44 /* Output registers */
45 volatile u8 *oclrintradr; /* adr to R_DMA_CHx_CLR_INTR */
46 volatile u32 *ofirstadr; /* adr to R_DMA_CHx_FIRST */
47 volatile u8 *ocmdadr; /* adr to R_DMA_CHx_CMD */
48 const volatile u8 *ostatusadr; /* adr to R_DMA_CHx_STATUS */
49
50 /* Input registers */
51 volatile u8 *iclrintradr; /* adr to R_DMA_CHx_CLR_INTR */
52 volatile u32 *ifirstadr; /* adr to R_DMA_CHx_FIRST */
53 volatile u8 *icmdadr; /* adr to R_DMA_CHx_CMD */
54 volatile u32 *idescradr; /* adr to R_DMA_CHx_DESCR */
55
56 int flags; /* defined in tty.h */
57
58 u8 rx_ctrl; /* shadow for R_SERIALx_REC_CTRL */
59 u8 tx_ctrl; /* shadow for R_SERIALx_TR_CTRL */
60 u8 iseteop; /* bit number for R_SET_EOP for the input dma */
61 int enabled; /* Set to 1 if the port is enabled in HW config */
62
63 u8 dma_out_enabled; /* Set to 1 if DMA should be used */
64 u8 dma_in_enabled; /* Set to 1 if DMA should be used */
65
66 /* end of fields defined in rs_table[] in .c-file */
67 int dma_owner;
68 unsigned int dma_in_nbr;
69 unsigned int dma_out_nbr;
70 unsigned int dma_in_irq_nbr;
71 unsigned int dma_out_irq_nbr;
72 unsigned long dma_in_irq_flags;
73 unsigned long dma_out_irq_flags;
74 char *dma_in_irq_description;
75 char *dma_out_irq_description;
76
77 enum cris_io_interface io_if;
78 char *io_if_description;
79
80 u8 uses_dma_in; /* Set to 1 if DMA is used */
81 u8 uses_dma_out; /* Set to 1 if DMA is used */
82 u8 forced_eop; /* a fifo eop has been forced */
83 int baud_base; /* For special baudrates */
84 int custom_divisor; /* For special baudrates */
85 struct etrax_dma_descr tr_descr;
86 struct etrax_dma_descr rec_descr[SERIAL_RECV_DESCRIPTORS];
87 int cur_rec_descr;
88
89 volatile int tr_running; /* 1 if output is running */
90
91 struct tty_struct *tty;
92 int read_status_mask;
93 int ignore_status_mask;
94 int x_char; /* xon/xoff character */
95 int close_delay;
96 unsigned short closing_wait;
97 unsigned short closing_wait2;
98 unsigned long event;
99 unsigned long last_active;
100 int line;
101 int type; /* PORT_ETRAX */
102 int count; /* # of fd on device */
103 int blocked_open; /* # of blocked opens */
104 struct circ_buf xmit;
105 struct etrax_recv_buffer *first_recv_buffer;
106 struct etrax_recv_buffer *last_recv_buffer;
107 unsigned int recv_cnt;
108 unsigned int max_recv_cnt;
109
110 struct work_struct work;
111 struct async_icount icount; /* error-statistics etc.*/
112 struct ktermios normal_termios;
113 struct ktermios callout_termios;
114 wait_queue_head_t open_wait;
115 wait_queue_head_t close_wait;
116
117 unsigned long char_time_usec; /* The time for 1 char, in usecs */
118 unsigned long flush_time_usec; /* How often we should flush */
119 unsigned long last_tx_active_usec; /* Last tx usec in the jiffies */
120 unsigned long last_tx_active; /* Last tx time in jiffies */
121 unsigned long last_rx_active_usec; /* Last rx usec in the jiffies */
122 unsigned long last_rx_active; /* Last rx time in jiffies */
123
124 int break_detected_cnt;
125 int errorcode;
126
127#ifdef CONFIG_ETRAX_RS485
128 struct serial_rs485 rs485; /* RS-485 support */
129#endif
130};
131
132/* this PORT is not in the standard serial.h. it's not actually used for
133 * anything since we only have one type of async serial-port anyway in this
134 * system.
135 */
136
137#define PORT_ETRAX 1
138
139/*
140 * Events are used to schedule things to happen at timer-interrupt
141 * time, instead of at rs interrupt time.
142 */
143#define RS_EVENT_WRITE_WAKEUP 0
144
145#endif /* __KERNEL__ */
146
147#endif /* !_ETRAX_SERIAL_H */
diff --git a/drivers/tty/serial/dz.c b/drivers/tty/serial/dz.c
new file mode 100644
index 000000000000..57421d776329
--- /dev/null
+++ b/drivers/tty/serial/dz.c
@@ -0,0 +1,955 @@
1/*
2 * dz.c: Serial port driver for DECstations equipped
3 * with the DZ chipset.
4 *
5 * Copyright (C) 1998 Olivier A. D. Lebaillif
6 *
7 * Email: olivier.lebaillif@ifrsys.com
8 *
9 * Copyright (C) 2004, 2006, 2007 Maciej W. Rozycki
10 *
11 * [31-AUG-98] triemer
12 * Changed IRQ to use Harald's dec internals interrupts.h
13 * removed base_addr code - moving address assignment to setup.c
14 * Changed name of dz_init to rs_init to be consistent with tc code
15 * [13-NOV-98] triemer fixed code to receive characters
16 * after patches by harald to irq code.
17 * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
18 * field from "current" - somewhere between 2.1.121 and 2.1.131
19 Qua Jun 27 15:02:26 BRT 2001
20 * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
21 *
22 * Parts (C) 1999 David Airlie, airlied@linux.ie
23 * [07-SEP-99] Bugfixes
24 *
25 * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
26 * Converted to new serial core
27 */
28
29#undef DEBUG_DZ
30
31#if defined(CONFIG_SERIAL_DZ_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
32#define SUPPORT_SYSRQ
33#endif
34
35#include <linux/bitops.h>
36#include <linux/compiler.h>
37#include <linux/console.h>
38#include <linux/delay.h>
39#include <linux/errno.h>
40#include <linux/init.h>
41#include <linux/interrupt.h>
42#include <linux/ioport.h>
43#include <linux/kernel.h>
44#include <linux/major.h>
45#include <linux/module.h>
46#include <linux/serial.h>
47#include <linux/serial_core.h>
48#include <linux/sysrq.h>
49#include <linux/tty.h>
50
51#include <asm/atomic.h>
52#include <asm/bootinfo.h>
53#include <asm/io.h>
54#include <asm/system.h>
55
56#include <asm/dec/interrupts.h>
57#include <asm/dec/kn01.h>
58#include <asm/dec/kn02.h>
59#include <asm/dec/machtype.h>
60#include <asm/dec/prom.h>
61#include <asm/dec/system.h>
62
63#include "dz.h"
64
65
66MODULE_DESCRIPTION("DECstation DZ serial driver");
67MODULE_LICENSE("GPL");
68
69
70static char dz_name[] __initdata = "DECstation DZ serial driver version ";
71static char dz_version[] __initdata = "1.04";
72
73struct dz_port {
74 struct dz_mux *mux;
75 struct uart_port port;
76 unsigned int cflag;
77};
78
79struct dz_mux {
80 struct dz_port dport[DZ_NB_PORT];
81 atomic_t map_guard;
82 atomic_t irq_guard;
83 int initialised;
84};
85
86static struct dz_mux dz_mux;
87
88static inline struct dz_port *to_dport(struct uart_port *uport)
89{
90 return container_of(uport, struct dz_port, port);
91}
92
93/*
94 * ------------------------------------------------------------
95 * dz_in () and dz_out ()
96 *
97 * These routines are used to access the registers of the DZ
98 * chip, hiding relocation differences between implementation.
99 * ------------------------------------------------------------
100 */
101
102static u16 dz_in(struct dz_port *dport, unsigned offset)
103{
104 void __iomem *addr = dport->port.membase + offset;
105
106 return readw(addr);
107}
108
109static void dz_out(struct dz_port *dport, unsigned offset, u16 value)
110{
111 void __iomem *addr = dport->port.membase + offset;
112
113 writew(value, addr);
114}
115
116/*
117 * ------------------------------------------------------------
118 * rs_stop () and rs_start ()
119 *
120 * These routines are called before setting or resetting
121 * tty->stopped. They enable or disable transmitter interrupts,
122 * as necessary.
123 * ------------------------------------------------------------
124 */
125
126static void dz_stop_tx(struct uart_port *uport)
127{
128 struct dz_port *dport = to_dport(uport);
129 u16 tmp, mask = 1 << dport->port.line;
130
131 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
132 tmp &= ~mask; /* clear the TX flag */
133 dz_out(dport, DZ_TCR, tmp);
134}
135
136static void dz_start_tx(struct uart_port *uport)
137{
138 struct dz_port *dport = to_dport(uport);
139 u16 tmp, mask = 1 << dport->port.line;
140
141 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
142 tmp |= mask; /* set the TX flag */
143 dz_out(dport, DZ_TCR, tmp);
144}
145
146static void dz_stop_rx(struct uart_port *uport)
147{
148 struct dz_port *dport = to_dport(uport);
149
150 dport->cflag &= ~DZ_RXENAB;
151 dz_out(dport, DZ_LPR, dport->cflag);
152}
153
154static void dz_enable_ms(struct uart_port *uport)
155{
156 /* nothing to do */
157}
158
159/*
160 * ------------------------------------------------------------
161 *
162 * Here start the interrupt handling routines. All of the following
163 * subroutines are declared as inline and are folded into
164 * dz_interrupt. They were separated out for readability's sake.
165 *
166 * Note: dz_interrupt() is a "fast" interrupt, which means that it
167 * runs with interrupts turned off. People who may want to modify
168 * dz_interrupt() should try to keep the interrupt handler as fast as
169 * possible. After you are done making modifications, it is not a bad
170 * idea to do:
171 *
172 * make drivers/serial/dz.s
173 *
174 * and look at the resulting assemble code in dz.s.
175 *
176 * ------------------------------------------------------------
177 */
178
179/*
180 * ------------------------------------------------------------
181 * receive_char ()
182 *
183 * This routine deals with inputs from any lines.
184 * ------------------------------------------------------------
185 */
186static inline void dz_receive_chars(struct dz_mux *mux)
187{
188 struct uart_port *uport;
189 struct dz_port *dport = &mux->dport[0];
190 struct tty_struct *tty = NULL;
191 struct uart_icount *icount;
192 int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
193 unsigned char ch, flag;
194 u16 status;
195 int i;
196
197 while ((status = dz_in(dport, DZ_RBUF)) & DZ_DVAL) {
198 dport = &mux->dport[LINE(status)];
199 uport = &dport->port;
200 tty = uport->state->port.tty; /* point to the proper dev */
201
202 ch = UCHAR(status); /* grab the char */
203 flag = TTY_NORMAL;
204
205 icount = &uport->icount;
206 icount->rx++;
207
208 if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
209
210 /*
211 * There is no separate BREAK status bit, so treat
212 * null characters with framing errors as BREAKs;
213 * normally, otherwise. For this move the Framing
214 * Error bit to a simulated BREAK bit.
215 */
216 if (!ch) {
217 status |= (status & DZ_FERR) >>
218 (ffs(DZ_FERR) - ffs(DZ_BREAK));
219 status &= ~DZ_FERR;
220 }
221
222 /* Handle SysRq/SAK & keep track of the statistics. */
223 if (status & DZ_BREAK) {
224 icount->brk++;
225 if (uart_handle_break(uport))
226 continue;
227 } else if (status & DZ_FERR)
228 icount->frame++;
229 else if (status & DZ_PERR)
230 icount->parity++;
231 if (status & DZ_OERR)
232 icount->overrun++;
233
234 status &= uport->read_status_mask;
235 if (status & DZ_BREAK)
236 flag = TTY_BREAK;
237 else if (status & DZ_FERR)
238 flag = TTY_FRAME;
239 else if (status & DZ_PERR)
240 flag = TTY_PARITY;
241
242 }
243
244 if (uart_handle_sysrq_char(uport, ch))
245 continue;
246
247 uart_insert_char(uport, status, DZ_OERR, ch, flag);
248 lines_rx[LINE(status)] = 1;
249 }
250 for (i = 0; i < DZ_NB_PORT; i++)
251 if (lines_rx[i])
252 tty_flip_buffer_push(mux->dport[i].port.state->port.tty);
253}
254
255/*
256 * ------------------------------------------------------------
257 * transmit_char ()
258 *
259 * This routine deals with outputs to any lines.
260 * ------------------------------------------------------------
261 */
262static inline void dz_transmit_chars(struct dz_mux *mux)
263{
264 struct dz_port *dport = &mux->dport[0];
265 struct circ_buf *xmit;
266 unsigned char tmp;
267 u16 status;
268
269 status = dz_in(dport, DZ_CSR);
270 dport = &mux->dport[LINE(status)];
271 xmit = &dport->port.state->xmit;
272
273 if (dport->port.x_char) { /* XON/XOFF chars */
274 dz_out(dport, DZ_TDR, dport->port.x_char);
275 dport->port.icount.tx++;
276 dport->port.x_char = 0;
277 return;
278 }
279 /* If nothing to do or stopped or hardware stopped. */
280 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
281 spin_lock(&dport->port.lock);
282 dz_stop_tx(&dport->port);
283 spin_unlock(&dport->port.lock);
284 return;
285 }
286
287 /*
288 * If something to do... (remember the dz has no output fifo,
289 * so we go one char at a time) :-<
290 */
291 tmp = xmit->buf[xmit->tail];
292 xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
293 dz_out(dport, DZ_TDR, tmp);
294 dport->port.icount.tx++;
295
296 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
297 uart_write_wakeup(&dport->port);
298
299 /* Are we are done. */
300 if (uart_circ_empty(xmit)) {
301 spin_lock(&dport->port.lock);
302 dz_stop_tx(&dport->port);
303 spin_unlock(&dport->port.lock);
304 }
305}
306
307/*
308 * ------------------------------------------------------------
309 * check_modem_status()
310 *
311 * DS 3100 & 5100: Only valid for the MODEM line, duh!
312 * DS 5000/200: Valid for the MODEM and PRINTER line.
313 * ------------------------------------------------------------
314 */
315static inline void check_modem_status(struct dz_port *dport)
316{
317 /*
318 * FIXME:
319 * 1. No status change interrupt; use a timer.
320 * 2. Handle the 3100/5000 as appropriate. --macro
321 */
322 u16 status;
323
324 /* If not the modem line just return. */
325 if (dport->port.line != DZ_MODEM)
326 return;
327
328 status = dz_in(dport, DZ_MSR);
329
330 /* it's easy, since DSR2 is the only bit in the register */
331 if (status)
332 dport->port.icount.dsr++;
333}
334
335/*
336 * ------------------------------------------------------------
337 * dz_interrupt ()
338 *
339 * this is the main interrupt routine for the DZ chip.
340 * It deals with the multiple ports.
341 * ------------------------------------------------------------
342 */
343static irqreturn_t dz_interrupt(int irq, void *dev_id)
344{
345 struct dz_mux *mux = dev_id;
346 struct dz_port *dport = &mux->dport[0];
347 u16 status;
348
349 /* get the reason why we just got an irq */
350 status = dz_in(dport, DZ_CSR);
351
352 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
353 dz_receive_chars(mux);
354
355 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
356 dz_transmit_chars(mux);
357
358 return IRQ_HANDLED;
359}
360
361/*
362 * -------------------------------------------------------------------
363 * Here ends the DZ interrupt routines.
364 * -------------------------------------------------------------------
365 */
366
367static unsigned int dz_get_mctrl(struct uart_port *uport)
368{
369 /*
370 * FIXME: Handle the 3100/5000 as appropriate. --macro
371 */
372 struct dz_port *dport = to_dport(uport);
373 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
374
375 if (dport->port.line == DZ_MODEM) {
376 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
377 mctrl &= ~TIOCM_DSR;
378 }
379
380 return mctrl;
381}
382
383static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
384{
385 /*
386 * FIXME: Handle the 3100/5000 as appropriate. --macro
387 */
388 struct dz_port *dport = to_dport(uport);
389 u16 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 = to_dport(uport);
411 struct dz_mux *mux = dport->mux;
412 unsigned long flags;
413 int irq_guard;
414 int ret;
415 u16 tmp;
416
417 irq_guard = atomic_add_return(1, &mux->irq_guard);
418 if (irq_guard != 1)
419 return 0;
420
421 ret = request_irq(dport->port.irq, dz_interrupt,
422 IRQF_SHARED, "dz", mux);
423 if (ret) {
424 atomic_add(-1, &mux->irq_guard);
425 printk(KERN_ERR "dz: Cannot get IRQ %d!\n", dport->port.irq);
426 return ret;
427 }
428
429 spin_lock_irqsave(&dport->port.lock, flags);
430
431 /* Enable interrupts. */
432 tmp = dz_in(dport, DZ_CSR);
433 tmp |= DZ_RIE | DZ_TIE;
434 dz_out(dport, DZ_CSR, tmp);
435
436 spin_unlock_irqrestore(&dport->port.lock, flags);
437
438 return 0;
439}
440
441/*
442 * -------------------------------------------------------------------
443 * shutdown ()
444 *
445 * This routine will shutdown a serial port; interrupts are disabled, and
446 * DTR is dropped if the hangup on close termio flag is on.
447 * -------------------------------------------------------------------
448 */
449static void dz_shutdown(struct uart_port *uport)
450{
451 struct dz_port *dport = to_dport(uport);
452 struct dz_mux *mux = dport->mux;
453 unsigned long flags;
454 int irq_guard;
455 u16 tmp;
456
457 spin_lock_irqsave(&dport->port.lock, flags);
458 dz_stop_tx(&dport->port);
459 spin_unlock_irqrestore(&dport->port.lock, flags);
460
461 irq_guard = atomic_add_return(-1, &mux->irq_guard);
462 if (!irq_guard) {
463 /* Disable interrupts. */
464 tmp = dz_in(dport, DZ_CSR);
465 tmp &= ~(DZ_RIE | DZ_TIE);
466 dz_out(dport, DZ_CSR, tmp);
467
468 free_irq(dport->port.irq, mux);
469 }
470}
471
472/*
473 * -------------------------------------------------------------------
474 * dz_tx_empty() -- get the transmitter empty status
475 *
476 * Purpose: Let user call ioctl() to get info when the UART physically
477 * is emptied. On bus types like RS485, the transmitter must
478 * release the bus after transmitting. This must be done when
479 * the transmit shift register is empty, not be done when the
480 * transmit holding register is empty. This functionality
481 * allows an RS485 driver to be written in user space.
482 * -------------------------------------------------------------------
483 */
484static unsigned int dz_tx_empty(struct uart_port *uport)
485{
486 struct dz_port *dport = to_dport(uport);
487 unsigned short tmp, mask = 1 << dport->port.line;
488
489 tmp = dz_in(dport, DZ_TCR);
490 tmp &= mask;
491
492 return tmp ? 0 : TIOCSER_TEMT;
493}
494
495static void dz_break_ctl(struct uart_port *uport, int break_state)
496{
497 /*
498 * FIXME: Can't access BREAK bits in TDR easily;
499 * reuse the code for polled TX. --macro
500 */
501 struct dz_port *dport = to_dport(uport);
502 unsigned long flags;
503 unsigned short tmp, mask = 1 << dport->port.line;
504
505 spin_lock_irqsave(&uport->lock, flags);
506 tmp = dz_in(dport, DZ_TCR);
507 if (break_state)
508 tmp |= mask;
509 else
510 tmp &= ~mask;
511 dz_out(dport, DZ_TCR, tmp);
512 spin_unlock_irqrestore(&uport->lock, flags);
513}
514
515static int dz_encode_baud_rate(unsigned int baud)
516{
517 switch (baud) {
518 case 50:
519 return DZ_B50;
520 case 75:
521 return DZ_B75;
522 case 110:
523 return DZ_B110;
524 case 134:
525 return DZ_B134;
526 case 150:
527 return DZ_B150;
528 case 300:
529 return DZ_B300;
530 case 600:
531 return DZ_B600;
532 case 1200:
533 return DZ_B1200;
534 case 1800:
535 return DZ_B1800;
536 case 2000:
537 return DZ_B2000;
538 case 2400:
539 return DZ_B2400;
540 case 3600:
541 return DZ_B3600;
542 case 4800:
543 return DZ_B4800;
544 case 7200:
545 return DZ_B7200;
546 case 9600:
547 return DZ_B9600;
548 default:
549 return -1;
550 }
551}
552
553
554static void dz_reset(struct dz_port *dport)
555{
556 struct dz_mux *mux = dport->mux;
557
558 if (mux->initialised)
559 return;
560
561 dz_out(dport, DZ_CSR, DZ_CLR);
562 while (dz_in(dport, DZ_CSR) & DZ_CLR);
563 iob();
564
565 /* Enable scanning. */
566 dz_out(dport, DZ_CSR, DZ_MSE);
567
568 mux->initialised = 1;
569}
570
571static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
572 struct ktermios *old_termios)
573{
574 struct dz_port *dport = to_dport(uport);
575 unsigned long flags;
576 unsigned int cflag, baud;
577 int bflag;
578
579 cflag = dport->port.line;
580
581 switch (termios->c_cflag & CSIZE) {
582 case CS5:
583 cflag |= DZ_CS5;
584 break;
585 case CS6:
586 cflag |= DZ_CS6;
587 break;
588 case CS7:
589 cflag |= DZ_CS7;
590 break;
591 case CS8:
592 default:
593 cflag |= DZ_CS8;
594 }
595
596 if (termios->c_cflag & CSTOPB)
597 cflag |= DZ_CSTOPB;
598 if (termios->c_cflag & PARENB)
599 cflag |= DZ_PARENB;
600 if (termios->c_cflag & PARODD)
601 cflag |= DZ_PARODD;
602
603 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
604 bflag = dz_encode_baud_rate(baud);
605 if (bflag < 0) { /* Try to keep unchanged. */
606 baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
607 bflag = dz_encode_baud_rate(baud);
608 if (bflag < 0) { /* Resort to 9600. */
609 baud = 9600;
610 bflag = DZ_B9600;
611 }
612 tty_termios_encode_baud_rate(termios, baud, baud);
613 }
614 cflag |= bflag;
615
616 if (termios->c_cflag & CREAD)
617 cflag |= DZ_RXENAB;
618
619 spin_lock_irqsave(&dport->port.lock, flags);
620
621 uart_update_timeout(uport, termios->c_cflag, baud);
622
623 dz_out(dport, DZ_LPR, cflag);
624 dport->cflag = cflag;
625
626 /* setup accept flag */
627 dport->port.read_status_mask = DZ_OERR;
628 if (termios->c_iflag & INPCK)
629 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
630 if (termios->c_iflag & (BRKINT | PARMRK))
631 dport->port.read_status_mask |= DZ_BREAK;
632
633 /* characters to ignore */
634 uport->ignore_status_mask = 0;
635 if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
636 dport->port.ignore_status_mask |= DZ_OERR;
637 if (termios->c_iflag & IGNPAR)
638 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
639 if (termios->c_iflag & IGNBRK)
640 dport->port.ignore_status_mask |= DZ_BREAK;
641
642 spin_unlock_irqrestore(&dport->port.lock, flags);
643}
644
645/*
646 * Hack alert!
647 * Required solely so that the initial PROM-based console
648 * works undisturbed in parallel with this one.
649 */
650static void dz_pm(struct uart_port *uport, unsigned int state,
651 unsigned int oldstate)
652{
653 struct dz_port *dport = to_dport(uport);
654 unsigned long flags;
655
656 spin_lock_irqsave(&dport->port.lock, flags);
657 if (state < 3)
658 dz_start_tx(&dport->port);
659 else
660 dz_stop_tx(&dport->port);
661 spin_unlock_irqrestore(&dport->port.lock, flags);
662}
663
664
665static const char *dz_type(struct uart_port *uport)
666{
667 return "DZ";
668}
669
670static void dz_release_port(struct uart_port *uport)
671{
672 struct dz_mux *mux = to_dport(uport)->mux;
673 int map_guard;
674
675 iounmap(uport->membase);
676 uport->membase = NULL;
677
678 map_guard = atomic_add_return(-1, &mux->map_guard);
679 if (!map_guard)
680 release_mem_region(uport->mapbase, dec_kn_slot_size);
681}
682
683static int dz_map_port(struct uart_port *uport)
684{
685 if (!uport->membase)
686 uport->membase = ioremap_nocache(uport->mapbase,
687 dec_kn_slot_size);
688 if (!uport->membase) {
689 printk(KERN_ERR "dz: Cannot map MMIO\n");
690 return -ENOMEM;
691 }
692 return 0;
693}
694
695static int dz_request_port(struct uart_port *uport)
696{
697 struct dz_mux *mux = to_dport(uport)->mux;
698 int map_guard;
699 int ret;
700
701 map_guard = atomic_add_return(1, &mux->map_guard);
702 if (map_guard == 1) {
703 if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
704 "dz")) {
705 atomic_add(-1, &mux->map_guard);
706 printk(KERN_ERR
707 "dz: Unable to reserve MMIO resource\n");
708 return -EBUSY;
709 }
710 }
711 ret = dz_map_port(uport);
712 if (ret) {
713 map_guard = atomic_add_return(-1, &mux->map_guard);
714 if (!map_guard)
715 release_mem_region(uport->mapbase, dec_kn_slot_size);
716 return ret;
717 }
718 return 0;
719}
720
721static void dz_config_port(struct uart_port *uport, int flags)
722{
723 struct dz_port *dport = to_dport(uport);
724
725 if (flags & UART_CONFIG_TYPE) {
726 if (dz_request_port(uport))
727 return;
728
729 uport->type = PORT_DZ;
730
731 dz_reset(dport);
732 }
733}
734
735/*
736 * Verify the new serial_struct (for TIOCSSERIAL).
737 */
738static int dz_verify_port(struct uart_port *uport, struct serial_struct *ser)
739{
740 int ret = 0;
741
742 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
743 ret = -EINVAL;
744 if (ser->irq != uport->irq)
745 ret = -EINVAL;
746 return ret;
747}
748
749static struct uart_ops dz_ops = {
750 .tx_empty = dz_tx_empty,
751 .get_mctrl = dz_get_mctrl,
752 .set_mctrl = dz_set_mctrl,
753 .stop_tx = dz_stop_tx,
754 .start_tx = dz_start_tx,
755 .stop_rx = dz_stop_rx,
756 .enable_ms = dz_enable_ms,
757 .break_ctl = dz_break_ctl,
758 .startup = dz_startup,
759 .shutdown = dz_shutdown,
760 .set_termios = dz_set_termios,
761 .pm = dz_pm,
762 .type = dz_type,
763 .release_port = dz_release_port,
764 .request_port = dz_request_port,
765 .config_port = dz_config_port,
766 .verify_port = dz_verify_port,
767};
768
769static void __init dz_init_ports(void)
770{
771 static int first = 1;
772 unsigned long base;
773 int line;
774
775 if (!first)
776 return;
777 first = 0;
778
779 if (mips_machtype == MACH_DS23100 || mips_machtype == MACH_DS5100)
780 base = dec_kn_slot_base + KN01_DZ11;
781 else
782 base = dec_kn_slot_base + KN02_DZ11;
783
784 for (line = 0; line < DZ_NB_PORT; line++) {
785 struct dz_port *dport = &dz_mux.dport[line];
786 struct uart_port *uport = &dport->port;
787
788 dport->mux = &dz_mux;
789
790 uport->irq = dec_interrupt[DEC_IRQ_DZ11];
791 uport->fifosize = 1;
792 uport->iotype = UPIO_MEM;
793 uport->flags = UPF_BOOT_AUTOCONF;
794 uport->ops = &dz_ops;
795 uport->line = line;
796 uport->mapbase = base;
797 }
798}
799
800#ifdef CONFIG_SERIAL_DZ_CONSOLE
801/*
802 * -------------------------------------------------------------------
803 * dz_console_putchar() -- transmit a character
804 *
805 * Polled transmission. This is tricky. We need to mask transmit
806 * interrupts so that they do not interfere, enable the transmitter
807 * for the line requested and then wait till the transmit scanner
808 * requests data for this line. But it may request data for another
809 * line first, in which case we have to disable its transmitter and
810 * repeat waiting till our line pops up. Only then the character may
811 * be transmitted. Finally, the state of the transmitter mask is
812 * restored. Welcome to the world of PDP-11!
813 * -------------------------------------------------------------------
814 */
815static void dz_console_putchar(struct uart_port *uport, int ch)
816{
817 struct dz_port *dport = to_dport(uport);
818 unsigned long flags;
819 unsigned short csr, tcr, trdy, mask;
820 int loops = 10000;
821
822 spin_lock_irqsave(&dport->port.lock, flags);
823 csr = dz_in(dport, DZ_CSR);
824 dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
825 tcr = dz_in(dport, DZ_TCR);
826 tcr |= 1 << dport->port.line;
827 mask = tcr;
828 dz_out(dport, DZ_TCR, mask);
829 iob();
830 spin_unlock_irqrestore(&dport->port.lock, flags);
831
832 do {
833 trdy = dz_in(dport, DZ_CSR);
834 if (!(trdy & DZ_TRDY))
835 continue;
836 trdy = (trdy & DZ_TLINE) >> 8;
837 if (trdy == dport->port.line)
838 break;
839 mask &= ~(1 << trdy);
840 dz_out(dport, DZ_TCR, mask);
841 iob();
842 udelay(2);
843 } while (--loops);
844
845 if (loops) /* Cannot send otherwise. */
846 dz_out(dport, DZ_TDR, ch);
847
848 dz_out(dport, DZ_TCR, tcr);
849 dz_out(dport, DZ_CSR, csr);
850}
851
852/*
853 * -------------------------------------------------------------------
854 * dz_console_print ()
855 *
856 * dz_console_print is registered for printk.
857 * The console must be locked when we get here.
858 * -------------------------------------------------------------------
859 */
860static void dz_console_print(struct console *co,
861 const char *str,
862 unsigned int count)
863{
864 struct dz_port *dport = &dz_mux.dport[co->index];
865#ifdef DEBUG_DZ
866 prom_printf((char *) str);
867#endif
868 uart_console_write(&dport->port, str, count, dz_console_putchar);
869}
870
871static int __init dz_console_setup(struct console *co, char *options)
872{
873 struct dz_port *dport = &dz_mux.dport[co->index];
874 struct uart_port *uport = &dport->port;
875 int baud = 9600;
876 int bits = 8;
877 int parity = 'n';
878 int flow = 'n';
879 int ret;
880
881 ret = dz_map_port(uport);
882 if (ret)
883 return ret;
884
885 spin_lock_init(&dport->port.lock); /* For dz_pm(). */
886
887 dz_reset(dport);
888 dz_pm(uport, 0, -1);
889
890 if (options)
891 uart_parse_options(options, &baud, &parity, &bits, &flow);
892
893 return uart_set_options(&dport->port, co, baud, parity, bits, flow);
894}
895
896static struct uart_driver dz_reg;
897static struct console dz_console = {
898 .name = "ttyS",
899 .write = dz_console_print,
900 .device = uart_console_device,
901 .setup = dz_console_setup,
902 .flags = CON_PRINTBUFFER,
903 .index = -1,
904 .data = &dz_reg,
905};
906
907static int __init dz_serial_console_init(void)
908{
909 if (!IOASIC) {
910 dz_init_ports();
911 register_console(&dz_console);
912 return 0;
913 } else
914 return -ENXIO;
915}
916
917console_initcall(dz_serial_console_init);
918
919#define SERIAL_DZ_CONSOLE &dz_console
920#else
921#define SERIAL_DZ_CONSOLE NULL
922#endif /* CONFIG_SERIAL_DZ_CONSOLE */
923
924static struct uart_driver dz_reg = {
925 .owner = THIS_MODULE,
926 .driver_name = "serial",
927 .dev_name = "ttyS",
928 .major = TTY_MAJOR,
929 .minor = 64,
930 .nr = DZ_NB_PORT,
931 .cons = SERIAL_DZ_CONSOLE,
932};
933
934static int __init dz_init(void)
935{
936 int ret, i;
937
938 if (IOASIC)
939 return -ENXIO;
940
941 printk("%s%s\n", dz_name, dz_version);
942
943 dz_init_ports();
944
945 ret = uart_register_driver(&dz_reg);
946 if (ret)
947 return ret;
948
949 for (i = 0; i < DZ_NB_PORT; i++)
950 uart_add_one_port(&dz_reg, &dz_mux.dport[i].port);
951
952 return 0;
953}
954
955module_init(dz_init);
diff --git a/drivers/tty/serial/dz.h b/drivers/tty/serial/dz.h
new file mode 100644
index 000000000000..faf169ed27b3
--- /dev/null
+++ b/drivers/tty/serial/dz.h
@@ -0,0 +1,129 @@
1/*
2 * dz.h: Serial port driver for DECstations equipped
3 * with the DZ chipset.
4 *
5 * Copyright (C) 1998 Olivier A. D. Lebaillif
6 *
7 * Email: olivier.lebaillif@ifrsys.com
8 *
9 * Copyright (C) 2004, 2006 Maciej W. Rozycki
10 */
11#ifndef DZ_SERIAL_H
12#define DZ_SERIAL_H
13
14/*
15 * Definitions for the Control and Status Register.
16 */
17#define DZ_TRDY 0x8000 /* Transmitter empty */
18#define DZ_TIE 0x4000 /* Transmitter Interrupt Enbl */
19#define DZ_TLINE 0x0300 /* Transmitter Line Number */
20#define DZ_RDONE 0x0080 /* Receiver data ready */
21#define DZ_RIE 0x0040 /* Receive Interrupt Enable */
22#define DZ_MSE 0x0020 /* Master Scan Enable */
23#define DZ_CLR 0x0010 /* Master reset */
24#define DZ_MAINT 0x0008 /* Loop Back Mode */
25
26/*
27 * Definitions for the Receiver Buffer Register.
28 */
29#define DZ_RBUF_MASK 0x00FF /* Data Mask */
30#define DZ_LINE_MASK 0x0300 /* Line Mask */
31#define DZ_DVAL 0x8000 /* Valid Data indicator */
32#define DZ_OERR 0x4000 /* Overrun error indicator */
33#define DZ_FERR 0x2000 /* Frame error indicator */
34#define DZ_PERR 0x1000 /* Parity error indicator */
35
36#define DZ_BREAK 0x0800 /* BREAK event software flag */
37
38#define LINE(x) ((x & DZ_LINE_MASK) >> 8) /* Get the line number
39 from the input buffer */
40#define UCHAR(x) ((unsigned char)(x & DZ_RBUF_MASK))
41
42/*
43 * Definitions for the Transmit Control Register.
44 */
45#define DZ_LINE_KEYBOARD 0x0001
46#define DZ_LINE_MOUSE 0x0002
47#define DZ_LINE_MODEM 0x0004
48#define DZ_LINE_PRINTER 0x0008
49
50#define DZ_MODEM_RTS 0x0800 /* RTS for the modem line (2) */
51#define DZ_MODEM_DTR 0x0400 /* DTR for the modem line (2) */
52#define DZ_PRINT_RTS 0x0200 /* RTS for the prntr line (3) */
53#define DZ_PRINT_DTR 0x0100 /* DTR for the prntr line (3) */
54#define DZ_LNENB 0x000f /* Transmitter Line Enable */
55
56/*
57 * Definitions for the Modem Status Register.
58 */
59#define DZ_MODEM_RI 0x0800 /* RI for the modem line (2) */
60#define DZ_MODEM_CD 0x0400 /* CD for the modem line (2) */
61#define DZ_MODEM_DSR 0x0200 /* DSR for the modem line (2) */
62#define DZ_MODEM_CTS 0x0100 /* CTS for the modem line (2) */
63#define DZ_PRINT_RI 0x0008 /* RI for the printer line (3) */
64#define DZ_PRINT_CD 0x0004 /* CD for the printer line (3) */
65#define DZ_PRINT_DSR 0x0002 /* DSR for the prntr line (3) */
66#define DZ_PRINT_CTS 0x0001 /* CTS for the prntr line (3) */
67
68/*
69 * Definitions for the Transmit Data Register.
70 */
71#define DZ_BRK0 0x0100 /* Break assertion for line 0 */
72#define DZ_BRK1 0x0200 /* Break assertion for line 1 */
73#define DZ_BRK2 0x0400 /* Break assertion for line 2 */
74#define DZ_BRK3 0x0800 /* Break assertion for line 3 */
75
76/*
77 * Definitions for the Line Parameter Register.
78 */
79#define DZ_KEYBOARD 0x0000 /* line 0 = keyboard */
80#define DZ_MOUSE 0x0001 /* line 1 = mouse */
81#define DZ_MODEM 0x0002 /* line 2 = modem */
82#define DZ_PRINTER 0x0003 /* line 3 = printer */
83
84#define DZ_CSIZE 0x0018 /* Number of bits per byte (mask) */
85#define DZ_CS5 0x0000 /* 5 bits per byte */
86#define DZ_CS6 0x0008 /* 6 bits per byte */
87#define DZ_CS7 0x0010 /* 7 bits per byte */
88#define DZ_CS8 0x0018 /* 8 bits per byte */
89
90#define DZ_CSTOPB 0x0020 /* 2 stop bits instead of one */
91
92#define DZ_PARENB 0x0040 /* Parity enable */
93#define DZ_PARODD 0x0080 /* Odd parity instead of even */
94
95#define DZ_CBAUD 0x0E00 /* Baud Rate (mask) */
96#define DZ_B50 0x0000
97#define DZ_B75 0x0100
98#define DZ_B110 0x0200
99#define DZ_B134 0x0300
100#define DZ_B150 0x0400
101#define DZ_B300 0x0500
102#define DZ_B600 0x0600
103#define DZ_B1200 0x0700
104#define DZ_B1800 0x0800
105#define DZ_B2000 0x0900
106#define DZ_B2400 0x0A00
107#define DZ_B3600 0x0B00
108#define DZ_B4800 0x0C00
109#define DZ_B7200 0x0D00
110#define DZ_B9600 0x0E00
111
112#define DZ_RXENAB 0x1000 /* Receiver Enable */
113
114/*
115 * Addresses for the DZ registers
116 */
117#define DZ_CSR 0x00 /* Control and Status Register */
118#define DZ_RBUF 0x08 /* Receive Buffer */
119#define DZ_LPR 0x08 /* Line Parameters Register */
120#define DZ_TCR 0x10 /* Transmitter Control Register */
121#define DZ_MSR 0x18 /* Modem Status Register */
122#define DZ_TDR 0x18 /* Transmit Data Register */
123
124#define DZ_NB_PORT 4
125
126#define DZ_XMIT_SIZE 4096 /* buffer size */
127#define DZ_WAKEUP_CHARS DZ_XMIT_SIZE/4
128
129#endif /* DZ_SERIAL_H */
diff --git a/drivers/tty/serial/icom.c b/drivers/tty/serial/icom.c
new file mode 100644
index 000000000000..53a468227056
--- /dev/null
+++ b/drivers/tty/serial/icom.c
@@ -0,0 +1,1658 @@
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/kernel.h>
28#include <linux/errno.h>
29#include <linux/signal.h>
30#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/termios.h>
34#include <linux/fs.h>
35#include <linux/tty_flip.h>
36#include <linux/serial.h>
37#include <linux/serial_reg.h>
38#include <linux/major.h>
39#include <linux/string.h>
40#include <linux/fcntl.h>
41#include <linux/ptrace.h>
42#include <linux/ioport.h>
43#include <linux/mm.h>
44#include <linux/slab.h>
45#include <linux/init.h>
46#include <linux/delay.h>
47#include <linux/pci.h>
48#include <linux/vmalloc.h>
49#include <linux/smp.h>
50#include <linux/spinlock.h>
51#include <linux/kref.h>
52#include <linux/firmware.h>
53#include <linux/bitops.h>
54
55#include <asm/system.h>
56#include <asm/io.h>
57#include <asm/irq.h>
58#include <asm/uaccess.h>
59
60#include "icom.h"
61
62/*#define ICOM_TRACE enable port trace capabilities */
63
64#define ICOM_DRIVER_NAME "icom"
65#define ICOM_VERSION_STR "1.3.1"
66#define NR_PORTS 128
67#define ICOM_PORT ((struct icom_port *)port)
68#define to_icom_adapter(d) container_of(d, struct icom_adapter, kref)
69
70static const struct pci_device_id icom_pci_table[] = {
71 {
72 .vendor = PCI_VENDOR_ID_IBM,
73 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_1,
74 .subvendor = PCI_ANY_ID,
75 .subdevice = PCI_ANY_ID,
76 .driver_data = ADAPTER_V1,
77 },
78 {
79 .vendor = PCI_VENDOR_ID_IBM,
80 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
81 .subvendor = PCI_VENDOR_ID_IBM,
82 .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_TWO_PORTS_RVX,
83 .driver_data = ADAPTER_V2,
84 },
85 {
86 .vendor = PCI_VENDOR_ID_IBM,
87 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
88 .subvendor = PCI_VENDOR_ID_IBM,
89 .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM,
90 .driver_data = ADAPTER_V2,
91 },
92 {
93 .vendor = PCI_VENDOR_ID_IBM,
94 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
95 .subvendor = PCI_VENDOR_ID_IBM,
96 .subdevice = PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL,
97 .driver_data = ADAPTER_V2,
98 },
99 {
100 .vendor = PCI_VENDOR_ID_IBM,
101 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
102 .subvendor = PCI_VENDOR_ID_IBM,
103 .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM_PCIE,
104 .driver_data = ADAPTER_V2,
105 },
106 {}
107};
108
109struct lookup_proc_table start_proc[4] = {
110 {NULL, ICOM_CONTROL_START_A},
111 {NULL, ICOM_CONTROL_START_B},
112 {NULL, ICOM_CONTROL_START_C},
113 {NULL, ICOM_CONTROL_START_D}
114};
115
116
117struct lookup_proc_table stop_proc[4] = {
118 {NULL, ICOM_CONTROL_STOP_A},
119 {NULL, ICOM_CONTROL_STOP_B},
120 {NULL, ICOM_CONTROL_STOP_C},
121 {NULL, ICOM_CONTROL_STOP_D}
122};
123
124struct lookup_int_table int_mask_tbl[4] = {
125 {NULL, ICOM_INT_MASK_PRC_A},
126 {NULL, ICOM_INT_MASK_PRC_B},
127 {NULL, ICOM_INT_MASK_PRC_C},
128 {NULL, ICOM_INT_MASK_PRC_D},
129};
130
131
132MODULE_DEVICE_TABLE(pci, icom_pci_table);
133
134static LIST_HEAD(icom_adapter_head);
135
136/* spinlock for adapter initialization and changing adapter operations */
137static spinlock_t icom_lock;
138
139#ifdef ICOM_TRACE
140static inline void trace(struct icom_port *icom_port, char *trace_pt,
141 unsigned long trace_data)
142{
143 dev_info(&icom_port->adapter->pci_dev->dev, ":%d:%s - %lx\n",
144 icom_port->port, trace_pt, trace_data);
145}
146#else
147static inline void trace(struct icom_port *icom_port, char *trace_pt, unsigned long trace_data) {};
148#endif
149static void icom_kref_release(struct kref *kref);
150
151static void free_port_memory(struct icom_port *icom_port)
152{
153 struct pci_dev *dev = icom_port->adapter->pci_dev;
154
155 trace(icom_port, "RET_PORT_MEM", 0);
156 if (icom_port->recv_buf) {
157 pci_free_consistent(dev, 4096, icom_port->recv_buf,
158 icom_port->recv_buf_pci);
159 icom_port->recv_buf = NULL;
160 }
161 if (icom_port->xmit_buf) {
162 pci_free_consistent(dev, 4096, icom_port->xmit_buf,
163 icom_port->xmit_buf_pci);
164 icom_port->xmit_buf = NULL;
165 }
166 if (icom_port->statStg) {
167 pci_free_consistent(dev, 4096, icom_port->statStg,
168 icom_port->statStg_pci);
169 icom_port->statStg = NULL;
170 }
171
172 if (icom_port->xmitRestart) {
173 pci_free_consistent(dev, 4096, icom_port->xmitRestart,
174 icom_port->xmitRestart_pci);
175 icom_port->xmitRestart = NULL;
176 }
177}
178
179static int __devinit get_port_memory(struct icom_port *icom_port)
180{
181 int index;
182 unsigned long stgAddr;
183 unsigned long startStgAddr;
184 unsigned long offset;
185 struct pci_dev *dev = icom_port->adapter->pci_dev;
186
187 icom_port->xmit_buf =
188 pci_alloc_consistent(dev, 4096, &icom_port->xmit_buf_pci);
189 if (!icom_port->xmit_buf) {
190 dev_err(&dev->dev, "Can not allocate Transmit buffer\n");
191 return -ENOMEM;
192 }
193
194 trace(icom_port, "GET_PORT_MEM",
195 (unsigned long) icom_port->xmit_buf);
196
197 icom_port->recv_buf =
198 pci_alloc_consistent(dev, 4096, &icom_port->recv_buf_pci);
199 if (!icom_port->recv_buf) {
200 dev_err(&dev->dev, "Can not allocate Receive buffer\n");
201 free_port_memory(icom_port);
202 return -ENOMEM;
203 }
204 trace(icom_port, "GET_PORT_MEM",
205 (unsigned long) icom_port->recv_buf);
206
207 icom_port->statStg =
208 pci_alloc_consistent(dev, 4096, &icom_port->statStg_pci);
209 if (!icom_port->statStg) {
210 dev_err(&dev->dev, "Can not allocate Status buffer\n");
211 free_port_memory(icom_port);
212 return -ENOMEM;
213 }
214 trace(icom_port, "GET_PORT_MEM",
215 (unsigned long) icom_port->statStg);
216
217 icom_port->xmitRestart =
218 pci_alloc_consistent(dev, 4096, &icom_port->xmitRestart_pci);
219 if (!icom_port->xmitRestart) {
220 dev_err(&dev->dev,
221 "Can not allocate xmit Restart buffer\n");
222 free_port_memory(icom_port);
223 return -ENOMEM;
224 }
225
226 memset(icom_port->statStg, 0, 4096);
227
228 /* FODs: Frame Out Descriptor Queue, this is a FIFO queue that
229 indicates that frames are to be transmitted
230 */
231
232 stgAddr = (unsigned long) icom_port->statStg;
233 for (index = 0; index < NUM_XBUFFS; index++) {
234 trace(icom_port, "FOD_ADDR", stgAddr);
235 stgAddr = stgAddr + sizeof(icom_port->statStg->xmit[0]);
236 if (index < (NUM_XBUFFS - 1)) {
237 memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
238 icom_port->statStg->xmit[index].leLengthASD =
239 (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ);
240 trace(icom_port, "FOD_ADDR", stgAddr);
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 if (index == (NUM_XBUFFS - 1)) {
246 memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
247 icom_port->statStg->xmit[index].leLengthASD =
248 (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ);
249 trace(icom_port, "FOD_XBUFF",
250 (unsigned long) icom_port->xmit_buf);
251 icom_port->statStg->xmit[index].leBuffer =
252 cpu_to_le32(icom_port->xmit_buf_pci);
253 } else {
254 memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
255 }
256 }
257 /* FIDs */
258 startStgAddr = stgAddr;
259
260 /* fill in every entry, even if no buffer */
261 for (index = 0; index < NUM_RBUFFS; index++) {
262 trace(icom_port, "FID_ADDR", stgAddr);
263 stgAddr = stgAddr + sizeof(icom_port->statStg->rcv[0]);
264 icom_port->statStg->rcv[index].leLength = 0;
265 icom_port->statStg->rcv[index].WorkingLength =
266 (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
267 if (index < (NUM_RBUFFS - 1) ) {
268 offset = stgAddr - (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);
273 icom_port->statStg->rcv[index].leBuffer =
274 cpu_to_le32(icom_port->recv_buf_pci);
275 } else if (index == (NUM_RBUFFS -1) ) {
276 offset = startStgAddr - (unsigned long) icom_port->statStg;
277 icom_port->statStg->rcv[index].leNext =
278 cpu_to_le32(icom_port-> statStg_pci + offset);
279 trace(icom_port, "FID_RBUFF",
280 (unsigned long) icom_port->recv_buf + 2048);
281 icom_port->statStg->rcv[index].leBuffer =
282 cpu_to_le32(icom_port->recv_buf_pci + 2048);
283 } else {
284 icom_port->statStg->rcv[index].leNext = 0;
285 icom_port->statStg->rcv[index].leBuffer = 0;
286 }
287 }
288
289 return 0;
290}
291
292static void stop_processor(struct icom_port *icom_port)
293{
294 unsigned long temp;
295 unsigned long flags;
296 int port;
297
298 spin_lock_irqsave(&icom_lock, flags);
299
300 port = icom_port->port;
301 if (port == 0 || port == 1)
302 stop_proc[port].global_control_reg = &icom_port->global_reg->control;
303 else
304 stop_proc[port].global_control_reg = &icom_port->global_reg->control_2;
305
306
307 if (port < 4) {
308 temp = readl(stop_proc[port].global_control_reg);
309 temp =
310 (temp & ~start_proc[port].processor_id) | stop_proc[port].processor_id;
311 writel(temp, stop_proc[port].global_control_reg);
312
313 /* write flush */
314 readl(stop_proc[port].global_control_reg);
315 } else {
316 dev_err(&icom_port->adapter->pci_dev->dev,
317 "Invalid port assignment\n");
318 }
319
320 spin_unlock_irqrestore(&icom_lock, flags);
321}
322
323static void start_processor(struct icom_port *icom_port)
324{
325 unsigned long temp;
326 unsigned long flags;
327 int port;
328
329 spin_lock_irqsave(&icom_lock, flags);
330
331 port = icom_port->port;
332 if (port == 0 || port == 1)
333 start_proc[port].global_control_reg = &icom_port->global_reg->control;
334 else
335 start_proc[port].global_control_reg = &icom_port->global_reg->control_2;
336 if (port < 4) {
337 temp = readl(start_proc[port].global_control_reg);
338 temp =
339 (temp & ~stop_proc[port].processor_id) | start_proc[port].processor_id;
340 writel(temp, start_proc[port].global_control_reg);
341
342 /* write flush */
343 readl(start_proc[port].global_control_reg);
344 } else {
345 dev_err(&icom_port->adapter->pci_dev->dev,
346 "Invalid port assignment\n");
347 }
348
349 spin_unlock_irqrestore(&icom_lock, flags);
350}
351
352static void load_code(struct icom_port *icom_port)
353{
354 const struct firmware *fw;
355 char __iomem *iram_ptr;
356 int index;
357 int status = 0;
358 void __iomem *dram_ptr = icom_port->dram;
359 dma_addr_t temp_pci;
360 unsigned char *new_page = NULL;
361 unsigned char cable_id = NO_CABLE;
362 struct pci_dev *dev = icom_port->adapter->pci_dev;
363
364 /* Clear out any pending interrupts */
365 writew(0x3FFF, icom_port->int_reg);
366
367 trace(icom_port, "CLEAR_INTERRUPTS", 0);
368
369 /* Stop processor */
370 stop_processor(icom_port);
371
372 /* Zero out DRAM */
373 memset_io(dram_ptr, 0, 512);
374
375 /* Load Call Setup into Adapter */
376 if (request_firmware(&fw, "icom_call_setup.bin", &dev->dev) < 0) {
377 dev_err(&dev->dev,"Unable to load icom_call_setup.bin firmware image\n");
378 status = -1;
379 goto load_code_exit;
380 }
381
382 if (fw->size > ICOM_DCE_IRAM_OFFSET) {
383 dev_err(&dev->dev, "Invalid firmware image for icom_call_setup.bin found.\n");
384 release_firmware(fw);
385 status = -1;
386 goto load_code_exit;
387 }
388
389 iram_ptr = (char __iomem *)icom_port->dram + ICOM_IRAM_OFFSET;
390 for (index = 0; index < fw->size; index++)
391 writeb(fw->data[index], &iram_ptr[index]);
392
393 release_firmware(fw);
394
395 /* Load Resident DCE portion of Adapter */
396 if (request_firmware(&fw, "icom_res_dce.bin", &dev->dev) < 0) {
397 dev_err(&dev->dev,"Unable to load icom_res_dce.bin firmware image\n");
398 status = -1;
399 goto load_code_exit;
400 }
401
402 if (fw->size > ICOM_IRAM_SIZE) {
403 dev_err(&dev->dev, "Invalid firmware image for icom_res_dce.bin found.\n");
404 release_firmware(fw);
405 status = -1;
406 goto load_code_exit;
407 }
408
409 iram_ptr = (char __iomem *) icom_port->dram + ICOM_IRAM_OFFSET;
410 for (index = ICOM_DCE_IRAM_OFFSET; index < fw->size; index++)
411 writeb(fw->data[index], &iram_ptr[index]);
412
413 release_firmware(fw);
414
415 /* Set Hardware level */
416 if (icom_port->adapter->version == ADAPTER_V2)
417 writeb(V2_HARDWARE, &(icom_port->dram->misc_flags));
418
419 /* Start the processor in Adapter */
420 start_processor(icom_port);
421
422 writeb((HDLC_PPP_PURE_ASYNC | HDLC_FF_FILL),
423 &(icom_port->dram->HDLCConfigReg));
424 writeb(0x04, &(icom_port->dram->FlagFillIdleTimer)); /* 0.5 seconds */
425 writeb(0x00, &(icom_port->dram->CmdReg));
426 writeb(0x10, &(icom_port->dram->async_config3));
427 writeb((ICOM_ACFG_DRIVE1 | ICOM_ACFG_NO_PARITY | ICOM_ACFG_8BPC |
428 ICOM_ACFG_1STOP_BIT), &(icom_port->dram->async_config2));
429
430 /*Set up data in icom DRAM to indicate where personality
431 *code is located and its length.
432 */
433 new_page = pci_alloc_consistent(dev, 4096, &temp_pci);
434
435 if (!new_page) {
436 dev_err(&dev->dev, "Can not allocate DMA buffer\n");
437 status = -1;
438 goto load_code_exit;
439 }
440
441 if (request_firmware(&fw, "icom_asc.bin", &dev->dev) < 0) {
442 dev_err(&dev->dev,"Unable to load icom_asc.bin firmware image\n");
443 status = -1;
444 goto load_code_exit;
445 }
446
447 if (fw->size > ICOM_DCE_IRAM_OFFSET) {
448 dev_err(&dev->dev, "Invalid firmware image for icom_asc.bin found.\n");
449 release_firmware(fw);
450 status = -1;
451 goto load_code_exit;
452 }
453
454 for (index = 0; index < fw->size; index++)
455 new_page[index] = fw->data[index];
456
457 release_firmware(fw);
458
459 writeb((char) ((fw->size + 16)/16), &icom_port->dram->mac_length);
460 writel(temp_pci, &icom_port->dram->mac_load_addr);
461
462 /*Setting the syncReg to 0x80 causes adapter to start downloading
463 the personality code into adapter instruction RAM.
464 Once code is loaded, it will begin executing and, based on
465 information provided above, will start DMAing data from
466 shared memory to adapter DRAM.
467 */
468 /* the wait loop below verifies this write operation has been done
469 and processed
470 */
471 writeb(START_DOWNLOAD, &icom_port->dram->sync);
472
473 /* Wait max 1 Sec for data download and processor to start */
474 for (index = 0; index < 10; index++) {
475 msleep(100);
476 if (readb(&icom_port->dram->misc_flags) & ICOM_HDW_ACTIVE)
477 break;
478 }
479
480 if (index == 10)
481 status = -1;
482
483 /*
484 * check Cable ID
485 */
486 cable_id = readb(&icom_port->dram->cable_id);
487
488 if (cable_id & ICOM_CABLE_ID_VALID) {
489 /* Get cable ID into the lower 4 bits (standard form) */
490 cable_id = (cable_id & ICOM_CABLE_ID_MASK) >> 4;
491 icom_port->cable_id = cable_id;
492 } else {
493 dev_err(&dev->dev,"Invalid or no cable attached\n");
494 icom_port->cable_id = NO_CABLE;
495 }
496
497 load_code_exit:
498
499 if (status != 0) {
500 /* Clear out any pending interrupts */
501 writew(0x3FFF, icom_port->int_reg);
502
503 /* Turn off port */
504 writeb(ICOM_DISABLE, &(icom_port->dram->disable));
505
506 /* Stop processor */
507 stop_processor(icom_port);
508
509 dev_err(&icom_port->adapter->pci_dev->dev,"Port not opertional\n");
510 }
511
512 if (new_page != NULL)
513 pci_free_consistent(dev, 4096, new_page, temp_pci);
514}
515
516static int startup(struct icom_port *icom_port)
517{
518 unsigned long temp;
519 unsigned char cable_id, raw_cable_id;
520 unsigned long flags;
521 int port;
522
523 trace(icom_port, "STARTUP", 0);
524
525 if (!icom_port->dram) {
526 /* should NEVER be NULL */
527 dev_err(&icom_port->adapter->pci_dev->dev,
528 "Unusable Port, port configuration missing\n");
529 return -ENODEV;
530 }
531
532 /*
533 * check Cable ID
534 */
535 raw_cable_id = readb(&icom_port->dram->cable_id);
536 trace(icom_port, "CABLE_ID", raw_cable_id);
537
538 /* Get cable ID into the lower 4 bits (standard form) */
539 cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4;
540
541 /* Check for valid Cable ID */
542 if (!(raw_cable_id & ICOM_CABLE_ID_VALID) ||
543 (cable_id != icom_port->cable_id)) {
544
545 /* reload adapter code, pick up any potential changes in cable id */
546 load_code(icom_port);
547
548 /* still no sign of cable, error out */
549 raw_cable_id = readb(&icom_port->dram->cable_id);
550 cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4;
551 if (!(raw_cable_id & ICOM_CABLE_ID_VALID) ||
552 (icom_port->cable_id == NO_CABLE))
553 return -EIO;
554 }
555
556 /*
557 * Finally, clear and enable interrupts
558 */
559 spin_lock_irqsave(&icom_lock, flags);
560 port = icom_port->port;
561 if (port == 0 || port == 1)
562 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
563 else
564 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2;
565
566 if (port == 0 || port == 2)
567 writew(0x00FF, icom_port->int_reg);
568 else
569 writew(0x3F00, icom_port->int_reg);
570 if (port < 4) {
571 temp = readl(int_mask_tbl[port].global_int_mask);
572 writel(temp & ~int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
573
574 /* write flush */
575 readl(int_mask_tbl[port].global_int_mask);
576 } else {
577 dev_err(&icom_port->adapter->pci_dev->dev,
578 "Invalid port assignment\n");
579 }
580
581 spin_unlock_irqrestore(&icom_lock, flags);
582 return 0;
583}
584
585static void shutdown(struct icom_port *icom_port)
586{
587 unsigned long temp;
588 unsigned char cmdReg;
589 unsigned long flags;
590 int port;
591
592 spin_lock_irqsave(&icom_lock, flags);
593 trace(icom_port, "SHUTDOWN", 0);
594
595 /*
596 * disable all interrupts
597 */
598 port = icom_port->port;
599 if (port == 0 || port == 1)
600 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
601 else
602 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2;
603
604 if (port < 4) {
605 temp = readl(int_mask_tbl[port].global_int_mask);
606 writel(temp | int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
607
608 /* write flush */
609 readl(int_mask_tbl[port].global_int_mask);
610 } else {
611 dev_err(&icom_port->adapter->pci_dev->dev,
612 "Invalid port assignment\n");
613 }
614 spin_unlock_irqrestore(&icom_lock, flags);
615
616 /*
617 * disable break condition
618 */
619 cmdReg = readb(&icom_port->dram->CmdReg);
620 if (cmdReg & CMD_SND_BREAK) {
621 writeb(cmdReg & ~CMD_SND_BREAK, &icom_port->dram->CmdReg);
622 }
623}
624
625static int icom_write(struct uart_port *port)
626{
627 unsigned long data_count;
628 unsigned char cmdReg;
629 unsigned long offset;
630 int temp_tail = port->state->xmit.tail;
631
632 trace(ICOM_PORT, "WRITE", 0);
633
634 if (cpu_to_le16(ICOM_PORT->statStg->xmit[0].flags) &
635 SA_FLAGS_READY_TO_XMIT) {
636 trace(ICOM_PORT, "WRITE_FULL", 0);
637 return 0;
638 }
639
640 data_count = 0;
641 while ((port->state->xmit.head != temp_tail) &&
642 (data_count <= XMIT_BUFF_SZ)) {
643
644 ICOM_PORT->xmit_buf[data_count++] =
645 port->state->xmit.buf[temp_tail];
646
647 temp_tail++;
648 temp_tail &= (UART_XMIT_SIZE - 1);
649 }
650
651 if (data_count) {
652 ICOM_PORT->statStg->xmit[0].flags =
653 cpu_to_le16(SA_FLAGS_READY_TO_XMIT);
654 ICOM_PORT->statStg->xmit[0].leLength =
655 cpu_to_le16(data_count);
656 offset =
657 (unsigned long) &ICOM_PORT->statStg->xmit[0] -
658 (unsigned long) ICOM_PORT->statStg;
659 *ICOM_PORT->xmitRestart =
660 cpu_to_le32(ICOM_PORT->statStg_pci + offset);
661 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
662 writeb(cmdReg | CMD_XMIT_RCV_ENABLE,
663 &ICOM_PORT->dram->CmdReg);
664 writeb(START_XMIT, &ICOM_PORT->dram->StartXmitCmd);
665 trace(ICOM_PORT, "WRITE_START", data_count);
666 /* write flush */
667 readb(&ICOM_PORT->dram->StartXmitCmd);
668 }
669
670 return data_count;
671}
672
673static inline void check_modem_status(struct icom_port *icom_port)
674{
675 static char old_status = 0;
676 char delta_status;
677 unsigned char status;
678
679 spin_lock(&icom_port->uart_port.lock);
680
681 /*modem input register */
682 status = readb(&icom_port->dram->isr);
683 trace(icom_port, "CHECK_MODEM", status);
684 delta_status = status ^ old_status;
685 if (delta_status) {
686 if (delta_status & ICOM_RI)
687 icom_port->uart_port.icount.rng++;
688 if (delta_status & ICOM_DSR)
689 icom_port->uart_port.icount.dsr++;
690 if (delta_status & ICOM_DCD)
691 uart_handle_dcd_change(&icom_port->uart_port,
692 delta_status & ICOM_DCD);
693 if (delta_status & ICOM_CTS)
694 uart_handle_cts_change(&icom_port->uart_port,
695 delta_status & ICOM_CTS);
696
697 wake_up_interruptible(&icom_port->uart_port.state->
698 port.delta_msr_wait);
699 old_status = status;
700 }
701 spin_unlock(&icom_port->uart_port.lock);
702}
703
704static void xmit_interrupt(u16 port_int_reg, struct icom_port *icom_port)
705{
706 unsigned short int count;
707 int i;
708
709 if (port_int_reg & (INT_XMIT_COMPLETED)) {
710 trace(icom_port, "XMIT_COMPLETE", 0);
711
712 /* clear buffer in use bit */
713 icom_port->statStg->xmit[0].flags &=
714 cpu_to_le16(~SA_FLAGS_READY_TO_XMIT);
715
716 count = (unsigned short int)
717 cpu_to_le16(icom_port->statStg->xmit[0].leLength);
718 icom_port->uart_port.icount.tx += count;
719
720 for (i=0; i<count &&
721 !uart_circ_empty(&icom_port->uart_port.state->xmit); i++) {
722
723 icom_port->uart_port.state->xmit.tail++;
724 icom_port->uart_port.state->xmit.tail &=
725 (UART_XMIT_SIZE - 1);
726 }
727
728 if (!icom_write(&icom_port->uart_port))
729 /* activate write queue */
730 uart_write_wakeup(&icom_port->uart_port);
731 } else
732 trace(icom_port, "XMIT_DISABLED", 0);
733}
734
735static void recv_interrupt(u16 port_int_reg, struct icom_port *icom_port)
736{
737 short int count, rcv_buff;
738 struct tty_struct *tty = icom_port->uart_port.state->port.tty;
739 unsigned short int status;
740 struct uart_icount *icount;
741 unsigned long offset;
742 unsigned char flag;
743
744 trace(icom_port, "RCV_COMPLETE", 0);
745 rcv_buff = icom_port->next_rcv;
746
747 status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags);
748 while (status & SA_FL_RCV_DONE) {
749 int first = -1;
750
751 trace(icom_port, "FID_STATUS", status);
752 count = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].leLength);
753
754 trace(icom_port, "RCV_COUNT", count);
755
756 trace(icom_port, "REAL_COUNT", count);
757
758 offset =
759 cpu_to_le32(icom_port->statStg->rcv[rcv_buff].leBuffer) -
760 icom_port->recv_buf_pci;
761
762 /* Block copy all but the last byte as this may have status */
763 if (count > 0) {
764 first = icom_port->recv_buf[offset];
765 tty_insert_flip_string(tty, icom_port->recv_buf + offset, count - 1);
766 }
767
768 icount = &icom_port->uart_port.icount;
769 icount->rx += count;
770
771 /* Break detect logic */
772 if ((status & SA_FLAGS_FRAME_ERROR)
773 && first == 0) {
774 status &= ~SA_FLAGS_FRAME_ERROR;
775 status |= SA_FLAGS_BREAK_DET;
776 trace(icom_port, "BREAK_DET", 0);
777 }
778
779 flag = TTY_NORMAL;
780
781 if (status &
782 (SA_FLAGS_BREAK_DET | SA_FLAGS_PARITY_ERROR |
783 SA_FLAGS_FRAME_ERROR | SA_FLAGS_OVERRUN)) {
784
785 if (status & SA_FLAGS_BREAK_DET)
786 icount->brk++;
787 if (status & SA_FLAGS_PARITY_ERROR)
788 icount->parity++;
789 if (status & SA_FLAGS_FRAME_ERROR)
790 icount->frame++;
791 if (status & SA_FLAGS_OVERRUN)
792 icount->overrun++;
793
794 /*
795 * Now check to see if character should be
796 * ignored, and mask off conditions which
797 * should be ignored.
798 */
799 if (status & icom_port->ignore_status_mask) {
800 trace(icom_port, "IGNORE_CHAR", 0);
801 goto ignore_char;
802 }
803
804 status &= icom_port->read_status_mask;
805
806 if (status & SA_FLAGS_BREAK_DET) {
807 flag = TTY_BREAK;
808 } else if (status & SA_FLAGS_PARITY_ERROR) {
809 trace(icom_port, "PARITY_ERROR", 0);
810 flag = TTY_PARITY;
811 } else if (status & SA_FLAGS_FRAME_ERROR)
812 flag = TTY_FRAME;
813
814 }
815
816 tty_insert_flip_char(tty, *(icom_port->recv_buf + offset + count - 1), flag);
817
818 if (status & SA_FLAGS_OVERRUN)
819 /*
820 * Overrun is special, since it's
821 * reported immediately, and doesn't
822 * affect the current character
823 */
824 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
825ignore_char:
826 icom_port->statStg->rcv[rcv_buff].flags = 0;
827 icom_port->statStg->rcv[rcv_buff].leLength = 0;
828 icom_port->statStg->rcv[rcv_buff].WorkingLength =
829 (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
830
831 rcv_buff++;
832 if (rcv_buff == NUM_RBUFFS)
833 rcv_buff = 0;
834
835 status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags);
836 }
837 icom_port->next_rcv = rcv_buff;
838 tty_flip_buffer_push(tty);
839}
840
841static void process_interrupt(u16 port_int_reg,
842 struct icom_port *icom_port)
843{
844
845 spin_lock(&icom_port->uart_port.lock);
846 trace(icom_port, "INTERRUPT", port_int_reg);
847
848 if (port_int_reg & (INT_XMIT_COMPLETED | INT_XMIT_DISABLED))
849 xmit_interrupt(port_int_reg, icom_port);
850
851 if (port_int_reg & INT_RCV_COMPLETED)
852 recv_interrupt(port_int_reg, icom_port);
853
854 spin_unlock(&icom_port->uart_port.lock);
855}
856
857static irqreturn_t icom_interrupt(int irq, void *dev_id)
858{
859 void __iomem * int_reg;
860 u32 adapter_interrupts;
861 u16 port_int_reg;
862 struct icom_adapter *icom_adapter;
863 struct icom_port *icom_port;
864
865 /* find icom_port for this interrupt */
866 icom_adapter = (struct icom_adapter *) dev_id;
867
868 if (icom_adapter->version == ADAPTER_V2) {
869 int_reg = icom_adapter->base_addr + 0x8024;
870
871 adapter_interrupts = readl(int_reg);
872
873 if (adapter_interrupts & 0x00003FFF) {
874 /* port 2 interrupt, NOTE: for all ADAPTER_V2, port 2 will be active */
875 icom_port = &icom_adapter->port_info[2];
876 port_int_reg = (u16) adapter_interrupts;
877 process_interrupt(port_int_reg, icom_port);
878 check_modem_status(icom_port);
879 }
880 if (adapter_interrupts & 0x3FFF0000) {
881 /* port 3 interrupt */
882 icom_port = &icom_adapter->port_info[3];
883 if (icom_port->status == ICOM_PORT_ACTIVE) {
884 port_int_reg =
885 (u16) (adapter_interrupts >> 16);
886 process_interrupt(port_int_reg, icom_port);
887 check_modem_status(icom_port);
888 }
889 }
890
891 /* Clear out any pending interrupts */
892 writel(adapter_interrupts, int_reg);
893
894 int_reg = icom_adapter->base_addr + 0x8004;
895 } else {
896 int_reg = icom_adapter->base_addr + 0x4004;
897 }
898
899 adapter_interrupts = readl(int_reg);
900
901 if (adapter_interrupts & 0x00003FFF) {
902 /* port 0 interrupt, NOTE: for all adapters, port 0 will be active */
903 icom_port = &icom_adapter->port_info[0];
904 port_int_reg = (u16) adapter_interrupts;
905 process_interrupt(port_int_reg, icom_port);
906 check_modem_status(icom_port);
907 }
908 if (adapter_interrupts & 0x3FFF0000) {
909 /* port 1 interrupt */
910 icom_port = &icom_adapter->port_info[1];
911 if (icom_port->status == ICOM_PORT_ACTIVE) {
912 port_int_reg = (u16) (adapter_interrupts >> 16);
913 process_interrupt(port_int_reg, icom_port);
914 check_modem_status(icom_port);
915 }
916 }
917
918 /* Clear out any pending interrupts */
919 writel(adapter_interrupts, int_reg);
920
921 /* flush the write */
922 adapter_interrupts = readl(int_reg);
923
924 return IRQ_HANDLED;
925}
926
927/*
928 * ------------------------------------------------------------------
929 * Begin serial-core API
930 * ------------------------------------------------------------------
931 */
932static unsigned int icom_tx_empty(struct uart_port *port)
933{
934 int ret;
935 unsigned long flags;
936
937 spin_lock_irqsave(&port->lock, flags);
938 if (cpu_to_le16(ICOM_PORT->statStg->xmit[0].flags) &
939 SA_FLAGS_READY_TO_XMIT)
940 ret = TIOCSER_TEMT;
941 else
942 ret = 0;
943
944 spin_unlock_irqrestore(&port->lock, flags);
945 return ret;
946}
947
948static void icom_set_mctrl(struct uart_port *port, unsigned int mctrl)
949{
950 unsigned char local_osr;
951
952 trace(ICOM_PORT, "SET_MODEM", 0);
953 local_osr = readb(&ICOM_PORT->dram->osr);
954
955 if (mctrl & TIOCM_RTS) {
956 trace(ICOM_PORT, "RAISE_RTS", 0);
957 local_osr |= ICOM_RTS;
958 } else {
959 trace(ICOM_PORT, "LOWER_RTS", 0);
960 local_osr &= ~ICOM_RTS;
961 }
962
963 if (mctrl & TIOCM_DTR) {
964 trace(ICOM_PORT, "RAISE_DTR", 0);
965 local_osr |= ICOM_DTR;
966 } else {
967 trace(ICOM_PORT, "LOWER_DTR", 0);
968 local_osr &= ~ICOM_DTR;
969 }
970
971 writeb(local_osr, &ICOM_PORT->dram->osr);
972}
973
974static unsigned int icom_get_mctrl(struct uart_port *port)
975{
976 unsigned char status;
977 unsigned int result;
978
979 trace(ICOM_PORT, "GET_MODEM", 0);
980
981 status = readb(&ICOM_PORT->dram->isr);
982
983 result = ((status & ICOM_DCD) ? TIOCM_CAR : 0)
984 | ((status & ICOM_RI) ? TIOCM_RNG : 0)
985 | ((status & ICOM_DSR) ? TIOCM_DSR : 0)
986 | ((status & ICOM_CTS) ? TIOCM_CTS : 0);
987 return result;
988}
989
990static void icom_stop_tx(struct uart_port *port)
991{
992 unsigned char cmdReg;
993
994 trace(ICOM_PORT, "STOP", 0);
995 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
996 writeb(cmdReg | CMD_HOLD_XMIT, &ICOM_PORT->dram->CmdReg);
997}
998
999static void icom_start_tx(struct uart_port *port)
1000{
1001 unsigned char cmdReg;
1002
1003 trace(ICOM_PORT, "START", 0);
1004 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1005 if ((cmdReg & CMD_HOLD_XMIT) == CMD_HOLD_XMIT)
1006 writeb(cmdReg & ~CMD_HOLD_XMIT,
1007 &ICOM_PORT->dram->CmdReg);
1008
1009 icom_write(port);
1010}
1011
1012static void icom_send_xchar(struct uart_port *port, char ch)
1013{
1014 unsigned char xdata;
1015 int index;
1016 unsigned long flags;
1017
1018 trace(ICOM_PORT, "SEND_XCHAR", ch);
1019
1020 /* wait .1 sec to send char */
1021 for (index = 0; index < 10; index++) {
1022 spin_lock_irqsave(&port->lock, flags);
1023 xdata = readb(&ICOM_PORT->dram->xchar);
1024 if (xdata == 0x00) {
1025 trace(ICOM_PORT, "QUICK_WRITE", 0);
1026 writeb(ch, &ICOM_PORT->dram->xchar);
1027
1028 /* flush write operation */
1029 xdata = readb(&ICOM_PORT->dram->xchar);
1030 spin_unlock_irqrestore(&port->lock, flags);
1031 break;
1032 }
1033 spin_unlock_irqrestore(&port->lock, flags);
1034 msleep(10);
1035 }
1036}
1037
1038static void icom_stop_rx(struct uart_port *port)
1039{
1040 unsigned char cmdReg;
1041
1042 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1043 writeb(cmdReg & ~CMD_RCV_ENABLE, &ICOM_PORT->dram->CmdReg);
1044}
1045
1046static void icom_enable_ms(struct uart_port *port)
1047{
1048 /* no-op */
1049}
1050
1051static void icom_break(struct uart_port *port, int break_state)
1052{
1053 unsigned char cmdReg;
1054 unsigned long flags;
1055
1056 spin_lock_irqsave(&port->lock, flags);
1057 trace(ICOM_PORT, "BREAK", 0);
1058 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1059 if (break_state == -1) {
1060 writeb(cmdReg | CMD_SND_BREAK, &ICOM_PORT->dram->CmdReg);
1061 } else {
1062 writeb(cmdReg & ~CMD_SND_BREAK, &ICOM_PORT->dram->CmdReg);
1063 }
1064 spin_unlock_irqrestore(&port->lock, flags);
1065}
1066
1067static int icom_open(struct uart_port *port)
1068{
1069 int retval;
1070
1071 kref_get(&ICOM_PORT->adapter->kref);
1072 retval = startup(ICOM_PORT);
1073
1074 if (retval) {
1075 kref_put(&ICOM_PORT->adapter->kref, icom_kref_release);
1076 trace(ICOM_PORT, "STARTUP_ERROR", 0);
1077 return retval;
1078 }
1079
1080 return 0;
1081}
1082
1083static void icom_close(struct uart_port *port)
1084{
1085 unsigned char cmdReg;
1086
1087 trace(ICOM_PORT, "CLOSE", 0);
1088
1089 /* stop receiver */
1090 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1091 writeb(cmdReg & (unsigned char) ~CMD_RCV_ENABLE,
1092 &ICOM_PORT->dram->CmdReg);
1093
1094 shutdown(ICOM_PORT);
1095
1096 kref_put(&ICOM_PORT->adapter->kref, icom_kref_release);
1097}
1098
1099static void icom_set_termios(struct uart_port *port,
1100 struct ktermios *termios,
1101 struct ktermios *old_termios)
1102{
1103 int baud;
1104 unsigned cflag, iflag;
1105 char new_config2;
1106 char new_config3 = 0;
1107 char tmp_byte;
1108 int index;
1109 int rcv_buff, xmit_buff;
1110 unsigned long offset;
1111 unsigned long flags;
1112
1113 spin_lock_irqsave(&port->lock, flags);
1114 trace(ICOM_PORT, "CHANGE_SPEED", 0);
1115
1116 cflag = termios->c_cflag;
1117 iflag = termios->c_iflag;
1118
1119 new_config2 = ICOM_ACFG_DRIVE1;
1120
1121 /* byte size and parity */
1122 switch (cflag & CSIZE) {
1123 case CS5: /* 5 bits/char */
1124 new_config2 |= ICOM_ACFG_5BPC;
1125 break;
1126 case CS6: /* 6 bits/char */
1127 new_config2 |= ICOM_ACFG_6BPC;
1128 break;
1129 case CS7: /* 7 bits/char */
1130 new_config2 |= ICOM_ACFG_7BPC;
1131 break;
1132 case CS8: /* 8 bits/char */
1133 new_config2 |= ICOM_ACFG_8BPC;
1134 break;
1135 default:
1136 break;
1137 }
1138 if (cflag & CSTOPB) {
1139 /* 2 stop bits */
1140 new_config2 |= ICOM_ACFG_2STOP_BIT;
1141 }
1142 if (cflag & PARENB) {
1143 /* parity bit enabled */
1144 new_config2 |= ICOM_ACFG_PARITY_ENAB;
1145 trace(ICOM_PORT, "PARENB", 0);
1146 }
1147 if (cflag & PARODD) {
1148 /* odd parity */
1149 new_config2 |= ICOM_ACFG_PARITY_ODD;
1150 trace(ICOM_PORT, "PARODD", 0);
1151 }
1152
1153 /* Determine divisor based on baud rate */
1154 baud = uart_get_baud_rate(port, termios, old_termios,
1155 icom_acfg_baud[0],
1156 icom_acfg_baud[BAUD_TABLE_LIMIT]);
1157 if (!baud)
1158 baud = 9600; /* B0 transition handled in rs_set_termios */
1159
1160 for (index = 0; index < BAUD_TABLE_LIMIT; index++) {
1161 if (icom_acfg_baud[index] == baud) {
1162 new_config3 = index;
1163 break;
1164 }
1165 }
1166
1167 uart_update_timeout(port, cflag, baud);
1168
1169 /* CTS flow control flag and modem status interrupts */
1170 tmp_byte = readb(&(ICOM_PORT->dram->HDLCConfigReg));
1171 if (cflag & CRTSCTS)
1172 tmp_byte |= HDLC_HDW_FLOW;
1173 else
1174 tmp_byte &= ~HDLC_HDW_FLOW;
1175 writeb(tmp_byte, &(ICOM_PORT->dram->HDLCConfigReg));
1176
1177 /*
1178 * Set up parity check flag
1179 */
1180 ICOM_PORT->read_status_mask = SA_FLAGS_OVERRUN | SA_FL_RCV_DONE;
1181 if (iflag & INPCK)
1182 ICOM_PORT->read_status_mask |=
1183 SA_FLAGS_FRAME_ERROR | SA_FLAGS_PARITY_ERROR;
1184
1185 if ((iflag & BRKINT) || (iflag & PARMRK))
1186 ICOM_PORT->read_status_mask |= SA_FLAGS_BREAK_DET;
1187
1188 /*
1189 * Characters to ignore
1190 */
1191 ICOM_PORT->ignore_status_mask = 0;
1192 if (iflag & IGNPAR)
1193 ICOM_PORT->ignore_status_mask |=
1194 SA_FLAGS_PARITY_ERROR | SA_FLAGS_FRAME_ERROR;
1195 if (iflag & IGNBRK) {
1196 ICOM_PORT->ignore_status_mask |= SA_FLAGS_BREAK_DET;
1197 /*
1198 * If we're ignore parity and break indicators, ignore
1199 * overruns too. (For real raw support).
1200 */
1201 if (iflag & IGNPAR)
1202 ICOM_PORT->ignore_status_mask |= SA_FLAGS_OVERRUN;
1203 }
1204
1205 /*
1206 * !!! ignore all characters if CREAD is not set
1207 */
1208 if ((cflag & CREAD) == 0)
1209 ICOM_PORT->ignore_status_mask |= SA_FL_RCV_DONE;
1210
1211 /* Turn off Receiver to prepare for reset */
1212 writeb(CMD_RCV_DISABLE, &ICOM_PORT->dram->CmdReg);
1213
1214 for (index = 0; index < 10; index++) {
1215 if (readb(&ICOM_PORT->dram->PrevCmdReg) == 0x00) {
1216 break;
1217 }
1218 }
1219
1220 /* clear all current buffers of data */
1221 for (rcv_buff = 0; rcv_buff < NUM_RBUFFS; rcv_buff++) {
1222 ICOM_PORT->statStg->rcv[rcv_buff].flags = 0;
1223 ICOM_PORT->statStg->rcv[rcv_buff].leLength = 0;
1224 ICOM_PORT->statStg->rcv[rcv_buff].WorkingLength =
1225 (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
1226 }
1227
1228 for (xmit_buff = 0; xmit_buff < NUM_XBUFFS; xmit_buff++) {
1229 ICOM_PORT->statStg->xmit[xmit_buff].flags = 0;
1230 }
1231
1232 /* activate changes and start xmit and receiver here */
1233 /* Enable the receiver */
1234 writeb(new_config3, &(ICOM_PORT->dram->async_config3));
1235 writeb(new_config2, &(ICOM_PORT->dram->async_config2));
1236 tmp_byte = readb(&(ICOM_PORT->dram->HDLCConfigReg));
1237 tmp_byte |= HDLC_PPP_PURE_ASYNC | HDLC_FF_FILL;
1238 writeb(tmp_byte, &(ICOM_PORT->dram->HDLCConfigReg));
1239 writeb(0x04, &(ICOM_PORT->dram->FlagFillIdleTimer)); /* 0.5 seconds */
1240 writeb(0xFF, &(ICOM_PORT->dram->ier)); /* enable modem signal interrupts */
1241
1242 /* reset processor */
1243 writeb(CMD_RESTART, &ICOM_PORT->dram->CmdReg);
1244
1245 for (index = 0; index < 10; index++) {
1246 if (readb(&ICOM_PORT->dram->CmdReg) == 0x00) {
1247 break;
1248 }
1249 }
1250
1251 /* Enable Transmitter and Reciever */
1252 offset =
1253 (unsigned long) &ICOM_PORT->statStg->rcv[0] -
1254 (unsigned long) ICOM_PORT->statStg;
1255 writel(ICOM_PORT->statStg_pci + offset,
1256 &ICOM_PORT->dram->RcvStatusAddr);
1257 ICOM_PORT->next_rcv = 0;
1258 ICOM_PORT->put_length = 0;
1259 *ICOM_PORT->xmitRestart = 0;
1260 writel(ICOM_PORT->xmitRestart_pci,
1261 &ICOM_PORT->dram->XmitStatusAddr);
1262 trace(ICOM_PORT, "XR_ENAB", 0);
1263 writeb(CMD_XMIT_RCV_ENABLE, &ICOM_PORT->dram->CmdReg);
1264
1265 spin_unlock_irqrestore(&port->lock, flags);
1266}
1267
1268static const char *icom_type(struct uart_port *port)
1269{
1270 return "icom";
1271}
1272
1273static void icom_release_port(struct uart_port *port)
1274{
1275}
1276
1277static int icom_request_port(struct uart_port *port)
1278{
1279 return 0;
1280}
1281
1282static void icom_config_port(struct uart_port *port, int flags)
1283{
1284 port->type = PORT_ICOM;
1285}
1286
1287static struct uart_ops icom_ops = {
1288 .tx_empty = icom_tx_empty,
1289 .set_mctrl = icom_set_mctrl,
1290 .get_mctrl = icom_get_mctrl,
1291 .stop_tx = icom_stop_tx,
1292 .start_tx = icom_start_tx,
1293 .send_xchar = icom_send_xchar,
1294 .stop_rx = icom_stop_rx,
1295 .enable_ms = icom_enable_ms,
1296 .break_ctl = icom_break,
1297 .startup = icom_open,
1298 .shutdown = icom_close,
1299 .set_termios = icom_set_termios,
1300 .type = icom_type,
1301 .release_port = icom_release_port,
1302 .request_port = icom_request_port,
1303 .config_port = icom_config_port,
1304};
1305
1306#define ICOM_CONSOLE NULL
1307
1308static struct uart_driver icom_uart_driver = {
1309 .owner = THIS_MODULE,
1310 .driver_name = ICOM_DRIVER_NAME,
1311 .dev_name = "ttyA",
1312 .major = ICOM_MAJOR,
1313 .minor = ICOM_MINOR_START,
1314 .nr = NR_PORTS,
1315 .cons = ICOM_CONSOLE,
1316};
1317
1318static int __devinit icom_init_ports(struct icom_adapter *icom_adapter)
1319{
1320 u32 subsystem_id = icom_adapter->subsystem_id;
1321 int i;
1322 struct icom_port *icom_port;
1323
1324 if (icom_adapter->version == ADAPTER_V1) {
1325 icom_adapter->numb_ports = 2;
1326
1327 for (i = 0; i < 2; i++) {
1328 icom_port = &icom_adapter->port_info[i];
1329 icom_port->port = i;
1330 icom_port->status = ICOM_PORT_ACTIVE;
1331 icom_port->imbed_modem = ICOM_UNKNOWN;
1332 }
1333 } else {
1334 if (subsystem_id == PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL) {
1335 icom_adapter->numb_ports = 4;
1336
1337 for (i = 0; i < 4; i++) {
1338 icom_port = &icom_adapter->port_info[i];
1339
1340 icom_port->port = i;
1341 icom_port->status = ICOM_PORT_ACTIVE;
1342 icom_port->imbed_modem = ICOM_IMBED_MODEM;
1343 }
1344 } else {
1345 icom_adapter->numb_ports = 4;
1346
1347 icom_adapter->port_info[0].port = 0;
1348 icom_adapter->port_info[0].status = ICOM_PORT_ACTIVE;
1349
1350 if (subsystem_id ==
1351 PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM) {
1352 icom_adapter->port_info[0].imbed_modem = ICOM_IMBED_MODEM;
1353 } else {
1354 icom_adapter->port_info[0].imbed_modem = ICOM_RVX;
1355 }
1356
1357 icom_adapter->port_info[1].status = ICOM_PORT_OFF;
1358
1359 icom_adapter->port_info[2].port = 2;
1360 icom_adapter->port_info[2].status = ICOM_PORT_ACTIVE;
1361 icom_adapter->port_info[2].imbed_modem = ICOM_RVX;
1362 icom_adapter->port_info[3].status = ICOM_PORT_OFF;
1363 }
1364 }
1365
1366 return 0;
1367}
1368
1369static void icom_port_active(struct icom_port *icom_port, struct icom_adapter *icom_adapter, int port_num)
1370{
1371 if (icom_adapter->version == ADAPTER_V1) {
1372 icom_port->global_reg = icom_adapter->base_addr + 0x4000;
1373 icom_port->int_reg = icom_adapter->base_addr +
1374 0x4004 + 2 - 2 * port_num;
1375 } else {
1376 icom_port->global_reg = icom_adapter->base_addr + 0x8000;
1377 if (icom_port->port < 2)
1378 icom_port->int_reg = icom_adapter->base_addr +
1379 0x8004 + 2 - 2 * icom_port->port;
1380 else
1381 icom_port->int_reg = icom_adapter->base_addr +
1382 0x8024 + 2 - 2 * (icom_port->port - 2);
1383 }
1384}
1385static int __devinit icom_load_ports(struct icom_adapter *icom_adapter)
1386{
1387 struct icom_port *icom_port;
1388 int port_num;
1389
1390 for (port_num = 0; port_num < icom_adapter->numb_ports; port_num++) {
1391
1392 icom_port = &icom_adapter->port_info[port_num];
1393
1394 if (icom_port->status == ICOM_PORT_ACTIVE) {
1395 icom_port_active(icom_port, icom_adapter, port_num);
1396 icom_port->dram = icom_adapter->base_addr +
1397 0x2000 * icom_port->port;
1398
1399 icom_port->adapter = icom_adapter;
1400
1401 /* get port memory */
1402 if (get_port_memory(icom_port) != 0) {
1403 dev_err(&icom_port->adapter->pci_dev->dev,
1404 "Memory allocation for port FAILED\n");
1405 }
1406 }
1407 }
1408 return 0;
1409}
1410
1411static int __devinit icom_alloc_adapter(struct icom_adapter
1412 **icom_adapter_ref)
1413{
1414 int adapter_count = 0;
1415 struct icom_adapter *icom_adapter;
1416 struct icom_adapter *cur_adapter_entry;
1417 struct list_head *tmp;
1418
1419 icom_adapter = (struct icom_adapter *)
1420 kzalloc(sizeof(struct icom_adapter), GFP_KERNEL);
1421
1422 if (!icom_adapter) {
1423 return -ENOMEM;
1424 }
1425
1426 list_for_each(tmp, &icom_adapter_head) {
1427 cur_adapter_entry =
1428 list_entry(tmp, struct icom_adapter,
1429 icom_adapter_entry);
1430 if (cur_adapter_entry->index != adapter_count) {
1431 break;
1432 }
1433 adapter_count++;
1434 }
1435
1436 icom_adapter->index = adapter_count;
1437 list_add_tail(&icom_adapter->icom_adapter_entry, tmp);
1438
1439 *icom_adapter_ref = icom_adapter;
1440 return 0;
1441}
1442
1443static void icom_free_adapter(struct icom_adapter *icom_adapter)
1444{
1445 list_del(&icom_adapter->icom_adapter_entry);
1446 kfree(icom_adapter);
1447}
1448
1449static void icom_remove_adapter(struct icom_adapter *icom_adapter)
1450{
1451 struct icom_port *icom_port;
1452 int index;
1453
1454 for (index = 0; index < icom_adapter->numb_ports; index++) {
1455 icom_port = &icom_adapter->port_info[index];
1456
1457 if (icom_port->status == ICOM_PORT_ACTIVE) {
1458 dev_info(&icom_adapter->pci_dev->dev,
1459 "Device removed\n");
1460
1461 uart_remove_one_port(&icom_uart_driver,
1462 &icom_port->uart_port);
1463
1464 /* be sure that DTR and RTS are dropped */
1465 writeb(0x00, &icom_port->dram->osr);
1466
1467 /* Wait 0.1 Sec for simple Init to complete */
1468 msleep(100);
1469
1470 /* Stop proccessor */
1471 stop_processor(icom_port);
1472
1473 free_port_memory(icom_port);
1474 }
1475 }
1476
1477 free_irq(icom_adapter->pci_dev->irq, (void *) icom_adapter);
1478 iounmap(icom_adapter->base_addr);
1479 pci_release_regions(icom_adapter->pci_dev);
1480 icom_free_adapter(icom_adapter);
1481}
1482
1483static void icom_kref_release(struct kref *kref)
1484{
1485 struct icom_adapter *icom_adapter;
1486
1487 icom_adapter = to_icom_adapter(kref);
1488 icom_remove_adapter(icom_adapter);
1489}
1490
1491static int __devinit icom_probe(struct pci_dev *dev,
1492 const struct pci_device_id *ent)
1493{
1494 int index;
1495 unsigned int command_reg;
1496 int retval;
1497 struct icom_adapter *icom_adapter;
1498 struct icom_port *icom_port;
1499
1500 retval = pci_enable_device(dev);
1501 if (retval) {
1502 dev_err(&dev->dev, "Device enable FAILED\n");
1503 return retval;
1504 }
1505
1506 if ( (retval = pci_request_regions(dev, "icom"))) {
1507 dev_err(&dev->dev, "pci_request_regions FAILED\n");
1508 pci_disable_device(dev);
1509 return retval;
1510 }
1511
1512 pci_set_master(dev);
1513
1514 if ( (retval = pci_read_config_dword(dev, PCI_COMMAND, &command_reg))) {
1515 dev_err(&dev->dev, "PCI Config read FAILED\n");
1516 return retval;
1517 }
1518
1519 pci_write_config_dword(dev, PCI_COMMAND,
1520 command_reg | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1521 | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1522
1523 if (ent->driver_data == ADAPTER_V1) {
1524 pci_write_config_dword(dev, 0x44, 0x8300830A);
1525 } else {
1526 pci_write_config_dword(dev, 0x44, 0x42004200);
1527 pci_write_config_dword(dev, 0x48, 0x42004200);
1528 }
1529
1530
1531 retval = icom_alloc_adapter(&icom_adapter);
1532 if (retval) {
1533 dev_err(&dev->dev, "icom_alloc_adapter FAILED\n");
1534 retval = -EIO;
1535 goto probe_exit0;
1536 }
1537
1538 icom_adapter->base_addr_pci = pci_resource_start(dev, 0);
1539 icom_adapter->pci_dev = dev;
1540 icom_adapter->version = ent->driver_data;
1541 icom_adapter->subsystem_id = ent->subdevice;
1542
1543
1544 retval = icom_init_ports(icom_adapter);
1545 if (retval) {
1546 dev_err(&dev->dev, "Port configuration failed\n");
1547 goto probe_exit1;
1548 }
1549
1550 icom_adapter->base_addr = pci_ioremap_bar(dev, 0);
1551
1552 if (!icom_adapter->base_addr)
1553 goto probe_exit1;
1554
1555 /* save off irq and request irq line */
1556 if ( (retval = request_irq(dev->irq, icom_interrupt,
1557 IRQF_DISABLED | IRQF_SHARED, ICOM_DRIVER_NAME,
1558 (void *) icom_adapter))) {
1559 goto probe_exit2;
1560 }
1561
1562 retval = icom_load_ports(icom_adapter);
1563
1564 for (index = 0; index < icom_adapter->numb_ports; index++) {
1565 icom_port = &icom_adapter->port_info[index];
1566
1567 if (icom_port->status == ICOM_PORT_ACTIVE) {
1568 icom_port->uart_port.irq = icom_port->adapter->pci_dev->irq;
1569 icom_port->uart_port.type = PORT_ICOM;
1570 icom_port->uart_port.iotype = UPIO_MEM;
1571 icom_port->uart_port.membase =
1572 (char *) icom_adapter->base_addr_pci;
1573 icom_port->uart_port.fifosize = 16;
1574 icom_port->uart_port.ops = &icom_ops;
1575 icom_port->uart_port.line =
1576 icom_port->port + icom_adapter->index * 4;
1577 if (uart_add_one_port (&icom_uart_driver, &icom_port->uart_port)) {
1578 icom_port->status = ICOM_PORT_OFF;
1579 dev_err(&dev->dev, "Device add failed\n");
1580 } else
1581 dev_info(&dev->dev, "Device added\n");
1582 }
1583 }
1584
1585 kref_init(&icom_adapter->kref);
1586 return 0;
1587
1588probe_exit2:
1589 iounmap(icom_adapter->base_addr);
1590probe_exit1:
1591 icom_free_adapter(icom_adapter);
1592
1593probe_exit0:
1594 pci_release_regions(dev);
1595 pci_disable_device(dev);
1596
1597 return retval;
1598}
1599
1600static void __devexit icom_remove(struct pci_dev *dev)
1601{
1602 struct icom_adapter *icom_adapter;
1603 struct list_head *tmp;
1604
1605 list_for_each(tmp, &icom_adapter_head) {
1606 icom_adapter = list_entry(tmp, struct icom_adapter,
1607 icom_adapter_entry);
1608 if (icom_adapter->pci_dev == dev) {
1609 kref_put(&icom_adapter->kref, icom_kref_release);
1610 return;
1611 }
1612 }
1613
1614 dev_err(&dev->dev, "Unable to find device to remove\n");
1615}
1616
1617static struct pci_driver icom_pci_driver = {
1618 .name = ICOM_DRIVER_NAME,
1619 .id_table = icom_pci_table,
1620 .probe = icom_probe,
1621 .remove = __devexit_p(icom_remove),
1622};
1623
1624static int __init icom_init(void)
1625{
1626 int ret;
1627
1628 spin_lock_init(&icom_lock);
1629
1630 ret = uart_register_driver(&icom_uart_driver);
1631 if (ret)
1632 return ret;
1633
1634 ret = pci_register_driver(&icom_pci_driver);
1635
1636 if (ret < 0)
1637 uart_unregister_driver(&icom_uart_driver);
1638
1639 return ret;
1640}
1641
1642static void __exit icom_exit(void)
1643{
1644 pci_unregister_driver(&icom_pci_driver);
1645 uart_unregister_driver(&icom_uart_driver);
1646}
1647
1648module_init(icom_init);
1649module_exit(icom_exit);
1650
1651MODULE_AUTHOR("Michael Anderson <mjanders@us.ibm.com>");
1652MODULE_DESCRIPTION("IBM iSeries Serial IOA driver");
1653MODULE_SUPPORTED_DEVICE
1654 ("IBM iSeries 2745, 2771, 2772, 2742, 2793 and 2805 Communications adapters");
1655MODULE_LICENSE("GPL");
1656MODULE_FIRMWARE("icom_call_setup.bin");
1657MODULE_FIRMWARE("icom_res_dce.bin");
1658MODULE_FIRMWARE("icom_asc.bin");
diff --git a/drivers/tty/serial/icom.h b/drivers/tty/serial/icom.h
new file mode 100644
index 000000000000..c8029e0025c9
--- /dev/null
+++ b/drivers/tty/serial/icom.h
@@ -0,0 +1,287 @@
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 struct pci_dev *pci_dev;
262 struct icom_port port_info[4];
263 int index;
264 int version;
265#define ADAPTER_V1 0x0001
266#define ADAPTER_V2 0x0002
267 u32 subsystem_id;
268#define FOUR_PORT_MODEL 0x0252
269#define V2_TWO_PORTS_RVX 0x021A
270#define V2_ONE_PORT_RVX_ONE_PORT_IMBED_MDM 0x0251
271 int numb_ports;
272 struct list_head icom_adapter_entry;
273 struct kref kref;
274};
275
276/* prototype */
277extern void iCom_sercons_init(void);
278
279struct lookup_proc_table {
280 u32 __iomem *global_control_reg;
281 unsigned long processor_id;
282};
283
284struct lookup_int_table {
285 u32 __iomem *global_int_mask;
286 unsigned long processor_id;
287};
diff --git a/drivers/tty/serial/ifx6x60.c b/drivers/tty/serial/ifx6x60.c
new file mode 100644
index 000000000000..ab93763862d5
--- /dev/null
+++ b/drivers/tty/serial/ifx6x60.c
@@ -0,0 +1,1406 @@
1/****************************************************************************
2 *
3 * Driver for the IFX 6x60 spi modem.
4 *
5 * Copyright (C) 2008 Option International
6 * Copyright (C) 2008 Filip Aben <f.aben@option.com>
7 * Denis Joseph Barrow <d.barow@option.com>
8 * Jan Dumon <j.dumon@option.com>
9 *
10 * Copyright (C) 2009, 2010 Intel Corp
11 * Russ Gorby <richardx.r.gorby@intel.com>
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 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 * USA
26 *
27 * Driver modified by Intel from Option gtm501l_spi.c
28 *
29 * Notes
30 * o The driver currently assumes a single device only. If you need to
31 * change this then look for saved_ifx_dev and add a device lookup
32 * o The driver is intended to be big-endian safe but has never been
33 * tested that way (no suitable hardware). There are a couple of FIXME
34 * notes by areas that may need addressing
35 * o Some of the GPIO naming/setup assumptions may need revisiting if
36 * you need to use this driver for another platform.
37 *
38 *****************************************************************************/
39#include <linux/module.h>
40#include <linux/termios.h>
41#include <linux/tty.h>
42#include <linux/device.h>
43#include <linux/spi/spi.h>
44#include <linux/tty.h>
45#include <linux/kfifo.h>
46#include <linux/tty_flip.h>
47#include <linux/timer.h>
48#include <linux/serial.h>
49#include <linux/interrupt.h>
50#include <linux/irq.h>
51#include <linux/rfkill.h>
52#include <linux/fs.h>
53#include <linux/ip.h>
54#include <linux/dmapool.h>
55#include <linux/gpio.h>
56#include <linux/sched.h>
57#include <linux/time.h>
58#include <linux/wait.h>
59#include <linux/tty.h>
60#include <linux/pm.h>
61#include <linux/pm_runtime.h>
62#include <linux/spi/ifx_modem.h>
63#include <linux/delay.h>
64
65#include "ifx6x60.h"
66
67#define IFX_SPI_MORE_MASK 0x10
68#define IFX_SPI_MORE_BIT 12 /* bit position in u16 */
69#define IFX_SPI_CTS_BIT 13 /* bit position in u16 */
70#define IFX_SPI_TTY_ID 0
71#define IFX_SPI_TIMEOUT_SEC 2
72#define IFX_SPI_HEADER_0 (-1)
73#define IFX_SPI_HEADER_F (-2)
74
75/* forward reference */
76static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev);
77
78/* local variables */
79static int spi_b16 = 1; /* 8 or 16 bit word length */
80static struct tty_driver *tty_drv;
81static struct ifx_spi_device *saved_ifx_dev;
82static struct lock_class_key ifx_spi_key;
83
84/* GPIO/GPE settings */
85
86/**
87 * mrdy_set_high - set MRDY GPIO
88 * @ifx: device we are controlling
89 *
90 */
91static inline void mrdy_set_high(struct ifx_spi_device *ifx)
92{
93 gpio_set_value(ifx->gpio.mrdy, 1);
94}
95
96/**
97 * mrdy_set_low - clear MRDY GPIO
98 * @ifx: device we are controlling
99 *
100 */
101static inline void mrdy_set_low(struct ifx_spi_device *ifx)
102{
103 gpio_set_value(ifx->gpio.mrdy, 0);
104}
105
106/**
107 * ifx_spi_power_state_set
108 * @ifx_dev: our SPI device
109 * @val: bits to set
110 *
111 * Set bit in power status and signal power system if status becomes non-0
112 */
113static void
114ifx_spi_power_state_set(struct ifx_spi_device *ifx_dev, unsigned char val)
115{
116 unsigned long flags;
117
118 spin_lock_irqsave(&ifx_dev->power_lock, flags);
119
120 /*
121 * if power status is already non-0, just update, else
122 * tell power system
123 */
124 if (!ifx_dev->power_status)
125 pm_runtime_get(&ifx_dev->spi_dev->dev);
126 ifx_dev->power_status |= val;
127
128 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
129}
130
131/**
132 * ifx_spi_power_state_clear - clear power bit
133 * @ifx_dev: our SPI device
134 * @val: bits to clear
135 *
136 * clear bit in power status and signal power system if status becomes 0
137 */
138static void
139ifx_spi_power_state_clear(struct ifx_spi_device *ifx_dev, unsigned char val)
140{
141 unsigned long flags;
142
143 spin_lock_irqsave(&ifx_dev->power_lock, flags);
144
145 if (ifx_dev->power_status) {
146 ifx_dev->power_status &= ~val;
147 if (!ifx_dev->power_status)
148 pm_runtime_put(&ifx_dev->spi_dev->dev);
149 }
150
151 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
152}
153
154/**
155 * swap_buf
156 * @buf: our buffer
157 * @len : number of bytes (not words) in the buffer
158 * @end: end of buffer
159 *
160 * Swap the contents of a buffer into big endian format
161 */
162static inline void swap_buf(u16 *buf, int len, void *end)
163{
164 int n;
165
166 len = ((len + 1) >> 1);
167 if ((void *)&buf[len] > end) {
168 pr_err("swap_buf: swap exceeds boundary (%p > %p)!",
169 &buf[len], end);
170 return;
171 }
172 for (n = 0; n < len; n++) {
173 *buf = cpu_to_be16(*buf);
174 buf++;
175 }
176}
177
178/**
179 * mrdy_assert - assert MRDY line
180 * @ifx_dev: our SPI device
181 *
182 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
183 * now.
184 *
185 * FIXME: Can SRDY even go high as we are running this code ?
186 */
187static void mrdy_assert(struct ifx_spi_device *ifx_dev)
188{
189 int val = gpio_get_value(ifx_dev->gpio.srdy);
190 if (!val) {
191 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING,
192 &ifx_dev->flags)) {
193 ifx_dev->spi_timer.expires =
194 jiffies + IFX_SPI_TIMEOUT_SEC*HZ;
195 add_timer(&ifx_dev->spi_timer);
196
197 }
198 }
199 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING);
200 mrdy_set_high(ifx_dev);
201}
202
203/**
204 * ifx_spi_hangup - hang up an IFX device
205 * @ifx_dev: our SPI device
206 *
207 * Hang up the tty attached to the IFX device if one is currently
208 * open. If not take no action
209 */
210static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev)
211{
212 struct tty_port *pport = &ifx_dev->tty_port;
213 struct tty_struct *tty = tty_port_tty_get(pport);
214 if (tty) {
215 tty_hangup(tty);
216 tty_kref_put(tty);
217 }
218}
219
220/**
221 * ifx_spi_timeout - SPI timeout
222 * @arg: our SPI device
223 *
224 * The SPI has timed out: hang up the tty. Users will then see a hangup
225 * and error events.
226 */
227static void ifx_spi_timeout(unsigned long arg)
228{
229 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg;
230
231 dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***");
232 ifx_spi_ttyhangup(ifx_dev);
233 mrdy_set_low(ifx_dev);
234 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
235}
236
237/* char/tty operations */
238
239/**
240 * ifx_spi_tiocmget - get modem lines
241 * @tty: our tty device
242 * @filp: file handle issuing the request
243 *
244 * Map the signal state into Linux modem flags and report the value
245 * in Linux terms
246 */
247static int ifx_spi_tiocmget(struct tty_struct *tty, struct file *filp)
248{
249 unsigned int value;
250 struct ifx_spi_device *ifx_dev = tty->driver_data;
251
252 value =
253 (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) |
254 (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) |
255 (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) |
256 (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) |
257 (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) |
258 (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0);
259 return value;
260}
261
262/**
263 * ifx_spi_tiocmset - set modem bits
264 * @tty: the tty structure
265 * @filp: file handle issuing the request
266 * @set: bits to set
267 * @clear: bits to clear
268 *
269 * The IFX6x60 only supports DTR and RTS. Set them accordingly
270 * and flag that an update to the modem is needed.
271 *
272 * FIXME: do we need to kick the tranfers when we do this ?
273 */
274static int ifx_spi_tiocmset(struct tty_struct *tty, struct file *filp,
275 unsigned int set, unsigned int clear)
276{
277 struct ifx_spi_device *ifx_dev = tty->driver_data;
278
279 if (set & TIOCM_RTS)
280 set_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
281 if (set & TIOCM_DTR)
282 set_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
283 if (clear & TIOCM_RTS)
284 clear_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
285 if (clear & TIOCM_DTR)
286 clear_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
287
288 set_bit(IFX_SPI_UPDATE, &ifx_dev->signal_state);
289 return 0;
290}
291
292/**
293 * ifx_spi_open - called on tty open
294 * @tty: our tty device
295 * @filp: file handle being associated with the tty
296 *
297 * Open the tty interface. We let the tty_port layer do all the work
298 * for us.
299 *
300 * FIXME: Remove single device assumption and saved_ifx_dev
301 */
302static int ifx_spi_open(struct tty_struct *tty, struct file *filp)
303{
304 return tty_port_open(&saved_ifx_dev->tty_port, tty, filp);
305}
306
307/**
308 * ifx_spi_close - called when our tty closes
309 * @tty: the tty being closed
310 * @filp: the file handle being closed
311 *
312 * Perform the close of the tty. We use the tty_port layer to do all
313 * our hard work.
314 */
315static void ifx_spi_close(struct tty_struct *tty, struct file *filp)
316{
317 struct ifx_spi_device *ifx_dev = tty->driver_data;
318 tty_port_close(&ifx_dev->tty_port, tty, filp);
319 /* FIXME: should we do an ifx_spi_reset here ? */
320}
321
322/**
323 * ifx_decode_spi_header - decode received header
324 * @buffer: the received data
325 * @length: decoded length
326 * @more: decoded more flag
327 * @received_cts: status of cts we received
328 *
329 * Note how received_cts is handled -- if header is all F it is left
330 * the same as it was, if header is all 0 it is set to 0 otherwise it is
331 * taken from the incoming header.
332 *
333 * FIXME: endianness
334 */
335static int ifx_spi_decode_spi_header(unsigned char *buffer, int *length,
336 unsigned char *more, unsigned char *received_cts)
337{
338 u16 h1;
339 u16 h2;
340 u16 *in_buffer = (u16 *)buffer;
341
342 h1 = *in_buffer;
343 h2 = *(in_buffer+1);
344
345 if (h1 == 0 && h2 == 0) {
346 *received_cts = 0;
347 return IFX_SPI_HEADER_0;
348 } else if (h1 == 0xffff && h2 == 0xffff) {
349 /* spi_slave_cts remains as it was */
350 return IFX_SPI_HEADER_F;
351 }
352
353 *length = h1 & 0xfff; /* upper bits of byte are flags */
354 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
355 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
356 return 0;
357}
358
359/**
360 * ifx_setup_spi_header - set header fields
361 * @txbuffer: pointer to start of SPI buffer
362 * @tx_count: bytes
363 * @more: indicate if more to follow
364 *
365 * Format up an SPI header for a transfer
366 *
367 * FIXME: endianness?
368 */
369static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
370 unsigned char more)
371{
372 *(u16 *)(txbuffer) = tx_count;
373 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
374 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
375}
376
377/**
378 * ifx_spi_wakeup_serial - SPI space made
379 * @port_data: our SPI device
380 *
381 * We have emptied the FIFO enough that we want to get more data
382 * queued into it. Poke the line discipline via tty_wakeup so that
383 * it will feed us more bits
384 */
385static void ifx_spi_wakeup_serial(struct ifx_spi_device *ifx_dev)
386{
387 struct tty_struct *tty;
388
389 tty = tty_port_tty_get(&ifx_dev->tty_port);
390 if (!tty)
391 return;
392 tty_wakeup(tty);
393 tty_kref_put(tty);
394}
395
396/**
397 * ifx_spi_prepare_tx_buffer - prepare transmit frame
398 * @ifx_dev: our SPI device
399 *
400 * The transmit buffr needs a header and various other bits of
401 * information followed by as much data as we can pull from the FIFO
402 * and transfer. This function formats up a suitable buffer in the
403 * ifx_dev->tx_buffer
404 *
405 * FIXME: performance - should we wake the tty when the queue is half
406 * empty ?
407 */
408static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
409{
410 int temp_count;
411 int queue_length;
412 int tx_count;
413 unsigned char *tx_buffer;
414
415 tx_buffer = ifx_dev->tx_buffer;
416 memset(tx_buffer, 0, IFX_SPI_TRANSFER_SIZE);
417
418 /* make room for required SPI header */
419 tx_buffer += IFX_SPI_HEADER_OVERHEAD;
420 tx_count = IFX_SPI_HEADER_OVERHEAD;
421
422 /* clear to signal no more data if this turns out to be the
423 * last buffer sent in a sequence */
424 ifx_dev->spi_more = 0;
425
426 /* if modem cts is set, just send empty buffer */
427 if (!ifx_dev->spi_slave_cts) {
428 /* see if there's tx data */
429 queue_length = kfifo_len(&ifx_dev->tx_fifo);
430 if (queue_length != 0) {
431 /* data to mux -- see if there's room for it */
432 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
433 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
434 tx_buffer, temp_count,
435 &ifx_dev->fifo_lock);
436
437 /* update buffer pointer and data count in message */
438 tx_buffer += temp_count;
439 tx_count += temp_count;
440 if (temp_count == queue_length)
441 /* poke port to get more data */
442 ifx_spi_wakeup_serial(ifx_dev);
443 else /* more data in port, use next SPI message */
444 ifx_dev->spi_more = 1;
445 }
446 }
447 /* have data and info for header -- set up SPI header in buffer */
448 /* spi header needs payload size, not entire buffer size */
449 ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
450 tx_count-IFX_SPI_HEADER_OVERHEAD,
451 ifx_dev->spi_more);
452 /* swap actual data in the buffer */
453 swap_buf((u16 *)(ifx_dev->tx_buffer), tx_count,
454 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
455 return tx_count;
456}
457
458/**
459 * ifx_spi_write - line discipline write
460 * @tty: our tty device
461 * @buf: pointer to buffer to write (kernel space)
462 * @count: size of buffer
463 *
464 * Write the characters we have been given into the FIFO. If the device
465 * is not active then activate it, when the SRDY line is asserted back
466 * this will commence I/O
467 */
468static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
469 int count)
470{
471 struct ifx_spi_device *ifx_dev = tty->driver_data;
472 unsigned char *tmp_buf = (unsigned char *)buf;
473 int tx_count = kfifo_in_locked(&ifx_dev->tx_fifo, tmp_buf, count,
474 &ifx_dev->fifo_lock);
475 mrdy_assert(ifx_dev);
476 return tx_count;
477}
478
479/**
480 * ifx_spi_chars_in_buffer - line discipline helper
481 * @tty: our tty device
482 *
483 * Report how much data we can accept before we drop bytes. As we use
484 * a simple FIFO this is nice and easy.
485 */
486static int ifx_spi_write_room(struct tty_struct *tty)
487{
488 struct ifx_spi_device *ifx_dev = tty->driver_data;
489 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
490}
491
492/**
493 * ifx_spi_chars_in_buffer - line discipline helper
494 * @tty: our tty device
495 *
496 * Report how many characters we have buffered. In our case this is the
497 * number of bytes sitting in our transmit FIFO.
498 */
499static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
500{
501 struct ifx_spi_device *ifx_dev = tty->driver_data;
502 return kfifo_len(&ifx_dev->tx_fifo);
503}
504
505/**
506 * ifx_port_hangup
507 * @port: our tty port
508 *
509 * tty port hang up. Called when tty_hangup processing is invoked either
510 * by loss of carrier, or by software (eg vhangup). Serialized against
511 * activate/shutdown by the tty layer.
512 */
513static void ifx_spi_hangup(struct tty_struct *tty)
514{
515 struct ifx_spi_device *ifx_dev = tty->driver_data;
516 tty_port_hangup(&ifx_dev->tty_port);
517}
518
519/**
520 * ifx_port_activate
521 * @port: our tty port
522 *
523 * tty port activate method - called for first open. Serialized
524 * with hangup and shutdown by the tty layer.
525 */
526static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
527{
528 struct ifx_spi_device *ifx_dev =
529 container_of(port, struct ifx_spi_device, tty_port);
530
531 /* clear any old data; can't do this in 'close' */
532 kfifo_reset(&ifx_dev->tx_fifo);
533
534 /* put port data into this tty */
535 tty->driver_data = ifx_dev;
536
537 /* allows flip string push from int context */
538 tty->low_latency = 1;
539
540 return 0;
541}
542
543/**
544 * ifx_port_shutdown
545 * @port: our tty port
546 *
547 * tty port shutdown method - called for last port close. Serialized
548 * with hangup and activate by the tty layer.
549 */
550static void ifx_port_shutdown(struct tty_port *port)
551{
552 struct ifx_spi_device *ifx_dev =
553 container_of(port, struct ifx_spi_device, tty_port);
554
555 mrdy_set_low(ifx_dev);
556 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
557 tasklet_kill(&ifx_dev->io_work_tasklet);
558}
559
560static const struct tty_port_operations ifx_tty_port_ops = {
561 .activate = ifx_port_activate,
562 .shutdown = ifx_port_shutdown,
563};
564
565static const struct tty_operations ifx_spi_serial_ops = {
566 .open = ifx_spi_open,
567 .close = ifx_spi_close,
568 .write = ifx_spi_write,
569 .hangup = ifx_spi_hangup,
570 .write_room = ifx_spi_write_room,
571 .chars_in_buffer = ifx_spi_chars_in_buffer,
572 .tiocmget = ifx_spi_tiocmget,
573 .tiocmset = ifx_spi_tiocmset,
574};
575
576/**
577 * ifx_spi_insert_fip_string - queue received data
578 * @ifx_ser: our SPI device
579 * @chars: buffer we have received
580 * @size: number of chars reeived
581 *
582 * Queue bytes to the tty assuming the tty side is currently open. If
583 * not the discard the data.
584 */
585static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
586 unsigned char *chars, size_t size)
587{
588 struct tty_struct *tty = tty_port_tty_get(&ifx_dev->tty_port);
589 if (!tty)
590 return;
591 tty_insert_flip_string(tty, chars, size);
592 tty_flip_buffer_push(tty);
593 tty_kref_put(tty);
594}
595
596/**
597 * ifx_spi_complete - SPI transfer completed
598 * @ctx: our SPI device
599 *
600 * An SPI transfer has completed. Process any received data and kick off
601 * any further transmits we can commence.
602 */
603static void ifx_spi_complete(void *ctx)
604{
605 struct ifx_spi_device *ifx_dev = ctx;
606 struct tty_struct *tty;
607 struct tty_ldisc *ldisc = NULL;
608 int length;
609 int actual_length;
610 unsigned char more;
611 unsigned char cts;
612 int local_write_pending = 0;
613 int queue_length;
614 int srdy;
615 int decode_result;
616
617 mrdy_set_low(ifx_dev);
618
619 if (!ifx_dev->spi_msg.status) {
620 /* check header validity, get comm flags */
621 swap_buf((u16 *)ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
622 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
623 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
624 &length, &more, &cts);
625 if (decode_result == IFX_SPI_HEADER_0) {
626 dev_dbg(&ifx_dev->spi_dev->dev,
627 "ignore input: invalid header 0");
628 ifx_dev->spi_slave_cts = 0;
629 goto complete_exit;
630 } else if (decode_result == IFX_SPI_HEADER_F) {
631 dev_dbg(&ifx_dev->spi_dev->dev,
632 "ignore input: invalid header F");
633 goto complete_exit;
634 }
635
636 ifx_dev->spi_slave_cts = cts;
637
638 actual_length = min((unsigned int)length,
639 ifx_dev->spi_msg.actual_length);
640 swap_buf((u16 *)(ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
641 actual_length,
642 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
643 ifx_spi_insert_flip_string(
644 ifx_dev,
645 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
646 (size_t)actual_length);
647 } else {
648 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
649 ifx_dev->spi_msg.status);
650 }
651
652complete_exit:
653 if (ifx_dev->write_pending) {
654 ifx_dev->write_pending = 0;
655 local_write_pending = 1;
656 }
657
658 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
659
660 queue_length = kfifo_len(&ifx_dev->tx_fifo);
661 srdy = gpio_get_value(ifx_dev->gpio.srdy);
662 if (!srdy)
663 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
664
665 /* schedule output if there is more to do */
666 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
667 tasklet_schedule(&ifx_dev->io_work_tasklet);
668 else {
669 if (more || ifx_dev->spi_more || queue_length > 0 ||
670 local_write_pending) {
671 if (ifx_dev->spi_slave_cts) {
672 if (more)
673 mrdy_assert(ifx_dev);
674 } else
675 mrdy_assert(ifx_dev);
676 } else {
677 /*
678 * poke line discipline driver if any for more data
679 * may or may not get more data to write
680 * for now, say not busy
681 */
682 ifx_spi_power_state_clear(ifx_dev,
683 IFX_SPI_POWER_DATA_PENDING);
684 tty = tty_port_tty_get(&ifx_dev->tty_port);
685 if (tty) {
686 ldisc = tty_ldisc_ref(tty);
687 if (ldisc) {
688 ldisc->ops->write_wakeup(tty);
689 tty_ldisc_deref(ldisc);
690 }
691 tty_kref_put(tty);
692 }
693 }
694 }
695}
696
697/**
698 * ifx_spio_io - I/O tasklet
699 * @data: our SPI device
700 *
701 * Queue data for transmission if possible and then kick off the
702 * transfer.
703 */
704static void ifx_spi_io(unsigned long data)
705{
706 int retval;
707 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
708
709 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags)) {
710 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
711 ifx_dev->gpio.unack_srdy_int_nb--;
712
713 ifx_spi_prepare_tx_buffer(ifx_dev);
714
715 spi_message_init(&ifx_dev->spi_msg);
716 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
717
718 ifx_dev->spi_msg.context = ifx_dev;
719 ifx_dev->spi_msg.complete = ifx_spi_complete;
720
721 /* set up our spi transfer */
722 /* note len is BYTES, not transfers */
723 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
724 ifx_dev->spi_xfer.cs_change = 0;
725 ifx_dev->spi_xfer.speed_hz = 12500000;
726 /* ifx_dev->spi_xfer.speed_hz = 390625; */
727 ifx_dev->spi_xfer.bits_per_word = spi_b16 ? 16 : 8;
728
729 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
730 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
731
732 /*
733 * setup dma pointers
734 */
735 if (ifx_dev->is_6160) {
736 ifx_dev->spi_msg.is_dma_mapped = 1;
737 ifx_dev->tx_dma = ifx_dev->tx_bus;
738 ifx_dev->rx_dma = ifx_dev->rx_bus;
739 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
740 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
741 } else {
742 ifx_dev->spi_msg.is_dma_mapped = 0;
743 ifx_dev->tx_dma = (dma_addr_t)0;
744 ifx_dev->rx_dma = (dma_addr_t)0;
745 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
746 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
747 }
748
749 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
750
751 /* Assert MRDY. This may have already been done by the write
752 * routine.
753 */
754 mrdy_assert(ifx_dev);
755
756 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
757 if (retval) {
758 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
759 &ifx_dev->flags);
760 tasklet_schedule(&ifx_dev->io_work_tasklet);
761 return;
762 }
763 } else
764 ifx_dev->write_pending = 1;
765}
766
767/**
768 * ifx_spi_free_port - free up the tty side
769 * @ifx_dev: IFX device going away
770 *
771 * Unregister and free up a port when the device goes away
772 */
773static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
774{
775 if (ifx_dev->tty_dev)
776 tty_unregister_device(tty_drv, ifx_dev->minor);
777 kfifo_free(&ifx_dev->tx_fifo);
778}
779
780/**
781 * ifx_spi_create_port - create a new port
782 * @ifx_dev: our spi device
783 *
784 * Allocate and initialise the tty port that goes with this interface
785 * and add it to the tty layer so that it can be opened.
786 */
787static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
788{
789 int ret = 0;
790 struct tty_port *pport = &ifx_dev->tty_port;
791
792 spin_lock_init(&ifx_dev->fifo_lock);
793 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
794 &ifx_spi_key, 0);
795
796 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
797 ret = -ENOMEM;
798 goto error_ret;
799 }
800
801 pport->ops = &ifx_tty_port_ops;
802 tty_port_init(pport);
803 ifx_dev->minor = IFX_SPI_TTY_ID;
804 ifx_dev->tty_dev = tty_register_device(tty_drv, ifx_dev->minor,
805 &ifx_dev->spi_dev->dev);
806 if (IS_ERR(ifx_dev->tty_dev)) {
807 dev_dbg(&ifx_dev->spi_dev->dev,
808 "%s: registering tty device failed", __func__);
809 ret = PTR_ERR(ifx_dev->tty_dev);
810 goto error_ret;
811 }
812 return 0;
813
814error_ret:
815 ifx_spi_free_port(ifx_dev);
816 return ret;
817}
818
819/**
820 * ifx_spi_handle_srdy - handle SRDY
821 * @ifx_dev: device asserting SRDY
822 *
823 * Check our device state and see what we need to kick off when SRDY
824 * is asserted. This usually means killing the timer and firing off the
825 * I/O processing.
826 */
827static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
828{
829 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
830 del_timer_sync(&ifx_dev->spi_timer);
831 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
832 }
833
834 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
835
836 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
837 tasklet_schedule(&ifx_dev->io_work_tasklet);
838 else
839 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
840}
841
842/**
843 * ifx_spi_srdy_interrupt - SRDY asserted
844 * @irq: our IRQ number
845 * @dev: our ifx device
846 *
847 * The modem asserted SRDY. Handle the srdy event
848 */
849static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
850{
851 struct ifx_spi_device *ifx_dev = dev;
852 ifx_dev->gpio.unack_srdy_int_nb++;
853 ifx_spi_handle_srdy(ifx_dev);
854 return IRQ_HANDLED;
855}
856
857/**
858 * ifx_spi_reset_interrupt - Modem has changed reset state
859 * @irq: interrupt number
860 * @dev: our device pointer
861 *
862 * The modem has either entered or left reset state. Check the GPIO
863 * line to see which.
864 *
865 * FIXME: review locking on MR_INPROGRESS versus
866 * parallel unsolicited reset/solicited reset
867 */
868static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
869{
870 struct ifx_spi_device *ifx_dev = dev;
871 int val = gpio_get_value(ifx_dev->gpio.reset_out);
872 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
873
874 if (val == 0) {
875 /* entered reset */
876 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
877 if (!solreset) {
878 /* unsolicited reset */
879 ifx_spi_ttyhangup(ifx_dev);
880 }
881 } else {
882 /* exited reset */
883 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
884 if (solreset) {
885 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
886 wake_up(&ifx_dev->mdm_reset_wait);
887 }
888 }
889 return IRQ_HANDLED;
890}
891
892/**
893 * ifx_spi_free_device - free device
894 * @ifx_dev: device to free
895 *
896 * Free the IFX device
897 */
898static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
899{
900 ifx_spi_free_port(ifx_dev);
901 dma_free_coherent(&ifx_dev->spi_dev->dev,
902 IFX_SPI_TRANSFER_SIZE,
903 ifx_dev->tx_buffer,
904 ifx_dev->tx_bus);
905 dma_free_coherent(&ifx_dev->spi_dev->dev,
906 IFX_SPI_TRANSFER_SIZE,
907 ifx_dev->rx_buffer,
908 ifx_dev->rx_bus);
909}
910
911/**
912 * ifx_spi_reset - reset modem
913 * @ifx_dev: modem to reset
914 *
915 * Perform a reset on the modem
916 */
917static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
918{
919 int ret;
920 /*
921 * set up modem power, reset
922 *
923 * delays are required on some platforms for the modem
924 * to reset properly
925 */
926 set_bit(MR_START, &ifx_dev->mdm_reset_state);
927 gpio_set_value(ifx_dev->gpio.po, 0);
928 gpio_set_value(ifx_dev->gpio.reset, 0);
929 msleep(25);
930 gpio_set_value(ifx_dev->gpio.reset, 1);
931 msleep(1);
932 gpio_set_value(ifx_dev->gpio.po, 1);
933 msleep(1);
934 gpio_set_value(ifx_dev->gpio.po, 0);
935 ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
936 test_bit(MR_COMPLETE,
937 &ifx_dev->mdm_reset_state),
938 IFX_RESET_TIMEOUT);
939 if (!ret)
940 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
941 ifx_dev->mdm_reset_state);
942
943 ifx_dev->mdm_reset_state = 0;
944 return ret;
945}
946
947/**
948 * ifx_spi_spi_probe - probe callback
949 * @spi: our possible matching SPI device
950 *
951 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
952 * GPIO setup.
953 *
954 * FIXME:
955 * - Support for multiple devices
956 * - Split out MID specific GPIO handling eventually
957 */
958
959static int ifx_spi_spi_probe(struct spi_device *spi)
960{
961 int ret;
962 int srdy;
963 struct ifx_modem_platform_data *pl_data = NULL;
964 struct ifx_spi_device *ifx_dev;
965
966 if (saved_ifx_dev) {
967 dev_dbg(&spi->dev, "ignoring subsequent detection");
968 return -ENODEV;
969 }
970
971 /* initialize structure to hold our device variables */
972 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
973 if (!ifx_dev) {
974 dev_err(&spi->dev, "spi device allocation failed");
975 return -ENOMEM;
976 }
977 saved_ifx_dev = ifx_dev;
978 ifx_dev->spi_dev = spi;
979 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
980 spin_lock_init(&ifx_dev->write_lock);
981 spin_lock_init(&ifx_dev->power_lock);
982 ifx_dev->power_status = 0;
983 init_timer(&ifx_dev->spi_timer);
984 ifx_dev->spi_timer.function = ifx_spi_timeout;
985 ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
986 ifx_dev->is_6160 = pl_data->is_6160;
987
988 /* ensure SPI protocol flags are initialized to enable transfer */
989 ifx_dev->spi_more = 0;
990 ifx_dev->spi_slave_cts = 0;
991
992 /*initialize transfer and dma buffers */
993 ifx_dev->tx_buffer = dma_alloc_coherent(&ifx_dev->spi_dev->dev,
994 IFX_SPI_TRANSFER_SIZE,
995 &ifx_dev->tx_bus,
996 GFP_KERNEL);
997 if (!ifx_dev->tx_buffer) {
998 dev_err(&spi->dev, "DMA-TX buffer allocation failed");
999 ret = -ENOMEM;
1000 goto error_ret;
1001 }
1002 ifx_dev->rx_buffer = dma_alloc_coherent(&ifx_dev->spi_dev->dev,
1003 IFX_SPI_TRANSFER_SIZE,
1004 &ifx_dev->rx_bus,
1005 GFP_KERNEL);
1006 if (!ifx_dev->rx_buffer) {
1007 dev_err(&spi->dev, "DMA-RX buffer allocation failed");
1008 ret = -ENOMEM;
1009 goto error_ret;
1010 }
1011
1012 /* initialize waitq for modem reset */
1013 init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1014
1015 spi_set_drvdata(spi, ifx_dev);
1016 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1017 (unsigned long)ifx_dev);
1018
1019 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1020
1021 /* create our tty port */
1022 ret = ifx_spi_create_port(ifx_dev);
1023 if (ret != 0) {
1024 dev_err(&spi->dev, "create default tty port failed");
1025 goto error_ret;
1026 }
1027
1028 pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data;
1029 if (pl_data) {
1030 ifx_dev->gpio.reset = pl_data->rst_pmu;
1031 ifx_dev->gpio.po = pl_data->pwr_on;
1032 ifx_dev->gpio.mrdy = pl_data->mrdy;
1033 ifx_dev->gpio.srdy = pl_data->srdy;
1034 ifx_dev->gpio.reset_out = pl_data->rst_out;
1035 } else {
1036 dev_err(&spi->dev, "missing platform data!");
1037 ret = -ENODEV;
1038 goto error_ret;
1039 }
1040
1041 dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
1042 ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
1043 ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
1044
1045 /* Configure gpios */
1046 ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
1047 if (ret < 0) {
1048 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
1049 ifx_dev->gpio.reset);
1050 goto error_ret;
1051 }
1052 ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
1053 ret += gpio_export(ifx_dev->gpio.reset, 1);
1054 if (ret) {
1055 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
1056 ifx_dev->gpio.reset);
1057 ret = -EBUSY;
1058 goto error_ret2;
1059 }
1060
1061 ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
1062 ret += gpio_direction_output(ifx_dev->gpio.po, 0);
1063 ret += gpio_export(ifx_dev->gpio.po, 1);
1064 if (ret) {
1065 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
1066 ifx_dev->gpio.po);
1067 ret = -EBUSY;
1068 goto error_ret3;
1069 }
1070
1071 ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
1072 if (ret < 0) {
1073 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
1074 ifx_dev->gpio.mrdy);
1075 goto error_ret3;
1076 }
1077 ret += gpio_export(ifx_dev->gpio.mrdy, 1);
1078 ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
1079 if (ret) {
1080 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
1081 ifx_dev->gpio.mrdy);
1082 ret = -EBUSY;
1083 goto error_ret4;
1084 }
1085
1086 ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
1087 if (ret < 0) {
1088 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
1089 ifx_dev->gpio.srdy);
1090 ret = -EBUSY;
1091 goto error_ret4;
1092 }
1093 ret += gpio_export(ifx_dev->gpio.srdy, 1);
1094 ret += gpio_direction_input(ifx_dev->gpio.srdy);
1095 if (ret) {
1096 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
1097 ifx_dev->gpio.srdy);
1098 ret = -EBUSY;
1099 goto error_ret5;
1100 }
1101
1102 ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
1103 if (ret < 0) {
1104 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
1105 ifx_dev->gpio.reset_out);
1106 goto error_ret5;
1107 }
1108 ret += gpio_export(ifx_dev->gpio.reset_out, 1);
1109 ret += gpio_direction_input(ifx_dev->gpio.reset_out);
1110 if (ret) {
1111 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
1112 ifx_dev->gpio.reset_out);
1113 ret = -EBUSY;
1114 goto error_ret6;
1115 }
1116
1117 ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
1118 ifx_spi_reset_interrupt,
1119 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
1120 (void *)ifx_dev);
1121 if (ret) {
1122 dev_err(&spi->dev, "Unable to get irq %x\n",
1123 gpio_to_irq(ifx_dev->gpio.reset_out));
1124 goto error_ret6;
1125 }
1126
1127 ret = ifx_spi_reset(ifx_dev);
1128
1129 ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
1130 ifx_spi_srdy_interrupt,
1131 IRQF_TRIGGER_RISING, DRVNAME,
1132 (void *)ifx_dev);
1133 if (ret) {
1134 dev_err(&spi->dev, "Unable to get irq %x",
1135 gpio_to_irq(ifx_dev->gpio.srdy));
1136 goto error_ret7;
1137 }
1138
1139 /* set pm runtime power state and register with power system */
1140 pm_runtime_set_active(&spi->dev);
1141 pm_runtime_enable(&spi->dev);
1142
1143 /* handle case that modem is already signaling SRDY */
1144 /* no outgoing tty open at this point, this just satisfies the
1145 * modem's read and should reset communication properly
1146 */
1147 srdy = gpio_get_value(ifx_dev->gpio.srdy);
1148
1149 if (srdy) {
1150 mrdy_assert(ifx_dev);
1151 ifx_spi_handle_srdy(ifx_dev);
1152 } else
1153 mrdy_set_low(ifx_dev);
1154 return 0;
1155
1156error_ret7:
1157 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1158error_ret6:
1159 gpio_free(ifx_dev->gpio.srdy);
1160error_ret5:
1161 gpio_free(ifx_dev->gpio.mrdy);
1162error_ret4:
1163 gpio_free(ifx_dev->gpio.reset);
1164error_ret3:
1165 gpio_free(ifx_dev->gpio.po);
1166error_ret2:
1167 gpio_free(ifx_dev->gpio.reset_out);
1168error_ret:
1169 ifx_spi_free_device(ifx_dev);
1170 saved_ifx_dev = NULL;
1171 return ret;
1172}
1173
1174/**
1175 * ifx_spi_spi_remove - SPI device was removed
1176 * @spi: SPI device
1177 *
1178 * FIXME: We should be shutting the device down here not in
1179 * the module unload path.
1180 */
1181
1182static int ifx_spi_spi_remove(struct spi_device *spi)
1183{
1184 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1185 /* stop activity */
1186 tasklet_kill(&ifx_dev->io_work_tasklet);
1187 /* free irq */
1188 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1189 free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
1190
1191 gpio_free(ifx_dev->gpio.srdy);
1192 gpio_free(ifx_dev->gpio.mrdy);
1193 gpio_free(ifx_dev->gpio.reset);
1194 gpio_free(ifx_dev->gpio.po);
1195 gpio_free(ifx_dev->gpio.reset_out);
1196
1197 /* free allocations */
1198 ifx_spi_free_device(ifx_dev);
1199
1200 saved_ifx_dev = NULL;
1201 return 0;
1202}
1203
1204/**
1205 * ifx_spi_spi_shutdown - called on SPI shutdown
1206 * @spi: SPI device
1207 *
1208 * No action needs to be taken here
1209 */
1210
1211static void ifx_spi_spi_shutdown(struct spi_device *spi)
1212{
1213}
1214
1215/*
1216 * various suspends and resumes have nothing to do
1217 * no hardware to save state for
1218 */
1219
1220/**
1221 * ifx_spi_spi_suspend - suspend SPI on system suspend
1222 * @dev: device being suspended
1223 *
1224 * Suspend the SPI side. No action needed on Intel MID platforms, may
1225 * need extending for other systems.
1226 */
1227static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg)
1228{
1229 return 0;
1230}
1231
1232/**
1233 * ifx_spi_spi_resume - resume SPI side on system resume
1234 * @dev: device being suspended
1235 *
1236 * Suspend the SPI side. No action needed on Intel MID platforms, may
1237 * need extending for other systems.
1238 */
1239static int ifx_spi_spi_resume(struct spi_device *spi)
1240{
1241 return 0;
1242}
1243
1244/**
1245 * ifx_spi_pm_suspend - suspend modem on system suspend
1246 * @dev: device being suspended
1247 *
1248 * Suspend the modem. No action needed on Intel MID platforms, may
1249 * need extending for other systems.
1250 */
1251static int ifx_spi_pm_suspend(struct device *dev)
1252{
1253 return 0;
1254}
1255
1256/**
1257 * ifx_spi_pm_resume - resume modem on system resume
1258 * @dev: device being suspended
1259 *
1260 * Allow the modem to resume. No action needed.
1261 *
1262 * FIXME: do we need to reset anything here ?
1263 */
1264static int ifx_spi_pm_resume(struct device *dev)
1265{
1266 return 0;
1267}
1268
1269/**
1270 * ifx_spi_pm_runtime_resume - suspend modem
1271 * @dev: device being suspended
1272 *
1273 * Allow the modem to resume. No action needed.
1274 */
1275static int ifx_spi_pm_runtime_resume(struct device *dev)
1276{
1277 return 0;
1278}
1279
1280/**
1281 * ifx_spi_pm_runtime_suspend - suspend modem
1282 * @dev: device being suspended
1283 *
1284 * Allow the modem to suspend and thus suspend to continue up the
1285 * device tree.
1286 */
1287static int ifx_spi_pm_runtime_suspend(struct device *dev)
1288{
1289 return 0;
1290}
1291
1292/**
1293 * ifx_spi_pm_runtime_idle - check if modem idle
1294 * @dev: our device
1295 *
1296 * Check conditions and queue runtime suspend if idle.
1297 */
1298static int ifx_spi_pm_runtime_idle(struct device *dev)
1299{
1300 struct spi_device *spi = to_spi_device(dev);
1301 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1302
1303 if (!ifx_dev->power_status)
1304 pm_runtime_suspend(dev);
1305
1306 return 0;
1307}
1308
1309static const struct dev_pm_ops ifx_spi_pm = {
1310 .resume = ifx_spi_pm_resume,
1311 .suspend = ifx_spi_pm_suspend,
1312 .runtime_resume = ifx_spi_pm_runtime_resume,
1313 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1314 .runtime_idle = ifx_spi_pm_runtime_idle
1315};
1316
1317static const struct spi_device_id ifx_id_table[] = {
1318 {"ifx6160", 0},
1319 {"ifx6260", 0},
1320 { }
1321};
1322MODULE_DEVICE_TABLE(spi, ifx_id_table);
1323
1324/* spi operations */
1325static const struct spi_driver ifx_spi_driver_6160 = {
1326 .driver = {
1327 .name = "ifx6160",
1328 .bus = &spi_bus_type,
1329 .pm = &ifx_spi_pm,
1330 .owner = THIS_MODULE},
1331 .probe = ifx_spi_spi_probe,
1332 .shutdown = ifx_spi_spi_shutdown,
1333 .remove = __devexit_p(ifx_spi_spi_remove),
1334 .suspend = ifx_spi_spi_suspend,
1335 .resume = ifx_spi_spi_resume,
1336 .id_table = ifx_id_table
1337};
1338
1339/**
1340 * ifx_spi_exit - module exit
1341 *
1342 * Unload the module.
1343 */
1344
1345static void __exit ifx_spi_exit(void)
1346{
1347 /* unregister */
1348 tty_unregister_driver(tty_drv);
1349 spi_unregister_driver((void *)&ifx_spi_driver_6160);
1350}
1351
1352/**
1353 * ifx_spi_init - module entry point
1354 *
1355 * Initialise the SPI and tty interfaces for the IFX SPI driver
1356 * We need to initialize upper-edge spi driver after the tty
1357 * driver because otherwise the spi probe will race
1358 */
1359
1360static int __init ifx_spi_init(void)
1361{
1362 int result;
1363
1364 tty_drv = alloc_tty_driver(1);
1365 if (!tty_drv) {
1366 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1367 return -ENOMEM;
1368 }
1369
1370 tty_drv->magic = TTY_DRIVER_MAGIC;
1371 tty_drv->owner = THIS_MODULE;
1372 tty_drv->driver_name = DRVNAME;
1373 tty_drv->name = TTYNAME;
1374 tty_drv->minor_start = IFX_SPI_TTY_ID;
1375 tty_drv->num = 1;
1376 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1377 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1378 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1379 tty_drv->init_termios = tty_std_termios;
1380
1381 tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1382
1383 result = tty_register_driver(tty_drv);
1384 if (result) {
1385 pr_err("%s: tty_register_driver failed(%d)",
1386 DRVNAME, result);
1387 put_tty_driver(tty_drv);
1388 return result;
1389 }
1390
1391 result = spi_register_driver((void *)&ifx_spi_driver_6160);
1392 if (result) {
1393 pr_err("%s: spi_register_driver failed(%d)",
1394 DRVNAME, result);
1395 tty_unregister_driver(tty_drv);
1396 }
1397 return result;
1398}
1399
1400module_init(ifx_spi_init);
1401module_exit(ifx_spi_exit);
1402
1403MODULE_AUTHOR("Intel");
1404MODULE_DESCRIPTION("IFX6x60 spi driver");
1405MODULE_LICENSE("GPL");
1406MODULE_INFO(Version, "0.1-IFX6x60");
diff --git a/drivers/tty/serial/ifx6x60.h b/drivers/tty/serial/ifx6x60.h
new file mode 100644
index 000000000000..deb7b8d977dc
--- /dev/null
+++ b/drivers/tty/serial/ifx6x60.h
@@ -0,0 +1,129 @@
1/****************************************************************************
2 *
3 * Driver for the IFX spi modem.
4 *
5 * Copyright (C) 2009, 2010 Intel Corp
6 * Jim Stanley <jim.stanley@intel.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
22 *
23 *
24 *
25 *****************************************************************************/
26#ifndef _IFX6X60_H
27#define _IFX6X60_H
28
29#define DRVNAME "ifx6x60"
30#define TTYNAME "ttyIFX"
31
32/* #define IFX_THROTTLE_CODE */
33
34#define IFX_SPI_MAX_MINORS 1
35#define IFX_SPI_TRANSFER_SIZE 2048
36#define IFX_SPI_FIFO_SIZE 4096
37
38#define IFX_SPI_HEADER_OVERHEAD 4
39#define IFX_RESET_TIMEOUT msecs_to_jiffies(50)
40
41/* device flags bitfield definitions */
42#define IFX_SPI_STATE_PRESENT 0
43#define IFX_SPI_STATE_IO_IN_PROGRESS 1
44#define IFX_SPI_STATE_IO_READY 2
45#define IFX_SPI_STATE_TIMER_PENDING 3
46
47/* flow control bitfields */
48#define IFX_SPI_DCD 0
49#define IFX_SPI_CTS 1
50#define IFX_SPI_DSR 2
51#define IFX_SPI_RI 3
52#define IFX_SPI_DTR 4
53#define IFX_SPI_RTS 5
54#define IFX_SPI_TX_FC 6
55#define IFX_SPI_RX_FC 7
56#define IFX_SPI_UPDATE 8
57
58#define IFX_SPI_PAYLOAD_SIZE (IFX_SPI_TRANSFER_SIZE - \
59 IFX_SPI_HEADER_OVERHEAD)
60
61#define IFX_SPI_IRQ_TYPE DETECT_EDGE_RISING
62#define IFX_SPI_GPIO_TARGET 0
63#define IFX_SPI_GPIO0 0x105
64
65#define IFX_SPI_STATUS_TIMEOUT (2000*HZ)
66
67/* values for bits in power status byte */
68#define IFX_SPI_POWER_DATA_PENDING 1
69#define IFX_SPI_POWER_SRDY 2
70
71struct ifx_spi_device {
72 /* Our SPI device */
73 struct spi_device *spi_dev;
74
75 /* Port specific data */
76 struct kfifo tx_fifo;
77 spinlock_t fifo_lock;
78 unsigned long signal_state;
79
80 /* TTY Layer logic */
81 struct tty_port tty_port;
82 struct device *tty_dev;
83 int minor;
84
85 /* Low level I/O work */
86 struct tasklet_struct io_work_tasklet;
87 unsigned long flags;
88 dma_addr_t rx_dma;
89 dma_addr_t tx_dma;
90
91 int is_6160; /* Modem type */
92
93 spinlock_t write_lock;
94 int write_pending;
95 spinlock_t power_lock;
96 unsigned char power_status;
97
98 unsigned char *rx_buffer;
99 unsigned char *tx_buffer;
100 dma_addr_t rx_bus;
101 dma_addr_t tx_bus;
102 unsigned char spi_more;
103 unsigned char spi_slave_cts;
104
105 struct timer_list spi_timer;
106
107 struct spi_message spi_msg;
108 struct spi_transfer spi_xfer;
109
110 struct {
111 /* gpio lines */
112 unsigned short srdy; /* slave-ready gpio */
113 unsigned short mrdy; /* master-ready gpio */
114 unsigned short reset; /* modem-reset gpio */
115 unsigned short po; /* modem-on gpio */
116 unsigned short reset_out; /* modem-in-reset gpio */
117 /* state/stats */
118 int unack_srdy_int_nb;
119 } gpio;
120
121 /* modem reset */
122 unsigned long mdm_reset_state;
123#define MR_START 0
124#define MR_INPROGRESS 1
125#define MR_COMPLETE 2
126 wait_queue_head_t mdm_reset_wait;
127};
128
129#endif /* _IFX6X60_H */
diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
new file mode 100644
index 000000000000..dfcf4b1878aa
--- /dev/null
+++ b/drivers/tty/serial/imx.c
@@ -0,0 +1,1380 @@
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 * Copyright (C) 2009 emlix GmbH
12 * Author: Fabian Godehardt (added IrDA support for iMX)
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 * [29-Mar-2005] Mike Lee
29 * Added hardware handshake
30 */
31
32#if defined(CONFIG_SERIAL_IMX_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/platform_device.h>
42#include <linux/tty.h>
43#include <linux/tty_flip.h>
44#include <linux/serial_core.h>
45#include <linux/serial.h>
46#include <linux/clk.h>
47#include <linux/delay.h>
48#include <linux/rational.h>
49#include <linux/slab.h>
50
51#include <asm/io.h>
52#include <asm/irq.h>
53#include <mach/hardware.h>
54#include <mach/imx-uart.h>
55
56/* Register definitions */
57#define URXD0 0x0 /* Receiver Register */
58#define URTX0 0x40 /* Transmitter Register */
59#define UCR1 0x80 /* Control Register 1 */
60#define UCR2 0x84 /* Control Register 2 */
61#define UCR3 0x88 /* Control Register 3 */
62#define UCR4 0x8c /* Control Register 4 */
63#define UFCR 0x90 /* FIFO Control Register */
64#define USR1 0x94 /* Status Register 1 */
65#define USR2 0x98 /* Status Register 2 */
66#define UESC 0x9c /* Escape Character Register */
67#define UTIM 0xa0 /* Escape Timer Register */
68#define UBIR 0xa4 /* BRM Incremental Register */
69#define UBMR 0xa8 /* BRM Modulator Register */
70#define UBRC 0xac /* Baud Rate Count Register */
71#define MX2_ONEMS 0xb0 /* One Millisecond register */
72#define UTS (cpu_is_mx1() ? 0xd0 : 0xb4) /* UART Test Register */
73
74/* UART Control Register Bit Fields.*/
75#define URXD_CHARRDY (1<<15)
76#define URXD_ERR (1<<14)
77#define URXD_OVRRUN (1<<13)
78#define URXD_FRMERR (1<<12)
79#define URXD_BRK (1<<11)
80#define URXD_PRERR (1<<10)
81#define UCR1_ADEN (1<<15) /* Auto dectect interrupt */
82#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
83#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
84#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
85#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
86#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
87#define UCR1_IREN (1<<7) /* Infrared interface enable */
88#define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
89#define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
90#define UCR1_SNDBRK (1<<4) /* Send break */
91#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
92#define MX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, mx1 only */
93#define UCR1_DOZE (1<<1) /* Doze */
94#define UCR1_UARTEN (1<<0) /* UART enabled */
95#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
96#define UCR2_IRTS (1<<14) /* Ignore RTS pin */
97#define UCR2_CTSC (1<<13) /* CTS pin control */
98#define UCR2_CTS (1<<12) /* Clear to send */
99#define UCR2_ESCEN (1<<11) /* Escape enable */
100#define UCR2_PREN (1<<8) /* Parity enable */
101#define UCR2_PROE (1<<7) /* Parity odd/even */
102#define UCR2_STPB (1<<6) /* Stop */
103#define UCR2_WS (1<<5) /* Word size */
104#define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
105#define UCR2_TXEN (1<<2) /* Transmitter enabled */
106#define UCR2_RXEN (1<<1) /* Receiver enabled */
107#define UCR2_SRST (1<<0) /* SW reset */
108#define UCR3_DTREN (1<<13) /* DTR interrupt enable */
109#define UCR3_PARERREN (1<<12) /* Parity enable */
110#define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
111#define UCR3_DSR (1<<10) /* Data set ready */
112#define UCR3_DCD (1<<9) /* Data carrier detect */
113#define UCR3_RI (1<<8) /* Ring indicator */
114#define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
115#define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
116#define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
117#define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
118#define MX1_UCR3_REF25 (1<<3) /* Ref freq 25 MHz, only on mx1 */
119#define MX1_UCR3_REF30 (1<<2) /* Ref Freq 30 MHz, only on mx1 */
120#define MX2_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select, on mx2/mx3 */
121#define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
122#define UCR3_BPEN (1<<0) /* Preset registers enable */
123#define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
124#define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
125#define UCR4_INVR (1<<9) /* Inverted infrared reception */
126#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
127#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
128#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
129#define UCR4_IRSC (1<<5) /* IR special case */
130#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
131#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
132#define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
133#define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
134#define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
135#define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
136#define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
137#define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
138#define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
139#define USR1_RTSS (1<<14) /* RTS pin status */
140#define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
141#define USR1_RTSD (1<<12) /* RTS delta */
142#define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
143#define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
144#define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
145#define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
146#define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
147#define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
148#define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
149#define USR2_ADET (1<<15) /* Auto baud rate detect complete */
150#define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
151#define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
152#define USR2_IDLE (1<<12) /* Idle condition */
153#define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
154#define USR2_WAKE (1<<7) /* Wake */
155#define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
156#define USR2_TXDC (1<<3) /* Transmitter complete */
157#define USR2_BRCD (1<<2) /* Break condition */
158#define USR2_ORE (1<<1) /* Overrun error */
159#define USR2_RDR (1<<0) /* Recv data ready */
160#define UTS_FRCPERR (1<<13) /* Force parity error */
161#define UTS_LOOP (1<<12) /* Loop tx and rx */
162#define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
163#define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
164#define UTS_TXFULL (1<<4) /* TxFIFO full */
165#define UTS_RXFULL (1<<3) /* RxFIFO full */
166#define UTS_SOFTRST (1<<0) /* Software reset */
167
168/* We've been assigned a range on the "Low-density serial ports" major */
169#define SERIAL_IMX_MAJOR 207
170#define MINOR_START 16
171#define DEV_NAME "ttymxc"
172#define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
173
174/*
175 * This determines how often we check the modem status signals
176 * for any change. They generally aren't connected to an IRQ
177 * so we have to poll them. We also check immediately before
178 * filling the TX fifo incase CTS has been dropped.
179 */
180#define MCTRL_TIMEOUT (250*HZ/1000)
181
182#define DRIVER_NAME "IMX-uart"
183
184#define UART_NR 8
185
186struct imx_port {
187 struct uart_port port;
188 struct timer_list timer;
189 unsigned int old_status;
190 int txirq,rxirq,rtsirq;
191 unsigned int have_rtscts:1;
192 unsigned int use_irda:1;
193 unsigned int irda_inv_rx:1;
194 unsigned int irda_inv_tx:1;
195 unsigned short trcv_delay; /* transceiver delay */
196 struct clk *clk;
197};
198
199#ifdef CONFIG_IRDA
200#define USE_IRDA(sport) ((sport)->use_irda)
201#else
202#define USE_IRDA(sport) (0)
203#endif
204
205/*
206 * Handle any change of modem status signal since we were last called.
207 */
208static void imx_mctrl_check(struct imx_port *sport)
209{
210 unsigned int status, changed;
211
212 status = sport->port.ops->get_mctrl(&sport->port);
213 changed = status ^ sport->old_status;
214
215 if (changed == 0)
216 return;
217
218 sport->old_status = status;
219
220 if (changed & TIOCM_RI)
221 sport->port.icount.rng++;
222 if (changed & TIOCM_DSR)
223 sport->port.icount.dsr++;
224 if (changed & TIOCM_CAR)
225 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
226 if (changed & TIOCM_CTS)
227 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
228
229 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
230}
231
232/*
233 * This is our per-port timeout handler, for checking the
234 * modem status signals.
235 */
236static void imx_timeout(unsigned long data)
237{
238 struct imx_port *sport = (struct imx_port *)data;
239 unsigned long flags;
240
241 if (sport->port.state) {
242 spin_lock_irqsave(&sport->port.lock, flags);
243 imx_mctrl_check(sport);
244 spin_unlock_irqrestore(&sport->port.lock, flags);
245
246 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
247 }
248}
249
250/*
251 * interrupts disabled on entry
252 */
253static void imx_stop_tx(struct uart_port *port)
254{
255 struct imx_port *sport = (struct imx_port *)port;
256 unsigned long temp;
257
258 if (USE_IRDA(sport)) {
259 /* half duplex - wait for end of transmission */
260 int n = 256;
261 while ((--n > 0) &&
262 !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
263 udelay(5);
264 barrier();
265 }
266 /*
267 * irda transceiver - wait a bit more to avoid
268 * cutoff, hardware dependent
269 */
270 udelay(sport->trcv_delay);
271
272 /*
273 * half duplex - reactivate receive mode,
274 * flush receive pipe echo crap
275 */
276 if (readl(sport->port.membase + USR2) & USR2_TXDC) {
277 temp = readl(sport->port.membase + UCR1);
278 temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
279 writel(temp, sport->port.membase + UCR1);
280
281 temp = readl(sport->port.membase + UCR4);
282 temp &= ~(UCR4_TCEN);
283 writel(temp, sport->port.membase + UCR4);
284
285 while (readl(sport->port.membase + URXD0) &
286 URXD_CHARRDY)
287 barrier();
288
289 temp = readl(sport->port.membase + UCR1);
290 temp |= UCR1_RRDYEN;
291 writel(temp, sport->port.membase + UCR1);
292
293 temp = readl(sport->port.membase + UCR4);
294 temp |= UCR4_DREN;
295 writel(temp, sport->port.membase + UCR4);
296 }
297 return;
298 }
299
300 temp = readl(sport->port.membase + UCR1);
301 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
302}
303
304/*
305 * interrupts disabled on entry
306 */
307static void imx_stop_rx(struct uart_port *port)
308{
309 struct imx_port *sport = (struct imx_port *)port;
310 unsigned long temp;
311
312 temp = readl(sport->port.membase + UCR2);
313 writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
314}
315
316/*
317 * Set the modem control timer to fire immediately.
318 */
319static void imx_enable_ms(struct uart_port *port)
320{
321 struct imx_port *sport = (struct imx_port *)port;
322
323 mod_timer(&sport->timer, jiffies);
324}
325
326static inline void imx_transmit_buffer(struct imx_port *sport)
327{
328 struct circ_buf *xmit = &sport->port.state->xmit;
329
330 while (!uart_circ_empty(xmit) &&
331 !(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
332 /* send xmit->buf[xmit->tail]
333 * out the port here */
334 writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
335 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
336 sport->port.icount.tx++;
337 }
338
339 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
340 uart_write_wakeup(&sport->port);
341
342 if (uart_circ_empty(xmit))
343 imx_stop_tx(&sport->port);
344}
345
346/*
347 * interrupts disabled on entry
348 */
349static void imx_start_tx(struct uart_port *port)
350{
351 struct imx_port *sport = (struct imx_port *)port;
352 unsigned long temp;
353
354 if (USE_IRDA(sport)) {
355 /* half duplex in IrDA mode; have to disable receive mode */
356 temp = readl(sport->port.membase + UCR4);
357 temp &= ~(UCR4_DREN);
358 writel(temp, sport->port.membase + UCR4);
359
360 temp = readl(sport->port.membase + UCR1);
361 temp &= ~(UCR1_RRDYEN);
362 writel(temp, sport->port.membase + UCR1);
363 }
364
365 temp = readl(sport->port.membase + UCR1);
366 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
367
368 if (USE_IRDA(sport)) {
369 temp = readl(sport->port.membase + UCR1);
370 temp |= UCR1_TRDYEN;
371 writel(temp, sport->port.membase + UCR1);
372
373 temp = readl(sport->port.membase + UCR4);
374 temp |= UCR4_TCEN;
375 writel(temp, sport->port.membase + UCR4);
376 }
377
378 if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
379 imx_transmit_buffer(sport);
380}
381
382static irqreturn_t imx_rtsint(int irq, void *dev_id)
383{
384 struct imx_port *sport = dev_id;
385 unsigned int val = readl(sport->port.membase + USR1) & USR1_RTSS;
386 unsigned long flags;
387
388 spin_lock_irqsave(&sport->port.lock, flags);
389
390 writel(USR1_RTSD, sport->port.membase + USR1);
391 uart_handle_cts_change(&sport->port, !!val);
392 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
393
394 spin_unlock_irqrestore(&sport->port.lock, flags);
395 return IRQ_HANDLED;
396}
397
398static irqreturn_t imx_txint(int irq, void *dev_id)
399{
400 struct imx_port *sport = dev_id;
401 struct circ_buf *xmit = &sport->port.state->xmit;
402 unsigned long flags;
403
404 spin_lock_irqsave(&sport->port.lock,flags);
405 if (sport->port.x_char)
406 {
407 /* Send next char */
408 writel(sport->port.x_char, sport->port.membase + URTX0);
409 goto out;
410 }
411
412 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
413 imx_stop_tx(&sport->port);
414 goto out;
415 }
416
417 imx_transmit_buffer(sport);
418
419 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
420 uart_write_wakeup(&sport->port);
421
422out:
423 spin_unlock_irqrestore(&sport->port.lock,flags);
424 return IRQ_HANDLED;
425}
426
427static irqreturn_t imx_rxint(int irq, void *dev_id)
428{
429 struct imx_port *sport = dev_id;
430 unsigned int rx,flg,ignored = 0;
431 struct tty_struct *tty = sport->port.state->port.tty;
432 unsigned long flags, temp;
433
434 spin_lock_irqsave(&sport->port.lock,flags);
435
436 while (readl(sport->port.membase + USR2) & USR2_RDR) {
437 flg = TTY_NORMAL;
438 sport->port.icount.rx++;
439
440 rx = readl(sport->port.membase + URXD0);
441
442 temp = readl(sport->port.membase + USR2);
443 if (temp & USR2_BRCD) {
444 writel(USR2_BRCD, sport->port.membase + USR2);
445 if (uart_handle_break(&sport->port))
446 continue;
447 }
448
449 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
450 continue;
451
452 if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) {
453 if (rx & URXD_PRERR)
454 sport->port.icount.parity++;
455 else if (rx & URXD_FRMERR)
456 sport->port.icount.frame++;
457 if (rx & URXD_OVRRUN)
458 sport->port.icount.overrun++;
459
460 if (rx & sport->port.ignore_status_mask) {
461 if (++ignored > 100)
462 goto out;
463 continue;
464 }
465
466 rx &= sport->port.read_status_mask;
467
468 if (rx & URXD_PRERR)
469 flg = TTY_PARITY;
470 else if (rx & URXD_FRMERR)
471 flg = TTY_FRAME;
472 if (rx & URXD_OVRRUN)
473 flg = TTY_OVERRUN;
474
475#ifdef SUPPORT_SYSRQ
476 sport->port.sysrq = 0;
477#endif
478 }
479
480 tty_insert_flip_char(tty, rx, flg);
481 }
482
483out:
484 spin_unlock_irqrestore(&sport->port.lock,flags);
485 tty_flip_buffer_push(tty);
486 return IRQ_HANDLED;
487}
488
489static irqreturn_t imx_int(int irq, void *dev_id)
490{
491 struct imx_port *sport = dev_id;
492 unsigned int sts;
493
494 sts = readl(sport->port.membase + USR1);
495
496 if (sts & USR1_RRDY)
497 imx_rxint(irq, dev_id);
498
499 if (sts & USR1_TRDY &&
500 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
501 imx_txint(irq, dev_id);
502
503 if (sts & USR1_RTSD)
504 imx_rtsint(irq, dev_id);
505
506 return IRQ_HANDLED;
507}
508
509/*
510 * Return TIOCSER_TEMT when transmitter is not busy.
511 */
512static unsigned int imx_tx_empty(struct uart_port *port)
513{
514 struct imx_port *sport = (struct imx_port *)port;
515
516 return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
517}
518
519/*
520 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
521 */
522static unsigned int imx_get_mctrl(struct uart_port *port)
523{
524 struct imx_port *sport = (struct imx_port *)port;
525 unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
526
527 if (readl(sport->port.membase + USR1) & USR1_RTSS)
528 tmp |= TIOCM_CTS;
529
530 if (readl(sport->port.membase + UCR2) & UCR2_CTS)
531 tmp |= TIOCM_RTS;
532
533 return tmp;
534}
535
536static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
537{
538 struct imx_port *sport = (struct imx_port *)port;
539 unsigned long temp;
540
541 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
542
543 if (mctrl & TIOCM_RTS)
544 temp |= UCR2_CTS;
545
546 writel(temp, sport->port.membase + UCR2);
547}
548
549/*
550 * Interrupts always disabled.
551 */
552static void imx_break_ctl(struct uart_port *port, int break_state)
553{
554 struct imx_port *sport = (struct imx_port *)port;
555 unsigned long flags, temp;
556
557 spin_lock_irqsave(&sport->port.lock, flags);
558
559 temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
560
561 if ( break_state != 0 )
562 temp |= UCR1_SNDBRK;
563
564 writel(temp, sport->port.membase + UCR1);
565
566 spin_unlock_irqrestore(&sport->port.lock, flags);
567}
568
569#define TXTL 2 /* reset default */
570#define RXTL 1 /* reset default */
571
572static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
573{
574 unsigned int val;
575 unsigned int ufcr_rfdiv;
576
577 /* set receiver / transmitter trigger level.
578 * RFDIV is set such way to satisfy requested uartclk value
579 */
580 val = TXTL << 10 | RXTL;
581 ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2)
582 / sport->port.uartclk;
583
584 if(!ufcr_rfdiv)
585 ufcr_rfdiv = 1;
586
587 val |= UFCR_RFDIV_REG(ufcr_rfdiv);
588
589 writel(val, sport->port.membase + UFCR);
590
591 return 0;
592}
593
594/* half the RX buffer size */
595#define CTSTL 16
596
597static int imx_startup(struct uart_port *port)
598{
599 struct imx_port *sport = (struct imx_port *)port;
600 int retval;
601 unsigned long flags, temp;
602
603 imx_setup_ufcr(sport, 0);
604
605 /* disable the DREN bit (Data Ready interrupt enable) before
606 * requesting IRQs
607 */
608 temp = readl(sport->port.membase + UCR4);
609
610 if (USE_IRDA(sport))
611 temp |= UCR4_IRSC;
612
613 /* set the trigger level for CTS */
614 temp &= ~(UCR4_CTSTL_MASK<< UCR4_CTSTL_SHF);
615 temp |= CTSTL<< UCR4_CTSTL_SHF;
616
617 writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
618
619 if (USE_IRDA(sport)) {
620 /* reset fifo's and state machines */
621 int i = 100;
622 temp = readl(sport->port.membase + UCR2);
623 temp &= ~UCR2_SRST;
624 writel(temp, sport->port.membase + UCR2);
625 while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
626 (--i > 0)) {
627 udelay(1);
628 }
629 }
630
631 /*
632 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
633 * chips only have one interrupt.
634 */
635 if (sport->txirq > 0) {
636 retval = request_irq(sport->rxirq, imx_rxint, 0,
637 DRIVER_NAME, sport);
638 if (retval)
639 goto error_out1;
640
641 retval = request_irq(sport->txirq, imx_txint, 0,
642 DRIVER_NAME, sport);
643 if (retval)
644 goto error_out2;
645
646 /* do not use RTS IRQ on IrDA */
647 if (!USE_IRDA(sport)) {
648 retval = request_irq(sport->rtsirq, imx_rtsint,
649 (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
650 IRQF_TRIGGER_FALLING |
651 IRQF_TRIGGER_RISING,
652 DRIVER_NAME, sport);
653 if (retval)
654 goto error_out3;
655 }
656 } else {
657 retval = request_irq(sport->port.irq, imx_int, 0,
658 DRIVER_NAME, sport);
659 if (retval) {
660 free_irq(sport->port.irq, sport);
661 goto error_out1;
662 }
663 }
664
665 /*
666 * Finally, clear and enable interrupts
667 */
668 writel(USR1_RTSD, sport->port.membase + USR1);
669
670 temp = readl(sport->port.membase + UCR1);
671 temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
672
673 if (USE_IRDA(sport)) {
674 temp |= UCR1_IREN;
675 temp &= ~(UCR1_RTSDEN);
676 }
677
678 writel(temp, sport->port.membase + UCR1);
679
680 temp = readl(sport->port.membase + UCR2);
681 temp |= (UCR2_RXEN | UCR2_TXEN);
682 writel(temp, sport->port.membase + UCR2);
683
684 if (USE_IRDA(sport)) {
685 /* clear RX-FIFO */
686 int i = 64;
687 while ((--i > 0) &&
688 (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
689 barrier();
690 }
691 }
692
693 if (!cpu_is_mx1()) {
694 temp = readl(sport->port.membase + UCR3);
695 temp |= MX2_UCR3_RXDMUXSEL;
696 writel(temp, sport->port.membase + UCR3);
697 }
698
699 if (USE_IRDA(sport)) {
700 temp = readl(sport->port.membase + UCR4);
701 if (sport->irda_inv_rx)
702 temp |= UCR4_INVR;
703 else
704 temp &= ~(UCR4_INVR);
705 writel(temp | UCR4_DREN, sport->port.membase + UCR4);
706
707 temp = readl(sport->port.membase + UCR3);
708 if (sport->irda_inv_tx)
709 temp |= UCR3_INVT;
710 else
711 temp &= ~(UCR3_INVT);
712 writel(temp, sport->port.membase + UCR3);
713 }
714
715 /*
716 * Enable modem status interrupts
717 */
718 spin_lock_irqsave(&sport->port.lock,flags);
719 imx_enable_ms(&sport->port);
720 spin_unlock_irqrestore(&sport->port.lock,flags);
721
722 if (USE_IRDA(sport)) {
723 struct imxuart_platform_data *pdata;
724 pdata = sport->port.dev->platform_data;
725 sport->irda_inv_rx = pdata->irda_inv_rx;
726 sport->irda_inv_tx = pdata->irda_inv_tx;
727 sport->trcv_delay = pdata->transceiver_delay;
728 if (pdata->irda_enable)
729 pdata->irda_enable(1);
730 }
731
732 return 0;
733
734error_out3:
735 if (sport->txirq)
736 free_irq(sport->txirq, sport);
737error_out2:
738 if (sport->rxirq)
739 free_irq(sport->rxirq, sport);
740error_out1:
741 return retval;
742}
743
744static void imx_shutdown(struct uart_port *port)
745{
746 struct imx_port *sport = (struct imx_port *)port;
747 unsigned long temp;
748
749 temp = readl(sport->port.membase + UCR2);
750 temp &= ~(UCR2_TXEN);
751 writel(temp, sport->port.membase + UCR2);
752
753 if (USE_IRDA(sport)) {
754 struct imxuart_platform_data *pdata;
755 pdata = sport->port.dev->platform_data;
756 if (pdata->irda_enable)
757 pdata->irda_enable(0);
758 }
759
760 /*
761 * Stop our timer.
762 */
763 del_timer_sync(&sport->timer);
764
765 /*
766 * Free the interrupts
767 */
768 if (sport->txirq > 0) {
769 if (!USE_IRDA(sport))
770 free_irq(sport->rtsirq, sport);
771 free_irq(sport->txirq, sport);
772 free_irq(sport->rxirq, sport);
773 } else
774 free_irq(sport->port.irq, sport);
775
776 /*
777 * Disable all interrupts, port and break condition.
778 */
779
780 temp = readl(sport->port.membase + UCR1);
781 temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
782 if (USE_IRDA(sport))
783 temp &= ~(UCR1_IREN);
784
785 writel(temp, sport->port.membase + UCR1);
786}
787
788static void
789imx_set_termios(struct uart_port *port, struct ktermios *termios,
790 struct ktermios *old)
791{
792 struct imx_port *sport = (struct imx_port *)port;
793 unsigned long flags;
794 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
795 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
796 unsigned int div, ufcr;
797 unsigned long num, denom;
798 uint64_t tdiv64;
799
800 /*
801 * If we don't support modem control lines, don't allow
802 * these to be set.
803 */
804 if (0) {
805 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
806 termios->c_cflag |= CLOCAL;
807 }
808
809 /*
810 * We only support CS7 and CS8.
811 */
812 while ((termios->c_cflag & CSIZE) != CS7 &&
813 (termios->c_cflag & CSIZE) != CS8) {
814 termios->c_cflag &= ~CSIZE;
815 termios->c_cflag |= old_csize;
816 old_csize = CS8;
817 }
818
819 if ((termios->c_cflag & CSIZE) == CS8)
820 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
821 else
822 ucr2 = UCR2_SRST | UCR2_IRTS;
823
824 if (termios->c_cflag & CRTSCTS) {
825 if( sport->have_rtscts ) {
826 ucr2 &= ~UCR2_IRTS;
827 ucr2 |= UCR2_CTSC;
828 } else {
829 termios->c_cflag &= ~CRTSCTS;
830 }
831 }
832
833 if (termios->c_cflag & CSTOPB)
834 ucr2 |= UCR2_STPB;
835 if (termios->c_cflag & PARENB) {
836 ucr2 |= UCR2_PREN;
837 if (termios->c_cflag & PARODD)
838 ucr2 |= UCR2_PROE;
839 }
840
841 /*
842 * Ask the core to calculate the divisor for us.
843 */
844 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
845 quot = uart_get_divisor(port, baud);
846
847 spin_lock_irqsave(&sport->port.lock, flags);
848
849 sport->port.read_status_mask = 0;
850 if (termios->c_iflag & INPCK)
851 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
852 if (termios->c_iflag & (BRKINT | PARMRK))
853 sport->port.read_status_mask |= URXD_BRK;
854
855 /*
856 * Characters to ignore
857 */
858 sport->port.ignore_status_mask = 0;
859 if (termios->c_iflag & IGNPAR)
860 sport->port.ignore_status_mask |= URXD_PRERR;
861 if (termios->c_iflag & IGNBRK) {
862 sport->port.ignore_status_mask |= URXD_BRK;
863 /*
864 * If we're ignoring parity and break indicators,
865 * ignore overruns too (for real raw support).
866 */
867 if (termios->c_iflag & IGNPAR)
868 sport->port.ignore_status_mask |= URXD_OVRRUN;
869 }
870
871 del_timer_sync(&sport->timer);
872
873 /*
874 * Update the per-port timeout.
875 */
876 uart_update_timeout(port, termios->c_cflag, baud);
877
878 /*
879 * disable interrupts and drain transmitter
880 */
881 old_ucr1 = readl(sport->port.membase + UCR1);
882 writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
883 sport->port.membase + UCR1);
884
885 while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
886 barrier();
887
888 /* then, disable everything */
889 old_txrxen = readl(sport->port.membase + UCR2);
890 writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
891 sport->port.membase + UCR2);
892 old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
893
894 if (USE_IRDA(sport)) {
895 /*
896 * use maximum available submodule frequency to
897 * avoid missing short pulses due to low sampling rate
898 */
899 div = 1;
900 } else {
901 div = sport->port.uartclk / (baud * 16);
902 if (div > 7)
903 div = 7;
904 if (!div)
905 div = 1;
906 }
907
908 rational_best_approximation(16 * div * baud, sport->port.uartclk,
909 1 << 16, 1 << 16, &num, &denom);
910
911 tdiv64 = sport->port.uartclk;
912 tdiv64 *= num;
913 do_div(tdiv64, denom * 16 * div);
914 tty_termios_encode_baud_rate(termios,
915 (speed_t)tdiv64, (speed_t)tdiv64);
916
917 num -= 1;
918 denom -= 1;
919
920 ufcr = readl(sport->port.membase + UFCR);
921 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
922 writel(ufcr, sport->port.membase + UFCR);
923
924 writel(num, sport->port.membase + UBIR);
925 writel(denom, sport->port.membase + UBMR);
926
927 if (!cpu_is_mx1())
928 writel(sport->port.uartclk / div / 1000,
929 sport->port.membase + MX2_ONEMS);
930
931 writel(old_ucr1, sport->port.membase + UCR1);
932
933 /* set the parity, stop bits and data size */
934 writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
935
936 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
937 imx_enable_ms(&sport->port);
938
939 spin_unlock_irqrestore(&sport->port.lock, flags);
940}
941
942static const char *imx_type(struct uart_port *port)
943{
944 struct imx_port *sport = (struct imx_port *)port;
945
946 return sport->port.type == PORT_IMX ? "IMX" : NULL;
947}
948
949/*
950 * Release the memory region(s) being used by 'port'.
951 */
952static void imx_release_port(struct uart_port *port)
953{
954 struct platform_device *pdev = to_platform_device(port->dev);
955 struct resource *mmres;
956
957 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
958 release_mem_region(mmres->start, mmres->end - mmres->start + 1);
959}
960
961/*
962 * Request the memory region(s) being used by 'port'.
963 */
964static int imx_request_port(struct uart_port *port)
965{
966 struct platform_device *pdev = to_platform_device(port->dev);
967 struct resource *mmres;
968 void *ret;
969
970 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
971 if (!mmres)
972 return -ENODEV;
973
974 ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
975 "imx-uart");
976
977 return ret ? 0 : -EBUSY;
978}
979
980/*
981 * Configure/autoconfigure the port.
982 */
983static void imx_config_port(struct uart_port *port, int flags)
984{
985 struct imx_port *sport = (struct imx_port *)port;
986
987 if (flags & UART_CONFIG_TYPE &&
988 imx_request_port(&sport->port) == 0)
989 sport->port.type = PORT_IMX;
990}
991
992/*
993 * Verify the new serial_struct (for TIOCSSERIAL).
994 * The only change we allow are to the flags and type, and
995 * even then only between PORT_IMX and PORT_UNKNOWN
996 */
997static int
998imx_verify_port(struct uart_port *port, struct serial_struct *ser)
999{
1000 struct imx_port *sport = (struct imx_port *)port;
1001 int ret = 0;
1002
1003 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1004 ret = -EINVAL;
1005 if (sport->port.irq != ser->irq)
1006 ret = -EINVAL;
1007 if (ser->io_type != UPIO_MEM)
1008 ret = -EINVAL;
1009 if (sport->port.uartclk / 16 != ser->baud_base)
1010 ret = -EINVAL;
1011 if ((void *)sport->port.mapbase != ser->iomem_base)
1012 ret = -EINVAL;
1013 if (sport->port.iobase != ser->port)
1014 ret = -EINVAL;
1015 if (ser->hub6 != 0)
1016 ret = -EINVAL;
1017 return ret;
1018}
1019
1020static struct uart_ops imx_pops = {
1021 .tx_empty = imx_tx_empty,
1022 .set_mctrl = imx_set_mctrl,
1023 .get_mctrl = imx_get_mctrl,
1024 .stop_tx = imx_stop_tx,
1025 .start_tx = imx_start_tx,
1026 .stop_rx = imx_stop_rx,
1027 .enable_ms = imx_enable_ms,
1028 .break_ctl = imx_break_ctl,
1029 .startup = imx_startup,
1030 .shutdown = imx_shutdown,
1031 .set_termios = imx_set_termios,
1032 .type = imx_type,
1033 .release_port = imx_release_port,
1034 .request_port = imx_request_port,
1035 .config_port = imx_config_port,
1036 .verify_port = imx_verify_port,
1037};
1038
1039static struct imx_port *imx_ports[UART_NR];
1040
1041#ifdef CONFIG_SERIAL_IMX_CONSOLE
1042static void imx_console_putchar(struct uart_port *port, int ch)
1043{
1044 struct imx_port *sport = (struct imx_port *)port;
1045
1046 while (readl(sport->port.membase + UTS) & UTS_TXFULL)
1047 barrier();
1048
1049 writel(ch, sport->port.membase + URTX0);
1050}
1051
1052/*
1053 * Interrupts are disabled on entering
1054 */
1055static void
1056imx_console_write(struct console *co, const char *s, unsigned int count)
1057{
1058 struct imx_port *sport = imx_ports[co->index];
1059 unsigned int old_ucr1, old_ucr2, ucr1;
1060
1061 /*
1062 * First, save UCR1/2 and then disable interrupts
1063 */
1064 ucr1 = old_ucr1 = readl(sport->port.membase + UCR1);
1065 old_ucr2 = readl(sport->port.membase + UCR2);
1066
1067 if (cpu_is_mx1())
1068 ucr1 |= MX1_UCR1_UARTCLKEN;
1069 ucr1 |= UCR1_UARTEN;
1070 ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
1071
1072 writel(ucr1, sport->port.membase + UCR1);
1073
1074 writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
1075
1076 uart_console_write(&sport->port, s, count, imx_console_putchar);
1077
1078 /*
1079 * Finally, wait for transmitter to become empty
1080 * and restore UCR1/2
1081 */
1082 while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
1083
1084 writel(old_ucr1, sport->port.membase + UCR1);
1085 writel(old_ucr2, sport->port.membase + UCR2);
1086}
1087
1088/*
1089 * If the port was already initialised (eg, by a boot loader),
1090 * try to determine the current setup.
1091 */
1092static void __init
1093imx_console_get_options(struct imx_port *sport, int *baud,
1094 int *parity, int *bits)
1095{
1096
1097 if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
1098 /* ok, the port was enabled */
1099 unsigned int ucr2, ubir,ubmr, uartclk;
1100 unsigned int baud_raw;
1101 unsigned int ucfr_rfdiv;
1102
1103 ucr2 = readl(sport->port.membase + UCR2);
1104
1105 *parity = 'n';
1106 if (ucr2 & UCR2_PREN) {
1107 if (ucr2 & UCR2_PROE)
1108 *parity = 'o';
1109 else
1110 *parity = 'e';
1111 }
1112
1113 if (ucr2 & UCR2_WS)
1114 *bits = 8;
1115 else
1116 *bits = 7;
1117
1118 ubir = readl(sport->port.membase + UBIR) & 0xffff;
1119 ubmr = readl(sport->port.membase + UBMR) & 0xffff;
1120
1121 ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
1122 if (ucfr_rfdiv == 6)
1123 ucfr_rfdiv = 7;
1124 else
1125 ucfr_rfdiv = 6 - ucfr_rfdiv;
1126
1127 uartclk = clk_get_rate(sport->clk);
1128 uartclk /= ucfr_rfdiv;
1129
1130 { /*
1131 * The next code provides exact computation of
1132 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
1133 * without need of float support or long long division,
1134 * which would be required to prevent 32bit arithmetic overflow
1135 */
1136 unsigned int mul = ubir + 1;
1137 unsigned int div = 16 * (ubmr + 1);
1138 unsigned int rem = uartclk % div;
1139
1140 baud_raw = (uartclk / div) * mul;
1141 baud_raw += (rem * mul + div / 2) / div;
1142 *baud = (baud_raw + 50) / 100 * 100;
1143 }
1144
1145 if(*baud != baud_raw)
1146 printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
1147 baud_raw, *baud);
1148 }
1149}
1150
1151static int __init
1152imx_console_setup(struct console *co, char *options)
1153{
1154 struct imx_port *sport;
1155 int baud = 9600;
1156 int bits = 8;
1157 int parity = 'n';
1158 int flow = 'n';
1159
1160 /*
1161 * Check whether an invalid uart number has been specified, and
1162 * if so, search for the first available port that does have
1163 * console support.
1164 */
1165 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
1166 co->index = 0;
1167 sport = imx_ports[co->index];
1168 if(sport == NULL)
1169 return -ENODEV;
1170
1171 if (options)
1172 uart_parse_options(options, &baud, &parity, &bits, &flow);
1173 else
1174 imx_console_get_options(sport, &baud, &parity, &bits);
1175
1176 imx_setup_ufcr(sport, 0);
1177
1178 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
1179}
1180
1181static struct uart_driver imx_reg;
1182static struct console imx_console = {
1183 .name = DEV_NAME,
1184 .write = imx_console_write,
1185 .device = uart_console_device,
1186 .setup = imx_console_setup,
1187 .flags = CON_PRINTBUFFER,
1188 .index = -1,
1189 .data = &imx_reg,
1190};
1191
1192#define IMX_CONSOLE &imx_console
1193#else
1194#define IMX_CONSOLE NULL
1195#endif
1196
1197static struct uart_driver imx_reg = {
1198 .owner = THIS_MODULE,
1199 .driver_name = DRIVER_NAME,
1200 .dev_name = DEV_NAME,
1201 .major = SERIAL_IMX_MAJOR,
1202 .minor = MINOR_START,
1203 .nr = ARRAY_SIZE(imx_ports),
1204 .cons = IMX_CONSOLE,
1205};
1206
1207static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
1208{
1209 struct imx_port *sport = platform_get_drvdata(dev);
1210
1211 if (sport)
1212 uart_suspend_port(&imx_reg, &sport->port);
1213
1214 return 0;
1215}
1216
1217static int serial_imx_resume(struct platform_device *dev)
1218{
1219 struct imx_port *sport = platform_get_drvdata(dev);
1220
1221 if (sport)
1222 uart_resume_port(&imx_reg, &sport->port);
1223
1224 return 0;
1225}
1226
1227static int serial_imx_probe(struct platform_device *pdev)
1228{
1229 struct imx_port *sport;
1230 struct imxuart_platform_data *pdata;
1231 void __iomem *base;
1232 int ret = 0;
1233 struct resource *res;
1234
1235 sport = kzalloc(sizeof(*sport), GFP_KERNEL);
1236 if (!sport)
1237 return -ENOMEM;
1238
1239 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1240 if (!res) {
1241 ret = -ENODEV;
1242 goto free;
1243 }
1244
1245 base = ioremap(res->start, PAGE_SIZE);
1246 if (!base) {
1247 ret = -ENOMEM;
1248 goto free;
1249 }
1250
1251 sport->port.dev = &pdev->dev;
1252 sport->port.mapbase = res->start;
1253 sport->port.membase = base;
1254 sport->port.type = PORT_IMX,
1255 sport->port.iotype = UPIO_MEM;
1256 sport->port.irq = platform_get_irq(pdev, 0);
1257 sport->rxirq = platform_get_irq(pdev, 0);
1258 sport->txirq = platform_get_irq(pdev, 1);
1259 sport->rtsirq = platform_get_irq(pdev, 2);
1260 sport->port.fifosize = 32;
1261 sport->port.ops = &imx_pops;
1262 sport->port.flags = UPF_BOOT_AUTOCONF;
1263 sport->port.line = pdev->id;
1264 init_timer(&sport->timer);
1265 sport->timer.function = imx_timeout;
1266 sport->timer.data = (unsigned long)sport;
1267
1268 sport->clk = clk_get(&pdev->dev, "uart");
1269 if (IS_ERR(sport->clk)) {
1270 ret = PTR_ERR(sport->clk);
1271 goto unmap;
1272 }
1273 clk_enable(sport->clk);
1274
1275 sport->port.uartclk = clk_get_rate(sport->clk);
1276
1277 imx_ports[pdev->id] = sport;
1278
1279 pdata = pdev->dev.platform_data;
1280 if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
1281 sport->have_rtscts = 1;
1282
1283#ifdef CONFIG_IRDA
1284 if (pdata && (pdata->flags & IMXUART_IRDA))
1285 sport->use_irda = 1;
1286#endif
1287
1288 if (pdata && pdata->init) {
1289 ret = pdata->init(pdev);
1290 if (ret)
1291 goto clkput;
1292 }
1293
1294 ret = uart_add_one_port(&imx_reg, &sport->port);
1295 if (ret)
1296 goto deinit;
1297 platform_set_drvdata(pdev, &sport->port);
1298
1299 return 0;
1300deinit:
1301 if (pdata && pdata->exit)
1302 pdata->exit(pdev);
1303clkput:
1304 clk_put(sport->clk);
1305 clk_disable(sport->clk);
1306unmap:
1307 iounmap(sport->port.membase);
1308free:
1309 kfree(sport);
1310
1311 return ret;
1312}
1313
1314static int serial_imx_remove(struct platform_device *pdev)
1315{
1316 struct imxuart_platform_data *pdata;
1317 struct imx_port *sport = platform_get_drvdata(pdev);
1318
1319 pdata = pdev->dev.platform_data;
1320
1321 platform_set_drvdata(pdev, NULL);
1322
1323 if (sport) {
1324 uart_remove_one_port(&imx_reg, &sport->port);
1325 clk_put(sport->clk);
1326 }
1327
1328 clk_disable(sport->clk);
1329
1330 if (pdata && pdata->exit)
1331 pdata->exit(pdev);
1332
1333 iounmap(sport->port.membase);
1334 kfree(sport);
1335
1336 return 0;
1337}
1338
1339static struct platform_driver serial_imx_driver = {
1340 .probe = serial_imx_probe,
1341 .remove = serial_imx_remove,
1342
1343 .suspend = serial_imx_suspend,
1344 .resume = serial_imx_resume,
1345 .driver = {
1346 .name = "imx-uart",
1347 .owner = THIS_MODULE,
1348 },
1349};
1350
1351static int __init imx_serial_init(void)
1352{
1353 int ret;
1354
1355 printk(KERN_INFO "Serial: IMX driver\n");
1356
1357 ret = uart_register_driver(&imx_reg);
1358 if (ret)
1359 return ret;
1360
1361 ret = platform_driver_register(&serial_imx_driver);
1362 if (ret != 0)
1363 uart_unregister_driver(&imx_reg);
1364
1365 return 0;
1366}
1367
1368static void __exit imx_serial_exit(void)
1369{
1370 platform_driver_unregister(&serial_imx_driver);
1371 uart_unregister_driver(&imx_reg);
1372}
1373
1374module_init(imx_serial_init);
1375module_exit(imx_serial_exit);
1376
1377MODULE_AUTHOR("Sascha Hauer");
1378MODULE_DESCRIPTION("IMX generic serial port driver");
1379MODULE_LICENSE("GPL");
1380MODULE_ALIAS("platform:imx-uart");
diff --git a/drivers/tty/serial/ioc3_serial.c b/drivers/tty/serial/ioc3_serial.c
new file mode 100644
index 000000000000..ee43efc7bdcc
--- /dev/null
+++ b/drivers/tty/serial/ioc3_serial.c
@@ -0,0 +1,2199 @@
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) 2005 Silicon Graphics, Inc. All Rights Reserved.
7 */
8
9/*
10 * This file contains a module version of the ioc3 serial driver. This
11 * includes all the support functions needed (support functions, etc.)
12 * and the serial driver itself.
13 */
14#include <linux/errno.h>
15#include <linux/tty.h>
16#include <linux/serial.h>
17#include <linux/circ_buf.h>
18#include <linux/serial_reg.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21#include <linux/serial_core.h>
22#include <linux/ioc3.h>
23#include <linux/slab.h>
24
25/*
26 * Interesting things about the ioc3
27 */
28
29#define LOGICAL_PORTS 2 /* rs232(0) and rs422(1) */
30#define PORTS_PER_CARD 2
31#define LOGICAL_PORTS_PER_CARD (PORTS_PER_CARD * LOGICAL_PORTS)
32#define MAX_CARDS 8
33#define MAX_LOGICAL_PORTS (LOGICAL_PORTS_PER_CARD * MAX_CARDS)
34
35/* determine given the sio_ir what port it applies to */
36#define GET_PORT_FROM_SIO_IR(_x) (_x & SIO_IR_SA) ? 0 : 1
37
38
39/*
40 * we have 2 logical ports (rs232, rs422) for each physical port
41 * evens are rs232, odds are rs422
42 */
43#define GET_PHYSICAL_PORT(_x) ((_x) >> 1)
44#define GET_LOGICAL_PORT(_x) ((_x) & 1)
45#define IS_PHYSICAL_PORT(_x) !((_x) & 1)
46#define IS_RS232(_x) !((_x) & 1)
47
48static unsigned int Num_of_ioc3_cards;
49static unsigned int Submodule_slot;
50
51/* defining this will get you LOTS of great debug info */
52//#define DEBUG_INTERRUPTS
53#define DPRINT_CONFIG(_x...) ;
54//#define DPRINT_CONFIG(_x...) printk _x
55#define NOT_PROGRESS() ;
56//#define NOT_PROGRESS() printk("%s : fails %d\n", __func__, __LINE__)
57
58/* number of characters we want to transmit to the lower level at a time */
59#define MAX_CHARS 256
60#define FIFO_SIZE (MAX_CHARS-1) /* it's a uchar */
61
62/* Device name we're using */
63#define DEVICE_NAME "ttySIOC"
64#define DEVICE_MAJOR 204
65#define DEVICE_MINOR 116
66
67/* flags for next_char_state */
68#define NCS_BREAK 0x1
69#define NCS_PARITY 0x2
70#define NCS_FRAMING 0x4
71#define NCS_OVERRUN 0x8
72
73/* cause we need SOME parameters ... */
74#define MIN_BAUD_SUPPORTED 1200
75#define MAX_BAUD_SUPPORTED 115200
76
77/* protocol types supported */
78#define PROTO_RS232 0
79#define PROTO_RS422 1
80
81/* Notification types */
82#define N_DATA_READY 0x01
83#define N_OUTPUT_LOWAT 0x02
84#define N_BREAK 0x04
85#define N_PARITY_ERROR 0x08
86#define N_FRAMING_ERROR 0x10
87#define N_OVERRUN_ERROR 0x20
88#define N_DDCD 0x40
89#define N_DCTS 0x80
90
91#define N_ALL_INPUT (N_DATA_READY | N_BREAK \
92 | N_PARITY_ERROR | N_FRAMING_ERROR \
93 | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
94
95#define N_ALL_OUTPUT N_OUTPUT_LOWAT
96
97#define N_ALL_ERRORS (N_PARITY_ERROR | N_FRAMING_ERROR \
98 | N_OVERRUN_ERROR)
99
100#define N_ALL (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK \
101 | N_PARITY_ERROR | N_FRAMING_ERROR \
102 | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
103
104#define SER_CLK_SPEED(prediv) ((22000000 << 1) / prediv)
105#define SER_DIVISOR(x, clk) (((clk) + (x) * 8) / ((x) * 16))
106#define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
107
108/* Some masks */
109#define LCR_MASK_BITS_CHAR (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
110 | UART_LCR_WLEN7 | UART_LCR_WLEN8)
111#define LCR_MASK_STOP_BITS (UART_LCR_STOP)
112
113#define PENDING(_a, _p) (readl(&(_p)->vma->sio_ir) & (_a)->ic_enable)
114
115#define RING_BUF_SIZE 4096
116#define BUF_SIZE_BIT SBBR_L_SIZE
117#define PROD_CONS_MASK PROD_CONS_PTR_4K
118
119#define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
120
121/* driver specific - one per card */
122struct ioc3_card {
123 struct {
124 /* uart ports are allocated here */
125 struct uart_port icp_uart_port[LOGICAL_PORTS];
126 /* the ioc3_port used for this port */
127 struct ioc3_port *icp_port;
128 } ic_port[PORTS_PER_CARD];
129 /* currently enabled interrupts */
130 uint32_t ic_enable;
131};
132
133/* Local port info for each IOC3 serial port */
134struct ioc3_port {
135 /* handy reference material */
136 struct uart_port *ip_port;
137 struct ioc3_card *ip_card;
138 struct ioc3_driver_data *ip_idd;
139 struct ioc3_submodule *ip_is;
140
141 /* pci mem addresses for this port */
142 struct ioc3_serialregs __iomem *ip_serial_regs;
143 struct ioc3_uartregs __iomem *ip_uart_regs;
144
145 /* Ring buffer page for this port */
146 dma_addr_t ip_dma_ringbuf;
147 /* vaddr of ring buffer */
148 struct ring_buffer *ip_cpu_ringbuf;
149
150 /* Rings for this port */
151 struct ring *ip_inring;
152 struct ring *ip_outring;
153
154 /* Hook to port specific values */
155 struct port_hooks *ip_hooks;
156
157 spinlock_t ip_lock;
158
159 /* Various rx/tx parameters */
160 int ip_baud;
161 int ip_tx_lowat;
162 int ip_rx_timeout;
163
164 /* Copy of notification bits */
165 int ip_notify;
166
167 /* Shadow copies of various registers so we don't need to PIO
168 * read them constantly
169 */
170 uint32_t ip_sscr;
171 uint32_t ip_tx_prod;
172 uint32_t ip_rx_cons;
173 unsigned char ip_flags;
174};
175
176/* tx low water mark. We need to notify the driver whenever tx is getting
177 * close to empty so it can refill the tx buffer and keep things going.
178 * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
179 * have no trouble getting in more chars in time (I certainly hope so).
180 */
181#define TX_LOWAT_LATENCY 1000
182#define TX_LOWAT_HZ (1000000 / TX_LOWAT_LATENCY)
183#define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
184
185/* Flags per port */
186#define INPUT_HIGH 0x01
187 /* used to signify that we have turned off the rx_high
188 * temporarily - we need to drain the fifo and don't
189 * want to get blasted with interrupts.
190 */
191#define DCD_ON 0x02
192 /* DCD state is on */
193#define LOWAT_WRITTEN 0x04
194#define READ_ABORTED 0x08
195 /* the read was aborted - used to avaoid infinate looping
196 * in the interrupt handler
197 */
198#define INPUT_ENABLE 0x10
199
200/* Since each port has different register offsets and bitmasks
201 * for everything, we'll store those that we need in tables so we
202 * don't have to be constantly checking the port we are dealing with.
203 */
204struct port_hooks {
205 uint32_t intr_delta_dcd;
206 uint32_t intr_delta_cts;
207 uint32_t intr_tx_mt;
208 uint32_t intr_rx_timer;
209 uint32_t intr_rx_high;
210 uint32_t intr_tx_explicit;
211 uint32_t intr_clear;
212 uint32_t intr_all;
213 char rs422_select_pin;
214};
215
216static struct port_hooks hooks_array[PORTS_PER_CARD] = {
217 /* values for port A */
218 {
219 .intr_delta_dcd = SIO_IR_SA_DELTA_DCD,
220 .intr_delta_cts = SIO_IR_SA_DELTA_CTS,
221 .intr_tx_mt = SIO_IR_SA_TX_MT,
222 .intr_rx_timer = SIO_IR_SA_RX_TIMER,
223 .intr_rx_high = SIO_IR_SA_RX_HIGH,
224 .intr_tx_explicit = SIO_IR_SA_TX_EXPLICIT,
225 .intr_clear = (SIO_IR_SA_TX_MT | SIO_IR_SA_RX_FULL
226 | SIO_IR_SA_RX_HIGH
227 | SIO_IR_SA_RX_TIMER
228 | SIO_IR_SA_DELTA_DCD
229 | SIO_IR_SA_DELTA_CTS
230 | SIO_IR_SA_INT
231 | SIO_IR_SA_TX_EXPLICIT
232 | SIO_IR_SA_MEMERR),
233 .intr_all = SIO_IR_SA,
234 .rs422_select_pin = GPPR_UARTA_MODESEL_PIN,
235 },
236
237 /* values for port B */
238 {
239 .intr_delta_dcd = SIO_IR_SB_DELTA_DCD,
240 .intr_delta_cts = SIO_IR_SB_DELTA_CTS,
241 .intr_tx_mt = SIO_IR_SB_TX_MT,
242 .intr_rx_timer = SIO_IR_SB_RX_TIMER,
243 .intr_rx_high = SIO_IR_SB_RX_HIGH,
244 .intr_tx_explicit = SIO_IR_SB_TX_EXPLICIT,
245 .intr_clear = (SIO_IR_SB_TX_MT | SIO_IR_SB_RX_FULL
246 | SIO_IR_SB_RX_HIGH
247 | SIO_IR_SB_RX_TIMER
248 | SIO_IR_SB_DELTA_DCD
249 | SIO_IR_SB_DELTA_CTS
250 | SIO_IR_SB_INT
251 | SIO_IR_SB_TX_EXPLICIT
252 | SIO_IR_SB_MEMERR),
253 .intr_all = SIO_IR_SB,
254 .rs422_select_pin = GPPR_UARTB_MODESEL_PIN,
255 }
256};
257
258struct ring_entry {
259 union {
260 struct {
261 uint32_t alldata;
262 uint32_t allsc;
263 } all;
264 struct {
265 char data[4]; /* data bytes */
266 char sc[4]; /* status/control */
267 } s;
268 } u;
269};
270
271/* Test the valid bits in any of the 4 sc chars using "allsc" member */
272#define RING_ANY_VALID \
273 ((uint32_t)(RXSB_MODEM_VALID | RXSB_DATA_VALID) * 0x01010101)
274
275#define ring_sc u.s.sc
276#define ring_data u.s.data
277#define ring_allsc u.all.allsc
278
279/* Number of entries per ring buffer. */
280#define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
281
282/* An individual ring */
283struct ring {
284 struct ring_entry entries[ENTRIES_PER_RING];
285};
286
287/* The whole enchilada */
288struct ring_buffer {
289 struct ring TX_A;
290 struct ring RX_A;
291 struct ring TX_B;
292 struct ring RX_B;
293};
294
295/* Get a ring from a port struct */
296#define RING(_p, _wh) &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
297
298/* for Infinite loop detection */
299#define MAXITER 10000000
300
301
302/**
303 * set_baud - Baud rate setting code
304 * @port: port to set
305 * @baud: baud rate to use
306 */
307static int set_baud(struct ioc3_port *port, int baud)
308{
309 int divisor;
310 int actual_baud;
311 int diff;
312 int lcr, prediv;
313 struct ioc3_uartregs __iomem *uart;
314
315 for (prediv = 6; prediv < 64; prediv++) {
316 divisor = SER_DIVISOR(baud, SER_CLK_SPEED(prediv));
317 if (!divisor)
318 continue; /* invalid divisor */
319 actual_baud = DIVISOR_TO_BAUD(divisor, SER_CLK_SPEED(prediv));
320
321 diff = actual_baud - baud;
322 if (diff < 0)
323 diff = -diff;
324
325 /* if we're within 1% we've found a match */
326 if (diff * 100 <= actual_baud)
327 break;
328 }
329
330 /* if the above loop completed, we didn't match
331 * the baud rate. give up.
332 */
333 if (prediv == 64) {
334 NOT_PROGRESS();
335 return 1;
336 }
337
338 uart = port->ip_uart_regs;
339 lcr = readb(&uart->iu_lcr);
340
341 writeb(lcr | UART_LCR_DLAB, &uart->iu_lcr);
342 writeb((unsigned char)divisor, &uart->iu_dll);
343 writeb((unsigned char)(divisor >> 8), &uart->iu_dlm);
344 writeb((unsigned char)prediv, &uart->iu_scr);
345 writeb((unsigned char)lcr, &uart->iu_lcr);
346
347 return 0;
348}
349
350/**
351 * get_ioc3_port - given a uart port, return the control structure
352 * @the_port: uart port to find
353 */
354static struct ioc3_port *get_ioc3_port(struct uart_port *the_port)
355{
356 struct ioc3_driver_data *idd = dev_get_drvdata(the_port->dev);
357 struct ioc3_card *card_ptr = idd->data[Submodule_slot];
358 int ii, jj;
359
360 if (!card_ptr) {
361 NOT_PROGRESS();
362 return NULL;
363 }
364 for (ii = 0; ii < PORTS_PER_CARD; ii++) {
365 for (jj = 0; jj < LOGICAL_PORTS; jj++) {
366 if (the_port == &card_ptr->ic_port[ii].icp_uart_port[jj])
367 return card_ptr->ic_port[ii].icp_port;
368 }
369 }
370 NOT_PROGRESS();
371 return NULL;
372}
373
374/**
375 * port_init - Initialize the sio and ioc3 hardware for a given port
376 * called per port from attach...
377 * @port: port to initialize
378 */
379static int inline port_init(struct ioc3_port *port)
380{
381 uint32_t sio_cr;
382 struct port_hooks *hooks = port->ip_hooks;
383 struct ioc3_uartregs __iomem *uart;
384 int reset_loop_counter = 0xfffff;
385 struct ioc3_driver_data *idd = port->ip_idd;
386
387 /* Idle the IOC3 serial interface */
388 writel(SSCR_RESET, &port->ip_serial_regs->sscr);
389
390 /* Wait until any pending bus activity for this port has ceased */
391 do {
392 sio_cr = readl(&idd->vma->sio_cr);
393 if (reset_loop_counter-- <= 0) {
394 printk(KERN_WARNING
395 "IOC3 unable to come out of reset"
396 " scr 0x%x\n", sio_cr);
397 return -1;
398 }
399 } while (!(sio_cr & SIO_CR_ARB_DIAG_IDLE) &&
400 (((sio_cr &= SIO_CR_ARB_DIAG) == SIO_CR_ARB_DIAG_TXA)
401 || sio_cr == SIO_CR_ARB_DIAG_TXB
402 || sio_cr == SIO_CR_ARB_DIAG_RXA
403 || sio_cr == SIO_CR_ARB_DIAG_RXB));
404
405 /* Finish reset sequence */
406 writel(0, &port->ip_serial_regs->sscr);
407
408 /* Once RESET is done, reload cached tx_prod and rx_cons values
409 * and set rings to empty by making prod == cons
410 */
411 port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
412 writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
413 port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
414 writel(port->ip_rx_cons | SRCIR_ARM, &port->ip_serial_regs->srcir);
415
416 /* Disable interrupts for this 16550 */
417 uart = port->ip_uart_regs;
418 writeb(0, &uart->iu_lcr);
419 writeb(0, &uart->iu_ier);
420
421 /* Set the default baud */
422 set_baud(port, port->ip_baud);
423
424 /* Set line control to 8 bits no parity */
425 writeb(UART_LCR_WLEN8 | 0, &uart->iu_lcr);
426 /* UART_LCR_STOP == 1 stop */
427
428 /* Enable the FIFOs */
429 writeb(UART_FCR_ENABLE_FIFO, &uart->iu_fcr);
430 /* then reset 16550 FIFOs */
431 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
432 &uart->iu_fcr);
433
434 /* Clear modem control register */
435 writeb(0, &uart->iu_mcr);
436
437 /* Clear deltas in modem status register */
438 writel(0, &port->ip_serial_regs->shadow);
439
440 /* Only do this once per port pair */
441 if (port->ip_hooks == &hooks_array[0]) {
442 unsigned long ring_pci_addr;
443 uint32_t __iomem *sbbr_l, *sbbr_h;
444
445 sbbr_l = &idd->vma->sbbr_l;
446 sbbr_h = &idd->vma->sbbr_h;
447 ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
448 DPRINT_CONFIG(("%s: ring_pci_addr 0x%p\n",
449 __func__, (void *)ring_pci_addr));
450
451 writel((unsigned int)((uint64_t) ring_pci_addr >> 32), sbbr_h);
452 writel((unsigned int)ring_pci_addr | BUF_SIZE_BIT, sbbr_l);
453 }
454
455 /* Set the receive timeout value to 10 msec */
456 writel(SRTR_HZ / 100, &port->ip_serial_regs->srtr);
457
458 /* Set rx threshold, enable DMA */
459 /* Set high water mark at 3/4 of full ring */
460 port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
461
462 /* uart experiences pauses at high baud rate reducing actual
463 * throughput by 10% or so unless we enable high speed polling
464 * XXX when this hardware bug is resolved we should revert to
465 * normal polling speed
466 */
467 port->ip_sscr |= SSCR_HIGH_SPD;
468
469 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
470
471 /* Disable and clear all serial related interrupt bits */
472 port->ip_card->ic_enable &= ~hooks->intr_clear;
473 ioc3_disable(port->ip_is, idd, hooks->intr_clear);
474 ioc3_ack(port->ip_is, idd, hooks->intr_clear);
475 return 0;
476}
477
478/**
479 * enable_intrs - enable interrupts
480 * @port: port to enable
481 * @mask: mask to use
482 */
483static void enable_intrs(struct ioc3_port *port, uint32_t mask)
484{
485 if ((port->ip_card->ic_enable & mask) != mask) {
486 port->ip_card->ic_enable |= mask;
487 ioc3_enable(port->ip_is, port->ip_idd, mask);
488 }
489}
490
491/**
492 * local_open - local open a port
493 * @port: port to open
494 */
495static inline int local_open(struct ioc3_port *port)
496{
497 int spiniter = 0;
498
499 port->ip_flags = INPUT_ENABLE;
500
501 /* Pause the DMA interface if necessary */
502 if (port->ip_sscr & SSCR_DMA_EN) {
503 writel(port->ip_sscr | SSCR_DMA_PAUSE,
504 &port->ip_serial_regs->sscr);
505 while ((readl(&port->ip_serial_regs->sscr)
506 & SSCR_PAUSE_STATE) == 0) {
507 spiniter++;
508 if (spiniter > MAXITER) {
509 NOT_PROGRESS();
510 return -1;
511 }
512 }
513 }
514
515 /* Reset the input fifo. If the uart received chars while the port
516 * was closed and DMA is not enabled, the uart may have a bunch of
517 * chars hanging around in its rx fifo which will not be discarded
518 * by rclr in the upper layer. We must get rid of them here.
519 */
520 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
521 &port->ip_uart_regs->iu_fcr);
522
523 writeb(UART_LCR_WLEN8, &port->ip_uart_regs->iu_lcr);
524 /* UART_LCR_STOP == 1 stop */
525
526 /* Re-enable DMA, set default threshold to intr whenever there is
527 * data available.
528 */
529 port->ip_sscr &= ~SSCR_RX_THRESHOLD;
530 port->ip_sscr |= 1; /* default threshold */
531
532 /* Plug in the new sscr. This implicitly clears the DMA_PAUSE
533 * flag if it was set above
534 */
535 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
536 port->ip_tx_lowat = 1;
537 return 0;
538}
539
540/**
541 * set_rx_timeout - Set rx timeout and threshold values.
542 * @port: port to use
543 * @timeout: timeout value in ticks
544 */
545static inline int set_rx_timeout(struct ioc3_port *port, int timeout)
546{
547 int threshold;
548
549 port->ip_rx_timeout = timeout;
550
551 /* Timeout is in ticks. Let's figure out how many chars we
552 * can receive at the current baud rate in that interval
553 * and set the rx threshold to that amount. There are 4 chars
554 * per ring entry, so we'll divide the number of chars that will
555 * arrive in timeout by 4.
556 * So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
557 */
558 threshold = timeout * port->ip_baud / 4000;
559 if (threshold == 0)
560 threshold = 1; /* otherwise we'll intr all the time! */
561
562 if ((unsigned)threshold > (unsigned)SSCR_RX_THRESHOLD)
563 return 1;
564
565 port->ip_sscr &= ~SSCR_RX_THRESHOLD;
566 port->ip_sscr |= threshold;
567 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
568
569 /* Now set the rx timeout to the given value
570 * again timeout * SRTR_HZ / HZ
571 */
572 timeout = timeout * SRTR_HZ / 100;
573 if (timeout > SRTR_CNT)
574 timeout = SRTR_CNT;
575 writel(timeout, &port->ip_serial_regs->srtr);
576 return 0;
577}
578
579/**
580 * config_port - config the hardware
581 * @port: port to config
582 * @baud: baud rate for the port
583 * @byte_size: data size
584 * @stop_bits: number of stop bits
585 * @parenb: parity enable ?
586 * @parodd: odd parity ?
587 */
588static inline int
589config_port(struct ioc3_port *port,
590 int baud, int byte_size, int stop_bits, int parenb, int parodd)
591{
592 char lcr, sizebits;
593 int spiniter = 0;
594
595 DPRINT_CONFIG(("%s: line %d baud %d byte_size %d stop %d parenb %d "
596 "parodd %d\n",
597 __func__, ((struct uart_port *)port->ip_port)->line,
598 baud, byte_size, stop_bits, parenb, parodd));
599
600 if (set_baud(port, baud))
601 return 1;
602
603 switch (byte_size) {
604 case 5:
605 sizebits = UART_LCR_WLEN5;
606 break;
607 case 6:
608 sizebits = UART_LCR_WLEN6;
609 break;
610 case 7:
611 sizebits = UART_LCR_WLEN7;
612 break;
613 case 8:
614 sizebits = UART_LCR_WLEN8;
615 break;
616 default:
617 return 1;
618 }
619
620 /* Pause the DMA interface if necessary */
621 if (port->ip_sscr & SSCR_DMA_EN) {
622 writel(port->ip_sscr | SSCR_DMA_PAUSE,
623 &port->ip_serial_regs->sscr);
624 while ((readl(&port->ip_serial_regs->sscr)
625 & SSCR_PAUSE_STATE) == 0) {
626 spiniter++;
627 if (spiniter > MAXITER)
628 return -1;
629 }
630 }
631
632 /* Clear relevant fields in lcr */
633 lcr = readb(&port->ip_uart_regs->iu_lcr);
634 lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
635 UART_LCR_PARITY | LCR_MASK_STOP_BITS);
636
637 /* Set byte size in lcr */
638 lcr |= sizebits;
639
640 /* Set parity */
641 if (parenb) {
642 lcr |= UART_LCR_PARITY;
643 if (!parodd)
644 lcr |= UART_LCR_EPAR;
645 }
646
647 /* Set stop bits */
648 if (stop_bits)
649 lcr |= UART_LCR_STOP /* 2 stop bits */ ;
650
651 writeb(lcr, &port->ip_uart_regs->iu_lcr);
652
653 /* Re-enable the DMA interface if necessary */
654 if (port->ip_sscr & SSCR_DMA_EN) {
655 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
656 }
657 port->ip_baud = baud;
658
659 /* When we get within this number of ring entries of filling the
660 * entire ring on tx, place an EXPLICIT intr to generate a lowat
661 * notification when output has drained.
662 */
663 port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
664 if (port->ip_tx_lowat == 0)
665 port->ip_tx_lowat = 1;
666
667 set_rx_timeout(port, 2);
668 return 0;
669}
670
671/**
672 * do_write - Write bytes to the port. Returns the number of bytes
673 * actually written. Called from transmit_chars
674 * @port: port to use
675 * @buf: the stuff to write
676 * @len: how many bytes in 'buf'
677 */
678static inline int do_write(struct ioc3_port *port, char *buf, int len)
679{
680 int prod_ptr, cons_ptr, total = 0;
681 struct ring *outring;
682 struct ring_entry *entry;
683 struct port_hooks *hooks = port->ip_hooks;
684
685 BUG_ON(!(len >= 0));
686
687 prod_ptr = port->ip_tx_prod;
688 cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
689 outring = port->ip_outring;
690
691 /* Maintain a 1-entry red-zone. The ring buffer is full when
692 * (cons - prod) % ring_size is 1. Rather than do this subtraction
693 * in the body of the loop, I'll do it now.
694 */
695 cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
696
697 /* Stuff the bytes into the output */
698 while ((prod_ptr != cons_ptr) && (len > 0)) {
699 int xx;
700
701 /* Get 4 bytes (one ring entry) at a time */
702 entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
703
704 /* Invalidate all entries */
705 entry->ring_allsc = 0;
706
707 /* Copy in some bytes */
708 for (xx = 0; (xx < 4) && (len > 0); xx++) {
709 entry->ring_data[xx] = *buf++;
710 entry->ring_sc[xx] = TXCB_VALID;
711 len--;
712 total++;
713 }
714
715 /* If we are within some small threshold of filling up the
716 * entire ring buffer, we must place an EXPLICIT intr here
717 * to generate a lowat interrupt in case we subsequently
718 * really do fill up the ring and the caller goes to sleep.
719 * No need to place more than one though.
720 */
721 if (!(port->ip_flags & LOWAT_WRITTEN) &&
722 ((cons_ptr - prod_ptr) & PROD_CONS_MASK)
723 <= port->ip_tx_lowat * (int)sizeof(struct ring_entry)) {
724 port->ip_flags |= LOWAT_WRITTEN;
725 entry->ring_sc[0] |= TXCB_INT_WHEN_DONE;
726 }
727
728 /* Go on to next entry */
729 prod_ptr += sizeof(struct ring_entry);
730 prod_ptr &= PROD_CONS_MASK;
731 }
732
733 /* If we sent something, start DMA if necessary */
734 if (total > 0 && !(port->ip_sscr & SSCR_DMA_EN)) {
735 port->ip_sscr |= SSCR_DMA_EN;
736 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
737 }
738
739 /* Store the new producer pointer. If tx is disabled, we stuff the
740 * data into the ring buffer, but we don't actually start tx.
741 */
742 if (!uart_tx_stopped(port->ip_port)) {
743 writel(prod_ptr, &port->ip_serial_regs->stpir);
744
745 /* If we are now transmitting, enable tx_mt interrupt so we
746 * can disable DMA if necessary when the tx finishes.
747 */
748 if (total > 0)
749 enable_intrs(port, hooks->intr_tx_mt);
750 }
751 port->ip_tx_prod = prod_ptr;
752
753 return total;
754}
755
756/**
757 * disable_intrs - disable interrupts
758 * @port: port to enable
759 * @mask: mask to use
760 */
761static inline void disable_intrs(struct ioc3_port *port, uint32_t mask)
762{
763 if (port->ip_card->ic_enable & mask) {
764 ioc3_disable(port->ip_is, port->ip_idd, mask);
765 port->ip_card->ic_enable &= ~mask;
766 }
767}
768
769/**
770 * set_notification - Modify event notification
771 * @port: port to use
772 * @mask: events mask
773 * @set_on: set ?
774 */
775static int set_notification(struct ioc3_port *port, int mask, int set_on)
776{
777 struct port_hooks *hooks = port->ip_hooks;
778 uint32_t intrbits, sscrbits;
779
780 BUG_ON(!mask);
781
782 intrbits = sscrbits = 0;
783
784 if (mask & N_DATA_READY)
785 intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
786 if (mask & N_OUTPUT_LOWAT)
787 intrbits |= hooks->intr_tx_explicit;
788 if (mask & N_DDCD) {
789 intrbits |= hooks->intr_delta_dcd;
790 sscrbits |= SSCR_RX_RING_DCD;
791 }
792 if (mask & N_DCTS)
793 intrbits |= hooks->intr_delta_cts;
794
795 if (set_on) {
796 enable_intrs(port, intrbits);
797 port->ip_notify |= mask;
798 port->ip_sscr |= sscrbits;
799 } else {
800 disable_intrs(port, intrbits);
801 port->ip_notify &= ~mask;
802 port->ip_sscr &= ~sscrbits;
803 }
804
805 /* We require DMA if either DATA_READY or DDCD notification is
806 * currently requested. If neither of these is requested and
807 * there is currently no tx in progress, DMA may be disabled.
808 */
809 if (port->ip_notify & (N_DATA_READY | N_DDCD))
810 port->ip_sscr |= SSCR_DMA_EN;
811 else if (!(port->ip_card->ic_enable & hooks->intr_tx_mt))
812 port->ip_sscr &= ~SSCR_DMA_EN;
813
814 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
815 return 0;
816}
817
818/**
819 * set_mcr - set the master control reg
820 * @the_port: port to use
821 * @mask1: mcr mask
822 * @mask2: shadow mask
823 */
824static inline int set_mcr(struct uart_port *the_port,
825 int mask1, int mask2)
826{
827 struct ioc3_port *port = get_ioc3_port(the_port);
828 uint32_t shadow;
829 int spiniter = 0;
830 char mcr;
831
832 if (!port)
833 return -1;
834
835 /* Pause the DMA interface if necessary */
836 if (port->ip_sscr & SSCR_DMA_EN) {
837 writel(port->ip_sscr | SSCR_DMA_PAUSE,
838 &port->ip_serial_regs->sscr);
839 while ((readl(&port->ip_serial_regs->sscr)
840 & SSCR_PAUSE_STATE) == 0) {
841 spiniter++;
842 if (spiniter > MAXITER)
843 return -1;
844 }
845 }
846 shadow = readl(&port->ip_serial_regs->shadow);
847 mcr = (shadow & 0xff000000) >> 24;
848
849 /* Set new value */
850 mcr |= mask1;
851 shadow |= mask2;
852 writeb(mcr, &port->ip_uart_regs->iu_mcr);
853 writel(shadow, &port->ip_serial_regs->shadow);
854
855 /* Re-enable the DMA interface if necessary */
856 if (port->ip_sscr & SSCR_DMA_EN) {
857 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
858 }
859 return 0;
860}
861
862/**
863 * ioc3_set_proto - set the protocol for the port
864 * @port: port to use
865 * @proto: protocol to use
866 */
867static int ioc3_set_proto(struct ioc3_port *port, int proto)
868{
869 struct port_hooks *hooks = port->ip_hooks;
870
871 switch (proto) {
872 default:
873 case PROTO_RS232:
874 /* Clear the appropriate GIO pin */
875 DPRINT_CONFIG(("%s: rs232\n", __func__));
876 writel(0, (&port->ip_idd->vma->gppr[0]
877 + hooks->rs422_select_pin));
878 break;
879
880 case PROTO_RS422:
881 /* Set the appropriate GIO pin */
882 DPRINT_CONFIG(("%s: rs422\n", __func__));
883 writel(1, (&port->ip_idd->vma->gppr[0]
884 + hooks->rs422_select_pin));
885 break;
886 }
887 return 0;
888}
889
890/**
891 * transmit_chars - upper level write, called with the_port->lock
892 * @the_port: port to write
893 */
894static void transmit_chars(struct uart_port *the_port)
895{
896 int xmit_count, tail, head;
897 int result;
898 char *start;
899 struct tty_struct *tty;
900 struct ioc3_port *port = get_ioc3_port(the_port);
901 struct uart_state *state;
902
903 if (!the_port)
904 return;
905 if (!port)
906 return;
907
908 state = the_port->state;
909 tty = state->port.tty;
910
911 if (uart_circ_empty(&state->xmit) || uart_tx_stopped(the_port)) {
912 /* Nothing to do or hw stopped */
913 set_notification(port, N_ALL_OUTPUT, 0);
914 return;
915 }
916
917 head = state->xmit.head;
918 tail = state->xmit.tail;
919 start = (char *)&state->xmit.buf[tail];
920
921 /* write out all the data or until the end of the buffer */
922 xmit_count = (head < tail) ? (UART_XMIT_SIZE - tail) : (head - tail);
923 if (xmit_count > 0) {
924 result = do_write(port, start, xmit_count);
925 if (result > 0) {
926 /* booking */
927 xmit_count -= result;
928 the_port->icount.tx += result;
929 /* advance the pointers */
930 tail += result;
931 tail &= UART_XMIT_SIZE - 1;
932 state->xmit.tail = tail;
933 start = (char *)&state->xmit.buf[tail];
934 }
935 }
936 if (uart_circ_chars_pending(&state->xmit) < WAKEUP_CHARS)
937 uart_write_wakeup(the_port);
938
939 if (uart_circ_empty(&state->xmit)) {
940 set_notification(port, N_OUTPUT_LOWAT, 0);
941 } else {
942 set_notification(port, N_OUTPUT_LOWAT, 1);
943 }
944}
945
946/**
947 * ioc3_change_speed - change the speed of the port
948 * @the_port: port to change
949 * @new_termios: new termios settings
950 * @old_termios: old termios settings
951 */
952static void
953ioc3_change_speed(struct uart_port *the_port,
954 struct ktermios *new_termios, struct ktermios *old_termios)
955{
956 struct ioc3_port *port = get_ioc3_port(the_port);
957 unsigned int cflag, iflag;
958 int baud;
959 int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
960 struct uart_state *state = the_port->state;
961
962 cflag = new_termios->c_cflag;
963 iflag = new_termios->c_iflag;
964
965 switch (cflag & CSIZE) {
966 case CS5:
967 new_data = 5;
968 break;
969 case CS6:
970 new_data = 6;
971 break;
972 case CS7:
973 new_data = 7;
974 break;
975 case CS8:
976 new_data = 8;
977 break;
978 default:
979 /* cuz we always need a default ... */
980 new_data = 5;
981 break;
982 }
983 if (cflag & CSTOPB) {
984 new_stop = 1;
985 }
986 if (cflag & PARENB) {
987 new_parity_enable = 1;
988 if (cflag & PARODD)
989 new_parity = 1;
990 }
991 baud = uart_get_baud_rate(the_port, new_termios, old_termios,
992 MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
993 DPRINT_CONFIG(("%s: returned baud %d for line %d\n", __func__, baud,
994 the_port->line));
995
996 if (!the_port->fifosize)
997 the_port->fifosize = FIFO_SIZE;
998 uart_update_timeout(the_port, cflag, baud);
999
1000 the_port->ignore_status_mask = N_ALL_INPUT;
1001
1002 state->port.tty->low_latency = 1;
1003
1004 if (iflag & IGNPAR)
1005 the_port->ignore_status_mask &= ~(N_PARITY_ERROR
1006 | N_FRAMING_ERROR);
1007 if (iflag & IGNBRK) {
1008 the_port->ignore_status_mask &= ~N_BREAK;
1009 if (iflag & IGNPAR)
1010 the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
1011 }
1012 if (!(cflag & CREAD)) {
1013 /* ignore everything */
1014 the_port->ignore_status_mask &= ~N_DATA_READY;
1015 }
1016
1017 if (cflag & CRTSCTS) {
1018 /* enable hardware flow control */
1019 port->ip_sscr |= SSCR_HFC_EN;
1020 }
1021 else {
1022 /* disable hardware flow control */
1023 port->ip_sscr &= ~SSCR_HFC_EN;
1024 }
1025 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1026
1027 /* Set the configuration and proper notification call */
1028 DPRINT_CONFIG(("%s : port 0x%p line %d cflag 0%o "
1029 "config_port(baud %d data %d stop %d penable %d "
1030 " parity %d), notification 0x%x\n",
1031 __func__, (void *)port, the_port->line, cflag, baud,
1032 new_data, new_stop, new_parity_enable, new_parity,
1033 the_port->ignore_status_mask));
1034
1035 if ((config_port(port, baud, /* baud */
1036 new_data, /* byte size */
1037 new_stop, /* stop bits */
1038 new_parity_enable, /* set parity */
1039 new_parity)) >= 0) { /* parity 1==odd */
1040 set_notification(port, the_port->ignore_status_mask, 1);
1041 }
1042}
1043
1044/**
1045 * ic3_startup_local - Start up the serial port - returns >= 0 if no errors
1046 * @the_port: Port to operate on
1047 */
1048static inline int ic3_startup_local(struct uart_port *the_port)
1049{
1050 struct ioc3_port *port;
1051
1052 if (!the_port) {
1053 NOT_PROGRESS();
1054 return -1;
1055 }
1056
1057 port = get_ioc3_port(the_port);
1058 if (!port) {
1059 NOT_PROGRESS();
1060 return -1;
1061 }
1062
1063 local_open(port);
1064
1065 /* set the protocol */
1066 ioc3_set_proto(port, IS_RS232(the_port->line) ? PROTO_RS232 :
1067 PROTO_RS422);
1068 return 0;
1069}
1070
1071/*
1072 * ioc3_cb_output_lowat - called when the output low water mark is hit
1073 * @port: port to output
1074 */
1075static void ioc3_cb_output_lowat(struct ioc3_port *port)
1076{
1077 unsigned long pflags;
1078
1079 /* the_port->lock is set on the call here */
1080 if (port->ip_port) {
1081 spin_lock_irqsave(&port->ip_port->lock, pflags);
1082 transmit_chars(port->ip_port);
1083 spin_unlock_irqrestore(&port->ip_port->lock, pflags);
1084 }
1085}
1086
1087/*
1088 * ioc3_cb_post_ncs - called for some basic errors
1089 * @port: port to use
1090 * @ncs: event
1091 */
1092static void ioc3_cb_post_ncs(struct uart_port *the_port, int ncs)
1093{
1094 struct uart_icount *icount;
1095
1096 icount = &the_port->icount;
1097
1098 if (ncs & NCS_BREAK)
1099 icount->brk++;
1100 if (ncs & NCS_FRAMING)
1101 icount->frame++;
1102 if (ncs & NCS_OVERRUN)
1103 icount->overrun++;
1104 if (ncs & NCS_PARITY)
1105 icount->parity++;
1106}
1107
1108/**
1109 * do_read - Read in bytes from the port. Return the number of bytes
1110 * actually read.
1111 * @the_port: port to use
1112 * @buf: place to put the stuff we read
1113 * @len: how big 'buf' is
1114 */
1115
1116static inline int do_read(struct uart_port *the_port, char *buf, int len)
1117{
1118 int prod_ptr, cons_ptr, total;
1119 struct ioc3_port *port = get_ioc3_port(the_port);
1120 struct ring *inring;
1121 struct ring_entry *entry;
1122 struct port_hooks *hooks = port->ip_hooks;
1123 int byte_num;
1124 char *sc;
1125 int loop_counter;
1126
1127 BUG_ON(!(len >= 0));
1128 BUG_ON(!port);
1129
1130 /* There is a nasty timing issue in the IOC3. When the rx_timer
1131 * expires or the rx_high condition arises, we take an interrupt.
1132 * At some point while servicing the interrupt, we read bytes from
1133 * the ring buffer and re-arm the rx_timer. However the rx_timer is
1134 * not started until the first byte is received *after* it is armed,
1135 * and any bytes pending in the rx construction buffers are not drained
1136 * to memory until either there are 4 bytes available or the rx_timer
1137 * expires. This leads to a potential situation where data is left
1138 * in the construction buffers forever - 1 to 3 bytes were received
1139 * after the interrupt was generated but before the rx_timer was
1140 * re-armed. At that point as long as no subsequent bytes are received
1141 * the timer will never be started and the bytes will remain in the
1142 * construction buffer forever. The solution is to execute a DRAIN
1143 * command after rearming the timer. This way any bytes received before
1144 * the DRAIN will be drained to memory, and any bytes received after
1145 * the DRAIN will start the TIMER and be drained when it expires.
1146 * Luckily, this only needs to be done when the DMA buffer is empty
1147 * since there is no requirement that this function return all
1148 * available data as long as it returns some.
1149 */
1150 /* Re-arm the timer */
1151
1152 writel(port->ip_rx_cons | SRCIR_ARM, &port->ip_serial_regs->srcir);
1153
1154 prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
1155 cons_ptr = port->ip_rx_cons;
1156
1157 if (prod_ptr == cons_ptr) {
1158 int reset_dma = 0;
1159
1160 /* Input buffer appears empty, do a flush. */
1161
1162 /* DMA must be enabled for this to work. */
1163 if (!(port->ip_sscr & SSCR_DMA_EN)) {
1164 port->ip_sscr |= SSCR_DMA_EN;
1165 reset_dma = 1;
1166 }
1167
1168 /* Potential race condition: we must reload the srpir after
1169 * issuing the drain command, otherwise we could think the rx
1170 * buffer is empty, then take a very long interrupt, and when
1171 * we come back it's full and we wait forever for the drain to
1172 * complete.
1173 */
1174 writel(port->ip_sscr | SSCR_RX_DRAIN,
1175 &port->ip_serial_regs->sscr);
1176 prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
1177
1178 /* We must not wait for the DRAIN to complete unless there are
1179 * at least 8 bytes (2 ring entries) available to receive the
1180 * data otherwise the DRAIN will never complete and we'll
1181 * deadlock here.
1182 * In fact, to make things easier, I'll just ignore the flush if
1183 * there is any data at all now available.
1184 */
1185 if (prod_ptr == cons_ptr) {
1186 loop_counter = 0;
1187 while (readl(&port->ip_serial_regs->sscr) &
1188 SSCR_RX_DRAIN) {
1189 loop_counter++;
1190 if (loop_counter > MAXITER)
1191 return -1;
1192 }
1193
1194 /* SIGH. We have to reload the prod_ptr *again* since
1195 * the drain may have caused it to change
1196 */
1197 prod_ptr = readl(&port->ip_serial_regs->srpir)
1198 & PROD_CONS_MASK;
1199 }
1200 if (reset_dma) {
1201 port->ip_sscr &= ~SSCR_DMA_EN;
1202 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1203 }
1204 }
1205 inring = port->ip_inring;
1206 port->ip_flags &= ~READ_ABORTED;
1207
1208 total = 0;
1209 loop_counter = 0xfffff; /* to avoid hangs */
1210
1211 /* Grab bytes from the hardware */
1212 while ((prod_ptr != cons_ptr) && (len > 0)) {
1213 entry = (struct ring_entry *)((caddr_t) inring + cons_ptr);
1214
1215 if (loop_counter-- <= 0) {
1216 printk(KERN_WARNING "IOC3 serial: "
1217 "possible hang condition/"
1218 "port stuck on read (line %d).\n",
1219 the_port->line);
1220 break;
1221 }
1222
1223 /* According to the producer pointer, this ring entry
1224 * must contain some data. But if the PIO happened faster
1225 * than the DMA, the data may not be available yet, so let's
1226 * wait until it arrives.
1227 */
1228 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
1229 /* Indicate the read is aborted so we don't disable
1230 * the interrupt thinking that the consumer is
1231 * congested.
1232 */
1233 port->ip_flags |= READ_ABORTED;
1234 len = 0;
1235 break;
1236 }
1237
1238 /* Load the bytes/status out of the ring entry */
1239 for (byte_num = 0; byte_num < 4 && len > 0; byte_num++) {
1240 sc = &(entry->ring_sc[byte_num]);
1241
1242 /* Check for change in modem state or overrun */
1243 if ((*sc & RXSB_MODEM_VALID)
1244 && (port->ip_notify & N_DDCD)) {
1245 /* Notify upper layer if DCD dropped */
1246 if ((port->ip_flags & DCD_ON)
1247 && !(*sc & RXSB_DCD)) {
1248 /* If we have already copied some data,
1249 * return it. We'll pick up the carrier
1250 * drop on the next pass. That way we
1251 * don't throw away the data that has
1252 * already been copied back to
1253 * the caller's buffer.
1254 */
1255 if (total > 0) {
1256 len = 0;
1257 break;
1258 }
1259 port->ip_flags &= ~DCD_ON;
1260
1261 /* Turn off this notification so the
1262 * carrier drop protocol won't see it
1263 * again when it does a read.
1264 */
1265 *sc &= ~RXSB_MODEM_VALID;
1266
1267 /* To keep things consistent, we need
1268 * to update the consumer pointer so
1269 * the next reader won't come in and
1270 * try to read the same ring entries
1271 * again. This must be done here before
1272 * the dcd change.
1273 */
1274
1275 if ((entry->ring_allsc & RING_ANY_VALID)
1276 == 0) {
1277 cons_ptr += (int)sizeof
1278 (struct ring_entry);
1279 cons_ptr &= PROD_CONS_MASK;
1280 }
1281 writel(cons_ptr,
1282 &port->ip_serial_regs->srcir);
1283 port->ip_rx_cons = cons_ptr;
1284
1285 /* Notify upper layer of carrier drop */
1286 if ((port->ip_notify & N_DDCD)
1287 && port->ip_port) {
1288 uart_handle_dcd_change
1289 (port->ip_port, 0);
1290 wake_up_interruptible
1291 (&the_port->state->
1292 port.delta_msr_wait);
1293 }
1294
1295 /* If we had any data to return, we
1296 * would have returned it above.
1297 */
1298 return 0;
1299 }
1300 }
1301 if (*sc & RXSB_MODEM_VALID) {
1302 /* Notify that an input overrun occurred */
1303 if ((*sc & RXSB_OVERRUN)
1304 && (port->ip_notify & N_OVERRUN_ERROR)) {
1305 ioc3_cb_post_ncs(the_port, NCS_OVERRUN);
1306 }
1307 /* Don't look at this byte again */
1308 *sc &= ~RXSB_MODEM_VALID;
1309 }
1310
1311 /* Check for valid data or RX errors */
1312 if ((*sc & RXSB_DATA_VALID) &&
1313 ((*sc & (RXSB_PAR_ERR
1314 | RXSB_FRAME_ERR | RXSB_BREAK))
1315 && (port->ip_notify & (N_PARITY_ERROR
1316 | N_FRAMING_ERROR
1317 | N_BREAK)))) {
1318 /* There is an error condition on the next byte.
1319 * If we have already transferred some bytes,
1320 * we'll stop here. Otherwise if this is the
1321 * first byte to be read, we'll just transfer
1322 * it alone after notifying the
1323 * upper layer of its status.
1324 */
1325 if (total > 0) {
1326 len = 0;
1327 break;
1328 } else {
1329 if ((*sc & RXSB_PAR_ERR) &&
1330 (port->
1331 ip_notify & N_PARITY_ERROR)) {
1332 ioc3_cb_post_ncs(the_port,
1333 NCS_PARITY);
1334 }
1335 if ((*sc & RXSB_FRAME_ERR) &&
1336 (port->
1337 ip_notify & N_FRAMING_ERROR)) {
1338 ioc3_cb_post_ncs(the_port,
1339 NCS_FRAMING);
1340 }
1341 if ((*sc & RXSB_BREAK)
1342 && (port->ip_notify & N_BREAK)) {
1343 ioc3_cb_post_ncs
1344 (the_port, NCS_BREAK);
1345 }
1346 len = 1;
1347 }
1348 }
1349 if (*sc & RXSB_DATA_VALID) {
1350 *sc &= ~RXSB_DATA_VALID;
1351 *buf = entry->ring_data[byte_num];
1352 buf++;
1353 len--;
1354 total++;
1355 }
1356 }
1357
1358 /* If we used up this entry entirely, go on to the next one,
1359 * otherwise we must have run out of buffer space, so
1360 * leave the consumer pointer here for the next read in case
1361 * there are still unread bytes in this entry.
1362 */
1363 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
1364 cons_ptr += (int)sizeof(struct ring_entry);
1365 cons_ptr &= PROD_CONS_MASK;
1366 }
1367 }
1368
1369 /* Update consumer pointer and re-arm rx timer interrupt */
1370 writel(cons_ptr, &port->ip_serial_regs->srcir);
1371 port->ip_rx_cons = cons_ptr;
1372
1373 /* If we have now dipped below the rx high water mark and we have
1374 * rx_high interrupt turned off, we can now turn it back on again.
1375 */
1376 if ((port->ip_flags & INPUT_HIGH) && (((prod_ptr - cons_ptr)
1377 & PROD_CONS_MASK) <
1378 ((port->
1379 ip_sscr &
1380 SSCR_RX_THRESHOLD)
1381 << PROD_CONS_PTR_OFF))) {
1382 port->ip_flags &= ~INPUT_HIGH;
1383 enable_intrs(port, hooks->intr_rx_high);
1384 }
1385 return total;
1386}
1387
1388/**
1389 * receive_chars - upper level read.
1390 * @the_port: port to read from
1391 */
1392static int receive_chars(struct uart_port *the_port)
1393{
1394 struct tty_struct *tty;
1395 unsigned char ch[MAX_CHARS];
1396 int read_count = 0, read_room, flip = 0;
1397 struct uart_state *state = the_port->state;
1398 struct ioc3_port *port = get_ioc3_port(the_port);
1399 unsigned long pflags;
1400
1401 /* Make sure all the pointers are "good" ones */
1402 if (!state)
1403 return 0;
1404 if (!state->port.tty)
1405 return 0;
1406
1407 if (!(port->ip_flags & INPUT_ENABLE))
1408 return 0;
1409
1410 spin_lock_irqsave(&the_port->lock, pflags);
1411 tty = state->port.tty;
1412
1413 read_count = do_read(the_port, ch, MAX_CHARS);
1414 if (read_count > 0) {
1415 flip = 1;
1416 read_room = tty_insert_flip_string(tty, ch, read_count);
1417 the_port->icount.rx += read_count;
1418 }
1419 spin_unlock_irqrestore(&the_port->lock, pflags);
1420
1421 if (flip)
1422 tty_flip_buffer_push(tty);
1423
1424 return read_count;
1425}
1426
1427/**
1428 * ioc3uart_intr_one - lowest level (per port) interrupt handler.
1429 * @is : submodule
1430 * @idd: driver data
1431 * @pending: interrupts to handle
1432 */
1433
1434static int inline
1435ioc3uart_intr_one(struct ioc3_submodule *is,
1436 struct ioc3_driver_data *idd,
1437 unsigned int pending)
1438{
1439 int port_num = GET_PORT_FROM_SIO_IR(pending);
1440 struct port_hooks *hooks;
1441 unsigned int rx_high_rd_aborted = 0;
1442 unsigned long flags;
1443 struct uart_port *the_port;
1444 struct ioc3_port *port;
1445 int loop_counter;
1446 struct ioc3_card *card_ptr;
1447 unsigned int sio_ir;
1448
1449 card_ptr = idd->data[is->id];
1450 port = card_ptr->ic_port[port_num].icp_port;
1451 hooks = port->ip_hooks;
1452
1453 /* Possible race condition here: The tx_mt interrupt bit may be
1454 * cleared without the intervention of the interrupt handler,
1455 * e.g. by a write. If the top level interrupt handler reads a
1456 * tx_mt, then some other processor does a write, starting up
1457 * output, then we come in here, see the tx_mt and stop DMA, the
1458 * output started by the other processor will hang. Thus we can
1459 * only rely on tx_mt being legitimate if it is read while the
1460 * port lock is held. Therefore this bit must be ignored in the
1461 * passed in interrupt mask which was read by the top level
1462 * interrupt handler since the port lock was not held at the time
1463 * it was read. We can only rely on this bit being accurate if it
1464 * is read while the port lock is held. So we'll clear it for now,
1465 * and reload it later once we have the port lock.
1466 */
1467
1468 sio_ir = pending & ~(hooks->intr_tx_mt);
1469 spin_lock_irqsave(&port->ip_lock, flags);
1470
1471 loop_counter = MAXITER; /* to avoid hangs */
1472
1473 do {
1474 uint32_t shadow;
1475
1476 if (loop_counter-- <= 0) {
1477 printk(KERN_WARNING "IOC3 serial: "
1478 "possible hang condition/"
1479 "port stuck on interrupt (line %d).\n",
1480 ((struct uart_port *)port->ip_port)->line);
1481 break;
1482 }
1483 /* Handle a DCD change */
1484 if (sio_ir & hooks->intr_delta_dcd) {
1485 ioc3_ack(is, idd, hooks->intr_delta_dcd);
1486 shadow = readl(&port->ip_serial_regs->shadow);
1487
1488 if ((port->ip_notify & N_DDCD)
1489 && (shadow & SHADOW_DCD)
1490 && (port->ip_port)) {
1491 the_port = port->ip_port;
1492 uart_handle_dcd_change(the_port,
1493 shadow & SHADOW_DCD);
1494 wake_up_interruptible
1495 (&the_port->state->port.delta_msr_wait);
1496 } else if ((port->ip_notify & N_DDCD)
1497 && !(shadow & SHADOW_DCD)) {
1498 /* Flag delta DCD/no DCD */
1499 uart_handle_dcd_change(port->ip_port,
1500 shadow & SHADOW_DCD);
1501 port->ip_flags |= DCD_ON;
1502 }
1503 }
1504
1505 /* Handle a CTS change */
1506 if (sio_ir & hooks->intr_delta_cts) {
1507 ioc3_ack(is, idd, hooks->intr_delta_cts);
1508 shadow = readl(&port->ip_serial_regs->shadow);
1509
1510 if ((port->ip_notify & N_DCTS) && (port->ip_port)) {
1511 the_port = port->ip_port;
1512 uart_handle_cts_change(the_port, shadow
1513 & SHADOW_CTS);
1514 wake_up_interruptible
1515 (&the_port->state->port.delta_msr_wait);
1516 }
1517 }
1518
1519 /* rx timeout interrupt. Must be some data available. Put this
1520 * before the check for rx_high since servicing this condition
1521 * may cause that condition to clear.
1522 */
1523 if (sio_ir & hooks->intr_rx_timer) {
1524 ioc3_ack(is, idd, hooks->intr_rx_timer);
1525 if ((port->ip_notify & N_DATA_READY)
1526 && (port->ip_port)) {
1527 receive_chars(port->ip_port);
1528 }
1529 }
1530
1531 /* rx high interrupt. Must be after rx_timer. */
1532 else if (sio_ir & hooks->intr_rx_high) {
1533 /* Data available, notify upper layer */
1534 if ((port->ip_notify & N_DATA_READY) && port->ip_port) {
1535 receive_chars(port->ip_port);
1536 }
1537
1538 /* We can't ACK this interrupt. If receive_chars didn't
1539 * cause the condition to clear, we'll have to disable
1540 * the interrupt until the data is drained.
1541 * If the read was aborted, don't disable the interrupt
1542 * as this may cause us to hang indefinitely. An
1543 * aborted read generally means that this interrupt
1544 * hasn't been delivered to the cpu yet anyway, even
1545 * though we see it as asserted when we read the sio_ir.
1546 */
1547 if ((sio_ir = PENDING(card_ptr, idd))
1548 & hooks->intr_rx_high) {
1549 if (port->ip_flags & READ_ABORTED) {
1550 rx_high_rd_aborted++;
1551 }
1552 else {
1553 card_ptr->ic_enable &= ~hooks->intr_rx_high;
1554 port->ip_flags |= INPUT_HIGH;
1555 }
1556 }
1557 }
1558
1559 /* We got a low water interrupt: notify upper layer to
1560 * send more data. Must come before tx_mt since servicing
1561 * this condition may cause that condition to clear.
1562 */
1563 if (sio_ir & hooks->intr_tx_explicit) {
1564 port->ip_flags &= ~LOWAT_WRITTEN;
1565 ioc3_ack(is, idd, hooks->intr_tx_explicit);
1566 if (port->ip_notify & N_OUTPUT_LOWAT)
1567 ioc3_cb_output_lowat(port);
1568 }
1569
1570 /* Handle tx_mt. Must come after tx_explicit. */
1571 else if (sio_ir & hooks->intr_tx_mt) {
1572 /* If we are expecting a lowat notification
1573 * and we get to this point it probably means that for
1574 * some reason the tx_explicit didn't work as expected
1575 * (that can legitimately happen if the output buffer is
1576 * filled up in just the right way).
1577 * So send the notification now.
1578 */
1579 if (port->ip_notify & N_OUTPUT_LOWAT) {
1580 ioc3_cb_output_lowat(port);
1581
1582 /* We need to reload the sio_ir since the lowat
1583 * call may have caused another write to occur,
1584 * clearing the tx_mt condition.
1585 */
1586 sio_ir = PENDING(card_ptr, idd);
1587 }
1588
1589 /* If the tx_mt condition still persists even after the
1590 * lowat call, we've got some work to do.
1591 */
1592 if (sio_ir & hooks->intr_tx_mt) {
1593 /* If we are not currently expecting DMA input,
1594 * and the transmitter has just gone idle,
1595 * there is no longer any reason for DMA, so
1596 * disable it.
1597 */
1598 if (!(port->ip_notify
1599 & (N_DATA_READY | N_DDCD))) {
1600 BUG_ON(!(port->ip_sscr
1601 & SSCR_DMA_EN));
1602 port->ip_sscr &= ~SSCR_DMA_EN;
1603 writel(port->ip_sscr,
1604 &port->ip_serial_regs->sscr);
1605 }
1606 /* Prevent infinite tx_mt interrupt */
1607 card_ptr->ic_enable &= ~hooks->intr_tx_mt;
1608 }
1609 }
1610 sio_ir = PENDING(card_ptr, idd);
1611
1612 /* if the read was aborted and only hooks->intr_rx_high,
1613 * clear hooks->intr_rx_high, so we do not loop forever.
1614 */
1615
1616 if (rx_high_rd_aborted && (sio_ir == hooks->intr_rx_high)) {
1617 sio_ir &= ~hooks->intr_rx_high;
1618 }
1619 } while (sio_ir & hooks->intr_all);
1620
1621 spin_unlock_irqrestore(&port->ip_lock, flags);
1622 ioc3_enable(is, idd, card_ptr->ic_enable);
1623 return 0;
1624}
1625
1626/**
1627 * ioc3uart_intr - field all serial interrupts
1628 * @is : submodule
1629 * @idd: driver data
1630 * @pending: interrupts to handle
1631 *
1632 */
1633
1634static int ioc3uart_intr(struct ioc3_submodule *is,
1635 struct ioc3_driver_data *idd,
1636 unsigned int pending)
1637{
1638 int ret = 0;
1639
1640 /*
1641 * The upper level interrupt handler sends interrupts for both ports
1642 * here. So we need to call for each port with its interrupts.
1643 */
1644
1645 if (pending & SIO_IR_SA)
1646 ret |= ioc3uart_intr_one(is, idd, pending & SIO_IR_SA);
1647 if (pending & SIO_IR_SB)
1648 ret |= ioc3uart_intr_one(is, idd, pending & SIO_IR_SB);
1649
1650 return ret;
1651}
1652
1653/**
1654 * ic3_type
1655 * @port: Port to operate with (we ignore since we only have one port)
1656 *
1657 */
1658static const char *ic3_type(struct uart_port *the_port)
1659{
1660 if (IS_RS232(the_port->line))
1661 return "SGI IOC3 Serial [rs232]";
1662 else
1663 return "SGI IOC3 Serial [rs422]";
1664}
1665
1666/**
1667 * ic3_tx_empty - Is the transmitter empty?
1668 * @port: Port to operate on
1669 *
1670 */
1671static unsigned int ic3_tx_empty(struct uart_port *the_port)
1672{
1673 unsigned int ret = 0;
1674 struct ioc3_port *port = get_ioc3_port(the_port);
1675
1676 if (readl(&port->ip_serial_regs->shadow) & SHADOW_TEMT)
1677 ret = TIOCSER_TEMT;
1678 return ret;
1679}
1680
1681/**
1682 * ic3_stop_tx - stop the transmitter
1683 * @port: Port to operate on
1684 *
1685 */
1686static void ic3_stop_tx(struct uart_port *the_port)
1687{
1688 struct ioc3_port *port = get_ioc3_port(the_port);
1689
1690 if (port)
1691 set_notification(port, N_OUTPUT_LOWAT, 0);
1692}
1693
1694/**
1695 * ic3_stop_rx - stop the receiver
1696 * @port: Port to operate on
1697 *
1698 */
1699static void ic3_stop_rx(struct uart_port *the_port)
1700{
1701 struct ioc3_port *port = get_ioc3_port(the_port);
1702
1703 if (port)
1704 port->ip_flags &= ~INPUT_ENABLE;
1705}
1706
1707/**
1708 * null_void_function
1709 * @port: Port to operate on
1710 *
1711 */
1712static void null_void_function(struct uart_port *the_port)
1713{
1714}
1715
1716/**
1717 * ic3_shutdown - shut down the port - free irq and disable
1718 * @port: port to shut down
1719 *
1720 */
1721static void ic3_shutdown(struct uart_port *the_port)
1722{
1723 unsigned long port_flags;
1724 struct ioc3_port *port;
1725 struct uart_state *state;
1726
1727 port = get_ioc3_port(the_port);
1728 if (!port)
1729 return;
1730
1731 state = the_port->state;
1732 wake_up_interruptible(&state->port.delta_msr_wait);
1733
1734 spin_lock_irqsave(&the_port->lock, port_flags);
1735 set_notification(port, N_ALL, 0);
1736 spin_unlock_irqrestore(&the_port->lock, port_flags);
1737}
1738
1739/**
1740 * ic3_set_mctrl - set control lines (dtr, rts, etc)
1741 * @port: Port to operate on
1742 * @mctrl: Lines to set/unset
1743 *
1744 */
1745static void ic3_set_mctrl(struct uart_port *the_port, unsigned int mctrl)
1746{
1747 unsigned char mcr = 0;
1748
1749 if (mctrl & TIOCM_RTS)
1750 mcr |= UART_MCR_RTS;
1751 if (mctrl & TIOCM_DTR)
1752 mcr |= UART_MCR_DTR;
1753 if (mctrl & TIOCM_OUT1)
1754 mcr |= UART_MCR_OUT1;
1755 if (mctrl & TIOCM_OUT2)
1756 mcr |= UART_MCR_OUT2;
1757 if (mctrl & TIOCM_LOOP)
1758 mcr |= UART_MCR_LOOP;
1759
1760 set_mcr(the_port, mcr, SHADOW_DTR);
1761}
1762
1763/**
1764 * ic3_get_mctrl - get control line info
1765 * @port: port to operate on
1766 *
1767 */
1768static unsigned int ic3_get_mctrl(struct uart_port *the_port)
1769{
1770 struct ioc3_port *port = get_ioc3_port(the_port);
1771 uint32_t shadow;
1772 unsigned int ret = 0;
1773
1774 if (!port)
1775 return 0;
1776
1777 shadow = readl(&port->ip_serial_regs->shadow);
1778 if (shadow & SHADOW_DCD)
1779 ret |= TIOCM_CD;
1780 if (shadow & SHADOW_DR)
1781 ret |= TIOCM_DSR;
1782 if (shadow & SHADOW_CTS)
1783 ret |= TIOCM_CTS;
1784 return ret;
1785}
1786
1787/**
1788 * ic3_start_tx - Start transmitter. Called with the_port->lock
1789 * @port: Port to operate on
1790 *
1791 */
1792static void ic3_start_tx(struct uart_port *the_port)
1793{
1794 struct ioc3_port *port = get_ioc3_port(the_port);
1795
1796 if (port) {
1797 set_notification(port, N_OUTPUT_LOWAT, 1);
1798 enable_intrs(port, port->ip_hooks->intr_tx_mt);
1799 }
1800}
1801
1802/**
1803 * ic3_break_ctl - handle breaks
1804 * @port: Port to operate on
1805 * @break_state: Break state
1806 *
1807 */
1808static void ic3_break_ctl(struct uart_port *the_port, int break_state)
1809{
1810}
1811
1812/**
1813 * ic3_startup - Start up the serial port - always return 0 (We're always on)
1814 * @port: Port to operate on
1815 *
1816 */
1817static int ic3_startup(struct uart_port *the_port)
1818{
1819 int retval;
1820 struct ioc3_port *port;
1821 struct ioc3_card *card_ptr;
1822 unsigned long port_flags;
1823
1824 if (!the_port) {
1825 NOT_PROGRESS();
1826 return -ENODEV;
1827 }
1828 port = get_ioc3_port(the_port);
1829 if (!port) {
1830 NOT_PROGRESS();
1831 return -ENODEV;
1832 }
1833 card_ptr = port->ip_card;
1834 port->ip_port = the_port;
1835
1836 if (!card_ptr) {
1837 NOT_PROGRESS();
1838 return -ENODEV;
1839 }
1840
1841 /* Start up the serial port */
1842 spin_lock_irqsave(&the_port->lock, port_flags);
1843 retval = ic3_startup_local(the_port);
1844 spin_unlock_irqrestore(&the_port->lock, port_flags);
1845 return retval;
1846}
1847
1848/**
1849 * ic3_set_termios - set termios stuff
1850 * @port: port to operate on
1851 * @termios: New settings
1852 * @termios: Old
1853 *
1854 */
1855static void
1856ic3_set_termios(struct uart_port *the_port,
1857 struct ktermios *termios, struct ktermios *old_termios)
1858{
1859 unsigned long port_flags;
1860
1861 spin_lock_irqsave(&the_port->lock, port_flags);
1862 ioc3_change_speed(the_port, termios, old_termios);
1863 spin_unlock_irqrestore(&the_port->lock, port_flags);
1864}
1865
1866/**
1867 * ic3_request_port - allocate resources for port - no op....
1868 * @port: port to operate on
1869 *
1870 */
1871static int ic3_request_port(struct uart_port *port)
1872{
1873 return 0;
1874}
1875
1876/* Associate the uart functions above - given to serial core */
1877static struct uart_ops ioc3_ops = {
1878 .tx_empty = ic3_tx_empty,
1879 .set_mctrl = ic3_set_mctrl,
1880 .get_mctrl = ic3_get_mctrl,
1881 .stop_tx = ic3_stop_tx,
1882 .start_tx = ic3_start_tx,
1883 .stop_rx = ic3_stop_rx,
1884 .enable_ms = null_void_function,
1885 .break_ctl = ic3_break_ctl,
1886 .startup = ic3_startup,
1887 .shutdown = ic3_shutdown,
1888 .set_termios = ic3_set_termios,
1889 .type = ic3_type,
1890 .release_port = null_void_function,
1891 .request_port = ic3_request_port,
1892};
1893
1894/*
1895 * Boot-time initialization code
1896 */
1897
1898static struct uart_driver ioc3_uart = {
1899 .owner = THIS_MODULE,
1900 .driver_name = "ioc3_serial",
1901 .dev_name = DEVICE_NAME,
1902 .major = DEVICE_MAJOR,
1903 .minor = DEVICE_MINOR,
1904 .nr = MAX_LOGICAL_PORTS
1905};
1906
1907/**
1908 * ioc3_serial_core_attach - register with serial core
1909 * This is done during pci probing
1910 * @is: submodule struct for this
1911 * @idd: handle for this card
1912 */
1913static inline int ioc3_serial_core_attach( struct ioc3_submodule *is,
1914 struct ioc3_driver_data *idd)
1915{
1916 struct ioc3_port *port;
1917 struct uart_port *the_port;
1918 struct ioc3_card *card_ptr = idd->data[is->id];
1919 int ii, phys_port;
1920 struct pci_dev *pdev = idd->pdev;
1921
1922 DPRINT_CONFIG(("%s: attach pdev 0x%p - card_ptr 0x%p\n",
1923 __func__, pdev, (void *)card_ptr));
1924
1925 if (!card_ptr)
1926 return -ENODEV;
1927
1928 /* once around for each logical port on this card */
1929 for (ii = 0; ii < LOGICAL_PORTS_PER_CARD; ii++) {
1930 phys_port = GET_PHYSICAL_PORT(ii);
1931 the_port = &card_ptr->ic_port[phys_port].
1932 icp_uart_port[GET_LOGICAL_PORT(ii)];
1933 port = card_ptr->ic_port[phys_port].icp_port;
1934 port->ip_port = the_port;
1935
1936 DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p [%d/%d]\n",
1937 __func__, (void *)the_port, (void *)port,
1938 phys_port, ii));
1939
1940 /* membase, iobase and mapbase just need to be non-0 */
1941 the_port->membase = (unsigned char __iomem *)1;
1942 the_port->iobase = (pdev->bus->number << 16) | ii;
1943 the_port->line = (Num_of_ioc3_cards << 2) | ii;
1944 the_port->mapbase = 1;
1945 the_port->type = PORT_16550A;
1946 the_port->fifosize = FIFO_SIZE;
1947 the_port->ops = &ioc3_ops;
1948 the_port->irq = idd->irq_io;
1949 the_port->dev = &pdev->dev;
1950
1951 if (uart_add_one_port(&ioc3_uart, the_port) < 0) {
1952 printk(KERN_WARNING
1953 "%s: unable to add port %d bus %d\n",
1954 __func__, the_port->line, pdev->bus->number);
1955 } else {
1956 DPRINT_CONFIG(("IOC3 serial port %d irq %d bus %d\n",
1957 the_port->line, the_port->irq, pdev->bus->number));
1958 }
1959
1960 /* all ports are rs232 for now */
1961 if (IS_PHYSICAL_PORT(ii))
1962 ioc3_set_proto(port, PROTO_RS232);
1963 }
1964 return 0;
1965}
1966
1967/**
1968 * ioc3uart_remove - register detach function
1969 * @is: submodule struct for this submodule
1970 * @idd: ioc3 driver data for this submodule
1971 */
1972
1973static int ioc3uart_remove(struct ioc3_submodule *is,
1974 struct ioc3_driver_data *idd)
1975{
1976 struct ioc3_card *card_ptr = idd->data[is->id];
1977 struct uart_port *the_port;
1978 struct ioc3_port *port;
1979 int ii;
1980
1981 if (card_ptr) {
1982 for (ii = 0; ii < LOGICAL_PORTS_PER_CARD; ii++) {
1983 the_port = &card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].
1984 icp_uart_port[GET_LOGICAL_PORT(ii)];
1985 if (the_port)
1986 uart_remove_one_port(&ioc3_uart, the_port);
1987 port = card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].icp_port;
1988 if (port && IS_PHYSICAL_PORT(ii)
1989 && (GET_PHYSICAL_PORT(ii) == 0)) {
1990 pci_free_consistent(port->ip_idd->pdev,
1991 TOTAL_RING_BUF_SIZE,
1992 (void *)port->ip_cpu_ringbuf,
1993 port->ip_dma_ringbuf);
1994 kfree(port);
1995 card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].
1996 icp_port = NULL;
1997 }
1998 }
1999 kfree(card_ptr);
2000 idd->data[is->id] = NULL;
2001 }
2002 return 0;
2003}
2004
2005/**
2006 * ioc3uart_probe - card probe function called from shim driver
2007 * @is: submodule struct for this submodule
2008 * @idd: ioc3 driver data for this card
2009 */
2010
2011static int __devinit
2012ioc3uart_probe(struct ioc3_submodule *is, struct ioc3_driver_data *idd)
2013{
2014 struct pci_dev *pdev = idd->pdev;
2015 struct ioc3_card *card_ptr;
2016 int ret = 0;
2017 struct ioc3_port *port;
2018 struct ioc3_port *ports[PORTS_PER_CARD];
2019 int phys_port;
2020 int cnt;
2021
2022 DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __func__, is, idd));
2023
2024 card_ptr = kzalloc(sizeof(struct ioc3_card), GFP_KERNEL);
2025 if (!card_ptr) {
2026 printk(KERN_WARNING "ioc3_attach_one"
2027 ": unable to get memory for the IOC3\n");
2028 return -ENOMEM;
2029 }
2030 idd->data[is->id] = card_ptr;
2031 Submodule_slot = is->id;
2032
2033 writel(((UARTA_BASE >> 3) << SIO_CR_SER_A_BASE_SHIFT) |
2034 ((UARTB_BASE >> 3) << SIO_CR_SER_B_BASE_SHIFT) |
2035 (0xf << SIO_CR_CMD_PULSE_SHIFT), &idd->vma->sio_cr);
2036
2037 pci_write_config_dword(pdev, PCI_LAT, 0xff00);
2038
2039 /* Enable serial port mode select generic PIO pins as outputs */
2040 ioc3_gpcr_set(idd, GPCR_UARTA_MODESEL | GPCR_UARTB_MODESEL);
2041
2042 /* Create port structures for each port */
2043 for (phys_port = 0; phys_port < PORTS_PER_CARD; phys_port++) {
2044 port = kzalloc(sizeof(struct ioc3_port), GFP_KERNEL);
2045 if (!port) {
2046 printk(KERN_WARNING
2047 "IOC3 serial memory not available for port\n");
2048 ret = -ENOMEM;
2049 goto out4;
2050 }
2051 spin_lock_init(&port->ip_lock);
2052
2053 /* we need to remember the previous ones, to point back to
2054 * them farther down - setting up the ring buffers.
2055 */
2056 ports[phys_port] = port;
2057
2058 /* init to something useful */
2059 card_ptr->ic_port[phys_port].icp_port = port;
2060 port->ip_is = is;
2061 port->ip_idd = idd;
2062 port->ip_baud = 9600;
2063 port->ip_card = card_ptr;
2064 port->ip_hooks = &hooks_array[phys_port];
2065
2066 /* Setup each port */
2067 if (phys_port == 0) {
2068 port->ip_serial_regs = &idd->vma->port_a;
2069 port->ip_uart_regs = &idd->vma->sregs.uarta;
2070
2071 DPRINT_CONFIG(("%s : Port A ip_serial_regs 0x%p "
2072 "ip_uart_regs 0x%p\n",
2073 __func__,
2074 (void *)port->ip_serial_regs,
2075 (void *)port->ip_uart_regs));
2076
2077 /* setup ring buffers */
2078 port->ip_cpu_ringbuf = pci_alloc_consistent(pdev,
2079 TOTAL_RING_BUF_SIZE, &port->ip_dma_ringbuf);
2080
2081 BUG_ON(!((((int64_t) port->ip_dma_ringbuf) &
2082 (TOTAL_RING_BUF_SIZE - 1)) == 0));
2083 port->ip_inring = RING(port, RX_A);
2084 port->ip_outring = RING(port, TX_A);
2085 DPRINT_CONFIG(("%s : Port A ip_cpu_ringbuf 0x%p "
2086 "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
2087 "ip_outring 0x%p\n",
2088 __func__,
2089 (void *)port->ip_cpu_ringbuf,
2090 (void *)port->ip_dma_ringbuf,
2091 (void *)port->ip_inring,
2092 (void *)port->ip_outring));
2093 }
2094 else {
2095 port->ip_serial_regs = &idd->vma->port_b;
2096 port->ip_uart_regs = &idd->vma->sregs.uartb;
2097
2098 DPRINT_CONFIG(("%s : Port B ip_serial_regs 0x%p "
2099 "ip_uart_regs 0x%p\n",
2100 __func__,
2101 (void *)port->ip_serial_regs,
2102 (void *)port->ip_uart_regs));
2103
2104 /* share the ring buffers */
2105 port->ip_dma_ringbuf =
2106 ports[phys_port - 1]->ip_dma_ringbuf;
2107 port->ip_cpu_ringbuf =
2108 ports[phys_port - 1]->ip_cpu_ringbuf;
2109 port->ip_inring = RING(port, RX_B);
2110 port->ip_outring = RING(port, TX_B);
2111 DPRINT_CONFIG(("%s : Port B ip_cpu_ringbuf 0x%p "
2112 "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
2113 "ip_outring 0x%p\n",
2114 __func__,
2115 (void *)port->ip_cpu_ringbuf,
2116 (void *)port->ip_dma_ringbuf,
2117 (void *)port->ip_inring,
2118 (void *)port->ip_outring));
2119 }
2120
2121 DPRINT_CONFIG(("%s : port %d [addr 0x%p] card_ptr 0x%p",
2122 __func__,
2123 phys_port, (void *)port, (void *)card_ptr));
2124 DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
2125 (void *)port->ip_serial_regs,
2126 (void *)port->ip_uart_regs));
2127
2128 /* Initialize the hardware for IOC3 */
2129 port_init(port);
2130
2131 DPRINT_CONFIG(("%s: phys_port %d port 0x%p inring 0x%p "
2132 "outring 0x%p\n",
2133 __func__,
2134 phys_port, (void *)port,
2135 (void *)port->ip_inring,
2136 (void *)port->ip_outring));
2137
2138 }
2139
2140 /* register port with the serial core */
2141
2142 if ((ret = ioc3_serial_core_attach(is, idd)))
2143 goto out4;
2144
2145 Num_of_ioc3_cards++;
2146
2147 return ret;
2148
2149 /* error exits that give back resources */
2150out4:
2151 for (cnt = 0; cnt < phys_port; cnt++)
2152 kfree(ports[cnt]);
2153
2154 kfree(card_ptr);
2155 return ret;
2156}
2157
2158static struct ioc3_submodule ioc3uart_ops = {
2159 .name = "IOC3uart",
2160 .probe = ioc3uart_probe,
2161 .remove = ioc3uart_remove,
2162 /* call .intr for both ports initially */
2163 .irq_mask = SIO_IR_SA | SIO_IR_SB,
2164 .intr = ioc3uart_intr,
2165 .owner = THIS_MODULE,
2166};
2167
2168/**
2169 * ioc3_detect - module init called,
2170 */
2171static int __init ioc3uart_init(void)
2172{
2173 int ret;
2174
2175 /* register with serial core */
2176 if ((ret = uart_register_driver(&ioc3_uart)) < 0) {
2177 printk(KERN_WARNING
2178 "%s: Couldn't register IOC3 uart serial driver\n",
2179 __func__);
2180 return ret;
2181 }
2182 ret = ioc3_register_submodule(&ioc3uart_ops);
2183 if (ret)
2184 uart_unregister_driver(&ioc3_uart);
2185 return ret;
2186}
2187
2188static void __exit ioc3uart_exit(void)
2189{
2190 ioc3_unregister_submodule(&ioc3uart_ops);
2191 uart_unregister_driver(&ioc3_uart);
2192}
2193
2194module_init(ioc3uart_init);
2195module_exit(ioc3uart_exit);
2196
2197MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
2198MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC3 card");
2199MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/ioc4_serial.c b/drivers/tty/serial/ioc4_serial.c
new file mode 100644
index 000000000000..fcfe82653ac8
--- /dev/null
+++ b/drivers/tty/serial/ioc4_serial.c
@@ -0,0 +1,2953 @@
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-2006 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.h>
24#include <linux/serial_core.h>
25#include <linux/slab.h>
26
27/*
28 * interesting things about the ioc4
29 */
30
31#define IOC4_NUM_SERIAL_PORTS 4 /* max ports per card */
32#define IOC4_NUM_CARDS 8 /* max cards per partition */
33
34#define GET_SIO_IR(_n) (_n == 0) ? (IOC4_SIO_IR_S0) : \
35 (_n == 1) ? (IOC4_SIO_IR_S1) : \
36 (_n == 2) ? (IOC4_SIO_IR_S2) : \
37 (IOC4_SIO_IR_S3)
38
39#define GET_OTHER_IR(_n) (_n == 0) ? (IOC4_OTHER_IR_S0_MEMERR) : \
40 (_n == 1) ? (IOC4_OTHER_IR_S1_MEMERR) : \
41 (_n == 2) ? (IOC4_OTHER_IR_S2_MEMERR) : \
42 (IOC4_OTHER_IR_S3_MEMERR)
43
44
45/*
46 * All IOC4 registers are 32 bits wide.
47 */
48
49/*
50 * PCI Memory Space Map
51 */
52#define IOC4_PCI_ERR_ADDR_L 0x000 /* Low Error Address */
53#define IOC4_PCI_ERR_ADDR_VLD (0x1 << 0)
54#define IOC4_PCI_ERR_ADDR_MST_ID_MSK (0xf << 1)
55#define IOC4_PCI_ERR_ADDR_MST_NUM_MSK (0xe << 1)
56#define IOC4_PCI_ERR_ADDR_MST_TYP_MSK (0x1 << 1)
57#define IOC4_PCI_ERR_ADDR_MUL_ERR (0x1 << 5)
58#define IOC4_PCI_ERR_ADDR_ADDR_MSK (0x3ffffff << 6)
59
60/* Interrupt types */
61#define IOC4_SIO_INTR_TYPE 0
62#define IOC4_OTHER_INTR_TYPE 1
63#define IOC4_NUM_INTR_TYPES 2
64
65/* Bitmasks for IOC4_SIO_IR, IOC4_SIO_IEC, and IOC4_SIO_IES */
66#define IOC4_SIO_IR_S0_TX_MT 0x00000001 /* Serial port 0 TX empty */
67#define IOC4_SIO_IR_S0_RX_FULL 0x00000002 /* Port 0 RX buf full */
68#define IOC4_SIO_IR_S0_RX_HIGH 0x00000004 /* Port 0 RX hiwat */
69#define IOC4_SIO_IR_S0_RX_TIMER 0x00000008 /* Port 0 RX timeout */
70#define IOC4_SIO_IR_S0_DELTA_DCD 0x00000010 /* Port 0 delta DCD */
71#define IOC4_SIO_IR_S0_DELTA_CTS 0x00000020 /* Port 0 delta CTS */
72#define IOC4_SIO_IR_S0_INT 0x00000040 /* Port 0 pass-thru intr */
73#define IOC4_SIO_IR_S0_TX_EXPLICIT 0x00000080 /* Port 0 explicit TX thru */
74#define IOC4_SIO_IR_S1_TX_MT 0x00000100 /* Serial port 1 */
75#define IOC4_SIO_IR_S1_RX_FULL 0x00000200 /* */
76#define IOC4_SIO_IR_S1_RX_HIGH 0x00000400 /* */
77#define IOC4_SIO_IR_S1_RX_TIMER 0x00000800 /* */
78#define IOC4_SIO_IR_S1_DELTA_DCD 0x00001000 /* */
79#define IOC4_SIO_IR_S1_DELTA_CTS 0x00002000 /* */
80#define IOC4_SIO_IR_S1_INT 0x00004000 /* */
81#define IOC4_SIO_IR_S1_TX_EXPLICIT 0x00008000 /* */
82#define IOC4_SIO_IR_S2_TX_MT 0x00010000 /* Serial port 2 */
83#define IOC4_SIO_IR_S2_RX_FULL 0x00020000 /* */
84#define IOC4_SIO_IR_S2_RX_HIGH 0x00040000 /* */
85#define IOC4_SIO_IR_S2_RX_TIMER 0x00080000 /* */
86#define IOC4_SIO_IR_S2_DELTA_DCD 0x00100000 /* */
87#define IOC4_SIO_IR_S2_DELTA_CTS 0x00200000 /* */
88#define IOC4_SIO_IR_S2_INT 0x00400000 /* */
89#define IOC4_SIO_IR_S2_TX_EXPLICIT 0x00800000 /* */
90#define IOC4_SIO_IR_S3_TX_MT 0x01000000 /* Serial port 3 */
91#define IOC4_SIO_IR_S3_RX_FULL 0x02000000 /* */
92#define IOC4_SIO_IR_S3_RX_HIGH 0x04000000 /* */
93#define IOC4_SIO_IR_S3_RX_TIMER 0x08000000 /* */
94#define IOC4_SIO_IR_S3_DELTA_DCD 0x10000000 /* */
95#define IOC4_SIO_IR_S3_DELTA_CTS 0x20000000 /* */
96#define IOC4_SIO_IR_S3_INT 0x40000000 /* */
97#define IOC4_SIO_IR_S3_TX_EXPLICIT 0x80000000 /* */
98
99/* Per device interrupt masks */
100#define IOC4_SIO_IR_S0 (IOC4_SIO_IR_S0_TX_MT | \
101 IOC4_SIO_IR_S0_RX_FULL | \
102 IOC4_SIO_IR_S0_RX_HIGH | \
103 IOC4_SIO_IR_S0_RX_TIMER | \
104 IOC4_SIO_IR_S0_DELTA_DCD | \
105 IOC4_SIO_IR_S0_DELTA_CTS | \
106 IOC4_SIO_IR_S0_INT | \
107 IOC4_SIO_IR_S0_TX_EXPLICIT)
108#define IOC4_SIO_IR_S1 (IOC4_SIO_IR_S1_TX_MT | \
109 IOC4_SIO_IR_S1_RX_FULL | \
110 IOC4_SIO_IR_S1_RX_HIGH | \
111 IOC4_SIO_IR_S1_RX_TIMER | \
112 IOC4_SIO_IR_S1_DELTA_DCD | \
113 IOC4_SIO_IR_S1_DELTA_CTS | \
114 IOC4_SIO_IR_S1_INT | \
115 IOC4_SIO_IR_S1_TX_EXPLICIT)
116#define IOC4_SIO_IR_S2 (IOC4_SIO_IR_S2_TX_MT | \
117 IOC4_SIO_IR_S2_RX_FULL | \
118 IOC4_SIO_IR_S2_RX_HIGH | \
119 IOC4_SIO_IR_S2_RX_TIMER | \
120 IOC4_SIO_IR_S2_DELTA_DCD | \
121 IOC4_SIO_IR_S2_DELTA_CTS | \
122 IOC4_SIO_IR_S2_INT | \
123 IOC4_SIO_IR_S2_TX_EXPLICIT)
124#define IOC4_SIO_IR_S3 (IOC4_SIO_IR_S3_TX_MT | \
125 IOC4_SIO_IR_S3_RX_FULL | \
126 IOC4_SIO_IR_S3_RX_HIGH | \
127 IOC4_SIO_IR_S3_RX_TIMER | \
128 IOC4_SIO_IR_S3_DELTA_DCD | \
129 IOC4_SIO_IR_S3_DELTA_CTS | \
130 IOC4_SIO_IR_S3_INT | \
131 IOC4_SIO_IR_S3_TX_EXPLICIT)
132
133/* Bitmasks for IOC4_OTHER_IR, IOC4_OTHER_IEC, and IOC4_OTHER_IES */
134#define IOC4_OTHER_IR_ATA_INT 0x00000001 /* ATAPI intr pass-thru */
135#define IOC4_OTHER_IR_ATA_MEMERR 0x00000002 /* ATAPI DMA PCI error */
136#define IOC4_OTHER_IR_S0_MEMERR 0x00000004 /* Port 0 PCI error */
137#define IOC4_OTHER_IR_S1_MEMERR 0x00000008 /* Port 1 PCI error */
138#define IOC4_OTHER_IR_S2_MEMERR 0x00000010 /* Port 2 PCI error */
139#define IOC4_OTHER_IR_S3_MEMERR 0x00000020 /* Port 3 PCI error */
140#define IOC4_OTHER_IR_KBD_INT 0x00000040 /* Keyboard/mouse */
141#define IOC4_OTHER_IR_RESERVED 0x007fff80 /* Reserved */
142#define IOC4_OTHER_IR_RT_INT 0x00800000 /* INT_OUT section output */
143#define IOC4_OTHER_IR_GEN_INT 0xff000000 /* Generic pins */
144
145#define IOC4_OTHER_IR_SER_MEMERR (IOC4_OTHER_IR_S0_MEMERR | IOC4_OTHER_IR_S1_MEMERR | \
146 IOC4_OTHER_IR_S2_MEMERR | IOC4_OTHER_IR_S3_MEMERR)
147
148/* Bitmasks for IOC4_SIO_CR */
149#define IOC4_SIO_CR_CMD_PULSE_SHIFT 0 /* byte bus strobe shift */
150#define IOC4_SIO_CR_ARB_DIAG_TX0 0x00000000
151#define IOC4_SIO_CR_ARB_DIAG_RX0 0x00000010
152#define IOC4_SIO_CR_ARB_DIAG_TX1 0x00000020
153#define IOC4_SIO_CR_ARB_DIAG_RX1 0x00000030
154#define IOC4_SIO_CR_ARB_DIAG_TX2 0x00000040
155#define IOC4_SIO_CR_ARB_DIAG_RX2 0x00000050
156#define IOC4_SIO_CR_ARB_DIAG_TX3 0x00000060
157#define IOC4_SIO_CR_ARB_DIAG_RX3 0x00000070
158#define IOC4_SIO_CR_SIO_DIAG_IDLE 0x00000080 /* 0 -> active request among
159 serial ports (ro) */
160/* Defs for some of the generic I/O pins */
161#define IOC4_GPCR_UART0_MODESEL 0x10 /* Pin is output to port 0
162 mode sel */
163#define IOC4_GPCR_UART1_MODESEL 0x20 /* Pin is output to port 1
164 mode sel */
165#define IOC4_GPCR_UART2_MODESEL 0x40 /* Pin is output to port 2
166 mode sel */
167#define IOC4_GPCR_UART3_MODESEL 0x80 /* Pin is output to port 3
168 mode sel */
169
170#define IOC4_GPPR_UART0_MODESEL_PIN 4 /* GIO pin controlling
171 uart 0 mode select */
172#define IOC4_GPPR_UART1_MODESEL_PIN 5 /* GIO pin controlling
173 uart 1 mode select */
174#define IOC4_GPPR_UART2_MODESEL_PIN 6 /* GIO pin controlling
175 uart 2 mode select */
176#define IOC4_GPPR_UART3_MODESEL_PIN 7 /* GIO pin controlling
177 uart 3 mode select */
178
179/* Bitmasks for serial RX status byte */
180#define IOC4_RXSB_OVERRUN 0x01 /* Char(s) lost */
181#define IOC4_RXSB_PAR_ERR 0x02 /* Parity error */
182#define IOC4_RXSB_FRAME_ERR 0x04 /* Framing error */
183#define IOC4_RXSB_BREAK 0x08 /* Break character */
184#define IOC4_RXSB_CTS 0x10 /* State of CTS */
185#define IOC4_RXSB_DCD 0x20 /* State of DCD */
186#define IOC4_RXSB_MODEM_VALID 0x40 /* DCD, CTS, and OVERRUN are valid */
187#define IOC4_RXSB_DATA_VALID 0x80 /* Data byte, FRAME_ERR PAR_ERR
188 * & BREAK valid */
189
190/* Bitmasks for serial TX control byte */
191#define IOC4_TXCB_INT_WHEN_DONE 0x20 /* Interrupt after this byte is sent */
192#define IOC4_TXCB_INVALID 0x00 /* Byte is invalid */
193#define IOC4_TXCB_VALID 0x40 /* Byte is valid */
194#define IOC4_TXCB_MCR 0x80 /* Data<7:0> to modem control reg */
195#define IOC4_TXCB_DELAY 0xc0 /* Delay data<7:0> mSec */
196
197/* Bitmasks for IOC4_SBBR_L */
198#define IOC4_SBBR_L_SIZE 0x00000001 /* 0 == 1KB rings, 1 == 4KB rings */
199
200/* Bitmasks for IOC4_SSCR_<3:0> */
201#define IOC4_SSCR_RX_THRESHOLD 0x000001ff /* Hiwater mark */
202#define IOC4_SSCR_TX_TIMER_BUSY 0x00010000 /* TX timer in progress */
203#define IOC4_SSCR_HFC_EN 0x00020000 /* Hardware flow control enabled */
204#define IOC4_SSCR_RX_RING_DCD 0x00040000 /* Post RX record on delta-DCD */
205#define IOC4_SSCR_RX_RING_CTS 0x00080000 /* Post RX record on delta-CTS */
206#define IOC4_SSCR_DIAG 0x00200000 /* Bypass clock divider for sim */
207#define IOC4_SSCR_RX_DRAIN 0x08000000 /* Drain RX buffer to memory */
208#define IOC4_SSCR_DMA_EN 0x10000000 /* Enable ring buffer DMA */
209#define IOC4_SSCR_DMA_PAUSE 0x20000000 /* Pause DMA */
210#define IOC4_SSCR_PAUSE_STATE 0x40000000 /* Sets when PAUSE takes effect */
211#define IOC4_SSCR_RESET 0x80000000 /* Reset DMA channels */
212
213/* All producer/comsumer pointers are the same bitfield */
214#define IOC4_PROD_CONS_PTR_4K 0x00000ff8 /* For 4K buffers */
215#define IOC4_PROD_CONS_PTR_1K 0x000003f8 /* For 1K buffers */
216#define IOC4_PROD_CONS_PTR_OFF 3
217
218/* Bitmasks for IOC4_SRCIR_<3:0> */
219#define IOC4_SRCIR_ARM 0x80000000 /* Arm RX timer */
220
221/* Bitmasks for IOC4_SHADOW_<3:0> */
222#define IOC4_SHADOW_DR 0x00000001 /* Data ready */
223#define IOC4_SHADOW_OE 0x00000002 /* Overrun error */
224#define IOC4_SHADOW_PE 0x00000004 /* Parity error */
225#define IOC4_SHADOW_FE 0x00000008 /* Framing error */
226#define IOC4_SHADOW_BI 0x00000010 /* Break interrupt */
227#define IOC4_SHADOW_THRE 0x00000020 /* Xmit holding register empty */
228#define IOC4_SHADOW_TEMT 0x00000040 /* Xmit shift register empty */
229#define IOC4_SHADOW_RFCE 0x00000080 /* Char in RX fifo has an error */
230#define IOC4_SHADOW_DCTS 0x00010000 /* Delta clear to send */
231#define IOC4_SHADOW_DDCD 0x00080000 /* Delta data carrier detect */
232#define IOC4_SHADOW_CTS 0x00100000 /* Clear to send */
233#define IOC4_SHADOW_DCD 0x00800000 /* Data carrier detect */
234#define IOC4_SHADOW_DTR 0x01000000 /* Data terminal ready */
235#define IOC4_SHADOW_RTS 0x02000000 /* Request to send */
236#define IOC4_SHADOW_OUT1 0x04000000 /* 16550 OUT1 bit */
237#define IOC4_SHADOW_OUT2 0x08000000 /* 16550 OUT2 bit */
238#define IOC4_SHADOW_LOOP 0x10000000 /* Loopback enabled */
239
240/* Bitmasks for IOC4_SRTR_<3:0> */
241#define IOC4_SRTR_CNT 0x00000fff /* Reload value for RX timer */
242#define IOC4_SRTR_CNT_VAL 0x0fff0000 /* Current value of RX timer */
243#define IOC4_SRTR_CNT_VAL_SHIFT 16
244#define IOC4_SRTR_HZ 16000 /* SRTR clock frequency */
245
246/* Serial port register map used for DMA and PIO serial I/O */
247struct ioc4_serialregs {
248 uint32_t sscr;
249 uint32_t stpir;
250 uint32_t stcir;
251 uint32_t srpir;
252 uint32_t srcir;
253 uint32_t srtr;
254 uint32_t shadow;
255};
256
257/* IOC4 UART register map */
258struct ioc4_uartregs {
259 char i4u_lcr;
260 union {
261 char iir; /* read only */
262 char fcr; /* write only */
263 } u3;
264 union {
265 char ier; /* DLAB == 0 */
266 char dlm; /* DLAB == 1 */
267 } u2;
268 union {
269 char rbr; /* read only, DLAB == 0 */
270 char thr; /* write only, DLAB == 0 */
271 char dll; /* DLAB == 1 */
272 } u1;
273 char i4u_scr;
274 char i4u_msr;
275 char i4u_lsr;
276 char i4u_mcr;
277};
278
279/* short names */
280#define i4u_dll u1.dll
281#define i4u_ier u2.ier
282#define i4u_dlm u2.dlm
283#define i4u_fcr u3.fcr
284
285/* Serial port registers used for DMA serial I/O */
286struct ioc4_serial {
287 uint32_t sbbr01_l;
288 uint32_t sbbr01_h;
289 uint32_t sbbr23_l;
290 uint32_t sbbr23_h;
291
292 struct ioc4_serialregs port_0;
293 struct ioc4_serialregs port_1;
294 struct ioc4_serialregs port_2;
295 struct ioc4_serialregs port_3;
296 struct ioc4_uartregs uart_0;
297 struct ioc4_uartregs uart_1;
298 struct ioc4_uartregs uart_2;
299 struct ioc4_uartregs uart_3;
300} ioc4_serial;
301
302/* UART clock speed */
303#define IOC4_SER_XIN_CLK_66 66666667
304#define IOC4_SER_XIN_CLK_33 33333333
305
306#define IOC4_W_IES 0
307#define IOC4_W_IEC 1
308
309typedef void ioc4_intr_func_f(void *, uint32_t);
310typedef ioc4_intr_func_f *ioc4_intr_func_t;
311
312static unsigned int Num_of_ioc4_cards;
313
314/* defining this will get you LOTS of great debug info */
315//#define DEBUG_INTERRUPTS
316#define DPRINT_CONFIG(_x...) ;
317//#define DPRINT_CONFIG(_x...) printk _x
318
319/* number of characters left in xmit buffer before we ask for more */
320#define WAKEUP_CHARS 256
321
322/* number of characters we want to transmit to the lower level at a time */
323#define IOC4_MAX_CHARS 256
324#define IOC4_FIFO_CHARS 255
325
326/* Device name we're using */
327#define DEVICE_NAME_RS232 "ttyIOC"
328#define DEVICE_NAME_RS422 "ttyAIOC"
329#define DEVICE_MAJOR 204
330#define DEVICE_MINOR_RS232 50
331#define DEVICE_MINOR_RS422 84
332
333
334/* register offsets */
335#define IOC4_SERIAL_OFFSET 0x300
336
337/* flags for next_char_state */
338#define NCS_BREAK 0x1
339#define NCS_PARITY 0x2
340#define NCS_FRAMING 0x4
341#define NCS_OVERRUN 0x8
342
343/* cause we need SOME parameters ... */
344#define MIN_BAUD_SUPPORTED 1200
345#define MAX_BAUD_SUPPORTED 115200
346
347/* protocol types supported */
348#define PROTO_RS232 3
349#define PROTO_RS422 7
350
351/* Notification types */
352#define N_DATA_READY 0x01
353#define N_OUTPUT_LOWAT 0x02
354#define N_BREAK 0x04
355#define N_PARITY_ERROR 0x08
356#define N_FRAMING_ERROR 0x10
357#define N_OVERRUN_ERROR 0x20
358#define N_DDCD 0x40
359#define N_DCTS 0x80
360
361#define N_ALL_INPUT (N_DATA_READY | N_BREAK | \
362 N_PARITY_ERROR | N_FRAMING_ERROR | \
363 N_OVERRUN_ERROR | N_DDCD | N_DCTS)
364
365#define N_ALL_OUTPUT N_OUTPUT_LOWAT
366
367#define N_ALL_ERRORS (N_PARITY_ERROR | N_FRAMING_ERROR | N_OVERRUN_ERROR)
368
369#define N_ALL (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK | \
370 N_PARITY_ERROR | N_FRAMING_ERROR | \
371 N_OVERRUN_ERROR | N_DDCD | N_DCTS)
372
373#define SER_DIVISOR(_x, clk) (((clk) + (_x) * 8) / ((_x) * 16))
374#define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
375
376/* Some masks */
377#define LCR_MASK_BITS_CHAR (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
378 | UART_LCR_WLEN7 | UART_LCR_WLEN8)
379#define LCR_MASK_STOP_BITS (UART_LCR_STOP)
380
381#define PENDING(_p) (readl(&(_p)->ip_mem->sio_ir.raw) & _p->ip_ienb)
382#define READ_SIO_IR(_p) readl(&(_p)->ip_mem->sio_ir.raw)
383
384/* Default to 4k buffers */
385#ifdef IOC4_1K_BUFFERS
386#define RING_BUF_SIZE 1024
387#define IOC4_BUF_SIZE_BIT 0
388#define PROD_CONS_MASK IOC4_PROD_CONS_PTR_1K
389#else
390#define RING_BUF_SIZE 4096
391#define IOC4_BUF_SIZE_BIT IOC4_SBBR_L_SIZE
392#define PROD_CONS_MASK IOC4_PROD_CONS_PTR_4K
393#endif
394
395#define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
396
397/*
398 * This is the entry saved by the driver - one per card
399 */
400
401#define UART_PORT_MIN 0
402#define UART_PORT_RS232 UART_PORT_MIN
403#define UART_PORT_RS422 1
404#define UART_PORT_COUNT 2 /* one for each mode */
405
406struct ioc4_control {
407 int ic_irq;
408 struct {
409 /* uart ports are allocated here - 1 for rs232, 1 for rs422 */
410 struct uart_port icp_uart_port[UART_PORT_COUNT];
411 /* Handy reference material */
412 struct ioc4_port *icp_port;
413 } ic_port[IOC4_NUM_SERIAL_PORTS];
414 struct ioc4_soft *ic_soft;
415};
416
417/*
418 * per-IOC4 data structure
419 */
420#define MAX_IOC4_INTR_ENTS (8 * sizeof(uint32_t))
421struct ioc4_soft {
422 struct ioc4_misc_regs __iomem *is_ioc4_misc_addr;
423 struct ioc4_serial __iomem *is_ioc4_serial_addr;
424
425 /* Each interrupt type has an entry in the array */
426 struct ioc4_intr_type {
427
428 /*
429 * Each in-use entry in this array contains at least
430 * one nonzero bit in sd_bits; no two entries in this
431 * array have overlapping sd_bits values.
432 */
433 struct ioc4_intr_info {
434 uint32_t sd_bits;
435 ioc4_intr_func_f *sd_intr;
436 void *sd_info;
437 } is_intr_info[MAX_IOC4_INTR_ENTS];
438
439 /* Number of entries active in the above array */
440 atomic_t is_num_intrs;
441 } is_intr_type[IOC4_NUM_INTR_TYPES];
442
443 /* is_ir_lock must be held while
444 * modifying sio_ie values, so
445 * we can be sure that sio_ie is
446 * not changing when we read it
447 * along with sio_ir.
448 */
449 spinlock_t is_ir_lock; /* SIO_IE[SC] mod lock */
450};
451
452/* Local port info for each IOC4 serial ports */
453struct ioc4_port {
454 struct uart_port *ip_port; /* current active port ptr */
455 /* Ptrs for all ports */
456 struct uart_port *ip_all_ports[UART_PORT_COUNT];
457 /* Back ptrs for this port */
458 struct ioc4_control *ip_control;
459 struct pci_dev *ip_pdev;
460 struct ioc4_soft *ip_ioc4_soft;
461
462 /* pci mem addresses */
463 struct ioc4_misc_regs __iomem *ip_mem;
464 struct ioc4_serial __iomem *ip_serial;
465 struct ioc4_serialregs __iomem *ip_serial_regs;
466 struct ioc4_uartregs __iomem *ip_uart_regs;
467
468 /* Ring buffer page for this port */
469 dma_addr_t ip_dma_ringbuf;
470 /* vaddr of ring buffer */
471 struct ring_buffer *ip_cpu_ringbuf;
472
473 /* Rings for this port */
474 struct ring *ip_inring;
475 struct ring *ip_outring;
476
477 /* Hook to port specific values */
478 struct hooks *ip_hooks;
479
480 spinlock_t ip_lock;
481
482 /* Various rx/tx parameters */
483 int ip_baud;
484 int ip_tx_lowat;
485 int ip_rx_timeout;
486
487 /* Copy of notification bits */
488 int ip_notify;
489
490 /* Shadow copies of various registers so we don't need to PIO
491 * read them constantly
492 */
493 uint32_t ip_ienb; /* Enabled interrupts */
494 uint32_t ip_sscr;
495 uint32_t ip_tx_prod;
496 uint32_t ip_rx_cons;
497 int ip_pci_bus_speed;
498 unsigned char ip_flags;
499};
500
501/* tx low water mark. We need to notify the driver whenever tx is getting
502 * close to empty so it can refill the tx buffer and keep things going.
503 * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
504 * have no trouble getting in more chars in time (I certainly hope so).
505 */
506#define TX_LOWAT_LATENCY 1000
507#define TX_LOWAT_HZ (1000000 / TX_LOWAT_LATENCY)
508#define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
509
510/* Flags per port */
511#define INPUT_HIGH 0x01
512#define DCD_ON 0x02
513#define LOWAT_WRITTEN 0x04
514#define READ_ABORTED 0x08
515#define PORT_ACTIVE 0x10
516#define PORT_INACTIVE 0 /* This is the value when "off" */
517
518
519/* Since each port has different register offsets and bitmasks
520 * for everything, we'll store those that we need in tables so we
521 * don't have to be constantly checking the port we are dealing with.
522 */
523struct hooks {
524 uint32_t intr_delta_dcd;
525 uint32_t intr_delta_cts;
526 uint32_t intr_tx_mt;
527 uint32_t intr_rx_timer;
528 uint32_t intr_rx_high;
529 uint32_t intr_tx_explicit;
530 uint32_t intr_dma_error;
531 uint32_t intr_clear;
532 uint32_t intr_all;
533 int rs422_select_pin;
534};
535
536static struct hooks hooks_array[IOC4_NUM_SERIAL_PORTS] = {
537 /* Values for port 0 */
538 {
539 IOC4_SIO_IR_S0_DELTA_DCD, IOC4_SIO_IR_S0_DELTA_CTS,
540 IOC4_SIO_IR_S0_TX_MT, IOC4_SIO_IR_S0_RX_TIMER,
541 IOC4_SIO_IR_S0_RX_HIGH, IOC4_SIO_IR_S0_TX_EXPLICIT,
542 IOC4_OTHER_IR_S0_MEMERR,
543 (IOC4_SIO_IR_S0_TX_MT | IOC4_SIO_IR_S0_RX_FULL |
544 IOC4_SIO_IR_S0_RX_HIGH | IOC4_SIO_IR_S0_RX_TIMER |
545 IOC4_SIO_IR_S0_DELTA_DCD | IOC4_SIO_IR_S0_DELTA_CTS |
546 IOC4_SIO_IR_S0_INT | IOC4_SIO_IR_S0_TX_EXPLICIT),
547 IOC4_SIO_IR_S0, IOC4_GPPR_UART0_MODESEL_PIN,
548 },
549
550 /* Values for port 1 */
551 {
552 IOC4_SIO_IR_S1_DELTA_DCD, IOC4_SIO_IR_S1_DELTA_CTS,
553 IOC4_SIO_IR_S1_TX_MT, IOC4_SIO_IR_S1_RX_TIMER,
554 IOC4_SIO_IR_S1_RX_HIGH, IOC4_SIO_IR_S1_TX_EXPLICIT,
555 IOC4_OTHER_IR_S1_MEMERR,
556 (IOC4_SIO_IR_S1_TX_MT | IOC4_SIO_IR_S1_RX_FULL |
557 IOC4_SIO_IR_S1_RX_HIGH | IOC4_SIO_IR_S1_RX_TIMER |
558 IOC4_SIO_IR_S1_DELTA_DCD | IOC4_SIO_IR_S1_DELTA_CTS |
559 IOC4_SIO_IR_S1_INT | IOC4_SIO_IR_S1_TX_EXPLICIT),
560 IOC4_SIO_IR_S1, IOC4_GPPR_UART1_MODESEL_PIN,
561 },
562
563 /* Values for port 2 */
564 {
565 IOC4_SIO_IR_S2_DELTA_DCD, IOC4_SIO_IR_S2_DELTA_CTS,
566 IOC4_SIO_IR_S2_TX_MT, IOC4_SIO_IR_S2_RX_TIMER,
567 IOC4_SIO_IR_S2_RX_HIGH, IOC4_SIO_IR_S2_TX_EXPLICIT,
568 IOC4_OTHER_IR_S2_MEMERR,
569 (IOC4_SIO_IR_S2_TX_MT | IOC4_SIO_IR_S2_RX_FULL |
570 IOC4_SIO_IR_S2_RX_HIGH | IOC4_SIO_IR_S2_RX_TIMER |
571 IOC4_SIO_IR_S2_DELTA_DCD | IOC4_SIO_IR_S2_DELTA_CTS |
572 IOC4_SIO_IR_S2_INT | IOC4_SIO_IR_S2_TX_EXPLICIT),
573 IOC4_SIO_IR_S2, IOC4_GPPR_UART2_MODESEL_PIN,
574 },
575
576 /* Values for port 3 */
577 {
578 IOC4_SIO_IR_S3_DELTA_DCD, IOC4_SIO_IR_S3_DELTA_CTS,
579 IOC4_SIO_IR_S3_TX_MT, IOC4_SIO_IR_S3_RX_TIMER,
580 IOC4_SIO_IR_S3_RX_HIGH, IOC4_SIO_IR_S3_TX_EXPLICIT,
581 IOC4_OTHER_IR_S3_MEMERR,
582 (IOC4_SIO_IR_S3_TX_MT | IOC4_SIO_IR_S3_RX_FULL |
583 IOC4_SIO_IR_S3_RX_HIGH | IOC4_SIO_IR_S3_RX_TIMER |
584 IOC4_SIO_IR_S3_DELTA_DCD | IOC4_SIO_IR_S3_DELTA_CTS |
585 IOC4_SIO_IR_S3_INT | IOC4_SIO_IR_S3_TX_EXPLICIT),
586 IOC4_SIO_IR_S3, IOC4_GPPR_UART3_MODESEL_PIN,
587 }
588};
589
590/* A ring buffer entry */
591struct ring_entry {
592 union {
593 struct {
594 uint32_t alldata;
595 uint32_t allsc;
596 } all;
597 struct {
598 char data[4]; /* data bytes */
599 char sc[4]; /* status/control */
600 } s;
601 } u;
602};
603
604/* Test the valid bits in any of the 4 sc chars using "allsc" member */
605#define RING_ANY_VALID \
606 ((uint32_t)(IOC4_RXSB_MODEM_VALID | IOC4_RXSB_DATA_VALID) * 0x01010101)
607
608#define ring_sc u.s.sc
609#define ring_data u.s.data
610#define ring_allsc u.all.allsc
611
612/* Number of entries per ring buffer. */
613#define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
614
615/* An individual ring */
616struct ring {
617 struct ring_entry entries[ENTRIES_PER_RING];
618};
619
620/* The whole enchilada */
621struct ring_buffer {
622 struct ring TX_0_OR_2;
623 struct ring RX_0_OR_2;
624 struct ring TX_1_OR_3;
625 struct ring RX_1_OR_3;
626};
627
628/* Get a ring from a port struct */
629#define RING(_p, _wh) &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
630
631/* Infinite loop detection.
632 */
633#define MAXITER 10000000
634
635/* Prototypes */
636static void receive_chars(struct uart_port *);
637static void handle_intr(void *arg, uint32_t sio_ir);
638
639/*
640 * port_is_active - determines if this port is currently active
641 * @port: ptr to soft struct for this port
642 * @uart_port: uart port to test for
643 */
644static inline int port_is_active(struct ioc4_port *port,
645 struct uart_port *uart_port)
646{
647 if (port) {
648 if ((port->ip_flags & PORT_ACTIVE)
649 && (port->ip_port == uart_port))
650 return 1;
651 }
652 return 0;
653}
654
655
656/**
657 * write_ireg - write the interrupt regs
658 * @ioc4_soft: ptr to soft struct for this port
659 * @val: value to write
660 * @which: which register
661 * @type: which ireg set
662 */
663static inline void
664write_ireg(struct ioc4_soft *ioc4_soft, uint32_t val, int which, int type)
665{
666 struct ioc4_misc_regs __iomem *mem = ioc4_soft->is_ioc4_misc_addr;
667 unsigned long flags;
668
669 spin_lock_irqsave(&ioc4_soft->is_ir_lock, flags);
670
671 switch (type) {
672 case IOC4_SIO_INTR_TYPE:
673 switch (which) {
674 case IOC4_W_IES:
675 writel(val, &mem->sio_ies.raw);
676 break;
677
678 case IOC4_W_IEC:
679 writel(val, &mem->sio_iec.raw);
680 break;
681 }
682 break;
683
684 case IOC4_OTHER_INTR_TYPE:
685 switch (which) {
686 case IOC4_W_IES:
687 writel(val, &mem->other_ies.raw);
688 break;
689
690 case IOC4_W_IEC:
691 writel(val, &mem->other_iec.raw);
692 break;
693 }
694 break;
695
696 default:
697 break;
698 }
699 spin_unlock_irqrestore(&ioc4_soft->is_ir_lock, flags);
700}
701
702/**
703 * set_baud - Baud rate setting code
704 * @port: port to set
705 * @baud: baud rate to use
706 */
707static int set_baud(struct ioc4_port *port, int baud)
708{
709 int actual_baud;
710 int diff;
711 int lcr;
712 unsigned short divisor;
713 struct ioc4_uartregs __iomem *uart;
714
715 divisor = SER_DIVISOR(baud, port->ip_pci_bus_speed);
716 if (!divisor)
717 return 1;
718 actual_baud = DIVISOR_TO_BAUD(divisor, port->ip_pci_bus_speed);
719
720 diff = actual_baud - baud;
721 if (diff < 0)
722 diff = -diff;
723
724 /* If we're within 1%, we've found a match */
725 if (diff * 100 > actual_baud)
726 return 1;
727
728 uart = port->ip_uart_regs;
729 lcr = readb(&uart->i4u_lcr);
730 writeb(lcr | UART_LCR_DLAB, &uart->i4u_lcr);
731 writeb((unsigned char)divisor, &uart->i4u_dll);
732 writeb((unsigned char)(divisor >> 8), &uart->i4u_dlm);
733 writeb(lcr, &uart->i4u_lcr);
734 return 0;
735}
736
737
738/**
739 * get_ioc4_port - given a uart port, return the control structure
740 * @port: uart port
741 * @set: set this port as current
742 */
743static struct ioc4_port *get_ioc4_port(struct uart_port *the_port, int set)
744{
745 struct ioc4_driver_data *idd = dev_get_drvdata(the_port->dev);
746 struct ioc4_control *control = idd->idd_serial_data;
747 struct ioc4_port *port;
748 int port_num, port_type;
749
750 if (control) {
751 for ( port_num = 0; port_num < IOC4_NUM_SERIAL_PORTS;
752 port_num++ ) {
753 port = control->ic_port[port_num].icp_port;
754 if (!port)
755 continue;
756 for (port_type = UART_PORT_MIN;
757 port_type < UART_PORT_COUNT;
758 port_type++) {
759 if (the_port == port->ip_all_ports
760 [port_type]) {
761 /* set local copy */
762 if (set) {
763 port->ip_port = the_port;
764 }
765 return port;
766 }
767 }
768 }
769 }
770 return NULL;
771}
772
773/* The IOC4 hardware provides no atomic way to determine if interrupts
774 * are pending since two reads are required to do so. The handler must
775 * read the SIO_IR and the SIO_IES, and take the logical and of the
776 * two. When this value is zero, all interrupts have been serviced and
777 * the handler may return.
778 *
779 * This has the unfortunate "hole" that, if some other CPU or
780 * some other thread or some higher level interrupt manages to
781 * modify SIO_IE between our reads of SIO_IR and SIO_IE, we may
782 * think we have observed SIO_IR&SIO_IE==0 when in fact this
783 * condition never really occurred.
784 *
785 * To solve this, we use a simple spinlock that must be held
786 * whenever modifying SIO_IE; holding this lock while observing
787 * both SIO_IR and SIO_IE guarantees that we do not falsely
788 * conclude that no enabled interrupts are pending.
789 */
790
791static inline uint32_t
792pending_intrs(struct ioc4_soft *soft, int type)
793{
794 struct ioc4_misc_regs __iomem *mem = soft->is_ioc4_misc_addr;
795 unsigned long flag;
796 uint32_t intrs = 0;
797
798 BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
799 || (type == IOC4_OTHER_INTR_TYPE)));
800
801 spin_lock_irqsave(&soft->is_ir_lock, flag);
802
803 switch (type) {
804 case IOC4_SIO_INTR_TYPE:
805 intrs = readl(&mem->sio_ir.raw) & readl(&mem->sio_ies.raw);
806 break;
807
808 case IOC4_OTHER_INTR_TYPE:
809 intrs = readl(&mem->other_ir.raw) & readl(&mem->other_ies.raw);
810
811 /* Don't process any ATA interrupte */
812 intrs &= ~(IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
813 break;
814
815 default:
816 break;
817 }
818 spin_unlock_irqrestore(&soft->is_ir_lock, flag);
819 return intrs;
820}
821
822/**
823 * port_init - Initialize the sio and ioc4 hardware for a given port
824 * called per port from attach...
825 * @port: port to initialize
826 */
827static int inline port_init(struct ioc4_port *port)
828{
829 uint32_t sio_cr;
830 struct hooks *hooks = port->ip_hooks;
831 struct ioc4_uartregs __iomem *uart;
832
833 /* Idle the IOC4 serial interface */
834 writel(IOC4_SSCR_RESET, &port->ip_serial_regs->sscr);
835
836 /* Wait until any pending bus activity for this port has ceased */
837 do
838 sio_cr = readl(&port->ip_mem->sio_cr.raw);
839 while (!(sio_cr & IOC4_SIO_CR_SIO_DIAG_IDLE));
840
841 /* Finish reset sequence */
842 writel(0, &port->ip_serial_regs->sscr);
843
844 /* Once RESET is done, reload cached tx_prod and rx_cons values
845 * and set rings to empty by making prod == cons
846 */
847 port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
848 writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
849 port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
850 writel(port->ip_rx_cons | IOC4_SRCIR_ARM, &port->ip_serial_regs->srcir);
851
852 /* Disable interrupts for this 16550 */
853 uart = port->ip_uart_regs;
854 writeb(0, &uart->i4u_lcr);
855 writeb(0, &uart->i4u_ier);
856
857 /* Set the default baud */
858 set_baud(port, port->ip_baud);
859
860 /* Set line control to 8 bits no parity */
861 writeb(UART_LCR_WLEN8 | 0, &uart->i4u_lcr);
862 /* UART_LCR_STOP == 1 stop */
863
864 /* Enable the FIFOs */
865 writeb(UART_FCR_ENABLE_FIFO, &uart->i4u_fcr);
866 /* then reset 16550 FIFOs */
867 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
868 &uart->i4u_fcr);
869
870 /* Clear modem control register */
871 writeb(0, &uart->i4u_mcr);
872
873 /* Clear deltas in modem status register */
874 readb(&uart->i4u_msr);
875
876 /* Only do this once per port pair */
877 if (port->ip_hooks == &hooks_array[0]
878 || port->ip_hooks == &hooks_array[2]) {
879 unsigned long ring_pci_addr;
880 uint32_t __iomem *sbbr_l;
881 uint32_t __iomem *sbbr_h;
882
883 if (port->ip_hooks == &hooks_array[0]) {
884 sbbr_l = &port->ip_serial->sbbr01_l;
885 sbbr_h = &port->ip_serial->sbbr01_h;
886 } else {
887 sbbr_l = &port->ip_serial->sbbr23_l;
888 sbbr_h = &port->ip_serial->sbbr23_h;
889 }
890
891 ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
892 DPRINT_CONFIG(("%s: ring_pci_addr 0x%lx\n",
893 __func__, ring_pci_addr));
894
895 writel((unsigned int)((uint64_t)ring_pci_addr >> 32), sbbr_h);
896 writel((unsigned int)ring_pci_addr | IOC4_BUF_SIZE_BIT, sbbr_l);
897 }
898
899 /* Set the receive timeout value to 10 msec */
900 writel(IOC4_SRTR_HZ / 100, &port->ip_serial_regs->srtr);
901
902 /* Set rx threshold, enable DMA */
903 /* Set high water mark at 3/4 of full ring */
904 port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
905 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
906
907 /* Disable and clear all serial related interrupt bits */
908 write_ireg(port->ip_ioc4_soft, hooks->intr_clear,
909 IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
910 port->ip_ienb &= ~hooks->intr_clear;
911 writel(hooks->intr_clear, &port->ip_mem->sio_ir.raw);
912 return 0;
913}
914
915/**
916 * handle_dma_error_intr - service any pending DMA error interrupts for the
917 * given port - 2nd level called via sd_intr
918 * @arg: handler arg
919 * @other_ir: ioc4regs
920 */
921static void handle_dma_error_intr(void *arg, uint32_t other_ir)
922{
923 struct ioc4_port *port = (struct ioc4_port *)arg;
924 struct hooks *hooks = port->ip_hooks;
925 unsigned long flags;
926
927 spin_lock_irqsave(&port->ip_lock, flags);
928
929 /* ACK the interrupt */
930 writel(hooks->intr_dma_error, &port->ip_mem->other_ir.raw);
931
932 if (readl(&port->ip_mem->pci_err_addr_l.raw) & IOC4_PCI_ERR_ADDR_VLD) {
933 printk(KERN_ERR
934 "PCI error address is 0x%llx, "
935 "master is serial port %c %s\n",
936 (((uint64_t)readl(&port->ip_mem->pci_err_addr_h)
937 << 32)
938 | readl(&port->ip_mem->pci_err_addr_l.raw))
939 & IOC4_PCI_ERR_ADDR_ADDR_MSK, '1' +
940 ((char)(readl(&port->ip_mem->pci_err_addr_l.raw) &
941 IOC4_PCI_ERR_ADDR_MST_NUM_MSK) >> 1),
942 (readl(&port->ip_mem->pci_err_addr_l.raw)
943 & IOC4_PCI_ERR_ADDR_MST_TYP_MSK)
944 ? "RX" : "TX");
945
946 if (readl(&port->ip_mem->pci_err_addr_l.raw)
947 & IOC4_PCI_ERR_ADDR_MUL_ERR) {
948 printk(KERN_ERR
949 "Multiple errors occurred\n");
950 }
951 }
952 spin_unlock_irqrestore(&port->ip_lock, flags);
953
954 /* Re-enable DMA error interrupts */
955 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error, IOC4_W_IES,
956 IOC4_OTHER_INTR_TYPE);
957}
958
959/**
960 * intr_connect - interrupt connect function
961 * @soft: soft struct for this card
962 * @type: interrupt type
963 * @intrbits: bit pattern to set
964 * @intr: handler function
965 * @info: handler arg
966 */
967static void
968intr_connect(struct ioc4_soft *soft, int type,
969 uint32_t intrbits, ioc4_intr_func_f * intr, void *info)
970{
971 int i;
972 struct ioc4_intr_info *intr_ptr;
973
974 BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
975 || (type == IOC4_OTHER_INTR_TYPE)));
976
977 i = atomic_inc(&soft-> is_intr_type[type].is_num_intrs) - 1;
978 BUG_ON(!(i < MAX_IOC4_INTR_ENTS || (printk("i %d\n", i), 0)));
979
980 /* Save off the lower level interrupt handler */
981 intr_ptr = &soft->is_intr_type[type].is_intr_info[i];
982 intr_ptr->sd_bits = intrbits;
983 intr_ptr->sd_intr = intr;
984 intr_ptr->sd_info = info;
985}
986
987/**
988 * ioc4_intr - Top level IOC4 interrupt handler.
989 * @irq: irq value
990 * @arg: handler arg
991 */
992
993static irqreturn_t ioc4_intr(int irq, void *arg)
994{
995 struct ioc4_soft *soft;
996 uint32_t this_ir, this_mir;
997 int xx, num_intrs = 0;
998 int intr_type;
999 int handled = 0;
1000 struct ioc4_intr_info *intr_info;
1001
1002 soft = arg;
1003 for (intr_type = 0; intr_type < IOC4_NUM_INTR_TYPES; intr_type++) {
1004 num_intrs = (int)atomic_read(
1005 &soft->is_intr_type[intr_type].is_num_intrs);
1006
1007 this_mir = this_ir = pending_intrs(soft, intr_type);
1008
1009 /* Farm out the interrupt to the various drivers depending on
1010 * which interrupt bits are set.
1011 */
1012 for (xx = 0; xx < num_intrs; xx++) {
1013 intr_info = &soft->is_intr_type[intr_type].is_intr_info[xx];
1014 if ((this_mir = this_ir & intr_info->sd_bits)) {
1015 /* Disable owned interrupts, call handler */
1016 handled++;
1017 write_ireg(soft, intr_info->sd_bits, IOC4_W_IEC,
1018 intr_type);
1019 intr_info->sd_intr(intr_info->sd_info, this_mir);
1020 this_ir &= ~this_mir;
1021 }
1022 }
1023 }
1024#ifdef DEBUG_INTERRUPTS
1025 {
1026 struct ioc4_misc_regs __iomem *mem = soft->is_ioc4_misc_addr;
1027 unsigned long flag;
1028
1029 spin_lock_irqsave(&soft->is_ir_lock, flag);
1030 printk ("%s : %d : mem 0x%p sio_ir 0x%x sio_ies 0x%x "
1031 "other_ir 0x%x other_ies 0x%x mask 0x%x\n",
1032 __func__, __LINE__,
1033 (void *)mem, readl(&mem->sio_ir.raw),
1034 readl(&mem->sio_ies.raw),
1035 readl(&mem->other_ir.raw),
1036 readl(&mem->other_ies.raw),
1037 IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
1038 spin_unlock_irqrestore(&soft->is_ir_lock, flag);
1039 }
1040#endif
1041 return handled ? IRQ_HANDLED : IRQ_NONE;
1042}
1043
1044/**
1045 * ioc4_attach_local - Device initialization.
1046 * Called at *_attach() time for each
1047 * IOC4 with serial ports in the system.
1048 * @idd: Master module data for this IOC4
1049 */
1050static int inline ioc4_attach_local(struct ioc4_driver_data *idd)
1051{
1052 struct ioc4_port *port;
1053 struct ioc4_port *ports[IOC4_NUM_SERIAL_PORTS];
1054 int port_number;
1055 uint16_t ioc4_revid_min = 62;
1056 uint16_t ioc4_revid;
1057 struct pci_dev *pdev = idd->idd_pdev;
1058 struct ioc4_control* control = idd->idd_serial_data;
1059 struct ioc4_soft *soft = control->ic_soft;
1060 void __iomem *ioc4_misc = idd->idd_misc_regs;
1061 void __iomem *ioc4_serial = soft->is_ioc4_serial_addr;
1062
1063 /* IOC4 firmware must be at least rev 62 */
1064 pci_read_config_word(pdev, PCI_COMMAND_SPECIAL, &ioc4_revid);
1065
1066 printk(KERN_INFO "IOC4 firmware revision %d\n", ioc4_revid);
1067 if (ioc4_revid < ioc4_revid_min) {
1068 printk(KERN_WARNING
1069 "IOC4 serial not supported on firmware rev %d, "
1070 "please upgrade to rev %d or higher\n",
1071 ioc4_revid, ioc4_revid_min);
1072 return -EPERM;
1073 }
1074 BUG_ON(ioc4_misc == NULL);
1075 BUG_ON(ioc4_serial == NULL);
1076
1077 /* Create port structures for each port */
1078 for (port_number = 0; port_number < IOC4_NUM_SERIAL_PORTS;
1079 port_number++) {
1080 port = kzalloc(sizeof(struct ioc4_port), GFP_KERNEL);
1081 if (!port) {
1082 printk(KERN_WARNING
1083 "IOC4 serial memory not available for port\n");
1084 return -ENOMEM;
1085 }
1086 spin_lock_init(&port->ip_lock);
1087
1088 /* we need to remember the previous ones, to point back to
1089 * them farther down - setting up the ring buffers.
1090 */
1091 ports[port_number] = port;
1092
1093 /* Allocate buffers and jumpstart the hardware. */
1094 control->ic_port[port_number].icp_port = port;
1095 port->ip_ioc4_soft = soft;
1096 port->ip_pdev = pdev;
1097 port->ip_ienb = 0;
1098 /* Use baud rate calculations based on detected PCI
1099 * bus speed. Simply test whether the PCI clock is
1100 * running closer to 66MHz or 33MHz.
1101 */
1102 if (idd->count_period/IOC4_EXTINT_COUNT_DIVISOR < 20) {
1103 port->ip_pci_bus_speed = IOC4_SER_XIN_CLK_66;
1104 } else {
1105 port->ip_pci_bus_speed = IOC4_SER_XIN_CLK_33;
1106 }
1107 port->ip_baud = 9600;
1108 port->ip_control = control;
1109 port->ip_mem = ioc4_misc;
1110 port->ip_serial = ioc4_serial;
1111
1112 /* point to the right hook */
1113 port->ip_hooks = &hooks_array[port_number];
1114
1115 /* Get direct hooks to the serial regs and uart regs
1116 * for this port
1117 */
1118 switch (port_number) {
1119 case 0:
1120 port->ip_serial_regs = &(port->ip_serial->port_0);
1121 port->ip_uart_regs = &(port->ip_serial->uart_0);
1122 break;
1123 case 1:
1124 port->ip_serial_regs = &(port->ip_serial->port_1);
1125 port->ip_uart_regs = &(port->ip_serial->uart_1);
1126 break;
1127 case 2:
1128 port->ip_serial_regs = &(port->ip_serial->port_2);
1129 port->ip_uart_regs = &(port->ip_serial->uart_2);
1130 break;
1131 default:
1132 case 3:
1133 port->ip_serial_regs = &(port->ip_serial->port_3);
1134 port->ip_uart_regs = &(port->ip_serial->uart_3);
1135 break;
1136 }
1137
1138 /* ring buffers are 1 to a pair of ports */
1139 if (port_number && (port_number & 1)) {
1140 /* odd use the evens buffer */
1141 port->ip_dma_ringbuf =
1142 ports[port_number - 1]->ip_dma_ringbuf;
1143 port->ip_cpu_ringbuf =
1144 ports[port_number - 1]->ip_cpu_ringbuf;
1145 port->ip_inring = RING(port, RX_1_OR_3);
1146 port->ip_outring = RING(port, TX_1_OR_3);
1147
1148 } else {
1149 if (port->ip_dma_ringbuf == 0) {
1150 port->ip_cpu_ringbuf = pci_alloc_consistent
1151 (pdev, TOTAL_RING_BUF_SIZE,
1152 &port->ip_dma_ringbuf);
1153
1154 }
1155 BUG_ON(!((((int64_t)port->ip_dma_ringbuf) &
1156 (TOTAL_RING_BUF_SIZE - 1)) == 0));
1157 DPRINT_CONFIG(("%s : ip_cpu_ringbuf 0x%p "
1158 "ip_dma_ringbuf 0x%p\n",
1159 __func__,
1160 (void *)port->ip_cpu_ringbuf,
1161 (void *)port->ip_dma_ringbuf));
1162 port->ip_inring = RING(port, RX_0_OR_2);
1163 port->ip_outring = RING(port, TX_0_OR_2);
1164 }
1165 DPRINT_CONFIG(("%s : port %d [addr 0x%p] control 0x%p",
1166 __func__,
1167 port_number, (void *)port, (void *)control));
1168 DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
1169 (void *)port->ip_serial_regs,
1170 (void *)port->ip_uart_regs));
1171
1172 /* Initialize the hardware for IOC4 */
1173 port_init(port);
1174
1175 DPRINT_CONFIG(("%s: port_number %d port 0x%p inring 0x%p "
1176 "outring 0x%p\n",
1177 __func__,
1178 port_number, (void *)port,
1179 (void *)port->ip_inring,
1180 (void *)port->ip_outring));
1181
1182 /* Attach interrupt handlers */
1183 intr_connect(soft, IOC4_SIO_INTR_TYPE,
1184 GET_SIO_IR(port_number),
1185 handle_intr, port);
1186
1187 intr_connect(soft, IOC4_OTHER_INTR_TYPE,
1188 GET_OTHER_IR(port_number),
1189 handle_dma_error_intr, port);
1190 }
1191 return 0;
1192}
1193
1194/**
1195 * enable_intrs - enable interrupts
1196 * @port: port to enable
1197 * @mask: mask to use
1198 */
1199static void enable_intrs(struct ioc4_port *port, uint32_t mask)
1200{
1201 struct hooks *hooks = port->ip_hooks;
1202
1203 if ((port->ip_ienb & mask) != mask) {
1204 write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IES,
1205 IOC4_SIO_INTR_TYPE);
1206 port->ip_ienb |= mask;
1207 }
1208
1209 if (port->ip_ienb)
1210 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
1211 IOC4_W_IES, IOC4_OTHER_INTR_TYPE);
1212}
1213
1214/**
1215 * local_open - local open a port
1216 * @port: port to open
1217 */
1218static inline int local_open(struct ioc4_port *port)
1219{
1220 int spiniter = 0;
1221
1222 port->ip_flags = PORT_ACTIVE;
1223
1224 /* Pause the DMA interface if necessary */
1225 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1226 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1227 &port->ip_serial_regs->sscr);
1228 while((readl(&port->ip_serial_regs-> sscr)
1229 & IOC4_SSCR_PAUSE_STATE) == 0) {
1230 spiniter++;
1231 if (spiniter > MAXITER) {
1232 port->ip_flags = PORT_INACTIVE;
1233 return -1;
1234 }
1235 }
1236 }
1237
1238 /* Reset the input fifo. If the uart received chars while the port
1239 * was closed and DMA is not enabled, the uart may have a bunch of
1240 * chars hanging around in its rx fifo which will not be discarded
1241 * by rclr in the upper layer. We must get rid of them here.
1242 */
1243 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
1244 &port->ip_uart_regs->i4u_fcr);
1245
1246 writeb(UART_LCR_WLEN8, &port->ip_uart_regs->i4u_lcr);
1247 /* UART_LCR_STOP == 1 stop */
1248
1249 /* Re-enable DMA, set default threshold to intr whenever there is
1250 * data available.
1251 */
1252 port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
1253 port->ip_sscr |= 1; /* default threshold */
1254
1255 /* Plug in the new sscr. This implicitly clears the DMA_PAUSE
1256 * flag if it was set above
1257 */
1258 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1259 port->ip_tx_lowat = 1;
1260 return 0;
1261}
1262
1263/**
1264 * set_rx_timeout - Set rx timeout and threshold values.
1265 * @port: port to use
1266 * @timeout: timeout value in ticks
1267 */
1268static inline int set_rx_timeout(struct ioc4_port *port, int timeout)
1269{
1270 int threshold;
1271
1272 port->ip_rx_timeout = timeout;
1273
1274 /* Timeout is in ticks. Let's figure out how many chars we
1275 * can receive at the current baud rate in that interval
1276 * and set the rx threshold to that amount. There are 4 chars
1277 * per ring entry, so we'll divide the number of chars that will
1278 * arrive in timeout by 4.
1279 * So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
1280 */
1281 threshold = timeout * port->ip_baud / 4000;
1282 if (threshold == 0)
1283 threshold = 1; /* otherwise we'll intr all the time! */
1284
1285 if ((unsigned)threshold > (unsigned)IOC4_SSCR_RX_THRESHOLD)
1286 return 1;
1287
1288 port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
1289 port->ip_sscr |= threshold;
1290
1291 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1292
1293 /* Now set the rx timeout to the given value
1294 * again timeout * IOC4_SRTR_HZ / HZ
1295 */
1296 timeout = timeout * IOC4_SRTR_HZ / 100;
1297 if (timeout > IOC4_SRTR_CNT)
1298 timeout = IOC4_SRTR_CNT;
1299
1300 writel(timeout, &port->ip_serial_regs->srtr);
1301 return 0;
1302}
1303
1304/**
1305 * config_port - config the hardware
1306 * @port: port to config
1307 * @baud: baud rate for the port
1308 * @byte_size: data size
1309 * @stop_bits: number of stop bits
1310 * @parenb: parity enable ?
1311 * @parodd: odd parity ?
1312 */
1313static inline int
1314config_port(struct ioc4_port *port,
1315 int baud, int byte_size, int stop_bits, int parenb, int parodd)
1316{
1317 char lcr, sizebits;
1318 int spiniter = 0;
1319
1320 DPRINT_CONFIG(("%s: baud %d byte_size %d stop %d parenb %d parodd %d\n",
1321 __func__, baud, byte_size, stop_bits, parenb, parodd));
1322
1323 if (set_baud(port, baud))
1324 return 1;
1325
1326 switch (byte_size) {
1327 case 5:
1328 sizebits = UART_LCR_WLEN5;
1329 break;
1330 case 6:
1331 sizebits = UART_LCR_WLEN6;
1332 break;
1333 case 7:
1334 sizebits = UART_LCR_WLEN7;
1335 break;
1336 case 8:
1337 sizebits = UART_LCR_WLEN8;
1338 break;
1339 default:
1340 return 1;
1341 }
1342
1343 /* Pause the DMA interface if necessary */
1344 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1345 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1346 &port->ip_serial_regs->sscr);
1347 while((readl(&port->ip_serial_regs->sscr)
1348 & IOC4_SSCR_PAUSE_STATE) == 0) {
1349 spiniter++;
1350 if (spiniter > MAXITER)
1351 return -1;
1352 }
1353 }
1354
1355 /* Clear relevant fields in lcr */
1356 lcr = readb(&port->ip_uart_regs->i4u_lcr);
1357 lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
1358 UART_LCR_PARITY | LCR_MASK_STOP_BITS);
1359
1360 /* Set byte size in lcr */
1361 lcr |= sizebits;
1362
1363 /* Set parity */
1364 if (parenb) {
1365 lcr |= UART_LCR_PARITY;
1366 if (!parodd)
1367 lcr |= UART_LCR_EPAR;
1368 }
1369
1370 /* Set stop bits */
1371 if (stop_bits)
1372 lcr |= UART_LCR_STOP /* 2 stop bits */ ;
1373
1374 writeb(lcr, &port->ip_uart_regs->i4u_lcr);
1375
1376 /* Re-enable the DMA interface if necessary */
1377 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1378 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1379 }
1380 port->ip_baud = baud;
1381
1382 /* When we get within this number of ring entries of filling the
1383 * entire ring on tx, place an EXPLICIT intr to generate a lowat
1384 * notification when output has drained.
1385 */
1386 port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
1387 if (port->ip_tx_lowat == 0)
1388 port->ip_tx_lowat = 1;
1389
1390 set_rx_timeout(port, 2);
1391
1392 return 0;
1393}
1394
1395/**
1396 * do_write - Write bytes to the port. Returns the number of bytes
1397 * actually written. Called from transmit_chars
1398 * @port: port to use
1399 * @buf: the stuff to write
1400 * @len: how many bytes in 'buf'
1401 */
1402static inline int do_write(struct ioc4_port *port, char *buf, int len)
1403{
1404 int prod_ptr, cons_ptr, total = 0;
1405 struct ring *outring;
1406 struct ring_entry *entry;
1407 struct hooks *hooks = port->ip_hooks;
1408
1409 BUG_ON(!(len >= 0));
1410
1411 prod_ptr = port->ip_tx_prod;
1412 cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
1413 outring = port->ip_outring;
1414
1415 /* Maintain a 1-entry red-zone. The ring buffer is full when
1416 * (cons - prod) % ring_size is 1. Rather than do this subtraction
1417 * in the body of the loop, I'll do it now.
1418 */
1419 cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
1420
1421 /* Stuff the bytes into the output */
1422 while ((prod_ptr != cons_ptr) && (len > 0)) {
1423 int xx;
1424
1425 /* Get 4 bytes (one ring entry) at a time */
1426 entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
1427
1428 /* Invalidate all entries */
1429 entry->ring_allsc = 0;
1430
1431 /* Copy in some bytes */
1432 for (xx = 0; (xx < 4) && (len > 0); xx++) {
1433 entry->ring_data[xx] = *buf++;
1434 entry->ring_sc[xx] = IOC4_TXCB_VALID;
1435 len--;
1436 total++;
1437 }
1438
1439 /* If we are within some small threshold of filling up the
1440 * entire ring buffer, we must place an EXPLICIT intr here
1441 * to generate a lowat interrupt in case we subsequently
1442 * really do fill up the ring and the caller goes to sleep.
1443 * No need to place more than one though.
1444 */
1445 if (!(port->ip_flags & LOWAT_WRITTEN) &&
1446 ((cons_ptr - prod_ptr) & PROD_CONS_MASK)
1447 <= port->ip_tx_lowat
1448 * (int)sizeof(struct ring_entry)) {
1449 port->ip_flags |= LOWAT_WRITTEN;
1450 entry->ring_sc[0] |= IOC4_TXCB_INT_WHEN_DONE;
1451 }
1452
1453 /* Go on to next entry */
1454 prod_ptr += sizeof(struct ring_entry);
1455 prod_ptr &= PROD_CONS_MASK;
1456 }
1457
1458 /* If we sent something, start DMA if necessary */
1459 if (total > 0 && !(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
1460 port->ip_sscr |= IOC4_SSCR_DMA_EN;
1461 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1462 }
1463
1464 /* Store the new producer pointer. If tx is disabled, we stuff the
1465 * data into the ring buffer, but we don't actually start tx.
1466 */
1467 if (!uart_tx_stopped(port->ip_port)) {
1468 writel(prod_ptr, &port->ip_serial_regs->stpir);
1469
1470 /* If we are now transmitting, enable tx_mt interrupt so we
1471 * can disable DMA if necessary when the tx finishes.
1472 */
1473 if (total > 0)
1474 enable_intrs(port, hooks->intr_tx_mt);
1475 }
1476 port->ip_tx_prod = prod_ptr;
1477 return total;
1478}
1479
1480/**
1481 * disable_intrs - disable interrupts
1482 * @port: port to enable
1483 * @mask: mask to use
1484 */
1485static void disable_intrs(struct ioc4_port *port, uint32_t mask)
1486{
1487 struct hooks *hooks = port->ip_hooks;
1488
1489 if (port->ip_ienb & mask) {
1490 write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IEC,
1491 IOC4_SIO_INTR_TYPE);
1492 port->ip_ienb &= ~mask;
1493 }
1494
1495 if (!port->ip_ienb)
1496 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
1497 IOC4_W_IEC, IOC4_OTHER_INTR_TYPE);
1498}
1499
1500/**
1501 * set_notification - Modify event notification
1502 * @port: port to use
1503 * @mask: events mask
1504 * @set_on: set ?
1505 */
1506static int set_notification(struct ioc4_port *port, int mask, int set_on)
1507{
1508 struct hooks *hooks = port->ip_hooks;
1509 uint32_t intrbits, sscrbits;
1510
1511 BUG_ON(!mask);
1512
1513 intrbits = sscrbits = 0;
1514
1515 if (mask & N_DATA_READY)
1516 intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
1517 if (mask & N_OUTPUT_LOWAT)
1518 intrbits |= hooks->intr_tx_explicit;
1519 if (mask & N_DDCD) {
1520 intrbits |= hooks->intr_delta_dcd;
1521 sscrbits |= IOC4_SSCR_RX_RING_DCD;
1522 }
1523 if (mask & N_DCTS)
1524 intrbits |= hooks->intr_delta_cts;
1525
1526 if (set_on) {
1527 enable_intrs(port, intrbits);
1528 port->ip_notify |= mask;
1529 port->ip_sscr |= sscrbits;
1530 } else {
1531 disable_intrs(port, intrbits);
1532 port->ip_notify &= ~mask;
1533 port->ip_sscr &= ~sscrbits;
1534 }
1535
1536 /* We require DMA if either DATA_READY or DDCD notification is
1537 * currently requested. If neither of these is requested and
1538 * there is currently no tx in progress, DMA may be disabled.
1539 */
1540 if (port->ip_notify & (N_DATA_READY | N_DDCD))
1541 port->ip_sscr |= IOC4_SSCR_DMA_EN;
1542 else if (!(port->ip_ienb & hooks->intr_tx_mt))
1543 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
1544
1545 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1546 return 0;
1547}
1548
1549/**
1550 * set_mcr - set the master control reg
1551 * @the_port: port to use
1552 * @mask1: mcr mask
1553 * @mask2: shadow mask
1554 */
1555static inline int set_mcr(struct uart_port *the_port,
1556 int mask1, int mask2)
1557{
1558 struct ioc4_port *port = get_ioc4_port(the_port, 0);
1559 uint32_t shadow;
1560 int spiniter = 0;
1561 char mcr;
1562
1563 if (!port)
1564 return -1;
1565
1566 /* Pause the DMA interface if necessary */
1567 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1568 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1569 &port->ip_serial_regs->sscr);
1570 while ((readl(&port->ip_serial_regs->sscr)
1571 & IOC4_SSCR_PAUSE_STATE) == 0) {
1572 spiniter++;
1573 if (spiniter > MAXITER)
1574 return -1;
1575 }
1576 }
1577 shadow = readl(&port->ip_serial_regs->shadow);
1578 mcr = (shadow & 0xff000000) >> 24;
1579
1580 /* Set new value */
1581 mcr |= mask1;
1582 shadow |= mask2;
1583
1584 writeb(mcr, &port->ip_uart_regs->i4u_mcr);
1585 writel(shadow, &port->ip_serial_regs->shadow);
1586
1587 /* Re-enable the DMA interface if necessary */
1588 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1589 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1590 }
1591 return 0;
1592}
1593
1594/**
1595 * ioc4_set_proto - set the protocol for the port
1596 * @port: port to use
1597 * @proto: protocol to use
1598 */
1599static int ioc4_set_proto(struct ioc4_port *port, int proto)
1600{
1601 struct hooks *hooks = port->ip_hooks;
1602
1603 switch (proto) {
1604 case PROTO_RS232:
1605 /* Clear the appropriate GIO pin */
1606 writel(0, (&port->ip_mem->gppr[hooks->rs422_select_pin].raw));
1607 break;
1608
1609 case PROTO_RS422:
1610 /* Set the appropriate GIO pin */
1611 writel(1, (&port->ip_mem->gppr[hooks->rs422_select_pin].raw));
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, 0);
1631 struct uart_state *state;
1632
1633 if (!the_port)
1634 return;
1635 if (!port)
1636 return;
1637
1638 state = the_port->state;
1639 tty = state->port.tty;
1640
1641 if (uart_circ_empty(&state->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 = state->xmit.head;
1648 tail = state->xmit.tail;
1649 start = (char *)&state->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 state->xmit.tail = tail;
1663 start = (char *)&state->xmit.buf[tail];
1664 }
1665 }
1666 if (uart_circ_chars_pending(&state->xmit) < WAKEUP_CHARS)
1667 uart_write_wakeup(the_port);
1668
1669 if (uart_circ_empty(&state->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 ktermios *new_termios, struct ktermios *old_termios)
1685{
1686 struct ioc4_port *port = get_ioc4_port(the_port, 0);
1687 int baud, bits;
1688 unsigned cflag, iflag;
1689 int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
1690 struct uart_state *state = the_port->state;
1691
1692 cflag = new_termios->c_cflag;
1693 iflag = new_termios->c_iflag;
1694
1695 switch (cflag & CSIZE) {
1696 case CS5:
1697 new_data = 5;
1698 bits = 7;
1699 break;
1700 case CS6:
1701 new_data = 6;
1702 bits = 8;
1703 break;
1704 case CS7:
1705 new_data = 7;
1706 bits = 9;
1707 break;
1708 case CS8:
1709 new_data = 8;
1710 bits = 10;
1711 break;
1712 default:
1713 /* cuz we always need a default ... */
1714 new_data = 5;
1715 bits = 7;
1716 break;
1717 }
1718 if (cflag & CSTOPB) {
1719 bits++;
1720 new_stop = 1;
1721 }
1722 if (cflag & PARENB) {
1723 bits++;
1724 new_parity_enable = 1;
1725 if (cflag & PARODD)
1726 new_parity = 1;
1727 }
1728 baud = uart_get_baud_rate(the_port, new_termios, old_termios,
1729 MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
1730 DPRINT_CONFIG(("%s: returned baud %d\n", __func__, baud));
1731
1732 /* default is 9600 */
1733 if (!baud)
1734 baud = 9600;
1735
1736 if (!the_port->fifosize)
1737 the_port->fifosize = IOC4_FIFO_CHARS;
1738 the_port->timeout = ((the_port->fifosize * HZ * bits) / (baud / 10));
1739 the_port->timeout += HZ / 50; /* Add .02 seconds of slop */
1740
1741 the_port->ignore_status_mask = N_ALL_INPUT;
1742
1743 state->port.tty->low_latency = 1;
1744
1745 if (iflag & IGNPAR)
1746 the_port->ignore_status_mask &= ~(N_PARITY_ERROR
1747 | N_FRAMING_ERROR);
1748 if (iflag & IGNBRK) {
1749 the_port->ignore_status_mask &= ~N_BREAK;
1750 if (iflag & IGNPAR)
1751 the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
1752 }
1753 if (!(cflag & CREAD)) {
1754 /* ignore everything */
1755 the_port->ignore_status_mask &= ~N_DATA_READY;
1756 }
1757
1758 if (cflag & CRTSCTS) {
1759 port->ip_sscr |= IOC4_SSCR_HFC_EN;
1760 }
1761 else {
1762 port->ip_sscr &= ~IOC4_SSCR_HFC_EN;
1763 }
1764 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1765
1766 /* Set the configuration and proper notification call */
1767 DPRINT_CONFIG(("%s : port 0x%p cflag 0%o "
1768 "config_port(baud %d data %d stop %d p enable %d parity %d),"
1769 " notification 0x%x\n",
1770 __func__, (void *)port, cflag, baud, new_data, new_stop,
1771 new_parity_enable, new_parity, the_port->ignore_status_mask));
1772
1773 if ((config_port(port, baud, /* baud */
1774 new_data, /* byte size */
1775 new_stop, /* stop bits */
1776 new_parity_enable, /* set parity */
1777 new_parity)) >= 0) { /* parity 1==odd */
1778 set_notification(port, the_port->ignore_status_mask, 1);
1779 }
1780}
1781
1782/**
1783 * ic4_startup_local - Start up the serial port - returns >= 0 if no errors
1784 * @the_port: Port to operate on
1785 */
1786static inline int ic4_startup_local(struct uart_port *the_port)
1787{
1788 struct ioc4_port *port;
1789 struct uart_state *state;
1790
1791 if (!the_port)
1792 return -1;
1793
1794 port = get_ioc4_port(the_port, 0);
1795 if (!port)
1796 return -1;
1797
1798 state = the_port->state;
1799
1800 local_open(port);
1801
1802 /* set the protocol - mapbase has the port type */
1803 ioc4_set_proto(port, the_port->mapbase);
1804
1805 /* set the speed of the serial port */
1806 ioc4_change_speed(the_port, state->port.tty->termios,
1807 (struct ktermios *)0);
1808
1809 return 0;
1810}
1811
1812/*
1813 * ioc4_cb_output_lowat - called when the output low water mark is hit
1814 * @the_port: port to output
1815 */
1816static void ioc4_cb_output_lowat(struct uart_port *the_port)
1817{
1818 unsigned long pflags;
1819
1820 /* ip_lock is set on the call here */
1821 if (the_port) {
1822 spin_lock_irqsave(&the_port->lock, pflags);
1823 transmit_chars(the_port);
1824 spin_unlock_irqrestore(&the_port->lock, pflags);
1825 }
1826}
1827
1828/**
1829 * handle_intr - service any interrupts for the given port - 2nd level
1830 * called via sd_intr
1831 * @arg: handler arg
1832 * @sio_ir: ioc4regs
1833 */
1834static void handle_intr(void *arg, uint32_t sio_ir)
1835{
1836 struct ioc4_port *port = (struct ioc4_port *)arg;
1837 struct hooks *hooks = port->ip_hooks;
1838 unsigned int rx_high_rd_aborted = 0;
1839 unsigned long flags;
1840 struct uart_port *the_port;
1841 int loop_counter;
1842
1843 /* Possible race condition here: The tx_mt interrupt bit may be
1844 * cleared without the intervention of the interrupt handler,
1845 * e.g. by a write. If the top level interrupt handler reads a
1846 * tx_mt, then some other processor does a write, starting up
1847 * output, then we come in here, see the tx_mt and stop DMA, the
1848 * output started by the other processor will hang. Thus we can
1849 * only rely on tx_mt being legitimate if it is read while the
1850 * port lock is held. Therefore this bit must be ignored in the
1851 * passed in interrupt mask which was read by the top level
1852 * interrupt handler since the port lock was not held at the time
1853 * it was read. We can only rely on this bit being accurate if it
1854 * is read while the port lock is held. So we'll clear it for now,
1855 * and reload it later once we have the port lock.
1856 */
1857 sio_ir &= ~(hooks->intr_tx_mt);
1858
1859 spin_lock_irqsave(&port->ip_lock, flags);
1860
1861 loop_counter = MAXITER; /* to avoid hangs */
1862
1863 do {
1864 uint32_t shadow;
1865
1866 if ( loop_counter-- <= 0 ) {
1867 printk(KERN_WARNING "IOC4 serial: "
1868 "possible hang condition/"
1869 "port stuck on interrupt.\n");
1870 break;
1871 }
1872
1873 /* Handle a DCD change */
1874 if (sio_ir & hooks->intr_delta_dcd) {
1875 /* ACK the interrupt */
1876 writel(hooks->intr_delta_dcd,
1877 &port->ip_mem->sio_ir.raw);
1878
1879 shadow = readl(&port->ip_serial_regs->shadow);
1880
1881 if ((port->ip_notify & N_DDCD)
1882 && (shadow & IOC4_SHADOW_DCD)
1883 && (port->ip_port)) {
1884 the_port = port->ip_port;
1885 the_port->icount.dcd = 1;
1886 wake_up_interruptible
1887 (&the_port->state->port.delta_msr_wait);
1888 } else if ((port->ip_notify & N_DDCD)
1889 && !(shadow & IOC4_SHADOW_DCD)) {
1890 /* Flag delta DCD/no DCD */
1891 port->ip_flags |= DCD_ON;
1892 }
1893 }
1894
1895 /* Handle a CTS change */
1896 if (sio_ir & hooks->intr_delta_cts) {
1897 /* ACK the interrupt */
1898 writel(hooks->intr_delta_cts,
1899 &port->ip_mem->sio_ir.raw);
1900
1901 shadow = readl(&port->ip_serial_regs->shadow);
1902
1903 if ((port->ip_notify & N_DCTS)
1904 && (port->ip_port)) {
1905 the_port = port->ip_port;
1906 the_port->icount.cts =
1907 (shadow & IOC4_SHADOW_CTS) ? 1 : 0;
1908 wake_up_interruptible
1909 (&the_port->state->port.delta_msr_wait);
1910 }
1911 }
1912
1913 /* rx timeout interrupt. Must be some data available. Put this
1914 * before the check for rx_high since servicing this condition
1915 * may cause that condition to clear.
1916 */
1917 if (sio_ir & hooks->intr_rx_timer) {
1918 /* ACK the interrupt */
1919 writel(hooks->intr_rx_timer,
1920 &port->ip_mem->sio_ir.raw);
1921
1922 if ((port->ip_notify & N_DATA_READY)
1923 && (port->ip_port)) {
1924 /* ip_lock is set on call here */
1925 receive_chars(port->ip_port);
1926 }
1927 }
1928
1929 /* rx high interrupt. Must be after rx_timer. */
1930 else if (sio_ir & hooks->intr_rx_high) {
1931 /* Data available, notify upper layer */
1932 if ((port->ip_notify & N_DATA_READY)
1933 && port->ip_port) {
1934 /* ip_lock is set on call here */
1935 receive_chars(port->ip_port);
1936 }
1937
1938 /* We can't ACK this interrupt. If receive_chars didn't
1939 * cause the condition to clear, we'll have to disable
1940 * the interrupt until the data is drained.
1941 * If the read was aborted, don't disable the interrupt
1942 * as this may cause us to hang indefinitely. An
1943 * aborted read generally means that this interrupt
1944 * hasn't been delivered to the cpu yet anyway, even
1945 * though we see it as asserted when we read the sio_ir.
1946 */
1947 if ((sio_ir = PENDING(port)) & hooks->intr_rx_high) {
1948 if ((port->ip_flags & READ_ABORTED) == 0) {
1949 port->ip_ienb &= ~hooks->intr_rx_high;
1950 port->ip_flags |= INPUT_HIGH;
1951 } else {
1952 rx_high_rd_aborted++;
1953 }
1954 }
1955 }
1956
1957 /* We got a low water interrupt: notify upper layer to
1958 * send more data. Must come before tx_mt since servicing
1959 * this condition may cause that condition to clear.
1960 */
1961 if (sio_ir & hooks->intr_tx_explicit) {
1962 port->ip_flags &= ~LOWAT_WRITTEN;
1963
1964 /* ACK the interrupt */
1965 writel(hooks->intr_tx_explicit,
1966 &port->ip_mem->sio_ir.raw);
1967
1968 if (port->ip_notify & N_OUTPUT_LOWAT)
1969 ioc4_cb_output_lowat(port->ip_port);
1970 }
1971
1972 /* Handle tx_mt. Must come after tx_explicit. */
1973 else if (sio_ir & hooks->intr_tx_mt) {
1974 /* If we are expecting a lowat notification
1975 * and we get to this point it probably means that for
1976 * some reason the tx_explicit didn't work as expected
1977 * (that can legitimately happen if the output buffer is
1978 * filled up in just the right way).
1979 * So send the notification now.
1980 */
1981 if (port->ip_notify & N_OUTPUT_LOWAT) {
1982 ioc4_cb_output_lowat(port->ip_port);
1983
1984 /* We need to reload the sio_ir since the lowat
1985 * call may have caused another write to occur,
1986 * clearing the tx_mt condition.
1987 */
1988 sio_ir = PENDING(port);
1989 }
1990
1991 /* If the tx_mt condition still persists even after the
1992 * lowat call, we've got some work to do.
1993 */
1994 if (sio_ir & hooks->intr_tx_mt) {
1995
1996 /* If we are not currently expecting DMA input,
1997 * and the transmitter has just gone idle,
1998 * there is no longer any reason for DMA, so
1999 * disable it.
2000 */
2001 if (!(port->ip_notify
2002 & (N_DATA_READY | N_DDCD))) {
2003 BUG_ON(!(port->ip_sscr
2004 & IOC4_SSCR_DMA_EN));
2005 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
2006 writel(port->ip_sscr,
2007 &port->ip_serial_regs->sscr);
2008 }
2009
2010 /* Prevent infinite tx_mt interrupt */
2011 port->ip_ienb &= ~hooks->intr_tx_mt;
2012 }
2013 }
2014 sio_ir = PENDING(port);
2015
2016 /* if the read was aborted and only hooks->intr_rx_high,
2017 * clear hooks->intr_rx_high, so we do not loop forever.
2018 */
2019
2020 if (rx_high_rd_aborted && (sio_ir == hooks->intr_rx_high)) {
2021 sio_ir &= ~hooks->intr_rx_high;
2022 }
2023 } while (sio_ir & hooks->intr_all);
2024
2025 spin_unlock_irqrestore(&port->ip_lock, flags);
2026
2027 /* Re-enable interrupts before returning from interrupt handler.
2028 * Getting interrupted here is okay. It'll just v() our semaphore, and
2029 * we'll come through the loop again.
2030 */
2031
2032 write_ireg(port->ip_ioc4_soft, port->ip_ienb, IOC4_W_IES,
2033 IOC4_SIO_INTR_TYPE);
2034}
2035
2036/*
2037 * ioc4_cb_post_ncs - called for some basic errors
2038 * @port: port to use
2039 * @ncs: event
2040 */
2041static void ioc4_cb_post_ncs(struct uart_port *the_port, int ncs)
2042{
2043 struct uart_icount *icount;
2044
2045 icount = &the_port->icount;
2046
2047 if (ncs & NCS_BREAK)
2048 icount->brk++;
2049 if (ncs & NCS_FRAMING)
2050 icount->frame++;
2051 if (ncs & NCS_OVERRUN)
2052 icount->overrun++;
2053 if (ncs & NCS_PARITY)
2054 icount->parity++;
2055}
2056
2057/**
2058 * do_read - Read in bytes from the port. Return the number of bytes
2059 * actually read.
2060 * @the_port: port to use
2061 * @buf: place to put the stuff we read
2062 * @len: how big 'buf' is
2063 */
2064
2065static inline int do_read(struct uart_port *the_port, unsigned char *buf,
2066 int len)
2067{
2068 int prod_ptr, cons_ptr, total;
2069 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2070 struct ring *inring;
2071 struct ring_entry *entry;
2072 struct hooks *hooks = port->ip_hooks;
2073 int byte_num;
2074 char *sc;
2075 int loop_counter;
2076
2077 BUG_ON(!(len >= 0));
2078 BUG_ON(!port);
2079
2080 /* There is a nasty timing issue in the IOC4. When the rx_timer
2081 * expires or the rx_high condition arises, we take an interrupt.
2082 * At some point while servicing the interrupt, we read bytes from
2083 * the ring buffer and re-arm the rx_timer. However the rx_timer is
2084 * not started until the first byte is received *after* it is armed,
2085 * and any bytes pending in the rx construction buffers are not drained
2086 * to memory until either there are 4 bytes available or the rx_timer
2087 * expires. This leads to a potential situation where data is left
2088 * in the construction buffers forever - 1 to 3 bytes were received
2089 * after the interrupt was generated but before the rx_timer was
2090 * re-armed. At that point as long as no subsequent bytes are received
2091 * the timer will never be started and the bytes will remain in the
2092 * construction buffer forever. The solution is to execute a DRAIN
2093 * command after rearming the timer. This way any bytes received before
2094 * the DRAIN will be drained to memory, and any bytes received after
2095 * the DRAIN will start the TIMER and be drained when it expires.
2096 * Luckily, this only needs to be done when the DMA buffer is empty
2097 * since there is no requirement that this function return all
2098 * available data as long as it returns some.
2099 */
2100 /* Re-arm the timer */
2101 writel(port->ip_rx_cons | IOC4_SRCIR_ARM, &port->ip_serial_regs->srcir);
2102
2103 prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
2104 cons_ptr = port->ip_rx_cons;
2105
2106 if (prod_ptr == cons_ptr) {
2107 int reset_dma = 0;
2108
2109 /* Input buffer appears empty, do a flush. */
2110
2111 /* DMA must be enabled for this to work. */
2112 if (!(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
2113 port->ip_sscr |= IOC4_SSCR_DMA_EN;
2114 reset_dma = 1;
2115 }
2116
2117 /* Potential race condition: we must reload the srpir after
2118 * issuing the drain command, otherwise we could think the rx
2119 * buffer is empty, then take a very long interrupt, and when
2120 * we come back it's full and we wait forever for the drain to
2121 * complete.
2122 */
2123 writel(port->ip_sscr | IOC4_SSCR_RX_DRAIN,
2124 &port->ip_serial_regs->sscr);
2125 prod_ptr = readl(&port->ip_serial_regs->srpir)
2126 & PROD_CONS_MASK;
2127
2128 /* We must not wait for the DRAIN to complete unless there are
2129 * at least 8 bytes (2 ring entries) available to receive the
2130 * data otherwise the DRAIN will never complete and we'll
2131 * deadlock here.
2132 * In fact, to make things easier, I'll just ignore the flush if
2133 * there is any data at all now available.
2134 */
2135 if (prod_ptr == cons_ptr) {
2136 loop_counter = 0;
2137 while (readl(&port->ip_serial_regs->sscr) &
2138 IOC4_SSCR_RX_DRAIN) {
2139 loop_counter++;
2140 if (loop_counter > MAXITER)
2141 return -1;
2142 }
2143
2144 /* SIGH. We have to reload the prod_ptr *again* since
2145 * the drain may have caused it to change
2146 */
2147 prod_ptr = readl(&port->ip_serial_regs->srpir)
2148 & PROD_CONS_MASK;
2149 }
2150 if (reset_dma) {
2151 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
2152 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
2153 }
2154 }
2155 inring = port->ip_inring;
2156 port->ip_flags &= ~READ_ABORTED;
2157
2158 total = 0;
2159 loop_counter = 0xfffff; /* to avoid hangs */
2160
2161 /* Grab bytes from the hardware */
2162 while ((prod_ptr != cons_ptr) && (len > 0)) {
2163 entry = (struct ring_entry *)((caddr_t)inring + cons_ptr);
2164
2165 if ( loop_counter-- <= 0 ) {
2166 printk(KERN_WARNING "IOC4 serial: "
2167 "possible hang condition/"
2168 "port stuck on read.\n");
2169 break;
2170 }
2171
2172 /* According to the producer pointer, this ring entry
2173 * must contain some data. But if the PIO happened faster
2174 * than the DMA, the data may not be available yet, so let's
2175 * wait until it arrives.
2176 */
2177 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
2178 /* Indicate the read is aborted so we don't disable
2179 * the interrupt thinking that the consumer is
2180 * congested.
2181 */
2182 port->ip_flags |= READ_ABORTED;
2183 len = 0;
2184 break;
2185 }
2186
2187 /* Load the bytes/status out of the ring entry */
2188 for (byte_num = 0; byte_num < 4 && len > 0; byte_num++) {
2189 sc = &(entry->ring_sc[byte_num]);
2190
2191 /* Check for change in modem state or overrun */
2192 if ((*sc & IOC4_RXSB_MODEM_VALID)
2193 && (port->ip_notify & N_DDCD)) {
2194 /* Notify upper layer if DCD dropped */
2195
2196 if ((port->ip_flags & DCD_ON)
2197 && !(*sc & IOC4_RXSB_DCD)) {
2198
2199 /* If we have already copied some data,
2200 * return it. We'll pick up the carrier
2201 * drop on the next pass. That way we
2202 * don't throw away the data that has
2203 * already been copied back to
2204 * the caller's buffer.
2205 */
2206 if (total > 0) {
2207 len = 0;
2208 break;
2209 }
2210 port->ip_flags &= ~DCD_ON;
2211
2212 /* Turn off this notification so the
2213 * carrier drop protocol won't see it
2214 * again when it does a read.
2215 */
2216 *sc &= ~IOC4_RXSB_MODEM_VALID;
2217
2218 /* To keep things consistent, we need
2219 * to update the consumer pointer so
2220 * the next reader won't come in and
2221 * try to read the same ring entries
2222 * again. This must be done here before
2223 * the dcd change.
2224 */
2225
2226 if ((entry->ring_allsc & RING_ANY_VALID)
2227 == 0) {
2228 cons_ptr += (int)sizeof
2229 (struct ring_entry);
2230 cons_ptr &= PROD_CONS_MASK;
2231 }
2232 writel(cons_ptr,
2233 &port->ip_serial_regs->srcir);
2234 port->ip_rx_cons = cons_ptr;
2235
2236 /* Notify upper layer of carrier drop */
2237 if ((port->ip_notify & N_DDCD)
2238 && port->ip_port) {
2239 the_port->icount.dcd = 0;
2240 wake_up_interruptible
2241 (&the_port->state->
2242 port.delta_msr_wait);
2243 }
2244
2245 /* If we had any data to return, we
2246 * would have returned it above.
2247 */
2248 return 0;
2249 }
2250 }
2251 if (*sc & IOC4_RXSB_MODEM_VALID) {
2252 /* Notify that an input overrun occurred */
2253 if ((*sc & IOC4_RXSB_OVERRUN)
2254 && (port->ip_notify & N_OVERRUN_ERROR)) {
2255 ioc4_cb_post_ncs(the_port, NCS_OVERRUN);
2256 }
2257 /* Don't look at this byte again */
2258 *sc &= ~IOC4_RXSB_MODEM_VALID;
2259 }
2260
2261 /* Check for valid data or RX errors */
2262 if ((*sc & IOC4_RXSB_DATA_VALID) &&
2263 ((*sc & (IOC4_RXSB_PAR_ERR
2264 | IOC4_RXSB_FRAME_ERR
2265 | IOC4_RXSB_BREAK))
2266 && (port->ip_notify & (N_PARITY_ERROR
2267 | N_FRAMING_ERROR
2268 | N_BREAK)))) {
2269 /* There is an error condition on the next byte.
2270 * If we have already transferred some bytes,
2271 * we'll stop here. Otherwise if this is the
2272 * first byte to be read, we'll just transfer
2273 * it alone after notifying the
2274 * upper layer of its status.
2275 */
2276 if (total > 0) {
2277 len = 0;
2278 break;
2279 } else {
2280 if ((*sc & IOC4_RXSB_PAR_ERR) &&
2281 (port->ip_notify & N_PARITY_ERROR)) {
2282 ioc4_cb_post_ncs(the_port,
2283 NCS_PARITY);
2284 }
2285 if ((*sc & IOC4_RXSB_FRAME_ERR) &&
2286 (port->ip_notify & N_FRAMING_ERROR)){
2287 ioc4_cb_post_ncs(the_port,
2288 NCS_FRAMING);
2289 }
2290 if ((*sc & IOC4_RXSB_BREAK)
2291 && (port->ip_notify & N_BREAK)) {
2292 ioc4_cb_post_ncs
2293 (the_port,
2294 NCS_BREAK);
2295 }
2296 len = 1;
2297 }
2298 }
2299 if (*sc & IOC4_RXSB_DATA_VALID) {
2300 *sc &= ~IOC4_RXSB_DATA_VALID;
2301 *buf = entry->ring_data[byte_num];
2302 buf++;
2303 len--;
2304 total++;
2305 }
2306 }
2307
2308 /* If we used up this entry entirely, go on to the next one,
2309 * otherwise we must have run out of buffer space, so
2310 * leave the consumer pointer here for the next read in case
2311 * there are still unread bytes in this entry.
2312 */
2313 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
2314 cons_ptr += (int)sizeof(struct ring_entry);
2315 cons_ptr &= PROD_CONS_MASK;
2316 }
2317 }
2318
2319 /* Update consumer pointer and re-arm rx timer interrupt */
2320 writel(cons_ptr, &port->ip_serial_regs->srcir);
2321 port->ip_rx_cons = cons_ptr;
2322
2323 /* If we have now dipped below the rx high water mark and we have
2324 * rx_high interrupt turned off, we can now turn it back on again.
2325 */
2326 if ((port->ip_flags & INPUT_HIGH) && (((prod_ptr - cons_ptr)
2327 & PROD_CONS_MASK) < ((port->ip_sscr &
2328 IOC4_SSCR_RX_THRESHOLD)
2329 << IOC4_PROD_CONS_PTR_OFF))) {
2330 port->ip_flags &= ~INPUT_HIGH;
2331 enable_intrs(port, hooks->intr_rx_high);
2332 }
2333 return total;
2334}
2335
2336/**
2337 * receive_chars - upper level read. Called with ip_lock.
2338 * @the_port: port to read from
2339 */
2340static void receive_chars(struct uart_port *the_port)
2341{
2342 struct tty_struct *tty;
2343 unsigned char ch[IOC4_MAX_CHARS];
2344 int read_count, request_count = IOC4_MAX_CHARS;
2345 struct uart_icount *icount;
2346 struct uart_state *state = the_port->state;
2347 unsigned long pflags;
2348
2349 /* Make sure all the pointers are "good" ones */
2350 if (!state)
2351 return;
2352 if (!state->port.tty)
2353 return;
2354
2355 spin_lock_irqsave(&the_port->lock, pflags);
2356 tty = state->port.tty;
2357
2358 request_count = tty_buffer_request_room(tty, IOC4_MAX_CHARS);
2359
2360 if (request_count > 0) {
2361 icount = &the_port->icount;
2362 read_count = do_read(the_port, ch, request_count);
2363 if (read_count > 0) {
2364 tty_insert_flip_string(tty, ch, read_count);
2365 icount->rx += read_count;
2366 }
2367 }
2368
2369 spin_unlock_irqrestore(&the_port->lock, pflags);
2370
2371 tty_flip_buffer_push(tty);
2372}
2373
2374/**
2375 * ic4_type - What type of console are we?
2376 * @port: Port to operate with (we ignore since we only have one port)
2377 *
2378 */
2379static const char *ic4_type(struct uart_port *the_port)
2380{
2381 if (the_port->mapbase == PROTO_RS232)
2382 return "SGI IOC4 Serial [rs232]";
2383 else
2384 return "SGI IOC4 Serial [rs422]";
2385}
2386
2387/**
2388 * ic4_tx_empty - Is the transmitter empty?
2389 * @port: Port to operate on
2390 *
2391 */
2392static unsigned int ic4_tx_empty(struct uart_port *the_port)
2393{
2394 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2395 unsigned int ret = 0;
2396
2397 if (port_is_active(port, the_port)) {
2398 if (readl(&port->ip_serial_regs->shadow) & IOC4_SHADOW_TEMT)
2399 ret = TIOCSER_TEMT;
2400 }
2401 return ret;
2402}
2403
2404/**
2405 * ic4_stop_tx - stop the transmitter
2406 * @port: Port to operate on
2407 *
2408 */
2409static void ic4_stop_tx(struct uart_port *the_port)
2410{
2411 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2412
2413 if (port_is_active(port, the_port))
2414 set_notification(port, N_OUTPUT_LOWAT, 0);
2415}
2416
2417/**
2418 * null_void_function -
2419 * @port: Port to operate on
2420 *
2421 */
2422static void null_void_function(struct uart_port *the_port)
2423{
2424}
2425
2426/**
2427 * ic4_shutdown - shut down the port - free irq and disable
2428 * @port: Port to shut down
2429 *
2430 */
2431static void ic4_shutdown(struct uart_port *the_port)
2432{
2433 unsigned long port_flags;
2434 struct ioc4_port *port;
2435 struct uart_state *state;
2436
2437 port = get_ioc4_port(the_port, 0);
2438 if (!port)
2439 return;
2440
2441 state = the_port->state;
2442 port->ip_port = NULL;
2443
2444 wake_up_interruptible(&state->port.delta_msr_wait);
2445
2446 if (state->port.tty)
2447 set_bit(TTY_IO_ERROR, &state->port.tty->flags);
2448
2449 spin_lock_irqsave(&the_port->lock, port_flags);
2450 set_notification(port, N_ALL, 0);
2451 port->ip_flags = PORT_INACTIVE;
2452 spin_unlock_irqrestore(&the_port->lock, port_flags);
2453}
2454
2455/**
2456 * ic4_set_mctrl - set control lines (dtr, rts, etc)
2457 * @port: Port to operate on
2458 * @mctrl: Lines to set/unset
2459 *
2460 */
2461static void ic4_set_mctrl(struct uart_port *the_port, unsigned int mctrl)
2462{
2463 unsigned char mcr = 0;
2464 struct ioc4_port *port;
2465
2466 port = get_ioc4_port(the_port, 0);
2467 if (!port_is_active(port, the_port))
2468 return;
2469
2470 if (mctrl & TIOCM_RTS)
2471 mcr |= UART_MCR_RTS;
2472 if (mctrl & TIOCM_DTR)
2473 mcr |= UART_MCR_DTR;
2474 if (mctrl & TIOCM_OUT1)
2475 mcr |= UART_MCR_OUT1;
2476 if (mctrl & TIOCM_OUT2)
2477 mcr |= UART_MCR_OUT2;
2478 if (mctrl & TIOCM_LOOP)
2479 mcr |= UART_MCR_LOOP;
2480
2481 set_mcr(the_port, mcr, IOC4_SHADOW_DTR);
2482}
2483
2484/**
2485 * ic4_get_mctrl - get control line info
2486 * @port: port to operate on
2487 *
2488 */
2489static unsigned int ic4_get_mctrl(struct uart_port *the_port)
2490{
2491 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2492 uint32_t shadow;
2493 unsigned int ret = 0;
2494
2495 if (!port_is_active(port, the_port))
2496 return 0;
2497
2498 shadow = readl(&port->ip_serial_regs->shadow);
2499 if (shadow & IOC4_SHADOW_DCD)
2500 ret |= TIOCM_CAR;
2501 if (shadow & IOC4_SHADOW_DR)
2502 ret |= TIOCM_DSR;
2503 if (shadow & IOC4_SHADOW_CTS)
2504 ret |= TIOCM_CTS;
2505 return ret;
2506}
2507
2508/**
2509 * ic4_start_tx - Start transmitter, flush any output
2510 * @port: Port to operate on
2511 *
2512 */
2513static void ic4_start_tx(struct uart_port *the_port)
2514{
2515 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2516
2517 if (port_is_active(port, the_port)) {
2518 set_notification(port, N_OUTPUT_LOWAT, 1);
2519 enable_intrs(port, port->ip_hooks->intr_tx_mt);
2520 }
2521}
2522
2523/**
2524 * ic4_break_ctl - handle breaks
2525 * @port: Port to operate on
2526 * @break_state: Break state
2527 *
2528 */
2529static void ic4_break_ctl(struct uart_port *the_port, int break_state)
2530{
2531}
2532
2533/**
2534 * ic4_startup - Start up the serial port
2535 * @port: Port to operate on
2536 *
2537 */
2538static int ic4_startup(struct uart_port *the_port)
2539{
2540 int retval;
2541 struct ioc4_port *port;
2542 struct ioc4_control *control;
2543 struct uart_state *state;
2544 unsigned long port_flags;
2545
2546 if (!the_port)
2547 return -ENODEV;
2548 port = get_ioc4_port(the_port, 1);
2549 if (!port)
2550 return -ENODEV;
2551 state = the_port->state;
2552
2553 control = port->ip_control;
2554 if (!control) {
2555 port->ip_port = NULL;
2556 return -ENODEV;
2557 }
2558
2559 /* Start up the serial port */
2560 spin_lock_irqsave(&the_port->lock, port_flags);
2561 retval = ic4_startup_local(the_port);
2562 spin_unlock_irqrestore(&the_port->lock, port_flags);
2563 return retval;
2564}
2565
2566/**
2567 * ic4_set_termios - set termios stuff
2568 * @port: port to operate on
2569 * @termios: New settings
2570 * @termios: Old
2571 *
2572 */
2573static void
2574ic4_set_termios(struct uart_port *the_port,
2575 struct ktermios *termios, struct ktermios *old_termios)
2576{
2577 unsigned long port_flags;
2578
2579 spin_lock_irqsave(&the_port->lock, port_flags);
2580 ioc4_change_speed(the_port, termios, old_termios);
2581 spin_unlock_irqrestore(&the_port->lock, port_flags);
2582}
2583
2584/**
2585 * ic4_request_port - allocate resources for port - no op....
2586 * @port: port to operate on
2587 *
2588 */
2589static int ic4_request_port(struct uart_port *port)
2590{
2591 return 0;
2592}
2593
2594/* Associate the uart functions above - given to serial core */
2595
2596static struct uart_ops ioc4_ops = {
2597 .tx_empty = ic4_tx_empty,
2598 .set_mctrl = ic4_set_mctrl,
2599 .get_mctrl = ic4_get_mctrl,
2600 .stop_tx = ic4_stop_tx,
2601 .start_tx = ic4_start_tx,
2602 .stop_rx = null_void_function,
2603 .enable_ms = null_void_function,
2604 .break_ctl = ic4_break_ctl,
2605 .startup = ic4_startup,
2606 .shutdown = ic4_shutdown,
2607 .set_termios = ic4_set_termios,
2608 .type = ic4_type,
2609 .release_port = null_void_function,
2610 .request_port = ic4_request_port,
2611};
2612
2613/*
2614 * Boot-time initialization code
2615 */
2616
2617static struct uart_driver ioc4_uart_rs232 = {
2618 .owner = THIS_MODULE,
2619 .driver_name = "ioc4_serial_rs232",
2620 .dev_name = DEVICE_NAME_RS232,
2621 .major = DEVICE_MAJOR,
2622 .minor = DEVICE_MINOR_RS232,
2623 .nr = IOC4_NUM_CARDS * IOC4_NUM_SERIAL_PORTS,
2624};
2625
2626static struct uart_driver ioc4_uart_rs422 = {
2627 .owner = THIS_MODULE,
2628 .driver_name = "ioc4_serial_rs422",
2629 .dev_name = DEVICE_NAME_RS422,
2630 .major = DEVICE_MAJOR,
2631 .minor = DEVICE_MINOR_RS422,
2632 .nr = IOC4_NUM_CARDS * IOC4_NUM_SERIAL_PORTS,
2633};
2634
2635
2636/**
2637 * ioc4_serial_remove_one - detach function
2638 *
2639 * @idd: IOC4 master module data for this IOC4
2640 */
2641
2642static int ioc4_serial_remove_one(struct ioc4_driver_data *idd)
2643{
2644 int port_num, port_type;
2645 struct ioc4_control *control;
2646 struct uart_port *the_port;
2647 struct ioc4_port *port;
2648 struct ioc4_soft *soft;
2649
2650 /* If serial driver did not attach, don't try to detach */
2651 control = idd->idd_serial_data;
2652 if (!control)
2653 return 0;
2654
2655 for (port_num = 0; port_num < IOC4_NUM_SERIAL_PORTS; port_num++) {
2656 for (port_type = UART_PORT_MIN;
2657 port_type < UART_PORT_COUNT;
2658 port_type++) {
2659 the_port = &control->ic_port[port_num].icp_uart_port
2660 [port_type];
2661 if (the_port) {
2662 switch (port_type) {
2663 case UART_PORT_RS422:
2664 uart_remove_one_port(&ioc4_uart_rs422,
2665 the_port);
2666 break;
2667 default:
2668 case UART_PORT_RS232:
2669 uart_remove_one_port(&ioc4_uart_rs232,
2670 the_port);
2671 break;
2672 }
2673 }
2674 }
2675 port = control->ic_port[port_num].icp_port;
2676 /* we allocate in pairs */
2677 if (!(port_num & 1) && port) {
2678 pci_free_consistent(port->ip_pdev,
2679 TOTAL_RING_BUF_SIZE,
2680 port->ip_cpu_ringbuf,
2681 port->ip_dma_ringbuf);
2682 kfree(port);
2683 }
2684 }
2685 soft = control->ic_soft;
2686 if (soft) {
2687 free_irq(control->ic_irq, soft);
2688 if (soft->is_ioc4_serial_addr) {
2689 iounmap(soft->is_ioc4_serial_addr);
2690 release_mem_region((unsigned long)
2691 soft->is_ioc4_serial_addr,
2692 sizeof(struct ioc4_serial));
2693 }
2694 kfree(soft);
2695 }
2696 kfree(control);
2697 idd->idd_serial_data = NULL;
2698
2699 return 0;
2700}
2701
2702
2703/**
2704 * ioc4_serial_core_attach_rs232 - register with serial core
2705 * This is done during pci probing
2706 * @pdev: handle for this card
2707 */
2708static inline int
2709ioc4_serial_core_attach(struct pci_dev *pdev, int port_type)
2710{
2711 struct ioc4_port *port;
2712 struct uart_port *the_port;
2713 struct ioc4_driver_data *idd = pci_get_drvdata(pdev);
2714 struct ioc4_control *control = idd->idd_serial_data;
2715 int port_num;
2716 int port_type_idx;
2717 struct uart_driver *u_driver;
2718
2719
2720 DPRINT_CONFIG(("%s: attach pdev 0x%p - control 0x%p\n",
2721 __func__, pdev, (void *)control));
2722
2723 if (!control)
2724 return -ENODEV;
2725
2726 port_type_idx = (port_type == PROTO_RS232) ? UART_PORT_RS232
2727 : UART_PORT_RS422;
2728
2729 u_driver = (port_type == PROTO_RS232) ? &ioc4_uart_rs232
2730 : &ioc4_uart_rs422;
2731
2732 /* once around for each port on this card */
2733 for (port_num = 0; port_num < IOC4_NUM_SERIAL_PORTS; port_num++) {
2734 the_port = &control->ic_port[port_num].icp_uart_port
2735 [port_type_idx];
2736 port = control->ic_port[port_num].icp_port;
2737 port->ip_all_ports[port_type_idx] = the_port;
2738
2739 DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p : type %s\n",
2740 __func__, (void *)the_port,
2741 (void *)port,
2742 port_type == PROTO_RS232 ? "rs232" : "rs422"));
2743
2744 /* membase, iobase and mapbase just need to be non-0 */
2745 the_port->membase = (unsigned char __iomem *)1;
2746 the_port->iobase = (pdev->bus->number << 16) | port_num;
2747 the_port->line = (Num_of_ioc4_cards << 2) | port_num;
2748 the_port->mapbase = port_type;
2749 the_port->type = PORT_16550A;
2750 the_port->fifosize = IOC4_FIFO_CHARS;
2751 the_port->ops = &ioc4_ops;
2752 the_port->irq = control->ic_irq;
2753 the_port->dev = &pdev->dev;
2754 spin_lock_init(&the_port->lock);
2755 if (uart_add_one_port(u_driver, the_port) < 0) {
2756 printk(KERN_WARNING
2757 "%s: unable to add port %d bus %d\n",
2758 __func__, the_port->line, pdev->bus->number);
2759 } else {
2760 DPRINT_CONFIG(
2761 ("IOC4 serial port %d irq = %d, bus %d\n",
2762 the_port->line, the_port->irq, pdev->bus->number));
2763 }
2764 }
2765 return 0;
2766}
2767
2768/**
2769 * ioc4_serial_attach_one - register attach function
2770 * called per card found from IOC4 master module.
2771 * @idd: Master module data for this IOC4
2772 */
2773int
2774ioc4_serial_attach_one(struct ioc4_driver_data *idd)
2775{
2776 unsigned long tmp_addr1;
2777 struct ioc4_serial __iomem *serial;
2778 struct ioc4_soft *soft;
2779 struct ioc4_control *control;
2780 int ret = 0;
2781
2782
2783 DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __func__, idd->idd_pdev,
2784 idd->idd_pci_id));
2785
2786 /* PCI-RT does not bring out serial connections.
2787 * Do not attach to this particular IOC4.
2788 */
2789 if (idd->idd_variant == IOC4_VARIANT_PCI_RT)
2790 return 0;
2791
2792 /* request serial registers */
2793 tmp_addr1 = idd->idd_bar0 + IOC4_SERIAL_OFFSET;
2794
2795 if (!request_mem_region(tmp_addr1, sizeof(struct ioc4_serial),
2796 "sioc4_uart")) {
2797 printk(KERN_WARNING
2798 "ioc4 (%p): unable to get request region for "
2799 "uart space\n", (void *)idd->idd_pdev);
2800 ret = -ENODEV;
2801 goto out1;
2802 }
2803 serial = ioremap(tmp_addr1, sizeof(struct ioc4_serial));
2804 if (!serial) {
2805 printk(KERN_WARNING
2806 "ioc4 (%p) : unable to remap ioc4 serial register\n",
2807 (void *)idd->idd_pdev);
2808 ret = -ENODEV;
2809 goto out2;
2810 }
2811 DPRINT_CONFIG(("%s : mem 0x%p, serial 0x%p\n",
2812 __func__, (void *)idd->idd_misc_regs,
2813 (void *)serial));
2814
2815 /* Get memory for the new card */
2816 control = kzalloc(sizeof(struct ioc4_control), GFP_KERNEL);
2817
2818 if (!control) {
2819 printk(KERN_WARNING "ioc4_attach_one"
2820 ": unable to get memory for the IOC4\n");
2821 ret = -ENOMEM;
2822 goto out2;
2823 }
2824 idd->idd_serial_data = control;
2825
2826 /* Allocate the soft structure */
2827 soft = kzalloc(sizeof(struct ioc4_soft), GFP_KERNEL);
2828 if (!soft) {
2829 printk(KERN_WARNING
2830 "ioc4 (%p): unable to get memory for the soft struct\n",
2831 (void *)idd->idd_pdev);
2832 ret = -ENOMEM;
2833 goto out3;
2834 }
2835
2836 spin_lock_init(&soft->is_ir_lock);
2837 soft->is_ioc4_misc_addr = idd->idd_misc_regs;
2838 soft->is_ioc4_serial_addr = serial;
2839
2840 /* Init the IOC4 */
2841 writel(0xf << IOC4_SIO_CR_CMD_PULSE_SHIFT,
2842 &idd->idd_misc_regs->sio_cr.raw);
2843
2844 /* Enable serial port mode select generic PIO pins as outputs */
2845 writel(IOC4_GPCR_UART0_MODESEL | IOC4_GPCR_UART1_MODESEL
2846 | IOC4_GPCR_UART2_MODESEL | IOC4_GPCR_UART3_MODESEL,
2847 &idd->idd_misc_regs->gpcr_s.raw);
2848
2849 /* Clear and disable all serial interrupts */
2850 write_ireg(soft, ~0, IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
2851 writel(~0, &idd->idd_misc_regs->sio_ir.raw);
2852 write_ireg(soft, IOC4_OTHER_IR_SER_MEMERR, IOC4_W_IEC,
2853 IOC4_OTHER_INTR_TYPE);
2854 writel(IOC4_OTHER_IR_SER_MEMERR, &idd->idd_misc_regs->other_ir.raw);
2855 control->ic_soft = soft;
2856
2857 /* Hook up interrupt handler */
2858 if (!request_irq(idd->idd_pdev->irq, ioc4_intr, IRQF_SHARED,
2859 "sgi-ioc4serial", soft)) {
2860 control->ic_irq = idd->idd_pdev->irq;
2861 } else {
2862 printk(KERN_WARNING
2863 "%s : request_irq fails for IRQ 0x%x\n ",
2864 __func__, idd->idd_pdev->irq);
2865 }
2866 ret = ioc4_attach_local(idd);
2867 if (ret)
2868 goto out4;
2869
2870 /* register port with the serial core - 1 rs232, 1 rs422 */
2871
2872 if ((ret = ioc4_serial_core_attach(idd->idd_pdev, PROTO_RS232)))
2873 goto out4;
2874
2875 if ((ret = ioc4_serial_core_attach(idd->idd_pdev, PROTO_RS422)))
2876 goto out5;
2877
2878 Num_of_ioc4_cards++;
2879
2880 return ret;
2881
2882 /* error exits that give back resources */
2883out5:
2884 ioc4_serial_remove_one(idd);
2885out4:
2886 kfree(soft);
2887out3:
2888 kfree(control);
2889out2:
2890 if (serial)
2891 iounmap(serial);
2892 release_mem_region(tmp_addr1, sizeof(struct ioc4_serial));
2893out1:
2894
2895 return ret;
2896}
2897
2898
2899static struct ioc4_submodule ioc4_serial_submodule = {
2900 .is_name = "IOC4_serial",
2901 .is_owner = THIS_MODULE,
2902 .is_probe = ioc4_serial_attach_one,
2903 .is_remove = ioc4_serial_remove_one,
2904};
2905
2906/**
2907 * ioc4_serial_init - module init
2908 */
2909static int __init ioc4_serial_init(void)
2910{
2911 int ret;
2912
2913 /* register with serial core */
2914 if ((ret = uart_register_driver(&ioc4_uart_rs232)) < 0) {
2915 printk(KERN_WARNING
2916 "%s: Couldn't register rs232 IOC4 serial driver\n",
2917 __func__);
2918 goto out;
2919 }
2920 if ((ret = uart_register_driver(&ioc4_uart_rs422)) < 0) {
2921 printk(KERN_WARNING
2922 "%s: Couldn't register rs422 IOC4 serial driver\n",
2923 __func__);
2924 goto out_uart_rs232;
2925 }
2926
2927 /* register with IOC4 main module */
2928 ret = ioc4_register_submodule(&ioc4_serial_submodule);
2929 if (ret)
2930 goto out_uart_rs422;
2931 return 0;
2932
2933out_uart_rs422:
2934 uart_unregister_driver(&ioc4_uart_rs422);
2935out_uart_rs232:
2936 uart_unregister_driver(&ioc4_uart_rs232);
2937out:
2938 return ret;
2939}
2940
2941static void __exit ioc4_serial_exit(void)
2942{
2943 ioc4_unregister_submodule(&ioc4_serial_submodule);
2944 uart_unregister_driver(&ioc4_uart_rs232);
2945 uart_unregister_driver(&ioc4_uart_rs422);
2946}
2947
2948late_initcall(ioc4_serial_init); /* Call only after tty init is done */
2949module_exit(ioc4_serial_exit);
2950
2951MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
2952MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC4 Base-IO Card");
2953MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/ip22zilog.c b/drivers/tty/serial/ip22zilog.c
new file mode 100644
index 000000000000..ebff4a1d4bcc
--- /dev/null
+++ b/drivers/tty/serial/ip22zilog.c
@@ -0,0 +1,1221 @@
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/module.h>
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/delay.h>
19#include <linux/tty.h>
20#include <linux/tty_flip.h>
21#include <linux/major.h>
22#include <linux/string.h>
23#include <linux/ptrace.h>
24#include <linux/ioport.h>
25#include <linux/slab.h>
26#include <linux/circ_buf.h>
27#include <linux/serial.h>
28#include <linux/sysrq.h>
29#include <linux/console.h>
30#include <linux/spinlock.h>
31#include <linux/init.h>
32
33#include <asm/io.h>
34#include <asm/irq.h>
35#include <asm/sgialib.h>
36#include <asm/sgi/ioc.h>
37#include <asm/sgi/hpc3.h>
38#include <asm/sgi/ip22.h>
39
40#if defined(CONFIG_SERIAL_IP22_ZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
41#define SUPPORT_SYSRQ
42#endif
43
44#include <linux/serial_core.h>
45
46#include "ip22zilog.h"
47
48/*
49 * On IP22 we need to delay after register accesses but we do not need to
50 * flush writes.
51 */
52#define ZSDELAY() udelay(5)
53#define ZSDELAY_LONG() udelay(20)
54#define ZS_WSYNC(channel) do { } while (0)
55
56#define NUM_IP22ZILOG 1
57#define NUM_CHANNELS (NUM_IP22ZILOG * 2)
58
59#define ZS_CLOCK 3672000 /* Zilog input clock rate. */
60#define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
61
62/*
63 * We wrap our port structure around the generic uart_port.
64 */
65struct uart_ip22zilog_port {
66 struct uart_port port;
67
68 /* IRQ servicing chain. */
69 struct uart_ip22zilog_port *next;
70
71 /* Current values of Zilog write registers. */
72 unsigned char curregs[NUM_ZSREGS];
73
74 unsigned int flags;
75#define IP22ZILOG_FLAG_IS_CONS 0x00000004
76#define IP22ZILOG_FLAG_IS_KGDB 0x00000008
77#define IP22ZILOG_FLAG_MODEM_STATUS 0x00000010
78#define IP22ZILOG_FLAG_IS_CHANNEL_A 0x00000020
79#define IP22ZILOG_FLAG_REGS_HELD 0x00000040
80#define IP22ZILOG_FLAG_TX_STOPPED 0x00000080
81#define IP22ZILOG_FLAG_TX_ACTIVE 0x00000100
82#define IP22ZILOG_FLAG_RESET_DONE 0x00000200
83
84 unsigned int tty_break;
85
86 unsigned char parity_mask;
87 unsigned char prev_status;
88};
89
90#define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel *)((PORT)->membase))
91#define UART_ZILOG(PORT) ((struct uart_ip22zilog_port *)(PORT))
92#define IP22ZILOG_GET_CURR_REG(PORT, REGNUM) \
93 (UART_ZILOG(PORT)->curregs[REGNUM])
94#define IP22ZILOG_SET_CURR_REG(PORT, REGNUM, REGVAL) \
95 ((UART_ZILOG(PORT)->curregs[REGNUM]) = (REGVAL))
96#define ZS_IS_CONS(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_CONS)
97#define ZS_IS_KGDB(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_KGDB)
98#define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & IP22ZILOG_FLAG_MODEM_STATUS)
99#define ZS_IS_CHANNEL_A(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_CHANNEL_A)
100#define ZS_REGS_HELD(UP) ((UP)->flags & IP22ZILOG_FLAG_REGS_HELD)
101#define ZS_TX_STOPPED(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_STOPPED)
102#define ZS_TX_ACTIVE(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_ACTIVE)
103
104/* Reading and writing Zilog8530 registers. The delays are to make this
105 * driver work on the IP22 which needs a settling delay after each chip
106 * register access, other machines handle this in hardware via auxiliary
107 * flip-flops which implement the settle time we do in software.
108 *
109 * The port lock must be held and local IRQs must be disabled
110 * when {read,write}_zsreg is invoked.
111 */
112static unsigned char read_zsreg(struct zilog_channel *channel,
113 unsigned char reg)
114{
115 unsigned char retval;
116
117 writeb(reg, &channel->control);
118 ZSDELAY();
119 retval = readb(&channel->control);
120 ZSDELAY();
121
122 return retval;
123}
124
125static void write_zsreg(struct zilog_channel *channel,
126 unsigned char reg, unsigned char value)
127{
128 writeb(reg, &channel->control);
129 ZSDELAY();
130 writeb(value, &channel->control);
131 ZSDELAY();
132}
133
134static void ip22zilog_clear_fifo(struct zilog_channel *channel)
135{
136 int i;
137
138 for (i = 0; i < 32; i++) {
139 unsigned char regval;
140
141 regval = readb(&channel->control);
142 ZSDELAY();
143 if (regval & Rx_CH_AV)
144 break;
145
146 regval = read_zsreg(channel, R1);
147 readb(&channel->data);
148 ZSDELAY();
149
150 if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
151 writeb(ERR_RES, &channel->control);
152 ZSDELAY();
153 ZS_WSYNC(channel);
154 }
155 }
156}
157
158/* This function must only be called when the TX is not busy. The UART
159 * port lock must be held and local interrupts disabled.
160 */
161static void __load_zsregs(struct zilog_channel *channel, unsigned char *regs)
162{
163 int i;
164
165 /* Let pending transmits finish. */
166 for (i = 0; i < 1000; i++) {
167 unsigned char stat = read_zsreg(channel, R1);
168 if (stat & ALL_SNT)
169 break;
170 udelay(100);
171 }
172
173 writeb(ERR_RES, &channel->control);
174 ZSDELAY();
175 ZS_WSYNC(channel);
176
177 ip22zilog_clear_fifo(channel);
178
179 /* Disable all interrupts. */
180 write_zsreg(channel, R1,
181 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
182
183 /* Set parity, sync config, stop bits, and clock divisor. */
184 write_zsreg(channel, R4, regs[R4]);
185
186 /* Set misc. TX/RX control bits. */
187 write_zsreg(channel, R10, regs[R10]);
188
189 /* Set TX/RX controls sans the enable bits. */
190 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
191 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
192
193 /* Synchronous mode config. */
194 write_zsreg(channel, R6, regs[R6]);
195 write_zsreg(channel, R7, regs[R7]);
196
197 /* Don't mess with the interrupt vector (R2, unused by us) and
198 * master interrupt control (R9). We make sure this is setup
199 * properly at probe time then never touch it again.
200 */
201
202 /* Disable baud generator. */
203 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
204
205 /* Clock mode control. */
206 write_zsreg(channel, R11, regs[R11]);
207
208 /* Lower and upper byte of baud rate generator divisor. */
209 write_zsreg(channel, R12, regs[R12]);
210 write_zsreg(channel, R13, regs[R13]);
211
212 /* Now rewrite R14, with BRENAB (if set). */
213 write_zsreg(channel, R14, regs[R14]);
214
215 /* External status interrupt control. */
216 write_zsreg(channel, R15, regs[R15]);
217
218 /* Reset external status interrupts. */
219 write_zsreg(channel, R0, RES_EXT_INT);
220 write_zsreg(channel, R0, RES_EXT_INT);
221
222 /* Rewrite R3/R5, this time without enables masked. */
223 write_zsreg(channel, R3, regs[R3]);
224 write_zsreg(channel, R5, regs[R5]);
225
226 /* Rewrite R1, this time without IRQ enabled masked. */
227 write_zsreg(channel, R1, regs[R1]);
228}
229
230/* Reprogram the Zilog channel HW registers with the copies found in the
231 * software state struct. If the transmitter is busy, we defer this update
232 * until the next TX complete interrupt. Else, we do it right now.
233 *
234 * The UART port lock must be held and local interrupts disabled.
235 */
236static void ip22zilog_maybe_update_regs(struct uart_ip22zilog_port *up,
237 struct zilog_channel *channel)
238{
239 if (!ZS_REGS_HELD(up)) {
240 if (ZS_TX_ACTIVE(up)) {
241 up->flags |= IP22ZILOG_FLAG_REGS_HELD;
242 } else {
243 __load_zsregs(channel, up->curregs);
244 }
245 }
246}
247
248#define Rx_BRK 0x0100 /* BREAK event software flag. */
249#define Rx_SYS 0x0200 /* SysRq event software flag. */
250
251static struct tty_struct *ip22zilog_receive_chars(struct uart_ip22zilog_port *up,
252 struct zilog_channel *channel)
253{
254 struct tty_struct *tty;
255 unsigned char ch, flag;
256 unsigned int r1;
257
258 tty = NULL;
259 if (up->port.state != NULL &&
260 up->port.state->port.tty != NULL)
261 tty = up->port.state->port.tty;
262
263 for (;;) {
264 ch = readb(&channel->control);
265 ZSDELAY();
266 if (!(ch & Rx_CH_AV))
267 break;
268
269 r1 = read_zsreg(channel, R1);
270 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
271 writeb(ERR_RES, &channel->control);
272 ZSDELAY();
273 ZS_WSYNC(channel);
274 }
275
276 ch = readb(&channel->data);
277 ZSDELAY();
278
279 ch &= up->parity_mask;
280
281 /* Handle the null char got when BREAK is removed. */
282 if (!ch)
283 r1 |= up->tty_break;
284
285 /* A real serial line, record the character and status. */
286 flag = TTY_NORMAL;
287 up->port.icount.rx++;
288 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | Rx_SYS | Rx_BRK)) {
289 up->tty_break = 0;
290
291 if (r1 & (Rx_SYS | Rx_BRK)) {
292 up->port.icount.brk++;
293 if (r1 & Rx_SYS)
294 continue;
295 r1 &= ~(PAR_ERR | CRC_ERR);
296 }
297 else if (r1 & PAR_ERR)
298 up->port.icount.parity++;
299 else if (r1 & CRC_ERR)
300 up->port.icount.frame++;
301 if (r1 & Rx_OVR)
302 up->port.icount.overrun++;
303 r1 &= up->port.read_status_mask;
304 if (r1 & Rx_BRK)
305 flag = TTY_BREAK;
306 else if (r1 & PAR_ERR)
307 flag = TTY_PARITY;
308 else if (r1 & CRC_ERR)
309 flag = TTY_FRAME;
310 }
311
312 if (uart_handle_sysrq_char(&up->port, ch))
313 continue;
314
315 if (tty)
316 uart_insert_char(&up->port, r1, Rx_OVR, ch, flag);
317 }
318 return tty;
319}
320
321static void ip22zilog_status_handle(struct uart_ip22zilog_port *up,
322 struct zilog_channel *channel)
323{
324 unsigned char status;
325
326 status = readb(&channel->control);
327 ZSDELAY();
328
329 writeb(RES_EXT_INT, &channel->control);
330 ZSDELAY();
331 ZS_WSYNC(channel);
332
333 if (up->curregs[R15] & BRKIE) {
334 if ((status & BRK_ABRT) && !(up->prev_status & BRK_ABRT)) {
335 if (uart_handle_break(&up->port))
336 up->tty_break = Rx_SYS;
337 else
338 up->tty_break = Rx_BRK;
339 }
340 }
341
342 if (ZS_WANTS_MODEM_STATUS(up)) {
343 if (status & SYNC)
344 up->port.icount.dsr++;
345
346 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
347 * But it does not tell us which bit has changed, we have to keep
348 * track of this ourselves.
349 */
350 if ((status ^ up->prev_status) ^ DCD)
351 uart_handle_dcd_change(&up->port,
352 (status & DCD));
353 if ((status ^ up->prev_status) ^ CTS)
354 uart_handle_cts_change(&up->port,
355 (status & CTS));
356
357 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
358 }
359
360 up->prev_status = status;
361}
362
363static void ip22zilog_transmit_chars(struct uart_ip22zilog_port *up,
364 struct zilog_channel *channel)
365{
366 struct circ_buf *xmit;
367
368 if (ZS_IS_CONS(up)) {
369 unsigned char status = readb(&channel->control);
370 ZSDELAY();
371
372 /* TX still busy? Just wait for the next TX done interrupt.
373 *
374 * It can occur because of how we do serial console writes. It would
375 * be nice to transmit console writes just like we normally would for
376 * a TTY line. (ie. buffered and TX interrupt driven). That is not
377 * easy because console writes cannot sleep. One solution might be
378 * to poll on enough port->xmit space becomming free. -DaveM
379 */
380 if (!(status & Tx_BUF_EMP))
381 return;
382 }
383
384 up->flags &= ~IP22ZILOG_FLAG_TX_ACTIVE;
385
386 if (ZS_REGS_HELD(up)) {
387 __load_zsregs(channel, up->curregs);
388 up->flags &= ~IP22ZILOG_FLAG_REGS_HELD;
389 }
390
391 if (ZS_TX_STOPPED(up)) {
392 up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
393 goto ack_tx_int;
394 }
395
396 if (up->port.x_char) {
397 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
398 writeb(up->port.x_char, &channel->data);
399 ZSDELAY();
400 ZS_WSYNC(channel);
401
402 up->port.icount.tx++;
403 up->port.x_char = 0;
404 return;
405 }
406
407 if (up->port.state == NULL)
408 goto ack_tx_int;
409 xmit = &up->port.state->xmit;
410 if (uart_circ_empty(xmit))
411 goto ack_tx_int;
412 if (uart_tx_stopped(&up->port))
413 goto ack_tx_int;
414
415 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
416 writeb(xmit->buf[xmit->tail], &channel->data);
417 ZSDELAY();
418 ZS_WSYNC(channel);
419
420 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
421 up->port.icount.tx++;
422
423 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
424 uart_write_wakeup(&up->port);
425
426 return;
427
428ack_tx_int:
429 writeb(RES_Tx_P, &channel->control);
430 ZSDELAY();
431 ZS_WSYNC(channel);
432}
433
434static irqreturn_t ip22zilog_interrupt(int irq, void *dev_id)
435{
436 struct uart_ip22zilog_port *up = dev_id;
437
438 while (up) {
439 struct zilog_channel *channel
440 = ZILOG_CHANNEL_FROM_PORT(&up->port);
441 struct tty_struct *tty;
442 unsigned char r3;
443
444 spin_lock(&up->port.lock);
445 r3 = read_zsreg(channel, R3);
446
447 /* Channel A */
448 tty = NULL;
449 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
450 writeb(RES_H_IUS, &channel->control);
451 ZSDELAY();
452 ZS_WSYNC(channel);
453
454 if (r3 & CHARxIP)
455 tty = ip22zilog_receive_chars(up, channel);
456 if (r3 & CHAEXT)
457 ip22zilog_status_handle(up, channel);
458 if (r3 & CHATxIP)
459 ip22zilog_transmit_chars(up, channel);
460 }
461 spin_unlock(&up->port.lock);
462
463 if (tty)
464 tty_flip_buffer_push(tty);
465
466 /* Channel B */
467 up = up->next;
468 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
469
470 spin_lock(&up->port.lock);
471 tty = NULL;
472 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
473 writeb(RES_H_IUS, &channel->control);
474 ZSDELAY();
475 ZS_WSYNC(channel);
476
477 if (r3 & CHBRxIP)
478 tty = ip22zilog_receive_chars(up, channel);
479 if (r3 & CHBEXT)
480 ip22zilog_status_handle(up, channel);
481 if (r3 & CHBTxIP)
482 ip22zilog_transmit_chars(up, channel);
483 }
484 spin_unlock(&up->port.lock);
485
486 if (tty)
487 tty_flip_buffer_push(tty);
488
489 up = up->next;
490 }
491
492 return IRQ_HANDLED;
493}
494
495/* A convenient way to quickly get R0 status. The caller must _not_ hold the
496 * port lock, it is acquired here.
497 */
498static __inline__ unsigned char ip22zilog_read_channel_status(struct uart_port *port)
499{
500 struct zilog_channel *channel;
501 unsigned char status;
502
503 channel = ZILOG_CHANNEL_FROM_PORT(port);
504 status = readb(&channel->control);
505 ZSDELAY();
506
507 return status;
508}
509
510/* The port lock is not held. */
511static unsigned int ip22zilog_tx_empty(struct uart_port *port)
512{
513 unsigned long flags;
514 unsigned char status;
515 unsigned int ret;
516
517 spin_lock_irqsave(&port->lock, flags);
518
519 status = ip22zilog_read_channel_status(port);
520
521 spin_unlock_irqrestore(&port->lock, flags);
522
523 if (status & Tx_BUF_EMP)
524 ret = TIOCSER_TEMT;
525 else
526 ret = 0;
527
528 return ret;
529}
530
531/* The port lock is held and interrupts are disabled. */
532static unsigned int ip22zilog_get_mctrl(struct uart_port *port)
533{
534 unsigned char status;
535 unsigned int ret;
536
537 status = ip22zilog_read_channel_status(port);
538
539 ret = 0;
540 if (status & DCD)
541 ret |= TIOCM_CAR;
542 if (status & SYNC)
543 ret |= TIOCM_DSR;
544 if (status & CTS)
545 ret |= TIOCM_CTS;
546
547 return ret;
548}
549
550/* The port lock is held and interrupts are disabled. */
551static void ip22zilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
552{
553 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
554 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
555 unsigned char set_bits, clear_bits;
556
557 set_bits = clear_bits = 0;
558
559 if (mctrl & TIOCM_RTS)
560 set_bits |= RTS;
561 else
562 clear_bits |= RTS;
563 if (mctrl & TIOCM_DTR)
564 set_bits |= DTR;
565 else
566 clear_bits |= DTR;
567
568 /* NOTE: Not subject to 'transmitter active' rule. */
569 up->curregs[R5] |= set_bits;
570 up->curregs[R5] &= ~clear_bits;
571 write_zsreg(channel, R5, up->curregs[R5]);
572}
573
574/* The port lock is held and interrupts are disabled. */
575static void ip22zilog_stop_tx(struct uart_port *port)
576{
577 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
578
579 up->flags |= IP22ZILOG_FLAG_TX_STOPPED;
580}
581
582/* The port lock is held and interrupts are disabled. */
583static void ip22zilog_start_tx(struct uart_port *port)
584{
585 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
586 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
587 unsigned char status;
588
589 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
590 up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
591
592 status = readb(&channel->control);
593 ZSDELAY();
594
595 /* TX busy? Just wait for the TX done interrupt. */
596 if (!(status & Tx_BUF_EMP))
597 return;
598
599 /* Send the first character to jump-start the TX done
600 * IRQ sending engine.
601 */
602 if (port->x_char) {
603 writeb(port->x_char, &channel->data);
604 ZSDELAY();
605 ZS_WSYNC(channel);
606
607 port->icount.tx++;
608 port->x_char = 0;
609 } else {
610 struct circ_buf *xmit = &port->state->xmit;
611
612 writeb(xmit->buf[xmit->tail], &channel->data);
613 ZSDELAY();
614 ZS_WSYNC(channel);
615
616 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
617 port->icount.tx++;
618
619 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
620 uart_write_wakeup(&up->port);
621 }
622}
623
624/* The port lock is held and interrupts are disabled. */
625static void ip22zilog_stop_rx(struct uart_port *port)
626{
627 struct uart_ip22zilog_port *up = UART_ZILOG(port);
628 struct zilog_channel *channel;
629
630 if (ZS_IS_CONS(up))
631 return;
632
633 channel = ZILOG_CHANNEL_FROM_PORT(port);
634
635 /* Disable all RX interrupts. */
636 up->curregs[R1] &= ~RxINT_MASK;
637 ip22zilog_maybe_update_regs(up, channel);
638}
639
640/* The port lock is held. */
641static void ip22zilog_enable_ms(struct uart_port *port)
642{
643 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
644 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
645 unsigned char new_reg;
646
647 new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
648 if (new_reg != up->curregs[R15]) {
649 up->curregs[R15] = new_reg;
650
651 /* NOTE: Not subject to 'transmitter active' rule. */
652 write_zsreg(channel, R15, up->curregs[R15]);
653 }
654}
655
656/* The port lock is not held. */
657static void ip22zilog_break_ctl(struct uart_port *port, int break_state)
658{
659 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
660 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
661 unsigned char set_bits, clear_bits, new_reg;
662 unsigned long flags;
663
664 set_bits = clear_bits = 0;
665
666 if (break_state)
667 set_bits |= SND_BRK;
668 else
669 clear_bits |= SND_BRK;
670
671 spin_lock_irqsave(&port->lock, flags);
672
673 new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
674 if (new_reg != up->curregs[R5]) {
675 up->curregs[R5] = new_reg;
676
677 /* NOTE: Not subject to 'transmitter active' rule. */
678 write_zsreg(channel, R5, up->curregs[R5]);
679 }
680
681 spin_unlock_irqrestore(&port->lock, flags);
682}
683
684static void __ip22zilog_reset(struct uart_ip22zilog_port *up)
685{
686 struct zilog_channel *channel;
687 int i;
688
689 if (up->flags & IP22ZILOG_FLAG_RESET_DONE)
690 return;
691
692 /* Let pending transmits finish. */
693 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
694 for (i = 0; i < 1000; i++) {
695 unsigned char stat = read_zsreg(channel, R1);
696 if (stat & ALL_SNT)
697 break;
698 udelay(100);
699 }
700
701 if (!ZS_IS_CHANNEL_A(up)) {
702 up++;
703 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
704 }
705 write_zsreg(channel, R9, FHWRES);
706 ZSDELAY_LONG();
707 (void) read_zsreg(channel, R0);
708
709 up->flags |= IP22ZILOG_FLAG_RESET_DONE;
710 up->next->flags |= IP22ZILOG_FLAG_RESET_DONE;
711}
712
713static void __ip22zilog_startup(struct uart_ip22zilog_port *up)
714{
715 struct zilog_channel *channel;
716
717 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
718
719 __ip22zilog_reset(up);
720
721 __load_zsregs(channel, up->curregs);
722 /* set master interrupt enable */
723 write_zsreg(channel, R9, up->curregs[R9]);
724 up->prev_status = readb(&channel->control);
725
726 /* Enable receiver and transmitter. */
727 up->curregs[R3] |= RxENAB;
728 up->curregs[R5] |= TxENAB;
729
730 up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
731 ip22zilog_maybe_update_regs(up, channel);
732}
733
734static int ip22zilog_startup(struct uart_port *port)
735{
736 struct uart_ip22zilog_port *up = UART_ZILOG(port);
737 unsigned long flags;
738
739 if (ZS_IS_CONS(up))
740 return 0;
741
742 spin_lock_irqsave(&port->lock, flags);
743 __ip22zilog_startup(up);
744 spin_unlock_irqrestore(&port->lock, flags);
745 return 0;
746}
747
748/*
749 * The test for ZS_IS_CONS is explained by the following e-mail:
750 *****
751 * From: Russell King <rmk@arm.linux.org.uk>
752 * Date: Sun, 8 Dec 2002 10:18:38 +0000
753 *
754 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
755 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
756 * > and I noticed that something is not right with reference
757 * > counting in this case. It seems that when the console
758 * > is open by kernel initially, this is not accounted
759 * > as an open, and uart_startup is not called.
760 *
761 * That is correct. We are unable to call uart_startup when the serial
762 * console is initialised because it may need to allocate memory (as
763 * request_irq does) and the memory allocators may not have been
764 * initialised.
765 *
766 * 1. initialise the port into a state where it can send characters in the
767 * console write method.
768 *
769 * 2. don't do the actual hardware shutdown in your shutdown() method (but
770 * do the normal software shutdown - ie, free irqs etc)
771 *****
772 */
773static void ip22zilog_shutdown(struct uart_port *port)
774{
775 struct uart_ip22zilog_port *up = UART_ZILOG(port);
776 struct zilog_channel *channel;
777 unsigned long flags;
778
779 if (ZS_IS_CONS(up))
780 return;
781
782 spin_lock_irqsave(&port->lock, flags);
783
784 channel = ZILOG_CHANNEL_FROM_PORT(port);
785
786 /* Disable receiver and transmitter. */
787 up->curregs[R3] &= ~RxENAB;
788 up->curregs[R5] &= ~TxENAB;
789
790 /* Disable all interrupts and BRK assertion. */
791 up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
792 up->curregs[R5] &= ~SND_BRK;
793 ip22zilog_maybe_update_regs(up, channel);
794
795 spin_unlock_irqrestore(&port->lock, flags);
796}
797
798/* Shared by TTY driver and serial console setup. The port lock is held
799 * and local interrupts are disabled.
800 */
801static void
802ip22zilog_convert_to_zs(struct uart_ip22zilog_port *up, unsigned int cflag,
803 unsigned int iflag, int brg)
804{
805
806 up->curregs[R10] = NRZ;
807 up->curregs[R11] = TCBR | RCBR;
808
809 /* Program BAUD and clock source. */
810 up->curregs[R4] &= ~XCLK_MASK;
811 up->curregs[R4] |= X16CLK;
812 up->curregs[R12] = brg & 0xff;
813 up->curregs[R13] = (brg >> 8) & 0xff;
814 up->curregs[R14] = BRENAB;
815
816 /* Character size, stop bits, and parity. */
817 up->curregs[3] &= ~RxN_MASK;
818 up->curregs[5] &= ~TxN_MASK;
819 switch (cflag & CSIZE) {
820 case CS5:
821 up->curregs[3] |= Rx5;
822 up->curregs[5] |= Tx5;
823 up->parity_mask = 0x1f;
824 break;
825 case CS6:
826 up->curregs[3] |= Rx6;
827 up->curregs[5] |= Tx6;
828 up->parity_mask = 0x3f;
829 break;
830 case CS7:
831 up->curregs[3] |= Rx7;
832 up->curregs[5] |= Tx7;
833 up->parity_mask = 0x7f;
834 break;
835 case CS8:
836 default:
837 up->curregs[3] |= Rx8;
838 up->curregs[5] |= Tx8;
839 up->parity_mask = 0xff;
840 break;
841 };
842 up->curregs[4] &= ~0x0c;
843 if (cflag & CSTOPB)
844 up->curregs[4] |= SB2;
845 else
846 up->curregs[4] |= SB1;
847 if (cflag & PARENB)
848 up->curregs[4] |= PAR_ENAB;
849 else
850 up->curregs[4] &= ~PAR_ENAB;
851 if (!(cflag & PARODD))
852 up->curregs[4] |= PAR_EVEN;
853 else
854 up->curregs[4] &= ~PAR_EVEN;
855
856 up->port.read_status_mask = Rx_OVR;
857 if (iflag & INPCK)
858 up->port.read_status_mask |= CRC_ERR | PAR_ERR;
859 if (iflag & (BRKINT | PARMRK))
860 up->port.read_status_mask |= BRK_ABRT;
861
862 up->port.ignore_status_mask = 0;
863 if (iflag & IGNPAR)
864 up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
865 if (iflag & IGNBRK) {
866 up->port.ignore_status_mask |= BRK_ABRT;
867 if (iflag & IGNPAR)
868 up->port.ignore_status_mask |= Rx_OVR;
869 }
870
871 if ((cflag & CREAD) == 0)
872 up->port.ignore_status_mask = 0xff;
873}
874
875/* The port lock is not held. */
876static void
877ip22zilog_set_termios(struct uart_port *port, struct ktermios *termios,
878 struct ktermios *old)
879{
880 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
881 unsigned long flags;
882 int baud, brg;
883
884 baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
885
886 spin_lock_irqsave(&up->port.lock, flags);
887
888 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
889
890 ip22zilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
891
892 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
893 up->flags |= IP22ZILOG_FLAG_MODEM_STATUS;
894 else
895 up->flags &= ~IP22ZILOG_FLAG_MODEM_STATUS;
896
897 ip22zilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
898 uart_update_timeout(port, termios->c_cflag, baud);
899
900 spin_unlock_irqrestore(&up->port.lock, flags);
901}
902
903static const char *ip22zilog_type(struct uart_port *port)
904{
905 return "IP22-Zilog";
906}
907
908/* We do not request/release mappings of the registers here, this
909 * happens at early serial probe time.
910 */
911static void ip22zilog_release_port(struct uart_port *port)
912{
913}
914
915static int ip22zilog_request_port(struct uart_port *port)
916{
917 return 0;
918}
919
920/* These do not need to do anything interesting either. */
921static void ip22zilog_config_port(struct uart_port *port, int flags)
922{
923}
924
925/* We do not support letting the user mess with the divisor, IRQ, etc. */
926static int ip22zilog_verify_port(struct uart_port *port, struct serial_struct *ser)
927{
928 return -EINVAL;
929}
930
931static struct uart_ops ip22zilog_pops = {
932 .tx_empty = ip22zilog_tx_empty,
933 .set_mctrl = ip22zilog_set_mctrl,
934 .get_mctrl = ip22zilog_get_mctrl,
935 .stop_tx = ip22zilog_stop_tx,
936 .start_tx = ip22zilog_start_tx,
937 .stop_rx = ip22zilog_stop_rx,
938 .enable_ms = ip22zilog_enable_ms,
939 .break_ctl = ip22zilog_break_ctl,
940 .startup = ip22zilog_startup,
941 .shutdown = ip22zilog_shutdown,
942 .set_termios = ip22zilog_set_termios,
943 .type = ip22zilog_type,
944 .release_port = ip22zilog_release_port,
945 .request_port = ip22zilog_request_port,
946 .config_port = ip22zilog_config_port,
947 .verify_port = ip22zilog_verify_port,
948};
949
950static struct uart_ip22zilog_port *ip22zilog_port_table;
951static struct zilog_layout **ip22zilog_chip_regs;
952
953static struct uart_ip22zilog_port *ip22zilog_irq_chain;
954static int zilog_irq = -1;
955
956static void * __init alloc_one_table(unsigned long size)
957{
958 return kzalloc(size, GFP_KERNEL);
959}
960
961static void __init ip22zilog_alloc_tables(void)
962{
963 ip22zilog_port_table = (struct uart_ip22zilog_port *)
964 alloc_one_table(NUM_CHANNELS * sizeof(struct uart_ip22zilog_port));
965 ip22zilog_chip_regs = (struct zilog_layout **)
966 alloc_one_table(NUM_IP22ZILOG * sizeof(struct zilog_layout *));
967
968 if (ip22zilog_port_table == NULL || ip22zilog_chip_regs == NULL) {
969 panic("IP22-Zilog: Cannot allocate IP22-Zilog tables.");
970 }
971}
972
973/* Get the address of the registers for IP22-Zilog instance CHIP. */
974static struct zilog_layout * __init get_zs(int chip)
975{
976 unsigned long base;
977
978 if (chip < 0 || chip >= NUM_IP22ZILOG) {
979 panic("IP22-Zilog: Illegal chip number %d in get_zs.", chip);
980 }
981
982 /* Not probe-able, hard code it. */
983 base = (unsigned long) &sgioc->uart;
984
985 zilog_irq = SGI_SERIAL_IRQ;
986 request_mem_region(base, 8, "IP22-Zilog");
987
988 return (struct zilog_layout *) base;
989}
990
991#define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
992
993#ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
994static void ip22zilog_put_char(struct uart_port *port, int ch)
995{
996 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
997 int loops = ZS_PUT_CHAR_MAX_DELAY;
998
999 /* This is a timed polling loop so do not switch the explicit
1000 * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
1001 */
1002 do {
1003 unsigned char val = readb(&channel->control);
1004 if (val & Tx_BUF_EMP) {
1005 ZSDELAY();
1006 break;
1007 }
1008 udelay(5);
1009 } while (--loops);
1010
1011 writeb(ch, &channel->data);
1012 ZSDELAY();
1013 ZS_WSYNC(channel);
1014}
1015
1016static void
1017ip22zilog_console_write(struct console *con, const char *s, unsigned int count)
1018{
1019 struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
1020 unsigned long flags;
1021
1022 spin_lock_irqsave(&up->port.lock, flags);
1023 uart_console_write(&up->port, s, count, ip22zilog_put_char);
1024 udelay(2);
1025 spin_unlock_irqrestore(&up->port.lock, flags);
1026}
1027
1028static int __init ip22zilog_console_setup(struct console *con, char *options)
1029{
1030 struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
1031 unsigned long flags;
1032 int baud = 9600, bits = 8;
1033 int parity = 'n';
1034 int flow = 'n';
1035
1036 up->flags |= IP22ZILOG_FLAG_IS_CONS;
1037
1038 printk(KERN_INFO "Console: ttyS%d (IP22-Zilog)\n", con->index);
1039
1040 spin_lock_irqsave(&up->port.lock, flags);
1041
1042 up->curregs[R15] |= BRKIE;
1043
1044 __ip22zilog_startup(up);
1045
1046 spin_unlock_irqrestore(&up->port.lock, flags);
1047
1048 if (options)
1049 uart_parse_options(options, &baud, &parity, &bits, &flow);
1050 return uart_set_options(&up->port, con, baud, parity, bits, flow);
1051}
1052
1053static struct uart_driver ip22zilog_reg;
1054
1055static struct console ip22zilog_console = {
1056 .name = "ttyS",
1057 .write = ip22zilog_console_write,
1058 .device = uart_console_device,
1059 .setup = ip22zilog_console_setup,
1060 .flags = CON_PRINTBUFFER,
1061 .index = -1,
1062 .data = &ip22zilog_reg,
1063};
1064#endif /* CONFIG_SERIAL_IP22_ZILOG_CONSOLE */
1065
1066static struct uart_driver ip22zilog_reg = {
1067 .owner = THIS_MODULE,
1068 .driver_name = "serial",
1069 .dev_name = "ttyS",
1070 .major = TTY_MAJOR,
1071 .minor = 64,
1072 .nr = NUM_CHANNELS,
1073#ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
1074 .cons = &ip22zilog_console,
1075#endif
1076};
1077
1078static void __init ip22zilog_prepare(void)
1079{
1080 struct uart_ip22zilog_port *up;
1081 struct zilog_layout *rp;
1082 int channel, chip;
1083
1084 /*
1085 * Temporary fix.
1086 */
1087 for (channel = 0; channel < NUM_CHANNELS; channel++)
1088 spin_lock_init(&ip22zilog_port_table[channel].port.lock);
1089
1090 ip22zilog_irq_chain = &ip22zilog_port_table[NUM_CHANNELS - 1];
1091 up = &ip22zilog_port_table[0];
1092 for (channel = NUM_CHANNELS - 1 ; channel > 0; channel--)
1093 up[channel].next = &up[channel - 1];
1094 up[channel].next = NULL;
1095
1096 for (chip = 0; chip < NUM_IP22ZILOG; chip++) {
1097 if (!ip22zilog_chip_regs[chip]) {
1098 ip22zilog_chip_regs[chip] = rp = get_zs(chip);
1099
1100 up[(chip * 2) + 0].port.membase = (char *) &rp->channelB;
1101 up[(chip * 2) + 1].port.membase = (char *) &rp->channelA;
1102
1103 /* In theory mapbase is the physical address ... */
1104 up[(chip * 2) + 0].port.mapbase =
1105 (unsigned long) ioremap((unsigned long) &rp->channelB, 8);
1106 up[(chip * 2) + 1].port.mapbase =
1107 (unsigned long) ioremap((unsigned long) &rp->channelA, 8);
1108 }
1109
1110 /* Channel A */
1111 up[(chip * 2) + 0].port.iotype = UPIO_MEM;
1112 up[(chip * 2) + 0].port.irq = zilog_irq;
1113 up[(chip * 2) + 0].port.uartclk = ZS_CLOCK;
1114 up[(chip * 2) + 0].port.fifosize = 1;
1115 up[(chip * 2) + 0].port.ops = &ip22zilog_pops;
1116 up[(chip * 2) + 0].port.type = PORT_IP22ZILOG;
1117 up[(chip * 2) + 0].port.flags = 0;
1118 up[(chip * 2) + 0].port.line = (chip * 2) + 0;
1119 up[(chip * 2) + 0].flags = 0;
1120
1121 /* Channel B */
1122 up[(chip * 2) + 1].port.iotype = UPIO_MEM;
1123 up[(chip * 2) + 1].port.irq = zilog_irq;
1124 up[(chip * 2) + 1].port.uartclk = ZS_CLOCK;
1125 up[(chip * 2) + 1].port.fifosize = 1;
1126 up[(chip * 2) + 1].port.ops = &ip22zilog_pops;
1127 up[(chip * 2) + 1].port.type = PORT_IP22ZILOG;
1128 up[(chip * 2) + 1].port.line = (chip * 2) + 1;
1129 up[(chip * 2) + 1].flags |= IP22ZILOG_FLAG_IS_CHANNEL_A;
1130 }
1131
1132 for (channel = 0; channel < NUM_CHANNELS; channel++) {
1133 struct uart_ip22zilog_port *up = &ip22zilog_port_table[channel];
1134 int brg;
1135
1136 /* Normal serial TTY. */
1137 up->parity_mask = 0xff;
1138 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1139 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1140 up->curregs[R3] = RxENAB | Rx8;
1141 up->curregs[R5] = TxENAB | Tx8;
1142 up->curregs[R9] = NV | MIE;
1143 up->curregs[R10] = NRZ;
1144 up->curregs[R11] = TCBR | RCBR;
1145 brg = BPS_TO_BRG(9600, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1146 up->curregs[R12] = (brg & 0xff);
1147 up->curregs[R13] = (brg >> 8) & 0xff;
1148 up->curregs[R14] = BRENAB;
1149 }
1150}
1151
1152static int __init ip22zilog_ports_init(void)
1153{
1154 int ret;
1155
1156 printk(KERN_INFO "Serial: IP22 Zilog driver (%d chips).\n", NUM_IP22ZILOG);
1157
1158 ip22zilog_prepare();
1159
1160 if (request_irq(zilog_irq, ip22zilog_interrupt, 0,
1161 "IP22-Zilog", ip22zilog_irq_chain)) {
1162 panic("IP22-Zilog: Unable to register zs interrupt handler.\n");
1163 }
1164
1165 ret = uart_register_driver(&ip22zilog_reg);
1166 if (ret == 0) {
1167 int i;
1168
1169 for (i = 0; i < NUM_CHANNELS; i++) {
1170 struct uart_ip22zilog_port *up = &ip22zilog_port_table[i];
1171
1172 uart_add_one_port(&ip22zilog_reg, &up->port);
1173 }
1174 }
1175
1176 return ret;
1177}
1178
1179static int __init ip22zilog_init(void)
1180{
1181 /* IP22 Zilog setup is hard coded, no probing to do. */
1182 ip22zilog_alloc_tables();
1183 ip22zilog_ports_init();
1184
1185 return 0;
1186}
1187
1188static void __exit ip22zilog_exit(void)
1189{
1190 int i;
1191 struct uart_ip22zilog_port *up;
1192
1193 for (i = 0; i < NUM_CHANNELS; i++) {
1194 up = &ip22zilog_port_table[i];
1195
1196 uart_remove_one_port(&ip22zilog_reg, &up->port);
1197 }
1198
1199 /* Free IO mem */
1200 up = &ip22zilog_port_table[0];
1201 for (i = 0; i < NUM_IP22ZILOG; i++) {
1202 if (up[(i * 2) + 0].port.mapbase) {
1203 iounmap((void*)up[(i * 2) + 0].port.mapbase);
1204 up[(i * 2) + 0].port.mapbase = 0;
1205 }
1206 if (up[(i * 2) + 1].port.mapbase) {
1207 iounmap((void*)up[(i * 2) + 1].port.mapbase);
1208 up[(i * 2) + 1].port.mapbase = 0;
1209 }
1210 }
1211
1212 uart_unregister_driver(&ip22zilog_reg);
1213}
1214
1215module_init(ip22zilog_init);
1216module_exit(ip22zilog_exit);
1217
1218/* David wrote it but I'm to blame for the bugs ... */
1219MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1220MODULE_DESCRIPTION("SGI Zilog serial port driver");
1221MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/ip22zilog.h b/drivers/tty/serial/ip22zilog.h
new file mode 100644
index 000000000000..a59a9a8341d2
--- /dev/null
+++ b/drivers/tty/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/tty/serial/jsm/Makefile b/drivers/tty/serial/jsm/Makefile
new file mode 100644
index 000000000000..e46b6e0f8b18
--- /dev/null
+++ b/drivers/tty/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/tty/serial/jsm/jsm.h b/drivers/tty/serial/jsm/jsm.h
new file mode 100644
index 000000000000..38a509c684cd
--- /dev/null
+++ b/drivers/tty/serial/jsm/jsm.h
@@ -0,0 +1,388 @@
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.ibm.com>
24 *
25 ***********************************************************************/
26
27#ifndef __JSM_DRIVER_H
28#define __JSM_DRIVER_H
29
30#include <linux/kernel.h>
31#include <linux/types.h> /* To pick up the varions Linux types */
32#include <linux/tty.h>
33#include <linux/serial_core.h>
34#include <linux/device.h>
35
36/*
37 * Debugging levels can be set using debug insmod variable
38 * They can also be compiled out completely.
39 */
40enum {
41 DBG_INIT = 0x01,
42 DBG_BASIC = 0x02,
43 DBG_CORE = 0x04,
44 DBG_OPEN = 0x08,
45 DBG_CLOSE = 0x10,
46 DBG_READ = 0x20,
47 DBG_WRITE = 0x40,
48 DBG_IOCTL = 0x80,
49 DBG_PROC = 0x100,
50 DBG_PARAM = 0x200,
51 DBG_PSCAN = 0x400,
52 DBG_EVENT = 0x800,
53 DBG_DRAIN = 0x1000,
54 DBG_MSIGS = 0x2000,
55 DBG_MGMT = 0x4000,
56 DBG_INTR = 0x8000,
57 DBG_CARR = 0x10000,
58};
59
60#define jsm_printk(nlevel, klevel, pdev, fmt, args...) \
61 if ((DBG_##nlevel & jsm_debug)) \
62 dev_printk(KERN_##klevel, pdev->dev, fmt, ## args)
63
64#define MAXLINES 256
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.2-1-INKERNEL"
94#define JSM_PARTNUM "40002438_A-INKERNEL"
95
96struct jsm_board;
97struct jsm_channel;
98
99/************************************************************************
100 * Per board operations structure *
101 ************************************************************************/
102struct board_ops {
103 irq_handler_t intr;
104 void (*uart_init) (struct jsm_channel *ch);
105 void (*uart_off) (struct jsm_channel *ch);
106 void (*param) (struct jsm_channel *ch);
107 void (*assert_modem_signals) (struct jsm_channel *ch);
108 void (*flush_uart_write) (struct jsm_channel *ch);
109 void (*flush_uart_read) (struct jsm_channel *ch);
110 void (*disable_receiver) (struct jsm_channel *ch);
111 void (*enable_receiver) (struct jsm_channel *ch);
112 void (*send_break) (struct jsm_channel *ch);
113 void (*clear_break) (struct jsm_channel *ch, int);
114 void (*send_start_character) (struct jsm_channel *ch);
115 void (*send_stop_character) (struct jsm_channel *ch);
116 void (*copy_data_from_queue_to_uart) (struct jsm_channel *ch);
117 u32 (*get_uart_bytes_left) (struct jsm_channel *ch);
118 void (*send_immediate_char) (struct jsm_channel *ch, unsigned char);
119};
120
121
122/*
123 * Per-board information
124 */
125struct jsm_board
126{
127 int boardnum; /* Board number: 0-32 */
128
129 int type; /* Type of board */
130 u8 rev; /* PCI revision ID */
131 struct pci_dev *pci_dev;
132 u32 maxports; /* MAX ports this board can handle */
133
134 spinlock_t bd_intr_lock; /* Used to protect the poller tasklet and
135 * the interrupt routine from each other.
136 */
137
138 u32 nasync; /* Number of ports on card */
139
140 u32 irq; /* Interrupt request number */
141
142 u64 membase; /* Start of base memory of the card */
143 u64 membase_end; /* End of base memory of the card */
144
145 u8 __iomem *re_map_membase;/* Remapped memory of the card */
146
147 u64 iobase; /* Start of io base of the card */
148 u64 iobase_end; /* End of io base of the card */
149
150 u32 bd_uart_offset; /* Space between each UART */
151
152 struct jsm_channel *channels[MAXPORTS]; /* array of pointers to our channels. */
153 char *flipbuf; /* Our flip buffer, alloced if board is found */
154
155 u32 bd_dividend; /* Board/UARTs specific dividend */
156
157 struct board_ops *bd_ops;
158
159 struct list_head jsm_board_entry;
160};
161
162/************************************************************************
163 * Device flag definitions for ch_flags.
164 ************************************************************************/
165#define CH_PRON 0x0001 /* Printer on string */
166#define CH_STOP 0x0002 /* Output is stopped */
167#define CH_STOPI 0x0004 /* Input is stopped */
168#define CH_CD 0x0008 /* Carrier is present */
169#define CH_FCAR 0x0010 /* Carrier forced on */
170#define CH_HANGUP 0x0020 /* Hangup received */
171
172#define CH_RECEIVER_OFF 0x0040 /* Receiver is off */
173#define CH_OPENING 0x0080 /* Port in fragile open state */
174#define CH_CLOSING 0x0100 /* Port in fragile close state */
175#define CH_FIFO_ENABLED 0x0200 /* Port has FIFOs enabled */
176#define CH_TX_FIFO_EMPTY 0x0400 /* TX Fifo is completely empty */
177#define CH_TX_FIFO_LWM 0x0800 /* TX Fifo is below Low Water */
178#define CH_BREAK_SENDING 0x1000 /* Break is being sent */
179#define CH_LOOPBACK 0x2000 /* Channel is in lookback mode */
180#define CH_FLIPBUF_IN_USE 0x4000 /* Channel's flipbuf is in use */
181#define CH_BAUD0 0x08000 /* Used for checking B0 transitions */
182
183/* Our Read/Error/Write queue sizes */
184#define RQUEUEMASK 0x1FFF /* 8 K - 1 */
185#define EQUEUEMASK 0x1FFF /* 8 K - 1 */
186#define WQUEUEMASK 0x0FFF /* 4 K - 1 */
187#define RQUEUESIZE (RQUEUEMASK + 1)
188#define EQUEUESIZE RQUEUESIZE
189#define WQUEUESIZE (WQUEUEMASK + 1)
190
191
192/************************************************************************
193 * Channel information structure.
194 ************************************************************************/
195struct jsm_channel {
196 struct uart_port uart_port;
197 struct jsm_board *ch_bd; /* Board structure pointer */
198
199 spinlock_t ch_lock; /* provide for serialization */
200 wait_queue_head_t ch_flags_wait;
201
202 u32 ch_portnum; /* Port number, 0 offset. */
203 u32 ch_open_count; /* open count */
204 u32 ch_flags; /* Channel flags */
205
206 u64 ch_close_delay; /* How long we should drop RTS/DTR for */
207
208 tcflag_t ch_c_iflag; /* channel iflags */
209 tcflag_t ch_c_cflag; /* channel cflags */
210 tcflag_t ch_c_oflag; /* channel oflags */
211 tcflag_t ch_c_lflag; /* channel lflags */
212 u8 ch_stopc; /* Stop character */
213 u8 ch_startc; /* Start character */
214
215 u8 ch_mostat; /* FEP output modem status */
216 u8 ch_mistat; /* FEP input modem status */
217
218 struct neo_uart_struct __iomem *ch_neo_uart; /* Pointer to the "mapped" UART struct */
219 u8 ch_cached_lsr; /* Cached value of the LSR register */
220
221 u8 *ch_rqueue; /* Our read queue buffer - malloc'ed */
222 u16 ch_r_head; /* Head location of the read queue */
223 u16 ch_r_tail; /* Tail location of the read queue */
224
225 u8 *ch_equeue; /* Our error queue buffer - malloc'ed */
226 u16 ch_e_head; /* Head location of the error queue */
227 u16 ch_e_tail; /* Tail location of the error queue */
228
229 u8 *ch_wqueue; /* Our write queue buffer - malloc'ed */
230 u16 ch_w_head; /* Head location of the write queue */
231 u16 ch_w_tail; /* Tail location of the write queue */
232
233 u64 ch_rxcount; /* total of data received so far */
234 u64 ch_txcount; /* total of data transmitted so far */
235
236 u8 ch_r_tlevel; /* Receive Trigger level */
237 u8 ch_t_tlevel; /* Transmit Trigger level */
238
239 u8 ch_r_watermark; /* Receive Watermark */
240
241
242 u32 ch_stops_sent; /* How many times I have sent a stop character
243 * to try to stop the other guy sending.
244 */
245 u64 ch_err_parity; /* Count of parity errors on channel */
246 u64 ch_err_frame; /* Count of framing errors on channel */
247 u64 ch_err_break; /* Count of breaks on channel */
248 u64 ch_err_overrun; /* Count of overruns on channel */
249
250 u64 ch_xon_sends; /* Count of xons transmitted */
251 u64 ch_xoff_sends; /* Count of xoffs transmitted */
252};
253
254
255/************************************************************************
256 * Per channel/port NEO UART structure *
257 ************************************************************************
258 * Base Structure Entries Usage Meanings to Host *
259 * *
260 * W = read write R = read only *
261 * U = Unused. *
262 ************************************************************************/
263
264struct neo_uart_struct {
265 u8 txrx; /* WR RHR/THR - Holding Reg */
266 u8 ier; /* WR IER - Interrupt Enable Reg */
267 u8 isr_fcr; /* WR ISR/FCR - Interrupt Status Reg/Fifo Control Reg */
268 u8 lcr; /* WR LCR - Line Control Reg */
269 u8 mcr; /* WR MCR - Modem Control Reg */
270 u8 lsr; /* WR LSR - Line Status Reg */
271 u8 msr; /* WR MSR - Modem Status Reg */
272 u8 spr; /* WR SPR - Scratch Pad Reg */
273 u8 fctr; /* WR FCTR - Feature Control Reg */
274 u8 efr; /* WR EFR - Enhanced Function Reg */
275 u8 tfifo; /* WR TXCNT/TXTRG - Transmit FIFO Reg */
276 u8 rfifo; /* WR RXCNT/RXTRG - Recieve FIFO Reg */
277 u8 xoffchar1; /* WR XOFF 1 - XOff Character 1 Reg */
278 u8 xoffchar2; /* WR XOFF 2 - XOff Character 2 Reg */
279 u8 xonchar1; /* WR XON 1 - Xon Character 1 Reg */
280 u8 xonchar2; /* WR XON 2 - XOn Character 2 Reg */
281
282 u8 reserved1[0x2ff - 0x200]; /* U Reserved by Exar */
283 u8 txrxburst[64]; /* RW 64 bytes of RX/TX FIFO Data */
284 u8 reserved2[0x37f - 0x340]; /* U Reserved by Exar */
285 u8 rxburst_with_errors[64]; /* R 64 bytes of RX FIFO Data + LSR */
286};
287
288/* Where to read the extended interrupt register (32bits instead of 8bits) */
289#define UART_17158_POLL_ADDR_OFFSET 0x80
290
291/*
292 * These are the redefinitions for the FCTR on the XR17C158, since
293 * Exar made them different than their earlier design. (XR16C854)
294 */
295
296/* These are only applicable when table D is selected */
297#define UART_17158_FCTR_RTS_NODELAY 0x00
298#define UART_17158_FCTR_RTS_4DELAY 0x01
299#define UART_17158_FCTR_RTS_6DELAY 0x02
300#define UART_17158_FCTR_RTS_8DELAY 0x03
301#define UART_17158_FCTR_RTS_12DELAY 0x12
302#define UART_17158_FCTR_RTS_16DELAY 0x05
303#define UART_17158_FCTR_RTS_20DELAY 0x13
304#define UART_17158_FCTR_RTS_24DELAY 0x06
305#define UART_17158_FCTR_RTS_28DELAY 0x14
306#define UART_17158_FCTR_RTS_32DELAY 0x07
307#define UART_17158_FCTR_RTS_36DELAY 0x16
308#define UART_17158_FCTR_RTS_40DELAY 0x08
309#define UART_17158_FCTR_RTS_44DELAY 0x09
310#define UART_17158_FCTR_RTS_48DELAY 0x10
311#define UART_17158_FCTR_RTS_52DELAY 0x11
312
313#define UART_17158_FCTR_RTS_IRDA 0x10
314#define UART_17158_FCTR_RS485 0x20
315#define UART_17158_FCTR_TRGA 0x00
316#define UART_17158_FCTR_TRGB 0x40
317#define UART_17158_FCTR_TRGC 0x80
318#define UART_17158_FCTR_TRGD 0xC0
319
320/* 17158 trigger table selects.. */
321#define UART_17158_FCTR_BIT6 0x40
322#define UART_17158_FCTR_BIT7 0x80
323
324/* 17158 TX/RX memmapped buffer offsets */
325#define UART_17158_RX_FIFOSIZE 64
326#define UART_17158_TX_FIFOSIZE 64
327
328/* 17158 Extended IIR's */
329#define UART_17158_IIR_RDI_TIMEOUT 0x0C /* Receiver data TIMEOUT */
330#define UART_17158_IIR_XONXOFF 0x10 /* Received an XON/XOFF char */
331#define UART_17158_IIR_HWFLOW_STATE_CHANGE 0x20 /* CTS/DSR or RTS/DTR state change */
332#define UART_17158_IIR_FIFO_ENABLED 0xC0 /* 16550 FIFOs are Enabled */
333
334/*
335 * These are the extended interrupts that get sent
336 * back to us from the UART's 32bit interrupt register
337 */
338#define UART_17158_RX_LINE_STATUS 0x1 /* RX Ready */
339#define UART_17158_RXRDY_TIMEOUT 0x2 /* RX Ready Timeout */
340#define UART_17158_TXRDY 0x3 /* TX Ready */
341#define UART_17158_MSR 0x4 /* Modem State Change */
342#define UART_17158_TX_AND_FIFO_CLR 0x40 /* Transmitter Holding Reg Empty */
343#define UART_17158_RX_FIFO_DATA_ERROR 0x80 /* UART detected an RX FIFO Data error */
344
345/*
346 * These are the EXTENDED definitions for the 17C158's Interrupt
347 * Enable Register.
348 */
349#define UART_17158_EFR_ECB 0x10 /* Enhanced control bit */
350#define UART_17158_EFR_IXON 0x2 /* Receiver compares Xon1/Xoff1 */
351#define UART_17158_EFR_IXOFF 0x8 /* Transmit Xon1/Xoff1 */
352#define UART_17158_EFR_RTSDTR 0x40 /* Auto RTS/DTR Flow Control Enable */
353#define UART_17158_EFR_CTSDSR 0x80 /* Auto CTS/DSR Flow COntrol Enable */
354
355#define UART_17158_XOFF_DETECT 0x1 /* Indicates whether chip saw an incoming XOFF char */
356#define UART_17158_XON_DETECT 0x2 /* Indicates whether chip saw an incoming XON char */
357
358#define UART_17158_IER_RSVD1 0x10 /* Reserved by Exar */
359#define UART_17158_IER_XOFF 0x20 /* Xoff Interrupt Enable */
360#define UART_17158_IER_RTSDTR 0x40 /* Output Interrupt Enable */
361#define UART_17158_IER_CTSDSR 0x80 /* Input Interrupt Enable */
362
363#define PCI_DEVICE_NEO_2DB9_PCI_NAME "Neo 2 - DB9 Universal PCI"
364#define PCI_DEVICE_NEO_2DB9PRI_PCI_NAME "Neo 2 - DB9 Universal PCI - Powered Ring Indicator"
365#define PCI_DEVICE_NEO_2RJ45_PCI_NAME "Neo 2 - RJ45 Universal PCI"
366#define PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME "Neo 2 - RJ45 Universal PCI - Powered Ring Indicator"
367#define PCIE_DEVICE_NEO_IBM_PCI_NAME "Neo 4 - PCI Express - IBM"
368
369/*
370 * Our Global Variables.
371 */
372extern struct uart_driver jsm_uart_driver;
373extern struct board_ops jsm_neo_ops;
374extern int jsm_debug;
375
376/*************************************************************************
377 *
378 * Prototypes for non-static functions used in more than one module
379 *
380 *************************************************************************/
381int jsm_tty_write(struct uart_port *port);
382int jsm_tty_init(struct jsm_board *);
383int jsm_uart_port_init(struct jsm_board *);
384int jsm_remove_uart_port(struct jsm_board *);
385void jsm_input(struct jsm_channel *ch);
386void jsm_check_queue_flow_control(struct jsm_channel *ch);
387
388#endif
diff --git a/drivers/tty/serial/jsm/jsm_driver.c b/drivers/tty/serial/jsm/jsm_driver.c
new file mode 100644
index 000000000000..18f548449c63
--- /dev/null
+++ b/drivers/tty/serial/jsm/jsm_driver.c
@@ -0,0 +1,297 @@
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.ibm.com>
24 *
25 *
26 ***********************************************************************/
27#include <linux/moduleparam.h>
28#include <linux/pci.h>
29#include <linux/slab.h>
30
31#include "jsm.h"
32
33MODULE_AUTHOR("Digi International, http://www.digi.com");
34MODULE_DESCRIPTION("Driver for the Digi International "
35 "Neo PCI based product line");
36MODULE_LICENSE("GPL");
37MODULE_SUPPORTED_DEVICE("jsm");
38
39#define JSM_DRIVER_NAME "jsm"
40#define NR_PORTS 32
41#define JSM_MINOR_START 0
42
43struct uart_driver jsm_uart_driver = {
44 .owner = THIS_MODULE,
45 .driver_name = JSM_DRIVER_NAME,
46 .dev_name = "ttyn",
47 .major = 0,
48 .minor = JSM_MINOR_START,
49 .nr = NR_PORTS,
50};
51
52static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
53 pci_channel_state_t state);
54static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev);
55static void jsm_io_resume(struct pci_dev *pdev);
56
57static struct pci_error_handlers jsm_err_handler = {
58 .error_detected = jsm_io_error_detected,
59 .slot_reset = jsm_io_slot_reset,
60 .resume = jsm_io_resume,
61};
62
63int jsm_debug;
64module_param(jsm_debug, int, 0);
65MODULE_PARM_DESC(jsm_debug, "Driver debugging level");
66
67static int __devinit jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent)
68{
69 int rc = 0;
70 struct jsm_board *brd;
71 static int adapter_count = 0;
72
73 rc = pci_enable_device(pdev);
74 if (rc) {
75 dev_err(&pdev->dev, "Device enable FAILED\n");
76 goto out;
77 }
78
79 rc = pci_request_regions(pdev, "jsm");
80 if (rc) {
81 dev_err(&pdev->dev, "pci_request_region FAILED\n");
82 goto out_disable_device;
83 }
84
85 brd = kzalloc(sizeof(struct jsm_board), GFP_KERNEL);
86 if (!brd) {
87 dev_err(&pdev->dev,
88 "memory allocation for board structure failed\n");
89 rc = -ENOMEM;
90 goto out_release_regions;
91 }
92
93 /* store the info for the board we've found */
94 brd->boardnum = adapter_count++;
95 brd->pci_dev = pdev;
96 if (pdev->device == PCIE_DEVICE_ID_NEO_4_IBM)
97 brd->maxports = 4;
98 else if (pdev->device == PCI_DEVICE_ID_DIGI_NEO_8)
99 brd->maxports = 8;
100 else
101 brd->maxports = 2;
102
103 spin_lock_init(&brd->bd_intr_lock);
104
105 /* store which revision we have */
106 brd->rev = pdev->revision;
107
108 brd->irq = pdev->irq;
109
110 jsm_printk(INIT, INFO, &brd->pci_dev,
111 "jsm_found_board - NEO adapter\n");
112
113 /* get the PCI Base Address Registers */
114 brd->membase = pci_resource_start(pdev, 0);
115 brd->membase_end = pci_resource_end(pdev, 0);
116
117 if (brd->membase & 1)
118 brd->membase &= ~3;
119 else
120 brd->membase &= ~15;
121
122 /* Assign the board_ops struct */
123 brd->bd_ops = &jsm_neo_ops;
124
125 brd->bd_uart_offset = 0x200;
126 brd->bd_dividend = 921600;
127
128 brd->re_map_membase = ioremap(brd->membase, 0x1000);
129 if (!brd->re_map_membase) {
130 dev_err(&pdev->dev,
131 "card has no PCI Memory resources, "
132 "failing board.\n");
133 rc = -ENOMEM;
134 goto out_kfree_brd;
135 }
136
137 rc = request_irq(brd->irq, brd->bd_ops->intr,
138 IRQF_SHARED, "JSM", brd);
139 if (rc) {
140 printk(KERN_WARNING "Failed to hook IRQ %d\n",brd->irq);
141 goto out_iounmap;
142 }
143
144 rc = jsm_tty_init(brd);
145 if (rc < 0) {
146 dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
147 rc = -ENXIO;
148 goto out_free_irq;
149 }
150
151 rc = jsm_uart_port_init(brd);
152 if (rc < 0) {
153 /* XXX: leaking all resources from jsm_tty_init here! */
154 dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
155 rc = -ENXIO;
156 goto out_free_irq;
157 }
158
159 /* Log the information about the board */
160 dev_info(&pdev->dev, "board %d: Digi Neo (rev %d), irq %d\n",
161 adapter_count, brd->rev, brd->irq);
162
163 /*
164 * allocate flip buffer for board.
165 *
166 * Okay to malloc with GFP_KERNEL, we are not at interrupt
167 * context, and there are no locks held.
168 */
169 brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
170 if (!brd->flipbuf) {
171 /* XXX: leaking all resources from jsm_tty_init and
172 jsm_uart_port_init here! */
173 dev_err(&pdev->dev, "memory allocation for flipbuf failed\n");
174 rc = -ENOMEM;
175 goto out_free_uart;
176 }
177
178 pci_set_drvdata(pdev, brd);
179 pci_save_state(pdev);
180
181 return 0;
182 out_free_uart:
183 jsm_remove_uart_port(brd);
184 out_free_irq:
185 jsm_remove_uart_port(brd);
186 free_irq(brd->irq, brd);
187 out_iounmap:
188 iounmap(brd->re_map_membase);
189 out_kfree_brd:
190 kfree(brd);
191 out_release_regions:
192 pci_release_regions(pdev);
193 out_disable_device:
194 pci_disable_device(pdev);
195 out:
196 return rc;
197}
198
199static void __devexit jsm_remove_one(struct pci_dev *pdev)
200{
201 struct jsm_board *brd = pci_get_drvdata(pdev);
202 int i = 0;
203
204 jsm_remove_uart_port(brd);
205
206 free_irq(brd->irq, brd);
207 iounmap(brd->re_map_membase);
208
209 /* Free all allocated channels structs */
210 for (i = 0; i < brd->maxports; i++) {
211 if (brd->channels[i]) {
212 kfree(brd->channels[i]->ch_rqueue);
213 kfree(brd->channels[i]->ch_equeue);
214 kfree(brd->channels[i]->ch_wqueue);
215 kfree(brd->channels[i]);
216 }
217 }
218
219 pci_release_regions(pdev);
220 pci_disable_device(pdev);
221 kfree(brd->flipbuf);
222 kfree(brd);
223}
224
225static struct pci_device_id jsm_pci_tbl[] = {
226 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
227 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
228 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
229 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
230 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4_IBM), 0, 0, 4 },
231 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_NEO_8), 0, 0, 5 },
232 { 0, }
233};
234MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
235
236static struct pci_driver jsm_driver = {
237 .name = "jsm",
238 .id_table = jsm_pci_tbl,
239 .probe = jsm_probe_one,
240 .remove = __devexit_p(jsm_remove_one),
241 .err_handler = &jsm_err_handler,
242};
243
244static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
245 pci_channel_state_t state)
246{
247 struct jsm_board *brd = pci_get_drvdata(pdev);
248
249 jsm_remove_uart_port(brd);
250
251 return PCI_ERS_RESULT_NEED_RESET;
252}
253
254static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev)
255{
256 int rc;
257
258 rc = pci_enable_device(pdev);
259
260 if (rc)
261 return PCI_ERS_RESULT_DISCONNECT;
262
263 pci_set_master(pdev);
264
265 return PCI_ERS_RESULT_RECOVERED;
266}
267
268static void jsm_io_resume(struct pci_dev *pdev)
269{
270 struct jsm_board *brd = pci_get_drvdata(pdev);
271
272 pci_restore_state(pdev);
273
274 jsm_uart_port_init(brd);
275}
276
277static int __init jsm_init_module(void)
278{
279 int rc;
280
281 rc = uart_register_driver(&jsm_uart_driver);
282 if (!rc) {
283 rc = pci_register_driver(&jsm_driver);
284 if (rc)
285 uart_unregister_driver(&jsm_uart_driver);
286 }
287 return rc;
288}
289
290static void __exit jsm_exit_module(void)
291{
292 pci_unregister_driver(&jsm_driver);
293 uart_unregister_driver(&jsm_uart_driver);
294}
295
296module_init(jsm_init_module);
297module_exit(jsm_exit_module);
diff --git a/drivers/tty/serial/jsm/jsm_neo.c b/drivers/tty/serial/jsm/jsm_neo.c
new file mode 100644
index 000000000000..7960d9633c15
--- /dev/null
+++ b/drivers/tty/serial/jsm/jsm_neo.c
@@ -0,0 +1,1412 @@
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.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, efr;
52 ier = readb(&ch->ch_neo_uart->ier);
53 efr = readb(&ch->ch_neo_uart->efr);
54
55 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting CTSFLOW\n");
56
57 /* Turn on auto CTS flow control */
58 ier |= (UART_17158_IER_CTSDSR);
59 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_CTSDSR);
60
61 /* Turn off auto Xon flow control */
62 efr &= ~(UART_17158_EFR_IXON);
63
64 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
65 writeb(0, &ch->ch_neo_uart->efr);
66
67 /* Turn on UART enhanced bits */
68 writeb(efr, &ch->ch_neo_uart->efr);
69
70 /* Turn on table D, with 8 char hi/low watermarks */
71 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY), &ch->ch_neo_uart->fctr);
72
73 /* Feed the UART our trigger levels */
74 writeb(8, &ch->ch_neo_uart->tfifo);
75 ch->ch_t_tlevel = 8;
76
77 writeb(ier, &ch->ch_neo_uart->ier);
78}
79
80static void neo_set_rts_flow_control(struct jsm_channel *ch)
81{
82 u8 ier, efr;
83 ier = readb(&ch->ch_neo_uart->ier);
84 efr = readb(&ch->ch_neo_uart->efr);
85
86 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting RTSFLOW\n");
87
88 /* Turn on auto RTS flow control */
89 ier |= (UART_17158_IER_RTSDTR);
90 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_RTSDTR);
91
92 /* Turn off auto Xoff flow control */
93 ier &= ~(UART_17158_IER_XOFF);
94 efr &= ~(UART_17158_EFR_IXOFF);
95
96 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
97 writeb(0, &ch->ch_neo_uart->efr);
98
99 /* Turn on UART enhanced bits */
100 writeb(efr, &ch->ch_neo_uart->efr);
101
102 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY), &ch->ch_neo_uart->fctr);
103 ch->ch_r_watermark = 4;
104
105 writeb(56, &ch->ch_neo_uart->rfifo);
106 ch->ch_r_tlevel = 56;
107
108 writeb(ier, &ch->ch_neo_uart->ier);
109
110 /*
111 * From the Neo UART spec sheet:
112 * The auto RTS/DTR function must be started by asserting
113 * RTS/DTR# output pin (MCR bit-0 or 1 to logic 1 after
114 * it is enabled.
115 */
116 ch->ch_mostat |= (UART_MCR_RTS);
117}
118
119
120static void neo_set_ixon_flow_control(struct jsm_channel *ch)
121{
122 u8 ier, efr;
123 ier = readb(&ch->ch_neo_uart->ier);
124 efr = readb(&ch->ch_neo_uart->efr);
125
126 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting IXON FLOW\n");
127
128 /* Turn off auto CTS flow control */
129 ier &= ~(UART_17158_IER_CTSDSR);
130 efr &= ~(UART_17158_EFR_CTSDSR);
131
132 /* Turn on auto Xon flow control */
133 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXON);
134
135 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
136 writeb(0, &ch->ch_neo_uart->efr);
137
138 /* Turn on UART enhanced bits */
139 writeb(efr, &ch->ch_neo_uart->efr);
140
141 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
142 ch->ch_r_watermark = 4;
143
144 writeb(32, &ch->ch_neo_uart->rfifo);
145 ch->ch_r_tlevel = 32;
146
147 /* Tell UART what start/stop chars it should be looking for */
148 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
149 writeb(0, &ch->ch_neo_uart->xonchar2);
150
151 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
152 writeb(0, &ch->ch_neo_uart->xoffchar2);
153
154 writeb(ier, &ch->ch_neo_uart->ier);
155}
156
157static void neo_set_ixoff_flow_control(struct jsm_channel *ch)
158{
159 u8 ier, efr;
160 ier = readb(&ch->ch_neo_uart->ier);
161 efr = readb(&ch->ch_neo_uart->efr);
162
163 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting IXOFF FLOW\n");
164
165 /* Turn off auto RTS flow control */
166 ier &= ~(UART_17158_IER_RTSDTR);
167 efr &= ~(UART_17158_EFR_RTSDTR);
168
169 /* Turn on auto Xoff flow control */
170 ier |= (UART_17158_IER_XOFF);
171 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
172
173 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
174 writeb(0, &ch->ch_neo_uart->efr);
175
176 /* Turn on UART enhanced bits */
177 writeb(efr, &ch->ch_neo_uart->efr);
178
179 /* Turn on table D, with 8 char hi/low watermarks */
180 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
181
182 writeb(8, &ch->ch_neo_uart->tfifo);
183 ch->ch_t_tlevel = 8;
184
185 /* Tell UART what start/stop chars it should be looking for */
186 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
187 writeb(0, &ch->ch_neo_uart->xonchar2);
188
189 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
190 writeb(0, &ch->ch_neo_uart->xoffchar2);
191
192 writeb(ier, &ch->ch_neo_uart->ier);
193}
194
195static void neo_set_no_input_flow_control(struct jsm_channel *ch)
196{
197 u8 ier, efr;
198 ier = readb(&ch->ch_neo_uart->ier);
199 efr = readb(&ch->ch_neo_uart->efr);
200
201 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Unsetting Input FLOW\n");
202
203 /* Turn off auto RTS flow control */
204 ier &= ~(UART_17158_IER_RTSDTR);
205 efr &= ~(UART_17158_EFR_RTSDTR);
206
207 /* Turn off auto Xoff flow control */
208 ier &= ~(UART_17158_IER_XOFF);
209 if (ch->ch_c_iflag & IXON)
210 efr &= ~(UART_17158_EFR_IXOFF);
211 else
212 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
213
214 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
215 writeb(0, &ch->ch_neo_uart->efr);
216
217 /* Turn on UART enhanced bits */
218 writeb(efr, &ch->ch_neo_uart->efr);
219
220 /* Turn on table D, with 8 char hi/low watermarks */
221 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
222
223 ch->ch_r_watermark = 0;
224
225 writeb(16, &ch->ch_neo_uart->tfifo);
226 ch->ch_t_tlevel = 16;
227
228 writeb(16, &ch->ch_neo_uart->rfifo);
229 ch->ch_r_tlevel = 16;
230
231 writeb(ier, &ch->ch_neo_uart->ier);
232}
233
234static void neo_set_no_output_flow_control(struct jsm_channel *ch)
235{
236 u8 ier, efr;
237 ier = readb(&ch->ch_neo_uart->ier);
238 efr = readb(&ch->ch_neo_uart->efr);
239
240 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Unsetting Output FLOW\n");
241
242 /* Turn off auto CTS flow control */
243 ier &= ~(UART_17158_IER_CTSDSR);
244 efr &= ~(UART_17158_EFR_CTSDSR);
245
246 /* Turn off auto Xon flow control */
247 if (ch->ch_c_iflag & IXOFF)
248 efr &= ~(UART_17158_EFR_IXON);
249 else
250 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXON);
251
252 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
253 writeb(0, &ch->ch_neo_uart->efr);
254
255 /* Turn on UART enhanced bits */
256 writeb(efr, &ch->ch_neo_uart->efr);
257
258 /* Turn on table D, with 8 char hi/low watermarks */
259 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
260
261 ch->ch_r_watermark = 0;
262
263 writeb(16, &ch->ch_neo_uart->tfifo);
264 ch->ch_t_tlevel = 16;
265
266 writeb(16, &ch->ch_neo_uart->rfifo);
267 ch->ch_r_tlevel = 16;
268
269 writeb(ier, &ch->ch_neo_uart->ier);
270}
271
272static inline void neo_set_new_start_stop_chars(struct jsm_channel *ch)
273{
274
275 /* if hardware flow control is set, then skip this whole thing */
276 if (ch->ch_c_cflag & CRTSCTS)
277 return;
278
279 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "start\n");
280
281 /* Tell UART what start/stop chars it should be looking for */
282 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
283 writeb(0, &ch->ch_neo_uart->xonchar2);
284
285 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
286 writeb(0, &ch->ch_neo_uart->xoffchar2);
287}
288
289static void neo_copy_data_from_uart_to_queue(struct jsm_channel *ch)
290{
291 int qleft = 0;
292 u8 linestatus = 0;
293 u8 error_mask = 0;
294 int n = 0;
295 int total = 0;
296 u16 head;
297 u16 tail;
298
299 if (!ch)
300 return;
301
302 /* cache head and tail of queue */
303 head = ch->ch_r_head & RQUEUEMASK;
304 tail = ch->ch_r_tail & RQUEUEMASK;
305
306 /* Get our cached LSR */
307 linestatus = ch->ch_cached_lsr;
308 ch->ch_cached_lsr = 0;
309
310 /* Store how much space we have left in the queue */
311 if ((qleft = tail - head - 1) < 0)
312 qleft += RQUEUEMASK + 1;
313
314 /*
315 * If the UART is not in FIFO mode, force the FIFO copy to
316 * NOT be run, by setting total to 0.
317 *
318 * On the other hand, if the UART IS in FIFO mode, then ask
319 * the UART to give us an approximation of data it has RX'ed.
320 */
321 if (!(ch->ch_flags & CH_FIFO_ENABLED))
322 total = 0;
323 else {
324 total = readb(&ch->ch_neo_uart->rfifo);
325
326 /*
327 * EXAR chip bug - RX FIFO COUNT - Fudge factor.
328 *
329 * This resolves a problem/bug with the Exar chip that sometimes
330 * returns a bogus value in the rfifo register.
331 * The count can be any where from 0-3 bytes "off".
332 * Bizarre, but true.
333 */
334 total -= 3;
335 }
336
337 /*
338 * Finally, bound the copy to make sure we don't overflow
339 * our own queue...
340 * The byte by byte copy loop below this loop this will
341 * deal with the queue overflow possibility.
342 */
343 total = min(total, qleft);
344
345 while (total > 0) {
346 /*
347 * Grab the linestatus register, we need to check
348 * to see if there are any errors in the FIFO.
349 */
350 linestatus = readb(&ch->ch_neo_uart->lsr);
351
352 /*
353 * Break out if there is a FIFO error somewhere.
354 * This will allow us to go byte by byte down below,
355 * finding the exact location of the error.
356 */
357 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
358 break;
359
360 /* Make sure we don't go over the end of our queue */
361 n = min(((u32) total), (RQUEUESIZE - (u32) head));
362
363 /*
364 * Cut down n even further if needed, this is to fix
365 * a problem with memcpy_fromio() with the Neo on the
366 * IBM pSeries platform.
367 * 15 bytes max appears to be the magic number.
368 */
369 n = min((u32) n, (u32) 12);
370
371 /*
372 * Since we are grabbing the linestatus register, which
373 * will reset some bits after our read, we need to ensure
374 * we don't miss our TX FIFO emptys.
375 */
376 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR))
377 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
378
379 linestatus = 0;
380
381 /* Copy data from uart to the queue */
382 memcpy_fromio(ch->ch_rqueue + head, &ch->ch_neo_uart->txrxburst, n);
383 /*
384 * Since RX_FIFO_DATA_ERROR was 0, we are guarenteed
385 * that all the data currently in the FIFO is free of
386 * breaks and parity/frame/orun errors.
387 */
388 memset(ch->ch_equeue + head, 0, n);
389
390 /* Add to and flip head if needed */
391 head = (head + n) & RQUEUEMASK;
392 total -= n;
393 qleft -= n;
394 ch->ch_rxcount += n;
395 }
396
397 /*
398 * Create a mask to determine whether we should
399 * insert the character (if any) into our queue.
400 */
401 if (ch->ch_c_iflag & IGNBRK)
402 error_mask |= UART_LSR_BI;
403
404 /*
405 * Now cleanup any leftover bytes still in the UART.
406 * Also deal with any possible queue overflow here as well.
407 */
408 while (1) {
409
410 /*
411 * Its possible we have a linestatus from the loop above
412 * this, so we "OR" on any extra bits.
413 */
414 linestatus |= readb(&ch->ch_neo_uart->lsr);
415
416 /*
417 * If the chip tells us there is no more data pending to
418 * be read, we can then leave.
419 * But before we do, cache the linestatus, just in case.
420 */
421 if (!(linestatus & UART_LSR_DR)) {
422 ch->ch_cached_lsr = linestatus;
423 break;
424 }
425
426 /* No need to store this bit */
427 linestatus &= ~UART_LSR_DR;
428
429 /*
430 * Since we are grabbing the linestatus register, which
431 * will reset some bits after our read, we need to ensure
432 * we don't miss our TX FIFO emptys.
433 */
434 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR)) {
435 linestatus &= ~(UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR);
436 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
437 }
438
439 /*
440 * Discard character if we are ignoring the error mask.
441 */
442 if (linestatus & error_mask) {
443 u8 discard;
444 linestatus = 0;
445 memcpy_fromio(&discard, &ch->ch_neo_uart->txrxburst, 1);
446 continue;
447 }
448
449 /*
450 * If our queue is full, we have no choice but to drop some data.
451 * The assumption is that HWFLOW or SWFLOW should have stopped
452 * things way way before we got to this point.
453 *
454 * I decided that I wanted to ditch the oldest data first,
455 * I hope thats okay with everyone? Yes? Good.
456 */
457 while (qleft < 1) {
458 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
459 "Queue full, dropping DATA:%x LSR:%x\n",
460 ch->ch_rqueue[tail], ch->ch_equeue[tail]);
461
462 ch->ch_r_tail = tail = (tail + 1) & RQUEUEMASK;
463 ch->ch_err_overrun++;
464 qleft++;
465 }
466
467 memcpy_fromio(ch->ch_rqueue + head, &ch->ch_neo_uart->txrxburst, 1);
468 ch->ch_equeue[head] = (u8) linestatus;
469
470 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
471 "DATA/LSR pair: %x %x\n", ch->ch_rqueue[head], ch->ch_equeue[head]);
472
473 /* Ditch any remaining linestatus value. */
474 linestatus = 0;
475
476 /* Add to and flip head if needed */
477 head = (head + 1) & RQUEUEMASK;
478
479 qleft--;
480 ch->ch_rxcount++;
481 }
482
483 /*
484 * Write new final heads to channel structure.
485 */
486 ch->ch_r_head = head & RQUEUEMASK;
487 ch->ch_e_head = head & EQUEUEMASK;
488 jsm_input(ch);
489}
490
491static void neo_copy_data_from_queue_to_uart(struct jsm_channel *ch)
492{
493 u16 head;
494 u16 tail;
495 int n;
496 int s;
497 int qlen;
498 u32 len_written = 0;
499
500 if (!ch)
501 return;
502
503 /* No data to write to the UART */
504 if (ch->ch_w_tail == ch->ch_w_head)
505 return;
506
507 /* If port is "stopped", don't send any data to the UART */
508 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_BREAK_SENDING))
509 return;
510 /*
511 * If FIFOs are disabled. Send data directly to txrx register
512 */
513 if (!(ch->ch_flags & CH_FIFO_ENABLED)) {
514 u8 lsrbits = readb(&ch->ch_neo_uart->lsr);
515
516 ch->ch_cached_lsr |= lsrbits;
517 if (ch->ch_cached_lsr & UART_LSR_THRE) {
518 ch->ch_cached_lsr &= ~(UART_LSR_THRE);
519
520 writeb(ch->ch_wqueue[ch->ch_w_tail], &ch->ch_neo_uart->txrx);
521 jsm_printk(WRITE, INFO, &ch->ch_bd->pci_dev,
522 "Tx data: %x\n", ch->ch_wqueue[ch->ch_w_head]);
523 ch->ch_w_tail++;
524 ch->ch_w_tail &= WQUEUEMASK;
525 ch->ch_txcount++;
526 }
527 return;
528 }
529
530 /*
531 * We have to do it this way, because of the EXAR TXFIFO count bug.
532 */
533 if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM)))
534 return;
535
536 n = UART_17158_TX_FIFOSIZE - ch->ch_t_tlevel;
537
538 /* cache head and tail of queue */
539 head = ch->ch_w_head & WQUEUEMASK;
540 tail = ch->ch_w_tail & WQUEUEMASK;
541 qlen = (head - tail) & WQUEUEMASK;
542
543 /* Find minimum of the FIFO space, versus queue length */
544 n = min(n, qlen);
545
546 while (n > 0) {
547
548 s = ((head >= tail) ? head : WQUEUESIZE) - tail;
549 s = min(s, n);
550
551 if (s <= 0)
552 break;
553
554 memcpy_toio(&ch->ch_neo_uart->txrxburst, ch->ch_wqueue + tail, s);
555 /* Add and flip queue if needed */
556 tail = (tail + s) & WQUEUEMASK;
557 n -= s;
558 ch->ch_txcount += s;
559 len_written += s;
560 }
561
562 /* Update the final tail */
563 ch->ch_w_tail = tail & WQUEUEMASK;
564
565 if (len_written >= ch->ch_t_tlevel)
566 ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
567
568 if (!jsm_tty_write(&ch->uart_port))
569 uart_write_wakeup(&ch->uart_port);
570}
571
572static void neo_parse_modem(struct jsm_channel *ch, u8 signals)
573{
574 u8 msignals = signals;
575
576 jsm_printk(MSIGS, INFO, &ch->ch_bd->pci_dev,
577 "neo_parse_modem: port: %d msignals: %x\n", ch->ch_portnum, msignals);
578
579 /* Scrub off lower bits. They signify delta's, which I don't care about */
580 /* Keep DDCD and DDSR though */
581 msignals &= 0xf8;
582
583 if (msignals & UART_MSR_DDCD)
584 uart_handle_dcd_change(&ch->uart_port, msignals & UART_MSR_DCD);
585 if (msignals & UART_MSR_DDSR)
586 uart_handle_cts_change(&ch->uart_port, msignals & UART_MSR_CTS);
587 if (msignals & UART_MSR_DCD)
588 ch->ch_mistat |= UART_MSR_DCD;
589 else
590 ch->ch_mistat &= ~UART_MSR_DCD;
591
592 if (msignals & UART_MSR_DSR)
593 ch->ch_mistat |= UART_MSR_DSR;
594 else
595 ch->ch_mistat &= ~UART_MSR_DSR;
596
597 if (msignals & UART_MSR_RI)
598 ch->ch_mistat |= UART_MSR_RI;
599 else
600 ch->ch_mistat &= ~UART_MSR_RI;
601
602 if (msignals & UART_MSR_CTS)
603 ch->ch_mistat |= UART_MSR_CTS;
604 else
605 ch->ch_mistat &= ~UART_MSR_CTS;
606
607 jsm_printk(MSIGS, INFO, &ch->ch_bd->pci_dev,
608 "Port: %d DTR: %d RTS: %d CTS: %d DSR: %d " "RI: %d CD: %d\n",
609 ch->ch_portnum,
610 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_DTR),
611 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_RTS),
612 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_CTS),
613 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DSR),
614 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_RI),
615 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DCD));
616}
617
618/* Make the UART raise any of the output signals we want up */
619static void neo_assert_modem_signals(struct jsm_channel *ch)
620{
621 if (!ch)
622 return;
623
624 writeb(ch->ch_mostat, &ch->ch_neo_uart->mcr);
625
626 /* flush write operation */
627 neo_pci_posting_flush(ch->ch_bd);
628}
629
630/*
631 * Flush the WRITE FIFO on the Neo.
632 *
633 * NOTE: Channel lock MUST be held before calling this function!
634 */
635static void neo_flush_uart_write(struct jsm_channel *ch)
636{
637 u8 tmp = 0;
638 int i = 0;
639
640 if (!ch)
641 return;
642
643 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT), &ch->ch_neo_uart->isr_fcr);
644
645 for (i = 0; i < 10; i++) {
646
647 /* Check to see if the UART feels it completely flushed the FIFO. */
648 tmp = readb(&ch->ch_neo_uart->isr_fcr);
649 if (tmp & 4) {
650 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
651 "Still flushing TX UART... i: %d\n", i);
652 udelay(10);
653 }
654 else
655 break;
656 }
657
658 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
659}
660
661
662/*
663 * Flush the READ FIFO on the Neo.
664 *
665 * NOTE: Channel lock MUST be held before calling this function!
666 */
667static void neo_flush_uart_read(struct jsm_channel *ch)
668{
669 u8 tmp = 0;
670 int i = 0;
671
672 if (!ch)
673 return;
674
675 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR), &ch->ch_neo_uart->isr_fcr);
676
677 for (i = 0; i < 10; i++) {
678
679 /* Check to see if the UART feels it completely flushed the FIFO. */
680 tmp = readb(&ch->ch_neo_uart->isr_fcr);
681 if (tmp & 2) {
682 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
683 "Still flushing RX UART... i: %d\n", i);
684 udelay(10);
685 }
686 else
687 break;
688 }
689}
690
691/*
692 * No locks are assumed to be held when calling this function.
693 */
694static void neo_clear_break(struct jsm_channel *ch, int force)
695{
696 unsigned long lock_flags;
697
698 spin_lock_irqsave(&ch->ch_lock, lock_flags);
699
700 /* Turn break off, and unset some variables */
701 if (ch->ch_flags & CH_BREAK_SENDING) {
702 u8 temp = readb(&ch->ch_neo_uart->lcr);
703 writeb((temp & ~UART_LCR_SBC), &ch->ch_neo_uart->lcr);
704
705 ch->ch_flags &= ~(CH_BREAK_SENDING);
706 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
707 "clear break Finishing UART_LCR_SBC! finished: %lx\n", jiffies);
708
709 /* flush write operation */
710 neo_pci_posting_flush(ch->ch_bd);
711 }
712 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
713}
714
715/*
716 * Parse the ISR register.
717 */
718static inline void neo_parse_isr(struct jsm_board *brd, u32 port)
719{
720 struct jsm_channel *ch;
721 u8 isr;
722 u8 cause;
723 unsigned long lock_flags;
724
725 if (!brd)
726 return;
727
728 if (port > brd->maxports)
729 return;
730
731 ch = brd->channels[port];
732 if (!ch)
733 return;
734
735 /* Here we try to figure out what caused the interrupt to happen */
736 while (1) {
737
738 isr = readb(&ch->ch_neo_uart->isr_fcr);
739
740 /* Bail if no pending interrupt */
741 if (isr & UART_IIR_NO_INT)
742 break;
743
744 /*
745 * Yank off the upper 2 bits, which just show that the FIFO's are enabled.
746 */
747 isr &= ~(UART_17158_IIR_FIFO_ENABLED);
748
749 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
750 "%s:%d isr: %x\n", __FILE__, __LINE__, isr);
751
752 if (isr & (UART_17158_IIR_RDI_TIMEOUT | UART_IIR_RDI)) {
753 /* Read data from uart -> queue */
754 neo_copy_data_from_uart_to_queue(ch);
755
756 /* Call our tty layer to enforce queue flow control if needed. */
757 spin_lock_irqsave(&ch->ch_lock, lock_flags);
758 jsm_check_queue_flow_control(ch);
759 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
760 }
761
762 if (isr & UART_IIR_THRI) {
763 /* Transfer data (if any) from Write Queue -> UART. */
764 spin_lock_irqsave(&ch->ch_lock, lock_flags);
765 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
766 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
767 neo_copy_data_from_queue_to_uart(ch);
768 }
769
770 if (isr & UART_17158_IIR_XONXOFF) {
771 cause = readb(&ch->ch_neo_uart->xoffchar1);
772
773 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
774 "Port %d. Got ISR_XONXOFF: cause:%x\n", port, cause);
775
776 /*
777 * Since the UART detected either an XON or
778 * XOFF match, we need to figure out which
779 * one it was, so we can suspend or resume data flow.
780 */
781 spin_lock_irqsave(&ch->ch_lock, lock_flags);
782 if (cause == UART_17158_XON_DETECT) {
783 /* Is output stopped right now, if so, resume it */
784 if (brd->channels[port]->ch_flags & CH_STOP) {
785 ch->ch_flags &= ~(CH_STOP);
786 }
787 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
788 "Port %d. XON detected in incoming data\n", port);
789 }
790 else if (cause == UART_17158_XOFF_DETECT) {
791 if (!(brd->channels[port]->ch_flags & CH_STOP)) {
792 ch->ch_flags |= CH_STOP;
793 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
794 "Setting CH_STOP\n");
795 }
796 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
797 "Port: %d. XOFF detected in incoming data\n", port);
798 }
799 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
800 }
801
802 if (isr & UART_17158_IIR_HWFLOW_STATE_CHANGE) {
803 /*
804 * If we get here, this means the hardware is doing auto flow control.
805 * Check to see whether RTS/DTR or CTS/DSR caused this interrupt.
806 */
807 cause = readb(&ch->ch_neo_uart->mcr);
808
809 /* Which pin is doing auto flow? RTS or DTR? */
810 spin_lock_irqsave(&ch->ch_lock, lock_flags);
811 if ((cause & 0x4) == 0) {
812 if (cause & UART_MCR_RTS)
813 ch->ch_mostat |= UART_MCR_RTS;
814 else
815 ch->ch_mostat &= ~(UART_MCR_RTS);
816 } else {
817 if (cause & UART_MCR_DTR)
818 ch->ch_mostat |= UART_MCR_DTR;
819 else
820 ch->ch_mostat &= ~(UART_MCR_DTR);
821 }
822 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
823 }
824
825 /* Parse any modem signal changes */
826 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
827 "MOD_STAT: sending to parse_modem_sigs\n");
828 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
829 }
830}
831
832static inline void neo_parse_lsr(struct jsm_board *brd, u32 port)
833{
834 struct jsm_channel *ch;
835 int linestatus;
836 unsigned long lock_flags;
837
838 if (!brd)
839 return;
840
841 if (port > brd->maxports)
842 return;
843
844 ch = brd->channels[port];
845 if (!ch)
846 return;
847
848 linestatus = readb(&ch->ch_neo_uart->lsr);
849
850 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
851 "%s:%d port: %d linestatus: %x\n", __FILE__, __LINE__, port, linestatus);
852
853 ch->ch_cached_lsr |= linestatus;
854
855 if (ch->ch_cached_lsr & UART_LSR_DR) {
856 /* Read data from uart -> queue */
857 neo_copy_data_from_uart_to_queue(ch);
858 spin_lock_irqsave(&ch->ch_lock, lock_flags);
859 jsm_check_queue_flow_control(ch);
860 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
861 }
862
863 /*
864 * This is a special flag. It indicates that at least 1
865 * RX error (parity, framing, or break) has happened.
866 * Mark this in our struct, which will tell me that I have
867 *to do the special RX+LSR read for this FIFO load.
868 */
869 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
870 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
871 "%s:%d Port: %d Got an RX error, need to parse LSR\n",
872 __FILE__, __LINE__, port);
873
874 /*
875 * The next 3 tests should *NOT* happen, as the above test
876 * should encapsulate all 3... At least, thats what Exar says.
877 */
878
879 if (linestatus & UART_LSR_PE) {
880 ch->ch_err_parity++;
881 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
882 "%s:%d Port: %d. PAR ERR!\n", __FILE__, __LINE__, port);
883 }
884
885 if (linestatus & UART_LSR_FE) {
886 ch->ch_err_frame++;
887 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
888 "%s:%d Port: %d. FRM ERR!\n", __FILE__, __LINE__, port);
889 }
890
891 if (linestatus & UART_LSR_BI) {
892 ch->ch_err_break++;
893 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
894 "%s:%d Port: %d. BRK INTR!\n", __FILE__, __LINE__, port);
895 }
896
897 if (linestatus & UART_LSR_OE) {
898 /*
899 * Rx Oruns. Exar says that an orun will NOT corrupt
900 * the FIFO. It will just replace the holding register
901 * with this new data byte. So basically just ignore this.
902 * Probably we should eventually have an orun stat in our driver...
903 */
904 ch->ch_err_overrun++;
905 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
906 "%s:%d Port: %d. Rx Overrun!\n", __FILE__, __LINE__, port);
907 }
908
909 if (linestatus & UART_LSR_THRE) {
910 spin_lock_irqsave(&ch->ch_lock, lock_flags);
911 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
912 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
913
914 /* Transfer data (if any) from Write Queue -> UART. */
915 neo_copy_data_from_queue_to_uart(ch);
916 }
917 else if (linestatus & UART_17158_TX_AND_FIFO_CLR) {
918 spin_lock_irqsave(&ch->ch_lock, lock_flags);
919 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
920 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
921
922 /* Transfer data (if any) from Write Queue -> UART. */
923 neo_copy_data_from_queue_to_uart(ch);
924 }
925}
926
927/*
928 * neo_param()
929 * Send any/all changes to the line to the UART.
930 */
931static void neo_param(struct jsm_channel *ch)
932{
933 u8 lcr = 0;
934 u8 uart_lcr, ier;
935 u32 baud;
936 int quot;
937 struct jsm_board *bd;
938
939 bd = ch->ch_bd;
940 if (!bd)
941 return;
942
943 /*
944 * If baud rate is zero, flush queues, and set mval to drop DTR.
945 */
946 if ((ch->ch_c_cflag & (CBAUD)) == 0) {
947 ch->ch_r_head = ch->ch_r_tail = 0;
948 ch->ch_e_head = ch->ch_e_tail = 0;
949 ch->ch_w_head = ch->ch_w_tail = 0;
950
951 neo_flush_uart_write(ch);
952 neo_flush_uart_read(ch);
953
954 ch->ch_flags |= (CH_BAUD0);
955 ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
956 neo_assert_modem_signals(ch);
957 return;
958
959 } else {
960 int i;
961 unsigned int cflag;
962 static struct {
963 unsigned int rate;
964 unsigned int cflag;
965 } baud_rates[] = {
966 { 921600, B921600 },
967 { 460800, B460800 },
968 { 230400, B230400 },
969 { 115200, B115200 },
970 { 57600, B57600 },
971 { 38400, B38400 },
972 { 19200, B19200 },
973 { 9600, B9600 },
974 { 4800, B4800 },
975 { 2400, B2400 },
976 { 1200, B1200 },
977 { 600, B600 },
978 { 300, B300 },
979 { 200, B200 },
980 { 150, B150 },
981 { 134, B134 },
982 { 110, B110 },
983 { 75, B75 },
984 { 50, B50 },
985 };
986
987 cflag = C_BAUD(ch->uart_port.state->port.tty);
988 baud = 9600;
989 for (i = 0; i < ARRAY_SIZE(baud_rates); i++) {
990 if (baud_rates[i].cflag == cflag) {
991 baud = baud_rates[i].rate;
992 break;
993 }
994 }
995
996 if (ch->ch_flags & CH_BAUD0)
997 ch->ch_flags &= ~(CH_BAUD0);
998 }
999
1000 if (ch->ch_c_cflag & PARENB)
1001 lcr |= UART_LCR_PARITY;
1002
1003 if (!(ch->ch_c_cflag & PARODD))
1004 lcr |= UART_LCR_EPAR;
1005
1006 /*
1007 * Not all platforms support mark/space parity,
1008 * so this will hide behind an ifdef.
1009 */
1010#ifdef CMSPAR
1011 if (ch->ch_c_cflag & CMSPAR)
1012 lcr |= UART_LCR_SPAR;
1013#endif
1014
1015 if (ch->ch_c_cflag & CSTOPB)
1016 lcr |= UART_LCR_STOP;
1017
1018 switch (ch->ch_c_cflag & CSIZE) {
1019 case CS5:
1020 lcr |= UART_LCR_WLEN5;
1021 break;
1022 case CS6:
1023 lcr |= UART_LCR_WLEN6;
1024 break;
1025 case CS7:
1026 lcr |= UART_LCR_WLEN7;
1027 break;
1028 case CS8:
1029 default:
1030 lcr |= UART_LCR_WLEN8;
1031 break;
1032 }
1033
1034 ier = readb(&ch->ch_neo_uart->ier);
1035 uart_lcr = readb(&ch->ch_neo_uart->lcr);
1036
1037 if (baud == 0)
1038 baud = 9600;
1039
1040 quot = ch->ch_bd->bd_dividend / baud;
1041
1042 if (quot != 0) {
1043 writeb(UART_LCR_DLAB, &ch->ch_neo_uart->lcr);
1044 writeb((quot & 0xff), &ch->ch_neo_uart->txrx);
1045 writeb((quot >> 8), &ch->ch_neo_uart->ier);
1046 writeb(lcr, &ch->ch_neo_uart->lcr);
1047 }
1048
1049 if (uart_lcr != lcr)
1050 writeb(lcr, &ch->ch_neo_uart->lcr);
1051
1052 if (ch->ch_c_cflag & CREAD)
1053 ier |= (UART_IER_RDI | UART_IER_RLSI);
1054
1055 ier |= (UART_IER_THRI | UART_IER_MSI);
1056
1057 writeb(ier, &ch->ch_neo_uart->ier);
1058
1059 /* Set new start/stop chars */
1060 neo_set_new_start_stop_chars(ch);
1061
1062 if (ch->ch_c_cflag & CRTSCTS)
1063 neo_set_cts_flow_control(ch);
1064 else if (ch->ch_c_iflag & IXON) {
1065 /* If start/stop is set to disable, then we should disable flow control */
1066 if ((ch->ch_startc == __DISABLED_CHAR) || (ch->ch_stopc == __DISABLED_CHAR))
1067 neo_set_no_output_flow_control(ch);
1068 else
1069 neo_set_ixon_flow_control(ch);
1070 }
1071 else
1072 neo_set_no_output_flow_control(ch);
1073
1074 if (ch->ch_c_cflag & CRTSCTS)
1075 neo_set_rts_flow_control(ch);
1076 else if (ch->ch_c_iflag & IXOFF) {
1077 /* If start/stop is set to disable, then we should disable flow control */
1078 if ((ch->ch_startc == __DISABLED_CHAR) || (ch->ch_stopc == __DISABLED_CHAR))
1079 neo_set_no_input_flow_control(ch);
1080 else
1081 neo_set_ixoff_flow_control(ch);
1082 }
1083 else
1084 neo_set_no_input_flow_control(ch);
1085 /*
1086 * Adjust the RX FIFO Trigger level if baud is less than 9600.
1087 * Not exactly elegant, but this is needed because of the Exar chip's
1088 * delay on firing off the RX FIFO interrupt on slower baud rates.
1089 */
1090 if (baud < 9600) {
1091 writeb(1, &ch->ch_neo_uart->rfifo);
1092 ch->ch_r_tlevel = 1;
1093 }
1094
1095 neo_assert_modem_signals(ch);
1096
1097 /* Get current status of the modem signals now */
1098 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
1099 return;
1100}
1101
1102/*
1103 * jsm_neo_intr()
1104 *
1105 * Neo specific interrupt handler.
1106 */
1107static irqreturn_t neo_intr(int irq, void *voidbrd)
1108{
1109 struct jsm_board *brd = voidbrd;
1110 struct jsm_channel *ch;
1111 int port = 0;
1112 int type = 0;
1113 int current_port;
1114 u32 tmp;
1115 u32 uart_poll;
1116 unsigned long lock_flags;
1117 unsigned long lock_flags2;
1118 int outofloop_count = 0;
1119
1120 /* Lock out the slow poller from running on this board. */
1121 spin_lock_irqsave(&brd->bd_intr_lock, lock_flags);
1122
1123 /*
1124 * Read in "extended" IRQ information from the 32bit Neo register.
1125 * Bits 0-7: What port triggered the interrupt.
1126 * Bits 8-31: Each 3bits indicate what type of interrupt occurred.
1127 */
1128 uart_poll = readl(brd->re_map_membase + UART_17158_POLL_ADDR_OFFSET);
1129
1130 jsm_printk(INTR, INFO, &brd->pci_dev,
1131 "%s:%d uart_poll: %x\n", __FILE__, __LINE__, uart_poll);
1132
1133 if (!uart_poll) {
1134 jsm_printk(INTR, INFO, &brd->pci_dev,
1135 "Kernel interrupted to me, but no pending interrupts...\n");
1136 spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
1137 return IRQ_NONE;
1138 }
1139
1140 /* At this point, we have at least SOMETHING to service, dig further... */
1141
1142 current_port = 0;
1143
1144 /* Loop on each port */
1145 while (((uart_poll & 0xff) != 0) && (outofloop_count < 0xff)){
1146
1147 tmp = uart_poll;
1148 outofloop_count++;
1149
1150 /* Check current port to see if it has interrupt pending */
1151 if ((tmp & jsm_offset_table[current_port]) != 0) {
1152 port = current_port;
1153 type = tmp >> (8 + (port * 3));
1154 type &= 0x7;
1155 } else {
1156 current_port++;
1157 continue;
1158 }
1159
1160 jsm_printk(INTR, INFO, &brd->pci_dev,
1161 "%s:%d port: %x type: %x\n", __FILE__, __LINE__, port, type);
1162
1163 /* Remove this port + type from uart_poll */
1164 uart_poll &= ~(jsm_offset_table[port]);
1165
1166 if (!type) {
1167 /* If no type, just ignore it, and move onto next port */
1168 jsm_printk(INTR, ERR, &brd->pci_dev,
1169 "Interrupt with no type! port: %d\n", port);
1170 continue;
1171 }
1172
1173 /* Switch on type of interrupt we have */
1174 switch (type) {
1175
1176 case UART_17158_RXRDY_TIMEOUT:
1177 /*
1178 * RXRDY Time-out is cleared by reading data in the
1179 * RX FIFO until it falls below the trigger level.
1180 */
1181
1182 /* Verify the port is in range. */
1183 if (port > brd->nasync)
1184 continue;
1185
1186 ch = brd->channels[port];
1187 neo_copy_data_from_uart_to_queue(ch);
1188
1189 /* Call our tty layer to enforce queue flow control if needed. */
1190 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1191 jsm_check_queue_flow_control(ch);
1192 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1193
1194 continue;
1195
1196 case UART_17158_RX_LINE_STATUS:
1197 /*
1198 * RXRDY and RX LINE Status (logic OR of LSR[4:1])
1199 */
1200 neo_parse_lsr(brd, port);
1201 continue;
1202
1203 case UART_17158_TXRDY:
1204 /*
1205 * TXRDY interrupt clears after reading ISR register for the UART channel.
1206 */
1207
1208 /*
1209 * Yes, this is odd...
1210 * Why would I check EVERY possibility of type of
1211 * interrupt, when we know its TXRDY???
1212 * Becuz for some reason, even tho we got triggered for TXRDY,
1213 * it seems to be occassionally wrong. Instead of TX, which
1214 * it should be, I was getting things like RXDY too. Weird.
1215 */
1216 neo_parse_isr(brd, port);
1217 continue;
1218
1219 case UART_17158_MSR:
1220 /*
1221 * MSR or flow control was seen.
1222 */
1223 neo_parse_isr(brd, port);
1224 continue;
1225
1226 default:
1227 /*
1228 * The UART triggered us with a bogus interrupt type.
1229 * It appears the Exar chip, when REALLY bogged down, will throw
1230 * these once and awhile.
1231 * Its harmless, just ignore it and move on.
1232 */
1233 jsm_printk(INTR, ERR, &brd->pci_dev,
1234 "%s:%d Unknown Interrupt type: %x\n", __FILE__, __LINE__, type);
1235 continue;
1236 }
1237 }
1238
1239 spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
1240
1241 jsm_printk(INTR, INFO, &brd->pci_dev, "finish.\n");
1242 return IRQ_HANDLED;
1243}
1244
1245/*
1246 * Neo specific way of turning off the receiver.
1247 * Used as a way to enforce queue flow control when in
1248 * hardware flow control mode.
1249 */
1250static void neo_disable_receiver(struct jsm_channel *ch)
1251{
1252 u8 tmp = readb(&ch->ch_neo_uart->ier);
1253 tmp &= ~(UART_IER_RDI);
1254 writeb(tmp, &ch->ch_neo_uart->ier);
1255
1256 /* flush write operation */
1257 neo_pci_posting_flush(ch->ch_bd);
1258}
1259
1260
1261/*
1262 * Neo specific way of turning on the receiver.
1263 * Used as a way to un-enforce queue flow control when in
1264 * hardware flow control mode.
1265 */
1266static void neo_enable_receiver(struct jsm_channel *ch)
1267{
1268 u8 tmp = readb(&ch->ch_neo_uart->ier);
1269 tmp |= (UART_IER_RDI);
1270 writeb(tmp, &ch->ch_neo_uart->ier);
1271
1272 /* flush write operation */
1273 neo_pci_posting_flush(ch->ch_bd);
1274}
1275
1276static void neo_send_start_character(struct jsm_channel *ch)
1277{
1278 if (!ch)
1279 return;
1280
1281 if (ch->ch_startc != __DISABLED_CHAR) {
1282 ch->ch_xon_sends++;
1283 writeb(ch->ch_startc, &ch->ch_neo_uart->txrx);
1284
1285 /* flush write operation */
1286 neo_pci_posting_flush(ch->ch_bd);
1287 }
1288}
1289
1290static void neo_send_stop_character(struct jsm_channel *ch)
1291{
1292 if (!ch)
1293 return;
1294
1295 if (ch->ch_stopc != __DISABLED_CHAR) {
1296 ch->ch_xoff_sends++;
1297 writeb(ch->ch_stopc, &ch->ch_neo_uart->txrx);
1298
1299 /* flush write operation */
1300 neo_pci_posting_flush(ch->ch_bd);
1301 }
1302}
1303
1304/*
1305 * neo_uart_init
1306 */
1307static void neo_uart_init(struct jsm_channel *ch)
1308{
1309 writeb(0, &ch->ch_neo_uart->ier);
1310 writeb(0, &ch->ch_neo_uart->efr);
1311 writeb(UART_EFR_ECB, &ch->ch_neo_uart->efr);
1312
1313 /* Clear out UART and FIFO */
1314 readb(&ch->ch_neo_uart->txrx);
1315 writeb((UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT), &ch->ch_neo_uart->isr_fcr);
1316 readb(&ch->ch_neo_uart->lsr);
1317 readb(&ch->ch_neo_uart->msr);
1318
1319 ch->ch_flags |= CH_FIFO_ENABLED;
1320
1321 /* Assert any signals we want up */
1322 writeb(ch->ch_mostat, &ch->ch_neo_uart->mcr);
1323}
1324
1325/*
1326 * Make the UART completely turn off.
1327 */
1328static void neo_uart_off(struct jsm_channel *ch)
1329{
1330 /* Turn off UART enhanced bits */
1331 writeb(0, &ch->ch_neo_uart->efr);
1332
1333 /* Stop all interrupts from occurring. */
1334 writeb(0, &ch->ch_neo_uart->ier);
1335}
1336
1337static u32 neo_get_uart_bytes_left(struct jsm_channel *ch)
1338{
1339 u8 left = 0;
1340 u8 lsr = readb(&ch->ch_neo_uart->lsr);
1341
1342 /* We must cache the LSR as some of the bits get reset once read... */
1343 ch->ch_cached_lsr |= lsr;
1344
1345 /* Determine whether the Transmitter is empty or not */
1346 if (!(lsr & UART_LSR_TEMT))
1347 left = 1;
1348 else {
1349 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1350 left = 0;
1351 }
1352
1353 return left;
1354}
1355
1356/* Channel lock MUST be held by the calling function! */
1357static void neo_send_break(struct jsm_channel *ch)
1358{
1359 /*
1360 * Set the time we should stop sending the break.
1361 * If we are already sending a break, toss away the existing
1362 * time to stop, and use this new value instead.
1363 */
1364
1365 /* Tell the UART to start sending the break */
1366 if (!(ch->ch_flags & CH_BREAK_SENDING)) {
1367 u8 temp = readb(&ch->ch_neo_uart->lcr);
1368 writeb((temp | UART_LCR_SBC), &ch->ch_neo_uart->lcr);
1369 ch->ch_flags |= (CH_BREAK_SENDING);
1370
1371 /* flush write operation */
1372 neo_pci_posting_flush(ch->ch_bd);
1373 }
1374}
1375
1376/*
1377 * neo_send_immediate_char.
1378 *
1379 * Sends a specific character as soon as possible to the UART,
1380 * jumping over any bytes that might be in the write queue.
1381 *
1382 * The channel lock MUST be held by the calling function.
1383 */
1384static void neo_send_immediate_char(struct jsm_channel *ch, unsigned char c)
1385{
1386 if (!ch)
1387 return;
1388
1389 writeb(c, &ch->ch_neo_uart->txrx);
1390
1391 /* flush write operation */
1392 neo_pci_posting_flush(ch->ch_bd);
1393}
1394
1395struct board_ops jsm_neo_ops = {
1396 .intr = neo_intr,
1397 .uart_init = neo_uart_init,
1398 .uart_off = neo_uart_off,
1399 .param = neo_param,
1400 .assert_modem_signals = neo_assert_modem_signals,
1401 .flush_uart_write = neo_flush_uart_write,
1402 .flush_uart_read = neo_flush_uart_read,
1403 .disable_receiver = neo_disable_receiver,
1404 .enable_receiver = neo_enable_receiver,
1405 .send_break = neo_send_break,
1406 .clear_break = neo_clear_break,
1407 .send_start_character = neo_send_start_character,
1408 .send_stop_character = neo_send_stop_character,
1409 .copy_data_from_queue_to_uart = neo_copy_data_from_queue_to_uart,
1410 .get_uart_bytes_left = neo_get_uart_bytes_left,
1411 .send_immediate_char = neo_send_immediate_char
1412};
diff --git a/drivers/tty/serial/jsm/jsm_tty.c b/drivers/tty/serial/jsm/jsm_tty.c
new file mode 100644
index 000000000000..7a4a914ecff0
--- /dev/null
+++ b/drivers/tty/serial/jsm/jsm_tty.c
@@ -0,0 +1,910 @@
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 * Ananda Venkatarman <mansarov@us.ibm.com>
24 * Modifications:
25 * 01/19/06: changed jsm_input routine to use the dynamically allocated
26 * tty_buffer changes. Contributors: Scott Kilau and Ananda V.
27 ***********************************************************************/
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial_reg.h>
31#include <linux/delay.h> /* For udelay */
32#include <linux/pci.h>
33#include <linux/slab.h>
34
35#include "jsm.h"
36
37static DECLARE_BITMAP(linemap, MAXLINES);
38
39static void jsm_carrier(struct jsm_channel *ch);
40
41static inline int jsm_get_mstat(struct jsm_channel *ch)
42{
43 unsigned char mstat;
44 unsigned result;
45
46 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "start\n");
47
48 mstat = (ch->ch_mostat | ch->ch_mistat);
49
50 result = 0;
51
52 if (mstat & UART_MCR_DTR)
53 result |= TIOCM_DTR;
54 if (mstat & UART_MCR_RTS)
55 result |= TIOCM_RTS;
56 if (mstat & UART_MSR_CTS)
57 result |= TIOCM_CTS;
58 if (mstat & UART_MSR_DSR)
59 result |= TIOCM_DSR;
60 if (mstat & UART_MSR_RI)
61 result |= TIOCM_RI;
62 if (mstat & UART_MSR_DCD)
63 result |= TIOCM_CD;
64
65 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
66 return result;
67}
68
69static unsigned int jsm_tty_tx_empty(struct uart_port *port)
70{
71 return TIOCSER_TEMT;
72}
73
74/*
75 * Return modem signals to ld.
76 */
77static unsigned int jsm_tty_get_mctrl(struct uart_port *port)
78{
79 int result;
80 struct jsm_channel *channel = (struct jsm_channel *)port;
81
82 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
83
84 result = jsm_get_mstat(channel);
85
86 if (result < 0)
87 return -ENXIO;
88
89 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
90
91 return result;
92}
93
94/*
95 * jsm_set_modem_info()
96 *
97 * Set modem signals, called by ld.
98 */
99static void jsm_tty_set_mctrl(struct uart_port *port, unsigned int mctrl)
100{
101 struct jsm_channel *channel = (struct jsm_channel *)port;
102
103 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
104
105 if (mctrl & TIOCM_RTS)
106 channel->ch_mostat |= UART_MCR_RTS;
107 else
108 channel->ch_mostat &= ~UART_MCR_RTS;
109
110 if (mctrl & TIOCM_DTR)
111 channel->ch_mostat |= UART_MCR_DTR;
112 else
113 channel->ch_mostat &= ~UART_MCR_DTR;
114
115 channel->ch_bd->bd_ops->assert_modem_signals(channel);
116
117 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
118 udelay(10);
119}
120
121static void jsm_tty_start_tx(struct uart_port *port)
122{
123 struct jsm_channel *channel = (struct jsm_channel *)port;
124
125 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
126
127 channel->ch_flags &= ~(CH_STOP);
128 jsm_tty_write(port);
129
130 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
131}
132
133static void jsm_tty_stop_tx(struct uart_port *port)
134{
135 struct jsm_channel *channel = (struct jsm_channel *)port;
136
137 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
138
139 channel->ch_flags |= (CH_STOP);
140
141 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
142}
143
144static void jsm_tty_send_xchar(struct uart_port *port, char ch)
145{
146 unsigned long lock_flags;
147 struct jsm_channel *channel = (struct jsm_channel *)port;
148 struct ktermios *termios;
149
150 spin_lock_irqsave(&port->lock, lock_flags);
151 termios = port->state->port.tty->termios;
152 if (ch == termios->c_cc[VSTART])
153 channel->ch_bd->bd_ops->send_start_character(channel);
154
155 if (ch == termios->c_cc[VSTOP])
156 channel->ch_bd->bd_ops->send_stop_character(channel);
157 spin_unlock_irqrestore(&port->lock, lock_flags);
158}
159
160static void jsm_tty_stop_rx(struct uart_port *port)
161{
162 struct jsm_channel *channel = (struct jsm_channel *)port;
163
164 channel->ch_bd->bd_ops->disable_receiver(channel);
165}
166
167static void jsm_tty_enable_ms(struct uart_port *port)
168{
169 /* Nothing needed */
170}
171
172static void jsm_tty_break(struct uart_port *port, int break_state)
173{
174 unsigned long lock_flags;
175 struct jsm_channel *channel = (struct jsm_channel *)port;
176
177 spin_lock_irqsave(&port->lock, lock_flags);
178 if (break_state == -1)
179 channel->ch_bd->bd_ops->send_break(channel);
180 else
181 channel->ch_bd->bd_ops->clear_break(channel, 0);
182
183 spin_unlock_irqrestore(&port->lock, lock_flags);
184}
185
186static int jsm_tty_open(struct uart_port *port)
187{
188 struct jsm_board *brd;
189 struct jsm_channel *channel = (struct jsm_channel *)port;
190 struct ktermios *termios;
191
192 /* Get board pointer from our array of majors we have allocated */
193 brd = channel->ch_bd;
194
195 /*
196 * Allocate channel buffers for read/write/error.
197 * Set flag, so we don't get trounced on.
198 */
199 channel->ch_flags |= (CH_OPENING);
200
201 /* Drop locks, as malloc with GFP_KERNEL can sleep */
202
203 if (!channel->ch_rqueue) {
204 channel->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
205 if (!channel->ch_rqueue) {
206 jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
207 "unable to allocate read queue buf");
208 return -ENOMEM;
209 }
210 }
211 if (!channel->ch_equeue) {
212 channel->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
213 if (!channel->ch_equeue) {
214 jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
215 "unable to allocate error queue buf");
216 return -ENOMEM;
217 }
218 }
219 if (!channel->ch_wqueue) {
220 channel->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
221 if (!channel->ch_wqueue) {
222 jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
223 "unable to allocate write queue buf");
224 return -ENOMEM;
225 }
226 }
227
228 channel->ch_flags &= ~(CH_OPENING);
229 /*
230 * Initialize if neither terminal is open.
231 */
232 jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev,
233 "jsm_open: initializing channel in open...\n");
234
235 /*
236 * Flush input queues.
237 */
238 channel->ch_r_head = channel->ch_r_tail = 0;
239 channel->ch_e_head = channel->ch_e_tail = 0;
240 channel->ch_w_head = channel->ch_w_tail = 0;
241
242 brd->bd_ops->flush_uart_write(channel);
243 brd->bd_ops->flush_uart_read(channel);
244
245 channel->ch_flags = 0;
246 channel->ch_cached_lsr = 0;
247 channel->ch_stops_sent = 0;
248
249 termios = port->state->port.tty->termios;
250 channel->ch_c_cflag = termios->c_cflag;
251 channel->ch_c_iflag = termios->c_iflag;
252 channel->ch_c_oflag = termios->c_oflag;
253 channel->ch_c_lflag = termios->c_lflag;
254 channel->ch_startc = termios->c_cc[VSTART];
255 channel->ch_stopc = termios->c_cc[VSTOP];
256
257 /* Tell UART to init itself */
258 brd->bd_ops->uart_init(channel);
259
260 /*
261 * Run param in case we changed anything
262 */
263 brd->bd_ops->param(channel);
264
265 jsm_carrier(channel);
266
267 channel->ch_open_count++;
268
269 jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev, "finish\n");
270 return 0;
271}
272
273static void jsm_tty_close(struct uart_port *port)
274{
275 struct jsm_board *bd;
276 struct ktermios *ts;
277 struct jsm_channel *channel = (struct jsm_channel *)port;
278
279 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "start\n");
280
281 bd = channel->ch_bd;
282 ts = port->state->port.tty->termios;
283
284 channel->ch_flags &= ~(CH_STOPI);
285
286 channel->ch_open_count--;
287
288 /*
289 * If we have HUPCL set, lower DTR and RTS
290 */
291 if (channel->ch_c_cflag & HUPCL) {
292 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev,
293 "Close. HUPCL set, dropping DTR/RTS\n");
294
295 /* Drop RTS/DTR */
296 channel->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
297 bd->bd_ops->assert_modem_signals(channel);
298 }
299
300 /* Turn off UART interrupts for this port */
301 channel->ch_bd->bd_ops->uart_off(channel);
302
303 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "finish\n");
304}
305
306static void jsm_tty_set_termios(struct uart_port *port,
307 struct ktermios *termios,
308 struct ktermios *old_termios)
309{
310 unsigned long lock_flags;
311 struct jsm_channel *channel = (struct jsm_channel *)port;
312
313 spin_lock_irqsave(&port->lock, lock_flags);
314 channel->ch_c_cflag = termios->c_cflag;
315 channel->ch_c_iflag = termios->c_iflag;
316 channel->ch_c_oflag = termios->c_oflag;
317 channel->ch_c_lflag = termios->c_lflag;
318 channel->ch_startc = termios->c_cc[VSTART];
319 channel->ch_stopc = termios->c_cc[VSTOP];
320
321 channel->ch_bd->bd_ops->param(channel);
322 jsm_carrier(channel);
323 spin_unlock_irqrestore(&port->lock, lock_flags);
324}
325
326static const char *jsm_tty_type(struct uart_port *port)
327{
328 return "jsm";
329}
330
331static void jsm_tty_release_port(struct uart_port *port)
332{
333}
334
335static int jsm_tty_request_port(struct uart_port *port)
336{
337 return 0;
338}
339
340static void jsm_config_port(struct uart_port *port, int flags)
341{
342 port->type = PORT_JSM;
343}
344
345static struct uart_ops jsm_ops = {
346 .tx_empty = jsm_tty_tx_empty,
347 .set_mctrl = jsm_tty_set_mctrl,
348 .get_mctrl = jsm_tty_get_mctrl,
349 .stop_tx = jsm_tty_stop_tx,
350 .start_tx = jsm_tty_start_tx,
351 .send_xchar = jsm_tty_send_xchar,
352 .stop_rx = jsm_tty_stop_rx,
353 .enable_ms = jsm_tty_enable_ms,
354 .break_ctl = jsm_tty_break,
355 .startup = jsm_tty_open,
356 .shutdown = jsm_tty_close,
357 .set_termios = jsm_tty_set_termios,
358 .type = jsm_tty_type,
359 .release_port = jsm_tty_release_port,
360 .request_port = jsm_tty_request_port,
361 .config_port = jsm_config_port,
362};
363
364/*
365 * jsm_tty_init()
366 *
367 * Init the tty subsystem. Called once per board after board has been
368 * downloaded and init'ed.
369 */
370int __devinit jsm_tty_init(struct jsm_board *brd)
371{
372 int i;
373 void __iomem *vaddr;
374 struct jsm_channel *ch;
375
376 if (!brd)
377 return -ENXIO;
378
379 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
380
381 /*
382 * Initialize board structure elements.
383 */
384
385 brd->nasync = brd->maxports;
386
387 /*
388 * Allocate channel memory that might not have been allocated
389 * when the driver was first loaded.
390 */
391 for (i = 0; i < brd->nasync; i++) {
392 if (!brd->channels[i]) {
393
394 /*
395 * Okay to malloc with GFP_KERNEL, we are not at
396 * interrupt context, and there are no locks held.
397 */
398 brd->channels[i] = kzalloc(sizeof(struct jsm_channel), GFP_KERNEL);
399 if (!brd->channels[i]) {
400 jsm_printk(CORE, ERR, &brd->pci_dev,
401 "%s:%d Unable to allocate memory for channel struct\n",
402 __FILE__, __LINE__);
403 }
404 }
405 }
406
407 ch = brd->channels[0];
408 vaddr = brd->re_map_membase;
409
410 /* Set up channel variables */
411 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
412
413 if (!brd->channels[i])
414 continue;
415
416 spin_lock_init(&ch->ch_lock);
417
418 if (brd->bd_uart_offset == 0x200)
419 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
420
421 ch->ch_bd = brd;
422 ch->ch_portnum = i;
423
424 /* .25 second delay */
425 ch->ch_close_delay = 250;
426
427 init_waitqueue_head(&ch->ch_flags_wait);
428 }
429
430 jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
431 return 0;
432}
433
434int jsm_uart_port_init(struct jsm_board *brd)
435{
436 int i, rc;
437 unsigned int line;
438 struct jsm_channel *ch;
439
440 if (!brd)
441 return -ENXIO;
442
443 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
444
445 /*
446 * Initialize board structure elements.
447 */
448
449 brd->nasync = brd->maxports;
450
451 /* Set up channel variables */
452 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
453
454 if (!brd->channels[i])
455 continue;
456
457 brd->channels[i]->uart_port.irq = brd->irq;
458 brd->channels[i]->uart_port.uartclk = 14745600;
459 brd->channels[i]->uart_port.type = PORT_JSM;
460 brd->channels[i]->uart_port.iotype = UPIO_MEM;
461 brd->channels[i]->uart_port.membase = brd->re_map_membase;
462 brd->channels[i]->uart_port.fifosize = 16;
463 brd->channels[i]->uart_port.ops = &jsm_ops;
464 line = find_first_zero_bit(linemap, MAXLINES);
465 if (line >= MAXLINES) {
466 printk(KERN_INFO "jsm: linemap is full, added device failed\n");
467 continue;
468 } else
469 set_bit(line, linemap);
470 brd->channels[i]->uart_port.line = line;
471 rc = uart_add_one_port (&jsm_uart_driver, &brd->channels[i]->uart_port);
472 if (rc){
473 printk(KERN_INFO "jsm: Port %d failed. Aborting...\n", i);
474 return rc;
475 }
476 else
477 printk(KERN_INFO "jsm: Port %d added\n", i);
478 }
479
480 jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
481 return 0;
482}
483
484int jsm_remove_uart_port(struct jsm_board *brd)
485{
486 int i;
487 struct jsm_channel *ch;
488
489 if (!brd)
490 return -ENXIO;
491
492 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
493
494 /*
495 * Initialize board structure elements.
496 */
497
498 brd->nasync = brd->maxports;
499
500 /* Set up channel variables */
501 for (i = 0; i < brd->nasync; i++) {
502
503 if (!brd->channels[i])
504 continue;
505
506 ch = brd->channels[i];
507
508 clear_bit(ch->uart_port.line, linemap);
509 uart_remove_one_port(&jsm_uart_driver, &brd->channels[i]->uart_port);
510 }
511
512 jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
513 return 0;
514}
515
516void jsm_input(struct jsm_channel *ch)
517{
518 struct jsm_board *bd;
519 struct tty_struct *tp;
520 u32 rmask;
521 u16 head;
522 u16 tail;
523 int data_len;
524 unsigned long lock_flags;
525 int len = 0;
526 int n = 0;
527 int s = 0;
528 int i = 0;
529
530 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
531
532 if (!ch)
533 return;
534
535 tp = ch->uart_port.state->port.tty;
536
537 bd = ch->ch_bd;
538 if(!bd)
539 return;
540
541 spin_lock_irqsave(&ch->ch_lock, lock_flags);
542
543 /*
544 *Figure the number of characters in the buffer.
545 *Exit immediately if none.
546 */
547
548 rmask = RQUEUEMASK;
549
550 head = ch->ch_r_head & rmask;
551 tail = ch->ch_r_tail & rmask;
552
553 data_len = (head - tail) & rmask;
554 if (data_len == 0) {
555 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
556 return;
557 }
558
559 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
560
561 /*
562 *If the device is not open, or CREAD is off, flush
563 *input data and return immediately.
564 */
565 if (!tp ||
566 !(tp->termios->c_cflag & CREAD) ) {
567
568 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
569 "input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum);
570 ch->ch_r_head = tail;
571
572 /* Force queue flow control to be released, if needed */
573 jsm_check_queue_flow_control(ch);
574
575 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
576 return;
577 }
578
579 /*
580 * If we are throttled, simply don't read any data.
581 */
582 if (ch->ch_flags & CH_STOPI) {
583 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
584 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
585 "Port %d throttled, not reading any data. head: %x tail: %x\n",
586 ch->ch_portnum, head, tail);
587 return;
588 }
589
590 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start 2\n");
591
592 if (data_len <= 0) {
593 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
594 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "jsm_input 1\n");
595 return;
596 }
597
598 len = tty_buffer_request_room(tp, data_len);
599 n = len;
600
601 /*
602 * n now contains the most amount of data we can copy,
603 * bounded either by the flip buffer size or the amount
604 * of data the card actually has pending...
605 */
606 while (n) {
607 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
608 s = min(s, n);
609
610 if (s <= 0)
611 break;
612
613 /*
614 * If conditions are such that ld needs to see all
615 * UART errors, we will have to walk each character
616 * and error byte and send them to the buffer one at
617 * a time.
618 */
619
620 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
621 for (i = 0; i < s; i++) {
622 /*
623 * Give the Linux ld the flags in the
624 * format it likes.
625 */
626 if (*(ch->ch_equeue +tail +i) & UART_LSR_BI)
627 tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_BREAK);
628 else if (*(ch->ch_equeue +tail +i) & UART_LSR_PE)
629 tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_PARITY);
630 else if (*(ch->ch_equeue +tail +i) & UART_LSR_FE)
631 tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_FRAME);
632 else
633 tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_NORMAL);
634 }
635 } else {
636 tty_insert_flip_string(tp, ch->ch_rqueue + tail, s) ;
637 }
638 tail += s;
639 n -= s;
640 /* Flip queue if needed */
641 tail &= rmask;
642 }
643
644 ch->ch_r_tail = tail & rmask;
645 ch->ch_e_tail = tail & rmask;
646 jsm_check_queue_flow_control(ch);
647 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
648
649 /* Tell the tty layer its okay to "eat" the data now */
650 tty_flip_buffer_push(tp);
651
652 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
653}
654
655static void jsm_carrier(struct jsm_channel *ch)
656{
657 struct jsm_board *bd;
658
659 int virt_carrier = 0;
660 int phys_carrier = 0;
661
662 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev, "start\n");
663 if (!ch)
664 return;
665
666 bd = ch->ch_bd;
667
668 if (!bd)
669 return;
670
671 if (ch->ch_mistat & UART_MSR_DCD) {
672 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
673 "mistat: %x D_CD: %x\n", ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD);
674 phys_carrier = 1;
675 }
676
677 if (ch->ch_c_cflag & CLOCAL)
678 virt_carrier = 1;
679
680 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
681 "DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier);
682
683 /*
684 * Test for a VIRTUAL carrier transition to HIGH.
685 */
686 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
687
688 /*
689 * When carrier rises, wake any threads waiting
690 * for carrier in the open routine.
691 */
692
693 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
694 "carrier: virt DCD rose\n");
695
696 if (waitqueue_active(&(ch->ch_flags_wait)))
697 wake_up_interruptible(&ch->ch_flags_wait);
698 }
699
700 /*
701 * Test for a PHYSICAL carrier transition to HIGH.
702 */
703 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
704
705 /*
706 * When carrier rises, wake any threads waiting
707 * for carrier in the open routine.
708 */
709
710 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
711 "carrier: physical DCD rose\n");
712
713 if (waitqueue_active(&(ch->ch_flags_wait)))
714 wake_up_interruptible(&ch->ch_flags_wait);
715 }
716
717 /*
718 * Test for a PHYSICAL transition to low, so long as we aren't
719 * currently ignoring physical transitions (which is what "virtual
720 * carrier" indicates).
721 *
722 * The transition of the virtual carrier to low really doesn't
723 * matter... it really only means "ignore carrier state", not
724 * "make pretend that carrier is there".
725 */
726 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0)
727 && (phys_carrier == 0)) {
728 /*
729 * When carrier drops:
730 *
731 * Drop carrier on all open units.
732 *
733 * Flush queues, waking up any task waiting in the
734 * line discipline.
735 *
736 * Send a hangup to the control terminal.
737 *
738 * Enable all select calls.
739 */
740 if (waitqueue_active(&(ch->ch_flags_wait)))
741 wake_up_interruptible(&ch->ch_flags_wait);
742 }
743
744 /*
745 * Make sure that our cached values reflect the current reality.
746 */
747 if (virt_carrier == 1)
748 ch->ch_flags |= CH_FCAR;
749 else
750 ch->ch_flags &= ~CH_FCAR;
751
752 if (phys_carrier == 1)
753 ch->ch_flags |= CH_CD;
754 else
755 ch->ch_flags &= ~CH_CD;
756}
757
758
759void jsm_check_queue_flow_control(struct jsm_channel *ch)
760{
761 struct board_ops *bd_ops = ch->ch_bd->bd_ops;
762 int qleft;
763
764 /* Store how much space we have left in the queue */
765 if ((qleft = ch->ch_r_tail - ch->ch_r_head - 1) < 0)
766 qleft += RQUEUEMASK + 1;
767
768 /*
769 * Check to see if we should enforce flow control on our queue because
770 * the ld (or user) isn't reading data out of our queue fast enuf.
771 *
772 * NOTE: This is done based on what the current flow control of the
773 * port is set for.
774 *
775 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
776 * This will cause the UART's FIFO to back up, and force
777 * the RTS signal to be dropped.
778 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
779 * the other side, in hopes it will stop sending data to us.
780 * 3) NONE - Nothing we can do. We will simply drop any extra data
781 * that gets sent into us when the queue fills up.
782 */
783 if (qleft < 256) {
784 /* HWFLOW */
785 if (ch->ch_c_cflag & CRTSCTS) {
786 if(!(ch->ch_flags & CH_RECEIVER_OFF)) {
787 bd_ops->disable_receiver(ch);
788 ch->ch_flags |= (CH_RECEIVER_OFF);
789 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
790 "Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
791 qleft);
792 }
793 }
794 /* SWFLOW */
795 else if (ch->ch_c_iflag & IXOFF) {
796 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
797 bd_ops->send_stop_character(ch);
798 ch->ch_stops_sent++;
799 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
800 "Sending stop char! Times sent: %x\n", ch->ch_stops_sent);
801 }
802 }
803 }
804
805 /*
806 * Check to see if we should unenforce flow control because
807 * ld (or user) finally read enuf data out of our queue.
808 *
809 * NOTE: This is done based on what the current flow control of the
810 * port is set for.
811 *
812 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
813 * This will cause the UART's FIFO to raise RTS back up,
814 * which will allow the other side to start sending data again.
815 * 2) SWFLOW (IXOFF) - Send a start character to
816 * the other side, so it will start sending data to us again.
817 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
818 * other side, we don't need to do anything now.
819 */
820 if (qleft > (RQUEUESIZE / 2)) {
821 /* HWFLOW */
822 if (ch->ch_c_cflag & CRTSCTS) {
823 if (ch->ch_flags & CH_RECEIVER_OFF) {
824 bd_ops->enable_receiver(ch);
825 ch->ch_flags &= ~(CH_RECEIVER_OFF);
826 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
827 "Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
828 qleft);
829 }
830 }
831 /* SWFLOW */
832 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
833 ch->ch_stops_sent = 0;
834 bd_ops->send_start_character(ch);
835 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "Sending start char!\n");
836 }
837 }
838}
839
840/*
841 * jsm_tty_write()
842 *
843 * Take data from the user or kernel and send it out to the FEP.
844 * In here exists all the Transparent Print magic as well.
845 */
846int jsm_tty_write(struct uart_port *port)
847{
848 int bufcount;
849 int data_count = 0,data_count1 =0;
850 u16 head;
851 u16 tail;
852 u16 tmask;
853 u32 remain;
854 int temp_tail = port->state->xmit.tail;
855 struct jsm_channel *channel = (struct jsm_channel *)port;
856
857 tmask = WQUEUEMASK;
858 head = (channel->ch_w_head) & tmask;
859 tail = (channel->ch_w_tail) & tmask;
860
861 if ((bufcount = tail - head - 1) < 0)
862 bufcount += WQUEUESIZE;
863
864 bufcount = min(bufcount, 56);
865 remain = WQUEUESIZE - head;
866
867 data_count = 0;
868 if (bufcount >= remain) {
869 bufcount -= remain;
870 while ((port->state->xmit.head != temp_tail) &&
871 (data_count < remain)) {
872 channel->ch_wqueue[head++] =
873 port->state->xmit.buf[temp_tail];
874
875 temp_tail++;
876 temp_tail &= (UART_XMIT_SIZE - 1);
877 data_count++;
878 }
879 if (data_count == remain) head = 0;
880 }
881
882 data_count1 = 0;
883 if (bufcount > 0) {
884 remain = bufcount;
885 while ((port->state->xmit.head != temp_tail) &&
886 (data_count1 < remain)) {
887 channel->ch_wqueue[head++] =
888 port->state->xmit.buf[temp_tail];
889
890 temp_tail++;
891 temp_tail &= (UART_XMIT_SIZE - 1);
892 data_count1++;
893
894 }
895 }
896
897 port->state->xmit.tail = temp_tail;
898
899 data_count += data_count1;
900 if (data_count) {
901 head &= tmask;
902 channel->ch_w_head = head;
903 }
904
905 if (data_count) {
906 channel->ch_bd->bd_ops->copy_data_from_queue_to_uart(channel);
907 }
908
909 return data_count;
910}
diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
new file mode 100644
index 000000000000..25a8bc565f40
--- /dev/null
+++ b/drivers/tty/serial/kgdboc.c
@@ -0,0 +1,328 @@
1/*
2 * Based on the same principle as kgdboe using the NETPOLL api, this
3 * driver uses a console polling api to implement a gdb serial inteface
4 * which is multiplexed on a console port.
5 *
6 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
7 *
8 * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 */
14#include <linux/kernel.h>
15#include <linux/ctype.h>
16#include <linux/kgdb.h>
17#include <linux/kdb.h>
18#include <linux/tty.h>
19#include <linux/console.h>
20#include <linux/vt_kern.h>
21#include <linux/input.h>
22
23#define MAX_CONFIG_LEN 40
24
25static struct kgdb_io kgdboc_io_ops;
26
27/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
28static int configured = -1;
29
30static char config[MAX_CONFIG_LEN];
31static struct kparam_string kps = {
32 .string = config,
33 .maxlen = MAX_CONFIG_LEN,
34};
35
36static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
37static struct tty_driver *kgdb_tty_driver;
38static int kgdb_tty_line;
39
40#ifdef CONFIG_KDB_KEYBOARD
41static int kgdboc_reset_connect(struct input_handler *handler,
42 struct input_dev *dev,
43 const struct input_device_id *id)
44{
45 input_reset_device(dev);
46
47 /* Retrun an error - we do not want to bind, just to reset */
48 return -ENODEV;
49}
50
51static void kgdboc_reset_disconnect(struct input_handle *handle)
52{
53 /* We do not expect anyone to actually bind to us */
54 BUG();
55}
56
57static const struct input_device_id kgdboc_reset_ids[] = {
58 {
59 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
60 .evbit = { BIT_MASK(EV_KEY) },
61 },
62 { }
63};
64
65static struct input_handler kgdboc_reset_handler = {
66 .connect = kgdboc_reset_connect,
67 .disconnect = kgdboc_reset_disconnect,
68 .name = "kgdboc_reset",
69 .id_table = kgdboc_reset_ids,
70};
71
72static DEFINE_MUTEX(kgdboc_reset_mutex);
73
74static void kgdboc_restore_input_helper(struct work_struct *dummy)
75{
76 /*
77 * We need to take a mutex to prevent several instances of
78 * this work running on different CPUs so they don't try
79 * to register again already registered handler.
80 */
81 mutex_lock(&kgdboc_reset_mutex);
82
83 if (input_register_handler(&kgdboc_reset_handler) == 0)
84 input_unregister_handler(&kgdboc_reset_handler);
85
86 mutex_unlock(&kgdboc_reset_mutex);
87}
88
89static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
90
91static void kgdboc_restore_input(void)
92{
93 if (likely(system_state == SYSTEM_RUNNING))
94 schedule_work(&kgdboc_restore_input_work);
95}
96
97static int kgdboc_register_kbd(char **cptr)
98{
99 if (strncmp(*cptr, "kbd", 3) == 0) {
100 if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
101 kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
102 kdb_poll_idx++;
103 if (cptr[0][3] == ',')
104 *cptr += 4;
105 else
106 return 1;
107 }
108 }
109 return 0;
110}
111
112static void kgdboc_unregister_kbd(void)
113{
114 int i;
115
116 for (i = 0; i < kdb_poll_idx; i++) {
117 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
118 kdb_poll_idx--;
119 kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
120 kdb_poll_funcs[kdb_poll_idx] = NULL;
121 i--;
122 }
123 }
124 flush_work_sync(&kgdboc_restore_input_work);
125}
126#else /* ! CONFIG_KDB_KEYBOARD */
127#define kgdboc_register_kbd(x) 0
128#define kgdboc_unregister_kbd()
129#define kgdboc_restore_input()
130#endif /* ! CONFIG_KDB_KEYBOARD */
131
132static int kgdboc_option_setup(char *opt)
133{
134 if (strlen(opt) > MAX_CONFIG_LEN) {
135 printk(KERN_ERR "kgdboc: config string too long\n");
136 return -ENOSPC;
137 }
138 strcpy(config, opt);
139
140 return 0;
141}
142
143__setup("kgdboc=", kgdboc_option_setup);
144
145static void cleanup_kgdboc(void)
146{
147 kgdboc_unregister_kbd();
148 if (configured == 1)
149 kgdb_unregister_io_module(&kgdboc_io_ops);
150}
151
152static int configure_kgdboc(void)
153{
154 struct tty_driver *p;
155 int tty_line = 0;
156 int err;
157 char *cptr = config;
158 struct console *cons;
159
160 err = kgdboc_option_setup(config);
161 if (err || !strlen(config) || isspace(config[0]))
162 goto noconfig;
163
164 err = -ENODEV;
165 kgdboc_io_ops.is_console = 0;
166 kgdb_tty_driver = NULL;
167
168 kgdboc_use_kms = 0;
169 if (strncmp(cptr, "kms,", 4) == 0) {
170 cptr += 4;
171 kgdboc_use_kms = 1;
172 }
173
174 if (kgdboc_register_kbd(&cptr))
175 goto do_register;
176
177 p = tty_find_polling_driver(cptr, &tty_line);
178 if (!p)
179 goto noconfig;
180
181 cons = console_drivers;
182 while (cons) {
183 int idx;
184 if (cons->device && cons->device(cons, &idx) == p &&
185 idx == tty_line) {
186 kgdboc_io_ops.is_console = 1;
187 break;
188 }
189 cons = cons->next;
190 }
191
192 kgdb_tty_driver = p;
193 kgdb_tty_line = tty_line;
194
195do_register:
196 err = kgdb_register_io_module(&kgdboc_io_ops);
197 if (err)
198 goto noconfig;
199
200 configured = 1;
201
202 return 0;
203
204noconfig:
205 config[0] = 0;
206 configured = 0;
207 cleanup_kgdboc();
208
209 return err;
210}
211
212static int __init init_kgdboc(void)
213{
214 /* Already configured? */
215 if (configured == 1)
216 return 0;
217
218 return configure_kgdboc();
219}
220
221static int kgdboc_get_char(void)
222{
223 if (!kgdb_tty_driver)
224 return -1;
225 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
226 kgdb_tty_line);
227}
228
229static void kgdboc_put_char(u8 chr)
230{
231 if (!kgdb_tty_driver)
232 return;
233 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
234 kgdb_tty_line, chr);
235}
236
237static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
238{
239 int len = strlen(kmessage);
240
241 if (len >= MAX_CONFIG_LEN) {
242 printk(KERN_ERR "kgdboc: config string too long\n");
243 return -ENOSPC;
244 }
245
246 /* Only copy in the string if the init function has not run yet */
247 if (configured < 0) {
248 strcpy(config, kmessage);
249 return 0;
250 }
251
252 if (kgdb_connected) {
253 printk(KERN_ERR
254 "kgdboc: Cannot reconfigure while KGDB is connected.\n");
255
256 return -EBUSY;
257 }
258
259 strcpy(config, kmessage);
260 /* Chop out \n char as a result of echo */
261 if (config[len - 1] == '\n')
262 config[len - 1] = '\0';
263
264 if (configured == 1)
265 cleanup_kgdboc();
266
267 /* Go and configure with the new params. */
268 return configure_kgdboc();
269}
270
271static int dbg_restore_graphics;
272
273static void kgdboc_pre_exp_handler(void)
274{
275 if (!dbg_restore_graphics && kgdboc_use_kms) {
276 dbg_restore_graphics = 1;
277 con_debug_enter(vc_cons[fg_console].d);
278 }
279 /* Increment the module count when the debugger is active */
280 if (!kgdb_connected)
281 try_module_get(THIS_MODULE);
282}
283
284static void kgdboc_post_exp_handler(void)
285{
286 /* decrement the module count when the debugger detaches */
287 if (!kgdb_connected)
288 module_put(THIS_MODULE);
289 if (kgdboc_use_kms && dbg_restore_graphics) {
290 dbg_restore_graphics = 0;
291 con_debug_leave();
292 }
293 kgdboc_restore_input();
294}
295
296static struct kgdb_io kgdboc_io_ops = {
297 .name = "kgdboc",
298 .read_char = kgdboc_get_char,
299 .write_char = kgdboc_put_char,
300 .pre_exception = kgdboc_pre_exp_handler,
301 .post_exception = kgdboc_post_exp_handler,
302};
303
304#ifdef CONFIG_KGDB_SERIAL_CONSOLE
305/* This is only available if kgdboc is a built in for early debugging */
306static int __init kgdboc_early_init(char *opt)
307{
308 /* save the first character of the config string because the
309 * init routine can destroy it.
310 */
311 char save_ch;
312
313 kgdboc_option_setup(opt);
314 save_ch = config[0];
315 init_kgdboc();
316 config[0] = save_ch;
317 return 0;
318}
319
320early_param("ekgdboc", kgdboc_early_init);
321#endif /* CONFIG_KGDB_SERIAL_CONSOLE */
322
323module_init(init_kgdboc);
324module_exit(cleanup_kgdboc);
325module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
326MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
327MODULE_DESCRIPTION("KGDB Console TTY Driver");
328MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/m32r_sio.c b/drivers/tty/serial/m32r_sio.c
new file mode 100644
index 000000000000..bea5c215460c
--- /dev/null
+++ b/drivers/tty/serial/m32r_sio.c
@@ -0,0 +1,1192 @@
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
29#if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
30#define SUPPORT_SYSRQ
31#endif
32
33#include <linux/module.h>
34#include <linux/tty.h>
35#include <linux/ioport.h>
36#include <linux/init.h>
37#include <linux/console.h>
38#include <linux/sysrq.h>
39#include <linux/serial.h>
40#include <linux/serialP.h>
41#include <linux/delay.h>
42
43#include <asm/m32r.h>
44#include <asm/io.h>
45#include <asm/irq.h>
46
47#define PORT_M32R_BASE PORT_M32R_SIO
48#define PORT_INDEX(x) (x - PORT_M32R_BASE + 1)
49#define BAUD_RATE 115200
50
51#include <linux/serial_core.h>
52#include "m32r_sio.h"
53#include "m32r_sio_reg.h"
54
55/*
56 * Debugging.
57 */
58#if 0
59#define DEBUG_AUTOCONF(fmt...) printk(fmt)
60#else
61#define DEBUG_AUTOCONF(fmt...) do { } while (0)
62#endif
63
64#if 0
65#define DEBUG_INTR(fmt...) printk(fmt)
66#else
67#define DEBUG_INTR(fmt...) do { } while (0)
68#endif
69
70#define PASS_LIMIT 256
71
72/*
73 * We default to IRQ0 for the "no irq" hack. Some
74 * machine types want others as well - they're free
75 * to redefine this in their header file.
76 */
77#define is_real_interrupt(irq) ((irq) != 0)
78
79#define BASE_BAUD 115200
80
81/* Standard COM flags */
82#define STD_COM_FLAGS (UPF_BOOT_AUTOCONF | UPF_SKIP_TEST)
83
84/*
85 * SERIAL_PORT_DFNS tells us about built-in ports that have no
86 * standard enumeration mechanism. Platforms that can find all
87 * serial ports via mechanisms like ACPI or PCI need not supply it.
88 */
89#if defined(CONFIG_PLAT_USRV)
90
91#define SERIAL_PORT_DFNS \
92 /* UART CLK PORT IRQ FLAGS */ \
93 { 0, BASE_BAUD, 0x3F8, PLD_IRQ_UART0, STD_COM_FLAGS }, /* ttyS0 */ \
94 { 0, BASE_BAUD, 0x2F8, PLD_IRQ_UART1, STD_COM_FLAGS }, /* ttyS1 */
95
96#else /* !CONFIG_PLAT_USRV */
97
98#if defined(CONFIG_SERIAL_M32R_PLDSIO)
99#define SERIAL_PORT_DFNS \
100 { 0, BASE_BAUD, ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV, \
101 STD_COM_FLAGS }, /* ttyS0 */
102#else
103#define SERIAL_PORT_DFNS \
104 { 0, BASE_BAUD, M32R_SIO_OFFSET, M32R_IRQ_SIO0_R, \
105 STD_COM_FLAGS }, /* ttyS0 */
106#endif
107
108#endif /* !CONFIG_PLAT_USRV */
109
110static struct old_serial_port old_serial_port[] = {
111 SERIAL_PORT_DFNS
112};
113
114#define UART_NR ARRAY_SIZE(old_serial_port)
115
116struct uart_sio_port {
117 struct uart_port port;
118 struct timer_list timer; /* "no irq" timer */
119 struct list_head list; /* ports on this IRQ */
120 unsigned short rev;
121 unsigned char acr;
122 unsigned char ier;
123 unsigned char lcr;
124 unsigned char mcr_mask; /* mask of user bits */
125 unsigned char mcr_force; /* mask of forced bits */
126 unsigned char lsr_break_flag;
127
128 /*
129 * We provide a per-port pm hook.
130 */
131 void (*pm)(struct uart_port *port,
132 unsigned int state, unsigned int old);
133};
134
135struct irq_info {
136 spinlock_t lock;
137 struct list_head *head;
138};
139
140static struct irq_info irq_lists[NR_IRQS];
141
142/*
143 * Here we define the default xmit fifo size used for each type of UART.
144 */
145static const struct serial_uart_config uart_config[] = {
146 [PORT_UNKNOWN] = {
147 .name = "unknown",
148 .dfl_xmit_fifo_size = 1,
149 .flags = 0,
150 },
151 [PORT_INDEX(PORT_M32R_SIO)] = {
152 .name = "M32RSIO",
153 .dfl_xmit_fifo_size = 1,
154 .flags = 0,
155 },
156};
157
158#ifdef CONFIG_SERIAL_M32R_PLDSIO
159
160#define __sio_in(x) inw((unsigned long)(x))
161#define __sio_out(v,x) outw((v),(unsigned long)(x))
162
163static inline void sio_set_baud_rate(unsigned long baud)
164{
165 unsigned short sbaud;
166 sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
167 __sio_out(sbaud, PLD_ESIO0BAUR);
168}
169
170static void sio_reset(void)
171{
172 unsigned short tmp;
173
174 tmp = __sio_in(PLD_ESIO0RXB);
175 tmp = __sio_in(PLD_ESIO0RXB);
176 tmp = __sio_in(PLD_ESIO0CR);
177 sio_set_baud_rate(BAUD_RATE);
178 __sio_out(0x0300, PLD_ESIO0CR);
179 __sio_out(0x0003, PLD_ESIO0CR);
180}
181
182static void sio_init(void)
183{
184 unsigned short tmp;
185
186 tmp = __sio_in(PLD_ESIO0RXB);
187 tmp = __sio_in(PLD_ESIO0RXB);
188 tmp = __sio_in(PLD_ESIO0CR);
189 __sio_out(0x0300, PLD_ESIO0CR);
190 __sio_out(0x0003, PLD_ESIO0CR);
191}
192
193static void sio_error(int *status)
194{
195 printk("SIO0 error[%04x]\n", *status);
196 do {
197 sio_init();
198 } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
199}
200
201#else /* not CONFIG_SERIAL_M32R_PLDSIO */
202
203#define __sio_in(x) inl(x)
204#define __sio_out(v,x) outl((v),(x))
205
206static inline void sio_set_baud_rate(unsigned long baud)
207{
208 unsigned long i, j;
209
210 i = boot_cpu_data.bus_clock / (baud * 16);
211 j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
212 i -= 1;
213 j = (j + 1) >> 1;
214
215 __sio_out(i, M32R_SIO0_BAUR_PORTL);
216 __sio_out(j, M32R_SIO0_RBAUR_PORTL);
217}
218
219static void sio_reset(void)
220{
221 __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */
222 __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */
223 __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */
224 sio_set_baud_rate(BAUD_RATE);
225 __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
226 __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */
227}
228
229static void sio_init(void)
230{
231 unsigned int tmp;
232
233 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
234 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
235 tmp = __sio_in(M32R_SIO0_STS_PORTL);
236 __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
237}
238
239static void sio_error(int *status)
240{
241 printk("SIO0 error[%04x]\n", *status);
242 do {
243 sio_init();
244 } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
245}
246
247#endif /* CONFIG_SERIAL_M32R_PLDSIO */
248
249static unsigned int sio_in(struct uart_sio_port *up, int offset)
250{
251 return __sio_in(up->port.iobase + offset);
252}
253
254static void sio_out(struct uart_sio_port *up, int offset, int value)
255{
256 __sio_out(value, up->port.iobase + offset);
257}
258
259static unsigned int serial_in(struct uart_sio_port *up, int offset)
260{
261 if (!offset)
262 return 0;
263
264 return __sio_in(offset);
265}
266
267static void serial_out(struct uart_sio_port *up, int offset, int value)
268{
269 if (!offset)
270 return;
271
272 __sio_out(value, offset);
273}
274
275static void m32r_sio_stop_tx(struct uart_port *port)
276{
277 struct uart_sio_port *up = (struct uart_sio_port *)port;
278
279 if (up->ier & UART_IER_THRI) {
280 up->ier &= ~UART_IER_THRI;
281 serial_out(up, UART_IER, up->ier);
282 }
283}
284
285static void m32r_sio_start_tx(struct uart_port *port)
286{
287#ifdef CONFIG_SERIAL_M32R_PLDSIO
288 struct uart_sio_port *up = (struct uart_sio_port *)port;
289 struct circ_buf *xmit = &up->port.state->xmit;
290
291 if (!(up->ier & UART_IER_THRI)) {
292 up->ier |= UART_IER_THRI;
293 serial_out(up, UART_IER, up->ier);
294 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
295 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
296 up->port.icount.tx++;
297 }
298 while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
299#else
300 struct uart_sio_port *up = (struct uart_sio_port *)port;
301
302 if (!(up->ier & UART_IER_THRI)) {
303 up->ier |= UART_IER_THRI;
304 serial_out(up, UART_IER, up->ier);
305 }
306#endif
307}
308
309static void m32r_sio_stop_rx(struct uart_port *port)
310{
311 struct uart_sio_port *up = (struct uart_sio_port *)port;
312
313 up->ier &= ~UART_IER_RLSI;
314 up->port.read_status_mask &= ~UART_LSR_DR;
315 serial_out(up, UART_IER, up->ier);
316}
317
318static void m32r_sio_enable_ms(struct uart_port *port)
319{
320 struct uart_sio_port *up = (struct uart_sio_port *)port;
321
322 up->ier |= UART_IER_MSI;
323 serial_out(up, UART_IER, up->ier);
324}
325
326static void receive_chars(struct uart_sio_port *up, int *status)
327{
328 struct tty_struct *tty = up->port.state->port.tty;
329 unsigned char ch;
330 unsigned char flag;
331 int max_count = 256;
332
333 do {
334 ch = sio_in(up, SIORXB);
335 flag = TTY_NORMAL;
336 up->port.icount.rx++;
337
338 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
339 UART_LSR_FE | UART_LSR_OE))) {
340 /*
341 * For statistics only
342 */
343 if (*status & UART_LSR_BI) {
344 *status &= ~(UART_LSR_FE | UART_LSR_PE);
345 up->port.icount.brk++;
346 /*
347 * We do the SysRQ and SAK checking
348 * here because otherwise the break
349 * may get masked by ignore_status_mask
350 * or read_status_mask.
351 */
352 if (uart_handle_break(&up->port))
353 goto ignore_char;
354 } else if (*status & UART_LSR_PE)
355 up->port.icount.parity++;
356 else if (*status & UART_LSR_FE)
357 up->port.icount.frame++;
358 if (*status & UART_LSR_OE)
359 up->port.icount.overrun++;
360
361 /*
362 * Mask off conditions which should be ingored.
363 */
364 *status &= up->port.read_status_mask;
365
366 if (up->port.line == up->port.cons->index) {
367 /* Recover the break flag from console xmit */
368 *status |= up->lsr_break_flag;
369 up->lsr_break_flag = 0;
370 }
371
372 if (*status & UART_LSR_BI) {
373 DEBUG_INTR("handling break....");
374 flag = TTY_BREAK;
375 } else if (*status & UART_LSR_PE)
376 flag = TTY_PARITY;
377 else if (*status & UART_LSR_FE)
378 flag = TTY_FRAME;
379 }
380 if (uart_handle_sysrq_char(&up->port, ch))
381 goto ignore_char;
382 if ((*status & up->port.ignore_status_mask) == 0)
383 tty_insert_flip_char(tty, ch, flag);
384
385 if (*status & UART_LSR_OE) {
386 /*
387 * Overrun is special, since it's reported
388 * immediately, and doesn't affect the current
389 * character.
390 */
391 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
392 }
393 ignore_char:
394 *status = serial_in(up, UART_LSR);
395 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
396 tty_flip_buffer_push(tty);
397}
398
399static void transmit_chars(struct uart_sio_port *up)
400{
401 struct circ_buf *xmit = &up->port.state->xmit;
402 int count;
403
404 if (up->port.x_char) {
405#ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
406 serial_out(up, UART_TX, up->port.x_char);
407#endif
408 up->port.icount.tx++;
409 up->port.x_char = 0;
410 return;
411 }
412 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
413 m32r_sio_stop_tx(&up->port);
414 return;
415 }
416
417 count = up->port.fifosize;
418 do {
419 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
420 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
421 up->port.icount.tx++;
422 if (uart_circ_empty(xmit))
423 break;
424 while (!(serial_in(up, UART_LSR) & UART_LSR_THRE));
425
426 } while (--count > 0);
427
428 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
429 uart_write_wakeup(&up->port);
430
431 DEBUG_INTR("THRE...");
432
433 if (uart_circ_empty(xmit))
434 m32r_sio_stop_tx(&up->port);
435}
436
437/*
438 * This handles the interrupt from one port.
439 */
440static inline void m32r_sio_handle_port(struct uart_sio_port *up,
441 unsigned int status)
442{
443 DEBUG_INTR("status = %x...", status);
444
445 if (status & 0x04)
446 receive_chars(up, &status);
447 if (status & 0x01)
448 transmit_chars(up);
449}
450
451/*
452 * This is the serial driver's interrupt routine.
453 *
454 * Arjan thinks the old way was overly complex, so it got simplified.
455 * Alan disagrees, saying that need the complexity to handle the weird
456 * nature of ISA shared interrupts. (This is a special exception.)
457 *
458 * In order to handle ISA shared interrupts properly, we need to check
459 * that all ports have been serviced, and therefore the ISA interrupt
460 * line has been de-asserted.
461 *
462 * This means we need to loop through all ports. checking that they
463 * don't have an interrupt pending.
464 */
465static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id)
466{
467 struct irq_info *i = dev_id;
468 struct list_head *l, *end = NULL;
469 int pass_counter = 0;
470
471 DEBUG_INTR("m32r_sio_interrupt(%d)...", irq);
472
473#ifdef CONFIG_SERIAL_M32R_PLDSIO
474// if (irq == PLD_IRQ_SIO0_SND)
475// irq = PLD_IRQ_SIO0_RCV;
476#else
477 if (irq == M32R_IRQ_SIO0_S)
478 irq = M32R_IRQ_SIO0_R;
479#endif
480
481 spin_lock(&i->lock);
482
483 l = i->head;
484 do {
485 struct uart_sio_port *up;
486 unsigned int sts;
487
488 up = list_entry(l, struct uart_sio_port, list);
489
490 sts = sio_in(up, SIOSTS);
491 if (sts & 0x5) {
492 spin_lock(&up->port.lock);
493 m32r_sio_handle_port(up, sts);
494 spin_unlock(&up->port.lock);
495
496 end = NULL;
497 } else if (end == NULL)
498 end = l;
499
500 l = l->next;
501
502 if (l == i->head && pass_counter++ > PASS_LIMIT) {
503 if (sts & 0xe0)
504 sio_error(&sts);
505 break;
506 }
507 } while (l != end);
508
509 spin_unlock(&i->lock);
510
511 DEBUG_INTR("end.\n");
512
513 return IRQ_HANDLED;
514}
515
516/*
517 * To support ISA shared interrupts, we need to have one interrupt
518 * handler that ensures that the IRQ line has been deasserted
519 * before returning. Failing to do this will result in the IRQ
520 * line being stuck active, and, since ISA irqs are edge triggered,
521 * no more IRQs will be seen.
522 */
523static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
524{
525 spin_lock_irq(&i->lock);
526
527 if (!list_empty(i->head)) {
528 if (i->head == &up->list)
529 i->head = i->head->next;
530 list_del(&up->list);
531 } else {
532 BUG_ON(i->head != &up->list);
533 i->head = NULL;
534 }
535
536 spin_unlock_irq(&i->lock);
537}
538
539static int serial_link_irq_chain(struct uart_sio_port *up)
540{
541 struct irq_info *i = irq_lists + up->port.irq;
542 int ret, irq_flags = 0;
543
544 spin_lock_irq(&i->lock);
545
546 if (i->head) {
547 list_add(&up->list, i->head);
548 spin_unlock_irq(&i->lock);
549
550 ret = 0;
551 } else {
552 INIT_LIST_HEAD(&up->list);
553 i->head = &up->list;
554 spin_unlock_irq(&i->lock);
555
556 ret = request_irq(up->port.irq, m32r_sio_interrupt,
557 irq_flags, "SIO0-RX", i);
558 ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
559 irq_flags, "SIO0-TX", i);
560 if (ret < 0)
561 serial_do_unlink(i, up);
562 }
563
564 return ret;
565}
566
567static void serial_unlink_irq_chain(struct uart_sio_port *up)
568{
569 struct irq_info *i = irq_lists + up->port.irq;
570
571 BUG_ON(i->head == NULL);
572
573 if (list_empty(i->head)) {
574 free_irq(up->port.irq, i);
575 free_irq(up->port.irq + 1, i);
576 }
577
578 serial_do_unlink(i, up);
579}
580
581/*
582 * This function is used to handle ports that do not have an interrupt.
583 */
584static void m32r_sio_timeout(unsigned long data)
585{
586 struct uart_sio_port *up = (struct uart_sio_port *)data;
587 unsigned int timeout;
588 unsigned int sts;
589
590 sts = sio_in(up, SIOSTS);
591 if (sts & 0x5) {
592 spin_lock(&up->port.lock);
593 m32r_sio_handle_port(up, sts);
594 spin_unlock(&up->port.lock);
595 }
596
597 timeout = up->port.timeout;
598 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
599 mod_timer(&up->timer, jiffies + timeout);
600}
601
602static unsigned int m32r_sio_tx_empty(struct uart_port *port)
603{
604 struct uart_sio_port *up = (struct uart_sio_port *)port;
605 unsigned long flags;
606 unsigned int ret;
607
608 spin_lock_irqsave(&up->port.lock, flags);
609 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
610 spin_unlock_irqrestore(&up->port.lock, flags);
611
612 return ret;
613}
614
615static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
616{
617 return 0;
618}
619
620static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
621{
622
623}
624
625static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
626{
627
628}
629
630static int m32r_sio_startup(struct uart_port *port)
631{
632 struct uart_sio_port *up = (struct uart_sio_port *)port;
633 int retval;
634
635 sio_init();
636
637 /*
638 * If the "interrupt" for this port doesn't correspond with any
639 * hardware interrupt, we use a timer-based system. The original
640 * driver used to do this with IRQ0.
641 */
642 if (!is_real_interrupt(up->port.irq)) {
643 unsigned int timeout = up->port.timeout;
644
645 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
646
647 up->timer.data = (unsigned long)up;
648 mod_timer(&up->timer, jiffies + timeout);
649 } else {
650 retval = serial_link_irq_chain(up);
651 if (retval)
652 return retval;
653 }
654
655 /*
656 * Finally, enable interrupts. Note: Modem status interrupts
657 * are set via set_termios(), which will be occurring imminently
658 * anyway, so we don't enable them here.
659 * - M32R_SIO: 0x0c
660 * - M32R_PLDSIO: 0x04
661 */
662 up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
663 sio_out(up, SIOTRCR, up->ier);
664
665 /*
666 * And clear the interrupt registers again for luck.
667 */
668 sio_reset();
669
670 return 0;
671}
672
673static void m32r_sio_shutdown(struct uart_port *port)
674{
675 struct uart_sio_port *up = (struct uart_sio_port *)port;
676
677 /*
678 * Disable interrupts from this port
679 */
680 up->ier = 0;
681 sio_out(up, SIOTRCR, 0);
682
683 /*
684 * Disable break condition and FIFOs
685 */
686
687 sio_init();
688
689 if (!is_real_interrupt(up->port.irq))
690 del_timer_sync(&up->timer);
691 else
692 serial_unlink_irq_chain(up);
693}
694
695static unsigned int m32r_sio_get_divisor(struct uart_port *port,
696 unsigned int baud)
697{
698 return uart_get_divisor(port, baud);
699}
700
701static void m32r_sio_set_termios(struct uart_port *port,
702 struct ktermios *termios, struct ktermios *old)
703{
704 struct uart_sio_port *up = (struct uart_sio_port *)port;
705 unsigned char cval = 0;
706 unsigned long flags;
707 unsigned int baud, quot;
708
709 switch (termios->c_cflag & CSIZE) {
710 case CS5:
711 cval = UART_LCR_WLEN5;
712 break;
713 case CS6:
714 cval = UART_LCR_WLEN6;
715 break;
716 case CS7:
717 cval = UART_LCR_WLEN7;
718 break;
719 default:
720 case CS8:
721 cval = UART_LCR_WLEN8;
722 break;
723 }
724
725 if (termios->c_cflag & CSTOPB)
726 cval |= UART_LCR_STOP;
727 if (termios->c_cflag & PARENB)
728 cval |= UART_LCR_PARITY;
729 if (!(termios->c_cflag & PARODD))
730 cval |= UART_LCR_EPAR;
731#ifdef CMSPAR
732 if (termios->c_cflag & CMSPAR)
733 cval |= UART_LCR_SPAR;
734#endif
735
736 /*
737 * Ask the core to calculate the divisor for us.
738 */
739#ifdef CONFIG_SERIAL_M32R_PLDSIO
740 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
741#else
742 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
743#endif
744 quot = m32r_sio_get_divisor(port, baud);
745
746 /*
747 * Ok, we're now changing the port state. Do it with
748 * interrupts disabled.
749 */
750 spin_lock_irqsave(&up->port.lock, flags);
751
752 sio_set_baud_rate(baud);
753
754 /*
755 * Update the per-port timeout.
756 */
757 uart_update_timeout(port, termios->c_cflag, baud);
758
759 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
760 if (termios->c_iflag & INPCK)
761 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
762 if (termios->c_iflag & (BRKINT | PARMRK))
763 up->port.read_status_mask |= UART_LSR_BI;
764
765 /*
766 * Characteres to ignore
767 */
768 up->port.ignore_status_mask = 0;
769 if (termios->c_iflag & IGNPAR)
770 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
771 if (termios->c_iflag & IGNBRK) {
772 up->port.ignore_status_mask |= UART_LSR_BI;
773 /*
774 * If we're ignoring parity and break indicators,
775 * ignore overruns too (for real raw support).
776 */
777 if (termios->c_iflag & IGNPAR)
778 up->port.ignore_status_mask |= UART_LSR_OE;
779 }
780
781 /*
782 * ignore all characters if CREAD is not set
783 */
784 if ((termios->c_cflag & CREAD) == 0)
785 up->port.ignore_status_mask |= UART_LSR_DR;
786
787 /*
788 * CTS flow control flag and modem status interrupts
789 */
790 up->ier &= ~UART_IER_MSI;
791 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
792 up->ier |= UART_IER_MSI;
793
794 serial_out(up, UART_IER, up->ier);
795
796 up->lcr = cval; /* Save LCR */
797 spin_unlock_irqrestore(&up->port.lock, flags);
798}
799
800static void m32r_sio_pm(struct uart_port *port, unsigned int state,
801 unsigned int oldstate)
802{
803 struct uart_sio_port *up = (struct uart_sio_port *)port;
804
805 if (up->pm)
806 up->pm(port, state, oldstate);
807}
808
809/*
810 * Resource handling. This is complicated by the fact that resources
811 * depend on the port type. Maybe we should be claiming the standard
812 * 8250 ports, and then trying to get other resources as necessary?
813 */
814static int
815m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
816{
817 unsigned int size = 8 << up->port.regshift;
818#ifndef CONFIG_SERIAL_M32R_PLDSIO
819 unsigned long start;
820#endif
821 int ret = 0;
822
823 switch (up->port.iotype) {
824 case UPIO_MEM:
825 if (up->port.mapbase) {
826#ifdef CONFIG_SERIAL_M32R_PLDSIO
827 *res = request_mem_region(up->port.mapbase, size, "serial");
828#else
829 start = up->port.mapbase;
830 *res = request_mem_region(start, size, "serial");
831#endif
832 if (!*res)
833 ret = -EBUSY;
834 }
835 break;
836
837 case UPIO_PORT:
838 *res = request_region(up->port.iobase, size, "serial");
839 if (!*res)
840 ret = -EBUSY;
841 break;
842 }
843 return ret;
844}
845
846static void m32r_sio_release_port(struct uart_port *port)
847{
848 struct uart_sio_port *up = (struct uart_sio_port *)port;
849 unsigned long start, offset = 0, size = 0;
850
851 size <<= up->port.regshift;
852
853 switch (up->port.iotype) {
854 case UPIO_MEM:
855 if (up->port.mapbase) {
856 /*
857 * Unmap the area.
858 */
859 iounmap(up->port.membase);
860 up->port.membase = NULL;
861
862 start = up->port.mapbase;
863
864 if (size)
865 release_mem_region(start + offset, size);
866 release_mem_region(start, 8 << up->port.regshift);
867 }
868 break;
869
870 case UPIO_PORT:
871 start = up->port.iobase;
872
873 if (size)
874 release_region(start + offset, size);
875 release_region(start + offset, 8 << up->port.regshift);
876 break;
877
878 default:
879 break;
880 }
881}
882
883static int m32r_sio_request_port(struct uart_port *port)
884{
885 struct uart_sio_port *up = (struct uart_sio_port *)port;
886 struct resource *res = NULL;
887 int ret = 0;
888
889 ret = m32r_sio_request_std_resource(up, &res);
890
891 /*
892 * If we have a mapbase, then request that as well.
893 */
894 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
895 int size = res->end - res->start + 1;
896
897 up->port.membase = ioremap(up->port.mapbase, size);
898 if (!up->port.membase)
899 ret = -ENOMEM;
900 }
901
902 if (ret < 0) {
903 if (res)
904 release_resource(res);
905 }
906
907 return ret;
908}
909
910static void m32r_sio_config_port(struct uart_port *port, int flags)
911{
912 struct uart_sio_port *up = (struct uart_sio_port *)port;
913
914 spin_lock_irqsave(&up->port.lock, flags);
915
916 up->port.type = (PORT_M32R_SIO - PORT_M32R_BASE + 1);
917 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
918
919 spin_unlock_irqrestore(&up->port.lock, flags);
920}
921
922static int
923m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
924{
925 if (ser->irq >= nr_irqs || ser->irq < 0 ||
926 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
927 ser->type >= ARRAY_SIZE(uart_config))
928 return -EINVAL;
929 return 0;
930}
931
932static const char *
933m32r_sio_type(struct uart_port *port)
934{
935 int type = port->type;
936
937 if (type >= ARRAY_SIZE(uart_config))
938 type = 0;
939 return uart_config[type].name;
940}
941
942static struct uart_ops m32r_sio_pops = {
943 .tx_empty = m32r_sio_tx_empty,
944 .set_mctrl = m32r_sio_set_mctrl,
945 .get_mctrl = m32r_sio_get_mctrl,
946 .stop_tx = m32r_sio_stop_tx,
947 .start_tx = m32r_sio_start_tx,
948 .stop_rx = m32r_sio_stop_rx,
949 .enable_ms = m32r_sio_enable_ms,
950 .break_ctl = m32r_sio_break_ctl,
951 .startup = m32r_sio_startup,
952 .shutdown = m32r_sio_shutdown,
953 .set_termios = m32r_sio_set_termios,
954 .pm = m32r_sio_pm,
955 .type = m32r_sio_type,
956 .release_port = m32r_sio_release_port,
957 .request_port = m32r_sio_request_port,
958 .config_port = m32r_sio_config_port,
959 .verify_port = m32r_sio_verify_port,
960};
961
962static struct uart_sio_port m32r_sio_ports[UART_NR];
963
964static void __init m32r_sio_init_ports(void)
965{
966 struct uart_sio_port *up;
967 static int first = 1;
968 int i;
969
970 if (!first)
971 return;
972 first = 0;
973
974 for (i = 0, up = m32r_sio_ports; i < ARRAY_SIZE(old_serial_port);
975 i++, up++) {
976 up->port.iobase = old_serial_port[i].port;
977 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
978 up->port.uartclk = old_serial_port[i].baud_base * 16;
979 up->port.flags = old_serial_port[i].flags;
980 up->port.membase = old_serial_port[i].iomem_base;
981 up->port.iotype = old_serial_port[i].io_type;
982 up->port.regshift = old_serial_port[i].iomem_reg_shift;
983 up->port.ops = &m32r_sio_pops;
984 }
985}
986
987static void __init m32r_sio_register_ports(struct uart_driver *drv)
988{
989 int i;
990
991 m32r_sio_init_ports();
992
993 for (i = 0; i < UART_NR; i++) {
994 struct uart_sio_port *up = &m32r_sio_ports[i];
995
996 up->port.line = i;
997 up->port.ops = &m32r_sio_pops;
998 init_timer(&up->timer);
999 up->timer.function = m32r_sio_timeout;
1000
1001 /*
1002 * ALPHA_KLUDGE_MCR needs to be killed.
1003 */
1004 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1005 up->mcr_force = ALPHA_KLUDGE_MCR;
1006
1007 uart_add_one_port(drv, &up->port);
1008 }
1009}
1010
1011#ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
1012
1013/*
1014 * Wait for transmitter & holding register to empty
1015 */
1016static inline void wait_for_xmitr(struct uart_sio_port *up)
1017{
1018 unsigned int status, tmout = 10000;
1019
1020 /* Wait up to 10ms for the character(s) to be sent. */
1021 do {
1022 status = sio_in(up, SIOSTS);
1023
1024 if (--tmout == 0)
1025 break;
1026 udelay(1);
1027 } while ((status & UART_EMPTY) != UART_EMPTY);
1028
1029 /* Wait up to 1s for flow control if necessary */
1030 if (up->port.flags & UPF_CONS_FLOW) {
1031 tmout = 1000000;
1032 while (--tmout)
1033 udelay(1);
1034 }
1035}
1036
1037static void m32r_sio_console_putchar(struct uart_port *port, int ch)
1038{
1039 struct uart_sio_port *up = (struct uart_sio_port *)port;
1040
1041 wait_for_xmitr(up);
1042 sio_out(up, SIOTXB, ch);
1043}
1044
1045/*
1046 * Print a string to the serial port trying not to disturb
1047 * any possible real use of the port...
1048 *
1049 * The console_lock must be held when we get here.
1050 */
1051static void m32r_sio_console_write(struct console *co, const char *s,
1052 unsigned int count)
1053{
1054 struct uart_sio_port *up = &m32r_sio_ports[co->index];
1055 unsigned int ier;
1056
1057 /*
1058 * First save the UER then disable the interrupts
1059 */
1060 ier = sio_in(up, SIOTRCR);
1061 sio_out(up, SIOTRCR, 0);
1062
1063 uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
1064
1065 /*
1066 * Finally, wait for transmitter to become empty
1067 * and restore the IER
1068 */
1069 wait_for_xmitr(up);
1070 sio_out(up, SIOTRCR, ier);
1071}
1072
1073static int __init m32r_sio_console_setup(struct console *co, char *options)
1074{
1075 struct uart_port *port;
1076 int baud = 9600;
1077 int bits = 8;
1078 int parity = 'n';
1079 int flow = 'n';
1080
1081 /*
1082 * Check whether an invalid uart number has been specified, and
1083 * if so, search for the first available port that does have
1084 * console support.
1085 */
1086 if (co->index >= UART_NR)
1087 co->index = 0;
1088 port = &m32r_sio_ports[co->index].port;
1089
1090 /*
1091 * Temporary fix.
1092 */
1093 spin_lock_init(&port->lock);
1094
1095 if (options)
1096 uart_parse_options(options, &baud, &parity, &bits, &flow);
1097
1098 return uart_set_options(port, co, baud, parity, bits, flow);
1099}
1100
1101static struct uart_driver m32r_sio_reg;
1102static struct console m32r_sio_console = {
1103 .name = "ttyS",
1104 .write = m32r_sio_console_write,
1105 .device = uart_console_device,
1106 .setup = m32r_sio_console_setup,
1107 .flags = CON_PRINTBUFFER,
1108 .index = -1,
1109 .data = &m32r_sio_reg,
1110};
1111
1112static int __init m32r_sio_console_init(void)
1113{
1114 sio_reset();
1115 sio_init();
1116 m32r_sio_init_ports();
1117 register_console(&m32r_sio_console);
1118 return 0;
1119}
1120console_initcall(m32r_sio_console_init);
1121
1122#define M32R_SIO_CONSOLE &m32r_sio_console
1123#else
1124#define M32R_SIO_CONSOLE NULL
1125#endif
1126
1127static struct uart_driver m32r_sio_reg = {
1128 .owner = THIS_MODULE,
1129 .driver_name = "sio",
1130 .dev_name = "ttyS",
1131 .major = TTY_MAJOR,
1132 .minor = 64,
1133 .nr = UART_NR,
1134 .cons = M32R_SIO_CONSOLE,
1135};
1136
1137/**
1138 * m32r_sio_suspend_port - suspend one serial port
1139 * @line: serial line number
1140 *
1141 * Suspend one serial port.
1142 */
1143void m32r_sio_suspend_port(int line)
1144{
1145 uart_suspend_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1146}
1147
1148/**
1149 * m32r_sio_resume_port - resume one serial port
1150 * @line: serial line number
1151 *
1152 * Resume one serial port.
1153 */
1154void m32r_sio_resume_port(int line)
1155{
1156 uart_resume_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1157}
1158
1159static int __init m32r_sio_init(void)
1160{
1161 int ret, i;
1162
1163 printk(KERN_INFO "Serial: M32R SIO driver\n");
1164
1165 for (i = 0; i < nr_irqs; i++)
1166 spin_lock_init(&irq_lists[i].lock);
1167
1168 ret = uart_register_driver(&m32r_sio_reg);
1169 if (ret >= 0)
1170 m32r_sio_register_ports(&m32r_sio_reg);
1171
1172 return ret;
1173}
1174
1175static void __exit m32r_sio_exit(void)
1176{
1177 int i;
1178
1179 for (i = 0; i < UART_NR; i++)
1180 uart_remove_one_port(&m32r_sio_reg, &m32r_sio_ports[i].port);
1181
1182 uart_unregister_driver(&m32r_sio_reg);
1183}
1184
1185module_init(m32r_sio_init);
1186module_exit(m32r_sio_exit);
1187
1188EXPORT_SYMBOL(m32r_sio_suspend_port);
1189EXPORT_SYMBOL(m32r_sio_resume_port);
1190
1191MODULE_LICENSE("GPL");
1192MODULE_DESCRIPTION("Generic M32R SIO serial driver");
diff --git a/drivers/tty/serial/m32r_sio.h b/drivers/tty/serial/m32r_sio.h
new file mode 100644
index 000000000000..e9b7e11793b1
--- /dev/null
+++ b/drivers/tty/serial/m32r_sio.h
@@ -0,0 +1,48 @@
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
19struct m32r_sio_probe {
20 struct module *owner;
21 int (*pci_init_one)(struct pci_dev *dev);
22 void (*pci_remove_one)(struct pci_dev *dev);
23 void (*pnp_init)(void);
24};
25
26int m32r_sio_register_probe(struct m32r_sio_probe *probe);
27void m32r_sio_unregister_probe(struct m32r_sio_probe *probe);
28void m32r_sio_get_irq_map(unsigned int *map);
29void m32r_sio_suspend_port(int line);
30void m32r_sio_resume_port(int line);
31
32struct old_serial_port {
33 unsigned int uart;
34 unsigned int baud_base;
35 unsigned int port;
36 unsigned int irq;
37 unsigned int flags;
38 unsigned char io_type;
39 unsigned char __iomem *iomem_base;
40 unsigned short iomem_reg_shift;
41};
42
43#define _INLINE_ inline
44
45#define PROBE_RSA (1 << 0)
46#define PROBE_ANY (~0)
47
48#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
diff --git a/drivers/tty/serial/m32r_sio_reg.h b/drivers/tty/serial/m32r_sio_reg.h
new file mode 100644
index 000000000000..4671473793e3
--- /dev/null
+++ b/drivers/tty/serial/m32r_sio_reg.h
@@ -0,0 +1,152 @@
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
19#ifdef CONFIG_SERIAL_M32R_PLDSIO
20
21#define SIOCR 0x000
22#define SIOMOD0 0x002
23#define SIOMOD1 0x004
24#define SIOSTS 0x006
25#define SIOTRCR 0x008
26#define SIOBAUR 0x00a
27// #define SIORBAUR 0x018
28#define SIOTXB 0x00c
29#define SIORXB 0x00e
30
31#define UART_RX ((unsigned long) PLD_ESIO0RXB)
32 /* In: Receive buffer (DLAB=0) */
33#define UART_TX ((unsigned long) PLD_ESIO0TXB)
34 /* Out: Transmit buffer (DLAB=0) */
35#define UART_DLL 0 /* Out: Divisor Latch Low (DLAB=1) */
36#define UART_TRG 0 /* (LCR=BF) FCTR bit 7 selects Rx or Tx
37 * In: Fifo count
38 * Out: Fifo custom trigger levels
39 * XR16C85x only */
40
41#define UART_DLM 0 /* Out: Divisor Latch High (DLAB=1) */
42#define UART_IER ((unsigned long) PLD_ESIO0INTCR)
43 /* Out: Interrupt Enable Register */
44#define UART_FCTR 0 /* (LCR=BF) Feature Control Register
45 * XR16C85x only */
46
47#define UART_IIR 0 /* In: Interrupt ID Register */
48#define UART_FCR 0 /* Out: FIFO Control Register */
49#define UART_EFR 0 /* I/O: Extended Features Register */
50 /* (DLAB=1, 16C660 only) */
51
52#define UART_LCR 0 /* Out: Line Control Register */
53#define UART_MCR 0 /* Out: Modem Control Register */
54#define UART_LSR ((unsigned long) PLD_ESIO0STS)
55 /* In: Line Status Register */
56#define UART_MSR 0 /* In: Modem Status Register */
57#define UART_SCR 0 /* I/O: Scratch Register */
58#define UART_EMSR 0 /* (LCR=BF) Extended Mode Select Register
59 * FCTR bit 6 selects SCR or EMSR
60 * XR16c85x only */
61
62#else /* not CONFIG_SERIAL_M32R_PLDSIO */
63
64#define SIOCR 0x000
65#define SIOMOD0 0x004
66#define SIOMOD1 0x008
67#define SIOSTS 0x00c
68#define SIOTRCR 0x010
69#define SIOBAUR 0x014
70#define SIORBAUR 0x018
71#define SIOTXB 0x01c
72#define SIORXB 0x020
73
74#define UART_RX M32R_SIO0_RXB_PORTL /* In: Receive buffer (DLAB=0) */
75#define UART_TX M32R_SIO0_TXB_PORTL /* Out: Transmit buffer (DLAB=0) */
76#define UART_DLL 0 /* Out: Divisor Latch Low (DLAB=1) */
77#define UART_TRG 0 /* (LCR=BF) FCTR bit 7 selects Rx or Tx
78 * In: Fifo count
79 * Out: Fifo custom trigger levels
80 * XR16C85x only */
81
82#define UART_DLM 0 /* Out: Divisor Latch High (DLAB=1) */
83#define UART_IER M32R_SIO0_TRCR_PORTL /* Out: Interrupt Enable Register */
84#define UART_FCTR 0 /* (LCR=BF) Feature Control Register
85 * XR16C85x only */
86
87#define UART_IIR 0 /* In: Interrupt ID Register */
88#define UART_FCR 0 /* Out: FIFO Control Register */
89#define UART_EFR 0 /* I/O: Extended Features Register */
90 /* (DLAB=1, 16C660 only) */
91
92#define UART_LCR 0 /* Out: Line Control Register */
93#define UART_MCR 0 /* Out: Modem Control Register */
94#define UART_LSR M32R_SIO0_STS_PORTL /* In: Line Status Register */
95#define UART_MSR 0 /* In: Modem Status Register */
96#define UART_SCR 0 /* I/O: Scratch Register */
97#define UART_EMSR 0 /* (LCR=BF) Extended Mode Select Register
98 * FCTR bit 6 selects SCR or EMSR
99 * XR16c85x only */
100
101#endif /* CONFIG_SERIAL_M32R_PLDSIO */
102
103#define UART_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
104
105/*
106 * These are the definitions for the Line Control Register
107 *
108 * Note: if the word length is 5 bits (UART_LCR_WLEN5), then setting
109 * UART_LCR_STOP will select 1.5 stop bits, not 2 stop bits.
110 */
111#define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
112#define UART_LCR_SBC 0x40 /* Set break control */
113#define UART_LCR_SPAR 0x20 /* Stick parity (?) */
114#define UART_LCR_EPAR 0x10 /* Even parity select */
115#define UART_LCR_PARITY 0x08 /* Parity Enable */
116#define UART_LCR_STOP 0x04 /* Stop bits: 0=1 stop bit, 1= 2 stop bits */
117#define UART_LCR_WLEN5 0x00 /* Wordlength: 5 bits */
118#define UART_LCR_WLEN6 0x01 /* Wordlength: 6 bits */
119#define UART_LCR_WLEN7 0x02 /* Wordlength: 7 bits */
120#define UART_LCR_WLEN8 0x03 /* Wordlength: 8 bits */
121
122/*
123 * These are the definitions for the Line Status Register
124 */
125#define UART_LSR_TEMT 0x02 /* Transmitter empty */
126#define UART_LSR_THRE 0x01 /* Transmit-hold-register empty */
127#define UART_LSR_BI 0x00 /* Break interrupt indicator */
128#define UART_LSR_FE 0x80 /* Frame error indicator */
129#define UART_LSR_PE 0x40 /* Parity error indicator */
130#define UART_LSR_OE 0x20 /* Overrun error indicator */
131#define UART_LSR_DR 0x04 /* Receiver data ready */
132
133/*
134 * These are the definitions for the Interrupt Identification Register
135 */
136#define UART_IIR_NO_INT 0x01 /* No interrupts pending */
137#define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
138
139#define UART_IIR_MSI 0x00 /* Modem status interrupt */
140#define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
141#define UART_IIR_RDI 0x04 /* Receiver data interrupt */
142#define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
143
144/*
145 * These are the definitions for the Interrupt Enable Register
146 */
147#define UART_IER_MSI 0x00 /* Enable Modem status interrupt */
148#define UART_IER_RLSI 0x08 /* Enable receiver line status interrupt */
149#define UART_IER_THRI 0x03 /* Enable Transmitter holding register int. */
150#define UART_IER_RDI 0x04 /* Enable receiver data interrupt */
151
152#endif /* _M32R_SIO_REG_H */
diff --git a/drivers/tty/serial/max3100.c b/drivers/tty/serial/max3100.c
new file mode 100644
index 000000000000..beb1afa27d8d
--- /dev/null
+++ b/drivers/tty/serial/max3100.c
@@ -0,0 +1,926 @@
1/*
2 *
3 * Copyright (C) 2008 Christian Pellegrin <chripell@evolware.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 *
11 * Notes: the MAX3100 doesn't provide an interrupt on CTS so we have
12 * to use polling for flow control. TX empty IRQ is unusable, since
13 * writing conf clears FIFO buffer and we cannot have this interrupt
14 * always asking us for attention.
15 *
16 * Example platform data:
17
18 static struct plat_max3100 max3100_plat_data = {
19 .loopback = 0,
20 .crystal = 0,
21 .poll_time = 100,
22 };
23
24 static struct spi_board_info spi_board_info[] = {
25 {
26 .modalias = "max3100",
27 .platform_data = &max3100_plat_data,
28 .irq = IRQ_EINT12,
29 .max_speed_hz = 5*1000*1000,
30 .chip_select = 0,
31 },
32 };
33
34 * The initial minor number is 209 in the low-density serial port:
35 * mknod /dev/ttyMAX0 c 204 209
36 */
37
38#define MAX3100_MAJOR 204
39#define MAX3100_MINOR 209
40/* 4 MAX3100s should be enough for everyone */
41#define MAX_MAX3100 4
42
43#include <linux/delay.h>
44#include <linux/slab.h>
45#include <linux/device.h>
46#include <linux/serial_core.h>
47#include <linux/serial.h>
48#include <linux/spi/spi.h>
49#include <linux/freezer.h>
50
51#include <linux/serial_max3100.h>
52
53#define MAX3100_C (1<<14)
54#define MAX3100_D (0<<14)
55#define MAX3100_W (1<<15)
56#define MAX3100_RX (0<<15)
57
58#define MAX3100_WC (MAX3100_W | MAX3100_C)
59#define MAX3100_RC (MAX3100_RX | MAX3100_C)
60#define MAX3100_WD (MAX3100_W | MAX3100_D)
61#define MAX3100_RD (MAX3100_RX | MAX3100_D)
62#define MAX3100_CMD (3 << 14)
63
64#define MAX3100_T (1<<14)
65#define MAX3100_R (1<<15)
66
67#define MAX3100_FEN (1<<13)
68#define MAX3100_SHDN (1<<12)
69#define MAX3100_TM (1<<11)
70#define MAX3100_RM (1<<10)
71#define MAX3100_PM (1<<9)
72#define MAX3100_RAM (1<<8)
73#define MAX3100_IR (1<<7)
74#define MAX3100_ST (1<<6)
75#define MAX3100_PE (1<<5)
76#define MAX3100_L (1<<4)
77#define MAX3100_BAUD (0xf)
78
79#define MAX3100_TE (1<<10)
80#define MAX3100_RAFE (1<<10)
81#define MAX3100_RTS (1<<9)
82#define MAX3100_CTS (1<<9)
83#define MAX3100_PT (1<<8)
84#define MAX3100_DATA (0xff)
85
86#define MAX3100_RT (MAX3100_R | MAX3100_T)
87#define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
88
89/* the following simulate a status reg for ignore_status_mask */
90#define MAX3100_STATUS_PE 1
91#define MAX3100_STATUS_FE 2
92#define MAX3100_STATUS_OE 4
93
94struct max3100_port {
95 struct uart_port port;
96 struct spi_device *spi;
97
98 int cts; /* last CTS received for flow ctrl */
99 int tx_empty; /* last TX empty bit */
100
101 spinlock_t conf_lock; /* shared data */
102 int conf_commit; /* need to make changes */
103 int conf; /* configuration for the MAX31000
104 * (bits 0-7, bits 8-11 are irqs) */
105 int rts_commit; /* need to change rts */
106 int rts; /* rts status */
107 int baud; /* current baud rate */
108
109 int parity; /* keeps track if we should send parity */
110#define MAX3100_PARITY_ON 1
111#define MAX3100_PARITY_ODD 2
112#define MAX3100_7BIT 4
113 int rx_enabled; /* if we should rx chars */
114
115 int irq; /* irq assigned to the max3100 */
116
117 int minor; /* minor number */
118 int crystal; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
119 int loopback; /* 1 if we are in loopback mode */
120
121 /* for handling irqs: need workqueue since we do spi_sync */
122 struct workqueue_struct *workqueue;
123 struct work_struct work;
124 /* set to 1 to make the workhandler exit as soon as possible */
125 int force_end_work;
126 /* need to know we are suspending to avoid deadlock on workqueue */
127 int suspending;
128
129 /* hook for suspending MAX3100 via dedicated pin */
130 void (*max3100_hw_suspend) (int suspend);
131
132 /* poll time (in ms) for ctrl lines */
133 int poll_time;
134 /* and its timer */
135 struct timer_list timer;
136};
137
138static struct max3100_port *max3100s[MAX_MAX3100]; /* the chips */
139static DEFINE_MUTEX(max3100s_lock); /* race on probe */
140
141static int max3100_do_parity(struct max3100_port *s, u16 c)
142{
143 int parity;
144
145 if (s->parity & MAX3100_PARITY_ODD)
146 parity = 1;
147 else
148 parity = 0;
149
150 if (s->parity & MAX3100_7BIT)
151 c &= 0x7f;
152 else
153 c &= 0xff;
154
155 parity = parity ^ (hweight8(c) & 1);
156 return parity;
157}
158
159static int max3100_check_parity(struct max3100_port *s, u16 c)
160{
161 return max3100_do_parity(s, c) == ((c >> 8) & 1);
162}
163
164static void max3100_calc_parity(struct max3100_port *s, u16 *c)
165{
166 if (s->parity & MAX3100_7BIT)
167 *c &= 0x7f;
168 else
169 *c &= 0xff;
170
171 if (s->parity & MAX3100_PARITY_ON)
172 *c |= max3100_do_parity(s, *c) << 8;
173}
174
175static void max3100_work(struct work_struct *w);
176
177static void max3100_dowork(struct max3100_port *s)
178{
179 if (!s->force_end_work && !work_pending(&s->work) &&
180 !freezing(current) && !s->suspending)
181 queue_work(s->workqueue, &s->work);
182}
183
184static void max3100_timeout(unsigned long data)
185{
186 struct max3100_port *s = (struct max3100_port *)data;
187
188 if (s->port.state) {
189 max3100_dowork(s);
190 mod_timer(&s->timer, jiffies + s->poll_time);
191 }
192}
193
194static int max3100_sr(struct max3100_port *s, u16 tx, u16 *rx)
195{
196 struct spi_message message;
197 u16 etx, erx;
198 int status;
199 struct spi_transfer tran = {
200 .tx_buf = &etx,
201 .rx_buf = &erx,
202 .len = 2,
203 };
204
205 etx = cpu_to_be16(tx);
206 spi_message_init(&message);
207 spi_message_add_tail(&tran, &message);
208 status = spi_sync(s->spi, &message);
209 if (status) {
210 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
211 return -EIO;
212 }
213 *rx = be16_to_cpu(erx);
214 s->tx_empty = (*rx & MAX3100_T) > 0;
215 dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx);
216 return 0;
217}
218
219static int max3100_handlerx(struct max3100_port *s, u16 rx)
220{
221 unsigned int ch, flg, status = 0;
222 int ret = 0, cts;
223
224 if (rx & MAX3100_R && s->rx_enabled) {
225 dev_dbg(&s->spi->dev, "%s\n", __func__);
226 ch = rx & (s->parity & MAX3100_7BIT ? 0x7f : 0xff);
227 if (rx & MAX3100_RAFE) {
228 s->port.icount.frame++;
229 flg = TTY_FRAME;
230 status |= MAX3100_STATUS_FE;
231 } else {
232 if (s->parity & MAX3100_PARITY_ON) {
233 if (max3100_check_parity(s, rx)) {
234 s->port.icount.rx++;
235 flg = TTY_NORMAL;
236 } else {
237 s->port.icount.parity++;
238 flg = TTY_PARITY;
239 status |= MAX3100_STATUS_PE;
240 }
241 } else {
242 s->port.icount.rx++;
243 flg = TTY_NORMAL;
244 }
245 }
246 uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg);
247 ret = 1;
248 }
249
250 cts = (rx & MAX3100_CTS) > 0;
251 if (s->cts != cts) {
252 s->cts = cts;
253 uart_handle_cts_change(&s->port, cts ? TIOCM_CTS : 0);
254 }
255
256 return ret;
257}
258
259static void max3100_work(struct work_struct *w)
260{
261 struct max3100_port *s = container_of(w, struct max3100_port, work);
262 int rxchars;
263 u16 tx, rx;
264 int conf, cconf, rts, crts;
265 struct circ_buf *xmit = &s->port.state->xmit;
266
267 dev_dbg(&s->spi->dev, "%s\n", __func__);
268
269 rxchars = 0;
270 do {
271 spin_lock(&s->conf_lock);
272 conf = s->conf;
273 cconf = s->conf_commit;
274 s->conf_commit = 0;
275 rts = s->rts;
276 crts = s->rts_commit;
277 s->rts_commit = 0;
278 spin_unlock(&s->conf_lock);
279 if (cconf)
280 max3100_sr(s, MAX3100_WC | conf, &rx);
281 if (crts) {
282 max3100_sr(s, MAX3100_WD | MAX3100_TE |
283 (s->rts ? MAX3100_RTS : 0), &rx);
284 rxchars += max3100_handlerx(s, rx);
285 }
286
287 max3100_sr(s, MAX3100_RD, &rx);
288 rxchars += max3100_handlerx(s, rx);
289
290 if (rx & MAX3100_T) {
291 tx = 0xffff;
292 if (s->port.x_char) {
293 tx = s->port.x_char;
294 s->port.icount.tx++;
295 s->port.x_char = 0;
296 } else if (!uart_circ_empty(xmit) &&
297 !uart_tx_stopped(&s->port)) {
298 tx = xmit->buf[xmit->tail];
299 xmit->tail = (xmit->tail + 1) &
300 (UART_XMIT_SIZE - 1);
301 s->port.icount.tx++;
302 }
303 if (tx != 0xffff) {
304 max3100_calc_parity(s, &tx);
305 tx |= MAX3100_WD | (s->rts ? MAX3100_RTS : 0);
306 max3100_sr(s, tx, &rx);
307 rxchars += max3100_handlerx(s, rx);
308 }
309 }
310
311 if (rxchars > 16 && s->port.state->port.tty != NULL) {
312 tty_flip_buffer_push(s->port.state->port.tty);
313 rxchars = 0;
314 }
315 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
316 uart_write_wakeup(&s->port);
317
318 } while (!s->force_end_work &&
319 !freezing(current) &&
320 ((rx & MAX3100_R) ||
321 (!uart_circ_empty(xmit) &&
322 !uart_tx_stopped(&s->port))));
323
324 if (rxchars > 0 && s->port.state->port.tty != NULL)
325 tty_flip_buffer_push(s->port.state->port.tty);
326}
327
328static irqreturn_t max3100_irq(int irqno, void *dev_id)
329{
330 struct max3100_port *s = dev_id;
331
332 dev_dbg(&s->spi->dev, "%s\n", __func__);
333
334 max3100_dowork(s);
335 return IRQ_HANDLED;
336}
337
338static void max3100_enable_ms(struct uart_port *port)
339{
340 struct max3100_port *s = container_of(port,
341 struct max3100_port,
342 port);
343
344 if (s->poll_time > 0)
345 mod_timer(&s->timer, jiffies);
346 dev_dbg(&s->spi->dev, "%s\n", __func__);
347}
348
349static void max3100_start_tx(struct uart_port *port)
350{
351 struct max3100_port *s = container_of(port,
352 struct max3100_port,
353 port);
354
355 dev_dbg(&s->spi->dev, "%s\n", __func__);
356
357 max3100_dowork(s);
358}
359
360static void max3100_stop_rx(struct uart_port *port)
361{
362 struct max3100_port *s = container_of(port,
363 struct max3100_port,
364 port);
365
366 dev_dbg(&s->spi->dev, "%s\n", __func__);
367
368 s->rx_enabled = 0;
369 spin_lock(&s->conf_lock);
370 s->conf &= ~MAX3100_RM;
371 s->conf_commit = 1;
372 spin_unlock(&s->conf_lock);
373 max3100_dowork(s);
374}
375
376static unsigned int max3100_tx_empty(struct uart_port *port)
377{
378 struct max3100_port *s = container_of(port,
379 struct max3100_port,
380 port);
381
382 dev_dbg(&s->spi->dev, "%s\n", __func__);
383
384 /* may not be truly up-to-date */
385 max3100_dowork(s);
386 return s->tx_empty;
387}
388
389static unsigned int max3100_get_mctrl(struct uart_port *port)
390{
391 struct max3100_port *s = container_of(port,
392 struct max3100_port,
393 port);
394
395 dev_dbg(&s->spi->dev, "%s\n", __func__);
396
397 /* may not be truly up-to-date */
398 max3100_dowork(s);
399 /* always assert DCD and DSR since these lines are not wired */
400 return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
401}
402
403static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl)
404{
405 struct max3100_port *s = container_of(port,
406 struct max3100_port,
407 port);
408 int rts;
409
410 dev_dbg(&s->spi->dev, "%s\n", __func__);
411
412 rts = (mctrl & TIOCM_RTS) > 0;
413
414 spin_lock(&s->conf_lock);
415 if (s->rts != rts) {
416 s->rts = rts;
417 s->rts_commit = 1;
418 max3100_dowork(s);
419 }
420 spin_unlock(&s->conf_lock);
421}
422
423static void
424max3100_set_termios(struct uart_port *port, struct ktermios *termios,
425 struct ktermios *old)
426{
427 struct max3100_port *s = container_of(port,
428 struct max3100_port,
429 port);
430 int baud = 0;
431 unsigned cflag;
432 u32 param_new, param_mask, parity = 0;
433
434 dev_dbg(&s->spi->dev, "%s\n", __func__);
435
436 cflag = termios->c_cflag;
437 param_new = 0;
438 param_mask = 0;
439
440 baud = tty_termios_baud_rate(termios);
441 param_new = s->conf & MAX3100_BAUD;
442 switch (baud) {
443 case 300:
444 if (s->crystal)
445 baud = s->baud;
446 else
447 param_new = 15;
448 break;
449 case 600:
450 param_new = 14 + s->crystal;
451 break;
452 case 1200:
453 param_new = 13 + s->crystal;
454 break;
455 case 2400:
456 param_new = 12 + s->crystal;
457 break;
458 case 4800:
459 param_new = 11 + s->crystal;
460 break;
461 case 9600:
462 param_new = 10 + s->crystal;
463 break;
464 case 19200:
465 param_new = 9 + s->crystal;
466 break;
467 case 38400:
468 param_new = 8 + s->crystal;
469 break;
470 case 57600:
471 param_new = 1 + s->crystal;
472 break;
473 case 115200:
474 param_new = 0 + s->crystal;
475 break;
476 case 230400:
477 if (s->crystal)
478 param_new = 0;
479 else
480 baud = s->baud;
481 break;
482 default:
483 baud = s->baud;
484 }
485 tty_termios_encode_baud_rate(termios, baud, baud);
486 s->baud = baud;
487 param_mask |= MAX3100_BAUD;
488
489 if ((cflag & CSIZE) == CS8) {
490 param_new &= ~MAX3100_L;
491 parity &= ~MAX3100_7BIT;
492 } else {
493 param_new |= MAX3100_L;
494 parity |= MAX3100_7BIT;
495 cflag = (cflag & ~CSIZE) | CS7;
496 }
497 param_mask |= MAX3100_L;
498
499 if (cflag & CSTOPB)
500 param_new |= MAX3100_ST;
501 else
502 param_new &= ~MAX3100_ST;
503 param_mask |= MAX3100_ST;
504
505 if (cflag & PARENB) {
506 param_new |= MAX3100_PE;
507 parity |= MAX3100_PARITY_ON;
508 } else {
509 param_new &= ~MAX3100_PE;
510 parity &= ~MAX3100_PARITY_ON;
511 }
512 param_mask |= MAX3100_PE;
513
514 if (cflag & PARODD)
515 parity |= MAX3100_PARITY_ODD;
516 else
517 parity &= ~MAX3100_PARITY_ODD;
518
519 /* mask termios capabilities we don't support */
520 cflag &= ~CMSPAR;
521 termios->c_cflag = cflag;
522
523 s->port.ignore_status_mask = 0;
524 if (termios->c_iflag & IGNPAR)
525 s->port.ignore_status_mask |=
526 MAX3100_STATUS_PE | MAX3100_STATUS_FE |
527 MAX3100_STATUS_OE;
528
529 /* we are sending char from a workqueue so enable */
530 s->port.state->port.tty->low_latency = 1;
531
532 if (s->poll_time > 0)
533 del_timer_sync(&s->timer);
534
535 uart_update_timeout(port, termios->c_cflag, baud);
536
537 spin_lock(&s->conf_lock);
538 s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
539 s->conf_commit = 1;
540 s->parity = parity;
541 spin_unlock(&s->conf_lock);
542 max3100_dowork(s);
543
544 if (UART_ENABLE_MS(&s->port, termios->c_cflag))
545 max3100_enable_ms(&s->port);
546}
547
548static void max3100_shutdown(struct uart_port *port)
549{
550 struct max3100_port *s = container_of(port,
551 struct max3100_port,
552 port);
553
554 dev_dbg(&s->spi->dev, "%s\n", __func__);
555
556 if (s->suspending)
557 return;
558
559 s->force_end_work = 1;
560
561 if (s->poll_time > 0)
562 del_timer_sync(&s->timer);
563
564 if (s->workqueue) {
565 flush_workqueue(s->workqueue);
566 destroy_workqueue(s->workqueue);
567 s->workqueue = NULL;
568 }
569 if (s->irq)
570 free_irq(s->irq, s);
571
572 /* set shutdown mode to save power */
573 if (s->max3100_hw_suspend)
574 s->max3100_hw_suspend(1);
575 else {
576 u16 tx, rx;
577
578 tx = MAX3100_WC | MAX3100_SHDN;
579 max3100_sr(s, tx, &rx);
580 }
581}
582
583static int max3100_startup(struct uart_port *port)
584{
585 struct max3100_port *s = container_of(port,
586 struct max3100_port,
587 port);
588 char b[12];
589
590 dev_dbg(&s->spi->dev, "%s\n", __func__);
591
592 s->conf = MAX3100_RM;
593 s->baud = s->crystal ? 230400 : 115200;
594 s->rx_enabled = 1;
595
596 if (s->suspending)
597 return 0;
598
599 s->force_end_work = 0;
600 s->parity = 0;
601 s->rts = 0;
602
603 sprintf(b, "max3100-%d", s->minor);
604 s->workqueue = create_freezeable_workqueue(b);
605 if (!s->workqueue) {
606 dev_warn(&s->spi->dev, "cannot create workqueue\n");
607 return -EBUSY;
608 }
609 INIT_WORK(&s->work, max3100_work);
610
611 if (request_irq(s->irq, max3100_irq,
612 IRQF_TRIGGER_FALLING, "max3100", s) < 0) {
613 dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
614 s->irq = 0;
615 destroy_workqueue(s->workqueue);
616 s->workqueue = NULL;
617 return -EBUSY;
618 }
619
620 if (s->loopback) {
621 u16 tx, rx;
622 tx = 0x4001;
623 max3100_sr(s, tx, &rx);
624 }
625
626 if (s->max3100_hw_suspend)
627 s->max3100_hw_suspend(0);
628 s->conf_commit = 1;
629 max3100_dowork(s);
630 /* wait for clock to settle */
631 msleep(50);
632
633 max3100_enable_ms(&s->port);
634
635 return 0;
636}
637
638static const char *max3100_type(struct uart_port *port)
639{
640 struct max3100_port *s = container_of(port,
641 struct max3100_port,
642 port);
643
644 dev_dbg(&s->spi->dev, "%s\n", __func__);
645
646 return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
647}
648
649static void max3100_release_port(struct uart_port *port)
650{
651 struct max3100_port *s = container_of(port,
652 struct max3100_port,
653 port);
654
655 dev_dbg(&s->spi->dev, "%s\n", __func__);
656}
657
658static void max3100_config_port(struct uart_port *port, int flags)
659{
660 struct max3100_port *s = container_of(port,
661 struct max3100_port,
662 port);
663
664 dev_dbg(&s->spi->dev, "%s\n", __func__);
665
666 if (flags & UART_CONFIG_TYPE)
667 s->port.type = PORT_MAX3100;
668}
669
670static int max3100_verify_port(struct uart_port *port,
671 struct serial_struct *ser)
672{
673 struct max3100_port *s = container_of(port,
674 struct max3100_port,
675 port);
676 int ret = -EINVAL;
677
678 dev_dbg(&s->spi->dev, "%s\n", __func__);
679
680 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100)
681 ret = 0;
682 return ret;
683}
684
685static void max3100_stop_tx(struct uart_port *port)
686{
687 struct max3100_port *s = container_of(port,
688 struct max3100_port,
689 port);
690
691 dev_dbg(&s->spi->dev, "%s\n", __func__);
692}
693
694static int max3100_request_port(struct uart_port *port)
695{
696 struct max3100_port *s = container_of(port,
697 struct max3100_port,
698 port);
699
700 dev_dbg(&s->spi->dev, "%s\n", __func__);
701 return 0;
702}
703
704static void max3100_break_ctl(struct uart_port *port, int break_state)
705{
706 struct max3100_port *s = container_of(port,
707 struct max3100_port,
708 port);
709
710 dev_dbg(&s->spi->dev, "%s\n", __func__);
711}
712
713static struct uart_ops max3100_ops = {
714 .tx_empty = max3100_tx_empty,
715 .set_mctrl = max3100_set_mctrl,
716 .get_mctrl = max3100_get_mctrl,
717 .stop_tx = max3100_stop_tx,
718 .start_tx = max3100_start_tx,
719 .stop_rx = max3100_stop_rx,
720 .enable_ms = max3100_enable_ms,
721 .break_ctl = max3100_break_ctl,
722 .startup = max3100_startup,
723 .shutdown = max3100_shutdown,
724 .set_termios = max3100_set_termios,
725 .type = max3100_type,
726 .release_port = max3100_release_port,
727 .request_port = max3100_request_port,
728 .config_port = max3100_config_port,
729 .verify_port = max3100_verify_port,
730};
731
732static struct uart_driver max3100_uart_driver = {
733 .owner = THIS_MODULE,
734 .driver_name = "ttyMAX",
735 .dev_name = "ttyMAX",
736 .major = MAX3100_MAJOR,
737 .minor = MAX3100_MINOR,
738 .nr = MAX_MAX3100,
739};
740static int uart_driver_registered;
741
742static int __devinit max3100_probe(struct spi_device *spi)
743{
744 int i, retval;
745 struct plat_max3100 *pdata;
746 u16 tx, rx;
747
748 mutex_lock(&max3100s_lock);
749
750 if (!uart_driver_registered) {
751 uart_driver_registered = 1;
752 retval = uart_register_driver(&max3100_uart_driver);
753 if (retval) {
754 printk(KERN_ERR "Couldn't register max3100 uart driver\n");
755 mutex_unlock(&max3100s_lock);
756 return retval;
757 }
758 }
759
760 for (i = 0; i < MAX_MAX3100; i++)
761 if (!max3100s[i])
762 break;
763 if (i == MAX_MAX3100) {
764 dev_warn(&spi->dev, "too many MAX3100 chips\n");
765 mutex_unlock(&max3100s_lock);
766 return -ENOMEM;
767 }
768
769 max3100s[i] = kzalloc(sizeof(struct max3100_port), GFP_KERNEL);
770 if (!max3100s[i]) {
771 dev_warn(&spi->dev,
772 "kmalloc for max3100 structure %d failed!\n", i);
773 mutex_unlock(&max3100s_lock);
774 return -ENOMEM;
775 }
776 max3100s[i]->spi = spi;
777 max3100s[i]->irq = spi->irq;
778 spin_lock_init(&max3100s[i]->conf_lock);
779 dev_set_drvdata(&spi->dev, max3100s[i]);
780 pdata = spi->dev.platform_data;
781 max3100s[i]->crystal = pdata->crystal;
782 max3100s[i]->loopback = pdata->loopback;
783 max3100s[i]->poll_time = pdata->poll_time * HZ / 1000;
784 if (pdata->poll_time > 0 && max3100s[i]->poll_time == 0)
785 max3100s[i]->poll_time = 1;
786 max3100s[i]->max3100_hw_suspend = pdata->max3100_hw_suspend;
787 max3100s[i]->minor = i;
788 init_timer(&max3100s[i]->timer);
789 max3100s[i]->timer.function = max3100_timeout;
790 max3100s[i]->timer.data = (unsigned long) max3100s[i];
791
792 dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i);
793 max3100s[i]->port.irq = max3100s[i]->irq;
794 max3100s[i]->port.uartclk = max3100s[i]->crystal ? 3686400 : 1843200;
795 max3100s[i]->port.fifosize = 16;
796 max3100s[i]->port.ops = &max3100_ops;
797 max3100s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
798 max3100s[i]->port.line = i;
799 max3100s[i]->port.type = PORT_MAX3100;
800 max3100s[i]->port.dev = &spi->dev;
801 retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
802 if (retval < 0)
803 dev_warn(&spi->dev,
804 "uart_add_one_port failed for line %d with error %d\n",
805 i, retval);
806
807 /* set shutdown mode to save power. Will be woken-up on open */
808 if (max3100s[i]->max3100_hw_suspend)
809 max3100s[i]->max3100_hw_suspend(1);
810 else {
811 tx = MAX3100_WC | MAX3100_SHDN;
812 max3100_sr(max3100s[i], tx, &rx);
813 }
814 mutex_unlock(&max3100s_lock);
815 return 0;
816}
817
818static int __devexit max3100_remove(struct spi_device *spi)
819{
820 struct max3100_port *s = dev_get_drvdata(&spi->dev);
821 int i;
822
823 mutex_lock(&max3100s_lock);
824
825 /* find out the index for the chip we are removing */
826 for (i = 0; i < MAX_MAX3100; i++)
827 if (max3100s[i] == s)
828 break;
829
830 dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
831 uart_remove_one_port(&max3100_uart_driver, &max3100s[i]->port);
832 kfree(max3100s[i]);
833 max3100s[i] = NULL;
834
835 /* check if this is the last chip we have */
836 for (i = 0; i < MAX_MAX3100; i++)
837 if (max3100s[i]) {
838 mutex_unlock(&max3100s_lock);
839 return 0;
840 }
841 pr_debug("removing max3100 driver\n");
842 uart_unregister_driver(&max3100_uart_driver);
843
844 mutex_unlock(&max3100s_lock);
845 return 0;
846}
847
848#ifdef CONFIG_PM
849
850static int max3100_suspend(struct spi_device *spi, pm_message_t state)
851{
852 struct max3100_port *s = dev_get_drvdata(&spi->dev);
853
854 dev_dbg(&s->spi->dev, "%s\n", __func__);
855
856 disable_irq(s->irq);
857
858 s->suspending = 1;
859 uart_suspend_port(&max3100_uart_driver, &s->port);
860
861 if (s->max3100_hw_suspend)
862 s->max3100_hw_suspend(1);
863 else {
864 /* no HW suspend, so do SW one */
865 u16 tx, rx;
866
867 tx = MAX3100_WC | MAX3100_SHDN;
868 max3100_sr(s, tx, &rx);
869 }
870 return 0;
871}
872
873static int max3100_resume(struct spi_device *spi)
874{
875 struct max3100_port *s = dev_get_drvdata(&spi->dev);
876
877 dev_dbg(&s->spi->dev, "%s\n", __func__);
878
879 if (s->max3100_hw_suspend)
880 s->max3100_hw_suspend(0);
881 uart_resume_port(&max3100_uart_driver, &s->port);
882 s->suspending = 0;
883
884 enable_irq(s->irq);
885
886 s->conf_commit = 1;
887 if (s->workqueue)
888 max3100_dowork(s);
889
890 return 0;
891}
892
893#else
894#define max3100_suspend NULL
895#define max3100_resume NULL
896#endif
897
898static struct spi_driver max3100_driver = {
899 .driver = {
900 .name = "max3100",
901 .bus = &spi_bus_type,
902 .owner = THIS_MODULE,
903 },
904
905 .probe = max3100_probe,
906 .remove = __devexit_p(max3100_remove),
907 .suspend = max3100_suspend,
908 .resume = max3100_resume,
909};
910
911static int __init max3100_init(void)
912{
913 return spi_register_driver(&max3100_driver);
914}
915module_init(max3100_init);
916
917static void __exit max3100_exit(void)
918{
919 spi_unregister_driver(&max3100_driver);
920}
921module_exit(max3100_exit);
922
923MODULE_DESCRIPTION("MAX3100 driver");
924MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
925MODULE_LICENSE("GPL");
926MODULE_ALIAS("spi:max3100");
diff --git a/drivers/tty/serial/max3107-aava.c b/drivers/tty/serial/max3107-aava.c
new file mode 100644
index 000000000000..a1fe304f2f52
--- /dev/null
+++ b/drivers/tty/serial/max3107-aava.c
@@ -0,0 +1,344 @@
1/*
2 * max3107.c - spi uart protocol driver for Maxim 3107
3 * Based on max3100.c
4 * by Christian Pellegrin <chripell@evolware.org>
5 * and max3110.c
6 * by Feng Tang <feng.tang@intel.com>
7 *
8 * Copyright (C) Aavamobile 2009
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 */
29
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/spi/spi.h>
35#include <linux/freezer.h>
36#include <linux/platform_device.h>
37#include <linux/gpio.h>
38#include <linux/sfi.h>
39#include <asm/mrst.h>
40#include "max3107.h"
41
42/* GPIO direction to input function */
43static int max3107_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
44{
45 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
46 u16 buf[1]; /* Buffer for SPI transfer */
47
48 if (offset >= MAX3107_GPIO_COUNT) {
49 dev_err(&s->spi->dev, "Invalid GPIO\n");
50 return -EINVAL;
51 }
52
53 /* Read current GPIO configuration register */
54 buf[0] = MAX3107_GPIOCFG_REG;
55 /* Perform SPI transfer */
56 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
57 dev_err(&s->spi->dev, "SPI transfer GPIO read failed\n");
58 return -EIO;
59 }
60 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
61
62 /* Set GPIO to input */
63 buf[0] &= ~(0x0001 << offset);
64
65 /* Write new GPIO configuration register value */
66 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
67 /* Perform SPI transfer */
68 if (max3107_rw(s, (u8 *)buf, NULL, 2)) {
69 dev_err(&s->spi->dev, "SPI transfer GPIO write failed\n");
70 return -EIO;
71 }
72 return 0;
73}
74
75/* GPIO direction to output function */
76static int max3107_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
77 int value)
78{
79 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
80 u16 buf[2]; /* Buffer for SPI transfers */
81
82 if (offset >= MAX3107_GPIO_COUNT) {
83 dev_err(&s->spi->dev, "Invalid GPIO\n");
84 return -EINVAL;
85 }
86
87 /* Read current GPIO configuration and data registers */
88 buf[0] = MAX3107_GPIOCFG_REG;
89 buf[1] = MAX3107_GPIODATA_REG;
90 /* Perform SPI transfer */
91 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
92 dev_err(&s->spi->dev, "SPI transfer gpio failed\n");
93 return -EIO;
94 }
95 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
96 buf[1] &= MAX3107_SPI_RX_DATA_MASK;
97
98 /* Set GPIO to output */
99 buf[0] |= (0x0001 << offset);
100 /* Set value */
101 if (value)
102 buf[1] |= (0x0001 << offset);
103 else
104 buf[1] &= ~(0x0001 << offset);
105
106 /* Write new GPIO configuration and data register values */
107 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
108 buf[1] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
109 /* Perform SPI transfer */
110 if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
111 dev_err(&s->spi->dev,
112 "SPI transfer for GPIO conf data w failed\n");
113 return -EIO;
114 }
115 return 0;
116}
117
118/* GPIO value query function */
119static int max3107_gpio_get(struct gpio_chip *chip, unsigned offset)
120{
121 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
122 u16 buf[1]; /* Buffer for SPI transfer */
123
124 if (offset >= MAX3107_GPIO_COUNT) {
125 dev_err(&s->spi->dev, "Invalid GPIO\n");
126 return -EINVAL;
127 }
128
129 /* Read current GPIO data register */
130 buf[0] = MAX3107_GPIODATA_REG;
131 /* Perform SPI transfer */
132 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
133 dev_err(&s->spi->dev, "SPI transfer GPIO data r failed\n");
134 return -EIO;
135 }
136 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
137
138 /* Return value */
139 return buf[0] & (0x0001 << offset);
140}
141
142/* GPIO value set function */
143static void max3107_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
144{
145 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
146 u16 buf[2]; /* Buffer for SPI transfers */
147
148 if (offset >= MAX3107_GPIO_COUNT) {
149 dev_err(&s->spi->dev, "Invalid GPIO\n");
150 return;
151 }
152
153 /* Read current GPIO configuration registers*/
154 buf[0] = MAX3107_GPIODATA_REG;
155 buf[1] = MAX3107_GPIOCFG_REG;
156 /* Perform SPI transfer */
157 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
158 dev_err(&s->spi->dev,
159 "SPI transfer for GPIO data and config read failed\n");
160 return;
161 }
162 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
163 buf[1] &= MAX3107_SPI_RX_DATA_MASK;
164
165 if (!(buf[1] & (0x0001 << offset))) {
166 /* Configured as input, can't set value */
167 dev_warn(&s->spi->dev,
168 "Trying to set value for input GPIO\n");
169 return;
170 }
171
172 /* Set value */
173 if (value)
174 buf[0] |= (0x0001 << offset);
175 else
176 buf[0] &= ~(0x0001 << offset);
177
178 /* Write new GPIO data register value */
179 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
180 /* Perform SPI transfer */
181 if (max3107_rw(s, (u8 *)buf, NULL, 2))
182 dev_err(&s->spi->dev, "SPI transfer GPIO data w failed\n");
183}
184
185/* GPIO chip data */
186static struct gpio_chip max3107_gpio_chip = {
187 .owner = THIS_MODULE,
188 .direction_input = max3107_gpio_direction_in,
189 .direction_output = max3107_gpio_direction_out,
190 .get = max3107_gpio_get,
191 .set = max3107_gpio_set,
192 .can_sleep = 1,
193 .base = MAX3107_GPIO_BASE,
194 .ngpio = MAX3107_GPIO_COUNT,
195};
196
197/**
198 * max3107_aava_reset - reset on AAVA systems
199 * @spi: The SPI device we are probing
200 *
201 * Reset the device ready for probing.
202 */
203
204static int max3107_aava_reset(struct spi_device *spi)
205{
206 /* Reset the chip */
207 if (gpio_request(MAX3107_RESET_GPIO, "max3107")) {
208 pr_err("Requesting RESET GPIO failed\n");
209 return -EIO;
210 }
211 if (gpio_direction_output(MAX3107_RESET_GPIO, 0)) {
212 pr_err("Setting RESET GPIO to 0 failed\n");
213 gpio_free(MAX3107_RESET_GPIO);
214 return -EIO;
215 }
216 msleep(MAX3107_RESET_DELAY);
217 if (gpio_direction_output(MAX3107_RESET_GPIO, 1)) {
218 pr_err("Setting RESET GPIO to 1 failed\n");
219 gpio_free(MAX3107_RESET_GPIO);
220 return -EIO;
221 }
222 gpio_free(MAX3107_RESET_GPIO);
223 msleep(MAX3107_WAKEUP_DELAY);
224 return 0;
225}
226
227static int max3107_aava_configure(struct max3107_port *s)
228{
229 int retval;
230
231 /* Initialize GPIO chip data */
232 s->chip = max3107_gpio_chip;
233 s->chip.label = s->spi->modalias;
234 s->chip.dev = &s->spi->dev;
235
236 /* Add GPIO chip */
237 retval = gpiochip_add(&s->chip);
238 if (retval) {
239 dev_err(&s->spi->dev, "Adding GPIO chip failed\n");
240 return retval;
241 }
242
243 /* Temporary fix for EV2 boot problems, set modem reset to 0 */
244 max3107_gpio_direction_out(&s->chip, 3, 0);
245 return 0;
246}
247
248#if 0
249/* This will get enabled once we have the board stuff merged for this
250 specific case */
251
252static const struct baud_table brg13_ext[] = {
253 { 300, MAX3107_BRG13_B300 },
254 { 600, MAX3107_BRG13_B600 },
255 { 1200, MAX3107_BRG13_B1200 },
256 { 2400, MAX3107_BRG13_B2400 },
257 { 4800, MAX3107_BRG13_B4800 },
258 { 9600, MAX3107_BRG13_B9600 },
259 { 19200, MAX3107_BRG13_B19200 },
260 { 57600, MAX3107_BRG13_B57600 },
261 { 115200, MAX3107_BRG13_B115200 },
262 { 230400, MAX3107_BRG13_B230400 },
263 { 460800, MAX3107_BRG13_B460800 },
264 { 921600, MAX3107_BRG13_B921600 },
265 { 0, 0 }
266};
267
268static void max3107_aava_init(struct max3107_port *s)
269{
270 /*override for AAVA SC specific*/
271 if (mrst_platform_id() == MRST_PLATFORM_AAVA_SC) {
272 if (get_koski_build_id() <= KOSKI_EV2)
273 if (s->ext_clk) {
274 s->brg_cfg = MAX3107_BRG13_B9600;
275 s->baud_tbl = (struct baud_table *)brg13_ext;
276 }
277 }
278}
279#endif
280
281static int __devexit max3107_aava_remove(struct spi_device *spi)
282{
283 struct max3107_port *s = dev_get_drvdata(&spi->dev);
284
285 /* Remove GPIO chip */
286 if (gpiochip_remove(&s->chip))
287 dev_warn(&spi->dev, "Removing GPIO chip failed\n");
288
289 /* Then do the default remove */
290 return max3107_remove(spi);
291}
292
293/* Platform data */
294static struct max3107_plat aava_plat_data = {
295 .loopback = 0,
296 .ext_clk = 1,
297/* .init = max3107_aava_init, */
298 .configure = max3107_aava_configure,
299 .hw_suspend = max3107_hw_susp,
300 .polled_mode = 0,
301 .poll_time = 0,
302};
303
304
305static int __devinit max3107_probe_aava(struct spi_device *spi)
306{
307 int err = max3107_aava_reset(spi);
308 if (err < 0)
309 return err;
310 return max3107_probe(spi, &aava_plat_data);
311}
312
313/* Spi driver data */
314static struct spi_driver max3107_driver = {
315 .driver = {
316 .name = "aava-max3107",
317 .bus = &spi_bus_type,
318 .owner = THIS_MODULE,
319 },
320 .probe = max3107_probe_aava,
321 .remove = __devexit_p(max3107_aava_remove),
322 .suspend = max3107_suspend,
323 .resume = max3107_resume,
324};
325
326/* Driver init function */
327static int __init max3107_init(void)
328{
329 return spi_register_driver(&max3107_driver);
330}
331
332/* Driver exit function */
333static void __exit max3107_exit(void)
334{
335 spi_unregister_driver(&max3107_driver);
336}
337
338module_init(max3107_init);
339module_exit(max3107_exit);
340
341MODULE_DESCRIPTION("MAX3107 driver");
342MODULE_AUTHOR("Aavamobile");
343MODULE_ALIAS("aava-max3107-spi");
344MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/max3107.c b/drivers/tty/serial/max3107.c
new file mode 100644
index 000000000000..910870edf708
--- /dev/null
+++ b/drivers/tty/serial/max3107.c
@@ -0,0 +1,1213 @@
1/*
2 * max3107.c - spi uart protocol driver for Maxim 3107
3 * Based on max3100.c
4 * by Christian Pellegrin <chripell@evolware.org>
5 * and max3110.c
6 * by Feng Tang <feng.tang@intel.com>
7 *
8 * Copyright (C) Aavamobile 2009
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 */
29
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/gpio.h>
35#include <linux/spi/spi.h>
36#include <linux/freezer.h>
37#include "max3107.h"
38
39static const struct baud_table brg26_ext[] = {
40 { 300, MAX3107_BRG26_B300 },
41 { 600, MAX3107_BRG26_B600 },
42 { 1200, MAX3107_BRG26_B1200 },
43 { 2400, MAX3107_BRG26_B2400 },
44 { 4800, MAX3107_BRG26_B4800 },
45 { 9600, MAX3107_BRG26_B9600 },
46 { 19200, MAX3107_BRG26_B19200 },
47 { 57600, MAX3107_BRG26_B57600 },
48 { 115200, MAX3107_BRG26_B115200 },
49 { 230400, MAX3107_BRG26_B230400 },
50 { 460800, MAX3107_BRG26_B460800 },
51 { 921600, MAX3107_BRG26_B921600 },
52 { 0, 0 }
53};
54
55static const struct baud_table brg13_int[] = {
56 { 300, MAX3107_BRG13_IB300 },
57 { 600, MAX3107_BRG13_IB600 },
58 { 1200, MAX3107_BRG13_IB1200 },
59 { 2400, MAX3107_BRG13_IB2400 },
60 { 4800, MAX3107_BRG13_IB4800 },
61 { 9600, MAX3107_BRG13_IB9600 },
62 { 19200, MAX3107_BRG13_IB19200 },
63 { 57600, MAX3107_BRG13_IB57600 },
64 { 115200, MAX3107_BRG13_IB115200 },
65 { 230400, MAX3107_BRG13_IB230400 },
66 { 460800, MAX3107_BRG13_IB460800 },
67 { 921600, MAX3107_BRG13_IB921600 },
68 { 0, 0 }
69};
70
71static u32 get_new_brg(int baud, struct max3107_port *s)
72{
73 int i;
74 const struct baud_table *baud_tbl = s->baud_tbl;
75
76 for (i = 0; i < 13; i++) {
77 if (baud == baud_tbl[i].baud)
78 return baud_tbl[i].new_brg;
79 }
80
81 return 0;
82}
83
84/* Perform SPI transfer for write/read of device register(s) */
85int max3107_rw(struct max3107_port *s, u8 *tx, u8 *rx, int len)
86{
87 struct spi_message spi_msg;
88 struct spi_transfer spi_xfer;
89
90 /* Initialize SPI ,message */
91 spi_message_init(&spi_msg);
92
93 /* Initialize SPI transfer */
94 memset(&spi_xfer, 0, sizeof spi_xfer);
95 spi_xfer.len = len;
96 spi_xfer.tx_buf = tx;
97 spi_xfer.rx_buf = rx;
98 spi_xfer.speed_hz = MAX3107_SPI_SPEED;
99
100 /* Add SPI transfer to SPI message */
101 spi_message_add_tail(&spi_xfer, &spi_msg);
102
103#ifdef DBG_TRACE_SPI_DATA
104 {
105 int i;
106 pr_info("tx len %d:\n", spi_xfer.len);
107 for (i = 0 ; i < spi_xfer.len && i < 32 ; i++)
108 pr_info(" %x", ((u8 *)spi_xfer.tx_buf)[i]);
109 pr_info("\n");
110 }
111#endif
112
113 /* Perform synchronous SPI transfer */
114 if (spi_sync(s->spi, &spi_msg)) {
115 dev_err(&s->spi->dev, "spi_sync failure\n");
116 return -EIO;
117 }
118
119#ifdef DBG_TRACE_SPI_DATA
120 if (spi_xfer.rx_buf) {
121 int i;
122 pr_info("rx len %d:\n", spi_xfer.len);
123 for (i = 0 ; i < spi_xfer.len && i < 32 ; i++)
124 pr_info(" %x", ((u8 *)spi_xfer.rx_buf)[i]);
125 pr_info("\n");
126 }
127#endif
128 return 0;
129}
130EXPORT_SYMBOL_GPL(max3107_rw);
131
132/* Puts received data to circular buffer */
133static void put_data_to_circ_buf(struct max3107_port *s, unsigned char *data,
134 int len)
135{
136 struct uart_port *port = &s->port;
137 struct tty_struct *tty;
138
139 if (!port->state)
140 return;
141
142 tty = port->state->port.tty;
143 if (!tty)
144 return;
145
146 /* Insert received data */
147 tty_insert_flip_string(tty, data, len);
148 /* Update RX counter */
149 port->icount.rx += len;
150}
151
152/* Handle data receiving */
153static void max3107_handlerx(struct max3107_port *s, u16 rxlvl)
154{
155 int i;
156 int j;
157 int len; /* SPI transfer buffer length */
158 u16 *buf;
159 u8 *valid_str;
160
161 if (!s->rx_enabled)
162 /* RX is disabled */
163 return;
164
165 if (rxlvl == 0) {
166 /* RX fifo is empty */
167 return;
168 } else if (rxlvl >= MAX3107_RX_FIFO_SIZE) {
169 dev_warn(&s->spi->dev, "Possible RX FIFO overrun %d\n", rxlvl);
170 /* Ensure sanity of RX level */
171 rxlvl = MAX3107_RX_FIFO_SIZE;
172 }
173 if ((s->rxbuf == 0) || (s->rxstr == 0)) {
174 dev_warn(&s->spi->dev, "Rx buffer/str isn't ready\n");
175 return;
176 }
177 buf = s->rxbuf;
178 valid_str = s->rxstr;
179 while (rxlvl) {
180 pr_debug("rxlvl %d\n", rxlvl);
181 /* Clear buffer */
182 memset(buf, 0, sizeof(u16) * (MAX3107_RX_FIFO_SIZE + 2));
183 len = 0;
184 if (s->irqen_reg & MAX3107_IRQ_RXFIFO_BIT) {
185 /* First disable RX FIFO interrupt */
186 pr_debug("Disabling RX INT\n");
187 buf[0] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
188 s->irqen_reg &= ~MAX3107_IRQ_RXFIFO_BIT;
189 buf[0] |= s->irqen_reg;
190 len++;
191 }
192 /* Just increase the length by amount of words in FIFO since
193 * buffer was zeroed and SPI transfer of 0x0000 means reading
194 * from RX FIFO
195 */
196 len += rxlvl;
197 /* Append RX level query */
198 buf[len] = MAX3107_RXFIFOLVL_REG;
199 len++;
200
201 /* Perform the SPI transfer */
202 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, len * 2)) {
203 dev_err(&s->spi->dev, "SPI transfer for RX h failed\n");
204 return;
205 }
206
207 /* Skip RX FIFO interrupt disabling word if it was added */
208 j = ((len - 1) - rxlvl);
209 /* Read received words */
210 for (i = 0; i < rxlvl; i++, j++)
211 valid_str[i] = (u8)buf[j];
212 put_data_to_circ_buf(s, valid_str, rxlvl);
213 /* Get new RX level */
214 rxlvl = (buf[len - 1] & MAX3107_SPI_RX_DATA_MASK);
215 }
216
217 if (s->rx_enabled) {
218 /* RX still enabled, re-enable RX FIFO interrupt */
219 pr_debug("Enabling RX INT\n");
220 buf[0] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
221 s->irqen_reg |= MAX3107_IRQ_RXFIFO_BIT;
222 buf[0] |= s->irqen_reg;
223 if (max3107_rw(s, (u8 *)buf, NULL, 2))
224 dev_err(&s->spi->dev, "RX FIFO INT enabling failed\n");
225 }
226
227 /* Push the received data to receivers */
228 if (s->port.state->port.tty)
229 tty_flip_buffer_push(s->port.state->port.tty);
230}
231
232
233/* Handle data sending */
234static void max3107_handletx(struct max3107_port *s)
235{
236 struct circ_buf *xmit = &s->port.state->xmit;
237 int i;
238 unsigned long flags;
239 int len; /* SPI transfer buffer length */
240 u16 *buf;
241
242 if (!s->tx_fifo_empty)
243 /* Don't send more data before previous data is sent */
244 return;
245
246 if (uart_circ_empty(xmit) || uart_tx_stopped(&s->port))
247 /* No data to send or TX is stopped */
248 return;
249
250 if (!s->txbuf) {
251 dev_warn(&s->spi->dev, "Txbuf isn't ready\n");
252 return;
253 }
254 buf = s->txbuf;
255 /* Get length of data pending in circular buffer */
256 len = uart_circ_chars_pending(xmit);
257 if (len) {
258 /* Limit to size of TX FIFO */
259 if (len > MAX3107_TX_FIFO_SIZE)
260 len = MAX3107_TX_FIFO_SIZE;
261
262 pr_debug("txlen %d\n", len);
263
264 /* Update TX counter */
265 s->port.icount.tx += len;
266
267 /* TX FIFO will no longer be empty */
268 s->tx_fifo_empty = 0;
269
270 i = 0;
271 if (s->irqen_reg & MAX3107_IRQ_TXEMPTY_BIT) {
272 /* First disable TX empty interrupt */
273 pr_debug("Disabling TE INT\n");
274 buf[i] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
275 s->irqen_reg &= ~MAX3107_IRQ_TXEMPTY_BIT;
276 buf[i] |= s->irqen_reg;
277 i++;
278 len++;
279 }
280 /* Add data to send */
281 spin_lock_irqsave(&s->port.lock, flags);
282 for ( ; i < len ; i++) {
283 buf[i] = (MAX3107_WRITE_BIT | MAX3107_THR_REG);
284 buf[i] |= ((u16)xmit->buf[xmit->tail] &
285 MAX3107_SPI_TX_DATA_MASK);
286 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
287 }
288 spin_unlock_irqrestore(&s->port.lock, flags);
289 if (!(s->irqen_reg & MAX3107_IRQ_TXEMPTY_BIT)) {
290 /* Enable TX empty interrupt */
291 pr_debug("Enabling TE INT\n");
292 buf[i] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
293 s->irqen_reg |= MAX3107_IRQ_TXEMPTY_BIT;
294 buf[i] |= s->irqen_reg;
295 i++;
296 len++;
297 }
298 if (!s->tx_enabled) {
299 /* Enable TX */
300 pr_debug("Enable TX\n");
301 buf[i] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
302 spin_lock_irqsave(&s->data_lock, flags);
303 s->mode1_reg &= ~MAX3107_MODE1_TXDIS_BIT;
304 buf[i] |= s->mode1_reg;
305 spin_unlock_irqrestore(&s->data_lock, flags);
306 s->tx_enabled = 1;
307 i++;
308 len++;
309 }
310
311 /* Perform the SPI transfer */
312 if (max3107_rw(s, (u8 *)buf, NULL, len*2)) {
313 dev_err(&s->spi->dev,
314 "SPI transfer TX handling failed\n");
315 return;
316 }
317 }
318
319 /* Indicate wake up if circular buffer is getting low on data */
320 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
321 uart_write_wakeup(&s->port);
322
323}
324
325/* Handle interrupts
326 * Also reads and returns current RX FIFO level
327 */
328static u16 handle_interrupt(struct max3107_port *s)
329{
330 u16 buf[4]; /* Buffer for SPI transfers */
331 u8 irq_status;
332 u16 rx_level;
333 unsigned long flags;
334
335 /* Read IRQ status register */
336 buf[0] = MAX3107_IRQSTS_REG;
337 /* Read status IRQ status register */
338 buf[1] = MAX3107_STS_IRQSTS_REG;
339 /* Read LSR IRQ status register */
340 buf[2] = MAX3107_LSR_IRQSTS_REG;
341 /* Query RX level */
342 buf[3] = MAX3107_RXFIFOLVL_REG;
343
344 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 8)) {
345 dev_err(&s->spi->dev,
346 "SPI transfer for INTR handling failed\n");
347 return 0;
348 }
349
350 irq_status = (u8)buf[0];
351 pr_debug("IRQSTS %x\n", irq_status);
352 rx_level = (buf[3] & MAX3107_SPI_RX_DATA_MASK);
353
354 if (irq_status & MAX3107_IRQ_LSR_BIT) {
355 /* LSR interrupt */
356 if (buf[2] & MAX3107_LSR_RXTO_BIT)
357 /* RX timeout interrupt,
358 * handled by normal RX handling
359 */
360 pr_debug("RX TO INT\n");
361 }
362
363 if (irq_status & MAX3107_IRQ_TXEMPTY_BIT) {
364 /* Tx empty interrupt,
365 * disable TX and set tx_fifo_empty flag
366 */
367 pr_debug("TE INT, disabling TX\n");
368 buf[0] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
369 spin_lock_irqsave(&s->data_lock, flags);
370 s->mode1_reg |= MAX3107_MODE1_TXDIS_BIT;
371 buf[0] |= s->mode1_reg;
372 spin_unlock_irqrestore(&s->data_lock, flags);
373 if (max3107_rw(s, (u8 *)buf, NULL, 2))
374 dev_err(&s->spi->dev, "SPI transfer TX dis failed\n");
375 s->tx_enabled = 0;
376 s->tx_fifo_empty = 1;
377 }
378
379 if (irq_status & MAX3107_IRQ_RXFIFO_BIT)
380 /* RX FIFO interrupt,
381 * handled by normal RX handling
382 */
383 pr_debug("RFIFO INT\n");
384
385 /* Return RX level */
386 return rx_level;
387}
388
389/* Trigger work thread*/
390static void max3107_dowork(struct max3107_port *s)
391{
392 if (!work_pending(&s->work) && !freezing(current) && !s->suspended)
393 queue_work(s->workqueue, &s->work);
394 else
395 dev_warn(&s->spi->dev, "interrup isn't serviced normally!\n");
396}
397
398/* Work thread */
399static void max3107_work(struct work_struct *w)
400{
401 struct max3107_port *s = container_of(w, struct max3107_port, work);
402 u16 rxlvl = 0;
403 int len; /* SPI transfer buffer length */
404 u16 buf[5]; /* Buffer for SPI transfers */
405 unsigned long flags;
406
407 /* Start by reading current RX FIFO level */
408 buf[0] = MAX3107_RXFIFOLVL_REG;
409 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
410 dev_err(&s->spi->dev, "SPI transfer RX lev failed\n");
411 rxlvl = 0;
412 } else {
413 rxlvl = (buf[0] & MAX3107_SPI_RX_DATA_MASK);
414 }
415
416 do {
417 pr_debug("rxlvl %d\n", rxlvl);
418
419 /* Handle RX */
420 max3107_handlerx(s, rxlvl);
421 rxlvl = 0;
422
423 if (s->handle_irq) {
424 /* Handle pending interrupts
425 * We also get new RX FIFO level since new data may
426 * have been received while pushing received data to
427 * receivers
428 */
429 s->handle_irq = 0;
430 rxlvl = handle_interrupt(s);
431 }
432
433 /* Handle TX */
434 max3107_handletx(s);
435
436 /* Handle configuration changes */
437 len = 0;
438 spin_lock_irqsave(&s->data_lock, flags);
439 if (s->mode1_commit) {
440 pr_debug("mode1_commit\n");
441 buf[len] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
442 buf[len++] |= s->mode1_reg;
443 s->mode1_commit = 0;
444 }
445 if (s->lcr_commit) {
446 pr_debug("lcr_commit\n");
447 buf[len] = (MAX3107_WRITE_BIT | MAX3107_LCR_REG);
448 buf[len++] |= s->lcr_reg;
449 s->lcr_commit = 0;
450 }
451 if (s->brg_commit) {
452 pr_debug("brg_commit\n");
453 buf[len] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVMSB_REG);
454 buf[len++] |= ((s->brg_cfg >> 16) &
455 MAX3107_SPI_TX_DATA_MASK);
456 buf[len] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVLSB_REG);
457 buf[len++] |= ((s->brg_cfg >> 8) &
458 MAX3107_SPI_TX_DATA_MASK);
459 buf[len] = (MAX3107_WRITE_BIT | MAX3107_BRGCFG_REG);
460 buf[len++] |= ((s->brg_cfg) & 0xff);
461 s->brg_commit = 0;
462 }
463 spin_unlock_irqrestore(&s->data_lock, flags);
464
465 if (len > 0) {
466 if (max3107_rw(s, (u8 *)buf, NULL, len * 2))
467 dev_err(&s->spi->dev,
468 "SPI transfer config failed\n");
469 }
470
471 /* Reloop if interrupt handling indicated data in RX FIFO */
472 } while (rxlvl);
473
474}
475
476/* Set sleep mode */
477static void max3107_set_sleep(struct max3107_port *s, int mode)
478{
479 u16 buf[1]; /* Buffer for SPI transfer */
480 unsigned long flags;
481 pr_debug("enter, mode %d\n", mode);
482
483 buf[0] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
484 spin_lock_irqsave(&s->data_lock, flags);
485 switch (mode) {
486 case MAX3107_DISABLE_FORCED_SLEEP:
487 s->mode1_reg &= ~MAX3107_MODE1_FORCESLEEP_BIT;
488 break;
489 case MAX3107_ENABLE_FORCED_SLEEP:
490 s->mode1_reg |= MAX3107_MODE1_FORCESLEEP_BIT;
491 break;
492 case MAX3107_DISABLE_AUTOSLEEP:
493 s->mode1_reg &= ~MAX3107_MODE1_AUTOSLEEP_BIT;
494 break;
495 case MAX3107_ENABLE_AUTOSLEEP:
496 s->mode1_reg |= MAX3107_MODE1_AUTOSLEEP_BIT;
497 break;
498 default:
499 spin_unlock_irqrestore(&s->data_lock, flags);
500 dev_warn(&s->spi->dev, "invalid sleep mode\n");
501 return;
502 }
503 buf[0] |= s->mode1_reg;
504 spin_unlock_irqrestore(&s->data_lock, flags);
505
506 if (max3107_rw(s, (u8 *)buf, NULL, 2))
507 dev_err(&s->spi->dev, "SPI transfer sleep mode failed\n");
508
509 if (mode == MAX3107_DISABLE_AUTOSLEEP ||
510 mode == MAX3107_DISABLE_FORCED_SLEEP)
511 msleep(MAX3107_WAKEUP_DELAY);
512}
513
514/* Perform full register initialization */
515static void max3107_register_init(struct max3107_port *s)
516{
517 u16 buf[11]; /* Buffer for SPI transfers */
518
519 /* 1. Configure baud rate, 9600 as default */
520 s->baud = 9600;
521 /* the below is default*/
522 if (s->ext_clk) {
523 s->brg_cfg = MAX3107_BRG26_B9600;
524 s->baud_tbl = (struct baud_table *)brg26_ext;
525 } else {
526 s->brg_cfg = MAX3107_BRG13_IB9600;
527 s->baud_tbl = (struct baud_table *)brg13_int;
528 }
529
530 if (s->pdata->init)
531 s->pdata->init(s);
532
533 buf[0] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVMSB_REG)
534 | ((s->brg_cfg >> 16) & MAX3107_SPI_TX_DATA_MASK);
535 buf[1] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVLSB_REG)
536 | ((s->brg_cfg >> 8) & MAX3107_SPI_TX_DATA_MASK);
537 buf[2] = (MAX3107_WRITE_BIT | MAX3107_BRGCFG_REG)
538 | ((s->brg_cfg) & 0xff);
539
540 /* 2. Configure LCR register, 8N1 mode by default */
541 s->lcr_reg = MAX3107_LCR_WORD_LEN_8;
542 buf[3] = (MAX3107_WRITE_BIT | MAX3107_LCR_REG)
543 | s->lcr_reg;
544
545 /* 3. Configure MODE 1 register */
546 s->mode1_reg = 0;
547 /* Enable IRQ pin */
548 s->mode1_reg |= MAX3107_MODE1_IRQSEL_BIT;
549 /* Disable TX */
550 s->mode1_reg |= MAX3107_MODE1_TXDIS_BIT;
551 s->tx_enabled = 0;
552 /* RX is enabled */
553 s->rx_enabled = 1;
554 buf[4] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG)
555 | s->mode1_reg;
556
557 /* 4. Configure MODE 2 register */
558 buf[5] = (MAX3107_WRITE_BIT | MAX3107_MODE2_REG);
559 if (s->loopback) {
560 /* Enable loopback */
561 buf[5] |= MAX3107_MODE2_LOOPBACK_BIT;
562 }
563 /* Reset FIFOs */
564 buf[5] |= MAX3107_MODE2_FIFORST_BIT;
565 s->tx_fifo_empty = 1;
566
567 /* 5. Configure FIFO trigger level register */
568 buf[6] = (MAX3107_WRITE_BIT | MAX3107_FIFOTRIGLVL_REG);
569 /* RX FIFO trigger for 16 words, TX FIFO trigger not used */
570 buf[6] |= (MAX3107_FIFOTRIGLVL_RX(16) | MAX3107_FIFOTRIGLVL_TX(0));
571
572 /* 6. Configure flow control levels */
573 buf[7] = (MAX3107_WRITE_BIT | MAX3107_FLOWLVL_REG);
574 /* Flow control halt level 96, resume level 48 */
575 buf[7] |= (MAX3107_FLOWLVL_RES(48) | MAX3107_FLOWLVL_HALT(96));
576
577 /* 7. Configure flow control */
578 buf[8] = (MAX3107_WRITE_BIT | MAX3107_FLOWCTRL_REG);
579 /* Enable auto CTS and auto RTS flow control */
580 buf[8] |= (MAX3107_FLOWCTRL_AUTOCTS_BIT | MAX3107_FLOWCTRL_AUTORTS_BIT);
581
582 /* 8. Configure RX timeout register */
583 buf[9] = (MAX3107_WRITE_BIT | MAX3107_RXTO_REG);
584 /* Timeout after 48 character intervals */
585 buf[9] |= 0x0030;
586
587 /* 9. Configure LSR interrupt enable register */
588 buf[10] = (MAX3107_WRITE_BIT | MAX3107_LSR_IRQEN_REG);
589 /* Enable RX timeout interrupt */
590 buf[10] |= MAX3107_LSR_RXTO_BIT;
591
592 /* Perform SPI transfer */
593 if (max3107_rw(s, (u8 *)buf, NULL, 22))
594 dev_err(&s->spi->dev, "SPI transfer for init failed\n");
595
596 /* 10. Clear IRQ status register by reading it */
597 buf[0] = MAX3107_IRQSTS_REG;
598
599 /* 11. Configure interrupt enable register */
600 /* Enable LSR interrupt */
601 s->irqen_reg = MAX3107_IRQ_LSR_BIT;
602 /* Enable RX FIFO interrupt */
603 s->irqen_reg |= MAX3107_IRQ_RXFIFO_BIT;
604 buf[1] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG)
605 | s->irqen_reg;
606
607 /* 12. Clear FIFO reset that was set in step 6 */
608 buf[2] = (MAX3107_WRITE_BIT | MAX3107_MODE2_REG);
609 if (s->loopback) {
610 /* Keep loopback enabled */
611 buf[2] |= MAX3107_MODE2_LOOPBACK_BIT;
612 }
613
614 /* Perform SPI transfer */
615 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 6))
616 dev_err(&s->spi->dev, "SPI transfer for init failed\n");
617
618}
619
620/* IRQ handler */
621static irqreturn_t max3107_irq(int irqno, void *dev_id)
622{
623 struct max3107_port *s = dev_id;
624
625 if (irqno != s->spi->irq) {
626 /* Unexpected IRQ */
627 return IRQ_NONE;
628 }
629
630 /* Indicate irq */
631 s->handle_irq = 1;
632
633 /* Trigger work thread */
634 max3107_dowork(s);
635
636 return IRQ_HANDLED;
637}
638
639/* HW suspension function
640 *
641 * Currently autosleep is used to decrease current consumption, alternative
642 * approach would be to set the chip to reset mode if UART is not being
643 * used but that would mess the GPIOs
644 *
645 */
646void max3107_hw_susp(struct max3107_port *s, int suspend)
647{
648 pr_debug("enter, suspend %d\n", suspend);
649
650 if (suspend) {
651 /* Suspend requested,
652 * enable autosleep to decrease current consumption
653 */
654 s->suspended = 1;
655 max3107_set_sleep(s, MAX3107_ENABLE_AUTOSLEEP);
656 } else {
657 /* Resume requested,
658 * disable autosleep
659 */
660 s->suspended = 0;
661 max3107_set_sleep(s, MAX3107_DISABLE_AUTOSLEEP);
662 }
663}
664EXPORT_SYMBOL_GPL(max3107_hw_susp);
665
666/* Modem status IRQ enabling */
667static void max3107_enable_ms(struct uart_port *port)
668{
669 /* Modem status not supported */
670}
671
672/* Data send function */
673static void max3107_start_tx(struct uart_port *port)
674{
675 struct max3107_port *s = container_of(port, struct max3107_port, port);
676
677 /* Trigger work thread for sending data */
678 max3107_dowork(s);
679}
680
681/* Function for checking that there is no pending transfers */
682static unsigned int max3107_tx_empty(struct uart_port *port)
683{
684 struct max3107_port *s = container_of(port, struct max3107_port, port);
685
686 pr_debug("returning %d\n",
687 (s->tx_fifo_empty && uart_circ_empty(&s->port.state->xmit)));
688 return s->tx_fifo_empty && uart_circ_empty(&s->port.state->xmit);
689}
690
691/* Function for stopping RX */
692static void max3107_stop_rx(struct uart_port *port)
693{
694 struct max3107_port *s = container_of(port, struct max3107_port, port);
695 unsigned long flags;
696
697 /* Set RX disabled in MODE 1 register */
698 spin_lock_irqsave(&s->data_lock, flags);
699 s->mode1_reg |= MAX3107_MODE1_RXDIS_BIT;
700 s->mode1_commit = 1;
701 spin_unlock_irqrestore(&s->data_lock, flags);
702 /* Set RX disabled */
703 s->rx_enabled = 0;
704 /* Trigger work thread for doing the actual configuration change */
705 max3107_dowork(s);
706}
707
708/* Function for returning control pin states */
709static unsigned int max3107_get_mctrl(struct uart_port *port)
710{
711 /* DCD and DSR are not wired and CTS/RTS is handled automatically
712 * so just indicate DSR and CAR asserted
713 */
714 return TIOCM_DSR | TIOCM_CAR;
715}
716
717/* Function for setting control pin states */
718static void max3107_set_mctrl(struct uart_port *port, unsigned int mctrl)
719{
720 /* DCD and DSR are not wired and CTS/RTS is hadnled automatically
721 * so do nothing
722 */
723}
724
725/* Function for configuring UART parameters */
726static void max3107_set_termios(struct uart_port *port,
727 struct ktermios *termios,
728 struct ktermios *old)
729{
730 struct max3107_port *s = container_of(port, struct max3107_port, port);
731 struct tty_struct *tty;
732 int baud;
733 u16 new_lcr = 0;
734 u32 new_brg = 0;
735 unsigned long flags;
736
737 if (!port->state)
738 return;
739
740 tty = port->state->port.tty;
741 if (!tty)
742 return;
743
744 /* Get new LCR register values */
745 /* Word size */
746 if ((termios->c_cflag & CSIZE) == CS7)
747 new_lcr |= MAX3107_LCR_WORD_LEN_7;
748 else
749 new_lcr |= MAX3107_LCR_WORD_LEN_8;
750
751 /* Parity */
752 if (termios->c_cflag & PARENB) {
753 new_lcr |= MAX3107_LCR_PARITY_BIT;
754 if (!(termios->c_cflag & PARODD))
755 new_lcr |= MAX3107_LCR_EVENPARITY_BIT;
756 }
757
758 /* Stop bits */
759 if (termios->c_cflag & CSTOPB) {
760 /* 2 stop bits */
761 new_lcr |= MAX3107_LCR_STOPLEN_BIT;
762 }
763
764 /* Mask termios capabilities we don't support */
765 termios->c_cflag &= ~CMSPAR;
766
767 /* Set status ignore mask */
768 s->port.ignore_status_mask = 0;
769 if (termios->c_iflag & IGNPAR)
770 s->port.ignore_status_mask |= MAX3107_ALL_ERRORS;
771
772 /* Set low latency to immediately handle pushed data */
773 s->port.state->port.tty->low_latency = 1;
774
775 /* Get new baud rate generator configuration */
776 baud = tty_get_baud_rate(tty);
777
778 spin_lock_irqsave(&s->data_lock, flags);
779 new_brg = get_new_brg(baud, s);
780 /* if can't find the corrent config, use previous */
781 if (!new_brg) {
782 baud = s->baud;
783 new_brg = s->brg_cfg;
784 }
785 spin_unlock_irqrestore(&s->data_lock, flags);
786 tty_termios_encode_baud_rate(termios, baud, baud);
787 s->baud = baud;
788
789 /* Update timeout according to new baud rate */
790 uart_update_timeout(port, termios->c_cflag, baud);
791
792 spin_lock_irqsave(&s->data_lock, flags);
793 if (s->lcr_reg != new_lcr) {
794 s->lcr_reg = new_lcr;
795 s->lcr_commit = 1;
796 }
797 if (s->brg_cfg != new_brg) {
798 s->brg_cfg = new_brg;
799 s->brg_commit = 1;
800 }
801 spin_unlock_irqrestore(&s->data_lock, flags);
802
803 /* Trigger work thread for doing the actual configuration change */
804 max3107_dowork(s);
805}
806
807/* Port shutdown function */
808static void max3107_shutdown(struct uart_port *port)
809{
810 struct max3107_port *s = container_of(port, struct max3107_port, port);
811
812 if (s->suspended && s->pdata->hw_suspend)
813 s->pdata->hw_suspend(s, 0);
814
815 /* Free the interrupt */
816 free_irq(s->spi->irq, s);
817
818 if (s->workqueue) {
819 /* Flush and destroy work queue */
820 flush_workqueue(s->workqueue);
821 destroy_workqueue(s->workqueue);
822 s->workqueue = NULL;
823 }
824
825 /* Suspend HW */
826 if (s->pdata->hw_suspend)
827 s->pdata->hw_suspend(s, 1);
828}
829
830/* Port startup function */
831static int max3107_startup(struct uart_port *port)
832{
833 struct max3107_port *s = container_of(port, struct max3107_port, port);
834
835 /* Initialize work queue */
836 s->workqueue = create_freezeable_workqueue("max3107");
837 if (!s->workqueue) {
838 dev_err(&s->spi->dev, "Workqueue creation failed\n");
839 return -EBUSY;
840 }
841 INIT_WORK(&s->work, max3107_work);
842
843 /* Setup IRQ */
844 if (request_irq(s->spi->irq, max3107_irq, IRQF_TRIGGER_FALLING,
845 "max3107", s)) {
846 dev_err(&s->spi->dev, "IRQ reguest failed\n");
847 destroy_workqueue(s->workqueue);
848 s->workqueue = NULL;
849 return -EBUSY;
850 }
851
852 /* Resume HW */
853 if (s->pdata->hw_suspend)
854 s->pdata->hw_suspend(s, 0);
855
856 /* Init registers */
857 max3107_register_init(s);
858
859 return 0;
860}
861
862/* Port type function */
863static const char *max3107_type(struct uart_port *port)
864{
865 struct max3107_port *s = container_of(port, struct max3107_port, port);
866 return s->spi->modalias;
867}
868
869/* Port release function */
870static void max3107_release_port(struct uart_port *port)
871{
872 /* Do nothing */
873}
874
875/* Port request function */
876static int max3107_request_port(struct uart_port *port)
877{
878 /* Do nothing */
879 return 0;
880}
881
882/* Port config function */
883static void max3107_config_port(struct uart_port *port, int flags)
884{
885 struct max3107_port *s = container_of(port, struct max3107_port, port);
886 s->port.type = PORT_MAX3107;
887}
888
889/* Port verify function */
890static int max3107_verify_port(struct uart_port *port,
891 struct serial_struct *ser)
892{
893 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3107)
894 return 0;
895
896 return -EINVAL;
897}
898
899/* Port stop TX function */
900static void max3107_stop_tx(struct uart_port *port)
901{
902 /* Do nothing */
903}
904
905/* Port break control function */
906static void max3107_break_ctl(struct uart_port *port, int break_state)
907{
908 /* We don't support break control, do nothing */
909}
910
911
912/* Port functions */
913static struct uart_ops max3107_ops = {
914 .tx_empty = max3107_tx_empty,
915 .set_mctrl = max3107_set_mctrl,
916 .get_mctrl = max3107_get_mctrl,
917 .stop_tx = max3107_stop_tx,
918 .start_tx = max3107_start_tx,
919 .stop_rx = max3107_stop_rx,
920 .enable_ms = max3107_enable_ms,
921 .break_ctl = max3107_break_ctl,
922 .startup = max3107_startup,
923 .shutdown = max3107_shutdown,
924 .set_termios = max3107_set_termios,
925 .type = max3107_type,
926 .release_port = max3107_release_port,
927 .request_port = max3107_request_port,
928 .config_port = max3107_config_port,
929 .verify_port = max3107_verify_port,
930};
931
932/* UART driver data */
933static struct uart_driver max3107_uart_driver = {
934 .owner = THIS_MODULE,
935 .driver_name = "ttyMAX",
936 .dev_name = "ttyMAX",
937 .nr = 1,
938};
939
940static int driver_registered = 0;
941
942
943
944/* 'Generic' platform data */
945static struct max3107_plat generic_plat_data = {
946 .loopback = 0,
947 .ext_clk = 1,
948 .hw_suspend = max3107_hw_susp,
949 .polled_mode = 0,
950 .poll_time = 0,
951};
952
953
954/*******************************************************************/
955
956/**
957 * max3107_probe - SPI bus probe entry point
958 * @spi: the spi device
959 *
960 * SPI wants us to probe this device and if appropriate claim it.
961 * Perform any platform specific requirements and then initialise
962 * the device.
963 */
964
965int max3107_probe(struct spi_device *spi, struct max3107_plat *pdata)
966{
967 struct max3107_port *s;
968 u16 buf[2]; /* Buffer for SPI transfers */
969 int retval;
970
971 pr_info("enter max3107 probe\n");
972
973 /* Allocate port structure */
974 s = kzalloc(sizeof(*s), GFP_KERNEL);
975 if (!s) {
976 pr_err("Allocating port structure failed\n");
977 return -ENOMEM;
978 }
979
980 s->pdata = pdata;
981
982 /* SPI Rx buffer
983 * +2 for RX FIFO interrupt
984 * disabling and RX level query
985 */
986 s->rxbuf = kzalloc(sizeof(u16) * (MAX3107_RX_FIFO_SIZE+2), GFP_KERNEL);
987 if (!s->rxbuf) {
988 pr_err("Allocating RX buffer failed\n");
989 retval = -ENOMEM;
990 goto err_free4;
991 }
992 s->rxstr = kzalloc(sizeof(u8) * MAX3107_RX_FIFO_SIZE, GFP_KERNEL);
993 if (!s->rxstr) {
994 pr_err("Allocating RX buffer failed\n");
995 retval = -ENOMEM;
996 goto err_free3;
997 }
998 /* SPI Tx buffer
999 * SPI transfer buffer
1000 * +3 for TX FIFO empty
1001 * interrupt disabling and
1002 * enabling and TX enabling
1003 */
1004 s->txbuf = kzalloc(sizeof(u16) * MAX3107_TX_FIFO_SIZE + 3, GFP_KERNEL);
1005 if (!s->txbuf) {
1006 pr_err("Allocating TX buffer failed\n");
1007 retval = -ENOMEM;
1008 goto err_free2;
1009 }
1010 /* Initialize shared data lock */
1011 spin_lock_init(&s->data_lock);
1012
1013 /* SPI intializations */
1014 dev_set_drvdata(&spi->dev, s);
1015 spi->mode = SPI_MODE_0;
1016 spi->dev.platform_data = pdata;
1017 spi->bits_per_word = 16;
1018 s->ext_clk = pdata->ext_clk;
1019 s->loopback = pdata->loopback;
1020 spi_setup(spi);
1021 s->spi = spi;
1022
1023 /* Check REV ID to ensure we are talking to what we expect */
1024 buf[0] = MAX3107_REVID_REG;
1025 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
1026 dev_err(&s->spi->dev, "SPI transfer for REVID read failed\n");
1027 retval = -EIO;
1028 goto err_free1;
1029 }
1030 if ((buf[0] & MAX3107_SPI_RX_DATA_MASK) != MAX3107_REVID1 &&
1031 (buf[0] & MAX3107_SPI_RX_DATA_MASK) != MAX3107_REVID2) {
1032 dev_err(&s->spi->dev, "REVID %x does not match\n",
1033 (buf[0] & MAX3107_SPI_RX_DATA_MASK));
1034 retval = -ENODEV;
1035 goto err_free1;
1036 }
1037
1038 /* Disable all interrupts */
1039 buf[0] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG | 0x0000);
1040 buf[0] |= 0x0000;
1041
1042 /* Configure clock source */
1043 buf[1] = (MAX3107_WRITE_BIT | MAX3107_CLKSRC_REG);
1044 if (s->ext_clk) {
1045 /* External clock */
1046 buf[1] |= MAX3107_CLKSRC_EXTCLK_BIT;
1047 }
1048
1049 /* PLL bypass ON */
1050 buf[1] |= MAX3107_CLKSRC_PLLBYP_BIT;
1051
1052 /* Perform SPI transfer */
1053 if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
1054 dev_err(&s->spi->dev, "SPI transfer for init failed\n");
1055 retval = -EIO;
1056 goto err_free1;
1057 }
1058
1059 /* Register UART driver */
1060 if (!driver_registered) {
1061 retval = uart_register_driver(&max3107_uart_driver);
1062 if (retval) {
1063 dev_err(&s->spi->dev, "Registering UART driver failed\n");
1064 goto err_free1;
1065 }
1066 driver_registered = 1;
1067 }
1068
1069 /* Initialize UART port data */
1070 s->port.fifosize = 128;
1071 s->port.ops = &max3107_ops;
1072 s->port.line = 0;
1073 s->port.dev = &spi->dev;
1074 s->port.uartclk = 9600;
1075 s->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
1076 s->port.irq = s->spi->irq;
1077 s->port.type = PORT_MAX3107;
1078
1079 /* Add UART port */
1080 retval = uart_add_one_port(&max3107_uart_driver, &s->port);
1081 if (retval < 0) {
1082 dev_err(&s->spi->dev, "Adding UART port failed\n");
1083 goto err_free1;
1084 }
1085
1086 if (pdata->configure) {
1087 retval = pdata->configure(s);
1088 if (retval < 0)
1089 goto err_free1;
1090 }
1091
1092 /* Go to suspend mode */
1093 if (pdata->hw_suspend)
1094 pdata->hw_suspend(s, 1);
1095
1096 return 0;
1097
1098err_free1:
1099 kfree(s->txbuf);
1100err_free2:
1101 kfree(s->rxstr);
1102err_free3:
1103 kfree(s->rxbuf);
1104err_free4:
1105 kfree(s);
1106 return retval;
1107}
1108EXPORT_SYMBOL_GPL(max3107_probe);
1109
1110/* Driver remove function */
1111int max3107_remove(struct spi_device *spi)
1112{
1113 struct max3107_port *s = dev_get_drvdata(&spi->dev);
1114
1115 pr_info("enter max3107 remove\n");
1116
1117 /* Remove port */
1118 if (uart_remove_one_port(&max3107_uart_driver, &s->port))
1119 dev_warn(&s->spi->dev, "Removing UART port failed\n");
1120
1121
1122 /* Free TxRx buffer */
1123 kfree(s->rxbuf);
1124 kfree(s->rxstr);
1125 kfree(s->txbuf);
1126
1127 /* Free port structure */
1128 kfree(s);
1129
1130 return 0;
1131}
1132EXPORT_SYMBOL_GPL(max3107_remove);
1133
1134/* Driver suspend function */
1135int max3107_suspend(struct spi_device *spi, pm_message_t state)
1136{
1137#ifdef CONFIG_PM
1138 struct max3107_port *s = dev_get_drvdata(&spi->dev);
1139
1140 pr_debug("enter suspend\n");
1141
1142 /* Suspend UART port */
1143 uart_suspend_port(&max3107_uart_driver, &s->port);
1144
1145 /* Go to suspend mode */
1146 if (s->pdata->hw_suspend)
1147 s->pdata->hw_suspend(s, 1);
1148#endif /* CONFIG_PM */
1149 return 0;
1150}
1151EXPORT_SYMBOL_GPL(max3107_suspend);
1152
1153/* Driver resume function */
1154int max3107_resume(struct spi_device *spi)
1155{
1156#ifdef CONFIG_PM
1157 struct max3107_port *s = dev_get_drvdata(&spi->dev);
1158
1159 pr_debug("enter resume\n");
1160
1161 /* Resume from suspend */
1162 if (s->pdata->hw_suspend)
1163 s->pdata->hw_suspend(s, 0);
1164
1165 /* Resume UART port */
1166 uart_resume_port(&max3107_uart_driver, &s->port);
1167#endif /* CONFIG_PM */
1168 return 0;
1169}
1170EXPORT_SYMBOL_GPL(max3107_resume);
1171
1172static int max3107_probe_generic(struct spi_device *spi)
1173{
1174 return max3107_probe(spi, &generic_plat_data);
1175}
1176
1177/* Spi driver data */
1178static struct spi_driver max3107_driver = {
1179 .driver = {
1180 .name = "max3107",
1181 .bus = &spi_bus_type,
1182 .owner = THIS_MODULE,
1183 },
1184 .probe = max3107_probe_generic,
1185 .remove = __devexit_p(max3107_remove),
1186 .suspend = max3107_suspend,
1187 .resume = max3107_resume,
1188};
1189
1190/* Driver init function */
1191static int __init max3107_init(void)
1192{
1193 pr_info("enter max3107 init\n");
1194 return spi_register_driver(&max3107_driver);
1195}
1196
1197/* Driver exit function */
1198static void __exit max3107_exit(void)
1199{
1200 pr_info("enter max3107 exit\n");
1201 /* Unregister UART driver */
1202 if (driver_registered)
1203 uart_unregister_driver(&max3107_uart_driver);
1204 spi_unregister_driver(&max3107_driver);
1205}
1206
1207module_init(max3107_init);
1208module_exit(max3107_exit);
1209
1210MODULE_DESCRIPTION("MAX3107 driver");
1211MODULE_AUTHOR("Aavamobile");
1212MODULE_ALIAS("max3107-spi");
1213MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/max3107.h b/drivers/tty/serial/max3107.h
new file mode 100644
index 000000000000..7ab632392502
--- /dev/null
+++ b/drivers/tty/serial/max3107.h
@@ -0,0 +1,441 @@
1/*
2 * max3107.h - spi uart protocol driver header for Maxim 3107
3 *
4 * Copyright (C) Aavamobile 2009
5 * Based on serial_max3100.h by Christian Pellegrin
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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef _MAX3107_H
14#define _MAX3107_H
15
16/* Serial error status definitions */
17#define MAX3107_PARITY_ERROR 1
18#define MAX3107_FRAME_ERROR 2
19#define MAX3107_OVERRUN_ERROR 4
20#define MAX3107_ALL_ERRORS (MAX3107_PARITY_ERROR | \
21 MAX3107_FRAME_ERROR | \
22 MAX3107_OVERRUN_ERROR)
23
24/* GPIO definitions */
25#define MAX3107_GPIO_BASE 88
26#define MAX3107_GPIO_COUNT 4
27
28
29/* GPIO connected to chip's reset pin */
30#define MAX3107_RESET_GPIO 87
31
32
33/* Chip reset delay */
34#define MAX3107_RESET_DELAY 10
35
36/* Chip wakeup delay */
37#define MAX3107_WAKEUP_DELAY 50
38
39
40/* Sleep mode definitions */
41#define MAX3107_DISABLE_FORCED_SLEEP 0
42#define MAX3107_ENABLE_FORCED_SLEEP 1
43#define MAX3107_DISABLE_AUTOSLEEP 2
44#define MAX3107_ENABLE_AUTOSLEEP 3
45
46
47/* Definitions for register access with SPI transfers
48 *
49 * SPI transfer format:
50 *
51 * Master to slave bits xzzzzzzzyyyyyyyy
52 * Slave to master bits aaaaaaaabbbbbbbb
53 *
54 * where:
55 * x = 0 for reads, 1 for writes
56 * z = register address
57 * y = new register value if write, 0 if read
58 * a = unspecified
59 * b = register value if read, unspecified if write
60 */
61
62/* SPI speed */
63#define MAX3107_SPI_SPEED (3125000 * 2)
64
65/* Write bit */
66#define MAX3107_WRITE_BIT (1 << 15)
67
68/* SPI TX data mask */
69#define MAX3107_SPI_RX_DATA_MASK (0x00ff)
70
71/* SPI RX data mask */
72#define MAX3107_SPI_TX_DATA_MASK (0x00ff)
73
74/* Register access masks */
75#define MAX3107_RHR_REG (0x0000) /* RX FIFO */
76#define MAX3107_THR_REG (0x0000) /* TX FIFO */
77#define MAX3107_IRQEN_REG (0x0100) /* IRQ enable */
78#define MAX3107_IRQSTS_REG (0x0200) /* IRQ status */
79#define MAX3107_LSR_IRQEN_REG (0x0300) /* LSR IRQ enable */
80#define MAX3107_LSR_IRQSTS_REG (0x0400) /* LSR IRQ status */
81#define MAX3107_SPCHR_IRQEN_REG (0x0500) /* Special char IRQ enable */
82#define MAX3107_SPCHR_IRQSTS_REG (0x0600) /* Special char IRQ status */
83#define MAX3107_STS_IRQEN_REG (0x0700) /* Status IRQ enable */
84#define MAX3107_STS_IRQSTS_REG (0x0800) /* Status IRQ status */
85#define MAX3107_MODE1_REG (0x0900) /* MODE1 */
86#define MAX3107_MODE2_REG (0x0a00) /* MODE2 */
87#define MAX3107_LCR_REG (0x0b00) /* LCR */
88#define MAX3107_RXTO_REG (0x0c00) /* RX timeout */
89#define MAX3107_HDPIXDELAY_REG (0x0d00) /* Auto transceiver delays */
90#define MAX3107_IRDA_REG (0x0e00) /* IRDA settings */
91#define MAX3107_FLOWLVL_REG (0x0f00) /* Flow control levels */
92#define MAX3107_FIFOTRIGLVL_REG (0x1000) /* FIFO IRQ trigger levels */
93#define MAX3107_TXFIFOLVL_REG (0x1100) /* TX FIFO level */
94#define MAX3107_RXFIFOLVL_REG (0x1200) /* RX FIFO level */
95#define MAX3107_FLOWCTRL_REG (0x1300) /* Flow control */
96#define MAX3107_XON1_REG (0x1400) /* XON1 character */
97#define MAX3107_XON2_REG (0x1500) /* XON2 character */
98#define MAX3107_XOFF1_REG (0x1600) /* XOFF1 character */
99#define MAX3107_XOFF2_REG (0x1700) /* XOFF2 character */
100#define MAX3107_GPIOCFG_REG (0x1800) /* GPIO config */
101#define MAX3107_GPIODATA_REG (0x1900) /* GPIO data */
102#define MAX3107_PLLCFG_REG (0x1a00) /* PLL config */
103#define MAX3107_BRGCFG_REG (0x1b00) /* Baud rate generator conf */
104#define MAX3107_BRGDIVLSB_REG (0x1c00) /* Baud rate divisor LSB */
105#define MAX3107_BRGDIVMSB_REG (0x1d00) /* Baud rate divisor MSB */
106#define MAX3107_CLKSRC_REG (0x1e00) /* Clock source */
107#define MAX3107_REVID_REG (0x1f00) /* Revision identification */
108
109/* IRQ register bits */
110#define MAX3107_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
111#define MAX3107_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */
112#define MAX3107_IRQ_STS_BIT (1 << 2) /* Status interrupt */
113#define MAX3107_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */
114#define MAX3107_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */
115#define MAX3107_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */
116#define MAX3107_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */
117#define MAX3107_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */
118
119/* LSR register bits */
120#define MAX3107_LSR_RXTO_BIT (1 << 0) /* RX timeout */
121#define MAX3107_LSR_RXOVR_BIT (1 << 1) /* RX overrun */
122#define MAX3107_LSR_RXPAR_BIT (1 << 2) /* RX parity error */
123#define MAX3107_LSR_FRERR_BIT (1 << 3) /* Frame error */
124#define MAX3107_LSR_RXBRK_BIT (1 << 4) /* RX break */
125#define MAX3107_LSR_RXNOISE_BIT (1 << 5) /* RX noise */
126#define MAX3107_LSR_UNDEF6_BIT (1 << 6) /* Undefined/not used */
127#define MAX3107_LSR_CTS_BIT (1 << 7) /* CTS pin state */
128
129/* Special character register bits */
130#define MAX3107_SPCHR_XON1_BIT (1 << 0) /* XON1 character */
131#define MAX3107_SPCHR_XON2_BIT (1 << 1) /* XON2 character */
132#define MAX3107_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */
133#define MAX3107_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */
134#define MAX3107_SPCHR_BREAK_BIT (1 << 4) /* RX break */
135#define MAX3107_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */
136#define MAX3107_SPCHR_UNDEF6_BIT (1 << 6) /* Undefined/not used */
137#define MAX3107_SPCHR_UNDEF7_BIT (1 << 7) /* Undefined/not used */
138
139/* Status register bits */
140#define MAX3107_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */
141#define MAX3107_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */
142#define MAX3107_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */
143#define MAX3107_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */
144#define MAX3107_STS_UNDEF4_BIT (1 << 4) /* Undefined/not used */
145#define MAX3107_STS_CLKREADY_BIT (1 << 5) /* Clock ready */
146#define MAX3107_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */
147#define MAX3107_STS_UNDEF7_BIT (1 << 7) /* Undefined/not used */
148
149/* MODE1 register bits */
150#define MAX3107_MODE1_RXDIS_BIT (1 << 0) /* RX disable */
151#define MAX3107_MODE1_TXDIS_BIT (1 << 1) /* TX disable */
152#define MAX3107_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */
153#define MAX3107_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */
154#define MAX3107_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */
155#define MAX3107_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */
156#define MAX3107_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */
157#define MAX3107_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */
158
159/* MODE2 register bits */
160#define MAX3107_MODE2_RST_BIT (1 << 0) /* Chip reset */
161#define MAX3107_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */
162#define MAX3107_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */
163#define MAX3107_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */
164#define MAX3107_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */
165#define MAX3107_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */
166#define MAX3107_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */
167#define MAX3107_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */
168
169/* LCR register bits */
170#define MAX3107_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
171#define MAX3107_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
172 *
173 * Word length bits table:
174 * 00 -> 5 bit words
175 * 01 -> 6 bit words
176 * 10 -> 7 bit words
177 * 11 -> 8 bit words
178 */
179#define MAX3107_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
180 *
181 * STOP length bit table:
182 * 0 -> 1 stop bit
183 * 1 -> 1-1.5 stop bits if
184 * word length is 5,
185 * 2 stop bits otherwise
186 */
187#define MAX3107_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
188#define MAX3107_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
189#define MAX3107_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
190#define MAX3107_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
191#define MAX3107_LCR_RTS_BIT (1 << 7) /* RTS pin control */
192#define MAX3107_LCR_WORD_LEN_5 (0x0000)
193#define MAX3107_LCR_WORD_LEN_6 (0x0001)
194#define MAX3107_LCR_WORD_LEN_7 (0x0002)
195#define MAX3107_LCR_WORD_LEN_8 (0x0003)
196
197
198/* IRDA register bits */
199#define MAX3107_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */
200#define MAX3107_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */
201#define MAX3107_IRDA_SHORTIR_BIT (1 << 2) /* Short SIR mode enable */
202#define MAX3107_IRDA_MIR_BIT (1 << 3) /* MIR mode enable */
203#define MAX3107_IRDA_RXINV_BIT (1 << 4) /* RX logic inversion enable */
204#define MAX3107_IRDA_TXINV_BIT (1 << 5) /* TX logic inversion enable */
205#define MAX3107_IRDA_UNDEF6_BIT (1 << 6) /* Undefined/not used */
206#define MAX3107_IRDA_UNDEF7_BIT (1 << 7) /* Undefined/not used */
207
208/* Flow control trigger level register masks */
209#define MAX3107_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */
210#define MAX3107_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */
211#define MAX3107_FLOWLVL_HALT(words) ((words/8) & 0x000f)
212#define MAX3107_FLOWLVL_RES(words) (((words/8) & 0x000f) << 4)
213
214/* FIFO interrupt trigger level register masks */
215#define MAX3107_FIFOTRIGLVL_TX_MASK (0x000f) /* TX FIFO trigger level */
216#define MAX3107_FIFOTRIGLVL_RX_MASK (0x00f0) /* RX FIFO trigger level */
217#define MAX3107_FIFOTRIGLVL_TX(words) ((words/8) & 0x000f)
218#define MAX3107_FIFOTRIGLVL_RX(words) (((words/8) & 0x000f) << 4)
219
220/* Flow control register bits */
221#define MAX3107_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */
222#define MAX3107_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */
223#define MAX3107_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs
224 * are used in conjunction with
225 * XOFF2 for definition of
226 * special character */
227#define MAX3107_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */
228#define MAX3107_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */
229#define MAX3107_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1
230 *
231 * SWFLOW bits 1 & 0 table:
232 * 00 -> no transmitter flow
233 * control
234 * 01 -> receiver compares
235 * XON2 and XOFF2
236 * and controls
237 * transmitter
238 * 10 -> receiver compares
239 * XON1 and XOFF1
240 * and controls
241 * transmitter
242 * 11 -> receiver compares
243 * XON1, XON2, XOFF1 and
244 * XOFF2 and controls
245 * transmitter
246 */
247#define MAX3107_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */
248#define MAX3107_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3
249 *
250 * SWFLOW bits 3 & 2 table:
251 * 00 -> no received flow
252 * control
253 * 01 -> transmitter generates
254 * XON2 and XOFF2
255 * 10 -> transmitter generates
256 * XON1 and XOFF1
257 * 11 -> transmitter generates
258 * XON1, XON2, XOFF1 and
259 * XOFF2
260 */
261
262/* GPIO configuration register bits */
263#define MAX3107_GPIOCFG_GP0OUT_BIT (1 << 0) /* GPIO 0 output enable */
264#define MAX3107_GPIOCFG_GP1OUT_BIT (1 << 1) /* GPIO 1 output enable */
265#define MAX3107_GPIOCFG_GP2OUT_BIT (1 << 2) /* GPIO 2 output enable */
266#define MAX3107_GPIOCFG_GP3OUT_BIT (1 << 3) /* GPIO 3 output enable */
267#define MAX3107_GPIOCFG_GP0OD_BIT (1 << 4) /* GPIO 0 open-drain enable */
268#define MAX3107_GPIOCFG_GP1OD_BIT (1 << 5) /* GPIO 1 open-drain enable */
269#define MAX3107_GPIOCFG_GP2OD_BIT (1 << 6) /* GPIO 2 open-drain enable */
270#define MAX3107_GPIOCFG_GP3OD_BIT (1 << 7) /* GPIO 3 open-drain enable */
271
272/* GPIO DATA register bits */
273#define MAX3107_GPIODATA_GP0OUT_BIT (1 << 0) /* GPIO 0 output value */
274#define MAX3107_GPIODATA_GP1OUT_BIT (1 << 1) /* GPIO 1 output value */
275#define MAX3107_GPIODATA_GP2OUT_BIT (1 << 2) /* GPIO 2 output value */
276#define MAX3107_GPIODATA_GP3OUT_BIT (1 << 3) /* GPIO 3 output value */
277#define MAX3107_GPIODATA_GP0IN_BIT (1 << 4) /* GPIO 0 input value */
278#define MAX3107_GPIODATA_GP1IN_BIT (1 << 5) /* GPIO 1 input value */
279#define MAX3107_GPIODATA_GP2IN_BIT (1 << 6) /* GPIO 2 input value */
280#define MAX3107_GPIODATA_GP3IN_BIT (1 << 7) /* GPIO 3 input value */
281
282/* PLL configuration register masks */
283#define MAX3107_PLLCFG_PREDIV_MASK (0x003f) /* PLL predivision value */
284#define MAX3107_PLLCFG_PLLFACTOR_MASK (0x00c0) /* PLL multiplication factor */
285
286/* Baud rate generator configuration register masks and bits */
287#define MAX3107_BRGCFG_FRACT_MASK (0x000f) /* Fractional portion of
288 * Baud rate generator divisor
289 */
290#define MAX3107_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */
291#define MAX3107_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */
292#define MAX3107_BRGCFG_UNDEF6_BIT (1 << 6) /* Undefined/not used */
293#define MAX3107_BRGCFG_UNDEF7_BIT (1 << 7) /* Undefined/not used */
294
295/* Clock source register bits */
296#define MAX3107_CLKSRC_INTOSC_BIT (1 << 0) /* Internal osc enable */
297#define MAX3107_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */
298#define MAX3107_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */
299#define MAX3107_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */
300#define MAX3107_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
301#define MAX3107_CLKSRC_UNDEF5_BIT (1 << 5) /* Undefined/not used */
302#define MAX3107_CLKSRC_UNDEF6_BIT (1 << 6) /* Undefined/not used */
303#define MAX3107_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
304
305
306/* HW definitions */
307#define MAX3107_RX_FIFO_SIZE 128
308#define MAX3107_TX_FIFO_SIZE 128
309#define MAX3107_REVID1 0x00a0
310#define MAX3107_REVID2 0x00a1
311
312
313/* Baud rate generator configuration values for external clock 13MHz */
314#define MAX3107_BRG13_B300 (0x0A9400 | 0x05)
315#define MAX3107_BRG13_B600 (0x054A00 | 0x03)
316#define MAX3107_BRG13_B1200 (0x02A500 | 0x01)
317#define MAX3107_BRG13_B2400 (0x015200 | 0x09)
318#define MAX3107_BRG13_B4800 (0x00A900 | 0x04)
319#define MAX3107_BRG13_B9600 (0x005400 | 0x0A)
320#define MAX3107_BRG13_B19200 (0x002A00 | 0x05)
321#define MAX3107_BRG13_B38400 (0x001500 | 0x03)
322#define MAX3107_BRG13_B57600 (0x000E00 | 0x02)
323#define MAX3107_BRG13_B115200 (0x000700 | 0x01)
324#define MAX3107_BRG13_B230400 (0x000300 | 0x08)
325#define MAX3107_BRG13_B460800 (0x000100 | 0x0c)
326#define MAX3107_BRG13_B921600 (0x000100 | 0x1c)
327
328/* Baud rate generator configuration values for external clock 26MHz */
329#define MAX3107_BRG26_B300 (0x152800 | 0x0A)
330#define MAX3107_BRG26_B600 (0x0A9400 | 0x05)
331#define MAX3107_BRG26_B1200 (0x054A00 | 0x03)
332#define MAX3107_BRG26_B2400 (0x02A500 | 0x01)
333#define MAX3107_BRG26_B4800 (0x015200 | 0x09)
334#define MAX3107_BRG26_B9600 (0x00A900 | 0x04)
335#define MAX3107_BRG26_B19200 (0x005400 | 0x0A)
336#define MAX3107_BRG26_B38400 (0x002A00 | 0x05)
337#define MAX3107_BRG26_B57600 (0x001C00 | 0x03)
338#define MAX3107_BRG26_B115200 (0x000E00 | 0x02)
339#define MAX3107_BRG26_B230400 (0x000700 | 0x01)
340#define MAX3107_BRG26_B460800 (0x000300 | 0x08)
341#define MAX3107_BRG26_B921600 (0x000100 | 0x0C)
342
343/* Baud rate generator configuration values for internal clock */
344#define MAX3107_BRG13_IB300 (0x008000 | 0x00)
345#define MAX3107_BRG13_IB600 (0x004000 | 0x00)
346#define MAX3107_BRG13_IB1200 (0x002000 | 0x00)
347#define MAX3107_BRG13_IB2400 (0x001000 | 0x00)
348#define MAX3107_BRG13_IB4800 (0x000800 | 0x00)
349#define MAX3107_BRG13_IB9600 (0x000400 | 0x00)
350#define MAX3107_BRG13_IB19200 (0x000200 | 0x00)
351#define MAX3107_BRG13_IB38400 (0x000100 | 0x00)
352#define MAX3107_BRG13_IB57600 (0x000000 | 0x0B)
353#define MAX3107_BRG13_IB115200 (0x000000 | 0x05)
354#define MAX3107_BRG13_IB230400 (0x000000 | 0x03)
355#define MAX3107_BRG13_IB460800 (0x000000 | 0x00)
356#define MAX3107_BRG13_IB921600 (0x000000 | 0x00)
357
358
359struct baud_table {
360 int baud;
361 u32 new_brg;
362};
363
364struct max3107_port {
365 /* UART port structure */
366 struct uart_port port;
367
368 /* SPI device structure */
369 struct spi_device *spi;
370
371#if defined(CONFIG_GPIOLIB)
372 /* GPIO chip stucture */
373 struct gpio_chip chip;
374#endif
375
376 /* Workqueue that does all the magic */
377 struct workqueue_struct *workqueue;
378 struct work_struct work;
379
380 /* Lock for shared data */
381 spinlock_t data_lock;
382
383 /* Device configuration */
384 int ext_clk; /* 1 if external clock used */
385 int loopback; /* Current loopback mode state */
386 int baud; /* Current baud rate */
387
388 /* State flags */
389 int suspended; /* Indicates suspend mode */
390 int tx_fifo_empty; /* Flag for TX FIFO state */
391 int rx_enabled; /* Flag for receiver state */
392 int tx_enabled; /* Flag for transmitter state */
393
394 u16 irqen_reg; /* Current IRQ enable register value */
395 /* Shared data */
396 u16 mode1_reg; /* Current mode1 register value*/
397 int mode1_commit; /* Flag for setting new mode1 register value */
398 u16 lcr_reg; /* Current LCR register value */
399 int lcr_commit; /* Flag for setting new LCR register value */
400 u32 brg_cfg; /* Current Baud rate generator config */
401 int brg_commit; /* Flag for setting new baud rate generator
402 * config
403 */
404 struct baud_table *baud_tbl;
405 int handle_irq; /* Indicates that IRQ should be handled */
406
407 /* Rx buffer and str*/
408 u16 *rxbuf;
409 u8 *rxstr;
410 /* Tx buffer*/
411 u16 *txbuf;
412
413 struct max3107_plat *pdata; /* Platform data */
414};
415
416/* Platform data structure */
417struct max3107_plat {
418 /* Loopback mode enable */
419 int loopback;
420 /* External clock enable */
421 int ext_clk;
422 /* Called during the register initialisation */
423 void (*init)(struct max3107_port *s);
424 /* Called when the port is found and configured */
425 int (*configure)(struct max3107_port *s);
426 /* HW suspend function */
427 void (*hw_suspend) (struct max3107_port *s, int suspend);
428 /* Polling mode enable */
429 int polled_mode;
430 /* Polling period if polling mode enabled */
431 int poll_time;
432};
433
434extern int max3107_rw(struct max3107_port *s, u8 *tx, u8 *rx, int len);
435extern void max3107_hw_susp(struct max3107_port *s, int suspend);
436extern int max3107_probe(struct spi_device *spi, struct max3107_plat *pdata);
437extern int max3107_remove(struct spi_device *spi);
438extern int max3107_suspend(struct spi_device *spi, pm_message_t state);
439extern int max3107_resume(struct spi_device *spi);
440
441#endif /* _LINUX_SERIAL_MAX3107_H */
diff --git a/drivers/tty/serial/mcf.c b/drivers/tty/serial/mcf.c
new file mode 100644
index 000000000000..3394b7cc1722
--- /dev/null
+++ b/drivers/tty/serial/mcf.c
@@ -0,0 +1,662 @@
1/****************************************************************************/
2
3/*
4 * mcf.c -- Freescale ColdFire UART driver
5 *
6 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
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
14/****************************************************************************/
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/console.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/serial.h>
24#include <linux/serial_core.h>
25#include <linux/io.h>
26#include <asm/coldfire.h>
27#include <asm/mcfsim.h>
28#include <asm/mcfuart.h>
29#include <asm/nettel.h>
30
31/****************************************************************************/
32
33/*
34 * Some boards implement the DTR/DCD lines using GPIO lines, most
35 * don't. Dummy out the access macros for those that don't. Those
36 * that do should define these macros somewhere in there board
37 * specific inlude files.
38 */
39#if !defined(mcf_getppdcd)
40#define mcf_getppdcd(p) (1)
41#endif
42#if !defined(mcf_getppdtr)
43#define mcf_getppdtr(p) (1)
44#endif
45#if !defined(mcf_setppdtr)
46#define mcf_setppdtr(p, v) do { } while (0)
47#endif
48
49/****************************************************************************/
50
51/*
52 * Local per-uart structure.
53 */
54struct mcf_uart {
55 struct uart_port port;
56 unsigned int sigs; /* Local copy of line sigs */
57 unsigned char imr; /* Local IMR mirror */
58};
59
60/****************************************************************************/
61
62static unsigned int mcf_tx_empty(struct uart_port *port)
63{
64 return (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXEMPTY) ?
65 TIOCSER_TEMT : 0;
66}
67
68/****************************************************************************/
69
70static unsigned int mcf_get_mctrl(struct uart_port *port)
71{
72 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
73 unsigned int sigs;
74
75 sigs = (readb(port->membase + MCFUART_UIPR) & MCFUART_UIPR_CTS) ?
76 0 : TIOCM_CTS;
77 sigs |= (pp->sigs & TIOCM_RTS);
78 sigs |= (mcf_getppdcd(port->line) ? TIOCM_CD : 0);
79 sigs |= (mcf_getppdtr(port->line) ? TIOCM_DTR : 0);
80
81 return sigs;
82}
83
84/****************************************************************************/
85
86static void mcf_set_mctrl(struct uart_port *port, unsigned int sigs)
87{
88 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
89
90 pp->sigs = sigs;
91 mcf_setppdtr(port->line, (sigs & TIOCM_DTR));
92 if (sigs & TIOCM_RTS)
93 writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP1);
94 else
95 writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP0);
96}
97
98/****************************************************************************/
99
100static void mcf_start_tx(struct uart_port *port)
101{
102 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
103
104 pp->imr |= MCFUART_UIR_TXREADY;
105 writeb(pp->imr, port->membase + MCFUART_UIMR);
106}
107
108/****************************************************************************/
109
110static void mcf_stop_tx(struct uart_port *port)
111{
112 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
113
114 pp->imr &= ~MCFUART_UIR_TXREADY;
115 writeb(pp->imr, port->membase + MCFUART_UIMR);
116}
117
118/****************************************************************************/
119
120static void mcf_stop_rx(struct uart_port *port)
121{
122 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
123
124 pp->imr &= ~MCFUART_UIR_RXREADY;
125 writeb(pp->imr, port->membase + MCFUART_UIMR);
126}
127
128/****************************************************************************/
129
130static void mcf_break_ctl(struct uart_port *port, int break_state)
131{
132 unsigned long flags;
133
134 spin_lock_irqsave(&port->lock, flags);
135 if (break_state == -1)
136 writeb(MCFUART_UCR_CMDBREAKSTART, port->membase + MCFUART_UCR);
137 else
138 writeb(MCFUART_UCR_CMDBREAKSTOP, port->membase + MCFUART_UCR);
139 spin_unlock_irqrestore(&port->lock, flags);
140}
141
142/****************************************************************************/
143
144static void mcf_enable_ms(struct uart_port *port)
145{
146}
147
148/****************************************************************************/
149
150static int mcf_startup(struct uart_port *port)
151{
152 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
153 unsigned long flags;
154
155 spin_lock_irqsave(&port->lock, flags);
156
157 /* Reset UART, get it into known state... */
158 writeb(MCFUART_UCR_CMDRESETRX, port->membase + MCFUART_UCR);
159 writeb(MCFUART_UCR_CMDRESETTX, port->membase + MCFUART_UCR);
160
161 /* Enable the UART transmitter and receiver */
162 writeb(MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE,
163 port->membase + MCFUART_UCR);
164
165 /* Enable RX interrupts now */
166 pp->imr = MCFUART_UIR_RXREADY;
167 writeb(pp->imr, port->membase + MCFUART_UIMR);
168
169 spin_unlock_irqrestore(&port->lock, flags);
170
171 return 0;
172}
173
174/****************************************************************************/
175
176static void mcf_shutdown(struct uart_port *port)
177{
178 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
179 unsigned long flags;
180
181 spin_lock_irqsave(&port->lock, flags);
182
183 /* Disable all interrupts now */
184 pp->imr = 0;
185 writeb(pp->imr, port->membase + MCFUART_UIMR);
186
187 /* Disable UART transmitter and receiver */
188 writeb(MCFUART_UCR_CMDRESETRX, port->membase + MCFUART_UCR);
189 writeb(MCFUART_UCR_CMDRESETTX, port->membase + MCFUART_UCR);
190
191 spin_unlock_irqrestore(&port->lock, flags);
192}
193
194/****************************************************************************/
195
196static void mcf_set_termios(struct uart_port *port, struct ktermios *termios,
197 struct ktermios *old)
198{
199 unsigned long flags;
200 unsigned int baud, baudclk;
201#if defined(CONFIG_M5272)
202 unsigned int baudfr;
203#endif
204 unsigned char mr1, mr2;
205
206 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
207#if defined(CONFIG_M5272)
208 baudclk = (MCF_BUSCLK / baud) / 32;
209 baudfr = (((MCF_BUSCLK / baud) + 1) / 2) % 16;
210#else
211 baudclk = ((MCF_BUSCLK / baud) + 16) / 32;
212#endif
213
214 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
215 mr2 = 0;
216
217 switch (termios->c_cflag & CSIZE) {
218 case CS5: mr1 |= MCFUART_MR1_CS5; break;
219 case CS6: mr1 |= MCFUART_MR1_CS6; break;
220 case CS7: mr1 |= MCFUART_MR1_CS7; break;
221 case CS8:
222 default: mr1 |= MCFUART_MR1_CS8; break;
223 }
224
225 if (termios->c_cflag & PARENB) {
226 if (termios->c_cflag & CMSPAR) {
227 if (termios->c_cflag & PARODD)
228 mr1 |= MCFUART_MR1_PARITYMARK;
229 else
230 mr1 |= MCFUART_MR1_PARITYSPACE;
231 } else {
232 if (termios->c_cflag & PARODD)
233 mr1 |= MCFUART_MR1_PARITYODD;
234 else
235 mr1 |= MCFUART_MR1_PARITYEVEN;
236 }
237 } else {
238 mr1 |= MCFUART_MR1_PARITYNONE;
239 }
240
241 if (termios->c_cflag & CSTOPB)
242 mr2 |= MCFUART_MR2_STOP2;
243 else
244 mr2 |= MCFUART_MR2_STOP1;
245
246 if (termios->c_cflag & CRTSCTS) {
247 mr1 |= MCFUART_MR1_RXRTS;
248 mr2 |= MCFUART_MR2_TXCTS;
249 }
250
251 spin_lock_irqsave(&port->lock, flags);
252 uart_update_timeout(port, termios->c_cflag, baud);
253 writeb(MCFUART_UCR_CMDRESETRX, port->membase + MCFUART_UCR);
254 writeb(MCFUART_UCR_CMDRESETTX, port->membase + MCFUART_UCR);
255 writeb(MCFUART_UCR_CMDRESETMRPTR, port->membase + MCFUART_UCR);
256 writeb(mr1, port->membase + MCFUART_UMR);
257 writeb(mr2, port->membase + MCFUART_UMR);
258 writeb((baudclk & 0xff00) >> 8, port->membase + MCFUART_UBG1);
259 writeb((baudclk & 0xff), port->membase + MCFUART_UBG2);
260#if defined(CONFIG_M5272)
261 writeb((baudfr & 0x0f), port->membase + MCFUART_UFPD);
262#endif
263 writeb(MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER,
264 port->membase + MCFUART_UCSR);
265 writeb(MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE,
266 port->membase + MCFUART_UCR);
267 spin_unlock_irqrestore(&port->lock, flags);
268}
269
270/****************************************************************************/
271
272static void mcf_rx_chars(struct mcf_uart *pp)
273{
274 struct uart_port *port = &pp->port;
275 unsigned char status, ch, flag;
276
277 while ((status = readb(port->membase + MCFUART_USR)) & MCFUART_USR_RXREADY) {
278 ch = readb(port->membase + MCFUART_URB);
279 flag = TTY_NORMAL;
280 port->icount.rx++;
281
282 if (status & MCFUART_USR_RXERR) {
283 writeb(MCFUART_UCR_CMDRESETERR,
284 port->membase + MCFUART_UCR);
285
286 if (status & MCFUART_USR_RXBREAK) {
287 port->icount.brk++;
288 if (uart_handle_break(port))
289 continue;
290 } else if (status & MCFUART_USR_RXPARITY) {
291 port->icount.parity++;
292 } else if (status & MCFUART_USR_RXOVERRUN) {
293 port->icount.overrun++;
294 } else if (status & MCFUART_USR_RXFRAMING) {
295 port->icount.frame++;
296 }
297
298 status &= port->read_status_mask;
299
300 if (status & MCFUART_USR_RXBREAK)
301 flag = TTY_BREAK;
302 else if (status & MCFUART_USR_RXPARITY)
303 flag = TTY_PARITY;
304 else if (status & MCFUART_USR_RXFRAMING)
305 flag = TTY_FRAME;
306 }
307
308 if (uart_handle_sysrq_char(port, ch))
309 continue;
310 uart_insert_char(port, status, MCFUART_USR_RXOVERRUN, ch, flag);
311 }
312
313 tty_flip_buffer_push(port->state->port.tty);
314}
315
316/****************************************************************************/
317
318static void mcf_tx_chars(struct mcf_uart *pp)
319{
320 struct uart_port *port = &pp->port;
321 struct circ_buf *xmit = &port->state->xmit;
322
323 if (port->x_char) {
324 /* Send special char - probably flow control */
325 writeb(port->x_char, port->membase + MCFUART_UTB);
326 port->x_char = 0;
327 port->icount.tx++;
328 return;
329 }
330
331 while (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY) {
332 if (xmit->head == xmit->tail)
333 break;
334 writeb(xmit->buf[xmit->tail], port->membase + MCFUART_UTB);
335 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
336 port->icount.tx++;
337 }
338
339 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
340 uart_write_wakeup(port);
341
342 if (xmit->head == xmit->tail) {
343 pp->imr &= ~MCFUART_UIR_TXREADY;
344 writeb(pp->imr, port->membase + MCFUART_UIMR);
345 }
346}
347
348/****************************************************************************/
349
350static irqreturn_t mcf_interrupt(int irq, void *data)
351{
352 struct uart_port *port = data;
353 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
354 unsigned int isr;
355 irqreturn_t ret = IRQ_NONE;
356
357 isr = readb(port->membase + MCFUART_UISR) & pp->imr;
358
359 spin_lock(&port->lock);
360 if (isr & MCFUART_UIR_RXREADY) {
361 mcf_rx_chars(pp);
362 ret = IRQ_HANDLED;
363 }
364 if (isr & MCFUART_UIR_TXREADY) {
365 mcf_tx_chars(pp);
366 ret = IRQ_HANDLED;
367 }
368 spin_unlock(&port->lock);
369
370 return ret;
371}
372
373/****************************************************************************/
374
375static void mcf_config_port(struct uart_port *port, int flags)
376{
377 port->type = PORT_MCF;
378 port->fifosize = MCFUART_TXFIFOSIZE;
379
380 /* Clear mask, so no surprise interrupts. */
381 writeb(0, port->membase + MCFUART_UIMR);
382
383 if (request_irq(port->irq, mcf_interrupt, IRQF_DISABLED, "UART", port))
384 printk(KERN_ERR "MCF: unable to attach ColdFire UART %d "
385 "interrupt vector=%d\n", port->line, port->irq);
386}
387
388/****************************************************************************/
389
390static const char *mcf_type(struct uart_port *port)
391{
392 return (port->type == PORT_MCF) ? "ColdFire UART" : NULL;
393}
394
395/****************************************************************************/
396
397static int mcf_request_port(struct uart_port *port)
398{
399 /* UARTs always present */
400 return 0;
401}
402
403/****************************************************************************/
404
405static void mcf_release_port(struct uart_port *port)
406{
407 /* Nothing to release... */
408}
409
410/****************************************************************************/
411
412static int mcf_verify_port(struct uart_port *port, struct serial_struct *ser)
413{
414 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_MCF))
415 return -EINVAL;
416 return 0;
417}
418
419/****************************************************************************/
420
421/*
422 * Define the basic serial functions we support.
423 */
424static const struct uart_ops mcf_uart_ops = {
425 .tx_empty = mcf_tx_empty,
426 .get_mctrl = mcf_get_mctrl,
427 .set_mctrl = mcf_set_mctrl,
428 .start_tx = mcf_start_tx,
429 .stop_tx = mcf_stop_tx,
430 .stop_rx = mcf_stop_rx,
431 .enable_ms = mcf_enable_ms,
432 .break_ctl = mcf_break_ctl,
433 .startup = mcf_startup,
434 .shutdown = mcf_shutdown,
435 .set_termios = mcf_set_termios,
436 .type = mcf_type,
437 .request_port = mcf_request_port,
438 .release_port = mcf_release_port,
439 .config_port = mcf_config_port,
440 .verify_port = mcf_verify_port,
441};
442
443static struct mcf_uart mcf_ports[4];
444
445#define MCF_MAXPORTS ARRAY_SIZE(mcf_ports)
446
447/****************************************************************************/
448#if defined(CONFIG_SERIAL_MCF_CONSOLE)
449/****************************************************************************/
450
451int __init early_mcf_setup(struct mcf_platform_uart *platp)
452{
453 struct uart_port *port;
454 int i;
455
456 for (i = 0; ((i < MCF_MAXPORTS) && (platp[i].mapbase)); i++) {
457 port = &mcf_ports[i].port;
458
459 port->line = i;
460 port->type = PORT_MCF;
461 port->mapbase = platp[i].mapbase;
462 port->membase = (platp[i].membase) ? platp[i].membase :
463 (unsigned char __iomem *) port->mapbase;
464 port->iotype = SERIAL_IO_MEM;
465 port->irq = platp[i].irq;
466 port->uartclk = MCF_BUSCLK;
467 port->flags = ASYNC_BOOT_AUTOCONF;
468 port->ops = &mcf_uart_ops;
469 }
470
471 return 0;
472}
473
474/****************************************************************************/
475
476static void mcf_console_putc(struct console *co, const char c)
477{
478 struct uart_port *port = &(mcf_ports + co->index)->port;
479 int i;
480
481 for (i = 0; (i < 0x10000); i++) {
482 if (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY)
483 break;
484 }
485 writeb(c, port->membase + MCFUART_UTB);
486 for (i = 0; (i < 0x10000); i++) {
487 if (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY)
488 break;
489 }
490}
491
492/****************************************************************************/
493
494static void mcf_console_write(struct console *co, const char *s, unsigned int count)
495{
496 for (; (count); count--, s++) {
497 mcf_console_putc(co, *s);
498 if (*s == '\n')
499 mcf_console_putc(co, '\r');
500 }
501}
502
503/****************************************************************************/
504
505static int __init mcf_console_setup(struct console *co, char *options)
506{
507 struct uart_port *port;
508 int baud = CONFIG_SERIAL_MCF_BAUDRATE;
509 int bits = 8;
510 int parity = 'n';
511 int flow = 'n';
512
513 if ((co->index < 0) || (co->index >= MCF_MAXPORTS))
514 co->index = 0;
515 port = &mcf_ports[co->index].port;
516 if (port->membase == 0)
517 return -ENODEV;
518
519 if (options)
520 uart_parse_options(options, &baud, &parity, &bits, &flow);
521
522 return uart_set_options(port, co, baud, parity, bits, flow);
523}
524
525/****************************************************************************/
526
527static struct uart_driver mcf_driver;
528
529static struct console mcf_console = {
530 .name = "ttyS",
531 .write = mcf_console_write,
532 .device = uart_console_device,
533 .setup = mcf_console_setup,
534 .flags = CON_PRINTBUFFER,
535 .index = -1,
536 .data = &mcf_driver,
537};
538
539static int __init mcf_console_init(void)
540{
541 register_console(&mcf_console);
542 return 0;
543}
544
545console_initcall(mcf_console_init);
546
547#define MCF_CONSOLE &mcf_console
548
549/****************************************************************************/
550#else
551/****************************************************************************/
552
553#define MCF_CONSOLE NULL
554
555/****************************************************************************/
556#endif /* CONFIG_MCF_CONSOLE */
557/****************************************************************************/
558
559/*
560 * Define the mcf UART driver structure.
561 */
562static struct uart_driver mcf_driver = {
563 .owner = THIS_MODULE,
564 .driver_name = "mcf",
565 .dev_name = "ttyS",
566 .major = TTY_MAJOR,
567 .minor = 64,
568 .nr = MCF_MAXPORTS,
569 .cons = MCF_CONSOLE,
570};
571
572/****************************************************************************/
573
574static int __devinit mcf_probe(struct platform_device *pdev)
575{
576 struct mcf_platform_uart *platp = pdev->dev.platform_data;
577 struct uart_port *port;
578 int i;
579
580 for (i = 0; ((i < MCF_MAXPORTS) && (platp[i].mapbase)); i++) {
581 port = &mcf_ports[i].port;
582
583 port->line = i;
584 port->type = PORT_MCF;
585 port->mapbase = platp[i].mapbase;
586 port->membase = (platp[i].membase) ? platp[i].membase :
587 (unsigned char __iomem *) platp[i].mapbase;
588 port->iotype = SERIAL_IO_MEM;
589 port->irq = platp[i].irq;
590 port->uartclk = MCF_BUSCLK;
591 port->ops = &mcf_uart_ops;
592 port->flags = ASYNC_BOOT_AUTOCONF;
593
594 uart_add_one_port(&mcf_driver, port);
595 }
596
597 return 0;
598}
599
600/****************************************************************************/
601
602static int __devexit mcf_remove(struct platform_device *pdev)
603{
604 struct uart_port *port;
605 int i;
606
607 for (i = 0; (i < MCF_MAXPORTS); i++) {
608 port = &mcf_ports[i].port;
609 if (port)
610 uart_remove_one_port(&mcf_driver, port);
611 }
612
613 return 0;
614}
615
616/****************************************************************************/
617
618static struct platform_driver mcf_platform_driver = {
619 .probe = mcf_probe,
620 .remove = __devexit_p(mcf_remove),
621 .driver = {
622 .name = "mcfuart",
623 .owner = THIS_MODULE,
624 },
625};
626
627/****************************************************************************/
628
629static int __init mcf_init(void)
630{
631 int rc;
632
633 printk("ColdFire internal UART serial driver\n");
634
635 rc = uart_register_driver(&mcf_driver);
636 if (rc)
637 return rc;
638 rc = platform_driver_register(&mcf_platform_driver);
639 if (rc)
640 return rc;
641 return 0;
642}
643
644/****************************************************************************/
645
646static void __exit mcf_exit(void)
647{
648 platform_driver_unregister(&mcf_platform_driver);
649 uart_unregister_driver(&mcf_driver);
650}
651
652/****************************************************************************/
653
654module_init(mcf_init);
655module_exit(mcf_exit);
656
657MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>");
658MODULE_DESCRIPTION("Freescale ColdFire UART driver");
659MODULE_LICENSE("GPL");
660MODULE_ALIAS("platform:mcfuart");
661
662/****************************************************************************/
diff --git a/drivers/tty/serial/mfd.c b/drivers/tty/serial/mfd.c
new file mode 100644
index 000000000000..d40010a22ecd
--- /dev/null
+++ b/drivers/tty/serial/mfd.c
@@ -0,0 +1,1513 @@
1/*
2 * mfd.c: driver for High Speed UART device of Intel Medfield platform
3 *
4 * Refer pxa.c, 8250.c and some other drivers in drivers/serial/
5 *
6 * (C) Copyright 2010 Intel Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
11 * of the License.
12 */
13
14/* Notes:
15 * 1. DMA channel allocation: 0/1 channel are assigned to port 0,
16 * 2/3 chan to port 1, 4/5 chan to port 3. Even number chans
17 * are used for RX, odd chans for TX
18 *
19 * 2. In A0 stepping, UART will not support TX half empty flag
20 *
21 * 3. The RI/DSR/DCD/DTR are not pinned out, DCD & DSR are always
22 * asserted, only when the HW is reset the DDCD and DDSR will
23 * be triggered
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/console.h>
29#include <linux/sysrq.h>
30#include <linux/slab.h>
31#include <linux/serial_reg.h>
32#include <linux/circ_buf.h>
33#include <linux/delay.h>
34#include <linux/interrupt.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/serial_core.h>
38#include <linux/serial_mfd.h>
39#include <linux/dma-mapping.h>
40#include <linux/pci.h>
41#include <linux/io.h>
42#include <linux/debugfs.h>
43
44#define MFD_HSU_A0_STEPPING 1
45
46#define HSU_DMA_BUF_SIZE 2048
47
48#define chan_readl(chan, offset) readl(chan->reg + offset)
49#define chan_writel(chan, offset, val) writel(val, chan->reg + offset)
50
51#define mfd_readl(obj, offset) readl(obj->reg + offset)
52#define mfd_writel(obj, offset, val) writel(val, obj->reg + offset)
53
54#define HSU_DMA_TIMEOUT_CHECK_FREQ (HZ/10)
55
56struct hsu_dma_buffer {
57 u8 *buf;
58 dma_addr_t dma_addr;
59 u32 dma_size;
60 u32 ofs;
61};
62
63struct hsu_dma_chan {
64 u32 id;
65 enum dma_data_direction dirt;
66 struct uart_hsu_port *uport;
67 void __iomem *reg;
68 struct timer_list rx_timer; /* only needed by RX channel */
69};
70
71struct uart_hsu_port {
72 struct uart_port port;
73 unsigned char ier;
74 unsigned char lcr;
75 unsigned char mcr;
76 unsigned int lsr_break_flag;
77 char name[12];
78 int index;
79 struct device *dev;
80
81 struct hsu_dma_chan *txc;
82 struct hsu_dma_chan *rxc;
83 struct hsu_dma_buffer txbuf;
84 struct hsu_dma_buffer rxbuf;
85 int use_dma; /* flag for DMA/PIO */
86 int running;
87 int dma_tx_on;
88};
89
90/* Top level data structure of HSU */
91struct hsu_port {
92 void __iomem *reg;
93 unsigned long paddr;
94 unsigned long iolen;
95 u32 irq;
96
97 struct uart_hsu_port port[3];
98 struct hsu_dma_chan chans[10];
99
100 struct dentry *debugfs;
101};
102
103static inline unsigned int serial_in(struct uart_hsu_port *up, int offset)
104{
105 unsigned int val;
106
107 if (offset > UART_MSR) {
108 offset <<= 2;
109 val = readl(up->port.membase + offset);
110 } else
111 val = (unsigned int)readb(up->port.membase + offset);
112
113 return val;
114}
115
116static inline void serial_out(struct uart_hsu_port *up, int offset, int value)
117{
118 if (offset > UART_MSR) {
119 offset <<= 2;
120 writel(value, up->port.membase + offset);
121 } else {
122 unsigned char val = value & 0xff;
123 writeb(val, up->port.membase + offset);
124 }
125}
126
127#ifdef CONFIG_DEBUG_FS
128
129#define HSU_REGS_BUFSIZE 1024
130
131static int hsu_show_regs_open(struct inode *inode, struct file *file)
132{
133 file->private_data = inode->i_private;
134 return 0;
135}
136
137static ssize_t port_show_regs(struct file *file, char __user *user_buf,
138 size_t count, loff_t *ppos)
139{
140 struct uart_hsu_port *up = file->private_data;
141 char *buf;
142 u32 len = 0;
143 ssize_t ret;
144
145 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
146 if (!buf)
147 return 0;
148
149 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
150 "MFD HSU port[%d] regs:\n", up->index);
151
152 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
153 "=================================\n");
154 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
155 "IER: \t\t0x%08x\n", serial_in(up, UART_IER));
156 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
157 "IIR: \t\t0x%08x\n", serial_in(up, UART_IIR));
158 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
159 "LCR: \t\t0x%08x\n", serial_in(up, UART_LCR));
160 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
161 "MCR: \t\t0x%08x\n", serial_in(up, UART_MCR));
162 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
163 "LSR: \t\t0x%08x\n", serial_in(up, UART_LSR));
164 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
165 "MSR: \t\t0x%08x\n", serial_in(up, UART_MSR));
166 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
167 "FOR: \t\t0x%08x\n", serial_in(up, UART_FOR));
168 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
169 "PS: \t\t0x%08x\n", serial_in(up, UART_PS));
170 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
171 "MUL: \t\t0x%08x\n", serial_in(up, UART_MUL));
172 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
173 "DIV: \t\t0x%08x\n", serial_in(up, UART_DIV));
174
175 if (len > HSU_REGS_BUFSIZE)
176 len = HSU_REGS_BUFSIZE;
177
178 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
179 kfree(buf);
180 return ret;
181}
182
183static ssize_t dma_show_regs(struct file *file, char __user *user_buf,
184 size_t count, loff_t *ppos)
185{
186 struct hsu_dma_chan *chan = file->private_data;
187 char *buf;
188 u32 len = 0;
189 ssize_t ret;
190
191 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
192 if (!buf)
193 return 0;
194
195 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
196 "MFD HSU DMA channel [%d] regs:\n", chan->id);
197
198 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
199 "=================================\n");
200 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
201 "CR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_CR));
202 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
203 "DCR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_DCR));
204 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
205 "BSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_BSR));
206 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
207 "MOTSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_MOTSR));
208 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
209 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0SAR));
210 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
211 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0TSR));
212 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
213 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1SAR));
214 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
215 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1TSR));
216 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
217 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2SAR));
218 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
219 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2TSR));
220 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
221 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3SAR));
222 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
223 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3TSR));
224
225 if (len > HSU_REGS_BUFSIZE)
226 len = HSU_REGS_BUFSIZE;
227
228 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
229 kfree(buf);
230 return ret;
231}
232
233static const struct file_operations port_regs_ops = {
234 .owner = THIS_MODULE,
235 .open = hsu_show_regs_open,
236 .read = port_show_regs,
237 .llseek = default_llseek,
238};
239
240static const struct file_operations dma_regs_ops = {
241 .owner = THIS_MODULE,
242 .open = hsu_show_regs_open,
243 .read = dma_show_regs,
244 .llseek = default_llseek,
245};
246
247static int hsu_debugfs_init(struct hsu_port *hsu)
248{
249 int i;
250 char name[32];
251
252 hsu->debugfs = debugfs_create_dir("hsu", NULL);
253 if (!hsu->debugfs)
254 return -ENOMEM;
255
256 for (i = 0; i < 3; i++) {
257 snprintf(name, sizeof(name), "port_%d_regs", i);
258 debugfs_create_file(name, S_IFREG | S_IRUGO,
259 hsu->debugfs, (void *)(&hsu->port[i]), &port_regs_ops);
260 }
261
262 for (i = 0; i < 6; i++) {
263 snprintf(name, sizeof(name), "dma_chan_%d_regs", i);
264 debugfs_create_file(name, S_IFREG | S_IRUGO,
265 hsu->debugfs, (void *)&hsu->chans[i], &dma_regs_ops);
266 }
267
268 return 0;
269}
270
271static void hsu_debugfs_remove(struct hsu_port *hsu)
272{
273 if (hsu->debugfs)
274 debugfs_remove_recursive(hsu->debugfs);
275}
276
277#else
278static inline int hsu_debugfs_init(struct hsu_port *hsu)
279{
280 return 0;
281}
282
283static inline void hsu_debugfs_remove(struct hsu_port *hsu)
284{
285}
286#endif /* CONFIG_DEBUG_FS */
287
288static void serial_hsu_enable_ms(struct uart_port *port)
289{
290 struct uart_hsu_port *up =
291 container_of(port, struct uart_hsu_port, port);
292
293 up->ier |= UART_IER_MSI;
294 serial_out(up, UART_IER, up->ier);
295}
296
297void hsu_dma_tx(struct uart_hsu_port *up)
298{
299 struct circ_buf *xmit = &up->port.state->xmit;
300 struct hsu_dma_buffer *dbuf = &up->txbuf;
301 int count;
302
303 /* test_and_set_bit may be better, but anyway it's in lock protected mode */
304 if (up->dma_tx_on)
305 return;
306
307 /* Update the circ buf info */
308 xmit->tail += dbuf->ofs;
309 xmit->tail &= UART_XMIT_SIZE - 1;
310
311 up->port.icount.tx += dbuf->ofs;
312 dbuf->ofs = 0;
313
314 /* Disable the channel */
315 chan_writel(up->txc, HSU_CH_CR, 0x0);
316
317 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&up->port)) {
318 dma_sync_single_for_device(up->port.dev,
319 dbuf->dma_addr,
320 dbuf->dma_size,
321 DMA_TO_DEVICE);
322
323 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
324 dbuf->ofs = count;
325
326 /* Reprogram the channel */
327 chan_writel(up->txc, HSU_CH_D0SAR, dbuf->dma_addr + xmit->tail);
328 chan_writel(up->txc, HSU_CH_D0TSR, count);
329
330 /* Reenable the channel */
331 chan_writel(up->txc, HSU_CH_DCR, 0x1
332 | (0x1 << 8)
333 | (0x1 << 16)
334 | (0x1 << 24));
335 up->dma_tx_on = 1;
336 chan_writel(up->txc, HSU_CH_CR, 0x1);
337 }
338
339 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
340 uart_write_wakeup(&up->port);
341}
342
343/* The buffer is already cache coherent */
344void hsu_dma_start_rx_chan(struct hsu_dma_chan *rxc, struct hsu_dma_buffer *dbuf)
345{
346 dbuf->ofs = 0;
347
348 chan_writel(rxc, HSU_CH_BSR, 32);
349 chan_writel(rxc, HSU_CH_MOTSR, 4);
350
351 chan_writel(rxc, HSU_CH_D0SAR, dbuf->dma_addr);
352 chan_writel(rxc, HSU_CH_D0TSR, dbuf->dma_size);
353 chan_writel(rxc, HSU_CH_DCR, 0x1 | (0x1 << 8)
354 | (0x1 << 16)
355 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
356 );
357 chan_writel(rxc, HSU_CH_CR, 0x3);
358
359 mod_timer(&rxc->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
360}
361
362/* Protected by spin_lock_irqsave(port->lock) */
363static void serial_hsu_start_tx(struct uart_port *port)
364{
365 struct uart_hsu_port *up =
366 container_of(port, struct uart_hsu_port, port);
367
368 if (up->use_dma) {
369 hsu_dma_tx(up);
370 } else if (!(up->ier & UART_IER_THRI)) {
371 up->ier |= UART_IER_THRI;
372 serial_out(up, UART_IER, up->ier);
373 }
374}
375
376static void serial_hsu_stop_tx(struct uart_port *port)
377{
378 struct uart_hsu_port *up =
379 container_of(port, struct uart_hsu_port, port);
380 struct hsu_dma_chan *txc = up->txc;
381
382 if (up->use_dma)
383 chan_writel(txc, HSU_CH_CR, 0x0);
384 else if (up->ier & UART_IER_THRI) {
385 up->ier &= ~UART_IER_THRI;
386 serial_out(up, UART_IER, up->ier);
387 }
388}
389
390/* This is always called in spinlock protected mode, so
391 * modify timeout timer is safe here */
392void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
393{
394 struct hsu_dma_buffer *dbuf = &up->rxbuf;
395 struct hsu_dma_chan *chan = up->rxc;
396 struct uart_port *port = &up->port;
397 struct tty_struct *tty = port->state->port.tty;
398 int count;
399
400 if (!tty)
401 return;
402
403 /*
404 * First need to know how many is already transferred,
405 * then check if its a timeout DMA irq, and return
406 * the trail bytes out, push them up and reenable the
407 * channel
408 */
409
410 /* Timeout IRQ, need wait some time, see Errata 2 */
411 if (int_sts & 0xf00)
412 udelay(2);
413
414 /* Stop the channel */
415 chan_writel(chan, HSU_CH_CR, 0x0);
416
417 count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
418 if (!count) {
419 /* Restart the channel before we leave */
420 chan_writel(chan, HSU_CH_CR, 0x3);
421 return;
422 }
423 del_timer(&chan->rx_timer);
424
425 dma_sync_single_for_cpu(port->dev, dbuf->dma_addr,
426 dbuf->dma_size, DMA_FROM_DEVICE);
427
428 /*
429 * Head will only wrap around when we recycle
430 * the DMA buffer, and when that happens, we
431 * explicitly set tail to 0. So head will
432 * always be greater than tail.
433 */
434 tty_insert_flip_string(tty, dbuf->buf, count);
435 port->icount.rx += count;
436
437 dma_sync_single_for_device(up->port.dev, dbuf->dma_addr,
438 dbuf->dma_size, DMA_FROM_DEVICE);
439
440 /* Reprogram the channel */
441 chan_writel(chan, HSU_CH_D0SAR, dbuf->dma_addr);
442 chan_writel(chan, HSU_CH_D0TSR, dbuf->dma_size);
443 chan_writel(chan, HSU_CH_DCR, 0x1
444 | (0x1 << 8)
445 | (0x1 << 16)
446 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
447 );
448 tty_flip_buffer_push(tty);
449
450 chan_writel(chan, HSU_CH_CR, 0x3);
451 chan->rx_timer.expires = jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ;
452 add_timer(&chan->rx_timer);
453
454}
455
456static void serial_hsu_stop_rx(struct uart_port *port)
457{
458 struct uart_hsu_port *up =
459 container_of(port, struct uart_hsu_port, port);
460 struct hsu_dma_chan *chan = up->rxc;
461
462 if (up->use_dma)
463 chan_writel(chan, HSU_CH_CR, 0x2);
464 else {
465 up->ier &= ~UART_IER_RLSI;
466 up->port.read_status_mask &= ~UART_LSR_DR;
467 serial_out(up, UART_IER, up->ier);
468 }
469}
470
471static inline void receive_chars(struct uart_hsu_port *up, int *status)
472{
473 struct tty_struct *tty = up->port.state->port.tty;
474 unsigned int ch, flag;
475 unsigned int max_count = 256;
476
477 if (!tty)
478 return;
479
480 do {
481 ch = serial_in(up, UART_RX);
482 flag = TTY_NORMAL;
483 up->port.icount.rx++;
484
485 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
486 UART_LSR_FE | UART_LSR_OE))) {
487
488 dev_warn(up->dev, "We really rush into ERR/BI case"
489 "status = 0x%02x", *status);
490 /* For statistics only */
491 if (*status & UART_LSR_BI) {
492 *status &= ~(UART_LSR_FE | UART_LSR_PE);
493 up->port.icount.brk++;
494 /*
495 * We do the SysRQ and SAK checking
496 * here because otherwise the break
497 * may get masked by ignore_status_mask
498 * or read_status_mask.
499 */
500 if (uart_handle_break(&up->port))
501 goto ignore_char;
502 } else if (*status & UART_LSR_PE)
503 up->port.icount.parity++;
504 else if (*status & UART_LSR_FE)
505 up->port.icount.frame++;
506 if (*status & UART_LSR_OE)
507 up->port.icount.overrun++;
508
509 /* Mask off conditions which should be ignored. */
510 *status &= up->port.read_status_mask;
511
512#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
513 if (up->port.cons &&
514 up->port.cons->index == up->port.line) {
515 /* Recover the break flag from console xmit */
516 *status |= up->lsr_break_flag;
517 up->lsr_break_flag = 0;
518 }
519#endif
520 if (*status & UART_LSR_BI) {
521 flag = TTY_BREAK;
522 } else if (*status & UART_LSR_PE)
523 flag = TTY_PARITY;
524 else if (*status & UART_LSR_FE)
525 flag = TTY_FRAME;
526 }
527
528 if (uart_handle_sysrq_char(&up->port, ch))
529 goto ignore_char;
530
531 uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
532 ignore_char:
533 *status = serial_in(up, UART_LSR);
534 } while ((*status & UART_LSR_DR) && max_count--);
535 tty_flip_buffer_push(tty);
536}
537
538static void transmit_chars(struct uart_hsu_port *up)
539{
540 struct circ_buf *xmit = &up->port.state->xmit;
541 int count;
542
543 if (up->port.x_char) {
544 serial_out(up, UART_TX, up->port.x_char);
545 up->port.icount.tx++;
546 up->port.x_char = 0;
547 return;
548 }
549 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
550 serial_hsu_stop_tx(&up->port);
551 return;
552 }
553
554#ifndef MFD_HSU_A0_STEPPING
555 count = up->port.fifosize / 2;
556#else
557 /*
558 * A0 only supports fully empty IRQ, and the first char written
559 * into it won't clear the EMPT bit, so we may need be cautious
560 * by useing a shorter buffer
561 */
562 count = up->port.fifosize - 4;
563#endif
564 do {
565 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
566 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
567
568 up->port.icount.tx++;
569 if (uart_circ_empty(xmit))
570 break;
571 } while (--count > 0);
572
573 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
574 uart_write_wakeup(&up->port);
575
576 if (uart_circ_empty(xmit))
577 serial_hsu_stop_tx(&up->port);
578}
579
580static inline void check_modem_status(struct uart_hsu_port *up)
581{
582 int status;
583
584 status = serial_in(up, UART_MSR);
585
586 if ((status & UART_MSR_ANY_DELTA) == 0)
587 return;
588
589 if (status & UART_MSR_TERI)
590 up->port.icount.rng++;
591 if (status & UART_MSR_DDSR)
592 up->port.icount.dsr++;
593 /* We may only get DDCD when HW init and reset */
594 if (status & UART_MSR_DDCD)
595 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
596 /* Will start/stop_tx accordingly */
597 if (status & UART_MSR_DCTS)
598 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
599
600 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
601}
602
603/*
604 * This handles the interrupt from one port.
605 */
606static irqreturn_t port_irq(int irq, void *dev_id)
607{
608 struct uart_hsu_port *up = dev_id;
609 unsigned int iir, lsr;
610 unsigned long flags;
611
612 if (unlikely(!up->running))
613 return IRQ_NONE;
614
615 spin_lock_irqsave(&up->port.lock, flags);
616 if (up->use_dma) {
617 lsr = serial_in(up, UART_LSR);
618 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
619 UART_LSR_FE | UART_LSR_OE)))
620 dev_warn(up->dev,
621 "Got lsr irq while using DMA, lsr = 0x%2x\n",
622 lsr);
623 check_modem_status(up);
624 spin_unlock_irqrestore(&up->port.lock, flags);
625 return IRQ_HANDLED;
626 }
627
628 iir = serial_in(up, UART_IIR);
629 if (iir & UART_IIR_NO_INT) {
630 spin_unlock_irqrestore(&up->port.lock, flags);
631 return IRQ_NONE;
632 }
633
634 lsr = serial_in(up, UART_LSR);
635 if (lsr & UART_LSR_DR)
636 receive_chars(up, &lsr);
637 check_modem_status(up);
638
639 /* lsr will be renewed during the receive_chars */
640 if (lsr & UART_LSR_THRE)
641 transmit_chars(up);
642
643 spin_unlock_irqrestore(&up->port.lock, flags);
644 return IRQ_HANDLED;
645}
646
647static inline void dma_chan_irq(struct hsu_dma_chan *chan)
648{
649 struct uart_hsu_port *up = chan->uport;
650 unsigned long flags;
651 u32 int_sts;
652
653 spin_lock_irqsave(&up->port.lock, flags);
654
655 if (!up->use_dma || !up->running)
656 goto exit;
657
658 /*
659 * No matter what situation, need read clear the IRQ status
660 * There is a bug, see Errata 5, HSD 2900918
661 */
662 int_sts = chan_readl(chan, HSU_CH_SR);
663
664 /* Rx channel */
665 if (chan->dirt == DMA_FROM_DEVICE)
666 hsu_dma_rx(up, int_sts);
667
668 /* Tx channel */
669 if (chan->dirt == DMA_TO_DEVICE) {
670 chan_writel(chan, HSU_CH_CR, 0x0);
671 up->dma_tx_on = 0;
672 hsu_dma_tx(up);
673 }
674
675exit:
676 spin_unlock_irqrestore(&up->port.lock, flags);
677 return;
678}
679
680static irqreturn_t dma_irq(int irq, void *dev_id)
681{
682 struct hsu_port *hsu = dev_id;
683 u32 int_sts, i;
684
685 int_sts = mfd_readl(hsu, HSU_GBL_DMAISR);
686
687 /* Currently we only have 6 channels may be used */
688 for (i = 0; i < 6; i++) {
689 if (int_sts & 0x1)
690 dma_chan_irq(&hsu->chans[i]);
691 int_sts >>= 1;
692 }
693
694 return IRQ_HANDLED;
695}
696
697static unsigned int serial_hsu_tx_empty(struct uart_port *port)
698{
699 struct uart_hsu_port *up =
700 container_of(port, struct uart_hsu_port, port);
701 unsigned long flags;
702 unsigned int ret;
703
704 spin_lock_irqsave(&up->port.lock, flags);
705 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
706 spin_unlock_irqrestore(&up->port.lock, flags);
707
708 return ret;
709}
710
711static unsigned int serial_hsu_get_mctrl(struct uart_port *port)
712{
713 struct uart_hsu_port *up =
714 container_of(port, struct uart_hsu_port, port);
715 unsigned char status;
716 unsigned int ret;
717
718 status = serial_in(up, UART_MSR);
719
720 ret = 0;
721 if (status & UART_MSR_DCD)
722 ret |= TIOCM_CAR;
723 if (status & UART_MSR_RI)
724 ret |= TIOCM_RNG;
725 if (status & UART_MSR_DSR)
726 ret |= TIOCM_DSR;
727 if (status & UART_MSR_CTS)
728 ret |= TIOCM_CTS;
729 return ret;
730}
731
732static void serial_hsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
733{
734 struct uart_hsu_port *up =
735 container_of(port, struct uart_hsu_port, port);
736 unsigned char mcr = 0;
737
738 if (mctrl & TIOCM_RTS)
739 mcr |= UART_MCR_RTS;
740 if (mctrl & TIOCM_DTR)
741 mcr |= UART_MCR_DTR;
742 if (mctrl & TIOCM_OUT1)
743 mcr |= UART_MCR_OUT1;
744 if (mctrl & TIOCM_OUT2)
745 mcr |= UART_MCR_OUT2;
746 if (mctrl & TIOCM_LOOP)
747 mcr |= UART_MCR_LOOP;
748
749 mcr |= up->mcr;
750
751 serial_out(up, UART_MCR, mcr);
752}
753
754static void serial_hsu_break_ctl(struct uart_port *port, int break_state)
755{
756 struct uart_hsu_port *up =
757 container_of(port, struct uart_hsu_port, port);
758 unsigned long flags;
759
760 spin_lock_irqsave(&up->port.lock, flags);
761 if (break_state == -1)
762 up->lcr |= UART_LCR_SBC;
763 else
764 up->lcr &= ~UART_LCR_SBC;
765 serial_out(up, UART_LCR, up->lcr);
766 spin_unlock_irqrestore(&up->port.lock, flags);
767}
768
769/*
770 * What special to do:
771 * 1. chose the 64B fifo mode
772 * 2. make sure not to select half empty mode for A0 stepping
773 * 3. start dma or pio depends on configuration
774 * 4. we only allocate dma memory when needed
775 */
776static int serial_hsu_startup(struct uart_port *port)
777{
778 struct uart_hsu_port *up =
779 container_of(port, struct uart_hsu_port, port);
780 unsigned long flags;
781
782 /*
783 * Clear the FIFO buffers and disable them.
784 * (they will be reenabled in set_termios())
785 */
786 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
787 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
788 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
789 serial_out(up, UART_FCR, 0);
790
791 /* Clear the interrupt registers. */
792 (void) serial_in(up, UART_LSR);
793 (void) serial_in(up, UART_RX);
794 (void) serial_in(up, UART_IIR);
795 (void) serial_in(up, UART_MSR);
796
797 /* Now, initialize the UART, default is 8n1 */
798 serial_out(up, UART_LCR, UART_LCR_WLEN8);
799
800 spin_lock_irqsave(&up->port.lock, flags);
801
802 up->port.mctrl |= TIOCM_OUT2;
803 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
804
805 /*
806 * Finally, enable interrupts. Note: Modem status interrupts
807 * are set via set_termios(), which will be occurring imminently
808 * anyway, so we don't enable them here.
809 */
810 if (!up->use_dma)
811 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE;
812 else
813 up->ier = 0;
814 serial_out(up, UART_IER, up->ier);
815
816 spin_unlock_irqrestore(&up->port.lock, flags);
817
818 /* DMA init */
819 if (up->use_dma) {
820 struct hsu_dma_buffer *dbuf;
821 struct circ_buf *xmit = &port->state->xmit;
822
823 up->dma_tx_on = 0;
824
825 /* First allocate the RX buffer */
826 dbuf = &up->rxbuf;
827 dbuf->buf = kzalloc(HSU_DMA_BUF_SIZE, GFP_KERNEL);
828 if (!dbuf->buf) {
829 up->use_dma = 0;
830 goto exit;
831 }
832 dbuf->dma_addr = dma_map_single(port->dev,
833 dbuf->buf,
834 HSU_DMA_BUF_SIZE,
835 DMA_FROM_DEVICE);
836 dbuf->dma_size = HSU_DMA_BUF_SIZE;
837
838 /* Start the RX channel right now */
839 hsu_dma_start_rx_chan(up->rxc, dbuf);
840
841 /* Next init the TX DMA */
842 dbuf = &up->txbuf;
843 dbuf->buf = xmit->buf;
844 dbuf->dma_addr = dma_map_single(port->dev,
845 dbuf->buf,
846 UART_XMIT_SIZE,
847 DMA_TO_DEVICE);
848 dbuf->dma_size = UART_XMIT_SIZE;
849
850 /* This should not be changed all around */
851 chan_writel(up->txc, HSU_CH_BSR, 32);
852 chan_writel(up->txc, HSU_CH_MOTSR, 4);
853 dbuf->ofs = 0;
854 }
855
856exit:
857 /* And clear the interrupt registers again for luck. */
858 (void) serial_in(up, UART_LSR);
859 (void) serial_in(up, UART_RX);
860 (void) serial_in(up, UART_IIR);
861 (void) serial_in(up, UART_MSR);
862
863 up->running = 1;
864 return 0;
865}
866
867static void serial_hsu_shutdown(struct uart_port *port)
868{
869 struct uart_hsu_port *up =
870 container_of(port, struct uart_hsu_port, port);
871 unsigned long flags;
872
873 del_timer_sync(&up->rxc->rx_timer);
874
875 /* Disable interrupts from this port */
876 up->ier = 0;
877 serial_out(up, UART_IER, 0);
878 up->running = 0;
879
880 spin_lock_irqsave(&up->port.lock, flags);
881 up->port.mctrl &= ~TIOCM_OUT2;
882 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
883 spin_unlock_irqrestore(&up->port.lock, flags);
884
885 /* Disable break condition and FIFOs */
886 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
887 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
888 UART_FCR_CLEAR_RCVR |
889 UART_FCR_CLEAR_XMIT);
890 serial_out(up, UART_FCR, 0);
891}
892
893static void
894serial_hsu_set_termios(struct uart_port *port, struct ktermios *termios,
895 struct ktermios *old)
896{
897 struct uart_hsu_port *up =
898 container_of(port, struct uart_hsu_port, port);
899 struct tty_struct *tty = port->state->port.tty;
900 unsigned char cval, fcr = 0;
901 unsigned long flags;
902 unsigned int baud, quot;
903 u32 ps, mul;
904
905 switch (termios->c_cflag & CSIZE) {
906 case CS5:
907 cval = UART_LCR_WLEN5;
908 break;
909 case CS6:
910 cval = UART_LCR_WLEN6;
911 break;
912 case CS7:
913 cval = UART_LCR_WLEN7;
914 break;
915 default:
916 case CS8:
917 cval = UART_LCR_WLEN8;
918 break;
919 }
920
921 /* CMSPAR isn't supported by this driver */
922 if (tty)
923 tty->termios->c_cflag &= ~CMSPAR;
924
925 if (termios->c_cflag & CSTOPB)
926 cval |= UART_LCR_STOP;
927 if (termios->c_cflag & PARENB)
928 cval |= UART_LCR_PARITY;
929 if (!(termios->c_cflag & PARODD))
930 cval |= UART_LCR_EPAR;
931
932 /*
933 * The base clk is 50Mhz, and the baud rate come from:
934 * baud = 50M * MUL / (DIV * PS * DLAB)
935 *
936 * For those basic low baud rate we can get the direct
937 * scalar from 2746800, like 115200 = 2746800/24. For those
938 * higher baud rate, we handle them case by case, mainly by
939 * adjusting the MUL/PS registers, and DIV register is kept
940 * as default value 0x3d09 to make things simple
941 */
942 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
943
944 quot = 1;
945 ps = 0x10;
946 mul = 0x3600;
947 switch (baud) {
948 case 3500000:
949 mul = 0x3345;
950 ps = 0xC;
951 break;
952 case 1843200:
953 mul = 0x2400;
954 break;
955 case 3000000:
956 case 2500000:
957 case 2000000:
958 case 1500000:
959 case 1000000:
960 case 500000:
961 /* mul/ps/quot = 0x9C4/0x10/0x1 will make a 500000 bps */
962 mul = baud / 500000 * 0x9C4;
963 break;
964 default:
965 /* Use uart_get_divisor to get quot for other baud rates */
966 quot = 0;
967 }
968
969 if (!quot)
970 quot = uart_get_divisor(port, baud);
971
972 if ((up->port.uartclk / quot) < (2400 * 16))
973 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_1B;
974 else if ((up->port.uartclk / quot) < (230400 * 16))
975 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_16B;
976 else
977 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_32B;
978
979 fcr |= UART_FCR_HSU_64B_FIFO;
980#ifdef MFD_HSU_A0_STEPPING
981 /* A0 doesn't support half empty IRQ */
982 fcr |= UART_FCR_FULL_EMPT_TXI;
983#endif
984
985 /*
986 * Ok, we're now changing the port state. Do it with
987 * interrupts disabled.
988 */
989 spin_lock_irqsave(&up->port.lock, flags);
990
991 /* Update the per-port timeout */
992 uart_update_timeout(port, termios->c_cflag, baud);
993
994 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
995 if (termios->c_iflag & INPCK)
996 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
997 if (termios->c_iflag & (BRKINT | PARMRK))
998 up->port.read_status_mask |= UART_LSR_BI;
999
1000 /* Characters to ignore */
1001 up->port.ignore_status_mask = 0;
1002 if (termios->c_iflag & IGNPAR)
1003 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1004 if (termios->c_iflag & IGNBRK) {
1005 up->port.ignore_status_mask |= UART_LSR_BI;
1006 /*
1007 * If we're ignoring parity and break indicators,
1008 * ignore overruns too (for real raw support).
1009 */
1010 if (termios->c_iflag & IGNPAR)
1011 up->port.ignore_status_mask |= UART_LSR_OE;
1012 }
1013
1014 /* Ignore all characters if CREAD is not set */
1015 if ((termios->c_cflag & CREAD) == 0)
1016 up->port.ignore_status_mask |= UART_LSR_DR;
1017
1018 /*
1019 * CTS flow control flag and modem status interrupts, disable
1020 * MSI by default
1021 */
1022 up->ier &= ~UART_IER_MSI;
1023 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
1024 up->ier |= UART_IER_MSI;
1025
1026 serial_out(up, UART_IER, up->ier);
1027
1028 if (termios->c_cflag & CRTSCTS)
1029 up->mcr |= UART_MCR_AFE | UART_MCR_RTS;
1030 else
1031 up->mcr &= ~UART_MCR_AFE;
1032
1033 serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1034 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
1035 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
1036 serial_out(up, UART_LCR, cval); /* reset DLAB */
1037 serial_out(up, UART_MUL, mul); /* set MUL */
1038 serial_out(up, UART_PS, ps); /* set PS */
1039 up->lcr = cval; /* Save LCR */
1040 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
1041 serial_out(up, UART_FCR, fcr);
1042 spin_unlock_irqrestore(&up->port.lock, flags);
1043}
1044
1045static void
1046serial_hsu_pm(struct uart_port *port, unsigned int state,
1047 unsigned int oldstate)
1048{
1049}
1050
1051static void serial_hsu_release_port(struct uart_port *port)
1052{
1053}
1054
1055static int serial_hsu_request_port(struct uart_port *port)
1056{
1057 return 0;
1058}
1059
1060static void serial_hsu_config_port(struct uart_port *port, int flags)
1061{
1062 struct uart_hsu_port *up =
1063 container_of(port, struct uart_hsu_port, port);
1064 up->port.type = PORT_MFD;
1065}
1066
1067static int
1068serial_hsu_verify_port(struct uart_port *port, struct serial_struct *ser)
1069{
1070 /* We don't want the core code to modify any port params */
1071 return -EINVAL;
1072}
1073
1074static const char *
1075serial_hsu_type(struct uart_port *port)
1076{
1077 struct uart_hsu_port *up =
1078 container_of(port, struct uart_hsu_port, port);
1079 return up->name;
1080}
1081
1082/* Mainly for uart console use */
1083static struct uart_hsu_port *serial_hsu_ports[3];
1084static struct uart_driver serial_hsu_reg;
1085
1086#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1087
1088#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1089
1090/* Wait for transmitter & holding register to empty */
1091static inline void wait_for_xmitr(struct uart_hsu_port *up)
1092{
1093 unsigned int status, tmout = 1000;
1094
1095 /* Wait up to 1ms for the character to be sent. */
1096 do {
1097 status = serial_in(up, UART_LSR);
1098
1099 if (status & UART_LSR_BI)
1100 up->lsr_break_flag = UART_LSR_BI;
1101
1102 if (--tmout == 0)
1103 break;
1104 udelay(1);
1105 } while (!(status & BOTH_EMPTY));
1106
1107 /* Wait up to 1s for flow control if necessary */
1108 if (up->port.flags & UPF_CONS_FLOW) {
1109 tmout = 1000000;
1110 while (--tmout &&
1111 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1112 udelay(1);
1113 }
1114}
1115
1116static void serial_hsu_console_putchar(struct uart_port *port, int ch)
1117{
1118 struct uart_hsu_port *up =
1119 container_of(port, struct uart_hsu_port, port);
1120
1121 wait_for_xmitr(up);
1122 serial_out(up, UART_TX, ch);
1123}
1124
1125/*
1126 * Print a string to the serial port trying not to disturb
1127 * any possible real use of the port...
1128 *
1129 * The console_lock must be held when we get here.
1130 */
1131static void
1132serial_hsu_console_write(struct console *co, const char *s, unsigned int count)
1133{
1134 struct uart_hsu_port *up = serial_hsu_ports[co->index];
1135 unsigned long flags;
1136 unsigned int ier;
1137 int locked = 1;
1138
1139 local_irq_save(flags);
1140 if (up->port.sysrq)
1141 locked = 0;
1142 else if (oops_in_progress) {
1143 locked = spin_trylock(&up->port.lock);
1144 } else
1145 spin_lock(&up->port.lock);
1146
1147 /* First save the IER then disable the interrupts */
1148 ier = serial_in(up, UART_IER);
1149 serial_out(up, UART_IER, 0);
1150
1151 uart_console_write(&up->port, s, count, serial_hsu_console_putchar);
1152
1153 /*
1154 * Finally, wait for transmitter to become empty
1155 * and restore the IER
1156 */
1157 wait_for_xmitr(up);
1158 serial_out(up, UART_IER, ier);
1159
1160 if (locked)
1161 spin_unlock(&up->port.lock);
1162 local_irq_restore(flags);
1163}
1164
1165static struct console serial_hsu_console;
1166
1167static int __init
1168serial_hsu_console_setup(struct console *co, char *options)
1169{
1170 struct uart_hsu_port *up;
1171 int baud = 115200;
1172 int bits = 8;
1173 int parity = 'n';
1174 int flow = 'n';
1175 int ret;
1176
1177 if (co->index == -1 || co->index >= serial_hsu_reg.nr)
1178 co->index = 0;
1179 up = serial_hsu_ports[co->index];
1180 if (!up)
1181 return -ENODEV;
1182
1183 if (options)
1184 uart_parse_options(options, &baud, &parity, &bits, &flow);
1185
1186 ret = uart_set_options(&up->port, co, baud, parity, bits, flow);
1187
1188 return ret;
1189}
1190
1191static struct console serial_hsu_console = {
1192 .name = "ttyMFD",
1193 .write = serial_hsu_console_write,
1194 .device = uart_console_device,
1195 .setup = serial_hsu_console_setup,
1196 .flags = CON_PRINTBUFFER,
1197 .index = 2,
1198 .data = &serial_hsu_reg,
1199};
1200#endif
1201
1202struct uart_ops serial_hsu_pops = {
1203 .tx_empty = serial_hsu_tx_empty,
1204 .set_mctrl = serial_hsu_set_mctrl,
1205 .get_mctrl = serial_hsu_get_mctrl,
1206 .stop_tx = serial_hsu_stop_tx,
1207 .start_tx = serial_hsu_start_tx,
1208 .stop_rx = serial_hsu_stop_rx,
1209 .enable_ms = serial_hsu_enable_ms,
1210 .break_ctl = serial_hsu_break_ctl,
1211 .startup = serial_hsu_startup,
1212 .shutdown = serial_hsu_shutdown,
1213 .set_termios = serial_hsu_set_termios,
1214 .pm = serial_hsu_pm,
1215 .type = serial_hsu_type,
1216 .release_port = serial_hsu_release_port,
1217 .request_port = serial_hsu_request_port,
1218 .config_port = serial_hsu_config_port,
1219 .verify_port = serial_hsu_verify_port,
1220};
1221
1222static struct uart_driver serial_hsu_reg = {
1223 .owner = THIS_MODULE,
1224 .driver_name = "MFD serial",
1225 .dev_name = "ttyMFD",
1226 .major = TTY_MAJOR,
1227 .minor = 128,
1228 .nr = 3,
1229};
1230
1231#ifdef CONFIG_PM
1232static int serial_hsu_suspend(struct pci_dev *pdev, pm_message_t state)
1233{
1234 void *priv = pci_get_drvdata(pdev);
1235 struct uart_hsu_port *up;
1236
1237 /* Make sure this is not the internal dma controller */
1238 if (priv && (pdev->device != 0x081E)) {
1239 up = priv;
1240 uart_suspend_port(&serial_hsu_reg, &up->port);
1241 }
1242
1243 pci_save_state(pdev);
1244 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1245 return 0;
1246}
1247
1248static int serial_hsu_resume(struct pci_dev *pdev)
1249{
1250 void *priv = pci_get_drvdata(pdev);
1251 struct uart_hsu_port *up;
1252 int ret;
1253
1254 pci_set_power_state(pdev, PCI_D0);
1255 pci_restore_state(pdev);
1256
1257 ret = pci_enable_device(pdev);
1258 if (ret)
1259 dev_warn(&pdev->dev,
1260 "HSU: can't re-enable device, try to continue\n");
1261
1262 if (priv && (pdev->device != 0x081E)) {
1263 up = priv;
1264 uart_resume_port(&serial_hsu_reg, &up->port);
1265 }
1266 return 0;
1267}
1268#else
1269#define serial_hsu_suspend NULL
1270#define serial_hsu_resume NULL
1271#endif
1272
1273/* temp global pointer before we settle down on using one or four PCI dev */
1274static struct hsu_port *phsu;
1275
1276static int serial_hsu_probe(struct pci_dev *pdev,
1277 const struct pci_device_id *ent)
1278{
1279 struct uart_hsu_port *uport;
1280 int index, ret;
1281
1282 printk(KERN_INFO "HSU: found PCI Serial controller(ID: %04x:%04x)\n",
1283 pdev->vendor, pdev->device);
1284
1285 switch (pdev->device) {
1286 case 0x081B:
1287 index = 0;
1288 break;
1289 case 0x081C:
1290 index = 1;
1291 break;
1292 case 0x081D:
1293 index = 2;
1294 break;
1295 case 0x081E:
1296 /* internal DMA controller */
1297 index = 3;
1298 break;
1299 default:
1300 dev_err(&pdev->dev, "HSU: out of index!");
1301 return -ENODEV;
1302 }
1303
1304 ret = pci_enable_device(pdev);
1305 if (ret)
1306 return ret;
1307
1308 if (index == 3) {
1309 /* DMA controller */
1310 ret = request_irq(pdev->irq, dma_irq, 0, "hsu_dma", phsu);
1311 if (ret) {
1312 dev_err(&pdev->dev, "can not get IRQ\n");
1313 goto err_disable;
1314 }
1315 pci_set_drvdata(pdev, phsu);
1316 } else {
1317 /* UART port 0~2 */
1318 uport = &phsu->port[index];
1319 uport->port.irq = pdev->irq;
1320 uport->port.dev = &pdev->dev;
1321 uport->dev = &pdev->dev;
1322
1323 ret = request_irq(pdev->irq, port_irq, 0, uport->name, uport);
1324 if (ret) {
1325 dev_err(&pdev->dev, "can not get IRQ\n");
1326 goto err_disable;
1327 }
1328 uart_add_one_port(&serial_hsu_reg, &uport->port);
1329
1330#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1331 if (index == 2) {
1332 register_console(&serial_hsu_console);
1333 uport->port.cons = &serial_hsu_console;
1334 }
1335#endif
1336 pci_set_drvdata(pdev, uport);
1337 }
1338
1339 return 0;
1340
1341err_disable:
1342 pci_disable_device(pdev);
1343 return ret;
1344}
1345
1346static void hsu_dma_rx_timeout(unsigned long data)
1347{
1348 struct hsu_dma_chan *chan = (void *)data;
1349 struct uart_hsu_port *up = chan->uport;
1350 struct hsu_dma_buffer *dbuf = &up->rxbuf;
1351 int count = 0;
1352 unsigned long flags;
1353
1354 spin_lock_irqsave(&up->port.lock, flags);
1355
1356 count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
1357
1358 if (!count) {
1359 mod_timer(&chan->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
1360 goto exit;
1361 }
1362
1363 hsu_dma_rx(up, 0);
1364exit:
1365 spin_unlock_irqrestore(&up->port.lock, flags);
1366}
1367
1368static void hsu_global_init(void)
1369{
1370 struct hsu_port *hsu;
1371 struct uart_hsu_port *uport;
1372 struct hsu_dma_chan *dchan;
1373 int i, ret;
1374
1375 hsu = kzalloc(sizeof(struct hsu_port), GFP_KERNEL);
1376 if (!hsu)
1377 return;
1378
1379 /* Get basic io resource and map it */
1380 hsu->paddr = 0xffa28000;
1381 hsu->iolen = 0x1000;
1382
1383 if (!(request_mem_region(hsu->paddr, hsu->iolen, "HSU global")))
1384 pr_warning("HSU: error in request mem region\n");
1385
1386 hsu->reg = ioremap_nocache((unsigned long)hsu->paddr, hsu->iolen);
1387 if (!hsu->reg) {
1388 pr_err("HSU: error in ioremap\n");
1389 ret = -ENOMEM;
1390 goto err_free_region;
1391 }
1392
1393 /* Initialise the 3 UART ports */
1394 uport = hsu->port;
1395 for (i = 0; i < 3; i++) {
1396 uport->port.type = PORT_MFD;
1397 uport->port.iotype = UPIO_MEM;
1398 uport->port.mapbase = (resource_size_t)hsu->paddr
1399 + HSU_PORT_REG_OFFSET
1400 + i * HSU_PORT_REG_LENGTH;
1401 uport->port.membase = hsu->reg + HSU_PORT_REG_OFFSET
1402 + i * HSU_PORT_REG_LENGTH;
1403
1404 sprintf(uport->name, "hsu_port%d", i);
1405 uport->port.fifosize = 64;
1406 uport->port.ops = &serial_hsu_pops;
1407 uport->port.line = i;
1408 uport->port.flags = UPF_IOREMAP;
1409 /* set the scalable maxim support rate to 2746800 bps */
1410 uport->port.uartclk = 115200 * 24 * 16;
1411
1412 uport->running = 0;
1413 uport->txc = &hsu->chans[i * 2];
1414 uport->rxc = &hsu->chans[i * 2 + 1];
1415
1416 serial_hsu_ports[i] = uport;
1417 uport->index = i;
1418 uport++;
1419 }
1420
1421 /* Initialise 6 dma channels */
1422 dchan = hsu->chans;
1423 for (i = 0; i < 6; i++) {
1424 dchan->id = i;
1425 dchan->dirt = (i & 0x1) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1426 dchan->uport = &hsu->port[i/2];
1427 dchan->reg = hsu->reg + HSU_DMA_CHANS_REG_OFFSET +
1428 i * HSU_DMA_CHANS_REG_LENGTH;
1429
1430 /* Work around for RX */
1431 if (dchan->dirt == DMA_FROM_DEVICE) {
1432 init_timer(&dchan->rx_timer);
1433 dchan->rx_timer.function = hsu_dma_rx_timeout;
1434 dchan->rx_timer.data = (unsigned long)dchan;
1435 }
1436 dchan++;
1437 }
1438
1439 phsu = hsu;
1440 hsu_debugfs_init(hsu);
1441 return;
1442
1443err_free_region:
1444 release_mem_region(hsu->paddr, hsu->iolen);
1445 kfree(hsu);
1446 return;
1447}
1448
1449static void serial_hsu_remove(struct pci_dev *pdev)
1450{
1451 void *priv = pci_get_drvdata(pdev);
1452 struct uart_hsu_port *up;
1453
1454 if (!priv)
1455 return;
1456
1457 /* For port 0/1/2, priv is the address of uart_hsu_port */
1458 if (pdev->device != 0x081E) {
1459 up = priv;
1460 uart_remove_one_port(&serial_hsu_reg, &up->port);
1461 }
1462
1463 pci_set_drvdata(pdev, NULL);
1464 free_irq(pdev->irq, priv);
1465 pci_disable_device(pdev);
1466}
1467
1468/* First 3 are UART ports, and the 4th is the DMA */
1469static const struct pci_device_id pci_ids[] __devinitdata = {
1470 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081B) },
1471 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081C) },
1472 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081D) },
1473 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081E) },
1474 {},
1475};
1476
1477static struct pci_driver hsu_pci_driver = {
1478 .name = "HSU serial",
1479 .id_table = pci_ids,
1480 .probe = serial_hsu_probe,
1481 .remove = __devexit_p(serial_hsu_remove),
1482 .suspend = serial_hsu_suspend,
1483 .resume = serial_hsu_resume,
1484};
1485
1486static int __init hsu_pci_init(void)
1487{
1488 int ret;
1489
1490 hsu_global_init();
1491
1492 ret = uart_register_driver(&serial_hsu_reg);
1493 if (ret)
1494 return ret;
1495
1496 return pci_register_driver(&hsu_pci_driver);
1497}
1498
1499static void __exit hsu_pci_exit(void)
1500{
1501 pci_unregister_driver(&hsu_pci_driver);
1502 uart_unregister_driver(&serial_hsu_reg);
1503
1504 hsu_debugfs_remove(phsu);
1505
1506 kfree(phsu);
1507}
1508
1509module_init(hsu_pci_init);
1510module_exit(hsu_pci_exit);
1511
1512MODULE_LICENSE("GPL v2");
1513MODULE_ALIAS("platform:medfield-hsu");
diff --git a/drivers/tty/serial/mpc52xx_uart.c b/drivers/tty/serial/mpc52xx_uart.c
new file mode 100644
index 000000000000..126ec7f568ec
--- /dev/null
+++ b/drivers/tty/serial/mpc52xx_uart.c
@@ -0,0 +1,1527 @@
1/*
2 * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
3 *
4 * FIXME According to the usermanual the status bits in the status register
5 * are only updated when the peripherals access the FIFO and not when the
6 * CPU access them. So since we use this bits to know when we stop writing
7 * and reading, they may not be updated in-time and a race condition may
8 * exists. But I haven't be able to prove this and I don't care. But if
9 * any problem arises, it might worth checking. The TX/RX FIFO Stats
10 * registers should be used in addition.
11 * Update: Actually, they seem updated ... At least the bits we use.
12 *
13 *
14 * Maintainer : Sylvain Munaut <tnt@246tNt.com>
15 *
16 * Some of the code has been inspired/copied from the 2.4 code written
17 * by Dale Farnsworth <dfarnsworth@mvista.com>.
18 *
19 * Copyright (C) 2008 Freescale Semiconductor Inc.
20 * John Rigby <jrigby@gmail.com>
21 * Added support for MPC5121
22 * Copyright (C) 2006 Secret Lab Technologies Ltd.
23 * Grant Likely <grant.likely@secretlab.ca>
24 * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
25 * Copyright (C) 2003 MontaVista, Software, Inc.
26 *
27 * This file is licensed under the terms of the GNU General Public License
28 * version 2. This program is licensed "as is" without any warranty of any
29 * kind, whether express or implied.
30 */
31
32#undef DEBUG
33
34#include <linux/device.h>
35#include <linux/module.h>
36#include <linux/tty.h>
37#include <linux/serial.h>
38#include <linux/sysrq.h>
39#include <linux/console.h>
40#include <linux/delay.h>
41#include <linux/io.h>
42#include <linux/of.h>
43#include <linux/of_platform.h>
44#include <linux/clk.h>
45
46#include <asm/mpc52xx.h>
47#include <asm/mpc52xx_psc.h>
48
49#if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
50#define SUPPORT_SYSRQ
51#endif
52
53#include <linux/serial_core.h>
54
55
56/* We've been assigned a range on the "Low-density serial ports" major */
57#define SERIAL_PSC_MAJOR 204
58#define SERIAL_PSC_MINOR 148
59
60
61#define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
62
63
64static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
65 /* Rem: - We use the read_status_mask as a shadow of
66 * psc->mpc52xx_psc_imr
67 * - It's important that is array is all zero on start as we
68 * use it to know if it's initialized or not ! If it's not sure
69 * it's cleared, then a memset(...,0,...) should be added to
70 * the console_init
71 */
72
73/* lookup table for matching device nodes to index numbers */
74static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
75
76static void mpc52xx_uart_of_enumerate(void);
77
78
79#define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
80
81
82/* Forward declaration of the interruption handling routine */
83static irqreturn_t mpc52xx_uart_int(int irq, void *dev_id);
84static irqreturn_t mpc5xxx_uart_process_int(struct uart_port *port);
85
86
87/* Simple macro to test if a port is console or not. This one is taken
88 * for serial_core.c and maybe should be moved to serial_core.h ? */
89#ifdef CONFIG_SERIAL_CORE_CONSOLE
90#define uart_console(port) \
91 ((port)->cons && (port)->cons->index == (port)->line)
92#else
93#define uart_console(port) (0)
94#endif
95
96/* ======================================================================== */
97/* PSC fifo operations for isolating differences between 52xx and 512x */
98/* ======================================================================== */
99
100struct psc_ops {
101 void (*fifo_init)(struct uart_port *port);
102 int (*raw_rx_rdy)(struct uart_port *port);
103 int (*raw_tx_rdy)(struct uart_port *port);
104 int (*rx_rdy)(struct uart_port *port);
105 int (*tx_rdy)(struct uart_port *port);
106 int (*tx_empty)(struct uart_port *port);
107 void (*stop_rx)(struct uart_port *port);
108 void (*start_tx)(struct uart_port *port);
109 void (*stop_tx)(struct uart_port *port);
110 void (*rx_clr_irq)(struct uart_port *port);
111 void (*tx_clr_irq)(struct uart_port *port);
112 void (*write_char)(struct uart_port *port, unsigned char c);
113 unsigned char (*read_char)(struct uart_port *port);
114 void (*cw_disable_ints)(struct uart_port *port);
115 void (*cw_restore_ints)(struct uart_port *port);
116 unsigned int (*set_baudrate)(struct uart_port *port,
117 struct ktermios *new,
118 struct ktermios *old);
119 int (*clock)(struct uart_port *port, int enable);
120 int (*fifoc_init)(void);
121 void (*fifoc_uninit)(void);
122 void (*get_irq)(struct uart_port *, struct device_node *);
123 irqreturn_t (*handle_irq)(struct uart_port *port);
124};
125
126/* setting the prescaler and divisor reg is common for all chips */
127static inline void mpc52xx_set_divisor(struct mpc52xx_psc __iomem *psc,
128 u16 prescaler, unsigned int divisor)
129{
130 /* select prescaler */
131 out_be16(&psc->mpc52xx_psc_clock_select, prescaler);
132 out_8(&psc->ctur, divisor >> 8);
133 out_8(&psc->ctlr, divisor & 0xff);
134}
135
136#ifdef CONFIG_PPC_MPC52xx
137#define FIFO_52xx(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
138static void mpc52xx_psc_fifo_init(struct uart_port *port)
139{
140 struct mpc52xx_psc __iomem *psc = PSC(port);
141 struct mpc52xx_psc_fifo __iomem *fifo = FIFO_52xx(port);
142
143 out_8(&fifo->rfcntl, 0x00);
144 out_be16(&fifo->rfalarm, 0x1ff);
145 out_8(&fifo->tfcntl, 0x07);
146 out_be16(&fifo->tfalarm, 0x80);
147
148 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
149 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
150}
151
152static int mpc52xx_psc_raw_rx_rdy(struct uart_port *port)
153{
154 return in_be16(&PSC(port)->mpc52xx_psc_status)
155 & MPC52xx_PSC_SR_RXRDY;
156}
157
158static int mpc52xx_psc_raw_tx_rdy(struct uart_port *port)
159{
160 return in_be16(&PSC(port)->mpc52xx_psc_status)
161 & MPC52xx_PSC_SR_TXRDY;
162}
163
164
165static int mpc52xx_psc_rx_rdy(struct uart_port *port)
166{
167 return in_be16(&PSC(port)->mpc52xx_psc_isr)
168 & port->read_status_mask
169 & MPC52xx_PSC_IMR_RXRDY;
170}
171
172static int mpc52xx_psc_tx_rdy(struct uart_port *port)
173{
174 return in_be16(&PSC(port)->mpc52xx_psc_isr)
175 & port->read_status_mask
176 & MPC52xx_PSC_IMR_TXRDY;
177}
178
179static int mpc52xx_psc_tx_empty(struct uart_port *port)
180{
181 return in_be16(&PSC(port)->mpc52xx_psc_status)
182 & MPC52xx_PSC_SR_TXEMP;
183}
184
185static void mpc52xx_psc_start_tx(struct uart_port *port)
186{
187 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
188 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
189}
190
191static void mpc52xx_psc_stop_tx(struct uart_port *port)
192{
193 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
194 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
195}
196
197static void mpc52xx_psc_stop_rx(struct uart_port *port)
198{
199 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
200 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
201}
202
203static void mpc52xx_psc_rx_clr_irq(struct uart_port *port)
204{
205}
206
207static void mpc52xx_psc_tx_clr_irq(struct uart_port *port)
208{
209}
210
211static void mpc52xx_psc_write_char(struct uart_port *port, unsigned char c)
212{
213 out_8(&PSC(port)->mpc52xx_psc_buffer_8, c);
214}
215
216static unsigned char mpc52xx_psc_read_char(struct uart_port *port)
217{
218 return in_8(&PSC(port)->mpc52xx_psc_buffer_8);
219}
220
221static void mpc52xx_psc_cw_disable_ints(struct uart_port *port)
222{
223 out_be16(&PSC(port)->mpc52xx_psc_imr, 0);
224}
225
226static void mpc52xx_psc_cw_restore_ints(struct uart_port *port)
227{
228 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
229}
230
231static unsigned int mpc5200_psc_set_baudrate(struct uart_port *port,
232 struct ktermios *new,
233 struct ktermios *old)
234{
235 unsigned int baud;
236 unsigned int divisor;
237
238 /* The 5200 has a fixed /32 prescaler, uartclk contains the ipb freq */
239 baud = uart_get_baud_rate(port, new, old,
240 port->uartclk / (32 * 0xffff) + 1,
241 port->uartclk / 32);
242 divisor = (port->uartclk + 16 * baud) / (32 * baud);
243
244 /* enable the /32 prescaler and set the divisor */
245 mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
246 return baud;
247}
248
249static unsigned int mpc5200b_psc_set_baudrate(struct uart_port *port,
250 struct ktermios *new,
251 struct ktermios *old)
252{
253 unsigned int baud;
254 unsigned int divisor;
255 u16 prescaler;
256
257 /* The 5200B has a selectable /4 or /32 prescaler, uartclk contains the
258 * ipb freq */
259 baud = uart_get_baud_rate(port, new, old,
260 port->uartclk / (32 * 0xffff) + 1,
261 port->uartclk / 4);
262 divisor = (port->uartclk + 2 * baud) / (4 * baud);
263
264 /* select the proper prescaler and set the divisor */
265 if (divisor > 0xffff) {
266 divisor = (divisor + 4) / 8;
267 prescaler = 0xdd00; /* /32 */
268 } else
269 prescaler = 0xff00; /* /4 */
270 mpc52xx_set_divisor(PSC(port), prescaler, divisor);
271 return baud;
272}
273
274static void mpc52xx_psc_get_irq(struct uart_port *port, struct device_node *np)
275{
276 port->irqflags = IRQF_DISABLED;
277 port->irq = irq_of_parse_and_map(np, 0);
278}
279
280/* 52xx specific interrupt handler. The caller holds the port lock */
281static irqreturn_t mpc52xx_psc_handle_irq(struct uart_port *port)
282{
283 return mpc5xxx_uart_process_int(port);
284}
285
286static struct psc_ops mpc52xx_psc_ops = {
287 .fifo_init = mpc52xx_psc_fifo_init,
288 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
289 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
290 .rx_rdy = mpc52xx_psc_rx_rdy,
291 .tx_rdy = mpc52xx_psc_tx_rdy,
292 .tx_empty = mpc52xx_psc_tx_empty,
293 .stop_rx = mpc52xx_psc_stop_rx,
294 .start_tx = mpc52xx_psc_start_tx,
295 .stop_tx = mpc52xx_psc_stop_tx,
296 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
297 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
298 .write_char = mpc52xx_psc_write_char,
299 .read_char = mpc52xx_psc_read_char,
300 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
301 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
302 .set_baudrate = mpc5200_psc_set_baudrate,
303 .get_irq = mpc52xx_psc_get_irq,
304 .handle_irq = mpc52xx_psc_handle_irq,
305};
306
307static struct psc_ops mpc5200b_psc_ops = {
308 .fifo_init = mpc52xx_psc_fifo_init,
309 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
310 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
311 .rx_rdy = mpc52xx_psc_rx_rdy,
312 .tx_rdy = mpc52xx_psc_tx_rdy,
313 .tx_empty = mpc52xx_psc_tx_empty,
314 .stop_rx = mpc52xx_psc_stop_rx,
315 .start_tx = mpc52xx_psc_start_tx,
316 .stop_tx = mpc52xx_psc_stop_tx,
317 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
318 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
319 .write_char = mpc52xx_psc_write_char,
320 .read_char = mpc52xx_psc_read_char,
321 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
322 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
323 .set_baudrate = mpc5200b_psc_set_baudrate,
324 .get_irq = mpc52xx_psc_get_irq,
325 .handle_irq = mpc52xx_psc_handle_irq,
326};
327
328#endif /* CONFIG_MPC52xx */
329
330#ifdef CONFIG_PPC_MPC512x
331#define FIFO_512x(port) ((struct mpc512x_psc_fifo __iomem *)(PSC(port)+1))
332
333/* PSC FIFO Controller for mpc512x */
334struct psc_fifoc {
335 u32 fifoc_cmd;
336 u32 fifoc_int;
337 u32 fifoc_dma;
338 u32 fifoc_axe;
339 u32 fifoc_debug;
340};
341
342static struct psc_fifoc __iomem *psc_fifoc;
343static unsigned int psc_fifoc_irq;
344
345static void mpc512x_psc_fifo_init(struct uart_port *port)
346{
347 /* /32 prescaler */
348 out_be16(&PSC(port)->mpc52xx_psc_clock_select, 0xdd00);
349
350 out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
351 out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
352 out_be32(&FIFO_512x(port)->txalarm, 1);
353 out_be32(&FIFO_512x(port)->tximr, 0);
354
355 out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
356 out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
357 out_be32(&FIFO_512x(port)->rxalarm, 1);
358 out_be32(&FIFO_512x(port)->rximr, 0);
359
360 out_be32(&FIFO_512x(port)->tximr, MPC512x_PSC_FIFO_ALARM);
361 out_be32(&FIFO_512x(port)->rximr, MPC512x_PSC_FIFO_ALARM);
362}
363
364static int mpc512x_psc_raw_rx_rdy(struct uart_port *port)
365{
366 return !(in_be32(&FIFO_512x(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
367}
368
369static int mpc512x_psc_raw_tx_rdy(struct uart_port *port)
370{
371 return !(in_be32(&FIFO_512x(port)->txsr) & MPC512x_PSC_FIFO_FULL);
372}
373
374static int mpc512x_psc_rx_rdy(struct uart_port *port)
375{
376 return in_be32(&FIFO_512x(port)->rxsr)
377 & in_be32(&FIFO_512x(port)->rximr)
378 & MPC512x_PSC_FIFO_ALARM;
379}
380
381static int mpc512x_psc_tx_rdy(struct uart_port *port)
382{
383 return in_be32(&FIFO_512x(port)->txsr)
384 & in_be32(&FIFO_512x(port)->tximr)
385 & MPC512x_PSC_FIFO_ALARM;
386}
387
388static int mpc512x_psc_tx_empty(struct uart_port *port)
389{
390 return in_be32(&FIFO_512x(port)->txsr)
391 & MPC512x_PSC_FIFO_EMPTY;
392}
393
394static void mpc512x_psc_stop_rx(struct uart_port *port)
395{
396 unsigned long rx_fifo_imr;
397
398 rx_fifo_imr = in_be32(&FIFO_512x(port)->rximr);
399 rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
400 out_be32(&FIFO_512x(port)->rximr, rx_fifo_imr);
401}
402
403static void mpc512x_psc_start_tx(struct uart_port *port)
404{
405 unsigned long tx_fifo_imr;
406
407 tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
408 tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
409 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
410}
411
412static void mpc512x_psc_stop_tx(struct uart_port *port)
413{
414 unsigned long tx_fifo_imr;
415
416 tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
417 tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
418 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
419}
420
421static void mpc512x_psc_rx_clr_irq(struct uart_port *port)
422{
423 out_be32(&FIFO_512x(port)->rxisr, in_be32(&FIFO_512x(port)->rxisr));
424}
425
426static void mpc512x_psc_tx_clr_irq(struct uart_port *port)
427{
428 out_be32(&FIFO_512x(port)->txisr, in_be32(&FIFO_512x(port)->txisr));
429}
430
431static void mpc512x_psc_write_char(struct uart_port *port, unsigned char c)
432{
433 out_8(&FIFO_512x(port)->txdata_8, c);
434}
435
436static unsigned char mpc512x_psc_read_char(struct uart_port *port)
437{
438 return in_8(&FIFO_512x(port)->rxdata_8);
439}
440
441static void mpc512x_psc_cw_disable_ints(struct uart_port *port)
442{
443 port->read_status_mask =
444 in_be32(&FIFO_512x(port)->tximr) << 16 |
445 in_be32(&FIFO_512x(port)->rximr);
446 out_be32(&FIFO_512x(port)->tximr, 0);
447 out_be32(&FIFO_512x(port)->rximr, 0);
448}
449
450static void mpc512x_psc_cw_restore_ints(struct uart_port *port)
451{
452 out_be32(&FIFO_512x(port)->tximr,
453 (port->read_status_mask >> 16) & 0x7f);
454 out_be32(&FIFO_512x(port)->rximr, port->read_status_mask & 0x7f);
455}
456
457static unsigned int mpc512x_psc_set_baudrate(struct uart_port *port,
458 struct ktermios *new,
459 struct ktermios *old)
460{
461 unsigned int baud;
462 unsigned int divisor;
463
464 /*
465 * The "MPC5121e Microcontroller Reference Manual, Rev. 3" says on
466 * pg. 30-10 that the chip supports a /32 and a /10 prescaler.
467 * Furthermore, it states that "After reset, the prescaler by 10
468 * for the UART mode is selected", but the reset register value is
469 * 0x0000 which means a /32 prescaler. This is wrong.
470 *
471 * In reality using /32 prescaler doesn't work, as it is not supported!
472 * Use /16 or /10 prescaler, see "MPC5121e Hardware Design Guide",
473 * Chapter 4.1 PSC in UART Mode.
474 * Calculate with a /16 prescaler here.
475 */
476
477 /* uartclk contains the ips freq */
478 baud = uart_get_baud_rate(port, new, old,
479 port->uartclk / (16 * 0xffff) + 1,
480 port->uartclk / 16);
481 divisor = (port->uartclk + 8 * baud) / (16 * baud);
482
483 /* enable the /16 prescaler and set the divisor */
484 mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
485 return baud;
486}
487
488/* Init PSC FIFO Controller */
489static int __init mpc512x_psc_fifoc_init(void)
490{
491 struct device_node *np;
492
493 np = of_find_compatible_node(NULL, NULL,
494 "fsl,mpc5121-psc-fifo");
495 if (!np) {
496 pr_err("%s: Can't find FIFOC node\n", __func__);
497 return -ENODEV;
498 }
499
500 psc_fifoc = of_iomap(np, 0);
501 if (!psc_fifoc) {
502 pr_err("%s: Can't map FIFOC\n", __func__);
503 of_node_put(np);
504 return -ENODEV;
505 }
506
507 psc_fifoc_irq = irq_of_parse_and_map(np, 0);
508 of_node_put(np);
509 if (psc_fifoc_irq == NO_IRQ) {
510 pr_err("%s: Can't get FIFOC irq\n", __func__);
511 iounmap(psc_fifoc);
512 return -ENODEV;
513 }
514
515 return 0;
516}
517
518static void __exit mpc512x_psc_fifoc_uninit(void)
519{
520 iounmap(psc_fifoc);
521}
522
523/* 512x specific interrupt handler. The caller holds the port lock */
524static irqreturn_t mpc512x_psc_handle_irq(struct uart_port *port)
525{
526 unsigned long fifoc_int;
527 int psc_num;
528
529 /* Read pending PSC FIFOC interrupts */
530 fifoc_int = in_be32(&psc_fifoc->fifoc_int);
531
532 /* Check if it is an interrupt for this port */
533 psc_num = (port->mapbase & 0xf00) >> 8;
534 if (test_bit(psc_num, &fifoc_int) ||
535 test_bit(psc_num + 16, &fifoc_int))
536 return mpc5xxx_uart_process_int(port);
537
538 return IRQ_NONE;
539}
540
541static int mpc512x_psc_clock(struct uart_port *port, int enable)
542{
543 struct clk *psc_clk;
544 int psc_num;
545 char clk_name[10];
546
547 if (uart_console(port))
548 return 0;
549
550 psc_num = (port->mapbase & 0xf00) >> 8;
551 snprintf(clk_name, sizeof(clk_name), "psc%d_clk", psc_num);
552 psc_clk = clk_get(port->dev, clk_name);
553 if (IS_ERR(psc_clk)) {
554 dev_err(port->dev, "Failed to get PSC clock entry!\n");
555 return -ENODEV;
556 }
557
558 dev_dbg(port->dev, "%s %sable\n", clk_name, enable ? "en" : "dis");
559
560 if (enable)
561 clk_enable(psc_clk);
562 else
563 clk_disable(psc_clk);
564
565 return 0;
566}
567
568static void mpc512x_psc_get_irq(struct uart_port *port, struct device_node *np)
569{
570 port->irqflags = IRQF_SHARED;
571 port->irq = psc_fifoc_irq;
572}
573
574static struct psc_ops mpc512x_psc_ops = {
575 .fifo_init = mpc512x_psc_fifo_init,
576 .raw_rx_rdy = mpc512x_psc_raw_rx_rdy,
577 .raw_tx_rdy = mpc512x_psc_raw_tx_rdy,
578 .rx_rdy = mpc512x_psc_rx_rdy,
579 .tx_rdy = mpc512x_psc_tx_rdy,
580 .tx_empty = mpc512x_psc_tx_empty,
581 .stop_rx = mpc512x_psc_stop_rx,
582 .start_tx = mpc512x_psc_start_tx,
583 .stop_tx = mpc512x_psc_stop_tx,
584 .rx_clr_irq = mpc512x_psc_rx_clr_irq,
585 .tx_clr_irq = mpc512x_psc_tx_clr_irq,
586 .write_char = mpc512x_psc_write_char,
587 .read_char = mpc512x_psc_read_char,
588 .cw_disable_ints = mpc512x_psc_cw_disable_ints,
589 .cw_restore_ints = mpc512x_psc_cw_restore_ints,
590 .set_baudrate = mpc512x_psc_set_baudrate,
591 .clock = mpc512x_psc_clock,
592 .fifoc_init = mpc512x_psc_fifoc_init,
593 .fifoc_uninit = mpc512x_psc_fifoc_uninit,
594 .get_irq = mpc512x_psc_get_irq,
595 .handle_irq = mpc512x_psc_handle_irq,
596};
597#endif
598
599static struct psc_ops *psc_ops;
600
601/* ======================================================================== */
602/* UART operations */
603/* ======================================================================== */
604
605static unsigned int
606mpc52xx_uart_tx_empty(struct uart_port *port)
607{
608 return psc_ops->tx_empty(port) ? TIOCSER_TEMT : 0;
609}
610
611static void
612mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
613{
614 if (mctrl & TIOCM_RTS)
615 out_8(&PSC(port)->op1, MPC52xx_PSC_OP_RTS);
616 else
617 out_8(&PSC(port)->op0, MPC52xx_PSC_OP_RTS);
618}
619
620static unsigned int
621mpc52xx_uart_get_mctrl(struct uart_port *port)
622{
623 unsigned int ret = TIOCM_DSR;
624 u8 status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
625
626 if (!(status & MPC52xx_PSC_CTS))
627 ret |= TIOCM_CTS;
628 if (!(status & MPC52xx_PSC_DCD))
629 ret |= TIOCM_CAR;
630
631 return ret;
632}
633
634static void
635mpc52xx_uart_stop_tx(struct uart_port *port)
636{
637 /* port->lock taken by caller */
638 psc_ops->stop_tx(port);
639}
640
641static void
642mpc52xx_uart_start_tx(struct uart_port *port)
643{
644 /* port->lock taken by caller */
645 psc_ops->start_tx(port);
646}
647
648static void
649mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
650{
651 unsigned long flags;
652 spin_lock_irqsave(&port->lock, flags);
653
654 port->x_char = ch;
655 if (ch) {
656 /* Make sure tx interrupts are on */
657 /* Truly necessary ??? They should be anyway */
658 psc_ops->start_tx(port);
659 }
660
661 spin_unlock_irqrestore(&port->lock, flags);
662}
663
664static void
665mpc52xx_uart_stop_rx(struct uart_port *port)
666{
667 /* port->lock taken by caller */
668 psc_ops->stop_rx(port);
669}
670
671static void
672mpc52xx_uart_enable_ms(struct uart_port *port)
673{
674 struct mpc52xx_psc __iomem *psc = PSC(port);
675
676 /* clear D_*-bits by reading them */
677 in_8(&psc->mpc52xx_psc_ipcr);
678 /* enable CTS and DCD as IPC interrupts */
679 out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
680
681 port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
682 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
683}
684
685static void
686mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
687{
688 unsigned long flags;
689 spin_lock_irqsave(&port->lock, flags);
690
691 if (ctl == -1)
692 out_8(&PSC(port)->command, MPC52xx_PSC_START_BRK);
693 else
694 out_8(&PSC(port)->command, MPC52xx_PSC_STOP_BRK);
695
696 spin_unlock_irqrestore(&port->lock, flags);
697}
698
699static int
700mpc52xx_uart_startup(struct uart_port *port)
701{
702 struct mpc52xx_psc __iomem *psc = PSC(port);
703 int ret;
704
705 if (psc_ops->clock) {
706 ret = psc_ops->clock(port, 1);
707 if (ret)
708 return ret;
709 }
710
711 /* Request IRQ */
712 ret = request_irq(port->irq, mpc52xx_uart_int,
713 port->irqflags, "mpc52xx_psc_uart", port);
714 if (ret)
715 return ret;
716
717 /* Reset/activate the port, clear and enable interrupts */
718 out_8(&psc->command, MPC52xx_PSC_RST_RX);
719 out_8(&psc->command, MPC52xx_PSC_RST_TX);
720
721 out_be32(&psc->sicr, 0); /* UART mode DCD ignored */
722
723 psc_ops->fifo_init(port);
724
725 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
726 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
727
728 return 0;
729}
730
731static void
732mpc52xx_uart_shutdown(struct uart_port *port)
733{
734 struct mpc52xx_psc __iomem *psc = PSC(port);
735
736 /* Shut down the port. Leave TX active if on a console port */
737 out_8(&psc->command, MPC52xx_PSC_RST_RX);
738 if (!uart_console(port))
739 out_8(&psc->command, MPC52xx_PSC_RST_TX);
740
741 port->read_status_mask = 0;
742 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
743
744 if (psc_ops->clock)
745 psc_ops->clock(port, 0);
746
747 /* Release interrupt */
748 free_irq(port->irq, port);
749}
750
751static void
752mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
753 struct ktermios *old)
754{
755 struct mpc52xx_psc __iomem *psc = PSC(port);
756 unsigned long flags;
757 unsigned char mr1, mr2;
758 unsigned int j;
759 unsigned int baud;
760
761 /* Prepare what we're gonna write */
762 mr1 = 0;
763
764 switch (new->c_cflag & CSIZE) {
765 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
766 break;
767 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
768 break;
769 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
770 break;
771 case CS8:
772 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
773 }
774
775 if (new->c_cflag & PARENB) {
776 mr1 |= (new->c_cflag & PARODD) ?
777 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
778 } else
779 mr1 |= MPC52xx_PSC_MODE_PARNONE;
780
781
782 mr2 = 0;
783
784 if (new->c_cflag & CSTOPB)
785 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
786 else
787 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
788 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
789 MPC52xx_PSC_MODE_ONE_STOP;
790
791 if (new->c_cflag & CRTSCTS) {
792 mr1 |= MPC52xx_PSC_MODE_RXRTS;
793 mr2 |= MPC52xx_PSC_MODE_TXCTS;
794 }
795
796 /* Get the lock */
797 spin_lock_irqsave(&port->lock, flags);
798
799 /* Do our best to flush TX & RX, so we don't lose anything */
800 /* But we don't wait indefinitely ! */
801 j = 5000000; /* Maximum wait */
802 /* FIXME Can't receive chars since set_termios might be called at early
803 * boot for the console, all stuff is not yet ready to receive at that
804 * time and that just makes the kernel oops */
805 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
806 while (!mpc52xx_uart_tx_empty(port) && --j)
807 udelay(1);
808
809 if (!j)
810 printk(KERN_ERR "mpc52xx_uart.c: "
811 "Unable to flush RX & TX fifos in-time in set_termios."
812 "Some chars may have been lost.\n");
813
814 /* Reset the TX & RX */
815 out_8(&psc->command, MPC52xx_PSC_RST_RX);
816 out_8(&psc->command, MPC52xx_PSC_RST_TX);
817
818 /* Send new mode settings */
819 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
820 out_8(&psc->mode, mr1);
821 out_8(&psc->mode, mr2);
822 baud = psc_ops->set_baudrate(port, new, old);
823
824 /* Update the per-port timeout */
825 uart_update_timeout(port, new->c_cflag, baud);
826
827 if (UART_ENABLE_MS(port, new->c_cflag))
828 mpc52xx_uart_enable_ms(port);
829
830 /* Reenable TX & RX */
831 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
832 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
833
834 /* We're all set, release the lock */
835 spin_unlock_irqrestore(&port->lock, flags);
836}
837
838static const char *
839mpc52xx_uart_type(struct uart_port *port)
840{
841 /*
842 * We keep using PORT_MPC52xx for historic reasons although it applies
843 * for MPC512x, too, but print "MPC5xxx" to not irritate users
844 */
845 return port->type == PORT_MPC52xx ? "MPC5xxx PSC" : NULL;
846}
847
848static void
849mpc52xx_uart_release_port(struct uart_port *port)
850{
851 /* remapped by us ? */
852 if (port->flags & UPF_IOREMAP) {
853 iounmap(port->membase);
854 port->membase = NULL;
855 }
856
857 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
858}
859
860static int
861mpc52xx_uart_request_port(struct uart_port *port)
862{
863 int err;
864
865 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
866 port->membase = ioremap(port->mapbase,
867 sizeof(struct mpc52xx_psc));
868
869 if (!port->membase)
870 return -EINVAL;
871
872 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
873 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
874
875 if (err && (port->flags & UPF_IOREMAP)) {
876 iounmap(port->membase);
877 port->membase = NULL;
878 }
879
880 return err;
881}
882
883static void
884mpc52xx_uart_config_port(struct uart_port *port, int flags)
885{
886 if ((flags & UART_CONFIG_TYPE)
887 && (mpc52xx_uart_request_port(port) == 0))
888 port->type = PORT_MPC52xx;
889}
890
891static int
892mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
893{
894 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
895 return -EINVAL;
896
897 if ((ser->irq != port->irq) ||
898 (ser->io_type != UPIO_MEM) ||
899 (ser->baud_base != port->uartclk) ||
900 (ser->iomem_base != (void *)port->mapbase) ||
901 (ser->hub6 != 0))
902 return -EINVAL;
903
904 return 0;
905}
906
907
908static struct uart_ops mpc52xx_uart_ops = {
909 .tx_empty = mpc52xx_uart_tx_empty,
910 .set_mctrl = mpc52xx_uart_set_mctrl,
911 .get_mctrl = mpc52xx_uart_get_mctrl,
912 .stop_tx = mpc52xx_uart_stop_tx,
913 .start_tx = mpc52xx_uart_start_tx,
914 .send_xchar = mpc52xx_uart_send_xchar,
915 .stop_rx = mpc52xx_uart_stop_rx,
916 .enable_ms = mpc52xx_uart_enable_ms,
917 .break_ctl = mpc52xx_uart_break_ctl,
918 .startup = mpc52xx_uart_startup,
919 .shutdown = mpc52xx_uart_shutdown,
920 .set_termios = mpc52xx_uart_set_termios,
921/* .pm = mpc52xx_uart_pm, Not supported yet */
922/* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
923 .type = mpc52xx_uart_type,
924 .release_port = mpc52xx_uart_release_port,
925 .request_port = mpc52xx_uart_request_port,
926 .config_port = mpc52xx_uart_config_port,
927 .verify_port = mpc52xx_uart_verify_port
928};
929
930
931/* ======================================================================== */
932/* Interrupt handling */
933/* ======================================================================== */
934
935static inline int
936mpc52xx_uart_int_rx_chars(struct uart_port *port)
937{
938 struct tty_struct *tty = port->state->port.tty;
939 unsigned char ch, flag;
940 unsigned short status;
941
942 /* While we can read, do so ! */
943 while (psc_ops->raw_rx_rdy(port)) {
944 /* Get the char */
945 ch = psc_ops->read_char(port);
946
947 /* Handle sysreq char */
948#ifdef SUPPORT_SYSRQ
949 if (uart_handle_sysrq_char(port, ch)) {
950 port->sysrq = 0;
951 continue;
952 }
953#endif
954
955 /* Store it */
956
957 flag = TTY_NORMAL;
958 port->icount.rx++;
959
960 status = in_be16(&PSC(port)->mpc52xx_psc_status);
961
962 if (status & (MPC52xx_PSC_SR_PE |
963 MPC52xx_PSC_SR_FE |
964 MPC52xx_PSC_SR_RB)) {
965
966 if (status & MPC52xx_PSC_SR_RB) {
967 flag = TTY_BREAK;
968 uart_handle_break(port);
969 port->icount.brk++;
970 } else if (status & MPC52xx_PSC_SR_PE) {
971 flag = TTY_PARITY;
972 port->icount.parity++;
973 }
974 else if (status & MPC52xx_PSC_SR_FE) {
975 flag = TTY_FRAME;
976 port->icount.frame++;
977 }
978
979 /* Clear error condition */
980 out_8(&PSC(port)->command, MPC52xx_PSC_RST_ERR_STAT);
981
982 }
983 tty_insert_flip_char(tty, ch, flag);
984 if (status & MPC52xx_PSC_SR_OE) {
985 /*
986 * Overrun is special, since it's
987 * reported immediately, and doesn't
988 * affect the current character
989 */
990 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
991 port->icount.overrun++;
992 }
993 }
994
995 spin_unlock(&port->lock);
996 tty_flip_buffer_push(tty);
997 spin_lock(&port->lock);
998
999 return psc_ops->raw_rx_rdy(port);
1000}
1001
1002static inline int
1003mpc52xx_uart_int_tx_chars(struct uart_port *port)
1004{
1005 struct circ_buf *xmit = &port->state->xmit;
1006
1007 /* Process out of band chars */
1008 if (port->x_char) {
1009 psc_ops->write_char(port, port->x_char);
1010 port->icount.tx++;
1011 port->x_char = 0;
1012 return 1;
1013 }
1014
1015 /* Nothing to do ? */
1016 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
1017 mpc52xx_uart_stop_tx(port);
1018 return 0;
1019 }
1020
1021 /* Send chars */
1022 while (psc_ops->raw_tx_rdy(port)) {
1023 psc_ops->write_char(port, xmit->buf[xmit->tail]);
1024 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1025 port->icount.tx++;
1026 if (uart_circ_empty(xmit))
1027 break;
1028 }
1029
1030 /* Wake up */
1031 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1032 uart_write_wakeup(port);
1033
1034 /* Maybe we're done after all */
1035 if (uart_circ_empty(xmit)) {
1036 mpc52xx_uart_stop_tx(port);
1037 return 0;
1038 }
1039
1040 return 1;
1041}
1042
1043static irqreturn_t
1044mpc5xxx_uart_process_int(struct uart_port *port)
1045{
1046 unsigned long pass = ISR_PASS_LIMIT;
1047 unsigned int keepgoing;
1048 u8 status;
1049
1050 /* While we have stuff to do, we continue */
1051 do {
1052 /* If we don't find anything to do, we stop */
1053 keepgoing = 0;
1054
1055 psc_ops->rx_clr_irq(port);
1056 if (psc_ops->rx_rdy(port))
1057 keepgoing |= mpc52xx_uart_int_rx_chars(port);
1058
1059 psc_ops->tx_clr_irq(port);
1060 if (psc_ops->tx_rdy(port))
1061 keepgoing |= mpc52xx_uart_int_tx_chars(port);
1062
1063 status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
1064 if (status & MPC52xx_PSC_D_DCD)
1065 uart_handle_dcd_change(port, !(status & MPC52xx_PSC_DCD));
1066
1067 if (status & MPC52xx_PSC_D_CTS)
1068 uart_handle_cts_change(port, !(status & MPC52xx_PSC_CTS));
1069
1070 /* Limit number of iteration */
1071 if (!(--pass))
1072 keepgoing = 0;
1073
1074 } while (keepgoing);
1075
1076 return IRQ_HANDLED;
1077}
1078
1079static irqreturn_t
1080mpc52xx_uart_int(int irq, void *dev_id)
1081{
1082 struct uart_port *port = dev_id;
1083 irqreturn_t ret;
1084
1085 spin_lock(&port->lock);
1086
1087 ret = psc_ops->handle_irq(port);
1088
1089 spin_unlock(&port->lock);
1090
1091 return ret;
1092}
1093
1094/* ======================================================================== */
1095/* Console ( if applicable ) */
1096/* ======================================================================== */
1097
1098#ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
1099
1100static void __init
1101mpc52xx_console_get_options(struct uart_port *port,
1102 int *baud, int *parity, int *bits, int *flow)
1103{
1104 struct mpc52xx_psc __iomem *psc = PSC(port);
1105 unsigned char mr1;
1106
1107 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
1108
1109 /* Read the mode registers */
1110 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
1111 mr1 = in_8(&psc->mode);
1112
1113 /* CT{U,L}R are write-only ! */
1114 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
1115
1116 /* Parse them */
1117 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
1118 case MPC52xx_PSC_MODE_5_BITS:
1119 *bits = 5;
1120 break;
1121 case MPC52xx_PSC_MODE_6_BITS:
1122 *bits = 6;
1123 break;
1124 case MPC52xx_PSC_MODE_7_BITS:
1125 *bits = 7;
1126 break;
1127 case MPC52xx_PSC_MODE_8_BITS:
1128 default:
1129 *bits = 8;
1130 }
1131
1132 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
1133 *parity = 'n';
1134 else
1135 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
1136}
1137
1138static void
1139mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
1140{
1141 struct uart_port *port = &mpc52xx_uart_ports[co->index];
1142 unsigned int i, j;
1143
1144 /* Disable interrupts */
1145 psc_ops->cw_disable_ints(port);
1146
1147 /* Wait the TX buffer to be empty */
1148 j = 5000000; /* Maximum wait */
1149 while (!mpc52xx_uart_tx_empty(port) && --j)
1150 udelay(1);
1151
1152 /* Write all the chars */
1153 for (i = 0; i < count; i++, s++) {
1154 /* Line return handling */
1155 if (*s == '\n')
1156 psc_ops->write_char(port, '\r');
1157
1158 /* Send the char */
1159 psc_ops->write_char(port, *s);
1160
1161 /* Wait the TX buffer to be empty */
1162 j = 20000; /* Maximum wait */
1163 while (!mpc52xx_uart_tx_empty(port) && --j)
1164 udelay(1);
1165 }
1166
1167 /* Restore interrupt state */
1168 psc_ops->cw_restore_ints(port);
1169}
1170
1171
1172static int __init
1173mpc52xx_console_setup(struct console *co, char *options)
1174{
1175 struct uart_port *port = &mpc52xx_uart_ports[co->index];
1176 struct device_node *np = mpc52xx_uart_nodes[co->index];
1177 unsigned int uartclk;
1178 struct resource res;
1179 int ret;
1180
1181 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
1182 int bits = 8;
1183 int parity = 'n';
1184 int flow = 'n';
1185
1186 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
1187 co, co->index, options);
1188
1189 if ((co->index < 0) || (co->index >= MPC52xx_PSC_MAXNUM)) {
1190 pr_debug("PSC%x out of range\n", co->index);
1191 return -EINVAL;
1192 }
1193
1194 if (!np) {
1195 pr_debug("PSC%x not found in device tree\n", co->index);
1196 return -EINVAL;
1197 }
1198
1199 pr_debug("Console on ttyPSC%x is %s\n",
1200 co->index, mpc52xx_uart_nodes[co->index]->full_name);
1201
1202 /* Fetch register locations */
1203 ret = of_address_to_resource(np, 0, &res);
1204 if (ret) {
1205 pr_debug("Could not get resources for PSC%x\n", co->index);
1206 return ret;
1207 }
1208
1209 uartclk = mpc5xxx_get_bus_frequency(np);
1210 if (uartclk == 0) {
1211 pr_debug("Could not find uart clock frequency!\n");
1212 return -EINVAL;
1213 }
1214
1215 /* Basic port init. Needed since we use some uart_??? func before
1216 * real init for early access */
1217 spin_lock_init(&port->lock);
1218 port->uartclk = uartclk;
1219 port->ops = &mpc52xx_uart_ops;
1220 port->mapbase = res.start;
1221 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
1222 port->irq = irq_of_parse_and_map(np, 0);
1223
1224 if (port->membase == NULL)
1225 return -EINVAL;
1226
1227 pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
1228 (void *)port->mapbase, port->membase,
1229 port->irq, port->uartclk);
1230
1231 /* Setup the port parameters accoding to options */
1232 if (options)
1233 uart_parse_options(options, &baud, &parity, &bits, &flow);
1234 else
1235 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
1236
1237 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
1238 baud, bits, parity, flow);
1239
1240 return uart_set_options(port, co, baud, parity, bits, flow);
1241}
1242
1243
1244static struct uart_driver mpc52xx_uart_driver;
1245
1246static struct console mpc52xx_console = {
1247 .name = "ttyPSC",
1248 .write = mpc52xx_console_write,
1249 .device = uart_console_device,
1250 .setup = mpc52xx_console_setup,
1251 .flags = CON_PRINTBUFFER,
1252 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
1253 .data = &mpc52xx_uart_driver,
1254};
1255
1256
1257static int __init
1258mpc52xx_console_init(void)
1259{
1260 mpc52xx_uart_of_enumerate();
1261 register_console(&mpc52xx_console);
1262 return 0;
1263}
1264
1265console_initcall(mpc52xx_console_init);
1266
1267#define MPC52xx_PSC_CONSOLE &mpc52xx_console
1268#else
1269#define MPC52xx_PSC_CONSOLE NULL
1270#endif
1271
1272
1273/* ======================================================================== */
1274/* UART Driver */
1275/* ======================================================================== */
1276
1277static struct uart_driver mpc52xx_uart_driver = {
1278 .driver_name = "mpc52xx_psc_uart",
1279 .dev_name = "ttyPSC",
1280 .major = SERIAL_PSC_MAJOR,
1281 .minor = SERIAL_PSC_MINOR,
1282 .nr = MPC52xx_PSC_MAXNUM,
1283 .cons = MPC52xx_PSC_CONSOLE,
1284};
1285
1286/* ======================================================================== */
1287/* OF Platform Driver */
1288/* ======================================================================== */
1289
1290static struct of_device_id mpc52xx_uart_of_match[] = {
1291#ifdef CONFIG_PPC_MPC52xx
1292 { .compatible = "fsl,mpc5200b-psc-uart", .data = &mpc5200b_psc_ops, },
1293 { .compatible = "fsl,mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1294 /* binding used by old lite5200 device trees: */
1295 { .compatible = "mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1296 /* binding used by efika: */
1297 { .compatible = "mpc5200-serial", .data = &mpc52xx_psc_ops, },
1298#endif
1299#ifdef CONFIG_PPC_MPC512x
1300 { .compatible = "fsl,mpc5121-psc-uart", .data = &mpc512x_psc_ops, },
1301#endif
1302 {},
1303};
1304
1305static int __devinit
1306mpc52xx_uart_of_probe(struct platform_device *op, const struct of_device_id *match)
1307{
1308 int idx = -1;
1309 unsigned int uartclk;
1310 struct uart_port *port = NULL;
1311 struct resource res;
1312 int ret;
1313
1314 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
1315
1316 /* Check validity & presence */
1317 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
1318 if (mpc52xx_uart_nodes[idx] == op->dev.of_node)
1319 break;
1320 if (idx >= MPC52xx_PSC_MAXNUM)
1321 return -EINVAL;
1322 pr_debug("Found %s assigned to ttyPSC%x\n",
1323 mpc52xx_uart_nodes[idx]->full_name, idx);
1324
1325 /* set the uart clock to the input clock of the psc, the different
1326 * prescalers are taken into account in the set_baudrate() methods
1327 * of the respective chip */
1328 uartclk = mpc5xxx_get_bus_frequency(op->dev.of_node);
1329 if (uartclk == 0) {
1330 dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
1331 return -EINVAL;
1332 }
1333
1334 /* Init the port structure */
1335 port = &mpc52xx_uart_ports[idx];
1336
1337 spin_lock_init(&port->lock);
1338 port->uartclk = uartclk;
1339 port->fifosize = 512;
1340 port->iotype = UPIO_MEM;
1341 port->flags = UPF_BOOT_AUTOCONF |
1342 (uart_console(port) ? 0 : UPF_IOREMAP);
1343 port->line = idx;
1344 port->ops = &mpc52xx_uart_ops;
1345 port->dev = &op->dev;
1346
1347 /* Search for IRQ and mapbase */
1348 ret = of_address_to_resource(op->dev.of_node, 0, &res);
1349 if (ret)
1350 return ret;
1351
1352 port->mapbase = res.start;
1353 if (!port->mapbase) {
1354 dev_dbg(&op->dev, "Could not allocate resources for PSC\n");
1355 return -EINVAL;
1356 }
1357
1358 psc_ops->get_irq(port, op->dev.of_node);
1359 if (port->irq == NO_IRQ) {
1360 dev_dbg(&op->dev, "Could not get irq\n");
1361 return -EINVAL;
1362 }
1363
1364 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
1365 (void *)port->mapbase, port->irq, port->uartclk);
1366
1367 /* Add the port to the uart sub-system */
1368 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
1369 if (ret)
1370 return ret;
1371
1372 dev_set_drvdata(&op->dev, (void *)port);
1373 return 0;
1374}
1375
1376static int
1377mpc52xx_uart_of_remove(struct platform_device *op)
1378{
1379 struct uart_port *port = dev_get_drvdata(&op->dev);
1380 dev_set_drvdata(&op->dev, NULL);
1381
1382 if (port)
1383 uart_remove_one_port(&mpc52xx_uart_driver, port);
1384
1385 return 0;
1386}
1387
1388#ifdef CONFIG_PM
1389static int
1390mpc52xx_uart_of_suspend(struct platform_device *op, pm_message_t state)
1391{
1392 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1393
1394 if (port)
1395 uart_suspend_port(&mpc52xx_uart_driver, port);
1396
1397 return 0;
1398}
1399
1400static int
1401mpc52xx_uart_of_resume(struct platform_device *op)
1402{
1403 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1404
1405 if (port)
1406 uart_resume_port(&mpc52xx_uart_driver, port);
1407
1408 return 0;
1409}
1410#endif
1411
1412static void
1413mpc52xx_uart_of_assign(struct device_node *np)
1414{
1415 int i;
1416
1417 /* Find the first free PSC number */
1418 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1419 if (mpc52xx_uart_nodes[i] == NULL) {
1420 of_node_get(np);
1421 mpc52xx_uart_nodes[i] = np;
1422 return;
1423 }
1424 }
1425}
1426
1427static void
1428mpc52xx_uart_of_enumerate(void)
1429{
1430 static int enum_done;
1431 struct device_node *np;
1432 const struct of_device_id *match;
1433 int i;
1434
1435 if (enum_done)
1436 return;
1437
1438 /* Assign index to each PSC in device tree */
1439 for_each_matching_node(np, mpc52xx_uart_of_match) {
1440 match = of_match_node(mpc52xx_uart_of_match, np);
1441 psc_ops = match->data;
1442 mpc52xx_uart_of_assign(np);
1443 }
1444
1445 enum_done = 1;
1446
1447 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1448 if (mpc52xx_uart_nodes[i])
1449 pr_debug("%s assigned to ttyPSC%x\n",
1450 mpc52xx_uart_nodes[i]->full_name, i);
1451 }
1452}
1453
1454MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1455
1456static struct of_platform_driver mpc52xx_uart_of_driver = {
1457 .probe = mpc52xx_uart_of_probe,
1458 .remove = mpc52xx_uart_of_remove,
1459#ifdef CONFIG_PM
1460 .suspend = mpc52xx_uart_of_suspend,
1461 .resume = mpc52xx_uart_of_resume,
1462#endif
1463 .driver = {
1464 .name = "mpc52xx-psc-uart",
1465 .owner = THIS_MODULE,
1466 .of_match_table = mpc52xx_uart_of_match,
1467 },
1468};
1469
1470
1471/* ======================================================================== */
1472/* Module */
1473/* ======================================================================== */
1474
1475static int __init
1476mpc52xx_uart_init(void)
1477{
1478 int ret;
1479
1480 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1481
1482 ret = uart_register_driver(&mpc52xx_uart_driver);
1483 if (ret) {
1484 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1485 __FILE__, ret);
1486 return ret;
1487 }
1488
1489 mpc52xx_uart_of_enumerate();
1490
1491 /*
1492 * Map the PSC FIFO Controller and init if on MPC512x.
1493 */
1494 if (psc_ops && psc_ops->fifoc_init) {
1495 ret = psc_ops->fifoc_init();
1496 if (ret)
1497 return ret;
1498 }
1499
1500 ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
1501 if (ret) {
1502 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
1503 __FILE__, ret);
1504 uart_unregister_driver(&mpc52xx_uart_driver);
1505 return ret;
1506 }
1507
1508 return 0;
1509}
1510
1511static void __exit
1512mpc52xx_uart_exit(void)
1513{
1514 if (psc_ops->fifoc_uninit)
1515 psc_ops->fifoc_uninit();
1516
1517 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1518 uart_unregister_driver(&mpc52xx_uart_driver);
1519}
1520
1521
1522module_init(mpc52xx_uart_init);
1523module_exit(mpc52xx_uart_exit);
1524
1525MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1526MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1527MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/mpsc.c b/drivers/tty/serial/mpsc.c
new file mode 100644
index 000000000000..6a9c6605666a
--- /dev/null
+++ b/drivers/tty/serial/mpsc.c
@@ -0,0 +1,2159 @@
1/*
2 * Generic driver for the MPSC (UART mode) on Marvell parts (e.g., GT64240,
3 * GT64260, MV64340, MV64360, GT96100, ... ).
4 *
5 * Author: Mark A. Greer <mgreer@mvista.com>
6 *
7 * Based on an old MPSC driver that was in the linuxppc tree. It appears to
8 * have been created by Chris Zankel (formerly of MontaVista) but there
9 * is no proper Copyright so I'm not sure. Apparently, parts were also
10 * taken from PPCBoot (now U-Boot). Also based on drivers/serial/8250.c
11 * by Russell King.
12 *
13 * 2004 (c) MontaVista, Software, Inc. This file is licensed under
14 * the terms of the GNU General Public License version 2. This program
15 * is licensed "as is" without any warranty of any kind, whether express
16 * or implied.
17 */
18/*
19 * The MPSC interface is much like a typical network controller's interface.
20 * That is, you set up separate rings of descriptors for transmitting and
21 * receiving data. There is also a pool of buffers with (one buffer per
22 * descriptor) that incoming data are dma'd into or outgoing data are dma'd
23 * out of.
24 *
25 * The MPSC requires two other controllers to be able to work. The Baud Rate
26 * Generator (BRG) provides a clock at programmable frequencies which determines
27 * the baud rate. The Serial DMA Controller (SDMA) takes incoming data from the
28 * MPSC and DMA's it into memory or DMA's outgoing data and passes it to the
29 * MPSC. It is actually the SDMA interrupt that the driver uses to keep the
30 * transmit and receive "engines" going (i.e., indicate data has been
31 * transmitted or received).
32 *
33 * NOTES:
34 *
35 * 1) Some chips have an erratum where several regs cannot be
36 * read. To work around that, we keep a local copy of those regs in
37 * 'mpsc_port_info'.
38 *
39 * 2) Some chips have an erratum where the ctlr will hang when the SDMA ctlr
40 * accesses system mem with coherency enabled. For that reason, the driver
41 * assumes that coherency for that ctlr has been disabled. This means
42 * that when in a cache coherent system, the driver has to manually manage
43 * the data cache on the areas that it touches because the dma_* macro are
44 * basically no-ops.
45 *
46 * 3) There is an erratum (on PPC) where you can't use the instruction to do
47 * a DMA_TO_DEVICE/cache clean so DMA_BIDIRECTIONAL/flushes are used in places
48 * where a DMA_TO_DEVICE/clean would have [otherwise] sufficed.
49 *
50 * 4) AFAICT, hardware flow control isn't supported by the controller --MAG.
51 */
52
53
54#if defined(CONFIG_SERIAL_MPSC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
55#define SUPPORT_SYSRQ
56#endif
57
58#include <linux/module.h>
59#include <linux/moduleparam.h>
60#include <linux/tty.h>
61#include <linux/tty_flip.h>
62#include <linux/ioport.h>
63#include <linux/init.h>
64#include <linux/console.h>
65#include <linux/sysrq.h>
66#include <linux/serial.h>
67#include <linux/serial_core.h>
68#include <linux/delay.h>
69#include <linux/device.h>
70#include <linux/dma-mapping.h>
71#include <linux/mv643xx.h>
72#include <linux/platform_device.h>
73#include <linux/gfp.h>
74
75#include <asm/io.h>
76#include <asm/irq.h>
77
78#define MPSC_NUM_CTLRS 2
79
80/*
81 * Descriptors and buffers must be cache line aligned.
82 * Buffers lengths must be multiple of cache line size.
83 * Number of Tx & Rx descriptors must be powers of 2.
84 */
85#define MPSC_RXR_ENTRIES 32
86#define MPSC_RXRE_SIZE dma_get_cache_alignment()
87#define MPSC_RXR_SIZE (MPSC_RXR_ENTRIES * MPSC_RXRE_SIZE)
88#define MPSC_RXBE_SIZE dma_get_cache_alignment()
89#define MPSC_RXB_SIZE (MPSC_RXR_ENTRIES * MPSC_RXBE_SIZE)
90
91#define MPSC_TXR_ENTRIES 32
92#define MPSC_TXRE_SIZE dma_get_cache_alignment()
93#define MPSC_TXR_SIZE (MPSC_TXR_ENTRIES * MPSC_TXRE_SIZE)
94#define MPSC_TXBE_SIZE dma_get_cache_alignment()
95#define MPSC_TXB_SIZE (MPSC_TXR_ENTRIES * MPSC_TXBE_SIZE)
96
97#define MPSC_DMA_ALLOC_SIZE (MPSC_RXR_SIZE + MPSC_RXB_SIZE + MPSC_TXR_SIZE \
98 + MPSC_TXB_SIZE + dma_get_cache_alignment() /* for alignment */)
99
100/* Rx and Tx Ring entry descriptors -- assume entry size is <= cacheline size */
101struct mpsc_rx_desc {
102 u16 bufsize;
103 u16 bytecnt;
104 u32 cmdstat;
105 u32 link;
106 u32 buf_ptr;
107} __attribute((packed));
108
109struct mpsc_tx_desc {
110 u16 bytecnt;
111 u16 shadow;
112 u32 cmdstat;
113 u32 link;
114 u32 buf_ptr;
115} __attribute((packed));
116
117/*
118 * Some regs that have the erratum that you can't read them are are shared
119 * between the two MPSC controllers. This struct contains those shared regs.
120 */
121struct mpsc_shared_regs {
122 phys_addr_t mpsc_routing_base_p;
123 phys_addr_t sdma_intr_base_p;
124
125 void __iomem *mpsc_routing_base;
126 void __iomem *sdma_intr_base;
127
128 u32 MPSC_MRR_m;
129 u32 MPSC_RCRR_m;
130 u32 MPSC_TCRR_m;
131 u32 SDMA_INTR_CAUSE_m;
132 u32 SDMA_INTR_MASK_m;
133};
134
135/* The main driver data structure */
136struct mpsc_port_info {
137 struct uart_port port; /* Overlay uart_port structure */
138
139 /* Internal driver state for this ctlr */
140 u8 ready;
141 u8 rcv_data;
142 tcflag_t c_iflag; /* save termios->c_iflag */
143 tcflag_t c_cflag; /* save termios->c_cflag */
144
145 /* Info passed in from platform */
146 u8 mirror_regs; /* Need to mirror regs? */
147 u8 cache_mgmt; /* Need manual cache mgmt? */
148 u8 brg_can_tune; /* BRG has baud tuning? */
149 u32 brg_clk_src;
150 u16 mpsc_max_idle;
151 int default_baud;
152 int default_bits;
153 int default_parity;
154 int default_flow;
155
156 /* Physical addresses of various blocks of registers (from platform) */
157 phys_addr_t mpsc_base_p;
158 phys_addr_t sdma_base_p;
159 phys_addr_t brg_base_p;
160
161 /* Virtual addresses of various blocks of registers (from platform) */
162 void __iomem *mpsc_base;
163 void __iomem *sdma_base;
164 void __iomem *brg_base;
165
166 /* Descriptor ring and buffer allocations */
167 void *dma_region;
168 dma_addr_t dma_region_p;
169
170 dma_addr_t rxr; /* Rx descriptor ring */
171 dma_addr_t rxr_p; /* Phys addr of rxr */
172 u8 *rxb; /* Rx Ring I/O buf */
173 u8 *rxb_p; /* Phys addr of rxb */
174 u32 rxr_posn; /* First desc w/ Rx data */
175
176 dma_addr_t txr; /* Tx descriptor ring */
177 dma_addr_t txr_p; /* Phys addr of txr */
178 u8 *txb; /* Tx Ring I/O buf */
179 u8 *txb_p; /* Phys addr of txb */
180 int txr_head; /* Where new data goes */
181 int txr_tail; /* Where sent data comes off */
182 spinlock_t tx_lock; /* transmit lock */
183
184 /* Mirrored values of regs we can't read (if 'mirror_regs' set) */
185 u32 MPSC_MPCR_m;
186 u32 MPSC_CHR_1_m;
187 u32 MPSC_CHR_2_m;
188 u32 MPSC_CHR_10_m;
189 u32 BRG_BCR_m;
190 struct mpsc_shared_regs *shared_regs;
191};
192
193/* Hooks to platform-specific code */
194int mpsc_platform_register_driver(void);
195void mpsc_platform_unregister_driver(void);
196
197/* Hooks back in to mpsc common to be called by platform-specific code */
198struct mpsc_port_info *mpsc_device_probe(int index);
199struct mpsc_port_info *mpsc_device_remove(int index);
200
201/* Main MPSC Configuration Register Offsets */
202#define MPSC_MMCRL 0x0000
203#define MPSC_MMCRH 0x0004
204#define MPSC_MPCR 0x0008
205#define MPSC_CHR_1 0x000c
206#define MPSC_CHR_2 0x0010
207#define MPSC_CHR_3 0x0014
208#define MPSC_CHR_4 0x0018
209#define MPSC_CHR_5 0x001c
210#define MPSC_CHR_6 0x0020
211#define MPSC_CHR_7 0x0024
212#define MPSC_CHR_8 0x0028
213#define MPSC_CHR_9 0x002c
214#define MPSC_CHR_10 0x0030
215#define MPSC_CHR_11 0x0034
216
217#define MPSC_MPCR_FRZ (1 << 9)
218#define MPSC_MPCR_CL_5 0
219#define MPSC_MPCR_CL_6 1
220#define MPSC_MPCR_CL_7 2
221#define MPSC_MPCR_CL_8 3
222#define MPSC_MPCR_SBL_1 0
223#define MPSC_MPCR_SBL_2 1
224
225#define MPSC_CHR_2_TEV (1<<1)
226#define MPSC_CHR_2_TA (1<<7)
227#define MPSC_CHR_2_TTCS (1<<9)
228#define MPSC_CHR_2_REV (1<<17)
229#define MPSC_CHR_2_RA (1<<23)
230#define MPSC_CHR_2_CRD (1<<25)
231#define MPSC_CHR_2_EH (1<<31)
232#define MPSC_CHR_2_PAR_ODD 0
233#define MPSC_CHR_2_PAR_SPACE 1
234#define MPSC_CHR_2_PAR_EVEN 2
235#define MPSC_CHR_2_PAR_MARK 3
236
237/* MPSC Signal Routing */
238#define MPSC_MRR 0x0000
239#define MPSC_RCRR 0x0004
240#define MPSC_TCRR 0x0008
241
242/* Serial DMA Controller Interface Registers */
243#define SDMA_SDC 0x0000
244#define SDMA_SDCM 0x0008
245#define SDMA_RX_DESC 0x0800
246#define SDMA_RX_BUF_PTR 0x0808
247#define SDMA_SCRDP 0x0810
248#define SDMA_TX_DESC 0x0c00
249#define SDMA_SCTDP 0x0c10
250#define SDMA_SFTDP 0x0c14
251
252#define SDMA_DESC_CMDSTAT_PE (1<<0)
253#define SDMA_DESC_CMDSTAT_CDL (1<<1)
254#define SDMA_DESC_CMDSTAT_FR (1<<3)
255#define SDMA_DESC_CMDSTAT_OR (1<<6)
256#define SDMA_DESC_CMDSTAT_BR (1<<9)
257#define SDMA_DESC_CMDSTAT_MI (1<<10)
258#define SDMA_DESC_CMDSTAT_A (1<<11)
259#define SDMA_DESC_CMDSTAT_AM (1<<12)
260#define SDMA_DESC_CMDSTAT_CT (1<<13)
261#define SDMA_DESC_CMDSTAT_C (1<<14)
262#define SDMA_DESC_CMDSTAT_ES (1<<15)
263#define SDMA_DESC_CMDSTAT_L (1<<16)
264#define SDMA_DESC_CMDSTAT_F (1<<17)
265#define SDMA_DESC_CMDSTAT_P (1<<18)
266#define SDMA_DESC_CMDSTAT_EI (1<<23)
267#define SDMA_DESC_CMDSTAT_O (1<<31)
268
269#define SDMA_DESC_DFLT (SDMA_DESC_CMDSTAT_O \
270 | SDMA_DESC_CMDSTAT_EI)
271
272#define SDMA_SDC_RFT (1<<0)
273#define SDMA_SDC_SFM (1<<1)
274#define SDMA_SDC_BLMR (1<<6)
275#define SDMA_SDC_BLMT (1<<7)
276#define SDMA_SDC_POVR (1<<8)
277#define SDMA_SDC_RIFB (1<<9)
278
279#define SDMA_SDCM_ERD (1<<7)
280#define SDMA_SDCM_AR (1<<15)
281#define SDMA_SDCM_STD (1<<16)
282#define SDMA_SDCM_TXD (1<<23)
283#define SDMA_SDCM_AT (1<<31)
284
285#define SDMA_0_CAUSE_RXBUF (1<<0)
286#define SDMA_0_CAUSE_RXERR (1<<1)
287#define SDMA_0_CAUSE_TXBUF (1<<2)
288#define SDMA_0_CAUSE_TXEND (1<<3)
289#define SDMA_1_CAUSE_RXBUF (1<<8)
290#define SDMA_1_CAUSE_RXERR (1<<9)
291#define SDMA_1_CAUSE_TXBUF (1<<10)
292#define SDMA_1_CAUSE_TXEND (1<<11)
293
294#define SDMA_CAUSE_RX_MASK (SDMA_0_CAUSE_RXBUF | SDMA_0_CAUSE_RXERR \
295 | SDMA_1_CAUSE_RXBUF | SDMA_1_CAUSE_RXERR)
296#define SDMA_CAUSE_TX_MASK (SDMA_0_CAUSE_TXBUF | SDMA_0_CAUSE_TXEND \
297 | SDMA_1_CAUSE_TXBUF | SDMA_1_CAUSE_TXEND)
298
299/* SDMA Interrupt registers */
300#define SDMA_INTR_CAUSE 0x0000
301#define SDMA_INTR_MASK 0x0080
302
303/* Baud Rate Generator Interface Registers */
304#define BRG_BCR 0x0000
305#define BRG_BTR 0x0004
306
307/*
308 * Define how this driver is known to the outside (we've been assigned a
309 * range on the "Low-density serial ports" major).
310 */
311#define MPSC_MAJOR 204
312#define MPSC_MINOR_START 44
313#define MPSC_DRIVER_NAME "MPSC"
314#define MPSC_DEV_NAME "ttyMM"
315#define MPSC_VERSION "1.00"
316
317static struct mpsc_port_info mpsc_ports[MPSC_NUM_CTLRS];
318static struct mpsc_shared_regs mpsc_shared_regs;
319static struct uart_driver mpsc_reg;
320
321static void mpsc_start_rx(struct mpsc_port_info *pi);
322static void mpsc_free_ring_mem(struct mpsc_port_info *pi);
323static void mpsc_release_port(struct uart_port *port);
324/*
325 ******************************************************************************
326 *
327 * Baud Rate Generator Routines (BRG)
328 *
329 ******************************************************************************
330 */
331static void mpsc_brg_init(struct mpsc_port_info *pi, u32 clk_src)
332{
333 u32 v;
334
335 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
336 v = (v & ~(0xf << 18)) | ((clk_src & 0xf) << 18);
337
338 if (pi->brg_can_tune)
339 v &= ~(1 << 25);
340
341 if (pi->mirror_regs)
342 pi->BRG_BCR_m = v;
343 writel(v, pi->brg_base + BRG_BCR);
344
345 writel(readl(pi->brg_base + BRG_BTR) & 0xffff0000,
346 pi->brg_base + BRG_BTR);
347}
348
349static void mpsc_brg_enable(struct mpsc_port_info *pi)
350{
351 u32 v;
352
353 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
354 v |= (1 << 16);
355
356 if (pi->mirror_regs)
357 pi->BRG_BCR_m = v;
358 writel(v, pi->brg_base + BRG_BCR);
359}
360
361static void mpsc_brg_disable(struct mpsc_port_info *pi)
362{
363 u32 v;
364
365 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
366 v &= ~(1 << 16);
367
368 if (pi->mirror_regs)
369 pi->BRG_BCR_m = v;
370 writel(v, pi->brg_base + BRG_BCR);
371}
372
373/*
374 * To set the baud, we adjust the CDV field in the BRG_BCR reg.
375 * From manual: Baud = clk / ((CDV+1)*2) ==> CDV = (clk / (baud*2)) - 1.
376 * However, the input clock is divided by 16 in the MPSC b/c of how
377 * 'MPSC_MMCRH' was set up so we have to divide the 'clk' used in our
378 * calculation by 16 to account for that. So the real calculation
379 * that accounts for the way the mpsc is set up is:
380 * CDV = (clk / (baud*2*16)) - 1 ==> CDV = (clk / (baud << 5)) - 1.
381 */
382static void mpsc_set_baudrate(struct mpsc_port_info *pi, u32 baud)
383{
384 u32 cdv = (pi->port.uartclk / (baud << 5)) - 1;
385 u32 v;
386
387 mpsc_brg_disable(pi);
388 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
389 v = (v & 0xffff0000) | (cdv & 0xffff);
390
391 if (pi->mirror_regs)
392 pi->BRG_BCR_m = v;
393 writel(v, pi->brg_base + BRG_BCR);
394 mpsc_brg_enable(pi);
395}
396
397/*
398 ******************************************************************************
399 *
400 * Serial DMA Routines (SDMA)
401 *
402 ******************************************************************************
403 */
404
405static void mpsc_sdma_burstsize(struct mpsc_port_info *pi, u32 burst_size)
406{
407 u32 v;
408
409 pr_debug("mpsc_sdma_burstsize[%d]: burst_size: %d\n",
410 pi->port.line, burst_size);
411
412 burst_size >>= 3; /* Divide by 8 b/c reg values are 8-byte chunks */
413
414 if (burst_size < 2)
415 v = 0x0; /* 1 64-bit word */
416 else if (burst_size < 4)
417 v = 0x1; /* 2 64-bit words */
418 else if (burst_size < 8)
419 v = 0x2; /* 4 64-bit words */
420 else
421 v = 0x3; /* 8 64-bit words */
422
423 writel((readl(pi->sdma_base + SDMA_SDC) & (0x3 << 12)) | (v << 12),
424 pi->sdma_base + SDMA_SDC);
425}
426
427static void mpsc_sdma_init(struct mpsc_port_info *pi, u32 burst_size)
428{
429 pr_debug("mpsc_sdma_init[%d]: burst_size: %d\n", pi->port.line,
430 burst_size);
431
432 writel((readl(pi->sdma_base + SDMA_SDC) & 0x3ff) | 0x03f,
433 pi->sdma_base + SDMA_SDC);
434 mpsc_sdma_burstsize(pi, burst_size);
435}
436
437static u32 mpsc_sdma_intr_mask(struct mpsc_port_info *pi, u32 mask)
438{
439 u32 old, v;
440
441 pr_debug("mpsc_sdma_intr_mask[%d]: mask: 0x%x\n", pi->port.line, mask);
442
443 old = v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
444 readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
445
446 mask &= 0xf;
447 if (pi->port.line)
448 mask <<= 8;
449 v &= ~mask;
450
451 if (pi->mirror_regs)
452 pi->shared_regs->SDMA_INTR_MASK_m = v;
453 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
454
455 if (pi->port.line)
456 old >>= 8;
457 return old & 0xf;
458}
459
460static void mpsc_sdma_intr_unmask(struct mpsc_port_info *pi, u32 mask)
461{
462 u32 v;
463
464 pr_debug("mpsc_sdma_intr_unmask[%d]: mask: 0x%x\n", pi->port.line,mask);
465
466 v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m
467 : readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
468
469 mask &= 0xf;
470 if (pi->port.line)
471 mask <<= 8;
472 v |= mask;
473
474 if (pi->mirror_regs)
475 pi->shared_regs->SDMA_INTR_MASK_m = v;
476 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
477}
478
479static void mpsc_sdma_intr_ack(struct mpsc_port_info *pi)
480{
481 pr_debug("mpsc_sdma_intr_ack[%d]: Acknowledging IRQ\n", pi->port.line);
482
483 if (pi->mirror_regs)
484 pi->shared_regs->SDMA_INTR_CAUSE_m = 0;
485 writeb(0x00, pi->shared_regs->sdma_intr_base + SDMA_INTR_CAUSE
486 + pi->port.line);
487}
488
489static void mpsc_sdma_set_rx_ring(struct mpsc_port_info *pi,
490 struct mpsc_rx_desc *rxre_p)
491{
492 pr_debug("mpsc_sdma_set_rx_ring[%d]: rxre_p: 0x%x\n",
493 pi->port.line, (u32)rxre_p);
494
495 writel((u32)rxre_p, pi->sdma_base + SDMA_SCRDP);
496}
497
498static void mpsc_sdma_set_tx_ring(struct mpsc_port_info *pi,
499 struct mpsc_tx_desc *txre_p)
500{
501 writel((u32)txre_p, pi->sdma_base + SDMA_SFTDP);
502 writel((u32)txre_p, pi->sdma_base + SDMA_SCTDP);
503}
504
505static void mpsc_sdma_cmd(struct mpsc_port_info *pi, u32 val)
506{
507 u32 v;
508
509 v = readl(pi->sdma_base + SDMA_SDCM);
510 if (val)
511 v |= val;
512 else
513 v = 0;
514 wmb();
515 writel(v, pi->sdma_base + SDMA_SDCM);
516 wmb();
517}
518
519static uint mpsc_sdma_tx_active(struct mpsc_port_info *pi)
520{
521 return readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_TXD;
522}
523
524static void mpsc_sdma_start_tx(struct mpsc_port_info *pi)
525{
526 struct mpsc_tx_desc *txre, *txre_p;
527
528 /* If tx isn't running & there's a desc ready to go, start it */
529 if (!mpsc_sdma_tx_active(pi)) {
530 txre = (struct mpsc_tx_desc *)(pi->txr
531 + (pi->txr_tail * MPSC_TXRE_SIZE));
532 dma_cache_sync(pi->port.dev, (void *)txre, MPSC_TXRE_SIZE,
533 DMA_FROM_DEVICE);
534#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
535 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
536 invalidate_dcache_range((ulong)txre,
537 (ulong)txre + MPSC_TXRE_SIZE);
538#endif
539
540 if (be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O) {
541 txre_p = (struct mpsc_tx_desc *)
542 (pi->txr_p + (pi->txr_tail * MPSC_TXRE_SIZE));
543
544 mpsc_sdma_set_tx_ring(pi, txre_p);
545 mpsc_sdma_cmd(pi, SDMA_SDCM_STD | SDMA_SDCM_TXD);
546 }
547 }
548}
549
550static void mpsc_sdma_stop(struct mpsc_port_info *pi)
551{
552 pr_debug("mpsc_sdma_stop[%d]: Stopping SDMA\n", pi->port.line);
553
554 /* Abort any SDMA transfers */
555 mpsc_sdma_cmd(pi, 0);
556 mpsc_sdma_cmd(pi, SDMA_SDCM_AR | SDMA_SDCM_AT);
557
558 /* Clear the SDMA current and first TX and RX pointers */
559 mpsc_sdma_set_tx_ring(pi, NULL);
560 mpsc_sdma_set_rx_ring(pi, NULL);
561
562 /* Disable interrupts */
563 mpsc_sdma_intr_mask(pi, 0xf);
564 mpsc_sdma_intr_ack(pi);
565}
566
567/*
568 ******************************************************************************
569 *
570 * Multi-Protocol Serial Controller Routines (MPSC)
571 *
572 ******************************************************************************
573 */
574
575static void mpsc_hw_init(struct mpsc_port_info *pi)
576{
577 u32 v;
578
579 pr_debug("mpsc_hw_init[%d]: Initializing hardware\n", pi->port.line);
580
581 /* Set up clock routing */
582 if (pi->mirror_regs) {
583 v = pi->shared_regs->MPSC_MRR_m;
584 v &= ~0x1c7;
585 pi->shared_regs->MPSC_MRR_m = v;
586 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
587
588 v = pi->shared_regs->MPSC_RCRR_m;
589 v = (v & ~0xf0f) | 0x100;
590 pi->shared_regs->MPSC_RCRR_m = v;
591 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
592
593 v = pi->shared_regs->MPSC_TCRR_m;
594 v = (v & ~0xf0f) | 0x100;
595 pi->shared_regs->MPSC_TCRR_m = v;
596 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
597 } else {
598 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_MRR);
599 v &= ~0x1c7;
600 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
601
602 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
603 v = (v & ~0xf0f) | 0x100;
604 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
605
606 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
607 v = (v & ~0xf0f) | 0x100;
608 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
609 }
610
611 /* Put MPSC in UART mode & enabel Tx/Rx egines */
612 writel(0x000004c4, pi->mpsc_base + MPSC_MMCRL);
613
614 /* No preamble, 16x divider, low-latency, */
615 writel(0x04400400, pi->mpsc_base + MPSC_MMCRH);
616 mpsc_set_baudrate(pi, pi->default_baud);
617
618 if (pi->mirror_regs) {
619 pi->MPSC_CHR_1_m = 0;
620 pi->MPSC_CHR_2_m = 0;
621 }
622 writel(0, pi->mpsc_base + MPSC_CHR_1);
623 writel(0, pi->mpsc_base + MPSC_CHR_2);
624 writel(pi->mpsc_max_idle, pi->mpsc_base + MPSC_CHR_3);
625 writel(0, pi->mpsc_base + MPSC_CHR_4);
626 writel(0, pi->mpsc_base + MPSC_CHR_5);
627 writel(0, pi->mpsc_base + MPSC_CHR_6);
628 writel(0, pi->mpsc_base + MPSC_CHR_7);
629 writel(0, pi->mpsc_base + MPSC_CHR_8);
630 writel(0, pi->mpsc_base + MPSC_CHR_9);
631 writel(0, pi->mpsc_base + MPSC_CHR_10);
632}
633
634static void mpsc_enter_hunt(struct mpsc_port_info *pi)
635{
636 pr_debug("mpsc_enter_hunt[%d]: Hunting...\n", pi->port.line);
637
638 if (pi->mirror_regs) {
639 writel(pi->MPSC_CHR_2_m | MPSC_CHR_2_EH,
640 pi->mpsc_base + MPSC_CHR_2);
641 /* Erratum prevents reading CHR_2 so just delay for a while */
642 udelay(100);
643 } else {
644 writel(readl(pi->mpsc_base + MPSC_CHR_2) | MPSC_CHR_2_EH,
645 pi->mpsc_base + MPSC_CHR_2);
646
647 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_EH)
648 udelay(10);
649 }
650}
651
652static void mpsc_freeze(struct mpsc_port_info *pi)
653{
654 u32 v;
655
656 pr_debug("mpsc_freeze[%d]: Freezing\n", pi->port.line);
657
658 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
659 readl(pi->mpsc_base + MPSC_MPCR);
660 v |= MPSC_MPCR_FRZ;
661
662 if (pi->mirror_regs)
663 pi->MPSC_MPCR_m = v;
664 writel(v, pi->mpsc_base + MPSC_MPCR);
665}
666
667static void mpsc_unfreeze(struct mpsc_port_info *pi)
668{
669 u32 v;
670
671 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
672 readl(pi->mpsc_base + MPSC_MPCR);
673 v &= ~MPSC_MPCR_FRZ;
674
675 if (pi->mirror_regs)
676 pi->MPSC_MPCR_m = v;
677 writel(v, pi->mpsc_base + MPSC_MPCR);
678
679 pr_debug("mpsc_unfreeze[%d]: Unfrozen\n", pi->port.line);
680}
681
682static void mpsc_set_char_length(struct mpsc_port_info *pi, u32 len)
683{
684 u32 v;
685
686 pr_debug("mpsc_set_char_length[%d]: char len: %d\n", pi->port.line,len);
687
688 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
689 readl(pi->mpsc_base + MPSC_MPCR);
690 v = (v & ~(0x3 << 12)) | ((len & 0x3) << 12);
691
692 if (pi->mirror_regs)
693 pi->MPSC_MPCR_m = v;
694 writel(v, pi->mpsc_base + MPSC_MPCR);
695}
696
697static void mpsc_set_stop_bit_length(struct mpsc_port_info *pi, u32 len)
698{
699 u32 v;
700
701 pr_debug("mpsc_set_stop_bit_length[%d]: stop bits: %d\n",
702 pi->port.line, len);
703
704 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
705 readl(pi->mpsc_base + MPSC_MPCR);
706
707 v = (v & ~(1 << 14)) | ((len & 0x1) << 14);
708
709 if (pi->mirror_regs)
710 pi->MPSC_MPCR_m = v;
711 writel(v, pi->mpsc_base + MPSC_MPCR);
712}
713
714static void mpsc_set_parity(struct mpsc_port_info *pi, u32 p)
715{
716 u32 v;
717
718 pr_debug("mpsc_set_parity[%d]: parity bits: 0x%x\n", pi->port.line, p);
719
720 v = (pi->mirror_regs) ? pi->MPSC_CHR_2_m :
721 readl(pi->mpsc_base + MPSC_CHR_2);
722
723 p &= 0x3;
724 v = (v & ~0xc000c) | (p << 18) | (p << 2);
725
726 if (pi->mirror_regs)
727 pi->MPSC_CHR_2_m = v;
728 writel(v, pi->mpsc_base + MPSC_CHR_2);
729}
730
731/*
732 ******************************************************************************
733 *
734 * Driver Init Routines
735 *
736 ******************************************************************************
737 */
738
739static void mpsc_init_hw(struct mpsc_port_info *pi)
740{
741 pr_debug("mpsc_init_hw[%d]: Initializing\n", pi->port.line);
742
743 mpsc_brg_init(pi, pi->brg_clk_src);
744 mpsc_brg_enable(pi);
745 mpsc_sdma_init(pi, dma_get_cache_alignment()); /* burst a cacheline */
746 mpsc_sdma_stop(pi);
747 mpsc_hw_init(pi);
748}
749
750static int mpsc_alloc_ring_mem(struct mpsc_port_info *pi)
751{
752 int rc = 0;
753
754 pr_debug("mpsc_alloc_ring_mem[%d]: Allocating ring mem\n",
755 pi->port.line);
756
757 if (!pi->dma_region) {
758 if (!dma_supported(pi->port.dev, 0xffffffff)) {
759 printk(KERN_ERR "MPSC: Inadequate DMA support\n");
760 rc = -ENXIO;
761 } else if ((pi->dma_region = dma_alloc_noncoherent(pi->port.dev,
762 MPSC_DMA_ALLOC_SIZE,
763 &pi->dma_region_p, GFP_KERNEL))
764 == NULL) {
765 printk(KERN_ERR "MPSC: Can't alloc Desc region\n");
766 rc = -ENOMEM;
767 }
768 }
769
770 return rc;
771}
772
773static void mpsc_free_ring_mem(struct mpsc_port_info *pi)
774{
775 pr_debug("mpsc_free_ring_mem[%d]: Freeing ring mem\n", pi->port.line);
776
777 if (pi->dma_region) {
778 dma_free_noncoherent(pi->port.dev, MPSC_DMA_ALLOC_SIZE,
779 pi->dma_region, pi->dma_region_p);
780 pi->dma_region = NULL;
781 pi->dma_region_p = (dma_addr_t)NULL;
782 }
783}
784
785static void mpsc_init_rings(struct mpsc_port_info *pi)
786{
787 struct mpsc_rx_desc *rxre;
788 struct mpsc_tx_desc *txre;
789 dma_addr_t dp, dp_p;
790 u8 *bp, *bp_p;
791 int i;
792
793 pr_debug("mpsc_init_rings[%d]: Initializing rings\n", pi->port.line);
794
795 BUG_ON(pi->dma_region == NULL);
796
797 memset(pi->dma_region, 0, MPSC_DMA_ALLOC_SIZE);
798
799 /*
800 * Descriptors & buffers are multiples of cacheline size and must be
801 * cacheline aligned.
802 */
803 dp = ALIGN((u32)pi->dma_region, dma_get_cache_alignment());
804 dp_p = ALIGN((u32)pi->dma_region_p, dma_get_cache_alignment());
805
806 /*
807 * Partition dma region into rx ring descriptor, rx buffers,
808 * tx ring descriptors, and tx buffers.
809 */
810 pi->rxr = dp;
811 pi->rxr_p = dp_p;
812 dp += MPSC_RXR_SIZE;
813 dp_p += MPSC_RXR_SIZE;
814
815 pi->rxb = (u8 *)dp;
816 pi->rxb_p = (u8 *)dp_p;
817 dp += MPSC_RXB_SIZE;
818 dp_p += MPSC_RXB_SIZE;
819
820 pi->rxr_posn = 0;
821
822 pi->txr = dp;
823 pi->txr_p = dp_p;
824 dp += MPSC_TXR_SIZE;
825 dp_p += MPSC_TXR_SIZE;
826
827 pi->txb = (u8 *)dp;
828 pi->txb_p = (u8 *)dp_p;
829
830 pi->txr_head = 0;
831 pi->txr_tail = 0;
832
833 /* Init rx ring descriptors */
834 dp = pi->rxr;
835 dp_p = pi->rxr_p;
836 bp = pi->rxb;
837 bp_p = pi->rxb_p;
838
839 for (i = 0; i < MPSC_RXR_ENTRIES; i++) {
840 rxre = (struct mpsc_rx_desc *)dp;
841
842 rxre->bufsize = cpu_to_be16(MPSC_RXBE_SIZE);
843 rxre->bytecnt = cpu_to_be16(0);
844 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O
845 | SDMA_DESC_CMDSTAT_EI | SDMA_DESC_CMDSTAT_F
846 | SDMA_DESC_CMDSTAT_L);
847 rxre->link = cpu_to_be32(dp_p + MPSC_RXRE_SIZE);
848 rxre->buf_ptr = cpu_to_be32(bp_p);
849
850 dp += MPSC_RXRE_SIZE;
851 dp_p += MPSC_RXRE_SIZE;
852 bp += MPSC_RXBE_SIZE;
853 bp_p += MPSC_RXBE_SIZE;
854 }
855 rxre->link = cpu_to_be32(pi->rxr_p); /* Wrap last back to first */
856
857 /* Init tx ring descriptors */
858 dp = pi->txr;
859 dp_p = pi->txr_p;
860 bp = pi->txb;
861 bp_p = pi->txb_p;
862
863 for (i = 0; i < MPSC_TXR_ENTRIES; i++) {
864 txre = (struct mpsc_tx_desc *)dp;
865
866 txre->link = cpu_to_be32(dp_p + MPSC_TXRE_SIZE);
867 txre->buf_ptr = cpu_to_be32(bp_p);
868
869 dp += MPSC_TXRE_SIZE;
870 dp_p += MPSC_TXRE_SIZE;
871 bp += MPSC_TXBE_SIZE;
872 bp_p += MPSC_TXBE_SIZE;
873 }
874 txre->link = cpu_to_be32(pi->txr_p); /* Wrap last back to first */
875
876 dma_cache_sync(pi->port.dev, (void *)pi->dma_region,
877 MPSC_DMA_ALLOC_SIZE, DMA_BIDIRECTIONAL);
878#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
879 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
880 flush_dcache_range((ulong)pi->dma_region,
881 (ulong)pi->dma_region
882 + MPSC_DMA_ALLOC_SIZE);
883#endif
884
885 return;
886}
887
888static void mpsc_uninit_rings(struct mpsc_port_info *pi)
889{
890 pr_debug("mpsc_uninit_rings[%d]: Uninitializing rings\n",pi->port.line);
891
892 BUG_ON(pi->dma_region == NULL);
893
894 pi->rxr = 0;
895 pi->rxr_p = 0;
896 pi->rxb = NULL;
897 pi->rxb_p = NULL;
898 pi->rxr_posn = 0;
899
900 pi->txr = 0;
901 pi->txr_p = 0;
902 pi->txb = NULL;
903 pi->txb_p = NULL;
904 pi->txr_head = 0;
905 pi->txr_tail = 0;
906}
907
908static int mpsc_make_ready(struct mpsc_port_info *pi)
909{
910 int rc;
911
912 pr_debug("mpsc_make_ready[%d]: Making cltr ready\n", pi->port.line);
913
914 if (!pi->ready) {
915 mpsc_init_hw(pi);
916 if ((rc = mpsc_alloc_ring_mem(pi)))
917 return rc;
918 mpsc_init_rings(pi);
919 pi->ready = 1;
920 }
921
922 return 0;
923}
924
925#ifdef CONFIG_CONSOLE_POLL
926static int serial_polled;
927#endif
928
929/*
930 ******************************************************************************
931 *
932 * Interrupt Handling Routines
933 *
934 ******************************************************************************
935 */
936
937static int mpsc_rx_intr(struct mpsc_port_info *pi)
938{
939 struct mpsc_rx_desc *rxre;
940 struct tty_struct *tty = pi->port.state->port.tty;
941 u32 cmdstat, bytes_in, i;
942 int rc = 0;
943 u8 *bp;
944 char flag = TTY_NORMAL;
945
946 pr_debug("mpsc_rx_intr[%d]: Handling Rx intr\n", pi->port.line);
947
948 rxre = (struct mpsc_rx_desc *)(pi->rxr + (pi->rxr_posn*MPSC_RXRE_SIZE));
949
950 dma_cache_sync(pi->port.dev, (void *)rxre, MPSC_RXRE_SIZE,
951 DMA_FROM_DEVICE);
952#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
953 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
954 invalidate_dcache_range((ulong)rxre,
955 (ulong)rxre + MPSC_RXRE_SIZE);
956#endif
957
958 /*
959 * Loop through Rx descriptors handling ones that have been completed.
960 */
961 while (!((cmdstat = be32_to_cpu(rxre->cmdstat))
962 & SDMA_DESC_CMDSTAT_O)) {
963 bytes_in = be16_to_cpu(rxre->bytecnt);
964#ifdef CONFIG_CONSOLE_POLL
965 if (unlikely(serial_polled)) {
966 serial_polled = 0;
967 return 0;
968 }
969#endif
970 /* Following use of tty struct directly is deprecated */
971 if (unlikely(tty_buffer_request_room(tty, bytes_in)
972 < bytes_in)) {
973 if (tty->low_latency)
974 tty_flip_buffer_push(tty);
975 /*
976 * If this failed then we will throw away the bytes
977 * but must do so to clear interrupts.
978 */
979 }
980
981 bp = pi->rxb + (pi->rxr_posn * MPSC_RXBE_SIZE);
982 dma_cache_sync(pi->port.dev, (void *)bp, MPSC_RXBE_SIZE,
983 DMA_FROM_DEVICE);
984#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
985 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
986 invalidate_dcache_range((ulong)bp,
987 (ulong)bp + MPSC_RXBE_SIZE);
988#endif
989
990 /*
991 * Other than for parity error, the manual provides little
992 * info on what data will be in a frame flagged by any of
993 * these errors. For parity error, it is the last byte in
994 * the buffer that had the error. As for the rest, I guess
995 * we'll assume there is no data in the buffer.
996 * If there is...it gets lost.
997 */
998 if (unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR
999 | SDMA_DESC_CMDSTAT_FR
1000 | SDMA_DESC_CMDSTAT_OR))) {
1001
1002 pi->port.icount.rx++;
1003
1004 if (cmdstat & SDMA_DESC_CMDSTAT_BR) { /* Break */
1005 pi->port.icount.brk++;
1006
1007 if (uart_handle_break(&pi->port))
1008 goto next_frame;
1009 } else if (cmdstat & SDMA_DESC_CMDSTAT_FR) {
1010 pi->port.icount.frame++;
1011 } else if (cmdstat & SDMA_DESC_CMDSTAT_OR) {
1012 pi->port.icount.overrun++;
1013 }
1014
1015 cmdstat &= pi->port.read_status_mask;
1016
1017 if (cmdstat & SDMA_DESC_CMDSTAT_BR)
1018 flag = TTY_BREAK;
1019 else if (cmdstat & SDMA_DESC_CMDSTAT_FR)
1020 flag = TTY_FRAME;
1021 else if (cmdstat & SDMA_DESC_CMDSTAT_OR)
1022 flag = TTY_OVERRUN;
1023 else if (cmdstat & SDMA_DESC_CMDSTAT_PE)
1024 flag = TTY_PARITY;
1025 }
1026
1027 if (uart_handle_sysrq_char(&pi->port, *bp)) {
1028 bp++;
1029 bytes_in--;
1030#ifdef CONFIG_CONSOLE_POLL
1031 if (unlikely(serial_polled)) {
1032 serial_polled = 0;
1033 return 0;
1034 }
1035#endif
1036 goto next_frame;
1037 }
1038
1039 if ((unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR
1040 | SDMA_DESC_CMDSTAT_FR
1041 | SDMA_DESC_CMDSTAT_OR)))
1042 && !(cmdstat & pi->port.ignore_status_mask)) {
1043 tty_insert_flip_char(tty, *bp, flag);
1044 } else {
1045 for (i=0; i<bytes_in; i++)
1046 tty_insert_flip_char(tty, *bp++, TTY_NORMAL);
1047
1048 pi->port.icount.rx += bytes_in;
1049 }
1050
1051next_frame:
1052 rxre->bytecnt = cpu_to_be16(0);
1053 wmb();
1054 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O
1055 | SDMA_DESC_CMDSTAT_EI | SDMA_DESC_CMDSTAT_F
1056 | SDMA_DESC_CMDSTAT_L);
1057 wmb();
1058 dma_cache_sync(pi->port.dev, (void *)rxre, MPSC_RXRE_SIZE,
1059 DMA_BIDIRECTIONAL);
1060#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1061 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1062 flush_dcache_range((ulong)rxre,
1063 (ulong)rxre + MPSC_RXRE_SIZE);
1064#endif
1065
1066 /* Advance to next descriptor */
1067 pi->rxr_posn = (pi->rxr_posn + 1) & (MPSC_RXR_ENTRIES - 1);
1068 rxre = (struct mpsc_rx_desc *)
1069 (pi->rxr + (pi->rxr_posn * MPSC_RXRE_SIZE));
1070 dma_cache_sync(pi->port.dev, (void *)rxre, MPSC_RXRE_SIZE,
1071 DMA_FROM_DEVICE);
1072#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1073 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1074 invalidate_dcache_range((ulong)rxre,
1075 (ulong)rxre + MPSC_RXRE_SIZE);
1076#endif
1077 rc = 1;
1078 }
1079
1080 /* Restart rx engine, if its stopped */
1081 if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0)
1082 mpsc_start_rx(pi);
1083
1084 tty_flip_buffer_push(tty);
1085 return rc;
1086}
1087
1088static void mpsc_setup_tx_desc(struct mpsc_port_info *pi, u32 count, u32 intr)
1089{
1090 struct mpsc_tx_desc *txre;
1091
1092 txre = (struct mpsc_tx_desc *)(pi->txr
1093 + (pi->txr_head * MPSC_TXRE_SIZE));
1094
1095 txre->bytecnt = cpu_to_be16(count);
1096 txre->shadow = txre->bytecnt;
1097 wmb(); /* ensure cmdstat is last field updated */
1098 txre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O | SDMA_DESC_CMDSTAT_F
1099 | SDMA_DESC_CMDSTAT_L
1100 | ((intr) ? SDMA_DESC_CMDSTAT_EI : 0));
1101 wmb();
1102 dma_cache_sync(pi->port.dev, (void *)txre, MPSC_TXRE_SIZE,
1103 DMA_BIDIRECTIONAL);
1104#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1105 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1106 flush_dcache_range((ulong)txre,
1107 (ulong)txre + MPSC_TXRE_SIZE);
1108#endif
1109}
1110
1111static void mpsc_copy_tx_data(struct mpsc_port_info *pi)
1112{
1113 struct circ_buf *xmit = &pi->port.state->xmit;
1114 u8 *bp;
1115 u32 i;
1116
1117 /* Make sure the desc ring isn't full */
1118 while (CIRC_CNT(pi->txr_head, pi->txr_tail, MPSC_TXR_ENTRIES)
1119 < (MPSC_TXR_ENTRIES - 1)) {
1120 if (pi->port.x_char) {
1121 /*
1122 * Ideally, we should use the TCS field in
1123 * CHR_1 to put the x_char out immediately but
1124 * errata prevents us from being able to read
1125 * CHR_2 to know that its safe to write to
1126 * CHR_1. Instead, just put it in-band with
1127 * all the other Tx data.
1128 */
1129 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1130 *bp = pi->port.x_char;
1131 pi->port.x_char = 0;
1132 i = 1;
1133 } else if (!uart_circ_empty(xmit)
1134 && !uart_tx_stopped(&pi->port)) {
1135 i = min((u32)MPSC_TXBE_SIZE,
1136 (u32)uart_circ_chars_pending(xmit));
1137 i = min(i, (u32)CIRC_CNT_TO_END(xmit->head, xmit->tail,
1138 UART_XMIT_SIZE));
1139 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1140 memcpy(bp, &xmit->buf[xmit->tail], i);
1141 xmit->tail = (xmit->tail + i) & (UART_XMIT_SIZE - 1);
1142
1143 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1144 uart_write_wakeup(&pi->port);
1145 } else { /* All tx data copied into ring bufs */
1146 return;
1147 }
1148
1149 dma_cache_sync(pi->port.dev, (void *)bp, MPSC_TXBE_SIZE,
1150 DMA_BIDIRECTIONAL);
1151#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1152 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1153 flush_dcache_range((ulong)bp,
1154 (ulong)bp + MPSC_TXBE_SIZE);
1155#endif
1156 mpsc_setup_tx_desc(pi, i, 1);
1157
1158 /* Advance to next descriptor */
1159 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
1160 }
1161}
1162
1163static int mpsc_tx_intr(struct mpsc_port_info *pi)
1164{
1165 struct mpsc_tx_desc *txre;
1166 int rc = 0;
1167 unsigned long iflags;
1168
1169 spin_lock_irqsave(&pi->tx_lock, iflags);
1170
1171 if (!mpsc_sdma_tx_active(pi)) {
1172 txre = (struct mpsc_tx_desc *)(pi->txr
1173 + (pi->txr_tail * MPSC_TXRE_SIZE));
1174
1175 dma_cache_sync(pi->port.dev, (void *)txre, MPSC_TXRE_SIZE,
1176 DMA_FROM_DEVICE);
1177#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1178 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1179 invalidate_dcache_range((ulong)txre,
1180 (ulong)txre + MPSC_TXRE_SIZE);
1181#endif
1182
1183 while (!(be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O)) {
1184 rc = 1;
1185 pi->port.icount.tx += be16_to_cpu(txre->bytecnt);
1186 pi->txr_tail = (pi->txr_tail+1) & (MPSC_TXR_ENTRIES-1);
1187
1188 /* If no more data to tx, fall out of loop */
1189 if (pi->txr_head == pi->txr_tail)
1190 break;
1191
1192 txre = (struct mpsc_tx_desc *)(pi->txr
1193 + (pi->txr_tail * MPSC_TXRE_SIZE));
1194 dma_cache_sync(pi->port.dev, (void *)txre,
1195 MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
1196#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1197 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1198 invalidate_dcache_range((ulong)txre,
1199 (ulong)txre + MPSC_TXRE_SIZE);
1200#endif
1201 }
1202
1203 mpsc_copy_tx_data(pi);
1204 mpsc_sdma_start_tx(pi); /* start next desc if ready */
1205 }
1206
1207 spin_unlock_irqrestore(&pi->tx_lock, iflags);
1208 return rc;
1209}
1210
1211/*
1212 * This is the driver's interrupt handler. To avoid a race, we first clear
1213 * the interrupt, then handle any completed Rx/Tx descriptors. When done
1214 * handling those descriptors, we restart the Rx/Tx engines if they're stopped.
1215 */
1216static irqreturn_t mpsc_sdma_intr(int irq, void *dev_id)
1217{
1218 struct mpsc_port_info *pi = dev_id;
1219 ulong iflags;
1220 int rc = IRQ_NONE;
1221
1222 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Received\n",pi->port.line);
1223
1224 spin_lock_irqsave(&pi->port.lock, iflags);
1225 mpsc_sdma_intr_ack(pi);
1226 if (mpsc_rx_intr(pi))
1227 rc = IRQ_HANDLED;
1228 if (mpsc_tx_intr(pi))
1229 rc = IRQ_HANDLED;
1230 spin_unlock_irqrestore(&pi->port.lock, iflags);
1231
1232 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Handled\n", pi->port.line);
1233 return rc;
1234}
1235
1236/*
1237 ******************************************************************************
1238 *
1239 * serial_core.c Interface routines
1240 *
1241 ******************************************************************************
1242 */
1243static uint mpsc_tx_empty(struct uart_port *port)
1244{
1245 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1246 ulong iflags;
1247 uint rc;
1248
1249 spin_lock_irqsave(&pi->port.lock, iflags);
1250 rc = mpsc_sdma_tx_active(pi) ? 0 : TIOCSER_TEMT;
1251 spin_unlock_irqrestore(&pi->port.lock, iflags);
1252
1253 return rc;
1254}
1255
1256static void mpsc_set_mctrl(struct uart_port *port, uint mctrl)
1257{
1258 /* Have no way to set modem control lines AFAICT */
1259}
1260
1261static uint mpsc_get_mctrl(struct uart_port *port)
1262{
1263 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1264 u32 mflags, status;
1265
1266 status = (pi->mirror_regs) ? pi->MPSC_CHR_10_m
1267 : readl(pi->mpsc_base + MPSC_CHR_10);
1268
1269 mflags = 0;
1270 if (status & 0x1)
1271 mflags |= TIOCM_CTS;
1272 if (status & 0x2)
1273 mflags |= TIOCM_CAR;
1274
1275 return mflags | TIOCM_DSR; /* No way to tell if DSR asserted */
1276}
1277
1278static void mpsc_stop_tx(struct uart_port *port)
1279{
1280 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1281
1282 pr_debug("mpsc_stop_tx[%d]\n", port->line);
1283
1284 mpsc_freeze(pi);
1285}
1286
1287static void mpsc_start_tx(struct uart_port *port)
1288{
1289 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1290 unsigned long iflags;
1291
1292 spin_lock_irqsave(&pi->tx_lock, iflags);
1293
1294 mpsc_unfreeze(pi);
1295 mpsc_copy_tx_data(pi);
1296 mpsc_sdma_start_tx(pi);
1297
1298 spin_unlock_irqrestore(&pi->tx_lock, iflags);
1299
1300 pr_debug("mpsc_start_tx[%d]\n", port->line);
1301}
1302
1303static void mpsc_start_rx(struct mpsc_port_info *pi)
1304{
1305 pr_debug("mpsc_start_rx[%d]: Starting...\n", pi->port.line);
1306
1307 if (pi->rcv_data) {
1308 mpsc_enter_hunt(pi);
1309 mpsc_sdma_cmd(pi, SDMA_SDCM_ERD);
1310 }
1311}
1312
1313static void mpsc_stop_rx(struct uart_port *port)
1314{
1315 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1316
1317 pr_debug("mpsc_stop_rx[%d]: Stopping...\n", port->line);
1318
1319 if (pi->mirror_regs) {
1320 writel(pi->MPSC_CHR_2_m | MPSC_CHR_2_RA,
1321 pi->mpsc_base + MPSC_CHR_2);
1322 /* Erratum prevents reading CHR_2 so just delay for a while */
1323 udelay(100);
1324 } else {
1325 writel(readl(pi->mpsc_base + MPSC_CHR_2) | MPSC_CHR_2_RA,
1326 pi->mpsc_base + MPSC_CHR_2);
1327
1328 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_RA)
1329 udelay(10);
1330 }
1331
1332 mpsc_sdma_cmd(pi, SDMA_SDCM_AR);
1333}
1334
1335static void mpsc_enable_ms(struct uart_port *port)
1336{
1337}
1338
1339static void mpsc_break_ctl(struct uart_port *port, int ctl)
1340{
1341 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1342 ulong flags;
1343 u32 v;
1344
1345 v = ctl ? 0x00ff0000 : 0;
1346
1347 spin_lock_irqsave(&pi->port.lock, flags);
1348 if (pi->mirror_regs)
1349 pi->MPSC_CHR_1_m = v;
1350 writel(v, pi->mpsc_base + MPSC_CHR_1);
1351 spin_unlock_irqrestore(&pi->port.lock, flags);
1352}
1353
1354static int mpsc_startup(struct uart_port *port)
1355{
1356 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1357 u32 flag = 0;
1358 int rc;
1359
1360 pr_debug("mpsc_startup[%d]: Starting up MPSC, irq: %d\n",
1361 port->line, pi->port.irq);
1362
1363 if ((rc = mpsc_make_ready(pi)) == 0) {
1364 /* Setup IRQ handler */
1365 mpsc_sdma_intr_ack(pi);
1366
1367 /* If irq's are shared, need to set flag */
1368 if (mpsc_ports[0].port.irq == mpsc_ports[1].port.irq)
1369 flag = IRQF_SHARED;
1370
1371 if (request_irq(pi->port.irq, mpsc_sdma_intr, flag,
1372 "mpsc-sdma", pi))
1373 printk(KERN_ERR "MPSC: Can't get SDMA IRQ %d\n",
1374 pi->port.irq);
1375
1376 mpsc_sdma_intr_unmask(pi, 0xf);
1377 mpsc_sdma_set_rx_ring(pi, (struct mpsc_rx_desc *)(pi->rxr_p
1378 + (pi->rxr_posn * MPSC_RXRE_SIZE)));
1379 }
1380
1381 return rc;
1382}
1383
1384static void mpsc_shutdown(struct uart_port *port)
1385{
1386 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1387
1388 pr_debug("mpsc_shutdown[%d]: Shutting down MPSC\n", port->line);
1389
1390 mpsc_sdma_stop(pi);
1391 free_irq(pi->port.irq, pi);
1392}
1393
1394static void mpsc_set_termios(struct uart_port *port, struct ktermios *termios,
1395 struct ktermios *old)
1396{
1397 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1398 u32 baud;
1399 ulong flags;
1400 u32 chr_bits, stop_bits, par;
1401
1402 pi->c_iflag = termios->c_iflag;
1403 pi->c_cflag = termios->c_cflag;
1404
1405 switch (termios->c_cflag & CSIZE) {
1406 case CS5:
1407 chr_bits = MPSC_MPCR_CL_5;
1408 break;
1409 case CS6:
1410 chr_bits = MPSC_MPCR_CL_6;
1411 break;
1412 case CS7:
1413 chr_bits = MPSC_MPCR_CL_7;
1414 break;
1415 case CS8:
1416 default:
1417 chr_bits = MPSC_MPCR_CL_8;
1418 break;
1419 }
1420
1421 if (termios->c_cflag & CSTOPB)
1422 stop_bits = MPSC_MPCR_SBL_2;
1423 else
1424 stop_bits = MPSC_MPCR_SBL_1;
1425
1426 par = MPSC_CHR_2_PAR_EVEN;
1427 if (termios->c_cflag & PARENB)
1428 if (termios->c_cflag & PARODD)
1429 par = MPSC_CHR_2_PAR_ODD;
1430#ifdef CMSPAR
1431 if (termios->c_cflag & CMSPAR) {
1432 if (termios->c_cflag & PARODD)
1433 par = MPSC_CHR_2_PAR_MARK;
1434 else
1435 par = MPSC_CHR_2_PAR_SPACE;
1436 }
1437#endif
1438
1439 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk);
1440
1441 spin_lock_irqsave(&pi->port.lock, flags);
1442
1443 uart_update_timeout(port, termios->c_cflag, baud);
1444
1445 mpsc_set_char_length(pi, chr_bits);
1446 mpsc_set_stop_bit_length(pi, stop_bits);
1447 mpsc_set_parity(pi, par);
1448 mpsc_set_baudrate(pi, baud);
1449
1450 /* Characters/events to read */
1451 pi->port.read_status_mask = SDMA_DESC_CMDSTAT_OR;
1452
1453 if (termios->c_iflag & INPCK)
1454 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_PE
1455 | SDMA_DESC_CMDSTAT_FR;
1456
1457 if (termios->c_iflag & (BRKINT | PARMRK))
1458 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_BR;
1459
1460 /* Characters/events to ignore */
1461 pi->port.ignore_status_mask = 0;
1462
1463 if (termios->c_iflag & IGNPAR)
1464 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_PE
1465 | SDMA_DESC_CMDSTAT_FR;
1466
1467 if (termios->c_iflag & IGNBRK) {
1468 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_BR;
1469
1470 if (termios->c_iflag & IGNPAR)
1471 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_OR;
1472 }
1473
1474 if ((termios->c_cflag & CREAD)) {
1475 if (!pi->rcv_data) {
1476 pi->rcv_data = 1;
1477 mpsc_start_rx(pi);
1478 }
1479 } else if (pi->rcv_data) {
1480 mpsc_stop_rx(port);
1481 pi->rcv_data = 0;
1482 }
1483
1484 spin_unlock_irqrestore(&pi->port.lock, flags);
1485}
1486
1487static const char *mpsc_type(struct uart_port *port)
1488{
1489 pr_debug("mpsc_type[%d]: port type: %s\n", port->line,MPSC_DRIVER_NAME);
1490 return MPSC_DRIVER_NAME;
1491}
1492
1493static int mpsc_request_port(struct uart_port *port)
1494{
1495 /* Should make chip/platform specific call */
1496 return 0;
1497}
1498
1499static void mpsc_release_port(struct uart_port *port)
1500{
1501 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1502
1503 if (pi->ready) {
1504 mpsc_uninit_rings(pi);
1505 mpsc_free_ring_mem(pi);
1506 pi->ready = 0;
1507 }
1508}
1509
1510static void mpsc_config_port(struct uart_port *port, int flags)
1511{
1512}
1513
1514static int mpsc_verify_port(struct uart_port *port, struct serial_struct *ser)
1515{
1516 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1517 int rc = 0;
1518
1519 pr_debug("mpsc_verify_port[%d]: Verifying port data\n", pi->port.line);
1520
1521 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPSC)
1522 rc = -EINVAL;
1523 else if (pi->port.irq != ser->irq)
1524 rc = -EINVAL;
1525 else if (ser->io_type != SERIAL_IO_MEM)
1526 rc = -EINVAL;
1527 else if (pi->port.uartclk / 16 != ser->baud_base) /* Not sure */
1528 rc = -EINVAL;
1529 else if ((void *)pi->port.mapbase != ser->iomem_base)
1530 rc = -EINVAL;
1531 else if (pi->port.iobase != ser->port)
1532 rc = -EINVAL;
1533 else if (ser->hub6 != 0)
1534 rc = -EINVAL;
1535
1536 return rc;
1537}
1538#ifdef CONFIG_CONSOLE_POLL
1539/* Serial polling routines for writing and reading from the uart while
1540 * in an interrupt or debug context.
1541 */
1542
1543static char poll_buf[2048];
1544static int poll_ptr;
1545static int poll_cnt;
1546static void mpsc_put_poll_char(struct uart_port *port,
1547 unsigned char c);
1548
1549static int mpsc_get_poll_char(struct uart_port *port)
1550{
1551 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1552 struct mpsc_rx_desc *rxre;
1553 u32 cmdstat, bytes_in, i;
1554 u8 *bp;
1555
1556 if (!serial_polled)
1557 serial_polled = 1;
1558
1559 pr_debug("mpsc_rx_intr[%d]: Handling Rx intr\n", pi->port.line);
1560
1561 if (poll_cnt) {
1562 poll_cnt--;
1563 return poll_buf[poll_ptr++];
1564 }
1565 poll_ptr = 0;
1566 poll_cnt = 0;
1567
1568 while (poll_cnt == 0) {
1569 rxre = (struct mpsc_rx_desc *)(pi->rxr +
1570 (pi->rxr_posn*MPSC_RXRE_SIZE));
1571 dma_cache_sync(pi->port.dev, (void *)rxre,
1572 MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
1573#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1574 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1575 invalidate_dcache_range((ulong)rxre,
1576 (ulong)rxre + MPSC_RXRE_SIZE);
1577#endif
1578 /*
1579 * Loop through Rx descriptors handling ones that have
1580 * been completed.
1581 */
1582 while (poll_cnt == 0 &&
1583 !((cmdstat = be32_to_cpu(rxre->cmdstat)) &
1584 SDMA_DESC_CMDSTAT_O)){
1585 bytes_in = be16_to_cpu(rxre->bytecnt);
1586 bp = pi->rxb + (pi->rxr_posn * MPSC_RXBE_SIZE);
1587 dma_cache_sync(pi->port.dev, (void *) bp,
1588 MPSC_RXBE_SIZE, DMA_FROM_DEVICE);
1589#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1590 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1591 invalidate_dcache_range((ulong)bp,
1592 (ulong)bp + MPSC_RXBE_SIZE);
1593#endif
1594 if ((unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
1595 SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) &&
1596 !(cmdstat & pi->port.ignore_status_mask)) {
1597 poll_buf[poll_cnt] = *bp;
1598 poll_cnt++;
1599 } else {
1600 for (i = 0; i < bytes_in; i++) {
1601 poll_buf[poll_cnt] = *bp++;
1602 poll_cnt++;
1603 }
1604 pi->port.icount.rx += bytes_in;
1605 }
1606 rxre->bytecnt = cpu_to_be16(0);
1607 wmb();
1608 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
1609 SDMA_DESC_CMDSTAT_EI |
1610 SDMA_DESC_CMDSTAT_F |
1611 SDMA_DESC_CMDSTAT_L);
1612 wmb();
1613 dma_cache_sync(pi->port.dev, (void *)rxre,
1614 MPSC_RXRE_SIZE, DMA_BIDIRECTIONAL);
1615#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1616 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1617 flush_dcache_range((ulong)rxre,
1618 (ulong)rxre + MPSC_RXRE_SIZE);
1619#endif
1620
1621 /* Advance to next descriptor */
1622 pi->rxr_posn = (pi->rxr_posn + 1) &
1623 (MPSC_RXR_ENTRIES - 1);
1624 rxre = (struct mpsc_rx_desc *)(pi->rxr +
1625 (pi->rxr_posn * MPSC_RXRE_SIZE));
1626 dma_cache_sync(pi->port.dev, (void *)rxre,
1627 MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
1628#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1629 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1630 invalidate_dcache_range((ulong)rxre,
1631 (ulong)rxre + MPSC_RXRE_SIZE);
1632#endif
1633 }
1634
1635 /* Restart rx engine, if its stopped */
1636 if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0)
1637 mpsc_start_rx(pi);
1638 }
1639 if (poll_cnt) {
1640 poll_cnt--;
1641 return poll_buf[poll_ptr++];
1642 }
1643
1644 return 0;
1645}
1646
1647
1648static void mpsc_put_poll_char(struct uart_port *port,
1649 unsigned char c)
1650{
1651 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1652 u32 data;
1653
1654 data = readl(pi->mpsc_base + MPSC_MPCR);
1655 writeb(c, pi->mpsc_base + MPSC_CHR_1);
1656 mb();
1657 data = readl(pi->mpsc_base + MPSC_CHR_2);
1658 data |= MPSC_CHR_2_TTCS;
1659 writel(data, pi->mpsc_base + MPSC_CHR_2);
1660 mb();
1661
1662 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_TTCS);
1663}
1664#endif
1665
1666static struct uart_ops mpsc_pops = {
1667 .tx_empty = mpsc_tx_empty,
1668 .set_mctrl = mpsc_set_mctrl,
1669 .get_mctrl = mpsc_get_mctrl,
1670 .stop_tx = mpsc_stop_tx,
1671 .start_tx = mpsc_start_tx,
1672 .stop_rx = mpsc_stop_rx,
1673 .enable_ms = mpsc_enable_ms,
1674 .break_ctl = mpsc_break_ctl,
1675 .startup = mpsc_startup,
1676 .shutdown = mpsc_shutdown,
1677 .set_termios = mpsc_set_termios,
1678 .type = mpsc_type,
1679 .release_port = mpsc_release_port,
1680 .request_port = mpsc_request_port,
1681 .config_port = mpsc_config_port,
1682 .verify_port = mpsc_verify_port,
1683#ifdef CONFIG_CONSOLE_POLL
1684 .poll_get_char = mpsc_get_poll_char,
1685 .poll_put_char = mpsc_put_poll_char,
1686#endif
1687};
1688
1689/*
1690 ******************************************************************************
1691 *
1692 * Console Interface Routines
1693 *
1694 ******************************************************************************
1695 */
1696
1697#ifdef CONFIG_SERIAL_MPSC_CONSOLE
1698static void mpsc_console_write(struct console *co, const char *s, uint count)
1699{
1700 struct mpsc_port_info *pi = &mpsc_ports[co->index];
1701 u8 *bp, *dp, add_cr = 0;
1702 int i;
1703 unsigned long iflags;
1704
1705 spin_lock_irqsave(&pi->tx_lock, iflags);
1706
1707 while (pi->txr_head != pi->txr_tail) {
1708 while (mpsc_sdma_tx_active(pi))
1709 udelay(100);
1710 mpsc_sdma_intr_ack(pi);
1711 mpsc_tx_intr(pi);
1712 }
1713
1714 while (mpsc_sdma_tx_active(pi))
1715 udelay(100);
1716
1717 while (count > 0) {
1718 bp = dp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1719
1720 for (i = 0; i < MPSC_TXBE_SIZE; i++) {
1721 if (count == 0)
1722 break;
1723
1724 if (add_cr) {
1725 *(dp++) = '\r';
1726 add_cr = 0;
1727 } else {
1728 *(dp++) = *s;
1729
1730 if (*(s++) == '\n') { /* add '\r' after '\n' */
1731 add_cr = 1;
1732 count++;
1733 }
1734 }
1735
1736 count--;
1737 }
1738
1739 dma_cache_sync(pi->port.dev, (void *)bp, MPSC_TXBE_SIZE,
1740 DMA_BIDIRECTIONAL);
1741#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1742 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1743 flush_dcache_range((ulong)bp,
1744 (ulong)bp + MPSC_TXBE_SIZE);
1745#endif
1746 mpsc_setup_tx_desc(pi, i, 0);
1747 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
1748 mpsc_sdma_start_tx(pi);
1749
1750 while (mpsc_sdma_tx_active(pi))
1751 udelay(100);
1752
1753 pi->txr_tail = (pi->txr_tail + 1) & (MPSC_TXR_ENTRIES - 1);
1754 }
1755
1756 spin_unlock_irqrestore(&pi->tx_lock, iflags);
1757}
1758
1759static int __init mpsc_console_setup(struct console *co, char *options)
1760{
1761 struct mpsc_port_info *pi;
1762 int baud, bits, parity, flow;
1763
1764 pr_debug("mpsc_console_setup[%d]: options: %s\n", co->index, options);
1765
1766 if (co->index >= MPSC_NUM_CTLRS)
1767 co->index = 0;
1768
1769 pi = &mpsc_ports[co->index];
1770
1771 baud = pi->default_baud;
1772 bits = pi->default_bits;
1773 parity = pi->default_parity;
1774 flow = pi->default_flow;
1775
1776 if (!pi->port.ops)
1777 return -ENODEV;
1778
1779 spin_lock_init(&pi->port.lock); /* Temporary fix--copied from 8250.c */
1780
1781 if (options)
1782 uart_parse_options(options, &baud, &parity, &bits, &flow);
1783
1784 return uart_set_options(&pi->port, co, baud, parity, bits, flow);
1785}
1786
1787static struct console mpsc_console = {
1788 .name = MPSC_DEV_NAME,
1789 .write = mpsc_console_write,
1790 .device = uart_console_device,
1791 .setup = mpsc_console_setup,
1792 .flags = CON_PRINTBUFFER,
1793 .index = -1,
1794 .data = &mpsc_reg,
1795};
1796
1797static int __init mpsc_late_console_init(void)
1798{
1799 pr_debug("mpsc_late_console_init: Enter\n");
1800
1801 if (!(mpsc_console.flags & CON_ENABLED))
1802 register_console(&mpsc_console);
1803 return 0;
1804}
1805
1806late_initcall(mpsc_late_console_init);
1807
1808#define MPSC_CONSOLE &mpsc_console
1809#else
1810#define MPSC_CONSOLE NULL
1811#endif
1812/*
1813 ******************************************************************************
1814 *
1815 * Dummy Platform Driver to extract & map shared register regions
1816 *
1817 ******************************************************************************
1818 */
1819static void mpsc_resource_err(char *s)
1820{
1821 printk(KERN_WARNING "MPSC: Platform device resource error in %s\n", s);
1822}
1823
1824static int mpsc_shared_map_regs(struct platform_device *pd)
1825{
1826 struct resource *r;
1827
1828 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1829 MPSC_ROUTING_BASE_ORDER))
1830 && request_mem_region(r->start,
1831 MPSC_ROUTING_REG_BLOCK_SIZE,
1832 "mpsc_routing_regs")) {
1833 mpsc_shared_regs.mpsc_routing_base = ioremap(r->start,
1834 MPSC_ROUTING_REG_BLOCK_SIZE);
1835 mpsc_shared_regs.mpsc_routing_base_p = r->start;
1836 } else {
1837 mpsc_resource_err("MPSC routing base");
1838 return -ENOMEM;
1839 }
1840
1841 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1842 MPSC_SDMA_INTR_BASE_ORDER))
1843 && request_mem_region(r->start,
1844 MPSC_SDMA_INTR_REG_BLOCK_SIZE,
1845 "sdma_intr_regs")) {
1846 mpsc_shared_regs.sdma_intr_base = ioremap(r->start,
1847 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1848 mpsc_shared_regs.sdma_intr_base_p = r->start;
1849 } else {
1850 iounmap(mpsc_shared_regs.mpsc_routing_base);
1851 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1852 MPSC_ROUTING_REG_BLOCK_SIZE);
1853 mpsc_resource_err("SDMA intr base");
1854 return -ENOMEM;
1855 }
1856
1857 return 0;
1858}
1859
1860static void mpsc_shared_unmap_regs(void)
1861{
1862 if (!mpsc_shared_regs.mpsc_routing_base) {
1863 iounmap(mpsc_shared_regs.mpsc_routing_base);
1864 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1865 MPSC_ROUTING_REG_BLOCK_SIZE);
1866 }
1867 if (!mpsc_shared_regs.sdma_intr_base) {
1868 iounmap(mpsc_shared_regs.sdma_intr_base);
1869 release_mem_region(mpsc_shared_regs.sdma_intr_base_p,
1870 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1871 }
1872
1873 mpsc_shared_regs.mpsc_routing_base = NULL;
1874 mpsc_shared_regs.sdma_intr_base = NULL;
1875
1876 mpsc_shared_regs.mpsc_routing_base_p = 0;
1877 mpsc_shared_regs.sdma_intr_base_p = 0;
1878}
1879
1880static int mpsc_shared_drv_probe(struct platform_device *dev)
1881{
1882 struct mpsc_shared_pdata *pdata;
1883 int rc = -ENODEV;
1884
1885 if (dev->id == 0) {
1886 if (!(rc = mpsc_shared_map_regs(dev))) {
1887 pdata = (struct mpsc_shared_pdata *)
1888 dev->dev.platform_data;
1889
1890 mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val;
1891 mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val;
1892 mpsc_shared_regs.MPSC_TCRR_m= pdata->tcrr_val;
1893 mpsc_shared_regs.SDMA_INTR_CAUSE_m =
1894 pdata->intr_cause_val;
1895 mpsc_shared_regs.SDMA_INTR_MASK_m =
1896 pdata->intr_mask_val;
1897
1898 rc = 0;
1899 }
1900 }
1901
1902 return rc;
1903}
1904
1905static int mpsc_shared_drv_remove(struct platform_device *dev)
1906{
1907 int rc = -ENODEV;
1908
1909 if (dev->id == 0) {
1910 mpsc_shared_unmap_regs();
1911 mpsc_shared_regs.MPSC_MRR_m = 0;
1912 mpsc_shared_regs.MPSC_RCRR_m = 0;
1913 mpsc_shared_regs.MPSC_TCRR_m = 0;
1914 mpsc_shared_regs.SDMA_INTR_CAUSE_m = 0;
1915 mpsc_shared_regs.SDMA_INTR_MASK_m = 0;
1916 rc = 0;
1917 }
1918
1919 return rc;
1920}
1921
1922static struct platform_driver mpsc_shared_driver = {
1923 .probe = mpsc_shared_drv_probe,
1924 .remove = mpsc_shared_drv_remove,
1925 .driver = {
1926 .name = MPSC_SHARED_NAME,
1927 },
1928};
1929
1930/*
1931 ******************************************************************************
1932 *
1933 * Driver Interface Routines
1934 *
1935 ******************************************************************************
1936 */
1937static struct uart_driver mpsc_reg = {
1938 .owner = THIS_MODULE,
1939 .driver_name = MPSC_DRIVER_NAME,
1940 .dev_name = MPSC_DEV_NAME,
1941 .major = MPSC_MAJOR,
1942 .minor = MPSC_MINOR_START,
1943 .nr = MPSC_NUM_CTLRS,
1944 .cons = MPSC_CONSOLE,
1945};
1946
1947static int mpsc_drv_map_regs(struct mpsc_port_info *pi,
1948 struct platform_device *pd)
1949{
1950 struct resource *r;
1951
1952 if ((r = platform_get_resource(pd, IORESOURCE_MEM, MPSC_BASE_ORDER))
1953 && request_mem_region(r->start, MPSC_REG_BLOCK_SIZE,
1954 "mpsc_regs")) {
1955 pi->mpsc_base = ioremap(r->start, MPSC_REG_BLOCK_SIZE);
1956 pi->mpsc_base_p = r->start;
1957 } else {
1958 mpsc_resource_err("MPSC base");
1959 goto err;
1960 }
1961
1962 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1963 MPSC_SDMA_BASE_ORDER))
1964 && request_mem_region(r->start,
1965 MPSC_SDMA_REG_BLOCK_SIZE, "sdma_regs")) {
1966 pi->sdma_base = ioremap(r->start,MPSC_SDMA_REG_BLOCK_SIZE);
1967 pi->sdma_base_p = r->start;
1968 } else {
1969 mpsc_resource_err("SDMA base");
1970 if (pi->mpsc_base) {
1971 iounmap(pi->mpsc_base);
1972 pi->mpsc_base = NULL;
1973 }
1974 goto err;
1975 }
1976
1977 if ((r = platform_get_resource(pd,IORESOURCE_MEM,MPSC_BRG_BASE_ORDER))
1978 && request_mem_region(r->start,
1979 MPSC_BRG_REG_BLOCK_SIZE, "brg_regs")) {
1980 pi->brg_base = ioremap(r->start, MPSC_BRG_REG_BLOCK_SIZE);
1981 pi->brg_base_p = r->start;
1982 } else {
1983 mpsc_resource_err("BRG base");
1984 if (pi->mpsc_base) {
1985 iounmap(pi->mpsc_base);
1986 pi->mpsc_base = NULL;
1987 }
1988 if (pi->sdma_base) {
1989 iounmap(pi->sdma_base);
1990 pi->sdma_base = NULL;
1991 }
1992 goto err;
1993 }
1994 return 0;
1995
1996err:
1997 return -ENOMEM;
1998}
1999
2000static void mpsc_drv_unmap_regs(struct mpsc_port_info *pi)
2001{
2002 if (!pi->mpsc_base) {
2003 iounmap(pi->mpsc_base);
2004 release_mem_region(pi->mpsc_base_p, MPSC_REG_BLOCK_SIZE);
2005 }
2006 if (!pi->sdma_base) {
2007 iounmap(pi->sdma_base);
2008 release_mem_region(pi->sdma_base_p, MPSC_SDMA_REG_BLOCK_SIZE);
2009 }
2010 if (!pi->brg_base) {
2011 iounmap(pi->brg_base);
2012 release_mem_region(pi->brg_base_p, MPSC_BRG_REG_BLOCK_SIZE);
2013 }
2014
2015 pi->mpsc_base = NULL;
2016 pi->sdma_base = NULL;
2017 pi->brg_base = NULL;
2018
2019 pi->mpsc_base_p = 0;
2020 pi->sdma_base_p = 0;
2021 pi->brg_base_p = 0;
2022}
2023
2024static void mpsc_drv_get_platform_data(struct mpsc_port_info *pi,
2025 struct platform_device *pd, int num)
2026{
2027 struct mpsc_pdata *pdata;
2028
2029 pdata = (struct mpsc_pdata *)pd->dev.platform_data;
2030
2031 pi->port.uartclk = pdata->brg_clk_freq;
2032 pi->port.iotype = UPIO_MEM;
2033 pi->port.line = num;
2034 pi->port.type = PORT_MPSC;
2035 pi->port.fifosize = MPSC_TXBE_SIZE;
2036 pi->port.membase = pi->mpsc_base;
2037 pi->port.mapbase = (ulong)pi->mpsc_base;
2038 pi->port.ops = &mpsc_pops;
2039
2040 pi->mirror_regs = pdata->mirror_regs;
2041 pi->cache_mgmt = pdata->cache_mgmt;
2042 pi->brg_can_tune = pdata->brg_can_tune;
2043 pi->brg_clk_src = pdata->brg_clk_src;
2044 pi->mpsc_max_idle = pdata->max_idle;
2045 pi->default_baud = pdata->default_baud;
2046 pi->default_bits = pdata->default_bits;
2047 pi->default_parity = pdata->default_parity;
2048 pi->default_flow = pdata->default_flow;
2049
2050 /* Initial values of mirrored regs */
2051 pi->MPSC_CHR_1_m = pdata->chr_1_val;
2052 pi->MPSC_CHR_2_m = pdata->chr_2_val;
2053 pi->MPSC_CHR_10_m = pdata->chr_10_val;
2054 pi->MPSC_MPCR_m = pdata->mpcr_val;
2055 pi->BRG_BCR_m = pdata->bcr_val;
2056
2057 pi->shared_regs = &mpsc_shared_regs;
2058
2059 pi->port.irq = platform_get_irq(pd, 0);
2060}
2061
2062static int mpsc_drv_probe(struct platform_device *dev)
2063{
2064 struct mpsc_port_info *pi;
2065 int rc = -ENODEV;
2066
2067 pr_debug("mpsc_drv_probe: Adding MPSC %d\n", dev->id);
2068
2069 if (dev->id < MPSC_NUM_CTLRS) {
2070 pi = &mpsc_ports[dev->id];
2071
2072 if (!(rc = mpsc_drv_map_regs(pi, dev))) {
2073 mpsc_drv_get_platform_data(pi, dev, dev->id);
2074 pi->port.dev = &dev->dev;
2075
2076 if (!(rc = mpsc_make_ready(pi))) {
2077 spin_lock_init(&pi->tx_lock);
2078 if (!(rc = uart_add_one_port(&mpsc_reg,
2079 &pi->port))) {
2080 rc = 0;
2081 } else {
2082 mpsc_release_port((struct uart_port *)
2083 pi);
2084 mpsc_drv_unmap_regs(pi);
2085 }
2086 } else {
2087 mpsc_drv_unmap_regs(pi);
2088 }
2089 }
2090 }
2091
2092 return rc;
2093}
2094
2095static int mpsc_drv_remove(struct platform_device *dev)
2096{
2097 pr_debug("mpsc_drv_exit: Removing MPSC %d\n", dev->id);
2098
2099 if (dev->id < MPSC_NUM_CTLRS) {
2100 uart_remove_one_port(&mpsc_reg, &mpsc_ports[dev->id].port);
2101 mpsc_release_port((struct uart_port *)
2102 &mpsc_ports[dev->id].port);
2103 mpsc_drv_unmap_regs(&mpsc_ports[dev->id]);
2104 return 0;
2105 } else {
2106 return -ENODEV;
2107 }
2108}
2109
2110static struct platform_driver mpsc_driver = {
2111 .probe = mpsc_drv_probe,
2112 .remove = mpsc_drv_remove,
2113 .driver = {
2114 .name = MPSC_CTLR_NAME,
2115 .owner = THIS_MODULE,
2116 },
2117};
2118
2119static int __init mpsc_drv_init(void)
2120{
2121 int rc;
2122
2123 printk(KERN_INFO "Serial: MPSC driver\n");
2124
2125 memset(mpsc_ports, 0, sizeof(mpsc_ports));
2126 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
2127
2128 if (!(rc = uart_register_driver(&mpsc_reg))) {
2129 if (!(rc = platform_driver_register(&mpsc_shared_driver))) {
2130 if ((rc = platform_driver_register(&mpsc_driver))) {
2131 platform_driver_unregister(&mpsc_shared_driver);
2132 uart_unregister_driver(&mpsc_reg);
2133 }
2134 } else {
2135 uart_unregister_driver(&mpsc_reg);
2136 }
2137 }
2138
2139 return rc;
2140}
2141
2142static void __exit mpsc_drv_exit(void)
2143{
2144 platform_driver_unregister(&mpsc_driver);
2145 platform_driver_unregister(&mpsc_shared_driver);
2146 uart_unregister_driver(&mpsc_reg);
2147 memset(mpsc_ports, 0, sizeof(mpsc_ports));
2148 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
2149}
2150
2151module_init(mpsc_drv_init);
2152module_exit(mpsc_drv_exit);
2153
2154MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
2155MODULE_DESCRIPTION("Generic Marvell MPSC serial/UART driver");
2156MODULE_VERSION(MPSC_VERSION);
2157MODULE_LICENSE("GPL");
2158MODULE_ALIAS_CHARDEV_MAJOR(MPSC_MAJOR);
2159MODULE_ALIAS("platform:" MPSC_CTLR_NAME);
diff --git a/drivers/tty/serial/mrst_max3110.c b/drivers/tty/serial/mrst_max3110.c
new file mode 100644
index 000000000000..b62857bf2fdb
--- /dev/null
+++ b/drivers/tty/serial/mrst_max3110.c
@@ -0,0 +1,919 @@
1/*
2 * mrst_max3110.c - spi uart protocol driver for Maxim 3110
3 *
4 * Copyright (c) 2008-2010, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20/*
21 * Note:
22 * 1. From Max3110 spec, the Rx FIFO has 8 words, while the Tx FIFO only has
23 * 1 word. If SPI master controller doesn't support sclk frequency change,
24 * then the char need be sent out one by one with some delay
25 *
26 * 2. Currently only RX availabe interrrupt is used, no need for waiting TXE
27 * interrupt for a low speed UART device
28 */
29
30#include <linux/module.h>
31#include <linux/ioport.h>
32#include <linux/irq.h>
33#include <linux/init.h>
34#include <linux/console.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/serial_core.h>
38#include <linux/serial_reg.h>
39
40#include <linux/kthread.h>
41#include <linux/spi/spi.h>
42
43#include "mrst_max3110.h"
44
45#define PR_FMT "mrst_max3110: "
46
47#define UART_TX_NEEDED 1
48#define CON_TX_NEEDED 2
49#define BIT_IRQ_PENDING 3
50
51struct uart_max3110 {
52 struct uart_port port;
53 struct spi_device *spi;
54 char name[24];
55
56 wait_queue_head_t wq;
57 struct task_struct *main_thread;
58 struct task_struct *read_thread;
59 struct mutex thread_mutex;;
60
61 u32 baud;
62 u16 cur_conf;
63 u8 clock;
64 u8 parity, word_7bits;
65 u16 irq;
66
67 unsigned long uart_flags;
68
69 /* console related */
70 struct circ_buf con_xmit;
71};
72
73/* global data structure, may need be removed */
74static struct uart_max3110 *pmax;
75
76static void receive_chars(struct uart_max3110 *max,
77 unsigned char *str, int len);
78static int max3110_read_multi(struct uart_max3110 *max, u8 *buf);
79static void max3110_con_receive(struct uart_max3110 *max);
80
81static int max3110_write_then_read(struct uart_max3110 *max,
82 const void *txbuf, void *rxbuf, unsigned len, int always_fast)
83{
84 struct spi_device *spi = max->spi;
85 struct spi_message message;
86 struct spi_transfer x;
87 int ret;
88
89 spi_message_init(&message);
90 memset(&x, 0, sizeof x);
91 x.len = len;
92 x.tx_buf = txbuf;
93 x.rx_buf = rxbuf;
94 spi_message_add_tail(&x, &message);
95
96 if (always_fast)
97 x.speed_hz = spi->max_speed_hz;
98 else if (max->baud)
99 x.speed_hz = max->baud;
100
101 /* Do the i/o */
102 ret = spi_sync(spi, &message);
103 return ret;
104}
105
106/* Write a 16b word to the device */
107static int max3110_out(struct uart_max3110 *max, const u16 out)
108{
109 void *buf;
110 u16 *obuf, *ibuf;
111 u8 ch;
112 int ret;
113
114 buf = kzalloc(8, GFP_KERNEL | GFP_DMA);
115 if (!buf)
116 return -ENOMEM;
117
118 obuf = buf;
119 ibuf = buf + 4;
120 *obuf = out;
121 ret = max3110_write_then_read(max, obuf, ibuf, 2, 1);
122 if (ret) {
123 pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n",
124 __func__, ret, out);
125 goto exit;
126 }
127
128 /* If some valid data is read back */
129 if (*ibuf & MAX3110_READ_DATA_AVAILABLE) {
130 ch = *ibuf & 0xff;
131 receive_chars(max, &ch, 1);
132 }
133
134exit:
135 kfree(buf);
136 return ret;
137}
138
139/*
140 * This is usually used to read data from SPIC RX FIFO, which doesn't
141 * need any delay like flushing character out.
142 *
143 * Return how many valide bytes are read back
144 */
145static int max3110_read_multi(struct uart_max3110 *max, u8 *rxbuf)
146{
147 void *buf;
148 u16 *obuf, *ibuf;
149 u8 *pbuf, valid_str[M3110_RX_FIFO_DEPTH];
150 int i, j, blen;
151
152 blen = M3110_RX_FIFO_DEPTH * sizeof(u16);
153 buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA);
154 if (!buf) {
155 pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__);
156 return 0;
157 }
158
159 /* tx/rx always have the same length */
160 obuf = buf;
161 ibuf = buf + blen;
162
163 if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) {
164 kfree(buf);
165 return 0;
166 }
167
168 /* If caller doesn't provide a buffer, then handle received char */
169 pbuf = rxbuf ? rxbuf : valid_str;
170
171 for (i = 0, j = 0; i < M3110_RX_FIFO_DEPTH; i++) {
172 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
173 pbuf[j++] = ibuf[i] & 0xff;
174 }
175
176 if (j && (pbuf == valid_str))
177 receive_chars(max, valid_str, j);
178
179 kfree(buf);
180 return j;
181}
182
183static void serial_m3110_con_putchar(struct uart_port *port, int ch)
184{
185 struct uart_max3110 *max =
186 container_of(port, struct uart_max3110, port);
187 struct circ_buf *xmit = &max->con_xmit;
188
189 if (uart_circ_chars_free(xmit)) {
190 xmit->buf[xmit->head] = (char)ch;
191 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
192 }
193}
194
195/*
196 * Print a string to the serial port trying not to disturb
197 * any possible real use of the port...
198 *
199 * The console_lock must be held when we get here.
200 */
201static void serial_m3110_con_write(struct console *co,
202 const char *s, unsigned int count)
203{
204 if (!pmax)
205 return;
206
207 uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
208
209 if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags))
210 wake_up_process(pmax->main_thread);
211}
212
213static int __init
214serial_m3110_con_setup(struct console *co, char *options)
215{
216 struct uart_max3110 *max = pmax;
217 int baud = 115200;
218 int bits = 8;
219 int parity = 'n';
220 int flow = 'n';
221
222 pr_info(PR_FMT "setting up console\n");
223
224 if (co->index == -1)
225 co->index = 0;
226
227 if (!max) {
228 pr_err(PR_FMT "pmax is NULL, return");
229 return -ENODEV;
230 }
231
232 if (options)
233 uart_parse_options(options, &baud, &parity, &bits, &flow);
234
235 return uart_set_options(&max->port, co, baud, parity, bits, flow);
236}
237
238static struct tty_driver *serial_m3110_con_device(struct console *co,
239 int *index)
240{
241 struct uart_driver *p = co->data;
242 *index = co->index;
243 return p->tty_driver;
244}
245
246static struct uart_driver serial_m3110_reg;
247static struct console serial_m3110_console = {
248 .name = "ttyS",
249 .write = serial_m3110_con_write,
250 .device = serial_m3110_con_device,
251 .setup = serial_m3110_con_setup,
252 .flags = CON_PRINTBUFFER,
253 .index = -1,
254 .data = &serial_m3110_reg,
255};
256
257static unsigned int serial_m3110_tx_empty(struct uart_port *port)
258{
259 return 1;
260}
261
262static void serial_m3110_stop_tx(struct uart_port *port)
263{
264 return;
265}
266
267/* stop_rx will be called in spin_lock env */
268static void serial_m3110_stop_rx(struct uart_port *port)
269{
270 return;
271}
272
273#define WORDS_PER_XFER 128
274static void send_circ_buf(struct uart_max3110 *max,
275 struct circ_buf *xmit)
276{
277 void *buf;
278 u16 *obuf, *ibuf;
279 u8 valid_str[WORDS_PER_XFER];
280 int i, j, len, blen, dma_size, left, ret = 0;
281
282
283 dma_size = WORDS_PER_XFER * sizeof(u16) * 2;
284 buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA);
285 if (!buf)
286 return;
287 obuf = buf;
288 ibuf = buf + dma_size/2;
289
290 while (!uart_circ_empty(xmit)) {
291 left = uart_circ_chars_pending(xmit);
292 while (left) {
293 len = min(left, WORDS_PER_XFER);
294 blen = len * sizeof(u16);
295 memset(ibuf, 0, blen);
296
297 for (i = 0; i < len; i++) {
298 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
299 xmit->tail = (xmit->tail + 1) &
300 (UART_XMIT_SIZE - 1);
301 }
302
303 /* Fail to send msg to console is not very critical */
304 ret = max3110_write_then_read(max, obuf, ibuf, blen, 0);
305 if (ret)
306 pr_warning(PR_FMT "%s(): get err msg %d\n",
307 __func__, ret);
308
309 for (i = 0, j = 0; i < len; i++) {
310 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
311 valid_str[j++] = ibuf[i] & 0xff;
312 }
313
314 if (j)
315 receive_chars(max, valid_str, j);
316
317 max->port.icount.tx += len;
318 left -= len;
319 }
320 }
321
322 kfree(buf);
323}
324
325static void transmit_char(struct uart_max3110 *max)
326{
327 struct uart_port *port = &max->port;
328 struct circ_buf *xmit = &port->state->xmit;
329
330 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
331 return;
332
333 send_circ_buf(max, xmit);
334
335 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
336 uart_write_wakeup(port);
337
338 if (uart_circ_empty(xmit))
339 serial_m3110_stop_tx(port);
340}
341
342/*
343 * This will be called by uart_write() and tty_write, can't
344 * go to sleep
345 */
346static void serial_m3110_start_tx(struct uart_port *port)
347{
348 struct uart_max3110 *max =
349 container_of(port, struct uart_max3110, port);
350
351 if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
352 wake_up_process(max->main_thread);
353}
354
355static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len)
356{
357 struct uart_port *port = &max->port;
358 struct tty_struct *tty;
359 int usable;
360
361 /* If uart is not opened, just return */
362 if (!port->state)
363 return;
364
365 tty = port->state->port.tty;
366 if (!tty)
367 return;
368
369 while (len) {
370 usable = tty_buffer_request_room(tty, len);
371 if (usable) {
372 tty_insert_flip_string(tty, str, usable);
373 str += usable;
374 port->icount.rx += usable;
375 }
376 len -= usable;
377 }
378 tty_flip_buffer_push(tty);
379}
380
381/*
382 * This routine will be used in read_thread or RX IRQ handling,
383 * it will first do one round buffer read(8 words), if there is some
384 * valid RX data, will try to read 5 more rounds till all data
385 * is read out.
386 *
387 * Use stack space as data buffer to save some system load, and chose
388 * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
389 * receiving bulk data, a much bigger buffer may cause stack overflow
390 */
391static void max3110_con_receive(struct uart_max3110 *max)
392{
393 int loop = 1, num, total = 0;
394 u8 recv_buf[512], *pbuf;
395
396 pbuf = recv_buf;
397 do {
398 num = max3110_read_multi(max, pbuf);
399
400 if (num) {
401 loop = 5;
402 pbuf += num;
403 total += num;
404
405 if (total >= 504) {
406 receive_chars(max, recv_buf, total);
407 pbuf = recv_buf;
408 total = 0;
409 }
410 }
411 } while (--loop);
412
413 if (total)
414 receive_chars(max, recv_buf, total);
415}
416
417static int max3110_main_thread(void *_max)
418{
419 struct uart_max3110 *max = _max;
420 wait_queue_head_t *wq = &max->wq;
421 int ret = 0;
422 struct circ_buf *xmit = &max->con_xmit;
423
424 init_waitqueue_head(wq);
425 pr_info(PR_FMT "start main thread\n");
426
427 do {
428 wait_event_interruptible(*wq, max->uart_flags || kthread_should_stop());
429
430 mutex_lock(&max->thread_mutex);
431
432 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
433 max3110_con_receive(max);
434
435 /* first handle console output */
436 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
437 send_circ_buf(max, xmit);
438
439 /* handle uart output */
440 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
441 transmit_char(max);
442
443 mutex_unlock(&max->thread_mutex);
444
445 } while (!kthread_should_stop());
446
447 return ret;
448}
449
450static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
451{
452 struct uart_max3110 *max = dev_id;
453
454 /* max3110's irq is a falling edge, not level triggered,
455 * so no need to disable the irq */
456 if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
457 wake_up_process(max->main_thread);
458
459 return IRQ_HANDLED;
460}
461
462/* if don't use RX IRQ, then need a thread to polling read */
463static int max3110_read_thread(void *_max)
464{
465 struct uart_max3110 *max = _max;
466
467 pr_info(PR_FMT "start read thread\n");
468 do {
469 /*
470 * If can't acquire the mutex, it means the main thread
471 * is running which will also perform the rx job
472 */
473 if (mutex_trylock(&max->thread_mutex)) {
474 max3110_con_receive(max);
475 mutex_unlock(&max->thread_mutex);
476 }
477
478 set_current_state(TASK_INTERRUPTIBLE);
479 schedule_timeout(HZ / 20);
480 } while (!kthread_should_stop());
481
482 return 0;
483}
484
485static int serial_m3110_startup(struct uart_port *port)
486{
487 struct uart_max3110 *max =
488 container_of(port, struct uart_max3110, port);
489 u16 config = 0;
490 int ret = 0;
491
492 if (port->line != 0) {
493 pr_err(PR_FMT "uart port startup failed\n");
494 return -1;
495 }
496
497 /* Disable all IRQ and config it to 115200, 8n1 */
498 config = WC_TAG | WC_FIFO_ENABLE
499 | WC_1_STOPBITS
500 | WC_8BIT_WORD
501 | WC_BAUD_DR2;
502
503 /* as we use thread to handle tx/rx, need set low latency */
504 port->state->port.tty->low_latency = 1;
505
506 if (max->irq) {
507 max->read_thread = NULL;
508 ret = request_irq(max->irq, serial_m3110_irq,
509 IRQ_TYPE_EDGE_FALLING, "max3110", max);
510 if (ret) {
511 max->irq = 0;
512 pr_err(PR_FMT "unable to allocate IRQ, polling\n");
513 } else {
514 /* Enable RX IRQ only */
515 config |= WC_RXA_IRQ_ENABLE;
516 }
517 }
518
519 if (max->irq == 0) {
520 /* If IRQ is disabled, start a read thread for input data */
521 max->read_thread =
522 kthread_run(max3110_read_thread, max, "max3110_read");
523 if (IS_ERR(max->read_thread)) {
524 ret = PTR_ERR(max->read_thread);
525 max->read_thread = NULL;
526 pr_err(PR_FMT "Can't create read thread!\n");
527 return ret;
528 }
529 }
530
531 ret = max3110_out(max, config);
532 if (ret) {
533 if (max->irq)
534 free_irq(max->irq, max);
535 if (max->read_thread)
536 kthread_stop(max->read_thread);
537 max->read_thread = NULL;
538 return ret;
539 }
540
541 max->cur_conf = config;
542 return 0;
543}
544
545static void serial_m3110_shutdown(struct uart_port *port)
546{
547 struct uart_max3110 *max =
548 container_of(port, struct uart_max3110, port);
549 u16 config;
550
551 if (max->read_thread) {
552 kthread_stop(max->read_thread);
553 max->read_thread = NULL;
554 }
555
556 if (max->irq)
557 free_irq(max->irq, max);
558
559 /* Disable interrupts from this port */
560 config = WC_TAG | WC_SW_SHDI;
561 max3110_out(max, config);
562}
563
564static void serial_m3110_release_port(struct uart_port *port)
565{
566}
567
568static int serial_m3110_request_port(struct uart_port *port)
569{
570 return 0;
571}
572
573static void serial_m3110_config_port(struct uart_port *port, int flags)
574{
575 port->type = PORT_MAX3100;
576}
577
578static int
579serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
580{
581 /* we don't want the core code to modify any port params */
582 return -EINVAL;
583}
584
585
586static const char *serial_m3110_type(struct uart_port *port)
587{
588 struct uart_max3110 *max =
589 container_of(port, struct uart_max3110, port);
590 return max->name;
591}
592
593static void
594serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
595 struct ktermios *old)
596{
597 struct uart_max3110 *max =
598 container_of(port, struct uart_max3110, port);
599 unsigned char cval;
600 unsigned int baud, parity = 0;
601 int clk_div = -1;
602 u16 new_conf = max->cur_conf;
603
604 switch (termios->c_cflag & CSIZE) {
605 case CS7:
606 cval = UART_LCR_WLEN7;
607 new_conf |= WC_7BIT_WORD;
608 break;
609 default:
610 /* We only support CS7 & CS8 */
611 termios->c_cflag &= ~CSIZE;
612 termios->c_cflag |= CS8;
613 case CS8:
614 cval = UART_LCR_WLEN8;
615 new_conf |= WC_8BIT_WORD;
616 break;
617 }
618
619 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
620
621 /* First calc the div for 1.8MHZ clock case */
622 switch (baud) {
623 case 300:
624 clk_div = WC_BAUD_DR384;
625 break;
626 case 600:
627 clk_div = WC_BAUD_DR192;
628 break;
629 case 1200:
630 clk_div = WC_BAUD_DR96;
631 break;
632 case 2400:
633 clk_div = WC_BAUD_DR48;
634 break;
635 case 4800:
636 clk_div = WC_BAUD_DR24;
637 break;
638 case 9600:
639 clk_div = WC_BAUD_DR12;
640 break;
641 case 19200:
642 clk_div = WC_BAUD_DR6;
643 break;
644 case 38400:
645 clk_div = WC_BAUD_DR3;
646 break;
647 case 57600:
648 clk_div = WC_BAUD_DR2;
649 break;
650 case 115200:
651 clk_div = WC_BAUD_DR1;
652 break;
653 case 230400:
654 if (max->clock & MAX3110_HIGH_CLK)
655 break;
656 default:
657 /* Pick the previous baud rate */
658 baud = max->baud;
659 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
660 tty_termios_encode_baud_rate(termios, baud, baud);
661 }
662
663 if (max->clock & MAX3110_HIGH_CLK) {
664 clk_div += 1;
665 /* High clk version max3110 doesn't support B300 */
666 if (baud == 300) {
667 baud = 600;
668 clk_div = WC_BAUD_DR384;
669 }
670 if (baud == 230400)
671 clk_div = WC_BAUD_DR1;
672 tty_termios_encode_baud_rate(termios, baud, baud);
673 }
674
675 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
676
677 if (unlikely(termios->c_cflag & CMSPAR))
678 termios->c_cflag &= ~CMSPAR;
679
680 if (termios->c_cflag & CSTOPB)
681 new_conf |= WC_2_STOPBITS;
682 else
683 new_conf &= ~WC_2_STOPBITS;
684
685 if (termios->c_cflag & PARENB) {
686 new_conf |= WC_PARITY_ENABLE;
687 parity |= UART_LCR_PARITY;
688 } else
689 new_conf &= ~WC_PARITY_ENABLE;
690
691 if (!(termios->c_cflag & PARODD))
692 parity |= UART_LCR_EPAR;
693 max->parity = parity;
694
695 uart_update_timeout(port, termios->c_cflag, baud);
696
697 new_conf |= WC_TAG;
698 if (new_conf != max->cur_conf) {
699 if (!max3110_out(max, new_conf)) {
700 max->cur_conf = new_conf;
701 max->baud = baud;
702 }
703 }
704}
705
706/* Don't handle hw handshaking */
707static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
708{
709 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
710}
711
712static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
713{
714}
715
716static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
717{
718}
719
720static void serial_m3110_pm(struct uart_port *port, unsigned int state,
721 unsigned int oldstate)
722{
723}
724
725static void serial_m3110_enable_ms(struct uart_port *port)
726{
727}
728
729struct uart_ops serial_m3110_ops = {
730 .tx_empty = serial_m3110_tx_empty,
731 .set_mctrl = serial_m3110_set_mctrl,
732 .get_mctrl = serial_m3110_get_mctrl,
733 .stop_tx = serial_m3110_stop_tx,
734 .start_tx = serial_m3110_start_tx,
735 .stop_rx = serial_m3110_stop_rx,
736 .enable_ms = serial_m3110_enable_ms,
737 .break_ctl = serial_m3110_break_ctl,
738 .startup = serial_m3110_startup,
739 .shutdown = serial_m3110_shutdown,
740 .set_termios = serial_m3110_set_termios,
741 .pm = serial_m3110_pm,
742 .type = serial_m3110_type,
743 .release_port = serial_m3110_release_port,
744 .request_port = serial_m3110_request_port,
745 .config_port = serial_m3110_config_port,
746 .verify_port = serial_m3110_verify_port,
747};
748
749static struct uart_driver serial_m3110_reg = {
750 .owner = THIS_MODULE,
751 .driver_name = "MRST serial",
752 .dev_name = "ttyS",
753 .major = TTY_MAJOR,
754 .minor = 64,
755 .nr = 1,
756 .cons = &serial_m3110_console,
757};
758
759#ifdef CONFIG_PM
760static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
761{
762 struct uart_max3110 *max = spi_get_drvdata(spi);
763
764 disable_irq(max->irq);
765 uart_suspend_port(&serial_m3110_reg, &max->port);
766 max3110_out(max, max->cur_conf | WC_SW_SHDI);
767 return 0;
768}
769
770static int serial_m3110_resume(struct spi_device *spi)
771{
772 struct uart_max3110 *max = spi_get_drvdata(spi);
773
774 max3110_out(max, max->cur_conf);
775 uart_resume_port(&serial_m3110_reg, &max->port);
776 enable_irq(max->irq);
777 return 0;
778}
779#else
780#define serial_m3110_suspend NULL
781#define serial_m3110_resume NULL
782#endif
783
784static int __devinit serial_m3110_probe(struct spi_device *spi)
785{
786 struct uart_max3110 *max;
787 void *buffer;
788 u16 res;
789 int ret = 0;
790
791 max = kzalloc(sizeof(*max), GFP_KERNEL);
792 if (!max)
793 return -ENOMEM;
794
795 /* Set spi info */
796 spi->bits_per_word = 16;
797 max->clock = MAX3110_HIGH_CLK;
798
799 spi_setup(spi);
800
801 max->port.type = PORT_MAX3100;
802 max->port.fifosize = 2; /* Only have 16b buffer */
803 max->port.ops = &serial_m3110_ops;
804 max->port.line = 0;
805 max->port.dev = &spi->dev;
806 max->port.uartclk = 115200;
807
808 max->spi = spi;
809 strcpy(max->name, spi->modalias);
810 max->irq = (u16)spi->irq;
811
812 mutex_init(&max->thread_mutex);
813
814 max->word_7bits = 0;
815 max->parity = 0;
816 max->baud = 0;
817
818 max->cur_conf = 0;
819 max->uart_flags = 0;
820
821 /* Check if reading configuration register returns something sane */
822
823 res = RC_TAG;
824 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
825 if (ret < 0 || res == 0 || res == 0xffff) {
826 printk(KERN_ERR "MAX3111 deemed not present (conf reg %04x)",
827 res);
828 ret = -ENODEV;
829 goto err_get_page;
830 }
831
832 buffer = (void *)__get_free_page(GFP_KERNEL);
833 if (!buffer) {
834 ret = -ENOMEM;
835 goto err_get_page;
836 }
837 max->con_xmit.buf = buffer;
838 max->con_xmit.head = 0;
839 max->con_xmit.tail = 0;
840
841 max->main_thread = kthread_run(max3110_main_thread,
842 max, "max3110_main");
843 if (IS_ERR(max->main_thread)) {
844 ret = PTR_ERR(max->main_thread);
845 goto err_kthread;
846 }
847
848 spi_set_drvdata(spi, max);
849 pmax = max;
850
851 /* Give membase a psudo value to pass serial_core's check */
852 max->port.membase = (void *)0xff110000;
853 uart_add_one_port(&serial_m3110_reg, &max->port);
854
855 return 0;
856
857err_kthread:
858 free_page((unsigned long)buffer);
859err_get_page:
860 kfree(max);
861 return ret;
862}
863
864static int __devexit serial_m3110_remove(struct spi_device *dev)
865{
866 struct uart_max3110 *max = spi_get_drvdata(dev);
867
868 if (!max)
869 return 0;
870
871 uart_remove_one_port(&serial_m3110_reg, &max->port);
872
873 free_page((unsigned long)max->con_xmit.buf);
874
875 if (max->main_thread)
876 kthread_stop(max->main_thread);
877
878 kfree(max);
879 return 0;
880}
881
882static struct spi_driver uart_max3110_driver = {
883 .driver = {
884 .name = "spi_max3111",
885 .bus = &spi_bus_type,
886 .owner = THIS_MODULE,
887 },
888 .probe = serial_m3110_probe,
889 .remove = __devexit_p(serial_m3110_remove),
890 .suspend = serial_m3110_suspend,
891 .resume = serial_m3110_resume,
892};
893
894static int __init serial_m3110_init(void)
895{
896 int ret = 0;
897
898 ret = uart_register_driver(&serial_m3110_reg);
899 if (ret)
900 return ret;
901
902 ret = spi_register_driver(&uart_max3110_driver);
903 if (ret)
904 uart_unregister_driver(&serial_m3110_reg);
905
906 return ret;
907}
908
909static void __exit serial_m3110_exit(void)
910{
911 spi_unregister_driver(&uart_max3110_driver);
912 uart_unregister_driver(&serial_m3110_reg);
913}
914
915module_init(serial_m3110_init);
916module_exit(serial_m3110_exit);
917
918MODULE_LICENSE("GPL v2");
919MODULE_ALIAS("max3110-uart");
diff --git a/drivers/tty/serial/mrst_max3110.h b/drivers/tty/serial/mrst_max3110.h
new file mode 100644
index 000000000000..d1ef43af397c
--- /dev/null
+++ b/drivers/tty/serial/mrst_max3110.h
@@ -0,0 +1,60 @@
1#ifndef _MRST_MAX3110_H
2#define _MRST_MAX3110_H
3
4#define MAX3110_HIGH_CLK 0x1 /* 3.6864 MHZ */
5#define MAX3110_LOW_CLK 0x0 /* 1.8432 MHZ */
6
7/* status bits for all 4 MAX3110 operate modes */
8#define MAX3110_READ_DATA_AVAILABLE (1 << 15)
9#define MAX3110_WRITE_BUF_EMPTY (1 << 14)
10
11#define WC_TAG (3 << 14)
12#define RC_TAG (1 << 14)
13#define WD_TAG (2 << 14)
14#define RD_TAG (0 << 14)
15
16/* bits def for write configuration */
17#define WC_FIFO_ENABLE_MASK (1 << 13)
18#define WC_FIFO_ENABLE (0 << 13)
19
20#define WC_SW_SHDI (1 << 12)
21
22#define WC_IRQ_MASK (0xF << 8)
23#define WC_TXE_IRQ_ENABLE (1 << 11) /* TX empty irq */
24#define WC_RXA_IRQ_ENABLE (1 << 10) /* RX availabe irq */
25#define WC_PAR_HIGH_IRQ_ENABLE (1 << 9)
26#define WC_REC_ACT_IRQ_ENABLE (1 << 8)
27
28#define WC_IRDA_ENABLE (1 << 7)
29
30#define WC_STOPBITS_MASK (1 << 6)
31#define WC_2_STOPBITS (1 << 6)
32#define WC_1_STOPBITS (0 << 6)
33
34#define WC_PARITY_ENABLE_MASK (1 << 5)
35#define WC_PARITY_ENABLE (1 << 5)
36
37#define WC_WORDLEN_MASK (1 << 4)
38#define WC_7BIT_WORD (1 << 4)
39#define WC_8BIT_WORD (0 << 4)
40
41#define WC_BAUD_DIV_MASK (0xF)
42#define WC_BAUD_DR1 (0x0)
43#define WC_BAUD_DR2 (0x1)
44#define WC_BAUD_DR4 (0x2)
45#define WC_BAUD_DR8 (0x3)
46#define WC_BAUD_DR16 (0x4)
47#define WC_BAUD_DR32 (0x5)
48#define WC_BAUD_DR64 (0x6)
49#define WC_BAUD_DR128 (0x7)
50#define WC_BAUD_DR3 (0x8)
51#define WC_BAUD_DR6 (0x9)
52#define WC_BAUD_DR12 (0xA)
53#define WC_BAUD_DR24 (0xB)
54#define WC_BAUD_DR48 (0xC)
55#define WC_BAUD_DR96 (0xD)
56#define WC_BAUD_DR192 (0xE)
57#define WC_BAUD_DR384 (0xF)
58
59#define M3110_RX_FIFO_DEPTH 8
60#endif
diff --git a/drivers/tty/serial/msm_serial.c b/drivers/tty/serial/msm_serial.c
new file mode 100644
index 000000000000..8e43a7b69e64
--- /dev/null
+++ b/drivers/tty/serial/msm_serial.c
@@ -0,0 +1,758 @@
1/*
2 * drivers/serial/msm_serial.c - driver for msm7k serial device and console
3 *
4 * Copyright (C) 2007 Google, Inc.
5 * Author: Robert Love <rlove@google.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#if defined(CONFIG_SERIAL_MSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18# define SUPPORT_SYSRQ
19#endif
20
21#include <linux/hrtimer.h>
22#include <linux/module.h>
23#include <linux/io.h>
24#include <linux/ioport.h>
25#include <linux/irq.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial_core.h>
31#include <linux/serial.h>
32#include <linux/clk.h>
33#include <linux/platform_device.h>
34
35#include "msm_serial.h"
36
37struct msm_port {
38 struct uart_port uart;
39 char name[16];
40 struct clk *clk;
41 unsigned int imr;
42};
43
44static void msm_stop_tx(struct uart_port *port)
45{
46 struct msm_port *msm_port = UART_TO_MSM(port);
47
48 msm_port->imr &= ~UART_IMR_TXLEV;
49 msm_write(port, msm_port->imr, UART_IMR);
50}
51
52static void msm_start_tx(struct uart_port *port)
53{
54 struct msm_port *msm_port = UART_TO_MSM(port);
55
56 msm_port->imr |= UART_IMR_TXLEV;
57 msm_write(port, msm_port->imr, UART_IMR);
58}
59
60static void msm_stop_rx(struct uart_port *port)
61{
62 struct msm_port *msm_port = UART_TO_MSM(port);
63
64 msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
65 msm_write(port, msm_port->imr, UART_IMR);
66}
67
68static void msm_enable_ms(struct uart_port *port)
69{
70 struct msm_port *msm_port = UART_TO_MSM(port);
71
72 msm_port->imr |= UART_IMR_DELTA_CTS;
73 msm_write(port, msm_port->imr, UART_IMR);
74}
75
76static void handle_rx(struct uart_port *port)
77{
78 struct tty_struct *tty = port->state->port.tty;
79 unsigned int sr;
80
81 /*
82 * Handle overrun. My understanding of the hardware is that overrun
83 * is not tied to the RX buffer, so we handle the case out of band.
84 */
85 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
86 port->icount.overrun++;
87 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
88 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
89 }
90
91 /* and now the main RX loop */
92 while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) {
93 unsigned int c;
94 char flag = TTY_NORMAL;
95
96 c = msm_read(port, UART_RF);
97
98 if (sr & UART_SR_RX_BREAK) {
99 port->icount.brk++;
100 if (uart_handle_break(port))
101 continue;
102 } else if (sr & UART_SR_PAR_FRAME_ERR) {
103 port->icount.frame++;
104 } else {
105 port->icount.rx++;
106 }
107
108 /* Mask conditions we're ignorning. */
109 sr &= port->read_status_mask;
110
111 if (sr & UART_SR_RX_BREAK) {
112 flag = TTY_BREAK;
113 } else if (sr & UART_SR_PAR_FRAME_ERR) {
114 flag = TTY_FRAME;
115 }
116
117 if (!uart_handle_sysrq_char(port, c))
118 tty_insert_flip_char(tty, c, flag);
119 }
120
121 tty_flip_buffer_push(tty);
122}
123
124static void handle_tx(struct uart_port *port)
125{
126 struct circ_buf *xmit = &port->state->xmit;
127 struct msm_port *msm_port = UART_TO_MSM(port);
128 int sent_tx;
129
130 if (port->x_char) {
131 msm_write(port, port->x_char, UART_TF);
132 port->icount.tx++;
133 port->x_char = 0;
134 }
135
136 while (msm_read(port, UART_SR) & UART_SR_TX_READY) {
137 if (uart_circ_empty(xmit)) {
138 /* disable tx interrupts */
139 msm_port->imr &= ~UART_IMR_TXLEV;
140 msm_write(port, msm_port->imr, UART_IMR);
141 break;
142 }
143
144 msm_write(port, xmit->buf[xmit->tail], UART_TF);
145
146 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
147 port->icount.tx++;
148 sent_tx = 1;
149 }
150
151 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
152 uart_write_wakeup(port);
153}
154
155static void handle_delta_cts(struct uart_port *port)
156{
157 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
158 port->icount.cts++;
159 wake_up_interruptible(&port->state->port.delta_msr_wait);
160}
161
162static irqreturn_t msm_irq(int irq, void *dev_id)
163{
164 struct uart_port *port = dev_id;
165 struct msm_port *msm_port = UART_TO_MSM(port);
166 unsigned int misr;
167
168 spin_lock(&port->lock);
169 misr = msm_read(port, UART_MISR);
170 msm_write(port, 0, UART_IMR); /* disable interrupt */
171
172 if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE))
173 handle_rx(port);
174 if (misr & UART_IMR_TXLEV)
175 handle_tx(port);
176 if (misr & UART_IMR_DELTA_CTS)
177 handle_delta_cts(port);
178
179 msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
180 spin_unlock(&port->lock);
181
182 return IRQ_HANDLED;
183}
184
185static unsigned int msm_tx_empty(struct uart_port *port)
186{
187 return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
188}
189
190static unsigned int msm_get_mctrl(struct uart_port *port)
191{
192 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
193}
194
195static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
196{
197 unsigned int mr;
198
199 mr = msm_read(port, UART_MR1);
200
201 if (!(mctrl & TIOCM_RTS)) {
202 mr &= ~UART_MR1_RX_RDY_CTL;
203 msm_write(port, mr, UART_MR1);
204 msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR);
205 } else {
206 mr |= UART_MR1_RX_RDY_CTL;
207 msm_write(port, mr, UART_MR1);
208 }
209}
210
211static void msm_break_ctl(struct uart_port *port, int break_ctl)
212{
213 if (break_ctl)
214 msm_write(port, UART_CR_CMD_START_BREAK, UART_CR);
215 else
216 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
217}
218
219static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
220{
221 unsigned int baud_code, rxstale, watermark;
222
223 switch (baud) {
224 case 300:
225 baud_code = UART_CSR_300;
226 rxstale = 1;
227 break;
228 case 600:
229 baud_code = UART_CSR_600;
230 rxstale = 1;
231 break;
232 case 1200:
233 baud_code = UART_CSR_1200;
234 rxstale = 1;
235 break;
236 case 2400:
237 baud_code = UART_CSR_2400;
238 rxstale = 1;
239 break;
240 case 4800:
241 baud_code = UART_CSR_4800;
242 rxstale = 1;
243 break;
244 case 9600:
245 baud_code = UART_CSR_9600;
246 rxstale = 2;
247 break;
248 case 14400:
249 baud_code = UART_CSR_14400;
250 rxstale = 3;
251 break;
252 case 19200:
253 baud_code = UART_CSR_19200;
254 rxstale = 4;
255 break;
256 case 28800:
257 baud_code = UART_CSR_28800;
258 rxstale = 6;
259 break;
260 case 38400:
261 baud_code = UART_CSR_38400;
262 rxstale = 8;
263 break;
264 case 57600:
265 baud_code = UART_CSR_57600;
266 rxstale = 16;
267 break;
268 case 115200:
269 default:
270 baud_code = UART_CSR_115200;
271 baud = 115200;
272 rxstale = 31;
273 break;
274 }
275
276 msm_write(port, baud_code, UART_CSR);
277
278 /* RX stale watermark */
279 watermark = UART_IPR_STALE_LSB & rxstale;
280 watermark |= UART_IPR_RXSTALE_LAST;
281 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
282 msm_write(port, watermark, UART_IPR);
283
284 /* set RX watermark */
285 watermark = (port->fifosize * 3) / 4;
286 msm_write(port, watermark, UART_RFWR);
287
288 /* set TX watermark */
289 msm_write(port, 10, UART_TFWR);
290
291 return baud;
292}
293
294static void msm_reset(struct uart_port *port)
295{
296 /* reset everything */
297 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
298 msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
299 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
300 msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
301 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
302 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
303}
304
305static void msm_init_clock(struct uart_port *port)
306{
307 struct msm_port *msm_port = UART_TO_MSM(port);
308
309 clk_enable(msm_port->clk);
310 msm_serial_set_mnd_regs(port);
311}
312
313static int msm_startup(struct uart_port *port)
314{
315 struct msm_port *msm_port = UART_TO_MSM(port);
316 unsigned int data, rfr_level;
317 int ret;
318
319 snprintf(msm_port->name, sizeof(msm_port->name),
320 "msm_serial%d", port->line);
321
322 ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
323 msm_port->name, port);
324 if (unlikely(ret))
325 return ret;
326
327 msm_init_clock(port);
328
329 if (likely(port->fifosize > 12))
330 rfr_level = port->fifosize - 12;
331 else
332 rfr_level = port->fifosize;
333
334 /* set automatic RFR level */
335 data = msm_read(port, UART_MR1);
336 data &= ~UART_MR1_AUTO_RFR_LEVEL1;
337 data &= ~UART_MR1_AUTO_RFR_LEVEL0;
338 data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
339 data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
340 msm_write(port, data, UART_MR1);
341
342 /* make sure that RXSTALE count is non-zero */
343 data = msm_read(port, UART_IPR);
344 if (unlikely(!data)) {
345 data |= UART_IPR_RXSTALE_LAST;
346 data |= UART_IPR_STALE_LSB;
347 msm_write(port, data, UART_IPR);
348 }
349
350 msm_reset(port);
351
352 msm_write(port, 0x05, UART_CR); /* enable TX & RX */
353
354 /* turn on RX and CTS interrupts */
355 msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
356 UART_IMR_CURRENT_CTS;
357 msm_write(port, msm_port->imr, UART_IMR);
358
359 return 0;
360}
361
362static void msm_shutdown(struct uart_port *port)
363{
364 struct msm_port *msm_port = UART_TO_MSM(port);
365
366 msm_port->imr = 0;
367 msm_write(port, 0, UART_IMR); /* disable interrupts */
368
369 clk_disable(msm_port->clk);
370
371 free_irq(port->irq, port);
372}
373
374static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
375 struct ktermios *old)
376{
377 unsigned long flags;
378 unsigned int baud, mr;
379
380 spin_lock_irqsave(&port->lock, flags);
381
382 /* calculate and set baud rate */
383 baud = uart_get_baud_rate(port, termios, old, 300, 115200);
384 baud = msm_set_baud_rate(port, baud);
385 if (tty_termios_baud_rate(termios))
386 tty_termios_encode_baud_rate(termios, baud, baud);
387
388 /* calculate parity */
389 mr = msm_read(port, UART_MR2);
390 mr &= ~UART_MR2_PARITY_MODE;
391 if (termios->c_cflag & PARENB) {
392 if (termios->c_cflag & PARODD)
393 mr |= UART_MR2_PARITY_MODE_ODD;
394 else if (termios->c_cflag & CMSPAR)
395 mr |= UART_MR2_PARITY_MODE_SPACE;
396 else
397 mr |= UART_MR2_PARITY_MODE_EVEN;
398 }
399
400 /* calculate bits per char */
401 mr &= ~UART_MR2_BITS_PER_CHAR;
402 switch (termios->c_cflag & CSIZE) {
403 case CS5:
404 mr |= UART_MR2_BITS_PER_CHAR_5;
405 break;
406 case CS6:
407 mr |= UART_MR2_BITS_PER_CHAR_6;
408 break;
409 case CS7:
410 mr |= UART_MR2_BITS_PER_CHAR_7;
411 break;
412 case CS8:
413 default:
414 mr |= UART_MR2_BITS_PER_CHAR_8;
415 break;
416 }
417
418 /* calculate stop bits */
419 mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO);
420 if (termios->c_cflag & CSTOPB)
421 mr |= UART_MR2_STOP_BIT_LEN_TWO;
422 else
423 mr |= UART_MR2_STOP_BIT_LEN_ONE;
424
425 /* set parity, bits per char, and stop bit */
426 msm_write(port, mr, UART_MR2);
427
428 /* calculate and set hardware flow control */
429 mr = msm_read(port, UART_MR1);
430 mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL);
431 if (termios->c_cflag & CRTSCTS) {
432 mr |= UART_MR1_CTS_CTL;
433 mr |= UART_MR1_RX_RDY_CTL;
434 }
435 msm_write(port, mr, UART_MR1);
436
437 /* Configure status bits to ignore based on termio flags. */
438 port->read_status_mask = 0;
439 if (termios->c_iflag & INPCK)
440 port->read_status_mask |= UART_SR_PAR_FRAME_ERR;
441 if (termios->c_iflag & (BRKINT | PARMRK))
442 port->read_status_mask |= UART_SR_RX_BREAK;
443
444 uart_update_timeout(port, termios->c_cflag, baud);
445
446 spin_unlock_irqrestore(&port->lock, flags);
447}
448
449static const char *msm_type(struct uart_port *port)
450{
451 return "MSM";
452}
453
454static void msm_release_port(struct uart_port *port)
455{
456 struct platform_device *pdev = to_platform_device(port->dev);
457 struct resource *resource;
458 resource_size_t size;
459
460 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
461 if (unlikely(!resource))
462 return;
463 size = resource->end - resource->start + 1;
464
465 release_mem_region(port->mapbase, size);
466 iounmap(port->membase);
467 port->membase = NULL;
468}
469
470static int msm_request_port(struct uart_port *port)
471{
472 struct platform_device *pdev = to_platform_device(port->dev);
473 struct resource *resource;
474 resource_size_t size;
475
476 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
477 if (unlikely(!resource))
478 return -ENXIO;
479 size = resource->end - resource->start + 1;
480
481 if (unlikely(!request_mem_region(port->mapbase, size, "msm_serial")))
482 return -EBUSY;
483
484 port->membase = ioremap(port->mapbase, size);
485 if (!port->membase) {
486 release_mem_region(port->mapbase, size);
487 return -EBUSY;
488 }
489
490 return 0;
491}
492
493static void msm_config_port(struct uart_port *port, int flags)
494{
495 if (flags & UART_CONFIG_TYPE) {
496 port->type = PORT_MSM;
497 msm_request_port(port);
498 }
499}
500
501static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
502{
503 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
504 return -EINVAL;
505 if (unlikely(port->irq != ser->irq))
506 return -EINVAL;
507 return 0;
508}
509
510static void msm_power(struct uart_port *port, unsigned int state,
511 unsigned int oldstate)
512{
513 struct msm_port *msm_port = UART_TO_MSM(port);
514
515 switch (state) {
516 case 0:
517 clk_enable(msm_port->clk);
518 break;
519 case 3:
520 clk_disable(msm_port->clk);
521 break;
522 default:
523 printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
524 }
525}
526
527static struct uart_ops msm_uart_pops = {
528 .tx_empty = msm_tx_empty,
529 .set_mctrl = msm_set_mctrl,
530 .get_mctrl = msm_get_mctrl,
531 .stop_tx = msm_stop_tx,
532 .start_tx = msm_start_tx,
533 .stop_rx = msm_stop_rx,
534 .enable_ms = msm_enable_ms,
535 .break_ctl = msm_break_ctl,
536 .startup = msm_startup,
537 .shutdown = msm_shutdown,
538 .set_termios = msm_set_termios,
539 .type = msm_type,
540 .release_port = msm_release_port,
541 .request_port = msm_request_port,
542 .config_port = msm_config_port,
543 .verify_port = msm_verify_port,
544 .pm = msm_power,
545};
546
547static struct msm_port msm_uart_ports[] = {
548 {
549 .uart = {
550 .iotype = UPIO_MEM,
551 .ops = &msm_uart_pops,
552 .flags = UPF_BOOT_AUTOCONF,
553 .fifosize = 512,
554 .line = 0,
555 },
556 },
557 {
558 .uart = {
559 .iotype = UPIO_MEM,
560 .ops = &msm_uart_pops,
561 .flags = UPF_BOOT_AUTOCONF,
562 .fifosize = 512,
563 .line = 1,
564 },
565 },
566 {
567 .uart = {
568 .iotype = UPIO_MEM,
569 .ops = &msm_uart_pops,
570 .flags = UPF_BOOT_AUTOCONF,
571 .fifosize = 64,
572 .line = 2,
573 },
574 },
575};
576
577#define UART_NR ARRAY_SIZE(msm_uart_ports)
578
579static inline struct uart_port *get_port_from_line(unsigned int line)
580{
581 return &msm_uart_ports[line].uart;
582}
583
584#ifdef CONFIG_SERIAL_MSM_CONSOLE
585
586static void msm_console_putchar(struct uart_port *port, int c)
587{
588 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
589 ;
590 msm_write(port, c, UART_TF);
591}
592
593static void msm_console_write(struct console *co, const char *s,
594 unsigned int count)
595{
596 struct uart_port *port;
597 struct msm_port *msm_port;
598
599 BUG_ON(co->index < 0 || co->index >= UART_NR);
600
601 port = get_port_from_line(co->index);
602 msm_port = UART_TO_MSM(port);
603
604 spin_lock(&port->lock);
605 uart_console_write(port, s, count, msm_console_putchar);
606 spin_unlock(&port->lock);
607}
608
609static int __init msm_console_setup(struct console *co, char *options)
610{
611 struct uart_port *port;
612 int baud, flow, bits, parity;
613
614 if (unlikely(co->index >= UART_NR || co->index < 0))
615 return -ENXIO;
616
617 port = get_port_from_line(co->index);
618
619 if (unlikely(!port->membase))
620 return -ENXIO;
621
622 port->cons = co;
623
624 msm_init_clock(port);
625
626 if (options)
627 uart_parse_options(options, &baud, &parity, &bits, &flow);
628
629 bits = 8;
630 parity = 'n';
631 flow = 'n';
632 msm_write(port, UART_MR2_BITS_PER_CHAR_8 | UART_MR2_STOP_BIT_LEN_ONE,
633 UART_MR2); /* 8N1 */
634
635 if (baud < 300 || baud > 115200)
636 baud = 115200;
637 msm_set_baud_rate(port, baud);
638
639 msm_reset(port);
640
641 printk(KERN_INFO "msm_serial: console setup on port #%d\n", port->line);
642
643 return uart_set_options(port, co, baud, parity, bits, flow);
644}
645
646static struct uart_driver msm_uart_driver;
647
648static struct console msm_console = {
649 .name = "ttyMSM",
650 .write = msm_console_write,
651 .device = uart_console_device,
652 .setup = msm_console_setup,
653 .flags = CON_PRINTBUFFER,
654 .index = -1,
655 .data = &msm_uart_driver,
656};
657
658#define MSM_CONSOLE (&msm_console)
659
660#else
661#define MSM_CONSOLE NULL
662#endif
663
664static struct uart_driver msm_uart_driver = {
665 .owner = THIS_MODULE,
666 .driver_name = "msm_serial",
667 .dev_name = "ttyMSM",
668 .nr = UART_NR,
669 .cons = MSM_CONSOLE,
670};
671
672static int __init msm_serial_probe(struct platform_device *pdev)
673{
674 struct msm_port *msm_port;
675 struct resource *resource;
676 struct uart_port *port;
677 int irq;
678
679 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
680 return -ENXIO;
681
682 printk(KERN_INFO "msm_serial: detected port #%d\n", pdev->id);
683
684 port = get_port_from_line(pdev->id);
685 port->dev = &pdev->dev;
686 msm_port = UART_TO_MSM(port);
687
688 msm_port->clk = clk_get(&pdev->dev, "uart_clk");
689 if (IS_ERR(msm_port->clk))
690 return PTR_ERR(msm_port->clk);
691 port->uartclk = clk_get_rate(msm_port->clk);
692 printk(KERN_INFO "uartclk = %d\n", port->uartclk);
693
694
695 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
696 if (unlikely(!resource))
697 return -ENXIO;
698 port->mapbase = resource->start;
699
700 irq = platform_get_irq(pdev, 0);
701 if (unlikely(irq < 0))
702 return -ENXIO;
703 port->irq = irq;
704
705 platform_set_drvdata(pdev, port);
706
707 return uart_add_one_port(&msm_uart_driver, port);
708}
709
710static int __devexit msm_serial_remove(struct platform_device *pdev)
711{
712 struct msm_port *msm_port = platform_get_drvdata(pdev);
713
714 clk_put(msm_port->clk);
715
716 return 0;
717}
718
719static struct platform_driver msm_platform_driver = {
720 .remove = msm_serial_remove,
721 .driver = {
722 .name = "msm_serial",
723 .owner = THIS_MODULE,
724 },
725};
726
727static int __init msm_serial_init(void)
728{
729 int ret;
730
731 ret = uart_register_driver(&msm_uart_driver);
732 if (unlikely(ret))
733 return ret;
734
735 ret = platform_driver_probe(&msm_platform_driver, msm_serial_probe);
736 if (unlikely(ret))
737 uart_unregister_driver(&msm_uart_driver);
738
739 printk(KERN_INFO "msm_serial: driver initialized\n");
740
741 return ret;
742}
743
744static void __exit msm_serial_exit(void)
745{
746#ifdef CONFIG_SERIAL_MSM_CONSOLE
747 unregister_console(&msm_console);
748#endif
749 platform_driver_unregister(&msm_platform_driver);
750 uart_unregister_driver(&msm_uart_driver);
751}
752
753module_init(msm_serial_init);
754module_exit(msm_serial_exit);
755
756MODULE_AUTHOR("Robert Love <rlove@google.com>");
757MODULE_DESCRIPTION("Driver for msm7x serial device");
758MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/msm_serial.h b/drivers/tty/serial/msm_serial.h
new file mode 100644
index 000000000000..f6ca9ca79e98
--- /dev/null
+++ b/drivers/tty/serial/msm_serial.h
@@ -0,0 +1,173 @@
1/*
2 * drivers/serial/msm_serial.h
3 *
4 * Copyright (C) 2007 Google, Inc.
5 * Author: Robert Love <rlove@google.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#ifndef __DRIVERS_SERIAL_MSM_SERIAL_H
18#define __DRIVERS_SERIAL_MSM_SERIAL_H
19
20#define UART_MR1 0x0000
21
22#define UART_MR1_AUTO_RFR_LEVEL0 0x3F
23#define UART_MR1_AUTO_RFR_LEVEL1 0x3FF00
24#define UART_MR1_RX_RDY_CTL (1 << 7)
25#define UART_MR1_CTS_CTL (1 << 6)
26
27#define UART_MR2 0x0004
28#define UART_MR2_ERROR_MODE (1 << 6)
29#define UART_MR2_BITS_PER_CHAR 0x30
30#define UART_MR2_BITS_PER_CHAR_5 (0x0 << 4)
31#define UART_MR2_BITS_PER_CHAR_6 (0x1 << 4)
32#define UART_MR2_BITS_PER_CHAR_7 (0x2 << 4)
33#define UART_MR2_BITS_PER_CHAR_8 (0x3 << 4)
34#define UART_MR2_STOP_BIT_LEN_ONE (0x1 << 2)
35#define UART_MR2_STOP_BIT_LEN_TWO (0x3 << 2)
36#define UART_MR2_PARITY_MODE_NONE 0x0
37#define UART_MR2_PARITY_MODE_ODD 0x1
38#define UART_MR2_PARITY_MODE_EVEN 0x2
39#define UART_MR2_PARITY_MODE_SPACE 0x3
40#define UART_MR2_PARITY_MODE 0x3
41
42#define UART_CSR 0x0008
43#define UART_CSR_115200 0xFF
44#define UART_CSR_57600 0xEE
45#define UART_CSR_38400 0xDD
46#define UART_CSR_28800 0xCC
47#define UART_CSR_19200 0xBB
48#define UART_CSR_14400 0xAA
49#define UART_CSR_9600 0x99
50#define UART_CSR_4800 0x77
51#define UART_CSR_2400 0x55
52#define UART_CSR_1200 0x44
53#define UART_CSR_600 0x33
54#define UART_CSR_300 0x22
55
56#define UART_TF 0x000C
57
58#define UART_CR 0x0010
59#define UART_CR_CMD_NULL (0 << 4)
60#define UART_CR_CMD_RESET_RX (1 << 4)
61#define UART_CR_CMD_RESET_TX (2 << 4)
62#define UART_CR_CMD_RESET_ERR (3 << 4)
63#define UART_CR_CMD_RESET_BREAK_INT (4 << 4)
64#define UART_CR_CMD_START_BREAK (5 << 4)
65#define UART_CR_CMD_STOP_BREAK (6 << 4)
66#define UART_CR_CMD_RESET_CTS (7 << 4)
67#define UART_CR_CMD_PACKET_MODE (9 << 4)
68#define UART_CR_CMD_MODE_RESET (12 << 4)
69#define UART_CR_CMD_SET_RFR (13 << 4)
70#define UART_CR_CMD_RESET_RFR (14 << 4)
71#define UART_CR_TX_DISABLE (1 << 3)
72#define UART_CR_TX_ENABLE (1 << 3)
73#define UART_CR_RX_DISABLE (1 << 3)
74#define UART_CR_RX_ENABLE (1 << 3)
75
76#define UART_IMR 0x0014
77#define UART_IMR_TXLEV (1 << 0)
78#define UART_IMR_RXSTALE (1 << 3)
79#define UART_IMR_RXLEV (1 << 4)
80#define UART_IMR_DELTA_CTS (1 << 5)
81#define UART_IMR_CURRENT_CTS (1 << 6)
82
83#define UART_IPR_RXSTALE_LAST 0x20
84#define UART_IPR_STALE_LSB 0x1F
85#define UART_IPR_STALE_TIMEOUT_MSB 0x3FF80
86
87#define UART_IPR 0x0018
88#define UART_TFWR 0x001C
89#define UART_RFWR 0x0020
90#define UART_HCR 0x0024
91
92#define UART_MREG 0x0028
93#define UART_NREG 0x002C
94#define UART_DREG 0x0030
95#define UART_MNDREG 0x0034
96#define UART_IRDA 0x0038
97#define UART_MISR_MODE 0x0040
98#define UART_MISR_RESET 0x0044
99#define UART_MISR_EXPORT 0x0048
100#define UART_MISR_VAL 0x004C
101#define UART_TEST_CTRL 0x0050
102
103#define UART_SR 0x0008
104#define UART_SR_HUNT_CHAR (1 << 7)
105#define UART_SR_RX_BREAK (1 << 6)
106#define UART_SR_PAR_FRAME_ERR (1 << 5)
107#define UART_SR_OVERRUN (1 << 4)
108#define UART_SR_TX_EMPTY (1 << 3)
109#define UART_SR_TX_READY (1 << 2)
110#define UART_SR_RX_FULL (1 << 1)
111#define UART_SR_RX_READY (1 << 0)
112
113#define UART_RF 0x000C
114#define UART_MISR 0x0010
115#define UART_ISR 0x0014
116
117#define UART_TO_MSM(uart_port) ((struct msm_port *) uart_port)
118
119static inline
120void msm_write(struct uart_port *port, unsigned int val, unsigned int off)
121{
122 __raw_writel(val, port->membase + off);
123}
124
125static inline
126unsigned int msm_read(struct uart_port *port, unsigned int off)
127{
128 return __raw_readl(port->membase + off);
129}
130
131/*
132 * Setup the MND registers to use the TCXO clock.
133 */
134static inline void msm_serial_set_mnd_regs_tcxo(struct uart_port *port)
135{
136 msm_write(port, 0x06, UART_MREG);
137 msm_write(port, 0xF1, UART_NREG);
138 msm_write(port, 0x0F, UART_DREG);
139 msm_write(port, 0x1A, UART_MNDREG);
140}
141
142/*
143 * Setup the MND registers to use the TCXO clock divided by 4.
144 */
145static inline void msm_serial_set_mnd_regs_tcxoby4(struct uart_port *port)
146{
147 msm_write(port, 0x18, UART_MREG);
148 msm_write(port, 0xF6, UART_NREG);
149 msm_write(port, 0x0F, UART_DREG);
150 msm_write(port, 0x0A, UART_MNDREG);
151}
152
153static inline
154void msm_serial_set_mnd_regs_from_uartclk(struct uart_port *port)
155{
156 if (port->uartclk == 19200000)
157 msm_serial_set_mnd_regs_tcxo(port);
158 else
159 msm_serial_set_mnd_regs_tcxoby4(port);
160}
161
162/*
163 * TROUT has a specific defect that makes it report it's uartclk
164 * as 19.2Mhz (TCXO) when it's actually 4.8Mhz (TCXO/4). This special
165 * cases TROUT to use the right clock.
166 */
167#ifdef CONFIG_MACH_TROUT
168#define msm_serial_set_mnd_regs msm_serial_set_mnd_regs_tcxoby4
169#else
170#define msm_serial_set_mnd_regs msm_serial_set_mnd_regs_from_uartclk
171#endif
172
173#endif /* __DRIVERS_SERIAL_MSM_SERIAL_H */
diff --git a/drivers/tty/serial/mux.c b/drivers/tty/serial/mux.c
new file mode 100644
index 000000000000..9711e06a8374
--- /dev/null
+++ b/drivers/tty/serial/mux.c
@@ -0,0 +1,633 @@
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/module.h>
20#include <linux/tty.h>
21#include <linux/ioport.h>
22#include <linux/init.h>
23#include <linux/serial.h>
24#include <linux/console.h>
25#include <linux/delay.h> /* for udelay */
26#include <linux/device.h>
27#include <asm/io.h>
28#include <asm/irq.h>
29#include <asm/parisc-device.h>
30
31#ifdef CONFIG_MAGIC_SYSRQ
32#include <linux/sysrq.h>
33#define SUPPORT_SYSRQ
34#endif
35
36#include <linux/serial_core.h>
37
38#define MUX_OFFSET 0x800
39#define MUX_LINE_OFFSET 0x80
40
41#define MUX_FIFO_SIZE 255
42#define MUX_POLL_DELAY (30 * HZ / 1000)
43
44#define IO_DATA_REG_OFFSET 0x3c
45#define IO_DCOUNT_REG_OFFSET 0x40
46
47#define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000)
48#define MUX_STATUS(status) ((status & 0xF000) == 0x8000)
49#define MUX_BREAK(status) ((status & 0xF000) == 0x2000)
50
51#define MUX_NR 256
52static unsigned int port_cnt __read_mostly;
53struct mux_port {
54 struct uart_port port;
55 int enabled;
56};
57static struct mux_port mux_ports[MUX_NR];
58
59static struct uart_driver mux_driver = {
60 .owner = THIS_MODULE,
61 .driver_name = "ttyB",
62 .dev_name = "ttyB",
63 .major = MUX_MAJOR,
64 .minor = 0,
65 .nr = MUX_NR,
66};
67
68static struct timer_list mux_timer;
69
70#define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + IO_DATA_REG_OFFSET)
71#define UART_GET_FIFO_CNT(p) __raw_readl((p)->membase + IO_DCOUNT_REG_OFFSET)
72
73/**
74 * get_mux_port_count - Get the number of available ports on the Mux.
75 * @dev: The parisc device.
76 *
77 * This function is used to determine the number of ports the Mux
78 * supports. The IODC data reports the number of ports the Mux
79 * can support, but there are cases where not all the Mux ports
80 * are connected. This function can override the IODC and
81 * return the true port count.
82 */
83static int __init get_mux_port_count(struct parisc_device *dev)
84{
85 int status;
86 u8 iodc_data[32];
87 unsigned long bytecnt;
88
89 /* If this is the built-in Mux for the K-Class (Eole CAP/MUX),
90 * we only need to allocate resources for 1 port since the
91 * other 7 ports are not connected.
92 */
93 if(dev->id.hversion == 0x15)
94 return 1;
95
96 status = pdc_iodc_read(&bytecnt, dev->hpa.start, 0, iodc_data, 32);
97 BUG_ON(status != PDC_OK);
98
99 /* Return the number of ports specified in the iodc data. */
100 return ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8;
101}
102
103/**
104 * mux_tx_empty - Check if the transmitter fifo is empty.
105 * @port: Ptr to the uart_port.
106 *
107 * This function test if the transmitter fifo for the port
108 * described by 'port' is empty. If it is empty, this function
109 * should return TIOCSER_TEMT, otherwise return 0.
110 */
111static unsigned int mux_tx_empty(struct uart_port *port)
112{
113 return UART_GET_FIFO_CNT(port) ? 0 : TIOCSER_TEMT;
114}
115
116/**
117 * mux_set_mctrl - Set the current state of the modem control inputs.
118 * @ports: Ptr to the uart_port.
119 * @mctrl: Modem control bits.
120 *
121 * The Serial MUX does not support CTS, DCD or DSR so this function
122 * is ignored.
123 */
124static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
125{
126}
127
128/**
129 * mux_get_mctrl - Returns the current state of modem control inputs.
130 * @port: Ptr to the uart_port.
131 *
132 * The Serial MUX does not support CTS, DCD or DSR so these lines are
133 * treated as permanently active.
134 */
135static unsigned int mux_get_mctrl(struct uart_port *port)
136{
137 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
138}
139
140/**
141 * mux_stop_tx - Stop transmitting characters.
142 * @port: Ptr to the uart_port.
143 *
144 * The Serial MUX does not support this function.
145 */
146static void mux_stop_tx(struct uart_port *port)
147{
148}
149
150/**
151 * mux_start_tx - Start transmitting characters.
152 * @port: Ptr to the uart_port.
153 *
154 * The Serial Mux does not support this function.
155 */
156static void mux_start_tx(struct uart_port *port)
157{
158}
159
160/**
161 * mux_stop_rx - Stop receiving characters.
162 * @port: Ptr to the uart_port.
163 *
164 * The Serial Mux does not support this function.
165 */
166static void mux_stop_rx(struct uart_port *port)
167{
168}
169
170/**
171 * mux_enable_ms - Enable modum status interrupts.
172 * @port: Ptr to the uart_port.
173 *
174 * The Serial Mux does not support this function.
175 */
176static void mux_enable_ms(struct uart_port *port)
177{
178}
179
180/**
181 * mux_break_ctl - Control the transmitssion of a break signal.
182 * @port: Ptr to the uart_port.
183 * @break_state: Raise/Lower the break signal.
184 *
185 * The Serial Mux does not support this function.
186 */
187static void mux_break_ctl(struct uart_port *port, int break_state)
188{
189}
190
191/**
192 * mux_write - Write chars to the mux fifo.
193 * @port: Ptr to the uart_port.
194 *
195 * This function writes all the data from the uart buffer to
196 * the mux fifo.
197 */
198static void mux_write(struct uart_port *port)
199{
200 int count;
201 struct circ_buf *xmit = &port->state->xmit;
202
203 if(port->x_char) {
204 UART_PUT_CHAR(port, port->x_char);
205 port->icount.tx++;
206 port->x_char = 0;
207 return;
208 }
209
210 if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
211 mux_stop_tx(port);
212 return;
213 }
214
215 count = (port->fifosize) - UART_GET_FIFO_CNT(port);
216 do {
217 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
218 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
219 port->icount.tx++;
220 if(uart_circ_empty(xmit))
221 break;
222
223 } while(--count > 0);
224
225 while(UART_GET_FIFO_CNT(port))
226 udelay(1);
227
228 if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
229 uart_write_wakeup(port);
230
231 if (uart_circ_empty(xmit))
232 mux_stop_tx(port);
233}
234
235/**
236 * mux_read - Read chars from the mux fifo.
237 * @port: Ptr to the uart_port.
238 *
239 * This reads all available data from the mux's fifo and pushes
240 * the data to the tty layer.
241 */
242static void mux_read(struct uart_port *port)
243{
244 int data;
245 struct tty_struct *tty = port->state->port.tty;
246 __u32 start_count = port->icount.rx;
247
248 while(1) {
249 data = __raw_readl(port->membase + IO_DATA_REG_OFFSET);
250
251 if (MUX_STATUS(data))
252 continue;
253
254 if (MUX_EOFIFO(data))
255 break;
256
257 port->icount.rx++;
258
259 if (MUX_BREAK(data)) {
260 port->icount.brk++;
261 if(uart_handle_break(port))
262 continue;
263 }
264
265 if (uart_handle_sysrq_char(port, data & 0xffu))
266 continue;
267
268 tty_insert_flip_char(tty, data & 0xFF, TTY_NORMAL);
269 }
270
271 if (start_count != port->icount.rx) {
272 tty_flip_buffer_push(tty);
273 }
274}
275
276/**
277 * mux_startup - Initialize the port.
278 * @port: Ptr to the uart_port.
279 *
280 * Grab any resources needed for this port and start the
281 * mux timer.
282 */
283static int mux_startup(struct uart_port *port)
284{
285 mux_ports[port->line].enabled = 1;
286 return 0;
287}
288
289/**
290 * mux_shutdown - Disable the port.
291 * @port: Ptr to the uart_port.
292 *
293 * Release any resources needed for the port.
294 */
295static void mux_shutdown(struct uart_port *port)
296{
297 mux_ports[port->line].enabled = 0;
298}
299
300/**
301 * mux_set_termios - Chane port parameters.
302 * @port: Ptr to the uart_port.
303 * @termios: new termios settings.
304 * @old: old termios settings.
305 *
306 * The Serial Mux does not support this function.
307 */
308static void
309mux_set_termios(struct uart_port *port, struct ktermios *termios,
310 struct ktermios *old)
311{
312}
313
314/**
315 * mux_type - Describe the port.
316 * @port: Ptr to the uart_port.
317 *
318 * Return a pointer to a string constant describing the
319 * specified port.
320 */
321static const char *mux_type(struct uart_port *port)
322{
323 return "Mux";
324}
325
326/**
327 * mux_release_port - Release memory and IO regions.
328 * @port: Ptr to the uart_port.
329 *
330 * Release any memory and IO region resources currently in use by
331 * the port.
332 */
333static void mux_release_port(struct uart_port *port)
334{
335}
336
337/**
338 * mux_request_port - Request memory and IO regions.
339 * @port: Ptr to the uart_port.
340 *
341 * Request any memory and IO region resources required by the port.
342 * If any fail, no resources should be registered when this function
343 * returns, and it should return -EBUSY on failure.
344 */
345static int mux_request_port(struct uart_port *port)
346{
347 return 0;
348}
349
350/**
351 * mux_config_port - Perform port autoconfiguration.
352 * @port: Ptr to the uart_port.
353 * @type: Bitmask of required configurations.
354 *
355 * Perform any autoconfiguration steps for the port. This function is
356 * called if the UPF_BOOT_AUTOCONF flag is specified for the port.
357 * [Note: This is required for now because of a bug in the Serial core.
358 * rmk has already submitted a patch to linus, should be available for
359 * 2.5.47.]
360 */
361static void mux_config_port(struct uart_port *port, int type)
362{
363 port->type = PORT_MUX;
364}
365
366/**
367 * mux_verify_port - Verify the port information.
368 * @port: Ptr to the uart_port.
369 * @ser: Ptr to the serial information.
370 *
371 * Verify the new serial port information contained within serinfo is
372 * suitable for this port type.
373 */
374static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
375{
376 if(port->membase == NULL)
377 return -EINVAL;
378
379 return 0;
380}
381
382/**
383 * mux_drv_poll - Mux poll function.
384 * @unused: Unused variable
385 *
386 * This function periodically polls the Serial MUX to check for new data.
387 */
388static void mux_poll(unsigned long unused)
389{
390 int i;
391
392 for(i = 0; i < port_cnt; ++i) {
393 if(!mux_ports[i].enabled)
394 continue;
395
396 mux_read(&mux_ports[i].port);
397 mux_write(&mux_ports[i].port);
398 }
399
400 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
401}
402
403
404#ifdef CONFIG_SERIAL_MUX_CONSOLE
405static void mux_console_write(struct console *co, const char *s, unsigned count)
406{
407 /* Wait until the FIFO drains. */
408 while(UART_GET_FIFO_CNT(&mux_ports[0].port))
409 udelay(1);
410
411 while(count--) {
412 if(*s == '\n') {
413 UART_PUT_CHAR(&mux_ports[0].port, '\r');
414 }
415 UART_PUT_CHAR(&mux_ports[0].port, *s++);
416 }
417
418}
419
420static int mux_console_setup(struct console *co, char *options)
421{
422 return 0;
423}
424
425struct tty_driver *mux_console_device(struct console *co, int *index)
426{
427 *index = co->index;
428 return mux_driver.tty_driver;
429}
430
431static struct console mux_console = {
432 .name = "ttyB",
433 .write = mux_console_write,
434 .device = mux_console_device,
435 .setup = mux_console_setup,
436 .flags = CON_ENABLED | CON_PRINTBUFFER,
437 .index = 0,
438};
439
440#define MUX_CONSOLE &mux_console
441#else
442#define MUX_CONSOLE NULL
443#endif
444
445static struct uart_ops mux_pops = {
446 .tx_empty = mux_tx_empty,
447 .set_mctrl = mux_set_mctrl,
448 .get_mctrl = mux_get_mctrl,
449 .stop_tx = mux_stop_tx,
450 .start_tx = mux_start_tx,
451 .stop_rx = mux_stop_rx,
452 .enable_ms = mux_enable_ms,
453 .break_ctl = mux_break_ctl,
454 .startup = mux_startup,
455 .shutdown = mux_shutdown,
456 .set_termios = mux_set_termios,
457 .type = mux_type,
458 .release_port = mux_release_port,
459 .request_port = mux_request_port,
460 .config_port = mux_config_port,
461 .verify_port = mux_verify_port,
462};
463
464/**
465 * mux_probe - Determine if the Serial Mux should claim this device.
466 * @dev: The parisc device.
467 *
468 * Deterimine if the Serial Mux should claim this chip (return 0)
469 * or not (return 1).
470 */
471static int __init mux_probe(struct parisc_device *dev)
472{
473 int i, status;
474
475 int port_count = get_mux_port_count(dev);
476 printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.6\n", port_count);
477
478 dev_set_drvdata(&dev->dev, (void *)(long)port_count);
479 request_mem_region(dev->hpa.start + MUX_OFFSET,
480 port_count * MUX_LINE_OFFSET, "Mux");
481
482 if(!port_cnt) {
483 mux_driver.cons = MUX_CONSOLE;
484
485 status = uart_register_driver(&mux_driver);
486 if(status) {
487 printk(KERN_ERR "Serial mux: Unable to register driver.\n");
488 return 1;
489 }
490 }
491
492 for(i = 0; i < port_count; ++i, ++port_cnt) {
493 struct uart_port *port = &mux_ports[port_cnt].port;
494 port->iobase = 0;
495 port->mapbase = dev->hpa.start + MUX_OFFSET +
496 (i * MUX_LINE_OFFSET);
497 port->membase = ioremap_nocache(port->mapbase, MUX_LINE_OFFSET);
498 port->iotype = UPIO_MEM;
499 port->type = PORT_MUX;
500 port->irq = NO_IRQ;
501 port->uartclk = 0;
502 port->fifosize = MUX_FIFO_SIZE;
503 port->ops = &mux_pops;
504 port->flags = UPF_BOOT_AUTOCONF;
505 port->line = port_cnt;
506
507 /* The port->timeout needs to match what is present in
508 * uart_wait_until_sent in serial_core.c. Otherwise
509 * the time spent in msleep_interruptable will be very
510 * long, causing the appearance of a console hang.
511 */
512 port->timeout = HZ / 50;
513 spin_lock_init(&port->lock);
514
515 status = uart_add_one_port(&mux_driver, port);
516 BUG_ON(status);
517 }
518
519 return 0;
520}
521
522static int __devexit mux_remove(struct parisc_device *dev)
523{
524 int i, j;
525 int port_count = (long)dev_get_drvdata(&dev->dev);
526
527 /* Find Port 0 for this card in the mux_ports list. */
528 for(i = 0; i < port_cnt; ++i) {
529 if(mux_ports[i].port.mapbase == dev->hpa.start + MUX_OFFSET)
530 break;
531 }
532 BUG_ON(i + port_count > port_cnt);
533
534 /* Release the resources associated with each port on the device. */
535 for(j = 0; j < port_count; ++j, ++i) {
536 struct uart_port *port = &mux_ports[i].port;
537
538 uart_remove_one_port(&mux_driver, port);
539 if(port->membase)
540 iounmap(port->membase);
541 }
542
543 release_mem_region(dev->hpa.start + MUX_OFFSET, port_count * MUX_LINE_OFFSET);
544 return 0;
545}
546
547/* Hack. This idea was taken from the 8250_gsc.c on how to properly order
548 * the serial port detection in the proper order. The idea is we always
549 * want the builtin mux to be detected before addin mux cards, so we
550 * specifically probe for the builtin mux cards first.
551 *
552 * This table only contains the parisc_device_id of known builtin mux
553 * devices. All other mux cards will be detected by the generic mux_tbl.
554 */
555static struct parisc_device_id builtin_mux_tbl[] = {
556 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x15, 0x0000D }, /* All K-class */
557 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x44, 0x0000D }, /* E35, E45, and E55 */
558 { 0, }
559};
560
561static struct parisc_device_id mux_tbl[] = {
562 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
563 { 0, }
564};
565
566MODULE_DEVICE_TABLE(parisc, builtin_mux_tbl);
567MODULE_DEVICE_TABLE(parisc, mux_tbl);
568
569static struct parisc_driver builtin_serial_mux_driver = {
570 .name = "builtin_serial_mux",
571 .id_table = builtin_mux_tbl,
572 .probe = mux_probe,
573 .remove = __devexit_p(mux_remove),
574};
575
576static struct parisc_driver serial_mux_driver = {
577 .name = "serial_mux",
578 .id_table = mux_tbl,
579 .probe = mux_probe,
580 .remove = __devexit_p(mux_remove),
581};
582
583/**
584 * mux_init - Serial MUX initialization procedure.
585 *
586 * Register the Serial MUX driver.
587 */
588static int __init mux_init(void)
589{
590 register_parisc_driver(&builtin_serial_mux_driver);
591 register_parisc_driver(&serial_mux_driver);
592
593 if(port_cnt > 0) {
594 /* Start the Mux timer */
595 init_timer(&mux_timer);
596 mux_timer.function = mux_poll;
597 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
598
599#ifdef CONFIG_SERIAL_MUX_CONSOLE
600 register_console(&mux_console);
601#endif
602 }
603
604 return 0;
605}
606
607/**
608 * mux_exit - Serial MUX cleanup procedure.
609 *
610 * Unregister the Serial MUX driver from the tty layer.
611 */
612static void __exit mux_exit(void)
613{
614 /* Delete the Mux timer. */
615 if(port_cnt > 0) {
616 del_timer(&mux_timer);
617#ifdef CONFIG_SERIAL_MUX_CONSOLE
618 unregister_console(&mux_console);
619#endif
620 }
621
622 unregister_parisc_driver(&builtin_serial_mux_driver);
623 unregister_parisc_driver(&serial_mux_driver);
624 uart_unregister_driver(&mux_driver);
625}
626
627module_init(mux_init);
628module_exit(mux_exit);
629
630MODULE_AUTHOR("Ryan Bradetich");
631MODULE_DESCRIPTION("Serial MUX driver");
632MODULE_LICENSE("GPL");
633MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);
diff --git a/drivers/tty/serial/netx-serial.c b/drivers/tty/serial/netx-serial.c
new file mode 100644
index 000000000000..7735c9f35fa0
--- /dev/null
+++ b/drivers/tty/serial/netx-serial.c
@@ -0,0 +1,750 @@
1/*
2 * drivers/serial/netx-serial.c
3 *
4 * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
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
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#if defined(CONFIG_SERIAL_NETX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21#define SUPPORT_SYSRQ
22#endif
23
24#include <linux/device.h>
25#include <linux/module.h>
26#include <linux/ioport.h>
27#include <linux/init.h>
28#include <linux/console.h>
29#include <linux/sysrq.h>
30#include <linux/platform_device.h>
31#include <linux/tty.h>
32#include <linux/tty_flip.h>
33#include <linux/serial_core.h>
34#include <linux/serial.h>
35
36#include <asm/io.h>
37#include <asm/irq.h>
38#include <mach/hardware.h>
39#include <mach/netx-regs.h>
40
41/* We've been assigned a range on the "Low-density serial ports" major */
42#define SERIAL_NX_MAJOR 204
43#define MINOR_START 170
44
45enum uart_regs {
46 UART_DR = 0x00,
47 UART_SR = 0x04,
48 UART_LINE_CR = 0x08,
49 UART_BAUDDIV_MSB = 0x0c,
50 UART_BAUDDIV_LSB = 0x10,
51 UART_CR = 0x14,
52 UART_FR = 0x18,
53 UART_IIR = 0x1c,
54 UART_ILPR = 0x20,
55 UART_RTS_CR = 0x24,
56 UART_RTS_LEAD = 0x28,
57 UART_RTS_TRAIL = 0x2c,
58 UART_DRV_ENABLE = 0x30,
59 UART_BRM_CR = 0x34,
60 UART_RXFIFO_IRQLEVEL = 0x38,
61 UART_TXFIFO_IRQLEVEL = 0x3c,
62};
63
64#define SR_FE (1<<0)
65#define SR_PE (1<<1)
66#define SR_BE (1<<2)
67#define SR_OE (1<<3)
68
69#define LINE_CR_BRK (1<<0)
70#define LINE_CR_PEN (1<<1)
71#define LINE_CR_EPS (1<<2)
72#define LINE_CR_STP2 (1<<3)
73#define LINE_CR_FEN (1<<4)
74#define LINE_CR_5BIT (0<<5)
75#define LINE_CR_6BIT (1<<5)
76#define LINE_CR_7BIT (2<<5)
77#define LINE_CR_8BIT (3<<5)
78#define LINE_CR_BITS_MASK (3<<5)
79
80#define CR_UART_EN (1<<0)
81#define CR_SIREN (1<<1)
82#define CR_SIRLP (1<<2)
83#define CR_MSIE (1<<3)
84#define CR_RIE (1<<4)
85#define CR_TIE (1<<5)
86#define CR_RTIE (1<<6)
87#define CR_LBE (1<<7)
88
89#define FR_CTS (1<<0)
90#define FR_DSR (1<<1)
91#define FR_DCD (1<<2)
92#define FR_BUSY (1<<3)
93#define FR_RXFE (1<<4)
94#define FR_TXFF (1<<5)
95#define FR_RXFF (1<<6)
96#define FR_TXFE (1<<7)
97
98#define IIR_MIS (1<<0)
99#define IIR_RIS (1<<1)
100#define IIR_TIS (1<<2)
101#define IIR_RTIS (1<<3)
102#define IIR_MASK 0xf
103
104#define RTS_CR_AUTO (1<<0)
105#define RTS_CR_RTS (1<<1)
106#define RTS_CR_COUNT (1<<2)
107#define RTS_CR_MOD2 (1<<3)
108#define RTS_CR_RTS_POL (1<<4)
109#define RTS_CR_CTS_CTR (1<<5)
110#define RTS_CR_CTS_POL (1<<6)
111#define RTS_CR_STICK (1<<7)
112
113#define UART_PORT_SIZE 0x40
114#define DRIVER_NAME "netx-uart"
115
116struct netx_port {
117 struct uart_port port;
118};
119
120static void netx_stop_tx(struct uart_port *port)
121{
122 unsigned int val;
123 val = readl(port->membase + UART_CR);
124 writel(val & ~CR_TIE, port->membase + UART_CR);
125}
126
127static void netx_stop_rx(struct uart_port *port)
128{
129 unsigned int val;
130 val = readl(port->membase + UART_CR);
131 writel(val & ~CR_RIE, port->membase + UART_CR);
132}
133
134static void netx_enable_ms(struct uart_port *port)
135{
136 unsigned int val;
137 val = readl(port->membase + UART_CR);
138 writel(val | CR_MSIE, port->membase + UART_CR);
139}
140
141static inline void netx_transmit_buffer(struct uart_port *port)
142{
143 struct circ_buf *xmit = &port->state->xmit;
144
145 if (port->x_char) {
146 writel(port->x_char, port->membase + UART_DR);
147 port->icount.tx++;
148 port->x_char = 0;
149 return;
150 }
151
152 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
153 netx_stop_tx(port);
154 return;
155 }
156
157 do {
158 /* send xmit->buf[xmit->tail]
159 * out the port here */
160 writel(xmit->buf[xmit->tail], port->membase + UART_DR);
161 xmit->tail = (xmit->tail + 1) &
162 (UART_XMIT_SIZE - 1);
163 port->icount.tx++;
164 if (uart_circ_empty(xmit))
165 break;
166 } while (!(readl(port->membase + UART_FR) & FR_TXFF));
167
168 if (uart_circ_empty(xmit))
169 netx_stop_tx(port);
170}
171
172static void netx_start_tx(struct uart_port *port)
173{
174 writel(
175 readl(port->membase + UART_CR) | CR_TIE, port->membase + UART_CR);
176
177 if (!(readl(port->membase + UART_FR) & FR_TXFF))
178 netx_transmit_buffer(port);
179}
180
181static unsigned int netx_tx_empty(struct uart_port *port)
182{
183 return readl(port->membase + UART_FR) & FR_BUSY ? 0 : TIOCSER_TEMT;
184}
185
186static void netx_txint(struct uart_port *port)
187{
188 struct circ_buf *xmit = &port->state->xmit;
189
190 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
191 netx_stop_tx(port);
192 return;
193 }
194
195 netx_transmit_buffer(port);
196
197 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
198 uart_write_wakeup(port);
199}
200
201static void netx_rxint(struct uart_port *port)
202{
203 unsigned char rx, flg, status;
204 struct tty_struct *tty = port->state->port.tty;
205
206 while (!(readl(port->membase + UART_FR) & FR_RXFE)) {
207 rx = readl(port->membase + UART_DR);
208 flg = TTY_NORMAL;
209 port->icount.rx++;
210 status = readl(port->membase + UART_SR);
211 if (status & SR_BE) {
212 writel(0, port->membase + UART_SR);
213 if (uart_handle_break(port))
214 continue;
215 }
216
217 if (unlikely(status & (SR_FE | SR_PE | SR_OE))) {
218
219 if (status & SR_PE)
220 port->icount.parity++;
221 else if (status & SR_FE)
222 port->icount.frame++;
223 if (status & SR_OE)
224 port->icount.overrun++;
225
226 status &= port->read_status_mask;
227
228 if (status & SR_BE)
229 flg = TTY_BREAK;
230 else if (status & SR_PE)
231 flg = TTY_PARITY;
232 else if (status & SR_FE)
233 flg = TTY_FRAME;
234 }
235
236 if (uart_handle_sysrq_char(port, rx))
237 continue;
238
239 uart_insert_char(port, status, SR_OE, rx, flg);
240 }
241
242 tty_flip_buffer_push(tty);
243 return;
244}
245
246static irqreturn_t netx_int(int irq, void *dev_id)
247{
248 struct uart_port *port = dev_id;
249 unsigned long flags;
250 unsigned char status;
251
252 spin_lock_irqsave(&port->lock,flags);
253
254 status = readl(port->membase + UART_IIR) & IIR_MASK;
255 while (status) {
256 if (status & IIR_RIS)
257 netx_rxint(port);
258 if (status & IIR_TIS)
259 netx_txint(port);
260 if (status & IIR_MIS) {
261 if (readl(port->membase + UART_FR) & FR_CTS)
262 uart_handle_cts_change(port, 1);
263 else
264 uart_handle_cts_change(port, 0);
265 }
266 writel(0, port->membase + UART_IIR);
267 status = readl(port->membase + UART_IIR) & IIR_MASK;
268 }
269
270 spin_unlock_irqrestore(&port->lock,flags);
271 return IRQ_HANDLED;
272}
273
274static unsigned int netx_get_mctrl(struct uart_port *port)
275{
276 unsigned int ret = TIOCM_DSR | TIOCM_CAR;
277
278 if (readl(port->membase + UART_FR) & FR_CTS)
279 ret |= TIOCM_CTS;
280
281 return ret;
282}
283
284static void netx_set_mctrl(struct uart_port *port, unsigned int mctrl)
285{
286 unsigned int val;
287
288 /* FIXME: Locking needed ? */
289 if (mctrl & TIOCM_RTS) {
290 val = readl(port->membase + UART_RTS_CR);
291 writel(val | RTS_CR_RTS, port->membase + UART_RTS_CR);
292 }
293}
294
295static void netx_break_ctl(struct uart_port *port, int break_state)
296{
297 unsigned int line_cr;
298 spin_lock_irq(&port->lock);
299
300 line_cr = readl(port->membase + UART_LINE_CR);
301 if (break_state != 0)
302 line_cr |= LINE_CR_BRK;
303 else
304 line_cr &= ~LINE_CR_BRK;
305 writel(line_cr, port->membase + UART_LINE_CR);
306
307 spin_unlock_irq(&port->lock);
308}
309
310static int netx_startup(struct uart_port *port)
311{
312 int ret;
313
314 ret = request_irq(port->irq, netx_int, 0,
315 DRIVER_NAME, port);
316 if (ret) {
317 dev_err(port->dev, "unable to grab irq%d\n",port->irq);
318 goto exit;
319 }
320
321 writel(readl(port->membase + UART_LINE_CR) | LINE_CR_FEN,
322 port->membase + UART_LINE_CR);
323
324 writel(CR_MSIE | CR_RIE | CR_TIE | CR_RTIE | CR_UART_EN,
325 port->membase + UART_CR);
326
327exit:
328 return ret;
329}
330
331static void netx_shutdown(struct uart_port *port)
332{
333 writel(0, port->membase + UART_CR) ;
334
335 free_irq(port->irq, port);
336}
337
338static void
339netx_set_termios(struct uart_port *port, struct ktermios *termios,
340 struct ktermios *old)
341{
342 unsigned int baud, quot;
343 unsigned char old_cr;
344 unsigned char line_cr = LINE_CR_FEN;
345 unsigned char rts_cr = 0;
346
347 switch (termios->c_cflag & CSIZE) {
348 case CS5:
349 line_cr |= LINE_CR_5BIT;
350 break;
351 case CS6:
352 line_cr |= LINE_CR_6BIT;
353 break;
354 case CS7:
355 line_cr |= LINE_CR_7BIT;
356 break;
357 case CS8:
358 line_cr |= LINE_CR_8BIT;
359 break;
360 }
361
362 if (termios->c_cflag & CSTOPB)
363 line_cr |= LINE_CR_STP2;
364
365 if (termios->c_cflag & PARENB) {
366 line_cr |= LINE_CR_PEN;
367 if (!(termios->c_cflag & PARODD))
368 line_cr |= LINE_CR_EPS;
369 }
370
371 if (termios->c_cflag & CRTSCTS)
372 rts_cr = RTS_CR_AUTO | RTS_CR_CTS_CTR | RTS_CR_RTS_POL;
373
374 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
375 quot = baud * 4096;
376 quot /= 1000;
377 quot *= 256;
378 quot /= 100000;
379
380 spin_lock_irq(&port->lock);
381
382 uart_update_timeout(port, termios->c_cflag, baud);
383
384 old_cr = readl(port->membase + UART_CR);
385
386 /* disable interrupts */
387 writel(old_cr & ~(CR_MSIE | CR_RIE | CR_TIE | CR_RTIE),
388 port->membase + UART_CR);
389
390 /* drain transmitter */
391 while (readl(port->membase + UART_FR) & FR_BUSY);
392
393 /* disable UART */
394 writel(old_cr & ~CR_UART_EN, port->membase + UART_CR);
395
396 /* modem status interrupts */
397 old_cr &= ~CR_MSIE;
398 if (UART_ENABLE_MS(port, termios->c_cflag))
399 old_cr |= CR_MSIE;
400
401 writel((quot>>8) & 0xff, port->membase + UART_BAUDDIV_MSB);
402 writel(quot & 0xff, port->membase + UART_BAUDDIV_LSB);
403 writel(line_cr, port->membase + UART_LINE_CR);
404
405 writel(rts_cr, port->membase + UART_RTS_CR);
406
407 /*
408 * Characters to ignore
409 */
410 port->ignore_status_mask = 0;
411 if (termios->c_iflag & IGNPAR)
412 port->ignore_status_mask |= SR_PE;
413 if (termios->c_iflag & IGNBRK) {
414 port->ignore_status_mask |= SR_BE;
415 /*
416 * If we're ignoring parity and break indicators,
417 * ignore overruns too (for real raw support).
418 */
419 if (termios->c_iflag & IGNPAR)
420 port->ignore_status_mask |= SR_PE;
421 }
422
423 port->read_status_mask = 0;
424 if (termios->c_iflag & (BRKINT | PARMRK))
425 port->read_status_mask |= SR_BE;
426 if (termios->c_iflag & INPCK)
427 port->read_status_mask |= SR_PE | SR_FE;
428
429 writel(old_cr, port->membase + UART_CR);
430
431 spin_unlock_irq(&port->lock);
432}
433
434static const char *netx_type(struct uart_port *port)
435{
436 return port->type == PORT_NETX ? "NETX" : NULL;
437}
438
439static void netx_release_port(struct uart_port *port)
440{
441 release_mem_region(port->mapbase, UART_PORT_SIZE);
442}
443
444static int netx_request_port(struct uart_port *port)
445{
446 return request_mem_region(port->mapbase, UART_PORT_SIZE,
447 DRIVER_NAME) != NULL ? 0 : -EBUSY;
448}
449
450static void netx_config_port(struct uart_port *port, int flags)
451{
452 if (flags & UART_CONFIG_TYPE && netx_request_port(port) == 0)
453 port->type = PORT_NETX;
454}
455
456static int
457netx_verify_port(struct uart_port *port, struct serial_struct *ser)
458{
459 int ret = 0;
460
461 if (ser->type != PORT_UNKNOWN && ser->type != PORT_NETX)
462 ret = -EINVAL;
463
464 return ret;
465}
466
467static struct uart_ops netx_pops = {
468 .tx_empty = netx_tx_empty,
469 .set_mctrl = netx_set_mctrl,
470 .get_mctrl = netx_get_mctrl,
471 .stop_tx = netx_stop_tx,
472 .start_tx = netx_start_tx,
473 .stop_rx = netx_stop_rx,
474 .enable_ms = netx_enable_ms,
475 .break_ctl = netx_break_ctl,
476 .startup = netx_startup,
477 .shutdown = netx_shutdown,
478 .set_termios = netx_set_termios,
479 .type = netx_type,
480 .release_port = netx_release_port,
481 .request_port = netx_request_port,
482 .config_port = netx_config_port,
483 .verify_port = netx_verify_port,
484};
485
486static struct netx_port netx_ports[] = {
487 {
488 .port = {
489 .type = PORT_NETX,
490 .iotype = UPIO_MEM,
491 .membase = (char __iomem *)io_p2v(NETX_PA_UART0),
492 .mapbase = NETX_PA_UART0,
493 .irq = NETX_IRQ_UART0,
494 .uartclk = 100000000,
495 .fifosize = 16,
496 .flags = UPF_BOOT_AUTOCONF,
497 .ops = &netx_pops,
498 .line = 0,
499 },
500 }, {
501 .port = {
502 .type = PORT_NETX,
503 .iotype = UPIO_MEM,
504 .membase = (char __iomem *)io_p2v(NETX_PA_UART1),
505 .mapbase = NETX_PA_UART1,
506 .irq = NETX_IRQ_UART1,
507 .uartclk = 100000000,
508 .fifosize = 16,
509 .flags = UPF_BOOT_AUTOCONF,
510 .ops = &netx_pops,
511 .line = 1,
512 },
513 }, {
514 .port = {
515 .type = PORT_NETX,
516 .iotype = UPIO_MEM,
517 .membase = (char __iomem *)io_p2v(NETX_PA_UART2),
518 .mapbase = NETX_PA_UART2,
519 .irq = NETX_IRQ_UART2,
520 .uartclk = 100000000,
521 .fifosize = 16,
522 .flags = UPF_BOOT_AUTOCONF,
523 .ops = &netx_pops,
524 .line = 2,
525 },
526 }
527};
528
529#ifdef CONFIG_SERIAL_NETX_CONSOLE
530
531static void netx_console_putchar(struct uart_port *port, int ch)
532{
533 while (readl(port->membase + UART_FR) & FR_BUSY);
534 writel(ch, port->membase + UART_DR);
535}
536
537static void
538netx_console_write(struct console *co, const char *s, unsigned int count)
539{
540 struct uart_port *port = &netx_ports[co->index].port;
541 unsigned char cr_save;
542
543 cr_save = readl(port->membase + UART_CR);
544 writel(cr_save | CR_UART_EN, port->membase + UART_CR);
545
546 uart_console_write(port, s, count, netx_console_putchar);
547
548 while (readl(port->membase + UART_FR) & FR_BUSY);
549 writel(cr_save, port->membase + UART_CR);
550}
551
552static void __init
553netx_console_get_options(struct uart_port *port, int *baud,
554 int *parity, int *bits, int *flow)
555{
556 unsigned char line_cr;
557
558 *baud = (readl(port->membase + UART_BAUDDIV_MSB) << 8) |
559 readl(port->membase + UART_BAUDDIV_LSB);
560 *baud *= 1000;
561 *baud /= 4096;
562 *baud *= 1000;
563 *baud /= 256;
564 *baud *= 100;
565
566 line_cr = readl(port->membase + UART_LINE_CR);
567 *parity = 'n';
568 if (line_cr & LINE_CR_PEN) {
569 if (line_cr & LINE_CR_EPS)
570 *parity = 'e';
571 else
572 *parity = 'o';
573 }
574
575 switch (line_cr & LINE_CR_BITS_MASK) {
576 case LINE_CR_8BIT:
577 *bits = 8;
578 break;
579 case LINE_CR_7BIT:
580 *bits = 7;
581 break;
582 case LINE_CR_6BIT:
583 *bits = 6;
584 break;
585 case LINE_CR_5BIT:
586 *bits = 5;
587 break;
588 }
589
590 if (readl(port->membase + UART_RTS_CR) & RTS_CR_AUTO)
591 *flow = 'r';
592}
593
594static int __init
595netx_console_setup(struct console *co, char *options)
596{
597 struct netx_port *sport;
598 int baud = 9600;
599 int bits = 8;
600 int parity = 'n';
601 int flow = 'n';
602
603 /*
604 * Check whether an invalid uart number has been specified, and
605 * if so, search for the first available port that does have
606 * console support.
607 */
608 if (co->index == -1 || co->index >= ARRAY_SIZE(netx_ports))
609 co->index = 0;
610 sport = &netx_ports[co->index];
611
612 if (options) {
613 uart_parse_options(options, &baud, &parity, &bits, &flow);
614 } else {
615 /* if the UART is enabled, assume it has been correctly setup
616 * by the bootloader and get the options
617 */
618 if (readl(sport->port.membase + UART_CR) & CR_UART_EN) {
619 netx_console_get_options(&sport->port, &baud,
620 &parity, &bits, &flow);
621 }
622
623 }
624
625 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
626}
627
628static struct uart_driver netx_reg;
629static struct console netx_console = {
630 .name = "ttyNX",
631 .write = netx_console_write,
632 .device = uart_console_device,
633 .setup = netx_console_setup,
634 .flags = CON_PRINTBUFFER,
635 .index = -1,
636 .data = &netx_reg,
637};
638
639static int __init netx_console_init(void)
640{
641 register_console(&netx_console);
642 return 0;
643}
644console_initcall(netx_console_init);
645
646#define NETX_CONSOLE &netx_console
647#else
648#define NETX_CONSOLE NULL
649#endif
650
651static struct uart_driver netx_reg = {
652 .owner = THIS_MODULE,
653 .driver_name = DRIVER_NAME,
654 .dev_name = "ttyNX",
655 .major = SERIAL_NX_MAJOR,
656 .minor = MINOR_START,
657 .nr = ARRAY_SIZE(netx_ports),
658 .cons = NETX_CONSOLE,
659};
660
661static int serial_netx_suspend(struct platform_device *pdev, pm_message_t state)
662{
663 struct netx_port *sport = platform_get_drvdata(pdev);
664
665 if (sport)
666 uart_suspend_port(&netx_reg, &sport->port);
667
668 return 0;
669}
670
671static int serial_netx_resume(struct platform_device *pdev)
672{
673 struct netx_port *sport = platform_get_drvdata(pdev);
674
675 if (sport)
676 uart_resume_port(&netx_reg, &sport->port);
677
678 return 0;
679}
680
681static int serial_netx_probe(struct platform_device *pdev)
682{
683 struct uart_port *port = &netx_ports[pdev->id].port;
684
685 dev_info(&pdev->dev, "initialising\n");
686
687 port->dev = &pdev->dev;
688
689 writel(1, port->membase + UART_RXFIFO_IRQLEVEL);
690 uart_add_one_port(&netx_reg, &netx_ports[pdev->id].port);
691 platform_set_drvdata(pdev, &netx_ports[pdev->id]);
692
693 return 0;
694}
695
696static int serial_netx_remove(struct platform_device *pdev)
697{
698 struct netx_port *sport = platform_get_drvdata(pdev);
699
700 platform_set_drvdata(pdev, NULL);
701
702 if (sport)
703 uart_remove_one_port(&netx_reg, &sport->port);
704
705 return 0;
706}
707
708static struct platform_driver serial_netx_driver = {
709 .probe = serial_netx_probe,
710 .remove = serial_netx_remove,
711
712 .suspend = serial_netx_suspend,
713 .resume = serial_netx_resume,
714
715 .driver = {
716 .name = DRIVER_NAME,
717 .owner = THIS_MODULE,
718 },
719};
720
721static int __init netx_serial_init(void)
722{
723 int ret;
724
725 printk(KERN_INFO "Serial: NetX driver\n");
726
727 ret = uart_register_driver(&netx_reg);
728 if (ret)
729 return ret;
730
731 ret = platform_driver_register(&serial_netx_driver);
732 if (ret != 0)
733 uart_unregister_driver(&netx_reg);
734
735 return 0;
736}
737
738static void __exit netx_serial_exit(void)
739{
740 platform_driver_unregister(&serial_netx_driver);
741 uart_unregister_driver(&netx_reg);
742}
743
744module_init(netx_serial_init);
745module_exit(netx_serial_exit);
746
747MODULE_AUTHOR("Sascha Hauer");
748MODULE_DESCRIPTION("NetX serial port driver");
749MODULE_LICENSE("GPL");
750MODULE_ALIAS("platform:" DRIVER_NAME);
diff --git a/drivers/tty/serial/nwpserial.c b/drivers/tty/serial/nwpserial.c
new file mode 100644
index 000000000000..de173671e3d0
--- /dev/null
+++ b/drivers/tty/serial/nwpserial.c
@@ -0,0 +1,477 @@
1/*
2 * Serial Port driver for a NWP uart device
3 *
4 * Copyright (C) 2008 IBM Corp., Benjamin Krill <ben@codiert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <linux/init.h>
13#include <linux/console.h>
14#include <linux/serial.h>
15#include <linux/serial_reg.h>
16#include <linux/serial_core.h>
17#include <linux/tty.h>
18#include <linux/irqreturn.h>
19#include <linux/mutex.h>
20#include <linux/of_platform.h>
21#include <linux/of_device.h>
22#include <linux/nwpserial.h>
23#include <asm/prom.h>
24#include <asm/dcr.h>
25
26#define NWPSERIAL_NR 2
27
28#define NWPSERIAL_STATUS_RXVALID 0x1
29#define NWPSERIAL_STATUS_TXFULL 0x2
30
31struct nwpserial_port {
32 struct uart_port port;
33 dcr_host_t dcr_host;
34 unsigned int ier;
35 unsigned int mcr;
36};
37
38static DEFINE_MUTEX(nwpserial_mutex);
39static struct nwpserial_port nwpserial_ports[NWPSERIAL_NR];
40
41static void wait_for_bits(struct nwpserial_port *up, int bits)
42{
43 unsigned int status, tmout = 10000;
44
45 /* Wait up to 10ms for the character(s) to be sent. */
46 do {
47 status = dcr_read(up->dcr_host, UART_LSR);
48
49 if (--tmout == 0)
50 break;
51 udelay(1);
52 } while ((status & bits) != bits);
53}
54
55#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
56static void nwpserial_console_putchar(struct uart_port *port, int c)
57{
58 struct nwpserial_port *up;
59 up = container_of(port, struct nwpserial_port, port);
60 /* check if tx buffer is full */
61 wait_for_bits(up, UART_LSR_THRE);
62 dcr_write(up->dcr_host, UART_TX, c);
63 up->port.icount.tx++;
64}
65
66static void
67nwpserial_console_write(struct console *co, const char *s, unsigned int count)
68{
69 struct nwpserial_port *up = &nwpserial_ports[co->index];
70 unsigned long flags;
71 int locked = 1;
72
73 if (oops_in_progress)
74 locked = spin_trylock_irqsave(&up->port.lock, flags);
75 else
76 spin_lock_irqsave(&up->port.lock, flags);
77
78 /* save and disable interrupt */
79 up->ier = dcr_read(up->dcr_host, UART_IER);
80 dcr_write(up->dcr_host, UART_IER, up->ier & ~UART_IER_RDI);
81
82 uart_console_write(&up->port, s, count, nwpserial_console_putchar);
83
84 /* wait for transmitter to become empty */
85 while ((dcr_read(up->dcr_host, UART_LSR) & UART_LSR_THRE) == 0)
86 cpu_relax();
87
88 /* restore interrupt state */
89 dcr_write(up->dcr_host, UART_IER, up->ier);
90
91 if (locked)
92 spin_unlock_irqrestore(&up->port.lock, flags);
93}
94
95static struct uart_driver nwpserial_reg;
96static struct console nwpserial_console = {
97 .name = "ttySQ",
98 .write = nwpserial_console_write,
99 .device = uart_console_device,
100 .flags = CON_PRINTBUFFER,
101 .index = -1,
102 .data = &nwpserial_reg,
103};
104#define NWPSERIAL_CONSOLE (&nwpserial_console)
105#else
106#define NWPSERIAL_CONSOLE NULL
107#endif /* CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE */
108
109/**************************************************************************/
110
111static int nwpserial_request_port(struct uart_port *port)
112{
113 return 0;
114}
115
116static void nwpserial_release_port(struct uart_port *port)
117{
118 /* N/A */
119}
120
121static void nwpserial_config_port(struct uart_port *port, int flags)
122{
123 port->type = PORT_NWPSERIAL;
124}
125
126static irqreturn_t nwpserial_interrupt(int irq, void *dev_id)
127{
128 struct nwpserial_port *up = dev_id;
129 struct tty_struct *tty = up->port.state->port.tty;
130 irqreturn_t ret;
131 unsigned int iir;
132 unsigned char ch;
133
134 spin_lock(&up->port.lock);
135
136 /* check if the uart was the interrupt source. */
137 iir = dcr_read(up->dcr_host, UART_IIR);
138 if (!iir) {
139 ret = IRQ_NONE;
140 goto out;
141 }
142
143 do {
144 up->port.icount.rx++;
145 ch = dcr_read(up->dcr_host, UART_RX);
146 if (up->port.ignore_status_mask != NWPSERIAL_STATUS_RXVALID)
147 tty_insert_flip_char(tty, ch, TTY_NORMAL);
148 } while (dcr_read(up->dcr_host, UART_LSR) & UART_LSR_DR);
149
150 tty_flip_buffer_push(tty);
151 ret = IRQ_HANDLED;
152
153 /* clear interrupt */
154 dcr_write(up->dcr_host, UART_IIR, 1);
155out:
156 spin_unlock(&up->port.lock);
157 return ret;
158}
159
160static int nwpserial_startup(struct uart_port *port)
161{
162 struct nwpserial_port *up;
163 int err;
164
165 up = container_of(port, struct nwpserial_port, port);
166
167 /* disable flow control by default */
168 up->mcr = dcr_read(up->dcr_host, UART_MCR) & ~UART_MCR_AFE;
169 dcr_write(up->dcr_host, UART_MCR, up->mcr);
170
171 /* register interrupt handler */
172 err = request_irq(up->port.irq, nwpserial_interrupt,
173 IRQF_SHARED, "nwpserial", up);
174 if (err)
175 return err;
176
177 /* enable interrupts */
178 up->ier = UART_IER_RDI;
179 dcr_write(up->dcr_host, UART_IER, up->ier);
180
181 /* enable receiving */
182 up->port.ignore_status_mask &= ~NWPSERIAL_STATUS_RXVALID;
183
184 return 0;
185}
186
187static void nwpserial_shutdown(struct uart_port *port)
188{
189 struct nwpserial_port *up;
190 up = container_of(port, struct nwpserial_port, port);
191
192 /* disable receiving */
193 up->port.ignore_status_mask |= NWPSERIAL_STATUS_RXVALID;
194
195 /* disable interrupts from this port */
196 up->ier = 0;
197 dcr_write(up->dcr_host, UART_IER, up->ier);
198
199 /* free irq */
200 free_irq(up->port.irq, port);
201}
202
203static int nwpserial_verify_port(struct uart_port *port,
204 struct serial_struct *ser)
205{
206 return -EINVAL;
207}
208
209static const char *nwpserial_type(struct uart_port *port)
210{
211 return port->type == PORT_NWPSERIAL ? "nwpserial" : NULL;
212}
213
214static void nwpserial_set_termios(struct uart_port *port,
215 struct ktermios *termios, struct ktermios *old)
216{
217 struct nwpserial_port *up;
218 up = container_of(port, struct nwpserial_port, port);
219
220 up->port.read_status_mask = NWPSERIAL_STATUS_RXVALID
221 | NWPSERIAL_STATUS_TXFULL;
222
223 up->port.ignore_status_mask = 0;
224 /* ignore all characters if CREAD is not set */
225 if ((termios->c_cflag & CREAD) == 0)
226 up->port.ignore_status_mask |= NWPSERIAL_STATUS_RXVALID;
227
228 /* Copy back the old hardware settings */
229 if (old)
230 tty_termios_copy_hw(termios, old);
231}
232
233static void nwpserial_break_ctl(struct uart_port *port, int ctl)
234{
235 /* N/A */
236}
237
238static void nwpserial_enable_ms(struct uart_port *port)
239{
240 /* N/A */
241}
242
243static void nwpserial_stop_rx(struct uart_port *port)
244{
245 struct nwpserial_port *up;
246 up = container_of(port, struct nwpserial_port, port);
247 /* don't forward any more data (like !CREAD) */
248 up->port.ignore_status_mask = NWPSERIAL_STATUS_RXVALID;
249}
250
251static void nwpserial_putchar(struct nwpserial_port *up, unsigned char c)
252{
253 /* check if tx buffer is full */
254 wait_for_bits(up, UART_LSR_THRE);
255 dcr_write(up->dcr_host, UART_TX, c);
256 up->port.icount.tx++;
257}
258
259static void nwpserial_start_tx(struct uart_port *port)
260{
261 struct nwpserial_port *up;
262 struct circ_buf *xmit;
263 up = container_of(port, struct nwpserial_port, port);
264 xmit = &up->port.state->xmit;
265
266 if (port->x_char) {
267 nwpserial_putchar(up, up->port.x_char);
268 port->x_char = 0;
269 }
270
271 while (!(uart_circ_empty(xmit) || uart_tx_stopped(&up->port))) {
272 nwpserial_putchar(up, xmit->buf[xmit->tail]);
273 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
274 }
275}
276
277static unsigned int nwpserial_get_mctrl(struct uart_port *port)
278{
279 return 0;
280}
281
282static void nwpserial_set_mctrl(struct uart_port *port, unsigned int mctrl)
283{
284 /* N/A */
285}
286
287static void nwpserial_stop_tx(struct uart_port *port)
288{
289 /* N/A */
290}
291
292static unsigned int nwpserial_tx_empty(struct uart_port *port)
293{
294 struct nwpserial_port *up;
295 unsigned long flags;
296 int ret;
297 up = container_of(port, struct nwpserial_port, port);
298
299 spin_lock_irqsave(&up->port.lock, flags);
300 ret = dcr_read(up->dcr_host, UART_LSR);
301 spin_unlock_irqrestore(&up->port.lock, flags);
302
303 return ret & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
304}
305
306static struct uart_ops nwpserial_pops = {
307 .tx_empty = nwpserial_tx_empty,
308 .set_mctrl = nwpserial_set_mctrl,
309 .get_mctrl = nwpserial_get_mctrl,
310 .stop_tx = nwpserial_stop_tx,
311 .start_tx = nwpserial_start_tx,
312 .stop_rx = nwpserial_stop_rx,
313 .enable_ms = nwpserial_enable_ms,
314 .break_ctl = nwpserial_break_ctl,
315 .startup = nwpserial_startup,
316 .shutdown = nwpserial_shutdown,
317 .set_termios = nwpserial_set_termios,
318 .type = nwpserial_type,
319 .release_port = nwpserial_release_port,
320 .request_port = nwpserial_request_port,
321 .config_port = nwpserial_config_port,
322 .verify_port = nwpserial_verify_port,
323};
324
325static struct uart_driver nwpserial_reg = {
326 .owner = THIS_MODULE,
327 .driver_name = "nwpserial",
328 .dev_name = "ttySQ",
329 .major = TTY_MAJOR,
330 .minor = 68,
331 .nr = NWPSERIAL_NR,
332 .cons = NWPSERIAL_CONSOLE,
333};
334
335int nwpserial_register_port(struct uart_port *port)
336{
337 struct nwpserial_port *up = NULL;
338 int ret = -1;
339 int i;
340 static int first = 1;
341 int dcr_len;
342 int dcr_base;
343 struct device_node *dn;
344
345 mutex_lock(&nwpserial_mutex);
346
347 dn = port->dev->of_node;
348 if (dn == NULL)
349 goto out;
350
351 /* get dcr base. */
352 dcr_base = dcr_resource_start(dn, 0);
353
354 /* find matching entry */
355 for (i = 0; i < NWPSERIAL_NR; i++)
356 if (nwpserial_ports[i].port.iobase == dcr_base) {
357 up = &nwpserial_ports[i];
358 break;
359 }
360
361 /* we didn't find a mtching entry, search for a free port */
362 if (up == NULL)
363 for (i = 0; i < NWPSERIAL_NR; i++)
364 if (nwpserial_ports[i].port.type == PORT_UNKNOWN &&
365 nwpserial_ports[i].port.iobase == 0) {
366 up = &nwpserial_ports[i];
367 break;
368 }
369
370 if (up == NULL) {
371 ret = -EBUSY;
372 goto out;
373 }
374
375 if (first)
376 uart_register_driver(&nwpserial_reg);
377 first = 0;
378
379 up->port.membase = port->membase;
380 up->port.irq = port->irq;
381 up->port.uartclk = port->uartclk;
382 up->port.fifosize = port->fifosize;
383 up->port.regshift = port->regshift;
384 up->port.iotype = port->iotype;
385 up->port.flags = port->flags;
386 up->port.mapbase = port->mapbase;
387 up->port.private_data = port->private_data;
388
389 if (port->dev)
390 up->port.dev = port->dev;
391
392 if (up->port.iobase != dcr_base) {
393 up->port.ops = &nwpserial_pops;
394 up->port.fifosize = 16;
395
396 spin_lock_init(&up->port.lock);
397
398 up->port.iobase = dcr_base;
399 dcr_len = dcr_resource_len(dn, 0);
400
401 up->dcr_host = dcr_map(dn, dcr_base, dcr_len);
402 if (!DCR_MAP_OK(up->dcr_host)) {
403 printk(KERN_ERR "Cannot map DCR resources for NWPSERIAL");
404 goto out;
405 }
406 }
407
408 ret = uart_add_one_port(&nwpserial_reg, &up->port);
409 if (ret == 0)
410 ret = up->port.line;
411
412out:
413 mutex_unlock(&nwpserial_mutex);
414
415 return ret;
416}
417EXPORT_SYMBOL(nwpserial_register_port);
418
419void nwpserial_unregister_port(int line)
420{
421 struct nwpserial_port *up = &nwpserial_ports[line];
422 mutex_lock(&nwpserial_mutex);
423 uart_remove_one_port(&nwpserial_reg, &up->port);
424
425 up->port.type = PORT_UNKNOWN;
426
427 mutex_unlock(&nwpserial_mutex);
428}
429EXPORT_SYMBOL(nwpserial_unregister_port);
430
431#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
432static int __init nwpserial_console_init(void)
433{
434 struct nwpserial_port *up = NULL;
435 struct device_node *dn;
436 const char *name;
437 int dcr_base;
438 int dcr_len;
439 int i;
440
441 /* search for a free port */
442 for (i = 0; i < NWPSERIAL_NR; i++)
443 if (nwpserial_ports[i].port.type == PORT_UNKNOWN) {
444 up = &nwpserial_ports[i];
445 break;
446 }
447
448 if (up == NULL)
449 return -1;
450
451 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
452 if (name == NULL)
453 return -1;
454
455 dn = of_find_node_by_path(name);
456 if (!dn)
457 return -1;
458
459 spin_lock_init(&up->port.lock);
460 up->port.ops = &nwpserial_pops;
461 up->port.type = PORT_NWPSERIAL;
462 up->port.fifosize = 16;
463
464 dcr_base = dcr_resource_start(dn, 0);
465 dcr_len = dcr_resource_len(dn, 0);
466 up->port.iobase = dcr_base;
467
468 up->dcr_host = dcr_map(dn, dcr_base, dcr_len);
469 if (!DCR_MAP_OK(up->dcr_host)) {
470 printk("Cannot map DCR resources for SERIAL");
471 return -1;
472 }
473 register_console(&nwpserial_console);
474 return 0;
475}
476console_initcall(nwpserial_console_init);
477#endif /* CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE */
diff --git a/drivers/tty/serial/of_serial.c b/drivers/tty/serial/of_serial.c
new file mode 100644
index 000000000000..5c7abe4c94dd
--- /dev/null
+++ b/drivers/tty/serial/of_serial.c
@@ -0,0 +1,201 @@
1/*
2 * Serial Port driver for Open Firmware platform devices
3 *
4 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/serial_core.h>
16#include <linux/serial_8250.h>
17#include <linux/of_address.h>
18#include <linux/of_irq.h>
19#include <linux/of_platform.h>
20#include <linux/nwpserial.h>
21
22struct of_serial_info {
23 int type;
24 int line;
25};
26
27/*
28 * Fill a struct uart_port for a given device node
29 */
30static int __devinit of_platform_serial_setup(struct platform_device *ofdev,
31 int type, struct uart_port *port)
32{
33 struct resource resource;
34 struct device_node *np = ofdev->dev.of_node;
35 const __be32 *clk, *spd;
36 const __be32 *prop;
37 int ret, prop_size;
38
39 memset(port, 0, sizeof *port);
40 spd = of_get_property(np, "current-speed", NULL);
41 clk = of_get_property(np, "clock-frequency", NULL);
42 if (!clk) {
43 dev_warn(&ofdev->dev, "no clock-frequency property set\n");
44 return -ENODEV;
45 }
46
47 ret = of_address_to_resource(np, 0, &resource);
48 if (ret) {
49 dev_warn(&ofdev->dev, "invalid address\n");
50 return ret;
51 }
52
53 spin_lock_init(&port->lock);
54 port->mapbase = resource.start;
55
56 /* Check for shifted address mapping */
57 prop = of_get_property(np, "reg-offset", &prop_size);
58 if (prop && (prop_size == sizeof(u32)))
59 port->mapbase += be32_to_cpup(prop);
60
61 /* Check for registers offset within the devices address range */
62 prop = of_get_property(np, "reg-shift", &prop_size);
63 if (prop && (prop_size == sizeof(u32)))
64 port->regshift = be32_to_cpup(prop);
65
66 port->irq = irq_of_parse_and_map(np, 0);
67 port->iotype = UPIO_MEM;
68 port->type = type;
69 port->uartclk = be32_to_cpup(clk);
70 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
71 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
72 port->dev = &ofdev->dev;
73 /* If current-speed was set, then try not to change it. */
74 if (spd)
75 port->custom_divisor = be32_to_cpup(clk) / (16 * (be32_to_cpup(spd)));
76
77 return 0;
78}
79
80/*
81 * Try to register a serial port
82 */
83static int __devinit of_platform_serial_probe(struct platform_device *ofdev,
84 const struct of_device_id *id)
85{
86 struct of_serial_info *info;
87 struct uart_port port;
88 int port_type;
89 int ret;
90
91 if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
92 return -EBUSY;
93
94 info = kmalloc(sizeof(*info), GFP_KERNEL);
95 if (info == NULL)
96 return -ENOMEM;
97
98 port_type = (unsigned long)id->data;
99 ret = of_platform_serial_setup(ofdev, port_type, &port);
100 if (ret)
101 goto out;
102
103 switch (port_type) {
104#ifdef CONFIG_SERIAL_8250
105 case PORT_8250 ... PORT_MAX_8250:
106 ret = serial8250_register_port(&port);
107 break;
108#endif
109#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
110 case PORT_NWPSERIAL:
111 ret = nwpserial_register_port(&port);
112 break;
113#endif
114 default:
115 /* need to add code for these */
116 case PORT_UNKNOWN:
117 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
118 ret = -ENODEV;
119 break;
120 }
121 if (ret < 0)
122 goto out;
123
124 info->type = port_type;
125 info->line = ret;
126 dev_set_drvdata(&ofdev->dev, info);
127 return 0;
128out:
129 kfree(info);
130 irq_dispose_mapping(port.irq);
131 return ret;
132}
133
134/*
135 * Release a line
136 */
137static int of_platform_serial_remove(struct platform_device *ofdev)
138{
139 struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
140 switch (info->type) {
141#ifdef CONFIG_SERIAL_8250
142 case PORT_8250 ... PORT_MAX_8250:
143 serial8250_unregister_port(info->line);
144 break;
145#endif
146#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
147 case PORT_NWPSERIAL:
148 nwpserial_unregister_port(info->line);
149 break;
150#endif
151 default:
152 /* need to add code for these */
153 break;
154 }
155 kfree(info);
156 return 0;
157}
158
159/*
160 * A few common types, add more as needed.
161 */
162static struct of_device_id __devinitdata of_platform_serial_table[] = {
163 { .type = "serial", .compatible = "ns8250", .data = (void *)PORT_8250, },
164 { .type = "serial", .compatible = "ns16450", .data = (void *)PORT_16450, },
165 { .type = "serial", .compatible = "ns16550a", .data = (void *)PORT_16550A, },
166 { .type = "serial", .compatible = "ns16550", .data = (void *)PORT_16550, },
167 { .type = "serial", .compatible = "ns16750", .data = (void *)PORT_16750, },
168 { .type = "serial", .compatible = "ns16850", .data = (void *)PORT_16850, },
169#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
170 { .type = "serial", .compatible = "ibm,qpace-nwp-serial",
171 .data = (void *)PORT_NWPSERIAL, },
172#endif
173 { .type = "serial", .data = (void *)PORT_UNKNOWN, },
174 { /* end of list */ },
175};
176
177static struct of_platform_driver of_platform_serial_driver = {
178 .driver = {
179 .name = "of_serial",
180 .owner = THIS_MODULE,
181 .of_match_table = of_platform_serial_table,
182 },
183 .probe = of_platform_serial_probe,
184 .remove = of_platform_serial_remove,
185};
186
187static int __init of_platform_serial_init(void)
188{
189 return of_register_platform_driver(&of_platform_serial_driver);
190}
191module_init(of_platform_serial_init);
192
193static void __exit of_platform_serial_exit(void)
194{
195 return of_unregister_platform_driver(&of_platform_serial_driver);
196};
197module_exit(of_platform_serial_exit);
198
199MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
200MODULE_LICENSE("GPL");
201MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");
diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
new file mode 100644
index 000000000000..7f2f01058789
--- /dev/null
+++ b/drivers/tty/serial/omap-serial.c
@@ -0,0 +1,1359 @@
1/*
2 * Driver for OMAP-UART controller.
3 * Based on drivers/serial/8250.c
4 *
5 * Copyright (C) 2010 Texas Instruments.
6 *
7 * Authors:
8 * Govindraj R <govindraj.raja@ti.com>
9 * Thara Gopinath <thara@ti.com>
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 * Note: This driver is made seperate from 8250 driver as we cannot
17 * over load 8250 driver with omap platform specific configuration for
18 * features like DMA, it makes easier to implement features like DMA and
19 * hardware flow control and software flow control configuration with
20 * this driver as required for the omap-platform.
21 */
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/console.h>
26#include <linux/serial_reg.h>
27#include <linux/delay.h>
28#include <linux/slab.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/io.h>
32#include <linux/dma-mapping.h>
33#include <linux/clk.h>
34#include <linux/serial_core.h>
35#include <linux/irq.h>
36
37#include <plat/dma.h>
38#include <plat/dmtimer.h>
39#include <plat/omap-serial.h>
40
41static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
42
43/* Forward declaration of functions */
44static void uart_tx_dma_callback(int lch, u16 ch_status, void *data);
45static void serial_omap_rx_timeout(unsigned long uart_no);
46static int serial_omap_start_rxdma(struct uart_omap_port *up);
47
48static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
49{
50 offset <<= up->port.regshift;
51 return readw(up->port.membase + offset);
52}
53
54static inline void serial_out(struct uart_omap_port *up, int offset, int value)
55{
56 offset <<= up->port.regshift;
57 writew(value, up->port.membase + offset);
58}
59
60static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
61{
62 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
63 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
64 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
65 serial_out(up, UART_FCR, 0);
66}
67
68/*
69 * serial_omap_get_divisor - calculate divisor value
70 * @port: uart port info
71 * @baud: baudrate for which divisor needs to be calculated.
72 *
73 * We have written our own function to get the divisor so as to support
74 * 13x mode. 3Mbps Baudrate as an different divisor.
75 * Reference OMAP TRM Chapter 17:
76 * Table 17-1. UART Mode Baud Rates, Divisor Values, and Error Rates
77 * referring to oversampling - divisor value
78 * baudrate 460,800 to 3,686,400 all have divisor 13
79 * except 3,000,000 which has divisor value 16
80 */
81static unsigned int
82serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
83{
84 unsigned int divisor;
85
86 if (baud > OMAP_MODE13X_SPEED && baud != 3000000)
87 divisor = 13;
88 else
89 divisor = 16;
90 return port->uartclk/(baud * divisor);
91}
92
93static void serial_omap_stop_rxdma(struct uart_omap_port *up)
94{
95 if (up->uart_dma.rx_dma_used) {
96 del_timer(&up->uart_dma.rx_timer);
97 omap_stop_dma(up->uart_dma.rx_dma_channel);
98 omap_free_dma(up->uart_dma.rx_dma_channel);
99 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
100 up->uart_dma.rx_dma_used = false;
101 }
102}
103
104static void serial_omap_enable_ms(struct uart_port *port)
105{
106 struct uart_omap_port *up = (struct uart_omap_port *)port;
107
108 dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->pdev->id);
109 up->ier |= UART_IER_MSI;
110 serial_out(up, UART_IER, up->ier);
111}
112
113static void serial_omap_stop_tx(struct uart_port *port)
114{
115 struct uart_omap_port *up = (struct uart_omap_port *)port;
116
117 if (up->use_dma &&
118 up->uart_dma.tx_dma_channel != OMAP_UART_DMA_CH_FREE) {
119 /*
120 * Check if dma is still active. If yes do nothing,
121 * return. Else stop dma
122 */
123 if (omap_get_dma_active_status(up->uart_dma.tx_dma_channel))
124 return;
125 omap_stop_dma(up->uart_dma.tx_dma_channel);
126 omap_free_dma(up->uart_dma.tx_dma_channel);
127 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
128 }
129
130 if (up->ier & UART_IER_THRI) {
131 up->ier &= ~UART_IER_THRI;
132 serial_out(up, UART_IER, up->ier);
133 }
134}
135
136static void serial_omap_stop_rx(struct uart_port *port)
137{
138 struct uart_omap_port *up = (struct uart_omap_port *)port;
139
140 if (up->use_dma)
141 serial_omap_stop_rxdma(up);
142 up->ier &= ~UART_IER_RLSI;
143 up->port.read_status_mask &= ~UART_LSR_DR;
144 serial_out(up, UART_IER, up->ier);
145}
146
147static inline void receive_chars(struct uart_omap_port *up, int *status)
148{
149 struct tty_struct *tty = up->port.state->port.tty;
150 unsigned int flag;
151 unsigned char ch, lsr = *status;
152 int max_count = 256;
153
154 do {
155 if (likely(lsr & UART_LSR_DR))
156 ch = serial_in(up, UART_RX);
157 flag = TTY_NORMAL;
158 up->port.icount.rx++;
159
160 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
161 /*
162 * For statistics only
163 */
164 if (lsr & UART_LSR_BI) {
165 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
166 up->port.icount.brk++;
167 /*
168 * We do the SysRQ and SAK checking
169 * here because otherwise the break
170 * may get masked by ignore_status_mask
171 * or read_status_mask.
172 */
173 if (uart_handle_break(&up->port))
174 goto ignore_char;
175 } else if (lsr & UART_LSR_PE) {
176 up->port.icount.parity++;
177 } else if (lsr & UART_LSR_FE) {
178 up->port.icount.frame++;
179 }
180
181 if (lsr & UART_LSR_OE)
182 up->port.icount.overrun++;
183
184 /*
185 * Mask off conditions which should be ignored.
186 */
187 lsr &= up->port.read_status_mask;
188
189#ifdef CONFIG_SERIAL_OMAP_CONSOLE
190 if (up->port.line == up->port.cons->index) {
191 /* Recover the break flag from console xmit */
192 lsr |= up->lsr_break_flag;
193 up->lsr_break_flag = 0;
194 }
195#endif
196 if (lsr & UART_LSR_BI)
197 flag = TTY_BREAK;
198 else if (lsr & UART_LSR_PE)
199 flag = TTY_PARITY;
200 else if (lsr & UART_LSR_FE)
201 flag = TTY_FRAME;
202 }
203
204 if (uart_handle_sysrq_char(&up->port, ch))
205 goto ignore_char;
206 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
207ignore_char:
208 lsr = serial_in(up, UART_LSR);
209 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
210 spin_unlock(&up->port.lock);
211 tty_flip_buffer_push(tty);
212 spin_lock(&up->port.lock);
213}
214
215static void transmit_chars(struct uart_omap_port *up)
216{
217 struct circ_buf *xmit = &up->port.state->xmit;
218 int count;
219
220 if (up->port.x_char) {
221 serial_out(up, UART_TX, up->port.x_char);
222 up->port.icount.tx++;
223 up->port.x_char = 0;
224 return;
225 }
226 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
227 serial_omap_stop_tx(&up->port);
228 return;
229 }
230 count = up->port.fifosize / 4;
231 do {
232 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
233 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
234 up->port.icount.tx++;
235 if (uart_circ_empty(xmit))
236 break;
237 } while (--count > 0);
238
239 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
240 uart_write_wakeup(&up->port);
241
242 if (uart_circ_empty(xmit))
243 serial_omap_stop_tx(&up->port);
244}
245
246static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
247{
248 if (!(up->ier & UART_IER_THRI)) {
249 up->ier |= UART_IER_THRI;
250 serial_out(up, UART_IER, up->ier);
251 }
252}
253
254static void serial_omap_start_tx(struct uart_port *port)
255{
256 struct uart_omap_port *up = (struct uart_omap_port *)port;
257 struct circ_buf *xmit;
258 unsigned int start;
259 int ret = 0;
260
261 if (!up->use_dma) {
262 serial_omap_enable_ier_thri(up);
263 return;
264 }
265
266 if (up->uart_dma.tx_dma_used)
267 return;
268
269 xmit = &up->port.state->xmit;
270
271 if (up->uart_dma.tx_dma_channel == OMAP_UART_DMA_CH_FREE) {
272 ret = omap_request_dma(up->uart_dma.uart_dma_tx,
273 "UART Tx DMA",
274 (void *)uart_tx_dma_callback, up,
275 &(up->uart_dma.tx_dma_channel));
276
277 if (ret < 0) {
278 serial_omap_enable_ier_thri(up);
279 return;
280 }
281 }
282 spin_lock(&(up->uart_dma.tx_lock));
283 up->uart_dma.tx_dma_used = true;
284 spin_unlock(&(up->uart_dma.tx_lock));
285
286 start = up->uart_dma.tx_buf_dma_phys +
287 (xmit->tail & (UART_XMIT_SIZE - 1));
288
289 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
290 /*
291 * It is a circular buffer. See if the buffer has wounded back.
292 * If yes it will have to be transferred in two separate dma
293 * transfers
294 */
295 if (start + up->uart_dma.tx_buf_size >=
296 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
297 up->uart_dma.tx_buf_size =
298 (up->uart_dma.tx_buf_dma_phys +
299 UART_XMIT_SIZE) - start;
300
301 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
302 OMAP_DMA_AMODE_CONSTANT,
303 up->uart_dma.uart_base, 0, 0);
304 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
305 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
306 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
307 OMAP_DMA_DATA_TYPE_S8,
308 up->uart_dma.tx_buf_size, 1,
309 OMAP_DMA_SYNC_ELEMENT,
310 up->uart_dma.uart_dma_tx, 0);
311 /* FIXME: Cache maintenance needed here? */
312 omap_start_dma(up->uart_dma.tx_dma_channel);
313}
314
315static unsigned int check_modem_status(struct uart_omap_port *up)
316{
317 unsigned int status;
318
319 status = serial_in(up, UART_MSR);
320 status |= up->msr_saved_flags;
321 up->msr_saved_flags = 0;
322 if ((status & UART_MSR_ANY_DELTA) == 0)
323 return status;
324
325 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
326 up->port.state != NULL) {
327 if (status & UART_MSR_TERI)
328 up->port.icount.rng++;
329 if (status & UART_MSR_DDSR)
330 up->port.icount.dsr++;
331 if (status & UART_MSR_DDCD)
332 uart_handle_dcd_change
333 (&up->port, status & UART_MSR_DCD);
334 if (status & UART_MSR_DCTS)
335 uart_handle_cts_change
336 (&up->port, status & UART_MSR_CTS);
337 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
338 }
339
340 return status;
341}
342
343/**
344 * serial_omap_irq() - This handles the interrupt from one port
345 * @irq: uart port irq number
346 * @dev_id: uart port info
347 */
348static inline irqreturn_t serial_omap_irq(int irq, void *dev_id)
349{
350 struct uart_omap_port *up = dev_id;
351 unsigned int iir, lsr;
352 unsigned long flags;
353
354 iir = serial_in(up, UART_IIR);
355 if (iir & UART_IIR_NO_INT)
356 return IRQ_NONE;
357
358 spin_lock_irqsave(&up->port.lock, flags);
359 lsr = serial_in(up, UART_LSR);
360 if (iir & UART_IIR_RLSI) {
361 if (!up->use_dma) {
362 if (lsr & UART_LSR_DR)
363 receive_chars(up, &lsr);
364 } else {
365 up->ier &= ~(UART_IER_RDI | UART_IER_RLSI);
366 serial_out(up, UART_IER, up->ier);
367 if ((serial_omap_start_rxdma(up) != 0) &&
368 (lsr & UART_LSR_DR))
369 receive_chars(up, &lsr);
370 }
371 }
372
373 check_modem_status(up);
374 if ((lsr & UART_LSR_THRE) && (iir & UART_IIR_THRI))
375 transmit_chars(up);
376
377 spin_unlock_irqrestore(&up->port.lock, flags);
378 up->port_activity = jiffies;
379 return IRQ_HANDLED;
380}
381
382static unsigned int serial_omap_tx_empty(struct uart_port *port)
383{
384 struct uart_omap_port *up = (struct uart_omap_port *)port;
385 unsigned long flags = 0;
386 unsigned int ret = 0;
387
388 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->pdev->id);
389 spin_lock_irqsave(&up->port.lock, flags);
390 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
391 spin_unlock_irqrestore(&up->port.lock, flags);
392
393 return ret;
394}
395
396static unsigned int serial_omap_get_mctrl(struct uart_port *port)
397{
398 struct uart_omap_port *up = (struct uart_omap_port *)port;
399 unsigned char status;
400 unsigned int ret = 0;
401
402 status = check_modem_status(up);
403 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->pdev->id);
404
405 if (status & UART_MSR_DCD)
406 ret |= TIOCM_CAR;
407 if (status & UART_MSR_RI)
408 ret |= TIOCM_RNG;
409 if (status & UART_MSR_DSR)
410 ret |= TIOCM_DSR;
411 if (status & UART_MSR_CTS)
412 ret |= TIOCM_CTS;
413 return ret;
414}
415
416static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
417{
418 struct uart_omap_port *up = (struct uart_omap_port *)port;
419 unsigned char mcr = 0;
420
421 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->pdev->id);
422 if (mctrl & TIOCM_RTS)
423 mcr |= UART_MCR_RTS;
424 if (mctrl & TIOCM_DTR)
425 mcr |= UART_MCR_DTR;
426 if (mctrl & TIOCM_OUT1)
427 mcr |= UART_MCR_OUT1;
428 if (mctrl & TIOCM_OUT2)
429 mcr |= UART_MCR_OUT2;
430 if (mctrl & TIOCM_LOOP)
431 mcr |= UART_MCR_LOOP;
432
433 mcr |= up->mcr;
434 serial_out(up, UART_MCR, mcr);
435}
436
437static void serial_omap_break_ctl(struct uart_port *port, int break_state)
438{
439 struct uart_omap_port *up = (struct uart_omap_port *)port;
440 unsigned long flags = 0;
441
442 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->pdev->id);
443 spin_lock_irqsave(&up->port.lock, flags);
444 if (break_state == -1)
445 up->lcr |= UART_LCR_SBC;
446 else
447 up->lcr &= ~UART_LCR_SBC;
448 serial_out(up, UART_LCR, up->lcr);
449 spin_unlock_irqrestore(&up->port.lock, flags);
450}
451
452static int serial_omap_startup(struct uart_port *port)
453{
454 struct uart_omap_port *up = (struct uart_omap_port *)port;
455 unsigned long flags = 0;
456 int retval;
457
458 /*
459 * Allocate the IRQ
460 */
461 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
462 up->name, up);
463 if (retval)
464 return retval;
465
466 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->pdev->id);
467
468 /*
469 * Clear the FIFO buffers and disable them.
470 * (they will be reenabled in set_termios())
471 */
472 serial_omap_clear_fifos(up);
473 /* For Hardware flow control */
474 serial_out(up, UART_MCR, UART_MCR_RTS);
475
476 /*
477 * Clear the interrupt registers.
478 */
479 (void) serial_in(up, UART_LSR);
480 if (serial_in(up, UART_LSR) & UART_LSR_DR)
481 (void) serial_in(up, UART_RX);
482 (void) serial_in(up, UART_IIR);
483 (void) serial_in(up, UART_MSR);
484
485 /*
486 * Now, initialize the UART
487 */
488 serial_out(up, UART_LCR, UART_LCR_WLEN8);
489 spin_lock_irqsave(&up->port.lock, flags);
490 /*
491 * Most PC uarts need OUT2 raised to enable interrupts.
492 */
493 up->port.mctrl |= TIOCM_OUT2;
494 serial_omap_set_mctrl(&up->port, up->port.mctrl);
495 spin_unlock_irqrestore(&up->port.lock, flags);
496
497 up->msr_saved_flags = 0;
498 if (up->use_dma) {
499 free_page((unsigned long)up->port.state->xmit.buf);
500 up->port.state->xmit.buf = dma_alloc_coherent(NULL,
501 UART_XMIT_SIZE,
502 (dma_addr_t *)&(up->uart_dma.tx_buf_dma_phys),
503 0);
504 init_timer(&(up->uart_dma.rx_timer));
505 up->uart_dma.rx_timer.function = serial_omap_rx_timeout;
506 up->uart_dma.rx_timer.data = up->pdev->id;
507 /* Currently the buffer size is 4KB. Can increase it */
508 up->uart_dma.rx_buf = dma_alloc_coherent(NULL,
509 up->uart_dma.rx_buf_size,
510 (dma_addr_t *)&(up->uart_dma.rx_buf_dma_phys), 0);
511 }
512 /*
513 * Finally, enable interrupts. Note: Modem status interrupts
514 * are set via set_termios(), which will be occurring imminently
515 * anyway, so we don't enable them here.
516 */
517 up->ier = UART_IER_RLSI | UART_IER_RDI;
518 serial_out(up, UART_IER, up->ier);
519
520 up->port_activity = jiffies;
521 return 0;
522}
523
524static void serial_omap_shutdown(struct uart_port *port)
525{
526 struct uart_omap_port *up = (struct uart_omap_port *)port;
527 unsigned long flags = 0;
528
529 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->pdev->id);
530 /*
531 * Disable interrupts from this port
532 */
533 up->ier = 0;
534 serial_out(up, UART_IER, 0);
535
536 spin_lock_irqsave(&up->port.lock, flags);
537 up->port.mctrl &= ~TIOCM_OUT2;
538 serial_omap_set_mctrl(&up->port, up->port.mctrl);
539 spin_unlock_irqrestore(&up->port.lock, flags);
540
541 /*
542 * Disable break condition and FIFOs
543 */
544 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
545 serial_omap_clear_fifos(up);
546
547 /*
548 * Read data port to reset things, and then free the irq
549 */
550 if (serial_in(up, UART_LSR) & UART_LSR_DR)
551 (void) serial_in(up, UART_RX);
552 if (up->use_dma) {
553 dma_free_coherent(up->port.dev,
554 UART_XMIT_SIZE, up->port.state->xmit.buf,
555 up->uart_dma.tx_buf_dma_phys);
556 up->port.state->xmit.buf = NULL;
557 serial_omap_stop_rx(port);
558 dma_free_coherent(up->port.dev,
559 up->uart_dma.rx_buf_size, up->uart_dma.rx_buf,
560 up->uart_dma.rx_buf_dma_phys);
561 up->uart_dma.rx_buf = NULL;
562 }
563 free_irq(up->port.irq, up);
564}
565
566static inline void
567serial_omap_configure_xonxoff
568 (struct uart_omap_port *up, struct ktermios *termios)
569{
570 unsigned char efr = 0;
571
572 up->lcr = serial_in(up, UART_LCR);
573 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
574 up->efr = serial_in(up, UART_EFR);
575 serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);
576
577 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
578 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
579
580 /* clear SW control mode bits */
581 efr = up->efr;
582 efr &= OMAP_UART_SW_CLR;
583
584 /*
585 * IXON Flag:
586 * Enable XON/XOFF flow control on output.
587 * Transmit XON1, XOFF1
588 */
589 if (termios->c_iflag & IXON)
590 efr |= OMAP_UART_SW_TX;
591
592 /*
593 * IXOFF Flag:
594 * Enable XON/XOFF flow control on input.
595 * Receiver compares XON1, XOFF1.
596 */
597 if (termios->c_iflag & IXOFF)
598 efr |= OMAP_UART_SW_RX;
599
600 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
601 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
602
603 up->mcr = serial_in(up, UART_MCR);
604
605 /*
606 * IXANY Flag:
607 * Enable any character to restart output.
608 * Operation resumes after receiving any
609 * character after recognition of the XOFF character
610 */
611 if (termios->c_iflag & IXANY)
612 up->mcr |= UART_MCR_XONANY;
613
614 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
615 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
616 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
617 /* Enable special char function UARTi.EFR_REG[5] and
618 * load the new software flow control mode IXON or IXOFF
619 * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
620 */
621 serial_out(up, UART_EFR, efr | UART_EFR_SCD);
622 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
623
624 serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
625 serial_out(up, UART_LCR, up->lcr);
626}
627
628static void
629serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
630 struct ktermios *old)
631{
632 struct uart_omap_port *up = (struct uart_omap_port *)port;
633 unsigned char cval = 0;
634 unsigned char efr = 0;
635 unsigned long flags = 0;
636 unsigned int baud, quot;
637
638 switch (termios->c_cflag & CSIZE) {
639 case CS5:
640 cval = UART_LCR_WLEN5;
641 break;
642 case CS6:
643 cval = UART_LCR_WLEN6;
644 break;
645 case CS7:
646 cval = UART_LCR_WLEN7;
647 break;
648 default:
649 case CS8:
650 cval = UART_LCR_WLEN8;
651 break;
652 }
653
654 if (termios->c_cflag & CSTOPB)
655 cval |= UART_LCR_STOP;
656 if (termios->c_cflag & PARENB)
657 cval |= UART_LCR_PARITY;
658 if (!(termios->c_cflag & PARODD))
659 cval |= UART_LCR_EPAR;
660
661 /*
662 * Ask the core to calculate the divisor for us.
663 */
664
665 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
666 quot = serial_omap_get_divisor(port, baud);
667
668 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
669 UART_FCR_ENABLE_FIFO;
670 if (up->use_dma)
671 up->fcr |= UART_FCR_DMA_SELECT;
672
673 /*
674 * Ok, we're now changing the port state. Do it with
675 * interrupts disabled.
676 */
677 spin_lock_irqsave(&up->port.lock, flags);
678
679 /*
680 * Update the per-port timeout.
681 */
682 uart_update_timeout(port, termios->c_cflag, baud);
683
684 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
685 if (termios->c_iflag & INPCK)
686 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
687 if (termios->c_iflag & (BRKINT | PARMRK))
688 up->port.read_status_mask |= UART_LSR_BI;
689
690 /*
691 * Characters to ignore
692 */
693 up->port.ignore_status_mask = 0;
694 if (termios->c_iflag & IGNPAR)
695 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
696 if (termios->c_iflag & IGNBRK) {
697 up->port.ignore_status_mask |= UART_LSR_BI;
698 /*
699 * If we're ignoring parity and break indicators,
700 * ignore overruns too (for real raw support).
701 */
702 if (termios->c_iflag & IGNPAR)
703 up->port.ignore_status_mask |= UART_LSR_OE;
704 }
705
706 /*
707 * ignore all characters if CREAD is not set
708 */
709 if ((termios->c_cflag & CREAD) == 0)
710 up->port.ignore_status_mask |= UART_LSR_DR;
711
712 /*
713 * Modem status interrupts
714 */
715 up->ier &= ~UART_IER_MSI;
716 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
717 up->ier |= UART_IER_MSI;
718 serial_out(up, UART_IER, up->ier);
719 serial_out(up, UART_LCR, cval); /* reset DLAB */
720
721 /* FIFOs and DMA Settings */
722
723 /* FCR can be changed only when the
724 * baud clock is not running
725 * DLL_REG and DLH_REG set to 0.
726 */
727 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
728 serial_out(up, UART_DLL, 0);
729 serial_out(up, UART_DLM, 0);
730 serial_out(up, UART_LCR, 0);
731
732 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
733
734 up->efr = serial_in(up, UART_EFR);
735 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
736
737 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
738 up->mcr = serial_in(up, UART_MCR);
739 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
740 /* FIFO ENABLE, DMA MODE */
741 serial_out(up, UART_FCR, up->fcr);
742 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
743
744 if (up->use_dma) {
745 serial_out(up, UART_TI752_TLR, 0);
746 serial_out(up, UART_OMAP_SCR,
747 (UART_FCR_TRIGGER_4 | UART_FCR_TRIGGER_8));
748 }
749
750 serial_out(up, UART_EFR, up->efr);
751 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
752 serial_out(up, UART_MCR, up->mcr);
753
754 /* Protocol, Baud Rate, and Interrupt Settings */
755
756 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
757 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
758
759 up->efr = serial_in(up, UART_EFR);
760 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
761
762 serial_out(up, UART_LCR, 0);
763 serial_out(up, UART_IER, 0);
764 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
765
766 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
767 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
768
769 serial_out(up, UART_LCR, 0);
770 serial_out(up, UART_IER, up->ier);
771 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
772
773 serial_out(up, UART_EFR, up->efr);
774 serial_out(up, UART_LCR, cval);
775
776 if (baud > 230400 && baud != 3000000)
777 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_13X_MODE);
778 else
779 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
780
781 /* Hardware Flow Control Configuration */
782
783 if (termios->c_cflag & CRTSCTS) {
784 efr |= (UART_EFR_CTS | UART_EFR_RTS);
785 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
786
787 up->mcr = serial_in(up, UART_MCR);
788 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
789
790 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
791 up->efr = serial_in(up, UART_EFR);
792 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
793
794 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
795 serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
796 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
797 serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
798 serial_out(up, UART_LCR, cval);
799 }
800
801 serial_omap_set_mctrl(&up->port, up->port.mctrl);
802 /* Software Flow Control Configuration */
803 if (termios->c_iflag & (IXON | IXOFF))
804 serial_omap_configure_xonxoff(up, termios);
805
806 spin_unlock_irqrestore(&up->port.lock, flags);
807 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->pdev->id);
808}
809
810static void
811serial_omap_pm(struct uart_port *port, unsigned int state,
812 unsigned int oldstate)
813{
814 struct uart_omap_port *up = (struct uart_omap_port *)port;
815 unsigned char efr;
816
817 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->pdev->id);
818 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
819 efr = serial_in(up, UART_EFR);
820 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
821 serial_out(up, UART_LCR, 0);
822
823 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
824 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
825 serial_out(up, UART_EFR, efr);
826 serial_out(up, UART_LCR, 0);
827 /* Enable module level wake up */
828 serial_out(up, UART_OMAP_WER,
829 (state != 0) ? OMAP_UART_WER_MOD_WKUP : 0);
830}
831
832static void serial_omap_release_port(struct uart_port *port)
833{
834 dev_dbg(port->dev, "serial_omap_release_port+\n");
835}
836
837static int serial_omap_request_port(struct uart_port *port)
838{
839 dev_dbg(port->dev, "serial_omap_request_port+\n");
840 return 0;
841}
842
843static void serial_omap_config_port(struct uart_port *port, int flags)
844{
845 struct uart_omap_port *up = (struct uart_omap_port *)port;
846
847 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
848 up->pdev->id);
849 up->port.type = PORT_OMAP;
850}
851
852static int
853serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
854{
855 /* we don't want the core code to modify any port params */
856 dev_dbg(port->dev, "serial_omap_verify_port+\n");
857 return -EINVAL;
858}
859
860static const char *
861serial_omap_type(struct uart_port *port)
862{
863 struct uart_omap_port *up = (struct uart_omap_port *)port;
864
865 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->pdev->id);
866 return up->name;
867}
868
869#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
870
871static inline void wait_for_xmitr(struct uart_omap_port *up)
872{
873 unsigned int status, tmout = 10000;
874
875 /* Wait up to 10ms for the character(s) to be sent. */
876 do {
877 status = serial_in(up, UART_LSR);
878
879 if (status & UART_LSR_BI)
880 up->lsr_break_flag = UART_LSR_BI;
881
882 if (--tmout == 0)
883 break;
884 udelay(1);
885 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
886
887 /* Wait up to 1s for flow control if necessary */
888 if (up->port.flags & UPF_CONS_FLOW) {
889 tmout = 1000000;
890 for (tmout = 1000000; tmout; tmout--) {
891 unsigned int msr = serial_in(up, UART_MSR);
892
893 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
894 if (msr & UART_MSR_CTS)
895 break;
896
897 udelay(1);
898 }
899 }
900}
901
902#ifdef CONFIG_CONSOLE_POLL
903
904static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
905{
906 struct uart_omap_port *up = (struct uart_omap_port *)port;
907 wait_for_xmitr(up);
908 serial_out(up, UART_TX, ch);
909}
910
911static int serial_omap_poll_get_char(struct uart_port *port)
912{
913 struct uart_omap_port *up = (struct uart_omap_port *)port;
914 unsigned int status = serial_in(up, UART_LSR);
915
916 if (!(status & UART_LSR_DR))
917 return NO_POLL_CHAR;
918
919 return serial_in(up, UART_RX);
920}
921
922#endif /* CONFIG_CONSOLE_POLL */
923
924#ifdef CONFIG_SERIAL_OMAP_CONSOLE
925
926static struct uart_omap_port *serial_omap_console_ports[4];
927
928static struct uart_driver serial_omap_reg;
929
930static void serial_omap_console_putchar(struct uart_port *port, int ch)
931{
932 struct uart_omap_port *up = (struct uart_omap_port *)port;
933
934 wait_for_xmitr(up);
935 serial_out(up, UART_TX, ch);
936}
937
938static void
939serial_omap_console_write(struct console *co, const char *s,
940 unsigned int count)
941{
942 struct uart_omap_port *up = serial_omap_console_ports[co->index];
943 unsigned long flags;
944 unsigned int ier;
945 int locked = 1;
946
947 local_irq_save(flags);
948 if (up->port.sysrq)
949 locked = 0;
950 else if (oops_in_progress)
951 locked = spin_trylock(&up->port.lock);
952 else
953 spin_lock(&up->port.lock);
954
955 /*
956 * First save the IER then disable the interrupts
957 */
958 ier = serial_in(up, UART_IER);
959 serial_out(up, UART_IER, 0);
960
961 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
962
963 /*
964 * Finally, wait for transmitter to become empty
965 * and restore the IER
966 */
967 wait_for_xmitr(up);
968 serial_out(up, UART_IER, ier);
969 /*
970 * The receive handling will happen properly because the
971 * receive ready bit will still be set; it is not cleared
972 * on read. However, modem control will not, we must
973 * call it if we have saved something in the saved flags
974 * while processing with interrupts off.
975 */
976 if (up->msr_saved_flags)
977 check_modem_status(up);
978
979 if (locked)
980 spin_unlock(&up->port.lock);
981 local_irq_restore(flags);
982}
983
984static int __init
985serial_omap_console_setup(struct console *co, char *options)
986{
987 struct uart_omap_port *up;
988 int baud = 115200;
989 int bits = 8;
990 int parity = 'n';
991 int flow = 'n';
992
993 if (serial_omap_console_ports[co->index] == NULL)
994 return -ENODEV;
995 up = serial_omap_console_ports[co->index];
996
997 if (options)
998 uart_parse_options(options, &baud, &parity, &bits, &flow);
999
1000 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1001}
1002
1003static struct console serial_omap_console = {
1004 .name = OMAP_SERIAL_NAME,
1005 .write = serial_omap_console_write,
1006 .device = uart_console_device,
1007 .setup = serial_omap_console_setup,
1008 .flags = CON_PRINTBUFFER,
1009 .index = -1,
1010 .data = &serial_omap_reg,
1011};
1012
1013static void serial_omap_add_console_port(struct uart_omap_port *up)
1014{
1015 serial_omap_console_ports[up->pdev->id] = up;
1016}
1017
1018#define OMAP_CONSOLE (&serial_omap_console)
1019
1020#else
1021
1022#define OMAP_CONSOLE NULL
1023
1024static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1025{}
1026
1027#endif
1028
1029static struct uart_ops serial_omap_pops = {
1030 .tx_empty = serial_omap_tx_empty,
1031 .set_mctrl = serial_omap_set_mctrl,
1032 .get_mctrl = serial_omap_get_mctrl,
1033 .stop_tx = serial_omap_stop_tx,
1034 .start_tx = serial_omap_start_tx,
1035 .stop_rx = serial_omap_stop_rx,
1036 .enable_ms = serial_omap_enable_ms,
1037 .break_ctl = serial_omap_break_ctl,
1038 .startup = serial_omap_startup,
1039 .shutdown = serial_omap_shutdown,
1040 .set_termios = serial_omap_set_termios,
1041 .pm = serial_omap_pm,
1042 .type = serial_omap_type,
1043 .release_port = serial_omap_release_port,
1044 .request_port = serial_omap_request_port,
1045 .config_port = serial_omap_config_port,
1046 .verify_port = serial_omap_verify_port,
1047#ifdef CONFIG_CONSOLE_POLL
1048 .poll_put_char = serial_omap_poll_put_char,
1049 .poll_get_char = serial_omap_poll_get_char,
1050#endif
1051};
1052
1053static struct uart_driver serial_omap_reg = {
1054 .owner = THIS_MODULE,
1055 .driver_name = "OMAP-SERIAL",
1056 .dev_name = OMAP_SERIAL_NAME,
1057 .nr = OMAP_MAX_HSUART_PORTS,
1058 .cons = OMAP_CONSOLE,
1059};
1060
1061static int
1062serial_omap_suspend(struct platform_device *pdev, pm_message_t state)
1063{
1064 struct uart_omap_port *up = platform_get_drvdata(pdev);
1065
1066 if (up)
1067 uart_suspend_port(&serial_omap_reg, &up->port);
1068 return 0;
1069}
1070
1071static int serial_omap_resume(struct platform_device *dev)
1072{
1073 struct uart_omap_port *up = platform_get_drvdata(dev);
1074
1075 if (up)
1076 uart_resume_port(&serial_omap_reg, &up->port);
1077 return 0;
1078}
1079
1080static void serial_omap_rx_timeout(unsigned long uart_no)
1081{
1082 struct uart_omap_port *up = ui[uart_no];
1083 unsigned int curr_dma_pos, curr_transmitted_size;
1084 int ret = 0;
1085
1086 curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
1087 if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
1088 (curr_dma_pos == 0)) {
1089 if (jiffies_to_msecs(jiffies - up->port_activity) <
1090 RX_TIMEOUT) {
1091 mod_timer(&up->uart_dma.rx_timer, jiffies +
1092 usecs_to_jiffies(up->uart_dma.rx_timeout));
1093 } else {
1094 serial_omap_stop_rxdma(up);
1095 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1096 serial_out(up, UART_IER, up->ier);
1097 }
1098 return;
1099 }
1100
1101 curr_transmitted_size = curr_dma_pos -
1102 up->uart_dma.prev_rx_dma_pos;
1103 up->port.icount.rx += curr_transmitted_size;
1104 tty_insert_flip_string(up->port.state->port.tty,
1105 up->uart_dma.rx_buf +
1106 (up->uart_dma.prev_rx_dma_pos -
1107 up->uart_dma.rx_buf_dma_phys),
1108 curr_transmitted_size);
1109 tty_flip_buffer_push(up->port.state->port.tty);
1110 up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
1111 if (up->uart_dma.rx_buf_size +
1112 up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
1113 ret = serial_omap_start_rxdma(up);
1114 if (ret < 0) {
1115 serial_omap_stop_rxdma(up);
1116 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1117 serial_out(up, UART_IER, up->ier);
1118 }
1119 } else {
1120 mod_timer(&up->uart_dma.rx_timer, jiffies +
1121 usecs_to_jiffies(up->uart_dma.rx_timeout));
1122 }
1123 up->port_activity = jiffies;
1124}
1125
1126static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
1127{
1128 return;
1129}
1130
1131static int serial_omap_start_rxdma(struct uart_omap_port *up)
1132{
1133 int ret = 0;
1134
1135 if (up->uart_dma.rx_dma_channel == -1) {
1136 ret = omap_request_dma(up->uart_dma.uart_dma_rx,
1137 "UART Rx DMA",
1138 (void *)uart_rx_dma_callback, up,
1139 &(up->uart_dma.rx_dma_channel));
1140 if (ret < 0)
1141 return ret;
1142
1143 omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
1144 OMAP_DMA_AMODE_CONSTANT,
1145 up->uart_dma.uart_base, 0, 0);
1146 omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
1147 OMAP_DMA_AMODE_POST_INC,
1148 up->uart_dma.rx_buf_dma_phys, 0, 0);
1149 omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
1150 OMAP_DMA_DATA_TYPE_S8,
1151 up->uart_dma.rx_buf_size, 1,
1152 OMAP_DMA_SYNC_ELEMENT,
1153 up->uart_dma.uart_dma_rx, 0);
1154 }
1155 up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
1156 /* FIXME: Cache maintenance needed here? */
1157 omap_start_dma(up->uart_dma.rx_dma_channel);
1158 mod_timer(&up->uart_dma.rx_timer, jiffies +
1159 usecs_to_jiffies(up->uart_dma.rx_timeout));
1160 up->uart_dma.rx_dma_used = true;
1161 return ret;
1162}
1163
1164static void serial_omap_continue_tx(struct uart_omap_port *up)
1165{
1166 struct circ_buf *xmit = &up->port.state->xmit;
1167 unsigned int start = up->uart_dma.tx_buf_dma_phys
1168 + (xmit->tail & (UART_XMIT_SIZE - 1));
1169
1170 if (uart_circ_empty(xmit))
1171 return;
1172
1173 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
1174 /*
1175 * It is a circular buffer. See if the buffer has wounded back.
1176 * If yes it will have to be transferred in two separate dma
1177 * transfers
1178 */
1179 if (start + up->uart_dma.tx_buf_size >=
1180 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
1181 up->uart_dma.tx_buf_size =
1182 (up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
1183 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
1184 OMAP_DMA_AMODE_CONSTANT,
1185 up->uart_dma.uart_base, 0, 0);
1186 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
1187 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
1188 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
1189 OMAP_DMA_DATA_TYPE_S8,
1190 up->uart_dma.tx_buf_size, 1,
1191 OMAP_DMA_SYNC_ELEMENT,
1192 up->uart_dma.uart_dma_tx, 0);
1193 /* FIXME: Cache maintenance needed here? */
1194 omap_start_dma(up->uart_dma.tx_dma_channel);
1195}
1196
1197static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
1198{
1199 struct uart_omap_port *up = (struct uart_omap_port *)data;
1200 struct circ_buf *xmit = &up->port.state->xmit;
1201
1202 xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
1203 (UART_XMIT_SIZE - 1);
1204 up->port.icount.tx += up->uart_dma.tx_buf_size;
1205
1206 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1207 uart_write_wakeup(&up->port);
1208
1209 if (uart_circ_empty(xmit)) {
1210 spin_lock(&(up->uart_dma.tx_lock));
1211 serial_omap_stop_tx(&up->port);
1212 up->uart_dma.tx_dma_used = false;
1213 spin_unlock(&(up->uart_dma.tx_lock));
1214 } else {
1215 omap_stop_dma(up->uart_dma.tx_dma_channel);
1216 serial_omap_continue_tx(up);
1217 }
1218 up->port_activity = jiffies;
1219 return;
1220}
1221
1222static int serial_omap_probe(struct platform_device *pdev)
1223{
1224 struct uart_omap_port *up;
1225 struct resource *mem, *irq, *dma_tx, *dma_rx;
1226 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
1227 int ret = -ENOSPC;
1228
1229 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1230 if (!mem) {
1231 dev_err(&pdev->dev, "no mem resource?\n");
1232 return -ENODEV;
1233 }
1234
1235 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1236 if (!irq) {
1237 dev_err(&pdev->dev, "no irq resource?\n");
1238 return -ENODEV;
1239 }
1240
1241 if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
1242 pdev->dev.driver->name)) {
1243 dev_err(&pdev->dev, "memory region already claimed\n");
1244 return -EBUSY;
1245 }
1246
1247 dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1248 if (!dma_rx) {
1249 ret = -EINVAL;
1250 goto err;
1251 }
1252
1253 dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1254 if (!dma_tx) {
1255 ret = -EINVAL;
1256 goto err;
1257 }
1258
1259 up = kzalloc(sizeof(*up), GFP_KERNEL);
1260 if (up == NULL) {
1261 ret = -ENOMEM;
1262 goto do_release_region;
1263 }
1264 sprintf(up->name, "OMAP UART%d", pdev->id);
1265 up->pdev = pdev;
1266 up->port.dev = &pdev->dev;
1267 up->port.type = PORT_OMAP;
1268 up->port.iotype = UPIO_MEM;
1269 up->port.irq = irq->start;
1270
1271 up->port.regshift = 2;
1272 up->port.fifosize = 64;
1273 up->port.ops = &serial_omap_pops;
1274 up->port.line = pdev->id;
1275
1276 up->port.membase = omap_up_info->membase;
1277 up->port.mapbase = omap_up_info->mapbase;
1278 up->port.flags = omap_up_info->flags;
1279 up->port.irqflags = omap_up_info->irqflags;
1280 up->port.uartclk = omap_up_info->uartclk;
1281 up->uart_dma.uart_base = mem->start;
1282
1283 if (omap_up_info->dma_enabled) {
1284 up->uart_dma.uart_dma_tx = dma_tx->start;
1285 up->uart_dma.uart_dma_rx = dma_rx->start;
1286 up->use_dma = 1;
1287 up->uart_dma.rx_buf_size = 4096;
1288 up->uart_dma.rx_timeout = 2;
1289 spin_lock_init(&(up->uart_dma.tx_lock));
1290 spin_lock_init(&(up->uart_dma.rx_lock));
1291 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
1292 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
1293 }
1294
1295 ui[pdev->id] = up;
1296 serial_omap_add_console_port(up);
1297
1298 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1299 if (ret != 0)
1300 goto do_release_region;
1301
1302 platform_set_drvdata(pdev, up);
1303 return 0;
1304err:
1305 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1306 pdev->id, __func__, ret);
1307do_release_region:
1308 release_mem_region(mem->start, (mem->end - mem->start) + 1);
1309 return ret;
1310}
1311
1312static int serial_omap_remove(struct platform_device *dev)
1313{
1314 struct uart_omap_port *up = platform_get_drvdata(dev);
1315
1316 platform_set_drvdata(dev, NULL);
1317 if (up) {
1318 uart_remove_one_port(&serial_omap_reg, &up->port);
1319 kfree(up);
1320 }
1321 return 0;
1322}
1323
1324static struct platform_driver serial_omap_driver = {
1325 .probe = serial_omap_probe,
1326 .remove = serial_omap_remove,
1327
1328 .suspend = serial_omap_suspend,
1329 .resume = serial_omap_resume,
1330 .driver = {
1331 .name = DRIVER_NAME,
1332 },
1333};
1334
1335static int __init serial_omap_init(void)
1336{
1337 int ret;
1338
1339 ret = uart_register_driver(&serial_omap_reg);
1340 if (ret != 0)
1341 return ret;
1342 ret = platform_driver_register(&serial_omap_driver);
1343 if (ret != 0)
1344 uart_unregister_driver(&serial_omap_reg);
1345 return ret;
1346}
1347
1348static void __exit serial_omap_exit(void)
1349{
1350 platform_driver_unregister(&serial_omap_driver);
1351 uart_unregister_driver(&serial_omap_reg);
1352}
1353
1354module_init(serial_omap_init);
1355module_exit(serial_omap_exit);
1356
1357MODULE_DESCRIPTION("OMAP High Speed UART driver");
1358MODULE_LICENSE("GPL");
1359MODULE_AUTHOR("Texas Instruments Inc");
diff --git a/drivers/tty/serial/pch_uart.c b/drivers/tty/serial/pch_uart.c
new file mode 100644
index 000000000000..70a61458ec42
--- /dev/null
+++ b/drivers/tty/serial/pch_uart.c
@@ -0,0 +1,1451 @@
1/*
2 *Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
3 *
4 *This program is free software; you can redistribute it and/or modify
5 *it under the terms of the GNU General Public License as published by
6 *the Free Software Foundation; version 2 of the License.
7 *
8 *This program is distributed in the hope that it will be useful,
9 *but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 *GNU General Public License for more details.
12 *
13 *You should have received a copy of the GNU General Public License
14 *along with this program; if not, write to the Free Software
15 *Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 */
17#include <linux/serial_reg.h>
18#include <linux/pci.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21#include <linux/serial_core.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24
25#include <linux/dmaengine.h>
26#include <linux/pch_dma.h>
27
28enum {
29 PCH_UART_HANDLED_RX_INT_SHIFT,
30 PCH_UART_HANDLED_TX_INT_SHIFT,
31 PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
32 PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
33 PCH_UART_HANDLED_MS_INT_SHIFT,
34};
35
36enum {
37 PCH_UART_8LINE,
38 PCH_UART_2LINE,
39};
40
41#define PCH_UART_DRIVER_DEVICE "ttyPCH"
42
43#define PCH_UART_NR_GE_256FIFO 1
44#define PCH_UART_NR_GE_64FIFO 3
45#define PCH_UART_NR_GE (PCH_UART_NR_GE_256FIFO+PCH_UART_NR_GE_64FIFO)
46#define PCH_UART_NR PCH_UART_NR_GE
47
48#define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
49#define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
50#define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
51 PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
52#define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
53 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
54#define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
55
56#define PCH_UART_RBR 0x00
57#define PCH_UART_THR 0x00
58
59#define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
60 PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
61#define PCH_UART_IER_ERBFI 0x00000001
62#define PCH_UART_IER_ETBEI 0x00000002
63#define PCH_UART_IER_ELSI 0x00000004
64#define PCH_UART_IER_EDSSI 0x00000008
65
66#define PCH_UART_IIR_IP 0x00000001
67#define PCH_UART_IIR_IID 0x00000006
68#define PCH_UART_IIR_MSI 0x00000000
69#define PCH_UART_IIR_TRI 0x00000002
70#define PCH_UART_IIR_RRI 0x00000004
71#define PCH_UART_IIR_REI 0x00000006
72#define PCH_UART_IIR_TOI 0x00000008
73#define PCH_UART_IIR_FIFO256 0x00000020
74#define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
75#define PCH_UART_IIR_FE 0x000000C0
76
77#define PCH_UART_FCR_FIFOE 0x00000001
78#define PCH_UART_FCR_RFR 0x00000002
79#define PCH_UART_FCR_TFR 0x00000004
80#define PCH_UART_FCR_DMS 0x00000008
81#define PCH_UART_FCR_FIFO256 0x00000020
82#define PCH_UART_FCR_RFTL 0x000000C0
83
84#define PCH_UART_FCR_RFTL1 0x00000000
85#define PCH_UART_FCR_RFTL64 0x00000040
86#define PCH_UART_FCR_RFTL128 0x00000080
87#define PCH_UART_FCR_RFTL224 0x000000C0
88#define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
89#define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
90#define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
91#define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
92#define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
93#define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
94#define PCH_UART_FCR_RFTL_SHIFT 6
95
96#define PCH_UART_LCR_WLS 0x00000003
97#define PCH_UART_LCR_STB 0x00000004
98#define PCH_UART_LCR_PEN 0x00000008
99#define PCH_UART_LCR_EPS 0x00000010
100#define PCH_UART_LCR_SP 0x00000020
101#define PCH_UART_LCR_SB 0x00000040
102#define PCH_UART_LCR_DLAB 0x00000080
103#define PCH_UART_LCR_NP 0x00000000
104#define PCH_UART_LCR_OP PCH_UART_LCR_PEN
105#define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
106#define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
107#define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
108 PCH_UART_LCR_SP)
109
110#define PCH_UART_LCR_5BIT 0x00000000
111#define PCH_UART_LCR_6BIT 0x00000001
112#define PCH_UART_LCR_7BIT 0x00000002
113#define PCH_UART_LCR_8BIT 0x00000003
114
115#define PCH_UART_MCR_DTR 0x00000001
116#define PCH_UART_MCR_RTS 0x00000002
117#define PCH_UART_MCR_OUT 0x0000000C
118#define PCH_UART_MCR_LOOP 0x00000010
119#define PCH_UART_MCR_AFE 0x00000020
120
121#define PCH_UART_LSR_DR 0x00000001
122#define PCH_UART_LSR_ERR (1<<7)
123
124#define PCH_UART_MSR_DCTS 0x00000001
125#define PCH_UART_MSR_DDSR 0x00000002
126#define PCH_UART_MSR_TERI 0x00000004
127#define PCH_UART_MSR_DDCD 0x00000008
128#define PCH_UART_MSR_CTS 0x00000010
129#define PCH_UART_MSR_DSR 0x00000020
130#define PCH_UART_MSR_RI 0x00000040
131#define PCH_UART_MSR_DCD 0x00000080
132#define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
133 PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
134
135#define PCH_UART_DLL 0x00
136#define PCH_UART_DLM 0x01
137
138#define DIV_ROUND(a, b) (((a) + ((b)/2)) / (b))
139
140#define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
141#define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
142#define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
143#define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
144#define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
145
146#define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
147#define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
148#define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
149#define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
150#define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
151#define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
152#define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
153#define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
154#define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
155#define PCH_UART_HAL_STB1 0
156#define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
157
158#define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
159#define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
160#define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
161 PCH_UART_HAL_CLR_RX_FIFO)
162
163#define PCH_UART_HAL_DMA_MODE0 0
164#define PCH_UART_HAL_FIFO_DIS 0
165#define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
166#define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
167 PCH_UART_FCR_FIFO256)
168#define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
169#define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
170#define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
171#define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
172#define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
173#define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
174#define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
175#define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
176#define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
177#define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
178#define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
179#define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
180#define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
181#define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
182
183#define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
184#define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
185#define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
186#define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
187#define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
188
189#define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
190#define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
191#define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
192#define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
193#define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
194
195struct pch_uart_buffer {
196 unsigned char *buf;
197 int size;
198};
199
200struct eg20t_port {
201 struct uart_port port;
202 int port_type;
203 void __iomem *membase;
204 resource_size_t mapbase;
205 unsigned int iobase;
206 struct pci_dev *pdev;
207 int fifo_size;
208 int base_baud;
209 int start_tx;
210 int start_rx;
211 int tx_empty;
212 int int_dis_flag;
213 int trigger;
214 int trigger_level;
215 struct pch_uart_buffer rxbuf;
216 unsigned int dmsr;
217 unsigned int fcr;
218 unsigned int use_dma;
219 unsigned int use_dma_flag;
220 struct dma_async_tx_descriptor *desc_tx;
221 struct dma_async_tx_descriptor *desc_rx;
222 struct pch_dma_slave param_tx;
223 struct pch_dma_slave param_rx;
224 struct dma_chan *chan_tx;
225 struct dma_chan *chan_rx;
226 struct scatterlist sg_tx;
227 struct scatterlist sg_rx;
228 int tx_dma_use;
229 void *rx_buf_virt;
230 dma_addr_t rx_buf_dma;
231};
232
233static unsigned int default_baud = 9600;
234static const int trigger_level_256[4] = { 1, 64, 128, 224 };
235static const int trigger_level_64[4] = { 1, 16, 32, 56 };
236static const int trigger_level_16[4] = { 1, 4, 8, 14 };
237static const int trigger_level_1[4] = { 1, 1, 1, 1 };
238
239static void pch_uart_hal_request(struct pci_dev *pdev, int fifosize,
240 int base_baud)
241{
242 struct eg20t_port *priv = pci_get_drvdata(pdev);
243
244 priv->trigger_level = 1;
245 priv->fcr = 0;
246}
247
248static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
249{
250 unsigned int msr = ioread8(base + UART_MSR);
251 priv->dmsr |= msr & PCH_UART_MSR_DELTA;
252
253 return msr;
254}
255
256static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
257 unsigned int flag)
258{
259 u8 ier = ioread8(priv->membase + UART_IER);
260 ier |= flag & PCH_UART_IER_MASK;
261 iowrite8(ier, priv->membase + UART_IER);
262}
263
264static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
265 unsigned int flag)
266{
267 u8 ier = ioread8(priv->membase + UART_IER);
268 ier &= ~(flag & PCH_UART_IER_MASK);
269 iowrite8(ier, priv->membase + UART_IER);
270}
271
272static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
273 unsigned int parity, unsigned int bits,
274 unsigned int stb)
275{
276 unsigned int dll, dlm, lcr;
277 int div;
278
279 div = DIV_ROUND(priv->base_baud / 16, baud);
280 if (div < 0 || USHRT_MAX <= div) {
281 pr_err("Invalid Baud(div=0x%x)\n", div);
282 return -EINVAL;
283 }
284
285 dll = (unsigned int)div & 0x00FFU;
286 dlm = ((unsigned int)div >> 8) & 0x00FFU;
287
288 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
289 pr_err("Invalid parity(0x%x)\n", parity);
290 return -EINVAL;
291 }
292
293 if (bits & ~PCH_UART_LCR_WLS) {
294 pr_err("Invalid bits(0x%x)\n", bits);
295 return -EINVAL;
296 }
297
298 if (stb & ~PCH_UART_LCR_STB) {
299 pr_err("Invalid STB(0x%x)\n", stb);
300 return -EINVAL;
301 }
302
303 lcr = parity;
304 lcr |= bits;
305 lcr |= stb;
306
307 pr_debug("%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
308 __func__, baud, div, lcr, jiffies);
309 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
310 iowrite8(dll, priv->membase + PCH_UART_DLL);
311 iowrite8(dlm, priv->membase + PCH_UART_DLM);
312 iowrite8(lcr, priv->membase + UART_LCR);
313
314 return 0;
315}
316
317static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
318 unsigned int flag)
319{
320 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
321 pr_err("%s:Invalid flag(0x%x)\n", __func__, flag);
322 return -EINVAL;
323 }
324
325 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
326 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
327 priv->membase + UART_FCR);
328 iowrite8(priv->fcr, priv->membase + UART_FCR);
329
330 return 0;
331}
332
333static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
334 unsigned int dmamode,
335 unsigned int fifo_size, unsigned int trigger)
336{
337 u8 fcr;
338
339 if (dmamode & ~PCH_UART_FCR_DMS) {
340 pr_err("%s:Invalid DMA Mode(0x%x)\n", __func__, dmamode);
341 return -EINVAL;
342 }
343
344 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
345 pr_err("%s:Invalid FIFO SIZE(0x%x)\n", __func__, fifo_size);
346 return -EINVAL;
347 }
348
349 if (trigger & ~PCH_UART_FCR_RFTL) {
350 pr_err("%s:Invalid TRIGGER(0x%x)\n", __func__, trigger);
351 return -EINVAL;
352 }
353
354 switch (priv->fifo_size) {
355 case 256:
356 priv->trigger_level =
357 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
358 break;
359 case 64:
360 priv->trigger_level =
361 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
362 break;
363 case 16:
364 priv->trigger_level =
365 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
366 break;
367 default:
368 priv->trigger_level =
369 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
370 break;
371 }
372 fcr =
373 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
374 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
375 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
376 priv->membase + UART_FCR);
377 iowrite8(fcr, priv->membase + UART_FCR);
378 priv->fcr = fcr;
379
380 return 0;
381}
382
383static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
384{
385 priv->dmsr = 0;
386 return get_msr(priv, priv->membase);
387}
388
389static int pch_uart_hal_write(struct eg20t_port *priv,
390 const unsigned char *buf, int tx_size)
391{
392 int i;
393 unsigned int thr;
394
395 for (i = 0; i < tx_size;) {
396 thr = buf[i++];
397 iowrite8(thr, priv->membase + PCH_UART_THR);
398 }
399 return i;
400}
401
402static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
403 int rx_size)
404{
405 int i;
406 u8 rbr, lsr;
407
408 lsr = ioread8(priv->membase + UART_LSR);
409 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
410 i < rx_size && lsr & UART_LSR_DR;
411 lsr = ioread8(priv->membase + UART_LSR)) {
412 rbr = ioread8(priv->membase + PCH_UART_RBR);
413 buf[i++] = rbr;
414 }
415 return i;
416}
417
418static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
419{
420 unsigned int iir;
421 int ret;
422
423 iir = ioread8(priv->membase + UART_IIR);
424 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
425 return ret;
426}
427
428static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
429{
430 return ioread8(priv->membase + UART_LSR);
431}
432
433static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
434{
435 unsigned int lcr;
436
437 lcr = ioread8(priv->membase + UART_LCR);
438 if (on)
439 lcr |= PCH_UART_LCR_SB;
440 else
441 lcr &= ~PCH_UART_LCR_SB;
442
443 iowrite8(lcr, priv->membase + UART_LCR);
444}
445
446static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
447 int size)
448{
449 struct uart_port *port;
450 struct tty_struct *tty;
451
452 port = &priv->port;
453 tty = tty_port_tty_get(&port->state->port);
454 if (!tty) {
455 pr_debug("%s:tty is busy now", __func__);
456 return -EBUSY;
457 }
458
459 tty_insert_flip_string(tty, buf, size);
460 tty_flip_buffer_push(tty);
461 tty_kref_put(tty);
462
463 return 0;
464}
465
466static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
467{
468 int ret;
469 struct uart_port *port = &priv->port;
470
471 if (port->x_char) {
472 pr_debug("%s:X character send %02x (%lu)\n", __func__,
473 port->x_char, jiffies);
474 buf[0] = port->x_char;
475 port->x_char = 0;
476 ret = 1;
477 } else {
478 ret = 0;
479 }
480
481 return ret;
482}
483
484static int dma_push_rx(struct eg20t_port *priv, int size)
485{
486 struct tty_struct *tty;
487 int room;
488 struct uart_port *port = &priv->port;
489
490 port = &priv->port;
491 tty = tty_port_tty_get(&port->state->port);
492 if (!tty) {
493 pr_debug("%s:tty is busy now", __func__);
494 return 0;
495 }
496
497 room = tty_buffer_request_room(tty, size);
498
499 if (room < size)
500 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
501 size - room);
502 if (!room)
503 return room;
504
505 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
506
507 port->icount.rx += room;
508 tty_kref_put(tty);
509
510 return room;
511}
512
513static void pch_free_dma(struct uart_port *port)
514{
515 struct eg20t_port *priv;
516 priv = container_of(port, struct eg20t_port, port);
517
518 if (priv->chan_tx) {
519 dma_release_channel(priv->chan_tx);
520 priv->chan_tx = NULL;
521 }
522 if (priv->chan_rx) {
523 dma_release_channel(priv->chan_rx);
524 priv->chan_rx = NULL;
525 }
526 if (sg_dma_address(&priv->sg_rx))
527 dma_free_coherent(port->dev, port->fifosize,
528 sg_virt(&priv->sg_rx),
529 sg_dma_address(&priv->sg_rx));
530
531 return;
532}
533
534static bool filter(struct dma_chan *chan, void *slave)
535{
536 struct pch_dma_slave *param = slave;
537
538 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
539 chan->device->dev)) {
540 chan->private = param;
541 return true;
542 } else {
543 return false;
544 }
545}
546
547static void pch_request_dma(struct uart_port *port)
548{
549 dma_cap_mask_t mask;
550 struct dma_chan *chan;
551 struct pci_dev *dma_dev;
552 struct pch_dma_slave *param;
553 struct eg20t_port *priv =
554 container_of(port, struct eg20t_port, port);
555 dma_cap_zero(mask);
556 dma_cap_set(DMA_SLAVE, mask);
557
558 dma_dev = pci_get_bus_and_slot(2, PCI_DEVFN(0xa, 0)); /* Get DMA's dev
559 information */
560 /* Set Tx DMA */
561 param = &priv->param_tx;
562 param->dma_dev = &dma_dev->dev;
563 param->chan_id = priv->port.line;
564 param->tx_reg = port->mapbase + UART_TX;
565 chan = dma_request_channel(mask, filter, param);
566 if (!chan) {
567 pr_err("%s:dma_request_channel FAILS(Tx)\n", __func__);
568 return;
569 }
570 priv->chan_tx = chan;
571
572 /* Set Rx DMA */
573 param = &priv->param_rx;
574 param->dma_dev = &dma_dev->dev;
575 param->chan_id = priv->port.line + 1; /* Rx = Tx + 1 */
576 param->rx_reg = port->mapbase + UART_RX;
577 chan = dma_request_channel(mask, filter, param);
578 if (!chan) {
579 pr_err("%s:dma_request_channel FAILS(Rx)\n", __func__);
580 dma_release_channel(priv->chan_tx);
581 return;
582 }
583
584 /* Get Consistent memory for DMA */
585 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
586 &priv->rx_buf_dma, GFP_KERNEL);
587 priv->chan_rx = chan;
588}
589
590static void pch_dma_rx_complete(void *arg)
591{
592 struct eg20t_port *priv = arg;
593 struct uart_port *port = &priv->port;
594 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
595
596 if (!tty) {
597 pr_debug("%s:tty is busy now", __func__);
598 return;
599 }
600
601 if (dma_push_rx(priv, priv->trigger_level))
602 tty_flip_buffer_push(tty);
603
604 tty_kref_put(tty);
605}
606
607static void pch_dma_tx_complete(void *arg)
608{
609 struct eg20t_port *priv = arg;
610 struct uart_port *port = &priv->port;
611 struct circ_buf *xmit = &port->state->xmit;
612
613 xmit->tail += sg_dma_len(&priv->sg_tx);
614 xmit->tail &= UART_XMIT_SIZE - 1;
615 port->icount.tx += sg_dma_len(&priv->sg_tx);
616
617 async_tx_ack(priv->desc_tx);
618 priv->tx_dma_use = 0;
619}
620
621static int pop_tx(struct eg20t_port *priv, unsigned char *buf, int size)
622{
623 int count = 0;
624 struct uart_port *port = &priv->port;
625 struct circ_buf *xmit = &port->state->xmit;
626
627 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
628 goto pop_tx_end;
629
630 do {
631 int cnt_to_end =
632 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
633 int sz = min(size - count, cnt_to_end);
634 memcpy(&buf[count], &xmit->buf[xmit->tail], sz);
635 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
636 count += sz;
637 } while (!uart_circ_empty(xmit) && count < size);
638
639pop_tx_end:
640 pr_debug("%d characters. Remained %d characters. (%lu)\n",
641 count, size - count, jiffies);
642
643 return count;
644}
645
646static int handle_rx_to(struct eg20t_port *priv)
647{
648 struct pch_uart_buffer *buf;
649 int rx_size;
650 int ret;
651 if (!priv->start_rx) {
652 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
653 return 0;
654 }
655 buf = &priv->rxbuf;
656 do {
657 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
658 ret = push_rx(priv, buf->buf, rx_size);
659 if (ret)
660 return 0;
661 } while (rx_size == buf->size);
662
663 return PCH_UART_HANDLED_RX_INT;
664}
665
666static int handle_rx(struct eg20t_port *priv)
667{
668 return handle_rx_to(priv);
669}
670
671static int dma_handle_rx(struct eg20t_port *priv)
672{
673 struct uart_port *port = &priv->port;
674 struct dma_async_tx_descriptor *desc;
675 struct scatterlist *sg;
676
677 priv = container_of(port, struct eg20t_port, port);
678 sg = &priv->sg_rx;
679
680 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
681
682 sg_dma_len(sg) = priv->fifo_size;
683
684 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
685 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
686 ~PAGE_MASK);
687
688 sg_dma_address(sg) = priv->rx_buf_dma;
689
690 desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
691 sg, 1, DMA_FROM_DEVICE,
692 DMA_PREP_INTERRUPT);
693 if (!desc)
694 return 0;
695
696 priv->desc_rx = desc;
697 desc->callback = pch_dma_rx_complete;
698 desc->callback_param = priv;
699 desc->tx_submit(desc);
700 dma_async_issue_pending(priv->chan_rx);
701
702 return PCH_UART_HANDLED_RX_INT;
703}
704
705static unsigned int handle_tx(struct eg20t_port *priv)
706{
707 struct uart_port *port = &priv->port;
708 struct circ_buf *xmit = &port->state->xmit;
709 int ret;
710 int fifo_size;
711 int tx_size;
712 int size;
713 int tx_empty;
714
715 if (!priv->start_tx) {
716 pr_info("%s:Tx isn't started. (%lu)\n", __func__, jiffies);
717 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
718 priv->tx_empty = 1;
719 return 0;
720 }
721
722 fifo_size = max(priv->fifo_size, 1);
723 tx_empty = 1;
724 if (pop_tx_x(priv, xmit->buf)) {
725 pch_uart_hal_write(priv, xmit->buf, 1);
726 port->icount.tx++;
727 tx_empty = 0;
728 fifo_size--;
729 }
730 size = min(xmit->head - xmit->tail, fifo_size);
731 tx_size = pop_tx(priv, xmit->buf, size);
732 if (tx_size > 0) {
733 ret = pch_uart_hal_write(priv, xmit->buf, tx_size);
734 port->icount.tx += ret;
735 tx_empty = 0;
736 }
737
738 priv->tx_empty = tx_empty;
739
740 if (tx_empty)
741 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
742
743 return PCH_UART_HANDLED_TX_INT;
744}
745
746static unsigned int dma_handle_tx(struct eg20t_port *priv)
747{
748 struct uart_port *port = &priv->port;
749 struct circ_buf *xmit = &port->state->xmit;
750 struct scatterlist *sg = &priv->sg_tx;
751 int nent;
752 int fifo_size;
753 int tx_empty;
754 struct dma_async_tx_descriptor *desc;
755
756 if (!priv->start_tx) {
757 pr_info("%s:Tx isn't started. (%lu)\n", __func__, jiffies);
758 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
759 priv->tx_empty = 1;
760 return 0;
761 }
762
763 fifo_size = max(priv->fifo_size, 1);
764 tx_empty = 1;
765 if (pop_tx_x(priv, xmit->buf)) {
766 pch_uart_hal_write(priv, xmit->buf, 1);
767 port->icount.tx++;
768 tx_empty = 0;
769 fifo_size--;
770 }
771
772 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
773
774 priv->tx_dma_use = 1;
775
776 sg_init_table(&priv->sg_tx, 1); /* Initialize SG table */
777
778 sg_set_page(&priv->sg_tx, virt_to_page(xmit->buf),
779 UART_XMIT_SIZE, (int)xmit->buf & ~PAGE_MASK);
780
781 nent = dma_map_sg(port->dev, &priv->sg_tx, 1, DMA_TO_DEVICE);
782 if (!nent) {
783 pr_err("%s:dma_map_sg Failed\n", __func__);
784 return 0;
785 }
786
787 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
788 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
789 sg->offset;
790 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail,
791 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
792 xmit->tail, UART_XMIT_SIZE));
793
794 desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
795 sg, nent, DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
796 if (!desc) {
797 pr_err("%s:device_prep_slave_sg Failed\n", __func__);
798 return 0;
799 }
800
801 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
802
803 priv->desc_tx = desc;
804 desc->callback = pch_dma_tx_complete;
805 desc->callback_param = priv;
806
807 desc->tx_submit(desc);
808
809 dma_async_issue_pending(priv->chan_tx);
810
811 return PCH_UART_HANDLED_TX_INT;
812}
813
814static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
815{
816 u8 fcr = ioread8(priv->membase + UART_FCR);
817
818 /* Reset FIFO */
819 fcr |= UART_FCR_CLEAR_RCVR;
820 iowrite8(fcr, priv->membase + UART_FCR);
821
822 if (lsr & PCH_UART_LSR_ERR)
823 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
824
825 if (lsr & UART_LSR_FE)
826 dev_err(&priv->pdev->dev, "Framing Error\n");
827
828 if (lsr & UART_LSR_PE)
829 dev_err(&priv->pdev->dev, "Parity Error\n");
830
831 if (lsr & UART_LSR_OE)
832 dev_err(&priv->pdev->dev, "Overrun Error\n");
833}
834
835static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
836{
837 struct eg20t_port *priv = dev_id;
838 unsigned int handled;
839 u8 lsr;
840 int ret = 0;
841 unsigned int iid;
842 unsigned long flags;
843
844 spin_lock_irqsave(&priv->port.lock, flags);
845 handled = 0;
846 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
847 switch (iid) {
848 case PCH_UART_IID_RLS: /* Receiver Line Status */
849 lsr = pch_uart_hal_get_line_status(priv);
850 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
851 UART_LSR_PE | UART_LSR_OE)) {
852 pch_uart_err_ir(priv, lsr);
853 ret = PCH_UART_HANDLED_RX_ERR_INT;
854 }
855 break;
856 case PCH_UART_IID_RDR: /* Received Data Ready */
857 if (priv->use_dma)
858 ret = dma_handle_rx(priv);
859 else
860 ret = handle_rx(priv);
861 break;
862 case PCH_UART_IID_RDR_TO: /* Received Data Ready
863 (FIFO Timeout) */
864 ret = handle_rx_to(priv);
865 break;
866 case PCH_UART_IID_THRE: /* Transmitter Holding Register
867 Empty */
868 if (priv->use_dma)
869 ret = dma_handle_tx(priv);
870 else
871 ret = handle_tx(priv);
872 break;
873 case PCH_UART_IID_MS: /* Modem Status */
874 ret = PCH_UART_HANDLED_MS_INT;
875 break;
876 default: /* Never junp to this label */
877 pr_err("%s:iid=%d (%lu)\n", __func__, iid, jiffies);
878 ret = -1;
879 break;
880 }
881 handled |= (unsigned int)ret;
882 }
883 if (handled == 0 && iid <= 1) {
884 if (priv->int_dis_flag)
885 priv->int_dis_flag = 0;
886 }
887
888 spin_unlock_irqrestore(&priv->port.lock, flags);
889 return IRQ_RETVAL(handled);
890}
891
892/* This function tests whether the transmitter fifo and shifter for the port
893 described by 'port' is empty. */
894static unsigned int pch_uart_tx_empty(struct uart_port *port)
895{
896 struct eg20t_port *priv;
897 int ret;
898 priv = container_of(port, struct eg20t_port, port);
899 if (priv->tx_empty)
900 ret = TIOCSER_TEMT;
901 else
902 ret = 0;
903
904 return ret;
905}
906
907/* Returns the current state of modem control inputs. */
908static unsigned int pch_uart_get_mctrl(struct uart_port *port)
909{
910 struct eg20t_port *priv;
911 u8 modem;
912 unsigned int ret = 0;
913
914 priv = container_of(port, struct eg20t_port, port);
915 modem = pch_uart_hal_get_modem(priv);
916
917 if (modem & UART_MSR_DCD)
918 ret |= TIOCM_CAR;
919
920 if (modem & UART_MSR_RI)
921 ret |= TIOCM_RNG;
922
923 if (modem & UART_MSR_DSR)
924 ret |= TIOCM_DSR;
925
926 if (modem & UART_MSR_CTS)
927 ret |= TIOCM_CTS;
928
929 return ret;
930}
931
932static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
933{
934 u32 mcr = 0;
935 unsigned int dat;
936 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
937
938 if (mctrl & TIOCM_DTR)
939 mcr |= UART_MCR_DTR;
940 if (mctrl & TIOCM_RTS)
941 mcr |= UART_MCR_RTS;
942 if (mctrl & TIOCM_LOOP)
943 mcr |= UART_MCR_LOOP;
944
945 if (mctrl) {
946 dat = pch_uart_get_mctrl(port);
947 dat |= mcr;
948 iowrite8(dat, priv->membase + UART_MCR);
949 }
950}
951
952static void pch_uart_stop_tx(struct uart_port *port)
953{
954 struct eg20t_port *priv;
955 priv = container_of(port, struct eg20t_port, port);
956 priv->start_tx = 0;
957 priv->tx_dma_use = 0;
958}
959
960static void pch_uart_start_tx(struct uart_port *port)
961{
962 struct eg20t_port *priv;
963
964 priv = container_of(port, struct eg20t_port, port);
965
966 if (priv->use_dma)
967 if (priv->tx_dma_use)
968 return;
969
970 priv->start_tx = 1;
971 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
972}
973
974static void pch_uart_stop_rx(struct uart_port *port)
975{
976 struct eg20t_port *priv;
977 priv = container_of(port, struct eg20t_port, port);
978 priv->start_rx = 0;
979 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
980 priv->int_dis_flag = 1;
981}
982
983/* Enable the modem status interrupts. */
984static void pch_uart_enable_ms(struct uart_port *port)
985{
986 struct eg20t_port *priv;
987 priv = container_of(port, struct eg20t_port, port);
988 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
989}
990
991/* Control the transmission of a break signal. */
992static void pch_uart_break_ctl(struct uart_port *port, int ctl)
993{
994 struct eg20t_port *priv;
995 unsigned long flags;
996
997 priv = container_of(port, struct eg20t_port, port);
998 spin_lock_irqsave(&port->lock, flags);
999 pch_uart_hal_set_break(priv, ctl);
1000 spin_unlock_irqrestore(&port->lock, flags);
1001}
1002
1003/* Grab any interrupt resources and initialise any low level driver state. */
1004static int pch_uart_startup(struct uart_port *port)
1005{
1006 struct eg20t_port *priv;
1007 int ret;
1008 int fifo_size;
1009 int trigger_level;
1010
1011 priv = container_of(port, struct eg20t_port, port);
1012 priv->tx_empty = 1;
1013 port->uartclk = priv->base_baud;
1014 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1015 ret = pch_uart_hal_set_line(priv, default_baud,
1016 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1017 PCH_UART_HAL_STB1);
1018 if (ret)
1019 return ret;
1020
1021 switch (priv->fifo_size) {
1022 case 256:
1023 fifo_size = PCH_UART_HAL_FIFO256;
1024 break;
1025 case 64:
1026 fifo_size = PCH_UART_HAL_FIFO64;
1027 break;
1028 case 16:
1029 fifo_size = PCH_UART_HAL_FIFO16;
1030 case 1:
1031 default:
1032 fifo_size = PCH_UART_HAL_FIFO_DIS;
1033 break;
1034 }
1035
1036 switch (priv->trigger) {
1037 case PCH_UART_HAL_TRIGGER1:
1038 trigger_level = 1;
1039 break;
1040 case PCH_UART_HAL_TRIGGER_L:
1041 trigger_level = priv->fifo_size / 4;
1042 break;
1043 case PCH_UART_HAL_TRIGGER_M:
1044 trigger_level = priv->fifo_size / 2;
1045 break;
1046 case PCH_UART_HAL_TRIGGER_H:
1047 default:
1048 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1049 break;
1050 }
1051
1052 priv->trigger_level = trigger_level;
1053 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1054 fifo_size, priv->trigger);
1055 if (ret < 0)
1056 return ret;
1057
1058 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1059 KBUILD_MODNAME, priv);
1060 if (ret < 0)
1061 return ret;
1062
1063 if (priv->use_dma)
1064 pch_request_dma(port);
1065
1066 priv->start_rx = 1;
1067 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1068 uart_update_timeout(port, CS8, default_baud);
1069
1070 return 0;
1071}
1072
1073static void pch_uart_shutdown(struct uart_port *port)
1074{
1075 struct eg20t_port *priv;
1076 int ret;
1077
1078 priv = container_of(port, struct eg20t_port, port);
1079 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1080 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1081 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1082 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1083 if (ret)
1084 pr_err("pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
1085
1086 if (priv->use_dma_flag)
1087 pch_free_dma(port);
1088
1089 free_irq(priv->port.irq, priv);
1090}
1091
1092/* Change the port parameters, including word length, parity, stop
1093 *bits. Update read_status_mask and ignore_status_mask to indicate
1094 *the types of events we are interested in receiving. */
1095static void pch_uart_set_termios(struct uart_port *port,
1096 struct ktermios *termios, struct ktermios *old)
1097{
1098 int baud;
1099 int rtn;
1100 unsigned int parity, bits, stb;
1101 struct eg20t_port *priv;
1102 unsigned long flags;
1103
1104 priv = container_of(port, struct eg20t_port, port);
1105 switch (termios->c_cflag & CSIZE) {
1106 case CS5:
1107 bits = PCH_UART_HAL_5BIT;
1108 break;
1109 case CS6:
1110 bits = PCH_UART_HAL_6BIT;
1111 break;
1112 case CS7:
1113 bits = PCH_UART_HAL_7BIT;
1114 break;
1115 default: /* CS8 */
1116 bits = PCH_UART_HAL_8BIT;
1117 break;
1118 }
1119 if (termios->c_cflag & CSTOPB)
1120 stb = PCH_UART_HAL_STB2;
1121 else
1122 stb = PCH_UART_HAL_STB1;
1123
1124 if (termios->c_cflag & PARENB) {
1125 if (!(termios->c_cflag & PARODD))
1126 parity = PCH_UART_HAL_PARITY_ODD;
1127 else
1128 parity = PCH_UART_HAL_PARITY_EVEN;
1129
1130 } else {
1131 parity = PCH_UART_HAL_PARITY_NONE;
1132 }
1133 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1134
1135 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1136
1137 spin_lock_irqsave(&port->lock, flags);
1138
1139 uart_update_timeout(port, termios->c_cflag, baud);
1140 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1141 if (rtn)
1142 goto out;
1143
1144 /* Don't rewrite B0 */
1145 if (tty_termios_baud_rate(termios))
1146 tty_termios_encode_baud_rate(termios, baud, baud);
1147
1148out:
1149 spin_unlock_irqrestore(&port->lock, flags);
1150}
1151
1152static const char *pch_uart_type(struct uart_port *port)
1153{
1154 return KBUILD_MODNAME;
1155}
1156
1157static void pch_uart_release_port(struct uart_port *port)
1158{
1159 struct eg20t_port *priv;
1160
1161 priv = container_of(port, struct eg20t_port, port);
1162 pci_iounmap(priv->pdev, priv->membase);
1163 pci_release_regions(priv->pdev);
1164}
1165
1166static int pch_uart_request_port(struct uart_port *port)
1167{
1168 struct eg20t_port *priv;
1169 int ret;
1170 void __iomem *membase;
1171
1172 priv = container_of(port, struct eg20t_port, port);
1173 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1174 if (ret < 0)
1175 return -EBUSY;
1176
1177 membase = pci_iomap(priv->pdev, 1, 0);
1178 if (!membase) {
1179 pci_release_regions(priv->pdev);
1180 return -EBUSY;
1181 }
1182 priv->membase = port->membase = membase;
1183
1184 return 0;
1185}
1186
1187static void pch_uart_config_port(struct uart_port *port, int type)
1188{
1189 struct eg20t_port *priv;
1190
1191 priv = container_of(port, struct eg20t_port, port);
1192 if (type & UART_CONFIG_TYPE) {
1193 port->type = priv->port_type;
1194 pch_uart_request_port(port);
1195 }
1196}
1197
1198static int pch_uart_verify_port(struct uart_port *port,
1199 struct serial_struct *serinfo)
1200{
1201 struct eg20t_port *priv;
1202
1203 priv = container_of(port, struct eg20t_port, port);
1204 if (serinfo->flags & UPF_LOW_LATENCY) {
1205 pr_info("PCH UART : Use PIO Mode (without DMA)\n");
1206 priv->use_dma = 0;
1207 serinfo->flags &= ~UPF_LOW_LATENCY;
1208 } else {
1209#ifndef CONFIG_PCH_DMA
1210 pr_err("%s : PCH DMA is not Loaded.\n", __func__);
1211 return -EOPNOTSUPP;
1212#endif
1213 priv->use_dma = 1;
1214 priv->use_dma_flag = 1;
1215 pr_info("PCH UART : Use DMA Mode\n");
1216 }
1217
1218 return 0;
1219}
1220
1221static struct uart_ops pch_uart_ops = {
1222 .tx_empty = pch_uart_tx_empty,
1223 .set_mctrl = pch_uart_set_mctrl,
1224 .get_mctrl = pch_uart_get_mctrl,
1225 .stop_tx = pch_uart_stop_tx,
1226 .start_tx = pch_uart_start_tx,
1227 .stop_rx = pch_uart_stop_rx,
1228 .enable_ms = pch_uart_enable_ms,
1229 .break_ctl = pch_uart_break_ctl,
1230 .startup = pch_uart_startup,
1231 .shutdown = pch_uart_shutdown,
1232 .set_termios = pch_uart_set_termios,
1233/* .pm = pch_uart_pm, Not supported yet */
1234/* .set_wake = pch_uart_set_wake, Not supported yet */
1235 .type = pch_uart_type,
1236 .release_port = pch_uart_release_port,
1237 .request_port = pch_uart_request_port,
1238 .config_port = pch_uart_config_port,
1239 .verify_port = pch_uart_verify_port
1240};
1241
1242static struct uart_driver pch_uart_driver = {
1243 .owner = THIS_MODULE,
1244 .driver_name = KBUILD_MODNAME,
1245 .dev_name = PCH_UART_DRIVER_DEVICE,
1246 .major = 0,
1247 .minor = 0,
1248 .nr = PCH_UART_NR,
1249};
1250
1251static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
1252 int port_type)
1253{
1254 struct eg20t_port *priv;
1255 int ret;
1256 unsigned int iobase;
1257 unsigned int mapbase;
1258 unsigned char *rxbuf;
1259 int fifosize, base_baud;
1260 static int num;
1261
1262 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1263 if (priv == NULL)
1264 goto init_port_alloc_err;
1265
1266 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1267 if (!rxbuf)
1268 goto init_port_free_txbuf;
1269
1270 switch (port_type) {
1271 case PORT_UNKNOWN:
1272 fifosize = 256; /* UART0 */
1273 base_baud = 1843200; /* 1.8432MHz */
1274 break;
1275 case PORT_8250:
1276 fifosize = 64; /* UART1~3 */
1277 base_baud = 1843200; /* 1.8432MHz */
1278 break;
1279 default:
1280 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1281 goto init_port_hal_free;
1282 }
1283
1284 iobase = pci_resource_start(pdev, 0);
1285 mapbase = pci_resource_start(pdev, 1);
1286 priv->mapbase = mapbase;
1287 priv->iobase = iobase;
1288 priv->pdev = pdev;
1289 priv->tx_empty = 1;
1290 priv->rxbuf.buf = rxbuf;
1291 priv->rxbuf.size = PAGE_SIZE;
1292
1293 priv->fifo_size = fifosize;
1294 priv->base_baud = base_baud;
1295 priv->port_type = PORT_MAX_8250 + port_type + 1;
1296 priv->port.dev = &pdev->dev;
1297 priv->port.iobase = iobase;
1298 priv->port.membase = NULL;
1299 priv->port.mapbase = mapbase;
1300 priv->port.irq = pdev->irq;
1301 priv->port.iotype = UPIO_PORT;
1302 priv->port.ops = &pch_uart_ops;
1303 priv->port.flags = UPF_BOOT_AUTOCONF;
1304 priv->port.fifosize = fifosize;
1305 priv->port.line = num++;
1306 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1307
1308 pci_set_drvdata(pdev, priv);
1309 pch_uart_hal_request(pdev, fifosize, base_baud);
1310 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1311 if (ret < 0)
1312 goto init_port_hal_free;
1313
1314 return priv;
1315
1316init_port_hal_free:
1317 free_page((unsigned long)rxbuf);
1318init_port_free_txbuf:
1319 kfree(priv);
1320init_port_alloc_err:
1321
1322 return NULL;
1323}
1324
1325static void pch_uart_exit_port(struct eg20t_port *priv)
1326{
1327 uart_remove_one_port(&pch_uart_driver, &priv->port);
1328 pci_set_drvdata(priv->pdev, NULL);
1329 free_page((unsigned long)priv->rxbuf.buf);
1330}
1331
1332static void pch_uart_pci_remove(struct pci_dev *pdev)
1333{
1334 struct eg20t_port *priv;
1335
1336 priv = (struct eg20t_port *)pci_get_drvdata(pdev);
1337 pch_uart_exit_port(priv);
1338 pci_disable_device(pdev);
1339 kfree(priv);
1340 return;
1341}
1342#ifdef CONFIG_PM
1343static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1344{
1345 struct eg20t_port *priv = pci_get_drvdata(pdev);
1346
1347 uart_suspend_port(&pch_uart_driver, &priv->port);
1348
1349 pci_save_state(pdev);
1350 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1351 return 0;
1352}
1353
1354static int pch_uart_pci_resume(struct pci_dev *pdev)
1355{
1356 struct eg20t_port *priv = pci_get_drvdata(pdev);
1357 int ret;
1358
1359 pci_set_power_state(pdev, PCI_D0);
1360 pci_restore_state(pdev);
1361
1362 ret = pci_enable_device(pdev);
1363 if (ret) {
1364 dev_err(&pdev->dev,
1365 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1366 return ret;
1367 }
1368
1369 uart_resume_port(&pch_uart_driver, &priv->port);
1370
1371 return 0;
1372}
1373#else
1374#define pch_uart_pci_suspend NULL
1375#define pch_uart_pci_resume NULL
1376#endif
1377
1378static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1379 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
1380 .driver_data = PCH_UART_8LINE},
1381 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
1382 .driver_data = PCH_UART_2LINE},
1383 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
1384 .driver_data = PCH_UART_2LINE},
1385 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
1386 .driver_data = PCH_UART_2LINE},
1387 {0,},
1388};
1389
1390static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1391 const struct pci_device_id *id)
1392{
1393 int ret;
1394 struct eg20t_port *priv;
1395
1396 ret = pci_enable_device(pdev);
1397 if (ret < 0)
1398 goto probe_error;
1399
1400 priv = pch_uart_init_port(pdev, id->driver_data);
1401 if (!priv) {
1402 ret = -EBUSY;
1403 goto probe_disable_device;
1404 }
1405 pci_set_drvdata(pdev, priv);
1406
1407 return ret;
1408
1409probe_disable_device:
1410 pci_disable_device(pdev);
1411probe_error:
1412 return ret;
1413}
1414
1415static struct pci_driver pch_uart_pci_driver = {
1416 .name = "pch_uart",
1417 .id_table = pch_uart_pci_id,
1418 .probe = pch_uart_pci_probe,
1419 .remove = __devexit_p(pch_uart_pci_remove),
1420 .suspend = pch_uart_pci_suspend,
1421 .resume = pch_uart_pci_resume,
1422};
1423
1424static int __init pch_uart_module_init(void)
1425{
1426 int ret;
1427
1428 /* register as UART driver */
1429 ret = uart_register_driver(&pch_uart_driver);
1430 if (ret < 0)
1431 return ret;
1432
1433 /* register as PCI driver */
1434 ret = pci_register_driver(&pch_uart_pci_driver);
1435 if (ret < 0)
1436 uart_unregister_driver(&pch_uart_driver);
1437
1438 return ret;
1439}
1440module_init(pch_uart_module_init);
1441
1442static void __exit pch_uart_module_exit(void)
1443{
1444 pci_unregister_driver(&pch_uart_pci_driver);
1445 uart_unregister_driver(&pch_uart_driver);
1446}
1447module_exit(pch_uart_module_exit);
1448
1449MODULE_LICENSE("GPL v2");
1450MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1451module_param(default_baud, uint, S_IRUGO);
diff --git a/drivers/tty/serial/pmac_zilog.c b/drivers/tty/serial/pmac_zilog.c
new file mode 100644
index 000000000000..5b9cde79e4ea
--- /dev/null
+++ b/drivers/tty/serial/pmac_zilog.c
@@ -0,0 +1,2208 @@
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/module.h>
46#include <linux/tty.h>
47
48#include <linux/tty_flip.h>
49#include <linux/major.h>
50#include <linux/string.h>
51#include <linux/fcntl.h>
52#include <linux/mm.h>
53#include <linux/kernel.h>
54#include <linux/delay.h>
55#include <linux/init.h>
56#include <linux/console.h>
57#include <linux/adb.h>
58#include <linux/pmu.h>
59#include <linux/bitops.h>
60#include <linux/sysrq.h>
61#include <linux/mutex.h>
62#include <asm/sections.h>
63#include <asm/io.h>
64#include <asm/irq.h>
65
66#ifdef CONFIG_PPC_PMAC
67#include <asm/prom.h>
68#include <asm/machdep.h>
69#include <asm/pmac_feature.h>
70#include <asm/dbdma.h>
71#include <asm/macio.h>
72#else
73#include <linux/platform_device.h>
74#define of_machine_is_compatible(x) (0)
75#endif
76
77#if defined (CONFIG_SERIAL_PMACZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
78#define SUPPORT_SYSRQ
79#endif
80
81#include <linux/serial.h>
82#include <linux/serial_core.h>
83
84#include "pmac_zilog.h"
85
86/* Not yet implemented */
87#undef HAS_DBDMA
88
89static char version[] __initdata = "pmac_zilog: 0.6 (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
90MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
91MODULE_DESCRIPTION("Driver for the Mac and PowerMac serial ports.");
92MODULE_LICENSE("GPL");
93
94#ifdef CONFIG_SERIAL_PMACZILOG_TTYS
95#define PMACZILOG_MAJOR TTY_MAJOR
96#define PMACZILOG_MINOR 64
97#define PMACZILOG_NAME "ttyS"
98#else
99#define PMACZILOG_MAJOR 204
100#define PMACZILOG_MINOR 192
101#define PMACZILOG_NAME "ttyPZ"
102#endif
103
104
105/*
106 * For the sake of early serial console, we can do a pre-probe
107 * (optional) of the ports at rather early boot time.
108 */
109static struct uart_pmac_port pmz_ports[MAX_ZS_PORTS];
110static int pmz_ports_count;
111static DEFINE_MUTEX(pmz_irq_mutex);
112
113static struct uart_driver pmz_uart_reg = {
114 .owner = THIS_MODULE,
115 .driver_name = PMACZILOG_NAME,
116 .dev_name = PMACZILOG_NAME,
117 .major = PMACZILOG_MAJOR,
118 .minor = PMACZILOG_MINOR,
119};
120
121
122/*
123 * Load all registers to reprogram the port
124 * This function must only be called when the TX is not busy. The UART
125 * port lock must be held and local interrupts disabled.
126 */
127static void pmz_load_zsregs(struct uart_pmac_port *uap, u8 *regs)
128{
129 int i;
130
131 if (ZS_IS_ASLEEP(uap))
132 return;
133
134 /* Let pending transmits finish. */
135 for (i = 0; i < 1000; i++) {
136 unsigned char stat = read_zsreg(uap, R1);
137 if (stat & ALL_SNT)
138 break;
139 udelay(100);
140 }
141
142 ZS_CLEARERR(uap);
143 zssync(uap);
144 ZS_CLEARFIFO(uap);
145 zssync(uap);
146 ZS_CLEARERR(uap);
147
148 /* Disable all interrupts. */
149 write_zsreg(uap, R1,
150 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
151
152 /* Set parity, sync config, stop bits, and clock divisor. */
153 write_zsreg(uap, R4, regs[R4]);
154
155 /* Set misc. TX/RX control bits. */
156 write_zsreg(uap, R10, regs[R10]);
157
158 /* Set TX/RX controls sans the enable bits. */
159 write_zsreg(uap, R3, regs[R3] & ~RxENABLE);
160 write_zsreg(uap, R5, regs[R5] & ~TxENABLE);
161
162 /* now set R7 "prime" on ESCC */
163 write_zsreg(uap, R15, regs[R15] | EN85C30);
164 write_zsreg(uap, R7, regs[R7P]);
165
166 /* make sure we use R7 "non-prime" on ESCC */
167 write_zsreg(uap, R15, regs[R15] & ~EN85C30);
168
169 /* Synchronous mode config. */
170 write_zsreg(uap, R6, regs[R6]);
171 write_zsreg(uap, R7, regs[R7]);
172
173 /* Disable baud generator. */
174 write_zsreg(uap, R14, regs[R14] & ~BRENAB);
175
176 /* Clock mode control. */
177 write_zsreg(uap, R11, regs[R11]);
178
179 /* Lower and upper byte of baud rate generator divisor. */
180 write_zsreg(uap, R12, regs[R12]);
181 write_zsreg(uap, R13, regs[R13]);
182
183 /* Now rewrite R14, with BRENAB (if set). */
184 write_zsreg(uap, R14, regs[R14]);
185
186 /* Reset external status interrupts. */
187 write_zsreg(uap, R0, RES_EXT_INT);
188 write_zsreg(uap, R0, RES_EXT_INT);
189
190 /* Rewrite R3/R5, this time without enables masked. */
191 write_zsreg(uap, R3, regs[R3]);
192 write_zsreg(uap, R5, regs[R5]);
193
194 /* Rewrite R1, this time without IRQ enabled masked. */
195 write_zsreg(uap, R1, regs[R1]);
196
197 /* Enable interrupts */
198 write_zsreg(uap, R9, regs[R9]);
199}
200
201/*
202 * We do like sunzilog to avoid disrupting pending Tx
203 * Reprogram the Zilog channel HW registers with the copies found in the
204 * software state struct. If the transmitter is busy, we defer this update
205 * until the next TX complete interrupt. Else, we do it right now.
206 *
207 * The UART port lock must be held and local interrupts disabled.
208 */
209static void pmz_maybe_update_regs(struct uart_pmac_port *uap)
210{
211 if (!ZS_REGS_HELD(uap)) {
212 if (ZS_TX_ACTIVE(uap)) {
213 uap->flags |= PMACZILOG_FLAG_REGS_HELD;
214 } else {
215 pmz_debug("pmz: maybe_update_regs: updating\n");
216 pmz_load_zsregs(uap, uap->curregs);
217 }
218 }
219}
220
221static struct tty_struct *pmz_receive_chars(struct uart_pmac_port *uap)
222{
223 struct tty_struct *tty = NULL;
224 unsigned char ch, r1, drop, error, flag;
225 int loops = 0;
226
227 /* The interrupt can be enabled when the port isn't open, typically
228 * that happens when using one port is open and the other closed (stale
229 * interrupt) or when one port is used as a console.
230 */
231 if (!ZS_IS_OPEN(uap)) {
232 pmz_debug("pmz: draining input\n");
233 /* Port is closed, drain input data */
234 for (;;) {
235 if ((++loops) > 1000)
236 goto flood;
237 (void)read_zsreg(uap, R1);
238 write_zsreg(uap, R0, ERR_RES);
239 (void)read_zsdata(uap);
240 ch = read_zsreg(uap, R0);
241 if (!(ch & Rx_CH_AV))
242 break;
243 }
244 return NULL;
245 }
246
247 /* Sanity check, make sure the old bug is no longer happening */
248 if (uap->port.state == NULL || uap->port.state->port.tty == NULL) {
249 WARN_ON(1);
250 (void)read_zsdata(uap);
251 return NULL;
252 }
253 tty = uap->port.state->port.tty;
254
255 while (1) {
256 error = 0;
257 drop = 0;
258
259 r1 = read_zsreg(uap, R1);
260 ch = read_zsdata(uap);
261
262 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
263 write_zsreg(uap, R0, ERR_RES);
264 zssync(uap);
265 }
266
267 ch &= uap->parity_mask;
268 if (ch == 0 && uap->flags & PMACZILOG_FLAG_BREAK) {
269 uap->flags &= ~PMACZILOG_FLAG_BREAK;
270 }
271
272#if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_SERIAL_CORE_CONSOLE)
273#ifdef USE_CTRL_O_SYSRQ
274 /* Handle the SysRq ^O Hack */
275 if (ch == '\x0f') {
276 uap->port.sysrq = jiffies + HZ*5;
277 goto next_char;
278 }
279#endif /* USE_CTRL_O_SYSRQ */
280 if (uap->port.sysrq) {
281 int swallow;
282 spin_unlock(&uap->port.lock);
283 swallow = uart_handle_sysrq_char(&uap->port, ch);
284 spin_lock(&uap->port.lock);
285 if (swallow)
286 goto next_char;
287 }
288#endif /* CONFIG_MAGIC_SYSRQ && CONFIG_SERIAL_CORE_CONSOLE */
289
290 /* A real serial line, record the character and status. */
291 if (drop)
292 goto next_char;
293
294 flag = TTY_NORMAL;
295 uap->port.icount.rx++;
296
297 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | BRK_ABRT)) {
298 error = 1;
299 if (r1 & BRK_ABRT) {
300 pmz_debug("pmz: got break !\n");
301 r1 &= ~(PAR_ERR | CRC_ERR);
302 uap->port.icount.brk++;
303 if (uart_handle_break(&uap->port))
304 goto next_char;
305 }
306 else if (r1 & PAR_ERR)
307 uap->port.icount.parity++;
308 else if (r1 & CRC_ERR)
309 uap->port.icount.frame++;
310 if (r1 & Rx_OVR)
311 uap->port.icount.overrun++;
312 r1 &= uap->port.read_status_mask;
313 if (r1 & BRK_ABRT)
314 flag = TTY_BREAK;
315 else if (r1 & PAR_ERR)
316 flag = TTY_PARITY;
317 else if (r1 & CRC_ERR)
318 flag = TTY_FRAME;
319 }
320
321 if (uap->port.ignore_status_mask == 0xff ||
322 (r1 & uap->port.ignore_status_mask) == 0) {
323 tty_insert_flip_char(tty, ch, flag);
324 }
325 if (r1 & Rx_OVR)
326 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
327 next_char:
328 /* We can get stuck in an infinite loop getting char 0 when the
329 * line is in a wrong HW state, we break that here.
330 * When that happens, I disable the receive side of the driver.
331 * Note that what I've been experiencing is a real irq loop where
332 * I'm getting flooded regardless of the actual port speed.
333 * Something stange is going on with the HW
334 */
335 if ((++loops) > 1000)
336 goto flood;
337 ch = read_zsreg(uap, R0);
338 if (!(ch & Rx_CH_AV))
339 break;
340 }
341
342 return tty;
343 flood:
344 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
345 write_zsreg(uap, R1, uap->curregs[R1]);
346 zssync(uap);
347 pmz_error("pmz: rx irq flood !\n");
348 return tty;
349}
350
351static void pmz_status_handle(struct uart_pmac_port *uap)
352{
353 unsigned char status;
354
355 status = read_zsreg(uap, R0);
356 write_zsreg(uap, R0, RES_EXT_INT);
357 zssync(uap);
358
359 if (ZS_IS_OPEN(uap) && ZS_WANTS_MODEM_STATUS(uap)) {
360 if (status & SYNC_HUNT)
361 uap->port.icount.dsr++;
362
363 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
364 * But it does not tell us which bit has changed, we have to keep
365 * track of this ourselves.
366 * The CTS input is inverted for some reason. -- paulus
367 */
368 if ((status ^ uap->prev_status) & DCD)
369 uart_handle_dcd_change(&uap->port,
370 (status & DCD));
371 if ((status ^ uap->prev_status) & CTS)
372 uart_handle_cts_change(&uap->port,
373 !(status & CTS));
374
375 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
376 }
377
378 if (status & BRK_ABRT)
379 uap->flags |= PMACZILOG_FLAG_BREAK;
380
381 uap->prev_status = status;
382}
383
384static void pmz_transmit_chars(struct uart_pmac_port *uap)
385{
386 struct circ_buf *xmit;
387
388 if (ZS_IS_ASLEEP(uap))
389 return;
390 if (ZS_IS_CONS(uap)) {
391 unsigned char status = read_zsreg(uap, R0);
392
393 /* TX still busy? Just wait for the next TX done interrupt.
394 *
395 * It can occur because of how we do serial console writes. It would
396 * be nice to transmit console writes just like we normally would for
397 * a TTY line. (ie. buffered and TX interrupt driven). That is not
398 * easy because console writes cannot sleep. One solution might be
399 * to poll on enough port->xmit space becomming free. -DaveM
400 */
401 if (!(status & Tx_BUF_EMP))
402 return;
403 }
404
405 uap->flags &= ~PMACZILOG_FLAG_TX_ACTIVE;
406
407 if (ZS_REGS_HELD(uap)) {
408 pmz_load_zsregs(uap, uap->curregs);
409 uap->flags &= ~PMACZILOG_FLAG_REGS_HELD;
410 }
411
412 if (ZS_TX_STOPPED(uap)) {
413 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
414 goto ack_tx_int;
415 }
416
417 /* Under some circumstances, we see interrupts reported for
418 * a closed channel. The interrupt mask in R1 is clear, but
419 * R3 still signals the interrupts and we see them when taking
420 * an interrupt for the other channel (this could be a qemu
421 * bug but since the ESCC doc doesn't specify precsiely whether
422 * R3 interrup status bits are masked by R1 interrupt enable
423 * bits, better safe than sorry). --BenH.
424 */
425 if (!ZS_IS_OPEN(uap))
426 goto ack_tx_int;
427
428 if (uap->port.x_char) {
429 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
430 write_zsdata(uap, uap->port.x_char);
431 zssync(uap);
432 uap->port.icount.tx++;
433 uap->port.x_char = 0;
434 return;
435 }
436
437 if (uap->port.state == NULL)
438 goto ack_tx_int;
439 xmit = &uap->port.state->xmit;
440 if (uart_circ_empty(xmit)) {
441 uart_write_wakeup(&uap->port);
442 goto ack_tx_int;
443 }
444 if (uart_tx_stopped(&uap->port))
445 goto ack_tx_int;
446
447 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
448 write_zsdata(uap, xmit->buf[xmit->tail]);
449 zssync(uap);
450
451 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
452 uap->port.icount.tx++;
453
454 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
455 uart_write_wakeup(&uap->port);
456
457 return;
458
459ack_tx_int:
460 write_zsreg(uap, R0, RES_Tx_P);
461 zssync(uap);
462}
463
464/* Hrm... we register that twice, fixme later.... */
465static irqreturn_t pmz_interrupt(int irq, void *dev_id)
466{
467 struct uart_pmac_port *uap = dev_id;
468 struct uart_pmac_port *uap_a;
469 struct uart_pmac_port *uap_b;
470 int rc = IRQ_NONE;
471 struct tty_struct *tty;
472 u8 r3;
473
474 uap_a = pmz_get_port_A(uap);
475 uap_b = uap_a->mate;
476
477 spin_lock(&uap_a->port.lock);
478 r3 = read_zsreg(uap_a, R3);
479
480#ifdef DEBUG_HARD
481 pmz_debug("irq, r3: %x\n", r3);
482#endif
483 /* Channel A */
484 tty = NULL;
485 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
486 write_zsreg(uap_a, R0, RES_H_IUS);
487 zssync(uap_a);
488 if (r3 & CHAEXT)
489 pmz_status_handle(uap_a);
490 if (r3 & CHARxIP)
491 tty = pmz_receive_chars(uap_a);
492 if (r3 & CHATxIP)
493 pmz_transmit_chars(uap_a);
494 rc = IRQ_HANDLED;
495 }
496 spin_unlock(&uap_a->port.lock);
497 if (tty != NULL)
498 tty_flip_buffer_push(tty);
499
500 if (uap_b->node == NULL)
501 goto out;
502
503 spin_lock(&uap_b->port.lock);
504 tty = NULL;
505 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
506 write_zsreg(uap_b, R0, RES_H_IUS);
507 zssync(uap_b);
508 if (r3 & CHBEXT)
509 pmz_status_handle(uap_b);
510 if (r3 & CHBRxIP)
511 tty = pmz_receive_chars(uap_b);
512 if (r3 & CHBTxIP)
513 pmz_transmit_chars(uap_b);
514 rc = IRQ_HANDLED;
515 }
516 spin_unlock(&uap_b->port.lock);
517 if (tty != NULL)
518 tty_flip_buffer_push(tty);
519
520 out:
521#ifdef DEBUG_HARD
522 pmz_debug("irq done.\n");
523#endif
524 return rc;
525}
526
527/*
528 * Peek the status register, lock not held by caller
529 */
530static inline u8 pmz_peek_status(struct uart_pmac_port *uap)
531{
532 unsigned long flags;
533 u8 status;
534
535 spin_lock_irqsave(&uap->port.lock, flags);
536 status = read_zsreg(uap, R0);
537 spin_unlock_irqrestore(&uap->port.lock, flags);
538
539 return status;
540}
541
542/*
543 * Check if transmitter is empty
544 * The port lock is not held.
545 */
546static unsigned int pmz_tx_empty(struct uart_port *port)
547{
548 struct uart_pmac_port *uap = to_pmz(port);
549 unsigned char status;
550
551 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
552 return TIOCSER_TEMT;
553
554 status = pmz_peek_status(to_pmz(port));
555 if (status & Tx_BUF_EMP)
556 return TIOCSER_TEMT;
557 return 0;
558}
559
560/*
561 * Set Modem Control (RTS & DTR) bits
562 * The port lock is held and interrupts are disabled.
563 * Note: Shall we really filter out RTS on external ports or
564 * should that be dealt at higher level only ?
565 */
566static void pmz_set_mctrl(struct uart_port *port, unsigned int mctrl)
567{
568 struct uart_pmac_port *uap = to_pmz(port);
569 unsigned char set_bits, clear_bits;
570
571 /* Do nothing for irda for now... */
572 if (ZS_IS_IRDA(uap))
573 return;
574 /* We get called during boot with a port not up yet */
575 if (ZS_IS_ASLEEP(uap) ||
576 !(ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)))
577 return;
578
579 set_bits = clear_bits = 0;
580
581 if (ZS_IS_INTMODEM(uap)) {
582 if (mctrl & TIOCM_RTS)
583 set_bits |= RTS;
584 else
585 clear_bits |= RTS;
586 }
587 if (mctrl & TIOCM_DTR)
588 set_bits |= DTR;
589 else
590 clear_bits |= DTR;
591
592 /* NOTE: Not subject to 'transmitter active' rule. */
593 uap->curregs[R5] |= set_bits;
594 uap->curregs[R5] &= ~clear_bits;
595 if (ZS_IS_ASLEEP(uap))
596 return;
597 write_zsreg(uap, R5, uap->curregs[R5]);
598 pmz_debug("pmz_set_mctrl: set bits: %x, clear bits: %x -> %x\n",
599 set_bits, clear_bits, uap->curregs[R5]);
600 zssync(uap);
601}
602
603/*
604 * Get Modem Control bits (only the input ones, the core will
605 * or that with a cached value of the control ones)
606 * The port lock is held and interrupts are disabled.
607 */
608static unsigned int pmz_get_mctrl(struct uart_port *port)
609{
610 struct uart_pmac_port *uap = to_pmz(port);
611 unsigned char status;
612 unsigned int ret;
613
614 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
615 return 0;
616
617 status = read_zsreg(uap, R0);
618
619 ret = 0;
620 if (status & DCD)
621 ret |= TIOCM_CAR;
622 if (status & SYNC_HUNT)
623 ret |= TIOCM_DSR;
624 if (!(status & CTS))
625 ret |= TIOCM_CTS;
626
627 return ret;
628}
629
630/*
631 * Stop TX side. Dealt like sunzilog at next Tx interrupt,
632 * though for DMA, we will have to do a bit more.
633 * The port lock is held and interrupts are disabled.
634 */
635static void pmz_stop_tx(struct uart_port *port)
636{
637 to_pmz(port)->flags |= PMACZILOG_FLAG_TX_STOPPED;
638}
639
640/*
641 * Kick the Tx side.
642 * The port lock is held and interrupts are disabled.
643 */
644static void pmz_start_tx(struct uart_port *port)
645{
646 struct uart_pmac_port *uap = to_pmz(port);
647 unsigned char status;
648
649 pmz_debug("pmz: start_tx()\n");
650
651 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
652 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
653
654 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
655 return;
656
657 status = read_zsreg(uap, R0);
658
659 /* TX busy? Just wait for the TX done interrupt. */
660 if (!(status & Tx_BUF_EMP))
661 return;
662
663 /* Send the first character to jump-start the TX done
664 * IRQ sending engine.
665 */
666 if (port->x_char) {
667 write_zsdata(uap, port->x_char);
668 zssync(uap);
669 port->icount.tx++;
670 port->x_char = 0;
671 } else {
672 struct circ_buf *xmit = &port->state->xmit;
673
674 write_zsdata(uap, xmit->buf[xmit->tail]);
675 zssync(uap);
676 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
677 port->icount.tx++;
678
679 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
680 uart_write_wakeup(&uap->port);
681 }
682 pmz_debug("pmz: start_tx() done.\n");
683}
684
685/*
686 * Stop Rx side, basically disable emitting of
687 * Rx interrupts on the port. We don't disable the rx
688 * side of the chip proper though
689 * The port lock is held.
690 */
691static void pmz_stop_rx(struct uart_port *port)
692{
693 struct uart_pmac_port *uap = to_pmz(port);
694
695 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
696 return;
697
698 pmz_debug("pmz: stop_rx()()\n");
699
700 /* Disable all RX interrupts. */
701 uap->curregs[R1] &= ~RxINT_MASK;
702 pmz_maybe_update_regs(uap);
703
704 pmz_debug("pmz: stop_rx() done.\n");
705}
706
707/*
708 * Enable modem status change interrupts
709 * The port lock is held.
710 */
711static void pmz_enable_ms(struct uart_port *port)
712{
713 struct uart_pmac_port *uap = to_pmz(port);
714 unsigned char new_reg;
715
716 if (ZS_IS_IRDA(uap) || uap->node == NULL)
717 return;
718 new_reg = uap->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
719 if (new_reg != uap->curregs[R15]) {
720 uap->curregs[R15] = new_reg;
721
722 if (ZS_IS_ASLEEP(uap))
723 return;
724 /* NOTE: Not subject to 'transmitter active' rule. */
725 write_zsreg(uap, R15, uap->curregs[R15]);
726 }
727}
728
729/*
730 * Control break state emission
731 * The port lock is not held.
732 */
733static void pmz_break_ctl(struct uart_port *port, int break_state)
734{
735 struct uart_pmac_port *uap = to_pmz(port);
736 unsigned char set_bits, clear_bits, new_reg;
737 unsigned long flags;
738
739 if (uap->node == NULL)
740 return;
741 set_bits = clear_bits = 0;
742
743 if (break_state)
744 set_bits |= SND_BRK;
745 else
746 clear_bits |= SND_BRK;
747
748 spin_lock_irqsave(&port->lock, flags);
749
750 new_reg = (uap->curregs[R5] | set_bits) & ~clear_bits;
751 if (new_reg != uap->curregs[R5]) {
752 uap->curregs[R5] = new_reg;
753
754 /* NOTE: Not subject to 'transmitter active' rule. */
755 if (ZS_IS_ASLEEP(uap)) {
756 spin_unlock_irqrestore(&port->lock, flags);
757 return;
758 }
759 write_zsreg(uap, R5, uap->curregs[R5]);
760 }
761
762 spin_unlock_irqrestore(&port->lock, flags);
763}
764
765#ifdef CONFIG_PPC_PMAC
766
767/*
768 * Turn power on or off to the SCC and associated stuff
769 * (port drivers, modem, IR port, etc.)
770 * Returns the number of milliseconds we should wait before
771 * trying to use the port.
772 */
773static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
774{
775 int delay = 0;
776 int rc;
777
778 if (state) {
779 rc = pmac_call_feature(
780 PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 1);
781 pmz_debug("port power on result: %d\n", rc);
782 if (ZS_IS_INTMODEM(uap)) {
783 rc = pmac_call_feature(
784 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 1);
785 delay = 2500; /* wait for 2.5s before using */
786 pmz_debug("modem power result: %d\n", rc);
787 }
788 } else {
789 /* TODO: Make that depend on a timer, don't power down
790 * immediately
791 */
792 if (ZS_IS_INTMODEM(uap)) {
793 rc = pmac_call_feature(
794 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 0);
795 pmz_debug("port power off result: %d\n", rc);
796 }
797 pmac_call_feature(PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 0);
798 }
799 return delay;
800}
801
802#else
803
804static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
805{
806 return 0;
807}
808
809#endif /* !CONFIG_PPC_PMAC */
810
811/*
812 * FixZeroBug....Works around a bug in the SCC receving channel.
813 * Inspired from Darwin code, 15 Sept. 2000 -DanM
814 *
815 * The following sequence prevents a problem that is seen with O'Hare ASICs
816 * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
817 * at the input to the receiver becomes 'stuck' and locks up the receiver.
818 * This problem can occur as a result of a zero bit at the receiver input
819 * coincident with any of the following events:
820 *
821 * The SCC is initialized (hardware or software).
822 * A framing error is detected.
823 * The clocking option changes from synchronous or X1 asynchronous
824 * clocking to X16, X32, or X64 asynchronous clocking.
825 * The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
826 *
827 * This workaround attempts to recover from the lockup condition by placing
828 * the SCC in synchronous loopback mode with a fast clock before programming
829 * any of the asynchronous modes.
830 */
831static void pmz_fix_zero_bug_scc(struct uart_pmac_port *uap)
832{
833 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
834 zssync(uap);
835 udelay(10);
836 write_zsreg(uap, 9, (ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB) | NV);
837 zssync(uap);
838
839 write_zsreg(uap, 4, X1CLK | MONSYNC);
840 write_zsreg(uap, 3, Rx8);
841 write_zsreg(uap, 5, Tx8 | RTS);
842 write_zsreg(uap, 9, NV); /* Didn't we already do this? */
843 write_zsreg(uap, 11, RCBR | TCBR);
844 write_zsreg(uap, 12, 0);
845 write_zsreg(uap, 13, 0);
846 write_zsreg(uap, 14, (LOOPBAK | BRSRC));
847 write_zsreg(uap, 14, (LOOPBAK | BRSRC | BRENAB));
848 write_zsreg(uap, 3, Rx8 | RxENABLE);
849 write_zsreg(uap, 0, RES_EXT_INT);
850 write_zsreg(uap, 0, RES_EXT_INT);
851 write_zsreg(uap, 0, RES_EXT_INT); /* to kill some time */
852
853 /* The channel should be OK now, but it is probably receiving
854 * loopback garbage.
855 * Switch to asynchronous mode, disable the receiver,
856 * and discard everything in the receive buffer.
857 */
858 write_zsreg(uap, 9, NV);
859 write_zsreg(uap, 4, X16CLK | SB_MASK);
860 write_zsreg(uap, 3, Rx8);
861
862 while (read_zsreg(uap, 0) & Rx_CH_AV) {
863 (void)read_zsreg(uap, 8);
864 write_zsreg(uap, 0, RES_EXT_INT);
865 write_zsreg(uap, 0, ERR_RES);
866 }
867}
868
869/*
870 * Real startup routine, powers up the hardware and sets up
871 * the SCC. Returns a delay in ms where you need to wait before
872 * actually using the port, this is typically the internal modem
873 * powerup delay. This routine expect the lock to be taken.
874 */
875static int __pmz_startup(struct uart_pmac_port *uap)
876{
877 int pwr_delay = 0;
878
879 memset(&uap->curregs, 0, sizeof(uap->curregs));
880
881 /* Power up the SCC & underlying hardware (modem/irda) */
882 pwr_delay = pmz_set_scc_power(uap, 1);
883
884 /* Nice buggy HW ... */
885 pmz_fix_zero_bug_scc(uap);
886
887 /* Reset the channel */
888 uap->curregs[R9] = 0;
889 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
890 zssync(uap);
891 udelay(10);
892 write_zsreg(uap, 9, 0);
893 zssync(uap);
894
895 /* Clear the interrupt registers */
896 write_zsreg(uap, R1, 0);
897 write_zsreg(uap, R0, ERR_RES);
898 write_zsreg(uap, R0, ERR_RES);
899 write_zsreg(uap, R0, RES_H_IUS);
900 write_zsreg(uap, R0, RES_H_IUS);
901
902 /* Setup some valid baud rate */
903 uap->curregs[R4] = X16CLK | SB1;
904 uap->curregs[R3] = Rx8;
905 uap->curregs[R5] = Tx8 | RTS;
906 if (!ZS_IS_IRDA(uap))
907 uap->curregs[R5] |= DTR;
908 uap->curregs[R12] = 0;
909 uap->curregs[R13] = 0;
910 uap->curregs[R14] = BRENAB;
911
912 /* Clear handshaking, enable BREAK interrupts */
913 uap->curregs[R15] = BRKIE;
914
915 /* Master interrupt enable */
916 uap->curregs[R9] |= NV | MIE;
917
918 pmz_load_zsregs(uap, uap->curregs);
919
920 /* Enable receiver and transmitter. */
921 write_zsreg(uap, R3, uap->curregs[R3] |= RxENABLE);
922 write_zsreg(uap, R5, uap->curregs[R5] |= TxENABLE);
923
924 /* Remember status for DCD/CTS changes */
925 uap->prev_status = read_zsreg(uap, R0);
926
927 return pwr_delay;
928}
929
930static void pmz_irda_reset(struct uart_pmac_port *uap)
931{
932 uap->curregs[R5] |= DTR;
933 write_zsreg(uap, R5, uap->curregs[R5]);
934 zssync(uap);
935 mdelay(110);
936 uap->curregs[R5] &= ~DTR;
937 write_zsreg(uap, R5, uap->curregs[R5]);
938 zssync(uap);
939 mdelay(10);
940}
941
942/*
943 * This is the "normal" startup routine, using the above one
944 * wrapped with the lock and doing a schedule delay
945 */
946static int pmz_startup(struct uart_port *port)
947{
948 struct uart_pmac_port *uap = to_pmz(port);
949 unsigned long flags;
950 int pwr_delay = 0;
951
952 pmz_debug("pmz: startup()\n");
953
954 if (ZS_IS_ASLEEP(uap))
955 return -EAGAIN;
956 if (uap->node == NULL)
957 return -ENODEV;
958
959 mutex_lock(&pmz_irq_mutex);
960
961 uap->flags |= PMACZILOG_FLAG_IS_OPEN;
962
963 /* A console is never powered down. Else, power up and
964 * initialize the chip
965 */
966 if (!ZS_IS_CONS(uap)) {
967 spin_lock_irqsave(&port->lock, flags);
968 pwr_delay = __pmz_startup(uap);
969 spin_unlock_irqrestore(&port->lock, flags);
970 }
971
972 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
973 if (request_irq(uap->port.irq, pmz_interrupt, IRQF_SHARED,
974 "SCC", uap)) {
975 pmz_error("Unable to register zs interrupt handler.\n");
976 pmz_set_scc_power(uap, 0);
977 mutex_unlock(&pmz_irq_mutex);
978 return -ENXIO;
979 }
980
981 mutex_unlock(&pmz_irq_mutex);
982
983 /* Right now, we deal with delay by blocking here, I'll be
984 * smarter later on
985 */
986 if (pwr_delay != 0) {
987 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
988 msleep(pwr_delay);
989 }
990
991 /* IrDA reset is done now */
992 if (ZS_IS_IRDA(uap))
993 pmz_irda_reset(uap);
994
995 /* Enable interrupts emission from the chip */
996 spin_lock_irqsave(&port->lock, flags);
997 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
998 if (!ZS_IS_EXTCLK(uap))
999 uap->curregs[R1] |= EXT_INT_ENAB;
1000 write_zsreg(uap, R1, uap->curregs[R1]);
1001 spin_unlock_irqrestore(&port->lock, flags);
1002
1003 pmz_debug("pmz: startup() done.\n");
1004
1005 return 0;
1006}
1007
1008static void pmz_shutdown(struct uart_port *port)
1009{
1010 struct uart_pmac_port *uap = to_pmz(port);
1011 unsigned long flags;
1012
1013 pmz_debug("pmz: shutdown()\n");
1014
1015 if (uap->node == NULL)
1016 return;
1017
1018 mutex_lock(&pmz_irq_mutex);
1019
1020 /* Release interrupt handler */
1021 free_irq(uap->port.irq, uap);
1022
1023 spin_lock_irqsave(&port->lock, flags);
1024
1025 uap->flags &= ~PMACZILOG_FLAG_IS_OPEN;
1026
1027 if (!ZS_IS_OPEN(uap->mate))
1028 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1029
1030 /* Disable interrupts */
1031 if (!ZS_IS_ASLEEP(uap)) {
1032 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1033 write_zsreg(uap, R1, uap->curregs[R1]);
1034 zssync(uap);
1035 }
1036
1037 if (ZS_IS_CONS(uap) || ZS_IS_ASLEEP(uap)) {
1038 spin_unlock_irqrestore(&port->lock, flags);
1039 mutex_unlock(&pmz_irq_mutex);
1040 return;
1041 }
1042
1043 /* Disable receiver and transmitter. */
1044 uap->curregs[R3] &= ~RxENABLE;
1045 uap->curregs[R5] &= ~TxENABLE;
1046
1047 /* Disable all interrupts and BRK assertion. */
1048 uap->curregs[R5] &= ~SND_BRK;
1049 pmz_maybe_update_regs(uap);
1050
1051 /* Shut the chip down */
1052 pmz_set_scc_power(uap, 0);
1053
1054 spin_unlock_irqrestore(&port->lock, flags);
1055
1056 mutex_unlock(&pmz_irq_mutex);
1057
1058 pmz_debug("pmz: shutdown() done.\n");
1059}
1060
1061/* Shared by TTY driver and serial console setup. The port lock is held
1062 * and local interrupts are disabled.
1063 */
1064static void pmz_convert_to_zs(struct uart_pmac_port *uap, unsigned int cflag,
1065 unsigned int iflag, unsigned long baud)
1066{
1067 int brg;
1068
1069 /* Switch to external clocking for IrDA high clock rates. That
1070 * code could be re-used for Midi interfaces with different
1071 * multipliers
1072 */
1073 if (baud >= 115200 && ZS_IS_IRDA(uap)) {
1074 uap->curregs[R4] = X1CLK;
1075 uap->curregs[R11] = RCTRxCP | TCTRxCP;
1076 uap->curregs[R14] = 0; /* BRG off */
1077 uap->curregs[R12] = 0;
1078 uap->curregs[R13] = 0;
1079 uap->flags |= PMACZILOG_FLAG_IS_EXTCLK;
1080 } else {
1081 switch (baud) {
1082 case ZS_CLOCK/16: /* 230400 */
1083 uap->curregs[R4] = X16CLK;
1084 uap->curregs[R11] = 0;
1085 uap->curregs[R14] = 0;
1086 break;
1087 case ZS_CLOCK/32: /* 115200 */
1088 uap->curregs[R4] = X32CLK;
1089 uap->curregs[R11] = 0;
1090 uap->curregs[R14] = 0;
1091 break;
1092 default:
1093 uap->curregs[R4] = X16CLK;
1094 uap->curregs[R11] = TCBR | RCBR;
1095 brg = BPS_TO_BRG(baud, ZS_CLOCK / 16);
1096 uap->curregs[R12] = (brg & 255);
1097 uap->curregs[R13] = ((brg >> 8) & 255);
1098 uap->curregs[R14] = BRENAB;
1099 }
1100 uap->flags &= ~PMACZILOG_FLAG_IS_EXTCLK;
1101 }
1102
1103 /* Character size, stop bits, and parity. */
1104 uap->curregs[3] &= ~RxN_MASK;
1105 uap->curregs[5] &= ~TxN_MASK;
1106
1107 switch (cflag & CSIZE) {
1108 case CS5:
1109 uap->curregs[3] |= Rx5;
1110 uap->curregs[5] |= Tx5;
1111 uap->parity_mask = 0x1f;
1112 break;
1113 case CS6:
1114 uap->curregs[3] |= Rx6;
1115 uap->curregs[5] |= Tx6;
1116 uap->parity_mask = 0x3f;
1117 break;
1118 case CS7:
1119 uap->curregs[3] |= Rx7;
1120 uap->curregs[5] |= Tx7;
1121 uap->parity_mask = 0x7f;
1122 break;
1123 case CS8:
1124 default:
1125 uap->curregs[3] |= Rx8;
1126 uap->curregs[5] |= Tx8;
1127 uap->parity_mask = 0xff;
1128 break;
1129 };
1130 uap->curregs[4] &= ~(SB_MASK);
1131 if (cflag & CSTOPB)
1132 uap->curregs[4] |= SB2;
1133 else
1134 uap->curregs[4] |= SB1;
1135 if (cflag & PARENB)
1136 uap->curregs[4] |= PAR_ENAB;
1137 else
1138 uap->curregs[4] &= ~PAR_ENAB;
1139 if (!(cflag & PARODD))
1140 uap->curregs[4] |= PAR_EVEN;
1141 else
1142 uap->curregs[4] &= ~PAR_EVEN;
1143
1144 uap->port.read_status_mask = Rx_OVR;
1145 if (iflag & INPCK)
1146 uap->port.read_status_mask |= CRC_ERR | PAR_ERR;
1147 if (iflag & (BRKINT | PARMRK))
1148 uap->port.read_status_mask |= BRK_ABRT;
1149
1150 uap->port.ignore_status_mask = 0;
1151 if (iflag & IGNPAR)
1152 uap->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
1153 if (iflag & IGNBRK) {
1154 uap->port.ignore_status_mask |= BRK_ABRT;
1155 if (iflag & IGNPAR)
1156 uap->port.ignore_status_mask |= Rx_OVR;
1157 }
1158
1159 if ((cflag & CREAD) == 0)
1160 uap->port.ignore_status_mask = 0xff;
1161}
1162
1163
1164/*
1165 * Set the irda codec on the imac to the specified baud rate.
1166 */
1167static void pmz_irda_setup(struct uart_pmac_port *uap, unsigned long *baud)
1168{
1169 u8 cmdbyte;
1170 int t, version;
1171
1172 switch (*baud) {
1173 /* SIR modes */
1174 case 2400:
1175 cmdbyte = 0x53;
1176 break;
1177 case 4800:
1178 cmdbyte = 0x52;
1179 break;
1180 case 9600:
1181 cmdbyte = 0x51;
1182 break;
1183 case 19200:
1184 cmdbyte = 0x50;
1185 break;
1186 case 38400:
1187 cmdbyte = 0x4f;
1188 break;
1189 case 57600:
1190 cmdbyte = 0x4e;
1191 break;
1192 case 115200:
1193 cmdbyte = 0x4d;
1194 break;
1195 /* The FIR modes aren't really supported at this point, how
1196 * do we select the speed ? via the FCR on KeyLargo ?
1197 */
1198 case 1152000:
1199 cmdbyte = 0;
1200 break;
1201 case 4000000:
1202 cmdbyte = 0;
1203 break;
1204 default: /* 9600 */
1205 cmdbyte = 0x51;
1206 *baud = 9600;
1207 break;
1208 }
1209
1210 /* Wait for transmitter to drain */
1211 t = 10000;
1212 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0
1213 || (read_zsreg(uap, R1) & ALL_SNT) == 0) {
1214 if (--t <= 0) {
1215 pmz_error("transmitter didn't drain\n");
1216 return;
1217 }
1218 udelay(10);
1219 }
1220
1221 /* Drain the receiver too */
1222 t = 100;
1223 (void)read_zsdata(uap);
1224 (void)read_zsdata(uap);
1225 (void)read_zsdata(uap);
1226 mdelay(10);
1227 while (read_zsreg(uap, R0) & Rx_CH_AV) {
1228 read_zsdata(uap);
1229 mdelay(10);
1230 if (--t <= 0) {
1231 pmz_error("receiver didn't drain\n");
1232 return;
1233 }
1234 }
1235
1236 /* Switch to command mode */
1237 uap->curregs[R5] |= DTR;
1238 write_zsreg(uap, R5, uap->curregs[R5]);
1239 zssync(uap);
1240 mdelay(1);
1241
1242 /* Switch SCC to 19200 */
1243 pmz_convert_to_zs(uap, CS8, 0, 19200);
1244 pmz_load_zsregs(uap, uap->curregs);
1245 mdelay(1);
1246
1247 /* Write get_version command byte */
1248 write_zsdata(uap, 1);
1249 t = 5000;
1250 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1251 if (--t <= 0) {
1252 pmz_error("irda_setup timed out on get_version byte\n");
1253 goto out;
1254 }
1255 udelay(10);
1256 }
1257 version = read_zsdata(uap);
1258
1259 if (version < 4) {
1260 pmz_info("IrDA: dongle version %d not supported\n", version);
1261 goto out;
1262 }
1263
1264 /* Send speed mode */
1265 write_zsdata(uap, cmdbyte);
1266 t = 5000;
1267 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1268 if (--t <= 0) {
1269 pmz_error("irda_setup timed out on speed mode byte\n");
1270 goto out;
1271 }
1272 udelay(10);
1273 }
1274 t = read_zsdata(uap);
1275 if (t != cmdbyte)
1276 pmz_error("irda_setup speed mode byte = %x (%x)\n", t, cmdbyte);
1277
1278 pmz_info("IrDA setup for %ld bps, dongle version: %d\n",
1279 *baud, version);
1280
1281 (void)read_zsdata(uap);
1282 (void)read_zsdata(uap);
1283 (void)read_zsdata(uap);
1284
1285 out:
1286 /* Switch back to data mode */
1287 uap->curregs[R5] &= ~DTR;
1288 write_zsreg(uap, R5, uap->curregs[R5]);
1289 zssync(uap);
1290
1291 (void)read_zsdata(uap);
1292 (void)read_zsdata(uap);
1293 (void)read_zsdata(uap);
1294}
1295
1296
1297static void __pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1298 struct ktermios *old)
1299{
1300 struct uart_pmac_port *uap = to_pmz(port);
1301 unsigned long baud;
1302
1303 pmz_debug("pmz: set_termios()\n");
1304
1305 if (ZS_IS_ASLEEP(uap))
1306 return;
1307
1308 memcpy(&uap->termios_cache, termios, sizeof(struct ktermios));
1309
1310 /* XXX Check which revs of machines actually allow 1 and 4Mb speeds
1311 * on the IR dongle. Note that the IRTTY driver currently doesn't know
1312 * about the FIR mode and high speed modes. So these are unused. For
1313 * implementing proper support for these, we should probably add some
1314 * DMA as well, at least on the Rx side, which isn't a simple thing
1315 * at this point.
1316 */
1317 if (ZS_IS_IRDA(uap)) {
1318 /* Calc baud rate */
1319 baud = uart_get_baud_rate(port, termios, old, 1200, 4000000);
1320 pmz_debug("pmz: switch IRDA to %ld bauds\n", baud);
1321 /* Cet the irda codec to the right rate */
1322 pmz_irda_setup(uap, &baud);
1323 /* Set final baud rate */
1324 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1325 pmz_load_zsregs(uap, uap->curregs);
1326 zssync(uap);
1327 } else {
1328 baud = uart_get_baud_rate(port, termios, old, 1200, 230400);
1329 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1330 /* Make sure modem status interrupts are correctly configured */
1331 if (UART_ENABLE_MS(&uap->port, termios->c_cflag)) {
1332 uap->curregs[R15] |= DCDIE | SYNCIE | CTSIE;
1333 uap->flags |= PMACZILOG_FLAG_MODEM_STATUS;
1334 } else {
1335 uap->curregs[R15] &= ~(DCDIE | SYNCIE | CTSIE);
1336 uap->flags &= ~PMACZILOG_FLAG_MODEM_STATUS;
1337 }
1338
1339 /* Load registers to the chip */
1340 pmz_maybe_update_regs(uap);
1341 }
1342 uart_update_timeout(port, termios->c_cflag, baud);
1343
1344 pmz_debug("pmz: set_termios() done.\n");
1345}
1346
1347/* The port lock is not held. */
1348static void pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1349 struct ktermios *old)
1350{
1351 struct uart_pmac_port *uap = to_pmz(port);
1352 unsigned long flags;
1353
1354 spin_lock_irqsave(&port->lock, flags);
1355
1356 /* Disable IRQs on the port */
1357 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1358 write_zsreg(uap, R1, uap->curregs[R1]);
1359
1360 /* Setup new port configuration */
1361 __pmz_set_termios(port, termios, old);
1362
1363 /* Re-enable IRQs on the port */
1364 if (ZS_IS_OPEN(uap)) {
1365 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1366 if (!ZS_IS_EXTCLK(uap))
1367 uap->curregs[R1] |= EXT_INT_ENAB;
1368 write_zsreg(uap, R1, uap->curregs[R1]);
1369 }
1370 spin_unlock_irqrestore(&port->lock, flags);
1371}
1372
1373static const char *pmz_type(struct uart_port *port)
1374{
1375 struct uart_pmac_port *uap = to_pmz(port);
1376
1377 if (ZS_IS_IRDA(uap))
1378 return "Z85c30 ESCC - Infrared port";
1379 else if (ZS_IS_INTMODEM(uap))
1380 return "Z85c30 ESCC - Internal modem";
1381 return "Z85c30 ESCC - Serial port";
1382}
1383
1384/* We do not request/release mappings of the registers here, this
1385 * happens at early serial probe time.
1386 */
1387static void pmz_release_port(struct uart_port *port)
1388{
1389}
1390
1391static int pmz_request_port(struct uart_port *port)
1392{
1393 return 0;
1394}
1395
1396/* These do not need to do anything interesting either. */
1397static void pmz_config_port(struct uart_port *port, int flags)
1398{
1399}
1400
1401/* We do not support letting the user mess with the divisor, IRQ, etc. */
1402static int pmz_verify_port(struct uart_port *port, struct serial_struct *ser)
1403{
1404 return -EINVAL;
1405}
1406
1407#ifdef CONFIG_CONSOLE_POLL
1408
1409static int pmz_poll_get_char(struct uart_port *port)
1410{
1411 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1412
1413 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0)
1414 udelay(5);
1415 return read_zsdata(uap);
1416}
1417
1418static void pmz_poll_put_char(struct uart_port *port, unsigned char c)
1419{
1420 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1421
1422 /* Wait for the transmit buffer to empty. */
1423 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
1424 udelay(5);
1425 write_zsdata(uap, c);
1426}
1427
1428#endif /* CONFIG_CONSOLE_POLL */
1429
1430static struct uart_ops pmz_pops = {
1431 .tx_empty = pmz_tx_empty,
1432 .set_mctrl = pmz_set_mctrl,
1433 .get_mctrl = pmz_get_mctrl,
1434 .stop_tx = pmz_stop_tx,
1435 .start_tx = pmz_start_tx,
1436 .stop_rx = pmz_stop_rx,
1437 .enable_ms = pmz_enable_ms,
1438 .break_ctl = pmz_break_ctl,
1439 .startup = pmz_startup,
1440 .shutdown = pmz_shutdown,
1441 .set_termios = pmz_set_termios,
1442 .type = pmz_type,
1443 .release_port = pmz_release_port,
1444 .request_port = pmz_request_port,
1445 .config_port = pmz_config_port,
1446 .verify_port = pmz_verify_port,
1447#ifdef CONFIG_CONSOLE_POLL
1448 .poll_get_char = pmz_poll_get_char,
1449 .poll_put_char = pmz_poll_put_char,
1450#endif
1451};
1452
1453#ifdef CONFIG_PPC_PMAC
1454
1455/*
1456 * Setup one port structure after probing, HW is down at this point,
1457 * Unlike sunzilog, we don't need to pre-init the spinlock as we don't
1458 * register our console before uart_add_one_port() is called
1459 */
1460static int __init pmz_init_port(struct uart_pmac_port *uap)
1461{
1462 struct device_node *np = uap->node;
1463 const char *conn;
1464 const struct slot_names_prop {
1465 int count;
1466 char name[1];
1467 } *slots;
1468 int len;
1469 struct resource r_ports, r_rxdma, r_txdma;
1470
1471 /*
1472 * Request & map chip registers
1473 */
1474 if (of_address_to_resource(np, 0, &r_ports))
1475 return -ENODEV;
1476 uap->port.mapbase = r_ports.start;
1477 uap->port.membase = ioremap(uap->port.mapbase, 0x1000);
1478
1479 uap->control_reg = uap->port.membase;
1480 uap->data_reg = uap->control_reg + 0x10;
1481
1482 /*
1483 * Request & map DBDMA registers
1484 */
1485#ifdef HAS_DBDMA
1486 if (of_address_to_resource(np, 1, &r_txdma) == 0 &&
1487 of_address_to_resource(np, 2, &r_rxdma) == 0)
1488 uap->flags |= PMACZILOG_FLAG_HAS_DMA;
1489#else
1490 memset(&r_txdma, 0, sizeof(struct resource));
1491 memset(&r_rxdma, 0, sizeof(struct resource));
1492#endif
1493 if (ZS_HAS_DMA(uap)) {
1494 uap->tx_dma_regs = ioremap(r_txdma.start, 0x100);
1495 if (uap->tx_dma_regs == NULL) {
1496 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1497 goto no_dma;
1498 }
1499 uap->rx_dma_regs = ioremap(r_rxdma.start, 0x100);
1500 if (uap->rx_dma_regs == NULL) {
1501 iounmap(uap->tx_dma_regs);
1502 uap->tx_dma_regs = NULL;
1503 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1504 goto no_dma;
1505 }
1506 uap->tx_dma_irq = irq_of_parse_and_map(np, 1);
1507 uap->rx_dma_irq = irq_of_parse_and_map(np, 2);
1508 }
1509no_dma:
1510
1511 /*
1512 * Detect port type
1513 */
1514 if (of_device_is_compatible(np, "cobalt"))
1515 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1516 conn = of_get_property(np, "AAPL,connector", &len);
1517 if (conn && (strcmp(conn, "infrared") == 0))
1518 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1519 uap->port_type = PMAC_SCC_ASYNC;
1520 /* 1999 Powerbook G3 has slot-names property instead */
1521 slots = of_get_property(np, "slot-names", &len);
1522 if (slots && slots->count > 0) {
1523 if (strcmp(slots->name, "IrDA") == 0)
1524 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1525 else if (strcmp(slots->name, "Modem") == 0)
1526 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1527 }
1528 if (ZS_IS_IRDA(uap))
1529 uap->port_type = PMAC_SCC_IRDA;
1530 if (ZS_IS_INTMODEM(uap)) {
1531 struct device_node* i2c_modem =
1532 of_find_node_by_name(NULL, "i2c-modem");
1533 if (i2c_modem) {
1534 const char* mid =
1535 of_get_property(i2c_modem, "modem-id", NULL);
1536 if (mid) switch(*mid) {
1537 case 0x04 :
1538 case 0x05 :
1539 case 0x07 :
1540 case 0x08 :
1541 case 0x0b :
1542 case 0x0c :
1543 uap->port_type = PMAC_SCC_I2S1;
1544 }
1545 printk(KERN_INFO "pmac_zilog: i2c-modem detected, id: %d\n",
1546 mid ? (*mid) : 0);
1547 of_node_put(i2c_modem);
1548 } else {
1549 printk(KERN_INFO "pmac_zilog: serial modem detected\n");
1550 }
1551 }
1552
1553 /*
1554 * Init remaining bits of "port" structure
1555 */
1556 uap->port.iotype = UPIO_MEM;
1557 uap->port.irq = irq_of_parse_and_map(np, 0);
1558 uap->port.uartclk = ZS_CLOCK;
1559 uap->port.fifosize = 1;
1560 uap->port.ops = &pmz_pops;
1561 uap->port.type = PORT_PMAC_ZILOG;
1562 uap->port.flags = 0;
1563
1564 /*
1565 * Fixup for the port on Gatwick for which the device-tree has
1566 * missing interrupts. Normally, the macio_dev would contain
1567 * fixed up interrupt info, but we use the device-tree directly
1568 * here due to early probing so we need the fixup too.
1569 */
1570 if (uap->port.irq == NO_IRQ &&
1571 np->parent && np->parent->parent &&
1572 of_device_is_compatible(np->parent->parent, "gatwick")) {
1573 /* IRQs on gatwick are offset by 64 */
1574 uap->port.irq = irq_create_mapping(NULL, 64 + 15);
1575 uap->tx_dma_irq = irq_create_mapping(NULL, 64 + 4);
1576 uap->rx_dma_irq = irq_create_mapping(NULL, 64 + 5);
1577 }
1578
1579 /* Setup some valid baud rate information in the register
1580 * shadows so we don't write crap there before baud rate is
1581 * first initialized.
1582 */
1583 pmz_convert_to_zs(uap, CS8, 0, 9600);
1584
1585 return 0;
1586}
1587
1588/*
1589 * Get rid of a port on module removal
1590 */
1591static void pmz_dispose_port(struct uart_pmac_port *uap)
1592{
1593 struct device_node *np;
1594
1595 np = uap->node;
1596 iounmap(uap->rx_dma_regs);
1597 iounmap(uap->tx_dma_regs);
1598 iounmap(uap->control_reg);
1599 uap->node = NULL;
1600 of_node_put(np);
1601 memset(uap, 0, sizeof(struct uart_pmac_port));
1602}
1603
1604/*
1605 * Called upon match with an escc node in the device-tree.
1606 */
1607static int pmz_attach(struct macio_dev *mdev, const struct of_device_id *match)
1608{
1609 int i;
1610
1611 /* Iterate the pmz_ports array to find a matching entry
1612 */
1613 for (i = 0; i < MAX_ZS_PORTS; i++)
1614 if (pmz_ports[i].node == mdev->ofdev.dev.of_node) {
1615 struct uart_pmac_port *uap = &pmz_ports[i];
1616
1617 uap->dev = mdev;
1618 dev_set_drvdata(&mdev->ofdev.dev, uap);
1619 if (macio_request_resources(uap->dev, "pmac_zilog"))
1620 printk(KERN_WARNING "%s: Failed to request resource"
1621 ", port still active\n",
1622 uap->node->name);
1623 else
1624 uap->flags |= PMACZILOG_FLAG_RSRC_REQUESTED;
1625 return 0;
1626 }
1627 return -ENODEV;
1628}
1629
1630/*
1631 * That one should not be called, macio isn't really a hotswap device,
1632 * we don't expect one of those serial ports to go away...
1633 */
1634static int pmz_detach(struct macio_dev *mdev)
1635{
1636 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1637
1638 if (!uap)
1639 return -ENODEV;
1640
1641 if (uap->flags & PMACZILOG_FLAG_RSRC_REQUESTED) {
1642 macio_release_resources(uap->dev);
1643 uap->flags &= ~PMACZILOG_FLAG_RSRC_REQUESTED;
1644 }
1645 dev_set_drvdata(&mdev->ofdev.dev, NULL);
1646 uap->dev = NULL;
1647
1648 return 0;
1649}
1650
1651
1652static int pmz_suspend(struct macio_dev *mdev, pm_message_t pm_state)
1653{
1654 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1655 struct uart_state *state;
1656 unsigned long flags;
1657
1658 if (uap == NULL) {
1659 printk("HRM... pmz_suspend with NULL uap\n");
1660 return 0;
1661 }
1662
1663 if (pm_state.event == mdev->ofdev.dev.power.power_state.event)
1664 return 0;
1665
1666 pmz_debug("suspend, switching to state %d\n", pm_state.event);
1667
1668 state = pmz_uart_reg.state + uap->port.line;
1669
1670 mutex_lock(&pmz_irq_mutex);
1671 mutex_lock(&state->port.mutex);
1672
1673 spin_lock_irqsave(&uap->port.lock, flags);
1674
1675 if (ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)) {
1676 /* Disable receiver and transmitter. */
1677 uap->curregs[R3] &= ~RxENABLE;
1678 uap->curregs[R5] &= ~TxENABLE;
1679
1680 /* Disable all interrupts and BRK assertion. */
1681 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1682 uap->curregs[R5] &= ~SND_BRK;
1683 pmz_load_zsregs(uap, uap->curregs);
1684 uap->flags |= PMACZILOG_FLAG_IS_ASLEEP;
1685 mb();
1686 }
1687
1688 spin_unlock_irqrestore(&uap->port.lock, flags);
1689
1690 if (ZS_IS_OPEN(uap) || ZS_IS_OPEN(uap->mate))
1691 if (ZS_IS_ASLEEP(uap->mate) && ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1692 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1693 disable_irq(uap->port.irq);
1694 }
1695
1696 if (ZS_IS_CONS(uap))
1697 uap->port.cons->flags &= ~CON_ENABLED;
1698
1699 /* Shut the chip down */
1700 pmz_set_scc_power(uap, 0);
1701
1702 mutex_unlock(&state->port.mutex);
1703 mutex_unlock(&pmz_irq_mutex);
1704
1705 pmz_debug("suspend, switching complete\n");
1706
1707 mdev->ofdev.dev.power.power_state = pm_state;
1708
1709 return 0;
1710}
1711
1712
1713static int pmz_resume(struct macio_dev *mdev)
1714{
1715 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1716 struct uart_state *state;
1717 unsigned long flags;
1718 int pwr_delay = 0;
1719
1720 if (uap == NULL)
1721 return 0;
1722
1723 if (mdev->ofdev.dev.power.power_state.event == PM_EVENT_ON)
1724 return 0;
1725
1726 pmz_debug("resume, switching to state 0\n");
1727
1728 state = pmz_uart_reg.state + uap->port.line;
1729
1730 mutex_lock(&pmz_irq_mutex);
1731 mutex_lock(&state->port.mutex);
1732
1733 spin_lock_irqsave(&uap->port.lock, flags);
1734 if (!ZS_IS_OPEN(uap) && !ZS_IS_CONS(uap)) {
1735 spin_unlock_irqrestore(&uap->port.lock, flags);
1736 goto bail;
1737 }
1738 pwr_delay = __pmz_startup(uap);
1739
1740 /* Take care of config that may have changed while asleep */
1741 __pmz_set_termios(&uap->port, &uap->termios_cache, NULL);
1742
1743 if (ZS_IS_OPEN(uap)) {
1744 /* Enable interrupts */
1745 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1746 if (!ZS_IS_EXTCLK(uap))
1747 uap->curregs[R1] |= EXT_INT_ENAB;
1748 write_zsreg(uap, R1, uap->curregs[R1]);
1749 }
1750
1751 spin_unlock_irqrestore(&uap->port.lock, flags);
1752
1753 if (ZS_IS_CONS(uap))
1754 uap->port.cons->flags |= CON_ENABLED;
1755
1756 /* Re-enable IRQ on the controller */
1757 if (ZS_IS_OPEN(uap) && !ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1758 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
1759 enable_irq(uap->port.irq);
1760 }
1761
1762 bail:
1763 mutex_unlock(&state->port.mutex);
1764 mutex_unlock(&pmz_irq_mutex);
1765
1766 /* Right now, we deal with delay by blocking here, I'll be
1767 * smarter later on
1768 */
1769 if (pwr_delay != 0) {
1770 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
1771 msleep(pwr_delay);
1772 }
1773
1774 pmz_debug("resume, switching complete\n");
1775
1776 mdev->ofdev.dev.power.power_state.event = PM_EVENT_ON;
1777
1778 return 0;
1779}
1780
1781/*
1782 * Probe all ports in the system and build the ports array, we register
1783 * with the serial layer at this point, the macio-type probing is only
1784 * used later to "attach" to the sysfs tree so we get power management
1785 * events
1786 */
1787static int __init pmz_probe(void)
1788{
1789 struct device_node *node_p, *node_a, *node_b, *np;
1790 int count = 0;
1791 int rc;
1792
1793 /*
1794 * Find all escc chips in the system
1795 */
1796 node_p = of_find_node_by_name(NULL, "escc");
1797 while (node_p) {
1798 /*
1799 * First get channel A/B node pointers
1800 *
1801 * TODO: Add routines with proper locking to do that...
1802 */
1803 node_a = node_b = NULL;
1804 for (np = NULL; (np = of_get_next_child(node_p, np)) != NULL;) {
1805 if (strncmp(np->name, "ch-a", 4) == 0)
1806 node_a = of_node_get(np);
1807 else if (strncmp(np->name, "ch-b", 4) == 0)
1808 node_b = of_node_get(np);
1809 }
1810 if (!node_a && !node_b) {
1811 of_node_put(node_a);
1812 of_node_put(node_b);
1813 printk(KERN_ERR "pmac_zilog: missing node %c for escc %s\n",
1814 (!node_a) ? 'a' : 'b', node_p->full_name);
1815 goto next;
1816 }
1817
1818 /*
1819 * Fill basic fields in the port structures
1820 */
1821 pmz_ports[count].mate = &pmz_ports[count+1];
1822 pmz_ports[count+1].mate = &pmz_ports[count];
1823 pmz_ports[count].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1824 pmz_ports[count].node = node_a;
1825 pmz_ports[count+1].node = node_b;
1826 pmz_ports[count].port.line = count;
1827 pmz_ports[count+1].port.line = count+1;
1828
1829 /*
1830 * Setup the ports for real
1831 */
1832 rc = pmz_init_port(&pmz_ports[count]);
1833 if (rc == 0 && node_b != NULL)
1834 rc = pmz_init_port(&pmz_ports[count+1]);
1835 if (rc != 0) {
1836 of_node_put(node_a);
1837 of_node_put(node_b);
1838 memset(&pmz_ports[count], 0, sizeof(struct uart_pmac_port));
1839 memset(&pmz_ports[count+1], 0, sizeof(struct uart_pmac_port));
1840 goto next;
1841 }
1842 count += 2;
1843next:
1844 node_p = of_find_node_by_name(node_p, "escc");
1845 }
1846 pmz_ports_count = count;
1847
1848 return 0;
1849}
1850
1851#else
1852
1853extern struct platform_device scc_a_pdev, scc_b_pdev;
1854
1855static int __init pmz_init_port(struct uart_pmac_port *uap)
1856{
1857 struct resource *r_ports;
1858 int irq;
1859
1860 r_ports = platform_get_resource(uap->node, IORESOURCE_MEM, 0);
1861 irq = platform_get_irq(uap->node, 0);
1862 if (!r_ports || !irq)
1863 return -ENODEV;
1864
1865 uap->port.mapbase = r_ports->start;
1866 uap->port.membase = (unsigned char __iomem *) r_ports->start;
1867 uap->port.iotype = UPIO_MEM;
1868 uap->port.irq = irq;
1869 uap->port.uartclk = ZS_CLOCK;
1870 uap->port.fifosize = 1;
1871 uap->port.ops = &pmz_pops;
1872 uap->port.type = PORT_PMAC_ZILOG;
1873 uap->port.flags = 0;
1874
1875 uap->control_reg = uap->port.membase;
1876 uap->data_reg = uap->control_reg + 4;
1877 uap->port_type = 0;
1878
1879 pmz_convert_to_zs(uap, CS8, 0, 9600);
1880
1881 return 0;
1882}
1883
1884static int __init pmz_probe(void)
1885{
1886 int err;
1887
1888 pmz_ports_count = 0;
1889
1890 pmz_ports[0].mate = &pmz_ports[1];
1891 pmz_ports[0].port.line = 0;
1892 pmz_ports[0].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1893 pmz_ports[0].node = &scc_a_pdev;
1894 err = pmz_init_port(&pmz_ports[0]);
1895 if (err)
1896 return err;
1897 pmz_ports_count++;
1898
1899 pmz_ports[1].mate = &pmz_ports[0];
1900 pmz_ports[1].port.line = 1;
1901 pmz_ports[1].flags = 0;
1902 pmz_ports[1].node = &scc_b_pdev;
1903 err = pmz_init_port(&pmz_ports[1]);
1904 if (err)
1905 return err;
1906 pmz_ports_count++;
1907
1908 return 0;
1909}
1910
1911static void pmz_dispose_port(struct uart_pmac_port *uap)
1912{
1913 memset(uap, 0, sizeof(struct uart_pmac_port));
1914}
1915
1916static int __init pmz_attach(struct platform_device *pdev)
1917{
1918 int i;
1919
1920 for (i = 0; i < pmz_ports_count; i++)
1921 if (pmz_ports[i].node == pdev)
1922 return 0;
1923 return -ENODEV;
1924}
1925
1926static int __exit pmz_detach(struct platform_device *pdev)
1927{
1928 return 0;
1929}
1930
1931#endif /* !CONFIG_PPC_PMAC */
1932
1933#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1934
1935static void pmz_console_write(struct console *con, const char *s, unsigned int count);
1936static int __init pmz_console_setup(struct console *co, char *options);
1937
1938static struct console pmz_console = {
1939 .name = PMACZILOG_NAME,
1940 .write = pmz_console_write,
1941 .device = uart_console_device,
1942 .setup = pmz_console_setup,
1943 .flags = CON_PRINTBUFFER,
1944 .index = -1,
1945 .data = &pmz_uart_reg,
1946};
1947
1948#define PMACZILOG_CONSOLE &pmz_console
1949#else /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1950#define PMACZILOG_CONSOLE (NULL)
1951#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1952
1953/*
1954 * Register the driver, console driver and ports with the serial
1955 * core
1956 */
1957static int __init pmz_register(void)
1958{
1959 int i, rc;
1960
1961 pmz_uart_reg.nr = pmz_ports_count;
1962 pmz_uart_reg.cons = PMACZILOG_CONSOLE;
1963
1964 /*
1965 * Register this driver with the serial core
1966 */
1967 rc = uart_register_driver(&pmz_uart_reg);
1968 if (rc)
1969 return rc;
1970
1971 /*
1972 * Register each port with the serial core
1973 */
1974 for (i = 0; i < pmz_ports_count; i++) {
1975 struct uart_pmac_port *uport = &pmz_ports[i];
1976 /* NULL node may happen on wallstreet */
1977 if (uport->node != NULL)
1978 rc = uart_add_one_port(&pmz_uart_reg, &uport->port);
1979 if (rc)
1980 goto err_out;
1981 }
1982
1983 return 0;
1984err_out:
1985 while (i-- > 0) {
1986 struct uart_pmac_port *uport = &pmz_ports[i];
1987 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1988 }
1989 uart_unregister_driver(&pmz_uart_reg);
1990 return rc;
1991}
1992
1993#ifdef CONFIG_PPC_PMAC
1994
1995static struct of_device_id pmz_match[] =
1996{
1997 {
1998 .name = "ch-a",
1999 },
2000 {
2001 .name = "ch-b",
2002 },
2003 {},
2004};
2005MODULE_DEVICE_TABLE (of, pmz_match);
2006
2007static struct macio_driver pmz_driver = {
2008 .driver = {
2009 .name = "pmac_zilog",
2010 .owner = THIS_MODULE,
2011 .of_match_table = pmz_match,
2012 },
2013 .probe = pmz_attach,
2014 .remove = pmz_detach,
2015 .suspend = pmz_suspend,
2016 .resume = pmz_resume,
2017};
2018
2019#else
2020
2021static struct platform_driver pmz_driver = {
2022 .remove = __exit_p(pmz_detach),
2023 .driver = {
2024 .name = "scc",
2025 .owner = THIS_MODULE,
2026 },
2027};
2028
2029#endif /* !CONFIG_PPC_PMAC */
2030
2031static int __init init_pmz(void)
2032{
2033 int rc, i;
2034 printk(KERN_INFO "%s\n", version);
2035
2036 /*
2037 * First, we need to do a direct OF-based probe pass. We
2038 * do that because we want serial console up before the
2039 * macio stuffs calls us back, and since that makes it
2040 * easier to pass the proper number of channels to
2041 * uart_register_driver()
2042 */
2043 if (pmz_ports_count == 0)
2044 pmz_probe();
2045
2046 /*
2047 * Bail early if no port found
2048 */
2049 if (pmz_ports_count == 0)
2050 return -ENODEV;
2051
2052 /*
2053 * Now we register with the serial layer
2054 */
2055 rc = pmz_register();
2056 if (rc) {
2057 printk(KERN_ERR
2058 "pmac_zilog: Error registering serial device, disabling pmac_zilog.\n"
2059 "pmac_zilog: Did another serial driver already claim the minors?\n");
2060 /* effectively "pmz_unprobe()" */
2061 for (i=0; i < pmz_ports_count; i++)
2062 pmz_dispose_port(&pmz_ports[i]);
2063 return rc;
2064 }
2065
2066 /*
2067 * Then we register the macio driver itself
2068 */
2069#ifdef CONFIG_PPC_PMAC
2070 return macio_register_driver(&pmz_driver);
2071#else
2072 return platform_driver_probe(&pmz_driver, pmz_attach);
2073#endif
2074}
2075
2076static void __exit exit_pmz(void)
2077{
2078 int i;
2079
2080#ifdef CONFIG_PPC_PMAC
2081 /* Get rid of macio-driver (detach from macio) */
2082 macio_unregister_driver(&pmz_driver);
2083#else
2084 platform_driver_unregister(&pmz_driver);
2085#endif
2086
2087 for (i = 0; i < pmz_ports_count; i++) {
2088 struct uart_pmac_port *uport = &pmz_ports[i];
2089 if (uport->node != NULL) {
2090 uart_remove_one_port(&pmz_uart_reg, &uport->port);
2091 pmz_dispose_port(uport);
2092 }
2093 }
2094 /* Unregister UART driver */
2095 uart_unregister_driver(&pmz_uart_reg);
2096}
2097
2098#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
2099
2100static void pmz_console_putchar(struct uart_port *port, int ch)
2101{
2102 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
2103
2104 /* Wait for the transmit buffer to empty. */
2105 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
2106 udelay(5);
2107 write_zsdata(uap, ch);
2108}
2109
2110/*
2111 * Print a string to the serial port trying not to disturb
2112 * any possible real use of the port...
2113 */
2114static void pmz_console_write(struct console *con, const char *s, unsigned int count)
2115{
2116 struct uart_pmac_port *uap = &pmz_ports[con->index];
2117 unsigned long flags;
2118
2119 if (ZS_IS_ASLEEP(uap))
2120 return;
2121 spin_lock_irqsave(&uap->port.lock, flags);
2122
2123 /* Turn of interrupts and enable the transmitter. */
2124 write_zsreg(uap, R1, uap->curregs[1] & ~TxINT_ENAB);
2125 write_zsreg(uap, R5, uap->curregs[5] | TxENABLE | RTS | DTR);
2126
2127 uart_console_write(&uap->port, s, count, pmz_console_putchar);
2128
2129 /* Restore the values in the registers. */
2130 write_zsreg(uap, R1, uap->curregs[1]);
2131 /* Don't disable the transmitter. */
2132
2133 spin_unlock_irqrestore(&uap->port.lock, flags);
2134}
2135
2136/*
2137 * Setup the serial console
2138 */
2139static int __init pmz_console_setup(struct console *co, char *options)
2140{
2141 struct uart_pmac_port *uap;
2142 struct uart_port *port;
2143 int baud = 38400;
2144 int bits = 8;
2145 int parity = 'n';
2146 int flow = 'n';
2147 unsigned long pwr_delay;
2148
2149 /*
2150 * XServe's default to 57600 bps
2151 */
2152 if (of_machine_is_compatible("RackMac1,1")
2153 || of_machine_is_compatible("RackMac1,2")
2154 || of_machine_is_compatible("MacRISC4"))
2155 baud = 57600;
2156
2157 /*
2158 * Check whether an invalid uart number has been specified, and
2159 * if so, search for the first available port that does have
2160 * console support.
2161 */
2162 if (co->index >= pmz_ports_count)
2163 co->index = 0;
2164 uap = &pmz_ports[co->index];
2165 if (uap->node == NULL)
2166 return -ENODEV;
2167 port = &uap->port;
2168
2169 /*
2170 * Mark port as beeing a console
2171 */
2172 uap->flags |= PMACZILOG_FLAG_IS_CONS;
2173
2174 /*
2175 * Temporary fix for uart layer who didn't setup the spinlock yet
2176 */
2177 spin_lock_init(&port->lock);
2178
2179 /*
2180 * Enable the hardware
2181 */
2182 pwr_delay = __pmz_startup(uap);
2183 if (pwr_delay)
2184 mdelay(pwr_delay);
2185
2186 if (options)
2187 uart_parse_options(options, &baud, &parity, &bits, &flow);
2188
2189 return uart_set_options(port, co, baud, parity, bits, flow);
2190}
2191
2192static int __init pmz_console_init(void)
2193{
2194 /* Probe ports */
2195 pmz_probe();
2196
2197 /* TODO: Autoprobe console based on OF */
2198 /* pmz_console.index = i; */
2199 register_console(&pmz_console);
2200
2201 return 0;
2202
2203}
2204console_initcall(pmz_console_init);
2205#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
2206
2207module_init(init_pmz);
2208module_exit(exit_pmz);
diff --git a/drivers/tty/serial/pmac_zilog.h b/drivers/tty/serial/pmac_zilog.h
new file mode 100644
index 000000000000..cbc34fbb1b20
--- /dev/null
+++ b/drivers/tty/serial/pmac_zilog.h
@@ -0,0 +1,396 @@
1#ifndef __PMAC_ZILOG_H__
2#define __PMAC_ZILOG_H__
3
4#ifdef CONFIG_PPC_PMAC
5#define pmz_debug(fmt, arg...) dev_dbg(&uap->dev->ofdev.dev, fmt, ## arg)
6#define pmz_error(fmt, arg...) dev_err(&uap->dev->ofdev.dev, fmt, ## arg)
7#define pmz_info(fmt, arg...) dev_info(&uap->dev->ofdev.dev, fmt, ## arg)
8#else
9#define pmz_debug(fmt, arg...) dev_dbg(&uap->node->dev, fmt, ## arg)
10#define pmz_error(fmt, arg...) dev_err(&uap->node->dev, fmt, ## arg)
11#define pmz_info(fmt, arg...) dev_info(&uap->node->dev, fmt, ## arg)
12#endif
13
14/*
15 * At most 2 ESCCs with 2 ports each
16 */
17#define MAX_ZS_PORTS 4
18
19/*
20 * We wrap our port structure around the generic uart_port.
21 */
22#define NUM_ZSREGS 17
23
24struct uart_pmac_port {
25 struct uart_port port;
26 struct uart_pmac_port *mate;
27
28#ifdef CONFIG_PPC_PMAC
29 /* macio_dev for the escc holding this port (maybe be null on
30 * early inited port)
31 */
32 struct macio_dev *dev;
33 /* device node to this port, this points to one of 2 childs
34 * of "escc" node (ie. ch-a or ch-b)
35 */
36 struct device_node *node;
37#else
38 struct platform_device *node;
39#endif
40
41 /* Port type as obtained from device tree (IRDA, modem, ...) */
42 int port_type;
43 u8 curregs[NUM_ZSREGS];
44
45 unsigned int flags;
46#define PMACZILOG_FLAG_IS_CONS 0x00000001
47#define PMACZILOG_FLAG_IS_KGDB 0x00000002
48#define PMACZILOG_FLAG_MODEM_STATUS 0x00000004
49#define PMACZILOG_FLAG_IS_CHANNEL_A 0x00000008
50#define PMACZILOG_FLAG_REGS_HELD 0x00000010
51#define PMACZILOG_FLAG_TX_STOPPED 0x00000020
52#define PMACZILOG_FLAG_TX_ACTIVE 0x00000040
53#define PMACZILOG_FLAG_ENABLED 0x00000080
54#define PMACZILOG_FLAG_IS_IRDA 0x00000100
55#define PMACZILOG_FLAG_IS_INTMODEM 0x00000200
56#define PMACZILOG_FLAG_HAS_DMA 0x00000400
57#define PMACZILOG_FLAG_RSRC_REQUESTED 0x00000800
58#define PMACZILOG_FLAG_IS_ASLEEP 0x00001000
59#define PMACZILOG_FLAG_IS_OPEN 0x00002000
60#define PMACZILOG_FLAG_IS_IRQ_ON 0x00004000
61#define PMACZILOG_FLAG_IS_EXTCLK 0x00008000
62#define PMACZILOG_FLAG_BREAK 0x00010000
63
64 unsigned char parity_mask;
65 unsigned char prev_status;
66
67 volatile u8 __iomem *control_reg;
68 volatile u8 __iomem *data_reg;
69
70#ifdef CONFIG_PPC_PMAC
71 unsigned int tx_dma_irq;
72 unsigned int rx_dma_irq;
73 volatile struct dbdma_regs __iomem *tx_dma_regs;
74 volatile struct dbdma_regs __iomem *rx_dma_regs;
75#endif
76
77 struct ktermios termios_cache;
78};
79
80#define to_pmz(p) ((struct uart_pmac_port *)(p))
81
82static inline struct uart_pmac_port *pmz_get_port_A(struct uart_pmac_port *uap)
83{
84 if (uap->flags & PMACZILOG_FLAG_IS_CHANNEL_A)
85 return uap;
86 return uap->mate;
87}
88
89/*
90 * Register accessors. Note that we don't need to enforce a recovery
91 * delay on PCI PowerMac hardware, it's dealt in HW by the MacIO chip,
92 * though if we try to use this driver on older machines, we might have
93 * to add it back
94 */
95static inline u8 read_zsreg(struct uart_pmac_port *port, u8 reg)
96{
97 if (reg != 0)
98 writeb(reg, port->control_reg);
99 return readb(port->control_reg);
100}
101
102static inline void write_zsreg(struct uart_pmac_port *port, u8 reg, u8 value)
103{
104 if (reg != 0)
105 writeb(reg, port->control_reg);
106 writeb(value, port->control_reg);
107}
108
109static inline u8 read_zsdata(struct uart_pmac_port *port)
110{
111 return readb(port->data_reg);
112}
113
114static inline void write_zsdata(struct uart_pmac_port *port, u8 data)
115{
116 writeb(data, port->data_reg);
117}
118
119static inline void zssync(struct uart_pmac_port *port)
120{
121 (void)readb(port->control_reg);
122}
123
124/* Conversion routines to/from brg time constants from/to bits
125 * per second.
126 */
127#define BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
128#define BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
129
130#define ZS_CLOCK 3686400 /* Z8530 RTxC input clock rate */
131
132/* The Zilog register set */
133
134#define FLAG 0x7e
135
136/* Write Register 0 */
137#define R0 0 /* Register selects */
138#define R1 1
139#define R2 2
140#define R3 3
141#define R4 4
142#define R5 5
143#define R6 6
144#define R7 7
145#define R8 8
146#define R9 9
147#define R10 10
148#define R11 11
149#define R12 12
150#define R13 13
151#define R14 14
152#define R15 15
153#define R7P 16
154
155#define NULLCODE 0 /* Null Code */
156#define POINT_HIGH 0x8 /* Select upper half of registers */
157#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
158#define SEND_ABORT 0x18 /* HDLC Abort */
159#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
160#define RES_Tx_P 0x28 /* Reset TxINT Pending */
161#define ERR_RES 0x30 /* Error Reset */
162#define RES_H_IUS 0x38 /* Reset highest IUS */
163
164#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
165#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
166#define RES_EOM_L 0xC0 /* Reset EOM latch */
167
168/* Write Register 1 */
169
170#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
171#define TxINT_ENAB 0x2 /* Tx Int Enable */
172#define PAR_SPEC 0x4 /* Parity is special condition */
173
174#define RxINT_DISAB 0 /* Rx Int Disable */
175#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
176#define INT_ALL_Rx 0x10 /* Int on all Rx Characters or error */
177#define INT_ERR_Rx 0x18 /* Int on error only */
178#define RxINT_MASK 0x18
179
180#define WT_RDY_RT 0x20 /* W/Req reflects recv if 1, xmit if 0 */
181#define WT_FN_RDYFN 0x40 /* W/Req pin is DMA request if 1, wait if 0 */
182#define WT_RDY_ENAB 0x80 /* Enable W/Req pin */
183
184/* Write Register #2 (Interrupt Vector) */
185
186/* Write Register 3 */
187
188#define RxENABLE 0x1 /* Rx Enable */
189#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
190#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
191#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
192#define ENT_HM 0x10 /* Enter Hunt Mode */
193#define AUTO_ENAB 0x20 /* Auto Enables */
194#define Rx5 0x0 /* Rx 5 Bits/Character */
195#define Rx7 0x40 /* Rx 7 Bits/Character */
196#define Rx6 0x80 /* Rx 6 Bits/Character */
197#define Rx8 0xc0 /* Rx 8 Bits/Character */
198#define RxN_MASK 0xc0
199
200/* Write Register 4 */
201
202#define PAR_ENAB 0x1 /* Parity Enable */
203#define PAR_EVEN 0x2 /* Parity Even/Odd* */
204
205#define SYNC_ENAB 0 /* Sync Modes Enable */
206#define SB1 0x4 /* 1 stop bit/char */
207#define SB15 0x8 /* 1.5 stop bits/char */
208#define SB2 0xc /* 2 stop bits/char */
209#define SB_MASK 0xc
210
211#define MONSYNC 0 /* 8 Bit Sync character */
212#define BISYNC 0x10 /* 16 bit sync character */
213#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
214#define EXTSYNC 0x30 /* External Sync Mode */
215
216#define X1CLK 0x0 /* x1 clock mode */
217#define X16CLK 0x40 /* x16 clock mode */
218#define X32CLK 0x80 /* x32 clock mode */
219#define X64CLK 0xC0 /* x64 clock mode */
220#define XCLK_MASK 0xC0
221
222/* Write Register 5 */
223
224#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
225#define RTS 0x2 /* RTS */
226#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
227#define TxENABLE 0x8 /* Tx Enable */
228#define SND_BRK 0x10 /* Send Break */
229#define Tx5 0x0 /* Tx 5 bits (or less)/character */
230#define Tx7 0x20 /* Tx 7 bits/character */
231#define Tx6 0x40 /* Tx 6 bits/character */
232#define Tx8 0x60 /* Tx 8 bits/character */
233#define TxN_MASK 0x60
234#define DTR 0x80 /* DTR */
235
236/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
237
238/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
239
240/* Write Register 7' (Some enhanced feature control) */
241#define ENEXREAD 0x40 /* Enable read of some write registers */
242
243/* Write Register 8 (transmit buffer) */
244
245/* Write Register 9 (Master interrupt control) */
246#define VIS 1 /* Vector Includes Status */
247#define NV 2 /* No Vector */
248#define DLC 4 /* Disable Lower Chain */
249#define MIE 8 /* Master Interrupt Enable */
250#define STATHI 0x10 /* Status high */
251#define NORESET 0 /* No reset on write to R9 */
252#define CHRB 0x40 /* Reset channel B */
253#define CHRA 0x80 /* Reset channel A */
254#define FHWRES 0xc0 /* Force hardware reset */
255
256/* Write Register 10 (misc control bits) */
257#define BIT6 1 /* 6 bit/8bit sync */
258#define LOOPMODE 2 /* SDLC Loop mode */
259#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
260#define MARKIDLE 8 /* Mark/flag on idle */
261#define GAOP 0x10 /* Go active on poll */
262#define NRZ 0 /* NRZ mode */
263#define NRZI 0x20 /* NRZI mode */
264#define FM1 0x40 /* FM1 (transition = 1) */
265#define FM0 0x60 /* FM0 (transition = 0) */
266#define CRCPS 0x80 /* CRC Preset I/O */
267
268/* Write Register 11 (Clock Mode control) */
269#define TRxCXT 0 /* TRxC = Xtal output */
270#define TRxCTC 1 /* TRxC = Transmit clock */
271#define TRxCBR 2 /* TRxC = BR Generator Output */
272#define TRxCDP 3 /* TRxC = DPLL output */
273#define TRxCOI 4 /* TRxC O/I */
274#define TCRTxCP 0 /* Transmit clock = RTxC pin */
275#define TCTRxCP 8 /* Transmit clock = TRxC pin */
276#define TCBR 0x10 /* Transmit clock = BR Generator output */
277#define TCDPLL 0x18 /* Transmit clock = DPLL output */
278#define RCRTxCP 0 /* Receive clock = RTxC pin */
279#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
280#define RCBR 0x40 /* Receive clock = BR Generator output */
281#define RCDPLL 0x60 /* Receive clock = DPLL output */
282#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
283
284/* Write Register 12 (lower byte of baud rate generator time constant) */
285
286/* Write Register 13 (upper byte of baud rate generator time constant) */
287
288/* Write Register 14 (Misc control bits) */
289#define BRENAB 1 /* Baud rate generator enable */
290#define BRSRC 2 /* Baud rate generator source */
291#define DTRREQ 4 /* DTR/Request function */
292#define AUTOECHO 8 /* Auto Echo */
293#define LOOPBAK 0x10 /* Local loopback */
294#define SEARCH 0x20 /* Enter search mode */
295#define RMC 0x40 /* Reset missing clock */
296#define DISDPLL 0x60 /* Disable DPLL */
297#define SSBR 0x80 /* Set DPLL source = BR generator */
298#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
299#define SFMM 0xc0 /* Set FM mode */
300#define SNRZI 0xe0 /* Set NRZI mode */
301
302/* Write Register 15 (external/status interrupt control) */
303#define EN85C30 1 /* Enable some 85c30-enhanced registers */
304#define ZCIE 2 /* Zero count IE */
305#define ENSTFIFO 4 /* Enable status FIFO (SDLC) */
306#define DCDIE 8 /* DCD IE */
307#define SYNCIE 0x10 /* Sync/hunt IE */
308#define CTSIE 0x20 /* CTS IE */
309#define TxUIE 0x40 /* Tx Underrun/EOM IE */
310#define BRKIE 0x80 /* Break/Abort IE */
311
312
313/* Read Register 0 */
314#define Rx_CH_AV 0x1 /* Rx Character Available */
315#define ZCOUNT 0x2 /* Zero count */
316#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
317#define DCD 0x8 /* DCD */
318#define SYNC_HUNT 0x10 /* Sync/hunt */
319#define CTS 0x20 /* CTS */
320#define TxEOM 0x40 /* Tx underrun */
321#define BRK_ABRT 0x80 /* Break/Abort */
322
323/* Read Register 1 */
324#define ALL_SNT 0x1 /* All sent */
325/* Residue Data for 8 Rx bits/char programmed */
326#define RES3 0x8 /* 0/3 */
327#define RES4 0x4 /* 0/4 */
328#define RES5 0xc /* 0/5 */
329#define RES6 0x2 /* 0/6 */
330#define RES7 0xa /* 0/7 */
331#define RES8 0x6 /* 0/8 */
332#define RES18 0xe /* 1/8 */
333#define RES28 0x0 /* 2/8 */
334/* Special Rx Condition Interrupts */
335#define PAR_ERR 0x10 /* Parity error */
336#define Rx_OVR 0x20 /* Rx Overrun Error */
337#define CRC_ERR 0x40 /* CRC/Framing Error */
338#define END_FR 0x80 /* End of Frame (SDLC) */
339
340/* Read Register 2 (channel b only) - Interrupt vector */
341#define CHB_Tx_EMPTY 0x00
342#define CHB_EXT_STAT 0x02
343#define CHB_Rx_AVAIL 0x04
344#define CHB_SPECIAL 0x06
345#define CHA_Tx_EMPTY 0x08
346#define CHA_EXT_STAT 0x0a
347#define CHA_Rx_AVAIL 0x0c
348#define CHA_SPECIAL 0x0e
349#define STATUS_MASK 0x06
350
351/* Read Register 3 (interrupt pending register) ch a only */
352#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
353#define CHBTxIP 0x2 /* Channel B Tx IP */
354#define CHBRxIP 0x4 /* Channel B Rx IP */
355#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
356#define CHATxIP 0x10 /* Channel A Tx IP */
357#define CHARxIP 0x20 /* Channel A Rx IP */
358
359/* Read Register 8 (receive data register) */
360
361/* Read Register 10 (misc status bits) */
362#define ONLOOP 2 /* On loop */
363#define LOOPSEND 0x10 /* Loop sending */
364#define CLK2MIS 0x40 /* Two clocks missing */
365#define CLK1MIS 0x80 /* One clock missing */
366
367/* Read Register 12 (lower byte of baud rate generator constant) */
368
369/* Read Register 13 (upper byte of baud rate generator constant) */
370
371/* Read Register 15 (value of WR 15) */
372
373/* Misc macros */
374#define ZS_CLEARERR(port) (write_zsreg(port, 0, ERR_RES))
375#define ZS_CLEARFIFO(port) do { volatile unsigned char garbage; \
376 garbage = read_zsdata(port); \
377 garbage = read_zsdata(port); \
378 garbage = read_zsdata(port); \
379 } while(0)
380
381#define ZS_IS_CONS(UP) ((UP)->flags & PMACZILOG_FLAG_IS_CONS)
382#define ZS_IS_KGDB(UP) ((UP)->flags & PMACZILOG_FLAG_IS_KGDB)
383#define ZS_IS_CHANNEL_A(UP) ((UP)->flags & PMACZILOG_FLAG_IS_CHANNEL_A)
384#define ZS_REGS_HELD(UP) ((UP)->flags & PMACZILOG_FLAG_REGS_HELD)
385#define ZS_TX_STOPPED(UP) ((UP)->flags & PMACZILOG_FLAG_TX_STOPPED)
386#define ZS_TX_ACTIVE(UP) ((UP)->flags & PMACZILOG_FLAG_TX_ACTIVE)
387#define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & PMACZILOG_FLAG_MODEM_STATUS)
388#define ZS_IS_IRDA(UP) ((UP)->flags & PMACZILOG_FLAG_IS_IRDA)
389#define ZS_IS_INTMODEM(UP) ((UP)->flags & PMACZILOG_FLAG_IS_INTMODEM)
390#define ZS_HAS_DMA(UP) ((UP)->flags & PMACZILOG_FLAG_HAS_DMA)
391#define ZS_IS_ASLEEP(UP) ((UP)->flags & PMACZILOG_FLAG_IS_ASLEEP)
392#define ZS_IS_OPEN(UP) ((UP)->flags & PMACZILOG_FLAG_IS_OPEN)
393#define ZS_IS_IRQ_ON(UP) ((UP)->flags & PMACZILOG_FLAG_IS_IRQ_ON)
394#define ZS_IS_EXTCLK(UP) ((UP)->flags & PMACZILOG_FLAG_IS_EXTCLK)
395
396#endif /* __PMAC_ZILOG_H__ */
diff --git a/drivers/tty/serial/pnx8xxx_uart.c b/drivers/tty/serial/pnx8xxx_uart.c
new file mode 100644
index 000000000000..0aa75a97531c
--- /dev/null
+++ b/drivers/tty/serial/pnx8xxx_uart.c
@@ -0,0 +1,854 @@
1/*
2 * UART driver for PNX8XXX SoCs
3 *
4 * Author: Per Hallsmark per.hallsmark@mvista.com
5 * Ported to 2.6 kernel by EmbeddedAlley
6 * Reworked by Vitaly Wool <vitalywool@gmail.com>
7 *
8 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
10 *
11 * This file is licensed under the terms of the GNU General Public License
12 * version 2. This program is licensed "as is" without any warranty of
13 * any kind, whether express or implied.
14 *
15 */
16
17#if defined(CONFIG_SERIAL_PNX8XXX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18#define SUPPORT_SYSRQ
19#endif
20
21#include <linux/module.h>
22#include <linux/ioport.h>
23#include <linux/init.h>
24#include <linux/console.h>
25#include <linux/sysrq.h>
26#include <linux/device.h>
27#include <linux/platform_device.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial_core.h>
31#include <linux/serial.h>
32#include <linux/serial_pnx8xxx.h>
33
34#include <asm/io.h>
35#include <asm/irq.h>
36
37/* We'll be using StrongARM sa1100 serial port major/minor */
38#define SERIAL_PNX8XXX_MAJOR 204
39#define MINOR_START 5
40
41#define NR_PORTS 2
42
43#define PNX8XXX_ISR_PASS_LIMIT 256
44
45/*
46 * Convert from ignore_status_mask or read_status_mask to FIFO
47 * and interrupt status bits
48 */
49#define SM_TO_FIFO(x) ((x) >> 10)
50#define SM_TO_ISTAT(x) ((x) & 0x000001ff)
51#define FIFO_TO_SM(x) ((x) << 10)
52#define ISTAT_TO_SM(x) ((x) & 0x000001ff)
53
54/*
55 * This is the size of our serial port register set.
56 */
57#define UART_PORT_SIZE 0x1000
58
59/*
60 * This determines how often we check the modem status signals
61 * for any change. They generally aren't connected to an IRQ
62 * so we have to poll them. We also check immediately before
63 * filling the TX fifo incase CTS has been dropped.
64 */
65#define MCTRL_TIMEOUT (250*HZ/1000)
66
67extern struct pnx8xxx_port pnx8xxx_ports[];
68
69static inline int serial_in(struct pnx8xxx_port *sport, int offset)
70{
71 return (__raw_readl(sport->port.membase + offset));
72}
73
74static inline void serial_out(struct pnx8xxx_port *sport, int offset, int value)
75{
76 __raw_writel(value, sport->port.membase + offset);
77}
78
79/*
80 * Handle any change of modem status signal since we were last called.
81 */
82static void pnx8xxx_mctrl_check(struct pnx8xxx_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.state->port.delta_msr_wait);
104}
105
106/*
107 * This is our per-port timeout handler, for checking the
108 * modem status signals.
109 */
110static void pnx8xxx_timeout(unsigned long data)
111{
112 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)data;
113 unsigned long flags;
114
115 if (sport->port.state) {
116 spin_lock_irqsave(&sport->port.lock, flags);
117 pnx8xxx_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 pnx8xxx_stop_tx(struct uart_port *port)
128{
129 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
130 u32 ien;
131
132 /* Disable TX intr */
133 ien = serial_in(sport, PNX8XXX_IEN);
134 serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLTX);
135
136 /* Clear all pending TX intr */
137 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
138}
139
140/*
141 * interrupts may not be disabled on entry
142 */
143static void pnx8xxx_start_tx(struct uart_port *port)
144{
145 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
146 u32 ien;
147
148 /* Clear all pending TX intr */
149 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
150
151 /* Enable TX intr */
152 ien = serial_in(sport, PNX8XXX_IEN);
153 serial_out(sport, PNX8XXX_IEN, ien | PNX8XXX_UART_INT_ALLTX);
154}
155
156/*
157 * Interrupts enabled
158 */
159static void pnx8xxx_stop_rx(struct uart_port *port)
160{
161 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
162 u32 ien;
163
164 /* Disable RX intr */
165 ien = serial_in(sport, PNX8XXX_IEN);
166 serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLRX);
167
168 /* Clear all pending RX intr */
169 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX);
170}
171
172/*
173 * Set the modem control timer to fire immediately.
174 */
175static void pnx8xxx_enable_ms(struct uart_port *port)
176{
177 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
178
179 mod_timer(&sport->timer, jiffies);
180}
181
182static void pnx8xxx_rx_chars(struct pnx8xxx_port *sport)
183{
184 struct tty_struct *tty = sport->port.state->port.tty;
185 unsigned int status, ch, flg;
186
187 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
188 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
189 while (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFIFO)) {
190 ch = serial_in(sport, PNX8XXX_FIFO) & 0xff;
191
192 sport->port.icount.rx++;
193
194 flg = TTY_NORMAL;
195
196 /*
197 * note that the error handling code is
198 * out of the main execution path
199 */
200 if (status & (FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE |
201 PNX8XXX_UART_FIFO_RXPAR |
202 PNX8XXX_UART_FIFO_RXBRK) |
203 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))) {
204 if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXBRK)) {
205 status &= ~(FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
206 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR));
207 sport->port.icount.brk++;
208 if (uart_handle_break(&sport->port))
209 goto ignore_char;
210 } else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
211 sport->port.icount.parity++;
212 else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
213 sport->port.icount.frame++;
214 if (status & ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))
215 sport->port.icount.overrun++;
216
217 status &= sport->port.read_status_mask;
218
219 if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
220 flg = TTY_PARITY;
221 else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
222 flg = TTY_FRAME;
223
224#ifdef SUPPORT_SYSRQ
225 sport->port.sysrq = 0;
226#endif
227 }
228
229 if (uart_handle_sysrq_char(&sport->port, ch))
230 goto ignore_char;
231
232 uart_insert_char(&sport->port, status,
233 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN), ch, flg);
234
235 ignore_char:
236 serial_out(sport, PNX8XXX_LCR, serial_in(sport, PNX8XXX_LCR) |
237 PNX8XXX_UART_LCR_RX_NEXT);
238 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
239 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
240 }
241 tty_flip_buffer_push(tty);
242}
243
244static void pnx8xxx_tx_chars(struct pnx8xxx_port *sport)
245{
246 struct circ_buf *xmit = &sport->port.state->xmit;
247
248 if (sport->port.x_char) {
249 serial_out(sport, PNX8XXX_FIFO, sport->port.x_char);
250 sport->port.icount.tx++;
251 sport->port.x_char = 0;
252 return;
253 }
254
255 /*
256 * Check the modem control lines before
257 * transmitting anything.
258 */
259 pnx8xxx_mctrl_check(sport);
260
261 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
262 pnx8xxx_stop_tx(&sport->port);
263 return;
264 }
265
266 /*
267 * TX while bytes available
268 */
269 while (((serial_in(sport, PNX8XXX_FIFO) &
270 PNX8XXX_UART_FIFO_TXFIFO) >> 16) < 16) {
271 serial_out(sport, PNX8XXX_FIFO, xmit->buf[xmit->tail]);
272 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
273 sport->port.icount.tx++;
274 if (uart_circ_empty(xmit))
275 break;
276 }
277
278 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
279 uart_write_wakeup(&sport->port);
280
281 if (uart_circ_empty(xmit))
282 pnx8xxx_stop_tx(&sport->port);
283}
284
285static irqreturn_t pnx8xxx_int(int irq, void *dev_id)
286{
287 struct pnx8xxx_port *sport = dev_id;
288 unsigned int status;
289
290 spin_lock(&sport->port.lock);
291 /* Get the interrupts */
292 status = serial_in(sport, PNX8XXX_ISTAT) & serial_in(sport, PNX8XXX_IEN);
293
294 /* Byte or break signal received */
295 if (status & (PNX8XXX_UART_INT_RX | PNX8XXX_UART_INT_BREAK))
296 pnx8xxx_rx_chars(sport);
297
298 /* TX holding register empty - transmit a byte */
299 if (status & PNX8XXX_UART_INT_TX)
300 pnx8xxx_tx_chars(sport);
301
302 /* Clear the ISTAT register */
303 serial_out(sport, PNX8XXX_ICLR, status);
304
305 spin_unlock(&sport->port.lock);
306 return IRQ_HANDLED;
307}
308
309/*
310 * Return TIOCSER_TEMT when transmitter is not busy.
311 */
312static unsigned int pnx8xxx_tx_empty(struct uart_port *port)
313{
314 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
315
316 return serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA ? 0 : TIOCSER_TEMT;
317}
318
319static unsigned int pnx8xxx_get_mctrl(struct uart_port *port)
320{
321 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
322 unsigned int mctrl = TIOCM_DSR;
323 unsigned int msr;
324
325 /* REVISIT */
326
327 msr = serial_in(sport, PNX8XXX_MCR);
328
329 mctrl |= msr & PNX8XXX_UART_MCR_CTS ? TIOCM_CTS : 0;
330 mctrl |= msr & PNX8XXX_UART_MCR_DCD ? TIOCM_CAR : 0;
331
332 return mctrl;
333}
334
335static void pnx8xxx_set_mctrl(struct uart_port *port, unsigned int mctrl)
336{
337#if 0 /* FIXME */
338 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
339 unsigned int msr;
340#endif
341}
342
343/*
344 * Interrupts always disabled.
345 */
346static void pnx8xxx_break_ctl(struct uart_port *port, int break_state)
347{
348 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
349 unsigned long flags;
350 unsigned int lcr;
351
352 spin_lock_irqsave(&sport->port.lock, flags);
353 lcr = serial_in(sport, PNX8XXX_LCR);
354 if (break_state == -1)
355 lcr |= PNX8XXX_UART_LCR_TXBREAK;
356 else
357 lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
358 serial_out(sport, PNX8XXX_LCR, lcr);
359 spin_unlock_irqrestore(&sport->port.lock, flags);
360}
361
362static int pnx8xxx_startup(struct uart_port *port)
363{
364 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
365 int retval;
366
367 /*
368 * Allocate the IRQ
369 */
370 retval = request_irq(sport->port.irq, pnx8xxx_int, 0,
371 "pnx8xxx-uart", sport);
372 if (retval)
373 return retval;
374
375 /*
376 * Finally, clear and enable interrupts
377 */
378
379 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
380 PNX8XXX_UART_INT_ALLTX);
381
382 serial_out(sport, PNX8XXX_IEN, serial_in(sport, PNX8XXX_IEN) |
383 PNX8XXX_UART_INT_ALLRX |
384 PNX8XXX_UART_INT_ALLTX);
385
386 /*
387 * Enable modem status interrupts
388 */
389 spin_lock_irq(&sport->port.lock);
390 pnx8xxx_enable_ms(&sport->port);
391 spin_unlock_irq(&sport->port.lock);
392
393 return 0;
394}
395
396static void pnx8xxx_shutdown(struct uart_port *port)
397{
398 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
399 int lcr;
400
401 /*
402 * Stop our timer.
403 */
404 del_timer_sync(&sport->timer);
405
406 /*
407 * Disable all interrupts
408 */
409 serial_out(sport, PNX8XXX_IEN, 0);
410
411 /*
412 * Reset the Tx and Rx FIFOS, disable the break condition
413 */
414 lcr = serial_in(sport, PNX8XXX_LCR);
415 lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
416 lcr |= PNX8XXX_UART_LCR_TX_RST | PNX8XXX_UART_LCR_RX_RST;
417 serial_out(sport, PNX8XXX_LCR, lcr);
418
419 /*
420 * Clear all interrupts
421 */
422 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
423 PNX8XXX_UART_INT_ALLTX);
424
425 /*
426 * Free the interrupt
427 */
428 free_irq(sport->port.irq, sport);
429}
430
431static void
432pnx8xxx_set_termios(struct uart_port *port, struct ktermios *termios,
433 struct ktermios *old)
434{
435 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
436 unsigned long flags;
437 unsigned int lcr_fcr, old_ien, baud, quot;
438 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
439
440 /*
441 * We only support CS7 and CS8.
442 */
443 while ((termios->c_cflag & CSIZE) != CS7 &&
444 (termios->c_cflag & CSIZE) != CS8) {
445 termios->c_cflag &= ~CSIZE;
446 termios->c_cflag |= old_csize;
447 old_csize = CS8;
448 }
449
450 if ((termios->c_cflag & CSIZE) == CS8)
451 lcr_fcr = PNX8XXX_UART_LCR_8BIT;
452 else
453 lcr_fcr = 0;
454
455 if (termios->c_cflag & CSTOPB)
456 lcr_fcr |= PNX8XXX_UART_LCR_2STOPB;
457 if (termios->c_cflag & PARENB) {
458 lcr_fcr |= PNX8XXX_UART_LCR_PAREN;
459 if (!(termios->c_cflag & PARODD))
460 lcr_fcr |= PNX8XXX_UART_LCR_PAREVN;
461 }
462
463 /*
464 * Ask the core to calculate the divisor for us.
465 */
466 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
467 quot = uart_get_divisor(port, baud);
468
469 spin_lock_irqsave(&sport->port.lock, flags);
470
471 sport->port.read_status_mask = ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN) |
472 ISTAT_TO_SM(PNX8XXX_UART_INT_EMPTY) |
473 ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
474 if (termios->c_iflag & INPCK)
475 sport->port.read_status_mask |=
476 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
477 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
478 if (termios->c_iflag & (BRKINT | PARMRK))
479 sport->port.read_status_mask |=
480 ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
481
482 /*
483 * Characters to ignore
484 */
485 sport->port.ignore_status_mask = 0;
486 if (termios->c_iflag & IGNPAR)
487 sport->port.ignore_status_mask |=
488 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
489 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
490 if (termios->c_iflag & IGNBRK) {
491 sport->port.ignore_status_mask |=
492 ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
493 /*
494 * If we're ignoring parity and break indicators,
495 * ignore overruns too (for real raw support).
496 */
497 if (termios->c_iflag & IGNPAR)
498 sport->port.ignore_status_mask |=
499 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN);
500 }
501
502 /*
503 * ignore all characters if CREAD is not set
504 */
505 if ((termios->c_cflag & CREAD) == 0)
506 sport->port.ignore_status_mask |=
507 ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
508
509 del_timer_sync(&sport->timer);
510
511 /*
512 * Update the per-port timeout.
513 */
514 uart_update_timeout(port, termios->c_cflag, baud);
515
516 /*
517 * disable interrupts and drain transmitter
518 */
519 old_ien = serial_in(sport, PNX8XXX_IEN);
520 serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
521 PNX8XXX_UART_INT_ALLRX));
522
523 while (serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA)
524 barrier();
525
526 /* then, disable everything */
527 serial_out(sport, PNX8XXX_IEN, 0);
528
529 /* Reset the Rx and Tx FIFOs too */
530 lcr_fcr |= PNX8XXX_UART_LCR_TX_RST;
531 lcr_fcr |= PNX8XXX_UART_LCR_RX_RST;
532
533 /* set the parity, stop bits and data size */
534 serial_out(sport, PNX8XXX_LCR, lcr_fcr);
535
536 /* set the baud rate */
537 quot -= 1;
538 serial_out(sport, PNX8XXX_BAUD, quot);
539
540 serial_out(sport, PNX8XXX_ICLR, -1);
541
542 serial_out(sport, PNX8XXX_IEN, old_ien);
543
544 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
545 pnx8xxx_enable_ms(&sport->port);
546
547 spin_unlock_irqrestore(&sport->port.lock, flags);
548}
549
550static const char *pnx8xxx_type(struct uart_port *port)
551{
552 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
553
554 return sport->port.type == PORT_PNX8XXX ? "PNX8XXX" : NULL;
555}
556
557/*
558 * Release the memory region(s) being used by 'port'.
559 */
560static void pnx8xxx_release_port(struct uart_port *port)
561{
562 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
563
564 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
565}
566
567/*
568 * Request the memory region(s) being used by 'port'.
569 */
570static int pnx8xxx_request_port(struct uart_port *port)
571{
572 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
573 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
574 "pnx8xxx-uart") != NULL ? 0 : -EBUSY;
575}
576
577/*
578 * Configure/autoconfigure the port.
579 */
580static void pnx8xxx_config_port(struct uart_port *port, int flags)
581{
582 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
583
584 if (flags & UART_CONFIG_TYPE &&
585 pnx8xxx_request_port(&sport->port) == 0)
586 sport->port.type = PORT_PNX8XXX;
587}
588
589/*
590 * Verify the new serial_struct (for TIOCSSERIAL).
591 * The only change we allow are to the flags and type, and
592 * even then only between PORT_PNX8XXX and PORT_UNKNOWN
593 */
594static int
595pnx8xxx_verify_port(struct uart_port *port, struct serial_struct *ser)
596{
597 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
598 int ret = 0;
599
600 if (ser->type != PORT_UNKNOWN && ser->type != PORT_PNX8XXX)
601 ret = -EINVAL;
602 if (sport->port.irq != ser->irq)
603 ret = -EINVAL;
604 if (ser->io_type != SERIAL_IO_MEM)
605 ret = -EINVAL;
606 if (sport->port.uartclk / 16 != ser->baud_base)
607 ret = -EINVAL;
608 if ((void *)sport->port.mapbase != ser->iomem_base)
609 ret = -EINVAL;
610 if (sport->port.iobase != ser->port)
611 ret = -EINVAL;
612 if (ser->hub6 != 0)
613 ret = -EINVAL;
614 return ret;
615}
616
617static struct uart_ops pnx8xxx_pops = {
618 .tx_empty = pnx8xxx_tx_empty,
619 .set_mctrl = pnx8xxx_set_mctrl,
620 .get_mctrl = pnx8xxx_get_mctrl,
621 .stop_tx = pnx8xxx_stop_tx,
622 .start_tx = pnx8xxx_start_tx,
623 .stop_rx = pnx8xxx_stop_rx,
624 .enable_ms = pnx8xxx_enable_ms,
625 .break_ctl = pnx8xxx_break_ctl,
626 .startup = pnx8xxx_startup,
627 .shutdown = pnx8xxx_shutdown,
628 .set_termios = pnx8xxx_set_termios,
629 .type = pnx8xxx_type,
630 .release_port = pnx8xxx_release_port,
631 .request_port = pnx8xxx_request_port,
632 .config_port = pnx8xxx_config_port,
633 .verify_port = pnx8xxx_verify_port,
634};
635
636
637/*
638 * Setup the PNX8XXX serial ports.
639 *
640 * Note also that we support "console=ttySx" where "x" is either 0 or 1.
641 */
642static void __init pnx8xxx_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 init_timer(&pnx8xxx_ports[i].timer);
653 pnx8xxx_ports[i].timer.function = pnx8xxx_timeout;
654 pnx8xxx_ports[i].timer.data = (unsigned long)&pnx8xxx_ports[i];
655 pnx8xxx_ports[i].port.ops = &pnx8xxx_pops;
656 }
657}
658
659#ifdef CONFIG_SERIAL_PNX8XXX_CONSOLE
660
661static void pnx8xxx_console_putchar(struct uart_port *port, int ch)
662{
663 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
664 int status;
665
666 do {
667 /* Wait for UART_TX register to empty */
668 status = serial_in(sport, PNX8XXX_FIFO);
669 } while (status & PNX8XXX_UART_FIFO_TXFIFO);
670 serial_out(sport, PNX8XXX_FIFO, ch);
671}
672
673/*
674 * Interrupts are disabled on entering
675 */static void
676pnx8xxx_console_write(struct console *co, const char *s, unsigned int count)
677{
678 struct pnx8xxx_port *sport = &pnx8xxx_ports[co->index];
679 unsigned int old_ien, status;
680
681 /*
682 * First, save IEN and then disable interrupts
683 */
684 old_ien = serial_in(sport, PNX8XXX_IEN);
685 serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
686 PNX8XXX_UART_INT_ALLRX));
687
688 uart_console_write(&sport->port, s, count, pnx8xxx_console_putchar);
689
690 /*
691 * Finally, wait for transmitter to become empty
692 * and restore IEN
693 */
694 do {
695 /* Wait for UART_TX register to empty */
696 status = serial_in(sport, PNX8XXX_FIFO);
697 } while (status & PNX8XXX_UART_FIFO_TXFIFO);
698
699 /* Clear TX and EMPTY interrupt */
700 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_TX |
701 PNX8XXX_UART_INT_EMPTY);
702
703 serial_out(sport, PNX8XXX_IEN, old_ien);
704}
705
706static int __init
707pnx8xxx_console_setup(struct console *co, char *options)
708{
709 struct pnx8xxx_port *sport;
710 int baud = 38400;
711 int bits = 8;
712 int parity = 'n';
713 int flow = 'n';
714
715 /*
716 * Check whether an invalid uart number has been specified, and
717 * if so, search for the first available port that does have
718 * console support.
719 */
720 if (co->index == -1 || co->index >= NR_PORTS)
721 co->index = 0;
722 sport = &pnx8xxx_ports[co->index];
723
724 if (options)
725 uart_parse_options(options, &baud, &parity, &bits, &flow);
726
727 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
728}
729
730static struct uart_driver pnx8xxx_reg;
731static struct console pnx8xxx_console = {
732 .name = "ttyS",
733 .write = pnx8xxx_console_write,
734 .device = uart_console_device,
735 .setup = pnx8xxx_console_setup,
736 .flags = CON_PRINTBUFFER,
737 .index = -1,
738 .data = &pnx8xxx_reg,
739};
740
741static int __init pnx8xxx_rs_console_init(void)
742{
743 pnx8xxx_init_ports();
744 register_console(&pnx8xxx_console);
745 return 0;
746}
747console_initcall(pnx8xxx_rs_console_init);
748
749#define PNX8XXX_CONSOLE &pnx8xxx_console
750#else
751#define PNX8XXX_CONSOLE NULL
752#endif
753
754static struct uart_driver pnx8xxx_reg = {
755 .owner = THIS_MODULE,
756 .driver_name = "ttyS",
757 .dev_name = "ttyS",
758 .major = SERIAL_PNX8XXX_MAJOR,
759 .minor = MINOR_START,
760 .nr = NR_PORTS,
761 .cons = PNX8XXX_CONSOLE,
762};
763
764static int pnx8xxx_serial_suspend(struct platform_device *pdev, pm_message_t state)
765{
766 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
767
768 return uart_suspend_port(&pnx8xxx_reg, &sport->port);
769}
770
771static int pnx8xxx_serial_resume(struct platform_device *pdev)
772{
773 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
774
775 return uart_resume_port(&pnx8xxx_reg, &sport->port);
776}
777
778static int pnx8xxx_serial_probe(struct platform_device *pdev)
779{
780 struct resource *res = pdev->resource;
781 int i;
782
783 for (i = 0; i < pdev->num_resources; i++, res++) {
784 if (!(res->flags & IORESOURCE_MEM))
785 continue;
786
787 for (i = 0; i < NR_PORTS; i++) {
788 if (pnx8xxx_ports[i].port.mapbase != res->start)
789 continue;
790
791 pnx8xxx_ports[i].port.dev = &pdev->dev;
792 uart_add_one_port(&pnx8xxx_reg, &pnx8xxx_ports[i].port);
793 platform_set_drvdata(pdev, &pnx8xxx_ports[i]);
794 break;
795 }
796 }
797
798 return 0;
799}
800
801static int pnx8xxx_serial_remove(struct platform_device *pdev)
802{
803 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
804
805 platform_set_drvdata(pdev, NULL);
806
807 if (sport)
808 uart_remove_one_port(&pnx8xxx_reg, &sport->port);
809
810 return 0;
811}
812
813static struct platform_driver pnx8xxx_serial_driver = {
814 .driver = {
815 .name = "pnx8xxx-uart",
816 .owner = THIS_MODULE,
817 },
818 .probe = pnx8xxx_serial_probe,
819 .remove = pnx8xxx_serial_remove,
820 .suspend = pnx8xxx_serial_suspend,
821 .resume = pnx8xxx_serial_resume,
822};
823
824static int __init pnx8xxx_serial_init(void)
825{
826 int ret;
827
828 printk(KERN_INFO "Serial: PNX8XXX driver\n");
829
830 pnx8xxx_init_ports();
831
832 ret = uart_register_driver(&pnx8xxx_reg);
833 if (ret == 0) {
834 ret = platform_driver_register(&pnx8xxx_serial_driver);
835 if (ret)
836 uart_unregister_driver(&pnx8xxx_reg);
837 }
838 return ret;
839}
840
841static void __exit pnx8xxx_serial_exit(void)
842{
843 platform_driver_unregister(&pnx8xxx_serial_driver);
844 uart_unregister_driver(&pnx8xxx_reg);
845}
846
847module_init(pnx8xxx_serial_init);
848module_exit(pnx8xxx_serial_exit);
849
850MODULE_AUTHOR("Embedded Alley Solutions, Inc.");
851MODULE_DESCRIPTION("PNX8XXX SoCs serial port driver");
852MODULE_LICENSE("GPL");
853MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_PNX8XXX_MAJOR);
854MODULE_ALIAS("platform:pnx8xxx-uart");
diff --git a/drivers/tty/serial/pxa.c b/drivers/tty/serial/pxa.c
new file mode 100644
index 000000000000..1102a39b44f5
--- /dev/null
+++ b/drivers/tty/serial/pxa.c
@@ -0,0 +1,879 @@
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
28#if defined(CONFIG_SERIAL_PXA_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
29#define SUPPORT_SYSRQ
30#endif
31
32#include <linux/module.h>
33#include <linux/ioport.h>
34#include <linux/init.h>
35#include <linux/console.h>
36#include <linux/sysrq.h>
37#include <linux/serial_reg.h>
38#include <linux/circ_buf.h>
39#include <linux/delay.h>
40#include <linux/interrupt.h>
41#include <linux/platform_device.h>
42#include <linux/tty.h>
43#include <linux/tty_flip.h>
44#include <linux/serial_core.h>
45#include <linux/clk.h>
46#include <linux/io.h>
47#include <linux/slab.h>
48
49struct uart_pxa_port {
50 struct uart_port port;
51 unsigned char ier;
52 unsigned char lcr;
53 unsigned char mcr;
54 unsigned int lsr_break_flag;
55 struct clk *clk;
56 char *name;
57};
58
59static inline unsigned int serial_in(struct uart_pxa_port *up, int offset)
60{
61 offset <<= 2;
62 return readl(up->port.membase + offset);
63}
64
65static inline void serial_out(struct uart_pxa_port *up, int offset, int value)
66{
67 offset <<= 2;
68 writel(value, up->port.membase + offset);
69}
70
71static void serial_pxa_enable_ms(struct uart_port *port)
72{
73 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
74
75 up->ier |= UART_IER_MSI;
76 serial_out(up, UART_IER, up->ier);
77}
78
79static void serial_pxa_stop_tx(struct uart_port *port)
80{
81 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
82
83 if (up->ier & UART_IER_THRI) {
84 up->ier &= ~UART_IER_THRI;
85 serial_out(up, UART_IER, up->ier);
86 }
87}
88
89static void serial_pxa_stop_rx(struct uart_port *port)
90{
91 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
92
93 up->ier &= ~UART_IER_RLSI;
94 up->port.read_status_mask &= ~UART_LSR_DR;
95 serial_out(up, UART_IER, up->ier);
96}
97
98static inline void receive_chars(struct uart_pxa_port *up, int *status)
99{
100 struct tty_struct *tty = up->port.state->port.tty;
101 unsigned int ch, flag;
102 int max_count = 256;
103
104 do {
105 ch = serial_in(up, UART_RX);
106 flag = TTY_NORMAL;
107 up->port.icount.rx++;
108
109 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
110 UART_LSR_FE | UART_LSR_OE))) {
111 /*
112 * For statistics only
113 */
114 if (*status & UART_LSR_BI) {
115 *status &= ~(UART_LSR_FE | UART_LSR_PE);
116 up->port.icount.brk++;
117 /*
118 * We do the SysRQ and SAK checking
119 * here because otherwise the break
120 * may get masked by ignore_status_mask
121 * or read_status_mask.
122 */
123 if (uart_handle_break(&up->port))
124 goto ignore_char;
125 } else if (*status & UART_LSR_PE)
126 up->port.icount.parity++;
127 else if (*status & UART_LSR_FE)
128 up->port.icount.frame++;
129 if (*status & UART_LSR_OE)
130 up->port.icount.overrun++;
131
132 /*
133 * Mask off conditions which should be ignored.
134 */
135 *status &= up->port.read_status_mask;
136
137#ifdef CONFIG_SERIAL_PXA_CONSOLE
138 if (up->port.line == up->port.cons->index) {
139 /* Recover the break flag from console xmit */
140 *status |= up->lsr_break_flag;
141 up->lsr_break_flag = 0;
142 }
143#endif
144 if (*status & UART_LSR_BI) {
145 flag = TTY_BREAK;
146 } else if (*status & UART_LSR_PE)
147 flag = TTY_PARITY;
148 else if (*status & UART_LSR_FE)
149 flag = TTY_FRAME;
150 }
151
152 if (uart_handle_sysrq_char(&up->port, ch))
153 goto ignore_char;
154
155 uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
156
157 ignore_char:
158 *status = serial_in(up, UART_LSR);
159 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
160 tty_flip_buffer_push(tty);
161}
162
163static void transmit_chars(struct uart_pxa_port *up)
164{
165 struct circ_buf *xmit = &up->port.state->xmit;
166 int count;
167
168 if (up->port.x_char) {
169 serial_out(up, UART_TX, up->port.x_char);
170 up->port.icount.tx++;
171 up->port.x_char = 0;
172 return;
173 }
174 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
175 serial_pxa_stop_tx(&up->port);
176 return;
177 }
178
179 count = up->port.fifosize / 2;
180 do {
181 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
182 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
183 up->port.icount.tx++;
184 if (uart_circ_empty(xmit))
185 break;
186 } while (--count > 0);
187
188 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
189 uart_write_wakeup(&up->port);
190
191
192 if (uart_circ_empty(xmit))
193 serial_pxa_stop_tx(&up->port);
194}
195
196static void serial_pxa_start_tx(struct uart_port *port)
197{
198 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
199
200 if (!(up->ier & UART_IER_THRI)) {
201 up->ier |= UART_IER_THRI;
202 serial_out(up, UART_IER, up->ier);
203 }
204}
205
206static inline void check_modem_status(struct uart_pxa_port *up)
207{
208 int status;
209
210 status = serial_in(up, UART_MSR);
211
212 if ((status & UART_MSR_ANY_DELTA) == 0)
213 return;
214
215 if (status & UART_MSR_TERI)
216 up->port.icount.rng++;
217 if (status & UART_MSR_DDSR)
218 up->port.icount.dsr++;
219 if (status & UART_MSR_DDCD)
220 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
221 if (status & UART_MSR_DCTS)
222 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
223
224 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
225}
226
227/*
228 * This handles the interrupt from one port.
229 */
230static inline irqreturn_t serial_pxa_irq(int irq, void *dev_id)
231{
232 struct uart_pxa_port *up = dev_id;
233 unsigned int iir, lsr;
234
235 iir = serial_in(up, UART_IIR);
236 if (iir & UART_IIR_NO_INT)
237 return IRQ_NONE;
238 lsr = serial_in(up, UART_LSR);
239 if (lsr & UART_LSR_DR)
240 receive_chars(up, &lsr);
241 check_modem_status(up);
242 if (lsr & UART_LSR_THRE)
243 transmit_chars(up);
244 return IRQ_HANDLED;
245}
246
247static unsigned int serial_pxa_tx_empty(struct uart_port *port)
248{
249 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
250 unsigned long flags;
251 unsigned int ret;
252
253 spin_lock_irqsave(&up->port.lock, flags);
254 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
255 spin_unlock_irqrestore(&up->port.lock, flags);
256
257 return ret;
258}
259
260static unsigned int serial_pxa_get_mctrl(struct uart_port *port)
261{
262 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
263 unsigned char status;
264 unsigned int ret;
265
266 status = serial_in(up, UART_MSR);
267
268 ret = 0;
269 if (status & UART_MSR_DCD)
270 ret |= TIOCM_CAR;
271 if (status & UART_MSR_RI)
272 ret |= TIOCM_RNG;
273 if (status & UART_MSR_DSR)
274 ret |= TIOCM_DSR;
275 if (status & UART_MSR_CTS)
276 ret |= TIOCM_CTS;
277 return ret;
278}
279
280static void serial_pxa_set_mctrl(struct uart_port *port, unsigned int mctrl)
281{
282 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
283 unsigned char mcr = 0;
284
285 if (mctrl & TIOCM_RTS)
286 mcr |= UART_MCR_RTS;
287 if (mctrl & TIOCM_DTR)
288 mcr |= UART_MCR_DTR;
289 if (mctrl & TIOCM_OUT1)
290 mcr |= UART_MCR_OUT1;
291 if (mctrl & TIOCM_OUT2)
292 mcr |= UART_MCR_OUT2;
293 if (mctrl & TIOCM_LOOP)
294 mcr |= UART_MCR_LOOP;
295
296 mcr |= up->mcr;
297
298 serial_out(up, UART_MCR, mcr);
299}
300
301static void serial_pxa_break_ctl(struct uart_port *port, int break_state)
302{
303 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
304 unsigned long flags;
305
306 spin_lock_irqsave(&up->port.lock, flags);
307 if (break_state == -1)
308 up->lcr |= UART_LCR_SBC;
309 else
310 up->lcr &= ~UART_LCR_SBC;
311 serial_out(up, UART_LCR, up->lcr);
312 spin_unlock_irqrestore(&up->port.lock, flags);
313}
314
315#if 0
316static void serial_pxa_dma_init(struct pxa_uart *up)
317{
318 up->rxdma =
319 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_receive_dma, up);
320 if (up->rxdma < 0)
321 goto out;
322 up->txdma =
323 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_transmit_dma, up);
324 if (up->txdma < 0)
325 goto err_txdma;
326 up->dmadesc = kmalloc(4 * sizeof(pxa_dma_desc), GFP_KERNEL);
327 if (!up->dmadesc)
328 goto err_alloc;
329
330 /* ... */
331err_alloc:
332 pxa_free_dma(up->txdma);
333err_rxdma:
334 pxa_free_dma(up->rxdma);
335out:
336 return;
337}
338#endif
339
340static int serial_pxa_startup(struct uart_port *port)
341{
342 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
343 unsigned long flags;
344 int retval;
345
346 if (port->line == 3) /* HWUART */
347 up->mcr |= UART_MCR_AFE;
348 else
349 up->mcr = 0;
350
351 up->port.uartclk = clk_get_rate(up->clk);
352
353 /*
354 * Allocate the IRQ
355 */
356 retval = request_irq(up->port.irq, serial_pxa_irq, 0, up->name, up);
357 if (retval)
358 return retval;
359
360 /*
361 * Clear the FIFO buffers and disable them.
362 * (they will be reenabled in set_termios())
363 */
364 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
365 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
366 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
367 serial_out(up, UART_FCR, 0);
368
369 /*
370 * Clear the interrupt registers.
371 */
372 (void) serial_in(up, UART_LSR);
373 (void) serial_in(up, UART_RX);
374 (void) serial_in(up, UART_IIR);
375 (void) serial_in(up, UART_MSR);
376
377 /*
378 * Now, initialize the UART
379 */
380 serial_out(up, UART_LCR, UART_LCR_WLEN8);
381
382 spin_lock_irqsave(&up->port.lock, flags);
383 up->port.mctrl |= TIOCM_OUT2;
384 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
385 spin_unlock_irqrestore(&up->port.lock, flags);
386
387 /*
388 * Finally, enable interrupts. Note: Modem status interrupts
389 * are set via set_termios(), which will be occurring imminently
390 * anyway, so we don't enable them here.
391 */
392 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
393 serial_out(up, UART_IER, up->ier);
394
395 /*
396 * And clear the interrupt registers again for luck.
397 */
398 (void) serial_in(up, UART_LSR);
399 (void) serial_in(up, UART_RX);
400 (void) serial_in(up, UART_IIR);
401 (void) serial_in(up, UART_MSR);
402
403 return 0;
404}
405
406static void serial_pxa_shutdown(struct uart_port *port)
407{
408 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
409 unsigned long flags;
410
411 free_irq(up->port.irq, up);
412
413 /*
414 * Disable interrupts from this port
415 */
416 up->ier = 0;
417 serial_out(up, UART_IER, 0);
418
419 spin_lock_irqsave(&up->port.lock, flags);
420 up->port.mctrl &= ~TIOCM_OUT2;
421 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
422 spin_unlock_irqrestore(&up->port.lock, flags);
423
424 /*
425 * Disable break condition and FIFOs
426 */
427 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
428 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
429 UART_FCR_CLEAR_RCVR |
430 UART_FCR_CLEAR_XMIT);
431 serial_out(up, UART_FCR, 0);
432}
433
434static void
435serial_pxa_set_termios(struct uart_port *port, struct ktermios *termios,
436 struct ktermios *old)
437{
438 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
439 unsigned char cval, fcr = 0;
440 unsigned long flags;
441 unsigned int baud, quot;
442 unsigned int dll;
443
444 switch (termios->c_cflag & CSIZE) {
445 case CS5:
446 cval = UART_LCR_WLEN5;
447 break;
448 case CS6:
449 cval = UART_LCR_WLEN6;
450 break;
451 case CS7:
452 cval = UART_LCR_WLEN7;
453 break;
454 default:
455 case CS8:
456 cval = UART_LCR_WLEN8;
457 break;
458 }
459
460 if (termios->c_cflag & CSTOPB)
461 cval |= UART_LCR_STOP;
462 if (termios->c_cflag & PARENB)
463 cval |= UART_LCR_PARITY;
464 if (!(termios->c_cflag & PARODD))
465 cval |= UART_LCR_EPAR;
466
467 /*
468 * Ask the core to calculate the divisor for us.
469 */
470 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
471 quot = uart_get_divisor(port, baud);
472
473 if ((up->port.uartclk / quot) < (2400 * 16))
474 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR1;
475 else if ((up->port.uartclk / quot) < (230400 * 16))
476 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR8;
477 else
478 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR32;
479
480 /*
481 * Ok, we're now changing the port state. Do it with
482 * interrupts disabled.
483 */
484 spin_lock_irqsave(&up->port.lock, flags);
485
486 /*
487 * Ensure the port will be enabled.
488 * This is required especially for serial console.
489 */
490 up->ier |= UART_IER_UUE;
491
492 /*
493 * Update the per-port timeout.
494 */
495 uart_update_timeout(port, termios->c_cflag, baud);
496
497 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
498 if (termios->c_iflag & INPCK)
499 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
500 if (termios->c_iflag & (BRKINT | PARMRK))
501 up->port.read_status_mask |= UART_LSR_BI;
502
503 /*
504 * Characters to ignore
505 */
506 up->port.ignore_status_mask = 0;
507 if (termios->c_iflag & IGNPAR)
508 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
509 if (termios->c_iflag & IGNBRK) {
510 up->port.ignore_status_mask |= UART_LSR_BI;
511 /*
512 * If we're ignoring parity and break indicators,
513 * ignore overruns too (for real raw support).
514 */
515 if (termios->c_iflag & IGNPAR)
516 up->port.ignore_status_mask |= UART_LSR_OE;
517 }
518
519 /*
520 * ignore all characters if CREAD is not set
521 */
522 if ((termios->c_cflag & CREAD) == 0)
523 up->port.ignore_status_mask |= UART_LSR_DR;
524
525 /*
526 * CTS flow control flag and modem status interrupts
527 */
528 up->ier &= ~UART_IER_MSI;
529 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
530 up->ier |= UART_IER_MSI;
531
532 serial_out(up, UART_IER, up->ier);
533
534 if (termios->c_cflag & CRTSCTS)
535 up->mcr |= UART_MCR_AFE;
536 else
537 up->mcr &= ~UART_MCR_AFE;
538
539 serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
540 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
541
542 /*
543 * work around Errata #75 according to Intel(R) PXA27x Processor Family
544 * Specification Update (Nov 2005)
545 */
546 dll = serial_in(up, UART_DLL);
547 WARN_ON(dll != (quot & 0xff));
548
549 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
550 serial_out(up, UART_LCR, cval); /* reset DLAB */
551 up->lcr = cval; /* Save LCR */
552 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
553 serial_out(up, UART_FCR, fcr);
554 spin_unlock_irqrestore(&up->port.lock, flags);
555}
556
557static void
558serial_pxa_pm(struct uart_port *port, unsigned int state,
559 unsigned int oldstate)
560{
561 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
562
563 if (!state)
564 clk_enable(up->clk);
565 else
566 clk_disable(up->clk);
567}
568
569static void serial_pxa_release_port(struct uart_port *port)
570{
571}
572
573static int serial_pxa_request_port(struct uart_port *port)
574{
575 return 0;
576}
577
578static void serial_pxa_config_port(struct uart_port *port, int flags)
579{
580 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
581 up->port.type = PORT_PXA;
582}
583
584static int
585serial_pxa_verify_port(struct uart_port *port, struct serial_struct *ser)
586{
587 /* we don't want the core code to modify any port params */
588 return -EINVAL;
589}
590
591static const char *
592serial_pxa_type(struct uart_port *port)
593{
594 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
595 return up->name;
596}
597
598static struct uart_pxa_port *serial_pxa_ports[4];
599static struct uart_driver serial_pxa_reg;
600
601#ifdef CONFIG_SERIAL_PXA_CONSOLE
602
603#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
604
605/*
606 * Wait for transmitter & holding register to empty
607 */
608static inline void wait_for_xmitr(struct uart_pxa_port *up)
609{
610 unsigned int status, tmout = 10000;
611
612 /* Wait up to 10ms for the character(s) to be sent. */
613 do {
614 status = serial_in(up, UART_LSR);
615
616 if (status & UART_LSR_BI)
617 up->lsr_break_flag = UART_LSR_BI;
618
619 if (--tmout == 0)
620 break;
621 udelay(1);
622 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
623
624 /* Wait up to 1s for flow control if necessary */
625 if (up->port.flags & UPF_CONS_FLOW) {
626 tmout = 1000000;
627 while (--tmout &&
628 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
629 udelay(1);
630 }
631}
632
633static void serial_pxa_console_putchar(struct uart_port *port, int ch)
634{
635 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
636
637 wait_for_xmitr(up);
638 serial_out(up, UART_TX, ch);
639}
640
641/*
642 * Print a string to the serial port trying not to disturb
643 * any possible real use of the port...
644 *
645 * The console_lock must be held when we get here.
646 */
647static void
648serial_pxa_console_write(struct console *co, const char *s, unsigned int count)
649{
650 struct uart_pxa_port *up = serial_pxa_ports[co->index];
651 unsigned int ier;
652
653 clk_enable(up->clk);
654
655 /*
656 * First save the IER then disable the interrupts
657 */
658 ier = serial_in(up, UART_IER);
659 serial_out(up, UART_IER, UART_IER_UUE);
660
661 uart_console_write(&up->port, s, count, serial_pxa_console_putchar);
662
663 /*
664 * Finally, wait for transmitter to become empty
665 * and restore the IER
666 */
667 wait_for_xmitr(up);
668 serial_out(up, UART_IER, ier);
669
670 clk_disable(up->clk);
671}
672
673static int __init
674serial_pxa_console_setup(struct console *co, char *options)
675{
676 struct uart_pxa_port *up;
677 int baud = 9600;
678 int bits = 8;
679 int parity = 'n';
680 int flow = 'n';
681
682 if (co->index == -1 || co->index >= serial_pxa_reg.nr)
683 co->index = 0;
684 up = serial_pxa_ports[co->index];
685 if (!up)
686 return -ENODEV;
687
688 if (options)
689 uart_parse_options(options, &baud, &parity, &bits, &flow);
690
691 return uart_set_options(&up->port, co, baud, parity, bits, flow);
692}
693
694static struct console serial_pxa_console = {
695 .name = "ttyS",
696 .write = serial_pxa_console_write,
697 .device = uart_console_device,
698 .setup = serial_pxa_console_setup,
699 .flags = CON_PRINTBUFFER,
700 .index = -1,
701 .data = &serial_pxa_reg,
702};
703
704#define PXA_CONSOLE &serial_pxa_console
705#else
706#define PXA_CONSOLE NULL
707#endif
708
709struct uart_ops serial_pxa_pops = {
710 .tx_empty = serial_pxa_tx_empty,
711 .set_mctrl = serial_pxa_set_mctrl,
712 .get_mctrl = serial_pxa_get_mctrl,
713 .stop_tx = serial_pxa_stop_tx,
714 .start_tx = serial_pxa_start_tx,
715 .stop_rx = serial_pxa_stop_rx,
716 .enable_ms = serial_pxa_enable_ms,
717 .break_ctl = serial_pxa_break_ctl,
718 .startup = serial_pxa_startup,
719 .shutdown = serial_pxa_shutdown,
720 .set_termios = serial_pxa_set_termios,
721 .pm = serial_pxa_pm,
722 .type = serial_pxa_type,
723 .release_port = serial_pxa_release_port,
724 .request_port = serial_pxa_request_port,
725 .config_port = serial_pxa_config_port,
726 .verify_port = serial_pxa_verify_port,
727};
728
729static struct uart_driver serial_pxa_reg = {
730 .owner = THIS_MODULE,
731 .driver_name = "PXA serial",
732 .dev_name = "ttyS",
733 .major = TTY_MAJOR,
734 .minor = 64,
735 .nr = 4,
736 .cons = PXA_CONSOLE,
737};
738
739#ifdef CONFIG_PM
740static int serial_pxa_suspend(struct device *dev)
741{
742 struct uart_pxa_port *sport = dev_get_drvdata(dev);
743
744 if (sport)
745 uart_suspend_port(&serial_pxa_reg, &sport->port);
746
747 return 0;
748}
749
750static int serial_pxa_resume(struct device *dev)
751{
752 struct uart_pxa_port *sport = dev_get_drvdata(dev);
753
754 if (sport)
755 uart_resume_port(&serial_pxa_reg, &sport->port);
756
757 return 0;
758}
759
760static const struct dev_pm_ops serial_pxa_pm_ops = {
761 .suspend = serial_pxa_suspend,
762 .resume = serial_pxa_resume,
763};
764#endif
765
766static int serial_pxa_probe(struct platform_device *dev)
767{
768 struct uart_pxa_port *sport;
769 struct resource *mmres, *irqres;
770 int ret;
771
772 mmres = platform_get_resource(dev, IORESOURCE_MEM, 0);
773 irqres = platform_get_resource(dev, IORESOURCE_IRQ, 0);
774 if (!mmres || !irqres)
775 return -ENODEV;
776
777 sport = kzalloc(sizeof(struct uart_pxa_port), GFP_KERNEL);
778 if (!sport)
779 return -ENOMEM;
780
781 sport->clk = clk_get(&dev->dev, NULL);
782 if (IS_ERR(sport->clk)) {
783 ret = PTR_ERR(sport->clk);
784 goto err_free;
785 }
786
787 sport->port.type = PORT_PXA;
788 sport->port.iotype = UPIO_MEM;
789 sport->port.mapbase = mmres->start;
790 sport->port.irq = irqres->start;
791 sport->port.fifosize = 64;
792 sport->port.ops = &serial_pxa_pops;
793 sport->port.line = dev->id;
794 sport->port.dev = &dev->dev;
795 sport->port.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
796 sport->port.uartclk = clk_get_rate(sport->clk);
797
798 switch (dev->id) {
799 case 0: sport->name = "FFUART"; break;
800 case 1: sport->name = "BTUART"; break;
801 case 2: sport->name = "STUART"; break;
802 case 3: sport->name = "HWUART"; break;
803 default:
804 sport->name = "???";
805 break;
806 }
807
808 sport->port.membase = ioremap(mmres->start, mmres->end - mmres->start + 1);
809 if (!sport->port.membase) {
810 ret = -ENOMEM;
811 goto err_clk;
812 }
813
814 serial_pxa_ports[dev->id] = sport;
815
816 uart_add_one_port(&serial_pxa_reg, &sport->port);
817 platform_set_drvdata(dev, sport);
818
819 return 0;
820
821 err_clk:
822 clk_put(sport->clk);
823 err_free:
824 kfree(sport);
825 return ret;
826}
827
828static int serial_pxa_remove(struct platform_device *dev)
829{
830 struct uart_pxa_port *sport = platform_get_drvdata(dev);
831
832 platform_set_drvdata(dev, NULL);
833
834 uart_remove_one_port(&serial_pxa_reg, &sport->port);
835 clk_put(sport->clk);
836 kfree(sport);
837
838 return 0;
839}
840
841static struct platform_driver serial_pxa_driver = {
842 .probe = serial_pxa_probe,
843 .remove = serial_pxa_remove,
844
845 .driver = {
846 .name = "pxa2xx-uart",
847 .owner = THIS_MODULE,
848#ifdef CONFIG_PM
849 .pm = &serial_pxa_pm_ops,
850#endif
851 },
852};
853
854int __init serial_pxa_init(void)
855{
856 int ret;
857
858 ret = uart_register_driver(&serial_pxa_reg);
859 if (ret != 0)
860 return ret;
861
862 ret = platform_driver_register(&serial_pxa_driver);
863 if (ret != 0)
864 uart_unregister_driver(&serial_pxa_reg);
865
866 return ret;
867}
868
869void __exit serial_pxa_exit(void)
870{
871 platform_driver_unregister(&serial_pxa_driver);
872 uart_unregister_driver(&serial_pxa_reg);
873}
874
875module_init(serial_pxa_init);
876module_exit(serial_pxa_exit);
877
878MODULE_LICENSE("GPL");
879MODULE_ALIAS("platform:pxa2xx-uart");
diff --git a/drivers/tty/serial/s3c2400.c b/drivers/tty/serial/s3c2400.c
new file mode 100644
index 000000000000..fed1a9a1ffb4
--- /dev/null
+++ b/drivers/tty/serial/s3c2400.c
@@ -0,0 +1,106 @@
1/* linux/drivers/serial/s3c240.c
2 *
3 * Driver for Samsung SoC onboard UARTs.
4 *
5 * Ben Dooks, Copyright (c) 2003-2005 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
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
13#include <linux/module.h>
14#include <linux/ioport.h>
15#include <linux/io.h>
16#include <linux/platform_device.h>
17
18#include <asm/irq.h>
19
20#include <mach/hardware.h>
21
22#include <plat/regs-serial.h>
23#include <mach/regs-gpio.h>
24
25#include "samsung.h"
26
27static int s3c2400_serial_getsource(struct uart_port *port,
28 struct s3c24xx_uart_clksrc *clk)
29{
30 clk->divisor = 1;
31 clk->name = "pclk";
32
33 return 0;
34}
35
36static int s3c2400_serial_setsource(struct uart_port *port,
37 struct s3c24xx_uart_clksrc *clk)
38{
39 return 0;
40}
41
42static int s3c2400_serial_resetport(struct uart_port *port,
43 struct s3c2410_uartcfg *cfg)
44{
45 dbg("s3c2400_serial_resetport: port=%p (%08lx), cfg=%p\n",
46 port, port->mapbase, cfg);
47
48 wr_regl(port, S3C2410_UCON, cfg->ucon);
49 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
50
51 /* reset both fifos */
52
53 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
54 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
55
56 return 0;
57}
58
59static struct s3c24xx_uart_info s3c2400_uart_inf = {
60 .name = "Samsung S3C2400 UART",
61 .type = PORT_S3C2400,
62 .fifosize = 16,
63 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
64 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
65 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
66 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
67 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
68 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
69 .get_clksrc = s3c2400_serial_getsource,
70 .set_clksrc = s3c2400_serial_setsource,
71 .reset_port = s3c2400_serial_resetport,
72};
73
74static int s3c2400_serial_probe(struct platform_device *dev)
75{
76 return s3c24xx_serial_probe(dev, &s3c2400_uart_inf);
77}
78
79static struct platform_driver s3c2400_serial_driver = {
80 .probe = s3c2400_serial_probe,
81 .remove = __devexit_p(s3c24xx_serial_remove),
82 .driver = {
83 .name = "s3c2400-uart",
84 .owner = THIS_MODULE,
85 },
86};
87
88s3c24xx_console_init(&s3c2400_serial_driver, &s3c2400_uart_inf);
89
90static inline int s3c2400_serial_init(void)
91{
92 return s3c24xx_serial_init(&s3c2400_serial_driver, &s3c2400_uart_inf);
93}
94
95static inline void s3c2400_serial_exit(void)
96{
97 platform_driver_unregister(&s3c2400_serial_driver);
98}
99
100module_init(s3c2400_serial_init);
101module_exit(s3c2400_serial_exit);
102
103MODULE_LICENSE("GPL v2");
104MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
105MODULE_DESCRIPTION("Samsung S3C2400 SoC Serial port driver");
106MODULE_ALIAS("platform:s3c2400-uart");
diff --git a/drivers/tty/serial/s3c2410.c b/drivers/tty/serial/s3c2410.c
new file mode 100644
index 000000000000..73f089d3efd6
--- /dev/null
+++ b/drivers/tty/serial/s3c2410.c
@@ -0,0 +1,118 @@
1/* linux/drivers/serial/s3c2410.c
2 *
3 * Driver for Samsung S3C2410 SoC onboard UARTs.
4 *
5 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
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
13#include <linux/module.h>
14#include <linux/ioport.h>
15#include <linux/io.h>
16#include <linux/platform_device.h>
17#include <linux/init.h>
18#include <linux/serial_core.h>
19#include <linux/serial.h>
20
21#include <asm/irq.h>
22#include <mach/hardware.h>
23
24#include <plat/regs-serial.h>
25#include <mach/regs-gpio.h>
26
27#include "samsung.h"
28
29static int s3c2410_serial_setsource(struct uart_port *port,
30 struct s3c24xx_uart_clksrc *clk)
31{
32 unsigned long ucon = rd_regl(port, S3C2410_UCON);
33
34 if (strcmp(clk->name, "uclk") == 0)
35 ucon |= S3C2410_UCON_UCLK;
36 else
37 ucon &= ~S3C2410_UCON_UCLK;
38
39 wr_regl(port, S3C2410_UCON, ucon);
40 return 0;
41}
42
43static int s3c2410_serial_getsource(struct uart_port *port,
44 struct s3c24xx_uart_clksrc *clk)
45{
46 unsigned long ucon = rd_regl(port, S3C2410_UCON);
47
48 clk->divisor = 1;
49 clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
50
51 return 0;
52}
53
54static int s3c2410_serial_resetport(struct uart_port *port,
55 struct s3c2410_uartcfg *cfg)
56{
57 dbg("s3c2410_serial_resetport: port=%p (%08lx), cfg=%p\n",
58 port, port->mapbase, cfg);
59
60 wr_regl(port, S3C2410_UCON, cfg->ucon);
61 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
62
63 /* reset both fifos */
64
65 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
66 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
67
68 return 0;
69}
70
71static struct s3c24xx_uart_info s3c2410_uart_inf = {
72 .name = "Samsung S3C2410 UART",
73 .type = PORT_S3C2410,
74 .fifosize = 16,
75 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
76 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
77 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
78 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
79 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
80 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
81 .get_clksrc = s3c2410_serial_getsource,
82 .set_clksrc = s3c2410_serial_setsource,
83 .reset_port = s3c2410_serial_resetport,
84};
85
86static int s3c2410_serial_probe(struct platform_device *dev)
87{
88 return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
89}
90
91static struct platform_driver s3c2410_serial_driver = {
92 .probe = s3c2410_serial_probe,
93 .remove = __devexit_p(s3c24xx_serial_remove),
94 .driver = {
95 .name = "s3c2410-uart",
96 .owner = THIS_MODULE,
97 },
98};
99
100s3c24xx_console_init(&s3c2410_serial_driver, &s3c2410_uart_inf);
101
102static int __init s3c2410_serial_init(void)
103{
104 return s3c24xx_serial_init(&s3c2410_serial_driver, &s3c2410_uart_inf);
105}
106
107static void __exit s3c2410_serial_exit(void)
108{
109 platform_driver_unregister(&s3c2410_serial_driver);
110}
111
112module_init(s3c2410_serial_init);
113module_exit(s3c2410_serial_exit);
114
115MODULE_LICENSE("GPL v2");
116MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
117MODULE_DESCRIPTION("Samsung S3C2410 SoC Serial port driver");
118MODULE_ALIAS("platform:s3c2410-uart");
diff --git a/drivers/tty/serial/s3c2412.c b/drivers/tty/serial/s3c2412.c
new file mode 100644
index 000000000000..1700b1a2fb7e
--- /dev/null
+++ b/drivers/tty/serial/s3c2412.c
@@ -0,0 +1,152 @@
1/* linux/drivers/serial/s3c2412.c
2 *
3 * Driver for Samsung S3C2412 and S3C2413 SoC onboard UARTs.
4 *
5 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
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
13#include <linux/module.h>
14#include <linux/ioport.h>
15#include <linux/io.h>
16#include <linux/platform_device.h>
17#include <linux/init.h>
18#include <linux/serial_core.h>
19#include <linux/serial.h>
20
21#include <asm/irq.h>
22#include <mach/hardware.h>
23
24#include <plat/regs-serial.h>
25#include <mach/regs-gpio.h>
26
27#include "samsung.h"
28
29static int s3c2412_serial_setsource(struct uart_port *port,
30 struct s3c24xx_uart_clksrc *clk)
31{
32 unsigned long ucon = rd_regl(port, S3C2410_UCON);
33
34 ucon &= ~S3C2412_UCON_CLKMASK;
35
36 if (strcmp(clk->name, "uclk") == 0)
37 ucon |= S3C2440_UCON_UCLK;
38 else if (strcmp(clk->name, "pclk") == 0)
39 ucon |= S3C2440_UCON_PCLK;
40 else if (strcmp(clk->name, "usysclk") == 0)
41 ucon |= S3C2412_UCON_USYSCLK;
42 else {
43 printk(KERN_ERR "unknown clock source %s\n", clk->name);
44 return -EINVAL;
45 }
46
47 wr_regl(port, S3C2410_UCON, ucon);
48 return 0;
49}
50
51
52static int s3c2412_serial_getsource(struct uart_port *port,
53 struct s3c24xx_uart_clksrc *clk)
54{
55 unsigned long ucon = rd_regl(port, S3C2410_UCON);
56
57 switch (ucon & S3C2412_UCON_CLKMASK) {
58 case S3C2412_UCON_UCLK:
59 clk->divisor = 1;
60 clk->name = "uclk";
61 break;
62
63 case S3C2412_UCON_PCLK:
64 case S3C2412_UCON_PCLK2:
65 clk->divisor = 1;
66 clk->name = "pclk";
67 break;
68
69 case S3C2412_UCON_USYSCLK:
70 clk->divisor = 1;
71 clk->name = "usysclk";
72 break;
73 }
74
75 return 0;
76}
77
78static int s3c2412_serial_resetport(struct uart_port *port,
79 struct s3c2410_uartcfg *cfg)
80{
81 unsigned long ucon = rd_regl(port, S3C2410_UCON);
82
83 dbg("%s: port=%p (%08lx), cfg=%p\n",
84 __func__, port, port->mapbase, cfg);
85
86 /* ensure we don't change the clock settings... */
87
88 ucon &= S3C2412_UCON_CLKMASK;
89
90 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
91 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
92
93 /* reset both fifos */
94
95 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
96 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
97
98 return 0;
99}
100
101static struct s3c24xx_uart_info s3c2412_uart_inf = {
102 .name = "Samsung S3C2412 UART",
103 .type = PORT_S3C2412,
104 .fifosize = 64,
105 .has_divslot = 1,
106 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
107 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
108 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
109 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
110 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
111 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
112 .get_clksrc = s3c2412_serial_getsource,
113 .set_clksrc = s3c2412_serial_setsource,
114 .reset_port = s3c2412_serial_resetport,
115};
116
117/* device management */
118
119static int s3c2412_serial_probe(struct platform_device *dev)
120{
121 dbg("s3c2440_serial_probe: dev=%p\n", dev);
122 return s3c24xx_serial_probe(dev, &s3c2412_uart_inf);
123}
124
125static struct platform_driver s3c2412_serial_driver = {
126 .probe = s3c2412_serial_probe,
127 .remove = __devexit_p(s3c24xx_serial_remove),
128 .driver = {
129 .name = "s3c2412-uart",
130 .owner = THIS_MODULE,
131 },
132};
133
134s3c24xx_console_init(&s3c2412_serial_driver, &s3c2412_uart_inf);
135
136static inline int s3c2412_serial_init(void)
137{
138 return s3c24xx_serial_init(&s3c2412_serial_driver, &s3c2412_uart_inf);
139}
140
141static inline void s3c2412_serial_exit(void)
142{
143 platform_driver_unregister(&s3c2412_serial_driver);
144}
145
146module_init(s3c2412_serial_init);
147module_exit(s3c2412_serial_exit);
148
149MODULE_DESCRIPTION("Samsung S3C2412,S3C2413 SoC Serial port driver");
150MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
151MODULE_LICENSE("GPL v2");
152MODULE_ALIAS("platform:s3c2412-uart");
diff --git a/drivers/tty/serial/s3c2440.c b/drivers/tty/serial/s3c2440.c
new file mode 100644
index 000000000000..094cc3904b13
--- /dev/null
+++ b/drivers/tty/serial/s3c2440.c
@@ -0,0 +1,181 @@
1/* linux/drivers/serial/s3c2440.c
2 *
3 * Driver for Samsung S3C2440 and S3C2442 SoC onboard UARTs.
4 *
5 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
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
13#include <linux/module.h>
14#include <linux/ioport.h>
15#include <linux/io.h>
16#include <linux/platform_device.h>
17#include <linux/init.h>
18#include <linux/serial_core.h>
19#include <linux/serial.h>
20
21#include <asm/irq.h>
22#include <mach/hardware.h>
23
24#include <plat/regs-serial.h>
25#include <mach/regs-gpio.h>
26
27#include "samsung.h"
28
29
30static int s3c2440_serial_setsource(struct uart_port *port,
31 struct s3c24xx_uart_clksrc *clk)
32{
33 unsigned long ucon = rd_regl(port, S3C2410_UCON);
34
35 /* todo - proper fclk<>nonfclk switch. */
36
37 ucon &= ~S3C2440_UCON_CLKMASK;
38
39 if (strcmp(clk->name, "uclk") == 0)
40 ucon |= S3C2440_UCON_UCLK;
41 else if (strcmp(clk->name, "pclk") == 0)
42 ucon |= S3C2440_UCON_PCLK;
43 else if (strcmp(clk->name, "fclk") == 0)
44 ucon |= S3C2440_UCON_FCLK;
45 else {
46 printk(KERN_ERR "unknown clock source %s\n", clk->name);
47 return -EINVAL;
48 }
49
50 wr_regl(port, S3C2410_UCON, ucon);
51 return 0;
52}
53
54
55static int s3c2440_serial_getsource(struct uart_port *port,
56 struct s3c24xx_uart_clksrc *clk)
57{
58 unsigned long ucon = rd_regl(port, S3C2410_UCON);
59 unsigned long ucon0, ucon1, ucon2;
60
61 switch (ucon & S3C2440_UCON_CLKMASK) {
62 case S3C2440_UCON_UCLK:
63 clk->divisor = 1;
64 clk->name = "uclk";
65 break;
66
67 case S3C2440_UCON_PCLK:
68 case S3C2440_UCON_PCLK2:
69 clk->divisor = 1;
70 clk->name = "pclk";
71 break;
72
73 case S3C2440_UCON_FCLK:
74 /* the fun of calculating the uart divisors on
75 * the s3c2440 */
76
77 ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
78 ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
79 ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
80
81 printk("ucons: %08lx, %08lx, %08lx\n", ucon0, ucon1, ucon2);
82
83 ucon0 &= S3C2440_UCON0_DIVMASK;
84 ucon1 &= S3C2440_UCON1_DIVMASK;
85 ucon2 &= S3C2440_UCON2_DIVMASK;
86
87 if (ucon0 != 0) {
88 clk->divisor = ucon0 >> S3C2440_UCON_DIVSHIFT;
89 clk->divisor += 6;
90 } else if (ucon1 != 0) {
91 clk->divisor = ucon1 >> S3C2440_UCON_DIVSHIFT;
92 clk->divisor += 21;
93 } else if (ucon2 != 0) {
94 clk->divisor = ucon2 >> S3C2440_UCON_DIVSHIFT;
95 clk->divisor += 36;
96 } else {
97 /* manual calims 44, seems to be 9 */
98 clk->divisor = 9;
99 }
100
101 clk->name = "fclk";
102 break;
103 }
104
105 return 0;
106}
107
108static int s3c2440_serial_resetport(struct uart_port *port,
109 struct s3c2410_uartcfg *cfg)
110{
111 unsigned long ucon = rd_regl(port, S3C2410_UCON);
112
113 dbg("s3c2440_serial_resetport: port=%p (%08lx), cfg=%p\n",
114 port, port->mapbase, cfg);
115
116 /* ensure we don't change the clock settings... */
117
118 ucon &= (S3C2440_UCON0_DIVMASK | (3<<10));
119
120 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
121 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
122
123 /* reset both fifos */
124
125 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
126 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
127
128 return 0;
129}
130
131static struct s3c24xx_uart_info s3c2440_uart_inf = {
132 .name = "Samsung S3C2440 UART",
133 .type = PORT_S3C2440,
134 .fifosize = 64,
135 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
136 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
137 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
138 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
139 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
140 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
141 .get_clksrc = s3c2440_serial_getsource,
142 .set_clksrc = s3c2440_serial_setsource,
143 .reset_port = s3c2440_serial_resetport,
144};
145
146/* device management */
147
148static int s3c2440_serial_probe(struct platform_device *dev)
149{
150 dbg("s3c2440_serial_probe: dev=%p\n", dev);
151 return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
152}
153
154static struct platform_driver s3c2440_serial_driver = {
155 .probe = s3c2440_serial_probe,
156 .remove = __devexit_p(s3c24xx_serial_remove),
157 .driver = {
158 .name = "s3c2440-uart",
159 .owner = THIS_MODULE,
160 },
161};
162
163s3c24xx_console_init(&s3c2440_serial_driver, &s3c2440_uart_inf);
164
165static int __init s3c2440_serial_init(void)
166{
167 return s3c24xx_serial_init(&s3c2440_serial_driver, &s3c2440_uart_inf);
168}
169
170static void __exit s3c2440_serial_exit(void)
171{
172 platform_driver_unregister(&s3c2440_serial_driver);
173}
174
175module_init(s3c2440_serial_init);
176module_exit(s3c2440_serial_exit);
177
178MODULE_DESCRIPTION("Samsung S3C2440,S3C2442 SoC Serial port driver");
179MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
180MODULE_LICENSE("GPL v2");
181MODULE_ALIAS("platform:s3c2440-uart");
diff --git a/drivers/tty/serial/s3c24a0.c b/drivers/tty/serial/s3c24a0.c
new file mode 100644
index 000000000000..fad6083ca427
--- /dev/null
+++ b/drivers/tty/serial/s3c24a0.c
@@ -0,0 +1,118 @@
1/* linux/drivers/serial/s3c24a0.c
2 *
3 * Driver for Samsung S3C24A0 SoC onboard UARTs.
4 *
5 * Based on drivers/serial/s3c2410.c
6 *
7 * Author: Sandeep Patil <sandeep.patil@azingo.com>
8 *
9 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
10 * http://armlinux.simtec.co.uk/
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 version 2 as
14 * published by the Free Software Foundation.
15*/
16
17#include <linux/module.h>
18#include <linux/ioport.h>
19#include <linux/platform_device.h>
20#include <linux/init.h>
21#include <linux/serial_core.h>
22#include <linux/serial.h>
23#include <linux/io.h>
24#include <linux/irq.h>
25
26#include <mach/hardware.h>
27
28#include <plat/regs-serial.h>
29#include <mach/regs-gpio.h>
30
31#include "samsung.h"
32
33static int s3c24a0_serial_setsource(struct uart_port *port,
34 struct s3c24xx_uart_clksrc *clk)
35{
36 unsigned long ucon = rd_regl(port, S3C2410_UCON);
37
38 if (strcmp(clk->name, "uclk") == 0)
39 ucon |= S3C2410_UCON_UCLK;
40 else
41 ucon &= ~S3C2410_UCON_UCLK;
42
43 wr_regl(port, S3C2410_UCON, ucon);
44 return 0;
45}
46
47static int s3c24a0_serial_getsource(struct uart_port *port,
48 struct s3c24xx_uart_clksrc *clk)
49{
50 unsigned long ucon = rd_regl(port, S3C2410_UCON);
51
52 clk->divisor = 1;
53 clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
54
55 return 0;
56}
57
58static int s3c24a0_serial_resetport(struct uart_port *port,
59 struct s3c2410_uartcfg *cfg)
60{
61 dbg("s3c24a0_serial_resetport: port=%p (%08lx), cfg=%p\n",
62 port, port->mapbase, cfg);
63
64 wr_regl(port, S3C2410_UCON, cfg->ucon);
65 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
66
67 /* reset both fifos */
68
69 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
70 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
71
72 return 0;
73}
74
75static struct s3c24xx_uart_info s3c24a0_uart_inf = {
76 .name = "Samsung S3C24A0 UART",
77 .type = PORT_S3C2410,
78 .fifosize = 16,
79 .rx_fifomask = S3C24A0_UFSTAT_RXMASK,
80 .rx_fifoshift = S3C24A0_UFSTAT_RXSHIFT,
81 .rx_fifofull = S3C24A0_UFSTAT_RXFULL,
82 .tx_fifofull = S3C24A0_UFSTAT_TXFULL,
83 .tx_fifomask = S3C24A0_UFSTAT_TXMASK,
84 .tx_fifoshift = S3C24A0_UFSTAT_TXSHIFT,
85 .get_clksrc = s3c24a0_serial_getsource,
86 .set_clksrc = s3c24a0_serial_setsource,
87 .reset_port = s3c24a0_serial_resetport,
88};
89
90static int s3c24a0_serial_probe(struct platform_device *dev)
91{
92 return s3c24xx_serial_probe(dev, &s3c24a0_uart_inf);
93}
94
95static struct platform_driver s3c24a0_serial_driver = {
96 .probe = s3c24a0_serial_probe,
97 .remove = __devexit_p(s3c24xx_serial_remove),
98 .driver = {
99 .name = "s3c24a0-uart",
100 .owner = THIS_MODULE,
101 },
102};
103
104s3c24xx_console_init(&s3c24a0_serial_driver, &s3c24a0_uart_inf);
105
106static int __init s3c24a0_serial_init(void)
107{
108 return s3c24xx_serial_init(&s3c24a0_serial_driver, &s3c24a0_uart_inf);
109}
110
111static void __exit s3c24a0_serial_exit(void)
112{
113 platform_driver_unregister(&s3c24a0_serial_driver);
114}
115
116module_init(s3c24a0_serial_init);
117module_exit(s3c24a0_serial_exit);
118
diff --git a/drivers/tty/serial/s3c6400.c b/drivers/tty/serial/s3c6400.c
new file mode 100644
index 000000000000..4be92ab50058
--- /dev/null
+++ b/drivers/tty/serial/s3c6400.c
@@ -0,0 +1,152 @@
1/* linux/drivers/serial/s3c6400.c
2 *
3 * Driver for Samsung S3C6400 and S3C6410 SoC onboard UARTs.
4 *
5 * Copyright 2008 Openmoko, Inc.
6 * Copyright 2008 Simtec Electronics
7 * Ben Dooks <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
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 version 2 as
12 * published by the Free Software Foundation.
13*/
14
15#include <linux/module.h>
16#include <linux/ioport.h>
17#include <linux/io.h>
18#include <linux/platform_device.h>
19#include <linux/init.h>
20#include <linux/serial_core.h>
21#include <linux/serial.h>
22
23#include <asm/irq.h>
24#include <mach/hardware.h>
25
26#include <plat/regs-serial.h>
27
28#include "samsung.h"
29
30static int s3c6400_serial_setsource(struct uart_port *port,
31 struct s3c24xx_uart_clksrc *clk)
32{
33 unsigned long ucon = rd_regl(port, S3C2410_UCON);
34
35 if (strcmp(clk->name, "uclk0") == 0) {
36 ucon &= ~S3C6400_UCON_CLKMASK;
37 ucon |= S3C6400_UCON_UCLK0;
38 } else if (strcmp(clk->name, "uclk1") == 0)
39 ucon |= S3C6400_UCON_UCLK1;
40 else if (strcmp(clk->name, "pclk") == 0) {
41 /* See notes about transitioning from UCLK to PCLK */
42 ucon &= ~S3C6400_UCON_UCLK0;
43 } else {
44 printk(KERN_ERR "unknown clock source %s\n", clk->name);
45 return -EINVAL;
46 }
47
48 wr_regl(port, S3C2410_UCON, ucon);
49 return 0;
50}
51
52
53static int s3c6400_serial_getsource(struct uart_port *port,
54 struct s3c24xx_uart_clksrc *clk)
55{
56 u32 ucon = rd_regl(port, S3C2410_UCON);
57
58 clk->divisor = 1;
59
60 switch (ucon & S3C6400_UCON_CLKMASK) {
61 case S3C6400_UCON_UCLK0:
62 clk->name = "uclk0";
63 break;
64
65 case S3C6400_UCON_UCLK1:
66 clk->name = "uclk1";
67 break;
68
69 case S3C6400_UCON_PCLK:
70 case S3C6400_UCON_PCLK2:
71 clk->name = "pclk";
72 break;
73 }
74
75 return 0;
76}
77
78static int s3c6400_serial_resetport(struct uart_port *port,
79 struct s3c2410_uartcfg *cfg)
80{
81 unsigned long ucon = rd_regl(port, S3C2410_UCON);
82
83 dbg("s3c6400_serial_resetport: port=%p (%08lx), cfg=%p\n",
84 port, port->mapbase, cfg);
85
86 /* ensure we don't change the clock settings... */
87
88 ucon &= S3C6400_UCON_CLKMASK;
89
90 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
91 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
92
93 /* reset both fifos */
94
95 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
96 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
97
98 return 0;
99}
100
101static struct s3c24xx_uart_info s3c6400_uart_inf = {
102 .name = "Samsung S3C6400 UART",
103 .type = PORT_S3C6400,
104 .fifosize = 64,
105 .has_divslot = 1,
106 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
107 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
108 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
109 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
110 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
111 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
112 .get_clksrc = s3c6400_serial_getsource,
113 .set_clksrc = s3c6400_serial_setsource,
114 .reset_port = s3c6400_serial_resetport,
115};
116
117/* device management */
118
119static int s3c6400_serial_probe(struct platform_device *dev)
120{
121 dbg("s3c6400_serial_probe: dev=%p\n", dev);
122 return s3c24xx_serial_probe(dev, &s3c6400_uart_inf);
123}
124
125static struct platform_driver s3c6400_serial_driver = {
126 .probe = s3c6400_serial_probe,
127 .remove = __devexit_p(s3c24xx_serial_remove),
128 .driver = {
129 .name = "s3c6400-uart",
130 .owner = THIS_MODULE,
131 },
132};
133
134s3c24xx_console_init(&s3c6400_serial_driver, &s3c6400_uart_inf);
135
136static int __init s3c6400_serial_init(void)
137{
138 return s3c24xx_serial_init(&s3c6400_serial_driver, &s3c6400_uart_inf);
139}
140
141static void __exit s3c6400_serial_exit(void)
142{
143 platform_driver_unregister(&s3c6400_serial_driver);
144}
145
146module_init(s3c6400_serial_init);
147module_exit(s3c6400_serial_exit);
148
149MODULE_DESCRIPTION("Samsung S3C6400,S3C6410 SoC Serial port driver");
150MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
151MODULE_LICENSE("GPL v2");
152MODULE_ALIAS("platform:s3c6400-uart");
diff --git a/drivers/tty/serial/s5pv210.c b/drivers/tty/serial/s5pv210.c
new file mode 100644
index 000000000000..6ebccd70a707
--- /dev/null
+++ b/drivers/tty/serial/s5pv210.c
@@ -0,0 +1,162 @@
1/* linux/drivers/serial/s5pv210.c
2 *
3 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
5 *
6 * Based on drivers/serial/s3c6400.c
7 *
8 * Driver for Samsung S5PV210 SoC UARTs.
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 version 2 as
12 * published by the Free Software Foundation.
13*/
14
15#include <linux/module.h>
16#include <linux/ioport.h>
17#include <linux/io.h>
18#include <linux/platform_device.h>
19#include <linux/init.h>
20#include <linux/serial_core.h>
21#include <linux/serial.h>
22
23#include <asm/irq.h>
24#include <mach/hardware.h>
25#include <plat/regs-serial.h>
26#include "samsung.h"
27
28static int s5pv210_serial_setsource(struct uart_port *port,
29 struct s3c24xx_uart_clksrc *clk)
30{
31 struct s3c2410_uartcfg *cfg = port->dev->platform_data;
32 unsigned long ucon = rd_regl(port, S3C2410_UCON);
33
34 if ((cfg->clocks_size) == 1)
35 return 0;
36
37 if (strcmp(clk->name, "pclk") == 0)
38 ucon &= ~S5PV210_UCON_CLKMASK;
39 else if (strcmp(clk->name, "uclk1") == 0)
40 ucon |= S5PV210_UCON_CLKMASK;
41 else {
42 printk(KERN_ERR "unknown clock source %s\n", clk->name);
43 return -EINVAL;
44 }
45
46 wr_regl(port, S3C2410_UCON, ucon);
47 return 0;
48}
49
50
51static int s5pv210_serial_getsource(struct uart_port *port,
52 struct s3c24xx_uart_clksrc *clk)
53{
54 struct s3c2410_uartcfg *cfg = port->dev->platform_data;
55 u32 ucon = rd_regl(port, S3C2410_UCON);
56
57 clk->divisor = 1;
58
59 if ((cfg->clocks_size) == 1)
60 return 0;
61
62 switch (ucon & S5PV210_UCON_CLKMASK) {
63 case S5PV210_UCON_PCLK:
64 clk->name = "pclk";
65 break;
66 case S5PV210_UCON_UCLK:
67 clk->name = "uclk1";
68 break;
69 }
70
71 return 0;
72}
73
74static int s5pv210_serial_resetport(struct uart_port *port,
75 struct s3c2410_uartcfg *cfg)
76{
77 unsigned long ucon = rd_regl(port, S3C2410_UCON);
78
79 ucon &= S5PV210_UCON_CLKMASK;
80 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
81 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
82
83 /* reset both fifos */
84 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
85 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
86
87 return 0;
88}
89
90#define S5PV210_UART_DEFAULT_INFO(fifo_size) \
91 .name = "Samsung S5PV210 UART0", \
92 .type = PORT_S3C6400, \
93 .fifosize = fifo_size, \
94 .has_divslot = 1, \
95 .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
96 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
97 .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
98 .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
99 .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
100 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
101 .get_clksrc = s5pv210_serial_getsource, \
102 .set_clksrc = s5pv210_serial_setsource, \
103 .reset_port = s5pv210_serial_resetport
104
105static struct s3c24xx_uart_info s5p_port_fifo256 = {
106 S5PV210_UART_DEFAULT_INFO(256),
107};
108
109static struct s3c24xx_uart_info s5p_port_fifo64 = {
110 S5PV210_UART_DEFAULT_INFO(64),
111};
112
113static struct s3c24xx_uart_info s5p_port_fifo16 = {
114 S5PV210_UART_DEFAULT_INFO(16),
115};
116
117static struct s3c24xx_uart_info *s5p_uart_inf[] = {
118 [0] = &s5p_port_fifo256,
119 [1] = &s5p_port_fifo64,
120 [2] = &s5p_port_fifo16,
121 [3] = &s5p_port_fifo16,
122};
123
124/* device management */
125static int s5p_serial_probe(struct platform_device *pdev)
126{
127 return s3c24xx_serial_probe(pdev, s5p_uart_inf[pdev->id]);
128}
129
130static struct platform_driver s5p_serial_driver = {
131 .probe = s5p_serial_probe,
132 .remove = __devexit_p(s3c24xx_serial_remove),
133 .driver = {
134 .name = "s5pv210-uart",
135 .owner = THIS_MODULE,
136 },
137};
138
139static int __init s5pv210_serial_console_init(void)
140{
141 return s3c24xx_serial_initconsole(&s5p_serial_driver, s5p_uart_inf);
142}
143
144console_initcall(s5pv210_serial_console_init);
145
146static int __init s5p_serial_init(void)
147{
148 return s3c24xx_serial_init(&s5p_serial_driver, *s5p_uart_inf);
149}
150
151static void __exit s5p_serial_exit(void)
152{
153 platform_driver_unregister(&s5p_serial_driver);
154}
155
156module_init(s5p_serial_init);
157module_exit(s5p_serial_exit);
158
159MODULE_LICENSE("GPL");
160MODULE_ALIAS("platform:s5pv210-uart");
161MODULE_DESCRIPTION("Samsung S5PV210 UART Driver support");
162MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
diff --git a/drivers/tty/serial/sa1100.c b/drivers/tty/serial/sa1100.c
new file mode 100644
index 000000000000..2199d819a987
--- /dev/null
+++ b/drivers/tty/serial/sa1100.c
@@ -0,0 +1,918 @@
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
25#if defined(CONFIG_SERIAL_SA1100_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
26#define SUPPORT_SYSRQ
27#endif
28
29#include <linux/module.h>
30#include <linux/ioport.h>
31#include <linux/init.h>
32#include <linux/console.h>
33#include <linux/sysrq.h>
34#include <linux/platform_device.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/serial_core.h>
38#include <linux/serial.h>
39#include <linux/io.h>
40
41#include <asm/irq.h>
42#include <mach/hardware.h>
43#include <asm/mach/serial_sa1100.h>
44
45/* We've been assigned a range on the "Low-density serial ports" major */
46#define SERIAL_SA1100_MAJOR 204
47#define MINOR_START 5
48
49#define NR_PORTS 3
50
51#define SA1100_ISR_PASS_LIMIT 256
52
53/*
54 * Convert from ignore_status_mask or read_status_mask to UTSR[01]
55 */
56#define SM_TO_UTSR0(x) ((x) & 0xff)
57#define SM_TO_UTSR1(x) ((x) >> 8)
58#define UTSR0_TO_SM(x) ((x))
59#define UTSR1_TO_SM(x) ((x) << 8)
60
61#define UART_GET_UTCR0(sport) __raw_readl((sport)->port.membase + UTCR0)
62#define UART_GET_UTCR1(sport) __raw_readl((sport)->port.membase + UTCR1)
63#define UART_GET_UTCR2(sport) __raw_readl((sport)->port.membase + UTCR2)
64#define UART_GET_UTCR3(sport) __raw_readl((sport)->port.membase + UTCR3)
65#define UART_GET_UTSR0(sport) __raw_readl((sport)->port.membase + UTSR0)
66#define UART_GET_UTSR1(sport) __raw_readl((sport)->port.membase + UTSR1)
67#define UART_GET_CHAR(sport) __raw_readl((sport)->port.membase + UTDR)
68
69#define UART_PUT_UTCR0(sport,v) __raw_writel((v),(sport)->port.membase + UTCR0)
70#define UART_PUT_UTCR1(sport,v) __raw_writel((v),(sport)->port.membase + UTCR1)
71#define UART_PUT_UTCR2(sport,v) __raw_writel((v),(sport)->port.membase + UTCR2)
72#define UART_PUT_UTCR3(sport,v) __raw_writel((v),(sport)->port.membase + UTCR3)
73#define UART_PUT_UTSR0(sport,v) __raw_writel((v),(sport)->port.membase + UTSR0)
74#define UART_PUT_UTSR1(sport,v) __raw_writel((v),(sport)->port.membase + UTSR1)
75#define UART_PUT_CHAR(sport,v) __raw_writel((v),(sport)->port.membase + UTDR)
76
77/*
78 * This is the size of our serial port register set.
79 */
80#define UART_PORT_SIZE 0x24
81
82/*
83 * This determines how often we check the modem status signals
84 * for any change. They generally aren't connected to an IRQ
85 * so we have to poll them. We also check immediately before
86 * filling the TX fifo incase CTS has been dropped.
87 */
88#define MCTRL_TIMEOUT (250*HZ/1000)
89
90struct sa1100_port {
91 struct uart_port port;
92 struct timer_list timer;
93 unsigned int old_status;
94};
95
96/*
97 * Handle any change of modem status signal since we were last called.
98 */
99static void sa1100_mctrl_check(struct sa1100_port *sport)
100{
101 unsigned int status, changed;
102
103 status = sport->port.ops->get_mctrl(&sport->port);
104 changed = status ^ sport->old_status;
105
106 if (changed == 0)
107 return;
108
109 sport->old_status = status;
110
111 if (changed & TIOCM_RI)
112 sport->port.icount.rng++;
113 if (changed & TIOCM_DSR)
114 sport->port.icount.dsr++;
115 if (changed & TIOCM_CAR)
116 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
117 if (changed & TIOCM_CTS)
118 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
119
120 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
121}
122
123/*
124 * This is our per-port timeout handler, for checking the
125 * modem status signals.
126 */
127static void sa1100_timeout(unsigned long data)
128{
129 struct sa1100_port *sport = (struct sa1100_port *)data;
130 unsigned long flags;
131
132 if (sport->port.state) {
133 spin_lock_irqsave(&sport->port.lock, flags);
134 sa1100_mctrl_check(sport);
135 spin_unlock_irqrestore(&sport->port.lock, flags);
136
137 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
138 }
139}
140
141/*
142 * interrupts disabled on entry
143 */
144static void sa1100_stop_tx(struct uart_port *port)
145{
146 struct sa1100_port *sport = (struct sa1100_port *)port;
147 u32 utcr3;
148
149 utcr3 = UART_GET_UTCR3(sport);
150 UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_TIE);
151 sport->port.read_status_mask &= ~UTSR0_TO_SM(UTSR0_TFS);
152}
153
154/*
155 * port locked and interrupts disabled
156 */
157static void sa1100_start_tx(struct uart_port *port)
158{
159 struct sa1100_port *sport = (struct sa1100_port *)port;
160 u32 utcr3;
161
162 utcr3 = UART_GET_UTCR3(sport);
163 sport->port.read_status_mask |= UTSR0_TO_SM(UTSR0_TFS);
164 UART_PUT_UTCR3(sport, utcr3 | UTCR3_TIE);
165}
166
167/*
168 * Interrupts enabled
169 */
170static void sa1100_stop_rx(struct uart_port *port)
171{
172 struct sa1100_port *sport = (struct sa1100_port *)port;
173 u32 utcr3;
174
175 utcr3 = UART_GET_UTCR3(sport);
176 UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_RIE);
177}
178
179/*
180 * Set the modem control timer to fire immediately.
181 */
182static void sa1100_enable_ms(struct uart_port *port)
183{
184 struct sa1100_port *sport = (struct sa1100_port *)port;
185
186 mod_timer(&sport->timer, jiffies);
187}
188
189static void
190sa1100_rx_chars(struct sa1100_port *sport)
191{
192 struct tty_struct *tty = sport->port.state->port.tty;
193 unsigned int status, ch, flg;
194
195 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
196 UTSR0_TO_SM(UART_GET_UTSR0(sport));
197 while (status & UTSR1_TO_SM(UTSR1_RNE)) {
198 ch = UART_GET_CHAR(sport);
199
200 sport->port.icount.rx++;
201
202 flg = TTY_NORMAL;
203
204 /*
205 * note that the error handling code is
206 * out of the main execution path
207 */
208 if (status & UTSR1_TO_SM(UTSR1_PRE | UTSR1_FRE | UTSR1_ROR)) {
209 if (status & UTSR1_TO_SM(UTSR1_PRE))
210 sport->port.icount.parity++;
211 else if (status & UTSR1_TO_SM(UTSR1_FRE))
212 sport->port.icount.frame++;
213 if (status & UTSR1_TO_SM(UTSR1_ROR))
214 sport->port.icount.overrun++;
215
216 status &= sport->port.read_status_mask;
217
218 if (status & UTSR1_TO_SM(UTSR1_PRE))
219 flg = TTY_PARITY;
220 else if (status & UTSR1_TO_SM(UTSR1_FRE))
221 flg = TTY_FRAME;
222
223#ifdef SUPPORT_SYSRQ
224 sport->port.sysrq = 0;
225#endif
226 }
227
228 if (uart_handle_sysrq_char(&sport->port, ch))
229 goto ignore_char;
230
231 uart_insert_char(&sport->port, status, UTSR1_TO_SM(UTSR1_ROR), ch, flg);
232
233 ignore_char:
234 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
235 UTSR0_TO_SM(UART_GET_UTSR0(sport));
236 }
237 tty_flip_buffer_push(tty);
238}
239
240static void sa1100_tx_chars(struct sa1100_port *sport)
241{
242 struct circ_buf *xmit = &sport->port.state->xmit;
243
244 if (sport->port.x_char) {
245 UART_PUT_CHAR(sport, sport->port.x_char);
246 sport->port.icount.tx++;
247 sport->port.x_char = 0;
248 return;
249 }
250
251 /*
252 * Check the modem control lines before
253 * transmitting anything.
254 */
255 sa1100_mctrl_check(sport);
256
257 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
258 sa1100_stop_tx(&sport->port);
259 return;
260 }
261
262 /*
263 * Tried using FIFO (not checking TNF) for fifo fill:
264 * still had the '4 bytes repeated' problem.
265 */
266 while (UART_GET_UTSR1(sport) & UTSR1_TNF) {
267 UART_PUT_CHAR(sport, xmit->buf[xmit->tail]);
268 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
269 sport->port.icount.tx++;
270 if (uart_circ_empty(xmit))
271 break;
272 }
273
274 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
275 uart_write_wakeup(&sport->port);
276
277 if (uart_circ_empty(xmit))
278 sa1100_stop_tx(&sport->port);
279}
280
281static irqreturn_t sa1100_int(int irq, void *dev_id)
282{
283 struct sa1100_port *sport = dev_id;
284 unsigned int status, pass_counter = 0;
285
286 spin_lock(&sport->port.lock);
287 status = UART_GET_UTSR0(sport);
288 status &= SM_TO_UTSR0(sport->port.read_status_mask) | ~UTSR0_TFS;
289 do {
290 if (status & (UTSR0_RFS | UTSR0_RID)) {
291 /* Clear the receiver idle bit, if set */
292 if (status & UTSR0_RID)
293 UART_PUT_UTSR0(sport, UTSR0_RID);
294 sa1100_rx_chars(sport);
295 }
296
297 /* Clear the relevant break bits */
298 if (status & (UTSR0_RBB | UTSR0_REB))
299 UART_PUT_UTSR0(sport, status & (UTSR0_RBB | UTSR0_REB));
300
301 if (status & UTSR0_RBB)
302 sport->port.icount.brk++;
303
304 if (status & UTSR0_REB)
305 uart_handle_break(&sport->port);
306
307 if (status & UTSR0_TFS)
308 sa1100_tx_chars(sport);
309 if (pass_counter++ > SA1100_ISR_PASS_LIMIT)
310 break;
311 status = UART_GET_UTSR0(sport);
312 status &= SM_TO_UTSR0(sport->port.read_status_mask) |
313 ~UTSR0_TFS;
314 } while (status & (UTSR0_TFS | UTSR0_RFS | UTSR0_RID));
315 spin_unlock(&sport->port.lock);
316
317 return IRQ_HANDLED;
318}
319
320/*
321 * Return TIOCSER_TEMT when transmitter is not busy.
322 */
323static unsigned int sa1100_tx_empty(struct uart_port *port)
324{
325 struct sa1100_port *sport = (struct sa1100_port *)port;
326
327 return UART_GET_UTSR1(sport) & UTSR1_TBY ? 0 : TIOCSER_TEMT;
328}
329
330static unsigned int sa1100_get_mctrl(struct uart_port *port)
331{
332 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
333}
334
335static void sa1100_set_mctrl(struct uart_port *port, unsigned int mctrl)
336{
337}
338
339/*
340 * Interrupts always disabled.
341 */
342static void sa1100_break_ctl(struct uart_port *port, int break_state)
343{
344 struct sa1100_port *sport = (struct sa1100_port *)port;
345 unsigned long flags;
346 unsigned int utcr3;
347
348 spin_lock_irqsave(&sport->port.lock, flags);
349 utcr3 = UART_GET_UTCR3(sport);
350 if (break_state == -1)
351 utcr3 |= UTCR3_BRK;
352 else
353 utcr3 &= ~UTCR3_BRK;
354 UART_PUT_UTCR3(sport, utcr3);
355 spin_unlock_irqrestore(&sport->port.lock, flags);
356}
357
358static int sa1100_startup(struct uart_port *port)
359{
360 struct sa1100_port *sport = (struct sa1100_port *)port;
361 int retval;
362
363 /*
364 * Allocate the IRQ
365 */
366 retval = request_irq(sport->port.irq, sa1100_int, 0,
367 "sa11x0-uart", sport);
368 if (retval)
369 return retval;
370
371 /*
372 * Finally, clear and enable interrupts
373 */
374 UART_PUT_UTSR0(sport, -1);
375 UART_PUT_UTCR3(sport, UTCR3_RXE | UTCR3_TXE | UTCR3_RIE);
376
377 /*
378 * Enable modem status interrupts
379 */
380 spin_lock_irq(&sport->port.lock);
381 sa1100_enable_ms(&sport->port);
382 spin_unlock_irq(&sport->port.lock);
383
384 return 0;
385}
386
387static void sa1100_shutdown(struct uart_port *port)
388{
389 struct sa1100_port *sport = (struct sa1100_port *)port;
390
391 /*
392 * Stop our timer.
393 */
394 del_timer_sync(&sport->timer);
395
396 /*
397 * Free the interrupt
398 */
399 free_irq(sport->port.irq, sport);
400
401 /*
402 * Disable all interrupts, port and break condition.
403 */
404 UART_PUT_UTCR3(sport, 0);
405}
406
407static void
408sa1100_set_termios(struct uart_port *port, struct ktermios *termios,
409 struct ktermios *old)
410{
411 struct sa1100_port *sport = (struct sa1100_port *)port;
412 unsigned long flags;
413 unsigned int utcr0, old_utcr3, baud, quot;
414 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
415
416 /*
417 * We only support CS7 and CS8.
418 */
419 while ((termios->c_cflag & CSIZE) != CS7 &&
420 (termios->c_cflag & CSIZE) != CS8) {
421 termios->c_cflag &= ~CSIZE;
422 termios->c_cflag |= old_csize;
423 old_csize = CS8;
424 }
425
426 if ((termios->c_cflag & CSIZE) == CS8)
427 utcr0 = UTCR0_DSS;
428 else
429 utcr0 = 0;
430
431 if (termios->c_cflag & CSTOPB)
432 utcr0 |= UTCR0_SBS;
433 if (termios->c_cflag & PARENB) {
434 utcr0 |= UTCR0_PE;
435 if (!(termios->c_cflag & PARODD))
436 utcr0 |= UTCR0_OES;
437 }
438
439 /*
440 * Ask the core to calculate the divisor for us.
441 */
442 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
443 quot = uart_get_divisor(port, baud);
444
445 spin_lock_irqsave(&sport->port.lock, flags);
446
447 sport->port.read_status_mask &= UTSR0_TO_SM(UTSR0_TFS);
448 sport->port.read_status_mask |= UTSR1_TO_SM(UTSR1_ROR);
449 if (termios->c_iflag & INPCK)
450 sport->port.read_status_mask |=
451 UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
452 if (termios->c_iflag & (BRKINT | PARMRK))
453 sport->port.read_status_mask |=
454 UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
455
456 /*
457 * Characters to ignore
458 */
459 sport->port.ignore_status_mask = 0;
460 if (termios->c_iflag & IGNPAR)
461 sport->port.ignore_status_mask |=
462 UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
463 if (termios->c_iflag & IGNBRK) {
464 sport->port.ignore_status_mask |=
465 UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
466 /*
467 * If we're ignoring parity and break indicators,
468 * ignore overruns too (for real raw support).
469 */
470 if (termios->c_iflag & IGNPAR)
471 sport->port.ignore_status_mask |=
472 UTSR1_TO_SM(UTSR1_ROR);
473 }
474
475 del_timer_sync(&sport->timer);
476
477 /*
478 * Update the per-port timeout.
479 */
480 uart_update_timeout(port, termios->c_cflag, baud);
481
482 /*
483 * disable interrupts and drain transmitter
484 */
485 old_utcr3 = UART_GET_UTCR3(sport);
486 UART_PUT_UTCR3(sport, old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE));
487
488 while (UART_GET_UTSR1(sport) & UTSR1_TBY)
489 barrier();
490
491 /* then, disable everything */
492 UART_PUT_UTCR3(sport, 0);
493
494 /* set the parity, stop bits and data size */
495 UART_PUT_UTCR0(sport, utcr0);
496
497 /* set the baud rate */
498 quot -= 1;
499 UART_PUT_UTCR1(sport, ((quot & 0xf00) >> 8));
500 UART_PUT_UTCR2(sport, (quot & 0xff));
501
502 UART_PUT_UTSR0(sport, -1);
503
504 UART_PUT_UTCR3(sport, old_utcr3);
505
506 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
507 sa1100_enable_ms(&sport->port);
508
509 spin_unlock_irqrestore(&sport->port.lock, flags);
510}
511
512static const char *sa1100_type(struct uart_port *port)
513{
514 struct sa1100_port *sport = (struct sa1100_port *)port;
515
516 return sport->port.type == PORT_SA1100 ? "SA1100" : NULL;
517}
518
519/*
520 * Release the memory region(s) being used by 'port'.
521 */
522static void sa1100_release_port(struct uart_port *port)
523{
524 struct sa1100_port *sport = (struct sa1100_port *)port;
525
526 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
527}
528
529/*
530 * Request the memory region(s) being used by 'port'.
531 */
532static int sa1100_request_port(struct uart_port *port)
533{
534 struct sa1100_port *sport = (struct sa1100_port *)port;
535
536 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
537 "sa11x0-uart") != NULL ? 0 : -EBUSY;
538}
539
540/*
541 * Configure/autoconfigure the port.
542 */
543static void sa1100_config_port(struct uart_port *port, int flags)
544{
545 struct sa1100_port *sport = (struct sa1100_port *)port;
546
547 if (flags & UART_CONFIG_TYPE &&
548 sa1100_request_port(&sport->port) == 0)
549 sport->port.type = PORT_SA1100;
550}
551
552/*
553 * Verify the new serial_struct (for TIOCSSERIAL).
554 * The only change we allow are to the flags and type, and
555 * even then only between PORT_SA1100 and PORT_UNKNOWN
556 */
557static int
558sa1100_verify_port(struct uart_port *port, struct serial_struct *ser)
559{
560 struct sa1100_port *sport = (struct sa1100_port *)port;
561 int ret = 0;
562
563 if (ser->type != PORT_UNKNOWN && ser->type != PORT_SA1100)
564 ret = -EINVAL;
565 if (sport->port.irq != ser->irq)
566 ret = -EINVAL;
567 if (ser->io_type != SERIAL_IO_MEM)
568 ret = -EINVAL;
569 if (sport->port.uartclk / 16 != ser->baud_base)
570 ret = -EINVAL;
571 if ((void *)sport->port.mapbase != ser->iomem_base)
572 ret = -EINVAL;
573 if (sport->port.iobase != ser->port)
574 ret = -EINVAL;
575 if (ser->hub6 != 0)
576 ret = -EINVAL;
577 return ret;
578}
579
580static struct uart_ops sa1100_pops = {
581 .tx_empty = sa1100_tx_empty,
582 .set_mctrl = sa1100_set_mctrl,
583 .get_mctrl = sa1100_get_mctrl,
584 .stop_tx = sa1100_stop_tx,
585 .start_tx = sa1100_start_tx,
586 .stop_rx = sa1100_stop_rx,
587 .enable_ms = sa1100_enable_ms,
588 .break_ctl = sa1100_break_ctl,
589 .startup = sa1100_startup,
590 .shutdown = sa1100_shutdown,
591 .set_termios = sa1100_set_termios,
592 .type = sa1100_type,
593 .release_port = sa1100_release_port,
594 .request_port = sa1100_request_port,
595 .config_port = sa1100_config_port,
596 .verify_port = sa1100_verify_port,
597};
598
599static struct sa1100_port sa1100_ports[NR_PORTS];
600
601/*
602 * Setup the SA1100 serial ports. Note that we don't include the IrDA
603 * port here since we have our own SIR/FIR driver (see drivers/net/irda)
604 *
605 * Note also that we support "console=ttySAx" where "x" is either 0 or 1.
606 * Which serial port this ends up being depends on the machine you're
607 * running this kernel on. I'm not convinced that this is a good idea,
608 * but that's the way it traditionally works.
609 *
610 * Note that NanoEngine UART3 becomes UART2, and UART2 is no longer
611 * used here.
612 */
613static void __init sa1100_init_ports(void)
614{
615 static int first = 1;
616 int i;
617
618 if (!first)
619 return;
620 first = 0;
621
622 for (i = 0; i < NR_PORTS; i++) {
623 sa1100_ports[i].port.uartclk = 3686400;
624 sa1100_ports[i].port.ops = &sa1100_pops;
625 sa1100_ports[i].port.fifosize = 8;
626 sa1100_ports[i].port.line = i;
627 sa1100_ports[i].port.iotype = UPIO_MEM;
628 init_timer(&sa1100_ports[i].timer);
629 sa1100_ports[i].timer.function = sa1100_timeout;
630 sa1100_ports[i].timer.data = (unsigned long)&sa1100_ports[i];
631 }
632
633 /*
634 * make transmit lines outputs, so that when the port
635 * is closed, the output is in the MARK state.
636 */
637 PPDR |= PPC_TXD1 | PPC_TXD3;
638 PPSR |= PPC_TXD1 | PPC_TXD3;
639}
640
641void __devinit sa1100_register_uart_fns(struct sa1100_port_fns *fns)
642{
643 if (fns->get_mctrl)
644 sa1100_pops.get_mctrl = fns->get_mctrl;
645 if (fns->set_mctrl)
646 sa1100_pops.set_mctrl = fns->set_mctrl;
647
648 sa1100_pops.pm = fns->pm;
649 sa1100_pops.set_wake = fns->set_wake;
650}
651
652void __init sa1100_register_uart(int idx, int port)
653{
654 if (idx >= NR_PORTS) {
655 printk(KERN_ERR "%s: bad index number %d\n", __func__, idx);
656 return;
657 }
658
659 switch (port) {
660 case 1:
661 sa1100_ports[idx].port.membase = (void __iomem *)&Ser1UTCR0;
662 sa1100_ports[idx].port.mapbase = _Ser1UTCR0;
663 sa1100_ports[idx].port.irq = IRQ_Ser1UART;
664 sa1100_ports[idx].port.flags = UPF_BOOT_AUTOCONF;
665 break;
666
667 case 2:
668 sa1100_ports[idx].port.membase = (void __iomem *)&Ser2UTCR0;
669 sa1100_ports[idx].port.mapbase = _Ser2UTCR0;
670 sa1100_ports[idx].port.irq = IRQ_Ser2ICP;
671 sa1100_ports[idx].port.flags = UPF_BOOT_AUTOCONF;
672 break;
673
674 case 3:
675 sa1100_ports[idx].port.membase = (void __iomem *)&Ser3UTCR0;
676 sa1100_ports[idx].port.mapbase = _Ser3UTCR0;
677 sa1100_ports[idx].port.irq = IRQ_Ser3UART;
678 sa1100_ports[idx].port.flags = UPF_BOOT_AUTOCONF;
679 break;
680
681 default:
682 printk(KERN_ERR "%s: bad port number %d\n", __func__, port);
683 }
684}
685
686
687#ifdef CONFIG_SERIAL_SA1100_CONSOLE
688static void sa1100_console_putchar(struct uart_port *port, int ch)
689{
690 struct sa1100_port *sport = (struct sa1100_port *)port;
691
692 while (!(UART_GET_UTSR1(sport) & UTSR1_TNF))
693 barrier();
694 UART_PUT_CHAR(sport, ch);
695}
696
697/*
698 * Interrupts are disabled on entering
699 */
700static void
701sa1100_console_write(struct console *co, const char *s, unsigned int count)
702{
703 struct sa1100_port *sport = &sa1100_ports[co->index];
704 unsigned int old_utcr3, status;
705
706 /*
707 * First, save UTCR3 and then disable interrupts
708 */
709 old_utcr3 = UART_GET_UTCR3(sport);
710 UART_PUT_UTCR3(sport, (old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE)) |
711 UTCR3_TXE);
712
713 uart_console_write(&sport->port, s, count, sa1100_console_putchar);
714
715 /*
716 * Finally, wait for transmitter to become empty
717 * and restore UTCR3
718 */
719 do {
720 status = UART_GET_UTSR1(sport);
721 } while (status & UTSR1_TBY);
722 UART_PUT_UTCR3(sport, old_utcr3);
723}
724
725/*
726 * If the port was already initialised (eg, by a boot loader),
727 * try to determine the current setup.
728 */
729static void __init
730sa1100_console_get_options(struct sa1100_port *sport, int *baud,
731 int *parity, int *bits)
732{
733 unsigned int utcr3;
734
735 utcr3 = UART_GET_UTCR3(sport) & (UTCR3_RXE | UTCR3_TXE);
736 if (utcr3 == (UTCR3_RXE | UTCR3_TXE)) {
737 /* ok, the port was enabled */
738 unsigned int utcr0, quot;
739
740 utcr0 = UART_GET_UTCR0(sport);
741
742 *parity = 'n';
743 if (utcr0 & UTCR0_PE) {
744 if (utcr0 & UTCR0_OES)
745 *parity = 'e';
746 else
747 *parity = 'o';
748 }
749
750 if (utcr0 & UTCR0_DSS)
751 *bits = 8;
752 else
753 *bits = 7;
754
755 quot = UART_GET_UTCR2(sport) | UART_GET_UTCR1(sport) << 8;
756 quot &= 0xfff;
757 *baud = sport->port.uartclk / (16 * (quot + 1));
758 }
759}
760
761static int __init
762sa1100_console_setup(struct console *co, char *options)
763{
764 struct sa1100_port *sport;
765 int baud = 9600;
766 int bits = 8;
767 int parity = 'n';
768 int flow = 'n';
769
770 /*
771 * Check whether an invalid uart number has been specified, and
772 * if so, search for the first available port that does have
773 * console support.
774 */
775 if (co->index == -1 || co->index >= NR_PORTS)
776 co->index = 0;
777 sport = &sa1100_ports[co->index];
778
779 if (options)
780 uart_parse_options(options, &baud, &parity, &bits, &flow);
781 else
782 sa1100_console_get_options(sport, &baud, &parity, &bits);
783
784 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
785}
786
787static struct uart_driver sa1100_reg;
788static struct console sa1100_console = {
789 .name = "ttySA",
790 .write = sa1100_console_write,
791 .device = uart_console_device,
792 .setup = sa1100_console_setup,
793 .flags = CON_PRINTBUFFER,
794 .index = -1,
795 .data = &sa1100_reg,
796};
797
798static int __init sa1100_rs_console_init(void)
799{
800 sa1100_init_ports();
801 register_console(&sa1100_console);
802 return 0;
803}
804console_initcall(sa1100_rs_console_init);
805
806#define SA1100_CONSOLE &sa1100_console
807#else
808#define SA1100_CONSOLE NULL
809#endif
810
811static struct uart_driver sa1100_reg = {
812 .owner = THIS_MODULE,
813 .driver_name = "ttySA",
814 .dev_name = "ttySA",
815 .major = SERIAL_SA1100_MAJOR,
816 .minor = MINOR_START,
817 .nr = NR_PORTS,
818 .cons = SA1100_CONSOLE,
819};
820
821static int sa1100_serial_suspend(struct platform_device *dev, pm_message_t state)
822{
823 struct sa1100_port *sport = platform_get_drvdata(dev);
824
825 if (sport)
826 uart_suspend_port(&sa1100_reg, &sport->port);
827
828 return 0;
829}
830
831static int sa1100_serial_resume(struct platform_device *dev)
832{
833 struct sa1100_port *sport = platform_get_drvdata(dev);
834
835 if (sport)
836 uart_resume_port(&sa1100_reg, &sport->port);
837
838 return 0;
839}
840
841static int sa1100_serial_probe(struct platform_device *dev)
842{
843 struct resource *res = dev->resource;
844 int i;
845
846 for (i = 0; i < dev->num_resources; i++, res++)
847 if (res->flags & IORESOURCE_MEM)
848 break;
849
850 if (i < dev->num_resources) {
851 for (i = 0; i < NR_PORTS; i++) {
852 if (sa1100_ports[i].port.mapbase != res->start)
853 continue;
854
855 sa1100_ports[i].port.dev = &dev->dev;
856 uart_add_one_port(&sa1100_reg, &sa1100_ports[i].port);
857 platform_set_drvdata(dev, &sa1100_ports[i]);
858 break;
859 }
860 }
861
862 return 0;
863}
864
865static int sa1100_serial_remove(struct platform_device *pdev)
866{
867 struct sa1100_port *sport = platform_get_drvdata(pdev);
868
869 platform_set_drvdata(pdev, NULL);
870
871 if (sport)
872 uart_remove_one_port(&sa1100_reg, &sport->port);
873
874 return 0;
875}
876
877static struct platform_driver sa11x0_serial_driver = {
878 .probe = sa1100_serial_probe,
879 .remove = sa1100_serial_remove,
880 .suspend = sa1100_serial_suspend,
881 .resume = sa1100_serial_resume,
882 .driver = {
883 .name = "sa11x0-uart",
884 .owner = THIS_MODULE,
885 },
886};
887
888static int __init sa1100_serial_init(void)
889{
890 int ret;
891
892 printk(KERN_INFO "Serial: SA11x0 driver\n");
893
894 sa1100_init_ports();
895
896 ret = uart_register_driver(&sa1100_reg);
897 if (ret == 0) {
898 ret = platform_driver_register(&sa11x0_serial_driver);
899 if (ret)
900 uart_unregister_driver(&sa1100_reg);
901 }
902 return ret;
903}
904
905static void __exit sa1100_serial_exit(void)
906{
907 platform_driver_unregister(&sa11x0_serial_driver);
908 uart_unregister_driver(&sa1100_reg);
909}
910
911module_init(sa1100_serial_init);
912module_exit(sa1100_serial_exit);
913
914MODULE_AUTHOR("Deep Blue Solutions Ltd");
915MODULE_DESCRIPTION("SA1100 generic serial port driver");
916MODULE_LICENSE("GPL");
917MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_SA1100_MAJOR);
918MODULE_ALIAS("platform:sa11x0-uart");
diff --git a/drivers/tty/serial/samsung.c b/drivers/tty/serial/samsung.c
new file mode 100644
index 000000000000..7ac2bf5167cd
--- /dev/null
+++ b/drivers/tty/serial/samsung.c
@@ -0,0 +1,1487 @@
1/* linux/drivers/serial/samsuing.c
2 *
3 * Driver core for Samsung SoC onboard UARTs.
4 *
5 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
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
13/* Hote on 2410 error handling
14 *
15 * The s3c2410 manual has a love/hate affair with the contents of the
16 * UERSTAT register in the UART blocks, and keeps marking some of the
17 * error bits as reserved. Having checked with the s3c2410x01,
18 * it copes with BREAKs properly, so I am happy to ignore the RESERVED
19 * feature from the latter versions of the manual.
20 *
21 * If it becomes aparrent that latter versions of the 2410 remove these
22 * bits, then action will have to be taken to differentiate the versions
23 * and change the policy on BREAK
24 *
25 * BJD, 04-Nov-2004
26*/
27
28#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
29#define SUPPORT_SYSRQ
30#endif
31
32#include <linux/module.h>
33#include <linux/ioport.h>
34#include <linux/io.h>
35#include <linux/platform_device.h>
36#include <linux/init.h>
37#include <linux/sysrq.h>
38#include <linux/console.h>
39#include <linux/tty.h>
40#include <linux/tty_flip.h>
41#include <linux/serial_core.h>
42#include <linux/serial.h>
43#include <linux/delay.h>
44#include <linux/clk.h>
45#include <linux/cpufreq.h>
46
47#include <asm/irq.h>
48
49#include <mach/hardware.h>
50#include <mach/map.h>
51
52#include <plat/regs-serial.h>
53
54#include "samsung.h"
55
56/* UART name and device definitions */
57
58#define S3C24XX_SERIAL_NAME "ttySAC"
59#define S3C24XX_SERIAL_MAJOR 204
60#define S3C24XX_SERIAL_MINOR 64
61
62/* macros to change one thing to another */
63
64#define tx_enabled(port) ((port)->unused[0])
65#define rx_enabled(port) ((port)->unused[1])
66
67/* flag to ignore all characters comming in */
68#define RXSTAT_DUMMY_READ (0x10000000)
69
70static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
71{
72 return container_of(port, struct s3c24xx_uart_port, port);
73}
74
75/* translate a port to the device name */
76
77static inline const char *s3c24xx_serial_portname(struct uart_port *port)
78{
79 return to_platform_device(port->dev)->name;
80}
81
82static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
83{
84 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
85}
86
87static void s3c24xx_serial_rx_enable(struct uart_port *port)
88{
89 unsigned long flags;
90 unsigned int ucon, ufcon;
91 int count = 10000;
92
93 spin_lock_irqsave(&port->lock, flags);
94
95 while (--count && !s3c24xx_serial_txempty_nofifo(port))
96 udelay(100);
97
98 ufcon = rd_regl(port, S3C2410_UFCON);
99 ufcon |= S3C2410_UFCON_RESETRX;
100 wr_regl(port, S3C2410_UFCON, ufcon);
101
102 ucon = rd_regl(port, S3C2410_UCON);
103 ucon |= S3C2410_UCON_RXIRQMODE;
104 wr_regl(port, S3C2410_UCON, ucon);
105
106 rx_enabled(port) = 1;
107 spin_unlock_irqrestore(&port->lock, flags);
108}
109
110static void s3c24xx_serial_rx_disable(struct uart_port *port)
111{
112 unsigned long flags;
113 unsigned int ucon;
114
115 spin_lock_irqsave(&port->lock, flags);
116
117 ucon = rd_regl(port, S3C2410_UCON);
118 ucon &= ~S3C2410_UCON_RXIRQMODE;
119 wr_regl(port, S3C2410_UCON, ucon);
120
121 rx_enabled(port) = 0;
122 spin_unlock_irqrestore(&port->lock, flags);
123}
124
125static void s3c24xx_serial_stop_tx(struct uart_port *port)
126{
127 struct s3c24xx_uart_port *ourport = to_ourport(port);
128
129 if (tx_enabled(port)) {
130 disable_irq_nosync(ourport->tx_irq);
131 tx_enabled(port) = 0;
132 if (port->flags & UPF_CONS_FLOW)
133 s3c24xx_serial_rx_enable(port);
134 }
135}
136
137static void s3c24xx_serial_start_tx(struct uart_port *port)
138{
139 struct s3c24xx_uart_port *ourport = to_ourport(port);
140
141 if (!tx_enabled(port)) {
142 if (port->flags & UPF_CONS_FLOW)
143 s3c24xx_serial_rx_disable(port);
144
145 enable_irq(ourport->tx_irq);
146 tx_enabled(port) = 1;
147 }
148}
149
150
151static void s3c24xx_serial_stop_rx(struct uart_port *port)
152{
153 struct s3c24xx_uart_port *ourport = to_ourport(port);
154
155 if (rx_enabled(port)) {
156 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
157 disable_irq_nosync(ourport->rx_irq);
158 rx_enabled(port) = 0;
159 }
160}
161
162static void s3c24xx_serial_enable_ms(struct uart_port *port)
163{
164}
165
166static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
167{
168 return to_ourport(port)->info;
169}
170
171static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
172{
173 if (port->dev == NULL)
174 return NULL;
175
176 return (struct s3c2410_uartcfg *)port->dev->platform_data;
177}
178
179static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
180 unsigned long ufstat)
181{
182 struct s3c24xx_uart_info *info = ourport->info;
183
184 if (ufstat & info->rx_fifofull)
185 return info->fifosize;
186
187 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
188}
189
190
191/* ? - where has parity gone?? */
192#define S3C2410_UERSTAT_PARITY (0x1000)
193
194static irqreturn_t
195s3c24xx_serial_rx_chars(int irq, void *dev_id)
196{
197 struct s3c24xx_uart_port *ourport = dev_id;
198 struct uart_port *port = &ourport->port;
199 struct tty_struct *tty = port->state->port.tty;
200 unsigned int ufcon, ch, flag, ufstat, uerstat;
201 int max_count = 64;
202
203 while (max_count-- > 0) {
204 ufcon = rd_regl(port, S3C2410_UFCON);
205 ufstat = rd_regl(port, S3C2410_UFSTAT);
206
207 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
208 break;
209
210 uerstat = rd_regl(port, S3C2410_UERSTAT);
211 ch = rd_regb(port, S3C2410_URXH);
212
213 if (port->flags & UPF_CONS_FLOW) {
214 int txe = s3c24xx_serial_txempty_nofifo(port);
215
216 if (rx_enabled(port)) {
217 if (!txe) {
218 rx_enabled(port) = 0;
219 continue;
220 }
221 } else {
222 if (txe) {
223 ufcon |= S3C2410_UFCON_RESETRX;
224 wr_regl(port, S3C2410_UFCON, ufcon);
225 rx_enabled(port) = 1;
226 goto out;
227 }
228 continue;
229 }
230 }
231
232 /* insert the character into the buffer */
233
234 flag = TTY_NORMAL;
235 port->icount.rx++;
236
237 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
238 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
239 ch, uerstat);
240
241 /* check for break */
242 if (uerstat & S3C2410_UERSTAT_BREAK) {
243 dbg("break!\n");
244 port->icount.brk++;
245 if (uart_handle_break(port))
246 goto ignore_char;
247 }
248
249 if (uerstat & S3C2410_UERSTAT_FRAME)
250 port->icount.frame++;
251 if (uerstat & S3C2410_UERSTAT_OVERRUN)
252 port->icount.overrun++;
253
254 uerstat &= port->read_status_mask;
255
256 if (uerstat & S3C2410_UERSTAT_BREAK)
257 flag = TTY_BREAK;
258 else if (uerstat & S3C2410_UERSTAT_PARITY)
259 flag = TTY_PARITY;
260 else if (uerstat & (S3C2410_UERSTAT_FRAME |
261 S3C2410_UERSTAT_OVERRUN))
262 flag = TTY_FRAME;
263 }
264
265 if (uart_handle_sysrq_char(port, ch))
266 goto ignore_char;
267
268 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
269 ch, flag);
270
271 ignore_char:
272 continue;
273 }
274 tty_flip_buffer_push(tty);
275
276 out:
277 return IRQ_HANDLED;
278}
279
280static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
281{
282 struct s3c24xx_uart_port *ourport = id;
283 struct uart_port *port = &ourport->port;
284 struct circ_buf *xmit = &port->state->xmit;
285 int count = 256;
286
287 if (port->x_char) {
288 wr_regb(port, S3C2410_UTXH, port->x_char);
289 port->icount.tx++;
290 port->x_char = 0;
291 goto out;
292 }
293
294 /* if there isnt anything more to transmit, or the uart is now
295 * stopped, disable the uart and exit
296 */
297
298 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
299 s3c24xx_serial_stop_tx(port);
300 goto out;
301 }
302
303 /* try and drain the buffer... */
304
305 while (!uart_circ_empty(xmit) && count-- > 0) {
306 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
307 break;
308
309 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
310 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
311 port->icount.tx++;
312 }
313
314 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
315 uart_write_wakeup(port);
316
317 if (uart_circ_empty(xmit))
318 s3c24xx_serial_stop_tx(port);
319
320 out:
321 return IRQ_HANDLED;
322}
323
324static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
325{
326 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
327 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
328 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
329
330 if (ufcon & S3C2410_UFCON_FIFOMODE) {
331 if ((ufstat & info->tx_fifomask) != 0 ||
332 (ufstat & info->tx_fifofull))
333 return 0;
334
335 return 1;
336 }
337
338 return s3c24xx_serial_txempty_nofifo(port);
339}
340
341/* no modem control lines */
342static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
343{
344 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
345
346 if (umstat & S3C2410_UMSTAT_CTS)
347 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
348 else
349 return TIOCM_CAR | TIOCM_DSR;
350}
351
352static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
353{
354 /* todo - possibly remove AFC and do manual CTS */
355}
356
357static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
358{
359 unsigned long flags;
360 unsigned int ucon;
361
362 spin_lock_irqsave(&port->lock, flags);
363
364 ucon = rd_regl(port, S3C2410_UCON);
365
366 if (break_state)
367 ucon |= S3C2410_UCON_SBREAK;
368 else
369 ucon &= ~S3C2410_UCON_SBREAK;
370
371 wr_regl(port, S3C2410_UCON, ucon);
372
373 spin_unlock_irqrestore(&port->lock, flags);
374}
375
376static void s3c24xx_serial_shutdown(struct uart_port *port)
377{
378 struct s3c24xx_uart_port *ourport = to_ourport(port);
379
380 if (ourport->tx_claimed) {
381 free_irq(ourport->tx_irq, ourport);
382 tx_enabled(port) = 0;
383 ourport->tx_claimed = 0;
384 }
385
386 if (ourport->rx_claimed) {
387 free_irq(ourport->rx_irq, ourport);
388 ourport->rx_claimed = 0;
389 rx_enabled(port) = 0;
390 }
391}
392
393
394static int s3c24xx_serial_startup(struct uart_port *port)
395{
396 struct s3c24xx_uart_port *ourport = to_ourport(port);
397 int ret;
398
399 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
400 port->mapbase, port->membase);
401
402 rx_enabled(port) = 1;
403
404 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
405 s3c24xx_serial_portname(port), ourport);
406
407 if (ret != 0) {
408 printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
409 return ret;
410 }
411
412 ourport->rx_claimed = 1;
413
414 dbg("requesting tx irq...\n");
415
416 tx_enabled(port) = 1;
417
418 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
419 s3c24xx_serial_portname(port), ourport);
420
421 if (ret) {
422 printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
423 goto err;
424 }
425
426 ourport->tx_claimed = 1;
427
428 dbg("s3c24xx_serial_startup ok\n");
429
430 /* the port reset code should have done the correct
431 * register setup for the port controls */
432
433 return ret;
434
435 err:
436 s3c24xx_serial_shutdown(port);
437 return ret;
438}
439
440/* power power management control */
441
442static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
443 unsigned int old)
444{
445 struct s3c24xx_uart_port *ourport = to_ourport(port);
446
447 ourport->pm_level = level;
448
449 switch (level) {
450 case 3:
451 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
452 clk_disable(ourport->baudclk);
453
454 clk_disable(ourport->clk);
455 break;
456
457 case 0:
458 clk_enable(ourport->clk);
459
460 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
461 clk_enable(ourport->baudclk);
462
463 break;
464 default:
465 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
466 }
467}
468
469/* baud rate calculation
470 *
471 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
472 * of different sources, including the peripheral clock ("pclk") and an
473 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
474 * with a programmable extra divisor.
475 *
476 * The following code goes through the clock sources, and calculates the
477 * baud clocks (and the resultant actual baud rates) and then tries to
478 * pick the closest one and select that.
479 *
480*/
481
482
483#define MAX_CLKS (8)
484
485static struct s3c24xx_uart_clksrc tmp_clksrc = {
486 .name = "pclk",
487 .min_baud = 0,
488 .max_baud = 0,
489 .divisor = 1,
490};
491
492static inline int
493s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
494{
495 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
496
497 return (info->get_clksrc)(port, c);
498}
499
500static inline int
501s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
502{
503 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
504
505 return (info->set_clksrc)(port, c);
506}
507
508struct baud_calc {
509 struct s3c24xx_uart_clksrc *clksrc;
510 unsigned int calc;
511 unsigned int divslot;
512 unsigned int quot;
513 struct clk *src;
514};
515
516static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
517 struct uart_port *port,
518 struct s3c24xx_uart_clksrc *clksrc,
519 unsigned int baud)
520{
521 struct s3c24xx_uart_port *ourport = to_ourport(port);
522 unsigned long rate;
523
524 calc->src = clk_get(port->dev, clksrc->name);
525 if (calc->src == NULL || IS_ERR(calc->src))
526 return 0;
527
528 rate = clk_get_rate(calc->src);
529 rate /= clksrc->divisor;
530
531 calc->clksrc = clksrc;
532
533 if (ourport->info->has_divslot) {
534 unsigned long div = rate / baud;
535
536 /* The UDIVSLOT register on the newer UARTs allows us to
537 * get a divisor adjustment of 1/16th on the baud clock.
538 *
539 * We don't keep the UDIVSLOT value (the 16ths we calculated
540 * by not multiplying the baud by 16) as it is easy enough
541 * to recalculate.
542 */
543
544 calc->quot = div / 16;
545 calc->calc = rate / div;
546 } else {
547 calc->quot = (rate + (8 * baud)) / (16 * baud);
548 calc->calc = (rate / (calc->quot * 16));
549 }
550
551 calc->quot--;
552 return 1;
553}
554
555static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
556 struct s3c24xx_uart_clksrc **clksrc,
557 struct clk **clk,
558 unsigned int baud)
559{
560 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
561 struct s3c24xx_uart_clksrc *clkp;
562 struct baud_calc res[MAX_CLKS];
563 struct baud_calc *resptr, *best, *sptr;
564 int i;
565
566 clkp = cfg->clocks;
567 best = NULL;
568
569 if (cfg->clocks_size < 2) {
570 if (cfg->clocks_size == 0)
571 clkp = &tmp_clksrc;
572
573 /* check to see if we're sourcing fclk, and if so we're
574 * going to have to update the clock source
575 */
576
577 if (strcmp(clkp->name, "fclk") == 0) {
578 struct s3c24xx_uart_clksrc src;
579
580 s3c24xx_serial_getsource(port, &src);
581
582 /* check that the port already using fclk, and if
583 * not, then re-select fclk
584 */
585
586 if (strcmp(src.name, clkp->name) == 0) {
587 s3c24xx_serial_setsource(port, clkp);
588 s3c24xx_serial_getsource(port, &src);
589 }
590
591 clkp->divisor = src.divisor;
592 }
593
594 s3c24xx_serial_calcbaud(res, port, clkp, baud);
595 best = res;
596 resptr = best + 1;
597 } else {
598 resptr = res;
599
600 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
601 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
602 resptr++;
603 }
604 }
605
606 /* ok, we now need to select the best clock we found */
607
608 if (!best) {
609 unsigned int deviation = (1<<30)|((1<<30)-1);
610 int calc_deviation;
611
612 for (sptr = res; sptr < resptr; sptr++) {
613 calc_deviation = baud - sptr->calc;
614 if (calc_deviation < 0)
615 calc_deviation = -calc_deviation;
616
617 if (calc_deviation < deviation) {
618 best = sptr;
619 deviation = calc_deviation;
620 }
621 }
622 }
623
624 /* store results to pass back */
625
626 *clksrc = best->clksrc;
627 *clk = best->src;
628
629 return best->quot;
630}
631
632/* udivslot_table[]
633 *
634 * This table takes the fractional value of the baud divisor and gives
635 * the recommended setting for the UDIVSLOT register.
636 */
637static u16 udivslot_table[16] = {
638 [0] = 0x0000,
639 [1] = 0x0080,
640 [2] = 0x0808,
641 [3] = 0x0888,
642 [4] = 0x2222,
643 [5] = 0x4924,
644 [6] = 0x4A52,
645 [7] = 0x54AA,
646 [8] = 0x5555,
647 [9] = 0xD555,
648 [10] = 0xD5D5,
649 [11] = 0xDDD5,
650 [12] = 0xDDDD,
651 [13] = 0xDFDD,
652 [14] = 0xDFDF,
653 [15] = 0xFFDF,
654};
655
656static void s3c24xx_serial_set_termios(struct uart_port *port,
657 struct ktermios *termios,
658 struct ktermios *old)
659{
660 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
661 struct s3c24xx_uart_port *ourport = to_ourport(port);
662 struct s3c24xx_uart_clksrc *clksrc = NULL;
663 struct clk *clk = NULL;
664 unsigned long flags;
665 unsigned int baud, quot;
666 unsigned int ulcon;
667 unsigned int umcon;
668 unsigned int udivslot = 0;
669
670 /*
671 * We don't support modem control lines.
672 */
673 termios->c_cflag &= ~(HUPCL | CMSPAR);
674 termios->c_cflag |= CLOCAL;
675
676 /*
677 * Ask the core to calculate the divisor for us.
678 */
679
680 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
681
682 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
683 quot = port->custom_divisor;
684 else
685 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
686
687 /* check to see if we need to change clock source */
688
689 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
690 dbg("selecting clock %p\n", clk);
691 s3c24xx_serial_setsource(port, clksrc);
692
693 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
694 clk_disable(ourport->baudclk);
695 ourport->baudclk = NULL;
696 }
697
698 clk_enable(clk);
699
700 ourport->clksrc = clksrc;
701 ourport->baudclk = clk;
702 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
703 }
704
705 if (ourport->info->has_divslot) {
706 unsigned int div = ourport->baudclk_rate / baud;
707
708 if (cfg->has_fracval) {
709 udivslot = (div & 15);
710 dbg("fracval = %04x\n", udivslot);
711 } else {
712 udivslot = udivslot_table[div & 15];
713 dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
714 }
715 }
716
717 switch (termios->c_cflag & CSIZE) {
718 case CS5:
719 dbg("config: 5bits/char\n");
720 ulcon = S3C2410_LCON_CS5;
721 break;
722 case CS6:
723 dbg("config: 6bits/char\n");
724 ulcon = S3C2410_LCON_CS6;
725 break;
726 case CS7:
727 dbg("config: 7bits/char\n");
728 ulcon = S3C2410_LCON_CS7;
729 break;
730 case CS8:
731 default:
732 dbg("config: 8bits/char\n");
733 ulcon = S3C2410_LCON_CS8;
734 break;
735 }
736
737 /* preserve original lcon IR settings */
738 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
739
740 if (termios->c_cflag & CSTOPB)
741 ulcon |= S3C2410_LCON_STOPB;
742
743 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
744
745 if (termios->c_cflag & PARENB) {
746 if (termios->c_cflag & PARODD)
747 ulcon |= S3C2410_LCON_PODD;
748 else
749 ulcon |= S3C2410_LCON_PEVEN;
750 } else {
751 ulcon |= S3C2410_LCON_PNONE;
752 }
753
754 spin_lock_irqsave(&port->lock, flags);
755
756 dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
757 ulcon, quot, udivslot);
758
759 wr_regl(port, S3C2410_ULCON, ulcon);
760 wr_regl(port, S3C2410_UBRDIV, quot);
761 wr_regl(port, S3C2410_UMCON, umcon);
762
763 if (ourport->info->has_divslot)
764 wr_regl(port, S3C2443_DIVSLOT, udivslot);
765
766 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
767 rd_regl(port, S3C2410_ULCON),
768 rd_regl(port, S3C2410_UCON),
769 rd_regl(port, S3C2410_UFCON));
770
771 /*
772 * Update the per-port timeout.
773 */
774 uart_update_timeout(port, termios->c_cflag, baud);
775
776 /*
777 * Which character status flags are we interested in?
778 */
779 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
780 if (termios->c_iflag & INPCK)
781 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
782
783 /*
784 * Which character status flags should we ignore?
785 */
786 port->ignore_status_mask = 0;
787 if (termios->c_iflag & IGNPAR)
788 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
789 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
790 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
791
792 /*
793 * Ignore all characters if CREAD is not set.
794 */
795 if ((termios->c_cflag & CREAD) == 0)
796 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
797
798 spin_unlock_irqrestore(&port->lock, flags);
799}
800
801static const char *s3c24xx_serial_type(struct uart_port *port)
802{
803 switch (port->type) {
804 case PORT_S3C2410:
805 return "S3C2410";
806 case PORT_S3C2440:
807 return "S3C2440";
808 case PORT_S3C2412:
809 return "S3C2412";
810 case PORT_S3C6400:
811 return "S3C6400/10";
812 default:
813 return NULL;
814 }
815}
816
817#define MAP_SIZE (0x100)
818
819static void s3c24xx_serial_release_port(struct uart_port *port)
820{
821 release_mem_region(port->mapbase, MAP_SIZE);
822}
823
824static int s3c24xx_serial_request_port(struct uart_port *port)
825{
826 const char *name = s3c24xx_serial_portname(port);
827 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
828}
829
830static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
831{
832 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
833
834 if (flags & UART_CONFIG_TYPE &&
835 s3c24xx_serial_request_port(port) == 0)
836 port->type = info->type;
837}
838
839/*
840 * verify the new serial_struct (for TIOCSSERIAL).
841 */
842static int
843s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
844{
845 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
846
847 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
848 return -EINVAL;
849
850 return 0;
851}
852
853
854#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
855
856static struct console s3c24xx_serial_console;
857
858#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
859#else
860#define S3C24XX_SERIAL_CONSOLE NULL
861#endif
862
863static struct uart_ops s3c24xx_serial_ops = {
864 .pm = s3c24xx_serial_pm,
865 .tx_empty = s3c24xx_serial_tx_empty,
866 .get_mctrl = s3c24xx_serial_get_mctrl,
867 .set_mctrl = s3c24xx_serial_set_mctrl,
868 .stop_tx = s3c24xx_serial_stop_tx,
869 .start_tx = s3c24xx_serial_start_tx,
870 .stop_rx = s3c24xx_serial_stop_rx,
871 .enable_ms = s3c24xx_serial_enable_ms,
872 .break_ctl = s3c24xx_serial_break_ctl,
873 .startup = s3c24xx_serial_startup,
874 .shutdown = s3c24xx_serial_shutdown,
875 .set_termios = s3c24xx_serial_set_termios,
876 .type = s3c24xx_serial_type,
877 .release_port = s3c24xx_serial_release_port,
878 .request_port = s3c24xx_serial_request_port,
879 .config_port = s3c24xx_serial_config_port,
880 .verify_port = s3c24xx_serial_verify_port,
881};
882
883
884static struct uart_driver s3c24xx_uart_drv = {
885 .owner = THIS_MODULE,
886 .dev_name = "s3c2410_serial",
887 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
888 .cons = S3C24XX_SERIAL_CONSOLE,
889 .driver_name = S3C24XX_SERIAL_NAME,
890 .major = S3C24XX_SERIAL_MAJOR,
891 .minor = S3C24XX_SERIAL_MINOR,
892};
893
894static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
895 [0] = {
896 .port = {
897 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
898 .iotype = UPIO_MEM,
899 .irq = IRQ_S3CUART_RX0,
900 .uartclk = 0,
901 .fifosize = 16,
902 .ops = &s3c24xx_serial_ops,
903 .flags = UPF_BOOT_AUTOCONF,
904 .line = 0,
905 }
906 },
907 [1] = {
908 .port = {
909 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
910 .iotype = UPIO_MEM,
911 .irq = IRQ_S3CUART_RX1,
912 .uartclk = 0,
913 .fifosize = 16,
914 .ops = &s3c24xx_serial_ops,
915 .flags = UPF_BOOT_AUTOCONF,
916 .line = 1,
917 }
918 },
919#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
920
921 [2] = {
922 .port = {
923 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
924 .iotype = UPIO_MEM,
925 .irq = IRQ_S3CUART_RX2,
926 .uartclk = 0,
927 .fifosize = 16,
928 .ops = &s3c24xx_serial_ops,
929 .flags = UPF_BOOT_AUTOCONF,
930 .line = 2,
931 }
932 },
933#endif
934#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
935 [3] = {
936 .port = {
937 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
938 .iotype = UPIO_MEM,
939 .irq = IRQ_S3CUART_RX3,
940 .uartclk = 0,
941 .fifosize = 16,
942 .ops = &s3c24xx_serial_ops,
943 .flags = UPF_BOOT_AUTOCONF,
944 .line = 3,
945 }
946 }
947#endif
948};
949
950/* s3c24xx_serial_resetport
951 *
952 * wrapper to call the specific reset for this port (reset the fifos
953 * and the settings)
954*/
955
956static inline int s3c24xx_serial_resetport(struct uart_port *port,
957 struct s3c2410_uartcfg *cfg)
958{
959 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
960
961 return (info->reset_port)(port, cfg);
962}
963
964
965#ifdef CONFIG_CPU_FREQ
966
967static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
968 unsigned long val, void *data)
969{
970 struct s3c24xx_uart_port *port;
971 struct uart_port *uport;
972
973 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
974 uport = &port->port;
975
976 /* check to see if port is enabled */
977
978 if (port->pm_level != 0)
979 return 0;
980
981 /* try and work out if the baudrate is changing, we can detect
982 * a change in rate, but we do not have support for detecting
983 * a disturbance in the clock-rate over the change.
984 */
985
986 if (IS_ERR(port->clk))
987 goto exit;
988
989 if (port->baudclk_rate == clk_get_rate(port->clk))
990 goto exit;
991
992 if (val == CPUFREQ_PRECHANGE) {
993 /* we should really shut the port down whilst the
994 * frequency change is in progress. */
995
996 } else if (val == CPUFREQ_POSTCHANGE) {
997 struct ktermios *termios;
998 struct tty_struct *tty;
999
1000 if (uport->state == NULL)
1001 goto exit;
1002
1003 tty = uport->state->port.tty;
1004
1005 if (tty == NULL)
1006 goto exit;
1007
1008 termios = tty->termios;
1009
1010 if (termios == NULL) {
1011 printk(KERN_WARNING "%s: no termios?\n", __func__);
1012 goto exit;
1013 }
1014
1015 s3c24xx_serial_set_termios(uport, termios, NULL);
1016 }
1017
1018 exit:
1019 return 0;
1020}
1021
1022static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1023{
1024 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1025
1026 return cpufreq_register_notifier(&port->freq_transition,
1027 CPUFREQ_TRANSITION_NOTIFIER);
1028}
1029
1030static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1031{
1032 cpufreq_unregister_notifier(&port->freq_transition,
1033 CPUFREQ_TRANSITION_NOTIFIER);
1034}
1035
1036#else
1037static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1038{
1039 return 0;
1040}
1041
1042static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1043{
1044}
1045#endif
1046
1047/* s3c24xx_serial_init_port
1048 *
1049 * initialise a single serial port from the platform device given
1050 */
1051
1052static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1053 struct s3c24xx_uart_info *info,
1054 struct platform_device *platdev)
1055{
1056 struct uart_port *port = &ourport->port;
1057 struct s3c2410_uartcfg *cfg;
1058 struct resource *res;
1059 int ret;
1060
1061 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1062
1063 if (platdev == NULL)
1064 return -ENODEV;
1065
1066 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1067
1068 if (port->mapbase != 0)
1069 return 0;
1070
1071 if (cfg->hwport > CONFIG_SERIAL_SAMSUNG_UARTS) {
1072 printk(KERN_ERR "%s: port %d bigger than %d\n", __func__,
1073 cfg->hwport, CONFIG_SERIAL_SAMSUNG_UARTS);
1074 return -ERANGE;
1075 }
1076
1077 /* setup info for port */
1078 port->dev = &platdev->dev;
1079 ourport->info = info;
1080
1081 /* copy the info in from provided structure */
1082 ourport->port.fifosize = info->fifosize;
1083
1084 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1085
1086 port->uartclk = 1;
1087
1088 if (cfg->uart_flags & UPF_CONS_FLOW) {
1089 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1090 port->flags |= UPF_CONS_FLOW;
1091 }
1092
1093 /* sort our the physical and virtual addresses for each UART */
1094
1095 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1096 if (res == NULL) {
1097 printk(KERN_ERR "failed to find memory resource for uart\n");
1098 return -EINVAL;
1099 }
1100
1101 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1102
1103 port->mapbase = res->start;
1104 port->membase = S3C_VA_UART + (res->start & 0xfffff);
1105 ret = platform_get_irq(platdev, 0);
1106 if (ret < 0)
1107 port->irq = 0;
1108 else {
1109 port->irq = ret;
1110 ourport->rx_irq = ret;
1111 ourport->tx_irq = ret + 1;
1112 }
1113
1114 ret = platform_get_irq(platdev, 1);
1115 if (ret > 0)
1116 ourport->tx_irq = ret;
1117
1118 ourport->clk = clk_get(&platdev->dev, "uart");
1119
1120 dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
1121 port->mapbase, port->membase, port->irq,
1122 ourport->rx_irq, ourport->tx_irq, port->uartclk);
1123
1124 /* reset the fifos (and setup the uart) */
1125 s3c24xx_serial_resetport(port, cfg);
1126 return 0;
1127}
1128
1129static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1130 struct device_attribute *attr,
1131 char *buf)
1132{
1133 struct uart_port *port = s3c24xx_dev_to_port(dev);
1134 struct s3c24xx_uart_port *ourport = to_ourport(port);
1135
1136 return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
1137}
1138
1139static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
1140
1141/* Device driver serial port probe */
1142
1143static int probe_index;
1144
1145int s3c24xx_serial_probe(struct platform_device *dev,
1146 struct s3c24xx_uart_info *info)
1147{
1148 struct s3c24xx_uart_port *ourport;
1149 int ret;
1150
1151 dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
1152
1153 ourport = &s3c24xx_serial_ports[probe_index];
1154 probe_index++;
1155
1156 dbg("%s: initialising port %p...\n", __func__, ourport);
1157
1158 ret = s3c24xx_serial_init_port(ourport, info, dev);
1159 if (ret < 0)
1160 goto probe_err;
1161
1162 dbg("%s: adding port\n", __func__);
1163 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1164 platform_set_drvdata(dev, &ourport->port);
1165
1166 ret = device_create_file(&dev->dev, &dev_attr_clock_source);
1167 if (ret < 0)
1168 printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
1169
1170 ret = s3c24xx_serial_cpufreq_register(ourport);
1171 if (ret < 0)
1172 dev_err(&dev->dev, "failed to add cpufreq notifier\n");
1173
1174 return 0;
1175
1176 probe_err:
1177 return ret;
1178}
1179
1180EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
1181
1182int __devexit s3c24xx_serial_remove(struct platform_device *dev)
1183{
1184 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1185
1186 if (port) {
1187 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
1188 device_remove_file(&dev->dev, &dev_attr_clock_source);
1189 uart_remove_one_port(&s3c24xx_uart_drv, port);
1190 }
1191
1192 return 0;
1193}
1194
1195EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
1196
1197/* UART power management code */
1198
1199#ifdef CONFIG_PM
1200
1201static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
1202{
1203 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1204
1205 if (port)
1206 uart_suspend_port(&s3c24xx_uart_drv, port);
1207
1208 return 0;
1209}
1210
1211static int s3c24xx_serial_resume(struct platform_device *dev)
1212{
1213 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1214 struct s3c24xx_uart_port *ourport = to_ourport(port);
1215
1216 if (port) {
1217 clk_enable(ourport->clk);
1218 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1219 clk_disable(ourport->clk);
1220
1221 uart_resume_port(&s3c24xx_uart_drv, port);
1222 }
1223
1224 return 0;
1225}
1226#endif
1227
1228int s3c24xx_serial_init(struct platform_driver *drv,
1229 struct s3c24xx_uart_info *info)
1230{
1231 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1232
1233#ifdef CONFIG_PM
1234 drv->suspend = s3c24xx_serial_suspend;
1235 drv->resume = s3c24xx_serial_resume;
1236#endif
1237
1238 return platform_driver_register(drv);
1239}
1240
1241EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
1242
1243/* module initialisation code */
1244
1245static int __init s3c24xx_serial_modinit(void)
1246{
1247 int ret;
1248
1249 ret = uart_register_driver(&s3c24xx_uart_drv);
1250 if (ret < 0) {
1251 printk(KERN_ERR "failed to register UART driver\n");
1252 return -1;
1253 }
1254
1255 return 0;
1256}
1257
1258static void __exit s3c24xx_serial_modexit(void)
1259{
1260 uart_unregister_driver(&s3c24xx_uart_drv);
1261}
1262
1263module_init(s3c24xx_serial_modinit);
1264module_exit(s3c24xx_serial_modexit);
1265
1266/* Console code */
1267
1268#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1269
1270static struct uart_port *cons_uart;
1271
1272static int
1273s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1274{
1275 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1276 unsigned long ufstat, utrstat;
1277
1278 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1279 /* fifo mode - check amount of data in fifo registers... */
1280
1281 ufstat = rd_regl(port, S3C2410_UFSTAT);
1282 return (ufstat & info->tx_fifofull) ? 0 : 1;
1283 }
1284
1285 /* in non-fifo mode, we go and use the tx buffer empty */
1286
1287 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1288 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1289}
1290
1291static void
1292s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1293{
1294 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1295 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1296 barrier();
1297 wr_regb(cons_uart, S3C2410_UTXH, ch);
1298}
1299
1300static void
1301s3c24xx_serial_console_write(struct console *co, const char *s,
1302 unsigned int count)
1303{
1304 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1305}
1306
1307static void __init
1308s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1309 int *parity, int *bits)
1310{
1311 struct s3c24xx_uart_clksrc clksrc;
1312 struct clk *clk;
1313 unsigned int ulcon;
1314 unsigned int ucon;
1315 unsigned int ubrdiv;
1316 unsigned long rate;
1317
1318 ulcon = rd_regl(port, S3C2410_ULCON);
1319 ucon = rd_regl(port, S3C2410_UCON);
1320 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1321
1322 dbg("s3c24xx_serial_get_options: port=%p\n"
1323 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1324 port, ulcon, ucon, ubrdiv);
1325
1326 if ((ucon & 0xf) != 0) {
1327 /* consider the serial port configured if the tx/rx mode set */
1328
1329 switch (ulcon & S3C2410_LCON_CSMASK) {
1330 case S3C2410_LCON_CS5:
1331 *bits = 5;
1332 break;
1333 case S3C2410_LCON_CS6:
1334 *bits = 6;
1335 break;
1336 case S3C2410_LCON_CS7:
1337 *bits = 7;
1338 break;
1339 default:
1340 case S3C2410_LCON_CS8:
1341 *bits = 8;
1342 break;
1343 }
1344
1345 switch (ulcon & S3C2410_LCON_PMASK) {
1346 case S3C2410_LCON_PEVEN:
1347 *parity = 'e';
1348 break;
1349
1350 case S3C2410_LCON_PODD:
1351 *parity = 'o';
1352 break;
1353
1354 case S3C2410_LCON_PNONE:
1355 default:
1356 *parity = 'n';
1357 }
1358
1359 /* now calculate the baud rate */
1360
1361 s3c24xx_serial_getsource(port, &clksrc);
1362
1363 clk = clk_get(port->dev, clksrc.name);
1364 if (!IS_ERR(clk) && clk != NULL)
1365 rate = clk_get_rate(clk) / clksrc.divisor;
1366 else
1367 rate = 1;
1368
1369
1370 *baud = rate / (16 * (ubrdiv + 1));
1371 dbg("calculated baud %d\n", *baud);
1372 }
1373
1374}
1375
1376/* s3c24xx_serial_init_ports
1377 *
1378 * initialise the serial ports from the machine provided initialisation
1379 * data.
1380*/
1381
1382static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info **info)
1383{
1384 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1385 struct platform_device **platdev_ptr;
1386 int i;
1387
1388 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1389
1390 platdev_ptr = s3c24xx_uart_devs;
1391
1392 for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++, ptr++, platdev_ptr++) {
1393 s3c24xx_serial_init_port(ptr, info[i], *platdev_ptr);
1394 }
1395
1396 return 0;
1397}
1398
1399static int __init
1400s3c24xx_serial_console_setup(struct console *co, char *options)
1401{
1402 struct uart_port *port;
1403 int baud = 9600;
1404 int bits = 8;
1405 int parity = 'n';
1406 int flow = 'n';
1407
1408 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1409 co, co->index, options);
1410
1411 /* is this a valid port */
1412
1413 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
1414 co->index = 0;
1415
1416 port = &s3c24xx_serial_ports[co->index].port;
1417
1418 /* is the port configured? */
1419
1420 if (port->mapbase == 0x0) {
1421 co->index = 0;
1422 port = &s3c24xx_serial_ports[co->index].port;
1423 }
1424
1425 cons_uart = port;
1426
1427 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1428
1429 /*
1430 * Check whether an invalid uart number has been specified, and
1431 * if so, search for the first available port that does have
1432 * console support.
1433 */
1434 if (options)
1435 uart_parse_options(options, &baud, &parity, &bits, &flow);
1436 else
1437 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1438
1439 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1440
1441 return uart_set_options(port, co, baud, parity, bits, flow);
1442}
1443
1444/* s3c24xx_serial_initconsole
1445 *
1446 * initialise the console from one of the uart drivers
1447*/
1448
1449static struct console s3c24xx_serial_console = {
1450 .name = S3C24XX_SERIAL_NAME,
1451 .device = uart_console_device,
1452 .flags = CON_PRINTBUFFER,
1453 .index = -1,
1454 .write = s3c24xx_serial_console_write,
1455 .setup = s3c24xx_serial_console_setup
1456};
1457
1458int s3c24xx_serial_initconsole(struct platform_driver *drv,
1459 struct s3c24xx_uart_info **info)
1460
1461{
1462 struct platform_device *dev = s3c24xx_uart_devs[0];
1463
1464 dbg("s3c24xx_serial_initconsole\n");
1465
1466 /* select driver based on the cpu */
1467
1468 if (dev == NULL) {
1469 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1470 return 0;
1471 }
1472
1473 if (strcmp(dev->name, drv->driver.name) != 0)
1474 return 0;
1475
1476 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1477 s3c24xx_serial_init_ports(info);
1478
1479 register_console(&s3c24xx_serial_console);
1480 return 0;
1481}
1482
1483#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1484
1485MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1486MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1487MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/samsung.h b/drivers/tty/serial/samsung.h
new file mode 100644
index 000000000000..0ac06a07d25f
--- /dev/null
+++ b/drivers/tty/serial/samsung.h
@@ -0,0 +1,120 @@
1/* linux/drivers/serial/samsung.h
2 *
3 * Driver for Samsung SoC onboard UARTs.
4 *
5 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
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
13struct s3c24xx_uart_info {
14 char *name;
15 unsigned int type;
16 unsigned int fifosize;
17 unsigned long rx_fifomask;
18 unsigned long rx_fifoshift;
19 unsigned long rx_fifofull;
20 unsigned long tx_fifomask;
21 unsigned long tx_fifoshift;
22 unsigned long tx_fifofull;
23
24 /* uart port features */
25
26 unsigned int has_divslot:1;
27
28 /* clock source control */
29
30 int (*get_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
31 int (*set_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
32
33 /* uart controls */
34 int (*reset_port)(struct uart_port *, struct s3c2410_uartcfg *);
35};
36
37struct s3c24xx_uart_port {
38 unsigned char rx_claimed;
39 unsigned char tx_claimed;
40 unsigned int pm_level;
41 unsigned long baudclk_rate;
42
43 unsigned int rx_irq;
44 unsigned int tx_irq;
45
46 struct s3c24xx_uart_info *info;
47 struct s3c24xx_uart_clksrc *clksrc;
48 struct clk *clk;
49 struct clk *baudclk;
50 struct uart_port port;
51
52#ifdef CONFIG_CPU_FREQ
53 struct notifier_block freq_transition;
54#endif
55};
56
57/* conversion functions */
58
59#define s3c24xx_dev_to_port(__dev) (struct uart_port *)dev_get_drvdata(__dev)
60#define s3c24xx_dev_to_cfg(__dev) (struct s3c2410_uartcfg *)((__dev)->platform_data)
61
62/* register access controls */
63
64#define portaddr(port, reg) ((port)->membase + (reg))
65
66#define rd_regb(port, reg) (__raw_readb(portaddr(port, reg)))
67#define rd_regl(port, reg) (__raw_readl(portaddr(port, reg)))
68
69#define wr_regb(port, reg, val) __raw_writeb(val, portaddr(port, reg))
70#define wr_regl(port, reg, val) __raw_writel(val, portaddr(port, reg))
71
72extern int s3c24xx_serial_probe(struct platform_device *dev,
73 struct s3c24xx_uart_info *uart);
74
75extern int __devexit s3c24xx_serial_remove(struct platform_device *dev);
76
77extern int s3c24xx_serial_initconsole(struct platform_driver *drv,
78 struct s3c24xx_uart_info **uart);
79
80extern int s3c24xx_serial_init(struct platform_driver *drv,
81 struct s3c24xx_uart_info *info);
82
83#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
84
85#define s3c24xx_console_init(__drv, __inf) \
86static int __init s3c_serial_console_init(void) \
87{ \
88 struct s3c24xx_uart_info *uinfo[CONFIG_SERIAL_SAMSUNG_UARTS]; \
89 int i; \
90 \
91 for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++) \
92 uinfo[i] = __inf; \
93 return s3c24xx_serial_initconsole(__drv, uinfo); \
94} \
95 \
96console_initcall(s3c_serial_console_init)
97
98#else
99#define s3c24xx_console_init(drv, inf) extern void no_console(void)
100#endif
101
102#ifdef CONFIG_SERIAL_SAMSUNG_DEBUG
103
104extern void printascii(const char *);
105
106static void dbg(const char *fmt, ...)
107{
108 va_list va;
109 char buff[256];
110
111 va_start(va, fmt);
112 vsprintf(buff, fmt, va);
113 va_end(va);
114
115 printascii(buff);
116}
117
118#else
119#define dbg(x...) do { } while (0)
120#endif
diff --git a/drivers/tty/serial/sb1250-duart.c b/drivers/tty/serial/sb1250-duart.c
new file mode 100644
index 000000000000..a2f2b3254499
--- /dev/null
+++ b/drivers/tty/serial/sb1250-duart.c
@@ -0,0 +1,976 @@
1/*
2 * drivers/serial/sb1250-duart.c
3 *
4 * Support for the asynchronous serial interface (DUART) included
5 * in the BCM1250 and derived System-On-a-Chip (SOC) devices.
6 *
7 * Copyright (c) 2007 Maciej W. Rozycki
8 *
9 * Derived from drivers/char/sb1250_duart.c for which the following
10 * copyright applies:
11 *
12 * Copyright (c) 2000, 2001, 2002, 2003, 2004 Broadcom Corporation
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 * References:
20 *
21 * "BCM1250/BCM1125/BCM1125H User Manual", Broadcom Corporation
22 */
23
24#if defined(CONFIG_SERIAL_SB1250_DUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25#define SUPPORT_SYSRQ
26#endif
27
28#include <linux/compiler.h>
29#include <linux/console.h>
30#include <linux/delay.h>
31#include <linux/errno.h>
32#include <linux/init.h>
33#include <linux/interrupt.h>
34#include <linux/ioport.h>
35#include <linux/kernel.h>
36#include <linux/major.h>
37#include <linux/serial.h>
38#include <linux/serial_core.h>
39#include <linux/spinlock.h>
40#include <linux/sysrq.h>
41#include <linux/tty.h>
42#include <linux/types.h>
43
44#include <asm/atomic.h>
45#include <asm/io.h>
46#include <asm/war.h>
47
48#include <asm/sibyte/sb1250.h>
49#include <asm/sibyte/sb1250_uart.h>
50#include <asm/sibyte/swarm.h>
51
52
53#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
54#include <asm/sibyte/bcm1480_regs.h>
55#include <asm/sibyte/bcm1480_int.h>
56
57#define SBD_CHANREGS(line) A_BCM1480_DUART_CHANREG((line), 0)
58#define SBD_CTRLREGS(line) A_BCM1480_DUART_CTRLREG((line), 0)
59#define SBD_INT(line) (K_BCM1480_INT_UART_0 + (line))
60
61#define DUART_CHANREG_SPACING BCM1480_DUART_CHANREG_SPACING
62
63#define R_DUART_IMRREG(line) R_BCM1480_DUART_IMRREG(line)
64#define R_DUART_INCHREG(line) R_BCM1480_DUART_INCHREG(line)
65#define R_DUART_ISRREG(line) R_BCM1480_DUART_ISRREG(line)
66
67#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
68#include <asm/sibyte/sb1250_regs.h>
69#include <asm/sibyte/sb1250_int.h>
70
71#define SBD_CHANREGS(line) A_DUART_CHANREG((line), 0)
72#define SBD_CTRLREGS(line) A_DUART_CTRLREG(0)
73#define SBD_INT(line) (K_INT_UART_0 + (line))
74
75#else
76#error invalid SB1250 UART configuration
77
78#endif
79
80
81MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
82MODULE_DESCRIPTION("BCM1xxx on-chip DUART serial driver");
83MODULE_LICENSE("GPL");
84
85
86#define DUART_MAX_CHIP 2
87#define DUART_MAX_SIDE 2
88
89/*
90 * Per-port state.
91 */
92struct sbd_port {
93 struct sbd_duart *duart;
94 struct uart_port port;
95 unsigned char __iomem *memctrl;
96 int tx_stopped;
97 int initialised;
98};
99
100/*
101 * Per-DUART state for the shared register space.
102 */
103struct sbd_duart {
104 struct sbd_port sport[2];
105 unsigned long mapctrl;
106 atomic_t map_guard;
107};
108
109#define to_sport(uport) container_of(uport, struct sbd_port, port)
110
111static struct sbd_duart sbd_duarts[DUART_MAX_CHIP];
112
113
114/*
115 * Reading and writing SB1250 DUART registers.
116 *
117 * There are three register spaces: two per-channel ones and
118 * a shared one. We have to define accessors appropriately.
119 * All registers are 64-bit and all but the Baud Rate Clock
120 * registers only define 8 least significant bits. There is
121 * also a workaround to take into account. Raw accessors use
122 * the full register width, but cooked ones truncate it
123 * intentionally so that the rest of the driver does not care.
124 */
125static u64 __read_sbdchn(struct sbd_port *sport, int reg)
126{
127 void __iomem *csr = sport->port.membase + reg;
128
129 return __raw_readq(csr);
130}
131
132static u64 __read_sbdshr(struct sbd_port *sport, int reg)
133{
134 void __iomem *csr = sport->memctrl + reg;
135
136 return __raw_readq(csr);
137}
138
139static void __write_sbdchn(struct sbd_port *sport, int reg, u64 value)
140{
141 void __iomem *csr = sport->port.membase + reg;
142
143 __raw_writeq(value, csr);
144}
145
146static void __write_sbdshr(struct sbd_port *sport, int reg, u64 value)
147{
148 void __iomem *csr = sport->memctrl + reg;
149
150 __raw_writeq(value, csr);
151}
152
153/*
154 * In bug 1956, we get glitches that can mess up uart registers. This
155 * "read-mode-reg after any register access" is an accepted workaround.
156 */
157static void __war_sbd1956(struct sbd_port *sport)
158{
159 __read_sbdchn(sport, R_DUART_MODE_REG_1);
160 __read_sbdchn(sport, R_DUART_MODE_REG_2);
161}
162
163static unsigned char read_sbdchn(struct sbd_port *sport, int reg)
164{
165 unsigned char retval;
166
167 retval = __read_sbdchn(sport, reg);
168 if (SIBYTE_1956_WAR)
169 __war_sbd1956(sport);
170 return retval;
171}
172
173static unsigned char read_sbdshr(struct sbd_port *sport, int reg)
174{
175 unsigned char retval;
176
177 retval = __read_sbdshr(sport, reg);
178 if (SIBYTE_1956_WAR)
179 __war_sbd1956(sport);
180 return retval;
181}
182
183static void write_sbdchn(struct sbd_port *sport, int reg, unsigned int value)
184{
185 __write_sbdchn(sport, reg, value);
186 if (SIBYTE_1956_WAR)
187 __war_sbd1956(sport);
188}
189
190static void write_sbdshr(struct sbd_port *sport, int reg, unsigned int value)
191{
192 __write_sbdshr(sport, reg, value);
193 if (SIBYTE_1956_WAR)
194 __war_sbd1956(sport);
195}
196
197
198static int sbd_receive_ready(struct sbd_port *sport)
199{
200 return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_RX_RDY;
201}
202
203static int sbd_receive_drain(struct sbd_port *sport)
204{
205 int loops = 10000;
206
207 while (sbd_receive_ready(sport) && --loops)
208 read_sbdchn(sport, R_DUART_RX_HOLD);
209 return loops;
210}
211
212static int __maybe_unused sbd_transmit_ready(struct sbd_port *sport)
213{
214 return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_TX_RDY;
215}
216
217static int __maybe_unused sbd_transmit_drain(struct sbd_port *sport)
218{
219 int loops = 10000;
220
221 while (!sbd_transmit_ready(sport) && --loops)
222 udelay(2);
223 return loops;
224}
225
226static int sbd_transmit_empty(struct sbd_port *sport)
227{
228 return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_TX_EMT;
229}
230
231static int sbd_line_drain(struct sbd_port *sport)
232{
233 int loops = 10000;
234
235 while (!sbd_transmit_empty(sport) && --loops)
236 udelay(2);
237 return loops;
238}
239
240
241static unsigned int sbd_tx_empty(struct uart_port *uport)
242{
243 struct sbd_port *sport = to_sport(uport);
244
245 return sbd_transmit_empty(sport) ? TIOCSER_TEMT : 0;
246}
247
248static unsigned int sbd_get_mctrl(struct uart_port *uport)
249{
250 struct sbd_port *sport = to_sport(uport);
251 unsigned int mctrl, status;
252
253 status = read_sbdshr(sport, R_DUART_IN_PORT);
254 status >>= (uport->line) % 2;
255 mctrl = (!(status & M_DUART_IN_PIN0_VAL) ? TIOCM_CTS : 0) |
256 (!(status & M_DUART_IN_PIN4_VAL) ? TIOCM_CAR : 0) |
257 (!(status & M_DUART_RIN0_PIN) ? TIOCM_RNG : 0) |
258 (!(status & M_DUART_IN_PIN2_VAL) ? TIOCM_DSR : 0);
259 return mctrl;
260}
261
262static void sbd_set_mctrl(struct uart_port *uport, unsigned int mctrl)
263{
264 struct sbd_port *sport = to_sport(uport);
265 unsigned int clr = 0, set = 0, mode2;
266
267 if (mctrl & TIOCM_DTR)
268 set |= M_DUART_SET_OPR2;
269 else
270 clr |= M_DUART_CLR_OPR2;
271 if (mctrl & TIOCM_RTS)
272 set |= M_DUART_SET_OPR0;
273 else
274 clr |= M_DUART_CLR_OPR0;
275 clr <<= (uport->line) % 2;
276 set <<= (uport->line) % 2;
277
278 mode2 = read_sbdchn(sport, R_DUART_MODE_REG_2);
279 mode2 &= ~M_DUART_CHAN_MODE;
280 if (mctrl & TIOCM_LOOP)
281 mode2 |= V_DUART_CHAN_MODE_LCL_LOOP;
282 else
283 mode2 |= V_DUART_CHAN_MODE_NORMAL;
284
285 write_sbdshr(sport, R_DUART_CLEAR_OPR, clr);
286 write_sbdshr(sport, R_DUART_SET_OPR, set);
287 write_sbdchn(sport, R_DUART_MODE_REG_2, mode2);
288}
289
290static void sbd_stop_tx(struct uart_port *uport)
291{
292 struct sbd_port *sport = to_sport(uport);
293
294 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
295 sport->tx_stopped = 1;
296};
297
298static void sbd_start_tx(struct uart_port *uport)
299{
300 struct sbd_port *sport = to_sport(uport);
301 unsigned int mask;
302
303 /* Enable tx interrupts. */
304 mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
305 mask |= M_DUART_IMR_TX;
306 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
307
308 /* Go!, go!, go!... */
309 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
310 sport->tx_stopped = 0;
311};
312
313static void sbd_stop_rx(struct uart_port *uport)
314{
315 struct sbd_port *sport = to_sport(uport);
316
317 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), 0);
318};
319
320static void sbd_enable_ms(struct uart_port *uport)
321{
322 struct sbd_port *sport = to_sport(uport);
323
324 write_sbdchn(sport, R_DUART_AUXCTL_X,
325 M_DUART_CIN_CHNG_ENA | M_DUART_CTS_CHNG_ENA);
326}
327
328static void sbd_break_ctl(struct uart_port *uport, int break_state)
329{
330 struct sbd_port *sport = to_sport(uport);
331
332 if (break_state == -1)
333 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_START_BREAK);
334 else
335 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_STOP_BREAK);
336}
337
338
339static void sbd_receive_chars(struct sbd_port *sport)
340{
341 struct uart_port *uport = &sport->port;
342 struct uart_icount *icount;
343 unsigned int status, ch, flag;
344 int count;
345
346 for (count = 16; count; count--) {
347 status = read_sbdchn(sport, R_DUART_STATUS);
348 if (!(status & M_DUART_RX_RDY))
349 break;
350
351 ch = read_sbdchn(sport, R_DUART_RX_HOLD);
352
353 flag = TTY_NORMAL;
354
355 icount = &uport->icount;
356 icount->rx++;
357
358 if (unlikely(status &
359 (M_DUART_RCVD_BRK | M_DUART_FRM_ERR |
360 M_DUART_PARITY_ERR | M_DUART_OVRUN_ERR))) {
361 if (status & M_DUART_RCVD_BRK) {
362 icount->brk++;
363 if (uart_handle_break(uport))
364 continue;
365 } else if (status & M_DUART_FRM_ERR)
366 icount->frame++;
367 else if (status & M_DUART_PARITY_ERR)
368 icount->parity++;
369 if (status & M_DUART_OVRUN_ERR)
370 icount->overrun++;
371
372 status &= uport->read_status_mask;
373 if (status & M_DUART_RCVD_BRK)
374 flag = TTY_BREAK;
375 else if (status & M_DUART_FRM_ERR)
376 flag = TTY_FRAME;
377 else if (status & M_DUART_PARITY_ERR)
378 flag = TTY_PARITY;
379 }
380
381 if (uart_handle_sysrq_char(uport, ch))
382 continue;
383
384 uart_insert_char(uport, status, M_DUART_OVRUN_ERR, ch, flag);
385 }
386
387 tty_flip_buffer_push(uport->state->port.tty);
388}
389
390static void sbd_transmit_chars(struct sbd_port *sport)
391{
392 struct uart_port *uport = &sport->port;
393 struct circ_buf *xmit = &sport->port.state->xmit;
394 unsigned int mask;
395 int stop_tx;
396
397 /* XON/XOFF chars. */
398 if (sport->port.x_char) {
399 write_sbdchn(sport, R_DUART_TX_HOLD, sport->port.x_char);
400 sport->port.icount.tx++;
401 sport->port.x_char = 0;
402 return;
403 }
404
405 /* If nothing to do or stopped or hardware stopped. */
406 stop_tx = (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port));
407
408 /* Send char. */
409 if (!stop_tx) {
410 write_sbdchn(sport, R_DUART_TX_HOLD, xmit->buf[xmit->tail]);
411 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
412 sport->port.icount.tx++;
413
414 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
415 uart_write_wakeup(&sport->port);
416 }
417
418 /* Are we are done? */
419 if (stop_tx || uart_circ_empty(xmit)) {
420 /* Disable tx interrupts. */
421 mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
422 mask &= ~M_DUART_IMR_TX;
423 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
424 }
425}
426
427static void sbd_status_handle(struct sbd_port *sport)
428{
429 struct uart_port *uport = &sport->port;
430 unsigned int delta;
431
432 delta = read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
433 delta >>= (uport->line) % 2;
434
435 if (delta & (M_DUART_IN_PIN0_VAL << S_DUART_IN_PIN_CHNG))
436 uart_handle_cts_change(uport, !(delta & M_DUART_IN_PIN0_VAL));
437
438 if (delta & (M_DUART_IN_PIN2_VAL << S_DUART_IN_PIN_CHNG))
439 uport->icount.dsr++;
440
441 if (delta & ((M_DUART_IN_PIN2_VAL | M_DUART_IN_PIN0_VAL) <<
442 S_DUART_IN_PIN_CHNG))
443 wake_up_interruptible(&uport->state->port.delta_msr_wait);
444}
445
446static irqreturn_t sbd_interrupt(int irq, void *dev_id)
447{
448 struct sbd_port *sport = dev_id;
449 struct uart_port *uport = &sport->port;
450 irqreturn_t status = IRQ_NONE;
451 unsigned int intstat;
452 int count;
453
454 for (count = 16; count; count--) {
455 intstat = read_sbdshr(sport,
456 R_DUART_ISRREG((uport->line) % 2));
457 intstat &= read_sbdshr(sport,
458 R_DUART_IMRREG((uport->line) % 2));
459 intstat &= M_DUART_ISR_ALL;
460 if (!intstat)
461 break;
462
463 if (intstat & M_DUART_ISR_RX)
464 sbd_receive_chars(sport);
465 if (intstat & M_DUART_ISR_IN)
466 sbd_status_handle(sport);
467 if (intstat & M_DUART_ISR_TX)
468 sbd_transmit_chars(sport);
469
470 status = IRQ_HANDLED;
471 }
472
473 return status;
474}
475
476
477static int sbd_startup(struct uart_port *uport)
478{
479 struct sbd_port *sport = to_sport(uport);
480 unsigned int mode1;
481 int ret;
482
483 ret = request_irq(sport->port.irq, sbd_interrupt,
484 IRQF_SHARED, "sb1250-duart", sport);
485 if (ret)
486 return ret;
487
488 /* Clear the receive FIFO. */
489 sbd_receive_drain(sport);
490
491 /* Clear the interrupt registers. */
492 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_BREAK_INT);
493 read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
494
495 /* Set rx/tx interrupt to FIFO available. */
496 mode1 = read_sbdchn(sport, R_DUART_MODE_REG_1);
497 mode1 &= ~(M_DUART_RX_IRQ_SEL_RXFULL | M_DUART_TX_IRQ_SEL_TXEMPT);
498 write_sbdchn(sport, R_DUART_MODE_REG_1, mode1);
499
500 /* Disable tx, enable rx. */
501 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_EN);
502 sport->tx_stopped = 1;
503
504 /* Enable interrupts. */
505 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
506 M_DUART_IMR_IN | M_DUART_IMR_RX);
507
508 return 0;
509}
510
511static void sbd_shutdown(struct uart_port *uport)
512{
513 struct sbd_port *sport = to_sport(uport);
514
515 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
516 sport->tx_stopped = 1;
517 free_irq(sport->port.irq, sport);
518}
519
520
521static void sbd_init_port(struct sbd_port *sport)
522{
523 struct uart_port *uport = &sport->port;
524
525 if (sport->initialised)
526 return;
527
528 /* There is no DUART reset feature, so just set some sane defaults. */
529 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_TX);
530 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_RX);
531 write_sbdchn(sport, R_DUART_MODE_REG_1, V_DUART_BITS_PER_CHAR_8);
532 write_sbdchn(sport, R_DUART_MODE_REG_2, 0);
533 write_sbdchn(sport, R_DUART_FULL_CTL,
534 V_DUART_INT_TIME(0) | V_DUART_SIG_FULL(15));
535 write_sbdchn(sport, R_DUART_OPCR_X, 0);
536 write_sbdchn(sport, R_DUART_AUXCTL_X, 0);
537 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), 0);
538
539 sport->initialised = 1;
540}
541
542static void sbd_set_termios(struct uart_port *uport, struct ktermios *termios,
543 struct ktermios *old_termios)
544{
545 struct sbd_port *sport = to_sport(uport);
546 unsigned int mode1 = 0, mode2 = 0, aux = 0;
547 unsigned int mode1mask = 0, mode2mask = 0, auxmask = 0;
548 unsigned int oldmode1, oldmode2, oldaux;
549 unsigned int baud, brg;
550 unsigned int command;
551
552 mode1mask |= ~(M_DUART_PARITY_MODE | M_DUART_PARITY_TYPE_ODD |
553 M_DUART_BITS_PER_CHAR);
554 mode2mask |= ~M_DUART_STOP_BIT_LEN_2;
555 auxmask |= ~M_DUART_CTS_CHNG_ENA;
556
557 /* Byte size. */
558 switch (termios->c_cflag & CSIZE) {
559 case CS5:
560 case CS6:
561 /* Unsupported, leave unchanged. */
562 mode1mask |= M_DUART_PARITY_MODE;
563 break;
564 case CS7:
565 mode1 |= V_DUART_BITS_PER_CHAR_7;
566 break;
567 case CS8:
568 default:
569 mode1 |= V_DUART_BITS_PER_CHAR_8;
570 break;
571 }
572
573 /* Parity and stop bits. */
574 if (termios->c_cflag & CSTOPB)
575 mode2 |= M_DUART_STOP_BIT_LEN_2;
576 else
577 mode2 |= M_DUART_STOP_BIT_LEN_1;
578 if (termios->c_cflag & PARENB)
579 mode1 |= V_DUART_PARITY_MODE_ADD;
580 else
581 mode1 |= V_DUART_PARITY_MODE_NONE;
582 if (termios->c_cflag & PARODD)
583 mode1 |= M_DUART_PARITY_TYPE_ODD;
584 else
585 mode1 |= M_DUART_PARITY_TYPE_EVEN;
586
587 baud = uart_get_baud_rate(uport, termios, old_termios, 1200, 5000000);
588 brg = V_DUART_BAUD_RATE(baud);
589 /* The actual lower bound is 1221bps, so compensate. */
590 if (brg > M_DUART_CLK_COUNTER)
591 brg = M_DUART_CLK_COUNTER;
592
593 uart_update_timeout(uport, termios->c_cflag, baud);
594
595 uport->read_status_mask = M_DUART_OVRUN_ERR;
596 if (termios->c_iflag & INPCK)
597 uport->read_status_mask |= M_DUART_FRM_ERR |
598 M_DUART_PARITY_ERR;
599 if (termios->c_iflag & (BRKINT | PARMRK))
600 uport->read_status_mask |= M_DUART_RCVD_BRK;
601
602 uport->ignore_status_mask = 0;
603 if (termios->c_iflag & IGNPAR)
604 uport->ignore_status_mask |= M_DUART_FRM_ERR |
605 M_DUART_PARITY_ERR;
606 if (termios->c_iflag & IGNBRK) {
607 uport->ignore_status_mask |= M_DUART_RCVD_BRK;
608 if (termios->c_iflag & IGNPAR)
609 uport->ignore_status_mask |= M_DUART_OVRUN_ERR;
610 }
611
612 if (termios->c_cflag & CREAD)
613 command = M_DUART_RX_EN;
614 else
615 command = M_DUART_RX_DIS;
616
617 if (termios->c_cflag & CRTSCTS)
618 aux |= M_DUART_CTS_CHNG_ENA;
619 else
620 aux &= ~M_DUART_CTS_CHNG_ENA;
621
622 spin_lock(&uport->lock);
623
624 if (sport->tx_stopped)
625 command |= M_DUART_TX_DIS;
626 else
627 command |= M_DUART_TX_EN;
628
629 oldmode1 = read_sbdchn(sport, R_DUART_MODE_REG_1) & mode1mask;
630 oldmode2 = read_sbdchn(sport, R_DUART_MODE_REG_2) & mode2mask;
631 oldaux = read_sbdchn(sport, R_DUART_AUXCTL_X) & auxmask;
632
633 if (!sport->tx_stopped)
634 sbd_line_drain(sport);
635 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
636
637 write_sbdchn(sport, R_DUART_MODE_REG_1, mode1 | oldmode1);
638 write_sbdchn(sport, R_DUART_MODE_REG_2, mode2 | oldmode2);
639 write_sbdchn(sport, R_DUART_CLK_SEL, brg);
640 write_sbdchn(sport, R_DUART_AUXCTL_X, aux | oldaux);
641
642 write_sbdchn(sport, R_DUART_CMD, command);
643
644 spin_unlock(&uport->lock);
645}
646
647
648static const char *sbd_type(struct uart_port *uport)
649{
650 return "SB1250 DUART";
651}
652
653static void sbd_release_port(struct uart_port *uport)
654{
655 struct sbd_port *sport = to_sport(uport);
656 struct sbd_duart *duart = sport->duart;
657 int map_guard;
658
659 iounmap(sport->memctrl);
660 sport->memctrl = NULL;
661 iounmap(uport->membase);
662 uport->membase = NULL;
663
664 map_guard = atomic_add_return(-1, &duart->map_guard);
665 if (!map_guard)
666 release_mem_region(duart->mapctrl, DUART_CHANREG_SPACING);
667 release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
668}
669
670static int sbd_map_port(struct uart_port *uport)
671{
672 const char *err = KERN_ERR "sbd: Cannot map MMIO\n";
673 struct sbd_port *sport = to_sport(uport);
674 struct sbd_duart *duart = sport->duart;
675
676 if (!uport->membase)
677 uport->membase = ioremap_nocache(uport->mapbase,
678 DUART_CHANREG_SPACING);
679 if (!uport->membase) {
680 printk(err);
681 return -ENOMEM;
682 }
683
684 if (!sport->memctrl)
685 sport->memctrl = ioremap_nocache(duart->mapctrl,
686 DUART_CHANREG_SPACING);
687 if (!sport->memctrl) {
688 printk(err);
689 iounmap(uport->membase);
690 uport->membase = NULL;
691 return -ENOMEM;
692 }
693
694 return 0;
695}
696
697static int sbd_request_port(struct uart_port *uport)
698{
699 const char *err = KERN_ERR "sbd: Unable to reserve MMIO resource\n";
700 struct sbd_duart *duart = to_sport(uport)->duart;
701 int map_guard;
702 int ret = 0;
703
704 if (!request_mem_region(uport->mapbase, DUART_CHANREG_SPACING,
705 "sb1250-duart")) {
706 printk(err);
707 return -EBUSY;
708 }
709 map_guard = atomic_add_return(1, &duart->map_guard);
710 if (map_guard == 1) {
711 if (!request_mem_region(duart->mapctrl, DUART_CHANREG_SPACING,
712 "sb1250-duart")) {
713 atomic_add(-1, &duart->map_guard);
714 printk(err);
715 ret = -EBUSY;
716 }
717 }
718 if (!ret) {
719 ret = sbd_map_port(uport);
720 if (ret) {
721 map_guard = atomic_add_return(-1, &duart->map_guard);
722 if (!map_guard)
723 release_mem_region(duart->mapctrl,
724 DUART_CHANREG_SPACING);
725 }
726 }
727 if (ret) {
728 release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
729 return ret;
730 }
731 return 0;
732}
733
734static void sbd_config_port(struct uart_port *uport, int flags)
735{
736 struct sbd_port *sport = to_sport(uport);
737
738 if (flags & UART_CONFIG_TYPE) {
739 if (sbd_request_port(uport))
740 return;
741
742 uport->type = PORT_SB1250_DUART;
743
744 sbd_init_port(sport);
745 }
746}
747
748static int sbd_verify_port(struct uart_port *uport, struct serial_struct *ser)
749{
750 int ret = 0;
751
752 if (ser->type != PORT_UNKNOWN && ser->type != PORT_SB1250_DUART)
753 ret = -EINVAL;
754 if (ser->irq != uport->irq)
755 ret = -EINVAL;
756 if (ser->baud_base != uport->uartclk / 16)
757 ret = -EINVAL;
758 return ret;
759}
760
761
762static const struct uart_ops sbd_ops = {
763 .tx_empty = sbd_tx_empty,
764 .set_mctrl = sbd_set_mctrl,
765 .get_mctrl = sbd_get_mctrl,
766 .stop_tx = sbd_stop_tx,
767 .start_tx = sbd_start_tx,
768 .stop_rx = sbd_stop_rx,
769 .enable_ms = sbd_enable_ms,
770 .break_ctl = sbd_break_ctl,
771 .startup = sbd_startup,
772 .shutdown = sbd_shutdown,
773 .set_termios = sbd_set_termios,
774 .type = sbd_type,
775 .release_port = sbd_release_port,
776 .request_port = sbd_request_port,
777 .config_port = sbd_config_port,
778 .verify_port = sbd_verify_port,
779};
780
781/* Initialize SB1250 DUART port structures. */
782static void __init sbd_probe_duarts(void)
783{
784 static int probed;
785 int chip, side;
786 int max_lines, line;
787
788 if (probed)
789 return;
790
791 /* Set the number of available units based on the SOC type. */
792 switch (soc_type) {
793 case K_SYS_SOC_TYPE_BCM1x55:
794 case K_SYS_SOC_TYPE_BCM1x80:
795 max_lines = 4;
796 break;
797 default:
798 /* Assume at least two serial ports at the normal address. */
799 max_lines = 2;
800 break;
801 }
802
803 probed = 1;
804
805 for (chip = 0, line = 0; chip < DUART_MAX_CHIP && line < max_lines;
806 chip++) {
807 sbd_duarts[chip].mapctrl = SBD_CTRLREGS(line);
808
809 for (side = 0; side < DUART_MAX_SIDE && line < max_lines;
810 side++, line++) {
811 struct sbd_port *sport = &sbd_duarts[chip].sport[side];
812 struct uart_port *uport = &sport->port;
813
814 sport->duart = &sbd_duarts[chip];
815
816 uport->irq = SBD_INT(line);
817 uport->uartclk = 100000000 / 20 * 16;
818 uport->fifosize = 16;
819 uport->iotype = UPIO_MEM;
820 uport->flags = UPF_BOOT_AUTOCONF;
821 uport->ops = &sbd_ops;
822 uport->line = line;
823 uport->mapbase = SBD_CHANREGS(line);
824 }
825 }
826}
827
828
829#ifdef CONFIG_SERIAL_SB1250_DUART_CONSOLE
830/*
831 * Serial console stuff. Very basic, polling driver for doing serial
832 * console output. The console_sem is held by the caller, so we
833 * shouldn't be interrupted for more console activity.
834 */
835static void sbd_console_putchar(struct uart_port *uport, int ch)
836{
837 struct sbd_port *sport = to_sport(uport);
838
839 sbd_transmit_drain(sport);
840 write_sbdchn(sport, R_DUART_TX_HOLD, ch);
841}
842
843static void sbd_console_write(struct console *co, const char *s,
844 unsigned int count)
845{
846 int chip = co->index / DUART_MAX_SIDE;
847 int side = co->index % DUART_MAX_SIDE;
848 struct sbd_port *sport = &sbd_duarts[chip].sport[side];
849 struct uart_port *uport = &sport->port;
850 unsigned long flags;
851 unsigned int mask;
852
853 /* Disable transmit interrupts and enable the transmitter. */
854 spin_lock_irqsave(&uport->lock, flags);
855 mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
856 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
857 mask & ~M_DUART_IMR_TX);
858 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
859 spin_unlock_irqrestore(&uport->lock, flags);
860
861 uart_console_write(&sport->port, s, count, sbd_console_putchar);
862
863 /* Restore transmit interrupts and the transmitter enable. */
864 spin_lock_irqsave(&uport->lock, flags);
865 sbd_line_drain(sport);
866 if (sport->tx_stopped)
867 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
868 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
869 spin_unlock_irqrestore(&uport->lock, flags);
870}
871
872static int __init sbd_console_setup(struct console *co, char *options)
873{
874 int chip = co->index / DUART_MAX_SIDE;
875 int side = co->index % DUART_MAX_SIDE;
876 struct sbd_port *sport = &sbd_duarts[chip].sport[side];
877 struct uart_port *uport = &sport->port;
878 int baud = 115200;
879 int bits = 8;
880 int parity = 'n';
881 int flow = 'n';
882 int ret;
883
884 if (!sport->duart)
885 return -ENXIO;
886
887 ret = sbd_map_port(uport);
888 if (ret)
889 return ret;
890
891 sbd_init_port(sport);
892
893 if (options)
894 uart_parse_options(options, &baud, &parity, &bits, &flow);
895 return uart_set_options(uport, co, baud, parity, bits, flow);
896}
897
898static struct uart_driver sbd_reg;
899static struct console sbd_console = {
900 .name = "duart",
901 .write = sbd_console_write,
902 .device = uart_console_device,
903 .setup = sbd_console_setup,
904 .flags = CON_PRINTBUFFER,
905 .index = -1,
906 .data = &sbd_reg
907};
908
909static int __init sbd_serial_console_init(void)
910{
911 sbd_probe_duarts();
912 register_console(&sbd_console);
913
914 return 0;
915}
916
917console_initcall(sbd_serial_console_init);
918
919#define SERIAL_SB1250_DUART_CONSOLE &sbd_console
920#else
921#define SERIAL_SB1250_DUART_CONSOLE NULL
922#endif /* CONFIG_SERIAL_SB1250_DUART_CONSOLE */
923
924
925static struct uart_driver sbd_reg = {
926 .owner = THIS_MODULE,
927 .driver_name = "sb1250_duart",
928 .dev_name = "duart",
929 .major = TTY_MAJOR,
930 .minor = SB1250_DUART_MINOR_BASE,
931 .nr = DUART_MAX_CHIP * DUART_MAX_SIDE,
932 .cons = SERIAL_SB1250_DUART_CONSOLE,
933};
934
935/* Set up the driver and register it. */
936static int __init sbd_init(void)
937{
938 int i, ret;
939
940 sbd_probe_duarts();
941
942 ret = uart_register_driver(&sbd_reg);
943 if (ret)
944 return ret;
945
946 for (i = 0; i < DUART_MAX_CHIP * DUART_MAX_SIDE; i++) {
947 struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
948 struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
949 struct uart_port *uport = &sport->port;
950
951 if (sport->duart)
952 uart_add_one_port(&sbd_reg, uport);
953 }
954
955 return 0;
956}
957
958/* Unload the driver. Unregister stuff, get ready to go away. */
959static void __exit sbd_exit(void)
960{
961 int i;
962
963 for (i = DUART_MAX_CHIP * DUART_MAX_SIDE - 1; i >= 0; i--) {
964 struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
965 struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
966 struct uart_port *uport = &sport->port;
967
968 if (sport->duart)
969 uart_remove_one_port(&sbd_reg, uport);
970 }
971
972 uart_unregister_driver(&sbd_reg);
973}
974
975module_init(sbd_init);
976module_exit(sbd_exit);
diff --git a/drivers/tty/serial/sc26xx.c b/drivers/tty/serial/sc26xx.c
new file mode 100644
index 000000000000..75038ad2b242
--- /dev/null
+++ b/drivers/tty/serial/sc26xx.c
@@ -0,0 +1,757 @@
1/*
2 * SC268xx.c: Serial driver for Philiphs SC2681/SC2692 devices.
3 *
4 * Copyright (C) 2006,2007 Thomas Bogendörfer (tsbogend@alpha.franken.de)
5 */
6
7#include <linux/module.h>
8#include <linux/kernel.h>
9#include <linux/errno.h>
10#include <linux/tty.h>
11#include <linux/tty_flip.h>
12#include <linux/major.h>
13#include <linux/circ_buf.h>
14#include <linux/serial.h>
15#include <linux/sysrq.h>
16#include <linux/console.h>
17#include <linux/spinlock.h>
18#include <linux/slab.h>
19#include <linux/delay.h>
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/irq.h>
23
24#if defined(CONFIG_MAGIC_SYSRQ)
25#define SUPPORT_SYSRQ
26#endif
27
28#include <linux/serial_core.h>
29
30#define SC26XX_MAJOR 204
31#define SC26XX_MINOR_START 205
32#define SC26XX_NR 2
33
34struct uart_sc26xx_port {
35 struct uart_port port[2];
36 u8 dsr_mask[2];
37 u8 cts_mask[2];
38 u8 dcd_mask[2];
39 u8 ri_mask[2];
40 u8 dtr_mask[2];
41 u8 rts_mask[2];
42 u8 imr;
43};
44
45/* register common to both ports */
46#define RD_ISR 0x14
47#define RD_IPR 0x34
48
49#define WR_ACR 0x10
50#define WR_IMR 0x14
51#define WR_OPCR 0x34
52#define WR_OPR_SET 0x38
53#define WR_OPR_CLR 0x3C
54
55/* access common register */
56#define READ_SC(p, r) readb((p)->membase + RD_##r)
57#define WRITE_SC(p, r, v) writeb((v), (p)->membase + WR_##r)
58
59/* register per port */
60#define RD_PORT_MRx 0x00
61#define RD_PORT_SR 0x04
62#define RD_PORT_RHR 0x0c
63
64#define WR_PORT_MRx 0x00
65#define WR_PORT_CSR 0x04
66#define WR_PORT_CR 0x08
67#define WR_PORT_THR 0x0c
68
69/* SR bits */
70#define SR_BREAK (1 << 7)
71#define SR_FRAME (1 << 6)
72#define SR_PARITY (1 << 5)
73#define SR_OVERRUN (1 << 4)
74#define SR_TXRDY (1 << 2)
75#define SR_RXRDY (1 << 0)
76
77#define CR_RES_MR (1 << 4)
78#define CR_RES_RX (2 << 4)
79#define CR_RES_TX (3 << 4)
80#define CR_STRT_BRK (6 << 4)
81#define CR_STOP_BRK (7 << 4)
82#define CR_DIS_TX (1 << 3)
83#define CR_ENA_TX (1 << 2)
84#define CR_DIS_RX (1 << 1)
85#define CR_ENA_RX (1 << 0)
86
87/* ISR bits */
88#define ISR_RXRDYB (1 << 5)
89#define ISR_TXRDYB (1 << 4)
90#define ISR_RXRDYA (1 << 1)
91#define ISR_TXRDYA (1 << 0)
92
93/* IMR bits */
94#define IMR_RXRDY (1 << 1)
95#define IMR_TXRDY (1 << 0)
96
97/* access port register */
98static inline u8 read_sc_port(struct uart_port *p, u8 reg)
99{
100 return readb(p->membase + p->line * 0x20 + reg);
101}
102
103static inline void write_sc_port(struct uart_port *p, u8 reg, u8 val)
104{
105 writeb(val, p->membase + p->line * 0x20 + reg);
106}
107
108#define READ_SC_PORT(p, r) read_sc_port(p, RD_PORT_##r)
109#define WRITE_SC_PORT(p, r, v) write_sc_port(p, WR_PORT_##r, v)
110
111static void sc26xx_enable_irq(struct uart_port *port, int mask)
112{
113 struct uart_sc26xx_port *up;
114 int line = port->line;
115
116 port -= line;
117 up = container_of(port, struct uart_sc26xx_port, port[0]);
118
119 up->imr |= mask << (line * 4);
120 WRITE_SC(port, IMR, up->imr);
121}
122
123static void sc26xx_disable_irq(struct uart_port *port, int mask)
124{
125 struct uart_sc26xx_port *up;
126 int line = port->line;
127
128 port -= line;
129 up = container_of(port, struct uart_sc26xx_port, port[0]);
130
131 up->imr &= ~(mask << (line * 4));
132 WRITE_SC(port, IMR, up->imr);
133}
134
135static struct tty_struct *receive_chars(struct uart_port *port)
136{
137 struct tty_struct *tty = NULL;
138 int limit = 10000;
139 unsigned char ch;
140 char flag;
141 u8 status;
142
143 if (port->state != NULL) /* Unopened serial console */
144 tty = port->state->port.tty;
145
146 while (limit-- > 0) {
147 status = READ_SC_PORT(port, SR);
148 if (!(status & SR_RXRDY))
149 break;
150 ch = READ_SC_PORT(port, RHR);
151
152 flag = TTY_NORMAL;
153 port->icount.rx++;
154
155 if (unlikely(status & (SR_BREAK | SR_FRAME |
156 SR_PARITY | SR_OVERRUN))) {
157 if (status & SR_BREAK) {
158 status &= ~(SR_PARITY | SR_FRAME);
159 port->icount.brk++;
160 if (uart_handle_break(port))
161 continue;
162 } else if (status & SR_PARITY)
163 port->icount.parity++;
164 else if (status & SR_FRAME)
165 port->icount.frame++;
166 if (status & SR_OVERRUN)
167 port->icount.overrun++;
168
169 status &= port->read_status_mask;
170 if (status & SR_BREAK)
171 flag = TTY_BREAK;
172 else if (status & SR_PARITY)
173 flag = TTY_PARITY;
174 else if (status & SR_FRAME)
175 flag = TTY_FRAME;
176 }
177
178 if (uart_handle_sysrq_char(port, ch))
179 continue;
180
181 if (status & port->ignore_status_mask)
182 continue;
183
184 tty_insert_flip_char(tty, ch, flag);
185 }
186 return tty;
187}
188
189static void transmit_chars(struct uart_port *port)
190{
191 struct circ_buf *xmit;
192
193 if (!port->state)
194 return;
195
196 xmit = &port->state->xmit;
197 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
198 sc26xx_disable_irq(port, IMR_TXRDY);
199 return;
200 }
201 while (!uart_circ_empty(xmit)) {
202 if (!(READ_SC_PORT(port, SR) & SR_TXRDY))
203 break;
204
205 WRITE_SC_PORT(port, THR, xmit->buf[xmit->tail]);
206 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
207 port->icount.tx++;
208 }
209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
210 uart_write_wakeup(port);
211}
212
213static irqreturn_t sc26xx_interrupt(int irq, void *dev_id)
214{
215 struct uart_sc26xx_port *up = dev_id;
216 struct tty_struct *tty;
217 unsigned long flags;
218 u8 isr;
219
220 spin_lock_irqsave(&up->port[0].lock, flags);
221
222 tty = NULL;
223 isr = READ_SC(&up->port[0], ISR);
224 if (isr & ISR_TXRDYA)
225 transmit_chars(&up->port[0]);
226 if (isr & ISR_RXRDYA)
227 tty = receive_chars(&up->port[0]);
228
229 spin_unlock(&up->port[0].lock);
230
231 if (tty)
232 tty_flip_buffer_push(tty);
233
234 spin_lock(&up->port[1].lock);
235
236 tty = NULL;
237 if (isr & ISR_TXRDYB)
238 transmit_chars(&up->port[1]);
239 if (isr & ISR_RXRDYB)
240 tty = receive_chars(&up->port[1]);
241
242 spin_unlock_irqrestore(&up->port[1].lock, flags);
243
244 if (tty)
245 tty_flip_buffer_push(tty);
246
247 return IRQ_HANDLED;
248}
249
250/* port->lock is not held. */
251static unsigned int sc26xx_tx_empty(struct uart_port *port)
252{
253 return (READ_SC_PORT(port, SR) & SR_TXRDY) ? TIOCSER_TEMT : 0;
254}
255
256/* port->lock held by caller. */
257static void sc26xx_set_mctrl(struct uart_port *port, unsigned int mctrl)
258{
259 struct uart_sc26xx_port *up;
260 int line = port->line;
261
262 port -= line;
263 up = container_of(port, struct uart_sc26xx_port, port[0]);
264
265 if (up->dtr_mask[line]) {
266 if (mctrl & TIOCM_DTR)
267 WRITE_SC(port, OPR_SET, up->dtr_mask[line]);
268 else
269 WRITE_SC(port, OPR_CLR, up->dtr_mask[line]);
270 }
271 if (up->rts_mask[line]) {
272 if (mctrl & TIOCM_RTS)
273 WRITE_SC(port, OPR_SET, up->rts_mask[line]);
274 else
275 WRITE_SC(port, OPR_CLR, up->rts_mask[line]);
276 }
277}
278
279/* port->lock is held by caller and interrupts are disabled. */
280static unsigned int sc26xx_get_mctrl(struct uart_port *port)
281{
282 struct uart_sc26xx_port *up;
283 int line = port->line;
284 unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
285 u8 ipr;
286
287 port -= line;
288 up = container_of(port, struct uart_sc26xx_port, port[0]);
289 ipr = READ_SC(port, IPR) ^ 0xff;
290
291 if (up->dsr_mask[line]) {
292 mctrl &= ~TIOCM_DSR;
293 mctrl |= ipr & up->dsr_mask[line] ? TIOCM_DSR : 0;
294 }
295 if (up->cts_mask[line]) {
296 mctrl &= ~TIOCM_CTS;
297 mctrl |= ipr & up->cts_mask[line] ? TIOCM_CTS : 0;
298 }
299 if (up->dcd_mask[line]) {
300 mctrl &= ~TIOCM_CAR;
301 mctrl |= ipr & up->dcd_mask[line] ? TIOCM_CAR : 0;
302 }
303 if (up->ri_mask[line]) {
304 mctrl &= ~TIOCM_RNG;
305 mctrl |= ipr & up->ri_mask[line] ? TIOCM_RNG : 0;
306 }
307 return mctrl;
308}
309
310/* port->lock held by caller. */
311static void sc26xx_stop_tx(struct uart_port *port)
312{
313 return;
314}
315
316/* port->lock held by caller. */
317static void sc26xx_start_tx(struct uart_port *port)
318{
319 struct circ_buf *xmit = &port->state->xmit;
320
321 while (!uart_circ_empty(xmit)) {
322 if (!(READ_SC_PORT(port, SR) & SR_TXRDY)) {
323 sc26xx_enable_irq(port, IMR_TXRDY);
324 break;
325 }
326 WRITE_SC_PORT(port, THR, xmit->buf[xmit->tail]);
327 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
328 port->icount.tx++;
329 }
330}
331
332/* port->lock held by caller. */
333static void sc26xx_stop_rx(struct uart_port *port)
334{
335}
336
337/* port->lock held by caller. */
338static void sc26xx_enable_ms(struct uart_port *port)
339{
340}
341
342/* port->lock is not held. */
343static void sc26xx_break_ctl(struct uart_port *port, int break_state)
344{
345 if (break_state == -1)
346 WRITE_SC_PORT(port, CR, CR_STRT_BRK);
347 else
348 WRITE_SC_PORT(port, CR, CR_STOP_BRK);
349}
350
351/* port->lock is not held. */
352static int sc26xx_startup(struct uart_port *port)
353{
354 sc26xx_disable_irq(port, IMR_TXRDY | IMR_RXRDY);
355 WRITE_SC(port, OPCR, 0);
356
357 /* reset tx and rx */
358 WRITE_SC_PORT(port, CR, CR_RES_RX);
359 WRITE_SC_PORT(port, CR, CR_RES_TX);
360
361 /* start rx/tx */
362 WRITE_SC_PORT(port, CR, CR_ENA_TX | CR_ENA_RX);
363
364 /* enable irqs */
365 sc26xx_enable_irq(port, IMR_RXRDY);
366 return 0;
367}
368
369/* port->lock is not held. */
370static void sc26xx_shutdown(struct uart_port *port)
371{
372 /* disable interrupst */
373 sc26xx_disable_irq(port, IMR_TXRDY | IMR_RXRDY);
374
375 /* stop tx/rx */
376 WRITE_SC_PORT(port, CR, CR_DIS_TX | CR_DIS_RX);
377}
378
379/* port->lock is not held. */
380static void sc26xx_set_termios(struct uart_port *port, struct ktermios *termios,
381 struct ktermios *old)
382{
383 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
384 unsigned int quot = uart_get_divisor(port, baud);
385 unsigned int iflag, cflag;
386 unsigned long flags;
387 u8 mr1, mr2, csr;
388
389 spin_lock_irqsave(&port->lock, flags);
390
391 while ((READ_SC_PORT(port, SR) & ((1 << 3) | (1 << 2))) != 0xc)
392 udelay(2);
393
394 WRITE_SC_PORT(port, CR, CR_DIS_TX | CR_DIS_RX);
395
396 iflag = termios->c_iflag;
397 cflag = termios->c_cflag;
398
399 port->read_status_mask = SR_OVERRUN;
400 if (iflag & INPCK)
401 port->read_status_mask |= SR_PARITY | SR_FRAME;
402 if (iflag & (BRKINT | PARMRK))
403 port->read_status_mask |= SR_BREAK;
404
405 port->ignore_status_mask = 0;
406 if (iflag & IGNBRK)
407 port->ignore_status_mask |= SR_BREAK;
408 if ((cflag & CREAD) == 0)
409 port->ignore_status_mask |= SR_BREAK | SR_FRAME |
410 SR_PARITY | SR_OVERRUN;
411
412 switch (cflag & CSIZE) {
413 case CS5:
414 mr1 = 0x00;
415 break;
416 case CS6:
417 mr1 = 0x01;
418 break;
419 case CS7:
420 mr1 = 0x02;
421 break;
422 default:
423 case CS8:
424 mr1 = 0x03;
425 break;
426 }
427 mr2 = 0x07;
428 if (cflag & CSTOPB)
429 mr2 = 0x0f;
430 if (cflag & PARENB) {
431 if (cflag & PARODD)
432 mr1 |= (1 << 2);
433 } else
434 mr1 |= (2 << 3);
435
436 switch (baud) {
437 case 50:
438 csr = 0x00;
439 break;
440 case 110:
441 csr = 0x11;
442 break;
443 case 134:
444 csr = 0x22;
445 break;
446 case 200:
447 csr = 0x33;
448 break;
449 case 300:
450 csr = 0x44;
451 break;
452 case 600:
453 csr = 0x55;
454 break;
455 case 1200:
456 csr = 0x66;
457 break;
458 case 2400:
459 csr = 0x88;
460 break;
461 case 4800:
462 csr = 0x99;
463 break;
464 default:
465 case 9600:
466 csr = 0xbb;
467 break;
468 case 19200:
469 csr = 0xcc;
470 break;
471 }
472
473 WRITE_SC_PORT(port, CR, CR_RES_MR);
474 WRITE_SC_PORT(port, MRx, mr1);
475 WRITE_SC_PORT(port, MRx, mr2);
476
477 WRITE_SC(port, ACR, 0x80);
478 WRITE_SC_PORT(port, CSR, csr);
479
480 /* reset tx and rx */
481 WRITE_SC_PORT(port, CR, CR_RES_RX);
482 WRITE_SC_PORT(port, CR, CR_RES_TX);
483
484 WRITE_SC_PORT(port, CR, CR_ENA_TX | CR_ENA_RX);
485 while ((READ_SC_PORT(port, SR) & ((1 << 3) | (1 << 2))) != 0xc)
486 udelay(2);
487
488 /* XXX */
489 uart_update_timeout(port, cflag,
490 (port->uartclk / (16 * quot)));
491
492 spin_unlock_irqrestore(&port->lock, flags);
493}
494
495static const char *sc26xx_type(struct uart_port *port)
496{
497 return "SC26XX";
498}
499
500static void sc26xx_release_port(struct uart_port *port)
501{
502}
503
504static int sc26xx_request_port(struct uart_port *port)
505{
506 return 0;
507}
508
509static void sc26xx_config_port(struct uart_port *port, int flags)
510{
511}
512
513static int sc26xx_verify_port(struct uart_port *port, struct serial_struct *ser)
514{
515 return -EINVAL;
516}
517
518static struct uart_ops sc26xx_ops = {
519 .tx_empty = sc26xx_tx_empty,
520 .set_mctrl = sc26xx_set_mctrl,
521 .get_mctrl = sc26xx_get_mctrl,
522 .stop_tx = sc26xx_stop_tx,
523 .start_tx = sc26xx_start_tx,
524 .stop_rx = sc26xx_stop_rx,
525 .enable_ms = sc26xx_enable_ms,
526 .break_ctl = sc26xx_break_ctl,
527 .startup = sc26xx_startup,
528 .shutdown = sc26xx_shutdown,
529 .set_termios = sc26xx_set_termios,
530 .type = sc26xx_type,
531 .release_port = sc26xx_release_port,
532 .request_port = sc26xx_request_port,
533 .config_port = sc26xx_config_port,
534 .verify_port = sc26xx_verify_port,
535};
536
537static struct uart_port *sc26xx_port;
538
539#ifdef CONFIG_SERIAL_SC26XX_CONSOLE
540static void sc26xx_console_putchar(struct uart_port *port, char c)
541{
542 unsigned long flags;
543 int limit = 1000000;
544
545 spin_lock_irqsave(&port->lock, flags);
546
547 while (limit-- > 0) {
548 if (READ_SC_PORT(port, SR) & SR_TXRDY) {
549 WRITE_SC_PORT(port, THR, c);
550 break;
551 }
552 udelay(2);
553 }
554
555 spin_unlock_irqrestore(&port->lock, flags);
556}
557
558static void sc26xx_console_write(struct console *con, const char *s, unsigned n)
559{
560 struct uart_port *port = sc26xx_port;
561 int i;
562
563 for (i = 0; i < n; i++) {
564 if (*s == '\n')
565 sc26xx_console_putchar(port, '\r');
566 sc26xx_console_putchar(port, *s++);
567 }
568}
569
570static int __init sc26xx_console_setup(struct console *con, char *options)
571{
572 struct uart_port *port = sc26xx_port;
573 int baud = 9600;
574 int bits = 8;
575 int parity = 'n';
576 int flow = 'n';
577
578 if (port->type != PORT_SC26XX)
579 return -1;
580
581 printk(KERN_INFO "Console: ttySC%d (SC26XX)\n", con->index);
582 if (options)
583 uart_parse_options(options, &baud, &parity, &bits, &flow);
584
585 return uart_set_options(port, con, baud, parity, bits, flow);
586}
587
588static struct uart_driver sc26xx_reg;
589static struct console sc26xx_console = {
590 .name = "ttySC",
591 .write = sc26xx_console_write,
592 .device = uart_console_device,
593 .setup = sc26xx_console_setup,
594 .flags = CON_PRINTBUFFER,
595 .index = -1,
596 .data = &sc26xx_reg,
597};
598#define SC26XX_CONSOLE &sc26xx_console
599#else
600#define SC26XX_CONSOLE NULL
601#endif
602
603static struct uart_driver sc26xx_reg = {
604 .owner = THIS_MODULE,
605 .driver_name = "SC26xx",
606 .dev_name = "ttySC",
607 .major = SC26XX_MAJOR,
608 .minor = SC26XX_MINOR_START,
609 .nr = SC26XX_NR,
610 .cons = SC26XX_CONSOLE,
611};
612
613static u8 sc26xx_flags2mask(unsigned int flags, unsigned int bitpos)
614{
615 unsigned int bit = (flags >> bitpos) & 15;
616
617 return bit ? (1 << (bit - 1)) : 0;
618}
619
620static void __devinit sc26xx_init_masks(struct uart_sc26xx_port *up,
621 int line, unsigned int data)
622{
623 up->dtr_mask[line] = sc26xx_flags2mask(data, 0);
624 up->rts_mask[line] = sc26xx_flags2mask(data, 4);
625 up->dsr_mask[line] = sc26xx_flags2mask(data, 8);
626 up->cts_mask[line] = sc26xx_flags2mask(data, 12);
627 up->dcd_mask[line] = sc26xx_flags2mask(data, 16);
628 up->ri_mask[line] = sc26xx_flags2mask(data, 20);
629}
630
631static int __devinit sc26xx_probe(struct platform_device *dev)
632{
633 struct resource *res;
634 struct uart_sc26xx_port *up;
635 unsigned int *sc26xx_data = dev->dev.platform_data;
636 int err;
637
638 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
639 if (!res)
640 return -ENODEV;
641
642 up = kzalloc(sizeof *up, GFP_KERNEL);
643 if (unlikely(!up))
644 return -ENOMEM;
645
646 up->port[0].line = 0;
647 up->port[0].ops = &sc26xx_ops;
648 up->port[0].type = PORT_SC26XX;
649 up->port[0].uartclk = (29491200 / 16); /* arbitrary */
650
651 up->port[0].mapbase = res->start;
652 up->port[0].membase = ioremap_nocache(up->port[0].mapbase, 0x40);
653 up->port[0].iotype = UPIO_MEM;
654 up->port[0].irq = platform_get_irq(dev, 0);
655
656 up->port[0].dev = &dev->dev;
657
658 sc26xx_init_masks(up, 0, sc26xx_data[0]);
659
660 sc26xx_port = &up->port[0];
661
662 up->port[1].line = 1;
663 up->port[1].ops = &sc26xx_ops;
664 up->port[1].type = PORT_SC26XX;
665 up->port[1].uartclk = (29491200 / 16); /* arbitrary */
666
667 up->port[1].mapbase = up->port[0].mapbase;
668 up->port[1].membase = up->port[0].membase;
669 up->port[1].iotype = UPIO_MEM;
670 up->port[1].irq = up->port[0].irq;
671
672 up->port[1].dev = &dev->dev;
673
674 sc26xx_init_masks(up, 1, sc26xx_data[1]);
675
676 err = uart_register_driver(&sc26xx_reg);
677 if (err)
678 goto out_free_port;
679
680 sc26xx_reg.tty_driver->name_base = sc26xx_reg.minor;
681
682 err = uart_add_one_port(&sc26xx_reg, &up->port[0]);
683 if (err)
684 goto out_unregister_driver;
685
686 err = uart_add_one_port(&sc26xx_reg, &up->port[1]);
687 if (err)
688 goto out_remove_port0;
689
690 err = request_irq(up->port[0].irq, sc26xx_interrupt, 0, "sc26xx", up);
691 if (err)
692 goto out_remove_ports;
693
694 dev_set_drvdata(&dev->dev, up);
695 return 0;
696
697out_remove_ports:
698 uart_remove_one_port(&sc26xx_reg, &up->port[1]);
699out_remove_port0:
700 uart_remove_one_port(&sc26xx_reg, &up->port[0]);
701
702out_unregister_driver:
703 uart_unregister_driver(&sc26xx_reg);
704
705out_free_port:
706 kfree(up);
707 sc26xx_port = NULL;
708 return err;
709}
710
711
712static int __exit sc26xx_driver_remove(struct platform_device *dev)
713{
714 struct uart_sc26xx_port *up = dev_get_drvdata(&dev->dev);
715
716 free_irq(up->port[0].irq, up);
717
718 uart_remove_one_port(&sc26xx_reg, &up->port[0]);
719 uart_remove_one_port(&sc26xx_reg, &up->port[1]);
720
721 uart_unregister_driver(&sc26xx_reg);
722
723 kfree(up);
724 sc26xx_port = NULL;
725
726 dev_set_drvdata(&dev->dev, NULL);
727 return 0;
728}
729
730static struct platform_driver sc26xx_driver = {
731 .probe = sc26xx_probe,
732 .remove = __devexit_p(sc26xx_driver_remove),
733 .driver = {
734 .name = "SC26xx",
735 .owner = THIS_MODULE,
736 },
737};
738
739static int __init sc26xx_init(void)
740{
741 return platform_driver_register(&sc26xx_driver);
742}
743
744static void __exit sc26xx_exit(void)
745{
746 platform_driver_unregister(&sc26xx_driver);
747}
748
749module_init(sc26xx_init);
750module_exit(sc26xx_exit);
751
752
753MODULE_AUTHOR("Thomas Bogendörfer");
754MODULE_DESCRIPTION("SC681/SC2692 serial driver");
755MODULE_VERSION("1.0");
756MODULE_LICENSE("GPL");
757MODULE_ALIAS("platform:SC26xx");
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
new file mode 100644
index 000000000000..460a72d91bb7
--- /dev/null
+++ b/drivers/tty/serial/serial_core.c
@@ -0,0 +1,2578 @@
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/module.h>
26#include <linux/tty.h>
27#include <linux/slab.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
32#include <linux/device.h>
33#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34#include <linux/serial_core.h>
35#include <linux/delay.h>
36#include <linux/mutex.h>
37
38#include <asm/irq.h>
39#include <asm/uaccess.h>
40
41/*
42 * This is used to lock changes in serial line configuration.
43 */
44static DEFINE_MUTEX(port_mutex);
45
46/*
47 * lockdep: port->lock is initialized in two places, but we
48 * want only one lock-class:
49 */
50static struct lock_class_key port_lock_key;
51
52#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53
54#ifdef CONFIG_SERIAL_CORE_CONSOLE
55#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
56#else
57#define uart_console(port) (0)
58#endif
59
60static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
61 struct ktermios *old_termios);
62static void __uart_wait_until_sent(struct uart_port *port, int timeout);
63static void uart_change_pm(struct uart_state *state, int pm_state);
64
65/*
66 * This routine is used by the interrupt handler to schedule processing in
67 * the software interrupt portion of the driver.
68 */
69void uart_write_wakeup(struct uart_port *port)
70{
71 struct uart_state *state = port->state;
72 /*
73 * This means you called this function _after_ the port was
74 * closed. No cookie for you.
75 */
76 BUG_ON(!state);
77 tasklet_schedule(&state->tlet);
78}
79
80static void uart_stop(struct tty_struct *tty)
81{
82 struct uart_state *state = tty->driver_data;
83 struct uart_port *port = state->uart_port;
84 unsigned long flags;
85
86 spin_lock_irqsave(&port->lock, flags);
87 port->ops->stop_tx(port);
88 spin_unlock_irqrestore(&port->lock, flags);
89}
90
91static void __uart_start(struct tty_struct *tty)
92{
93 struct uart_state *state = tty->driver_data;
94 struct uart_port *port = state->uart_port;
95
96 if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
97 !tty->stopped && !tty->hw_stopped)
98 port->ops->start_tx(port);
99}
100
101static void uart_start(struct tty_struct *tty)
102{
103 struct uart_state *state = tty->driver_data;
104 struct uart_port *port = state->uart_port;
105 unsigned long flags;
106
107 spin_lock_irqsave(&port->lock, flags);
108 __uart_start(tty);
109 spin_unlock_irqrestore(&port->lock, flags);
110}
111
112static void uart_tasklet_action(unsigned long data)
113{
114 struct uart_state *state = (struct uart_state *)data;
115 tty_wakeup(state->port.tty);
116}
117
118static inline void
119uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
120{
121 unsigned long flags;
122 unsigned int old;
123
124 spin_lock_irqsave(&port->lock, flags);
125 old = port->mctrl;
126 port->mctrl = (old & ~clear) | set;
127 if (old != port->mctrl)
128 port->ops->set_mctrl(port, port->mctrl);
129 spin_unlock_irqrestore(&port->lock, flags);
130}
131
132#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
133#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
134
135/*
136 * Startup the port. This will be called once per open. All calls
137 * will be serialised by the per-port mutex.
138 */
139static int uart_startup(struct tty_struct *tty, struct uart_state *state, int init_hw)
140{
141 struct uart_port *uport = state->uart_port;
142 struct tty_port *port = &state->port;
143 unsigned long page;
144 int retval = 0;
145
146 if (port->flags & ASYNC_INITIALIZED)
147 return 0;
148
149 /*
150 * Set the TTY IO error marker - we will only clear this
151 * once we have successfully opened the port. Also set
152 * up the tty->alt_speed kludge
153 */
154 set_bit(TTY_IO_ERROR, &tty->flags);
155
156 if (uport->type == PORT_UNKNOWN)
157 return 0;
158
159 /*
160 * Initialise and allocate the transmit and temporary
161 * buffer.
162 */
163 if (!state->xmit.buf) {
164 /* This is protected by the per port mutex */
165 page = get_zeroed_page(GFP_KERNEL);
166 if (!page)
167 return -ENOMEM;
168
169 state->xmit.buf = (unsigned char *) page;
170 uart_circ_clear(&state->xmit);
171 }
172
173 retval = uport->ops->startup(uport);
174 if (retval == 0) {
175 if (init_hw) {
176 /*
177 * Initialise the hardware port settings.
178 */
179 uart_change_speed(tty, state, NULL);
180
181 /*
182 * Setup the RTS and DTR signals once the
183 * port is open and ready to respond.
184 */
185 if (tty->termios->c_cflag & CBAUD)
186 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
187 }
188
189 if (port->flags & ASYNC_CTS_FLOW) {
190 spin_lock_irq(&uport->lock);
191 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
192 tty->hw_stopped = 1;
193 spin_unlock_irq(&uport->lock);
194 }
195
196 set_bit(ASYNCB_INITIALIZED, &port->flags);
197
198 clear_bit(TTY_IO_ERROR, &tty->flags);
199 }
200
201 if (retval && capable(CAP_SYS_ADMIN))
202 retval = 0;
203
204 return retval;
205}
206
207/*
208 * This routine will shutdown a serial port; interrupts are disabled, and
209 * DTR is dropped if the hangup on close termio flag is on. Calls to
210 * uart_shutdown are serialised by the per-port semaphore.
211 */
212static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
213{
214 struct uart_port *uport = state->uart_port;
215 struct tty_port *port = &state->port;
216
217 /*
218 * Set the TTY IO error marker
219 */
220 if (tty)
221 set_bit(TTY_IO_ERROR, &tty->flags);
222
223 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
224 /*
225 * Turn off DTR and RTS early.
226 */
227 if (!tty || (tty->termios->c_cflag & HUPCL))
228 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
229
230 /*
231 * clear delta_msr_wait queue to avoid mem leaks: we may free
232 * the irq here so the queue might never be woken up. Note
233 * that we won't end up waiting on delta_msr_wait again since
234 * any outstanding file descriptors should be pointing at
235 * hung_up_tty_fops now.
236 */
237 wake_up_interruptible(&port->delta_msr_wait);
238
239 /*
240 * Free the IRQ and disable the port.
241 */
242 uport->ops->shutdown(uport);
243
244 /*
245 * Ensure that the IRQ handler isn't running on another CPU.
246 */
247 synchronize_irq(uport->irq);
248 }
249
250 /*
251 * kill off our tasklet
252 */
253 tasklet_kill(&state->tlet);
254
255 /*
256 * Free the transmit buffer page.
257 */
258 if (state->xmit.buf) {
259 free_page((unsigned long)state->xmit.buf);
260 state->xmit.buf = NULL;
261 }
262}
263
264/**
265 * uart_update_timeout - update per-port FIFO timeout.
266 * @port: uart_port structure describing the port
267 * @cflag: termios cflag value
268 * @baud: speed of the port
269 *
270 * Set the port FIFO timeout value. The @cflag value should
271 * reflect the actual hardware settings.
272 */
273void
274uart_update_timeout(struct uart_port *port, unsigned int cflag,
275 unsigned int baud)
276{
277 unsigned int bits;
278
279 /* byte size and parity */
280 switch (cflag & CSIZE) {
281 case CS5:
282 bits = 7;
283 break;
284 case CS6:
285 bits = 8;
286 break;
287 case CS7:
288 bits = 9;
289 break;
290 default:
291 bits = 10;
292 break; /* CS8 */
293 }
294
295 if (cflag & CSTOPB)
296 bits++;
297 if (cflag & PARENB)
298 bits++;
299
300 /*
301 * The total number of bits to be transmitted in the fifo.
302 */
303 bits = bits * port->fifosize;
304
305 /*
306 * Figure the timeout to send the above number of bits.
307 * Add .02 seconds of slop
308 */
309 port->timeout = (HZ * bits) / baud + HZ/50;
310}
311
312EXPORT_SYMBOL(uart_update_timeout);
313
314/**
315 * uart_get_baud_rate - return baud rate for a particular port
316 * @port: uart_port structure describing the port in question.
317 * @termios: desired termios settings.
318 * @old: old termios (or NULL)
319 * @min: minimum acceptable baud rate
320 * @max: maximum acceptable baud rate
321 *
322 * Decode the termios structure into a numeric baud rate,
323 * taking account of the magic 38400 baud rate (with spd_*
324 * flags), and mapping the %B0 rate to 9600 baud.
325 *
326 * If the new baud rate is invalid, try the old termios setting.
327 * If it's still invalid, we try 9600 baud.
328 *
329 * Update the @termios structure to reflect the baud rate
330 * we're actually going to be using. Don't do this for the case
331 * where B0 is requested ("hang up").
332 */
333unsigned int
334uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
335 struct ktermios *old, unsigned int min, unsigned int max)
336{
337 unsigned int try, baud, altbaud = 38400;
338 int hung_up = 0;
339 upf_t flags = port->flags & UPF_SPD_MASK;
340
341 if (flags == UPF_SPD_HI)
342 altbaud = 57600;
343 else if (flags == UPF_SPD_VHI)
344 altbaud = 115200;
345 else if (flags == UPF_SPD_SHI)
346 altbaud = 230400;
347 else if (flags == UPF_SPD_WARP)
348 altbaud = 460800;
349
350 for (try = 0; try < 2; try++) {
351 baud = tty_termios_baud_rate(termios);
352
353 /*
354 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
355 * Die! Die! Die!
356 */
357 if (baud == 38400)
358 baud = altbaud;
359
360 /*
361 * Special case: B0 rate.
362 */
363 if (baud == 0) {
364 hung_up = 1;
365 baud = 9600;
366 }
367
368 if (baud >= min && baud <= max)
369 return baud;
370
371 /*
372 * Oops, the quotient was zero. Try again with
373 * the old baud rate if possible.
374 */
375 termios->c_cflag &= ~CBAUD;
376 if (old) {
377 baud = tty_termios_baud_rate(old);
378 if (!hung_up)
379 tty_termios_encode_baud_rate(termios,
380 baud, baud);
381 old = NULL;
382 continue;
383 }
384
385 /*
386 * As a last resort, if the range cannot be met then clip to
387 * the nearest chip supported rate.
388 */
389 if (!hung_up) {
390 if (baud <= min)
391 tty_termios_encode_baud_rate(termios,
392 min + 1, min + 1);
393 else
394 tty_termios_encode_baud_rate(termios,
395 max - 1, max - 1);
396 }
397 }
398 /* Should never happen */
399 WARN_ON(1);
400 return 0;
401}
402
403EXPORT_SYMBOL(uart_get_baud_rate);
404
405/**
406 * uart_get_divisor - return uart clock divisor
407 * @port: uart_port structure describing the port.
408 * @baud: desired baud rate
409 *
410 * Calculate the uart clock divisor for the port.
411 */
412unsigned int
413uart_get_divisor(struct uart_port *port, unsigned int baud)
414{
415 unsigned int quot;
416
417 /*
418 * Old custom speed handling.
419 */
420 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
421 quot = port->custom_divisor;
422 else
423 quot = (port->uartclk + (8 * baud)) / (16 * baud);
424
425 return quot;
426}
427
428EXPORT_SYMBOL(uart_get_divisor);
429
430/* FIXME: Consistent locking policy */
431static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
432 struct ktermios *old_termios)
433{
434 struct tty_port *port = &state->port;
435 struct uart_port *uport = state->uart_port;
436 struct ktermios *termios;
437
438 /*
439 * If we have no tty, termios, or the port does not exist,
440 * then we can't set the parameters for this port.
441 */
442 if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
443 return;
444
445 termios = tty->termios;
446
447 /*
448 * Set flags based on termios cflag
449 */
450 if (termios->c_cflag & CRTSCTS)
451 set_bit(ASYNCB_CTS_FLOW, &port->flags);
452 else
453 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
454
455 if (termios->c_cflag & CLOCAL)
456 clear_bit(ASYNCB_CHECK_CD, &port->flags);
457 else
458 set_bit(ASYNCB_CHECK_CD, &port->flags);
459
460 uport->ops->set_termios(uport, termios, old_termios);
461}
462
463static inline int __uart_put_char(struct uart_port *port,
464 struct circ_buf *circ, unsigned char c)
465{
466 unsigned long flags;
467 int ret = 0;
468
469 if (!circ->buf)
470 return 0;
471
472 spin_lock_irqsave(&port->lock, flags);
473 if (uart_circ_chars_free(circ) != 0) {
474 circ->buf[circ->head] = c;
475 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
476 ret = 1;
477 }
478 spin_unlock_irqrestore(&port->lock, flags);
479 return ret;
480}
481
482static int uart_put_char(struct tty_struct *tty, unsigned char ch)
483{
484 struct uart_state *state = tty->driver_data;
485
486 return __uart_put_char(state->uart_port, &state->xmit, ch);
487}
488
489static void uart_flush_chars(struct tty_struct *tty)
490{
491 uart_start(tty);
492}
493
494static int uart_write(struct tty_struct *tty,
495 const unsigned char *buf, int count)
496{
497 struct uart_state *state = tty->driver_data;
498 struct uart_port *port;
499 struct circ_buf *circ;
500 unsigned long flags;
501 int c, ret = 0;
502
503 /*
504 * This means you called this function _after_ the port was
505 * closed. No cookie for you.
506 */
507 if (!state) {
508 WARN_ON(1);
509 return -EL3HLT;
510 }
511
512 port = state->uart_port;
513 circ = &state->xmit;
514
515 if (!circ->buf)
516 return 0;
517
518 spin_lock_irqsave(&port->lock, flags);
519 while (1) {
520 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
521 if (count < c)
522 c = count;
523 if (c <= 0)
524 break;
525 memcpy(circ->buf + circ->head, buf, c);
526 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
527 buf += c;
528 count -= c;
529 ret += c;
530 }
531 spin_unlock_irqrestore(&port->lock, flags);
532
533 uart_start(tty);
534 return ret;
535}
536
537static int uart_write_room(struct tty_struct *tty)
538{
539 struct uart_state *state = tty->driver_data;
540 unsigned long flags;
541 int ret;
542
543 spin_lock_irqsave(&state->uart_port->lock, flags);
544 ret = uart_circ_chars_free(&state->xmit);
545 spin_unlock_irqrestore(&state->uart_port->lock, flags);
546 return ret;
547}
548
549static int uart_chars_in_buffer(struct tty_struct *tty)
550{
551 struct uart_state *state = tty->driver_data;
552 unsigned long flags;
553 int ret;
554
555 spin_lock_irqsave(&state->uart_port->lock, flags);
556 ret = uart_circ_chars_pending(&state->xmit);
557 spin_unlock_irqrestore(&state->uart_port->lock, flags);
558 return ret;
559}
560
561static void uart_flush_buffer(struct tty_struct *tty)
562{
563 struct uart_state *state = tty->driver_data;
564 struct uart_port *port;
565 unsigned long flags;
566
567 /*
568 * This means you called this function _after_ the port was
569 * closed. No cookie for you.
570 */
571 if (!state) {
572 WARN_ON(1);
573 return;
574 }
575
576 port = state->uart_port;
577 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
578
579 spin_lock_irqsave(&port->lock, flags);
580 uart_circ_clear(&state->xmit);
581 if (port->ops->flush_buffer)
582 port->ops->flush_buffer(port);
583 spin_unlock_irqrestore(&port->lock, flags);
584 tty_wakeup(tty);
585}
586
587/*
588 * This function is used to send a high-priority XON/XOFF character to
589 * the device
590 */
591static void uart_send_xchar(struct tty_struct *tty, char ch)
592{
593 struct uart_state *state = tty->driver_data;
594 struct uart_port *port = state->uart_port;
595 unsigned long flags;
596
597 if (port->ops->send_xchar)
598 port->ops->send_xchar(port, ch);
599 else {
600 port->x_char = ch;
601 if (ch) {
602 spin_lock_irqsave(&port->lock, flags);
603 port->ops->start_tx(port);
604 spin_unlock_irqrestore(&port->lock, flags);
605 }
606 }
607}
608
609static void uart_throttle(struct tty_struct *tty)
610{
611 struct uart_state *state = tty->driver_data;
612
613 if (I_IXOFF(tty))
614 uart_send_xchar(tty, STOP_CHAR(tty));
615
616 if (tty->termios->c_cflag & CRTSCTS)
617 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
618}
619
620static void uart_unthrottle(struct tty_struct *tty)
621{
622 struct uart_state *state = tty->driver_data;
623 struct uart_port *port = state->uart_port;
624
625 if (I_IXOFF(tty)) {
626 if (port->x_char)
627 port->x_char = 0;
628 else
629 uart_send_xchar(tty, START_CHAR(tty));
630 }
631
632 if (tty->termios->c_cflag & CRTSCTS)
633 uart_set_mctrl(port, TIOCM_RTS);
634}
635
636static int uart_get_info(struct uart_state *state,
637 struct serial_struct __user *retinfo)
638{
639 struct uart_port *uport = state->uart_port;
640 struct tty_port *port = &state->port;
641 struct serial_struct tmp;
642
643 memset(&tmp, 0, sizeof(tmp));
644
645 /* Ensure the state we copy is consistent and no hardware changes
646 occur as we go */
647 mutex_lock(&port->mutex);
648
649 tmp.type = uport->type;
650 tmp.line = uport->line;
651 tmp.port = uport->iobase;
652 if (HIGH_BITS_OFFSET)
653 tmp.port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
654 tmp.irq = uport->irq;
655 tmp.flags = uport->flags;
656 tmp.xmit_fifo_size = uport->fifosize;
657 tmp.baud_base = uport->uartclk / 16;
658 tmp.close_delay = port->close_delay / 10;
659 tmp.closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
660 ASYNC_CLOSING_WAIT_NONE :
661 port->closing_wait / 10;
662 tmp.custom_divisor = uport->custom_divisor;
663 tmp.hub6 = uport->hub6;
664 tmp.io_type = uport->iotype;
665 tmp.iomem_reg_shift = uport->regshift;
666 tmp.iomem_base = (void *)(unsigned long)uport->mapbase;
667
668 mutex_unlock(&port->mutex);
669
670 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
671 return -EFAULT;
672 return 0;
673}
674
675static int uart_set_info(struct tty_struct *tty, struct uart_state *state,
676 struct serial_struct __user *newinfo)
677{
678 struct serial_struct new_serial;
679 struct uart_port *uport = state->uart_port;
680 struct tty_port *port = &state->port;
681 unsigned long new_port;
682 unsigned int change_irq, change_port, closing_wait;
683 unsigned int old_custom_divisor, close_delay;
684 upf_t old_flags, new_flags;
685 int retval = 0;
686
687 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
688 return -EFAULT;
689
690 new_port = new_serial.port;
691 if (HIGH_BITS_OFFSET)
692 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
693
694 new_serial.irq = irq_canonicalize(new_serial.irq);
695 close_delay = new_serial.close_delay * 10;
696 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
697 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
698
699 /*
700 * This semaphore protects port->count. It is also
701 * very useful to prevent opens. Also, take the
702 * port configuration semaphore to make sure that a
703 * module insertion/removal doesn't change anything
704 * under us.
705 */
706 mutex_lock(&port->mutex);
707
708 change_irq = !(uport->flags & UPF_FIXED_PORT)
709 && new_serial.irq != uport->irq;
710
711 /*
712 * Since changing the 'type' of the port changes its resource
713 * allocations, we should treat type changes the same as
714 * IO port changes.
715 */
716 change_port = !(uport->flags & UPF_FIXED_PORT)
717 && (new_port != uport->iobase ||
718 (unsigned long)new_serial.iomem_base != uport->mapbase ||
719 new_serial.hub6 != uport->hub6 ||
720 new_serial.io_type != uport->iotype ||
721 new_serial.iomem_reg_shift != uport->regshift ||
722 new_serial.type != uport->type);
723
724 old_flags = uport->flags;
725 new_flags = new_serial.flags;
726 old_custom_divisor = uport->custom_divisor;
727
728 if (!capable(CAP_SYS_ADMIN)) {
729 retval = -EPERM;
730 if (change_irq || change_port ||
731 (new_serial.baud_base != uport->uartclk / 16) ||
732 (close_delay != port->close_delay) ||
733 (closing_wait != port->closing_wait) ||
734 (new_serial.xmit_fifo_size &&
735 new_serial.xmit_fifo_size != uport->fifosize) ||
736 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
737 goto exit;
738 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
739 (new_flags & UPF_USR_MASK));
740 uport->custom_divisor = new_serial.custom_divisor;
741 goto check_and_exit;
742 }
743
744 /*
745 * Ask the low level driver to verify the settings.
746 */
747 if (uport->ops->verify_port)
748 retval = uport->ops->verify_port(uport, &new_serial);
749
750 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
751 (new_serial.baud_base < 9600))
752 retval = -EINVAL;
753
754 if (retval)
755 goto exit;
756
757 if (change_port || change_irq) {
758 retval = -EBUSY;
759
760 /*
761 * Make sure that we are the sole user of this port.
762 */
763 if (tty_port_users(port) > 1)
764 goto exit;
765
766 /*
767 * We need to shutdown the serial port at the old
768 * port/type/irq combination.
769 */
770 uart_shutdown(tty, state);
771 }
772
773 if (change_port) {
774 unsigned long old_iobase, old_mapbase;
775 unsigned int old_type, old_iotype, old_hub6, old_shift;
776
777 old_iobase = uport->iobase;
778 old_mapbase = uport->mapbase;
779 old_type = uport->type;
780 old_hub6 = uport->hub6;
781 old_iotype = uport->iotype;
782 old_shift = uport->regshift;
783
784 /*
785 * Free and release old regions
786 */
787 if (old_type != PORT_UNKNOWN)
788 uport->ops->release_port(uport);
789
790 uport->iobase = new_port;
791 uport->type = new_serial.type;
792 uport->hub6 = new_serial.hub6;
793 uport->iotype = new_serial.io_type;
794 uport->regshift = new_serial.iomem_reg_shift;
795 uport->mapbase = (unsigned long)new_serial.iomem_base;
796
797 /*
798 * Claim and map the new regions
799 */
800 if (uport->type != PORT_UNKNOWN) {
801 retval = uport->ops->request_port(uport);
802 } else {
803 /* Always success - Jean II */
804 retval = 0;
805 }
806
807 /*
808 * If we fail to request resources for the
809 * new port, try to restore the old settings.
810 */
811 if (retval && old_type != PORT_UNKNOWN) {
812 uport->iobase = old_iobase;
813 uport->type = old_type;
814 uport->hub6 = old_hub6;
815 uport->iotype = old_iotype;
816 uport->regshift = old_shift;
817 uport->mapbase = old_mapbase;
818 retval = uport->ops->request_port(uport);
819 /*
820 * If we failed to restore the old settings,
821 * we fail like this.
822 */
823 if (retval)
824 uport->type = PORT_UNKNOWN;
825
826 /*
827 * We failed anyway.
828 */
829 retval = -EBUSY;
830 /* Added to return the correct error -Ram Gupta */
831 goto exit;
832 }
833 }
834
835 if (change_irq)
836 uport->irq = new_serial.irq;
837 if (!(uport->flags & UPF_FIXED_PORT))
838 uport->uartclk = new_serial.baud_base * 16;
839 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
840 (new_flags & UPF_CHANGE_MASK);
841 uport->custom_divisor = new_serial.custom_divisor;
842 port->close_delay = close_delay;
843 port->closing_wait = closing_wait;
844 if (new_serial.xmit_fifo_size)
845 uport->fifosize = new_serial.xmit_fifo_size;
846 if (port->tty)
847 port->tty->low_latency =
848 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
849
850 check_and_exit:
851 retval = 0;
852 if (uport->type == PORT_UNKNOWN)
853 goto exit;
854 if (port->flags & ASYNC_INITIALIZED) {
855 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
856 old_custom_divisor != uport->custom_divisor) {
857 /*
858 * If they're setting up a custom divisor or speed,
859 * instead of clearing it, then bitch about it. No
860 * need to rate-limit; it's CAP_SYS_ADMIN only.
861 */
862 if (uport->flags & UPF_SPD_MASK) {
863 char buf[64];
864 printk(KERN_NOTICE
865 "%s sets custom speed on %s. This "
866 "is deprecated.\n", current->comm,
867 tty_name(port->tty, buf));
868 }
869 uart_change_speed(tty, state, NULL);
870 }
871 } else
872 retval = uart_startup(tty, state, 1);
873 exit:
874 mutex_unlock(&port->mutex);
875 return retval;
876}
877
878/**
879 * uart_get_lsr_info - get line status register info
880 * @tty: tty associated with the UART
881 * @state: UART being queried
882 * @value: returned modem value
883 *
884 * Note: uart_ioctl protects us against hangups.
885 */
886static int uart_get_lsr_info(struct tty_struct *tty,
887 struct uart_state *state, unsigned int __user *value)
888{
889 struct uart_port *uport = state->uart_port;
890 unsigned int result;
891
892 result = uport->ops->tx_empty(uport);
893
894 /*
895 * If we're about to load something into the transmit
896 * register, we'll pretend the transmitter isn't empty to
897 * avoid a race condition (depending on when the transmit
898 * interrupt happens).
899 */
900 if (uport->x_char ||
901 ((uart_circ_chars_pending(&state->xmit) > 0) &&
902 !tty->stopped && !tty->hw_stopped))
903 result &= ~TIOCSER_TEMT;
904
905 return put_user(result, value);
906}
907
908static int uart_tiocmget(struct tty_struct *tty, struct file *file)
909{
910 struct uart_state *state = tty->driver_data;
911 struct tty_port *port = &state->port;
912 struct uart_port *uport = state->uart_port;
913 int result = -EIO;
914
915 mutex_lock(&port->mutex);
916 if ((!file || !tty_hung_up_p(file)) &&
917 !(tty->flags & (1 << TTY_IO_ERROR))) {
918 result = uport->mctrl;
919
920 spin_lock_irq(&uport->lock);
921 result |= uport->ops->get_mctrl(uport);
922 spin_unlock_irq(&uport->lock);
923 }
924 mutex_unlock(&port->mutex);
925
926 return result;
927}
928
929static int
930uart_tiocmset(struct tty_struct *tty, struct file *file,
931 unsigned int set, unsigned int clear)
932{
933 struct uart_state *state = tty->driver_data;
934 struct uart_port *uport = state->uart_port;
935 struct tty_port *port = &state->port;
936 int ret = -EIO;
937
938 mutex_lock(&port->mutex);
939 if ((!file || !tty_hung_up_p(file)) &&
940 !(tty->flags & (1 << TTY_IO_ERROR))) {
941 uart_update_mctrl(uport, set, clear);
942 ret = 0;
943 }
944 mutex_unlock(&port->mutex);
945 return ret;
946}
947
948static int uart_break_ctl(struct tty_struct *tty, int break_state)
949{
950 struct uart_state *state = tty->driver_data;
951 struct tty_port *port = &state->port;
952 struct uart_port *uport = state->uart_port;
953
954 mutex_lock(&port->mutex);
955
956 if (uport->type != PORT_UNKNOWN)
957 uport->ops->break_ctl(uport, break_state);
958
959 mutex_unlock(&port->mutex);
960 return 0;
961}
962
963static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
964{
965 struct uart_port *uport = state->uart_port;
966 struct tty_port *port = &state->port;
967 int flags, ret;
968
969 if (!capable(CAP_SYS_ADMIN))
970 return -EPERM;
971
972 /*
973 * Take the per-port semaphore. This prevents count from
974 * changing, and hence any extra opens of the port while
975 * we're auto-configuring.
976 */
977 if (mutex_lock_interruptible(&port->mutex))
978 return -ERESTARTSYS;
979
980 ret = -EBUSY;
981 if (tty_port_users(port) == 1) {
982 uart_shutdown(tty, state);
983
984 /*
985 * If we already have a port type configured,
986 * we must release its resources.
987 */
988 if (uport->type != PORT_UNKNOWN)
989 uport->ops->release_port(uport);
990
991 flags = UART_CONFIG_TYPE;
992 if (uport->flags & UPF_AUTO_IRQ)
993 flags |= UART_CONFIG_IRQ;
994
995 /*
996 * This will claim the ports resources if
997 * a port is found.
998 */
999 uport->ops->config_port(uport, flags);
1000
1001 ret = uart_startup(tty, state, 1);
1002 }
1003 mutex_unlock(&port->mutex);
1004 return ret;
1005}
1006
1007/*
1008 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1009 * - mask passed in arg for lines of interest
1010 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1011 * Caller should use TIOCGICOUNT to see which one it was
1012 *
1013 * FIXME: This wants extracting into a common all driver implementation
1014 * of TIOCMWAIT using tty_port.
1015 */
1016static int
1017uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1018{
1019 struct uart_port *uport = state->uart_port;
1020 struct tty_port *port = &state->port;
1021 DECLARE_WAITQUEUE(wait, current);
1022 struct uart_icount cprev, cnow;
1023 int ret;
1024
1025 /*
1026 * note the counters on entry
1027 */
1028 spin_lock_irq(&uport->lock);
1029 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1030
1031 /*
1032 * Force modem status interrupts on
1033 */
1034 uport->ops->enable_ms(uport);
1035 spin_unlock_irq(&uport->lock);
1036
1037 add_wait_queue(&port->delta_msr_wait, &wait);
1038 for (;;) {
1039 spin_lock_irq(&uport->lock);
1040 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1041 spin_unlock_irq(&uport->lock);
1042
1043 set_current_state(TASK_INTERRUPTIBLE);
1044
1045 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1046 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1047 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1048 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1049 ret = 0;
1050 break;
1051 }
1052
1053 schedule();
1054
1055 /* see if a signal did it */
1056 if (signal_pending(current)) {
1057 ret = -ERESTARTSYS;
1058 break;
1059 }
1060
1061 cprev = cnow;
1062 }
1063
1064 current->state = TASK_RUNNING;
1065 remove_wait_queue(&port->delta_msr_wait, &wait);
1066
1067 return ret;
1068}
1069
1070/*
1071 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1072 * Return: write counters to the user passed counter struct
1073 * NB: both 1->0 and 0->1 transitions are counted except for
1074 * RI where only 0->1 is counted.
1075 */
1076static int uart_get_icount(struct tty_struct *tty,
1077 struct serial_icounter_struct *icount)
1078{
1079 struct uart_state *state = tty->driver_data;
1080 struct uart_icount cnow;
1081 struct uart_port *uport = state->uart_port;
1082
1083 spin_lock_irq(&uport->lock);
1084 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1085 spin_unlock_irq(&uport->lock);
1086
1087 icount->cts = cnow.cts;
1088 icount->dsr = cnow.dsr;
1089 icount->rng = cnow.rng;
1090 icount->dcd = cnow.dcd;
1091 icount->rx = cnow.rx;
1092 icount->tx = cnow.tx;
1093 icount->frame = cnow.frame;
1094 icount->overrun = cnow.overrun;
1095 icount->parity = cnow.parity;
1096 icount->brk = cnow.brk;
1097 icount->buf_overrun = cnow.buf_overrun;
1098
1099 return 0;
1100}
1101
1102/*
1103 * Called via sys_ioctl. We can use spin_lock_irq() here.
1104 */
1105static int
1106uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1107 unsigned long arg)
1108{
1109 struct uart_state *state = tty->driver_data;
1110 struct tty_port *port = &state->port;
1111 void __user *uarg = (void __user *)arg;
1112 int ret = -ENOIOCTLCMD;
1113
1114
1115 /*
1116 * These ioctls don't rely on the hardware to be present.
1117 */
1118 switch (cmd) {
1119 case TIOCGSERIAL:
1120 ret = uart_get_info(state, uarg);
1121 break;
1122
1123 case TIOCSSERIAL:
1124 ret = uart_set_info(tty, state, uarg);
1125 break;
1126
1127 case TIOCSERCONFIG:
1128 ret = uart_do_autoconfig(tty, state);
1129 break;
1130
1131 case TIOCSERGWILD: /* obsolete */
1132 case TIOCSERSWILD: /* obsolete */
1133 ret = 0;
1134 break;
1135 }
1136
1137 if (ret != -ENOIOCTLCMD)
1138 goto out;
1139
1140 if (tty->flags & (1 << TTY_IO_ERROR)) {
1141 ret = -EIO;
1142 goto out;
1143 }
1144
1145 /*
1146 * The following should only be used when hardware is present.
1147 */
1148 switch (cmd) {
1149 case TIOCMIWAIT:
1150 ret = uart_wait_modem_status(state, arg);
1151 break;
1152 }
1153
1154 if (ret != -ENOIOCTLCMD)
1155 goto out;
1156
1157 mutex_lock(&port->mutex);
1158
1159 if (tty_hung_up_p(filp)) {
1160 ret = -EIO;
1161 goto out_up;
1162 }
1163
1164 /*
1165 * All these rely on hardware being present and need to be
1166 * protected against the tty being hung up.
1167 */
1168 switch (cmd) {
1169 case TIOCSERGETLSR: /* Get line status register */
1170 ret = uart_get_lsr_info(tty, state, uarg);
1171 break;
1172
1173 default: {
1174 struct uart_port *uport = state->uart_port;
1175 if (uport->ops->ioctl)
1176 ret = uport->ops->ioctl(uport, cmd, arg);
1177 break;
1178 }
1179 }
1180out_up:
1181 mutex_unlock(&port->mutex);
1182out:
1183 return ret;
1184}
1185
1186static void uart_set_ldisc(struct tty_struct *tty)
1187{
1188 struct uart_state *state = tty->driver_data;
1189 struct uart_port *uport = state->uart_port;
1190
1191 if (uport->ops->set_ldisc)
1192 uport->ops->set_ldisc(uport, tty->termios->c_line);
1193}
1194
1195static void uart_set_termios(struct tty_struct *tty,
1196 struct ktermios *old_termios)
1197{
1198 struct uart_state *state = tty->driver_data;
1199 unsigned long flags;
1200 unsigned int cflag = tty->termios->c_cflag;
1201
1202
1203 /*
1204 * These are the bits that are used to setup various
1205 * flags in the low level driver. We can ignore the Bfoo
1206 * bits in c_cflag; c_[io]speed will always be set
1207 * appropriately by set_termios() in tty_ioctl.c
1208 */
1209#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1210 if ((cflag ^ old_termios->c_cflag) == 0 &&
1211 tty->termios->c_ospeed == old_termios->c_ospeed &&
1212 tty->termios->c_ispeed == old_termios->c_ispeed &&
1213 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
1214 return;
1215 }
1216
1217 uart_change_speed(tty, state, old_termios);
1218
1219 /* Handle transition to B0 status */
1220 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1221 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
1222 /* Handle transition away from B0 status */
1223 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1224 unsigned int mask = TIOCM_DTR;
1225 if (!(cflag & CRTSCTS) ||
1226 !test_bit(TTY_THROTTLED, &tty->flags))
1227 mask |= TIOCM_RTS;
1228 uart_set_mctrl(state->uart_port, mask);
1229 }
1230
1231 /* Handle turning off CRTSCTS */
1232 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1233 spin_lock_irqsave(&state->uart_port->lock, flags);
1234 tty->hw_stopped = 0;
1235 __uart_start(tty);
1236 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1237 }
1238 /* Handle turning on CRTSCTS */
1239 else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1240 spin_lock_irqsave(&state->uart_port->lock, flags);
1241 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
1242 tty->hw_stopped = 1;
1243 state->uart_port->ops->stop_tx(state->uart_port);
1244 }
1245 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1246 }
1247#if 0
1248 /*
1249 * No need to wake up processes in open wait, since they
1250 * sample the CLOCAL flag once, and don't recheck it.
1251 * XXX It's not clear whether the current behavior is correct
1252 * or not. Hence, this may change.....
1253 */
1254 if (!(old_termios->c_cflag & CLOCAL) &&
1255 (tty->termios->c_cflag & CLOCAL))
1256 wake_up_interruptible(&state->uart_port.open_wait);
1257#endif
1258}
1259
1260/*
1261 * In 2.4.5, calls to this will be serialized via the BKL in
1262 * linux/drivers/char/tty_io.c:tty_release()
1263 * linux/drivers/char/tty_io.c:do_tty_handup()
1264 */
1265static void uart_close(struct tty_struct *tty, struct file *filp)
1266{
1267 struct uart_state *state = tty->driver_data;
1268 struct tty_port *port;
1269 struct uart_port *uport;
1270 unsigned long flags;
1271
1272 BUG_ON(!tty_locked());
1273
1274 if (!state)
1275 return;
1276
1277 uport = state->uart_port;
1278 port = &state->port;
1279
1280 pr_debug("uart_close(%d) called\n", uport->line);
1281
1282 mutex_lock(&port->mutex);
1283 spin_lock_irqsave(&port->lock, flags);
1284
1285 if (tty_hung_up_p(filp)) {
1286 spin_unlock_irqrestore(&port->lock, flags);
1287 goto done;
1288 }
1289
1290 if ((tty->count == 1) && (port->count != 1)) {
1291 /*
1292 * Uh, oh. tty->count is 1, which means that the tty
1293 * structure will be freed. port->count should always
1294 * be one in these conditions. If it's greater than
1295 * one, we've got real problems, since it means the
1296 * serial port won't be shutdown.
1297 */
1298 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1299 "port->count is %d\n", port->count);
1300 port->count = 1;
1301 }
1302 if (--port->count < 0) {
1303 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1304 tty->name, port->count);
1305 port->count = 0;
1306 }
1307 if (port->count) {
1308 spin_unlock_irqrestore(&port->lock, flags);
1309 goto done;
1310 }
1311
1312 /*
1313 * Now we wait for the transmit buffer to clear; and we notify
1314 * the line discipline to only process XON/XOFF characters by
1315 * setting tty->closing.
1316 */
1317 tty->closing = 1;
1318 spin_unlock_irqrestore(&port->lock, flags);
1319
1320 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
1321 /*
1322 * hack: open-coded tty_wait_until_sent to avoid
1323 * recursive tty_lock
1324 */
1325 long timeout = msecs_to_jiffies(port->closing_wait);
1326 if (wait_event_interruptible_timeout(tty->write_wait,
1327 !tty_chars_in_buffer(tty), timeout) >= 0)
1328 __uart_wait_until_sent(uport, timeout);
1329 }
1330
1331 /*
1332 * At this point, we stop accepting input. To do this, we
1333 * disable the receive line status interrupts.
1334 */
1335 if (port->flags & ASYNC_INITIALIZED) {
1336 unsigned long flags;
1337 spin_lock_irqsave(&uport->lock, flags);
1338 uport->ops->stop_rx(uport);
1339 spin_unlock_irqrestore(&uport->lock, flags);
1340 /*
1341 * Before we drop DTR, make sure the UART transmitter
1342 * has completely drained; this is especially
1343 * important if there is a transmit FIFO!
1344 */
1345 __uart_wait_until_sent(uport, uport->timeout);
1346 }
1347
1348 uart_shutdown(tty, state);
1349 uart_flush_buffer(tty);
1350
1351 tty_ldisc_flush(tty);
1352
1353 tty_port_tty_set(port, NULL);
1354 spin_lock_irqsave(&port->lock, flags);
1355 tty->closing = 0;
1356
1357 if (port->blocked_open) {
1358 spin_unlock_irqrestore(&port->lock, flags);
1359 if (port->close_delay)
1360 msleep_interruptible(port->close_delay);
1361 spin_lock_irqsave(&port->lock, flags);
1362 } else if (!uart_console(uport)) {
1363 spin_unlock_irqrestore(&port->lock, flags);
1364 uart_change_pm(state, 3);
1365 spin_lock_irqsave(&port->lock, flags);
1366 }
1367
1368 /*
1369 * Wake up anyone trying to open this port.
1370 */
1371 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1372 spin_unlock_irqrestore(&port->lock, flags);
1373 wake_up_interruptible(&port->open_wait);
1374
1375done:
1376 mutex_unlock(&port->mutex);
1377}
1378
1379static void __uart_wait_until_sent(struct uart_port *port, int timeout)
1380{
1381 unsigned long char_time, expire;
1382
1383 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1384 return;
1385
1386 /*
1387 * Set the check interval to be 1/5 of the estimated time to
1388 * send a single character, and make it at least 1. The check
1389 * interval should also be less than the timeout.
1390 *
1391 * Note: we have to use pretty tight timings here to satisfy
1392 * the NIST-PCTS.
1393 */
1394 char_time = (port->timeout - HZ/50) / port->fifosize;
1395 char_time = char_time / 5;
1396 if (char_time == 0)
1397 char_time = 1;
1398 if (timeout && timeout < char_time)
1399 char_time = timeout;
1400
1401 /*
1402 * If the transmitter hasn't cleared in twice the approximate
1403 * amount of time to send the entire FIFO, it probably won't
1404 * ever clear. This assumes the UART isn't doing flow
1405 * control, which is currently the case. Hence, if it ever
1406 * takes longer than port->timeout, this is probably due to a
1407 * UART bug of some kind. So, we clamp the timeout parameter at
1408 * 2*port->timeout.
1409 */
1410 if (timeout == 0 || timeout > 2 * port->timeout)
1411 timeout = 2 * port->timeout;
1412
1413 expire = jiffies + timeout;
1414
1415 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1416 port->line, jiffies, expire);
1417
1418 /*
1419 * Check whether the transmitter is empty every 'char_time'.
1420 * 'timeout' / 'expire' give us the maximum amount of time
1421 * we wait.
1422 */
1423 while (!port->ops->tx_empty(port)) {
1424 msleep_interruptible(jiffies_to_msecs(char_time));
1425 if (signal_pending(current))
1426 break;
1427 if (time_after(jiffies, expire))
1428 break;
1429 }
1430 set_current_state(TASK_RUNNING); /* might not be needed */
1431}
1432
1433static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1434{
1435 struct uart_state *state = tty->driver_data;
1436 struct uart_port *port = state->uart_port;
1437
1438 tty_lock();
1439 __uart_wait_until_sent(port, timeout);
1440 tty_unlock();
1441}
1442
1443/*
1444 * This is called with the BKL held in
1445 * linux/drivers/char/tty_io.c:do_tty_hangup()
1446 * We're called from the eventd thread, so we can sleep for
1447 * a _short_ time only.
1448 */
1449static void uart_hangup(struct tty_struct *tty)
1450{
1451 struct uart_state *state = tty->driver_data;
1452 struct tty_port *port = &state->port;
1453 unsigned long flags;
1454
1455 BUG_ON(!tty_locked());
1456 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1457
1458 mutex_lock(&port->mutex);
1459 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1460 uart_flush_buffer(tty);
1461 uart_shutdown(tty, state);
1462 spin_lock_irqsave(&port->lock, flags);
1463 port->count = 0;
1464 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1465 spin_unlock_irqrestore(&port->lock, flags);
1466 tty_port_tty_set(port, NULL);
1467 wake_up_interruptible(&port->open_wait);
1468 wake_up_interruptible(&port->delta_msr_wait);
1469 }
1470 mutex_unlock(&port->mutex);
1471}
1472
1473/**
1474 * uart_update_termios - update the terminal hw settings
1475 * @tty: tty associated with UART
1476 * @state: UART to update
1477 *
1478 * Copy across the serial console cflag setting into the termios settings
1479 * for the initial open of the port. This allows continuity between the
1480 * kernel settings, and the settings init adopts when it opens the port
1481 * for the first time.
1482 */
1483static void uart_update_termios(struct tty_struct *tty,
1484 struct uart_state *state)
1485{
1486 struct uart_port *port = state->uart_port;
1487
1488 if (uart_console(port) && port->cons->cflag) {
1489 tty->termios->c_cflag = port->cons->cflag;
1490 port->cons->cflag = 0;
1491 }
1492
1493 /*
1494 * If the device failed to grab its irq resources,
1495 * or some other error occurred, don't try to talk
1496 * to the port hardware.
1497 */
1498 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1499 /*
1500 * Make termios settings take effect.
1501 */
1502 uart_change_speed(tty, state, NULL);
1503
1504 /*
1505 * And finally enable the RTS and DTR signals.
1506 */
1507 if (tty->termios->c_cflag & CBAUD)
1508 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1509 }
1510}
1511
1512static int uart_carrier_raised(struct tty_port *port)
1513{
1514 struct uart_state *state = container_of(port, struct uart_state, port);
1515 struct uart_port *uport = state->uart_port;
1516 int mctrl;
1517 spin_lock_irq(&uport->lock);
1518 uport->ops->enable_ms(uport);
1519 mctrl = uport->ops->get_mctrl(uport);
1520 spin_unlock_irq(&uport->lock);
1521 if (mctrl & TIOCM_CAR)
1522 return 1;
1523 return 0;
1524}
1525
1526static void uart_dtr_rts(struct tty_port *port, int onoff)
1527{
1528 struct uart_state *state = container_of(port, struct uart_state, port);
1529 struct uart_port *uport = state->uart_port;
1530
1531 if (onoff) {
1532 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1533
1534 /*
1535 * If this is the first open to succeed,
1536 * adjust things to suit.
1537 */
1538 if (!test_and_set_bit(ASYNCB_NORMAL_ACTIVE, &port->flags))
1539 uart_update_termios(port->tty, state);
1540 }
1541 else
1542 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1543}
1544
1545static struct uart_state *uart_get(struct uart_driver *drv, int line)
1546{
1547 struct uart_state *state;
1548 struct tty_port *port;
1549 int ret = 0;
1550
1551 state = drv->state + line;
1552 port = &state->port;
1553 if (mutex_lock_interruptible(&port->mutex)) {
1554 ret = -ERESTARTSYS;
1555 goto err;
1556 }
1557
1558 port->count++;
1559 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1560 ret = -ENXIO;
1561 goto err_unlock;
1562 }
1563 return state;
1564
1565 err_unlock:
1566 port->count--;
1567 mutex_unlock(&port->mutex);
1568 err:
1569 return ERR_PTR(ret);
1570}
1571
1572/*
1573 * calls to uart_open are serialised by the BKL in
1574 * fs/char_dev.c:chrdev_open()
1575 * Note that if this fails, then uart_close() _will_ be called.
1576 *
1577 * In time, we want to scrap the "opening nonpresent ports"
1578 * behaviour and implement an alternative way for setserial
1579 * to set base addresses/ports/types. This will allow us to
1580 * get rid of a certain amount of extra tests.
1581 */
1582static int uart_open(struct tty_struct *tty, struct file *filp)
1583{
1584 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1585 struct uart_state *state;
1586 struct tty_port *port;
1587 int retval, line = tty->index;
1588
1589 BUG_ON(!tty_locked());
1590 pr_debug("uart_open(%d) called\n", line);
1591
1592 /*
1593 * tty->driver->num won't change, so we won't fail here with
1594 * tty->driver_data set to something non-NULL (and therefore
1595 * we won't get caught by uart_close()).
1596 */
1597 retval = -ENODEV;
1598 if (line >= tty->driver->num)
1599 goto fail;
1600
1601 /*
1602 * We take the semaphore inside uart_get to guarantee that we won't
1603 * be re-entered while allocating the state structure, or while we
1604 * request any IRQs that the driver may need. This also has the nice
1605 * side-effect that it delays the action of uart_hangup, so we can
1606 * guarantee that state->port.tty will always contain something
1607 * reasonable.
1608 */
1609 state = uart_get(drv, line);
1610 if (IS_ERR(state)) {
1611 retval = PTR_ERR(state);
1612 goto fail;
1613 }
1614 port = &state->port;
1615
1616 /*
1617 * Once we set tty->driver_data here, we are guaranteed that
1618 * uart_close() will decrement the driver module use count.
1619 * Any failures from here onwards should not touch the count.
1620 */
1621 tty->driver_data = state;
1622 state->uart_port->state = state;
1623 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1624 tty->alt_speed = 0;
1625 tty_port_tty_set(port, tty);
1626
1627 /*
1628 * If the port is in the middle of closing, bail out now.
1629 */
1630 if (tty_hung_up_p(filp)) {
1631 retval = -EAGAIN;
1632 port->count--;
1633 mutex_unlock(&port->mutex);
1634 goto fail;
1635 }
1636
1637 /*
1638 * Make sure the device is in D0 state.
1639 */
1640 if (port->count == 1)
1641 uart_change_pm(state, 0);
1642
1643 /*
1644 * Start up the serial port.
1645 */
1646 retval = uart_startup(tty, state, 0);
1647
1648 /*
1649 * If we succeeded, wait until the port is ready.
1650 */
1651 mutex_unlock(&port->mutex);
1652 if (retval == 0)
1653 retval = tty_port_block_til_ready(port, tty, filp);
1654
1655fail:
1656 return retval;
1657}
1658
1659static const char *uart_type(struct uart_port *port)
1660{
1661 const char *str = NULL;
1662
1663 if (port->ops->type)
1664 str = port->ops->type(port);
1665
1666 if (!str)
1667 str = "unknown";
1668
1669 return str;
1670}
1671
1672#ifdef CONFIG_PROC_FS
1673
1674static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1675{
1676 struct uart_state *state = drv->state + i;
1677 struct tty_port *port = &state->port;
1678 int pm_state;
1679 struct uart_port *uport = state->uart_port;
1680 char stat_buf[32];
1681 unsigned int status;
1682 int mmio;
1683
1684 if (!uport)
1685 return;
1686
1687 mmio = uport->iotype >= UPIO_MEM;
1688 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1689 uport->line, uart_type(uport),
1690 mmio ? "mmio:0x" : "port:",
1691 mmio ? (unsigned long long)uport->mapbase
1692 : (unsigned long long)uport->iobase,
1693 uport->irq);
1694
1695 if (uport->type == PORT_UNKNOWN) {
1696 seq_putc(m, '\n');
1697 return;
1698 }
1699
1700 if (capable(CAP_SYS_ADMIN)) {
1701 mutex_lock(&port->mutex);
1702 pm_state = state->pm_state;
1703 if (pm_state)
1704 uart_change_pm(state, 0);
1705 spin_lock_irq(&uport->lock);
1706 status = uport->ops->get_mctrl(uport);
1707 spin_unlock_irq(&uport->lock);
1708 if (pm_state)
1709 uart_change_pm(state, pm_state);
1710 mutex_unlock(&port->mutex);
1711
1712 seq_printf(m, " tx:%d rx:%d",
1713 uport->icount.tx, uport->icount.rx);
1714 if (uport->icount.frame)
1715 seq_printf(m, " fe:%d",
1716 uport->icount.frame);
1717 if (uport->icount.parity)
1718 seq_printf(m, " pe:%d",
1719 uport->icount.parity);
1720 if (uport->icount.brk)
1721 seq_printf(m, " brk:%d",
1722 uport->icount.brk);
1723 if (uport->icount.overrun)
1724 seq_printf(m, " oe:%d",
1725 uport->icount.overrun);
1726
1727#define INFOBIT(bit, str) \
1728 if (uport->mctrl & (bit)) \
1729 strncat(stat_buf, (str), sizeof(stat_buf) - \
1730 strlen(stat_buf) - 2)
1731#define STATBIT(bit, str) \
1732 if (status & (bit)) \
1733 strncat(stat_buf, (str), sizeof(stat_buf) - \
1734 strlen(stat_buf) - 2)
1735
1736 stat_buf[0] = '\0';
1737 stat_buf[1] = '\0';
1738 INFOBIT(TIOCM_RTS, "|RTS");
1739 STATBIT(TIOCM_CTS, "|CTS");
1740 INFOBIT(TIOCM_DTR, "|DTR");
1741 STATBIT(TIOCM_DSR, "|DSR");
1742 STATBIT(TIOCM_CAR, "|CD");
1743 STATBIT(TIOCM_RNG, "|RI");
1744 if (stat_buf[0])
1745 stat_buf[0] = ' ';
1746
1747 seq_puts(m, stat_buf);
1748 }
1749 seq_putc(m, '\n');
1750#undef STATBIT
1751#undef INFOBIT
1752}
1753
1754static int uart_proc_show(struct seq_file *m, void *v)
1755{
1756 struct tty_driver *ttydrv = m->private;
1757 struct uart_driver *drv = ttydrv->driver_state;
1758 int i;
1759
1760 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1761 "", "", "");
1762 for (i = 0; i < drv->nr; i++)
1763 uart_line_info(m, drv, i);
1764 return 0;
1765}
1766
1767static int uart_proc_open(struct inode *inode, struct file *file)
1768{
1769 return single_open(file, uart_proc_show, PDE(inode)->data);
1770}
1771
1772static const struct file_operations uart_proc_fops = {
1773 .owner = THIS_MODULE,
1774 .open = uart_proc_open,
1775 .read = seq_read,
1776 .llseek = seq_lseek,
1777 .release = single_release,
1778};
1779#endif
1780
1781#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1782/*
1783 * uart_console_write - write a console message to a serial port
1784 * @port: the port to write the message
1785 * @s: array of characters
1786 * @count: number of characters in string to write
1787 * @write: function to write character to port
1788 */
1789void uart_console_write(struct uart_port *port, const char *s,
1790 unsigned int count,
1791 void (*putchar)(struct uart_port *, int))
1792{
1793 unsigned int i;
1794
1795 for (i = 0; i < count; i++, s++) {
1796 if (*s == '\n')
1797 putchar(port, '\r');
1798 putchar(port, *s);
1799 }
1800}
1801EXPORT_SYMBOL_GPL(uart_console_write);
1802
1803/*
1804 * Check whether an invalid uart number has been specified, and
1805 * if so, search for the first available port that does have
1806 * console support.
1807 */
1808struct uart_port * __init
1809uart_get_console(struct uart_port *ports, int nr, struct console *co)
1810{
1811 int idx = co->index;
1812
1813 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1814 ports[idx].membase == NULL))
1815 for (idx = 0; idx < nr; idx++)
1816 if (ports[idx].iobase != 0 ||
1817 ports[idx].membase != NULL)
1818 break;
1819
1820 co->index = idx;
1821
1822 return ports + idx;
1823}
1824
1825/**
1826 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1827 * @options: pointer to option string
1828 * @baud: pointer to an 'int' variable for the baud rate.
1829 * @parity: pointer to an 'int' variable for the parity.
1830 * @bits: pointer to an 'int' variable for the number of data bits.
1831 * @flow: pointer to an 'int' variable for the flow control character.
1832 *
1833 * uart_parse_options decodes a string containing the serial console
1834 * options. The format of the string is <baud><parity><bits><flow>,
1835 * eg: 115200n8r
1836 */
1837void
1838uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1839{
1840 char *s = options;
1841
1842 *baud = simple_strtoul(s, NULL, 10);
1843 while (*s >= '0' && *s <= '9')
1844 s++;
1845 if (*s)
1846 *parity = *s++;
1847 if (*s)
1848 *bits = *s++ - '0';
1849 if (*s)
1850 *flow = *s;
1851}
1852EXPORT_SYMBOL_GPL(uart_parse_options);
1853
1854struct baud_rates {
1855 unsigned int rate;
1856 unsigned int cflag;
1857};
1858
1859static const struct baud_rates baud_rates[] = {
1860 { 921600, B921600 },
1861 { 460800, B460800 },
1862 { 230400, B230400 },
1863 { 115200, B115200 },
1864 { 57600, B57600 },
1865 { 38400, B38400 },
1866 { 19200, B19200 },
1867 { 9600, B9600 },
1868 { 4800, B4800 },
1869 { 2400, B2400 },
1870 { 1200, B1200 },
1871 { 0, B38400 }
1872};
1873
1874/**
1875 * uart_set_options - setup the serial console parameters
1876 * @port: pointer to the serial ports uart_port structure
1877 * @co: console pointer
1878 * @baud: baud rate
1879 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1880 * @bits: number of data bits
1881 * @flow: flow control character - 'r' (rts)
1882 */
1883int
1884uart_set_options(struct uart_port *port, struct console *co,
1885 int baud, int parity, int bits, int flow)
1886{
1887 struct ktermios termios;
1888 static struct ktermios dummy;
1889 int i;
1890
1891 /*
1892 * Ensure that the serial console lock is initialised
1893 * early.
1894 */
1895 spin_lock_init(&port->lock);
1896 lockdep_set_class(&port->lock, &port_lock_key);
1897
1898 memset(&termios, 0, sizeof(struct ktermios));
1899
1900 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1901
1902 /*
1903 * Construct a cflag setting.
1904 */
1905 for (i = 0; baud_rates[i].rate; i++)
1906 if (baud_rates[i].rate <= baud)
1907 break;
1908
1909 termios.c_cflag |= baud_rates[i].cflag;
1910
1911 if (bits == 7)
1912 termios.c_cflag |= CS7;
1913 else
1914 termios.c_cflag |= CS8;
1915
1916 switch (parity) {
1917 case 'o': case 'O':
1918 termios.c_cflag |= PARODD;
1919 /*fall through*/
1920 case 'e': case 'E':
1921 termios.c_cflag |= PARENB;
1922 break;
1923 }
1924
1925 if (flow == 'r')
1926 termios.c_cflag |= CRTSCTS;
1927
1928 /*
1929 * some uarts on other side don't support no flow control.
1930 * So we set * DTR in host uart to make them happy
1931 */
1932 port->mctrl |= TIOCM_DTR;
1933
1934 port->ops->set_termios(port, &termios, &dummy);
1935 /*
1936 * Allow the setting of the UART parameters with a NULL console
1937 * too:
1938 */
1939 if (co)
1940 co->cflag = termios.c_cflag;
1941
1942 return 0;
1943}
1944EXPORT_SYMBOL_GPL(uart_set_options);
1945#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1946
1947static void uart_change_pm(struct uart_state *state, int pm_state)
1948{
1949 struct uart_port *port = state->uart_port;
1950
1951 if (state->pm_state != pm_state) {
1952 if (port->ops->pm)
1953 port->ops->pm(port, pm_state, state->pm_state);
1954 state->pm_state = pm_state;
1955 }
1956}
1957
1958struct uart_match {
1959 struct uart_port *port;
1960 struct uart_driver *driver;
1961};
1962
1963static int serial_match_port(struct device *dev, void *data)
1964{
1965 struct uart_match *match = data;
1966 struct tty_driver *tty_drv = match->driver->tty_driver;
1967 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1968 match->port->line;
1969
1970 return dev->devt == devt; /* Actually, only one tty per port */
1971}
1972
1973int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1974{
1975 struct uart_state *state = drv->state + uport->line;
1976 struct tty_port *port = &state->port;
1977 struct device *tty_dev;
1978 struct uart_match match = {uport, drv};
1979 struct tty_struct *tty;
1980
1981 mutex_lock(&port->mutex);
1982
1983 /* Must be inside the mutex lock until we convert to tty_port */
1984 tty = port->tty;
1985
1986 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1987 if (device_may_wakeup(tty_dev)) {
1988 if (!enable_irq_wake(uport->irq))
1989 uport->irq_wake = 1;
1990 put_device(tty_dev);
1991 mutex_unlock(&port->mutex);
1992 return 0;
1993 }
1994 if (console_suspend_enabled || !uart_console(uport))
1995 uport->suspended = 1;
1996
1997 if (port->flags & ASYNC_INITIALIZED) {
1998 const struct uart_ops *ops = uport->ops;
1999 int tries;
2000
2001 if (console_suspend_enabled || !uart_console(uport)) {
2002 set_bit(ASYNCB_SUSPENDED, &port->flags);
2003 clear_bit(ASYNCB_INITIALIZED, &port->flags);
2004
2005 spin_lock_irq(&uport->lock);
2006 ops->stop_tx(uport);
2007 ops->set_mctrl(uport, 0);
2008 ops->stop_rx(uport);
2009 spin_unlock_irq(&uport->lock);
2010 }
2011
2012 /*
2013 * Wait for the transmitter to empty.
2014 */
2015 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2016 msleep(10);
2017 if (!tries)
2018 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2019 "transmitter\n",
2020 uport->dev ? dev_name(uport->dev) : "",
2021 uport->dev ? ": " : "",
2022 drv->dev_name,
2023 drv->tty_driver->name_base + uport->line);
2024
2025 if (console_suspend_enabled || !uart_console(uport))
2026 ops->shutdown(uport);
2027 }
2028
2029 /*
2030 * Disable the console device before suspending.
2031 */
2032 if (console_suspend_enabled && uart_console(uport))
2033 console_stop(uport->cons);
2034
2035 if (console_suspend_enabled || !uart_console(uport))
2036 uart_change_pm(state, 3);
2037
2038 mutex_unlock(&port->mutex);
2039
2040 return 0;
2041}
2042
2043int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2044{
2045 struct uart_state *state = drv->state + uport->line;
2046 struct tty_port *port = &state->port;
2047 struct device *tty_dev;
2048 struct uart_match match = {uport, drv};
2049 struct ktermios termios;
2050
2051 mutex_lock(&port->mutex);
2052
2053 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2054 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2055 if (uport->irq_wake) {
2056 disable_irq_wake(uport->irq);
2057 uport->irq_wake = 0;
2058 }
2059 mutex_unlock(&port->mutex);
2060 return 0;
2061 }
2062 uport->suspended = 0;
2063
2064 /*
2065 * Re-enable the console device after suspending.
2066 */
2067 if (console_suspend_enabled && uart_console(uport)) {
2068 /*
2069 * First try to use the console cflag setting.
2070 */
2071 memset(&termios, 0, sizeof(struct ktermios));
2072 termios.c_cflag = uport->cons->cflag;
2073
2074 /*
2075 * If that's unset, use the tty termios setting.
2076 */
2077 if (port->tty && port->tty->termios && termios.c_cflag == 0)
2078 termios = *(port->tty->termios);
2079
2080 uart_change_pm(state, 0);
2081 uport->ops->set_termios(uport, &termios, NULL);
2082 console_start(uport->cons);
2083 }
2084
2085 if (port->flags & ASYNC_SUSPENDED) {
2086 const struct uart_ops *ops = uport->ops;
2087 int ret;
2088
2089 uart_change_pm(state, 0);
2090 spin_lock_irq(&uport->lock);
2091 ops->set_mctrl(uport, 0);
2092 spin_unlock_irq(&uport->lock);
2093 if (console_suspend_enabled || !uart_console(uport)) {
2094 /* Protected by port mutex for now */
2095 struct tty_struct *tty = port->tty;
2096 ret = ops->startup(uport);
2097 if (ret == 0) {
2098 if (tty)
2099 uart_change_speed(tty, state, NULL);
2100 spin_lock_irq(&uport->lock);
2101 ops->set_mctrl(uport, uport->mctrl);
2102 ops->start_tx(uport);
2103 spin_unlock_irq(&uport->lock);
2104 set_bit(ASYNCB_INITIALIZED, &port->flags);
2105 } else {
2106 /*
2107 * Failed to resume - maybe hardware went away?
2108 * Clear the "initialized" flag so we won't try
2109 * to call the low level drivers shutdown method.
2110 */
2111 uart_shutdown(tty, state);
2112 }
2113 }
2114
2115 clear_bit(ASYNCB_SUSPENDED, &port->flags);
2116 }
2117
2118 mutex_unlock(&port->mutex);
2119
2120 return 0;
2121}
2122
2123static inline void
2124uart_report_port(struct uart_driver *drv, struct uart_port *port)
2125{
2126 char address[64];
2127
2128 switch (port->iotype) {
2129 case UPIO_PORT:
2130 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2131 break;
2132 case UPIO_HUB6:
2133 snprintf(address, sizeof(address),
2134 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2135 break;
2136 case UPIO_MEM:
2137 case UPIO_MEM32:
2138 case UPIO_AU:
2139 case UPIO_TSI:
2140 case UPIO_DWAPB:
2141 case UPIO_DWAPB32:
2142 snprintf(address, sizeof(address),
2143 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2144 break;
2145 default:
2146 strlcpy(address, "*unknown*", sizeof(address));
2147 break;
2148 }
2149
2150 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2151 port->dev ? dev_name(port->dev) : "",
2152 port->dev ? ": " : "",
2153 drv->dev_name,
2154 drv->tty_driver->name_base + port->line,
2155 address, port->irq, uart_type(port));
2156}
2157
2158static void
2159uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2160 struct uart_port *port)
2161{
2162 unsigned int flags;
2163
2164 /*
2165 * If there isn't a port here, don't do anything further.
2166 */
2167 if (!port->iobase && !port->mapbase && !port->membase)
2168 return;
2169
2170 /*
2171 * Now do the auto configuration stuff. Note that config_port
2172 * is expected to claim the resources and map the port for us.
2173 */
2174 flags = 0;
2175 if (port->flags & UPF_AUTO_IRQ)
2176 flags |= UART_CONFIG_IRQ;
2177 if (port->flags & UPF_BOOT_AUTOCONF) {
2178 if (!(port->flags & UPF_FIXED_TYPE)) {
2179 port->type = PORT_UNKNOWN;
2180 flags |= UART_CONFIG_TYPE;
2181 }
2182 port->ops->config_port(port, flags);
2183 }
2184
2185 if (port->type != PORT_UNKNOWN) {
2186 unsigned long flags;
2187
2188 uart_report_port(drv, port);
2189
2190 /* Power up port for set_mctrl() */
2191 uart_change_pm(state, 0);
2192
2193 /*
2194 * Ensure that the modem control lines are de-activated.
2195 * keep the DTR setting that is set in uart_set_options()
2196 * We probably don't need a spinlock around this, but
2197 */
2198 spin_lock_irqsave(&port->lock, flags);
2199 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2200 spin_unlock_irqrestore(&port->lock, flags);
2201
2202 /*
2203 * If this driver supports console, and it hasn't been
2204 * successfully registered yet, try to re-register it.
2205 * It may be that the port was not available.
2206 */
2207 if (port->cons && !(port->cons->flags & CON_ENABLED))
2208 register_console(port->cons);
2209
2210 /*
2211 * Power down all ports by default, except the
2212 * console if we have one.
2213 */
2214 if (!uart_console(port))
2215 uart_change_pm(state, 3);
2216 }
2217}
2218
2219#ifdef CONFIG_CONSOLE_POLL
2220
2221static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2222{
2223 struct uart_driver *drv = driver->driver_state;
2224 struct uart_state *state = drv->state + line;
2225 struct uart_port *port;
2226 int baud = 9600;
2227 int bits = 8;
2228 int parity = 'n';
2229 int flow = 'n';
2230
2231 if (!state || !state->uart_port)
2232 return -1;
2233
2234 port = state->uart_port;
2235 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2236 return -1;
2237
2238 if (options) {
2239 uart_parse_options(options, &baud, &parity, &bits, &flow);
2240 return uart_set_options(port, NULL, baud, parity, bits, flow);
2241 }
2242
2243 return 0;
2244}
2245
2246static int uart_poll_get_char(struct tty_driver *driver, int line)
2247{
2248 struct uart_driver *drv = driver->driver_state;
2249 struct uart_state *state = drv->state + line;
2250 struct uart_port *port;
2251
2252 if (!state || !state->uart_port)
2253 return -1;
2254
2255 port = state->uart_port;
2256 return port->ops->poll_get_char(port);
2257}
2258
2259static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2260{
2261 struct uart_driver *drv = driver->driver_state;
2262 struct uart_state *state = drv->state + line;
2263 struct uart_port *port;
2264
2265 if (!state || !state->uart_port)
2266 return;
2267
2268 port = state->uart_port;
2269 port->ops->poll_put_char(port, ch);
2270}
2271#endif
2272
2273static const struct tty_operations uart_ops = {
2274 .open = uart_open,
2275 .close = uart_close,
2276 .write = uart_write,
2277 .put_char = uart_put_char,
2278 .flush_chars = uart_flush_chars,
2279 .write_room = uart_write_room,
2280 .chars_in_buffer= uart_chars_in_buffer,
2281 .flush_buffer = uart_flush_buffer,
2282 .ioctl = uart_ioctl,
2283 .throttle = uart_throttle,
2284 .unthrottle = uart_unthrottle,
2285 .send_xchar = uart_send_xchar,
2286 .set_termios = uart_set_termios,
2287 .set_ldisc = uart_set_ldisc,
2288 .stop = uart_stop,
2289 .start = uart_start,
2290 .hangup = uart_hangup,
2291 .break_ctl = uart_break_ctl,
2292 .wait_until_sent= uart_wait_until_sent,
2293#ifdef CONFIG_PROC_FS
2294 .proc_fops = &uart_proc_fops,
2295#endif
2296 .tiocmget = uart_tiocmget,
2297 .tiocmset = uart_tiocmset,
2298 .get_icount = uart_get_icount,
2299#ifdef CONFIG_CONSOLE_POLL
2300 .poll_init = uart_poll_init,
2301 .poll_get_char = uart_poll_get_char,
2302 .poll_put_char = uart_poll_put_char,
2303#endif
2304};
2305
2306static const struct tty_port_operations uart_port_ops = {
2307 .carrier_raised = uart_carrier_raised,
2308 .dtr_rts = uart_dtr_rts,
2309};
2310
2311/**
2312 * uart_register_driver - register a driver with the uart core layer
2313 * @drv: low level driver structure
2314 *
2315 * Register a uart driver with the core driver. We in turn register
2316 * with the tty layer, and initialise the core driver per-port state.
2317 *
2318 * We have a proc file in /proc/tty/driver which is named after the
2319 * normal driver.
2320 *
2321 * drv->port should be NULL, and the per-port structures should be
2322 * registered using uart_add_one_port after this call has succeeded.
2323 */
2324int uart_register_driver(struct uart_driver *drv)
2325{
2326 struct tty_driver *normal;
2327 int i, retval;
2328
2329 BUG_ON(drv->state);
2330
2331 /*
2332 * Maybe we should be using a slab cache for this, especially if
2333 * we have a large number of ports to handle.
2334 */
2335 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2336 if (!drv->state)
2337 goto out;
2338
2339 normal = alloc_tty_driver(drv->nr);
2340 if (!normal)
2341 goto out_kfree;
2342
2343 drv->tty_driver = normal;
2344
2345 normal->owner = drv->owner;
2346 normal->driver_name = drv->driver_name;
2347 normal->name = drv->dev_name;
2348 normal->major = drv->major;
2349 normal->minor_start = drv->minor;
2350 normal->type = TTY_DRIVER_TYPE_SERIAL;
2351 normal->subtype = SERIAL_TYPE_NORMAL;
2352 normal->init_termios = tty_std_termios;
2353 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2354 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2355 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2356 normal->driver_state = drv;
2357 tty_set_operations(normal, &uart_ops);
2358
2359 /*
2360 * Initialise the UART state(s).
2361 */
2362 for (i = 0; i < drv->nr; i++) {
2363 struct uart_state *state = drv->state + i;
2364 struct tty_port *port = &state->port;
2365
2366 tty_port_init(port);
2367 port->ops = &uart_port_ops;
2368 port->close_delay = 500; /* .5 seconds */
2369 port->closing_wait = 30000; /* 30 seconds */
2370 tasklet_init(&state->tlet, uart_tasklet_action,
2371 (unsigned long)state);
2372 }
2373
2374 retval = tty_register_driver(normal);
2375 if (retval >= 0)
2376 return retval;
2377
2378 put_tty_driver(normal);
2379out_kfree:
2380 kfree(drv->state);
2381out:
2382 return -ENOMEM;
2383}
2384
2385/**
2386 * uart_unregister_driver - remove a driver from the uart core layer
2387 * @drv: low level driver structure
2388 *
2389 * Remove all references to a driver from the core driver. The low
2390 * level driver must have removed all its ports via the
2391 * uart_remove_one_port() if it registered them with uart_add_one_port().
2392 * (ie, drv->port == NULL)
2393 */
2394void uart_unregister_driver(struct uart_driver *drv)
2395{
2396 struct tty_driver *p = drv->tty_driver;
2397 tty_unregister_driver(p);
2398 put_tty_driver(p);
2399 kfree(drv->state);
2400 drv->tty_driver = NULL;
2401}
2402
2403struct tty_driver *uart_console_device(struct console *co, int *index)
2404{
2405 struct uart_driver *p = co->data;
2406 *index = co->index;
2407 return p->tty_driver;
2408}
2409
2410/**
2411 * uart_add_one_port - attach a driver-defined port structure
2412 * @drv: pointer to the uart low level driver structure for this port
2413 * @uport: uart port structure to use for this port.
2414 *
2415 * This allows the driver to register its own uart_port structure
2416 * with the core driver. The main purpose is to allow the low
2417 * level uart drivers to expand uart_port, rather than having yet
2418 * more levels of structures.
2419 */
2420int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2421{
2422 struct uart_state *state;
2423 struct tty_port *port;
2424 int ret = 0;
2425 struct device *tty_dev;
2426
2427 BUG_ON(in_interrupt());
2428
2429 if (uport->line >= drv->nr)
2430 return -EINVAL;
2431
2432 state = drv->state + uport->line;
2433 port = &state->port;
2434
2435 mutex_lock(&port_mutex);
2436 mutex_lock(&port->mutex);
2437 if (state->uart_port) {
2438 ret = -EINVAL;
2439 goto out;
2440 }
2441
2442 state->uart_port = uport;
2443 state->pm_state = -1;
2444
2445 uport->cons = drv->cons;
2446 uport->state = state;
2447
2448 /*
2449 * If this port is a console, then the spinlock is already
2450 * initialised.
2451 */
2452 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2453 spin_lock_init(&uport->lock);
2454 lockdep_set_class(&uport->lock, &port_lock_key);
2455 }
2456
2457 uart_configure_port(drv, state, uport);
2458
2459 /*
2460 * Register the port whether it's detected or not. This allows
2461 * setserial to be used to alter this ports parameters.
2462 */
2463 tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
2464 if (likely(!IS_ERR(tty_dev))) {
2465 device_init_wakeup(tty_dev, 1);
2466 device_set_wakeup_enable(tty_dev, 0);
2467 } else
2468 printk(KERN_ERR "Cannot register tty device on line %d\n",
2469 uport->line);
2470
2471 /*
2472 * Ensure UPF_DEAD is not set.
2473 */
2474 uport->flags &= ~UPF_DEAD;
2475
2476 out:
2477 mutex_unlock(&port->mutex);
2478 mutex_unlock(&port_mutex);
2479
2480 return ret;
2481}
2482
2483/**
2484 * uart_remove_one_port - detach a driver defined port structure
2485 * @drv: pointer to the uart low level driver structure for this port
2486 * @uport: uart port structure for this port
2487 *
2488 * This unhooks (and hangs up) the specified port structure from the
2489 * core driver. No further calls will be made to the low-level code
2490 * for this port.
2491 */
2492int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2493{
2494 struct uart_state *state = drv->state + uport->line;
2495 struct tty_port *port = &state->port;
2496
2497 BUG_ON(in_interrupt());
2498
2499 if (state->uart_port != uport)
2500 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2501 state->uart_port, uport);
2502
2503 mutex_lock(&port_mutex);
2504
2505 /*
2506 * Mark the port "dead" - this prevents any opens from
2507 * succeeding while we shut down the port.
2508 */
2509 mutex_lock(&port->mutex);
2510 uport->flags |= UPF_DEAD;
2511 mutex_unlock(&port->mutex);
2512
2513 /*
2514 * Remove the devices from the tty layer
2515 */
2516 tty_unregister_device(drv->tty_driver, uport->line);
2517
2518 if (port->tty)
2519 tty_vhangup(port->tty);
2520
2521 /*
2522 * Free the port IO and memory resources, if any.
2523 */
2524 if (uport->type != PORT_UNKNOWN)
2525 uport->ops->release_port(uport);
2526
2527 /*
2528 * Indicate that there isn't a port here anymore.
2529 */
2530 uport->type = PORT_UNKNOWN;
2531
2532 /*
2533 * Kill the tasklet, and free resources.
2534 */
2535 tasklet_kill(&state->tlet);
2536
2537 state->uart_port = NULL;
2538 mutex_unlock(&port_mutex);
2539
2540 return 0;
2541}
2542
2543/*
2544 * Are the two ports equivalent?
2545 */
2546int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2547{
2548 if (port1->iotype != port2->iotype)
2549 return 0;
2550
2551 switch (port1->iotype) {
2552 case UPIO_PORT:
2553 return (port1->iobase == port2->iobase);
2554 case UPIO_HUB6:
2555 return (port1->iobase == port2->iobase) &&
2556 (port1->hub6 == port2->hub6);
2557 case UPIO_MEM:
2558 case UPIO_MEM32:
2559 case UPIO_AU:
2560 case UPIO_TSI:
2561 case UPIO_DWAPB:
2562 case UPIO_DWAPB32:
2563 return (port1->mapbase == port2->mapbase);
2564 }
2565 return 0;
2566}
2567EXPORT_SYMBOL(uart_match_port);
2568
2569EXPORT_SYMBOL(uart_write_wakeup);
2570EXPORT_SYMBOL(uart_register_driver);
2571EXPORT_SYMBOL(uart_unregister_driver);
2572EXPORT_SYMBOL(uart_suspend_port);
2573EXPORT_SYMBOL(uart_resume_port);
2574EXPORT_SYMBOL(uart_add_one_port);
2575EXPORT_SYMBOL(uart_remove_one_port);
2576
2577MODULE_DESCRIPTION("Serial driver core");
2578MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/serial_cs.c b/drivers/tty/serial/serial_cs.c
new file mode 100644
index 000000000000..93760b2ea172
--- /dev/null
+++ b/drivers/tty/serial/serial_cs.c
@@ -0,0 +1,869 @@
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/ptrace.h>
39#include <linux/slab.h>
40#include <linux/string.h>
41#include <linux/timer.h>
42#include <linux/serial_core.h>
43#include <linux/delay.h>
44#include <linux/major.h>
45#include <asm/io.h>
46#include <asm/system.h>
47
48#include <pcmcia/cistpl.h>
49#include <pcmcia/ciscode.h>
50#include <pcmcia/ds.h>
51#include <pcmcia/cisreg.h>
52
53#include "8250.h"
54
55
56/*====================================================================*/
57
58/* Parameters that can be set with 'insmod' */
59
60/* Enable the speaker? */
61static int do_sound = 1;
62/* Skip strict UART tests? */
63static int buggy_uart;
64
65module_param(do_sound, int, 0444);
66module_param(buggy_uart, int, 0444);
67
68/*====================================================================*/
69
70/* Table of multi-port card ID's */
71
72struct serial_quirk {
73 unsigned int manfid;
74 unsigned int prodid;
75 int multi; /* 1 = multifunction, > 1 = # ports */
76 void (*config)(struct pcmcia_device *);
77 void (*setup)(struct pcmcia_device *, struct uart_port *);
78 void (*wakeup)(struct pcmcia_device *);
79 int (*post)(struct pcmcia_device *);
80};
81
82struct serial_info {
83 struct pcmcia_device *p_dev;
84 int ndev;
85 int multi;
86 int slave;
87 int manfid;
88 int prodid;
89 int c950ctrl;
90 int line[4];
91 const struct serial_quirk *quirk;
92};
93
94struct serial_cfg_mem {
95 tuple_t tuple;
96 cisparse_t parse;
97 u_char buf[256];
98};
99
100/*
101 * vers_1 5.0, "Brain Boxes", "2-Port RS232 card", "r6"
102 * manfid 0x0160, 0x0104
103 * This card appears to have a 14.7456MHz clock.
104 */
105/* Generic Modem: MD55x (GPRS/EDGE) have
106 * Elan VPU16551 UART with 14.7456MHz oscillator
107 * manfid 0x015D, 0x4C45
108 */
109static void quirk_setup_brainboxes_0104(struct pcmcia_device *link, struct uart_port *port)
110{
111 port->uartclk = 14745600;
112}
113
114static int quirk_post_ibm(struct pcmcia_device *link)
115{
116 u8 val;
117 int ret;
118
119 ret = pcmcia_read_config_byte(link, 0x800, &val);
120 if (ret)
121 goto failed;
122
123 ret = pcmcia_write_config_byte(link, 0x800, val | 1);
124 if (ret)
125 goto failed;
126 return 0;
127
128 failed:
129 return -ENODEV;
130}
131
132/*
133 * Nokia cards are not really multiport cards. Shouldn't this
134 * be handled by setting the quirk entry .multi = 0 | 1 ?
135 */
136static void quirk_config_nokia(struct pcmcia_device *link)
137{
138 struct serial_info *info = link->priv;
139
140 if (info->multi > 1)
141 info->multi = 1;
142}
143
144static void quirk_wakeup_oxsemi(struct pcmcia_device *link)
145{
146 struct serial_info *info = link->priv;
147
148 if (info->c950ctrl)
149 outb(12, info->c950ctrl + 1);
150}
151
152/* request_region? oxsemi branch does no request_region too... */
153/*
154 * This sequence is needed to properly initialize MC45 attached to OXCF950.
155 * I tried decreasing these msleep()s, but it worked properly (survived
156 * 1000 stop/start operations) with these timeouts (or bigger).
157 */
158static void quirk_wakeup_possio_gcc(struct pcmcia_device *link)
159{
160 struct serial_info *info = link->priv;
161 unsigned int ctrl = info->c950ctrl;
162
163 outb(0xA, ctrl + 1);
164 msleep(100);
165 outb(0xE, ctrl + 1);
166 msleep(300);
167 outb(0xC, ctrl + 1);
168 msleep(100);
169 outb(0xE, ctrl + 1);
170 msleep(200);
171 outb(0xF, ctrl + 1);
172 msleep(100);
173 outb(0xE, ctrl + 1);
174 msleep(100);
175 outb(0xC, ctrl + 1);
176}
177
178/*
179 * Socket Dual IO: this enables irq's for second port
180 */
181static void quirk_config_socket(struct pcmcia_device *link)
182{
183 struct serial_info *info = link->priv;
184
185 if (info->multi)
186 link->config_flags |= CONF_ENABLE_ESR;
187}
188
189static const struct serial_quirk quirks[] = {
190 {
191 .manfid = 0x0160,
192 .prodid = 0x0104,
193 .multi = -1,
194 .setup = quirk_setup_brainboxes_0104,
195 }, {
196 .manfid = 0x015D,
197 .prodid = 0x4C45,
198 .multi = -1,
199 .setup = quirk_setup_brainboxes_0104,
200 }, {
201 .manfid = MANFID_IBM,
202 .prodid = ~0,
203 .multi = -1,
204 .post = quirk_post_ibm,
205 }, {
206 .manfid = MANFID_INTEL,
207 .prodid = PRODID_INTEL_DUAL_RS232,
208 .multi = 2,
209 }, {
210 .manfid = MANFID_NATINST,
211 .prodid = PRODID_NATINST_QUAD_RS232,
212 .multi = 4,
213 }, {
214 .manfid = MANFID_NOKIA,
215 .prodid = ~0,
216 .multi = -1,
217 .config = quirk_config_nokia,
218 }, {
219 .manfid = MANFID_OMEGA,
220 .prodid = PRODID_OMEGA_QSP_100,
221 .multi = 4,
222 }, {
223 .manfid = MANFID_OXSEMI,
224 .prodid = ~0,
225 .multi = -1,
226 .wakeup = quirk_wakeup_oxsemi,
227 }, {
228 .manfid = MANFID_POSSIO,
229 .prodid = PRODID_POSSIO_GCC,
230 .multi = -1,
231 .wakeup = quirk_wakeup_possio_gcc,
232 }, {
233 .manfid = MANFID_QUATECH,
234 .prodid = PRODID_QUATECH_DUAL_RS232,
235 .multi = 2,
236 }, {
237 .manfid = MANFID_QUATECH,
238 .prodid = PRODID_QUATECH_DUAL_RS232_D1,
239 .multi = 2,
240 }, {
241 .manfid = MANFID_QUATECH,
242 .prodid = PRODID_QUATECH_DUAL_RS232_G,
243 .multi = 2,
244 }, {
245 .manfid = MANFID_QUATECH,
246 .prodid = PRODID_QUATECH_QUAD_RS232,
247 .multi = 4,
248 }, {
249 .manfid = MANFID_SOCKET,
250 .prodid = PRODID_SOCKET_DUAL_RS232,
251 .multi = 2,
252 .config = quirk_config_socket,
253 }, {
254 .manfid = MANFID_SOCKET,
255 .prodid = ~0,
256 .multi = -1,
257 .config = quirk_config_socket,
258 }
259};
260
261
262static int serial_config(struct pcmcia_device * link);
263
264
265static void serial_remove(struct pcmcia_device *link)
266{
267 struct serial_info *info = link->priv;
268 int i;
269
270 dev_dbg(&link->dev, "serial_release\n");
271
272 /*
273 * Recheck to see if the device is still configured.
274 */
275 for (i = 0; i < info->ndev; i++)
276 serial8250_unregister_port(info->line[i]);
277
278 if (!info->slave)
279 pcmcia_disable_device(link);
280}
281
282static int serial_suspend(struct pcmcia_device *link)
283{
284 struct serial_info *info = link->priv;
285 int i;
286
287 for (i = 0; i < info->ndev; i++)
288 serial8250_suspend_port(info->line[i]);
289
290 return 0;
291}
292
293static int serial_resume(struct pcmcia_device *link)
294{
295 struct serial_info *info = link->priv;
296 int i;
297
298 for (i = 0; i < info->ndev; i++)
299 serial8250_resume_port(info->line[i]);
300
301 if (info->quirk && info->quirk->wakeup)
302 info->quirk->wakeup(link);
303
304 return 0;
305}
306
307static int serial_probe(struct pcmcia_device *link)
308{
309 struct serial_info *info;
310
311 dev_dbg(&link->dev, "serial_attach()\n");
312
313 /* Create new serial device */
314 info = kzalloc(sizeof (*info), GFP_KERNEL);
315 if (!info)
316 return -ENOMEM;
317 info->p_dev = link;
318 link->priv = info;
319
320 link->config_flags |= CONF_ENABLE_IRQ;
321 if (do_sound)
322 link->config_flags |= CONF_ENABLE_SPKR;
323
324 return serial_config(link);
325}
326
327static void serial_detach(struct pcmcia_device *link)
328{
329 struct serial_info *info = link->priv;
330
331 dev_dbg(&link->dev, "serial_detach\n");
332
333 /*
334 * Ensure that the ports have been released.
335 */
336 serial_remove(link);
337
338 /* free bits */
339 kfree(info);
340}
341
342/*====================================================================*/
343
344static int setup_serial(struct pcmcia_device *handle, struct serial_info * info,
345 unsigned int iobase, int irq)
346{
347 struct uart_port port;
348 int line;
349
350 memset(&port, 0, sizeof (struct uart_port));
351 port.iobase = iobase;
352 port.irq = irq;
353 port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
354 port.uartclk = 1843200;
355 port.dev = &handle->dev;
356 if (buggy_uart)
357 port.flags |= UPF_BUGGY_UART;
358
359 if (info->quirk && info->quirk->setup)
360 info->quirk->setup(handle, &port);
361
362 line = serial8250_register_port(&port);
363 if (line < 0) {
364 printk(KERN_NOTICE "serial_cs: serial8250_register_port() at "
365 "0x%04lx, irq %d failed\n", (u_long)iobase, irq);
366 return -EINVAL;
367 }
368
369 info->line[info->ndev] = line;
370 info->ndev++;
371
372 return 0;
373}
374
375/*====================================================================*/
376
377static int pfc_config(struct pcmcia_device *p_dev)
378{
379 unsigned int port = 0;
380 struct serial_info *info = p_dev->priv;
381
382 if ((p_dev->resource[1]->end != 0) &&
383 (resource_size(p_dev->resource[1]) == 8)) {
384 port = p_dev->resource[1]->start;
385 info->slave = 1;
386 } else if ((info->manfid == MANFID_OSITECH) &&
387 (resource_size(p_dev->resource[0]) == 0x40)) {
388 port = p_dev->resource[0]->start + 0x28;
389 info->slave = 1;
390 }
391 if (info->slave)
392 return setup_serial(p_dev, info, port, p_dev->irq);
393
394 dev_warn(&p_dev->dev, "no usable port range found, giving up\n");
395 return -ENODEV;
396}
397
398static int simple_config_check(struct pcmcia_device *p_dev, void *priv_data)
399{
400 static const int size_table[2] = { 8, 16 };
401 int *try = priv_data;
402
403 if (p_dev->resource[0]->start == 0)
404 return -ENODEV;
405
406 if ((*try & 0x1) == 0)
407 p_dev->io_lines = 16;
408
409 if (p_dev->resource[0]->end != size_table[(*try >> 1)])
410 return -ENODEV;
411
412 p_dev->resource[0]->end = 8;
413 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
414 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
415
416 return pcmcia_request_io(p_dev);
417}
418
419static int simple_config_check_notpicky(struct pcmcia_device *p_dev,
420 void *priv_data)
421{
422 static const unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
423 int j;
424
425 if (p_dev->io_lines > 3)
426 return -ENODEV;
427
428 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
429 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
430 p_dev->resource[0]->end = 8;
431
432 for (j = 0; j < 5; j++) {
433 p_dev->resource[0]->start = base[j];
434 p_dev->io_lines = base[j] ? 16 : 3;
435 if (!pcmcia_request_io(p_dev))
436 return 0;
437 }
438 return -ENODEV;
439}
440
441static int simple_config(struct pcmcia_device *link)
442{
443 struct serial_info *info = link->priv;
444 int i = -ENODEV, try;
445
446 /* First pass: look for a config entry that looks normal.
447 * Two tries: without IO aliases, then with aliases */
448 link->config_flags |= CONF_AUTO_SET_VPP | CONF_AUTO_SET_IO;
449 for (try = 0; try < 4; try++)
450 if (!pcmcia_loop_config(link, simple_config_check, &try))
451 goto found_port;
452
453 /* Second pass: try to find an entry that isn't picky about
454 its base address, then try to grab any standard serial port
455 address, and finally try to get any free port. */
456 if (!pcmcia_loop_config(link, simple_config_check_notpicky, NULL))
457 goto found_port;
458
459 dev_warn(&link->dev, "no usable port range found, giving up\n");
460 return -1;
461
462found_port:
463 if (info->multi && (info->manfid == MANFID_3COM))
464 link->config_index &= ~(0x08);
465
466 /*
467 * Apply any configuration quirks.
468 */
469 if (info->quirk && info->quirk->config)
470 info->quirk->config(link);
471
472 i = pcmcia_enable_device(link);
473 if (i != 0)
474 return -1;
475 return setup_serial(link, info, link->resource[0]->start, link->irq);
476}
477
478static int multi_config_check(struct pcmcia_device *p_dev, void *priv_data)
479{
480 int *multi = priv_data;
481
482 if (p_dev->resource[1]->end)
483 return -EINVAL;
484
485 /* The quad port cards have bad CIS's, so just look for a
486 window larger than 8 ports and assume it will be right */
487 if (p_dev->resource[0]->end <= 8)
488 return -EINVAL;
489
490 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
491 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
492 p_dev->resource[0]->end = *multi * 8;
493
494 if (pcmcia_request_io(p_dev))
495 return -ENODEV;
496 return 0;
497}
498
499static int multi_config_check_notpicky(struct pcmcia_device *p_dev,
500 void *priv_data)
501{
502 int *base2 = priv_data;
503
504 if (!p_dev->resource[0]->end || !p_dev->resource[1]->end)
505 return -ENODEV;
506
507 p_dev->resource[0]->end = p_dev->resource[1]->end = 8;
508 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
509 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
510
511 if (pcmcia_request_io(p_dev))
512 return -ENODEV;
513
514 *base2 = p_dev->resource[0]->start + 8;
515 return 0;
516}
517
518static int multi_config(struct pcmcia_device *link)
519{
520 struct serial_info *info = link->priv;
521 int i, base2 = 0;
522
523 link->config_flags |= CONF_AUTO_SET_IO;
524 /* First, look for a generic full-sized window */
525 if (!pcmcia_loop_config(link, multi_config_check, &info->multi))
526 base2 = link->resource[0]->start + 8;
527 else {
528 /* If that didn't work, look for two windows */
529 info->multi = 2;
530 if (pcmcia_loop_config(link, multi_config_check_notpicky,
531 &base2)) {
532 dev_warn(&link->dev, "no usable port range "
533 "found, giving up\n");
534 return -ENODEV;
535 }
536 }
537
538 if (!link->irq)
539 dev_warn(&link->dev, "no usable IRQ found, continuing...\n");
540
541 /*
542 * Apply any configuration quirks.
543 */
544 if (info->quirk && info->quirk->config)
545 info->quirk->config(link);
546
547 i = pcmcia_enable_device(link);
548 if (i != 0)
549 return -ENODEV;
550
551 /* The Oxford Semiconductor OXCF950 cards are in fact single-port:
552 * 8 registers are for the UART, the others are extra registers.
553 * Siemen's MC45 PCMCIA (Possio's GCC) is OXCF950 based too.
554 */
555 if (info->manfid == MANFID_OXSEMI || (info->manfid == MANFID_POSSIO &&
556 info->prodid == PRODID_POSSIO_GCC)) {
557 int err;
558
559 if (link->config_index == 1 ||
560 link->config_index == 3) {
561 err = setup_serial(link, info, base2,
562 link->irq);
563 base2 = link->resource[0]->start;
564 } else {
565 err = setup_serial(link, info, link->resource[0]->start,
566 link->irq);
567 }
568 info->c950ctrl = base2;
569
570 /*
571 * FIXME: We really should wake up the port prior to
572 * handing it over to the serial layer.
573 */
574 if (info->quirk && info->quirk->wakeup)
575 info->quirk->wakeup(link);
576
577 return 0;
578 }
579
580 setup_serial(link, info, link->resource[0]->start, link->irq);
581 for (i = 0; i < info->multi - 1; i++)
582 setup_serial(link, info, base2 + (8 * i),
583 link->irq);
584 return 0;
585}
586
587static int serial_check_for_multi(struct pcmcia_device *p_dev, void *priv_data)
588{
589 struct serial_info *info = p_dev->priv;
590
591 if (!p_dev->resource[0]->end)
592 return -EINVAL;
593
594 if ((!p_dev->resource[1]->end) && (p_dev->resource[0]->end % 8 == 0))
595 info->multi = p_dev->resource[0]->end >> 3;
596
597 if ((p_dev->resource[1]->end) && (p_dev->resource[0]->end == 8)
598 && (p_dev->resource[1]->end == 8))
599 info->multi = 2;
600
601 return 0; /* break */
602}
603
604
605static int serial_config(struct pcmcia_device * link)
606{
607 struct serial_info *info = link->priv;
608 int i;
609
610 dev_dbg(&link->dev, "serial_config\n");
611
612 /* Is this a compliant multifunction card? */
613 info->multi = (link->socket->functions > 1);
614
615 /* Is this a multiport card? */
616 info->manfid = link->manf_id;
617 info->prodid = link->card_id;
618
619 for (i = 0; i < ARRAY_SIZE(quirks); i++)
620 if ((quirks[i].manfid == ~0 ||
621 quirks[i].manfid == info->manfid) &&
622 (quirks[i].prodid == ~0 ||
623 quirks[i].prodid == info->prodid)) {
624 info->quirk = &quirks[i];
625 break;
626 }
627
628 /* Another check for dual-serial cards: look for either serial or
629 multifunction cards that ask for appropriate IO port ranges */
630 if ((info->multi == 0) &&
631 (link->has_func_id) &&
632 (link->socket->pcmcia_pfc == 0) &&
633 ((link->func_id == CISTPL_FUNCID_MULTI) ||
634 (link->func_id == CISTPL_FUNCID_SERIAL)))
635 pcmcia_loop_config(link, serial_check_for_multi, info);
636
637 /*
638 * Apply any multi-port quirk.
639 */
640 if (info->quirk && info->quirk->multi != -1)
641 info->multi = info->quirk->multi;
642
643 dev_info(&link->dev,
644 "trying to set up [0x%04x:0x%04x] (pfc: %d, multi: %d, quirk: %p)\n",
645 link->manf_id, link->card_id,
646 link->socket->pcmcia_pfc, info->multi, info->quirk);
647 if (link->socket->pcmcia_pfc)
648 i = pfc_config(link);
649 else if (info->multi > 1)
650 i = multi_config(link);
651 else
652 i = simple_config(link);
653
654 if (i || info->ndev == 0)
655 goto failed;
656
657 /*
658 * Apply any post-init quirk. FIXME: This should really happen
659 * before we register the port, since it might already be in use.
660 */
661 if (info->quirk && info->quirk->post)
662 if (info->quirk->post(link))
663 goto failed;
664
665 return 0;
666
667failed:
668 dev_warn(&link->dev, "failed to initialize\n");
669 serial_remove(link);
670 return -ENODEV;
671}
672
673static struct pcmcia_device_id serial_ids[] = {
674 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0057, 0x0021),
675 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0089, 0x110a),
676 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0104, 0x000a),
677 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0105, 0x0d0a),
678 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0105, 0x0e0a),
679 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0105, 0xea15),
680 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0109, 0x0501),
681 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0138, 0x110a),
682 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0140, 0x000a),
683 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0143, 0x3341),
684 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0143, 0xc0ab),
685 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x016c, 0x0081),
686 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x021b, 0x0101),
687 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x08a1, 0xc0ab),
688 PCMCIA_PFC_DEVICE_PROD_ID123(1, "MEGAHERTZ", "CC/XJEM3288", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x04cd2988, 0x46a52d63),
689 PCMCIA_PFC_DEVICE_PROD_ID123(1, "MEGAHERTZ", "CC/XJEM3336", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x0143b773, 0x46a52d63),
690 PCMCIA_PFC_DEVICE_PROD_ID123(1, "MEGAHERTZ", "EM1144T", "PCMCIA MODEM", 0xf510db04, 0x856d66c8, 0xbd6c43ef),
691 PCMCIA_PFC_DEVICE_PROD_ID123(1, "MEGAHERTZ", "XJEM1144/CCEM1144", "PCMCIA MODEM", 0xf510db04, 0x52d21e1e, 0xbd6c43ef),
692 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "CEM28", 0x2e3ee845, 0x0ea978ea),
693 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "CEM33", 0x2e3ee845, 0x80609023),
694 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "CEM56", 0x2e3ee845, 0xa650c32a),
695 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "REM10", 0x2e3ee845, 0x76df1d29),
696 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "XEM5600", 0x2e3ee845, 0xf1403719),
697 PCMCIA_PFC_DEVICE_PROD_ID12(1, "AnyCom", "Fast Ethernet + 56K COMBO", 0x578ba6e7, 0xb0ac62c4),
698 PCMCIA_PFC_DEVICE_PROD_ID12(1, "ATKK", "LM33-PCM-T", 0xba9eb7e2, 0x077c174e),
699 PCMCIA_PFC_DEVICE_PROD_ID12(1, "D-Link", "DME336T", 0x1a424a1c, 0xb23897ff),
700 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Gateway 2000", "XJEM3336", 0xdd9989be, 0x662c394c),
701 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Grey Cell", "GCS3000", 0x2a151fac, 0x48b932ae),
702 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Linksys", "EtherFast 10&100 + 56K PC Card (PCMLM56)", 0x0733cc81, 0xb3765033),
703 PCMCIA_PFC_DEVICE_PROD_ID12(1, "LINKSYS", "PCMLM336", 0xf7cb0b07, 0x7a821b58),
704 PCMCIA_PFC_DEVICE_PROD_ID12(1, "MEGAHERTZ", "XJEM1144/CCEM1144", 0xf510db04, 0x52d21e1e),
705 PCMCIA_PFC_DEVICE_PROD_ID12(1, "MICRO RESEARCH", "COMBO-L/M-336", 0xb2ced065, 0x3ced0555),
706 PCMCIA_PFC_DEVICE_PROD_ID12(1, "NEC", "PK-UG-J001" ,0x18df0ba0 ,0x831b1064),
707 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Ositech", "Trumpcard:Jack of Diamonds Modem+Ethernet", 0xc2f80cd, 0x656947b9),
708 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Ositech", "Trumpcard:Jack of Hearts Modem+Ethernet", 0xc2f80cd, 0xdc9ba5ed),
709 PCMCIA_PFC_DEVICE_PROD_ID12(1, "PCMCIAs", "ComboCard", 0xdcfe12d3, 0xcd8906cc),
710 PCMCIA_PFC_DEVICE_PROD_ID12(1, "PCMCIAs", "LanModem", 0xdcfe12d3, 0xc67c648f),
711 PCMCIA_PFC_DEVICE_PROD_ID12(1, "TDK", "GlobalNetworker 3410/3412", 0x1eae9475, 0xd9a93bed),
712 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Xircom", "CreditCard Ethernet+Modem II", 0x2e3ee845, 0xeca401bf),
713 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0e01),
714 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0a05),
715 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x1101),
716 PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0104, 0x0070),
717 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0101, 0x0562),
718 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0104, 0x0070),
719 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x016c, 0x0020),
720 PCMCIA_MFC_DEVICE_PROD_ID123(1, "APEX DATA", "MULTICARD", "ETHERNET-MODEM", 0x11c2da09, 0x7289dc5d, 0xaad95e1f),
721 PCMCIA_MFC_DEVICE_PROD_ID12(1, "IBM", "Home and Away 28.8 PC Card ", 0xb569a6e5, 0x5bd4ff2c),
722 PCMCIA_MFC_DEVICE_PROD_ID12(1, "IBM", "Home and Away Credit Card Adapter", 0xb569a6e5, 0x4bdf15c3),
723 PCMCIA_MFC_DEVICE_PROD_ID12(1, "IBM", "w95 Home and Away Credit Card ", 0xb569a6e5, 0xae911c15),
724 PCMCIA_MFC_DEVICE_PROD_ID1(1, "Motorola MARQUIS", 0xf03e4e77),
725 PCMCIA_MFC_DEVICE_PROD_ID2(1, "FAX/Modem/Ethernet Combo Card ", 0x1ed59302),
726 PCMCIA_DEVICE_MANF_CARD(0x0089, 0x0301),
727 PCMCIA_DEVICE_MANF_CARD(0x00a4, 0x0276),
728 PCMCIA_DEVICE_MANF_CARD(0x0101, 0x0039),
729 PCMCIA_DEVICE_MANF_CARD(0x0104, 0x0006),
730 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x0101), /* TDK DF2814 */
731 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x100a), /* Xircom CM-56G */
732 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x3e0a), /* TDK DF5660 */
733 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x410a),
734 PCMCIA_DEVICE_MANF_CARD(0x0107, 0x0002), /* USRobotics 14,400 */
735 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d50),
736 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d51),
737 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d52),
738 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d53),
739 PCMCIA_DEVICE_MANF_CARD(0x010b, 0xd180),
740 PCMCIA_DEVICE_MANF_CARD(0x0115, 0x3330), /* USRobotics/SUN 14,400 */
741 PCMCIA_DEVICE_MANF_CARD(0x0124, 0x0100), /* Nokia DTP-2 ver II */
742 PCMCIA_DEVICE_MANF_CARD(0x0134, 0x5600), /* LASAT COMMUNICATIONS A/S */
743 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x000e),
744 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x001b),
745 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0025),
746 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0045),
747 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0052),
748 PCMCIA_DEVICE_MANF_CARD(0x016c, 0x0006), /* Psion 56K+Fax */
749 PCMCIA_DEVICE_MANF_CARD(0x0200, 0x0001), /* MultiMobile */
750 PCMCIA_DEVICE_PROD_ID134("ADV", "TECH", "COMpad-32/85", 0x67459937, 0x916d02ba, 0x8fbe92ae),
751 PCMCIA_DEVICE_PROD_ID124("GATEWAY2000", "CC3144", "PCMCIA MODEM", 0x506bccae, 0xcb3685f1, 0xbd6c43ef),
752 PCMCIA_DEVICE_PROD_ID14("MEGAHERTZ", "PCMCIA MODEM", 0xf510db04, 0xbd6c43ef),
753 PCMCIA_DEVICE_PROD_ID124("TOSHIBA", "T144PF", "PCMCIA MODEM", 0xb4585a1a, 0x7271409c, 0xbd6c43ef),
754 PCMCIA_DEVICE_PROD_ID123("FUJITSU", "FC14F ", "MBH10213", 0x6ee5a3d8, 0x30ead12b, 0xb00f05a0),
755 PCMCIA_DEVICE_PROD_ID123("Novatel Wireless", "Merlin UMTS Modem", "U630", 0x32607776, 0xd9e73b13, 0xe87332e),
756 PCMCIA_DEVICE_PROD_ID13("MEGAHERTZ", "V.34 PCMCIA MODEM", 0xf510db04, 0xbb2cce4a),
757 PCMCIA_DEVICE_PROD_ID12("Brain Boxes", "Bluetooth PC Card", 0xee138382, 0xd4ce9b02),
758 PCMCIA_DEVICE_PROD_ID12("CIRRUS LOGIC", "FAX MODEM", 0xe625f451, 0xcecd6dfa),
759 PCMCIA_DEVICE_PROD_ID12("COMPAQ", "PCMCIA 28800 FAX/DATA MODEM", 0xa3a3062c, 0x8cbd7c76),
760 PCMCIA_DEVICE_PROD_ID12("COMPAQ", "PCMCIA 33600 FAX/DATA MODEM", 0xa3a3062c, 0x5a00ce95),
761 PCMCIA_DEVICE_PROD_ID12("Computerboards, Inc.", "PCM-COM422", 0xd0b78f51, 0x7e2d49ed),
762 PCMCIA_DEVICE_PROD_ID12("Dr. Neuhaus", "FURY CARD 14K4", 0x76942813, 0x8b96ce65),
763 PCMCIA_DEVICE_PROD_ID12("IBM", "ISDN/56K/GSM", 0xb569a6e5, 0xfee5297b),
764 PCMCIA_DEVICE_PROD_ID12("Intelligent", "ANGIA FAX/MODEM", 0xb496e65e, 0xf31602a6),
765 PCMCIA_DEVICE_PROD_ID12("Intel", "MODEM 2400+", 0x816cc815, 0x412729fb),
766 PCMCIA_DEVICE_PROD_ID12("Intertex", "IX34-PCMCIA", 0xf8a097e3, 0x97880447),
767 PCMCIA_DEVICE_PROD_ID12("IOTech Inc ", "PCMCIA Dual RS-232 Serial Port Card", 0x3bd2d898, 0x92abc92f),
768 PCMCIA_DEVICE_PROD_ID12("MACRONIX", "FAX/MODEM", 0x668388b3, 0x3f9bdf2f),
769 PCMCIA_DEVICE_PROD_ID12("Multi-Tech", "MT1432LT", 0x5f73be51, 0x0b3e2383),
770 PCMCIA_DEVICE_PROD_ID12("Multi-Tech", "MT2834LT", 0x5f73be51, 0x4cd7c09e),
771 PCMCIA_DEVICE_PROD_ID12("OEM ", "C288MX ", 0xb572d360, 0xd2385b7a),
772 PCMCIA_DEVICE_PROD_ID12("Option International", "V34bis GSM/PSTN Data/Fax Modem", 0x9d7cd6f5, 0x5cb8bf41),
773 PCMCIA_DEVICE_PROD_ID12("PCMCIA ", "C336MX ", 0x99bcafe9, 0xaa25bcab),
774 PCMCIA_DEVICE_PROD_ID12("Quatech Inc", "PCMCIA Dual RS-232 Serial Port Card", 0xc4420b35, 0x92abc92f),
775 PCMCIA_DEVICE_PROD_ID12("Quatech Inc", "Dual RS-232 Serial Port PC Card", 0xc4420b35, 0x031a380d),
776 PCMCIA_DEVICE_PROD_ID12("Telia", "SurfinBird 560P/A+", 0xe2cdd5e, 0xc9314b38),
777 PCMCIA_DEVICE_PROD_ID1("Smart Serial Port", 0x2d8ce292),
778 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "PCMCIA", "EN2218-LAN/MODEM", 0x281f1c5d, 0x570f348e, "cis/PCMLM28.cis"),
779 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "PCMCIA", "UE2218-LAN/MODEM", 0x281f1c5d, 0x6fdcacee, "cis/PCMLM28.cis"),
780 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "Psion Dacom", "Gold Card V34 Ethernet", 0xf5f025c2, 0x338e8155, "cis/PCMLM28.cis"),
781 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "Psion Dacom", "Gold Card V34 Ethernet GSM", 0xf5f025c2, 0x4ae85d35, "cis/PCMLM28.cis"),
782 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "LINKSYS", "PCMLM28", 0xf7cb0b07, 0x66881874, "cis/PCMLM28.cis"),
783 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "TOSHIBA", "Modem/LAN Card", 0xb4585a1a, 0x53f922f8, "cis/PCMLM28.cis"),
784 PCMCIA_MFC_DEVICE_CIS_PROD_ID12(1, "DAYNA COMMUNICATIONS", "LAN AND MODEM MULTIFUNCTION", 0x8fdf8f89, 0xdd5ed9e8, "cis/DP83903.cis"),
785 PCMCIA_MFC_DEVICE_CIS_PROD_ID4(1, "NSC MF LAN/Modem", 0x58fc6056, "cis/DP83903.cis"),
786 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0101, 0x0556, "cis/3CCFEM556.cis"),
787 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0175, 0x0000, "cis/DP83903.cis"),
788 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0101, 0x0035, "cis/3CXEM556.cis"),
789 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0101, 0x003d, "cis/3CXEM556.cis"),
790 PCMCIA_DEVICE_CIS_PROD_ID12("Sierra Wireless", "AC850", 0xd85f6206, 0x42a2c018, "cis/SW_8xx_SER.cis"), /* Sierra Wireless AC850 3G Network Adapter R1 */
791 PCMCIA_DEVICE_CIS_PROD_ID12("Sierra Wireless", "AC860", 0xd85f6206, 0x698f93db, "cis/SW_8xx_SER.cis"), /* Sierra Wireless AC860 3G Network Adapter R1 */
792 PCMCIA_DEVICE_CIS_PROD_ID12("Sierra Wireless", "AC710/AC750", 0xd85f6206, 0x761b11e0, "cis/SW_7xx_SER.cis"), /* Sierra Wireless AC710/AC750 GPRS Network Adapter R1 */
793 PCMCIA_DEVICE_CIS_MANF_CARD(0x0192, 0xa555, "cis/SW_555_SER.cis"), /* Sierra Aircard 555 CDMA 1xrtt Modem -- pre update */
794 PCMCIA_DEVICE_CIS_MANF_CARD(0x013f, 0xa555, "cis/SW_555_SER.cis"), /* Sierra Aircard 555 CDMA 1xrtt Modem -- post update */
795 PCMCIA_DEVICE_CIS_PROD_ID12("MultiTech", "PCMCIA 56K DataFax", 0x842047ee, 0xc2efcf03, "cis/MT5634ZLX.cis"),
796 PCMCIA_DEVICE_CIS_PROD_ID12("ADVANTECH", "COMpad-32/85B-2", 0x96913a85, 0x27ab5437, "cis/COMpad2.cis"),
797 PCMCIA_DEVICE_CIS_PROD_ID12("ADVANTECH", "COMpad-32/85B-4", 0x96913a85, 0xcec8f102, "cis/COMpad4.cis"),
798 PCMCIA_DEVICE_CIS_PROD_ID123("ADVANTECH", "COMpad-32/85", "1.0", 0x96913a85, 0x8fbe92ae, 0x0877b627, "cis/COMpad2.cis"),
799 PCMCIA_DEVICE_CIS_PROD_ID2("RS-COM 2P", 0xad20b156, "cis/RS-COM-2P.cis"),
800 PCMCIA_DEVICE_CIS_MANF_CARD(0x0013, 0x0000, "cis/GLOBETROTTER.cis"),
801 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL100 1.00.",0x19ca78af,0xf964f42b),
802 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL100",0x19ca78af,0x71d98e83),
803 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL232 1.00.",0x19ca78af,0x69fb7490),
804 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL232",0x19ca78af,0xb6bc0235),
805 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c2000.","SERIAL CARD: CF232",0x63f2e0bd,0xb9e175d3),
806 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c2000.","SERIAL CARD: CF232-5",0x63f2e0bd,0xfce33442),
807 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF232",0x3beb8cf2,0x171e7190),
808 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF232-5",0x3beb8cf2,0x20da4262),
809 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF428",0x3beb8cf2,0xea5dd57d),
810 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF500",0x3beb8cf2,0xd77255fa),
811 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: IC232",0x3beb8cf2,0x6a709903),
812 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: SL232",0x3beb8cf2,0x18430676),
813 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: XL232",0x3beb8cf2,0x6f933767),
814 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: CF332",0x3beb8cf2,0x16dc1ba7),
815 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: SL332",0x3beb8cf2,0x19816c41),
816 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: SL385",0x3beb8cf2,0x64112029),
817 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
818 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial+Parallel Port: SP230",0x3beb8cf2,0xdb9e58bc),
819 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: CF332",0x3beb8cf2,0x16dc1ba7),
820 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: SL332",0x3beb8cf2,0x19816c41),
821 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: SL385",0x3beb8cf2,0x64112029),
822 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
823 PCMCIA_MFC_DEVICE_PROD_ID12(2,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
824 PCMCIA_MFC_DEVICE_PROD_ID12(3,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
825 PCMCIA_DEVICE_MANF_CARD(0x0279, 0x950b),
826 /* too generic */
827 /* PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0160, 0x0002), */
828 /* PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0160, 0x0002), */
829 PCMCIA_DEVICE_FUNC_ID(2),
830 PCMCIA_DEVICE_NULL,
831};
832MODULE_DEVICE_TABLE(pcmcia, serial_ids);
833
834MODULE_FIRMWARE("cis/PCMLM28.cis");
835MODULE_FIRMWARE("cis/DP83903.cis");
836MODULE_FIRMWARE("cis/3CCFEM556.cis");
837MODULE_FIRMWARE("cis/3CXEM556.cis");
838MODULE_FIRMWARE("cis/SW_8xx_SER.cis");
839MODULE_FIRMWARE("cis/SW_7xx_SER.cis");
840MODULE_FIRMWARE("cis/SW_555_SER.cis");
841MODULE_FIRMWARE("cis/MT5634ZLX.cis");
842MODULE_FIRMWARE("cis/COMpad2.cis");
843MODULE_FIRMWARE("cis/COMpad4.cis");
844MODULE_FIRMWARE("cis/RS-COM-2P.cis");
845
846static struct pcmcia_driver serial_cs_driver = {
847 .owner = THIS_MODULE,
848 .name = "serial_cs",
849 .probe = serial_probe,
850 .remove = serial_detach,
851 .id_table = serial_ids,
852 .suspend = serial_suspend,
853 .resume = serial_resume,
854};
855
856static int __init init_serial_cs(void)
857{
858 return pcmcia_register_driver(&serial_cs_driver);
859}
860
861static void __exit exit_serial_cs(void)
862{
863 pcmcia_unregister_driver(&serial_cs_driver);
864}
865
866module_init(init_serial_cs);
867module_exit(exit_serial_cs);
868
869MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/serial_ks8695.c b/drivers/tty/serial/serial_ks8695.c
new file mode 100644
index 000000000000..b1962025b1aa
--- /dev/null
+++ b/drivers/tty/serial/serial_ks8695.c
@@ -0,0 +1,705 @@
1/*
2 * drivers/serial/serial_ks8695.c
3 *
4 * Driver for KS8695 serial ports
5 *
6 * Based on drivers/serial/serial_amba.c, by Kam Lee.
7 *
8 * Copyright 2002-2005 Micrel 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 */
16#include <linux/module.h>
17#include <linux/tty.h>
18#include <linux/ioport.h>
19#include <linux/init.h>
20#include <linux/serial.h>
21#include <linux/console.h>
22#include <linux/sysrq.h>
23#include <linux/device.h>
24
25#include <asm/io.h>
26#include <asm/irq.h>
27#include <asm/mach/irq.h>
28
29#include <mach/regs-uart.h>
30#include <mach/regs-irq.h>
31
32#if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
33#define SUPPORT_SYSRQ
34#endif
35
36#include <linux/serial_core.h>
37
38
39#define SERIAL_KS8695_MAJOR 204
40#define SERIAL_KS8695_MINOR 16
41#define SERIAL_KS8695_DEVNAME "ttyAM"
42
43#define SERIAL_KS8695_NR 1
44
45/*
46 * Access macros for the KS8695 UART
47 */
48#define UART_GET_CHAR(p) (__raw_readl((p)->membase + KS8695_URRB) & 0xFF)
49#define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + KS8695_URTH)
50#define UART_GET_FCR(p) __raw_readl((p)->membase + KS8695_URFC)
51#define UART_PUT_FCR(p, c) __raw_writel((c), (p)->membase + KS8695_URFC)
52#define UART_GET_MSR(p) __raw_readl((p)->membase + KS8695_URMS)
53#define UART_GET_LSR(p) __raw_readl((p)->membase + KS8695_URLS)
54#define UART_GET_LCR(p) __raw_readl((p)->membase + KS8695_URLC)
55#define UART_PUT_LCR(p, c) __raw_writel((c), (p)->membase + KS8695_URLC)
56#define UART_GET_MCR(p) __raw_readl((p)->membase + KS8695_URMC)
57#define UART_PUT_MCR(p, c) __raw_writel((c), (p)->membase + KS8695_URMC)
58#define UART_GET_BRDR(p) __raw_readl((p)->membase + KS8695_URBD)
59#define UART_PUT_BRDR(p, c) __raw_writel((c), (p)->membase + KS8695_URBD)
60
61#define KS8695_CLR_TX_INT() __raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
62
63#define UART_DUMMY_LSR_RX 0x100
64#define UART_PORT_SIZE (KS8695_USR - KS8695_URRB + 4)
65
66static inline int tx_enabled(struct uart_port *port)
67{
68 return port->unused[0] & 1;
69}
70
71static inline int rx_enabled(struct uart_port *port)
72{
73 return port->unused[0] & 2;
74}
75
76static inline int ms_enabled(struct uart_port *port)
77{
78 return port->unused[0] & 4;
79}
80
81static inline void ms_enable(struct uart_port *port, int enabled)
82{
83 if(enabled)
84 port->unused[0] |= 4;
85 else
86 port->unused[0] &= ~4;
87}
88
89static inline void rx_enable(struct uart_port *port, int enabled)
90{
91 if(enabled)
92 port->unused[0] |= 2;
93 else
94 port->unused[0] &= ~2;
95}
96
97static inline void tx_enable(struct uart_port *port, int enabled)
98{
99 if(enabled)
100 port->unused[0] |= 1;
101 else
102 port->unused[0] &= ~1;
103}
104
105
106#ifdef SUPPORT_SYSRQ
107static struct console ks8695_console;
108#endif
109
110static void ks8695uart_stop_tx(struct uart_port *port)
111{
112 if (tx_enabled(port)) {
113 /* use disable_irq_nosync() and not disable_irq() to avoid self
114 * imposed deadlock by not waiting for irq handler to end,
115 * since this ks8695uart_stop_tx() is called from interrupt context.
116 */
117 disable_irq_nosync(KS8695_IRQ_UART_TX);
118 tx_enable(port, 0);
119 }
120}
121
122static void ks8695uart_start_tx(struct uart_port *port)
123{
124 if (!tx_enabled(port)) {
125 enable_irq(KS8695_IRQ_UART_TX);
126 tx_enable(port, 1);
127 }
128}
129
130static void ks8695uart_stop_rx(struct uart_port *port)
131{
132 if (rx_enabled(port)) {
133 disable_irq(KS8695_IRQ_UART_RX);
134 rx_enable(port, 0);
135 }
136}
137
138static void ks8695uart_enable_ms(struct uart_port *port)
139{
140 if (!ms_enabled(port)) {
141 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
142 ms_enable(port,1);
143 }
144}
145
146static void ks8695uart_disable_ms(struct uart_port *port)
147{
148 if (ms_enabled(port)) {
149 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
150 ms_enable(port,0);
151 }
152}
153
154static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
155{
156 struct uart_port *port = dev_id;
157 struct tty_struct *tty = port->state->port.tty;
158 unsigned int status, ch, lsr, flg, max_count = 256;
159
160 status = UART_GET_LSR(port); /* clears pending LSR interrupts */
161 while ((status & URLS_URDR) && max_count--) {
162 ch = UART_GET_CHAR(port);
163 flg = TTY_NORMAL;
164
165 port->icount.rx++;
166
167 /*
168 * Note that the error handling code is
169 * out of the main execution path
170 */
171 lsr = UART_GET_LSR(port) | UART_DUMMY_LSR_RX;
172 if (unlikely(lsr & (URLS_URBI | URLS_URPE | URLS_URFE | URLS_URROE))) {
173 if (lsr & URLS_URBI) {
174 lsr &= ~(URLS_URFE | URLS_URPE);
175 port->icount.brk++;
176 if (uart_handle_break(port))
177 goto ignore_char;
178 }
179 if (lsr & URLS_URPE)
180 port->icount.parity++;
181 if (lsr & URLS_URFE)
182 port->icount.frame++;
183 if (lsr & URLS_URROE)
184 port->icount.overrun++;
185
186 lsr &= port->read_status_mask;
187
188 if (lsr & URLS_URBI)
189 flg = TTY_BREAK;
190 else if (lsr & URLS_URPE)
191 flg = TTY_PARITY;
192 else if (lsr & URLS_URFE)
193 flg = TTY_FRAME;
194 }
195
196 if (uart_handle_sysrq_char(port, ch))
197 goto ignore_char;
198
199 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
200
201ignore_char:
202 status = UART_GET_LSR(port);
203 }
204 tty_flip_buffer_push(tty);
205
206 return IRQ_HANDLED;
207}
208
209
210static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
211{
212 struct uart_port *port = dev_id;
213 struct circ_buf *xmit = &port->state->xmit;
214 unsigned int count;
215
216 if (port->x_char) {
217 KS8695_CLR_TX_INT();
218 UART_PUT_CHAR(port, port->x_char);
219 port->icount.tx++;
220 port->x_char = 0;
221 return IRQ_HANDLED;
222 }
223
224 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
225 ks8695uart_stop_tx(port);
226 return IRQ_HANDLED;
227 }
228
229 count = 16; /* fifo size */
230 while (!uart_circ_empty(xmit) && (count-- > 0)) {
231 KS8695_CLR_TX_INT();
232 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
233
234 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
235 port->icount.tx++;
236 }
237
238 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
239 uart_write_wakeup(port);
240
241 if (uart_circ_empty(xmit))
242 ks8695uart_stop_tx(port);
243
244 return IRQ_HANDLED;
245}
246
247static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
248{
249 struct uart_port *port = dev_id;
250 unsigned int status;
251
252 /*
253 * clear modem interrupt by reading MSR
254 */
255 status = UART_GET_MSR(port);
256
257 if (status & URMS_URDDCD)
258 uart_handle_dcd_change(port, status & URMS_URDDCD);
259
260 if (status & URMS_URDDST)
261 port->icount.dsr++;
262
263 if (status & URMS_URDCTS)
264 uart_handle_cts_change(port, status & URMS_URDCTS);
265
266 if (status & URMS_URTERI)
267 port->icount.rng++;
268
269 wake_up_interruptible(&port->state->port.delta_msr_wait);
270
271 return IRQ_HANDLED;
272}
273
274static unsigned int ks8695uart_tx_empty(struct uart_port *port)
275{
276 return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
277}
278
279static unsigned int ks8695uart_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 & URMS_URDCD)
286 result |= TIOCM_CAR;
287 if (status & URMS_URDSR)
288 result |= TIOCM_DSR;
289 if (status & URMS_URCTS)
290 result |= TIOCM_CTS;
291 if (status & URMS_URRI)
292 result |= TIOCM_RI;
293
294 return result;
295}
296
297static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
298{
299 unsigned int mcr;
300
301 mcr = UART_GET_MCR(port);
302 if (mctrl & TIOCM_RTS)
303 mcr |= URMC_URRTS;
304 else
305 mcr &= ~URMC_URRTS;
306
307 if (mctrl & TIOCM_DTR)
308 mcr |= URMC_URDTR;
309 else
310 mcr &= ~URMC_URDTR;
311
312 UART_PUT_MCR(port, mcr);
313}
314
315static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
316{
317 unsigned int lcr;
318
319 lcr = UART_GET_LCR(port);
320
321 if (break_state == -1)
322 lcr |= URLC_URSBC;
323 else
324 lcr &= ~URLC_URSBC;
325
326 UART_PUT_LCR(port, lcr);
327}
328
329static int ks8695uart_startup(struct uart_port *port)
330{
331 int retval;
332
333 set_irq_flags(KS8695_IRQ_UART_TX, IRQF_VALID | IRQF_NOAUTOEN);
334 tx_enable(port, 0);
335 rx_enable(port, 1);
336 ms_enable(port, 1);
337
338 /*
339 * Allocate the IRQ
340 */
341 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, IRQF_DISABLED, "UART TX", port);
342 if (retval)
343 goto err_tx;
344
345 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, IRQF_DISABLED, "UART RX", port);
346 if (retval)
347 goto err_rx;
348
349 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, IRQF_DISABLED, "UART LineStatus", port);
350 if (retval)
351 goto err_ls;
352
353 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, IRQF_DISABLED, "UART ModemStatus", port);
354 if (retval)
355 goto err_ms;
356
357 return 0;
358
359err_ms:
360 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
361err_ls:
362 free_irq(KS8695_IRQ_UART_RX, port);
363err_rx:
364 free_irq(KS8695_IRQ_UART_TX, port);
365err_tx:
366 return retval;
367}
368
369static void ks8695uart_shutdown(struct uart_port *port)
370{
371 /*
372 * Free the interrupt
373 */
374 free_irq(KS8695_IRQ_UART_RX, port);
375 free_irq(KS8695_IRQ_UART_TX, port);
376 free_irq(KS8695_IRQ_UART_MODEM_STATUS, port);
377 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
378
379 /* disable break condition and fifos */
380 UART_PUT_LCR(port, UART_GET_LCR(port) & ~URLC_URSBC);
381 UART_PUT_FCR(port, UART_GET_FCR(port) & ~URFC_URFE);
382}
383
384static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
385{
386 unsigned int lcr, fcr = 0;
387 unsigned long flags;
388 unsigned int baud, quot;
389
390 /*
391 * Ask the core to calculate the divisor for us.
392 */
393 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
394 quot = uart_get_divisor(port, baud);
395
396 switch (termios->c_cflag & CSIZE) {
397 case CS5:
398 lcr = URCL_5;
399 break;
400 case CS6:
401 lcr = URCL_6;
402 break;
403 case CS7:
404 lcr = URCL_7;
405 break;
406 default:
407 lcr = URCL_8;
408 break;
409 }
410
411 /* stop bits */
412 if (termios->c_cflag & CSTOPB)
413 lcr |= URLC_URSB;
414
415 /* parity */
416 if (termios->c_cflag & PARENB) {
417 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
418 if (termios->c_cflag & PARODD)
419 lcr |= URPE_MARK;
420 else
421 lcr |= URPE_SPACE;
422 }
423 else if (termios->c_cflag & PARODD)
424 lcr |= URPE_ODD;
425 else
426 lcr |= URPE_EVEN;
427 }
428
429 if (port->fifosize > 1)
430 fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
431
432 spin_lock_irqsave(&port->lock, flags);
433
434 /*
435 * Update the per-port timeout.
436 */
437 uart_update_timeout(port, termios->c_cflag, baud);
438
439 port->read_status_mask = URLS_URROE;
440 if (termios->c_iflag & INPCK)
441 port->read_status_mask |= (URLS_URFE | URLS_URPE);
442 if (termios->c_iflag & (BRKINT | PARMRK))
443 port->read_status_mask |= URLS_URBI;
444
445 /*
446 * Characters to ignore
447 */
448 port->ignore_status_mask = 0;
449 if (termios->c_iflag & IGNPAR)
450 port->ignore_status_mask |= (URLS_URFE | URLS_URPE);
451 if (termios->c_iflag & IGNBRK) {
452 port->ignore_status_mask |= URLS_URBI;
453 /*
454 * If we're ignoring parity and break indicators,
455 * ignore overruns too (for real raw support).
456 */
457 if (termios->c_iflag & IGNPAR)
458 port->ignore_status_mask |= URLS_URROE;
459 }
460
461 /*
462 * Ignore all characters if CREAD is not set.
463 */
464 if ((termios->c_cflag & CREAD) == 0)
465 port->ignore_status_mask |= UART_DUMMY_LSR_RX;
466
467 /* first, disable everything */
468 if (UART_ENABLE_MS(port, termios->c_cflag))
469 ks8695uart_enable_ms(port);
470 else
471 ks8695uart_disable_ms(port);
472
473 /* Set baud rate */
474 UART_PUT_BRDR(port, quot);
475
476 UART_PUT_LCR(port, lcr);
477 UART_PUT_FCR(port, fcr);
478
479 spin_unlock_irqrestore(&port->lock, flags);
480}
481
482static const char *ks8695uart_type(struct uart_port *port)
483{
484 return port->type == PORT_KS8695 ? "KS8695" : NULL;
485}
486
487/*
488 * Release the memory region(s) being used by 'port'
489 */
490static void ks8695uart_release_port(struct uart_port *port)
491{
492 release_mem_region(port->mapbase, UART_PORT_SIZE);
493}
494
495/*
496 * Request the memory region(s) being used by 'port'
497 */
498static int ks8695uart_request_port(struct uart_port *port)
499{
500 return request_mem_region(port->mapbase, UART_PORT_SIZE,
501 "serial_ks8695") != NULL ? 0 : -EBUSY;
502}
503
504/*
505 * Configure/autoconfigure the port.
506 */
507static void ks8695uart_config_port(struct uart_port *port, int flags)
508{
509 if (flags & UART_CONFIG_TYPE) {
510 port->type = PORT_KS8695;
511 ks8695uart_request_port(port);
512 }
513}
514
515/*
516 * verify the new serial_struct (for TIOCSSERIAL).
517 */
518static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
519{
520 int ret = 0;
521
522 if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
523 ret = -EINVAL;
524 if (ser->irq != port->irq)
525 ret = -EINVAL;
526 if (ser->baud_base < 9600)
527 ret = -EINVAL;
528 return ret;
529}
530
531static struct uart_ops ks8695uart_pops = {
532 .tx_empty = ks8695uart_tx_empty,
533 .set_mctrl = ks8695uart_set_mctrl,
534 .get_mctrl = ks8695uart_get_mctrl,
535 .stop_tx = ks8695uart_stop_tx,
536 .start_tx = ks8695uart_start_tx,
537 .stop_rx = ks8695uart_stop_rx,
538 .enable_ms = ks8695uart_enable_ms,
539 .break_ctl = ks8695uart_break_ctl,
540 .startup = ks8695uart_startup,
541 .shutdown = ks8695uart_shutdown,
542 .set_termios = ks8695uart_set_termios,
543 .type = ks8695uart_type,
544 .release_port = ks8695uart_release_port,
545 .request_port = ks8695uart_request_port,
546 .config_port = ks8695uart_config_port,
547 .verify_port = ks8695uart_verify_port,
548};
549
550static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
551 {
552 .membase = (void *) KS8695_UART_VA,
553 .mapbase = KS8695_UART_VA,
554 .iotype = SERIAL_IO_MEM,
555 .irq = KS8695_IRQ_UART_TX,
556 .uartclk = KS8695_CLOCK_RATE * 16,
557 .fifosize = 16,
558 .ops = &ks8695uart_pops,
559 .flags = ASYNC_BOOT_AUTOCONF,
560 .line = 0,
561 }
562};
563
564#ifdef CONFIG_SERIAL_KS8695_CONSOLE
565static void ks8695_console_putchar(struct uart_port *port, int ch)
566{
567 while (!(UART_GET_LSR(port) & URLS_URTHRE))
568 barrier();
569
570 UART_PUT_CHAR(port, ch);
571}
572
573static void ks8695_console_write(struct console *co, const char *s, u_int count)
574{
575 struct uart_port *port = ks8695uart_ports + co->index;
576
577 uart_console_write(port, s, count, ks8695_console_putchar);
578}
579
580static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
581{
582 unsigned int lcr;
583
584 lcr = UART_GET_LCR(port);
585
586 switch (lcr & URLC_PARITY) {
587 case URPE_ODD:
588 *parity = 'o';
589 break;
590 case URPE_EVEN:
591 *parity = 'e';
592 break;
593 default:
594 *parity = 'n';
595 }
596
597 switch (lcr & URLC_URCL) {
598 case URCL_5:
599 *bits = 5;
600 break;
601 case URCL_6:
602 *bits = 6;
603 break;
604 case URCL_7:
605 *bits = 7;
606 break;
607 default:
608 *bits = 8;
609 }
610
611 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
612 *baud /= 16;
613 *baud &= 0xFFFFFFF0;
614}
615
616static int __init ks8695_console_setup(struct console *co, char *options)
617{
618 struct uart_port *port;
619 int baud = 115200;
620 int bits = 8;
621 int parity = 'n';
622 int flow = 'n';
623
624 /*
625 * Check whether an invalid uart number has been specified, and
626 * if so, search for the first available port that does have
627 * console support.
628 */
629 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
630
631 if (options)
632 uart_parse_options(options, &baud, &parity, &bits, &flow);
633 else
634 ks8695_console_get_options(port, &baud, &parity, &bits);
635
636 return uart_set_options(port, co, baud, parity, bits, flow);
637}
638
639static struct uart_driver ks8695_reg;
640
641static struct console ks8695_console = {
642 .name = SERIAL_KS8695_DEVNAME,
643 .write = ks8695_console_write,
644 .device = uart_console_device,
645 .setup = ks8695_console_setup,
646 .flags = CON_PRINTBUFFER,
647 .index = -1,
648 .data = &ks8695_reg,
649};
650
651static int __init ks8695_console_init(void)
652{
653 add_preferred_console(SERIAL_KS8695_DEVNAME, 0, NULL);
654 register_console(&ks8695_console);
655 return 0;
656}
657
658console_initcall(ks8695_console_init);
659
660#define KS8695_CONSOLE &ks8695_console
661#else
662#define KS8695_CONSOLE NULL
663#endif
664
665static struct uart_driver ks8695_reg = {
666 .owner = THIS_MODULE,
667 .driver_name = "serial_ks8695",
668 .dev_name = SERIAL_KS8695_DEVNAME,
669 .major = SERIAL_KS8695_MAJOR,
670 .minor = SERIAL_KS8695_MINOR,
671 .nr = SERIAL_KS8695_NR,
672 .cons = KS8695_CONSOLE,
673};
674
675static int __init ks8695uart_init(void)
676{
677 int i, ret;
678
679 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
680
681 ret = uart_register_driver(&ks8695_reg);
682 if (ret)
683 return ret;
684
685 for (i = 0; i < SERIAL_KS8695_NR; i++)
686 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
687
688 return 0;
689}
690
691static void __exit ks8695uart_exit(void)
692{
693 int i;
694
695 for (i = 0; i < SERIAL_KS8695_NR; i++)
696 uart_remove_one_port(&ks8695_reg, &ks8695uart_ports[0]);
697 uart_unregister_driver(&ks8695_reg);
698}
699
700module_init(ks8695uart_init);
701module_exit(ks8695uart_exit);
702
703MODULE_DESCRIPTION("KS8695 serial port driver");
704MODULE_AUTHOR("Micrel Inc.");
705MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/serial_lh7a40x.c b/drivers/tty/serial/serial_lh7a40x.c
new file mode 100644
index 000000000000..ea744707c4d6
--- /dev/null
+++ b/drivers/tty/serial/serial_lh7a40x.c
@@ -0,0 +1,682 @@
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
31#if defined(CONFIG_SERIAL_LH7A40X_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#include <linux/io.h>
45
46#include <asm/irq.h>
47#include <mach/hardware.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)
116{
117 BIT_CLR (port, UART_R_INTEN, TxInt);
118}
119
120static void lh7a40xuart_start_tx (struct uart_port* port)
121{
122 BIT_SET (port, UART_R_INTEN, TxInt);
123
124 /* *** FIXME: do I need to check for startup of the
125 transmitter? The old driver did, but AMBA
126 doesn't . */
127}
128
129static void lh7a40xuart_stop_rx (struct uart_port* port)
130{
131 BIT_SET (port, UART_R_INTEN, RxTimeoutInt | RxInt);
132}
133
134static void lh7a40xuart_enable_ms (struct uart_port* port)
135{
136 BIT_SET (port, UART_R_INTEN, ModemInt);
137}
138
139static void lh7a40xuart_rx_chars (struct uart_port* port)
140{
141 struct tty_struct* tty = port->state->port.tty;
142 int cbRxMax = 256; /* (Gross) limit on receive */
143 unsigned int data; /* Received data and status */
144 unsigned int flag;
145
146 while (!(UR (port, UART_R_STATUS) & nRxRdy) && --cbRxMax) {
147 data = UR (port, UART_R_DATA);
148 flag = TTY_NORMAL;
149 ++port->icount.rx;
150
151 if (unlikely(data & RxError)) {
152 if (data & RxBreak) {
153 data &= ~(RxFramingError | RxParityError);
154 ++port->icount.brk;
155 if (uart_handle_break (port))
156 continue;
157 }
158 else if (data & RxParityError)
159 ++port->icount.parity;
160 else if (data & RxFramingError)
161 ++port->icount.frame;
162 if (data & RxOverrunError)
163 ++port->icount.overrun;
164
165 /* Mask by termios, leave Rx'd byte */
166 data &= port->read_status_mask | 0xff;
167
168 if (data & RxBreak)
169 flag = TTY_BREAK;
170 else if (data & RxParityError)
171 flag = TTY_PARITY;
172 else if (data & RxFramingError)
173 flag = TTY_FRAME;
174 }
175
176 if (uart_handle_sysrq_char (port, (unsigned char) data))
177 continue;
178
179 uart_insert_char(port, data, RxOverrunError, data, flag);
180 }
181 tty_flip_buffer_push (tty);
182 return;
183}
184
185static void lh7a40xuart_tx_chars (struct uart_port* port)
186{
187 struct circ_buf* xmit = &port->state->xmit;
188 int cbTxMax = port->fifosize;
189
190 if (port->x_char) {
191 UR (port, UART_R_DATA) = port->x_char;
192 ++port->icount.tx;
193 port->x_char = 0;
194 return;
195 }
196 if (uart_circ_empty (xmit) || uart_tx_stopped (port)) {
197 lh7a40xuart_stop_tx (port);
198 return;
199 }
200
201 /* Unlike the AMBA UART, the lh7a40x UART does not guarantee
202 that at least half of the FIFO is empty. Instead, we check
203 status for every character. Using the AMBA method causes
204 the transmitter to drop characters. */
205
206 do {
207 UR (port, UART_R_DATA) = xmit->buf[xmit->tail];
208 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
209 ++port->icount.tx;
210 if (uart_circ_empty(xmit))
211 break;
212 } while (!(UR (port, UART_R_STATUS) & nTxRdy)
213 && cbTxMax--);
214
215 if (uart_circ_chars_pending (xmit) < WAKEUP_CHARS)
216 uart_write_wakeup (port);
217
218 if (uart_circ_empty (xmit))
219 lh7a40xuart_stop_tx (port);
220}
221
222static void lh7a40xuart_modem_status (struct uart_port* port)
223{
224 unsigned int status = UR (port, UART_R_STATUS);
225 unsigned int delta
226 = status ^ ((struct uart_port_lh7a40x*) port)->statusPrev;
227
228 BIT_SET (port, UART_R_RAWISR, MSEOI); /* Clear modem status intr */
229
230 if (!delta) /* Only happens if we missed 2 transitions */
231 return;
232
233 ((struct uart_port_lh7a40x*) port)->statusPrev = status;
234
235 if (delta & DCD)
236 uart_handle_dcd_change (port, status & DCD);
237
238 if (delta & DSR)
239 ++port->icount.dsr;
240
241 if (delta & CTS)
242 uart_handle_cts_change (port, status & CTS);
243
244 wake_up_interruptible (&port->state->port.delta_msr_wait);
245}
246
247static irqreturn_t lh7a40xuart_int (int irq, void* dev_id)
248{
249 struct uart_port* port = dev_id;
250 unsigned int cLoopLimit = ISR_LOOP_LIMIT;
251 unsigned int isr = UR (port, UART_R_ISR);
252
253
254 do {
255 if (isr & (RxInt | RxTimeoutInt))
256 lh7a40xuart_rx_chars(port);
257 if (isr & ModemInt)
258 lh7a40xuart_modem_status (port);
259 if (isr & TxInt)
260 lh7a40xuart_tx_chars (port);
261
262 if (--cLoopLimit == 0)
263 break;
264
265 isr = UR (port, UART_R_ISR);
266 } while (isr & (RxInt | TxInt | RxTimeoutInt));
267
268 return IRQ_HANDLED;
269}
270
271static unsigned int lh7a40xuart_tx_empty (struct uart_port* port)
272{
273 return (UR (port, UART_R_STATUS) & TxEmpty) ? TIOCSER_TEMT : 0;
274}
275
276static unsigned int lh7a40xuart_get_mctrl (struct uart_port* port)
277{
278 unsigned int result = 0;
279 unsigned int status = UR (port, UART_R_STATUS);
280
281 if (status & DCD)
282 result |= TIOCM_CAR;
283 if (status & DSR)
284 result |= TIOCM_DSR;
285 if (status & CTS)
286 result |= TIOCM_CTS;
287
288 return result;
289}
290
291static void lh7a40xuart_set_mctrl (struct uart_port* port, unsigned int mctrl)
292{
293 /* None of the ports supports DTR. UART1 supports RTS through GPIO. */
294 /* Note, kernel appears to be setting DTR and RTS on console. */
295
296 /* *** FIXME: this deserves more work. There's some work in
297 tracing all of the IO pins. */
298#if 0
299 if( port->mapbase == UART1_PHYS) {
300 gpioRegs_t *gpio = (gpioRegs_t *)IO_ADDRESS(GPIO_PHYS);
301
302 if (mctrl & TIOCM_RTS)
303 gpio->pbdr &= ~GPIOB_UART1_RTS;
304 else
305 gpio->pbdr |= GPIOB_UART1_RTS;
306 }
307#endif
308}
309
310static void lh7a40xuart_break_ctl (struct uart_port* port, int break_state)
311{
312 unsigned long flags;
313
314 spin_lock_irqsave(&port->lock, flags);
315 if (break_state == -1)
316 BIT_SET (port, UART_R_FCON, BRK); /* Assert break */
317 else
318 BIT_CLR (port, UART_R_FCON, BRK); /* Deassert break */
319 spin_unlock_irqrestore(&port->lock, flags);
320}
321
322static int lh7a40xuart_startup (struct uart_port* port)
323{
324 int retval;
325
326 retval = request_irq (port->irq, lh7a40xuart_int, 0,
327 "serial_lh7a40x", port);
328 if (retval)
329 return retval;
330
331 /* Initial modem control-line settings */
332 ((struct uart_port_lh7a40x*) port)->statusPrev
333 = UR (port, UART_R_STATUS);
334
335 /* There is presently no configuration option to enable IR.
336 Thus, we always disable it. */
337
338 BIT_SET (port, UART_R_CON, UARTEN | SIRDIS);
339 BIT_SET (port, UART_R_INTEN, RxTimeoutInt | RxInt);
340
341 return 0;
342}
343
344static void lh7a40xuart_shutdown (struct uart_port* port)
345{
346 free_irq (port->irq, port);
347 BIT_CLR (port, UART_R_FCON, BRK | FEN);
348 BIT_CLR (port, UART_R_CON, UARTEN);
349}
350
351static void lh7a40xuart_set_termios (struct uart_port* port,
352 struct ktermios* termios,
353 struct ktermios* old)
354{
355 unsigned int con;
356 unsigned int inten;
357 unsigned int fcon;
358 unsigned long flags;
359 unsigned int baud;
360 unsigned int quot;
361
362 baud = uart_get_baud_rate (port, termios, old, 8, port->uartclk/16);
363 quot = uart_get_divisor (port, baud); /* -1 performed elsewhere */
364
365 switch (termios->c_cflag & CSIZE) {
366 case CS5:
367 fcon = WLEN_5;
368 break;
369 case CS6:
370 fcon = WLEN_6;
371 break;
372 case CS7:
373 fcon = WLEN_7;
374 break;
375 case CS8:
376 default:
377 fcon = WLEN_8;
378 break;
379 }
380 if (termios->c_cflag & CSTOPB)
381 fcon |= STP2;
382 if (termios->c_cflag & PARENB) {
383 fcon |= PEN;
384 if (!(termios->c_cflag & PARODD))
385 fcon |= EPS;
386 }
387 if (port->fifosize > 1)
388 fcon |= FEN;
389
390 spin_lock_irqsave (&port->lock, flags);
391
392 uart_update_timeout (port, termios->c_cflag, baud);
393
394 port->read_status_mask = RxOverrunError;
395 if (termios->c_iflag & INPCK)
396 port->read_status_mask |= RxFramingError | RxParityError;
397 if (termios->c_iflag & (BRKINT | PARMRK))
398 port->read_status_mask |= RxBreak;
399
400 /* Figure mask for status we ignore */
401 port->ignore_status_mask = 0;
402 if (termios->c_iflag & IGNPAR)
403 port->ignore_status_mask |= RxFramingError | RxParityError;
404 if (termios->c_iflag & IGNBRK) {
405 port->ignore_status_mask |= RxBreak;
406 /* Ignore overrun when ignorning parity */
407 /* *** FIXME: is this in the right place? */
408 if (termios->c_iflag & IGNPAR)
409 port->ignore_status_mask |= RxOverrunError;
410 }
411
412 /* Ignore all receive errors when receive disabled */
413 if ((termios->c_cflag & CREAD) == 0)
414 port->ignore_status_mask |= RxError;
415
416 con = UR (port, UART_R_CON);
417 inten = (UR (port, UART_R_INTEN) & ~ModemInt);
418
419 if (UART_ENABLE_MS (port, termios->c_cflag))
420 inten |= ModemInt;
421
422 BIT_CLR (port, UART_R_CON, UARTEN); /* Disable UART */
423 UR (port, UART_R_INTEN) = 0; /* Disable interrupts */
424 UR (port, UART_R_BRCON) = quot - 1; /* Set baud rate divisor */
425 UR (port, UART_R_FCON) = fcon; /* Set FIFO and frame ctrl */
426 UR (port, UART_R_INTEN) = inten; /* Enable interrupts */
427 UR (port, UART_R_CON) = con; /* Restore UART mode */
428
429 spin_unlock_irqrestore(&port->lock, flags);
430}
431
432static const char* lh7a40xuart_type (struct uart_port* port)
433{
434 return port->type == PORT_LH7A40X ? "LH7A40X" : NULL;
435}
436
437static void lh7a40xuart_release_port (struct uart_port* port)
438{
439 release_mem_region (port->mapbase, UART_REG_SIZE);
440}
441
442static int lh7a40xuart_request_port (struct uart_port* port)
443{
444 return request_mem_region (port->mapbase, UART_REG_SIZE,
445 "serial_lh7a40x") != NULL
446 ? 0 : -EBUSY;
447}
448
449static void lh7a40xuart_config_port (struct uart_port* port, int flags)
450{
451 if (flags & UART_CONFIG_TYPE) {
452 port->type = PORT_LH7A40X;
453 lh7a40xuart_request_port (port);
454 }
455}
456
457static int lh7a40xuart_verify_port (struct uart_port* port,
458 struct serial_struct* ser)
459{
460 int ret = 0;
461
462 if (ser->type != PORT_UNKNOWN && ser->type != PORT_LH7A40X)
463 ret = -EINVAL;
464 if (ser->irq < 0 || ser->irq >= nr_irqs)
465 ret = -EINVAL;
466 if (ser->baud_base < 9600) /* *** FIXME: is this true? */
467 ret = -EINVAL;
468 return ret;
469}
470
471static struct uart_ops lh7a40x_uart_ops = {
472 .tx_empty = lh7a40xuart_tx_empty,
473 .set_mctrl = lh7a40xuart_set_mctrl,
474 .get_mctrl = lh7a40xuart_get_mctrl,
475 .stop_tx = lh7a40xuart_stop_tx,
476 .start_tx = lh7a40xuart_start_tx,
477 .stop_rx = lh7a40xuart_stop_rx,
478 .enable_ms = lh7a40xuart_enable_ms,
479 .break_ctl = lh7a40xuart_break_ctl,
480 .startup = lh7a40xuart_startup,
481 .shutdown = lh7a40xuart_shutdown,
482 .set_termios = lh7a40xuart_set_termios,
483 .type = lh7a40xuart_type,
484 .release_port = lh7a40xuart_release_port,
485 .request_port = lh7a40xuart_request_port,
486 .config_port = lh7a40xuart_config_port,
487 .verify_port = lh7a40xuart_verify_port,
488};
489
490static struct uart_port_lh7a40x lh7a40x_ports[DEV_NR] = {
491 {
492 .port = {
493 .membase = (void*) io_p2v (UART1_PHYS),
494 .mapbase = UART1_PHYS,
495 .iotype = UPIO_MEM,
496 .irq = IRQ_UART1INTR,
497 .uartclk = 14745600/2,
498 .fifosize = 16,
499 .ops = &lh7a40x_uart_ops,
500 .flags = UPF_BOOT_AUTOCONF,
501 .line = 0,
502 },
503 },
504 {
505 .port = {
506 .membase = (void*) io_p2v (UART2_PHYS),
507 .mapbase = UART2_PHYS,
508 .iotype = UPIO_MEM,
509 .irq = IRQ_UART2INTR,
510 .uartclk = 14745600/2,
511 .fifosize = 16,
512 .ops = &lh7a40x_uart_ops,
513 .flags = UPF_BOOT_AUTOCONF,
514 .line = 1,
515 },
516 },
517 {
518 .port = {
519 .membase = (void*) io_p2v (UART3_PHYS),
520 .mapbase = UART3_PHYS,
521 .iotype = UPIO_MEM,
522 .irq = IRQ_UART3INTR,
523 .uartclk = 14745600/2,
524 .fifosize = 16,
525 .ops = &lh7a40x_uart_ops,
526 .flags = UPF_BOOT_AUTOCONF,
527 .line = 2,
528 },
529 },
530};
531
532#ifndef CONFIG_SERIAL_LH7A40X_CONSOLE
533# define LH7A40X_CONSOLE NULL
534#else
535# define LH7A40X_CONSOLE &lh7a40x_console
536
537static void lh7a40xuart_console_putchar(struct uart_port *port, int ch)
538{
539 while (UR(port, UART_R_STATUS) & nTxRdy)
540 ;
541 UR(port, UART_R_DATA) = ch;
542}
543
544static void lh7a40xuart_console_write (struct console* co,
545 const char* s,
546 unsigned int count)
547{
548 struct uart_port* port = &lh7a40x_ports[co->index].port;
549 unsigned int con = UR (port, UART_R_CON);
550 unsigned int inten = UR (port, UART_R_INTEN);
551
552
553 UR (port, UART_R_INTEN) = 0; /* Disable all interrupts */
554 BIT_SET (port, UART_R_CON, UARTEN | SIRDIS); /* Enable UART */
555
556 uart_console_write(port, s, count, lh7a40xuart_console_putchar);
557
558 /* Wait until all characters are sent */
559 while (UR (port, UART_R_STATUS) & TxBusy)
560 ;
561
562 /* Restore control and interrupt mask */
563 UR (port, UART_R_CON) = con;
564 UR (port, UART_R_INTEN) = inten;
565}
566
567static void __init lh7a40xuart_console_get_options (struct uart_port* port,
568 int* baud,
569 int* parity,
570 int* bits)
571{
572 if (UR (port, UART_R_CON) & UARTEN) {
573 unsigned int fcon = UR (port, UART_R_FCON);
574 unsigned int quot = UR (port, UART_R_BRCON) + 1;
575
576 switch (fcon & (PEN | EPS)) {
577 default: *parity = 'n'; break;
578 case PEN: *parity = 'o'; break;
579 case PEN | EPS: *parity = 'e'; break;
580 }
581
582 switch (fcon & WLEN) {
583 default:
584 case WLEN_8: *bits = 8; break;
585 case WLEN_7: *bits = 7; break;
586 case WLEN_6: *bits = 6; break;
587 case WLEN_5: *bits = 5; break;
588 }
589
590 *baud = port->uartclk/(16*quot);
591 }
592}
593
594static int __init lh7a40xuart_console_setup (struct console* co, char* options)
595{
596 struct uart_port* port;
597 int baud = 38400;
598 int bits = 8;
599 int parity = 'n';
600 int flow = 'n';
601
602 if (co->index >= DEV_NR) /* Bounds check on device number */
603 co->index = 0;
604 port = &lh7a40x_ports[co->index].port;
605
606 if (options)
607 uart_parse_options (options, &baud, &parity, &bits, &flow);
608 else
609 lh7a40xuart_console_get_options (port, &baud, &parity, &bits);
610
611 return uart_set_options (port, co, baud, parity, bits, flow);
612}
613
614static struct uart_driver lh7a40x_reg;
615static struct console lh7a40x_console = {
616 .name = "ttyAM",
617 .write = lh7a40xuart_console_write,
618 .device = uart_console_device,
619 .setup = lh7a40xuart_console_setup,
620 .flags = CON_PRINTBUFFER,
621 .index = -1,
622 .data = &lh7a40x_reg,
623};
624
625static int __init lh7a40xuart_console_init(void)
626{
627 register_console (&lh7a40x_console);
628 return 0;
629}
630
631console_initcall (lh7a40xuart_console_init);
632
633#endif
634
635static struct uart_driver lh7a40x_reg = {
636 .owner = THIS_MODULE,
637 .driver_name = "ttyAM",
638 .dev_name = "ttyAM",
639 .major = DEV_MAJOR,
640 .minor = DEV_MINOR,
641 .nr = DEV_NR,
642 .cons = LH7A40X_CONSOLE,
643};
644
645static int __init lh7a40xuart_init(void)
646{
647 int ret;
648
649 printk (KERN_INFO "serial: LH7A40X serial driver\n");
650
651 ret = uart_register_driver (&lh7a40x_reg);
652
653 if (ret == 0) {
654 int i;
655
656 for (i = 0; i < DEV_NR; i++) {
657 /* UART3, when used, requires GPIO pin reallocation */
658 if (lh7a40x_ports[i].port.mapbase == UART3_PHYS)
659 GPIO_PINMUX |= 1<<3;
660 uart_add_one_port (&lh7a40x_reg,
661 &lh7a40x_ports[i].port);
662 }
663 }
664 return ret;
665}
666
667static void __exit lh7a40xuart_exit(void)
668{
669 int i;
670
671 for (i = 0; i < DEV_NR; i++)
672 uart_remove_one_port (&lh7a40x_reg, &lh7a40x_ports[i].port);
673
674 uart_unregister_driver (&lh7a40x_reg);
675}
676
677module_init (lh7a40xuart_init);
678module_exit (lh7a40xuart_exit);
679
680MODULE_AUTHOR ("Marc Singer");
681MODULE_DESCRIPTION ("Sharp LH7A40X serial port driver");
682MODULE_LICENSE ("GPL");
diff --git a/drivers/tty/serial/serial_txx9.c b/drivers/tty/serial/serial_txx9.c
new file mode 100644
index 000000000000..c50e9fbbf743
--- /dev/null
+++ b/drivers/tty/serial/serial_txx9.c
@@ -0,0 +1,1344 @@
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
20#if defined(CONFIG_SERIAL_TXX9_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21#define SUPPORT_SYSRQ
22#endif
23
24#include <linux/module.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/delay.h>
29#include <linux/platform_device.h>
30#include <linux/pci.h>
31#include <linux/serial_core.h>
32#include <linux/serial.h>
33
34#include <asm/io.h>
35
36static char *serial_version = "1.11";
37static char *serial_name = "TX39/49 Serial driver";
38
39#define PASS_LIMIT 256
40
41#if !defined(CONFIG_SERIAL_TXX9_STDSERIAL)
42/* "ttyS" is used for standard serial driver */
43#define TXX9_TTY_NAME "ttyTX"
44#define TXX9_TTY_MINOR_START 196
45#define TXX9_TTY_MAJOR 204
46#else
47/* acts like standard serial driver */
48#define TXX9_TTY_NAME "ttyS"
49#define TXX9_TTY_MINOR_START 64
50#define TXX9_TTY_MAJOR TTY_MAJOR
51#endif
52
53/* flag aliases */
54#define UPF_TXX9_HAVE_CTS_LINE UPF_BUGGY_UART
55#define UPF_TXX9_USE_SCLK UPF_MAGIC_MULTIPLIER
56
57#ifdef CONFIG_PCI
58/* support for Toshiba TC86C001 SIO */
59#define ENABLE_SERIAL_TXX9_PCI
60#endif
61
62/*
63 * Number of serial ports
64 */
65#define UART_NR CONFIG_SERIAL_TXX9_NR_UARTS
66
67struct uart_txx9_port {
68 struct uart_port port;
69 /* No additional info for now */
70};
71
72#define TXX9_REGION_SIZE 0x24
73
74/* TXX9 Serial Registers */
75#define TXX9_SILCR 0x00
76#define TXX9_SIDICR 0x04
77#define TXX9_SIDISR 0x08
78#define TXX9_SICISR 0x0c
79#define TXX9_SIFCR 0x10
80#define TXX9_SIFLCR 0x14
81#define TXX9_SIBGR 0x18
82#define TXX9_SITFIFO 0x1c
83#define TXX9_SIRFIFO 0x20
84
85/* SILCR : Line Control */
86#define TXX9_SILCR_SCS_MASK 0x00000060
87#define TXX9_SILCR_SCS_IMCLK 0x00000000
88#define TXX9_SILCR_SCS_IMCLK_BG 0x00000020
89#define TXX9_SILCR_SCS_SCLK 0x00000040
90#define TXX9_SILCR_SCS_SCLK_BG 0x00000060
91#define TXX9_SILCR_UEPS 0x00000010
92#define TXX9_SILCR_UPEN 0x00000008
93#define TXX9_SILCR_USBL_MASK 0x00000004
94#define TXX9_SILCR_USBL_1BIT 0x00000000
95#define TXX9_SILCR_USBL_2BIT 0x00000004
96#define TXX9_SILCR_UMODE_MASK 0x00000003
97#define TXX9_SILCR_UMODE_8BIT 0x00000000
98#define TXX9_SILCR_UMODE_7BIT 0x00000001
99
100/* SIDICR : DMA/Int. Control */
101#define TXX9_SIDICR_TDE 0x00008000
102#define TXX9_SIDICR_RDE 0x00004000
103#define TXX9_SIDICR_TIE 0x00002000
104#define TXX9_SIDICR_RIE 0x00001000
105#define TXX9_SIDICR_SPIE 0x00000800
106#define TXX9_SIDICR_CTSAC 0x00000600
107#define TXX9_SIDICR_STIE_MASK 0x0000003f
108#define TXX9_SIDICR_STIE_OERS 0x00000020
109#define TXX9_SIDICR_STIE_CTSS 0x00000010
110#define TXX9_SIDICR_STIE_RBRKD 0x00000008
111#define TXX9_SIDICR_STIE_TRDY 0x00000004
112#define TXX9_SIDICR_STIE_TXALS 0x00000002
113#define TXX9_SIDICR_STIE_UBRKD 0x00000001
114
115/* SIDISR : DMA/Int. Status */
116#define TXX9_SIDISR_UBRK 0x00008000
117#define TXX9_SIDISR_UVALID 0x00004000
118#define TXX9_SIDISR_UFER 0x00002000
119#define TXX9_SIDISR_UPER 0x00001000
120#define TXX9_SIDISR_UOER 0x00000800
121#define TXX9_SIDISR_ERI 0x00000400
122#define TXX9_SIDISR_TOUT 0x00000200
123#define TXX9_SIDISR_TDIS 0x00000100
124#define TXX9_SIDISR_RDIS 0x00000080
125#define TXX9_SIDISR_STIS 0x00000040
126#define TXX9_SIDISR_RFDN_MASK 0x0000001f
127
128/* SICISR : Change Int. Status */
129#define TXX9_SICISR_OERS 0x00000020
130#define TXX9_SICISR_CTSS 0x00000010
131#define TXX9_SICISR_RBRKD 0x00000008
132#define TXX9_SICISR_TRDY 0x00000004
133#define TXX9_SICISR_TXALS 0x00000002
134#define TXX9_SICISR_UBRKD 0x00000001
135
136/* SIFCR : FIFO Control */
137#define TXX9_SIFCR_SWRST 0x00008000
138#define TXX9_SIFCR_RDIL_MASK 0x00000180
139#define TXX9_SIFCR_RDIL_1 0x00000000
140#define TXX9_SIFCR_RDIL_4 0x00000080
141#define TXX9_SIFCR_RDIL_8 0x00000100
142#define TXX9_SIFCR_RDIL_12 0x00000180
143#define TXX9_SIFCR_RDIL_MAX 0x00000180
144#define TXX9_SIFCR_TDIL_MASK 0x00000018
145#define TXX9_SIFCR_TDIL_MASK 0x00000018
146#define TXX9_SIFCR_TDIL_1 0x00000000
147#define TXX9_SIFCR_TDIL_4 0x00000001
148#define TXX9_SIFCR_TDIL_8 0x00000010
149#define TXX9_SIFCR_TDIL_MAX 0x00000010
150#define TXX9_SIFCR_TFRST 0x00000004
151#define TXX9_SIFCR_RFRST 0x00000002
152#define TXX9_SIFCR_FRSTE 0x00000001
153#define TXX9_SIO_TX_FIFO 8
154#define TXX9_SIO_RX_FIFO 16
155
156/* SIFLCR : Flow Control */
157#define TXX9_SIFLCR_RCS 0x00001000
158#define TXX9_SIFLCR_TES 0x00000800
159#define TXX9_SIFLCR_RTSSC 0x00000200
160#define TXX9_SIFLCR_RSDE 0x00000100
161#define TXX9_SIFLCR_TSDE 0x00000080
162#define TXX9_SIFLCR_RTSTL_MASK 0x0000001e
163#define TXX9_SIFLCR_RTSTL_MAX 0x0000001e
164#define TXX9_SIFLCR_TBRK 0x00000001
165
166/* SIBGR : Baudrate Control */
167#define TXX9_SIBGR_BCLK_MASK 0x00000300
168#define TXX9_SIBGR_BCLK_T0 0x00000000
169#define TXX9_SIBGR_BCLK_T2 0x00000100
170#define TXX9_SIBGR_BCLK_T4 0x00000200
171#define TXX9_SIBGR_BCLK_T6 0x00000300
172#define TXX9_SIBGR_BRD_MASK 0x000000ff
173
174static inline unsigned int sio_in(struct uart_txx9_port *up, int offset)
175{
176 switch (up->port.iotype) {
177 default:
178 return __raw_readl(up->port.membase + offset);
179 case UPIO_PORT:
180 return inl(up->port.iobase + offset);
181 }
182}
183
184static inline void
185sio_out(struct uart_txx9_port *up, int offset, int value)
186{
187 switch (up->port.iotype) {
188 default:
189 __raw_writel(value, up->port.membase + offset);
190 break;
191 case UPIO_PORT:
192 outl(value, up->port.iobase + offset);
193 break;
194 }
195}
196
197static inline void
198sio_mask(struct uart_txx9_port *up, int offset, unsigned int value)
199{
200 sio_out(up, offset, sio_in(up, offset) & ~value);
201}
202static inline void
203sio_set(struct uart_txx9_port *up, int offset, unsigned int value)
204{
205 sio_out(up, offset, sio_in(up, offset) | value);
206}
207
208static inline void
209sio_quot_set(struct uart_txx9_port *up, int quot)
210{
211 quot >>= 1;
212 if (quot < 256)
213 sio_out(up, TXX9_SIBGR, quot | TXX9_SIBGR_BCLK_T0);
214 else if (quot < (256 << 2))
215 sio_out(up, TXX9_SIBGR, (quot >> 2) | TXX9_SIBGR_BCLK_T2);
216 else if (quot < (256 << 4))
217 sio_out(up, TXX9_SIBGR, (quot >> 4) | TXX9_SIBGR_BCLK_T4);
218 else if (quot < (256 << 6))
219 sio_out(up, TXX9_SIBGR, (quot >> 6) | TXX9_SIBGR_BCLK_T6);
220 else
221 sio_out(up, TXX9_SIBGR, 0xff | TXX9_SIBGR_BCLK_T6);
222}
223
224static struct uart_txx9_port *to_uart_txx9_port(struct uart_port *port)
225{
226 return container_of(port, struct uart_txx9_port, port);
227}
228
229static void serial_txx9_stop_tx(struct uart_port *port)
230{
231 struct uart_txx9_port *up = to_uart_txx9_port(port);
232 sio_mask(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
233}
234
235static void serial_txx9_start_tx(struct uart_port *port)
236{
237 struct uart_txx9_port *up = to_uart_txx9_port(port);
238 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
239}
240
241static void serial_txx9_stop_rx(struct uart_port *port)
242{
243 struct uart_txx9_port *up = to_uart_txx9_port(port);
244 up->port.read_status_mask &= ~TXX9_SIDISR_RDIS;
245}
246
247static void serial_txx9_enable_ms(struct uart_port *port)
248{
249 /* TXX9-SIO can not control DTR... */
250}
251
252static void serial_txx9_initialize(struct uart_port *port)
253{
254 struct uart_txx9_port *up = to_uart_txx9_port(port);
255 unsigned int tmout = 10000;
256
257 sio_out(up, TXX9_SIFCR, TXX9_SIFCR_SWRST);
258 /* TX4925 BUG WORKAROUND. Accessing SIOC register
259 * immediately after soft reset causes bus error. */
260 mmiowb();
261 udelay(1);
262 while ((sio_in(up, TXX9_SIFCR) & TXX9_SIFCR_SWRST) && --tmout)
263 udelay(1);
264 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
265 sio_set(up, TXX9_SIFCR,
266 TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1);
267 /* initial settings */
268 sio_out(up, TXX9_SILCR,
269 TXX9_SILCR_UMODE_8BIT | TXX9_SILCR_USBL_1BIT |
270 ((up->port.flags & UPF_TXX9_USE_SCLK) ?
271 TXX9_SILCR_SCS_SCLK_BG : TXX9_SILCR_SCS_IMCLK_BG));
272 sio_quot_set(up, uart_get_divisor(port, 9600));
273 sio_out(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSTL_MAX /* 15 */);
274 sio_out(up, TXX9_SIDICR, 0);
275}
276
277static inline void
278receive_chars(struct uart_txx9_port *up, unsigned int *status)
279{
280 struct tty_struct *tty = up->port.state->port.tty;
281 unsigned char ch;
282 unsigned int disr = *status;
283 int max_count = 256;
284 char flag;
285 unsigned int next_ignore_status_mask;
286
287 do {
288 ch = sio_in(up, TXX9_SIRFIFO);
289 flag = TTY_NORMAL;
290 up->port.icount.rx++;
291
292 /* mask out RFDN_MASK bit added by previous overrun */
293 next_ignore_status_mask =
294 up->port.ignore_status_mask & ~TXX9_SIDISR_RFDN_MASK;
295 if (unlikely(disr & (TXX9_SIDISR_UBRK | TXX9_SIDISR_UPER |
296 TXX9_SIDISR_UFER | TXX9_SIDISR_UOER))) {
297 /*
298 * For statistics only
299 */
300 if (disr & TXX9_SIDISR_UBRK) {
301 disr &= ~(TXX9_SIDISR_UFER | TXX9_SIDISR_UPER);
302 up->port.icount.brk++;
303 /*
304 * We do the SysRQ and SAK checking
305 * here because otherwise the break
306 * may get masked by ignore_status_mask
307 * or read_status_mask.
308 */
309 if (uart_handle_break(&up->port))
310 goto ignore_char;
311 } else if (disr & TXX9_SIDISR_UPER)
312 up->port.icount.parity++;
313 else if (disr & TXX9_SIDISR_UFER)
314 up->port.icount.frame++;
315 if (disr & TXX9_SIDISR_UOER) {
316 up->port.icount.overrun++;
317 /*
318 * The receiver read buffer still hold
319 * a char which caused overrun.
320 * Ignore next char by adding RFDN_MASK
321 * to ignore_status_mask temporarily.
322 */
323 next_ignore_status_mask |=
324 TXX9_SIDISR_RFDN_MASK;
325 }
326
327 /*
328 * Mask off conditions which should be ingored.
329 */
330 disr &= up->port.read_status_mask;
331
332 if (disr & TXX9_SIDISR_UBRK) {
333 flag = TTY_BREAK;
334 } else if (disr & TXX9_SIDISR_UPER)
335 flag = TTY_PARITY;
336 else if (disr & TXX9_SIDISR_UFER)
337 flag = TTY_FRAME;
338 }
339 if (uart_handle_sysrq_char(&up->port, ch))
340 goto ignore_char;
341
342 uart_insert_char(&up->port, disr, TXX9_SIDISR_UOER, ch, flag);
343
344 ignore_char:
345 up->port.ignore_status_mask = next_ignore_status_mask;
346 disr = sio_in(up, TXX9_SIDISR);
347 } while (!(disr & TXX9_SIDISR_UVALID) && (max_count-- > 0));
348 spin_unlock(&up->port.lock);
349 tty_flip_buffer_push(tty);
350 spin_lock(&up->port.lock);
351 *status = disr;
352}
353
354static inline void transmit_chars(struct uart_txx9_port *up)
355{
356 struct circ_buf *xmit = &up->port.state->xmit;
357 int count;
358
359 if (up->port.x_char) {
360 sio_out(up, TXX9_SITFIFO, up->port.x_char);
361 up->port.icount.tx++;
362 up->port.x_char = 0;
363 return;
364 }
365 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
366 serial_txx9_stop_tx(&up->port);
367 return;
368 }
369
370 count = TXX9_SIO_TX_FIFO;
371 do {
372 sio_out(up, TXX9_SITFIFO, xmit->buf[xmit->tail]);
373 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
374 up->port.icount.tx++;
375 if (uart_circ_empty(xmit))
376 break;
377 } while (--count > 0);
378
379 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
380 uart_write_wakeup(&up->port);
381
382 if (uart_circ_empty(xmit))
383 serial_txx9_stop_tx(&up->port);
384}
385
386static irqreturn_t serial_txx9_interrupt(int irq, void *dev_id)
387{
388 int pass_counter = 0;
389 struct uart_txx9_port *up = dev_id;
390 unsigned int status;
391
392 while (1) {
393 spin_lock(&up->port.lock);
394 status = sio_in(up, TXX9_SIDISR);
395 if (!(sio_in(up, TXX9_SIDICR) & TXX9_SIDICR_TIE))
396 status &= ~TXX9_SIDISR_TDIS;
397 if (!(status & (TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
398 TXX9_SIDISR_TOUT))) {
399 spin_unlock(&up->port.lock);
400 break;
401 }
402
403 if (status & TXX9_SIDISR_RDIS)
404 receive_chars(up, &status);
405 if (status & TXX9_SIDISR_TDIS)
406 transmit_chars(up);
407 /* Clear TX/RX Int. Status */
408 sio_mask(up, TXX9_SIDISR,
409 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
410 TXX9_SIDISR_TOUT);
411 spin_unlock(&up->port.lock);
412
413 if (pass_counter++ > PASS_LIMIT)
414 break;
415 }
416
417 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
418}
419
420static unsigned int serial_txx9_tx_empty(struct uart_port *port)
421{
422 struct uart_txx9_port *up = to_uart_txx9_port(port);
423 unsigned long flags;
424 unsigned int ret;
425
426 spin_lock_irqsave(&up->port.lock, flags);
427 ret = (sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS) ? TIOCSER_TEMT : 0;
428 spin_unlock_irqrestore(&up->port.lock, flags);
429
430 return ret;
431}
432
433static unsigned int serial_txx9_get_mctrl(struct uart_port *port)
434{
435 struct uart_txx9_port *up = to_uart_txx9_port(port);
436 unsigned int ret;
437
438 /* no modem control lines */
439 ret = TIOCM_CAR | TIOCM_DSR;
440 ret |= (sio_in(up, TXX9_SIFLCR) & TXX9_SIFLCR_RTSSC) ? 0 : TIOCM_RTS;
441 ret |= (sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS) ? 0 : TIOCM_CTS;
442
443 return ret;
444}
445
446static void serial_txx9_set_mctrl(struct uart_port *port, unsigned int mctrl)
447{
448 struct uart_txx9_port *up = to_uart_txx9_port(port);
449
450 if (mctrl & TIOCM_RTS)
451 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
452 else
453 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
454}
455
456static void serial_txx9_break_ctl(struct uart_port *port, int break_state)
457{
458 struct uart_txx9_port *up = to_uart_txx9_port(port);
459 unsigned long flags;
460
461 spin_lock_irqsave(&up->port.lock, flags);
462 if (break_state == -1)
463 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
464 else
465 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
466 spin_unlock_irqrestore(&up->port.lock, flags);
467}
468
469#if defined(CONFIG_SERIAL_TXX9_CONSOLE) || (CONFIG_CONSOLE_POLL)
470/*
471 * Wait for transmitter & holding register to empty
472 */
473static void wait_for_xmitr(struct uart_txx9_port *up)
474{
475 unsigned int tmout = 10000;
476
477 /* Wait up to 10ms for the character(s) to be sent. */
478 while (--tmout &&
479 !(sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS))
480 udelay(1);
481
482 /* Wait up to 1s for flow control if necessary */
483 if (up->port.flags & UPF_CONS_FLOW) {
484 tmout = 1000000;
485 while (--tmout &&
486 (sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS))
487 udelay(1);
488 }
489}
490#endif
491
492#ifdef CONFIG_CONSOLE_POLL
493/*
494 * Console polling routines for writing and reading from the uart while
495 * in an interrupt or debug context.
496 */
497
498static int serial_txx9_get_poll_char(struct uart_port *port)
499{
500 unsigned int ier;
501 unsigned char c;
502 struct uart_txx9_port *up = to_uart_txx9_port(port);
503
504 /*
505 * First save the IER then disable the interrupts
506 */
507 ier = sio_in(up, TXX9_SIDICR);
508 sio_out(up, TXX9_SIDICR, 0);
509
510 while (sio_in(up, TXX9_SIDISR) & TXX9_SIDISR_UVALID)
511 ;
512
513 c = sio_in(up, TXX9_SIRFIFO);
514
515 /*
516 * Finally, clear RX interrupt status
517 * and restore the IER
518 */
519 sio_mask(up, TXX9_SIDISR, TXX9_SIDISR_RDIS);
520 sio_out(up, TXX9_SIDICR, ier);
521 return c;
522}
523
524
525static void serial_txx9_put_poll_char(struct uart_port *port, unsigned char c)
526{
527 unsigned int ier;
528 struct uart_txx9_port *up = to_uart_txx9_port(port);
529
530 /*
531 * First save the IER then disable the interrupts
532 */
533 ier = sio_in(up, TXX9_SIDICR);
534 sio_out(up, TXX9_SIDICR, 0);
535
536 wait_for_xmitr(up);
537 /*
538 * Send the character out.
539 * If a LF, also do CR...
540 */
541 sio_out(up, TXX9_SITFIFO, c);
542 if (c == 10) {
543 wait_for_xmitr(up);
544 sio_out(up, TXX9_SITFIFO, 13);
545 }
546
547 /*
548 * Finally, wait for transmitter to become empty
549 * and restore the IER
550 */
551 wait_for_xmitr(up);
552 sio_out(up, TXX9_SIDICR, ier);
553}
554
555#endif /* CONFIG_CONSOLE_POLL */
556
557static int serial_txx9_startup(struct uart_port *port)
558{
559 struct uart_txx9_port *up = to_uart_txx9_port(port);
560 unsigned long flags;
561 int retval;
562
563 /*
564 * Clear the FIFO buffers and disable them.
565 * (they will be reenabled in set_termios())
566 */
567 sio_set(up, TXX9_SIFCR,
568 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
569 /* clear reset */
570 sio_mask(up, TXX9_SIFCR,
571 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
572 sio_out(up, TXX9_SIDICR, 0);
573
574 /*
575 * Clear the interrupt registers.
576 */
577 sio_out(up, TXX9_SIDISR, 0);
578
579 retval = request_irq(up->port.irq, serial_txx9_interrupt,
580 IRQF_SHARED, "serial_txx9", up);
581 if (retval)
582 return retval;
583
584 /*
585 * Now, initialize the UART
586 */
587 spin_lock_irqsave(&up->port.lock, flags);
588 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
589 spin_unlock_irqrestore(&up->port.lock, flags);
590
591 /* Enable RX/TX */
592 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
593
594 /*
595 * Finally, enable interrupts.
596 */
597 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_RIE);
598
599 return 0;
600}
601
602static void serial_txx9_shutdown(struct uart_port *port)
603{
604 struct uart_txx9_port *up = to_uart_txx9_port(port);
605 unsigned long flags;
606
607 /*
608 * Disable interrupts from this port
609 */
610 sio_out(up, TXX9_SIDICR, 0); /* disable all intrs */
611
612 spin_lock_irqsave(&up->port.lock, flags);
613 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
614 spin_unlock_irqrestore(&up->port.lock, flags);
615
616 /*
617 * Disable break condition
618 */
619 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
620
621#ifdef CONFIG_SERIAL_TXX9_CONSOLE
622 if (up->port.cons && up->port.line == up->port.cons->index) {
623 free_irq(up->port.irq, up);
624 return;
625 }
626#endif
627 /* reset FIFOs */
628 sio_set(up, TXX9_SIFCR,
629 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
630 /* clear reset */
631 sio_mask(up, TXX9_SIFCR,
632 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
633
634 /* Disable RX/TX */
635 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
636
637 free_irq(up->port.irq, up);
638}
639
640static void
641serial_txx9_set_termios(struct uart_port *port, struct ktermios *termios,
642 struct ktermios *old)
643{
644 struct uart_txx9_port *up = to_uart_txx9_port(port);
645 unsigned int cval, fcr = 0;
646 unsigned long flags;
647 unsigned int baud, quot;
648
649 /*
650 * We don't support modem control lines.
651 */
652 termios->c_cflag &= ~(HUPCL | CMSPAR);
653 termios->c_cflag |= CLOCAL;
654
655 cval = sio_in(up, TXX9_SILCR);
656 /* byte size and parity */
657 cval &= ~TXX9_SILCR_UMODE_MASK;
658 switch (termios->c_cflag & CSIZE) {
659 case CS7:
660 cval |= TXX9_SILCR_UMODE_7BIT;
661 break;
662 default:
663 case CS5: /* not supported */
664 case CS6: /* not supported */
665 case CS8:
666 cval |= TXX9_SILCR_UMODE_8BIT;
667 break;
668 }
669
670 cval &= ~TXX9_SILCR_USBL_MASK;
671 if (termios->c_cflag & CSTOPB)
672 cval |= TXX9_SILCR_USBL_2BIT;
673 else
674 cval |= TXX9_SILCR_USBL_1BIT;
675 cval &= ~(TXX9_SILCR_UPEN | TXX9_SILCR_UEPS);
676 if (termios->c_cflag & PARENB)
677 cval |= TXX9_SILCR_UPEN;
678 if (!(termios->c_cflag & PARODD))
679 cval |= TXX9_SILCR_UEPS;
680
681 /*
682 * Ask the core to calculate the divisor for us.
683 */
684 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16/2);
685 quot = uart_get_divisor(port, baud);
686
687 /* Set up FIFOs */
688 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
689 fcr = TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1;
690
691 /*
692 * Ok, we're now changing the port state. Do it with
693 * interrupts disabled.
694 */
695 spin_lock_irqsave(&up->port.lock, flags);
696
697 /*
698 * Update the per-port timeout.
699 */
700 uart_update_timeout(port, termios->c_cflag, baud);
701
702 up->port.read_status_mask = TXX9_SIDISR_UOER |
703 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS;
704 if (termios->c_iflag & INPCK)
705 up->port.read_status_mask |= TXX9_SIDISR_UFER | TXX9_SIDISR_UPER;
706 if (termios->c_iflag & (BRKINT | PARMRK))
707 up->port.read_status_mask |= TXX9_SIDISR_UBRK;
708
709 /*
710 * Characteres to ignore
711 */
712 up->port.ignore_status_mask = 0;
713 if (termios->c_iflag & IGNPAR)
714 up->port.ignore_status_mask |= TXX9_SIDISR_UPER | TXX9_SIDISR_UFER;
715 if (termios->c_iflag & IGNBRK) {
716 up->port.ignore_status_mask |= TXX9_SIDISR_UBRK;
717 /*
718 * If we're ignoring parity and break indicators,
719 * ignore overruns too (for real raw support).
720 */
721 if (termios->c_iflag & IGNPAR)
722 up->port.ignore_status_mask |= TXX9_SIDISR_UOER;
723 }
724
725 /*
726 * ignore all characters if CREAD is not set
727 */
728 if ((termios->c_cflag & CREAD) == 0)
729 up->port.ignore_status_mask |= TXX9_SIDISR_RDIS;
730
731 /* CTS flow control flag */
732 if ((termios->c_cflag & CRTSCTS) &&
733 (up->port.flags & UPF_TXX9_HAVE_CTS_LINE)) {
734 sio_set(up, TXX9_SIFLCR,
735 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
736 } else {
737 sio_mask(up, TXX9_SIFLCR,
738 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
739 }
740
741 sio_out(up, TXX9_SILCR, cval);
742 sio_quot_set(up, quot);
743 sio_out(up, TXX9_SIFCR, fcr);
744
745 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
746 spin_unlock_irqrestore(&up->port.lock, flags);
747}
748
749static void
750serial_txx9_pm(struct uart_port *port, unsigned int state,
751 unsigned int oldstate)
752{
753 /*
754 * If oldstate was -1 this is called from
755 * uart_configure_port(). In this case do not initialize the
756 * port now, because the port was already initialized (for
757 * non-console port) or should not be initialized here (for
758 * console port). If we initialized the port here we lose
759 * serial console settings.
760 */
761 if (state == 0 && oldstate != -1)
762 serial_txx9_initialize(port);
763}
764
765static int serial_txx9_request_resource(struct uart_txx9_port *up)
766{
767 unsigned int size = TXX9_REGION_SIZE;
768 int ret = 0;
769
770 switch (up->port.iotype) {
771 default:
772 if (!up->port.mapbase)
773 break;
774
775 if (!request_mem_region(up->port.mapbase, size, "serial_txx9")) {
776 ret = -EBUSY;
777 break;
778 }
779
780 if (up->port.flags & UPF_IOREMAP) {
781 up->port.membase = ioremap(up->port.mapbase, size);
782 if (!up->port.membase) {
783 release_mem_region(up->port.mapbase, size);
784 ret = -ENOMEM;
785 }
786 }
787 break;
788
789 case UPIO_PORT:
790 if (!request_region(up->port.iobase, size, "serial_txx9"))
791 ret = -EBUSY;
792 break;
793 }
794 return ret;
795}
796
797static void serial_txx9_release_resource(struct uart_txx9_port *up)
798{
799 unsigned int size = TXX9_REGION_SIZE;
800
801 switch (up->port.iotype) {
802 default:
803 if (!up->port.mapbase)
804 break;
805
806 if (up->port.flags & UPF_IOREMAP) {
807 iounmap(up->port.membase);
808 up->port.membase = NULL;
809 }
810
811 release_mem_region(up->port.mapbase, size);
812 break;
813
814 case UPIO_PORT:
815 release_region(up->port.iobase, size);
816 break;
817 }
818}
819
820static void serial_txx9_release_port(struct uart_port *port)
821{
822 struct uart_txx9_port *up = to_uart_txx9_port(port);
823 serial_txx9_release_resource(up);
824}
825
826static int serial_txx9_request_port(struct uart_port *port)
827{
828 struct uart_txx9_port *up = to_uart_txx9_port(port);
829 return serial_txx9_request_resource(up);
830}
831
832static void serial_txx9_config_port(struct uart_port *port, int uflags)
833{
834 struct uart_txx9_port *up = to_uart_txx9_port(port);
835 int ret;
836
837 /*
838 * Find the region that we can probe for. This in turn
839 * tells us whether we can probe for the type of port.
840 */
841 ret = serial_txx9_request_resource(up);
842 if (ret < 0)
843 return;
844 port->type = PORT_TXX9;
845 up->port.fifosize = TXX9_SIO_TX_FIFO;
846
847#ifdef CONFIG_SERIAL_TXX9_CONSOLE
848 if (up->port.line == up->port.cons->index)
849 return;
850#endif
851 serial_txx9_initialize(port);
852}
853
854static const char *
855serial_txx9_type(struct uart_port *port)
856{
857 return "txx9";
858}
859
860static struct uart_ops serial_txx9_pops = {
861 .tx_empty = serial_txx9_tx_empty,
862 .set_mctrl = serial_txx9_set_mctrl,
863 .get_mctrl = serial_txx9_get_mctrl,
864 .stop_tx = serial_txx9_stop_tx,
865 .start_tx = serial_txx9_start_tx,
866 .stop_rx = serial_txx9_stop_rx,
867 .enable_ms = serial_txx9_enable_ms,
868 .break_ctl = serial_txx9_break_ctl,
869 .startup = serial_txx9_startup,
870 .shutdown = serial_txx9_shutdown,
871 .set_termios = serial_txx9_set_termios,
872 .pm = serial_txx9_pm,
873 .type = serial_txx9_type,
874 .release_port = serial_txx9_release_port,
875 .request_port = serial_txx9_request_port,
876 .config_port = serial_txx9_config_port,
877#ifdef CONFIG_CONSOLE_POLL
878 .poll_get_char = serial_txx9_get_poll_char,
879 .poll_put_char = serial_txx9_put_poll_char,
880#endif
881};
882
883static struct uart_txx9_port serial_txx9_ports[UART_NR];
884
885static void __init serial_txx9_register_ports(struct uart_driver *drv,
886 struct device *dev)
887{
888 int i;
889
890 for (i = 0; i < UART_NR; i++) {
891 struct uart_txx9_port *up = &serial_txx9_ports[i];
892
893 up->port.line = i;
894 up->port.ops = &serial_txx9_pops;
895 up->port.dev = dev;
896 if (up->port.iobase || up->port.mapbase)
897 uart_add_one_port(drv, &up->port);
898 }
899}
900
901#ifdef CONFIG_SERIAL_TXX9_CONSOLE
902
903static void serial_txx9_console_putchar(struct uart_port *port, int ch)
904{
905 struct uart_txx9_port *up = to_uart_txx9_port(port);
906
907 wait_for_xmitr(up);
908 sio_out(up, TXX9_SITFIFO, ch);
909}
910
911/*
912 * Print a string to the serial port trying not to disturb
913 * any possible real use of the port...
914 *
915 * The console_lock must be held when we get here.
916 */
917static void
918serial_txx9_console_write(struct console *co, const char *s, unsigned int count)
919{
920 struct uart_txx9_port *up = &serial_txx9_ports[co->index];
921 unsigned int ier, flcr;
922
923 /*
924 * First save the UER then disable the interrupts
925 */
926 ier = sio_in(up, TXX9_SIDICR);
927 sio_out(up, TXX9_SIDICR, 0);
928 /*
929 * Disable flow-control if enabled (and unnecessary)
930 */
931 flcr = sio_in(up, TXX9_SIFLCR);
932 if (!(up->port.flags & UPF_CONS_FLOW) && (flcr & TXX9_SIFLCR_TES))
933 sio_out(up, TXX9_SIFLCR, flcr & ~TXX9_SIFLCR_TES);
934
935 uart_console_write(&up->port, s, count, serial_txx9_console_putchar);
936
937 /*
938 * Finally, wait for transmitter to become empty
939 * and restore the IER
940 */
941 wait_for_xmitr(up);
942 sio_out(up, TXX9_SIFLCR, flcr);
943 sio_out(up, TXX9_SIDICR, ier);
944}
945
946static int __init serial_txx9_console_setup(struct console *co, char *options)
947{
948 struct uart_port *port;
949 struct uart_txx9_port *up;
950 int baud = 9600;
951 int bits = 8;
952 int parity = 'n';
953 int flow = 'n';
954
955 /*
956 * Check whether an invalid uart number has been specified, and
957 * if so, search for the first available port that does have
958 * console support.
959 */
960 if (co->index >= UART_NR)
961 co->index = 0;
962 up = &serial_txx9_ports[co->index];
963 port = &up->port;
964 if (!port->ops)
965 return -ENODEV;
966
967 serial_txx9_initialize(&up->port);
968
969 if (options)
970 uart_parse_options(options, &baud, &parity, &bits, &flow);
971
972 return uart_set_options(port, co, baud, parity, bits, flow);
973}
974
975static struct uart_driver serial_txx9_reg;
976static struct console serial_txx9_console = {
977 .name = TXX9_TTY_NAME,
978 .write = serial_txx9_console_write,
979 .device = uart_console_device,
980 .setup = serial_txx9_console_setup,
981 .flags = CON_PRINTBUFFER,
982 .index = -1,
983 .data = &serial_txx9_reg,
984};
985
986static int __init serial_txx9_console_init(void)
987{
988 register_console(&serial_txx9_console);
989 return 0;
990}
991console_initcall(serial_txx9_console_init);
992
993#define SERIAL_TXX9_CONSOLE &serial_txx9_console
994#else
995#define SERIAL_TXX9_CONSOLE NULL
996#endif
997
998static struct uart_driver serial_txx9_reg = {
999 .owner = THIS_MODULE,
1000 .driver_name = "serial_txx9",
1001 .dev_name = TXX9_TTY_NAME,
1002 .major = TXX9_TTY_MAJOR,
1003 .minor = TXX9_TTY_MINOR_START,
1004 .nr = UART_NR,
1005 .cons = SERIAL_TXX9_CONSOLE,
1006};
1007
1008int __init early_serial_txx9_setup(struct uart_port *port)
1009{
1010 if (port->line >= ARRAY_SIZE(serial_txx9_ports))
1011 return -ENODEV;
1012
1013 serial_txx9_ports[port->line].port = *port;
1014 serial_txx9_ports[port->line].port.ops = &serial_txx9_pops;
1015 serial_txx9_ports[port->line].port.flags |=
1016 UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
1017 return 0;
1018}
1019
1020static DEFINE_MUTEX(serial_txx9_mutex);
1021
1022/**
1023 * serial_txx9_register_port - register a serial port
1024 * @port: serial port template
1025 *
1026 * Configure the serial port specified by the request.
1027 *
1028 * The port is then probed and if necessary the IRQ is autodetected
1029 * If this fails an error is returned.
1030 *
1031 * On success the port is ready to use and the line number is returned.
1032 */
1033static int __devinit serial_txx9_register_port(struct uart_port *port)
1034{
1035 int i;
1036 struct uart_txx9_port *uart;
1037 int ret = -ENOSPC;
1038
1039 mutex_lock(&serial_txx9_mutex);
1040 for (i = 0; i < UART_NR; i++) {
1041 uart = &serial_txx9_ports[i];
1042 if (uart_match_port(&uart->port, port)) {
1043 uart_remove_one_port(&serial_txx9_reg, &uart->port);
1044 break;
1045 }
1046 }
1047 if (i == UART_NR) {
1048 /* Find unused port */
1049 for (i = 0; i < UART_NR; i++) {
1050 uart = &serial_txx9_ports[i];
1051 if (!(uart->port.iobase || uart->port.mapbase))
1052 break;
1053 }
1054 }
1055 if (i < UART_NR) {
1056 uart->port.iobase = port->iobase;
1057 uart->port.membase = port->membase;
1058 uart->port.irq = port->irq;
1059 uart->port.uartclk = port->uartclk;
1060 uart->port.iotype = port->iotype;
1061 uart->port.flags = port->flags
1062 | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
1063 uart->port.mapbase = port->mapbase;
1064 if (port->dev)
1065 uart->port.dev = port->dev;
1066 ret = uart_add_one_port(&serial_txx9_reg, &uart->port);
1067 if (ret == 0)
1068 ret = uart->port.line;
1069 }
1070 mutex_unlock(&serial_txx9_mutex);
1071 return ret;
1072}
1073
1074/**
1075 * serial_txx9_unregister_port - remove a txx9 serial port at runtime
1076 * @line: serial line number
1077 *
1078 * Remove one serial port. This may not be called from interrupt
1079 * context. We hand the port back to the our control.
1080 */
1081static void __devexit serial_txx9_unregister_port(int line)
1082{
1083 struct uart_txx9_port *uart = &serial_txx9_ports[line];
1084
1085 mutex_lock(&serial_txx9_mutex);
1086 uart_remove_one_port(&serial_txx9_reg, &uart->port);
1087 uart->port.flags = 0;
1088 uart->port.type = PORT_UNKNOWN;
1089 uart->port.iobase = 0;
1090 uart->port.mapbase = 0;
1091 uart->port.membase = NULL;
1092 uart->port.dev = NULL;
1093 mutex_unlock(&serial_txx9_mutex);
1094}
1095
1096/*
1097 * Register a set of serial devices attached to a platform device.
1098 */
1099static int __devinit serial_txx9_probe(struct platform_device *dev)
1100{
1101 struct uart_port *p = dev->dev.platform_data;
1102 struct uart_port port;
1103 int ret, i;
1104
1105 memset(&port, 0, sizeof(struct uart_port));
1106 for (i = 0; p && p->uartclk != 0; p++, i++) {
1107 port.iobase = p->iobase;
1108 port.membase = p->membase;
1109 port.irq = p->irq;
1110 port.uartclk = p->uartclk;
1111 port.iotype = p->iotype;
1112 port.flags = p->flags;
1113 port.mapbase = p->mapbase;
1114 port.dev = &dev->dev;
1115 ret = serial_txx9_register_port(&port);
1116 if (ret < 0) {
1117 dev_err(&dev->dev, "unable to register port at index %d "
1118 "(IO%lx MEM%llx IRQ%d): %d\n", i,
1119 p->iobase, (unsigned long long)p->mapbase,
1120 p->irq, ret);
1121 }
1122 }
1123 return 0;
1124}
1125
1126/*
1127 * Remove serial ports registered against a platform device.
1128 */
1129static int __devexit serial_txx9_remove(struct platform_device *dev)
1130{
1131 int i;
1132
1133 for (i = 0; i < UART_NR; i++) {
1134 struct uart_txx9_port *up = &serial_txx9_ports[i];
1135
1136 if (up->port.dev == &dev->dev)
1137 serial_txx9_unregister_port(i);
1138 }
1139 return 0;
1140}
1141
1142#ifdef CONFIG_PM
1143static int serial_txx9_suspend(struct platform_device *dev, pm_message_t state)
1144{
1145 int i;
1146
1147 for (i = 0; i < UART_NR; i++) {
1148 struct uart_txx9_port *up = &serial_txx9_ports[i];
1149
1150 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
1151 uart_suspend_port(&serial_txx9_reg, &up->port);
1152 }
1153
1154 return 0;
1155}
1156
1157static int serial_txx9_resume(struct platform_device *dev)
1158{
1159 int i;
1160
1161 for (i = 0; i < UART_NR; i++) {
1162 struct uart_txx9_port *up = &serial_txx9_ports[i];
1163
1164 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
1165 uart_resume_port(&serial_txx9_reg, &up->port);
1166 }
1167
1168 return 0;
1169}
1170#endif
1171
1172static struct platform_driver serial_txx9_plat_driver = {
1173 .probe = serial_txx9_probe,
1174 .remove = __devexit_p(serial_txx9_remove),
1175#ifdef CONFIG_PM
1176 .suspend = serial_txx9_suspend,
1177 .resume = serial_txx9_resume,
1178#endif
1179 .driver = {
1180 .name = "serial_txx9",
1181 .owner = THIS_MODULE,
1182 },
1183};
1184
1185#ifdef ENABLE_SERIAL_TXX9_PCI
1186/*
1187 * Probe one serial board. Unfortunately, there is no rhyme nor reason
1188 * to the arrangement of serial ports on a PCI card.
1189 */
1190static int __devinit
1191pciserial_txx9_init_one(struct pci_dev *dev, const struct pci_device_id *ent)
1192{
1193 struct uart_port port;
1194 int line;
1195 int rc;
1196
1197 rc = pci_enable_device(dev);
1198 if (rc)
1199 return rc;
1200
1201 memset(&port, 0, sizeof(port));
1202 port.ops = &serial_txx9_pops;
1203 port.flags |= UPF_TXX9_HAVE_CTS_LINE;
1204 port.uartclk = 66670000;
1205 port.irq = dev->irq;
1206 port.iotype = UPIO_PORT;
1207 port.iobase = pci_resource_start(dev, 1);
1208 port.dev = &dev->dev;
1209 line = serial_txx9_register_port(&port);
1210 if (line < 0) {
1211 printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), line);
1212 pci_disable_device(dev);
1213 return line;
1214 }
1215 pci_set_drvdata(dev, &serial_txx9_ports[line]);
1216
1217 return 0;
1218}
1219
1220static void __devexit pciserial_txx9_remove_one(struct pci_dev *dev)
1221{
1222 struct uart_txx9_port *up = pci_get_drvdata(dev);
1223
1224 pci_set_drvdata(dev, NULL);
1225
1226 if (up) {
1227 serial_txx9_unregister_port(up->port.line);
1228 pci_disable_device(dev);
1229 }
1230}
1231
1232#ifdef CONFIG_PM
1233static int pciserial_txx9_suspend_one(struct pci_dev *dev, pm_message_t state)
1234{
1235 struct uart_txx9_port *up = pci_get_drvdata(dev);
1236
1237 if (up)
1238 uart_suspend_port(&serial_txx9_reg, &up->port);
1239 pci_save_state(dev);
1240 pci_set_power_state(dev, pci_choose_state(dev, state));
1241 return 0;
1242}
1243
1244static int pciserial_txx9_resume_one(struct pci_dev *dev)
1245{
1246 struct uart_txx9_port *up = pci_get_drvdata(dev);
1247
1248 pci_set_power_state(dev, PCI_D0);
1249 pci_restore_state(dev);
1250 if (up)
1251 uart_resume_port(&serial_txx9_reg, &up->port);
1252 return 0;
1253}
1254#endif
1255
1256static const struct pci_device_id serial_txx9_pci_tbl[] = {
1257 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC86C001_MISC) },
1258 { 0, }
1259};
1260
1261static struct pci_driver serial_txx9_pci_driver = {
1262 .name = "serial_txx9",
1263 .probe = pciserial_txx9_init_one,
1264 .remove = __devexit_p(pciserial_txx9_remove_one),
1265#ifdef CONFIG_PM
1266 .suspend = pciserial_txx9_suspend_one,
1267 .resume = pciserial_txx9_resume_one,
1268#endif
1269 .id_table = serial_txx9_pci_tbl,
1270};
1271
1272MODULE_DEVICE_TABLE(pci, serial_txx9_pci_tbl);
1273#endif /* ENABLE_SERIAL_TXX9_PCI */
1274
1275static struct platform_device *serial_txx9_plat_devs;
1276
1277static int __init serial_txx9_init(void)
1278{
1279 int ret;
1280
1281 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1282
1283 ret = uart_register_driver(&serial_txx9_reg);
1284 if (ret)
1285 goto out;
1286
1287 serial_txx9_plat_devs = platform_device_alloc("serial_txx9", -1);
1288 if (!serial_txx9_plat_devs) {
1289 ret = -ENOMEM;
1290 goto unreg_uart_drv;
1291 }
1292
1293 ret = platform_device_add(serial_txx9_plat_devs);
1294 if (ret)
1295 goto put_dev;
1296
1297 serial_txx9_register_ports(&serial_txx9_reg,
1298 &serial_txx9_plat_devs->dev);
1299
1300 ret = platform_driver_register(&serial_txx9_plat_driver);
1301 if (ret)
1302 goto del_dev;
1303
1304#ifdef ENABLE_SERIAL_TXX9_PCI
1305 ret = pci_register_driver(&serial_txx9_pci_driver);
1306#endif
1307 if (ret == 0)
1308 goto out;
1309
1310 del_dev:
1311 platform_device_del(serial_txx9_plat_devs);
1312 put_dev:
1313 platform_device_put(serial_txx9_plat_devs);
1314 unreg_uart_drv:
1315 uart_unregister_driver(&serial_txx9_reg);
1316 out:
1317 return ret;
1318}
1319
1320static void __exit serial_txx9_exit(void)
1321{
1322 int i;
1323
1324#ifdef ENABLE_SERIAL_TXX9_PCI
1325 pci_unregister_driver(&serial_txx9_pci_driver);
1326#endif
1327 platform_driver_unregister(&serial_txx9_plat_driver);
1328 platform_device_unregister(serial_txx9_plat_devs);
1329 for (i = 0; i < UART_NR; i++) {
1330 struct uart_txx9_port *up = &serial_txx9_ports[i];
1331 if (up->port.iobase || up->port.mapbase)
1332 uart_remove_one_port(&serial_txx9_reg, &up->port);
1333 }
1334
1335 uart_unregister_driver(&serial_txx9_reg);
1336}
1337
1338module_init(serial_txx9_init);
1339module_exit(serial_txx9_exit);
1340
1341MODULE_LICENSE("GPL");
1342MODULE_DESCRIPTION("TX39/49 serial driver");
1343
1344MODULE_ALIAS_CHARDEV_MAJOR(TXX9_TTY_MAJOR);
diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
new file mode 100644
index 000000000000..c291b3add1d2
--- /dev/null
+++ b/drivers/tty/serial/sh-sci.c
@@ -0,0 +1,2027 @@
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 - 2008 Paul Mundt
7 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
8 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
16 * Removed SH7300 support (Jul 2007).
17 *
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
20 * for more details.
21 */
22#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
25
26#undef DEBUG
27
28#include <linux/module.h>
29#include <linux/errno.h>
30#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/sysrq.h>
38#include <linux/ioport.h>
39#include <linux/mm.h>
40#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/console.h>
43#include <linux/platform_device.h>
44#include <linux/serial_sci.h>
45#include <linux/notifier.h>
46#include <linux/cpufreq.h>
47#include <linux/clk.h>
48#include <linux/ctype.h>
49#include <linux/err.h>
50#include <linux/list.h>
51#include <linux/dmaengine.h>
52#include <linux/scatterlist.h>
53#include <linux/slab.h>
54
55#ifdef CONFIG_SUPERH
56#include <asm/sh_bios.h>
57#endif
58
59#ifdef CONFIG_H8300
60#include <asm/gpio.h>
61#endif
62
63#include "sh-sci.h"
64
65struct sci_port {
66 struct uart_port port;
67
68 /* Port type */
69 unsigned int type;
70
71 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
72 unsigned int irqs[SCIx_NR_IRQS];
73
74 /* Port enable callback */
75 void (*enable)(struct uart_port *port);
76
77 /* Port disable callback */
78 void (*disable)(struct uart_port *port);
79
80 /* Break timer */
81 struct timer_list break_timer;
82 int break_flag;
83
84 /* Interface clock */
85 struct clk *iclk;
86 /* Function clock */
87 struct clk *fclk;
88
89 struct list_head node;
90 struct dma_chan *chan_tx;
91 struct dma_chan *chan_rx;
92#ifdef CONFIG_SERIAL_SH_SCI_DMA
93 struct device *dma_dev;
94 unsigned int slave_tx;
95 unsigned int slave_rx;
96 struct dma_async_tx_descriptor *desc_tx;
97 struct dma_async_tx_descriptor *desc_rx[2];
98 dma_cookie_t cookie_tx;
99 dma_cookie_t cookie_rx[2];
100 dma_cookie_t active_rx;
101 struct scatterlist sg_tx;
102 unsigned int sg_len_tx;
103 struct scatterlist sg_rx[2];
104 size_t buf_len_rx;
105 struct sh_dmae_slave param_tx;
106 struct sh_dmae_slave param_rx;
107 struct work_struct work_tx;
108 struct work_struct work_rx;
109 struct timer_list rx_timer;
110 unsigned int rx_timeout;
111#endif
112};
113
114struct sh_sci_priv {
115 spinlock_t lock;
116 struct list_head ports;
117 struct notifier_block clk_nb;
118};
119
120/* Function prototypes */
121static void sci_stop_tx(struct uart_port *port);
122
123#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
124
125static struct sci_port sci_ports[SCI_NPORTS];
126static struct uart_driver sci_uart_driver;
127
128static inline struct sci_port *
129to_sci_port(struct uart_port *uart)
130{
131 return container_of(uart, struct sci_port, port);
132}
133
134#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
135
136#ifdef CONFIG_CONSOLE_POLL
137static inline void handle_error(struct uart_port *port)
138{
139 /* Clear error flags */
140 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
141}
142
143static int sci_poll_get_char(struct uart_port *port)
144{
145 unsigned short status;
146 int c;
147
148 do {
149 status = sci_in(port, SCxSR);
150 if (status & SCxSR_ERRORS(port)) {
151 handle_error(port);
152 continue;
153 }
154 break;
155 } while (1);
156
157 if (!(status & SCxSR_RDxF(port)))
158 return NO_POLL_CHAR;
159
160 c = sci_in(port, SCxRDR);
161
162 /* Dummy read */
163 sci_in(port, SCxSR);
164 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
165
166 return c;
167}
168#endif
169
170static void sci_poll_put_char(struct uart_port *port, unsigned char c)
171{
172 unsigned short status;
173
174 do {
175 status = sci_in(port, SCxSR);
176 } while (!(status & SCxSR_TDxE(port)));
177
178 sci_out(port, SCxTDR, c);
179 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
180}
181#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
182
183#if defined(__H8300H__) || defined(__H8300S__)
184static void sci_init_pins(struct uart_port *port, unsigned int cflag)
185{
186 int ch = (port->mapbase - SMR0) >> 3;
187
188 /* set DDR regs */
189 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
190 h8300_sci_pins[ch].rx,
191 H8300_GPIO_INPUT);
192 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
193 h8300_sci_pins[ch].tx,
194 H8300_GPIO_OUTPUT);
195
196 /* tx mark output*/
197 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
198}
199#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
200static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
201{
202 if (port->mapbase == 0xA4400000) {
203 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
204 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
205 } else if (port->mapbase == 0xA4410000)
206 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
207}
208#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
209static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
210{
211 unsigned short data;
212
213 if (cflag & CRTSCTS) {
214 /* enable RTS/CTS */
215 if (port->mapbase == 0xa4430000) { /* SCIF0 */
216 /* Clear PTCR bit 9-2; enable all scif pins but sck */
217 data = __raw_readw(PORT_PTCR);
218 __raw_writew((data & 0xfc03), PORT_PTCR);
219 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
220 /* Clear PVCR bit 9-2 */
221 data = __raw_readw(PORT_PVCR);
222 __raw_writew((data & 0xfc03), PORT_PVCR);
223 }
224 } else {
225 if (port->mapbase == 0xa4430000) { /* SCIF0 */
226 /* Clear PTCR bit 5-2; enable only tx and rx */
227 data = __raw_readw(PORT_PTCR);
228 __raw_writew((data & 0xffc3), PORT_PTCR);
229 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
230 /* Clear PVCR bit 5-2 */
231 data = __raw_readw(PORT_PVCR);
232 __raw_writew((data & 0xffc3), PORT_PVCR);
233 }
234 }
235}
236#elif defined(CONFIG_CPU_SH3)
237/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
238static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
239{
240 unsigned short data;
241
242 /* We need to set SCPCR to enable RTS/CTS */
243 data = __raw_readw(SCPCR);
244 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
245 __raw_writew(data & 0x0fcf, SCPCR);
246
247 if (!(cflag & CRTSCTS)) {
248 /* We need to set SCPCR to enable RTS/CTS */
249 data = __raw_readw(SCPCR);
250 /* Clear out SCP7MD1,0, SCP4MD1,0,
251 Set SCP6MD1,0 = {01} (output) */
252 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
253
254 data = __raw_readb(SCPDR);
255 /* Set /RTS2 (bit6) = 0 */
256 __raw_writeb(data & 0xbf, SCPDR);
257 }
258}
259#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
260static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
261{
262 unsigned short data;
263
264 if (port->mapbase == 0xffe00000) {
265 data = __raw_readw(PSCR);
266 data &= ~0x03cf;
267 if (!(cflag & CRTSCTS))
268 data |= 0x0340;
269
270 __raw_writew(data, PSCR);
271 }
272}
273#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
274 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
275 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
276 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
277 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
278 defined(CONFIG_CPU_SUBTYPE_SHX3)
279static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
280{
281 if (!(cflag & CRTSCTS))
282 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
283}
284#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
285static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
286{
287 if (!(cflag & CRTSCTS))
288 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
289}
290#else
291static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
292{
293 /* Nothing to do */
294}
295#endif
296
297#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
298 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
299 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
300 defined(CONFIG_CPU_SUBTYPE_SH7786)
301static int scif_txfill(struct uart_port *port)
302{
303 return sci_in(port, SCTFDR) & 0xff;
304}
305
306static int scif_txroom(struct uart_port *port)
307{
308 return SCIF_TXROOM_MAX - scif_txfill(port);
309}
310
311static int scif_rxfill(struct uart_port *port)
312{
313 return sci_in(port, SCRFDR) & 0xff;
314}
315#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
316static int scif_txfill(struct uart_port *port)
317{
318 if (port->mapbase == 0xffe00000 ||
319 port->mapbase == 0xffe08000)
320 /* SCIF0/1*/
321 return sci_in(port, SCTFDR) & 0xff;
322 else
323 /* SCIF2 */
324 return sci_in(port, SCFDR) >> 8;
325}
326
327static int scif_txroom(struct uart_port *port)
328{
329 if (port->mapbase == 0xffe00000 ||
330 port->mapbase == 0xffe08000)
331 /* SCIF0/1*/
332 return SCIF_TXROOM_MAX - scif_txfill(port);
333 else
334 /* SCIF2 */
335 return SCIF2_TXROOM_MAX - scif_txfill(port);
336}
337
338static int scif_rxfill(struct uart_port *port)
339{
340 if ((port->mapbase == 0xffe00000) ||
341 (port->mapbase == 0xffe08000)) {
342 /* SCIF0/1*/
343 return sci_in(port, SCRFDR) & 0xff;
344 } else {
345 /* SCIF2 */
346 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
347 }
348}
349#elif defined(CONFIG_ARCH_SH7372)
350static int scif_txfill(struct uart_port *port)
351{
352 if (port->type == PORT_SCIFA)
353 return sci_in(port, SCFDR) >> 8;
354 else
355 return sci_in(port, SCTFDR);
356}
357
358static int scif_txroom(struct uart_port *port)
359{
360 return port->fifosize - scif_txfill(port);
361}
362
363static int scif_rxfill(struct uart_port *port)
364{
365 if (port->type == PORT_SCIFA)
366 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
367 else
368 return sci_in(port, SCRFDR);
369}
370#else
371static int scif_txfill(struct uart_port *port)
372{
373 return sci_in(port, SCFDR) >> 8;
374}
375
376static int scif_txroom(struct uart_port *port)
377{
378 return SCIF_TXROOM_MAX - scif_txfill(port);
379}
380
381static int scif_rxfill(struct uart_port *port)
382{
383 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
384}
385#endif
386
387static int sci_txfill(struct uart_port *port)
388{
389 return !(sci_in(port, SCxSR) & SCI_TDRE);
390}
391
392static int sci_txroom(struct uart_port *port)
393{
394 return !sci_txfill(port);
395}
396
397static int sci_rxfill(struct uart_port *port)
398{
399 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
400}
401
402/* ********************************************************************** *
403 * the interrupt related routines *
404 * ********************************************************************** */
405
406static void sci_transmit_chars(struct uart_port *port)
407{
408 struct circ_buf *xmit = &port->state->xmit;
409 unsigned int stopped = uart_tx_stopped(port);
410 unsigned short status;
411 unsigned short ctrl;
412 int count;
413
414 status = sci_in(port, SCxSR);
415 if (!(status & SCxSR_TDxE(port))) {
416 ctrl = sci_in(port, SCSCR);
417 if (uart_circ_empty(xmit))
418 ctrl &= ~SCI_CTRL_FLAGS_TIE;
419 else
420 ctrl |= SCI_CTRL_FLAGS_TIE;
421 sci_out(port, SCSCR, ctrl);
422 return;
423 }
424
425 if (port->type == PORT_SCI)
426 count = sci_txroom(port);
427 else
428 count = scif_txroom(port);
429
430 do {
431 unsigned char c;
432
433 if (port->x_char) {
434 c = port->x_char;
435 port->x_char = 0;
436 } else if (!uart_circ_empty(xmit) && !stopped) {
437 c = xmit->buf[xmit->tail];
438 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
439 } else {
440 break;
441 }
442
443 sci_out(port, SCxTDR, c);
444
445 port->icount.tx++;
446 } while (--count > 0);
447
448 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
449
450 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
451 uart_write_wakeup(port);
452 if (uart_circ_empty(xmit)) {
453 sci_stop_tx(port);
454 } else {
455 ctrl = sci_in(port, SCSCR);
456
457 if (port->type != PORT_SCI) {
458 sci_in(port, SCxSR); /* Dummy read */
459 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
460 }
461
462 ctrl |= SCI_CTRL_FLAGS_TIE;
463 sci_out(port, SCSCR, ctrl);
464 }
465}
466
467/* On SH3, SCIF may read end-of-break as a space->mark char */
468#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
469
470static inline void sci_receive_chars(struct uart_port *port)
471{
472 struct sci_port *sci_port = to_sci_port(port);
473 struct tty_struct *tty = port->state->port.tty;
474 int i, count, copied = 0;
475 unsigned short status;
476 unsigned char flag;
477
478 status = sci_in(port, SCxSR);
479 if (!(status & SCxSR_RDxF(port)))
480 return;
481
482 while (1) {
483 if (port->type == PORT_SCI)
484 count = sci_rxfill(port);
485 else
486 count = scif_rxfill(port);
487
488 /* Don't copy more bytes than there is room for in the buffer */
489 count = tty_buffer_request_room(tty, count);
490
491 /* If for any reason we can't copy more data, we're done! */
492 if (count == 0)
493 break;
494
495 if (port->type == PORT_SCI) {
496 char c = sci_in(port, SCxRDR);
497 if (uart_handle_sysrq_char(port, c) ||
498 sci_port->break_flag)
499 count = 0;
500 else
501 tty_insert_flip_char(tty, c, TTY_NORMAL);
502 } else {
503 for (i = 0; i < count; i++) {
504 char c = sci_in(port, SCxRDR);
505 status = sci_in(port, SCxSR);
506#if defined(CONFIG_CPU_SH3)
507 /* Skip "chars" during break */
508 if (sci_port->break_flag) {
509 if ((c == 0) &&
510 (status & SCxSR_FER(port))) {
511 count--; i--;
512 continue;
513 }
514
515 /* Nonzero => end-of-break */
516 dev_dbg(port->dev, "debounce<%02x>\n", c);
517 sci_port->break_flag = 0;
518
519 if (STEPFN(c)) {
520 count--; i--;
521 continue;
522 }
523 }
524#endif /* CONFIG_CPU_SH3 */
525 if (uart_handle_sysrq_char(port, c)) {
526 count--; i--;
527 continue;
528 }
529
530 /* Store data and status */
531 if (status & SCxSR_FER(port)) {
532 flag = TTY_FRAME;
533 dev_notice(port->dev, "frame error\n");
534 } else if (status & SCxSR_PER(port)) {
535 flag = TTY_PARITY;
536 dev_notice(port->dev, "parity error\n");
537 } else
538 flag = TTY_NORMAL;
539
540 tty_insert_flip_char(tty, c, flag);
541 }
542 }
543
544 sci_in(port, SCxSR); /* dummy read */
545 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
546
547 copied += count;
548 port->icount.rx += count;
549 }
550
551 if (copied) {
552 /* Tell the rest of the system the news. New characters! */
553 tty_flip_buffer_push(tty);
554 } else {
555 sci_in(port, SCxSR); /* dummy read */
556 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
557 }
558}
559
560#define SCI_BREAK_JIFFIES (HZ/20)
561/* The sci generates interrupts during the break,
562 * 1 per millisecond or so during the break period, for 9600 baud.
563 * So dont bother disabling interrupts.
564 * But dont want more than 1 break event.
565 * Use a kernel timer to periodically poll the rx line until
566 * the break is finished.
567 */
568static void sci_schedule_break_timer(struct sci_port *port)
569{
570 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
571 add_timer(&port->break_timer);
572}
573/* Ensure that two consecutive samples find the break over. */
574static void sci_break_timer(unsigned long data)
575{
576 struct sci_port *port = (struct sci_port *)data;
577
578 if (sci_rxd_in(&port->port) == 0) {
579 port->break_flag = 1;
580 sci_schedule_break_timer(port);
581 } else if (port->break_flag == 1) {
582 /* break is over. */
583 port->break_flag = 2;
584 sci_schedule_break_timer(port);
585 } else
586 port->break_flag = 0;
587}
588
589static inline int sci_handle_errors(struct uart_port *port)
590{
591 int copied = 0;
592 unsigned short status = sci_in(port, SCxSR);
593 struct tty_struct *tty = port->state->port.tty;
594
595 if (status & SCxSR_ORER(port)) {
596 /* overrun error */
597 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
598 copied++;
599
600 dev_notice(port->dev, "overrun error");
601 }
602
603 if (status & SCxSR_FER(port)) {
604 if (sci_rxd_in(port) == 0) {
605 /* Notify of BREAK */
606 struct sci_port *sci_port = to_sci_port(port);
607
608 if (!sci_port->break_flag) {
609 sci_port->break_flag = 1;
610 sci_schedule_break_timer(sci_port);
611
612 /* Do sysrq handling. */
613 if (uart_handle_break(port))
614 return 0;
615
616 dev_dbg(port->dev, "BREAK detected\n");
617
618 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
619 copied++;
620 }
621
622 } else {
623 /* frame error */
624 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
625 copied++;
626
627 dev_notice(port->dev, "frame error\n");
628 }
629 }
630
631 if (status & SCxSR_PER(port)) {
632 /* parity error */
633 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
634 copied++;
635
636 dev_notice(port->dev, "parity error");
637 }
638
639 if (copied)
640 tty_flip_buffer_push(tty);
641
642 return copied;
643}
644
645static inline int sci_handle_fifo_overrun(struct uart_port *port)
646{
647 struct tty_struct *tty = port->state->port.tty;
648 int copied = 0;
649
650 if (port->type != PORT_SCIF)
651 return 0;
652
653 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
654 sci_out(port, SCLSR, 0);
655
656 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
657 tty_flip_buffer_push(tty);
658
659 dev_notice(port->dev, "overrun error\n");
660 copied++;
661 }
662
663 return copied;
664}
665
666static inline int sci_handle_breaks(struct uart_port *port)
667{
668 int copied = 0;
669 unsigned short status = sci_in(port, SCxSR);
670 struct tty_struct *tty = port->state->port.tty;
671 struct sci_port *s = to_sci_port(port);
672
673 if (uart_handle_break(port))
674 return 0;
675
676 if (!s->break_flag && status & SCxSR_BRK(port)) {
677#if defined(CONFIG_CPU_SH3)
678 /* Debounce break */
679 s->break_flag = 1;
680#endif
681 /* Notify of BREAK */
682 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
683 copied++;
684
685 dev_dbg(port->dev, "BREAK detected\n");
686 }
687
688 if (copied)
689 tty_flip_buffer_push(tty);
690
691 copied += sci_handle_fifo_overrun(port);
692
693 return copied;
694}
695
696static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
697{
698#ifdef CONFIG_SERIAL_SH_SCI_DMA
699 struct uart_port *port = ptr;
700 struct sci_port *s = to_sci_port(port);
701
702 if (s->chan_rx) {
703 u16 scr = sci_in(port, SCSCR);
704 u16 ssr = sci_in(port, SCxSR);
705
706 /* Disable future Rx interrupts */
707 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
708 disable_irq_nosync(irq);
709 scr |= 0x4000;
710 } else {
711 scr &= ~SCI_CTRL_FLAGS_RIE;
712 }
713 sci_out(port, SCSCR, scr);
714 /* Clear current interrupt */
715 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
716 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
717 jiffies, s->rx_timeout);
718 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
719
720 return IRQ_HANDLED;
721 }
722#endif
723
724 /* I think sci_receive_chars has to be called irrespective
725 * of whether the I_IXOFF is set, otherwise, how is the interrupt
726 * to be disabled?
727 */
728 sci_receive_chars(ptr);
729
730 return IRQ_HANDLED;
731}
732
733static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
734{
735 struct uart_port *port = ptr;
736 unsigned long flags;
737
738 spin_lock_irqsave(&port->lock, flags);
739 sci_transmit_chars(port);
740 spin_unlock_irqrestore(&port->lock, flags);
741
742 return IRQ_HANDLED;
743}
744
745static irqreturn_t sci_er_interrupt(int irq, void *ptr)
746{
747 struct uart_port *port = ptr;
748
749 /* Handle errors */
750 if (port->type == PORT_SCI) {
751 if (sci_handle_errors(port)) {
752 /* discard character in rx buffer */
753 sci_in(port, SCxSR);
754 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
755 }
756 } else {
757 sci_handle_fifo_overrun(port);
758 sci_rx_interrupt(irq, ptr);
759 }
760
761 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
762
763 /* Kick the transmission */
764 sci_tx_interrupt(irq, ptr);
765
766 return IRQ_HANDLED;
767}
768
769static irqreturn_t sci_br_interrupt(int irq, void *ptr)
770{
771 struct uart_port *port = ptr;
772
773 /* Handle BREAKs */
774 sci_handle_breaks(port);
775 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
776
777 return IRQ_HANDLED;
778}
779
780static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
781{
782 unsigned short ssr_status, scr_status, err_enabled;
783 struct uart_port *port = ptr;
784 struct sci_port *s = to_sci_port(port);
785 irqreturn_t ret = IRQ_NONE;
786
787 ssr_status = sci_in(port, SCxSR);
788 scr_status = sci_in(port, SCSCR);
789 err_enabled = scr_status & (SCI_CTRL_FLAGS_REIE | SCI_CTRL_FLAGS_RIE);
790
791 /* Tx Interrupt */
792 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCI_CTRL_FLAGS_TIE) &&
793 !s->chan_tx)
794 ret = sci_tx_interrupt(irq, ptr);
795 /*
796 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
797 * DR flags
798 */
799 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
800 (scr_status & SCI_CTRL_FLAGS_RIE))
801 ret = sci_rx_interrupt(irq, ptr);
802 /* Error Interrupt */
803 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
804 ret = sci_er_interrupt(irq, ptr);
805 /* Break Interrupt */
806 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
807 ret = sci_br_interrupt(irq, ptr);
808
809 return ret;
810}
811
812/*
813 * Here we define a transistion notifier so that we can update all of our
814 * ports' baud rate when the peripheral clock changes.
815 */
816static int sci_notifier(struct notifier_block *self,
817 unsigned long phase, void *p)
818{
819 struct sh_sci_priv *priv = container_of(self,
820 struct sh_sci_priv, clk_nb);
821 struct sci_port *sci_port;
822 unsigned long flags;
823
824 if ((phase == CPUFREQ_POSTCHANGE) ||
825 (phase == CPUFREQ_RESUMECHANGE)) {
826 spin_lock_irqsave(&priv->lock, flags);
827 list_for_each_entry(sci_port, &priv->ports, node)
828 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
829 spin_unlock_irqrestore(&priv->lock, flags);
830 }
831
832 return NOTIFY_OK;
833}
834
835static void sci_clk_enable(struct uart_port *port)
836{
837 struct sci_port *sci_port = to_sci_port(port);
838
839 clk_enable(sci_port->iclk);
840 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
841 clk_enable(sci_port->fclk);
842}
843
844static void sci_clk_disable(struct uart_port *port)
845{
846 struct sci_port *sci_port = to_sci_port(port);
847
848 clk_disable(sci_port->fclk);
849 clk_disable(sci_port->iclk);
850}
851
852static int sci_request_irq(struct sci_port *port)
853{
854 int i;
855 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
856 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
857 sci_br_interrupt,
858 };
859 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
860 "SCI Transmit Data Empty", "SCI Break" };
861
862 if (port->irqs[0] == port->irqs[1]) {
863 if (unlikely(!port->irqs[0]))
864 return -ENODEV;
865
866 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
867 IRQF_DISABLED, "sci", port)) {
868 dev_err(port->port.dev, "Can't allocate IRQ\n");
869 return -ENODEV;
870 }
871 } else {
872 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
873 if (unlikely(!port->irqs[i]))
874 continue;
875
876 if (request_irq(port->irqs[i], handlers[i],
877 IRQF_DISABLED, desc[i], port)) {
878 dev_err(port->port.dev, "Can't allocate IRQ\n");
879 return -ENODEV;
880 }
881 }
882 }
883
884 return 0;
885}
886
887static void sci_free_irq(struct sci_port *port)
888{
889 int i;
890
891 if (port->irqs[0] == port->irqs[1])
892 free_irq(port->irqs[0], port);
893 else {
894 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
895 if (!port->irqs[i])
896 continue;
897
898 free_irq(port->irqs[i], port);
899 }
900 }
901}
902
903static unsigned int sci_tx_empty(struct uart_port *port)
904{
905 unsigned short status = sci_in(port, SCxSR);
906 unsigned short in_tx_fifo = scif_txfill(port);
907
908 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
909}
910
911static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
912{
913 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
914 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
915 /* If you have signals for DTR and DCD, please implement here. */
916}
917
918static unsigned int sci_get_mctrl(struct uart_port *port)
919{
920 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
921 and CTS/RTS */
922
923 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
924}
925
926#ifdef CONFIG_SERIAL_SH_SCI_DMA
927static void sci_dma_tx_complete(void *arg)
928{
929 struct sci_port *s = arg;
930 struct uart_port *port = &s->port;
931 struct circ_buf *xmit = &port->state->xmit;
932 unsigned long flags;
933
934 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
935
936 spin_lock_irqsave(&port->lock, flags);
937
938 xmit->tail += sg_dma_len(&s->sg_tx);
939 xmit->tail &= UART_XMIT_SIZE - 1;
940
941 port->icount.tx += sg_dma_len(&s->sg_tx);
942
943 async_tx_ack(s->desc_tx);
944 s->cookie_tx = -EINVAL;
945 s->desc_tx = NULL;
946
947 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
948 uart_write_wakeup(port);
949
950 if (!uart_circ_empty(xmit)) {
951 schedule_work(&s->work_tx);
952 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
953 u16 ctrl = sci_in(port, SCSCR);
954 sci_out(port, SCSCR, ctrl & ~SCI_CTRL_FLAGS_TIE);
955 }
956
957 spin_unlock_irqrestore(&port->lock, flags);
958}
959
960/* Locking: called with port lock held */
961static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
962 size_t count)
963{
964 struct uart_port *port = &s->port;
965 int i, active, room;
966
967 room = tty_buffer_request_room(tty, count);
968
969 if (s->active_rx == s->cookie_rx[0]) {
970 active = 0;
971 } else if (s->active_rx == s->cookie_rx[1]) {
972 active = 1;
973 } else {
974 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
975 return 0;
976 }
977
978 if (room < count)
979 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
980 count - room);
981 if (!room)
982 return room;
983
984 for (i = 0; i < room; i++)
985 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
986 TTY_NORMAL);
987
988 port->icount.rx += room;
989
990 return room;
991}
992
993static void sci_dma_rx_complete(void *arg)
994{
995 struct sci_port *s = arg;
996 struct uart_port *port = &s->port;
997 struct tty_struct *tty = port->state->port.tty;
998 unsigned long flags;
999 int count;
1000
1001 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
1002
1003 spin_lock_irqsave(&port->lock, flags);
1004
1005 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1006
1007 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1008
1009 spin_unlock_irqrestore(&port->lock, flags);
1010
1011 if (count)
1012 tty_flip_buffer_push(tty);
1013
1014 schedule_work(&s->work_rx);
1015}
1016
1017static void sci_start_rx(struct uart_port *port);
1018static void sci_start_tx(struct uart_port *port);
1019
1020static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1021{
1022 struct dma_chan *chan = s->chan_rx;
1023 struct uart_port *port = &s->port;
1024
1025 s->chan_rx = NULL;
1026 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1027 dma_release_channel(chan);
1028 if (sg_dma_address(&s->sg_rx[0]))
1029 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1030 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
1031 if (enable_pio)
1032 sci_start_rx(port);
1033}
1034
1035static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1036{
1037 struct dma_chan *chan = s->chan_tx;
1038 struct uart_port *port = &s->port;
1039
1040 s->chan_tx = NULL;
1041 s->cookie_tx = -EINVAL;
1042 dma_release_channel(chan);
1043 if (enable_pio)
1044 sci_start_tx(port);
1045}
1046
1047static void sci_submit_rx(struct sci_port *s)
1048{
1049 struct dma_chan *chan = s->chan_rx;
1050 int i;
1051
1052 for (i = 0; i < 2; i++) {
1053 struct scatterlist *sg = &s->sg_rx[i];
1054 struct dma_async_tx_descriptor *desc;
1055
1056 desc = chan->device->device_prep_slave_sg(chan,
1057 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1058
1059 if (desc) {
1060 s->desc_rx[i] = desc;
1061 desc->callback = sci_dma_rx_complete;
1062 desc->callback_param = s;
1063 s->cookie_rx[i] = desc->tx_submit(desc);
1064 }
1065
1066 if (!desc || s->cookie_rx[i] < 0) {
1067 if (i) {
1068 async_tx_ack(s->desc_rx[0]);
1069 s->cookie_rx[0] = -EINVAL;
1070 }
1071 if (desc) {
1072 async_tx_ack(desc);
1073 s->cookie_rx[i] = -EINVAL;
1074 }
1075 dev_warn(s->port.dev,
1076 "failed to re-start DMA, using PIO\n");
1077 sci_rx_dma_release(s, true);
1078 return;
1079 }
1080 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1081 s->cookie_rx[i], i);
1082 }
1083
1084 s->active_rx = s->cookie_rx[0];
1085
1086 dma_async_issue_pending(chan);
1087}
1088
1089static void work_fn_rx(struct work_struct *work)
1090{
1091 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1092 struct uart_port *port = &s->port;
1093 struct dma_async_tx_descriptor *desc;
1094 int new;
1095
1096 if (s->active_rx == s->cookie_rx[0]) {
1097 new = 0;
1098 } else if (s->active_rx == s->cookie_rx[1]) {
1099 new = 1;
1100 } else {
1101 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1102 return;
1103 }
1104 desc = s->desc_rx[new];
1105
1106 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1107 DMA_SUCCESS) {
1108 /* Handle incomplete DMA receive */
1109 struct tty_struct *tty = port->state->port.tty;
1110 struct dma_chan *chan = s->chan_rx;
1111 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1112 async_tx);
1113 unsigned long flags;
1114 int count;
1115
1116 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
1117 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1118 sh_desc->partial, sh_desc->cookie);
1119
1120 spin_lock_irqsave(&port->lock, flags);
1121 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1122 spin_unlock_irqrestore(&port->lock, flags);
1123
1124 if (count)
1125 tty_flip_buffer_push(tty);
1126
1127 sci_submit_rx(s);
1128
1129 return;
1130 }
1131
1132 s->cookie_rx[new] = desc->tx_submit(desc);
1133 if (s->cookie_rx[new] < 0) {
1134 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1135 sci_rx_dma_release(s, true);
1136 return;
1137 }
1138
1139 s->active_rx = s->cookie_rx[!new];
1140
1141 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1142 s->cookie_rx[new], new, s->active_rx);
1143}
1144
1145static void work_fn_tx(struct work_struct *work)
1146{
1147 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1148 struct dma_async_tx_descriptor *desc;
1149 struct dma_chan *chan = s->chan_tx;
1150 struct uart_port *port = &s->port;
1151 struct circ_buf *xmit = &port->state->xmit;
1152 struct scatterlist *sg = &s->sg_tx;
1153
1154 /*
1155 * DMA is idle now.
1156 * Port xmit buffer is already mapped, and it is one page... Just adjust
1157 * offsets and lengths. Since it is a circular buffer, we have to
1158 * transmit till the end, and then the rest. Take the port lock to get a
1159 * consistent xmit buffer state.
1160 */
1161 spin_lock_irq(&port->lock);
1162 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
1163 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
1164 sg->offset;
1165 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
1166 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
1167 spin_unlock_irq(&port->lock);
1168
1169 BUG_ON(!sg_dma_len(sg));
1170
1171 desc = chan->device->device_prep_slave_sg(chan,
1172 sg, s->sg_len_tx, DMA_TO_DEVICE,
1173 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1174 if (!desc) {
1175 /* switch to PIO */
1176 sci_tx_dma_release(s, true);
1177 return;
1178 }
1179
1180 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1181
1182 spin_lock_irq(&port->lock);
1183 s->desc_tx = desc;
1184 desc->callback = sci_dma_tx_complete;
1185 desc->callback_param = s;
1186 spin_unlock_irq(&port->lock);
1187 s->cookie_tx = desc->tx_submit(desc);
1188 if (s->cookie_tx < 0) {
1189 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1190 /* switch to PIO */
1191 sci_tx_dma_release(s, true);
1192 return;
1193 }
1194
1195 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1196 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1197
1198 dma_async_issue_pending(chan);
1199}
1200#endif
1201
1202static void sci_start_tx(struct uart_port *port)
1203{
1204 struct sci_port *s = to_sci_port(port);
1205 unsigned short ctrl;
1206
1207#ifdef CONFIG_SERIAL_SH_SCI_DMA
1208 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1209 u16 new, scr = sci_in(port, SCSCR);
1210 if (s->chan_tx)
1211 new = scr | 0x8000;
1212 else
1213 new = scr & ~0x8000;
1214 if (new != scr)
1215 sci_out(port, SCSCR, new);
1216 }
1217 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1218 s->cookie_tx < 0)
1219 schedule_work(&s->work_tx);
1220#endif
1221 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1222 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1223 ctrl = sci_in(port, SCSCR);
1224 sci_out(port, SCSCR, ctrl | SCI_CTRL_FLAGS_TIE);
1225 }
1226}
1227
1228static void sci_stop_tx(struct uart_port *port)
1229{
1230 unsigned short ctrl;
1231
1232 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
1233 ctrl = sci_in(port, SCSCR);
1234 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1235 ctrl &= ~0x8000;
1236 ctrl &= ~SCI_CTRL_FLAGS_TIE;
1237 sci_out(port, SCSCR, ctrl);
1238}
1239
1240static void sci_start_rx(struct uart_port *port)
1241{
1242 unsigned short ctrl = SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
1243
1244 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
1245 ctrl |= sci_in(port, SCSCR);
1246 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1247 ctrl &= ~0x4000;
1248 sci_out(port, SCSCR, ctrl);
1249}
1250
1251static void sci_stop_rx(struct uart_port *port)
1252{
1253 unsigned short ctrl;
1254
1255 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
1256 ctrl = sci_in(port, SCSCR);
1257 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1258 ctrl &= ~0x4000;
1259 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
1260 sci_out(port, SCSCR, ctrl);
1261}
1262
1263static void sci_enable_ms(struct uart_port *port)
1264{
1265 /* Nothing here yet .. */
1266}
1267
1268static void sci_break_ctl(struct uart_port *port, int break_state)
1269{
1270 /* Nothing here yet .. */
1271}
1272
1273#ifdef CONFIG_SERIAL_SH_SCI_DMA
1274static bool filter(struct dma_chan *chan, void *slave)
1275{
1276 struct sh_dmae_slave *param = slave;
1277
1278 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1279 param->slave_id);
1280
1281 if (param->dma_dev == chan->device->dev) {
1282 chan->private = param;
1283 return true;
1284 } else {
1285 return false;
1286 }
1287}
1288
1289static void rx_timer_fn(unsigned long arg)
1290{
1291 struct sci_port *s = (struct sci_port *)arg;
1292 struct uart_port *port = &s->port;
1293 u16 scr = sci_in(port, SCSCR);
1294
1295 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1296 scr &= ~0x4000;
1297 enable_irq(s->irqs[1]);
1298 }
1299 sci_out(port, SCSCR, scr | SCI_CTRL_FLAGS_RIE);
1300 dev_dbg(port->dev, "DMA Rx timed out\n");
1301 schedule_work(&s->work_rx);
1302}
1303
1304static void sci_request_dma(struct uart_port *port)
1305{
1306 struct sci_port *s = to_sci_port(port);
1307 struct sh_dmae_slave *param;
1308 struct dma_chan *chan;
1309 dma_cap_mask_t mask;
1310 int nent;
1311
1312 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
1313 port->line, s->dma_dev);
1314
1315 if (!s->dma_dev)
1316 return;
1317
1318 dma_cap_zero(mask);
1319 dma_cap_set(DMA_SLAVE, mask);
1320
1321 param = &s->param_tx;
1322
1323 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1324 param->slave_id = s->slave_tx;
1325 param->dma_dev = s->dma_dev;
1326
1327 s->cookie_tx = -EINVAL;
1328 chan = dma_request_channel(mask, filter, param);
1329 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1330 if (chan) {
1331 s->chan_tx = chan;
1332 sg_init_table(&s->sg_tx, 1);
1333 /* UART circular tx buffer is an aligned page. */
1334 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1335 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1336 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1337 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1338 if (!nent)
1339 sci_tx_dma_release(s, false);
1340 else
1341 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1342 sg_dma_len(&s->sg_tx),
1343 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1344
1345 s->sg_len_tx = nent;
1346
1347 INIT_WORK(&s->work_tx, work_fn_tx);
1348 }
1349
1350 param = &s->param_rx;
1351
1352 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1353 param->slave_id = s->slave_rx;
1354 param->dma_dev = s->dma_dev;
1355
1356 chan = dma_request_channel(mask, filter, param);
1357 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1358 if (chan) {
1359 dma_addr_t dma[2];
1360 void *buf[2];
1361 int i;
1362
1363 s->chan_rx = chan;
1364
1365 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1366 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1367 &dma[0], GFP_KERNEL);
1368
1369 if (!buf[0]) {
1370 dev_warn(port->dev,
1371 "failed to allocate dma buffer, using PIO\n");
1372 sci_rx_dma_release(s, true);
1373 return;
1374 }
1375
1376 buf[1] = buf[0] + s->buf_len_rx;
1377 dma[1] = dma[0] + s->buf_len_rx;
1378
1379 for (i = 0; i < 2; i++) {
1380 struct scatterlist *sg = &s->sg_rx[i];
1381
1382 sg_init_table(sg, 1);
1383 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1384 (int)buf[i] & ~PAGE_MASK);
1385 sg_dma_address(sg) = dma[i];
1386 }
1387
1388 INIT_WORK(&s->work_rx, work_fn_rx);
1389 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1390
1391 sci_submit_rx(s);
1392 }
1393}
1394
1395static void sci_free_dma(struct uart_port *port)
1396{
1397 struct sci_port *s = to_sci_port(port);
1398
1399 if (!s->dma_dev)
1400 return;
1401
1402 if (s->chan_tx)
1403 sci_tx_dma_release(s, false);
1404 if (s->chan_rx)
1405 sci_rx_dma_release(s, false);
1406}
1407#endif
1408
1409static int sci_startup(struct uart_port *port)
1410{
1411 struct sci_port *s = to_sci_port(port);
1412
1413 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1414
1415 if (s->enable)
1416 s->enable(port);
1417
1418 sci_request_irq(s);
1419#ifdef CONFIG_SERIAL_SH_SCI_DMA
1420 sci_request_dma(port);
1421#endif
1422 sci_start_tx(port);
1423 sci_start_rx(port);
1424
1425 return 0;
1426}
1427
1428static void sci_shutdown(struct uart_port *port)
1429{
1430 struct sci_port *s = to_sci_port(port);
1431
1432 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1433
1434 sci_stop_rx(port);
1435 sci_stop_tx(port);
1436#ifdef CONFIG_SERIAL_SH_SCI_DMA
1437 sci_free_dma(port);
1438#endif
1439 sci_free_irq(s);
1440
1441 if (s->disable)
1442 s->disable(port);
1443}
1444
1445static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1446 struct ktermios *old)
1447{
1448#ifdef CONFIG_SERIAL_SH_SCI_DMA
1449 struct sci_port *s = to_sci_port(port);
1450#endif
1451 unsigned int status, baud, smr_val, max_baud;
1452 int t = -1;
1453 u16 scfcr = 0;
1454
1455 /*
1456 * earlyprintk comes here early on with port->uartclk set to zero.
1457 * the clock framework is not up and running at this point so here
1458 * we assume that 115200 is the maximum baud rate. please note that
1459 * the baud rate is not programmed during earlyprintk - it is assumed
1460 * that the previous boot loader has enabled required clocks and
1461 * setup the baud rate generator hardware for us already.
1462 */
1463 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1464
1465 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1466 if (likely(baud && port->uartclk))
1467 t = SCBRR_VALUE(baud, port->uartclk);
1468
1469 do {
1470 status = sci_in(port, SCxSR);
1471 } while (!(status & SCxSR_TEND(port)));
1472
1473 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1474
1475 if (port->type != PORT_SCI)
1476 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
1477
1478 smr_val = sci_in(port, SCSMR) & 3;
1479 if ((termios->c_cflag & CSIZE) == CS7)
1480 smr_val |= 0x40;
1481 if (termios->c_cflag & PARENB)
1482 smr_val |= 0x20;
1483 if (termios->c_cflag & PARODD)
1484 smr_val |= 0x30;
1485 if (termios->c_cflag & CSTOPB)
1486 smr_val |= 0x08;
1487
1488 uart_update_timeout(port, termios->c_cflag, baud);
1489
1490 sci_out(port, SCSMR, smr_val);
1491
1492 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
1493 SCSCR_INIT(port));
1494
1495 if (t > 0) {
1496 if (t >= 256) {
1497 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1498 t >>= 2;
1499 } else
1500 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1501
1502 sci_out(port, SCBRR, t);
1503 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1504 }
1505
1506 sci_init_pins(port, termios->c_cflag);
1507 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
1508
1509 sci_out(port, SCSCR, SCSCR_INIT(port));
1510
1511#ifdef CONFIG_SERIAL_SH_SCI_DMA
1512 /*
1513 * Calculate delay for 1.5 DMA buffers: see
1514 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1515 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1516 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1517 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1518 * sizes), but it has been found out experimentally, that this is not
1519 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1520 * as a minimum seem to work perfectly.
1521 */
1522 if (s->chan_rx) {
1523 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1524 port->fifosize / 2;
1525 dev_dbg(port->dev,
1526 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1527 s->rx_timeout * 1000 / HZ, port->timeout);
1528 if (s->rx_timeout < msecs_to_jiffies(20))
1529 s->rx_timeout = msecs_to_jiffies(20);
1530 }
1531#endif
1532
1533 if ((termios->c_cflag & CREAD) != 0)
1534 sci_start_rx(port);
1535}
1536
1537static const char *sci_type(struct uart_port *port)
1538{
1539 switch (port->type) {
1540 case PORT_IRDA:
1541 return "irda";
1542 case PORT_SCI:
1543 return "sci";
1544 case PORT_SCIF:
1545 return "scif";
1546 case PORT_SCIFA:
1547 return "scifa";
1548 case PORT_SCIFB:
1549 return "scifb";
1550 }
1551
1552 return NULL;
1553}
1554
1555static void sci_release_port(struct uart_port *port)
1556{
1557 /* Nothing here yet .. */
1558}
1559
1560static int sci_request_port(struct uart_port *port)
1561{
1562 /* Nothing here yet .. */
1563 return 0;
1564}
1565
1566static void sci_config_port(struct uart_port *port, int flags)
1567{
1568 struct sci_port *s = to_sci_port(port);
1569
1570 port->type = s->type;
1571
1572 if (port->membase)
1573 return;
1574
1575 if (port->flags & UPF_IOREMAP) {
1576 port->membase = ioremap_nocache(port->mapbase, 0x40);
1577
1578 if (IS_ERR(port->membase))
1579 dev_err(port->dev, "can't remap port#%d\n", port->line);
1580 } else {
1581 /*
1582 * For the simple (and majority of) cases where we don't
1583 * need to do any remapping, just cast the cookie
1584 * directly.
1585 */
1586 port->membase = (void __iomem *)port->mapbase;
1587 }
1588}
1589
1590static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1591{
1592 struct sci_port *s = to_sci_port(port);
1593
1594 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
1595 return -EINVAL;
1596 if (ser->baud_base < 2400)
1597 /* No paper tape reader for Mitch.. */
1598 return -EINVAL;
1599
1600 return 0;
1601}
1602
1603static struct uart_ops sci_uart_ops = {
1604 .tx_empty = sci_tx_empty,
1605 .set_mctrl = sci_set_mctrl,
1606 .get_mctrl = sci_get_mctrl,
1607 .start_tx = sci_start_tx,
1608 .stop_tx = sci_stop_tx,
1609 .stop_rx = sci_stop_rx,
1610 .enable_ms = sci_enable_ms,
1611 .break_ctl = sci_break_ctl,
1612 .startup = sci_startup,
1613 .shutdown = sci_shutdown,
1614 .set_termios = sci_set_termios,
1615 .type = sci_type,
1616 .release_port = sci_release_port,
1617 .request_port = sci_request_port,
1618 .config_port = sci_config_port,
1619 .verify_port = sci_verify_port,
1620#ifdef CONFIG_CONSOLE_POLL
1621 .poll_get_char = sci_poll_get_char,
1622 .poll_put_char = sci_poll_put_char,
1623#endif
1624};
1625
1626static int __devinit sci_init_single(struct platform_device *dev,
1627 struct sci_port *sci_port,
1628 unsigned int index,
1629 struct plat_sci_port *p)
1630{
1631 struct uart_port *port = &sci_port->port;
1632
1633 port->ops = &sci_uart_ops;
1634 port->iotype = UPIO_MEM;
1635 port->line = index;
1636
1637 switch (p->type) {
1638 case PORT_SCIFB:
1639 port->fifosize = 256;
1640 break;
1641 case PORT_SCIFA:
1642 port->fifosize = 64;
1643 break;
1644 case PORT_SCIF:
1645 port->fifosize = 16;
1646 break;
1647 default:
1648 port->fifosize = 1;
1649 break;
1650 }
1651
1652 if (dev) {
1653 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1654 if (IS_ERR(sci_port->iclk)) {
1655 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1656 if (IS_ERR(sci_port->iclk)) {
1657 dev_err(&dev->dev, "can't get iclk\n");
1658 return PTR_ERR(sci_port->iclk);
1659 }
1660 }
1661
1662 /*
1663 * The function clock is optional, ignore it if we can't
1664 * find it.
1665 */
1666 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1667 if (IS_ERR(sci_port->fclk))
1668 sci_port->fclk = NULL;
1669
1670 sci_port->enable = sci_clk_enable;
1671 sci_port->disable = sci_clk_disable;
1672 port->dev = &dev->dev;
1673 }
1674
1675 sci_port->break_timer.data = (unsigned long)sci_port;
1676 sci_port->break_timer.function = sci_break_timer;
1677 init_timer(&sci_port->break_timer);
1678
1679 port->mapbase = p->mapbase;
1680 port->membase = p->membase;
1681
1682 port->irq = p->irqs[SCIx_TXI_IRQ];
1683 port->flags = p->flags;
1684 sci_port->type = port->type = p->type;
1685
1686#ifdef CONFIG_SERIAL_SH_SCI_DMA
1687 sci_port->dma_dev = p->dma_dev;
1688 sci_port->slave_tx = p->dma_slave_tx;
1689 sci_port->slave_rx = p->dma_slave_rx;
1690
1691 dev_dbg(port->dev, "%s: DMA device %p, tx %d, rx %d\n", __func__,
1692 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
1693#endif
1694
1695 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
1696 return 0;
1697}
1698
1699#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1700static struct tty_driver *serial_console_device(struct console *co, int *index)
1701{
1702 struct uart_driver *p = &sci_uart_driver;
1703 *index = co->index;
1704 return p->tty_driver;
1705}
1706
1707static void serial_console_putchar(struct uart_port *port, int ch)
1708{
1709 sci_poll_put_char(port, ch);
1710}
1711
1712/*
1713 * Print a string to the serial port trying not to disturb
1714 * any possible real use of the port...
1715 */
1716static void serial_console_write(struct console *co, const char *s,
1717 unsigned count)
1718{
1719 struct uart_port *port = co->data;
1720 struct sci_port *sci_port = to_sci_port(port);
1721 unsigned short bits;
1722
1723 if (sci_port->enable)
1724 sci_port->enable(port);
1725
1726 uart_console_write(port, s, count, serial_console_putchar);
1727
1728 /* wait until fifo is empty and last bit has been transmitted */
1729 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1730 while ((sci_in(port, SCxSR) & bits) != bits)
1731 cpu_relax();
1732
1733 if (sci_port->disable)
1734 sci_port->disable(port);
1735}
1736
1737static int __devinit serial_console_setup(struct console *co, char *options)
1738{
1739 struct sci_port *sci_port;
1740 struct uart_port *port;
1741 int baud = 115200;
1742 int bits = 8;
1743 int parity = 'n';
1744 int flow = 'n';
1745 int ret;
1746
1747 /*
1748 * Check whether an invalid uart number has been specified, and
1749 * if so, search for the first available port that does have
1750 * console support.
1751 */
1752 if (co->index >= SCI_NPORTS)
1753 co->index = 0;
1754
1755 if (co->data) {
1756 port = co->data;
1757 sci_port = to_sci_port(port);
1758 } else {
1759 sci_port = &sci_ports[co->index];
1760 port = &sci_port->port;
1761 co->data = port;
1762 }
1763
1764 /*
1765 * Also need to check port->type, we don't actually have any
1766 * UPIO_PORT ports, but uart_report_port() handily misreports
1767 * it anyways if we don't have a port available by the time this is
1768 * called.
1769 */
1770 if (!port->type)
1771 return -ENODEV;
1772
1773 sci_config_port(port, 0);
1774
1775 if (sci_port->enable)
1776 sci_port->enable(port);
1777
1778 if (options)
1779 uart_parse_options(options, &baud, &parity, &bits, &flow);
1780
1781 ret = uart_set_options(port, co, baud, parity, bits, flow);
1782#if defined(__H8300H__) || defined(__H8300S__)
1783 /* disable rx interrupt */
1784 if (ret == 0)
1785 sci_stop_rx(port);
1786#endif
1787 /* TODO: disable clock */
1788 return ret;
1789}
1790
1791static struct console serial_console = {
1792 .name = "ttySC",
1793 .device = serial_console_device,
1794 .write = serial_console_write,
1795 .setup = serial_console_setup,
1796 .flags = CON_PRINTBUFFER,
1797 .index = -1,
1798};
1799
1800static int __init sci_console_init(void)
1801{
1802 register_console(&serial_console);
1803 return 0;
1804}
1805console_initcall(sci_console_init);
1806
1807static struct sci_port early_serial_port;
1808static struct console early_serial_console = {
1809 .name = "early_ttySC",
1810 .write = serial_console_write,
1811 .flags = CON_PRINTBUFFER,
1812};
1813static char early_serial_buf[32];
1814
1815#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1816
1817#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1818#define SCI_CONSOLE (&serial_console)
1819#else
1820#define SCI_CONSOLE 0
1821#endif
1822
1823static char banner[] __initdata =
1824 KERN_INFO "SuperH SCI(F) driver initialized\n";
1825
1826static struct uart_driver sci_uart_driver = {
1827 .owner = THIS_MODULE,
1828 .driver_name = "sci",
1829 .dev_name = "ttySC",
1830 .major = SCI_MAJOR,
1831 .minor = SCI_MINOR_START,
1832 .nr = SCI_NPORTS,
1833 .cons = SCI_CONSOLE,
1834};
1835
1836
1837static int sci_remove(struct platform_device *dev)
1838{
1839 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1840 struct sci_port *p;
1841 unsigned long flags;
1842
1843 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1844
1845 spin_lock_irqsave(&priv->lock, flags);
1846 list_for_each_entry(p, &priv->ports, node) {
1847 uart_remove_one_port(&sci_uart_driver, &p->port);
1848 clk_put(p->iclk);
1849 clk_put(p->fclk);
1850 }
1851 spin_unlock_irqrestore(&priv->lock, flags);
1852
1853 kfree(priv);
1854 return 0;
1855}
1856
1857static int __devinit sci_probe_single(struct platform_device *dev,
1858 unsigned int index,
1859 struct plat_sci_port *p,
1860 struct sci_port *sciport)
1861{
1862 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1863 unsigned long flags;
1864 int ret;
1865
1866 /* Sanity check */
1867 if (unlikely(index >= SCI_NPORTS)) {
1868 dev_notice(&dev->dev, "Attempting to register port "
1869 "%d when only %d are available.\n",
1870 index+1, SCI_NPORTS);
1871 dev_notice(&dev->dev, "Consider bumping "
1872 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1873 return 0;
1874 }
1875
1876 ret = sci_init_single(dev, sciport, index, p);
1877 if (ret)
1878 return ret;
1879
1880 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
1881 if (ret)
1882 return ret;
1883
1884 INIT_LIST_HEAD(&sciport->node);
1885
1886 spin_lock_irqsave(&priv->lock, flags);
1887 list_add(&sciport->node, &priv->ports);
1888 spin_unlock_irqrestore(&priv->lock, flags);
1889
1890 return 0;
1891}
1892
1893/*
1894 * Register a set of serial devices attached to a platform device. The
1895 * list is terminated with a zero flags entry, which means we expect
1896 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1897 * remapping (such as sh64) should also set UPF_IOREMAP.
1898 */
1899static int __devinit sci_probe(struct platform_device *dev)
1900{
1901 struct plat_sci_port *p = dev->dev.platform_data;
1902 struct sh_sci_priv *priv;
1903 int i, ret = -EINVAL;
1904
1905#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1906 if (is_early_platform_device(dev)) {
1907 if (dev->id == -1)
1908 return -ENOTSUPP;
1909 early_serial_console.index = dev->id;
1910 early_serial_console.data = &early_serial_port.port;
1911 sci_init_single(NULL, &early_serial_port, dev->id, p);
1912 serial_console_setup(&early_serial_console, early_serial_buf);
1913 if (!strstr(early_serial_buf, "keep"))
1914 early_serial_console.flags |= CON_BOOT;
1915 register_console(&early_serial_console);
1916 return 0;
1917 }
1918#endif
1919
1920 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1921 if (!priv)
1922 return -ENOMEM;
1923
1924 INIT_LIST_HEAD(&priv->ports);
1925 spin_lock_init(&priv->lock);
1926 platform_set_drvdata(dev, priv);
1927
1928 priv->clk_nb.notifier_call = sci_notifier;
1929 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1930
1931 if (dev->id != -1) {
1932 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1933 if (ret)
1934 goto err_unreg;
1935 } else {
1936 for (i = 0; p && p->flags != 0; p++, i++) {
1937 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1938 if (ret)
1939 goto err_unreg;
1940 }
1941 }
1942
1943#ifdef CONFIG_SH_STANDARD_BIOS
1944 sh_bios_gdb_detach();
1945#endif
1946
1947 return 0;
1948
1949err_unreg:
1950 sci_remove(dev);
1951 return ret;
1952}
1953
1954static int sci_suspend(struct device *dev)
1955{
1956 struct sh_sci_priv *priv = dev_get_drvdata(dev);
1957 struct sci_port *p;
1958 unsigned long flags;
1959
1960 spin_lock_irqsave(&priv->lock, flags);
1961 list_for_each_entry(p, &priv->ports, node)
1962 uart_suspend_port(&sci_uart_driver, &p->port);
1963 spin_unlock_irqrestore(&priv->lock, flags);
1964
1965 return 0;
1966}
1967
1968static int sci_resume(struct device *dev)
1969{
1970 struct sh_sci_priv *priv = dev_get_drvdata(dev);
1971 struct sci_port *p;
1972 unsigned long flags;
1973
1974 spin_lock_irqsave(&priv->lock, flags);
1975 list_for_each_entry(p, &priv->ports, node)
1976 uart_resume_port(&sci_uart_driver, &p->port);
1977 spin_unlock_irqrestore(&priv->lock, flags);
1978
1979 return 0;
1980}
1981
1982static const struct dev_pm_ops sci_dev_pm_ops = {
1983 .suspend = sci_suspend,
1984 .resume = sci_resume,
1985};
1986
1987static struct platform_driver sci_driver = {
1988 .probe = sci_probe,
1989 .remove = sci_remove,
1990 .driver = {
1991 .name = "sh-sci",
1992 .owner = THIS_MODULE,
1993 .pm = &sci_dev_pm_ops,
1994 },
1995};
1996
1997static int __init sci_init(void)
1998{
1999 int ret;
2000
2001 printk(banner);
2002
2003 ret = uart_register_driver(&sci_uart_driver);
2004 if (likely(ret == 0)) {
2005 ret = platform_driver_register(&sci_driver);
2006 if (unlikely(ret))
2007 uart_unregister_driver(&sci_uart_driver);
2008 }
2009
2010 return ret;
2011}
2012
2013static void __exit sci_exit(void)
2014{
2015 platform_driver_unregister(&sci_driver);
2016 uart_unregister_driver(&sci_uart_driver);
2017}
2018
2019#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2020early_platform_init_buffer("earlyprintk", &sci_driver,
2021 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2022#endif
2023module_init(sci_init);
2024module_exit(sci_exit);
2025
2026MODULE_LICENSE("GPL");
2027MODULE_ALIAS("platform:sh-sci");
diff --git a/drivers/tty/serial/sh-sci.h b/drivers/tty/serial/sh-sci.h
new file mode 100644
index 000000000000..4bc614e4221c
--- /dev/null
+++ b/drivers/tty/serial/sh-sci.h
@@ -0,0 +1,660 @@
1#include <linux/serial_core.h>
2#include <linux/io.h>
3#include <linux/gpio.h>
4
5#if defined(CONFIG_H83007) || defined(CONFIG_H83068)
6#include <asm/regs306x.h>
7#endif
8#if defined(CONFIG_H8S2678)
9#include <asm/regs267x.h>
10#endif
11
12#if defined(CONFIG_CPU_SUBTYPE_SH7706) || \
13 defined(CONFIG_CPU_SUBTYPE_SH7707) || \
14 defined(CONFIG_CPU_SUBTYPE_SH7708) || \
15 defined(CONFIG_CPU_SUBTYPE_SH7709)
16# define SCPCR 0xA4000116 /* 16 bit SCI and SCIF */
17# define SCPDR 0xA4000136 /* 8 bit SCI and SCIF */
18# define SCSCR_INIT(port) 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */
19#elif defined(CONFIG_CPU_SUBTYPE_SH7705)
20# define SCIF0 0xA4400000
21# define SCIF2 0xA4410000
22# define SCSMR_Ir 0xA44A0000
23# define IRDA_SCIF SCIF0
24# define SCPCR 0xA4000116
25# define SCPDR 0xA4000136
26
27/* Set the clock source,
28 * SCIF2 (0xA4410000) -> External clock, SCK pin used as clock input
29 * SCIF0 (0xA4400000) -> Internal clock, SCK pin as serial clock output
30 */
31# define SCSCR_INIT(port) (port->mapbase == SCIF2) ? 0xF3 : 0xF0
32#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || \
33 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
34 defined(CONFIG_ARCH_SH73A0) || \
35 defined(CONFIG_ARCH_SH7367) || \
36 defined(CONFIG_ARCH_SH7377) || \
37 defined(CONFIG_ARCH_SH7372)
38# define SCSCR_INIT(port) 0x0030 /* TIE=0,RIE=0,TE=1,RE=1 */
39# define PORT_PTCR 0xA405011EUL
40# define PORT_PVCR 0xA4050122UL
41# define SCIF_ORER 0x0200 /* overrun error bit */
42#elif defined(CONFIG_SH_RTS7751R2D)
43# define SCSPTR1 0xFFE0001C /* 8 bit SCIF */
44# define SCSPTR2 0xFFE80020 /* 16 bit SCIF */
45# define SCIF_ORER 0x0001 /* overrun error bit */
46# define SCSCR_INIT(port) 0x3a /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
47#elif defined(CONFIG_CPU_SUBTYPE_SH7750) || \
48 defined(CONFIG_CPU_SUBTYPE_SH7750R) || \
49 defined(CONFIG_CPU_SUBTYPE_SH7750S) || \
50 defined(CONFIG_CPU_SUBTYPE_SH7091) || \
51 defined(CONFIG_CPU_SUBTYPE_SH7751) || \
52 defined(CONFIG_CPU_SUBTYPE_SH7751R)
53# define SCSPTR1 0xffe0001c /* 8 bit SCI */
54# define SCSPTR2 0xFFE80020 /* 16 bit SCIF */
55# define SCIF_ORER 0x0001 /* overrun error bit */
56# define SCSCR_INIT(port) (((port)->type == PORT_SCI) ? \
57 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */ : \
58 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */ )
59#elif defined(CONFIG_CPU_SUBTYPE_SH7760)
60# define SCSPTR0 0xfe600024 /* 16 bit SCIF */
61# define SCSPTR1 0xfe610024 /* 16 bit SCIF */
62# define SCSPTR2 0xfe620024 /* 16 bit SCIF */
63# define SCIF_ORER 0x0001 /* overrun error bit */
64# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
65#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
66# define SCSPTR0 0xA4400000 /* 16 bit SCIF */
67# define SCIF_ORER 0x0001 /* overrun error bit */
68# define PACR 0xa4050100
69# define PBCR 0xa4050102
70# define SCSCR_INIT(port) 0x3B
71#elif defined(CONFIG_CPU_SUBTYPE_SH7343)
72# define SCSPTR0 0xffe00010 /* 16 bit SCIF */
73# define SCSPTR1 0xffe10010 /* 16 bit SCIF */
74# define SCSPTR2 0xffe20010 /* 16 bit SCIF */
75# define SCSPTR3 0xffe30010 /* 16 bit SCIF */
76# define SCSCR_INIT(port) 0x32 /* TIE=0,RIE=0,TE=1,RE=1,REIE=0,CKE=1 */
77#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
78# define PADR 0xA4050120
79# define PSDR 0xA405013e
80# define PWDR 0xA4050166
81# define PSCR 0xA405011E
82# define SCIF_ORER 0x0001 /* overrun error bit */
83# define SCSCR_INIT(port) 0x0038 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
84#elif defined(CONFIG_CPU_SUBTYPE_SH7366)
85# define SCPDR0 0xA405013E /* 16 bit SCIF0 PSDR */
86# define SCSPTR0 SCPDR0
87# define SCIF_ORER 0x0001 /* overrun error bit */
88# define SCSCR_INIT(port) 0x0038 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
89#elif defined(CONFIG_CPU_SUBTYPE_SH7723)
90# define SCSPTR0 0xa4050160
91# define SCSPTR1 0xa405013e
92# define SCSPTR2 0xa4050160
93# define SCSPTR3 0xa405013e
94# define SCSPTR4 0xa4050128
95# define SCSPTR5 0xa4050128
96# define SCIF_ORER 0x0001 /* overrun error bit */
97# define SCSCR_INIT(port) 0x0038 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
98#elif defined(CONFIG_CPU_SUBTYPE_SH7724)
99# define SCIF_ORER 0x0001 /* overrun error bit */
100# define SCSCR_INIT(port) ((port)->type == PORT_SCIFA ? \
101 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */ : \
102 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */ )
103#elif defined(CONFIG_CPU_SUBTYPE_SH4_202)
104# define SCSPTR2 0xffe80020 /* 16 bit SCIF */
105# define SCIF_ORER 0x0001 /* overrun error bit */
106# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
107#elif defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
108# define SCIF_BASE_ADDR 0x01030000
109# define SCIF_ADDR_SH5 PHYS_PERIPHERAL_BLOCK+SCIF_BASE_ADDR
110# define SCIF_PTR2_OFFS 0x0000020
111# define SCIF_LSR2_OFFS 0x0000024
112# define SCSPTR2 ((port->mapbase)+SCIF_PTR2_OFFS) /* 16 bit SCIF */
113# define SCLSR2 ((port->mapbase)+SCIF_LSR2_OFFS) /* 16 bit SCIF */
114# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0, TE=1,RE=1,REIE=1 */
115#elif defined(CONFIG_H83007) || defined(CONFIG_H83068)
116# define SCSCR_INIT(port) 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */
117# define H8300_SCI_DR(ch) *(volatile char *)(P1DR + h8300_sci_pins[ch].port)
118#elif defined(CONFIG_H8S2678)
119# define SCSCR_INIT(port) 0x30 /* TIE=0,RIE=0,TE=1,RE=1 */
120# define H8300_SCI_DR(ch) *(volatile char *)(P1DR + h8300_sci_pins[ch].port)
121#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
122# define SCSPTR0 0xfe4b0020
123# define SCSPTR1 0xfe4b0020
124# define SCSPTR2 0xfe4b0020
125# define SCIF_ORER 0x0001
126# define SCSCR_INIT(port) 0x38
127# define SCIF_ONLY
128#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
129# define SCSPTR0 0xffe00024 /* 16 bit SCIF */
130# define SCSPTR1 0xffe08024 /* 16 bit SCIF */
131# define SCSPTR2 0xffe10020 /* 16 bit SCIF/IRDA */
132# define SCIF_ORER 0x0001 /* overrun error bit */
133# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
134#elif defined(CONFIG_CPU_SUBTYPE_SH7770)
135# define SCSPTR0 0xff923020 /* 16 bit SCIF */
136# define SCSPTR1 0xff924020 /* 16 bit SCIF */
137# define SCSPTR2 0xff925020 /* 16 bit SCIF */
138# define SCIF_ORER 0x0001 /* overrun error bit */
139# define SCSCR_INIT(port) 0x3c /* TIE=0,RIE=0,TE=1,RE=1,REIE=1,cke=2 */
140#elif defined(CONFIG_CPU_SUBTYPE_SH7780)
141# define SCSPTR0 0xffe00024 /* 16 bit SCIF */
142# define SCSPTR1 0xffe10024 /* 16 bit SCIF */
143# define SCIF_ORER 0x0001 /* Overrun error bit */
144
145#if defined(CONFIG_SH_SH2007)
146/* TIE=0,RIE=0,TE=1,RE=1,REIE=1,CKE1=0 */
147# define SCSCR_INIT(port) 0x38
148#else
149/* TIE=0,RIE=0,TE=1,RE=1,REIE=1,CKE1=1 */
150# define SCSCR_INIT(port) 0x3a
151#endif
152
153#elif defined(CONFIG_CPU_SUBTYPE_SH7785) || \
154 defined(CONFIG_CPU_SUBTYPE_SH7786)
155# define SCSPTR0 0xffea0024 /* 16 bit SCIF */
156# define SCSPTR1 0xffeb0024 /* 16 bit SCIF */
157# define SCSPTR2 0xffec0024 /* 16 bit SCIF */
158# define SCSPTR3 0xffed0024 /* 16 bit SCIF */
159# define SCSPTR4 0xffee0024 /* 16 bit SCIF */
160# define SCSPTR5 0xffef0024 /* 16 bit SCIF */
161# define SCIF_ORER 0x0001 /* Overrun error bit */
162# define SCSCR_INIT(port) 0x3a /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
163#elif defined(CONFIG_CPU_SUBTYPE_SH7201) || \
164 defined(CONFIG_CPU_SUBTYPE_SH7203) || \
165 defined(CONFIG_CPU_SUBTYPE_SH7206) || \
166 defined(CONFIG_CPU_SUBTYPE_SH7263)
167# define SCSPTR0 0xfffe8020 /* 16 bit SCIF */
168# define SCSPTR1 0xfffe8820 /* 16 bit SCIF */
169# define SCSPTR2 0xfffe9020 /* 16 bit SCIF */
170# define SCSPTR3 0xfffe9820 /* 16 bit SCIF */
171# if defined(CONFIG_CPU_SUBTYPE_SH7201)
172# define SCSPTR4 0xfffeA020 /* 16 bit SCIF */
173# define SCSPTR5 0xfffeA820 /* 16 bit SCIF */
174# define SCSPTR6 0xfffeB020 /* 16 bit SCIF */
175# define SCSPTR7 0xfffeB820 /* 16 bit SCIF */
176# endif
177# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
178#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
179# define SCSPTR0 0xf8400020 /* 16 bit SCIF */
180# define SCSPTR1 0xf8410020 /* 16 bit SCIF */
181# define SCSPTR2 0xf8420020 /* 16 bit SCIF */
182# define SCIF_ORER 0x0001 /* overrun error bit */
183# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
184#elif defined(CONFIG_CPU_SUBTYPE_SHX3)
185# define SCSPTR0 0xffc30020 /* 16 bit SCIF */
186# define SCSPTR1 0xffc40020 /* 16 bit SCIF */
187# define SCSPTR2 0xffc50020 /* 16 bit SCIF */
188# define SCSPTR3 0xffc60020 /* 16 bit SCIF */
189# define SCIF_ORER 0x0001 /* Overrun error bit */
190# define SCSCR_INIT(port) 0x38 /* TIE=0,RIE=0,TE=1,RE=1,REIE=1 */
191#else
192# error CPU subtype not defined
193#endif
194
195/* SCSCR */
196#define SCI_CTRL_FLAGS_TIE 0x80 /* all */
197#define SCI_CTRL_FLAGS_RIE 0x40 /* all */
198#define SCI_CTRL_FLAGS_TE 0x20 /* all */
199#define SCI_CTRL_FLAGS_RE 0x10 /* all */
200#if defined(CONFIG_CPU_SUBTYPE_SH7750) || \
201 defined(CONFIG_CPU_SUBTYPE_SH7091) || \
202 defined(CONFIG_CPU_SUBTYPE_SH7750R) || \
203 defined(CONFIG_CPU_SUBTYPE_SH7722) || \
204 defined(CONFIG_CPU_SUBTYPE_SH7750S) || \
205 defined(CONFIG_CPU_SUBTYPE_SH7751) || \
206 defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
207 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
208 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
209 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
210 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
211 defined(CONFIG_CPU_SUBTYPE_SHX3)
212#define SCI_CTRL_FLAGS_REIE 0x08 /* 7750 SCIF */
213#elif defined(CONFIG_CPU_SUBTYPE_SH7724)
214#define SCI_CTRL_FLAGS_REIE ((port)->type == PORT_SCIFA ? 0 : 8)
215#else
216#define SCI_CTRL_FLAGS_REIE 0
217#endif
218/* SCI_CTRL_FLAGS_MPIE 0x08 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
219/* SCI_CTRL_FLAGS_TEIE 0x04 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
220/* SCI_CTRL_FLAGS_CKE1 0x02 * all */
221/* SCI_CTRL_FLAGS_CKE0 0x01 * 7707 SCI/SCIF, 7708 SCI, 7709 SCI/SCIF, 7750 SCI */
222
223/* SCxSR SCI */
224#define SCI_TDRE 0x80 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
225#define SCI_RDRF 0x40 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
226#define SCI_ORER 0x20 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
227#define SCI_FER 0x10 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
228#define SCI_PER 0x08 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
229#define SCI_TEND 0x04 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
230/* SCI_MPB 0x02 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
231/* SCI_MPBT 0x01 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
232
233#define SCI_ERRORS ( SCI_PER | SCI_FER | SCI_ORER)
234
235/* SCxSR SCIF */
236#define SCIF_ER 0x0080 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
237#define SCIF_TEND 0x0040 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
238#define SCIF_TDFE 0x0020 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
239#define SCIF_BRK 0x0010 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
240#define SCIF_FER 0x0008 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
241#define SCIF_PER 0x0004 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
242#define SCIF_RDF 0x0002 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
243#define SCIF_DR 0x0001 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
244
245#if defined(CONFIG_CPU_SUBTYPE_SH7705) || \
246 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
247 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
248 defined(CONFIG_ARCH_SH73A0) || \
249 defined(CONFIG_ARCH_SH7367) || \
250 defined(CONFIG_ARCH_SH7377) || \
251 defined(CONFIG_ARCH_SH7372)
252# define SCIF_ORER 0x0200
253# define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK | SCIF_ORER)
254# define SCIF_RFDC_MASK 0x007f
255# define SCIF_TXROOM_MAX 64
256#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
257# define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK )
258# define SCIF_RFDC_MASK 0x007f
259# define SCIF_TXROOM_MAX 64
260/* SH7763 SCIF2 support */
261# define SCIF2_RFDC_MASK 0x001f
262# define SCIF2_TXROOM_MAX 16
263#else
264# define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK)
265# define SCIF_RFDC_MASK 0x001f
266# define SCIF_TXROOM_MAX 16
267#endif
268
269#ifndef SCIF_ORER
270#define SCIF_ORER 0x0000
271#endif
272
273#define SCxSR_TEND(port) (((port)->type == PORT_SCI) ? SCI_TEND : SCIF_TEND)
274#define SCxSR_ERRORS(port) (((port)->type == PORT_SCI) ? SCI_ERRORS : SCIF_ERRORS)
275#define SCxSR_RDxF(port) (((port)->type == PORT_SCI) ? SCI_RDRF : SCIF_RDF)
276#define SCxSR_TDxE(port) (((port)->type == PORT_SCI) ? SCI_TDRE : SCIF_TDFE)
277#define SCxSR_FER(port) (((port)->type == PORT_SCI) ? SCI_FER : SCIF_FER)
278#define SCxSR_PER(port) (((port)->type == PORT_SCI) ? SCI_PER : SCIF_PER)
279#define SCxSR_BRK(port) (((port)->type == PORT_SCI) ? 0x00 : SCIF_BRK)
280#define SCxSR_ORER(port) (((port)->type == PORT_SCI) ? SCI_ORER : SCIF_ORER)
281
282#if defined(CONFIG_CPU_SUBTYPE_SH7705) || \
283 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
284 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
285 defined(CONFIG_ARCH_SH73A0) || \
286 defined(CONFIG_ARCH_SH7367) || \
287 defined(CONFIG_ARCH_SH7377) || \
288 defined(CONFIG_ARCH_SH7372)
289# define SCxSR_RDxF_CLEAR(port) (sci_in(port, SCxSR) & 0xfffc)
290# define SCxSR_ERROR_CLEAR(port) (sci_in(port, SCxSR) & 0xfd73)
291# define SCxSR_TDxE_CLEAR(port) (sci_in(port, SCxSR) & 0xffdf)
292# define SCxSR_BREAK_CLEAR(port) (sci_in(port, SCxSR) & 0xffe3)
293#else
294# define SCxSR_RDxF_CLEAR(port) (((port)->type == PORT_SCI) ? 0xbc : 0x00fc)
295# define SCxSR_ERROR_CLEAR(port) (((port)->type == PORT_SCI) ? 0xc4 : 0x0073)
296# define SCxSR_TDxE_CLEAR(port) (((port)->type == PORT_SCI) ? 0x78 : 0x00df)
297# define SCxSR_BREAK_CLEAR(port) (((port)->type == PORT_SCI) ? 0xc4 : 0x00e3)
298#endif
299
300/* SCFCR */
301#define SCFCR_RFRST 0x0002
302#define SCFCR_TFRST 0x0004
303#define SCFCR_TCRST 0x4000
304#define SCFCR_MCE 0x0008
305
306#define SCI_MAJOR 204
307#define SCI_MINOR_START 8
308
309/* Generic serial flags */
310#define SCI_RX_THROTTLE 0x0000001
311
312#define SCI_MAGIC 0xbabeface
313
314/*
315 * Events are used to schedule things to happen at timer-interrupt
316 * time, instead of at rs interrupt time.
317 */
318#define SCI_EVENT_WRITE_WAKEUP 0
319
320#define SCI_IN(size, offset) \
321 if ((size) == 8) { \
322 return ioread8(port->membase + (offset)); \
323 } else { \
324 return ioread16(port->membase + (offset)); \
325 }
326#define SCI_OUT(size, offset, value) \
327 if ((size) == 8) { \
328 iowrite8(value, port->membase + (offset)); \
329 } else if ((size) == 16) { \
330 iowrite16(value, port->membase + (offset)); \
331 }
332
333#define CPU_SCIx_FNS(name, sci_offset, sci_size, scif_offset, scif_size)\
334 static inline unsigned int sci_##name##_in(struct uart_port *port) \
335 { \
336 if (port->type == PORT_SCIF || port->type == PORT_SCIFB) { \
337 SCI_IN(scif_size, scif_offset) \
338 } else { /* PORT_SCI or PORT_SCIFA */ \
339 SCI_IN(sci_size, sci_offset); \
340 } \
341 } \
342 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
343 { \
344 if (port->type == PORT_SCIF || port->type == PORT_SCIFB) { \
345 SCI_OUT(scif_size, scif_offset, value) \
346 } else { /* PORT_SCI or PORT_SCIFA */ \
347 SCI_OUT(sci_size, sci_offset, value); \
348 } \
349 }
350
351#ifdef CONFIG_H8300
352/* h8300 don't have SCIF */
353#define CPU_SCIF_FNS(name) \
354 static inline unsigned int sci_##name##_in(struct uart_port *port) \
355 { \
356 return 0; \
357 } \
358 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
359 { \
360 }
361#else
362#define CPU_SCIF_FNS(name, scif_offset, scif_size) \
363 static inline unsigned int sci_##name##_in(struct uart_port *port) \
364 { \
365 SCI_IN(scif_size, scif_offset); \
366 } \
367 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
368 { \
369 SCI_OUT(scif_size, scif_offset, value); \
370 }
371#endif
372
373#define CPU_SCI_FNS(name, sci_offset, sci_size) \
374 static inline unsigned int sci_##name##_in(struct uart_port* port) \
375 { \
376 SCI_IN(sci_size, sci_offset); \
377 } \
378 static inline void sci_##name##_out(struct uart_port* port, unsigned int value) \
379 { \
380 SCI_OUT(sci_size, sci_offset, value); \
381 }
382
383#if defined(CONFIG_CPU_SH3) || \
384 defined(CONFIG_ARCH_SH73A0) || \
385 defined(CONFIG_ARCH_SH7367) || \
386 defined(CONFIG_ARCH_SH7377) || \
387 defined(CONFIG_ARCH_SH7372)
388#if defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
389#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
390 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
391 h8_sci_offset, h8_sci_size) \
392 CPU_SCIx_FNS(name, sh4_sci_offset, sh4_sci_size, sh4_scif_offset, sh4_scif_size)
393#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
394 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
395#elif defined(CONFIG_CPU_SUBTYPE_SH7705) || \
396 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
397 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
398 defined(CONFIG_ARCH_SH73A0) || \
399 defined(CONFIG_ARCH_SH7367) || \
400 defined(CONFIG_ARCH_SH7377)
401#define SCIF_FNS(name, scif_offset, scif_size) \
402 CPU_SCIF_FNS(name, scif_offset, scif_size)
403#elif defined(CONFIG_ARCH_SH7372)
404#define SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scifb_offset, sh4_scifb_size) \
405 CPU_SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scifb_offset, sh4_scifb_size)
406#define SCIF_FNS(name, scif_offset, scif_size) \
407 CPU_SCIF_FNS(name, scif_offset, scif_size)
408#else
409#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
410 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
411 h8_sci_offset, h8_sci_size) \
412 CPU_SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh3_scif_offset, sh3_scif_size)
413#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
414 CPU_SCIF_FNS(name, sh3_scif_offset, sh3_scif_size)
415#endif
416#elif defined(__H8300H__) || defined(__H8300S__)
417#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
418 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
419 h8_sci_offset, h8_sci_size) \
420 CPU_SCI_FNS(name, h8_sci_offset, h8_sci_size)
421#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
422 CPU_SCIF_FNS(name)
423#elif defined(CONFIG_CPU_SUBTYPE_SH7723) ||\
424 defined(CONFIG_CPU_SUBTYPE_SH7724)
425 #define SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scif_offset, sh4_scif_size) \
426 CPU_SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scif_offset, sh4_scif_size)
427 #define SCIF_FNS(name, sh4_scif_offset, sh4_scif_size) \
428 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
429#else
430#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
431 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
432 h8_sci_offset, h8_sci_size) \
433 CPU_SCIx_FNS(name, sh4_sci_offset, sh4_sci_size, sh4_scif_offset, sh4_scif_size)
434#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
435 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
436#endif
437
438#if defined(CONFIG_CPU_SUBTYPE_SH7705) || \
439 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
440 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
441 defined(CONFIG_ARCH_SH73A0) || \
442 defined(CONFIG_ARCH_SH7367) || \
443 defined(CONFIG_ARCH_SH7377)
444
445SCIF_FNS(SCSMR, 0x00, 16)
446SCIF_FNS(SCBRR, 0x04, 8)
447SCIF_FNS(SCSCR, 0x08, 16)
448SCIF_FNS(SCTDSR, 0x0c, 8)
449SCIF_FNS(SCFER, 0x10, 16)
450SCIF_FNS(SCxSR, 0x14, 16)
451SCIF_FNS(SCFCR, 0x18, 16)
452SCIF_FNS(SCFDR, 0x1c, 16)
453SCIF_FNS(SCxTDR, 0x20, 8)
454SCIF_FNS(SCxRDR, 0x24, 8)
455SCIF_FNS(SCLSR, 0x00, 0)
456#elif defined(CONFIG_ARCH_SH7372)
457SCIF_FNS(SCSMR, 0x00, 16)
458SCIF_FNS(SCBRR, 0x04, 8)
459SCIF_FNS(SCSCR, 0x08, 16)
460SCIF_FNS(SCTDSR, 0x0c, 16)
461SCIF_FNS(SCFER, 0x10, 16)
462SCIF_FNS(SCxSR, 0x14, 16)
463SCIF_FNS(SCFCR, 0x18, 16)
464SCIF_FNS(SCFDR, 0x1c, 16)
465SCIF_FNS(SCTFDR, 0x38, 16)
466SCIF_FNS(SCRFDR, 0x3c, 16)
467SCIx_FNS(SCxTDR, 0x20, 8, 0x40, 8)
468SCIx_FNS(SCxRDR, 0x24, 8, 0x60, 8)
469SCIF_FNS(SCLSR, 0x00, 0)
470#elif defined(CONFIG_CPU_SUBTYPE_SH7723) ||\
471 defined(CONFIG_CPU_SUBTYPE_SH7724)
472SCIx_FNS(SCSMR, 0x00, 16, 0x00, 16)
473SCIx_FNS(SCBRR, 0x04, 8, 0x04, 8)
474SCIx_FNS(SCSCR, 0x08, 16, 0x08, 16)
475SCIx_FNS(SCxTDR, 0x20, 8, 0x0c, 8)
476SCIx_FNS(SCxSR, 0x14, 16, 0x10, 16)
477SCIx_FNS(SCxRDR, 0x24, 8, 0x14, 8)
478SCIx_FNS(SCSPTR, 0, 0, 0, 0)
479SCIF_FNS(SCTDSR, 0x0c, 8)
480SCIF_FNS(SCFER, 0x10, 16)
481SCIF_FNS(SCFCR, 0x18, 16)
482SCIF_FNS(SCFDR, 0x1c, 16)
483SCIF_FNS(SCLSR, 0x24, 16)
484#else
485/* reg SCI/SH3 SCI/SH4 SCIF/SH3 SCIF/SH4 SCI/H8*/
486/* name off sz off sz off sz off sz off sz*/
487SCIx_FNS(SCSMR, 0x00, 8, 0x00, 8, 0x00, 8, 0x00, 16, 0x00, 8)
488SCIx_FNS(SCBRR, 0x02, 8, 0x04, 8, 0x02, 8, 0x04, 8, 0x01, 8)
489SCIx_FNS(SCSCR, 0x04, 8, 0x08, 8, 0x04, 8, 0x08, 16, 0x02, 8)
490SCIx_FNS(SCxTDR, 0x06, 8, 0x0c, 8, 0x06, 8, 0x0C, 8, 0x03, 8)
491SCIx_FNS(SCxSR, 0x08, 8, 0x10, 8, 0x08, 16, 0x10, 16, 0x04, 8)
492SCIx_FNS(SCxRDR, 0x0a, 8, 0x14, 8, 0x0A, 8, 0x14, 8, 0x05, 8)
493SCIF_FNS(SCFCR, 0x0c, 8, 0x18, 16)
494#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
495 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
496 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
497 defined(CONFIG_CPU_SUBTYPE_SH7786)
498SCIF_FNS(SCFDR, 0x0e, 16, 0x1C, 16)
499SCIF_FNS(SCTFDR, 0x0e, 16, 0x1C, 16)
500SCIF_FNS(SCRFDR, 0x0e, 16, 0x20, 16)
501SCIF_FNS(SCSPTR, 0, 0, 0x24, 16)
502SCIF_FNS(SCLSR, 0, 0, 0x28, 16)
503#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
504SCIF_FNS(SCFDR, 0, 0, 0x1C, 16)
505SCIF_FNS(SCSPTR2, 0, 0, 0x20, 16)
506SCIF_FNS(SCLSR2, 0, 0, 0x24, 16)
507SCIF_FNS(SCTFDR, 0x0e, 16, 0x1C, 16)
508SCIF_FNS(SCRFDR, 0x0e, 16, 0x20, 16)
509SCIF_FNS(SCSPTR, 0, 0, 0x24, 16)
510SCIF_FNS(SCLSR, 0, 0, 0x28, 16)
511#else
512SCIF_FNS(SCFDR, 0x0e, 16, 0x1C, 16)
513#if defined(CONFIG_CPU_SUBTYPE_SH7722)
514SCIF_FNS(SCSPTR, 0, 0, 0, 0)
515#else
516SCIF_FNS(SCSPTR, 0, 0, 0x20, 16)
517#endif
518SCIF_FNS(SCLSR, 0, 0, 0x24, 16)
519#endif
520#endif
521#define sci_in(port, reg) sci_##reg##_in(port)
522#define sci_out(port, reg, value) sci_##reg##_out(port, value)
523
524/* H8/300 series SCI pins assignment */
525#if defined(__H8300H__) || defined(__H8300S__)
526static const struct __attribute__((packed)) {
527 int port; /* GPIO port no */
528 unsigned short rx,tx; /* GPIO bit no */
529} h8300_sci_pins[] = {
530#if defined(CONFIG_H83007) || defined(CONFIG_H83068)
531 { /* SCI0 */
532 .port = H8300_GPIO_P9,
533 .rx = H8300_GPIO_B2,
534 .tx = H8300_GPIO_B0,
535 },
536 { /* SCI1 */
537 .port = H8300_GPIO_P9,
538 .rx = H8300_GPIO_B3,
539 .tx = H8300_GPIO_B1,
540 },
541 { /* SCI2 */
542 .port = H8300_GPIO_PB,
543 .rx = H8300_GPIO_B7,
544 .tx = H8300_GPIO_B6,
545 }
546#elif defined(CONFIG_H8S2678)
547 { /* SCI0 */
548 .port = H8300_GPIO_P3,
549 .rx = H8300_GPIO_B2,
550 .tx = H8300_GPIO_B0,
551 },
552 { /* SCI1 */
553 .port = H8300_GPIO_P3,
554 .rx = H8300_GPIO_B3,
555 .tx = H8300_GPIO_B1,
556 },
557 { /* SCI2 */
558 .port = H8300_GPIO_P5,
559 .rx = H8300_GPIO_B1,
560 .tx = H8300_GPIO_B0,
561 }
562#endif
563};
564#endif
565
566#if defined(CONFIG_CPU_SUBTYPE_SH7706) || \
567 defined(CONFIG_CPU_SUBTYPE_SH7707) || \
568 defined(CONFIG_CPU_SUBTYPE_SH7708) || \
569 defined(CONFIG_CPU_SUBTYPE_SH7709)
570static inline int sci_rxd_in(struct uart_port *port)
571{
572 if (port->mapbase == 0xfffffe80)
573 return __raw_readb(SCPDR)&0x01 ? 1 : 0; /* SCI */
574 return 1;
575}
576#elif defined(CONFIG_CPU_SUBTYPE_SH7750) || \
577 defined(CONFIG_CPU_SUBTYPE_SH7751) || \
578 defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
579 defined(CONFIG_CPU_SUBTYPE_SH7750R) || \
580 defined(CONFIG_CPU_SUBTYPE_SH7750S) || \
581 defined(CONFIG_CPU_SUBTYPE_SH7091)
582static inline int sci_rxd_in(struct uart_port *port)
583{
584 if (port->mapbase == 0xffe00000)
585 return __raw_readb(SCSPTR1)&0x01 ? 1 : 0; /* SCI */
586 return 1;
587}
588#elif defined(__H8300H__) || defined(__H8300S__)
589static inline int sci_rxd_in(struct uart_port *port)
590{
591 int ch = (port->mapbase - SMR0) >> 3;
592 return (H8300_SCI_DR(ch) & h8300_sci_pins[ch].rx) ? 1 : 0;
593}
594#else /* default case for non-SCI processors */
595static inline int sci_rxd_in(struct uart_port *port)
596{
597 return 1;
598}
599#endif
600
601/*
602 * Values for the BitRate Register (SCBRR)
603 *
604 * The values are actually divisors for a frequency which can
605 * be internal to the SH3 (14.7456MHz) or derived from an external
606 * clock source. This driver assumes the internal clock is used;
607 * to support using an external clock source, config options or
608 * possibly command-line options would need to be added.
609 *
610 * Also, to support speeds below 2400 (why?) the lower 2 bits of
611 * the SCSMR register would also need to be set to non-zero values.
612 *
613 * -- Greg Banks 27Feb2000
614 *
615 * Answer: The SCBRR register is only eight bits, and the value in
616 * it gets larger with lower baud rates. At around 2400 (depending on
617 * the peripherial module clock) you run out of bits. However the
618 * lower two bits of SCSMR allow the module clock to be divided down,
619 * scaling the value which is needed in SCBRR.
620 *
621 * -- Stuart Menefy - 23 May 2000
622 *
623 * I meant, why would anyone bother with bitrates below 2400.
624 *
625 * -- Greg Banks - 7Jul2000
626 *
627 * You "speedist"! How will I use my 110bps ASR-33 teletype with paper
628 * tape reader as a console!
629 *
630 * -- Mitch Davis - 15 Jul 2000
631 */
632
633#if (defined(CONFIG_CPU_SUBTYPE_SH7780) || \
634 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
635 defined(CONFIG_CPU_SUBTYPE_SH7786)) && \
636 !defined(CONFIG_SH_SH2007)
637#define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(16*bps)-1)
638#elif defined(CONFIG_CPU_SUBTYPE_SH7705) || \
639 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
640 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
641 defined(CONFIG_ARCH_SH73A0) || \
642 defined(CONFIG_ARCH_SH7367) || \
643 defined(CONFIG_ARCH_SH7377) || \
644 defined(CONFIG_ARCH_SH7372)
645#define SCBRR_VALUE(bps, clk) (((clk*2)+16*bps)/(32*bps)-1)
646#elif defined(CONFIG_CPU_SUBTYPE_SH7723) ||\
647 defined(CONFIG_CPU_SUBTYPE_SH7724)
648static inline int scbrr_calc(struct uart_port *port, int bps, int clk)
649{
650 if (port->type == PORT_SCIF)
651 return (clk+16*bps)/(32*bps)-1;
652 else
653 return ((clk*2)+16*bps)/(16*bps)-1;
654}
655#define SCBRR_VALUE(bps, clk) scbrr_calc(port, bps, clk)
656#elif defined(__H8300H__) || defined(__H8300S__)
657#define SCBRR_VALUE(bps, clk) (((clk*1000/32)/bps)-1)
658#else /* Generic SH */
659#define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(32*bps)-1)
660#endif
diff --git a/drivers/tty/serial/sn_console.c b/drivers/tty/serial/sn_console.c
new file mode 100644
index 000000000000..cff9a306660f
--- /dev/null
+++ b/drivers/tty/serial/sn_console.c
@@ -0,0 +1,1085 @@
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-2006 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/interrupt.h>
41#include <linux/tty.h>
42#include <linux/serial.h>
43#include <linux/console.h>
44#include <linux/module.h>
45#include <linux/sysrq.h>
46#include <linux/circ_buf.h>
47#include <linux/serial_reg.h>
48#include <linux/delay.h> /* for mdelay */
49#include <linux/miscdevice.h>
50#include <linux/serial_core.h>
51
52#include <asm/io.h>
53#include <asm/sn/simulator.h>
54#include <asm/sn/sn_sal.h>
55
56/* number of characters we can transmit to the SAL console at a time */
57#define SN_SAL_MAX_CHARS 120
58
59/* 64K, when we're asynch, it must be at least printk's LOG_BUF_LEN to
60 * avoid losing chars, (always has to be a power of 2) */
61#define SN_SAL_BUFFER_SIZE (64 * (1 << 10))
62
63#define SN_SAL_UART_FIFO_DEPTH 16
64#define SN_SAL_UART_FIFO_SPEED_CPS (9600/10)
65
66/* sn_transmit_chars() calling args */
67#define TRANSMIT_BUFFERED 0
68#define TRANSMIT_RAW 1
69
70/* To use dynamic numbers only and not use the assigned major and minor,
71 * define the following.. */
72 /* #define USE_DYNAMIC_MINOR 1 *//* use dynamic minor number */
73#define USE_DYNAMIC_MINOR 0 /* Don't rely on misc_register dynamic minor */
74
75/* Device name we're using */
76#define DEVICE_NAME "ttySG"
77#define DEVICE_NAME_DYNAMIC "ttySG0" /* need full name for misc_register */
78/* The major/minor we are using, ignored for USE_DYNAMIC_MINOR */
79#define DEVICE_MAJOR 204
80#define DEVICE_MINOR 40
81
82#ifdef CONFIG_MAGIC_SYSRQ
83static char sysrq_serial_str[] = "\eSYS";
84static char *sysrq_serial_ptr = sysrq_serial_str;
85static unsigned long sysrq_requested;
86#endif /* CONFIG_MAGIC_SYSRQ */
87
88/*
89 * Port definition - this kinda drives it all
90 */
91struct sn_cons_port {
92 struct timer_list sc_timer;
93 struct uart_port sc_port;
94 struct sn_sal_ops {
95 int (*sal_puts_raw) (const char *s, int len);
96 int (*sal_puts) (const char *s, int len);
97 int (*sal_getc) (void);
98 int (*sal_input_pending) (void);
99 void (*sal_wakeup_transmit) (struct sn_cons_port *, int);
100 } *sc_ops;
101 unsigned long sc_interrupt_timeout;
102 int sc_is_asynch;
103};
104
105static struct sn_cons_port sal_console_port;
106static int sn_process_input;
107
108/* Only used if USE_DYNAMIC_MINOR is set to 1 */
109static struct miscdevice misc; /* used with misc_register for dynamic */
110
111extern void early_sn_setup(void);
112
113#undef DEBUG
114#ifdef DEBUG
115static int sn_debug_printf(const char *fmt, ...);
116#define DPRINTF(x...) sn_debug_printf(x)
117#else
118#define DPRINTF(x...) do { } while (0)
119#endif
120
121/* Prototypes */
122static int snt_hw_puts_raw(const char *, int);
123static int snt_hw_puts_buffered(const char *, int);
124static int snt_poll_getc(void);
125static int snt_poll_input_pending(void);
126static int snt_intr_getc(void);
127static int snt_intr_input_pending(void);
128static void sn_transmit_chars(struct sn_cons_port *, int);
129
130/* A table for polling:
131 */
132static struct sn_sal_ops poll_ops = {
133 .sal_puts_raw = snt_hw_puts_raw,
134 .sal_puts = snt_hw_puts_raw,
135 .sal_getc = snt_poll_getc,
136 .sal_input_pending = snt_poll_input_pending
137};
138
139/* A table for interrupts enabled */
140static struct sn_sal_ops intr_ops = {
141 .sal_puts_raw = snt_hw_puts_raw,
142 .sal_puts = snt_hw_puts_buffered,
143 .sal_getc = snt_intr_getc,
144 .sal_input_pending = snt_intr_input_pending,
145 .sal_wakeup_transmit = sn_transmit_chars
146};
147
148/* the console does output in two distinctly different ways:
149 * synchronous (raw) and asynchronous (buffered). initally, early_printk
150 * does synchronous output. any data written goes directly to the SAL
151 * to be output (incidentally, it is internally buffered by the SAL)
152 * after interrupts and timers are initialized and available for use,
153 * the console init code switches to asynchronous output. this is
154 * also the earliest opportunity to begin polling for console input.
155 * after console initialization, console output and tty (serial port)
156 * output is buffered and sent to the SAL asynchronously (either by
157 * timer callback or by UART interrupt) */
158
159/* routines for running the console in polling mode */
160
161/**
162 * snt_poll_getc - Get a character from the console in polling mode
163 *
164 */
165static int snt_poll_getc(void)
166{
167 int ch;
168
169 ia64_sn_console_getc(&ch);
170 return ch;
171}
172
173/**
174 * snt_poll_input_pending - Check if any input is waiting - polling mode.
175 *
176 */
177static int snt_poll_input_pending(void)
178{
179 int status, input;
180
181 status = ia64_sn_console_check(&input);
182 return !status && input;
183}
184
185/* routines for an interrupt driven console (normal) */
186
187/**
188 * snt_intr_getc - Get a character from the console, interrupt mode
189 *
190 */
191static int snt_intr_getc(void)
192{
193 return ia64_sn_console_readc();
194}
195
196/**
197 * snt_intr_input_pending - Check if input is pending, interrupt mode
198 *
199 */
200static int snt_intr_input_pending(void)
201{
202 return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
203}
204
205/* these functions are polled and interrupt */
206
207/**
208 * snt_hw_puts_raw - Send raw string to the console, polled or interrupt mode
209 * @s: String
210 * @len: Length
211 *
212 */
213static int snt_hw_puts_raw(const char *s, int len)
214{
215 /* this will call the PROM and not return until this is done */
216 return ia64_sn_console_putb(s, len);
217}
218
219/**
220 * snt_hw_puts_buffered - Send string to console, polled or interrupt mode
221 * @s: String
222 * @len: Length
223 *
224 */
225static int snt_hw_puts_buffered(const char *s, int len)
226{
227 /* queue data to the PROM */
228 return ia64_sn_console_xmit_chars((char *)s, len);
229}
230
231/* uart interface structs
232 * These functions are associated with the uart_port that the serial core
233 * infrastructure calls.
234 *
235 * Note: Due to how the console works, many routines are no-ops.
236 */
237
238/**
239 * snp_type - What type of console are we?
240 * @port: Port to operate with (we ignore since we only have one port)
241 *
242 */
243static const char *snp_type(struct uart_port *port)
244{
245 return ("SGI SN L1");
246}
247
248/**
249 * snp_tx_empty - Is the transmitter empty? We pretend we're always empty
250 * @port: Port to operate on (we ignore since we only have one port)
251 *
252 */
253static unsigned int snp_tx_empty(struct uart_port *port)
254{
255 return 1;
256}
257
258/**
259 * snp_stop_tx - stop the transmitter - no-op for us
260 * @port: Port to operat eon - we ignore - no-op function
261 *
262 */
263static void snp_stop_tx(struct uart_port *port)
264{
265}
266
267/**
268 * snp_release_port - Free i/o and resources for port - no-op for us
269 * @port: Port to operate on - we ignore - no-op function
270 *
271 */
272static void snp_release_port(struct uart_port *port)
273{
274}
275
276/**
277 * snp_enable_ms - Force modem status interrupts on - no-op for us
278 * @port: Port to operate on - we ignore - no-op function
279 *
280 */
281static void snp_enable_ms(struct uart_port *port)
282{
283}
284
285/**
286 * snp_shutdown - shut down the port - free irq and disable - no-op for us
287 * @port: Port to shut down - we ignore
288 *
289 */
290static void snp_shutdown(struct uart_port *port)
291{
292}
293
294/**
295 * snp_set_mctrl - set control lines (dtr, rts, etc) - no-op for our console
296 * @port: Port to operate on - we ignore
297 * @mctrl: Lines to set/unset - we ignore
298 *
299 */
300static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
301{
302}
303
304/**
305 * snp_get_mctrl - get contorl line info, we just return a static value
306 * @port: port to operate on - we only have one port so we ignore this
307 *
308 */
309static unsigned int snp_get_mctrl(struct uart_port *port)
310{
311 return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
312}
313
314/**
315 * snp_stop_rx - Stop the receiver - we ignor ethis
316 * @port: Port to operate on - we ignore
317 *
318 */
319static void snp_stop_rx(struct uart_port *port)
320{
321}
322
323/**
324 * snp_start_tx - Start transmitter
325 * @port: Port to operate on
326 *
327 */
328static void snp_start_tx(struct uart_port *port)
329{
330 if (sal_console_port.sc_ops->sal_wakeup_transmit)
331 sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
332 TRANSMIT_BUFFERED);
333
334}
335
336/**
337 * snp_break_ctl - handle breaks - ignored by us
338 * @port: Port to operate on
339 * @break_state: Break state
340 *
341 */
342static void snp_break_ctl(struct uart_port *port, int break_state)
343{
344}
345
346/**
347 * snp_startup - Start up the serial port - always return 0 (We're always on)
348 * @port: Port to operate on
349 *
350 */
351static int snp_startup(struct uart_port *port)
352{
353 return 0;
354}
355
356/**
357 * snp_set_termios - set termios stuff - we ignore these
358 * @port: port to operate on
359 * @termios: New settings
360 * @termios: Old
361 *
362 */
363static void
364snp_set_termios(struct uart_port *port, struct ktermios *termios,
365 struct ktermios *old)
366{
367}
368
369/**
370 * snp_request_port - allocate resources for port - ignored by us
371 * @port: port to operate on
372 *
373 */
374static int snp_request_port(struct uart_port *port)
375{
376 return 0;
377}
378
379/**
380 * snp_config_port - allocate resources, set up - we ignore, we're always on
381 * @port: Port to operate on
382 * @flags: flags used for port setup
383 *
384 */
385static void snp_config_port(struct uart_port *port, int flags)
386{
387}
388
389/* Associate the uart functions above - given to serial core */
390
391static struct uart_ops sn_console_ops = {
392 .tx_empty = snp_tx_empty,
393 .set_mctrl = snp_set_mctrl,
394 .get_mctrl = snp_get_mctrl,
395 .stop_tx = snp_stop_tx,
396 .start_tx = snp_start_tx,
397 .stop_rx = snp_stop_rx,
398 .enable_ms = snp_enable_ms,
399 .break_ctl = snp_break_ctl,
400 .startup = snp_startup,
401 .shutdown = snp_shutdown,
402 .set_termios = snp_set_termios,
403 .pm = NULL,
404 .type = snp_type,
405 .release_port = snp_release_port,
406 .request_port = snp_request_port,
407 .config_port = snp_config_port,
408 .verify_port = NULL,
409};
410
411/* End of uart struct functions and defines */
412
413#ifdef DEBUG
414
415/**
416 * sn_debug_printf - close to hardware debugging printf
417 * @fmt: printf format
418 *
419 * This is as "close to the metal" as we can get, used when the driver
420 * itself may be broken.
421 *
422 */
423static int sn_debug_printf(const char *fmt, ...)
424{
425 static char printk_buf[1024];
426 int printed_len;
427 va_list args;
428
429 va_start(args, fmt);
430 printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
431
432 if (!sal_console_port.sc_ops) {
433 sal_console_port.sc_ops = &poll_ops;
434 early_sn_setup();
435 }
436 sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
437
438 va_end(args);
439 return printed_len;
440}
441#endif /* DEBUG */
442
443/*
444 * Interrupt handling routines.
445 */
446
447/**
448 * sn_receive_chars - Grab characters, pass them to tty layer
449 * @port: Port to operate on
450 * @flags: irq flags
451 *
452 * Note: If we're not registered with the serial core infrastructure yet,
453 * we don't try to send characters to it...
454 *
455 */
456static void
457sn_receive_chars(struct sn_cons_port *port, unsigned long flags)
458{
459 int ch;
460 struct tty_struct *tty;
461
462 if (!port) {
463 printk(KERN_ERR "sn_receive_chars - port NULL so can't receieve\n");
464 return;
465 }
466
467 if (!port->sc_ops) {
468 printk(KERN_ERR "sn_receive_chars - port->sc_ops NULL so can't receieve\n");
469 return;
470 }
471
472 if (port->sc_port.state) {
473 /* The serial_core stuffs are initialized, use them */
474 tty = port->sc_port.state->port.tty;
475 }
476 else {
477 /* Not registered yet - can't pass to tty layer. */
478 tty = NULL;
479 }
480
481 while (port->sc_ops->sal_input_pending()) {
482 ch = port->sc_ops->sal_getc();
483 if (ch < 0) {
484 printk(KERN_ERR "sn_console: An error occured while "
485 "obtaining data from the console (0x%0x)\n", ch);
486 break;
487 }
488#ifdef CONFIG_MAGIC_SYSRQ
489 if (sysrq_requested) {
490 unsigned long sysrq_timeout = sysrq_requested + HZ*5;
491
492 sysrq_requested = 0;
493 if (ch && time_before(jiffies, sysrq_timeout)) {
494 spin_unlock_irqrestore(&port->sc_port.lock, flags);
495 handle_sysrq(ch);
496 spin_lock_irqsave(&port->sc_port.lock, flags);
497 /* ignore actual sysrq command char */
498 continue;
499 }
500 }
501 if (ch == *sysrq_serial_ptr) {
502 if (!(*++sysrq_serial_ptr)) {
503 sysrq_requested = jiffies;
504 sysrq_serial_ptr = sysrq_serial_str;
505 }
506 /*
507 * ignore the whole sysrq string except for the
508 * leading escape
509 */
510 if (ch != '\e')
511 continue;
512 }
513 else
514 sysrq_serial_ptr = sysrq_serial_str;
515#endif /* CONFIG_MAGIC_SYSRQ */
516
517 /* record the character to pass up to the tty layer */
518 if (tty) {
519 if(tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
520 break;
521 }
522 port->sc_port.icount.rx++;
523 }
524
525 if (tty)
526 tty_flip_buffer_push(tty);
527}
528
529/**
530 * sn_transmit_chars - grab characters from serial core, send off
531 * @port: Port to operate on
532 * @raw: Transmit raw or buffered
533 *
534 * Note: If we're early, before we're registered with serial core, the
535 * writes are going through sn_sal_console_write because that's how
536 * register_console has been set up. We currently could have asynch
537 * polls calling this function due to sn_sal_switch_to_asynch but we can
538 * ignore them until we register with the serial core stuffs.
539 *
540 */
541static void sn_transmit_chars(struct sn_cons_port *port, int raw)
542{
543 int xmit_count, tail, head, loops, ii;
544 int result;
545 char *start;
546 struct circ_buf *xmit;
547
548 if (!port)
549 return;
550
551 BUG_ON(!port->sc_is_asynch);
552
553 if (port->sc_port.state) {
554 /* We're initialized, using serial core infrastructure */
555 xmit = &port->sc_port.state->xmit;
556 } else {
557 /* Probably sn_sal_switch_to_asynch has been run but serial core isn't
558 * initialized yet. Just return. Writes are going through
559 * sn_sal_console_write (due to register_console) at this time.
560 */
561 return;
562 }
563
564 if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
565 /* Nothing to do. */
566 ia64_sn_console_intr_disable(SAL_CONSOLE_INTR_XMIT);
567 return;
568 }
569
570 head = xmit->head;
571 tail = xmit->tail;
572 start = &xmit->buf[tail];
573
574 /* twice around gets the tail to the end of the buffer and
575 * then to the head, if needed */
576 loops = (head < tail) ? 2 : 1;
577
578 for (ii = 0; ii < loops; ii++) {
579 xmit_count = (head < tail) ?
580 (UART_XMIT_SIZE - tail) : (head - tail);
581
582 if (xmit_count > 0) {
583 if (raw == TRANSMIT_RAW)
584 result =
585 port->sc_ops->sal_puts_raw(start,
586 xmit_count);
587 else
588 result =
589 port->sc_ops->sal_puts(start, xmit_count);
590#ifdef DEBUG
591 if (!result)
592 DPRINTF("`");
593#endif
594 if (result > 0) {
595 xmit_count -= result;
596 port->sc_port.icount.tx += result;
597 tail += result;
598 tail &= UART_XMIT_SIZE - 1;
599 xmit->tail = tail;
600 start = &xmit->buf[tail];
601 }
602 }
603 }
604
605 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
606 uart_write_wakeup(&port->sc_port);
607
608 if (uart_circ_empty(xmit))
609 snp_stop_tx(&port->sc_port); /* no-op for us */
610}
611
612/**
613 * sn_sal_interrupt - Handle console interrupts
614 * @irq: irq #, useful for debug statements
615 * @dev_id: our pointer to our port (sn_cons_port which contains the uart port)
616 *
617 */
618static irqreturn_t sn_sal_interrupt(int irq, void *dev_id)
619{
620 struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
621 unsigned long flags;
622 int status = ia64_sn_console_intr_status();
623
624 if (!port)
625 return IRQ_NONE;
626
627 spin_lock_irqsave(&port->sc_port.lock, flags);
628 if (status & SAL_CONSOLE_INTR_RECV) {
629 sn_receive_chars(port, flags);
630 }
631 if (status & SAL_CONSOLE_INTR_XMIT) {
632 sn_transmit_chars(port, TRANSMIT_BUFFERED);
633 }
634 spin_unlock_irqrestore(&port->sc_port.lock, flags);
635 return IRQ_HANDLED;
636}
637
638/**
639 * sn_sal_timer_poll - this function handles polled console mode
640 * @data: A pointer to our sn_cons_port (which contains the uart port)
641 *
642 * data is the pointer that init_timer will store for us. This function is
643 * associated with init_timer to see if there is any console traffic.
644 * Obviously not used in interrupt mode
645 *
646 */
647static void sn_sal_timer_poll(unsigned long data)
648{
649 struct sn_cons_port *port = (struct sn_cons_port *)data;
650 unsigned long flags;
651
652 if (!port)
653 return;
654
655 if (!port->sc_port.irq) {
656 spin_lock_irqsave(&port->sc_port.lock, flags);
657 if (sn_process_input)
658 sn_receive_chars(port, flags);
659 sn_transmit_chars(port, TRANSMIT_RAW);
660 spin_unlock_irqrestore(&port->sc_port.lock, flags);
661 mod_timer(&port->sc_timer,
662 jiffies + port->sc_interrupt_timeout);
663 }
664}
665
666/*
667 * Boot-time initialization code
668 */
669
670/**
671 * sn_sal_switch_to_asynch - Switch to async mode (as opposed to synch)
672 * @port: Our sn_cons_port (which contains the uart port)
673 *
674 * So this is used by sn_sal_serial_console_init (early on, before we're
675 * registered with serial core). It's also used by sn_sal_module_init
676 * right after we've registered with serial core. The later only happens
677 * if we didn't already come through here via sn_sal_serial_console_init.
678 *
679 */
680static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
681{
682 unsigned long flags;
683
684 if (!port)
685 return;
686
687 DPRINTF("sn_console: about to switch to asynchronous console\n");
688
689 /* without early_printk, we may be invoked late enough to race
690 * with other cpus doing console IO at this point, however
691 * console interrupts will never be enabled */
692 spin_lock_irqsave(&port->sc_port.lock, flags);
693
694 /* early_printk invocation may have done this for us */
695 if (!port->sc_ops)
696 port->sc_ops = &poll_ops;
697
698 /* we can't turn on the console interrupt (as request_irq
699 * calls kmalloc, which isn't set up yet), so we rely on a
700 * timer to poll for input and push data from the console
701 * buffer.
702 */
703 init_timer(&port->sc_timer);
704 port->sc_timer.function = sn_sal_timer_poll;
705 port->sc_timer.data = (unsigned long)port;
706
707 if (IS_RUNNING_ON_SIMULATOR())
708 port->sc_interrupt_timeout = 6;
709 else {
710 /* 960cps / 16 char FIFO = 60HZ
711 * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
712 port->sc_interrupt_timeout =
713 HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
714 }
715 mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
716
717 port->sc_is_asynch = 1;
718 spin_unlock_irqrestore(&port->sc_port.lock, flags);
719}
720
721/**
722 * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
723 * @port: Our sn_cons_port (which contains the uart port)
724 *
725 * In sn_sal_module_init, after we're registered with serial core and
726 * the port is added, this function is called to switch us to interrupt
727 * mode. We were previously in asynch/polling mode (using init_timer).
728 *
729 * We attempt to switch to interrupt mode here by calling
730 * request_irq. If that works out, we enable receive interrupts.
731 */
732static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
733{
734 unsigned long flags;
735
736 if (port) {
737 DPRINTF("sn_console: switching to interrupt driven console\n");
738
739 if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
740 IRQF_DISABLED | IRQF_SHARED,
741 "SAL console driver", port) >= 0) {
742 spin_lock_irqsave(&port->sc_port.lock, flags);
743 port->sc_port.irq = SGI_UART_VECTOR;
744 port->sc_ops = &intr_ops;
745
746 /* turn on receive interrupts */
747 ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
748 spin_unlock_irqrestore(&port->sc_port.lock, flags);
749 }
750 else {
751 printk(KERN_INFO
752 "sn_console: console proceeding in polled mode\n");
753 }
754 }
755}
756
757/*
758 * Kernel console definitions
759 */
760
761static void sn_sal_console_write(struct console *, const char *, unsigned);
762static int sn_sal_console_setup(struct console *, char *);
763static struct uart_driver sal_console_uart;
764extern struct tty_driver *uart_console_device(struct console *, int *);
765
766static struct console sal_console = {
767 .name = DEVICE_NAME,
768 .write = sn_sal_console_write,
769 .device = uart_console_device,
770 .setup = sn_sal_console_setup,
771 .index = -1, /* unspecified */
772 .data = &sal_console_uart,
773};
774
775#define SAL_CONSOLE &sal_console
776
777static struct uart_driver sal_console_uart = {
778 .owner = THIS_MODULE,
779 .driver_name = "sn_console",
780 .dev_name = DEVICE_NAME,
781 .major = 0, /* major/minor set at registration time per USE_DYNAMIC_MINOR */
782 .minor = 0,
783 .nr = 1, /* one port */
784 .cons = SAL_CONSOLE,
785};
786
787/**
788 * sn_sal_module_init - When the kernel loads us, get us rolling w/ serial core
789 *
790 * Before this is called, we've been printing kernel messages in a special
791 * early mode not making use of the serial core infrastructure. When our
792 * driver is loaded for real, we register the driver and port with serial
793 * core and try to enable interrupt driven mode.
794 *
795 */
796static int __init sn_sal_module_init(void)
797{
798 int retval;
799
800 if (!ia64_platform_is("sn2"))
801 return 0;
802
803 printk(KERN_INFO "sn_console: Console driver init\n");
804
805 if (USE_DYNAMIC_MINOR == 1) {
806 misc.minor = MISC_DYNAMIC_MINOR;
807 misc.name = DEVICE_NAME_DYNAMIC;
808 retval = misc_register(&misc);
809 if (retval != 0) {
810 printk(KERN_WARNING "Failed to register console "
811 "device using misc_register.\n");
812 return -ENODEV;
813 }
814 sal_console_uart.major = MISC_MAJOR;
815 sal_console_uart.minor = misc.minor;
816 } else {
817 sal_console_uart.major = DEVICE_MAJOR;
818 sal_console_uart.minor = DEVICE_MINOR;
819 }
820
821 /* We register the driver and the port before switching to interrupts
822 * or async above so the proper uart structures are populated */
823
824 if (uart_register_driver(&sal_console_uart) < 0) {
825 printk
826 ("ERROR sn_sal_module_init failed uart_register_driver, line %d\n",
827 __LINE__);
828 return -ENODEV;
829 }
830
831 spin_lock_init(&sal_console_port.sc_port.lock);
832
833 /* Setup the port struct with the minimum needed */
834 sal_console_port.sc_port.membase = (char *)1; /* just needs to be non-zero */
835 sal_console_port.sc_port.type = PORT_16550A;
836 sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
837 sal_console_port.sc_port.ops = &sn_console_ops;
838 sal_console_port.sc_port.line = 0;
839
840 if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
841 /* error - not sure what I'd do - so I'll do nothing */
842 printk(KERN_ERR "%s: unable to add port\n", __func__);
843 }
844
845 /* when this driver is compiled in, the console initialization
846 * will have already switched us into asynchronous operation
847 * before we get here through the module initcalls */
848 if (!sal_console_port.sc_is_asynch) {
849 sn_sal_switch_to_asynch(&sal_console_port);
850 }
851
852 /* at this point (module_init) we can try to turn on interrupts */
853 if (!IS_RUNNING_ON_SIMULATOR()) {
854 sn_sal_switch_to_interrupts(&sal_console_port);
855 }
856 sn_process_input = 1;
857 return 0;
858}
859
860/**
861 * sn_sal_module_exit - When we're unloaded, remove the driver/port
862 *
863 */
864static void __exit sn_sal_module_exit(void)
865{
866 del_timer_sync(&sal_console_port.sc_timer);
867 uart_remove_one_port(&sal_console_uart, &sal_console_port.sc_port);
868 uart_unregister_driver(&sal_console_uart);
869 misc_deregister(&misc);
870}
871
872module_init(sn_sal_module_init);
873module_exit(sn_sal_module_exit);
874
875/**
876 * puts_raw_fixed - sn_sal_console_write helper for adding \r's as required
877 * @puts_raw : puts function to do the writing
878 * @s: input string
879 * @count: length
880 *
881 * We need a \r ahead of every \n for direct writes through
882 * ia64_sn_console_putb (what sal_puts_raw below actually does).
883 *
884 */
885
886static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
887 const char *s, int count)
888{
889 const char *s1;
890
891 /* Output '\r' before each '\n' */
892 while ((s1 = memchr(s, '\n', count)) != NULL) {
893 puts_raw(s, s1 - s);
894 puts_raw("\r\n", 2);
895 count -= s1 + 1 - s;
896 s = s1 + 1;
897 }
898 puts_raw(s, count);
899}
900
901/**
902 * sn_sal_console_write - Print statements before serial core available
903 * @console: Console to operate on - we ignore since we have just one
904 * @s: String to send
905 * @count: length
906 *
907 * This is referenced in the console struct. It is used for early
908 * console printing before we register with serial core and for things
909 * such as kdb. The console_lock must be held when we get here.
910 *
911 * This function has some code for trying to print output even if the lock
912 * is held. We try to cover the case where a lock holder could have died.
913 * We don't use this special case code if we're not registered with serial
914 * core yet. After we're registered with serial core, the only time this
915 * function would be used is for high level kernel output like magic sys req,
916 * kdb, and printk's.
917 */
918static void
919sn_sal_console_write(struct console *co, const char *s, unsigned count)
920{
921 unsigned long flags = 0;
922 struct sn_cons_port *port = &sal_console_port;
923 static int stole_lock = 0;
924
925 BUG_ON(!port->sc_is_asynch);
926
927 /* We can't look at the xmit buffer if we're not registered with serial core
928 * yet. So only do the fancy recovery after registering
929 */
930 if (!port->sc_port.state) {
931 /* Not yet registered with serial core - simple case */
932 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
933 return;
934 }
935
936 /* somebody really wants this output, might be an
937 * oops, kdb, panic, etc. make sure they get it. */
938 if (spin_is_locked(&port->sc_port.lock)) {
939 int lhead = port->sc_port.state->xmit.head;
940 int ltail = port->sc_port.state->xmit.tail;
941 int counter, got_lock = 0;
942
943 /*
944 * We attempt to determine if someone has died with the
945 * lock. We wait ~20 secs after the head and tail ptrs
946 * stop moving and assume the lock holder is not functional
947 * and plow ahead. If the lock is freed within the time out
948 * period we re-get the lock and go ahead normally. We also
949 * remember if we have plowed ahead so that we don't have
950 * to wait out the time out period again - the asumption
951 * is that we will time out again.
952 */
953
954 for (counter = 0; counter < 150; mdelay(125), counter++) {
955 if (!spin_is_locked(&port->sc_port.lock)
956 || stole_lock) {
957 if (!stole_lock) {
958 spin_lock_irqsave(&port->sc_port.lock,
959 flags);
960 got_lock = 1;
961 }
962 break;
963 } else {
964 /* still locked */
965 if ((lhead != port->sc_port.state->xmit.head)
966 || (ltail !=
967 port->sc_port.state->xmit.tail)) {
968 lhead =
969 port->sc_port.state->xmit.head;
970 ltail =
971 port->sc_port.state->xmit.tail;
972 counter = 0;
973 }
974 }
975 }
976 /* flush anything in the serial core xmit buffer, raw */
977 sn_transmit_chars(port, 1);
978 if (got_lock) {
979 spin_unlock_irqrestore(&port->sc_port.lock, flags);
980 stole_lock = 0;
981 } else {
982 /* fell thru */
983 stole_lock = 1;
984 }
985 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
986 } else {
987 stole_lock = 0;
988 spin_lock_irqsave(&port->sc_port.lock, flags);
989 sn_transmit_chars(port, 1);
990 spin_unlock_irqrestore(&port->sc_port.lock, flags);
991
992 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
993 }
994}
995
996
997/**
998 * sn_sal_console_setup - Set up console for early printing
999 * @co: Console to work with
1000 * @options: Options to set
1001 *
1002 * Altix console doesn't do anything with baud rates, etc, anyway.
1003 *
1004 * This isn't required since not providing the setup function in the
1005 * console struct is ok. However, other patches like KDB plop something
1006 * here so providing it is easier.
1007 *
1008 */
1009static int sn_sal_console_setup(struct console *co, char *options)
1010{
1011 return 0;
1012}
1013
1014/**
1015 * sn_sal_console_write_early - simple early output routine
1016 * @co - console struct
1017 * @s - string to print
1018 * @count - count
1019 *
1020 * Simple function to provide early output, before even
1021 * sn_sal_serial_console_init is called. Referenced in the
1022 * console struct registerd in sn_serial_console_early_setup.
1023 *
1024 */
1025static void __init
1026sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
1027{
1028 puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
1029}
1030
1031/* Used for very early console printing - again, before
1032 * sn_sal_serial_console_init is run */
1033static struct console sal_console_early __initdata = {
1034 .name = "sn_sal",
1035 .write = sn_sal_console_write_early,
1036 .flags = CON_PRINTBUFFER,
1037 .index = -1,
1038};
1039
1040/**
1041 * sn_serial_console_early_setup - Sets up early console output support
1042 *
1043 * Register a console early on... This is for output before even
1044 * sn_sal_serial_cosnole_init is called. This function is called from
1045 * setup.c. This allows us to do really early polled writes. When
1046 * sn_sal_serial_console_init is called, this console is unregistered
1047 * and a new one registered.
1048 */
1049int __init sn_serial_console_early_setup(void)
1050{
1051 if (!ia64_platform_is("sn2"))
1052 return -1;
1053
1054 sal_console_port.sc_ops = &poll_ops;
1055 spin_lock_init(&sal_console_port.sc_port.lock);
1056 early_sn_setup(); /* Find SAL entry points */
1057 register_console(&sal_console_early);
1058
1059 return 0;
1060}
1061
1062/**
1063 * sn_sal_serial_console_init - Early console output - set up for register
1064 *
1065 * This function is called when regular console init happens. Because we
1066 * support even earlier console output with sn_serial_console_early_setup
1067 * (called from setup.c directly), this function unregisters the really
1068 * early console.
1069 *
1070 * Note: Even if setup.c doesn't register sal_console_early, unregistering
1071 * it here doesn't hurt anything.
1072 *
1073 */
1074static int __init sn_sal_serial_console_init(void)
1075{
1076 if (ia64_platform_is("sn2")) {
1077 sn_sal_switch_to_asynch(&sal_console_port);
1078 DPRINTF("sn_sal_serial_console_init : register console\n");
1079 register_console(&sal_console);
1080 unregister_console(&sal_console_early);
1081 }
1082 return 0;
1083}
1084
1085console_initcall(sn_sal_serial_console_init);
diff --git a/drivers/tty/serial/suncore.c b/drivers/tty/serial/suncore.c
new file mode 100644
index 000000000000..6381a0282ee7
--- /dev/null
+++ b/drivers/tty/serial/suncore.c
@@ -0,0 +1,247 @@
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/module.h>
14#include <linux/kernel.h>
15#include <linux/console.h>
16#include <linux/tty.h>
17#include <linux/errno.h>
18#include <linux/string.h>
19#include <linux/serial_core.h>
20#include <linux/init.h>
21
22#include <asm/prom.h>
23
24#include "suncore.h"
25
26static int sunserial_current_minor = 64;
27
28int sunserial_register_minors(struct uart_driver *drv, int count)
29{
30 int err = 0;
31
32 drv->minor = sunserial_current_minor;
33 drv->nr += count;
34 /* Register the driver on the first call */
35 if (drv->nr == count)
36 err = uart_register_driver(drv);
37 if (err == 0) {
38 sunserial_current_minor += count;
39 drv->tty_driver->name_base = drv->minor - 64;
40 }
41 return err;
42}
43EXPORT_SYMBOL(sunserial_register_minors);
44
45void sunserial_unregister_minors(struct uart_driver *drv, int count)
46{
47 drv->nr -= count;
48 sunserial_current_minor -= count;
49
50 if (drv->nr == 0)
51 uart_unregister_driver(drv);
52}
53EXPORT_SYMBOL(sunserial_unregister_minors);
54
55int sunserial_console_match(struct console *con, struct device_node *dp,
56 struct uart_driver *drv, int line, bool ignore_line)
57{
58 if (!con)
59 return 0;
60
61 drv->cons = con;
62
63 if (of_console_device != dp)
64 return 0;
65
66 if (!ignore_line) {
67 int off = 0;
68
69 if (of_console_options &&
70 *of_console_options == 'b')
71 off = 1;
72
73 if ((line & 1) != off)
74 return 0;
75 }
76
77 if (!console_set_on_cmdline) {
78 con->index = line;
79 add_preferred_console(con->name, line, NULL);
80 }
81 return 1;
82}
83EXPORT_SYMBOL(sunserial_console_match);
84
85void sunserial_console_termios(struct console *con, struct device_node *uart_dp)
86{
87 const char *mode, *s;
88 char mode_prop[] = "ttyX-mode";
89 int baud, bits, stop, cflag;
90 char parity;
91
92 if (!strcmp(uart_dp->name, "rsc") ||
93 !strcmp(uart_dp->name, "rsc-console") ||
94 !strcmp(uart_dp->name, "rsc-control")) {
95 mode = of_get_property(uart_dp,
96 "ssp-console-modes", NULL);
97 if (!mode)
98 mode = "115200,8,n,1,-";
99 } else if (!strcmp(uart_dp->name, "lom-console")) {
100 mode = "9600,8,n,1,-";
101 } else {
102 struct device_node *dp;
103 char c;
104
105 c = 'a';
106 if (of_console_options)
107 c = *of_console_options;
108
109 mode_prop[3] = c;
110
111 dp = of_find_node_by_path("/options");
112 mode = of_get_property(dp, mode_prop, NULL);
113 if (!mode)
114 mode = "9600,8,n,1,-";
115 }
116
117 cflag = CREAD | HUPCL | CLOCAL;
118
119 s = mode;
120 baud = simple_strtoul(s, NULL, 0);
121 s = strchr(s, ',');
122 bits = simple_strtoul(++s, NULL, 0);
123 s = strchr(s, ',');
124 parity = *(++s);
125 s = strchr(s, ',');
126 stop = simple_strtoul(++s, NULL, 0);
127 s = strchr(s, ',');
128 /* XXX handshake is not handled here. */
129
130 switch (baud) {
131 case 150: cflag |= B150; break;
132 case 300: cflag |= B300; break;
133 case 600: cflag |= B600; break;
134 case 1200: cflag |= B1200; break;
135 case 2400: cflag |= B2400; break;
136 case 4800: cflag |= B4800; break;
137 case 9600: cflag |= B9600; break;
138 case 19200: cflag |= B19200; break;
139 case 38400: cflag |= B38400; break;
140 case 57600: cflag |= B57600; break;
141 case 115200: cflag |= B115200; break;
142 case 230400: cflag |= B230400; break;
143 case 460800: cflag |= B460800; break;
144 default: baud = 9600; cflag |= B9600; break;
145 }
146
147 switch (bits) {
148 case 5: cflag |= CS5; break;
149 case 6: cflag |= CS6; break;
150 case 7: cflag |= CS7; break;
151 case 8: cflag |= CS8; break;
152 default: cflag |= CS8; break;
153 }
154
155 switch (parity) {
156 case 'o': cflag |= (PARENB | PARODD); break;
157 case 'e': cflag |= PARENB; break;
158 case 'n': default: break;
159 }
160
161 switch (stop) {
162 case 2: cflag |= CSTOPB; break;
163 case 1: default: break;
164 }
165
166 con->cflag = cflag;
167}
168
169/* Sun serial MOUSE auto baud rate detection. */
170static struct mouse_baud_cflag {
171 int baud;
172 unsigned int cflag;
173} mouse_baud_table[] = {
174 { 1200, B1200 },
175 { 2400, B2400 },
176 { 4800, B4800 },
177 { 9600, B9600 },
178 { -1, ~0 },
179 { -1, ~0 },
180};
181
182unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
183{
184 int i;
185
186 for (i = 0; mouse_baud_table[i].baud != -1; i++)
187 if (mouse_baud_table[i].cflag == (cflag & CBAUD))
188 break;
189
190 i += 1;
191 if (mouse_baud_table[i].baud == -1)
192 i = 0;
193
194 *new_baud = mouse_baud_table[i].baud;
195 return mouse_baud_table[i].cflag;
196}
197
198EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
199
200/* Basically, when the baud rate is wrong the mouse spits out
201 * breaks to us.
202 */
203int suncore_mouse_baud_detection(unsigned char ch, int is_break)
204{
205 static int mouse_got_break = 0;
206 static int ctr = 0;
207
208 if (is_break) {
209 /* Let a few normal bytes go by before we jump the gun
210 * and say we need to try another baud rate.
211 */
212 if (mouse_got_break && ctr < 8)
213 return 1;
214
215 /* Ok, we need to try another baud. */
216 ctr = 0;
217 mouse_got_break = 1;
218 return 2;
219 }
220 if (mouse_got_break) {
221 ctr++;
222 if (ch == 0x87) {
223 /* Correct baud rate determined. */
224 mouse_got_break = 0;
225 }
226 return 1;
227 }
228 return 0;
229}
230
231EXPORT_SYMBOL(suncore_mouse_baud_detection);
232
233static int __init suncore_init(void)
234{
235 return 0;
236}
237
238static void __exit suncore_exit(void)
239{
240}
241
242module_init(suncore_init);
243module_exit(suncore_exit);
244
245MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
246MODULE_DESCRIPTION("Sun serial common layer");
247MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/suncore.h b/drivers/tty/serial/suncore.h
new file mode 100644
index 000000000000..db2057936c31
--- /dev/null
+++ b/drivers/tty/serial/suncore.h
@@ -0,0 +1,33 @@
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_register_minors(struct uart_driver *, int);
26extern void sunserial_unregister_minors(struct uart_driver *, int);
27
28extern int sunserial_console_match(struct console *, struct device_node *,
29 struct uart_driver *, int, bool);
30extern void sunserial_console_termios(struct console *,
31 struct device_node *);
32
33#endif /* !(_SERIAL_SUN_H) */
diff --git a/drivers/tty/serial/sunhv.c b/drivers/tty/serial/sunhv.c
new file mode 100644
index 000000000000..c9014868297d
--- /dev/null
+++ b/drivers/tty/serial/sunhv.c
@@ -0,0 +1,661 @@
1/* sunhv.c: Serial driver for SUN4V hypervisor console.
2 *
3 * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
4 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/errno.h>
9#include <linux/tty.h>
10#include <linux/tty_flip.h>
11#include <linux/major.h>
12#include <linux/circ_buf.h>
13#include <linux/serial.h>
14#include <linux/sysrq.h>
15#include <linux/console.h>
16#include <linux/spinlock.h>
17#include <linux/slab.h>
18#include <linux/delay.h>
19#include <linux/init.h>
20#include <linux/of_device.h>
21
22#include <asm/hypervisor.h>
23#include <asm/spitfire.h>
24#include <asm/prom.h>
25#include <asm/irq.h>
26
27#if defined(CONFIG_MAGIC_SYSRQ)
28#define SUPPORT_SYSRQ
29#endif
30
31#include <linux/serial_core.h>
32
33#include "suncore.h"
34
35#define CON_BREAK ((long)-1)
36#define CON_HUP ((long)-2)
37
38#define IGNORE_BREAK 0x1
39#define IGNORE_ALL 0x2
40
41static char *con_write_page;
42static char *con_read_page;
43
44static int hung_up = 0;
45
46static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit)
47{
48 while (!uart_circ_empty(xmit)) {
49 long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
50
51 if (status != HV_EOK)
52 break;
53
54 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
55 port->icount.tx++;
56 }
57}
58
59static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit)
60{
61 while (!uart_circ_empty(xmit)) {
62 unsigned long ra = __pa(xmit->buf + xmit->tail);
63 unsigned long len, status, sent;
64
65 len = CIRC_CNT_TO_END(xmit->head, xmit->tail,
66 UART_XMIT_SIZE);
67 status = sun4v_con_write(ra, len, &sent);
68 if (status != HV_EOK)
69 break;
70 xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1);
71 port->icount.tx += sent;
72 }
73}
74
75static int receive_chars_getchar(struct uart_port *port, struct tty_struct *tty)
76{
77 int saw_console_brk = 0;
78 int limit = 10000;
79
80 while (limit-- > 0) {
81 long status;
82 long c = sun4v_con_getchar(&status);
83
84 if (status == HV_EWOULDBLOCK)
85 break;
86
87 if (c == CON_BREAK) {
88 if (uart_handle_break(port))
89 continue;
90 saw_console_brk = 1;
91 c = 0;
92 }
93
94 if (c == CON_HUP) {
95 hung_up = 1;
96 uart_handle_dcd_change(port, 0);
97 } else if (hung_up) {
98 hung_up = 0;
99 uart_handle_dcd_change(port, 1);
100 }
101
102 if (tty == NULL) {
103 uart_handle_sysrq_char(port, c);
104 continue;
105 }
106
107 port->icount.rx++;
108
109 if (uart_handle_sysrq_char(port, c))
110 continue;
111
112 tty_insert_flip_char(tty, c, TTY_NORMAL);
113 }
114
115 return saw_console_brk;
116}
117
118static int receive_chars_read(struct uart_port *port, struct tty_struct *tty)
119{
120 int saw_console_brk = 0;
121 int limit = 10000;
122
123 while (limit-- > 0) {
124 unsigned long ra = __pa(con_read_page);
125 unsigned long bytes_read, i;
126 long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read);
127
128 if (stat != HV_EOK) {
129 bytes_read = 0;
130
131 if (stat == CON_BREAK) {
132 if (uart_handle_break(port))
133 continue;
134 saw_console_brk = 1;
135 *con_read_page = 0;
136 bytes_read = 1;
137 } else if (stat == CON_HUP) {
138 hung_up = 1;
139 uart_handle_dcd_change(port, 0);
140 continue;
141 } else {
142 /* HV_EWOULDBLOCK, etc. */
143 break;
144 }
145 }
146
147 if (hung_up) {
148 hung_up = 0;
149 uart_handle_dcd_change(port, 1);
150 }
151
152 for (i = 0; i < bytes_read; i++)
153 uart_handle_sysrq_char(port, con_read_page[i]);
154
155 if (tty == NULL)
156 continue;
157
158 port->icount.rx += bytes_read;
159
160 tty_insert_flip_string(tty, con_read_page, bytes_read);
161 }
162
163 return saw_console_brk;
164}
165
166struct sunhv_ops {
167 void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
168 int (*receive_chars)(struct uart_port *port, struct tty_struct *tty);
169};
170
171static struct sunhv_ops bychar_ops = {
172 .transmit_chars = transmit_chars_putchar,
173 .receive_chars = receive_chars_getchar,
174};
175
176static struct sunhv_ops bywrite_ops = {
177 .transmit_chars = transmit_chars_write,
178 .receive_chars = receive_chars_read,
179};
180
181static struct sunhv_ops *sunhv_ops = &bychar_ops;
182
183static struct tty_struct *receive_chars(struct uart_port *port)
184{
185 struct tty_struct *tty = NULL;
186
187 if (port->state != NULL) /* Unopened serial console */
188 tty = port->state->port.tty;
189
190 if (sunhv_ops->receive_chars(port, tty))
191 sun_do_break();
192
193 return tty;
194}
195
196static void transmit_chars(struct uart_port *port)
197{
198 struct circ_buf *xmit;
199
200 if (!port->state)
201 return;
202
203 xmit = &port->state->xmit;
204 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
205 return;
206
207 sunhv_ops->transmit_chars(port, xmit);
208
209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
210 uart_write_wakeup(port);
211}
212
213static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
214{
215 struct uart_port *port = dev_id;
216 struct tty_struct *tty;
217 unsigned long flags;
218
219 spin_lock_irqsave(&port->lock, flags);
220 tty = receive_chars(port);
221 transmit_chars(port);
222 spin_unlock_irqrestore(&port->lock, flags);
223
224 if (tty)
225 tty_flip_buffer_push(tty);
226
227 return IRQ_HANDLED;
228}
229
230/* port->lock is not held. */
231static unsigned int sunhv_tx_empty(struct uart_port *port)
232{
233 /* Transmitter is always empty for us. If the circ buffer
234 * is non-empty or there is an x_char pending, our caller
235 * will do the right thing and ignore what we return here.
236 */
237 return TIOCSER_TEMT;
238}
239
240/* port->lock held by caller. */
241static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
242{
243 return;
244}
245
246/* port->lock is held by caller and interrupts are disabled. */
247static unsigned int sunhv_get_mctrl(struct uart_port *port)
248{
249 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
250}
251
252/* port->lock held by caller. */
253static void sunhv_stop_tx(struct uart_port *port)
254{
255 return;
256}
257
258/* port->lock held by caller. */
259static void sunhv_start_tx(struct uart_port *port)
260{
261 transmit_chars(port);
262}
263
264/* port->lock is not held. */
265static void sunhv_send_xchar(struct uart_port *port, char ch)
266{
267 unsigned long flags;
268 int limit = 10000;
269
270 spin_lock_irqsave(&port->lock, flags);
271
272 while (limit-- > 0) {
273 long status = sun4v_con_putchar(ch);
274 if (status == HV_EOK)
275 break;
276 udelay(1);
277 }
278
279 spin_unlock_irqrestore(&port->lock, flags);
280}
281
282/* port->lock held by caller. */
283static void sunhv_stop_rx(struct uart_port *port)
284{
285}
286
287/* port->lock held by caller. */
288static void sunhv_enable_ms(struct uart_port *port)
289{
290}
291
292/* port->lock is not held. */
293static void sunhv_break_ctl(struct uart_port *port, int break_state)
294{
295 if (break_state) {
296 unsigned long flags;
297 int limit = 10000;
298
299 spin_lock_irqsave(&port->lock, flags);
300
301 while (limit-- > 0) {
302 long status = sun4v_con_putchar(CON_BREAK);
303 if (status == HV_EOK)
304 break;
305 udelay(1);
306 }
307
308 spin_unlock_irqrestore(&port->lock, flags);
309 }
310}
311
312/* port->lock is not held. */
313static int sunhv_startup(struct uart_port *port)
314{
315 return 0;
316}
317
318/* port->lock is not held. */
319static void sunhv_shutdown(struct uart_port *port)
320{
321}
322
323/* port->lock is not held. */
324static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
325 struct ktermios *old)
326{
327 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
328 unsigned int quot = uart_get_divisor(port, baud);
329 unsigned int iflag, cflag;
330 unsigned long flags;
331
332 spin_lock_irqsave(&port->lock, flags);
333
334 iflag = termios->c_iflag;
335 cflag = termios->c_cflag;
336
337 port->ignore_status_mask = 0;
338 if (iflag & IGNBRK)
339 port->ignore_status_mask |= IGNORE_BREAK;
340 if ((cflag & CREAD) == 0)
341 port->ignore_status_mask |= IGNORE_ALL;
342
343 /* XXX */
344 uart_update_timeout(port, cflag,
345 (port->uartclk / (16 * quot)));
346
347 spin_unlock_irqrestore(&port->lock, flags);
348}
349
350static const char *sunhv_type(struct uart_port *port)
351{
352 return "SUN4V HCONS";
353}
354
355static void sunhv_release_port(struct uart_port *port)
356{
357}
358
359static int sunhv_request_port(struct uart_port *port)
360{
361 return 0;
362}
363
364static void sunhv_config_port(struct uart_port *port, int flags)
365{
366}
367
368static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
369{
370 return -EINVAL;
371}
372
373static struct uart_ops sunhv_pops = {
374 .tx_empty = sunhv_tx_empty,
375 .set_mctrl = sunhv_set_mctrl,
376 .get_mctrl = sunhv_get_mctrl,
377 .stop_tx = sunhv_stop_tx,
378 .start_tx = sunhv_start_tx,
379 .send_xchar = sunhv_send_xchar,
380 .stop_rx = sunhv_stop_rx,
381 .enable_ms = sunhv_enable_ms,
382 .break_ctl = sunhv_break_ctl,
383 .startup = sunhv_startup,
384 .shutdown = sunhv_shutdown,
385 .set_termios = sunhv_set_termios,
386 .type = sunhv_type,
387 .release_port = sunhv_release_port,
388 .request_port = sunhv_request_port,
389 .config_port = sunhv_config_port,
390 .verify_port = sunhv_verify_port,
391};
392
393static struct uart_driver sunhv_reg = {
394 .owner = THIS_MODULE,
395 .driver_name = "sunhv",
396 .dev_name = "ttyS",
397 .major = TTY_MAJOR,
398};
399
400static struct uart_port *sunhv_port;
401
402/* Copy 's' into the con_write_page, decoding "\n" into
403 * "\r\n" along the way. We have to return two lengths
404 * because the caller needs to know how much to advance
405 * 's' and also how many bytes to output via con_write_page.
406 */
407static int fill_con_write_page(const char *s, unsigned int n,
408 unsigned long *page_bytes)
409{
410 const char *orig_s = s;
411 char *p = con_write_page;
412 int left = PAGE_SIZE;
413
414 while (n--) {
415 if (*s == '\n') {
416 if (left < 2)
417 break;
418 *p++ = '\r';
419 left--;
420 } else if (left < 1)
421 break;
422 *p++ = *s++;
423 left--;
424 }
425 *page_bytes = p - con_write_page;
426 return s - orig_s;
427}
428
429static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
430{
431 struct uart_port *port = sunhv_port;
432 unsigned long flags;
433 int locked = 1;
434
435 local_irq_save(flags);
436 if (port->sysrq) {
437 locked = 0;
438 } else if (oops_in_progress) {
439 locked = spin_trylock(&port->lock);
440 } else
441 spin_lock(&port->lock);
442
443 while (n > 0) {
444 unsigned long ra = __pa(con_write_page);
445 unsigned long page_bytes;
446 unsigned int cpy = fill_con_write_page(s, n,
447 &page_bytes);
448
449 n -= cpy;
450 s += cpy;
451 while (page_bytes > 0) {
452 unsigned long written;
453 int limit = 1000000;
454
455 while (limit--) {
456 unsigned long stat;
457
458 stat = sun4v_con_write(ra, page_bytes,
459 &written);
460 if (stat == HV_EOK)
461 break;
462 udelay(1);
463 }
464 if (limit < 0)
465 break;
466 page_bytes -= written;
467 ra += written;
468 }
469 }
470
471 if (locked)
472 spin_unlock(&port->lock);
473 local_irq_restore(flags);
474}
475
476static inline void sunhv_console_putchar(struct uart_port *port, char c)
477{
478 int limit = 1000000;
479
480 while (limit-- > 0) {
481 long status = sun4v_con_putchar(c);
482 if (status == HV_EOK)
483 break;
484 udelay(1);
485 }
486}
487
488static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
489{
490 struct uart_port *port = sunhv_port;
491 unsigned long flags;
492 int i, locked = 1;
493
494 local_irq_save(flags);
495 if (port->sysrq) {
496 locked = 0;
497 } else if (oops_in_progress) {
498 locked = spin_trylock(&port->lock);
499 } else
500 spin_lock(&port->lock);
501
502 for (i = 0; i < n; i++) {
503 if (*s == '\n')
504 sunhv_console_putchar(port, '\r');
505 sunhv_console_putchar(port, *s++);
506 }
507
508 if (locked)
509 spin_unlock(&port->lock);
510 local_irq_restore(flags);
511}
512
513static struct console sunhv_console = {
514 .name = "ttyHV",
515 .write = sunhv_console_write_bychar,
516 .device = uart_console_device,
517 .flags = CON_PRINTBUFFER,
518 .index = -1,
519 .data = &sunhv_reg,
520};
521
522static int __devinit hv_probe(struct platform_device *op, const struct of_device_id *match)
523{
524 struct uart_port *port;
525 unsigned long minor;
526 int err;
527
528 if (op->archdata.irqs[0] == 0xffffffff)
529 return -ENODEV;
530
531 port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
532 if (unlikely(!port))
533 return -ENOMEM;
534
535 minor = 1;
536 if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
537 minor >= 1) {
538 err = -ENOMEM;
539 con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
540 if (!con_write_page)
541 goto out_free_port;
542
543 con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
544 if (!con_read_page)
545 goto out_free_con_write_page;
546
547 sunhv_console.write = sunhv_console_write_paged;
548 sunhv_ops = &bywrite_ops;
549 }
550
551 sunhv_port = port;
552
553 port->line = 0;
554 port->ops = &sunhv_pops;
555 port->type = PORT_SUNHV;
556 port->uartclk = ( 29491200 / 16 ); /* arbitrary */
557
558 port->membase = (unsigned char __iomem *) __pa(port);
559
560 port->irq = op->archdata.irqs[0];
561
562 port->dev = &op->dev;
563
564 err = sunserial_register_minors(&sunhv_reg, 1);
565 if (err)
566 goto out_free_con_read_page;
567
568 sunserial_console_match(&sunhv_console, op->dev.of_node,
569 &sunhv_reg, port->line, false);
570
571 err = uart_add_one_port(&sunhv_reg, port);
572 if (err)
573 goto out_unregister_driver;
574
575 err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
576 if (err)
577 goto out_remove_port;
578
579 dev_set_drvdata(&op->dev, port);
580
581 return 0;
582
583out_remove_port:
584 uart_remove_one_port(&sunhv_reg, port);
585
586out_unregister_driver:
587 sunserial_unregister_minors(&sunhv_reg, 1);
588
589out_free_con_read_page:
590 kfree(con_read_page);
591
592out_free_con_write_page:
593 kfree(con_write_page);
594
595out_free_port:
596 kfree(port);
597 sunhv_port = NULL;
598 return err;
599}
600
601static int __devexit hv_remove(struct platform_device *dev)
602{
603 struct uart_port *port = dev_get_drvdata(&dev->dev);
604
605 free_irq(port->irq, port);
606
607 uart_remove_one_port(&sunhv_reg, port);
608
609 sunserial_unregister_minors(&sunhv_reg, 1);
610
611 kfree(port);
612 sunhv_port = NULL;
613
614 dev_set_drvdata(&dev->dev, NULL);
615
616 return 0;
617}
618
619static const struct of_device_id hv_match[] = {
620 {
621 .name = "console",
622 .compatible = "qcn",
623 },
624 {
625 .name = "console",
626 .compatible = "SUNW,sun4v-console",
627 },
628 {},
629};
630MODULE_DEVICE_TABLE(of, hv_match);
631
632static struct of_platform_driver hv_driver = {
633 .driver = {
634 .name = "hv",
635 .owner = THIS_MODULE,
636 .of_match_table = hv_match,
637 },
638 .probe = hv_probe,
639 .remove = __devexit_p(hv_remove),
640};
641
642static int __init sunhv_init(void)
643{
644 if (tlb_type != hypervisor)
645 return -ENODEV;
646
647 return of_register_platform_driver(&hv_driver);
648}
649
650static void __exit sunhv_exit(void)
651{
652 of_unregister_platform_driver(&hv_driver);
653}
654
655module_init(sunhv_init);
656module_exit(sunhv_exit);
657
658MODULE_AUTHOR("David S. Miller");
659MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
660MODULE_VERSION("2.0");
661MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/sunsab.c b/drivers/tty/serial/sunsab.c
new file mode 100644
index 000000000000..5b246b18f42f
--- /dev/null
+++ b/drivers/tty/serial/sunsab.c
@@ -0,0 +1,1152 @@
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, 2006 David S. Miller (davem@davemloft.net)
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@davemloft.net>
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/errno.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/circ_buf.h>
28#include <linux/serial.h>
29#include <linux/sysrq.h>
30#include <linux/console.h>
31#include <linux/spinlock.h>
32#include <linux/slab.h>
33#include <linux/delay.h>
34#include <linux/init.h>
35#include <linux/of_device.h>
36
37#include <asm/io.h>
38#include <asm/irq.h>
39#include <asm/prom.h>
40
41#if defined(CONFIG_SERIAL_SUNSAB_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
42#define SUPPORT_SYSRQ
43#endif
44
45#include <linux/serial_core.h>
46
47#include "suncore.h"
48#include "sunsab.h"
49
50struct uart_sunsab_port {
51 struct uart_port port; /* Generic UART port */
52 union sab82532_async_regs __iomem *regs; /* Chip registers */
53 unsigned long irqflags; /* IRQ state flags */
54 int dsr; /* Current DSR state */
55 unsigned int cec_timeout; /* Chip poll timeout... */
56 unsigned int tec_timeout; /* likewise */
57 unsigned char interrupt_mask0;/* ISR0 masking */
58 unsigned char interrupt_mask1;/* ISR1 masking */
59 unsigned char pvr_dtr_bit; /* Which PVR bit is DTR */
60 unsigned char pvr_dsr_bit; /* Which PVR bit is DSR */
61 unsigned int gis_shift;
62 int type; /* SAB82532 version */
63
64 /* Setting configuration bits while the transmitter is active
65 * can cause garbage characters to get emitted by the chip.
66 * Therefore, we cache such writes here and do the real register
67 * write the next time the transmitter becomes idle.
68 */
69 unsigned int cached_ebrg;
70 unsigned char cached_mode;
71 unsigned char cached_pvr;
72 unsigned char cached_dafo;
73};
74
75/*
76 * This assumes you have a 29.4912 MHz clock for your UART.
77 */
78#define SAB_BASE_BAUD ( 29491200 / 16 )
79
80static char *sab82532_version[16] = {
81 "V1.0", "V2.0", "V3.2", "V(0x03)",
82 "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
83 "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
84 "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
85};
86
87#define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
88#define SAB82532_MAX_CEC_TIMEOUT 50000 /* 2.5 TX CLKs (at 50 baud) */
89
90#define SAB82532_RECV_FIFO_SIZE 32 /* Standard async fifo sizes */
91#define SAB82532_XMIT_FIFO_SIZE 32
92
93static __inline__ void sunsab_tec_wait(struct uart_sunsab_port *up)
94{
95 int timeout = up->tec_timeout;
96
97 while ((readb(&up->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
98 udelay(1);
99}
100
101static __inline__ void sunsab_cec_wait(struct uart_sunsab_port *up)
102{
103 int timeout = up->cec_timeout;
104
105 while ((readb(&up->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
106 udelay(1);
107}
108
109static struct tty_struct *
110receive_chars(struct uart_sunsab_port *up,
111 union sab82532_irq_status *stat)
112{
113 struct tty_struct *tty = NULL;
114 unsigned char buf[32];
115 int saw_console_brk = 0;
116 int free_fifo = 0;
117 int count = 0;
118 int i;
119
120 if (up->port.state != NULL) /* Unopened serial console */
121 tty = up->port.state->port.tty;
122
123 /* Read number of BYTES (Character + Status) available. */
124 if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
125 count = SAB82532_RECV_FIFO_SIZE;
126 free_fifo++;
127 }
128
129 if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
130 count = readb(&up->regs->r.rbcl) & (SAB82532_RECV_FIFO_SIZE - 1);
131 free_fifo++;
132 }
133
134 /* Issue a FIFO read command in case we where idle. */
135 if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
136 sunsab_cec_wait(up);
137 writeb(SAB82532_CMDR_RFRD, &up->regs->w.cmdr);
138 return tty;
139 }
140
141 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
142 free_fifo++;
143
144 /* Read the FIFO. */
145 for (i = 0; i < count; i++)
146 buf[i] = readb(&up->regs->r.rfifo[i]);
147
148 /* Issue Receive Message Complete command. */
149 if (free_fifo) {
150 sunsab_cec_wait(up);
151 writeb(SAB82532_CMDR_RMC, &up->regs->w.cmdr);
152 }
153
154 /* Count may be zero for BRK, so we check for it here */
155 if ((stat->sreg.isr1 & SAB82532_ISR1_BRK) &&
156 (up->port.line == up->port.cons->index))
157 saw_console_brk = 1;
158
159 for (i = 0; i < count; i++) {
160 unsigned char ch = buf[i], flag;
161
162 if (tty == NULL) {
163 uart_handle_sysrq_char(&up->port, ch);
164 continue;
165 }
166
167 flag = TTY_NORMAL;
168 up->port.icount.rx++;
169
170 if (unlikely(stat->sreg.isr0 & (SAB82532_ISR0_PERR |
171 SAB82532_ISR0_FERR |
172 SAB82532_ISR0_RFO)) ||
173 unlikely(stat->sreg.isr1 & SAB82532_ISR1_BRK)) {
174 /*
175 * For statistics only
176 */
177 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
178 stat->sreg.isr0 &= ~(SAB82532_ISR0_PERR |
179 SAB82532_ISR0_FERR);
180 up->port.icount.brk++;
181 /*
182 * We do the SysRQ and SAK checking
183 * here because otherwise the break
184 * may get masked by ignore_status_mask
185 * or read_status_mask.
186 */
187 if (uart_handle_break(&up->port))
188 continue;
189 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
190 up->port.icount.parity++;
191 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
192 up->port.icount.frame++;
193 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
194 up->port.icount.overrun++;
195
196 /*
197 * Mask off conditions which should be ingored.
198 */
199 stat->sreg.isr0 &= (up->port.read_status_mask & 0xff);
200 stat->sreg.isr1 &= ((up->port.read_status_mask >> 8) & 0xff);
201
202 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
203 flag = TTY_BREAK;
204 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
205 flag = TTY_PARITY;
206 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
207 flag = TTY_FRAME;
208 }
209
210 if (uart_handle_sysrq_char(&up->port, ch))
211 continue;
212
213 if ((stat->sreg.isr0 & (up->port.ignore_status_mask & 0xff)) == 0 &&
214 (stat->sreg.isr1 & ((up->port.ignore_status_mask >> 8) & 0xff)) == 0)
215 tty_insert_flip_char(tty, ch, flag);
216 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
217 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
218 }
219
220 if (saw_console_brk)
221 sun_do_break();
222
223 return tty;
224}
225
226static void sunsab_stop_tx(struct uart_port *);
227static void sunsab_tx_idle(struct uart_sunsab_port *);
228
229static void transmit_chars(struct uart_sunsab_port *up,
230 union sab82532_irq_status *stat)
231{
232 struct circ_buf *xmit = &up->port.state->xmit;
233 int i;
234
235 if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
236 up->interrupt_mask1 |= SAB82532_IMR1_ALLS;
237 writeb(up->interrupt_mask1, &up->regs->w.imr1);
238 set_bit(SAB82532_ALLS, &up->irqflags);
239 }
240
241#if 0 /* bde@nwlink.com says this check causes problems */
242 if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
243 return;
244#endif
245
246 if (!(readb(&up->regs->r.star) & SAB82532_STAR_XFW))
247 return;
248
249 set_bit(SAB82532_XPR, &up->irqflags);
250 sunsab_tx_idle(up);
251
252 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
253 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
254 writeb(up->interrupt_mask1, &up->regs->w.imr1);
255 return;
256 }
257
258 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
259 writeb(up->interrupt_mask1, &up->regs->w.imr1);
260 clear_bit(SAB82532_ALLS, &up->irqflags);
261
262 /* Stuff 32 bytes into Transmit FIFO. */
263 clear_bit(SAB82532_XPR, &up->irqflags);
264 for (i = 0; i < up->port.fifosize; i++) {
265 writeb(xmit->buf[xmit->tail],
266 &up->regs->w.xfifo[i]);
267 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
268 up->port.icount.tx++;
269 if (uart_circ_empty(xmit))
270 break;
271 }
272
273 /* Issue a Transmit Frame command. */
274 sunsab_cec_wait(up);
275 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
276
277 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
278 uart_write_wakeup(&up->port);
279
280 if (uart_circ_empty(xmit))
281 sunsab_stop_tx(&up->port);
282}
283
284static void check_status(struct uart_sunsab_port *up,
285 union sab82532_irq_status *stat)
286{
287 if (stat->sreg.isr0 & SAB82532_ISR0_CDSC)
288 uart_handle_dcd_change(&up->port,
289 !(readb(&up->regs->r.vstr) & SAB82532_VSTR_CD));
290
291 if (stat->sreg.isr1 & SAB82532_ISR1_CSC)
292 uart_handle_cts_change(&up->port,
293 (readb(&up->regs->r.star) & SAB82532_STAR_CTS));
294
295 if ((readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ^ up->dsr) {
296 up->dsr = (readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ? 0 : 1;
297 up->port.icount.dsr++;
298 }
299
300 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
301}
302
303static irqreturn_t sunsab_interrupt(int irq, void *dev_id)
304{
305 struct uart_sunsab_port *up = dev_id;
306 struct tty_struct *tty;
307 union sab82532_irq_status status;
308 unsigned long flags;
309 unsigned char gis;
310
311 spin_lock_irqsave(&up->port.lock, flags);
312
313 status.stat = 0;
314 gis = readb(&up->regs->r.gis) >> up->gis_shift;
315 if (gis & 1)
316 status.sreg.isr0 = readb(&up->regs->r.isr0);
317 if (gis & 2)
318 status.sreg.isr1 = readb(&up->regs->r.isr1);
319
320 tty = NULL;
321 if (status.stat) {
322 if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
323 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
324 (status.sreg.isr1 & SAB82532_ISR1_BRK))
325 tty = receive_chars(up, &status);
326 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
327 (status.sreg.isr1 & SAB82532_ISR1_CSC))
328 check_status(up, &status);
329 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
330 transmit_chars(up, &status);
331 }
332
333 spin_unlock_irqrestore(&up->port.lock, flags);
334
335 if (tty)
336 tty_flip_buffer_push(tty);
337
338 return IRQ_HANDLED;
339}
340
341/* port->lock is not held. */
342static unsigned int sunsab_tx_empty(struct uart_port *port)
343{
344 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
345 int ret;
346
347 /* Do not need a lock for a state test like this. */
348 if (test_bit(SAB82532_ALLS, &up->irqflags))
349 ret = TIOCSER_TEMT;
350 else
351 ret = 0;
352
353 return ret;
354}
355
356/* port->lock held by caller. */
357static void sunsab_set_mctrl(struct uart_port *port, unsigned int mctrl)
358{
359 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
360
361 if (mctrl & TIOCM_RTS) {
362 up->cached_mode &= ~SAB82532_MODE_FRTS;
363 up->cached_mode |= SAB82532_MODE_RTS;
364 } else {
365 up->cached_mode |= (SAB82532_MODE_FRTS |
366 SAB82532_MODE_RTS);
367 }
368 if (mctrl & TIOCM_DTR) {
369 up->cached_pvr &= ~(up->pvr_dtr_bit);
370 } else {
371 up->cached_pvr |= up->pvr_dtr_bit;
372 }
373
374 set_bit(SAB82532_REGS_PENDING, &up->irqflags);
375 if (test_bit(SAB82532_XPR, &up->irqflags))
376 sunsab_tx_idle(up);
377}
378
379/* port->lock is held by caller and interrupts are disabled. */
380static unsigned int sunsab_get_mctrl(struct uart_port *port)
381{
382 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
383 unsigned char val;
384 unsigned int result;
385
386 result = 0;
387
388 val = readb(&up->regs->r.pvr);
389 result |= (val & up->pvr_dsr_bit) ? 0 : TIOCM_DSR;
390
391 val = readb(&up->regs->r.vstr);
392 result |= (val & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR;
393
394 val = readb(&up->regs->r.star);
395 result |= (val & SAB82532_STAR_CTS) ? TIOCM_CTS : 0;
396
397 return result;
398}
399
400/* port->lock held by caller. */
401static void sunsab_stop_tx(struct uart_port *port)
402{
403 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
404
405 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
406 writeb(up->interrupt_mask1, &up->regs->w.imr1);
407}
408
409/* port->lock held by caller. */
410static void sunsab_tx_idle(struct uart_sunsab_port *up)
411{
412 if (test_bit(SAB82532_REGS_PENDING, &up->irqflags)) {
413 u8 tmp;
414
415 clear_bit(SAB82532_REGS_PENDING, &up->irqflags);
416 writeb(up->cached_mode, &up->regs->rw.mode);
417 writeb(up->cached_pvr, &up->regs->rw.pvr);
418 writeb(up->cached_dafo, &up->regs->w.dafo);
419
420 writeb(up->cached_ebrg & 0xff, &up->regs->w.bgr);
421 tmp = readb(&up->regs->rw.ccr2);
422 tmp &= ~0xc0;
423 tmp |= (up->cached_ebrg >> 2) & 0xc0;
424 writeb(tmp, &up->regs->rw.ccr2);
425 }
426}
427
428/* port->lock held by caller. */
429static void sunsab_start_tx(struct uart_port *port)
430{
431 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
432 struct circ_buf *xmit = &up->port.state->xmit;
433 int i;
434
435 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
436 writeb(up->interrupt_mask1, &up->regs->w.imr1);
437
438 if (!test_bit(SAB82532_XPR, &up->irqflags))
439 return;
440
441 clear_bit(SAB82532_ALLS, &up->irqflags);
442 clear_bit(SAB82532_XPR, &up->irqflags);
443
444 for (i = 0; i < up->port.fifosize; i++) {
445 writeb(xmit->buf[xmit->tail],
446 &up->regs->w.xfifo[i]);
447 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
448 up->port.icount.tx++;
449 if (uart_circ_empty(xmit))
450 break;
451 }
452
453 /* Issue a Transmit Frame command. */
454 sunsab_cec_wait(up);
455 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
456}
457
458/* port->lock is not held. */
459static void sunsab_send_xchar(struct uart_port *port, char ch)
460{
461 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
462 unsigned long flags;
463
464 spin_lock_irqsave(&up->port.lock, flags);
465
466 sunsab_tec_wait(up);
467 writeb(ch, &up->regs->w.tic);
468
469 spin_unlock_irqrestore(&up->port.lock, flags);
470}
471
472/* port->lock held by caller. */
473static void sunsab_stop_rx(struct uart_port *port)
474{
475 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
476
477 up->interrupt_mask0 |= SAB82532_IMR0_TCD;
478 writeb(up->interrupt_mask1, &up->regs->w.imr0);
479}
480
481/* port->lock held by caller. */
482static void sunsab_enable_ms(struct uart_port *port)
483{
484 /* For now we always receive these interrupts. */
485}
486
487/* port->lock is not held. */
488static void sunsab_break_ctl(struct uart_port *port, int break_state)
489{
490 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
491 unsigned long flags;
492 unsigned char val;
493
494 spin_lock_irqsave(&up->port.lock, flags);
495
496 val = up->cached_dafo;
497 if (break_state)
498 val |= SAB82532_DAFO_XBRK;
499 else
500 val &= ~SAB82532_DAFO_XBRK;
501 up->cached_dafo = val;
502
503 set_bit(SAB82532_REGS_PENDING, &up->irqflags);
504 if (test_bit(SAB82532_XPR, &up->irqflags))
505 sunsab_tx_idle(up);
506
507 spin_unlock_irqrestore(&up->port.lock, flags);
508}
509
510/* port->lock is not held. */
511static int sunsab_startup(struct uart_port *port)
512{
513 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
514 unsigned long flags;
515 unsigned char tmp;
516 int err = request_irq(up->port.irq, sunsab_interrupt,
517 IRQF_SHARED, "sab", up);
518 if (err)
519 return err;
520
521 spin_lock_irqsave(&up->port.lock, flags);
522
523 /*
524 * Wait for any commands or immediate characters
525 */
526 sunsab_cec_wait(up);
527 sunsab_tec_wait(up);
528
529 /*
530 * Clear the FIFO buffers.
531 */
532 writeb(SAB82532_CMDR_RRES, &up->regs->w.cmdr);
533 sunsab_cec_wait(up);
534 writeb(SAB82532_CMDR_XRES, &up->regs->w.cmdr);
535
536 /*
537 * Clear the interrupt registers.
538 */
539 (void) readb(&up->regs->r.isr0);
540 (void) readb(&up->regs->r.isr1);
541
542 /*
543 * Now, initialize the UART
544 */
545 writeb(0, &up->regs->w.ccr0); /* power-down */
546 writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
547 SAB82532_CCR0_SM_ASYNC, &up->regs->w.ccr0);
548 writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &up->regs->w.ccr1);
549 writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
550 SAB82532_CCR2_TOE, &up->regs->w.ccr2);
551 writeb(0, &up->regs->w.ccr3);
552 writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &up->regs->w.ccr4);
553 up->cached_mode = (SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
554 SAB82532_MODE_RAC);
555 writeb(up->cached_mode, &up->regs->w.mode);
556 writeb(SAB82532_RFC_DPS|SAB82532_RFC_RFTH_32, &up->regs->w.rfc);
557
558 tmp = readb(&up->regs->rw.ccr0);
559 tmp |= SAB82532_CCR0_PU; /* power-up */
560 writeb(tmp, &up->regs->rw.ccr0);
561
562 /*
563 * Finally, enable interrupts
564 */
565 up->interrupt_mask0 = (SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
566 SAB82532_IMR0_PLLA);
567 writeb(up->interrupt_mask0, &up->regs->w.imr0);
568 up->interrupt_mask1 = (SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
569 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
570 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
571 SAB82532_IMR1_XPR);
572 writeb(up->interrupt_mask1, &up->regs->w.imr1);
573 set_bit(SAB82532_ALLS, &up->irqflags);
574 set_bit(SAB82532_XPR, &up->irqflags);
575
576 spin_unlock_irqrestore(&up->port.lock, flags);
577
578 return 0;
579}
580
581/* port->lock is not held. */
582static void sunsab_shutdown(struct uart_port *port)
583{
584 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
585 unsigned long flags;
586
587 spin_lock_irqsave(&up->port.lock, flags);
588
589 /* Disable Interrupts */
590 up->interrupt_mask0 = 0xff;
591 writeb(up->interrupt_mask0, &up->regs->w.imr0);
592 up->interrupt_mask1 = 0xff;
593 writeb(up->interrupt_mask1, &up->regs->w.imr1);
594
595 /* Disable break condition */
596 up->cached_dafo = readb(&up->regs->rw.dafo);
597 up->cached_dafo &= ~SAB82532_DAFO_XBRK;
598 writeb(up->cached_dafo, &up->regs->rw.dafo);
599
600 /* Disable Receiver */
601 up->cached_mode &= ~SAB82532_MODE_RAC;
602 writeb(up->cached_mode, &up->regs->rw.mode);
603
604 /*
605 * XXX FIXME
606 *
607 * If the chip is powered down here the system hangs/crashes during
608 * reboot or shutdown. This needs to be investigated further,
609 * similar behaviour occurs in 2.4 when the driver is configured
610 * as a module only. One hint may be that data is sometimes
611 * transmitted at 9600 baud during shutdown (regardless of the
612 * speed the chip was configured for when the port was open).
613 */
614#if 0
615 /* Power Down */
616 tmp = readb(&up->regs->rw.ccr0);
617 tmp &= ~SAB82532_CCR0_PU;
618 writeb(tmp, &up->regs->rw.ccr0);
619#endif
620
621 spin_unlock_irqrestore(&up->port.lock, flags);
622 free_irq(up->port.irq, up);
623}
624
625/*
626 * This is used to figure out the divisor speeds.
627 *
628 * The formula is: Baud = SAB_BASE_BAUD / ((N + 1) * (1 << M)),
629 *
630 * with 0 <= N < 64 and 0 <= M < 16
631 */
632
633static void calc_ebrg(int baud, int *n_ret, int *m_ret)
634{
635 int n, m;
636
637 if (baud == 0) {
638 *n_ret = 0;
639 *m_ret = 0;
640 return;
641 }
642
643 /*
644 * We scale numbers by 10 so that we get better accuracy
645 * without having to use floating point. Here we increment m
646 * until n is within the valid range.
647 */
648 n = (SAB_BASE_BAUD * 10) / baud;
649 m = 0;
650 while (n >= 640) {
651 n = n / 2;
652 m++;
653 }
654 n = (n+5) / 10;
655 /*
656 * We try very hard to avoid speeds with M == 0 since they may
657 * not work correctly for XTAL frequences above 10 MHz.
658 */
659 if ((m == 0) && ((n & 1) == 0)) {
660 n = n / 2;
661 m++;
662 }
663 *n_ret = n - 1;
664 *m_ret = m;
665}
666
667/* Internal routine, port->lock is held and local interrupts are disabled. */
668static void sunsab_convert_to_sab(struct uart_sunsab_port *up, unsigned int cflag,
669 unsigned int iflag, unsigned int baud,
670 unsigned int quot)
671{
672 unsigned char dafo;
673 int bits, n, m;
674
675 /* Byte size and parity */
676 switch (cflag & CSIZE) {
677 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
678 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
679 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
680 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
681 /* Never happens, but GCC is too dumb to figure it out */
682 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
683 }
684
685 if (cflag & CSTOPB) {
686 dafo |= SAB82532_DAFO_STOP;
687 bits++;
688 }
689
690 if (cflag & PARENB) {
691 dafo |= SAB82532_DAFO_PARE;
692 bits++;
693 }
694
695 if (cflag & PARODD) {
696 dafo |= SAB82532_DAFO_PAR_ODD;
697 } else {
698 dafo |= SAB82532_DAFO_PAR_EVEN;
699 }
700 up->cached_dafo = dafo;
701
702 calc_ebrg(baud, &n, &m);
703
704 up->cached_ebrg = n | (m << 6);
705
706 up->tec_timeout = (10 * 1000000) / baud;
707 up->cec_timeout = up->tec_timeout >> 2;
708
709 /* CTS flow control flags */
710 /* We encode read_status_mask and ignore_status_mask like so:
711 *
712 * ---------------------
713 * | ... | ISR1 | ISR0 |
714 * ---------------------
715 * .. 15 8 7 0
716 */
717
718 up->port.read_status_mask = (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
719 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF |
720 SAB82532_ISR0_CDSC);
721 up->port.read_status_mask |= (SAB82532_ISR1_CSC |
722 SAB82532_ISR1_ALLS |
723 SAB82532_ISR1_XPR) << 8;
724 if (iflag & INPCK)
725 up->port.read_status_mask |= (SAB82532_ISR0_PERR |
726 SAB82532_ISR0_FERR);
727 if (iflag & (BRKINT | PARMRK))
728 up->port.read_status_mask |= (SAB82532_ISR1_BRK << 8);
729
730 /*
731 * Characteres to ignore
732 */
733 up->port.ignore_status_mask = 0;
734 if (iflag & IGNPAR)
735 up->port.ignore_status_mask |= (SAB82532_ISR0_PERR |
736 SAB82532_ISR0_FERR);
737 if (iflag & IGNBRK) {
738 up->port.ignore_status_mask |= (SAB82532_ISR1_BRK << 8);
739 /*
740 * If we're ignoring parity and break indicators,
741 * ignore overruns too (for real raw support).
742 */
743 if (iflag & IGNPAR)
744 up->port.ignore_status_mask |= SAB82532_ISR0_RFO;
745 }
746
747 /*
748 * ignore all characters if CREAD is not set
749 */
750 if ((cflag & CREAD) == 0)
751 up->port.ignore_status_mask |= (SAB82532_ISR0_RPF |
752 SAB82532_ISR0_TCD);
753
754 uart_update_timeout(&up->port, cflag,
755 (up->port.uartclk / (16 * quot)));
756
757 /* Now schedule a register update when the chip's
758 * transmitter is idle.
759 */
760 up->cached_mode |= SAB82532_MODE_RAC;
761 set_bit(SAB82532_REGS_PENDING, &up->irqflags);
762 if (test_bit(SAB82532_XPR, &up->irqflags))
763 sunsab_tx_idle(up);
764}
765
766/* port->lock is not held. */
767static void sunsab_set_termios(struct uart_port *port, struct ktermios *termios,
768 struct ktermios *old)
769{
770 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
771 unsigned long flags;
772 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
773 unsigned int quot = uart_get_divisor(port, baud);
774
775 spin_lock_irqsave(&up->port.lock, flags);
776 sunsab_convert_to_sab(up, termios->c_cflag, termios->c_iflag, baud, quot);
777 spin_unlock_irqrestore(&up->port.lock, flags);
778}
779
780static const char *sunsab_type(struct uart_port *port)
781{
782 struct uart_sunsab_port *up = (void *)port;
783 static char buf[36];
784
785 sprintf(buf, "SAB82532 %s", sab82532_version[up->type]);
786 return buf;
787}
788
789static void sunsab_release_port(struct uart_port *port)
790{
791}
792
793static int sunsab_request_port(struct uart_port *port)
794{
795 return 0;
796}
797
798static void sunsab_config_port(struct uart_port *port, int flags)
799{
800}
801
802static int sunsab_verify_port(struct uart_port *port, struct serial_struct *ser)
803{
804 return -EINVAL;
805}
806
807static struct uart_ops sunsab_pops = {
808 .tx_empty = sunsab_tx_empty,
809 .set_mctrl = sunsab_set_mctrl,
810 .get_mctrl = sunsab_get_mctrl,
811 .stop_tx = sunsab_stop_tx,
812 .start_tx = sunsab_start_tx,
813 .send_xchar = sunsab_send_xchar,
814 .stop_rx = sunsab_stop_rx,
815 .enable_ms = sunsab_enable_ms,
816 .break_ctl = sunsab_break_ctl,
817 .startup = sunsab_startup,
818 .shutdown = sunsab_shutdown,
819 .set_termios = sunsab_set_termios,
820 .type = sunsab_type,
821 .release_port = sunsab_release_port,
822 .request_port = sunsab_request_port,
823 .config_port = sunsab_config_port,
824 .verify_port = sunsab_verify_port,
825};
826
827static struct uart_driver sunsab_reg = {
828 .owner = THIS_MODULE,
829 .driver_name = "sunsab",
830 .dev_name = "ttyS",
831 .major = TTY_MAJOR,
832};
833
834static struct uart_sunsab_port *sunsab_ports;
835
836#ifdef CONFIG_SERIAL_SUNSAB_CONSOLE
837
838static void sunsab_console_putchar(struct uart_port *port, int c)
839{
840 struct uart_sunsab_port *up = (struct uart_sunsab_port *)port;
841
842 sunsab_tec_wait(up);
843 writeb(c, &up->regs->w.tic);
844}
845
846static void sunsab_console_write(struct console *con, const char *s, unsigned n)
847{
848 struct uart_sunsab_port *up = &sunsab_ports[con->index];
849 unsigned long flags;
850 int locked = 1;
851
852 local_irq_save(flags);
853 if (up->port.sysrq) {
854 locked = 0;
855 } else if (oops_in_progress) {
856 locked = spin_trylock(&up->port.lock);
857 } else
858 spin_lock(&up->port.lock);
859
860 uart_console_write(&up->port, s, n, sunsab_console_putchar);
861 sunsab_tec_wait(up);
862
863 if (locked)
864 spin_unlock(&up->port.lock);
865 local_irq_restore(flags);
866}
867
868static int sunsab_console_setup(struct console *con, char *options)
869{
870 struct uart_sunsab_port *up = &sunsab_ports[con->index];
871 unsigned long flags;
872 unsigned int baud, quot;
873
874 /*
875 * The console framework calls us for each and every port
876 * registered. Defer the console setup until the requested
877 * port has been properly discovered. A bit of a hack,
878 * though...
879 */
880 if (up->port.type != PORT_SUNSAB)
881 return -1;
882
883 printk("Console: ttyS%d (SAB82532)\n",
884 (sunsab_reg.minor - 64) + con->index);
885
886 sunserial_console_termios(con, up->port.dev->of_node);
887
888 switch (con->cflag & CBAUD) {
889 case B150: baud = 150; break;
890 case B300: baud = 300; break;
891 case B600: baud = 600; break;
892 case B1200: baud = 1200; break;
893 case B2400: baud = 2400; break;
894 case B4800: baud = 4800; break;
895 default: case B9600: baud = 9600; break;
896 case B19200: baud = 19200; break;
897 case B38400: baud = 38400; break;
898 case B57600: baud = 57600; break;
899 case B115200: baud = 115200; break;
900 case B230400: baud = 230400; break;
901 case B460800: baud = 460800; break;
902 };
903
904 /*
905 * Temporary fix.
906 */
907 spin_lock_init(&up->port.lock);
908
909 /*
910 * Initialize the hardware
911 */
912 sunsab_startup(&up->port);
913
914 spin_lock_irqsave(&up->port.lock, flags);
915
916 /*
917 * Finally, enable interrupts
918 */
919 up->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
920 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
921 writeb(up->interrupt_mask0, &up->regs->w.imr0);
922 up->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
923 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
924 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
925 SAB82532_IMR1_XPR;
926 writeb(up->interrupt_mask1, &up->regs->w.imr1);
927
928 quot = uart_get_divisor(&up->port, baud);
929 sunsab_convert_to_sab(up, con->cflag, 0, baud, quot);
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
947static inline struct console *SUNSAB_CONSOLE(void)
948{
949 return &sunsab_console;
950}
951#else
952#define SUNSAB_CONSOLE() (NULL)
953#define sunsab_console_init() do { } while (0)
954#endif
955
956static int __devinit sunsab_init_one(struct uart_sunsab_port *up,
957 struct platform_device *op,
958 unsigned long offset,
959 int line)
960{
961 up->port.line = line;
962 up->port.dev = &op->dev;
963
964 up->port.mapbase = op->resource[0].start + offset;
965 up->port.membase = of_ioremap(&op->resource[0], offset,
966 sizeof(union sab82532_async_regs),
967 "sab");
968 if (!up->port.membase)
969 return -ENOMEM;
970 up->regs = (union sab82532_async_regs __iomem *) up->port.membase;
971
972 up->port.irq = op->archdata.irqs[0];
973
974 up->port.fifosize = SAB82532_XMIT_FIFO_SIZE;
975 up->port.iotype = UPIO_MEM;
976
977 writeb(SAB82532_IPC_IC_ACT_LOW, &up->regs->w.ipc);
978
979 up->port.ops = &sunsab_pops;
980 up->port.type = PORT_SUNSAB;
981 up->port.uartclk = SAB_BASE_BAUD;
982
983 up->type = readb(&up->regs->r.vstr) & 0x0f;
984 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &up->regs->w.pcr);
985 writeb(0xff, &up->regs->w.pim);
986 if ((up->port.line & 0x1) == 0) {
987 up->pvr_dsr_bit = (1 << 0);
988 up->pvr_dtr_bit = (1 << 1);
989 up->gis_shift = 2;
990 } else {
991 up->pvr_dsr_bit = (1 << 3);
992 up->pvr_dtr_bit = (1 << 2);
993 up->gis_shift = 0;
994 }
995 up->cached_pvr = (1 << 1) | (1 << 2) | (1 << 4);
996 writeb(up->cached_pvr, &up->regs->w.pvr);
997 up->cached_mode = readb(&up->regs->rw.mode);
998 up->cached_mode |= SAB82532_MODE_FRTS;
999 writeb(up->cached_mode, &up->regs->rw.mode);
1000 up->cached_mode |= SAB82532_MODE_RTS;
1001 writeb(up->cached_mode, &up->regs->rw.mode);
1002
1003 up->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
1004 up->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
1005
1006 return 0;
1007}
1008
1009static int __devinit sab_probe(struct platform_device *op, const struct of_device_id *match)
1010{
1011 static int inst;
1012 struct uart_sunsab_port *up;
1013 int err;
1014
1015 up = &sunsab_ports[inst * 2];
1016
1017 err = sunsab_init_one(&up[0], op,
1018 0,
1019 (inst * 2) + 0);
1020 if (err)
1021 goto out;
1022
1023 err = sunsab_init_one(&up[1], op,
1024 sizeof(union sab82532_async_regs),
1025 (inst * 2) + 1);
1026 if (err)
1027 goto out1;
1028
1029 sunserial_console_match(SUNSAB_CONSOLE(), op->dev.of_node,
1030 &sunsab_reg, up[0].port.line,
1031 false);
1032
1033 sunserial_console_match(SUNSAB_CONSOLE(), op->dev.of_node,
1034 &sunsab_reg, up[1].port.line,
1035 false);
1036
1037 err = uart_add_one_port(&sunsab_reg, &up[0].port);
1038 if (err)
1039 goto out2;
1040
1041 err = uart_add_one_port(&sunsab_reg, &up[1].port);
1042 if (err)
1043 goto out3;
1044
1045 dev_set_drvdata(&op->dev, &up[0]);
1046
1047 inst++;
1048
1049 return 0;
1050
1051out3:
1052 uart_remove_one_port(&sunsab_reg, &up[0].port);
1053out2:
1054 of_iounmap(&op->resource[0],
1055 up[1].port.membase,
1056 sizeof(union sab82532_async_regs));
1057out1:
1058 of_iounmap(&op->resource[0],
1059 up[0].port.membase,
1060 sizeof(union sab82532_async_regs));
1061out:
1062 return err;
1063}
1064
1065static int __devexit sab_remove(struct platform_device *op)
1066{
1067 struct uart_sunsab_port *up = dev_get_drvdata(&op->dev);
1068
1069 uart_remove_one_port(&sunsab_reg, &up[1].port);
1070 uart_remove_one_port(&sunsab_reg, &up[0].port);
1071 of_iounmap(&op->resource[0],
1072 up[1].port.membase,
1073 sizeof(union sab82532_async_regs));
1074 of_iounmap(&op->resource[0],
1075 up[0].port.membase,
1076 sizeof(union sab82532_async_regs));
1077
1078 dev_set_drvdata(&op->dev, NULL);
1079
1080 return 0;
1081}
1082
1083static const struct of_device_id sab_match[] = {
1084 {
1085 .name = "se",
1086 },
1087 {
1088 .name = "serial",
1089 .compatible = "sab82532",
1090 },
1091 {},
1092};
1093MODULE_DEVICE_TABLE(of, sab_match);
1094
1095static struct of_platform_driver sab_driver = {
1096 .driver = {
1097 .name = "sab",
1098 .owner = THIS_MODULE,
1099 .of_match_table = sab_match,
1100 },
1101 .probe = sab_probe,
1102 .remove = __devexit_p(sab_remove),
1103};
1104
1105static int __init sunsab_init(void)
1106{
1107 struct device_node *dp;
1108 int err;
1109 int num_channels = 0;
1110
1111 for_each_node_by_name(dp, "se")
1112 num_channels += 2;
1113 for_each_node_by_name(dp, "serial") {
1114 if (of_device_is_compatible(dp, "sab82532"))
1115 num_channels += 2;
1116 }
1117
1118 if (num_channels) {
1119 sunsab_ports = kzalloc(sizeof(struct uart_sunsab_port) *
1120 num_channels, GFP_KERNEL);
1121 if (!sunsab_ports)
1122 return -ENOMEM;
1123
1124 err = sunserial_register_minors(&sunsab_reg, num_channels);
1125 if (err) {
1126 kfree(sunsab_ports);
1127 sunsab_ports = NULL;
1128
1129 return err;
1130 }
1131 }
1132
1133 return of_register_platform_driver(&sab_driver);
1134}
1135
1136static void __exit sunsab_exit(void)
1137{
1138 of_unregister_platform_driver(&sab_driver);
1139 if (sunsab_reg.nr) {
1140 sunserial_unregister_minors(&sunsab_reg, sunsab_reg.nr);
1141 }
1142
1143 kfree(sunsab_ports);
1144 sunsab_ports = NULL;
1145}
1146
1147module_init(sunsab_init);
1148module_exit(sunsab_exit);
1149
1150MODULE_AUTHOR("Eddie C. Dost and David S. Miller");
1151MODULE_DESCRIPTION("Sun SAB82532 serial port driver");
1152MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/sunsab.h b/drivers/tty/serial/sunsab.h
new file mode 100644
index 000000000000..b78e1f7b8050
--- /dev/null
+++ b/drivers/tty/serial/sunsab.h
@@ -0,0 +1,322 @@
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#define SAB82532_REGS_PENDING 0x00000004
130
131/* RFIFO Status Byte */
132#define SAB82532_RSTAT_PE 0x80
133#define SAB82532_RSTAT_FE 0x40
134#define SAB82532_RSTAT_PARITY 0x01
135
136/* Status Register (STAR) */
137#define SAB82532_STAR_XDOV 0x80
138#define SAB82532_STAR_XFW 0x40
139#define SAB82532_STAR_RFNE 0x20
140#define SAB82532_STAR_FCS 0x10
141#define SAB82532_STAR_TEC 0x08
142#define SAB82532_STAR_CEC 0x04
143#define SAB82532_STAR_CTS 0x02
144
145/* Command Register (CMDR) */
146#define SAB82532_CMDR_RMC 0x80
147#define SAB82532_CMDR_RRES 0x40
148#define SAB82532_CMDR_RFRD 0x20
149#define SAB82532_CMDR_STI 0x10
150#define SAB82532_CMDR_XF 0x08
151#define SAB82532_CMDR_XRES 0x01
152
153/* Mode Register (MODE) */
154#define SAB82532_MODE_FRTS 0x40
155#define SAB82532_MODE_FCTS 0x20
156#define SAB82532_MODE_FLON 0x10
157#define SAB82532_MODE_RAC 0x08
158#define SAB82532_MODE_RTS 0x04
159#define SAB82532_MODE_TRS 0x02
160#define SAB82532_MODE_TLP 0x01
161
162/* Timer Register (TIMR) */
163#define SAB82532_TIMR_CNT_MASK 0xe0
164#define SAB82532_TIMR_VALUE_MASK 0x1f
165
166/* Data Format (DAFO) */
167#define SAB82532_DAFO_XBRK 0x40
168#define SAB82532_DAFO_STOP 0x20
169#define SAB82532_DAFO_PAR_SPACE 0x00
170#define SAB82532_DAFO_PAR_ODD 0x08
171#define SAB82532_DAFO_PAR_EVEN 0x10
172#define SAB82532_DAFO_PAR_MARK 0x18
173#define SAB82532_DAFO_PARE 0x04
174#define SAB82532_DAFO_CHL8 0x00
175#define SAB82532_DAFO_CHL7 0x01
176#define SAB82532_DAFO_CHL6 0x02
177#define SAB82532_DAFO_CHL5 0x03
178
179/* RFIFO Control Register (RFC) */
180#define SAB82532_RFC_DPS 0x40
181#define SAB82532_RFC_DXS 0x20
182#define SAB82532_RFC_RFDF 0x10
183#define SAB82532_RFC_RFTH_1 0x00
184#define SAB82532_RFC_RFTH_4 0x04
185#define SAB82532_RFC_RFTH_16 0x08
186#define SAB82532_RFC_RFTH_32 0x0c
187#define SAB82532_RFC_TCDE 0x01
188
189/* Received Byte Count High (RBCH) */
190#define SAB82532_RBCH_DMA 0x80
191#define SAB82532_RBCH_CAS 0x20
192
193/* Transmit Byte Count High (XBCH) */
194#define SAB82532_XBCH_DMA 0x80
195#define SAB82532_XBCH_CAS 0x20
196#define SAB82532_XBCH_XC 0x10
197
198/* Channel Configuration Register 0 (CCR0) */
199#define SAB82532_CCR0_PU 0x80
200#define SAB82532_CCR0_MCE 0x40
201#define SAB82532_CCR0_SC_NRZ 0x00
202#define SAB82532_CCR0_SC_NRZI 0x08
203#define SAB82532_CCR0_SC_FM0 0x10
204#define SAB82532_CCR0_SC_FM1 0x14
205#define SAB82532_CCR0_SC_MANCH 0x18
206#define SAB82532_CCR0_SM_HDLC 0x00
207#define SAB82532_CCR0_SM_SDLC_LOOP 0x01
208#define SAB82532_CCR0_SM_BISYNC 0x02
209#define SAB82532_CCR0_SM_ASYNC 0x03
210
211/* Channel Configuration Register 1 (CCR1) */
212#define SAB82532_CCR1_ODS 0x10
213#define SAB82532_CCR1_BCR 0x08
214#define SAB82532_CCR1_CM_MASK 0x07
215
216/* Channel Configuration Register 2 (CCR2) */
217#define SAB82532_CCR2_SOC1 0x80
218#define SAB82532_CCR2_SOC0 0x40
219#define SAB82532_CCR2_BR9 0x80
220#define SAB82532_CCR2_BR8 0x40
221#define SAB82532_CCR2_BDF 0x20
222#define SAB82532_CCR2_SSEL 0x10
223#define SAB82532_CCR2_XCS0 0x20
224#define SAB82532_CCR2_RCS0 0x10
225#define SAB82532_CCR2_TOE 0x08
226#define SAB82532_CCR2_RWX 0x04
227#define SAB82532_CCR2_DIV 0x01
228
229/* Channel Configuration Register 3 (CCR3) */
230#define SAB82532_CCR3_PSD 0x01
231
232/* Time Slot Assignment Register Transmit (TSAX) */
233#define SAB82532_TSAX_TSNX_MASK 0xfc
234#define SAB82532_TSAX_XCS2 0x02 /* see also CCR2 */
235#define SAB82532_TSAX_XCS1 0x01
236
237/* Time Slot Assignment Register Receive (TSAR) */
238#define SAB82532_TSAR_TSNR_MASK 0xfc
239#define SAB82532_TSAR_RCS2 0x02 /* see also CCR2 */
240#define SAB82532_TSAR_RCS1 0x01
241
242/* Version Status Register (VSTR) */
243#define SAB82532_VSTR_CD 0x80
244#define SAB82532_VSTR_DPLA 0x40
245#define SAB82532_VSTR_VN_MASK 0x0f
246#define SAB82532_VSTR_VN_1 0x00
247#define SAB82532_VSTR_VN_2 0x01
248#define SAB82532_VSTR_VN_3_2 0x02
249
250/* Global Interrupt Status Register (GIS) */
251#define SAB82532_GIS_PI 0x80
252#define SAB82532_GIS_ISA1 0x08
253#define SAB82532_GIS_ISA0 0x04
254#define SAB82532_GIS_ISB1 0x02
255#define SAB82532_GIS_ISB0 0x01
256
257/* Interrupt Vector Address (IVA) */
258#define SAB82532_IVA_MASK 0xf1
259
260/* Interrupt Port Configuration (IPC) */
261#define SAB82532_IPC_VIS 0x80
262#define SAB82532_IPC_SLA1 0x10
263#define SAB82532_IPC_SLA0 0x08
264#define SAB82532_IPC_CASM 0x04
265#define SAB82532_IPC_IC_OPEN_DRAIN 0x00
266#define SAB82532_IPC_IC_ACT_LOW 0x01
267#define SAB82532_IPC_IC_ACT_HIGH 0x03
268
269/* Interrupt Status Register 0 (ISR0) */
270#define SAB82532_ISR0_TCD 0x80
271#define SAB82532_ISR0_TIME 0x40
272#define SAB82532_ISR0_PERR 0x20
273#define SAB82532_ISR0_FERR 0x10
274#define SAB82532_ISR0_PLLA 0x08
275#define SAB82532_ISR0_CDSC 0x04
276#define SAB82532_ISR0_RFO 0x02
277#define SAB82532_ISR0_RPF 0x01
278
279/* Interrupt Status Register 1 (ISR1) */
280#define SAB82532_ISR1_BRK 0x80
281#define SAB82532_ISR1_BRKT 0x40
282#define SAB82532_ISR1_ALLS 0x20
283#define SAB82532_ISR1_XOFF 0x10
284#define SAB82532_ISR1_TIN 0x08
285#define SAB82532_ISR1_CSC 0x04
286#define SAB82532_ISR1_XON 0x02
287#define SAB82532_ISR1_XPR 0x01
288
289/* Interrupt Mask Register 0 (IMR0) */
290#define SAB82532_IMR0_TCD 0x80
291#define SAB82532_IMR0_TIME 0x40
292#define SAB82532_IMR0_PERR 0x20
293#define SAB82532_IMR0_FERR 0x10
294#define SAB82532_IMR0_PLLA 0x08
295#define SAB82532_IMR0_CDSC 0x04
296#define SAB82532_IMR0_RFO 0x02
297#define SAB82532_IMR0_RPF 0x01
298
299/* Interrupt Mask Register 1 (IMR1) */
300#define SAB82532_IMR1_BRK 0x80
301#define SAB82532_IMR1_BRKT 0x40
302#define SAB82532_IMR1_ALLS 0x20
303#define SAB82532_IMR1_XOFF 0x10
304#define SAB82532_IMR1_TIN 0x08
305#define SAB82532_IMR1_CSC 0x04
306#define SAB82532_IMR1_XON 0x02
307#define SAB82532_IMR1_XPR 0x01
308
309/* Port Interrupt Status Register (PIS) */
310#define SAB82532_PIS_SYNC_B 0x08
311#define SAB82532_PIS_DTR_B 0x04
312#define SAB82532_PIS_DTR_A 0x02
313#define SAB82532_PIS_SYNC_A 0x01
314
315/* Channel Configuration Register 4 (CCR4) */
316#define SAB82532_CCR4_MCK4 0x80
317#define SAB82532_CCR4_EBRG 0x40
318#define SAB82532_CCR4_TST1 0x20
319#define SAB82532_CCR4_ICD 0x10
320
321
322#endif /* !(_SUNSAB_H) */
diff --git a/drivers/tty/serial/sunsu.c b/drivers/tty/serial/sunsu.c
new file mode 100644
index 000000000000..551ebfe3ccbb
--- /dev/null
+++ b/drivers/tty/serial/sunsu.c
@@ -0,0 +1,1608 @@
1/*
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@davemloft.net), 2002-Jul-29
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/spinlock.h>
21#include <linux/errno.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/circ_buf.h>
29#include <linux/serial.h>
30#include <linux/sysrq.h>
31#include <linux/console.h>
32#include <linux/slab.h>
33#ifdef CONFIG_SERIO
34#include <linux/serio.h>
35#endif
36#include <linux/serial_reg.h>
37#include <linux/init.h>
38#include <linux/delay.h>
39#include <linux/of_device.h>
40
41#include <asm/io.h>
42#include <asm/irq.h>
43#include <asm/prom.h>
44
45#if defined(CONFIG_SERIAL_SUNSU_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
46#define SUPPORT_SYSRQ
47#endif
48
49#include <linux/serial_core.h>
50
51#include "suncore.h"
52
53/* We are on a NS PC87303 clocked with 24.0 MHz, which results
54 * in a UART clock of 1.8462 MHz.
55 */
56#define SU_BASE_BAUD (1846200 / 16)
57
58enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
59static char *su_typev[] = { "su(???)", "su(mouse)", "su(kbd)", "su(serial)" };
60
61/*
62 * Here we define the default xmit fifo size used for each type of UART.
63 */
64static const struct serial_uart_config uart_config[PORT_MAX_8250+1] = {
65 { "unknown", 1, 0 },
66 { "8250", 1, 0 },
67 { "16450", 1, 0 },
68 { "16550", 1, 0 },
69 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
70 { "Cirrus", 1, 0 },
71 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
72 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
73 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO },
74 { "Startech", 1, 0 },
75 { "16C950/954", 128, UART_CLEAR_FIFO | UART_USE_FIFO },
76 { "ST16654", 64, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
77 { "XR16850", 128, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
78 { "RSA", 2048, UART_CLEAR_FIFO | UART_USE_FIFO }
79};
80
81struct uart_sunsu_port {
82 struct uart_port port;
83 unsigned char acr;
84 unsigned char ier;
85 unsigned short rev;
86 unsigned char lcr;
87 unsigned int lsr_break_flag;
88 unsigned int cflag;
89
90 /* Probing information. */
91 enum su_type su_type;
92 unsigned int type_probed; /* XXX Stupid */
93 unsigned long reg_size;
94
95#ifdef CONFIG_SERIO
96 struct serio serio;
97 int serio_open;
98#endif
99};
100
101static unsigned int serial_in(struct uart_sunsu_port *up, int offset)
102{
103 offset <<= up->port.regshift;
104
105 switch (up->port.iotype) {
106 case UPIO_HUB6:
107 outb(up->port.hub6 - 1 + offset, up->port.iobase);
108 return inb(up->port.iobase + 1);
109
110 case UPIO_MEM:
111 return readb(up->port.membase + offset);
112
113 default:
114 return inb(up->port.iobase + offset);
115 }
116}
117
118static void serial_out(struct uart_sunsu_port *up, int offset, int value)
119{
120#ifndef CONFIG_SPARC64
121 /*
122 * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
123 * connected with a gate then go to SlavIO. When IRQ4 goes tristated
124 * gate outputs a logical one. Since we use level triggered interrupts
125 * we have lockup and watchdog reset. We cannot mask IRQ because
126 * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
127 * This problem is similar to what Alpha people suffer, see serial.c.
128 */
129 if (offset == UART_MCR)
130 value |= UART_MCR_OUT2;
131#endif
132 offset <<= up->port.regshift;
133
134 switch (up->port.iotype) {
135 case UPIO_HUB6:
136 outb(up->port.hub6 - 1 + offset, up->port.iobase);
137 outb(value, up->port.iobase + 1);
138 break;
139
140 case UPIO_MEM:
141 writeb(value, up->port.membase + offset);
142 break;
143
144 default:
145 outb(value, up->port.iobase + offset);
146 }
147}
148
149/*
150 * We used to support using pause I/O for certain machines. We
151 * haven't supported this for a while, but just in case it's badly
152 * needed for certain old 386 machines, I've left these #define's
153 * in....
154 */
155#define serial_inp(up, offset) serial_in(up, offset)
156#define serial_outp(up, offset, value) serial_out(up, offset, value)
157
158
159/*
160 * For the 16C950
161 */
162static void serial_icr_write(struct uart_sunsu_port *up, int offset, int value)
163{
164 serial_out(up, UART_SCR, offset);
165 serial_out(up, UART_ICR, value);
166}
167
168#if 0 /* Unused currently */
169static unsigned int serial_icr_read(struct uart_sunsu_port *up, int offset)
170{
171 unsigned int value;
172
173 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
174 serial_out(up, UART_SCR, offset);
175 value = serial_in(up, UART_ICR);
176 serial_icr_write(up, UART_ACR, up->acr);
177
178 return value;
179}
180#endif
181
182#ifdef CONFIG_SERIAL_8250_RSA
183/*
184 * Attempts to turn on the RSA FIFO. Returns zero on failure.
185 * We set the port uart clock rate if we succeed.
186 */
187static int __enable_rsa(struct uart_sunsu_port *up)
188{
189 unsigned char mode;
190 int result;
191
192 mode = serial_inp(up, UART_RSA_MSR);
193 result = mode & UART_RSA_MSR_FIFO;
194
195 if (!result) {
196 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
197 mode = serial_inp(up, UART_RSA_MSR);
198 result = mode & UART_RSA_MSR_FIFO;
199 }
200
201 if (result)
202 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
203
204 return result;
205}
206
207static void enable_rsa(struct uart_sunsu_port *up)
208{
209 if (up->port.type == PORT_RSA) {
210 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
211 spin_lock_irq(&up->port.lock);
212 __enable_rsa(up);
213 spin_unlock_irq(&up->port.lock);
214 }
215 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
216 serial_outp(up, UART_RSA_FRR, 0);
217 }
218}
219
220/*
221 * Attempts to turn off the RSA FIFO. Returns zero on failure.
222 * It is unknown why interrupts were disabled in here. However,
223 * the caller is expected to preserve this behaviour by grabbing
224 * the spinlock before calling this function.
225 */
226static void disable_rsa(struct uart_sunsu_port *up)
227{
228 unsigned char mode;
229 int result;
230
231 if (up->port.type == PORT_RSA &&
232 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
233 spin_lock_irq(&up->port.lock);
234
235 mode = serial_inp(up, UART_RSA_MSR);
236 result = !(mode & UART_RSA_MSR_FIFO);
237
238 if (!result) {
239 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
240 mode = serial_inp(up, UART_RSA_MSR);
241 result = !(mode & UART_RSA_MSR_FIFO);
242 }
243
244 if (result)
245 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
246 spin_unlock_irq(&up->port.lock);
247 }
248}
249#endif /* CONFIG_SERIAL_8250_RSA */
250
251static inline void __stop_tx(struct uart_sunsu_port *p)
252{
253 if (p->ier & UART_IER_THRI) {
254 p->ier &= ~UART_IER_THRI;
255 serial_out(p, UART_IER, p->ier);
256 }
257}
258
259static void sunsu_stop_tx(struct uart_port *port)
260{
261 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
262
263 __stop_tx(up);
264
265 /*
266 * We really want to stop the transmitter from sending.
267 */
268 if (up->port.type == PORT_16C950) {
269 up->acr |= UART_ACR_TXDIS;
270 serial_icr_write(up, UART_ACR, up->acr);
271 }
272}
273
274static void sunsu_start_tx(struct uart_port *port)
275{
276 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
277
278 if (!(up->ier & UART_IER_THRI)) {
279 up->ier |= UART_IER_THRI;
280 serial_out(up, UART_IER, up->ier);
281 }
282
283 /*
284 * Re-enable the transmitter if we disabled it.
285 */
286 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
287 up->acr &= ~UART_ACR_TXDIS;
288 serial_icr_write(up, UART_ACR, up->acr);
289 }
290}
291
292static void sunsu_stop_rx(struct uart_port *port)
293{
294 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
295
296 up->ier &= ~UART_IER_RLSI;
297 up->port.read_status_mask &= ~UART_LSR_DR;
298 serial_out(up, UART_IER, up->ier);
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 struct tty_struct *
313receive_chars(struct uart_sunsu_port *up, unsigned char *status)
314{
315 struct tty_struct *tty = up->port.state->port.tty;
316 unsigned char ch, flag;
317 int max_count = 256;
318 int saw_console_brk = 0;
319
320 do {
321 ch = serial_inp(up, UART_RX);
322 flag = TTY_NORMAL;
323 up->port.icount.rx++;
324
325 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
326 UART_LSR_FE | UART_LSR_OE))) {
327 /*
328 * For statistics only
329 */
330 if (*status & UART_LSR_BI) {
331 *status &= ~(UART_LSR_FE | UART_LSR_PE);
332 up->port.icount.brk++;
333 if (up->port.cons != NULL &&
334 up->port.line == up->port.cons->index)
335 saw_console_brk = 1;
336 /*
337 * We do the SysRQ and SAK checking
338 * here because otherwise the break
339 * may get masked by ignore_status_mask
340 * or read_status_mask.
341 */
342 if (uart_handle_break(&up->port))
343 goto ignore_char;
344 } else if (*status & UART_LSR_PE)
345 up->port.icount.parity++;
346 else if (*status & UART_LSR_FE)
347 up->port.icount.frame++;
348 if (*status & UART_LSR_OE)
349 up->port.icount.overrun++;
350
351 /*
352 * Mask off conditions which should be ingored.
353 */
354 *status &= up->port.read_status_mask;
355
356 if (up->port.cons != NULL &&
357 up->port.line == up->port.cons->index) {
358 /* Recover the break flag from console xmit */
359 *status |= up->lsr_break_flag;
360 up->lsr_break_flag = 0;
361 }
362
363 if (*status & UART_LSR_BI) {
364 flag = TTY_BREAK;
365 } else if (*status & UART_LSR_PE)
366 flag = TTY_PARITY;
367 else if (*status & UART_LSR_FE)
368 flag = TTY_FRAME;
369 }
370 if (uart_handle_sysrq_char(&up->port, ch))
371 goto ignore_char;
372 if ((*status & up->port.ignore_status_mask) == 0)
373 tty_insert_flip_char(tty, ch, flag);
374 if (*status & UART_LSR_OE)
375 /*
376 * Overrun is special, since it's reported
377 * immediately, and doesn't affect the current
378 * character.
379 */
380 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
381 ignore_char:
382 *status = serial_inp(up, UART_LSR);
383 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
384
385 if (saw_console_brk)
386 sun_do_break();
387
388 return tty;
389}
390
391static void transmit_chars(struct uart_sunsu_port *up)
392{
393 struct circ_buf *xmit = &up->port.state->xmit;
394 int count;
395
396 if (up->port.x_char) {
397 serial_outp(up, UART_TX, up->port.x_char);
398 up->port.icount.tx++;
399 up->port.x_char = 0;
400 return;
401 }
402 if (uart_tx_stopped(&up->port)) {
403 sunsu_stop_tx(&up->port);
404 return;
405 }
406 if (uart_circ_empty(xmit)) {
407 __stop_tx(up);
408 return;
409 }
410
411 count = up->port.fifosize;
412 do {
413 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
414 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
415 up->port.icount.tx++;
416 if (uart_circ_empty(xmit))
417 break;
418 } while (--count > 0);
419
420 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
421 uart_write_wakeup(&up->port);
422
423 if (uart_circ_empty(xmit))
424 __stop_tx(up);
425}
426
427static void check_modem_status(struct uart_sunsu_port *up)
428{
429 int status;
430
431 status = serial_in(up, UART_MSR);
432
433 if ((status & UART_MSR_ANY_DELTA) == 0)
434 return;
435
436 if (status & UART_MSR_TERI)
437 up->port.icount.rng++;
438 if (status & UART_MSR_DDSR)
439 up->port.icount.dsr++;
440 if (status & UART_MSR_DDCD)
441 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
442 if (status & UART_MSR_DCTS)
443 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
444
445 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
446}
447
448static irqreturn_t sunsu_serial_interrupt(int irq, void *dev_id)
449{
450 struct uart_sunsu_port *up = dev_id;
451 unsigned long flags;
452 unsigned char status;
453
454 spin_lock_irqsave(&up->port.lock, flags);
455
456 do {
457 struct tty_struct *tty;
458
459 status = serial_inp(up, UART_LSR);
460 tty = NULL;
461 if (status & UART_LSR_DR)
462 tty = receive_chars(up, &status);
463 check_modem_status(up);
464 if (status & UART_LSR_THRE)
465 transmit_chars(up);
466
467 spin_unlock_irqrestore(&up->port.lock, flags);
468
469 if (tty)
470 tty_flip_buffer_push(tty);
471
472 spin_lock_irqsave(&up->port.lock, flags);
473
474 } while (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT));
475
476 spin_unlock_irqrestore(&up->port.lock, flags);
477
478 return IRQ_HANDLED;
479}
480
481/* Separate interrupt handling path for keyboard/mouse ports. */
482
483static void
484sunsu_change_speed(struct uart_port *port, unsigned int cflag,
485 unsigned int iflag, unsigned int quot);
486
487static void sunsu_change_mouse_baud(struct uart_sunsu_port *up)
488{
489 unsigned int cur_cflag = up->cflag;
490 int quot, new_baud;
491
492 up->cflag &= ~CBAUD;
493 up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
494
495 quot = up->port.uartclk / (16 * new_baud);
496
497 sunsu_change_speed(&up->port, up->cflag, 0, quot);
498}
499
500static void receive_kbd_ms_chars(struct uart_sunsu_port *up, int is_break)
501{
502 do {
503 unsigned char ch = serial_inp(up, UART_RX);
504
505 /* Stop-A is handled by drivers/char/keyboard.c now. */
506 if (up->su_type == SU_PORT_KBD) {
507#ifdef CONFIG_SERIO
508 serio_interrupt(&up->serio, ch, 0);
509#endif
510 } else if (up->su_type == SU_PORT_MS) {
511 int ret = suncore_mouse_baud_detection(ch, is_break);
512
513 switch (ret) {
514 case 2:
515 sunsu_change_mouse_baud(up);
516 /* fallthru */
517 case 1:
518 break;
519
520 case 0:
521#ifdef CONFIG_SERIO
522 serio_interrupt(&up->serio, ch, 0);
523#endif
524 break;
525 };
526 }
527 } while (serial_in(up, UART_LSR) & UART_LSR_DR);
528}
529
530static irqreturn_t sunsu_kbd_ms_interrupt(int irq, void *dev_id)
531{
532 struct uart_sunsu_port *up = dev_id;
533
534 if (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT)) {
535 unsigned char status = serial_inp(up, UART_LSR);
536
537 if ((status & UART_LSR_DR) || (status & UART_LSR_BI))
538 receive_kbd_ms_chars(up, (status & UART_LSR_BI) != 0);
539 }
540
541 return IRQ_HANDLED;
542}
543
544static unsigned int sunsu_tx_empty(struct uart_port *port)
545{
546 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
547 unsigned long flags;
548 unsigned int ret;
549
550 spin_lock_irqsave(&up->port.lock, flags);
551 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
552 spin_unlock_irqrestore(&up->port.lock, flags);
553
554 return ret;
555}
556
557static unsigned int sunsu_get_mctrl(struct uart_port *port)
558{
559 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
560 unsigned char status;
561 unsigned int ret;
562
563 status = serial_in(up, UART_MSR);
564
565 ret = 0;
566 if (status & UART_MSR_DCD)
567 ret |= TIOCM_CAR;
568 if (status & UART_MSR_RI)
569 ret |= TIOCM_RNG;
570 if (status & UART_MSR_DSR)
571 ret |= TIOCM_DSR;
572 if (status & UART_MSR_CTS)
573 ret |= TIOCM_CTS;
574 return ret;
575}
576
577static void sunsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
578{
579 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
580 unsigned char mcr = 0;
581
582 if (mctrl & TIOCM_RTS)
583 mcr |= UART_MCR_RTS;
584 if (mctrl & TIOCM_DTR)
585 mcr |= UART_MCR_DTR;
586 if (mctrl & TIOCM_OUT1)
587 mcr |= UART_MCR_OUT1;
588 if (mctrl & TIOCM_OUT2)
589 mcr |= UART_MCR_OUT2;
590 if (mctrl & TIOCM_LOOP)
591 mcr |= UART_MCR_LOOP;
592
593 serial_out(up, UART_MCR, mcr);
594}
595
596static void sunsu_break_ctl(struct uart_port *port, int break_state)
597{
598 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
599 unsigned long flags;
600
601 spin_lock_irqsave(&up->port.lock, flags);
602 if (break_state == -1)
603 up->lcr |= UART_LCR_SBC;
604 else
605 up->lcr &= ~UART_LCR_SBC;
606 serial_out(up, UART_LCR, up->lcr);
607 spin_unlock_irqrestore(&up->port.lock, flags);
608}
609
610static int sunsu_startup(struct uart_port *port)
611{
612 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
613 unsigned long flags;
614 int retval;
615
616 if (up->port.type == PORT_16C950) {
617 /* Wake up and initialize UART */
618 up->acr = 0;
619 serial_outp(up, UART_LCR, 0xBF);
620 serial_outp(up, UART_EFR, UART_EFR_ECB);
621 serial_outp(up, UART_IER, 0);
622 serial_outp(up, UART_LCR, 0);
623 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
624 serial_outp(up, UART_LCR, 0xBF);
625 serial_outp(up, UART_EFR, UART_EFR_ECB);
626 serial_outp(up, UART_LCR, 0);
627 }
628
629#ifdef CONFIG_SERIAL_8250_RSA
630 /*
631 * If this is an RSA port, see if we can kick it up to the
632 * higher speed clock.
633 */
634 enable_rsa(up);
635#endif
636
637 /*
638 * Clear the FIFO buffers and disable them.
639 * (they will be reenabled in set_termios())
640 */
641 if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
642 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
643 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
644 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
645 serial_outp(up, UART_FCR, 0);
646 }
647
648 /*
649 * Clear the interrupt registers.
650 */
651 (void) serial_inp(up, UART_LSR);
652 (void) serial_inp(up, UART_RX);
653 (void) serial_inp(up, UART_IIR);
654 (void) serial_inp(up, UART_MSR);
655
656 /*
657 * At this point, there's no way the LSR could still be 0xff;
658 * if it is, then bail out, because there's likely no UART
659 * here.
660 */
661 if (!(up->port.flags & UPF_BUGGY_UART) &&
662 (serial_inp(up, UART_LSR) == 0xff)) {
663 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
664 return -ENODEV;
665 }
666
667 if (up->su_type != SU_PORT_PORT) {
668 retval = request_irq(up->port.irq, sunsu_kbd_ms_interrupt,
669 IRQF_SHARED, su_typev[up->su_type], up);
670 } else {
671 retval = request_irq(up->port.irq, sunsu_serial_interrupt,
672 IRQF_SHARED, su_typev[up->su_type], up);
673 }
674 if (retval) {
675 printk("su: Cannot register IRQ %d\n", up->port.irq);
676 return retval;
677 }
678
679 /*
680 * Now, initialize the UART
681 */
682 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
683
684 spin_lock_irqsave(&up->port.lock, flags);
685
686 up->port.mctrl |= TIOCM_OUT2;
687
688 sunsu_set_mctrl(&up->port, up->port.mctrl);
689 spin_unlock_irqrestore(&up->port.lock, flags);
690
691 /*
692 * Finally, enable interrupts. Note: Modem status interrupts
693 * are set via set_termios(), which will be occurring imminently
694 * anyway, so we don't enable them here.
695 */
696 up->ier = UART_IER_RLSI | UART_IER_RDI;
697 serial_outp(up, UART_IER, up->ier);
698
699 if (up->port.flags & UPF_FOURPORT) {
700 unsigned int icp;
701 /*
702 * Enable interrupts on the AST Fourport board
703 */
704 icp = (up->port.iobase & 0xfe0) | 0x01f;
705 outb_p(0x80, icp);
706 (void) inb_p(icp);
707 }
708
709 /*
710 * And clear the interrupt registers again for luck.
711 */
712 (void) serial_inp(up, UART_LSR);
713 (void) serial_inp(up, UART_RX);
714 (void) serial_inp(up, UART_IIR);
715 (void) serial_inp(up, UART_MSR);
716
717 return 0;
718}
719
720static void sunsu_shutdown(struct uart_port *port)
721{
722 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
723 unsigned long flags;
724
725 /*
726 * Disable interrupts from this port
727 */
728 up->ier = 0;
729 serial_outp(up, UART_IER, 0);
730
731 spin_lock_irqsave(&up->port.lock, flags);
732 if (up->port.flags & UPF_FOURPORT) {
733 /* reset interrupts on the AST Fourport board */
734 inb((up->port.iobase & 0xfe0) | 0x1f);
735 up->port.mctrl |= TIOCM_OUT1;
736 } else
737 up->port.mctrl &= ~TIOCM_OUT2;
738
739 sunsu_set_mctrl(&up->port, up->port.mctrl);
740 spin_unlock_irqrestore(&up->port.lock, flags);
741
742 /*
743 * Disable break condition and FIFOs
744 */
745 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
746 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
747 UART_FCR_CLEAR_RCVR |
748 UART_FCR_CLEAR_XMIT);
749 serial_outp(up, UART_FCR, 0);
750
751#ifdef CONFIG_SERIAL_8250_RSA
752 /*
753 * Reset the RSA board back to 115kbps compat mode.
754 */
755 disable_rsa(up);
756#endif
757
758 /*
759 * Read data port to reset things.
760 */
761 (void) serial_in(up, UART_RX);
762
763 free_irq(up->port.irq, up);
764}
765
766static void
767sunsu_change_speed(struct uart_port *port, unsigned int cflag,
768 unsigned int iflag, unsigned int quot)
769{
770 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
771 unsigned char cval, fcr = 0;
772 unsigned long flags;
773
774 switch (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 (cflag & CSTOPB)
791 cval |= 0x04;
792 if (cflag & PARENB)
793 cval |= UART_LCR_PARITY;
794 if (!(cflag & PARODD))
795 cval |= UART_LCR_EPAR;
796#ifdef CMSPAR
797 if (cflag & CMSPAR)
798 cval |= UART_LCR_SPAR;
799#endif
800
801 /*
802 * Work around a bug in the Oxford Semiconductor 952 rev B
803 * chip which causes it to seriously miscalculate baud rates
804 * when DLL is 0.
805 */
806 if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
807 up->rev == 0x5201)
808 quot ++;
809
810 if (uart_config[up->port.type].flags & UART_USE_FIFO) {
811 if ((up->port.uartclk / quot) < (2400 * 16))
812 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
813#ifdef CONFIG_SERIAL_8250_RSA
814 else if (up->port.type == PORT_RSA)
815 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_14;
816#endif
817 else
818 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
819 }
820 if (up->port.type == PORT_16750)
821 fcr |= UART_FCR7_64BYTE;
822
823 /*
824 * Ok, we're now changing the port state. Do it with
825 * interrupts disabled.
826 */
827 spin_lock_irqsave(&up->port.lock, flags);
828
829 /*
830 * Update the per-port timeout.
831 */
832 uart_update_timeout(port, cflag, (port->uartclk / (16 * quot)));
833
834 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
835 if (iflag & INPCK)
836 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
837 if (iflag & (BRKINT | PARMRK))
838 up->port.read_status_mask |= UART_LSR_BI;
839
840 /*
841 * Characteres to ignore
842 */
843 up->port.ignore_status_mask = 0;
844 if (iflag & IGNPAR)
845 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
846 if (iflag & IGNBRK) {
847 up->port.ignore_status_mask |= UART_LSR_BI;
848 /*
849 * If we're ignoring parity and break indicators,
850 * ignore overruns too (for real raw support).
851 */
852 if (iflag & IGNPAR)
853 up->port.ignore_status_mask |= UART_LSR_OE;
854 }
855
856 /*
857 * ignore all characters if CREAD is not set
858 */
859 if ((cflag & CREAD) == 0)
860 up->port.ignore_status_mask |= UART_LSR_DR;
861
862 /*
863 * CTS flow control flag and modem status interrupts
864 */
865 up->ier &= ~UART_IER_MSI;
866 if (UART_ENABLE_MS(&up->port, cflag))
867 up->ier |= UART_IER_MSI;
868
869 serial_out(up, UART_IER, up->ier);
870
871 if (uart_config[up->port.type].flags & UART_STARTECH) {
872 serial_outp(up, UART_LCR, 0xBF);
873 serial_outp(up, UART_EFR, cflag & CRTSCTS ? UART_EFR_CTS :0);
874 }
875 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
876 serial_outp(up, UART_DLL, quot & 0xff); /* LS of divisor */
877 serial_outp(up, UART_DLM, quot >> 8); /* MS of divisor */
878 if (up->port.type == PORT_16750)
879 serial_outp(up, UART_FCR, fcr); /* set fcr */
880 serial_outp(up, UART_LCR, cval); /* reset DLAB */
881 up->lcr = cval; /* Save LCR */
882 if (up->port.type != PORT_16750) {
883 if (fcr & UART_FCR_ENABLE_FIFO) {
884 /* emulated UARTs (Lucent Venus 167x) need two steps */
885 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
886 }
887 serial_outp(up, UART_FCR, fcr); /* set fcr */
888 }
889
890 up->cflag = cflag;
891
892 spin_unlock_irqrestore(&up->port.lock, flags);
893}
894
895static void
896sunsu_set_termios(struct uart_port *port, struct ktermios *termios,
897 struct ktermios *old)
898{
899 unsigned int baud, quot;
900
901 /*
902 * Ask the core to calculate the divisor for us.
903 */
904 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
905 quot = uart_get_divisor(port, baud);
906
907 sunsu_change_speed(port, termios->c_cflag, termios->c_iflag, quot);
908}
909
910static void sunsu_release_port(struct uart_port *port)
911{
912}
913
914static int sunsu_request_port(struct uart_port *port)
915{
916 return 0;
917}
918
919static void sunsu_config_port(struct uart_port *port, int flags)
920{
921 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
922
923 if (flags & UART_CONFIG_TYPE) {
924 /*
925 * We are supposed to call autoconfig here, but this requires
926 * splitting all the OBP probing crap from the UART probing.
927 * We'll do it when we kill sunsu.c altogether.
928 */
929 port->type = up->type_probed; /* XXX */
930 }
931}
932
933static int
934sunsu_verify_port(struct uart_port *port, struct serial_struct *ser)
935{
936 return -EINVAL;
937}
938
939static const char *
940sunsu_type(struct uart_port *port)
941{
942 int type = port->type;
943
944 if (type >= ARRAY_SIZE(uart_config))
945 type = 0;
946 return uart_config[type].name;
947}
948
949static struct uart_ops sunsu_pops = {
950 .tx_empty = sunsu_tx_empty,
951 .set_mctrl = sunsu_set_mctrl,
952 .get_mctrl = sunsu_get_mctrl,
953 .stop_tx = sunsu_stop_tx,
954 .start_tx = sunsu_start_tx,
955 .stop_rx = sunsu_stop_rx,
956 .enable_ms = sunsu_enable_ms,
957 .break_ctl = sunsu_break_ctl,
958 .startup = sunsu_startup,
959 .shutdown = sunsu_shutdown,
960 .set_termios = sunsu_set_termios,
961 .type = sunsu_type,
962 .release_port = sunsu_release_port,
963 .request_port = sunsu_request_port,
964 .config_port = sunsu_config_port,
965 .verify_port = sunsu_verify_port,
966};
967
968#define UART_NR 4
969
970static struct uart_sunsu_port sunsu_ports[UART_NR];
971
972#ifdef CONFIG_SERIO
973
974static DEFINE_SPINLOCK(sunsu_serio_lock);
975
976static int sunsu_serio_write(struct serio *serio, unsigned char ch)
977{
978 struct uart_sunsu_port *up = serio->port_data;
979 unsigned long flags;
980 int lsr;
981
982 spin_lock_irqsave(&sunsu_serio_lock, flags);
983
984 do {
985 lsr = serial_in(up, UART_LSR);
986 } while (!(lsr & UART_LSR_THRE));
987
988 /* Send the character out. */
989 serial_out(up, UART_TX, ch);
990
991 spin_unlock_irqrestore(&sunsu_serio_lock, flags);
992
993 return 0;
994}
995
996static int sunsu_serio_open(struct serio *serio)
997{
998 struct uart_sunsu_port *up = serio->port_data;
999 unsigned long flags;
1000 int ret;
1001
1002 spin_lock_irqsave(&sunsu_serio_lock, flags);
1003 if (!up->serio_open) {
1004 up->serio_open = 1;
1005 ret = 0;
1006 } else
1007 ret = -EBUSY;
1008 spin_unlock_irqrestore(&sunsu_serio_lock, flags);
1009
1010 return ret;
1011}
1012
1013static void sunsu_serio_close(struct serio *serio)
1014{
1015 struct uart_sunsu_port *up = serio->port_data;
1016 unsigned long flags;
1017
1018 spin_lock_irqsave(&sunsu_serio_lock, flags);
1019 up->serio_open = 0;
1020 spin_unlock_irqrestore(&sunsu_serio_lock, flags);
1021}
1022
1023#endif /* CONFIG_SERIO */
1024
1025static void sunsu_autoconfig(struct uart_sunsu_port *up)
1026{
1027 unsigned char status1, status2, scratch, scratch2, scratch3;
1028 unsigned char save_lcr, save_mcr;
1029 unsigned long flags;
1030
1031 if (up->su_type == SU_PORT_NONE)
1032 return;
1033
1034 up->type_probed = PORT_UNKNOWN;
1035 up->port.iotype = UPIO_MEM;
1036
1037 spin_lock_irqsave(&up->port.lock, flags);
1038
1039 if (!(up->port.flags & UPF_BUGGY_UART)) {
1040 /*
1041 * Do a simple existence test first; if we fail this, there's
1042 * no point trying anything else.
1043 *
1044 * 0x80 is used as a nonsense port to prevent against false
1045 * positives due to ISA bus float. The assumption is that
1046 * 0x80 is a non-existent port; which should be safe since
1047 * include/asm/io.h also makes this assumption.
1048 */
1049 scratch = serial_inp(up, UART_IER);
1050 serial_outp(up, UART_IER, 0);
1051#ifdef __i386__
1052 outb(0xff, 0x080);
1053#endif
1054 scratch2 = serial_inp(up, UART_IER);
1055 serial_outp(up, UART_IER, 0x0f);
1056#ifdef __i386__
1057 outb(0, 0x080);
1058#endif
1059 scratch3 = serial_inp(up, UART_IER);
1060 serial_outp(up, UART_IER, scratch);
1061 if (scratch2 != 0 || scratch3 != 0x0F)
1062 goto out; /* We failed; there's nothing here */
1063 }
1064
1065 save_mcr = serial_in(up, UART_MCR);
1066 save_lcr = serial_in(up, UART_LCR);
1067
1068 /*
1069 * Check to see if a UART is really there. Certain broken
1070 * internal modems based on the Rockwell chipset fail this
1071 * test, because they apparently don't implement the loopback
1072 * test mode. So this test is skipped on the COM 1 through
1073 * COM 4 ports. This *should* be safe, since no board
1074 * manufacturer would be stupid enough to design a board
1075 * that conflicts with COM 1-4 --- we hope!
1076 */
1077 if (!(up->port.flags & UPF_SKIP_TEST)) {
1078 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1079 status1 = serial_inp(up, UART_MSR) & 0xF0;
1080 serial_outp(up, UART_MCR, save_mcr);
1081 if (status1 != 0x90)
1082 goto out; /* We failed loopback test */
1083 }
1084 serial_outp(up, UART_LCR, 0xBF); /* set up for StarTech test */
1085 serial_outp(up, UART_EFR, 0); /* EFR is the same as FCR */
1086 serial_outp(up, UART_LCR, 0);
1087 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1088 scratch = serial_in(up, UART_IIR) >> 6;
1089 switch (scratch) {
1090 case 0:
1091 up->port.type = PORT_16450;
1092 break;
1093 case 1:
1094 up->port.type = PORT_UNKNOWN;
1095 break;
1096 case 2:
1097 up->port.type = PORT_16550;
1098 break;
1099 case 3:
1100 up->port.type = PORT_16550A;
1101 break;
1102 }
1103 if (up->port.type == PORT_16550A) {
1104 /* Check for Startech UART's */
1105 serial_outp(up, UART_LCR, UART_LCR_DLAB);
1106 if (serial_in(up, UART_EFR) == 0) {
1107 up->port.type = PORT_16650;
1108 } else {
1109 serial_outp(up, UART_LCR, 0xBF);
1110 if (serial_in(up, UART_EFR) == 0)
1111 up->port.type = PORT_16650V2;
1112 }
1113 }
1114 if (up->port.type == PORT_16550A) {
1115 /* Check for TI 16750 */
1116 serial_outp(up, UART_LCR, save_lcr | UART_LCR_DLAB);
1117 serial_outp(up, UART_FCR,
1118 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1119 scratch = serial_in(up, UART_IIR) >> 5;
1120 if (scratch == 7) {
1121 /*
1122 * If this is a 16750, and not a cheap UART
1123 * clone, then it should only go into 64 byte
1124 * mode if the UART_FCR7_64BYTE bit was set
1125 * while UART_LCR_DLAB was latched.
1126 */
1127 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1128 serial_outp(up, UART_LCR, 0);
1129 serial_outp(up, UART_FCR,
1130 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1131 scratch = serial_in(up, UART_IIR) >> 5;
1132 if (scratch == 6)
1133 up->port.type = PORT_16750;
1134 }
1135 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1136 }
1137 serial_outp(up, UART_LCR, save_lcr);
1138 if (up->port.type == PORT_16450) {
1139 scratch = serial_in(up, UART_SCR);
1140 serial_outp(up, UART_SCR, 0xa5);
1141 status1 = serial_in(up, UART_SCR);
1142 serial_outp(up, UART_SCR, 0x5a);
1143 status2 = serial_in(up, UART_SCR);
1144 serial_outp(up, UART_SCR, scratch);
1145
1146 if ((status1 != 0xa5) || (status2 != 0x5a))
1147 up->port.type = PORT_8250;
1148 }
1149
1150 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
1151
1152 if (up->port.type == PORT_UNKNOWN)
1153 goto out;
1154 up->type_probed = up->port.type; /* XXX */
1155
1156 /*
1157 * Reset the UART.
1158 */
1159#ifdef CONFIG_SERIAL_8250_RSA
1160 if (up->port.type == PORT_RSA)
1161 serial_outp(up, UART_RSA_FRR, 0);
1162#endif
1163 serial_outp(up, UART_MCR, save_mcr);
1164 serial_outp(up, UART_FCR, (UART_FCR_ENABLE_FIFO |
1165 UART_FCR_CLEAR_RCVR |
1166 UART_FCR_CLEAR_XMIT));
1167 serial_outp(up, UART_FCR, 0);
1168 (void)serial_in(up, UART_RX);
1169 serial_outp(up, UART_IER, 0);
1170
1171out:
1172 spin_unlock_irqrestore(&up->port.lock, flags);
1173}
1174
1175static struct uart_driver sunsu_reg = {
1176 .owner = THIS_MODULE,
1177 .driver_name = "sunsu",
1178 .dev_name = "ttyS",
1179 .major = TTY_MAJOR,
1180};
1181
1182static int __devinit sunsu_kbd_ms_init(struct uart_sunsu_port *up)
1183{
1184 int quot, baud;
1185#ifdef CONFIG_SERIO
1186 struct serio *serio;
1187#endif
1188
1189 if (up->su_type == SU_PORT_KBD) {
1190 up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1191 baud = 1200;
1192 } else {
1193 up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1194 baud = 4800;
1195 }
1196 quot = up->port.uartclk / (16 * baud);
1197
1198 sunsu_autoconfig(up);
1199 if (up->port.type == PORT_UNKNOWN)
1200 return -ENODEV;
1201
1202 printk("%s: %s port at %llx, irq %u\n",
1203 up->port.dev->of_node->full_name,
1204 (up->su_type == SU_PORT_KBD) ? "Keyboard" : "Mouse",
1205 (unsigned long long) up->port.mapbase,
1206 up->port.irq);
1207
1208#ifdef CONFIG_SERIO
1209 serio = &up->serio;
1210 serio->port_data = up;
1211
1212 serio->id.type = SERIO_RS232;
1213 if (up->su_type == SU_PORT_KBD) {
1214 serio->id.proto = SERIO_SUNKBD;
1215 strlcpy(serio->name, "sukbd", sizeof(serio->name));
1216 } else {
1217 serio->id.proto = SERIO_SUN;
1218 serio->id.extra = 1;
1219 strlcpy(serio->name, "sums", sizeof(serio->name));
1220 }
1221 strlcpy(serio->phys,
1222 (!(up->port.line & 1) ? "su/serio0" : "su/serio1"),
1223 sizeof(serio->phys));
1224
1225 serio->write = sunsu_serio_write;
1226 serio->open = sunsu_serio_open;
1227 serio->close = sunsu_serio_close;
1228 serio->dev.parent = up->port.dev;
1229
1230 serio_register_port(serio);
1231#endif
1232
1233 sunsu_change_speed(&up->port, up->cflag, 0, quot);
1234
1235 sunsu_startup(&up->port);
1236 return 0;
1237}
1238
1239/*
1240 * ------------------------------------------------------------
1241 * Serial console driver
1242 * ------------------------------------------------------------
1243 */
1244
1245#ifdef CONFIG_SERIAL_SUNSU_CONSOLE
1246
1247#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1248
1249/*
1250 * Wait for transmitter & holding register to empty
1251 */
1252static __inline__ void wait_for_xmitr(struct uart_sunsu_port *up)
1253{
1254 unsigned int status, tmout = 10000;
1255
1256 /* Wait up to 10ms for the character(s) to be sent. */
1257 do {
1258 status = serial_in(up, UART_LSR);
1259
1260 if (status & UART_LSR_BI)
1261 up->lsr_break_flag = UART_LSR_BI;
1262
1263 if (--tmout == 0)
1264 break;
1265 udelay(1);
1266 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1267
1268 /* Wait up to 1s for flow control if necessary */
1269 if (up->port.flags & UPF_CONS_FLOW) {
1270 tmout = 1000000;
1271 while (--tmout &&
1272 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1273 udelay(1);
1274 }
1275}
1276
1277static void sunsu_console_putchar(struct uart_port *port, int ch)
1278{
1279 struct uart_sunsu_port *up = (struct uart_sunsu_port *)port;
1280
1281 wait_for_xmitr(up);
1282 serial_out(up, UART_TX, ch);
1283}
1284
1285/*
1286 * Print a string to the serial port trying not to disturb
1287 * any possible real use of the port...
1288 */
1289static void sunsu_console_write(struct console *co, const char *s,
1290 unsigned int count)
1291{
1292 struct uart_sunsu_port *up = &sunsu_ports[co->index];
1293 unsigned long flags;
1294 unsigned int ier;
1295 int locked = 1;
1296
1297 local_irq_save(flags);
1298 if (up->port.sysrq) {
1299 locked = 0;
1300 } else if (oops_in_progress) {
1301 locked = spin_trylock(&up->port.lock);
1302 } else
1303 spin_lock(&up->port.lock);
1304
1305 /*
1306 * First save the UER then disable the interrupts
1307 */
1308 ier = serial_in(up, UART_IER);
1309 serial_out(up, UART_IER, 0);
1310
1311 uart_console_write(&up->port, s, count, sunsu_console_putchar);
1312
1313 /*
1314 * Finally, wait for transmitter to become empty
1315 * and restore the IER
1316 */
1317 wait_for_xmitr(up);
1318 serial_out(up, UART_IER, ier);
1319
1320 if (locked)
1321 spin_unlock(&up->port.lock);
1322 local_irq_restore(flags);
1323}
1324
1325/*
1326 * Setup initial baud/bits/parity. We do two things here:
1327 * - construct a cflag setting for the first su_open()
1328 * - initialize the serial port
1329 * Return non-zero if we didn't find a serial port.
1330 */
1331static int __init sunsu_console_setup(struct console *co, char *options)
1332{
1333 static struct ktermios dummy;
1334 struct ktermios termios;
1335 struct uart_port *port;
1336
1337 printk("Console: ttyS%d (SU)\n",
1338 (sunsu_reg.minor - 64) + co->index);
1339
1340 /*
1341 * Check whether an invalid uart number has been specified, and
1342 * if so, search for the first available port that does have
1343 * console support.
1344 */
1345 if (co->index >= UART_NR)
1346 co->index = 0;
1347 port = &sunsu_ports[co->index].port;
1348
1349 /*
1350 * Temporary fix.
1351 */
1352 spin_lock_init(&port->lock);
1353
1354 /* Get firmware console settings. */
1355 sunserial_console_termios(co, port->dev->of_node);
1356
1357 memset(&termios, 0, sizeof(struct ktermios));
1358 termios.c_cflag = co->cflag;
1359 port->mctrl |= TIOCM_DTR;
1360 port->ops->set_termios(port, &termios, &dummy);
1361
1362 return 0;
1363}
1364
1365static struct console sunsu_console = {
1366 .name = "ttyS",
1367 .write = sunsu_console_write,
1368 .device = uart_console_device,
1369 .setup = sunsu_console_setup,
1370 .flags = CON_PRINTBUFFER,
1371 .index = -1,
1372 .data = &sunsu_reg,
1373};
1374
1375/*
1376 * Register console.
1377 */
1378
1379static inline struct console *SUNSU_CONSOLE(void)
1380{
1381 return &sunsu_console;
1382}
1383#else
1384#define SUNSU_CONSOLE() (NULL)
1385#define sunsu_serial_console_init() do { } while (0)
1386#endif
1387
1388static enum su_type __devinit su_get_type(struct device_node *dp)
1389{
1390 struct device_node *ap = of_find_node_by_path("/aliases");
1391
1392 if (ap) {
1393 const char *keyb = of_get_property(ap, "keyboard", NULL);
1394 const char *ms = of_get_property(ap, "mouse", NULL);
1395
1396 if (keyb) {
1397 if (dp == of_find_node_by_path(keyb))
1398 return SU_PORT_KBD;
1399 }
1400 if (ms) {
1401 if (dp == of_find_node_by_path(ms))
1402 return SU_PORT_MS;
1403 }
1404 }
1405
1406 return SU_PORT_PORT;
1407}
1408
1409static int __devinit su_probe(struct platform_device *op, const struct of_device_id *match)
1410{
1411 static int inst;
1412 struct device_node *dp = op->dev.of_node;
1413 struct uart_sunsu_port *up;
1414 struct resource *rp;
1415 enum su_type type;
1416 bool ignore_line;
1417 int err;
1418
1419 type = su_get_type(dp);
1420 if (type == SU_PORT_PORT) {
1421 if (inst >= UART_NR)
1422 return -EINVAL;
1423 up = &sunsu_ports[inst];
1424 } else {
1425 up = kzalloc(sizeof(*up), GFP_KERNEL);
1426 if (!up)
1427 return -ENOMEM;
1428 }
1429
1430 up->port.line = inst;
1431
1432 spin_lock_init(&up->port.lock);
1433
1434 up->su_type = type;
1435
1436 rp = &op->resource[0];
1437 up->port.mapbase = rp->start;
1438 up->reg_size = (rp->end - rp->start) + 1;
1439 up->port.membase = of_ioremap(rp, 0, up->reg_size, "su");
1440 if (!up->port.membase) {
1441 if (type != SU_PORT_PORT)
1442 kfree(up);
1443 return -ENOMEM;
1444 }
1445
1446 up->port.irq = op->archdata.irqs[0];
1447
1448 up->port.dev = &op->dev;
1449
1450 up->port.type = PORT_UNKNOWN;
1451 up->port.uartclk = (SU_BASE_BAUD * 16);
1452
1453 err = 0;
1454 if (up->su_type == SU_PORT_KBD || up->su_type == SU_PORT_MS) {
1455 err = sunsu_kbd_ms_init(up);
1456 if (err) {
1457 of_iounmap(&op->resource[0],
1458 up->port.membase, up->reg_size);
1459 kfree(up);
1460 return err;
1461 }
1462 dev_set_drvdata(&op->dev, up);
1463
1464 return 0;
1465 }
1466
1467 up->port.flags |= UPF_BOOT_AUTOCONF;
1468
1469 sunsu_autoconfig(up);
1470
1471 err = -ENODEV;
1472 if (up->port.type == PORT_UNKNOWN)
1473 goto out_unmap;
1474
1475 up->port.ops = &sunsu_pops;
1476
1477 ignore_line = false;
1478 if (!strcmp(dp->name, "rsc-console") ||
1479 !strcmp(dp->name, "lom-console"))
1480 ignore_line = true;
1481
1482 sunserial_console_match(SUNSU_CONSOLE(), dp,
1483 &sunsu_reg, up->port.line,
1484 ignore_line);
1485 err = uart_add_one_port(&sunsu_reg, &up->port);
1486 if (err)
1487 goto out_unmap;
1488
1489 dev_set_drvdata(&op->dev, up);
1490
1491 inst++;
1492
1493 return 0;
1494
1495out_unmap:
1496 of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
1497 return err;
1498}
1499
1500static int __devexit su_remove(struct platform_device *op)
1501{
1502 struct uart_sunsu_port *up = dev_get_drvdata(&op->dev);
1503 bool kbdms = false;
1504
1505 if (up->su_type == SU_PORT_MS ||
1506 up->su_type == SU_PORT_KBD)
1507 kbdms = true;
1508
1509 if (kbdms) {
1510#ifdef CONFIG_SERIO
1511 serio_unregister_port(&up->serio);
1512#endif
1513 } else if (up->port.type != PORT_UNKNOWN)
1514 uart_remove_one_port(&sunsu_reg, &up->port);
1515
1516 if (up->port.membase)
1517 of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
1518
1519 if (kbdms)
1520 kfree(up);
1521
1522 dev_set_drvdata(&op->dev, NULL);
1523
1524 return 0;
1525}
1526
1527static const struct of_device_id su_match[] = {
1528 {
1529 .name = "su",
1530 },
1531 {
1532 .name = "su_pnp",
1533 },
1534 {
1535 .name = "serial",
1536 .compatible = "su",
1537 },
1538 {
1539 .type = "serial",
1540 .compatible = "su",
1541 },
1542 {},
1543};
1544MODULE_DEVICE_TABLE(of, su_match);
1545
1546static struct of_platform_driver su_driver = {
1547 .driver = {
1548 .name = "su",
1549 .owner = THIS_MODULE,
1550 .of_match_table = su_match,
1551 },
1552 .probe = su_probe,
1553 .remove = __devexit_p(su_remove),
1554};
1555
1556static int __init sunsu_init(void)
1557{
1558 struct device_node *dp;
1559 int err;
1560 int num_uart = 0;
1561
1562 for_each_node_by_name(dp, "su") {
1563 if (su_get_type(dp) == SU_PORT_PORT)
1564 num_uart++;
1565 }
1566 for_each_node_by_name(dp, "su_pnp") {
1567 if (su_get_type(dp) == SU_PORT_PORT)
1568 num_uart++;
1569 }
1570 for_each_node_by_name(dp, "serial") {
1571 if (of_device_is_compatible(dp, "su")) {
1572 if (su_get_type(dp) == SU_PORT_PORT)
1573 num_uart++;
1574 }
1575 }
1576 for_each_node_by_type(dp, "serial") {
1577 if (of_device_is_compatible(dp, "su")) {
1578 if (su_get_type(dp) == SU_PORT_PORT)
1579 num_uart++;
1580 }
1581 }
1582
1583 if (num_uart) {
1584 err = sunserial_register_minors(&sunsu_reg, num_uart);
1585 if (err)
1586 return err;
1587 }
1588
1589 err = of_register_platform_driver(&su_driver);
1590 if (err && num_uart)
1591 sunserial_unregister_minors(&sunsu_reg, num_uart);
1592
1593 return err;
1594}
1595
1596static void __exit sunsu_exit(void)
1597{
1598 if (sunsu_reg.nr)
1599 sunserial_unregister_minors(&sunsu_reg, sunsu_reg.nr);
1600}
1601
1602module_init(sunsu_init);
1603module_exit(sunsu_exit);
1604
1605MODULE_AUTHOR("Eddie C. Dost, Peter Zaitcev, and David S. Miller");
1606MODULE_DESCRIPTION("Sun SU serial port driver");
1607MODULE_VERSION("2.0");
1608MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/sunzilog.c b/drivers/tty/serial/sunzilog.c
new file mode 100644
index 000000000000..c1967ac1c07f
--- /dev/null
+++ b/drivers/tty/serial/sunzilog.c
@@ -0,0 +1,1655 @@
1/* sunzilog.c: Zilog serial driver for Sparc systems.
2 *
3 * Driver for Zilog serial chips found on Sun workstations and
4 * servers. This driver could actually be made more generic.
5 *
6 * This is based on the old drivers/sbus/char/zs.c code. A lot
7 * of code has been simply moved over directly from there but
8 * much has been rewritten. Credits therefore go out to Eddie
9 * C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell for their
10 * work there.
11 *
12 * Copyright (C) 2002, 2006, 2007 David S. Miller (davem@davemloft.net)
13 */
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/delay.h>
19#include <linux/tty.h>
20#include <linux/tty_flip.h>
21#include <linux/major.h>
22#include <linux/string.h>
23#include <linux/ptrace.h>
24#include <linux/ioport.h>
25#include <linux/slab.h>
26#include <linux/circ_buf.h>
27#include <linux/serial.h>
28#include <linux/sysrq.h>
29#include <linux/console.h>
30#include <linux/spinlock.h>
31#ifdef CONFIG_SERIO
32#include <linux/serio.h>
33#endif
34#include <linux/init.h>
35#include <linux/of_device.h>
36
37#include <asm/io.h>
38#include <asm/irq.h>
39#include <asm/prom.h>
40
41#if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
42#define SUPPORT_SYSRQ
43#endif
44
45#include <linux/serial_core.h>
46
47#include "suncore.h"
48#include "sunzilog.h"
49
50/* On 32-bit sparcs we need to delay after register accesses
51 * to accommodate sun4 systems, but we do not need to flush writes.
52 * On 64-bit sparc we only need to flush single writes to ensure
53 * completion.
54 */
55#ifndef CONFIG_SPARC64
56#define ZSDELAY() udelay(5)
57#define ZSDELAY_LONG() udelay(20)
58#define ZS_WSYNC(channel) do { } while (0)
59#else
60#define ZSDELAY()
61#define ZSDELAY_LONG()
62#define ZS_WSYNC(__channel) \
63 readb(&((__channel)->control))
64#endif
65
66#define ZS_CLOCK 4915200 /* Zilog input clock rate. */
67#define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
68
69/*
70 * We wrap our port structure around the generic uart_port.
71 */
72struct uart_sunzilog_port {
73 struct uart_port port;
74
75 /* IRQ servicing chain. */
76 struct uart_sunzilog_port *next;
77
78 /* Current values of Zilog write registers. */
79 unsigned char curregs[NUM_ZSREGS];
80
81 unsigned int flags;
82#define SUNZILOG_FLAG_CONS_KEYB 0x00000001
83#define SUNZILOG_FLAG_CONS_MOUSE 0x00000002
84#define SUNZILOG_FLAG_IS_CONS 0x00000004
85#define SUNZILOG_FLAG_IS_KGDB 0x00000008
86#define SUNZILOG_FLAG_MODEM_STATUS 0x00000010
87#define SUNZILOG_FLAG_IS_CHANNEL_A 0x00000020
88#define SUNZILOG_FLAG_REGS_HELD 0x00000040
89#define SUNZILOG_FLAG_TX_STOPPED 0x00000080
90#define SUNZILOG_FLAG_TX_ACTIVE 0x00000100
91#define SUNZILOG_FLAG_ESCC 0x00000200
92#define SUNZILOG_FLAG_ISR_HANDLER 0x00000400
93
94 unsigned int cflag;
95
96 unsigned char parity_mask;
97 unsigned char prev_status;
98
99#ifdef CONFIG_SERIO
100 struct serio serio;
101 int serio_open;
102#endif
103};
104
105static void sunzilog_putchar(struct uart_port *port, int ch);
106
107#define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel __iomem *)((PORT)->membase))
108#define UART_ZILOG(PORT) ((struct uart_sunzilog_port *)(PORT))
109
110#define ZS_IS_KEYB(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_KEYB)
111#define ZS_IS_MOUSE(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_MOUSE)
112#define ZS_IS_CONS(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CONS)
113#define ZS_IS_KGDB(UP) ((UP)->flags & SUNZILOG_FLAG_IS_KGDB)
114#define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & SUNZILOG_FLAG_MODEM_STATUS)
115#define ZS_IS_CHANNEL_A(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CHANNEL_A)
116#define ZS_REGS_HELD(UP) ((UP)->flags & SUNZILOG_FLAG_REGS_HELD)
117#define ZS_TX_STOPPED(UP) ((UP)->flags & SUNZILOG_FLAG_TX_STOPPED)
118#define ZS_TX_ACTIVE(UP) ((UP)->flags & SUNZILOG_FLAG_TX_ACTIVE)
119
120/* Reading and writing Zilog8530 registers. The delays are to make this
121 * driver work on the Sun4 which needs a settling delay after each chip
122 * register access, other machines handle this in hardware via auxiliary
123 * flip-flops which implement the settle time we do in software.
124 *
125 * The port lock must be held and local IRQs must be disabled
126 * when {read,write}_zsreg is invoked.
127 */
128static unsigned char read_zsreg(struct zilog_channel __iomem *channel,
129 unsigned char reg)
130{
131 unsigned char retval;
132
133 writeb(reg, &channel->control);
134 ZSDELAY();
135 retval = readb(&channel->control);
136 ZSDELAY();
137
138 return retval;
139}
140
141static void write_zsreg(struct zilog_channel __iomem *channel,
142 unsigned char reg, unsigned char value)
143{
144 writeb(reg, &channel->control);
145 ZSDELAY();
146 writeb(value, &channel->control);
147 ZSDELAY();
148}
149
150static void sunzilog_clear_fifo(struct zilog_channel __iomem *channel)
151{
152 int i;
153
154 for (i = 0; i < 32; i++) {
155 unsigned char regval;
156
157 regval = readb(&channel->control);
158 ZSDELAY();
159 if (regval & Rx_CH_AV)
160 break;
161
162 regval = read_zsreg(channel, R1);
163 readb(&channel->data);
164 ZSDELAY();
165
166 if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
167 writeb(ERR_RES, &channel->control);
168 ZSDELAY();
169 ZS_WSYNC(channel);
170 }
171 }
172}
173
174/* This function must only be called when the TX is not busy. The UART
175 * port lock must be held and local interrupts disabled.
176 */
177static int __load_zsregs(struct zilog_channel __iomem *channel, unsigned char *regs)
178{
179 int i;
180 int escc;
181 unsigned char r15;
182
183 /* Let pending transmits finish. */
184 for (i = 0; i < 1000; i++) {
185 unsigned char stat = read_zsreg(channel, R1);
186 if (stat & ALL_SNT)
187 break;
188 udelay(100);
189 }
190
191 writeb(ERR_RES, &channel->control);
192 ZSDELAY();
193 ZS_WSYNC(channel);
194
195 sunzilog_clear_fifo(channel);
196
197 /* Disable all interrupts. */
198 write_zsreg(channel, R1,
199 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
200
201 /* Set parity, sync config, stop bits, and clock divisor. */
202 write_zsreg(channel, R4, regs[R4]);
203
204 /* Set misc. TX/RX control bits. */
205 write_zsreg(channel, R10, regs[R10]);
206
207 /* Set TX/RX controls sans the enable bits. */
208 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
209 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
210
211 /* Synchronous mode config. */
212 write_zsreg(channel, R6, regs[R6]);
213 write_zsreg(channel, R7, regs[R7]);
214
215 /* Don't mess with the interrupt vector (R2, unused by us) and
216 * master interrupt control (R9). We make sure this is setup
217 * properly at probe time then never touch it again.
218 */
219
220 /* Disable baud generator. */
221 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
222
223 /* Clock mode control. */
224 write_zsreg(channel, R11, regs[R11]);
225
226 /* Lower and upper byte of baud rate generator divisor. */
227 write_zsreg(channel, R12, regs[R12]);
228 write_zsreg(channel, R13, regs[R13]);
229
230 /* Now rewrite R14, with BRENAB (if set). */
231 write_zsreg(channel, R14, regs[R14]);
232
233 /* External status interrupt control. */
234 write_zsreg(channel, R15, (regs[R15] | WR7pEN) & ~FIFOEN);
235
236 /* ESCC Extension Register */
237 r15 = read_zsreg(channel, R15);
238 if (r15 & 0x01) {
239 write_zsreg(channel, R7, regs[R7p]);
240
241 /* External status interrupt and FIFO control. */
242 write_zsreg(channel, R15, regs[R15] & ~WR7pEN);
243 escc = 1;
244 } else {
245 /* Clear FIFO bit case it is an issue */
246 regs[R15] &= ~FIFOEN;
247 escc = 0;
248 }
249
250 /* Reset external status interrupts. */
251 write_zsreg(channel, R0, RES_EXT_INT); /* First Latch */
252 write_zsreg(channel, R0, RES_EXT_INT); /* Second Latch */
253
254 /* Rewrite R3/R5, this time without enables masked. */
255 write_zsreg(channel, R3, regs[R3]);
256 write_zsreg(channel, R5, regs[R5]);
257
258 /* Rewrite R1, this time without IRQ enabled masked. */
259 write_zsreg(channel, R1, regs[R1]);
260
261 return escc;
262}
263
264/* Reprogram the Zilog channel HW registers with the copies found in the
265 * software state struct. If the transmitter is busy, we defer this update
266 * until the next TX complete interrupt. Else, we do it right now.
267 *
268 * The UART port lock must be held and local interrupts disabled.
269 */
270static void sunzilog_maybe_update_regs(struct uart_sunzilog_port *up,
271 struct zilog_channel __iomem *channel)
272{
273 if (!ZS_REGS_HELD(up)) {
274 if (ZS_TX_ACTIVE(up)) {
275 up->flags |= SUNZILOG_FLAG_REGS_HELD;
276 } else {
277 __load_zsregs(channel, up->curregs);
278 }
279 }
280}
281
282static void sunzilog_change_mouse_baud(struct uart_sunzilog_port *up)
283{
284 unsigned int cur_cflag = up->cflag;
285 int brg, new_baud;
286
287 up->cflag &= ~CBAUD;
288 up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
289
290 brg = BPS_TO_BRG(new_baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
291 up->curregs[R12] = (brg & 0xff);
292 up->curregs[R13] = (brg >> 8) & 0xff;
293 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(&up->port));
294}
295
296static void sunzilog_kbdms_receive_chars(struct uart_sunzilog_port *up,
297 unsigned char ch, int is_break)
298{
299 if (ZS_IS_KEYB(up)) {
300 /* Stop-A is handled by drivers/char/keyboard.c now. */
301#ifdef CONFIG_SERIO
302 if (up->serio_open)
303 serio_interrupt(&up->serio, ch, 0);
304#endif
305 } else if (ZS_IS_MOUSE(up)) {
306 int ret = suncore_mouse_baud_detection(ch, is_break);
307
308 switch (ret) {
309 case 2:
310 sunzilog_change_mouse_baud(up);
311 /* fallthru */
312 case 1:
313 break;
314
315 case 0:
316#ifdef CONFIG_SERIO
317 if (up->serio_open)
318 serio_interrupt(&up->serio, ch, 0);
319#endif
320 break;
321 };
322 }
323}
324
325static struct tty_struct *
326sunzilog_receive_chars(struct uart_sunzilog_port *up,
327 struct zilog_channel __iomem *channel)
328{
329 struct tty_struct *tty;
330 unsigned char ch, r1, flag;
331
332 tty = NULL;
333 if (up->port.state != NULL && /* Unopened serial console */
334 up->port.state->port.tty != NULL) /* Keyboard || mouse */
335 tty = up->port.state->port.tty;
336
337 for (;;) {
338
339 r1 = read_zsreg(channel, R1);
340 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
341 writeb(ERR_RES, &channel->control);
342 ZSDELAY();
343 ZS_WSYNC(channel);
344 }
345
346 ch = readb(&channel->control);
347 ZSDELAY();
348
349 /* This funny hack depends upon BRK_ABRT not interfering
350 * with the other bits we care about in R1.
351 */
352 if (ch & BRK_ABRT)
353 r1 |= BRK_ABRT;
354
355 if (!(ch & Rx_CH_AV))
356 break;
357
358 ch = readb(&channel->data);
359 ZSDELAY();
360
361 ch &= up->parity_mask;
362
363 if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
364 sunzilog_kbdms_receive_chars(up, ch, 0);
365 continue;
366 }
367
368 if (tty == NULL) {
369 uart_handle_sysrq_char(&up->port, ch);
370 continue;
371 }
372
373 /* A real serial line, record the character and status. */
374 flag = TTY_NORMAL;
375 up->port.icount.rx++;
376 if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
377 if (r1 & BRK_ABRT) {
378 r1 &= ~(PAR_ERR | CRC_ERR);
379 up->port.icount.brk++;
380 if (uart_handle_break(&up->port))
381 continue;
382 }
383 else if (r1 & PAR_ERR)
384 up->port.icount.parity++;
385 else if (r1 & CRC_ERR)
386 up->port.icount.frame++;
387 if (r1 & Rx_OVR)
388 up->port.icount.overrun++;
389 r1 &= up->port.read_status_mask;
390 if (r1 & BRK_ABRT)
391 flag = TTY_BREAK;
392 else if (r1 & PAR_ERR)
393 flag = TTY_PARITY;
394 else if (r1 & CRC_ERR)
395 flag = TTY_FRAME;
396 }
397 if (uart_handle_sysrq_char(&up->port, ch))
398 continue;
399
400 if (up->port.ignore_status_mask == 0xff ||
401 (r1 & up->port.ignore_status_mask) == 0) {
402 tty_insert_flip_char(tty, ch, flag);
403 }
404 if (r1 & Rx_OVR)
405 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
406 }
407
408 return tty;
409}
410
411static void sunzilog_status_handle(struct uart_sunzilog_port *up,
412 struct zilog_channel __iomem *channel)
413{
414 unsigned char status;
415
416 status = readb(&channel->control);
417 ZSDELAY();
418
419 writeb(RES_EXT_INT, &channel->control);
420 ZSDELAY();
421 ZS_WSYNC(channel);
422
423 if (status & BRK_ABRT) {
424 if (ZS_IS_MOUSE(up))
425 sunzilog_kbdms_receive_chars(up, 0, 1);
426 if (ZS_IS_CONS(up)) {
427 /* Wait for BREAK to deassert to avoid potentially
428 * confusing the PROM.
429 */
430 while (1) {
431 status = readb(&channel->control);
432 ZSDELAY();
433 if (!(status & BRK_ABRT))
434 break;
435 }
436 sun_do_break();
437 return;
438 }
439 }
440
441 if (ZS_WANTS_MODEM_STATUS(up)) {
442 if (status & SYNC)
443 up->port.icount.dsr++;
444
445 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
446 * But it does not tell us which bit has changed, we have to keep
447 * track of this ourselves.
448 */
449 if ((status ^ up->prev_status) ^ DCD)
450 uart_handle_dcd_change(&up->port,
451 (status & DCD));
452 if ((status ^ up->prev_status) ^ CTS)
453 uart_handle_cts_change(&up->port,
454 (status & CTS));
455
456 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
457 }
458
459 up->prev_status = status;
460}
461
462static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
463 struct zilog_channel __iomem *channel)
464{
465 struct circ_buf *xmit;
466
467 if (ZS_IS_CONS(up)) {
468 unsigned char status = readb(&channel->control);
469 ZSDELAY();
470
471 /* TX still busy? Just wait for the next TX done interrupt.
472 *
473 * It can occur because of how we do serial console writes. It would
474 * be nice to transmit console writes just like we normally would for
475 * a TTY line. (ie. buffered and TX interrupt driven). That is not
476 * easy because console writes cannot sleep. One solution might be
477 * to poll on enough port->xmit space becomming free. -DaveM
478 */
479 if (!(status & Tx_BUF_EMP))
480 return;
481 }
482
483 up->flags &= ~SUNZILOG_FLAG_TX_ACTIVE;
484
485 if (ZS_REGS_HELD(up)) {
486 __load_zsregs(channel, up->curregs);
487 up->flags &= ~SUNZILOG_FLAG_REGS_HELD;
488 }
489
490 if (ZS_TX_STOPPED(up)) {
491 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
492 goto ack_tx_int;
493 }
494
495 if (up->port.x_char) {
496 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
497 writeb(up->port.x_char, &channel->data);
498 ZSDELAY();
499 ZS_WSYNC(channel);
500
501 up->port.icount.tx++;
502 up->port.x_char = 0;
503 return;
504 }
505
506 if (up->port.state == NULL)
507 goto ack_tx_int;
508 xmit = &up->port.state->xmit;
509 if (uart_circ_empty(xmit))
510 goto ack_tx_int;
511
512 if (uart_tx_stopped(&up->port))
513 goto ack_tx_int;
514
515 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
516 writeb(xmit->buf[xmit->tail], &channel->data);
517 ZSDELAY();
518 ZS_WSYNC(channel);
519
520 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
521 up->port.icount.tx++;
522
523 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
524 uart_write_wakeup(&up->port);
525
526 return;
527
528ack_tx_int:
529 writeb(RES_Tx_P, &channel->control);
530 ZSDELAY();
531 ZS_WSYNC(channel);
532}
533
534static irqreturn_t sunzilog_interrupt(int irq, void *dev_id)
535{
536 struct uart_sunzilog_port *up = dev_id;
537
538 while (up) {
539 struct zilog_channel __iomem *channel
540 = ZILOG_CHANNEL_FROM_PORT(&up->port);
541 struct tty_struct *tty;
542 unsigned char r3;
543
544 spin_lock(&up->port.lock);
545 r3 = read_zsreg(channel, R3);
546
547 /* Channel A */
548 tty = NULL;
549 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
550 writeb(RES_H_IUS, &channel->control);
551 ZSDELAY();
552 ZS_WSYNC(channel);
553
554 if (r3 & CHARxIP)
555 tty = sunzilog_receive_chars(up, channel);
556 if (r3 & CHAEXT)
557 sunzilog_status_handle(up, channel);
558 if (r3 & CHATxIP)
559 sunzilog_transmit_chars(up, channel);
560 }
561 spin_unlock(&up->port.lock);
562
563 if (tty)
564 tty_flip_buffer_push(tty);
565
566 /* Channel B */
567 up = up->next;
568 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
569
570 spin_lock(&up->port.lock);
571 tty = NULL;
572 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
573 writeb(RES_H_IUS, &channel->control);
574 ZSDELAY();
575 ZS_WSYNC(channel);
576
577 if (r3 & CHBRxIP)
578 tty = sunzilog_receive_chars(up, channel);
579 if (r3 & CHBEXT)
580 sunzilog_status_handle(up, channel);
581 if (r3 & CHBTxIP)
582 sunzilog_transmit_chars(up, channel);
583 }
584 spin_unlock(&up->port.lock);
585
586 if (tty)
587 tty_flip_buffer_push(tty);
588
589 up = up->next;
590 }
591
592 return IRQ_HANDLED;
593}
594
595/* A convenient way to quickly get R0 status. The caller must _not_ hold the
596 * port lock, it is acquired here.
597 */
598static __inline__ unsigned char sunzilog_read_channel_status(struct uart_port *port)
599{
600 struct zilog_channel __iomem *channel;
601 unsigned char status;
602
603 channel = ZILOG_CHANNEL_FROM_PORT(port);
604 status = readb(&channel->control);
605 ZSDELAY();
606
607 return status;
608}
609
610/* The port lock is not held. */
611static unsigned int sunzilog_tx_empty(struct uart_port *port)
612{
613 unsigned long flags;
614 unsigned char status;
615 unsigned int ret;
616
617 spin_lock_irqsave(&port->lock, flags);
618
619 status = sunzilog_read_channel_status(port);
620
621 spin_unlock_irqrestore(&port->lock, flags);
622
623 if (status & Tx_BUF_EMP)
624 ret = TIOCSER_TEMT;
625 else
626 ret = 0;
627
628 return ret;
629}
630
631/* The port lock is held and interrupts are disabled. */
632static unsigned int sunzilog_get_mctrl(struct uart_port *port)
633{
634 unsigned char status;
635 unsigned int ret;
636
637 status = sunzilog_read_channel_status(port);
638
639 ret = 0;
640 if (status & DCD)
641 ret |= TIOCM_CAR;
642 if (status & SYNC)
643 ret |= TIOCM_DSR;
644 if (status & CTS)
645 ret |= TIOCM_CTS;
646
647 return ret;
648}
649
650/* The port lock is held and interrupts are disabled. */
651static void sunzilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
652{
653 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
654 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
655 unsigned char set_bits, clear_bits;
656
657 set_bits = clear_bits = 0;
658
659 if (mctrl & TIOCM_RTS)
660 set_bits |= RTS;
661 else
662 clear_bits |= RTS;
663 if (mctrl & TIOCM_DTR)
664 set_bits |= DTR;
665 else
666 clear_bits |= DTR;
667
668 /* NOTE: Not subject to 'transmitter active' rule. */
669 up->curregs[R5] |= set_bits;
670 up->curregs[R5] &= ~clear_bits;
671 write_zsreg(channel, R5, up->curregs[R5]);
672}
673
674/* The port lock is held and interrupts are disabled. */
675static void sunzilog_stop_tx(struct uart_port *port)
676{
677 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
678
679 up->flags |= SUNZILOG_FLAG_TX_STOPPED;
680}
681
682/* The port lock is held and interrupts are disabled. */
683static void sunzilog_start_tx(struct uart_port *port)
684{
685 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
686 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
687 unsigned char status;
688
689 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
690 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
691
692 status = readb(&channel->control);
693 ZSDELAY();
694
695 /* TX busy? Just wait for the TX done interrupt. */
696 if (!(status & Tx_BUF_EMP))
697 return;
698
699 /* Send the first character to jump-start the TX done
700 * IRQ sending engine.
701 */
702 if (port->x_char) {
703 writeb(port->x_char, &channel->data);
704 ZSDELAY();
705 ZS_WSYNC(channel);
706
707 port->icount.tx++;
708 port->x_char = 0;
709 } else {
710 struct circ_buf *xmit = &port->state->xmit;
711
712 writeb(xmit->buf[xmit->tail], &channel->data);
713 ZSDELAY();
714 ZS_WSYNC(channel);
715
716 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
717 port->icount.tx++;
718
719 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
720 uart_write_wakeup(&up->port);
721 }
722}
723
724/* The port lock is held. */
725static void sunzilog_stop_rx(struct uart_port *port)
726{
727 struct uart_sunzilog_port *up = UART_ZILOG(port);
728 struct zilog_channel __iomem *channel;
729
730 if (ZS_IS_CONS(up))
731 return;
732
733 channel = ZILOG_CHANNEL_FROM_PORT(port);
734
735 /* Disable all RX interrupts. */
736 up->curregs[R1] &= ~RxINT_MASK;
737 sunzilog_maybe_update_regs(up, channel);
738}
739
740/* The port lock is held. */
741static void sunzilog_enable_ms(struct uart_port *port)
742{
743 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
744 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
745 unsigned char new_reg;
746
747 new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
748 if (new_reg != up->curregs[R15]) {
749 up->curregs[R15] = new_reg;
750
751 /* NOTE: Not subject to 'transmitter active' rule. */
752 write_zsreg(channel, R15, up->curregs[R15] & ~WR7pEN);
753 }
754}
755
756/* The port lock is not held. */
757static void sunzilog_break_ctl(struct uart_port *port, int break_state)
758{
759 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
760 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
761 unsigned char set_bits, clear_bits, new_reg;
762 unsigned long flags;
763
764 set_bits = clear_bits = 0;
765
766 if (break_state)
767 set_bits |= SND_BRK;
768 else
769 clear_bits |= SND_BRK;
770
771 spin_lock_irqsave(&port->lock, flags);
772
773 new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
774 if (new_reg != up->curregs[R5]) {
775 up->curregs[R5] = new_reg;
776
777 /* NOTE: Not subject to 'transmitter active' rule. */
778 write_zsreg(channel, R5, up->curregs[R5]);
779 }
780
781 spin_unlock_irqrestore(&port->lock, flags);
782}
783
784static void __sunzilog_startup(struct uart_sunzilog_port *up)
785{
786 struct zilog_channel __iomem *channel;
787
788 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
789 up->prev_status = readb(&channel->control);
790
791 /* Enable receiver and transmitter. */
792 up->curregs[R3] |= RxENAB;
793 up->curregs[R5] |= TxENAB;
794
795 up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
796 sunzilog_maybe_update_regs(up, channel);
797}
798
799static int sunzilog_startup(struct uart_port *port)
800{
801 struct uart_sunzilog_port *up = UART_ZILOG(port);
802 unsigned long flags;
803
804 if (ZS_IS_CONS(up))
805 return 0;
806
807 spin_lock_irqsave(&port->lock, flags);
808 __sunzilog_startup(up);
809 spin_unlock_irqrestore(&port->lock, flags);
810 return 0;
811}
812
813/*
814 * The test for ZS_IS_CONS is explained by the following e-mail:
815 *****
816 * From: Russell King <rmk@arm.linux.org.uk>
817 * Date: Sun, 8 Dec 2002 10:18:38 +0000
818 *
819 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
820 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
821 * > and I noticed that something is not right with reference
822 * > counting in this case. It seems that when the console
823 * > is open by kernel initially, this is not accounted
824 * > as an open, and uart_startup is not called.
825 *
826 * That is correct. We are unable to call uart_startup when the serial
827 * console is initialised because it may need to allocate memory (as
828 * request_irq does) and the memory allocators may not have been
829 * initialised.
830 *
831 * 1. initialise the port into a state where it can send characters in the
832 * console write method.
833 *
834 * 2. don't do the actual hardware shutdown in your shutdown() method (but
835 * do the normal software shutdown - ie, free irqs etc)
836 *****
837 */
838static void sunzilog_shutdown(struct uart_port *port)
839{
840 struct uart_sunzilog_port *up = UART_ZILOG(port);
841 struct zilog_channel __iomem *channel;
842 unsigned long flags;
843
844 if (ZS_IS_CONS(up))
845 return;
846
847 spin_lock_irqsave(&port->lock, flags);
848
849 channel = ZILOG_CHANNEL_FROM_PORT(port);
850
851 /* Disable receiver and transmitter. */
852 up->curregs[R3] &= ~RxENAB;
853 up->curregs[R5] &= ~TxENAB;
854
855 /* Disable all interrupts and BRK assertion. */
856 up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
857 up->curregs[R5] &= ~SND_BRK;
858 sunzilog_maybe_update_regs(up, channel);
859
860 spin_unlock_irqrestore(&port->lock, flags);
861}
862
863/* Shared by TTY driver and serial console setup. The port lock is held
864 * and local interrupts are disabled.
865 */
866static void
867sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
868 unsigned int iflag, int brg)
869{
870
871 up->curregs[R10] = NRZ;
872 up->curregs[R11] = TCBR | RCBR;
873
874 /* Program BAUD and clock source. */
875 up->curregs[R4] &= ~XCLK_MASK;
876 up->curregs[R4] |= X16CLK;
877 up->curregs[R12] = brg & 0xff;
878 up->curregs[R13] = (brg >> 8) & 0xff;
879 up->curregs[R14] = BRSRC | BRENAB;
880
881 /* Character size, stop bits, and parity. */
882 up->curregs[R3] &= ~RxN_MASK;
883 up->curregs[R5] &= ~TxN_MASK;
884 switch (cflag & CSIZE) {
885 case CS5:
886 up->curregs[R3] |= Rx5;
887 up->curregs[R5] |= Tx5;
888 up->parity_mask = 0x1f;
889 break;
890 case CS6:
891 up->curregs[R3] |= Rx6;
892 up->curregs[R5] |= Tx6;
893 up->parity_mask = 0x3f;
894 break;
895 case CS7:
896 up->curregs[R3] |= Rx7;
897 up->curregs[R5] |= Tx7;
898 up->parity_mask = 0x7f;
899 break;
900 case CS8:
901 default:
902 up->curregs[R3] |= Rx8;
903 up->curregs[R5] |= Tx8;
904 up->parity_mask = 0xff;
905 break;
906 };
907 up->curregs[R4] &= ~0x0c;
908 if (cflag & CSTOPB)
909 up->curregs[R4] |= SB2;
910 else
911 up->curregs[R4] |= SB1;
912 if (cflag & PARENB)
913 up->curregs[R4] |= PAR_ENAB;
914 else
915 up->curregs[R4] &= ~PAR_ENAB;
916 if (!(cflag & PARODD))
917 up->curregs[R4] |= PAR_EVEN;
918 else
919 up->curregs[R4] &= ~PAR_EVEN;
920
921 up->port.read_status_mask = Rx_OVR;
922 if (iflag & INPCK)
923 up->port.read_status_mask |= CRC_ERR | PAR_ERR;
924 if (iflag & (BRKINT | PARMRK))
925 up->port.read_status_mask |= BRK_ABRT;
926
927 up->port.ignore_status_mask = 0;
928 if (iflag & IGNPAR)
929 up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
930 if (iflag & IGNBRK) {
931 up->port.ignore_status_mask |= BRK_ABRT;
932 if (iflag & IGNPAR)
933 up->port.ignore_status_mask |= Rx_OVR;
934 }
935
936 if ((cflag & CREAD) == 0)
937 up->port.ignore_status_mask = 0xff;
938}
939
940/* The port lock is not held. */
941static void
942sunzilog_set_termios(struct uart_port *port, struct ktermios *termios,
943 struct ktermios *old)
944{
945 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
946 unsigned long flags;
947 int baud, brg;
948
949 baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
950
951 spin_lock_irqsave(&up->port.lock, flags);
952
953 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
954
955 sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
956
957 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
958 up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
959 else
960 up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;
961
962 up->cflag = termios->c_cflag;
963
964 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
965
966 uart_update_timeout(port, termios->c_cflag, baud);
967
968 spin_unlock_irqrestore(&up->port.lock, flags);
969}
970
971static const char *sunzilog_type(struct uart_port *port)
972{
973 struct uart_sunzilog_port *up = UART_ZILOG(port);
974
975 return (up->flags & SUNZILOG_FLAG_ESCC) ? "zs (ESCC)" : "zs";
976}
977
978/* We do not request/release mappings of the registers here, this
979 * happens at early serial probe time.
980 */
981static void sunzilog_release_port(struct uart_port *port)
982{
983}
984
985static int sunzilog_request_port(struct uart_port *port)
986{
987 return 0;
988}
989
990/* These do not need to do anything interesting either. */
991static void sunzilog_config_port(struct uart_port *port, int flags)
992{
993}
994
995/* We do not support letting the user mess with the divisor, IRQ, etc. */
996static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
997{
998 return -EINVAL;
999}
1000
1001#ifdef CONFIG_CONSOLE_POLL
1002static int sunzilog_get_poll_char(struct uart_port *port)
1003{
1004 unsigned char ch, r1;
1005 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
1006 struct zilog_channel __iomem *channel
1007 = ZILOG_CHANNEL_FROM_PORT(&up->port);
1008
1009
1010 r1 = read_zsreg(channel, R1);
1011 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
1012 writeb(ERR_RES, &channel->control);
1013 ZSDELAY();
1014 ZS_WSYNC(channel);
1015 }
1016
1017 ch = readb(&channel->control);
1018 ZSDELAY();
1019
1020 /* This funny hack depends upon BRK_ABRT not interfering
1021 * with the other bits we care about in R1.
1022 */
1023 if (ch & BRK_ABRT)
1024 r1 |= BRK_ABRT;
1025
1026 if (!(ch & Rx_CH_AV))
1027 return NO_POLL_CHAR;
1028
1029 ch = readb(&channel->data);
1030 ZSDELAY();
1031
1032 ch &= up->parity_mask;
1033 return ch;
1034}
1035
1036static void sunzilog_put_poll_char(struct uart_port *port,
1037 unsigned char ch)
1038{
1039 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *)port;
1040
1041 sunzilog_putchar(&up->port, ch);
1042}
1043#endif /* CONFIG_CONSOLE_POLL */
1044
1045static struct uart_ops sunzilog_pops = {
1046 .tx_empty = sunzilog_tx_empty,
1047 .set_mctrl = sunzilog_set_mctrl,
1048 .get_mctrl = sunzilog_get_mctrl,
1049 .stop_tx = sunzilog_stop_tx,
1050 .start_tx = sunzilog_start_tx,
1051 .stop_rx = sunzilog_stop_rx,
1052 .enable_ms = sunzilog_enable_ms,
1053 .break_ctl = sunzilog_break_ctl,
1054 .startup = sunzilog_startup,
1055 .shutdown = sunzilog_shutdown,
1056 .set_termios = sunzilog_set_termios,
1057 .type = sunzilog_type,
1058 .release_port = sunzilog_release_port,
1059 .request_port = sunzilog_request_port,
1060 .config_port = sunzilog_config_port,
1061 .verify_port = sunzilog_verify_port,
1062#ifdef CONFIG_CONSOLE_POLL
1063 .poll_get_char = sunzilog_get_poll_char,
1064 .poll_put_char = sunzilog_put_poll_char,
1065#endif
1066};
1067
1068static int uart_chip_count;
1069static struct uart_sunzilog_port *sunzilog_port_table;
1070static struct zilog_layout __iomem **sunzilog_chip_regs;
1071
1072static struct uart_sunzilog_port *sunzilog_irq_chain;
1073
1074static struct uart_driver sunzilog_reg = {
1075 .owner = THIS_MODULE,
1076 .driver_name = "sunzilog",
1077 .dev_name = "ttyS",
1078 .major = TTY_MAJOR,
1079};
1080
1081static int __init sunzilog_alloc_tables(int num_sunzilog)
1082{
1083 struct uart_sunzilog_port *up;
1084 unsigned long size;
1085 int num_channels = num_sunzilog * 2;
1086 int i;
1087
1088 size = num_channels * sizeof(struct uart_sunzilog_port);
1089 sunzilog_port_table = kzalloc(size, GFP_KERNEL);
1090 if (!sunzilog_port_table)
1091 return -ENOMEM;
1092
1093 for (i = 0; i < num_channels; i++) {
1094 up = &sunzilog_port_table[i];
1095
1096 spin_lock_init(&up->port.lock);
1097
1098 if (i == 0)
1099 sunzilog_irq_chain = up;
1100
1101 if (i < num_channels - 1)
1102 up->next = up + 1;
1103 else
1104 up->next = NULL;
1105 }
1106
1107 size = num_sunzilog * sizeof(struct zilog_layout __iomem *);
1108 sunzilog_chip_regs = kzalloc(size, GFP_KERNEL);
1109 if (!sunzilog_chip_regs) {
1110 kfree(sunzilog_port_table);
1111 sunzilog_irq_chain = NULL;
1112 return -ENOMEM;
1113 }
1114
1115 return 0;
1116}
1117
1118static void sunzilog_free_tables(void)
1119{
1120 kfree(sunzilog_port_table);
1121 sunzilog_irq_chain = NULL;
1122 kfree(sunzilog_chip_regs);
1123}
1124
1125#define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
1126
1127static void sunzilog_putchar(struct uart_port *port, int ch)
1128{
1129 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
1130 int loops = ZS_PUT_CHAR_MAX_DELAY;
1131
1132 /* This is a timed polling loop so do not switch the explicit
1133 * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
1134 */
1135 do {
1136 unsigned char val = readb(&channel->control);
1137 if (val & Tx_BUF_EMP) {
1138 ZSDELAY();
1139 break;
1140 }
1141 udelay(5);
1142 } while (--loops);
1143
1144 writeb(ch, &channel->data);
1145 ZSDELAY();
1146 ZS_WSYNC(channel);
1147}
1148
1149#ifdef CONFIG_SERIO
1150
1151static DEFINE_SPINLOCK(sunzilog_serio_lock);
1152
1153static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
1154{
1155 struct uart_sunzilog_port *up = serio->port_data;
1156 unsigned long flags;
1157
1158 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1159
1160 sunzilog_putchar(&up->port, ch);
1161
1162 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1163
1164 return 0;
1165}
1166
1167static int sunzilog_serio_open(struct serio *serio)
1168{
1169 struct uart_sunzilog_port *up = serio->port_data;
1170 unsigned long flags;
1171 int ret;
1172
1173 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1174 if (!up->serio_open) {
1175 up->serio_open = 1;
1176 ret = 0;
1177 } else
1178 ret = -EBUSY;
1179 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1180
1181 return ret;
1182}
1183
1184static void sunzilog_serio_close(struct serio *serio)
1185{
1186 struct uart_sunzilog_port *up = serio->port_data;
1187 unsigned long flags;
1188
1189 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1190 up->serio_open = 0;
1191 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1192}
1193
1194#endif /* CONFIG_SERIO */
1195
1196#ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
1197static void
1198sunzilog_console_write(struct console *con, const char *s, unsigned int count)
1199{
1200 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1201 unsigned long flags;
1202 int locked = 1;
1203
1204 local_irq_save(flags);
1205 if (up->port.sysrq) {
1206 locked = 0;
1207 } else if (oops_in_progress) {
1208 locked = spin_trylock(&up->port.lock);
1209 } else
1210 spin_lock(&up->port.lock);
1211
1212 uart_console_write(&up->port, s, count, sunzilog_putchar);
1213 udelay(2);
1214
1215 if (locked)
1216 spin_unlock(&up->port.lock);
1217 local_irq_restore(flags);
1218}
1219
1220static int __init sunzilog_console_setup(struct console *con, char *options)
1221{
1222 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1223 unsigned long flags;
1224 int baud, brg;
1225
1226 if (up->port.type != PORT_SUNZILOG)
1227 return -1;
1228
1229 printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
1230 (sunzilog_reg.minor - 64) + con->index, con->index);
1231
1232 /* Get firmware console settings. */
1233 sunserial_console_termios(con, up->port.dev->of_node);
1234
1235 /* Firmware console speed is limited to 150-->38400 baud so
1236 * this hackish cflag thing is OK.
1237 */
1238 switch (con->cflag & CBAUD) {
1239 case B150: baud = 150; break;
1240 case B300: baud = 300; break;
1241 case B600: baud = 600; break;
1242 case B1200: baud = 1200; break;
1243 case B2400: baud = 2400; break;
1244 case B4800: baud = 4800; break;
1245 default: case B9600: baud = 9600; break;
1246 case B19200: baud = 19200; break;
1247 case B38400: baud = 38400; break;
1248 };
1249
1250 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1251
1252 spin_lock_irqsave(&up->port.lock, flags);
1253
1254 up->curregs[R15] |= BRKIE;
1255 sunzilog_convert_to_zs(up, con->cflag, 0, brg);
1256
1257 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1258 __sunzilog_startup(up);
1259
1260 spin_unlock_irqrestore(&up->port.lock, flags);
1261
1262 return 0;
1263}
1264
1265static struct console sunzilog_console_ops = {
1266 .name = "ttyS",
1267 .write = sunzilog_console_write,
1268 .device = uart_console_device,
1269 .setup = sunzilog_console_setup,
1270 .flags = CON_PRINTBUFFER,
1271 .index = -1,
1272 .data = &sunzilog_reg,
1273};
1274
1275static inline struct console *SUNZILOG_CONSOLE(void)
1276{
1277 return &sunzilog_console_ops;
1278}
1279
1280#else
1281#define SUNZILOG_CONSOLE() (NULL)
1282#endif
1283
1284static void __devinit sunzilog_init_kbdms(struct uart_sunzilog_port *up)
1285{
1286 int baud, brg;
1287
1288 if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
1289 up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1290 baud = 1200;
1291 } else {
1292 up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1293 baud = 4800;
1294 }
1295
1296 up->curregs[R15] |= BRKIE;
1297 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1298 sunzilog_convert_to_zs(up, up->cflag, 0, brg);
1299 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1300 __sunzilog_startup(up);
1301}
1302
1303#ifdef CONFIG_SERIO
1304static void __devinit sunzilog_register_serio(struct uart_sunzilog_port *up)
1305{
1306 struct serio *serio = &up->serio;
1307
1308 serio->port_data = up;
1309
1310 serio->id.type = SERIO_RS232;
1311 if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
1312 serio->id.proto = SERIO_SUNKBD;
1313 strlcpy(serio->name, "zskbd", sizeof(serio->name));
1314 } else {
1315 serio->id.proto = SERIO_SUN;
1316 serio->id.extra = 1;
1317 strlcpy(serio->name, "zsms", sizeof(serio->name));
1318 }
1319 strlcpy(serio->phys,
1320 ((up->flags & SUNZILOG_FLAG_CONS_KEYB) ?
1321 "zs/serio0" : "zs/serio1"),
1322 sizeof(serio->phys));
1323
1324 serio->write = sunzilog_serio_write;
1325 serio->open = sunzilog_serio_open;
1326 serio->close = sunzilog_serio_close;
1327 serio->dev.parent = up->port.dev;
1328
1329 serio_register_port(serio);
1330}
1331#endif
1332
1333static void __devinit sunzilog_init_hw(struct uart_sunzilog_port *up)
1334{
1335 struct zilog_channel __iomem *channel;
1336 unsigned long flags;
1337 int baud, brg;
1338
1339 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1340
1341 spin_lock_irqsave(&up->port.lock, flags);
1342 if (ZS_IS_CHANNEL_A(up)) {
1343 write_zsreg(channel, R9, FHWRES);
1344 ZSDELAY_LONG();
1345 (void) read_zsreg(channel, R0);
1346 }
1347
1348 if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
1349 SUNZILOG_FLAG_CONS_MOUSE)) {
1350 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1351 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1352 up->curregs[R3] = RxENAB | Rx8;
1353 up->curregs[R5] = TxENAB | Tx8;
1354 up->curregs[R6] = 0x00; /* SDLC Address */
1355 up->curregs[R7] = 0x7E; /* SDLC Flag */
1356 up->curregs[R9] = NV;
1357 up->curregs[R7p] = 0x00;
1358 sunzilog_init_kbdms(up);
1359 /* Only enable interrupts if an ISR handler available */
1360 if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
1361 up->curregs[R9] |= MIE;
1362 write_zsreg(channel, R9, up->curregs[R9]);
1363 } else {
1364 /* Normal serial TTY. */
1365 up->parity_mask = 0xff;
1366 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1367 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1368 up->curregs[R3] = RxENAB | Rx8;
1369 up->curregs[R5] = TxENAB | Tx8;
1370 up->curregs[R6] = 0x00; /* SDLC Address */
1371 up->curregs[R7] = 0x7E; /* SDLC Flag */
1372 up->curregs[R9] = NV;
1373 up->curregs[R10] = NRZ;
1374 up->curregs[R11] = TCBR | RCBR;
1375 baud = 9600;
1376 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1377 up->curregs[R12] = (brg & 0xff);
1378 up->curregs[R13] = (brg >> 8) & 0xff;
1379 up->curregs[R14] = BRSRC | BRENAB;
1380 up->curregs[R15] = FIFOEN; /* Use FIFO if on ESCC */
1381 up->curregs[R7p] = TxFIFO_LVL | RxFIFO_LVL;
1382 if (__load_zsregs(channel, up->curregs)) {
1383 up->flags |= SUNZILOG_FLAG_ESCC;
1384 }
1385 /* Only enable interrupts if an ISR handler available */
1386 if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
1387 up->curregs[R9] |= MIE;
1388 write_zsreg(channel, R9, up->curregs[R9]);
1389 }
1390
1391 spin_unlock_irqrestore(&up->port.lock, flags);
1392
1393#ifdef CONFIG_SERIO
1394 if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
1395 SUNZILOG_FLAG_CONS_MOUSE))
1396 sunzilog_register_serio(up);
1397#endif
1398}
1399
1400static int zilog_irq = -1;
1401
1402static int __devinit zs_probe(struct platform_device *op, const struct of_device_id *match)
1403{
1404 static int kbm_inst, uart_inst;
1405 int inst;
1406 struct uart_sunzilog_port *up;
1407 struct zilog_layout __iomem *rp;
1408 int keyboard_mouse = 0;
1409 int err;
1410
1411 if (of_find_property(op->dev.of_node, "keyboard", NULL))
1412 keyboard_mouse = 1;
1413
1414 /* uarts must come before keyboards/mice */
1415 if (keyboard_mouse)
1416 inst = uart_chip_count + kbm_inst;
1417 else
1418 inst = uart_inst;
1419
1420 sunzilog_chip_regs[inst] = of_ioremap(&op->resource[0], 0,
1421 sizeof(struct zilog_layout),
1422 "zs");
1423 if (!sunzilog_chip_regs[inst])
1424 return -ENOMEM;
1425
1426 rp = sunzilog_chip_regs[inst];
1427
1428 if (zilog_irq == -1)
1429 zilog_irq = op->archdata.irqs[0];
1430
1431 up = &sunzilog_port_table[inst * 2];
1432
1433 /* Channel A */
1434 up[0].port.mapbase = op->resource[0].start + 0x00;
1435 up[0].port.membase = (void __iomem *) &rp->channelA;
1436 up[0].port.iotype = UPIO_MEM;
1437 up[0].port.irq = op->archdata.irqs[0];
1438 up[0].port.uartclk = ZS_CLOCK;
1439 up[0].port.fifosize = 1;
1440 up[0].port.ops = &sunzilog_pops;
1441 up[0].port.type = PORT_SUNZILOG;
1442 up[0].port.flags = 0;
1443 up[0].port.line = (inst * 2) + 0;
1444 up[0].port.dev = &op->dev;
1445 up[0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
1446 if (keyboard_mouse)
1447 up[0].flags |= SUNZILOG_FLAG_CONS_KEYB;
1448 sunzilog_init_hw(&up[0]);
1449
1450 /* Channel B */
1451 up[1].port.mapbase = op->resource[0].start + 0x04;
1452 up[1].port.membase = (void __iomem *) &rp->channelB;
1453 up[1].port.iotype = UPIO_MEM;
1454 up[1].port.irq = op->archdata.irqs[0];
1455 up[1].port.uartclk = ZS_CLOCK;
1456 up[1].port.fifosize = 1;
1457 up[1].port.ops = &sunzilog_pops;
1458 up[1].port.type = PORT_SUNZILOG;
1459 up[1].port.flags = 0;
1460 up[1].port.line = (inst * 2) + 1;
1461 up[1].port.dev = &op->dev;
1462 up[1].flags |= 0;
1463 if (keyboard_mouse)
1464 up[1].flags |= SUNZILOG_FLAG_CONS_MOUSE;
1465 sunzilog_init_hw(&up[1]);
1466
1467 if (!keyboard_mouse) {
1468 if (sunserial_console_match(SUNZILOG_CONSOLE(), op->dev.of_node,
1469 &sunzilog_reg, up[0].port.line,
1470 false))
1471 up->flags |= SUNZILOG_FLAG_IS_CONS;
1472 err = uart_add_one_port(&sunzilog_reg, &up[0].port);
1473 if (err) {
1474 of_iounmap(&op->resource[0],
1475 rp, sizeof(struct zilog_layout));
1476 return err;
1477 }
1478 if (sunserial_console_match(SUNZILOG_CONSOLE(), op->dev.of_node,
1479 &sunzilog_reg, up[1].port.line,
1480 false))
1481 up->flags |= SUNZILOG_FLAG_IS_CONS;
1482 err = uart_add_one_port(&sunzilog_reg, &up[1].port);
1483 if (err) {
1484 uart_remove_one_port(&sunzilog_reg, &up[0].port);
1485 of_iounmap(&op->resource[0],
1486 rp, sizeof(struct zilog_layout));
1487 return err;
1488 }
1489 uart_inst++;
1490 } else {
1491 printk(KERN_INFO "%s: Keyboard at MMIO 0x%llx (irq = %d) "
1492 "is a %s\n",
1493 dev_name(&op->dev),
1494 (unsigned long long) up[0].port.mapbase,
1495 op->archdata.irqs[0], sunzilog_type(&up[0].port));
1496 printk(KERN_INFO "%s: Mouse at MMIO 0x%llx (irq = %d) "
1497 "is a %s\n",
1498 dev_name(&op->dev),
1499 (unsigned long long) up[1].port.mapbase,
1500 op->archdata.irqs[0], sunzilog_type(&up[1].port));
1501 kbm_inst++;
1502 }
1503
1504 dev_set_drvdata(&op->dev, &up[0]);
1505
1506 return 0;
1507}
1508
1509static void __devexit zs_remove_one(struct uart_sunzilog_port *up)
1510{
1511 if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
1512#ifdef CONFIG_SERIO
1513 serio_unregister_port(&up->serio);
1514#endif
1515 } else
1516 uart_remove_one_port(&sunzilog_reg, &up->port);
1517}
1518
1519static int __devexit zs_remove(struct platform_device *op)
1520{
1521 struct uart_sunzilog_port *up = dev_get_drvdata(&op->dev);
1522 struct zilog_layout __iomem *regs;
1523
1524 zs_remove_one(&up[0]);
1525 zs_remove_one(&up[1]);
1526
1527 regs = sunzilog_chip_regs[up[0].port.line / 2];
1528 of_iounmap(&op->resource[0], regs, sizeof(struct zilog_layout));
1529
1530 dev_set_drvdata(&op->dev, NULL);
1531
1532 return 0;
1533}
1534
1535static const struct of_device_id zs_match[] = {
1536 {
1537 .name = "zs",
1538 },
1539 {},
1540};
1541MODULE_DEVICE_TABLE(of, zs_match);
1542
1543static struct of_platform_driver zs_driver = {
1544 .driver = {
1545 .name = "zs",
1546 .owner = THIS_MODULE,
1547 .of_match_table = zs_match,
1548 },
1549 .probe = zs_probe,
1550 .remove = __devexit_p(zs_remove),
1551};
1552
1553static int __init sunzilog_init(void)
1554{
1555 struct device_node *dp;
1556 int err;
1557 int num_keybms = 0;
1558 int num_sunzilog = 0;
1559
1560 for_each_node_by_name(dp, "zs") {
1561 num_sunzilog++;
1562 if (of_find_property(dp, "keyboard", NULL))
1563 num_keybms++;
1564 }
1565
1566 if (num_sunzilog) {
1567 err = sunzilog_alloc_tables(num_sunzilog);
1568 if (err)
1569 goto out;
1570
1571 uart_chip_count = num_sunzilog - num_keybms;
1572
1573 err = sunserial_register_minors(&sunzilog_reg,
1574 uart_chip_count * 2);
1575 if (err)
1576 goto out_free_tables;
1577 }
1578
1579 err = of_register_platform_driver(&zs_driver);
1580 if (err)
1581 goto out_unregister_uart;
1582
1583 if (zilog_irq != -1) {
1584 struct uart_sunzilog_port *up = sunzilog_irq_chain;
1585 err = request_irq(zilog_irq, sunzilog_interrupt, IRQF_SHARED,
1586 "zs", sunzilog_irq_chain);
1587 if (err)
1588 goto out_unregister_driver;
1589
1590 /* Enable Interrupts */
1591 while (up) {
1592 struct zilog_channel __iomem *channel;
1593
1594 /* printk (KERN_INFO "Enable IRQ for ZILOG Hardware %p\n", up); */
1595 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1596 up->flags |= SUNZILOG_FLAG_ISR_HANDLER;
1597 up->curregs[R9] |= MIE;
1598 write_zsreg(channel, R9, up->curregs[R9]);
1599 up = up->next;
1600 }
1601 }
1602
1603out:
1604 return err;
1605
1606out_unregister_driver:
1607 of_unregister_platform_driver(&zs_driver);
1608
1609out_unregister_uart:
1610 if (num_sunzilog) {
1611 sunserial_unregister_minors(&sunzilog_reg, num_sunzilog);
1612 sunzilog_reg.cons = NULL;
1613 }
1614
1615out_free_tables:
1616 sunzilog_free_tables();
1617 goto out;
1618}
1619
1620static void __exit sunzilog_exit(void)
1621{
1622 of_unregister_platform_driver(&zs_driver);
1623
1624 if (zilog_irq != -1) {
1625 struct uart_sunzilog_port *up = sunzilog_irq_chain;
1626
1627 /* Disable Interrupts */
1628 while (up) {
1629 struct zilog_channel __iomem *channel;
1630
1631 /* printk (KERN_INFO "Disable IRQ for ZILOG Hardware %p\n", up); */
1632 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1633 up->flags &= ~SUNZILOG_FLAG_ISR_HANDLER;
1634 up->curregs[R9] &= ~MIE;
1635 write_zsreg(channel, R9, up->curregs[R9]);
1636 up = up->next;
1637 }
1638
1639 free_irq(zilog_irq, sunzilog_irq_chain);
1640 zilog_irq = -1;
1641 }
1642
1643 if (sunzilog_reg.nr) {
1644 sunserial_unregister_minors(&sunzilog_reg, sunzilog_reg.nr);
1645 sunzilog_free_tables();
1646 }
1647}
1648
1649module_init(sunzilog_init);
1650module_exit(sunzilog_exit);
1651
1652MODULE_AUTHOR("David S. Miller");
1653MODULE_DESCRIPTION("Sun Zilog serial port driver");
1654MODULE_VERSION("2.0");
1655MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/sunzilog.h b/drivers/tty/serial/sunzilog.h
new file mode 100644
index 000000000000..5dec7b47cc38
--- /dev/null
+++ b/drivers/tty/serial/sunzilog.h
@@ -0,0 +1,289 @@
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 17
17#define R7p 16 /* Written as R7 with P15 bit 0 set */
18
19/* Conversion routines to/from brg time constants from/to bits
20 * per second.
21 */
22#define BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
23#define BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
24
25/* The Zilog register set */
26
27#define FLAG 0x7e
28
29/* Write Register 0 */
30#define R0 0 /* Register selects */
31#define R1 1
32#define R2 2
33#define R3 3
34#define R4 4
35#define R5 5
36#define R6 6
37#define R7 7
38#define R8 8
39#define R9 9
40#define R10 10
41#define R11 11
42#define R12 12
43#define R13 13
44#define R14 14
45#define R15 15
46
47#define NULLCODE 0 /* Null Code */
48#define POINT_HIGH 0x8 /* Select upper half of registers */
49#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
50#define SEND_ABORT 0x18 /* HDLC Abort */
51#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
52#define RES_Tx_P 0x28 /* Reset TxINT Pending */
53#define ERR_RES 0x30 /* Error Reset */
54#define RES_H_IUS 0x38 /* Reset highest IUS */
55
56#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
57#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
58#define RES_EOM_L 0xC0 /* Reset EOM latch */
59
60/* Write Register 1 */
61
62#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
63#define TxINT_ENAB 0x2 /* Tx Int Enable */
64#define PAR_SPEC 0x4 /* Parity is special condition */
65
66#define RxINT_DISAB 0 /* Rx Int Disable */
67#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
68#define INT_ALL_Rx 0x10 /* Int on all Rx Characters or error */
69#define INT_ERR_Rx 0x18 /* Int on error only */
70#define RxINT_MASK 0x18
71
72#define WT_RDY_RT 0x20 /* Wait/Ready on R/T */
73#define WT_FN_RDYFN 0x40 /* Wait/FN/Ready FN */
74#define WT_RDY_ENAB 0x80 /* Wait/Ready Enable */
75
76/* Write Register #2 (Interrupt Vector) */
77
78/* Write Register 3 */
79
80#define RxENAB 0x1 /* Rx Enable */
81#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
82#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
83#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
84#define ENT_HM 0x10 /* Enter Hunt Mode */
85#define AUTO_ENAB 0x20 /* Auto Enables */
86#define Rx5 0x0 /* Rx 5 Bits/Character */
87#define Rx7 0x40 /* Rx 7 Bits/Character */
88#define Rx6 0x80 /* Rx 6 Bits/Character */
89#define Rx8 0xc0 /* Rx 8 Bits/Character */
90#define RxN_MASK 0xc0
91
92/* Write Register 4 */
93
94#define PAR_ENAB 0x1 /* Parity Enable */
95#define PAR_EVEN 0x2 /* Parity Even/Odd* */
96
97#define SYNC_ENAB 0 /* Sync Modes Enable */
98#define SB1 0x4 /* 1 stop bit/char */
99#define SB15 0x8 /* 1.5 stop bits/char */
100#define SB2 0xc /* 2 stop bits/char */
101
102#define MONSYNC 0 /* 8 Bit Sync character */
103#define BISYNC 0x10 /* 16 bit sync character */
104#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
105#define EXTSYNC 0x30 /* External Sync Mode */
106
107#define X1CLK 0x0 /* x1 clock mode */
108#define X16CLK 0x40 /* x16 clock mode */
109#define X32CLK 0x80 /* x32 clock mode */
110#define X64CLK 0xC0 /* x64 clock mode */
111#define XCLK_MASK 0xC0
112
113/* Write Register 5 */
114
115#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
116#define RTS 0x2 /* RTS */
117#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
118#define TxENAB 0x8 /* Tx Enable */
119#define SND_BRK 0x10 /* Send Break */
120#define Tx5 0x0 /* Tx 5 bits (or less)/character */
121#define Tx7 0x20 /* Tx 7 bits/character */
122#define Tx6 0x40 /* Tx 6 bits/character */
123#define Tx8 0x60 /* Tx 8 bits/character */
124#define TxN_MASK 0x60
125#define DTR 0x80 /* DTR */
126
127/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
128
129/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
130
131/* Write Register 7' (ESCC Only) */
132#define AUTO_TxFLAG 1 /* Automatic Tx SDLC Flag */
133#define AUTO_EOM_RST 2 /* Automatic EOM Reset */
134#define AUTOnRTS 4 /* Automatic /RTS pin deactivation */
135#define RxFIFO_LVL 8 /* Receive FIFO interrupt level */
136#define nDTRnREQ 0x10 /* /DTR/REQ timing */
137#define TxFIFO_LVL 0x20 /* Transmit FIFO interrupt level */
138#define EXT_RD_EN 0x40 /* Extended read register enable */
139
140/* Write Register 8 (transmit buffer) */
141
142/* Write Register 9 (Master interrupt control) */
143#define VIS 1 /* Vector Includes Status */
144#define NV 2 /* No Vector */
145#define DLC 4 /* Disable Lower Chain */
146#define MIE 8 /* Master Interrupt Enable */
147#define STATHI 0x10 /* Status high */
148#define SWIACK 0x20 /* Software Interrupt Ack (not on NMOS) */
149#define NORESET 0 /* No reset on write to R9 */
150#define CHRB 0x40 /* Reset channel B */
151#define CHRA 0x80 /* Reset channel A */
152#define FHWRES 0xc0 /* Force hardware reset */
153
154/* Write Register 10 (misc control bits) */
155#define BIT6 1 /* 6 bit/8bit sync */
156#define LOOPMODE 2 /* SDLC Loop mode */
157#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
158#define MARKIDLE 8 /* Mark/flag on idle */
159#define GAOP 0x10 /* Go active on poll */
160#define NRZ 0 /* NRZ mode */
161#define NRZI 0x20 /* NRZI mode */
162#define FM1 0x40 /* FM1 (transition = 1) */
163#define FM0 0x60 /* FM0 (transition = 0) */
164#define CRCPS 0x80 /* CRC Preset I/O */
165
166/* Write Register 11 (Clock Mode control) */
167#define TRxCXT 0 /* TRxC = Xtal output */
168#define TRxCTC 1 /* TRxC = Transmit clock */
169#define TRxCBR 2 /* TRxC = BR Generator Output */
170#define TRxCDP 3 /* TRxC = DPLL output */
171#define TRxCOI 4 /* TRxC O/I */
172#define TCRTxCP 0 /* Transmit clock = RTxC pin */
173#define TCTRxCP 8 /* Transmit clock = TRxC pin */
174#define TCBR 0x10 /* Transmit clock = BR Generator output */
175#define TCDPLL 0x18 /* Transmit clock = DPLL output */
176#define RCRTxCP 0 /* Receive clock = RTxC pin */
177#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
178#define RCBR 0x40 /* Receive clock = BR Generator output */
179#define RCDPLL 0x60 /* Receive clock = DPLL output */
180#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
181
182/* Write Register 12 (lower byte of baud rate generator time constant) */
183
184/* Write Register 13 (upper byte of baud rate generator time constant) */
185
186/* Write Register 14 (Misc control bits) */
187#define BRENAB 1 /* Baud rate generator enable */
188#define BRSRC 2 /* Baud rate generator source */
189#define DTRREQ 4 /* DTR/Request function */
190#define AUTOECHO 8 /* Auto Echo */
191#define LOOPBAK 0x10 /* Local loopback */
192#define SEARCH 0x20 /* Enter search mode */
193#define RMC 0x40 /* Reset missing clock */
194#define DISDPLL 0x60 /* Disable DPLL */
195#define SSBR 0x80 /* Set DPLL source = BR generator */
196#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
197#define SFMM 0xc0 /* Set FM mode */
198#define SNRZI 0xe0 /* Set NRZI mode */
199
200/* Write Register 15 (external/status interrupt control) */
201#define WR7pEN 1 /* WR7' Enable (ESCC only) */
202#define ZCIE 2 /* Zero count IE */
203#define FIFOEN 4 /* FIFO Enable (ESCC only) */
204#define DCDIE 8 /* DCD IE */
205#define SYNCIE 0x10 /* Sync/hunt IE */
206#define CTSIE 0x20 /* CTS IE */
207#define TxUIE 0x40 /* Tx Underrun/EOM IE */
208#define BRKIE 0x80 /* Break/Abort IE */
209
210
211/* Read Register 0 */
212#define Rx_CH_AV 0x1 /* Rx Character Available */
213#define ZCOUNT 0x2 /* Zero count */
214#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
215#define DCD 0x8 /* DCD */
216#define SYNC 0x10 /* Sync/hunt */
217#define CTS 0x20 /* CTS */
218#define TxEOM 0x40 /* Tx underrun */
219#define BRK_ABRT 0x80 /* Break/Abort */
220
221/* Read Register 1 */
222#define ALL_SNT 0x1 /* All sent */
223/* Residue Data for 8 Rx bits/char programmed */
224#define RES3 0x8 /* 0/3 */
225#define RES4 0x4 /* 0/4 */
226#define RES5 0xc /* 0/5 */
227#define RES6 0x2 /* 0/6 */
228#define RES7 0xa /* 0/7 */
229#define RES8 0x6 /* 0/8 */
230#define RES18 0xe /* 1/8 */
231#define RES28 0x0 /* 2/8 */
232/* Special Rx Condition Interrupts */
233#define PAR_ERR 0x10 /* Parity error */
234#define Rx_OVR 0x20 /* Rx Overrun Error */
235#define CRC_ERR 0x40 /* CRC/Framing Error */
236#define END_FR 0x80 /* End of Frame (SDLC) */
237
238/* Read Register 2 (channel b only) - Interrupt vector */
239#define CHB_Tx_EMPTY 0x00
240#define CHB_EXT_STAT 0x02
241#define CHB_Rx_AVAIL 0x04
242#define CHB_SPECIAL 0x06
243#define CHA_Tx_EMPTY 0x08
244#define CHA_EXT_STAT 0x0a
245#define CHA_Rx_AVAIL 0x0c
246#define CHA_SPECIAL 0x0e
247#define STATUS_MASK 0x0e
248
249/* Read Register 3 (interrupt pending register) ch a only */
250#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
251#define CHBTxIP 0x2 /* Channel B Tx IP */
252#define CHBRxIP 0x4 /* Channel B Rx IP */
253#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
254#define CHATxIP 0x10 /* Channel A Tx IP */
255#define CHARxIP 0x20 /* Channel A Rx IP */
256
257/* Read Register 6 (LSB frame byte count [Not on NMOS]) */
258
259/* Read Register 7 (MSB frame byte count and FIFO status [Not on NMOS]) */
260
261/* Read Register 8 (receive data register) */
262
263/* Read Register 10 (misc status bits) */
264#define ONLOOP 2 /* On loop */
265#define LOOPSEND 0x10 /* Loop sending */
266#define CLK2MIS 0x40 /* Two clocks missing */
267#define CLK1MIS 0x80 /* One clock missing */
268
269/* Read Register 12 (lower byte of baud rate generator constant) */
270
271/* Read Register 13 (upper byte of baud rate generator constant) */
272
273/* Read Register 15 (value of WR 15) */
274
275/* Misc macros */
276#define ZS_CLEARERR(channel) do { sbus_writeb(ERR_RES, &channel->control); \
277 udelay(5); } while(0)
278
279#define ZS_CLEARSTAT(channel) do { sbus_writeb(RES_EXT_INT, &channel->control); \
280 udelay(5); } while(0)
281
282#define ZS_CLEARFIFO(channel) do { sbus_readb(&channel->data); \
283 udelay(2); \
284 sbus_readb(&channel->data); \
285 udelay(2); \
286 sbus_readb(&channel->data); \
287 udelay(2); } while(0)
288
289#endif /* _SUNZILOG_H */
diff --git a/drivers/tty/serial/timbuart.c b/drivers/tty/serial/timbuart.c
new file mode 100644
index 000000000000..1f36b7eb7351
--- /dev/null
+++ b/drivers/tty/serial/timbuart.c
@@ -0,0 +1,531 @@
1/*
2 * timbuart.c timberdale FPGA UART driver
3 * Copyright (c) 2009 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* Supports:
20 * Timberdale FPGA UART
21 */
22
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/serial_core.h>
26#include <linux/kernel.h>
27#include <linux/platform_device.h>
28#include <linux/ioport.h>
29#include <linux/slab.h>
30
31#include "timbuart.h"
32
33struct timbuart_port {
34 struct uart_port port;
35 struct tasklet_struct tasklet;
36 int usedma;
37 u32 last_ier;
38 struct platform_device *dev;
39};
40
41static int baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 460800,
42 921600, 1843200, 3250000};
43
44static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier);
45
46static irqreturn_t timbuart_handleinterrupt(int irq, void *devid);
47
48static void timbuart_stop_rx(struct uart_port *port)
49{
50 /* spin lock held by upper layer, disable all RX interrupts */
51 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~RXFLAGS;
52 iowrite32(ier, port->membase + TIMBUART_IER);
53}
54
55static void timbuart_stop_tx(struct uart_port *port)
56{
57 /* spinlock held by upper layer, disable TX interrupt */
58 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~TXBAE;
59 iowrite32(ier, port->membase + TIMBUART_IER);
60}
61
62static void timbuart_start_tx(struct uart_port *port)
63{
64 struct timbuart_port *uart =
65 container_of(port, struct timbuart_port, port);
66
67 /* do not transfer anything here -> fire off the tasklet */
68 tasklet_schedule(&uart->tasklet);
69}
70
71static unsigned int timbuart_tx_empty(struct uart_port *port)
72{
73 u32 isr = ioread32(port->membase + TIMBUART_ISR);
74
75 return (isr & TXBE) ? TIOCSER_TEMT : 0;
76}
77
78static void timbuart_flush_buffer(struct uart_port *port)
79{
80 if (!timbuart_tx_empty(port)) {
81 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
82 TIMBUART_CTRL_FLSHTX;
83
84 iowrite8(ctl, port->membase + TIMBUART_CTRL);
85 iowrite32(TXBF, port->membase + TIMBUART_ISR);
86 }
87}
88
89static void timbuart_rx_chars(struct uart_port *port)
90{
91 struct tty_struct *tty = port->state->port.tty;
92
93 while (ioread32(port->membase + TIMBUART_ISR) & RXDP) {
94 u8 ch = ioread8(port->membase + TIMBUART_RXFIFO);
95 port->icount.rx++;
96 tty_insert_flip_char(tty, ch, TTY_NORMAL);
97 }
98
99 spin_unlock(&port->lock);
100 tty_flip_buffer_push(port->state->port.tty);
101 spin_lock(&port->lock);
102
103 dev_dbg(port->dev, "%s - total read %d bytes\n",
104 __func__, port->icount.rx);
105}
106
107static void timbuart_tx_chars(struct uart_port *port)
108{
109 struct circ_buf *xmit = &port->state->xmit;
110
111 while (!(ioread32(port->membase + TIMBUART_ISR) & TXBF) &&
112 !uart_circ_empty(xmit)) {
113 iowrite8(xmit->buf[xmit->tail],
114 port->membase + TIMBUART_TXFIFO);
115 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
116 port->icount.tx++;
117 }
118
119 dev_dbg(port->dev,
120 "%s - total written %d bytes, CTL: %x, RTS: %x, baud: %x\n",
121 __func__,
122 port->icount.tx,
123 ioread8(port->membase + TIMBUART_CTRL),
124 port->mctrl & TIOCM_RTS,
125 ioread8(port->membase + TIMBUART_BAUDRATE));
126}
127
128static void timbuart_handle_tx_port(struct uart_port *port, u32 isr, u32 *ier)
129{
130 struct timbuart_port *uart =
131 container_of(port, struct timbuart_port, port);
132 struct circ_buf *xmit = &port->state->xmit;
133
134 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
135 return;
136
137 if (port->x_char)
138 return;
139
140 if (isr & TXFLAGS) {
141 timbuart_tx_chars(port);
142 /* clear all TX interrupts */
143 iowrite32(TXFLAGS, port->membase + TIMBUART_ISR);
144
145 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
146 uart_write_wakeup(port);
147 } else
148 /* Re-enable any tx interrupt */
149 *ier |= uart->last_ier & TXFLAGS;
150
151 /* enable interrupts if there are chars in the transmit buffer,
152 * Or if we delivered some bytes and want the almost empty interrupt
153 * we wake up the upper layer later when we got the interrupt
154 * to give it some time to go out...
155 */
156 if (!uart_circ_empty(xmit))
157 *ier |= TXBAE;
158
159 dev_dbg(port->dev, "%s - leaving\n", __func__);
160}
161
162void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier)
163{
164 if (isr & RXFLAGS) {
165 /* Some RX status is set */
166 if (isr & RXBF) {
167 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
168 TIMBUART_CTRL_FLSHRX;
169 iowrite8(ctl, port->membase + TIMBUART_CTRL);
170 port->icount.overrun++;
171 } else if (isr & (RXDP))
172 timbuart_rx_chars(port);
173
174 /* ack all RX interrupts */
175 iowrite32(RXFLAGS, port->membase + TIMBUART_ISR);
176 }
177
178 /* always have the RX interrupts enabled */
179 *ier |= RXBAF | RXBF | RXTT;
180
181 dev_dbg(port->dev, "%s - leaving\n", __func__);
182}
183
184void timbuart_tasklet(unsigned long arg)
185{
186 struct timbuart_port *uart = (struct timbuart_port *)arg;
187 u32 isr, ier = 0;
188
189 spin_lock(&uart->port.lock);
190
191 isr = ioread32(uart->port.membase + TIMBUART_ISR);
192 dev_dbg(uart->port.dev, "%s ISR: %x\n", __func__, isr);
193
194 if (!uart->usedma)
195 timbuart_handle_tx_port(&uart->port, isr, &ier);
196
197 timbuart_mctrl_check(&uart->port, isr, &ier);
198
199 if (!uart->usedma)
200 timbuart_handle_rx_port(&uart->port, isr, &ier);
201
202 iowrite32(ier, uart->port.membase + TIMBUART_IER);
203
204 spin_unlock(&uart->port.lock);
205 dev_dbg(uart->port.dev, "%s leaving\n", __func__);
206}
207
208static unsigned int timbuart_get_mctrl(struct uart_port *port)
209{
210 u8 cts = ioread8(port->membase + TIMBUART_CTRL);
211 dev_dbg(port->dev, "%s - cts %x\n", __func__, cts);
212
213 if (cts & TIMBUART_CTRL_CTS)
214 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
215 else
216 return TIOCM_DSR | TIOCM_CAR;
217}
218
219static void timbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
220{
221 dev_dbg(port->dev, "%s - %x\n", __func__, mctrl);
222
223 if (mctrl & TIOCM_RTS)
224 iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL);
225 else
226 iowrite8(0, port->membase + TIMBUART_CTRL);
227}
228
229static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier)
230{
231 unsigned int cts;
232
233 if (isr & CTS_DELTA) {
234 /* ack */
235 iowrite32(CTS_DELTA, port->membase + TIMBUART_ISR);
236 cts = timbuart_get_mctrl(port);
237 uart_handle_cts_change(port, cts & TIOCM_CTS);
238 wake_up_interruptible(&port->state->port.delta_msr_wait);
239 }
240
241 *ier |= CTS_DELTA;
242}
243
244static void timbuart_enable_ms(struct uart_port *port)
245{
246 /* N/A */
247}
248
249static void timbuart_break_ctl(struct uart_port *port, int ctl)
250{
251 /* N/A */
252}
253
254static int timbuart_startup(struct uart_port *port)
255{
256 struct timbuart_port *uart =
257 container_of(port, struct timbuart_port, port);
258
259 dev_dbg(port->dev, "%s\n", __func__);
260
261 iowrite8(TIMBUART_CTRL_FLSHRX, port->membase + TIMBUART_CTRL);
262 iowrite32(0x1ff, port->membase + TIMBUART_ISR);
263 /* Enable all but TX interrupts */
264 iowrite32(RXBAF | RXBF | RXTT | CTS_DELTA,
265 port->membase + TIMBUART_IER);
266
267 return request_irq(port->irq, timbuart_handleinterrupt, IRQF_SHARED,
268 "timb-uart", uart);
269}
270
271static void timbuart_shutdown(struct uart_port *port)
272{
273 struct timbuart_port *uart =
274 container_of(port, struct timbuart_port, port);
275 dev_dbg(port->dev, "%s\n", __func__);
276 free_irq(port->irq, uart);
277 iowrite32(0, port->membase + TIMBUART_IER);
278}
279
280static int get_bindex(int baud)
281{
282 int i;
283
284 for (i = 0; i < ARRAY_SIZE(baudrates); i++)
285 if (baud <= baudrates[i])
286 return i;
287
288 return -1;
289}
290
291static void timbuart_set_termios(struct uart_port *port,
292 struct ktermios *termios,
293 struct ktermios *old)
294{
295 unsigned int baud;
296 short bindex;
297 unsigned long flags;
298
299 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
300 bindex = get_bindex(baud);
301 dev_dbg(port->dev, "%s - bindex %d\n", __func__, bindex);
302
303 if (bindex < 0)
304 bindex = 0;
305 baud = baudrates[bindex];
306
307 /* The serial layer calls into this once with old = NULL when setting
308 up initially */
309 if (old)
310 tty_termios_copy_hw(termios, old);
311 tty_termios_encode_baud_rate(termios, baud, baud);
312
313 spin_lock_irqsave(&port->lock, flags);
314 iowrite8((u8)bindex, port->membase + TIMBUART_BAUDRATE);
315 uart_update_timeout(port, termios->c_cflag, baud);
316 spin_unlock_irqrestore(&port->lock, flags);
317}
318
319static const char *timbuart_type(struct uart_port *port)
320{
321 return port->type == PORT_UNKNOWN ? "timbuart" : NULL;
322}
323
324/* We do not request/release mappings of the registers here,
325 * currently it's done in the proble function.
326 */
327static void timbuart_release_port(struct uart_port *port)
328{
329 struct platform_device *pdev = to_platform_device(port->dev);
330 int size =
331 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
332
333 if (port->flags & UPF_IOREMAP) {
334 iounmap(port->membase);
335 port->membase = NULL;
336 }
337
338 release_mem_region(port->mapbase, size);
339}
340
341static int timbuart_request_port(struct uart_port *port)
342{
343 struct platform_device *pdev = to_platform_device(port->dev);
344 int size =
345 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
346
347 if (!request_mem_region(port->mapbase, size, "timb-uart"))
348 return -EBUSY;
349
350 if (port->flags & UPF_IOREMAP) {
351 port->membase = ioremap(port->mapbase, size);
352 if (port->membase == NULL) {
353 release_mem_region(port->mapbase, size);
354 return -ENOMEM;
355 }
356 }
357
358 return 0;
359}
360
361static irqreturn_t timbuart_handleinterrupt(int irq, void *devid)
362{
363 struct timbuart_port *uart = (struct timbuart_port *)devid;
364
365 if (ioread8(uart->port.membase + TIMBUART_IPR)) {
366 uart->last_ier = ioread32(uart->port.membase + TIMBUART_IER);
367
368 /* disable interrupts, the tasklet enables them again */
369 iowrite32(0, uart->port.membase + TIMBUART_IER);
370
371 /* fire off bottom half */
372 tasklet_schedule(&uart->tasklet);
373
374 return IRQ_HANDLED;
375 } else
376 return IRQ_NONE;
377}
378
379/*
380 * Configure/autoconfigure the port.
381 */
382static void timbuart_config_port(struct uart_port *port, int flags)
383{
384 if (flags & UART_CONFIG_TYPE) {
385 port->type = PORT_TIMBUART;
386 timbuart_request_port(port);
387 }
388}
389
390static int timbuart_verify_port(struct uart_port *port,
391 struct serial_struct *ser)
392{
393 /* we don't want the core code to modify any port params */
394 return -EINVAL;
395}
396
397static struct uart_ops timbuart_ops = {
398 .tx_empty = timbuart_tx_empty,
399 .set_mctrl = timbuart_set_mctrl,
400 .get_mctrl = timbuart_get_mctrl,
401 .stop_tx = timbuart_stop_tx,
402 .start_tx = timbuart_start_tx,
403 .flush_buffer = timbuart_flush_buffer,
404 .stop_rx = timbuart_stop_rx,
405 .enable_ms = timbuart_enable_ms,
406 .break_ctl = timbuart_break_ctl,
407 .startup = timbuart_startup,
408 .shutdown = timbuart_shutdown,
409 .set_termios = timbuart_set_termios,
410 .type = timbuart_type,
411 .release_port = timbuart_release_port,
412 .request_port = timbuart_request_port,
413 .config_port = timbuart_config_port,
414 .verify_port = timbuart_verify_port
415};
416
417static struct uart_driver timbuart_driver = {
418 .owner = THIS_MODULE,
419 .driver_name = "timberdale_uart",
420 .dev_name = "ttyTU",
421 .major = TIMBUART_MAJOR,
422 .minor = TIMBUART_MINOR,
423 .nr = 1
424};
425
426static int __devinit timbuart_probe(struct platform_device *dev)
427{
428 int err, irq;
429 struct timbuart_port *uart;
430 struct resource *iomem;
431
432 dev_dbg(&dev->dev, "%s\n", __func__);
433
434 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
435 if (!uart) {
436 err = -EINVAL;
437 goto err_mem;
438 }
439
440 uart->usedma = 0;
441
442 uart->port.uartclk = 3250000 * 16;
443 uart->port.fifosize = TIMBUART_FIFO_SIZE;
444 uart->port.regshift = 2;
445 uart->port.iotype = UPIO_MEM;
446 uart->port.ops = &timbuart_ops;
447 uart->port.irq = 0;
448 uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
449 uart->port.line = 0;
450 uart->port.dev = &dev->dev;
451
452 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
453 if (!iomem) {
454 err = -ENOMEM;
455 goto err_register;
456 }
457 uart->port.mapbase = iomem->start;
458 uart->port.membase = NULL;
459
460 irq = platform_get_irq(dev, 0);
461 if (irq < 0) {
462 err = -EINVAL;
463 goto err_register;
464 }
465 uart->port.irq = irq;
466
467 tasklet_init(&uart->tasklet, timbuart_tasklet, (unsigned long)uart);
468
469 err = uart_register_driver(&timbuart_driver);
470 if (err)
471 goto err_register;
472
473 err = uart_add_one_port(&timbuart_driver, &uart->port);
474 if (err)
475 goto err_add_port;
476
477 platform_set_drvdata(dev, uart);
478
479 return 0;
480
481err_add_port:
482 uart_unregister_driver(&timbuart_driver);
483err_register:
484 kfree(uart);
485err_mem:
486 printk(KERN_ERR "timberdale: Failed to register Timberdale UART: %d\n",
487 err);
488
489 return err;
490}
491
492static int __devexit timbuart_remove(struct platform_device *dev)
493{
494 struct timbuart_port *uart = platform_get_drvdata(dev);
495
496 tasklet_kill(&uart->tasklet);
497 uart_remove_one_port(&timbuart_driver, &uart->port);
498 uart_unregister_driver(&timbuart_driver);
499 kfree(uart);
500
501 return 0;
502}
503
504static struct platform_driver timbuart_platform_driver = {
505 .driver = {
506 .name = "timb-uart",
507 .owner = THIS_MODULE,
508 },
509 .probe = timbuart_probe,
510 .remove = __devexit_p(timbuart_remove),
511};
512
513/*--------------------------------------------------------------------------*/
514
515static int __init timbuart_init(void)
516{
517 return platform_driver_register(&timbuart_platform_driver);
518}
519
520static void __exit timbuart_exit(void)
521{
522 platform_driver_unregister(&timbuart_platform_driver);
523}
524
525module_init(timbuart_init);
526module_exit(timbuart_exit);
527
528MODULE_DESCRIPTION("Timberdale UART driver");
529MODULE_LICENSE("GPL v2");
530MODULE_ALIAS("platform:timb-uart");
531
diff --git a/drivers/tty/serial/timbuart.h b/drivers/tty/serial/timbuart.h
new file mode 100644
index 000000000000..7e566766bc43
--- /dev/null
+++ b/drivers/tty/serial/timbuart.h
@@ -0,0 +1,58 @@
1/*
2 * timbuart.c timberdale FPGA GPIO driver
3 * Copyright (c) 2009 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* Supports:
20 * Timberdale FPGA UART
21 */
22
23#ifndef _TIMBUART_H
24#define _TIMBUART_H
25
26#define TIMBUART_FIFO_SIZE 2048
27
28#define TIMBUART_RXFIFO 0x08
29#define TIMBUART_TXFIFO 0x0c
30#define TIMBUART_IER 0x10
31#define TIMBUART_IPR 0x14
32#define TIMBUART_ISR 0x18
33#define TIMBUART_CTRL 0x1c
34#define TIMBUART_BAUDRATE 0x20
35
36#define TIMBUART_CTRL_RTS 0x01
37#define TIMBUART_CTRL_CTS 0x02
38#define TIMBUART_CTRL_FLSHTX 0x40
39#define TIMBUART_CTRL_FLSHRX 0x80
40
41#define TXBF 0x01
42#define TXBAE 0x02
43#define CTS_DELTA 0x04
44#define RXDP 0x08
45#define RXBAF 0x10
46#define RXBF 0x20
47#define RXTT 0x40
48#define RXBNAE 0x80
49#define TXBE 0x100
50
51#define RXFLAGS (RXDP | RXBAF | RXBF | RXTT | RXBNAE)
52#define TXFLAGS (TXBF | TXBAE)
53
54#define TIMBUART_MAJOR 204
55#define TIMBUART_MINOR 192
56
57#endif /* _TIMBUART_H */
58
diff --git a/drivers/tty/serial/uartlite.c b/drivers/tty/serial/uartlite.c
new file mode 100644
index 000000000000..d2fce865b731
--- /dev/null
+++ b/drivers/tty/serial/uartlite.c
@@ -0,0 +1,709 @@
1/*
2 * uartlite.c: Serial driver for Xilinx uartlite serial controller
3 *
4 * Copyright (C) 2006 Peter Korsgaard <jacmet@sunsite.dk>
5 * Copyright (C) 2007 Secret Lab Technologies Ltd.
6 *
7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied.
10 */
11
12#include <linux/platform_device.h>
13#include <linux/module.h>
14#include <linux/console.h>
15#include <linux/serial.h>
16#include <linux/serial_core.h>
17#include <linux/tty.h>
18#include <linux/delay.h>
19#include <linux/interrupt.h>
20#include <linux/init.h>
21#include <asm/io.h>
22#if defined(CONFIG_OF) && (defined(CONFIG_PPC32) || defined(CONFIG_MICROBLAZE))
23#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/of_device.h>
26#include <linux/of_platform.h>
27
28/* Match table for of_platform binding */
29static struct of_device_id ulite_of_match[] __devinitdata = {
30 { .compatible = "xlnx,opb-uartlite-1.00.b", },
31 { .compatible = "xlnx,xps-uartlite-1.00.a", },
32 {}
33};
34MODULE_DEVICE_TABLE(of, ulite_of_match);
35
36#endif
37
38#define ULITE_NAME "ttyUL"
39#define ULITE_MAJOR 204
40#define ULITE_MINOR 187
41#define ULITE_NR_UARTS 4
42
43/* ---------------------------------------------------------------------
44 * Register definitions
45 *
46 * For register details see datasheet:
47 * http://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
48 */
49
50#define ULITE_RX 0x00
51#define ULITE_TX 0x04
52#define ULITE_STATUS 0x08
53#define ULITE_CONTROL 0x0c
54
55#define ULITE_REGION 16
56
57#define ULITE_STATUS_RXVALID 0x01
58#define ULITE_STATUS_RXFULL 0x02
59#define ULITE_STATUS_TXEMPTY 0x04
60#define ULITE_STATUS_TXFULL 0x08
61#define ULITE_STATUS_IE 0x10
62#define ULITE_STATUS_OVERRUN 0x20
63#define ULITE_STATUS_FRAME 0x40
64#define ULITE_STATUS_PARITY 0x80
65
66#define ULITE_CONTROL_RST_TX 0x01
67#define ULITE_CONTROL_RST_RX 0x02
68#define ULITE_CONTROL_IE 0x10
69
70
71static struct uart_port ulite_ports[ULITE_NR_UARTS];
72
73/* ---------------------------------------------------------------------
74 * Core UART driver operations
75 */
76
77static int ulite_receive(struct uart_port *port, int stat)
78{
79 struct tty_struct *tty = port->state->port.tty;
80 unsigned char ch = 0;
81 char flag = TTY_NORMAL;
82
83 if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
84 | ULITE_STATUS_FRAME)) == 0)
85 return 0;
86
87 /* stats */
88 if (stat & ULITE_STATUS_RXVALID) {
89 port->icount.rx++;
90 ch = ioread32be(port->membase + ULITE_RX);
91
92 if (stat & ULITE_STATUS_PARITY)
93 port->icount.parity++;
94 }
95
96 if (stat & ULITE_STATUS_OVERRUN)
97 port->icount.overrun++;
98
99 if (stat & ULITE_STATUS_FRAME)
100 port->icount.frame++;
101
102
103 /* drop byte with parity error if IGNPAR specificed */
104 if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
105 stat &= ~ULITE_STATUS_RXVALID;
106
107 stat &= port->read_status_mask;
108
109 if (stat & ULITE_STATUS_PARITY)
110 flag = TTY_PARITY;
111
112
113 stat &= ~port->ignore_status_mask;
114
115 if (stat & ULITE_STATUS_RXVALID)
116 tty_insert_flip_char(tty, ch, flag);
117
118 if (stat & ULITE_STATUS_FRAME)
119 tty_insert_flip_char(tty, 0, TTY_FRAME);
120
121 if (stat & ULITE_STATUS_OVERRUN)
122 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
123
124 return 1;
125}
126
127static int ulite_transmit(struct uart_port *port, int stat)
128{
129 struct circ_buf *xmit = &port->state->xmit;
130
131 if (stat & ULITE_STATUS_TXFULL)
132 return 0;
133
134 if (port->x_char) {
135 iowrite32be(port->x_char, port->membase + ULITE_TX);
136 port->x_char = 0;
137 port->icount.tx++;
138 return 1;
139 }
140
141 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
142 return 0;
143
144 iowrite32be(xmit->buf[xmit->tail], port->membase + ULITE_TX);
145 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
146 port->icount.tx++;
147
148 /* wake up */
149 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
150 uart_write_wakeup(port);
151
152 return 1;
153}
154
155static irqreturn_t ulite_isr(int irq, void *dev_id)
156{
157 struct uart_port *port = dev_id;
158 int busy, n = 0;
159
160 do {
161 int stat = ioread32be(port->membase + ULITE_STATUS);
162 busy = ulite_receive(port, stat);
163 busy |= ulite_transmit(port, stat);
164 n++;
165 } while (busy);
166
167 /* work done? */
168 if (n > 1) {
169 tty_flip_buffer_push(port->state->port.tty);
170 return IRQ_HANDLED;
171 } else {
172 return IRQ_NONE;
173 }
174}
175
176static unsigned int ulite_tx_empty(struct uart_port *port)
177{
178 unsigned long flags;
179 unsigned int ret;
180
181 spin_lock_irqsave(&port->lock, flags);
182 ret = ioread32be(port->membase + ULITE_STATUS);
183 spin_unlock_irqrestore(&port->lock, flags);
184
185 return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
186}
187
188static unsigned int ulite_get_mctrl(struct uart_port *port)
189{
190 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
191}
192
193static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
194{
195 /* N/A */
196}
197
198static void ulite_stop_tx(struct uart_port *port)
199{
200 /* N/A */
201}
202
203static void ulite_start_tx(struct uart_port *port)
204{
205 ulite_transmit(port, ioread32be(port->membase + ULITE_STATUS));
206}
207
208static void ulite_stop_rx(struct uart_port *port)
209{
210 /* don't forward any more data (like !CREAD) */
211 port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
212 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
213}
214
215static void ulite_enable_ms(struct uart_port *port)
216{
217 /* N/A */
218}
219
220static void ulite_break_ctl(struct uart_port *port, int ctl)
221{
222 /* N/A */
223}
224
225static int ulite_startup(struct uart_port *port)
226{
227 int ret;
228
229 ret = request_irq(port->irq, ulite_isr,
230 IRQF_SHARED | IRQF_SAMPLE_RANDOM, "uartlite", port);
231 if (ret)
232 return ret;
233
234 iowrite32be(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
235 port->membase + ULITE_CONTROL);
236 iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
237
238 return 0;
239}
240
241static void ulite_shutdown(struct uart_port *port)
242{
243 iowrite32be(0, port->membase + ULITE_CONTROL);
244 ioread32be(port->membase + ULITE_CONTROL); /* dummy */
245 free_irq(port->irq, port);
246}
247
248static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
249 struct ktermios *old)
250{
251 unsigned long flags;
252 unsigned int baud;
253
254 spin_lock_irqsave(&port->lock, flags);
255
256 port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
257 | ULITE_STATUS_TXFULL;
258
259 if (termios->c_iflag & INPCK)
260 port->read_status_mask |=
261 ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
262
263 port->ignore_status_mask = 0;
264 if (termios->c_iflag & IGNPAR)
265 port->ignore_status_mask |= ULITE_STATUS_PARITY
266 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
267
268 /* ignore all characters if CREAD is not set */
269 if ((termios->c_cflag & CREAD) == 0)
270 port->ignore_status_mask |=
271 ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
272 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
273
274 /* update timeout */
275 baud = uart_get_baud_rate(port, termios, old, 0, 460800);
276 uart_update_timeout(port, termios->c_cflag, baud);
277
278 spin_unlock_irqrestore(&port->lock, flags);
279}
280
281static const char *ulite_type(struct uart_port *port)
282{
283 return port->type == PORT_UARTLITE ? "uartlite" : NULL;
284}
285
286static void ulite_release_port(struct uart_port *port)
287{
288 release_mem_region(port->mapbase, ULITE_REGION);
289 iounmap(port->membase);
290 port->membase = NULL;
291}
292
293static int ulite_request_port(struct uart_port *port)
294{
295 pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
296 port, (unsigned long long) port->mapbase);
297
298 if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
299 dev_err(port->dev, "Memory region busy\n");
300 return -EBUSY;
301 }
302
303 port->membase = ioremap(port->mapbase, ULITE_REGION);
304 if (!port->membase) {
305 dev_err(port->dev, "Unable to map registers\n");
306 release_mem_region(port->mapbase, ULITE_REGION);
307 return -EBUSY;
308 }
309
310 return 0;
311}
312
313static void ulite_config_port(struct uart_port *port, int flags)
314{
315 if (!ulite_request_port(port))
316 port->type = PORT_UARTLITE;
317}
318
319static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
320{
321 /* we don't want the core code to modify any port params */
322 return -EINVAL;
323}
324
325#ifdef CONFIG_CONSOLE_POLL
326static int ulite_get_poll_char(struct uart_port *port)
327{
328 if (!(ioread32be(port->membase + ULITE_STATUS)
329 & ULITE_STATUS_RXVALID))
330 return NO_POLL_CHAR;
331
332 return ioread32be(port->membase + ULITE_RX);
333}
334
335static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
336{
337 while (ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_TXFULL)
338 cpu_relax();
339
340 /* write char to device */
341 iowrite32be(ch, port->membase + ULITE_TX);
342}
343#endif
344
345static struct uart_ops ulite_ops = {
346 .tx_empty = ulite_tx_empty,
347 .set_mctrl = ulite_set_mctrl,
348 .get_mctrl = ulite_get_mctrl,
349 .stop_tx = ulite_stop_tx,
350 .start_tx = ulite_start_tx,
351 .stop_rx = ulite_stop_rx,
352 .enable_ms = ulite_enable_ms,
353 .break_ctl = ulite_break_ctl,
354 .startup = ulite_startup,
355 .shutdown = ulite_shutdown,
356 .set_termios = ulite_set_termios,
357 .type = ulite_type,
358 .release_port = ulite_release_port,
359 .request_port = ulite_request_port,
360 .config_port = ulite_config_port,
361 .verify_port = ulite_verify_port,
362#ifdef CONFIG_CONSOLE_POLL
363 .poll_get_char = ulite_get_poll_char,
364 .poll_put_char = ulite_put_poll_char,
365#endif
366};
367
368/* ---------------------------------------------------------------------
369 * Console driver operations
370 */
371
372#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
373static void ulite_console_wait_tx(struct uart_port *port)
374{
375 int i;
376 u8 val;
377
378 /* Spin waiting for TX fifo to have space available */
379 for (i = 0; i < 100000; i++) {
380 val = ioread32be(port->membase + ULITE_STATUS);
381 if ((val & ULITE_STATUS_TXFULL) == 0)
382 break;
383 cpu_relax();
384 }
385}
386
387static void ulite_console_putchar(struct uart_port *port, int ch)
388{
389 ulite_console_wait_tx(port);
390 iowrite32be(ch, port->membase + ULITE_TX);
391}
392
393static void ulite_console_write(struct console *co, const char *s,
394 unsigned int count)
395{
396 struct uart_port *port = &ulite_ports[co->index];
397 unsigned long flags;
398 unsigned int ier;
399 int locked = 1;
400
401 if (oops_in_progress) {
402 locked = spin_trylock_irqsave(&port->lock, flags);
403 } else
404 spin_lock_irqsave(&port->lock, flags);
405
406 /* save and disable interrupt */
407 ier = ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_IE;
408 iowrite32be(0, port->membase + ULITE_CONTROL);
409
410 uart_console_write(port, s, count, ulite_console_putchar);
411
412 ulite_console_wait_tx(port);
413
414 /* restore interrupt state */
415 if (ier)
416 iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
417
418 if (locked)
419 spin_unlock_irqrestore(&port->lock, flags);
420}
421
422static int __devinit ulite_console_setup(struct console *co, char *options)
423{
424 struct uart_port *port;
425 int baud = 9600;
426 int bits = 8;
427 int parity = 'n';
428 int flow = 'n';
429
430 if (co->index < 0 || co->index >= ULITE_NR_UARTS)
431 return -EINVAL;
432
433 port = &ulite_ports[co->index];
434
435 /* Has the device been initialized yet? */
436 if (!port->mapbase) {
437 pr_debug("console on ttyUL%i not present\n", co->index);
438 return -ENODEV;
439 }
440
441 /* not initialized yet? */
442 if (!port->membase) {
443 if (ulite_request_port(port))
444 return -ENODEV;
445 }
446
447 if (options)
448 uart_parse_options(options, &baud, &parity, &bits, &flow);
449
450 return uart_set_options(port, co, baud, parity, bits, flow);
451}
452
453static struct uart_driver ulite_uart_driver;
454
455static struct console ulite_console = {
456 .name = ULITE_NAME,
457 .write = ulite_console_write,
458 .device = uart_console_device,
459 .setup = ulite_console_setup,
460 .flags = CON_PRINTBUFFER,
461 .index = -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
462 .data = &ulite_uart_driver,
463};
464
465static int __init ulite_console_init(void)
466{
467 register_console(&ulite_console);
468 return 0;
469}
470
471console_initcall(ulite_console_init);
472
473#endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
474
475static struct uart_driver ulite_uart_driver = {
476 .owner = THIS_MODULE,
477 .driver_name = "uartlite",
478 .dev_name = ULITE_NAME,
479 .major = ULITE_MAJOR,
480 .minor = ULITE_MINOR,
481 .nr = ULITE_NR_UARTS,
482#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
483 .cons = &ulite_console,
484#endif
485};
486
487/* ---------------------------------------------------------------------
488 * Port assignment functions (mapping devices to uart_port structures)
489 */
490
491/** ulite_assign: register a uartlite device with the driver
492 *
493 * @dev: pointer to device structure
494 * @id: requested id number. Pass -1 for automatic port assignment
495 * @base: base address of uartlite registers
496 * @irq: irq number for uartlite
497 *
498 * Returns: 0 on success, <0 otherwise
499 */
500static int __devinit ulite_assign(struct device *dev, int id, u32 base, int irq)
501{
502 struct uart_port *port;
503 int rc;
504
505 /* if id = -1; then scan for a free id and use that */
506 if (id < 0) {
507 for (id = 0; id < ULITE_NR_UARTS; id++)
508 if (ulite_ports[id].mapbase == 0)
509 break;
510 }
511 if (id < 0 || id >= ULITE_NR_UARTS) {
512 dev_err(dev, "%s%i too large\n", ULITE_NAME, id);
513 return -EINVAL;
514 }
515
516 if ((ulite_ports[id].mapbase) && (ulite_ports[id].mapbase != base)) {
517 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
518 ULITE_NAME, id);
519 return -EBUSY;
520 }
521
522 port = &ulite_ports[id];
523
524 spin_lock_init(&port->lock);
525 port->fifosize = 16;
526 port->regshift = 2;
527 port->iotype = UPIO_MEM;
528 port->iobase = 1; /* mark port in use */
529 port->mapbase = base;
530 port->membase = NULL;
531 port->ops = &ulite_ops;
532 port->irq = irq;
533 port->flags = UPF_BOOT_AUTOCONF;
534 port->dev = dev;
535 port->type = PORT_UNKNOWN;
536 port->line = id;
537
538 dev_set_drvdata(dev, port);
539
540 /* Register the port */
541 rc = uart_add_one_port(&ulite_uart_driver, port);
542 if (rc) {
543 dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
544 port->mapbase = 0;
545 dev_set_drvdata(dev, NULL);
546 return rc;
547 }
548
549 return 0;
550}
551
552/** ulite_release: register a uartlite device with the driver
553 *
554 * @dev: pointer to device structure
555 */
556static int __devexit ulite_release(struct device *dev)
557{
558 struct uart_port *port = dev_get_drvdata(dev);
559 int rc = 0;
560
561 if (port) {
562 rc = uart_remove_one_port(&ulite_uart_driver, port);
563 dev_set_drvdata(dev, NULL);
564 port->mapbase = 0;
565 }
566
567 return rc;
568}
569
570/* ---------------------------------------------------------------------
571 * Platform bus binding
572 */
573
574static int __devinit ulite_probe(struct platform_device *pdev)
575{
576 struct resource *res, *res2;
577
578 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
579 if (!res)
580 return -ENODEV;
581
582 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
583 if (!res2)
584 return -ENODEV;
585
586 return ulite_assign(&pdev->dev, pdev->id, res->start, res2->start);
587}
588
589static int __devexit ulite_remove(struct platform_device *pdev)
590{
591 return ulite_release(&pdev->dev);
592}
593
594/* work with hotplug and coldplug */
595MODULE_ALIAS("platform:uartlite");
596
597static struct platform_driver ulite_platform_driver = {
598 .probe = ulite_probe,
599 .remove = __devexit_p(ulite_remove),
600 .driver = {
601 .owner = THIS_MODULE,
602 .name = "uartlite",
603 },
604};
605
606/* ---------------------------------------------------------------------
607 * OF bus bindings
608 */
609#if defined(CONFIG_OF) && (defined(CONFIG_PPC32) || defined(CONFIG_MICROBLAZE))
610static int __devinit
611ulite_of_probe(struct platform_device *op, const struct of_device_id *match)
612{
613 struct resource res;
614 const unsigned int *id;
615 int irq, rc;
616
617 dev_dbg(&op->dev, "%s(%p, %p)\n", __func__, op, match);
618
619 rc = of_address_to_resource(op->dev.of_node, 0, &res);
620 if (rc) {
621 dev_err(&op->dev, "invalid address\n");
622 return rc;
623 }
624
625 irq = irq_of_parse_and_map(op->dev.of_node, 0);
626
627 id = of_get_property(op->dev.of_node, "port-number", NULL);
628
629 return ulite_assign(&op->dev, id ? *id : -1, res.start, irq);
630}
631
632static int __devexit ulite_of_remove(struct platform_device *op)
633{
634 return ulite_release(&op->dev);
635}
636
637static struct of_platform_driver ulite_of_driver = {
638 .probe = ulite_of_probe,
639 .remove = __devexit_p(ulite_of_remove),
640 .driver = {
641 .name = "uartlite",
642 .owner = THIS_MODULE,
643 .of_match_table = ulite_of_match,
644 },
645};
646
647/* Registration helpers to keep the number of #ifdefs to a minimum */
648static inline int __init ulite_of_register(void)
649{
650 pr_debug("uartlite: calling of_register_platform_driver()\n");
651 return of_register_platform_driver(&ulite_of_driver);
652}
653
654static inline void __exit ulite_of_unregister(void)
655{
656 of_unregister_platform_driver(&ulite_of_driver);
657}
658#else /* CONFIG_OF && (CONFIG_PPC32 || CONFIG_MICROBLAZE) */
659/* Appropriate config not enabled; do nothing helpers */
660static inline int __init ulite_of_register(void) { return 0; }
661static inline void __exit ulite_of_unregister(void) { }
662#endif /* CONFIG_OF && (CONFIG_PPC32 || CONFIG_MICROBLAZE) */
663
664/* ---------------------------------------------------------------------
665 * Module setup/teardown
666 */
667
668int __init ulite_init(void)
669{
670 int ret;
671
672 pr_debug("uartlite: calling uart_register_driver()\n");
673 ret = uart_register_driver(&ulite_uart_driver);
674 if (ret)
675 goto err_uart;
676
677 ret = ulite_of_register();
678 if (ret)
679 goto err_of;
680
681 pr_debug("uartlite: calling platform_driver_register()\n");
682 ret = platform_driver_register(&ulite_platform_driver);
683 if (ret)
684 goto err_plat;
685
686 return 0;
687
688err_plat:
689 ulite_of_unregister();
690err_of:
691 uart_unregister_driver(&ulite_uart_driver);
692err_uart:
693 printk(KERN_ERR "registering uartlite driver failed: err=%i", ret);
694 return ret;
695}
696
697void __exit ulite_exit(void)
698{
699 platform_driver_unregister(&ulite_platform_driver);
700 ulite_of_unregister();
701 uart_unregister_driver(&ulite_uart_driver);
702}
703
704module_init(ulite_init);
705module_exit(ulite_exit);
706
707MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
708MODULE_DESCRIPTION("Xilinx uartlite serial driver");
709MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/ucc_uart.c b/drivers/tty/serial/ucc_uart.c
new file mode 100644
index 000000000000..3f4848e2174a
--- /dev/null
+++ b/drivers/tty/serial/ucc_uart.c
@@ -0,0 +1,1537 @@
1/*
2 * Freescale QUICC Engine UART device driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
6 * Copyright 2007 Freescale Semiconductor, 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 * This driver adds support for UART devices via Freescale's QUICC Engine
12 * found on some Freescale SOCs.
13 *
14 * If Soft-UART support is needed but not already present, then this driver
15 * will request and upload the "Soft-UART" microcode upon probe. The
16 * filename of the microcode should be fsl_qe_ucode_uart_X_YZ.bin, where "X"
17 * is the name of the SOC (e.g. 8323), and YZ is the revision of the SOC,
18 * (e.g. "11" for 1.1).
19 */
20
21#include <linux/module.h>
22#include <linux/serial.h>
23#include <linux/slab.h>
24#include <linux/serial_core.h>
25#include <linux/io.h>
26#include <linux/of_platform.h>
27#include <linux/dma-mapping.h>
28
29#include <linux/fs_uart_pd.h>
30#include <asm/ucc_slow.h>
31
32#include <linux/firmware.h>
33#include <asm/reg.h>
34
35/*
36 * The GUMR flag for Soft UART. This would normally be defined in qe.h,
37 * but Soft-UART is a hack and we want to keep everything related to it in
38 * this file.
39 */
40#define UCC_SLOW_GUMR_H_SUART 0x00004000 /* Soft-UART */
41
42/*
43 * soft_uart is 1 if we need to use Soft-UART mode
44 */
45static int soft_uart;
46/*
47 * firmware_loaded is 1 if the firmware has been loaded, 0 otherwise.
48 */
49static int firmware_loaded;
50
51/* Enable this macro to configure all serial ports in internal loopback
52 mode */
53/* #define LOOPBACK */
54
55/* The major and minor device numbers are defined in
56 * http://www.lanana.org/docs/device-list/devices-2.6+.txt. For the QE
57 * UART, we have major number 204 and minor numbers 46 - 49, which are the
58 * same as for the CPM2. This decision was made because no Freescale part
59 * has both a CPM and a QE.
60 */
61#define SERIAL_QE_MAJOR 204
62#define SERIAL_QE_MINOR 46
63
64/* Since we only have minor numbers 46 - 49, there is a hard limit of 4 ports */
65#define UCC_MAX_UART 4
66
67/* The number of buffer descriptors for receiving characters. */
68#define RX_NUM_FIFO 4
69
70/* The number of buffer descriptors for transmitting characters. */
71#define TX_NUM_FIFO 4
72
73/* The maximum size of the character buffer for a single RX BD. */
74#define RX_BUF_SIZE 32
75
76/* The maximum size of the character buffer for a single TX BD. */
77#define TX_BUF_SIZE 32
78
79/*
80 * The number of jiffies to wait after receiving a close command before the
81 * device is actually closed. This allows the last few characters to be
82 * sent over the wire.
83 */
84#define UCC_WAIT_CLOSING 100
85
86struct ucc_uart_pram {
87 struct ucc_slow_pram common;
88 u8 res1[8]; /* reserved */
89 __be16 maxidl; /* Maximum idle chars */
90 __be16 idlc; /* temp idle counter */
91 __be16 brkcr; /* Break count register */
92 __be16 parec; /* receive parity error counter */
93 __be16 frmec; /* receive framing error counter */
94 __be16 nosec; /* receive noise counter */
95 __be16 brkec; /* receive break condition counter */
96 __be16 brkln; /* last received break length */
97 __be16 uaddr[2]; /* UART address character 1 & 2 */
98 __be16 rtemp; /* Temp storage */
99 __be16 toseq; /* Transmit out of sequence char */
100 __be16 cchars[8]; /* control characters 1-8 */
101 __be16 rccm; /* receive control character mask */
102 __be16 rccr; /* receive control character register */
103 __be16 rlbc; /* receive last break character */
104 __be16 res2; /* reserved */
105 __be32 res3; /* reserved, should be cleared */
106 u8 res4; /* reserved, should be cleared */
107 u8 res5[3]; /* reserved, should be cleared */
108 __be32 res6; /* reserved, should be cleared */
109 __be32 res7; /* reserved, should be cleared */
110 __be32 res8; /* reserved, should be cleared */
111 __be32 res9; /* reserved, should be cleared */
112 __be32 res10; /* reserved, should be cleared */
113 __be32 res11; /* reserved, should be cleared */
114 __be32 res12; /* reserved, should be cleared */
115 __be32 res13; /* reserved, should be cleared */
116/* The rest is for Soft-UART only */
117 __be16 supsmr; /* 0x90, Shadow UPSMR */
118 __be16 res92; /* 0x92, reserved, initialize to 0 */
119 __be32 rx_state; /* 0x94, RX state, initialize to 0 */
120 __be32 rx_cnt; /* 0x98, RX count, initialize to 0 */
121 u8 rx_length; /* 0x9C, Char length, set to 1+CL+PEN+1+SL */
122 u8 rx_bitmark; /* 0x9D, reserved, initialize to 0 */
123 u8 rx_temp_dlst_qe; /* 0x9E, reserved, initialize to 0 */
124 u8 res14[0xBC - 0x9F]; /* reserved */
125 __be32 dump_ptr; /* 0xBC, Dump pointer */
126 __be32 rx_frame_rem; /* 0xC0, reserved, initialize to 0 */
127 u8 rx_frame_rem_size; /* 0xC4, reserved, initialize to 0 */
128 u8 tx_mode; /* 0xC5, mode, 0=AHDLC, 1=UART */
129 __be16 tx_state; /* 0xC6, TX state */
130 u8 res15[0xD0 - 0xC8]; /* reserved */
131 __be32 resD0; /* 0xD0, reserved, initialize to 0 */
132 u8 resD4; /* 0xD4, reserved, initialize to 0 */
133 __be16 resD5; /* 0xD5, reserved, initialize to 0 */
134} __attribute__ ((packed));
135
136/* SUPSMR definitions, for Soft-UART only */
137#define UCC_UART_SUPSMR_SL 0x8000
138#define UCC_UART_SUPSMR_RPM_MASK 0x6000
139#define UCC_UART_SUPSMR_RPM_ODD 0x0000
140#define UCC_UART_SUPSMR_RPM_LOW 0x2000
141#define UCC_UART_SUPSMR_RPM_EVEN 0x4000
142#define UCC_UART_SUPSMR_RPM_HIGH 0x6000
143#define UCC_UART_SUPSMR_PEN 0x1000
144#define UCC_UART_SUPSMR_TPM_MASK 0x0C00
145#define UCC_UART_SUPSMR_TPM_ODD 0x0000
146#define UCC_UART_SUPSMR_TPM_LOW 0x0400
147#define UCC_UART_SUPSMR_TPM_EVEN 0x0800
148#define UCC_UART_SUPSMR_TPM_HIGH 0x0C00
149#define UCC_UART_SUPSMR_FRZ 0x0100
150#define UCC_UART_SUPSMR_UM_MASK 0x00c0
151#define UCC_UART_SUPSMR_UM_NORMAL 0x0000
152#define UCC_UART_SUPSMR_UM_MAN_MULTI 0x0040
153#define UCC_UART_SUPSMR_UM_AUTO_MULTI 0x00c0
154#define UCC_UART_SUPSMR_CL_MASK 0x0030
155#define UCC_UART_SUPSMR_CL_8 0x0030
156#define UCC_UART_SUPSMR_CL_7 0x0020
157#define UCC_UART_SUPSMR_CL_6 0x0010
158#define UCC_UART_SUPSMR_CL_5 0x0000
159
160#define UCC_UART_TX_STATE_AHDLC 0x00
161#define UCC_UART_TX_STATE_UART 0x01
162#define UCC_UART_TX_STATE_X1 0x00
163#define UCC_UART_TX_STATE_X16 0x80
164
165#define UCC_UART_PRAM_ALIGNMENT 0x100
166
167#define UCC_UART_SIZE_OF_BD UCC_SLOW_SIZE_OF_BD
168#define NUM_CONTROL_CHARS 8
169
170/* Private per-port data structure */
171struct uart_qe_port {
172 struct uart_port port;
173 struct ucc_slow __iomem *uccp;
174 struct ucc_uart_pram __iomem *uccup;
175 struct ucc_slow_info us_info;
176 struct ucc_slow_private *us_private;
177 struct device_node *np;
178 unsigned int ucc_num; /* First ucc is 0, not 1 */
179
180 u16 rx_nrfifos;
181 u16 rx_fifosize;
182 u16 tx_nrfifos;
183 u16 tx_fifosize;
184 int wait_closing;
185 u32 flags;
186 struct qe_bd *rx_bd_base;
187 struct qe_bd *rx_cur;
188 struct qe_bd *tx_bd_base;
189 struct qe_bd *tx_cur;
190 unsigned char *tx_buf;
191 unsigned char *rx_buf;
192 void *bd_virt; /* virtual address of the BD buffers */
193 dma_addr_t bd_dma_addr; /* bus address of the BD buffers */
194 unsigned int bd_size; /* size of BD buffer space */
195};
196
197static struct uart_driver ucc_uart_driver = {
198 .owner = THIS_MODULE,
199 .driver_name = "ucc_uart",
200 .dev_name = "ttyQE",
201 .major = SERIAL_QE_MAJOR,
202 .minor = SERIAL_QE_MINOR,
203 .nr = UCC_MAX_UART,
204};
205
206/*
207 * Virtual to physical address translation.
208 *
209 * Given the virtual address for a character buffer, this function returns
210 * the physical (DMA) equivalent.
211 */
212static inline dma_addr_t cpu2qe_addr(void *addr, struct uart_qe_port *qe_port)
213{
214 if (likely((addr >= qe_port->bd_virt)) &&
215 (addr < (qe_port->bd_virt + qe_port->bd_size)))
216 return qe_port->bd_dma_addr + (addr - qe_port->bd_virt);
217
218 /* something nasty happened */
219 printk(KERN_ERR "%s: addr=%p\n", __func__, addr);
220 BUG();
221 return 0;
222}
223
224/*
225 * Physical to virtual address translation.
226 *
227 * Given the physical (DMA) address for a character buffer, this function
228 * returns the virtual equivalent.
229 */
230static inline void *qe2cpu_addr(dma_addr_t addr, struct uart_qe_port *qe_port)
231{
232 /* sanity check */
233 if (likely((addr >= qe_port->bd_dma_addr) &&
234 (addr < (qe_port->bd_dma_addr + qe_port->bd_size))))
235 return qe_port->bd_virt + (addr - qe_port->bd_dma_addr);
236
237 /* something nasty happened */
238 printk(KERN_ERR "%s: addr=%x\n", __func__, addr);
239 BUG();
240 return NULL;
241}
242
243/*
244 * Return 1 if the QE is done transmitting all buffers for this port
245 *
246 * This function scans each BD in sequence. If we find a BD that is not
247 * ready (READY=1), then we return 0 indicating that the QE is still sending
248 * data. If we reach the last BD (WRAP=1), then we know we've scanned
249 * the entire list, and all BDs are done.
250 */
251static unsigned int qe_uart_tx_empty(struct uart_port *port)
252{
253 struct uart_qe_port *qe_port =
254 container_of(port, struct uart_qe_port, port);
255 struct qe_bd *bdp = qe_port->tx_bd_base;
256
257 while (1) {
258 if (in_be16(&bdp->status) & BD_SC_READY)
259 /* This BD is not done, so return "not done" */
260 return 0;
261
262 if (in_be16(&bdp->status) & BD_SC_WRAP)
263 /*
264 * This BD is done and it's the last one, so return
265 * "done"
266 */
267 return 1;
268
269 bdp++;
270 };
271}
272
273/*
274 * Set the modem control lines
275 *
276 * Although the QE can control the modem control lines (e.g. CTS), we
277 * don't need that support. This function must exist, however, otherwise
278 * the kernel will panic.
279 */
280void qe_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
281{
282}
283
284/*
285 * Get the current modem control line status
286 *
287 * Although the QE can control the modem control lines (e.g. CTS), this
288 * driver currently doesn't support that, so we always return Carrier
289 * Detect, Data Set Ready, and Clear To Send.
290 */
291static unsigned int qe_uart_get_mctrl(struct uart_port *port)
292{
293 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
294}
295
296/*
297 * Disable the transmit interrupt.
298 *
299 * Although this function is called "stop_tx", it does not actually stop
300 * transmission of data. Instead, it tells the QE to not generate an
301 * interrupt when the UCC is finished sending characters.
302 */
303static void qe_uart_stop_tx(struct uart_port *port)
304{
305 struct uart_qe_port *qe_port =
306 container_of(port, struct uart_qe_port, port);
307
308 clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
309}
310
311/*
312 * Transmit as many characters to the HW as possible.
313 *
314 * This function will attempt to stuff of all the characters from the
315 * kernel's transmit buffer into TX BDs.
316 *
317 * A return value of non-zero indicates that it successfully stuffed all
318 * characters from the kernel buffer.
319 *
320 * A return value of zero indicates that there are still characters in the
321 * kernel's buffer that have not been transmitted, but there are no more BDs
322 * available. This function should be called again after a BD has been made
323 * available.
324 */
325static int qe_uart_tx_pump(struct uart_qe_port *qe_port)
326{
327 struct qe_bd *bdp;
328 unsigned char *p;
329 unsigned int count;
330 struct uart_port *port = &qe_port->port;
331 struct circ_buf *xmit = &port->state->xmit;
332
333 bdp = qe_port->rx_cur;
334
335 /* Handle xon/xoff */
336 if (port->x_char) {
337 /* Pick next descriptor and fill from buffer */
338 bdp = qe_port->tx_cur;
339
340 p = qe2cpu_addr(bdp->buf, qe_port);
341
342 *p++ = port->x_char;
343 out_be16(&bdp->length, 1);
344 setbits16(&bdp->status, BD_SC_READY);
345 /* Get next BD. */
346 if (in_be16(&bdp->status) & BD_SC_WRAP)
347 bdp = qe_port->tx_bd_base;
348 else
349 bdp++;
350 qe_port->tx_cur = bdp;
351
352 port->icount.tx++;
353 port->x_char = 0;
354 return 1;
355 }
356
357 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
358 qe_uart_stop_tx(port);
359 return 0;
360 }
361
362 /* Pick next descriptor and fill from buffer */
363 bdp = qe_port->tx_cur;
364
365 while (!(in_be16(&bdp->status) & BD_SC_READY) &&
366 (xmit->tail != xmit->head)) {
367 count = 0;
368 p = qe2cpu_addr(bdp->buf, qe_port);
369 while (count < qe_port->tx_fifosize) {
370 *p++ = xmit->buf[xmit->tail];
371 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
372 port->icount.tx++;
373 count++;
374 if (xmit->head == xmit->tail)
375 break;
376 }
377
378 out_be16(&bdp->length, count);
379 setbits16(&bdp->status, BD_SC_READY);
380
381 /* Get next BD. */
382 if (in_be16(&bdp->status) & BD_SC_WRAP)
383 bdp = qe_port->tx_bd_base;
384 else
385 bdp++;
386 }
387 qe_port->tx_cur = bdp;
388
389 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
390 uart_write_wakeup(port);
391
392 if (uart_circ_empty(xmit)) {
393 /* The kernel buffer is empty, so turn off TX interrupts. We
394 don't need to be told when the QE is finished transmitting
395 the data. */
396 qe_uart_stop_tx(port);
397 return 0;
398 }
399
400 return 1;
401}
402
403/*
404 * Start transmitting data
405 *
406 * This function will start transmitting any available data, if the port
407 * isn't already transmitting data.
408 */
409static void qe_uart_start_tx(struct uart_port *port)
410{
411 struct uart_qe_port *qe_port =
412 container_of(port, struct uart_qe_port, port);
413
414 /* If we currently are transmitting, then just return */
415 if (in_be16(&qe_port->uccp->uccm) & UCC_UART_UCCE_TX)
416 return;
417
418 /* Otherwise, pump the port and start transmission */
419 if (qe_uart_tx_pump(qe_port))
420 setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
421}
422
423/*
424 * Stop transmitting data
425 */
426static void qe_uart_stop_rx(struct uart_port *port)
427{
428 struct uart_qe_port *qe_port =
429 container_of(port, struct uart_qe_port, port);
430
431 clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
432}
433
434/*
435 * Enable status change interrupts
436 *
437 * We don't support status change interrupts, but we need to define this
438 * function otherwise the kernel will panic.
439 */
440static void qe_uart_enable_ms(struct uart_port *port)
441{
442}
443
444/* Start or stop sending break signal
445 *
446 * This function controls the sending of a break signal. If break_state=1,
447 * then we start sending a break signal. If break_state=0, then we stop
448 * sending the break signal.
449 */
450static void qe_uart_break_ctl(struct uart_port *port, int break_state)
451{
452 struct uart_qe_port *qe_port =
453 container_of(port, struct uart_qe_port, port);
454
455 if (break_state)
456 ucc_slow_stop_tx(qe_port->us_private);
457 else
458 ucc_slow_restart_tx(qe_port->us_private);
459}
460
461/* ISR helper function for receiving character.
462 *
463 * This function is called by the ISR to handling receiving characters
464 */
465static void qe_uart_int_rx(struct uart_qe_port *qe_port)
466{
467 int i;
468 unsigned char ch, *cp;
469 struct uart_port *port = &qe_port->port;
470 struct tty_struct *tty = port->state->port.tty;
471 struct qe_bd *bdp;
472 u16 status;
473 unsigned int flg;
474
475 /* Just loop through the closed BDs and copy the characters into
476 * the buffer.
477 */
478 bdp = qe_port->rx_cur;
479 while (1) {
480 status = in_be16(&bdp->status);
481
482 /* If this one is empty, then we assume we've read them all */
483 if (status & BD_SC_EMPTY)
484 break;
485
486 /* get number of characters, and check space in RX buffer */
487 i = in_be16(&bdp->length);
488
489 /* If we don't have enough room in RX buffer for the entire BD,
490 * then we try later, which will be the next RX interrupt.
491 */
492 if (tty_buffer_request_room(tty, i) < i) {
493 dev_dbg(port->dev, "ucc-uart: no room in RX buffer\n");
494 return;
495 }
496
497 /* get pointer */
498 cp = qe2cpu_addr(bdp->buf, qe_port);
499
500 /* loop through the buffer */
501 while (i-- > 0) {
502 ch = *cp++;
503 port->icount.rx++;
504 flg = TTY_NORMAL;
505
506 if (!i && status &
507 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
508 goto handle_error;
509 if (uart_handle_sysrq_char(port, ch))
510 continue;
511
512error_return:
513 tty_insert_flip_char(tty, ch, flg);
514
515 }
516
517 /* This BD is ready to be used again. Clear status. get next */
518 clrsetbits_be16(&bdp->status, BD_SC_BR | BD_SC_FR | BD_SC_PR |
519 BD_SC_OV | BD_SC_ID, BD_SC_EMPTY);
520 if (in_be16(&bdp->status) & BD_SC_WRAP)
521 bdp = qe_port->rx_bd_base;
522 else
523 bdp++;
524
525 }
526
527 /* Write back buffer pointer */
528 qe_port->rx_cur = bdp;
529
530 /* Activate BH processing */
531 tty_flip_buffer_push(tty);
532
533 return;
534
535 /* Error processing */
536
537handle_error:
538 /* Statistics */
539 if (status & BD_SC_BR)
540 port->icount.brk++;
541 if (status & BD_SC_PR)
542 port->icount.parity++;
543 if (status & BD_SC_FR)
544 port->icount.frame++;
545 if (status & BD_SC_OV)
546 port->icount.overrun++;
547
548 /* Mask out ignored conditions */
549 status &= port->read_status_mask;
550
551 /* Handle the remaining ones */
552 if (status & BD_SC_BR)
553 flg = TTY_BREAK;
554 else if (status & BD_SC_PR)
555 flg = TTY_PARITY;
556 else if (status & BD_SC_FR)
557 flg = TTY_FRAME;
558
559 /* Overrun does not affect the current character ! */
560 if (status & BD_SC_OV)
561 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
562#ifdef SUPPORT_SYSRQ
563 port->sysrq = 0;
564#endif
565 goto error_return;
566}
567
568/* Interrupt handler
569 *
570 * This interrupt handler is called after a BD is processed.
571 */
572static irqreturn_t qe_uart_int(int irq, void *data)
573{
574 struct uart_qe_port *qe_port = (struct uart_qe_port *) data;
575 struct ucc_slow __iomem *uccp = qe_port->uccp;
576 u16 events;
577
578 /* Clear the interrupts */
579 events = in_be16(&uccp->ucce);
580 out_be16(&uccp->ucce, events);
581
582 if (events & UCC_UART_UCCE_BRKE)
583 uart_handle_break(&qe_port->port);
584
585 if (events & UCC_UART_UCCE_RX)
586 qe_uart_int_rx(qe_port);
587
588 if (events & UCC_UART_UCCE_TX)
589 qe_uart_tx_pump(qe_port);
590
591 return events ? IRQ_HANDLED : IRQ_NONE;
592}
593
594/* Initialize buffer descriptors
595 *
596 * This function initializes all of the RX and TX buffer descriptors.
597 */
598static void qe_uart_initbd(struct uart_qe_port *qe_port)
599{
600 int i;
601 void *bd_virt;
602 struct qe_bd *bdp;
603
604 /* Set the physical address of the host memory buffers in the buffer
605 * descriptors, and the virtual address for us to work with.
606 */
607 bd_virt = qe_port->bd_virt;
608 bdp = qe_port->rx_bd_base;
609 qe_port->rx_cur = qe_port->rx_bd_base;
610 for (i = 0; i < (qe_port->rx_nrfifos - 1); i++) {
611 out_be16(&bdp->status, BD_SC_EMPTY | BD_SC_INTRPT);
612 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
613 out_be16(&bdp->length, 0);
614 bd_virt += qe_port->rx_fifosize;
615 bdp++;
616 }
617
618 /* */
619 out_be16(&bdp->status, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
620 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
621 out_be16(&bdp->length, 0);
622
623 /* Set the physical address of the host memory
624 * buffers in the buffer descriptors, and the
625 * virtual address for us to work with.
626 */
627 bd_virt = qe_port->bd_virt +
628 L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
629 qe_port->tx_cur = qe_port->tx_bd_base;
630 bdp = qe_port->tx_bd_base;
631 for (i = 0; i < (qe_port->tx_nrfifos - 1); i++) {
632 out_be16(&bdp->status, BD_SC_INTRPT);
633 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
634 out_be16(&bdp->length, 0);
635 bd_virt += qe_port->tx_fifosize;
636 bdp++;
637 }
638
639 /* Loopback requires the preamble bit to be set on the first TX BD */
640#ifdef LOOPBACK
641 setbits16(&qe_port->tx_cur->status, BD_SC_P);
642#endif
643
644 out_be16(&bdp->status, BD_SC_WRAP | BD_SC_INTRPT);
645 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
646 out_be16(&bdp->length, 0);
647}
648
649/*
650 * Initialize a UCC for UART.
651 *
652 * This function configures a given UCC to be used as a UART device. Basic
653 * UCC initialization is handled in qe_uart_request_port(). This function
654 * does all the UART-specific stuff.
655 */
656static void qe_uart_init_ucc(struct uart_qe_port *qe_port)
657{
658 u32 cecr_subblock;
659 struct ucc_slow __iomem *uccp = qe_port->uccp;
660 struct ucc_uart_pram *uccup = qe_port->uccup;
661
662 unsigned int i;
663
664 /* First, disable TX and RX in the UCC */
665 ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
666
667 /* Program the UCC UART parameter RAM */
668 out_8(&uccup->common.rbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
669 out_8(&uccup->common.tbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
670 out_be16(&uccup->common.mrblr, qe_port->rx_fifosize);
671 out_be16(&uccup->maxidl, 0x10);
672 out_be16(&uccup->brkcr, 1);
673 out_be16(&uccup->parec, 0);
674 out_be16(&uccup->frmec, 0);
675 out_be16(&uccup->nosec, 0);
676 out_be16(&uccup->brkec, 0);
677 out_be16(&uccup->uaddr[0], 0);
678 out_be16(&uccup->uaddr[1], 0);
679 out_be16(&uccup->toseq, 0);
680 for (i = 0; i < 8; i++)
681 out_be16(&uccup->cchars[i], 0xC000);
682 out_be16(&uccup->rccm, 0xc0ff);
683
684 /* Configure the GUMR registers for UART */
685 if (soft_uart) {
686 /* Soft-UART requires a 1X multiplier for TX */
687 clrsetbits_be32(&uccp->gumr_l,
688 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
689 UCC_SLOW_GUMR_L_RDCR_MASK,
690 UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_1 |
691 UCC_SLOW_GUMR_L_RDCR_16);
692
693 clrsetbits_be32(&uccp->gumr_h, UCC_SLOW_GUMR_H_RFW,
694 UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX);
695 } else {
696 clrsetbits_be32(&uccp->gumr_l,
697 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
698 UCC_SLOW_GUMR_L_RDCR_MASK,
699 UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_16 |
700 UCC_SLOW_GUMR_L_RDCR_16);
701
702 clrsetbits_be32(&uccp->gumr_h,
703 UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX,
704 UCC_SLOW_GUMR_H_RFW);
705 }
706
707#ifdef LOOPBACK
708 clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
709 UCC_SLOW_GUMR_L_DIAG_LOOP);
710 clrsetbits_be32(&uccp->gumr_h,
711 UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_RSYN,
712 UCC_SLOW_GUMR_H_CDS);
713#endif
714
715 /* Disable rx interrupts and clear all pending events. */
716 out_be16(&uccp->uccm, 0);
717 out_be16(&uccp->ucce, 0xffff);
718 out_be16(&uccp->udsr, 0x7e7e);
719
720 /* Initialize UPSMR */
721 out_be16(&uccp->upsmr, 0);
722
723 if (soft_uart) {
724 out_be16(&uccup->supsmr, 0x30);
725 out_be16(&uccup->res92, 0);
726 out_be32(&uccup->rx_state, 0);
727 out_be32(&uccup->rx_cnt, 0);
728 out_8(&uccup->rx_bitmark, 0);
729 out_8(&uccup->rx_length, 10);
730 out_be32(&uccup->dump_ptr, 0x4000);
731 out_8(&uccup->rx_temp_dlst_qe, 0);
732 out_be32(&uccup->rx_frame_rem, 0);
733 out_8(&uccup->rx_frame_rem_size, 0);
734 /* Soft-UART requires TX to be 1X */
735 out_8(&uccup->tx_mode,
736 UCC_UART_TX_STATE_UART | UCC_UART_TX_STATE_X1);
737 out_be16(&uccup->tx_state, 0);
738 out_8(&uccup->resD4, 0);
739 out_be16(&uccup->resD5, 0);
740
741 /* Set UART mode.
742 * Enable receive and transmit.
743 */
744
745 /* From the microcode errata:
746 * 1.GUMR_L register, set mode=0010 (QMC).
747 * 2.Set GUMR_H[17] bit. (UART/AHDLC mode).
748 * 3.Set GUMR_H[19:20] (Transparent mode)
749 * 4.Clear GUMR_H[26] (RFW)
750 * ...
751 * 6.Receiver must use 16x over sampling
752 */
753 clrsetbits_be32(&uccp->gumr_l,
754 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
755 UCC_SLOW_GUMR_L_RDCR_MASK,
756 UCC_SLOW_GUMR_L_MODE_QMC | UCC_SLOW_GUMR_L_TDCR_16 |
757 UCC_SLOW_GUMR_L_RDCR_16);
758
759 clrsetbits_be32(&uccp->gumr_h,
760 UCC_SLOW_GUMR_H_RFW | UCC_SLOW_GUMR_H_RSYN,
761 UCC_SLOW_GUMR_H_SUART | UCC_SLOW_GUMR_H_TRX |
762 UCC_SLOW_GUMR_H_TTX | UCC_SLOW_GUMR_H_TFL);
763
764#ifdef LOOPBACK
765 clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
766 UCC_SLOW_GUMR_L_DIAG_LOOP);
767 clrbits32(&uccp->gumr_h, UCC_SLOW_GUMR_H_CTSP |
768 UCC_SLOW_GUMR_H_CDS);
769#endif
770
771 cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
772 qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
773 QE_CR_PROTOCOL_UNSPECIFIED, 0);
774 } else {
775 cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
776 qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
777 QE_CR_PROTOCOL_UART, 0);
778 }
779}
780
781/*
782 * Initialize the port.
783 */
784static int qe_uart_startup(struct uart_port *port)
785{
786 struct uart_qe_port *qe_port =
787 container_of(port, struct uart_qe_port, port);
788 int ret;
789
790 /*
791 * If we're using Soft-UART mode, then we need to make sure the
792 * firmware has been uploaded first.
793 */
794 if (soft_uart && !firmware_loaded) {
795 dev_err(port->dev, "Soft-UART firmware not uploaded\n");
796 return -ENODEV;
797 }
798
799 qe_uart_initbd(qe_port);
800 qe_uart_init_ucc(qe_port);
801
802 /* Install interrupt handler. */
803 ret = request_irq(port->irq, qe_uart_int, IRQF_SHARED, "ucc-uart",
804 qe_port);
805 if (ret) {
806 dev_err(port->dev, "could not claim IRQ %u\n", port->irq);
807 return ret;
808 }
809
810 /* Startup rx-int */
811 setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
812 ucc_slow_enable(qe_port->us_private, COMM_DIR_RX_AND_TX);
813
814 return 0;
815}
816
817/*
818 * Shutdown the port.
819 */
820static void qe_uart_shutdown(struct uart_port *port)
821{
822 struct uart_qe_port *qe_port =
823 container_of(port, struct uart_qe_port, port);
824 struct ucc_slow __iomem *uccp = qe_port->uccp;
825 unsigned int timeout = 20;
826
827 /* Disable RX and TX */
828
829 /* Wait for all the BDs marked sent */
830 while (!qe_uart_tx_empty(port)) {
831 if (!--timeout) {
832 dev_warn(port->dev, "shutdown timeout\n");
833 break;
834 }
835 set_current_state(TASK_UNINTERRUPTIBLE);
836 schedule_timeout(2);
837 }
838
839 if (qe_port->wait_closing) {
840 /* Wait a bit longer */
841 set_current_state(TASK_UNINTERRUPTIBLE);
842 schedule_timeout(qe_port->wait_closing);
843 }
844
845 /* Stop uarts */
846 ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
847 clrbits16(&uccp->uccm, UCC_UART_UCCE_TX | UCC_UART_UCCE_RX);
848
849 /* Shut them really down and reinit buffer descriptors */
850 ucc_slow_graceful_stop_tx(qe_port->us_private);
851 qe_uart_initbd(qe_port);
852
853 free_irq(port->irq, qe_port);
854}
855
856/*
857 * Set the serial port parameters.
858 */
859static void qe_uart_set_termios(struct uart_port *port,
860 struct ktermios *termios, struct ktermios *old)
861{
862 struct uart_qe_port *qe_port =
863 container_of(port, struct uart_qe_port, port);
864 struct ucc_slow __iomem *uccp = qe_port->uccp;
865 unsigned int baud;
866 unsigned long flags;
867 u16 upsmr = in_be16(&uccp->upsmr);
868 struct ucc_uart_pram __iomem *uccup = qe_port->uccup;
869 u16 supsmr = in_be16(&uccup->supsmr);
870 u8 char_length = 2; /* 1 + CL + PEN + 1 + SL */
871
872 /* Character length programmed into the mode register is the
873 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
874 * 1 or 2 stop bits, minus 1.
875 * The value 'bits' counts this for us.
876 */
877
878 /* byte size */
879 upsmr &= UCC_UART_UPSMR_CL_MASK;
880 supsmr &= UCC_UART_SUPSMR_CL_MASK;
881
882 switch (termios->c_cflag & CSIZE) {
883 case CS5:
884 upsmr |= UCC_UART_UPSMR_CL_5;
885 supsmr |= UCC_UART_SUPSMR_CL_5;
886 char_length += 5;
887 break;
888 case CS6:
889 upsmr |= UCC_UART_UPSMR_CL_6;
890 supsmr |= UCC_UART_SUPSMR_CL_6;
891 char_length += 6;
892 break;
893 case CS7:
894 upsmr |= UCC_UART_UPSMR_CL_7;
895 supsmr |= UCC_UART_SUPSMR_CL_7;
896 char_length += 7;
897 break;
898 default: /* case CS8 */
899 upsmr |= UCC_UART_UPSMR_CL_8;
900 supsmr |= UCC_UART_SUPSMR_CL_8;
901 char_length += 8;
902 break;
903 }
904
905 /* If CSTOPB is set, we want two stop bits */
906 if (termios->c_cflag & CSTOPB) {
907 upsmr |= UCC_UART_UPSMR_SL;
908 supsmr |= UCC_UART_SUPSMR_SL;
909 char_length++; /* + SL */
910 }
911
912 if (termios->c_cflag & PARENB) {
913 upsmr |= UCC_UART_UPSMR_PEN;
914 supsmr |= UCC_UART_SUPSMR_PEN;
915 char_length++; /* + PEN */
916
917 if (!(termios->c_cflag & PARODD)) {
918 upsmr &= ~(UCC_UART_UPSMR_RPM_MASK |
919 UCC_UART_UPSMR_TPM_MASK);
920 upsmr |= UCC_UART_UPSMR_RPM_EVEN |
921 UCC_UART_UPSMR_TPM_EVEN;
922 supsmr &= ~(UCC_UART_SUPSMR_RPM_MASK |
923 UCC_UART_SUPSMR_TPM_MASK);
924 supsmr |= UCC_UART_SUPSMR_RPM_EVEN |
925 UCC_UART_SUPSMR_TPM_EVEN;
926 }
927 }
928
929 /*
930 * Set up parity check flag
931 */
932 port->read_status_mask = BD_SC_EMPTY | BD_SC_OV;
933 if (termios->c_iflag & INPCK)
934 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
935 if (termios->c_iflag & (BRKINT | PARMRK))
936 port->read_status_mask |= BD_SC_BR;
937
938 /*
939 * Characters to ignore
940 */
941 port->ignore_status_mask = 0;
942 if (termios->c_iflag & IGNPAR)
943 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
944 if (termios->c_iflag & IGNBRK) {
945 port->ignore_status_mask |= BD_SC_BR;
946 /*
947 * If we're ignore parity and break indicators, ignore
948 * overruns too. (For real raw support).
949 */
950 if (termios->c_iflag & IGNPAR)
951 port->ignore_status_mask |= BD_SC_OV;
952 }
953 /*
954 * !!! ignore all characters if CREAD is not set
955 */
956 if ((termios->c_cflag & CREAD) == 0)
957 port->read_status_mask &= ~BD_SC_EMPTY;
958
959 baud = uart_get_baud_rate(port, termios, old, 0, 115200);
960
961 /* Do we really need a spinlock here? */
962 spin_lock_irqsave(&port->lock, flags);
963
964 out_be16(&uccp->upsmr, upsmr);
965 if (soft_uart) {
966 out_be16(&uccup->supsmr, supsmr);
967 out_8(&uccup->rx_length, char_length);
968
969 /* Soft-UART requires a 1X multiplier for TX */
970 qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
971 qe_setbrg(qe_port->us_info.tx_clock, baud, 1);
972 } else {
973 qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
974 qe_setbrg(qe_port->us_info.tx_clock, baud, 16);
975 }
976
977 spin_unlock_irqrestore(&port->lock, flags);
978}
979
980/*
981 * Return a pointer to a string that describes what kind of port this is.
982 */
983static const char *qe_uart_type(struct uart_port *port)
984{
985 return "QE";
986}
987
988/*
989 * Allocate any memory and I/O resources required by the port.
990 */
991static int qe_uart_request_port(struct uart_port *port)
992{
993 int ret;
994 struct uart_qe_port *qe_port =
995 container_of(port, struct uart_qe_port, port);
996 struct ucc_slow_info *us_info = &qe_port->us_info;
997 struct ucc_slow_private *uccs;
998 unsigned int rx_size, tx_size;
999 void *bd_virt;
1000 dma_addr_t bd_dma_addr = 0;
1001
1002 ret = ucc_slow_init(us_info, &uccs);
1003 if (ret) {
1004 dev_err(port->dev, "could not initialize UCC%u\n",
1005 qe_port->ucc_num);
1006 return ret;
1007 }
1008
1009 qe_port->us_private = uccs;
1010 qe_port->uccp = uccs->us_regs;
1011 qe_port->uccup = (struct ucc_uart_pram *) uccs->us_pram;
1012 qe_port->rx_bd_base = uccs->rx_bd;
1013 qe_port->tx_bd_base = uccs->tx_bd;
1014
1015 /*
1016 * Allocate the transmit and receive data buffers.
1017 */
1018
1019 rx_size = L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
1020 tx_size = L1_CACHE_ALIGN(qe_port->tx_nrfifos * qe_port->tx_fifosize);
1021
1022 bd_virt = dma_alloc_coherent(port->dev, rx_size + tx_size, &bd_dma_addr,
1023 GFP_KERNEL);
1024 if (!bd_virt) {
1025 dev_err(port->dev, "could not allocate buffer descriptors\n");
1026 return -ENOMEM;
1027 }
1028
1029 qe_port->bd_virt = bd_virt;
1030 qe_port->bd_dma_addr = bd_dma_addr;
1031 qe_port->bd_size = rx_size + tx_size;
1032
1033 qe_port->rx_buf = bd_virt;
1034 qe_port->tx_buf = qe_port->rx_buf + rx_size;
1035
1036 return 0;
1037}
1038
1039/*
1040 * Configure the port.
1041 *
1042 * We say we're a CPM-type port because that's mostly true. Once the device
1043 * is configured, this driver operates almost identically to the CPM serial
1044 * driver.
1045 */
1046static void qe_uart_config_port(struct uart_port *port, int flags)
1047{
1048 if (flags & UART_CONFIG_TYPE) {
1049 port->type = PORT_CPM;
1050 qe_uart_request_port(port);
1051 }
1052}
1053
1054/*
1055 * Release any memory and I/O resources that were allocated in
1056 * qe_uart_request_port().
1057 */
1058static void qe_uart_release_port(struct uart_port *port)
1059{
1060 struct uart_qe_port *qe_port =
1061 container_of(port, struct uart_qe_port, port);
1062 struct ucc_slow_private *uccs = qe_port->us_private;
1063
1064 dma_free_coherent(port->dev, qe_port->bd_size, qe_port->bd_virt,
1065 qe_port->bd_dma_addr);
1066
1067 ucc_slow_free(uccs);
1068}
1069
1070/*
1071 * Verify that the data in serial_struct is suitable for this device.
1072 */
1073static int qe_uart_verify_port(struct uart_port *port,
1074 struct serial_struct *ser)
1075{
1076 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
1077 return -EINVAL;
1078
1079 if (ser->irq < 0 || ser->irq >= nr_irqs)
1080 return -EINVAL;
1081
1082 if (ser->baud_base < 9600)
1083 return -EINVAL;
1084
1085 return 0;
1086}
1087/* UART operations
1088 *
1089 * Details on these functions can be found in Documentation/serial/driver
1090 */
1091static struct uart_ops qe_uart_pops = {
1092 .tx_empty = qe_uart_tx_empty,
1093 .set_mctrl = qe_uart_set_mctrl,
1094 .get_mctrl = qe_uart_get_mctrl,
1095 .stop_tx = qe_uart_stop_tx,
1096 .start_tx = qe_uart_start_tx,
1097 .stop_rx = qe_uart_stop_rx,
1098 .enable_ms = qe_uart_enable_ms,
1099 .break_ctl = qe_uart_break_ctl,
1100 .startup = qe_uart_startup,
1101 .shutdown = qe_uart_shutdown,
1102 .set_termios = qe_uart_set_termios,
1103 .type = qe_uart_type,
1104 .release_port = qe_uart_release_port,
1105 .request_port = qe_uart_request_port,
1106 .config_port = qe_uart_config_port,
1107 .verify_port = qe_uart_verify_port,
1108};
1109
1110/*
1111 * Obtain the SOC model number and revision level
1112 *
1113 * This function parses the device tree to obtain the SOC model. It then
1114 * reads the SVR register to the revision.
1115 *
1116 * The device tree stores the SOC model two different ways.
1117 *
1118 * The new way is:
1119 *
1120 * cpu@0 {
1121 * compatible = "PowerPC,8323";
1122 * device_type = "cpu";
1123 * ...
1124 *
1125 *
1126 * The old way is:
1127 * PowerPC,8323@0 {
1128 * device_type = "cpu";
1129 * ...
1130 *
1131 * This code first checks the new way, and then the old way.
1132 */
1133static unsigned int soc_info(unsigned int *rev_h, unsigned int *rev_l)
1134{
1135 struct device_node *np;
1136 const char *soc_string;
1137 unsigned int svr;
1138 unsigned int soc;
1139
1140 /* Find the CPU node */
1141 np = of_find_node_by_type(NULL, "cpu");
1142 if (!np)
1143 return 0;
1144 /* Find the compatible property */
1145 soc_string = of_get_property(np, "compatible", NULL);
1146 if (!soc_string)
1147 /* No compatible property, so try the name. */
1148 soc_string = np->name;
1149
1150 /* Extract the SOC number from the "PowerPC," string */
1151 if ((sscanf(soc_string, "PowerPC,%u", &soc) != 1) || !soc)
1152 return 0;
1153
1154 /* Get the revision from the SVR */
1155 svr = mfspr(SPRN_SVR);
1156 *rev_h = (svr >> 4) & 0xf;
1157 *rev_l = svr & 0xf;
1158
1159 return soc;
1160}
1161
1162/*
1163 * requst_firmware_nowait() callback function
1164 *
1165 * This function is called by the kernel when a firmware is made available,
1166 * or if it times out waiting for the firmware.
1167 */
1168static void uart_firmware_cont(const struct firmware *fw, void *context)
1169{
1170 struct qe_firmware *firmware;
1171 struct device *dev = context;
1172 int ret;
1173
1174 if (!fw) {
1175 dev_err(dev, "firmware not found\n");
1176 return;
1177 }
1178
1179 firmware = (struct qe_firmware *) fw->data;
1180
1181 if (firmware->header.length != fw->size) {
1182 dev_err(dev, "invalid firmware\n");
1183 goto out;
1184 }
1185
1186 ret = qe_upload_firmware(firmware);
1187 if (ret) {
1188 dev_err(dev, "could not load firmware\n");
1189 goto out;
1190 }
1191
1192 firmware_loaded = 1;
1193 out:
1194 release_firmware(fw);
1195}
1196
1197static int ucc_uart_probe(struct platform_device *ofdev,
1198 const struct of_device_id *match)
1199{
1200 struct device_node *np = ofdev->dev.of_node;
1201 const unsigned int *iprop; /* Integer OF properties */
1202 const char *sprop; /* String OF properties */
1203 struct uart_qe_port *qe_port = NULL;
1204 struct resource res;
1205 int ret;
1206
1207 /*
1208 * Determine if we need Soft-UART mode
1209 */
1210 if (of_find_property(np, "soft-uart", NULL)) {
1211 dev_dbg(&ofdev->dev, "using Soft-UART mode\n");
1212 soft_uart = 1;
1213 }
1214
1215 /*
1216 * If we are using Soft-UART, determine if we need to upload the
1217 * firmware, too.
1218 */
1219 if (soft_uart) {
1220 struct qe_firmware_info *qe_fw_info;
1221
1222 qe_fw_info = qe_get_firmware_info();
1223
1224 /* Check if the firmware has been uploaded. */
1225 if (qe_fw_info && strstr(qe_fw_info->id, "Soft-UART")) {
1226 firmware_loaded = 1;
1227 } else {
1228 char filename[32];
1229 unsigned int soc;
1230 unsigned int rev_h;
1231 unsigned int rev_l;
1232
1233 soc = soc_info(&rev_h, &rev_l);
1234 if (!soc) {
1235 dev_err(&ofdev->dev, "unknown CPU model\n");
1236 return -ENXIO;
1237 }
1238 sprintf(filename, "fsl_qe_ucode_uart_%u_%u%u.bin",
1239 soc, rev_h, rev_l);
1240
1241 dev_info(&ofdev->dev, "waiting for firmware %s\n",
1242 filename);
1243
1244 /*
1245 * We call request_firmware_nowait instead of
1246 * request_firmware so that the driver can load and
1247 * initialize the ports without holding up the rest of
1248 * the kernel. If hotplug support is enabled in the
1249 * kernel, then we use it.
1250 */
1251 ret = request_firmware_nowait(THIS_MODULE,
1252 FW_ACTION_HOTPLUG, filename, &ofdev->dev,
1253 GFP_KERNEL, &ofdev->dev, uart_firmware_cont);
1254 if (ret) {
1255 dev_err(&ofdev->dev,
1256 "could not load firmware %s\n",
1257 filename);
1258 return ret;
1259 }
1260 }
1261 }
1262
1263 qe_port = kzalloc(sizeof(struct uart_qe_port), GFP_KERNEL);
1264 if (!qe_port) {
1265 dev_err(&ofdev->dev, "can't allocate QE port structure\n");
1266 return -ENOMEM;
1267 }
1268
1269 /* Search for IRQ and mapbase */
1270 ret = of_address_to_resource(np, 0, &res);
1271 if (ret) {
1272 dev_err(&ofdev->dev, "missing 'reg' property in device tree\n");
1273 kfree(qe_port);
1274 return ret;
1275 }
1276 if (!res.start) {
1277 dev_err(&ofdev->dev, "invalid 'reg' property in device tree\n");
1278 kfree(qe_port);
1279 return -EINVAL;
1280 }
1281 qe_port->port.mapbase = res.start;
1282
1283 /* Get the UCC number (device ID) */
1284 /* UCCs are numbered 1-7 */
1285 iprop = of_get_property(np, "cell-index", NULL);
1286 if (!iprop) {
1287 iprop = of_get_property(np, "device-id", NULL);
1288 if (!iprop) {
1289 kfree(qe_port);
1290 dev_err(&ofdev->dev, "UCC is unspecified in "
1291 "device tree\n");
1292 return -EINVAL;
1293 }
1294 }
1295
1296 if ((*iprop < 1) || (*iprop > UCC_MAX_NUM)) {
1297 dev_err(&ofdev->dev, "no support for UCC%u\n", *iprop);
1298 kfree(qe_port);
1299 return -ENODEV;
1300 }
1301 qe_port->ucc_num = *iprop - 1;
1302
1303 /*
1304 * In the future, we should not require the BRG to be specified in the
1305 * device tree. If no clock-source is specified, then just pick a BRG
1306 * to use. This requires a new QE library function that manages BRG
1307 * assignments.
1308 */
1309
1310 sprop = of_get_property(np, "rx-clock-name", NULL);
1311 if (!sprop) {
1312 dev_err(&ofdev->dev, "missing rx-clock-name in device tree\n");
1313 kfree(qe_port);
1314 return -ENODEV;
1315 }
1316
1317 qe_port->us_info.rx_clock = qe_clock_source(sprop);
1318 if ((qe_port->us_info.rx_clock < QE_BRG1) ||
1319 (qe_port->us_info.rx_clock > QE_BRG16)) {
1320 dev_err(&ofdev->dev, "rx-clock-name must be a BRG for UART\n");
1321 kfree(qe_port);
1322 return -ENODEV;
1323 }
1324
1325#ifdef LOOPBACK
1326 /* In internal loopback mode, TX and RX must use the same clock */
1327 qe_port->us_info.tx_clock = qe_port->us_info.rx_clock;
1328#else
1329 sprop = of_get_property(np, "tx-clock-name", NULL);
1330 if (!sprop) {
1331 dev_err(&ofdev->dev, "missing tx-clock-name in device tree\n");
1332 kfree(qe_port);
1333 return -ENODEV;
1334 }
1335 qe_port->us_info.tx_clock = qe_clock_source(sprop);
1336#endif
1337 if ((qe_port->us_info.tx_clock < QE_BRG1) ||
1338 (qe_port->us_info.tx_clock > QE_BRG16)) {
1339 dev_err(&ofdev->dev, "tx-clock-name must be a BRG for UART\n");
1340 kfree(qe_port);
1341 return -ENODEV;
1342 }
1343
1344 /* Get the port number, numbered 0-3 */
1345 iprop = of_get_property(np, "port-number", NULL);
1346 if (!iprop) {
1347 dev_err(&ofdev->dev, "missing port-number in device tree\n");
1348 kfree(qe_port);
1349 return -EINVAL;
1350 }
1351 qe_port->port.line = *iprop;
1352 if (qe_port->port.line >= UCC_MAX_UART) {
1353 dev_err(&ofdev->dev, "port-number must be 0-%u\n",
1354 UCC_MAX_UART - 1);
1355 kfree(qe_port);
1356 return -EINVAL;
1357 }
1358
1359 qe_port->port.irq = irq_of_parse_and_map(np, 0);
1360 if (qe_port->port.irq == NO_IRQ) {
1361 dev_err(&ofdev->dev, "could not map IRQ for UCC%u\n",
1362 qe_port->ucc_num + 1);
1363 kfree(qe_port);
1364 return -EINVAL;
1365 }
1366
1367 /*
1368 * Newer device trees have an "fsl,qe" compatible property for the QE
1369 * node, but we still need to support older device trees.
1370 */
1371 np = of_find_compatible_node(NULL, NULL, "fsl,qe");
1372 if (!np) {
1373 np = of_find_node_by_type(NULL, "qe");
1374 if (!np) {
1375 dev_err(&ofdev->dev, "could not find 'qe' node\n");
1376 kfree(qe_port);
1377 return -EINVAL;
1378 }
1379 }
1380
1381 iprop = of_get_property(np, "brg-frequency", NULL);
1382 if (!iprop) {
1383 dev_err(&ofdev->dev,
1384 "missing brg-frequency in device tree\n");
1385 kfree(qe_port);
1386 return -EINVAL;
1387 }
1388
1389 if (*iprop)
1390 qe_port->port.uartclk = *iprop;
1391 else {
1392 /*
1393 * Older versions of U-Boot do not initialize the brg-frequency
1394 * property, so in this case we assume the BRG frequency is
1395 * half the QE bus frequency.
1396 */
1397 iprop = of_get_property(np, "bus-frequency", NULL);
1398 if (!iprop) {
1399 dev_err(&ofdev->dev,
1400 "missing QE bus-frequency in device tree\n");
1401 kfree(qe_port);
1402 return -EINVAL;
1403 }
1404 if (*iprop)
1405 qe_port->port.uartclk = *iprop / 2;
1406 else {
1407 dev_err(&ofdev->dev,
1408 "invalid QE bus-frequency in device tree\n");
1409 kfree(qe_port);
1410 return -EINVAL;
1411 }
1412 }
1413
1414 spin_lock_init(&qe_port->port.lock);
1415 qe_port->np = np;
1416 qe_port->port.dev = &ofdev->dev;
1417 qe_port->port.ops = &qe_uart_pops;
1418 qe_port->port.iotype = UPIO_MEM;
1419
1420 qe_port->tx_nrfifos = TX_NUM_FIFO;
1421 qe_port->tx_fifosize = TX_BUF_SIZE;
1422 qe_port->rx_nrfifos = RX_NUM_FIFO;
1423 qe_port->rx_fifosize = RX_BUF_SIZE;
1424
1425 qe_port->wait_closing = UCC_WAIT_CLOSING;
1426 qe_port->port.fifosize = 512;
1427 qe_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
1428
1429 qe_port->us_info.ucc_num = qe_port->ucc_num;
1430 qe_port->us_info.regs = (phys_addr_t) res.start;
1431 qe_port->us_info.irq = qe_port->port.irq;
1432
1433 qe_port->us_info.rx_bd_ring_len = qe_port->rx_nrfifos;
1434 qe_port->us_info.tx_bd_ring_len = qe_port->tx_nrfifos;
1435
1436 /* Make sure ucc_slow_init() initializes both TX and RX */
1437 qe_port->us_info.init_tx = 1;
1438 qe_port->us_info.init_rx = 1;
1439
1440 /* Add the port to the uart sub-system. This will cause
1441 * qe_uart_config_port() to be called, so the us_info structure must
1442 * be initialized.
1443 */
1444 ret = uart_add_one_port(&ucc_uart_driver, &qe_port->port);
1445 if (ret) {
1446 dev_err(&ofdev->dev, "could not add /dev/ttyQE%u\n",
1447 qe_port->port.line);
1448 kfree(qe_port);
1449 return ret;
1450 }
1451
1452 dev_set_drvdata(&ofdev->dev, qe_port);
1453
1454 dev_info(&ofdev->dev, "UCC%u assigned to /dev/ttyQE%u\n",
1455 qe_port->ucc_num + 1, qe_port->port.line);
1456
1457 /* Display the mknod command for this device */
1458 dev_dbg(&ofdev->dev, "mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
1459 qe_port->port.line, SERIAL_QE_MAJOR,
1460 SERIAL_QE_MINOR + qe_port->port.line);
1461
1462 return 0;
1463}
1464
1465static int ucc_uart_remove(struct platform_device *ofdev)
1466{
1467 struct uart_qe_port *qe_port = dev_get_drvdata(&ofdev->dev);
1468
1469 dev_info(&ofdev->dev, "removing /dev/ttyQE%u\n", qe_port->port.line);
1470
1471 uart_remove_one_port(&ucc_uart_driver, &qe_port->port);
1472
1473 dev_set_drvdata(&ofdev->dev, NULL);
1474 kfree(qe_port);
1475
1476 return 0;
1477}
1478
1479static struct of_device_id ucc_uart_match[] = {
1480 {
1481 .type = "serial",
1482 .compatible = "ucc_uart",
1483 },
1484 {},
1485};
1486MODULE_DEVICE_TABLE(of, ucc_uart_match);
1487
1488static struct of_platform_driver ucc_uart_of_driver = {
1489 .driver = {
1490 .name = "ucc_uart",
1491 .owner = THIS_MODULE,
1492 .of_match_table = ucc_uart_match,
1493 },
1494 .probe = ucc_uart_probe,
1495 .remove = ucc_uart_remove,
1496};
1497
1498static int __init ucc_uart_init(void)
1499{
1500 int ret;
1501
1502 printk(KERN_INFO "Freescale QUICC Engine UART device driver\n");
1503#ifdef LOOPBACK
1504 printk(KERN_INFO "ucc-uart: Using loopback mode\n");
1505#endif
1506
1507 ret = uart_register_driver(&ucc_uart_driver);
1508 if (ret) {
1509 printk(KERN_ERR "ucc-uart: could not register UART driver\n");
1510 return ret;
1511 }
1512
1513 ret = of_register_platform_driver(&ucc_uart_of_driver);
1514 if (ret)
1515 printk(KERN_ERR
1516 "ucc-uart: could not register platform driver\n");
1517
1518 return ret;
1519}
1520
1521static void __exit ucc_uart_exit(void)
1522{
1523 printk(KERN_INFO
1524 "Freescale QUICC Engine UART device driver unloading\n");
1525
1526 of_unregister_platform_driver(&ucc_uart_of_driver);
1527 uart_unregister_driver(&ucc_uart_driver);
1528}
1529
1530module_init(ucc_uart_init);
1531module_exit(ucc_uart_exit);
1532
1533MODULE_DESCRIPTION("Freescale QUICC Engine (QE) UART");
1534MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1535MODULE_LICENSE("GPL v2");
1536MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_QE_MAJOR);
1537
diff --git a/drivers/tty/serial/vr41xx_siu.c b/drivers/tty/serial/vr41xx_siu.c
new file mode 100644
index 000000000000..3beb6ab4fa68
--- /dev/null
+++ b/drivers/tty/serial/vr41xx_siu.c
@@ -0,0 +1,978 @@
1/*
2 * Driver for NEC VR4100 series Serial Interface Unit.
3 *
4 * Copyright (C) 2004-2008 Yoichi Yuasa <yuasa@linux-mips.org>
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
23#if defined(CONFIG_SERIAL_VR41XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24#define SUPPORT_SYSRQ
25#endif
26
27#include <linux/console.h>
28#include <linux/errno.h>
29#include <linux/init.h>
30#include <linux/interrupt.h>
31#include <linux/ioport.h>
32#include <linux/module.h>
33#include <linux/platform_device.h>
34#include <linux/serial.h>
35#include <linux/serial_core.h>
36#include <linux/serial_reg.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39
40#include <asm/io.h>
41#include <asm/vr41xx/siu.h>
42#include <asm/vr41xx/vr41xx.h>
43
44#define SIU_BAUD_BASE 1152000
45#define SIU_MAJOR 204
46#define SIU_MINOR_BASE 82
47
48#define RX_MAX_COUNT 256
49#define TX_MAX_COUNT 15
50
51#define SIUIRSEL 0x08
52 #define TMICMODE 0x20
53 #define TMICTX 0x10
54 #define IRMSEL 0x0c
55 #define IRMSEL_HP 0x08
56 #define IRMSEL_TEMIC 0x04
57 #define IRMSEL_SHARP 0x00
58 #define IRUSESEL 0x02
59 #define SIRSEL 0x01
60
61static struct uart_port siu_uart_ports[SIU_PORTS_MAX] = {
62 [0 ... SIU_PORTS_MAX-1] = {
63 .lock = __SPIN_LOCK_UNLOCKED(siu_uart_ports->lock),
64 .irq = -1,
65 },
66};
67
68#ifdef CONFIG_SERIAL_VR41XX_CONSOLE
69static uint8_t lsr_break_flag[SIU_PORTS_MAX];
70#endif
71
72#define siu_read(port, offset) readb((port)->membase + (offset))
73#define siu_write(port, offset, value) writeb((value), (port)->membase + (offset))
74
75void vr41xx_select_siu_interface(siu_interface_t interface)
76{
77 struct uart_port *port;
78 unsigned long flags;
79 uint8_t irsel;
80
81 port = &siu_uart_ports[0];
82
83 spin_lock_irqsave(&port->lock, flags);
84
85 irsel = siu_read(port, SIUIRSEL);
86 if (interface == SIU_INTERFACE_IRDA)
87 irsel |= SIRSEL;
88 else
89 irsel &= ~SIRSEL;
90 siu_write(port, SIUIRSEL, irsel);
91
92 spin_unlock_irqrestore(&port->lock, flags);
93}
94EXPORT_SYMBOL_GPL(vr41xx_select_siu_interface);
95
96void vr41xx_use_irda(irda_use_t use)
97{
98 struct uart_port *port;
99 unsigned long flags;
100 uint8_t irsel;
101
102 port = &siu_uart_ports[0];
103
104 spin_lock_irqsave(&port->lock, flags);
105
106 irsel = siu_read(port, SIUIRSEL);
107 if (use == FIR_USE_IRDA)
108 irsel |= IRUSESEL;
109 else
110 irsel &= ~IRUSESEL;
111 siu_write(port, SIUIRSEL, irsel);
112
113 spin_unlock_irqrestore(&port->lock, flags);
114}
115EXPORT_SYMBOL_GPL(vr41xx_use_irda);
116
117void vr41xx_select_irda_module(irda_module_t module, irda_speed_t speed)
118{
119 struct uart_port *port;
120 unsigned long flags;
121 uint8_t irsel;
122
123 port = &siu_uart_ports[0];
124
125 spin_lock_irqsave(&port->lock, flags);
126
127 irsel = siu_read(port, SIUIRSEL);
128 irsel &= ~(IRMSEL | TMICTX | TMICMODE);
129 switch (module) {
130 case SHARP_IRDA:
131 irsel |= IRMSEL_SHARP;
132 break;
133 case TEMIC_IRDA:
134 irsel |= IRMSEL_TEMIC | TMICMODE;
135 if (speed == IRDA_TX_4MBPS)
136 irsel |= TMICTX;
137 break;
138 case HP_IRDA:
139 irsel |= IRMSEL_HP;
140 break;
141 default:
142 break;
143 }
144 siu_write(port, SIUIRSEL, irsel);
145
146 spin_unlock_irqrestore(&port->lock, flags);
147}
148EXPORT_SYMBOL_GPL(vr41xx_select_irda_module);
149
150static inline void siu_clear_fifo(struct uart_port *port)
151{
152 siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO);
153 siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
154 UART_FCR_CLEAR_XMIT);
155 siu_write(port, UART_FCR, 0);
156}
157
158static inline unsigned long siu_port_size(struct uart_port *port)
159{
160 switch (port->type) {
161 case PORT_VR41XX_SIU:
162 return 11UL;
163 case PORT_VR41XX_DSIU:
164 return 8UL;
165 }
166
167 return 0;
168}
169
170static inline unsigned int siu_check_type(struct uart_port *port)
171{
172 if (port->line == 0)
173 return PORT_VR41XX_SIU;
174 if (port->line == 1 && port->irq != -1)
175 return PORT_VR41XX_DSIU;
176
177 return PORT_UNKNOWN;
178}
179
180static inline const char *siu_type_name(struct uart_port *port)
181{
182 switch (port->type) {
183 case PORT_VR41XX_SIU:
184 return "SIU";
185 case PORT_VR41XX_DSIU:
186 return "DSIU";
187 }
188
189 return NULL;
190}
191
192static unsigned int siu_tx_empty(struct uart_port *port)
193{
194 uint8_t lsr;
195
196 lsr = siu_read(port, UART_LSR);
197 if (lsr & UART_LSR_TEMT)
198 return TIOCSER_TEMT;
199
200 return 0;
201}
202
203static void siu_set_mctrl(struct uart_port *port, unsigned int mctrl)
204{
205 uint8_t mcr = 0;
206
207 if (mctrl & TIOCM_DTR)
208 mcr |= UART_MCR_DTR;
209 if (mctrl & TIOCM_RTS)
210 mcr |= UART_MCR_RTS;
211 if (mctrl & TIOCM_OUT1)
212 mcr |= UART_MCR_OUT1;
213 if (mctrl & TIOCM_OUT2)
214 mcr |= UART_MCR_OUT2;
215 if (mctrl & TIOCM_LOOP)
216 mcr |= UART_MCR_LOOP;
217
218 siu_write(port, UART_MCR, mcr);
219}
220
221static unsigned int siu_get_mctrl(struct uart_port *port)
222{
223 uint8_t msr;
224 unsigned int mctrl = 0;
225
226 msr = siu_read(port, UART_MSR);
227 if (msr & UART_MSR_DCD)
228 mctrl |= TIOCM_CAR;
229 if (msr & UART_MSR_RI)
230 mctrl |= TIOCM_RNG;
231 if (msr & UART_MSR_DSR)
232 mctrl |= TIOCM_DSR;
233 if (msr & UART_MSR_CTS)
234 mctrl |= TIOCM_CTS;
235
236 return mctrl;
237}
238
239static void siu_stop_tx(struct uart_port *port)
240{
241 unsigned long flags;
242 uint8_t ier;
243
244 spin_lock_irqsave(&port->lock, flags);
245
246 ier = siu_read(port, UART_IER);
247 ier &= ~UART_IER_THRI;
248 siu_write(port, UART_IER, ier);
249
250 spin_unlock_irqrestore(&port->lock, flags);
251}
252
253static void siu_start_tx(struct uart_port *port)
254{
255 unsigned long flags;
256 uint8_t ier;
257
258 spin_lock_irqsave(&port->lock, flags);
259
260 ier = siu_read(port, UART_IER);
261 ier |= UART_IER_THRI;
262 siu_write(port, UART_IER, ier);
263
264 spin_unlock_irqrestore(&port->lock, flags);
265}
266
267static void siu_stop_rx(struct uart_port *port)
268{
269 unsigned long flags;
270 uint8_t ier;
271
272 spin_lock_irqsave(&port->lock, flags);
273
274 ier = siu_read(port, UART_IER);
275 ier &= ~UART_IER_RLSI;
276 siu_write(port, UART_IER, ier);
277
278 port->read_status_mask &= ~UART_LSR_DR;
279
280 spin_unlock_irqrestore(&port->lock, flags);
281}
282
283static void siu_enable_ms(struct uart_port *port)
284{
285 unsigned long flags;
286 uint8_t ier;
287
288 spin_lock_irqsave(&port->lock, flags);
289
290 ier = siu_read(port, UART_IER);
291 ier |= UART_IER_MSI;
292 siu_write(port, UART_IER, ier);
293
294 spin_unlock_irqrestore(&port->lock, flags);
295}
296
297static void siu_break_ctl(struct uart_port *port, int ctl)
298{
299 unsigned long flags;
300 uint8_t lcr;
301
302 spin_lock_irqsave(&port->lock, flags);
303
304 lcr = siu_read(port, UART_LCR);
305 if (ctl == -1)
306 lcr |= UART_LCR_SBC;
307 else
308 lcr &= ~UART_LCR_SBC;
309 siu_write(port, UART_LCR, lcr);
310
311 spin_unlock_irqrestore(&port->lock, flags);
312}
313
314static inline void receive_chars(struct uart_port *port, uint8_t *status)
315{
316 struct tty_struct *tty;
317 uint8_t lsr, ch;
318 char flag;
319 int max_count = RX_MAX_COUNT;
320
321 tty = port->state->port.tty;
322 lsr = *status;
323
324 do {
325 ch = siu_read(port, UART_RX);
326 port->icount.rx++;
327 flag = TTY_NORMAL;
328
329#ifdef CONFIG_SERIAL_VR41XX_CONSOLE
330 lsr |= lsr_break_flag[port->line];
331 lsr_break_flag[port->line] = 0;
332#endif
333 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_FE |
334 UART_LSR_PE | UART_LSR_OE))) {
335 if (lsr & UART_LSR_BI) {
336 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
337 port->icount.brk++;
338
339 if (uart_handle_break(port))
340 goto ignore_char;
341 }
342
343 if (lsr & UART_LSR_FE)
344 port->icount.frame++;
345 if (lsr & UART_LSR_PE)
346 port->icount.parity++;
347 if (lsr & UART_LSR_OE)
348 port->icount.overrun++;
349
350 lsr &= port->read_status_mask;
351 if (lsr & UART_LSR_BI)
352 flag = TTY_BREAK;
353 if (lsr & UART_LSR_FE)
354 flag = TTY_FRAME;
355 if (lsr & UART_LSR_PE)
356 flag = TTY_PARITY;
357 }
358
359 if (uart_handle_sysrq_char(port, ch))
360 goto ignore_char;
361
362 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
363
364 ignore_char:
365 lsr = siu_read(port, UART_LSR);
366 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
367
368 tty_flip_buffer_push(tty);
369
370 *status = lsr;
371}
372
373static inline void check_modem_status(struct uart_port *port)
374{
375 uint8_t msr;
376
377 msr = siu_read(port, UART_MSR);
378 if ((msr & UART_MSR_ANY_DELTA) == 0)
379 return;
380 if (msr & UART_MSR_DDCD)
381 uart_handle_dcd_change(port, msr & UART_MSR_DCD);
382 if (msr & UART_MSR_TERI)
383 port->icount.rng++;
384 if (msr & UART_MSR_DDSR)
385 port->icount.dsr++;
386 if (msr & UART_MSR_DCTS)
387 uart_handle_cts_change(port, msr & UART_MSR_CTS);
388
389 wake_up_interruptible(&port->state->port.delta_msr_wait);
390}
391
392static inline void transmit_chars(struct uart_port *port)
393{
394 struct circ_buf *xmit;
395 int max_count = TX_MAX_COUNT;
396
397 xmit = &port->state->xmit;
398
399 if (port->x_char) {
400 siu_write(port, UART_TX, port->x_char);
401 port->icount.tx++;
402 port->x_char = 0;
403 return;
404 }
405
406 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
407 siu_stop_tx(port);
408 return;
409 }
410
411 do {
412 siu_write(port, UART_TX, xmit->buf[xmit->tail]);
413 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
414 port->icount.tx++;
415 if (uart_circ_empty(xmit))
416 break;
417 } while (max_count-- > 0);
418
419 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
420 uart_write_wakeup(port);
421
422 if (uart_circ_empty(xmit))
423 siu_stop_tx(port);
424}
425
426static irqreturn_t siu_interrupt(int irq, void *dev_id)
427{
428 struct uart_port *port;
429 uint8_t iir, lsr;
430
431 port = (struct uart_port *)dev_id;
432
433 iir = siu_read(port, UART_IIR);
434 if (iir & UART_IIR_NO_INT)
435 return IRQ_NONE;
436
437 lsr = siu_read(port, UART_LSR);
438 if (lsr & UART_LSR_DR)
439 receive_chars(port, &lsr);
440
441 check_modem_status(port);
442
443 if (lsr & UART_LSR_THRE)
444 transmit_chars(port);
445
446 return IRQ_HANDLED;
447}
448
449static int siu_startup(struct uart_port *port)
450{
451 int retval;
452
453 if (port->membase == NULL)
454 return -ENODEV;
455
456 siu_clear_fifo(port);
457
458 (void)siu_read(port, UART_LSR);
459 (void)siu_read(port, UART_RX);
460 (void)siu_read(port, UART_IIR);
461 (void)siu_read(port, UART_MSR);
462
463 if (siu_read(port, UART_LSR) == 0xff)
464 return -ENODEV;
465
466 retval = request_irq(port->irq, siu_interrupt, 0, siu_type_name(port), port);
467 if (retval)
468 return retval;
469
470 if (port->type == PORT_VR41XX_DSIU)
471 vr41xx_enable_dsiuint(DSIUINT_ALL);
472
473 siu_write(port, UART_LCR, UART_LCR_WLEN8);
474
475 spin_lock_irq(&port->lock);
476 siu_set_mctrl(port, port->mctrl);
477 spin_unlock_irq(&port->lock);
478
479 siu_write(port, UART_IER, UART_IER_RLSI | UART_IER_RDI);
480
481 (void)siu_read(port, UART_LSR);
482 (void)siu_read(port, UART_RX);
483 (void)siu_read(port, UART_IIR);
484 (void)siu_read(port, UART_MSR);
485
486 return 0;
487}
488
489static void siu_shutdown(struct uart_port *port)
490{
491 unsigned long flags;
492 uint8_t lcr;
493
494 siu_write(port, UART_IER, 0);
495
496 spin_lock_irqsave(&port->lock, flags);
497
498 port->mctrl &= ~TIOCM_OUT2;
499 siu_set_mctrl(port, port->mctrl);
500
501 spin_unlock_irqrestore(&port->lock, flags);
502
503 lcr = siu_read(port, UART_LCR);
504 lcr &= ~UART_LCR_SBC;
505 siu_write(port, UART_LCR, lcr);
506
507 siu_clear_fifo(port);
508
509 (void)siu_read(port, UART_RX);
510
511 if (port->type == PORT_VR41XX_DSIU)
512 vr41xx_disable_dsiuint(DSIUINT_ALL);
513
514 free_irq(port->irq, port);
515}
516
517static void siu_set_termios(struct uart_port *port, struct ktermios *new,
518 struct ktermios *old)
519{
520 tcflag_t c_cflag, c_iflag;
521 uint8_t lcr, fcr, ier;
522 unsigned int baud, quot;
523 unsigned long flags;
524
525 c_cflag = new->c_cflag;
526 switch (c_cflag & CSIZE) {
527 case CS5:
528 lcr = UART_LCR_WLEN5;
529 break;
530 case CS6:
531 lcr = UART_LCR_WLEN6;
532 break;
533 case CS7:
534 lcr = UART_LCR_WLEN7;
535 break;
536 default:
537 lcr = UART_LCR_WLEN8;
538 break;
539 }
540
541 if (c_cflag & CSTOPB)
542 lcr |= UART_LCR_STOP;
543 if (c_cflag & PARENB)
544 lcr |= UART_LCR_PARITY;
545 if ((c_cflag & PARODD) != PARODD)
546 lcr |= UART_LCR_EPAR;
547 if (c_cflag & CMSPAR)
548 lcr |= UART_LCR_SPAR;
549
550 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
551 quot = uart_get_divisor(port, baud);
552
553 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
554
555 spin_lock_irqsave(&port->lock, flags);
556
557 uart_update_timeout(port, c_cflag, baud);
558
559 c_iflag = new->c_iflag;
560
561 port->read_status_mask = UART_LSR_THRE | UART_LSR_OE | UART_LSR_DR;
562 if (c_iflag & INPCK)
563 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
564 if (c_iflag & (BRKINT | PARMRK))
565 port->read_status_mask |= UART_LSR_BI;
566
567 port->ignore_status_mask = 0;
568 if (c_iflag & IGNPAR)
569 port->ignore_status_mask |= UART_LSR_FE | UART_LSR_PE;
570 if (c_iflag & IGNBRK) {
571 port->ignore_status_mask |= UART_LSR_BI;
572 if (c_iflag & IGNPAR)
573 port->ignore_status_mask |= UART_LSR_OE;
574 }
575
576 if ((c_cflag & CREAD) == 0)
577 port->ignore_status_mask |= UART_LSR_DR;
578
579 ier = siu_read(port, UART_IER);
580 ier &= ~UART_IER_MSI;
581 if (UART_ENABLE_MS(port, c_cflag))
582 ier |= UART_IER_MSI;
583 siu_write(port, UART_IER, ier);
584
585 siu_write(port, UART_LCR, lcr | UART_LCR_DLAB);
586
587 siu_write(port, UART_DLL, (uint8_t)quot);
588 siu_write(port, UART_DLM, (uint8_t)(quot >> 8));
589
590 siu_write(port, UART_LCR, lcr);
591
592 siu_write(port, UART_FCR, fcr);
593
594 siu_set_mctrl(port, port->mctrl);
595
596 spin_unlock_irqrestore(&port->lock, flags);
597}
598
599static void siu_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
600{
601 switch (state) {
602 case 0:
603 switch (port->type) {
604 case PORT_VR41XX_SIU:
605 vr41xx_supply_clock(SIU_CLOCK);
606 break;
607 case PORT_VR41XX_DSIU:
608 vr41xx_supply_clock(DSIU_CLOCK);
609 break;
610 }
611 break;
612 case 3:
613 switch (port->type) {
614 case PORT_VR41XX_SIU:
615 vr41xx_mask_clock(SIU_CLOCK);
616 break;
617 case PORT_VR41XX_DSIU:
618 vr41xx_mask_clock(DSIU_CLOCK);
619 break;
620 }
621 break;
622 }
623}
624
625static const char *siu_type(struct uart_port *port)
626{
627 return siu_type_name(port);
628}
629
630static void siu_release_port(struct uart_port *port)
631{
632 unsigned long size;
633
634 if (port->flags & UPF_IOREMAP) {
635 iounmap(port->membase);
636 port->membase = NULL;
637 }
638
639 size = siu_port_size(port);
640 release_mem_region(port->mapbase, size);
641}
642
643static int siu_request_port(struct uart_port *port)
644{
645 unsigned long size;
646 struct resource *res;
647
648 size = siu_port_size(port);
649 res = request_mem_region(port->mapbase, size, siu_type_name(port));
650 if (res == NULL)
651 return -EBUSY;
652
653 if (port->flags & UPF_IOREMAP) {
654 port->membase = ioremap(port->mapbase, size);
655 if (port->membase == NULL) {
656 release_resource(res);
657 return -ENOMEM;
658 }
659 }
660
661 return 0;
662}
663
664static void siu_config_port(struct uart_port *port, int flags)
665{
666 if (flags & UART_CONFIG_TYPE) {
667 port->type = siu_check_type(port);
668 (void)siu_request_port(port);
669 }
670}
671
672static int siu_verify_port(struct uart_port *port, struct serial_struct *serial)
673{
674 if (port->type != PORT_VR41XX_SIU && port->type != PORT_VR41XX_DSIU)
675 return -EINVAL;
676 if (port->irq != serial->irq)
677 return -EINVAL;
678 if (port->iotype != serial->io_type)
679 return -EINVAL;
680 if (port->mapbase != (unsigned long)serial->iomem_base)
681 return -EINVAL;
682
683 return 0;
684}
685
686static struct uart_ops siu_uart_ops = {
687 .tx_empty = siu_tx_empty,
688 .set_mctrl = siu_set_mctrl,
689 .get_mctrl = siu_get_mctrl,
690 .stop_tx = siu_stop_tx,
691 .start_tx = siu_start_tx,
692 .stop_rx = siu_stop_rx,
693 .enable_ms = siu_enable_ms,
694 .break_ctl = siu_break_ctl,
695 .startup = siu_startup,
696 .shutdown = siu_shutdown,
697 .set_termios = siu_set_termios,
698 .pm = siu_pm,
699 .type = siu_type,
700 .release_port = siu_release_port,
701 .request_port = siu_request_port,
702 .config_port = siu_config_port,
703 .verify_port = siu_verify_port,
704};
705
706static int siu_init_ports(struct platform_device *pdev)
707{
708 struct uart_port *port;
709 struct resource *res;
710 int *type = pdev->dev.platform_data;
711 int i;
712
713 if (!type)
714 return 0;
715
716 port = siu_uart_ports;
717 for (i = 0; i < SIU_PORTS_MAX; i++) {
718 port->type = type[i];
719 if (port->type == PORT_UNKNOWN)
720 continue;
721 port->irq = platform_get_irq(pdev, i);
722 port->uartclk = SIU_BAUD_BASE * 16;
723 port->fifosize = 16;
724 port->regshift = 0;
725 port->iotype = UPIO_MEM;
726 port->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
727 port->line = i;
728 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
729 port->mapbase = res->start;
730 port++;
731 }
732
733 return i;
734}
735
736#ifdef CONFIG_SERIAL_VR41XX_CONSOLE
737
738#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
739
740static void wait_for_xmitr(struct uart_port *port)
741{
742 int timeout = 10000;
743 uint8_t lsr, msr;
744
745 do {
746 lsr = siu_read(port, UART_LSR);
747 if (lsr & UART_LSR_BI)
748 lsr_break_flag[port->line] = UART_LSR_BI;
749
750 if ((lsr & BOTH_EMPTY) == BOTH_EMPTY)
751 break;
752 } while (timeout-- > 0);
753
754 if (port->flags & UPF_CONS_FLOW) {
755 timeout = 1000000;
756
757 do {
758 msr = siu_read(port, UART_MSR);
759 if ((msr & UART_MSR_CTS) != 0)
760 break;
761 } while (timeout-- > 0);
762 }
763}
764
765static void siu_console_putchar(struct uart_port *port, int ch)
766{
767 wait_for_xmitr(port);
768 siu_write(port, UART_TX, ch);
769}
770
771static void siu_console_write(struct console *con, const char *s, unsigned count)
772{
773 struct uart_port *port;
774 uint8_t ier;
775
776 port = &siu_uart_ports[con->index];
777
778 ier = siu_read(port, UART_IER);
779 siu_write(port, UART_IER, 0);
780
781 uart_console_write(port, s, count, siu_console_putchar);
782
783 wait_for_xmitr(port);
784 siu_write(port, UART_IER, ier);
785}
786
787static int __init siu_console_setup(struct console *con, char *options)
788{
789 struct uart_port *port;
790 int baud = 9600;
791 int parity = 'n';
792 int bits = 8;
793 int flow = 'n';
794
795 if (con->index >= SIU_PORTS_MAX)
796 con->index = 0;
797
798 port = &siu_uart_ports[con->index];
799 if (port->membase == NULL) {
800 if (port->mapbase == 0)
801 return -ENODEV;
802 port->membase = ioremap(port->mapbase, siu_port_size(port));
803 }
804
805 if (port->type == PORT_VR41XX_SIU)
806 vr41xx_select_siu_interface(SIU_INTERFACE_RS232C);
807
808 if (options != NULL)
809 uart_parse_options(options, &baud, &parity, &bits, &flow);
810
811 return uart_set_options(port, con, baud, parity, bits, flow);
812}
813
814static struct uart_driver siu_uart_driver;
815
816static struct console siu_console = {
817 .name = "ttyVR",
818 .write = siu_console_write,
819 .device = uart_console_device,
820 .setup = siu_console_setup,
821 .flags = CON_PRINTBUFFER,
822 .index = -1,
823 .data = &siu_uart_driver,
824};
825
826static int __devinit siu_console_init(void)
827{
828 struct uart_port *port;
829 int i;
830
831 for (i = 0; i < SIU_PORTS_MAX; i++) {
832 port = &siu_uart_ports[i];
833 port->ops = &siu_uart_ops;
834 }
835
836 register_console(&siu_console);
837
838 return 0;
839}
840
841console_initcall(siu_console_init);
842
843void __init vr41xx_siu_early_setup(struct uart_port *port)
844{
845 if (port->type == PORT_UNKNOWN)
846 return;
847
848 siu_uart_ports[port->line].line = port->line;
849 siu_uart_ports[port->line].type = port->type;
850 siu_uart_ports[port->line].uartclk = SIU_BAUD_BASE * 16;
851 siu_uart_ports[port->line].mapbase = port->mapbase;
852 siu_uart_ports[port->line].mapbase = port->mapbase;
853 siu_uart_ports[port->line].ops = &siu_uart_ops;
854}
855
856#define SERIAL_VR41XX_CONSOLE &siu_console
857#else
858#define SERIAL_VR41XX_CONSOLE NULL
859#endif
860
861static struct uart_driver siu_uart_driver = {
862 .owner = THIS_MODULE,
863 .driver_name = "SIU",
864 .dev_name = "ttyVR",
865 .major = SIU_MAJOR,
866 .minor = SIU_MINOR_BASE,
867 .cons = SERIAL_VR41XX_CONSOLE,
868};
869
870static int __devinit siu_probe(struct platform_device *dev)
871{
872 struct uart_port *port;
873 int num, i, retval;
874
875 num = siu_init_ports(dev);
876 if (num <= 0)
877 return -ENODEV;
878
879 siu_uart_driver.nr = num;
880 retval = uart_register_driver(&siu_uart_driver);
881 if (retval)
882 return retval;
883
884 for (i = 0; i < num; i++) {
885 port = &siu_uart_ports[i];
886 port->ops = &siu_uart_ops;
887 port->dev = &dev->dev;
888
889 retval = uart_add_one_port(&siu_uart_driver, port);
890 if (retval < 0) {
891 port->dev = NULL;
892 break;
893 }
894 }
895
896 if (i == 0 && retval < 0) {
897 uart_unregister_driver(&siu_uart_driver);
898 return retval;
899 }
900
901 return 0;
902}
903
904static int __devexit siu_remove(struct platform_device *dev)
905{
906 struct uart_port *port;
907 int i;
908
909 for (i = 0; i < siu_uart_driver.nr; i++) {
910 port = &siu_uart_ports[i];
911 if (port->dev == &dev->dev) {
912 uart_remove_one_port(&siu_uart_driver, port);
913 port->dev = NULL;
914 }
915 }
916
917 uart_unregister_driver(&siu_uart_driver);
918
919 return 0;
920}
921
922static int siu_suspend(struct platform_device *dev, pm_message_t state)
923{
924 struct uart_port *port;
925 int i;
926
927 for (i = 0; i < siu_uart_driver.nr; i++) {
928 port = &siu_uart_ports[i];
929 if ((port->type == PORT_VR41XX_SIU ||
930 port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
931 uart_suspend_port(&siu_uart_driver, port);
932
933 }
934
935 return 0;
936}
937
938static int siu_resume(struct platform_device *dev)
939{
940 struct uart_port *port;
941 int i;
942
943 for (i = 0; i < siu_uart_driver.nr; i++) {
944 port = &siu_uart_ports[i];
945 if ((port->type == PORT_VR41XX_SIU ||
946 port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
947 uart_resume_port(&siu_uart_driver, port);
948 }
949
950 return 0;
951}
952
953static struct platform_driver siu_device_driver = {
954 .probe = siu_probe,
955 .remove = __devexit_p(siu_remove),
956 .suspend = siu_suspend,
957 .resume = siu_resume,
958 .driver = {
959 .name = "SIU",
960 .owner = THIS_MODULE,
961 },
962};
963
964static int __init vr41xx_siu_init(void)
965{
966 return platform_driver_register(&siu_device_driver);
967}
968
969static void __exit vr41xx_siu_exit(void)
970{
971 platform_driver_unregister(&siu_device_driver);
972}
973
974module_init(vr41xx_siu_init);
975module_exit(vr41xx_siu_exit);
976
977MODULE_LICENSE("GPL");
978MODULE_ALIAS("platform:SIU");
diff --git a/drivers/tty/serial/vt8500_serial.c b/drivers/tty/serial/vt8500_serial.c
new file mode 100644
index 000000000000..322bf56c0d89
--- /dev/null
+++ b/drivers/tty/serial/vt8500_serial.c
@@ -0,0 +1,648 @@
1/*
2 * drivers/serial/vt8500_serial.c
3 *
4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
5 *
6 * Based on msm_serial.c, which is:
7 * Copyright (C) 2007 Google, Inc.
8 * Author: Robert Love <rlove@google.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#if defined(CONFIG_SERIAL_VT8500_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21# define SUPPORT_SYSRQ
22#endif
23
24#include <linux/hrtimer.h>
25#include <linux/delay.h>
26#include <linux/module.h>
27#include <linux/io.h>
28#include <linux/ioport.h>
29#include <linux/irq.h>
30#include <linux/init.h>
31#include <linux/console.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial_core.h>
35#include <linux/serial.h>
36#include <linux/slab.h>
37#include <linux/clk.h>
38#include <linux/platform_device.h>
39
40/*
41 * UART Register offsets
42 */
43
44#define VT8500_URTDR 0x0000 /* Transmit data */
45#define VT8500_URRDR 0x0004 /* Receive data */
46#define VT8500_URDIV 0x0008 /* Clock/Baud rate divisor */
47#define VT8500_URLCR 0x000C /* Line control */
48#define VT8500_URICR 0x0010 /* IrDA control */
49#define VT8500_URIER 0x0014 /* Interrupt enable */
50#define VT8500_URISR 0x0018 /* Interrupt status */
51#define VT8500_URUSR 0x001c /* UART status */
52#define VT8500_URFCR 0x0020 /* FIFO control */
53#define VT8500_URFIDX 0x0024 /* FIFO index */
54#define VT8500_URBKR 0x0028 /* Break signal count */
55#define VT8500_URTOD 0x002c /* Time out divisor */
56#define VT8500_TXFIFO 0x1000 /* Transmit FIFO (16x8) */
57#define VT8500_RXFIFO 0x1020 /* Receive FIFO (16x10) */
58
59/*
60 * Interrupt enable and status bits
61 */
62
63#define TXDE (1 << 0) /* Tx Data empty */
64#define RXDF (1 << 1) /* Rx Data full */
65#define TXFAE (1 << 2) /* Tx FIFO almost empty */
66#define TXFE (1 << 3) /* Tx FIFO empty */
67#define RXFAF (1 << 4) /* Rx FIFO almost full */
68#define RXFF (1 << 5) /* Rx FIFO full */
69#define TXUDR (1 << 6) /* Tx underrun */
70#define RXOVER (1 << 7) /* Rx overrun */
71#define PER (1 << 8) /* Parity error */
72#define FER (1 << 9) /* Frame error */
73#define TCTS (1 << 10) /* Toggle of CTS */
74#define RXTOUT (1 << 11) /* Rx timeout */
75#define BKDONE (1 << 12) /* Break signal done */
76#define ERR (1 << 13) /* AHB error response */
77
78#define RX_FIFO_INTS (RXFAF | RXFF | RXOVER | PER | FER | RXTOUT)
79#define TX_FIFO_INTS (TXFAE | TXFE | TXUDR)
80
81struct vt8500_port {
82 struct uart_port uart;
83 char name[16];
84 struct clk *clk;
85 unsigned int ier;
86};
87
88static inline void vt8500_write(struct uart_port *port, unsigned int val,
89 unsigned int off)
90{
91 writel(val, port->membase + off);
92}
93
94static inline unsigned int vt8500_read(struct uart_port *port, unsigned int off)
95{
96 return readl(port->membase + off);
97}
98
99static void vt8500_stop_tx(struct uart_port *port)
100{
101 struct vt8500_port *vt8500_port = container_of(port,
102 struct vt8500_port,
103 uart);
104
105 vt8500_port->ier &= ~TX_FIFO_INTS;
106 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
107}
108
109static void vt8500_stop_rx(struct uart_port *port)
110{
111 struct vt8500_port *vt8500_port = container_of(port,
112 struct vt8500_port,
113 uart);
114
115 vt8500_port->ier &= ~RX_FIFO_INTS;
116 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
117}
118
119static void vt8500_enable_ms(struct uart_port *port)
120{
121 struct vt8500_port *vt8500_port = container_of(port,
122 struct vt8500_port,
123 uart);
124
125 vt8500_port->ier |= TCTS;
126 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
127}
128
129static void handle_rx(struct uart_port *port)
130{
131 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
132 if (!tty) {
133 /* Discard data: no tty available */
134 int count = (vt8500_read(port, VT8500_URFIDX) & 0x1f00) >> 8;
135 u16 ch;
136 while (count--)
137 ch = readw(port->membase + VT8500_RXFIFO);
138 return;
139 }
140
141 /*
142 * Handle overrun
143 */
144 if ((vt8500_read(port, VT8500_URISR) & RXOVER)) {
145 port->icount.overrun++;
146 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
147 }
148
149 /* and now the main RX loop */
150 while (vt8500_read(port, VT8500_URFIDX) & 0x1f00) {
151 unsigned int c;
152 char flag = TTY_NORMAL;
153
154 c = readw(port->membase + VT8500_RXFIFO) & 0x3ff;
155
156 /* Mask conditions we're ignorning. */
157 c &= ~port->read_status_mask;
158
159 if (c & FER) {
160 port->icount.frame++;
161 flag = TTY_FRAME;
162 } else if (c & PER) {
163 port->icount.parity++;
164 flag = TTY_PARITY;
165 }
166 port->icount.rx++;
167
168 if (!uart_handle_sysrq_char(port, c))
169 tty_insert_flip_char(tty, c, flag);
170 }
171
172 tty_flip_buffer_push(tty);
173 tty_kref_put(tty);
174}
175
176static void handle_tx(struct uart_port *port)
177{
178 struct circ_buf *xmit = &port->state->xmit;
179
180 if (port->x_char) {
181 writeb(port->x_char, port->membase + VT8500_TXFIFO);
182 port->icount.tx++;
183 port->x_char = 0;
184 }
185 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
186 vt8500_stop_tx(port);
187 return;
188 }
189
190 while ((vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16) {
191 if (uart_circ_empty(xmit))
192 break;
193
194 writeb(xmit->buf[xmit->tail], port->membase + VT8500_TXFIFO);
195
196 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
197 port->icount.tx++;
198 }
199
200 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
201 uart_write_wakeup(port);
202
203 if (uart_circ_empty(xmit))
204 vt8500_stop_tx(port);
205}
206
207static void vt8500_start_tx(struct uart_port *port)
208{
209 struct vt8500_port *vt8500_port = container_of(port,
210 struct vt8500_port,
211 uart);
212
213 vt8500_port->ier &= ~TX_FIFO_INTS;
214 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
215 handle_tx(port);
216 vt8500_port->ier |= TX_FIFO_INTS;
217 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
218}
219
220static void handle_delta_cts(struct uart_port *port)
221{
222 port->icount.cts++;
223 wake_up_interruptible(&port->state->port.delta_msr_wait);
224}
225
226static irqreturn_t vt8500_irq(int irq, void *dev_id)
227{
228 struct uart_port *port = dev_id;
229 unsigned long isr;
230
231 spin_lock(&port->lock);
232 isr = vt8500_read(port, VT8500_URISR);
233
234 /* Acknowledge active status bits */
235 vt8500_write(port, isr, VT8500_URISR);
236
237 if (isr & RX_FIFO_INTS)
238 handle_rx(port);
239 if (isr & TX_FIFO_INTS)
240 handle_tx(port);
241 if (isr & TCTS)
242 handle_delta_cts(port);
243
244 spin_unlock(&port->lock);
245
246 return IRQ_HANDLED;
247}
248
249static unsigned int vt8500_tx_empty(struct uart_port *port)
250{
251 return (vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16 ?
252 TIOCSER_TEMT : 0;
253}
254
255static unsigned int vt8500_get_mctrl(struct uart_port *port)
256{
257 unsigned int usr;
258
259 usr = vt8500_read(port, VT8500_URUSR);
260 if (usr & (1 << 4))
261 return TIOCM_CTS;
262 else
263 return 0;
264}
265
266static void vt8500_set_mctrl(struct uart_port *port, unsigned int mctrl)
267{
268}
269
270static void vt8500_break_ctl(struct uart_port *port, int break_ctl)
271{
272 if (break_ctl)
273 vt8500_write(port, vt8500_read(port, VT8500_URLCR) | (1 << 9),
274 VT8500_URLCR);
275}
276
277static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud)
278{
279 unsigned long div;
280 unsigned int loops = 1000;
281
282 div = vt8500_read(port, VT8500_URDIV) & ~(0x3ff);
283
284 if (unlikely((baud < 900) || (baud > 921600)))
285 div |= 7;
286 else
287 div |= (921600 / baud) - 1;
288
289 while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops)
290 cpu_relax();
291 vt8500_write(port, div, VT8500_URDIV);
292
293 return baud;
294}
295
296static int vt8500_startup(struct uart_port *port)
297{
298 struct vt8500_port *vt8500_port =
299 container_of(port, struct vt8500_port, uart);
300 int ret;
301
302 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
303 "vt8500_serial%d", port->line);
304
305 ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH,
306 vt8500_port->name, port);
307 if (unlikely(ret))
308 return ret;
309
310 vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */
311
312 return 0;
313}
314
315static void vt8500_shutdown(struct uart_port *port)
316{
317 struct vt8500_port *vt8500_port =
318 container_of(port, struct vt8500_port, uart);
319
320 vt8500_port->ier = 0;
321
322 /* disable interrupts and FIFOs */
323 vt8500_write(&vt8500_port->uart, 0, VT8500_URIER);
324 vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR);
325 free_irq(port->irq, port);
326}
327
328static void vt8500_set_termios(struct uart_port *port,
329 struct ktermios *termios,
330 struct ktermios *old)
331{
332 struct vt8500_port *vt8500_port =
333 container_of(port, struct vt8500_port, uart);
334 unsigned long flags;
335 unsigned int baud, lcr;
336 unsigned int loops = 1000;
337
338 spin_lock_irqsave(&port->lock, flags);
339
340 /* calculate and set baud rate */
341 baud = uart_get_baud_rate(port, termios, old, 900, 921600);
342 baud = vt8500_set_baud_rate(port, baud);
343 if (tty_termios_baud_rate(termios))
344 tty_termios_encode_baud_rate(termios, baud, baud);
345
346 /* calculate parity */
347 lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR);
348 lcr &= ~((1 << 5) | (1 << 4));
349 if (termios->c_cflag & PARENB) {
350 lcr |= (1 << 4);
351 termios->c_cflag &= ~CMSPAR;
352 if (termios->c_cflag & PARODD)
353 lcr |= (1 << 5);
354 }
355
356 /* calculate bits per char */
357 lcr &= ~(1 << 2);
358 switch (termios->c_cflag & CSIZE) {
359 case CS7:
360 break;
361 case CS8:
362 default:
363 lcr |= (1 << 2);
364 termios->c_cflag &= ~CSIZE;
365 termios->c_cflag |= CS8;
366 break;
367 }
368
369 /* calculate stop bits */
370 lcr &= ~(1 << 3);
371 if (termios->c_cflag & CSTOPB)
372 lcr |= (1 << 3);
373
374 /* set parity, bits per char, and stop bit */
375 vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR);
376
377 /* Configure status bits to ignore based on termio flags. */
378 port->read_status_mask = 0;
379 if (termios->c_iflag & IGNPAR)
380 port->read_status_mask = FER | PER;
381
382 uart_update_timeout(port, termios->c_cflag, baud);
383
384 /* Reset FIFOs */
385 vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR);
386 while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc)
387 && --loops)
388 cpu_relax();
389
390 /* Every possible FIFO-related interrupt */
391 vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS;
392
393 /*
394 * CTS flow control
395 */
396 if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag))
397 vt8500_port->ier |= TCTS;
398
399 vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR);
400 vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER);
401
402 spin_unlock_irqrestore(&port->lock, flags);
403}
404
405static const char *vt8500_type(struct uart_port *port)
406{
407 struct vt8500_port *vt8500_port =
408 container_of(port, struct vt8500_port, uart);
409 return vt8500_port->name;
410}
411
412static void vt8500_release_port(struct uart_port *port)
413{
414}
415
416static int vt8500_request_port(struct uart_port *port)
417{
418 return 0;
419}
420
421static void vt8500_config_port(struct uart_port *port, int flags)
422{
423 port->type = PORT_VT8500;
424}
425
426static int vt8500_verify_port(struct uart_port *port,
427 struct serial_struct *ser)
428{
429 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500))
430 return -EINVAL;
431 if (unlikely(port->irq != ser->irq))
432 return -EINVAL;
433 return 0;
434}
435
436static struct vt8500_port *vt8500_uart_ports[4];
437static struct uart_driver vt8500_uart_driver;
438
439#ifdef CONFIG_SERIAL_VT8500_CONSOLE
440
441static inline void wait_for_xmitr(struct uart_port *port)
442{
443 unsigned int status, tmout = 10000;
444
445 /* Wait up to 10ms for the character(s) to be sent. */
446 do {
447 status = vt8500_read(port, VT8500_URFIDX);
448
449 if (--tmout == 0)
450 break;
451 udelay(1);
452 } while (status & 0x10);
453}
454
455static void vt8500_console_putchar(struct uart_port *port, int c)
456{
457 wait_for_xmitr(port);
458 writeb(c, port->membase + VT8500_TXFIFO);
459}
460
461static void vt8500_console_write(struct console *co, const char *s,
462 unsigned int count)
463{
464 struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index];
465 unsigned long ier;
466
467 BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr);
468
469 ier = vt8500_read(&vt8500_port->uart, VT8500_URIER);
470 vt8500_write(&vt8500_port->uart, VT8500_URIER, 0);
471
472 uart_console_write(&vt8500_port->uart, s, count,
473 vt8500_console_putchar);
474
475 /*
476 * Finally, wait for transmitter to become empty
477 * and switch back to FIFO
478 */
479 wait_for_xmitr(&vt8500_port->uart);
480 vt8500_write(&vt8500_port->uart, VT8500_URIER, ier);
481}
482
483static int __init vt8500_console_setup(struct console *co, char *options)
484{
485 struct vt8500_port *vt8500_port;
486 int baud = 9600;
487 int bits = 8;
488 int parity = 'n';
489 int flow = 'n';
490
491 if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0))
492 return -ENXIO;
493
494 vt8500_port = vt8500_uart_ports[co->index];
495
496 if (!vt8500_port)
497 return -ENODEV;
498
499 if (options)
500 uart_parse_options(options, &baud, &parity, &bits, &flow);
501
502 return uart_set_options(&vt8500_port->uart,
503 co, baud, parity, bits, flow);
504}
505
506static struct console vt8500_console = {
507 .name = "ttyWMT",
508 .write = vt8500_console_write,
509 .device = uart_console_device,
510 .setup = vt8500_console_setup,
511 .flags = CON_PRINTBUFFER,
512 .index = -1,
513 .data = &vt8500_uart_driver,
514};
515
516#define VT8500_CONSOLE (&vt8500_console)
517
518#else
519#define VT8500_CONSOLE NULL
520#endif
521
522static struct uart_ops vt8500_uart_pops = {
523 .tx_empty = vt8500_tx_empty,
524 .set_mctrl = vt8500_set_mctrl,
525 .get_mctrl = vt8500_get_mctrl,
526 .stop_tx = vt8500_stop_tx,
527 .start_tx = vt8500_start_tx,
528 .stop_rx = vt8500_stop_rx,
529 .enable_ms = vt8500_enable_ms,
530 .break_ctl = vt8500_break_ctl,
531 .startup = vt8500_startup,
532 .shutdown = vt8500_shutdown,
533 .set_termios = vt8500_set_termios,
534 .type = vt8500_type,
535 .release_port = vt8500_release_port,
536 .request_port = vt8500_request_port,
537 .config_port = vt8500_config_port,
538 .verify_port = vt8500_verify_port,
539};
540
541static struct uart_driver vt8500_uart_driver = {
542 .owner = THIS_MODULE,
543 .driver_name = "vt8500_serial",
544 .dev_name = "ttyWMT",
545 .nr = 6,
546 .cons = VT8500_CONSOLE,
547};
548
549static int __init vt8500_serial_probe(struct platform_device *pdev)
550{
551 struct vt8500_port *vt8500_port;
552 struct resource *mmres, *irqres;
553 int ret;
554
555 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
556 irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
557 if (!mmres || !irqres)
558 return -ENODEV;
559
560 vt8500_port = kzalloc(sizeof(struct vt8500_port), GFP_KERNEL);
561 if (!vt8500_port)
562 return -ENOMEM;
563
564 vt8500_port->uart.type = PORT_VT8500;
565 vt8500_port->uart.iotype = UPIO_MEM;
566 vt8500_port->uart.mapbase = mmres->start;
567 vt8500_port->uart.irq = irqres->start;
568 vt8500_port->uart.fifosize = 16;
569 vt8500_port->uart.ops = &vt8500_uart_pops;
570 vt8500_port->uart.line = pdev->id;
571 vt8500_port->uart.dev = &pdev->dev;
572 vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
573 vt8500_port->uart.uartclk = 24000000;
574
575 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
576 "VT8500 UART%d", pdev->id);
577
578 vt8500_port->uart.membase = ioremap(mmres->start,
579 mmres->end - mmres->start + 1);
580 if (!vt8500_port->uart.membase) {
581 ret = -ENOMEM;
582 goto err;
583 }
584
585 vt8500_uart_ports[pdev->id] = vt8500_port;
586
587 uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart);
588
589 platform_set_drvdata(pdev, vt8500_port);
590
591 return 0;
592
593err:
594 kfree(vt8500_port);
595 return ret;
596}
597
598static int __devexit vt8500_serial_remove(struct platform_device *pdev)
599{
600 struct vt8500_port *vt8500_port = platform_get_drvdata(pdev);
601
602 platform_set_drvdata(pdev, NULL);
603 uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart);
604 kfree(vt8500_port);
605
606 return 0;
607}
608
609static struct platform_driver vt8500_platform_driver = {
610 .probe = vt8500_serial_probe,
611 .remove = vt8500_serial_remove,
612 .driver = {
613 .name = "vt8500_serial",
614 .owner = THIS_MODULE,
615 },
616};
617
618static int __init vt8500_serial_init(void)
619{
620 int ret;
621
622 ret = uart_register_driver(&vt8500_uart_driver);
623 if (unlikely(ret))
624 return ret;
625
626 ret = platform_driver_register(&vt8500_platform_driver);
627
628 if (unlikely(ret))
629 uart_unregister_driver(&vt8500_uart_driver);
630
631 return ret;
632}
633
634static void __exit vt8500_serial_exit(void)
635{
636#ifdef CONFIG_SERIAL_VT8500_CONSOLE
637 unregister_console(&vt8500_console);
638#endif
639 platform_driver_unregister(&vt8500_platform_driver);
640 uart_unregister_driver(&vt8500_uart_driver);
641}
642
643module_init(vt8500_serial_init);
644module_exit(vt8500_serial_exit);
645
646MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
647MODULE_DESCRIPTION("Driver for vt8500 serial device");
648MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/zs.c b/drivers/tty/serial/zs.c
new file mode 100644
index 000000000000..1a7fd3e70315
--- /dev/null
+++ b/drivers/tty/serial/zs.c
@@ -0,0 +1,1304 @@
1/*
2 * zs.c: Serial port driver for IOASIC DECstations.
3 *
4 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5 * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
6 *
7 * DECstation changes
8 * Copyright (C) 1998-2000 Harald Koerfgen
9 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
10 *
11 * For the rest of the code the original Copyright applies:
12 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
14 *
15 *
16 * Note: for IOASIC systems the wiring is as follows:
17 *
18 * mouse/keyboard:
19 * DIN-7 MJ-4 signal SCC
20 * 2 1 TxD <- A.TxD
21 * 3 4 RxD -> A.RxD
22 *
23 * EIA-232/EIA-423:
24 * DB-25 MMJ-6 signal SCC
25 * 2 2 TxD <- B.TxD
26 * 3 5 RxD -> B.RxD
27 * 4 RTS <- ~A.RTS
28 * 5 CTS -> ~B.CTS
29 * 6 6 DSR -> ~A.SYNC
30 * 8 CD -> ~B.DCD
31 * 12 DSRS(DCE) -> ~A.CTS (*)
32 * 15 TxC -> B.TxC
33 * 17 RxC -> B.RxC
34 * 20 1 DTR <- ~A.DTR
35 * 22 RI -> ~A.DCD
36 * 23 DSRS(DTE) <- ~B.RTS
37 *
38 * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39 * is shared with DSRS(DTE) at pin 23.
40 *
41 * As you can immediately notice the wiring of the RTS, DTR and DSR signals
42 * is a bit odd. This makes the handling of port B unnecessarily
43 * complicated and prevents the use of some automatic modes of operation.
44 */
45
46#if defined(CONFIG_SERIAL_ZS_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
47#define SUPPORT_SYSRQ
48#endif
49
50#include <linux/bug.h>
51#include <linux/console.h>
52#include <linux/delay.h>
53#include <linux/errno.h>
54#include <linux/init.h>
55#include <linux/interrupt.h>
56#include <linux/io.h>
57#include <linux/ioport.h>
58#include <linux/irqflags.h>
59#include <linux/kernel.h>
60#include <linux/major.h>
61#include <linux/serial.h>
62#include <linux/serial_core.h>
63#include <linux/spinlock.h>
64#include <linux/sysrq.h>
65#include <linux/tty.h>
66#include <linux/types.h>
67
68#include <asm/atomic.h>
69#include <asm/system.h>
70
71#include <asm/dec/interrupts.h>
72#include <asm/dec/ioasic_addrs.h>
73#include <asm/dec/system.h>
74
75#include "zs.h"
76
77
78MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
79MODULE_DESCRIPTION("DECstation Z85C30 serial driver");
80MODULE_LICENSE("GPL");
81
82
83static char zs_name[] __initdata = "DECstation Z85C30 serial driver version ";
84static char zs_version[] __initdata = "0.10";
85
86/*
87 * It would be nice to dynamically allocate everything that
88 * depends on ZS_NUM_SCCS, so we could support any number of
89 * Z85C30s, but for now...
90 */
91#define ZS_NUM_SCCS 2 /* Max # of ZS chips supported. */
92#define ZS_NUM_CHAN 2 /* 2 channels per chip. */
93#define ZS_CHAN_A 0 /* Index of the channel A. */
94#define ZS_CHAN_B 1 /* Index of the channel B. */
95#define ZS_CHAN_IO_SIZE 8 /* IOMEM space size. */
96#define ZS_CHAN_IO_STRIDE 4 /* Register alignment. */
97#define ZS_CHAN_IO_OFFSET 1 /* The SCC resides on the high byte
98 of the 16-bit IOBUS. */
99#define ZS_CLOCK 7372800 /* Z85C30 PCLK input clock rate. */
100
101#define to_zport(uport) container_of(uport, struct zs_port, port)
102
103struct zs_parms {
104 resource_size_t scc[ZS_NUM_SCCS];
105 int irq[ZS_NUM_SCCS];
106};
107
108static struct zs_scc zs_sccs[ZS_NUM_SCCS];
109
110static u8 zs_init_regs[ZS_NUM_REGS] __initdata = {
111 0, /* write 0 */
112 PAR_SPEC, /* write 1 */
113 0, /* write 2 */
114 0, /* write 3 */
115 X16CLK | SB1, /* write 4 */
116 0, /* write 5 */
117 0, 0, 0, /* write 6, 7, 8 */
118 MIE | DLC | NV, /* write 9 */
119 NRZ, /* write 10 */
120 TCBR | RCBR, /* write 11 */
121 0, 0, /* BRG time constant, write 12 + 13 */
122 BRSRC | BRENABL, /* write 14 */
123 0, /* write 15 */
124};
125
126/*
127 * Debugging.
128 */
129#undef ZS_DEBUG_REGS
130
131
132/*
133 * Reading and writing Z85C30 registers.
134 */
135static void recovery_delay(void)
136{
137 udelay(2);
138}
139
140static u8 read_zsreg(struct zs_port *zport, int reg)
141{
142 void __iomem *control = zport->port.membase + ZS_CHAN_IO_OFFSET;
143 u8 retval;
144
145 if (reg != 0) {
146 writeb(reg & 0xf, control);
147 fast_iob();
148 recovery_delay();
149 }
150 retval = readb(control);
151 recovery_delay();
152 return retval;
153}
154
155static void write_zsreg(struct zs_port *zport, int reg, u8 value)
156{
157 void __iomem *control = zport->port.membase + ZS_CHAN_IO_OFFSET;
158
159 if (reg != 0) {
160 writeb(reg & 0xf, control);
161 fast_iob(); recovery_delay();
162 }
163 writeb(value, control);
164 fast_iob();
165 recovery_delay();
166 return;
167}
168
169static u8 read_zsdata(struct zs_port *zport)
170{
171 void __iomem *data = zport->port.membase +
172 ZS_CHAN_IO_STRIDE + ZS_CHAN_IO_OFFSET;
173 u8 retval;
174
175 retval = readb(data);
176 recovery_delay();
177 return retval;
178}
179
180static void write_zsdata(struct zs_port *zport, u8 value)
181{
182 void __iomem *data = zport->port.membase +
183 ZS_CHAN_IO_STRIDE + ZS_CHAN_IO_OFFSET;
184
185 writeb(value, data);
186 fast_iob();
187 recovery_delay();
188 return;
189}
190
191#ifdef ZS_DEBUG_REGS
192void zs_dump(void)
193{
194 struct zs_port *zport;
195 int i, j;
196
197 for (i = 0; i < ZS_NUM_SCCS * ZS_NUM_CHAN; i++) {
198 zport = &zs_sccs[i / ZS_NUM_CHAN].zport[i % ZS_NUM_CHAN];
199
200 if (!zport->scc)
201 continue;
202
203 for (j = 0; j < 16; j++)
204 printk("W%-2d = 0x%02x\t", j, zport->regs[j]);
205 printk("\n");
206 for (j = 0; j < 16; j++)
207 printk("R%-2d = 0x%02x\t", j, read_zsreg(zport, j));
208 printk("\n\n");
209 }
210}
211#endif
212
213
214static void zs_spin_lock_cond_irq(spinlock_t *lock, int irq)
215{
216 if (irq)
217 spin_lock_irq(lock);
218 else
219 spin_lock(lock);
220}
221
222static void zs_spin_unlock_cond_irq(spinlock_t *lock, int irq)
223{
224 if (irq)
225 spin_unlock_irq(lock);
226 else
227 spin_unlock(lock);
228}
229
230static int zs_receive_drain(struct zs_port *zport)
231{
232 int loops = 10000;
233
234 while ((read_zsreg(zport, R0) & Rx_CH_AV) && --loops)
235 read_zsdata(zport);
236 return loops;
237}
238
239static int zs_transmit_drain(struct zs_port *zport, int irq)
240{
241 struct zs_scc *scc = zport->scc;
242 int loops = 10000;
243
244 while (!(read_zsreg(zport, R0) & Tx_BUF_EMP) && --loops) {
245 zs_spin_unlock_cond_irq(&scc->zlock, irq);
246 udelay(2);
247 zs_spin_lock_cond_irq(&scc->zlock, irq);
248 }
249 return loops;
250}
251
252static int zs_line_drain(struct zs_port *zport, int irq)
253{
254 struct zs_scc *scc = zport->scc;
255 int loops = 10000;
256
257 while (!(read_zsreg(zport, R1) & ALL_SNT) && --loops) {
258 zs_spin_unlock_cond_irq(&scc->zlock, irq);
259 udelay(2);
260 zs_spin_lock_cond_irq(&scc->zlock, irq);
261 }
262 return loops;
263}
264
265
266static void load_zsregs(struct zs_port *zport, u8 *regs, int irq)
267{
268 /* Let the current transmission finish. */
269 zs_line_drain(zport, irq);
270 /* Load 'em up. */
271 write_zsreg(zport, R3, regs[3] & ~RxENABLE);
272 write_zsreg(zport, R5, regs[5] & ~TxENAB);
273 write_zsreg(zport, R4, regs[4]);
274 write_zsreg(zport, R9, regs[9]);
275 write_zsreg(zport, R1, regs[1]);
276 write_zsreg(zport, R2, regs[2]);
277 write_zsreg(zport, R10, regs[10]);
278 write_zsreg(zport, R14, regs[14] & ~BRENABL);
279 write_zsreg(zport, R11, regs[11]);
280 write_zsreg(zport, R12, regs[12]);
281 write_zsreg(zport, R13, regs[13]);
282 write_zsreg(zport, R14, regs[14]);
283 write_zsreg(zport, R15, regs[15]);
284 if (regs[3] & RxENABLE)
285 write_zsreg(zport, R3, regs[3]);
286 if (regs[5] & TxENAB)
287 write_zsreg(zport, R5, regs[5]);
288 return;
289}
290
291
292/*
293 * Status handling routines.
294 */
295
296/*
297 * zs_tx_empty() -- get the transmitter empty status
298 *
299 * Purpose: Let user call ioctl() to get info when the UART physically
300 * is emptied. On bus types like RS485, the transmitter must
301 * release the bus after transmitting. This must be done when
302 * the transmit shift register is empty, not be done when the
303 * transmit holding register is empty. This functionality
304 * allows an RS485 driver to be written in user space.
305 */
306static unsigned int zs_tx_empty(struct uart_port *uport)
307{
308 struct zs_port *zport = to_zport(uport);
309 struct zs_scc *scc = zport->scc;
310 unsigned long flags;
311 u8 status;
312
313 spin_lock_irqsave(&scc->zlock, flags);
314 status = read_zsreg(zport, R1);
315 spin_unlock_irqrestore(&scc->zlock, flags);
316
317 return status & ALL_SNT ? TIOCSER_TEMT : 0;
318}
319
320static unsigned int zs_raw_get_ab_mctrl(struct zs_port *zport_a,
321 struct zs_port *zport_b)
322{
323 u8 status_a, status_b;
324 unsigned int mctrl;
325
326 status_a = read_zsreg(zport_a, R0);
327 status_b = read_zsreg(zport_b, R0);
328
329 mctrl = ((status_b & CTS) ? TIOCM_CTS : 0) |
330 ((status_b & DCD) ? TIOCM_CAR : 0) |
331 ((status_a & DCD) ? TIOCM_RNG : 0) |
332 ((status_a & SYNC_HUNT) ? TIOCM_DSR : 0);
333
334 return mctrl;
335}
336
337static unsigned int zs_raw_get_mctrl(struct zs_port *zport)
338{
339 struct zs_port *zport_a = &zport->scc->zport[ZS_CHAN_A];
340
341 return zport != zport_a ? zs_raw_get_ab_mctrl(zport_a, zport) : 0;
342}
343
344static unsigned int zs_raw_xor_mctrl(struct zs_port *zport)
345{
346 struct zs_port *zport_a = &zport->scc->zport[ZS_CHAN_A];
347 unsigned int mmask, mctrl, delta;
348 u8 mask_a, mask_b;
349
350 if (zport == zport_a)
351 return 0;
352
353 mask_a = zport_a->regs[15];
354 mask_b = zport->regs[15];
355
356 mmask = ((mask_b & CTSIE) ? TIOCM_CTS : 0) |
357 ((mask_b & DCDIE) ? TIOCM_CAR : 0) |
358 ((mask_a & DCDIE) ? TIOCM_RNG : 0) |
359 ((mask_a & SYNCIE) ? TIOCM_DSR : 0);
360
361 mctrl = zport->mctrl;
362 if (mmask) {
363 mctrl &= ~mmask;
364 mctrl |= zs_raw_get_ab_mctrl(zport_a, zport) & mmask;
365 }
366
367 delta = mctrl ^ zport->mctrl;
368 if (delta)
369 zport->mctrl = mctrl;
370
371 return delta;
372}
373
374static unsigned int zs_get_mctrl(struct uart_port *uport)
375{
376 struct zs_port *zport = to_zport(uport);
377 struct zs_scc *scc = zport->scc;
378 unsigned int mctrl;
379
380 spin_lock(&scc->zlock);
381 mctrl = zs_raw_get_mctrl(zport);
382 spin_unlock(&scc->zlock);
383
384 return mctrl;
385}
386
387static void zs_set_mctrl(struct uart_port *uport, unsigned int mctrl)
388{
389 struct zs_port *zport = to_zport(uport);
390 struct zs_scc *scc = zport->scc;
391 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
392 u8 oldloop, newloop;
393
394 spin_lock(&scc->zlock);
395 if (zport != zport_a) {
396 if (mctrl & TIOCM_DTR)
397 zport_a->regs[5] |= DTR;
398 else
399 zport_a->regs[5] &= ~DTR;
400 if (mctrl & TIOCM_RTS)
401 zport_a->regs[5] |= RTS;
402 else
403 zport_a->regs[5] &= ~RTS;
404 write_zsreg(zport_a, R5, zport_a->regs[5]);
405 }
406
407 /* Rarely modified, so don't poke at hardware unless necessary. */
408 oldloop = zport->regs[14];
409 newloop = oldloop;
410 if (mctrl & TIOCM_LOOP)
411 newloop |= LOOPBAK;
412 else
413 newloop &= ~LOOPBAK;
414 if (newloop != oldloop) {
415 zport->regs[14] = newloop;
416 write_zsreg(zport, R14, zport->regs[14]);
417 }
418 spin_unlock(&scc->zlock);
419}
420
421static void zs_raw_stop_tx(struct zs_port *zport)
422{
423 write_zsreg(zport, R0, RES_Tx_P);
424 zport->tx_stopped = 1;
425}
426
427static void zs_stop_tx(struct uart_port *uport)
428{
429 struct zs_port *zport = to_zport(uport);
430 struct zs_scc *scc = zport->scc;
431
432 spin_lock(&scc->zlock);
433 zs_raw_stop_tx(zport);
434 spin_unlock(&scc->zlock);
435}
436
437static void zs_raw_transmit_chars(struct zs_port *);
438
439static void zs_start_tx(struct uart_port *uport)
440{
441 struct zs_port *zport = to_zport(uport);
442 struct zs_scc *scc = zport->scc;
443
444 spin_lock(&scc->zlock);
445 if (zport->tx_stopped) {
446 zs_transmit_drain(zport, 0);
447 zport->tx_stopped = 0;
448 zs_raw_transmit_chars(zport);
449 }
450 spin_unlock(&scc->zlock);
451}
452
453static void zs_stop_rx(struct uart_port *uport)
454{
455 struct zs_port *zport = to_zport(uport);
456 struct zs_scc *scc = zport->scc;
457 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
458
459 spin_lock(&scc->zlock);
460 zport->regs[15] &= ~BRKIE;
461 zport->regs[1] &= ~(RxINT_MASK | TxINT_ENAB);
462 zport->regs[1] |= RxINT_DISAB;
463
464 if (zport != zport_a) {
465 /* A-side DCD tracks RI and SYNC tracks DSR. */
466 zport_a->regs[15] &= ~(DCDIE | SYNCIE);
467 write_zsreg(zport_a, R15, zport_a->regs[15]);
468 if (!(zport_a->regs[15] & BRKIE)) {
469 zport_a->regs[1] &= ~EXT_INT_ENAB;
470 write_zsreg(zport_a, R1, zport_a->regs[1]);
471 }
472
473 /* This-side DCD tracks DCD and CTS tracks CTS. */
474 zport->regs[15] &= ~(DCDIE | CTSIE);
475 zport->regs[1] &= ~EXT_INT_ENAB;
476 } else {
477 /* DCD tracks RI and SYNC tracks DSR for the B side. */
478 if (!(zport->regs[15] & (DCDIE | SYNCIE)))
479 zport->regs[1] &= ~EXT_INT_ENAB;
480 }
481
482 write_zsreg(zport, R15, zport->regs[15]);
483 write_zsreg(zport, R1, zport->regs[1]);
484 spin_unlock(&scc->zlock);
485}
486
487static void zs_enable_ms(struct uart_port *uport)
488{
489 struct zs_port *zport = to_zport(uport);
490 struct zs_scc *scc = zport->scc;
491 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
492
493 if (zport == zport_a)
494 return;
495
496 spin_lock(&scc->zlock);
497
498 /* Clear Ext interrupts if not being handled already. */
499 if (!(zport_a->regs[1] & EXT_INT_ENAB))
500 write_zsreg(zport_a, R0, RES_EXT_INT);
501
502 /* A-side DCD tracks RI and SYNC tracks DSR. */
503 zport_a->regs[1] |= EXT_INT_ENAB;
504 zport_a->regs[15] |= DCDIE | SYNCIE;
505
506 /* This-side DCD tracks DCD and CTS tracks CTS. */
507 zport->regs[15] |= DCDIE | CTSIE;
508
509 zs_raw_xor_mctrl(zport);
510
511 write_zsreg(zport_a, R1, zport_a->regs[1]);
512 write_zsreg(zport_a, R15, zport_a->regs[15]);
513 write_zsreg(zport, R15, zport->regs[15]);
514 spin_unlock(&scc->zlock);
515}
516
517static void zs_break_ctl(struct uart_port *uport, int break_state)
518{
519 struct zs_port *zport = to_zport(uport);
520 struct zs_scc *scc = zport->scc;
521 unsigned long flags;
522
523 spin_lock_irqsave(&scc->zlock, flags);
524 if (break_state == -1)
525 zport->regs[5] |= SND_BRK;
526 else
527 zport->regs[5] &= ~SND_BRK;
528 write_zsreg(zport, R5, zport->regs[5]);
529 spin_unlock_irqrestore(&scc->zlock, flags);
530}
531
532
533/*
534 * Interrupt handling routines.
535 */
536#define Rx_BRK 0x0100 /* BREAK event software flag. */
537#define Rx_SYS 0x0200 /* SysRq event software flag. */
538
539static void zs_receive_chars(struct zs_port *zport)
540{
541 struct uart_port *uport = &zport->port;
542 struct zs_scc *scc = zport->scc;
543 struct uart_icount *icount;
544 unsigned int avail, status, ch, flag;
545 int count;
546
547 for (count = 16; count; count--) {
548 spin_lock(&scc->zlock);
549 avail = read_zsreg(zport, R0) & Rx_CH_AV;
550 spin_unlock(&scc->zlock);
551 if (!avail)
552 break;
553
554 spin_lock(&scc->zlock);
555 status = read_zsreg(zport, R1) & (Rx_OVR | FRM_ERR | PAR_ERR);
556 ch = read_zsdata(zport);
557 spin_unlock(&scc->zlock);
558
559 flag = TTY_NORMAL;
560
561 icount = &uport->icount;
562 icount->rx++;
563
564 /* Handle the null char got when BREAK is removed. */
565 if (!ch)
566 status |= zport->tty_break;
567 if (unlikely(status &
568 (Rx_OVR | FRM_ERR | PAR_ERR | Rx_SYS | Rx_BRK))) {
569 zport->tty_break = 0;
570
571 /* Reset the error indication. */
572 if (status & (Rx_OVR | FRM_ERR | PAR_ERR)) {
573 spin_lock(&scc->zlock);
574 write_zsreg(zport, R0, ERR_RES);
575 spin_unlock(&scc->zlock);
576 }
577
578 if (status & (Rx_SYS | Rx_BRK)) {
579 icount->brk++;
580 /* SysRq discards the null char. */
581 if (status & Rx_SYS)
582 continue;
583 } else if (status & FRM_ERR)
584 icount->frame++;
585 else if (status & PAR_ERR)
586 icount->parity++;
587 if (status & Rx_OVR)
588 icount->overrun++;
589
590 status &= uport->read_status_mask;
591 if (status & Rx_BRK)
592 flag = TTY_BREAK;
593 else if (status & FRM_ERR)
594 flag = TTY_FRAME;
595 else if (status & PAR_ERR)
596 flag = TTY_PARITY;
597 }
598
599 if (uart_handle_sysrq_char(uport, ch))
600 continue;
601
602 uart_insert_char(uport, status, Rx_OVR, ch, flag);
603 }
604
605 tty_flip_buffer_push(uport->state->port.tty);
606}
607
608static void zs_raw_transmit_chars(struct zs_port *zport)
609{
610 struct circ_buf *xmit = &zport->port.state->xmit;
611
612 /* XON/XOFF chars. */
613 if (zport->port.x_char) {
614 write_zsdata(zport, zport->port.x_char);
615 zport->port.icount.tx++;
616 zport->port.x_char = 0;
617 return;
618 }
619
620 /* If nothing to do or stopped or hardware stopped. */
621 if (uart_circ_empty(xmit) || uart_tx_stopped(&zport->port)) {
622 zs_raw_stop_tx(zport);
623 return;
624 }
625
626 /* Send char. */
627 write_zsdata(zport, xmit->buf[xmit->tail]);
628 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
629 zport->port.icount.tx++;
630
631 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
632 uart_write_wakeup(&zport->port);
633
634 /* Are we are done? */
635 if (uart_circ_empty(xmit))
636 zs_raw_stop_tx(zport);
637}
638
639static void zs_transmit_chars(struct zs_port *zport)
640{
641 struct zs_scc *scc = zport->scc;
642
643 spin_lock(&scc->zlock);
644 zs_raw_transmit_chars(zport);
645 spin_unlock(&scc->zlock);
646}
647
648static void zs_status_handle(struct zs_port *zport, struct zs_port *zport_a)
649{
650 struct uart_port *uport = &zport->port;
651 struct zs_scc *scc = zport->scc;
652 unsigned int delta;
653 u8 status, brk;
654
655 spin_lock(&scc->zlock);
656
657 /* Get status from Read Register 0. */
658 status = read_zsreg(zport, R0);
659
660 if (zport->regs[15] & BRKIE) {
661 brk = status & BRK_ABRT;
662 if (brk && !zport->brk) {
663 spin_unlock(&scc->zlock);
664 if (uart_handle_break(uport))
665 zport->tty_break = Rx_SYS;
666 else
667 zport->tty_break = Rx_BRK;
668 spin_lock(&scc->zlock);
669 }
670 zport->brk = brk;
671 }
672
673 if (zport != zport_a) {
674 delta = zs_raw_xor_mctrl(zport);
675 spin_unlock(&scc->zlock);
676
677 if (delta & TIOCM_CTS)
678 uart_handle_cts_change(uport,
679 zport->mctrl & TIOCM_CTS);
680 if (delta & TIOCM_CAR)
681 uart_handle_dcd_change(uport,
682 zport->mctrl & TIOCM_CAR);
683 if (delta & TIOCM_RNG)
684 uport->icount.dsr++;
685 if (delta & TIOCM_DSR)
686 uport->icount.rng++;
687
688 if (delta)
689 wake_up_interruptible(&uport->state->port.delta_msr_wait);
690
691 spin_lock(&scc->zlock);
692 }
693
694 /* Clear the status condition... */
695 write_zsreg(zport, R0, RES_EXT_INT);
696
697 spin_unlock(&scc->zlock);
698}
699
700/*
701 * This is the Z85C30 driver's generic interrupt routine.
702 */
703static irqreturn_t zs_interrupt(int irq, void *dev_id)
704{
705 struct zs_scc *scc = dev_id;
706 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
707 struct zs_port *zport_b = &scc->zport[ZS_CHAN_B];
708 irqreturn_t status = IRQ_NONE;
709 u8 zs_intreg;
710 int count;
711
712 /*
713 * NOTE: The read register 3, which holds the irq status,
714 * does so for both channels on each chip. Although
715 * the status value itself must be read from the A
716 * channel and is only valid when read from channel A.
717 * Yes... broken hardware...
718 */
719 for (count = 16; count; count--) {
720 spin_lock(&scc->zlock);
721 zs_intreg = read_zsreg(zport_a, R3);
722 spin_unlock(&scc->zlock);
723 if (!zs_intreg)
724 break;
725
726 /*
727 * We do not like losing characters, so we prioritise
728 * interrupt sources a little bit differently than
729 * the SCC would, was it allowed to.
730 */
731 if (zs_intreg & CHBRxIP)
732 zs_receive_chars(zport_b);
733 if (zs_intreg & CHARxIP)
734 zs_receive_chars(zport_a);
735 if (zs_intreg & CHBEXT)
736 zs_status_handle(zport_b, zport_a);
737 if (zs_intreg & CHAEXT)
738 zs_status_handle(zport_a, zport_a);
739 if (zs_intreg & CHBTxIP)
740 zs_transmit_chars(zport_b);
741 if (zs_intreg & CHATxIP)
742 zs_transmit_chars(zport_a);
743
744 status = IRQ_HANDLED;
745 }
746
747 return status;
748}
749
750
751/*
752 * Finally, routines used to initialize the serial port.
753 */
754static int zs_startup(struct uart_port *uport)
755{
756 struct zs_port *zport = to_zport(uport);
757 struct zs_scc *scc = zport->scc;
758 unsigned long flags;
759 int irq_guard;
760 int ret;
761
762 irq_guard = atomic_add_return(1, &scc->irq_guard);
763 if (irq_guard == 1) {
764 ret = request_irq(zport->port.irq, zs_interrupt,
765 IRQF_SHARED, "scc", scc);
766 if (ret) {
767 atomic_add(-1, &scc->irq_guard);
768 printk(KERN_ERR "zs: can't get irq %d\n",
769 zport->port.irq);
770 return ret;
771 }
772 }
773
774 spin_lock_irqsave(&scc->zlock, flags);
775
776 /* Clear the receive FIFO. */
777 zs_receive_drain(zport);
778
779 /* Clear the interrupt registers. */
780 write_zsreg(zport, R0, ERR_RES);
781 write_zsreg(zport, R0, RES_Tx_P);
782 /* But Ext only if not being handled already. */
783 if (!(zport->regs[1] & EXT_INT_ENAB))
784 write_zsreg(zport, R0, RES_EXT_INT);
785
786 /* Finally, enable sequencing and interrupts. */
787 zport->regs[1] &= ~RxINT_MASK;
788 zport->regs[1] |= RxINT_ALL | TxINT_ENAB | EXT_INT_ENAB;
789 zport->regs[3] |= RxENABLE;
790 zport->regs[15] |= BRKIE;
791 write_zsreg(zport, R1, zport->regs[1]);
792 write_zsreg(zport, R3, zport->regs[3]);
793 write_zsreg(zport, R5, zport->regs[5]);
794 write_zsreg(zport, R15, zport->regs[15]);
795
796 /* Record the current state of RR0. */
797 zport->mctrl = zs_raw_get_mctrl(zport);
798 zport->brk = read_zsreg(zport, R0) & BRK_ABRT;
799
800 zport->tx_stopped = 1;
801
802 spin_unlock_irqrestore(&scc->zlock, flags);
803
804 return 0;
805}
806
807static void zs_shutdown(struct uart_port *uport)
808{
809 struct zs_port *zport = to_zport(uport);
810 struct zs_scc *scc = zport->scc;
811 unsigned long flags;
812 int irq_guard;
813
814 spin_lock_irqsave(&scc->zlock, flags);
815
816 zport->regs[3] &= ~RxENABLE;
817 write_zsreg(zport, R5, zport->regs[5]);
818 write_zsreg(zport, R3, zport->regs[3]);
819
820 spin_unlock_irqrestore(&scc->zlock, flags);
821
822 irq_guard = atomic_add_return(-1, &scc->irq_guard);
823 if (!irq_guard)
824 free_irq(zport->port.irq, scc);
825}
826
827
828static void zs_reset(struct zs_port *zport)
829{
830 struct zs_scc *scc = zport->scc;
831 int irq;
832 unsigned long flags;
833
834 spin_lock_irqsave(&scc->zlock, flags);
835 irq = !irqs_disabled_flags(flags);
836 if (!scc->initialised) {
837 /* Reset the pointer first, just in case... */
838 read_zsreg(zport, R0);
839 /* And let the current transmission finish. */
840 zs_line_drain(zport, irq);
841 write_zsreg(zport, R9, FHWRES);
842 udelay(10);
843 write_zsreg(zport, R9, 0);
844 scc->initialised = 1;
845 }
846 load_zsregs(zport, zport->regs, irq);
847 spin_unlock_irqrestore(&scc->zlock, flags);
848}
849
850static void zs_set_termios(struct uart_port *uport, struct ktermios *termios,
851 struct ktermios *old_termios)
852{
853 struct zs_port *zport = to_zport(uport);
854 struct zs_scc *scc = zport->scc;
855 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
856 int irq;
857 unsigned int baud, brg;
858 unsigned long flags;
859
860 spin_lock_irqsave(&scc->zlock, flags);
861 irq = !irqs_disabled_flags(flags);
862
863 /* Byte size. */
864 zport->regs[3] &= ~RxNBITS_MASK;
865 zport->regs[5] &= ~TxNBITS_MASK;
866 switch (termios->c_cflag & CSIZE) {
867 case CS5:
868 zport->regs[3] |= Rx5;
869 zport->regs[5] |= Tx5;
870 break;
871 case CS6:
872 zport->regs[3] |= Rx6;
873 zport->regs[5] |= Tx6;
874 break;
875 case CS7:
876 zport->regs[3] |= Rx7;
877 zport->regs[5] |= Tx7;
878 break;
879 case CS8:
880 default:
881 zport->regs[3] |= Rx8;
882 zport->regs[5] |= Tx8;
883 break;
884 }
885
886 /* Parity and stop bits. */
887 zport->regs[4] &= ~(XCLK_MASK | SB_MASK | PAR_ENA | PAR_EVEN);
888 if (termios->c_cflag & CSTOPB)
889 zport->regs[4] |= SB2;
890 else
891 zport->regs[4] |= SB1;
892 if (termios->c_cflag & PARENB)
893 zport->regs[4] |= PAR_ENA;
894 if (!(termios->c_cflag & PARODD))
895 zport->regs[4] |= PAR_EVEN;
896 switch (zport->clk_mode) {
897 case 64:
898 zport->regs[4] |= X64CLK;
899 break;
900 case 32:
901 zport->regs[4] |= X32CLK;
902 break;
903 case 16:
904 zport->regs[4] |= X16CLK;
905 break;
906 case 1:
907 zport->regs[4] |= X1CLK;
908 break;
909 default:
910 BUG();
911 }
912
913 baud = uart_get_baud_rate(uport, termios, old_termios, 0,
914 uport->uartclk / zport->clk_mode / 4);
915
916 brg = ZS_BPS_TO_BRG(baud, uport->uartclk / zport->clk_mode);
917 zport->regs[12] = brg & 0xff;
918 zport->regs[13] = (brg >> 8) & 0xff;
919
920 uart_update_timeout(uport, termios->c_cflag, baud);
921
922 uport->read_status_mask = Rx_OVR;
923 if (termios->c_iflag & INPCK)
924 uport->read_status_mask |= FRM_ERR | PAR_ERR;
925 if (termios->c_iflag & (BRKINT | PARMRK))
926 uport->read_status_mask |= Rx_BRK;
927
928 uport->ignore_status_mask = 0;
929 if (termios->c_iflag & IGNPAR)
930 uport->ignore_status_mask |= FRM_ERR | PAR_ERR;
931 if (termios->c_iflag & IGNBRK) {
932 uport->ignore_status_mask |= Rx_BRK;
933 if (termios->c_iflag & IGNPAR)
934 uport->ignore_status_mask |= Rx_OVR;
935 }
936
937 if (termios->c_cflag & CREAD)
938 zport->regs[3] |= RxENABLE;
939 else
940 zport->regs[3] &= ~RxENABLE;
941
942 if (zport != zport_a) {
943 if (!(termios->c_cflag & CLOCAL)) {
944 zport->regs[15] |= DCDIE;
945 } else
946 zport->regs[15] &= ~DCDIE;
947 if (termios->c_cflag & CRTSCTS) {
948 zport->regs[15] |= CTSIE;
949 } else
950 zport->regs[15] &= ~CTSIE;
951 zs_raw_xor_mctrl(zport);
952 }
953
954 /* Load up the new values. */
955 load_zsregs(zport, zport->regs, irq);
956
957 spin_unlock_irqrestore(&scc->zlock, flags);
958}
959
960/*
961 * Hack alert!
962 * Required solely so that the initial PROM-based console
963 * works undisturbed in parallel with this one.
964 */
965static void zs_pm(struct uart_port *uport, unsigned int state,
966 unsigned int oldstate)
967{
968 struct zs_port *zport = to_zport(uport);
969
970 if (state < 3)
971 zport->regs[5] |= TxENAB;
972 else
973 zport->regs[5] &= ~TxENAB;
974 write_zsreg(zport, R5, zport->regs[5]);
975}
976
977
978static const char *zs_type(struct uart_port *uport)
979{
980 return "Z85C30 SCC";
981}
982
983static void zs_release_port(struct uart_port *uport)
984{
985 iounmap(uport->membase);
986 uport->membase = 0;
987 release_mem_region(uport->mapbase, ZS_CHAN_IO_SIZE);
988}
989
990static int zs_map_port(struct uart_port *uport)
991{
992 if (!uport->membase)
993 uport->membase = ioremap_nocache(uport->mapbase,
994 ZS_CHAN_IO_SIZE);
995 if (!uport->membase) {
996 printk(KERN_ERR "zs: Cannot map MMIO\n");
997 return -ENOMEM;
998 }
999 return 0;
1000}
1001
1002static int zs_request_port(struct uart_port *uport)
1003{
1004 int ret;
1005
1006 if (!request_mem_region(uport->mapbase, ZS_CHAN_IO_SIZE, "scc")) {
1007 printk(KERN_ERR "zs: Unable to reserve MMIO resource\n");
1008 return -EBUSY;
1009 }
1010 ret = zs_map_port(uport);
1011 if (ret) {
1012 release_mem_region(uport->mapbase, ZS_CHAN_IO_SIZE);
1013 return ret;
1014 }
1015 return 0;
1016}
1017
1018static void zs_config_port(struct uart_port *uport, int flags)
1019{
1020 struct zs_port *zport = to_zport(uport);
1021
1022 if (flags & UART_CONFIG_TYPE) {
1023 if (zs_request_port(uport))
1024 return;
1025
1026 uport->type = PORT_ZS;
1027
1028 zs_reset(zport);
1029 }
1030}
1031
1032static int zs_verify_port(struct uart_port *uport, struct serial_struct *ser)
1033{
1034 struct zs_port *zport = to_zport(uport);
1035 int ret = 0;
1036
1037 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ZS)
1038 ret = -EINVAL;
1039 if (ser->irq != uport->irq)
1040 ret = -EINVAL;
1041 if (ser->baud_base != uport->uartclk / zport->clk_mode / 4)
1042 ret = -EINVAL;
1043 return ret;
1044}
1045
1046
1047static struct uart_ops zs_ops = {
1048 .tx_empty = zs_tx_empty,
1049 .set_mctrl = zs_set_mctrl,
1050 .get_mctrl = zs_get_mctrl,
1051 .stop_tx = zs_stop_tx,
1052 .start_tx = zs_start_tx,
1053 .stop_rx = zs_stop_rx,
1054 .enable_ms = zs_enable_ms,
1055 .break_ctl = zs_break_ctl,
1056 .startup = zs_startup,
1057 .shutdown = zs_shutdown,
1058 .set_termios = zs_set_termios,
1059 .pm = zs_pm,
1060 .type = zs_type,
1061 .release_port = zs_release_port,
1062 .request_port = zs_request_port,
1063 .config_port = zs_config_port,
1064 .verify_port = zs_verify_port,
1065};
1066
1067/*
1068 * Initialize Z85C30 port structures.
1069 */
1070static int __init zs_probe_sccs(void)
1071{
1072 static int probed;
1073 struct zs_parms zs_parms;
1074 int chip, side, irq;
1075 int n_chips = 0;
1076 int i;
1077
1078 if (probed)
1079 return 0;
1080
1081 irq = dec_interrupt[DEC_IRQ_SCC0];
1082 if (irq >= 0) {
1083 zs_parms.scc[n_chips] = IOASIC_SCC0;
1084 zs_parms.irq[n_chips] = dec_interrupt[DEC_IRQ_SCC0];
1085 n_chips++;
1086 }
1087 irq = dec_interrupt[DEC_IRQ_SCC1];
1088 if (irq >= 0) {
1089 zs_parms.scc[n_chips] = IOASIC_SCC1;
1090 zs_parms.irq[n_chips] = dec_interrupt[DEC_IRQ_SCC1];
1091 n_chips++;
1092 }
1093 if (!n_chips)
1094 return -ENXIO;
1095
1096 probed = 1;
1097
1098 for (chip = 0; chip < n_chips; chip++) {
1099 spin_lock_init(&zs_sccs[chip].zlock);
1100 for (side = 0; side < ZS_NUM_CHAN; side++) {
1101 struct zs_port *zport = &zs_sccs[chip].zport[side];
1102 struct uart_port *uport = &zport->port;
1103
1104 zport->scc = &zs_sccs[chip];
1105 zport->clk_mode = 16;
1106
1107 uport->irq = zs_parms.irq[chip];
1108 uport->uartclk = ZS_CLOCK;
1109 uport->fifosize = 1;
1110 uport->iotype = UPIO_MEM;
1111 uport->flags = UPF_BOOT_AUTOCONF;
1112 uport->ops = &zs_ops;
1113 uport->line = chip * ZS_NUM_CHAN + side;
1114 uport->mapbase = dec_kn_slot_base +
1115 zs_parms.scc[chip] +
1116 (side ^ ZS_CHAN_B) * ZS_CHAN_IO_SIZE;
1117
1118 for (i = 0; i < ZS_NUM_REGS; i++)
1119 zport->regs[i] = zs_init_regs[i];
1120 }
1121 }
1122
1123 return 0;
1124}
1125
1126
1127#ifdef CONFIG_SERIAL_ZS_CONSOLE
1128static void zs_console_putchar(struct uart_port *uport, int ch)
1129{
1130 struct zs_port *zport = to_zport(uport);
1131 struct zs_scc *scc = zport->scc;
1132 int irq;
1133 unsigned long flags;
1134
1135 spin_lock_irqsave(&scc->zlock, flags);
1136 irq = !irqs_disabled_flags(flags);
1137 if (zs_transmit_drain(zport, irq))
1138 write_zsdata(zport, ch);
1139 spin_unlock_irqrestore(&scc->zlock, flags);
1140}
1141
1142/*
1143 * Print a string to the serial port trying not to disturb
1144 * any possible real use of the port...
1145 */
1146static void zs_console_write(struct console *co, const char *s,
1147 unsigned int count)
1148{
1149 int chip = co->index / ZS_NUM_CHAN, side = co->index % ZS_NUM_CHAN;
1150 struct zs_port *zport = &zs_sccs[chip].zport[side];
1151 struct zs_scc *scc = zport->scc;
1152 unsigned long flags;
1153 u8 txint, txenb;
1154 int irq;
1155
1156 /* Disable transmit interrupts and enable the transmitter. */
1157 spin_lock_irqsave(&scc->zlock, flags);
1158 txint = zport->regs[1];
1159 txenb = zport->regs[5];
1160 if (txint & TxINT_ENAB) {
1161 zport->regs[1] = txint & ~TxINT_ENAB;
1162 write_zsreg(zport, R1, zport->regs[1]);
1163 }
1164 if (!(txenb & TxENAB)) {
1165 zport->regs[5] = txenb | TxENAB;
1166 write_zsreg(zport, R5, zport->regs[5]);
1167 }
1168 spin_unlock_irqrestore(&scc->zlock, flags);
1169
1170 uart_console_write(&zport->port, s, count, zs_console_putchar);
1171
1172 /* Restore transmit interrupts and the transmitter enable. */
1173 spin_lock_irqsave(&scc->zlock, flags);
1174 irq = !irqs_disabled_flags(flags);
1175 zs_line_drain(zport, irq);
1176 if (!(txenb & TxENAB)) {
1177 zport->regs[5] &= ~TxENAB;
1178 write_zsreg(zport, R5, zport->regs[5]);
1179 }
1180 if (txint & TxINT_ENAB) {
1181 zport->regs[1] |= TxINT_ENAB;
1182 write_zsreg(zport, R1, zport->regs[1]);
1183 }
1184 spin_unlock_irqrestore(&scc->zlock, flags);
1185}
1186
1187/*
1188 * Setup serial console baud/bits/parity. We do two things here:
1189 * - construct a cflag setting for the first uart_open()
1190 * - initialise the serial port
1191 * Return non-zero if we didn't find a serial port.
1192 */
1193static int __init zs_console_setup(struct console *co, char *options)
1194{
1195 int chip = co->index / ZS_NUM_CHAN, side = co->index % ZS_NUM_CHAN;
1196 struct zs_port *zport = &zs_sccs[chip].zport[side];
1197 struct uart_port *uport = &zport->port;
1198 int baud = 9600;
1199 int bits = 8;
1200 int parity = 'n';
1201 int flow = 'n';
1202 int ret;
1203
1204 ret = zs_map_port(uport);
1205 if (ret)
1206 return ret;
1207
1208 zs_reset(zport);
1209 zs_pm(uport, 0, -1);
1210
1211 if (options)
1212 uart_parse_options(options, &baud, &parity, &bits, &flow);
1213 return uart_set_options(uport, co, baud, parity, bits, flow);
1214}
1215
1216static struct uart_driver zs_reg;
1217static struct console zs_console = {
1218 .name = "ttyS",
1219 .write = zs_console_write,
1220 .device = uart_console_device,
1221 .setup = zs_console_setup,
1222 .flags = CON_PRINTBUFFER,
1223 .index = -1,
1224 .data = &zs_reg,
1225};
1226
1227/*
1228 * Register console.
1229 */
1230static int __init zs_serial_console_init(void)
1231{
1232 int ret;
1233
1234 ret = zs_probe_sccs();
1235 if (ret)
1236 return ret;
1237 register_console(&zs_console);
1238
1239 return 0;
1240}
1241
1242console_initcall(zs_serial_console_init);
1243
1244#define SERIAL_ZS_CONSOLE &zs_console
1245#else
1246#define SERIAL_ZS_CONSOLE NULL
1247#endif /* CONFIG_SERIAL_ZS_CONSOLE */
1248
1249static struct uart_driver zs_reg = {
1250 .owner = THIS_MODULE,
1251 .driver_name = "serial",
1252 .dev_name = "ttyS",
1253 .major = TTY_MAJOR,
1254 .minor = 64,
1255 .nr = ZS_NUM_SCCS * ZS_NUM_CHAN,
1256 .cons = SERIAL_ZS_CONSOLE,
1257};
1258
1259/* zs_init inits the driver. */
1260static int __init zs_init(void)
1261{
1262 int i, ret;
1263
1264 pr_info("%s%s\n", zs_name, zs_version);
1265
1266 /* Find out how many Z85C30 SCCs we have. */
1267 ret = zs_probe_sccs();
1268 if (ret)
1269 return ret;
1270
1271 ret = uart_register_driver(&zs_reg);
1272 if (ret)
1273 return ret;
1274
1275 for (i = 0; i < ZS_NUM_SCCS * ZS_NUM_CHAN; i++) {
1276 struct zs_scc *scc = &zs_sccs[i / ZS_NUM_CHAN];
1277 struct zs_port *zport = &scc->zport[i % ZS_NUM_CHAN];
1278 struct uart_port *uport = &zport->port;
1279
1280 if (zport->scc)
1281 uart_add_one_port(&zs_reg, uport);
1282 }
1283
1284 return 0;
1285}
1286
1287static void __exit zs_exit(void)
1288{
1289 int i;
1290
1291 for (i = ZS_NUM_SCCS * ZS_NUM_CHAN - 1; i >= 0; i--) {
1292 struct zs_scc *scc = &zs_sccs[i / ZS_NUM_CHAN];
1293 struct zs_port *zport = &scc->zport[i % ZS_NUM_CHAN];
1294 struct uart_port *uport = &zport->port;
1295
1296 if (zport->scc)
1297 uart_remove_one_port(&zs_reg, uport);
1298 }
1299
1300 uart_unregister_driver(&zs_reg);
1301}
1302
1303module_init(zs_init);
1304module_exit(zs_exit);
diff --git a/drivers/tty/serial/zs.h b/drivers/tty/serial/zs.h
new file mode 100644
index 000000000000..aa921b57d827
--- /dev/null
+++ b/drivers/tty/serial/zs.h
@@ -0,0 +1,284 @@
1/*
2 * zs.h: Definitions for the DECstation Z85C30 serial driver.
3 *
4 * Adapted from drivers/sbus/char/sunserial.h by Paul Mackerras.
5 * Adapted from drivers/macintosh/macserial.h by Harald Koerfgen.
6 *
7 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
8 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
9 * Copyright (C) 2004, 2005, 2007 Maciej W. Rozycki
10 */
11#ifndef _SERIAL_ZS_H
12#define _SERIAL_ZS_H
13
14#ifdef __KERNEL__
15
16#define ZS_NUM_REGS 16
17
18/*
19 * This is our internal structure for each serial port's state.
20 */
21struct zs_port {
22 struct zs_scc *scc; /* Containing SCC. */
23 struct uart_port port; /* Underlying UART. */
24
25 int clk_mode; /* May be 1, 16, 32, or 64. */
26
27 unsigned int tty_break; /* Set on BREAK condition. */
28 int tx_stopped; /* Output is suspended. */
29
30 unsigned int mctrl; /* State of modem lines. */
31 u8 brk; /* BREAK state from RR0. */
32
33 u8 regs[ZS_NUM_REGS]; /* Channel write registers. */
34};
35
36/*
37 * Per-SCC state for locking and the interrupt handler.
38 */
39struct zs_scc {
40 struct zs_port zport[2];
41 spinlock_t zlock;
42 atomic_t irq_guard;
43 int initialised;
44};
45
46#endif /* __KERNEL__ */
47
48/*
49 * Conversion routines to/from brg time constants from/to bits per second.
50 */
51#define ZS_BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
52#define ZS_BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
53
54/*
55 * The Zilog register set.
56 */
57
58/* Write Register 0 (Command) */
59#define R0 0 /* Register selects */
60#define R1 1
61#define R2 2
62#define R3 3
63#define R4 4
64#define R5 5
65#define R6 6
66#define R7 7
67#define R8 8
68#define R9 9
69#define R10 10
70#define R11 11
71#define R12 12
72#define R13 13
73#define R14 14
74#define R15 15
75
76#define NULLCODE 0 /* Null Code */
77#define POINT_HIGH 0x8 /* Select upper half of registers */
78#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
79#define SEND_ABORT 0x18 /* HDLC Abort */
80#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
81#define RES_Tx_P 0x28 /* Reset TxINT Pending */
82#define ERR_RES 0x30 /* Error Reset */
83#define RES_H_IUS 0x38 /* Reset highest IUS */
84
85#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
86#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
87#define RES_EOM_L 0xC0 /* Reset EOM latch */
88
89/* Write Register 1 (Tx/Rx/Ext Int Enable and WAIT/DMA Commands) */
90#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
91#define TxINT_ENAB 0x2 /* Tx Int Enable */
92#define PAR_SPEC 0x4 /* Parity is special condition */
93
94#define RxINT_DISAB 0 /* Rx Int Disable */
95#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
96#define RxINT_ALL 0x10 /* Int on all Rx Characters or error */
97#define RxINT_ERR 0x18 /* Int on error only */
98#define RxINT_MASK 0x18
99
100#define WT_RDY_RT 0x20 /* Wait/Ready on R/T */
101#define WT_FN_RDYFN 0x40 /* Wait/FN/Ready FN */
102#define WT_RDY_ENAB 0x80 /* Wait/Ready Enable */
103
104/* Write Register 2 (Interrupt Vector) */
105
106/* Write Register 3 (Receive Parameters and Control) */
107#define RxENABLE 0x1 /* Rx Enable */
108#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
109#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
110#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
111#define ENT_HM 0x10 /* Enter Hunt Mode */
112#define AUTO_ENAB 0x20 /* Auto Enables */
113#define Rx5 0x0 /* Rx 5 Bits/Character */
114#define Rx7 0x40 /* Rx 7 Bits/Character */
115#define Rx6 0x80 /* Rx 6 Bits/Character */
116#define Rx8 0xc0 /* Rx 8 Bits/Character */
117#define RxNBITS_MASK 0xc0
118
119/* Write Register 4 (Transmit/Receive Miscellaneous Parameters and Modes) */
120#define PAR_ENA 0x1 /* Parity Enable */
121#define PAR_EVEN 0x2 /* Parity Even/Odd* */
122
123#define SYNC_ENAB 0 /* Sync Modes Enable */
124#define SB1 0x4 /* 1 stop bit/char */
125#define SB15 0x8 /* 1.5 stop bits/char */
126#define SB2 0xc /* 2 stop bits/char */
127#define SB_MASK 0xc
128
129#define MONSYNC 0 /* 8 Bit Sync character */
130#define BISYNC 0x10 /* 16 bit sync character */
131#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
132#define EXTSYNC 0x30 /* External Sync Mode */
133
134#define X1CLK 0x0 /* x1 clock mode */
135#define X16CLK 0x40 /* x16 clock mode */
136#define X32CLK 0x80 /* x32 clock mode */
137#define X64CLK 0xc0 /* x64 clock mode */
138#define XCLK_MASK 0xc0
139
140/* Write Register 5 (Transmit Parameters and Controls) */
141#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
142#define RTS 0x2 /* RTS */
143#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
144#define TxENAB 0x8 /* Tx Enable */
145#define SND_BRK 0x10 /* Send Break */
146#define Tx5 0x0 /* Tx 5 bits (or less)/character */
147#define Tx7 0x20 /* Tx 7 bits/character */
148#define Tx6 0x40 /* Tx 6 bits/character */
149#define Tx8 0x60 /* Tx 8 bits/character */
150#define TxNBITS_MASK 0x60
151#define DTR 0x80 /* DTR */
152
153/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
154
155/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
156
157/* Write Register 8 (Transmit Buffer) */
158
159/* Write Register 9 (Master Interrupt Control) */
160#define VIS 1 /* Vector Includes Status */
161#define NV 2 /* No Vector */
162#define DLC 4 /* Disable Lower Chain */
163#define MIE 8 /* Master Interrupt Enable */
164#define STATHI 0x10 /* Status high */
165#define SOFTACK 0x20 /* Software Interrupt Acknowledge */
166#define NORESET 0 /* No reset on write to R9 */
167#define CHRB 0x40 /* Reset channel B */
168#define CHRA 0x80 /* Reset channel A */
169#define FHWRES 0xc0 /* Force hardware reset */
170
171/* Write Register 10 (Miscellaneous Transmitter/Receiver Control Bits) */
172#define BIT6 1 /* 6 bit/8bit sync */
173#define LOOPMODE 2 /* SDLC Loop mode */
174#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
175#define MARKIDLE 8 /* Mark/flag on idle */
176#define GAOP 0x10 /* Go active on poll */
177#define NRZ 0 /* NRZ mode */
178#define NRZI 0x20 /* NRZI mode */
179#define FM1 0x40 /* FM1 (transition = 1) */
180#define FM0 0x60 /* FM0 (transition = 0) */
181#define CRCPS 0x80 /* CRC Preset I/O */
182
183/* Write Register 11 (Clock Mode Control) */
184#define TRxCXT 0 /* TRxC = Xtal output */
185#define TRxCTC 1 /* TRxC = Transmit clock */
186#define TRxCBR 2 /* TRxC = BR Generator Output */
187#define TRxCDP 3 /* TRxC = DPLL output */
188#define TRxCOI 4 /* TRxC O/I */
189#define TCRTxCP 0 /* Transmit clock = RTxC pin */
190#define TCTRxCP 8 /* Transmit clock = TRxC pin */
191#define TCBR 0x10 /* Transmit clock = BR Generator output */
192#define TCDPLL 0x18 /* Transmit clock = DPLL output */
193#define RCRTxCP 0 /* Receive clock = RTxC pin */
194#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
195#define RCBR 0x40 /* Receive clock = BR Generator output */
196#define RCDPLL 0x60 /* Receive clock = DPLL output */
197#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
198
199/* Write Register 12 (Lower Byte of Baud Rate Generator Time Constant) */
200
201/* Write Register 13 (Upper Byte of Baud Rate Generator Time Constant) */
202
203/* Write Register 14 (Miscellaneous Control Bits) */
204#define BRENABL 1 /* Baud rate generator enable */
205#define BRSRC 2 /* Baud rate generator source */
206#define DTRREQ 4 /* DTR/Request function */
207#define AUTOECHO 8 /* Auto Echo */
208#define LOOPBAK 0x10 /* Local loopback */
209#define SEARCH 0x20 /* Enter search mode */
210#define RMC 0x40 /* Reset missing clock */
211#define DISDPLL 0x60 /* Disable DPLL */
212#define SSBR 0x80 /* Set DPLL source = BR generator */
213#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
214#define SFMM 0xc0 /* Set FM mode */
215#define SNRZI 0xe0 /* Set NRZI mode */
216
217/* Write Register 15 (External/Status Interrupt Control) */
218#define WR7P_EN 1 /* WR7 Prime SDLC Feature Enable */
219#define ZCIE 2 /* Zero count IE */
220#define DCDIE 8 /* DCD IE */
221#define SYNCIE 0x10 /* Sync/hunt IE */
222#define CTSIE 0x20 /* CTS IE */
223#define TxUIE 0x40 /* Tx Underrun/EOM IE */
224#define BRKIE 0x80 /* Break/Abort IE */
225
226
227/* Read Register 0 (Transmit/Receive Buffer Status and External Status) */
228#define Rx_CH_AV 0x1 /* Rx Character Available */
229#define ZCOUNT 0x2 /* Zero count */
230#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
231#define DCD 0x8 /* DCD */
232#define SYNC_HUNT 0x10 /* Sync/hunt */
233#define CTS 0x20 /* CTS */
234#define TxEOM 0x40 /* Tx underrun */
235#define BRK_ABRT 0x80 /* Break/Abort */
236
237/* Read Register 1 (Special Receive Condition Status) */
238#define ALL_SNT 0x1 /* All sent */
239/* Residue Data for 8 Rx bits/char programmed */
240#define RES3 0x8 /* 0/3 */
241#define RES4 0x4 /* 0/4 */
242#define RES5 0xc /* 0/5 */
243#define RES6 0x2 /* 0/6 */
244#define RES7 0xa /* 0/7 */
245#define RES8 0x6 /* 0/8 */
246#define RES18 0xe /* 1/8 */
247#define RES28 0x0 /* 2/8 */
248/* Special Rx Condition Interrupts */
249#define PAR_ERR 0x10 /* Parity Error */
250#define Rx_OVR 0x20 /* Rx Overrun Error */
251#define FRM_ERR 0x40 /* CRC/Framing Error */
252#define END_FR 0x80 /* End of Frame (SDLC) */
253
254/* Read Register 2 (Interrupt Vector (WR2) -- channel A). */
255
256/* Read Register 2 (Modified Interrupt Vector -- channel B). */
257
258/* Read Register 3 (Interrupt Pending Bits -- channel A only). */
259#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
260#define CHBTxIP 0x2 /* Channel B Tx IP */
261#define CHBRxIP 0x4 /* Channel B Rx IP */
262#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
263#define CHATxIP 0x10 /* Channel A Tx IP */
264#define CHARxIP 0x20 /* Channel A Rx IP */
265
266/* Read Register 6 (SDLC FIFO Status and Byte Count LSB) */
267
268/* Read Register 7 (SDLC FIFO Status and Byte Count MSB) */
269
270/* Read Register 8 (Receive Data) */
271
272/* Read Register 10 (Miscellaneous Status Bits) */
273#define ONLOOP 2 /* On loop */
274#define LOOPSEND 0x10 /* Loop sending */
275#define CLK2MIS 0x40 /* Two clocks missing */
276#define CLK1MIS 0x80 /* One clock missing */
277
278/* Read Register 12 (Lower Byte of Baud Rate Generator Constant (WR12)) */
279
280/* Read Register 13 (Upper Byte of Baud Rate Generator Constant (WR13) */
281
282/* Read Register 15 (External/Status Interrupt Control (WR15)) */
283
284#endif /* _SERIAL_ZS_H */